bcm63xx_hsspi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * Copyright (C) 2017 Álvaro Fernández Rojas <noltari@gmail.com>
  3. *
  4. * Derived from linux/drivers/spi/spi-bcm63xx-hsspi.c:
  5. * Copyright (C) 2000-2010 Broadcom Corporation
  6. * Copyright (C) 2012-2013 Jonas Gorski <jogo@openwrt.org>
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <clk.h>
  12. #include <dm.h>
  13. #include <spi.h>
  14. #include <reset.h>
  15. #include <wait_bit.h>
  16. #include <asm/io.h>
  17. DECLARE_GLOBAL_DATA_PTR;
  18. #define HSSPI_PP 0
  19. #define SPI_MAX_SYNC_CLOCK 30000000
  20. /* SPI Control register */
  21. #define SPI_CTL_REG 0x000
  22. #define SPI_CTL_CS_POL_SHIFT 0
  23. #define SPI_CTL_CS_POL_MASK (0xff << SPI_CTL_CS_POL_SHIFT)
  24. #define SPI_CTL_CLK_GATE_SHIFT 16
  25. #define SPI_CTL_CLK_GATE_MASK (1 << SPI_CTL_CLK_GATE_SHIFT)
  26. #define SPI_CTL_CLK_POL_SHIFT 17
  27. #define SPI_CTL_CLK_POL_MASK (1 << SPI_CTL_CLK_POL_SHIFT)
  28. /* SPI Interrupts registers */
  29. #define SPI_IR_STAT_REG 0x008
  30. #define SPI_IR_ST_MASK_REG 0x00c
  31. #define SPI_IR_MASK_REG 0x010
  32. #define SPI_IR_CLEAR_ALL 0xff001f1f
  33. /* SPI Ping-Pong Command registers */
  34. #define SPI_CMD_REG (0x080 + (0x40 * (HSSPI_PP)) + 0x00)
  35. #define SPI_CMD_OP_SHIFT 0
  36. #define SPI_CMD_OP_START (0x1 << SPI_CMD_OP_SHIFT)
  37. #define SPI_CMD_PFL_SHIFT 8
  38. #define SPI_CMD_PFL_MASK (0x7 << SPI_CMD_PFL_SHIFT)
  39. #define SPI_CMD_SLAVE_SHIFT 12
  40. #define SPI_CMD_SLAVE_MASK (0x7 << SPI_CMD_SLAVE_SHIFT)
  41. /* SPI Ping-Pong Status registers */
  42. #define SPI_STAT_REG (0x080 + (0x40 * (HSSPI_PP)) + 0x04)
  43. #define SPI_STAT_SRCBUSY_SHIFT 1
  44. #define SPI_STAT_SRCBUSY_MASK (1 << SPI_STAT_SRCBUSY_SHIFT)
  45. /* SPI Profile Clock registers */
  46. #define SPI_PFL_CLK_REG(x) (0x100 + (0x20 * (x)) + 0x00)
  47. #define SPI_PFL_CLK_FREQ_SHIFT 0
  48. #define SPI_PFL_CLK_FREQ_MASK (0x3fff << SPI_PFL_CLK_FREQ_SHIFT)
  49. #define SPI_PFL_CLK_RSTLOOP_SHIFT 15
  50. #define SPI_PFL_CLK_RSTLOOP_MASK (1 << SPI_PFL_CLK_RSTLOOP_SHIFT)
  51. /* SPI Profile Signal registers */
  52. #define SPI_PFL_SIG_REG(x) (0x100 + (0x20 * (x)) + 0x04)
  53. #define SPI_PFL_SIG_LATCHRIS_SHIFT 12
  54. #define SPI_PFL_SIG_LATCHRIS_MASK (1 << SPI_PFL_SIG_LATCHRIS_SHIFT)
  55. #define SPI_PFL_SIG_LAUNCHRIS_SHIFT 13
  56. #define SPI_PFL_SIG_LAUNCHRIS_MASK (1 << SPI_PFL_SIG_LAUNCHRIS_SHIFT)
  57. #define SPI_PFL_SIG_ASYNCIN_SHIFT 16
  58. #define SPI_PFL_SIG_ASYNCIN_MASK (1 << SPI_PFL_SIG_ASYNCIN_SHIFT)
  59. /* SPI Profile Mode registers */
  60. #define SPI_PFL_MODE_REG(x) (0x100 + (0x20 * (x)) + 0x08)
  61. #define SPI_PFL_MODE_FILL_SHIFT 0
  62. #define SPI_PFL_MODE_FILL_MASK (0xff << SPI_PFL_MODE_FILL_SHIFT)
  63. #define SPI_PFL_MODE_MDRDSZ_SHIFT 16
  64. #define SPI_PFL_MODE_MDRDSZ_MASK (1 << SPI_PFL_MODE_MDRDSZ_SHIFT)
  65. #define SPI_PFL_MODE_MDWRSZ_SHIFT 18
  66. #define SPI_PFL_MODE_MDWRSZ_MASK (1 << SPI_PFL_MODE_MDWRSZ_SHIFT)
  67. #define SPI_PFL_MODE_3WIRE_SHIFT 20
  68. #define SPI_PFL_MODE_3WIRE_MASK (1 << SPI_PFL_MODE_3WIRE_SHIFT)
  69. /* SPI Ping-Pong FIFO registers */
  70. #define HSSPI_FIFO_SIZE 0x200
  71. #define HSSPI_FIFO_BASE (0x200 + \
  72. (HSSPI_FIFO_SIZE * HSSPI_PP))
  73. /* SPI Ping-Pong FIFO OP register */
  74. #define HSSPI_FIFO_OP_SIZE 0x2
  75. #define HSSPI_FIFO_OP_REG (HSSPI_FIFO_BASE + 0x00)
  76. #define HSSPI_FIFO_OP_BYTES_SHIFT 0
  77. #define HSSPI_FIFO_OP_BYTES_MASK (0x3ff << HSSPI_FIFO_OP_BYTES_SHIFT)
  78. #define HSSPI_FIFO_OP_MBIT_SHIFT 11
  79. #define HSSPI_FIFO_OP_MBIT_MASK (1 << HSSPI_FIFO_OP_MBIT_SHIFT)
  80. #define HSSPI_FIFO_OP_CODE_SHIFT 13
  81. #define HSSPI_FIFO_OP_READ_WRITE (1 << HSSPI_FIFO_OP_CODE_SHIFT)
  82. #define HSSPI_FIFO_OP_CODE_W (2 << HSSPI_FIFO_OP_CODE_SHIFT)
  83. #define HSSPI_FIFO_OP_CODE_R (3 << HSSPI_FIFO_OP_CODE_SHIFT)
  84. struct bcm63xx_hsspi_priv {
  85. void __iomem *regs;
  86. ulong clk_rate;
  87. uint8_t num_cs;
  88. uint8_t cs_pols;
  89. uint speed;
  90. };
  91. static int bcm63xx_hsspi_cs_info(struct udevice *bus, uint cs,
  92. struct spi_cs_info *info)
  93. {
  94. struct bcm63xx_hsspi_priv *priv = dev_get_priv(bus);
  95. if (cs >= priv->num_cs) {
  96. printf("no cs %u\n", cs);
  97. return -ENODEV;
  98. }
  99. return 0;
  100. }
  101. static int bcm63xx_hsspi_set_mode(struct udevice *bus, uint mode)
  102. {
  103. struct bcm63xx_hsspi_priv *priv = dev_get_priv(bus);
  104. /* clock polarity */
  105. if (mode & SPI_CPOL)
  106. setbits_be32(priv->regs + SPI_CTL_REG, SPI_CTL_CLK_POL_MASK);
  107. else
  108. clrbits_be32(priv->regs + SPI_CTL_REG, SPI_CTL_CLK_POL_MASK);
  109. return 0;
  110. }
  111. static int bcm63xx_hsspi_set_speed(struct udevice *bus, uint speed)
  112. {
  113. struct bcm63xx_hsspi_priv *priv = dev_get_priv(bus);
  114. priv->speed = speed;
  115. return 0;
  116. }
  117. static void bcm63xx_hsspi_activate_cs(struct bcm63xx_hsspi_priv *priv,
  118. struct dm_spi_slave_platdata *plat)
  119. {
  120. uint32_t clr, set;
  121. /* profile clock */
  122. set = DIV_ROUND_UP(priv->clk_rate, priv->speed);
  123. set = DIV_ROUND_UP(2048, set);
  124. set &= SPI_PFL_CLK_FREQ_MASK;
  125. set |= SPI_PFL_CLK_RSTLOOP_MASK;
  126. writel_be(set, priv->regs + SPI_PFL_CLK_REG(plat->cs));
  127. /* profile signal */
  128. set = 0;
  129. clr = SPI_PFL_SIG_LAUNCHRIS_MASK |
  130. SPI_PFL_SIG_LATCHRIS_MASK |
  131. SPI_PFL_SIG_ASYNCIN_MASK;
  132. /* latch/launch config */
  133. if (plat->mode & SPI_CPHA)
  134. set |= SPI_PFL_SIG_LAUNCHRIS_MASK;
  135. else
  136. set |= SPI_PFL_SIG_LATCHRIS_MASK;
  137. /* async clk */
  138. if (priv->speed > SPI_MAX_SYNC_CLOCK)
  139. set |= SPI_PFL_SIG_ASYNCIN_MASK;
  140. clrsetbits_be32(priv->regs + SPI_PFL_SIG_REG(plat->cs), clr, set);
  141. /* global control */
  142. set = 0;
  143. clr = 0;
  144. /* invert cs polarity */
  145. if (priv->cs_pols & BIT(plat->cs))
  146. clr |= BIT(plat->cs);
  147. else
  148. set |= BIT(plat->cs);
  149. /* invert dummy cs polarity */
  150. if (priv->cs_pols & BIT(!plat->cs))
  151. clr |= BIT(!plat->cs);
  152. else
  153. set |= BIT(!plat->cs);
  154. clrsetbits_be32(priv->regs + SPI_CTL_REG, clr, set);
  155. }
  156. static void bcm63xx_hsspi_deactivate_cs(struct bcm63xx_hsspi_priv *priv)
  157. {
  158. /* restore cs polarities */
  159. clrsetbits_be32(priv->regs + SPI_CTL_REG, SPI_CTL_CS_POL_MASK,
  160. priv->cs_pols);
  161. }
  162. /*
  163. * BCM63xx HSSPI driver doesn't allow keeping CS active between transfers
  164. * because they are controlled by HW.
  165. * However, it provides a mechanism to prepend write transfers prior to read
  166. * transfers (with a maximum prepend of 15 bytes), which is usually enough for
  167. * SPI-connected flashes since reading requires prepending a write transfer of
  168. * 5 bytes. On the other hand it also provides a way to invert each CS
  169. * polarity, not only between transfers like the older BCM63xx SPI driver, but
  170. * also the rest of the time.
  171. *
  172. * Instead of using the prepend mechanism, this implementation inverts the
  173. * polarity of both the desired CS and another dummy CS when the bus is
  174. * claimed. This way, the dummy CS is restored to its inactive value when
  175. * transfers are issued and the desired CS is preserved in its active value
  176. * all the time. This hack is also used in the upstream linux driver and
  177. * allows keeping CS active between trasnfers even if the HW doesn't give
  178. * this possibility.
  179. */
  180. static int bcm63xx_hsspi_xfer(struct udevice *dev, unsigned int bitlen,
  181. const void *dout, void *din, unsigned long flags)
  182. {
  183. struct bcm63xx_hsspi_priv *priv = dev_get_priv(dev->parent);
  184. struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
  185. size_t data_bytes = bitlen / 8;
  186. size_t step_size = HSSPI_FIFO_SIZE;
  187. uint16_t opcode = 0;
  188. uint32_t val;
  189. const uint8_t *tx = dout;
  190. uint8_t *rx = din;
  191. if (flags & SPI_XFER_BEGIN)
  192. bcm63xx_hsspi_activate_cs(priv, plat);
  193. /* fifo operation */
  194. if (tx && rx)
  195. opcode = HSSPI_FIFO_OP_READ_WRITE;
  196. else if (rx)
  197. opcode = HSSPI_FIFO_OP_CODE_R;
  198. else if (tx)
  199. opcode = HSSPI_FIFO_OP_CODE_W;
  200. if (opcode != HSSPI_FIFO_OP_CODE_R)
  201. step_size -= HSSPI_FIFO_OP_SIZE;
  202. /* dual mode */
  203. if ((opcode == HSSPI_FIFO_OP_CODE_R && plat->mode == SPI_RX_DUAL) ||
  204. (opcode == HSSPI_FIFO_OP_CODE_W && plat->mode == SPI_TX_DUAL))
  205. opcode |= HSSPI_FIFO_OP_MBIT_MASK;
  206. /* profile mode */
  207. val = SPI_PFL_MODE_FILL_MASK |
  208. SPI_PFL_MODE_MDRDSZ_MASK |
  209. SPI_PFL_MODE_MDWRSZ_MASK;
  210. if (plat->mode & SPI_3WIRE)
  211. val |= SPI_PFL_MODE_3WIRE_MASK;
  212. writel_be(val, priv->regs + SPI_PFL_MODE_REG(plat->cs));
  213. /* transfer loop */
  214. while (data_bytes > 0) {
  215. size_t curr_step = min(step_size, data_bytes);
  216. int ret;
  217. /* copy tx data */
  218. if (tx) {
  219. memcpy_toio(priv->regs + HSSPI_FIFO_BASE +
  220. HSSPI_FIFO_OP_SIZE, tx, curr_step);
  221. tx += curr_step;
  222. }
  223. /* set fifo operation */
  224. writew_be(opcode | (curr_step & HSSPI_FIFO_OP_BYTES_MASK),
  225. priv->regs + HSSPI_FIFO_OP_REG);
  226. /* issue the transfer */
  227. val = SPI_CMD_OP_START;
  228. val |= (plat->cs << SPI_CMD_PFL_SHIFT) &
  229. SPI_CMD_PFL_MASK;
  230. val |= (!plat->cs << SPI_CMD_SLAVE_SHIFT) &
  231. SPI_CMD_SLAVE_MASK;
  232. writel_be(val, priv->regs + SPI_CMD_REG);
  233. /* wait for completion */
  234. ret = wait_for_bit_be32(priv->regs + SPI_STAT_REG,
  235. SPI_STAT_SRCBUSY_MASK, false,
  236. 1000, false);
  237. if (ret) {
  238. printf("interrupt timeout\n");
  239. return ret;
  240. }
  241. /* copy rx data */
  242. if (rx) {
  243. memcpy_fromio(rx, priv->regs + HSSPI_FIFO_BASE,
  244. curr_step);
  245. rx += curr_step;
  246. }
  247. data_bytes -= curr_step;
  248. }
  249. if (flags & SPI_XFER_END)
  250. bcm63xx_hsspi_deactivate_cs(priv);
  251. return 0;
  252. }
  253. static const struct dm_spi_ops bcm63xx_hsspi_ops = {
  254. .cs_info = bcm63xx_hsspi_cs_info,
  255. .set_mode = bcm63xx_hsspi_set_mode,
  256. .set_speed = bcm63xx_hsspi_set_speed,
  257. .xfer = bcm63xx_hsspi_xfer,
  258. };
  259. static const struct udevice_id bcm63xx_hsspi_ids[] = {
  260. { .compatible = "brcm,bcm6328-hsspi", },
  261. { /* sentinel */ }
  262. };
  263. static int bcm63xx_hsspi_child_pre_probe(struct udevice *dev)
  264. {
  265. struct bcm63xx_hsspi_priv *priv = dev_get_priv(dev->parent);
  266. struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
  267. /* check cs */
  268. if (plat->cs >= priv->num_cs) {
  269. printf("no cs %u\n", plat->cs);
  270. return -ENODEV;
  271. }
  272. /* cs polarity */
  273. if (plat->mode & SPI_CS_HIGH)
  274. priv->cs_pols |= BIT(plat->cs);
  275. else
  276. priv->cs_pols &= ~BIT(plat->cs);
  277. return 0;
  278. }
  279. static int bcm63xx_hsspi_probe(struct udevice *dev)
  280. {
  281. struct bcm63xx_hsspi_priv *priv = dev_get_priv(dev);
  282. struct reset_ctl rst_ctl;
  283. struct clk clk;
  284. fdt_addr_t addr;
  285. fdt_size_t size;
  286. int ret;
  287. addr = devfdt_get_addr_size_index(dev, 0, &size);
  288. if (addr == FDT_ADDR_T_NONE)
  289. return -EINVAL;
  290. priv->regs = ioremap(addr, size);
  291. priv->num_cs = fdtdec_get_uint(gd->fdt_blob, dev_of_offset(dev),
  292. "num-cs", 8);
  293. /* enable clock */
  294. ret = clk_get_by_name(dev, "hsspi", &clk);
  295. if (ret < 0)
  296. return ret;
  297. ret = clk_enable(&clk);
  298. if (ret < 0)
  299. return ret;
  300. ret = clk_free(&clk);
  301. if (ret < 0)
  302. return ret;
  303. /* get clock rate */
  304. ret = clk_get_by_name(dev, "pll", &clk);
  305. if (ret < 0)
  306. return ret;
  307. priv->clk_rate = clk_get_rate(&clk);
  308. ret = clk_free(&clk);
  309. if (ret < 0)
  310. return ret;
  311. /* perform reset */
  312. ret = reset_get_by_index(dev, 0, &rst_ctl);
  313. if (ret < 0)
  314. return ret;
  315. ret = reset_deassert(&rst_ctl);
  316. if (ret < 0)
  317. return ret;
  318. ret = reset_free(&rst_ctl);
  319. if (ret < 0)
  320. return ret;
  321. /* initialize hardware */
  322. writel_be(0, priv->regs + SPI_IR_MASK_REG);
  323. /* clear pending interrupts */
  324. writel_be(SPI_IR_CLEAR_ALL, priv->regs + SPI_IR_STAT_REG);
  325. /* enable clk gate */
  326. setbits_be32(priv->regs + SPI_CTL_REG, SPI_CTL_CLK_GATE_MASK);
  327. /* read default cs polarities */
  328. priv->cs_pols = readl_be(priv->regs + SPI_CTL_REG) &
  329. SPI_CTL_CS_POL_MASK;
  330. return 0;
  331. }
  332. U_BOOT_DRIVER(bcm63xx_hsspi) = {
  333. .name = "bcm63xx_hsspi",
  334. .id = UCLASS_SPI,
  335. .of_match = bcm63xx_hsspi_ids,
  336. .ops = &bcm63xx_hsspi_ops,
  337. .priv_auto_alloc_size = sizeof(struct bcm63xx_hsspi_priv),
  338. .child_pre_probe = bcm63xx_hsspi_child_pre_probe,
  339. .probe = bcm63xx_hsspi_probe,
  340. };