spi-au1550.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. /*
  2. * au1550 psc spi controller driver
  3. * may work also with au1200, au1210, au1250
  4. * will not work on au1000, au1100 and au1500 (no full spi controller there)
  5. *
  6. * Copyright (c) 2006 ATRON electronic GmbH
  7. * Author: Jan Nikitenko <jan.nikitenko@gmail.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include <linux/init.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/slab.h>
  22. #include <linux/errno.h>
  23. #include <linux/module.h>
  24. #include <linux/device.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/resource.h>
  27. #include <linux/spi/spi.h>
  28. #include <linux/spi/spi_bitbang.h>
  29. #include <linux/dma-mapping.h>
  30. #include <linux/completion.h>
  31. #include <asm/mach-au1x00/au1000.h>
  32. #include <asm/mach-au1x00/au1xxx_psc.h>
  33. #include <asm/mach-au1x00/au1xxx_dbdma.h>
  34. #include <asm/mach-au1x00/au1550_spi.h>
  35. static unsigned usedma = 1;
  36. module_param(usedma, uint, 0644);
  37. /*
  38. #define AU1550_SPI_DEBUG_LOOPBACK
  39. */
  40. #define AU1550_SPI_DBDMA_DESCRIPTORS 1
  41. #define AU1550_SPI_DMA_RXTMP_MINSIZE 2048U
  42. struct au1550_spi {
  43. struct spi_bitbang bitbang;
  44. volatile psc_spi_t __iomem *regs;
  45. int irq;
  46. unsigned len;
  47. unsigned tx_count;
  48. unsigned rx_count;
  49. const u8 *tx;
  50. u8 *rx;
  51. void (*rx_word)(struct au1550_spi *hw);
  52. void (*tx_word)(struct au1550_spi *hw);
  53. int (*txrx_bufs)(struct spi_device *spi, struct spi_transfer *t);
  54. irqreturn_t (*irq_callback)(struct au1550_spi *hw);
  55. struct completion master_done;
  56. unsigned usedma;
  57. u32 dma_tx_id;
  58. u32 dma_rx_id;
  59. u32 dma_tx_ch;
  60. u32 dma_rx_ch;
  61. u8 *dma_rx_tmpbuf;
  62. unsigned dma_rx_tmpbuf_size;
  63. u32 dma_rx_tmpbuf_addr;
  64. struct spi_master *master;
  65. struct device *dev;
  66. struct au1550_spi_info *pdata;
  67. struct resource *ioarea;
  68. };
  69. /* we use an 8-bit memory device for dma transfers to/from spi fifo */
  70. static dbdev_tab_t au1550_spi_mem_dbdev =
  71. {
  72. .dev_id = DBDMA_MEM_CHAN,
  73. .dev_flags = DEV_FLAGS_ANYUSE|DEV_FLAGS_SYNC,
  74. .dev_tsize = 0,
  75. .dev_devwidth = 8,
  76. .dev_physaddr = 0x00000000,
  77. .dev_intlevel = 0,
  78. .dev_intpolarity = 0
  79. };
  80. static int ddma_memid; /* id to above mem dma device */
  81. static void au1550_spi_bits_handlers_set(struct au1550_spi *hw, int bpw);
  82. /*
  83. * compute BRG and DIV bits to setup spi clock based on main input clock rate
  84. * that was specified in platform data structure
  85. * according to au1550 datasheet:
  86. * psc_tempclk = psc_mainclk / (2 << DIV)
  87. * spiclk = psc_tempclk / (2 * (BRG + 1))
  88. * BRG valid range is 4..63
  89. * DIV valid range is 0..3
  90. */
  91. static u32 au1550_spi_baudcfg(struct au1550_spi *hw, unsigned speed_hz)
  92. {
  93. u32 mainclk_hz = hw->pdata->mainclk_hz;
  94. u32 div, brg;
  95. for (div = 0; div < 4; div++) {
  96. brg = mainclk_hz / speed_hz / (4 << div);
  97. /* now we have BRG+1 in brg, so count with that */
  98. if (brg < (4 + 1)) {
  99. brg = (4 + 1); /* speed_hz too big */
  100. break; /* set lowest brg (div is == 0) */
  101. }
  102. if (brg <= (63 + 1))
  103. break; /* we have valid brg and div */
  104. }
  105. if (div == 4) {
  106. div = 3; /* speed_hz too small */
  107. brg = (63 + 1); /* set highest brg and div */
  108. }
  109. brg--;
  110. return PSC_SPICFG_SET_BAUD(brg) | PSC_SPICFG_SET_DIV(div);
  111. }
  112. static inline void au1550_spi_mask_ack_all(struct au1550_spi *hw)
  113. {
  114. hw->regs->psc_spimsk =
  115. PSC_SPIMSK_MM | PSC_SPIMSK_RR | PSC_SPIMSK_RO
  116. | PSC_SPIMSK_RU | PSC_SPIMSK_TR | PSC_SPIMSK_TO
  117. | PSC_SPIMSK_TU | PSC_SPIMSK_SD | PSC_SPIMSK_MD;
  118. wmb(); /* drain writebuffer */
  119. hw->regs->psc_spievent =
  120. PSC_SPIEVNT_MM | PSC_SPIEVNT_RR | PSC_SPIEVNT_RO
  121. | PSC_SPIEVNT_RU | PSC_SPIEVNT_TR | PSC_SPIEVNT_TO
  122. | PSC_SPIEVNT_TU | PSC_SPIEVNT_SD | PSC_SPIEVNT_MD;
  123. wmb(); /* drain writebuffer */
  124. }
  125. static void au1550_spi_reset_fifos(struct au1550_spi *hw)
  126. {
  127. u32 pcr;
  128. hw->regs->psc_spipcr = PSC_SPIPCR_RC | PSC_SPIPCR_TC;
  129. wmb(); /* drain writebuffer */
  130. do {
  131. pcr = hw->regs->psc_spipcr;
  132. wmb(); /* drain writebuffer */
  133. } while (pcr != 0);
  134. }
  135. /*
  136. * dma transfers are used for the most common spi word size of 8-bits
  137. * we cannot easily change already set up dma channels' width, so if we wanted
  138. * dma support for more than 8-bit words (up to 24 bits), we would need to
  139. * setup dma channels from scratch on each spi transfer, based on bits_per_word
  140. * instead we have pre set up 8 bit dma channels supporting spi 4 to 8 bits
  141. * transfers, and 9 to 24 bits spi transfers will be done in pio irq based mode
  142. * callbacks to handle dma or pio are set up in au1550_spi_bits_handlers_set()
  143. */
  144. static void au1550_spi_chipsel(struct spi_device *spi, int value)
  145. {
  146. struct au1550_spi *hw = spi_master_get_devdata(spi->master);
  147. unsigned cspol = spi->mode & SPI_CS_HIGH ? 1 : 0;
  148. u32 cfg, stat;
  149. switch (value) {
  150. case BITBANG_CS_INACTIVE:
  151. if (hw->pdata->deactivate_cs)
  152. hw->pdata->deactivate_cs(hw->pdata, spi->chip_select,
  153. cspol);
  154. break;
  155. case BITBANG_CS_ACTIVE:
  156. au1550_spi_bits_handlers_set(hw, spi->bits_per_word);
  157. cfg = hw->regs->psc_spicfg;
  158. wmb(); /* drain writebuffer */
  159. hw->regs->psc_spicfg = cfg & ~PSC_SPICFG_DE_ENABLE;
  160. wmb(); /* drain writebuffer */
  161. if (spi->mode & SPI_CPOL)
  162. cfg |= PSC_SPICFG_BI;
  163. else
  164. cfg &= ~PSC_SPICFG_BI;
  165. if (spi->mode & SPI_CPHA)
  166. cfg &= ~PSC_SPICFG_CDE;
  167. else
  168. cfg |= PSC_SPICFG_CDE;
  169. if (spi->mode & SPI_LSB_FIRST)
  170. cfg |= PSC_SPICFG_MLF;
  171. else
  172. cfg &= ~PSC_SPICFG_MLF;
  173. if (hw->usedma && spi->bits_per_word <= 8)
  174. cfg &= ~PSC_SPICFG_DD_DISABLE;
  175. else
  176. cfg |= PSC_SPICFG_DD_DISABLE;
  177. cfg = PSC_SPICFG_CLR_LEN(cfg);
  178. cfg |= PSC_SPICFG_SET_LEN(spi->bits_per_word);
  179. cfg = PSC_SPICFG_CLR_BAUD(cfg);
  180. cfg &= ~PSC_SPICFG_SET_DIV(3);
  181. cfg |= au1550_spi_baudcfg(hw, spi->max_speed_hz);
  182. hw->regs->psc_spicfg = cfg | PSC_SPICFG_DE_ENABLE;
  183. wmb(); /* drain writebuffer */
  184. do {
  185. stat = hw->regs->psc_spistat;
  186. wmb(); /* drain writebuffer */
  187. } while ((stat & PSC_SPISTAT_DR) == 0);
  188. if (hw->pdata->activate_cs)
  189. hw->pdata->activate_cs(hw->pdata, spi->chip_select,
  190. cspol);
  191. break;
  192. }
  193. }
  194. static int au1550_spi_setupxfer(struct spi_device *spi, struct spi_transfer *t)
  195. {
  196. struct au1550_spi *hw = spi_master_get_devdata(spi->master);
  197. unsigned bpw, hz;
  198. u32 cfg, stat;
  199. if (t) {
  200. bpw = t->bits_per_word;
  201. hz = t->speed_hz;
  202. } else {
  203. bpw = spi->bits_per_word;
  204. hz = spi->max_speed_hz;
  205. }
  206. if (!hz)
  207. return -EINVAL;
  208. au1550_spi_bits_handlers_set(hw, spi->bits_per_word);
  209. cfg = hw->regs->psc_spicfg;
  210. wmb(); /* drain writebuffer */
  211. hw->regs->psc_spicfg = cfg & ~PSC_SPICFG_DE_ENABLE;
  212. wmb(); /* drain writebuffer */
  213. if (hw->usedma && bpw <= 8)
  214. cfg &= ~PSC_SPICFG_DD_DISABLE;
  215. else
  216. cfg |= PSC_SPICFG_DD_DISABLE;
  217. cfg = PSC_SPICFG_CLR_LEN(cfg);
  218. cfg |= PSC_SPICFG_SET_LEN(bpw);
  219. cfg = PSC_SPICFG_CLR_BAUD(cfg);
  220. cfg &= ~PSC_SPICFG_SET_DIV(3);
  221. cfg |= au1550_spi_baudcfg(hw, hz);
  222. hw->regs->psc_spicfg = cfg;
  223. wmb(); /* drain writebuffer */
  224. if (cfg & PSC_SPICFG_DE_ENABLE) {
  225. do {
  226. stat = hw->regs->psc_spistat;
  227. wmb(); /* drain writebuffer */
  228. } while ((stat & PSC_SPISTAT_DR) == 0);
  229. }
  230. au1550_spi_reset_fifos(hw);
  231. au1550_spi_mask_ack_all(hw);
  232. return 0;
  233. }
  234. /*
  235. * for dma spi transfers, we have to setup rx channel, otherwise there is
  236. * no reliable way how to recognize that spi transfer is done
  237. * dma complete callbacks are called before real spi transfer is finished
  238. * and if only tx dma channel is set up (and rx fifo overflow event masked)
  239. * spi master done event irq is not generated unless rx fifo is empty (emptied)
  240. * so we need rx tmp buffer to use for rx dma if user does not provide one
  241. */
  242. static int au1550_spi_dma_rxtmp_alloc(struct au1550_spi *hw, unsigned size)
  243. {
  244. hw->dma_rx_tmpbuf = kmalloc(size, GFP_KERNEL);
  245. if (!hw->dma_rx_tmpbuf)
  246. return -ENOMEM;
  247. hw->dma_rx_tmpbuf_size = size;
  248. hw->dma_rx_tmpbuf_addr = dma_map_single(hw->dev, hw->dma_rx_tmpbuf,
  249. size, DMA_FROM_DEVICE);
  250. if (dma_mapping_error(hw->dev, hw->dma_rx_tmpbuf_addr)) {
  251. kfree(hw->dma_rx_tmpbuf);
  252. hw->dma_rx_tmpbuf = 0;
  253. hw->dma_rx_tmpbuf_size = 0;
  254. return -EFAULT;
  255. }
  256. return 0;
  257. }
  258. static void au1550_spi_dma_rxtmp_free(struct au1550_spi *hw)
  259. {
  260. dma_unmap_single(hw->dev, hw->dma_rx_tmpbuf_addr,
  261. hw->dma_rx_tmpbuf_size, DMA_FROM_DEVICE);
  262. kfree(hw->dma_rx_tmpbuf);
  263. hw->dma_rx_tmpbuf = 0;
  264. hw->dma_rx_tmpbuf_size = 0;
  265. }
  266. static int au1550_spi_dma_txrxb(struct spi_device *spi, struct spi_transfer *t)
  267. {
  268. struct au1550_spi *hw = spi_master_get_devdata(spi->master);
  269. dma_addr_t dma_tx_addr;
  270. dma_addr_t dma_rx_addr;
  271. u32 res;
  272. hw->len = t->len;
  273. hw->tx_count = 0;
  274. hw->rx_count = 0;
  275. hw->tx = t->tx_buf;
  276. hw->rx = t->rx_buf;
  277. dma_tx_addr = t->tx_dma;
  278. dma_rx_addr = t->rx_dma;
  279. /*
  280. * check if buffers are already dma mapped, map them otherwise:
  281. * - first map the TX buffer, so cache data gets written to memory
  282. * - then map the RX buffer, so that cache entries (with
  283. * soon-to-be-stale data) get removed
  284. * use rx buffer in place of tx if tx buffer was not provided
  285. * use temp rx buffer (preallocated or realloc to fit) for rx dma
  286. */
  287. if (t->tx_buf) {
  288. if (t->tx_dma == 0) { /* if DMA_ADDR_INVALID, map it */
  289. dma_tx_addr = dma_map_single(hw->dev,
  290. (void *)t->tx_buf,
  291. t->len, DMA_TO_DEVICE);
  292. if (dma_mapping_error(hw->dev, dma_tx_addr))
  293. dev_err(hw->dev, "tx dma map error\n");
  294. }
  295. }
  296. if (t->rx_buf) {
  297. if (t->rx_dma == 0) { /* if DMA_ADDR_INVALID, map it */
  298. dma_rx_addr = dma_map_single(hw->dev,
  299. (void *)t->rx_buf,
  300. t->len, DMA_FROM_DEVICE);
  301. if (dma_mapping_error(hw->dev, dma_rx_addr))
  302. dev_err(hw->dev, "rx dma map error\n");
  303. }
  304. } else {
  305. if (t->len > hw->dma_rx_tmpbuf_size) {
  306. int ret;
  307. au1550_spi_dma_rxtmp_free(hw);
  308. ret = au1550_spi_dma_rxtmp_alloc(hw, max(t->len,
  309. AU1550_SPI_DMA_RXTMP_MINSIZE));
  310. if (ret < 0)
  311. return ret;
  312. }
  313. hw->rx = hw->dma_rx_tmpbuf;
  314. dma_rx_addr = hw->dma_rx_tmpbuf_addr;
  315. dma_sync_single_for_device(hw->dev, dma_rx_addr,
  316. t->len, DMA_FROM_DEVICE);
  317. }
  318. if (!t->tx_buf) {
  319. dma_sync_single_for_device(hw->dev, dma_rx_addr,
  320. t->len, DMA_BIDIRECTIONAL);
  321. hw->tx = hw->rx;
  322. }
  323. /* put buffers on the ring */
  324. res = au1xxx_dbdma_put_dest(hw->dma_rx_ch, virt_to_phys(hw->rx),
  325. t->len, DDMA_FLAGS_IE);
  326. if (!res)
  327. dev_err(hw->dev, "rx dma put dest error\n");
  328. res = au1xxx_dbdma_put_source(hw->dma_tx_ch, virt_to_phys(hw->tx),
  329. t->len, DDMA_FLAGS_IE);
  330. if (!res)
  331. dev_err(hw->dev, "tx dma put source error\n");
  332. au1xxx_dbdma_start(hw->dma_rx_ch);
  333. au1xxx_dbdma_start(hw->dma_tx_ch);
  334. /* by default enable nearly all events interrupt */
  335. hw->regs->psc_spimsk = PSC_SPIMSK_SD;
  336. wmb(); /* drain writebuffer */
  337. /* start the transfer */
  338. hw->regs->psc_spipcr = PSC_SPIPCR_MS;
  339. wmb(); /* drain writebuffer */
  340. wait_for_completion(&hw->master_done);
  341. au1xxx_dbdma_stop(hw->dma_tx_ch);
  342. au1xxx_dbdma_stop(hw->dma_rx_ch);
  343. if (!t->rx_buf) {
  344. /* using the temporal preallocated and premapped buffer */
  345. dma_sync_single_for_cpu(hw->dev, dma_rx_addr, t->len,
  346. DMA_FROM_DEVICE);
  347. }
  348. /* unmap buffers if mapped above */
  349. if (t->rx_buf && t->rx_dma == 0 )
  350. dma_unmap_single(hw->dev, dma_rx_addr, t->len,
  351. DMA_FROM_DEVICE);
  352. if (t->tx_buf && t->tx_dma == 0 )
  353. dma_unmap_single(hw->dev, dma_tx_addr, t->len,
  354. DMA_TO_DEVICE);
  355. return hw->rx_count < hw->tx_count ? hw->rx_count : hw->tx_count;
  356. }
  357. static irqreturn_t au1550_spi_dma_irq_callback(struct au1550_spi *hw)
  358. {
  359. u32 stat, evnt;
  360. stat = hw->regs->psc_spistat;
  361. evnt = hw->regs->psc_spievent;
  362. wmb(); /* drain writebuffer */
  363. if ((stat & PSC_SPISTAT_DI) == 0) {
  364. dev_err(hw->dev, "Unexpected IRQ!\n");
  365. return IRQ_NONE;
  366. }
  367. if ((evnt & (PSC_SPIEVNT_MM | PSC_SPIEVNT_RO
  368. | PSC_SPIEVNT_RU | PSC_SPIEVNT_TO
  369. | PSC_SPIEVNT_TU | PSC_SPIEVNT_SD))
  370. != 0) {
  371. /*
  372. * due to an spi error we consider transfer as done,
  373. * so mask all events until before next transfer start
  374. * and stop the possibly running dma immediately
  375. */
  376. au1550_spi_mask_ack_all(hw);
  377. au1xxx_dbdma_stop(hw->dma_rx_ch);
  378. au1xxx_dbdma_stop(hw->dma_tx_ch);
  379. /* get number of transferred bytes */
  380. hw->rx_count = hw->len - au1xxx_get_dma_residue(hw->dma_rx_ch);
  381. hw->tx_count = hw->len - au1xxx_get_dma_residue(hw->dma_tx_ch);
  382. au1xxx_dbdma_reset(hw->dma_rx_ch);
  383. au1xxx_dbdma_reset(hw->dma_tx_ch);
  384. au1550_spi_reset_fifos(hw);
  385. if (evnt == PSC_SPIEVNT_RO)
  386. dev_err(hw->dev,
  387. "dma transfer: receive FIFO overflow!\n");
  388. else
  389. dev_err(hw->dev,
  390. "dma transfer: unexpected SPI error "
  391. "(event=0x%x stat=0x%x)!\n", evnt, stat);
  392. complete(&hw->master_done);
  393. return IRQ_HANDLED;
  394. }
  395. if ((evnt & PSC_SPIEVNT_MD) != 0) {
  396. /* transfer completed successfully */
  397. au1550_spi_mask_ack_all(hw);
  398. hw->rx_count = hw->len;
  399. hw->tx_count = hw->len;
  400. complete(&hw->master_done);
  401. }
  402. return IRQ_HANDLED;
  403. }
  404. /* routines to handle different word sizes in pio mode */
  405. #define AU1550_SPI_RX_WORD(size, mask) \
  406. static void au1550_spi_rx_word_##size(struct au1550_spi *hw) \
  407. { \
  408. u32 fifoword = hw->regs->psc_spitxrx & (u32)(mask); \
  409. wmb(); /* drain writebuffer */ \
  410. if (hw->rx) { \
  411. *(u##size *)hw->rx = (u##size)fifoword; \
  412. hw->rx += (size) / 8; \
  413. } \
  414. hw->rx_count += (size) / 8; \
  415. }
  416. #define AU1550_SPI_TX_WORD(size, mask) \
  417. static void au1550_spi_tx_word_##size(struct au1550_spi *hw) \
  418. { \
  419. u32 fifoword = 0; \
  420. if (hw->tx) { \
  421. fifoword = *(u##size *)hw->tx & (u32)(mask); \
  422. hw->tx += (size) / 8; \
  423. } \
  424. hw->tx_count += (size) / 8; \
  425. if (hw->tx_count >= hw->len) \
  426. fifoword |= PSC_SPITXRX_LC; \
  427. hw->regs->psc_spitxrx = fifoword; \
  428. wmb(); /* drain writebuffer */ \
  429. }
  430. AU1550_SPI_RX_WORD(8,0xff)
  431. AU1550_SPI_RX_WORD(16,0xffff)
  432. AU1550_SPI_RX_WORD(32,0xffffff)
  433. AU1550_SPI_TX_WORD(8,0xff)
  434. AU1550_SPI_TX_WORD(16,0xffff)
  435. AU1550_SPI_TX_WORD(32,0xffffff)
  436. static int au1550_spi_pio_txrxb(struct spi_device *spi, struct spi_transfer *t)
  437. {
  438. u32 stat, mask;
  439. struct au1550_spi *hw = spi_master_get_devdata(spi->master);
  440. hw->tx = t->tx_buf;
  441. hw->rx = t->rx_buf;
  442. hw->len = t->len;
  443. hw->tx_count = 0;
  444. hw->rx_count = 0;
  445. /* by default enable nearly all events after filling tx fifo */
  446. mask = PSC_SPIMSK_SD;
  447. /* fill the transmit FIFO */
  448. while (hw->tx_count < hw->len) {
  449. hw->tx_word(hw);
  450. if (hw->tx_count >= hw->len) {
  451. /* mask tx fifo request interrupt as we are done */
  452. mask |= PSC_SPIMSK_TR;
  453. }
  454. stat = hw->regs->psc_spistat;
  455. wmb(); /* drain writebuffer */
  456. if (stat & PSC_SPISTAT_TF)
  457. break;
  458. }
  459. /* enable event interrupts */
  460. hw->regs->psc_spimsk = mask;
  461. wmb(); /* drain writebuffer */
  462. /* start the transfer */
  463. hw->regs->psc_spipcr = PSC_SPIPCR_MS;
  464. wmb(); /* drain writebuffer */
  465. wait_for_completion(&hw->master_done);
  466. return hw->rx_count < hw->tx_count ? hw->rx_count : hw->tx_count;
  467. }
  468. static irqreturn_t au1550_spi_pio_irq_callback(struct au1550_spi *hw)
  469. {
  470. int busy;
  471. u32 stat, evnt;
  472. stat = hw->regs->psc_spistat;
  473. evnt = hw->regs->psc_spievent;
  474. wmb(); /* drain writebuffer */
  475. if ((stat & PSC_SPISTAT_DI) == 0) {
  476. dev_err(hw->dev, "Unexpected IRQ!\n");
  477. return IRQ_NONE;
  478. }
  479. if ((evnt & (PSC_SPIEVNT_MM | PSC_SPIEVNT_RO
  480. | PSC_SPIEVNT_RU | PSC_SPIEVNT_TO
  481. | PSC_SPIEVNT_SD))
  482. != 0) {
  483. /*
  484. * due to an error we consider transfer as done,
  485. * so mask all events until before next transfer start
  486. */
  487. au1550_spi_mask_ack_all(hw);
  488. au1550_spi_reset_fifos(hw);
  489. dev_err(hw->dev,
  490. "pio transfer: unexpected SPI error "
  491. "(event=0x%x stat=0x%x)!\n", evnt, stat);
  492. complete(&hw->master_done);
  493. return IRQ_HANDLED;
  494. }
  495. /*
  496. * while there is something to read from rx fifo
  497. * or there is a space to write to tx fifo:
  498. */
  499. do {
  500. busy = 0;
  501. stat = hw->regs->psc_spistat;
  502. wmb(); /* drain writebuffer */
  503. /*
  504. * Take care to not let the Rx FIFO overflow.
  505. *
  506. * We only write a byte if we have read one at least. Initially,
  507. * the write fifo is full, so we should read from the read fifo
  508. * first.
  509. * In case we miss a word from the read fifo, we should get a
  510. * RO event and should back out.
  511. */
  512. if (!(stat & PSC_SPISTAT_RE) && hw->rx_count < hw->len) {
  513. hw->rx_word(hw);
  514. busy = 1;
  515. if (!(stat & PSC_SPISTAT_TF) && hw->tx_count < hw->len)
  516. hw->tx_word(hw);
  517. }
  518. } while (busy);
  519. hw->regs->psc_spievent = PSC_SPIEVNT_RR | PSC_SPIEVNT_TR;
  520. wmb(); /* drain writebuffer */
  521. /*
  522. * Restart the SPI transmission in case of a transmit underflow.
  523. * This seems to work despite the notes in the Au1550 data book
  524. * of Figure 8-4 with flowchart for SPI master operation:
  525. *
  526. * """Note 1: An XFR Error Interrupt occurs, unless masked,
  527. * for any of the following events: Tx FIFO Underflow,
  528. * Rx FIFO Overflow, or Multiple-master Error
  529. * Note 2: In case of a Tx Underflow Error, all zeroes are
  530. * transmitted."""
  531. *
  532. * By simply restarting the spi transfer on Tx Underflow Error,
  533. * we assume that spi transfer was paused instead of zeroes
  534. * transmittion mentioned in the Note 2 of Au1550 data book.
  535. */
  536. if (evnt & PSC_SPIEVNT_TU) {
  537. hw->regs->psc_spievent = PSC_SPIEVNT_TU | PSC_SPIEVNT_MD;
  538. wmb(); /* drain writebuffer */
  539. hw->regs->psc_spipcr = PSC_SPIPCR_MS;
  540. wmb(); /* drain writebuffer */
  541. }
  542. if (hw->rx_count >= hw->len) {
  543. /* transfer completed successfully */
  544. au1550_spi_mask_ack_all(hw);
  545. complete(&hw->master_done);
  546. }
  547. return IRQ_HANDLED;
  548. }
  549. static int au1550_spi_txrx_bufs(struct spi_device *spi, struct spi_transfer *t)
  550. {
  551. struct au1550_spi *hw = spi_master_get_devdata(spi->master);
  552. return hw->txrx_bufs(spi, t);
  553. }
  554. static irqreturn_t au1550_spi_irq(int irq, void *dev)
  555. {
  556. struct au1550_spi *hw = dev;
  557. return hw->irq_callback(hw);
  558. }
  559. static void au1550_spi_bits_handlers_set(struct au1550_spi *hw, int bpw)
  560. {
  561. if (bpw <= 8) {
  562. if (hw->usedma) {
  563. hw->txrx_bufs = &au1550_spi_dma_txrxb;
  564. hw->irq_callback = &au1550_spi_dma_irq_callback;
  565. } else {
  566. hw->rx_word = &au1550_spi_rx_word_8;
  567. hw->tx_word = &au1550_spi_tx_word_8;
  568. hw->txrx_bufs = &au1550_spi_pio_txrxb;
  569. hw->irq_callback = &au1550_spi_pio_irq_callback;
  570. }
  571. } else if (bpw <= 16) {
  572. hw->rx_word = &au1550_spi_rx_word_16;
  573. hw->tx_word = &au1550_spi_tx_word_16;
  574. hw->txrx_bufs = &au1550_spi_pio_txrxb;
  575. hw->irq_callback = &au1550_spi_pio_irq_callback;
  576. } else {
  577. hw->rx_word = &au1550_spi_rx_word_32;
  578. hw->tx_word = &au1550_spi_tx_word_32;
  579. hw->txrx_bufs = &au1550_spi_pio_txrxb;
  580. hw->irq_callback = &au1550_spi_pio_irq_callback;
  581. }
  582. }
  583. static void au1550_spi_setup_psc_as_spi(struct au1550_spi *hw)
  584. {
  585. u32 stat, cfg;
  586. /* set up the PSC for SPI mode */
  587. hw->regs->psc_ctrl = PSC_CTRL_DISABLE;
  588. wmb(); /* drain writebuffer */
  589. hw->regs->psc_sel = PSC_SEL_PS_SPIMODE;
  590. wmb(); /* drain writebuffer */
  591. hw->regs->psc_spicfg = 0;
  592. wmb(); /* drain writebuffer */
  593. hw->regs->psc_ctrl = PSC_CTRL_ENABLE;
  594. wmb(); /* drain writebuffer */
  595. do {
  596. stat = hw->regs->psc_spistat;
  597. wmb(); /* drain writebuffer */
  598. } while ((stat & PSC_SPISTAT_SR) == 0);
  599. cfg = hw->usedma ? 0 : PSC_SPICFG_DD_DISABLE;
  600. cfg |= PSC_SPICFG_SET_LEN(8);
  601. cfg |= PSC_SPICFG_RT_FIFO8 | PSC_SPICFG_TT_FIFO8;
  602. /* use minimal allowed brg and div values as initial setting: */
  603. cfg |= PSC_SPICFG_SET_BAUD(4) | PSC_SPICFG_SET_DIV(0);
  604. #ifdef AU1550_SPI_DEBUG_LOOPBACK
  605. cfg |= PSC_SPICFG_LB;
  606. #endif
  607. hw->regs->psc_spicfg = cfg;
  608. wmb(); /* drain writebuffer */
  609. au1550_spi_mask_ack_all(hw);
  610. hw->regs->psc_spicfg |= PSC_SPICFG_DE_ENABLE;
  611. wmb(); /* drain writebuffer */
  612. do {
  613. stat = hw->regs->psc_spistat;
  614. wmb(); /* drain writebuffer */
  615. } while ((stat & PSC_SPISTAT_DR) == 0);
  616. au1550_spi_reset_fifos(hw);
  617. }
  618. static int au1550_spi_probe(struct platform_device *pdev)
  619. {
  620. struct au1550_spi *hw;
  621. struct spi_master *master;
  622. struct resource *r;
  623. int err = 0;
  624. master = spi_alloc_master(&pdev->dev, sizeof(struct au1550_spi));
  625. if (master == NULL) {
  626. dev_err(&pdev->dev, "No memory for spi_master\n");
  627. err = -ENOMEM;
  628. goto err_nomem;
  629. }
  630. /* the spi->mode bits understood by this driver: */
  631. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LSB_FIRST;
  632. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 24);
  633. hw = spi_master_get_devdata(master);
  634. hw->master = master;
  635. hw->pdata = dev_get_platdata(&pdev->dev);
  636. hw->dev = &pdev->dev;
  637. if (hw->pdata == NULL) {
  638. dev_err(&pdev->dev, "No platform data supplied\n");
  639. err = -ENOENT;
  640. goto err_no_pdata;
  641. }
  642. r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  643. if (!r) {
  644. dev_err(&pdev->dev, "no IRQ\n");
  645. err = -ENODEV;
  646. goto err_no_iores;
  647. }
  648. hw->irq = r->start;
  649. hw->usedma = 0;
  650. r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  651. if (r) {
  652. hw->dma_tx_id = r->start;
  653. r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  654. if (r) {
  655. hw->dma_rx_id = r->start;
  656. if (usedma && ddma_memid) {
  657. if (pdev->dev.dma_mask == NULL)
  658. dev_warn(&pdev->dev, "no dma mask\n");
  659. else
  660. hw->usedma = 1;
  661. }
  662. }
  663. }
  664. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  665. if (!r) {
  666. dev_err(&pdev->dev, "no mmio resource\n");
  667. err = -ENODEV;
  668. goto err_no_iores;
  669. }
  670. hw->ioarea = request_mem_region(r->start, sizeof(psc_spi_t),
  671. pdev->name);
  672. if (!hw->ioarea) {
  673. dev_err(&pdev->dev, "Cannot reserve iomem region\n");
  674. err = -ENXIO;
  675. goto err_no_iores;
  676. }
  677. hw->regs = (psc_spi_t __iomem *)ioremap(r->start, sizeof(psc_spi_t));
  678. if (!hw->regs) {
  679. dev_err(&pdev->dev, "cannot ioremap\n");
  680. err = -ENXIO;
  681. goto err_ioremap;
  682. }
  683. platform_set_drvdata(pdev, hw);
  684. init_completion(&hw->master_done);
  685. hw->bitbang.master = hw->master;
  686. hw->bitbang.setup_transfer = au1550_spi_setupxfer;
  687. hw->bitbang.chipselect = au1550_spi_chipsel;
  688. hw->bitbang.txrx_bufs = au1550_spi_txrx_bufs;
  689. if (hw->usedma) {
  690. hw->dma_tx_ch = au1xxx_dbdma_chan_alloc(ddma_memid,
  691. hw->dma_tx_id, NULL, (void *)hw);
  692. if (hw->dma_tx_ch == 0) {
  693. dev_err(&pdev->dev,
  694. "Cannot allocate tx dma channel\n");
  695. err = -ENXIO;
  696. goto err_no_txdma;
  697. }
  698. au1xxx_dbdma_set_devwidth(hw->dma_tx_ch, 8);
  699. if (au1xxx_dbdma_ring_alloc(hw->dma_tx_ch,
  700. AU1550_SPI_DBDMA_DESCRIPTORS) == 0) {
  701. dev_err(&pdev->dev,
  702. "Cannot allocate tx dma descriptors\n");
  703. err = -ENXIO;
  704. goto err_no_txdma_descr;
  705. }
  706. hw->dma_rx_ch = au1xxx_dbdma_chan_alloc(hw->dma_rx_id,
  707. ddma_memid, NULL, (void *)hw);
  708. if (hw->dma_rx_ch == 0) {
  709. dev_err(&pdev->dev,
  710. "Cannot allocate rx dma channel\n");
  711. err = -ENXIO;
  712. goto err_no_rxdma;
  713. }
  714. au1xxx_dbdma_set_devwidth(hw->dma_rx_ch, 8);
  715. if (au1xxx_dbdma_ring_alloc(hw->dma_rx_ch,
  716. AU1550_SPI_DBDMA_DESCRIPTORS) == 0) {
  717. dev_err(&pdev->dev,
  718. "Cannot allocate rx dma descriptors\n");
  719. err = -ENXIO;
  720. goto err_no_rxdma_descr;
  721. }
  722. err = au1550_spi_dma_rxtmp_alloc(hw,
  723. AU1550_SPI_DMA_RXTMP_MINSIZE);
  724. if (err < 0) {
  725. dev_err(&pdev->dev,
  726. "Cannot allocate initial rx dma tmp buffer\n");
  727. goto err_dma_rxtmp_alloc;
  728. }
  729. }
  730. au1550_spi_bits_handlers_set(hw, 8);
  731. err = request_irq(hw->irq, au1550_spi_irq, 0, pdev->name, hw);
  732. if (err) {
  733. dev_err(&pdev->dev, "Cannot claim IRQ\n");
  734. goto err_no_irq;
  735. }
  736. master->bus_num = pdev->id;
  737. master->num_chipselect = hw->pdata->num_chipselect;
  738. /*
  739. * precompute valid range for spi freq - from au1550 datasheet:
  740. * psc_tempclk = psc_mainclk / (2 << DIV)
  741. * spiclk = psc_tempclk / (2 * (BRG + 1))
  742. * BRG valid range is 4..63
  743. * DIV valid range is 0..3
  744. * round the min and max frequencies to values that would still
  745. * produce valid brg and div
  746. */
  747. {
  748. int min_div = (2 << 0) * (2 * (4 + 1));
  749. int max_div = (2 << 3) * (2 * (63 + 1));
  750. master->max_speed_hz = hw->pdata->mainclk_hz / min_div;
  751. master->min_speed_hz =
  752. hw->pdata->mainclk_hz / (max_div + 1) + 1;
  753. }
  754. au1550_spi_setup_psc_as_spi(hw);
  755. err = spi_bitbang_start(&hw->bitbang);
  756. if (err) {
  757. dev_err(&pdev->dev, "Failed to register SPI master\n");
  758. goto err_register;
  759. }
  760. dev_info(&pdev->dev,
  761. "spi master registered: bus_num=%d num_chipselect=%d\n",
  762. master->bus_num, master->num_chipselect);
  763. return 0;
  764. err_register:
  765. free_irq(hw->irq, hw);
  766. err_no_irq:
  767. au1550_spi_dma_rxtmp_free(hw);
  768. err_dma_rxtmp_alloc:
  769. err_no_rxdma_descr:
  770. if (hw->usedma)
  771. au1xxx_dbdma_chan_free(hw->dma_rx_ch);
  772. err_no_rxdma:
  773. err_no_txdma_descr:
  774. if (hw->usedma)
  775. au1xxx_dbdma_chan_free(hw->dma_tx_ch);
  776. err_no_txdma:
  777. iounmap((void __iomem *)hw->regs);
  778. err_ioremap:
  779. release_mem_region(r->start, sizeof(psc_spi_t));
  780. err_no_iores:
  781. err_no_pdata:
  782. spi_master_put(hw->master);
  783. err_nomem:
  784. return err;
  785. }
  786. static int au1550_spi_remove(struct platform_device *pdev)
  787. {
  788. struct au1550_spi *hw = platform_get_drvdata(pdev);
  789. dev_info(&pdev->dev, "spi master remove: bus_num=%d\n",
  790. hw->master->bus_num);
  791. spi_bitbang_stop(&hw->bitbang);
  792. free_irq(hw->irq, hw);
  793. iounmap((void __iomem *)hw->regs);
  794. release_mem_region(hw->ioarea->start, sizeof(psc_spi_t));
  795. if (hw->usedma) {
  796. au1550_spi_dma_rxtmp_free(hw);
  797. au1xxx_dbdma_chan_free(hw->dma_rx_ch);
  798. au1xxx_dbdma_chan_free(hw->dma_tx_ch);
  799. }
  800. spi_master_put(hw->master);
  801. return 0;
  802. }
  803. /* work with hotplug and coldplug */
  804. MODULE_ALIAS("platform:au1550-spi");
  805. static struct platform_driver au1550_spi_drv = {
  806. .probe = au1550_spi_probe,
  807. .remove = au1550_spi_remove,
  808. .driver = {
  809. .name = "au1550-spi",
  810. },
  811. };
  812. static int __init au1550_spi_init(void)
  813. {
  814. /*
  815. * create memory device with 8 bits dev_devwidth
  816. * needed for proper byte ordering to spi fifo
  817. */
  818. switch (alchemy_get_cputype()) {
  819. case ALCHEMY_CPU_AU1550:
  820. case ALCHEMY_CPU_AU1200:
  821. case ALCHEMY_CPU_AU1300:
  822. break;
  823. default:
  824. return -ENODEV;
  825. }
  826. if (usedma) {
  827. ddma_memid = au1xxx_ddma_add_device(&au1550_spi_mem_dbdev);
  828. if (!ddma_memid)
  829. printk(KERN_ERR "au1550-spi: cannot add memory"
  830. "dbdma device\n");
  831. }
  832. return platform_driver_register(&au1550_spi_drv);
  833. }
  834. module_init(au1550_spi_init);
  835. static void __exit au1550_spi_exit(void)
  836. {
  837. if (usedma && ddma_memid)
  838. au1xxx_ddma_del_device(ddma_memid);
  839. platform_driver_unregister(&au1550_spi_drv);
  840. }
  841. module_exit(au1550_spi_exit);
  842. MODULE_DESCRIPTION("Au1550 PSC SPI Driver");
  843. MODULE_AUTHOR("Jan Nikitenko <jan.nikitenko@gmail.com>");
  844. MODULE_LICENSE("GPL");