stm32f746-disco.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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/rcc.h>
  13. #include <asm/arch/fmc.h>
  14. #include <dm/platdata.h>
  15. #include <dm/platform_data/serial_stm32x7.h>
  16. #include <asm/arch/stm32_periph.h>
  17. #include <asm/arch/stm32_defs.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. /*
  102. * STM32 RCC FMC specific definitions
  103. */
  104. #define RCC_ENR_FMC (1 << 0) /* FMC module clock */
  105. static inline u32 _ns2clk(u32 ns, u32 freq)
  106. {
  107. u32 tmp = freq/1000000;
  108. return (tmp * ns) / 1000;
  109. }
  110. #define NS2CLK(ns) (_ns2clk(ns, freq))
  111. /*
  112. * Following are timings for IS42S16400J, from corresponding datasheet
  113. */
  114. #define SDRAM_CAS 3 /* 3 cycles */
  115. #define SDRAM_NB 1 /* Number of banks */
  116. #define SDRAM_MWID 1 /* 16 bit memory */
  117. #define SDRAM_NR 0x1 /* 12-bit row */
  118. #define SDRAM_NC 0x0 /* 8-bit col */
  119. #define SDRAM_RBURST 0x1 /* Single read requests always as bursts */
  120. #define SDRAM_RPIPE 0x0 /* No HCLK clock cycle delay */
  121. #define SDRAM_TRRD NS2CLK(12)
  122. #define SDRAM_TRCD NS2CLK(18)
  123. #define SDRAM_TRP NS2CLK(18)
  124. #define SDRAM_TRAS NS2CLK(42)
  125. #define SDRAM_TRC NS2CLK(60)
  126. #define SDRAM_TRFC NS2CLK(60)
  127. #define SDRAM_TCDL (1 - 1)
  128. #define SDRAM_TRDL NS2CLK(12)
  129. #define SDRAM_TBDL (1 - 1)
  130. #define SDRAM_TREF (NS2CLK(64000000 / 8192) - 20)
  131. #define SDRAM_TCCD (1 - 1)
  132. #define SDRAM_TXSR SDRAM_TRFC /* Row cycle time after precharge */
  133. #define SDRAM_TMRD 1 /* Page 10, Mode Register Set */
  134. /* Last data in to row precharge, need also comply ineq on page 1648 */
  135. #define SDRAM_TWR max(\
  136. (int)max((int)SDRAM_TRDL, (int)(SDRAM_TRAS - SDRAM_TRCD)), \
  137. (int)(SDRAM_TRC - SDRAM_TRCD - SDRAM_TRP)\
  138. )
  139. #define SDRAM_MODE_BL_SHIFT 0
  140. #define SDRAM_MODE_CAS_SHIFT 4
  141. #define SDRAM_MODE_BL 0
  142. #define SDRAM_MODE_CAS SDRAM_CAS
  143. int dram_init(void)
  144. {
  145. u32 freq;
  146. int rv;
  147. rv = fmc_setup_gpio();
  148. if (rv)
  149. return rv;
  150. setbits_le32(RCC_BASE + RCC_AHB3ENR, RCC_ENR_FMC);
  151. /*
  152. * Get frequency for NS2CLK calculation.
  153. */
  154. freq = clock_get(CLOCK_AHB) / CONFIG_SYS_RAM_FREQ_DIV;
  155. writel(
  156. CONFIG_SYS_RAM_FREQ_DIV << FMC_SDCR_SDCLK_SHIFT
  157. | SDRAM_CAS << FMC_SDCR_CAS_SHIFT
  158. | SDRAM_NB << FMC_SDCR_NB_SHIFT
  159. | SDRAM_MWID << FMC_SDCR_MWID_SHIFT
  160. | SDRAM_NR << FMC_SDCR_NR_SHIFT
  161. | SDRAM_NC << FMC_SDCR_NC_SHIFT
  162. | SDRAM_RPIPE << FMC_SDCR_RPIPE_SHIFT
  163. | SDRAM_RBURST << FMC_SDCR_RBURST_SHIFT,
  164. &STM32_SDRAM_FMC->sdcr1);
  165. writel(
  166. SDRAM_TRCD << FMC_SDTR_TRCD_SHIFT
  167. | SDRAM_TRP << FMC_SDTR_TRP_SHIFT
  168. | SDRAM_TWR << FMC_SDTR_TWR_SHIFT
  169. | SDRAM_TRC << FMC_SDTR_TRC_SHIFT
  170. | SDRAM_TRAS << FMC_SDTR_TRAS_SHIFT
  171. | SDRAM_TXSR << FMC_SDTR_TXSR_SHIFT
  172. | SDRAM_TMRD << FMC_SDTR_TMRD_SHIFT,
  173. &STM32_SDRAM_FMC->sdtr1);
  174. writel(FMC_SDCMR_BANK_1 | FMC_SDCMR_MODE_START_CLOCK,
  175. &STM32_SDRAM_FMC->sdcmr);
  176. udelay(200); /* 200 us delay, page 10, "Power-Up" */
  177. FMC_BUSY_WAIT();
  178. writel(FMC_SDCMR_BANK_1 | FMC_SDCMR_MODE_PRECHARGE,
  179. &STM32_SDRAM_FMC->sdcmr);
  180. udelay(100);
  181. FMC_BUSY_WAIT();
  182. writel((FMC_SDCMR_BANK_1 | FMC_SDCMR_MODE_AUTOREFRESH
  183. | 7 << FMC_SDCMR_NRFS_SHIFT), &STM32_SDRAM_FMC->sdcmr);
  184. udelay(100);
  185. FMC_BUSY_WAIT();
  186. writel(FMC_SDCMR_BANK_1 | (SDRAM_MODE_BL << SDRAM_MODE_BL_SHIFT
  187. | SDRAM_MODE_CAS << SDRAM_MODE_CAS_SHIFT)
  188. << FMC_SDCMR_MODE_REGISTER_SHIFT | FMC_SDCMR_MODE_WRITE_MODE,
  189. &STM32_SDRAM_FMC->sdcmr);
  190. udelay(100);
  191. FMC_BUSY_WAIT();
  192. writel(FMC_SDCMR_BANK_1 | FMC_SDCMR_MODE_NORMAL,
  193. &STM32_SDRAM_FMC->sdcmr);
  194. FMC_BUSY_WAIT();
  195. /* Refresh timer */
  196. writel(SDRAM_TREF, &STM32_SDRAM_FMC->sdrtr);
  197. /*
  198. * Fill in global info with description of SRAM configuration
  199. */
  200. gd->bd->bi_dram[0].start = CONFIG_SYS_RAM_BASE;
  201. gd->bd->bi_dram[0].size = CONFIG_SYS_RAM_SIZE;
  202. gd->ram_size = CONFIG_SYS_RAM_SIZE;
  203. return rv;
  204. }
  205. static const struct stm32_gpio_dsc usart_gpio[] = {
  206. {STM32_GPIO_PORT_A, STM32_GPIO_PIN_9}, /* TX */
  207. {STM32_GPIO_PORT_B, STM32_GPIO_PIN_7}, /* RX */
  208. };
  209. int uart_setup_gpio(void)
  210. {
  211. int i;
  212. int rv = 0;
  213. clock_setup(GPIO_A_CLOCK_CFG);
  214. clock_setup(GPIO_B_CLOCK_CFG);
  215. for (i = 0; i < ARRAY_SIZE(usart_gpio); i++) {
  216. rv = stm32_gpio_config(&usart_gpio[i], &gpio_ctl_usart);
  217. if (rv)
  218. goto out;
  219. }
  220. out:
  221. return rv;
  222. }
  223. static const struct stm32x7_serial_platdata serial_platdata = {
  224. .base = (struct stm32_usart *)USART1_BASE,
  225. .clock = CONFIG_SYS_CLK_FREQ,
  226. };
  227. U_BOOT_DEVICE(stm32x7_serials) = {
  228. .name = "serial_stm32x7",
  229. .platdata = &serial_platdata,
  230. };
  231. u32 get_board_rev(void)
  232. {
  233. return 0;
  234. }
  235. int board_early_init_f(void)
  236. {
  237. int res;
  238. res = uart_setup_gpio();
  239. clock_setup(USART1_CLOCK_CFG);
  240. if (res)
  241. return res;
  242. return 0;
  243. }
  244. int board_init(void)
  245. {
  246. gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100;
  247. return 0;
  248. }