spi-au1550.c 26 KB

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