atmel_spi.c 20 KB

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