atmel_spi.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  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 address
  297. *
  298. * This driver manages the dma address 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. /* tx_buf is a const void* where we need a void * for the dma
  307. * mapping */
  308. void *nonconst_tx = (void *)xfer->tx_buf;
  309. xfer->tx_dma = dma_map_single(dev,
  310. nonconst_tx, xfer->len,
  311. DMA_TO_DEVICE);
  312. if (dma_mapping_error(dev, xfer->tx_dma))
  313. return -ENOMEM;
  314. }
  315. if (xfer->rx_buf) {
  316. xfer->rx_dma = dma_map_single(dev,
  317. xfer->rx_buf, xfer->len,
  318. DMA_FROM_DEVICE);
  319. if (dma_mapping_error(dev, xfer->rx_dma)) {
  320. if (xfer->tx_buf)
  321. dma_unmap_single(dev,
  322. xfer->tx_dma, xfer->len,
  323. DMA_TO_DEVICE);
  324. return -ENOMEM;
  325. }
  326. }
  327. return 0;
  328. }
  329. static void atmel_spi_dma_unmap_xfer(struct spi_master *master,
  330. struct spi_transfer *xfer)
  331. {
  332. if (xfer->tx_dma != INVALID_DMA_ADDRESS)
  333. dma_unmap_single(master->dev.parent, xfer->tx_dma,
  334. xfer->len, DMA_TO_DEVICE);
  335. if (xfer->rx_dma != INVALID_DMA_ADDRESS)
  336. dma_unmap_single(master->dev.parent, xfer->rx_dma,
  337. xfer->len, DMA_FROM_DEVICE);
  338. }
  339. static void
  340. atmel_spi_msg_done(struct spi_master *master, struct atmel_spi *as,
  341. struct spi_message *msg, int status, int stay)
  342. {
  343. if (!stay || status < 0)
  344. cs_deactivate(as, msg->spi);
  345. else
  346. as->stay = msg->spi;
  347. list_del(&msg->queue);
  348. msg->status = status;
  349. dev_dbg(master->dev.parent,
  350. "xfer complete: %u bytes transferred\n",
  351. msg->actual_length);
  352. spin_unlock(&as->lock);
  353. msg->complete(msg->context);
  354. spin_lock(&as->lock);
  355. as->current_transfer = NULL;
  356. as->next_transfer = NULL;
  357. /* continue if needed */
  358. if (list_empty(&as->queue) || as->stopping)
  359. spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
  360. else
  361. atmel_spi_next_message(master);
  362. }
  363. static irqreturn_t
  364. atmel_spi_interrupt(int irq, void *dev_id)
  365. {
  366. struct spi_master *master = dev_id;
  367. struct atmel_spi *as = spi_master_get_devdata(master);
  368. struct spi_message *msg;
  369. struct spi_transfer *xfer;
  370. u32 status, pending, imr;
  371. int ret = IRQ_NONE;
  372. spin_lock(&as->lock);
  373. xfer = as->current_transfer;
  374. msg = list_entry(as->queue.next, struct spi_message, queue);
  375. imr = spi_readl(as, IMR);
  376. status = spi_readl(as, SR);
  377. pending = status & imr;
  378. if (pending & SPI_BIT(OVRES)) {
  379. int timeout;
  380. ret = IRQ_HANDLED;
  381. spi_writel(as, IDR, (SPI_BIT(RXBUFF) | SPI_BIT(ENDRX)
  382. | SPI_BIT(OVRES)));
  383. /*
  384. * When we get an overrun, we disregard the current
  385. * transfer. Data will not be copied back from any
  386. * bounce buffer and msg->actual_len will not be
  387. * updated with the last xfer.
  388. *
  389. * We will also not process any remaning transfers in
  390. * the message.
  391. *
  392. * First, stop the transfer and unmap the DMA buffers.
  393. */
  394. spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
  395. if (!msg->is_dma_mapped)
  396. atmel_spi_dma_unmap_xfer(master, xfer);
  397. /* REVISIT: udelay in irq is unfriendly */
  398. if (xfer->delay_usecs)
  399. udelay(xfer->delay_usecs);
  400. dev_warn(master->dev.parent, "overrun (%u/%u remaining)\n",
  401. spi_readl(as, TCR), spi_readl(as, RCR));
  402. /*
  403. * Clean up DMA registers and make sure the data
  404. * registers are empty.
  405. */
  406. spi_writel(as, RNCR, 0);
  407. spi_writel(as, TNCR, 0);
  408. spi_writel(as, RCR, 0);
  409. spi_writel(as, TCR, 0);
  410. for (timeout = 1000; timeout; timeout--)
  411. if (spi_readl(as, SR) & SPI_BIT(TXEMPTY))
  412. break;
  413. if (!timeout)
  414. dev_warn(master->dev.parent,
  415. "timeout waiting for TXEMPTY");
  416. while (spi_readl(as, SR) & SPI_BIT(RDRF))
  417. spi_readl(as, RDR);
  418. /* Clear any overrun happening while cleaning up */
  419. spi_readl(as, SR);
  420. atmel_spi_msg_done(master, as, msg, -EIO, 0);
  421. } else if (pending & (SPI_BIT(RXBUFF) | SPI_BIT(ENDRX))) {
  422. ret = IRQ_HANDLED;
  423. spi_writel(as, IDR, pending);
  424. if (as->current_remaining_bytes == 0) {
  425. msg->actual_length += xfer->len;
  426. if (!msg->is_dma_mapped)
  427. atmel_spi_dma_unmap_xfer(master, xfer);
  428. /* REVISIT: udelay in irq is unfriendly */
  429. if (xfer->delay_usecs)
  430. udelay(xfer->delay_usecs);
  431. if (atmel_spi_xfer_is_last(msg, xfer)) {
  432. /* report completed message */
  433. atmel_spi_msg_done(master, as, msg, 0,
  434. xfer->cs_change);
  435. } else {
  436. if (xfer->cs_change) {
  437. cs_deactivate(as, msg->spi);
  438. udelay(1);
  439. cs_activate(as, msg->spi);
  440. }
  441. /*
  442. * Not done yet. Submit the next transfer.
  443. *
  444. * FIXME handle protocol options for xfer
  445. */
  446. atmel_spi_next_xfer(master, msg);
  447. }
  448. } else {
  449. /*
  450. * Keep going, we still have data to send in
  451. * the current transfer.
  452. */
  453. atmel_spi_next_xfer(master, msg);
  454. }
  455. }
  456. spin_unlock(&as->lock);
  457. return ret;
  458. }
  459. static int atmel_spi_setup(struct spi_device *spi)
  460. {
  461. struct atmel_spi *as;
  462. struct atmel_spi_device *asd;
  463. u32 scbr, csr;
  464. unsigned int bits = spi->bits_per_word;
  465. unsigned long bus_hz;
  466. unsigned int npcs_pin;
  467. int ret;
  468. as = spi_master_get_devdata(spi->master);
  469. if (as->stopping)
  470. return -ESHUTDOWN;
  471. if (spi->chip_select > spi->master->num_chipselect) {
  472. dev_dbg(&spi->dev,
  473. "setup: invalid chipselect %u (%u defined)\n",
  474. spi->chip_select, spi->master->num_chipselect);
  475. return -EINVAL;
  476. }
  477. if (bits < 8 || bits > 16) {
  478. dev_dbg(&spi->dev,
  479. "setup: invalid bits_per_word %u (8 to 16)\n",
  480. bits);
  481. return -EINVAL;
  482. }
  483. /* see notes above re chipselect */
  484. if (!atmel_spi_is_v2()
  485. && spi->chip_select == 0
  486. && (spi->mode & SPI_CS_HIGH)) {
  487. dev_dbg(&spi->dev, "setup: can't be active-high\n");
  488. return -EINVAL;
  489. }
  490. /* v1 chips start out at half the peripheral bus speed. */
  491. bus_hz = clk_get_rate(as->clk);
  492. if (!atmel_spi_is_v2())
  493. bus_hz /= 2;
  494. if (spi->max_speed_hz) {
  495. /*
  496. * Calculate the lowest divider that satisfies the
  497. * constraint, assuming div32/fdiv/mbz == 0.
  498. */
  499. scbr = DIV_ROUND_UP(bus_hz, spi->max_speed_hz);
  500. /*
  501. * If the resulting divider doesn't fit into the
  502. * register bitfield, we can't satisfy the constraint.
  503. */
  504. if (scbr >= (1 << SPI_SCBR_SIZE)) {
  505. dev_dbg(&spi->dev,
  506. "setup: %d Hz too slow, scbr %u; min %ld Hz\n",
  507. spi->max_speed_hz, scbr, bus_hz/255);
  508. return -EINVAL;
  509. }
  510. } else
  511. /* speed zero means "as slow as possible" */
  512. scbr = 0xff;
  513. csr = SPI_BF(SCBR, scbr) | SPI_BF(BITS, bits - 8);
  514. if (spi->mode & SPI_CPOL)
  515. csr |= SPI_BIT(CPOL);
  516. if (!(spi->mode & SPI_CPHA))
  517. csr |= SPI_BIT(NCPHA);
  518. /* DLYBS is mostly irrelevant since we manage chipselect using GPIOs.
  519. *
  520. * DLYBCT would add delays between words, slowing down transfers.
  521. * It could potentially be useful to cope with DMA bottlenecks, but
  522. * in those cases it's probably best to just use a lower bitrate.
  523. */
  524. csr |= SPI_BF(DLYBS, 0);
  525. csr |= SPI_BF(DLYBCT, 0);
  526. /* chipselect must have been muxed as GPIO (e.g. in board setup) */
  527. npcs_pin = (unsigned int)spi->controller_data;
  528. asd = spi->controller_state;
  529. if (!asd) {
  530. asd = kzalloc(sizeof(struct atmel_spi_device), GFP_KERNEL);
  531. if (!asd)
  532. return -ENOMEM;
  533. ret = gpio_request(npcs_pin, dev_name(&spi->dev));
  534. if (ret) {
  535. kfree(asd);
  536. return ret;
  537. }
  538. asd->npcs_pin = npcs_pin;
  539. spi->controller_state = asd;
  540. gpio_direction_output(npcs_pin, !(spi->mode & SPI_CS_HIGH));
  541. } else {
  542. unsigned long flags;
  543. spin_lock_irqsave(&as->lock, flags);
  544. if (as->stay == spi)
  545. as->stay = NULL;
  546. cs_deactivate(as, spi);
  547. spin_unlock_irqrestore(&as->lock, flags);
  548. }
  549. asd->csr = csr;
  550. dev_dbg(&spi->dev,
  551. "setup: %lu Hz bpw %u mode 0x%x -> csr%d %08x\n",
  552. bus_hz / scbr, bits, spi->mode, spi->chip_select, csr);
  553. if (!atmel_spi_is_v2())
  554. spi_writel(as, CSR0 + 4 * spi->chip_select, csr);
  555. return 0;
  556. }
  557. static int atmel_spi_transfer(struct spi_device *spi, struct spi_message *msg)
  558. {
  559. struct atmel_spi *as;
  560. struct spi_transfer *xfer;
  561. unsigned long flags;
  562. struct device *controller = spi->master->dev.parent;
  563. u8 bits;
  564. struct atmel_spi_device *asd;
  565. as = spi_master_get_devdata(spi->master);
  566. dev_dbg(controller, "new message %p submitted for %s\n",
  567. msg, dev_name(&spi->dev));
  568. if (unlikely(list_empty(&msg->transfers)))
  569. return -EINVAL;
  570. if (as->stopping)
  571. return -ESHUTDOWN;
  572. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  573. if (!(xfer->tx_buf || xfer->rx_buf) && xfer->len) {
  574. dev_dbg(&spi->dev, "missing rx or tx buf\n");
  575. return -EINVAL;
  576. }
  577. if (xfer->bits_per_word) {
  578. asd = spi->controller_state;
  579. bits = (asd->csr >> 4) & 0xf;
  580. if (bits != xfer->bits_per_word - 8) {
  581. dev_dbg(&spi->dev, "you can't yet change "
  582. "bits_per_word in transfers\n");
  583. return -ENOPROTOOPT;
  584. }
  585. }
  586. /* FIXME implement these protocol options!! */
  587. if (xfer->speed_hz) {
  588. dev_dbg(&spi->dev, "no protocol options yet\n");
  589. return -ENOPROTOOPT;
  590. }
  591. /*
  592. * DMA map early, for performance (empties dcache ASAP) and
  593. * better fault reporting. This is a DMA-only driver.
  594. *
  595. * NOTE that if dma_unmap_single() ever starts to do work on
  596. * platforms supported by this driver, we would need to clean
  597. * up mappings for previously-mapped transfers.
  598. */
  599. if (!msg->is_dma_mapped) {
  600. if (atmel_spi_dma_map_xfer(as, xfer) < 0)
  601. return -ENOMEM;
  602. }
  603. }
  604. #ifdef VERBOSE
  605. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  606. dev_dbg(controller,
  607. " xfer %p: len %u tx %p/%08x rx %p/%08x\n",
  608. xfer, xfer->len,
  609. xfer->tx_buf, xfer->tx_dma,
  610. xfer->rx_buf, xfer->rx_dma);
  611. }
  612. #endif
  613. msg->status = -EINPROGRESS;
  614. msg->actual_length = 0;
  615. spin_lock_irqsave(&as->lock, flags);
  616. list_add_tail(&msg->queue, &as->queue);
  617. if (!as->current_transfer)
  618. atmel_spi_next_message(spi->master);
  619. spin_unlock_irqrestore(&as->lock, flags);
  620. return 0;
  621. }
  622. static void atmel_spi_cleanup(struct spi_device *spi)
  623. {
  624. struct atmel_spi *as = spi_master_get_devdata(spi->master);
  625. struct atmel_spi_device *asd = spi->controller_state;
  626. unsigned gpio = (unsigned) spi->controller_data;
  627. unsigned long flags;
  628. if (!asd)
  629. return;
  630. spin_lock_irqsave(&as->lock, flags);
  631. if (as->stay == spi) {
  632. as->stay = NULL;
  633. cs_deactivate(as, spi);
  634. }
  635. spin_unlock_irqrestore(&as->lock, flags);
  636. spi->controller_state = NULL;
  637. gpio_free(gpio);
  638. kfree(asd);
  639. }
  640. /*-------------------------------------------------------------------------*/
  641. static int __init atmel_spi_probe(struct platform_device *pdev)
  642. {
  643. struct resource *regs;
  644. int irq;
  645. struct clk *clk;
  646. int ret;
  647. struct spi_master *master;
  648. struct atmel_spi *as;
  649. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  650. if (!regs)
  651. return -ENXIO;
  652. irq = platform_get_irq(pdev, 0);
  653. if (irq < 0)
  654. return irq;
  655. clk = clk_get(&pdev->dev, "spi_clk");
  656. if (IS_ERR(clk))
  657. return PTR_ERR(clk);
  658. /* setup spi core then atmel-specific driver state */
  659. ret = -ENOMEM;
  660. master = spi_alloc_master(&pdev->dev, sizeof *as);
  661. if (!master)
  662. goto out_free;
  663. /* the spi->mode bits understood by this driver: */
  664. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  665. master->bus_num = pdev->id;
  666. master->num_chipselect = 4;
  667. master->setup = atmel_spi_setup;
  668. master->transfer = atmel_spi_transfer;
  669. master->cleanup = atmel_spi_cleanup;
  670. platform_set_drvdata(pdev, master);
  671. as = spi_master_get_devdata(master);
  672. /*
  673. * Scratch buffer is used for throwaway rx and tx data.
  674. * It's coherent to minimize dcache pollution.
  675. */
  676. as->buffer = dma_alloc_coherent(&pdev->dev, BUFFER_SIZE,
  677. &as->buffer_dma, GFP_KERNEL);
  678. if (!as->buffer)
  679. goto out_free;
  680. spin_lock_init(&as->lock);
  681. INIT_LIST_HEAD(&as->queue);
  682. as->pdev = pdev;
  683. as->regs = ioremap(regs->start, resource_size(regs));
  684. if (!as->regs)
  685. goto out_free_buffer;
  686. as->irq = irq;
  687. as->clk = clk;
  688. ret = request_irq(irq, atmel_spi_interrupt, 0,
  689. dev_name(&pdev->dev), master);
  690. if (ret)
  691. goto out_unmap_regs;
  692. /* Initialize the hardware */
  693. clk_enable(clk);
  694. spi_writel(as, CR, SPI_BIT(SWRST));
  695. spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */
  696. spi_writel(as, MR, SPI_BIT(MSTR) | SPI_BIT(MODFDIS));
  697. spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
  698. spi_writel(as, CR, SPI_BIT(SPIEN));
  699. /* go! */
  700. dev_info(&pdev->dev, "Atmel SPI Controller at 0x%08lx (irq %d)\n",
  701. (unsigned long)regs->start, irq);
  702. ret = spi_register_master(master);
  703. if (ret)
  704. goto out_reset_hw;
  705. return 0;
  706. out_reset_hw:
  707. spi_writel(as, CR, SPI_BIT(SWRST));
  708. spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */
  709. clk_disable(clk);
  710. free_irq(irq, master);
  711. out_unmap_regs:
  712. iounmap(as->regs);
  713. out_free_buffer:
  714. dma_free_coherent(&pdev->dev, BUFFER_SIZE, as->buffer,
  715. as->buffer_dma);
  716. out_free:
  717. clk_put(clk);
  718. spi_master_put(master);
  719. return ret;
  720. }
  721. static int __exit atmel_spi_remove(struct platform_device *pdev)
  722. {
  723. struct spi_master *master = platform_get_drvdata(pdev);
  724. struct atmel_spi *as = spi_master_get_devdata(master);
  725. struct spi_message *msg;
  726. /* reset the hardware and block queue progress */
  727. spin_lock_irq(&as->lock);
  728. as->stopping = 1;
  729. spi_writel(as, CR, SPI_BIT(SWRST));
  730. spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */
  731. spi_readl(as, SR);
  732. spin_unlock_irq(&as->lock);
  733. /* Terminate remaining queued transfers */
  734. list_for_each_entry(msg, &as->queue, queue) {
  735. /* REVISIT unmapping the dma is a NOP on ARM and AVR32
  736. * but we shouldn't depend on that...
  737. */
  738. msg->status = -ESHUTDOWN;
  739. msg->complete(msg->context);
  740. }
  741. dma_free_coherent(&pdev->dev, BUFFER_SIZE, as->buffer,
  742. as->buffer_dma);
  743. clk_disable(as->clk);
  744. clk_put(as->clk);
  745. free_irq(as->irq, master);
  746. iounmap(as->regs);
  747. spi_unregister_master(master);
  748. return 0;
  749. }
  750. #ifdef CONFIG_PM
  751. static int atmel_spi_suspend(struct platform_device *pdev, pm_message_t mesg)
  752. {
  753. struct spi_master *master = platform_get_drvdata(pdev);
  754. struct atmel_spi *as = spi_master_get_devdata(master);
  755. clk_disable(as->clk);
  756. return 0;
  757. }
  758. static int atmel_spi_resume(struct platform_device *pdev)
  759. {
  760. struct spi_master *master = platform_get_drvdata(pdev);
  761. struct atmel_spi *as = spi_master_get_devdata(master);
  762. clk_enable(as->clk);
  763. return 0;
  764. }
  765. #else
  766. #define atmel_spi_suspend NULL
  767. #define atmel_spi_resume NULL
  768. #endif
  769. static struct platform_driver atmel_spi_driver = {
  770. .driver = {
  771. .name = "atmel_spi",
  772. .owner = THIS_MODULE,
  773. },
  774. .suspend = atmel_spi_suspend,
  775. .resume = atmel_spi_resume,
  776. .remove = __exit_p(atmel_spi_remove),
  777. };
  778. static int __init atmel_spi_init(void)
  779. {
  780. return platform_driver_probe(&atmel_spi_driver, atmel_spi_probe);
  781. }
  782. module_init(atmel_spi_init);
  783. static void __exit atmel_spi_exit(void)
  784. {
  785. platform_driver_unregister(&atmel_spi_driver);
  786. }
  787. module_exit(atmel_spi_exit);
  788. MODULE_DESCRIPTION("Atmel AT32/AT91 SPI Controller driver");
  789. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  790. MODULE_LICENSE("GPL");
  791. MODULE_ALIAS("platform:atmel_spi");