spi-ep93xx.c 20 KB

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