atmel_spi.c 23 KB

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