spi-davinci.c 27 KB

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