spi-atmel.c 29 KB

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