atmel_spi.c 18 KB

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