atmel_spi.c 21 KB

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