atmel_spi.c 23 KB

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