spi-atmel.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518
  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. #define SPI_DMA_TIMEOUT (msecs_to_jiffies(1000))
  174. struct atmel_spi_dma {
  175. struct dma_chan *chan_rx;
  176. struct dma_chan *chan_tx;
  177. struct scatterlist sgrx;
  178. struct scatterlist sgtx;
  179. struct dma_async_tx_descriptor *data_desc_rx;
  180. struct dma_async_tx_descriptor *data_desc_tx;
  181. struct at_dma_slave dma_slave;
  182. };
  183. struct atmel_spi_caps {
  184. bool is_spi2;
  185. bool has_wdrbt;
  186. bool has_dma_support;
  187. };
  188. /*
  189. * The core SPI transfer engine just talks to a register bank to set up
  190. * DMA transfers; transfer queue progress is driven by IRQs. The clock
  191. * framework provides the base clock, subdivided for each spi_device.
  192. */
  193. struct atmel_spi {
  194. spinlock_t lock;
  195. unsigned long flags;
  196. phys_addr_t phybase;
  197. void __iomem *regs;
  198. int irq;
  199. struct clk *clk;
  200. struct platform_device *pdev;
  201. struct spi_transfer *current_transfer;
  202. unsigned long current_remaining_bytes;
  203. int done_status;
  204. struct completion xfer_completion;
  205. /* scratch buffer */
  206. void *buffer;
  207. dma_addr_t buffer_dma;
  208. struct atmel_spi_caps caps;
  209. bool use_dma;
  210. bool use_pdc;
  211. /* dmaengine data */
  212. struct atmel_spi_dma dma;
  213. bool keep_cs;
  214. bool cs_active;
  215. };
  216. /* Controller-specific per-slave state */
  217. struct atmel_spi_device {
  218. unsigned int npcs_pin;
  219. u32 csr;
  220. };
  221. #define BUFFER_SIZE PAGE_SIZE
  222. #define INVALID_DMA_ADDRESS 0xffffffff
  223. /*
  224. * Version 2 of the SPI controller has
  225. * - CR.LASTXFER
  226. * - SPI_MR.DIV32 may become FDIV or must-be-zero (here: always zero)
  227. * - SPI_SR.TXEMPTY, SPI_SR.NSSR (and corresponding irqs)
  228. * - SPI_CSRx.CSAAT
  229. * - SPI_CSRx.SBCR allows faster clocking
  230. */
  231. static bool atmel_spi_is_v2(struct atmel_spi *as)
  232. {
  233. return as->caps.is_spi2;
  234. }
  235. /*
  236. * Earlier SPI controllers (e.g. on at91rm9200) have a design bug whereby
  237. * they assume that spi slave device state will not change on deselect, so
  238. * that automagic deselection is OK. ("NPCSx rises if no data is to be
  239. * transmitted") Not so! Workaround uses nCSx pins as GPIOs; or newer
  240. * controllers have CSAAT and friends.
  241. *
  242. * Since the CSAAT functionality is a bit weird on newer controllers as
  243. * well, we use GPIO to control nCSx pins on all controllers, updating
  244. * MR.PCS to avoid confusing the controller. Using GPIOs also lets us
  245. * support active-high chipselects despite the controller's belief that
  246. * only active-low devices/systems exists.
  247. *
  248. * However, at91rm9200 has a second erratum whereby nCS0 doesn't work
  249. * right when driven with GPIO. ("Mode Fault does not allow more than one
  250. * Master on Chip Select 0.") No workaround exists for that ... so for
  251. * nCS0 on that chip, we (a) don't use the GPIO, (b) can't support CS_HIGH,
  252. * and (c) will trigger that first erratum in some cases.
  253. */
  254. static void cs_activate(struct atmel_spi *as, struct spi_device *spi)
  255. {
  256. struct atmel_spi_device *asd = spi->controller_state;
  257. unsigned active = spi->mode & SPI_CS_HIGH;
  258. u32 mr;
  259. if (atmel_spi_is_v2(as)) {
  260. spi_writel(as, CSR0 + 4 * spi->chip_select, asd->csr);
  261. /* For the low SPI version, there is a issue that PDC transfer
  262. * on CS1,2,3 needs SPI_CSR0.BITS config as SPI_CSR1,2,3.BITS
  263. */
  264. spi_writel(as, CSR0, asd->csr);
  265. if (as->caps.has_wdrbt) {
  266. spi_writel(as, MR,
  267. SPI_BF(PCS, ~(0x01 << spi->chip_select))
  268. | SPI_BIT(WDRBT)
  269. | SPI_BIT(MODFDIS)
  270. | SPI_BIT(MSTR));
  271. } else {
  272. spi_writel(as, MR,
  273. SPI_BF(PCS, ~(0x01 << spi->chip_select))
  274. | SPI_BIT(MODFDIS)
  275. | SPI_BIT(MSTR));
  276. }
  277. mr = spi_readl(as, MR);
  278. gpio_set_value(asd->npcs_pin, active);
  279. } else {
  280. u32 cpol = (spi->mode & SPI_CPOL) ? SPI_BIT(CPOL) : 0;
  281. int i;
  282. u32 csr;
  283. /* Make sure clock polarity is correct */
  284. for (i = 0; i < spi->master->num_chipselect; i++) {
  285. csr = spi_readl(as, CSR0 + 4 * i);
  286. if ((csr ^ cpol) & SPI_BIT(CPOL))
  287. spi_writel(as, CSR0 + 4 * i,
  288. csr ^ SPI_BIT(CPOL));
  289. }
  290. mr = spi_readl(as, MR);
  291. mr = SPI_BFINS(PCS, ~(1 << spi->chip_select), mr);
  292. if (spi->chip_select != 0)
  293. gpio_set_value(asd->npcs_pin, active);
  294. spi_writel(as, MR, mr);
  295. }
  296. dev_dbg(&spi->dev, "activate %u%s, mr %08x\n",
  297. asd->npcs_pin, active ? " (high)" : "",
  298. mr);
  299. }
  300. static void cs_deactivate(struct atmel_spi *as, struct spi_device *spi)
  301. {
  302. struct atmel_spi_device *asd = spi->controller_state;
  303. unsigned active = spi->mode & SPI_CS_HIGH;
  304. u32 mr;
  305. /* only deactivate *this* device; sometimes transfers to
  306. * another device may be active when this routine is called.
  307. */
  308. mr = spi_readl(as, MR);
  309. if (~SPI_BFEXT(PCS, mr) & (1 << spi->chip_select)) {
  310. mr = SPI_BFINS(PCS, 0xf, mr);
  311. spi_writel(as, MR, mr);
  312. }
  313. dev_dbg(&spi->dev, "DEactivate %u%s, mr %08x\n",
  314. asd->npcs_pin, active ? " (low)" : "",
  315. mr);
  316. if (atmel_spi_is_v2(as) || spi->chip_select != 0)
  317. gpio_set_value(asd->npcs_pin, !active);
  318. }
  319. static void atmel_spi_lock(struct atmel_spi *as) __acquires(&as->lock)
  320. {
  321. spin_lock_irqsave(&as->lock, as->flags);
  322. }
  323. static void atmel_spi_unlock(struct atmel_spi *as) __releases(&as->lock)
  324. {
  325. spin_unlock_irqrestore(&as->lock, as->flags);
  326. }
  327. static inline bool atmel_spi_use_dma(struct atmel_spi *as,
  328. struct spi_transfer *xfer)
  329. {
  330. return as->use_dma && xfer->len >= DMA_MIN_BYTES;
  331. }
  332. static int atmel_spi_dma_slave_config(struct atmel_spi *as,
  333. struct dma_slave_config *slave_config,
  334. u8 bits_per_word)
  335. {
  336. int err = 0;
  337. if (bits_per_word > 8) {
  338. slave_config->dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  339. slave_config->src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  340. } else {
  341. slave_config->dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  342. slave_config->src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  343. }
  344. slave_config->dst_addr = (dma_addr_t)as->phybase + SPI_TDR;
  345. slave_config->src_addr = (dma_addr_t)as->phybase + SPI_RDR;
  346. slave_config->src_maxburst = 1;
  347. slave_config->dst_maxburst = 1;
  348. slave_config->device_fc = false;
  349. slave_config->direction = DMA_MEM_TO_DEV;
  350. if (dmaengine_slave_config(as->dma.chan_tx, slave_config)) {
  351. dev_err(&as->pdev->dev,
  352. "failed to configure tx dma channel\n");
  353. err = -EINVAL;
  354. }
  355. slave_config->direction = DMA_DEV_TO_MEM;
  356. if (dmaengine_slave_config(as->dma.chan_rx, slave_config)) {
  357. dev_err(&as->pdev->dev,
  358. "failed to configure rx dma channel\n");
  359. err = -EINVAL;
  360. }
  361. return err;
  362. }
  363. static bool filter(struct dma_chan *chan, void *pdata)
  364. {
  365. struct atmel_spi_dma *sl_pdata = pdata;
  366. struct at_dma_slave *sl;
  367. if (!sl_pdata)
  368. return false;
  369. sl = &sl_pdata->dma_slave;
  370. if (sl->dma_dev == chan->device->dev) {
  371. chan->private = sl;
  372. return true;
  373. } else {
  374. return false;
  375. }
  376. }
  377. static int atmel_spi_configure_dma(struct atmel_spi *as)
  378. {
  379. struct dma_slave_config slave_config;
  380. struct device *dev = &as->pdev->dev;
  381. int err;
  382. dma_cap_mask_t mask;
  383. dma_cap_zero(mask);
  384. dma_cap_set(DMA_SLAVE, mask);
  385. as->dma.chan_tx = dma_request_slave_channel_compat(mask, filter,
  386. &as->dma,
  387. dev, "tx");
  388. if (!as->dma.chan_tx) {
  389. dev_err(dev,
  390. "DMA TX channel not available, SPI unable to use DMA\n");
  391. err = -EBUSY;
  392. goto error;
  393. }
  394. as->dma.chan_rx = dma_request_slave_channel_compat(mask, filter,
  395. &as->dma,
  396. dev, "rx");
  397. if (!as->dma.chan_rx) {
  398. dev_err(dev,
  399. "DMA RX channel not available, SPI unable to use DMA\n");
  400. err = -EBUSY;
  401. goto error;
  402. }
  403. err = atmel_spi_dma_slave_config(as, &slave_config, 8);
  404. if (err)
  405. goto error;
  406. dev_info(&as->pdev->dev,
  407. "Using %s (tx) and %s (rx) for DMA transfers\n",
  408. dma_chan_name(as->dma.chan_tx),
  409. dma_chan_name(as->dma.chan_rx));
  410. return 0;
  411. error:
  412. if (as->dma.chan_rx)
  413. dma_release_channel(as->dma.chan_rx);
  414. if (as->dma.chan_tx)
  415. dma_release_channel(as->dma.chan_tx);
  416. return err;
  417. }
  418. static void atmel_spi_stop_dma(struct atmel_spi *as)
  419. {
  420. if (as->dma.chan_rx)
  421. as->dma.chan_rx->device->device_control(as->dma.chan_rx,
  422. DMA_TERMINATE_ALL, 0);
  423. if (as->dma.chan_tx)
  424. as->dma.chan_tx->device->device_control(as->dma.chan_tx,
  425. DMA_TERMINATE_ALL, 0);
  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 = rxchan->device->device_prep_slave_sg(rxchan,
  518. &as->dma.sgrx,
  519. 1,
  520. DMA_FROM_DEVICE,
  521. DMA_PREP_INTERRUPT | DMA_CTRL_ACK,
  522. NULL);
  523. if (!rxdesc)
  524. goto err_dma;
  525. txdesc = txchan->device->device_prep_slave_sg(txchan,
  526. &as->dma.sgtx,
  527. 1,
  528. DMA_TO_DEVICE,
  529. DMA_PREP_INTERRUPT | DMA_CTRL_ACK,
  530. NULL);
  531. if (!txdesc)
  532. goto err_dma;
  533. dev_dbg(master->dev.parent,
  534. " start dma xfer %p: len %u tx %p/%08llx rx %p/%08llx\n",
  535. xfer, xfer->len, xfer->tx_buf, (unsigned long long)xfer->tx_dma,
  536. xfer->rx_buf, (unsigned long long)xfer->rx_dma);
  537. /* Enable relevant interrupts */
  538. spi_writel(as, IER, SPI_BIT(OVRES));
  539. /* Put the callback on the RX transfer only, that should finish last */
  540. rxdesc->callback = dma_callback;
  541. rxdesc->callback_param = master;
  542. /* Submit and fire RX and TX with TX last so we're ready to read! */
  543. cookie = rxdesc->tx_submit(rxdesc);
  544. if (dma_submit_error(cookie))
  545. goto err_dma;
  546. cookie = txdesc->tx_submit(txdesc);
  547. if (dma_submit_error(cookie))
  548. goto err_dma;
  549. rxchan->device->device_issue_pending(rxchan);
  550. txchan->device->device_issue_pending(txchan);
  551. /* take back lock */
  552. atmel_spi_lock(as);
  553. return 0;
  554. err_dma:
  555. spi_writel(as, IDR, SPI_BIT(OVRES));
  556. atmel_spi_stop_dma(as);
  557. err_exit:
  558. atmel_spi_lock(as);
  559. return -ENOMEM;
  560. }
  561. static void atmel_spi_next_xfer_data(struct spi_master *master,
  562. struct spi_transfer *xfer,
  563. dma_addr_t *tx_dma,
  564. dma_addr_t *rx_dma,
  565. u32 *plen)
  566. {
  567. struct atmel_spi *as = spi_master_get_devdata(master);
  568. u32 len = *plen;
  569. /* use scratch buffer only when rx or tx data is unspecified */
  570. if (xfer->rx_buf)
  571. *rx_dma = xfer->rx_dma + xfer->len - *plen;
  572. else {
  573. *rx_dma = as->buffer_dma;
  574. if (len > BUFFER_SIZE)
  575. len = BUFFER_SIZE;
  576. }
  577. if (xfer->tx_buf)
  578. *tx_dma = xfer->tx_dma + xfer->len - *plen;
  579. else {
  580. *tx_dma = as->buffer_dma;
  581. if (len > BUFFER_SIZE)
  582. len = BUFFER_SIZE;
  583. memset(as->buffer, 0, len);
  584. dma_sync_single_for_device(&as->pdev->dev,
  585. as->buffer_dma, len, DMA_TO_DEVICE);
  586. }
  587. *plen = len;
  588. }
  589. static int atmel_spi_set_xfer_speed(struct atmel_spi *as,
  590. struct spi_device *spi,
  591. struct spi_transfer *xfer)
  592. {
  593. u32 scbr, csr;
  594. unsigned long bus_hz;
  595. /* v1 chips start out at half the peripheral bus speed. */
  596. bus_hz = clk_get_rate(as->clk);
  597. if (!atmel_spi_is_v2(as))
  598. bus_hz /= 2;
  599. /*
  600. * Calculate the lowest divider that satisfies the
  601. * constraint, assuming div32/fdiv/mbz == 0.
  602. */
  603. if (xfer->speed_hz)
  604. scbr = DIV_ROUND_UP(bus_hz, xfer->speed_hz);
  605. else
  606. /*
  607. * This can happend if max_speed is null.
  608. * In this case, we set the lowest possible speed
  609. */
  610. scbr = 0xff;
  611. /*
  612. * If the resulting divider doesn't fit into the
  613. * register bitfield, we can't satisfy the constraint.
  614. */
  615. if (scbr >= (1 << SPI_SCBR_SIZE)) {
  616. dev_err(&spi->dev,
  617. "setup: %d Hz too slow, scbr %u; min %ld Hz\n",
  618. xfer->speed_hz, scbr, bus_hz/255);
  619. return -EINVAL;
  620. }
  621. if (scbr == 0) {
  622. dev_err(&spi->dev,
  623. "setup: %d Hz too high, scbr %u; max %ld Hz\n",
  624. xfer->speed_hz, scbr, bus_hz);
  625. return -EINVAL;
  626. }
  627. csr = spi_readl(as, CSR0 + 4 * spi->chip_select);
  628. csr = SPI_BFINS(SCBR, scbr, csr);
  629. spi_writel(as, CSR0 + 4 * spi->chip_select, csr);
  630. return 0;
  631. }
  632. /*
  633. * Submit next transfer for PDC.
  634. * lock is held, spi irq is blocked
  635. */
  636. static void atmel_spi_pdc_next_xfer(struct spi_master *master,
  637. struct spi_message *msg,
  638. struct spi_transfer *xfer)
  639. {
  640. struct atmel_spi *as = spi_master_get_devdata(master);
  641. u32 len;
  642. dma_addr_t tx_dma, rx_dma;
  643. spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
  644. len = as->current_remaining_bytes;
  645. atmel_spi_next_xfer_data(master, xfer, &tx_dma, &rx_dma, &len);
  646. as->current_remaining_bytes -= len;
  647. spi_writel(as, RPR, rx_dma);
  648. spi_writel(as, TPR, tx_dma);
  649. if (msg->spi->bits_per_word > 8)
  650. len >>= 1;
  651. spi_writel(as, RCR, len);
  652. spi_writel(as, TCR, len);
  653. dev_dbg(&msg->spi->dev,
  654. " start xfer %p: len %u tx %p/%08llx rx %p/%08llx\n",
  655. xfer, xfer->len, xfer->tx_buf,
  656. (unsigned long long)xfer->tx_dma, xfer->rx_buf,
  657. (unsigned long long)xfer->rx_dma);
  658. if (as->current_remaining_bytes) {
  659. len = as->current_remaining_bytes;
  660. atmel_spi_next_xfer_data(master, xfer, &tx_dma, &rx_dma, &len);
  661. as->current_remaining_bytes -= len;
  662. spi_writel(as, RNPR, rx_dma);
  663. spi_writel(as, TNPR, tx_dma);
  664. if (msg->spi->bits_per_word > 8)
  665. len >>= 1;
  666. spi_writel(as, RNCR, len);
  667. spi_writel(as, TNCR, len);
  668. dev_dbg(&msg->spi->dev,
  669. " next xfer %p: len %u tx %p/%08llx rx %p/%08llx\n",
  670. xfer, xfer->len, xfer->tx_buf,
  671. (unsigned long long)xfer->tx_dma, xfer->rx_buf,
  672. (unsigned long long)xfer->rx_dma);
  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, SPI_BIT(ENDRX) | SPI_BIT(OVRES));
  685. spi_writel(as, PTCR, SPI_BIT(TXTEN) | SPI_BIT(RXTEN));
  686. }
  687. /*
  688. * For DMA, tx_buf/tx_dma have the same relationship as rx_buf/rx_dma:
  689. * - The buffer is either valid for CPU access, else NULL
  690. * - If the buffer is valid, so is its DMA address
  691. *
  692. * This driver manages the dma address unless message->is_dma_mapped.
  693. */
  694. static int
  695. atmel_spi_dma_map_xfer(struct atmel_spi *as, struct spi_transfer *xfer)
  696. {
  697. struct device *dev = &as->pdev->dev;
  698. xfer->tx_dma = xfer->rx_dma = INVALID_DMA_ADDRESS;
  699. if (xfer->tx_buf) {
  700. /* tx_buf is a const void* where we need a void * for the dma
  701. * mapping */
  702. void *nonconst_tx = (void *)xfer->tx_buf;
  703. xfer->tx_dma = dma_map_single(dev,
  704. nonconst_tx, xfer->len,
  705. DMA_TO_DEVICE);
  706. if (dma_mapping_error(dev, xfer->tx_dma))
  707. return -ENOMEM;
  708. }
  709. if (xfer->rx_buf) {
  710. xfer->rx_dma = dma_map_single(dev,
  711. xfer->rx_buf, xfer->len,
  712. DMA_FROM_DEVICE);
  713. if (dma_mapping_error(dev, xfer->rx_dma)) {
  714. if (xfer->tx_buf)
  715. dma_unmap_single(dev,
  716. xfer->tx_dma, xfer->len,
  717. DMA_TO_DEVICE);
  718. return -ENOMEM;
  719. }
  720. }
  721. return 0;
  722. }
  723. static void atmel_spi_dma_unmap_xfer(struct spi_master *master,
  724. struct spi_transfer *xfer)
  725. {
  726. if (xfer->tx_dma != INVALID_DMA_ADDRESS)
  727. dma_unmap_single(master->dev.parent, xfer->tx_dma,
  728. xfer->len, DMA_TO_DEVICE);
  729. if (xfer->rx_dma != INVALID_DMA_ADDRESS)
  730. dma_unmap_single(master->dev.parent, xfer->rx_dma,
  731. xfer->len, DMA_FROM_DEVICE);
  732. }
  733. static void atmel_spi_disable_pdc_transfer(struct atmel_spi *as)
  734. {
  735. spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
  736. }
  737. /* Called from IRQ
  738. *
  739. * Must update "current_remaining_bytes" to keep track of data
  740. * to transfer.
  741. */
  742. static void
  743. atmel_spi_pump_pio_data(struct atmel_spi *as, struct spi_transfer *xfer)
  744. {
  745. u8 *rxp;
  746. u16 *rxp16;
  747. unsigned long xfer_pos = xfer->len - as->current_remaining_bytes;
  748. if (xfer->rx_buf) {
  749. if (xfer->bits_per_word > 8) {
  750. rxp16 = (u16 *)(((u8 *)xfer->rx_buf) + xfer_pos);
  751. *rxp16 = spi_readl(as, RDR);
  752. } else {
  753. rxp = ((u8 *)xfer->rx_buf) + xfer_pos;
  754. *rxp = spi_readl(as, RDR);
  755. }
  756. } else {
  757. spi_readl(as, RDR);
  758. }
  759. if (xfer->bits_per_word > 8) {
  760. as->current_remaining_bytes -= 2;
  761. if (as->current_remaining_bytes < 0)
  762. as->current_remaining_bytes = 0;
  763. } else {
  764. as->current_remaining_bytes--;
  765. }
  766. }
  767. /* Interrupt
  768. *
  769. * No need for locking in this Interrupt handler: done_status is the
  770. * only information modified.
  771. */
  772. static irqreturn_t
  773. atmel_spi_pio_interrupt(int irq, void *dev_id)
  774. {
  775. struct spi_master *master = dev_id;
  776. struct atmel_spi *as = spi_master_get_devdata(master);
  777. u32 status, pending, imr;
  778. struct spi_transfer *xfer;
  779. int ret = IRQ_NONE;
  780. imr = spi_readl(as, IMR);
  781. status = spi_readl(as, SR);
  782. pending = status & imr;
  783. if (pending & SPI_BIT(OVRES)) {
  784. ret = IRQ_HANDLED;
  785. spi_writel(as, IDR, SPI_BIT(OVRES));
  786. dev_warn(master->dev.parent, "overrun\n");
  787. /*
  788. * When we get an overrun, we disregard the current
  789. * transfer. Data will not be copied back from any
  790. * bounce buffer and msg->actual_len will not be
  791. * updated with the last xfer.
  792. *
  793. * We will also not process any remaning transfers in
  794. * the message.
  795. */
  796. as->done_status = -EIO;
  797. smp_wmb();
  798. /* Clear any overrun happening while cleaning up */
  799. spi_readl(as, SR);
  800. complete(&as->xfer_completion);
  801. } else if (pending & SPI_BIT(RDRF)) {
  802. atmel_spi_lock(as);
  803. if (as->current_remaining_bytes) {
  804. ret = IRQ_HANDLED;
  805. xfer = as->current_transfer;
  806. atmel_spi_pump_pio_data(as, xfer);
  807. if (!as->current_remaining_bytes)
  808. spi_writel(as, IDR, pending);
  809. complete(&as->xfer_completion);
  810. }
  811. atmel_spi_unlock(as);
  812. } else {
  813. WARN_ONCE(pending, "IRQ not handled, pending = %x\n", pending);
  814. ret = IRQ_HANDLED;
  815. spi_writel(as, IDR, pending);
  816. }
  817. return ret;
  818. }
  819. static irqreturn_t
  820. atmel_spi_pdc_interrupt(int irq, void *dev_id)
  821. {
  822. struct spi_master *master = dev_id;
  823. struct atmel_spi *as = spi_master_get_devdata(master);
  824. u32 status, pending, imr;
  825. int ret = IRQ_NONE;
  826. imr = spi_readl(as, IMR);
  827. status = spi_readl(as, SR);
  828. pending = status & imr;
  829. if (pending & SPI_BIT(OVRES)) {
  830. ret = IRQ_HANDLED;
  831. spi_writel(as, IDR, (SPI_BIT(RXBUFF) | SPI_BIT(ENDRX)
  832. | SPI_BIT(OVRES)));
  833. /* Clear any overrun happening while cleaning up */
  834. spi_readl(as, SR);
  835. as->done_status = -EIO;
  836. complete(&as->xfer_completion);
  837. } else if (pending & (SPI_BIT(RXBUFF) | SPI_BIT(ENDRX))) {
  838. ret = IRQ_HANDLED;
  839. spi_writel(as, IDR, pending);
  840. complete(&as->xfer_completion);
  841. }
  842. return ret;
  843. }
  844. static int atmel_spi_setup(struct spi_device *spi)
  845. {
  846. struct atmel_spi *as;
  847. struct atmel_spi_device *asd;
  848. u32 csr;
  849. unsigned int bits = spi->bits_per_word;
  850. unsigned int npcs_pin;
  851. int ret;
  852. as = spi_master_get_devdata(spi->master);
  853. if (spi->chip_select > spi->master->num_chipselect) {
  854. dev_dbg(&spi->dev,
  855. "setup: invalid chipselect %u (%u defined)\n",
  856. spi->chip_select, spi->master->num_chipselect);
  857. return -EINVAL;
  858. }
  859. /* see notes above re chipselect */
  860. if (!atmel_spi_is_v2(as)
  861. && spi->chip_select == 0
  862. && (spi->mode & SPI_CS_HIGH)) {
  863. dev_dbg(&spi->dev, "setup: can't be active-high\n");
  864. return -EINVAL;
  865. }
  866. csr = SPI_BF(BITS, bits - 8);
  867. if (spi->mode & SPI_CPOL)
  868. csr |= SPI_BIT(CPOL);
  869. if (!(spi->mode & SPI_CPHA))
  870. csr |= SPI_BIT(NCPHA);
  871. /* DLYBS is mostly irrelevant since we manage chipselect using GPIOs.
  872. *
  873. * DLYBCT would add delays between words, slowing down transfers.
  874. * It could potentially be useful to cope with DMA bottlenecks, but
  875. * in those cases it's probably best to just use a lower bitrate.
  876. */
  877. csr |= SPI_BF(DLYBS, 0);
  878. csr |= SPI_BF(DLYBCT, 0);
  879. /* chipselect must have been muxed as GPIO (e.g. in board setup) */
  880. npcs_pin = (unsigned int)spi->controller_data;
  881. if (gpio_is_valid(spi->cs_gpio))
  882. npcs_pin = spi->cs_gpio;
  883. asd = spi->controller_state;
  884. if (!asd) {
  885. asd = kzalloc(sizeof(struct atmel_spi_device), GFP_KERNEL);
  886. if (!asd)
  887. return -ENOMEM;
  888. ret = gpio_request(npcs_pin, dev_name(&spi->dev));
  889. if (ret) {
  890. kfree(asd);
  891. return ret;
  892. }
  893. asd->npcs_pin = npcs_pin;
  894. spi->controller_state = asd;
  895. gpio_direction_output(npcs_pin, !(spi->mode & SPI_CS_HIGH));
  896. }
  897. asd->csr = csr;
  898. dev_dbg(&spi->dev,
  899. "setup: bpw %u mode 0x%x -> csr%d %08x\n",
  900. bits, spi->mode, spi->chip_select, csr);
  901. if (!atmel_spi_is_v2(as))
  902. spi_writel(as, CSR0 + 4 * spi->chip_select, csr);
  903. return 0;
  904. }
  905. static int atmel_spi_one_transfer(struct spi_master *master,
  906. struct spi_message *msg,
  907. struct spi_transfer *xfer)
  908. {
  909. struct atmel_spi *as;
  910. struct spi_device *spi = msg->spi;
  911. u8 bits;
  912. u32 len;
  913. struct atmel_spi_device *asd;
  914. int timeout;
  915. int ret;
  916. as = spi_master_get_devdata(master);
  917. if (!(xfer->tx_buf || xfer->rx_buf) && xfer->len) {
  918. dev_dbg(&spi->dev, "missing rx or tx buf\n");
  919. return -EINVAL;
  920. }
  921. if (xfer->bits_per_word) {
  922. asd = spi->controller_state;
  923. bits = (asd->csr >> 4) & 0xf;
  924. if (bits != xfer->bits_per_word - 8) {
  925. dev_dbg(&spi->dev,
  926. "you can't yet change bits_per_word in transfers\n");
  927. return -ENOPROTOOPT;
  928. }
  929. }
  930. if (xfer->bits_per_word > 8) {
  931. if (xfer->len % 2) {
  932. dev_dbg(&spi->dev,
  933. "buffer len should be 16 bits aligned\n");
  934. return -EINVAL;
  935. }
  936. }
  937. /*
  938. * DMA map early, for performance (empties dcache ASAP) and
  939. * better fault reporting.
  940. */
  941. if ((!msg->is_dma_mapped)
  942. && (atmel_spi_use_dma(as, xfer) || as->use_pdc)) {
  943. if (atmel_spi_dma_map_xfer(as, xfer) < 0)
  944. return -ENOMEM;
  945. }
  946. atmel_spi_set_xfer_speed(as, msg->spi, xfer);
  947. as->done_status = 0;
  948. as->current_transfer = xfer;
  949. as->current_remaining_bytes = xfer->len;
  950. while (as->current_remaining_bytes) {
  951. reinit_completion(&as->xfer_completion);
  952. if (as->use_pdc) {
  953. atmel_spi_pdc_next_xfer(master, msg, xfer);
  954. } else if (atmel_spi_use_dma(as, xfer)) {
  955. len = as->current_remaining_bytes;
  956. ret = atmel_spi_next_xfer_dma_submit(master,
  957. xfer, &len);
  958. if (ret) {
  959. dev_err(&spi->dev,
  960. "unable to use DMA, fallback to PIO\n");
  961. atmel_spi_next_xfer_pio(master, xfer);
  962. } else {
  963. as->current_remaining_bytes -= len;
  964. }
  965. } else {
  966. atmel_spi_next_xfer_pio(master, xfer);
  967. }
  968. ret = wait_for_completion_timeout(&as->xfer_completion,
  969. SPI_DMA_TIMEOUT);
  970. if (WARN_ON(ret == 0)) {
  971. dev_err(&spi->dev,
  972. "spi trasfer timeout, err %d\n", ret);
  973. as->done_status = -EIO;
  974. } else {
  975. ret = 0;
  976. }
  977. if (as->done_status)
  978. break;
  979. }
  980. if (as->done_status) {
  981. if (as->use_pdc) {
  982. dev_warn(master->dev.parent,
  983. "overrun (%u/%u remaining)\n",
  984. spi_readl(as, TCR), spi_readl(as, RCR));
  985. /*
  986. * Clean up DMA registers and make sure the data
  987. * registers are empty.
  988. */
  989. spi_writel(as, RNCR, 0);
  990. spi_writel(as, TNCR, 0);
  991. spi_writel(as, RCR, 0);
  992. spi_writel(as, TCR, 0);
  993. for (timeout = 1000; timeout; timeout--)
  994. if (spi_readl(as, SR) & SPI_BIT(TXEMPTY))
  995. break;
  996. if (!timeout)
  997. dev_warn(master->dev.parent,
  998. "timeout waiting for TXEMPTY");
  999. while (spi_readl(as, SR) & SPI_BIT(RDRF))
  1000. spi_readl(as, RDR);
  1001. /* Clear any overrun happening while cleaning up */
  1002. spi_readl(as, SR);
  1003. } else if (atmel_spi_use_dma(as, xfer)) {
  1004. atmel_spi_stop_dma(as);
  1005. }
  1006. if (!msg->is_dma_mapped
  1007. && (atmel_spi_use_dma(as, xfer) || as->use_pdc))
  1008. atmel_spi_dma_unmap_xfer(master, xfer);
  1009. return 0;
  1010. } else {
  1011. /* only update length if no error */
  1012. msg->actual_length += xfer->len;
  1013. }
  1014. if (!msg->is_dma_mapped
  1015. && (atmel_spi_use_dma(as, xfer) || as->use_pdc))
  1016. atmel_spi_dma_unmap_xfer(master, xfer);
  1017. if (xfer->delay_usecs)
  1018. udelay(xfer->delay_usecs);
  1019. if (xfer->cs_change) {
  1020. if (list_is_last(&xfer->transfer_list,
  1021. &msg->transfers)) {
  1022. as->keep_cs = true;
  1023. } else {
  1024. as->cs_active = !as->cs_active;
  1025. if (as->cs_active)
  1026. cs_activate(as, msg->spi);
  1027. else
  1028. cs_deactivate(as, msg->spi);
  1029. }
  1030. }
  1031. return 0;
  1032. }
  1033. static int atmel_spi_transfer_one_message(struct spi_master *master,
  1034. struct spi_message *msg)
  1035. {
  1036. struct atmel_spi *as;
  1037. struct spi_transfer *xfer;
  1038. struct spi_device *spi = msg->spi;
  1039. int ret = 0;
  1040. as = spi_master_get_devdata(master);
  1041. dev_dbg(&spi->dev, "new message %p submitted for %s\n",
  1042. msg, dev_name(&spi->dev));
  1043. if (unlikely(list_empty(&msg->transfers)))
  1044. return -EINVAL;
  1045. atmel_spi_lock(as);
  1046. cs_activate(as, spi);
  1047. as->cs_active = true;
  1048. as->keep_cs = false;
  1049. msg->status = 0;
  1050. msg->actual_length = 0;
  1051. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  1052. ret = atmel_spi_one_transfer(master, msg, xfer);
  1053. if (ret)
  1054. goto msg_done;
  1055. }
  1056. if (as->use_pdc)
  1057. atmel_spi_disable_pdc_transfer(as);
  1058. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  1059. dev_dbg(&spi->dev,
  1060. " xfer %p: len %u tx %p/%08x rx %p/%08x\n",
  1061. xfer, xfer->len,
  1062. xfer->tx_buf, xfer->tx_dma,
  1063. xfer->rx_buf, xfer->rx_dma);
  1064. }
  1065. msg_done:
  1066. if (!as->keep_cs)
  1067. cs_deactivate(as, msg->spi);
  1068. atmel_spi_unlock(as);
  1069. msg->status = as->done_status;
  1070. spi_finalize_current_message(spi->master);
  1071. return ret;
  1072. }
  1073. static void atmel_spi_cleanup(struct spi_device *spi)
  1074. {
  1075. struct atmel_spi_device *asd = spi->controller_state;
  1076. unsigned gpio = (unsigned) spi->controller_data;
  1077. if (!asd)
  1078. return;
  1079. spi->controller_state = NULL;
  1080. gpio_free(gpio);
  1081. kfree(asd);
  1082. }
  1083. static inline unsigned int atmel_get_version(struct atmel_spi *as)
  1084. {
  1085. return spi_readl(as, VERSION) & 0x00000fff;
  1086. }
  1087. static void atmel_get_caps(struct atmel_spi *as)
  1088. {
  1089. unsigned int version;
  1090. version = atmel_get_version(as);
  1091. dev_info(&as->pdev->dev, "version: 0x%x\n", version);
  1092. as->caps.is_spi2 = version > 0x121;
  1093. as->caps.has_wdrbt = version >= 0x210;
  1094. as->caps.has_dma_support = version >= 0x212;
  1095. }
  1096. /*-------------------------------------------------------------------------*/
  1097. static int atmel_spi_probe(struct platform_device *pdev)
  1098. {
  1099. struct resource *regs;
  1100. int irq;
  1101. struct clk *clk;
  1102. int ret;
  1103. struct spi_master *master;
  1104. struct atmel_spi *as;
  1105. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1106. if (!regs)
  1107. return -ENXIO;
  1108. irq = platform_get_irq(pdev, 0);
  1109. if (irq < 0)
  1110. return irq;
  1111. clk = devm_clk_get(&pdev->dev, "spi_clk");
  1112. if (IS_ERR(clk))
  1113. return PTR_ERR(clk);
  1114. /* setup spi core then atmel-specific driver state */
  1115. ret = -ENOMEM;
  1116. master = spi_alloc_master(&pdev->dev, sizeof(*as));
  1117. if (!master)
  1118. goto out_free;
  1119. /* the spi->mode bits understood by this driver: */
  1120. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  1121. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(8, 16);
  1122. master->dev.of_node = pdev->dev.of_node;
  1123. master->bus_num = pdev->id;
  1124. master->num_chipselect = master->dev.of_node ? 0 : 4;
  1125. master->setup = atmel_spi_setup;
  1126. master->transfer_one_message = atmel_spi_transfer_one_message;
  1127. master->cleanup = atmel_spi_cleanup;
  1128. platform_set_drvdata(pdev, master);
  1129. as = spi_master_get_devdata(master);
  1130. /*
  1131. * Scratch buffer is used for throwaway rx and tx data.
  1132. * It's coherent to minimize dcache pollution.
  1133. */
  1134. as->buffer = dma_alloc_coherent(&pdev->dev, BUFFER_SIZE,
  1135. &as->buffer_dma, GFP_KERNEL);
  1136. if (!as->buffer)
  1137. goto out_free;
  1138. spin_lock_init(&as->lock);
  1139. as->pdev = pdev;
  1140. as->regs = devm_ioremap_resource(&pdev->dev, regs);
  1141. if (IS_ERR(as->regs)) {
  1142. ret = PTR_ERR(as->regs);
  1143. goto out_free_buffer;
  1144. }
  1145. as->phybase = regs->start;
  1146. as->irq = irq;
  1147. as->clk = clk;
  1148. init_completion(&as->xfer_completion);
  1149. atmel_get_caps(as);
  1150. as->use_dma = false;
  1151. as->use_pdc = false;
  1152. if (as->caps.has_dma_support) {
  1153. if (atmel_spi_configure_dma(as) == 0)
  1154. as->use_dma = true;
  1155. } else {
  1156. as->use_pdc = true;
  1157. }
  1158. if (as->caps.has_dma_support && !as->use_dma)
  1159. dev_info(&pdev->dev, "Atmel SPI Controller using PIO only\n");
  1160. if (as->use_pdc) {
  1161. ret = devm_request_irq(&pdev->dev, irq, atmel_spi_pdc_interrupt,
  1162. 0, dev_name(&pdev->dev), master);
  1163. } else {
  1164. ret = devm_request_irq(&pdev->dev, irq, atmel_spi_pio_interrupt,
  1165. 0, dev_name(&pdev->dev), master);
  1166. }
  1167. if (ret)
  1168. goto out_unmap_regs;
  1169. /* Initialize the hardware */
  1170. ret = clk_prepare_enable(clk);
  1171. if (ret)
  1172. goto out_free_irq;
  1173. spi_writel(as, CR, SPI_BIT(SWRST));
  1174. spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */
  1175. if (as->caps.has_wdrbt) {
  1176. spi_writel(as, MR, SPI_BIT(WDRBT) | SPI_BIT(MODFDIS)
  1177. | SPI_BIT(MSTR));
  1178. } else {
  1179. spi_writel(as, MR, SPI_BIT(MSTR) | SPI_BIT(MODFDIS));
  1180. }
  1181. if (as->use_pdc)
  1182. spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
  1183. spi_writel(as, CR, SPI_BIT(SPIEN));
  1184. /* go! */
  1185. dev_info(&pdev->dev, "Atmel SPI Controller at 0x%08lx (irq %d)\n",
  1186. (unsigned long)regs->start, irq);
  1187. ret = devm_spi_register_master(&pdev->dev, master);
  1188. if (ret)
  1189. goto out_free_dma;
  1190. return 0;
  1191. out_free_dma:
  1192. if (as->use_dma)
  1193. atmel_spi_release_dma(as);
  1194. spi_writel(as, CR, SPI_BIT(SWRST));
  1195. spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */
  1196. clk_disable_unprepare(clk);
  1197. out_free_irq:
  1198. out_unmap_regs:
  1199. out_free_buffer:
  1200. dma_free_coherent(&pdev->dev, BUFFER_SIZE, as->buffer,
  1201. as->buffer_dma);
  1202. out_free:
  1203. spi_master_put(master);
  1204. return ret;
  1205. }
  1206. static int atmel_spi_remove(struct platform_device *pdev)
  1207. {
  1208. struct spi_master *master = platform_get_drvdata(pdev);
  1209. struct atmel_spi *as = spi_master_get_devdata(master);
  1210. /* reset the hardware and block queue progress */
  1211. spin_lock_irq(&as->lock);
  1212. if (as->use_dma) {
  1213. atmel_spi_stop_dma(as);
  1214. atmel_spi_release_dma(as);
  1215. }
  1216. spi_writel(as, CR, SPI_BIT(SWRST));
  1217. spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */
  1218. spi_readl(as, SR);
  1219. spin_unlock_irq(&as->lock);
  1220. dma_free_coherent(&pdev->dev, BUFFER_SIZE, as->buffer,
  1221. as->buffer_dma);
  1222. clk_disable_unprepare(as->clk);
  1223. return 0;
  1224. }
  1225. #ifdef CONFIG_PM_SLEEP
  1226. static int atmel_spi_suspend(struct device *dev)
  1227. {
  1228. struct spi_master *master = dev_get_drvdata(dev);
  1229. struct atmel_spi *as = spi_master_get_devdata(master);
  1230. int ret;
  1231. /* Stop the queue running */
  1232. ret = spi_master_suspend(master);
  1233. if (ret) {
  1234. dev_warn(dev, "cannot suspend master\n");
  1235. return ret;
  1236. }
  1237. clk_disable_unprepare(as->clk);
  1238. return 0;
  1239. }
  1240. static int atmel_spi_resume(struct device *dev)
  1241. {
  1242. struct spi_master *master = dev_get_drvdata(dev);
  1243. struct atmel_spi *as = spi_master_get_devdata(master);
  1244. int ret;
  1245. clk_prepare_enable(as->clk);
  1246. /* Start the queue running */
  1247. ret = spi_master_resume(master);
  1248. if (ret)
  1249. dev_err(dev, "problem starting queue (%d)\n", ret);
  1250. return ret;
  1251. }
  1252. static SIMPLE_DEV_PM_OPS(atmel_spi_pm_ops, atmel_spi_suspend, atmel_spi_resume);
  1253. #define ATMEL_SPI_PM_OPS (&atmel_spi_pm_ops)
  1254. #else
  1255. #define ATMEL_SPI_PM_OPS NULL
  1256. #endif
  1257. #if defined(CONFIG_OF)
  1258. static const struct of_device_id atmel_spi_dt_ids[] = {
  1259. { .compatible = "atmel,at91rm9200-spi" },
  1260. { /* sentinel */ }
  1261. };
  1262. MODULE_DEVICE_TABLE(of, atmel_spi_dt_ids);
  1263. #endif
  1264. static struct platform_driver atmel_spi_driver = {
  1265. .driver = {
  1266. .name = "atmel_spi",
  1267. .owner = THIS_MODULE,
  1268. .pm = ATMEL_SPI_PM_OPS,
  1269. .of_match_table = of_match_ptr(atmel_spi_dt_ids),
  1270. },
  1271. .probe = atmel_spi_probe,
  1272. .remove = atmel_spi_remove,
  1273. };
  1274. module_platform_driver(atmel_spi_driver);
  1275. MODULE_DESCRIPTION("Atmel AT32/AT91 SPI Controller driver");
  1276. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  1277. MODULE_LICENSE("GPL");
  1278. MODULE_ALIAS("platform:atmel_spi");