spi-bcm53xx.c 8.7 KB

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