atmel_spi.c 22 KB

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