spi-au1550.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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. bpw = spi->bits_per_word;
  200. hz = spi->max_speed_hz;
  201. if (t) {
  202. if (t->bits_per_word)
  203. bpw = t->bits_per_word;
  204. if (t->speed_hz)
  205. hz = t->speed_hz;
  206. }
  207. if (!hz)
  208. return -EINVAL;
  209. au1550_spi_bits_handlers_set(hw, spi->bits_per_word);
  210. cfg = hw->regs->psc_spicfg;
  211. wmb(); /* drain writebuffer */
  212. hw->regs->psc_spicfg = cfg & ~PSC_SPICFG_DE_ENABLE;
  213. wmb(); /* drain writebuffer */
  214. if (hw->usedma && bpw <= 8)
  215. cfg &= ~PSC_SPICFG_DD_DISABLE;
  216. else
  217. cfg |= PSC_SPICFG_DD_DISABLE;
  218. cfg = PSC_SPICFG_CLR_LEN(cfg);
  219. cfg |= PSC_SPICFG_SET_LEN(bpw);
  220. cfg = PSC_SPICFG_CLR_BAUD(cfg);
  221. cfg &= ~PSC_SPICFG_SET_DIV(3);
  222. cfg |= au1550_spi_baudcfg(hw, hz);
  223. hw->regs->psc_spicfg = cfg;
  224. wmb(); /* drain writebuffer */
  225. if (cfg & PSC_SPICFG_DE_ENABLE) {
  226. do {
  227. stat = hw->regs->psc_spistat;
  228. wmb(); /* drain writebuffer */
  229. } while ((stat & PSC_SPISTAT_DR) == 0);
  230. }
  231. au1550_spi_reset_fifos(hw);
  232. au1550_spi_mask_ack_all(hw);
  233. return 0;
  234. }
  235. /*
  236. * for dma spi transfers, we have to setup rx channel, otherwise there is
  237. * no reliable way how to recognize that spi transfer is done
  238. * dma complete callbacks are called before real spi transfer is finished
  239. * and if only tx dma channel is set up (and rx fifo overflow event masked)
  240. * spi master done event irq is not generated unless rx fifo is empty (emptied)
  241. * so we need rx tmp buffer to use for rx dma if user does not provide one
  242. */
  243. static int au1550_spi_dma_rxtmp_alloc(struct au1550_spi *hw, unsigned size)
  244. {
  245. hw->dma_rx_tmpbuf = kmalloc(size, GFP_KERNEL);
  246. if (!hw->dma_rx_tmpbuf)
  247. return -ENOMEM;
  248. hw->dma_rx_tmpbuf_size = size;
  249. hw->dma_rx_tmpbuf_addr = dma_map_single(hw->dev, hw->dma_rx_tmpbuf,
  250. size, DMA_FROM_DEVICE);
  251. if (dma_mapping_error(hw->dev, hw->dma_rx_tmpbuf_addr)) {
  252. kfree(hw->dma_rx_tmpbuf);
  253. hw->dma_rx_tmpbuf = 0;
  254. hw->dma_rx_tmpbuf_size = 0;
  255. return -EFAULT;
  256. }
  257. return 0;
  258. }
  259. static void au1550_spi_dma_rxtmp_free(struct au1550_spi *hw)
  260. {
  261. dma_unmap_single(hw->dev, hw->dma_rx_tmpbuf_addr,
  262. hw->dma_rx_tmpbuf_size, DMA_FROM_DEVICE);
  263. kfree(hw->dma_rx_tmpbuf);
  264. hw->dma_rx_tmpbuf = 0;
  265. hw->dma_rx_tmpbuf_size = 0;
  266. }
  267. static int au1550_spi_dma_txrxb(struct spi_device *spi, struct spi_transfer *t)
  268. {
  269. struct au1550_spi *hw = spi_master_get_devdata(spi->master);
  270. dma_addr_t dma_tx_addr;
  271. dma_addr_t dma_rx_addr;
  272. u32 res;
  273. hw->len = t->len;
  274. hw->tx_count = 0;
  275. hw->rx_count = 0;
  276. hw->tx = t->tx_buf;
  277. hw->rx = t->rx_buf;
  278. dma_tx_addr = t->tx_dma;
  279. dma_rx_addr = t->rx_dma;
  280. /*
  281. * check if buffers are already dma mapped, map them otherwise:
  282. * - first map the TX buffer, so cache data gets written to memory
  283. * - then map the RX buffer, so that cache entries (with
  284. * soon-to-be-stale data) get removed
  285. * use rx buffer in place of tx if tx buffer was not provided
  286. * use temp rx buffer (preallocated or realloc to fit) for rx dma
  287. */
  288. if (t->tx_buf) {
  289. if (t->tx_dma == 0) { /* if DMA_ADDR_INVALID, map it */
  290. dma_tx_addr = dma_map_single(hw->dev,
  291. (void *)t->tx_buf,
  292. t->len, DMA_TO_DEVICE);
  293. if (dma_mapping_error(hw->dev, dma_tx_addr))
  294. dev_err(hw->dev, "tx dma map error\n");
  295. }
  296. }
  297. if (t->rx_buf) {
  298. if (t->rx_dma == 0) { /* if DMA_ADDR_INVALID, map it */
  299. dma_rx_addr = dma_map_single(hw->dev,
  300. (void *)t->rx_buf,
  301. t->len, DMA_FROM_DEVICE);
  302. if (dma_mapping_error(hw->dev, dma_rx_addr))
  303. dev_err(hw->dev, "rx dma map error\n");
  304. }
  305. } else {
  306. if (t->len > hw->dma_rx_tmpbuf_size) {
  307. int ret;
  308. au1550_spi_dma_rxtmp_free(hw);
  309. ret = au1550_spi_dma_rxtmp_alloc(hw, max(t->len,
  310. AU1550_SPI_DMA_RXTMP_MINSIZE));
  311. if (ret < 0)
  312. return ret;
  313. }
  314. hw->rx = hw->dma_rx_tmpbuf;
  315. dma_rx_addr = hw->dma_rx_tmpbuf_addr;
  316. dma_sync_single_for_device(hw->dev, dma_rx_addr,
  317. t->len, DMA_FROM_DEVICE);
  318. }
  319. if (!t->tx_buf) {
  320. dma_sync_single_for_device(hw->dev, dma_rx_addr,
  321. t->len, DMA_BIDIRECTIONAL);
  322. hw->tx = hw->rx;
  323. }
  324. /* put buffers on the ring */
  325. res = au1xxx_dbdma_put_dest(hw->dma_rx_ch, virt_to_phys(hw->rx),
  326. t->len, DDMA_FLAGS_IE);
  327. if (!res)
  328. dev_err(hw->dev, "rx dma put dest error\n");
  329. res = au1xxx_dbdma_put_source(hw->dma_tx_ch, virt_to_phys(hw->tx),
  330. t->len, DDMA_FLAGS_IE);
  331. if (!res)
  332. dev_err(hw->dev, "tx dma put source error\n");
  333. au1xxx_dbdma_start(hw->dma_rx_ch);
  334. au1xxx_dbdma_start(hw->dma_tx_ch);
  335. /* by default enable nearly all events interrupt */
  336. hw->regs->psc_spimsk = PSC_SPIMSK_SD;
  337. wmb(); /* drain writebuffer */
  338. /* start the transfer */
  339. hw->regs->psc_spipcr = PSC_SPIPCR_MS;
  340. wmb(); /* drain writebuffer */
  341. wait_for_completion(&hw->master_done);
  342. au1xxx_dbdma_stop(hw->dma_tx_ch);
  343. au1xxx_dbdma_stop(hw->dma_rx_ch);
  344. if (!t->rx_buf) {
  345. /* using the temporal preallocated and premapped buffer */
  346. dma_sync_single_for_cpu(hw->dev, dma_rx_addr, t->len,
  347. DMA_FROM_DEVICE);
  348. }
  349. /* unmap buffers if mapped above */
  350. if (t->rx_buf && t->rx_dma == 0 )
  351. dma_unmap_single(hw->dev, dma_rx_addr, t->len,
  352. DMA_FROM_DEVICE);
  353. if (t->tx_buf && t->tx_dma == 0 )
  354. dma_unmap_single(hw->dev, dma_tx_addr, t->len,
  355. DMA_TO_DEVICE);
  356. return hw->rx_count < hw->tx_count ? hw->rx_count : hw->tx_count;
  357. }
  358. static irqreturn_t au1550_spi_dma_irq_callback(struct au1550_spi *hw)
  359. {
  360. u32 stat, evnt;
  361. stat = hw->regs->psc_spistat;
  362. evnt = hw->regs->psc_spievent;
  363. wmb(); /* drain writebuffer */
  364. if ((stat & PSC_SPISTAT_DI) == 0) {
  365. dev_err(hw->dev, "Unexpected IRQ!\n");
  366. return IRQ_NONE;
  367. }
  368. if ((evnt & (PSC_SPIEVNT_MM | PSC_SPIEVNT_RO
  369. | PSC_SPIEVNT_RU | PSC_SPIEVNT_TO
  370. | PSC_SPIEVNT_TU | PSC_SPIEVNT_SD))
  371. != 0) {
  372. /*
  373. * due to an spi error we consider transfer as done,
  374. * so mask all events until before next transfer start
  375. * and stop the possibly running dma immediately
  376. */
  377. au1550_spi_mask_ack_all(hw);
  378. au1xxx_dbdma_stop(hw->dma_rx_ch);
  379. au1xxx_dbdma_stop(hw->dma_tx_ch);
  380. /* get number of transferred bytes */
  381. hw->rx_count = hw->len - au1xxx_get_dma_residue(hw->dma_rx_ch);
  382. hw->tx_count = hw->len - au1xxx_get_dma_residue(hw->dma_tx_ch);
  383. au1xxx_dbdma_reset(hw->dma_rx_ch);
  384. au1xxx_dbdma_reset(hw->dma_tx_ch);
  385. au1550_spi_reset_fifos(hw);
  386. if (evnt == PSC_SPIEVNT_RO)
  387. dev_err(hw->dev,
  388. "dma transfer: receive FIFO overflow!\n");
  389. else
  390. dev_err(hw->dev,
  391. "dma transfer: unexpected SPI error "
  392. "(event=0x%x stat=0x%x)!\n", evnt, stat);
  393. complete(&hw->master_done);
  394. return IRQ_HANDLED;
  395. }
  396. if ((evnt & PSC_SPIEVNT_MD) != 0) {
  397. /* transfer completed successfully */
  398. au1550_spi_mask_ack_all(hw);
  399. hw->rx_count = hw->len;
  400. hw->tx_count = hw->len;
  401. complete(&hw->master_done);
  402. }
  403. return IRQ_HANDLED;
  404. }
  405. /* routines to handle different word sizes in pio mode */
  406. #define AU1550_SPI_RX_WORD(size, mask) \
  407. static void au1550_spi_rx_word_##size(struct au1550_spi *hw) \
  408. { \
  409. u32 fifoword = hw->regs->psc_spitxrx & (u32)(mask); \
  410. wmb(); /* drain writebuffer */ \
  411. if (hw->rx) { \
  412. *(u##size *)hw->rx = (u##size)fifoword; \
  413. hw->rx += (size) / 8; \
  414. } \
  415. hw->rx_count += (size) / 8; \
  416. }
  417. #define AU1550_SPI_TX_WORD(size, mask) \
  418. static void au1550_spi_tx_word_##size(struct au1550_spi *hw) \
  419. { \
  420. u32 fifoword = 0; \
  421. if (hw->tx) { \
  422. fifoword = *(u##size *)hw->tx & (u32)(mask); \
  423. hw->tx += (size) / 8; \
  424. } \
  425. hw->tx_count += (size) / 8; \
  426. if (hw->tx_count >= hw->len) \
  427. fifoword |= PSC_SPITXRX_LC; \
  428. hw->regs->psc_spitxrx = fifoword; \
  429. wmb(); /* drain writebuffer */ \
  430. }
  431. AU1550_SPI_RX_WORD(8,0xff)
  432. AU1550_SPI_RX_WORD(16,0xffff)
  433. AU1550_SPI_RX_WORD(32,0xffffff)
  434. AU1550_SPI_TX_WORD(8,0xff)
  435. AU1550_SPI_TX_WORD(16,0xffff)
  436. AU1550_SPI_TX_WORD(32,0xffffff)
  437. static int au1550_spi_pio_txrxb(struct spi_device *spi, struct spi_transfer *t)
  438. {
  439. u32 stat, mask;
  440. struct au1550_spi *hw = spi_master_get_devdata(spi->master);
  441. hw->tx = t->tx_buf;
  442. hw->rx = t->rx_buf;
  443. hw->len = t->len;
  444. hw->tx_count = 0;
  445. hw->rx_count = 0;
  446. /* by default enable nearly all events after filling tx fifo */
  447. mask = PSC_SPIMSK_SD;
  448. /* fill the transmit FIFO */
  449. while (hw->tx_count < hw->len) {
  450. hw->tx_word(hw);
  451. if (hw->tx_count >= hw->len) {
  452. /* mask tx fifo request interrupt as we are done */
  453. mask |= PSC_SPIMSK_TR;
  454. }
  455. stat = hw->regs->psc_spistat;
  456. wmb(); /* drain writebuffer */
  457. if (stat & PSC_SPISTAT_TF)
  458. break;
  459. }
  460. /* enable event interrupts */
  461. hw->regs->psc_spimsk = mask;
  462. wmb(); /* drain writebuffer */
  463. /* start the transfer */
  464. hw->regs->psc_spipcr = PSC_SPIPCR_MS;
  465. wmb(); /* drain writebuffer */
  466. wait_for_completion(&hw->master_done);
  467. return hw->rx_count < hw->tx_count ? hw->rx_count : hw->tx_count;
  468. }
  469. static irqreturn_t au1550_spi_pio_irq_callback(struct au1550_spi *hw)
  470. {
  471. int busy;
  472. u32 stat, evnt;
  473. stat = hw->regs->psc_spistat;
  474. evnt = hw->regs->psc_spievent;
  475. wmb(); /* drain writebuffer */
  476. if ((stat & PSC_SPISTAT_DI) == 0) {
  477. dev_err(hw->dev, "Unexpected IRQ!\n");
  478. return IRQ_NONE;
  479. }
  480. if ((evnt & (PSC_SPIEVNT_MM | PSC_SPIEVNT_RO
  481. | PSC_SPIEVNT_RU | PSC_SPIEVNT_TO
  482. | PSC_SPIEVNT_SD))
  483. != 0) {
  484. /*
  485. * due to an error we consider transfer as done,
  486. * so mask all events until before next transfer start
  487. */
  488. au1550_spi_mask_ack_all(hw);
  489. au1550_spi_reset_fifos(hw);
  490. dev_err(hw->dev,
  491. "pio transfer: unexpected SPI error "
  492. "(event=0x%x stat=0x%x)!\n", evnt, stat);
  493. complete(&hw->master_done);
  494. return IRQ_HANDLED;
  495. }
  496. /*
  497. * while there is something to read from rx fifo
  498. * or there is a space to write to tx fifo:
  499. */
  500. do {
  501. busy = 0;
  502. stat = hw->regs->psc_spistat;
  503. wmb(); /* drain writebuffer */
  504. /*
  505. * Take care to not let the Rx FIFO overflow.
  506. *
  507. * We only write a byte if we have read one at least. Initially,
  508. * the write fifo is full, so we should read from the read fifo
  509. * first.
  510. * In case we miss a word from the read fifo, we should get a
  511. * RO event and should back out.
  512. */
  513. if (!(stat & PSC_SPISTAT_RE) && hw->rx_count < hw->len) {
  514. hw->rx_word(hw);
  515. busy = 1;
  516. if (!(stat & PSC_SPISTAT_TF) && hw->tx_count < hw->len)
  517. hw->tx_word(hw);
  518. }
  519. } while (busy);
  520. hw->regs->psc_spievent = PSC_SPIEVNT_RR | PSC_SPIEVNT_TR;
  521. wmb(); /* drain writebuffer */
  522. /*
  523. * Restart the SPI transmission in case of a transmit underflow.
  524. * This seems to work despite the notes in the Au1550 data book
  525. * of Figure 8-4 with flowchart for SPI master operation:
  526. *
  527. * """Note 1: An XFR Error Interrupt occurs, unless masked,
  528. * for any of the following events: Tx FIFO Underflow,
  529. * Rx FIFO Overflow, or Multiple-master Error
  530. * Note 2: In case of a Tx Underflow Error, all zeroes are
  531. * transmitted."""
  532. *
  533. * By simply restarting the spi transfer on Tx Underflow Error,
  534. * we assume that spi transfer was paused instead of zeroes
  535. * transmittion mentioned in the Note 2 of Au1550 data book.
  536. */
  537. if (evnt & PSC_SPIEVNT_TU) {
  538. hw->regs->psc_spievent = PSC_SPIEVNT_TU | PSC_SPIEVNT_MD;
  539. wmb(); /* drain writebuffer */
  540. hw->regs->psc_spipcr = PSC_SPIPCR_MS;
  541. wmb(); /* drain writebuffer */
  542. }
  543. if (hw->rx_count >= hw->len) {
  544. /* transfer completed successfully */
  545. au1550_spi_mask_ack_all(hw);
  546. complete(&hw->master_done);
  547. }
  548. return IRQ_HANDLED;
  549. }
  550. static int au1550_spi_txrx_bufs(struct spi_device *spi, struct spi_transfer *t)
  551. {
  552. struct au1550_spi *hw = spi_master_get_devdata(spi->master);
  553. return hw->txrx_bufs(spi, t);
  554. }
  555. static irqreturn_t au1550_spi_irq(int irq, void *dev)
  556. {
  557. struct au1550_spi *hw = dev;
  558. return hw->irq_callback(hw);
  559. }
  560. static void au1550_spi_bits_handlers_set(struct au1550_spi *hw, int bpw)
  561. {
  562. if (bpw <= 8) {
  563. if (hw->usedma) {
  564. hw->txrx_bufs = &au1550_spi_dma_txrxb;
  565. hw->irq_callback = &au1550_spi_dma_irq_callback;
  566. } else {
  567. hw->rx_word = &au1550_spi_rx_word_8;
  568. hw->tx_word = &au1550_spi_tx_word_8;
  569. hw->txrx_bufs = &au1550_spi_pio_txrxb;
  570. hw->irq_callback = &au1550_spi_pio_irq_callback;
  571. }
  572. } else if (bpw <= 16) {
  573. hw->rx_word = &au1550_spi_rx_word_16;
  574. hw->tx_word = &au1550_spi_tx_word_16;
  575. hw->txrx_bufs = &au1550_spi_pio_txrxb;
  576. hw->irq_callback = &au1550_spi_pio_irq_callback;
  577. } else {
  578. hw->rx_word = &au1550_spi_rx_word_32;
  579. hw->tx_word = &au1550_spi_tx_word_32;
  580. hw->txrx_bufs = &au1550_spi_pio_txrxb;
  581. hw->irq_callback = &au1550_spi_pio_irq_callback;
  582. }
  583. }
  584. static void au1550_spi_setup_psc_as_spi(struct au1550_spi *hw)
  585. {
  586. u32 stat, cfg;
  587. /* set up the PSC for SPI mode */
  588. hw->regs->psc_ctrl = PSC_CTRL_DISABLE;
  589. wmb(); /* drain writebuffer */
  590. hw->regs->psc_sel = PSC_SEL_PS_SPIMODE;
  591. wmb(); /* drain writebuffer */
  592. hw->regs->psc_spicfg = 0;
  593. wmb(); /* drain writebuffer */
  594. hw->regs->psc_ctrl = PSC_CTRL_ENABLE;
  595. wmb(); /* drain writebuffer */
  596. do {
  597. stat = hw->regs->psc_spistat;
  598. wmb(); /* drain writebuffer */
  599. } while ((stat & PSC_SPISTAT_SR) == 0);
  600. cfg = hw->usedma ? 0 : PSC_SPICFG_DD_DISABLE;
  601. cfg |= PSC_SPICFG_SET_LEN(8);
  602. cfg |= PSC_SPICFG_RT_FIFO8 | PSC_SPICFG_TT_FIFO8;
  603. /* use minimal allowed brg and div values as initial setting: */
  604. cfg |= PSC_SPICFG_SET_BAUD(4) | PSC_SPICFG_SET_DIV(0);
  605. #ifdef AU1550_SPI_DEBUG_LOOPBACK
  606. cfg |= PSC_SPICFG_LB;
  607. #endif
  608. hw->regs->psc_spicfg = cfg;
  609. wmb(); /* drain writebuffer */
  610. au1550_spi_mask_ack_all(hw);
  611. hw->regs->psc_spicfg |= PSC_SPICFG_DE_ENABLE;
  612. wmb(); /* drain writebuffer */
  613. do {
  614. stat = hw->regs->psc_spistat;
  615. wmb(); /* drain writebuffer */
  616. } while ((stat & PSC_SPISTAT_DR) == 0);
  617. au1550_spi_reset_fifos(hw);
  618. }
  619. static int au1550_spi_probe(struct platform_device *pdev)
  620. {
  621. struct au1550_spi *hw;
  622. struct spi_master *master;
  623. struct resource *r;
  624. int err = 0;
  625. master = spi_alloc_master(&pdev->dev, sizeof(struct au1550_spi));
  626. if (master == NULL) {
  627. dev_err(&pdev->dev, "No memory for spi_master\n");
  628. err = -ENOMEM;
  629. goto err_nomem;
  630. }
  631. /* the spi->mode bits understood by this driver: */
  632. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LSB_FIRST;
  633. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 24);
  634. hw = spi_master_get_devdata(master);
  635. hw->master = master;
  636. hw->pdata = dev_get_platdata(&pdev->dev);
  637. hw->dev = &pdev->dev;
  638. if (hw->pdata == NULL) {
  639. dev_err(&pdev->dev, "No platform data supplied\n");
  640. err = -ENOENT;
  641. goto err_no_pdata;
  642. }
  643. r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  644. if (!r) {
  645. dev_err(&pdev->dev, "no IRQ\n");
  646. err = -ENODEV;
  647. goto err_no_iores;
  648. }
  649. hw->irq = r->start;
  650. hw->usedma = 0;
  651. r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  652. if (r) {
  653. hw->dma_tx_id = r->start;
  654. r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  655. if (r) {
  656. hw->dma_rx_id = r->start;
  657. if (usedma && ddma_memid) {
  658. if (pdev->dev.dma_mask == NULL)
  659. dev_warn(&pdev->dev, "no dma mask\n");
  660. else
  661. hw->usedma = 1;
  662. }
  663. }
  664. }
  665. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  666. if (!r) {
  667. dev_err(&pdev->dev, "no mmio resource\n");
  668. err = -ENODEV;
  669. goto err_no_iores;
  670. }
  671. hw->ioarea = request_mem_region(r->start, sizeof(psc_spi_t),
  672. pdev->name);
  673. if (!hw->ioarea) {
  674. dev_err(&pdev->dev, "Cannot reserve iomem region\n");
  675. err = -ENXIO;
  676. goto err_no_iores;
  677. }
  678. hw->regs = (psc_spi_t __iomem *)ioremap(r->start, sizeof(psc_spi_t));
  679. if (!hw->regs) {
  680. dev_err(&pdev->dev, "cannot ioremap\n");
  681. err = -ENXIO;
  682. goto err_ioremap;
  683. }
  684. platform_set_drvdata(pdev, hw);
  685. init_completion(&hw->master_done);
  686. hw->bitbang.master = hw->master;
  687. hw->bitbang.setup_transfer = au1550_spi_setupxfer;
  688. hw->bitbang.chipselect = au1550_spi_chipsel;
  689. hw->bitbang.txrx_bufs = au1550_spi_txrx_bufs;
  690. if (hw->usedma) {
  691. hw->dma_tx_ch = au1xxx_dbdma_chan_alloc(ddma_memid,
  692. hw->dma_tx_id, NULL, (void *)hw);
  693. if (hw->dma_tx_ch == 0) {
  694. dev_err(&pdev->dev,
  695. "Cannot allocate tx dma channel\n");
  696. err = -ENXIO;
  697. goto err_no_txdma;
  698. }
  699. au1xxx_dbdma_set_devwidth(hw->dma_tx_ch, 8);
  700. if (au1xxx_dbdma_ring_alloc(hw->dma_tx_ch,
  701. AU1550_SPI_DBDMA_DESCRIPTORS) == 0) {
  702. dev_err(&pdev->dev,
  703. "Cannot allocate tx dma descriptors\n");
  704. err = -ENXIO;
  705. goto err_no_txdma_descr;
  706. }
  707. hw->dma_rx_ch = au1xxx_dbdma_chan_alloc(hw->dma_rx_id,
  708. ddma_memid, NULL, (void *)hw);
  709. if (hw->dma_rx_ch == 0) {
  710. dev_err(&pdev->dev,
  711. "Cannot allocate rx dma channel\n");
  712. err = -ENXIO;
  713. goto err_no_rxdma;
  714. }
  715. au1xxx_dbdma_set_devwidth(hw->dma_rx_ch, 8);
  716. if (au1xxx_dbdma_ring_alloc(hw->dma_rx_ch,
  717. AU1550_SPI_DBDMA_DESCRIPTORS) == 0) {
  718. dev_err(&pdev->dev,
  719. "Cannot allocate rx dma descriptors\n");
  720. err = -ENXIO;
  721. goto err_no_rxdma_descr;
  722. }
  723. err = au1550_spi_dma_rxtmp_alloc(hw,
  724. AU1550_SPI_DMA_RXTMP_MINSIZE);
  725. if (err < 0) {
  726. dev_err(&pdev->dev,
  727. "Cannot allocate initial rx dma tmp buffer\n");
  728. goto err_dma_rxtmp_alloc;
  729. }
  730. }
  731. au1550_spi_bits_handlers_set(hw, 8);
  732. err = request_irq(hw->irq, au1550_spi_irq, 0, pdev->name, hw);
  733. if (err) {
  734. dev_err(&pdev->dev, "Cannot claim IRQ\n");
  735. goto err_no_irq;
  736. }
  737. master->bus_num = pdev->id;
  738. master->num_chipselect = hw->pdata->num_chipselect;
  739. /*
  740. * precompute valid range for spi freq - from au1550 datasheet:
  741. * psc_tempclk = psc_mainclk / (2 << DIV)
  742. * spiclk = psc_tempclk / (2 * (BRG + 1))
  743. * BRG valid range is 4..63
  744. * DIV valid range is 0..3
  745. * round the min and max frequencies to values that would still
  746. * produce valid brg and div
  747. */
  748. {
  749. int min_div = (2 << 0) * (2 * (4 + 1));
  750. int max_div = (2 << 3) * (2 * (63 + 1));
  751. master->max_speed_hz = hw->pdata->mainclk_hz / min_div;
  752. master->min_speed_hz =
  753. hw->pdata->mainclk_hz / (max_div + 1) + 1;
  754. }
  755. au1550_spi_setup_psc_as_spi(hw);
  756. err = spi_bitbang_start(&hw->bitbang);
  757. if (err) {
  758. dev_err(&pdev->dev, "Failed to register SPI master\n");
  759. goto err_register;
  760. }
  761. dev_info(&pdev->dev,
  762. "spi master registered: bus_num=%d num_chipselect=%d\n",
  763. master->bus_num, master->num_chipselect);
  764. return 0;
  765. err_register:
  766. free_irq(hw->irq, hw);
  767. err_no_irq:
  768. au1550_spi_dma_rxtmp_free(hw);
  769. err_dma_rxtmp_alloc:
  770. err_no_rxdma_descr:
  771. if (hw->usedma)
  772. au1xxx_dbdma_chan_free(hw->dma_rx_ch);
  773. err_no_rxdma:
  774. err_no_txdma_descr:
  775. if (hw->usedma)
  776. au1xxx_dbdma_chan_free(hw->dma_tx_ch);
  777. err_no_txdma:
  778. iounmap((void __iomem *)hw->regs);
  779. err_ioremap:
  780. release_mem_region(r->start, sizeof(psc_spi_t));
  781. err_no_iores:
  782. err_no_pdata:
  783. spi_master_put(hw->master);
  784. err_nomem:
  785. return err;
  786. }
  787. static int au1550_spi_remove(struct platform_device *pdev)
  788. {
  789. struct au1550_spi *hw = platform_get_drvdata(pdev);
  790. dev_info(&pdev->dev, "spi master remove: bus_num=%d\n",
  791. hw->master->bus_num);
  792. spi_bitbang_stop(&hw->bitbang);
  793. free_irq(hw->irq, hw);
  794. iounmap((void __iomem *)hw->regs);
  795. release_mem_region(hw->ioarea->start, sizeof(psc_spi_t));
  796. if (hw->usedma) {
  797. au1550_spi_dma_rxtmp_free(hw);
  798. au1xxx_dbdma_chan_free(hw->dma_rx_ch);
  799. au1xxx_dbdma_chan_free(hw->dma_tx_ch);
  800. }
  801. spi_master_put(hw->master);
  802. return 0;
  803. }
  804. /* work with hotplug and coldplug */
  805. MODULE_ALIAS("platform:au1550-spi");
  806. static struct platform_driver au1550_spi_drv = {
  807. .probe = au1550_spi_probe,
  808. .remove = au1550_spi_remove,
  809. .driver = {
  810. .name = "au1550-spi",
  811. },
  812. };
  813. static int __init au1550_spi_init(void)
  814. {
  815. /*
  816. * create memory device with 8 bits dev_devwidth
  817. * needed for proper byte ordering to spi fifo
  818. */
  819. switch (alchemy_get_cputype()) {
  820. case ALCHEMY_CPU_AU1550:
  821. case ALCHEMY_CPU_AU1200:
  822. case ALCHEMY_CPU_AU1300:
  823. break;
  824. default:
  825. return -ENODEV;
  826. }
  827. if (usedma) {
  828. ddma_memid = au1xxx_ddma_add_device(&au1550_spi_mem_dbdev);
  829. if (!ddma_memid)
  830. printk(KERN_ERR "au1550-spi: cannot add memory"
  831. "dbdma device\n");
  832. }
  833. return platform_driver_register(&au1550_spi_drv);
  834. }
  835. module_init(au1550_spi_init);
  836. static void __exit au1550_spi_exit(void)
  837. {
  838. if (usedma && ddma_memid)
  839. au1xxx_ddma_del_device(ddma_memid);
  840. platform_driver_unregister(&au1550_spi_drv);
  841. }
  842. module_exit(au1550_spi_exit);
  843. MODULE_DESCRIPTION("Au1550 PSC SPI Driver");
  844. MODULE_AUTHOR("Jan Nikitenko <jan.nikitenko@gmail.com>");
  845. MODULE_LICENSE("GPL");