stm32f746-disco.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * (C) Copyright 2016
  3. * Vikas Manocha, <vikas.manocha@st.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <asm/io.h>
  9. #include <asm/armv7m.h>
  10. #include <asm/arch/stm32.h>
  11. #include <asm/arch/gpio.h>
  12. #include <asm/arch/fmc.h>
  13. #include <dm/platdata.h>
  14. #include <dm/platform_data/serial_stm32x7.h>
  15. #include <asm/arch/stm32_periph.h>
  16. #include <asm/arch/stm32_defs.h>
  17. #include <asm/arch/syscfg.h>
  18. DECLARE_GLOBAL_DATA_PTR;
  19. const struct stm32_gpio_ctl gpio_ctl_gpout = {
  20. .mode = STM32_GPIO_MODE_OUT,
  21. .otype = STM32_GPIO_OTYPE_PP,
  22. .speed = STM32_GPIO_SPEED_50M,
  23. .pupd = STM32_GPIO_PUPD_NO,
  24. .af = STM32_GPIO_AF0
  25. };
  26. const struct stm32_gpio_ctl gpio_ctl_usart = {
  27. .mode = STM32_GPIO_MODE_AF,
  28. .otype = STM32_GPIO_OTYPE_PP,
  29. .speed = STM32_GPIO_SPEED_50M,
  30. .pupd = STM32_GPIO_PUPD_UP,
  31. .af = STM32_GPIO_AF7
  32. };
  33. const struct stm32_gpio_ctl gpio_ctl_fmc = {
  34. .mode = STM32_GPIO_MODE_AF,
  35. .otype = STM32_GPIO_OTYPE_PP,
  36. .speed = STM32_GPIO_SPEED_100M,
  37. .pupd = STM32_GPIO_PUPD_NO,
  38. .af = STM32_GPIO_AF12
  39. };
  40. static const struct stm32_gpio_dsc ext_ram_fmc_gpio[] = {
  41. /* Chip is LQFP144, see DM00077036.pdf for details */
  42. {STM32_GPIO_PORT_D, STM32_GPIO_PIN_10}, /* 79, FMC_D15 */
  43. {STM32_GPIO_PORT_D, STM32_GPIO_PIN_9}, /* 78, FMC_D14 */
  44. {STM32_GPIO_PORT_D, STM32_GPIO_PIN_8}, /* 77, FMC_D13 */
  45. {STM32_GPIO_PORT_E, STM32_GPIO_PIN_15}, /* 68, FMC_D12 */
  46. {STM32_GPIO_PORT_E, STM32_GPIO_PIN_14}, /* 67, FMC_D11 */
  47. {STM32_GPIO_PORT_E, STM32_GPIO_PIN_13}, /* 66, FMC_D10 */
  48. {STM32_GPIO_PORT_E, STM32_GPIO_PIN_12}, /* 65, FMC_D9 */
  49. {STM32_GPIO_PORT_E, STM32_GPIO_PIN_11}, /* 64, FMC_D8 */
  50. {STM32_GPIO_PORT_E, STM32_GPIO_PIN_10}, /* 63, FMC_D7 */
  51. {STM32_GPIO_PORT_E, STM32_GPIO_PIN_9}, /* 60, FMC_D6 */
  52. {STM32_GPIO_PORT_E, STM32_GPIO_PIN_8}, /* 59, FMC_D5 */
  53. {STM32_GPIO_PORT_E, STM32_GPIO_PIN_7}, /* 58, FMC_D4 */
  54. {STM32_GPIO_PORT_D, STM32_GPIO_PIN_1}, /* 115, FMC_D3 */
  55. {STM32_GPIO_PORT_D, STM32_GPIO_PIN_0}, /* 114, FMC_D2 */
  56. {STM32_GPIO_PORT_D, STM32_GPIO_PIN_15}, /* 86, FMC_D1 */
  57. {STM32_GPIO_PORT_D, STM32_GPIO_PIN_14}, /* 85, FMC_D0 */
  58. {STM32_GPIO_PORT_E, STM32_GPIO_PIN_1}, /* 142, FMC_NBL1 */
  59. {STM32_GPIO_PORT_E, STM32_GPIO_PIN_0}, /* 141, FMC_NBL0 */
  60. {STM32_GPIO_PORT_G, STM32_GPIO_PIN_5}, /* 90, FMC_A15, BA1 */
  61. {STM32_GPIO_PORT_G, STM32_GPIO_PIN_4}, /* 89, FMC_A14, BA0 */
  62. {STM32_GPIO_PORT_G, STM32_GPIO_PIN_1}, /* 57, FMC_A11 */
  63. {STM32_GPIO_PORT_G, STM32_GPIO_PIN_0}, /* 56, FMC_A10 */
  64. {STM32_GPIO_PORT_F, STM32_GPIO_PIN_15}, /* 55, FMC_A9 */
  65. {STM32_GPIO_PORT_F, STM32_GPIO_PIN_14}, /* 54, FMC_A8 */
  66. {STM32_GPIO_PORT_F, STM32_GPIO_PIN_13}, /* 53, FMC_A7 */
  67. {STM32_GPIO_PORT_F, STM32_GPIO_PIN_12}, /* 50, FMC_A6 */
  68. {STM32_GPIO_PORT_F, STM32_GPIO_PIN_5}, /* 15, FMC_A5 */
  69. {STM32_GPIO_PORT_F, STM32_GPIO_PIN_4}, /* 14, FMC_A4 */
  70. {STM32_GPIO_PORT_F, STM32_GPIO_PIN_3}, /* 13, FMC_A3 */
  71. {STM32_GPIO_PORT_F, STM32_GPIO_PIN_2}, /* 12, FMC_A2 */
  72. {STM32_GPIO_PORT_F, STM32_GPIO_PIN_1}, /* 11, FMC_A1 */
  73. {STM32_GPIO_PORT_F, STM32_GPIO_PIN_0}, /* 10, FMC_A0 */
  74. {STM32_GPIO_PORT_H, STM32_GPIO_PIN_3}, /* 136, SDRAM_NE */
  75. {STM32_GPIO_PORT_F, STM32_GPIO_PIN_11}, /* 49, SDRAM_NRAS */
  76. {STM32_GPIO_PORT_G, STM32_GPIO_PIN_15}, /* 132, SDRAM_NCAS */
  77. {STM32_GPIO_PORT_H, STM32_GPIO_PIN_5}, /* 26, SDRAM_NWE */
  78. {STM32_GPIO_PORT_C, STM32_GPIO_PIN_3}, /* 135, SDRAM_CKE */
  79. {STM32_GPIO_PORT_G, STM32_GPIO_PIN_8}, /* 93, SDRAM_CLK */
  80. };
  81. static int fmc_setup_gpio(void)
  82. {
  83. int rv = 0;
  84. int i;
  85. clock_setup(GPIO_B_CLOCK_CFG);
  86. clock_setup(GPIO_C_CLOCK_CFG);
  87. clock_setup(GPIO_D_CLOCK_CFG);
  88. clock_setup(GPIO_E_CLOCK_CFG);
  89. clock_setup(GPIO_F_CLOCK_CFG);
  90. clock_setup(GPIO_G_CLOCK_CFG);
  91. clock_setup(GPIO_H_CLOCK_CFG);
  92. for (i = 0; i < ARRAY_SIZE(ext_ram_fmc_gpio); i++) {
  93. rv = stm32_gpio_config(&ext_ram_fmc_gpio[i],
  94. &gpio_ctl_fmc);
  95. if (rv)
  96. goto out;
  97. }
  98. out:
  99. return rv;
  100. }
  101. static inline u32 _ns2clk(u32 ns, u32 freq)
  102. {
  103. u32 tmp = freq/1000000;
  104. return (tmp * ns) / 1000;
  105. }
  106. #define NS2CLK(ns) (_ns2clk(ns, freq))
  107. /*
  108. * Following are timings for IS42S16400J, from corresponding datasheet
  109. */
  110. #define SDRAM_CAS 3 /* 3 cycles */
  111. #define SDRAM_NB 1 /* Number of banks */
  112. #define SDRAM_MWID 1 /* 16 bit memory */
  113. #define SDRAM_NR 0x1 /* 12-bit row */
  114. #define SDRAM_NC 0x0 /* 8-bit col */
  115. #define SDRAM_RBURST 0x1 /* Single read requests always as bursts */
  116. #define SDRAM_RPIPE 0x0 /* No HCLK clock cycle delay */
  117. #define SDRAM_TRRD NS2CLK(12)
  118. #define SDRAM_TRCD NS2CLK(18)
  119. #define SDRAM_TRP NS2CLK(18)
  120. #define SDRAM_TRAS NS2CLK(42)
  121. #define SDRAM_TRC NS2CLK(60)
  122. #define SDRAM_TRFC NS2CLK(60)
  123. #define SDRAM_TCDL (1 - 1)
  124. #define SDRAM_TRDL NS2CLK(12)
  125. #define SDRAM_TBDL (1 - 1)
  126. #define SDRAM_TREF (NS2CLK(64000000 / 8192) - 20)
  127. #define SDRAM_TCCD (1 - 1)
  128. #define SDRAM_TXSR SDRAM_TRFC /* Row cycle time after precharge */
  129. #define SDRAM_TMRD 1 /* Page 10, Mode Register Set */
  130. /* Last data in to row precharge, need also comply ineq on page 1648 */
  131. #define SDRAM_TWR max(\
  132. (int)max((int)SDRAM_TRDL, (int)(SDRAM_TRAS - SDRAM_TRCD)), \
  133. (int)(SDRAM_TRC - SDRAM_TRCD - SDRAM_TRP)\
  134. )
  135. #define SDRAM_MODE_BL_SHIFT 0
  136. #define SDRAM_MODE_CAS_SHIFT 4
  137. #define SDRAM_MODE_BL 0
  138. #define SDRAM_MODE_CAS SDRAM_CAS
  139. int dram_init(void)
  140. {
  141. u32 freq;
  142. int rv;
  143. rv = fmc_setup_gpio();
  144. if (rv)
  145. return rv;
  146. clock_setup(FMC_CLOCK_CFG);
  147. /*
  148. * Get frequency for NS2CLK calculation.
  149. */
  150. freq = clock_get(CLOCK_AHB) / CONFIG_SYS_RAM_FREQ_DIV;
  151. writel(
  152. CONFIG_SYS_RAM_FREQ_DIV << FMC_SDCR_SDCLK_SHIFT
  153. | SDRAM_CAS << FMC_SDCR_CAS_SHIFT
  154. | SDRAM_NB << FMC_SDCR_NB_SHIFT
  155. | SDRAM_MWID << FMC_SDCR_MWID_SHIFT
  156. | SDRAM_NR << FMC_SDCR_NR_SHIFT
  157. | SDRAM_NC << FMC_SDCR_NC_SHIFT
  158. | SDRAM_RPIPE << FMC_SDCR_RPIPE_SHIFT
  159. | SDRAM_RBURST << FMC_SDCR_RBURST_SHIFT,
  160. &STM32_SDRAM_FMC->sdcr1);
  161. writel(
  162. SDRAM_TRCD << FMC_SDTR_TRCD_SHIFT
  163. | SDRAM_TRP << FMC_SDTR_TRP_SHIFT
  164. | SDRAM_TWR << FMC_SDTR_TWR_SHIFT
  165. | SDRAM_TRC << FMC_SDTR_TRC_SHIFT
  166. | SDRAM_TRAS << FMC_SDTR_TRAS_SHIFT
  167. | SDRAM_TXSR << FMC_SDTR_TXSR_SHIFT
  168. | SDRAM_TMRD << FMC_SDTR_TMRD_SHIFT,
  169. &STM32_SDRAM_FMC->sdtr1);
  170. writel(FMC_SDCMR_BANK_1 | FMC_SDCMR_MODE_START_CLOCK,
  171. &STM32_SDRAM_FMC->sdcmr);
  172. udelay(200); /* 200 us delay, page 10, "Power-Up" */
  173. FMC_BUSY_WAIT();
  174. writel(FMC_SDCMR_BANK_1 | FMC_SDCMR_MODE_PRECHARGE,
  175. &STM32_SDRAM_FMC->sdcmr);
  176. udelay(100);
  177. FMC_BUSY_WAIT();
  178. writel((FMC_SDCMR_BANK_1 | FMC_SDCMR_MODE_AUTOREFRESH
  179. | 7 << FMC_SDCMR_NRFS_SHIFT), &STM32_SDRAM_FMC->sdcmr);
  180. udelay(100);
  181. FMC_BUSY_WAIT();
  182. writel(FMC_SDCMR_BANK_1 | (SDRAM_MODE_BL << SDRAM_MODE_BL_SHIFT
  183. | SDRAM_MODE_CAS << SDRAM_MODE_CAS_SHIFT)
  184. << FMC_SDCMR_MODE_REGISTER_SHIFT | FMC_SDCMR_MODE_WRITE_MODE,
  185. &STM32_SDRAM_FMC->sdcmr);
  186. udelay(100);
  187. FMC_BUSY_WAIT();
  188. writel(FMC_SDCMR_BANK_1 | FMC_SDCMR_MODE_NORMAL,
  189. &STM32_SDRAM_FMC->sdcmr);
  190. FMC_BUSY_WAIT();
  191. /* Refresh timer */
  192. writel(SDRAM_TREF, &STM32_SDRAM_FMC->sdrtr);
  193. /*
  194. * Fill in global info with description of SRAM configuration
  195. */
  196. gd->bd->bi_dram[0].start = CONFIG_SYS_RAM_BASE;
  197. gd->bd->bi_dram[0].size = CONFIG_SYS_RAM_SIZE;
  198. gd->ram_size = CONFIG_SYS_RAM_SIZE;
  199. return rv;
  200. }
  201. static const struct stm32_gpio_dsc usart_gpio[] = {
  202. {STM32_GPIO_PORT_A, STM32_GPIO_PIN_9}, /* TX */
  203. {STM32_GPIO_PORT_B, STM32_GPIO_PIN_7}, /* RX */
  204. };
  205. int uart_setup_gpio(void)
  206. {
  207. int i;
  208. int rv = 0;
  209. clock_setup(GPIO_A_CLOCK_CFG);
  210. clock_setup(GPIO_B_CLOCK_CFG);
  211. for (i = 0; i < ARRAY_SIZE(usart_gpio); i++) {
  212. rv = stm32_gpio_config(&usart_gpio[i], &gpio_ctl_usart);
  213. if (rv)
  214. goto out;
  215. }
  216. out:
  217. return rv;
  218. }
  219. #ifdef CONFIG_ETH_DESIGNWARE
  220. const struct stm32_gpio_ctl gpio_ctl_eth = {
  221. .mode = STM32_GPIO_MODE_AF,
  222. .otype = STM32_GPIO_OTYPE_PP,
  223. .speed = STM32_GPIO_SPEED_100M,
  224. .pupd = STM32_GPIO_PUPD_NO,
  225. .af = STM32_GPIO_AF11
  226. };
  227. static const struct stm32_gpio_dsc eth_gpio[] = {
  228. {STM32_GPIO_PORT_A, STM32_GPIO_PIN_1}, /* ETH_RMII_REF_CLK */
  229. {STM32_GPIO_PORT_A, STM32_GPIO_PIN_2}, /* ETH_MDIO */
  230. {STM32_GPIO_PORT_A, STM32_GPIO_PIN_7}, /* ETH_RMII_CRS_DV */
  231. {STM32_GPIO_PORT_C, STM32_GPIO_PIN_1}, /* ETH_MDC */
  232. {STM32_GPIO_PORT_C, STM32_GPIO_PIN_4}, /* ETH_RMII_RXD0 */
  233. {STM32_GPIO_PORT_C, STM32_GPIO_PIN_5}, /* ETH_RMII_RXD1 */
  234. {STM32_GPIO_PORT_G, STM32_GPIO_PIN_11}, /* ETH_RMII_TX_EN */
  235. {STM32_GPIO_PORT_G, STM32_GPIO_PIN_13}, /* ETH_RMII_TXD0 */
  236. {STM32_GPIO_PORT_G, STM32_GPIO_PIN_14}, /* ETH_RMII_TXD1 */
  237. };
  238. static int stmmac_setup(void)
  239. {
  240. int res = 0;
  241. int i;
  242. clock_setup(SYSCFG_CLOCK_CFG);
  243. /* Set >RMII mode */
  244. STM32_SYSCFG->pmc |= SYSCFG_PMC_MII_RMII_SEL;
  245. clock_setup(GPIO_A_CLOCK_CFG);
  246. clock_setup(GPIO_C_CLOCK_CFG);
  247. clock_setup(GPIO_G_CLOCK_CFG);
  248. for (i = 0; i < ARRAY_SIZE(eth_gpio); i++) {
  249. res = stm32_gpio_config(&eth_gpio[i], &gpio_ctl_eth);
  250. if (res)
  251. return res;
  252. }
  253. clock_setup(STMMAC_CLOCK_CFG);
  254. return 0;
  255. }
  256. #endif
  257. #ifdef CONFIG_STM32_QSPI
  258. const struct stm32_gpio_ctl gpio_ctl_qspi_9 = {
  259. .mode = STM32_GPIO_MODE_AF,
  260. .otype = STM32_GPIO_OTYPE_PP,
  261. .speed = STM32_GPIO_SPEED_100M,
  262. .pupd = STM32_GPIO_PUPD_NO,
  263. .af = STM32_GPIO_AF9
  264. };
  265. const struct stm32_gpio_ctl gpio_ctl_qspi_10 = {
  266. .mode = STM32_GPIO_MODE_AF,
  267. .otype = STM32_GPIO_OTYPE_PP,
  268. .speed = STM32_GPIO_SPEED_100M,
  269. .pupd = STM32_GPIO_PUPD_NO,
  270. .af = STM32_GPIO_AF10
  271. };
  272. static const struct stm32_gpio_dsc qspi_af9_gpio[] = {
  273. {STM32_GPIO_PORT_B, STM32_GPIO_PIN_2}, /* QUADSPI_CLK */
  274. {STM32_GPIO_PORT_D, STM32_GPIO_PIN_11}, /* QUADSPI_BK1_IO0 */
  275. {STM32_GPIO_PORT_D, STM32_GPIO_PIN_12}, /* QUADSPI_BK1_IO1 */
  276. {STM32_GPIO_PORT_D, STM32_GPIO_PIN_13}, /* QUADSPI_BK1_IO3 */
  277. {STM32_GPIO_PORT_E, STM32_GPIO_PIN_2}, /* QUADSPI_BK1_IO2 */
  278. };
  279. static const struct stm32_gpio_dsc qspi_af10_gpio[] = {
  280. {STM32_GPIO_PORT_B, STM32_GPIO_PIN_6}, /* QUADSPI_BK1_NCS */
  281. };
  282. static int qspi_setup(void)
  283. {
  284. int res = 0;
  285. int i;
  286. clock_setup(GPIO_B_CLOCK_CFG);
  287. clock_setup(GPIO_D_CLOCK_CFG);
  288. clock_setup(GPIO_E_CLOCK_CFG);
  289. for (i = 0; i < ARRAY_SIZE(qspi_af9_gpio); i++) {
  290. res = stm32_gpio_config(&qspi_af9_gpio[i], &gpio_ctl_qspi_9);
  291. if (res)
  292. return res;
  293. }
  294. for (i = 0; i < ARRAY_SIZE(qspi_af10_gpio); i++) {
  295. res = stm32_gpio_config(&qspi_af10_gpio[i], &gpio_ctl_qspi_10);
  296. if (res)
  297. return res;
  298. }
  299. return 0;
  300. }
  301. #endif
  302. u32 get_board_rev(void)
  303. {
  304. return 0;
  305. }
  306. int board_early_init_f(void)
  307. {
  308. int res;
  309. res = uart_setup_gpio();
  310. clock_setup(USART1_CLOCK_CFG);
  311. if (res)
  312. return res;
  313. #ifdef CONFIG_ETH_DESIGNWARE
  314. res = stmmac_setup();
  315. if (res)
  316. return res;
  317. #endif
  318. #ifdef CONFIG_STM32_QSPI
  319. res = qspi_setup();
  320. if (res)
  321. return res;
  322. #endif
  323. return 0;
  324. }
  325. int board_init(void)
  326. {
  327. gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100;
  328. return 0;
  329. }