Caesar Wang | 3e3c5b0 | 2016-05-25 19:03:04 +0800 | [diff] [blame] | 1 | /* |
Julius Werner | 65d5267 | 2019-05-24 20:37:58 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016-2019, ARM Limited and Contributors. All rights reserved. |
Caesar Wang | 3e3c5b0 | 2016-05-25 19:03:04 +0800 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Caesar Wang | 3e3c5b0 | 2016-05-25 19:03:04 +0800 | [diff] [blame] | 5 | */ |
| 6 | |
Caesar Wang | 3e3c5b0 | 2016-05-25 19:03:04 +0800 | [diff] [blame] | 7 | #include <assert.h> |
Heiko Stuebner | 6fd5b94 | 2019-04-24 20:26:51 +0200 | [diff] [blame] | 8 | #include <errno.h> |
Vasily Khoruzhick | 75afb1d | 2019-11-15 08:25:02 -0800 | [diff] [blame] | 9 | #include <limits.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 10 | #include <string.h> |
| 11 | |
Julius Werner | 65d5267 | 2019-05-24 20:37:58 -0700 | [diff] [blame] | 12 | #include <lib/bl_aux_params/bl_aux_params.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 13 | #include <common/bl_common.h> |
| 14 | #include <common/debug.h> |
| 15 | #include <drivers/console.h> |
| 16 | #include <drivers/gpio.h> |
Heiko Stuebner | bbd0f5a | 2019-03-07 08:07:11 +0100 | [diff] [blame] | 17 | #include <libfdt.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 18 | #include <lib/coreboot.h> |
| 19 | #include <lib/mmio.h> |
| 20 | #include <plat/common/platform.h> |
| 21 | |
Caesar Wang | 3e3c5b0 | 2016-05-25 19:03:04 +0800 | [diff] [blame] | 22 | #include <plat_params.h> |
| 23 | #include <plat_private.h> |
Caesar Wang | 3e3c5b0 | 2016-05-25 19:03:04 +0800 | [diff] [blame] | 24 | |
Vasily Khoruzhick | 75afb1d | 2019-11-15 08:25:02 -0800 | [diff] [blame] | 25 | static struct bl_aux_gpio_info rst_gpio = { .index = UINT_MAX } ; |
| 26 | static struct bl_aux_gpio_info poweroff_gpio = { .index = UINT_MAX }; |
Julius Werner | 65d5267 | 2019-05-24 20:37:58 -0700 | [diff] [blame] | 27 | static struct bl_aux_gpio_info suspend_gpio[10]; |
Caesar Wang | ef18007 | 2016-09-10 02:43:15 +0800 | [diff] [blame] | 28 | uint32_t suspend_gpio_cnt; |
Julius Werner | 65d5267 | 2019-05-24 20:37:58 -0700 | [diff] [blame] | 29 | static struct bl_aux_rk_apio_info suspend_apio; |
Heiko Stuebner | 42aba05 | 2019-08-05 14:46:00 +0200 | [diff] [blame] | 30 | |
Heiko Stuebner | 6fd5b94 | 2019-04-24 20:26:51 +0200 | [diff] [blame] | 31 | #if COREBOOT |
Julius Werner | 65d5267 | 2019-05-24 20:37:58 -0700 | [diff] [blame] | 32 | static int dt_process_fdt(u_register_t param_from_bl2) |
Heiko Stuebner | 6fd5b94 | 2019-04-24 20:26:51 +0200 | [diff] [blame] | 33 | { |
| 34 | return -ENODEV; |
| 35 | } |
| 36 | #else |
Heiko Stuebner | 40b3cb1 | 2019-08-05 16:40:35 +0200 | [diff] [blame] | 37 | static uint32_t rk_uart_base = PLAT_RK_UART_BASE; |
| 38 | static uint32_t rk_uart_baudrate = PLAT_RK_UART_BAUDRATE; |
| 39 | static uint32_t rk_uart_clock = PLAT_RK_UART_CLOCK; |
Heiko Stuebner | bbd0f5a | 2019-03-07 08:07:11 +0100 | [diff] [blame] | 40 | static uint8_t fdt_buffer[0x10000]; |
| 41 | |
| 42 | void *plat_get_fdt(void) |
| 43 | { |
| 44 | return &fdt_buffer[0]; |
| 45 | } |
| 46 | |
Christoph Müllner | cb9204a | 2019-04-19 14:16:27 +0200 | [diff] [blame] | 47 | static void plat_rockchip_dt_process_fdt_uart(void *fdt) |
| 48 | { |
| 49 | const char *path_name = "/chosen"; |
| 50 | const char *prop_name = "stdout-path"; |
| 51 | int node_offset; |
| 52 | int stdout_path_len; |
| 53 | const char *stdout_path; |
Heiko Stuebner | 42aba05 | 2019-08-05 14:46:00 +0200 | [diff] [blame] | 54 | const char *separator; |
| 55 | const char *baud_start; |
Christoph Müllner | cb9204a | 2019-04-19 14:16:27 +0200 | [diff] [blame] | 56 | char serial_char; |
| 57 | int serial_no; |
| 58 | uint32_t uart_base; |
Heiko Stuebner | 42aba05 | 2019-08-05 14:46:00 +0200 | [diff] [blame] | 59 | uint32_t baud; |
Christoph Müllner | cb9204a | 2019-04-19 14:16:27 +0200 | [diff] [blame] | 60 | |
| 61 | node_offset = fdt_path_offset(fdt, path_name); |
| 62 | if (node_offset < 0) |
| 63 | return; |
| 64 | |
| 65 | stdout_path = fdt_getprop(fdt, node_offset, prop_name, |
| 66 | &stdout_path_len); |
| 67 | if (stdout_path == NULL) |
| 68 | return; |
| 69 | |
| 70 | /* |
| 71 | * We expect something like: |
Heiko Stuebner | 42aba05 | 2019-08-05 14:46:00 +0200 | [diff] [blame] | 72 | * "serial0:baudrate" |
Christoph Müllner | cb9204a | 2019-04-19 14:16:27 +0200 | [diff] [blame] | 73 | */ |
| 74 | if (strncmp("serial", stdout_path, 6) != 0) |
| 75 | return; |
| 76 | |
| 77 | serial_char = stdout_path[6]; |
| 78 | serial_no = serial_char - '0'; |
| 79 | |
| 80 | switch (serial_no) { |
| 81 | case 0: |
| 82 | uart_base = UART0_BASE; |
| 83 | break; |
| 84 | case 1: |
| 85 | uart_base = UART1_BASE; |
| 86 | break; |
| 87 | case 2: |
| 88 | uart_base = UART2_BASE; |
| 89 | break; |
| 90 | #ifdef UART3_BASE |
| 91 | case 3: |
| 92 | uart_base = UART3_BASE; |
| 93 | break; |
| 94 | #endif |
| 95 | #ifdef UART4_BASE |
| 96 | case 4: |
| 97 | uart_base = UART4_BASE; |
| 98 | break; |
| 99 | #endif |
Heiko Stuebner | 64a4a7a | 2019-08-05 09:45:09 +0200 | [diff] [blame] | 100 | #ifdef UART5_BASE |
| 101 | case 5: |
| 102 | uart_base = UART5_BASE; |
| 103 | break; |
| 104 | #endif |
Christoph Müllner | cb9204a | 2019-04-19 14:16:27 +0200 | [diff] [blame] | 105 | default: |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | rk_uart_base = uart_base; |
Heiko Stuebner | 42aba05 | 2019-08-05 14:46:00 +0200 | [diff] [blame] | 110 | |
| 111 | separator = strchr(stdout_path, ':'); |
| 112 | if (!separator) |
| 113 | return; |
| 114 | |
| 115 | baud = 0; |
| 116 | baud_start = separator + 1; |
| 117 | while (*baud_start != '\0') { |
| 118 | /* |
| 119 | * uart binding is <baud>{<parity>{<bits>{...}}} |
| 120 | * So the baudrate either is the whole string, or |
| 121 | * we end in the parity characters. |
| 122 | */ |
| 123 | if (*baud_start == 'n' || *baud_start == 'o' || |
| 124 | *baud_start == 'e') |
| 125 | break; |
| 126 | |
| 127 | baud = baud * 10 + (*baud_start - '0'); |
| 128 | baud_start++; |
| 129 | } |
| 130 | |
| 131 | rk_uart_baudrate = baud; |
Christoph Müllner | cb9204a | 2019-04-19 14:16:27 +0200 | [diff] [blame] | 132 | } |
| 133 | |
Julius Werner | 65d5267 | 2019-05-24 20:37:58 -0700 | [diff] [blame] | 134 | static int dt_process_fdt(u_register_t param_from_bl2) |
Heiko Stuebner | 6fd5b94 | 2019-04-24 20:26:51 +0200 | [diff] [blame] | 135 | { |
| 136 | void *fdt = plat_get_fdt(); |
| 137 | int ret; |
| 138 | |
Julius Werner | 65d5267 | 2019-05-24 20:37:58 -0700 | [diff] [blame] | 139 | ret = fdt_open_into((void *)param_from_bl2, fdt, 0x10000); |
Heiko Stuebner | 6fd5b94 | 2019-04-24 20:26:51 +0200 | [diff] [blame] | 140 | if (ret < 0) |
| 141 | return ret; |
| 142 | |
Christoph Müllner | cb9204a | 2019-04-19 14:16:27 +0200 | [diff] [blame] | 143 | plat_rockchip_dt_process_fdt_uart(fdt); |
| 144 | |
Heiko Stuebner | 6fd5b94 | 2019-04-24 20:26:51 +0200 | [diff] [blame] | 145 | return 0; |
| 146 | } |
Heiko Stuebner | 40b3cb1 | 2019-08-05 16:40:35 +0200 | [diff] [blame] | 147 | #endif |
| 148 | |
| 149 | uint32_t rockchip_get_uart_base(void) |
| 150 | { |
| 151 | #if COREBOOT |
| 152 | return coreboot_serial.baseaddr; |
| 153 | #else |
| 154 | return rk_uart_base; |
| 155 | #endif |
| 156 | } |
| 157 | |
| 158 | uint32_t rockchip_get_uart_baudrate(void) |
| 159 | { |
| 160 | #if COREBOOT |
| 161 | return coreboot_serial.baud; |
| 162 | #else |
| 163 | return rk_uart_baudrate; |
| 164 | #endif |
| 165 | } |
| 166 | |
| 167 | uint32_t rockchip_get_uart_clock(void) |
| 168 | { |
| 169 | #if COREBOOT |
| 170 | return coreboot_serial.input_hertz; |
| 171 | #else |
| 172 | return rk_uart_clock; |
Heiko Stuebner | 6fd5b94 | 2019-04-24 20:26:51 +0200 | [diff] [blame] | 173 | #endif |
Heiko Stuebner | 40b3cb1 | 2019-08-05 16:40:35 +0200 | [diff] [blame] | 174 | } |
Heiko Stuebner | 6fd5b94 | 2019-04-24 20:26:51 +0200 | [diff] [blame] | 175 | |
Julius Werner | 65d5267 | 2019-05-24 20:37:58 -0700 | [diff] [blame] | 176 | struct bl_aux_gpio_info *plat_get_rockchip_gpio_reset(void) |
Caesar Wang | 3e3c5b0 | 2016-05-25 19:03:04 +0800 | [diff] [blame] | 177 | { |
Vasily Khoruzhick | 75afb1d | 2019-11-15 08:25:02 -0800 | [diff] [blame] | 178 | if (rst_gpio.index == UINT_MAX) |
| 179 | return NULL; |
| 180 | |
Julius Werner | 65d5267 | 2019-05-24 20:37:58 -0700 | [diff] [blame] | 181 | return &rst_gpio; |
Caesar Wang | 3e3c5b0 | 2016-05-25 19:03:04 +0800 | [diff] [blame] | 182 | } |
| 183 | |
Julius Werner | 65d5267 | 2019-05-24 20:37:58 -0700 | [diff] [blame] | 184 | struct bl_aux_gpio_info *plat_get_rockchip_gpio_poweroff(void) |
Caesar Wang | 3e3c5b0 | 2016-05-25 19:03:04 +0800 | [diff] [blame] | 185 | { |
Vasily Khoruzhick | 75afb1d | 2019-11-15 08:25:02 -0800 | [diff] [blame] | 186 | if (poweroff_gpio.index == UINT_MAX) |
| 187 | return NULL; |
| 188 | |
Julius Werner | 65d5267 | 2019-05-24 20:37:58 -0700 | [diff] [blame] | 189 | return &poweroff_gpio; |
Caesar Wang | 3e3c5b0 | 2016-05-25 19:03:04 +0800 | [diff] [blame] | 190 | } |
| 191 | |
Julius Werner | 65d5267 | 2019-05-24 20:37:58 -0700 | [diff] [blame] | 192 | struct bl_aux_gpio_info *plat_get_rockchip_suspend_gpio(uint32_t *count) |
Caesar Wang | ef18007 | 2016-09-10 02:43:15 +0800 | [diff] [blame] | 193 | { |
| 194 | *count = suspend_gpio_cnt; |
| 195 | |
| 196 | return &suspend_gpio[0]; |
| 197 | } |
| 198 | |
Julius Werner | 65d5267 | 2019-05-24 20:37:58 -0700 | [diff] [blame] | 199 | struct bl_aux_rk_apio_info *plat_get_rockchip_suspend_apio(void) |
Caesar Wang | 5045a1c | 2016-09-10 02:47:53 +0800 | [diff] [blame] | 200 | { |
Julius Werner | 65d5267 | 2019-05-24 20:37:58 -0700 | [diff] [blame] | 201 | return &suspend_apio; |
Caesar Wang | 5045a1c | 2016-09-10 02:47:53 +0800 | [diff] [blame] | 202 | } |
| 203 | |
Julius Werner | 65d5267 | 2019-05-24 20:37:58 -0700 | [diff] [blame] | 204 | static bool rk_aux_param_handler(struct bl_aux_param_header *param) |
Caesar Wang | 3e3c5b0 | 2016-05-25 19:03:04 +0800 | [diff] [blame] | 205 | { |
Julius Werner | 65d5267 | 2019-05-24 20:37:58 -0700 | [diff] [blame] | 206 | /* Store platform parameters for later processing if needed. */ |
| 207 | switch (param->type) { |
| 208 | case BL_AUX_PARAM_RK_RESET_GPIO: |
| 209 | rst_gpio = ((struct bl_aux_param_gpio *)param)->gpio; |
| 210 | return true; |
| 211 | case BL_AUX_PARAM_RK_POWEROFF_GPIO: |
| 212 | poweroff_gpio = ((struct bl_aux_param_gpio *)param)->gpio; |
| 213 | return true; |
| 214 | case BL_AUX_PARAM_RK_SUSPEND_GPIO: |
| 215 | if (suspend_gpio_cnt >= ARRAY_SIZE(suspend_gpio)) { |
| 216 | ERROR("Exceeded the supported suspend GPIO number.\n"); |
| 217 | return true; |
| 218 | } |
| 219 | suspend_gpio[suspend_gpio_cnt++] = |
| 220 | ((struct bl_aux_param_gpio *)param)->gpio; |
| 221 | return true; |
| 222 | case BL_AUX_PARAM_RK_SUSPEND_APIO: |
| 223 | suspend_apio = ((struct bl_aux_param_rk_apio *)param)->apio; |
| 224 | return true; |
| 225 | } |
Caesar Wang | 3e3c5b0 | 2016-05-25 19:03:04 +0800 | [diff] [blame] | 226 | |
Julius Werner | 65d5267 | 2019-05-24 20:37:58 -0700 | [diff] [blame] | 227 | return false; |
| 228 | } |
| 229 | |
| 230 | void params_early_setup(u_register_t plat_param_from_bl2) |
| 231 | { |
Heiko Stuebner | bbd0f5a | 2019-03-07 08:07:11 +0100 | [diff] [blame] | 232 | /* |
| 233 | * Test if this is a FDT passed as a platform-specific parameter |
| 234 | * block. |
| 235 | */ |
| 236 | if (!dt_process_fdt(plat_param_from_bl2)) |
| 237 | return; |
| 238 | |
Julius Werner | 65d5267 | 2019-05-24 20:37:58 -0700 | [diff] [blame] | 239 | bl_aux_params_parse(plat_param_from_bl2, rk_aux_param_handler); |
Caesar Wang | 3e3c5b0 | 2016-05-25 19:03:04 +0800 | [diff] [blame] | 240 | } |