spi-atmel.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748
  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. };
  247. /*
  248. * The core SPI transfer engine just talks to a register bank to set up
  249. * DMA transfers; transfer queue progress is driven by IRQs. The clock
  250. * framework provides the base clock, subdivided for each spi_device.
  251. */
  252. struct atmel_spi {
  253. spinlock_t lock;
  254. unsigned long flags;
  255. phys_addr_t phybase;
  256. void __iomem *regs;
  257. int irq;
  258. struct clk *clk;
  259. struct platform_device *pdev;
  260. unsigned long spi_clk;
  261. struct spi_transfer *current_transfer;
  262. int current_remaining_bytes;
  263. int done_status;
  264. struct completion xfer_completion;
  265. struct atmel_spi_caps caps;
  266. bool use_dma;
  267. bool use_pdc;
  268. bool use_cs_gpios;
  269. bool keep_cs;
  270. bool cs_active;
  271. u32 fifo_size;
  272. };
  273. /* Controller-specific per-slave state */
  274. struct atmel_spi_device {
  275. unsigned int npcs_pin;
  276. u32 csr;
  277. };
  278. #define SPI_MAX_DMA_XFER 65535 /* true for both PDC and DMA */
  279. #define INVALID_DMA_ADDRESS 0xffffffff
  280. /*
  281. * Version 2 of the SPI controller has
  282. * - CR.LASTXFER
  283. * - SPI_MR.DIV32 may become FDIV or must-be-zero (here: always zero)
  284. * - SPI_SR.TXEMPTY, SPI_SR.NSSR (and corresponding irqs)
  285. * - SPI_CSRx.CSAAT
  286. * - SPI_CSRx.SBCR allows faster clocking
  287. */
  288. static bool atmel_spi_is_v2(struct atmel_spi *as)
  289. {
  290. return as->caps.is_spi2;
  291. }
  292. /*
  293. * Earlier SPI controllers (e.g. on at91rm9200) have a design bug whereby
  294. * they assume that spi slave device state will not change on deselect, so
  295. * that automagic deselection is OK. ("NPCSx rises if no data is to be
  296. * transmitted") Not so! Workaround uses nCSx pins as GPIOs; or newer
  297. * controllers have CSAAT and friends.
  298. *
  299. * Since the CSAAT functionality is a bit weird on newer controllers as
  300. * well, we use GPIO to control nCSx pins on all controllers, updating
  301. * MR.PCS to avoid confusing the controller. Using GPIOs also lets us
  302. * support active-high chipselects despite the controller's belief that
  303. * only active-low devices/systems exists.
  304. *
  305. * However, at91rm9200 has a second erratum whereby nCS0 doesn't work
  306. * right when driven with GPIO. ("Mode Fault does not allow more than one
  307. * Master on Chip Select 0.") No workaround exists for that ... so for
  308. * nCS0 on that chip, we (a) don't use the GPIO, (b) can't support CS_HIGH,
  309. * and (c) will trigger that first erratum in some cases.
  310. */
  311. static void cs_activate(struct atmel_spi *as, struct spi_device *spi)
  312. {
  313. struct atmel_spi_device *asd = spi->controller_state;
  314. unsigned active = spi->mode & SPI_CS_HIGH;
  315. u32 mr;
  316. if (atmel_spi_is_v2(as)) {
  317. spi_writel(as, CSR0 + 4 * spi->chip_select, asd->csr);
  318. /* For the low SPI version, there is a issue that PDC transfer
  319. * on CS1,2,3 needs SPI_CSR0.BITS config as SPI_CSR1,2,3.BITS
  320. */
  321. spi_writel(as, CSR0, asd->csr);
  322. if (as->caps.has_wdrbt) {
  323. spi_writel(as, MR,
  324. SPI_BF(PCS, ~(0x01 << spi->chip_select))
  325. | SPI_BIT(WDRBT)
  326. | SPI_BIT(MODFDIS)
  327. | SPI_BIT(MSTR));
  328. } else {
  329. spi_writel(as, MR,
  330. SPI_BF(PCS, ~(0x01 << spi->chip_select))
  331. | SPI_BIT(MODFDIS)
  332. | SPI_BIT(MSTR));
  333. }
  334. mr = spi_readl(as, MR);
  335. if (as->use_cs_gpios)
  336. gpio_set_value(asd->npcs_pin, active);
  337. } else {
  338. u32 cpol = (spi->mode & SPI_CPOL) ? SPI_BIT(CPOL) : 0;
  339. int i;
  340. u32 csr;
  341. /* Make sure clock polarity is correct */
  342. for (i = 0; i < spi->master->num_chipselect; i++) {
  343. csr = spi_readl(as, CSR0 + 4 * i);
  344. if ((csr ^ cpol) & SPI_BIT(CPOL))
  345. spi_writel(as, CSR0 + 4 * i,
  346. csr ^ SPI_BIT(CPOL));
  347. }
  348. mr = spi_readl(as, MR);
  349. mr = SPI_BFINS(PCS, ~(1 << spi->chip_select), mr);
  350. if (as->use_cs_gpios && spi->chip_select != 0)
  351. gpio_set_value(asd->npcs_pin, active);
  352. spi_writel(as, MR, mr);
  353. }
  354. dev_dbg(&spi->dev, "activate %u%s, mr %08x\n",
  355. asd->npcs_pin, active ? " (high)" : "",
  356. mr);
  357. }
  358. static void cs_deactivate(struct atmel_spi *as, struct spi_device *spi)
  359. {
  360. struct atmel_spi_device *asd = spi->controller_state;
  361. unsigned active = spi->mode & SPI_CS_HIGH;
  362. u32 mr;
  363. /* only deactivate *this* device; sometimes transfers to
  364. * another device may be active when this routine is called.
  365. */
  366. mr = spi_readl(as, MR);
  367. if (~SPI_BFEXT(PCS, mr) & (1 << spi->chip_select)) {
  368. mr = SPI_BFINS(PCS, 0xf, mr);
  369. spi_writel(as, MR, mr);
  370. }
  371. dev_dbg(&spi->dev, "DEactivate %u%s, mr %08x\n",
  372. asd->npcs_pin, active ? " (low)" : "",
  373. mr);
  374. if (!as->use_cs_gpios)
  375. spi_writel(as, CR, SPI_BIT(LASTXFER));
  376. else if (atmel_spi_is_v2(as) || spi->chip_select != 0)
  377. gpio_set_value(asd->npcs_pin, !active);
  378. }
  379. static void atmel_spi_lock(struct atmel_spi *as) __acquires(&as->lock)
  380. {
  381. spin_lock_irqsave(&as->lock, as->flags);
  382. }
  383. static void atmel_spi_unlock(struct atmel_spi *as) __releases(&as->lock)
  384. {
  385. spin_unlock_irqrestore(&as->lock, as->flags);
  386. }
  387. static inline bool atmel_spi_use_dma(struct atmel_spi *as,
  388. struct spi_transfer *xfer)
  389. {
  390. return as->use_dma && xfer->len >= DMA_MIN_BYTES;
  391. }
  392. static bool atmel_spi_can_dma(struct spi_master *master,
  393. struct spi_device *spi,
  394. struct spi_transfer *xfer)
  395. {
  396. struct atmel_spi *as = spi_master_get_devdata(master);
  397. return atmel_spi_use_dma(as, xfer);
  398. }
  399. static int atmel_spi_dma_slave_config(struct atmel_spi *as,
  400. struct dma_slave_config *slave_config,
  401. u8 bits_per_word)
  402. {
  403. struct spi_master *master = platform_get_drvdata(as->pdev);
  404. int err = 0;
  405. if (bits_per_word > 8) {
  406. slave_config->dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  407. slave_config->src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  408. } else {
  409. slave_config->dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  410. slave_config->src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  411. }
  412. slave_config->dst_addr = (dma_addr_t)as->phybase + SPI_TDR;
  413. slave_config->src_addr = (dma_addr_t)as->phybase + SPI_RDR;
  414. slave_config->src_maxburst = 1;
  415. slave_config->dst_maxburst = 1;
  416. slave_config->device_fc = false;
  417. /*
  418. * This driver uses fixed peripheral select mode (PS bit set to '0' in
  419. * the Mode Register).
  420. * So according to the datasheet, when FIFOs are available (and
  421. * enabled), the Transmit FIFO operates in Multiple Data Mode.
  422. * In this mode, up to 2 data, not 4, can be written into the Transmit
  423. * Data Register in a single access.
  424. * However, the first data has to be written into the lowest 16 bits and
  425. * the second data into the highest 16 bits of the Transmit
  426. * Data Register. For 8bit data (the most frequent case), it would
  427. * require to rework tx_buf so each data would actualy fit 16 bits.
  428. * So we'd rather write only one data at the time. Hence the transmit
  429. * path works the same whether FIFOs are available (and enabled) or not.
  430. */
  431. slave_config->direction = DMA_MEM_TO_DEV;
  432. if (dmaengine_slave_config(master->dma_tx, slave_config)) {
  433. dev_err(&as->pdev->dev,
  434. "failed to configure tx dma channel\n");
  435. err = -EINVAL;
  436. }
  437. /*
  438. * This driver configures the spi controller for master mode (MSTR bit
  439. * set to '1' in the Mode Register).
  440. * So according to the datasheet, when FIFOs are available (and
  441. * enabled), the Receive FIFO operates in Single Data Mode.
  442. * So the receive path works the same whether FIFOs are available (and
  443. * enabled) or not.
  444. */
  445. slave_config->direction = DMA_DEV_TO_MEM;
  446. if (dmaengine_slave_config(master->dma_rx, slave_config)) {
  447. dev_err(&as->pdev->dev,
  448. "failed to configure rx dma channel\n");
  449. err = -EINVAL;
  450. }
  451. return err;
  452. }
  453. static int atmel_spi_configure_dma(struct spi_master *master,
  454. struct atmel_spi *as)
  455. {
  456. struct dma_slave_config slave_config;
  457. struct device *dev = &as->pdev->dev;
  458. int err;
  459. dma_cap_mask_t mask;
  460. dma_cap_zero(mask);
  461. dma_cap_set(DMA_SLAVE, mask);
  462. master->dma_tx = dma_request_slave_channel_reason(dev, "tx");
  463. if (IS_ERR(master->dma_tx)) {
  464. err = PTR_ERR(master->dma_tx);
  465. if (err == -EPROBE_DEFER) {
  466. dev_warn(dev, "no DMA channel available at the moment\n");
  467. goto error_clear;
  468. }
  469. dev_err(dev,
  470. "DMA TX channel not available, SPI unable to use DMA\n");
  471. err = -EBUSY;
  472. goto error_clear;
  473. }
  474. /*
  475. * No reason to check EPROBE_DEFER here since we have already requested
  476. * tx channel. If it fails here, it's for another reason.
  477. */
  478. master->dma_rx = dma_request_slave_channel(dev, "rx");
  479. if (!master->dma_rx) {
  480. dev_err(dev,
  481. "DMA RX channel not available, SPI unable to use DMA\n");
  482. err = -EBUSY;
  483. goto error;
  484. }
  485. err = atmel_spi_dma_slave_config(as, &slave_config, 8);
  486. if (err)
  487. goto error;
  488. dev_info(&as->pdev->dev,
  489. "Using %s (tx) and %s (rx) for DMA transfers\n",
  490. dma_chan_name(master->dma_tx),
  491. dma_chan_name(master->dma_rx));
  492. return 0;
  493. error:
  494. if (master->dma_rx)
  495. dma_release_channel(master->dma_rx);
  496. if (!IS_ERR(master->dma_tx))
  497. dma_release_channel(master->dma_tx);
  498. error_clear:
  499. master->dma_tx = master->dma_rx = NULL;
  500. return err;
  501. }
  502. static void atmel_spi_stop_dma(struct spi_master *master)
  503. {
  504. if (master->dma_rx)
  505. dmaengine_terminate_all(master->dma_rx);
  506. if (master->dma_tx)
  507. dmaengine_terminate_all(master->dma_tx);
  508. }
  509. static void atmel_spi_release_dma(struct spi_master *master)
  510. {
  511. if (master->dma_rx) {
  512. dma_release_channel(master->dma_rx);
  513. master->dma_rx = NULL;
  514. }
  515. if (master->dma_tx) {
  516. dma_release_channel(master->dma_tx);
  517. master->dma_tx = NULL;
  518. }
  519. }
  520. /* This function is called by the DMA driver from tasklet context */
  521. static void dma_callback(void *data)
  522. {
  523. struct spi_master *master = data;
  524. struct atmel_spi *as = spi_master_get_devdata(master);
  525. complete(&as->xfer_completion);
  526. }
  527. /*
  528. * Next transfer using PIO without FIFO.
  529. */
  530. static void atmel_spi_next_xfer_single(struct spi_master *master,
  531. struct spi_transfer *xfer)
  532. {
  533. struct atmel_spi *as = spi_master_get_devdata(master);
  534. unsigned long xfer_pos = xfer->len - as->current_remaining_bytes;
  535. dev_vdbg(master->dev.parent, "atmel_spi_next_xfer_pio\n");
  536. /* Make sure data is not remaining in RDR */
  537. spi_readl(as, RDR);
  538. while (spi_readl(as, SR) & SPI_BIT(RDRF)) {
  539. spi_readl(as, RDR);
  540. cpu_relax();
  541. }
  542. if (xfer->bits_per_word > 8)
  543. spi_writel(as, TDR, *(u16 *)(xfer->tx_buf + xfer_pos));
  544. else
  545. spi_writel(as, TDR, *(u8 *)(xfer->tx_buf + xfer_pos));
  546. dev_dbg(master->dev.parent,
  547. " start pio xfer %p: len %u tx %p rx %p bitpw %d\n",
  548. xfer, xfer->len, xfer->tx_buf, xfer->rx_buf,
  549. xfer->bits_per_word);
  550. /* Enable relevant interrupts */
  551. spi_writel(as, IER, SPI_BIT(RDRF) | SPI_BIT(OVRES));
  552. }
  553. /*
  554. * Next transfer using PIO with FIFO.
  555. */
  556. static void atmel_spi_next_xfer_fifo(struct spi_master *master,
  557. struct spi_transfer *xfer)
  558. {
  559. struct atmel_spi *as = spi_master_get_devdata(master);
  560. u32 current_remaining_data, num_data;
  561. u32 offset = xfer->len - as->current_remaining_bytes;
  562. const u16 *words = (const u16 *)((u8 *)xfer->tx_buf + offset);
  563. const u8 *bytes = (const u8 *)((u8 *)xfer->tx_buf + offset);
  564. u16 td0, td1;
  565. u32 fifomr;
  566. dev_vdbg(master->dev.parent, "atmel_spi_next_xfer_fifo\n");
  567. /* Compute the number of data to transfer in the current iteration */
  568. current_remaining_data = ((xfer->bits_per_word > 8) ?
  569. ((u32)as->current_remaining_bytes >> 1) :
  570. (u32)as->current_remaining_bytes);
  571. num_data = min(current_remaining_data, as->fifo_size);
  572. /* Flush RX and TX FIFOs */
  573. spi_writel(as, CR, SPI_BIT(RXFCLR) | SPI_BIT(TXFCLR));
  574. while (spi_readl(as, FLR))
  575. cpu_relax();
  576. /* Set RX FIFO Threshold to the number of data to transfer */
  577. fifomr = spi_readl(as, FMR);
  578. spi_writel(as, FMR, SPI_BFINS(RXFTHRES, num_data, fifomr));
  579. /* Clear FIFO flags in the Status Register, especially RXFTHF */
  580. (void)spi_readl(as, SR);
  581. /* Fill TX FIFO */
  582. while (num_data >= 2) {
  583. if (xfer->bits_per_word > 8) {
  584. td0 = *words++;
  585. td1 = *words++;
  586. } else {
  587. td0 = *bytes++;
  588. td1 = *bytes++;
  589. }
  590. spi_writel(as, TDR, (td1 << 16) | td0);
  591. num_data -= 2;
  592. }
  593. if (num_data) {
  594. if (xfer->bits_per_word > 8)
  595. td0 = *words++;
  596. else
  597. td0 = *bytes++;
  598. spi_writew(as, TDR, td0);
  599. num_data--;
  600. }
  601. dev_dbg(master->dev.parent,
  602. " start fifo xfer %p: len %u tx %p rx %p bitpw %d\n",
  603. xfer, xfer->len, xfer->tx_buf, xfer->rx_buf,
  604. xfer->bits_per_word);
  605. /*
  606. * Enable RX FIFO Threshold Flag interrupt to be notified about
  607. * transfer completion.
  608. */
  609. spi_writel(as, IER, SPI_BIT(RXFTHF) | SPI_BIT(OVRES));
  610. }
  611. /*
  612. * Next transfer using PIO.
  613. */
  614. static void atmel_spi_next_xfer_pio(struct spi_master *master,
  615. struct spi_transfer *xfer)
  616. {
  617. struct atmel_spi *as = spi_master_get_devdata(master);
  618. if (as->fifo_size)
  619. atmel_spi_next_xfer_fifo(master, xfer);
  620. else
  621. atmel_spi_next_xfer_single(master, xfer);
  622. }
  623. /*
  624. * Submit next transfer for DMA.
  625. */
  626. static int atmel_spi_next_xfer_dma_submit(struct spi_master *master,
  627. struct spi_transfer *xfer,
  628. u32 *plen)
  629. {
  630. struct atmel_spi *as = spi_master_get_devdata(master);
  631. struct dma_chan *rxchan = master->dma_rx;
  632. struct dma_chan *txchan = master->dma_tx;
  633. struct dma_async_tx_descriptor *rxdesc;
  634. struct dma_async_tx_descriptor *txdesc;
  635. struct dma_slave_config slave_config;
  636. dma_cookie_t cookie;
  637. dev_vdbg(master->dev.parent, "atmel_spi_next_xfer_dma_submit\n");
  638. /* Check that the channels are available */
  639. if (!rxchan || !txchan)
  640. return -ENODEV;
  641. /* release lock for DMA operations */
  642. atmel_spi_unlock(as);
  643. *plen = xfer->len;
  644. if (atmel_spi_dma_slave_config(as, &slave_config,
  645. xfer->bits_per_word))
  646. goto err_exit;
  647. /* Send both scatterlists */
  648. rxdesc = dmaengine_prep_slave_sg(rxchan,
  649. xfer->rx_sg.sgl, xfer->rx_sg.nents,
  650. DMA_FROM_DEVICE,
  651. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  652. if (!rxdesc)
  653. goto err_dma;
  654. txdesc = dmaengine_prep_slave_sg(txchan,
  655. xfer->tx_sg.sgl, xfer->tx_sg.nents,
  656. DMA_TO_DEVICE,
  657. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  658. if (!txdesc)
  659. goto err_dma;
  660. dev_dbg(master->dev.parent,
  661. " start dma xfer %p: len %u tx %p/%08llx rx %p/%08llx\n",
  662. xfer, xfer->len, xfer->tx_buf, (unsigned long long)xfer->tx_dma,
  663. xfer->rx_buf, (unsigned long long)xfer->rx_dma);
  664. /* Enable relevant interrupts */
  665. spi_writel(as, IER, SPI_BIT(OVRES));
  666. /* Put the callback on the RX transfer only, that should finish last */
  667. rxdesc->callback = dma_callback;
  668. rxdesc->callback_param = master;
  669. /* Submit and fire RX and TX with TX last so we're ready to read! */
  670. cookie = rxdesc->tx_submit(rxdesc);
  671. if (dma_submit_error(cookie))
  672. goto err_dma;
  673. cookie = txdesc->tx_submit(txdesc);
  674. if (dma_submit_error(cookie))
  675. goto err_dma;
  676. rxchan->device->device_issue_pending(rxchan);
  677. txchan->device->device_issue_pending(txchan);
  678. /* take back lock */
  679. atmel_spi_lock(as);
  680. return 0;
  681. err_dma:
  682. spi_writel(as, IDR, SPI_BIT(OVRES));
  683. atmel_spi_stop_dma(master);
  684. err_exit:
  685. atmel_spi_lock(as);
  686. return -ENOMEM;
  687. }
  688. static void atmel_spi_next_xfer_data(struct spi_master *master,
  689. struct spi_transfer *xfer,
  690. dma_addr_t *tx_dma,
  691. dma_addr_t *rx_dma,
  692. u32 *plen)
  693. {
  694. *rx_dma = xfer->rx_dma + xfer->len - *plen;
  695. *tx_dma = xfer->tx_dma + xfer->len - *plen;
  696. if (*plen > master->max_dma_len)
  697. *plen = master->max_dma_len;
  698. }
  699. static int atmel_spi_set_xfer_speed(struct atmel_spi *as,
  700. struct spi_device *spi,
  701. struct spi_transfer *xfer)
  702. {
  703. u32 scbr, csr;
  704. unsigned long bus_hz;
  705. /* v1 chips start out at half the peripheral bus speed. */
  706. bus_hz = as->spi_clk;
  707. if (!atmel_spi_is_v2(as))
  708. bus_hz /= 2;
  709. /*
  710. * Calculate the lowest divider that satisfies the
  711. * constraint, assuming div32/fdiv/mbz == 0.
  712. */
  713. scbr = DIV_ROUND_UP(bus_hz, xfer->speed_hz);
  714. /*
  715. * If the resulting divider doesn't fit into the
  716. * register bitfield, we can't satisfy the constraint.
  717. */
  718. if (scbr >= (1 << SPI_SCBR_SIZE)) {
  719. dev_err(&spi->dev,
  720. "setup: %d Hz too slow, scbr %u; min %ld Hz\n",
  721. xfer->speed_hz, scbr, bus_hz/255);
  722. return -EINVAL;
  723. }
  724. if (scbr == 0) {
  725. dev_err(&spi->dev,
  726. "setup: %d Hz too high, scbr %u; max %ld Hz\n",
  727. xfer->speed_hz, scbr, bus_hz);
  728. return -EINVAL;
  729. }
  730. csr = spi_readl(as, CSR0 + 4 * spi->chip_select);
  731. csr = SPI_BFINS(SCBR, scbr, csr);
  732. spi_writel(as, CSR0 + 4 * spi->chip_select, csr);
  733. return 0;
  734. }
  735. /*
  736. * Submit next transfer for PDC.
  737. * lock is held, spi irq is blocked
  738. */
  739. static void atmel_spi_pdc_next_xfer(struct spi_master *master,
  740. struct spi_message *msg,
  741. struct spi_transfer *xfer)
  742. {
  743. struct atmel_spi *as = spi_master_get_devdata(master);
  744. u32 len;
  745. dma_addr_t tx_dma, rx_dma;
  746. spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
  747. len = as->current_remaining_bytes;
  748. atmel_spi_next_xfer_data(master, xfer, &tx_dma, &rx_dma, &len);
  749. as->current_remaining_bytes -= len;
  750. spi_writel(as, RPR, rx_dma);
  751. spi_writel(as, TPR, tx_dma);
  752. if (msg->spi->bits_per_word > 8)
  753. len >>= 1;
  754. spi_writel(as, RCR, len);
  755. spi_writel(as, TCR, len);
  756. dev_dbg(&msg->spi->dev,
  757. " start xfer %p: len %u tx %p/%08llx rx %p/%08llx\n",
  758. xfer, xfer->len, xfer->tx_buf,
  759. (unsigned long long)xfer->tx_dma, xfer->rx_buf,
  760. (unsigned long long)xfer->rx_dma);
  761. if (as->current_remaining_bytes) {
  762. len = as->current_remaining_bytes;
  763. atmel_spi_next_xfer_data(master, xfer, &tx_dma, &rx_dma, &len);
  764. as->current_remaining_bytes -= len;
  765. spi_writel(as, RNPR, rx_dma);
  766. spi_writel(as, TNPR, tx_dma);
  767. if (msg->spi->bits_per_word > 8)
  768. len >>= 1;
  769. spi_writel(as, RNCR, len);
  770. spi_writel(as, TNCR, len);
  771. dev_dbg(&msg->spi->dev,
  772. " next xfer %p: len %u tx %p/%08llx rx %p/%08llx\n",
  773. xfer, xfer->len, xfer->tx_buf,
  774. (unsigned long long)xfer->tx_dma, xfer->rx_buf,
  775. (unsigned long long)xfer->rx_dma);
  776. }
  777. /* REVISIT: We're waiting for RXBUFF before we start the next
  778. * transfer because we need to handle some difficult timing
  779. * issues otherwise. If we wait for TXBUFE in one transfer and
  780. * then starts waiting for RXBUFF in the next, it's difficult
  781. * to tell the difference between the RXBUFF interrupt we're
  782. * actually waiting for and the RXBUFF interrupt of the
  783. * previous transfer.
  784. *
  785. * It should be doable, though. Just not now...
  786. */
  787. spi_writel(as, IER, SPI_BIT(RXBUFF) | SPI_BIT(OVRES));
  788. spi_writel(as, PTCR, SPI_BIT(TXTEN) | SPI_BIT(RXTEN));
  789. }
  790. /*
  791. * For DMA, tx_buf/tx_dma have the same relationship as rx_buf/rx_dma:
  792. * - The buffer is either valid for CPU access, else NULL
  793. * - If the buffer is valid, so is its DMA address
  794. *
  795. * This driver manages the dma address unless message->is_dma_mapped.
  796. */
  797. static int
  798. atmel_spi_dma_map_xfer(struct atmel_spi *as, struct spi_transfer *xfer)
  799. {
  800. struct device *dev = &as->pdev->dev;
  801. xfer->tx_dma = xfer->rx_dma = INVALID_DMA_ADDRESS;
  802. if (xfer->tx_buf) {
  803. /* tx_buf is a const void* where we need a void * for the dma
  804. * mapping */
  805. void *nonconst_tx = (void *)xfer->tx_buf;
  806. xfer->tx_dma = dma_map_single(dev,
  807. nonconst_tx, xfer->len,
  808. DMA_TO_DEVICE);
  809. if (dma_mapping_error(dev, xfer->tx_dma))
  810. return -ENOMEM;
  811. }
  812. if (xfer->rx_buf) {
  813. xfer->rx_dma = dma_map_single(dev,
  814. xfer->rx_buf, xfer->len,
  815. DMA_FROM_DEVICE);
  816. if (dma_mapping_error(dev, xfer->rx_dma)) {
  817. if (xfer->tx_buf)
  818. dma_unmap_single(dev,
  819. xfer->tx_dma, xfer->len,
  820. DMA_TO_DEVICE);
  821. return -ENOMEM;
  822. }
  823. }
  824. return 0;
  825. }
  826. static void atmel_spi_dma_unmap_xfer(struct spi_master *master,
  827. struct spi_transfer *xfer)
  828. {
  829. if (xfer->tx_dma != INVALID_DMA_ADDRESS)
  830. dma_unmap_single(master->dev.parent, xfer->tx_dma,
  831. xfer->len, DMA_TO_DEVICE);
  832. if (xfer->rx_dma != INVALID_DMA_ADDRESS)
  833. dma_unmap_single(master->dev.parent, xfer->rx_dma,
  834. xfer->len, DMA_FROM_DEVICE);
  835. }
  836. static void atmel_spi_disable_pdc_transfer(struct atmel_spi *as)
  837. {
  838. spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
  839. }
  840. static void
  841. atmel_spi_pump_single_data(struct atmel_spi *as, struct spi_transfer *xfer)
  842. {
  843. u8 *rxp;
  844. u16 *rxp16;
  845. unsigned long xfer_pos = xfer->len - as->current_remaining_bytes;
  846. if (xfer->bits_per_word > 8) {
  847. rxp16 = (u16 *)(((u8 *)xfer->rx_buf) + xfer_pos);
  848. *rxp16 = spi_readl(as, RDR);
  849. } else {
  850. rxp = ((u8 *)xfer->rx_buf) + xfer_pos;
  851. *rxp = spi_readl(as, RDR);
  852. }
  853. if (xfer->bits_per_word > 8) {
  854. if (as->current_remaining_bytes > 2)
  855. as->current_remaining_bytes -= 2;
  856. else
  857. as->current_remaining_bytes = 0;
  858. } else {
  859. as->current_remaining_bytes--;
  860. }
  861. }
  862. static void
  863. atmel_spi_pump_fifo_data(struct atmel_spi *as, struct spi_transfer *xfer)
  864. {
  865. u32 fifolr = spi_readl(as, FLR);
  866. u32 num_bytes, num_data = SPI_BFEXT(RXFL, fifolr);
  867. u32 offset = xfer->len - as->current_remaining_bytes;
  868. u16 *words = (u16 *)((u8 *)xfer->rx_buf + offset);
  869. u8 *bytes = (u8 *)((u8 *)xfer->rx_buf + offset);
  870. u16 rd; /* RD field is the lowest 16 bits of RDR */
  871. /* Update the number of remaining bytes to transfer */
  872. num_bytes = ((xfer->bits_per_word > 8) ?
  873. (num_data << 1) :
  874. num_data);
  875. if (as->current_remaining_bytes > num_bytes)
  876. as->current_remaining_bytes -= num_bytes;
  877. else
  878. as->current_remaining_bytes = 0;
  879. /* Handle odd number of bytes when data are more than 8bit width */
  880. if (xfer->bits_per_word > 8)
  881. as->current_remaining_bytes &= ~0x1;
  882. /* Read data */
  883. while (num_data) {
  884. rd = spi_readl(as, RDR);
  885. if (xfer->bits_per_word > 8)
  886. *words++ = rd;
  887. else
  888. *bytes++ = rd;
  889. num_data--;
  890. }
  891. }
  892. /* Called from IRQ
  893. *
  894. * Must update "current_remaining_bytes" to keep track of data
  895. * to transfer.
  896. */
  897. static void
  898. atmel_spi_pump_pio_data(struct atmel_spi *as, struct spi_transfer *xfer)
  899. {
  900. if (as->fifo_size)
  901. atmel_spi_pump_fifo_data(as, xfer);
  902. else
  903. atmel_spi_pump_single_data(as, xfer);
  904. }
  905. /* Interrupt
  906. *
  907. * No need for locking in this Interrupt handler: done_status is the
  908. * only information modified.
  909. */
  910. static irqreturn_t
  911. atmel_spi_pio_interrupt(int irq, void *dev_id)
  912. {
  913. struct spi_master *master = dev_id;
  914. struct atmel_spi *as = spi_master_get_devdata(master);
  915. u32 status, pending, imr;
  916. struct spi_transfer *xfer;
  917. int ret = IRQ_NONE;
  918. imr = spi_readl(as, IMR);
  919. status = spi_readl(as, SR);
  920. pending = status & imr;
  921. if (pending & SPI_BIT(OVRES)) {
  922. ret = IRQ_HANDLED;
  923. spi_writel(as, IDR, SPI_BIT(OVRES));
  924. dev_warn(master->dev.parent, "overrun\n");
  925. /*
  926. * When we get an overrun, we disregard the current
  927. * transfer. Data will not be copied back from any
  928. * bounce buffer and msg->actual_len will not be
  929. * updated with the last xfer.
  930. *
  931. * We will also not process any remaning transfers in
  932. * the message.
  933. */
  934. as->done_status = -EIO;
  935. smp_wmb();
  936. /* Clear any overrun happening while cleaning up */
  937. spi_readl(as, SR);
  938. complete(&as->xfer_completion);
  939. } else if (pending & (SPI_BIT(RDRF) | SPI_BIT(RXFTHF))) {
  940. atmel_spi_lock(as);
  941. if (as->current_remaining_bytes) {
  942. ret = IRQ_HANDLED;
  943. xfer = as->current_transfer;
  944. atmel_spi_pump_pio_data(as, xfer);
  945. if (!as->current_remaining_bytes)
  946. spi_writel(as, IDR, pending);
  947. complete(&as->xfer_completion);
  948. }
  949. atmel_spi_unlock(as);
  950. } else {
  951. WARN_ONCE(pending, "IRQ not handled, pending = %x\n", pending);
  952. ret = IRQ_HANDLED;
  953. spi_writel(as, IDR, pending);
  954. }
  955. return ret;
  956. }
  957. static irqreturn_t
  958. atmel_spi_pdc_interrupt(int irq, void *dev_id)
  959. {
  960. struct spi_master *master = dev_id;
  961. struct atmel_spi *as = spi_master_get_devdata(master);
  962. u32 status, pending, imr;
  963. int ret = IRQ_NONE;
  964. imr = spi_readl(as, IMR);
  965. status = spi_readl(as, SR);
  966. pending = status & imr;
  967. if (pending & SPI_BIT(OVRES)) {
  968. ret = IRQ_HANDLED;
  969. spi_writel(as, IDR, (SPI_BIT(RXBUFF) | SPI_BIT(ENDRX)
  970. | SPI_BIT(OVRES)));
  971. /* Clear any overrun happening while cleaning up */
  972. spi_readl(as, SR);
  973. as->done_status = -EIO;
  974. complete(&as->xfer_completion);
  975. } else if (pending & (SPI_BIT(RXBUFF) | SPI_BIT(ENDRX))) {
  976. ret = IRQ_HANDLED;
  977. spi_writel(as, IDR, pending);
  978. complete(&as->xfer_completion);
  979. }
  980. return ret;
  981. }
  982. static int atmel_spi_setup(struct spi_device *spi)
  983. {
  984. struct atmel_spi *as;
  985. struct atmel_spi_device *asd;
  986. u32 csr;
  987. unsigned int bits = spi->bits_per_word;
  988. unsigned int npcs_pin;
  989. as = spi_master_get_devdata(spi->master);
  990. /* see notes above re chipselect */
  991. if (!atmel_spi_is_v2(as)
  992. && spi->chip_select == 0
  993. && (spi->mode & SPI_CS_HIGH)) {
  994. dev_dbg(&spi->dev, "setup: can't be active-high\n");
  995. return -EINVAL;
  996. }
  997. csr = SPI_BF(BITS, bits - 8);
  998. if (spi->mode & SPI_CPOL)
  999. csr |= SPI_BIT(CPOL);
  1000. if (!(spi->mode & SPI_CPHA))
  1001. csr |= SPI_BIT(NCPHA);
  1002. if (!as->use_cs_gpios)
  1003. csr |= SPI_BIT(CSAAT);
  1004. /* DLYBS is mostly irrelevant since we manage chipselect using GPIOs.
  1005. *
  1006. * DLYBCT would add delays between words, slowing down transfers.
  1007. * It could potentially be useful to cope with DMA bottlenecks, but
  1008. * in those cases it's probably best to just use a lower bitrate.
  1009. */
  1010. csr |= SPI_BF(DLYBS, 0);
  1011. csr |= SPI_BF(DLYBCT, 0);
  1012. /* chipselect must have been muxed as GPIO (e.g. in board setup) */
  1013. npcs_pin = (unsigned long)spi->controller_data;
  1014. if (!as->use_cs_gpios)
  1015. npcs_pin = spi->chip_select;
  1016. else if (gpio_is_valid(spi->cs_gpio))
  1017. npcs_pin = spi->cs_gpio;
  1018. asd = spi->controller_state;
  1019. if (!asd) {
  1020. asd = kzalloc(sizeof(struct atmel_spi_device), GFP_KERNEL);
  1021. if (!asd)
  1022. return -ENOMEM;
  1023. if (as->use_cs_gpios)
  1024. gpio_direction_output(npcs_pin,
  1025. !(spi->mode & SPI_CS_HIGH));
  1026. asd->npcs_pin = npcs_pin;
  1027. spi->controller_state = asd;
  1028. }
  1029. asd->csr = csr;
  1030. dev_dbg(&spi->dev,
  1031. "setup: bpw %u mode 0x%x -> csr%d %08x\n",
  1032. bits, spi->mode, spi->chip_select, csr);
  1033. if (!atmel_spi_is_v2(as))
  1034. spi_writel(as, CSR0 + 4 * spi->chip_select, csr);
  1035. return 0;
  1036. }
  1037. static int atmel_spi_one_transfer(struct spi_master *master,
  1038. struct spi_message *msg,
  1039. struct spi_transfer *xfer)
  1040. {
  1041. struct atmel_spi *as;
  1042. struct spi_device *spi = msg->spi;
  1043. u8 bits;
  1044. u32 len;
  1045. struct atmel_spi_device *asd;
  1046. int timeout;
  1047. int ret;
  1048. unsigned long dma_timeout;
  1049. as = spi_master_get_devdata(master);
  1050. if (!(xfer->tx_buf || xfer->rx_buf) && xfer->len) {
  1051. dev_dbg(&spi->dev, "missing rx or tx buf\n");
  1052. return -EINVAL;
  1053. }
  1054. asd = spi->controller_state;
  1055. bits = (asd->csr >> 4) & 0xf;
  1056. if (bits != xfer->bits_per_word - 8) {
  1057. dev_dbg(&spi->dev,
  1058. "you can't yet change bits_per_word in transfers\n");
  1059. return -ENOPROTOOPT;
  1060. }
  1061. /*
  1062. * DMA map early, for performance (empties dcache ASAP) and
  1063. * better fault reporting.
  1064. */
  1065. if ((!msg->is_dma_mapped)
  1066. && as->use_pdc) {
  1067. if (atmel_spi_dma_map_xfer(as, xfer) < 0)
  1068. return -ENOMEM;
  1069. }
  1070. atmel_spi_set_xfer_speed(as, msg->spi, xfer);
  1071. as->done_status = 0;
  1072. as->current_transfer = xfer;
  1073. as->current_remaining_bytes = xfer->len;
  1074. while (as->current_remaining_bytes) {
  1075. reinit_completion(&as->xfer_completion);
  1076. if (as->use_pdc) {
  1077. atmel_spi_pdc_next_xfer(master, msg, xfer);
  1078. } else if (atmel_spi_use_dma(as, xfer)) {
  1079. len = as->current_remaining_bytes;
  1080. ret = atmel_spi_next_xfer_dma_submit(master,
  1081. xfer, &len);
  1082. if (ret) {
  1083. dev_err(&spi->dev,
  1084. "unable to use DMA, fallback to PIO\n");
  1085. atmel_spi_next_xfer_pio(master, xfer);
  1086. } else {
  1087. as->current_remaining_bytes -= len;
  1088. if (as->current_remaining_bytes < 0)
  1089. as->current_remaining_bytes = 0;
  1090. }
  1091. } else {
  1092. atmel_spi_next_xfer_pio(master, xfer);
  1093. }
  1094. /* interrupts are disabled, so free the lock for schedule */
  1095. atmel_spi_unlock(as);
  1096. dma_timeout = wait_for_completion_timeout(&as->xfer_completion,
  1097. SPI_DMA_TIMEOUT);
  1098. atmel_spi_lock(as);
  1099. if (WARN_ON(dma_timeout == 0)) {
  1100. dev_err(&spi->dev, "spi transfer timeout\n");
  1101. as->done_status = -EIO;
  1102. }
  1103. if (as->done_status)
  1104. break;
  1105. }
  1106. if (as->done_status) {
  1107. if (as->use_pdc) {
  1108. dev_warn(master->dev.parent,
  1109. "overrun (%u/%u remaining)\n",
  1110. spi_readl(as, TCR), spi_readl(as, RCR));
  1111. /*
  1112. * Clean up DMA registers and make sure the data
  1113. * registers are empty.
  1114. */
  1115. spi_writel(as, RNCR, 0);
  1116. spi_writel(as, TNCR, 0);
  1117. spi_writel(as, RCR, 0);
  1118. spi_writel(as, TCR, 0);
  1119. for (timeout = 1000; timeout; timeout--)
  1120. if (spi_readl(as, SR) & SPI_BIT(TXEMPTY))
  1121. break;
  1122. if (!timeout)
  1123. dev_warn(master->dev.parent,
  1124. "timeout waiting for TXEMPTY");
  1125. while (spi_readl(as, SR) & SPI_BIT(RDRF))
  1126. spi_readl(as, RDR);
  1127. /* Clear any overrun happening while cleaning up */
  1128. spi_readl(as, SR);
  1129. } else if (atmel_spi_use_dma(as, xfer)) {
  1130. atmel_spi_stop_dma(master);
  1131. }
  1132. if (!msg->is_dma_mapped
  1133. && as->use_pdc)
  1134. atmel_spi_dma_unmap_xfer(master, xfer);
  1135. return 0;
  1136. } else {
  1137. /* only update length if no error */
  1138. msg->actual_length += xfer->len;
  1139. }
  1140. if (!msg->is_dma_mapped
  1141. && as->use_pdc)
  1142. atmel_spi_dma_unmap_xfer(master, xfer);
  1143. if (xfer->delay_usecs)
  1144. udelay(xfer->delay_usecs);
  1145. if (xfer->cs_change) {
  1146. if (list_is_last(&xfer->transfer_list,
  1147. &msg->transfers)) {
  1148. as->keep_cs = true;
  1149. } else {
  1150. as->cs_active = !as->cs_active;
  1151. if (as->cs_active)
  1152. cs_activate(as, msg->spi);
  1153. else
  1154. cs_deactivate(as, msg->spi);
  1155. }
  1156. }
  1157. return 0;
  1158. }
  1159. static int atmel_spi_transfer_one_message(struct spi_master *master,
  1160. struct spi_message *msg)
  1161. {
  1162. struct atmel_spi *as;
  1163. struct spi_transfer *xfer;
  1164. struct spi_device *spi = msg->spi;
  1165. int ret = 0;
  1166. as = spi_master_get_devdata(master);
  1167. dev_dbg(&spi->dev, "new message %p submitted for %s\n",
  1168. msg, dev_name(&spi->dev));
  1169. atmel_spi_lock(as);
  1170. cs_activate(as, spi);
  1171. as->cs_active = true;
  1172. as->keep_cs = false;
  1173. msg->status = 0;
  1174. msg->actual_length = 0;
  1175. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  1176. ret = atmel_spi_one_transfer(master, msg, xfer);
  1177. if (ret)
  1178. goto msg_done;
  1179. }
  1180. if (as->use_pdc)
  1181. atmel_spi_disable_pdc_transfer(as);
  1182. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  1183. dev_dbg(&spi->dev,
  1184. " xfer %p: len %u tx %p/%pad rx %p/%pad\n",
  1185. xfer, xfer->len,
  1186. xfer->tx_buf, &xfer->tx_dma,
  1187. xfer->rx_buf, &xfer->rx_dma);
  1188. }
  1189. msg_done:
  1190. if (!as->keep_cs)
  1191. cs_deactivate(as, msg->spi);
  1192. atmel_spi_unlock(as);
  1193. msg->status = as->done_status;
  1194. spi_finalize_current_message(spi->master);
  1195. return ret;
  1196. }
  1197. static void atmel_spi_cleanup(struct spi_device *spi)
  1198. {
  1199. struct atmel_spi_device *asd = spi->controller_state;
  1200. if (!asd)
  1201. return;
  1202. spi->controller_state = NULL;
  1203. kfree(asd);
  1204. }
  1205. static inline unsigned int atmel_get_version(struct atmel_spi *as)
  1206. {
  1207. return spi_readl(as, VERSION) & 0x00000fff;
  1208. }
  1209. static void atmel_get_caps(struct atmel_spi *as)
  1210. {
  1211. unsigned int version;
  1212. version = atmel_get_version(as);
  1213. dev_info(&as->pdev->dev, "version: 0x%x\n", version);
  1214. as->caps.is_spi2 = version > 0x121;
  1215. as->caps.has_wdrbt = version >= 0x210;
  1216. as->caps.has_dma_support = version >= 0x212;
  1217. }
  1218. /*-------------------------------------------------------------------------*/
  1219. static int atmel_spi_gpio_cs(struct platform_device *pdev)
  1220. {
  1221. struct spi_master *master = platform_get_drvdata(pdev);
  1222. struct atmel_spi *as = spi_master_get_devdata(master);
  1223. struct device_node *np = master->dev.of_node;
  1224. int i;
  1225. int ret = 0;
  1226. int nb = 0;
  1227. if (!as->use_cs_gpios)
  1228. return 0;
  1229. if (!np)
  1230. return 0;
  1231. nb = of_gpio_named_count(np, "cs-gpios");
  1232. for (i = 0; i < nb; i++) {
  1233. int cs_gpio = of_get_named_gpio(pdev->dev.of_node,
  1234. "cs-gpios", i);
  1235. if (cs_gpio == -EPROBE_DEFER)
  1236. return cs_gpio;
  1237. if (gpio_is_valid(cs_gpio)) {
  1238. ret = devm_gpio_request(&pdev->dev, cs_gpio,
  1239. dev_name(&pdev->dev));
  1240. if (ret)
  1241. return ret;
  1242. }
  1243. }
  1244. return 0;
  1245. }
  1246. static int atmel_spi_probe(struct platform_device *pdev)
  1247. {
  1248. struct resource *regs;
  1249. int irq;
  1250. struct clk *clk;
  1251. int ret;
  1252. struct spi_master *master;
  1253. struct atmel_spi *as;
  1254. /* Select default pin state */
  1255. pinctrl_pm_select_default_state(&pdev->dev);
  1256. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1257. if (!regs)
  1258. return -ENXIO;
  1259. irq = platform_get_irq(pdev, 0);
  1260. if (irq < 0)
  1261. return irq;
  1262. clk = devm_clk_get(&pdev->dev, "spi_clk");
  1263. if (IS_ERR(clk))
  1264. return PTR_ERR(clk);
  1265. /* setup spi core then atmel-specific driver state */
  1266. ret = -ENOMEM;
  1267. master = spi_alloc_master(&pdev->dev, sizeof(*as));
  1268. if (!master)
  1269. goto out_free;
  1270. /* the spi->mode bits understood by this driver: */
  1271. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  1272. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(8, 16);
  1273. master->dev.of_node = pdev->dev.of_node;
  1274. master->bus_num = pdev->id;
  1275. master->num_chipselect = master->dev.of_node ? 0 : 4;
  1276. master->setup = atmel_spi_setup;
  1277. master->flags = (SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX);
  1278. master->transfer_one_message = atmel_spi_transfer_one_message;
  1279. master->cleanup = atmel_spi_cleanup;
  1280. master->auto_runtime_pm = true;
  1281. master->max_dma_len = SPI_MAX_DMA_XFER;
  1282. master->can_dma = atmel_spi_can_dma;
  1283. platform_set_drvdata(pdev, master);
  1284. as = spi_master_get_devdata(master);
  1285. spin_lock_init(&as->lock);
  1286. as->pdev = pdev;
  1287. as->regs = devm_ioremap_resource(&pdev->dev, regs);
  1288. if (IS_ERR(as->regs)) {
  1289. ret = PTR_ERR(as->regs);
  1290. goto out_unmap_regs;
  1291. }
  1292. as->phybase = regs->start;
  1293. as->irq = irq;
  1294. as->clk = clk;
  1295. init_completion(&as->xfer_completion);
  1296. atmel_get_caps(as);
  1297. as->use_cs_gpios = true;
  1298. if (atmel_spi_is_v2(as) &&
  1299. pdev->dev.of_node &&
  1300. !of_get_property(pdev->dev.of_node, "cs-gpios", NULL)) {
  1301. as->use_cs_gpios = false;
  1302. master->num_chipselect = 4;
  1303. }
  1304. ret = atmel_spi_gpio_cs(pdev);
  1305. if (ret)
  1306. goto out_unmap_regs;
  1307. as->use_dma = false;
  1308. as->use_pdc = false;
  1309. if (as->caps.has_dma_support) {
  1310. ret = atmel_spi_configure_dma(master, as);
  1311. if (ret == 0) {
  1312. as->use_dma = true;
  1313. } else if (ret == -EPROBE_DEFER) {
  1314. return ret;
  1315. }
  1316. } else {
  1317. as->use_pdc = true;
  1318. }
  1319. if (as->caps.has_dma_support && !as->use_dma)
  1320. dev_info(&pdev->dev, "Atmel SPI Controller using PIO only\n");
  1321. if (as->use_pdc) {
  1322. ret = devm_request_irq(&pdev->dev, irq, atmel_spi_pdc_interrupt,
  1323. 0, dev_name(&pdev->dev), master);
  1324. } else {
  1325. ret = devm_request_irq(&pdev->dev, irq, atmel_spi_pio_interrupt,
  1326. 0, dev_name(&pdev->dev), master);
  1327. }
  1328. if (ret)
  1329. goto out_unmap_regs;
  1330. /* Initialize the hardware */
  1331. ret = clk_prepare_enable(clk);
  1332. if (ret)
  1333. goto out_free_irq;
  1334. as->spi_clk = clk_get_rate(clk);
  1335. spi_writel(as, CR, SPI_BIT(SWRST));
  1336. spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */
  1337. if (as->caps.has_wdrbt) {
  1338. spi_writel(as, MR, SPI_BIT(WDRBT) | SPI_BIT(MODFDIS)
  1339. | SPI_BIT(MSTR));
  1340. } else {
  1341. spi_writel(as, MR, SPI_BIT(MSTR) | SPI_BIT(MODFDIS));
  1342. }
  1343. if (as->use_pdc)
  1344. spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
  1345. spi_writel(as, CR, SPI_BIT(SPIEN));
  1346. as->fifo_size = 0;
  1347. if (!of_property_read_u32(pdev->dev.of_node, "atmel,fifo-size",
  1348. &as->fifo_size)) {
  1349. dev_info(&pdev->dev, "Using FIFO (%u data)\n", as->fifo_size);
  1350. spi_writel(as, CR, SPI_BIT(FIFOEN));
  1351. }
  1352. pm_runtime_set_autosuspend_delay(&pdev->dev, AUTOSUSPEND_TIMEOUT);
  1353. pm_runtime_use_autosuspend(&pdev->dev);
  1354. pm_runtime_set_active(&pdev->dev);
  1355. pm_runtime_enable(&pdev->dev);
  1356. ret = devm_spi_register_master(&pdev->dev, master);
  1357. if (ret)
  1358. goto out_free_dma;
  1359. /* go! */
  1360. dev_info(&pdev->dev, "Atmel SPI Controller at 0x%08lx (irq %d)\n",
  1361. (unsigned long)regs->start, irq);
  1362. return 0;
  1363. out_free_dma:
  1364. pm_runtime_disable(&pdev->dev);
  1365. pm_runtime_set_suspended(&pdev->dev);
  1366. if (as->use_dma)
  1367. atmel_spi_release_dma(master);
  1368. spi_writel(as, CR, SPI_BIT(SWRST));
  1369. spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */
  1370. clk_disable_unprepare(clk);
  1371. out_free_irq:
  1372. out_unmap_regs:
  1373. out_free:
  1374. spi_master_put(master);
  1375. return ret;
  1376. }
  1377. static int atmel_spi_remove(struct platform_device *pdev)
  1378. {
  1379. struct spi_master *master = platform_get_drvdata(pdev);
  1380. struct atmel_spi *as = spi_master_get_devdata(master);
  1381. pm_runtime_get_sync(&pdev->dev);
  1382. /* reset the hardware and block queue progress */
  1383. spin_lock_irq(&as->lock);
  1384. if (as->use_dma) {
  1385. atmel_spi_stop_dma(master);
  1386. atmel_spi_release_dma(master);
  1387. }
  1388. spi_writel(as, CR, SPI_BIT(SWRST));
  1389. spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */
  1390. spi_readl(as, SR);
  1391. spin_unlock_irq(&as->lock);
  1392. clk_disable_unprepare(as->clk);
  1393. pm_runtime_put_noidle(&pdev->dev);
  1394. pm_runtime_disable(&pdev->dev);
  1395. return 0;
  1396. }
  1397. #ifdef CONFIG_PM
  1398. static int atmel_spi_runtime_suspend(struct device *dev)
  1399. {
  1400. struct spi_master *master = dev_get_drvdata(dev);
  1401. struct atmel_spi *as = spi_master_get_devdata(master);
  1402. clk_disable_unprepare(as->clk);
  1403. pinctrl_pm_select_sleep_state(dev);
  1404. return 0;
  1405. }
  1406. static int atmel_spi_runtime_resume(struct device *dev)
  1407. {
  1408. struct spi_master *master = dev_get_drvdata(dev);
  1409. struct atmel_spi *as = spi_master_get_devdata(master);
  1410. pinctrl_pm_select_default_state(dev);
  1411. return clk_prepare_enable(as->clk);
  1412. }
  1413. #ifdef CONFIG_PM_SLEEP
  1414. static int atmel_spi_suspend(struct device *dev)
  1415. {
  1416. struct spi_master *master = dev_get_drvdata(dev);
  1417. int ret;
  1418. /* Stop the queue running */
  1419. ret = spi_master_suspend(master);
  1420. if (ret) {
  1421. dev_warn(dev, "cannot suspend master\n");
  1422. return ret;
  1423. }
  1424. if (!pm_runtime_suspended(dev))
  1425. atmel_spi_runtime_suspend(dev);
  1426. return 0;
  1427. }
  1428. static int atmel_spi_resume(struct device *dev)
  1429. {
  1430. struct spi_master *master = dev_get_drvdata(dev);
  1431. int ret;
  1432. if (!pm_runtime_suspended(dev)) {
  1433. ret = atmel_spi_runtime_resume(dev);
  1434. if (ret)
  1435. return ret;
  1436. }
  1437. /* Start the queue running */
  1438. ret = spi_master_resume(master);
  1439. if (ret)
  1440. dev_err(dev, "problem starting queue (%d)\n", ret);
  1441. return ret;
  1442. }
  1443. #endif
  1444. static const struct dev_pm_ops atmel_spi_pm_ops = {
  1445. SET_SYSTEM_SLEEP_PM_OPS(atmel_spi_suspend, atmel_spi_resume)
  1446. SET_RUNTIME_PM_OPS(atmel_spi_runtime_suspend,
  1447. atmel_spi_runtime_resume, NULL)
  1448. };
  1449. #define ATMEL_SPI_PM_OPS (&atmel_spi_pm_ops)
  1450. #else
  1451. #define ATMEL_SPI_PM_OPS NULL
  1452. #endif
  1453. #if defined(CONFIG_OF)
  1454. static const struct of_device_id atmel_spi_dt_ids[] = {
  1455. { .compatible = "atmel,at91rm9200-spi" },
  1456. { /* sentinel */ }
  1457. };
  1458. MODULE_DEVICE_TABLE(of, atmel_spi_dt_ids);
  1459. #endif
  1460. static struct platform_driver atmel_spi_driver = {
  1461. .driver = {
  1462. .name = "atmel_spi",
  1463. .pm = ATMEL_SPI_PM_OPS,
  1464. .of_match_table = of_match_ptr(atmel_spi_dt_ids),
  1465. },
  1466. .probe = atmel_spi_probe,
  1467. .remove = atmel_spi_remove,
  1468. };
  1469. module_platform_driver(atmel_spi_driver);
  1470. MODULE_DESCRIPTION("Atmel AT32/AT91 SPI Controller driver");
  1471. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  1472. MODULE_LICENSE("GPL");
  1473. MODULE_ALIAS("platform:atmel_spi");