spi-cadence.c 21 KB

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