spi-zynqmp-gqspi.c 33 KB

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