davinci_spi.c 10.0 KB

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