spi-davinci.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2011-01-13 weety first version
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #include <dm36x.h>
  14. #include <edma.h>
  15. #include "spi-davinci.h"
  16. #define unlikely(x) x
  17. #define barrier() __asm__ __volatile__("": : :"memory")
  18. #define cpu_relax() barrier()
  19. #define SPI_DEBUG 0
  20. #if SPI_DEBUG
  21. #define spi_dbg(dev, fmt, ...) \
  22. do { \
  23. rt_kprintf("%s:", dev->parent.name); \
  24. rt_kprintf(fmt, ##__VA_ARGS__); \
  25. } while(0)
  26. #else
  27. #define spi_dbg(dev, fmt, ...)
  28. #endif
  29. #define SZ_64K 0x10000
  30. #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
  31. #define SPI_NO_RESOURCE ((resource_size_t)-1)
  32. #define SPI_MAX_CHIPSELECT 2
  33. #define CS_DEFAULT 0xFF
  34. #define __iomem
  35. #define BIT(nr) (1UL << (nr))
  36. #define SPIFMT_PHASE_MASK BIT(16)
  37. #define SPIFMT_POLARITY_MASK BIT(17)
  38. #define SPIFMT_DISTIMER_MASK BIT(18)
  39. #define SPIFMT_SHIFTDIR_MASK BIT(20)
  40. #define SPIFMT_WAITENA_MASK BIT(21)
  41. #define SPIFMT_PARITYENA_MASK BIT(22)
  42. #define SPIFMT_ODD_PARITY_MASK BIT(23)
  43. #define SPIFMT_WDELAY_MASK 0x3f000000u
  44. #define SPIFMT_WDELAY_SHIFT 24
  45. #define SPIFMT_PRESCALE_SHIFT 8
  46. /* SPIPC0 */
  47. #define SPIPC0_DIFUN_MASK BIT(11) /* MISO */
  48. #define SPIPC0_DOFUN_MASK BIT(10) /* MOSI */
  49. #define SPIPC0_CLKFUN_MASK BIT(9) /* CLK */
  50. #define SPIPC0_SPIENA_MASK BIT(8) /* nREADY */
  51. #define SPIINT_MASKALL 0x0101035F
  52. #define SPIINT_MASKINT 0x0000015F
  53. #define SPI_INTLVL_1 0x000001FF
  54. #define SPI_INTLVL_0 0x00000000
  55. /* SPIDAT1 (upper 16 bit defines) */
  56. #define SPIDAT1_CSHOLD_MASK BIT(12)
  57. /* SPIGCR1 */
  58. #define SPIGCR1_CLKMOD_MASK BIT(1)
  59. #define SPIGCR1_MASTER_MASK BIT(0)
  60. #define SPIGCR1_POWERDOWN_MASK BIT(8)
  61. #define SPIGCR1_LOOPBACK_MASK BIT(16)
  62. #define SPIGCR1_SPIENA_MASK BIT(24)
  63. /* SPIBUF */
  64. #define SPIBUF_TXFULL_MASK BIT(29)
  65. #define SPIBUF_RXEMPTY_MASK BIT(31)
  66. /* SPIDELAY */
  67. #define SPIDELAY_C2TDELAY_SHIFT 24
  68. #define SPIDELAY_C2TDELAY_MASK (0xFF << SPIDELAY_C2TDELAY_SHIFT)
  69. #define SPIDELAY_T2CDELAY_SHIFT 16
  70. #define SPIDELAY_T2CDELAY_MASK (0xFF << SPIDELAY_T2CDELAY_SHIFT)
  71. #define SPIDELAY_T2EDELAY_SHIFT 8
  72. #define SPIDELAY_T2EDELAY_MASK (0xFF << SPIDELAY_T2EDELAY_SHIFT)
  73. #define SPIDELAY_C2EDELAY_SHIFT 0
  74. #define SPIDELAY_C2EDELAY_MASK 0xFF
  75. /* Error Masks */
  76. #define SPIFLG_DLEN_ERR_MASK BIT(0)
  77. #define SPIFLG_TIMEOUT_MASK BIT(1)
  78. #define SPIFLG_PARERR_MASK BIT(2)
  79. #define SPIFLG_DESYNC_MASK BIT(3)
  80. #define SPIFLG_BITERR_MASK BIT(4)
  81. #define SPIFLG_OVRRUN_MASK BIT(6)
  82. #define SPIFLG_BUF_INIT_ACTIVE_MASK BIT(24)
  83. #define SPIFLG_ERROR_MASK (SPIFLG_DLEN_ERR_MASK \
  84. | SPIFLG_TIMEOUT_MASK | SPIFLG_PARERR_MASK \
  85. | SPIFLG_DESYNC_MASK | SPIFLG_BITERR_MASK \
  86. | SPIFLG_OVRRUN_MASK)
  87. #define SPIINT_DMA_REQ_EN BIT(16)
  88. /* SPI Controller registers */
  89. #define SPIGCR0 0x00
  90. #define SPIGCR1 0x04
  91. #define SPIINT 0x08
  92. #define SPILVL 0x0c
  93. #define SPIFLG 0x10
  94. #define SPIPC0 0x14
  95. #define SPIDAT1 0x3c
  96. #define SPIBUF 0x40
  97. #define SPIDELAY 0x48
  98. #define SPIDEF 0x4c
  99. #define SPIFMT0 0x50
  100. /* We have 2 DMA channels per CS, one for RX and one for TX */
  101. struct davinci_spi_dma {
  102. int tx_channel;
  103. int rx_channel;
  104. int dummy_param_slot;
  105. enum dma_event_q eventq;
  106. };
  107. /* SPI Controller driver's private data. */
  108. struct davinci_spi {
  109. struct rt_spi_bus parent;
  110. struct clk *clk;
  111. u8 version;
  112. void __iomem *base;
  113. u32 irq;
  114. struct rt_completion done;
  115. const void *tx;
  116. void *rx;
  117. #define SMP_CACHE_BYTES 32
  118. #define SPI_TMP_BUFSZ (SMP_CACHE_BYTES + 1)
  119. u8 rx_tmp_buf[SPI_TMP_BUFSZ];
  120. int rcount;
  121. int wcount;
  122. struct davinci_spi_dma dma;
  123. void (*get_rx)(u32 rx_data, struct davinci_spi *);
  124. u32 (*get_tx)(struct davinci_spi *);
  125. u8 bytes_per_word[SPI_MAX_CHIPSELECT];
  126. u8 chip_sel[SPI_MAX_CHIPSELECT];
  127. struct davinci_spi_config *controller_data;
  128. int cshold_bug;
  129. };
  130. static struct davinci_spi_config davinci_spi_default_cfg;
  131. extern void mmu_clean_dcache(rt_uint32_t buffer, rt_uint32_t size);
  132. extern void mmu_invalidate_dcache(rt_uint32_t buffer, rt_uint32_t size);
  133. static void davinci_spi_rx_buf_u8(u32 data, struct davinci_spi *dspi)
  134. {
  135. if (dspi->rx) {
  136. u8 *rx = dspi->rx;
  137. *rx++ = (u8)data;
  138. dspi->rx = rx;
  139. }
  140. }
  141. static void davinci_spi_rx_buf_u16(u32 data, struct davinci_spi *dspi)
  142. {
  143. if (dspi->rx) {
  144. u16 *rx = dspi->rx;
  145. *rx++ = (u16)data;
  146. dspi->rx = rx;
  147. }
  148. }
  149. static u32 davinci_spi_tx_buf_u8(struct davinci_spi *dspi)
  150. {
  151. u32 data = 0;
  152. if (dspi->tx) {
  153. const u8 *tx = dspi->tx;
  154. data = *tx++;
  155. dspi->tx = tx;
  156. }
  157. return data;
  158. }
  159. static u32 davinci_spi_tx_buf_u16(struct davinci_spi *dspi)
  160. {
  161. u32 data = 0;
  162. if (dspi->tx) {
  163. const u16 *tx = dspi->tx;
  164. data = *tx++;
  165. dspi->tx = tx;
  166. }
  167. return data;
  168. }
  169. static inline void set_io_bits(void __iomem *addr, u32 bits)
  170. {
  171. u32 v = readl(addr);
  172. v |= bits;
  173. writel(v, addr);
  174. }
  175. static inline void clear_io_bits(void __iomem *addr, u32 bits)
  176. {
  177. u32 v = readl(addr);
  178. v &= ~bits;
  179. writel(v, addr);
  180. }
  181. /*
  182. * Interface to control the chip select signal
  183. */
  184. static void davinci_spi_chipselect(struct rt_spi_device *spi, int value)
  185. {
  186. struct davinci_spi *dspi;
  187. u8 chip_sel = (u8)spi->parent.user_data;
  188. u16 spidat1 = CS_DEFAULT;
  189. bool gpio_chipsel = RT_FALSE;
  190. dspi = spi->bus->parent.user_data;
  191. if (chip_sel < SPI_MAX_CHIPSELECT &&
  192. dspi->chip_sel[chip_sel] != SPI_INTERN_CS)
  193. gpio_chipsel = RT_TRUE;
  194. /*
  195. * Board specific chip select logic decides the polarity and cs
  196. * line for the controller
  197. */
  198. if (gpio_chipsel) {
  199. if (value == 0)
  200. gpio_set_value(dspi->chip_sel[chip_sel], 0);
  201. else
  202. gpio_set_value(dspi->chip_sel[chip_sel], 1);
  203. } else {
  204. spidat1 = readw(dspi->base + SPIDAT1 + 2);
  205. if (value == 0) {
  206. spidat1 |= SPIDAT1_CSHOLD_MASK;
  207. spidat1 &= ~(0x1 << chip_sel);
  208. } else {
  209. spidat1 &= ~SPIDAT1_CSHOLD_MASK;
  210. spidat1 |= 0x03;
  211. }
  212. rt_kprintf("0x%04x\n", spidat1);
  213. writew(spidat1, dspi->base + SPIDAT1 + 2);
  214. }
  215. }
  216. /**
  217. * davinci_spi_get_prescale - Calculates the correct prescale value
  218. * @maxspeed_hz: the maximum rate the SPI clock can run at
  219. *
  220. * This function calculates the prescale value that generates a clock rate
  221. * less than or equal to the specified maximum.
  222. *
  223. * Returns: calculated prescale - 1 for easy programming into SPI registers
  224. * or negative error number if valid prescalar cannot be updated.
  225. */
  226. static inline int davinci_spi_get_prescale(struct davinci_spi *dspi,
  227. u32 max_speed_hz)
  228. {
  229. int ret;
  230. ret = DIV_ROUND_UP(clk_get_rate(dspi->clk), max_speed_hz);
  231. if (ret < 3) {
  232. rt_kprintf("spi clock freq too high\n");
  233. ret = 3;
  234. }
  235. if (ret > 256) {
  236. rt_kprintf("spi clock freq too litter\n");
  237. ret = 256;
  238. }
  239. /*if (ret < 3 || ret > 256)
  240. return -RT_ERROR;*/
  241. return ret - 1;
  242. }
  243. /**
  244. * davinci_spi_setup_transfer - This functions will determine transfer method
  245. * @spi: spi device on which data transfer to be done
  246. * @t: spi transfer in which transfer info is filled
  247. *
  248. * This function determines data transfer method (8/16/32 bit transfer).
  249. * It will also set the SPI Clock Control register according to
  250. * SPI slave device freq.
  251. */
  252. static int davinci_spi_setup_transfer(struct rt_spi_device *spi,
  253. struct rt_spi_configuration *cfg)
  254. {
  255. struct davinci_spi *dspi;
  256. struct davinci_spi_config *spicfg;
  257. u8 bits_per_word = 0;
  258. u32 hz = 0, spifmt = 0, prescale = 0;
  259. u8 chip_select = (u8)spi->parent.user_data;
  260. dspi = spi->bus->parent.user_data;
  261. spicfg = (struct davinci_spi_config *)dspi->controller_data;
  262. if (!spicfg)
  263. spicfg = &davinci_spi_default_cfg;
  264. bits_per_word = cfg->data_width;
  265. hz = cfg->max_hz;
  266. /*
  267. * Assign function pointer to appropriate transfer method
  268. * 8bit, 16bit or 32bit transfer
  269. */
  270. if (bits_per_word <= 8 && bits_per_word >= 2) {
  271. dspi->get_rx = davinci_spi_rx_buf_u8;
  272. dspi->get_tx = davinci_spi_tx_buf_u8;
  273. dspi->bytes_per_word[chip_select] = 1;
  274. } else if (bits_per_word <= 16 && bits_per_word >= 2) {
  275. dspi->get_rx = davinci_spi_rx_buf_u16;
  276. dspi->get_tx = davinci_spi_tx_buf_u16;
  277. dspi->bytes_per_word[chip_select] = 2;
  278. } else
  279. return -RT_ERROR;
  280. /* Set up SPIFMTn register, unique to this chipselect. */
  281. prescale = davinci_spi_get_prescale(dspi, hz);
  282. if (prescale < 0)
  283. return prescale;
  284. spifmt = (prescale << SPIFMT_PRESCALE_SHIFT) | (bits_per_word & 0x1f);
  285. if (!(cfg->mode & RT_SPI_MSB))
  286. spifmt |= SPIFMT_SHIFTDIR_MASK;
  287. if (cfg->mode & RT_SPI_CPOL)
  288. spifmt |= SPIFMT_POLARITY_MASK;
  289. if (!(cfg->mode & RT_SPI_CPHA))
  290. spifmt |= SPIFMT_PHASE_MASK;
  291. /*
  292. * Version 1 hardware supports two basic SPI modes:
  293. * - Standard SPI mode uses 4 pins, with chipselect
  294. * - 3 pin SPI is a 4 pin variant without CS (SPI_NO_CS)
  295. * (distinct from SPI_3WIRE, with just one data wire;
  296. * or similar variants without MOSI or without MISO)
  297. *
  298. * Version 2 hardware supports an optional handshaking signal,
  299. * so it can support two more modes:
  300. * - 5 pin SPI variant is standard SPI plus SPI_READY
  301. * - 4 pin with enable is (SPI_READY | SPI_NO_CS)
  302. */
  303. if (dspi->version == SPI_VERSION_2) {
  304. u32 delay = 0;
  305. spifmt |= ((spicfg->wdelay << SPIFMT_WDELAY_SHIFT)
  306. & SPIFMT_WDELAY_MASK);
  307. if (spicfg->odd_parity)
  308. spifmt |= SPIFMT_ODD_PARITY_MASK;
  309. if (spicfg->parity_enable)
  310. spifmt |= SPIFMT_PARITYENA_MASK;
  311. if (spicfg->timer_disable) {
  312. spifmt |= SPIFMT_DISTIMER_MASK;
  313. } else {
  314. delay |= (spicfg->c2tdelay << SPIDELAY_C2TDELAY_SHIFT)
  315. & SPIDELAY_C2TDELAY_MASK;
  316. delay |= (spicfg->t2cdelay << SPIDELAY_T2CDELAY_SHIFT)
  317. & SPIDELAY_T2CDELAY_MASK;
  318. }
  319. if (cfg->mode & RT_SPI_READY) {
  320. spifmt |= SPIFMT_WAITENA_MASK;
  321. delay |= (spicfg->t2edelay << SPIDELAY_T2EDELAY_SHIFT)
  322. & SPIDELAY_T2EDELAY_MASK;
  323. delay |= (spicfg->c2edelay << SPIDELAY_C2EDELAY_SHIFT)
  324. & SPIDELAY_C2EDELAY_MASK;
  325. }
  326. writel(delay, dspi->base + SPIDELAY);
  327. }
  328. writel(spifmt, dspi->base + SPIFMT0);
  329. return 0;
  330. }
  331. #if 0
  332. /**
  333. * davinci_spi_setup - This functions will set default transfer method
  334. * @spi: spi device on which data transfer to be done
  335. *
  336. * This functions sets the default transfer method.
  337. */
  338. static int davinci_spi_setup(struct spi_device *spi)
  339. {
  340. int retval = 0;
  341. struct davinci_spi *dspi;
  342. struct davinci_spi_platform_data *pdata;
  343. dspi = spi_master_get_devdata(spi->master);
  344. pdata = dspi->pdata;
  345. /* if bits per word length is zero then set it default 8 */
  346. if (!spi->bits_per_word)
  347. spi->bits_per_word = 8;
  348. if (!(spi->mode & SPI_NO_CS)) {
  349. if ((pdata->chip_sel == NULL) ||
  350. (pdata->chip_sel[spi->chip_select] == SPI_INTERN_CS))
  351. set_io_bits(dspi->base + SPIPC0, 1 << spi->chip_select);
  352. }
  353. if (spi->mode & SPI_READY)
  354. set_io_bits(dspi->base + SPIPC0, SPIPC0_SPIENA_MASK);
  355. if (spi->mode & SPI_LOOP)
  356. set_io_bits(dspi->base + SPIGCR1, SPIGCR1_LOOPBACK_MASK);
  357. else
  358. clear_io_bits(dspi->base + SPIGCR1, SPIGCR1_LOOPBACK_MASK);
  359. return retval;
  360. }
  361. #endif
  362. static int davinci_spi_check_error(struct davinci_spi *dspi, int int_status)
  363. {
  364. struct rt_device *sdev = &dspi->parent.parent;
  365. if (int_status & SPIFLG_TIMEOUT_MASK) {
  366. spi_dbg(sdev, "SPI Time-out Error\n");
  367. return -RT_ETIMEOUT;
  368. }
  369. if (int_status & SPIFLG_DESYNC_MASK) {
  370. spi_dbg(sdev, "SPI Desynchronization Error\n");
  371. return -RT_EIO;
  372. }
  373. if (int_status & SPIFLG_BITERR_MASK) {
  374. spi_dbg(sdev, "SPI Bit error\n");
  375. return -RT_EIO;
  376. }
  377. if (dspi->version == SPI_VERSION_2) {
  378. if (int_status & SPIFLG_DLEN_ERR_MASK) {
  379. spi_dbg(sdev, "SPI Data Length Error\n");
  380. return -RT_EIO;
  381. }
  382. if (int_status & SPIFLG_PARERR_MASK) {
  383. spi_dbg(sdev, "SPI Parity Error\n");
  384. return -RT_EIO;
  385. }
  386. if (int_status & SPIFLG_OVRRUN_MASK) {
  387. spi_dbg(sdev, "SPI Data Overrun error\n");
  388. return -RT_EIO;
  389. }
  390. if (int_status & SPIFLG_BUF_INIT_ACTIVE_MASK) {
  391. spi_dbg(sdev, "SPI Buffer Init Active\n");
  392. return -RT_EBUSY;
  393. }
  394. }
  395. return 0;
  396. }
  397. /**
  398. * davinci_spi_process_events - check for and handle any SPI controller events
  399. * @dspi: the controller data
  400. *
  401. * This function will check the SPIFLG register and handle any events that are
  402. * detected there
  403. */
  404. static int davinci_spi_process_events(struct davinci_spi *dspi)
  405. {
  406. u32 buf, status, errors = 0, spidat1;
  407. buf = readl(dspi->base + SPIBUF);
  408. if (dspi->rcount > 0 && !(buf & SPIBUF_RXEMPTY_MASK)) {
  409. dspi->get_rx(buf & 0xFFFF, dspi);
  410. dspi->rcount--;
  411. }
  412. status = readl(dspi->base + SPIFLG);
  413. if (unlikely(status & SPIFLG_ERROR_MASK)) {
  414. errors = status & SPIFLG_ERROR_MASK;
  415. goto out;
  416. }
  417. if (dspi->wcount > 0 && !(buf & SPIBUF_TXFULL_MASK)) {
  418. spidat1 = readl(dspi->base + SPIDAT1);
  419. dspi->wcount--;
  420. spidat1 &= ~0xFFFF;
  421. spidat1 |= 0xFFFF & dspi->get_tx(dspi);
  422. writel(spidat1, dspi->base + SPIDAT1);
  423. }
  424. out:
  425. return errors;
  426. }
  427. static void davinci_spi_dma_callback(unsigned lch, u16 status, void *data)
  428. {
  429. struct davinci_spi *dspi = data;
  430. struct davinci_spi_dma *dma = &dspi->dma;
  431. edma_stop(lch);
  432. if (status == DMA_COMPLETE) {
  433. if (lch == dma->rx_channel)
  434. dspi->rcount = 0;
  435. if (lch == dma->tx_channel)
  436. dspi->wcount = 0;
  437. }
  438. if ((!dspi->wcount && !dspi->rcount) || (status != DMA_COMPLETE))
  439. rt_completion_done(&dspi->done);
  440. }
  441. /**
  442. * davinci_spi_bufs - functions which will handle transfer data
  443. * @spi: spi device on which data transfer to be done
  444. * @t: spi transfer in which transfer info is filled
  445. *
  446. * This function will put data to be transferred into data register
  447. * of SPI controller and then wait until the completion will be marked
  448. * by the IRQ Handler.
  449. */
  450. static int davinci_spi_bufs(struct rt_spi_device *spi, struct rt_spi_message *msg)
  451. {
  452. struct davinci_spi *dspi;
  453. int data_type, ret;
  454. u32 tx_data, spidat1;
  455. u32 errors = 0;
  456. struct davinci_spi_config *spicfg;
  457. unsigned rx_buf_count;
  458. struct rt_device *sdev;
  459. u8 chip_select = (u8)spi->parent.user_data;
  460. dspi = spi->bus->parent.user_data;
  461. spicfg = (struct davinci_spi_config *)dspi->controller_data;
  462. if (!spicfg)
  463. spicfg = &davinci_spi_default_cfg;
  464. sdev = &dspi->parent.parent;
  465. /* convert len to words based on bits_per_word */
  466. data_type = dspi->bytes_per_word[chip_select];
  467. dspi->tx = msg->send_buf;
  468. dspi->rx = msg->recv_buf;
  469. dspi->wcount = msg->length / data_type;
  470. dspi->rcount = dspi->wcount;
  471. spidat1 = readl(dspi->base + SPIDAT1);
  472. clear_io_bits(dspi->base + SPIGCR1, SPIGCR1_POWERDOWN_MASK);
  473. set_io_bits(dspi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
  474. rt_completion_init(&(dspi->done));
  475. if (msg->cs_take)
  476. davinci_spi_chipselect(spi, 0);
  477. if (spicfg->io_type == SPI_IO_TYPE_INTR)
  478. set_io_bits(dspi->base + SPIINT, SPIINT_MASKINT);
  479. if (msg->length > 0) {
  480. if (spicfg->io_type != SPI_IO_TYPE_DMA) {
  481. /* start the transfer */
  482. dspi->wcount--;
  483. tx_data = dspi->get_tx(dspi);
  484. spidat1 &= 0xFFFF0000;
  485. spidat1 |= tx_data & 0xFFFF;
  486. writel(spidat1, dspi->base + SPIDAT1);
  487. } else {
  488. struct davinci_spi_dma *dma;
  489. unsigned long tx_reg, rx_reg;
  490. struct edmacc_param param;
  491. void *rx_buf;
  492. int b, c;
  493. dma = &dspi->dma;
  494. tx_reg = (unsigned long)dspi->base + SPIDAT1;
  495. rx_reg = (unsigned long)dspi->base + SPIBUF;
  496. /*
  497. * Transmit DMA setup
  498. *
  499. * If there is transmit data, map the transmit buffer, set it
  500. * as the source of data and set the source B index to data
  501. * size. If there is no transmit data, set the transmit register
  502. * as the source of data, and set the source B index to zero.
  503. *
  504. * The destination is always the transmit register itself. And
  505. * the destination never increments.
  506. */
  507. if (msg->send_buf) {
  508. mmu_clean_dcache((rt_uint32_t)msg->send_buf, (rt_uint32_t)msg->length);
  509. }
  510. /*
  511. * If number of words is greater than 65535, then we need
  512. * to configure a 3 dimension transfer. Use the BCNTRLD
  513. * feature to allow for transfers that aren't even multiples
  514. * of 65535 (or any other possible b size) by first transferring
  515. * the remainder amount then grabbing the next N blocks of
  516. * 65535 words.
  517. */
  518. c = dspi->wcount / (SZ_64K - 1); /* N 65535 Blocks */
  519. b = dspi->wcount - c * (SZ_64K - 1); /* Remainder */
  520. if (b)
  521. c++;
  522. else
  523. b = SZ_64K - 1;
  524. param.opt = TCINTEN | EDMA_TCC(dma->tx_channel);
  525. param.src = msg->send_buf ? msg->send_buf : tx_reg;
  526. param.a_b_cnt = b << 16 | data_type;
  527. param.dst = tx_reg;
  528. param.src_dst_bidx = msg->send_buf ? data_type : 0;
  529. param.link_bcntrld = 0xffffffff;
  530. param.src_dst_cidx = msg->send_buf ? data_type : 0;
  531. param.ccnt = c;
  532. edma_write_slot(dma->tx_channel, &param);
  533. edma_link(dma->tx_channel, dma->dummy_param_slot);
  534. /*
  535. * Receive DMA setup
  536. *
  537. * If there is receive buffer, use it to receive data. If there
  538. * is none provided, use a temporary receive buffer. Set the
  539. * destination B index to 0 so effectively only one byte is used
  540. * in the temporary buffer (address does not increment).
  541. *
  542. * The source of receive data is the receive data register. The
  543. * source address never increments.
  544. */
  545. if (msg->recv_buf) {
  546. rx_buf = msg->recv_buf;
  547. rx_buf_count = msg->length;
  548. } else {
  549. rx_buf = dspi->rx_tmp_buf;
  550. rx_buf_count = sizeof(dspi->rx_tmp_buf);
  551. }
  552. mmu_invalidate_dcache((rt_uint32_t)rx_buf, (rt_uint32_t)rx_buf_count);
  553. param.opt = TCINTEN | EDMA_TCC(dma->rx_channel);
  554. param.src = rx_reg;
  555. param.a_b_cnt = b << 16 | data_type;
  556. param.dst = rx_buf;
  557. param.src_dst_bidx = (msg->recv_buf ? data_type : 0) << 16;
  558. param.link_bcntrld = 0xffffffff;
  559. param.src_dst_cidx = (msg->recv_buf ? data_type : 0) << 16;
  560. param.ccnt = c;
  561. edma_write_slot(dma->rx_channel, &param);
  562. if (dspi->cshold_bug)
  563. writew(spidat1 >> 16, dspi->base + SPIDAT1 + 2);
  564. edma_start(dma->rx_channel);
  565. edma_start(dma->tx_channel);
  566. set_io_bits(dspi->base + SPIINT, SPIINT_DMA_REQ_EN);
  567. }
  568. /* Wait for the transfer to complete */
  569. if (spicfg->io_type != SPI_IO_TYPE_POLL) {
  570. rt_completion_wait(&(dspi->done), RT_WAITING_FOREVER);
  571. } else {
  572. while (dspi->rcount > 0 || dspi->wcount > 0) {
  573. errors = davinci_spi_process_events(dspi);
  574. if (errors)
  575. break;
  576. cpu_relax();
  577. }
  578. }
  579. }
  580. if (msg->cs_release)
  581. davinci_spi_chipselect(spi, 1);
  582. clear_io_bits(dspi->base + SPIINT, SPIINT_MASKALL);
  583. if (spicfg->io_type == SPI_IO_TYPE_DMA) {
  584. clear_io_bits(dspi->base + SPIINT, SPIINT_DMA_REQ_EN);
  585. }
  586. clear_io_bits(dspi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
  587. set_io_bits(dspi->base + SPIGCR1, SPIGCR1_POWERDOWN_MASK);
  588. /*
  589. * Check for bit error, desync error,parity error,timeout error and
  590. * receive overflow errors
  591. */
  592. if (errors) {
  593. ret = davinci_spi_check_error(dspi, errors);
  594. rt_kprintf("%s: error reported but no error found!\n",
  595. spi->bus->parent.parent.name);
  596. return ret;
  597. }
  598. if (dspi->rcount != 0 || dspi->wcount != 0) {
  599. spi_dbg(sdev, "SPI data transfer error\n");
  600. return -RT_EIO;
  601. }
  602. return msg->length;
  603. }
  604. /**
  605. * davinci_spi_irq - Interrupt handler for SPI Master Controller
  606. * @irq: IRQ number for this SPI Master
  607. * @context_data: structure for SPI Master controller davinci_spi
  608. *
  609. * ISR will determine that interrupt arrives either for READ or WRITE command.
  610. * According to command it will do the appropriate action. It will check
  611. * transfer length and if it is not zero then dispatch transfer command again.
  612. * If transfer length is zero then it will indicate the COMPLETION so that
  613. * davinci_spi_bufs function can go ahead.
  614. */
  615. static void davinci_spi_irq(int irq, void *data)
  616. {
  617. struct davinci_spi *dspi = data;
  618. int status;
  619. status = davinci_spi_process_events(dspi);
  620. if (unlikely(status != 0))
  621. clear_io_bits(dspi->base + SPIINT, SPIINT_MASKINT);
  622. if ((!dspi->rcount && !dspi->wcount) || status)
  623. rt_completion_done(&dspi->done);
  624. }
  625. static int davinci_spi_request_dma(struct davinci_spi *dspi)
  626. {
  627. int r;
  628. struct davinci_spi_dma *dma = &dspi->dma;
  629. r = edma_alloc_channel(dma->rx_channel, davinci_spi_dma_callback, dspi,
  630. dma->eventq);
  631. if (r < 0) {
  632. rt_kprintf("Unable to request DMA channel for SPI RX\n");
  633. r = -RT_EFULL;
  634. goto rx_dma_failed;
  635. }
  636. r = edma_alloc_channel(dma->tx_channel, davinci_spi_dma_callback, dspi,
  637. dma->eventq);
  638. if (r < 0) {
  639. rt_kprintf("Unable to request DMA channel for SPI TX\n");
  640. r = -RT_EFULL;
  641. goto tx_dma_failed;
  642. }
  643. r = edma_alloc_slot(EDMA_CTLR(dma->tx_channel), EDMA_SLOT_ANY);
  644. if (r < 0) {
  645. rt_kprintf("Unable to request SPI TX DMA param slot\n");
  646. r = -RT_EFULL;
  647. goto param_failed;
  648. }
  649. dma->dummy_param_slot = r;
  650. edma_link(dma->dummy_param_slot, dma->dummy_param_slot);
  651. return 0;
  652. param_failed:
  653. edma_free_channel(dma->tx_channel);
  654. tx_dma_failed:
  655. edma_free_channel(dma->rx_channel);
  656. rx_dma_failed:
  657. return r;
  658. }
  659. static rt_err_t configure(struct rt_spi_device *device,
  660. struct rt_spi_configuration *configuration)
  661. {
  662. return davinci_spi_setup_transfer(device, configuration);
  663. }
  664. static rt_uint32_t xfer(struct rt_spi_device *device, struct rt_spi_message *message)
  665. {
  666. return davinci_spi_bufs(device, message);
  667. };
  668. static struct rt_spi_ops davinci_spi_ops =
  669. {
  670. configure,
  671. xfer
  672. };
  673. static void udelay (volatile rt_uint32_t us)
  674. {
  675. volatile rt_int32_t i;
  676. for (; us > 0; us--)
  677. {
  678. i = 5000;
  679. while(i > 0)
  680. {
  681. i--;
  682. }
  683. }
  684. }
  685. void spi_pin_cfg(void)
  686. {
  687. rt_uint32_t val;
  688. val = davinci_readl(PINMUX3);
  689. val |= 0x80000000; /* SPI1 */
  690. davinci_writel(val, PINMUX3);
  691. val = davinci_readl(PINMUX4);
  692. val &= 0xffffffc0; /* SPI1 */
  693. val |= 0x05;//0x00000015; /* SPI1 */
  694. davinci_writel(val, PINMUX4);
  695. }
  696. /**
  697. * davinci_spi_probe - probe function for SPI Master Controller
  698. * @pdev: platform_device structure which contains plateform specific data
  699. *
  700. * According to Linux Device Model this function will be invoked by Linux
  701. * with platform_device struct which contains the device specific info.
  702. * This function will map the SPI controller's memory, register IRQ,
  703. * Reset SPI controller and setting its registers to default value.
  704. * It will invoke spi_bitbang_start to create work queue so that client driver
  705. * can register transfer method to work queue.
  706. */
  707. static int davinci_spi_probe(struct davinci_spi *dspi, char *spi_bus_name)
  708. {
  709. int i = 0, ret = 0;
  710. u32 spipc0;
  711. spi_pin_cfg();
  712. psc_change_state(DAVINCI_DM365_LPSC_SPI1, PSC_ENABLE);
  713. dspi->base = DM3XX_SPI1_BASE;//spi;
  714. dspi->irq = IRQ_DM3XX_SPINT1_0;
  715. rt_hw_interrupt_install(dspi->irq, davinci_spi_irq, dspi, spi_bus_name);
  716. rt_hw_interrupt_umask(dspi->irq);
  717. dspi->clk = clk_get("SPICLK");
  718. dspi->version = SPI_VERSION_1;
  719. dspi->chip_sel[0] = 29;//SPI_INTERN_CS;
  720. dspi->chip_sel[1] = 0;//GPIO0
  721. dspi->dma.rx_channel = 15;
  722. dspi->dma.tx_channel = 14;
  723. dspi->dma.eventq = EVENTQ_3;
  724. ret = davinci_spi_request_dma(dspi);
  725. if (ret)
  726. goto err;
  727. rt_kprintf("%s: DMA: supported\n", spi_bus_name);
  728. rt_kprintf("%s: DMA: RX channel: %d, TX channel: %d, "
  729. "event queue: %d\n", spi_bus_name, dspi->dma.rx_channel,
  730. dspi->dma.tx_channel, dspi->dma.eventq);
  731. dspi->get_rx = davinci_spi_rx_buf_u8;
  732. dspi->get_tx = davinci_spi_tx_buf_u8;
  733. rt_completion_init(&dspi->done);
  734. /* Reset In/OUT SPI module */
  735. writel(0, dspi->base + SPIGCR0);
  736. udelay(100);
  737. writel(1, dspi->base + SPIGCR0);
  738. /* Set up SPIPC0. CS and ENA init is done in davinci_spi_setup */
  739. spipc0 = SPIPC0_DIFUN_MASK | SPIPC0_DOFUN_MASK | SPIPC0_CLKFUN_MASK;
  740. writel(spipc0, dspi->base + SPIPC0);
  741. /* initialize chip selects */
  742. for (i = 0; i < SPI_MAX_CHIPSELECT; i++) {
  743. if (dspi->chip_sel[i] != SPI_INTERN_CS)
  744. gpio_direction_output(dspi->chip_sel[i], 1);
  745. }
  746. if (0)
  747. writel(SPI_INTLVL_1, dspi->base + SPILVL);
  748. else
  749. writel(SPI_INTLVL_0, dspi->base + SPILVL);
  750. writel(CS_DEFAULT, dspi->base + SPIDEF);
  751. /* master mode default */
  752. set_io_bits(dspi->base + SPIGCR1, SPIGCR1_CLKMOD_MASK);
  753. set_io_bits(dspi->base + SPIGCR1, SPIGCR1_MASTER_MASK);
  754. set_io_bits(dspi->base + SPIGCR1, SPIGCR1_POWERDOWN_MASK);
  755. //set_io_bits(dspi->base + SPIGCR1, SPIGCR1_LOOPBACK_MASK);//LOOP BACK mode
  756. rt_kprintf("%s: Controller at 0x%p\n", spi_bus_name, dspi->base);
  757. dspi->parent.parent.user_data = dspi;
  758. return rt_spi_bus_register(&dspi->parent, spi_bus_name, &davinci_spi_ops);
  759. return ret;
  760. free_dma:
  761. edma_free_channel(dspi->dma.tx_channel);
  762. edma_free_channel(dspi->dma.rx_channel);
  763. edma_free_slot(dspi->dma.dummy_param_slot);
  764. err:
  765. return ret;
  766. }
  767. int rt_hw_spi_init(void)
  768. {
  769. /* register spi bus */
  770. {
  771. static struct davinci_spi dspi;
  772. rt_memset(&dspi, 0, sizeof(dspi));
  773. davinci_spi_probe(&dspi, "spi1");
  774. }
  775. /* attach cs */
  776. {
  777. static struct rt_spi_device spi_device;
  778. rt_spi_bus_attach_device(&spi_device, "spi10", "spi1", (void *)0);
  779. }
  780. {
  781. static struct rt_spi_device spi_device;
  782. rt_spi_bus_attach_device(&spi_device, "spi11", "spi1", (void *)1);
  783. }
  784. return 0;
  785. }
  786. INIT_DEVICE_EXPORT(rt_hw_spi_init);