au1550_spi.c 24 KB

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