davinci_spi.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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 SPIINT_MASKINT 0x0000015F
  53. #define SPI_INTLVL_1 0x000001FF
  54. #define SPI_INTLVL_0 0x00000000
  55. /* SPIDAT1 (upper 16 bit defines) */
  56. #define SPIDAT1_CSHOLD_MASK BIT(12)
  57. /* SPIGCR1 */
  58. #define SPIGCR1_CLKMOD_MASK BIT(1)
  59. #define SPIGCR1_MASTER_MASK BIT(0)
  60. #define SPIGCR1_LOOPBACK_MASK BIT(16)
  61. #define SPIGCR1_SPIENA_MASK BIT(24)
  62. /* SPIBUF */
  63. #define SPIBUF_TXFULL_MASK BIT(29)
  64. #define SPIBUF_RXEMPTY_MASK BIT(31)
  65. /* SPIDELAY */
  66. #define SPIDELAY_C2TDELAY_SHIFT 24
  67. #define SPIDELAY_C2TDELAY_MASK (0xFF << SPIDELAY_C2TDELAY_SHIFT)
  68. #define SPIDELAY_T2CDELAY_SHIFT 16
  69. #define SPIDELAY_T2CDELAY_MASK (0xFF << SPIDELAY_T2CDELAY_SHIFT)
  70. #define SPIDELAY_T2EDELAY_SHIFT 8
  71. #define SPIDELAY_T2EDELAY_MASK (0xFF << SPIDELAY_T2EDELAY_SHIFT)
  72. #define SPIDELAY_C2EDELAY_SHIFT 0
  73. #define SPIDELAY_C2EDELAY_MASK 0xFF
  74. /* Error Masks */
  75. #define SPIFLG_DLEN_ERR_MASK BIT(0)
  76. #define SPIFLG_TIMEOUT_MASK BIT(1)
  77. #define SPIFLG_PARERR_MASK BIT(2)
  78. #define SPIFLG_DESYNC_MASK BIT(3)
  79. #define SPIFLG_BITERR_MASK BIT(4)
  80. #define SPIFLG_OVRRUN_MASK BIT(6)
  81. #define SPIFLG_BUF_INIT_ACTIVE_MASK BIT(24)
  82. #define SPIFLG_ERROR_MASK (SPIFLG_DLEN_ERR_MASK \
  83. | SPIFLG_TIMEOUT_MASK | SPIFLG_PARERR_MASK \
  84. | SPIFLG_DESYNC_MASK | SPIFLG_BITERR_MASK \
  85. | SPIFLG_OVRRUN_MASK)
  86. #define SPIINT_DMA_REQ_EN BIT(16)
  87. /* SPI Controller registers */
  88. #define SPIGCR0 0x00
  89. #define SPIGCR1 0x04
  90. #define SPIINT 0x08
  91. #define SPILVL 0x0c
  92. #define SPIFLG 0x10
  93. #define SPIPC0 0x14
  94. #define SPIDAT1 0x3c
  95. #define SPIBUF 0x40
  96. #define SPIDELAY 0x48
  97. #define SPIDEF 0x4c
  98. #define SPIFMT0 0x50
  99. /* We have 2 DMA channels per CS, one for RX and one for TX */
  100. struct davinci_spi_dma {
  101. int dma_tx_channel;
  102. int dma_rx_channel;
  103. int dma_tx_sync_dev;
  104. int dma_rx_sync_dev;
  105. enum dma_event_q eventq;
  106. struct completion dma_tx_completion;
  107. struct completion dma_rx_completion;
  108. };
  109. /* SPI Controller driver's private data. */
  110. struct davinci_spi {
  111. struct spi_bitbang bitbang;
  112. struct clk *clk;
  113. u8 version;
  114. resource_size_t pbase;
  115. void __iomem *base;
  116. size_t region_size;
  117. u32 irq;
  118. struct completion done;
  119. const void *tx;
  120. void *rx;
  121. u8 *tmp_buf;
  122. int rcount;
  123. int wcount;
  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;
  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;
  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;
  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 = 0;
  401. struct davinci_spi *davinci_spi;
  402. struct davinci_spi_dma *davinci_spi_dma;
  403. struct davinci_spi_platform_data *pdata;
  404. davinci_spi = spi_master_get_devdata(spi->master);
  405. pdata = davinci_spi->pdata;
  406. /* if bits per word length is zero then set it default 8 */
  407. if (!spi->bits_per_word)
  408. spi->bits_per_word = 8;
  409. if (!(spi->mode & SPI_NO_CS)) {
  410. if ((pdata->chip_sel == NULL) ||
  411. (pdata->chip_sel[spi->chip_select] == SPI_INTERN_CS))
  412. set_io_bits(davinci_spi->base + SPIPC0,
  413. 1 << spi->chip_select);
  414. }
  415. if (spi->mode & SPI_READY)
  416. set_io_bits(davinci_spi->base + SPIPC0, SPIPC0_SPIENA_MASK);
  417. if (spi->mode & SPI_LOOP)
  418. set_io_bits(davinci_spi->base + SPIGCR1,
  419. SPIGCR1_LOOPBACK_MASK);
  420. else
  421. clear_io_bits(davinci_spi->base + SPIGCR1,
  422. SPIGCR1_LOOPBACK_MASK);
  423. if (use_dma) {
  424. davinci_spi_dma = &davinci_spi->dma_channels;
  425. if ((davinci_spi_dma->dma_rx_channel == -1) ||
  426. (davinci_spi_dma->dma_tx_channel == -1))
  427. retval = davinci_spi_request_dma(spi);
  428. }
  429. return retval;
  430. }
  431. static void davinci_spi_cleanup(struct spi_device *spi)
  432. {
  433. if (use_dma) {
  434. struct davinci_spi *davinci_spi =
  435. spi_master_get_devdata(spi->master);
  436. struct davinci_spi_dma *davinci_spi_dma =
  437. &davinci_spi->dma_channels;
  438. if ((davinci_spi_dma->dma_rx_channel != -1)
  439. && (davinci_spi_dma->dma_tx_channel != -1)) {
  440. edma_free_channel(davinci_spi_dma->dma_tx_channel);
  441. edma_free_channel(davinci_spi_dma->dma_rx_channel);
  442. }
  443. }
  444. }
  445. static int davinci_spi_check_error(struct davinci_spi *davinci_spi,
  446. int int_status)
  447. {
  448. struct device *sdev = davinci_spi->bitbang.master->dev.parent;
  449. if (int_status & SPIFLG_TIMEOUT_MASK) {
  450. dev_dbg(sdev, "SPI Time-out Error\n");
  451. return -ETIMEDOUT;
  452. }
  453. if (int_status & SPIFLG_DESYNC_MASK) {
  454. dev_dbg(sdev, "SPI Desynchronization Error\n");
  455. return -EIO;
  456. }
  457. if (int_status & SPIFLG_BITERR_MASK) {
  458. dev_dbg(sdev, "SPI Bit error\n");
  459. return -EIO;
  460. }
  461. if (davinci_spi->version == SPI_VERSION_2) {
  462. if (int_status & SPIFLG_DLEN_ERR_MASK) {
  463. dev_dbg(sdev, "SPI Data Length Error\n");
  464. return -EIO;
  465. }
  466. if (int_status & SPIFLG_PARERR_MASK) {
  467. dev_dbg(sdev, "SPI Parity Error\n");
  468. return -EIO;
  469. }
  470. if (int_status & SPIFLG_OVRRUN_MASK) {
  471. dev_dbg(sdev, "SPI Data Overrun error\n");
  472. return -EIO;
  473. }
  474. if (int_status & SPIFLG_BUF_INIT_ACTIVE_MASK) {
  475. dev_dbg(sdev, "SPI Buffer Init Active\n");
  476. return -EBUSY;
  477. }
  478. }
  479. return 0;
  480. }
  481. /**
  482. * davinci_spi_process_events - check for and handle any SPI controller events
  483. * @davinci_spi: the controller data
  484. *
  485. * This function will check the SPIFLG register and handle any events that are
  486. * detected there
  487. */
  488. static int davinci_spi_process_events(struct davinci_spi *davinci_spi)
  489. {
  490. u32 buf, status, errors = 0, data1_reg_val;
  491. buf = ioread32(davinci_spi->base + SPIBUF);
  492. if (davinci_spi->rcount > 0 && !(buf & SPIBUF_RXEMPTY_MASK)) {
  493. davinci_spi->get_rx(buf & 0xFFFF, davinci_spi);
  494. davinci_spi->rcount--;
  495. }
  496. status = ioread32(davinci_spi->base + SPIFLG);
  497. if (unlikely(status & SPIFLG_ERROR_MASK)) {
  498. errors = status & SPIFLG_ERROR_MASK;
  499. goto out;
  500. }
  501. if (davinci_spi->wcount > 0 && !(buf & SPIBUF_TXFULL_MASK)) {
  502. data1_reg_val = ioread32(davinci_spi->base + SPIDAT1);
  503. davinci_spi->wcount--;
  504. data1_reg_val &= ~0xFFFF;
  505. data1_reg_val |= 0xFFFF & davinci_spi->get_tx(davinci_spi);
  506. iowrite32(data1_reg_val, davinci_spi->base + SPIDAT1);
  507. }
  508. out:
  509. return errors;
  510. }
  511. /**
  512. * davinci_spi_bufs - functions which will handle transfer data
  513. * @spi: spi device on which data transfer to be done
  514. * @t: spi transfer in which transfer info is filled
  515. *
  516. * This function will put data to be transferred into data register
  517. * of SPI controller and then wait until the completion will be marked
  518. * by the IRQ Handler.
  519. */
  520. static int davinci_spi_bufs_pio(struct spi_device *spi, struct spi_transfer *t)
  521. {
  522. struct davinci_spi *davinci_spi;
  523. int ret;
  524. u32 tx_data, data1_reg_val;
  525. u32 errors = 0;
  526. struct davinci_spi_config *spicfg;
  527. struct davinci_spi_platform_data *pdata;
  528. davinci_spi = spi_master_get_devdata(spi->master);
  529. pdata = davinci_spi->pdata;
  530. spicfg = (struct davinci_spi_config *)spi->controller_data;
  531. if (!spicfg)
  532. spicfg = &davinci_spi_default_cfg;
  533. davinci_spi->tx = t->tx_buf;
  534. davinci_spi->rx = t->rx_buf;
  535. davinci_spi->wcount = t->len /
  536. davinci_spi->bytes_per_word[spi->chip_select];
  537. davinci_spi->rcount = davinci_spi->wcount;
  538. data1_reg_val = ioread32(davinci_spi->base + SPIDAT1);
  539. /* Enable SPI */
  540. set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
  541. if (spicfg->io_type == SPI_IO_TYPE_INTR) {
  542. set_io_bits(davinci_spi->base + SPIINT, SPIINT_MASKINT);
  543. INIT_COMPLETION(davinci_spi->done);
  544. }
  545. /* start the transfer */
  546. davinci_spi->wcount--;
  547. tx_data = davinci_spi->get_tx(davinci_spi);
  548. data1_reg_val &= 0xFFFF0000;
  549. data1_reg_val |= tx_data & 0xFFFF;
  550. iowrite32(data1_reg_val, davinci_spi->base + SPIDAT1);
  551. /* Wait for the transfer to complete */
  552. if (spicfg->io_type == SPI_IO_TYPE_INTR) {
  553. wait_for_completion_interruptible(&(davinci_spi->done));
  554. } else {
  555. while (davinci_spi->rcount > 0 || davinci_spi->wcount > 0) {
  556. errors = davinci_spi_process_events(davinci_spi);
  557. if (errors)
  558. break;
  559. cpu_relax();
  560. }
  561. }
  562. clear_io_bits(davinci_spi->base + SPIINT, SPIINT_MASKALL);
  563. /*
  564. * Check for bit error, desync error,parity error,timeout error and
  565. * receive overflow errors
  566. */
  567. if (errors) {
  568. ret = davinci_spi_check_error(davinci_spi, errors);
  569. WARN(!ret, "%s: error reported but no error found!\n",
  570. dev_name(&spi->dev));
  571. return ret;
  572. }
  573. return t->len;
  574. }
  575. /**
  576. * davinci_spi_irq - Interrupt handler for SPI Master Controller
  577. * @irq: IRQ number for this SPI Master
  578. * @context_data: structure for SPI Master controller davinci_spi
  579. *
  580. * ISR will determine that interrupt arrives either for READ or WRITE command.
  581. * According to command it will do the appropriate action. It will check
  582. * transfer length and if it is not zero then dispatch transfer command again.
  583. * If transfer length is zero then it will indicate the COMPLETION so that
  584. * davinci_spi_bufs function can go ahead.
  585. */
  586. static irqreturn_t davinci_spi_irq(s32 irq, void *context_data)
  587. {
  588. struct davinci_spi *davinci_spi = context_data;
  589. int status;
  590. status = davinci_spi_process_events(davinci_spi);
  591. if (unlikely(status != 0))
  592. clear_io_bits(davinci_spi->base + SPIINT, SPIINT_MASKINT);
  593. if ((!davinci_spi->rcount && !davinci_spi->wcount) || status)
  594. complete(&davinci_spi->done);
  595. return IRQ_HANDLED;
  596. }
  597. static int davinci_spi_bufs_dma(struct spi_device *spi, struct spi_transfer *t)
  598. {
  599. struct davinci_spi *davinci_spi;
  600. int int_status = 0;
  601. int count, temp_count;
  602. struct davinci_spi_dma *davinci_spi_dma;
  603. int data_type, ret;
  604. unsigned long tx_reg, rx_reg;
  605. struct davinci_spi_platform_data *pdata;
  606. struct device *sdev;
  607. davinci_spi = spi_master_get_devdata(spi->master);
  608. pdata = davinci_spi->pdata;
  609. sdev = davinci_spi->bitbang.master->dev.parent;
  610. davinci_spi_dma = &davinci_spi->dma_channels;
  611. tx_reg = (unsigned long)davinci_spi->pbase + SPIDAT1;
  612. rx_reg = (unsigned long)davinci_spi->pbase + SPIBUF;
  613. davinci_spi->tx = t->tx_buf;
  614. davinci_spi->rx = t->rx_buf;
  615. /* convert len to words based on bits_per_word */
  616. data_type = davinci_spi->bytes_per_word[spi->chip_select];
  617. init_completion(&davinci_spi_dma->dma_rx_completion);
  618. init_completion(&davinci_spi_dma->dma_tx_completion);
  619. count = t->len / data_type; /* the number of elements */
  620. /* disable all interrupts for dma transfers */
  621. clear_io_bits(davinci_spi->base + SPIINT, SPIINT_MASKALL);
  622. /* Enable SPI */
  623. set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
  624. if (t->tx_buf) {
  625. t->tx_dma = dma_map_single(&spi->dev, (void *)t->tx_buf, count,
  626. DMA_TO_DEVICE);
  627. if (dma_mapping_error(&spi->dev, t->tx_dma)) {
  628. dev_dbg(sdev, "Unable to DMA map a %d bytes"
  629. " TX buffer\n", count);
  630. return -ENOMEM;
  631. }
  632. temp_count = count;
  633. } else {
  634. /* We need TX clocking for RX transaction */
  635. t->tx_dma = dma_map_single(&spi->dev,
  636. (void *)davinci_spi->tmp_buf, count + 1,
  637. DMA_TO_DEVICE);
  638. if (dma_mapping_error(&spi->dev, t->tx_dma)) {
  639. dev_dbg(sdev, "Unable to DMA map a %d bytes"
  640. " TX tmp buffer\n", count);
  641. return -ENOMEM;
  642. }
  643. temp_count = count + 1;
  644. }
  645. edma_set_transfer_params(davinci_spi_dma->dma_tx_channel,
  646. data_type, temp_count, 1, 0, ASYNC);
  647. edma_set_dest(davinci_spi_dma->dma_tx_channel, tx_reg, INCR, W8BIT);
  648. edma_set_src(davinci_spi_dma->dma_tx_channel, t->tx_dma, INCR, W8BIT);
  649. edma_set_src_index(davinci_spi_dma->dma_tx_channel, data_type, 0);
  650. edma_set_dest_index(davinci_spi_dma->dma_tx_channel, 0, 0);
  651. if (t->rx_buf) {
  652. t->rx_dma = dma_map_single(&spi->dev, (void *)t->rx_buf, count,
  653. DMA_FROM_DEVICE);
  654. if (dma_mapping_error(&spi->dev, t->rx_dma)) {
  655. dev_dbg(sdev, "Couldn't DMA map a %d bytes RX buffer\n",
  656. count);
  657. if (t->tx_buf != NULL)
  658. dma_unmap_single(NULL, t->tx_dma,
  659. count, DMA_TO_DEVICE);
  660. return -ENOMEM;
  661. }
  662. edma_set_transfer_params(davinci_spi_dma->dma_rx_channel,
  663. data_type, count, 1, 0, ASYNC);
  664. edma_set_src(davinci_spi_dma->dma_rx_channel,
  665. rx_reg, INCR, W8BIT);
  666. edma_set_dest(davinci_spi_dma->dma_rx_channel,
  667. t->rx_dma, INCR, W8BIT);
  668. edma_set_src_index(davinci_spi_dma->dma_rx_channel, 0, 0);
  669. edma_set_dest_index(davinci_spi_dma->dma_rx_channel,
  670. data_type, 0);
  671. }
  672. if (pdata->cshold_bug) {
  673. u16 spidat1 = ioread16(davinci_spi->base + SPIDAT1 + 2);
  674. iowrite16(spidat1, davinci_spi->base + SPIDAT1 + 2);
  675. }
  676. if (t->rx_buf)
  677. edma_start(davinci_spi_dma->dma_rx_channel);
  678. edma_start(davinci_spi_dma->dma_tx_channel);
  679. davinci_spi_set_dma_req(spi, 1);
  680. wait_for_completion_interruptible(&davinci_spi_dma->dma_tx_completion);
  681. if (t->rx_buf)
  682. wait_for_completion_interruptible(
  683. &davinci_spi_dma->dma_rx_completion);
  684. dma_unmap_single(NULL, t->tx_dma, temp_count, DMA_TO_DEVICE);
  685. if (t->rx_buf)
  686. dma_unmap_single(NULL, t->rx_dma, count, DMA_FROM_DEVICE);
  687. /*
  688. * Check for bit error, desync error,parity error,timeout error and
  689. * receive overflow errors
  690. */
  691. int_status = ioread32(davinci_spi->base + SPIFLG);
  692. ret = davinci_spi_check_error(davinci_spi, int_status);
  693. if (ret != 0)
  694. return ret;
  695. return t->len;
  696. }
  697. /**
  698. * davinci_spi_probe - probe function for SPI Master Controller
  699. * @pdev: platform_device structure which contains plateform specific data
  700. */
  701. static int davinci_spi_probe(struct platform_device *pdev)
  702. {
  703. struct spi_master *master;
  704. struct davinci_spi *davinci_spi;
  705. struct davinci_spi_platform_data *pdata;
  706. struct resource *r, *mem;
  707. resource_size_t dma_rx_chan = SPI_NO_RESOURCE;
  708. resource_size_t dma_tx_chan = SPI_NO_RESOURCE;
  709. resource_size_t dma_eventq = SPI_NO_RESOURCE;
  710. int i = 0, ret = 0;
  711. u32 spipc0;
  712. pdata = pdev->dev.platform_data;
  713. if (pdata == NULL) {
  714. ret = -ENODEV;
  715. goto err;
  716. }
  717. master = spi_alloc_master(&pdev->dev, sizeof(struct davinci_spi));
  718. if (master == NULL) {
  719. ret = -ENOMEM;
  720. goto err;
  721. }
  722. dev_set_drvdata(&pdev->dev, master);
  723. davinci_spi = spi_master_get_devdata(master);
  724. if (davinci_spi == NULL) {
  725. ret = -ENOENT;
  726. goto free_master;
  727. }
  728. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  729. if (r == NULL) {
  730. ret = -ENOENT;
  731. goto free_master;
  732. }
  733. davinci_spi->pbase = r->start;
  734. davinci_spi->region_size = resource_size(r);
  735. davinci_spi->pdata = pdata;
  736. mem = request_mem_region(r->start, davinci_spi->region_size,
  737. pdev->name);
  738. if (mem == NULL) {
  739. ret = -EBUSY;
  740. goto free_master;
  741. }
  742. davinci_spi->base = ioremap(r->start, davinci_spi->region_size);
  743. if (davinci_spi->base == NULL) {
  744. ret = -ENOMEM;
  745. goto release_region;
  746. }
  747. davinci_spi->irq = platform_get_irq(pdev, 0);
  748. if (davinci_spi->irq <= 0) {
  749. ret = -EINVAL;
  750. goto unmap_io;
  751. }
  752. ret = request_irq(davinci_spi->irq, davinci_spi_irq, 0,
  753. dev_name(&pdev->dev), davinci_spi);
  754. if (ret)
  755. goto unmap_io;
  756. /* Allocate tmp_buf for tx_buf */
  757. davinci_spi->tmp_buf = kzalloc(SPI_BUFSIZ, GFP_KERNEL);
  758. if (davinci_spi->tmp_buf == NULL) {
  759. ret = -ENOMEM;
  760. goto irq_free;
  761. }
  762. davinci_spi->bitbang.master = spi_master_get(master);
  763. if (davinci_spi->bitbang.master == NULL) {
  764. ret = -ENODEV;
  765. goto free_tmp_buf;
  766. }
  767. davinci_spi->clk = clk_get(&pdev->dev, NULL);
  768. if (IS_ERR(davinci_spi->clk)) {
  769. ret = -ENODEV;
  770. goto put_master;
  771. }
  772. clk_enable(davinci_spi->clk);
  773. master->bus_num = pdev->id;
  774. master->num_chipselect = pdata->num_chipselect;
  775. master->setup = davinci_spi_setup;
  776. master->cleanup = davinci_spi_cleanup;
  777. davinci_spi->bitbang.chipselect = davinci_spi_chipselect;
  778. davinci_spi->bitbang.setup_transfer = davinci_spi_setup_transfer;
  779. davinci_spi->version = pdata->version;
  780. use_dma = pdata->use_dma;
  781. davinci_spi->bitbang.flags = SPI_NO_CS | SPI_LSB_FIRST | SPI_LOOP;
  782. if (davinci_spi->version == SPI_VERSION_2)
  783. davinci_spi->bitbang.flags |= SPI_READY;
  784. if (use_dma) {
  785. r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  786. if (r)
  787. dma_rx_chan = r->start;
  788. r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  789. if (r)
  790. dma_tx_chan = r->start;
  791. r = platform_get_resource(pdev, IORESOURCE_DMA, 2);
  792. if (r)
  793. dma_eventq = r->start;
  794. }
  795. if (!use_dma ||
  796. dma_rx_chan == SPI_NO_RESOURCE ||
  797. dma_tx_chan == SPI_NO_RESOURCE ||
  798. dma_eventq == SPI_NO_RESOURCE) {
  799. davinci_spi->bitbang.txrx_bufs = davinci_spi_bufs_pio;
  800. use_dma = 0;
  801. } else {
  802. davinci_spi->bitbang.txrx_bufs = davinci_spi_bufs_dma;
  803. davinci_spi->dma_channels.dma_rx_channel = -1;
  804. davinci_spi->dma_channels.dma_rx_sync_dev = dma_rx_chan;
  805. davinci_spi->dma_channels.dma_tx_channel = -1;
  806. davinci_spi->dma_channels.dma_tx_sync_dev = dma_tx_chan;
  807. davinci_spi->dma_channels.eventq = dma_eventq;
  808. dev_info(&pdev->dev, "DaVinci SPI driver in EDMA mode\n"
  809. "Using RX channel = %d , TX channel = %d and "
  810. "event queue = %d", dma_rx_chan, dma_tx_chan,
  811. dma_eventq);
  812. }
  813. davinci_spi->get_rx = davinci_spi_rx_buf_u8;
  814. davinci_spi->get_tx = davinci_spi_tx_buf_u8;
  815. init_completion(&davinci_spi->done);
  816. /* Reset In/OUT SPI module */
  817. iowrite32(0, davinci_spi->base + SPIGCR0);
  818. udelay(100);
  819. iowrite32(1, davinci_spi->base + SPIGCR0);
  820. /* Set up SPIPC0. CS and ENA init is done in davinci_spi_setup */
  821. spipc0 = SPIPC0_DIFUN_MASK | SPIPC0_DOFUN_MASK | SPIPC0_CLKFUN_MASK;
  822. iowrite32(spipc0, davinci_spi->base + SPIPC0);
  823. /* initialize chip selects */
  824. if (pdata->chip_sel) {
  825. for (i = 0; i < pdata->num_chipselect; i++) {
  826. if (pdata->chip_sel[i] != SPI_INTERN_CS)
  827. gpio_direction_output(pdata->chip_sel[i], 1);
  828. }
  829. }
  830. /* Clock internal */
  831. if (davinci_spi->pdata->clk_internal)
  832. set_io_bits(davinci_spi->base + SPIGCR1,
  833. SPIGCR1_CLKMOD_MASK);
  834. else
  835. clear_io_bits(davinci_spi->base + SPIGCR1,
  836. SPIGCR1_CLKMOD_MASK);
  837. if (pdata->intr_line)
  838. iowrite32(SPI_INTLVL_1, davinci_spi->base + SPILVL);
  839. else
  840. iowrite32(SPI_INTLVL_0, davinci_spi->base + SPILVL);
  841. iowrite32(CS_DEFAULT, davinci_spi->base + SPIDEF);
  842. /* master mode default */
  843. set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_MASTER_MASK);
  844. ret = spi_bitbang_start(&davinci_spi->bitbang);
  845. if (ret)
  846. goto free_clk;
  847. dev_info(&pdev->dev, "Controller at 0x%p\n", davinci_spi->base);
  848. return ret;
  849. free_clk:
  850. clk_disable(davinci_spi->clk);
  851. clk_put(davinci_spi->clk);
  852. put_master:
  853. spi_master_put(master);
  854. free_tmp_buf:
  855. kfree(davinci_spi->tmp_buf);
  856. irq_free:
  857. free_irq(davinci_spi->irq, davinci_spi);
  858. unmap_io:
  859. iounmap(davinci_spi->base);
  860. release_region:
  861. release_mem_region(davinci_spi->pbase, davinci_spi->region_size);
  862. free_master:
  863. kfree(master);
  864. err:
  865. return ret;
  866. }
  867. /**
  868. * davinci_spi_remove - remove function for SPI Master Controller
  869. * @pdev: platform_device structure which contains plateform specific data
  870. *
  871. * This function will do the reverse action of davinci_spi_probe function
  872. * It will free the IRQ and SPI controller's memory region.
  873. * It will also call spi_bitbang_stop to destroy the work queue which was
  874. * created by spi_bitbang_start.
  875. */
  876. static int __exit davinci_spi_remove(struct platform_device *pdev)
  877. {
  878. struct davinci_spi *davinci_spi;
  879. struct spi_master *master;
  880. master = dev_get_drvdata(&pdev->dev);
  881. davinci_spi = spi_master_get_devdata(master);
  882. spi_bitbang_stop(&davinci_spi->bitbang);
  883. clk_disable(davinci_spi->clk);
  884. clk_put(davinci_spi->clk);
  885. spi_master_put(master);
  886. kfree(davinci_spi->tmp_buf);
  887. free_irq(davinci_spi->irq, davinci_spi);
  888. iounmap(davinci_spi->base);
  889. release_mem_region(davinci_spi->pbase, davinci_spi->region_size);
  890. return 0;
  891. }
  892. static struct platform_driver davinci_spi_driver = {
  893. .driver.name = "spi_davinci",
  894. .remove = __exit_p(davinci_spi_remove),
  895. };
  896. static int __init davinci_spi_init(void)
  897. {
  898. return platform_driver_probe(&davinci_spi_driver, davinci_spi_probe);
  899. }
  900. module_init(davinci_spi_init);
  901. static void __exit davinci_spi_exit(void)
  902. {
  903. platform_driver_unregister(&davinci_spi_driver);
  904. }
  905. module_exit(davinci_spi_exit);
  906. MODULE_DESCRIPTION("TI DaVinci SPI Master Controller Driver");
  907. MODULE_LICENSE("GPL");