microchip_coreqspi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2022 Microchip Technology Inc.
  4. * Padmarao Begari <padmarao.begari@microchip.com>
  5. * Naga Sureshkumar Relli <nagasuresh.relli@microchip.com>
  6. */
  7. #include <common.h>
  8. #include <clk.h>
  9. #include <dm.h>
  10. #include <log.h>
  11. #include <malloc.h>
  12. #include <spi.h>
  13. #include <spi-mem.h>
  14. #include <asm/io.h>
  15. #include <linux/bitops.h>
  16. #include <linux/delay.h>
  17. #include <linux/types.h>
  18. #include <linux/sizes.h>
  19. DECLARE_GLOBAL_DATA_PTR;
  20. /*
  21. * QSPI Control register mask defines
  22. */
  23. #define CONTROL_ENABLE BIT(0)
  24. #define CONTROL_MASTER BIT(1)
  25. #define CONTROL_XIP BIT(2)
  26. #define CONTROL_XIPADDR BIT(3)
  27. #define CONTROL_CLKIDLE BIT(10)
  28. #define CONTROL_SAMPLE_MASK GENMASK(12, 11)
  29. #define CONTROL_MODE0 BIT(13)
  30. #define CONTROL_MODE12_MASK GENMASK(15, 14)
  31. #define CONTROL_MODE12_EX_RO BIT(14)
  32. #define CONTROL_MODE12_EX_RW BIT(15)
  33. #define CONTROL_MODE12_FULL GENMASK(15, 14)
  34. #define CONTROL_FLAGSX4 BIT(16)
  35. #define CONTROL_CLKRATE_MASK GENMASK(27, 24)
  36. #define CONTROL_CLKRATE_SHIFT 24
  37. /*
  38. * QSPI Frames register mask defines
  39. */
  40. #define FRAMES_TOTALBYTES_MASK GENMASK(15, 0)
  41. #define FRAMES_CMDBYTES_MASK GENMASK(24, 16)
  42. #define FRAMES_CMDBYTES_SHIFT 16
  43. #define FRAMES_SHIFT 25
  44. #define FRAMES_IDLE_MASK GENMASK(29, 26)
  45. #define FRAMES_IDLE_SHIFT 26
  46. #define FRAMES_FLAGBYTE BIT(30)
  47. #define FRAMES_FLAGWORD BIT(31)
  48. /*
  49. * QSPI Interrupt Enable register mask defines
  50. */
  51. #define IEN_TXDONE BIT(0)
  52. #define IEN_RXDONE BIT(1)
  53. #define IEN_RXAVAILABLE BIT(2)
  54. #define IEN_TXAVAILABLE BIT(3)
  55. #define IEN_RXFIFOEMPTY BIT(4)
  56. #define IEN_TXFIFOFULL BIT(5)
  57. /*
  58. * QSPI Status register mask defines
  59. */
  60. #define STATUS_TXDONE BIT(0)
  61. #define STATUS_RXDONE BIT(1)
  62. #define STATUS_RXAVAILABLE BIT(2)
  63. #define STATUS_TXAVAILABLE BIT(3)
  64. #define STATUS_RXFIFOEMPTY BIT(4)
  65. #define STATUS_TXFIFOFULL BIT(5)
  66. #define STATUS_READY BIT(7)
  67. #define STATUS_FLAGSX4 BIT(8)
  68. #define STATUS_MASK GENMASK(8, 0)
  69. #define BYTESUPPER_MASK GENMASK(31, 16)
  70. #define BYTESLOWER_MASK GENMASK(15, 0)
  71. #define MAX_DIVIDER 16
  72. #define MIN_DIVIDER 0
  73. #define MAX_DATA_CMD_LEN 256
  74. /* QSPI ready time out value */
  75. #define TIMEOUT_MS (1000 * 500)
  76. /*
  77. * QSPI Register offsets.
  78. */
  79. #define REG_CONTROL (0x00)
  80. #define REG_FRAMES (0x04)
  81. #define REG_IEN (0x0c)
  82. #define REG_STATUS (0x10)
  83. #define REG_DIRECT_ACCESS (0x14)
  84. #define REG_UPPER_ACCESS (0x18)
  85. #define REG_RX_DATA (0x40)
  86. #define REG_TX_DATA (0x44)
  87. #define REG_X4_RX_DATA (0x48)
  88. #define REG_X4_TX_DATA (0x4c)
  89. #define REG_FRAMESUP (0x50)
  90. /**
  91. * struct mchp_coreqspi - Defines qspi driver instance
  92. * @regs: Address of the QSPI controller registers
  93. * @freq: QSPI Input frequency
  94. * @txbuf: TX buffer
  95. * @rxbuf: RX buffer
  96. * @tx_len: Number of bytes left to transfer
  97. * @rx_len: Number of bytes left to receive
  98. */
  99. struct mchp_coreqspi {
  100. void __iomem *regs;
  101. u32 freq;
  102. u8 *txbuf;
  103. u8 *rxbuf;
  104. int tx_len;
  105. int rx_len;
  106. };
  107. static void mchp_coreqspi_init_hw(struct mchp_coreqspi *qspi)
  108. {
  109. u32 control;
  110. control = CONTROL_CLKIDLE | CONTROL_ENABLE;
  111. writel(control, qspi->regs + REG_CONTROL);
  112. writel(0, qspi->regs + REG_IEN);
  113. }
  114. static inline void mchp_coreqspi_read_op(struct mchp_coreqspi *qspi)
  115. {
  116. u32 control, data;
  117. if (!qspi->rx_len)
  118. return;
  119. control = readl(qspi->regs + REG_CONTROL);
  120. /*
  121. * Read 4-bytes from the SPI FIFO in single transaction and then read
  122. * the reamaining data byte wise.
  123. */
  124. control |= CONTROL_FLAGSX4;
  125. writel(control, qspi->regs + REG_CONTROL);
  126. while (qspi->rx_len >= 4) {
  127. while (readl(qspi->regs + REG_STATUS) & STATUS_RXFIFOEMPTY)
  128. ;
  129. data = readl(qspi->regs + REG_X4_RX_DATA);
  130. *(u32 *)qspi->rxbuf = data;
  131. qspi->rxbuf += 4;
  132. qspi->rx_len -= 4;
  133. }
  134. control &= ~CONTROL_FLAGSX4;
  135. writel(control, qspi->regs + REG_CONTROL);
  136. while (qspi->rx_len--) {
  137. while (readl(qspi->regs + REG_STATUS) & STATUS_RXFIFOEMPTY)
  138. ;
  139. data = readl(qspi->regs + REG_RX_DATA);
  140. *qspi->rxbuf++ = (data & 0xFF);
  141. }
  142. }
  143. static inline void mchp_coreqspi_write_op(struct mchp_coreqspi *qspi, bool word)
  144. {
  145. u32 control, data;
  146. control = readl(qspi->regs + REG_CONTROL);
  147. control |= CONTROL_FLAGSX4;
  148. writel(control, qspi->regs + REG_CONTROL);
  149. while (qspi->tx_len >= 4) {
  150. while (readl(qspi->regs + REG_STATUS) & STATUS_TXFIFOFULL)
  151. ;
  152. data = *(u32 *)qspi->txbuf;
  153. qspi->txbuf += 4;
  154. qspi->tx_len -= 4;
  155. writel(data, qspi->regs + REG_X4_TX_DATA);
  156. }
  157. control &= ~CONTROL_FLAGSX4;
  158. writel(control, qspi->regs + REG_CONTROL);
  159. while (qspi->tx_len--) {
  160. while (readl(qspi->regs + REG_STATUS) & STATUS_TXFIFOFULL)
  161. ;
  162. data = *qspi->txbuf++;
  163. writel(data, qspi->regs + REG_TX_DATA);
  164. }
  165. }
  166. static inline void mchp_coreqspi_config_op(struct mchp_coreqspi *qspi,
  167. const struct spi_mem_op *op)
  168. {
  169. u32 idle_cycles = 0;
  170. int total_bytes, cmd_bytes, frames, ctrl;
  171. cmd_bytes = op->cmd.nbytes + op->addr.nbytes;
  172. total_bytes = cmd_bytes + op->data.nbytes;
  173. /*
  174. * As per the coreQSPI IP spec,the number of command and data bytes are
  175. * controlled by the frames register for each SPI sequence. This supports
  176. * the SPI flash memory read and writes sequences as below. so configure
  177. * the cmd and total bytes accordingly.
  178. * ---------------------------------------------------------------------
  179. * TOTAL BYTES | CMD BYTES | What happens |
  180. * ______________________________________________________________________
  181. * | | |
  182. * 1 | 1 | The SPI core will transmit a single byte |
  183. * | | and receive data is discarded |
  184. * | | |
  185. * 1 | 0 | The SPI core will transmit a single byte |
  186. * | | and return a single byte |
  187. * | | |
  188. * 10 | 4 | The SPI core will transmit 4 command |
  189. * | | bytes discarding the receive data and |
  190. * | | transmits 6 dummy bytes returning the 6 |
  191. * | | received bytes and return a single byte |
  192. * | | |
  193. * 10 | 10 | The SPI core will transmit 10 command |
  194. * | | |
  195. * 10 | 0 | The SPI core will transmit 10 command |
  196. * | | bytes and returning 10 received bytes |
  197. * ______________________________________________________________________
  198. */
  199. if (!(op->data.dir == SPI_MEM_DATA_IN))
  200. cmd_bytes = total_bytes;
  201. frames = total_bytes & BYTESUPPER_MASK;
  202. writel(frames, qspi->regs + REG_FRAMESUP);
  203. frames = total_bytes & BYTESLOWER_MASK;
  204. frames |= cmd_bytes << FRAMES_CMDBYTES_SHIFT;
  205. if (op->dummy.buswidth)
  206. idle_cycles = op->dummy.nbytes * 8 / op->dummy.buswidth;
  207. frames |= idle_cycles << FRAMES_IDLE_SHIFT;
  208. ctrl = readl(qspi->regs + REG_CONTROL);
  209. if (ctrl & CONTROL_MODE12_MASK)
  210. frames |= (1 << FRAMES_SHIFT);
  211. frames |= FRAMES_FLAGWORD;
  212. writel(frames, qspi->regs + REG_FRAMES);
  213. }
  214. static int mchp_coreqspi_wait_for_ready(struct spi_slave *slave)
  215. {
  216. struct mchp_coreqspi *qspi = dev_get_priv(slave->dev->parent);
  217. unsigned long count = 0;
  218. while (1) {
  219. if (readl(qspi->regs + REG_STATUS) & STATUS_READY)
  220. return 0;
  221. udelay(1);
  222. count += 1;
  223. if (count == TIMEOUT_MS)
  224. return -ETIMEDOUT;
  225. }
  226. }
  227. static int mchp_coreqspi_set_operate_mode(struct mchp_coreqspi *qspi,
  228. const struct spi_mem_op *op)
  229. {
  230. u32 control = readl(qspi->regs + REG_CONTROL);
  231. /*
  232. * The operating mode can be configured based on the command that needs
  233. * to be send.
  234. * bits[15:14]: Sets whether multiple bit SPI operates in normal,
  235. * extended or full modes.
  236. * 00: Normal (single DQ0 TX and single DQ1 RX lines)
  237. * 01: Extended RO (command and address bytes on DQ0 only)
  238. * 10: Extended RW (command byte on DQ0 only)
  239. * 11: Full. (command and address are on all DQ lines)
  240. * bit[13]: Sets whether multiple bit SPI uses 2 or 4 bits of data
  241. * 0: 2-bits (BSPI)
  242. * 1: 4-bits (QSPI)
  243. */
  244. if (op->data.buswidth == 4 || op->data.buswidth == 2) {
  245. control &= ~CONTROL_MODE12_MASK;
  246. if (op->cmd.buswidth == 1 && (op->addr.buswidth == 1 ||
  247. op->addr.buswidth == 0))
  248. control |= CONTROL_MODE12_EX_RO;
  249. else if (op->cmd.buswidth == 1)
  250. control |= CONTROL_MODE12_EX_RW;
  251. else
  252. control |= CONTROL_MODE12_FULL;
  253. control |= CONTROL_MODE0;
  254. } else {
  255. control &= ~(CONTROL_MODE12_MASK | CONTROL_MODE0);
  256. }
  257. writel(control, qspi->regs + REG_CONTROL);
  258. return 0;
  259. }
  260. static int mchp_coreqspi_exec_op(struct spi_slave *slave,
  261. const struct spi_mem_op *op)
  262. {
  263. struct mchp_coreqspi *qspi = dev_get_priv(slave->dev->parent);
  264. u32 address = op->addr.val;
  265. u8 opcode = op->cmd.opcode;
  266. u8 opaddr[5];
  267. int err = 0, i;
  268. err = mchp_coreqspi_wait_for_ready(slave);
  269. if (err)
  270. return err;
  271. err = mchp_coreqspi_set_operate_mode(qspi, op);
  272. if (err)
  273. return err;
  274. mchp_coreqspi_config_op(qspi, op);
  275. if (op->cmd.opcode) {
  276. qspi->txbuf = &opcode;
  277. qspi->rxbuf = NULL;
  278. qspi->tx_len = op->cmd.nbytes;
  279. qspi->rx_len = 0;
  280. mchp_coreqspi_write_op(qspi, false);
  281. }
  282. qspi->txbuf = &opaddr[0];
  283. if (op->addr.nbytes) {
  284. for (i = 0; i < op->addr.nbytes; i++)
  285. qspi->txbuf[i] = address >> (8 * (op->addr.nbytes - i - 1));
  286. qspi->rxbuf = NULL;
  287. qspi->tx_len = op->addr.nbytes;
  288. qspi->rx_len = 0;
  289. mchp_coreqspi_write_op(qspi, false);
  290. }
  291. if (op->data.nbytes) {
  292. if (op->data.dir == SPI_MEM_DATA_OUT) {
  293. qspi->txbuf = (u8 *)op->data.buf.out;
  294. qspi->rxbuf = NULL;
  295. qspi->rx_len = 0;
  296. qspi->tx_len = op->data.nbytes;
  297. mchp_coreqspi_write_op(qspi, true);
  298. } else {
  299. qspi->txbuf = NULL;
  300. qspi->rxbuf = (u8 *)op->data.buf.in;
  301. qspi->rx_len = op->data.nbytes;
  302. qspi->tx_len = 0;
  303. mchp_coreqspi_read_op(qspi);
  304. }
  305. }
  306. return 0;
  307. }
  308. static bool mchp_coreqspi_supports_op(struct spi_slave *slave,
  309. const struct spi_mem_op *op)
  310. {
  311. if (!spi_mem_default_supports_op(slave, op))
  312. return false;
  313. if ((op->data.buswidth == 4 || op->data.buswidth == 2) &&
  314. (op->cmd.buswidth == 1 && (op->addr.buswidth == 1 ||
  315. op->addr.buswidth == 0))) {
  316. /*
  317. * If the command and address are on DQ0 only, then this
  318. * controller doesn't support sending data on dual and
  319. * quad lines. but it supports reading data on dual and
  320. * quad lines with same configuration as command and
  321. * address on DQ0.
  322. * i.e. The control register[15:13] :EX_RO(read only) is
  323. * meant only for the command and address are on DQ0 but
  324. * not to write data, it is just to read.
  325. * Ex: 0x34h is Quad Load Program Data which is not
  326. * supported. Then the spi-mem layer will iterate over
  327. * each command and it will chose the supported one.
  328. */
  329. if (op->data.dir == SPI_MEM_DATA_OUT)
  330. return false;
  331. }
  332. return true;
  333. }
  334. static int mchp_coreqspi_adjust_op_size(struct spi_slave *slave,
  335. struct spi_mem_op *op)
  336. {
  337. if (op->data.dir == SPI_MEM_DATA_OUT) {
  338. if (op->data.nbytes > MAX_DATA_CMD_LEN)
  339. op->data.nbytes = MAX_DATA_CMD_LEN;
  340. }
  341. return 0;
  342. }
  343. static int mchp_coreqspi_set_speed(struct udevice *dev, uint speed)
  344. {
  345. struct mchp_coreqspi *qspi = dev_get_priv(dev);
  346. u32 control, baud_rate_val = 0;
  347. if (speed > (qspi->freq / 2))
  348. speed = qspi->freq / 2;
  349. baud_rate_val = DIV_ROUND_UP(qspi->freq, 2 * speed);
  350. if (baud_rate_val >= MAX_DIVIDER || baud_rate_val <= MIN_DIVIDER)
  351. return -EINVAL;
  352. control = readl(qspi->regs + REG_CONTROL);
  353. control &= ~CONTROL_CLKRATE_MASK;
  354. control |= baud_rate_val << CONTROL_CLKRATE_SHIFT;
  355. writel(control, qspi->regs + REG_CONTROL);
  356. return 0;
  357. }
  358. static int mchp_coreqspi_set_mode(struct udevice *dev, uint mode)
  359. {
  360. struct mchp_coreqspi *qspi = dev_get_priv(dev);
  361. u32 control;
  362. control = readl(qspi->regs + REG_CONTROL);
  363. if ((mode & SPI_CPOL) && (mode & SPI_CPHA))
  364. control |= CONTROL_CLKIDLE;
  365. else
  366. control &= ~CONTROL_CLKIDLE;
  367. writel(control, qspi->regs + REG_CONTROL);
  368. return 0;
  369. }
  370. static int mchp_coreqspi_claim_bus(struct udevice *dev)
  371. {
  372. return 0;
  373. }
  374. static int mchp_coreqspi_release_bus(struct udevice *dev)
  375. {
  376. return 0;
  377. }
  378. static int mchp_coreqspi_probe(struct udevice *dev)
  379. {
  380. struct mchp_coreqspi *qspi = dev_get_priv(dev);
  381. struct clk clk;
  382. ulong clk_rate;
  383. int ret;
  384. ret = clk_get_by_index(dev, 0, &clk);
  385. if (ret)
  386. return -EINVAL;
  387. ret = clk_enable(&clk);
  388. if (ret)
  389. return ret;
  390. clk_rate = clk_get_rate(&clk);
  391. if (!clk_rate)
  392. return -EINVAL;
  393. qspi->freq = clk_rate;
  394. qspi->regs = dev_read_addr_ptr(dev);
  395. if (!qspi->regs)
  396. return -EINVAL;
  397. /* Init the mpfs qspi hw */
  398. mchp_coreqspi_init_hw(qspi);
  399. return 0;
  400. }
  401. static const struct spi_controller_mem_ops mchp_coreqspi_mem_ops = {
  402. .adjust_op_size = mchp_coreqspi_adjust_op_size,
  403. .supports_op = mchp_coreqspi_supports_op,
  404. .exec_op = mchp_coreqspi_exec_op,
  405. };
  406. static const struct dm_spi_ops mchp_coreqspi_ops = {
  407. .claim_bus = mchp_coreqspi_claim_bus,
  408. .release_bus = mchp_coreqspi_release_bus,
  409. .set_speed = mchp_coreqspi_set_speed,
  410. .set_mode = mchp_coreqspi_set_mode,
  411. .mem_ops = &mchp_coreqspi_mem_ops,
  412. };
  413. static const struct udevice_id mchp_coreqspi_ids[] = {
  414. { .compatible = "microchip,mpfs-coreqspi-rtl-v2" },
  415. { .compatible = "microchip,mpfs-qspi" },
  416. { }
  417. };
  418. U_BOOT_DRIVER(mchp_coreqspi) = {
  419. .name = "mchp_coreqspi",
  420. .id = UCLASS_SPI,
  421. .of_match = mchp_coreqspi_ids,
  422. .ops = &mchp_coreqspi_ops,
  423. .priv_auto = sizeof(struct mchp_coreqspi),
  424. .probe = mchp_coreqspi_probe,
  425. };