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> |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 12 | #include <asm/arch/clock.h> |
Wadim Egorov | 40d4f79 | 2017-08-21 13:36:57 +0200 | [diff] [blame] | 13 | #include <asm/arch/cru_rk3288.h> |
Xu Ziyuan | b47ea79 | 2016-07-12 19:09:49 +0800 | [diff] [blame] | 14 | #include <asm/arch/periph.h> |
Jacob Chen | 67171e1 | 2016-09-19 18:46:28 +0800 | [diff] [blame] | 15 | #include <asm/arch/pmu_rk3288.h> |
Nickey Yang Nickey Yang | 9b83201 | 2016-12-29 10:47:30 +0800 | [diff] [blame] | 16 | #include <asm/arch/qos_rk3288.h> |
Jacob Chen | 67171e1 | 2016-09-19 18:46:28 +0800 | [diff] [blame] | 17 | #include <asm/arch/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 | 0d4d5fd | 2018-06-11 20:00:48 +0100 | [diff] [blame^] | 125 | ret = regulators_enable_boot_on(false); |
| 126 | if (ret) { |
| 127 | debug("%s: Cannot enable boot on regulators\n", __func__); |
| 128 | return ret; |
| 129 | } |
| 130 | |
Simon Glass | 20b13e8 | 2016-11-13 14:22:14 -0700 | [diff] [blame] | 131 | return 0; |
| 132 | } |
| 133 | #endif |
| 134 | |
Simon Glass | 2444dae | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 135 | int board_init(void) |
| 136 | { |
Philipp Tomsich | ee14d29 | 2017-06-29 11:21:15 +0200 | [diff] [blame] | 137 | #if CONFIG_IS_ENABLED(ROCKCHIP_BACK_TO_BROM) |
Xu Ziyuan | b47ea79 | 2016-07-12 19:09:49 +0800 | [diff] [blame] | 138 | struct udevice *pinctrl; |
| 139 | int ret; |
| 140 | |
Jacob Chen | 67171e1 | 2016-09-19 18:46:28 +0800 | [diff] [blame] | 141 | /* |
| 142 | * We need to implement sdcard iomux here for the further |
| 143 | * initlization, otherwise, it'll hit sdcard command sending |
| 144 | * timeout exception. |
| 145 | */ |
Xu Ziyuan | b47ea79 | 2016-07-12 19:09:49 +0800 | [diff] [blame] | 146 | ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl); |
| 147 | if (ret) { |
| 148 | debug("%s: Cannot find pinctrl device\n", __func__); |
| 149 | goto err; |
| 150 | } |
| 151 | ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_SDCARD); |
| 152 | if (ret) { |
| 153 | debug("%s: Failed to set up SD card\n", __func__); |
| 154 | goto err; |
| 155 | } |
| 156 | |
Simon Glass | 2444dae | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 157 | return 0; |
Xu Ziyuan | b47ea79 | 2016-07-12 19:09:49 +0800 | [diff] [blame] | 158 | err: |
| 159 | printf("board_init: Error %d\n", ret); |
| 160 | |
| 161 | /* No way to report error here */ |
| 162 | hang(); |
| 163 | |
| 164 | return -1; |
| 165 | #else |
Simon Glass | 20b13e8 | 2016-11-13 14:22:14 -0700 | [diff] [blame] | 166 | int ret; |
| 167 | |
| 168 | /* We do some SoC one time setting here */ |
| 169 | if (!fdt_node_check_compatible(gd->fdt_blob, 0, "google,veyron")) { |
| 170 | ret = veyron_init(); |
| 171 | if (ret) |
| 172 | return ret; |
| 173 | } |
| 174 | |
Xu Ziyuan | b47ea79 | 2016-07-12 19:09:49 +0800 | [diff] [blame] | 175 | return 0; |
| 176 | #endif |
Simon Glass | 2444dae | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 177 | } |
| 178 | |
Simon Glass | 2444dae | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 179 | #ifndef CONFIG_SYS_DCACHE_OFF |
| 180 | void enable_caches(void) |
| 181 | { |
| 182 | /* Enable D-cache. I-cache is already enabled in start.S */ |
| 183 | dcache_enable(); |
| 184 | } |
| 185 | #endif |
Simon Glass | ad443b7 | 2016-01-21 19:45:06 -0700 | [diff] [blame] | 186 | |
Xu Ziyuan | 266c8fa | 2016-07-15 00:26:59 +0800 | [diff] [blame] | 187 | #if defined(CONFIG_USB_GADGET) && defined(CONFIG_USB_GADGET_DWC2_OTG) |
| 188 | #include <usb.h> |
| 189 | #include <usb/dwc2_udc.h> |
| 190 | |
| 191 | static struct dwc2_plat_otg_data rk3288_otg_data = { |
| 192 | .rx_fifo_sz = 512, |
| 193 | .np_tx_fifo_sz = 16, |
| 194 | .tx_fifo_sz = 128, |
| 195 | }; |
| 196 | |
| 197 | int board_usb_init(int index, enum usb_init_type init) |
| 198 | { |
| 199 | int node, phy_node; |
| 200 | const char *mode; |
| 201 | bool matched = false; |
| 202 | const void *blob = gd->fdt_blob; |
| 203 | u32 grf_phy_offset; |
| 204 | |
| 205 | /* find the usb_otg node */ |
| 206 | node = fdt_node_offset_by_compatible(blob, -1, |
| 207 | "rockchip,rk3288-usb"); |
| 208 | |
| 209 | while (node > 0) { |
| 210 | mode = fdt_getprop(blob, node, "dr_mode", NULL); |
| 211 | if (mode && strcmp(mode, "otg") == 0) { |
| 212 | matched = true; |
| 213 | break; |
| 214 | } |
| 215 | |
| 216 | node = fdt_node_offset_by_compatible(blob, node, |
| 217 | "rockchip,rk3288-usb"); |
| 218 | } |
| 219 | if (!matched) { |
| 220 | debug("Not found usb_otg device\n"); |
| 221 | return -ENODEV; |
| 222 | } |
| 223 | rk3288_otg_data.regs_otg = fdtdec_get_addr(blob, node, "reg"); |
| 224 | |
| 225 | node = fdtdec_lookup_phandle(blob, node, "phys"); |
| 226 | if (node <= 0) { |
| 227 | debug("Not found usb phy device\n"); |
| 228 | return -ENODEV; |
| 229 | } |
| 230 | |
| 231 | phy_node = fdt_parent_offset(blob, node); |
| 232 | if (phy_node <= 0) { |
| 233 | debug("Not found usb phy device\n"); |
| 234 | return -ENODEV; |
| 235 | } |
| 236 | |
| 237 | rk3288_otg_data.phy_of_node = phy_node; |
| 238 | grf_phy_offset = fdtdec_get_addr(blob, node, "reg"); |
| 239 | |
| 240 | /* find the grf node */ |
| 241 | node = fdt_node_offset_by_compatible(blob, -1, |
| 242 | "rockchip,rk3288-grf"); |
| 243 | if (node <= 0) { |
| 244 | debug("Not found grf device\n"); |
| 245 | return -ENODEV; |
| 246 | } |
| 247 | rk3288_otg_data.regs_phy = grf_phy_offset + |
| 248 | fdtdec_get_addr(blob, node, "reg"); |
| 249 | |
| 250 | return dwc2_udc_probe(&rk3288_otg_data); |
| 251 | } |
| 252 | |
| 253 | int board_usb_cleanup(int index, enum usb_init_type init) |
| 254 | { |
| 255 | return 0; |
| 256 | } |
| 257 | #endif |
| 258 | |
Simon Glass | 74e53e0 | 2016-01-21 19:45:07 -0700 | [diff] [blame] | 259 | static int do_clock(cmd_tbl_t *cmdtp, int flag, int argc, |
| 260 | char * const argv[]) |
| 261 | { |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 262 | static const struct { |
| 263 | char *name; |
| 264 | int id; |
| 265 | } clks[] = { |
| 266 | { "osc", CLK_OSC }, |
| 267 | { "apll", CLK_ARM }, |
| 268 | { "dpll", CLK_DDR }, |
| 269 | { "cpll", CLK_CODEC }, |
| 270 | { "gpll", CLK_GENERAL }, |
| 271 | #ifdef CONFIG_ROCKCHIP_RK3036 |
| 272 | { "mpll", CLK_NEW }, |
| 273 | #else |
| 274 | { "npll", CLK_NEW }, |
| 275 | #endif |
| 276 | }; |
| 277 | int ret, i; |
Simon Glass | 74e53e0 | 2016-01-21 19:45:07 -0700 | [diff] [blame] | 278 | struct udevice *dev; |
| 279 | |
Simon Glass | c3aad6f | 2016-07-17 15:23:17 -0600 | [diff] [blame] | 280 | ret = rockchip_get_clk(&dev); |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 281 | if (ret) { |
| 282 | printf("clk-uclass not found\n"); |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | for (i = 0; i < ARRAY_SIZE(clks); i++) { |
| 287 | struct clk clk; |
Simon Glass | 74e53e0 | 2016-01-21 19:45:07 -0700 | [diff] [blame] | 288 | ulong rate; |
| 289 | |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 290 | clk.id = clks[i].id; |
| 291 | ret = clk_request(dev, &clk); |
| 292 | if (ret < 0) |
| 293 | continue; |
| 294 | |
| 295 | rate = clk_get_rate(&clk); |
| 296 | printf("%s: %lu\n", clks[i].name, rate); |
| 297 | |
| 298 | clk_free(&clk); |
Simon Glass | 74e53e0 | 2016-01-21 19:45:07 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | U_BOOT_CMD( |
| 305 | clock, 2, 1, do_clock, |
| 306 | "display information about clocks", |
| 307 | "" |
| 308 | ); |
Simon Glass | fe97471 | 2017-05-31 17:57:33 -0600 | [diff] [blame] | 309 | |
| 310 | #define GRF_SOC_CON2 0xff77024c |
| 311 | |
| 312 | int board_early_init_f(void) |
| 313 | { |
| 314 | struct udevice *pinctrl; |
| 315 | struct udevice *dev; |
| 316 | int ret; |
| 317 | |
| 318 | /* |
| 319 | * This init is done in SPL, but when chain-loading U-Boot SPL will |
| 320 | * have been skipped. Allow the clock driver to check if it needs |
| 321 | * setting up. |
| 322 | */ |
| 323 | ret = rockchip_get_clk(&dev); |
| 324 | if (ret) { |
| 325 | debug("CLK init failed: %d\n", ret); |
| 326 | return ret; |
| 327 | } |
| 328 | ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl); |
| 329 | if (ret) { |
| 330 | debug("%s: Cannot find pinctrl device\n", __func__); |
| 331 | return ret; |
| 332 | } |
| 333 | |
| 334 | /* Enable debug UART */ |
| 335 | ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_UART_DBG); |
| 336 | if (ret) { |
| 337 | debug("%s: Failed to set up console UART\n", __func__); |
| 338 | return ret; |
| 339 | } |
| 340 | rk_setreg(GRF_SOC_CON2, 1 << 0); |
| 341 | |
| 342 | return 0; |
| 343 | } |