au1550_spi.c 25 KB

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