spi-davinci.c 25 KB

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