spi-atmel.c 29 KB

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