davinci_spi.c 27 KB

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