spi-atmel.c 29 KB

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