Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Kever Yang | 0d3d783 | 2016-07-19 21:16:59 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2016 Rockchip Electronics Co., Ltd |
Kever Yang | 0d3d783 | 2016-07-19 21:16:59 +0800 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Kever Yang | 9fbe17c | 2019-03-28 11:01:23 +0800 | [diff] [blame] | 7 | #include <spl_gpio.h> |
Kever Yang | 0d3d783 | 2016-07-19 21:16:59 +0800 | [diff] [blame] | 8 | #include <asm/armv8/mmu.h> |
Kever Yang | f3ea046 | 2016-10-07 15:56:16 +0800 | [diff] [blame] | 9 | #include <asm/io.h> |
Philipp Tomsich | c3ee462 | 2019-04-29 19:05:26 +0200 | [diff] [blame] | 10 | #include <asm/arch-rockchip/gpio.h> |
Kever Yang | 91379d9 | 2019-03-29 09:09:06 +0800 | [diff] [blame] | 11 | #include <asm/arch-rockchip/grf_rk3399.h> |
Kever Yang | 9fbe17c | 2019-03-28 11:01:23 +0800 | [diff] [blame] | 12 | #include <asm/arch-rockchip/hardware.h> |
Kever Yang | f3ea046 | 2016-10-07 15:56:16 +0800 | [diff] [blame] | 13 | |
Kever Yang | c205326 | 2017-06-23 16:11:11 +0800 | [diff] [blame] | 14 | DECLARE_GLOBAL_DATA_PTR; |
| 15 | |
Kever Yang | f3ea046 | 2016-10-07 15:56:16 +0800 | [diff] [blame] | 16 | #define GRF_EMMCCORE_CON11 0xff77f02c |
Kever Yang | 91379d9 | 2019-03-29 09:09:06 +0800 | [diff] [blame] | 17 | #define GRF_BASE 0xff770000 |
Kever Yang | 0d3d783 | 2016-07-19 21:16:59 +0800 | [diff] [blame] | 18 | |
| 19 | static struct mm_region rk3399_mem_map[] = { |
| 20 | { |
| 21 | .virt = 0x0UL, |
| 22 | .phys = 0x0UL, |
Kever Yang | da77e49 | 2017-04-17 16:42:44 +0800 | [diff] [blame] | 23 | .size = 0xf8000000UL, |
Kever Yang | 0d3d783 | 2016-07-19 21:16:59 +0800 | [diff] [blame] | 24 | .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) | |
| 25 | PTE_BLOCK_INNER_SHARE |
| 26 | }, { |
Kever Yang | da77e49 | 2017-04-17 16:42:44 +0800 | [diff] [blame] | 27 | .virt = 0xf8000000UL, |
| 28 | .phys = 0xf8000000UL, |
| 29 | .size = 0x08000000UL, |
Kever Yang | 0d3d783 | 2016-07-19 21:16:59 +0800 | [diff] [blame] | 30 | .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | |
| 31 | PTE_BLOCK_NON_SHARE | |
| 32 | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
| 33 | }, { |
| 34 | /* List terminator */ |
| 35 | 0, |
| 36 | } |
| 37 | }; |
| 38 | |
| 39 | struct mm_region *mem_map = rk3399_mem_map; |
Kever Yang | f3ea046 | 2016-10-07 15:56:16 +0800 | [diff] [blame] | 40 | |
Kever Yang | e937a99 | 2019-07-09 22:05:59 +0800 | [diff] [blame] | 41 | #ifdef CONFIG_SPL_BUILD |
| 42 | |
| 43 | #define TIMER_END_COUNT_L 0x00 |
| 44 | #define TIMER_END_COUNT_H 0x04 |
| 45 | #define TIMER_INIT_COUNT_L 0x10 |
| 46 | #define TIMER_INIT_COUNT_H 0x14 |
| 47 | #define TIMER_CONTROL_REG 0x1c |
| 48 | |
| 49 | #define TIMER_EN 0x1 |
| 50 | #define TIMER_FMODE BIT(0) |
| 51 | #define TIMER_RMODE BIT(1) |
| 52 | |
| 53 | void rockchip_stimer_init(void) |
| 54 | { |
| 55 | /* If Timer already enabled, don't re-init it */ |
| 56 | u32 reg = readl(CONFIG_ROCKCHIP_STIMER_BASE + TIMER_CONTROL_REG); |
| 57 | |
| 58 | if (reg & TIMER_EN) |
| 59 | return; |
| 60 | |
| 61 | writel(0xffffffff, CONFIG_ROCKCHIP_STIMER_BASE + TIMER_END_COUNT_L); |
| 62 | writel(0xffffffff, CONFIG_ROCKCHIP_STIMER_BASE + TIMER_END_COUNT_H); |
| 63 | writel(0, CONFIG_ROCKCHIP_STIMER_BASE + TIMER_INIT_COUNT_L); |
| 64 | writel(0, CONFIG_ROCKCHIP_STIMER_BASE + TIMER_INIT_COUNT_H); |
| 65 | writel(TIMER_EN | TIMER_FMODE, CONFIG_ROCKCHIP_STIMER_BASE + \ |
| 66 | TIMER_CONTROL_REG); |
| 67 | } |
| 68 | #endif |
| 69 | |
Kever Yang | c205326 | 2017-06-23 16:11:11 +0800 | [diff] [blame] | 70 | int dram_init_banksize(void) |
| 71 | { |
| 72 | size_t max_size = min((unsigned long)gd->ram_size, gd->ram_top); |
| 73 | |
| 74 | /* Reserve 0x200000 for ATF bl31 */ |
| 75 | gd->bd->bi_dram[0].start = 0x200000; |
| 76 | gd->bd->bi_dram[0].size = max_size - gd->bd->bi_dram[0].start; |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
Kever Yang | f3ea046 | 2016-10-07 15:56:16 +0800 | [diff] [blame] | 81 | int arch_cpu_init(void) |
| 82 | { |
| 83 | /* We do some SoC one time setting here. */ |
Kever Yang | 91379d9 | 2019-03-29 09:09:06 +0800 | [diff] [blame] | 84 | struct rk3399_grf_regs * const grf = (void *)GRF_BASE; |
Kever Yang | f3ea046 | 2016-10-07 15:56:16 +0800 | [diff] [blame] | 85 | |
| 86 | /* Emmc clock generator: disable the clock multipilier */ |
Kever Yang | 91379d9 | 2019-03-29 09:09:06 +0800 | [diff] [blame] | 87 | rk_clrreg(&grf->emmccore_con[11], 0x0ff); |
Kever Yang | f3ea046 | 2016-10-07 15:56:16 +0800 | [diff] [blame] | 88 | |
| 89 | return 0; |
| 90 | } |
Kever Yang | 0f7c824 | 2019-03-29 09:09:07 +0800 | [diff] [blame] | 91 | |
| 92 | #ifdef CONFIG_DEBUG_UART_BOARD_INIT |
| 93 | void board_debug_uart_init(void) |
| 94 | { |
| 95 | #define GRF_BASE 0xff770000 |
| 96 | #define GPIO0_BASE 0xff720000 |
| 97 | #define PMUGRF_BASE 0xff320000 |
| 98 | struct rk3399_grf_regs * const grf = (void *)GRF_BASE; |
| 99 | #ifdef CONFIG_TARGET_CHROMEBOOK_BOB |
| 100 | struct rk3399_pmugrf_regs * const pmugrf = (void *)PMUGRF_BASE; |
| 101 | struct rockchip_gpio_regs * const gpio = (void *)GPIO0_BASE; |
| 102 | #endif |
| 103 | |
| 104 | #if defined(CONFIG_DEBUG_UART_BASE) && (CONFIG_DEBUG_UART_BASE == 0xff180000) |
| 105 | /* Enable early UART0 on the RK3399 */ |
| 106 | rk_clrsetreg(&grf->gpio2c_iomux, |
| 107 | GRF_GPIO2C0_SEL_MASK, |
| 108 | GRF_UART0BT_SIN << GRF_GPIO2C0_SEL_SHIFT); |
| 109 | rk_clrsetreg(&grf->gpio2c_iomux, |
| 110 | GRF_GPIO2C1_SEL_MASK, |
| 111 | GRF_UART0BT_SOUT << GRF_GPIO2C1_SEL_SHIFT); |
Christoph Muellner | fca4476 | 2019-05-07 10:58:43 +0200 | [diff] [blame] | 112 | #elif defined(CONFIG_DEBUG_UART_BASE) && (CONFIG_DEBUG_UART_BASE == 0xff1B0000) |
| 113 | /* Enable early UART3 on the RK3399 */ |
| 114 | rk_clrsetreg(&grf->gpio3b_iomux, |
| 115 | GRF_GPIO3B6_SEL_MASK, |
| 116 | GRF_UART3_SIN << GRF_GPIO3B6_SEL_SHIFT); |
| 117 | rk_clrsetreg(&grf->gpio3b_iomux, |
| 118 | GRF_GPIO3B7_SEL_MASK, |
| 119 | GRF_UART3_SOUT << GRF_GPIO3B7_SEL_SHIFT); |
Kever Yang | 0f7c824 | 2019-03-29 09:09:07 +0800 | [diff] [blame] | 120 | #else |
| 121 | # ifdef CONFIG_TARGET_CHROMEBOOK_BOB |
| 122 | rk_setreg(&grf->io_vsel, 1 << 0); |
| 123 | |
| 124 | /* |
| 125 | * Let's enable these power rails here, we are already running the SPI |
| 126 | * Flash based code. |
| 127 | */ |
| 128 | spl_gpio_output(gpio, GPIO(BANK_B, 2), 1); /* PP1500_EN */ |
| 129 | spl_gpio_set_pull(&pmugrf->gpio0_p, GPIO(BANK_B, 2), GPIO_PULL_NORMAL); |
| 130 | |
| 131 | spl_gpio_output(gpio, GPIO(BANK_B, 4), 1); /* PP3000_EN */ |
| 132 | spl_gpio_set_pull(&pmugrf->gpio0_p, GPIO(BANK_B, 4), GPIO_PULL_NORMAL); |
| 133 | #endif /* CONFIG_TARGET_CHROMEBOOK_BOB */ |
| 134 | |
| 135 | /* Enable early UART2 channel C on the RK3399 */ |
| 136 | rk_clrsetreg(&grf->gpio4c_iomux, |
| 137 | GRF_GPIO4C3_SEL_MASK, |
| 138 | GRF_UART2DGBC_SIN << GRF_GPIO4C3_SEL_SHIFT); |
| 139 | rk_clrsetreg(&grf->gpio4c_iomux, |
| 140 | GRF_GPIO4C4_SEL_MASK, |
| 141 | GRF_UART2DBGC_SOUT << GRF_GPIO4C4_SEL_SHIFT); |
| 142 | /* Set channel C as UART2 input */ |
| 143 | rk_clrsetreg(&grf->soc_con7, |
| 144 | GRF_UART_DBG_SEL_MASK, |
| 145 | GRF_UART_DBG_SEL_C << GRF_UART_DBG_SEL_SHIFT); |
| 146 | #endif |
| 147 | } |
| 148 | #endif |
Kever Yang | e5a5961 | 2019-07-22 19:59:36 +0800 | [diff] [blame^] | 149 | |
| 150 | #ifdef CONFIG_SPL_BUILD |
| 151 | const char *spl_decode_boot_device(u32 boot_device) |
| 152 | { |
| 153 | int i; |
| 154 | static const struct { |
| 155 | u32 boot_device; |
| 156 | const char *ofpath; |
| 157 | } spl_boot_devices_tbl[] = { |
| 158 | { BOOT_DEVICE_MMC1, "/dwmmc@fe320000" }, |
| 159 | { BOOT_DEVICE_MMC2, "/sdhci@fe330000" }, |
| 160 | { BOOT_DEVICE_SPI, "/spi@ff1d0000" }, |
| 161 | }; |
| 162 | |
| 163 | for (i = 0; i < ARRAY_SIZE(spl_boot_devices_tbl); ++i) |
| 164 | if (spl_boot_devices_tbl[i].boot_device == boot_device) |
| 165 | return spl_boot_devices_tbl[i].ofpath; |
| 166 | |
| 167 | return NULL; |
| 168 | } |
| 169 | |
| 170 | void spl_perform_fixups(struct spl_image_info *spl_image) |
| 171 | { |
| 172 | void *blob = spl_image->fdt_addr; |
| 173 | const char *boot_ofpath; |
| 174 | int chosen; |
| 175 | |
| 176 | /* |
| 177 | * Inject the ofpath of the device the full U-Boot (or Linux in |
| 178 | * Falcon-mode) was booted from into the FDT, if a FDT has been |
| 179 | * loaded at the same time. |
| 180 | */ |
| 181 | if (!blob) |
| 182 | return; |
| 183 | |
| 184 | boot_ofpath = spl_decode_boot_device(spl_image->boot_device); |
| 185 | if (!boot_ofpath) { |
| 186 | pr_err("%s: could not map boot_device to ofpath\n", __func__); |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | chosen = fdt_find_or_add_subnode(blob, 0, "chosen"); |
| 191 | if (chosen < 0) { |
| 192 | pr_err("%s: could not find/create '/chosen'\n", __func__); |
| 193 | return; |
| 194 | } |
| 195 | fdt_setprop_string(blob, chosen, |
| 196 | "u-boot,spl-boot-device", boot_ofpath); |
| 197 | } |
| 198 | #endif |