spi-atmel.c 29 KB

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