spi-atmel.c 28 KB

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