blob: 1c53ccabacf65c8801e08f4308d86d2fe61d8275 [file] [log] [blame]
Simon Glass2cffe662015-08-30 16:55:38 -06001/*
2 * (C) Copyright 2015 Google, Inc
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include <common.h>
Simon Glass38675c42016-01-21 19:45:07 -07008#include <clk.h>
Simon Glass2cffe662015-08-30 16:55:38 -06009#include <dm.h>
10#include <ram.h>
Jacob Chenc95f3782016-09-19 18:46:28 +080011#include <syscon.h>
huang lin1115b642015-11-17 14:20:27 +080012#include <asm/io.h>
Stephen Warrena9622432016-06-17 09:44:00 -060013#include <asm/arch/clock.h>
Wadim Egorove9eb66c2017-08-21 13:36:57 +020014#include <asm/arch/cru_rk3288.h>
Xu Ziyuan5401eb82016-07-12 19:09:49 +080015#include <asm/arch/periph.h>
Jacob Chenc95f3782016-09-19 18:46:28 +080016#include <asm/arch/pmu_rk3288.h>
Nickey Yang Nickey Yang6766d142016-12-29 10:47:30 +080017#include <asm/arch/qos_rk3288.h>
Jacob Chenc95f3782016-09-19 18:46:28 +080018#include <asm/arch/boot_mode.h>
Xu Ziyuan5401eb82016-07-12 19:09:49 +080019#include <asm/gpio.h>
20#include <dm/pinctrl.h>
Simon Glass9293fa42016-11-13 14:22:14 -070021#include <dt-bindings/clock/rk3288-cru.h>
22#include <power/regulator.h>
Simon Glass2cffe662015-08-30 16:55:38 -060023
24DECLARE_GLOBAL_DATA_PTR;
25
Jacob Chenc95f3782016-09-19 18:46:28 +080026__weak int rk_board_late_init(void)
27{
28 return 0;
29}
30
Nickey Yang Nickey Yang6766d142016-12-29 10:47:30 +080031int rk3288_qos_init(void)
32{
33 int val = 2 << PRIORITY_HIGH_SHIFT | 2 << PRIORITY_LOW_SHIFT;
34 /* set vop qos to higher priority */
35 writel(val, CPU_AXI_QOS_PRIORITY + VIO0_VOP_QOS);
36 writel(val, CPU_AXI_QOS_PRIORITY + VIO1_VOP_QOS);
37
38 if (!fdt_node_check_compatible(gd->fdt_blob, 0,
Eddie Cai1e1a79f2017-01-18 11:03:54 +080039 "rockchip,rk3288-tinker"))
Nickey Yang Nickey Yang6766d142016-12-29 10:47:30 +080040 {
41 /* set isp qos to higher priority */
42 writel(val, CPU_AXI_QOS_PRIORITY + VIO1_ISP_R_QOS);
43 writel(val, CPU_AXI_QOS_PRIORITY + VIO1_ISP_W0_QOS);
44 writel(val, CPU_AXI_QOS_PRIORITY + VIO1_ISP_W1_QOS);
45 }
46 return 0;
47}
48
Wadim Egorove9eb66c2017-08-21 13:36:57 +020049static void rk3288_detect_reset_reason(void)
50{
51 struct rk3288_cru *cru = rockchip_get_cru();
52 const char *reason;
53
54 if (IS_ERR(cru))
55 return;
56
57 switch (cru->cru_glb_rst_st) {
58 case GLB_POR_RST:
59 reason = "POR";
60 break;
61 case FST_GLB_RST_ST:
62 case SND_GLB_RST_ST:
63 reason = "RST";
64 break;
65 case FST_GLB_TSADC_RST_ST:
66 case SND_GLB_TSADC_RST_ST:
67 reason = "THERMAL";
68 break;
69 case FST_GLB_WDT_RST_ST:
70 case SND_GLB_WDT_RST_ST:
71 reason = "WDOG";
72 break;
73 default:
74 reason = "unknown reset";
75 }
76
77 env_set("reset_reason", reason);
78
79 /*
80 * Clear cru_glb_rst_st, so we can determine the last reset cause
81 * for following resets.
82 */
83 rk_clrreg(&cru->cru_glb_rst_st, GLB_RST_ST_MASK);
84}
85
Jacob Chenc95f3782016-09-19 18:46:28 +080086int board_late_init(void)
87{
88 setup_boot_mode();
Nickey Yang Nickey Yang6766d142016-12-29 10:47:30 +080089 rk3288_qos_init();
Wadim Egorove9eb66c2017-08-21 13:36:57 +020090 rk3288_detect_reset_reason();
Jacob Chenc95f3782016-09-19 18:46:28 +080091
92 return rk_board_late_init();
93}
94
Philipp Tomsich798370f2017-06-29 11:21:15 +020095#if !CONFIG_IS_ENABLED(ROCKCHIP_BACK_TO_BROM)
Simon Glass9293fa42016-11-13 14:22:14 -070096static int veyron_init(void)
97{
98 struct udevice *dev;
99 struct clk clk;
100 int ret;
101
102 ret = regulator_get_by_platname("vdd_arm", &dev);
Simon Glassc4c2ca62017-05-31 17:57:27 -0600103 if (ret) {
104 debug("Cannot set regulator name\n");
Simon Glass9293fa42016-11-13 14:22:14 -0700105 return ret;
Simon Glassc4c2ca62017-05-31 17:57:27 -0600106 }
Simon Glass9293fa42016-11-13 14:22:14 -0700107
108 /* Slowly raise to max CPU voltage to prevent overshoot */
109 ret = regulator_set_value(dev, 1200000);
110 if (ret)
111 return ret;
112 udelay(175); /* Must wait for voltage to stabilize, 2mV/us */
113 ret = regulator_set_value(dev, 1400000);
114 if (ret)
115 return ret;
116 udelay(100); /* Must wait for voltage to stabilize, 2mV/us */
117
118 ret = rockchip_get_clk(&clk.dev);
119 if (ret)
120 return ret;
121 clk.id = PLL_APLL;
122 ret = clk_set_rate(&clk, 1800000000);
123 if (IS_ERR_VALUE(ret))
124 return ret;
125
126 return 0;
127}
128#endif
129
Simon Glass2cffe662015-08-30 16:55:38 -0600130int board_init(void)
131{
Philipp Tomsich798370f2017-06-29 11:21:15 +0200132#if CONFIG_IS_ENABLED(ROCKCHIP_BACK_TO_BROM)
Xu Ziyuan5401eb82016-07-12 19:09:49 +0800133 struct udevice *pinctrl;
134 int ret;
135
Jacob Chenc95f3782016-09-19 18:46:28 +0800136 /*
137 * We need to implement sdcard iomux here for the further
138 * initlization, otherwise, it'll hit sdcard command sending
139 * timeout exception.
140 */
Xu Ziyuan5401eb82016-07-12 19:09:49 +0800141 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl);
142 if (ret) {
143 debug("%s: Cannot find pinctrl device\n", __func__);
144 goto err;
145 }
146 ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_SDCARD);
147 if (ret) {
148 debug("%s: Failed to set up SD card\n", __func__);
149 goto err;
150 }
151
152 return 0;
153err:
154 printf("board_init: Error %d\n", ret);
155
156 /* No way to report error here */
157 hang();
158
159 return -1;
160#else
Simon Glass9293fa42016-11-13 14:22:14 -0700161 int ret;
162
163 /* We do some SoC one time setting here */
164 if (!fdt_node_check_compatible(gd->fdt_blob, 0, "google,veyron")) {
165 ret = veyron_init();
166 if (ret)
167 return ret;
168 }
169
Simon Glass2cffe662015-08-30 16:55:38 -0600170 return 0;
Xu Ziyuan5401eb82016-07-12 19:09:49 +0800171#endif
Simon Glass2cffe662015-08-30 16:55:38 -0600172}
173
Simon Glass2cffe662015-08-30 16:55:38 -0600174#ifndef CONFIG_SYS_DCACHE_OFF
175void enable_caches(void)
176{
177 /* Enable D-cache. I-cache is already enabled in start.S */
178 dcache_enable();
179}
180#endif
Simon Glasse7d7a012016-01-21 19:45:06 -0700181
Xu Ziyuana11a53f2016-07-15 00:26:59 +0800182#if defined(CONFIG_USB_GADGET) && defined(CONFIG_USB_GADGET_DWC2_OTG)
183#include <usb.h>
184#include <usb/dwc2_udc.h>
185
186static struct dwc2_plat_otg_data rk3288_otg_data = {
187 .rx_fifo_sz = 512,
188 .np_tx_fifo_sz = 16,
189 .tx_fifo_sz = 128,
190};
191
192int board_usb_init(int index, enum usb_init_type init)
193{
194 int node, phy_node;
195 const char *mode;
196 bool matched = false;
197 const void *blob = gd->fdt_blob;
198 u32 grf_phy_offset;
199
200 /* find the usb_otg node */
201 node = fdt_node_offset_by_compatible(blob, -1,
202 "rockchip,rk3288-usb");
203
204 while (node > 0) {
205 mode = fdt_getprop(blob, node, "dr_mode", NULL);
206 if (mode && strcmp(mode, "otg") == 0) {
207 matched = true;
208 break;
209 }
210
211 node = fdt_node_offset_by_compatible(blob, node,
212 "rockchip,rk3288-usb");
213 }
214 if (!matched) {
215 debug("Not found usb_otg device\n");
216 return -ENODEV;
217 }
218 rk3288_otg_data.regs_otg = fdtdec_get_addr(blob, node, "reg");
219
220 node = fdtdec_lookup_phandle(blob, node, "phys");
221 if (node <= 0) {
222 debug("Not found usb phy device\n");
223 return -ENODEV;
224 }
225
226 phy_node = fdt_parent_offset(blob, node);
227 if (phy_node <= 0) {
228 debug("Not found usb phy device\n");
229 return -ENODEV;
230 }
231
232 rk3288_otg_data.phy_of_node = phy_node;
233 grf_phy_offset = fdtdec_get_addr(blob, node, "reg");
234
235 /* find the grf node */
236 node = fdt_node_offset_by_compatible(blob, -1,
237 "rockchip,rk3288-grf");
238 if (node <= 0) {
239 debug("Not found grf device\n");
240 return -ENODEV;
241 }
242 rk3288_otg_data.regs_phy = grf_phy_offset +
243 fdtdec_get_addr(blob, node, "reg");
244
245 return dwc2_udc_probe(&rk3288_otg_data);
246}
247
248int board_usb_cleanup(int index, enum usb_init_type init)
249{
250 return 0;
251}
252#endif
253
Simon Glass38675c42016-01-21 19:45:07 -0700254static int do_clock(cmd_tbl_t *cmdtp, int flag, int argc,
255 char * const argv[])
256{
Stephen Warrena9622432016-06-17 09:44:00 -0600257 static const struct {
258 char *name;
259 int id;
260 } clks[] = {
261 { "osc", CLK_OSC },
262 { "apll", CLK_ARM },
263 { "dpll", CLK_DDR },
264 { "cpll", CLK_CODEC },
265 { "gpll", CLK_GENERAL },
266#ifdef CONFIG_ROCKCHIP_RK3036
267 { "mpll", CLK_NEW },
268#else
269 { "npll", CLK_NEW },
270#endif
271 };
272 int ret, i;
Simon Glass38675c42016-01-21 19:45:07 -0700273 struct udevice *dev;
274
Simon Glassae8fe412016-07-17 15:23:17 -0600275 ret = rockchip_get_clk(&dev);
Stephen Warrena9622432016-06-17 09:44:00 -0600276 if (ret) {
277 printf("clk-uclass not found\n");
278 return 0;
279 }
280
281 for (i = 0; i < ARRAY_SIZE(clks); i++) {
282 struct clk clk;
Simon Glass38675c42016-01-21 19:45:07 -0700283 ulong rate;
284
Stephen Warrena9622432016-06-17 09:44:00 -0600285 clk.id = clks[i].id;
286 ret = clk_request(dev, &clk);
287 if (ret < 0)
288 continue;
289
290 rate = clk_get_rate(&clk);
291 printf("%s: %lu\n", clks[i].name, rate);
292
293 clk_free(&clk);
Simon Glass38675c42016-01-21 19:45:07 -0700294 }
295
296 return 0;
297}
298
299U_BOOT_CMD(
300 clock, 2, 1, do_clock,
301 "display information about clocks",
302 ""
303);
Simon Glasse608ced2017-05-31 17:57:33 -0600304
305#define GRF_SOC_CON2 0xff77024c
306
307int board_early_init_f(void)
308{
309 struct udevice *pinctrl;
310 struct udevice *dev;
311 int ret;
312
313 /*
314 * This init is done in SPL, but when chain-loading U-Boot SPL will
315 * have been skipped. Allow the clock driver to check if it needs
316 * setting up.
317 */
318 ret = rockchip_get_clk(&dev);
319 if (ret) {
320 debug("CLK init failed: %d\n", ret);
321 return ret;
322 }
323 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl);
324 if (ret) {
325 debug("%s: Cannot find pinctrl device\n", __func__);
326 return ret;
327 }
328
329 /* Enable debug UART */
330 ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_UART_DBG);
331 if (ret) {
332 debug("%s: Failed to set up console UART\n", __func__);
333 return ret;
334 }
335 rk_setreg(GRF_SOC_CON2, 1 << 0);
336
337 return 0;
338}