atmel_spi.c 22 KB

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