Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 2444dae | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2015 Google, Inc |
Simon Glass | 2444dae | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | 74e53e0 | 2016-01-21 19:45:07 -0700 | [diff] [blame] | 7 | #include <clk.h> |
Simon Glass | 2444dae | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 8 | #include <dm.h> |
| 9 | #include <ram.h> |
Jacob Chen | 67171e1 | 2016-09-19 18:46:28 +0800 | [diff] [blame] | 10 | #include <syscon.h> |
huang lin | be1d5e0 | 2015-11-17 14:20:27 +0800 | [diff] [blame] | 11 | #include <asm/io.h> |
Kever Yang | 15f09a1 | 2019-03-28 11:01:23 +0800 | [diff] [blame^] | 12 | #include <asm/arch-rockchip/clock.h> |
| 13 | #include <asm/arch-rockchip/cru_rk3288.h> |
| 14 | #include <asm/arch-rockchip/periph.h> |
| 15 | #include <asm/arch-rockchip/pmu_rk3288.h> |
| 16 | #include <asm/arch-rockchip/qos_rk3288.h> |
| 17 | #include <asm/arch-rockchip/boot_mode.h> |
Xu Ziyuan | b47ea79 | 2016-07-12 19:09:49 +0800 | [diff] [blame] | 18 | #include <asm/gpio.h> |
| 19 | #include <dm/pinctrl.h> |
Simon Glass | 20b13e8 | 2016-11-13 14:22:14 -0700 | [diff] [blame] | 20 | #include <dt-bindings/clock/rk3288-cru.h> |
| 21 | #include <power/regulator.h> |
Simon Glass | 2444dae | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 22 | |
| 23 | DECLARE_GLOBAL_DATA_PTR; |
| 24 | |
Jacob Chen | 67171e1 | 2016-09-19 18:46:28 +0800 | [diff] [blame] | 25 | __weak int rk_board_late_init(void) |
| 26 | { |
| 27 | return 0; |
| 28 | } |
| 29 | |
Nickey Yang Nickey Yang | 9b83201 | 2016-12-29 10:47:30 +0800 | [diff] [blame] | 30 | int rk3288_qos_init(void) |
| 31 | { |
| 32 | int val = 2 << PRIORITY_HIGH_SHIFT | 2 << PRIORITY_LOW_SHIFT; |
| 33 | /* set vop qos to higher priority */ |
| 34 | writel(val, CPU_AXI_QOS_PRIORITY + VIO0_VOP_QOS); |
| 35 | writel(val, CPU_AXI_QOS_PRIORITY + VIO1_VOP_QOS); |
| 36 | |
| 37 | if (!fdt_node_check_compatible(gd->fdt_blob, 0, |
Eddie Cai | 6f27976 | 2017-01-18 11:03:54 +0800 | [diff] [blame] | 38 | "rockchip,rk3288-tinker")) |
Nickey Yang Nickey Yang | 9b83201 | 2016-12-29 10:47:30 +0800 | [diff] [blame] | 39 | { |
| 40 | /* set isp qos to higher priority */ |
| 41 | writel(val, CPU_AXI_QOS_PRIORITY + VIO1_ISP_R_QOS); |
| 42 | writel(val, CPU_AXI_QOS_PRIORITY + VIO1_ISP_W0_QOS); |
| 43 | writel(val, CPU_AXI_QOS_PRIORITY + VIO1_ISP_W1_QOS); |
| 44 | } |
| 45 | return 0; |
| 46 | } |
| 47 | |
Wadim Egorov | 40d4f79 | 2017-08-21 13:36:57 +0200 | [diff] [blame] | 48 | static void rk3288_detect_reset_reason(void) |
| 49 | { |
| 50 | struct rk3288_cru *cru = rockchip_get_cru(); |
| 51 | const char *reason; |
| 52 | |
| 53 | if (IS_ERR(cru)) |
| 54 | return; |
| 55 | |
| 56 | switch (cru->cru_glb_rst_st) { |
| 57 | case GLB_POR_RST: |
| 58 | reason = "POR"; |
| 59 | break; |
| 60 | case FST_GLB_RST_ST: |
| 61 | case SND_GLB_RST_ST: |
| 62 | reason = "RST"; |
| 63 | break; |
| 64 | case FST_GLB_TSADC_RST_ST: |
| 65 | case SND_GLB_TSADC_RST_ST: |
| 66 | reason = "THERMAL"; |
| 67 | break; |
| 68 | case FST_GLB_WDT_RST_ST: |
| 69 | case SND_GLB_WDT_RST_ST: |
| 70 | reason = "WDOG"; |
| 71 | break; |
| 72 | default: |
| 73 | reason = "unknown reset"; |
| 74 | } |
| 75 | |
| 76 | env_set("reset_reason", reason); |
| 77 | |
| 78 | /* |
| 79 | * Clear cru_glb_rst_st, so we can determine the last reset cause |
| 80 | * for following resets. |
| 81 | */ |
| 82 | rk_clrreg(&cru->cru_glb_rst_st, GLB_RST_ST_MASK); |
| 83 | } |
| 84 | |
Jacob Chen | 67171e1 | 2016-09-19 18:46:28 +0800 | [diff] [blame] | 85 | int board_late_init(void) |
| 86 | { |
| 87 | setup_boot_mode(); |
Nickey Yang Nickey Yang | 9b83201 | 2016-12-29 10:47:30 +0800 | [diff] [blame] | 88 | rk3288_qos_init(); |
Wadim Egorov | 40d4f79 | 2017-08-21 13:36:57 +0200 | [diff] [blame] | 89 | rk3288_detect_reset_reason(); |
Jacob Chen | 67171e1 | 2016-09-19 18:46:28 +0800 | [diff] [blame] | 90 | |
| 91 | return rk_board_late_init(); |
| 92 | } |
| 93 | |
Philipp Tomsich | ee14d29 | 2017-06-29 11:21:15 +0200 | [diff] [blame] | 94 | #if !CONFIG_IS_ENABLED(ROCKCHIP_BACK_TO_BROM) |
Simon Glass | 20b13e8 | 2016-11-13 14:22:14 -0700 | [diff] [blame] | 95 | static int veyron_init(void) |
| 96 | { |
| 97 | struct udevice *dev; |
| 98 | struct clk clk; |
| 99 | int ret; |
| 100 | |
| 101 | ret = regulator_get_by_platname("vdd_arm", &dev); |
Simon Glass | 6f06ef5 | 2017-05-31 17:57:27 -0600 | [diff] [blame] | 102 | if (ret) { |
| 103 | debug("Cannot set regulator name\n"); |
Simon Glass | 20b13e8 | 2016-11-13 14:22:14 -0700 | [diff] [blame] | 104 | return ret; |
Simon Glass | 6f06ef5 | 2017-05-31 17:57:27 -0600 | [diff] [blame] | 105 | } |
Simon Glass | 20b13e8 | 2016-11-13 14:22:14 -0700 | [diff] [blame] | 106 | |
| 107 | /* Slowly raise to max CPU voltage to prevent overshoot */ |
| 108 | ret = regulator_set_value(dev, 1200000); |
| 109 | if (ret) |
| 110 | return ret; |
| 111 | udelay(175); /* Must wait for voltage to stabilize, 2mV/us */ |
| 112 | ret = regulator_set_value(dev, 1400000); |
| 113 | if (ret) |
| 114 | return ret; |
| 115 | udelay(100); /* Must wait for voltage to stabilize, 2mV/us */ |
| 116 | |
| 117 | ret = rockchip_get_clk(&clk.dev); |
| 118 | if (ret) |
| 119 | return ret; |
| 120 | clk.id = PLL_APLL; |
| 121 | ret = clk_set_rate(&clk, 1800000000); |
| 122 | if (IS_ERR_VALUE(ret)) |
| 123 | return ret; |
| 124 | |
Carlo Caione | 315ded9 | 2018-06-11 20:00:50 +0100 | [diff] [blame] | 125 | ret = regulator_get_by_platname("vcc33_sd", &dev); |
| 126 | if (ret) { |
| 127 | debug("Cannot get regulator name\n"); |
| 128 | return ret; |
| 129 | } |
| 130 | |
| 131 | ret = regulator_set_value(dev, 3300000); |
| 132 | if (ret) |
| 133 | return ret; |
| 134 | |
Carlo Caione | 0d4d5fd | 2018-06-11 20:00:48 +0100 | [diff] [blame] | 135 | ret = regulators_enable_boot_on(false); |
| 136 | if (ret) { |
| 137 | debug("%s: Cannot enable boot on regulators\n", __func__); |
| 138 | return ret; |
| 139 | } |
| 140 | |
Simon Glass | 20b13e8 | 2016-11-13 14:22:14 -0700 | [diff] [blame] | 141 | return 0; |
| 142 | } |
| 143 | #endif |
| 144 | |
Simon Glass | 2444dae | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 145 | int board_init(void) |
| 146 | { |
Philipp Tomsich | ee14d29 | 2017-06-29 11:21:15 +0200 | [diff] [blame] | 147 | #if CONFIG_IS_ENABLED(ROCKCHIP_BACK_TO_BROM) |
Xu Ziyuan | b47ea79 | 2016-07-12 19:09:49 +0800 | [diff] [blame] | 148 | struct udevice *pinctrl; |
| 149 | int ret; |
| 150 | |
Jacob Chen | 67171e1 | 2016-09-19 18:46:28 +0800 | [diff] [blame] | 151 | /* |
| 152 | * We need to implement sdcard iomux here for the further |
| 153 | * initlization, otherwise, it'll hit sdcard command sending |
| 154 | * timeout exception. |
| 155 | */ |
Xu Ziyuan | b47ea79 | 2016-07-12 19:09:49 +0800 | [diff] [blame] | 156 | ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl); |
| 157 | if (ret) { |
| 158 | debug("%s: Cannot find pinctrl device\n", __func__); |
| 159 | goto err; |
| 160 | } |
| 161 | ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_SDCARD); |
| 162 | if (ret) { |
| 163 | debug("%s: Failed to set up SD card\n", __func__); |
| 164 | goto err; |
| 165 | } |
| 166 | |
Simon Glass | 2444dae | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 167 | return 0; |
Xu Ziyuan | b47ea79 | 2016-07-12 19:09:49 +0800 | [diff] [blame] | 168 | err: |
| 169 | printf("board_init: Error %d\n", ret); |
| 170 | |
| 171 | /* No way to report error here */ |
| 172 | hang(); |
| 173 | |
| 174 | return -1; |
| 175 | #else |
Simon Glass | 20b13e8 | 2016-11-13 14:22:14 -0700 | [diff] [blame] | 176 | int ret; |
| 177 | |
| 178 | /* We do some SoC one time setting here */ |
| 179 | if (!fdt_node_check_compatible(gd->fdt_blob, 0, "google,veyron")) { |
| 180 | ret = veyron_init(); |
| 181 | if (ret) |
| 182 | return ret; |
| 183 | } |
| 184 | |
Xu Ziyuan | b47ea79 | 2016-07-12 19:09:49 +0800 | [diff] [blame] | 185 | return 0; |
| 186 | #endif |
Simon Glass | 2444dae | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 187 | } |
| 188 | |
Simon Glass | 2444dae | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 189 | #ifndef CONFIG_SYS_DCACHE_OFF |
| 190 | void enable_caches(void) |
| 191 | { |
| 192 | /* Enable D-cache. I-cache is already enabled in start.S */ |
| 193 | dcache_enable(); |
| 194 | } |
| 195 | #endif |
Simon Glass | ad443b7 | 2016-01-21 19:45:06 -0700 | [diff] [blame] | 196 | |
Xu Ziyuan | 266c8fa | 2016-07-15 00:26:59 +0800 | [diff] [blame] | 197 | #if defined(CONFIG_USB_GADGET) && defined(CONFIG_USB_GADGET_DWC2_OTG) |
| 198 | #include <usb.h> |
| 199 | #include <usb/dwc2_udc.h> |
| 200 | |
| 201 | static struct dwc2_plat_otg_data rk3288_otg_data = { |
| 202 | .rx_fifo_sz = 512, |
| 203 | .np_tx_fifo_sz = 16, |
| 204 | .tx_fifo_sz = 128, |
| 205 | }; |
| 206 | |
| 207 | int board_usb_init(int index, enum usb_init_type init) |
| 208 | { |
| 209 | int node, phy_node; |
| 210 | const char *mode; |
| 211 | bool matched = false; |
| 212 | const void *blob = gd->fdt_blob; |
| 213 | u32 grf_phy_offset; |
| 214 | |
| 215 | /* find the usb_otg node */ |
| 216 | node = fdt_node_offset_by_compatible(blob, -1, |
| 217 | "rockchip,rk3288-usb"); |
| 218 | |
| 219 | while (node > 0) { |
| 220 | mode = fdt_getprop(blob, node, "dr_mode", NULL); |
| 221 | if (mode && strcmp(mode, "otg") == 0) { |
| 222 | matched = true; |
| 223 | break; |
| 224 | } |
| 225 | |
| 226 | node = fdt_node_offset_by_compatible(blob, node, |
| 227 | "rockchip,rk3288-usb"); |
| 228 | } |
| 229 | if (!matched) { |
| 230 | debug("Not found usb_otg device\n"); |
| 231 | return -ENODEV; |
| 232 | } |
| 233 | rk3288_otg_data.regs_otg = fdtdec_get_addr(blob, node, "reg"); |
| 234 | |
| 235 | node = fdtdec_lookup_phandle(blob, node, "phys"); |
| 236 | if (node <= 0) { |
| 237 | debug("Not found usb phy device\n"); |
| 238 | return -ENODEV; |
| 239 | } |
| 240 | |
| 241 | phy_node = fdt_parent_offset(blob, node); |
| 242 | if (phy_node <= 0) { |
| 243 | debug("Not found usb phy device\n"); |
| 244 | return -ENODEV; |
| 245 | } |
| 246 | |
| 247 | rk3288_otg_data.phy_of_node = phy_node; |
| 248 | grf_phy_offset = fdtdec_get_addr(blob, node, "reg"); |
| 249 | |
| 250 | /* find the grf node */ |
| 251 | node = fdt_node_offset_by_compatible(blob, -1, |
| 252 | "rockchip,rk3288-grf"); |
| 253 | if (node <= 0) { |
| 254 | debug("Not found grf device\n"); |
| 255 | return -ENODEV; |
| 256 | } |
| 257 | rk3288_otg_data.regs_phy = grf_phy_offset + |
| 258 | fdtdec_get_addr(blob, node, "reg"); |
| 259 | |
| 260 | return dwc2_udc_probe(&rk3288_otg_data); |
| 261 | } |
| 262 | |
| 263 | int board_usb_cleanup(int index, enum usb_init_type init) |
| 264 | { |
| 265 | return 0; |
| 266 | } |
| 267 | #endif |
| 268 | |
Simon Glass | 74e53e0 | 2016-01-21 19:45:07 -0700 | [diff] [blame] | 269 | static int do_clock(cmd_tbl_t *cmdtp, int flag, int argc, |
| 270 | char * const argv[]) |
| 271 | { |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 272 | static const struct { |
| 273 | char *name; |
| 274 | int id; |
| 275 | } clks[] = { |
| 276 | { "osc", CLK_OSC }, |
| 277 | { "apll", CLK_ARM }, |
| 278 | { "dpll", CLK_DDR }, |
| 279 | { "cpll", CLK_CODEC }, |
| 280 | { "gpll", CLK_GENERAL }, |
| 281 | #ifdef CONFIG_ROCKCHIP_RK3036 |
| 282 | { "mpll", CLK_NEW }, |
| 283 | #else |
| 284 | { "npll", CLK_NEW }, |
| 285 | #endif |
| 286 | }; |
| 287 | int ret, i; |
Simon Glass | 74e53e0 | 2016-01-21 19:45:07 -0700 | [diff] [blame] | 288 | struct udevice *dev; |
| 289 | |
Simon Glass | c3aad6f | 2016-07-17 15:23:17 -0600 | [diff] [blame] | 290 | ret = rockchip_get_clk(&dev); |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 291 | if (ret) { |
| 292 | printf("clk-uclass not found\n"); |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | for (i = 0; i < ARRAY_SIZE(clks); i++) { |
| 297 | struct clk clk; |
Simon Glass | 74e53e0 | 2016-01-21 19:45:07 -0700 | [diff] [blame] | 298 | ulong rate; |
| 299 | |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 300 | clk.id = clks[i].id; |
| 301 | ret = clk_request(dev, &clk); |
| 302 | if (ret < 0) |
| 303 | continue; |
| 304 | |
| 305 | rate = clk_get_rate(&clk); |
| 306 | printf("%s: %lu\n", clks[i].name, rate); |
| 307 | |
| 308 | clk_free(&clk); |
Simon Glass | 74e53e0 | 2016-01-21 19:45:07 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | U_BOOT_CMD( |
| 315 | clock, 2, 1, do_clock, |
| 316 | "display information about clocks", |
| 317 | "" |
| 318 | ); |
Simon Glass | fe97471 | 2017-05-31 17:57:33 -0600 | [diff] [blame] | 319 | |
Simon Glass | fe97471 | 2017-05-31 17:57:33 -0600 | [diff] [blame] | 320 | int board_early_init_f(void) |
| 321 | { |
Carlo Caione | 389167c | 2018-06-11 20:00:49 +0100 | [diff] [blame] | 322 | const uintptr_t GRF_SOC_CON0 = 0xff770244; |
| 323 | const uintptr_t GRF_SOC_CON2 = 0xff77024c; |
Simon Glass | fe97471 | 2017-05-31 17:57:33 -0600 | [diff] [blame] | 324 | struct udevice *dev; |
| 325 | int ret; |
| 326 | |
| 327 | /* |
| 328 | * This init is done in SPL, but when chain-loading U-Boot SPL will |
| 329 | * have been skipped. Allow the clock driver to check if it needs |
| 330 | * setting up. |
| 331 | */ |
| 332 | ret = rockchip_get_clk(&dev); |
| 333 | if (ret) { |
| 334 | debug("CLK init failed: %d\n", ret); |
| 335 | return ret; |
| 336 | } |
Simon Glass | fe97471 | 2017-05-31 17:57:33 -0600 | [diff] [blame] | 337 | |
Simon Glass | fe97471 | 2017-05-31 17:57:33 -0600 | [diff] [blame] | 338 | rk_setreg(GRF_SOC_CON2, 1 << 0); |
| 339 | |
Carlo Caione | 389167c | 2018-06-11 20:00:49 +0100 | [diff] [blame] | 340 | /* |
| 341 | * Disable JTAG on sdmmc0 IO. The SDMMC won't work until this bit is |
| 342 | * cleared |
| 343 | */ |
| 344 | rk_clrreg(GRF_SOC_CON0, 1 << 12); |
| 345 | |
Simon Glass | fe97471 | 2017-05-31 17:57:33 -0600 | [diff] [blame] | 346 | return 0; |
| 347 | } |