au1550_spi.c 25 KB

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