spi-atmel.c 38 KB

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