cf_spi.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. *
  4. * (C) Copyright 2000-2003
  5. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  6. *
  7. * Copyright (C) 2004-2009 Freescale Semiconductor, Inc.
  8. * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
  9. */
  10. #include <common.h>
  11. #include <spi.h>
  12. #include <malloc.h>
  13. #include <asm/immap.h>
  14. struct cf_spi_slave {
  15. struct spi_slave slave;
  16. uint baudrate;
  17. int charbit;
  18. };
  19. extern void cfspi_port_conf(void);
  20. extern int cfspi_claim_bus(uint bus, uint cs);
  21. extern void cfspi_release_bus(uint bus, uint cs);
  22. DECLARE_GLOBAL_DATA_PTR;
  23. #ifndef CONFIG_SPI_IDLE_VAL
  24. #if defined(CONFIG_SPI_MMC)
  25. #define CONFIG_SPI_IDLE_VAL 0xFFFF
  26. #else
  27. #define CONFIG_SPI_IDLE_VAL 0x0
  28. #endif
  29. #endif
  30. #if defined(CONFIG_CF_DSPI)
  31. /* DSPI specific mode */
  32. #define SPI_MODE_MOD 0x00200000
  33. #define SPI_DBLRATE 0x00100000
  34. static inline struct cf_spi_slave *to_cf_spi_slave(struct spi_slave *slave)
  35. {
  36. return container_of(slave, struct cf_spi_slave, slave);
  37. }
  38. static void cfspi_init(void)
  39. {
  40. volatile dspi_t *dspi = (dspi_t *) MMAP_DSPI;
  41. cfspi_port_conf(); /* port configuration */
  42. dspi->mcr = DSPI_MCR_MSTR | DSPI_MCR_CSIS7 | DSPI_MCR_CSIS6 |
  43. DSPI_MCR_CSIS5 | DSPI_MCR_CSIS4 | DSPI_MCR_CSIS3 |
  44. DSPI_MCR_CSIS2 | DSPI_MCR_CSIS1 | DSPI_MCR_CSIS0 |
  45. DSPI_MCR_CRXF | DSPI_MCR_CTXF;
  46. /* Default setting in platform configuration */
  47. #ifdef CONFIG_SYS_DSPI_CTAR0
  48. dspi->ctar[0] = CONFIG_SYS_DSPI_CTAR0;
  49. #endif
  50. #ifdef CONFIG_SYS_DSPI_CTAR1
  51. dspi->ctar[1] = CONFIG_SYS_DSPI_CTAR1;
  52. #endif
  53. #ifdef CONFIG_SYS_DSPI_CTAR2
  54. dspi->ctar[2] = CONFIG_SYS_DSPI_CTAR2;
  55. #endif
  56. #ifdef CONFIG_SYS_DSPI_CTAR3
  57. dspi->ctar[3] = CONFIG_SYS_DSPI_CTAR3;
  58. #endif
  59. #ifdef CONFIG_SYS_DSPI_CTAR4
  60. dspi->ctar[4] = CONFIG_SYS_DSPI_CTAR4;
  61. #endif
  62. #ifdef CONFIG_SYS_DSPI_CTAR5
  63. dspi->ctar[5] = CONFIG_SYS_DSPI_CTAR5;
  64. #endif
  65. #ifdef CONFIG_SYS_DSPI_CTAR6
  66. dspi->ctar[6] = CONFIG_SYS_DSPI_CTAR6;
  67. #endif
  68. #ifdef CONFIG_SYS_DSPI_CTAR7
  69. dspi->ctar[7] = CONFIG_SYS_DSPI_CTAR7;
  70. #endif
  71. }
  72. static void cfspi_tx(u32 ctrl, u16 data)
  73. {
  74. volatile dspi_t *dspi = (dspi_t *) MMAP_DSPI;
  75. while ((dspi->sr & 0x0000F000) >= 4) ;
  76. dspi->tfr = (ctrl | data);
  77. }
  78. static u16 cfspi_rx(void)
  79. {
  80. volatile dspi_t *dspi = (dspi_t *) MMAP_DSPI;
  81. while ((dspi->sr & 0x000000F0) == 0) ;
  82. return (dspi->rfr & 0xFFFF);
  83. }
  84. static int cfspi_xfer(struct spi_slave *slave, uint bitlen, const void *dout,
  85. void *din, ulong flags)
  86. {
  87. struct cf_spi_slave *cfslave = to_cf_spi_slave(slave);
  88. u16 *spi_rd16 = NULL, *spi_wr16 = NULL;
  89. u8 *spi_rd = NULL, *spi_wr = NULL;
  90. static u32 ctrl = 0;
  91. uint len = bitlen >> 3;
  92. if (cfslave->charbit == 16) {
  93. bitlen >>= 1;
  94. spi_wr16 = (u16 *) dout;
  95. spi_rd16 = (u16 *) din;
  96. } else {
  97. spi_wr = (u8 *) dout;
  98. spi_rd = (u8 *) din;
  99. }
  100. if ((flags & SPI_XFER_BEGIN) == SPI_XFER_BEGIN)
  101. ctrl |= DSPI_TFR_CONT;
  102. ctrl = (ctrl & 0xFF000000) | ((1 << slave->cs) << 16);
  103. if (len > 1) {
  104. int tmp_len = len - 1;
  105. while (tmp_len--) {
  106. if (dout != NULL) {
  107. if (cfslave->charbit == 16)
  108. cfspi_tx(ctrl, *spi_wr16++);
  109. else
  110. cfspi_tx(ctrl, *spi_wr++);
  111. cfspi_rx();
  112. }
  113. if (din != NULL) {
  114. cfspi_tx(ctrl, CONFIG_SPI_IDLE_VAL);
  115. if (cfslave->charbit == 16)
  116. *spi_rd16++ = cfspi_rx();
  117. else
  118. *spi_rd++ = cfspi_rx();
  119. }
  120. }
  121. len = 1; /* remaining byte */
  122. }
  123. if ((flags & SPI_XFER_END) == SPI_XFER_END)
  124. ctrl &= ~DSPI_TFR_CONT;
  125. if (len) {
  126. if (dout != NULL) {
  127. if (cfslave->charbit == 16)
  128. cfspi_tx(ctrl, *spi_wr16);
  129. else
  130. cfspi_tx(ctrl, *spi_wr);
  131. cfspi_rx();
  132. }
  133. if (din != NULL) {
  134. cfspi_tx(ctrl, CONFIG_SPI_IDLE_VAL);
  135. if (cfslave->charbit == 16)
  136. *spi_rd16 = cfspi_rx();
  137. else
  138. *spi_rd = cfspi_rx();
  139. }
  140. } else {
  141. /* dummy read */
  142. cfspi_tx(ctrl, CONFIG_SPI_IDLE_VAL);
  143. cfspi_rx();
  144. }
  145. return 0;
  146. }
  147. static struct spi_slave *cfspi_setup_slave(struct cf_spi_slave *cfslave,
  148. uint mode)
  149. {
  150. /*
  151. * bit definition for mode:
  152. * bit 31 - 28: Transfer size 3 to 16 bits
  153. * 27 - 26: PCS to SCK delay prescaler
  154. * 25 - 24: After SCK delay prescaler
  155. * 23 - 22: Delay after transfer prescaler
  156. * 21 : Allow overwrite for bit 31-22 and bit 20-8
  157. * 20 : Double baud rate
  158. * 19 - 16: PCS to SCK delay scaler
  159. * 15 - 12: After SCK delay scaler
  160. * 11 - 8: Delay after transfer scaler
  161. * 7 - 0: SPI_CPHA, SPI_CPOL, SPI_LSB_FIRST
  162. */
  163. volatile dspi_t *dspi = (dspi_t *) MMAP_DSPI;
  164. int prescaler[] = { 2, 3, 5, 7 };
  165. int scaler[] = {
  166. 2, 4, 6, 8,
  167. 16, 32, 64, 128,
  168. 256, 512, 1024, 2048,
  169. 4096, 8192, 16384, 32768
  170. };
  171. int i, j, pbrcnt, brcnt, diff, tmp, dbr = 0;
  172. int best_i, best_j, bestmatch = 0x7FFFFFFF, baud_speed;
  173. u32 bus_setup = 0;
  174. tmp = (prescaler[3] * scaler[15]);
  175. /* Maximum and minimum baudrate it can handle */
  176. if ((cfslave->baudrate > (gd->bus_clk >> 1)) ||
  177. (cfslave->baudrate < (gd->bus_clk / tmp))) {
  178. printf("Exceed baudrate limitation: Max %d - Min %d\n",
  179. (int)(gd->bus_clk >> 1), (int)(gd->bus_clk / tmp));
  180. return NULL;
  181. }
  182. /* Activate Double Baud when it exceed 1/4 the bus clk */
  183. if ((CONFIG_SYS_DSPI_CTAR0 & DSPI_CTAR_DBR) ||
  184. (cfslave->baudrate > (gd->bus_clk / (prescaler[0] * scaler[0])))) {
  185. bus_setup |= DSPI_CTAR_DBR;
  186. dbr = 1;
  187. }
  188. if (mode & SPI_CPOL)
  189. bus_setup |= DSPI_CTAR_CPOL;
  190. if (mode & SPI_CPHA)
  191. bus_setup |= DSPI_CTAR_CPHA;
  192. if (mode & SPI_LSB_FIRST)
  193. bus_setup |= DSPI_CTAR_LSBFE;
  194. /* Overwrite default value set in platform configuration file */
  195. if (mode & SPI_MODE_MOD) {
  196. if ((mode & 0xF0000000) == 0)
  197. bus_setup |=
  198. dspi->ctar[cfslave->slave.bus] & 0x78000000;
  199. else
  200. bus_setup |= ((mode & 0xF0000000) >> 1);
  201. /*
  202. * Check to see if it is enabled by default in platform
  203. * config, or manual setting passed by mode parameter
  204. */
  205. if (mode & SPI_DBLRATE) {
  206. bus_setup |= DSPI_CTAR_DBR;
  207. dbr = 1;
  208. }
  209. bus_setup |= (mode & 0x0FC00000) >> 4; /* PSCSCK, PASC, PDT */
  210. bus_setup |= (mode & 0x000FFF00) >> 4; /* CSSCK, ASC, DT */
  211. } else
  212. bus_setup |= (dspi->ctar[cfslave->slave.bus] & 0x78FCFFF0);
  213. cfslave->charbit =
  214. ((dspi->ctar[cfslave->slave.bus] & 0x78000000) ==
  215. 0x78000000) ? 16 : 8;
  216. pbrcnt = sizeof(prescaler) / sizeof(int);
  217. brcnt = sizeof(scaler) / sizeof(int);
  218. /* baudrate calculation - to closer value, may not be exact match */
  219. for (best_i = 0, best_j = 0, i = 0; i < pbrcnt; i++) {
  220. baud_speed = gd->bus_clk / prescaler[i];
  221. for (j = 0; j < brcnt; j++) {
  222. tmp = (baud_speed / scaler[j]) * (1 + dbr);
  223. if (tmp > cfslave->baudrate)
  224. diff = tmp - cfslave->baudrate;
  225. else
  226. diff = cfslave->baudrate - tmp;
  227. if (diff < bestmatch) {
  228. bestmatch = diff;
  229. best_i = i;
  230. best_j = j;
  231. }
  232. }
  233. }
  234. bus_setup |= (DSPI_CTAR_PBR(best_i) | DSPI_CTAR_BR(best_j));
  235. dspi->ctar[cfslave->slave.bus] = bus_setup;
  236. return &cfslave->slave;
  237. }
  238. #endif /* CONFIG_CF_DSPI */
  239. #ifdef CONFIG_CMD_SPI
  240. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  241. {
  242. if (((cs >= 0) && (cs < 8)) && ((bus >= 0) && (bus < 8)))
  243. return 1;
  244. else
  245. return 0;
  246. }
  247. void spi_init(void)
  248. {
  249. cfspi_init();
  250. }
  251. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  252. unsigned int max_hz, unsigned int mode)
  253. {
  254. struct cf_spi_slave *cfslave;
  255. if (!spi_cs_is_valid(bus, cs))
  256. return NULL;
  257. cfslave = spi_alloc_slave(struct cf_spi_slave, bus, cs);
  258. if (!cfslave)
  259. return NULL;
  260. cfslave->baudrate = max_hz;
  261. /* specific setup */
  262. return cfspi_setup_slave(cfslave, mode);
  263. }
  264. void spi_free_slave(struct spi_slave *slave)
  265. {
  266. struct cf_spi_slave *cfslave = to_cf_spi_slave(slave);
  267. free(cfslave);
  268. }
  269. int spi_claim_bus(struct spi_slave *slave)
  270. {
  271. return cfspi_claim_bus(slave->bus, slave->cs);
  272. }
  273. void spi_release_bus(struct spi_slave *slave)
  274. {
  275. cfspi_release_bus(slave->bus, slave->cs);
  276. }
  277. int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  278. void *din, unsigned long flags)
  279. {
  280. return cfspi_xfer(slave, bitlen, dout, din, flags);
  281. }
  282. #endif /* CONFIG_CMD_SPI */