spi-atmel.c 47 KB

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