au1550_spi.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  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->dev, 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(hw->dev, 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(hw->dev, 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. if (evnt == PSC_SPIEVNT_RO)
  416. dev_err(hw->dev,
  417. "dma transfer: receive FIFO overflow!\n");
  418. else
  419. dev_err(hw->dev,
  420. "dma transfer: unexpected SPI error "
  421. "(event=0x%x stat=0x%x)!\n", evnt, stat);
  422. complete(&hw->master_done);
  423. return IRQ_HANDLED;
  424. }
  425. if ((evnt & PSC_SPIEVNT_MD) != 0) {
  426. /* transfer completed successfully */
  427. au1550_spi_mask_ack_all(hw);
  428. hw->rx_count = hw->len;
  429. hw->tx_count = hw->len;
  430. complete(&hw->master_done);
  431. }
  432. return IRQ_HANDLED;
  433. }
  434. /* routines to handle different word sizes in pio mode */
  435. #define AU1550_SPI_RX_WORD(size, mask) \
  436. static void au1550_spi_rx_word_##size(struct au1550_spi *hw) \
  437. { \
  438. u32 fifoword = hw->regs->psc_spitxrx & (u32)(mask); \
  439. au_sync(); \
  440. if (hw->rx) { \
  441. *(u##size *)hw->rx = (u##size)fifoword; \
  442. hw->rx += (size) / 8; \
  443. } \
  444. hw->rx_count += (size) / 8; \
  445. }
  446. #define AU1550_SPI_TX_WORD(size, mask) \
  447. static void au1550_spi_tx_word_##size(struct au1550_spi *hw) \
  448. { \
  449. u32 fifoword = 0; \
  450. if (hw->tx) { \
  451. fifoword = *(u##size *)hw->tx & (u32)(mask); \
  452. hw->tx += (size) / 8; \
  453. } \
  454. hw->tx_count += (size) / 8; \
  455. if (hw->tx_count >= hw->len) \
  456. fifoword |= PSC_SPITXRX_LC; \
  457. hw->regs->psc_spitxrx = fifoword; \
  458. au_sync(); \
  459. }
  460. AU1550_SPI_RX_WORD(8,0xff)
  461. AU1550_SPI_RX_WORD(16,0xffff)
  462. AU1550_SPI_RX_WORD(32,0xffffff)
  463. AU1550_SPI_TX_WORD(8,0xff)
  464. AU1550_SPI_TX_WORD(16,0xffff)
  465. AU1550_SPI_TX_WORD(32,0xffffff)
  466. static int au1550_spi_pio_txrxb(struct spi_device *spi, struct spi_transfer *t)
  467. {
  468. u32 stat, mask;
  469. struct au1550_spi *hw = spi_master_get_devdata(spi->master);
  470. hw->tx = t->tx_buf;
  471. hw->rx = t->rx_buf;
  472. hw->len = t->len;
  473. hw->tx_count = 0;
  474. hw->rx_count = 0;
  475. /* by default enable nearly all events after filling tx fifo */
  476. mask = PSC_SPIMSK_SD;
  477. /* fill the transmit FIFO */
  478. while (hw->tx_count < hw->len) {
  479. hw->tx_word(hw);
  480. if (hw->tx_count >= hw->len) {
  481. /* mask tx fifo request interrupt as we are done */
  482. mask |= PSC_SPIMSK_TR;
  483. }
  484. stat = hw->regs->psc_spistat;
  485. au_sync();
  486. if (stat & PSC_SPISTAT_TF)
  487. break;
  488. }
  489. /* enable event interrupts */
  490. hw->regs->psc_spimsk = mask;
  491. au_sync();
  492. /* start the transfer */
  493. hw->regs->psc_spipcr = PSC_SPIPCR_MS;
  494. au_sync();
  495. wait_for_completion(&hw->master_done);
  496. return hw->rx_count < hw->tx_count ? hw->rx_count : hw->tx_count;
  497. }
  498. static irqreturn_t au1550_spi_pio_irq_callback(struct au1550_spi *hw)
  499. {
  500. int busy;
  501. u32 stat, evnt;
  502. stat = hw->regs->psc_spistat;
  503. evnt = hw->regs->psc_spievent;
  504. au_sync();
  505. if ((stat & PSC_SPISTAT_DI) == 0) {
  506. dev_err(hw->dev, "Unexpected IRQ!\n");
  507. return IRQ_NONE;
  508. }
  509. if ((evnt & (PSC_SPIEVNT_MM | PSC_SPIEVNT_RO
  510. | PSC_SPIEVNT_RU | PSC_SPIEVNT_TO
  511. | PSC_SPIEVNT_SD))
  512. != 0) {
  513. /*
  514. * due to an error we consider transfer as done,
  515. * so mask all events until before next transfer start
  516. */
  517. au1550_spi_mask_ack_all(hw);
  518. au1550_spi_reset_fifos(hw);
  519. dev_err(hw->dev,
  520. "pio transfer: unexpected SPI error "
  521. "(event=0x%x stat=0x%x)!\n", evnt, stat);
  522. complete(&hw->master_done);
  523. return IRQ_HANDLED;
  524. }
  525. /*
  526. * while there is something to read from rx fifo
  527. * or there is a space to write to tx fifo:
  528. */
  529. do {
  530. busy = 0;
  531. stat = hw->regs->psc_spistat;
  532. au_sync();
  533. /*
  534. * Take care to not let the Rx FIFO overflow.
  535. *
  536. * We only write a byte if we have read one at least. Initially,
  537. * the write fifo is full, so we should read from the read fifo
  538. * first.
  539. * In case we miss a word from the read fifo, we should get a
  540. * RO event and should back out.
  541. */
  542. if (!(stat & PSC_SPISTAT_RE) && hw->rx_count < hw->len) {
  543. hw->rx_word(hw);
  544. busy = 1;
  545. if (!(stat & PSC_SPISTAT_TF) && hw->tx_count < hw->len)
  546. hw->tx_word(hw);
  547. }
  548. } while (busy);
  549. hw->regs->psc_spievent = PSC_SPIEVNT_RR | PSC_SPIEVNT_TR;
  550. au_sync();
  551. /*
  552. * Restart the SPI transmission in case of a transmit underflow.
  553. * This seems to work despite the notes in the Au1550 data book
  554. * of Figure 8-4 with flowchart for SPI master operation:
  555. *
  556. * """Note 1: An XFR Error Interrupt occurs, unless masked,
  557. * for any of the following events: Tx FIFO Underflow,
  558. * Rx FIFO Overflow, or Multiple-master Error
  559. * Note 2: In case of a Tx Underflow Error, all zeroes are
  560. * transmitted."""
  561. *
  562. * By simply restarting the spi transfer on Tx Underflow Error,
  563. * we assume that spi transfer was paused instead of zeroes
  564. * transmittion mentioned in the Note 2 of Au1550 data book.
  565. */
  566. if (evnt & PSC_SPIEVNT_TU) {
  567. hw->regs->psc_spievent = PSC_SPIEVNT_TU | PSC_SPIEVNT_MD;
  568. au_sync();
  569. hw->regs->psc_spipcr = PSC_SPIPCR_MS;
  570. au_sync();
  571. }
  572. if (hw->rx_count >= hw->len) {
  573. /* transfer completed successfully */
  574. au1550_spi_mask_ack_all(hw);
  575. complete(&hw->master_done);
  576. }
  577. return IRQ_HANDLED;
  578. }
  579. static int au1550_spi_txrx_bufs(struct spi_device *spi, struct spi_transfer *t)
  580. {
  581. struct au1550_spi *hw = spi_master_get_devdata(spi->master);
  582. return hw->txrx_bufs(spi, t);
  583. }
  584. static irqreturn_t au1550_spi_irq(int irq, void *dev)
  585. {
  586. struct au1550_spi *hw = dev;
  587. return hw->irq_callback(hw);
  588. }
  589. static void au1550_spi_bits_handlers_set(struct au1550_spi *hw, int bpw)
  590. {
  591. if (bpw <= 8) {
  592. if (hw->usedma) {
  593. hw->txrx_bufs = &au1550_spi_dma_txrxb;
  594. hw->irq_callback = &au1550_spi_dma_irq_callback;
  595. } else {
  596. hw->rx_word = &au1550_spi_rx_word_8;
  597. hw->tx_word = &au1550_spi_tx_word_8;
  598. hw->txrx_bufs = &au1550_spi_pio_txrxb;
  599. hw->irq_callback = &au1550_spi_pio_irq_callback;
  600. }
  601. } else if (bpw <= 16) {
  602. hw->rx_word = &au1550_spi_rx_word_16;
  603. hw->tx_word = &au1550_spi_tx_word_16;
  604. hw->txrx_bufs = &au1550_spi_pio_txrxb;
  605. hw->irq_callback = &au1550_spi_pio_irq_callback;
  606. } else {
  607. hw->rx_word = &au1550_spi_rx_word_32;
  608. hw->tx_word = &au1550_spi_tx_word_32;
  609. hw->txrx_bufs = &au1550_spi_pio_txrxb;
  610. hw->irq_callback = &au1550_spi_pio_irq_callback;
  611. }
  612. }
  613. static void __init au1550_spi_setup_psc_as_spi(struct au1550_spi *hw)
  614. {
  615. u32 stat, cfg;
  616. /* set up the PSC for SPI mode */
  617. hw->regs->psc_ctrl = PSC_CTRL_DISABLE;
  618. au_sync();
  619. hw->regs->psc_sel = PSC_SEL_PS_SPIMODE;
  620. au_sync();
  621. hw->regs->psc_spicfg = 0;
  622. au_sync();
  623. hw->regs->psc_ctrl = PSC_CTRL_ENABLE;
  624. au_sync();
  625. do {
  626. stat = hw->regs->psc_spistat;
  627. au_sync();
  628. } while ((stat & PSC_SPISTAT_SR) == 0);
  629. cfg = hw->usedma ? 0 : PSC_SPICFG_DD_DISABLE;
  630. cfg |= PSC_SPICFG_SET_LEN(8);
  631. cfg |= PSC_SPICFG_RT_FIFO8 | PSC_SPICFG_TT_FIFO8;
  632. /* use minimal allowed brg and div values as initial setting: */
  633. cfg |= PSC_SPICFG_SET_BAUD(4) | PSC_SPICFG_SET_DIV(0);
  634. #ifdef AU1550_SPI_DEBUG_LOOPBACK
  635. cfg |= PSC_SPICFG_LB;
  636. #endif
  637. hw->regs->psc_spicfg = cfg;
  638. au_sync();
  639. au1550_spi_mask_ack_all(hw);
  640. hw->regs->psc_spicfg |= PSC_SPICFG_DE_ENABLE;
  641. au_sync();
  642. do {
  643. stat = hw->regs->psc_spistat;
  644. au_sync();
  645. } while ((stat & PSC_SPISTAT_DR) == 0);
  646. au1550_spi_reset_fifos(hw);
  647. }
  648. static int __init au1550_spi_probe(struct platform_device *pdev)
  649. {
  650. struct au1550_spi *hw;
  651. struct spi_master *master;
  652. struct resource *r;
  653. int err = 0;
  654. master = spi_alloc_master(&pdev->dev, sizeof(struct au1550_spi));
  655. if (master == NULL) {
  656. dev_err(&pdev->dev, "No memory for spi_master\n");
  657. err = -ENOMEM;
  658. goto err_nomem;
  659. }
  660. hw = spi_master_get_devdata(master);
  661. hw->master = spi_master_get(master);
  662. hw->pdata = pdev->dev.platform_data;
  663. hw->dev = &pdev->dev;
  664. if (hw->pdata == NULL) {
  665. dev_err(&pdev->dev, "No platform data supplied\n");
  666. err = -ENOENT;
  667. goto err_no_pdata;
  668. }
  669. r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  670. if (!r) {
  671. dev_err(&pdev->dev, "no IRQ\n");
  672. err = -ENODEV;
  673. goto err_no_iores;
  674. }
  675. hw->irq = r->start;
  676. hw->usedma = 0;
  677. r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  678. if (r) {
  679. hw->dma_tx_id = r->start;
  680. r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  681. if (r) {
  682. hw->dma_rx_id = r->start;
  683. if (usedma && ddma_memid) {
  684. if (pdev->dev.dma_mask == NULL)
  685. dev_warn(&pdev->dev, "no dma mask\n");
  686. else
  687. hw->usedma = 1;
  688. }
  689. }
  690. }
  691. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  692. if (!r) {
  693. dev_err(&pdev->dev, "no mmio resource\n");
  694. err = -ENODEV;
  695. goto err_no_iores;
  696. }
  697. hw->ioarea = request_mem_region(r->start, sizeof(psc_spi_t),
  698. pdev->name);
  699. if (!hw->ioarea) {
  700. dev_err(&pdev->dev, "Cannot reserve iomem region\n");
  701. err = -ENXIO;
  702. goto err_no_iores;
  703. }
  704. hw->regs = (psc_spi_t __iomem *)ioremap(r->start, sizeof(psc_spi_t));
  705. if (!hw->regs) {
  706. dev_err(&pdev->dev, "cannot ioremap\n");
  707. err = -ENXIO;
  708. goto err_ioremap;
  709. }
  710. platform_set_drvdata(pdev, hw);
  711. init_completion(&hw->master_done);
  712. hw->bitbang.master = hw->master;
  713. hw->bitbang.setup_transfer = au1550_spi_setupxfer;
  714. hw->bitbang.chipselect = au1550_spi_chipsel;
  715. hw->bitbang.master->setup = au1550_spi_setup;
  716. hw->bitbang.txrx_bufs = au1550_spi_txrx_bufs;
  717. if (hw->usedma) {
  718. hw->dma_tx_ch = au1xxx_dbdma_chan_alloc(ddma_memid,
  719. hw->dma_tx_id, NULL, (void *)hw);
  720. if (hw->dma_tx_ch == 0) {
  721. dev_err(&pdev->dev,
  722. "Cannot allocate tx dma channel\n");
  723. err = -ENXIO;
  724. goto err_no_txdma;
  725. }
  726. au1xxx_dbdma_set_devwidth(hw->dma_tx_ch, 8);
  727. if (au1xxx_dbdma_ring_alloc(hw->dma_tx_ch,
  728. AU1550_SPI_DBDMA_DESCRIPTORS) == 0) {
  729. dev_err(&pdev->dev,
  730. "Cannot allocate tx dma descriptors\n");
  731. err = -ENXIO;
  732. goto err_no_txdma_descr;
  733. }
  734. hw->dma_rx_ch = au1xxx_dbdma_chan_alloc(hw->dma_rx_id,
  735. ddma_memid, NULL, (void *)hw);
  736. if (hw->dma_rx_ch == 0) {
  737. dev_err(&pdev->dev,
  738. "Cannot allocate rx dma channel\n");
  739. err = -ENXIO;
  740. goto err_no_rxdma;
  741. }
  742. au1xxx_dbdma_set_devwidth(hw->dma_rx_ch, 8);
  743. if (au1xxx_dbdma_ring_alloc(hw->dma_rx_ch,
  744. AU1550_SPI_DBDMA_DESCRIPTORS) == 0) {
  745. dev_err(&pdev->dev,
  746. "Cannot allocate rx dma descriptors\n");
  747. err = -ENXIO;
  748. goto err_no_rxdma_descr;
  749. }
  750. err = au1550_spi_dma_rxtmp_alloc(hw,
  751. AU1550_SPI_DMA_RXTMP_MINSIZE);
  752. if (err < 0) {
  753. dev_err(&pdev->dev,
  754. "Cannot allocate initial rx dma tmp buffer\n");
  755. goto err_dma_rxtmp_alloc;
  756. }
  757. }
  758. au1550_spi_bits_handlers_set(hw, 8);
  759. err = request_irq(hw->irq, au1550_spi_irq, 0, pdev->name, hw);
  760. if (err) {
  761. dev_err(&pdev->dev, "Cannot claim IRQ\n");
  762. goto err_no_irq;
  763. }
  764. master->bus_num = pdev->id;
  765. master->num_chipselect = hw->pdata->num_chipselect;
  766. /*
  767. * precompute valid range for spi freq - from au1550 datasheet:
  768. * psc_tempclk = psc_mainclk / (2 << DIV)
  769. * spiclk = psc_tempclk / (2 * (BRG + 1))
  770. * BRG valid range is 4..63
  771. * DIV valid range is 0..3
  772. * round the min and max frequencies to values that would still
  773. * produce valid brg and div
  774. */
  775. {
  776. int min_div = (2 << 0) * (2 * (4 + 1));
  777. int max_div = (2 << 3) * (2 * (63 + 1));
  778. hw->freq_max = hw->pdata->mainclk_hz / min_div;
  779. hw->freq_min = hw->pdata->mainclk_hz / (max_div + 1) + 1;
  780. }
  781. au1550_spi_setup_psc_as_spi(hw);
  782. err = spi_bitbang_start(&hw->bitbang);
  783. if (err) {
  784. dev_err(&pdev->dev, "Failed to register SPI master\n");
  785. goto err_register;
  786. }
  787. dev_info(&pdev->dev,
  788. "spi master registered: bus_num=%d num_chipselect=%d\n",
  789. master->bus_num, master->num_chipselect);
  790. return 0;
  791. err_register:
  792. free_irq(hw->irq, hw);
  793. err_no_irq:
  794. au1550_spi_dma_rxtmp_free(hw);
  795. err_dma_rxtmp_alloc:
  796. err_no_rxdma_descr:
  797. if (hw->usedma)
  798. au1xxx_dbdma_chan_free(hw->dma_rx_ch);
  799. err_no_rxdma:
  800. err_no_txdma_descr:
  801. if (hw->usedma)
  802. au1xxx_dbdma_chan_free(hw->dma_tx_ch);
  803. err_no_txdma:
  804. iounmap((void __iomem *)hw->regs);
  805. err_ioremap:
  806. release_resource(hw->ioarea);
  807. kfree(hw->ioarea);
  808. err_no_iores:
  809. err_no_pdata:
  810. spi_master_put(hw->master);
  811. err_nomem:
  812. return err;
  813. }
  814. static int __exit au1550_spi_remove(struct platform_device *pdev)
  815. {
  816. struct au1550_spi *hw = platform_get_drvdata(pdev);
  817. dev_info(&pdev->dev, "spi master remove: bus_num=%d\n",
  818. hw->master->bus_num);
  819. spi_bitbang_stop(&hw->bitbang);
  820. free_irq(hw->irq, hw);
  821. iounmap((void __iomem *)hw->regs);
  822. release_resource(hw->ioarea);
  823. kfree(hw->ioarea);
  824. if (hw->usedma) {
  825. au1550_spi_dma_rxtmp_free(hw);
  826. au1xxx_dbdma_chan_free(hw->dma_rx_ch);
  827. au1xxx_dbdma_chan_free(hw->dma_tx_ch);
  828. }
  829. platform_set_drvdata(pdev, NULL);
  830. spi_master_put(hw->master);
  831. return 0;
  832. }
  833. /* work with hotplug and coldplug */
  834. MODULE_ALIAS("platform:au1550-spi");
  835. static struct platform_driver au1550_spi_drv = {
  836. .remove = __exit_p(au1550_spi_remove),
  837. .driver = {
  838. .name = "au1550-spi",
  839. .owner = THIS_MODULE,
  840. },
  841. };
  842. static int __init au1550_spi_init(void)
  843. {
  844. /*
  845. * create memory device with 8 bits dev_devwidth
  846. * needed for proper byte ordering to spi fifo
  847. */
  848. if (usedma) {
  849. ddma_memid = au1xxx_ddma_add_device(&au1550_spi_mem_dbdev);
  850. if (!ddma_memid)
  851. printk(KERN_ERR "au1550-spi: cannot add memory"
  852. "dbdma device\n");
  853. }
  854. return platform_driver_probe(&au1550_spi_drv, au1550_spi_probe);
  855. }
  856. module_init(au1550_spi_init);
  857. static void __exit au1550_spi_exit(void)
  858. {
  859. if (usedma && ddma_memid)
  860. au1xxx_ddma_del_device(ddma_memid);
  861. platform_driver_unregister(&au1550_spi_drv);
  862. }
  863. module_exit(au1550_spi_exit);
  864. MODULE_DESCRIPTION("Au1550 PSC SPI Driver");
  865. MODULE_AUTHOR("Jan Nikitenko <jan.nikitenko@gmail.com>");
  866. MODULE_LICENSE("GPL");