spi-ep93xx.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for Cirrus Logic EP93xx SPI controller.
  4. *
  5. * Copyright (C) 2010-2011 Mika Westerberg
  6. *
  7. * Explicit FIFO handling code was inspired by amba-pl022 driver.
  8. *
  9. * Chip select support using other than built-in GPIOs by H. Hartley Sweeten.
  10. *
  11. * For more information about the SPI controller see documentation on Cirrus
  12. * Logic web site:
  13. * https://www.cirrus.com/en/pubs/manual/EP93xx_Users_Guide_UM1.pdf
  14. */
  15. #include <linux/io.h>
  16. #include <linux/clk.h>
  17. #include <linux/err.h>
  18. #include <linux/delay.h>
  19. #include <linux/device.h>
  20. #include <linux/dma-direction.h>
  21. #include <linux/dma-mapping.h>
  22. #include <linux/dmaengine.h>
  23. #include <linux/bitops.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/module.h>
  26. #include <linux/property.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/sched.h>
  29. #include <linux/scatterlist.h>
  30. #include <linux/spi/spi.h>
  31. #define SSPCR0 0x0000
  32. #define SSPCR0_SPO BIT(6)
  33. #define SSPCR0_SPH BIT(7)
  34. #define SSPCR0_SCR_SHIFT 8
  35. #define SSPCR1 0x0004
  36. #define SSPCR1_RIE BIT(0)
  37. #define SSPCR1_TIE BIT(1)
  38. #define SSPCR1_RORIE BIT(2)
  39. #define SSPCR1_LBM BIT(3)
  40. #define SSPCR1_SSE BIT(4)
  41. #define SSPCR1_MS BIT(5)
  42. #define SSPCR1_SOD BIT(6)
  43. #define SSPDR 0x0008
  44. #define SSPSR 0x000c
  45. #define SSPSR_TFE BIT(0)
  46. #define SSPSR_TNF BIT(1)
  47. #define SSPSR_RNE BIT(2)
  48. #define SSPSR_RFF BIT(3)
  49. #define SSPSR_BSY BIT(4)
  50. #define SSPCPSR 0x0010
  51. #define SSPIIR 0x0014
  52. #define SSPIIR_RIS BIT(0)
  53. #define SSPIIR_TIS BIT(1)
  54. #define SSPIIR_RORIS BIT(2)
  55. #define SSPICR SSPIIR
  56. /* timeout in milliseconds */
  57. #define SPI_TIMEOUT 5
  58. /* maximum depth of RX/TX FIFO */
  59. #define SPI_FIFO_SIZE 8
  60. /**
  61. * struct ep93xx_spi - EP93xx SPI controller structure
  62. * @clk: clock for the controller
  63. * @mmio: pointer to ioremap()'d registers
  64. * @sspdr_phys: physical address of the SSPDR register
  65. * @tx: current byte in transfer to transmit
  66. * @rx: current byte in transfer to receive
  67. * @fifo_level: how full is FIFO (%0..%SPI_FIFO_SIZE - %1). Receiving one
  68. * frame decreases this level and sending one frame increases it.
  69. * @dma_rx: RX DMA channel
  70. * @dma_tx: TX DMA channel
  71. * @rx_sgt: sg table for RX transfers
  72. * @tx_sgt: sg table for TX transfers
  73. * @zeropage: dummy page used as RX buffer when only TX buffer is passed in by
  74. * the client
  75. */
  76. struct ep93xx_spi {
  77. struct clk *clk;
  78. void __iomem *mmio;
  79. unsigned long sspdr_phys;
  80. size_t tx;
  81. size_t rx;
  82. size_t fifo_level;
  83. struct dma_chan *dma_rx;
  84. struct dma_chan *dma_tx;
  85. struct sg_table rx_sgt;
  86. struct sg_table tx_sgt;
  87. void *zeropage;
  88. };
  89. /* converts bits per word to CR0.DSS value */
  90. #define bits_per_word_to_dss(bpw) ((bpw) - 1)
  91. /**
  92. * ep93xx_spi_calc_divisors() - calculates SPI clock divisors
  93. * @host: SPI host
  94. * @rate: desired SPI output clock rate
  95. * @div_cpsr: pointer to return the cpsr (pre-scaler) divider
  96. * @div_scr: pointer to return the scr divider
  97. */
  98. static int ep93xx_spi_calc_divisors(struct spi_controller *host,
  99. u32 rate, u8 *div_cpsr, u8 *div_scr)
  100. {
  101. struct ep93xx_spi *espi = spi_controller_get_devdata(host);
  102. unsigned long spi_clk_rate = clk_get_rate(espi->clk);
  103. int cpsr, scr;
  104. /*
  105. * Make sure that max value is between values supported by the
  106. * controller.
  107. */
  108. rate = clamp(rate, host->min_speed_hz, host->max_speed_hz);
  109. /*
  110. * Calculate divisors so that we can get speed according the
  111. * following formula:
  112. * rate = spi_clock_rate / (cpsr * (1 + scr))
  113. *
  114. * cpsr must be even number and starts from 2, scr can be any number
  115. * between 0 and 255.
  116. */
  117. for (cpsr = 2; cpsr <= 254; cpsr += 2) {
  118. for (scr = 0; scr <= 255; scr++) {
  119. if ((spi_clk_rate / (cpsr * (scr + 1))) <= rate) {
  120. *div_scr = (u8)scr;
  121. *div_cpsr = (u8)cpsr;
  122. return 0;
  123. }
  124. }
  125. }
  126. return -EINVAL;
  127. }
  128. static int ep93xx_spi_chip_setup(struct spi_controller *host,
  129. struct spi_device *spi,
  130. struct spi_transfer *xfer)
  131. {
  132. struct ep93xx_spi *espi = spi_controller_get_devdata(host);
  133. u8 dss = bits_per_word_to_dss(xfer->bits_per_word);
  134. u8 div_cpsr = 0;
  135. u8 div_scr = 0;
  136. u16 cr0;
  137. int err;
  138. err = ep93xx_spi_calc_divisors(host, xfer->speed_hz,
  139. &div_cpsr, &div_scr);
  140. if (err)
  141. return err;
  142. cr0 = div_scr << SSPCR0_SCR_SHIFT;
  143. if (spi->mode & SPI_CPOL)
  144. cr0 |= SSPCR0_SPO;
  145. if (spi->mode & SPI_CPHA)
  146. cr0 |= SSPCR0_SPH;
  147. cr0 |= dss;
  148. dev_dbg(&host->dev, "setup: mode %d, cpsr %d, scr %d, dss %d\n",
  149. spi->mode, div_cpsr, div_scr, dss);
  150. dev_dbg(&host->dev, "setup: cr0 %#x\n", cr0);
  151. writel(div_cpsr, espi->mmio + SSPCPSR);
  152. writel(cr0, espi->mmio + SSPCR0);
  153. return 0;
  154. }
  155. static void ep93xx_do_write(struct spi_controller *host)
  156. {
  157. struct ep93xx_spi *espi = spi_controller_get_devdata(host);
  158. struct spi_transfer *xfer = host->cur_msg->state;
  159. u32 val = 0;
  160. if (xfer->bits_per_word > 8) {
  161. if (xfer->tx_buf)
  162. val = ((u16 *)xfer->tx_buf)[espi->tx];
  163. espi->tx += 2;
  164. } else {
  165. if (xfer->tx_buf)
  166. val = ((u8 *)xfer->tx_buf)[espi->tx];
  167. espi->tx += 1;
  168. }
  169. writel(val, espi->mmio + SSPDR);
  170. }
  171. static void ep93xx_do_read(struct spi_controller *host)
  172. {
  173. struct ep93xx_spi *espi = spi_controller_get_devdata(host);
  174. struct spi_transfer *xfer = host->cur_msg->state;
  175. u32 val;
  176. val = readl(espi->mmio + SSPDR);
  177. if (xfer->bits_per_word > 8) {
  178. if (xfer->rx_buf)
  179. ((u16 *)xfer->rx_buf)[espi->rx] = val;
  180. espi->rx += 2;
  181. } else {
  182. if (xfer->rx_buf)
  183. ((u8 *)xfer->rx_buf)[espi->rx] = val;
  184. espi->rx += 1;
  185. }
  186. }
  187. /**
  188. * ep93xx_spi_read_write() - perform next RX/TX transfer
  189. * @host: SPI host
  190. *
  191. * This function transfers next bytes (or half-words) to/from RX/TX FIFOs. If
  192. * called several times, the whole transfer will be completed. Returns
  193. * %-EINPROGRESS when current transfer was not yet completed otherwise %0.
  194. *
  195. * When this function is finished, RX FIFO should be empty and TX FIFO should be
  196. * full.
  197. */
  198. static int ep93xx_spi_read_write(struct spi_controller *host)
  199. {
  200. struct ep93xx_spi *espi = spi_controller_get_devdata(host);
  201. struct spi_transfer *xfer = host->cur_msg->state;
  202. /* read as long as RX FIFO has frames in it */
  203. while ((readl(espi->mmio + SSPSR) & SSPSR_RNE)) {
  204. ep93xx_do_read(host);
  205. espi->fifo_level--;
  206. }
  207. /* write as long as TX FIFO has room */
  208. while (espi->fifo_level < SPI_FIFO_SIZE && espi->tx < xfer->len) {
  209. ep93xx_do_write(host);
  210. espi->fifo_level++;
  211. }
  212. if (espi->rx == xfer->len)
  213. return 0;
  214. return -EINPROGRESS;
  215. }
  216. static enum dma_transfer_direction
  217. ep93xx_dma_data_to_trans_dir(enum dma_data_direction dir)
  218. {
  219. switch (dir) {
  220. case DMA_TO_DEVICE:
  221. return DMA_MEM_TO_DEV;
  222. case DMA_FROM_DEVICE:
  223. return DMA_DEV_TO_MEM;
  224. default:
  225. return DMA_TRANS_NONE;
  226. }
  227. }
  228. /**
  229. * ep93xx_spi_dma_prepare() - prepares a DMA transfer
  230. * @host: SPI host
  231. * @dir: DMA transfer direction
  232. *
  233. * Function configures the DMA, maps the buffer and prepares the DMA
  234. * descriptor. Returns a valid DMA descriptor in case of success and ERR_PTR
  235. * in case of failure.
  236. */
  237. static struct dma_async_tx_descriptor *
  238. ep93xx_spi_dma_prepare(struct spi_controller *host,
  239. enum dma_data_direction dir)
  240. {
  241. struct ep93xx_spi *espi = spi_controller_get_devdata(host);
  242. struct spi_transfer *xfer = host->cur_msg->state;
  243. struct dma_async_tx_descriptor *txd;
  244. enum dma_slave_buswidth buswidth;
  245. struct dma_slave_config conf;
  246. struct scatterlist *sg;
  247. struct sg_table *sgt;
  248. struct dma_chan *chan;
  249. const void *buf, *pbuf;
  250. size_t len = xfer->len;
  251. int i, ret, nents;
  252. if (xfer->bits_per_word > 8)
  253. buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
  254. else
  255. buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
  256. memset(&conf, 0, sizeof(conf));
  257. conf.direction = ep93xx_dma_data_to_trans_dir(dir);
  258. if (dir == DMA_FROM_DEVICE) {
  259. chan = espi->dma_rx;
  260. buf = xfer->rx_buf;
  261. sgt = &espi->rx_sgt;
  262. conf.src_addr = espi->sspdr_phys;
  263. conf.src_addr_width = buswidth;
  264. } else {
  265. chan = espi->dma_tx;
  266. buf = xfer->tx_buf;
  267. sgt = &espi->tx_sgt;
  268. conf.dst_addr = espi->sspdr_phys;
  269. conf.dst_addr_width = buswidth;
  270. }
  271. ret = dmaengine_slave_config(chan, &conf);
  272. if (ret)
  273. return ERR_PTR(ret);
  274. /*
  275. * We need to split the transfer into PAGE_SIZE'd chunks. This is
  276. * because we are using @espi->zeropage to provide a zero RX buffer
  277. * for the TX transfers and we have only allocated one page for that.
  278. *
  279. * For performance reasons we allocate a new sg_table only when
  280. * needed. Otherwise we will re-use the current one. Eventually the
  281. * last sg_table is released in ep93xx_spi_release_dma().
  282. */
  283. nents = DIV_ROUND_UP(len, PAGE_SIZE);
  284. if (nents != sgt->nents) {
  285. sg_free_table(sgt);
  286. ret = sg_alloc_table(sgt, nents, GFP_KERNEL);
  287. if (ret)
  288. return ERR_PTR(ret);
  289. }
  290. pbuf = buf;
  291. for_each_sg(sgt->sgl, sg, sgt->nents, i) {
  292. size_t bytes = min_t(size_t, len, PAGE_SIZE);
  293. if (buf) {
  294. sg_set_page(sg, virt_to_page(pbuf), bytes,
  295. offset_in_page(pbuf));
  296. } else {
  297. sg_set_page(sg, virt_to_page(espi->zeropage),
  298. bytes, 0);
  299. }
  300. pbuf += bytes;
  301. len -= bytes;
  302. }
  303. if (WARN_ON(len)) {
  304. dev_warn(&host->dev, "len = %zu expected 0!\n", len);
  305. return ERR_PTR(-EINVAL);
  306. }
  307. nents = dma_map_sg(chan->device->dev, sgt->sgl, sgt->nents, dir);
  308. if (!nents)
  309. return ERR_PTR(-ENOMEM);
  310. txd = dmaengine_prep_slave_sg(chan, sgt->sgl, nents, conf.direction,
  311. DMA_CTRL_ACK);
  312. if (!txd) {
  313. dma_unmap_sg(chan->device->dev, sgt->sgl, sgt->nents, dir);
  314. return ERR_PTR(-ENOMEM);
  315. }
  316. return txd;
  317. }
  318. /**
  319. * ep93xx_spi_dma_finish() - finishes with a DMA transfer
  320. * @host: SPI host
  321. * @dir: DMA transfer direction
  322. *
  323. * Function finishes with the DMA transfer. After this, the DMA buffer is
  324. * unmapped.
  325. */
  326. static void ep93xx_spi_dma_finish(struct spi_controller *host,
  327. enum dma_data_direction dir)
  328. {
  329. struct ep93xx_spi *espi = spi_controller_get_devdata(host);
  330. struct dma_chan *chan;
  331. struct sg_table *sgt;
  332. if (dir == DMA_FROM_DEVICE) {
  333. chan = espi->dma_rx;
  334. sgt = &espi->rx_sgt;
  335. } else {
  336. chan = espi->dma_tx;
  337. sgt = &espi->tx_sgt;
  338. }
  339. dma_unmap_sg(chan->device->dev, sgt->sgl, sgt->nents, dir);
  340. }
  341. static void ep93xx_spi_dma_callback(void *callback_param)
  342. {
  343. struct spi_controller *host = callback_param;
  344. ep93xx_spi_dma_finish(host, DMA_TO_DEVICE);
  345. ep93xx_spi_dma_finish(host, DMA_FROM_DEVICE);
  346. spi_finalize_current_transfer(host);
  347. }
  348. static int ep93xx_spi_dma_transfer(struct spi_controller *host)
  349. {
  350. struct ep93xx_spi *espi = spi_controller_get_devdata(host);
  351. struct dma_async_tx_descriptor *rxd, *txd;
  352. rxd = ep93xx_spi_dma_prepare(host, DMA_FROM_DEVICE);
  353. if (IS_ERR(rxd)) {
  354. dev_err(&host->dev, "DMA RX failed: %ld\n", PTR_ERR(rxd));
  355. return PTR_ERR(rxd);
  356. }
  357. txd = ep93xx_spi_dma_prepare(host, DMA_TO_DEVICE);
  358. if (IS_ERR(txd)) {
  359. ep93xx_spi_dma_finish(host, DMA_FROM_DEVICE);
  360. dev_err(&host->dev, "DMA TX failed: %ld\n", PTR_ERR(txd));
  361. return PTR_ERR(txd);
  362. }
  363. /* We are ready when RX is done */
  364. rxd->callback = ep93xx_spi_dma_callback;
  365. rxd->callback_param = host;
  366. /* Now submit both descriptors and start DMA */
  367. dmaengine_submit(rxd);
  368. dmaengine_submit(txd);
  369. dma_async_issue_pending(espi->dma_rx);
  370. dma_async_issue_pending(espi->dma_tx);
  371. /* signal that we need to wait for completion */
  372. return 1;
  373. }
  374. static irqreturn_t ep93xx_spi_interrupt(int irq, void *dev_id)
  375. {
  376. struct spi_controller *host = dev_id;
  377. struct ep93xx_spi *espi = spi_controller_get_devdata(host);
  378. u32 val;
  379. /*
  380. * If we got ROR (receive overrun) interrupt we know that something is
  381. * wrong. Just abort the message.
  382. */
  383. if (readl(espi->mmio + SSPIIR) & SSPIIR_RORIS) {
  384. /* clear the overrun interrupt */
  385. writel(0, espi->mmio + SSPICR);
  386. dev_warn(&host->dev,
  387. "receive overrun, aborting the message\n");
  388. host->cur_msg->status = -EIO;
  389. } else {
  390. /*
  391. * Interrupt is either RX (RIS) or TX (TIS). For both cases we
  392. * simply execute next data transfer.
  393. */
  394. if (ep93xx_spi_read_write(host)) {
  395. /*
  396. * In normal case, there still is some processing left
  397. * for current transfer. Let's wait for the next
  398. * interrupt then.
  399. */
  400. return IRQ_HANDLED;
  401. }
  402. }
  403. /*
  404. * Current transfer is finished, either with error or with success. In
  405. * any case we disable interrupts and notify the worker to handle
  406. * any post-processing of the message.
  407. */
  408. val = readl(espi->mmio + SSPCR1);
  409. val &= ~(SSPCR1_RORIE | SSPCR1_TIE | SSPCR1_RIE);
  410. writel(val, espi->mmio + SSPCR1);
  411. spi_finalize_current_transfer(host);
  412. return IRQ_HANDLED;
  413. }
  414. static int ep93xx_spi_transfer_one(struct spi_controller *host,
  415. struct spi_device *spi,
  416. struct spi_transfer *xfer)
  417. {
  418. struct ep93xx_spi *espi = spi_controller_get_devdata(host);
  419. u32 val;
  420. int ret;
  421. ret = ep93xx_spi_chip_setup(host, spi, xfer);
  422. if (ret) {
  423. dev_err(&host->dev, "failed to setup chip for transfer\n");
  424. return ret;
  425. }
  426. host->cur_msg->state = xfer;
  427. espi->rx = 0;
  428. espi->tx = 0;
  429. /*
  430. * There is no point of setting up DMA for the transfers which will
  431. * fit into the FIFO and can be transferred with a single interrupt.
  432. * So in these cases we will be using PIO and don't bother for DMA.
  433. */
  434. if (espi->dma_rx && xfer->len > SPI_FIFO_SIZE)
  435. return ep93xx_spi_dma_transfer(host);
  436. /* Using PIO so prime the TX FIFO and enable interrupts */
  437. ep93xx_spi_read_write(host);
  438. val = readl(espi->mmio + SSPCR1);
  439. val |= (SSPCR1_RORIE | SSPCR1_TIE | SSPCR1_RIE);
  440. writel(val, espi->mmio + SSPCR1);
  441. /* signal that we need to wait for completion */
  442. return 1;
  443. }
  444. static int ep93xx_spi_prepare_message(struct spi_controller *host,
  445. struct spi_message *msg)
  446. {
  447. struct ep93xx_spi *espi = spi_controller_get_devdata(host);
  448. unsigned long timeout;
  449. /*
  450. * Just to be sure: flush any data from RX FIFO.
  451. */
  452. timeout = jiffies + msecs_to_jiffies(SPI_TIMEOUT);
  453. while (readl(espi->mmio + SSPSR) & SSPSR_RNE) {
  454. if (time_after(jiffies, timeout)) {
  455. dev_warn(&host->dev,
  456. "timeout while flushing RX FIFO\n");
  457. return -ETIMEDOUT;
  458. }
  459. readl(espi->mmio + SSPDR);
  460. }
  461. /*
  462. * We explicitly handle FIFO level. This way we don't have to check TX
  463. * FIFO status using %SSPSR_TNF bit which may cause RX FIFO overruns.
  464. */
  465. espi->fifo_level = 0;
  466. return 0;
  467. }
  468. static int ep93xx_spi_prepare_hardware(struct spi_controller *host)
  469. {
  470. struct ep93xx_spi *espi = spi_controller_get_devdata(host);
  471. u32 val;
  472. int ret;
  473. ret = clk_prepare_enable(espi->clk);
  474. if (ret)
  475. return ret;
  476. val = readl(espi->mmio + SSPCR1);
  477. val |= SSPCR1_SSE;
  478. writel(val, espi->mmio + SSPCR1);
  479. return 0;
  480. }
  481. static int ep93xx_spi_unprepare_hardware(struct spi_controller *host)
  482. {
  483. struct ep93xx_spi *espi = spi_controller_get_devdata(host);
  484. u32 val;
  485. val = readl(espi->mmio + SSPCR1);
  486. val &= ~SSPCR1_SSE;
  487. writel(val, espi->mmio + SSPCR1);
  488. clk_disable_unprepare(espi->clk);
  489. return 0;
  490. }
  491. static int ep93xx_spi_setup_dma(struct device *dev, struct ep93xx_spi *espi)
  492. {
  493. int ret;
  494. espi->zeropage = (void *)get_zeroed_page(GFP_KERNEL);
  495. if (!espi->zeropage)
  496. return -ENOMEM;
  497. espi->dma_rx = dma_request_chan(dev, "rx");
  498. if (IS_ERR(espi->dma_rx)) {
  499. ret = dev_err_probe(dev, PTR_ERR(espi->dma_rx), "rx DMA setup failed");
  500. goto fail_free_page;
  501. }
  502. espi->dma_tx = dma_request_chan(dev, "tx");
  503. if (IS_ERR(espi->dma_tx)) {
  504. ret = dev_err_probe(dev, PTR_ERR(espi->dma_tx), "tx DMA setup failed");
  505. goto fail_release_rx;
  506. }
  507. return 0;
  508. fail_release_rx:
  509. dma_release_channel(espi->dma_rx);
  510. espi->dma_rx = NULL;
  511. fail_free_page:
  512. free_page((unsigned long)espi->zeropage);
  513. return ret;
  514. }
  515. static void ep93xx_spi_release_dma(struct ep93xx_spi *espi)
  516. {
  517. if (espi->dma_rx) {
  518. dma_release_channel(espi->dma_rx);
  519. sg_free_table(&espi->rx_sgt);
  520. }
  521. if (espi->dma_tx) {
  522. dma_release_channel(espi->dma_tx);
  523. sg_free_table(&espi->tx_sgt);
  524. }
  525. if (espi->zeropage)
  526. free_page((unsigned long)espi->zeropage);
  527. }
  528. static int ep93xx_spi_probe(struct platform_device *pdev)
  529. {
  530. struct spi_controller *host;
  531. struct ep93xx_spi *espi;
  532. struct resource *res;
  533. int irq;
  534. int error;
  535. irq = platform_get_irq(pdev, 0);
  536. if (irq < 0)
  537. return irq;
  538. host = spi_alloc_host(&pdev->dev, sizeof(*espi));
  539. if (!host)
  540. return -ENOMEM;
  541. host->use_gpio_descriptors = true;
  542. host->prepare_transfer_hardware = ep93xx_spi_prepare_hardware;
  543. host->unprepare_transfer_hardware = ep93xx_spi_unprepare_hardware;
  544. host->prepare_message = ep93xx_spi_prepare_message;
  545. host->transfer_one = ep93xx_spi_transfer_one;
  546. host->bus_num = pdev->id;
  547. host->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  548. host->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
  549. /*
  550. * The SPI core will count the number of GPIO descriptors to figure
  551. * out the number of chip selects available on the platform.
  552. */
  553. host->num_chipselect = 0;
  554. platform_set_drvdata(pdev, host);
  555. espi = spi_controller_get_devdata(host);
  556. espi->clk = devm_clk_get(&pdev->dev, NULL);
  557. if (IS_ERR(espi->clk)) {
  558. dev_err(&pdev->dev, "unable to get spi clock\n");
  559. error = PTR_ERR(espi->clk);
  560. goto fail_release_host;
  561. }
  562. /*
  563. * Calculate maximum and minimum supported clock rates
  564. * for the controller.
  565. */
  566. host->max_speed_hz = clk_get_rate(espi->clk) / 2;
  567. host->min_speed_hz = clk_get_rate(espi->clk) / (254 * 256);
  568. espi->mmio = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  569. if (IS_ERR(espi->mmio)) {
  570. error = PTR_ERR(espi->mmio);
  571. goto fail_release_host;
  572. }
  573. espi->sspdr_phys = res->start + SSPDR;
  574. error = devm_request_irq(&pdev->dev, irq, ep93xx_spi_interrupt,
  575. 0, "ep93xx-spi", host);
  576. if (error) {
  577. dev_err(&pdev->dev, "failed to request irq\n");
  578. goto fail_release_host;
  579. }
  580. error = ep93xx_spi_setup_dma(&pdev->dev, espi);
  581. if (error == -EPROBE_DEFER)
  582. goto fail_release_host;
  583. if (error)
  584. dev_warn(&pdev->dev, "DMA setup failed. Falling back to PIO\n");
  585. /* make sure that the hardware is disabled */
  586. writel(0, espi->mmio + SSPCR1);
  587. device_set_node(&host->dev, dev_fwnode(&pdev->dev));
  588. error = devm_spi_register_controller(&pdev->dev, host);
  589. if (error) {
  590. dev_err(&pdev->dev, "failed to register SPI host\n");
  591. goto fail_free_dma;
  592. }
  593. dev_info(&pdev->dev, "EP93xx SPI Controller at 0x%08lx irq %d\n",
  594. (unsigned long)res->start, irq);
  595. return 0;
  596. fail_free_dma:
  597. ep93xx_spi_release_dma(espi);
  598. fail_release_host:
  599. spi_controller_put(host);
  600. return error;
  601. }
  602. static void ep93xx_spi_remove(struct platform_device *pdev)
  603. {
  604. struct spi_controller *host = platform_get_drvdata(pdev);
  605. struct ep93xx_spi *espi = spi_controller_get_devdata(host);
  606. ep93xx_spi_release_dma(espi);
  607. }
  608. static const struct of_device_id ep93xx_spi_of_ids[] = {
  609. { .compatible = "cirrus,ep9301-spi" },
  610. { /* sentinel */ }
  611. };
  612. MODULE_DEVICE_TABLE(of, ep93xx_spi_of_ids);
  613. static struct platform_driver ep93xx_spi_driver = {
  614. .driver = {
  615. .name = "ep93xx-spi",
  616. .of_match_table = ep93xx_spi_of_ids,
  617. },
  618. .probe = ep93xx_spi_probe,
  619. .remove_new = ep93xx_spi_remove,
  620. };
  621. module_platform_driver(ep93xx_spi_driver);
  622. MODULE_DESCRIPTION("EP93xx SPI Controller driver");
  623. MODULE_AUTHOR("Mika Westerberg <mika.westerberg@iki.fi>");
  624. MODULE_LICENSE("GPL");
  625. MODULE_ALIAS("platform:ep93xx-spi");