spi-cadence.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. /*
  2. * Cadence SPI controller driver (master mode only)
  3. *
  4. * Copyright (C) 2008 - 2014 Xilinx, Inc.
  5. *
  6. * based on Blackfin On-Chip SPI Driver (spi_bfin5xx.c)
  7. *
  8. * This program is free software; you can redistribute it and/or modify it under
  9. * the terms of the GNU General Public License version 2 as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/io.h>
  17. #include <linux/module.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/of_address.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/spi/spi.h>
  22. /* Name of this driver */
  23. #define CDNS_SPI_NAME "cdns-spi"
  24. /* Register offset definitions */
  25. #define CDNS_SPI_CR_OFFSET 0x00 /* Configuration Register, RW */
  26. #define CDNS_SPI_ISR_OFFSET 0x04 /* Interrupt Status Register, RO */
  27. #define CDNS_SPI_IER_OFFSET 0x08 /* Interrupt Enable Register, WO */
  28. #define CDNS_SPI_IDR_OFFSET 0x0c /* Interrupt Disable Register, WO */
  29. #define CDNS_SPI_IMR_OFFSET 0x10 /* Interrupt Enabled Mask Register, RO */
  30. #define CDNS_SPI_ER_OFFSET 0x14 /* Enable/Disable Register, RW */
  31. #define CDNS_SPI_DR_OFFSET 0x18 /* Delay Register, RW */
  32. #define CDNS_SPI_TXD_OFFSET 0x1C /* Data Transmit Register, WO */
  33. #define CDNS_SPI_RXD_OFFSET 0x20 /* Data Receive Register, RO */
  34. #define CDNS_SPI_SICR_OFFSET 0x24 /* Slave Idle Count Register, RW */
  35. #define CDNS_SPI_THLD_OFFSET 0x28 /* Transmit FIFO Watermark Register,RW */
  36. /*
  37. * SPI Configuration Register bit Masks
  38. *
  39. * This register contains various control bits that affect the operation
  40. * of the SPI controller
  41. */
  42. #define CDNS_SPI_CR_MANSTRT_MASK 0x00010000 /* Manual TX Start */
  43. #define CDNS_SPI_CR_CPHA_MASK 0x00000004 /* Clock Phase Control */
  44. #define CDNS_SPI_CR_CPOL_MASK 0x00000002 /* Clock Polarity Control */
  45. #define CDNS_SPI_CR_SSCTRL_MASK 0x00003C00 /* Slave Select Mask */
  46. #define CDNS_SPI_CR_BAUD_DIV_MASK 0x00000038 /* Baud Rate Divisor Mask */
  47. #define CDNS_SPI_CR_MSTREN_MASK 0x00000001 /* Master Enable Mask */
  48. #define CDNS_SPI_CR_MANSTRTEN_MASK 0x00008000 /* Manual TX Enable Mask */
  49. #define CDNS_SPI_CR_SSFORCE_MASK 0x00004000 /* Manual SS Enable Mask */
  50. #define CDNS_SPI_CR_BAUD_DIV_4_MASK 0x00000008 /* Default Baud Div Mask */
  51. #define CDNS_SPI_CR_DEFAULT_MASK (CDNS_SPI_CR_MSTREN_MASK | \
  52. CDNS_SPI_CR_SSCTRL_MASK | \
  53. CDNS_SPI_CR_SSFORCE_MASK | \
  54. CDNS_SPI_CR_BAUD_DIV_4_MASK)
  55. /*
  56. * SPI Configuration Register - Baud rate and slave select
  57. *
  58. * These are the values used in the calculation of baud rate divisor and
  59. * setting the slave select.
  60. */
  61. #define CDNS_SPI_BAUD_DIV_MAX 7 /* Baud rate divisor maximum */
  62. #define CDNS_SPI_BAUD_DIV_MIN 1 /* Baud rate divisor minimum */
  63. #define CDNS_SPI_BAUD_DIV_SHIFT 3 /* Baud rate divisor shift in CR */
  64. #define CDNS_SPI_SS_SHIFT 10 /* Slave Select field shift in CR */
  65. #define CDNS_SPI_SS0 0x1 /* Slave Select zero */
  66. /*
  67. * SPI Interrupt Registers bit Masks
  68. *
  69. * All the four interrupt registers (Status/Mask/Enable/Disable) have the same
  70. * bit definitions.
  71. */
  72. #define CDNS_SPI_IXR_TXOW_MASK 0x00000004 /* SPI TX FIFO Overwater */
  73. #define CDNS_SPI_IXR_MODF_MASK 0x00000002 /* SPI Mode Fault */
  74. #define CDNS_SPI_IXR_RXNEMTY_MASK 0x00000010 /* SPI RX FIFO Not Empty */
  75. #define CDNS_SPI_IXR_DEFAULT_MASK (CDNS_SPI_IXR_TXOW_MASK | \
  76. CDNS_SPI_IXR_MODF_MASK)
  77. #define CDNS_SPI_IXR_TXFULL_MASK 0x00000008 /* SPI TX Full */
  78. #define CDNS_SPI_IXR_ALL_MASK 0x0000007F /* SPI all interrupts */
  79. /*
  80. * SPI Enable Register bit Masks
  81. *
  82. * This register is used to enable or disable the SPI controller
  83. */
  84. #define CDNS_SPI_ER_ENABLE_MASK 0x00000001 /* SPI Enable Bit Mask */
  85. #define CDNS_SPI_ER_DISABLE_MASK 0x0 /* SPI Disable Bit Mask */
  86. /* SPI FIFO depth in bytes */
  87. #define CDNS_SPI_FIFO_DEPTH 128
  88. /* Default number of chip select lines */
  89. #define CDNS_SPI_DEFAULT_NUM_CS 4
  90. /**
  91. * struct cdns_spi - This definition defines spi driver instance
  92. * @regs: Virtual address of the SPI controller registers
  93. * @ref_clk: Pointer to the peripheral clock
  94. * @pclk: Pointer to the APB clock
  95. * @speed_hz: Current SPI bus clock speed in Hz
  96. * @txbuf: Pointer to the TX buffer
  97. * @rxbuf: Pointer to the RX buffer
  98. * @tx_bytes: Number of bytes left to transfer
  99. * @rx_bytes: Number of bytes requested
  100. * @dev_busy: Device busy flag
  101. * @is_decoded_cs: Flag for decoder property set or not
  102. */
  103. struct cdns_spi {
  104. void __iomem *regs;
  105. struct clk *ref_clk;
  106. struct clk *pclk;
  107. u32 speed_hz;
  108. const u8 *txbuf;
  109. u8 *rxbuf;
  110. int tx_bytes;
  111. int rx_bytes;
  112. u8 dev_busy;
  113. u32 is_decoded_cs;
  114. };
  115. /* Macros for the SPI controller read/write */
  116. static inline u32 cdns_spi_read(struct cdns_spi *xspi, u32 offset)
  117. {
  118. return readl_relaxed(xspi->regs + offset);
  119. }
  120. static inline void cdns_spi_write(struct cdns_spi *xspi, u32 offset, u32 val)
  121. {
  122. writel_relaxed(val, xspi->regs + offset);
  123. }
  124. /**
  125. * cdns_spi_init_hw - Initialize the hardware and configure the SPI controller
  126. * @xspi: Pointer to the cdns_spi structure
  127. *
  128. * On reset the SPI controller is configured to be in master mode, baud rate
  129. * divisor is set to 4, threshold value for TX FIFO not full interrupt is set
  130. * to 1 and size of the word to be transferred as 8 bit.
  131. * This function initializes the SPI controller to disable and clear all the
  132. * interrupts, enable manual slave select and manual start, deselect all the
  133. * chip select lines, and enable the SPI controller.
  134. */
  135. static void cdns_spi_init_hw(struct cdns_spi *xspi)
  136. {
  137. cdns_spi_write(xspi, CDNS_SPI_ER_OFFSET,
  138. CDNS_SPI_ER_DISABLE_MASK);
  139. cdns_spi_write(xspi, CDNS_SPI_IDR_OFFSET,
  140. CDNS_SPI_IXR_ALL_MASK);
  141. /* Clear the RX FIFO */
  142. while (cdns_spi_read(xspi, CDNS_SPI_ISR_OFFSET) &
  143. CDNS_SPI_IXR_RXNEMTY_MASK)
  144. cdns_spi_read(xspi, CDNS_SPI_RXD_OFFSET);
  145. cdns_spi_write(xspi, CDNS_SPI_ISR_OFFSET,
  146. CDNS_SPI_IXR_ALL_MASK);
  147. cdns_spi_write(xspi, CDNS_SPI_CR_OFFSET,
  148. CDNS_SPI_CR_DEFAULT_MASK);
  149. cdns_spi_write(xspi, CDNS_SPI_ER_OFFSET,
  150. CDNS_SPI_ER_ENABLE_MASK);
  151. }
  152. /**
  153. * cdns_spi_chipselect - Select or deselect the chip select line
  154. * @spi: Pointer to the spi_device structure
  155. * @is_on: Select(0) or deselect (1) the chip select line
  156. */
  157. static void cdns_spi_chipselect(struct spi_device *spi, bool is_high)
  158. {
  159. struct cdns_spi *xspi = spi_master_get_devdata(spi->master);
  160. u32 ctrl_reg;
  161. ctrl_reg = cdns_spi_read(xspi, CDNS_SPI_CR_OFFSET);
  162. if (is_high) {
  163. /* Deselect the slave */
  164. ctrl_reg |= CDNS_SPI_CR_SSCTRL_MASK;
  165. } else {
  166. /* Select the slave */
  167. ctrl_reg &= ~CDNS_SPI_CR_SSCTRL_MASK;
  168. if (!(xspi->is_decoded_cs))
  169. ctrl_reg |= ((~(CDNS_SPI_SS0 << spi->chip_select)) <<
  170. CDNS_SPI_SS_SHIFT) &
  171. CDNS_SPI_CR_SSCTRL_MASK;
  172. else
  173. ctrl_reg |= (spi->chip_select << CDNS_SPI_SS_SHIFT) &
  174. CDNS_SPI_CR_SSCTRL_MASK;
  175. }
  176. cdns_spi_write(xspi, CDNS_SPI_CR_OFFSET, ctrl_reg);
  177. }
  178. /**
  179. * cdns_spi_config_clock_mode - Sets clock polarity and phase
  180. * @spi: Pointer to the spi_device structure
  181. *
  182. * Sets the requested clock polarity and phase.
  183. */
  184. static void cdns_spi_config_clock_mode(struct spi_device *spi)
  185. {
  186. struct cdns_spi *xspi = spi_master_get_devdata(spi->master);
  187. u32 ctrl_reg, new_ctrl_reg;
  188. new_ctrl_reg = ctrl_reg = cdns_spi_read(xspi, CDNS_SPI_CR_OFFSET);
  189. /* Set the SPI clock phase and clock polarity */
  190. new_ctrl_reg &= ~(CDNS_SPI_CR_CPHA_MASK | CDNS_SPI_CR_CPOL_MASK);
  191. if (spi->mode & SPI_CPHA)
  192. new_ctrl_reg |= CDNS_SPI_CR_CPHA_MASK;
  193. if (spi->mode & SPI_CPOL)
  194. new_ctrl_reg |= CDNS_SPI_CR_CPOL_MASK;
  195. if (new_ctrl_reg != ctrl_reg) {
  196. /*
  197. * Just writing the CR register does not seem to apply the clock
  198. * setting changes. This is problematic when changing the clock
  199. * polarity as it will cause the SPI slave to see spurious clock
  200. * transitions. To workaround the issue toggle the ER register.
  201. */
  202. cdns_spi_write(xspi, CDNS_SPI_ER_OFFSET,
  203. CDNS_SPI_ER_DISABLE_MASK);
  204. cdns_spi_write(xspi, CDNS_SPI_CR_OFFSET, new_ctrl_reg);
  205. cdns_spi_write(xspi, CDNS_SPI_ER_OFFSET,
  206. CDNS_SPI_ER_ENABLE_MASK);
  207. }
  208. }
  209. /**
  210. * cdns_spi_config_clock_freq - Sets clock frequency
  211. * @spi: Pointer to the spi_device structure
  212. * @transfer: Pointer to the spi_transfer structure which provides
  213. * information about next transfer setup parameters
  214. *
  215. * Sets the requested clock frequency.
  216. * Note: If the requested frequency is not an exact match with what can be
  217. * obtained using the prescalar value the driver sets the clock frequency which
  218. * is lower than the requested frequency (maximum lower) for the transfer. If
  219. * the requested frequency is higher or lower than that is supported by the SPI
  220. * controller the driver will set the highest or lowest frequency supported by
  221. * controller.
  222. */
  223. static void cdns_spi_config_clock_freq(struct spi_device *spi,
  224. struct spi_transfer *transfer)
  225. {
  226. struct cdns_spi *xspi = spi_master_get_devdata(spi->master);
  227. u32 ctrl_reg, baud_rate_val;
  228. unsigned long frequency;
  229. frequency = clk_get_rate(xspi->ref_clk);
  230. ctrl_reg = cdns_spi_read(xspi, CDNS_SPI_CR_OFFSET);
  231. /* Set the clock frequency */
  232. if (xspi->speed_hz != transfer->speed_hz) {
  233. /* first valid value is 1 */
  234. baud_rate_val = CDNS_SPI_BAUD_DIV_MIN;
  235. while ((baud_rate_val < CDNS_SPI_BAUD_DIV_MAX) &&
  236. (frequency / (2 << baud_rate_val)) > transfer->speed_hz)
  237. baud_rate_val++;
  238. ctrl_reg &= ~CDNS_SPI_CR_BAUD_DIV_MASK;
  239. ctrl_reg |= baud_rate_val << CDNS_SPI_BAUD_DIV_SHIFT;
  240. xspi->speed_hz = frequency / (2 << baud_rate_val);
  241. }
  242. cdns_spi_write(xspi, CDNS_SPI_CR_OFFSET, ctrl_reg);
  243. }
  244. /**
  245. * cdns_spi_setup_transfer - Configure SPI controller for specified transfer
  246. * @spi: Pointer to the spi_device structure
  247. * @transfer: Pointer to the spi_transfer structure which provides
  248. * information about next transfer setup parameters
  249. *
  250. * Sets the operational mode of SPI controller for the next SPI transfer and
  251. * sets the requested clock frequency.
  252. *
  253. * Return: Always 0
  254. */
  255. static int cdns_spi_setup_transfer(struct spi_device *spi,
  256. struct spi_transfer *transfer)
  257. {
  258. struct cdns_spi *xspi = spi_master_get_devdata(spi->master);
  259. cdns_spi_config_clock_freq(spi, transfer);
  260. dev_dbg(&spi->dev, "%s, mode %d, %u bits/w, %u clock speed\n",
  261. __func__, spi->mode, spi->bits_per_word,
  262. xspi->speed_hz);
  263. return 0;
  264. }
  265. /**
  266. * cdns_spi_fill_tx_fifo - Fills the TX FIFO with as many bytes as possible
  267. * @xspi: Pointer to the cdns_spi structure
  268. */
  269. static void cdns_spi_fill_tx_fifo(struct cdns_spi *xspi)
  270. {
  271. unsigned long trans_cnt = 0;
  272. while ((trans_cnt < CDNS_SPI_FIFO_DEPTH) &&
  273. (xspi->tx_bytes > 0)) {
  274. if (xspi->txbuf)
  275. cdns_spi_write(xspi, CDNS_SPI_TXD_OFFSET,
  276. *xspi->txbuf++);
  277. else
  278. cdns_spi_write(xspi, CDNS_SPI_TXD_OFFSET, 0);
  279. xspi->tx_bytes--;
  280. trans_cnt++;
  281. }
  282. }
  283. /**
  284. * cdns_spi_irq - Interrupt service routine of the SPI controller
  285. * @irq: IRQ number
  286. * @dev_id: Pointer to the xspi structure
  287. *
  288. * This function handles TX empty and Mode Fault interrupts only.
  289. * On TX empty interrupt this function reads the received data from RX FIFO and
  290. * fills the TX FIFO if there is any data remaining to be transferred.
  291. * On Mode Fault interrupt this function indicates that transfer is completed,
  292. * the SPI subsystem will identify the error as the remaining bytes to be
  293. * transferred is non-zero.
  294. *
  295. * Return: IRQ_HANDLED when handled; IRQ_NONE otherwise.
  296. */
  297. static irqreturn_t cdns_spi_irq(int irq, void *dev_id)
  298. {
  299. struct spi_master *master = dev_id;
  300. struct cdns_spi *xspi = spi_master_get_devdata(master);
  301. u32 intr_status, status;
  302. status = IRQ_NONE;
  303. intr_status = cdns_spi_read(xspi, CDNS_SPI_ISR_OFFSET);
  304. cdns_spi_write(xspi, CDNS_SPI_ISR_OFFSET, intr_status);
  305. if (intr_status & CDNS_SPI_IXR_MODF_MASK) {
  306. /* Indicate that transfer is completed, the SPI subsystem will
  307. * identify the error as the remaining bytes to be
  308. * transferred is non-zero
  309. */
  310. cdns_spi_write(xspi, CDNS_SPI_IDR_OFFSET,
  311. CDNS_SPI_IXR_DEFAULT_MASK);
  312. spi_finalize_current_transfer(master);
  313. status = IRQ_HANDLED;
  314. } else if (intr_status & CDNS_SPI_IXR_TXOW_MASK) {
  315. unsigned long trans_cnt;
  316. trans_cnt = xspi->rx_bytes - xspi->tx_bytes;
  317. /* Read out the data from the RX FIFO */
  318. while (trans_cnt) {
  319. u8 data;
  320. data = cdns_spi_read(xspi, CDNS_SPI_RXD_OFFSET);
  321. if (xspi->rxbuf)
  322. *xspi->rxbuf++ = data;
  323. xspi->rx_bytes--;
  324. trans_cnt--;
  325. }
  326. if (xspi->tx_bytes) {
  327. /* There is more data to send */
  328. cdns_spi_fill_tx_fifo(xspi);
  329. } else {
  330. /* Transfer is completed */
  331. cdns_spi_write(xspi, CDNS_SPI_IDR_OFFSET,
  332. CDNS_SPI_IXR_DEFAULT_MASK);
  333. spi_finalize_current_transfer(master);
  334. }
  335. status = IRQ_HANDLED;
  336. }
  337. return status;
  338. }
  339. static int cdns_prepare_message(struct spi_master *master,
  340. struct spi_message *msg)
  341. {
  342. cdns_spi_config_clock_mode(msg->spi);
  343. return 0;
  344. }
  345. /**
  346. * cdns_transfer_one - Initiates the SPI transfer
  347. * @master: Pointer to spi_master structure
  348. * @spi: Pointer to the spi_device structure
  349. * @transfer: Pointer to the spi_transfer structure which provides
  350. * information about next transfer parameters
  351. *
  352. * This function fills the TX FIFO, starts the SPI transfer and
  353. * returns a positive transfer count so that core will wait for completion.
  354. *
  355. * Return: Number of bytes transferred in the last transfer
  356. */
  357. static int cdns_transfer_one(struct spi_master *master,
  358. struct spi_device *spi,
  359. struct spi_transfer *transfer)
  360. {
  361. struct cdns_spi *xspi = spi_master_get_devdata(master);
  362. xspi->txbuf = transfer->tx_buf;
  363. xspi->rxbuf = transfer->rx_buf;
  364. xspi->tx_bytes = transfer->len;
  365. xspi->rx_bytes = transfer->len;
  366. cdns_spi_setup_transfer(spi, transfer);
  367. cdns_spi_fill_tx_fifo(xspi);
  368. cdns_spi_write(xspi, CDNS_SPI_IER_OFFSET,
  369. CDNS_SPI_IXR_DEFAULT_MASK);
  370. return transfer->len;
  371. }
  372. /**
  373. * cdns_prepare_transfer_hardware - Prepares hardware for transfer.
  374. * @master: Pointer to the spi_master structure which provides
  375. * information about the controller.
  376. *
  377. * This function enables SPI master controller.
  378. *
  379. * Return: 0 always
  380. */
  381. static int cdns_prepare_transfer_hardware(struct spi_master *master)
  382. {
  383. struct cdns_spi *xspi = spi_master_get_devdata(master);
  384. cdns_spi_write(xspi, CDNS_SPI_ER_OFFSET,
  385. CDNS_SPI_ER_ENABLE_MASK);
  386. return 0;
  387. }
  388. /**
  389. * cdns_unprepare_transfer_hardware - Relaxes hardware after transfer
  390. * @master: Pointer to the spi_master structure which provides
  391. * information about the controller.
  392. *
  393. * This function disables the SPI master controller.
  394. *
  395. * Return: 0 always
  396. */
  397. static int cdns_unprepare_transfer_hardware(struct spi_master *master)
  398. {
  399. struct cdns_spi *xspi = spi_master_get_devdata(master);
  400. cdns_spi_write(xspi, CDNS_SPI_ER_OFFSET,
  401. CDNS_SPI_ER_DISABLE_MASK);
  402. return 0;
  403. }
  404. /**
  405. * cdns_spi_probe - Probe method for the SPI driver
  406. * @pdev: Pointer to the platform_device structure
  407. *
  408. * This function initializes the driver data structures and the hardware.
  409. *
  410. * Return: 0 on success and error value on error
  411. */
  412. static int cdns_spi_probe(struct platform_device *pdev)
  413. {
  414. int ret = 0, irq;
  415. struct spi_master *master;
  416. struct cdns_spi *xspi;
  417. struct resource *res;
  418. u32 num_cs;
  419. master = spi_alloc_master(&pdev->dev, sizeof(*xspi));
  420. if (master == NULL)
  421. return -ENOMEM;
  422. xspi = spi_master_get_devdata(master);
  423. master->dev.of_node = pdev->dev.of_node;
  424. platform_set_drvdata(pdev, master);
  425. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  426. xspi->regs = devm_ioremap_resource(&pdev->dev, res);
  427. if (IS_ERR(xspi->regs)) {
  428. ret = PTR_ERR(xspi->regs);
  429. goto remove_master;
  430. }
  431. xspi->pclk = devm_clk_get(&pdev->dev, "pclk");
  432. if (IS_ERR(xspi->pclk)) {
  433. dev_err(&pdev->dev, "pclk clock not found.\n");
  434. ret = PTR_ERR(xspi->pclk);
  435. goto remove_master;
  436. }
  437. xspi->ref_clk = devm_clk_get(&pdev->dev, "ref_clk");
  438. if (IS_ERR(xspi->ref_clk)) {
  439. dev_err(&pdev->dev, "ref_clk clock not found.\n");
  440. ret = PTR_ERR(xspi->ref_clk);
  441. goto remove_master;
  442. }
  443. ret = clk_prepare_enable(xspi->pclk);
  444. if (ret) {
  445. dev_err(&pdev->dev, "Unable to enable APB clock.\n");
  446. goto remove_master;
  447. }
  448. ret = clk_prepare_enable(xspi->ref_clk);
  449. if (ret) {
  450. dev_err(&pdev->dev, "Unable to enable device clock.\n");
  451. goto clk_dis_apb;
  452. }
  453. /* SPI controller initializations */
  454. cdns_spi_init_hw(xspi);
  455. irq = platform_get_irq(pdev, 0);
  456. if (irq <= 0) {
  457. ret = -ENXIO;
  458. dev_err(&pdev->dev, "irq number is invalid\n");
  459. goto remove_master;
  460. }
  461. ret = devm_request_irq(&pdev->dev, irq, cdns_spi_irq,
  462. 0, pdev->name, master);
  463. if (ret != 0) {
  464. ret = -ENXIO;
  465. dev_err(&pdev->dev, "request_irq failed\n");
  466. goto remove_master;
  467. }
  468. ret = of_property_read_u32(pdev->dev.of_node, "num-cs", &num_cs);
  469. if (ret < 0)
  470. master->num_chipselect = CDNS_SPI_DEFAULT_NUM_CS;
  471. else
  472. master->num_chipselect = num_cs;
  473. ret = of_property_read_u32(pdev->dev.of_node, "is-decoded-cs",
  474. &xspi->is_decoded_cs);
  475. if (ret < 0)
  476. xspi->is_decoded_cs = 0;
  477. master->prepare_transfer_hardware = cdns_prepare_transfer_hardware;
  478. master->prepare_message = cdns_prepare_message;
  479. master->transfer_one = cdns_transfer_one;
  480. master->unprepare_transfer_hardware = cdns_unprepare_transfer_hardware;
  481. master->set_cs = cdns_spi_chipselect;
  482. master->mode_bits = SPI_CPOL | SPI_CPHA;
  483. /* Set to default valid value */
  484. master->max_speed_hz = clk_get_rate(xspi->ref_clk) / 4;
  485. xspi->speed_hz = master->max_speed_hz;
  486. master->bits_per_word_mask = SPI_BPW_MASK(8);
  487. ret = spi_register_master(master);
  488. if (ret) {
  489. dev_err(&pdev->dev, "spi_register_master failed\n");
  490. goto clk_dis_all;
  491. }
  492. return ret;
  493. clk_dis_all:
  494. clk_disable_unprepare(xspi->ref_clk);
  495. clk_dis_apb:
  496. clk_disable_unprepare(xspi->pclk);
  497. remove_master:
  498. spi_master_put(master);
  499. return ret;
  500. }
  501. /**
  502. * cdns_spi_remove - Remove method for the SPI driver
  503. * @pdev: Pointer to the platform_device structure
  504. *
  505. * This function is called if a device is physically removed from the system or
  506. * if the driver module is being unloaded. It frees all resources allocated to
  507. * the device.
  508. *
  509. * Return: 0 on success and error value on error
  510. */
  511. static int cdns_spi_remove(struct platform_device *pdev)
  512. {
  513. struct spi_master *master = platform_get_drvdata(pdev);
  514. struct cdns_spi *xspi = spi_master_get_devdata(master);
  515. cdns_spi_write(xspi, CDNS_SPI_ER_OFFSET,
  516. CDNS_SPI_ER_DISABLE_MASK);
  517. clk_disable_unprepare(xspi->ref_clk);
  518. clk_disable_unprepare(xspi->pclk);
  519. spi_unregister_master(master);
  520. return 0;
  521. }
  522. /**
  523. * cdns_spi_suspend - Suspend method for the SPI driver
  524. * @dev: Address of the platform_device structure
  525. *
  526. * This function disables the SPI controller and
  527. * changes the driver state to "suspend"
  528. *
  529. * Return: Always 0
  530. */
  531. static int __maybe_unused cdns_spi_suspend(struct device *dev)
  532. {
  533. struct platform_device *pdev = container_of(dev,
  534. struct platform_device, dev);
  535. struct spi_master *master = platform_get_drvdata(pdev);
  536. struct cdns_spi *xspi = spi_master_get_devdata(master);
  537. spi_master_suspend(master);
  538. clk_disable_unprepare(xspi->ref_clk);
  539. clk_disable_unprepare(xspi->pclk);
  540. return 0;
  541. }
  542. /**
  543. * cdns_spi_resume - Resume method for the SPI driver
  544. * @dev: Address of the platform_device structure
  545. *
  546. * This function changes the driver state to "ready"
  547. *
  548. * Return: 0 on success and error value on error
  549. */
  550. static int __maybe_unused cdns_spi_resume(struct device *dev)
  551. {
  552. struct platform_device *pdev = container_of(dev,
  553. struct platform_device, dev);
  554. struct spi_master *master = platform_get_drvdata(pdev);
  555. struct cdns_spi *xspi = spi_master_get_devdata(master);
  556. int ret = 0;
  557. ret = clk_prepare_enable(xspi->pclk);
  558. if (ret) {
  559. dev_err(dev, "Cannot enable APB clock.\n");
  560. return ret;
  561. }
  562. ret = clk_prepare_enable(xspi->ref_clk);
  563. if (ret) {
  564. dev_err(dev, "Cannot enable device clock.\n");
  565. clk_disable(xspi->pclk);
  566. return ret;
  567. }
  568. spi_master_resume(master);
  569. return 0;
  570. }
  571. static SIMPLE_DEV_PM_OPS(cdns_spi_dev_pm_ops, cdns_spi_suspend,
  572. cdns_spi_resume);
  573. static const struct of_device_id cdns_spi_of_match[] = {
  574. { .compatible = "xlnx,zynq-spi-r1p6" },
  575. { .compatible = "cdns,spi-r1p6" },
  576. { /* end of table */ }
  577. };
  578. MODULE_DEVICE_TABLE(of, cdns_spi_of_match);
  579. /* cdns_spi_driver - This structure defines the SPI subsystem platform driver */
  580. static struct platform_driver cdns_spi_driver = {
  581. .probe = cdns_spi_probe,
  582. .remove = cdns_spi_remove,
  583. .driver = {
  584. .name = CDNS_SPI_NAME,
  585. .of_match_table = cdns_spi_of_match,
  586. .pm = &cdns_spi_dev_pm_ops,
  587. },
  588. };
  589. module_platform_driver(cdns_spi_driver);
  590. MODULE_AUTHOR("Xilinx, Inc.");
  591. MODULE_DESCRIPTION("Cadence SPI driver");
  592. MODULE_LICENSE("GPL");