spi-bcm53xx.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  2. #include <linux/kernel.h>
  3. #include <linux/module.h>
  4. #include <linux/slab.h>
  5. #include <linux/delay.h>
  6. #include <linux/bcma/bcma.h>
  7. #include <linux/spi/spi.h>
  8. #include "spi-bcm53xx.h"
  9. #define BCM53XXSPI_MAX_SPI_BAUD 13500000 /* 216 MHz? */
  10. /* The longest observed required wait was 19 ms */
  11. #define BCM53XXSPI_SPE_TIMEOUT_MS 80
  12. struct bcm53xxspi {
  13. struct bcma_device *core;
  14. struct spi_master *master;
  15. size_t read_offset;
  16. };
  17. static inline u32 bcm53xxspi_read(struct bcm53xxspi *b53spi, u16 offset)
  18. {
  19. return bcma_read32(b53spi->core, offset);
  20. }
  21. static inline void bcm53xxspi_write(struct bcm53xxspi *b53spi, u16 offset,
  22. u32 value)
  23. {
  24. bcma_write32(b53spi->core, offset, value);
  25. }
  26. static inline unsigned int bcm53xxspi_calc_timeout(size_t len)
  27. {
  28. /* Do some magic calculation based on length and buad. Add 10% and 1. */
  29. return (len * 9000 / BCM53XXSPI_MAX_SPI_BAUD * 110 / 100) + 1;
  30. }
  31. static int bcm53xxspi_wait(struct bcm53xxspi *b53spi, unsigned int timeout_ms)
  32. {
  33. unsigned long deadline;
  34. u32 tmp;
  35. /* SPE bit has to be 0 before we read MSPI STATUS */
  36. deadline = jiffies + msecs_to_jiffies(BCM53XXSPI_SPE_TIMEOUT_MS);
  37. do {
  38. tmp = bcm53xxspi_read(b53spi, B53SPI_MSPI_SPCR2);
  39. if (!(tmp & B53SPI_MSPI_SPCR2_SPE))
  40. break;
  41. udelay(5);
  42. } while (!time_after_eq(jiffies, deadline));
  43. if (tmp & B53SPI_MSPI_SPCR2_SPE)
  44. goto spi_timeout;
  45. /* Check status */
  46. deadline = jiffies + msecs_to_jiffies(timeout_ms);
  47. do {
  48. tmp = bcm53xxspi_read(b53spi, B53SPI_MSPI_MSPI_STATUS);
  49. if (tmp & B53SPI_MSPI_MSPI_STATUS_SPIF) {
  50. bcm53xxspi_write(b53spi, B53SPI_MSPI_MSPI_STATUS, 0);
  51. return 0;
  52. }
  53. cpu_relax();
  54. udelay(100);
  55. } while (!time_after_eq(jiffies, deadline));
  56. spi_timeout:
  57. bcm53xxspi_write(b53spi, B53SPI_MSPI_MSPI_STATUS, 0);
  58. pr_err("Timeout waiting for SPI to be ready!\n");
  59. return -EBUSY;
  60. }
  61. static void bcm53xxspi_buf_write(struct bcm53xxspi *b53spi, u8 *w_buf,
  62. size_t len, bool cont)
  63. {
  64. u32 tmp;
  65. int i;
  66. for (i = 0; i < len; i++) {
  67. /* Transmit Register File MSB */
  68. bcm53xxspi_write(b53spi, B53SPI_MSPI_TXRAM + 4 * (i * 2),
  69. (unsigned int)w_buf[i]);
  70. }
  71. for (i = 0; i < len; i++) {
  72. tmp = B53SPI_CDRAM_CONT | B53SPI_CDRAM_PCS_DISABLE_ALL |
  73. B53SPI_CDRAM_PCS_DSCK;
  74. if (!cont && i == len - 1)
  75. tmp &= ~B53SPI_CDRAM_CONT;
  76. tmp &= ~0x1;
  77. /* Command Register File */
  78. bcm53xxspi_write(b53spi, B53SPI_MSPI_CDRAM + 4 * i, tmp);
  79. }
  80. /* Set queue pointers */
  81. bcm53xxspi_write(b53spi, B53SPI_MSPI_NEWQP, 0);
  82. bcm53xxspi_write(b53spi, B53SPI_MSPI_ENDQP, len - 1);
  83. if (cont)
  84. bcm53xxspi_write(b53spi, B53SPI_MSPI_WRITE_LOCK, 1);
  85. /* Start SPI transfer */
  86. tmp = bcm53xxspi_read(b53spi, B53SPI_MSPI_SPCR2);
  87. tmp |= B53SPI_MSPI_SPCR2_SPE;
  88. if (cont)
  89. tmp |= B53SPI_MSPI_SPCR2_CONT_AFTER_CMD;
  90. bcm53xxspi_write(b53spi, B53SPI_MSPI_SPCR2, tmp);
  91. /* Wait for SPI to finish */
  92. bcm53xxspi_wait(b53spi, bcm53xxspi_calc_timeout(len));
  93. if (!cont)
  94. bcm53xxspi_write(b53spi, B53SPI_MSPI_WRITE_LOCK, 0);
  95. b53spi->read_offset = len;
  96. }
  97. static void bcm53xxspi_buf_read(struct bcm53xxspi *b53spi, u8 *r_buf,
  98. size_t len, bool cont)
  99. {
  100. u32 tmp;
  101. int i;
  102. for (i = 0; i < b53spi->read_offset + len; i++) {
  103. tmp = B53SPI_CDRAM_CONT | B53SPI_CDRAM_PCS_DISABLE_ALL |
  104. B53SPI_CDRAM_PCS_DSCK;
  105. if (!cont && i == b53spi->read_offset + len - 1)
  106. tmp &= ~B53SPI_CDRAM_CONT;
  107. tmp &= ~0x1;
  108. /* Command Register File */
  109. bcm53xxspi_write(b53spi, B53SPI_MSPI_CDRAM + 4 * i, tmp);
  110. }
  111. /* Set queue pointers */
  112. bcm53xxspi_write(b53spi, B53SPI_MSPI_NEWQP, 0);
  113. bcm53xxspi_write(b53spi, B53SPI_MSPI_ENDQP,
  114. b53spi->read_offset + len - 1);
  115. if (cont)
  116. bcm53xxspi_write(b53spi, B53SPI_MSPI_WRITE_LOCK, 1);
  117. /* Start SPI transfer */
  118. tmp = bcm53xxspi_read(b53spi, B53SPI_MSPI_SPCR2);
  119. tmp |= B53SPI_MSPI_SPCR2_SPE;
  120. if (cont)
  121. tmp |= B53SPI_MSPI_SPCR2_CONT_AFTER_CMD;
  122. bcm53xxspi_write(b53spi, B53SPI_MSPI_SPCR2, tmp);
  123. /* Wait for SPI to finish */
  124. bcm53xxspi_wait(b53spi, bcm53xxspi_calc_timeout(len));
  125. if (!cont)
  126. bcm53xxspi_write(b53spi, B53SPI_MSPI_WRITE_LOCK, 0);
  127. for (i = 0; i < len; ++i) {
  128. int offset = b53spi->read_offset + i;
  129. /* Data stored in the transmit register file LSB */
  130. r_buf[i] = (u8)bcm53xxspi_read(b53spi, B53SPI_MSPI_RXRAM + 4 * (1 + offset * 2));
  131. }
  132. b53spi->read_offset = 0;
  133. }
  134. static int bcm53xxspi_transfer_one(struct spi_master *master,
  135. struct spi_device *spi,
  136. struct spi_transfer *t)
  137. {
  138. struct bcm53xxspi *b53spi = spi_master_get_devdata(master);
  139. u8 *buf;
  140. size_t left;
  141. if (t->tx_buf) {
  142. buf = (u8 *)t->tx_buf;
  143. left = t->len;
  144. while (left) {
  145. size_t to_write = min_t(size_t, 16, left);
  146. bool cont = left - to_write > 0;
  147. bcm53xxspi_buf_write(b53spi, buf, to_write, cont);
  148. left -= to_write;
  149. buf += to_write;
  150. }
  151. }
  152. if (t->rx_buf) {
  153. buf = (u8 *)t->rx_buf;
  154. left = t->len;
  155. while (left) {
  156. size_t to_read = min_t(size_t, 16 - b53spi->read_offset,
  157. left);
  158. bool cont = left - to_read > 0;
  159. bcm53xxspi_buf_read(b53spi, buf, to_read, cont);
  160. left -= to_read;
  161. buf += to_read;
  162. }
  163. }
  164. return 0;
  165. }
  166. /**************************************************
  167. * BCMA
  168. **************************************************/
  169. static struct spi_board_info bcm53xx_info = {
  170. .modalias = "bcm53xxspiflash",
  171. };
  172. static const struct bcma_device_id bcm53xxspi_bcma_tbl[] = {
  173. BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_NS_QSPI, BCMA_ANY_REV, BCMA_ANY_CLASS),
  174. {},
  175. };
  176. MODULE_DEVICE_TABLE(bcma, bcm53xxspi_bcma_tbl);
  177. static int bcm53xxspi_bcma_probe(struct bcma_device *core)
  178. {
  179. struct bcm53xxspi *b53spi;
  180. struct spi_master *master;
  181. int err;
  182. if (core->bus->drv_cc.core->id.rev != 42) {
  183. pr_err("SPI on SoC with unsupported ChipCommon rev\n");
  184. return -ENOTSUPP;
  185. }
  186. master = spi_alloc_master(&core->dev, sizeof(*b53spi));
  187. if (!master)
  188. return -ENOMEM;
  189. b53spi = spi_master_get_devdata(master);
  190. b53spi->master = master;
  191. b53spi->core = core;
  192. master->transfer_one = bcm53xxspi_transfer_one;
  193. bcma_set_drvdata(core, b53spi);
  194. err = devm_spi_register_master(&core->dev, master);
  195. if (err) {
  196. spi_master_put(master);
  197. bcma_set_drvdata(core, NULL);
  198. return err;
  199. }
  200. /* Broadcom SoCs (at least with the CC rev 42) use SPI for flash only */
  201. spi_new_device(master, &bcm53xx_info);
  202. return 0;
  203. }
  204. static struct bcma_driver bcm53xxspi_bcma_driver = {
  205. .name = KBUILD_MODNAME,
  206. .id_table = bcm53xxspi_bcma_tbl,
  207. .probe = bcm53xxspi_bcma_probe,
  208. };
  209. /**************************************************
  210. * Init & exit
  211. **************************************************/
  212. static int __init bcm53xxspi_module_init(void)
  213. {
  214. int err = 0;
  215. err = bcma_driver_register(&bcm53xxspi_bcma_driver);
  216. if (err)
  217. pr_err("Failed to register bcma driver: %d\n", err);
  218. return err;
  219. }
  220. static void __exit bcm53xxspi_module_exit(void)
  221. {
  222. bcma_driver_unregister(&bcm53xxspi_bcma_driver);
  223. }
  224. module_init(bcm53xxspi_module_init);
  225. module_exit(bcm53xxspi_module_exit);
  226. MODULE_DESCRIPTION("Broadcom BCM53xx SPI Controller driver");
  227. MODULE_AUTHOR("Rafał Miłecki <zajec5@gmail.com>");
  228. MODULE_LICENSE("GPL");