spi-davinci.c 27 KB

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