au1550_spi.c 26 KB

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