spi-davinci.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  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. bits_per_word = cfg->data_width;
  276. hz = cfg->max_hz;
  277. /*
  278. * Assign function pointer to appropriate transfer method
  279. * 8bit, 16bit or 32bit transfer
  280. */
  281. if (bits_per_word <= 8 && bits_per_word >= 2) {
  282. dspi->get_rx = davinci_spi_rx_buf_u8;
  283. dspi->get_tx = davinci_spi_tx_buf_u8;
  284. dspi->bytes_per_word[chip_select] = 1;
  285. } else if (bits_per_word <= 16 && bits_per_word >= 2) {
  286. dspi->get_rx = davinci_spi_rx_buf_u16;
  287. dspi->get_tx = davinci_spi_tx_buf_u16;
  288. dspi->bytes_per_word[chip_select] = 2;
  289. } else
  290. return -RT_ERROR;
  291. /* Set up SPIFMTn register, unique to this chipselect. */
  292. prescale = davinci_spi_get_prescale(dspi, hz);
  293. if (prescale < 0)
  294. return prescale;
  295. spifmt = (prescale << SPIFMT_PRESCALE_SHIFT) | (bits_per_word & 0x1f);
  296. if (!(cfg->mode & RT_SPI_MSB))
  297. spifmt |= SPIFMT_SHIFTDIR_MASK;
  298. if (cfg->mode & RT_SPI_CPOL)
  299. spifmt |= SPIFMT_POLARITY_MASK;
  300. if (!(cfg->mode & RT_SPI_CPHA))
  301. spifmt |= SPIFMT_PHASE_MASK;
  302. /*
  303. * Version 1 hardware supports two basic SPI modes:
  304. * - Standard SPI mode uses 4 pins, with chipselect
  305. * - 3 pin SPI is a 4 pin variant without CS (SPI_NO_CS)
  306. * (distinct from SPI_3WIRE, with just one data wire;
  307. * or similar variants without MOSI or without MISO)
  308. *
  309. * Version 2 hardware supports an optional handshaking signal,
  310. * so it can support two more modes:
  311. * - 5 pin SPI variant is standard SPI plus SPI_READY
  312. * - 4 pin with enable is (SPI_READY | SPI_NO_CS)
  313. */
  314. if (dspi->version == SPI_VERSION_2) {
  315. u32 delay = 0;
  316. spifmt |= ((spicfg->wdelay << SPIFMT_WDELAY_SHIFT)
  317. & SPIFMT_WDELAY_MASK);
  318. if (spicfg->odd_parity)
  319. spifmt |= SPIFMT_ODD_PARITY_MASK;
  320. if (spicfg->parity_enable)
  321. spifmt |= SPIFMT_PARITYENA_MASK;
  322. if (spicfg->timer_disable) {
  323. spifmt |= SPIFMT_DISTIMER_MASK;
  324. } else {
  325. delay |= (spicfg->c2tdelay << SPIDELAY_C2TDELAY_SHIFT)
  326. & SPIDELAY_C2TDELAY_MASK;
  327. delay |= (spicfg->t2cdelay << SPIDELAY_T2CDELAY_SHIFT)
  328. & SPIDELAY_T2CDELAY_MASK;
  329. }
  330. if (cfg->mode & RT_SPI_READY) {
  331. spifmt |= SPIFMT_WAITENA_MASK;
  332. delay |= (spicfg->t2edelay << SPIDELAY_T2EDELAY_SHIFT)
  333. & SPIDELAY_T2EDELAY_MASK;
  334. delay |= (spicfg->c2edelay << SPIDELAY_C2EDELAY_SHIFT)
  335. & SPIDELAY_C2EDELAY_MASK;
  336. }
  337. writel(delay, dspi->base + SPIDELAY);
  338. }
  339. writel(spifmt, dspi->base + SPIFMT0);
  340. return 0;
  341. }
  342. #if 0
  343. /**
  344. * davinci_spi_setup - This functions will set default transfer method
  345. * @spi: spi device on which data transfer to be done
  346. *
  347. * This functions sets the default transfer method.
  348. */
  349. static int davinci_spi_setup(struct spi_device *spi)
  350. {
  351. int retval = 0;
  352. struct davinci_spi *dspi;
  353. struct davinci_spi_platform_data *pdata;
  354. dspi = spi_master_get_devdata(spi->master);
  355. pdata = dspi->pdata;
  356. /* if bits per word length is zero then set it default 8 */
  357. if (!spi->bits_per_word)
  358. spi->bits_per_word = 8;
  359. if (!(spi->mode & SPI_NO_CS)) {
  360. if ((pdata->chip_sel == NULL) ||
  361. (pdata->chip_sel[spi->chip_select] == SPI_INTERN_CS))
  362. set_io_bits(dspi->base + SPIPC0, 1 << spi->chip_select);
  363. }
  364. if (spi->mode & SPI_READY)
  365. set_io_bits(dspi->base + SPIPC0, SPIPC0_SPIENA_MASK);
  366. if (spi->mode & SPI_LOOP)
  367. set_io_bits(dspi->base + SPIGCR1, SPIGCR1_LOOPBACK_MASK);
  368. else
  369. clear_io_bits(dspi->base + SPIGCR1, SPIGCR1_LOOPBACK_MASK);
  370. return retval;
  371. }
  372. #endif
  373. static int davinci_spi_check_error(struct davinci_spi *dspi, int int_status)
  374. {
  375. struct rt_device *sdev = &dspi->parent.parent;
  376. if (int_status & SPIFLG_TIMEOUT_MASK) {
  377. spi_dbg(sdev, "SPI Time-out Error\n");
  378. return -RT_ETIMEOUT;
  379. }
  380. if (int_status & SPIFLG_DESYNC_MASK) {
  381. spi_dbg(sdev, "SPI Desynchronization Error\n");
  382. return -RT_EIO;
  383. }
  384. if (int_status & SPIFLG_BITERR_MASK) {
  385. spi_dbg(sdev, "SPI Bit error\n");
  386. return -RT_EIO;
  387. }
  388. if (dspi->version == SPI_VERSION_2) {
  389. if (int_status & SPIFLG_DLEN_ERR_MASK) {
  390. spi_dbg(sdev, "SPI Data Length Error\n");
  391. return -RT_EIO;
  392. }
  393. if (int_status & SPIFLG_PARERR_MASK) {
  394. spi_dbg(sdev, "SPI Parity Error\n");
  395. return -RT_EIO;
  396. }
  397. if (int_status & SPIFLG_OVRRUN_MASK) {
  398. spi_dbg(sdev, "SPI Data Overrun error\n");
  399. return -RT_EIO;
  400. }
  401. if (int_status & SPIFLG_BUF_INIT_ACTIVE_MASK) {
  402. spi_dbg(sdev, "SPI Buffer Init Active\n");
  403. return -RT_EBUSY;
  404. }
  405. }
  406. return 0;
  407. }
  408. /**
  409. * davinci_spi_process_events - check for and handle any SPI controller events
  410. * @dspi: the controller data
  411. *
  412. * This function will check the SPIFLG register and handle any events that are
  413. * detected there
  414. */
  415. static int davinci_spi_process_events(struct davinci_spi *dspi)
  416. {
  417. u32 buf, status, errors = 0, spidat1;
  418. buf = readl(dspi->base + SPIBUF);
  419. if (dspi->rcount > 0 && !(buf & SPIBUF_RXEMPTY_MASK)) {
  420. dspi->get_rx(buf & 0xFFFF, dspi);
  421. dspi->rcount--;
  422. }
  423. status = readl(dspi->base + SPIFLG);
  424. if (unlikely(status & SPIFLG_ERROR_MASK)) {
  425. errors = status & SPIFLG_ERROR_MASK;
  426. goto out;
  427. }
  428. if (dspi->wcount > 0 && !(buf & SPIBUF_TXFULL_MASK)) {
  429. spidat1 = readl(dspi->base + SPIDAT1);
  430. dspi->wcount--;
  431. spidat1 &= ~0xFFFF;
  432. spidat1 |= 0xFFFF & dspi->get_tx(dspi);
  433. writel(spidat1, dspi->base + SPIDAT1);
  434. }
  435. out:
  436. return errors;
  437. }
  438. static void davinci_spi_dma_callback(unsigned lch, u16 status, void *data)
  439. {
  440. struct davinci_spi *dspi = data;
  441. struct davinci_spi_dma *dma = &dspi->dma;
  442. edma_stop(lch);
  443. if (status == DMA_COMPLETE) {
  444. if (lch == dma->rx_channel)
  445. dspi->rcount = 0;
  446. if (lch == dma->tx_channel)
  447. dspi->wcount = 0;
  448. }
  449. if ((!dspi->wcount && !dspi->rcount) || (status != DMA_COMPLETE))
  450. rt_completion_done(&dspi->done);
  451. }
  452. /**
  453. * davinci_spi_bufs - functions which will handle transfer data
  454. * @spi: spi device on which data transfer to be done
  455. * @t: spi transfer in which transfer info is filled
  456. *
  457. * This function will put data to be transferred into data register
  458. * of SPI controller and then wait until the completion will be marked
  459. * by the IRQ Handler.
  460. */
  461. static int davinci_spi_bufs(struct rt_spi_device *spi, struct rt_spi_message *msg)
  462. {
  463. struct davinci_spi *dspi;
  464. int data_type, ret;
  465. u32 tx_data, spidat1;
  466. u32 errors = 0;
  467. struct davinci_spi_config *spicfg;
  468. unsigned rx_buf_count;
  469. struct rt_device *sdev;
  470. u8 chip_select = (u8)spi->parent.user_data;
  471. dspi = spi->bus->parent.user_data;
  472. spicfg = (struct davinci_spi_config *)dspi->controller_data;
  473. if (!spicfg)
  474. spicfg = &davinci_spi_default_cfg;
  475. sdev = &dspi->parent.parent;
  476. /* convert len to words based on bits_per_word */
  477. data_type = dspi->bytes_per_word[chip_select];
  478. dspi->tx = msg->send_buf;
  479. dspi->rx = msg->recv_buf;
  480. dspi->wcount = msg->length / data_type;
  481. dspi->rcount = dspi->wcount;
  482. spidat1 = readl(dspi->base + SPIDAT1);
  483. clear_io_bits(dspi->base + SPIGCR1, SPIGCR1_POWERDOWN_MASK);
  484. set_io_bits(dspi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
  485. rt_completion_init(&(dspi->done));
  486. if (msg->cs_take)
  487. davinci_spi_chipselect(spi, 0);
  488. if (spicfg->io_type == SPI_IO_TYPE_INTR)
  489. set_io_bits(dspi->base + SPIINT, SPIINT_MASKINT);
  490. if (msg->length > 0) {
  491. if (spicfg->io_type != SPI_IO_TYPE_DMA) {
  492. /* start the transfer */
  493. dspi->wcount--;
  494. tx_data = dspi->get_tx(dspi);
  495. spidat1 &= 0xFFFF0000;
  496. spidat1 |= tx_data & 0xFFFF;
  497. writel(spidat1, dspi->base + SPIDAT1);
  498. } else {
  499. struct davinci_spi_dma *dma;
  500. unsigned long tx_reg, rx_reg;
  501. struct edmacc_param param;
  502. void *rx_buf;
  503. int b, c;
  504. dma = &dspi->dma;
  505. tx_reg = (unsigned long)dspi->base + SPIDAT1;
  506. rx_reg = (unsigned long)dspi->base + SPIBUF;
  507. /*
  508. * Transmit DMA setup
  509. *
  510. * If there is transmit data, map the transmit buffer, set it
  511. * as the source of data and set the source B index to data
  512. * size. If there is no transmit data, set the transmit register
  513. * as the source of data, and set the source B index to zero.
  514. *
  515. * The destination is always the transmit register itself. And
  516. * the destination never increments.
  517. */
  518. if (msg->send_buf) {
  519. mmu_clean_dcache((rt_uint32_t)msg->send_buf, (rt_uint32_t)msg->length);
  520. }
  521. /*
  522. * If number of words is greater than 65535, then we need
  523. * to configure a 3 dimension transfer. Use the BCNTRLD
  524. * feature to allow for transfers that aren't even multiples
  525. * of 65535 (or any other possible b size) by first transferring
  526. * the remainder amount then grabbing the next N blocks of
  527. * 65535 words.
  528. */
  529. c = dspi->wcount / (SZ_64K - 1); /* N 65535 Blocks */
  530. b = dspi->wcount - c * (SZ_64K - 1); /* Remainder */
  531. if (b)
  532. c++;
  533. else
  534. b = SZ_64K - 1;
  535. param.opt = TCINTEN | EDMA_TCC(dma->tx_channel);
  536. param.src = msg->send_buf ? msg->send_buf : tx_reg;
  537. param.a_b_cnt = b << 16 | data_type;
  538. param.dst = tx_reg;
  539. param.src_dst_bidx = msg->send_buf ? data_type : 0;
  540. param.link_bcntrld = 0xffffffff;
  541. param.src_dst_cidx = msg->send_buf ? data_type : 0;
  542. param.ccnt = c;
  543. edma_write_slot(dma->tx_channel, &param);
  544. edma_link(dma->tx_channel, dma->dummy_param_slot);
  545. /*
  546. * Receive DMA setup
  547. *
  548. * If there is receive buffer, use it to receive data. If there
  549. * is none provided, use a temporary receive buffer. Set the
  550. * destination B index to 0 so effectively only one byte is used
  551. * in the temporary buffer (address does not increment).
  552. *
  553. * The source of receive data is the receive data register. The
  554. * source address never increments.
  555. */
  556. if (msg->recv_buf) {
  557. rx_buf = msg->recv_buf;
  558. rx_buf_count = msg->length;
  559. } else {
  560. rx_buf = dspi->rx_tmp_buf;
  561. rx_buf_count = sizeof(dspi->rx_tmp_buf);
  562. }
  563. mmu_invalidate_dcache((rt_uint32_t)rx_buf, (rt_uint32_t)rx_buf_count);
  564. param.opt = TCINTEN | EDMA_TCC(dma->rx_channel);
  565. param.src = rx_reg;
  566. param.a_b_cnt = b << 16 | data_type;
  567. param.dst = rx_buf;
  568. param.src_dst_bidx = (msg->recv_buf ? data_type : 0) << 16;
  569. param.link_bcntrld = 0xffffffff;
  570. param.src_dst_cidx = (msg->recv_buf ? data_type : 0) << 16;
  571. param.ccnt = c;
  572. edma_write_slot(dma->rx_channel, &param);
  573. if (dspi->cshold_bug)
  574. writew(spidat1 >> 16, dspi->base + SPIDAT1 + 2);
  575. edma_start(dma->rx_channel);
  576. edma_start(dma->tx_channel);
  577. set_io_bits(dspi->base + SPIINT, SPIINT_DMA_REQ_EN);
  578. }
  579. /* Wait for the transfer to complete */
  580. if (spicfg->io_type != SPI_IO_TYPE_POLL) {
  581. rt_completion_wait(&(dspi->done), RT_WAITING_FOREVER);
  582. } else {
  583. while (dspi->rcount > 0 || dspi->wcount > 0) {
  584. errors = davinci_spi_process_events(dspi);
  585. if (errors)
  586. break;
  587. cpu_relax();
  588. }
  589. }
  590. }
  591. if (msg->cs_release)
  592. davinci_spi_chipselect(spi, 1);
  593. clear_io_bits(dspi->base + SPIINT, SPIINT_MASKALL);
  594. if (spicfg->io_type == SPI_IO_TYPE_DMA) {
  595. clear_io_bits(dspi->base + SPIINT, SPIINT_DMA_REQ_EN);
  596. }
  597. clear_io_bits(dspi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
  598. set_io_bits(dspi->base + SPIGCR1, SPIGCR1_POWERDOWN_MASK);
  599. /*
  600. * Check for bit error, desync error,parity error,timeout error and
  601. * receive overflow errors
  602. */
  603. if (errors) {
  604. ret = davinci_spi_check_error(dspi, errors);
  605. rt_kprintf("%s: error reported but no error found!\n",
  606. spi->bus->parent.parent.name);
  607. return ret;
  608. }
  609. if (dspi->rcount != 0 || dspi->wcount != 0) {
  610. spi_dbg(sdev, "SPI data transfer error\n");
  611. return -RT_EIO;
  612. }
  613. return msg->length;
  614. }
  615. /**
  616. * davinci_spi_irq - Interrupt handler for SPI Master Controller
  617. * @irq: IRQ number for this SPI Master
  618. * @context_data: structure for SPI Master controller davinci_spi
  619. *
  620. * ISR will determine that interrupt arrives either for READ or WRITE command.
  621. * According to command it will do the appropriate action. It will check
  622. * transfer length and if it is not zero then dispatch transfer command again.
  623. * If transfer length is zero then it will indicate the COMPLETION so that
  624. * davinci_spi_bufs function can go ahead.
  625. */
  626. static void davinci_spi_irq(int irq, void *data)
  627. {
  628. struct davinci_spi *dspi = data;
  629. int status;
  630. status = davinci_spi_process_events(dspi);
  631. if (unlikely(status != 0))
  632. clear_io_bits(dspi->base + SPIINT, SPIINT_MASKINT);
  633. if ((!dspi->rcount && !dspi->wcount) || status)
  634. rt_completion_done(&dspi->done);
  635. }
  636. static int davinci_spi_request_dma(struct davinci_spi *dspi)
  637. {
  638. int r;
  639. struct davinci_spi_dma *dma = &dspi->dma;
  640. r = edma_alloc_channel(dma->rx_channel, davinci_spi_dma_callback, dspi,
  641. dma->eventq);
  642. if (r < 0) {
  643. rt_kprintf("Unable to request DMA channel for SPI RX\n");
  644. r = -RT_EFULL;
  645. goto rx_dma_failed;
  646. }
  647. r = edma_alloc_channel(dma->tx_channel, davinci_spi_dma_callback, dspi,
  648. dma->eventq);
  649. if (r < 0) {
  650. rt_kprintf("Unable to request DMA channel for SPI TX\n");
  651. r = -RT_EFULL;
  652. goto tx_dma_failed;
  653. }
  654. r = edma_alloc_slot(EDMA_CTLR(dma->tx_channel), EDMA_SLOT_ANY);
  655. if (r < 0) {
  656. rt_kprintf("Unable to request SPI TX DMA param slot\n");
  657. r = -RT_EFULL;
  658. goto param_failed;
  659. }
  660. dma->dummy_param_slot = r;
  661. edma_link(dma->dummy_param_slot, dma->dummy_param_slot);
  662. return 0;
  663. param_failed:
  664. edma_free_channel(dma->tx_channel);
  665. tx_dma_failed:
  666. edma_free_channel(dma->rx_channel);
  667. rx_dma_failed:
  668. return r;
  669. }
  670. static rt_err_t configure(struct rt_spi_device *device,
  671. struct rt_spi_configuration *configuration)
  672. {
  673. return davinci_spi_setup_transfer(device, configuration);
  674. }
  675. static rt_uint32_t xfer(struct rt_spi_device *device, struct rt_spi_message *message)
  676. {
  677. return davinci_spi_bufs(device, message);
  678. };
  679. static struct rt_spi_ops davinci_spi_ops =
  680. {
  681. configure,
  682. xfer
  683. };
  684. static void udelay (volatile rt_uint32_t us)
  685. {
  686. volatile rt_int32_t i;
  687. for (; us > 0; us--)
  688. {
  689. i = 5000;
  690. while(i > 0)
  691. {
  692. i--;
  693. }
  694. }
  695. }
  696. void spi_pin_cfg(void)
  697. {
  698. rt_uint32_t val;
  699. val = davinci_readl(PINMUX3);
  700. val |= 0x80000000; /* SPI1 */
  701. davinci_writel(val, PINMUX3);
  702. val = davinci_readl(PINMUX4);
  703. val &= 0xffffffc0; /* SPI1 */
  704. val |= 0x05;//0x00000015; /* SPI1 */
  705. davinci_writel(val, PINMUX4);
  706. }
  707. /**
  708. * davinci_spi_probe - probe function for SPI Master Controller
  709. * @pdev: platform_device structure which contains plateform specific data
  710. *
  711. * According to Linux Device Model this function will be invoked by Linux
  712. * with platform_device struct which contains the device specific info.
  713. * This function will map the SPI controller's memory, register IRQ,
  714. * Reset SPI controller and setting its registers to default value.
  715. * It will invoke spi_bitbang_start to create work queue so that client driver
  716. * can register transfer method to work queue.
  717. */
  718. static int davinci_spi_probe(struct davinci_spi *dspi, char *spi_bus_name)
  719. {
  720. int i = 0, ret = 0;
  721. u32 spipc0;
  722. spi_pin_cfg();
  723. psc_change_state(DAVINCI_DM365_LPSC_SPI1, PSC_ENABLE);
  724. dspi->base = DM3XX_SPI1_BASE;//spi;
  725. dspi->irq = IRQ_DM3XX_SPINT1_0;
  726. rt_hw_interrupt_install(dspi->irq, davinci_spi_irq, dspi, spi_bus_name);
  727. rt_hw_interrupt_umask(dspi->irq);
  728. dspi->clk = clk_get("SPICLK");
  729. dspi->version = SPI_VERSION_1;
  730. dspi->chip_sel[0] = 29;//SPI_INTERN_CS;
  731. dspi->chip_sel[1] = 0;//GPIO0
  732. dspi->dma.rx_channel = 15;
  733. dspi->dma.tx_channel = 14;
  734. dspi->dma.eventq = EVENTQ_3;
  735. ret = davinci_spi_request_dma(dspi);
  736. if (ret)
  737. goto err;
  738. rt_kprintf("%s: DMA: supported\n", spi_bus_name);
  739. rt_kprintf("%s: DMA: RX channel: %d, TX channel: %d, "
  740. "event queue: %d\n", spi_bus_name, dspi->dma.rx_channel,
  741. dspi->dma.tx_channel, dspi->dma.eventq);
  742. dspi->get_rx = davinci_spi_rx_buf_u8;
  743. dspi->get_tx = davinci_spi_tx_buf_u8;
  744. rt_completion_init(&dspi->done);
  745. /* Reset In/OUT SPI module */
  746. writel(0, dspi->base + SPIGCR0);
  747. udelay(100);
  748. writel(1, dspi->base + SPIGCR0);
  749. /* Set up SPIPC0. CS and ENA init is done in davinci_spi_setup */
  750. spipc0 = SPIPC0_DIFUN_MASK | SPIPC0_DOFUN_MASK | SPIPC0_CLKFUN_MASK;
  751. writel(spipc0, dspi->base + SPIPC0);
  752. /* initialize chip selects */
  753. for (i = 0; i < SPI_MAX_CHIPSELECT; i++) {
  754. if (dspi->chip_sel[i] != SPI_INTERN_CS)
  755. gpio_direction_output(dspi->chip_sel[i], 1);
  756. }
  757. if (0)
  758. writel(SPI_INTLVL_1, dspi->base + SPILVL);
  759. else
  760. writel(SPI_INTLVL_0, dspi->base + SPILVL);
  761. writel(CS_DEFAULT, dspi->base + SPIDEF);
  762. /* master mode default */
  763. set_io_bits(dspi->base + SPIGCR1, SPIGCR1_CLKMOD_MASK);
  764. set_io_bits(dspi->base + SPIGCR1, SPIGCR1_MASTER_MASK);
  765. set_io_bits(dspi->base + SPIGCR1, SPIGCR1_POWERDOWN_MASK);
  766. //set_io_bits(dspi->base + SPIGCR1, SPIGCR1_LOOPBACK_MASK);//LOOP BACK mode
  767. rt_kprintf("%s: Controller at 0x%p\n", spi_bus_name, dspi->base);
  768. dspi->parent.parent.user_data = dspi;
  769. return rt_spi_bus_register(&dspi->parent, spi_bus_name, &davinci_spi_ops);
  770. return ret;
  771. free_dma:
  772. edma_free_channel(dspi->dma.tx_channel);
  773. edma_free_channel(dspi->dma.rx_channel);
  774. edma_free_slot(dspi->dma.dummy_param_slot);
  775. err:
  776. return ret;
  777. }
  778. int rt_hw_spi_init(void)
  779. {
  780. /* register spi bus */
  781. {
  782. static struct davinci_spi dspi;
  783. rt_memset(&dspi, 0, sizeof(dspi));
  784. davinci_spi_probe(&dspi, "spi1");
  785. }
  786. /* attach cs */
  787. {
  788. static struct rt_spi_device spi_device;
  789. rt_spi_bus_attach_device(&spi_device, "spi10", "spi1", (void *)0);
  790. }
  791. {
  792. static struct rt_spi_device spi_device;
  793. rt_spi_bus_attach_device(&spi_device, "spi11", "spi1", (void *)1);
  794. }
  795. return 0;
  796. }