spi-cadence.c 23 KB

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