spi-davinci.c 29 KB

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