spi-davinci.c 28 KB

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