spi-cadence.c 23 KB

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