spi-zynqmp-gqspi.c 35 KB

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