au1550_spi.c 26 KB

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