davinci_spi.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  1. /*
  2. * Copyright (C) 2009 Texas Instruments.
  3. * Copyright (C) 2010 EF Johnson Technologies
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/interrupt.h>
  20. #include <linux/io.h>
  21. #include <linux/gpio.h>
  22. #include <linux/module.h>
  23. #include <linux/delay.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/err.h>
  26. #include <linux/clk.h>
  27. #include <linux/dma-mapping.h>
  28. #include <linux/spi/spi.h>
  29. #include <linux/spi/spi_bitbang.h>
  30. #include <linux/slab.h>
  31. #include <mach/spi.h>
  32. #include <mach/edma.h>
  33. #define SPI_NO_RESOURCE ((resource_size_t)-1)
  34. #define SPI_MAX_CHIPSELECT 2
  35. #define CS_DEFAULT 0xFF
  36. #define SPIFMT_PHASE_MASK BIT(16)
  37. #define SPIFMT_POLARITY_MASK BIT(17)
  38. #define SPIFMT_DISTIMER_MASK BIT(18)
  39. #define SPIFMT_SHIFTDIR_MASK BIT(20)
  40. #define SPIFMT_WAITENA_MASK BIT(21)
  41. #define SPIFMT_PARITYENA_MASK BIT(22)
  42. #define SPIFMT_ODD_PARITY_MASK BIT(23)
  43. #define SPIFMT_WDELAY_MASK 0x3f000000u
  44. #define SPIFMT_WDELAY_SHIFT 24
  45. #define SPIFMT_PRESCALE_SHIFT 8
  46. /* SPIPC0 */
  47. #define SPIPC0_DIFUN_MASK BIT(11) /* MISO */
  48. #define SPIPC0_DOFUN_MASK BIT(10) /* MOSI */
  49. #define SPIPC0_CLKFUN_MASK BIT(9) /* CLK */
  50. #define SPIPC0_SPIENA_MASK BIT(8) /* nREADY */
  51. #define SPIINT_MASKALL 0x0101035F
  52. #define SPIINT_MASKINT 0x0000015F
  53. #define SPI_INTLVL_1 0x000001FF
  54. #define SPI_INTLVL_0 0x00000000
  55. /* SPIDAT1 (upper 16 bit defines) */
  56. #define SPIDAT1_CSHOLD_MASK BIT(12)
  57. /* SPIGCR1 */
  58. #define SPIGCR1_CLKMOD_MASK BIT(1)
  59. #define SPIGCR1_MASTER_MASK BIT(0)
  60. #define SPIGCR1_POWERDOWN_MASK BIT(8)
  61. #define SPIGCR1_LOOPBACK_MASK BIT(16)
  62. #define SPIGCR1_SPIENA_MASK BIT(24)
  63. /* SPIBUF */
  64. #define SPIBUF_TXFULL_MASK BIT(29)
  65. #define SPIBUF_RXEMPTY_MASK BIT(31)
  66. /* SPIDELAY */
  67. #define SPIDELAY_C2TDELAY_SHIFT 24
  68. #define SPIDELAY_C2TDELAY_MASK (0xFF << SPIDELAY_C2TDELAY_SHIFT)
  69. #define SPIDELAY_T2CDELAY_SHIFT 16
  70. #define SPIDELAY_T2CDELAY_MASK (0xFF << SPIDELAY_T2CDELAY_SHIFT)
  71. #define SPIDELAY_T2EDELAY_SHIFT 8
  72. #define SPIDELAY_T2EDELAY_MASK (0xFF << SPIDELAY_T2EDELAY_SHIFT)
  73. #define SPIDELAY_C2EDELAY_SHIFT 0
  74. #define SPIDELAY_C2EDELAY_MASK 0xFF
  75. /* Error Masks */
  76. #define SPIFLG_DLEN_ERR_MASK BIT(0)
  77. #define SPIFLG_TIMEOUT_MASK BIT(1)
  78. #define SPIFLG_PARERR_MASK BIT(2)
  79. #define SPIFLG_DESYNC_MASK BIT(3)
  80. #define SPIFLG_BITERR_MASK BIT(4)
  81. #define SPIFLG_OVRRUN_MASK BIT(6)
  82. #define SPIFLG_BUF_INIT_ACTIVE_MASK BIT(24)
  83. #define SPIFLG_ERROR_MASK (SPIFLG_DLEN_ERR_MASK \
  84. | SPIFLG_TIMEOUT_MASK | SPIFLG_PARERR_MASK \
  85. | SPIFLG_DESYNC_MASK | SPIFLG_BITERR_MASK \
  86. | SPIFLG_OVRRUN_MASK)
  87. #define SPIINT_DMA_REQ_EN BIT(16)
  88. /* SPI Controller registers */
  89. #define SPIGCR0 0x00
  90. #define SPIGCR1 0x04
  91. #define SPIINT 0x08
  92. #define SPILVL 0x0c
  93. #define SPIFLG 0x10
  94. #define SPIPC0 0x14
  95. #define SPIDAT1 0x3c
  96. #define SPIBUF 0x40
  97. #define SPIDELAY 0x48
  98. #define SPIDEF 0x4c
  99. #define SPIFMT0 0x50
  100. /* We have 2 DMA channels per CS, one for RX and one for TX */
  101. struct davinci_spi_dma {
  102. int tx_channel;
  103. int rx_channel;
  104. int dummy_param_slot;
  105. enum dma_event_q eventq;
  106. };
  107. /* SPI Controller driver's private data. */
  108. struct davinci_spi {
  109. struct spi_bitbang bitbang;
  110. struct clk *clk;
  111. u8 version;
  112. resource_size_t pbase;
  113. void __iomem *base;
  114. u32 irq;
  115. struct completion done;
  116. const void *tx;
  117. void *rx;
  118. #define SPI_TMP_BUFSZ (SMP_CACHE_BYTES + 1)
  119. u8 rx_tmp_buf[SPI_TMP_BUFSZ];
  120. int rcount;
  121. int wcount;
  122. struct davinci_spi_dma dma;
  123. struct davinci_spi_platform_data *pdata;
  124. void (*get_rx)(u32 rx_data, struct davinci_spi *);
  125. u32 (*get_tx)(struct davinci_spi *);
  126. u8 bytes_per_word[SPI_MAX_CHIPSELECT];
  127. };
  128. static struct davinci_spi_config davinci_spi_default_cfg;
  129. static void davinci_spi_rx_buf_u8(u32 data, struct davinci_spi *dspi)
  130. {
  131. if (dspi->rx) {
  132. u8 *rx = dspi->rx;
  133. *rx++ = (u8)data;
  134. dspi->rx = rx;
  135. }
  136. }
  137. static void davinci_spi_rx_buf_u16(u32 data, struct davinci_spi *dspi)
  138. {
  139. if (dspi->rx) {
  140. u16 *rx = dspi->rx;
  141. *rx++ = (u16)data;
  142. dspi->rx = rx;
  143. }
  144. }
  145. static u32 davinci_spi_tx_buf_u8(struct davinci_spi *dspi)
  146. {
  147. u32 data = 0;
  148. if (dspi->tx) {
  149. const u8 *tx = dspi->tx;
  150. data = *tx++;
  151. dspi->tx = tx;
  152. }
  153. return data;
  154. }
  155. static u32 davinci_spi_tx_buf_u16(struct davinci_spi *dspi)
  156. {
  157. u32 data = 0;
  158. if (dspi->tx) {
  159. const u16 *tx = dspi->tx;
  160. data = *tx++;
  161. dspi->tx = tx;
  162. }
  163. return data;
  164. }
  165. static inline void set_io_bits(void __iomem *addr, u32 bits)
  166. {
  167. u32 v = ioread32(addr);
  168. v |= bits;
  169. iowrite32(v, addr);
  170. }
  171. static inline void clear_io_bits(void __iomem *addr, u32 bits)
  172. {
  173. u32 v = ioread32(addr);
  174. v &= ~bits;
  175. iowrite32(v, addr);
  176. }
  177. /*
  178. * Interface to control the chip select signal
  179. */
  180. static void davinci_spi_chipselect(struct spi_device *spi, int value)
  181. {
  182. struct davinci_spi *dspi;
  183. struct davinci_spi_platform_data *pdata;
  184. u8 chip_sel = spi->chip_select;
  185. u16 spidat1 = CS_DEFAULT;
  186. bool gpio_chipsel = false;
  187. dspi = spi_master_get_devdata(spi->master);
  188. pdata = dspi->pdata;
  189. if (pdata->chip_sel && chip_sel < pdata->num_chipselect &&
  190. pdata->chip_sel[chip_sel] != SPI_INTERN_CS)
  191. gpio_chipsel = true;
  192. /*
  193. * Board specific chip select logic decides the polarity and cs
  194. * line for the controller
  195. */
  196. if (gpio_chipsel) {
  197. if (value == BITBANG_CS_ACTIVE)
  198. gpio_set_value(pdata->chip_sel[chip_sel], 0);
  199. else
  200. gpio_set_value(pdata->chip_sel[chip_sel], 1);
  201. } else {
  202. if (value == BITBANG_CS_ACTIVE) {
  203. spidat1 |= SPIDAT1_CSHOLD_MASK;
  204. spidat1 &= ~(0x1 << chip_sel);
  205. }
  206. iowrite16(spidat1, dspi->base + SPIDAT1 + 2);
  207. }
  208. }
  209. /**
  210. * davinci_spi_get_prescale - Calculates the correct prescale value
  211. * @maxspeed_hz: the maximum rate the SPI clock can run at
  212. *
  213. * This function calculates the prescale value that generates a clock rate
  214. * less than or equal to the specified maximum.
  215. *
  216. * Returns: calculated prescale - 1 for easy programming into SPI registers
  217. * or negative error number if valid prescalar cannot be updated.
  218. */
  219. static inline int davinci_spi_get_prescale(struct davinci_spi *dspi,
  220. u32 max_speed_hz)
  221. {
  222. int ret;
  223. ret = DIV_ROUND_UP(clk_get_rate(dspi->clk), max_speed_hz);
  224. if (ret < 3 || ret > 256)
  225. return -EINVAL;
  226. return ret - 1;
  227. }
  228. /**
  229. * davinci_spi_setup_transfer - This functions will determine transfer method
  230. * @spi: spi device on which data transfer to be done
  231. * @t: spi transfer in which transfer info is filled
  232. *
  233. * This function determines data transfer method (8/16/32 bit transfer).
  234. * It will also set the SPI Clock Control register according to
  235. * SPI slave device freq.
  236. */
  237. static int davinci_spi_setup_transfer(struct spi_device *spi,
  238. struct spi_transfer *t)
  239. {
  240. struct davinci_spi *dspi;
  241. struct davinci_spi_config *spicfg;
  242. u8 bits_per_word = 0;
  243. u32 hz = 0, spifmt = 0, prescale = 0;
  244. dspi = spi_master_get_devdata(spi->master);
  245. spicfg = (struct davinci_spi_config *)spi->controller_data;
  246. if (!spicfg)
  247. spicfg = &davinci_spi_default_cfg;
  248. if (t) {
  249. bits_per_word = t->bits_per_word;
  250. hz = t->speed_hz;
  251. }
  252. /* if bits_per_word is not set then set it default */
  253. if (!bits_per_word)
  254. bits_per_word = spi->bits_per_word;
  255. /*
  256. * Assign function pointer to appropriate transfer method
  257. * 8bit, 16bit or 32bit transfer
  258. */
  259. if (bits_per_word <= 8 && bits_per_word >= 2) {
  260. dspi->get_rx = davinci_spi_rx_buf_u8;
  261. dspi->get_tx = davinci_spi_tx_buf_u8;
  262. dspi->bytes_per_word[spi->chip_select] = 1;
  263. } else if (bits_per_word <= 16 && bits_per_word >= 2) {
  264. dspi->get_rx = davinci_spi_rx_buf_u16;
  265. dspi->get_tx = davinci_spi_tx_buf_u16;
  266. dspi->bytes_per_word[spi->chip_select] = 2;
  267. } else
  268. return -EINVAL;
  269. if (!hz)
  270. hz = spi->max_speed_hz;
  271. /* Set up SPIFMTn register, unique to this chipselect. */
  272. prescale = davinci_spi_get_prescale(dspi, hz);
  273. if (prescale < 0)
  274. return prescale;
  275. spifmt = (prescale << SPIFMT_PRESCALE_SHIFT) | (bits_per_word & 0x1f);
  276. if (spi->mode & SPI_LSB_FIRST)
  277. spifmt |= SPIFMT_SHIFTDIR_MASK;
  278. if (spi->mode & SPI_CPOL)
  279. spifmt |= SPIFMT_POLARITY_MASK;
  280. if (!(spi->mode & SPI_CPHA))
  281. spifmt |= SPIFMT_PHASE_MASK;
  282. /*
  283. * Version 1 hardware supports two basic SPI modes:
  284. * - Standard SPI mode uses 4 pins, with chipselect
  285. * - 3 pin SPI is a 4 pin variant without CS (SPI_NO_CS)
  286. * (distinct from SPI_3WIRE, with just one data wire;
  287. * or similar variants without MOSI or without MISO)
  288. *
  289. * Version 2 hardware supports an optional handshaking signal,
  290. * so it can support two more modes:
  291. * - 5 pin SPI variant is standard SPI plus SPI_READY
  292. * - 4 pin with enable is (SPI_READY | SPI_NO_CS)
  293. */
  294. if (dspi->version == SPI_VERSION_2) {
  295. u32 delay = 0;
  296. spifmt |= ((spicfg->wdelay << SPIFMT_WDELAY_SHIFT)
  297. & SPIFMT_WDELAY_MASK);
  298. if (spicfg->odd_parity)
  299. spifmt |= SPIFMT_ODD_PARITY_MASK;
  300. if (spicfg->parity_enable)
  301. spifmt |= SPIFMT_PARITYENA_MASK;
  302. if (spicfg->timer_disable) {
  303. spifmt |= SPIFMT_DISTIMER_MASK;
  304. } else {
  305. delay |= (spicfg->c2tdelay << SPIDELAY_C2TDELAY_SHIFT)
  306. & SPIDELAY_C2TDELAY_MASK;
  307. delay |= (spicfg->t2cdelay << SPIDELAY_T2CDELAY_SHIFT)
  308. & SPIDELAY_T2CDELAY_MASK;
  309. }
  310. if (spi->mode & SPI_READY) {
  311. spifmt |= SPIFMT_WAITENA_MASK;
  312. delay |= (spicfg->t2edelay << SPIDELAY_T2EDELAY_SHIFT)
  313. & SPIDELAY_T2EDELAY_MASK;
  314. delay |= (spicfg->c2edelay << SPIDELAY_C2EDELAY_SHIFT)
  315. & SPIDELAY_C2EDELAY_MASK;
  316. }
  317. iowrite32(delay, dspi->base + SPIDELAY);
  318. }
  319. iowrite32(spifmt, dspi->base + SPIFMT0);
  320. return 0;
  321. }
  322. /**
  323. * davinci_spi_setup - This functions will set default transfer method
  324. * @spi: spi device on which data transfer to be done
  325. *
  326. * This functions sets the default transfer method.
  327. */
  328. static int davinci_spi_setup(struct spi_device *spi)
  329. {
  330. int retval = 0;
  331. struct davinci_spi *dspi;
  332. struct davinci_spi_platform_data *pdata;
  333. dspi = spi_master_get_devdata(spi->master);
  334. pdata = dspi->pdata;
  335. /* if bits per word length is zero then set it default 8 */
  336. if (!spi->bits_per_word)
  337. spi->bits_per_word = 8;
  338. if (!(spi->mode & SPI_NO_CS)) {
  339. if ((pdata->chip_sel == NULL) ||
  340. (pdata->chip_sel[spi->chip_select] == SPI_INTERN_CS))
  341. set_io_bits(dspi->base + SPIPC0, 1 << spi->chip_select);
  342. }
  343. if (spi->mode & SPI_READY)
  344. set_io_bits(dspi->base + SPIPC0, SPIPC0_SPIENA_MASK);
  345. if (spi->mode & SPI_LOOP)
  346. set_io_bits(dspi->base + SPIGCR1, SPIGCR1_LOOPBACK_MASK);
  347. else
  348. clear_io_bits(dspi->base + SPIGCR1, SPIGCR1_LOOPBACK_MASK);
  349. return retval;
  350. }
  351. static int davinci_spi_check_error(struct davinci_spi *dspi, int int_status)
  352. {
  353. struct device *sdev = dspi->bitbang.master->dev.parent;
  354. if (int_status & SPIFLG_TIMEOUT_MASK) {
  355. dev_dbg(sdev, "SPI Time-out Error\n");
  356. return -ETIMEDOUT;
  357. }
  358. if (int_status & SPIFLG_DESYNC_MASK) {
  359. dev_dbg(sdev, "SPI Desynchronization Error\n");
  360. return -EIO;
  361. }
  362. if (int_status & SPIFLG_BITERR_MASK) {
  363. dev_dbg(sdev, "SPI Bit error\n");
  364. return -EIO;
  365. }
  366. if (dspi->version == SPI_VERSION_2) {
  367. if (int_status & SPIFLG_DLEN_ERR_MASK) {
  368. dev_dbg(sdev, "SPI Data Length Error\n");
  369. return -EIO;
  370. }
  371. if (int_status & SPIFLG_PARERR_MASK) {
  372. dev_dbg(sdev, "SPI Parity Error\n");
  373. return -EIO;
  374. }
  375. if (int_status & SPIFLG_OVRRUN_MASK) {
  376. dev_dbg(sdev, "SPI Data Overrun error\n");
  377. return -EIO;
  378. }
  379. if (int_status & SPIFLG_BUF_INIT_ACTIVE_MASK) {
  380. dev_dbg(sdev, "SPI Buffer Init Active\n");
  381. return -EBUSY;
  382. }
  383. }
  384. return 0;
  385. }
  386. /**
  387. * davinci_spi_process_events - check for and handle any SPI controller events
  388. * @dspi: the controller data
  389. *
  390. * This function will check the SPIFLG register and handle any events that are
  391. * detected there
  392. */
  393. static int davinci_spi_process_events(struct davinci_spi *dspi)
  394. {
  395. u32 buf, status, errors = 0, spidat1;
  396. buf = ioread32(dspi->base + SPIBUF);
  397. if (dspi->rcount > 0 && !(buf & SPIBUF_RXEMPTY_MASK)) {
  398. dspi->get_rx(buf & 0xFFFF, dspi);
  399. dspi->rcount--;
  400. }
  401. status = ioread32(dspi->base + SPIFLG);
  402. if (unlikely(status & SPIFLG_ERROR_MASK)) {
  403. errors = status & SPIFLG_ERROR_MASK;
  404. goto out;
  405. }
  406. if (dspi->wcount > 0 && !(buf & SPIBUF_TXFULL_MASK)) {
  407. spidat1 = ioread32(dspi->base + SPIDAT1);
  408. dspi->wcount--;
  409. spidat1 &= ~0xFFFF;
  410. spidat1 |= 0xFFFF & dspi->get_tx(dspi);
  411. iowrite32(spidat1, dspi->base + SPIDAT1);
  412. }
  413. out:
  414. return errors;
  415. }
  416. static void davinci_spi_dma_callback(unsigned lch, u16 status, void *data)
  417. {
  418. struct davinci_spi *dspi = data;
  419. struct davinci_spi_dma *dma = &dspi->dma;
  420. edma_stop(lch);
  421. if (status == DMA_COMPLETE) {
  422. if (lch == dma->rx_channel)
  423. dspi->rcount = 0;
  424. if (lch == dma->tx_channel)
  425. dspi->wcount = 0;
  426. }
  427. if ((!dspi->wcount && !dspi->rcount) || (status != DMA_COMPLETE))
  428. complete(&dspi->done);
  429. }
  430. /**
  431. * davinci_spi_bufs - functions which will handle transfer data
  432. * @spi: spi device on which data transfer to be done
  433. * @t: spi transfer in which transfer info is filled
  434. *
  435. * This function will put data to be transferred into data register
  436. * of SPI controller and then wait until the completion will be marked
  437. * by the IRQ Handler.
  438. */
  439. static int davinci_spi_bufs(struct spi_device *spi, struct spi_transfer *t)
  440. {
  441. struct davinci_spi *dspi;
  442. int data_type, ret;
  443. u32 tx_data, spidat1;
  444. u32 errors = 0;
  445. struct davinci_spi_config *spicfg;
  446. struct davinci_spi_platform_data *pdata;
  447. unsigned uninitialized_var(rx_buf_count);
  448. struct device *sdev;
  449. dspi = spi_master_get_devdata(spi->master);
  450. pdata = dspi->pdata;
  451. spicfg = (struct davinci_spi_config *)spi->controller_data;
  452. if (!spicfg)
  453. spicfg = &davinci_spi_default_cfg;
  454. sdev = dspi->bitbang.master->dev.parent;
  455. /* convert len to words based on bits_per_word */
  456. data_type = dspi->bytes_per_word[spi->chip_select];
  457. dspi->tx = t->tx_buf;
  458. dspi->rx = t->rx_buf;
  459. dspi->wcount = t->len / data_type;
  460. dspi->rcount = dspi->wcount;
  461. spidat1 = ioread32(dspi->base + SPIDAT1);
  462. clear_io_bits(dspi->base + SPIGCR1, SPIGCR1_POWERDOWN_MASK);
  463. set_io_bits(dspi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
  464. INIT_COMPLETION(dspi->done);
  465. if (spicfg->io_type == SPI_IO_TYPE_INTR)
  466. set_io_bits(dspi->base + SPIINT, SPIINT_MASKINT);
  467. if (spicfg->io_type != SPI_IO_TYPE_DMA) {
  468. /* start the transfer */
  469. dspi->wcount--;
  470. tx_data = dspi->get_tx(dspi);
  471. spidat1 &= 0xFFFF0000;
  472. spidat1 |= tx_data & 0xFFFF;
  473. iowrite32(spidat1, dspi->base + SPIDAT1);
  474. } else {
  475. struct davinci_spi_dma *dma;
  476. unsigned long tx_reg, rx_reg;
  477. struct edmacc_param param;
  478. void *rx_buf;
  479. int b, c;
  480. dma = &dspi->dma;
  481. tx_reg = (unsigned long)dspi->pbase + SPIDAT1;
  482. rx_reg = (unsigned long)dspi->pbase + SPIBUF;
  483. /*
  484. * Transmit DMA setup
  485. *
  486. * If there is transmit data, map the transmit buffer, set it
  487. * as the source of data and set the source B index to data
  488. * size. If there is no transmit data, set the transmit register
  489. * as the source of data, and set the source B index to zero.
  490. *
  491. * The destination is always the transmit register itself. And
  492. * the destination never increments.
  493. */
  494. if (t->tx_buf) {
  495. t->tx_dma = dma_map_single(&spi->dev, (void *)t->tx_buf,
  496. t->len, DMA_TO_DEVICE);
  497. if (dma_mapping_error(&spi->dev, t->tx_dma)) {
  498. dev_dbg(sdev, "Unable to DMA map %d bytes"
  499. "TX buffer\n", t->len);
  500. return -ENOMEM;
  501. }
  502. }
  503. /*
  504. * If number of words is greater than 65535, then we need
  505. * to configure a 3 dimension transfer. Use the BCNTRLD
  506. * feature to allow for transfers that aren't even multiples
  507. * of 65535 (or any other possible b size) by first transferring
  508. * the remainder amount then grabbing the next N blocks of
  509. * 65535 words.
  510. */
  511. c = dspi->wcount / (SZ_64K - 1); /* N 65535 Blocks */
  512. b = dspi->wcount - c * (SZ_64K - 1); /* Remainder */
  513. if (b)
  514. c++;
  515. else
  516. b = SZ_64K - 1;
  517. param.opt = TCINTEN | EDMA_TCC(dma->tx_channel);
  518. param.src = t->tx_buf ? t->tx_dma : tx_reg;
  519. param.a_b_cnt = b << 16 | data_type;
  520. param.dst = tx_reg;
  521. param.src_dst_bidx = t->tx_buf ? data_type : 0;
  522. param.link_bcntrld = 0xffffffff;
  523. param.src_dst_cidx = t->tx_buf ? data_type : 0;
  524. param.ccnt = c;
  525. edma_write_slot(dma->tx_channel, &param);
  526. edma_link(dma->tx_channel, dma->dummy_param_slot);
  527. /*
  528. * Receive DMA setup
  529. *
  530. * If there is receive buffer, use it to receive data. If there
  531. * is none provided, use a temporary receive buffer. Set the
  532. * destination B index to 0 so effectively only one byte is used
  533. * in the temporary buffer (address does not increment).
  534. *
  535. * The source of receive data is the receive data register. The
  536. * source address never increments.
  537. */
  538. if (t->rx_buf) {
  539. rx_buf = t->rx_buf;
  540. rx_buf_count = t->len;
  541. } else {
  542. rx_buf = dspi->rx_tmp_buf;
  543. rx_buf_count = sizeof(dspi->rx_tmp_buf);
  544. }
  545. t->rx_dma = dma_map_single(&spi->dev, rx_buf, rx_buf_count,
  546. DMA_FROM_DEVICE);
  547. if (dma_mapping_error(&spi->dev, t->rx_dma)) {
  548. dev_dbg(sdev, "Couldn't DMA map a %d bytes RX buffer\n",
  549. rx_buf_count);
  550. if (t->tx_buf)
  551. dma_unmap_single(NULL, t->tx_dma, t->len,
  552. DMA_TO_DEVICE);
  553. return -ENOMEM;
  554. }
  555. param.opt = TCINTEN | EDMA_TCC(dma->rx_channel);
  556. param.src = rx_reg;
  557. param.a_b_cnt = b << 16 | data_type;
  558. param.dst = t->rx_dma;
  559. param.src_dst_bidx = (t->rx_buf ? data_type : 0) << 16;
  560. param.link_bcntrld = 0xffffffff;
  561. param.src_dst_cidx = (t->rx_buf ? data_type : 0) << 16;
  562. param.ccnt = c;
  563. edma_write_slot(dma->rx_channel, &param);
  564. if (pdata->cshold_bug)
  565. iowrite16(spidat1 >> 16, dspi->base + SPIDAT1 + 2);
  566. edma_start(dma->rx_channel);
  567. edma_start(dma->tx_channel);
  568. set_io_bits(dspi->base + SPIINT, SPIINT_DMA_REQ_EN);
  569. }
  570. /* Wait for the transfer to complete */
  571. if (spicfg->io_type != SPI_IO_TYPE_POLL) {
  572. wait_for_completion_interruptible(&(dspi->done));
  573. } else {
  574. while (dspi->rcount > 0 || dspi->wcount > 0) {
  575. errors = davinci_spi_process_events(dspi);
  576. if (errors)
  577. break;
  578. cpu_relax();
  579. }
  580. }
  581. clear_io_bits(dspi->base + SPIINT, SPIINT_MASKALL);
  582. if (spicfg->io_type == SPI_IO_TYPE_DMA) {
  583. if (t->tx_buf)
  584. dma_unmap_single(NULL, t->tx_dma, t->len,
  585. DMA_TO_DEVICE);
  586. dma_unmap_single(NULL, t->rx_dma, rx_buf_count,
  587. DMA_FROM_DEVICE);
  588. clear_io_bits(dspi->base + SPIINT, SPIINT_DMA_REQ_EN);
  589. }
  590. clear_io_bits(dspi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
  591. set_io_bits(dspi->base + SPIGCR1, SPIGCR1_POWERDOWN_MASK);
  592. /*
  593. * Check for bit error, desync error,parity error,timeout error and
  594. * receive overflow errors
  595. */
  596. if (errors) {
  597. ret = davinci_spi_check_error(dspi, errors);
  598. WARN(!ret, "%s: error reported but no error found!\n",
  599. dev_name(&spi->dev));
  600. return ret;
  601. }
  602. if (dspi->rcount != 0 || dspi->wcount != 0) {
  603. dev_err(sdev, "SPI data transfer error\n");
  604. return -EIO;
  605. }
  606. return t->len;
  607. }
  608. /**
  609. * davinci_spi_irq - Interrupt handler for SPI Master Controller
  610. * @irq: IRQ number for this SPI Master
  611. * @context_data: structure for SPI Master controller davinci_spi
  612. *
  613. * ISR will determine that interrupt arrives either for READ or WRITE command.
  614. * According to command it will do the appropriate action. It will check
  615. * transfer length and if it is not zero then dispatch transfer command again.
  616. * If transfer length is zero then it will indicate the COMPLETION so that
  617. * davinci_spi_bufs function can go ahead.
  618. */
  619. static irqreturn_t davinci_spi_irq(s32 irq, void *data)
  620. {
  621. struct davinci_spi *dspi = data;
  622. int status;
  623. status = davinci_spi_process_events(dspi);
  624. if (unlikely(status != 0))
  625. clear_io_bits(dspi->base + SPIINT, SPIINT_MASKINT);
  626. if ((!dspi->rcount && !dspi->wcount) || status)
  627. complete(&dspi->done);
  628. return IRQ_HANDLED;
  629. }
  630. static int davinci_spi_request_dma(struct davinci_spi *dspi)
  631. {
  632. int r;
  633. struct davinci_spi_dma *dma = &dspi->dma;
  634. r = edma_alloc_channel(dma->rx_channel, davinci_spi_dma_callback, dspi,
  635. dma->eventq);
  636. if (r < 0) {
  637. pr_err("Unable to request DMA channel for SPI RX\n");
  638. r = -EAGAIN;
  639. goto rx_dma_failed;
  640. }
  641. r = edma_alloc_channel(dma->tx_channel, davinci_spi_dma_callback, dspi,
  642. dma->eventq);
  643. if (r < 0) {
  644. pr_err("Unable to request DMA channel for SPI TX\n");
  645. r = -EAGAIN;
  646. goto tx_dma_failed;
  647. }
  648. r = edma_alloc_slot(EDMA_CTLR(dma->tx_channel), EDMA_SLOT_ANY);
  649. if (r < 0) {
  650. pr_err("Unable to request SPI TX DMA param slot\n");
  651. r = -EAGAIN;
  652. goto param_failed;
  653. }
  654. dma->dummy_param_slot = r;
  655. edma_link(dma->dummy_param_slot, dma->dummy_param_slot);
  656. return 0;
  657. param_failed:
  658. edma_free_channel(dma->tx_channel);
  659. tx_dma_failed:
  660. edma_free_channel(dma->rx_channel);
  661. rx_dma_failed:
  662. return r;
  663. }
  664. /**
  665. * davinci_spi_probe - probe function for SPI Master Controller
  666. * @pdev: platform_device structure which contains plateform specific data
  667. *
  668. * According to Linux Device Model this function will be invoked by Linux
  669. * with platform_device struct which contains the device specific info.
  670. * This function will map the SPI controller's memory, register IRQ,
  671. * Reset SPI controller and setting its registers to default value.
  672. * It will invoke spi_bitbang_start to create work queue so that client driver
  673. * can register transfer method to work queue.
  674. */
  675. static int davinci_spi_probe(struct platform_device *pdev)
  676. {
  677. struct spi_master *master;
  678. struct davinci_spi *dspi;
  679. struct davinci_spi_platform_data *pdata;
  680. struct resource *r, *mem;
  681. resource_size_t dma_rx_chan = SPI_NO_RESOURCE;
  682. resource_size_t dma_tx_chan = SPI_NO_RESOURCE;
  683. int i = 0, ret = 0;
  684. u32 spipc0;
  685. pdata = pdev->dev.platform_data;
  686. if (pdata == NULL) {
  687. ret = -ENODEV;
  688. goto err;
  689. }
  690. master = spi_alloc_master(&pdev->dev, sizeof(struct davinci_spi));
  691. if (master == NULL) {
  692. ret = -ENOMEM;
  693. goto err;
  694. }
  695. dev_set_drvdata(&pdev->dev, master);
  696. dspi = spi_master_get_devdata(master);
  697. if (dspi == NULL) {
  698. ret = -ENOENT;
  699. goto free_master;
  700. }
  701. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  702. if (r == NULL) {
  703. ret = -ENOENT;
  704. goto free_master;
  705. }
  706. dspi->pbase = r->start;
  707. dspi->pdata = pdata;
  708. mem = request_mem_region(r->start, resource_size(r), pdev->name);
  709. if (mem == NULL) {
  710. ret = -EBUSY;
  711. goto free_master;
  712. }
  713. dspi->base = ioremap(r->start, resource_size(r));
  714. if (dspi->base == NULL) {
  715. ret = -ENOMEM;
  716. goto release_region;
  717. }
  718. dspi->irq = platform_get_irq(pdev, 0);
  719. if (dspi->irq <= 0) {
  720. ret = -EINVAL;
  721. goto unmap_io;
  722. }
  723. ret = request_irq(dspi->irq, davinci_spi_irq, 0, dev_name(&pdev->dev),
  724. dspi);
  725. if (ret)
  726. goto unmap_io;
  727. dspi->bitbang.master = spi_master_get(master);
  728. if (dspi->bitbang.master == NULL) {
  729. ret = -ENODEV;
  730. goto irq_free;
  731. }
  732. dspi->clk = clk_get(&pdev->dev, NULL);
  733. if (IS_ERR(dspi->clk)) {
  734. ret = -ENODEV;
  735. goto put_master;
  736. }
  737. clk_enable(dspi->clk);
  738. master->bus_num = pdev->id;
  739. master->num_chipselect = pdata->num_chipselect;
  740. master->setup = davinci_spi_setup;
  741. dspi->bitbang.chipselect = davinci_spi_chipselect;
  742. dspi->bitbang.setup_transfer = davinci_spi_setup_transfer;
  743. dspi->version = pdata->version;
  744. dspi->bitbang.flags = SPI_NO_CS | SPI_LSB_FIRST | SPI_LOOP;
  745. if (dspi->version == SPI_VERSION_2)
  746. dspi->bitbang.flags |= SPI_READY;
  747. r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  748. if (r)
  749. dma_rx_chan = r->start;
  750. r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  751. if (r)
  752. dma_tx_chan = r->start;
  753. dspi->bitbang.txrx_bufs = davinci_spi_bufs;
  754. if (dma_rx_chan != SPI_NO_RESOURCE &&
  755. dma_tx_chan != SPI_NO_RESOURCE) {
  756. dspi->dma.rx_channel = dma_rx_chan;
  757. dspi->dma.tx_channel = dma_tx_chan;
  758. dspi->dma.eventq = pdata->dma_event_q;
  759. ret = davinci_spi_request_dma(dspi);
  760. if (ret)
  761. goto free_clk;
  762. dev_info(&pdev->dev, "DMA: supported\n");
  763. dev_info(&pdev->dev, "DMA: RX channel: %d, TX channel: %d, "
  764. "event queue: %d\n", dma_rx_chan, dma_tx_chan,
  765. pdata->dma_event_q);
  766. }
  767. dspi->get_rx = davinci_spi_rx_buf_u8;
  768. dspi->get_tx = davinci_spi_tx_buf_u8;
  769. init_completion(&dspi->done);
  770. /* Reset In/OUT SPI module */
  771. iowrite32(0, dspi->base + SPIGCR0);
  772. udelay(100);
  773. iowrite32(1, dspi->base + SPIGCR0);
  774. /* Set up SPIPC0. CS and ENA init is done in davinci_spi_setup */
  775. spipc0 = SPIPC0_DIFUN_MASK | SPIPC0_DOFUN_MASK | SPIPC0_CLKFUN_MASK;
  776. iowrite32(spipc0, dspi->base + SPIPC0);
  777. /* initialize chip selects */
  778. if (pdata->chip_sel) {
  779. for (i = 0; i < pdata->num_chipselect; i++) {
  780. if (pdata->chip_sel[i] != SPI_INTERN_CS)
  781. gpio_direction_output(pdata->chip_sel[i], 1);
  782. }
  783. }
  784. if (pdata->intr_line)
  785. iowrite32(SPI_INTLVL_1, dspi->base + SPILVL);
  786. else
  787. iowrite32(SPI_INTLVL_0, dspi->base + SPILVL);
  788. iowrite32(CS_DEFAULT, dspi->base + SPIDEF);
  789. /* master mode default */
  790. set_io_bits(dspi->base + SPIGCR1, SPIGCR1_CLKMOD_MASK);
  791. set_io_bits(dspi->base + SPIGCR1, SPIGCR1_MASTER_MASK);
  792. set_io_bits(dspi->base + SPIGCR1, SPIGCR1_POWERDOWN_MASK);
  793. ret = spi_bitbang_start(&dspi->bitbang);
  794. if (ret)
  795. goto free_dma;
  796. dev_info(&pdev->dev, "Controller at 0x%p\n", dspi->base);
  797. return ret;
  798. free_dma:
  799. edma_free_channel(dspi->dma.tx_channel);
  800. edma_free_channel(dspi->dma.rx_channel);
  801. edma_free_slot(dspi->dma.dummy_param_slot);
  802. free_clk:
  803. clk_disable(dspi->clk);
  804. clk_put(dspi->clk);
  805. put_master:
  806. spi_master_put(master);
  807. irq_free:
  808. free_irq(dspi->irq, dspi);
  809. unmap_io:
  810. iounmap(dspi->base);
  811. release_region:
  812. release_mem_region(dspi->pbase, resource_size(r));
  813. free_master:
  814. kfree(master);
  815. err:
  816. return ret;
  817. }
  818. /**
  819. * davinci_spi_remove - remove function for SPI Master Controller
  820. * @pdev: platform_device structure which contains plateform specific data
  821. *
  822. * This function will do the reverse action of davinci_spi_probe function
  823. * It will free the IRQ and SPI controller's memory region.
  824. * It will also call spi_bitbang_stop to destroy the work queue which was
  825. * created by spi_bitbang_start.
  826. */
  827. static int __exit davinci_spi_remove(struct platform_device *pdev)
  828. {
  829. struct davinci_spi *dspi;
  830. struct spi_master *master;
  831. struct resource *r;
  832. master = dev_get_drvdata(&pdev->dev);
  833. dspi = spi_master_get_devdata(master);
  834. spi_bitbang_stop(&dspi->bitbang);
  835. clk_disable(dspi->clk);
  836. clk_put(dspi->clk);
  837. spi_master_put(master);
  838. free_irq(dspi->irq, dspi);
  839. iounmap(dspi->base);
  840. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  841. release_mem_region(dspi->pbase, resource_size(r));
  842. return 0;
  843. }
  844. static struct platform_driver davinci_spi_driver = {
  845. .driver = {
  846. .name = "spi_davinci",
  847. .owner = THIS_MODULE,
  848. },
  849. .remove = __exit_p(davinci_spi_remove),
  850. };
  851. static int __init davinci_spi_init(void)
  852. {
  853. return platform_driver_probe(&davinci_spi_driver, davinci_spi_probe);
  854. }
  855. module_init(davinci_spi_init);
  856. static void __exit davinci_spi_exit(void)
  857. {
  858. platform_driver_unregister(&davinci_spi_driver);
  859. }
  860. module_exit(davinci_spi_exit);
  861. MODULE_DESCRIPTION("TI DaVinci SPI Master Controller Driver");
  862. MODULE_LICENSE("GPL");