spi-atmel.c 37 KB

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