spi-atmel.c 38 KB

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