spi-atmel.c 45 KB

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