spi-ep93xx.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  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. /**
  220. * ep93xx_spi_dma_prepare() - prepares a DMA transfer
  221. * @master: SPI master
  222. * @dir: DMA transfer direction
  223. *
  224. * Function configures the DMA, maps the buffer and prepares the DMA
  225. * descriptor. Returns a valid DMA descriptor in case of success and ERR_PTR
  226. * in case of failure.
  227. */
  228. static struct dma_async_tx_descriptor *
  229. ep93xx_spi_dma_prepare(struct spi_master *master,
  230. enum dma_transfer_direction dir)
  231. {
  232. struct ep93xx_spi *espi = spi_master_get_devdata(master);
  233. struct spi_transfer *xfer = master->cur_msg->state;
  234. struct dma_async_tx_descriptor *txd;
  235. enum dma_slave_buswidth buswidth;
  236. struct dma_slave_config conf;
  237. struct scatterlist *sg;
  238. struct sg_table *sgt;
  239. struct dma_chan *chan;
  240. const void *buf, *pbuf;
  241. size_t len = xfer->len;
  242. int i, ret, nents;
  243. if (xfer->bits_per_word > 8)
  244. buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
  245. else
  246. buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
  247. memset(&conf, 0, sizeof(conf));
  248. conf.direction = dir;
  249. if (dir == DMA_DEV_TO_MEM) {
  250. chan = espi->dma_rx;
  251. buf = xfer->rx_buf;
  252. sgt = &espi->rx_sgt;
  253. conf.src_addr = espi->sspdr_phys;
  254. conf.src_addr_width = buswidth;
  255. } else {
  256. chan = espi->dma_tx;
  257. buf = xfer->tx_buf;
  258. sgt = &espi->tx_sgt;
  259. conf.dst_addr = espi->sspdr_phys;
  260. conf.dst_addr_width = buswidth;
  261. }
  262. ret = dmaengine_slave_config(chan, &conf);
  263. if (ret)
  264. return ERR_PTR(ret);
  265. /*
  266. * We need to split the transfer into PAGE_SIZE'd chunks. This is
  267. * because we are using @espi->zeropage to provide a zero RX buffer
  268. * for the TX transfers and we have only allocated one page for that.
  269. *
  270. * For performance reasons we allocate a new sg_table only when
  271. * needed. Otherwise we will re-use the current one. Eventually the
  272. * last sg_table is released in ep93xx_spi_release_dma().
  273. */
  274. nents = DIV_ROUND_UP(len, PAGE_SIZE);
  275. if (nents != sgt->nents) {
  276. sg_free_table(sgt);
  277. ret = sg_alloc_table(sgt, nents, GFP_KERNEL);
  278. if (ret)
  279. return ERR_PTR(ret);
  280. }
  281. pbuf = buf;
  282. for_each_sg(sgt->sgl, sg, sgt->nents, i) {
  283. size_t bytes = min_t(size_t, len, PAGE_SIZE);
  284. if (buf) {
  285. sg_set_page(sg, virt_to_page(pbuf), bytes,
  286. offset_in_page(pbuf));
  287. } else {
  288. sg_set_page(sg, virt_to_page(espi->zeropage),
  289. bytes, 0);
  290. }
  291. pbuf += bytes;
  292. len -= bytes;
  293. }
  294. if (WARN_ON(len)) {
  295. dev_warn(&master->dev, "len = %zu expected 0!\n", len);
  296. return ERR_PTR(-EINVAL);
  297. }
  298. nents = dma_map_sg(chan->device->dev, sgt->sgl, sgt->nents, dir);
  299. if (!nents)
  300. return ERR_PTR(-ENOMEM);
  301. txd = dmaengine_prep_slave_sg(chan, sgt->sgl, nents, dir, DMA_CTRL_ACK);
  302. if (!txd) {
  303. dma_unmap_sg(chan->device->dev, sgt->sgl, sgt->nents, dir);
  304. return ERR_PTR(-ENOMEM);
  305. }
  306. return txd;
  307. }
  308. /**
  309. * ep93xx_spi_dma_finish() - finishes with a DMA transfer
  310. * @master: SPI master
  311. * @dir: DMA transfer direction
  312. *
  313. * Function finishes with the DMA transfer. After this, the DMA buffer is
  314. * unmapped.
  315. */
  316. static void ep93xx_spi_dma_finish(struct spi_master *master,
  317. enum dma_transfer_direction dir)
  318. {
  319. struct ep93xx_spi *espi = spi_master_get_devdata(master);
  320. struct dma_chan *chan;
  321. struct sg_table *sgt;
  322. if (dir == DMA_DEV_TO_MEM) {
  323. chan = espi->dma_rx;
  324. sgt = &espi->rx_sgt;
  325. } else {
  326. chan = espi->dma_tx;
  327. sgt = &espi->tx_sgt;
  328. }
  329. dma_unmap_sg(chan->device->dev, sgt->sgl, sgt->nents, dir);
  330. }
  331. static void ep93xx_spi_dma_callback(void *callback_param)
  332. {
  333. struct spi_master *master = callback_param;
  334. ep93xx_spi_dma_finish(master, DMA_MEM_TO_DEV);
  335. ep93xx_spi_dma_finish(master, DMA_DEV_TO_MEM);
  336. spi_finalize_current_transfer(master);
  337. }
  338. static int ep93xx_spi_dma_transfer(struct spi_master *master)
  339. {
  340. struct ep93xx_spi *espi = spi_master_get_devdata(master);
  341. struct dma_async_tx_descriptor *rxd, *txd;
  342. rxd = ep93xx_spi_dma_prepare(master, DMA_DEV_TO_MEM);
  343. if (IS_ERR(rxd)) {
  344. dev_err(&master->dev, "DMA RX failed: %ld\n", PTR_ERR(rxd));
  345. return PTR_ERR(rxd);
  346. }
  347. txd = ep93xx_spi_dma_prepare(master, DMA_MEM_TO_DEV);
  348. if (IS_ERR(txd)) {
  349. ep93xx_spi_dma_finish(master, DMA_DEV_TO_MEM);
  350. dev_err(&master->dev, "DMA TX failed: %ld\n", PTR_ERR(txd));
  351. return PTR_ERR(txd);
  352. }
  353. /* We are ready when RX is done */
  354. rxd->callback = ep93xx_spi_dma_callback;
  355. rxd->callback_param = master;
  356. /* Now submit both descriptors and start DMA */
  357. dmaengine_submit(rxd);
  358. dmaengine_submit(txd);
  359. dma_async_issue_pending(espi->dma_rx);
  360. dma_async_issue_pending(espi->dma_tx);
  361. /* signal that we need to wait for completion */
  362. return 1;
  363. }
  364. static irqreturn_t ep93xx_spi_interrupt(int irq, void *dev_id)
  365. {
  366. struct spi_master *master = dev_id;
  367. struct ep93xx_spi *espi = spi_master_get_devdata(master);
  368. u32 val;
  369. /*
  370. * If we got ROR (receive overrun) interrupt we know that something is
  371. * wrong. Just abort the message.
  372. */
  373. if (readl(espi->mmio + SSPIIR) & SSPIIR_RORIS) {
  374. /* clear the overrun interrupt */
  375. writel(0, espi->mmio + SSPICR);
  376. dev_warn(&master->dev,
  377. "receive overrun, aborting the message\n");
  378. master->cur_msg->status = -EIO;
  379. } else {
  380. /*
  381. * Interrupt is either RX (RIS) or TX (TIS). For both cases we
  382. * simply execute next data transfer.
  383. */
  384. if (ep93xx_spi_read_write(master)) {
  385. /*
  386. * In normal case, there still is some processing left
  387. * for current transfer. Let's wait for the next
  388. * interrupt then.
  389. */
  390. return IRQ_HANDLED;
  391. }
  392. }
  393. /*
  394. * Current transfer is finished, either with error or with success. In
  395. * any case we disable interrupts and notify the worker to handle
  396. * any post-processing of the message.
  397. */
  398. val = readl(espi->mmio + SSPCR1);
  399. val &= ~(SSPCR1_RORIE | SSPCR1_TIE | SSPCR1_RIE);
  400. writel(val, espi->mmio + SSPCR1);
  401. spi_finalize_current_transfer(master);
  402. return IRQ_HANDLED;
  403. }
  404. static int ep93xx_spi_transfer_one(struct spi_master *master,
  405. struct spi_device *spi,
  406. struct spi_transfer *xfer)
  407. {
  408. struct ep93xx_spi *espi = spi_master_get_devdata(master);
  409. u32 val;
  410. int ret;
  411. ret = ep93xx_spi_chip_setup(master, spi, xfer);
  412. if (ret) {
  413. dev_err(&master->dev, "failed to setup chip for transfer\n");
  414. return ret;
  415. }
  416. master->cur_msg->state = xfer;
  417. espi->rx = 0;
  418. espi->tx = 0;
  419. /*
  420. * There is no point of setting up DMA for the transfers which will
  421. * fit into the FIFO and can be transferred with a single interrupt.
  422. * So in these cases we will be using PIO and don't bother for DMA.
  423. */
  424. if (espi->dma_rx && xfer->len > SPI_FIFO_SIZE)
  425. return ep93xx_spi_dma_transfer(master);
  426. /* Using PIO so prime the TX FIFO and enable interrupts */
  427. ep93xx_spi_read_write(master);
  428. val = readl(espi->mmio + SSPCR1);
  429. val |= (SSPCR1_RORIE | SSPCR1_TIE | SSPCR1_RIE);
  430. writel(val, espi->mmio + SSPCR1);
  431. /* signal that we need to wait for completion */
  432. return 1;
  433. }
  434. static int ep93xx_spi_prepare_message(struct spi_master *master,
  435. struct spi_message *msg)
  436. {
  437. struct ep93xx_spi *espi = spi_master_get_devdata(master);
  438. unsigned long timeout;
  439. /*
  440. * Just to be sure: flush any data from RX FIFO.
  441. */
  442. timeout = jiffies + msecs_to_jiffies(SPI_TIMEOUT);
  443. while (readl(espi->mmio + SSPSR) & SSPSR_RNE) {
  444. if (time_after(jiffies, timeout)) {
  445. dev_warn(&master->dev,
  446. "timeout while flushing RX FIFO\n");
  447. return -ETIMEDOUT;
  448. }
  449. readl(espi->mmio + SSPDR);
  450. }
  451. /*
  452. * We explicitly handle FIFO level. This way we don't have to check TX
  453. * FIFO status using %SSPSR_TNF bit which may cause RX FIFO overruns.
  454. */
  455. espi->fifo_level = 0;
  456. return 0;
  457. }
  458. static int ep93xx_spi_prepare_hardware(struct spi_master *master)
  459. {
  460. struct ep93xx_spi *espi = spi_master_get_devdata(master);
  461. u32 val;
  462. int ret;
  463. ret = clk_enable(espi->clk);
  464. if (ret)
  465. return ret;
  466. val = readl(espi->mmio + SSPCR1);
  467. val |= SSPCR1_SSE;
  468. writel(val, espi->mmio + SSPCR1);
  469. return 0;
  470. }
  471. static int ep93xx_spi_unprepare_hardware(struct spi_master *master)
  472. {
  473. struct ep93xx_spi *espi = spi_master_get_devdata(master);
  474. u32 val;
  475. val = readl(espi->mmio + SSPCR1);
  476. val &= ~SSPCR1_SSE;
  477. writel(val, espi->mmio + SSPCR1);
  478. clk_disable(espi->clk);
  479. return 0;
  480. }
  481. static bool ep93xx_spi_dma_filter(struct dma_chan *chan, void *filter_param)
  482. {
  483. if (ep93xx_dma_chan_is_m2p(chan))
  484. return false;
  485. chan->private = filter_param;
  486. return true;
  487. }
  488. static int ep93xx_spi_setup_dma(struct ep93xx_spi *espi)
  489. {
  490. dma_cap_mask_t mask;
  491. int ret;
  492. espi->zeropage = (void *)get_zeroed_page(GFP_KERNEL);
  493. if (!espi->zeropage)
  494. return -ENOMEM;
  495. dma_cap_zero(mask);
  496. dma_cap_set(DMA_SLAVE, mask);
  497. espi->dma_rx_data.port = EP93XX_DMA_SSP;
  498. espi->dma_rx_data.direction = DMA_DEV_TO_MEM;
  499. espi->dma_rx_data.name = "ep93xx-spi-rx";
  500. espi->dma_rx = dma_request_channel(mask, ep93xx_spi_dma_filter,
  501. &espi->dma_rx_data);
  502. if (!espi->dma_rx) {
  503. ret = -ENODEV;
  504. goto fail_free_page;
  505. }
  506. espi->dma_tx_data.port = EP93XX_DMA_SSP;
  507. espi->dma_tx_data.direction = DMA_MEM_TO_DEV;
  508. espi->dma_tx_data.name = "ep93xx-spi-tx";
  509. espi->dma_tx = dma_request_channel(mask, ep93xx_spi_dma_filter,
  510. &espi->dma_tx_data);
  511. if (!espi->dma_tx) {
  512. ret = -ENODEV;
  513. goto fail_release_rx;
  514. }
  515. return 0;
  516. fail_release_rx:
  517. dma_release_channel(espi->dma_rx);
  518. espi->dma_rx = NULL;
  519. fail_free_page:
  520. free_page((unsigned long)espi->zeropage);
  521. return ret;
  522. }
  523. static void ep93xx_spi_release_dma(struct ep93xx_spi *espi)
  524. {
  525. if (espi->dma_rx) {
  526. dma_release_channel(espi->dma_rx);
  527. sg_free_table(&espi->rx_sgt);
  528. }
  529. if (espi->dma_tx) {
  530. dma_release_channel(espi->dma_tx);
  531. sg_free_table(&espi->tx_sgt);
  532. }
  533. if (espi->zeropage)
  534. free_page((unsigned long)espi->zeropage);
  535. }
  536. static int ep93xx_spi_probe(struct platform_device *pdev)
  537. {
  538. struct spi_master *master;
  539. struct ep93xx_spi_info *info;
  540. struct ep93xx_spi *espi;
  541. struct resource *res;
  542. int irq;
  543. int error;
  544. int i;
  545. info = dev_get_platdata(&pdev->dev);
  546. if (!info) {
  547. dev_err(&pdev->dev, "missing platform data\n");
  548. return -EINVAL;
  549. }
  550. irq = platform_get_irq(pdev, 0);
  551. if (irq < 0) {
  552. dev_err(&pdev->dev, "failed to get irq resources\n");
  553. return -EBUSY;
  554. }
  555. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  556. if (!res) {
  557. dev_err(&pdev->dev, "unable to get iomem resource\n");
  558. return -ENODEV;
  559. }
  560. master = spi_alloc_master(&pdev->dev, sizeof(*espi));
  561. if (!master)
  562. return -ENOMEM;
  563. master->prepare_transfer_hardware = ep93xx_spi_prepare_hardware;
  564. master->unprepare_transfer_hardware = ep93xx_spi_unprepare_hardware;
  565. master->prepare_message = ep93xx_spi_prepare_message;
  566. master->transfer_one = ep93xx_spi_transfer_one;
  567. master->bus_num = pdev->id;
  568. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  569. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
  570. master->num_chipselect = info->num_chipselect;
  571. master->cs_gpios = devm_kzalloc(&master->dev,
  572. sizeof(int) * master->num_chipselect,
  573. GFP_KERNEL);
  574. if (!master->cs_gpios) {
  575. error = -ENOMEM;
  576. goto fail_release_master;
  577. }
  578. for (i = 0; i < master->num_chipselect; i++) {
  579. master->cs_gpios[i] = info->chipselect[i];
  580. if (!gpio_is_valid(master->cs_gpios[i]))
  581. continue;
  582. error = devm_gpio_request_one(&pdev->dev, master->cs_gpios[i],
  583. GPIOF_OUT_INIT_HIGH,
  584. "ep93xx-spi");
  585. if (error) {
  586. dev_err(&pdev->dev, "could not request cs gpio %d\n",
  587. master->cs_gpios[i]);
  588. goto fail_release_master;
  589. }
  590. }
  591. platform_set_drvdata(pdev, master);
  592. espi = spi_master_get_devdata(master);
  593. espi->clk = devm_clk_get(&pdev->dev, NULL);
  594. if (IS_ERR(espi->clk)) {
  595. dev_err(&pdev->dev, "unable to get spi clock\n");
  596. error = PTR_ERR(espi->clk);
  597. goto fail_release_master;
  598. }
  599. /*
  600. * Calculate maximum and minimum supported clock rates
  601. * for the controller.
  602. */
  603. master->max_speed_hz = clk_get_rate(espi->clk) / 2;
  604. master->min_speed_hz = clk_get_rate(espi->clk) / (254 * 256);
  605. espi->sspdr_phys = res->start + SSPDR;
  606. espi->mmio = devm_ioremap_resource(&pdev->dev, res);
  607. if (IS_ERR(espi->mmio)) {
  608. error = PTR_ERR(espi->mmio);
  609. goto fail_release_master;
  610. }
  611. error = devm_request_irq(&pdev->dev, irq, ep93xx_spi_interrupt,
  612. 0, "ep93xx-spi", master);
  613. if (error) {
  614. dev_err(&pdev->dev, "failed to request irq\n");
  615. goto fail_release_master;
  616. }
  617. if (info->use_dma && ep93xx_spi_setup_dma(espi))
  618. dev_warn(&pdev->dev, "DMA setup failed. Falling back to PIO\n");
  619. /* make sure that the hardware is disabled */
  620. writel(0, espi->mmio + SSPCR1);
  621. error = devm_spi_register_master(&pdev->dev, master);
  622. if (error) {
  623. dev_err(&pdev->dev, "failed to register SPI master\n");
  624. goto fail_free_dma;
  625. }
  626. dev_info(&pdev->dev, "EP93xx SPI Controller at 0x%08lx irq %d\n",
  627. (unsigned long)res->start, irq);
  628. return 0;
  629. fail_free_dma:
  630. ep93xx_spi_release_dma(espi);
  631. fail_release_master:
  632. spi_master_put(master);
  633. return error;
  634. }
  635. static int ep93xx_spi_remove(struct platform_device *pdev)
  636. {
  637. struct spi_master *master = platform_get_drvdata(pdev);
  638. struct ep93xx_spi *espi = spi_master_get_devdata(master);
  639. ep93xx_spi_release_dma(espi);
  640. return 0;
  641. }
  642. static struct platform_driver ep93xx_spi_driver = {
  643. .driver = {
  644. .name = "ep93xx-spi",
  645. },
  646. .probe = ep93xx_spi_probe,
  647. .remove = ep93xx_spi_remove,
  648. };
  649. module_platform_driver(ep93xx_spi_driver);
  650. MODULE_DESCRIPTION("EP93xx SPI Controller driver");
  651. MODULE_AUTHOR("Mika Westerberg <mika.westerberg@iki.fi>");
  652. MODULE_LICENSE("GPL");
  653. MODULE_ALIAS("platform:ep93xx-spi");