cf_spi.c 8.6 KB

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