spi-au1550.c 26 KB

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