davinci_spi.c 31 KB

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