spi-atmel.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690
  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/dmaengine.h>
  18. #include <linux/err.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/spi/spi.h>
  21. #include <linux/slab.h>
  22. #include <linux/platform_data/atmel.h>
  23. #include <linux/platform_data/dma-atmel.h>
  24. #include <linux/of.h>
  25. #include <linux/io.h>
  26. #include <linux/gpio.h>
  27. /* SPI register offsets */
  28. #define SPI_CR 0x0000
  29. #define SPI_MR 0x0004
  30. #define SPI_RDR 0x0008
  31. #define SPI_TDR 0x000c
  32. #define SPI_SR 0x0010
  33. #define SPI_IER 0x0014
  34. #define SPI_IDR 0x0018
  35. #define SPI_IMR 0x001c
  36. #define SPI_CSR0 0x0030
  37. #define SPI_CSR1 0x0034
  38. #define SPI_CSR2 0x0038
  39. #define SPI_CSR3 0x003c
  40. #define SPI_VERSION 0x00fc
  41. #define SPI_RPR 0x0100
  42. #define SPI_RCR 0x0104
  43. #define SPI_TPR 0x0108
  44. #define SPI_TCR 0x010c
  45. #define SPI_RNPR 0x0110
  46. #define SPI_RNCR 0x0114
  47. #define SPI_TNPR 0x0118
  48. #define SPI_TNCR 0x011c
  49. #define SPI_PTCR 0x0120
  50. #define SPI_PTSR 0x0124
  51. /* Bitfields in CR */
  52. #define SPI_SPIEN_OFFSET 0
  53. #define SPI_SPIEN_SIZE 1
  54. #define SPI_SPIDIS_OFFSET 1
  55. #define SPI_SPIDIS_SIZE 1
  56. #define SPI_SWRST_OFFSET 7
  57. #define SPI_SWRST_SIZE 1
  58. #define SPI_LASTXFER_OFFSET 24
  59. #define SPI_LASTXFER_SIZE 1
  60. /* Bitfields in MR */
  61. #define SPI_MSTR_OFFSET 0
  62. #define SPI_MSTR_SIZE 1
  63. #define SPI_PS_OFFSET 1
  64. #define SPI_PS_SIZE 1
  65. #define SPI_PCSDEC_OFFSET 2
  66. #define SPI_PCSDEC_SIZE 1
  67. #define SPI_FDIV_OFFSET 3
  68. #define SPI_FDIV_SIZE 1
  69. #define SPI_MODFDIS_OFFSET 4
  70. #define SPI_MODFDIS_SIZE 1
  71. #define SPI_WDRBT_OFFSET 5
  72. #define SPI_WDRBT_SIZE 1
  73. #define SPI_LLB_OFFSET 7
  74. #define SPI_LLB_SIZE 1
  75. #define SPI_PCS_OFFSET 16
  76. #define SPI_PCS_SIZE 4
  77. #define SPI_DLYBCS_OFFSET 24
  78. #define SPI_DLYBCS_SIZE 8
  79. /* Bitfields in RDR */
  80. #define SPI_RD_OFFSET 0
  81. #define SPI_RD_SIZE 16
  82. /* Bitfields in TDR */
  83. #define SPI_TD_OFFSET 0
  84. #define SPI_TD_SIZE 16
  85. /* Bitfields in SR */
  86. #define SPI_RDRF_OFFSET 0
  87. #define SPI_RDRF_SIZE 1
  88. #define SPI_TDRE_OFFSET 1
  89. #define SPI_TDRE_SIZE 1
  90. #define SPI_MODF_OFFSET 2
  91. #define SPI_MODF_SIZE 1
  92. #define SPI_OVRES_OFFSET 3
  93. #define SPI_OVRES_SIZE 1
  94. #define SPI_ENDRX_OFFSET 4
  95. #define SPI_ENDRX_SIZE 1
  96. #define SPI_ENDTX_OFFSET 5
  97. #define SPI_ENDTX_SIZE 1
  98. #define SPI_RXBUFF_OFFSET 6
  99. #define SPI_RXBUFF_SIZE 1
  100. #define SPI_TXBUFE_OFFSET 7
  101. #define SPI_TXBUFE_SIZE 1
  102. #define SPI_NSSR_OFFSET 8
  103. #define SPI_NSSR_SIZE 1
  104. #define SPI_TXEMPTY_OFFSET 9
  105. #define SPI_TXEMPTY_SIZE 1
  106. #define SPI_SPIENS_OFFSET 16
  107. #define SPI_SPIENS_SIZE 1
  108. /* Bitfields in CSR0 */
  109. #define SPI_CPOL_OFFSET 0
  110. #define SPI_CPOL_SIZE 1
  111. #define SPI_NCPHA_OFFSET 1
  112. #define SPI_NCPHA_SIZE 1
  113. #define SPI_CSAAT_OFFSET 3
  114. #define SPI_CSAAT_SIZE 1
  115. #define SPI_BITS_OFFSET 4
  116. #define SPI_BITS_SIZE 4
  117. #define SPI_SCBR_OFFSET 8
  118. #define SPI_SCBR_SIZE 8
  119. #define SPI_DLYBS_OFFSET 16
  120. #define SPI_DLYBS_SIZE 8
  121. #define SPI_DLYBCT_OFFSET 24
  122. #define SPI_DLYBCT_SIZE 8
  123. /* Bitfields in RCR */
  124. #define SPI_RXCTR_OFFSET 0
  125. #define SPI_RXCTR_SIZE 16
  126. /* Bitfields in TCR */
  127. #define SPI_TXCTR_OFFSET 0
  128. #define SPI_TXCTR_SIZE 16
  129. /* Bitfields in RNCR */
  130. #define SPI_RXNCR_OFFSET 0
  131. #define SPI_RXNCR_SIZE 16
  132. /* Bitfields in TNCR */
  133. #define SPI_TXNCR_OFFSET 0
  134. #define SPI_TXNCR_SIZE 16
  135. /* Bitfields in PTCR */
  136. #define SPI_RXTEN_OFFSET 0
  137. #define SPI_RXTEN_SIZE 1
  138. #define SPI_RXTDIS_OFFSET 1
  139. #define SPI_RXTDIS_SIZE 1
  140. #define SPI_TXTEN_OFFSET 8
  141. #define SPI_TXTEN_SIZE 1
  142. #define SPI_TXTDIS_OFFSET 9
  143. #define SPI_TXTDIS_SIZE 1
  144. /* Constants for BITS */
  145. #define SPI_BITS_8_BPT 0
  146. #define SPI_BITS_9_BPT 1
  147. #define SPI_BITS_10_BPT 2
  148. #define SPI_BITS_11_BPT 3
  149. #define SPI_BITS_12_BPT 4
  150. #define SPI_BITS_13_BPT 5
  151. #define SPI_BITS_14_BPT 6
  152. #define SPI_BITS_15_BPT 7
  153. #define SPI_BITS_16_BPT 8
  154. /* Bit manipulation macros */
  155. #define SPI_BIT(name) \
  156. (1 << SPI_##name##_OFFSET)
  157. #define SPI_BF(name,value) \
  158. (((value) & ((1 << SPI_##name##_SIZE) - 1)) << SPI_##name##_OFFSET)
  159. #define SPI_BFEXT(name,value) \
  160. (((value) >> SPI_##name##_OFFSET) & ((1 << SPI_##name##_SIZE) - 1))
  161. #define SPI_BFINS(name,value,old) \
  162. ( ((old) & ~(((1 << SPI_##name##_SIZE) - 1) << SPI_##name##_OFFSET)) \
  163. | SPI_BF(name,value))
  164. /* Register access macros */
  165. #define spi_readl(port,reg) \
  166. __raw_readl((port)->regs + SPI_##reg)
  167. #define spi_writel(port,reg,value) \
  168. __raw_writel((value), (port)->regs + SPI_##reg)
  169. /* use PIO for small transfers, avoiding DMA setup/teardown overhead and
  170. * cache operations; better heuristics consider wordsize and bitrate.
  171. */
  172. #define DMA_MIN_BYTES 16
  173. struct atmel_spi_dma {
  174. struct dma_chan *chan_rx;
  175. struct dma_chan *chan_tx;
  176. struct scatterlist sgrx;
  177. struct scatterlist sgtx;
  178. struct dma_async_tx_descriptor *data_desc_rx;
  179. struct dma_async_tx_descriptor *data_desc_tx;
  180. struct at_dma_slave dma_slave;
  181. };
  182. struct atmel_spi_caps {
  183. bool is_spi2;
  184. bool has_wdrbt;
  185. bool has_dma_support;
  186. };
  187. /*
  188. * The core SPI transfer engine just talks to a register bank to set up
  189. * DMA transfers; transfer queue progress is driven by IRQs. The clock
  190. * framework provides the base clock, subdivided for each spi_device.
  191. */
  192. struct atmel_spi {
  193. spinlock_t lock;
  194. unsigned long flags;
  195. phys_addr_t phybase;
  196. void __iomem *regs;
  197. int irq;
  198. struct clk *clk;
  199. struct platform_device *pdev;
  200. struct spi_device *stay;
  201. u8 stopping;
  202. struct list_head queue;
  203. struct tasklet_struct tasklet;
  204. struct spi_transfer *current_transfer;
  205. unsigned long current_remaining_bytes;
  206. struct spi_transfer *next_transfer;
  207. unsigned long next_remaining_bytes;
  208. int done_status;
  209. /* scratch buffer */
  210. void *buffer;
  211. dma_addr_t buffer_dma;
  212. struct atmel_spi_caps caps;
  213. bool use_dma;
  214. bool use_pdc;
  215. /* dmaengine data */
  216. struct atmel_spi_dma dma;
  217. };
  218. /* Controller-specific per-slave state */
  219. struct atmel_spi_device {
  220. unsigned int npcs_pin;
  221. u32 csr;
  222. };
  223. #define BUFFER_SIZE PAGE_SIZE
  224. #define INVALID_DMA_ADDRESS 0xffffffff
  225. /*
  226. * Version 2 of the SPI controller has
  227. * - CR.LASTXFER
  228. * - SPI_MR.DIV32 may become FDIV or must-be-zero (here: always zero)
  229. * - SPI_SR.TXEMPTY, SPI_SR.NSSR (and corresponding irqs)
  230. * - SPI_CSRx.CSAAT
  231. * - SPI_CSRx.SBCR allows faster clocking
  232. */
  233. static bool atmel_spi_is_v2(struct atmel_spi *as)
  234. {
  235. return as->caps.is_spi2;
  236. }
  237. /*
  238. * Earlier SPI controllers (e.g. on at91rm9200) have a design bug whereby
  239. * they assume that spi slave device state will not change on deselect, so
  240. * that automagic deselection is OK. ("NPCSx rises if no data is to be
  241. * transmitted") Not so! Workaround uses nCSx pins as GPIOs; or newer
  242. * controllers have CSAAT and friends.
  243. *
  244. * Since the CSAAT functionality is a bit weird on newer controllers as
  245. * well, we use GPIO to control nCSx pins on all controllers, updating
  246. * MR.PCS to avoid confusing the controller. Using GPIOs also lets us
  247. * support active-high chipselects despite the controller's belief that
  248. * only active-low devices/systems exists.
  249. *
  250. * However, at91rm9200 has a second erratum whereby nCS0 doesn't work
  251. * right when driven with GPIO. ("Mode Fault does not allow more than one
  252. * Master on Chip Select 0.") No workaround exists for that ... so for
  253. * nCS0 on that chip, we (a) don't use the GPIO, (b) can't support CS_HIGH,
  254. * and (c) will trigger that first erratum in some cases.
  255. */
  256. static void cs_activate(struct atmel_spi *as, struct spi_device *spi)
  257. {
  258. struct atmel_spi_device *asd = spi->controller_state;
  259. unsigned active = spi->mode & SPI_CS_HIGH;
  260. u32 mr;
  261. if (atmel_spi_is_v2(as)) {
  262. spi_writel(as, CSR0 + 4 * spi->chip_select, asd->csr);
  263. /* For the low SPI version, there is a issue that PDC transfer
  264. * on CS1,2,3 needs SPI_CSR0.BITS config as SPI_CSR1,2,3.BITS
  265. */
  266. spi_writel(as, CSR0, asd->csr);
  267. if (as->caps.has_wdrbt) {
  268. spi_writel(as, MR,
  269. SPI_BF(PCS, ~(0x01 << spi->chip_select))
  270. | SPI_BIT(WDRBT)
  271. | SPI_BIT(MODFDIS)
  272. | SPI_BIT(MSTR));
  273. } else {
  274. spi_writel(as, MR,
  275. SPI_BF(PCS, ~(0x01 << spi->chip_select))
  276. | SPI_BIT(MODFDIS)
  277. | SPI_BIT(MSTR));
  278. }
  279. mr = spi_readl(as, MR);
  280. gpio_set_value(asd->npcs_pin, active);
  281. } else {
  282. u32 cpol = (spi->mode & SPI_CPOL) ? SPI_BIT(CPOL) : 0;
  283. int i;
  284. u32 csr;
  285. /* Make sure clock polarity is correct */
  286. for (i = 0; i < spi->master->num_chipselect; i++) {
  287. csr = spi_readl(as, CSR0 + 4 * i);
  288. if ((csr ^ cpol) & SPI_BIT(CPOL))
  289. spi_writel(as, CSR0 + 4 * i,
  290. csr ^ SPI_BIT(CPOL));
  291. }
  292. mr = spi_readl(as, MR);
  293. mr = SPI_BFINS(PCS, ~(1 << spi->chip_select), mr);
  294. if (spi->chip_select != 0)
  295. gpio_set_value(asd->npcs_pin, active);
  296. spi_writel(as, MR, mr);
  297. }
  298. dev_dbg(&spi->dev, "activate %u%s, mr %08x\n",
  299. asd->npcs_pin, active ? " (high)" : "",
  300. mr);
  301. }
  302. static void cs_deactivate(struct atmel_spi *as, struct spi_device *spi)
  303. {
  304. struct atmel_spi_device *asd = spi->controller_state;
  305. unsigned active = spi->mode & SPI_CS_HIGH;
  306. u32 mr;
  307. /* only deactivate *this* device; sometimes transfers to
  308. * another device may be active when this routine is called.
  309. */
  310. mr = spi_readl(as, MR);
  311. if (~SPI_BFEXT(PCS, mr) & (1 << spi->chip_select)) {
  312. mr = SPI_BFINS(PCS, 0xf, mr);
  313. spi_writel(as, MR, mr);
  314. }
  315. dev_dbg(&spi->dev, "DEactivate %u%s, mr %08x\n",
  316. asd->npcs_pin, active ? " (low)" : "",
  317. mr);
  318. if (atmel_spi_is_v2(as) || spi->chip_select != 0)
  319. gpio_set_value(asd->npcs_pin, !active);
  320. }
  321. static void atmel_spi_lock(struct atmel_spi *as)
  322. {
  323. spin_lock_irqsave(&as->lock, as->flags);
  324. }
  325. static void atmel_spi_unlock(struct atmel_spi *as)
  326. {
  327. spin_unlock_irqrestore(&as->lock, as->flags);
  328. }
  329. static inline bool atmel_spi_use_dma(struct atmel_spi *as,
  330. struct spi_transfer *xfer)
  331. {
  332. return as->use_dma && xfer->len >= DMA_MIN_BYTES;
  333. }
  334. static inline int atmel_spi_xfer_is_last(struct spi_message *msg,
  335. struct spi_transfer *xfer)
  336. {
  337. return msg->transfers.prev == &xfer->transfer_list;
  338. }
  339. static inline int atmel_spi_xfer_can_be_chained(struct spi_transfer *xfer)
  340. {
  341. return xfer->delay_usecs == 0 && !xfer->cs_change;
  342. }
  343. static int atmel_spi_dma_slave_config(struct atmel_spi *as,
  344. struct dma_slave_config *slave_config,
  345. u8 bits_per_word)
  346. {
  347. int err = 0;
  348. if (bits_per_word > 8) {
  349. slave_config->dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  350. slave_config->src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  351. } else {
  352. slave_config->dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  353. slave_config->src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  354. }
  355. slave_config->dst_addr = (dma_addr_t)as->phybase + SPI_TDR;
  356. slave_config->src_addr = (dma_addr_t)as->phybase + SPI_RDR;
  357. slave_config->src_maxburst = 1;
  358. slave_config->dst_maxburst = 1;
  359. slave_config->device_fc = false;
  360. slave_config->direction = DMA_MEM_TO_DEV;
  361. if (dmaengine_slave_config(as->dma.chan_tx, slave_config)) {
  362. dev_err(&as->pdev->dev,
  363. "failed to configure tx dma channel\n");
  364. err = -EINVAL;
  365. }
  366. slave_config->direction = DMA_DEV_TO_MEM;
  367. if (dmaengine_slave_config(as->dma.chan_rx, slave_config)) {
  368. dev_err(&as->pdev->dev,
  369. "failed to configure rx dma channel\n");
  370. err = -EINVAL;
  371. }
  372. return err;
  373. }
  374. static bool filter(struct dma_chan *chan, void *slave)
  375. {
  376. struct at_dma_slave *sl = slave;
  377. if (sl->dma_dev == chan->device->dev) {
  378. chan->private = sl;
  379. return true;
  380. } else {
  381. return false;
  382. }
  383. }
  384. static int atmel_spi_configure_dma(struct atmel_spi *as)
  385. {
  386. struct at_dma_slave *sdata = &as->dma.dma_slave;
  387. struct dma_slave_config slave_config;
  388. int err;
  389. if (sdata && sdata->dma_dev) {
  390. dma_cap_mask_t mask;
  391. /* Try to grab two DMA channels */
  392. dma_cap_zero(mask);
  393. dma_cap_set(DMA_SLAVE, mask);
  394. as->dma.chan_tx = dma_request_channel(mask, filter, sdata);
  395. if (as->dma.chan_tx)
  396. as->dma.chan_rx =
  397. dma_request_channel(mask, filter, sdata);
  398. }
  399. if (!as->dma.chan_rx || !as->dma.chan_tx) {
  400. dev_err(&as->pdev->dev,
  401. "DMA channel not available, SPI unable to use DMA\n");
  402. err = -EBUSY;
  403. goto error;
  404. }
  405. err = atmel_spi_dma_slave_config(as, &slave_config, 8);
  406. if (err)
  407. goto error;
  408. dev_info(&as->pdev->dev,
  409. "Using %s (tx) and %s (rx) for DMA transfers\n",
  410. dma_chan_name(as->dma.chan_tx),
  411. dma_chan_name(as->dma.chan_rx));
  412. return 0;
  413. error:
  414. if (as->dma.chan_rx)
  415. dma_release_channel(as->dma.chan_rx);
  416. if (as->dma.chan_tx)
  417. dma_release_channel(as->dma.chan_tx);
  418. return err;
  419. }
  420. static void atmel_spi_stop_dma(struct atmel_spi *as)
  421. {
  422. if (as->dma.chan_rx)
  423. as->dma.chan_rx->device->device_control(as->dma.chan_rx,
  424. DMA_TERMINATE_ALL, 0);
  425. if (as->dma.chan_tx)
  426. as->dma.chan_tx->device->device_control(as->dma.chan_tx,
  427. DMA_TERMINATE_ALL, 0);
  428. }
  429. static void atmel_spi_release_dma(struct atmel_spi *as)
  430. {
  431. if (as->dma.chan_rx)
  432. dma_release_channel(as->dma.chan_rx);
  433. if (as->dma.chan_tx)
  434. dma_release_channel(as->dma.chan_tx);
  435. }
  436. /* This function is called by the DMA driver from tasklet context */
  437. static void dma_callback(void *data)
  438. {
  439. struct spi_master *master = data;
  440. struct atmel_spi *as = spi_master_get_devdata(master);
  441. /* trigger SPI tasklet */
  442. tasklet_schedule(&as->tasklet);
  443. }
  444. /*
  445. * Next transfer using PIO.
  446. * lock is held, spi tasklet is blocked
  447. */
  448. static void atmel_spi_next_xfer_pio(struct spi_master *master,
  449. struct spi_transfer *xfer)
  450. {
  451. struct atmel_spi *as = spi_master_get_devdata(master);
  452. dev_vdbg(master->dev.parent, "atmel_spi_next_xfer_pio\n");
  453. as->current_remaining_bytes = xfer->len;
  454. /* Make sure data is not remaining in RDR */
  455. spi_readl(as, RDR);
  456. while (spi_readl(as, SR) & SPI_BIT(RDRF)) {
  457. spi_readl(as, RDR);
  458. cpu_relax();
  459. }
  460. if (xfer->tx_buf)
  461. spi_writel(as, TDR, *(u8 *)(xfer->tx_buf));
  462. else
  463. spi_writel(as, TDR, 0);
  464. dev_dbg(master->dev.parent,
  465. " start pio xfer %p: len %u tx %p rx %p\n",
  466. xfer, xfer->len, xfer->tx_buf, xfer->rx_buf);
  467. /* Enable relevant interrupts */
  468. spi_writel(as, IER, SPI_BIT(RDRF) | SPI_BIT(OVRES));
  469. }
  470. /*
  471. * Submit next transfer for DMA.
  472. * lock is held, spi tasklet is blocked
  473. */
  474. static int atmel_spi_next_xfer_dma_submit(struct spi_master *master,
  475. struct spi_transfer *xfer,
  476. u32 *plen)
  477. {
  478. struct atmel_spi *as = spi_master_get_devdata(master);
  479. struct dma_chan *rxchan = as->dma.chan_rx;
  480. struct dma_chan *txchan = as->dma.chan_tx;
  481. struct dma_async_tx_descriptor *rxdesc;
  482. struct dma_async_tx_descriptor *txdesc;
  483. struct dma_slave_config slave_config;
  484. dma_cookie_t cookie;
  485. u32 len = *plen;
  486. dev_vdbg(master->dev.parent, "atmel_spi_next_xfer_dma_submit\n");
  487. /* Check that the channels are available */
  488. if (!rxchan || !txchan)
  489. return -ENODEV;
  490. /* release lock for DMA operations */
  491. atmel_spi_unlock(as);
  492. /* prepare the RX dma transfer */
  493. sg_init_table(&as->dma.sgrx, 1);
  494. if (xfer->rx_buf) {
  495. as->dma.sgrx.dma_address = xfer->rx_dma + xfer->len - *plen;
  496. } else {
  497. as->dma.sgrx.dma_address = as->buffer_dma;
  498. if (len > BUFFER_SIZE)
  499. len = BUFFER_SIZE;
  500. }
  501. /* prepare the TX dma transfer */
  502. sg_init_table(&as->dma.sgtx, 1);
  503. if (xfer->tx_buf) {
  504. as->dma.sgtx.dma_address = xfer->tx_dma + xfer->len - *plen;
  505. } else {
  506. as->dma.sgtx.dma_address = as->buffer_dma;
  507. if (len > BUFFER_SIZE)
  508. len = BUFFER_SIZE;
  509. memset(as->buffer, 0, len);
  510. }
  511. sg_dma_len(&as->dma.sgtx) = len;
  512. sg_dma_len(&as->dma.sgrx) = len;
  513. *plen = len;
  514. if (atmel_spi_dma_slave_config(as, &slave_config, 8))
  515. goto err_exit;
  516. /* Send both scatterlists */
  517. rxdesc = rxchan->device->device_prep_slave_sg(rxchan,
  518. &as->dma.sgrx,
  519. 1,
  520. DMA_FROM_DEVICE,
  521. DMA_PREP_INTERRUPT | DMA_CTRL_ACK,
  522. NULL);
  523. if (!rxdesc)
  524. goto err_dma;
  525. txdesc = txchan->device->device_prep_slave_sg(txchan,
  526. &as->dma.sgtx,
  527. 1,
  528. DMA_TO_DEVICE,
  529. DMA_PREP_INTERRUPT | DMA_CTRL_ACK,
  530. NULL);
  531. if (!txdesc)
  532. goto err_dma;
  533. dev_dbg(master->dev.parent,
  534. " start dma xfer %p: len %u tx %p/%08x rx %p/%08x\n",
  535. xfer, xfer->len, xfer->tx_buf, xfer->tx_dma,
  536. xfer->rx_buf, xfer->rx_dma);
  537. /* Enable relevant interrupts */
  538. spi_writel(as, IER, SPI_BIT(OVRES));
  539. /* Put the callback on the RX transfer only, that should finish last */
  540. rxdesc->callback = dma_callback;
  541. rxdesc->callback_param = master;
  542. /* Submit and fire RX and TX with TX last so we're ready to read! */
  543. cookie = rxdesc->tx_submit(rxdesc);
  544. if (dma_submit_error(cookie))
  545. goto err_dma;
  546. cookie = txdesc->tx_submit(txdesc);
  547. if (dma_submit_error(cookie))
  548. goto err_dma;
  549. rxchan->device->device_issue_pending(rxchan);
  550. txchan->device->device_issue_pending(txchan);
  551. /* take back lock */
  552. atmel_spi_lock(as);
  553. return 0;
  554. err_dma:
  555. spi_writel(as, IDR, SPI_BIT(OVRES));
  556. atmel_spi_stop_dma(as);
  557. err_exit:
  558. atmel_spi_lock(as);
  559. return -ENOMEM;
  560. }
  561. static void atmel_spi_next_xfer_data(struct spi_master *master,
  562. struct spi_transfer *xfer,
  563. dma_addr_t *tx_dma,
  564. dma_addr_t *rx_dma,
  565. u32 *plen)
  566. {
  567. struct atmel_spi *as = spi_master_get_devdata(master);
  568. u32 len = *plen;
  569. /* use scratch buffer only when rx or tx data is unspecified */
  570. if (xfer->rx_buf)
  571. *rx_dma = xfer->rx_dma + xfer->len - *plen;
  572. else {
  573. *rx_dma = as->buffer_dma;
  574. if (len > BUFFER_SIZE)
  575. len = BUFFER_SIZE;
  576. }
  577. if (xfer->tx_buf)
  578. *tx_dma = xfer->tx_dma + xfer->len - *plen;
  579. else {
  580. *tx_dma = as->buffer_dma;
  581. if (len > BUFFER_SIZE)
  582. len = BUFFER_SIZE;
  583. memset(as->buffer, 0, len);
  584. dma_sync_single_for_device(&as->pdev->dev,
  585. as->buffer_dma, len, DMA_TO_DEVICE);
  586. }
  587. *plen = len;
  588. }
  589. /*
  590. * Submit next transfer for PDC.
  591. * lock is held, spi irq is blocked
  592. */
  593. static void atmel_spi_pdc_next_xfer(struct spi_master *master,
  594. struct spi_message *msg)
  595. {
  596. struct atmel_spi *as = spi_master_get_devdata(master);
  597. struct spi_transfer *xfer;
  598. u32 len, remaining;
  599. u32 ieval;
  600. dma_addr_t tx_dma, rx_dma;
  601. if (!as->current_transfer)
  602. xfer = list_entry(msg->transfers.next,
  603. struct spi_transfer, transfer_list);
  604. else if (!as->next_transfer)
  605. xfer = list_entry(as->current_transfer->transfer_list.next,
  606. struct spi_transfer, transfer_list);
  607. else
  608. xfer = NULL;
  609. if (xfer) {
  610. spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
  611. len = xfer->len;
  612. atmel_spi_next_xfer_data(master, xfer, &tx_dma, &rx_dma, &len);
  613. remaining = xfer->len - len;
  614. spi_writel(as, RPR, rx_dma);
  615. spi_writel(as, TPR, tx_dma);
  616. if (msg->spi->bits_per_word > 8)
  617. len >>= 1;
  618. spi_writel(as, RCR, len);
  619. spi_writel(as, TCR, len);
  620. dev_dbg(&msg->spi->dev,
  621. " start xfer %p: len %u tx %p/%08x rx %p/%08x\n",
  622. xfer, xfer->len, xfer->tx_buf, xfer->tx_dma,
  623. xfer->rx_buf, xfer->rx_dma);
  624. } else {
  625. xfer = as->next_transfer;
  626. remaining = as->next_remaining_bytes;
  627. }
  628. as->current_transfer = xfer;
  629. as->current_remaining_bytes = remaining;
  630. if (remaining > 0)
  631. len = remaining;
  632. else if (!atmel_spi_xfer_is_last(msg, xfer)
  633. && atmel_spi_xfer_can_be_chained(xfer)) {
  634. xfer = list_entry(xfer->transfer_list.next,
  635. struct spi_transfer, transfer_list);
  636. len = xfer->len;
  637. } else
  638. xfer = NULL;
  639. as->next_transfer = xfer;
  640. if (xfer) {
  641. u32 total;
  642. total = len;
  643. atmel_spi_next_xfer_data(master, xfer, &tx_dma, &rx_dma, &len);
  644. as->next_remaining_bytes = total - len;
  645. spi_writel(as, RNPR, rx_dma);
  646. spi_writel(as, TNPR, tx_dma);
  647. if (msg->spi->bits_per_word > 8)
  648. len >>= 1;
  649. spi_writel(as, RNCR, len);
  650. spi_writel(as, TNCR, len);
  651. dev_dbg(&msg->spi->dev,
  652. " next xfer %p: len %u tx %p/%08x rx %p/%08x\n",
  653. xfer, xfer->len, xfer->tx_buf, xfer->tx_dma,
  654. xfer->rx_buf, xfer->rx_dma);
  655. ieval = SPI_BIT(ENDRX) | SPI_BIT(OVRES);
  656. } else {
  657. spi_writel(as, RNCR, 0);
  658. spi_writel(as, TNCR, 0);
  659. ieval = SPI_BIT(RXBUFF) | SPI_BIT(ENDRX) | SPI_BIT(OVRES);
  660. }
  661. /* REVISIT: We're waiting for ENDRX before we start the next
  662. * transfer because we need to handle some difficult timing
  663. * issues otherwise. If we wait for ENDTX in one transfer and
  664. * then starts waiting for ENDRX in the next, it's difficult
  665. * to tell the difference between the ENDRX interrupt we're
  666. * actually waiting for and the ENDRX interrupt of the
  667. * previous transfer.
  668. *
  669. * It should be doable, though. Just not now...
  670. */
  671. spi_writel(as, IER, ieval);
  672. spi_writel(as, PTCR, SPI_BIT(TXTEN) | SPI_BIT(RXTEN));
  673. }
  674. /*
  675. * Choose way to submit next transfer and start it.
  676. * lock is held, spi tasklet is blocked
  677. */
  678. static void atmel_spi_dma_next_xfer(struct spi_master *master,
  679. struct spi_message *msg)
  680. {
  681. struct atmel_spi *as = spi_master_get_devdata(master);
  682. struct spi_transfer *xfer;
  683. u32 remaining, len;
  684. remaining = as->current_remaining_bytes;
  685. if (remaining) {
  686. xfer = as->current_transfer;
  687. len = remaining;
  688. } else {
  689. if (!as->current_transfer)
  690. xfer = list_entry(msg->transfers.next,
  691. struct spi_transfer, transfer_list);
  692. else
  693. xfer = list_entry(
  694. as->current_transfer->transfer_list.next,
  695. struct spi_transfer, transfer_list);
  696. as->current_transfer = xfer;
  697. len = xfer->len;
  698. }
  699. if (atmel_spi_use_dma(as, xfer)) {
  700. u32 total = len;
  701. if (!atmel_spi_next_xfer_dma_submit(master, xfer, &len)) {
  702. as->current_remaining_bytes = total - len;
  703. return;
  704. } else {
  705. dev_err(&msg->spi->dev, "unable to use DMA, fallback to PIO\n");
  706. }
  707. }
  708. /* use PIO if error appened using DMA */
  709. atmel_spi_next_xfer_pio(master, xfer);
  710. }
  711. static void atmel_spi_next_message(struct spi_master *master)
  712. {
  713. struct atmel_spi *as = spi_master_get_devdata(master);
  714. struct spi_message *msg;
  715. struct spi_device *spi;
  716. BUG_ON(as->current_transfer);
  717. msg = list_entry(as->queue.next, struct spi_message, queue);
  718. spi = msg->spi;
  719. dev_dbg(master->dev.parent, "start message %p for %s\n",
  720. msg, dev_name(&spi->dev));
  721. /* select chip if it's not still active */
  722. if (as->stay) {
  723. if (as->stay != spi) {
  724. cs_deactivate(as, as->stay);
  725. cs_activate(as, spi);
  726. }
  727. as->stay = NULL;
  728. } else
  729. cs_activate(as, spi);
  730. if (as->use_pdc)
  731. atmel_spi_pdc_next_xfer(master, msg);
  732. else
  733. atmel_spi_dma_next_xfer(master, msg);
  734. }
  735. /*
  736. * For DMA, tx_buf/tx_dma have the same relationship as rx_buf/rx_dma:
  737. * - The buffer is either valid for CPU access, else NULL
  738. * - If the buffer is valid, so is its DMA address
  739. *
  740. * This driver manages the dma address unless message->is_dma_mapped.
  741. */
  742. static int
  743. atmel_spi_dma_map_xfer(struct atmel_spi *as, struct spi_transfer *xfer)
  744. {
  745. struct device *dev = &as->pdev->dev;
  746. xfer->tx_dma = xfer->rx_dma = INVALID_DMA_ADDRESS;
  747. if (xfer->tx_buf) {
  748. /* tx_buf is a const void* where we need a void * for the dma
  749. * mapping */
  750. void *nonconst_tx = (void *)xfer->tx_buf;
  751. xfer->tx_dma = dma_map_single(dev,
  752. nonconst_tx, xfer->len,
  753. DMA_TO_DEVICE);
  754. if (dma_mapping_error(dev, xfer->tx_dma))
  755. return -ENOMEM;
  756. }
  757. if (xfer->rx_buf) {
  758. xfer->rx_dma = dma_map_single(dev,
  759. xfer->rx_buf, xfer->len,
  760. DMA_FROM_DEVICE);
  761. if (dma_mapping_error(dev, xfer->rx_dma)) {
  762. if (xfer->tx_buf)
  763. dma_unmap_single(dev,
  764. xfer->tx_dma, xfer->len,
  765. DMA_TO_DEVICE);
  766. return -ENOMEM;
  767. }
  768. }
  769. return 0;
  770. }
  771. static void atmel_spi_dma_unmap_xfer(struct spi_master *master,
  772. struct spi_transfer *xfer)
  773. {
  774. if (xfer->tx_dma != INVALID_DMA_ADDRESS)
  775. dma_unmap_single(master->dev.parent, xfer->tx_dma,
  776. xfer->len, DMA_TO_DEVICE);
  777. if (xfer->rx_dma != INVALID_DMA_ADDRESS)
  778. dma_unmap_single(master->dev.parent, xfer->rx_dma,
  779. xfer->len, DMA_FROM_DEVICE);
  780. }
  781. static void atmel_spi_disable_pdc_transfer(struct atmel_spi *as)
  782. {
  783. spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
  784. }
  785. static void
  786. atmel_spi_msg_done(struct spi_master *master, struct atmel_spi *as,
  787. struct spi_message *msg, int stay)
  788. {
  789. if (!stay || as->done_status < 0)
  790. cs_deactivate(as, msg->spi);
  791. else
  792. as->stay = msg->spi;
  793. list_del(&msg->queue);
  794. msg->status = as->done_status;
  795. dev_dbg(master->dev.parent,
  796. "xfer complete: %u bytes transferred\n",
  797. msg->actual_length);
  798. atmel_spi_unlock(as);
  799. msg->complete(msg->context);
  800. atmel_spi_lock(as);
  801. as->current_transfer = NULL;
  802. as->next_transfer = NULL;
  803. as->done_status = 0;
  804. /* continue if needed */
  805. if (list_empty(&as->queue) || as->stopping) {
  806. if (as->use_pdc)
  807. atmel_spi_disable_pdc_transfer(as);
  808. } else {
  809. atmel_spi_next_message(master);
  810. }
  811. }
  812. /* Called from IRQ
  813. * lock is held
  814. *
  815. * Must update "current_remaining_bytes" to keep track of data
  816. * to transfer.
  817. */
  818. static void
  819. atmel_spi_pump_pio_data(struct atmel_spi *as, struct spi_transfer *xfer)
  820. {
  821. u8 *txp;
  822. u8 *rxp;
  823. unsigned long xfer_pos = xfer->len - as->current_remaining_bytes;
  824. if (xfer->rx_buf) {
  825. rxp = ((u8 *)xfer->rx_buf) + xfer_pos;
  826. *rxp = spi_readl(as, RDR);
  827. } else {
  828. spi_readl(as, RDR);
  829. }
  830. as->current_remaining_bytes--;
  831. if (as->current_remaining_bytes) {
  832. if (xfer->tx_buf) {
  833. txp = ((u8 *)xfer->tx_buf) + xfer_pos + 1;
  834. spi_writel(as, TDR, *txp);
  835. } else {
  836. spi_writel(as, TDR, 0);
  837. }
  838. }
  839. }
  840. /* Tasklet
  841. * Called from DMA callback + pio transfer and overrun IRQ.
  842. */
  843. static void atmel_spi_tasklet_func(unsigned long data)
  844. {
  845. struct spi_master *master = (struct spi_master *)data;
  846. struct atmel_spi *as = spi_master_get_devdata(master);
  847. struct spi_message *msg;
  848. struct spi_transfer *xfer;
  849. dev_vdbg(master->dev.parent, "atmel_spi_tasklet_func\n");
  850. atmel_spi_lock(as);
  851. xfer = as->current_transfer;
  852. if (xfer == NULL)
  853. /* already been there */
  854. goto tasklet_out;
  855. msg = list_entry(as->queue.next, struct spi_message, queue);
  856. if (as->current_remaining_bytes == 0) {
  857. if (as->done_status < 0) {
  858. /* error happened (overrun) */
  859. if (atmel_spi_use_dma(as, xfer))
  860. atmel_spi_stop_dma(as);
  861. } else {
  862. /* only update length if no error */
  863. msg->actual_length += xfer->len;
  864. }
  865. if (atmel_spi_use_dma(as, xfer))
  866. if (!msg->is_dma_mapped)
  867. atmel_spi_dma_unmap_xfer(master, xfer);
  868. if (xfer->delay_usecs)
  869. udelay(xfer->delay_usecs);
  870. if (atmel_spi_xfer_is_last(msg, xfer) || as->done_status < 0) {
  871. /* report completed (or erroneous) message */
  872. atmel_spi_msg_done(master, as, msg, xfer->cs_change);
  873. } else {
  874. if (xfer->cs_change) {
  875. cs_deactivate(as, msg->spi);
  876. udelay(1);
  877. cs_activate(as, msg->spi);
  878. }
  879. /*
  880. * Not done yet. Submit the next transfer.
  881. *
  882. * FIXME handle protocol options for xfer
  883. */
  884. atmel_spi_dma_next_xfer(master, msg);
  885. }
  886. } else {
  887. /*
  888. * Keep going, we still have data to send in
  889. * the current transfer.
  890. */
  891. atmel_spi_dma_next_xfer(master, msg);
  892. }
  893. tasklet_out:
  894. atmel_spi_unlock(as);
  895. }
  896. /* Interrupt
  897. *
  898. * No need for locking in this Interrupt handler: done_status is the
  899. * only information modified. What we need is the update of this field
  900. * before tasklet runs. This is ensured by using barrier.
  901. */
  902. static irqreturn_t
  903. atmel_spi_pio_interrupt(int irq, void *dev_id)
  904. {
  905. struct spi_master *master = dev_id;
  906. struct atmel_spi *as = spi_master_get_devdata(master);
  907. u32 status, pending, imr;
  908. struct spi_transfer *xfer;
  909. int ret = IRQ_NONE;
  910. imr = spi_readl(as, IMR);
  911. status = spi_readl(as, SR);
  912. pending = status & imr;
  913. if (pending & SPI_BIT(OVRES)) {
  914. ret = IRQ_HANDLED;
  915. spi_writel(as, IDR, SPI_BIT(OVRES));
  916. dev_warn(master->dev.parent, "overrun\n");
  917. /*
  918. * When we get an overrun, we disregard the current
  919. * transfer. Data will not be copied back from any
  920. * bounce buffer and msg->actual_len will not be
  921. * updated with the last xfer.
  922. *
  923. * We will also not process any remaning transfers in
  924. * the message.
  925. *
  926. * All actions are done in tasklet with done_status indication
  927. */
  928. as->done_status = -EIO;
  929. smp_wmb();
  930. /* Clear any overrun happening while cleaning up */
  931. spi_readl(as, SR);
  932. tasklet_schedule(&as->tasklet);
  933. } else if (pending & SPI_BIT(RDRF)) {
  934. atmel_spi_lock(as);
  935. if (as->current_remaining_bytes) {
  936. ret = IRQ_HANDLED;
  937. xfer = as->current_transfer;
  938. atmel_spi_pump_pio_data(as, xfer);
  939. if (!as->current_remaining_bytes) {
  940. /* no more data to xfer, kick tasklet */
  941. spi_writel(as, IDR, pending);
  942. tasklet_schedule(&as->tasklet);
  943. }
  944. }
  945. atmel_spi_unlock(as);
  946. } else {
  947. WARN_ONCE(pending, "IRQ not handled, pending = %x\n", pending);
  948. ret = IRQ_HANDLED;
  949. spi_writel(as, IDR, pending);
  950. }
  951. return ret;
  952. }
  953. static irqreturn_t
  954. atmel_spi_pdc_interrupt(int irq, void *dev_id)
  955. {
  956. struct spi_master *master = dev_id;
  957. struct atmel_spi *as = spi_master_get_devdata(master);
  958. struct spi_message *msg;
  959. struct spi_transfer *xfer;
  960. u32 status, pending, imr;
  961. int ret = IRQ_NONE;
  962. atmel_spi_lock(as);
  963. xfer = as->current_transfer;
  964. msg = list_entry(as->queue.next, struct spi_message, queue);
  965. imr = spi_readl(as, IMR);
  966. status = spi_readl(as, SR);
  967. pending = status & imr;
  968. if (pending & SPI_BIT(OVRES)) {
  969. int timeout;
  970. ret = IRQ_HANDLED;
  971. spi_writel(as, IDR, (SPI_BIT(RXBUFF) | SPI_BIT(ENDRX)
  972. | SPI_BIT(OVRES)));
  973. /*
  974. * When we get an overrun, we disregard the current
  975. * transfer. Data will not be copied back from any
  976. * bounce buffer and msg->actual_len will not be
  977. * updated with the last xfer.
  978. *
  979. * We will also not process any remaning transfers in
  980. * the message.
  981. *
  982. * First, stop the transfer and unmap the DMA buffers.
  983. */
  984. spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
  985. if (!msg->is_dma_mapped)
  986. atmel_spi_dma_unmap_xfer(master, xfer);
  987. /* REVISIT: udelay in irq is unfriendly */
  988. if (xfer->delay_usecs)
  989. udelay(xfer->delay_usecs);
  990. dev_warn(master->dev.parent, "overrun (%u/%u remaining)\n",
  991. spi_readl(as, TCR), spi_readl(as, RCR));
  992. /*
  993. * Clean up DMA registers and make sure the data
  994. * registers are empty.
  995. */
  996. spi_writel(as, RNCR, 0);
  997. spi_writel(as, TNCR, 0);
  998. spi_writel(as, RCR, 0);
  999. spi_writel(as, TCR, 0);
  1000. for (timeout = 1000; timeout; timeout--)
  1001. if (spi_readl(as, SR) & SPI_BIT(TXEMPTY))
  1002. break;
  1003. if (!timeout)
  1004. dev_warn(master->dev.parent,
  1005. "timeout waiting for TXEMPTY");
  1006. while (spi_readl(as, SR) & SPI_BIT(RDRF))
  1007. spi_readl(as, RDR);
  1008. /* Clear any overrun happening while cleaning up */
  1009. spi_readl(as, SR);
  1010. as->done_status = -EIO;
  1011. atmel_spi_msg_done(master, as, msg, 0);
  1012. } else if (pending & (SPI_BIT(RXBUFF) | SPI_BIT(ENDRX))) {
  1013. ret = IRQ_HANDLED;
  1014. spi_writel(as, IDR, pending);
  1015. if (as->current_remaining_bytes == 0) {
  1016. msg->actual_length += xfer->len;
  1017. if (!msg->is_dma_mapped)
  1018. atmel_spi_dma_unmap_xfer(master, xfer);
  1019. /* REVISIT: udelay in irq is unfriendly */
  1020. if (xfer->delay_usecs)
  1021. udelay(xfer->delay_usecs);
  1022. if (atmel_spi_xfer_is_last(msg, xfer)) {
  1023. /* report completed message */
  1024. atmel_spi_msg_done(master, as, msg,
  1025. xfer->cs_change);
  1026. } else {
  1027. if (xfer->cs_change) {
  1028. cs_deactivate(as, msg->spi);
  1029. udelay(1);
  1030. cs_activate(as, msg->spi);
  1031. }
  1032. /*
  1033. * Not done yet. Submit the next transfer.
  1034. *
  1035. * FIXME handle protocol options for xfer
  1036. */
  1037. atmel_spi_pdc_next_xfer(master, msg);
  1038. }
  1039. } else {
  1040. /*
  1041. * Keep going, we still have data to send in
  1042. * the current transfer.
  1043. */
  1044. atmel_spi_pdc_next_xfer(master, msg);
  1045. }
  1046. }
  1047. atmel_spi_unlock(as);
  1048. return ret;
  1049. }
  1050. static int atmel_spi_setup(struct spi_device *spi)
  1051. {
  1052. struct atmel_spi *as;
  1053. struct atmel_spi_device *asd;
  1054. u32 scbr, csr;
  1055. unsigned int bits = spi->bits_per_word;
  1056. unsigned long bus_hz;
  1057. unsigned int npcs_pin;
  1058. int ret;
  1059. as = spi_master_get_devdata(spi->master);
  1060. if (as->stopping)
  1061. return -ESHUTDOWN;
  1062. if (spi->chip_select > spi->master->num_chipselect) {
  1063. dev_dbg(&spi->dev,
  1064. "setup: invalid chipselect %u (%u defined)\n",
  1065. spi->chip_select, spi->master->num_chipselect);
  1066. return -EINVAL;
  1067. }
  1068. if (bits < 8 || bits > 16) {
  1069. dev_dbg(&spi->dev,
  1070. "setup: invalid bits_per_word %u (8 to 16)\n",
  1071. bits);
  1072. return -EINVAL;
  1073. }
  1074. /* see notes above re chipselect */
  1075. if (!atmel_spi_is_v2(as)
  1076. && spi->chip_select == 0
  1077. && (spi->mode & SPI_CS_HIGH)) {
  1078. dev_dbg(&spi->dev, "setup: can't be active-high\n");
  1079. return -EINVAL;
  1080. }
  1081. /* v1 chips start out at half the peripheral bus speed. */
  1082. bus_hz = clk_get_rate(as->clk);
  1083. if (!atmel_spi_is_v2(as))
  1084. bus_hz /= 2;
  1085. if (spi->max_speed_hz) {
  1086. /*
  1087. * Calculate the lowest divider that satisfies the
  1088. * constraint, assuming div32/fdiv/mbz == 0.
  1089. */
  1090. scbr = DIV_ROUND_UP(bus_hz, spi->max_speed_hz);
  1091. /*
  1092. * If the resulting divider doesn't fit into the
  1093. * register bitfield, we can't satisfy the constraint.
  1094. */
  1095. if (scbr >= (1 << SPI_SCBR_SIZE)) {
  1096. dev_dbg(&spi->dev,
  1097. "setup: %d Hz too slow, scbr %u; min %ld Hz\n",
  1098. spi->max_speed_hz, scbr, bus_hz/255);
  1099. return -EINVAL;
  1100. }
  1101. } else
  1102. /* speed zero means "as slow as possible" */
  1103. scbr = 0xff;
  1104. csr = SPI_BF(SCBR, scbr) | SPI_BF(BITS, bits - 8);
  1105. if (spi->mode & SPI_CPOL)
  1106. csr |= SPI_BIT(CPOL);
  1107. if (!(spi->mode & SPI_CPHA))
  1108. csr |= SPI_BIT(NCPHA);
  1109. /* DLYBS is mostly irrelevant since we manage chipselect using GPIOs.
  1110. *
  1111. * DLYBCT would add delays between words, slowing down transfers.
  1112. * It could potentially be useful to cope with DMA bottlenecks, but
  1113. * in those cases it's probably best to just use a lower bitrate.
  1114. */
  1115. csr |= SPI_BF(DLYBS, 0);
  1116. csr |= SPI_BF(DLYBCT, 0);
  1117. /* chipselect must have been muxed as GPIO (e.g. in board setup) */
  1118. npcs_pin = (unsigned int)spi->controller_data;
  1119. if (gpio_is_valid(spi->cs_gpio))
  1120. npcs_pin = spi->cs_gpio;
  1121. asd = spi->controller_state;
  1122. if (!asd) {
  1123. asd = kzalloc(sizeof(struct atmel_spi_device), GFP_KERNEL);
  1124. if (!asd)
  1125. return -ENOMEM;
  1126. ret = gpio_request(npcs_pin, dev_name(&spi->dev));
  1127. if (ret) {
  1128. kfree(asd);
  1129. return ret;
  1130. }
  1131. asd->npcs_pin = npcs_pin;
  1132. spi->controller_state = asd;
  1133. gpio_direction_output(npcs_pin, !(spi->mode & SPI_CS_HIGH));
  1134. } else {
  1135. atmel_spi_lock(as);
  1136. if (as->stay == spi)
  1137. as->stay = NULL;
  1138. cs_deactivate(as, spi);
  1139. atmel_spi_unlock(as);
  1140. }
  1141. asd->csr = csr;
  1142. dev_dbg(&spi->dev,
  1143. "setup: %lu Hz bpw %u mode 0x%x -> csr%d %08x\n",
  1144. bus_hz / scbr, bits, spi->mode, spi->chip_select, csr);
  1145. if (!atmel_spi_is_v2(as))
  1146. spi_writel(as, CSR0 + 4 * spi->chip_select, csr);
  1147. return 0;
  1148. }
  1149. static int atmel_spi_transfer(struct spi_device *spi, struct spi_message *msg)
  1150. {
  1151. struct atmel_spi *as;
  1152. struct spi_transfer *xfer;
  1153. struct device *controller = spi->master->dev.parent;
  1154. u8 bits;
  1155. struct atmel_spi_device *asd;
  1156. as = spi_master_get_devdata(spi->master);
  1157. dev_dbg(controller, "new message %p submitted for %s\n",
  1158. msg, dev_name(&spi->dev));
  1159. if (unlikely(list_empty(&msg->transfers)))
  1160. return -EINVAL;
  1161. if (as->stopping)
  1162. return -ESHUTDOWN;
  1163. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  1164. if (!(xfer->tx_buf || xfer->rx_buf) && xfer->len) {
  1165. dev_dbg(&spi->dev, "missing rx or tx buf\n");
  1166. return -EINVAL;
  1167. }
  1168. if (xfer->bits_per_word) {
  1169. asd = spi->controller_state;
  1170. bits = (asd->csr >> 4) & 0xf;
  1171. if (bits != xfer->bits_per_word - 8) {
  1172. dev_dbg(&spi->dev, "you can't yet change "
  1173. "bits_per_word in transfers\n");
  1174. return -ENOPROTOOPT;
  1175. }
  1176. }
  1177. /* FIXME implement these protocol options!! */
  1178. if (xfer->speed_hz) {
  1179. dev_dbg(&spi->dev, "no protocol options yet\n");
  1180. return -ENOPROTOOPT;
  1181. }
  1182. /*
  1183. * DMA map early, for performance (empties dcache ASAP) and
  1184. * better fault reporting.
  1185. */
  1186. if ((!msg->is_dma_mapped) && (atmel_spi_use_dma(as, xfer)
  1187. || as->use_pdc)) {
  1188. if (atmel_spi_dma_map_xfer(as, xfer) < 0)
  1189. return -ENOMEM;
  1190. }
  1191. }
  1192. #ifdef VERBOSE
  1193. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  1194. dev_dbg(controller,
  1195. " xfer %p: len %u tx %p/%08x rx %p/%08x\n",
  1196. xfer, xfer->len,
  1197. xfer->tx_buf, xfer->tx_dma,
  1198. xfer->rx_buf, xfer->rx_dma);
  1199. }
  1200. #endif
  1201. msg->status = -EINPROGRESS;
  1202. msg->actual_length = 0;
  1203. atmel_spi_lock(as);
  1204. list_add_tail(&msg->queue, &as->queue);
  1205. if (!as->current_transfer)
  1206. atmel_spi_next_message(spi->master);
  1207. atmel_spi_unlock(as);
  1208. return 0;
  1209. }
  1210. static void atmel_spi_cleanup(struct spi_device *spi)
  1211. {
  1212. struct atmel_spi *as = spi_master_get_devdata(spi->master);
  1213. struct atmel_spi_device *asd = spi->controller_state;
  1214. unsigned gpio = (unsigned) spi->controller_data;
  1215. if (!asd)
  1216. return;
  1217. atmel_spi_lock(as);
  1218. if (as->stay == spi) {
  1219. as->stay = NULL;
  1220. cs_deactivate(as, spi);
  1221. }
  1222. atmel_spi_unlock(as);
  1223. spi->controller_state = NULL;
  1224. gpio_free(gpio);
  1225. kfree(asd);
  1226. }
  1227. static inline unsigned int atmel_get_version(struct atmel_spi *as)
  1228. {
  1229. return spi_readl(as, VERSION) & 0x00000fff;
  1230. }
  1231. static void atmel_get_caps(struct atmel_spi *as)
  1232. {
  1233. unsigned int version;
  1234. version = atmel_get_version(as);
  1235. dev_info(&as->pdev->dev, "version: 0x%x\n", version);
  1236. as->caps.is_spi2 = version > 0x121;
  1237. as->caps.has_wdrbt = version >= 0x210;
  1238. as->caps.has_dma_support = version >= 0x212;
  1239. }
  1240. /*-------------------------------------------------------------------------*/
  1241. static int atmel_spi_probe(struct platform_device *pdev)
  1242. {
  1243. struct resource *regs;
  1244. int irq;
  1245. struct clk *clk;
  1246. int ret;
  1247. struct spi_master *master;
  1248. struct atmel_spi *as;
  1249. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1250. if (!regs)
  1251. return -ENXIO;
  1252. irq = platform_get_irq(pdev, 0);
  1253. if (irq < 0)
  1254. return irq;
  1255. clk = clk_get(&pdev->dev, "spi_clk");
  1256. if (IS_ERR(clk))
  1257. return PTR_ERR(clk);
  1258. /* setup spi core then atmel-specific driver state */
  1259. ret = -ENOMEM;
  1260. master = spi_alloc_master(&pdev->dev, sizeof *as);
  1261. if (!master)
  1262. goto out_free;
  1263. /* the spi->mode bits understood by this driver: */
  1264. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  1265. master->dev.of_node = pdev->dev.of_node;
  1266. master->bus_num = pdev->id;
  1267. master->num_chipselect = master->dev.of_node ? 0 : 4;
  1268. master->setup = atmel_spi_setup;
  1269. master->transfer = atmel_spi_transfer;
  1270. master->cleanup = atmel_spi_cleanup;
  1271. platform_set_drvdata(pdev, master);
  1272. as = spi_master_get_devdata(master);
  1273. /*
  1274. * Scratch buffer is used for throwaway rx and tx data.
  1275. * It's coherent to minimize dcache pollution.
  1276. */
  1277. as->buffer = dma_alloc_coherent(&pdev->dev, BUFFER_SIZE,
  1278. &as->buffer_dma, GFP_KERNEL);
  1279. if (!as->buffer)
  1280. goto out_free;
  1281. spin_lock_init(&as->lock);
  1282. INIT_LIST_HEAD(&as->queue);
  1283. as->pdev = pdev;
  1284. as->regs = ioremap(regs->start, resource_size(regs));
  1285. if (!as->regs)
  1286. goto out_free_buffer;
  1287. as->phybase = regs->start;
  1288. as->irq = irq;
  1289. as->clk = clk;
  1290. atmel_get_caps(as);
  1291. as->use_dma = false;
  1292. as->use_pdc = false;
  1293. if (as->caps.has_dma_support) {
  1294. if (atmel_spi_configure_dma(as) == 0)
  1295. as->use_dma = true;
  1296. } else {
  1297. as->use_pdc = true;
  1298. }
  1299. if (as->caps.has_dma_support && !as->use_dma)
  1300. dev_info(&pdev->dev, "Atmel SPI Controller using PIO only\n");
  1301. if (as->use_pdc) {
  1302. ret = request_irq(irq, atmel_spi_pdc_interrupt, 0,
  1303. dev_name(&pdev->dev), master);
  1304. } else {
  1305. tasklet_init(&as->tasklet, atmel_spi_tasklet_func,
  1306. (unsigned long)master);
  1307. ret = request_irq(irq, atmel_spi_pio_interrupt, 0,
  1308. dev_name(&pdev->dev), master);
  1309. }
  1310. if (ret)
  1311. goto out_unmap_regs;
  1312. /* Initialize the hardware */
  1313. clk_enable(clk);
  1314. spi_writel(as, CR, SPI_BIT(SWRST));
  1315. spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */
  1316. if (as->caps.has_wdrbt) {
  1317. spi_writel(as, MR, SPI_BIT(WDRBT) | SPI_BIT(MODFDIS)
  1318. | SPI_BIT(MSTR));
  1319. } else {
  1320. spi_writel(as, MR, SPI_BIT(MSTR) | SPI_BIT(MODFDIS));
  1321. }
  1322. if (as->use_pdc)
  1323. spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
  1324. spi_writel(as, CR, SPI_BIT(SPIEN));
  1325. /* go! */
  1326. dev_info(&pdev->dev, "Atmel SPI Controller at 0x%08lx (irq %d)\n",
  1327. (unsigned long)regs->start, irq);
  1328. ret = spi_register_master(master);
  1329. if (ret)
  1330. goto out_free_dma;
  1331. return 0;
  1332. out_free_dma:
  1333. if (as->use_dma)
  1334. atmel_spi_release_dma(as);
  1335. spi_writel(as, CR, SPI_BIT(SWRST));
  1336. spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */
  1337. clk_disable(clk);
  1338. free_irq(irq, master);
  1339. out_unmap_regs:
  1340. iounmap(as->regs);
  1341. out_free_buffer:
  1342. if (!as->use_pdc)
  1343. tasklet_kill(&as->tasklet);
  1344. dma_free_coherent(&pdev->dev, BUFFER_SIZE, as->buffer,
  1345. as->buffer_dma);
  1346. out_free:
  1347. clk_put(clk);
  1348. spi_master_put(master);
  1349. return ret;
  1350. }
  1351. static int atmel_spi_remove(struct platform_device *pdev)
  1352. {
  1353. struct spi_master *master = platform_get_drvdata(pdev);
  1354. struct atmel_spi *as = spi_master_get_devdata(master);
  1355. struct spi_message *msg;
  1356. struct spi_transfer *xfer;
  1357. /* reset the hardware and block queue progress */
  1358. spin_lock_irq(&as->lock);
  1359. as->stopping = 1;
  1360. if (as->use_dma) {
  1361. atmel_spi_stop_dma(as);
  1362. atmel_spi_release_dma(as);
  1363. }
  1364. spi_writel(as, CR, SPI_BIT(SWRST));
  1365. spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */
  1366. spi_readl(as, SR);
  1367. spin_unlock_irq(&as->lock);
  1368. /* Terminate remaining queued transfers */
  1369. list_for_each_entry(msg, &as->queue, queue) {
  1370. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  1371. if (!msg->is_dma_mapped
  1372. && (atmel_spi_use_dma(as, xfer)
  1373. || as->use_pdc))
  1374. atmel_spi_dma_unmap_xfer(master, xfer);
  1375. }
  1376. msg->status = -ESHUTDOWN;
  1377. msg->complete(msg->context);
  1378. }
  1379. if (!as->use_pdc)
  1380. tasklet_kill(&as->tasklet);
  1381. dma_free_coherent(&pdev->dev, BUFFER_SIZE, as->buffer,
  1382. as->buffer_dma);
  1383. clk_disable(as->clk);
  1384. clk_put(as->clk);
  1385. free_irq(as->irq, master);
  1386. iounmap(as->regs);
  1387. spi_unregister_master(master);
  1388. return 0;
  1389. }
  1390. #ifdef CONFIG_PM
  1391. static int atmel_spi_suspend(struct platform_device *pdev, pm_message_t mesg)
  1392. {
  1393. struct spi_master *master = platform_get_drvdata(pdev);
  1394. struct atmel_spi *as = spi_master_get_devdata(master);
  1395. clk_disable(as->clk);
  1396. return 0;
  1397. }
  1398. static int atmel_spi_resume(struct platform_device *pdev)
  1399. {
  1400. struct spi_master *master = platform_get_drvdata(pdev);
  1401. struct atmel_spi *as = spi_master_get_devdata(master);
  1402. clk_enable(as->clk);
  1403. return 0;
  1404. }
  1405. #else
  1406. #define atmel_spi_suspend NULL
  1407. #define atmel_spi_resume NULL
  1408. #endif
  1409. #if defined(CONFIG_OF)
  1410. static const struct of_device_id atmel_spi_dt_ids[] = {
  1411. { .compatible = "atmel,at91rm9200-spi" },
  1412. { /* sentinel */ }
  1413. };
  1414. MODULE_DEVICE_TABLE(of, atmel_spi_dt_ids);
  1415. #endif
  1416. static struct platform_driver atmel_spi_driver = {
  1417. .driver = {
  1418. .name = "atmel_spi",
  1419. .owner = THIS_MODULE,
  1420. .of_match_table = of_match_ptr(atmel_spi_dt_ids),
  1421. },
  1422. .suspend = atmel_spi_suspend,
  1423. .resume = atmel_spi_resume,
  1424. .probe = atmel_spi_probe,
  1425. .remove = atmel_spi_remove,
  1426. };
  1427. module_platform_driver(atmel_spi_driver);
  1428. MODULE_DESCRIPTION("Atmel AT32/AT91 SPI Controller driver");
  1429. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  1430. MODULE_LICENSE("GPL");
  1431. MODULE_ALIAS("platform:atmel_spi");