au1550_spi.c 26 KB

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