spi-ep93xx.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  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. * @pdev: pointer to platform device
  65. * @clk: clock for the controller
  66. * @regs_base: pointer to ioremap()'d registers
  67. * @sspdr_phys: physical address of the SSPDR register
  68. * @wait: wait here until given transfer is completed
  69. * @current_msg: message that is currently processed (or %NULL if none)
  70. * @tx: current byte in transfer to transmit
  71. * @rx: current byte in transfer to receive
  72. * @fifo_level: how full is FIFO (%0..%SPI_FIFO_SIZE - %1). Receiving one
  73. * frame decreases this level and sending one frame increases it.
  74. * @dma_rx: RX DMA channel
  75. * @dma_tx: TX DMA channel
  76. * @dma_rx_data: RX parameters passed to the DMA engine
  77. * @dma_tx_data: TX parameters passed to the DMA engine
  78. * @rx_sgt: sg table for RX transfers
  79. * @tx_sgt: sg table for TX transfers
  80. * @zeropage: dummy page used as RX buffer when only TX buffer is passed in by
  81. * the client
  82. */
  83. struct ep93xx_spi {
  84. const struct platform_device *pdev;
  85. struct clk *clk;
  86. void __iomem *regs_base;
  87. unsigned long sspdr_phys;
  88. struct completion wait;
  89. struct spi_message *current_msg;
  90. size_t tx;
  91. size_t rx;
  92. size_t fifo_level;
  93. struct dma_chan *dma_rx;
  94. struct dma_chan *dma_tx;
  95. struct ep93xx_dma_data dma_rx_data;
  96. struct ep93xx_dma_data dma_tx_data;
  97. struct sg_table rx_sgt;
  98. struct sg_table tx_sgt;
  99. void *zeropage;
  100. };
  101. /* converts bits per word to CR0.DSS value */
  102. #define bits_per_word_to_dss(bpw) ((bpw) - 1)
  103. static void ep93xx_spi_write_u8(const struct ep93xx_spi *espi,
  104. u16 reg, u8 value)
  105. {
  106. writeb(value, espi->regs_base + reg);
  107. }
  108. static u8 ep93xx_spi_read_u8(const struct ep93xx_spi *spi, u16 reg)
  109. {
  110. return readb(spi->regs_base + reg);
  111. }
  112. static void ep93xx_spi_write_u16(const struct ep93xx_spi *espi,
  113. u16 reg, u16 value)
  114. {
  115. writew(value, espi->regs_base + reg);
  116. }
  117. static u16 ep93xx_spi_read_u16(const struct ep93xx_spi *spi, u16 reg)
  118. {
  119. return readw(spi->regs_base + reg);
  120. }
  121. static int ep93xx_spi_enable(const struct ep93xx_spi *espi)
  122. {
  123. u8 regval;
  124. int err;
  125. err = clk_enable(espi->clk);
  126. if (err)
  127. return err;
  128. regval = ep93xx_spi_read_u8(espi, SSPCR1);
  129. regval |= SSPCR1_SSE;
  130. ep93xx_spi_write_u8(espi, SSPCR1, regval);
  131. return 0;
  132. }
  133. static void ep93xx_spi_disable(const struct ep93xx_spi *espi)
  134. {
  135. u8 regval;
  136. regval = ep93xx_spi_read_u8(espi, SSPCR1);
  137. regval &= ~SSPCR1_SSE;
  138. ep93xx_spi_write_u8(espi, SSPCR1, regval);
  139. clk_disable(espi->clk);
  140. }
  141. static void ep93xx_spi_enable_interrupts(const struct ep93xx_spi *espi)
  142. {
  143. u8 regval;
  144. regval = ep93xx_spi_read_u8(espi, SSPCR1);
  145. regval |= (SSPCR1_RORIE | SSPCR1_TIE | SSPCR1_RIE);
  146. ep93xx_spi_write_u8(espi, SSPCR1, regval);
  147. }
  148. static void ep93xx_spi_disable_interrupts(const struct ep93xx_spi *espi)
  149. {
  150. u8 regval;
  151. regval = ep93xx_spi_read_u8(espi, SSPCR1);
  152. regval &= ~(SSPCR1_RORIE | SSPCR1_TIE | SSPCR1_RIE);
  153. ep93xx_spi_write_u8(espi, SSPCR1, regval);
  154. }
  155. /**
  156. * ep93xx_spi_calc_divisors() - calculates SPI clock divisors
  157. * @espi: ep93xx SPI controller struct
  158. * @rate: desired SPI output clock rate
  159. * @div_cpsr: pointer to return the cpsr (pre-scaler) divider
  160. * @div_scr: pointer to return the scr divider
  161. */
  162. static int ep93xx_spi_calc_divisors(const struct ep93xx_spi *espi,
  163. u32 rate, u8 *div_cpsr, u8 *div_scr)
  164. {
  165. struct spi_master *master = platform_get_drvdata(espi->pdev);
  166. unsigned long spi_clk_rate = clk_get_rate(espi->clk);
  167. int cpsr, scr;
  168. /*
  169. * Make sure that max value is between values supported by the
  170. * controller. Note that minimum value is already checked in
  171. * ep93xx_spi_transfer_one_message().
  172. */
  173. rate = clamp(rate, master->min_speed_hz, master->max_speed_hz);
  174. /*
  175. * Calculate divisors so that we can get speed according the
  176. * following formula:
  177. * rate = spi_clock_rate / (cpsr * (1 + scr))
  178. *
  179. * cpsr must be even number and starts from 2, scr can be any number
  180. * between 0 and 255.
  181. */
  182. for (cpsr = 2; cpsr <= 254; cpsr += 2) {
  183. for (scr = 0; scr <= 255; scr++) {
  184. if ((spi_clk_rate / (cpsr * (scr + 1))) <= rate) {
  185. *div_scr = (u8)scr;
  186. *div_cpsr = (u8)cpsr;
  187. return 0;
  188. }
  189. }
  190. }
  191. return -EINVAL;
  192. }
  193. static void ep93xx_spi_cs_control(struct spi_device *spi, bool enable)
  194. {
  195. if (spi->mode & SPI_CS_HIGH)
  196. enable = !enable;
  197. if (gpio_is_valid(spi->cs_gpio))
  198. gpio_set_value(spi->cs_gpio, !enable);
  199. }
  200. static int ep93xx_spi_chip_setup(const struct ep93xx_spi *espi,
  201. struct spi_device *spi,
  202. struct spi_transfer *xfer)
  203. {
  204. u8 dss = bits_per_word_to_dss(xfer->bits_per_word);
  205. u8 div_cpsr = 0;
  206. u8 div_scr = 0;
  207. u16 cr0;
  208. int err;
  209. err = ep93xx_spi_calc_divisors(espi, xfer->speed_hz,
  210. &div_cpsr, &div_scr);
  211. if (err)
  212. return err;
  213. cr0 = div_scr << SSPCR0_SCR_SHIFT;
  214. cr0 |= (spi->mode & (SPI_CPHA | SPI_CPOL)) << SSPCR0_MODE_SHIFT;
  215. cr0 |= dss;
  216. dev_dbg(&espi->pdev->dev, "setup: mode %d, cpsr %d, scr %d, dss %d\n",
  217. spi->mode, div_cpsr, div_scr, dss);
  218. dev_dbg(&espi->pdev->dev, "setup: cr0 %#x\n", cr0);
  219. ep93xx_spi_write_u8(espi, SSPCPSR, div_cpsr);
  220. ep93xx_spi_write_u16(espi, SSPCR0, cr0);
  221. return 0;
  222. }
  223. static void ep93xx_do_write(struct ep93xx_spi *espi, struct spi_transfer *t)
  224. {
  225. if (t->bits_per_word > 8) {
  226. u16 tx_val = 0;
  227. if (t->tx_buf)
  228. tx_val = ((u16 *)t->tx_buf)[espi->tx];
  229. ep93xx_spi_write_u16(espi, SSPDR, tx_val);
  230. espi->tx += sizeof(tx_val);
  231. } else {
  232. u8 tx_val = 0;
  233. if (t->tx_buf)
  234. tx_val = ((u8 *)t->tx_buf)[espi->tx];
  235. ep93xx_spi_write_u8(espi, SSPDR, tx_val);
  236. espi->tx += sizeof(tx_val);
  237. }
  238. }
  239. static void ep93xx_do_read(struct ep93xx_spi *espi, struct spi_transfer *t)
  240. {
  241. if (t->bits_per_word > 8) {
  242. u16 rx_val;
  243. rx_val = ep93xx_spi_read_u16(espi, SSPDR);
  244. if (t->rx_buf)
  245. ((u16 *)t->rx_buf)[espi->rx] = rx_val;
  246. espi->rx += sizeof(rx_val);
  247. } else {
  248. u8 rx_val;
  249. rx_val = ep93xx_spi_read_u8(espi, SSPDR);
  250. if (t->rx_buf)
  251. ((u8 *)t->rx_buf)[espi->rx] = rx_val;
  252. espi->rx += sizeof(rx_val);
  253. }
  254. }
  255. /**
  256. * ep93xx_spi_read_write() - perform next RX/TX transfer
  257. * @espi: ep93xx SPI controller struct
  258. *
  259. * This function transfers next bytes (or half-words) to/from RX/TX FIFOs. If
  260. * called several times, the whole transfer will be completed. Returns
  261. * %-EINPROGRESS when current transfer was not yet completed otherwise %0.
  262. *
  263. * When this function is finished, RX FIFO should be empty and TX FIFO should be
  264. * full.
  265. */
  266. static int ep93xx_spi_read_write(struct ep93xx_spi *espi)
  267. {
  268. struct spi_message *msg = espi->current_msg;
  269. struct spi_transfer *t = msg->state;
  270. /* read as long as RX FIFO has frames in it */
  271. while ((ep93xx_spi_read_u8(espi, SSPSR) & SSPSR_RNE)) {
  272. ep93xx_do_read(espi, t);
  273. espi->fifo_level--;
  274. }
  275. /* write as long as TX FIFO has room */
  276. while (espi->fifo_level < SPI_FIFO_SIZE && espi->tx < t->len) {
  277. ep93xx_do_write(espi, t);
  278. espi->fifo_level++;
  279. }
  280. if (espi->rx == t->len)
  281. return 0;
  282. return -EINPROGRESS;
  283. }
  284. static void ep93xx_spi_pio_transfer(struct ep93xx_spi *espi)
  285. {
  286. /*
  287. * Now everything is set up for the current transfer. We prime the TX
  288. * FIFO, enable interrupts, and wait for the transfer to complete.
  289. */
  290. if (ep93xx_spi_read_write(espi)) {
  291. ep93xx_spi_enable_interrupts(espi);
  292. wait_for_completion(&espi->wait);
  293. }
  294. }
  295. /**
  296. * ep93xx_spi_dma_prepare() - prepares a DMA transfer
  297. * @espi: ep93xx SPI controller struct
  298. * @dir: DMA transfer direction
  299. *
  300. * Function configures the DMA, maps the buffer and prepares the DMA
  301. * descriptor. Returns a valid DMA descriptor in case of success and ERR_PTR
  302. * in case of failure.
  303. */
  304. static struct dma_async_tx_descriptor *
  305. ep93xx_spi_dma_prepare(struct ep93xx_spi *espi, enum dma_transfer_direction dir)
  306. {
  307. struct spi_transfer *t = espi->current_msg->state;
  308. struct dma_async_tx_descriptor *txd;
  309. enum dma_slave_buswidth buswidth;
  310. struct dma_slave_config conf;
  311. struct scatterlist *sg;
  312. struct sg_table *sgt;
  313. struct dma_chan *chan;
  314. const void *buf, *pbuf;
  315. size_t len = t->len;
  316. int i, ret, nents;
  317. if (t->bits_per_word > 8)
  318. buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
  319. else
  320. buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
  321. memset(&conf, 0, sizeof(conf));
  322. conf.direction = dir;
  323. if (dir == DMA_DEV_TO_MEM) {
  324. chan = espi->dma_rx;
  325. buf = t->rx_buf;
  326. sgt = &espi->rx_sgt;
  327. conf.src_addr = espi->sspdr_phys;
  328. conf.src_addr_width = buswidth;
  329. } else {
  330. chan = espi->dma_tx;
  331. buf = t->tx_buf;
  332. sgt = &espi->tx_sgt;
  333. conf.dst_addr = espi->sspdr_phys;
  334. conf.dst_addr_width = buswidth;
  335. }
  336. ret = dmaengine_slave_config(chan, &conf);
  337. if (ret)
  338. return ERR_PTR(ret);
  339. /*
  340. * We need to split the transfer into PAGE_SIZE'd chunks. This is
  341. * because we are using @espi->zeropage to provide a zero RX buffer
  342. * for the TX transfers and we have only allocated one page for that.
  343. *
  344. * For performance reasons we allocate a new sg_table only when
  345. * needed. Otherwise we will re-use the current one. Eventually the
  346. * last sg_table is released in ep93xx_spi_release_dma().
  347. */
  348. nents = DIV_ROUND_UP(len, PAGE_SIZE);
  349. if (nents != sgt->nents) {
  350. sg_free_table(sgt);
  351. ret = sg_alloc_table(sgt, nents, GFP_KERNEL);
  352. if (ret)
  353. return ERR_PTR(ret);
  354. }
  355. pbuf = buf;
  356. for_each_sg(sgt->sgl, sg, sgt->nents, i) {
  357. size_t bytes = min_t(size_t, len, PAGE_SIZE);
  358. if (buf) {
  359. sg_set_page(sg, virt_to_page(pbuf), bytes,
  360. offset_in_page(pbuf));
  361. } else {
  362. sg_set_page(sg, virt_to_page(espi->zeropage),
  363. bytes, 0);
  364. }
  365. pbuf += bytes;
  366. len -= bytes;
  367. }
  368. if (WARN_ON(len)) {
  369. dev_warn(&espi->pdev->dev, "len = %zu expected 0!\n", len);
  370. return ERR_PTR(-EINVAL);
  371. }
  372. nents = dma_map_sg(chan->device->dev, sgt->sgl, sgt->nents, dir);
  373. if (!nents)
  374. return ERR_PTR(-ENOMEM);
  375. txd = dmaengine_prep_slave_sg(chan, sgt->sgl, nents, dir, DMA_CTRL_ACK);
  376. if (!txd) {
  377. dma_unmap_sg(chan->device->dev, sgt->sgl, sgt->nents, dir);
  378. return ERR_PTR(-ENOMEM);
  379. }
  380. return txd;
  381. }
  382. /**
  383. * ep93xx_spi_dma_finish() - finishes with a DMA transfer
  384. * @espi: ep93xx SPI controller struct
  385. * @dir: DMA transfer direction
  386. *
  387. * Function finishes with the DMA transfer. After this, the DMA buffer is
  388. * unmapped.
  389. */
  390. static void ep93xx_spi_dma_finish(struct ep93xx_spi *espi,
  391. enum dma_transfer_direction dir)
  392. {
  393. struct dma_chan *chan;
  394. struct sg_table *sgt;
  395. if (dir == DMA_DEV_TO_MEM) {
  396. chan = espi->dma_rx;
  397. sgt = &espi->rx_sgt;
  398. } else {
  399. chan = espi->dma_tx;
  400. sgt = &espi->tx_sgt;
  401. }
  402. dma_unmap_sg(chan->device->dev, sgt->sgl, sgt->nents, dir);
  403. }
  404. static void ep93xx_spi_dma_callback(void *callback_param)
  405. {
  406. complete(callback_param);
  407. }
  408. static void ep93xx_spi_dma_transfer(struct ep93xx_spi *espi)
  409. {
  410. struct spi_message *msg = espi->current_msg;
  411. struct dma_async_tx_descriptor *rxd, *txd;
  412. rxd = ep93xx_spi_dma_prepare(espi, DMA_DEV_TO_MEM);
  413. if (IS_ERR(rxd)) {
  414. dev_err(&espi->pdev->dev, "DMA RX failed: %ld\n", PTR_ERR(rxd));
  415. msg->status = PTR_ERR(rxd);
  416. return;
  417. }
  418. txd = ep93xx_spi_dma_prepare(espi, DMA_MEM_TO_DEV);
  419. if (IS_ERR(txd)) {
  420. ep93xx_spi_dma_finish(espi, DMA_DEV_TO_MEM);
  421. dev_err(&espi->pdev->dev, "DMA TX failed: %ld\n", PTR_ERR(txd));
  422. msg->status = PTR_ERR(txd);
  423. return;
  424. }
  425. /* We are ready when RX is done */
  426. rxd->callback = ep93xx_spi_dma_callback;
  427. rxd->callback_param = &espi->wait;
  428. /* Now submit both descriptors and wait while they finish */
  429. dmaengine_submit(rxd);
  430. dmaengine_submit(txd);
  431. dma_async_issue_pending(espi->dma_rx);
  432. dma_async_issue_pending(espi->dma_tx);
  433. wait_for_completion(&espi->wait);
  434. ep93xx_spi_dma_finish(espi, DMA_MEM_TO_DEV);
  435. ep93xx_spi_dma_finish(espi, DMA_DEV_TO_MEM);
  436. }
  437. /**
  438. * ep93xx_spi_process_transfer() - processes one SPI transfer
  439. * @espi: ep93xx SPI controller struct
  440. * @msg: current message
  441. * @t: transfer to process
  442. *
  443. * This function processes one SPI transfer given in @t. Function waits until
  444. * transfer is complete (may sleep) and updates @msg->status based on whether
  445. * transfer was successfully processed or not.
  446. */
  447. static void ep93xx_spi_process_transfer(struct ep93xx_spi *espi,
  448. struct spi_message *msg,
  449. struct spi_transfer *t)
  450. {
  451. int err;
  452. msg->state = t;
  453. err = ep93xx_spi_chip_setup(espi, msg->spi, t);
  454. if (err) {
  455. dev_err(&espi->pdev->dev,
  456. "failed to setup chip for transfer\n");
  457. msg->status = err;
  458. return;
  459. }
  460. espi->rx = 0;
  461. espi->tx = 0;
  462. /*
  463. * There is no point of setting up DMA for the transfers which will
  464. * fit into the FIFO and can be transferred with a single interrupt.
  465. * So in these cases we will be using PIO and don't bother for DMA.
  466. */
  467. if (espi->dma_rx && t->len > SPI_FIFO_SIZE)
  468. ep93xx_spi_dma_transfer(espi);
  469. else
  470. ep93xx_spi_pio_transfer(espi);
  471. /*
  472. * In case of error during transmit, we bail out from processing
  473. * the message.
  474. */
  475. if (msg->status)
  476. return;
  477. msg->actual_length += t->len;
  478. /*
  479. * After this transfer is finished, perform any possible
  480. * post-transfer actions requested by the protocol driver.
  481. */
  482. if (t->delay_usecs) {
  483. set_current_state(TASK_UNINTERRUPTIBLE);
  484. schedule_timeout(usecs_to_jiffies(t->delay_usecs));
  485. }
  486. if (t->cs_change) {
  487. if (!list_is_last(&t->transfer_list, &msg->transfers)) {
  488. /*
  489. * In case protocol driver is asking us to drop the
  490. * chipselect briefly, we let the scheduler to handle
  491. * any "delay" here.
  492. */
  493. ep93xx_spi_cs_control(msg->spi, false);
  494. cond_resched();
  495. ep93xx_spi_cs_control(msg->spi, true);
  496. }
  497. }
  498. }
  499. /*
  500. * ep93xx_spi_process_message() - process one SPI message
  501. * @espi: ep93xx SPI controller struct
  502. * @msg: message to process
  503. *
  504. * This function processes a single SPI message. We go through all transfers in
  505. * the message and pass them to ep93xx_spi_process_transfer(). Chipselect is
  506. * asserted during the whole message (unless per transfer cs_change is set).
  507. *
  508. * @msg->status contains %0 in case of success or negative error code in case of
  509. * failure.
  510. */
  511. static void ep93xx_spi_process_message(struct ep93xx_spi *espi,
  512. struct spi_message *msg)
  513. {
  514. unsigned long timeout;
  515. struct spi_transfer *t;
  516. int err;
  517. /*
  518. * Enable the SPI controller and its clock.
  519. */
  520. err = ep93xx_spi_enable(espi);
  521. if (err) {
  522. dev_err(&espi->pdev->dev, "failed to enable SPI controller\n");
  523. msg->status = err;
  524. return;
  525. }
  526. /*
  527. * Just to be sure: flush any data from RX FIFO.
  528. */
  529. timeout = jiffies + msecs_to_jiffies(SPI_TIMEOUT);
  530. while (ep93xx_spi_read_u16(espi, SSPSR) & SSPSR_RNE) {
  531. if (time_after(jiffies, timeout)) {
  532. dev_warn(&espi->pdev->dev,
  533. "timeout while flushing RX FIFO\n");
  534. msg->status = -ETIMEDOUT;
  535. return;
  536. }
  537. ep93xx_spi_read_u16(espi, SSPDR);
  538. }
  539. /*
  540. * We explicitly handle FIFO level. This way we don't have to check TX
  541. * FIFO status using %SSPSR_TNF bit which may cause RX FIFO overruns.
  542. */
  543. espi->fifo_level = 0;
  544. /*
  545. * Assert the chipselect.
  546. */
  547. ep93xx_spi_cs_control(msg->spi, true);
  548. list_for_each_entry(t, &msg->transfers, transfer_list) {
  549. ep93xx_spi_process_transfer(espi, msg, t);
  550. if (msg->status)
  551. break;
  552. }
  553. /*
  554. * Now the whole message is transferred (or failed for some reason). We
  555. * deselect the device and disable the SPI controller.
  556. */
  557. ep93xx_spi_cs_control(msg->spi, false);
  558. ep93xx_spi_disable(espi);
  559. }
  560. static int ep93xx_spi_transfer_one_message(struct spi_master *master,
  561. struct spi_message *msg)
  562. {
  563. struct ep93xx_spi *espi = spi_master_get_devdata(master);
  564. msg->state = NULL;
  565. msg->status = 0;
  566. msg->actual_length = 0;
  567. espi->current_msg = msg;
  568. ep93xx_spi_process_message(espi, msg);
  569. espi->current_msg = NULL;
  570. spi_finalize_current_message(master);
  571. return 0;
  572. }
  573. static irqreturn_t ep93xx_spi_interrupt(int irq, void *dev_id)
  574. {
  575. struct ep93xx_spi *espi = dev_id;
  576. u8 irq_status = ep93xx_spi_read_u8(espi, SSPIIR);
  577. /*
  578. * If we got ROR (receive overrun) interrupt we know that something is
  579. * wrong. Just abort the message.
  580. */
  581. if (unlikely(irq_status & SSPIIR_RORIS)) {
  582. /* clear the overrun interrupt */
  583. ep93xx_spi_write_u8(espi, SSPICR, 0);
  584. dev_warn(&espi->pdev->dev,
  585. "receive overrun, aborting the message\n");
  586. espi->current_msg->status = -EIO;
  587. } else {
  588. /*
  589. * Interrupt is either RX (RIS) or TX (TIS). For both cases we
  590. * simply execute next data transfer.
  591. */
  592. if (ep93xx_spi_read_write(espi)) {
  593. /*
  594. * In normal case, there still is some processing left
  595. * for current transfer. Let's wait for the next
  596. * interrupt then.
  597. */
  598. return IRQ_HANDLED;
  599. }
  600. }
  601. /*
  602. * Current transfer is finished, either with error or with success. In
  603. * any case we disable interrupts and notify the worker to handle
  604. * any post-processing of the message.
  605. */
  606. ep93xx_spi_disable_interrupts(espi);
  607. complete(&espi->wait);
  608. return IRQ_HANDLED;
  609. }
  610. static bool ep93xx_spi_dma_filter(struct dma_chan *chan, void *filter_param)
  611. {
  612. if (ep93xx_dma_chan_is_m2p(chan))
  613. return false;
  614. chan->private = filter_param;
  615. return true;
  616. }
  617. static int ep93xx_spi_setup_dma(struct ep93xx_spi *espi)
  618. {
  619. dma_cap_mask_t mask;
  620. int ret;
  621. espi->zeropage = (void *)get_zeroed_page(GFP_KERNEL);
  622. if (!espi->zeropage)
  623. return -ENOMEM;
  624. dma_cap_zero(mask);
  625. dma_cap_set(DMA_SLAVE, mask);
  626. espi->dma_rx_data.port = EP93XX_DMA_SSP;
  627. espi->dma_rx_data.direction = DMA_DEV_TO_MEM;
  628. espi->dma_rx_data.name = "ep93xx-spi-rx";
  629. espi->dma_rx = dma_request_channel(mask, ep93xx_spi_dma_filter,
  630. &espi->dma_rx_data);
  631. if (!espi->dma_rx) {
  632. ret = -ENODEV;
  633. goto fail_free_page;
  634. }
  635. espi->dma_tx_data.port = EP93XX_DMA_SSP;
  636. espi->dma_tx_data.direction = DMA_MEM_TO_DEV;
  637. espi->dma_tx_data.name = "ep93xx-spi-tx";
  638. espi->dma_tx = dma_request_channel(mask, ep93xx_spi_dma_filter,
  639. &espi->dma_tx_data);
  640. if (!espi->dma_tx) {
  641. ret = -ENODEV;
  642. goto fail_release_rx;
  643. }
  644. return 0;
  645. fail_release_rx:
  646. dma_release_channel(espi->dma_rx);
  647. espi->dma_rx = NULL;
  648. fail_free_page:
  649. free_page((unsigned long)espi->zeropage);
  650. return ret;
  651. }
  652. static void ep93xx_spi_release_dma(struct ep93xx_spi *espi)
  653. {
  654. if (espi->dma_rx) {
  655. dma_release_channel(espi->dma_rx);
  656. sg_free_table(&espi->rx_sgt);
  657. }
  658. if (espi->dma_tx) {
  659. dma_release_channel(espi->dma_tx);
  660. sg_free_table(&espi->tx_sgt);
  661. }
  662. if (espi->zeropage)
  663. free_page((unsigned long)espi->zeropage);
  664. }
  665. static int ep93xx_spi_probe(struct platform_device *pdev)
  666. {
  667. struct spi_master *master;
  668. struct ep93xx_spi_info *info;
  669. struct ep93xx_spi *espi;
  670. struct resource *res;
  671. int irq;
  672. int error;
  673. int i;
  674. info = dev_get_platdata(&pdev->dev);
  675. if (!info) {
  676. dev_err(&pdev->dev, "missing platform data\n");
  677. return -EINVAL;
  678. }
  679. irq = platform_get_irq(pdev, 0);
  680. if (irq < 0) {
  681. dev_err(&pdev->dev, "failed to get irq resources\n");
  682. return -EBUSY;
  683. }
  684. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  685. if (!res) {
  686. dev_err(&pdev->dev, "unable to get iomem resource\n");
  687. return -ENODEV;
  688. }
  689. master = spi_alloc_master(&pdev->dev, sizeof(*espi));
  690. if (!master)
  691. return -ENOMEM;
  692. master->transfer_one_message = ep93xx_spi_transfer_one_message;
  693. master->bus_num = pdev->id;
  694. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  695. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
  696. master->num_chipselect = info->num_chipselect;
  697. master->cs_gpios = devm_kzalloc(&master->dev,
  698. sizeof(int) * master->num_chipselect,
  699. GFP_KERNEL);
  700. if (!master->cs_gpios) {
  701. error = -ENOMEM;
  702. goto fail_release_master;
  703. }
  704. for (i = 0; i < master->num_chipselect; i++) {
  705. master->cs_gpios[i] = info->chipselect[i];
  706. if (!gpio_is_valid(master->cs_gpios[i]))
  707. continue;
  708. error = devm_gpio_request_one(&pdev->dev, master->cs_gpios[i],
  709. GPIOF_OUT_INIT_HIGH,
  710. "ep93xx-spi");
  711. if (error) {
  712. dev_err(&pdev->dev, "could not request cs gpio %d\n",
  713. master->cs_gpios[i]);
  714. goto fail_release_master;
  715. }
  716. }
  717. platform_set_drvdata(pdev, master);
  718. espi = spi_master_get_devdata(master);
  719. espi->clk = devm_clk_get(&pdev->dev, NULL);
  720. if (IS_ERR(espi->clk)) {
  721. dev_err(&pdev->dev, "unable to get spi clock\n");
  722. error = PTR_ERR(espi->clk);
  723. goto fail_release_master;
  724. }
  725. init_completion(&espi->wait);
  726. /*
  727. * Calculate maximum and minimum supported clock rates
  728. * for the controller.
  729. */
  730. master->max_speed_hz = clk_get_rate(espi->clk) / 2;
  731. master->min_speed_hz = clk_get_rate(espi->clk) / (254 * 256);
  732. espi->pdev = pdev;
  733. espi->sspdr_phys = res->start + SSPDR;
  734. espi->regs_base = devm_ioremap_resource(&pdev->dev, res);
  735. if (IS_ERR(espi->regs_base)) {
  736. error = PTR_ERR(espi->regs_base);
  737. goto fail_release_master;
  738. }
  739. error = devm_request_irq(&pdev->dev, irq, ep93xx_spi_interrupt,
  740. 0, "ep93xx-spi", espi);
  741. if (error) {
  742. dev_err(&pdev->dev, "failed to request irq\n");
  743. goto fail_release_master;
  744. }
  745. if (info->use_dma && ep93xx_spi_setup_dma(espi))
  746. dev_warn(&pdev->dev, "DMA setup failed. Falling back to PIO\n");
  747. /* make sure that the hardware is disabled */
  748. ep93xx_spi_write_u8(espi, SSPCR1, 0);
  749. error = devm_spi_register_master(&pdev->dev, master);
  750. if (error) {
  751. dev_err(&pdev->dev, "failed to register SPI master\n");
  752. goto fail_free_dma;
  753. }
  754. dev_info(&pdev->dev, "EP93xx SPI Controller at 0x%08lx irq %d\n",
  755. (unsigned long)res->start, irq);
  756. return 0;
  757. fail_free_dma:
  758. ep93xx_spi_release_dma(espi);
  759. fail_release_master:
  760. spi_master_put(master);
  761. return error;
  762. }
  763. static int ep93xx_spi_remove(struct platform_device *pdev)
  764. {
  765. struct spi_master *master = platform_get_drvdata(pdev);
  766. struct ep93xx_spi *espi = spi_master_get_devdata(master);
  767. ep93xx_spi_release_dma(espi);
  768. return 0;
  769. }
  770. static struct platform_driver ep93xx_spi_driver = {
  771. .driver = {
  772. .name = "ep93xx-spi",
  773. },
  774. .probe = ep93xx_spi_probe,
  775. .remove = ep93xx_spi_remove,
  776. };
  777. module_platform_driver(ep93xx_spi_driver);
  778. MODULE_DESCRIPTION("EP93xx SPI Controller driver");
  779. MODULE_AUTHOR("Mika Westerberg <mika.westerberg@iki.fi>");
  780. MODULE_LICENSE("GPL");
  781. MODULE_ALIAS("platform:ep93xx-spi");