spi-au1550.c 25 KB

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