spi-zynqmp-gqspi.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Xilinx Zynq UltraScale+ MPSoC Quad-SPI (QSPI) controller driver
  4. * (master mode only)
  5. *
  6. * Copyright (C) 2009 - 2015 Xilinx, Inc.
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/delay.h>
  10. #include <linux/dma-mapping.h>
  11. #include <linux/dmaengine.h>
  12. #include <linux/firmware/xlnx-zynqmp.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/io.h>
  15. #include <linux/module.h>
  16. #include <linux/of_irq.h>
  17. #include <linux/of_address.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/pm_runtime.h>
  20. #include <linux/spi/spi.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/spi/spi-mem.h>
  24. /* Generic QSPI register offsets */
  25. #define GQSPI_CONFIG_OFST 0x00000100
  26. #define GQSPI_ISR_OFST 0x00000104
  27. #define GQSPI_IDR_OFST 0x0000010C
  28. #define GQSPI_IER_OFST 0x00000108
  29. #define GQSPI_IMASK_OFST 0x00000110
  30. #define GQSPI_EN_OFST 0x00000114
  31. #define GQSPI_TXD_OFST 0x0000011C
  32. #define GQSPI_RXD_OFST 0x00000120
  33. #define GQSPI_TX_THRESHOLD_OFST 0x00000128
  34. #define GQSPI_RX_THRESHOLD_OFST 0x0000012C
  35. #define GQSPI_LPBK_DLY_ADJ_OFST 0x00000138
  36. #define GQSPI_GEN_FIFO_OFST 0x00000140
  37. #define GQSPI_SEL_OFST 0x00000144
  38. #define GQSPI_GF_THRESHOLD_OFST 0x00000150
  39. #define GQSPI_FIFO_CTRL_OFST 0x0000014C
  40. #define GQSPI_QSPIDMA_DST_CTRL_OFST 0x0000080C
  41. #define GQSPI_QSPIDMA_DST_SIZE_OFST 0x00000804
  42. #define GQSPI_QSPIDMA_DST_STS_OFST 0x00000808
  43. #define GQSPI_QSPIDMA_DST_I_STS_OFST 0x00000814
  44. #define GQSPI_QSPIDMA_DST_I_EN_OFST 0x00000818
  45. #define GQSPI_QSPIDMA_DST_I_DIS_OFST 0x0000081C
  46. #define GQSPI_QSPIDMA_DST_I_MASK_OFST 0x00000820
  47. #define GQSPI_QSPIDMA_DST_ADDR_OFST 0x00000800
  48. #define GQSPI_QSPIDMA_DST_ADDR_MSB_OFST 0x00000828
  49. /* GQSPI register bit masks */
  50. #define GQSPI_SEL_MASK 0x00000001
  51. #define GQSPI_EN_MASK 0x00000001
  52. #define GQSPI_LPBK_DLY_ADJ_USE_LPBK_MASK 0x00000020
  53. #define GQSPI_ISR_WR_TO_CLR_MASK 0x00000002
  54. #define GQSPI_IDR_ALL_MASK 0x00000FBE
  55. #define GQSPI_CFG_MODE_EN_MASK 0xC0000000
  56. #define GQSPI_CFG_GEN_FIFO_START_MODE_MASK 0x20000000
  57. #define GQSPI_CFG_ENDIAN_MASK 0x04000000
  58. #define GQSPI_CFG_EN_POLL_TO_MASK 0x00100000
  59. #define GQSPI_CFG_WP_HOLD_MASK 0x00080000
  60. #define GQSPI_CFG_BAUD_RATE_DIV_MASK 0x00000038
  61. #define GQSPI_CFG_CLK_PHA_MASK 0x00000004
  62. #define GQSPI_CFG_CLK_POL_MASK 0x00000002
  63. #define GQSPI_CFG_START_GEN_FIFO_MASK 0x10000000
  64. #define GQSPI_GENFIFO_IMM_DATA_MASK 0x000000FF
  65. #define GQSPI_GENFIFO_DATA_XFER 0x00000100
  66. #define GQSPI_GENFIFO_EXP 0x00000200
  67. #define GQSPI_GENFIFO_MODE_SPI 0x00000400
  68. #define GQSPI_GENFIFO_MODE_DUALSPI 0x00000800
  69. #define GQSPI_GENFIFO_MODE_QUADSPI 0x00000C00
  70. #define GQSPI_GENFIFO_MODE_MASK 0x00000C00
  71. #define GQSPI_GENFIFO_CS_LOWER 0x00001000
  72. #define GQSPI_GENFIFO_CS_UPPER 0x00002000
  73. #define GQSPI_GENFIFO_BUS_LOWER 0x00004000
  74. #define GQSPI_GENFIFO_BUS_UPPER 0x00008000
  75. #define GQSPI_GENFIFO_BUS_BOTH 0x0000C000
  76. #define GQSPI_GENFIFO_BUS_MASK 0x0000C000
  77. #define GQSPI_GENFIFO_TX 0x00010000
  78. #define GQSPI_GENFIFO_RX 0x00020000
  79. #define GQSPI_GENFIFO_STRIPE 0x00040000
  80. #define GQSPI_GENFIFO_POLL 0x00080000
  81. #define GQSPI_GENFIFO_EXP_START 0x00000100
  82. #define GQSPI_FIFO_CTRL_RST_RX_FIFO_MASK 0x00000004
  83. #define GQSPI_FIFO_CTRL_RST_TX_FIFO_MASK 0x00000002
  84. #define GQSPI_FIFO_CTRL_RST_GEN_FIFO_MASK 0x00000001
  85. #define GQSPI_ISR_RXEMPTY_MASK 0x00000800
  86. #define GQSPI_ISR_GENFIFOFULL_MASK 0x00000400
  87. #define GQSPI_ISR_GENFIFONOT_FULL_MASK 0x00000200
  88. #define GQSPI_ISR_TXEMPTY_MASK 0x00000100
  89. #define GQSPI_ISR_GENFIFOEMPTY_MASK 0x00000080
  90. #define GQSPI_ISR_RXFULL_MASK 0x00000020
  91. #define GQSPI_ISR_RXNEMPTY_MASK 0x00000010
  92. #define GQSPI_ISR_TXFULL_MASK 0x00000008
  93. #define GQSPI_ISR_TXNOT_FULL_MASK 0x00000004
  94. #define GQSPI_ISR_POLL_TIME_EXPIRE_MASK 0x00000002
  95. #define GQSPI_IER_TXNOT_FULL_MASK 0x00000004
  96. #define GQSPI_IER_RXEMPTY_MASK 0x00000800
  97. #define GQSPI_IER_POLL_TIME_EXPIRE_MASK 0x00000002
  98. #define GQSPI_IER_RXNEMPTY_MASK 0x00000010
  99. #define GQSPI_IER_GENFIFOEMPTY_MASK 0x00000080
  100. #define GQSPI_IER_TXEMPTY_MASK 0x00000100
  101. #define GQSPI_QSPIDMA_DST_INTR_ALL_MASK 0x000000FE
  102. #define GQSPI_QSPIDMA_DST_STS_WTC 0x0000E000
  103. #define GQSPI_CFG_MODE_EN_DMA_MASK 0x80000000
  104. #define GQSPI_ISR_IDR_MASK 0x00000994
  105. #define GQSPI_QSPIDMA_DST_I_EN_DONE_MASK 0x00000002
  106. #define GQSPI_QSPIDMA_DST_I_STS_DONE_MASK 0x00000002
  107. #define GQSPI_IRQ_MASK 0x00000980
  108. #define GQSPI_CFG_BAUD_RATE_DIV_SHIFT 3
  109. #define GQSPI_GENFIFO_CS_SETUP 0x4
  110. #define GQSPI_GENFIFO_CS_HOLD 0x3
  111. #define GQSPI_TXD_DEPTH 64
  112. #define GQSPI_RX_FIFO_THRESHOLD 32
  113. #define GQSPI_RX_FIFO_FILL (GQSPI_RX_FIFO_THRESHOLD * 4)
  114. #define GQSPI_TX_FIFO_THRESHOLD_RESET_VAL 32
  115. #define GQSPI_TX_FIFO_FILL (GQSPI_TXD_DEPTH -\
  116. GQSPI_TX_FIFO_THRESHOLD_RESET_VAL)
  117. #define GQSPI_GEN_FIFO_THRESHOLD_RESET_VAL 0X10
  118. #define GQSPI_QSPIDMA_DST_CTRL_RESET_VAL 0x803FFA00
  119. #define GQSPI_SELECT_FLASH_CS_LOWER 0x1
  120. #define GQSPI_SELECT_FLASH_CS_UPPER 0x2
  121. #define GQSPI_SELECT_FLASH_CS_BOTH 0x3
  122. #define GQSPI_SELECT_FLASH_BUS_LOWER 0x1
  123. #define GQSPI_SELECT_FLASH_BUS_UPPER 0x2
  124. #define GQSPI_SELECT_FLASH_BUS_BOTH 0x3
  125. #define GQSPI_BAUD_DIV_MAX 7 /* Baud rate divisor maximum */
  126. #define GQSPI_BAUD_DIV_SHIFT 2 /* Baud rate divisor shift */
  127. #define GQSPI_SELECT_MODE_SPI 0x1
  128. #define GQSPI_SELECT_MODE_DUALSPI 0x2
  129. #define GQSPI_SELECT_MODE_QUADSPI 0x4
  130. #define GQSPI_DMA_UNALIGN 0x3
  131. #define GQSPI_DEFAULT_NUM_CS 1 /* Default number of chip selects */
  132. #define GQSPI_MAX_NUM_CS 2 /* Maximum number of chip selects */
  133. #define SPI_AUTOSUSPEND_TIMEOUT 3000
  134. enum mode_type {GQSPI_MODE_IO, GQSPI_MODE_DMA};
  135. /**
  136. * struct zynqmp_qspi - Defines qspi driver instance
  137. * @regs: Virtual address of the QSPI controller registers
  138. * @refclk: Pointer to the peripheral clock
  139. * @pclk: Pointer to the APB clock
  140. * @irq: IRQ number
  141. * @dev: Pointer to struct device
  142. * @txbuf: Pointer to the TX buffer
  143. * @rxbuf: Pointer to the RX buffer
  144. * @bytes_to_transfer: Number of bytes left to transfer
  145. * @bytes_to_receive: Number of bytes left to receive
  146. * @genfifocs: Used for chip select
  147. * @genfifobus: Used to select the upper or lower bus
  148. * @dma_rx_bytes: Remaining bytes to receive by DMA mode
  149. * @dma_addr: DMA address after mapping the kernel buffer
  150. * @genfifoentry: Used for storing the genfifoentry instruction.
  151. * @mode: Defines the mode in which QSPI is operating
  152. * @data_completion: completion structure
  153. */
  154. struct zynqmp_qspi {
  155. struct spi_controller *ctlr;
  156. void __iomem *regs;
  157. struct clk *refclk;
  158. struct clk *pclk;
  159. int irq;
  160. struct device *dev;
  161. const void *txbuf;
  162. void *rxbuf;
  163. int bytes_to_transfer;
  164. int bytes_to_receive;
  165. u32 genfifocs;
  166. u32 genfifobus;
  167. u32 dma_rx_bytes;
  168. dma_addr_t dma_addr;
  169. u32 genfifoentry;
  170. enum mode_type mode;
  171. struct completion data_completion;
  172. struct mutex op_lock;
  173. };
  174. /**
  175. * zynqmp_gqspi_read - For GQSPI controller read operation
  176. * @xqspi: Pointer to the zynqmp_qspi structure
  177. * @offset: Offset from where to read
  178. * Return: Value at the offset
  179. */
  180. static u32 zynqmp_gqspi_read(struct zynqmp_qspi *xqspi, u32 offset)
  181. {
  182. return readl_relaxed(xqspi->regs + offset);
  183. }
  184. /**
  185. * zynqmp_gqspi_write - For GQSPI controller write operation
  186. * @xqspi: Pointer to the zynqmp_qspi structure
  187. * @offset: Offset where to write
  188. * @val: Value to be written
  189. */
  190. static inline void zynqmp_gqspi_write(struct zynqmp_qspi *xqspi, u32 offset,
  191. u32 val)
  192. {
  193. writel_relaxed(val, (xqspi->regs + offset));
  194. }
  195. /**
  196. * zynqmp_gqspi_selectslave - For selection of slave device
  197. * @instanceptr: Pointer to the zynqmp_qspi structure
  198. * @slavecs: For chip select
  199. * @slavebus: To check which bus is selected- upper or lower
  200. */
  201. static void zynqmp_gqspi_selectslave(struct zynqmp_qspi *instanceptr,
  202. u8 slavecs, u8 slavebus)
  203. {
  204. /*
  205. * Bus and CS lines selected here will be updated in the instance and
  206. * used for subsequent GENFIFO entries during transfer.
  207. */
  208. /* Choose slave select line */
  209. switch (slavecs) {
  210. case GQSPI_SELECT_FLASH_CS_BOTH:
  211. instanceptr->genfifocs = GQSPI_GENFIFO_CS_LOWER |
  212. GQSPI_GENFIFO_CS_UPPER;
  213. break;
  214. case GQSPI_SELECT_FLASH_CS_UPPER:
  215. instanceptr->genfifocs = GQSPI_GENFIFO_CS_UPPER;
  216. break;
  217. case GQSPI_SELECT_FLASH_CS_LOWER:
  218. instanceptr->genfifocs = GQSPI_GENFIFO_CS_LOWER;
  219. break;
  220. default:
  221. dev_warn(instanceptr->dev, "Invalid slave select\n");
  222. }
  223. /* Choose the bus */
  224. switch (slavebus) {
  225. case GQSPI_SELECT_FLASH_BUS_BOTH:
  226. instanceptr->genfifobus = GQSPI_GENFIFO_BUS_LOWER |
  227. GQSPI_GENFIFO_BUS_UPPER;
  228. break;
  229. case GQSPI_SELECT_FLASH_BUS_UPPER:
  230. instanceptr->genfifobus = GQSPI_GENFIFO_BUS_UPPER;
  231. break;
  232. case GQSPI_SELECT_FLASH_BUS_LOWER:
  233. instanceptr->genfifobus = GQSPI_GENFIFO_BUS_LOWER;
  234. break;
  235. default:
  236. dev_warn(instanceptr->dev, "Invalid slave bus\n");
  237. }
  238. }
  239. /**
  240. * zynqmp_qspi_init_hw - Initialize the hardware
  241. * @xqspi: Pointer to the zynqmp_qspi structure
  242. *
  243. * The default settings of the QSPI controller's configurable parameters on
  244. * reset are
  245. * - Master mode
  246. * - TX threshold set to 1
  247. * - RX threshold set to 1
  248. * - Flash memory interface mode enabled
  249. * This function performs the following actions
  250. * - Disable and clear all the interrupts
  251. * - Enable manual slave select
  252. * - Enable manual start
  253. * - Deselect all the chip select lines
  254. * - Set the little endian mode of TX FIFO and
  255. * - Enable the QSPI controller
  256. */
  257. static void zynqmp_qspi_init_hw(struct zynqmp_qspi *xqspi)
  258. {
  259. u32 config_reg;
  260. /* Select the GQSPI mode */
  261. zynqmp_gqspi_write(xqspi, GQSPI_SEL_OFST, GQSPI_SEL_MASK);
  262. /* Clear and disable interrupts */
  263. zynqmp_gqspi_write(xqspi, GQSPI_ISR_OFST,
  264. zynqmp_gqspi_read(xqspi, GQSPI_ISR_OFST) |
  265. GQSPI_ISR_WR_TO_CLR_MASK);
  266. /* Clear the DMA STS */
  267. zynqmp_gqspi_write(xqspi, GQSPI_QSPIDMA_DST_I_STS_OFST,
  268. zynqmp_gqspi_read(xqspi,
  269. GQSPI_QSPIDMA_DST_I_STS_OFST));
  270. zynqmp_gqspi_write(xqspi, GQSPI_QSPIDMA_DST_STS_OFST,
  271. zynqmp_gqspi_read(xqspi,
  272. GQSPI_QSPIDMA_DST_STS_OFST) |
  273. GQSPI_QSPIDMA_DST_STS_WTC);
  274. zynqmp_gqspi_write(xqspi, GQSPI_IDR_OFST, GQSPI_IDR_ALL_MASK);
  275. zynqmp_gqspi_write(xqspi,
  276. GQSPI_QSPIDMA_DST_I_DIS_OFST,
  277. GQSPI_QSPIDMA_DST_INTR_ALL_MASK);
  278. /* Disable the GQSPI */
  279. zynqmp_gqspi_write(xqspi, GQSPI_EN_OFST, 0x0);
  280. config_reg = zynqmp_gqspi_read(xqspi, GQSPI_CONFIG_OFST);
  281. config_reg &= ~GQSPI_CFG_MODE_EN_MASK;
  282. /* Manual start */
  283. config_reg |= GQSPI_CFG_GEN_FIFO_START_MODE_MASK;
  284. /* Little endian by default */
  285. config_reg &= ~GQSPI_CFG_ENDIAN_MASK;
  286. /* Disable poll time out */
  287. config_reg &= ~GQSPI_CFG_EN_POLL_TO_MASK;
  288. /* Set hold bit */
  289. config_reg |= GQSPI_CFG_WP_HOLD_MASK;
  290. /* Clear pre-scalar by default */
  291. config_reg &= ~GQSPI_CFG_BAUD_RATE_DIV_MASK;
  292. /* CPHA 0 */
  293. config_reg &= ~GQSPI_CFG_CLK_PHA_MASK;
  294. /* CPOL 0 */
  295. config_reg &= ~GQSPI_CFG_CLK_POL_MASK;
  296. zynqmp_gqspi_write(xqspi, GQSPI_CONFIG_OFST, config_reg);
  297. /* Clear the TX and RX FIFO */
  298. zynqmp_gqspi_write(xqspi, GQSPI_FIFO_CTRL_OFST,
  299. GQSPI_FIFO_CTRL_RST_RX_FIFO_MASK |
  300. GQSPI_FIFO_CTRL_RST_TX_FIFO_MASK |
  301. GQSPI_FIFO_CTRL_RST_GEN_FIFO_MASK);
  302. /* Set by default to allow for high frequencies */
  303. zynqmp_gqspi_write(xqspi, GQSPI_LPBK_DLY_ADJ_OFST,
  304. zynqmp_gqspi_read(xqspi, GQSPI_LPBK_DLY_ADJ_OFST) |
  305. GQSPI_LPBK_DLY_ADJ_USE_LPBK_MASK);
  306. /* Reset thresholds */
  307. zynqmp_gqspi_write(xqspi, GQSPI_TX_THRESHOLD_OFST,
  308. GQSPI_TX_FIFO_THRESHOLD_RESET_VAL);
  309. zynqmp_gqspi_write(xqspi, GQSPI_RX_THRESHOLD_OFST,
  310. GQSPI_RX_FIFO_THRESHOLD);
  311. zynqmp_gqspi_write(xqspi, GQSPI_GF_THRESHOLD_OFST,
  312. GQSPI_GEN_FIFO_THRESHOLD_RESET_VAL);
  313. zynqmp_gqspi_selectslave(xqspi,
  314. GQSPI_SELECT_FLASH_CS_LOWER,
  315. GQSPI_SELECT_FLASH_BUS_LOWER);
  316. /* Initialize DMA */
  317. zynqmp_gqspi_write(xqspi,
  318. GQSPI_QSPIDMA_DST_CTRL_OFST,
  319. GQSPI_QSPIDMA_DST_CTRL_RESET_VAL);
  320. /* Enable the GQSPI */
  321. zynqmp_gqspi_write(xqspi, GQSPI_EN_OFST, GQSPI_EN_MASK);
  322. }
  323. /**
  324. * zynqmp_qspi_copy_read_data - Copy data to RX buffer
  325. * @xqspi: Pointer to the zynqmp_qspi structure
  326. * @data: The variable where data is stored
  327. * @size: Number of bytes to be copied from data to RX buffer
  328. */
  329. static void zynqmp_qspi_copy_read_data(struct zynqmp_qspi *xqspi,
  330. ulong data, u8 size)
  331. {
  332. memcpy(xqspi->rxbuf, &data, size);
  333. xqspi->rxbuf += size;
  334. xqspi->bytes_to_receive -= size;
  335. }
  336. /**
  337. * zynqmp_qspi_chipselect - Select or deselect the chip select line
  338. * @qspi: Pointer to the spi_device structure
  339. * @is_high: Select(0) or deselect (1) the chip select line
  340. */
  341. static void zynqmp_qspi_chipselect(struct spi_device *qspi, bool is_high)
  342. {
  343. struct zynqmp_qspi *xqspi = spi_master_get_devdata(qspi->master);
  344. ulong timeout;
  345. u32 genfifoentry = 0, statusreg;
  346. genfifoentry |= GQSPI_GENFIFO_MODE_SPI;
  347. if (!is_high) {
  348. if (!qspi->chip_select) {
  349. xqspi->genfifobus = GQSPI_GENFIFO_BUS_LOWER;
  350. xqspi->genfifocs = GQSPI_GENFIFO_CS_LOWER;
  351. } else {
  352. xqspi->genfifobus = GQSPI_GENFIFO_BUS_UPPER;
  353. xqspi->genfifocs = GQSPI_GENFIFO_CS_UPPER;
  354. }
  355. genfifoentry |= xqspi->genfifobus;
  356. genfifoentry |= xqspi->genfifocs;
  357. genfifoentry |= GQSPI_GENFIFO_CS_SETUP;
  358. } else {
  359. genfifoentry |= GQSPI_GENFIFO_CS_HOLD;
  360. }
  361. zynqmp_gqspi_write(xqspi, GQSPI_GEN_FIFO_OFST, genfifoentry);
  362. /* Manually start the generic FIFO command */
  363. zynqmp_gqspi_write(xqspi, GQSPI_CONFIG_OFST,
  364. zynqmp_gqspi_read(xqspi, GQSPI_CONFIG_OFST) |
  365. GQSPI_CFG_START_GEN_FIFO_MASK);
  366. timeout = jiffies + msecs_to_jiffies(1000);
  367. /* Wait until the generic FIFO command is empty */
  368. do {
  369. statusreg = zynqmp_gqspi_read(xqspi, GQSPI_ISR_OFST);
  370. if ((statusreg & GQSPI_ISR_GENFIFOEMPTY_MASK) &&
  371. (statusreg & GQSPI_ISR_TXEMPTY_MASK))
  372. break;
  373. cpu_relax();
  374. } while (!time_after_eq(jiffies, timeout));
  375. if (time_after_eq(jiffies, timeout))
  376. dev_err(xqspi->dev, "Chip select timed out\n");
  377. }
  378. /**
  379. * zynqmp_qspi_selectspimode - Selects SPI mode - x1 or x2 or x4.
  380. * @xqspi: xqspi is a pointer to the GQSPI instance
  381. * @spimode: spimode - SPI or DUAL or QUAD.
  382. * Return: Mask to set desired SPI mode in GENFIFO entry.
  383. */
  384. static inline u32 zynqmp_qspi_selectspimode(struct zynqmp_qspi *xqspi,
  385. u8 spimode)
  386. {
  387. u32 mask = 0;
  388. switch (spimode) {
  389. case GQSPI_SELECT_MODE_DUALSPI:
  390. mask = GQSPI_GENFIFO_MODE_DUALSPI;
  391. break;
  392. case GQSPI_SELECT_MODE_QUADSPI:
  393. mask = GQSPI_GENFIFO_MODE_QUADSPI;
  394. break;
  395. case GQSPI_SELECT_MODE_SPI:
  396. mask = GQSPI_GENFIFO_MODE_SPI;
  397. break;
  398. default:
  399. dev_warn(xqspi->dev, "Invalid SPI mode\n");
  400. }
  401. return mask;
  402. }
  403. /**
  404. * zynqmp_qspi_config_op - Configure QSPI controller for specified
  405. * transfer
  406. * @xqspi: Pointer to the zynqmp_qspi structure
  407. * @qspi: Pointer to the spi_device structure
  408. *
  409. * Sets the operational mode of QSPI controller for the next QSPI transfer and
  410. * sets the requested clock frequency.
  411. *
  412. * Return: Always 0
  413. *
  414. * Note:
  415. * If the requested frequency is not an exact match with what can be
  416. * obtained using the pre-scalar value, the driver sets the clock
  417. * frequency which is lower than the requested frequency (maximum lower)
  418. * for the transfer.
  419. *
  420. * If the requested frequency is higher or lower than that is supported
  421. * by the QSPI controller the driver will set the highest or lowest
  422. * frequency supported by controller.
  423. */
  424. static int zynqmp_qspi_config_op(struct zynqmp_qspi *xqspi,
  425. struct spi_device *qspi)
  426. {
  427. ulong clk_rate;
  428. u32 config_reg, baud_rate_val = 0;
  429. /* Set the clock frequency */
  430. /* If req_hz == 0, default to lowest speed */
  431. clk_rate = clk_get_rate(xqspi->refclk);
  432. while ((baud_rate_val < GQSPI_BAUD_DIV_MAX) &&
  433. (clk_rate /
  434. (GQSPI_BAUD_DIV_SHIFT << baud_rate_val)) > qspi->max_speed_hz)
  435. baud_rate_val++;
  436. config_reg = zynqmp_gqspi_read(xqspi, GQSPI_CONFIG_OFST);
  437. /* Set the QSPI clock phase and clock polarity */
  438. config_reg &= (~GQSPI_CFG_CLK_PHA_MASK) & (~GQSPI_CFG_CLK_POL_MASK);
  439. if (qspi->mode & SPI_CPHA)
  440. config_reg |= GQSPI_CFG_CLK_PHA_MASK;
  441. if (qspi->mode & SPI_CPOL)
  442. config_reg |= GQSPI_CFG_CLK_POL_MASK;
  443. config_reg &= ~GQSPI_CFG_BAUD_RATE_DIV_MASK;
  444. config_reg |= (baud_rate_val << GQSPI_CFG_BAUD_RATE_DIV_SHIFT);
  445. zynqmp_gqspi_write(xqspi, GQSPI_CONFIG_OFST, config_reg);
  446. return 0;
  447. }
  448. /**
  449. * zynqmp_qspi_setup_op - Configure the QSPI controller
  450. * @qspi: Pointer to the spi_device structure
  451. *
  452. * Sets the operational mode of QSPI controller for the next QSPI transfer,
  453. * baud rate and divisor value to setup the requested qspi clock.
  454. *
  455. * Return: 0 on success; error value otherwise.
  456. */
  457. static int zynqmp_qspi_setup_op(struct spi_device *qspi)
  458. {
  459. struct spi_controller *ctlr = qspi->master;
  460. struct zynqmp_qspi *xqspi = spi_controller_get_devdata(ctlr);
  461. if (ctlr->busy)
  462. return -EBUSY;
  463. zynqmp_gqspi_write(xqspi, GQSPI_EN_OFST, GQSPI_EN_MASK);
  464. return 0;
  465. }
  466. /**
  467. * zynqmp_qspi_filltxfifo - Fills the TX FIFO as long as there is room in
  468. * the FIFO or the bytes required to be
  469. * transmitted.
  470. * @xqspi: Pointer to the zynqmp_qspi structure
  471. * @size: Number of bytes to be copied from TX buffer to TX FIFO
  472. */
  473. static void zynqmp_qspi_filltxfifo(struct zynqmp_qspi *xqspi, int size)
  474. {
  475. u32 count = 0, intermediate;
  476. while ((xqspi->bytes_to_transfer > 0) && (count < size) && (xqspi->txbuf)) {
  477. if (xqspi->bytes_to_transfer >= 4) {
  478. memcpy(&intermediate, xqspi->txbuf, 4);
  479. xqspi->txbuf += 4;
  480. xqspi->bytes_to_transfer -= 4;
  481. count += 4;
  482. } else {
  483. memcpy(&intermediate, xqspi->txbuf,
  484. xqspi->bytes_to_transfer);
  485. xqspi->txbuf += xqspi->bytes_to_transfer;
  486. xqspi->bytes_to_transfer = 0;
  487. count += xqspi->bytes_to_transfer;
  488. }
  489. zynqmp_gqspi_write(xqspi, GQSPI_TXD_OFST, intermediate);
  490. }
  491. }
  492. /**
  493. * zynqmp_qspi_readrxfifo - Fills the RX FIFO as long as there is room in
  494. * the FIFO.
  495. * @xqspi: Pointer to the zynqmp_qspi structure
  496. * @size: Number of bytes to be copied from RX buffer to RX FIFO
  497. */
  498. static void zynqmp_qspi_readrxfifo(struct zynqmp_qspi *xqspi, u32 size)
  499. {
  500. ulong data;
  501. int count = 0;
  502. while ((count < size) && (xqspi->bytes_to_receive > 0)) {
  503. if (xqspi->bytes_to_receive >= 4) {
  504. (*(u32 *)xqspi->rxbuf) =
  505. zynqmp_gqspi_read(xqspi, GQSPI_RXD_OFST);
  506. xqspi->rxbuf += 4;
  507. xqspi->bytes_to_receive -= 4;
  508. count += 4;
  509. } else {
  510. data = zynqmp_gqspi_read(xqspi, GQSPI_RXD_OFST);
  511. count += xqspi->bytes_to_receive;
  512. zynqmp_qspi_copy_read_data(xqspi, data,
  513. xqspi->bytes_to_receive);
  514. xqspi->bytes_to_receive = 0;
  515. }
  516. }
  517. }
  518. /**
  519. * zynqmp_qspi_fillgenfifo - Fills the GENFIFO.
  520. * @xqspi: Pointer to the zynqmp_qspi structure
  521. * @nbits: Transfer/Receive buswidth.
  522. * @genfifoentry: Variable in which GENFIFO mask is saved
  523. */
  524. static void zynqmp_qspi_fillgenfifo(struct zynqmp_qspi *xqspi, u8 nbits,
  525. u32 genfifoentry)
  526. {
  527. u32 transfer_len = 0;
  528. if (xqspi->txbuf) {
  529. genfifoentry &= ~GQSPI_GENFIFO_RX;
  530. genfifoentry |= GQSPI_GENFIFO_DATA_XFER;
  531. genfifoentry |= GQSPI_GENFIFO_TX;
  532. transfer_len = xqspi->bytes_to_transfer;
  533. } else if (xqspi->rxbuf) {
  534. genfifoentry &= ~GQSPI_GENFIFO_TX;
  535. genfifoentry |= GQSPI_GENFIFO_DATA_XFER;
  536. genfifoentry |= GQSPI_GENFIFO_RX;
  537. if (xqspi->mode == GQSPI_MODE_DMA)
  538. transfer_len = xqspi->dma_rx_bytes;
  539. else
  540. transfer_len = xqspi->bytes_to_receive;
  541. } else {
  542. /* Sending dummy circles here */
  543. genfifoentry &= ~(GQSPI_GENFIFO_TX | GQSPI_GENFIFO_RX);
  544. genfifoentry |= GQSPI_GENFIFO_DATA_XFER;
  545. transfer_len = xqspi->bytes_to_transfer;
  546. }
  547. genfifoentry |= zynqmp_qspi_selectspimode(xqspi, nbits);
  548. xqspi->genfifoentry = genfifoentry;
  549. if ((transfer_len) < GQSPI_GENFIFO_IMM_DATA_MASK) {
  550. genfifoentry &= ~GQSPI_GENFIFO_IMM_DATA_MASK;
  551. genfifoentry |= transfer_len;
  552. zynqmp_gqspi_write(xqspi, GQSPI_GEN_FIFO_OFST, genfifoentry);
  553. } else {
  554. int tempcount = transfer_len;
  555. u32 exponent = 8; /* 2^8 = 256 */
  556. u8 imm_data = tempcount & 0xFF;
  557. tempcount &= ~(tempcount & 0xFF);
  558. /* Immediate entry */
  559. if (tempcount != 0) {
  560. /* Exponent entries */
  561. genfifoentry |= GQSPI_GENFIFO_EXP;
  562. while (tempcount != 0) {
  563. if (tempcount & GQSPI_GENFIFO_EXP_START) {
  564. genfifoentry &=
  565. ~GQSPI_GENFIFO_IMM_DATA_MASK;
  566. genfifoentry |= exponent;
  567. zynqmp_gqspi_write(xqspi,
  568. GQSPI_GEN_FIFO_OFST,
  569. genfifoentry);
  570. }
  571. tempcount = tempcount >> 1;
  572. exponent++;
  573. }
  574. }
  575. if (imm_data != 0) {
  576. genfifoentry &= ~GQSPI_GENFIFO_EXP;
  577. genfifoentry &= ~GQSPI_GENFIFO_IMM_DATA_MASK;
  578. genfifoentry |= (u8)(imm_data & 0xFF);
  579. zynqmp_gqspi_write(xqspi, GQSPI_GEN_FIFO_OFST,
  580. genfifoentry);
  581. }
  582. }
  583. if (xqspi->mode == GQSPI_MODE_IO && xqspi->rxbuf) {
  584. /* Dummy generic FIFO entry */
  585. zynqmp_gqspi_write(xqspi, GQSPI_GEN_FIFO_OFST, 0x0);
  586. }
  587. }
  588. /**
  589. * zynqmp_process_dma_irq - Handler for DMA done interrupt of QSPI
  590. * controller
  591. * @xqspi: zynqmp_qspi instance pointer
  592. *
  593. * This function handles DMA interrupt only.
  594. */
  595. static void zynqmp_process_dma_irq(struct zynqmp_qspi *xqspi)
  596. {
  597. u32 config_reg, genfifoentry;
  598. dma_unmap_single(xqspi->dev, xqspi->dma_addr,
  599. xqspi->dma_rx_bytes, DMA_FROM_DEVICE);
  600. xqspi->rxbuf += xqspi->dma_rx_bytes;
  601. xqspi->bytes_to_receive -= xqspi->dma_rx_bytes;
  602. xqspi->dma_rx_bytes = 0;
  603. /* Disabling the DMA interrupts */
  604. zynqmp_gqspi_write(xqspi, GQSPI_QSPIDMA_DST_I_DIS_OFST,
  605. GQSPI_QSPIDMA_DST_I_EN_DONE_MASK);
  606. if (xqspi->bytes_to_receive > 0) {
  607. /* Switch to IO mode,for remaining bytes to receive */
  608. config_reg = zynqmp_gqspi_read(xqspi, GQSPI_CONFIG_OFST);
  609. config_reg &= ~GQSPI_CFG_MODE_EN_MASK;
  610. zynqmp_gqspi_write(xqspi, GQSPI_CONFIG_OFST, config_reg);
  611. /* Initiate the transfer of remaining bytes */
  612. genfifoentry = xqspi->genfifoentry;
  613. genfifoentry |= xqspi->bytes_to_receive;
  614. zynqmp_gqspi_write(xqspi, GQSPI_GEN_FIFO_OFST, genfifoentry);
  615. /* Dummy generic FIFO entry */
  616. zynqmp_gqspi_write(xqspi, GQSPI_GEN_FIFO_OFST, 0x0);
  617. /* Manual start */
  618. zynqmp_gqspi_write(xqspi, GQSPI_CONFIG_OFST,
  619. (zynqmp_gqspi_read(xqspi,
  620. GQSPI_CONFIG_OFST) |
  621. GQSPI_CFG_START_GEN_FIFO_MASK));
  622. /* Enable the RX interrupts for IO mode */
  623. zynqmp_gqspi_write(xqspi, GQSPI_IER_OFST,
  624. GQSPI_IER_GENFIFOEMPTY_MASK |
  625. GQSPI_IER_RXNEMPTY_MASK |
  626. GQSPI_IER_RXEMPTY_MASK);
  627. }
  628. }
  629. /**
  630. * zynqmp_qspi_irq - Interrupt service routine of the QSPI controller
  631. * @irq: IRQ number
  632. * @dev_id: Pointer to the xqspi structure
  633. *
  634. * This function handles TX empty only.
  635. * On TX empty interrupt this function reads the received data from RX FIFO
  636. * and fills the TX FIFO if there is any data remaining to be transferred.
  637. *
  638. * Return: IRQ_HANDLED when interrupt is handled
  639. * IRQ_NONE otherwise.
  640. */
  641. static irqreturn_t zynqmp_qspi_irq(int irq, void *dev_id)
  642. {
  643. struct zynqmp_qspi *xqspi = (struct zynqmp_qspi *)dev_id;
  644. irqreturn_t ret = IRQ_NONE;
  645. u32 status, mask, dma_status = 0;
  646. status = zynqmp_gqspi_read(xqspi, GQSPI_ISR_OFST);
  647. zynqmp_gqspi_write(xqspi, GQSPI_ISR_OFST, status);
  648. mask = (status & ~(zynqmp_gqspi_read(xqspi, GQSPI_IMASK_OFST)));
  649. /* Read and clear DMA status */
  650. if (xqspi->mode == GQSPI_MODE_DMA) {
  651. dma_status =
  652. zynqmp_gqspi_read(xqspi, GQSPI_QSPIDMA_DST_I_STS_OFST);
  653. zynqmp_gqspi_write(xqspi, GQSPI_QSPIDMA_DST_I_STS_OFST,
  654. dma_status);
  655. }
  656. if (mask & GQSPI_ISR_TXNOT_FULL_MASK) {
  657. zynqmp_qspi_filltxfifo(xqspi, GQSPI_TX_FIFO_FILL);
  658. ret = IRQ_HANDLED;
  659. }
  660. if (dma_status & GQSPI_QSPIDMA_DST_I_STS_DONE_MASK) {
  661. zynqmp_process_dma_irq(xqspi);
  662. ret = IRQ_HANDLED;
  663. } else if (!(mask & GQSPI_IER_RXEMPTY_MASK) &&
  664. (mask & GQSPI_IER_GENFIFOEMPTY_MASK)) {
  665. zynqmp_qspi_readrxfifo(xqspi, GQSPI_RX_FIFO_FILL);
  666. ret = IRQ_HANDLED;
  667. }
  668. if (xqspi->bytes_to_receive == 0 && xqspi->bytes_to_transfer == 0 &&
  669. ((status & GQSPI_IRQ_MASK) == GQSPI_IRQ_MASK)) {
  670. zynqmp_gqspi_write(xqspi, GQSPI_IDR_OFST, GQSPI_ISR_IDR_MASK);
  671. complete(&xqspi->data_completion);
  672. ret = IRQ_HANDLED;
  673. }
  674. return ret;
  675. }
  676. /**
  677. * zynqmp_qspi_setuprxdma - This function sets up the RX DMA operation
  678. * @xqspi: xqspi is a pointer to the GQSPI instance.
  679. */
  680. static int zynqmp_qspi_setuprxdma(struct zynqmp_qspi *xqspi)
  681. {
  682. u32 rx_bytes, rx_rem, config_reg;
  683. dma_addr_t addr;
  684. u64 dma_align = (u64)(uintptr_t)xqspi->rxbuf;
  685. if (xqspi->bytes_to_receive < 8 ||
  686. ((dma_align & GQSPI_DMA_UNALIGN) != 0x0)) {
  687. /* Setting to IO mode */
  688. config_reg = zynqmp_gqspi_read(xqspi, GQSPI_CONFIG_OFST);
  689. config_reg &= ~GQSPI_CFG_MODE_EN_MASK;
  690. zynqmp_gqspi_write(xqspi, GQSPI_CONFIG_OFST, config_reg);
  691. xqspi->mode = GQSPI_MODE_IO;
  692. xqspi->dma_rx_bytes = 0;
  693. return 0;
  694. }
  695. rx_rem = xqspi->bytes_to_receive % 4;
  696. rx_bytes = (xqspi->bytes_to_receive - rx_rem);
  697. addr = dma_map_single(xqspi->dev, (void *)xqspi->rxbuf,
  698. rx_bytes, DMA_FROM_DEVICE);
  699. if (dma_mapping_error(xqspi->dev, addr)) {
  700. dev_err(xqspi->dev, "ERR:rxdma:memory not mapped\n");
  701. return -ENOMEM;
  702. }
  703. xqspi->dma_rx_bytes = rx_bytes;
  704. xqspi->dma_addr = addr;
  705. zynqmp_gqspi_write(xqspi, GQSPI_QSPIDMA_DST_ADDR_OFST,
  706. (u32)(addr & 0xffffffff));
  707. addr = ((addr >> 16) >> 16);
  708. zynqmp_gqspi_write(xqspi, GQSPI_QSPIDMA_DST_ADDR_MSB_OFST,
  709. ((u32)addr) & 0xfff);
  710. /* Enabling the DMA mode */
  711. config_reg = zynqmp_gqspi_read(xqspi, GQSPI_CONFIG_OFST);
  712. config_reg &= ~GQSPI_CFG_MODE_EN_MASK;
  713. config_reg |= GQSPI_CFG_MODE_EN_DMA_MASK;
  714. zynqmp_gqspi_write(xqspi, GQSPI_CONFIG_OFST, config_reg);
  715. /* Switch to DMA mode */
  716. xqspi->mode = GQSPI_MODE_DMA;
  717. /* Write the number of bytes to transfer */
  718. zynqmp_gqspi_write(xqspi, GQSPI_QSPIDMA_DST_SIZE_OFST, rx_bytes);
  719. return 0;
  720. }
  721. /**
  722. * zynqmp_qspi_write_op - This function sets up the GENFIFO entries,
  723. * TX FIFO, and fills the TX FIFO with as many
  724. * bytes as possible.
  725. * @xqspi: Pointer to the GQSPI instance.
  726. * @tx_nbits: Transfer buswidth.
  727. * @genfifoentry: Variable in which GENFIFO mask is returned
  728. * to calling function
  729. */
  730. static void zynqmp_qspi_write_op(struct zynqmp_qspi *xqspi, u8 tx_nbits,
  731. u32 genfifoentry)
  732. {
  733. u32 config_reg;
  734. zynqmp_qspi_fillgenfifo(xqspi, tx_nbits, genfifoentry);
  735. zynqmp_qspi_filltxfifo(xqspi, GQSPI_TXD_DEPTH);
  736. if (xqspi->mode == GQSPI_MODE_DMA) {
  737. config_reg = zynqmp_gqspi_read(xqspi,
  738. GQSPI_CONFIG_OFST);
  739. config_reg &= ~GQSPI_CFG_MODE_EN_MASK;
  740. zynqmp_gqspi_write(xqspi, GQSPI_CONFIG_OFST,
  741. config_reg);
  742. xqspi->mode = GQSPI_MODE_IO;
  743. }
  744. }
  745. /**
  746. * zynqmp_qspi_read_op - This function sets up the GENFIFO entries and
  747. * RX DMA operation.
  748. * @xqspi: xqspi is a pointer to the GQSPI instance.
  749. * @rx_nbits: Receive buswidth.
  750. * @genfifoentry: genfifoentry is pointer to the variable in which
  751. * GENFIFO mask is returned to calling function
  752. */
  753. static int zynqmp_qspi_read_op(struct zynqmp_qspi *xqspi, u8 rx_nbits,
  754. u32 genfifoentry)
  755. {
  756. int ret;
  757. ret = zynqmp_qspi_setuprxdma(xqspi);
  758. if (ret)
  759. return ret;
  760. zynqmp_qspi_fillgenfifo(xqspi, rx_nbits, genfifoentry);
  761. return 0;
  762. }
  763. /**
  764. * zynqmp_qspi_suspend - Suspend method for the QSPI driver
  765. * @dev: Address of the platform_device structure
  766. *
  767. * This function stops the QSPI driver queue and disables the QSPI controller
  768. *
  769. * Return: Always 0
  770. */
  771. static int __maybe_unused zynqmp_qspi_suspend(struct device *dev)
  772. {
  773. struct zynqmp_qspi *xqspi = dev_get_drvdata(dev);
  774. struct spi_controller *ctlr = xqspi->ctlr;
  775. int ret;
  776. ret = spi_controller_suspend(ctlr);
  777. if (ret)
  778. return ret;
  779. zynqmp_gqspi_write(xqspi, GQSPI_EN_OFST, 0x0);
  780. return 0;
  781. }
  782. /**
  783. * zynqmp_qspi_resume - Resume method for the QSPI driver
  784. * @dev: Address of the platform_device structure
  785. *
  786. * The function starts the QSPI driver queue and initializes the QSPI
  787. * controller
  788. *
  789. * Return: 0 on success; error value otherwise
  790. */
  791. static int __maybe_unused zynqmp_qspi_resume(struct device *dev)
  792. {
  793. struct zynqmp_qspi *xqspi = dev_get_drvdata(dev);
  794. struct spi_controller *ctlr = xqspi->ctlr;
  795. zynqmp_gqspi_write(xqspi, GQSPI_EN_OFST, GQSPI_EN_MASK);
  796. spi_controller_resume(ctlr);
  797. return 0;
  798. }
  799. /**
  800. * zynqmp_runtime_suspend - Runtime suspend method for the SPI driver
  801. * @dev: Address of the platform_device structure
  802. *
  803. * This function disables the clocks
  804. *
  805. * Return: Always 0
  806. */
  807. static int __maybe_unused zynqmp_runtime_suspend(struct device *dev)
  808. {
  809. struct zynqmp_qspi *xqspi = dev_get_drvdata(dev);
  810. clk_disable_unprepare(xqspi->refclk);
  811. clk_disable_unprepare(xqspi->pclk);
  812. return 0;
  813. }
  814. /**
  815. * zynqmp_runtime_resume - Runtime resume method for the SPI driver
  816. * @dev: Address of the platform_device structure
  817. *
  818. * This function enables the clocks
  819. *
  820. * Return: 0 on success and error value on error
  821. */
  822. static int __maybe_unused zynqmp_runtime_resume(struct device *dev)
  823. {
  824. struct zynqmp_qspi *xqspi = dev_get_drvdata(dev);
  825. int ret;
  826. ret = clk_prepare_enable(xqspi->pclk);
  827. if (ret) {
  828. dev_err(dev, "Cannot enable APB clock.\n");
  829. return ret;
  830. }
  831. ret = clk_prepare_enable(xqspi->refclk);
  832. if (ret) {
  833. dev_err(dev, "Cannot enable device clock.\n");
  834. clk_disable_unprepare(xqspi->pclk);
  835. return ret;
  836. }
  837. return 0;
  838. }
  839. /**
  840. * zynqmp_qspi_exec_op() - Initiates the QSPI transfer
  841. * @mem: The SPI memory
  842. * @op: The memory operation to execute
  843. *
  844. * Executes a memory operation.
  845. *
  846. * This function first selects the chip and starts the memory operation.
  847. *
  848. * Return: 0 in case of success, a negative error code otherwise.
  849. */
  850. static int zynqmp_qspi_exec_op(struct spi_mem *mem,
  851. const struct spi_mem_op *op)
  852. {
  853. struct zynqmp_qspi *xqspi = spi_controller_get_devdata
  854. (mem->spi->master);
  855. int err = 0, i;
  856. u32 genfifoentry = 0;
  857. u16 opcode = op->cmd.opcode;
  858. u64 opaddr;
  859. dev_dbg(xqspi->dev, "cmd:%#x mode:%d.%d.%d.%d\n",
  860. op->cmd.opcode, op->cmd.buswidth, op->addr.buswidth,
  861. op->dummy.buswidth, op->data.buswidth);
  862. mutex_lock(&xqspi->op_lock);
  863. zynqmp_qspi_config_op(xqspi, mem->spi);
  864. zynqmp_qspi_chipselect(mem->spi, false);
  865. genfifoentry |= xqspi->genfifocs;
  866. genfifoentry |= xqspi->genfifobus;
  867. if (op->cmd.opcode) {
  868. reinit_completion(&xqspi->data_completion);
  869. xqspi->txbuf = &opcode;
  870. xqspi->rxbuf = NULL;
  871. xqspi->bytes_to_transfer = op->cmd.nbytes;
  872. xqspi->bytes_to_receive = 0;
  873. zynqmp_qspi_write_op(xqspi, op->cmd.buswidth, genfifoentry);
  874. zynqmp_gqspi_write(xqspi, GQSPI_CONFIG_OFST,
  875. zynqmp_gqspi_read(xqspi, GQSPI_CONFIG_OFST) |
  876. GQSPI_CFG_START_GEN_FIFO_MASK);
  877. zynqmp_gqspi_write(xqspi, GQSPI_IER_OFST,
  878. GQSPI_IER_GENFIFOEMPTY_MASK |
  879. GQSPI_IER_TXNOT_FULL_MASK);
  880. if (!wait_for_completion_timeout
  881. (&xqspi->data_completion, msecs_to_jiffies(1000))) {
  882. err = -ETIMEDOUT;
  883. goto return_err;
  884. }
  885. }
  886. if (op->addr.nbytes) {
  887. xqspi->txbuf = &opaddr;
  888. for (i = 0; i < op->addr.nbytes; i++) {
  889. *(((u8 *)xqspi->txbuf) + i) = op->addr.val >>
  890. (8 * (op->addr.nbytes - i - 1));
  891. }
  892. reinit_completion(&xqspi->data_completion);
  893. xqspi->rxbuf = NULL;
  894. xqspi->bytes_to_transfer = op->addr.nbytes;
  895. xqspi->bytes_to_receive = 0;
  896. zynqmp_qspi_write_op(xqspi, op->addr.buswidth, genfifoentry);
  897. zynqmp_gqspi_write(xqspi, GQSPI_CONFIG_OFST,
  898. zynqmp_gqspi_read(xqspi,
  899. GQSPI_CONFIG_OFST) |
  900. GQSPI_CFG_START_GEN_FIFO_MASK);
  901. zynqmp_gqspi_write(xqspi, GQSPI_IER_OFST,
  902. GQSPI_IER_TXEMPTY_MASK |
  903. GQSPI_IER_GENFIFOEMPTY_MASK |
  904. GQSPI_IER_TXNOT_FULL_MASK);
  905. if (!wait_for_completion_timeout
  906. (&xqspi->data_completion, msecs_to_jiffies(1000))) {
  907. err = -ETIMEDOUT;
  908. goto return_err;
  909. }
  910. }
  911. if (op->dummy.nbytes) {
  912. xqspi->txbuf = NULL;
  913. xqspi->rxbuf = NULL;
  914. /*
  915. * xqspi->bytes_to_transfer here represents the dummy circles
  916. * which need to be sent.
  917. */
  918. xqspi->bytes_to_transfer = op->dummy.nbytes * 8 / op->dummy.buswidth;
  919. xqspi->bytes_to_receive = 0;
  920. /*
  921. * Using op->data.buswidth instead of op->dummy.buswidth here because
  922. * we need to use it to configure the correct SPI mode.
  923. */
  924. zynqmp_qspi_write_op(xqspi, op->data.buswidth,
  925. genfifoentry);
  926. zynqmp_gqspi_write(xqspi, GQSPI_CONFIG_OFST,
  927. zynqmp_gqspi_read(xqspi, GQSPI_CONFIG_OFST) |
  928. GQSPI_CFG_START_GEN_FIFO_MASK);
  929. }
  930. if (op->data.nbytes) {
  931. reinit_completion(&xqspi->data_completion);
  932. if (op->data.dir == SPI_MEM_DATA_OUT) {
  933. xqspi->txbuf = (u8 *)op->data.buf.out;
  934. xqspi->rxbuf = NULL;
  935. xqspi->bytes_to_transfer = op->data.nbytes;
  936. xqspi->bytes_to_receive = 0;
  937. zynqmp_qspi_write_op(xqspi, op->data.buswidth,
  938. genfifoentry);
  939. zynqmp_gqspi_write(xqspi, GQSPI_CONFIG_OFST,
  940. zynqmp_gqspi_read
  941. (xqspi, GQSPI_CONFIG_OFST) |
  942. GQSPI_CFG_START_GEN_FIFO_MASK);
  943. zynqmp_gqspi_write(xqspi, GQSPI_IER_OFST,
  944. GQSPI_IER_TXEMPTY_MASK |
  945. GQSPI_IER_GENFIFOEMPTY_MASK |
  946. GQSPI_IER_TXNOT_FULL_MASK);
  947. } else {
  948. xqspi->txbuf = NULL;
  949. xqspi->rxbuf = (u8 *)op->data.buf.in;
  950. xqspi->bytes_to_receive = op->data.nbytes;
  951. xqspi->bytes_to_transfer = 0;
  952. err = zynqmp_qspi_read_op(xqspi, op->data.buswidth,
  953. genfifoentry);
  954. if (err)
  955. goto return_err;
  956. zynqmp_gqspi_write(xqspi, GQSPI_CONFIG_OFST,
  957. zynqmp_gqspi_read
  958. (xqspi, GQSPI_CONFIG_OFST) |
  959. GQSPI_CFG_START_GEN_FIFO_MASK);
  960. if (xqspi->mode == GQSPI_MODE_DMA) {
  961. zynqmp_gqspi_write
  962. (xqspi, GQSPI_QSPIDMA_DST_I_EN_OFST,
  963. GQSPI_QSPIDMA_DST_I_EN_DONE_MASK);
  964. } else {
  965. zynqmp_gqspi_write(xqspi, GQSPI_IER_OFST,
  966. GQSPI_IER_GENFIFOEMPTY_MASK |
  967. GQSPI_IER_RXNEMPTY_MASK |
  968. GQSPI_IER_RXEMPTY_MASK);
  969. }
  970. }
  971. if (!wait_for_completion_timeout
  972. (&xqspi->data_completion, msecs_to_jiffies(1000)))
  973. err = -ETIMEDOUT;
  974. }
  975. return_err:
  976. zynqmp_qspi_chipselect(mem->spi, true);
  977. mutex_unlock(&xqspi->op_lock);
  978. return err;
  979. }
  980. static const struct dev_pm_ops zynqmp_qspi_dev_pm_ops = {
  981. SET_RUNTIME_PM_OPS(zynqmp_runtime_suspend,
  982. zynqmp_runtime_resume, NULL)
  983. SET_SYSTEM_SLEEP_PM_OPS(zynqmp_qspi_suspend, zynqmp_qspi_resume)
  984. };
  985. static const struct spi_controller_mem_ops zynqmp_qspi_mem_ops = {
  986. .exec_op = zynqmp_qspi_exec_op,
  987. };
  988. /**
  989. * zynqmp_qspi_probe - Probe method for the QSPI driver
  990. * @pdev: Pointer to the platform_device structure
  991. *
  992. * This function initializes the driver data structures and the hardware.
  993. *
  994. * Return: 0 on success; error value otherwise
  995. */
  996. static int zynqmp_qspi_probe(struct platform_device *pdev)
  997. {
  998. int ret = 0;
  999. struct spi_controller *ctlr;
  1000. struct zynqmp_qspi *xqspi;
  1001. struct device *dev = &pdev->dev;
  1002. struct device_node *np = dev->of_node;
  1003. u32 num_cs;
  1004. ctlr = spi_alloc_master(&pdev->dev, sizeof(*xqspi));
  1005. if (!ctlr)
  1006. return -ENOMEM;
  1007. xqspi = spi_controller_get_devdata(ctlr);
  1008. xqspi->dev = dev;
  1009. xqspi->ctlr = ctlr;
  1010. platform_set_drvdata(pdev, xqspi);
  1011. xqspi->regs = devm_platform_ioremap_resource(pdev, 0);
  1012. if (IS_ERR(xqspi->regs)) {
  1013. ret = PTR_ERR(xqspi->regs);
  1014. goto remove_master;
  1015. }
  1016. xqspi->pclk = devm_clk_get(&pdev->dev, "pclk");
  1017. if (IS_ERR(xqspi->pclk)) {
  1018. dev_err(dev, "pclk clock not found.\n");
  1019. ret = PTR_ERR(xqspi->pclk);
  1020. goto remove_master;
  1021. }
  1022. xqspi->refclk = devm_clk_get(&pdev->dev, "ref_clk");
  1023. if (IS_ERR(xqspi->refclk)) {
  1024. dev_err(dev, "ref_clk clock not found.\n");
  1025. ret = PTR_ERR(xqspi->refclk);
  1026. goto remove_master;
  1027. }
  1028. ret = clk_prepare_enable(xqspi->pclk);
  1029. if (ret) {
  1030. dev_err(dev, "Unable to enable APB clock.\n");
  1031. goto remove_master;
  1032. }
  1033. ret = clk_prepare_enable(xqspi->refclk);
  1034. if (ret) {
  1035. dev_err(dev, "Unable to enable device clock.\n");
  1036. goto clk_dis_pclk;
  1037. }
  1038. init_completion(&xqspi->data_completion);
  1039. mutex_init(&xqspi->op_lock);
  1040. pm_runtime_use_autosuspend(&pdev->dev);
  1041. pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
  1042. pm_runtime_set_active(&pdev->dev);
  1043. pm_runtime_enable(&pdev->dev);
  1044. ret = pm_runtime_get_sync(&pdev->dev);
  1045. if (ret < 0) {
  1046. dev_err(&pdev->dev, "Failed to pm_runtime_get_sync: %d\n", ret);
  1047. goto clk_dis_all;
  1048. }
  1049. /* QSPI controller initializations */
  1050. zynqmp_qspi_init_hw(xqspi);
  1051. xqspi->irq = platform_get_irq(pdev, 0);
  1052. if (xqspi->irq <= 0) {
  1053. ret = -ENXIO;
  1054. goto clk_dis_all;
  1055. }
  1056. ret = devm_request_irq(&pdev->dev, xqspi->irq, zynqmp_qspi_irq,
  1057. 0, pdev->name, xqspi);
  1058. if (ret != 0) {
  1059. ret = -ENXIO;
  1060. dev_err(dev, "request_irq failed\n");
  1061. goto clk_dis_all;
  1062. }
  1063. ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
  1064. if (ret)
  1065. goto clk_dis_all;
  1066. ret = of_property_read_u32(np, "num-cs", &num_cs);
  1067. if (ret < 0) {
  1068. ctlr->num_chipselect = GQSPI_DEFAULT_NUM_CS;
  1069. } else if (num_cs > GQSPI_MAX_NUM_CS) {
  1070. ret = -EINVAL;
  1071. dev_err(&pdev->dev, "only %d chip selects are available\n",
  1072. GQSPI_MAX_NUM_CS);
  1073. goto clk_dis_all;
  1074. } else {
  1075. ctlr->num_chipselect = num_cs;
  1076. }
  1077. ctlr->bits_per_word_mask = SPI_BPW_MASK(8);
  1078. ctlr->mem_ops = &zynqmp_qspi_mem_ops;
  1079. ctlr->setup = zynqmp_qspi_setup_op;
  1080. ctlr->max_speed_hz = clk_get_rate(xqspi->refclk) / 2;
  1081. ctlr->bits_per_word_mask = SPI_BPW_MASK(8);
  1082. ctlr->mode_bits = SPI_CPOL | SPI_CPHA | SPI_RX_DUAL | SPI_RX_QUAD |
  1083. SPI_TX_DUAL | SPI_TX_QUAD;
  1084. ctlr->dev.of_node = np;
  1085. ctlr->auto_runtime_pm = true;
  1086. ret = devm_spi_register_controller(&pdev->dev, ctlr);
  1087. if (ret) {
  1088. dev_err(&pdev->dev, "spi_register_controller failed\n");
  1089. goto clk_dis_all;
  1090. }
  1091. pm_runtime_mark_last_busy(&pdev->dev);
  1092. pm_runtime_put_autosuspend(&pdev->dev);
  1093. return 0;
  1094. clk_dis_all:
  1095. pm_runtime_disable(&pdev->dev);
  1096. pm_runtime_put_noidle(&pdev->dev);
  1097. pm_runtime_set_suspended(&pdev->dev);
  1098. clk_disable_unprepare(xqspi->refclk);
  1099. clk_dis_pclk:
  1100. clk_disable_unprepare(xqspi->pclk);
  1101. remove_master:
  1102. spi_controller_put(ctlr);
  1103. return ret;
  1104. }
  1105. /**
  1106. * zynqmp_qspi_remove - Remove method for the QSPI driver
  1107. * @pdev: Pointer to the platform_device structure
  1108. *
  1109. * This function is called if a device is physically removed from the system or
  1110. * if the driver module is being unloaded. It frees all resources allocated to
  1111. * the device.
  1112. *
  1113. * Return: 0 Always
  1114. */
  1115. static int zynqmp_qspi_remove(struct platform_device *pdev)
  1116. {
  1117. struct zynqmp_qspi *xqspi = platform_get_drvdata(pdev);
  1118. pm_runtime_get_sync(&pdev->dev);
  1119. zynqmp_gqspi_write(xqspi, GQSPI_EN_OFST, 0x0);
  1120. pm_runtime_disable(&pdev->dev);
  1121. pm_runtime_put_noidle(&pdev->dev);
  1122. pm_runtime_set_suspended(&pdev->dev);
  1123. clk_disable_unprepare(xqspi->refclk);
  1124. clk_disable_unprepare(xqspi->pclk);
  1125. return 0;
  1126. }
  1127. static const struct of_device_id zynqmp_qspi_of_match[] = {
  1128. { .compatible = "xlnx,zynqmp-qspi-1.0", },
  1129. { /* End of table */ }
  1130. };
  1131. MODULE_DEVICE_TABLE(of, zynqmp_qspi_of_match);
  1132. static struct platform_driver zynqmp_qspi_driver = {
  1133. .probe = zynqmp_qspi_probe,
  1134. .remove = zynqmp_qspi_remove,
  1135. .driver = {
  1136. .name = "zynqmp-qspi",
  1137. .of_match_table = zynqmp_qspi_of_match,
  1138. .pm = &zynqmp_qspi_dev_pm_ops,
  1139. },
  1140. };
  1141. module_platform_driver(zynqmp_qspi_driver);
  1142. MODULE_AUTHOR("Xilinx, Inc.");
  1143. MODULE_DESCRIPTION("Xilinx Zynqmp QSPI driver");
  1144. MODULE_LICENSE("GPL");