spi-atmel.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788
  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,
  677. xfer->bits_per_word))
  678. goto err_exit;
  679. /* Send both scatterlists */
  680. rxdesc = dmaengine_prep_slave_sg(rxchan, &as->dma.sgrx, 1,
  681. DMA_FROM_DEVICE,
  682. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  683. if (!rxdesc)
  684. goto err_dma;
  685. txdesc = dmaengine_prep_slave_sg(txchan, &as->dma.sgtx, 1,
  686. DMA_TO_DEVICE,
  687. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  688. if (!txdesc)
  689. goto err_dma;
  690. dev_dbg(master->dev.parent,
  691. " start dma xfer %p: len %u tx %p/%08llx rx %p/%08llx\n",
  692. xfer, xfer->len, xfer->tx_buf, (unsigned long long)xfer->tx_dma,
  693. xfer->rx_buf, (unsigned long long)xfer->rx_dma);
  694. /* Enable relevant interrupts */
  695. spi_writel(as, IER, SPI_BIT(OVRES));
  696. /* Put the callback on the RX transfer only, that should finish last */
  697. rxdesc->callback = dma_callback;
  698. rxdesc->callback_param = master;
  699. /* Submit and fire RX and TX with TX last so we're ready to read! */
  700. cookie = rxdesc->tx_submit(rxdesc);
  701. if (dma_submit_error(cookie))
  702. goto err_dma;
  703. cookie = txdesc->tx_submit(txdesc);
  704. if (dma_submit_error(cookie))
  705. goto err_dma;
  706. rxchan->device->device_issue_pending(rxchan);
  707. txchan->device->device_issue_pending(txchan);
  708. /* take back lock */
  709. atmel_spi_lock(as);
  710. return 0;
  711. err_dma:
  712. spi_writel(as, IDR, SPI_BIT(OVRES));
  713. atmel_spi_stop_dma(as);
  714. err_exit:
  715. atmel_spi_lock(as);
  716. return -ENOMEM;
  717. }
  718. static void atmel_spi_next_xfer_data(struct spi_master *master,
  719. struct spi_transfer *xfer,
  720. dma_addr_t *tx_dma,
  721. dma_addr_t *rx_dma,
  722. u32 *plen)
  723. {
  724. struct atmel_spi *as = spi_master_get_devdata(master);
  725. u32 len = *plen;
  726. /* use scratch buffer only when rx or tx data is unspecified */
  727. if (xfer->rx_buf)
  728. *rx_dma = xfer->rx_dma + xfer->len - *plen;
  729. else {
  730. *rx_dma = as->buffer_dma;
  731. if (len > BUFFER_SIZE)
  732. len = BUFFER_SIZE;
  733. }
  734. if (xfer->tx_buf)
  735. *tx_dma = xfer->tx_dma + xfer->len - *plen;
  736. else {
  737. *tx_dma = as->buffer_dma;
  738. if (len > BUFFER_SIZE)
  739. len = BUFFER_SIZE;
  740. memset(as->buffer, 0, len);
  741. dma_sync_single_for_device(&as->pdev->dev,
  742. as->buffer_dma, len, DMA_TO_DEVICE);
  743. }
  744. *plen = len;
  745. }
  746. static int atmel_spi_set_xfer_speed(struct atmel_spi *as,
  747. struct spi_device *spi,
  748. struct spi_transfer *xfer)
  749. {
  750. u32 scbr, csr;
  751. unsigned long bus_hz;
  752. /* v1 chips start out at half the peripheral bus speed. */
  753. bus_hz = clk_get_rate(as->clk);
  754. if (!atmel_spi_is_v2(as))
  755. bus_hz /= 2;
  756. /*
  757. * Calculate the lowest divider that satisfies the
  758. * constraint, assuming div32/fdiv/mbz == 0.
  759. */
  760. scbr = DIV_ROUND_UP(bus_hz, xfer->speed_hz);
  761. /*
  762. * If the resulting divider doesn't fit into the
  763. * register bitfield, we can't satisfy the constraint.
  764. */
  765. if (scbr >= (1 << SPI_SCBR_SIZE)) {
  766. dev_err(&spi->dev,
  767. "setup: %d Hz too slow, scbr %u; min %ld Hz\n",
  768. xfer->speed_hz, scbr, bus_hz/255);
  769. return -EINVAL;
  770. }
  771. if (scbr == 0) {
  772. dev_err(&spi->dev,
  773. "setup: %d Hz too high, scbr %u; max %ld Hz\n",
  774. xfer->speed_hz, scbr, bus_hz);
  775. return -EINVAL;
  776. }
  777. csr = spi_readl(as, CSR0 + 4 * spi->chip_select);
  778. csr = SPI_BFINS(SCBR, scbr, csr);
  779. spi_writel(as, CSR0 + 4 * spi->chip_select, csr);
  780. return 0;
  781. }
  782. /*
  783. * Submit next transfer for PDC.
  784. * lock is held, spi irq is blocked
  785. */
  786. static void atmel_spi_pdc_next_xfer(struct spi_master *master,
  787. struct spi_message *msg,
  788. struct spi_transfer *xfer)
  789. {
  790. struct atmel_spi *as = spi_master_get_devdata(master);
  791. u32 len;
  792. dma_addr_t tx_dma, rx_dma;
  793. spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
  794. len = as->current_remaining_bytes;
  795. atmel_spi_next_xfer_data(master, xfer, &tx_dma, &rx_dma, &len);
  796. as->current_remaining_bytes -= len;
  797. spi_writel(as, RPR, rx_dma);
  798. spi_writel(as, TPR, tx_dma);
  799. if (msg->spi->bits_per_word > 8)
  800. len >>= 1;
  801. spi_writel(as, RCR, len);
  802. spi_writel(as, TCR, len);
  803. dev_dbg(&msg->spi->dev,
  804. " start xfer %p: len %u tx %p/%08llx rx %p/%08llx\n",
  805. xfer, xfer->len, xfer->tx_buf,
  806. (unsigned long long)xfer->tx_dma, xfer->rx_buf,
  807. (unsigned long long)xfer->rx_dma);
  808. if (as->current_remaining_bytes) {
  809. len = as->current_remaining_bytes;
  810. atmel_spi_next_xfer_data(master, xfer, &tx_dma, &rx_dma, &len);
  811. as->current_remaining_bytes -= len;
  812. spi_writel(as, RNPR, rx_dma);
  813. spi_writel(as, TNPR, tx_dma);
  814. if (msg->spi->bits_per_word > 8)
  815. len >>= 1;
  816. spi_writel(as, RNCR, len);
  817. spi_writel(as, TNCR, len);
  818. dev_dbg(&msg->spi->dev,
  819. " next xfer %p: len %u tx %p/%08llx rx %p/%08llx\n",
  820. xfer, xfer->len, xfer->tx_buf,
  821. (unsigned long long)xfer->tx_dma, xfer->rx_buf,
  822. (unsigned long long)xfer->rx_dma);
  823. }
  824. /* REVISIT: We're waiting for RXBUFF before we start the next
  825. * transfer because we need to handle some difficult timing
  826. * issues otherwise. If we wait for TXBUFE in one transfer and
  827. * then starts waiting for RXBUFF in the next, it's difficult
  828. * to tell the difference between the RXBUFF interrupt we're
  829. * actually waiting for and the RXBUFF interrupt of the
  830. * previous transfer.
  831. *
  832. * It should be doable, though. Just not now...
  833. */
  834. spi_writel(as, IER, SPI_BIT(RXBUFF) | SPI_BIT(OVRES));
  835. spi_writel(as, PTCR, SPI_BIT(TXTEN) | SPI_BIT(RXTEN));
  836. }
  837. /*
  838. * For DMA, tx_buf/tx_dma have the same relationship as rx_buf/rx_dma:
  839. * - The buffer is either valid for CPU access, else NULL
  840. * - If the buffer is valid, so is its DMA address
  841. *
  842. * This driver manages the dma address unless message->is_dma_mapped.
  843. */
  844. static int
  845. atmel_spi_dma_map_xfer(struct atmel_spi *as, struct spi_transfer *xfer)
  846. {
  847. struct device *dev = &as->pdev->dev;
  848. xfer->tx_dma = xfer->rx_dma = INVALID_DMA_ADDRESS;
  849. if (xfer->tx_buf) {
  850. /* tx_buf is a const void* where we need a void * for the dma
  851. * mapping */
  852. void *nonconst_tx = (void *)xfer->tx_buf;
  853. xfer->tx_dma = dma_map_single(dev,
  854. nonconst_tx, xfer->len,
  855. DMA_TO_DEVICE);
  856. if (dma_mapping_error(dev, xfer->tx_dma))
  857. return -ENOMEM;
  858. }
  859. if (xfer->rx_buf) {
  860. xfer->rx_dma = dma_map_single(dev,
  861. xfer->rx_buf, xfer->len,
  862. DMA_FROM_DEVICE);
  863. if (dma_mapping_error(dev, xfer->rx_dma)) {
  864. if (xfer->tx_buf)
  865. dma_unmap_single(dev,
  866. xfer->tx_dma, xfer->len,
  867. DMA_TO_DEVICE);
  868. return -ENOMEM;
  869. }
  870. }
  871. return 0;
  872. }
  873. static void atmel_spi_dma_unmap_xfer(struct spi_master *master,
  874. struct spi_transfer *xfer)
  875. {
  876. if (xfer->tx_dma != INVALID_DMA_ADDRESS)
  877. dma_unmap_single(master->dev.parent, xfer->tx_dma,
  878. xfer->len, DMA_TO_DEVICE);
  879. if (xfer->rx_dma != INVALID_DMA_ADDRESS)
  880. dma_unmap_single(master->dev.parent, xfer->rx_dma,
  881. xfer->len, DMA_FROM_DEVICE);
  882. }
  883. static void atmel_spi_disable_pdc_transfer(struct atmel_spi *as)
  884. {
  885. spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
  886. }
  887. static void
  888. atmel_spi_pump_single_data(struct atmel_spi *as, struct spi_transfer *xfer)
  889. {
  890. u8 *rxp;
  891. u16 *rxp16;
  892. unsigned long xfer_pos = xfer->len - as->current_remaining_bytes;
  893. if (xfer->rx_buf) {
  894. if (xfer->bits_per_word > 8) {
  895. rxp16 = (u16 *)(((u8 *)xfer->rx_buf) + xfer_pos);
  896. *rxp16 = spi_readl(as, RDR);
  897. } else {
  898. rxp = ((u8 *)xfer->rx_buf) + xfer_pos;
  899. *rxp = spi_readl(as, RDR);
  900. }
  901. } else {
  902. spi_readl(as, RDR);
  903. }
  904. if (xfer->bits_per_word > 8) {
  905. if (as->current_remaining_bytes > 2)
  906. as->current_remaining_bytes -= 2;
  907. else
  908. as->current_remaining_bytes = 0;
  909. } else {
  910. as->current_remaining_bytes--;
  911. }
  912. }
  913. static void
  914. atmel_spi_pump_fifo_data(struct atmel_spi *as, struct spi_transfer *xfer)
  915. {
  916. u32 fifolr = spi_readl(as, FLR);
  917. u32 num_bytes, num_data = SPI_BFEXT(RXFL, fifolr);
  918. u32 offset = xfer->len - as->current_remaining_bytes;
  919. u16 *words = (u16 *)((u8 *)xfer->rx_buf + offset);
  920. u8 *bytes = (u8 *)((u8 *)xfer->rx_buf + offset);
  921. u16 rd; /* RD field is the lowest 16 bits of RDR */
  922. /* Update the number of remaining bytes to transfer */
  923. num_bytes = ((xfer->bits_per_word > 8) ?
  924. (num_data << 1) :
  925. num_data);
  926. if (as->current_remaining_bytes > num_bytes)
  927. as->current_remaining_bytes -= num_bytes;
  928. else
  929. as->current_remaining_bytes = 0;
  930. /* Handle odd number of bytes when data are more than 8bit width */
  931. if (xfer->bits_per_word > 8)
  932. as->current_remaining_bytes &= ~0x1;
  933. /* Read data */
  934. while (num_data) {
  935. rd = spi_readl(as, RDR);
  936. if (xfer->rx_buf) {
  937. if (xfer->bits_per_word > 8)
  938. *words++ = rd;
  939. else
  940. *bytes++ = rd;
  941. }
  942. num_data--;
  943. }
  944. }
  945. /* Called from IRQ
  946. *
  947. * Must update "current_remaining_bytes" to keep track of data
  948. * to transfer.
  949. */
  950. static void
  951. atmel_spi_pump_pio_data(struct atmel_spi *as, struct spi_transfer *xfer)
  952. {
  953. if (as->fifo_size)
  954. atmel_spi_pump_fifo_data(as, xfer);
  955. else
  956. atmel_spi_pump_single_data(as, xfer);
  957. }
  958. /* Interrupt
  959. *
  960. * No need for locking in this Interrupt handler: done_status is the
  961. * only information modified.
  962. */
  963. static irqreturn_t
  964. atmel_spi_pio_interrupt(int irq, void *dev_id)
  965. {
  966. struct spi_master *master = dev_id;
  967. struct atmel_spi *as = spi_master_get_devdata(master);
  968. u32 status, pending, imr;
  969. struct spi_transfer *xfer;
  970. int ret = IRQ_NONE;
  971. imr = spi_readl(as, IMR);
  972. status = spi_readl(as, SR);
  973. pending = status & imr;
  974. if (pending & SPI_BIT(OVRES)) {
  975. ret = IRQ_HANDLED;
  976. spi_writel(as, IDR, SPI_BIT(OVRES));
  977. dev_warn(master->dev.parent, "overrun\n");
  978. /*
  979. * When we get an overrun, we disregard the current
  980. * transfer. Data will not be copied back from any
  981. * bounce buffer and msg->actual_len will not be
  982. * updated with the last xfer.
  983. *
  984. * We will also not process any remaning transfers in
  985. * the message.
  986. */
  987. as->done_status = -EIO;
  988. smp_wmb();
  989. /* Clear any overrun happening while cleaning up */
  990. spi_readl(as, SR);
  991. complete(&as->xfer_completion);
  992. } else if (pending & (SPI_BIT(RDRF) | SPI_BIT(RXFTHF))) {
  993. atmel_spi_lock(as);
  994. if (as->current_remaining_bytes) {
  995. ret = IRQ_HANDLED;
  996. xfer = as->current_transfer;
  997. atmel_spi_pump_pio_data(as, xfer);
  998. if (!as->current_remaining_bytes)
  999. spi_writel(as, IDR, pending);
  1000. complete(&as->xfer_completion);
  1001. }
  1002. atmel_spi_unlock(as);
  1003. } else {
  1004. WARN_ONCE(pending, "IRQ not handled, pending = %x\n", pending);
  1005. ret = IRQ_HANDLED;
  1006. spi_writel(as, IDR, pending);
  1007. }
  1008. return ret;
  1009. }
  1010. static irqreturn_t
  1011. atmel_spi_pdc_interrupt(int irq, void *dev_id)
  1012. {
  1013. struct spi_master *master = dev_id;
  1014. struct atmel_spi *as = spi_master_get_devdata(master);
  1015. u32 status, pending, imr;
  1016. int ret = IRQ_NONE;
  1017. imr = spi_readl(as, IMR);
  1018. status = spi_readl(as, SR);
  1019. pending = status & imr;
  1020. if (pending & SPI_BIT(OVRES)) {
  1021. ret = IRQ_HANDLED;
  1022. spi_writel(as, IDR, (SPI_BIT(RXBUFF) | SPI_BIT(ENDRX)
  1023. | SPI_BIT(OVRES)));
  1024. /* Clear any overrun happening while cleaning up */
  1025. spi_readl(as, SR);
  1026. as->done_status = -EIO;
  1027. complete(&as->xfer_completion);
  1028. } else if (pending & (SPI_BIT(RXBUFF) | SPI_BIT(ENDRX))) {
  1029. ret = IRQ_HANDLED;
  1030. spi_writel(as, IDR, pending);
  1031. complete(&as->xfer_completion);
  1032. }
  1033. return ret;
  1034. }
  1035. static int atmel_spi_setup(struct spi_device *spi)
  1036. {
  1037. struct atmel_spi *as;
  1038. struct atmel_spi_device *asd;
  1039. u32 csr;
  1040. unsigned int bits = spi->bits_per_word;
  1041. unsigned int npcs_pin;
  1042. int ret;
  1043. as = spi_master_get_devdata(spi->master);
  1044. /* see notes above re chipselect */
  1045. if (!atmel_spi_is_v2(as)
  1046. && spi->chip_select == 0
  1047. && (spi->mode & SPI_CS_HIGH)) {
  1048. dev_dbg(&spi->dev, "setup: can't be active-high\n");
  1049. return -EINVAL;
  1050. }
  1051. csr = SPI_BF(BITS, bits - 8);
  1052. if (spi->mode & SPI_CPOL)
  1053. csr |= SPI_BIT(CPOL);
  1054. if (!(spi->mode & SPI_CPHA))
  1055. csr |= SPI_BIT(NCPHA);
  1056. if (!as->use_cs_gpios)
  1057. csr |= SPI_BIT(CSAAT);
  1058. /* DLYBS is mostly irrelevant since we manage chipselect using GPIOs.
  1059. *
  1060. * DLYBCT would add delays between words, slowing down transfers.
  1061. * It could potentially be useful to cope with DMA bottlenecks, but
  1062. * in those cases it's probably best to just use a lower bitrate.
  1063. */
  1064. csr |= SPI_BF(DLYBS, 0);
  1065. csr |= SPI_BF(DLYBCT, 0);
  1066. /* chipselect must have been muxed as GPIO (e.g. in board setup) */
  1067. npcs_pin = (unsigned long)spi->controller_data;
  1068. if (!as->use_cs_gpios)
  1069. npcs_pin = spi->chip_select;
  1070. else if (gpio_is_valid(spi->cs_gpio))
  1071. npcs_pin = spi->cs_gpio;
  1072. asd = spi->controller_state;
  1073. if (!asd) {
  1074. asd = kzalloc(sizeof(struct atmel_spi_device), GFP_KERNEL);
  1075. if (!asd)
  1076. return -ENOMEM;
  1077. if (as->use_cs_gpios) {
  1078. ret = gpio_request(npcs_pin, dev_name(&spi->dev));
  1079. if (ret) {
  1080. kfree(asd);
  1081. return ret;
  1082. }
  1083. gpio_direction_output(npcs_pin,
  1084. !(spi->mode & SPI_CS_HIGH));
  1085. }
  1086. asd->npcs_pin = npcs_pin;
  1087. spi->controller_state = asd;
  1088. }
  1089. asd->csr = csr;
  1090. dev_dbg(&spi->dev,
  1091. "setup: bpw %u mode 0x%x -> csr%d %08x\n",
  1092. bits, spi->mode, spi->chip_select, csr);
  1093. if (!atmel_spi_is_v2(as))
  1094. spi_writel(as, CSR0 + 4 * spi->chip_select, csr);
  1095. return 0;
  1096. }
  1097. static int atmel_spi_one_transfer(struct spi_master *master,
  1098. struct spi_message *msg,
  1099. struct spi_transfer *xfer)
  1100. {
  1101. struct atmel_spi *as;
  1102. struct spi_device *spi = msg->spi;
  1103. u8 bits;
  1104. u32 len;
  1105. struct atmel_spi_device *asd;
  1106. int timeout;
  1107. int ret;
  1108. unsigned long dma_timeout;
  1109. as = spi_master_get_devdata(master);
  1110. if (!(xfer->tx_buf || xfer->rx_buf) && xfer->len) {
  1111. dev_dbg(&spi->dev, "missing rx or tx buf\n");
  1112. return -EINVAL;
  1113. }
  1114. asd = spi->controller_state;
  1115. bits = (asd->csr >> 4) & 0xf;
  1116. if (bits != xfer->bits_per_word - 8) {
  1117. dev_dbg(&spi->dev,
  1118. "you can't yet change bits_per_word in transfers\n");
  1119. return -ENOPROTOOPT;
  1120. }
  1121. /*
  1122. * DMA map early, for performance (empties dcache ASAP) and
  1123. * better fault reporting.
  1124. */
  1125. if ((!msg->is_dma_mapped)
  1126. && (atmel_spi_use_dma(as, xfer) || as->use_pdc)) {
  1127. if (atmel_spi_dma_map_xfer(as, xfer) < 0)
  1128. return -ENOMEM;
  1129. }
  1130. atmel_spi_set_xfer_speed(as, msg->spi, xfer);
  1131. as->done_status = 0;
  1132. as->current_transfer = xfer;
  1133. as->current_remaining_bytes = xfer->len;
  1134. while (as->current_remaining_bytes) {
  1135. reinit_completion(&as->xfer_completion);
  1136. if (as->use_pdc) {
  1137. atmel_spi_pdc_next_xfer(master, msg, xfer);
  1138. } else if (atmel_spi_use_dma(as, xfer)) {
  1139. len = as->current_remaining_bytes;
  1140. ret = atmel_spi_next_xfer_dma_submit(master,
  1141. xfer, &len);
  1142. if (ret) {
  1143. dev_err(&spi->dev,
  1144. "unable to use DMA, fallback to PIO\n");
  1145. atmel_spi_next_xfer_pio(master, xfer);
  1146. } else {
  1147. as->current_remaining_bytes -= len;
  1148. if (as->current_remaining_bytes < 0)
  1149. as->current_remaining_bytes = 0;
  1150. }
  1151. } else {
  1152. atmel_spi_next_xfer_pio(master, xfer);
  1153. }
  1154. /* interrupts are disabled, so free the lock for schedule */
  1155. atmel_spi_unlock(as);
  1156. dma_timeout = wait_for_completion_timeout(&as->xfer_completion,
  1157. SPI_DMA_TIMEOUT);
  1158. atmel_spi_lock(as);
  1159. if (WARN_ON(dma_timeout == 0)) {
  1160. dev_err(&spi->dev, "spi transfer timeout\n");
  1161. as->done_status = -EIO;
  1162. }
  1163. if (as->done_status)
  1164. break;
  1165. }
  1166. if (as->done_status) {
  1167. if (as->use_pdc) {
  1168. dev_warn(master->dev.parent,
  1169. "overrun (%u/%u remaining)\n",
  1170. spi_readl(as, TCR), spi_readl(as, RCR));
  1171. /*
  1172. * Clean up DMA registers and make sure the data
  1173. * registers are empty.
  1174. */
  1175. spi_writel(as, RNCR, 0);
  1176. spi_writel(as, TNCR, 0);
  1177. spi_writel(as, RCR, 0);
  1178. spi_writel(as, TCR, 0);
  1179. for (timeout = 1000; timeout; timeout--)
  1180. if (spi_readl(as, SR) & SPI_BIT(TXEMPTY))
  1181. break;
  1182. if (!timeout)
  1183. dev_warn(master->dev.parent,
  1184. "timeout waiting for TXEMPTY");
  1185. while (spi_readl(as, SR) & SPI_BIT(RDRF))
  1186. spi_readl(as, RDR);
  1187. /* Clear any overrun happening while cleaning up */
  1188. spi_readl(as, SR);
  1189. } else if (atmel_spi_use_dma(as, xfer)) {
  1190. atmel_spi_stop_dma(as);
  1191. }
  1192. if (!msg->is_dma_mapped
  1193. && (atmel_spi_use_dma(as, xfer) || as->use_pdc))
  1194. atmel_spi_dma_unmap_xfer(master, xfer);
  1195. return 0;
  1196. } else {
  1197. /* only update length if no error */
  1198. msg->actual_length += xfer->len;
  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. if (xfer->delay_usecs)
  1204. udelay(xfer->delay_usecs);
  1205. if (xfer->cs_change) {
  1206. if (list_is_last(&xfer->transfer_list,
  1207. &msg->transfers)) {
  1208. as->keep_cs = true;
  1209. } else {
  1210. as->cs_active = !as->cs_active;
  1211. if (as->cs_active)
  1212. cs_activate(as, msg->spi);
  1213. else
  1214. cs_deactivate(as, msg->spi);
  1215. }
  1216. }
  1217. return 0;
  1218. }
  1219. static int atmel_spi_transfer_one_message(struct spi_master *master,
  1220. struct spi_message *msg)
  1221. {
  1222. struct atmel_spi *as;
  1223. struct spi_transfer *xfer;
  1224. struct spi_device *spi = msg->spi;
  1225. int ret = 0;
  1226. as = spi_master_get_devdata(master);
  1227. dev_dbg(&spi->dev, "new message %p submitted for %s\n",
  1228. msg, dev_name(&spi->dev));
  1229. atmel_spi_lock(as);
  1230. cs_activate(as, spi);
  1231. as->cs_active = true;
  1232. as->keep_cs = false;
  1233. msg->status = 0;
  1234. msg->actual_length = 0;
  1235. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  1236. ret = atmel_spi_one_transfer(master, msg, xfer);
  1237. if (ret)
  1238. goto msg_done;
  1239. }
  1240. if (as->use_pdc)
  1241. atmel_spi_disable_pdc_transfer(as);
  1242. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  1243. dev_dbg(&spi->dev,
  1244. " xfer %p: len %u tx %p/%pad rx %p/%pad\n",
  1245. xfer, xfer->len,
  1246. xfer->tx_buf, &xfer->tx_dma,
  1247. xfer->rx_buf, &xfer->rx_dma);
  1248. }
  1249. msg_done:
  1250. if (!as->keep_cs)
  1251. cs_deactivate(as, msg->spi);
  1252. atmel_spi_unlock(as);
  1253. msg->status = as->done_status;
  1254. spi_finalize_current_message(spi->master);
  1255. return ret;
  1256. }
  1257. static void atmel_spi_cleanup(struct spi_device *spi)
  1258. {
  1259. struct atmel_spi_device *asd = spi->controller_state;
  1260. unsigned gpio = (unsigned long) spi->controller_data;
  1261. if (!asd)
  1262. return;
  1263. spi->controller_state = NULL;
  1264. gpio_free(gpio);
  1265. kfree(asd);
  1266. }
  1267. static inline unsigned int atmel_get_version(struct atmel_spi *as)
  1268. {
  1269. return spi_readl(as, VERSION) & 0x00000fff;
  1270. }
  1271. static void atmel_get_caps(struct atmel_spi *as)
  1272. {
  1273. unsigned int version;
  1274. version = atmel_get_version(as);
  1275. dev_info(&as->pdev->dev, "version: 0x%x\n", version);
  1276. as->caps.is_spi2 = version > 0x121;
  1277. as->caps.has_wdrbt = version >= 0x210;
  1278. as->caps.has_dma_support = version >= 0x212;
  1279. }
  1280. /*-------------------------------------------------------------------------*/
  1281. static int atmel_spi_probe(struct platform_device *pdev)
  1282. {
  1283. struct resource *regs;
  1284. int irq;
  1285. struct clk *clk;
  1286. int ret;
  1287. struct spi_master *master;
  1288. struct atmel_spi *as;
  1289. /* Select default pin state */
  1290. pinctrl_pm_select_default_state(&pdev->dev);
  1291. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1292. if (!regs)
  1293. return -ENXIO;
  1294. irq = platform_get_irq(pdev, 0);
  1295. if (irq < 0)
  1296. return irq;
  1297. clk = devm_clk_get(&pdev->dev, "spi_clk");
  1298. if (IS_ERR(clk))
  1299. return PTR_ERR(clk);
  1300. /* setup spi core then atmel-specific driver state */
  1301. ret = -ENOMEM;
  1302. master = spi_alloc_master(&pdev->dev, sizeof(*as));
  1303. if (!master)
  1304. goto out_free;
  1305. /* the spi->mode bits understood by this driver: */
  1306. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  1307. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(8, 16);
  1308. master->dev.of_node = pdev->dev.of_node;
  1309. master->bus_num = pdev->id;
  1310. master->num_chipselect = master->dev.of_node ? 0 : 4;
  1311. master->setup = atmel_spi_setup;
  1312. master->transfer_one_message = atmel_spi_transfer_one_message;
  1313. master->cleanup = atmel_spi_cleanup;
  1314. master->auto_runtime_pm = true;
  1315. platform_set_drvdata(pdev, master);
  1316. as = spi_master_get_devdata(master);
  1317. /*
  1318. * Scratch buffer is used for throwaway rx and tx data.
  1319. * It's coherent to minimize dcache pollution.
  1320. */
  1321. as->buffer = dma_alloc_coherent(&pdev->dev, BUFFER_SIZE,
  1322. &as->buffer_dma, GFP_KERNEL);
  1323. if (!as->buffer)
  1324. goto out_free;
  1325. spin_lock_init(&as->lock);
  1326. as->pdev = pdev;
  1327. as->regs = devm_ioremap_resource(&pdev->dev, regs);
  1328. if (IS_ERR(as->regs)) {
  1329. ret = PTR_ERR(as->regs);
  1330. goto out_free_buffer;
  1331. }
  1332. as->phybase = regs->start;
  1333. as->irq = irq;
  1334. as->clk = clk;
  1335. init_completion(&as->xfer_completion);
  1336. atmel_get_caps(as);
  1337. as->use_cs_gpios = true;
  1338. if (atmel_spi_is_v2(as) &&
  1339. pdev->dev.of_node &&
  1340. !of_get_property(pdev->dev.of_node, "cs-gpios", NULL)) {
  1341. as->use_cs_gpios = false;
  1342. master->num_chipselect = 4;
  1343. }
  1344. as->use_dma = false;
  1345. as->use_pdc = false;
  1346. if (as->caps.has_dma_support) {
  1347. ret = atmel_spi_configure_dma(as);
  1348. if (ret == 0)
  1349. as->use_dma = true;
  1350. else if (ret == -EPROBE_DEFER)
  1351. return ret;
  1352. } else {
  1353. as->use_pdc = true;
  1354. }
  1355. if (as->caps.has_dma_support && !as->use_dma)
  1356. dev_info(&pdev->dev, "Atmel SPI Controller using PIO only\n");
  1357. if (as->use_pdc) {
  1358. ret = devm_request_irq(&pdev->dev, irq, atmel_spi_pdc_interrupt,
  1359. 0, dev_name(&pdev->dev), master);
  1360. } else {
  1361. ret = devm_request_irq(&pdev->dev, irq, atmel_spi_pio_interrupt,
  1362. 0, dev_name(&pdev->dev), master);
  1363. }
  1364. if (ret)
  1365. goto out_unmap_regs;
  1366. /* Initialize the hardware */
  1367. ret = clk_prepare_enable(clk);
  1368. if (ret)
  1369. goto out_free_irq;
  1370. spi_writel(as, CR, SPI_BIT(SWRST));
  1371. spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */
  1372. if (as->caps.has_wdrbt) {
  1373. spi_writel(as, MR, SPI_BIT(WDRBT) | SPI_BIT(MODFDIS)
  1374. | SPI_BIT(MSTR));
  1375. } else {
  1376. spi_writel(as, MR, SPI_BIT(MSTR) | SPI_BIT(MODFDIS));
  1377. }
  1378. if (as->use_pdc)
  1379. spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
  1380. spi_writel(as, CR, SPI_BIT(SPIEN));
  1381. as->fifo_size = 0;
  1382. if (!of_property_read_u32(pdev->dev.of_node, "atmel,fifo-size",
  1383. &as->fifo_size)) {
  1384. dev_info(&pdev->dev, "Using FIFO (%u data)\n", as->fifo_size);
  1385. spi_writel(as, CR, SPI_BIT(FIFOEN));
  1386. }
  1387. /* go! */
  1388. dev_info(&pdev->dev, "Atmel SPI Controller at 0x%08lx (irq %d)\n",
  1389. (unsigned long)regs->start, irq);
  1390. pm_runtime_set_autosuspend_delay(&pdev->dev, AUTOSUSPEND_TIMEOUT);
  1391. pm_runtime_use_autosuspend(&pdev->dev);
  1392. pm_runtime_set_active(&pdev->dev);
  1393. pm_runtime_enable(&pdev->dev);
  1394. ret = devm_spi_register_master(&pdev->dev, master);
  1395. if (ret)
  1396. goto out_free_dma;
  1397. return 0;
  1398. out_free_dma:
  1399. pm_runtime_disable(&pdev->dev);
  1400. pm_runtime_set_suspended(&pdev->dev);
  1401. if (as->use_dma)
  1402. atmel_spi_release_dma(as);
  1403. spi_writel(as, CR, SPI_BIT(SWRST));
  1404. spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */
  1405. clk_disable_unprepare(clk);
  1406. out_free_irq:
  1407. out_unmap_regs:
  1408. out_free_buffer:
  1409. dma_free_coherent(&pdev->dev, BUFFER_SIZE, as->buffer,
  1410. as->buffer_dma);
  1411. out_free:
  1412. spi_master_put(master);
  1413. return ret;
  1414. }
  1415. static int atmel_spi_remove(struct platform_device *pdev)
  1416. {
  1417. struct spi_master *master = platform_get_drvdata(pdev);
  1418. struct atmel_spi *as = spi_master_get_devdata(master);
  1419. pm_runtime_get_sync(&pdev->dev);
  1420. /* reset the hardware and block queue progress */
  1421. if (as->use_dma) {
  1422. atmel_spi_stop_dma(as);
  1423. atmel_spi_release_dma(as);
  1424. }
  1425. spin_lock_irq(&as->lock);
  1426. spi_writel(as, CR, SPI_BIT(SWRST));
  1427. spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */
  1428. spi_readl(as, SR);
  1429. spin_unlock_irq(&as->lock);
  1430. dma_free_coherent(&pdev->dev, BUFFER_SIZE, as->buffer,
  1431. as->buffer_dma);
  1432. clk_disable_unprepare(as->clk);
  1433. pm_runtime_put_noidle(&pdev->dev);
  1434. pm_runtime_disable(&pdev->dev);
  1435. return 0;
  1436. }
  1437. #ifdef CONFIG_PM
  1438. static int atmel_spi_runtime_suspend(struct device *dev)
  1439. {
  1440. struct spi_master *master = dev_get_drvdata(dev);
  1441. struct atmel_spi *as = spi_master_get_devdata(master);
  1442. clk_disable_unprepare(as->clk);
  1443. pinctrl_pm_select_sleep_state(dev);
  1444. return 0;
  1445. }
  1446. static int atmel_spi_runtime_resume(struct device *dev)
  1447. {
  1448. struct spi_master *master = dev_get_drvdata(dev);
  1449. struct atmel_spi *as = spi_master_get_devdata(master);
  1450. pinctrl_pm_select_default_state(dev);
  1451. return clk_prepare_enable(as->clk);
  1452. }
  1453. #ifdef CONFIG_PM_SLEEP
  1454. static int atmel_spi_suspend(struct device *dev)
  1455. {
  1456. struct spi_master *master = dev_get_drvdata(dev);
  1457. int ret;
  1458. /* Stop the queue running */
  1459. ret = spi_master_suspend(master);
  1460. if (ret) {
  1461. dev_warn(dev, "cannot suspend master\n");
  1462. return ret;
  1463. }
  1464. if (!pm_runtime_suspended(dev))
  1465. atmel_spi_runtime_suspend(dev);
  1466. return 0;
  1467. }
  1468. static int atmel_spi_resume(struct device *dev)
  1469. {
  1470. struct spi_master *master = dev_get_drvdata(dev);
  1471. int ret;
  1472. if (!pm_runtime_suspended(dev)) {
  1473. ret = atmel_spi_runtime_resume(dev);
  1474. if (ret)
  1475. return ret;
  1476. }
  1477. /* Start the queue running */
  1478. ret = spi_master_resume(master);
  1479. if (ret)
  1480. dev_err(dev, "problem starting queue (%d)\n", ret);
  1481. return ret;
  1482. }
  1483. #endif
  1484. static const struct dev_pm_ops atmel_spi_pm_ops = {
  1485. SET_SYSTEM_SLEEP_PM_OPS(atmel_spi_suspend, atmel_spi_resume)
  1486. SET_RUNTIME_PM_OPS(atmel_spi_runtime_suspend,
  1487. atmel_spi_runtime_resume, NULL)
  1488. };
  1489. #define ATMEL_SPI_PM_OPS (&atmel_spi_pm_ops)
  1490. #else
  1491. #define ATMEL_SPI_PM_OPS NULL
  1492. #endif
  1493. #if defined(CONFIG_OF)
  1494. static const struct of_device_id atmel_spi_dt_ids[] = {
  1495. { .compatible = "atmel,at91rm9200-spi" },
  1496. { /* sentinel */ }
  1497. };
  1498. MODULE_DEVICE_TABLE(of, atmel_spi_dt_ids);
  1499. #endif
  1500. static struct platform_driver atmel_spi_driver = {
  1501. .driver = {
  1502. .name = "atmel_spi",
  1503. .pm = ATMEL_SPI_PM_OPS,
  1504. .of_match_table = of_match_ptr(atmel_spi_dt_ids),
  1505. },
  1506. .probe = atmel_spi_probe,
  1507. .remove = atmel_spi_remove,
  1508. };
  1509. module_platform_driver(atmel_spi_driver);
  1510. MODULE_DESCRIPTION("Atmel AT32/AT91 SPI Controller driver");
  1511. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  1512. MODULE_LICENSE("GPL");
  1513. MODULE_ALIAS("platform:atmel_spi");