spi-cadence.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  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. /* When xspi in busy condition, bytes may send failed,
  278. * then spi control did't work thoroughly, add one byte delay
  279. */
  280. if (cdns_spi_read(xspi, CDNS_SPI_ISR) &
  281. CDNS_SPI_IXR_TXFULL)
  282. udelay(10);
  283. if (xspi->txbuf)
  284. cdns_spi_write(xspi, CDNS_SPI_TXD, *xspi->txbuf++);
  285. else
  286. cdns_spi_write(xspi, CDNS_SPI_TXD, 0);
  287. xspi->tx_bytes--;
  288. trans_cnt++;
  289. }
  290. }
  291. /**
  292. * cdns_spi_irq - Interrupt service routine of the SPI controller
  293. * @irq: IRQ number
  294. * @dev_id: Pointer to the xspi structure
  295. *
  296. * This function handles TX empty and Mode Fault interrupts only.
  297. * On TX empty interrupt this function reads the received data from RX FIFO and
  298. * fills the TX FIFO if there is any data remaining to be transferred.
  299. * On Mode Fault interrupt this function indicates that transfer is completed,
  300. * the SPI subsystem will identify the error as the remaining bytes to be
  301. * transferred is non-zero.
  302. *
  303. * Return: IRQ_HANDLED when handled; IRQ_NONE otherwise.
  304. */
  305. static irqreturn_t cdns_spi_irq(int irq, void *dev_id)
  306. {
  307. struct spi_master *master = dev_id;
  308. struct cdns_spi *xspi = spi_master_get_devdata(master);
  309. u32 intr_status, status;
  310. status = IRQ_NONE;
  311. intr_status = cdns_spi_read(xspi, CDNS_SPI_ISR);
  312. cdns_spi_write(xspi, CDNS_SPI_ISR, intr_status);
  313. if (intr_status & CDNS_SPI_IXR_MODF) {
  314. /* Indicate that transfer is completed, the SPI subsystem will
  315. * identify the error as the remaining bytes to be
  316. * transferred is non-zero
  317. */
  318. cdns_spi_write(xspi, CDNS_SPI_IDR, CDNS_SPI_IXR_DEFAULT);
  319. spi_finalize_current_transfer(master);
  320. status = IRQ_HANDLED;
  321. } else if (intr_status & CDNS_SPI_IXR_TXOW) {
  322. unsigned long trans_cnt;
  323. trans_cnt = xspi->rx_bytes - xspi->tx_bytes;
  324. /* Read out the data from the RX FIFO */
  325. while (trans_cnt) {
  326. u8 data;
  327. data = cdns_spi_read(xspi, CDNS_SPI_RXD);
  328. if (xspi->rxbuf)
  329. *xspi->rxbuf++ = data;
  330. xspi->rx_bytes--;
  331. trans_cnt--;
  332. }
  333. if (xspi->tx_bytes) {
  334. /* There is more data to send */
  335. cdns_spi_fill_tx_fifo(xspi);
  336. } else {
  337. /* Transfer is completed */
  338. cdns_spi_write(xspi, CDNS_SPI_IDR,
  339. CDNS_SPI_IXR_DEFAULT);
  340. spi_finalize_current_transfer(master);
  341. }
  342. status = IRQ_HANDLED;
  343. }
  344. return status;
  345. }
  346. static int cdns_prepare_message(struct spi_master *master,
  347. struct spi_message *msg)
  348. {
  349. cdns_spi_config_clock_mode(msg->spi);
  350. return 0;
  351. }
  352. /**
  353. * cdns_transfer_one - Initiates the SPI transfer
  354. * @master: Pointer to spi_master structure
  355. * @spi: Pointer to the spi_device structure
  356. * @transfer: Pointer to the spi_transfer structure which provides
  357. * information about next transfer parameters
  358. *
  359. * This function fills the TX FIFO, starts the SPI transfer and
  360. * returns a positive transfer count so that core will wait for completion.
  361. *
  362. * Return: Number of bytes transferred in the last transfer
  363. */
  364. static int cdns_transfer_one(struct spi_master *master,
  365. struct spi_device *spi,
  366. struct spi_transfer *transfer)
  367. {
  368. struct cdns_spi *xspi = spi_master_get_devdata(master);
  369. xspi->txbuf = transfer->tx_buf;
  370. xspi->rxbuf = transfer->rx_buf;
  371. xspi->tx_bytes = transfer->len;
  372. xspi->rx_bytes = transfer->len;
  373. cdns_spi_setup_transfer(spi, transfer);
  374. cdns_spi_fill_tx_fifo(xspi);
  375. cdns_spi_write(xspi, CDNS_SPI_IER, CDNS_SPI_IXR_DEFAULT);
  376. return transfer->len;
  377. }
  378. /**
  379. * cdns_prepare_transfer_hardware - Prepares hardware for transfer.
  380. * @master: Pointer to the spi_master structure which provides
  381. * information about the controller.
  382. *
  383. * This function enables SPI master controller.
  384. *
  385. * Return: 0 always
  386. */
  387. static int cdns_prepare_transfer_hardware(struct spi_master *master)
  388. {
  389. struct cdns_spi *xspi = spi_master_get_devdata(master);
  390. cdns_spi_write(xspi, CDNS_SPI_ER, CDNS_SPI_ER_ENABLE);
  391. return 0;
  392. }
  393. /**
  394. * cdns_unprepare_transfer_hardware - Relaxes hardware after transfer
  395. * @master: Pointer to the spi_master structure which provides
  396. * information about the controller.
  397. *
  398. * This function disables the SPI master controller.
  399. *
  400. * Return: 0 always
  401. */
  402. static int cdns_unprepare_transfer_hardware(struct spi_master *master)
  403. {
  404. struct cdns_spi *xspi = spi_master_get_devdata(master);
  405. cdns_spi_write(xspi, CDNS_SPI_ER, CDNS_SPI_ER_DISABLE);
  406. return 0;
  407. }
  408. static int cdns_spi_setup(struct spi_device *spi)
  409. {
  410. int ret = -EINVAL;
  411. struct cdns_spi_device_data *cdns_spi_data = spi_get_ctldata(spi);
  412. /* this is a pin managed by the controller, leave it alone */
  413. if (spi->cs_gpio == -ENOENT)
  414. return 0;
  415. /* this seems to be the first time we're here */
  416. if (!cdns_spi_data) {
  417. cdns_spi_data = kzalloc(sizeof(*cdns_spi_data), GFP_KERNEL);
  418. if (!cdns_spi_data)
  419. return -ENOMEM;
  420. cdns_spi_data->gpio_requested = false;
  421. spi_set_ctldata(spi, cdns_spi_data);
  422. }
  423. /* if we haven't done so, grab the gpio */
  424. if (!cdns_spi_data->gpio_requested && gpio_is_valid(spi->cs_gpio)) {
  425. ret = gpio_request_one(spi->cs_gpio,
  426. (spi->mode & SPI_CS_HIGH) ?
  427. GPIOF_OUT_INIT_LOW : GPIOF_OUT_INIT_HIGH,
  428. dev_name(&spi->dev));
  429. if (ret)
  430. dev_err(&spi->dev, "can't request chipselect gpio %d\n",
  431. spi->cs_gpio);
  432. else
  433. cdns_spi_data->gpio_requested = true;
  434. } else {
  435. if (gpio_is_valid(spi->cs_gpio)) {
  436. int mode = ((spi->mode & SPI_CS_HIGH) ?
  437. GPIOF_OUT_INIT_LOW : GPIOF_OUT_INIT_HIGH);
  438. ret = gpio_direction_output(spi->cs_gpio, mode);
  439. if (ret)
  440. dev_err(&spi->dev, "chipselect gpio %d setup failed (%d)\n",
  441. spi->cs_gpio, ret);
  442. }
  443. }
  444. return ret;
  445. }
  446. static void cdns_spi_cleanup(struct spi_device *spi)
  447. {
  448. struct cdns_spi_device_data *cdns_spi_data = spi_get_ctldata(spi);
  449. if (cdns_spi_data) {
  450. if (cdns_spi_data->gpio_requested)
  451. gpio_free(spi->cs_gpio);
  452. kfree(cdns_spi_data);
  453. spi_set_ctldata(spi, NULL);
  454. }
  455. }
  456. /**
  457. * cdns_spi_probe - Probe method for the SPI driver
  458. * @pdev: Pointer to the platform_device structure
  459. *
  460. * This function initializes the driver data structures and the hardware.
  461. *
  462. * Return: 0 on success and error value on error
  463. */
  464. static int cdns_spi_probe(struct platform_device *pdev)
  465. {
  466. int ret = 0, irq;
  467. struct spi_master *master;
  468. struct cdns_spi *xspi;
  469. struct resource *res;
  470. u32 num_cs;
  471. master = spi_alloc_master(&pdev->dev, sizeof(*xspi));
  472. if (!master)
  473. return -ENOMEM;
  474. xspi = spi_master_get_devdata(master);
  475. master->dev.of_node = pdev->dev.of_node;
  476. platform_set_drvdata(pdev, master);
  477. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  478. xspi->regs = devm_ioremap_resource(&pdev->dev, res);
  479. if (IS_ERR(xspi->regs)) {
  480. ret = PTR_ERR(xspi->regs);
  481. goto remove_master;
  482. }
  483. xspi->pclk = devm_clk_get(&pdev->dev, "pclk");
  484. if (IS_ERR(xspi->pclk)) {
  485. dev_err(&pdev->dev, "pclk clock not found.\n");
  486. ret = PTR_ERR(xspi->pclk);
  487. goto remove_master;
  488. }
  489. xspi->ref_clk = devm_clk_get(&pdev->dev, "ref_clk");
  490. if (IS_ERR(xspi->ref_clk)) {
  491. dev_err(&pdev->dev, "ref_clk clock not found.\n");
  492. ret = PTR_ERR(xspi->ref_clk);
  493. goto remove_master;
  494. }
  495. ret = clk_prepare_enable(xspi->pclk);
  496. if (ret) {
  497. dev_err(&pdev->dev, "Unable to enable APB clock.\n");
  498. goto remove_master;
  499. }
  500. ret = clk_prepare_enable(xspi->ref_clk);
  501. if (ret) {
  502. dev_err(&pdev->dev, "Unable to enable device clock.\n");
  503. goto clk_dis_apb;
  504. }
  505. ret = of_property_read_u32(pdev->dev.of_node, "num-cs", &num_cs);
  506. if (ret < 0)
  507. master->num_chipselect = CDNS_SPI_DEFAULT_NUM_CS;
  508. else
  509. master->num_chipselect = num_cs;
  510. ret = of_property_read_u32(pdev->dev.of_node, "is-decoded-cs",
  511. &xspi->is_decoded_cs);
  512. if (ret < 0)
  513. xspi->is_decoded_cs = 0;
  514. /* SPI controller initializations */
  515. cdns_spi_init_hw(xspi);
  516. pm_runtime_set_active(&pdev->dev);
  517. pm_runtime_enable(&pdev->dev);
  518. pm_runtime_use_autosuspend(&pdev->dev);
  519. pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
  520. irq = platform_get_irq(pdev, 0);
  521. if (irq <= 0) {
  522. ret = -ENXIO;
  523. dev_err(&pdev->dev, "irq number is invalid\n");
  524. goto clk_dis_all;
  525. }
  526. ret = devm_request_irq(&pdev->dev, irq, cdns_spi_irq,
  527. 0, pdev->name, master);
  528. if (ret != 0) {
  529. ret = -ENXIO;
  530. dev_err(&pdev->dev, "request_irq failed\n");
  531. goto clk_dis_all;
  532. }
  533. master->prepare_transfer_hardware = cdns_prepare_transfer_hardware;
  534. master->prepare_message = cdns_prepare_message;
  535. master->transfer_one = cdns_transfer_one;
  536. master->unprepare_transfer_hardware = cdns_unprepare_transfer_hardware;
  537. master->set_cs = cdns_spi_chipselect;
  538. master->setup = cdns_spi_setup;
  539. master->cleanup = cdns_spi_cleanup;
  540. master->auto_runtime_pm = true;
  541. master->mode_bits = SPI_CPOL | SPI_CPHA;
  542. /* Set to default valid value */
  543. master->max_speed_hz = clk_get_rate(xspi->ref_clk) / 4;
  544. xspi->speed_hz = master->max_speed_hz;
  545. master->bits_per_word_mask = SPI_BPW_MASK(8);
  546. ret = spi_register_master(master);
  547. if (ret) {
  548. dev_err(&pdev->dev, "spi_register_master failed\n");
  549. goto clk_dis_all;
  550. }
  551. return ret;
  552. clk_dis_all:
  553. pm_runtime_set_suspended(&pdev->dev);
  554. pm_runtime_disable(&pdev->dev);
  555. clk_disable_unprepare(xspi->ref_clk);
  556. clk_dis_apb:
  557. clk_disable_unprepare(xspi->pclk);
  558. remove_master:
  559. spi_master_put(master);
  560. return ret;
  561. }
  562. /**
  563. * cdns_spi_remove - Remove method for the SPI driver
  564. * @pdev: Pointer to the platform_device structure
  565. *
  566. * This function is called if a device is physically removed from the system or
  567. * if the driver module is being unloaded. It frees all resources allocated to
  568. * the device.
  569. *
  570. * Return: 0 on success and error value on error
  571. */
  572. static int cdns_spi_remove(struct platform_device *pdev)
  573. {
  574. struct spi_master *master = platform_get_drvdata(pdev);
  575. struct cdns_spi *xspi = spi_master_get_devdata(master);
  576. cdns_spi_write(xspi, CDNS_SPI_ER, CDNS_SPI_ER_DISABLE);
  577. clk_disable_unprepare(xspi->ref_clk);
  578. clk_disable_unprepare(xspi->pclk);
  579. pm_runtime_set_suspended(&pdev->dev);
  580. pm_runtime_disable(&pdev->dev);
  581. spi_unregister_master(master);
  582. return 0;
  583. }
  584. /**
  585. * cdns_spi_suspend - Suspend method for the SPI driver
  586. * @dev: Address of the platform_device structure
  587. *
  588. * This function disables the SPI controller and
  589. * changes the driver state to "suspend"
  590. *
  591. * Return: 0 on success and error value on error
  592. */
  593. static int __maybe_unused cdns_spi_suspend(struct device *dev)
  594. {
  595. struct spi_master *master = dev_get_drvdata(dev);
  596. return spi_master_suspend(master);
  597. }
  598. /**
  599. * cdns_spi_resume - Resume method for the SPI driver
  600. * @dev: Address of the platform_device structure
  601. *
  602. * This function changes the driver state to "ready"
  603. *
  604. * Return: 0 on success and error value on error
  605. */
  606. static int __maybe_unused cdns_spi_resume(struct device *dev)
  607. {
  608. struct spi_master *master = dev_get_drvdata(dev);
  609. struct cdns_spi *xspi = spi_master_get_devdata(master);
  610. cdns_spi_init_hw(xspi);
  611. return spi_master_resume(master);
  612. }
  613. /**
  614. * cdns_spi_runtime_resume - Runtime resume method for the SPI driver
  615. * @dev: Address of the platform_device structure
  616. *
  617. * This function enables the clocks
  618. *
  619. * Return: 0 on success and error value on error
  620. */
  621. static int __maybe_unused cnds_runtime_resume(struct device *dev)
  622. {
  623. struct spi_master *master = dev_get_drvdata(dev);
  624. struct cdns_spi *xspi = spi_master_get_devdata(master);
  625. int ret;
  626. ret = clk_prepare_enable(xspi->pclk);
  627. if (ret) {
  628. dev_err(dev, "Cannot enable APB clock.\n");
  629. return ret;
  630. }
  631. ret = clk_prepare_enable(xspi->ref_clk);
  632. if (ret) {
  633. dev_err(dev, "Cannot enable device clock.\n");
  634. clk_disable_unprepare(xspi->pclk);
  635. return ret;
  636. }
  637. return 0;
  638. }
  639. /**
  640. * cdns_spi_runtime_suspend - Runtime suspend method for the SPI driver
  641. * @dev: Address of the platform_device structure
  642. *
  643. * This function disables the clocks
  644. *
  645. * Return: Always 0
  646. */
  647. static int __maybe_unused cnds_runtime_suspend(struct device *dev)
  648. {
  649. struct spi_master *master = dev_get_drvdata(dev);
  650. struct cdns_spi *xspi = spi_master_get_devdata(master);
  651. clk_disable_unprepare(xspi->ref_clk);
  652. clk_disable_unprepare(xspi->pclk);
  653. return 0;
  654. }
  655. static const struct dev_pm_ops cdns_spi_dev_pm_ops = {
  656. SET_RUNTIME_PM_OPS(cnds_runtime_suspend,
  657. cnds_runtime_resume, NULL)
  658. SET_SYSTEM_SLEEP_PM_OPS(cdns_spi_suspend, cdns_spi_resume)
  659. };
  660. static const struct of_device_id cdns_spi_of_match[] = {
  661. { .compatible = "xlnx,zynq-spi-r1p6" },
  662. { .compatible = "cdns,spi-r1p6" },
  663. { /* end of table */ }
  664. };
  665. MODULE_DEVICE_TABLE(of, cdns_spi_of_match);
  666. /* cdns_spi_driver - This structure defines the SPI subsystem platform driver */
  667. static struct platform_driver cdns_spi_driver = {
  668. .probe = cdns_spi_probe,
  669. .remove = cdns_spi_remove,
  670. .driver = {
  671. .name = CDNS_SPI_NAME,
  672. .of_match_table = cdns_spi_of_match,
  673. .pm = &cdns_spi_dev_pm_ops,
  674. },
  675. };
  676. module_platform_driver(cdns_spi_driver);
  677. MODULE_AUTHOR("Xilinx, Inc.");
  678. MODULE_DESCRIPTION("Cadence SPI driver");
  679. MODULE_LICENSE("GPL");