stm32_qspi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /*
  2. * Copyright (c) 2019-2022, STMicroelectronics - All Rights Reserved
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
  5. */
  6. #include <inttypes.h>
  7. #include <common/debug.h>
  8. #include <common/fdt_wrappers.h>
  9. #include <drivers/clk.h>
  10. #include <drivers/delay_timer.h>
  11. #include <drivers/spi_mem.h>
  12. #include <drivers/st/stm32_gpio.h>
  13. #include <drivers/st/stm32_qspi.h>
  14. #include <drivers/st/stm32mp_reset.h>
  15. #include <lib/mmio.h>
  16. #include <lib/utils_def.h>
  17. #include <libfdt.h>
  18. #include <platform_def.h>
  19. /* Timeout for device interface reset */
  20. #define TIMEOUT_US_1_MS 1000U
  21. /* QUADSPI registers */
  22. #define QSPI_CR 0x00U
  23. #define QSPI_DCR 0x04U
  24. #define QSPI_SR 0x08U
  25. #define QSPI_FCR 0x0CU
  26. #define QSPI_DLR 0x10U
  27. #define QSPI_CCR 0x14U
  28. #define QSPI_AR 0x18U
  29. #define QSPI_ABR 0x1CU
  30. #define QSPI_DR 0x20U
  31. #define QSPI_PSMKR 0x24U
  32. #define QSPI_PSMAR 0x28U
  33. #define QSPI_PIR 0x2CU
  34. #define QSPI_LPTR 0x30U
  35. /* QUADSPI control register */
  36. #define QSPI_CR_EN BIT(0)
  37. #define QSPI_CR_ABORT BIT(1)
  38. #define QSPI_CR_DMAEN BIT(2)
  39. #define QSPI_CR_TCEN BIT(3)
  40. #define QSPI_CR_SSHIFT BIT(4)
  41. #define QSPI_CR_DFM BIT(6)
  42. #define QSPI_CR_FSEL BIT(7)
  43. #define QSPI_CR_FTHRES_SHIFT 8U
  44. #define QSPI_CR_TEIE BIT(16)
  45. #define QSPI_CR_TCIE BIT(17)
  46. #define QSPI_CR_FTIE BIT(18)
  47. #define QSPI_CR_SMIE BIT(19)
  48. #define QSPI_CR_TOIE BIT(20)
  49. #define QSPI_CR_APMS BIT(22)
  50. #define QSPI_CR_PMM BIT(23)
  51. #define QSPI_CR_PRESCALER_MASK GENMASK_32(31, 24)
  52. #define QSPI_CR_PRESCALER_SHIFT 24U
  53. /* QUADSPI device configuration register */
  54. #define QSPI_DCR_CKMODE BIT(0)
  55. #define QSPI_DCR_CSHT_MASK GENMASK_32(10, 8)
  56. #define QSPI_DCR_CSHT_SHIFT 8U
  57. #define QSPI_DCR_FSIZE_MASK GENMASK_32(20, 16)
  58. #define QSPI_DCR_FSIZE_SHIFT 16U
  59. /* QUADSPI status register */
  60. #define QSPI_SR_TEF BIT(0)
  61. #define QSPI_SR_TCF BIT(1)
  62. #define QSPI_SR_FTF BIT(2)
  63. #define QSPI_SR_SMF BIT(3)
  64. #define QSPI_SR_TOF BIT(4)
  65. #define QSPI_SR_BUSY BIT(5)
  66. /* QUADSPI flag clear register */
  67. #define QSPI_FCR_CTEF BIT(0)
  68. #define QSPI_FCR_CTCF BIT(1)
  69. #define QSPI_FCR_CSMF BIT(3)
  70. #define QSPI_FCR_CTOF BIT(4)
  71. /* QUADSPI communication configuration register */
  72. #define QSPI_CCR_DDRM BIT(31)
  73. #define QSPI_CCR_DHHC BIT(30)
  74. #define QSPI_CCR_SIOO BIT(28)
  75. #define QSPI_CCR_FMODE_SHIFT 26U
  76. #define QSPI_CCR_DMODE_SHIFT 24U
  77. #define QSPI_CCR_DCYC_SHIFT 18U
  78. #define QSPI_CCR_ABSIZE_SHIFT 16U
  79. #define QSPI_CCR_ABMODE_SHIFT 14U
  80. #define QSPI_CCR_ADSIZE_SHIFT 12U
  81. #define QSPI_CCR_ADMODE_SHIFT 10U
  82. #define QSPI_CCR_IMODE_SHIFT 8U
  83. #define QSPI_CCR_IND_WRITE 0U
  84. #define QSPI_CCR_IND_READ 1U
  85. #define QSPI_CCR_MEM_MAP 3U
  86. #define QSPI_MAX_CHIP 2U
  87. #define QSPI_FIFO_TIMEOUT_US 30U
  88. #define QSPI_CMD_TIMEOUT_US 1000U
  89. #define QSPI_BUSY_TIMEOUT_US 100U
  90. #define QSPI_ABT_TIMEOUT_US 100U
  91. #define DT_QSPI_COMPAT "st,stm32f469-qspi"
  92. #define FREQ_100MHZ 100000000U
  93. struct stm32_qspi_ctrl {
  94. uintptr_t reg_base;
  95. uintptr_t mm_base;
  96. size_t mm_size;
  97. unsigned long clock_id;
  98. unsigned int reset_id;
  99. };
  100. static struct stm32_qspi_ctrl stm32_qspi;
  101. static uintptr_t qspi_base(void)
  102. {
  103. return stm32_qspi.reg_base;
  104. }
  105. static int stm32_qspi_wait_for_not_busy(void)
  106. {
  107. uint64_t timeout = timeout_init_us(QSPI_BUSY_TIMEOUT_US);
  108. while ((mmio_read_32(qspi_base() + QSPI_SR) & QSPI_SR_BUSY) != 0U) {
  109. if (timeout_elapsed(timeout)) {
  110. ERROR("%s: busy timeout\n", __func__);
  111. return -ETIMEDOUT;
  112. }
  113. }
  114. return 0;
  115. }
  116. static int stm32_qspi_wait_cmd(const struct spi_mem_op *op)
  117. {
  118. int ret = 0;
  119. uint64_t timeout;
  120. timeout = timeout_init_us(QSPI_CMD_TIMEOUT_US);
  121. while ((mmio_read_32(qspi_base() + QSPI_SR) & QSPI_SR_TCF) == 0U) {
  122. if (timeout_elapsed(timeout)) {
  123. ret = -ETIMEDOUT;
  124. break;
  125. }
  126. }
  127. if (ret == 0) {
  128. if ((mmio_read_32(qspi_base() + QSPI_SR) & QSPI_SR_TEF) != 0U) {
  129. ERROR("%s: transfer error\n", __func__);
  130. ret = -EIO;
  131. }
  132. } else {
  133. ERROR("%s: cmd timeout\n", __func__);
  134. }
  135. /* Clear flags */
  136. mmio_write_32(qspi_base() + QSPI_FCR, QSPI_FCR_CTCF | QSPI_FCR_CTEF);
  137. if (ret == 0) {
  138. ret = stm32_qspi_wait_for_not_busy();
  139. }
  140. return ret;
  141. }
  142. static void stm32_qspi_read_fifo(uint8_t *val, uintptr_t addr)
  143. {
  144. *val = mmio_read_8(addr);
  145. }
  146. static void stm32_qspi_write_fifo(uint8_t *val, uintptr_t addr)
  147. {
  148. mmio_write_8(addr, *val);
  149. }
  150. static int stm32_qspi_poll(const struct spi_mem_op *op)
  151. {
  152. void (*fifo)(uint8_t *val, uintptr_t addr);
  153. uint32_t len;
  154. uint8_t *buf;
  155. if (op->data.dir == SPI_MEM_DATA_IN) {
  156. fifo = stm32_qspi_read_fifo;
  157. } else {
  158. fifo = stm32_qspi_write_fifo;
  159. }
  160. buf = (uint8_t *)op->data.buf;
  161. for (len = op->data.nbytes; len != 0U; len--) {
  162. uint64_t timeout = timeout_init_us(QSPI_FIFO_TIMEOUT_US);
  163. while ((mmio_read_32(qspi_base() + QSPI_SR) &
  164. QSPI_SR_FTF) == 0U) {
  165. if (timeout_elapsed(timeout)) {
  166. ERROR("%s: fifo timeout\n", __func__);
  167. return -ETIMEDOUT;
  168. }
  169. }
  170. fifo(buf++, qspi_base() + QSPI_DR);
  171. }
  172. return 0;
  173. }
  174. static int stm32_qspi_mm(const struct spi_mem_op *op)
  175. {
  176. memcpy(op->data.buf,
  177. (void *)(stm32_qspi.mm_base + (size_t)op->addr.val),
  178. op->data.nbytes);
  179. return 0;
  180. }
  181. static int stm32_qspi_tx(const struct spi_mem_op *op, uint8_t mode)
  182. {
  183. if (op->data.nbytes == 0U) {
  184. return 0;
  185. }
  186. if (mode == QSPI_CCR_MEM_MAP) {
  187. return stm32_qspi_mm(op);
  188. }
  189. return stm32_qspi_poll(op);
  190. }
  191. static unsigned int stm32_qspi_get_mode(uint8_t buswidth)
  192. {
  193. if (buswidth == 4U) {
  194. return 3U;
  195. }
  196. return buswidth;
  197. }
  198. static int stm32_qspi_exec_op(const struct spi_mem_op *op)
  199. {
  200. uint64_t timeout;
  201. uint32_t ccr;
  202. size_t addr_max;
  203. uint8_t mode = QSPI_CCR_IND_WRITE;
  204. int ret;
  205. VERBOSE("%s: cmd:%x mode:%d.%d.%d.%d addr:%" PRIx64 " len:%x\n",
  206. __func__, op->cmd.opcode, op->cmd.buswidth, op->addr.buswidth,
  207. op->dummy.buswidth, op->data.buswidth,
  208. op->addr.val, op->data.nbytes);
  209. addr_max = op->addr.val + op->data.nbytes + 1U;
  210. if ((op->data.dir == SPI_MEM_DATA_IN) && (op->data.nbytes != 0U)) {
  211. if ((addr_max < stm32_qspi.mm_size) &&
  212. (op->addr.buswidth != 0U)) {
  213. mode = QSPI_CCR_MEM_MAP;
  214. } else {
  215. mode = QSPI_CCR_IND_READ;
  216. }
  217. }
  218. if (op->data.nbytes != 0U) {
  219. mmio_write_32(qspi_base() + QSPI_DLR, op->data.nbytes - 1U);
  220. }
  221. ccr = mode << QSPI_CCR_FMODE_SHIFT;
  222. ccr |= op->cmd.opcode;
  223. ccr |= stm32_qspi_get_mode(op->cmd.buswidth) << QSPI_CCR_IMODE_SHIFT;
  224. if (op->addr.nbytes != 0U) {
  225. ccr |= (op->addr.nbytes - 1U) << QSPI_CCR_ADSIZE_SHIFT;
  226. ccr |= stm32_qspi_get_mode(op->addr.buswidth) <<
  227. QSPI_CCR_ADMODE_SHIFT;
  228. }
  229. if ((op->dummy.buswidth != 0U) && (op->dummy.nbytes != 0U)) {
  230. ccr |= (op->dummy.nbytes * 8U / op->dummy.buswidth) <<
  231. QSPI_CCR_DCYC_SHIFT;
  232. }
  233. if (op->data.nbytes != 0U) {
  234. ccr |= stm32_qspi_get_mode(op->data.buswidth) <<
  235. QSPI_CCR_DMODE_SHIFT;
  236. }
  237. mmio_write_32(qspi_base() + QSPI_CCR, ccr);
  238. if ((op->addr.nbytes != 0U) && (mode != QSPI_CCR_MEM_MAP)) {
  239. mmio_write_32(qspi_base() + QSPI_AR, op->addr.val);
  240. }
  241. ret = stm32_qspi_tx(op, mode);
  242. /*
  243. * Abort in:
  244. * - Error case.
  245. * - Memory mapped read: prefetching must be stopped if we read the last
  246. * byte of device (device size - fifo size). If device size is not
  247. * known then prefetching is always stopped.
  248. */
  249. if ((ret != 0) || (mode == QSPI_CCR_MEM_MAP)) {
  250. goto abort;
  251. }
  252. /* Wait end of TX in indirect mode */
  253. ret = stm32_qspi_wait_cmd(op);
  254. if (ret != 0) {
  255. goto abort;
  256. }
  257. return 0;
  258. abort:
  259. mmio_setbits_32(qspi_base() + QSPI_CR, QSPI_CR_ABORT);
  260. /* Wait clear of abort bit by hardware */
  261. timeout = timeout_init_us(QSPI_ABT_TIMEOUT_US);
  262. while ((mmio_read_32(qspi_base() + QSPI_CR) & QSPI_CR_ABORT) != 0U) {
  263. if (timeout_elapsed(timeout)) {
  264. ret = -ETIMEDOUT;
  265. break;
  266. }
  267. }
  268. mmio_write_32(qspi_base() + QSPI_FCR, QSPI_FCR_CTCF);
  269. if (ret != 0) {
  270. ERROR("%s: exec op error\n", __func__);
  271. }
  272. return ret;
  273. }
  274. static int stm32_qspi_claim_bus(unsigned int cs)
  275. {
  276. uint32_t cr;
  277. if (cs >= QSPI_MAX_CHIP) {
  278. return -ENODEV;
  279. }
  280. /* Set chip select and enable the controller */
  281. cr = QSPI_CR_EN;
  282. if (cs == 1U) {
  283. cr |= QSPI_CR_FSEL;
  284. }
  285. mmio_clrsetbits_32(qspi_base() + QSPI_CR, QSPI_CR_FSEL, cr);
  286. return 0;
  287. }
  288. static void stm32_qspi_release_bus(void)
  289. {
  290. mmio_clrbits_32(qspi_base() + QSPI_CR, QSPI_CR_EN);
  291. }
  292. static int stm32_qspi_set_speed(unsigned int hz)
  293. {
  294. unsigned long qspi_clk = clk_get_rate(stm32_qspi.clock_id);
  295. uint32_t prescaler = UINT8_MAX;
  296. uint32_t csht;
  297. int ret;
  298. if (qspi_clk == 0U) {
  299. return -EINVAL;
  300. }
  301. if (hz > 0U) {
  302. prescaler = div_round_up(qspi_clk, hz) - 1U;
  303. if (prescaler > UINT8_MAX) {
  304. prescaler = UINT8_MAX;
  305. }
  306. }
  307. csht = div_round_up((5U * qspi_clk) / (prescaler + 1U), FREQ_100MHZ);
  308. csht = ((csht - 1U) << QSPI_DCR_CSHT_SHIFT) & QSPI_DCR_CSHT_MASK;
  309. ret = stm32_qspi_wait_for_not_busy();
  310. if (ret != 0) {
  311. return ret;
  312. }
  313. mmio_clrsetbits_32(qspi_base() + QSPI_CR, QSPI_CR_PRESCALER_MASK,
  314. prescaler << QSPI_CR_PRESCALER_SHIFT);
  315. mmio_clrsetbits_32(qspi_base() + QSPI_DCR, QSPI_DCR_CSHT_MASK, csht);
  316. VERBOSE("%s: speed=%lu\n", __func__, qspi_clk / (prescaler + 1U));
  317. return 0;
  318. }
  319. static int stm32_qspi_set_mode(unsigned int mode)
  320. {
  321. int ret;
  322. ret = stm32_qspi_wait_for_not_busy();
  323. if (ret != 0) {
  324. return ret;
  325. }
  326. if ((mode & SPI_CS_HIGH) != 0U) {
  327. return -ENODEV;
  328. }
  329. if (((mode & SPI_CPHA) != 0U) && ((mode & SPI_CPOL) != 0U)) {
  330. mmio_setbits_32(qspi_base() + QSPI_DCR, QSPI_DCR_CKMODE);
  331. } else if (((mode & SPI_CPHA) == 0U) && ((mode & SPI_CPOL) == 0U)) {
  332. mmio_clrbits_32(qspi_base() + QSPI_DCR, QSPI_DCR_CKMODE);
  333. } else {
  334. return -ENODEV;
  335. }
  336. VERBOSE("%s: mode=0x%x\n", __func__, mode);
  337. if ((mode & SPI_RX_QUAD) != 0U) {
  338. VERBOSE("rx: quad\n");
  339. } else if ((mode & SPI_RX_DUAL) != 0U) {
  340. VERBOSE("rx: dual\n");
  341. } else {
  342. VERBOSE("rx: single\n");
  343. }
  344. if ((mode & SPI_TX_QUAD) != 0U) {
  345. VERBOSE("tx: quad\n");
  346. } else if ((mode & SPI_TX_DUAL) != 0U) {
  347. VERBOSE("tx: dual\n");
  348. } else {
  349. VERBOSE("tx: single\n");
  350. }
  351. return 0;
  352. }
  353. static const struct spi_bus_ops stm32_qspi_bus_ops = {
  354. .claim_bus = stm32_qspi_claim_bus,
  355. .release_bus = stm32_qspi_release_bus,
  356. .set_speed = stm32_qspi_set_speed,
  357. .set_mode = stm32_qspi_set_mode,
  358. .exec_op = stm32_qspi_exec_op,
  359. };
  360. int stm32_qspi_init(void)
  361. {
  362. size_t size;
  363. int qspi_node;
  364. struct dt_node_info info;
  365. void *fdt = NULL;
  366. int ret;
  367. if (fdt_get_address(&fdt) == 0) {
  368. return -FDT_ERR_NOTFOUND;
  369. }
  370. qspi_node = dt_get_node(&info, -1, DT_QSPI_COMPAT);
  371. if (qspi_node < 0) {
  372. ERROR("No QSPI ctrl found\n");
  373. return -FDT_ERR_NOTFOUND;
  374. }
  375. if (info.status == DT_DISABLED) {
  376. return -FDT_ERR_NOTFOUND;
  377. }
  378. ret = fdt_get_reg_props_by_name(fdt, qspi_node, "qspi",
  379. &stm32_qspi.reg_base, &size);
  380. if (ret != 0) {
  381. return ret;
  382. }
  383. ret = fdt_get_reg_props_by_name(fdt, qspi_node, "qspi_mm",
  384. &stm32_qspi.mm_base,
  385. &stm32_qspi.mm_size);
  386. if (ret != 0) {
  387. return ret;
  388. }
  389. if (dt_set_pinctrl_config(qspi_node) != 0) {
  390. return -FDT_ERR_BADVALUE;
  391. }
  392. if ((info.clock < 0) || (info.reset < 0)) {
  393. return -FDT_ERR_BADVALUE;
  394. }
  395. stm32_qspi.clock_id = (unsigned long)info.clock;
  396. stm32_qspi.reset_id = (unsigned int)info.reset;
  397. clk_enable(stm32_qspi.clock_id);
  398. ret = stm32mp_reset_assert(stm32_qspi.reset_id, TIMEOUT_US_1_MS);
  399. if (ret != 0) {
  400. panic();
  401. }
  402. ret = stm32mp_reset_deassert(stm32_qspi.reset_id, TIMEOUT_US_1_MS);
  403. if (ret != 0) {
  404. panic();
  405. }
  406. mmio_write_32(qspi_base() + QSPI_CR, QSPI_CR_SSHIFT);
  407. mmio_write_32(qspi_base() + QSPI_DCR, QSPI_DCR_FSIZE_MASK);
  408. return spi_mem_init_slave(fdt, qspi_node, &stm32_qspi_bus_ops);
  409. };