Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Heiko Stübner | 0b3c26a | 2017-02-18 19:46:38 +0100 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2015 Google, Inc |
Heiko Stübner | 0b3c26a | 2017-02-18 19:46:38 +0100 | [diff] [blame] | 4 | */ |
| 5 | |
Heiko Stübner | ba188bb | 2017-03-20 12:40:33 +0100 | [diff] [blame] | 6 | #include <clk.h> |
Heiko Stübner | 0b3c26a | 2017-02-18 19:46:38 +0100 | [diff] [blame] | 7 | #include <common.h> |
| 8 | #include <debug_uart.h> |
| 9 | #include <dm.h> |
| 10 | #include <fdtdec.h> |
| 11 | #include <led.h> |
| 12 | #include <malloc.h> |
| 13 | #include <ram.h> |
| 14 | #include <spl.h> |
Simon Glass | f473eba | 2019-01-21 14:53:31 -0700 | [diff] [blame] | 15 | #include <syscon.h> |
Heiko Stübner | 0b3c26a | 2017-02-18 19:46:38 +0100 | [diff] [blame] | 16 | #include <asm/gpio.h> |
| 17 | #include <asm/io.h> |
| 18 | #include <asm/arch/bootrom.h> |
| 19 | #include <asm/arch/clock.h> |
Heiko Stuebner | 9cc8feb | 2018-10-08 13:01:56 +0200 | [diff] [blame] | 20 | #include <asm/arch/grf_rk3188.h> |
Heiko Stübner | 0b3c26a | 2017-02-18 19:46:38 +0100 | [diff] [blame] | 21 | #include <asm/arch/hardware.h> |
| 22 | #include <asm/arch/periph.h> |
| 23 | #include <asm/arch/pmu_rk3188.h> |
| 24 | #include <asm/arch/sdram.h> |
| 25 | #include <asm/arch/timer.h> |
| 26 | #include <dm/pinctrl.h> |
| 27 | #include <dm/root.h> |
| 28 | #include <dm/test.h> |
| 29 | #include <dm/util.h> |
| 30 | #include <power/regulator.h> |
Heiko Stübner | 0b3c26a | 2017-02-18 19:46:38 +0100 | [diff] [blame] | 31 | |
| 32 | DECLARE_GLOBAL_DATA_PTR; |
| 33 | |
| 34 | u32 spl_boot_device(void) |
| 35 | { |
| 36 | #if !CONFIG_IS_ENABLED(OF_PLATDATA) |
| 37 | const void *blob = gd->fdt_blob; |
| 38 | struct udevice *dev; |
| 39 | const char *bootdev; |
| 40 | int node; |
| 41 | int ret; |
| 42 | |
| 43 | bootdev = fdtdec_get_config_string(blob, "u-boot,boot0"); |
| 44 | debug("Boot device %s\n", bootdev); |
| 45 | if (!bootdev) |
| 46 | goto fallback; |
| 47 | |
| 48 | node = fdt_path_offset(blob, bootdev); |
| 49 | if (node < 0) { |
| 50 | debug("node=%d\n", node); |
| 51 | goto fallback; |
| 52 | } |
Jean-Jacques Hiblot | a7b0d6a | 2018-08-09 16:17:44 +0200 | [diff] [blame] | 53 | ret = device_get_global_by_ofnode(offset_to_ofnode(node), &dev); |
Heiko Stübner | 0b3c26a | 2017-02-18 19:46:38 +0100 | [diff] [blame] | 54 | if (ret) { |
| 55 | debug("device at node %s/%d not found: %d\n", bootdev, node, |
| 56 | ret); |
| 57 | goto fallback; |
| 58 | } |
| 59 | debug("Found device %s\n", dev->name); |
| 60 | switch (device_get_uclass_id(dev)) { |
| 61 | case UCLASS_SPI_FLASH: |
| 62 | return BOOT_DEVICE_SPI; |
| 63 | case UCLASS_MMC: |
| 64 | return BOOT_DEVICE_MMC1; |
| 65 | default: |
| 66 | debug("Booting from device uclass '%s' not supported\n", |
| 67 | dev_get_uclass_name(dev)); |
| 68 | } |
| 69 | |
| 70 | fallback: |
| 71 | #endif |
| 72 | return BOOT_DEVICE_MMC1; |
| 73 | } |
| 74 | |
Heiko Stübner | ba188bb | 2017-03-20 12:40:33 +0100 | [diff] [blame] | 75 | static int setup_arm_clock(void) |
| 76 | { |
| 77 | struct udevice *dev; |
| 78 | struct clk clk; |
| 79 | int ret; |
| 80 | |
| 81 | ret = rockchip_get_clk(&dev); |
| 82 | if (ret) |
| 83 | return ret; |
| 84 | |
| 85 | clk.id = CLK_ARM; |
| 86 | ret = clk_request(dev, &clk); |
| 87 | if (ret < 0) |
| 88 | return ret; |
| 89 | |
| 90 | ret = clk_set_rate(&clk, 600000000); |
| 91 | |
| 92 | clk_free(&clk); |
| 93 | return ret; |
| 94 | } |
| 95 | |
Kever Yang | d2af98b | 2018-11-29 10:07:38 +0800 | [diff] [blame] | 96 | void board_debug_uart_init(void) |
Heiko Stübner | 0b3c26a | 2017-02-18 19:46:38 +0100 | [diff] [blame] | 97 | { |
Kever Yang | d2af98b | 2018-11-29 10:07:38 +0800 | [diff] [blame] | 98 | /* Enable early UART on the RK3188 */ |
| 99 | #define GRF_BASE 0x20008000 |
| 100 | struct rk3188_grf * const grf = (void *)GRF_BASE; |
Heiko Stuebner | c524eeb | 2018-10-08 13:01:57 +0200 | [diff] [blame] | 101 | enum { |
| 102 | GPIO1B1_SHIFT = 2, |
| 103 | GPIO1B1_MASK = 3, |
Kever Yang | d2af98b | 2018-11-29 10:07:38 +0800 | [diff] [blame] | 104 | GPIO1B1_GPIO = 0, |
| 105 | GPIO1B1_UART2_SOUT, |
Heiko Stuebner | c524eeb | 2018-10-08 13:01:57 +0200 | [diff] [blame] | 106 | |
| 107 | GPIO1B0_SHIFT = 0, |
| 108 | GPIO1B0_MASK = 3, |
Kever Yang | d2af98b | 2018-11-29 10:07:38 +0800 | [diff] [blame] | 109 | GPIO1B0_GPIO = 0, |
| 110 | GPIO1B0_UART2_SIN, |
Heiko Stuebner | c524eeb | 2018-10-08 13:01:57 +0200 | [diff] [blame] | 111 | }; |
| 112 | |
Heiko Stübner | 0b3c26a | 2017-02-18 19:46:38 +0100 | [diff] [blame] | 113 | /* Enable early UART on the RK3188 */ |
Heiko Stübner | 0b3c26a | 2017-02-18 19:46:38 +0100 | [diff] [blame] | 114 | rk_clrsetreg(&grf->gpio1b_iomux, |
| 115 | GPIO1B1_MASK << GPIO1B1_SHIFT | |
| 116 | GPIO1B0_MASK << GPIO1B0_SHIFT, |
| 117 | GPIO1B1_UART2_SOUT << GPIO1B1_SHIFT | |
| 118 | GPIO1B0_UART2_SIN << GPIO1B0_SHIFT); |
Kever Yang | d2af98b | 2018-11-29 10:07:38 +0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void board_init_f(ulong dummy) |
| 122 | { |
David Wu | cee4c82 | 2019-01-02 20:50:58 +0800 | [diff] [blame] | 123 | struct udevice *dev; |
Kever Yang | d2af98b | 2018-11-29 10:07:38 +0800 | [diff] [blame] | 124 | int ret; |
| 125 | |
| 126 | #define EARLY_UART |
| 127 | #ifdef EARLY_UART |
Heiko Stübner | 0b3c26a | 2017-02-18 19:46:38 +0100 | [diff] [blame] | 128 | /* |
| 129 | * Debug UART can be used from here if required: |
| 130 | * |
| 131 | * debug_uart_init(); |
| 132 | * printch('a'); |
| 133 | * printhex8(0x1234); |
| 134 | * printascii("string"); |
| 135 | */ |
| 136 | debug_uart_init(); |
David Wu | cee4c82 | 2019-01-02 20:50:58 +0800 | [diff] [blame] | 137 | printascii("U-Boot SPL board init"); |
Heiko Stübner | 0b3c26a | 2017-02-18 19:46:38 +0100 | [diff] [blame] | 138 | #endif |
| 139 | |
Heiko Stuebner | 9cc8feb | 2018-10-08 13:01:56 +0200 | [diff] [blame] | 140 | #ifdef CONFIG_ROCKCHIP_USB_UART |
| 141 | rk_clrsetreg(&grf->uoc0_con[0], |
| 142 | SIDDQ_MASK | UOC_DISABLE_MASK | COMMON_ON_N_MASK, |
| 143 | 1 << SIDDQ_SHIFT | 1 << UOC_DISABLE_SHIFT | |
| 144 | 1 << COMMON_ON_N_SHIFT); |
| 145 | rk_clrsetreg(&grf->uoc0_con[2], |
| 146 | SOFT_CON_SEL_MASK, 1 << SOFT_CON_SEL_SHIFT); |
| 147 | rk_clrsetreg(&grf->uoc0_con[3], |
| 148 | OPMODE_MASK | XCVRSELECT_MASK | |
| 149 | TERMSEL_FULLSPEED_MASK | SUSPENDN_MASK, |
| 150 | OPMODE_NODRIVING << OPMODE_SHIFT | |
| 151 | XCVRSELECT_FSTRANSC << XCVRSELECT_SHIFT | |
| 152 | 1 << TERMSEL_FULLSPEED_SHIFT | |
| 153 | 1 << SUSPENDN_SHIFT); |
| 154 | rk_clrsetreg(&grf->uoc0_con[0], |
| 155 | BYPASSSEL_MASK | BYPASSDMEN_MASK, |
| 156 | 1 << BYPASSSEL_SHIFT | 1 << BYPASSDMEN_SHIFT); |
| 157 | #endif |
| 158 | |
Kever Yang | e603a3d | 2017-03-20 14:47:16 +0800 | [diff] [blame] | 159 | ret = spl_early_init(); |
Heiko Stübner | 0b3c26a | 2017-02-18 19:46:38 +0100 | [diff] [blame] | 160 | if (ret) { |
Kever Yang | e603a3d | 2017-03-20 14:47:16 +0800 | [diff] [blame] | 161 | debug("spl_early_init() failed: %d\n", ret); |
Heiko Stübner | 0b3c26a | 2017-02-18 19:46:38 +0100 | [diff] [blame] | 162 | hang(); |
| 163 | } |
| 164 | |
Heiko Stübner | 0b3c26a | 2017-02-18 19:46:38 +0100 | [diff] [blame] | 165 | ret = rockchip_get_clk(&dev); |
| 166 | if (ret) { |
| 167 | debug("CLK init failed: %d\n", ret); |
| 168 | return; |
| 169 | } |
| 170 | |
Heiko Stübner | 0b3c26a | 2017-02-18 19:46:38 +0100 | [diff] [blame] | 171 | ret = uclass_get_device(UCLASS_RAM, 0, &dev); |
| 172 | if (ret) { |
| 173 | debug("DRAM init failed: %d\n", ret); |
| 174 | return; |
| 175 | } |
| 176 | |
Heiko Stübner | ba188bb | 2017-03-20 12:40:33 +0100 | [diff] [blame] | 177 | setup_arm_clock(); |
Philipp Tomsich | 798370f | 2017-06-29 11:21:15 +0200 | [diff] [blame] | 178 | #if CONFIG_IS_ENABLED(ROCKCHIP_BACK_TO_BROM) && !defined(CONFIG_SPL_BOARD_INIT) |
Philipp Tomsich | 7234c73 | 2017-10-10 16:21:16 +0200 | [diff] [blame] | 179 | back_to_bootrom(BROM_BOOT_NEXTSTAGE); |
Heiko Stübner | 0b3c26a | 2017-02-18 19:46:38 +0100 | [diff] [blame] | 180 | #endif |
| 181 | } |
| 182 | |
| 183 | static int setup_led(void) |
| 184 | { |
| 185 | #ifdef CONFIG_SPL_LED |
| 186 | struct udevice *dev; |
| 187 | char *led_name; |
| 188 | int ret; |
| 189 | |
| 190 | led_name = fdtdec_get_config_string(gd->fdt_blob, "u-boot,boot-led"); |
| 191 | if (!led_name) |
| 192 | return 0; |
| 193 | ret = led_get_by_label(led_name, &dev); |
| 194 | if (ret) { |
| 195 | debug("%s: get=%d\n", __func__, ret); |
| 196 | return ret; |
| 197 | } |
| 198 | ret = led_set_on(dev, 1); |
| 199 | if (ret) |
| 200 | return ret; |
| 201 | #endif |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | void spl_board_init(void) |
| 207 | { |
Heiko Stübner | 0b3c26a | 2017-02-18 19:46:38 +0100 | [diff] [blame] | 208 | int ret; |
| 209 | |
| 210 | ret = setup_led(); |
| 211 | if (ret) { |
| 212 | debug("LED ret=%d\n", ret); |
| 213 | hang(); |
| 214 | } |
| 215 | |
Heiko Stübner | 0b3c26a | 2017-02-18 19:46:38 +0100 | [diff] [blame] | 216 | preloader_console_init(); |
Philipp Tomsich | 798370f | 2017-06-29 11:21:15 +0200 | [diff] [blame] | 217 | #if CONFIG_IS_ENABLED(ROCKCHIP_BACK_TO_BROM) |
Philipp Tomsich | 7234c73 | 2017-10-10 16:21:16 +0200 | [diff] [blame] | 218 | back_to_bootrom(BROM_BOOT_NEXTSTAGE); |
Heiko Stübner | 0b3c26a | 2017-02-18 19:46:38 +0100 | [diff] [blame] | 219 | #endif |
| 220 | return; |
Heiko Stübner | 0b3c26a | 2017-02-18 19:46:38 +0100 | [diff] [blame] | 221 | } |