spi-atmel.c 43 KB

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