spi-atmel.c 43 KB

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