spi-atmel.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537
  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. #define spi_readl(port, reg) \
  167. __raw_readl((port)->regs + SPI_##reg)
  168. #define spi_writel(port, reg, value) \
  169. __raw_writel((value), (port)->regs + SPI_##reg)
  170. /* use PIO for small transfers, avoiding DMA setup/teardown overhead and
  171. * cache operations; better heuristics consider wordsize and bitrate.
  172. */
  173. #define DMA_MIN_BYTES 16
  174. #define SPI_DMA_TIMEOUT (msecs_to_jiffies(1000))
  175. #define AUTOSUSPEND_TIMEOUT 2000
  176. struct atmel_spi_dma {
  177. struct dma_chan *chan_rx;
  178. struct dma_chan *chan_tx;
  179. struct scatterlist sgrx;
  180. struct scatterlist sgtx;
  181. struct dma_async_tx_descriptor *data_desc_rx;
  182. struct dma_async_tx_descriptor *data_desc_tx;
  183. struct at_dma_slave dma_slave;
  184. };
  185. struct atmel_spi_caps {
  186. bool is_spi2;
  187. bool has_wdrbt;
  188. bool has_dma_support;
  189. };
  190. /*
  191. * The core SPI transfer engine just talks to a register bank to set up
  192. * DMA transfers; transfer queue progress is driven by IRQs. The clock
  193. * framework provides the base clock, subdivided for each spi_device.
  194. */
  195. struct atmel_spi {
  196. spinlock_t lock;
  197. unsigned long flags;
  198. phys_addr_t phybase;
  199. void __iomem *regs;
  200. int irq;
  201. struct clk *clk;
  202. struct platform_device *pdev;
  203. struct spi_transfer *current_transfer;
  204. int current_remaining_bytes;
  205. int done_status;
  206. struct completion xfer_completion;
  207. /* scratch buffer */
  208. void *buffer;
  209. dma_addr_t buffer_dma;
  210. struct atmel_spi_caps caps;
  211. bool use_dma;
  212. bool use_pdc;
  213. /* dmaengine data */
  214. struct atmel_spi_dma dma;
  215. bool keep_cs;
  216. bool cs_active;
  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) __acquires(&as->lock)
  322. {
  323. spin_lock_irqsave(&as->lock, as->flags);
  324. }
  325. static void atmel_spi_unlock(struct atmel_spi *as) __releases(&as->lock)
  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 int atmel_spi_dma_slave_config(struct atmel_spi *as,
  335. struct dma_slave_config *slave_config,
  336. u8 bits_per_word)
  337. {
  338. int err = 0;
  339. if (bits_per_word > 8) {
  340. slave_config->dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  341. slave_config->src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  342. } else {
  343. slave_config->dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  344. slave_config->src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  345. }
  346. slave_config->dst_addr = (dma_addr_t)as->phybase + SPI_TDR;
  347. slave_config->src_addr = (dma_addr_t)as->phybase + SPI_RDR;
  348. slave_config->src_maxburst = 1;
  349. slave_config->dst_maxburst = 1;
  350. slave_config->device_fc = false;
  351. slave_config->direction = DMA_MEM_TO_DEV;
  352. if (dmaengine_slave_config(as->dma.chan_tx, slave_config)) {
  353. dev_err(&as->pdev->dev,
  354. "failed to configure tx dma channel\n");
  355. err = -EINVAL;
  356. }
  357. slave_config->direction = DMA_DEV_TO_MEM;
  358. if (dmaengine_slave_config(as->dma.chan_rx, slave_config)) {
  359. dev_err(&as->pdev->dev,
  360. "failed to configure rx dma channel\n");
  361. err = -EINVAL;
  362. }
  363. return err;
  364. }
  365. static int atmel_spi_configure_dma(struct atmel_spi *as)
  366. {
  367. struct dma_slave_config slave_config;
  368. struct device *dev = &as->pdev->dev;
  369. int err;
  370. dma_cap_mask_t mask;
  371. dma_cap_zero(mask);
  372. dma_cap_set(DMA_SLAVE, mask);
  373. as->dma.chan_tx = dma_request_slave_channel_reason(dev, "tx");
  374. if (IS_ERR(as->dma.chan_tx)) {
  375. err = PTR_ERR(as->dma.chan_tx);
  376. if (err == -EPROBE_DEFER) {
  377. dev_warn(dev, "no DMA channel available at the moment\n");
  378. return err;
  379. }
  380. dev_err(dev,
  381. "DMA TX channel not available, SPI unable to use DMA\n");
  382. err = -EBUSY;
  383. goto error;
  384. }
  385. /*
  386. * No reason to check EPROBE_DEFER here since we have already requested
  387. * tx channel. If it fails here, it's for another reason.
  388. */
  389. as->dma.chan_rx = dma_request_slave_channel(dev, "rx");
  390. if (!as->dma.chan_rx) {
  391. dev_err(dev,
  392. "DMA RX channel not available, SPI unable to use DMA\n");
  393. err = -EBUSY;
  394. goto error;
  395. }
  396. err = atmel_spi_dma_slave_config(as, &slave_config, 8);
  397. if (err)
  398. goto error;
  399. dev_info(&as->pdev->dev,
  400. "Using %s (tx) and %s (rx) for DMA transfers\n",
  401. dma_chan_name(as->dma.chan_tx),
  402. dma_chan_name(as->dma.chan_rx));
  403. return 0;
  404. error:
  405. if (as->dma.chan_rx)
  406. dma_release_channel(as->dma.chan_rx);
  407. if (!IS_ERR(as->dma.chan_tx))
  408. dma_release_channel(as->dma.chan_tx);
  409. return err;
  410. }
  411. static void atmel_spi_stop_dma(struct atmel_spi *as)
  412. {
  413. if (as->dma.chan_rx)
  414. dmaengine_terminate_all(as->dma.chan_rx);
  415. if (as->dma.chan_tx)
  416. dmaengine_terminate_all(as->dma.chan_tx);
  417. }
  418. static void atmel_spi_release_dma(struct atmel_spi *as)
  419. {
  420. if (as->dma.chan_rx)
  421. dma_release_channel(as->dma.chan_rx);
  422. if (as->dma.chan_tx)
  423. dma_release_channel(as->dma.chan_tx);
  424. }
  425. /* This function is called by the DMA driver from tasklet context */
  426. static void dma_callback(void *data)
  427. {
  428. struct spi_master *master = data;
  429. struct atmel_spi *as = spi_master_get_devdata(master);
  430. complete(&as->xfer_completion);
  431. }
  432. /*
  433. * Next transfer using PIO.
  434. */
  435. static void atmel_spi_next_xfer_pio(struct spi_master *master,
  436. struct spi_transfer *xfer)
  437. {
  438. struct atmel_spi *as = spi_master_get_devdata(master);
  439. unsigned long xfer_pos = xfer->len - as->current_remaining_bytes;
  440. dev_vdbg(master->dev.parent, "atmel_spi_next_xfer_pio\n");
  441. /* Make sure data is not remaining in RDR */
  442. spi_readl(as, RDR);
  443. while (spi_readl(as, SR) & SPI_BIT(RDRF)) {
  444. spi_readl(as, RDR);
  445. cpu_relax();
  446. }
  447. if (xfer->tx_buf) {
  448. if (xfer->bits_per_word > 8)
  449. spi_writel(as, TDR, *(u16 *)(xfer->tx_buf + xfer_pos));
  450. else
  451. spi_writel(as, TDR, *(u8 *)(xfer->tx_buf + xfer_pos));
  452. } else {
  453. spi_writel(as, TDR, 0);
  454. }
  455. dev_dbg(master->dev.parent,
  456. " start pio xfer %p: len %u tx %p rx %p bitpw %d\n",
  457. xfer, xfer->len, xfer->tx_buf, xfer->rx_buf,
  458. xfer->bits_per_word);
  459. /* Enable relevant interrupts */
  460. spi_writel(as, IER, SPI_BIT(RDRF) | SPI_BIT(OVRES));
  461. }
  462. /*
  463. * Submit next transfer for DMA.
  464. */
  465. static int atmel_spi_next_xfer_dma_submit(struct spi_master *master,
  466. struct spi_transfer *xfer,
  467. u32 *plen)
  468. {
  469. struct atmel_spi *as = spi_master_get_devdata(master);
  470. struct dma_chan *rxchan = as->dma.chan_rx;
  471. struct dma_chan *txchan = as->dma.chan_tx;
  472. struct dma_async_tx_descriptor *rxdesc;
  473. struct dma_async_tx_descriptor *txdesc;
  474. struct dma_slave_config slave_config;
  475. dma_cookie_t cookie;
  476. u32 len = *plen;
  477. dev_vdbg(master->dev.parent, "atmel_spi_next_xfer_dma_submit\n");
  478. /* Check that the channels are available */
  479. if (!rxchan || !txchan)
  480. return -ENODEV;
  481. /* release lock for DMA operations */
  482. atmel_spi_unlock(as);
  483. /* prepare the RX dma transfer */
  484. sg_init_table(&as->dma.sgrx, 1);
  485. if (xfer->rx_buf) {
  486. as->dma.sgrx.dma_address = xfer->rx_dma + xfer->len - *plen;
  487. } else {
  488. as->dma.sgrx.dma_address = as->buffer_dma;
  489. if (len > BUFFER_SIZE)
  490. len = BUFFER_SIZE;
  491. }
  492. /* prepare the TX dma transfer */
  493. sg_init_table(&as->dma.sgtx, 1);
  494. if (xfer->tx_buf) {
  495. as->dma.sgtx.dma_address = xfer->tx_dma + xfer->len - *plen;
  496. } else {
  497. as->dma.sgtx.dma_address = as->buffer_dma;
  498. if (len > BUFFER_SIZE)
  499. len = BUFFER_SIZE;
  500. memset(as->buffer, 0, len);
  501. }
  502. sg_dma_len(&as->dma.sgtx) = len;
  503. sg_dma_len(&as->dma.sgrx) = len;
  504. *plen = len;
  505. if (atmel_spi_dma_slave_config(as, &slave_config, 8))
  506. goto err_exit;
  507. /* Send both scatterlists */
  508. rxdesc = dmaengine_prep_slave_sg(rxchan, &as->dma.sgrx, 1,
  509. DMA_FROM_DEVICE,
  510. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  511. if (!rxdesc)
  512. goto err_dma;
  513. txdesc = dmaengine_prep_slave_sg(txchan, &as->dma.sgtx, 1,
  514. DMA_TO_DEVICE,
  515. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  516. if (!txdesc)
  517. goto err_dma;
  518. dev_dbg(master->dev.parent,
  519. " start dma xfer %p: len %u tx %p/%08llx rx %p/%08llx\n",
  520. xfer, xfer->len, xfer->tx_buf, (unsigned long long)xfer->tx_dma,
  521. xfer->rx_buf, (unsigned long long)xfer->rx_dma);
  522. /* Enable relevant interrupts */
  523. spi_writel(as, IER, SPI_BIT(OVRES));
  524. /* Put the callback on the RX transfer only, that should finish last */
  525. rxdesc->callback = dma_callback;
  526. rxdesc->callback_param = master;
  527. /* Submit and fire RX and TX with TX last so we're ready to read! */
  528. cookie = rxdesc->tx_submit(rxdesc);
  529. if (dma_submit_error(cookie))
  530. goto err_dma;
  531. cookie = txdesc->tx_submit(txdesc);
  532. if (dma_submit_error(cookie))
  533. goto err_dma;
  534. rxchan->device->device_issue_pending(rxchan);
  535. txchan->device->device_issue_pending(txchan);
  536. /* take back lock */
  537. atmel_spi_lock(as);
  538. return 0;
  539. err_dma:
  540. spi_writel(as, IDR, SPI_BIT(OVRES));
  541. atmel_spi_stop_dma(as);
  542. err_exit:
  543. atmel_spi_lock(as);
  544. return -ENOMEM;
  545. }
  546. static void atmel_spi_next_xfer_data(struct spi_master *master,
  547. struct spi_transfer *xfer,
  548. dma_addr_t *tx_dma,
  549. dma_addr_t *rx_dma,
  550. u32 *plen)
  551. {
  552. struct atmel_spi *as = spi_master_get_devdata(master);
  553. u32 len = *plen;
  554. /* use scratch buffer only when rx or tx data is unspecified */
  555. if (xfer->rx_buf)
  556. *rx_dma = xfer->rx_dma + xfer->len - *plen;
  557. else {
  558. *rx_dma = as->buffer_dma;
  559. if (len > BUFFER_SIZE)
  560. len = BUFFER_SIZE;
  561. }
  562. if (xfer->tx_buf)
  563. *tx_dma = xfer->tx_dma + xfer->len - *plen;
  564. else {
  565. *tx_dma = as->buffer_dma;
  566. if (len > BUFFER_SIZE)
  567. len = BUFFER_SIZE;
  568. memset(as->buffer, 0, len);
  569. dma_sync_single_for_device(&as->pdev->dev,
  570. as->buffer_dma, len, DMA_TO_DEVICE);
  571. }
  572. *plen = len;
  573. }
  574. static int atmel_spi_set_xfer_speed(struct atmel_spi *as,
  575. struct spi_device *spi,
  576. struct spi_transfer *xfer)
  577. {
  578. u32 scbr, csr;
  579. unsigned long bus_hz;
  580. /* v1 chips start out at half the peripheral bus speed. */
  581. bus_hz = clk_get_rate(as->clk);
  582. if (!atmel_spi_is_v2(as))
  583. bus_hz /= 2;
  584. /*
  585. * Calculate the lowest divider that satisfies the
  586. * constraint, assuming div32/fdiv/mbz == 0.
  587. */
  588. if (xfer->speed_hz)
  589. scbr = DIV_ROUND_UP(bus_hz, xfer->speed_hz);
  590. else
  591. /*
  592. * This can happend if max_speed is null.
  593. * In this case, we set the lowest possible speed
  594. */
  595. scbr = 0xff;
  596. /*
  597. * If the resulting divider doesn't fit into the
  598. * register bitfield, we can't satisfy the constraint.
  599. */
  600. if (scbr >= (1 << SPI_SCBR_SIZE)) {
  601. dev_err(&spi->dev,
  602. "setup: %d Hz too slow, scbr %u; min %ld Hz\n",
  603. xfer->speed_hz, scbr, bus_hz/255);
  604. return -EINVAL;
  605. }
  606. if (scbr == 0) {
  607. dev_err(&spi->dev,
  608. "setup: %d Hz too high, scbr %u; max %ld Hz\n",
  609. xfer->speed_hz, scbr, bus_hz);
  610. return -EINVAL;
  611. }
  612. csr = spi_readl(as, CSR0 + 4 * spi->chip_select);
  613. csr = SPI_BFINS(SCBR, scbr, csr);
  614. spi_writel(as, CSR0 + 4 * spi->chip_select, csr);
  615. return 0;
  616. }
  617. /*
  618. * Submit next transfer for PDC.
  619. * lock is held, spi irq is blocked
  620. */
  621. static void atmel_spi_pdc_next_xfer(struct spi_master *master,
  622. struct spi_message *msg,
  623. struct spi_transfer *xfer)
  624. {
  625. struct atmel_spi *as = spi_master_get_devdata(master);
  626. u32 len;
  627. dma_addr_t tx_dma, rx_dma;
  628. spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
  629. len = as->current_remaining_bytes;
  630. atmel_spi_next_xfer_data(master, xfer, &tx_dma, &rx_dma, &len);
  631. as->current_remaining_bytes -= len;
  632. spi_writel(as, RPR, rx_dma);
  633. spi_writel(as, TPR, tx_dma);
  634. if (msg->spi->bits_per_word > 8)
  635. len >>= 1;
  636. spi_writel(as, RCR, len);
  637. spi_writel(as, TCR, len);
  638. dev_dbg(&msg->spi->dev,
  639. " start xfer %p: len %u tx %p/%08llx rx %p/%08llx\n",
  640. xfer, xfer->len, xfer->tx_buf,
  641. (unsigned long long)xfer->tx_dma, xfer->rx_buf,
  642. (unsigned long long)xfer->rx_dma);
  643. if (as->current_remaining_bytes) {
  644. len = as->current_remaining_bytes;
  645. atmel_spi_next_xfer_data(master, xfer, &tx_dma, &rx_dma, &len);
  646. as->current_remaining_bytes -= len;
  647. spi_writel(as, RNPR, rx_dma);
  648. spi_writel(as, TNPR, tx_dma);
  649. if (msg->spi->bits_per_word > 8)
  650. len >>= 1;
  651. spi_writel(as, RNCR, len);
  652. spi_writel(as, TNCR, len);
  653. dev_dbg(&msg->spi->dev,
  654. " next xfer %p: len %u tx %p/%08llx rx %p/%08llx\n",
  655. xfer, xfer->len, xfer->tx_buf,
  656. (unsigned long long)xfer->tx_dma, xfer->rx_buf,
  657. (unsigned long long)xfer->rx_dma);
  658. }
  659. /* REVISIT: We're waiting for ENDRX before we start the next
  660. * transfer because we need to handle some difficult timing
  661. * issues otherwise. If we wait for ENDTX in one transfer and
  662. * then starts waiting for ENDRX in the next, it's difficult
  663. * to tell the difference between the ENDRX interrupt we're
  664. * actually waiting for and the ENDRX interrupt of the
  665. * previous transfer.
  666. *
  667. * It should be doable, though. Just not now...
  668. */
  669. spi_writel(as, IER, SPI_BIT(ENDRX) | SPI_BIT(OVRES));
  670. spi_writel(as, PTCR, SPI_BIT(TXTEN) | SPI_BIT(RXTEN));
  671. }
  672. /*
  673. * For DMA, tx_buf/tx_dma have the same relationship as rx_buf/rx_dma:
  674. * - The buffer is either valid for CPU access, else NULL
  675. * - If the buffer is valid, so is its DMA address
  676. *
  677. * This driver manages the dma address unless message->is_dma_mapped.
  678. */
  679. static int
  680. atmel_spi_dma_map_xfer(struct atmel_spi *as, struct spi_transfer *xfer)
  681. {
  682. struct device *dev = &as->pdev->dev;
  683. xfer->tx_dma = xfer->rx_dma = INVALID_DMA_ADDRESS;
  684. if (xfer->tx_buf) {
  685. /* tx_buf is a const void* where we need a void * for the dma
  686. * mapping */
  687. void *nonconst_tx = (void *)xfer->tx_buf;
  688. xfer->tx_dma = dma_map_single(dev,
  689. nonconst_tx, xfer->len,
  690. DMA_TO_DEVICE);
  691. if (dma_mapping_error(dev, xfer->tx_dma))
  692. return -ENOMEM;
  693. }
  694. if (xfer->rx_buf) {
  695. xfer->rx_dma = dma_map_single(dev,
  696. xfer->rx_buf, xfer->len,
  697. DMA_FROM_DEVICE);
  698. if (dma_mapping_error(dev, xfer->rx_dma)) {
  699. if (xfer->tx_buf)
  700. dma_unmap_single(dev,
  701. xfer->tx_dma, xfer->len,
  702. DMA_TO_DEVICE);
  703. return -ENOMEM;
  704. }
  705. }
  706. return 0;
  707. }
  708. static void atmel_spi_dma_unmap_xfer(struct spi_master *master,
  709. struct spi_transfer *xfer)
  710. {
  711. if (xfer->tx_dma != INVALID_DMA_ADDRESS)
  712. dma_unmap_single(master->dev.parent, xfer->tx_dma,
  713. xfer->len, DMA_TO_DEVICE);
  714. if (xfer->rx_dma != INVALID_DMA_ADDRESS)
  715. dma_unmap_single(master->dev.parent, xfer->rx_dma,
  716. xfer->len, DMA_FROM_DEVICE);
  717. }
  718. static void atmel_spi_disable_pdc_transfer(struct atmel_spi *as)
  719. {
  720. spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
  721. }
  722. /* Called from IRQ
  723. *
  724. * Must update "current_remaining_bytes" to keep track of data
  725. * to transfer.
  726. */
  727. static void
  728. atmel_spi_pump_pio_data(struct atmel_spi *as, struct spi_transfer *xfer)
  729. {
  730. u8 *rxp;
  731. u16 *rxp16;
  732. unsigned long xfer_pos = xfer->len - as->current_remaining_bytes;
  733. if (xfer->rx_buf) {
  734. if (xfer->bits_per_word > 8) {
  735. rxp16 = (u16 *)(((u8 *)xfer->rx_buf) + xfer_pos);
  736. *rxp16 = spi_readl(as, RDR);
  737. } else {
  738. rxp = ((u8 *)xfer->rx_buf) + xfer_pos;
  739. *rxp = spi_readl(as, RDR);
  740. }
  741. } else {
  742. spi_readl(as, RDR);
  743. }
  744. if (xfer->bits_per_word > 8) {
  745. if (as->current_remaining_bytes > 2)
  746. as->current_remaining_bytes -= 2;
  747. else
  748. as->current_remaining_bytes = 0;
  749. } else {
  750. as->current_remaining_bytes--;
  751. }
  752. }
  753. /* Interrupt
  754. *
  755. * No need for locking in this Interrupt handler: done_status is the
  756. * only information modified.
  757. */
  758. static irqreturn_t
  759. atmel_spi_pio_interrupt(int irq, void *dev_id)
  760. {
  761. struct spi_master *master = dev_id;
  762. struct atmel_spi *as = spi_master_get_devdata(master);
  763. u32 status, pending, imr;
  764. struct spi_transfer *xfer;
  765. int ret = IRQ_NONE;
  766. imr = spi_readl(as, IMR);
  767. status = spi_readl(as, SR);
  768. pending = status & imr;
  769. if (pending & SPI_BIT(OVRES)) {
  770. ret = IRQ_HANDLED;
  771. spi_writel(as, IDR, SPI_BIT(OVRES));
  772. dev_warn(master->dev.parent, "overrun\n");
  773. /*
  774. * When we get an overrun, we disregard the current
  775. * transfer. Data will not be copied back from any
  776. * bounce buffer and msg->actual_len will not be
  777. * updated with the last xfer.
  778. *
  779. * We will also not process any remaning transfers in
  780. * the message.
  781. */
  782. as->done_status = -EIO;
  783. smp_wmb();
  784. /* Clear any overrun happening while cleaning up */
  785. spi_readl(as, SR);
  786. complete(&as->xfer_completion);
  787. } else if (pending & SPI_BIT(RDRF)) {
  788. atmel_spi_lock(as);
  789. if (as->current_remaining_bytes) {
  790. ret = IRQ_HANDLED;
  791. xfer = as->current_transfer;
  792. atmel_spi_pump_pio_data(as, xfer);
  793. if (!as->current_remaining_bytes)
  794. spi_writel(as, IDR, pending);
  795. complete(&as->xfer_completion);
  796. }
  797. atmel_spi_unlock(as);
  798. } else {
  799. WARN_ONCE(pending, "IRQ not handled, pending = %x\n", pending);
  800. ret = IRQ_HANDLED;
  801. spi_writel(as, IDR, pending);
  802. }
  803. return ret;
  804. }
  805. static irqreturn_t
  806. atmel_spi_pdc_interrupt(int irq, void *dev_id)
  807. {
  808. struct spi_master *master = dev_id;
  809. struct atmel_spi *as = spi_master_get_devdata(master);
  810. u32 status, pending, imr;
  811. int ret = IRQ_NONE;
  812. imr = spi_readl(as, IMR);
  813. status = spi_readl(as, SR);
  814. pending = status & imr;
  815. if (pending & SPI_BIT(OVRES)) {
  816. ret = IRQ_HANDLED;
  817. spi_writel(as, IDR, (SPI_BIT(RXBUFF) | SPI_BIT(ENDRX)
  818. | SPI_BIT(OVRES)));
  819. /* Clear any overrun happening while cleaning up */
  820. spi_readl(as, SR);
  821. as->done_status = -EIO;
  822. complete(&as->xfer_completion);
  823. } else if (pending & (SPI_BIT(RXBUFF) | SPI_BIT(ENDRX))) {
  824. ret = IRQ_HANDLED;
  825. spi_writel(as, IDR, pending);
  826. complete(&as->xfer_completion);
  827. }
  828. return ret;
  829. }
  830. static int atmel_spi_setup(struct spi_device *spi)
  831. {
  832. struct atmel_spi *as;
  833. struct atmel_spi_device *asd;
  834. u32 csr;
  835. unsigned int bits = spi->bits_per_word;
  836. unsigned int npcs_pin;
  837. int ret;
  838. as = spi_master_get_devdata(spi->master);
  839. /* see notes above re chipselect */
  840. if (!atmel_spi_is_v2(as)
  841. && spi->chip_select == 0
  842. && (spi->mode & SPI_CS_HIGH)) {
  843. dev_dbg(&spi->dev, "setup: can't be active-high\n");
  844. return -EINVAL;
  845. }
  846. csr = SPI_BF(BITS, bits - 8);
  847. if (spi->mode & SPI_CPOL)
  848. csr |= SPI_BIT(CPOL);
  849. if (!(spi->mode & SPI_CPHA))
  850. csr |= SPI_BIT(NCPHA);
  851. /* DLYBS is mostly irrelevant since we manage chipselect using GPIOs.
  852. *
  853. * DLYBCT would add delays between words, slowing down transfers.
  854. * It could potentially be useful to cope with DMA bottlenecks, but
  855. * in those cases it's probably best to just use a lower bitrate.
  856. */
  857. csr |= SPI_BF(DLYBS, 0);
  858. csr |= SPI_BF(DLYBCT, 0);
  859. /* chipselect must have been muxed as GPIO (e.g. in board setup) */
  860. npcs_pin = (unsigned long)spi->controller_data;
  861. if (gpio_is_valid(spi->cs_gpio))
  862. npcs_pin = spi->cs_gpio;
  863. asd = spi->controller_state;
  864. if (!asd) {
  865. asd = kzalloc(sizeof(struct atmel_spi_device), GFP_KERNEL);
  866. if (!asd)
  867. return -ENOMEM;
  868. ret = gpio_request(npcs_pin, dev_name(&spi->dev));
  869. if (ret) {
  870. kfree(asd);
  871. return ret;
  872. }
  873. asd->npcs_pin = npcs_pin;
  874. spi->controller_state = asd;
  875. gpio_direction_output(npcs_pin, !(spi->mode & SPI_CS_HIGH));
  876. }
  877. asd->csr = csr;
  878. dev_dbg(&spi->dev,
  879. "setup: bpw %u mode 0x%x -> csr%d %08x\n",
  880. bits, spi->mode, spi->chip_select, csr);
  881. if (!atmel_spi_is_v2(as))
  882. spi_writel(as, CSR0 + 4 * spi->chip_select, csr);
  883. return 0;
  884. }
  885. static int atmel_spi_one_transfer(struct spi_master *master,
  886. struct spi_message *msg,
  887. struct spi_transfer *xfer)
  888. {
  889. struct atmel_spi *as;
  890. struct spi_device *spi = msg->spi;
  891. u8 bits;
  892. u32 len;
  893. struct atmel_spi_device *asd;
  894. int timeout;
  895. int ret;
  896. as = spi_master_get_devdata(master);
  897. if (!(xfer->tx_buf || xfer->rx_buf) && xfer->len) {
  898. dev_dbg(&spi->dev, "missing rx or tx buf\n");
  899. return -EINVAL;
  900. }
  901. if (xfer->bits_per_word) {
  902. asd = spi->controller_state;
  903. bits = (asd->csr >> 4) & 0xf;
  904. if (bits != xfer->bits_per_word - 8) {
  905. dev_dbg(&spi->dev,
  906. "you can't yet change bits_per_word in transfers\n");
  907. return -ENOPROTOOPT;
  908. }
  909. }
  910. /*
  911. * DMA map early, for performance (empties dcache ASAP) and
  912. * better fault reporting.
  913. */
  914. if ((!msg->is_dma_mapped)
  915. && (atmel_spi_use_dma(as, xfer) || as->use_pdc)) {
  916. if (atmel_spi_dma_map_xfer(as, xfer) < 0)
  917. return -ENOMEM;
  918. }
  919. atmel_spi_set_xfer_speed(as, msg->spi, xfer);
  920. as->done_status = 0;
  921. as->current_transfer = xfer;
  922. as->current_remaining_bytes = xfer->len;
  923. while (as->current_remaining_bytes) {
  924. reinit_completion(&as->xfer_completion);
  925. if (as->use_pdc) {
  926. atmel_spi_pdc_next_xfer(master, msg, xfer);
  927. } else if (atmel_spi_use_dma(as, xfer)) {
  928. len = as->current_remaining_bytes;
  929. ret = atmel_spi_next_xfer_dma_submit(master,
  930. xfer, &len);
  931. if (ret) {
  932. dev_err(&spi->dev,
  933. "unable to use DMA, fallback to PIO\n");
  934. atmel_spi_next_xfer_pio(master, xfer);
  935. } else {
  936. as->current_remaining_bytes -= len;
  937. if (as->current_remaining_bytes < 0)
  938. as->current_remaining_bytes = 0;
  939. }
  940. } else {
  941. atmel_spi_next_xfer_pio(master, xfer);
  942. }
  943. /* interrupts are disabled, so free the lock for schedule */
  944. atmel_spi_unlock(as);
  945. ret = wait_for_completion_timeout(&as->xfer_completion,
  946. SPI_DMA_TIMEOUT);
  947. atmel_spi_lock(as);
  948. if (WARN_ON(ret == 0)) {
  949. dev_err(&spi->dev,
  950. "spi trasfer timeout, err %d\n", ret);
  951. as->done_status = -EIO;
  952. } else {
  953. ret = 0;
  954. }
  955. if (as->done_status)
  956. break;
  957. }
  958. if (as->done_status) {
  959. if (as->use_pdc) {
  960. dev_warn(master->dev.parent,
  961. "overrun (%u/%u remaining)\n",
  962. spi_readl(as, TCR), spi_readl(as, RCR));
  963. /*
  964. * Clean up DMA registers and make sure the data
  965. * registers are empty.
  966. */
  967. spi_writel(as, RNCR, 0);
  968. spi_writel(as, TNCR, 0);
  969. spi_writel(as, RCR, 0);
  970. spi_writel(as, TCR, 0);
  971. for (timeout = 1000; timeout; timeout--)
  972. if (spi_readl(as, SR) & SPI_BIT(TXEMPTY))
  973. break;
  974. if (!timeout)
  975. dev_warn(master->dev.parent,
  976. "timeout waiting for TXEMPTY");
  977. while (spi_readl(as, SR) & SPI_BIT(RDRF))
  978. spi_readl(as, RDR);
  979. /* Clear any overrun happening while cleaning up */
  980. spi_readl(as, SR);
  981. } else if (atmel_spi_use_dma(as, xfer)) {
  982. atmel_spi_stop_dma(as);
  983. }
  984. if (!msg->is_dma_mapped
  985. && (atmel_spi_use_dma(as, xfer) || as->use_pdc))
  986. atmel_spi_dma_unmap_xfer(master, xfer);
  987. return 0;
  988. } else {
  989. /* only update length if no error */
  990. msg->actual_length += xfer->len;
  991. }
  992. if (!msg->is_dma_mapped
  993. && (atmel_spi_use_dma(as, xfer) || as->use_pdc))
  994. atmel_spi_dma_unmap_xfer(master, xfer);
  995. if (xfer->delay_usecs)
  996. udelay(xfer->delay_usecs);
  997. if (xfer->cs_change) {
  998. if (list_is_last(&xfer->transfer_list,
  999. &msg->transfers)) {
  1000. as->keep_cs = true;
  1001. } else {
  1002. as->cs_active = !as->cs_active;
  1003. if (as->cs_active)
  1004. cs_activate(as, msg->spi);
  1005. else
  1006. cs_deactivate(as, msg->spi);
  1007. }
  1008. }
  1009. return 0;
  1010. }
  1011. static int atmel_spi_transfer_one_message(struct spi_master *master,
  1012. struct spi_message *msg)
  1013. {
  1014. struct atmel_spi *as;
  1015. struct spi_transfer *xfer;
  1016. struct spi_device *spi = msg->spi;
  1017. int ret = 0;
  1018. as = spi_master_get_devdata(master);
  1019. dev_dbg(&spi->dev, "new message %p submitted for %s\n",
  1020. msg, dev_name(&spi->dev));
  1021. atmel_spi_lock(as);
  1022. cs_activate(as, spi);
  1023. as->cs_active = true;
  1024. as->keep_cs = false;
  1025. msg->status = 0;
  1026. msg->actual_length = 0;
  1027. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  1028. ret = atmel_spi_one_transfer(master, msg, xfer);
  1029. if (ret)
  1030. goto msg_done;
  1031. }
  1032. if (as->use_pdc)
  1033. atmel_spi_disable_pdc_transfer(as);
  1034. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  1035. dev_dbg(&spi->dev,
  1036. " xfer %p: len %u tx %p/%pad rx %p/%pad\n",
  1037. xfer, xfer->len,
  1038. xfer->tx_buf, &xfer->tx_dma,
  1039. xfer->rx_buf, &xfer->rx_dma);
  1040. }
  1041. msg_done:
  1042. if (!as->keep_cs)
  1043. cs_deactivate(as, msg->spi);
  1044. atmel_spi_unlock(as);
  1045. msg->status = as->done_status;
  1046. spi_finalize_current_message(spi->master);
  1047. return ret;
  1048. }
  1049. static void atmel_spi_cleanup(struct spi_device *spi)
  1050. {
  1051. struct atmel_spi_device *asd = spi->controller_state;
  1052. unsigned gpio = (unsigned long) spi->controller_data;
  1053. if (!asd)
  1054. return;
  1055. spi->controller_state = NULL;
  1056. gpio_free(gpio);
  1057. kfree(asd);
  1058. }
  1059. static inline unsigned int atmel_get_version(struct atmel_spi *as)
  1060. {
  1061. return spi_readl(as, VERSION) & 0x00000fff;
  1062. }
  1063. static void atmel_get_caps(struct atmel_spi *as)
  1064. {
  1065. unsigned int version;
  1066. version = atmel_get_version(as);
  1067. dev_info(&as->pdev->dev, "version: 0x%x\n", version);
  1068. as->caps.is_spi2 = version > 0x121;
  1069. as->caps.has_wdrbt = version >= 0x210;
  1070. as->caps.has_dma_support = version >= 0x212;
  1071. }
  1072. /*-------------------------------------------------------------------------*/
  1073. static int atmel_spi_probe(struct platform_device *pdev)
  1074. {
  1075. struct resource *regs;
  1076. int irq;
  1077. struct clk *clk;
  1078. int ret;
  1079. struct spi_master *master;
  1080. struct atmel_spi *as;
  1081. /* Select default pin state */
  1082. pinctrl_pm_select_default_state(&pdev->dev);
  1083. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1084. if (!regs)
  1085. return -ENXIO;
  1086. irq = platform_get_irq(pdev, 0);
  1087. if (irq < 0)
  1088. return irq;
  1089. clk = devm_clk_get(&pdev->dev, "spi_clk");
  1090. if (IS_ERR(clk))
  1091. return PTR_ERR(clk);
  1092. /* setup spi core then atmel-specific driver state */
  1093. ret = -ENOMEM;
  1094. master = spi_alloc_master(&pdev->dev, sizeof(*as));
  1095. if (!master)
  1096. goto out_free;
  1097. /* the spi->mode bits understood by this driver: */
  1098. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  1099. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(8, 16);
  1100. master->dev.of_node = pdev->dev.of_node;
  1101. master->bus_num = pdev->id;
  1102. master->num_chipselect = master->dev.of_node ? 0 : 4;
  1103. master->setup = atmel_spi_setup;
  1104. master->transfer_one_message = atmel_spi_transfer_one_message;
  1105. master->cleanup = atmel_spi_cleanup;
  1106. master->auto_runtime_pm = true;
  1107. platform_set_drvdata(pdev, master);
  1108. as = spi_master_get_devdata(master);
  1109. /*
  1110. * Scratch buffer is used for throwaway rx and tx data.
  1111. * It's coherent to minimize dcache pollution.
  1112. */
  1113. as->buffer = dma_alloc_coherent(&pdev->dev, BUFFER_SIZE,
  1114. &as->buffer_dma, GFP_KERNEL);
  1115. if (!as->buffer)
  1116. goto out_free;
  1117. spin_lock_init(&as->lock);
  1118. as->pdev = pdev;
  1119. as->regs = devm_ioremap_resource(&pdev->dev, regs);
  1120. if (IS_ERR(as->regs)) {
  1121. ret = PTR_ERR(as->regs);
  1122. goto out_free_buffer;
  1123. }
  1124. as->phybase = regs->start;
  1125. as->irq = irq;
  1126. as->clk = clk;
  1127. init_completion(&as->xfer_completion);
  1128. atmel_get_caps(as);
  1129. as->use_dma = false;
  1130. as->use_pdc = false;
  1131. if (as->caps.has_dma_support) {
  1132. ret = atmel_spi_configure_dma(as);
  1133. if (ret == 0)
  1134. as->use_dma = true;
  1135. else if (ret == -EPROBE_DEFER)
  1136. return ret;
  1137. } else {
  1138. as->use_pdc = true;
  1139. }
  1140. if (as->caps.has_dma_support && !as->use_dma)
  1141. dev_info(&pdev->dev, "Atmel SPI Controller using PIO only\n");
  1142. if (as->use_pdc) {
  1143. ret = devm_request_irq(&pdev->dev, irq, atmel_spi_pdc_interrupt,
  1144. 0, dev_name(&pdev->dev), master);
  1145. } else {
  1146. ret = devm_request_irq(&pdev->dev, irq, atmel_spi_pio_interrupt,
  1147. 0, dev_name(&pdev->dev), master);
  1148. }
  1149. if (ret)
  1150. goto out_unmap_regs;
  1151. /* Initialize the hardware */
  1152. ret = clk_prepare_enable(clk);
  1153. if (ret)
  1154. goto out_free_irq;
  1155. spi_writel(as, CR, SPI_BIT(SWRST));
  1156. spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */
  1157. if (as->caps.has_wdrbt) {
  1158. spi_writel(as, MR, SPI_BIT(WDRBT) | SPI_BIT(MODFDIS)
  1159. | SPI_BIT(MSTR));
  1160. } else {
  1161. spi_writel(as, MR, SPI_BIT(MSTR) | SPI_BIT(MODFDIS));
  1162. }
  1163. if (as->use_pdc)
  1164. spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
  1165. spi_writel(as, CR, SPI_BIT(SPIEN));
  1166. /* go! */
  1167. dev_info(&pdev->dev, "Atmel SPI Controller at 0x%08lx (irq %d)\n",
  1168. (unsigned long)regs->start, irq);
  1169. pm_runtime_set_autosuspend_delay(&pdev->dev, AUTOSUSPEND_TIMEOUT);
  1170. pm_runtime_use_autosuspend(&pdev->dev);
  1171. pm_runtime_set_active(&pdev->dev);
  1172. pm_runtime_enable(&pdev->dev);
  1173. ret = devm_spi_register_master(&pdev->dev, master);
  1174. if (ret)
  1175. goto out_free_dma;
  1176. return 0;
  1177. out_free_dma:
  1178. pm_runtime_disable(&pdev->dev);
  1179. pm_runtime_set_suspended(&pdev->dev);
  1180. if (as->use_dma)
  1181. atmel_spi_release_dma(as);
  1182. spi_writel(as, CR, SPI_BIT(SWRST));
  1183. spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */
  1184. clk_disable_unprepare(clk);
  1185. out_free_irq:
  1186. out_unmap_regs:
  1187. out_free_buffer:
  1188. dma_free_coherent(&pdev->dev, BUFFER_SIZE, as->buffer,
  1189. as->buffer_dma);
  1190. out_free:
  1191. spi_master_put(master);
  1192. return ret;
  1193. }
  1194. static int atmel_spi_remove(struct platform_device *pdev)
  1195. {
  1196. struct spi_master *master = platform_get_drvdata(pdev);
  1197. struct atmel_spi *as = spi_master_get_devdata(master);
  1198. pm_runtime_get_sync(&pdev->dev);
  1199. /* reset the hardware and block queue progress */
  1200. spin_lock_irq(&as->lock);
  1201. if (as->use_dma) {
  1202. atmel_spi_stop_dma(as);
  1203. atmel_spi_release_dma(as);
  1204. }
  1205. spi_writel(as, CR, SPI_BIT(SWRST));
  1206. spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */
  1207. spi_readl(as, SR);
  1208. spin_unlock_irq(&as->lock);
  1209. dma_free_coherent(&pdev->dev, BUFFER_SIZE, as->buffer,
  1210. as->buffer_dma);
  1211. clk_disable_unprepare(as->clk);
  1212. pm_runtime_put_noidle(&pdev->dev);
  1213. pm_runtime_disable(&pdev->dev);
  1214. return 0;
  1215. }
  1216. #ifdef CONFIG_PM
  1217. static int atmel_spi_runtime_suspend(struct device *dev)
  1218. {
  1219. struct spi_master *master = dev_get_drvdata(dev);
  1220. struct atmel_spi *as = spi_master_get_devdata(master);
  1221. clk_disable_unprepare(as->clk);
  1222. pinctrl_pm_select_sleep_state(dev);
  1223. return 0;
  1224. }
  1225. static int atmel_spi_runtime_resume(struct device *dev)
  1226. {
  1227. struct spi_master *master = dev_get_drvdata(dev);
  1228. struct atmel_spi *as = spi_master_get_devdata(master);
  1229. pinctrl_pm_select_default_state(dev);
  1230. return clk_prepare_enable(as->clk);
  1231. }
  1232. static int atmel_spi_suspend(struct device *dev)
  1233. {
  1234. struct spi_master *master = dev_get_drvdata(dev);
  1235. int ret;
  1236. /* Stop the queue running */
  1237. ret = spi_master_suspend(master);
  1238. if (ret) {
  1239. dev_warn(dev, "cannot suspend master\n");
  1240. return ret;
  1241. }
  1242. if (!pm_runtime_suspended(dev))
  1243. atmel_spi_runtime_suspend(dev);
  1244. return 0;
  1245. }
  1246. static int atmel_spi_resume(struct device *dev)
  1247. {
  1248. struct spi_master *master = dev_get_drvdata(dev);
  1249. int ret;
  1250. if (!pm_runtime_suspended(dev)) {
  1251. ret = atmel_spi_runtime_resume(dev);
  1252. if (ret)
  1253. return ret;
  1254. }
  1255. /* Start the queue running */
  1256. ret = spi_master_resume(master);
  1257. if (ret)
  1258. dev_err(dev, "problem starting queue (%d)\n", ret);
  1259. return ret;
  1260. }
  1261. static const struct dev_pm_ops atmel_spi_pm_ops = {
  1262. SET_SYSTEM_SLEEP_PM_OPS(atmel_spi_suspend, atmel_spi_resume)
  1263. SET_RUNTIME_PM_OPS(atmel_spi_runtime_suspend,
  1264. atmel_spi_runtime_resume, NULL)
  1265. };
  1266. #define ATMEL_SPI_PM_OPS (&atmel_spi_pm_ops)
  1267. #else
  1268. #define ATMEL_SPI_PM_OPS NULL
  1269. #endif
  1270. #if defined(CONFIG_OF)
  1271. static const struct of_device_id atmel_spi_dt_ids[] = {
  1272. { .compatible = "atmel,at91rm9200-spi" },
  1273. { /* sentinel */ }
  1274. };
  1275. MODULE_DEVICE_TABLE(of, atmel_spi_dt_ids);
  1276. #endif
  1277. static struct platform_driver atmel_spi_driver = {
  1278. .driver = {
  1279. .name = "atmel_spi",
  1280. .owner = THIS_MODULE,
  1281. .pm = ATMEL_SPI_PM_OPS,
  1282. .of_match_table = of_match_ptr(atmel_spi_dt_ids),
  1283. },
  1284. .probe = atmel_spi_probe,
  1285. .remove = atmel_spi_remove,
  1286. };
  1287. module_platform_driver(atmel_spi_driver);
  1288. MODULE_DESCRIPTION("Atmel AT32/AT91 SPI Controller driver");
  1289. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  1290. MODULE_LICENSE("GPL");
  1291. MODULE_ALIAS("platform:atmel_spi");