davinci_spi.c 10.0 KB

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