spi-bcm53xx.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. #define BCM53XXSPI_FLASH_WINDOW SZ_32M
  11. /* The longest observed required wait was 19 ms */
  12. #define BCM53XXSPI_SPE_TIMEOUT_MS 80
  13. struct bcm53xxspi {
  14. struct bcma_device *core;
  15. struct spi_master *master;
  16. void __iomem *mmio_base;
  17. size_t read_offset;
  18. bool bspi; /* Boot SPI mode with memory mapping */
  19. };
  20. static inline u32 bcm53xxspi_read(struct bcm53xxspi *b53spi, u16 offset)
  21. {
  22. return bcma_read32(b53spi->core, offset);
  23. }
  24. static inline void bcm53xxspi_write(struct bcm53xxspi *b53spi, u16 offset,
  25. u32 value)
  26. {
  27. bcma_write32(b53spi->core, offset, value);
  28. }
  29. static void bcm53xxspi_disable_bspi(struct bcm53xxspi *b53spi)
  30. {
  31. struct device *dev = &b53spi->core->dev;
  32. unsigned long deadline;
  33. u32 tmp;
  34. if (!b53spi->bspi)
  35. return;
  36. tmp = bcm53xxspi_read(b53spi, B53SPI_BSPI_MAST_N_BOOT_CTRL);
  37. if (tmp & 0x1)
  38. return;
  39. deadline = jiffies + usecs_to_jiffies(200);
  40. do {
  41. tmp = bcm53xxspi_read(b53spi, B53SPI_BSPI_BUSY_STATUS);
  42. if (!(tmp & 0x1)) {
  43. bcm53xxspi_write(b53spi, B53SPI_BSPI_MAST_N_BOOT_CTRL,
  44. 0x1);
  45. ndelay(200);
  46. b53spi->bspi = false;
  47. return;
  48. }
  49. udelay(1);
  50. } while (!time_after_eq(jiffies, deadline));
  51. dev_warn(dev, "Timeout disabling BSPI\n");
  52. }
  53. static void bcm53xxspi_enable_bspi(struct bcm53xxspi *b53spi)
  54. {
  55. u32 tmp;
  56. if (b53spi->bspi)
  57. return;
  58. tmp = bcm53xxspi_read(b53spi, B53SPI_BSPI_MAST_N_BOOT_CTRL);
  59. if (!(tmp & 0x1))
  60. return;
  61. bcm53xxspi_write(b53spi, B53SPI_BSPI_MAST_N_BOOT_CTRL, 0x0);
  62. b53spi->bspi = true;
  63. }
  64. static inline unsigned int bcm53xxspi_calc_timeout(size_t len)
  65. {
  66. /* Do some magic calculation based on length and buad. Add 10% and 1. */
  67. return (len * 9000 / BCM53XXSPI_MAX_SPI_BAUD * 110 / 100) + 1;
  68. }
  69. static int bcm53xxspi_wait(struct bcm53xxspi *b53spi, unsigned int timeout_ms)
  70. {
  71. unsigned long deadline;
  72. u32 tmp;
  73. /* SPE bit has to be 0 before we read MSPI STATUS */
  74. deadline = jiffies + msecs_to_jiffies(BCM53XXSPI_SPE_TIMEOUT_MS);
  75. do {
  76. tmp = bcm53xxspi_read(b53spi, B53SPI_MSPI_SPCR2);
  77. if (!(tmp & B53SPI_MSPI_SPCR2_SPE))
  78. break;
  79. udelay(5);
  80. } while (!time_after_eq(jiffies, deadline));
  81. if (tmp & B53SPI_MSPI_SPCR2_SPE)
  82. goto spi_timeout;
  83. /* Check status */
  84. deadline = jiffies + msecs_to_jiffies(timeout_ms);
  85. do {
  86. tmp = bcm53xxspi_read(b53spi, B53SPI_MSPI_MSPI_STATUS);
  87. if (tmp & B53SPI_MSPI_MSPI_STATUS_SPIF) {
  88. bcm53xxspi_write(b53spi, B53SPI_MSPI_MSPI_STATUS, 0);
  89. return 0;
  90. }
  91. cpu_relax();
  92. udelay(100);
  93. } while (!time_after_eq(jiffies, deadline));
  94. spi_timeout:
  95. bcm53xxspi_write(b53spi, B53SPI_MSPI_MSPI_STATUS, 0);
  96. pr_err("Timeout waiting for SPI to be ready!\n");
  97. return -EBUSY;
  98. }
  99. static void bcm53xxspi_buf_write(struct bcm53xxspi *b53spi, u8 *w_buf,
  100. size_t len, bool cont)
  101. {
  102. u32 tmp;
  103. int i;
  104. for (i = 0; i < len; i++) {
  105. /* Transmit Register File MSB */
  106. bcm53xxspi_write(b53spi, B53SPI_MSPI_TXRAM + 4 * (i * 2),
  107. (unsigned int)w_buf[i]);
  108. }
  109. for (i = 0; i < len; i++) {
  110. tmp = B53SPI_CDRAM_CONT | B53SPI_CDRAM_PCS_DISABLE_ALL |
  111. B53SPI_CDRAM_PCS_DSCK;
  112. if (!cont && i == len - 1)
  113. tmp &= ~B53SPI_CDRAM_CONT;
  114. tmp &= ~0x1;
  115. /* Command Register File */
  116. bcm53xxspi_write(b53spi, B53SPI_MSPI_CDRAM + 4 * i, tmp);
  117. }
  118. /* Set queue pointers */
  119. bcm53xxspi_write(b53spi, B53SPI_MSPI_NEWQP, 0);
  120. bcm53xxspi_write(b53spi, B53SPI_MSPI_ENDQP, len - 1);
  121. if (cont)
  122. bcm53xxspi_write(b53spi, B53SPI_MSPI_WRITE_LOCK, 1);
  123. /* Start SPI transfer */
  124. tmp = bcm53xxspi_read(b53spi, B53SPI_MSPI_SPCR2);
  125. tmp |= B53SPI_MSPI_SPCR2_SPE;
  126. if (cont)
  127. tmp |= B53SPI_MSPI_SPCR2_CONT_AFTER_CMD;
  128. bcm53xxspi_write(b53spi, B53SPI_MSPI_SPCR2, tmp);
  129. /* Wait for SPI to finish */
  130. bcm53xxspi_wait(b53spi, bcm53xxspi_calc_timeout(len));
  131. if (!cont)
  132. bcm53xxspi_write(b53spi, B53SPI_MSPI_WRITE_LOCK, 0);
  133. b53spi->read_offset = len;
  134. }
  135. static void bcm53xxspi_buf_read(struct bcm53xxspi *b53spi, u8 *r_buf,
  136. size_t len, bool cont)
  137. {
  138. u32 tmp;
  139. int i;
  140. for (i = 0; i < b53spi->read_offset + len; i++) {
  141. tmp = B53SPI_CDRAM_CONT | B53SPI_CDRAM_PCS_DISABLE_ALL |
  142. B53SPI_CDRAM_PCS_DSCK;
  143. if (!cont && i == b53spi->read_offset + len - 1)
  144. tmp &= ~B53SPI_CDRAM_CONT;
  145. tmp &= ~0x1;
  146. /* Command Register File */
  147. bcm53xxspi_write(b53spi, B53SPI_MSPI_CDRAM + 4 * i, tmp);
  148. }
  149. /* Set queue pointers */
  150. bcm53xxspi_write(b53spi, B53SPI_MSPI_NEWQP, 0);
  151. bcm53xxspi_write(b53spi, B53SPI_MSPI_ENDQP,
  152. b53spi->read_offset + len - 1);
  153. if (cont)
  154. bcm53xxspi_write(b53spi, B53SPI_MSPI_WRITE_LOCK, 1);
  155. /* Start SPI transfer */
  156. tmp = bcm53xxspi_read(b53spi, B53SPI_MSPI_SPCR2);
  157. tmp |= B53SPI_MSPI_SPCR2_SPE;
  158. if (cont)
  159. tmp |= B53SPI_MSPI_SPCR2_CONT_AFTER_CMD;
  160. bcm53xxspi_write(b53spi, B53SPI_MSPI_SPCR2, tmp);
  161. /* Wait for SPI to finish */
  162. bcm53xxspi_wait(b53spi, bcm53xxspi_calc_timeout(len));
  163. if (!cont)
  164. bcm53xxspi_write(b53spi, B53SPI_MSPI_WRITE_LOCK, 0);
  165. for (i = 0; i < len; ++i) {
  166. int offset = b53spi->read_offset + i;
  167. /* Data stored in the transmit register file LSB */
  168. r_buf[i] = (u8)bcm53xxspi_read(b53spi, B53SPI_MSPI_RXRAM + 4 * (1 + offset * 2));
  169. }
  170. b53spi->read_offset = 0;
  171. }
  172. static int bcm53xxspi_transfer_one(struct spi_master *master,
  173. struct spi_device *spi,
  174. struct spi_transfer *t)
  175. {
  176. struct bcm53xxspi *b53spi = spi_master_get_devdata(master);
  177. u8 *buf;
  178. size_t left;
  179. bcm53xxspi_disable_bspi(b53spi);
  180. if (t->tx_buf) {
  181. buf = (u8 *)t->tx_buf;
  182. left = t->len;
  183. while (left) {
  184. size_t to_write = min_t(size_t, 16, left);
  185. bool cont = left - to_write > 0;
  186. bcm53xxspi_buf_write(b53spi, buf, to_write, cont);
  187. left -= to_write;
  188. buf += to_write;
  189. }
  190. }
  191. if (t->rx_buf) {
  192. buf = (u8 *)t->rx_buf;
  193. left = t->len;
  194. while (left) {
  195. size_t to_read = min_t(size_t, 16 - b53spi->read_offset,
  196. left);
  197. bool cont = left - to_read > 0;
  198. bcm53xxspi_buf_read(b53spi, buf, to_read, cont);
  199. left -= to_read;
  200. buf += to_read;
  201. }
  202. }
  203. return 0;
  204. }
  205. static int bcm53xxspi_flash_read(struct spi_device *spi,
  206. struct spi_flash_read_message *msg)
  207. {
  208. struct bcm53xxspi *b53spi = spi_master_get_devdata(spi->master);
  209. int ret = 0;
  210. if (msg->from + msg->len > BCM53XXSPI_FLASH_WINDOW)
  211. return -EINVAL;
  212. bcm53xxspi_enable_bspi(b53spi);
  213. memcpy_fromio(msg->buf, b53spi->mmio_base + msg->from, msg->len);
  214. msg->retlen = msg->len;
  215. return ret;
  216. }
  217. /**************************************************
  218. * BCMA
  219. **************************************************/
  220. static struct spi_board_info bcm53xx_info = {
  221. .modalias = "bcm53xxspiflash",
  222. };
  223. static const struct bcma_device_id bcm53xxspi_bcma_tbl[] = {
  224. BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_NS_QSPI, BCMA_ANY_REV, BCMA_ANY_CLASS),
  225. {},
  226. };
  227. MODULE_DEVICE_TABLE(bcma, bcm53xxspi_bcma_tbl);
  228. static int bcm53xxspi_bcma_probe(struct bcma_device *core)
  229. {
  230. struct device *dev = &core->dev;
  231. struct bcm53xxspi *b53spi;
  232. struct spi_master *master;
  233. int err;
  234. if (core->bus->drv_cc.core->id.rev != 42) {
  235. pr_err("SPI on SoC with unsupported ChipCommon rev\n");
  236. return -ENOTSUPP;
  237. }
  238. master = spi_alloc_master(dev, sizeof(*b53spi));
  239. if (!master)
  240. return -ENOMEM;
  241. b53spi = spi_master_get_devdata(master);
  242. b53spi->master = master;
  243. b53spi->core = core;
  244. if (core->addr_s[0])
  245. b53spi->mmio_base = devm_ioremap(dev, core->addr_s[0],
  246. BCM53XXSPI_FLASH_WINDOW);
  247. b53spi->bspi = true;
  248. bcm53xxspi_disable_bspi(b53spi);
  249. master->transfer_one = bcm53xxspi_transfer_one;
  250. if (b53spi->mmio_base)
  251. master->spi_flash_read = bcm53xxspi_flash_read;
  252. bcma_set_drvdata(core, b53spi);
  253. err = devm_spi_register_master(dev, master);
  254. if (err) {
  255. spi_master_put(master);
  256. bcma_set_drvdata(core, NULL);
  257. return err;
  258. }
  259. /* Broadcom SoCs (at least with the CC rev 42) use SPI for flash only */
  260. spi_new_device(master, &bcm53xx_info);
  261. return 0;
  262. }
  263. static struct bcma_driver bcm53xxspi_bcma_driver = {
  264. .name = KBUILD_MODNAME,
  265. .id_table = bcm53xxspi_bcma_tbl,
  266. .probe = bcm53xxspi_bcma_probe,
  267. };
  268. /**************************************************
  269. * Init & exit
  270. **************************************************/
  271. static int __init bcm53xxspi_module_init(void)
  272. {
  273. int err = 0;
  274. err = bcma_driver_register(&bcm53xxspi_bcma_driver);
  275. if (err)
  276. pr_err("Failed to register bcma driver: %d\n", err);
  277. return err;
  278. }
  279. static void __exit bcm53xxspi_module_exit(void)
  280. {
  281. bcma_driver_unregister(&bcm53xxspi_bcma_driver);
  282. }
  283. module_init(bcm53xxspi_module_init);
  284. module_exit(bcm53xxspi_module_exit);
  285. MODULE_DESCRIPTION("Broadcom BCM53xx SPI Controller driver");
  286. MODULE_AUTHOR("Rafał Miłecki <[email protected]>");
  287. MODULE_LICENSE("GPL");