cf_spi.c 7.9 KB

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