spi-atmel.c 39 KB

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