blob: 9e8ef40cecab0dab82be84524b359193ccc3ec7f [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>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00009#include <string.h>
10
Julius Werner65d52672019-05-24 20:37:58 -070011#include <lib/bl_aux_params/bl_aux_params.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000012#include <common/bl_common.h>
13#include <common/debug.h>
14#include <drivers/console.h>
15#include <drivers/gpio.h>
Heiko Stuebnerbbd0f5a2019-03-07 08:07:11 +010016#include <libfdt.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000017#include <lib/coreboot.h>
18#include <lib/mmio.h>
19#include <plat/common/platform.h>
20
Caesar Wang3e3c5b02016-05-25 19:03:04 +080021#include <plat_params.h>
22#include <plat_private.h>
Caesar Wang3e3c5b02016-05-25 19:03:04 +080023
Julius Werner65d52672019-05-24 20:37:58 -070024static struct bl_aux_gpio_info rst_gpio;
25static struct bl_aux_gpio_info poweroff_gpio;
26static struct bl_aux_gpio_info suspend_gpio[10];
Caesar Wangef180072016-09-10 02:43:15 +080027uint32_t suspend_gpio_cnt;
Julius Werner65d52672019-05-24 20:37:58 -070028static struct bl_aux_rk_apio_info suspend_apio;
Christoph Müllnercb9204a2019-04-19 14:16:27 +020029static uint32_t rk_uart_base = PLAT_RK_UART_BASE;
Heiko Stuebner42aba052019-08-05 14:46:00 +020030static uint32_t rk_uart_baudrate = PLAT_RK_UART_BAUDRATE;
Christoph Müllnercb9204a2019-04-19 14:16:27 +020031
32uint32_t rockchip_get_uart_base(void)
33{
34 return rk_uart_base;
35}
Caesar Wang3e3c5b02016-05-25 19:03:04 +080036
Heiko Stuebner42aba052019-08-05 14:46:00 +020037uint32_t rockchip_get_uart_baudrate(void)
38{
39 return rk_uart_baudrate;
40}
41
Heiko Stuebner6fd5b942019-04-24 20:26:51 +020042#if COREBOOT
Julius Werner65d52672019-05-24 20:37:58 -070043static int dt_process_fdt(u_register_t param_from_bl2)
Heiko Stuebner6fd5b942019-04-24 20:26:51 +020044{
45 return -ENODEV;
46}
47#else
Heiko Stuebnerbbd0f5a2019-03-07 08:07:11 +010048static uint8_t fdt_buffer[0x10000];
49
50void *plat_get_fdt(void)
51{
52 return &fdt_buffer[0];
53}
54
Christoph Müllnercb9204a2019-04-19 14:16:27 +020055static void plat_rockchip_dt_process_fdt_uart(void *fdt)
56{
57 const char *path_name = "/chosen";
58 const char *prop_name = "stdout-path";
59 int node_offset;
60 int stdout_path_len;
61 const char *stdout_path;
Heiko Stuebner42aba052019-08-05 14:46:00 +020062 const char *separator;
63 const char *baud_start;
Christoph Müllnercb9204a2019-04-19 14:16:27 +020064 char serial_char;
65 int serial_no;
66 uint32_t uart_base;
Heiko Stuebner42aba052019-08-05 14:46:00 +020067 uint32_t baud;
Christoph Müllnercb9204a2019-04-19 14:16:27 +020068
69 node_offset = fdt_path_offset(fdt, path_name);
70 if (node_offset < 0)
71 return;
72
73 stdout_path = fdt_getprop(fdt, node_offset, prop_name,
74 &stdout_path_len);
75 if (stdout_path == NULL)
76 return;
77
78 /*
79 * We expect something like:
Heiko Stuebner42aba052019-08-05 14:46:00 +020080 * "serial0:baudrate"
Christoph Müllnercb9204a2019-04-19 14:16:27 +020081 */
82 if (strncmp("serial", stdout_path, 6) != 0)
83 return;
84
85 serial_char = stdout_path[6];
86 serial_no = serial_char - '0';
87
88 switch (serial_no) {
89 case 0:
90 uart_base = UART0_BASE;
91 break;
92 case 1:
93 uart_base = UART1_BASE;
94 break;
95 case 2:
96 uart_base = UART2_BASE;
97 break;
98#ifdef UART3_BASE
99 case 3:
100 uart_base = UART3_BASE;
101 break;
102#endif
103#ifdef UART4_BASE
104 case 4:
105 uart_base = UART4_BASE;
106 break;
107#endif
Heiko Stuebner64a4a7a2019-08-05 09:45:09 +0200108#ifdef UART5_BASE
109 case 5:
110 uart_base = UART5_BASE;
111 break;
112#endif
Christoph Müllnercb9204a2019-04-19 14:16:27 +0200113 default:
114 return;
115 }
116
117 rk_uart_base = uart_base;
Heiko Stuebner42aba052019-08-05 14:46:00 +0200118
119 separator = strchr(stdout_path, ':');
120 if (!separator)
121 return;
122
123 baud = 0;
124 baud_start = separator + 1;
125 while (*baud_start != '\0') {
126 /*
127 * uart binding is <baud>{<parity>{<bits>{...}}}
128 * So the baudrate either is the whole string, or
129 * we end in the parity characters.
130 */
131 if (*baud_start == 'n' || *baud_start == 'o' ||
132 *baud_start == 'e')
133 break;
134
135 baud = baud * 10 + (*baud_start - '0');
136 baud_start++;
137 }
138
139 rk_uart_baudrate = baud;
Christoph Müllnercb9204a2019-04-19 14:16:27 +0200140}
141
Julius Werner65d52672019-05-24 20:37:58 -0700142static int dt_process_fdt(u_register_t param_from_bl2)
Heiko Stuebner6fd5b942019-04-24 20:26:51 +0200143{
144 void *fdt = plat_get_fdt();
145 int ret;
146
Julius Werner65d52672019-05-24 20:37:58 -0700147 ret = fdt_open_into((void *)param_from_bl2, fdt, 0x10000);
Heiko Stuebner6fd5b942019-04-24 20:26:51 +0200148 if (ret < 0)
149 return ret;
150
Christoph Müllnercb9204a2019-04-19 14:16:27 +0200151 plat_rockchip_dt_process_fdt_uart(fdt);
152
Heiko Stuebner6fd5b942019-04-24 20:26:51 +0200153 return 0;
154}
155#endif
156
Julius Werner65d52672019-05-24 20:37:58 -0700157struct bl_aux_gpio_info *plat_get_rockchip_gpio_reset(void)
Caesar Wang3e3c5b02016-05-25 19:03:04 +0800158{
Julius Werner65d52672019-05-24 20:37:58 -0700159 return &rst_gpio;
Caesar Wang3e3c5b02016-05-25 19:03:04 +0800160}
161
Julius Werner65d52672019-05-24 20:37:58 -0700162struct bl_aux_gpio_info *plat_get_rockchip_gpio_poweroff(void)
Caesar Wang3e3c5b02016-05-25 19:03:04 +0800163{
Julius Werner65d52672019-05-24 20:37:58 -0700164 return &poweroff_gpio;
Caesar Wang3e3c5b02016-05-25 19:03:04 +0800165}
166
Julius Werner65d52672019-05-24 20:37:58 -0700167struct bl_aux_gpio_info *plat_get_rockchip_suspend_gpio(uint32_t *count)
Caesar Wangef180072016-09-10 02:43:15 +0800168{
169 *count = suspend_gpio_cnt;
170
171 return &suspend_gpio[0];
172}
173
Julius Werner65d52672019-05-24 20:37:58 -0700174struct bl_aux_rk_apio_info *plat_get_rockchip_suspend_apio(void)
Caesar Wang5045a1c2016-09-10 02:47:53 +0800175{
Julius Werner65d52672019-05-24 20:37:58 -0700176 return &suspend_apio;
Caesar Wang5045a1c2016-09-10 02:47:53 +0800177}
178
Julius Werner65d52672019-05-24 20:37:58 -0700179static bool rk_aux_param_handler(struct bl_aux_param_header *param)
Caesar Wang3e3c5b02016-05-25 19:03:04 +0800180{
Julius Werner65d52672019-05-24 20:37:58 -0700181 /* Store platform parameters for later processing if needed. */
182 switch (param->type) {
183 case BL_AUX_PARAM_RK_RESET_GPIO:
184 rst_gpio = ((struct bl_aux_param_gpio *)param)->gpio;
185 return true;
186 case BL_AUX_PARAM_RK_POWEROFF_GPIO:
187 poweroff_gpio = ((struct bl_aux_param_gpio *)param)->gpio;
188 return true;
189 case BL_AUX_PARAM_RK_SUSPEND_GPIO:
190 if (suspend_gpio_cnt >= ARRAY_SIZE(suspend_gpio)) {
191 ERROR("Exceeded the supported suspend GPIO number.\n");
192 return true;
193 }
194 suspend_gpio[suspend_gpio_cnt++] =
195 ((struct bl_aux_param_gpio *)param)->gpio;
196 return true;
197 case BL_AUX_PARAM_RK_SUSPEND_APIO:
198 suspend_apio = ((struct bl_aux_param_rk_apio *)param)->apio;
199 return true;
200 }
Caesar Wang3e3c5b02016-05-25 19:03:04 +0800201
Julius Werner65d52672019-05-24 20:37:58 -0700202 return false;
203}
204
205void params_early_setup(u_register_t plat_param_from_bl2)
206{
Heiko Stuebnerbbd0f5a2019-03-07 08:07:11 +0100207 /*
208 * Test if this is a FDT passed as a platform-specific parameter
209 * block.
210 */
211 if (!dt_process_fdt(plat_param_from_bl2))
212 return;
213
Julius Werner65d52672019-05-24 20:37:58 -0700214 bl_aux_params_parse(plat_param_from_bl2, rk_aux_param_handler);
Caesar Wang3e3c5b02016-05-25 19:03:04 +0800215}