spi-davinci.c 28 KB

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