rk3288_spi.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. *
  4. * Copyright (C) 2012 Google Inc.
  5. * Copyright (C) 2016 Free Software Foundation, Inc.
  6. *
  7. * This is based on depthcharge code.
  8. *
  9. * GRUB is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * GRUB is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include <grub/mm.h>
  23. #include <grub/time.h>
  24. #include <grub/misc.h>
  25. #include <grub/fdtbus.h>
  26. #include <grub/machine/kernel.h>
  27. static grub_err_t
  28. spi_send (const struct grub_fdtbus_dev *dev, const void *data, grub_size_t sz)
  29. {
  30. const grub_uint8_t *ptr = data, *end = ptr + sz;
  31. volatile grub_uint32_t *spi = grub_fdtbus_map_reg (dev, 0, 0);
  32. spi[2] = 0;
  33. spi[1] = sz - 1;
  34. spi[0] = ((1 << 18) | spi[0]) & ~(1 << 19);
  35. spi[2] = 1;
  36. while (ptr < end)
  37. {
  38. while (spi[9] & 2);
  39. spi[256] = *ptr++;
  40. }
  41. while (spi[9] & 1);
  42. return GRUB_ERR_NONE;
  43. }
  44. static grub_err_t
  45. spi_receive (const struct grub_fdtbus_dev *dev, void *data, grub_size_t sz)
  46. {
  47. grub_uint8_t *ptr = data, *end = ptr + sz;
  48. volatile grub_uint32_t *spi = grub_fdtbus_map_reg (dev, 0, 0);
  49. spi[2] = 0;
  50. spi[1] = sz - 1;
  51. spi[0] = ((1 << 19) | spi[0]) & ~(1 << 18);
  52. spi[2] = 1;
  53. while (ptr < end)
  54. {
  55. while (spi[9] & 8);
  56. *ptr++ = spi[512];
  57. }
  58. while (spi[9] & 1);
  59. return GRUB_ERR_NONE;
  60. }
  61. static grub_err_t
  62. spi_start (const struct grub_fdtbus_dev *dev)
  63. {
  64. volatile grub_uint32_t *spi = grub_fdtbus_map_reg (dev, 0, 0);
  65. spi[3] = 1;
  66. return GRUB_ERR_NONE;
  67. }
  68. static void
  69. spi_stop (const struct grub_fdtbus_dev *dev)
  70. {
  71. volatile grub_uint32_t *spi = grub_fdtbus_map_reg (dev, 0, 0);
  72. spi[3] = 0;
  73. }
  74. static grub_err_t
  75. spi_attach(const struct grub_fdtbus_dev *dev)
  76. {
  77. if (!grub_fdtbus_is_mapping_valid (grub_fdtbus_map_reg (dev, 0, 0)))
  78. return GRUB_ERR_IO;
  79. return GRUB_ERR_NONE;
  80. }
  81. static struct grub_fdtbus_driver spi =
  82. {
  83. .compatible = "rockchip,rk3288-spi",
  84. .attach = spi_attach,
  85. .send = spi_send,
  86. .receive = spi_receive,
  87. .start = spi_start,
  88. .stop = spi_stop,
  89. };
  90. void
  91. grub_rk3288_spi_init (void)
  92. {
  93. grub_fdtbus_register (&spi);
  94. }