blob: b2fd2011e4d360324d53acdecf79069b45419ce9 [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;
Heiko Stuebnerbbd0f5a2019-03-07 08:07:11 +010040static uint8_t fdt_buffer[0x10000];
41
42void *plat_get_fdt(void)
43{
44 return &fdt_buffer[0];
45}
46
Christoph Müllnercb9204a2019-04-19 14:16:27 +020047static 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 Stuebner42aba052019-08-05 14:46:00 +020054 const char *separator;
55 const char *baud_start;
Christoph Müllnercb9204a2019-04-19 14:16:27 +020056 char serial_char;
57 int serial_no;
58 uint32_t uart_base;
Heiko Stuebner42aba052019-08-05 14:46:00 +020059 uint32_t baud;
Christoph Müllnercb9204a2019-04-19 14:16:27 +020060
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 Stuebner42aba052019-08-05 14:46:00 +020072 * "serial0:baudrate"
Christoph Müllnercb9204a2019-04-19 14:16:27 +020073 */
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 Stuebner64a4a7a2019-08-05 09:45:09 +0200100#ifdef UART5_BASE
101 case 5:
102 uart_base = UART5_BASE;
103 break;
104#endif
Christoph Müllnercb9204a2019-04-19 14:16:27 +0200105 default:
106 return;
107 }
108
109 rk_uart_base = uart_base;
Heiko Stuebner42aba052019-08-05 14:46:00 +0200110
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üllnercb9204a2019-04-19 14:16:27 +0200132}
133
Julius Werner65d52672019-05-24 20:37:58 -0700134static int dt_process_fdt(u_register_t param_from_bl2)
Heiko Stuebner6fd5b942019-04-24 20:26:51 +0200135{
136 void *fdt = plat_get_fdt();
137 int ret;
138
Julius Werner65d52672019-05-24 20:37:58 -0700139 ret = fdt_open_into((void *)param_from_bl2, fdt, 0x10000);
Heiko Stuebner6fd5b942019-04-24 20:26:51 +0200140 if (ret < 0)
141 return ret;
142
Christoph Müllnercb9204a2019-04-19 14:16:27 +0200143 plat_rockchip_dt_process_fdt_uart(fdt);
144
Heiko Stuebner6fd5b942019-04-24 20:26:51 +0200145 return 0;
146}
Heiko Stuebner40b3cb12019-08-05 16:40:35 +0200147#endif
148
149uint32_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
158uint32_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
167uint32_t rockchip_get_uart_clock(void)
168{
169#if COREBOOT
170 return coreboot_serial.input_hertz;
171#else
172 return rk_uart_clock;
Heiko Stuebner6fd5b942019-04-24 20:26:51 +0200173#endif
Heiko Stuebner40b3cb12019-08-05 16:40:35 +0200174}
Heiko Stuebner6fd5b942019-04-24 20:26:51 +0200175
Julius Werner65d52672019-05-24 20:37:58 -0700176struct bl_aux_gpio_info *plat_get_rockchip_gpio_reset(void)
Caesar Wang3e3c5b02016-05-25 19:03:04 +0800177{
Vasily Khoruzhick75afb1d2019-11-15 08:25:02 -0800178 if (rst_gpio.index == UINT_MAX)
179 return NULL;
180
Julius Werner65d52672019-05-24 20:37:58 -0700181 return &rst_gpio;
Caesar Wang3e3c5b02016-05-25 19:03:04 +0800182}
183
Julius Werner65d52672019-05-24 20:37:58 -0700184struct bl_aux_gpio_info *plat_get_rockchip_gpio_poweroff(void)
Caesar Wang3e3c5b02016-05-25 19:03:04 +0800185{
Vasily Khoruzhick75afb1d2019-11-15 08:25:02 -0800186 if (poweroff_gpio.index == UINT_MAX)
187 return NULL;
188
Julius Werner65d52672019-05-24 20:37:58 -0700189 return &poweroff_gpio;
Caesar Wang3e3c5b02016-05-25 19:03:04 +0800190}
191
Julius Werner65d52672019-05-24 20:37:58 -0700192struct bl_aux_gpio_info *plat_get_rockchip_suspend_gpio(uint32_t *count)
Caesar Wangef180072016-09-10 02:43:15 +0800193{
194 *count = suspend_gpio_cnt;
195
196 return &suspend_gpio[0];
197}
198
Julius Werner65d52672019-05-24 20:37:58 -0700199struct bl_aux_rk_apio_info *plat_get_rockchip_suspend_apio(void)
Caesar Wang5045a1c2016-09-10 02:47:53 +0800200{
Julius Werner65d52672019-05-24 20:37:58 -0700201 return &suspend_apio;
Caesar Wang5045a1c2016-09-10 02:47:53 +0800202}
203
Julius Werner65d52672019-05-24 20:37:58 -0700204static bool rk_aux_param_handler(struct bl_aux_param_header *param)
Caesar Wang3e3c5b02016-05-25 19:03:04 +0800205{
Julius Werner65d52672019-05-24 20:37:58 -0700206 /* 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 Wang3e3c5b02016-05-25 19:03:04 +0800226
Julius Werner65d52672019-05-24 20:37:58 -0700227 return false;
228}
229
230void params_early_setup(u_register_t plat_param_from_bl2)
231{
Heiko Stuebnerbbd0f5a2019-03-07 08:07:11 +0100232 /*
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 Werner65d52672019-05-24 20:37:58 -0700239 bl_aux_params_parse(plat_param_from_bl2, rk_aux_param_handler);
Caesar Wang3e3c5b02016-05-25 19:03:04 +0800240}