spi-atmel.c 47 KB

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