spi-atmel.c 45 KB

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