davinci_spi.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  1. /*
  2. * Copyright (C) 2009 Texas Instruments.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include <linux/interrupt.h>
  19. #include <linux/io.h>
  20. #include <linux/gpio.h>
  21. #include <linux/module.h>
  22. #include <linux/delay.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/err.h>
  25. #include <linux/clk.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/spi/spi.h>
  28. #include <linux/spi/spi_bitbang.h>
  29. #include <linux/slab.h>
  30. #include <mach/spi.h>
  31. #include <mach/edma.h>
  32. #define SPI_NO_RESOURCE ((resource_size_t)-1)
  33. #define SPI_MAX_CHIPSELECT 2
  34. #define CS_DEFAULT 0xFF
  35. #define SPI_BUFSIZ (SMP_CACHE_BYTES + 1)
  36. #define SPIFMT_PHASE_MASK BIT(16)
  37. #define SPIFMT_POLARITY_MASK BIT(17)
  38. #define SPIFMT_DISTIMER_MASK BIT(18)
  39. #define SPIFMT_SHIFTDIR_MASK BIT(20)
  40. #define SPIFMT_WAITENA_MASK BIT(21)
  41. #define SPIFMT_PARITYENA_MASK BIT(22)
  42. #define SPIFMT_ODD_PARITY_MASK BIT(23)
  43. #define SPIFMT_WDELAY_MASK 0x3f000000u
  44. #define SPIFMT_WDELAY_SHIFT 24
  45. #define SPIFMT_PRESCALE_SHIFT 8
  46. /* SPIPC0 */
  47. #define SPIPC0_DIFUN_MASK BIT(11) /* MISO */
  48. #define SPIPC0_DOFUN_MASK BIT(10) /* MOSI */
  49. #define SPIPC0_CLKFUN_MASK BIT(9) /* CLK */
  50. #define SPIPC0_SPIENA_MASK BIT(8) /* nREADY */
  51. #define SPIINT_MASKALL 0x0101035F
  52. #define SPI_INTLVL_1 0x000001FFu
  53. #define SPI_INTLVL_0 0x00000000u
  54. /* SPIDAT1 (upper 16 bit defines) */
  55. #define SPIDAT1_CSHOLD_MASK BIT(12)
  56. /* SPIGCR1 */
  57. #define SPIGCR1_CLKMOD_MASK BIT(1)
  58. #define SPIGCR1_MASTER_MASK BIT(0)
  59. #define SPIGCR1_LOOPBACK_MASK BIT(16)
  60. #define SPIGCR1_SPIENA_MASK BIT(24)
  61. /* SPIBUF */
  62. #define SPIBUF_TXFULL_MASK BIT(29)
  63. #define SPIBUF_RXEMPTY_MASK BIT(31)
  64. /* SPIDELAY */
  65. #define SPIDELAY_C2TDELAY_SHIFT 24
  66. #define SPIDELAY_C2TDELAY_MASK (0xFF << SPIDELAY_C2TDELAY_SHIFT)
  67. #define SPIDELAY_T2CDELAY_SHIFT 16
  68. #define SPIDELAY_T2CDELAY_MASK (0xFF << SPIDELAY_T2CDELAY_SHIFT)
  69. #define SPIDELAY_T2EDELAY_SHIFT 8
  70. #define SPIDELAY_T2EDELAY_MASK (0xFF << SPIDELAY_T2EDELAY_SHIFT)
  71. #define SPIDELAY_C2EDELAY_SHIFT 0
  72. #define SPIDELAY_C2EDELAY_MASK 0xFF
  73. /* Error Masks */
  74. #define SPIFLG_DLEN_ERR_MASK BIT(0)
  75. #define SPIFLG_TIMEOUT_MASK BIT(1)
  76. #define SPIFLG_PARERR_MASK BIT(2)
  77. #define SPIFLG_DESYNC_MASK BIT(3)
  78. #define SPIFLG_BITERR_MASK BIT(4)
  79. #define SPIFLG_OVRRUN_MASK BIT(6)
  80. #define SPIFLG_RX_INTR_MASK BIT(8)
  81. #define SPIFLG_TX_INTR_MASK BIT(9)
  82. #define SPIFLG_BUF_INIT_ACTIVE_MASK BIT(24)
  83. #define SPIINT_BITERR_INTR BIT(4)
  84. #define SPIINT_OVRRUN_INTR BIT(6)
  85. #define SPIINT_RX_INTR BIT(8)
  86. #define SPIINT_TX_INTR BIT(9)
  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. /* We have 2 DMA channels per CS, one for RX and one for TX */
  101. struct davinci_spi_dma {
  102. int dma_tx_channel;
  103. int dma_rx_channel;
  104. int dma_tx_sync_dev;
  105. int dma_rx_sync_dev;
  106. enum dma_event_q eventq;
  107. struct completion dma_tx_completion;
  108. struct completion dma_rx_completion;
  109. };
  110. /* SPI Controller driver's private data. */
  111. struct davinci_spi {
  112. struct spi_bitbang bitbang;
  113. struct clk *clk;
  114. u8 version;
  115. resource_size_t pbase;
  116. void __iomem *base;
  117. size_t region_size;
  118. u32 irq;
  119. struct completion done;
  120. const void *tx;
  121. void *rx;
  122. u8 *tmp_buf;
  123. int count;
  124. struct davinci_spi_dma *dma_channels;
  125. struct davinci_spi_platform_data *pdata;
  126. void (*get_rx)(u32 rx_data, struct davinci_spi *);
  127. u32 (*get_tx)(struct davinci_spi *);
  128. u8 bytes_per_word[SPI_MAX_CHIPSELECT];
  129. };
  130. static struct davinci_spi_config davinci_spi_default_cfg;
  131. static unsigned use_dma;
  132. static void davinci_spi_rx_buf_u8(u32 data, struct davinci_spi *davinci_spi)
  133. {
  134. if (davinci_spi->rx) {
  135. u8 *rx = davinci_spi->rx;
  136. *rx++ = (u8)data;
  137. davinci_spi->rx = rx;
  138. }
  139. }
  140. static void davinci_spi_rx_buf_u16(u32 data, struct davinci_spi *davinci_spi)
  141. {
  142. if (davinci_spi->rx) {
  143. u16 *rx = davinci_spi->rx;
  144. *rx++ = (u16)data;
  145. davinci_spi->rx = rx;
  146. }
  147. }
  148. static u32 davinci_spi_tx_buf_u8(struct davinci_spi *davinci_spi)
  149. {
  150. u32 data = 0;
  151. if (davinci_spi->tx) {
  152. const u8 *tx = davinci_spi->tx;
  153. data = *tx++;
  154. davinci_spi->tx = tx;
  155. }
  156. return data;
  157. }
  158. static u32 davinci_spi_tx_buf_u16(struct davinci_spi *davinci_spi)
  159. {
  160. u32 data = 0;
  161. if (davinci_spi->tx) {
  162. const u16 *tx = davinci_spi->tx;
  163. data = *tx++;
  164. davinci_spi->tx = tx;
  165. }
  166. return data;
  167. }
  168. static inline void set_io_bits(void __iomem *addr, u32 bits)
  169. {
  170. u32 v = ioread32(addr);
  171. v |= bits;
  172. iowrite32(v, addr);
  173. }
  174. static inline void clear_io_bits(void __iomem *addr, u32 bits)
  175. {
  176. u32 v = ioread32(addr);
  177. v &= ~bits;
  178. iowrite32(v, addr);
  179. }
  180. static void davinci_spi_set_dma_req(const struct spi_device *spi, int enable)
  181. {
  182. struct davinci_spi *davinci_spi = spi_master_get_devdata(spi->master);
  183. if (enable)
  184. set_io_bits(davinci_spi->base + SPIINT, SPIINT_DMA_REQ_EN);
  185. else
  186. clear_io_bits(davinci_spi->base + SPIINT, SPIINT_DMA_REQ_EN);
  187. }
  188. /*
  189. * Interface to control the chip select signal
  190. */
  191. static void davinci_spi_chipselect(struct spi_device *spi, int value)
  192. {
  193. struct davinci_spi *davinci_spi;
  194. struct davinci_spi_platform_data *pdata;
  195. u8 chip_sel = spi->chip_select;
  196. u16 spidat1_cfg = CS_DEFAULT;
  197. bool gpio_chipsel = false;
  198. davinci_spi = spi_master_get_devdata(spi->master);
  199. pdata = davinci_spi->pdata;
  200. if (pdata->chip_sel && chip_sel < pdata->num_chipselect &&
  201. pdata->chip_sel[chip_sel] != SPI_INTERN_CS)
  202. gpio_chipsel = true;
  203. /*
  204. * Board specific chip select logic decides the polarity and cs
  205. * line for the controller
  206. */
  207. if (gpio_chipsel) {
  208. if (value == BITBANG_CS_ACTIVE)
  209. gpio_set_value(pdata->chip_sel[chip_sel], 0);
  210. else
  211. gpio_set_value(pdata->chip_sel[chip_sel], 1);
  212. } else {
  213. if (value == BITBANG_CS_ACTIVE) {
  214. spidat1_cfg |= SPIDAT1_CSHOLD_MASK;
  215. spidat1_cfg &= ~(0x1 << chip_sel);
  216. }
  217. iowrite16(spidat1_cfg, davinci_spi->base + SPIDAT1 + 2);
  218. }
  219. }
  220. /**
  221. * davinci_spi_get_prescale - Calculates the correct prescale value
  222. * @maxspeed_hz: the maximum rate the SPI clock can run at
  223. *
  224. * This function calculates the prescale value that generates a clock rate
  225. * less than or equal to the specified maximum.
  226. *
  227. * Returns: calculated prescale - 1 for easy programming into SPI registers
  228. * or negative error number if valid prescalar cannot be updated.
  229. */
  230. static inline int davinci_spi_get_prescale(struct davinci_spi *davinci_spi,
  231. u32 max_speed_hz)
  232. {
  233. int ret;
  234. ret = DIV_ROUND_UP(clk_get_rate(davinci_spi->clk), max_speed_hz);
  235. if (ret < 3 || ret > 256)
  236. return -EINVAL;
  237. return ret - 1;
  238. }
  239. /**
  240. * davinci_spi_setup_transfer - This functions will determine transfer method
  241. * @spi: spi device on which data transfer to be done
  242. * @t: spi transfer in which transfer info is filled
  243. *
  244. * This function determines data transfer method (8/16/32 bit transfer).
  245. * It will also set the SPI Clock Control register according to
  246. * SPI slave device freq.
  247. */
  248. static int davinci_spi_setup_transfer(struct spi_device *spi,
  249. struct spi_transfer *t)
  250. {
  251. struct davinci_spi *davinci_spi;
  252. struct davinci_spi_config *spicfg;
  253. u8 bits_per_word = 0;
  254. u32 hz = 0, spifmt = 0, prescale = 0;
  255. davinci_spi = spi_master_get_devdata(spi->master);
  256. spicfg = (struct davinci_spi_config *)spi->controller_data;
  257. if (!spicfg)
  258. spicfg = &davinci_spi_default_cfg;
  259. if (t) {
  260. bits_per_word = t->bits_per_word;
  261. hz = t->speed_hz;
  262. }
  263. /* if bits_per_word is not set then set it default */
  264. if (!bits_per_word)
  265. bits_per_word = spi->bits_per_word;
  266. /*
  267. * Assign function pointer to appropriate transfer method
  268. * 8bit, 16bit or 32bit transfer
  269. */
  270. if (bits_per_word <= 8 && bits_per_word >= 2) {
  271. davinci_spi->get_rx = davinci_spi_rx_buf_u8;
  272. davinci_spi->get_tx = davinci_spi_tx_buf_u8;
  273. davinci_spi->bytes_per_word[spi->chip_select] = 1;
  274. } else if (bits_per_word <= 16 && bits_per_word >= 2) {
  275. davinci_spi->get_rx = davinci_spi_rx_buf_u16;
  276. davinci_spi->get_tx = davinci_spi_tx_buf_u16;
  277. davinci_spi->bytes_per_word[spi->chip_select] = 2;
  278. } else
  279. return -EINVAL;
  280. if (!hz)
  281. hz = spi->max_speed_hz;
  282. /* Set up SPIFMTn register, unique to this chipselect. */
  283. prescale = davinci_spi_get_prescale(davinci_spi, hz);
  284. if (prescale < 0)
  285. return prescale;
  286. spifmt = (prescale << SPIFMT_PRESCALE_SHIFT) | (bits_per_word & 0x1f);
  287. if (spi->mode & SPI_LSB_FIRST)
  288. spifmt |= SPIFMT_SHIFTDIR_MASK;
  289. if (spi->mode & SPI_CPOL)
  290. spifmt |= SPIFMT_POLARITY_MASK;
  291. if (!(spi->mode & SPI_CPHA))
  292. spifmt |= SPIFMT_PHASE_MASK;
  293. /*
  294. * Version 1 hardware supports two basic SPI modes:
  295. * - Standard SPI mode uses 4 pins, with chipselect
  296. * - 3 pin SPI is a 4 pin variant without CS (SPI_NO_CS)
  297. * (distinct from SPI_3WIRE, with just one data wire;
  298. * or similar variants without MOSI or without MISO)
  299. *
  300. * Version 2 hardware supports an optional handshaking signal,
  301. * so it can support two more modes:
  302. * - 5 pin SPI variant is standard SPI plus SPI_READY
  303. * - 4 pin with enable is (SPI_READY | SPI_NO_CS)
  304. */
  305. if (davinci_spi->version == SPI_VERSION_2) {
  306. u32 delay = 0;
  307. spifmt |= ((spicfg->wdelay << SPIFMT_WDELAY_SHIFT)
  308. & SPIFMT_WDELAY_MASK);
  309. if (spicfg->odd_parity)
  310. spifmt |= SPIFMT_ODD_PARITY_MASK;
  311. if (spicfg->parity_enable)
  312. spifmt |= SPIFMT_PARITYENA_MASK;
  313. if (spicfg->timer_disable) {
  314. spifmt |= SPIFMT_DISTIMER_MASK;
  315. } else {
  316. delay |= (spicfg->c2tdelay << SPIDELAY_C2TDELAY_SHIFT)
  317. & SPIDELAY_C2TDELAY_MASK;
  318. delay |= (spicfg->t2cdelay << SPIDELAY_T2CDELAY_SHIFT)
  319. & SPIDELAY_T2CDELAY_MASK;
  320. }
  321. if (spi->mode & SPI_READY) {
  322. spifmt |= SPIFMT_WAITENA_MASK;
  323. delay |= (spicfg->t2edelay << SPIDELAY_T2EDELAY_SHIFT)
  324. & SPIDELAY_T2EDELAY_MASK;
  325. delay |= (spicfg->c2edelay << SPIDELAY_C2EDELAY_SHIFT)
  326. & SPIDELAY_C2EDELAY_MASK;
  327. }
  328. iowrite32(delay, davinci_spi->base + SPIDELAY);
  329. }
  330. iowrite32(spifmt, davinci_spi->base + SPIFMT0);
  331. return 0;
  332. }
  333. static void davinci_spi_dma_rx_callback(unsigned lch, u16 ch_status, void *data)
  334. {
  335. struct spi_device *spi = (struct spi_device *)data;
  336. struct davinci_spi *davinci_spi;
  337. struct davinci_spi_dma *davinci_spi_dma;
  338. davinci_spi = spi_master_get_devdata(spi->master);
  339. davinci_spi_dma = &(davinci_spi->dma_channels[spi->chip_select]);
  340. if (ch_status == DMA_COMPLETE)
  341. edma_stop(davinci_spi_dma->dma_rx_channel);
  342. else
  343. edma_clean_channel(davinci_spi_dma->dma_rx_channel);
  344. complete(&davinci_spi_dma->dma_rx_completion);
  345. /* We must disable the DMA RX request */
  346. davinci_spi_set_dma_req(spi, 0);
  347. }
  348. static void davinci_spi_dma_tx_callback(unsigned lch, u16 ch_status, void *data)
  349. {
  350. struct spi_device *spi = (struct spi_device *)data;
  351. struct davinci_spi *davinci_spi;
  352. struct davinci_spi_dma *davinci_spi_dma;
  353. davinci_spi = spi_master_get_devdata(spi->master);
  354. davinci_spi_dma = &(davinci_spi->dma_channels[spi->chip_select]);
  355. if (ch_status == DMA_COMPLETE)
  356. edma_stop(davinci_spi_dma->dma_tx_channel);
  357. else
  358. edma_clean_channel(davinci_spi_dma->dma_tx_channel);
  359. complete(&davinci_spi_dma->dma_tx_completion);
  360. /* We must disable the DMA TX request */
  361. davinci_spi_set_dma_req(spi, 0);
  362. }
  363. static int davinci_spi_request_dma(struct spi_device *spi)
  364. {
  365. struct davinci_spi *davinci_spi;
  366. struct davinci_spi_dma *davinci_spi_dma;
  367. struct device *sdev;
  368. int r;
  369. davinci_spi = spi_master_get_devdata(spi->master);
  370. davinci_spi_dma = &davinci_spi->dma_channels[spi->chip_select];
  371. sdev = davinci_spi->bitbang.master->dev.parent;
  372. r = edma_alloc_channel(davinci_spi_dma->dma_rx_sync_dev,
  373. davinci_spi_dma_rx_callback, spi,
  374. davinci_spi_dma->eventq);
  375. if (r < 0) {
  376. dev_dbg(sdev, "Unable to request DMA channel for SPI RX\n");
  377. return -EAGAIN;
  378. }
  379. davinci_spi_dma->dma_rx_channel = r;
  380. r = edma_alloc_channel(davinci_spi_dma->dma_tx_sync_dev,
  381. davinci_spi_dma_tx_callback, spi,
  382. davinci_spi_dma->eventq);
  383. if (r < 0) {
  384. edma_free_channel(davinci_spi_dma->dma_rx_channel);
  385. davinci_spi_dma->dma_rx_channel = -1;
  386. dev_dbg(sdev, "Unable to request DMA channel for SPI TX\n");
  387. return -EAGAIN;
  388. }
  389. davinci_spi_dma->dma_tx_channel = r;
  390. return 0;
  391. }
  392. /**
  393. * davinci_spi_setup - This functions will set default transfer method
  394. * @spi: spi device on which data transfer to be done
  395. *
  396. * This functions sets the default transfer method.
  397. */
  398. static int davinci_spi_setup(struct spi_device *spi)
  399. {
  400. int retval;
  401. struct davinci_spi *davinci_spi;
  402. struct davinci_spi_dma *davinci_spi_dma;
  403. davinci_spi = spi_master_get_devdata(spi->master);
  404. /* if bits per word length is zero then set it default 8 */
  405. if (!spi->bits_per_word)
  406. spi->bits_per_word = 8;
  407. if (use_dma && davinci_spi->dma_channels) {
  408. davinci_spi_dma = &davinci_spi->dma_channels[spi->chip_select];
  409. if ((davinci_spi_dma->dma_rx_channel == -1)
  410. || (davinci_spi_dma->dma_tx_channel == -1)) {
  411. retval = davinci_spi_request_dma(spi);
  412. if (retval < 0)
  413. return retval;
  414. }
  415. }
  416. retval = davinci_spi_setup_transfer(spi, NULL);
  417. return retval;
  418. }
  419. static void davinci_spi_cleanup(struct spi_device *spi)
  420. {
  421. struct davinci_spi *davinci_spi = spi_master_get_devdata(spi->master);
  422. struct davinci_spi_dma *davinci_spi_dma;
  423. davinci_spi_dma = &davinci_spi->dma_channels[spi->chip_select];
  424. if (use_dma && davinci_spi->dma_channels) {
  425. davinci_spi_dma = &davinci_spi->dma_channels[spi->chip_select];
  426. if ((davinci_spi_dma->dma_rx_channel != -1)
  427. && (davinci_spi_dma->dma_tx_channel != -1)) {
  428. edma_free_channel(davinci_spi_dma->dma_tx_channel);
  429. edma_free_channel(davinci_spi_dma->dma_rx_channel);
  430. }
  431. }
  432. }
  433. static int davinci_spi_bufs_prep(struct spi_device *spi,
  434. struct davinci_spi *davinci_spi)
  435. {
  436. struct davinci_spi_platform_data *pdata;
  437. int op_mode = 0;
  438. /*
  439. * REVISIT unless devices disagree about SPI_LOOP or
  440. * SPI_READY (SPI_NO_CS only allows one device!), this
  441. * should not need to be done before each message...
  442. * optimize for both flags staying cleared.
  443. */
  444. op_mode = SPIPC0_DIFUN_MASK
  445. | SPIPC0_DOFUN_MASK
  446. | SPIPC0_CLKFUN_MASK;
  447. if (!(spi->mode & SPI_NO_CS)) {
  448. pdata = davinci_spi->pdata;
  449. if (!pdata->chip_sel ||
  450. pdata->chip_sel[spi->chip_select] == SPI_INTERN_CS)
  451. op_mode |= 1 << spi->chip_select;
  452. }
  453. if (spi->mode & SPI_READY)
  454. op_mode |= SPIPC0_SPIENA_MASK;
  455. iowrite32(op_mode, davinci_spi->base + SPIPC0);
  456. if (spi->mode & SPI_LOOP)
  457. set_io_bits(davinci_spi->base + SPIGCR1,
  458. SPIGCR1_LOOPBACK_MASK);
  459. else
  460. clear_io_bits(davinci_spi->base + SPIGCR1,
  461. SPIGCR1_LOOPBACK_MASK);
  462. return 0;
  463. }
  464. static int davinci_spi_check_error(struct davinci_spi *davinci_spi,
  465. int int_status)
  466. {
  467. struct device *sdev = davinci_spi->bitbang.master->dev.parent;
  468. if (int_status & SPIFLG_TIMEOUT_MASK) {
  469. dev_dbg(sdev, "SPI Time-out Error\n");
  470. return -ETIMEDOUT;
  471. }
  472. if (int_status & SPIFLG_DESYNC_MASK) {
  473. dev_dbg(sdev, "SPI Desynchronization Error\n");
  474. return -EIO;
  475. }
  476. if (int_status & SPIFLG_BITERR_MASK) {
  477. dev_dbg(sdev, "SPI Bit error\n");
  478. return -EIO;
  479. }
  480. if (davinci_spi->version == SPI_VERSION_2) {
  481. if (int_status & SPIFLG_DLEN_ERR_MASK) {
  482. dev_dbg(sdev, "SPI Data Length Error\n");
  483. return -EIO;
  484. }
  485. if (int_status & SPIFLG_PARERR_MASK) {
  486. dev_dbg(sdev, "SPI Parity Error\n");
  487. return -EIO;
  488. }
  489. if (int_status & SPIFLG_OVRRUN_MASK) {
  490. dev_dbg(sdev, "SPI Data Overrun error\n");
  491. return -EIO;
  492. }
  493. if (int_status & SPIFLG_TX_INTR_MASK) {
  494. dev_dbg(sdev, "SPI TX intr bit set\n");
  495. return -EIO;
  496. }
  497. if (int_status & SPIFLG_BUF_INIT_ACTIVE_MASK) {
  498. dev_dbg(sdev, "SPI Buffer Init Active\n");
  499. return -EBUSY;
  500. }
  501. }
  502. return 0;
  503. }
  504. /**
  505. * davinci_spi_bufs - functions which will handle transfer data
  506. * @spi: spi device on which data transfer to be done
  507. * @t: spi transfer in which transfer info is filled
  508. *
  509. * This function will put data to be transferred into data register
  510. * of SPI controller and then wait until the completion will be marked
  511. * by the IRQ Handler.
  512. */
  513. static int davinci_spi_bufs_pio(struct spi_device *spi, struct spi_transfer *t)
  514. {
  515. struct davinci_spi *davinci_spi;
  516. int int_status, count, ret;
  517. u8 conv;
  518. u32 tx_data, data1_reg_val;
  519. u32 buf_val, flg_val;
  520. struct davinci_spi_platform_data *pdata;
  521. davinci_spi = spi_master_get_devdata(spi->master);
  522. pdata = davinci_spi->pdata;
  523. davinci_spi->tx = t->tx_buf;
  524. davinci_spi->rx = t->rx_buf;
  525. /* convert len to words based on bits_per_word */
  526. conv = davinci_spi->bytes_per_word[spi->chip_select];
  527. davinci_spi->count = t->len / conv;
  528. data1_reg_val = ioread32(davinci_spi->base + SPIDAT1);
  529. INIT_COMPLETION(davinci_spi->done);
  530. ret = davinci_spi_bufs_prep(spi, davinci_spi);
  531. if (ret)
  532. return ret;
  533. /* Enable SPI */
  534. set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
  535. count = davinci_spi->count;
  536. /* Determine the command to execute READ or WRITE */
  537. if (t->tx_buf) {
  538. clear_io_bits(davinci_spi->base + SPIINT, SPIINT_MASKALL);
  539. while (1) {
  540. tx_data = davinci_spi->get_tx(davinci_spi);
  541. data1_reg_val &= ~(0xFFFF);
  542. data1_reg_val |= (0xFFFF & tx_data);
  543. buf_val = ioread32(davinci_spi->base + SPIBUF);
  544. if ((buf_val & SPIBUF_TXFULL_MASK) == 0) {
  545. iowrite32(data1_reg_val,
  546. davinci_spi->base + SPIDAT1);
  547. count--;
  548. }
  549. while (ioread32(davinci_spi->base + SPIBUF)
  550. & SPIBUF_RXEMPTY_MASK)
  551. cpu_relax();
  552. /* getting the returned byte */
  553. if (t->rx_buf) {
  554. buf_val = ioread32(davinci_spi->base + SPIBUF);
  555. davinci_spi->get_rx(buf_val, davinci_spi);
  556. }
  557. if (count <= 0)
  558. break;
  559. }
  560. } else {
  561. if (pdata->poll_mode) {
  562. while (1) {
  563. /* keeps the serial clock going */
  564. if ((ioread32(davinci_spi->base + SPIBUF)
  565. & SPIBUF_TXFULL_MASK) == 0)
  566. iowrite32(data1_reg_val,
  567. davinci_spi->base + SPIDAT1);
  568. while (ioread32(davinci_spi->base + SPIBUF) &
  569. SPIBUF_RXEMPTY_MASK)
  570. cpu_relax();
  571. flg_val = ioread32(davinci_spi->base + SPIFLG);
  572. buf_val = ioread32(davinci_spi->base + SPIBUF);
  573. davinci_spi->get_rx(buf_val, davinci_spi);
  574. count--;
  575. if (count <= 0)
  576. break;
  577. }
  578. } else { /* Receive in Interrupt mode */
  579. int i;
  580. for (i = 0; i < davinci_spi->count; i++) {
  581. set_io_bits(davinci_spi->base + SPIINT,
  582. SPIINT_BITERR_INTR
  583. | SPIINT_OVRRUN_INTR
  584. | SPIINT_RX_INTR);
  585. iowrite32(data1_reg_val,
  586. davinci_spi->base + SPIDAT1);
  587. while (ioread32(davinci_spi->base + SPIINT) &
  588. SPIINT_RX_INTR)
  589. cpu_relax();
  590. }
  591. iowrite32((data1_reg_val & 0x0ffcffff),
  592. davinci_spi->base + SPIDAT1);
  593. }
  594. }
  595. /*
  596. * Check for bit error, desync error,parity error,timeout error and
  597. * receive overflow errors
  598. */
  599. int_status = ioread32(davinci_spi->base + SPIFLG);
  600. ret = davinci_spi_check_error(davinci_spi, int_status);
  601. if (ret != 0)
  602. return ret;
  603. return t->len;
  604. }
  605. static int davinci_spi_bufs_dma(struct spi_device *spi, struct spi_transfer *t)
  606. {
  607. struct davinci_spi *davinci_spi;
  608. int int_status = 0;
  609. int count, temp_count;
  610. u32 data1_reg_val;
  611. struct davinci_spi_dma *davinci_spi_dma;
  612. int data_type, ret;
  613. unsigned long tx_reg, rx_reg;
  614. struct device *sdev;
  615. davinci_spi = spi_master_get_devdata(spi->master);
  616. sdev = davinci_spi->bitbang.master->dev.parent;
  617. davinci_spi_dma = &davinci_spi->dma_channels[spi->chip_select];
  618. tx_reg = (unsigned long)davinci_spi->pbase + SPIDAT1;
  619. rx_reg = (unsigned long)davinci_spi->pbase + SPIBUF;
  620. davinci_spi->tx = t->tx_buf;
  621. davinci_spi->rx = t->rx_buf;
  622. /* convert len to words based on bits_per_word */
  623. data_type = davinci_spi->bytes_per_word[spi->chip_select];
  624. davinci_spi->count = t->len / data_type;
  625. data1_reg_val = ioread32(davinci_spi->base + SPIDAT1);
  626. INIT_COMPLETION(davinci_spi->done);
  627. init_completion(&davinci_spi_dma->dma_rx_completion);
  628. init_completion(&davinci_spi_dma->dma_tx_completion);
  629. ret = davinci_spi_bufs_prep(spi, davinci_spi);
  630. if (ret)
  631. return ret;
  632. count = davinci_spi->count; /* the number of elements */
  633. /* disable all interrupts for dma transfers */
  634. clear_io_bits(davinci_spi->base + SPIINT, SPIINT_MASKALL);
  635. /* Enable SPI */
  636. set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
  637. if (t->tx_buf) {
  638. t->tx_dma = dma_map_single(&spi->dev, (void *)t->tx_buf, count,
  639. DMA_TO_DEVICE);
  640. if (dma_mapping_error(&spi->dev, t->tx_dma)) {
  641. dev_dbg(sdev, "Unable to DMA map a %d bytes"
  642. " TX buffer\n", count);
  643. return -ENOMEM;
  644. }
  645. temp_count = count;
  646. } else {
  647. /* We need TX clocking for RX transaction */
  648. t->tx_dma = dma_map_single(&spi->dev,
  649. (void *)davinci_spi->tmp_buf, count + 1,
  650. DMA_TO_DEVICE);
  651. if (dma_mapping_error(&spi->dev, t->tx_dma)) {
  652. dev_dbg(sdev, "Unable to DMA map a %d bytes"
  653. " TX tmp buffer\n", count);
  654. return -ENOMEM;
  655. }
  656. temp_count = count + 1;
  657. }
  658. edma_set_transfer_params(davinci_spi_dma->dma_tx_channel,
  659. data_type, temp_count, 1, 0, ASYNC);
  660. edma_set_dest(davinci_spi_dma->dma_tx_channel, tx_reg, INCR, W8BIT);
  661. edma_set_src(davinci_spi_dma->dma_tx_channel, t->tx_dma, INCR, W8BIT);
  662. edma_set_src_index(davinci_spi_dma->dma_tx_channel, data_type, 0);
  663. edma_set_dest_index(davinci_spi_dma->dma_tx_channel, 0, 0);
  664. if (t->rx_buf) {
  665. /* initiate transaction */
  666. iowrite32(data1_reg_val, davinci_spi->base + SPIDAT1);
  667. t->rx_dma = dma_map_single(&spi->dev, (void *)t->rx_buf, count,
  668. DMA_FROM_DEVICE);
  669. if (dma_mapping_error(&spi->dev, t->rx_dma)) {
  670. dev_dbg(sdev, "Couldn't DMA map a %d bytes RX buffer\n",
  671. count);
  672. if (t->tx_buf != NULL)
  673. dma_unmap_single(NULL, t->tx_dma,
  674. count, DMA_TO_DEVICE);
  675. return -ENOMEM;
  676. }
  677. edma_set_transfer_params(davinci_spi_dma->dma_rx_channel,
  678. data_type, count, 1, 0, ASYNC);
  679. edma_set_src(davinci_spi_dma->dma_rx_channel,
  680. rx_reg, INCR, W8BIT);
  681. edma_set_dest(davinci_spi_dma->dma_rx_channel,
  682. t->rx_dma, INCR, W8BIT);
  683. edma_set_src_index(davinci_spi_dma->dma_rx_channel, 0, 0);
  684. edma_set_dest_index(davinci_spi_dma->dma_rx_channel,
  685. data_type, 0);
  686. }
  687. if ((t->tx_buf) || (t->rx_buf))
  688. edma_start(davinci_spi_dma->dma_tx_channel);
  689. if (t->rx_buf)
  690. edma_start(davinci_spi_dma->dma_rx_channel);
  691. if ((t->rx_buf) || (t->tx_buf))
  692. davinci_spi_set_dma_req(spi, 1);
  693. if (t->tx_buf)
  694. wait_for_completion_interruptible(
  695. &davinci_spi_dma->dma_tx_completion);
  696. if (t->rx_buf)
  697. wait_for_completion_interruptible(
  698. &davinci_spi_dma->dma_rx_completion);
  699. dma_unmap_single(NULL, t->tx_dma, temp_count, DMA_TO_DEVICE);
  700. if (t->rx_buf)
  701. dma_unmap_single(NULL, t->rx_dma, count, DMA_FROM_DEVICE);
  702. /*
  703. * Check for bit error, desync error,parity error,timeout error and
  704. * receive overflow errors
  705. */
  706. int_status = ioread32(davinci_spi->base + SPIFLG);
  707. ret = davinci_spi_check_error(davinci_spi, int_status);
  708. if (ret != 0)
  709. return ret;
  710. return t->len;
  711. }
  712. /**
  713. * davinci_spi_irq - IRQ handler for DaVinci SPI
  714. * @irq: IRQ number for this SPI Master
  715. * @context_data: structure for SPI Master controller davinci_spi
  716. */
  717. static irqreturn_t davinci_spi_irq(s32 irq, void *context_data)
  718. {
  719. struct davinci_spi *davinci_spi = context_data;
  720. u32 int_status, rx_data = 0;
  721. irqreturn_t ret = IRQ_NONE;
  722. int_status = ioread32(davinci_spi->base + SPIFLG);
  723. while ((int_status & SPIFLG_RX_INTR_MASK)) {
  724. if (likely(int_status & SPIFLG_RX_INTR_MASK)) {
  725. ret = IRQ_HANDLED;
  726. rx_data = ioread32(davinci_spi->base + SPIBUF);
  727. davinci_spi->get_rx(rx_data, davinci_spi);
  728. /* Disable Receive Interrupt */
  729. iowrite32(~(SPIINT_RX_INTR | SPIINT_TX_INTR),
  730. davinci_spi->base + SPIINT);
  731. } else
  732. (void)davinci_spi_check_error(davinci_spi, int_status);
  733. int_status = ioread32(davinci_spi->base + SPIFLG);
  734. }
  735. return ret;
  736. }
  737. /**
  738. * davinci_spi_probe - probe function for SPI Master Controller
  739. * @pdev: platform_device structure which contains plateform specific data
  740. */
  741. static int davinci_spi_probe(struct platform_device *pdev)
  742. {
  743. struct spi_master *master;
  744. struct davinci_spi *davinci_spi;
  745. struct davinci_spi_platform_data *pdata;
  746. struct resource *r, *mem;
  747. resource_size_t dma_rx_chan = SPI_NO_RESOURCE;
  748. resource_size_t dma_tx_chan = SPI_NO_RESOURCE;
  749. resource_size_t dma_eventq = SPI_NO_RESOURCE;
  750. int i = 0, ret = 0;
  751. pdata = pdev->dev.platform_data;
  752. if (pdata == NULL) {
  753. ret = -ENODEV;
  754. goto err;
  755. }
  756. master = spi_alloc_master(&pdev->dev, sizeof(struct davinci_spi));
  757. if (master == NULL) {
  758. ret = -ENOMEM;
  759. goto err;
  760. }
  761. dev_set_drvdata(&pdev->dev, master);
  762. davinci_spi = spi_master_get_devdata(master);
  763. if (davinci_spi == NULL) {
  764. ret = -ENOENT;
  765. goto free_master;
  766. }
  767. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  768. if (r == NULL) {
  769. ret = -ENOENT;
  770. goto free_master;
  771. }
  772. davinci_spi->pbase = r->start;
  773. davinci_spi->region_size = resource_size(r);
  774. davinci_spi->pdata = pdata;
  775. mem = request_mem_region(r->start, davinci_spi->region_size,
  776. pdev->name);
  777. if (mem == NULL) {
  778. ret = -EBUSY;
  779. goto free_master;
  780. }
  781. davinci_spi->base = ioremap(r->start, davinci_spi->region_size);
  782. if (davinci_spi->base == NULL) {
  783. ret = -ENOMEM;
  784. goto release_region;
  785. }
  786. davinci_spi->irq = platform_get_irq(pdev, 0);
  787. if (davinci_spi->irq <= 0) {
  788. ret = -EINVAL;
  789. goto unmap_io;
  790. }
  791. ret = request_irq(davinci_spi->irq, davinci_spi_irq, IRQF_DISABLED,
  792. dev_name(&pdev->dev), davinci_spi);
  793. if (ret)
  794. goto unmap_io;
  795. /* Allocate tmp_buf for tx_buf */
  796. davinci_spi->tmp_buf = kzalloc(SPI_BUFSIZ, GFP_KERNEL);
  797. if (davinci_spi->tmp_buf == NULL) {
  798. ret = -ENOMEM;
  799. goto irq_free;
  800. }
  801. davinci_spi->bitbang.master = spi_master_get(master);
  802. if (davinci_spi->bitbang.master == NULL) {
  803. ret = -ENODEV;
  804. goto free_tmp_buf;
  805. }
  806. davinci_spi->clk = clk_get(&pdev->dev, NULL);
  807. if (IS_ERR(davinci_spi->clk)) {
  808. ret = -ENODEV;
  809. goto put_master;
  810. }
  811. clk_enable(davinci_spi->clk);
  812. master->bus_num = pdev->id;
  813. master->num_chipselect = pdata->num_chipselect;
  814. master->setup = davinci_spi_setup;
  815. master->cleanup = davinci_spi_cleanup;
  816. davinci_spi->bitbang.chipselect = davinci_spi_chipselect;
  817. davinci_spi->bitbang.setup_transfer = davinci_spi_setup_transfer;
  818. davinci_spi->version = pdata->version;
  819. use_dma = pdata->use_dma;
  820. davinci_spi->bitbang.flags = SPI_NO_CS | SPI_LSB_FIRST | SPI_LOOP;
  821. if (davinci_spi->version == SPI_VERSION_2)
  822. davinci_spi->bitbang.flags |= SPI_READY;
  823. if (use_dma) {
  824. r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  825. if (r)
  826. dma_rx_chan = r->start;
  827. r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  828. if (r)
  829. dma_tx_chan = r->start;
  830. r = platform_get_resource(pdev, IORESOURCE_DMA, 2);
  831. if (r)
  832. dma_eventq = r->start;
  833. }
  834. if (!use_dma ||
  835. dma_rx_chan == SPI_NO_RESOURCE ||
  836. dma_tx_chan == SPI_NO_RESOURCE ||
  837. dma_eventq == SPI_NO_RESOURCE) {
  838. davinci_spi->bitbang.txrx_bufs = davinci_spi_bufs_pio;
  839. use_dma = 0;
  840. } else {
  841. davinci_spi->bitbang.txrx_bufs = davinci_spi_bufs_dma;
  842. davinci_spi->dma_channels = kzalloc(master->num_chipselect
  843. * sizeof(struct davinci_spi_dma), GFP_KERNEL);
  844. if (davinci_spi->dma_channels == NULL) {
  845. ret = -ENOMEM;
  846. goto free_clk;
  847. }
  848. for (i = 0; i < master->num_chipselect; i++) {
  849. davinci_spi->dma_channels[i].dma_rx_channel = -1;
  850. davinci_spi->dma_channels[i].dma_rx_sync_dev =
  851. dma_rx_chan;
  852. davinci_spi->dma_channels[i].dma_tx_channel = -1;
  853. davinci_spi->dma_channels[i].dma_tx_sync_dev =
  854. dma_tx_chan;
  855. davinci_spi->dma_channels[i].eventq = dma_eventq;
  856. }
  857. dev_info(&pdev->dev, "DaVinci SPI driver in EDMA mode\n"
  858. "Using RX channel = %d , TX channel = %d and "
  859. "event queue = %d", dma_rx_chan, dma_tx_chan,
  860. dma_eventq);
  861. }
  862. davinci_spi->get_rx = davinci_spi_rx_buf_u8;
  863. davinci_spi->get_tx = davinci_spi_tx_buf_u8;
  864. init_completion(&davinci_spi->done);
  865. /* Reset In/OUT SPI module */
  866. iowrite32(0, davinci_spi->base + SPIGCR0);
  867. udelay(100);
  868. iowrite32(1, davinci_spi->base + SPIGCR0);
  869. /* initialize chip selects */
  870. if (pdata->chip_sel) {
  871. for (i = 0; i < pdata->num_chipselect; i++) {
  872. if (pdata->chip_sel[i] != SPI_INTERN_CS)
  873. gpio_direction_output(pdata->chip_sel[i], 1);
  874. }
  875. }
  876. /* Clock internal */
  877. if (davinci_spi->pdata->clk_internal)
  878. set_io_bits(davinci_spi->base + SPIGCR1,
  879. SPIGCR1_CLKMOD_MASK);
  880. else
  881. clear_io_bits(davinci_spi->base + SPIGCR1,
  882. SPIGCR1_CLKMOD_MASK);
  883. iowrite32(CS_DEFAULT, davinci_spi->base + SPIDEF);
  884. /* master mode default */
  885. set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_MASTER_MASK);
  886. if (davinci_spi->pdata->intr_level)
  887. iowrite32(SPI_INTLVL_1, davinci_spi->base + SPILVL);
  888. else
  889. iowrite32(SPI_INTLVL_0, davinci_spi->base + SPILVL);
  890. ret = spi_bitbang_start(&davinci_spi->bitbang);
  891. if (ret)
  892. goto free_clk;
  893. dev_info(&pdev->dev, "Controller at 0x%p\n", davinci_spi->base);
  894. if (!pdata->poll_mode)
  895. dev_info(&pdev->dev, "Operating in interrupt mode"
  896. " using IRQ %d\n", davinci_spi->irq);
  897. return ret;
  898. free_clk:
  899. clk_disable(davinci_spi->clk);
  900. clk_put(davinci_spi->clk);
  901. put_master:
  902. spi_master_put(master);
  903. free_tmp_buf:
  904. kfree(davinci_spi->tmp_buf);
  905. irq_free:
  906. free_irq(davinci_spi->irq, davinci_spi);
  907. unmap_io:
  908. iounmap(davinci_spi->base);
  909. release_region:
  910. release_mem_region(davinci_spi->pbase, davinci_spi->region_size);
  911. free_master:
  912. kfree(master);
  913. err:
  914. return ret;
  915. }
  916. /**
  917. * davinci_spi_remove - remove function for SPI Master Controller
  918. * @pdev: platform_device structure which contains plateform specific data
  919. *
  920. * This function will do the reverse action of davinci_spi_probe function
  921. * It will free the IRQ and SPI controller's memory region.
  922. * It will also call spi_bitbang_stop to destroy the work queue which was
  923. * created by spi_bitbang_start.
  924. */
  925. static int __exit davinci_spi_remove(struct platform_device *pdev)
  926. {
  927. struct davinci_spi *davinci_spi;
  928. struct spi_master *master;
  929. master = dev_get_drvdata(&pdev->dev);
  930. davinci_spi = spi_master_get_devdata(master);
  931. spi_bitbang_stop(&davinci_spi->bitbang);
  932. clk_disable(davinci_spi->clk);
  933. clk_put(davinci_spi->clk);
  934. spi_master_put(master);
  935. kfree(davinci_spi->tmp_buf);
  936. free_irq(davinci_spi->irq, davinci_spi);
  937. iounmap(davinci_spi->base);
  938. release_mem_region(davinci_spi->pbase, davinci_spi->region_size);
  939. return 0;
  940. }
  941. static struct platform_driver davinci_spi_driver = {
  942. .driver.name = "spi_davinci",
  943. .remove = __exit_p(davinci_spi_remove),
  944. };
  945. static int __init davinci_spi_init(void)
  946. {
  947. return platform_driver_probe(&davinci_spi_driver, davinci_spi_probe);
  948. }
  949. module_init(davinci_spi_init);
  950. static void __exit davinci_spi_exit(void)
  951. {
  952. platform_driver_unregister(&davinci_spi_driver);
  953. }
  954. module_exit(davinci_spi_exit);
  955. MODULE_DESCRIPTION("TI DaVinci SPI Master Controller Driver");
  956. MODULE_LICENSE("GPL");