spi-atmel.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731
  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)
  322. {
  323. spin_lock_irqsave(&as->lock, as->flags);
  324. }
  325. static void atmel_spi_unlock(struct atmel_spi *as)
  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/%08x rx %p/%08x\n",
  548. xfer, xfer->len, xfer->tx_buf, xfer->tx_dma,
  549. xfer->rx_buf, 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/%08x rx %p/%08x\n",
  635. xfer, xfer->len, xfer->tx_buf, xfer->tx_dma,
  636. xfer->rx_buf, xfer->rx_dma);
  637. } else {
  638. xfer = as->next_transfer;
  639. remaining = as->next_remaining_bytes;
  640. }
  641. as->current_transfer = xfer;
  642. as->current_remaining_bytes = remaining;
  643. if (remaining > 0)
  644. len = remaining;
  645. else if (!atmel_spi_xfer_is_last(msg, xfer)
  646. && atmel_spi_xfer_can_be_chained(xfer)) {
  647. xfer = list_entry(xfer->transfer_list.next,
  648. struct spi_transfer, transfer_list);
  649. len = xfer->len;
  650. } else
  651. xfer = NULL;
  652. as->next_transfer = xfer;
  653. if (xfer) {
  654. u32 total;
  655. total = len;
  656. atmel_spi_next_xfer_data(master, xfer, &tx_dma, &rx_dma, &len);
  657. as->next_remaining_bytes = total - 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/%08x rx %p/%08x\n",
  666. xfer, xfer->len, xfer->tx_buf, xfer->tx_dma,
  667. xfer->rx_buf, xfer->rx_dma);
  668. ieval = SPI_BIT(ENDRX) | SPI_BIT(OVRES);
  669. } else {
  670. spi_writel(as, RNCR, 0);
  671. spi_writel(as, TNCR, 0);
  672. ieval = SPI_BIT(RXBUFF) | SPI_BIT(ENDRX) | SPI_BIT(OVRES);
  673. }
  674. /* REVISIT: We're waiting for ENDRX before we start the next
  675. * transfer because we need to handle some difficult timing
  676. * issues otherwise. If we wait for ENDTX in one transfer and
  677. * then starts waiting for ENDRX in the next, it's difficult
  678. * to tell the difference between the ENDRX interrupt we're
  679. * actually waiting for and the ENDRX interrupt of the
  680. * previous transfer.
  681. *
  682. * It should be doable, though. Just not now...
  683. */
  684. spi_writel(as, IER, ieval);
  685. spi_writel(as, PTCR, SPI_BIT(TXTEN) | SPI_BIT(RXTEN));
  686. }
  687. /*
  688. * Choose way to submit next transfer and start it.
  689. * lock is held, spi tasklet is blocked
  690. */
  691. static void atmel_spi_dma_next_xfer(struct spi_master *master,
  692. struct spi_message *msg)
  693. {
  694. struct atmel_spi *as = spi_master_get_devdata(master);
  695. struct spi_transfer *xfer;
  696. u32 remaining, len;
  697. remaining = as->current_remaining_bytes;
  698. if (remaining) {
  699. xfer = as->current_transfer;
  700. len = remaining;
  701. } else {
  702. if (!as->current_transfer)
  703. xfer = list_entry(msg->transfers.next,
  704. struct spi_transfer, transfer_list);
  705. else
  706. xfer = list_entry(
  707. as->current_transfer->transfer_list.next,
  708. struct spi_transfer, transfer_list);
  709. as->current_transfer = xfer;
  710. len = xfer->len;
  711. }
  712. if (atmel_spi_use_dma(as, xfer)) {
  713. u32 total = len;
  714. if (!atmel_spi_next_xfer_dma_submit(master, xfer, &len)) {
  715. as->current_remaining_bytes = total - len;
  716. return;
  717. } else {
  718. dev_err(&msg->spi->dev, "unable to use DMA, fallback to PIO\n");
  719. }
  720. }
  721. /* use PIO if error appened using DMA */
  722. atmel_spi_next_xfer_pio(master, xfer);
  723. }
  724. static void atmel_spi_next_message(struct spi_master *master)
  725. {
  726. struct atmel_spi *as = spi_master_get_devdata(master);
  727. struct spi_message *msg;
  728. struct spi_device *spi;
  729. BUG_ON(as->current_transfer);
  730. msg = list_entry(as->queue.next, struct spi_message, queue);
  731. spi = msg->spi;
  732. dev_dbg(master->dev.parent, "start message %p for %s\n",
  733. msg, dev_name(&spi->dev));
  734. /* select chip if it's not still active */
  735. if (as->stay) {
  736. if (as->stay != spi) {
  737. cs_deactivate(as, as->stay);
  738. cs_activate(as, spi);
  739. }
  740. as->stay = NULL;
  741. } else
  742. cs_activate(as, spi);
  743. if (as->use_pdc)
  744. atmel_spi_pdc_next_xfer(master, msg);
  745. else
  746. atmel_spi_dma_next_xfer(master, msg);
  747. }
  748. /*
  749. * For DMA, tx_buf/tx_dma have the same relationship as rx_buf/rx_dma:
  750. * - The buffer is either valid for CPU access, else NULL
  751. * - If the buffer is valid, so is its DMA address
  752. *
  753. * This driver manages the dma address unless message->is_dma_mapped.
  754. */
  755. static int
  756. atmel_spi_dma_map_xfer(struct atmel_spi *as, struct spi_transfer *xfer)
  757. {
  758. struct device *dev = &as->pdev->dev;
  759. xfer->tx_dma = xfer->rx_dma = INVALID_DMA_ADDRESS;
  760. if (xfer->tx_buf) {
  761. /* tx_buf is a const void* where we need a void * for the dma
  762. * mapping */
  763. void *nonconst_tx = (void *)xfer->tx_buf;
  764. xfer->tx_dma = dma_map_single(dev,
  765. nonconst_tx, xfer->len,
  766. DMA_TO_DEVICE);
  767. if (dma_mapping_error(dev, xfer->tx_dma))
  768. return -ENOMEM;
  769. }
  770. if (xfer->rx_buf) {
  771. xfer->rx_dma = dma_map_single(dev,
  772. xfer->rx_buf, xfer->len,
  773. DMA_FROM_DEVICE);
  774. if (dma_mapping_error(dev, xfer->rx_dma)) {
  775. if (xfer->tx_buf)
  776. dma_unmap_single(dev,
  777. xfer->tx_dma, xfer->len,
  778. DMA_TO_DEVICE);
  779. return -ENOMEM;
  780. }
  781. }
  782. return 0;
  783. }
  784. static void atmel_spi_dma_unmap_xfer(struct spi_master *master,
  785. struct spi_transfer *xfer)
  786. {
  787. if (xfer->tx_dma != INVALID_DMA_ADDRESS)
  788. dma_unmap_single(master->dev.parent, xfer->tx_dma,
  789. xfer->len, DMA_TO_DEVICE);
  790. if (xfer->rx_dma != INVALID_DMA_ADDRESS)
  791. dma_unmap_single(master->dev.parent, xfer->rx_dma,
  792. xfer->len, DMA_FROM_DEVICE);
  793. }
  794. static void atmel_spi_disable_pdc_transfer(struct atmel_spi *as)
  795. {
  796. spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
  797. }
  798. static void
  799. atmel_spi_msg_done(struct spi_master *master, struct atmel_spi *as,
  800. struct spi_message *msg, int stay)
  801. {
  802. if (!stay || as->done_status < 0)
  803. cs_deactivate(as, msg->spi);
  804. else
  805. as->stay = msg->spi;
  806. list_del(&msg->queue);
  807. msg->status = as->done_status;
  808. dev_dbg(master->dev.parent,
  809. "xfer complete: %u bytes transferred\n",
  810. msg->actual_length);
  811. atmel_spi_unlock(as);
  812. msg->complete(msg->context);
  813. atmel_spi_lock(as);
  814. as->current_transfer = NULL;
  815. as->next_transfer = NULL;
  816. as->done_status = 0;
  817. /* continue if needed */
  818. if (list_empty(&as->queue) || as->stopping) {
  819. if (as->use_pdc)
  820. atmel_spi_disable_pdc_transfer(as);
  821. } else {
  822. atmel_spi_next_message(master);
  823. }
  824. }
  825. /* Called from IRQ
  826. * lock is held
  827. *
  828. * Must update "current_remaining_bytes" to keep track of data
  829. * to transfer.
  830. */
  831. static void
  832. atmel_spi_pump_pio_data(struct atmel_spi *as, struct spi_transfer *xfer)
  833. {
  834. u8 *txp;
  835. u8 *rxp;
  836. u16 *txp16;
  837. u16 *rxp16;
  838. unsigned long xfer_pos = xfer->len - as->current_remaining_bytes;
  839. if (xfer->rx_buf) {
  840. if (xfer->bits_per_word > 8) {
  841. rxp16 = (u16 *)(((u8 *)xfer->rx_buf) + xfer_pos);
  842. *rxp16 = spi_readl(as, RDR);
  843. } else {
  844. rxp = ((u8 *)xfer->rx_buf) + xfer_pos;
  845. *rxp = spi_readl(as, RDR);
  846. }
  847. } else {
  848. spi_readl(as, RDR);
  849. }
  850. if (xfer->bits_per_word > 8) {
  851. as->current_remaining_bytes -= 2;
  852. if (as->current_remaining_bytes < 0)
  853. as->current_remaining_bytes = 0;
  854. } else {
  855. as->current_remaining_bytes--;
  856. }
  857. if (as->current_remaining_bytes) {
  858. if (xfer->tx_buf) {
  859. if (xfer->bits_per_word > 8) {
  860. txp16 = (u16 *)(((u8 *)xfer->tx_buf)
  861. + xfer_pos + 2);
  862. spi_writel(as, TDR, *txp16);
  863. } else {
  864. txp = ((u8 *)xfer->tx_buf) + xfer_pos + 1;
  865. spi_writel(as, TDR, *txp);
  866. }
  867. } else {
  868. spi_writel(as, TDR, 0);
  869. }
  870. }
  871. }
  872. /* Tasklet
  873. * Called from DMA callback + pio transfer and overrun IRQ.
  874. */
  875. static void atmel_spi_tasklet_func(unsigned long data)
  876. {
  877. struct spi_master *master = (struct spi_master *)data;
  878. struct atmel_spi *as = spi_master_get_devdata(master);
  879. struct spi_message *msg;
  880. struct spi_transfer *xfer;
  881. dev_vdbg(master->dev.parent, "atmel_spi_tasklet_func\n");
  882. atmel_spi_lock(as);
  883. xfer = as->current_transfer;
  884. if (xfer == NULL)
  885. /* already been there */
  886. goto tasklet_out;
  887. msg = list_entry(as->queue.next, struct spi_message, queue);
  888. if (as->current_remaining_bytes == 0) {
  889. if (as->done_status < 0) {
  890. /* error happened (overrun) */
  891. if (atmel_spi_use_dma(as, xfer))
  892. atmel_spi_stop_dma(as);
  893. } else {
  894. /* only update length if no error */
  895. msg->actual_length += xfer->len;
  896. }
  897. if (atmel_spi_use_dma(as, xfer))
  898. if (!msg->is_dma_mapped)
  899. atmel_spi_dma_unmap_xfer(master, xfer);
  900. if (xfer->delay_usecs)
  901. udelay(xfer->delay_usecs);
  902. if (atmel_spi_xfer_is_last(msg, xfer) || as->done_status < 0) {
  903. /* report completed (or erroneous) message */
  904. atmel_spi_msg_done(master, as, msg, xfer->cs_change);
  905. } else {
  906. if (xfer->cs_change) {
  907. cs_deactivate(as, msg->spi);
  908. udelay(1);
  909. cs_activate(as, msg->spi);
  910. }
  911. /*
  912. * Not done yet. Submit the next transfer.
  913. *
  914. * FIXME handle protocol options for xfer
  915. */
  916. atmel_spi_dma_next_xfer(master, msg);
  917. }
  918. } else {
  919. /*
  920. * Keep going, we still have data to send in
  921. * the current transfer.
  922. */
  923. atmel_spi_dma_next_xfer(master, msg);
  924. }
  925. tasklet_out:
  926. atmel_spi_unlock(as);
  927. }
  928. /* Interrupt
  929. *
  930. * No need for locking in this Interrupt handler: done_status is the
  931. * only information modified. What we need is the update of this field
  932. * before tasklet runs. This is ensured by using barrier.
  933. */
  934. static irqreturn_t
  935. atmel_spi_pio_interrupt(int irq, void *dev_id)
  936. {
  937. struct spi_master *master = dev_id;
  938. struct atmel_spi *as = spi_master_get_devdata(master);
  939. u32 status, pending, imr;
  940. struct spi_transfer *xfer;
  941. int ret = IRQ_NONE;
  942. imr = spi_readl(as, IMR);
  943. status = spi_readl(as, SR);
  944. pending = status & imr;
  945. if (pending & SPI_BIT(OVRES)) {
  946. ret = IRQ_HANDLED;
  947. spi_writel(as, IDR, SPI_BIT(OVRES));
  948. dev_warn(master->dev.parent, "overrun\n");
  949. /*
  950. * When we get an overrun, we disregard the current
  951. * transfer. Data will not be copied back from any
  952. * bounce buffer and msg->actual_len will not be
  953. * updated with the last xfer.
  954. *
  955. * We will also not process any remaning transfers in
  956. * the message.
  957. *
  958. * All actions are done in tasklet with done_status indication
  959. */
  960. as->done_status = -EIO;
  961. smp_wmb();
  962. /* Clear any overrun happening while cleaning up */
  963. spi_readl(as, SR);
  964. tasklet_schedule(&as->tasklet);
  965. } else if (pending & SPI_BIT(RDRF)) {
  966. atmel_spi_lock(as);
  967. if (as->current_remaining_bytes) {
  968. ret = IRQ_HANDLED;
  969. xfer = as->current_transfer;
  970. atmel_spi_pump_pio_data(as, xfer);
  971. if (!as->current_remaining_bytes) {
  972. /* no more data to xfer, kick tasklet */
  973. spi_writel(as, IDR, pending);
  974. tasklet_schedule(&as->tasklet);
  975. }
  976. }
  977. atmel_spi_unlock(as);
  978. } else {
  979. WARN_ONCE(pending, "IRQ not handled, pending = %x\n", pending);
  980. ret = IRQ_HANDLED;
  981. spi_writel(as, IDR, pending);
  982. }
  983. return ret;
  984. }
  985. static irqreturn_t
  986. atmel_spi_pdc_interrupt(int irq, void *dev_id)
  987. {
  988. struct spi_master *master = dev_id;
  989. struct atmel_spi *as = spi_master_get_devdata(master);
  990. struct spi_message *msg;
  991. struct spi_transfer *xfer;
  992. u32 status, pending, imr;
  993. int ret = IRQ_NONE;
  994. atmel_spi_lock(as);
  995. xfer = as->current_transfer;
  996. msg = list_entry(as->queue.next, struct spi_message, queue);
  997. imr = spi_readl(as, IMR);
  998. status = spi_readl(as, SR);
  999. pending = status & imr;
  1000. if (pending & SPI_BIT(OVRES)) {
  1001. int timeout;
  1002. ret = IRQ_HANDLED;
  1003. spi_writel(as, IDR, (SPI_BIT(RXBUFF) | SPI_BIT(ENDRX)
  1004. | SPI_BIT(OVRES)));
  1005. /*
  1006. * When we get an overrun, we disregard the current
  1007. * transfer. Data will not be copied back from any
  1008. * bounce buffer and msg->actual_len will not be
  1009. * updated with the last xfer.
  1010. *
  1011. * We will also not process any remaning transfers in
  1012. * the message.
  1013. *
  1014. * First, stop the transfer and unmap the DMA buffers.
  1015. */
  1016. spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
  1017. if (!msg->is_dma_mapped)
  1018. atmel_spi_dma_unmap_xfer(master, xfer);
  1019. /* REVISIT: udelay in irq is unfriendly */
  1020. if (xfer->delay_usecs)
  1021. udelay(xfer->delay_usecs);
  1022. dev_warn(master->dev.parent, "overrun (%u/%u remaining)\n",
  1023. spi_readl(as, TCR), spi_readl(as, RCR));
  1024. /*
  1025. * Clean up DMA registers and make sure the data
  1026. * registers are empty.
  1027. */
  1028. spi_writel(as, RNCR, 0);
  1029. spi_writel(as, TNCR, 0);
  1030. spi_writel(as, RCR, 0);
  1031. spi_writel(as, TCR, 0);
  1032. for (timeout = 1000; timeout; timeout--)
  1033. if (spi_readl(as, SR) & SPI_BIT(TXEMPTY))
  1034. break;
  1035. if (!timeout)
  1036. dev_warn(master->dev.parent,
  1037. "timeout waiting for TXEMPTY");
  1038. while (spi_readl(as, SR) & SPI_BIT(RDRF))
  1039. spi_readl(as, RDR);
  1040. /* Clear any overrun happening while cleaning up */
  1041. spi_readl(as, SR);
  1042. as->done_status = -EIO;
  1043. atmel_spi_msg_done(master, as, msg, 0);
  1044. } else if (pending & (SPI_BIT(RXBUFF) | SPI_BIT(ENDRX))) {
  1045. ret = IRQ_HANDLED;
  1046. spi_writel(as, IDR, pending);
  1047. if (as->current_remaining_bytes == 0) {
  1048. msg->actual_length += xfer->len;
  1049. if (!msg->is_dma_mapped)
  1050. atmel_spi_dma_unmap_xfer(master, xfer);
  1051. /* REVISIT: udelay in irq is unfriendly */
  1052. if (xfer->delay_usecs)
  1053. udelay(xfer->delay_usecs);
  1054. if (atmel_spi_xfer_is_last(msg, xfer)) {
  1055. /* report completed message */
  1056. atmel_spi_msg_done(master, as, msg,
  1057. xfer->cs_change);
  1058. } else {
  1059. if (xfer->cs_change) {
  1060. cs_deactivate(as, msg->spi);
  1061. udelay(1);
  1062. cs_activate(as, msg->spi);
  1063. }
  1064. /*
  1065. * Not done yet. Submit the next transfer.
  1066. *
  1067. * FIXME handle protocol options for xfer
  1068. */
  1069. atmel_spi_pdc_next_xfer(master, msg);
  1070. }
  1071. } else {
  1072. /*
  1073. * Keep going, we still have data to send in
  1074. * the current transfer.
  1075. */
  1076. atmel_spi_pdc_next_xfer(master, msg);
  1077. }
  1078. }
  1079. atmel_spi_unlock(as);
  1080. return ret;
  1081. }
  1082. static int atmel_spi_setup(struct spi_device *spi)
  1083. {
  1084. struct atmel_spi *as;
  1085. struct atmel_spi_device *asd;
  1086. u32 scbr, csr;
  1087. unsigned int bits = spi->bits_per_word;
  1088. unsigned long bus_hz;
  1089. unsigned int npcs_pin;
  1090. int ret;
  1091. as = spi_master_get_devdata(spi->master);
  1092. if (as->stopping)
  1093. return -ESHUTDOWN;
  1094. if (spi->chip_select > spi->master->num_chipselect) {
  1095. dev_dbg(&spi->dev,
  1096. "setup: invalid chipselect %u (%u defined)\n",
  1097. spi->chip_select, spi->master->num_chipselect);
  1098. return -EINVAL;
  1099. }
  1100. if (bits < 8 || bits > 16) {
  1101. dev_dbg(&spi->dev,
  1102. "setup: invalid bits_per_word %u (8 to 16)\n",
  1103. bits);
  1104. return -EINVAL;
  1105. }
  1106. /* see notes above re chipselect */
  1107. if (!atmel_spi_is_v2(as)
  1108. && spi->chip_select == 0
  1109. && (spi->mode & SPI_CS_HIGH)) {
  1110. dev_dbg(&spi->dev, "setup: can't be active-high\n");
  1111. return -EINVAL;
  1112. }
  1113. /* v1 chips start out at half the peripheral bus speed. */
  1114. bus_hz = clk_get_rate(as->clk);
  1115. if (!atmel_spi_is_v2(as))
  1116. bus_hz /= 2;
  1117. if (spi->max_speed_hz) {
  1118. /*
  1119. * Calculate the lowest divider that satisfies the
  1120. * constraint, assuming div32/fdiv/mbz == 0.
  1121. */
  1122. scbr = DIV_ROUND_UP(bus_hz, spi->max_speed_hz);
  1123. /*
  1124. * If the resulting divider doesn't fit into the
  1125. * register bitfield, we can't satisfy the constraint.
  1126. */
  1127. if (scbr >= (1 << SPI_SCBR_SIZE)) {
  1128. dev_dbg(&spi->dev,
  1129. "setup: %d Hz too slow, scbr %u; min %ld Hz\n",
  1130. spi->max_speed_hz, scbr, bus_hz/255);
  1131. return -EINVAL;
  1132. }
  1133. } else
  1134. /* speed zero means "as slow as possible" */
  1135. scbr = 0xff;
  1136. csr = SPI_BF(SCBR, scbr) | SPI_BF(BITS, bits - 8);
  1137. if (spi->mode & SPI_CPOL)
  1138. csr |= SPI_BIT(CPOL);
  1139. if (!(spi->mode & SPI_CPHA))
  1140. csr |= SPI_BIT(NCPHA);
  1141. /* DLYBS is mostly irrelevant since we manage chipselect using GPIOs.
  1142. *
  1143. * DLYBCT would add delays between words, slowing down transfers.
  1144. * It could potentially be useful to cope with DMA bottlenecks, but
  1145. * in those cases it's probably best to just use a lower bitrate.
  1146. */
  1147. csr |= SPI_BF(DLYBS, 0);
  1148. csr |= SPI_BF(DLYBCT, 0);
  1149. /* chipselect must have been muxed as GPIO (e.g. in board setup) */
  1150. npcs_pin = (unsigned int)spi->controller_data;
  1151. if (gpio_is_valid(spi->cs_gpio))
  1152. npcs_pin = spi->cs_gpio;
  1153. asd = spi->controller_state;
  1154. if (!asd) {
  1155. asd = kzalloc(sizeof(struct atmel_spi_device), GFP_KERNEL);
  1156. if (!asd)
  1157. return -ENOMEM;
  1158. ret = gpio_request(npcs_pin, dev_name(&spi->dev));
  1159. if (ret) {
  1160. kfree(asd);
  1161. return ret;
  1162. }
  1163. asd->npcs_pin = npcs_pin;
  1164. spi->controller_state = asd;
  1165. gpio_direction_output(npcs_pin, !(spi->mode & SPI_CS_HIGH));
  1166. } else {
  1167. atmel_spi_lock(as);
  1168. if (as->stay == spi)
  1169. as->stay = NULL;
  1170. cs_deactivate(as, spi);
  1171. atmel_spi_unlock(as);
  1172. }
  1173. asd->csr = csr;
  1174. dev_dbg(&spi->dev,
  1175. "setup: %lu Hz bpw %u mode 0x%x -> csr%d %08x\n",
  1176. bus_hz / scbr, bits, spi->mode, spi->chip_select, csr);
  1177. if (!atmel_spi_is_v2(as))
  1178. spi_writel(as, CSR0 + 4 * spi->chip_select, csr);
  1179. return 0;
  1180. }
  1181. static int atmel_spi_transfer(struct spi_device *spi, struct spi_message *msg)
  1182. {
  1183. struct atmel_spi *as;
  1184. struct spi_transfer *xfer;
  1185. struct device *controller = spi->master->dev.parent;
  1186. u8 bits;
  1187. struct atmel_spi_device *asd;
  1188. as = spi_master_get_devdata(spi->master);
  1189. dev_dbg(controller, "new message %p submitted for %s\n",
  1190. msg, dev_name(&spi->dev));
  1191. if (unlikely(list_empty(&msg->transfers)))
  1192. return -EINVAL;
  1193. if (as->stopping)
  1194. return -ESHUTDOWN;
  1195. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  1196. if (!(xfer->tx_buf || xfer->rx_buf) && xfer->len) {
  1197. dev_dbg(&spi->dev, "missing rx or tx buf\n");
  1198. return -EINVAL;
  1199. }
  1200. if (xfer->bits_per_word) {
  1201. asd = spi->controller_state;
  1202. bits = (asd->csr >> 4) & 0xf;
  1203. if (bits != xfer->bits_per_word - 8) {
  1204. dev_dbg(&spi->dev, "you can't yet change "
  1205. "bits_per_word in transfers\n");
  1206. return -ENOPROTOOPT;
  1207. }
  1208. }
  1209. if (xfer->bits_per_word > 8) {
  1210. if (xfer->len % 2) {
  1211. dev_dbg(&spi->dev, "buffer len should be 16 bits aligned\n");
  1212. return -EINVAL;
  1213. }
  1214. }
  1215. /* FIXME implement these protocol options!! */
  1216. if (xfer->speed_hz < spi->max_speed_hz) {
  1217. dev_dbg(&spi->dev, "can't change speed in transfer\n");
  1218. return -ENOPROTOOPT;
  1219. }
  1220. /*
  1221. * DMA map early, for performance (empties dcache ASAP) and
  1222. * better fault reporting.
  1223. */
  1224. if ((!msg->is_dma_mapped) && (atmel_spi_use_dma(as, xfer)
  1225. || as->use_pdc)) {
  1226. if (atmel_spi_dma_map_xfer(as, xfer) < 0)
  1227. return -ENOMEM;
  1228. }
  1229. }
  1230. #ifdef VERBOSE
  1231. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  1232. dev_dbg(controller,
  1233. " xfer %p: len %u tx %p/%08x rx %p/%08x\n",
  1234. xfer, xfer->len,
  1235. xfer->tx_buf, xfer->tx_dma,
  1236. xfer->rx_buf, xfer->rx_dma);
  1237. }
  1238. #endif
  1239. msg->status = -EINPROGRESS;
  1240. msg->actual_length = 0;
  1241. atmel_spi_lock(as);
  1242. list_add_tail(&msg->queue, &as->queue);
  1243. if (!as->current_transfer)
  1244. atmel_spi_next_message(spi->master);
  1245. atmel_spi_unlock(as);
  1246. return 0;
  1247. }
  1248. static void atmel_spi_cleanup(struct spi_device *spi)
  1249. {
  1250. struct atmel_spi *as = spi_master_get_devdata(spi->master);
  1251. struct atmel_spi_device *asd = spi->controller_state;
  1252. unsigned gpio = (unsigned) spi->controller_data;
  1253. if (!asd)
  1254. return;
  1255. atmel_spi_lock(as);
  1256. if (as->stay == spi) {
  1257. as->stay = NULL;
  1258. cs_deactivate(as, spi);
  1259. }
  1260. atmel_spi_unlock(as);
  1261. spi->controller_state = NULL;
  1262. gpio_free(gpio);
  1263. kfree(asd);
  1264. }
  1265. static inline unsigned int atmel_get_version(struct atmel_spi *as)
  1266. {
  1267. return spi_readl(as, VERSION) & 0x00000fff;
  1268. }
  1269. static void atmel_get_caps(struct atmel_spi *as)
  1270. {
  1271. unsigned int version;
  1272. version = atmel_get_version(as);
  1273. dev_info(&as->pdev->dev, "version: 0x%x\n", version);
  1274. as->caps.is_spi2 = version > 0x121;
  1275. as->caps.has_wdrbt = version >= 0x210;
  1276. as->caps.has_dma_support = version >= 0x212;
  1277. }
  1278. /*-------------------------------------------------------------------------*/
  1279. static int atmel_spi_probe(struct platform_device *pdev)
  1280. {
  1281. struct resource *regs;
  1282. int irq;
  1283. struct clk *clk;
  1284. int ret;
  1285. struct spi_master *master;
  1286. struct atmel_spi *as;
  1287. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1288. if (!regs)
  1289. return -ENXIO;
  1290. irq = platform_get_irq(pdev, 0);
  1291. if (irq < 0)
  1292. return irq;
  1293. clk = clk_get(&pdev->dev, "spi_clk");
  1294. if (IS_ERR(clk))
  1295. return PTR_ERR(clk);
  1296. /* setup spi core then atmel-specific driver state */
  1297. ret = -ENOMEM;
  1298. master = spi_alloc_master(&pdev->dev, sizeof *as);
  1299. if (!master)
  1300. goto out_free;
  1301. /* the spi->mode bits understood by this driver: */
  1302. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  1303. master->dev.of_node = pdev->dev.of_node;
  1304. master->bus_num = pdev->id;
  1305. master->num_chipselect = master->dev.of_node ? 0 : 4;
  1306. master->setup = atmel_spi_setup;
  1307. master->transfer = atmel_spi_transfer;
  1308. master->cleanup = atmel_spi_cleanup;
  1309. platform_set_drvdata(pdev, master);
  1310. as = spi_master_get_devdata(master);
  1311. /*
  1312. * Scratch buffer is used for throwaway rx and tx data.
  1313. * It's coherent to minimize dcache pollution.
  1314. */
  1315. as->buffer = dma_alloc_coherent(&pdev->dev, BUFFER_SIZE,
  1316. &as->buffer_dma, GFP_KERNEL);
  1317. if (!as->buffer)
  1318. goto out_free;
  1319. spin_lock_init(&as->lock);
  1320. INIT_LIST_HEAD(&as->queue);
  1321. as->pdev = pdev;
  1322. as->regs = ioremap(regs->start, resource_size(regs));
  1323. if (!as->regs)
  1324. goto out_free_buffer;
  1325. as->phybase = regs->start;
  1326. as->irq = irq;
  1327. as->clk = clk;
  1328. atmel_get_caps(as);
  1329. as->use_dma = false;
  1330. as->use_pdc = false;
  1331. if (as->caps.has_dma_support) {
  1332. if (atmel_spi_configure_dma(as) == 0)
  1333. as->use_dma = true;
  1334. } else {
  1335. as->use_pdc = true;
  1336. }
  1337. if (as->caps.has_dma_support && !as->use_dma)
  1338. dev_info(&pdev->dev, "Atmel SPI Controller using PIO only\n");
  1339. if (as->use_pdc) {
  1340. ret = request_irq(irq, atmel_spi_pdc_interrupt, 0,
  1341. dev_name(&pdev->dev), master);
  1342. } else {
  1343. tasklet_init(&as->tasklet, atmel_spi_tasklet_func,
  1344. (unsigned long)master);
  1345. ret = request_irq(irq, atmel_spi_pio_interrupt, 0,
  1346. dev_name(&pdev->dev), master);
  1347. }
  1348. if (ret)
  1349. goto out_unmap_regs;
  1350. /* Initialize the hardware */
  1351. clk_enable(clk);
  1352. spi_writel(as, CR, SPI_BIT(SWRST));
  1353. spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */
  1354. if (as->caps.has_wdrbt) {
  1355. spi_writel(as, MR, SPI_BIT(WDRBT) | SPI_BIT(MODFDIS)
  1356. | SPI_BIT(MSTR));
  1357. } else {
  1358. spi_writel(as, MR, SPI_BIT(MSTR) | SPI_BIT(MODFDIS));
  1359. }
  1360. if (as->use_pdc)
  1361. spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
  1362. spi_writel(as, CR, SPI_BIT(SPIEN));
  1363. /* go! */
  1364. dev_info(&pdev->dev, "Atmel SPI Controller at 0x%08lx (irq %d)\n",
  1365. (unsigned long)regs->start, irq);
  1366. ret = spi_register_master(master);
  1367. if (ret)
  1368. goto out_free_dma;
  1369. return 0;
  1370. out_free_dma:
  1371. if (as->use_dma)
  1372. atmel_spi_release_dma(as);
  1373. spi_writel(as, CR, SPI_BIT(SWRST));
  1374. spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */
  1375. clk_disable(clk);
  1376. free_irq(irq, master);
  1377. out_unmap_regs:
  1378. iounmap(as->regs);
  1379. out_free_buffer:
  1380. if (!as->use_pdc)
  1381. tasklet_kill(&as->tasklet);
  1382. dma_free_coherent(&pdev->dev, BUFFER_SIZE, as->buffer,
  1383. as->buffer_dma);
  1384. out_free:
  1385. clk_put(clk);
  1386. spi_master_put(master);
  1387. return ret;
  1388. }
  1389. static int atmel_spi_remove(struct platform_device *pdev)
  1390. {
  1391. struct spi_master *master = platform_get_drvdata(pdev);
  1392. struct atmel_spi *as = spi_master_get_devdata(master);
  1393. struct spi_message *msg;
  1394. struct spi_transfer *xfer;
  1395. /* reset the hardware and block queue progress */
  1396. spin_lock_irq(&as->lock);
  1397. as->stopping = 1;
  1398. if (as->use_dma) {
  1399. atmel_spi_stop_dma(as);
  1400. atmel_spi_release_dma(as);
  1401. }
  1402. spi_writel(as, CR, SPI_BIT(SWRST));
  1403. spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */
  1404. spi_readl(as, SR);
  1405. spin_unlock_irq(&as->lock);
  1406. /* Terminate remaining queued transfers */
  1407. list_for_each_entry(msg, &as->queue, queue) {
  1408. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  1409. if (!msg->is_dma_mapped
  1410. && (atmel_spi_use_dma(as, xfer)
  1411. || as->use_pdc))
  1412. atmel_spi_dma_unmap_xfer(master, xfer);
  1413. }
  1414. msg->status = -ESHUTDOWN;
  1415. msg->complete(msg->context);
  1416. }
  1417. if (!as->use_pdc)
  1418. tasklet_kill(&as->tasklet);
  1419. dma_free_coherent(&pdev->dev, BUFFER_SIZE, as->buffer,
  1420. as->buffer_dma);
  1421. clk_disable(as->clk);
  1422. clk_put(as->clk);
  1423. free_irq(as->irq, master);
  1424. iounmap(as->regs);
  1425. spi_unregister_master(master);
  1426. return 0;
  1427. }
  1428. #ifdef CONFIG_PM
  1429. static int atmel_spi_suspend(struct platform_device *pdev, pm_message_t mesg)
  1430. {
  1431. struct spi_master *master = platform_get_drvdata(pdev);
  1432. struct atmel_spi *as = spi_master_get_devdata(master);
  1433. clk_disable(as->clk);
  1434. return 0;
  1435. }
  1436. static int atmel_spi_resume(struct platform_device *pdev)
  1437. {
  1438. struct spi_master *master = platform_get_drvdata(pdev);
  1439. struct atmel_spi *as = spi_master_get_devdata(master);
  1440. clk_enable(as->clk);
  1441. return 0;
  1442. }
  1443. #else
  1444. #define atmel_spi_suspend NULL
  1445. #define atmel_spi_resume NULL
  1446. #endif
  1447. #if defined(CONFIG_OF)
  1448. static const struct of_device_id atmel_spi_dt_ids[] = {
  1449. { .compatible = "atmel,at91rm9200-spi" },
  1450. { /* sentinel */ }
  1451. };
  1452. MODULE_DEVICE_TABLE(of, atmel_spi_dt_ids);
  1453. #endif
  1454. static struct platform_driver atmel_spi_driver = {
  1455. .driver = {
  1456. .name = "atmel_spi",
  1457. .owner = THIS_MODULE,
  1458. .of_match_table = of_match_ptr(atmel_spi_dt_ids),
  1459. },
  1460. .suspend = atmel_spi_suspend,
  1461. .resume = atmel_spi_resume,
  1462. .probe = atmel_spi_probe,
  1463. .remove = atmel_spi_remove,
  1464. };
  1465. module_platform_driver(atmel_spi_driver);
  1466. MODULE_DESCRIPTION("Atmel AT32/AT91 SPI Controller driver");
  1467. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  1468. MODULE_LICENSE("GPL");
  1469. MODULE_ALIAS("platform:atmel_spi");