spi-davinci.c 26 KB

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