iproc_qspi.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * Copyright (c) 2017 - 2020, Broadcom
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <string.h>
  7. #include <common/debug.h>
  8. #include <drivers/delay_timer.h>
  9. #include <endian.h>
  10. #include <lib/mmio.h>
  11. #include <platform_def.h>
  12. #include <spi.h>
  13. #include "iproc_qspi.h"
  14. struct bcmspi_priv spi_cfg;
  15. /* Redefined by platform to force appropriate information */
  16. #pragma weak plat_spi_init
  17. int plat_spi_init(uint32_t *max_hz)
  18. {
  19. return 0;
  20. }
  21. /* Initialize & setup iproc qspi controller */
  22. int iproc_qspi_setup(uint32_t bus, uint32_t cs, uint32_t max_hz, uint32_t mode)
  23. {
  24. struct bcmspi_priv *priv = NULL;
  25. uint32_t spbr;
  26. priv = &spi_cfg;
  27. priv->spi_mode = mode;
  28. priv->state = QSPI_STATE_DISABLED;
  29. priv->bspi_hw = QSPI_BSPI_MODE_REG_BASE;
  30. priv->mspi_hw = QSPI_MSPI_MODE_REG_BASE;
  31. /* Initialize clock and platform specific */
  32. if (plat_spi_init(&max_hz) != 0)
  33. return -1;
  34. priv->max_hz = max_hz;
  35. /* MSPI: Basic hardware initialization */
  36. mmio_write_32(priv->mspi_hw + MSPI_SPCR1_LSB_REG, 0);
  37. mmio_write_32(priv->mspi_hw + MSPI_SPCR1_MSB_REG, 0);
  38. mmio_write_32(priv->mspi_hw + MSPI_NEWQP_REG, 0);
  39. mmio_write_32(priv->mspi_hw + MSPI_ENDQP_REG, 0);
  40. mmio_write_32(priv->mspi_hw + MSPI_SPCR2_REG, 0);
  41. /* MSPI: SCK configuration */
  42. spbr = (QSPI_AXI_CLK - 1) / (2 * priv->max_hz) + 1;
  43. spbr = MIN(spbr, SPBR_DIV_MAX);
  44. spbr = MAX(spbr, SPBR_DIV_MIN);
  45. mmio_write_32(priv->mspi_hw + MSPI_SPCR0_LSB_REG, spbr);
  46. /* MSPI: Mode configuration (8 bits by default) */
  47. priv->mspi_16bit = 0;
  48. mmio_write_32(priv->mspi_hw + MSPI_SPCR0_MSB_REG,
  49. BIT(MSPI_SPCR0_MSB_REG_MSTR_SHIFT) | /* Master */
  50. MSPI_SPCR0_MSB_REG_16_BITS_PER_WD_SHIFT | /* 16 bits per word */
  51. (priv->spi_mode & MSPI_SPCR0_MSB_REG_MODE_MASK)); /* mode: CPOL / CPHA */
  52. /* Display bus info */
  53. VERBOSE("SPI: SPCR0_LSB: 0x%x\n",
  54. mmio_read_32(priv->mspi_hw + MSPI_SPCR0_LSB_REG));
  55. VERBOSE("SPI: SPCR0_MSB: 0x%x\n",
  56. mmio_read_32(priv->mspi_hw + MSPI_SPCR0_MSB_REG));
  57. VERBOSE("SPI: SPCR1_LSB: 0x%x\n",
  58. mmio_read_32(priv->mspi_hw + MSPI_SPCR1_LSB_REG));
  59. VERBOSE("SPI: SPCR1_MSB: 0x%x\n",
  60. mmio_read_32(priv->mspi_hw + MSPI_SPCR1_MSB_REG));
  61. VERBOSE("SPI: SPCR2: 0x%x\n",
  62. mmio_read_32(priv->mspi_hw + MSPI_SPCR2_REG));
  63. VERBOSE("SPI: CLK: %d\n", priv->max_hz);
  64. return 0;
  65. }
  66. void bcmspi_enable_bspi(struct bcmspi_priv *priv)
  67. {
  68. if (priv->state != QSPI_STATE_BSPI) {
  69. /* Switch to BSPI */
  70. mmio_write_32(priv->bspi_hw + BSPI_MAST_N_BOOT_CTRL_REG, 0);
  71. priv->state = QSPI_STATE_BSPI;
  72. }
  73. }
  74. static int bcmspi_disable_bspi(struct bcmspi_priv *priv)
  75. {
  76. uint32_t retry;
  77. if (priv->state == QSPI_STATE_MSPI)
  78. return 0;
  79. /* Switch to MSPI if not yet */
  80. if ((mmio_read_32(priv->bspi_hw + BSPI_MAST_N_BOOT_CTRL_REG) &
  81. MSPI_CTRL_MASK) == 0) {
  82. retry = QSPI_RETRY_COUNT_US_MAX;
  83. do {
  84. if ((mmio_read_32(
  85. priv->bspi_hw + BSPI_BUSY_STATUS_REG) &
  86. BSPI_BUSY_MASK) == 0) {
  87. mmio_write_32(priv->bspi_hw +
  88. BSPI_MAST_N_BOOT_CTRL_REG,
  89. MSPI_CTRL_MASK);
  90. udelay(1);
  91. break;
  92. }
  93. udelay(1);
  94. } while (retry--);
  95. if ((mmio_read_32(priv->bspi_hw + BSPI_MAST_N_BOOT_CTRL_REG) &
  96. MSPI_CTRL_MASK) != MSPI_CTRL_MASK) {
  97. ERROR("QSPI: Switching to QSPI error.\n");
  98. return -1;
  99. }
  100. }
  101. /* Update state */
  102. priv->state = QSPI_STATE_MSPI;
  103. return 0;
  104. }
  105. int iproc_qspi_claim_bus(void)
  106. {
  107. struct bcmspi_priv *priv = &spi_cfg;
  108. /* Switch to MSPI by default */
  109. if (bcmspi_disable_bspi(priv) != 0)
  110. return -1;
  111. return 0;
  112. }
  113. void iproc_qspi_release_bus(void)
  114. {
  115. struct bcmspi_priv *priv = &spi_cfg;
  116. /* Switch to BSPI by default */
  117. bcmspi_enable_bspi(priv);
  118. }
  119. static int mspi_xfer(struct bcmspi_priv *priv, uint32_t bytes,
  120. const uint8_t *tx, uint8_t *rx, uint32_t flag)
  121. {
  122. uint32_t retry;
  123. uint32_t mode = CDRAM_PCS0;
  124. if (flag & SPI_XFER_QUAD) {
  125. mode |= CDRAM_QUAD_MODE;
  126. VERBOSE("SPI: QUAD mode\n");
  127. if (!tx) {
  128. VERBOSE("SPI: 4 lane input\n");
  129. mode |= CDRAM_RBIT_INPUT;
  130. }
  131. }
  132. /* Use 8-bit queue for odd-bytes transfer */
  133. if (bytes & 1)
  134. priv->mspi_16bit = 0;
  135. else {
  136. priv->mspi_16bit = 1;
  137. mode |= CDRAM_BITS_EN;
  138. }
  139. while (bytes) {
  140. uint32_t chunk;
  141. uint32_t queues;
  142. uint32_t i;
  143. /* Separate code for 16bit and 8bit transfers for performance */
  144. if (priv->mspi_16bit) {
  145. VERBOSE("SPI: 16 bits xfer\n");
  146. /* Determine how many bytes to process this time */
  147. chunk = MIN(bytes, NUM_CDRAM_BYTES * 2);
  148. queues = (chunk - 1) / 2 + 1;
  149. bytes -= chunk;
  150. /* Fill CDRAMs */
  151. for (i = 0; i < queues; i++)
  152. mmio_write_32(priv->mspi_hw + MSPI_CDRAM_REG +
  153. (i << 2), mode | CDRAM_CONT);
  154. /* Fill TXRAMs */
  155. for (i = 0; i < chunk; i++)
  156. if (tx)
  157. mmio_write_32(priv->mspi_hw +
  158. MSPI_TXRAM_REG +
  159. (i << 2), tx[i]);
  160. } else {
  161. VERBOSE("SPI: 8 bits xfer\n");
  162. /* Determine how many bytes to process this time */
  163. chunk = MIN(bytes, NUM_CDRAM_BYTES);
  164. queues = chunk;
  165. bytes -= chunk;
  166. /* Fill CDRAMs and TXRAMS */
  167. for (i = 0; i < chunk; i++) {
  168. mmio_write_32(priv->mspi_hw + MSPI_CDRAM_REG +
  169. (i << 2), mode | CDRAM_CONT);
  170. if (tx)
  171. mmio_write_32(priv->mspi_hw +
  172. MSPI_TXRAM_REG +
  173. (i << 3), tx[i]);
  174. }
  175. }
  176. /* Advance pointers */
  177. if (tx)
  178. tx += chunk;
  179. /* Setup queue pointers */
  180. mmio_write_32(priv->mspi_hw + MSPI_NEWQP_REG, 0);
  181. mmio_write_32(priv->mspi_hw + MSPI_ENDQP_REG, queues - 1);
  182. /* Remove CONT on the last byte command */
  183. if (bytes == 0 && (flag & SPI_XFER_END))
  184. mmio_write_32(priv->mspi_hw + MSPI_CDRAM_REG +
  185. ((queues - 1) << 2), mode);
  186. /* Kick off */
  187. mmio_write_32(priv->mspi_hw + MSPI_STATUS_REG, 0);
  188. if (bytes == 0 && (flag & SPI_XFER_END))
  189. mmio_write_32(priv->mspi_hw + MSPI_SPCR2_REG, MSPI_SPE);
  190. else
  191. mmio_write_32(priv->mspi_hw + MSPI_SPCR2_REG,
  192. MSPI_SPE | MSPI_CONT_AFTER_CMD);
  193. /* Wait for completion */
  194. retry = QSPI_RETRY_COUNT_US_MAX;
  195. do {
  196. if (mmio_read_32(priv->mspi_hw + MSPI_STATUS_REG) &
  197. MSPI_CMD_COMPLETE_MASK)
  198. break;
  199. udelay(1);
  200. } while (retry--);
  201. if ((mmio_read_32(priv->mspi_hw + MSPI_STATUS_REG) &
  202. MSPI_CMD_COMPLETE_MASK) == 0) {
  203. ERROR("SPI: Completion timeout.\n");
  204. return -1;
  205. }
  206. /* Read data out */
  207. if (rx) {
  208. if (priv->mspi_16bit) {
  209. for (i = 0; i < chunk; i++) {
  210. rx[i] = mmio_read_32(priv->mspi_hw +
  211. MSPI_RXRAM_REG +
  212. (i << 2))
  213. & 0xff;
  214. }
  215. } else {
  216. for (i = 0; i < chunk; i++) {
  217. rx[i] = mmio_read_32(priv->mspi_hw +
  218. MSPI_RXRAM_REG +
  219. (((i << 1) + 1) << 2))
  220. & 0xff;
  221. }
  222. }
  223. rx += chunk;
  224. }
  225. }
  226. return 0;
  227. }
  228. int iproc_qspi_xfer(uint32_t bitlen,
  229. const void *dout, void *din, unsigned long flags)
  230. {
  231. struct bcmspi_priv *priv;
  232. const uint8_t *tx = dout;
  233. uint8_t *rx = din;
  234. uint32_t bytes = bitlen / 8;
  235. int ret = 0;
  236. priv = &spi_cfg;
  237. if (priv->state == QSPI_STATE_DISABLED) {
  238. ERROR("QSPI: state disabled\n");
  239. return -1;
  240. }
  241. /* we can only do 8 bit transfers */
  242. if (bitlen % 8) {
  243. ERROR("QSPI: Only support 8 bit transfers (requested %d)\n",
  244. bitlen);
  245. return -1;
  246. }
  247. /* MSPI: Enable write lock at the beginning */
  248. if (flags & SPI_XFER_BEGIN) {
  249. /* Switch to MSPI if not yet */
  250. if (bcmspi_disable_bspi(priv) != 0) {
  251. ERROR("QSPI: Switch to MSPI failed\n");
  252. return -1;
  253. }
  254. mmio_write_32(priv->mspi_hw + MSPI_WRITE_LOCK_REG, 1);
  255. }
  256. /* MSPI: Transfer it */
  257. if (bytes)
  258. ret = mspi_xfer(priv, bytes, tx, rx, flags);
  259. /* MSPI: Disable write lock if it's done */
  260. if (flags & SPI_XFER_END)
  261. mmio_write_32(priv->mspi_hw + MSPI_WRITE_LOCK_REG, 0);
  262. return ret;
  263. }