au1550_spi.c 26 KB

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