spi-davinci.c 25 KB

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