bcmstb_spi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2018 Cisco Systems, Inc.
  4. *
  5. * Author: Thomas Fitzsimmons <fitzsim@fitzsim.org>
  6. */
  7. #include <asm/global_data.h>
  8. #include <asm/io.h>
  9. #include <command.h>
  10. #include <config.h>
  11. #include <dm.h>
  12. #include <errno.h>
  13. #include <fdtdec.h>
  14. #include <linux/bitops.h>
  15. #include <linux/delay.h>
  16. #include <log.h>
  17. #include <malloc.h>
  18. #include <spi.h>
  19. #include <time.h>
  20. DECLARE_GLOBAL_DATA_PTR;
  21. #define SPBR_MIN 8
  22. #define BITS_PER_WORD 8
  23. #define NUM_TXRAM 32
  24. #define NUM_RXRAM 32
  25. #define NUM_CDRAM 16
  26. /* hif_mspi register structure. */
  27. struct bcmstb_hif_mspi_regs {
  28. u32 spcr0_lsb; /* 0x000 */
  29. u32 spcr0_msb; /* 0x004 */
  30. u32 spcr1_lsb; /* 0x008 */
  31. u32 spcr1_msb; /* 0x00c */
  32. u32 newqp; /* 0x010 */
  33. u32 endqp; /* 0x014 */
  34. u32 spcr2; /* 0x018 */
  35. u32 reserved0; /* 0x01c */
  36. u32 mspi_status; /* 0x020 */
  37. u32 cptqp; /* 0x024 */
  38. u32 spcr3; /* 0x028 */
  39. u32 revision; /* 0x02c */
  40. u32 reserved1[4]; /* 0x030 */
  41. u32 txram[NUM_TXRAM]; /* 0x040 */
  42. u32 rxram[NUM_RXRAM]; /* 0x0c0 */
  43. u32 cdram[NUM_CDRAM]; /* 0x140 */
  44. u32 write_lock; /* 0x180 */
  45. };
  46. /* hif_mspi masks. */
  47. #define HIF_MSPI_SPCR2_CONT_AFTER_CMD_MASK 0x00000080
  48. #define HIF_MSPI_SPCR2_SPE_MASK 0x00000040
  49. #define HIF_MSPI_SPCR2_SPIFIE_MASK 0x00000020
  50. #define HIF_MSPI_WRITE_LOCK_WRITE_LOCK_MASK 0x00000001
  51. /* bspi offsets. */
  52. #define BSPI_MAST_N_BOOT_CTRL 0x008
  53. /* bspi_raf is not used in this driver. */
  54. /* hif_spi_intr2 offsets and masks. */
  55. #define HIF_SPI_INTR2_CPU_CLEAR 0x08
  56. #define HIF_SPI_INTR2_CPU_MASK_SET 0x10
  57. #define HIF_SPI_INTR2_CPU_MASK_CLEAR 0x14
  58. #define HIF_SPI_INTR2_CPU_SET_MSPI_DONE_MASK 0x00000020
  59. /* SPI transfer timeout in milliseconds. */
  60. #define HIF_MSPI_WAIT 10
  61. enum bcmstb_base_type {
  62. HIF_MSPI,
  63. BSPI,
  64. HIF_SPI_INTR2,
  65. CS_REG,
  66. BASE_LAST,
  67. };
  68. struct bcmstb_spi_plat {
  69. void *base[4];
  70. };
  71. struct bcmstb_spi_priv {
  72. struct bcmstb_hif_mspi_regs *regs;
  73. void *bspi;
  74. void *hif_spi_intr2;
  75. void *cs_reg;
  76. int default_cs;
  77. int curr_cs;
  78. uint tx_slot;
  79. uint rx_slot;
  80. u8 saved_cmd[NUM_CDRAM];
  81. uint saved_cmd_len;
  82. void *saved_din_addr;
  83. };
  84. static int bcmstb_spi_of_to_plat(struct udevice *bus)
  85. {
  86. struct bcmstb_spi_plat *plat = dev_get_plat(bus);
  87. const void *fdt = gd->fdt_blob;
  88. int node = dev_of_offset(bus);
  89. int ret = 0;
  90. int i = 0;
  91. struct fdt_resource resource = { 0 };
  92. char *names[BASE_LAST] = { "hif_mspi", "bspi", "hif_spi_intr2",
  93. "cs_reg" };
  94. const phys_addr_t defaults[BASE_LAST] = { BCMSTB_HIF_MSPI_BASE,
  95. BCMSTB_BSPI_BASE,
  96. BCMSTB_HIF_SPI_INTR2,
  97. BCMSTB_CS_REG };
  98. for (i = 0; i < BASE_LAST; i++) {
  99. plat->base[i] = (void *)defaults[i];
  100. ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
  101. names[i], &resource);
  102. if (ret) {
  103. printf("%s: Assuming BCMSTB SPI %s address 0x0x%p\n",
  104. __func__, names[i], (void *)defaults[i]);
  105. } else {
  106. plat->base[i] = (void *)resource.start;
  107. debug("BCMSTB SPI %s address: 0x0x%p\n",
  108. names[i], (void *)plat->base[i]);
  109. }
  110. }
  111. return 0;
  112. }
  113. static void bcmstb_spi_hw_set_parms(struct bcmstb_spi_priv *priv)
  114. {
  115. writel(SPBR_MIN, &priv->regs->spcr0_lsb);
  116. writel(BITS_PER_WORD << 2 | SPI_MODE_3, &priv->regs->spcr0_msb);
  117. }
  118. static void bcmstb_spi_enable_interrupt(void *base, u32 mask)
  119. {
  120. void *reg = base + HIF_SPI_INTR2_CPU_MASK_CLEAR;
  121. writel(readl(reg) | mask, reg);
  122. readl(reg);
  123. }
  124. static void bcmstb_spi_disable_interrupt(void *base, u32 mask)
  125. {
  126. void *reg = base + HIF_SPI_INTR2_CPU_MASK_SET;
  127. writel(readl(reg) | mask, reg);
  128. readl(reg);
  129. }
  130. static void bcmstb_spi_clear_interrupt(void *base, u32 mask)
  131. {
  132. void *reg = base + HIF_SPI_INTR2_CPU_CLEAR;
  133. writel(readl(reg) | mask, reg);
  134. readl(reg);
  135. }
  136. static int bcmstb_spi_probe(struct udevice *bus)
  137. {
  138. struct bcmstb_spi_plat *plat = dev_get_plat(bus);
  139. struct bcmstb_spi_priv *priv = dev_get_priv(bus);
  140. priv->regs = plat->base[HIF_MSPI];
  141. priv->bspi = plat->base[BSPI];
  142. priv->hif_spi_intr2 = plat->base[HIF_SPI_INTR2];
  143. priv->cs_reg = plat->base[CS_REG];
  144. priv->default_cs = 0;
  145. priv->curr_cs = -1;
  146. priv->tx_slot = 0;
  147. priv->rx_slot = 0;
  148. memset(priv->saved_cmd, 0, NUM_CDRAM);
  149. priv->saved_cmd_len = 0;
  150. priv->saved_din_addr = NULL;
  151. debug("spi_xfer: tx regs: 0x%p\n", &priv->regs->txram[0]);
  152. debug("spi_xfer: rx regs: 0x%p\n", &priv->regs->rxram[0]);
  153. /* Disable BSPI. */
  154. writel(1, priv->bspi + BSPI_MAST_N_BOOT_CTRL);
  155. readl(priv->bspi + BSPI_MAST_N_BOOT_CTRL);
  156. /* Set up interrupts. */
  157. bcmstb_spi_disable_interrupt(priv->hif_spi_intr2, 0xffffffff);
  158. bcmstb_spi_clear_interrupt(priv->hif_spi_intr2, 0xffffffff);
  159. bcmstb_spi_enable_interrupt(priv->hif_spi_intr2,
  160. HIF_SPI_INTR2_CPU_SET_MSPI_DONE_MASK);
  161. /* Set up control registers. */
  162. writel(0, &priv->regs->spcr1_lsb);
  163. writel(0, &priv->regs->spcr1_msb);
  164. writel(0, &priv->regs->newqp);
  165. writel(0, &priv->regs->endqp);
  166. writel(HIF_MSPI_SPCR2_SPIFIE_MASK, &priv->regs->spcr2);
  167. writel(0, &priv->regs->spcr3);
  168. bcmstb_spi_hw_set_parms(priv);
  169. return 0;
  170. }
  171. static void bcmstb_spi_submit(struct bcmstb_spi_priv *priv, bool done)
  172. {
  173. debug("WR NEWQP: %d\n", 0);
  174. writel(0, &priv->regs->newqp);
  175. debug("WR ENDQP: %d\n", priv->tx_slot - 1);
  176. writel(priv->tx_slot - 1, &priv->regs->endqp);
  177. if (done) {
  178. debug("WR CDRAM[%d]: %02x\n", priv->tx_slot - 1,
  179. readl(&priv->regs->cdram[priv->tx_slot - 1]) & ~0x80);
  180. writel(readl(&priv->regs->cdram[priv->tx_slot - 1]) & ~0x80,
  181. &priv->regs->cdram[priv->tx_slot - 1]);
  182. }
  183. /* Force chip select first time. */
  184. if (priv->curr_cs != priv->default_cs) {
  185. debug("spi_xfer: switching chip select to %d\n",
  186. priv->default_cs);
  187. writel((readl(priv->cs_reg) & ~0xff) | (1 << priv->default_cs),
  188. priv->cs_reg);
  189. readl(priv->cs_reg);
  190. udelay(10);
  191. priv->curr_cs = priv->default_cs;
  192. }
  193. debug("WR WRITE_LOCK: %02x\n", 1);
  194. writel((readl(&priv->regs->write_lock) &
  195. ~HIF_MSPI_WRITE_LOCK_WRITE_LOCK_MASK) | 1,
  196. &priv->regs->write_lock);
  197. readl(&priv->regs->write_lock);
  198. debug("WR SPCR2: %02x\n",
  199. HIF_MSPI_SPCR2_SPIFIE_MASK |
  200. HIF_MSPI_SPCR2_SPE_MASK |
  201. HIF_MSPI_SPCR2_CONT_AFTER_CMD_MASK);
  202. writel(HIF_MSPI_SPCR2_SPIFIE_MASK |
  203. HIF_MSPI_SPCR2_SPE_MASK |
  204. HIF_MSPI_SPCR2_CONT_AFTER_CMD_MASK,
  205. &priv->regs->spcr2);
  206. }
  207. static int bcmstb_spi_wait(struct bcmstb_spi_priv *priv)
  208. {
  209. u32 start_time = get_timer(0);
  210. u32 status = readl(&priv->regs->mspi_status);
  211. while (!(status & 1)) {
  212. if (get_timer(start_time) > HIF_MSPI_WAIT)
  213. return -ETIMEDOUT;
  214. status = readl(&priv->regs->mspi_status);
  215. }
  216. writel(readl(&priv->regs->mspi_status) & ~1, &priv->regs->mspi_status);
  217. bcmstb_spi_clear_interrupt(priv->hif_spi_intr2,
  218. HIF_SPI_INTR2_CPU_SET_MSPI_DONE_MASK);
  219. return 0;
  220. }
  221. static int bcmstb_spi_xfer(struct udevice *dev, unsigned int bitlen,
  222. const void *dout, void *din, unsigned long flags)
  223. {
  224. uint len = bitlen / 8;
  225. uint tx_len = len;
  226. uint rx_len = len;
  227. const u8 *out_bytes = (u8 *)dout;
  228. u8 *in_bytes = (u8 *)din;
  229. struct udevice *bus = dev_get_parent(dev);
  230. struct bcmstb_spi_priv *priv = dev_get_priv(bus);
  231. struct bcmstb_hif_mspi_regs *regs = priv->regs;
  232. debug("spi_xfer: %d, t: 0x%p, r: 0x%p, f: %lx\n",
  233. len, dout, din, flags);
  234. debug("spi_xfer: chip select: %x\n", readl(priv->cs_reg) & 0xff);
  235. debug("spi_xfer: tx addr: 0x%p\n", &regs->txram[0]);
  236. debug("spi_xfer: rx addr: 0x%p\n", &regs->rxram[0]);
  237. debug("spi_xfer: cd addr: 0x%p\n", &regs->cdram[0]);
  238. if (flags & SPI_XFER_END) {
  239. debug("spi_xfer: clearing saved din address: 0x%p\n",
  240. priv->saved_din_addr);
  241. priv->saved_din_addr = NULL;
  242. priv->saved_cmd_len = 0;
  243. memset(priv->saved_cmd, 0, NUM_CDRAM);
  244. }
  245. if (bitlen == 0)
  246. return 0;
  247. if (bitlen % 8) {
  248. printf("%s: Non-byte-aligned transfer\n", __func__);
  249. return -EOPNOTSUPP;
  250. }
  251. if (flags & ~(SPI_XFER_BEGIN | SPI_XFER_END)) {
  252. printf("%s: Unsupported flags: %lx\n", __func__, flags);
  253. return -EOPNOTSUPP;
  254. }
  255. if (flags & SPI_XFER_BEGIN) {
  256. priv->tx_slot = 0;
  257. priv->rx_slot = 0;
  258. if (out_bytes && len > NUM_CDRAM) {
  259. printf("%s: Unable to save transfer\n", __func__);
  260. return -EOPNOTSUPP;
  261. }
  262. if (out_bytes && !(flags & SPI_XFER_END)) {
  263. /*
  264. * This is the start of a transmit operation
  265. * that will need repeating if the calling
  266. * code polls for the result. Save it for
  267. * subsequent transmission.
  268. */
  269. debug("spi_xfer: saving command: %x, %d\n",
  270. out_bytes[0], len);
  271. priv->saved_cmd_len = len;
  272. memcpy(priv->saved_cmd, out_bytes, priv->saved_cmd_len);
  273. }
  274. }
  275. if (!(flags & (SPI_XFER_BEGIN | SPI_XFER_END))) {
  276. if (priv->saved_din_addr == din) {
  277. /*
  278. * The caller is polling for status. Repeat
  279. * the last transmission.
  280. */
  281. int ret = 0;
  282. debug("spi_xfer: Making recursive call\n");
  283. ret = bcmstb_spi_xfer(dev, priv->saved_cmd_len * 8,
  284. priv->saved_cmd, NULL,
  285. SPI_XFER_BEGIN);
  286. if (ret) {
  287. printf("%s: Recursive call failed\n", __func__);
  288. return ret;
  289. }
  290. } else {
  291. debug("spi_xfer: saving din address: 0x%p\n", din);
  292. priv->saved_din_addr = din;
  293. }
  294. }
  295. while (rx_len > 0) {
  296. priv->rx_slot = priv->tx_slot;
  297. while (priv->tx_slot < NUM_CDRAM && tx_len > 0) {
  298. bcmstb_spi_hw_set_parms(priv);
  299. debug("WR TXRAM[%d]: %02x\n", priv->tx_slot,
  300. out_bytes ? out_bytes[len - tx_len] : 0xff);
  301. writel(out_bytes ? out_bytes[len - tx_len] : 0xff,
  302. &regs->txram[priv->tx_slot << 1]);
  303. debug("WR CDRAM[%d]: %02x\n", priv->tx_slot, 0x8e);
  304. writel(0x8e, &regs->cdram[priv->tx_slot]);
  305. priv->tx_slot++;
  306. tx_len--;
  307. if (!in_bytes)
  308. rx_len--;
  309. }
  310. debug("spi_xfer: early return clauses: %d, %d, %d\n",
  311. len <= NUM_CDRAM,
  312. !in_bytes,
  313. (flags & (SPI_XFER_BEGIN |
  314. SPI_XFER_END)) == SPI_XFER_BEGIN);
  315. if (len <= NUM_CDRAM &&
  316. !in_bytes &&
  317. (flags & (SPI_XFER_BEGIN | SPI_XFER_END)) == SPI_XFER_BEGIN)
  318. return 0;
  319. bcmstb_spi_submit(priv, tx_len == 0);
  320. if (bcmstb_spi_wait(priv) == -ETIMEDOUT) {
  321. printf("%s: Timed out\n", __func__);
  322. return -ETIMEDOUT;
  323. }
  324. priv->tx_slot %= NUM_CDRAM;
  325. if (in_bytes) {
  326. while (priv->rx_slot < NUM_CDRAM && rx_len > 0) {
  327. in_bytes[len - rx_len] =
  328. readl(&regs->rxram[(priv->rx_slot << 1)
  329. + 1])
  330. & 0xff;
  331. debug("RD RXRAM[%d]: %02x\n",
  332. priv->rx_slot, in_bytes[len - rx_len]);
  333. priv->rx_slot++;
  334. rx_len--;
  335. }
  336. }
  337. }
  338. if (flags & SPI_XFER_END) {
  339. debug("WR WRITE_LOCK: %02x\n", 0);
  340. writel((readl(&priv->regs->write_lock) &
  341. ~HIF_MSPI_WRITE_LOCK_WRITE_LOCK_MASK) | 0,
  342. &priv->regs->write_lock);
  343. readl(&priv->regs->write_lock);
  344. }
  345. return 0;
  346. }
  347. static int bcmstb_spi_set_speed(struct udevice *dev, uint speed)
  348. {
  349. return 0;
  350. }
  351. static int bcmstb_spi_set_mode(struct udevice *dev, uint mode)
  352. {
  353. return 0;
  354. }
  355. static const struct dm_spi_ops bcmstb_spi_ops = {
  356. .xfer = bcmstb_spi_xfer,
  357. .set_speed = bcmstb_spi_set_speed,
  358. .set_mode = bcmstb_spi_set_mode,
  359. };
  360. static const struct udevice_id bcmstb_spi_id[] = {
  361. { .compatible = "brcm,spi-brcmstb" },
  362. { }
  363. };
  364. U_BOOT_DRIVER(bcmstb_spi) = {
  365. .name = "bcmstb_spi",
  366. .id = UCLASS_SPI,
  367. .of_match = bcmstb_spi_id,
  368. .ops = &bcmstb_spi_ops,
  369. .of_to_plat = bcmstb_spi_of_to_plat,
  370. .probe = bcmstb_spi_probe,
  371. .plat_auto = sizeof(struct bcmstb_spi_plat),
  372. .priv_auto = sizeof(struct bcmstb_spi_priv),
  373. };