spi-davinci.c 27 KB

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