blob: aec53eee2e0d45694318aa1fb98ef2212a9251a7 [file] [log] [blame]
Caesar Wang3e3c5b02016-05-25 19:03:04 +08001/*
Julius Werner65d52672019-05-24 20:37:58 -07002 * Copyright (c) 2016-2019, ARM Limited and Contributors. All rights reserved.
Caesar Wang3e3c5b02016-05-25 19:03:04 +08003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Caesar Wang3e3c5b02016-05-25 19:03:04 +08005 */
6
Caesar Wang3e3c5b02016-05-25 19:03:04 +08007#include <assert.h>
Heiko Stuebner6fd5b942019-04-24 20:26:51 +02008#include <errno.h>
Vasily Khoruzhick75afb1d2019-11-15 08:25:02 -08009#include <limits.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000010#include <string.h>
11
Julius Werner65d52672019-05-24 20:37:58 -070012#include <lib/bl_aux_params/bl_aux_params.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000013#include <common/bl_common.h>
14#include <common/debug.h>
15#include <drivers/console.h>
16#include <drivers/gpio.h>
Heiko Stuebnerbbd0f5a2019-03-07 08:07:11 +010017#include <libfdt.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000018#include <lib/coreboot.h>
19#include <lib/mmio.h>
20#include <plat/common/platform.h>
21
Caesar Wang3e3c5b02016-05-25 19:03:04 +080022#include <plat_params.h>
23#include <plat_private.h>
Caesar Wang3e3c5b02016-05-25 19:03:04 +080024
Vasily Khoruzhick75afb1d2019-11-15 08:25:02 -080025static struct bl_aux_gpio_info rst_gpio = { .index = UINT_MAX } ;
26static struct bl_aux_gpio_info poweroff_gpio = { .index = UINT_MAX };
Julius Werner65d52672019-05-24 20:37:58 -070027static struct bl_aux_gpio_info suspend_gpio[10];
Caesar Wangef180072016-09-10 02:43:15 +080028uint32_t suspend_gpio_cnt;
Julius Werner65d52672019-05-24 20:37:58 -070029static struct bl_aux_rk_apio_info suspend_apio;
Heiko Stuebner42aba052019-08-05 14:46:00 +020030
Heiko Stuebner6fd5b942019-04-24 20:26:51 +020031#if COREBOOT
Julius Werner65d52672019-05-24 20:37:58 -070032static int dt_process_fdt(u_register_t param_from_bl2)
Heiko Stuebner6fd5b942019-04-24 20:26:51 +020033{
34 return -ENODEV;
35}
36#else
Heiko Stuebner40b3cb12019-08-05 16:40:35 +020037static uint32_t rk_uart_base = PLAT_RK_UART_BASE;
38static uint32_t rk_uart_baudrate = PLAT_RK_UART_BAUDRATE;
39static uint32_t rk_uart_clock = PLAT_RK_UART_CLOCK;
Hugh Cole-Bakeree96cda2020-06-08 22:24:36 +010040#define FDT_BUFFER_SIZE 0x20000
41static uint8_t fdt_buffer[FDT_BUFFER_SIZE];
Heiko Stuebnerbbd0f5a2019-03-07 08:07:11 +010042
43void *plat_get_fdt(void)
44{
45 return &fdt_buffer[0];
46}
47
Christoph Müllnercb9204a2019-04-19 14:16:27 +020048static void plat_rockchip_dt_process_fdt_uart(void *fdt)
49{
50 const char *path_name = "/chosen";
51 const char *prop_name = "stdout-path";
52 int node_offset;
53 int stdout_path_len;
54 const char *stdout_path;
Heiko Stuebner42aba052019-08-05 14:46:00 +020055 const char *separator;
56 const char *baud_start;
Christoph Müllnercb9204a2019-04-19 14:16:27 +020057 char serial_char;
58 int serial_no;
59 uint32_t uart_base;
Heiko Stuebner42aba052019-08-05 14:46:00 +020060 uint32_t baud;
Christoph Müllnercb9204a2019-04-19 14:16:27 +020061
62 node_offset = fdt_path_offset(fdt, path_name);
63 if (node_offset < 0)
64 return;
65
66 stdout_path = fdt_getprop(fdt, node_offset, prop_name,
67 &stdout_path_len);
68 if (stdout_path == NULL)
69 return;
70
71 /*
72 * We expect something like:
Heiko Stuebner42aba052019-08-05 14:46:00 +020073 * "serial0:baudrate"
Christoph Müllnercb9204a2019-04-19 14:16:27 +020074 */
75 if (strncmp("serial", stdout_path, 6) != 0)
76 return;
77
78 serial_char = stdout_path[6];
79 serial_no = serial_char - '0';
80
81 switch (serial_no) {
82 case 0:
83 uart_base = UART0_BASE;
84 break;
85 case 1:
86 uart_base = UART1_BASE;
87 break;
88 case 2:
89 uart_base = UART2_BASE;
90 break;
91#ifdef UART3_BASE
92 case 3:
93 uart_base = UART3_BASE;
94 break;
95#endif
96#ifdef UART4_BASE
97 case 4:
98 uart_base = UART4_BASE;
99 break;
100#endif
Heiko Stuebner64a4a7a2019-08-05 09:45:09 +0200101#ifdef UART5_BASE
102 case 5:
103 uart_base = UART5_BASE;
104 break;
105#endif
Christoph Müllnercb9204a2019-04-19 14:16:27 +0200106 default:
107 return;
108 }
109
110 rk_uart_base = uart_base;
Heiko Stuebner42aba052019-08-05 14:46:00 +0200111
112 separator = strchr(stdout_path, ':');
113 if (!separator)
114 return;
115
116 baud = 0;
117 baud_start = separator + 1;
118 while (*baud_start != '\0') {
119 /*
120 * uart binding is <baud>{<parity>{<bits>{...}}}
121 * So the baudrate either is the whole string, or
122 * we end in the parity characters.
123 */
124 if (*baud_start == 'n' || *baud_start == 'o' ||
125 *baud_start == 'e')
126 break;
127
128 baud = baud * 10 + (*baud_start - '0');
129 baud_start++;
130 }
131
132 rk_uart_baudrate = baud;
Christoph Müllnercb9204a2019-04-19 14:16:27 +0200133}
134
Julius Werner65d52672019-05-24 20:37:58 -0700135static int dt_process_fdt(u_register_t param_from_bl2)
Heiko Stuebner6fd5b942019-04-24 20:26:51 +0200136{
137 void *fdt = plat_get_fdt();
138 int ret;
139
Hugh Cole-Bakeree96cda2020-06-08 22:24:36 +0100140 ret = fdt_open_into((void *)param_from_bl2, fdt, FDT_BUFFER_SIZE);
Heiko Stuebner6fd5b942019-04-24 20:26:51 +0200141 if (ret < 0)
142 return ret;
143
Christoph Müllnercb9204a2019-04-19 14:16:27 +0200144 plat_rockchip_dt_process_fdt_uart(fdt);
145
Heiko Stuebner6fd5b942019-04-24 20:26:51 +0200146 return 0;
147}
Heiko Stuebner40b3cb12019-08-05 16:40:35 +0200148#endif
149
150uint32_t rockchip_get_uart_base(void)
151{
152#if COREBOOT
153 return coreboot_serial.baseaddr;
154#else
155 return rk_uart_base;
156#endif
157}
158
159uint32_t rockchip_get_uart_baudrate(void)
160{
161#if COREBOOT
162 return coreboot_serial.baud;
163#else
164 return rk_uart_baudrate;
165#endif
166}
167
168uint32_t rockchip_get_uart_clock(void)
169{
170#if COREBOOT
171 return coreboot_serial.input_hertz;
172#else
173 return rk_uart_clock;
Heiko Stuebner6fd5b942019-04-24 20:26:51 +0200174#endif
Heiko Stuebner40b3cb12019-08-05 16:40:35 +0200175}
Heiko Stuebner6fd5b942019-04-24 20:26:51 +0200176
Julius Werner65d52672019-05-24 20:37:58 -0700177struct bl_aux_gpio_info *plat_get_rockchip_gpio_reset(void)
Caesar Wang3e3c5b02016-05-25 19:03:04 +0800178{
Vasily Khoruzhick75afb1d2019-11-15 08:25:02 -0800179 if (rst_gpio.index == UINT_MAX)
180 return NULL;
181
Julius Werner65d52672019-05-24 20:37:58 -0700182 return &rst_gpio;
Caesar Wang3e3c5b02016-05-25 19:03:04 +0800183}
184
Julius Werner65d52672019-05-24 20:37:58 -0700185struct bl_aux_gpio_info *plat_get_rockchip_gpio_poweroff(void)
Caesar Wang3e3c5b02016-05-25 19:03:04 +0800186{
Vasily Khoruzhick75afb1d2019-11-15 08:25:02 -0800187 if (poweroff_gpio.index == UINT_MAX)
188 return NULL;
189
Julius Werner65d52672019-05-24 20:37:58 -0700190 return &poweroff_gpio;
Caesar Wang3e3c5b02016-05-25 19:03:04 +0800191}
192
Julius Werner65d52672019-05-24 20:37:58 -0700193struct bl_aux_gpio_info *plat_get_rockchip_suspend_gpio(uint32_t *count)
Caesar Wangef180072016-09-10 02:43:15 +0800194{
195 *count = suspend_gpio_cnt;
196
197 return &suspend_gpio[0];
198}
199
Julius Werner65d52672019-05-24 20:37:58 -0700200struct bl_aux_rk_apio_info *plat_get_rockchip_suspend_apio(void)
Caesar Wang5045a1c2016-09-10 02:47:53 +0800201{
Julius Werner65d52672019-05-24 20:37:58 -0700202 return &suspend_apio;
Caesar Wang5045a1c2016-09-10 02:47:53 +0800203}
204
Julius Werner65d52672019-05-24 20:37:58 -0700205static bool rk_aux_param_handler(struct bl_aux_param_header *param)
Caesar Wang3e3c5b02016-05-25 19:03:04 +0800206{
Julius Werner65d52672019-05-24 20:37:58 -0700207 /* Store platform parameters for later processing if needed. */
208 switch (param->type) {
209 case BL_AUX_PARAM_RK_RESET_GPIO:
210 rst_gpio = ((struct bl_aux_param_gpio *)param)->gpio;
211 return true;
212 case BL_AUX_PARAM_RK_POWEROFF_GPIO:
213 poweroff_gpio = ((struct bl_aux_param_gpio *)param)->gpio;
214 return true;
215 case BL_AUX_PARAM_RK_SUSPEND_GPIO:
216 if (suspend_gpio_cnt >= ARRAY_SIZE(suspend_gpio)) {
217 ERROR("Exceeded the supported suspend GPIO number.\n");
218 return true;
219 }
220 suspend_gpio[suspend_gpio_cnt++] =
221 ((struct bl_aux_param_gpio *)param)->gpio;
222 return true;
223 case BL_AUX_PARAM_RK_SUSPEND_APIO:
224 suspend_apio = ((struct bl_aux_param_rk_apio *)param)->apio;
225 return true;
226 }
Caesar Wang3e3c5b02016-05-25 19:03:04 +0800227
Julius Werner65d52672019-05-24 20:37:58 -0700228 return false;
229}
230
231void params_early_setup(u_register_t plat_param_from_bl2)
232{
Thomas Hebbc3b169c2020-04-05 02:33:37 -0400233 int ret;
234
Heiko Stuebnerbbd0f5a2019-03-07 08:07:11 +0100235 /*
236 * Test if this is a FDT passed as a platform-specific parameter
237 * block.
238 */
Thomas Hebbc3b169c2020-04-05 02:33:37 -0400239 ret = dt_process_fdt(plat_param_from_bl2);
240 if (!ret) {
241 return;
242 } else if (ret != -FDT_ERR_BADMAGIC) {
243 /*
244 * If we found an FDT but couldn't parse it (e.g. corrupt, not
245 * enough space), return and don't attempt to parse the param
246 * as something else, since we know that will also fail. All
247 * we're doing is setting up UART, this doesn't need to be
248 * fatal.
249 */
250 WARN("%s: found FDT but could not parse: error %d\n",
251 __func__, ret);
Heiko Stuebnerbbd0f5a2019-03-07 08:07:11 +0100252 return;
Thomas Hebbc3b169c2020-04-05 02:33:37 -0400253 }
Heiko Stuebnerbbd0f5a2019-03-07 08:07:11 +0100254
Julius Werner65d52672019-05-24 20:37:58 -0700255 bl_aux_params_parse(plat_param_from_bl2, rk_aux_param_handler);
Caesar Wang3e3c5b02016-05-25 19:03:04 +0800256}