atmel_spi.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. /*
  2. * Driver for Atmel AT32 and AT91 SPI Controllers
  3. *
  4. * Copyright (C) 2006 Atmel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/clk.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/delay.h>
  16. #include <linux/dma-mapping.h>
  17. #include <linux/err.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/spi/spi.h>
  20. #include <asm/io.h>
  21. #include <asm/arch/board.h>
  22. #include <asm/arch/gpio.h>
  23. #include <asm/arch/cpu.h>
  24. #include "atmel_spi.h"
  25. /*
  26. * The core SPI transfer engine just talks to a register bank to set up
  27. * DMA transfers; transfer queue progress is driven by IRQs. The clock
  28. * framework provides the base clock, subdivided for each spi_device.
  29. *
  30. * Newer controllers, marked with "new_1" flag, have:
  31. * - CR.LASTXFER
  32. * - SPI_MR.DIV32 may become FDIV or must-be-zero (here: always zero)
  33. * - SPI_SR.TXEMPTY, SPI_SR.NSSR (and corresponding irqs)
  34. * - SPI_CSRx.CSAAT
  35. * - SPI_CSRx.SBCR allows faster clocking
  36. */
  37. struct atmel_spi {
  38. spinlock_t lock;
  39. void __iomem *regs;
  40. int irq;
  41. struct clk *clk;
  42. struct platform_device *pdev;
  43. unsigned new_1:1;
  44. struct spi_device *stay;
  45. u8 stopping;
  46. struct list_head queue;
  47. struct spi_transfer *current_transfer;
  48. unsigned long current_remaining_bytes;
  49. struct spi_transfer *next_transfer;
  50. unsigned long next_remaining_bytes;
  51. void *buffer;
  52. dma_addr_t buffer_dma;
  53. };
  54. #define BUFFER_SIZE PAGE_SIZE
  55. #define INVALID_DMA_ADDRESS 0xffffffff
  56. /*
  57. * Earlier SPI controllers (e.g. on at91rm9200) have a design bug whereby
  58. * they assume that spi slave device state will not change on deselect, so
  59. * that automagic deselection is OK. ("NPCSx rises if no data is to be
  60. * transmitted") Not so! Workaround uses nCSx pins as GPIOs; or newer
  61. * controllers have CSAAT and friends.
  62. *
  63. * Since the CSAAT functionality is a bit weird on newer controllers as
  64. * well, we use GPIO to control nCSx pins on all controllers, updating
  65. * MR.PCS to avoid confusing the controller. Using GPIOs also lets us
  66. * support active-high chipselects despite the controller's belief that
  67. * only active-low devices/systems exists.
  68. *
  69. * However, at91rm9200 has a second erratum whereby nCS0 doesn't work
  70. * right when driven with GPIO. ("Mode Fault does not allow more than one
  71. * Master on Chip Select 0.") No workaround exists for that ... so for
  72. * nCS0 on that chip, we (a) don't use the GPIO, (b) can't support CS_HIGH,
  73. * and (c) will trigger that first erratum in some cases.
  74. */
  75. static void cs_activate(struct atmel_spi *as, struct spi_device *spi)
  76. {
  77. unsigned gpio = (unsigned) spi->controller_data;
  78. unsigned active = spi->mode & SPI_CS_HIGH;
  79. u32 mr;
  80. int i;
  81. u32 csr;
  82. u32 cpol = (spi->mode & SPI_CPOL) ? SPI_BIT(CPOL) : 0;
  83. /* Make sure clock polarity is correct */
  84. for (i = 0; i < spi->master->num_chipselect; i++) {
  85. csr = spi_readl(as, CSR0 + 4 * i);
  86. if ((csr ^ cpol) & SPI_BIT(CPOL))
  87. spi_writel(as, CSR0 + 4 * i, csr ^ SPI_BIT(CPOL));
  88. }
  89. mr = spi_readl(as, MR);
  90. mr = SPI_BFINS(PCS, ~(1 << spi->chip_select), mr);
  91. dev_dbg(&spi->dev, "activate %u%s, mr %08x\n",
  92. gpio, active ? " (high)" : "",
  93. mr);
  94. if (!(cpu_is_at91rm9200() && spi->chip_select == 0))
  95. gpio_set_value(gpio, active);
  96. spi_writel(as, MR, mr);
  97. }
  98. static void cs_deactivate(struct atmel_spi *as, struct spi_device *spi)
  99. {
  100. unsigned gpio = (unsigned) spi->controller_data;
  101. unsigned active = spi->mode & SPI_CS_HIGH;
  102. u32 mr;
  103. /* only deactivate *this* device; sometimes transfers to
  104. * another device may be active when this routine is called.
  105. */
  106. mr = spi_readl(as, MR);
  107. if (~SPI_BFEXT(PCS, mr) & (1 << spi->chip_select)) {
  108. mr = SPI_BFINS(PCS, 0xf, mr);
  109. spi_writel(as, MR, mr);
  110. }
  111. dev_dbg(&spi->dev, "DEactivate %u%s, mr %08x\n",
  112. gpio, active ? " (low)" : "",
  113. mr);
  114. if (!(cpu_is_at91rm9200() && spi->chip_select == 0))
  115. gpio_set_value(gpio, !active);
  116. }
  117. static inline int atmel_spi_xfer_is_last(struct spi_message *msg,
  118. struct spi_transfer *xfer)
  119. {
  120. return msg->transfers.prev == &xfer->transfer_list;
  121. }
  122. static inline int atmel_spi_xfer_can_be_chained(struct spi_transfer *xfer)
  123. {
  124. return xfer->delay_usecs == 0 && !xfer->cs_change;
  125. }
  126. static void atmel_spi_next_xfer_data(struct spi_master *master,
  127. struct spi_transfer *xfer,
  128. dma_addr_t *tx_dma,
  129. dma_addr_t *rx_dma,
  130. u32 *plen)
  131. {
  132. struct atmel_spi *as = spi_master_get_devdata(master);
  133. u32 len = *plen;
  134. /* use scratch buffer only when rx or tx data is unspecified */
  135. if (xfer->rx_buf)
  136. *rx_dma = xfer->rx_dma + xfer->len - len;
  137. else {
  138. *rx_dma = as->buffer_dma;
  139. if (len > BUFFER_SIZE)
  140. len = BUFFER_SIZE;
  141. }
  142. if (xfer->tx_buf)
  143. *tx_dma = xfer->tx_dma + xfer->len - len;
  144. else {
  145. *tx_dma = as->buffer_dma;
  146. if (len > BUFFER_SIZE)
  147. len = BUFFER_SIZE;
  148. memset(as->buffer, 0, len);
  149. dma_sync_single_for_device(&as->pdev->dev,
  150. as->buffer_dma, len, DMA_TO_DEVICE);
  151. }
  152. *plen = len;
  153. }
  154. /*
  155. * Submit next transfer for DMA.
  156. * lock is held, spi irq is blocked
  157. */
  158. static void atmel_spi_next_xfer(struct spi_master *master,
  159. struct spi_message *msg)
  160. {
  161. struct atmel_spi *as = spi_master_get_devdata(master);
  162. struct spi_transfer *xfer;
  163. u32 len, remaining, total;
  164. dma_addr_t tx_dma, rx_dma;
  165. if (!as->current_transfer)
  166. xfer = list_entry(msg->transfers.next,
  167. struct spi_transfer, transfer_list);
  168. else if (!as->next_transfer)
  169. xfer = list_entry(as->current_transfer->transfer_list.next,
  170. struct spi_transfer, transfer_list);
  171. else
  172. xfer = NULL;
  173. if (xfer) {
  174. len = xfer->len;
  175. atmel_spi_next_xfer_data(master, xfer, &tx_dma, &rx_dma, &len);
  176. remaining = xfer->len - len;
  177. spi_writel(as, RPR, rx_dma);
  178. spi_writel(as, TPR, tx_dma);
  179. if (msg->spi->bits_per_word > 8)
  180. len >>= 1;
  181. spi_writel(as, RCR, len);
  182. spi_writel(as, TCR, len);
  183. dev_dbg(&msg->spi->dev,
  184. " start xfer %p: len %u tx %p/%08x rx %p/%08x\n",
  185. xfer, xfer->len, xfer->tx_buf, xfer->tx_dma,
  186. xfer->rx_buf, xfer->rx_dma);
  187. } else {
  188. xfer = as->next_transfer;
  189. remaining = as->next_remaining_bytes;
  190. }
  191. as->current_transfer = xfer;
  192. as->current_remaining_bytes = remaining;
  193. if (remaining > 0)
  194. len = remaining;
  195. else if (!atmel_spi_xfer_is_last(msg, xfer)
  196. && atmel_spi_xfer_can_be_chained(xfer)) {
  197. xfer = list_entry(xfer->transfer_list.next,
  198. struct spi_transfer, transfer_list);
  199. len = xfer->len;
  200. } else
  201. xfer = NULL;
  202. as->next_transfer = xfer;
  203. if (xfer) {
  204. total = len;
  205. atmel_spi_next_xfer_data(master, xfer, &tx_dma, &rx_dma, &len);
  206. as->next_remaining_bytes = total - len;
  207. spi_writel(as, RNPR, rx_dma);
  208. spi_writel(as, TNPR, tx_dma);
  209. if (msg->spi->bits_per_word > 8)
  210. len >>= 1;
  211. spi_writel(as, RNCR, len);
  212. spi_writel(as, TNCR, len);
  213. dev_dbg(&msg->spi->dev,
  214. " next xfer %p: len %u tx %p/%08x rx %p/%08x\n",
  215. xfer, xfer->len, xfer->tx_buf, xfer->tx_dma,
  216. xfer->rx_buf, xfer->rx_dma);
  217. } else {
  218. spi_writel(as, RNCR, 0);
  219. spi_writel(as, TNCR, 0);
  220. }
  221. /* REVISIT: We're waiting for ENDRX before we start the next
  222. * transfer because we need to handle some difficult timing
  223. * issues otherwise. If we wait for ENDTX in one transfer and
  224. * then starts waiting for ENDRX in the next, it's difficult
  225. * to tell the difference between the ENDRX interrupt we're
  226. * actually waiting for and the ENDRX interrupt of the
  227. * previous transfer.
  228. *
  229. * It should be doable, though. Just not now...
  230. */
  231. spi_writel(as, IER, SPI_BIT(ENDRX) | SPI_BIT(OVRES));
  232. spi_writel(as, PTCR, SPI_BIT(TXTEN) | SPI_BIT(RXTEN));
  233. }
  234. static void atmel_spi_next_message(struct spi_master *master)
  235. {
  236. struct atmel_spi *as = spi_master_get_devdata(master);
  237. struct spi_message *msg;
  238. struct spi_device *spi;
  239. BUG_ON(as->current_transfer);
  240. msg = list_entry(as->queue.next, struct spi_message, queue);
  241. spi = msg->spi;
  242. dev_dbg(master->dev.parent, "start message %p for %s\n",
  243. msg, spi->dev.bus_id);
  244. /* select chip if it's not still active */
  245. if (as->stay) {
  246. if (as->stay != spi) {
  247. cs_deactivate(as, as->stay);
  248. cs_activate(as, spi);
  249. }
  250. as->stay = NULL;
  251. } else
  252. cs_activate(as, spi);
  253. atmel_spi_next_xfer(master, msg);
  254. }
  255. /*
  256. * For DMA, tx_buf/tx_dma have the same relationship as rx_buf/rx_dma:
  257. * - The buffer is either valid for CPU access, else NULL
  258. * - If the buffer is valid, so is its DMA addresss
  259. *
  260. * This driver manages the dma addresss unless message->is_dma_mapped.
  261. */
  262. static int
  263. atmel_spi_dma_map_xfer(struct atmel_spi *as, struct spi_transfer *xfer)
  264. {
  265. struct device *dev = &as->pdev->dev;
  266. xfer->tx_dma = xfer->rx_dma = INVALID_DMA_ADDRESS;
  267. if (xfer->tx_buf) {
  268. xfer->tx_dma = dma_map_single(dev,
  269. (void *) xfer->tx_buf, xfer->len,
  270. DMA_TO_DEVICE);
  271. if (dma_mapping_error(xfer->tx_dma))
  272. return -ENOMEM;
  273. }
  274. if (xfer->rx_buf) {
  275. xfer->rx_dma = dma_map_single(dev,
  276. xfer->rx_buf, xfer->len,
  277. DMA_FROM_DEVICE);
  278. if (dma_mapping_error(xfer->rx_dma)) {
  279. if (xfer->tx_buf)
  280. dma_unmap_single(dev,
  281. xfer->tx_dma, xfer->len,
  282. DMA_TO_DEVICE);
  283. return -ENOMEM;
  284. }
  285. }
  286. return 0;
  287. }
  288. static void atmel_spi_dma_unmap_xfer(struct spi_master *master,
  289. struct spi_transfer *xfer)
  290. {
  291. if (xfer->tx_dma != INVALID_DMA_ADDRESS)
  292. dma_unmap_single(master->dev.parent, xfer->tx_dma,
  293. xfer->len, DMA_TO_DEVICE);
  294. if (xfer->rx_dma != INVALID_DMA_ADDRESS)
  295. dma_unmap_single(master->dev.parent, xfer->rx_dma,
  296. xfer->len, DMA_FROM_DEVICE);
  297. }
  298. static void
  299. atmel_spi_msg_done(struct spi_master *master, struct atmel_spi *as,
  300. struct spi_message *msg, int status, int stay)
  301. {
  302. if (!stay || status < 0)
  303. cs_deactivate(as, msg->spi);
  304. else
  305. as->stay = msg->spi;
  306. list_del(&msg->queue);
  307. msg->status = status;
  308. dev_dbg(master->dev.parent,
  309. "xfer complete: %u bytes transferred\n",
  310. msg->actual_length);
  311. spin_unlock(&as->lock);
  312. msg->complete(msg->context);
  313. spin_lock(&as->lock);
  314. as->current_transfer = NULL;
  315. as->next_transfer = NULL;
  316. /* continue if needed */
  317. if (list_empty(&as->queue) || as->stopping)
  318. spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
  319. else
  320. atmel_spi_next_message(master);
  321. }
  322. static irqreturn_t
  323. atmel_spi_interrupt(int irq, void *dev_id)
  324. {
  325. struct spi_master *master = dev_id;
  326. struct atmel_spi *as = spi_master_get_devdata(master);
  327. struct spi_message *msg;
  328. struct spi_transfer *xfer;
  329. u32 status, pending, imr;
  330. int ret = IRQ_NONE;
  331. spin_lock(&as->lock);
  332. xfer = as->current_transfer;
  333. msg = list_entry(as->queue.next, struct spi_message, queue);
  334. imr = spi_readl(as, IMR);
  335. status = spi_readl(as, SR);
  336. pending = status & imr;
  337. if (pending & SPI_BIT(OVRES)) {
  338. int timeout;
  339. ret = IRQ_HANDLED;
  340. spi_writel(as, IDR, (SPI_BIT(ENDTX) | SPI_BIT(ENDRX)
  341. | SPI_BIT(OVRES)));
  342. /*
  343. * When we get an overrun, we disregard the current
  344. * transfer. Data will not be copied back from any
  345. * bounce buffer and msg->actual_len will not be
  346. * updated with the last xfer.
  347. *
  348. * We will also not process any remaning transfers in
  349. * the message.
  350. *
  351. * First, stop the transfer and unmap the DMA buffers.
  352. */
  353. spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
  354. if (!msg->is_dma_mapped)
  355. atmel_spi_dma_unmap_xfer(master, xfer);
  356. /* REVISIT: udelay in irq is unfriendly */
  357. if (xfer->delay_usecs)
  358. udelay(xfer->delay_usecs);
  359. dev_warn(master->dev.parent, "fifo overrun (%u/%u remaining)\n",
  360. spi_readl(as, TCR), spi_readl(as, RCR));
  361. /*
  362. * Clean up DMA registers and make sure the data
  363. * registers are empty.
  364. */
  365. spi_writel(as, RNCR, 0);
  366. spi_writel(as, TNCR, 0);
  367. spi_writel(as, RCR, 0);
  368. spi_writel(as, TCR, 0);
  369. for (timeout = 1000; timeout; timeout--)
  370. if (spi_readl(as, SR) & SPI_BIT(TXEMPTY))
  371. break;
  372. if (!timeout)
  373. dev_warn(master->dev.parent,
  374. "timeout waiting for TXEMPTY");
  375. while (spi_readl(as, SR) & SPI_BIT(RDRF))
  376. spi_readl(as, RDR);
  377. /* Clear any overrun happening while cleaning up */
  378. spi_readl(as, SR);
  379. atmel_spi_msg_done(master, as, msg, -EIO, 0);
  380. } else if (pending & SPI_BIT(ENDRX)) {
  381. ret = IRQ_HANDLED;
  382. spi_writel(as, IDR, pending);
  383. if (as->current_remaining_bytes == 0) {
  384. msg->actual_length += xfer->len;
  385. if (!msg->is_dma_mapped)
  386. atmel_spi_dma_unmap_xfer(master, xfer);
  387. /* REVISIT: udelay in irq is unfriendly */
  388. if (xfer->delay_usecs)
  389. udelay(xfer->delay_usecs);
  390. if (atmel_spi_xfer_is_last(msg, xfer)) {
  391. /* report completed message */
  392. atmel_spi_msg_done(master, as, msg, 0,
  393. xfer->cs_change);
  394. } else {
  395. if (xfer->cs_change) {
  396. cs_deactivate(as, msg->spi);
  397. udelay(1);
  398. cs_activate(as, msg->spi);
  399. }
  400. /*
  401. * Not done yet. Submit the next transfer.
  402. *
  403. * FIXME handle protocol options for xfer
  404. */
  405. atmel_spi_next_xfer(master, msg);
  406. }
  407. } else {
  408. /*
  409. * Keep going, we still have data to send in
  410. * the current transfer.
  411. */
  412. atmel_spi_next_xfer(master, msg);
  413. }
  414. }
  415. spin_unlock(&as->lock);
  416. return ret;
  417. }
  418. /* the spi->mode bits understood by this driver: */
  419. #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH)
  420. static int atmel_spi_setup(struct spi_device *spi)
  421. {
  422. struct atmel_spi *as;
  423. u32 scbr, csr;
  424. unsigned int bits = spi->bits_per_word;
  425. unsigned long bus_hz, sck_hz;
  426. unsigned int npcs_pin;
  427. int ret;
  428. as = spi_master_get_devdata(spi->master);
  429. if (as->stopping)
  430. return -ESHUTDOWN;
  431. if (spi->chip_select > spi->master->num_chipselect) {
  432. dev_dbg(&spi->dev,
  433. "setup: invalid chipselect %u (%u defined)\n",
  434. spi->chip_select, spi->master->num_chipselect);
  435. return -EINVAL;
  436. }
  437. if (bits == 0)
  438. bits = 8;
  439. if (bits < 8 || bits > 16) {
  440. dev_dbg(&spi->dev,
  441. "setup: invalid bits_per_word %u (8 to 16)\n",
  442. bits);
  443. return -EINVAL;
  444. }
  445. if (spi->mode & ~MODEBITS) {
  446. dev_dbg(&spi->dev, "setup: unsupported mode bits %x\n",
  447. spi->mode & ~MODEBITS);
  448. return -EINVAL;
  449. }
  450. /* see notes above re chipselect */
  451. if (cpu_is_at91rm9200()
  452. && spi->chip_select == 0
  453. && (spi->mode & SPI_CS_HIGH)) {
  454. dev_dbg(&spi->dev, "setup: can't be active-high\n");
  455. return -EINVAL;
  456. }
  457. /* speed zero convention is used by some upper layers */
  458. bus_hz = clk_get_rate(as->clk);
  459. if (spi->max_speed_hz) {
  460. /* assume div32/fdiv/mbz == 0 */
  461. if (!as->new_1)
  462. bus_hz /= 2;
  463. scbr = ((bus_hz + spi->max_speed_hz - 1)
  464. / spi->max_speed_hz);
  465. if (scbr >= (1 << SPI_SCBR_SIZE)) {
  466. dev_dbg(&spi->dev,
  467. "setup: %d Hz too slow, scbr %u; min %ld Hz\n",
  468. spi->max_speed_hz, scbr, bus_hz/255);
  469. return -EINVAL;
  470. }
  471. } else
  472. scbr = 0xff;
  473. sck_hz = bus_hz / scbr;
  474. csr = SPI_BF(SCBR, scbr) | SPI_BF(BITS, bits - 8);
  475. if (spi->mode & SPI_CPOL)
  476. csr |= SPI_BIT(CPOL);
  477. if (!(spi->mode & SPI_CPHA))
  478. csr |= SPI_BIT(NCPHA);
  479. /* DLYBS is mostly irrelevant since we manage chipselect using GPIOs.
  480. *
  481. * DLYBCT would add delays between words, slowing down transfers.
  482. * It could potentially be useful to cope with DMA bottlenecks, but
  483. * in those cases it's probably best to just use a lower bitrate.
  484. */
  485. csr |= SPI_BF(DLYBS, 0);
  486. csr |= SPI_BF(DLYBCT, 0);
  487. /* chipselect must have been muxed as GPIO (e.g. in board setup) */
  488. npcs_pin = (unsigned int)spi->controller_data;
  489. if (!spi->controller_state) {
  490. ret = gpio_request(npcs_pin, spi->dev.bus_id);
  491. if (ret)
  492. return ret;
  493. spi->controller_state = (void *)npcs_pin;
  494. gpio_direction_output(npcs_pin, !(spi->mode & SPI_CS_HIGH));
  495. } else {
  496. unsigned long flags;
  497. spin_lock_irqsave(&as->lock, flags);
  498. if (as->stay == spi)
  499. as->stay = NULL;
  500. cs_deactivate(as, spi);
  501. spin_unlock_irqrestore(&as->lock, flags);
  502. }
  503. dev_dbg(&spi->dev,
  504. "setup: %lu Hz bpw %u mode 0x%x -> csr%d %08x\n",
  505. sck_hz, bits, spi->mode, spi->chip_select, csr);
  506. spi_writel(as, CSR0 + 4 * spi->chip_select, csr);
  507. return 0;
  508. }
  509. static int atmel_spi_transfer(struct spi_device *spi, struct spi_message *msg)
  510. {
  511. struct atmel_spi *as;
  512. struct spi_transfer *xfer;
  513. unsigned long flags;
  514. struct device *controller = spi->master->dev.parent;
  515. as = spi_master_get_devdata(spi->master);
  516. dev_dbg(controller, "new message %p submitted for %s\n",
  517. msg, spi->dev.bus_id);
  518. if (unlikely(list_empty(&msg->transfers)
  519. || !spi->max_speed_hz))
  520. return -EINVAL;
  521. if (as->stopping)
  522. return -ESHUTDOWN;
  523. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  524. if (!(xfer->tx_buf || xfer->rx_buf)) {
  525. dev_dbg(&spi->dev, "missing rx or tx buf\n");
  526. return -EINVAL;
  527. }
  528. /* FIXME implement these protocol options!! */
  529. if (xfer->bits_per_word || xfer->speed_hz) {
  530. dev_dbg(&spi->dev, "no protocol options yet\n");
  531. return -ENOPROTOOPT;
  532. }
  533. /*
  534. * DMA map early, for performance (empties dcache ASAP) and
  535. * better fault reporting. This is a DMA-only driver.
  536. *
  537. * NOTE that if dma_unmap_single() ever starts to do work on
  538. * platforms supported by this driver, we would need to clean
  539. * up mappings for previously-mapped transfers.
  540. */
  541. if (!msg->is_dma_mapped) {
  542. if (atmel_spi_dma_map_xfer(as, xfer) < 0)
  543. return -ENOMEM;
  544. }
  545. }
  546. #ifdef VERBOSE
  547. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  548. dev_dbg(controller,
  549. " xfer %p: len %u tx %p/%08x rx %p/%08x\n",
  550. xfer, xfer->len,
  551. xfer->tx_buf, xfer->tx_dma,
  552. xfer->rx_buf, xfer->rx_dma);
  553. }
  554. #endif
  555. msg->status = -EINPROGRESS;
  556. msg->actual_length = 0;
  557. spin_lock_irqsave(&as->lock, flags);
  558. list_add_tail(&msg->queue, &as->queue);
  559. if (!as->current_transfer)
  560. atmel_spi_next_message(spi->master);
  561. spin_unlock_irqrestore(&as->lock, flags);
  562. return 0;
  563. }
  564. static void atmel_spi_cleanup(struct spi_device *spi)
  565. {
  566. struct atmel_spi *as = spi_master_get_devdata(spi->master);
  567. unsigned gpio = (unsigned) spi->controller_data;
  568. unsigned long flags;
  569. if (!spi->controller_state)
  570. return;
  571. spin_lock_irqsave(&as->lock, flags);
  572. if (as->stay == spi) {
  573. as->stay = NULL;
  574. cs_deactivate(as, spi);
  575. }
  576. spin_unlock_irqrestore(&as->lock, flags);
  577. gpio_free(gpio);
  578. }
  579. /*-------------------------------------------------------------------------*/
  580. static int __init atmel_spi_probe(struct platform_device *pdev)
  581. {
  582. struct resource *regs;
  583. int irq;
  584. struct clk *clk;
  585. int ret;
  586. struct spi_master *master;
  587. struct atmel_spi *as;
  588. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  589. if (!regs)
  590. return -ENXIO;
  591. irq = platform_get_irq(pdev, 0);
  592. if (irq < 0)
  593. return irq;
  594. clk = clk_get(&pdev->dev, "spi_clk");
  595. if (IS_ERR(clk))
  596. return PTR_ERR(clk);
  597. /* setup spi core then atmel-specific driver state */
  598. ret = -ENOMEM;
  599. master = spi_alloc_master(&pdev->dev, sizeof *as);
  600. if (!master)
  601. goto out_free;
  602. master->bus_num = pdev->id;
  603. master->num_chipselect = 4;
  604. master->setup = atmel_spi_setup;
  605. master->transfer = atmel_spi_transfer;
  606. master->cleanup = atmel_spi_cleanup;
  607. platform_set_drvdata(pdev, master);
  608. as = spi_master_get_devdata(master);
  609. /*
  610. * Scratch buffer is used for throwaway rx and tx data.
  611. * It's coherent to minimize dcache pollution.
  612. */
  613. as->buffer = dma_alloc_coherent(&pdev->dev, BUFFER_SIZE,
  614. &as->buffer_dma, GFP_KERNEL);
  615. if (!as->buffer)
  616. goto out_free;
  617. spin_lock_init(&as->lock);
  618. INIT_LIST_HEAD(&as->queue);
  619. as->pdev = pdev;
  620. as->regs = ioremap(regs->start, (regs->end - regs->start) + 1);
  621. if (!as->regs)
  622. goto out_free_buffer;
  623. as->irq = irq;
  624. as->clk = clk;
  625. if (!cpu_is_at91rm9200())
  626. as->new_1 = 1;
  627. ret = request_irq(irq, atmel_spi_interrupt, 0,
  628. pdev->dev.bus_id, master);
  629. if (ret)
  630. goto out_unmap_regs;
  631. /* Initialize the hardware */
  632. clk_enable(clk);
  633. spi_writel(as, CR, SPI_BIT(SWRST));
  634. spi_writel(as, MR, SPI_BIT(MSTR) | SPI_BIT(MODFDIS));
  635. spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
  636. spi_writel(as, CR, SPI_BIT(SPIEN));
  637. /* go! */
  638. dev_info(&pdev->dev, "Atmel SPI Controller at 0x%08lx (irq %d)\n",
  639. (unsigned long)regs->start, irq);
  640. ret = spi_register_master(master);
  641. if (ret)
  642. goto out_reset_hw;
  643. return 0;
  644. out_reset_hw:
  645. spi_writel(as, CR, SPI_BIT(SWRST));
  646. clk_disable(clk);
  647. free_irq(irq, master);
  648. out_unmap_regs:
  649. iounmap(as->regs);
  650. out_free_buffer:
  651. dma_free_coherent(&pdev->dev, BUFFER_SIZE, as->buffer,
  652. as->buffer_dma);
  653. out_free:
  654. clk_put(clk);
  655. spi_master_put(master);
  656. return ret;
  657. }
  658. static int __exit atmel_spi_remove(struct platform_device *pdev)
  659. {
  660. struct spi_master *master = platform_get_drvdata(pdev);
  661. struct atmel_spi *as = spi_master_get_devdata(master);
  662. struct spi_message *msg;
  663. /* reset the hardware and block queue progress */
  664. spin_lock_irq(&as->lock);
  665. as->stopping = 1;
  666. spi_writel(as, CR, SPI_BIT(SWRST));
  667. spi_readl(as, SR);
  668. spin_unlock_irq(&as->lock);
  669. /* Terminate remaining queued transfers */
  670. list_for_each_entry(msg, &as->queue, queue) {
  671. /* REVISIT unmapping the dma is a NOP on ARM and AVR32
  672. * but we shouldn't depend on that...
  673. */
  674. msg->status = -ESHUTDOWN;
  675. msg->complete(msg->context);
  676. }
  677. dma_free_coherent(&pdev->dev, BUFFER_SIZE, as->buffer,
  678. as->buffer_dma);
  679. clk_disable(as->clk);
  680. clk_put(as->clk);
  681. free_irq(as->irq, master);
  682. iounmap(as->regs);
  683. spi_unregister_master(master);
  684. return 0;
  685. }
  686. #ifdef CONFIG_PM
  687. static int atmel_spi_suspend(struct platform_device *pdev, pm_message_t mesg)
  688. {
  689. struct spi_master *master = platform_get_drvdata(pdev);
  690. struct atmel_spi *as = spi_master_get_devdata(master);
  691. clk_disable(as->clk);
  692. return 0;
  693. }
  694. static int atmel_spi_resume(struct platform_device *pdev)
  695. {
  696. struct spi_master *master = platform_get_drvdata(pdev);
  697. struct atmel_spi *as = spi_master_get_devdata(master);
  698. clk_enable(as->clk);
  699. return 0;
  700. }
  701. #else
  702. #define atmel_spi_suspend NULL
  703. #define atmel_spi_resume NULL
  704. #endif
  705. static struct platform_driver atmel_spi_driver = {
  706. .driver = {
  707. .name = "atmel_spi",
  708. .owner = THIS_MODULE,
  709. },
  710. .suspend = atmel_spi_suspend,
  711. .resume = atmel_spi_resume,
  712. .remove = __exit_p(atmel_spi_remove),
  713. };
  714. static int __init atmel_spi_init(void)
  715. {
  716. return platform_driver_probe(&atmel_spi_driver, atmel_spi_probe);
  717. }
  718. module_init(atmel_spi_init);
  719. static void __exit atmel_spi_exit(void)
  720. {
  721. platform_driver_unregister(&atmel_spi_driver);
  722. }
  723. module_exit(atmel_spi_exit);
  724. MODULE_DESCRIPTION("Atmel AT32/AT91 SPI Controller driver");
  725. MODULE_AUTHOR("Haavard Skinnemoen <hskinnemoen@atmel.com>");
  726. MODULE_LICENSE("GPL");
  727. MODULE_ALIAS("platform:atmel_spi");