davinci_spi.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*
  2. * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
  3. *
  4. * Driver for SPI controller on DaVinci. Based on atmel_spi.c
  5. * by Atmel Corporation
  6. *
  7. * Copyright (C) 2007 Atmel Corporation
  8. *
  9. * SPDX-License-Identifier: GPL-2.0+
  10. */
  11. #include <common.h>
  12. #include <spi.h>
  13. #include <malloc.h>
  14. #include <asm/io.h>
  15. #include <asm/arch/hardware.h>
  16. struct davinci_spi_regs {
  17. dv_reg gcr0; /* 0x00 */
  18. dv_reg gcr1; /* 0x04 */
  19. dv_reg int0; /* 0x08 */
  20. dv_reg lvl; /* 0x0c */
  21. dv_reg flg; /* 0x10 */
  22. dv_reg pc0; /* 0x14 */
  23. dv_reg pc1; /* 0x18 */
  24. dv_reg pc2; /* 0x1c */
  25. dv_reg pc3; /* 0x20 */
  26. dv_reg pc4; /* 0x24 */
  27. dv_reg pc5; /* 0x28 */
  28. dv_reg rsvd[3];
  29. dv_reg dat0; /* 0x38 */
  30. dv_reg dat1; /* 0x3c */
  31. dv_reg buf; /* 0x40 */
  32. dv_reg emu; /* 0x44 */
  33. dv_reg delay; /* 0x48 */
  34. dv_reg def; /* 0x4c */
  35. dv_reg fmt0; /* 0x50 */
  36. dv_reg fmt1; /* 0x54 */
  37. dv_reg fmt2; /* 0x58 */
  38. dv_reg fmt3; /* 0x5c */
  39. dv_reg intvec0; /* 0x60 */
  40. dv_reg intvec1; /* 0x64 */
  41. };
  42. #define BIT(x) (1 << (x))
  43. /* SPIGCR0 */
  44. #define SPIGCR0_SPIENA_MASK 0x1
  45. #define SPIGCR0_SPIRST_MASK 0x0
  46. /* SPIGCR0 */
  47. #define SPIGCR1_CLKMOD_MASK BIT(1)
  48. #define SPIGCR1_MASTER_MASK BIT(0)
  49. #define SPIGCR1_SPIENA_MASK BIT(24)
  50. /* SPIPC0 */
  51. #define SPIPC0_DIFUN_MASK BIT(11) /* SIMO */
  52. #define SPIPC0_DOFUN_MASK BIT(10) /* SOMI */
  53. #define SPIPC0_CLKFUN_MASK BIT(9) /* CLK */
  54. #define SPIPC0_EN0FUN_MASK BIT(0)
  55. /* SPIFMT0 */
  56. #define SPIFMT_SHIFTDIR_SHIFT 20
  57. #define SPIFMT_POLARITY_SHIFT 17
  58. #define SPIFMT_PHASE_SHIFT 16
  59. #define SPIFMT_PRESCALE_SHIFT 8
  60. /* SPIDAT1 */
  61. #define SPIDAT1_CSHOLD_SHIFT 28
  62. #define SPIDAT1_CSNR_SHIFT 16
  63. /* SPIDELAY */
  64. #define SPI_C2TDELAY_SHIFT 24
  65. #define SPI_T2CDELAY_SHIFT 16
  66. /* SPIBUF */
  67. #define SPIBUF_RXEMPTY_MASK BIT(31)
  68. #define SPIBUF_TXFULL_MASK BIT(29)
  69. /* SPIDEF */
  70. #define SPIDEF_CSDEF0_MASK BIT(0)
  71. #define SPI0_BUS 0
  72. #define SPI0_BASE CONFIG_SYS_SPI_BASE
  73. /*
  74. * Define default SPI0_NUM_CS as 1 for existing platforms that uses this
  75. * driver. Platform can configure number of CS using CONFIG_SYS_SPI0_NUM_CS
  76. * if more than one CS is supported and by defining CONFIG_SYS_SPI0.
  77. */
  78. #ifndef CONFIG_SYS_SPI0
  79. #define SPI0_NUM_CS 1
  80. #else
  81. #define SPI0_NUM_CS CONFIG_SYS_SPI0_NUM_CS
  82. #endif
  83. /*
  84. * define CONFIG_SYS_SPI1 when platform has spi-1 device (bus #1) and
  85. * CONFIG_SYS_SPI1_NUM_CS defines number of CS on this bus
  86. */
  87. #ifdef CONFIG_SYS_SPI1
  88. #define SPI1_BUS 1
  89. #define SPI1_NUM_CS CONFIG_SYS_SPI1_NUM_CS
  90. #define SPI1_BASE CONFIG_SYS_SPI1_BASE
  91. #endif
  92. /*
  93. * define CONFIG_SYS_SPI2 when platform has spi-2 device (bus #2) and
  94. * CONFIG_SYS_SPI2_NUM_CS defines number of CS on this bus
  95. */
  96. #ifdef CONFIG_SYS_SPI2
  97. #define SPI2_BUS 2
  98. #define SPI2_NUM_CS CONFIG_SYS_SPI2_NUM_CS
  99. #define SPI2_BASE CONFIG_SYS_SPI2_BASE
  100. #endif
  101. struct davinci_spi_slave {
  102. struct spi_slave slave;
  103. struct davinci_spi_regs *regs;
  104. unsigned int freq;
  105. };
  106. static inline struct davinci_spi_slave *to_davinci_spi(struct spi_slave *slave)
  107. {
  108. return container_of(slave, struct davinci_spi_slave, slave);
  109. }
  110. void spi_init()
  111. {
  112. /* do nothing */
  113. }
  114. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  115. unsigned int max_hz, unsigned int mode)
  116. {
  117. struct davinci_spi_slave *ds;
  118. if (!spi_cs_is_valid(bus, cs))
  119. return NULL;
  120. ds = spi_alloc_slave(struct davinci_spi_slave, bus, cs);
  121. if (!ds)
  122. return NULL;
  123. switch (bus) {
  124. case SPI0_BUS:
  125. ds->regs = (struct davinci_spi_regs *)SPI0_BASE;
  126. break;
  127. #ifdef CONFIG_SYS_SPI1
  128. case SPI1_BUS:
  129. ds->regs = (struct davinci_spi_regs *)SPI1_BASE;
  130. break;
  131. #endif
  132. #ifdef CONFIG_SYS_SPI2
  133. case SPI2_BUS:
  134. ds->regs = (struct davinci_spi_regs *)SPI2_BASE;
  135. break;
  136. #endif
  137. default: /* Invalid bus number */
  138. return NULL;
  139. }
  140. ds->freq = max_hz;
  141. return &ds->slave;
  142. }
  143. void spi_free_slave(struct spi_slave *slave)
  144. {
  145. struct davinci_spi_slave *ds = to_davinci_spi(slave);
  146. free(ds);
  147. }
  148. int spi_claim_bus(struct spi_slave *slave)
  149. {
  150. struct davinci_spi_slave *ds = to_davinci_spi(slave);
  151. unsigned int scalar;
  152. /* Enable the SPI hardware */
  153. writel(SPIGCR0_SPIRST_MASK, &ds->regs->gcr0);
  154. udelay(1000);
  155. writel(SPIGCR0_SPIENA_MASK, &ds->regs->gcr0);
  156. /* Set master mode, powered up and not activated */
  157. writel(SPIGCR1_MASTER_MASK | SPIGCR1_CLKMOD_MASK, &ds->regs->gcr1);
  158. /* CS, CLK, SIMO and SOMI are functional pins */
  159. writel(((1 << slave->cs) | SPIPC0_CLKFUN_MASK |
  160. SPIPC0_DOFUN_MASK | SPIPC0_DIFUN_MASK), &ds->regs->pc0);
  161. /* setup format */
  162. scalar = ((CONFIG_SYS_SPI_CLK / ds->freq) - 1) & 0xFF;
  163. /*
  164. * Use following format:
  165. * character length = 8,
  166. * clock signal delayed by half clk cycle,
  167. * clock low in idle state - Mode 0,
  168. * MSB shifted out first
  169. */
  170. writel(8 | (scalar << SPIFMT_PRESCALE_SHIFT) |
  171. (1 << SPIFMT_PHASE_SHIFT), &ds->regs->fmt0);
  172. /*
  173. * Including a minor delay. No science here. Should be good even with
  174. * no delay
  175. */
  176. writel((50 << SPI_C2TDELAY_SHIFT) |
  177. (50 << SPI_T2CDELAY_SHIFT), &ds->regs->delay);
  178. /* default chip select register */
  179. writel(SPIDEF_CSDEF0_MASK, &ds->regs->def);
  180. /* no interrupts */
  181. writel(0, &ds->regs->int0);
  182. writel(0, &ds->regs->lvl);
  183. /* enable SPI */
  184. writel((readl(&ds->regs->gcr1) | SPIGCR1_SPIENA_MASK), &ds->regs->gcr1);
  185. return 0;
  186. }
  187. void spi_release_bus(struct spi_slave *slave)
  188. {
  189. struct davinci_spi_slave *ds = to_davinci_spi(slave);
  190. /* Disable the SPI hardware */
  191. writel(SPIGCR0_SPIRST_MASK, &ds->regs->gcr0);
  192. }
  193. /*
  194. * This functions needs to act like a macro to avoid pipeline reloads in the
  195. * loops below. Use always_inline. This gains us about 160KiB/s and the bloat
  196. * appears to be zero bytes (da830).
  197. */
  198. __attribute__((always_inline))
  199. static inline u32 davinci_spi_xfer_data(struct davinci_spi_slave *ds, u32 data)
  200. {
  201. u32 buf_reg_val;
  202. /* send out data */
  203. writel(data, &ds->regs->dat1);
  204. /* wait for the data to clock in/out */
  205. while ((buf_reg_val = readl(&ds->regs->buf)) & SPIBUF_RXEMPTY_MASK)
  206. ;
  207. return buf_reg_val;
  208. }
  209. static int davinci_spi_read(struct spi_slave *slave, unsigned int len,
  210. u8 *rxp, unsigned long flags)
  211. {
  212. struct davinci_spi_slave *ds = to_davinci_spi(slave);
  213. unsigned int data1_reg_val;
  214. /* enable CS hold, CS[n] and clear the data bits */
  215. data1_reg_val = ((1 << SPIDAT1_CSHOLD_SHIFT) |
  216. (slave->cs << SPIDAT1_CSNR_SHIFT));
  217. /* wait till TXFULL is deasserted */
  218. while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK)
  219. ;
  220. /* preload the TX buffer to avoid clock starvation */
  221. writel(data1_reg_val, &ds->regs->dat1);
  222. /* keep reading 1 byte until only 1 byte left */
  223. while ((len--) > 1)
  224. *rxp++ = davinci_spi_xfer_data(ds, data1_reg_val);
  225. /* clear CS hold when we reach the end */
  226. if (flags & SPI_XFER_END)
  227. data1_reg_val &= ~(1 << SPIDAT1_CSHOLD_SHIFT);
  228. /* read the last byte */
  229. *rxp = davinci_spi_xfer_data(ds, data1_reg_val);
  230. return 0;
  231. }
  232. static int davinci_spi_write(struct spi_slave *slave, unsigned int len,
  233. const u8 *txp, unsigned long flags)
  234. {
  235. struct davinci_spi_slave *ds = to_davinci_spi(slave);
  236. unsigned int data1_reg_val;
  237. /* enable CS hold and clear the data bits */
  238. data1_reg_val = ((1 << SPIDAT1_CSHOLD_SHIFT) |
  239. (slave->cs << SPIDAT1_CSNR_SHIFT));
  240. /* wait till TXFULL is deasserted */
  241. while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK)
  242. ;
  243. /* preload the TX buffer to avoid clock starvation */
  244. if (len > 2) {
  245. writel(data1_reg_val | *txp++, &ds->regs->dat1);
  246. len--;
  247. }
  248. /* keep writing 1 byte until only 1 byte left */
  249. while ((len--) > 1)
  250. davinci_spi_xfer_data(ds, data1_reg_val | *txp++);
  251. /* clear CS hold when we reach the end */
  252. if (flags & SPI_XFER_END)
  253. data1_reg_val &= ~(1 << SPIDAT1_CSHOLD_SHIFT);
  254. /* write the last byte */
  255. davinci_spi_xfer_data(ds, data1_reg_val | *txp);
  256. return 0;
  257. }
  258. #ifndef CONFIG_SPI_HALF_DUPLEX
  259. static int davinci_spi_read_write(struct spi_slave *slave, unsigned int len,
  260. u8 *rxp, const u8 *txp, unsigned long flags)
  261. {
  262. struct davinci_spi_slave *ds = to_davinci_spi(slave);
  263. unsigned int data1_reg_val;
  264. /* enable CS hold and clear the data bits */
  265. data1_reg_val = ((1 << SPIDAT1_CSHOLD_SHIFT) |
  266. (slave->cs << SPIDAT1_CSNR_SHIFT));
  267. /* wait till TXFULL is deasserted */
  268. while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK)
  269. ;
  270. /* keep reading and writing 1 byte until only 1 byte left */
  271. while ((len--) > 1)
  272. *rxp++ = davinci_spi_xfer_data(ds, data1_reg_val | *txp++);
  273. /* clear CS hold when we reach the end */
  274. if (flags & SPI_XFER_END)
  275. data1_reg_val &= ~(1 << SPIDAT1_CSHOLD_SHIFT);
  276. /* read and write the last byte */
  277. *rxp = davinci_spi_xfer_data(ds, data1_reg_val | *txp);
  278. return 0;
  279. }
  280. #endif
  281. int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
  282. const void *dout, void *din, unsigned long flags)
  283. {
  284. unsigned int len;
  285. if (bitlen == 0)
  286. /* Finish any previously submitted transfers */
  287. goto out;
  288. /*
  289. * It's not clear how non-8-bit-aligned transfers are supposed to be
  290. * represented as a stream of bytes...this is a limitation of
  291. * the current SPI interface - here we terminate on receiving such a
  292. * transfer request.
  293. */
  294. if (bitlen % 8) {
  295. /* Errors always terminate an ongoing transfer */
  296. flags |= SPI_XFER_END;
  297. goto out;
  298. }
  299. len = bitlen / 8;
  300. if (!dout)
  301. return davinci_spi_read(slave, len, din, flags);
  302. else if (!din)
  303. return davinci_spi_write(slave, len, dout, flags);
  304. #ifndef CONFIG_SPI_HALF_DUPLEX
  305. else
  306. return davinci_spi_read_write(slave, len, din, dout, flags);
  307. #else
  308. printf("SPI full duplex transaction requested with "
  309. "CONFIG_SPI_HALF_DUPLEX defined.\n");
  310. flags |= SPI_XFER_END;
  311. #endif
  312. out:
  313. if (flags & SPI_XFER_END) {
  314. u8 dummy = 0;
  315. davinci_spi_write(slave, 1, &dummy, flags);
  316. }
  317. return 0;
  318. }
  319. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  320. {
  321. int ret = 0;
  322. switch (bus) {
  323. case SPI0_BUS:
  324. if (cs < SPI0_NUM_CS)
  325. ret = 1;
  326. break;
  327. #ifdef CONFIG_SYS_SPI1
  328. case SPI1_BUS:
  329. if (cs < SPI1_NUM_CS)
  330. ret = 1;
  331. break;
  332. #endif
  333. #ifdef CONFIG_SYS_SPI2
  334. case SPI2_BUS:
  335. if (cs < SPI2_NUM_CS)
  336. ret = 1;
  337. break;
  338. #endif
  339. default:
  340. /* Invalid bus number. Do nothing */
  341. break;
  342. }
  343. return ret;
  344. }
  345. void spi_cs_activate(struct spi_slave *slave)
  346. {
  347. /* do nothing */
  348. }
  349. void spi_cs_deactivate(struct spi_slave *slave)
  350. {
  351. /* do nothing */
  352. }