spi-atmel.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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/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/of.h>
  23. #include <asm/io.h>
  24. #include <asm/gpio.h>
  25. #include <mach/cpu.h>
  26. /* SPI register offsets */
  27. #define SPI_CR 0x0000
  28. #define SPI_MR 0x0004
  29. #define SPI_RDR 0x0008
  30. #define SPI_TDR 0x000c
  31. #define SPI_SR 0x0010
  32. #define SPI_IER 0x0014
  33. #define SPI_IDR 0x0018
  34. #define SPI_IMR 0x001c
  35. #define SPI_CSR0 0x0030
  36. #define SPI_CSR1 0x0034
  37. #define SPI_CSR2 0x0038
  38. #define SPI_CSR3 0x003c
  39. #define SPI_RPR 0x0100
  40. #define SPI_RCR 0x0104
  41. #define SPI_TPR 0x0108
  42. #define SPI_TCR 0x010c
  43. #define SPI_RNPR 0x0110
  44. #define SPI_RNCR 0x0114
  45. #define SPI_TNPR 0x0118
  46. #define SPI_TNCR 0x011c
  47. #define SPI_PTCR 0x0120
  48. #define SPI_PTSR 0x0124
  49. /* Bitfields in CR */
  50. #define SPI_SPIEN_OFFSET 0
  51. #define SPI_SPIEN_SIZE 1
  52. #define SPI_SPIDIS_OFFSET 1
  53. #define SPI_SPIDIS_SIZE 1
  54. #define SPI_SWRST_OFFSET 7
  55. #define SPI_SWRST_SIZE 1
  56. #define SPI_LASTXFER_OFFSET 24
  57. #define SPI_LASTXFER_SIZE 1
  58. /* Bitfields in MR */
  59. #define SPI_MSTR_OFFSET 0
  60. #define SPI_MSTR_SIZE 1
  61. #define SPI_PS_OFFSET 1
  62. #define SPI_PS_SIZE 1
  63. #define SPI_PCSDEC_OFFSET 2
  64. #define SPI_PCSDEC_SIZE 1
  65. #define SPI_FDIV_OFFSET 3
  66. #define SPI_FDIV_SIZE 1
  67. #define SPI_MODFDIS_OFFSET 4
  68. #define SPI_MODFDIS_SIZE 1
  69. #define SPI_LLB_OFFSET 7
  70. #define SPI_LLB_SIZE 1
  71. #define SPI_PCS_OFFSET 16
  72. #define SPI_PCS_SIZE 4
  73. #define SPI_DLYBCS_OFFSET 24
  74. #define SPI_DLYBCS_SIZE 8
  75. /* Bitfields in RDR */
  76. #define SPI_RD_OFFSET 0
  77. #define SPI_RD_SIZE 16
  78. /* Bitfields in TDR */
  79. #define SPI_TD_OFFSET 0
  80. #define SPI_TD_SIZE 16
  81. /* Bitfields in SR */
  82. #define SPI_RDRF_OFFSET 0
  83. #define SPI_RDRF_SIZE 1
  84. #define SPI_TDRE_OFFSET 1
  85. #define SPI_TDRE_SIZE 1
  86. #define SPI_MODF_OFFSET 2
  87. #define SPI_MODF_SIZE 1
  88. #define SPI_OVRES_OFFSET 3
  89. #define SPI_OVRES_SIZE 1
  90. #define SPI_ENDRX_OFFSET 4
  91. #define SPI_ENDRX_SIZE 1
  92. #define SPI_ENDTX_OFFSET 5
  93. #define SPI_ENDTX_SIZE 1
  94. #define SPI_RXBUFF_OFFSET 6
  95. #define SPI_RXBUFF_SIZE 1
  96. #define SPI_TXBUFE_OFFSET 7
  97. #define SPI_TXBUFE_SIZE 1
  98. #define SPI_NSSR_OFFSET 8
  99. #define SPI_NSSR_SIZE 1
  100. #define SPI_TXEMPTY_OFFSET 9
  101. #define SPI_TXEMPTY_SIZE 1
  102. #define SPI_SPIENS_OFFSET 16
  103. #define SPI_SPIENS_SIZE 1
  104. /* Bitfields in CSR0 */
  105. #define SPI_CPOL_OFFSET 0
  106. #define SPI_CPOL_SIZE 1
  107. #define SPI_NCPHA_OFFSET 1
  108. #define SPI_NCPHA_SIZE 1
  109. #define SPI_CSAAT_OFFSET 3
  110. #define SPI_CSAAT_SIZE 1
  111. #define SPI_BITS_OFFSET 4
  112. #define SPI_BITS_SIZE 4
  113. #define SPI_SCBR_OFFSET 8
  114. #define SPI_SCBR_SIZE 8
  115. #define SPI_DLYBS_OFFSET 16
  116. #define SPI_DLYBS_SIZE 8
  117. #define SPI_DLYBCT_OFFSET 24
  118. #define SPI_DLYBCT_SIZE 8
  119. /* Bitfields in RCR */
  120. #define SPI_RXCTR_OFFSET 0
  121. #define SPI_RXCTR_SIZE 16
  122. /* Bitfields in TCR */
  123. #define SPI_TXCTR_OFFSET 0
  124. #define SPI_TXCTR_SIZE 16
  125. /* Bitfields in RNCR */
  126. #define SPI_RXNCR_OFFSET 0
  127. #define SPI_RXNCR_SIZE 16
  128. /* Bitfields in TNCR */
  129. #define SPI_TXNCR_OFFSET 0
  130. #define SPI_TXNCR_SIZE 16
  131. /* Bitfields in PTCR */
  132. #define SPI_RXTEN_OFFSET 0
  133. #define SPI_RXTEN_SIZE 1
  134. #define SPI_RXTDIS_OFFSET 1
  135. #define SPI_RXTDIS_SIZE 1
  136. #define SPI_TXTEN_OFFSET 8
  137. #define SPI_TXTEN_SIZE 1
  138. #define SPI_TXTDIS_OFFSET 9
  139. #define SPI_TXTDIS_SIZE 1
  140. /* Constants for BITS */
  141. #define SPI_BITS_8_BPT 0
  142. #define SPI_BITS_9_BPT 1
  143. #define SPI_BITS_10_BPT 2
  144. #define SPI_BITS_11_BPT 3
  145. #define SPI_BITS_12_BPT 4
  146. #define SPI_BITS_13_BPT 5
  147. #define SPI_BITS_14_BPT 6
  148. #define SPI_BITS_15_BPT 7
  149. #define SPI_BITS_16_BPT 8
  150. /* Bit manipulation macros */
  151. #define SPI_BIT(name) \
  152. (1 << SPI_##name##_OFFSET)
  153. #define SPI_BF(name,value) \
  154. (((value) & ((1 << SPI_##name##_SIZE) - 1)) << SPI_##name##_OFFSET)
  155. #define SPI_BFEXT(name,value) \
  156. (((value) >> SPI_##name##_OFFSET) & ((1 << SPI_##name##_SIZE) - 1))
  157. #define SPI_BFINS(name,value,old) \
  158. ( ((old) & ~(((1 << SPI_##name##_SIZE) - 1) << SPI_##name##_OFFSET)) \
  159. | SPI_BF(name,value))
  160. /* Register access macros */
  161. #define spi_readl(port,reg) \
  162. __raw_readl((port)->regs + SPI_##reg)
  163. #define spi_writel(port,reg,value) \
  164. __raw_writel((value), (port)->regs + SPI_##reg)
  165. /*
  166. * The core SPI transfer engine just talks to a register bank to set up
  167. * DMA transfers; transfer queue progress is driven by IRQs. The clock
  168. * framework provides the base clock, subdivided for each spi_device.
  169. */
  170. struct atmel_spi {
  171. spinlock_t lock;
  172. void __iomem *regs;
  173. int irq;
  174. struct clk *clk;
  175. struct platform_device *pdev;
  176. struct spi_device *stay;
  177. u8 stopping;
  178. struct list_head queue;
  179. struct spi_transfer *current_transfer;
  180. unsigned long current_remaining_bytes;
  181. struct spi_transfer *next_transfer;
  182. unsigned long next_remaining_bytes;
  183. void *buffer;
  184. dma_addr_t buffer_dma;
  185. };
  186. /* Controller-specific per-slave state */
  187. struct atmel_spi_device {
  188. unsigned int npcs_pin;
  189. u32 csr;
  190. };
  191. #define BUFFER_SIZE PAGE_SIZE
  192. #define INVALID_DMA_ADDRESS 0xffffffff
  193. /*
  194. * Version 2 of the SPI controller has
  195. * - CR.LASTXFER
  196. * - SPI_MR.DIV32 may become FDIV or must-be-zero (here: always zero)
  197. * - SPI_SR.TXEMPTY, SPI_SR.NSSR (and corresponding irqs)
  198. * - SPI_CSRx.CSAAT
  199. * - SPI_CSRx.SBCR allows faster clocking
  200. *
  201. * We can determine the controller version by reading the VERSION
  202. * register, but I haven't checked that it exists on all chips, and
  203. * this is cheaper anyway.
  204. */
  205. static bool atmel_spi_is_v2(void)
  206. {
  207. return !cpu_is_at91rm9200();
  208. }
  209. /*
  210. * Earlier SPI controllers (e.g. on at91rm9200) have a design bug whereby
  211. * they assume that spi slave device state will not change on deselect, so
  212. * that automagic deselection is OK. ("NPCSx rises if no data is to be
  213. * transmitted") Not so! Workaround uses nCSx pins as GPIOs; or newer
  214. * controllers have CSAAT and friends.
  215. *
  216. * Since the CSAAT functionality is a bit weird on newer controllers as
  217. * well, we use GPIO to control nCSx pins on all controllers, updating
  218. * MR.PCS to avoid confusing the controller. Using GPIOs also lets us
  219. * support active-high chipselects despite the controller's belief that
  220. * only active-low devices/systems exists.
  221. *
  222. * However, at91rm9200 has a second erratum whereby nCS0 doesn't work
  223. * right when driven with GPIO. ("Mode Fault does not allow more than one
  224. * Master on Chip Select 0.") No workaround exists for that ... so for
  225. * nCS0 on that chip, we (a) don't use the GPIO, (b) can't support CS_HIGH,
  226. * and (c) will trigger that first erratum in some cases.
  227. *
  228. * TODO: Test if the atmel_spi_is_v2() branch below works on
  229. * AT91RM9200 if we use some other register than CSR0. However, don't
  230. * do this unconditionally since AP7000 has an errata where the BITS
  231. * field in CSR0 overrides all other CSRs.
  232. */
  233. static void cs_activate(struct atmel_spi *as, struct spi_device *spi)
  234. {
  235. struct atmel_spi_device *asd = spi->controller_state;
  236. unsigned active = spi->mode & SPI_CS_HIGH;
  237. u32 mr;
  238. if (atmel_spi_is_v2()) {
  239. /*
  240. * Always use CSR0. This ensures that the clock
  241. * switches to the correct idle polarity before we
  242. * toggle the CS.
  243. */
  244. spi_writel(as, CSR0, asd->csr);
  245. spi_writel(as, MR, SPI_BF(PCS, 0x0e) | SPI_BIT(MODFDIS)
  246. | SPI_BIT(MSTR));
  247. mr = spi_readl(as, MR);
  248. gpio_set_value(asd->npcs_pin, active);
  249. } else {
  250. u32 cpol = (spi->mode & SPI_CPOL) ? SPI_BIT(CPOL) : 0;
  251. int i;
  252. u32 csr;
  253. /* Make sure clock polarity is correct */
  254. for (i = 0; i < spi->master->num_chipselect; i++) {
  255. csr = spi_readl(as, CSR0 + 4 * i);
  256. if ((csr ^ cpol) & SPI_BIT(CPOL))
  257. spi_writel(as, CSR0 + 4 * i,
  258. csr ^ SPI_BIT(CPOL));
  259. }
  260. mr = spi_readl(as, MR);
  261. mr = SPI_BFINS(PCS, ~(1 << spi->chip_select), mr);
  262. if (spi->chip_select != 0)
  263. gpio_set_value(asd->npcs_pin, active);
  264. spi_writel(as, MR, mr);
  265. }
  266. dev_dbg(&spi->dev, "activate %u%s, mr %08x\n",
  267. asd->npcs_pin, active ? " (high)" : "",
  268. mr);
  269. }
  270. static void cs_deactivate(struct atmel_spi *as, struct spi_device *spi)
  271. {
  272. struct atmel_spi_device *asd = spi->controller_state;
  273. unsigned active = spi->mode & SPI_CS_HIGH;
  274. u32 mr;
  275. /* only deactivate *this* device; sometimes transfers to
  276. * another device may be active when this routine is called.
  277. */
  278. mr = spi_readl(as, MR);
  279. if (~SPI_BFEXT(PCS, mr) & (1 << spi->chip_select)) {
  280. mr = SPI_BFINS(PCS, 0xf, mr);
  281. spi_writel(as, MR, mr);
  282. }
  283. dev_dbg(&spi->dev, "DEactivate %u%s, mr %08x\n",
  284. asd->npcs_pin, active ? " (low)" : "",
  285. mr);
  286. if (atmel_spi_is_v2() || spi->chip_select != 0)
  287. gpio_set_value(asd->npcs_pin, !active);
  288. }
  289. static inline int atmel_spi_xfer_is_last(struct spi_message *msg,
  290. struct spi_transfer *xfer)
  291. {
  292. return msg->transfers.prev == &xfer->transfer_list;
  293. }
  294. static inline int atmel_spi_xfer_can_be_chained(struct spi_transfer *xfer)
  295. {
  296. return xfer->delay_usecs == 0 && !xfer->cs_change;
  297. }
  298. static void atmel_spi_next_xfer_data(struct spi_master *master,
  299. struct spi_transfer *xfer,
  300. dma_addr_t *tx_dma,
  301. dma_addr_t *rx_dma,
  302. u32 *plen)
  303. {
  304. struct atmel_spi *as = spi_master_get_devdata(master);
  305. u32 len = *plen;
  306. /* use scratch buffer only when rx or tx data is unspecified */
  307. if (xfer->rx_buf)
  308. *rx_dma = xfer->rx_dma + xfer->len - *plen;
  309. else {
  310. *rx_dma = as->buffer_dma;
  311. if (len > BUFFER_SIZE)
  312. len = BUFFER_SIZE;
  313. }
  314. if (xfer->tx_buf)
  315. *tx_dma = xfer->tx_dma + xfer->len - *plen;
  316. else {
  317. *tx_dma = as->buffer_dma;
  318. if (len > BUFFER_SIZE)
  319. len = BUFFER_SIZE;
  320. memset(as->buffer, 0, len);
  321. dma_sync_single_for_device(&as->pdev->dev,
  322. as->buffer_dma, len, DMA_TO_DEVICE);
  323. }
  324. *plen = len;
  325. }
  326. /*
  327. * Submit next transfer for DMA.
  328. * lock is held, spi irq is blocked
  329. */
  330. static void atmel_spi_next_xfer(struct spi_master *master,
  331. struct spi_message *msg)
  332. {
  333. struct atmel_spi *as = spi_master_get_devdata(master);
  334. struct spi_transfer *xfer;
  335. u32 len, remaining;
  336. u32 ieval;
  337. dma_addr_t tx_dma, rx_dma;
  338. if (!as->current_transfer)
  339. xfer = list_entry(msg->transfers.next,
  340. struct spi_transfer, transfer_list);
  341. else if (!as->next_transfer)
  342. xfer = list_entry(as->current_transfer->transfer_list.next,
  343. struct spi_transfer, transfer_list);
  344. else
  345. xfer = NULL;
  346. if (xfer) {
  347. spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
  348. len = xfer->len;
  349. atmel_spi_next_xfer_data(master, xfer, &tx_dma, &rx_dma, &len);
  350. remaining = xfer->len - len;
  351. spi_writel(as, RPR, rx_dma);
  352. spi_writel(as, TPR, tx_dma);
  353. if (msg->spi->bits_per_word > 8)
  354. len >>= 1;
  355. spi_writel(as, RCR, len);
  356. spi_writel(as, TCR, len);
  357. dev_dbg(&msg->spi->dev,
  358. " start xfer %p: len %u tx %p/%08x rx %p/%08x\n",
  359. xfer, xfer->len, xfer->tx_buf, xfer->tx_dma,
  360. xfer->rx_buf, xfer->rx_dma);
  361. } else {
  362. xfer = as->next_transfer;
  363. remaining = as->next_remaining_bytes;
  364. }
  365. as->current_transfer = xfer;
  366. as->current_remaining_bytes = remaining;
  367. if (remaining > 0)
  368. len = remaining;
  369. else if (!atmel_spi_xfer_is_last(msg, xfer)
  370. && atmel_spi_xfer_can_be_chained(xfer)) {
  371. xfer = list_entry(xfer->transfer_list.next,
  372. struct spi_transfer, transfer_list);
  373. len = xfer->len;
  374. } else
  375. xfer = NULL;
  376. as->next_transfer = xfer;
  377. if (xfer) {
  378. u32 total;
  379. total = len;
  380. atmel_spi_next_xfer_data(master, xfer, &tx_dma, &rx_dma, &len);
  381. as->next_remaining_bytes = total - len;
  382. spi_writel(as, RNPR, rx_dma);
  383. spi_writel(as, TNPR, tx_dma);
  384. if (msg->spi->bits_per_word > 8)
  385. len >>= 1;
  386. spi_writel(as, RNCR, len);
  387. spi_writel(as, TNCR, len);
  388. dev_dbg(&msg->spi->dev,
  389. " next xfer %p: len %u tx %p/%08x rx %p/%08x\n",
  390. xfer, xfer->len, xfer->tx_buf, xfer->tx_dma,
  391. xfer->rx_buf, xfer->rx_dma);
  392. ieval = SPI_BIT(ENDRX) | SPI_BIT(OVRES);
  393. } else {
  394. spi_writel(as, RNCR, 0);
  395. spi_writel(as, TNCR, 0);
  396. ieval = SPI_BIT(RXBUFF) | SPI_BIT(ENDRX) | SPI_BIT(OVRES);
  397. }
  398. /* REVISIT: We're waiting for ENDRX before we start the next
  399. * transfer because we need to handle some difficult timing
  400. * issues otherwise. If we wait for ENDTX in one transfer and
  401. * then starts waiting for ENDRX in the next, it's difficult
  402. * to tell the difference between the ENDRX interrupt we're
  403. * actually waiting for and the ENDRX interrupt of the
  404. * previous transfer.
  405. *
  406. * It should be doable, though. Just not now...
  407. */
  408. spi_writel(as, IER, ieval);
  409. spi_writel(as, PTCR, SPI_BIT(TXTEN) | SPI_BIT(RXTEN));
  410. }
  411. static void atmel_spi_next_message(struct spi_master *master)
  412. {
  413. struct atmel_spi *as = spi_master_get_devdata(master);
  414. struct spi_message *msg;
  415. struct spi_device *spi;
  416. BUG_ON(as->current_transfer);
  417. msg = list_entry(as->queue.next, struct spi_message, queue);
  418. spi = msg->spi;
  419. dev_dbg(master->dev.parent, "start message %p for %s\n",
  420. msg, dev_name(&spi->dev));
  421. /* select chip if it's not still active */
  422. if (as->stay) {
  423. if (as->stay != spi) {
  424. cs_deactivate(as, as->stay);
  425. cs_activate(as, spi);
  426. }
  427. as->stay = NULL;
  428. } else
  429. cs_activate(as, spi);
  430. atmel_spi_next_xfer(master, msg);
  431. }
  432. /*
  433. * For DMA, tx_buf/tx_dma have the same relationship as rx_buf/rx_dma:
  434. * - The buffer is either valid for CPU access, else NULL
  435. * - If the buffer is valid, so is its DMA address
  436. *
  437. * This driver manages the dma address unless message->is_dma_mapped.
  438. */
  439. static int
  440. atmel_spi_dma_map_xfer(struct atmel_spi *as, struct spi_transfer *xfer)
  441. {
  442. struct device *dev = &as->pdev->dev;
  443. xfer->tx_dma = xfer->rx_dma = INVALID_DMA_ADDRESS;
  444. if (xfer->tx_buf) {
  445. /* tx_buf is a const void* where we need a void * for the dma
  446. * mapping */
  447. void *nonconst_tx = (void *)xfer->tx_buf;
  448. xfer->tx_dma = dma_map_single(dev,
  449. nonconst_tx, xfer->len,
  450. DMA_TO_DEVICE);
  451. if (dma_mapping_error(dev, xfer->tx_dma))
  452. return -ENOMEM;
  453. }
  454. if (xfer->rx_buf) {
  455. xfer->rx_dma = dma_map_single(dev,
  456. xfer->rx_buf, xfer->len,
  457. DMA_FROM_DEVICE);
  458. if (dma_mapping_error(dev, xfer->rx_dma)) {
  459. if (xfer->tx_buf)
  460. dma_unmap_single(dev,
  461. xfer->tx_dma, xfer->len,
  462. DMA_TO_DEVICE);
  463. return -ENOMEM;
  464. }
  465. }
  466. return 0;
  467. }
  468. static void atmel_spi_dma_unmap_xfer(struct spi_master *master,
  469. struct spi_transfer *xfer)
  470. {
  471. if (xfer->tx_dma != INVALID_DMA_ADDRESS)
  472. dma_unmap_single(master->dev.parent, xfer->tx_dma,
  473. xfer->len, DMA_TO_DEVICE);
  474. if (xfer->rx_dma != INVALID_DMA_ADDRESS)
  475. dma_unmap_single(master->dev.parent, xfer->rx_dma,
  476. xfer->len, DMA_FROM_DEVICE);
  477. }
  478. static void
  479. atmel_spi_msg_done(struct spi_master *master, struct atmel_spi *as,
  480. struct spi_message *msg, int status, int stay)
  481. {
  482. if (!stay || status < 0)
  483. cs_deactivate(as, msg->spi);
  484. else
  485. as->stay = msg->spi;
  486. list_del(&msg->queue);
  487. msg->status = status;
  488. dev_dbg(master->dev.parent,
  489. "xfer complete: %u bytes transferred\n",
  490. msg->actual_length);
  491. spin_unlock(&as->lock);
  492. msg->complete(msg->context);
  493. spin_lock(&as->lock);
  494. as->current_transfer = NULL;
  495. as->next_transfer = NULL;
  496. /* continue if needed */
  497. if (list_empty(&as->queue) || as->stopping)
  498. spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
  499. else
  500. atmel_spi_next_message(master);
  501. }
  502. static irqreturn_t
  503. atmel_spi_interrupt(int irq, void *dev_id)
  504. {
  505. struct spi_master *master = dev_id;
  506. struct atmel_spi *as = spi_master_get_devdata(master);
  507. struct spi_message *msg;
  508. struct spi_transfer *xfer;
  509. u32 status, pending, imr;
  510. int ret = IRQ_NONE;
  511. spin_lock(&as->lock);
  512. xfer = as->current_transfer;
  513. msg = list_entry(as->queue.next, struct spi_message, queue);
  514. imr = spi_readl(as, IMR);
  515. status = spi_readl(as, SR);
  516. pending = status & imr;
  517. if (pending & SPI_BIT(OVRES)) {
  518. int timeout;
  519. ret = IRQ_HANDLED;
  520. spi_writel(as, IDR, (SPI_BIT(RXBUFF) | SPI_BIT(ENDRX)
  521. | SPI_BIT(OVRES)));
  522. /*
  523. * When we get an overrun, we disregard the current
  524. * transfer. Data will not be copied back from any
  525. * bounce buffer and msg->actual_len will not be
  526. * updated with the last xfer.
  527. *
  528. * We will also not process any remaning transfers in
  529. * the message.
  530. *
  531. * First, stop the transfer and unmap the DMA buffers.
  532. */
  533. spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
  534. if (!msg->is_dma_mapped)
  535. atmel_spi_dma_unmap_xfer(master, xfer);
  536. /* REVISIT: udelay in irq is unfriendly */
  537. if (xfer->delay_usecs)
  538. udelay(xfer->delay_usecs);
  539. dev_warn(master->dev.parent, "overrun (%u/%u remaining)\n",
  540. spi_readl(as, TCR), spi_readl(as, RCR));
  541. /*
  542. * Clean up DMA registers and make sure the data
  543. * registers are empty.
  544. */
  545. spi_writel(as, RNCR, 0);
  546. spi_writel(as, TNCR, 0);
  547. spi_writel(as, RCR, 0);
  548. spi_writel(as, TCR, 0);
  549. for (timeout = 1000; timeout; timeout--)
  550. if (spi_readl(as, SR) & SPI_BIT(TXEMPTY))
  551. break;
  552. if (!timeout)
  553. dev_warn(master->dev.parent,
  554. "timeout waiting for TXEMPTY");
  555. while (spi_readl(as, SR) & SPI_BIT(RDRF))
  556. spi_readl(as, RDR);
  557. /* Clear any overrun happening while cleaning up */
  558. spi_readl(as, SR);
  559. atmel_spi_msg_done(master, as, msg, -EIO, 0);
  560. } else if (pending & (SPI_BIT(RXBUFF) | SPI_BIT(ENDRX))) {
  561. ret = IRQ_HANDLED;
  562. spi_writel(as, IDR, pending);
  563. if (as->current_remaining_bytes == 0) {
  564. msg->actual_length += xfer->len;
  565. if (!msg->is_dma_mapped)
  566. atmel_spi_dma_unmap_xfer(master, xfer);
  567. /* REVISIT: udelay in irq is unfriendly */
  568. if (xfer->delay_usecs)
  569. udelay(xfer->delay_usecs);
  570. if (atmel_spi_xfer_is_last(msg, xfer)) {
  571. /* report completed message */
  572. atmel_spi_msg_done(master, as, msg, 0,
  573. xfer->cs_change);
  574. } else {
  575. if (xfer->cs_change) {
  576. cs_deactivate(as, msg->spi);
  577. udelay(1);
  578. cs_activate(as, msg->spi);
  579. }
  580. /*
  581. * Not done yet. Submit the next transfer.
  582. *
  583. * FIXME handle protocol options for xfer
  584. */
  585. atmel_spi_next_xfer(master, msg);
  586. }
  587. } else {
  588. /*
  589. * Keep going, we still have data to send in
  590. * the current transfer.
  591. */
  592. atmel_spi_next_xfer(master, msg);
  593. }
  594. }
  595. spin_unlock(&as->lock);
  596. return ret;
  597. }
  598. static int atmel_spi_setup(struct spi_device *spi)
  599. {
  600. struct atmel_spi *as;
  601. struct atmel_spi_device *asd;
  602. u32 scbr, csr;
  603. unsigned int bits = spi->bits_per_word;
  604. unsigned long bus_hz;
  605. unsigned int npcs_pin;
  606. int ret;
  607. as = spi_master_get_devdata(spi->master);
  608. if (as->stopping)
  609. return -ESHUTDOWN;
  610. if (spi->chip_select > spi->master->num_chipselect) {
  611. dev_dbg(&spi->dev,
  612. "setup: invalid chipselect %u (%u defined)\n",
  613. spi->chip_select, spi->master->num_chipselect);
  614. return -EINVAL;
  615. }
  616. if (bits < 8 || bits > 16) {
  617. dev_dbg(&spi->dev,
  618. "setup: invalid bits_per_word %u (8 to 16)\n",
  619. bits);
  620. return -EINVAL;
  621. }
  622. /* see notes above re chipselect */
  623. if (!atmel_spi_is_v2()
  624. && spi->chip_select == 0
  625. && (spi->mode & SPI_CS_HIGH)) {
  626. dev_dbg(&spi->dev, "setup: can't be active-high\n");
  627. return -EINVAL;
  628. }
  629. /* v1 chips start out at half the peripheral bus speed. */
  630. bus_hz = clk_get_rate(as->clk);
  631. if (!atmel_spi_is_v2())
  632. bus_hz /= 2;
  633. if (spi->max_speed_hz) {
  634. /*
  635. * Calculate the lowest divider that satisfies the
  636. * constraint, assuming div32/fdiv/mbz == 0.
  637. */
  638. scbr = DIV_ROUND_UP(bus_hz, spi->max_speed_hz);
  639. /*
  640. * If the resulting divider doesn't fit into the
  641. * register bitfield, we can't satisfy the constraint.
  642. */
  643. if (scbr >= (1 << SPI_SCBR_SIZE)) {
  644. dev_dbg(&spi->dev,
  645. "setup: %d Hz too slow, scbr %u; min %ld Hz\n",
  646. spi->max_speed_hz, scbr, bus_hz/255);
  647. return -EINVAL;
  648. }
  649. } else
  650. /* speed zero means "as slow as possible" */
  651. scbr = 0xff;
  652. csr = SPI_BF(SCBR, scbr) | SPI_BF(BITS, bits - 8);
  653. if (spi->mode & SPI_CPOL)
  654. csr |= SPI_BIT(CPOL);
  655. if (!(spi->mode & SPI_CPHA))
  656. csr |= SPI_BIT(NCPHA);
  657. /* DLYBS is mostly irrelevant since we manage chipselect using GPIOs.
  658. *
  659. * DLYBCT would add delays between words, slowing down transfers.
  660. * It could potentially be useful to cope with DMA bottlenecks, but
  661. * in those cases it's probably best to just use a lower bitrate.
  662. */
  663. csr |= SPI_BF(DLYBS, 0);
  664. csr |= SPI_BF(DLYBCT, 0);
  665. /* chipselect must have been muxed as GPIO (e.g. in board setup) */
  666. npcs_pin = (unsigned int)spi->controller_data;
  667. if (gpio_is_valid(spi->cs_gpio))
  668. npcs_pin = spi->cs_gpio;
  669. asd = spi->controller_state;
  670. if (!asd) {
  671. asd = kzalloc(sizeof(struct atmel_spi_device), GFP_KERNEL);
  672. if (!asd)
  673. return -ENOMEM;
  674. ret = gpio_request(npcs_pin, dev_name(&spi->dev));
  675. if (ret) {
  676. kfree(asd);
  677. return ret;
  678. }
  679. asd->npcs_pin = npcs_pin;
  680. spi->controller_state = asd;
  681. gpio_direction_output(npcs_pin, !(spi->mode & SPI_CS_HIGH));
  682. } else {
  683. unsigned long flags;
  684. spin_lock_irqsave(&as->lock, flags);
  685. if (as->stay == spi)
  686. as->stay = NULL;
  687. cs_deactivate(as, spi);
  688. spin_unlock_irqrestore(&as->lock, flags);
  689. }
  690. asd->csr = csr;
  691. dev_dbg(&spi->dev,
  692. "setup: %lu Hz bpw %u mode 0x%x -> csr%d %08x\n",
  693. bus_hz / scbr, bits, spi->mode, spi->chip_select, csr);
  694. if (!atmel_spi_is_v2())
  695. spi_writel(as, CSR0 + 4 * spi->chip_select, csr);
  696. return 0;
  697. }
  698. static int atmel_spi_transfer(struct spi_device *spi, struct spi_message *msg)
  699. {
  700. struct atmel_spi *as;
  701. struct spi_transfer *xfer;
  702. unsigned long flags;
  703. struct device *controller = spi->master->dev.parent;
  704. u8 bits;
  705. struct atmel_spi_device *asd;
  706. as = spi_master_get_devdata(spi->master);
  707. dev_dbg(controller, "new message %p submitted for %s\n",
  708. msg, dev_name(&spi->dev));
  709. if (unlikely(list_empty(&msg->transfers)))
  710. return -EINVAL;
  711. if (as->stopping)
  712. return -ESHUTDOWN;
  713. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  714. if (!(xfer->tx_buf || xfer->rx_buf) && xfer->len) {
  715. dev_dbg(&spi->dev, "missing rx or tx buf\n");
  716. return -EINVAL;
  717. }
  718. if (xfer->bits_per_word) {
  719. asd = spi->controller_state;
  720. bits = (asd->csr >> 4) & 0xf;
  721. if (bits != xfer->bits_per_word - 8) {
  722. dev_dbg(&spi->dev, "you can't yet change "
  723. "bits_per_word in transfers\n");
  724. return -ENOPROTOOPT;
  725. }
  726. }
  727. /* FIXME implement these protocol options!! */
  728. if (xfer->speed_hz) {
  729. dev_dbg(&spi->dev, "no protocol options yet\n");
  730. return -ENOPROTOOPT;
  731. }
  732. /*
  733. * DMA map early, for performance (empties dcache ASAP) and
  734. * better fault reporting. This is a DMA-only driver.
  735. *
  736. * NOTE that if dma_unmap_single() ever starts to do work on
  737. * platforms supported by this driver, we would need to clean
  738. * up mappings for previously-mapped transfers.
  739. */
  740. if (!msg->is_dma_mapped) {
  741. if (atmel_spi_dma_map_xfer(as, xfer) < 0)
  742. return -ENOMEM;
  743. }
  744. }
  745. #ifdef VERBOSE
  746. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  747. dev_dbg(controller,
  748. " xfer %p: len %u tx %p/%08x rx %p/%08x\n",
  749. xfer, xfer->len,
  750. xfer->tx_buf, xfer->tx_dma,
  751. xfer->rx_buf, xfer->rx_dma);
  752. }
  753. #endif
  754. msg->status = -EINPROGRESS;
  755. msg->actual_length = 0;
  756. spin_lock_irqsave(&as->lock, flags);
  757. list_add_tail(&msg->queue, &as->queue);
  758. if (!as->current_transfer)
  759. atmel_spi_next_message(spi->master);
  760. spin_unlock_irqrestore(&as->lock, flags);
  761. return 0;
  762. }
  763. static void atmel_spi_cleanup(struct spi_device *spi)
  764. {
  765. struct atmel_spi *as = spi_master_get_devdata(spi->master);
  766. struct atmel_spi_device *asd = spi->controller_state;
  767. unsigned gpio = (unsigned) spi->controller_data;
  768. unsigned long flags;
  769. if (!asd)
  770. return;
  771. spin_lock_irqsave(&as->lock, flags);
  772. if (as->stay == spi) {
  773. as->stay = NULL;
  774. cs_deactivate(as, spi);
  775. }
  776. spin_unlock_irqrestore(&as->lock, flags);
  777. spi->controller_state = NULL;
  778. gpio_free(gpio);
  779. kfree(asd);
  780. }
  781. /*-------------------------------------------------------------------------*/
  782. static int atmel_spi_probe(struct platform_device *pdev)
  783. {
  784. struct resource *regs;
  785. int irq;
  786. struct clk *clk;
  787. int ret;
  788. struct spi_master *master;
  789. struct atmel_spi *as;
  790. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  791. if (!regs)
  792. return -ENXIO;
  793. irq = platform_get_irq(pdev, 0);
  794. if (irq < 0)
  795. return irq;
  796. clk = clk_get(&pdev->dev, "spi_clk");
  797. if (IS_ERR(clk))
  798. return PTR_ERR(clk);
  799. /* setup spi core then atmel-specific driver state */
  800. ret = -ENOMEM;
  801. master = spi_alloc_master(&pdev->dev, sizeof *as);
  802. if (!master)
  803. goto out_free;
  804. /* the spi->mode bits understood by this driver: */
  805. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  806. master->dev.of_node = pdev->dev.of_node;
  807. master->bus_num = pdev->id;
  808. master->num_chipselect = master->dev.of_node ? 0 : 4;
  809. master->setup = atmel_spi_setup;
  810. master->transfer = atmel_spi_transfer;
  811. master->cleanup = atmel_spi_cleanup;
  812. platform_set_drvdata(pdev, master);
  813. as = spi_master_get_devdata(master);
  814. /*
  815. * Scratch buffer is used for throwaway rx and tx data.
  816. * It's coherent to minimize dcache pollution.
  817. */
  818. as->buffer = dma_alloc_coherent(&pdev->dev, BUFFER_SIZE,
  819. &as->buffer_dma, GFP_KERNEL);
  820. if (!as->buffer)
  821. goto out_free;
  822. spin_lock_init(&as->lock);
  823. INIT_LIST_HEAD(&as->queue);
  824. as->pdev = pdev;
  825. as->regs = ioremap(regs->start, resource_size(regs));
  826. if (!as->regs)
  827. goto out_free_buffer;
  828. as->irq = irq;
  829. as->clk = clk;
  830. ret = request_irq(irq, atmel_spi_interrupt, 0,
  831. dev_name(&pdev->dev), master);
  832. if (ret)
  833. goto out_unmap_regs;
  834. /* Initialize the hardware */
  835. clk_enable(clk);
  836. spi_writel(as, CR, SPI_BIT(SWRST));
  837. spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */
  838. spi_writel(as, MR, SPI_BIT(MSTR) | SPI_BIT(MODFDIS));
  839. spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
  840. spi_writel(as, CR, SPI_BIT(SPIEN));
  841. /* go! */
  842. dev_info(&pdev->dev, "Atmel SPI Controller at 0x%08lx (irq %d)\n",
  843. (unsigned long)regs->start, irq);
  844. ret = spi_register_master(master);
  845. if (ret)
  846. goto out_reset_hw;
  847. return 0;
  848. out_reset_hw:
  849. spi_writel(as, CR, SPI_BIT(SWRST));
  850. spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */
  851. clk_disable(clk);
  852. free_irq(irq, master);
  853. out_unmap_regs:
  854. iounmap(as->regs);
  855. out_free_buffer:
  856. dma_free_coherent(&pdev->dev, BUFFER_SIZE, as->buffer,
  857. as->buffer_dma);
  858. out_free:
  859. clk_put(clk);
  860. spi_master_put(master);
  861. return ret;
  862. }
  863. static int atmel_spi_remove(struct platform_device *pdev)
  864. {
  865. struct spi_master *master = platform_get_drvdata(pdev);
  866. struct atmel_spi *as = spi_master_get_devdata(master);
  867. struct spi_message *msg;
  868. /* reset the hardware and block queue progress */
  869. spin_lock_irq(&as->lock);
  870. as->stopping = 1;
  871. spi_writel(as, CR, SPI_BIT(SWRST));
  872. spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */
  873. spi_readl(as, SR);
  874. spin_unlock_irq(&as->lock);
  875. /* Terminate remaining queued transfers */
  876. list_for_each_entry(msg, &as->queue, queue) {
  877. /* REVISIT unmapping the dma is a NOP on ARM and AVR32
  878. * but we shouldn't depend on that...
  879. */
  880. msg->status = -ESHUTDOWN;
  881. msg->complete(msg->context);
  882. }
  883. dma_free_coherent(&pdev->dev, BUFFER_SIZE, as->buffer,
  884. as->buffer_dma);
  885. clk_disable(as->clk);
  886. clk_put(as->clk);
  887. free_irq(as->irq, master);
  888. iounmap(as->regs);
  889. spi_unregister_master(master);
  890. return 0;
  891. }
  892. #ifdef CONFIG_PM
  893. static int atmel_spi_suspend(struct platform_device *pdev, pm_message_t mesg)
  894. {
  895. struct spi_master *master = platform_get_drvdata(pdev);
  896. struct atmel_spi *as = spi_master_get_devdata(master);
  897. clk_disable(as->clk);
  898. return 0;
  899. }
  900. static int atmel_spi_resume(struct platform_device *pdev)
  901. {
  902. struct spi_master *master = platform_get_drvdata(pdev);
  903. struct atmel_spi *as = spi_master_get_devdata(master);
  904. clk_enable(as->clk);
  905. return 0;
  906. }
  907. #else
  908. #define atmel_spi_suspend NULL
  909. #define atmel_spi_resume NULL
  910. #endif
  911. #if defined(CONFIG_OF)
  912. static const struct of_device_id atmel_spi_dt_ids[] = {
  913. { .compatible = "atmel,at91rm9200-spi" },
  914. { /* sentinel */ }
  915. };
  916. MODULE_DEVICE_TABLE(of, atmel_spi_dt_ids);
  917. #endif
  918. static struct platform_driver atmel_spi_driver = {
  919. .driver = {
  920. .name = "atmel_spi",
  921. .owner = THIS_MODULE,
  922. .of_match_table = of_match_ptr(atmel_spi_dt_ids),
  923. },
  924. .suspend = atmel_spi_suspend,
  925. .resume = atmel_spi_resume,
  926. .probe = atmel_spi_probe,
  927. .remove = atmel_spi_remove,
  928. };
  929. module_platform_driver(atmel_spi_driver);
  930. MODULE_DESCRIPTION("Atmel AT32/AT91 SPI Controller driver");
  931. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  932. MODULE_LICENSE("GPL");
  933. MODULE_ALIAS("platform:atmel_spi");