spi-davinci.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  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. #include <linux/interrupt.h>
  16. #include <linux/io.h>
  17. #include <linux/gpio.h>
  18. #include <linux/module.h>
  19. #include <linux/delay.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/err.h>
  22. #include <linux/clk.h>
  23. #include <linux/dmaengine.h>
  24. #include <linux/dma-mapping.h>
  25. #include <linux/of.h>
  26. #include <linux/of_device.h>
  27. #include <linux/of_gpio.h>
  28. #include <linux/spi/spi.h>
  29. #include <linux/spi/spi_bitbang.h>
  30. #include <linux/slab.h>
  31. #include <linux/platform_data/spi-davinci.h>
  32. #define CS_DEFAULT 0xFF
  33. #define SPIFMT_PHASE_MASK BIT(16)
  34. #define SPIFMT_POLARITY_MASK BIT(17)
  35. #define SPIFMT_DISTIMER_MASK BIT(18)
  36. #define SPIFMT_SHIFTDIR_MASK BIT(20)
  37. #define SPIFMT_WAITENA_MASK BIT(21)
  38. #define SPIFMT_PARITYENA_MASK BIT(22)
  39. #define SPIFMT_ODD_PARITY_MASK BIT(23)
  40. #define SPIFMT_WDELAY_MASK 0x3f000000u
  41. #define SPIFMT_WDELAY_SHIFT 24
  42. #define SPIFMT_PRESCALE_SHIFT 8
  43. /* SPIPC0 */
  44. #define SPIPC0_DIFUN_MASK BIT(11) /* MISO */
  45. #define SPIPC0_DOFUN_MASK BIT(10) /* MOSI */
  46. #define SPIPC0_CLKFUN_MASK BIT(9) /* CLK */
  47. #define SPIPC0_SPIENA_MASK BIT(8) /* nREADY */
  48. #define SPIINT_MASKALL 0x0101035F
  49. #define SPIINT_MASKINT 0x0000015F
  50. #define SPI_INTLVL_1 0x000001FF
  51. #define SPI_INTLVL_0 0x00000000
  52. /* SPIDAT1 (upper 16 bit defines) */
  53. #define SPIDAT1_CSHOLD_MASK BIT(12)
  54. #define SPIDAT1_WDEL BIT(10)
  55. /* SPIGCR1 */
  56. #define SPIGCR1_CLKMOD_MASK BIT(1)
  57. #define SPIGCR1_MASTER_MASK BIT(0)
  58. #define SPIGCR1_POWERDOWN_MASK BIT(8)
  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. #define DMA_MIN_BYTES 16
  99. /* SPI Controller driver's private data. */
  100. struct davinci_spi {
  101. struct spi_bitbang bitbang;
  102. struct clk *clk;
  103. u8 version;
  104. resource_size_t pbase;
  105. void __iomem *base;
  106. u32 irq;
  107. struct completion done;
  108. const void *tx;
  109. void *rx;
  110. int rcount;
  111. int wcount;
  112. struct dma_chan *dma_rx;
  113. struct dma_chan *dma_tx;
  114. struct davinci_spi_platform_data pdata;
  115. void (*get_rx)(u32 rx_data, struct davinci_spi *);
  116. u32 (*get_tx)(struct davinci_spi *);
  117. u8 *bytes_per_word;
  118. u8 prescaler_limit;
  119. };
  120. static struct davinci_spi_config davinci_spi_default_cfg;
  121. static void davinci_spi_rx_buf_u8(u32 data, struct davinci_spi *dspi)
  122. {
  123. if (dspi->rx) {
  124. u8 *rx = dspi->rx;
  125. *rx++ = (u8)data;
  126. dspi->rx = rx;
  127. }
  128. }
  129. static void davinci_spi_rx_buf_u16(u32 data, struct davinci_spi *dspi)
  130. {
  131. if (dspi->rx) {
  132. u16 *rx = dspi->rx;
  133. *rx++ = (u16)data;
  134. dspi->rx = rx;
  135. }
  136. }
  137. static u32 davinci_spi_tx_buf_u8(struct davinci_spi *dspi)
  138. {
  139. u32 data = 0;
  140. if (dspi->tx) {
  141. const u8 *tx = dspi->tx;
  142. data = *tx++;
  143. dspi->tx = tx;
  144. }
  145. return data;
  146. }
  147. static u32 davinci_spi_tx_buf_u16(struct davinci_spi *dspi)
  148. {
  149. u32 data = 0;
  150. if (dspi->tx) {
  151. const u16 *tx = dspi->tx;
  152. data = *tx++;
  153. dspi->tx = tx;
  154. }
  155. return data;
  156. }
  157. static inline void set_io_bits(void __iomem *addr, u32 bits)
  158. {
  159. u32 v = ioread32(addr);
  160. v |= bits;
  161. iowrite32(v, addr);
  162. }
  163. static inline void clear_io_bits(void __iomem *addr, u32 bits)
  164. {
  165. u32 v = ioread32(addr);
  166. v &= ~bits;
  167. iowrite32(v, addr);
  168. }
  169. /*
  170. * Interface to control the chip select signal
  171. */
  172. static void davinci_spi_chipselect(struct spi_device *spi, int value)
  173. {
  174. struct davinci_spi *dspi;
  175. struct davinci_spi_platform_data *pdata;
  176. struct davinci_spi_config *spicfg = spi->controller_data;
  177. u8 chip_sel = spi->chip_select;
  178. u16 spidat1 = CS_DEFAULT;
  179. dspi = spi_master_get_devdata(spi->master);
  180. pdata = &dspi->pdata;
  181. /* program delay transfers if tx_delay is non zero */
  182. if (spicfg && spicfg->wdelay)
  183. spidat1 |= SPIDAT1_WDEL;
  184. /*
  185. * Board specific chip select logic decides the polarity and cs
  186. * line for the controller
  187. */
  188. if (spi->cs_gpio >= 0) {
  189. if (value == BITBANG_CS_ACTIVE)
  190. gpio_set_value(spi->cs_gpio, spi->mode & SPI_CS_HIGH);
  191. else
  192. gpio_set_value(spi->cs_gpio,
  193. !(spi->mode & SPI_CS_HIGH));
  194. } else {
  195. if (value == BITBANG_CS_ACTIVE) {
  196. spidat1 |= SPIDAT1_CSHOLD_MASK;
  197. spidat1 &= ~(0x1 << chip_sel);
  198. }
  199. }
  200. iowrite16(spidat1, dspi->base + SPIDAT1 + 2);
  201. }
  202. /**
  203. * davinci_spi_get_prescale - Calculates the correct prescale value
  204. * @maxspeed_hz: the maximum rate the SPI clock can run at
  205. *
  206. * This function calculates the prescale value that generates a clock rate
  207. * less than or equal to the specified maximum.
  208. *
  209. * Returns: calculated prescale value for easy programming into SPI registers
  210. * or negative error number if valid prescalar cannot be updated.
  211. */
  212. static inline int davinci_spi_get_prescale(struct davinci_spi *dspi,
  213. u32 max_speed_hz)
  214. {
  215. int ret;
  216. /* Subtract 1 to match what will be programmed into SPI register. */
  217. ret = DIV_ROUND_UP(clk_get_rate(dspi->clk), max_speed_hz) - 1;
  218. if (ret < dspi->prescaler_limit || ret > 255)
  219. return -EINVAL;
  220. return ret;
  221. }
  222. /**
  223. * davinci_spi_setup_transfer - This functions will determine transfer method
  224. * @spi: spi device on which data transfer to be done
  225. * @t: spi transfer in which transfer info is filled
  226. *
  227. * This function determines data transfer method (8/16/32 bit transfer).
  228. * It will also set the SPI Clock Control register according to
  229. * SPI slave device freq.
  230. */
  231. static int davinci_spi_setup_transfer(struct spi_device *spi,
  232. struct spi_transfer *t)
  233. {
  234. struct davinci_spi *dspi;
  235. struct davinci_spi_config *spicfg;
  236. u8 bits_per_word = 0;
  237. u32 hz = 0, spifmt = 0;
  238. int prescale;
  239. dspi = spi_master_get_devdata(spi->master);
  240. spicfg = spi->controller_data;
  241. if (!spicfg)
  242. spicfg = &davinci_spi_default_cfg;
  243. if (t) {
  244. bits_per_word = t->bits_per_word;
  245. hz = t->speed_hz;
  246. }
  247. /* if bits_per_word is not set then set it default */
  248. if (!bits_per_word)
  249. bits_per_word = spi->bits_per_word;
  250. /*
  251. * Assign function pointer to appropriate transfer method
  252. * 8bit, 16bit or 32bit transfer
  253. */
  254. if (bits_per_word <= 8) {
  255. dspi->get_rx = davinci_spi_rx_buf_u8;
  256. dspi->get_tx = davinci_spi_tx_buf_u8;
  257. dspi->bytes_per_word[spi->chip_select] = 1;
  258. } else {
  259. dspi->get_rx = davinci_spi_rx_buf_u16;
  260. dspi->get_tx = davinci_spi_tx_buf_u16;
  261. dspi->bytes_per_word[spi->chip_select] = 2;
  262. }
  263. if (!hz)
  264. hz = spi->max_speed_hz;
  265. /* Set up SPIFMTn register, unique to this chipselect. */
  266. prescale = davinci_spi_get_prescale(dspi, hz);
  267. if (prescale < 0)
  268. return prescale;
  269. spifmt = (prescale << SPIFMT_PRESCALE_SHIFT) | (bits_per_word & 0x1f);
  270. if (spi->mode & SPI_LSB_FIRST)
  271. spifmt |= SPIFMT_SHIFTDIR_MASK;
  272. if (spi->mode & SPI_CPOL)
  273. spifmt |= SPIFMT_POLARITY_MASK;
  274. if (!(spi->mode & SPI_CPHA))
  275. spifmt |= SPIFMT_PHASE_MASK;
  276. /*
  277. * Assume wdelay is used only on SPI peripherals that has this field
  278. * in SPIFMTn register and when it's configured from board file or DT.
  279. */
  280. if (spicfg->wdelay)
  281. spifmt |= ((spicfg->wdelay << SPIFMT_WDELAY_SHIFT)
  282. & SPIFMT_WDELAY_MASK);
  283. /*
  284. * Version 1 hardware supports two basic SPI modes:
  285. * - Standard SPI mode uses 4 pins, with chipselect
  286. * - 3 pin SPI is a 4 pin variant without CS (SPI_NO_CS)
  287. * (distinct from SPI_3WIRE, with just one data wire;
  288. * or similar variants without MOSI or without MISO)
  289. *
  290. * Version 2 hardware supports an optional handshaking signal,
  291. * so it can support two more modes:
  292. * - 5 pin SPI variant is standard SPI plus SPI_READY
  293. * - 4 pin with enable is (SPI_READY | SPI_NO_CS)
  294. */
  295. if (dspi->version == SPI_VERSION_2) {
  296. u32 delay = 0;
  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, dspi->base + SPIDELAY);
  317. }
  318. iowrite32(spifmt, dspi->base + SPIFMT0);
  319. return 0;
  320. }
  321. static int davinci_spi_of_setup(struct spi_device *spi)
  322. {
  323. struct davinci_spi_config *spicfg = spi->controller_data;
  324. struct device_node *np = spi->dev.of_node;
  325. struct davinci_spi *dspi = spi_master_get_devdata(spi->master);
  326. u32 prop;
  327. if (spicfg == NULL && np) {
  328. spicfg = kzalloc(sizeof(*spicfg), GFP_KERNEL);
  329. if (!spicfg)
  330. return -ENOMEM;
  331. *spicfg = davinci_spi_default_cfg;
  332. /* override with dt configured values */
  333. if (!of_property_read_u32(np, "ti,spi-wdelay", &prop))
  334. spicfg->wdelay = (u8)prop;
  335. spi->controller_data = spicfg;
  336. if (dspi->dma_rx && dspi->dma_tx)
  337. spicfg->io_type = SPI_IO_TYPE_DMA;
  338. }
  339. return 0;
  340. }
  341. /**
  342. * davinci_spi_setup - This functions will set default transfer method
  343. * @spi: spi device on which data transfer to be done
  344. *
  345. * This functions sets the default transfer method.
  346. */
  347. static int davinci_spi_setup(struct spi_device *spi)
  348. {
  349. int retval = 0;
  350. struct davinci_spi *dspi;
  351. struct davinci_spi_platform_data *pdata;
  352. struct spi_master *master = spi->master;
  353. struct device_node *np = spi->dev.of_node;
  354. bool internal_cs = true;
  355. dspi = spi_master_get_devdata(spi->master);
  356. pdata = &dspi->pdata;
  357. if (!(spi->mode & SPI_NO_CS)) {
  358. if (np && (master->cs_gpios != NULL) && (spi->cs_gpio >= 0)) {
  359. retval = gpio_direction_output(
  360. spi->cs_gpio, !(spi->mode & SPI_CS_HIGH));
  361. internal_cs = false;
  362. } else if (pdata->chip_sel &&
  363. spi->chip_select < pdata->num_chipselect &&
  364. pdata->chip_sel[spi->chip_select] != SPI_INTERN_CS) {
  365. spi->cs_gpio = pdata->chip_sel[spi->chip_select];
  366. retval = gpio_direction_output(
  367. spi->cs_gpio, !(spi->mode & SPI_CS_HIGH));
  368. internal_cs = false;
  369. }
  370. if (retval) {
  371. dev_err(&spi->dev, "GPIO %d setup failed (%d)\n",
  372. spi->cs_gpio, retval);
  373. return retval;
  374. }
  375. if (internal_cs)
  376. set_io_bits(dspi->base + SPIPC0, 1 << spi->chip_select);
  377. }
  378. if (spi->mode & SPI_READY)
  379. set_io_bits(dspi->base + SPIPC0, SPIPC0_SPIENA_MASK);
  380. if (spi->mode & SPI_LOOP)
  381. set_io_bits(dspi->base + SPIGCR1, SPIGCR1_LOOPBACK_MASK);
  382. else
  383. clear_io_bits(dspi->base + SPIGCR1, SPIGCR1_LOOPBACK_MASK);
  384. return davinci_spi_of_setup(spi);
  385. }
  386. static void davinci_spi_cleanup(struct spi_device *spi)
  387. {
  388. struct davinci_spi_config *spicfg = spi->controller_data;
  389. spi->controller_data = NULL;
  390. if (spi->dev.of_node)
  391. kfree(spicfg);
  392. }
  393. static bool davinci_spi_can_dma(struct spi_master *master,
  394. struct spi_device *spi,
  395. struct spi_transfer *xfer)
  396. {
  397. struct davinci_spi_config *spicfg = spi->controller_data;
  398. bool can_dma = false;
  399. if (spicfg)
  400. can_dma = (spicfg->io_type == SPI_IO_TYPE_DMA) &&
  401. (xfer->len >= DMA_MIN_BYTES) &&
  402. !is_vmalloc_addr(xfer->rx_buf) &&
  403. !is_vmalloc_addr(xfer->tx_buf);
  404. return can_dma;
  405. }
  406. static int davinci_spi_check_error(struct davinci_spi *dspi, int int_status)
  407. {
  408. struct device *sdev = dspi->bitbang.master->dev.parent;
  409. if (int_status & SPIFLG_TIMEOUT_MASK) {
  410. dev_err(sdev, "SPI Time-out Error\n");
  411. return -ETIMEDOUT;
  412. }
  413. if (int_status & SPIFLG_DESYNC_MASK) {
  414. dev_err(sdev, "SPI Desynchronization Error\n");
  415. return -EIO;
  416. }
  417. if (int_status & SPIFLG_BITERR_MASK) {
  418. dev_err(sdev, "SPI Bit error\n");
  419. return -EIO;
  420. }
  421. if (dspi->version == SPI_VERSION_2) {
  422. if (int_status & SPIFLG_DLEN_ERR_MASK) {
  423. dev_err(sdev, "SPI Data Length Error\n");
  424. return -EIO;
  425. }
  426. if (int_status & SPIFLG_PARERR_MASK) {
  427. dev_err(sdev, "SPI Parity Error\n");
  428. return -EIO;
  429. }
  430. if (int_status & SPIFLG_OVRRUN_MASK) {
  431. dev_err(sdev, "SPI Data Overrun error\n");
  432. return -EIO;
  433. }
  434. if (int_status & SPIFLG_BUF_INIT_ACTIVE_MASK) {
  435. dev_err(sdev, "SPI Buffer Init Active\n");
  436. return -EBUSY;
  437. }
  438. }
  439. return 0;
  440. }
  441. /**
  442. * davinci_spi_process_events - check for and handle any SPI controller events
  443. * @dspi: the controller data
  444. *
  445. * This function will check the SPIFLG register and handle any events that are
  446. * detected there
  447. */
  448. static int davinci_spi_process_events(struct davinci_spi *dspi)
  449. {
  450. u32 buf, status, errors = 0, spidat1;
  451. buf = ioread32(dspi->base + SPIBUF);
  452. if (dspi->rcount > 0 && !(buf & SPIBUF_RXEMPTY_MASK)) {
  453. dspi->get_rx(buf & 0xFFFF, dspi);
  454. dspi->rcount--;
  455. }
  456. status = ioread32(dspi->base + SPIFLG);
  457. if (unlikely(status & SPIFLG_ERROR_MASK)) {
  458. errors = status & SPIFLG_ERROR_MASK;
  459. goto out;
  460. }
  461. if (dspi->wcount > 0 && !(buf & SPIBUF_TXFULL_MASK)) {
  462. spidat1 = ioread32(dspi->base + SPIDAT1);
  463. dspi->wcount--;
  464. spidat1 &= ~0xFFFF;
  465. spidat1 |= 0xFFFF & dspi->get_tx(dspi);
  466. iowrite32(spidat1, dspi->base + SPIDAT1);
  467. }
  468. out:
  469. return errors;
  470. }
  471. static void davinci_spi_dma_rx_callback(void *data)
  472. {
  473. struct davinci_spi *dspi = (struct davinci_spi *)data;
  474. dspi->rcount = 0;
  475. if (!dspi->wcount && !dspi->rcount)
  476. complete(&dspi->done);
  477. }
  478. static void davinci_spi_dma_tx_callback(void *data)
  479. {
  480. struct davinci_spi *dspi = (struct davinci_spi *)data;
  481. dspi->wcount = 0;
  482. if (!dspi->wcount && !dspi->rcount)
  483. complete(&dspi->done);
  484. }
  485. /**
  486. * davinci_spi_bufs - functions which will handle transfer data
  487. * @spi: spi device on which data transfer to be done
  488. * @t: spi transfer in which transfer info is filled
  489. *
  490. * This function will put data to be transferred into data register
  491. * of SPI controller and then wait until the completion will be marked
  492. * by the IRQ Handler.
  493. */
  494. static int davinci_spi_bufs(struct spi_device *spi, struct spi_transfer *t)
  495. {
  496. struct davinci_spi *dspi;
  497. int data_type, ret = -ENOMEM;
  498. u32 tx_data, spidat1;
  499. u32 errors = 0;
  500. struct davinci_spi_config *spicfg;
  501. struct davinci_spi_platform_data *pdata;
  502. unsigned uninitialized_var(rx_buf_count);
  503. dspi = spi_master_get_devdata(spi->master);
  504. pdata = &dspi->pdata;
  505. spicfg = (struct davinci_spi_config *)spi->controller_data;
  506. if (!spicfg)
  507. spicfg = &davinci_spi_default_cfg;
  508. /* convert len to words based on bits_per_word */
  509. data_type = dspi->bytes_per_word[spi->chip_select];
  510. dspi->tx = t->tx_buf;
  511. dspi->rx = t->rx_buf;
  512. dspi->wcount = t->len / data_type;
  513. dspi->rcount = dspi->wcount;
  514. spidat1 = ioread32(dspi->base + SPIDAT1);
  515. clear_io_bits(dspi->base + SPIGCR1, SPIGCR1_POWERDOWN_MASK);
  516. set_io_bits(dspi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
  517. reinit_completion(&dspi->done);
  518. if (!davinci_spi_can_dma(spi->master, spi, t)) {
  519. if (spicfg->io_type != SPI_IO_TYPE_POLL)
  520. set_io_bits(dspi->base + SPIINT, SPIINT_MASKINT);
  521. /* start the transfer */
  522. dspi->wcount--;
  523. tx_data = dspi->get_tx(dspi);
  524. spidat1 &= 0xFFFF0000;
  525. spidat1 |= tx_data & 0xFFFF;
  526. iowrite32(spidat1, dspi->base + SPIDAT1);
  527. } else {
  528. struct dma_slave_config dma_rx_conf = {
  529. .direction = DMA_DEV_TO_MEM,
  530. .src_addr = (unsigned long)dspi->pbase + SPIBUF,
  531. .src_addr_width = data_type,
  532. .src_maxburst = 1,
  533. };
  534. struct dma_slave_config dma_tx_conf = {
  535. .direction = DMA_MEM_TO_DEV,
  536. .dst_addr = (unsigned long)dspi->pbase + SPIDAT1,
  537. .dst_addr_width = data_type,
  538. .dst_maxburst = 1,
  539. };
  540. struct dma_async_tx_descriptor *rxdesc;
  541. struct dma_async_tx_descriptor *txdesc;
  542. dmaengine_slave_config(dspi->dma_rx, &dma_rx_conf);
  543. dmaengine_slave_config(dspi->dma_tx, &dma_tx_conf);
  544. rxdesc = dmaengine_prep_slave_sg(dspi->dma_rx,
  545. t->rx_sg.sgl, t->rx_sg.nents, DMA_DEV_TO_MEM,
  546. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  547. if (!rxdesc)
  548. goto err_desc;
  549. if (!t->tx_buf) {
  550. /* To avoid errors when doing rx-only transfers with
  551. * many SG entries (> 20), use the rx buffer as the
  552. * dummy tx buffer so that dma reloads are done at the
  553. * same time for rx and tx.
  554. */
  555. t->tx_sg.sgl = t->rx_sg.sgl;
  556. t->tx_sg.nents = t->rx_sg.nents;
  557. }
  558. txdesc = dmaengine_prep_slave_sg(dspi->dma_tx,
  559. t->tx_sg.sgl, t->tx_sg.nents, DMA_MEM_TO_DEV,
  560. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  561. if (!txdesc)
  562. goto err_desc;
  563. rxdesc->callback = davinci_spi_dma_rx_callback;
  564. rxdesc->callback_param = (void *)dspi;
  565. txdesc->callback = davinci_spi_dma_tx_callback;
  566. txdesc->callback_param = (void *)dspi;
  567. if (pdata->cshold_bug)
  568. iowrite16(spidat1 >> 16, dspi->base + SPIDAT1 + 2);
  569. dmaengine_submit(rxdesc);
  570. dmaengine_submit(txdesc);
  571. dma_async_issue_pending(dspi->dma_rx);
  572. dma_async_issue_pending(dspi->dma_tx);
  573. set_io_bits(dspi->base + SPIINT, SPIINT_DMA_REQ_EN);
  574. }
  575. /* Wait for the transfer to complete */
  576. if (spicfg->io_type != SPI_IO_TYPE_POLL) {
  577. if (wait_for_completion_timeout(&dspi->done, HZ) == 0)
  578. errors = SPIFLG_TIMEOUT_MASK;
  579. } else {
  580. while (dspi->rcount > 0 || dspi->wcount > 0) {
  581. errors = davinci_spi_process_events(dspi);
  582. if (errors)
  583. break;
  584. cpu_relax();
  585. }
  586. }
  587. clear_io_bits(dspi->base + SPIINT, SPIINT_MASKALL);
  588. if (davinci_spi_can_dma(spi->master, spi, t))
  589. clear_io_bits(dspi->base + SPIINT, SPIINT_DMA_REQ_EN);
  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(&spi->dev, "SPI data transfer error\n");
  604. return -EIO;
  605. }
  606. return t->len;
  607. err_desc:
  608. return ret;
  609. }
  610. /**
  611. * dummy_thread_fn - dummy thread function
  612. * @irq: IRQ number for this SPI Master
  613. * @context_data: structure for SPI Master controller davinci_spi
  614. *
  615. * This is to satisfy the request_threaded_irq() API so that the irq
  616. * handler is called in interrupt context.
  617. */
  618. static irqreturn_t dummy_thread_fn(s32 irq, void *data)
  619. {
  620. return IRQ_HANDLED;
  621. }
  622. /**
  623. * davinci_spi_irq - Interrupt handler for SPI Master Controller
  624. * @irq: IRQ number for this SPI Master
  625. * @context_data: structure for SPI Master controller davinci_spi
  626. *
  627. * ISR will determine that interrupt arrives either for READ or WRITE command.
  628. * According to command it will do the appropriate action. It will check
  629. * transfer length and if it is not zero then dispatch transfer command again.
  630. * If transfer length is zero then it will indicate the COMPLETION so that
  631. * davinci_spi_bufs function can go ahead.
  632. */
  633. static irqreturn_t davinci_spi_irq(s32 irq, void *data)
  634. {
  635. struct davinci_spi *dspi = data;
  636. int status;
  637. status = davinci_spi_process_events(dspi);
  638. if (unlikely(status != 0))
  639. clear_io_bits(dspi->base + SPIINT, SPIINT_MASKINT);
  640. if ((!dspi->rcount && !dspi->wcount) || status)
  641. complete(&dspi->done);
  642. return IRQ_HANDLED;
  643. }
  644. static int davinci_spi_request_dma(struct davinci_spi *dspi)
  645. {
  646. struct device *sdev = dspi->bitbang.master->dev.parent;
  647. dspi->dma_rx = dma_request_chan(sdev, "rx");
  648. if (IS_ERR(dspi->dma_rx))
  649. return PTR_ERR(dspi->dma_rx);
  650. dspi->dma_tx = dma_request_chan(sdev, "tx");
  651. if (IS_ERR(dspi->dma_tx)) {
  652. dma_release_channel(dspi->dma_rx);
  653. return PTR_ERR(dspi->dma_tx);
  654. }
  655. return 0;
  656. }
  657. #if defined(CONFIG_OF)
  658. /* OF SPI data structure */
  659. struct davinci_spi_of_data {
  660. u8 version;
  661. u8 prescaler_limit;
  662. };
  663. static const struct davinci_spi_of_data dm6441_spi_data = {
  664. .version = SPI_VERSION_1,
  665. .prescaler_limit = 2,
  666. };
  667. static const struct davinci_spi_of_data da830_spi_data = {
  668. .version = SPI_VERSION_2,
  669. .prescaler_limit = 2,
  670. };
  671. static const struct davinci_spi_of_data keystone_spi_data = {
  672. .version = SPI_VERSION_1,
  673. .prescaler_limit = 0,
  674. };
  675. static const struct of_device_id davinci_spi_of_match[] = {
  676. {
  677. .compatible = "ti,dm6441-spi",
  678. .data = &dm6441_spi_data,
  679. },
  680. {
  681. .compatible = "ti,da830-spi",
  682. .data = &da830_spi_data,
  683. },
  684. {
  685. .compatible = "ti,keystone-spi",
  686. .data = &keystone_spi_data,
  687. },
  688. { },
  689. };
  690. MODULE_DEVICE_TABLE(of, davinci_spi_of_match);
  691. /**
  692. * spi_davinci_get_pdata - Get platform data from DTS binding
  693. * @pdev: ptr to platform data
  694. * @dspi: ptr to driver data
  695. *
  696. * Parses and populates pdata in dspi from device tree bindings.
  697. *
  698. * NOTE: Not all platform data params are supported currently.
  699. */
  700. static int spi_davinci_get_pdata(struct platform_device *pdev,
  701. struct davinci_spi *dspi)
  702. {
  703. struct device_node *node = pdev->dev.of_node;
  704. struct davinci_spi_of_data *spi_data;
  705. struct davinci_spi_platform_data *pdata;
  706. unsigned int num_cs, intr_line = 0;
  707. const struct of_device_id *match;
  708. pdata = &dspi->pdata;
  709. match = of_match_device(davinci_spi_of_match, &pdev->dev);
  710. if (!match)
  711. return -ENODEV;
  712. spi_data = (struct davinci_spi_of_data *)match->data;
  713. pdata->version = spi_data->version;
  714. pdata->prescaler_limit = spi_data->prescaler_limit;
  715. /*
  716. * default num_cs is 1 and all chipsel are internal to the chip
  717. * indicated by chip_sel being NULL or cs_gpios being NULL or
  718. * set to -ENOENT. num-cs includes internal as well as gpios.
  719. * indicated by chip_sel being NULL. GPIO based CS is not
  720. * supported yet in DT bindings.
  721. */
  722. num_cs = 1;
  723. of_property_read_u32(node, "num-cs", &num_cs);
  724. pdata->num_chipselect = num_cs;
  725. of_property_read_u32(node, "ti,davinci-spi-intr-line", &intr_line);
  726. pdata->intr_line = intr_line;
  727. return 0;
  728. }
  729. #else
  730. static int spi_davinci_get_pdata(struct platform_device *pdev,
  731. struct davinci_spi *dspi)
  732. {
  733. return -ENODEV;
  734. }
  735. #endif
  736. /**
  737. * davinci_spi_probe - probe function for SPI Master Controller
  738. * @pdev: platform_device structure which contains plateform specific data
  739. *
  740. * According to Linux Device Model this function will be invoked by Linux
  741. * with platform_device struct which contains the device specific info.
  742. * This function will map the SPI controller's memory, register IRQ,
  743. * Reset SPI controller and setting its registers to default value.
  744. * It will invoke spi_bitbang_start to create work queue so that client driver
  745. * can register transfer method to work queue.
  746. */
  747. static int davinci_spi_probe(struct platform_device *pdev)
  748. {
  749. struct spi_master *master;
  750. struct davinci_spi *dspi;
  751. struct davinci_spi_platform_data *pdata;
  752. struct resource *r;
  753. int ret = 0;
  754. u32 spipc0;
  755. master = spi_alloc_master(&pdev->dev, sizeof(struct davinci_spi));
  756. if (master == NULL) {
  757. ret = -ENOMEM;
  758. goto err;
  759. }
  760. platform_set_drvdata(pdev, master);
  761. dspi = spi_master_get_devdata(master);
  762. if (dev_get_platdata(&pdev->dev)) {
  763. pdata = dev_get_platdata(&pdev->dev);
  764. dspi->pdata = *pdata;
  765. } else {
  766. /* update dspi pdata with that from the DT */
  767. ret = spi_davinci_get_pdata(pdev, dspi);
  768. if (ret < 0)
  769. goto free_master;
  770. }
  771. /* pdata in dspi is now updated and point pdata to that */
  772. pdata = &dspi->pdata;
  773. dspi->bytes_per_word = devm_kcalloc(&pdev->dev,
  774. pdata->num_chipselect,
  775. sizeof(*dspi->bytes_per_word),
  776. GFP_KERNEL);
  777. if (dspi->bytes_per_word == NULL) {
  778. ret = -ENOMEM;
  779. goto free_master;
  780. }
  781. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  782. if (r == NULL) {
  783. ret = -ENOENT;
  784. goto free_master;
  785. }
  786. dspi->pbase = r->start;
  787. dspi->base = devm_ioremap_resource(&pdev->dev, r);
  788. if (IS_ERR(dspi->base)) {
  789. ret = PTR_ERR(dspi->base);
  790. goto free_master;
  791. }
  792. init_completion(&dspi->done);
  793. ret = platform_get_irq(pdev, 0);
  794. if (ret == 0)
  795. ret = -EINVAL;
  796. if (ret < 0)
  797. goto free_master;
  798. dspi->irq = ret;
  799. ret = devm_request_threaded_irq(&pdev->dev, dspi->irq, davinci_spi_irq,
  800. dummy_thread_fn, 0, dev_name(&pdev->dev), dspi);
  801. if (ret)
  802. goto free_master;
  803. dspi->bitbang.master = master;
  804. dspi->clk = devm_clk_get(&pdev->dev, NULL);
  805. if (IS_ERR(dspi->clk)) {
  806. ret = -ENODEV;
  807. goto free_master;
  808. }
  809. ret = clk_prepare_enable(dspi->clk);
  810. if (ret)
  811. goto free_master;
  812. master->dev.of_node = pdev->dev.of_node;
  813. master->bus_num = pdev->id;
  814. master->num_chipselect = pdata->num_chipselect;
  815. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(2, 16);
  816. master->flags = SPI_MASTER_MUST_RX;
  817. master->setup = davinci_spi_setup;
  818. master->cleanup = davinci_spi_cleanup;
  819. master->can_dma = davinci_spi_can_dma;
  820. dspi->bitbang.chipselect = davinci_spi_chipselect;
  821. dspi->bitbang.setup_transfer = davinci_spi_setup_transfer;
  822. dspi->prescaler_limit = pdata->prescaler_limit;
  823. dspi->version = pdata->version;
  824. dspi->bitbang.flags = SPI_NO_CS | SPI_LSB_FIRST | SPI_LOOP;
  825. if (dspi->version == SPI_VERSION_2)
  826. dspi->bitbang.flags |= SPI_READY;
  827. if (pdev->dev.of_node) {
  828. int i;
  829. for (i = 0; i < pdata->num_chipselect; i++) {
  830. int cs_gpio = of_get_named_gpio(pdev->dev.of_node,
  831. "cs-gpios", i);
  832. if (cs_gpio == -EPROBE_DEFER) {
  833. ret = cs_gpio;
  834. goto free_clk;
  835. }
  836. if (gpio_is_valid(cs_gpio)) {
  837. ret = devm_gpio_request(&pdev->dev, cs_gpio,
  838. dev_name(&pdev->dev));
  839. if (ret)
  840. goto free_clk;
  841. }
  842. }
  843. }
  844. dspi->bitbang.txrx_bufs = davinci_spi_bufs;
  845. ret = davinci_spi_request_dma(dspi);
  846. if (ret == -EPROBE_DEFER) {
  847. goto free_clk;
  848. } else if (ret) {
  849. dev_info(&pdev->dev, "DMA is not supported (%d)\n", ret);
  850. dspi->dma_rx = NULL;
  851. dspi->dma_tx = NULL;
  852. }
  853. dspi->get_rx = davinci_spi_rx_buf_u8;
  854. dspi->get_tx = davinci_spi_tx_buf_u8;
  855. /* Reset In/OUT SPI module */
  856. iowrite32(0, dspi->base + SPIGCR0);
  857. udelay(100);
  858. iowrite32(1, dspi->base + SPIGCR0);
  859. /* Set up SPIPC0. CS and ENA init is done in davinci_spi_setup */
  860. spipc0 = SPIPC0_DIFUN_MASK | SPIPC0_DOFUN_MASK | SPIPC0_CLKFUN_MASK;
  861. iowrite32(spipc0, dspi->base + SPIPC0);
  862. if (pdata->intr_line)
  863. iowrite32(SPI_INTLVL_1, dspi->base + SPILVL);
  864. else
  865. iowrite32(SPI_INTLVL_0, dspi->base + SPILVL);
  866. iowrite32(CS_DEFAULT, dspi->base + SPIDEF);
  867. /* master mode default */
  868. set_io_bits(dspi->base + SPIGCR1, SPIGCR1_CLKMOD_MASK);
  869. set_io_bits(dspi->base + SPIGCR1, SPIGCR1_MASTER_MASK);
  870. set_io_bits(dspi->base + SPIGCR1, SPIGCR1_POWERDOWN_MASK);
  871. ret = spi_bitbang_start(&dspi->bitbang);
  872. if (ret)
  873. goto free_dma;
  874. dev_info(&pdev->dev, "Controller at 0x%p\n", dspi->base);
  875. return ret;
  876. free_dma:
  877. if (dspi->dma_rx) {
  878. dma_release_channel(dspi->dma_rx);
  879. dma_release_channel(dspi->dma_tx);
  880. }
  881. free_clk:
  882. clk_disable_unprepare(dspi->clk);
  883. free_master:
  884. spi_master_put(master);
  885. err:
  886. return ret;
  887. }
  888. /**
  889. * davinci_spi_remove - remove function for SPI Master Controller
  890. * @pdev: platform_device structure which contains plateform specific data
  891. *
  892. * This function will do the reverse action of davinci_spi_probe function
  893. * It will free the IRQ and SPI controller's memory region.
  894. * It will also call spi_bitbang_stop to destroy the work queue which was
  895. * created by spi_bitbang_start.
  896. */
  897. static int davinci_spi_remove(struct platform_device *pdev)
  898. {
  899. struct davinci_spi *dspi;
  900. struct spi_master *master;
  901. master = platform_get_drvdata(pdev);
  902. dspi = spi_master_get_devdata(master);
  903. spi_bitbang_stop(&dspi->bitbang);
  904. clk_disable_unprepare(dspi->clk);
  905. spi_master_put(master);
  906. if (dspi->dma_rx) {
  907. dma_release_channel(dspi->dma_rx);
  908. dma_release_channel(dspi->dma_tx);
  909. }
  910. return 0;
  911. }
  912. static struct platform_driver davinci_spi_driver = {
  913. .driver = {
  914. .name = "spi_davinci",
  915. .of_match_table = of_match_ptr(davinci_spi_of_match),
  916. },
  917. .probe = davinci_spi_probe,
  918. .remove = davinci_spi_remove,
  919. };
  920. module_platform_driver(davinci_spi_driver);
  921. MODULE_DESCRIPTION("TI DaVinci SPI Master Controller Driver");
  922. MODULE_LICENSE("GPL");