blob: 73cd6684aad8e8eae99288bde9ce9e717dfcf03c [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass2444dae2015-08-30 16:55:38 -06002/*
3 * (C) Copyright 2015 Google, Inc
Simon Glass2444dae2015-08-30 16:55:38 -06004 */
5
6#include <common.h>
Simon Glass74e53e02016-01-21 19:45:07 -07007#include <clk.h>
Simon Glass2444dae2015-08-30 16:55:38 -06008#include <dm.h>
9#include <ram.h>
Jacob Chen67171e12016-09-19 18:46:28 +080010#include <syscon.h>
huang linbe1d5e02015-11-17 14:20:27 +080011#include <asm/io.h>
Stephen Warren135aa952016-06-17 09:44:00 -060012#include <asm/arch/clock.h>
Wadim Egorov40d4f792017-08-21 13:36:57 +020013#include <asm/arch/cru_rk3288.h>
Xu Ziyuanb47ea792016-07-12 19:09:49 +080014#include <asm/arch/periph.h>
Jacob Chen67171e12016-09-19 18:46:28 +080015#include <asm/arch/pmu_rk3288.h>
Nickey Yang Nickey Yang9b832012016-12-29 10:47:30 +080016#include <asm/arch/qos_rk3288.h>
Jacob Chen67171e12016-09-19 18:46:28 +080017#include <asm/arch/boot_mode.h>
Xu Ziyuanb47ea792016-07-12 19:09:49 +080018#include <asm/gpio.h>
19#include <dm/pinctrl.h>
Simon Glass20b13e82016-11-13 14:22:14 -070020#include <dt-bindings/clock/rk3288-cru.h>
21#include <power/regulator.h>
Simon Glass2444dae2015-08-30 16:55:38 -060022
23DECLARE_GLOBAL_DATA_PTR;
24
Jacob Chen67171e12016-09-19 18:46:28 +080025__weak int rk_board_late_init(void)
26{
27 return 0;
28}
29
Nickey Yang Nickey Yang9b832012016-12-29 10:47:30 +080030int rk3288_qos_init(void)
31{
32 int val = 2 << PRIORITY_HIGH_SHIFT | 2 << PRIORITY_LOW_SHIFT;
33 /* set vop qos to higher priority */
34 writel(val, CPU_AXI_QOS_PRIORITY + VIO0_VOP_QOS);
35 writel(val, CPU_AXI_QOS_PRIORITY + VIO1_VOP_QOS);
36
37 if (!fdt_node_check_compatible(gd->fdt_blob, 0,
Eddie Cai6f279762017-01-18 11:03:54 +080038 "rockchip,rk3288-tinker"))
Nickey Yang Nickey Yang9b832012016-12-29 10:47:30 +080039 {
40 /* set isp qos to higher priority */
41 writel(val, CPU_AXI_QOS_PRIORITY + VIO1_ISP_R_QOS);
42 writel(val, CPU_AXI_QOS_PRIORITY + VIO1_ISP_W0_QOS);
43 writel(val, CPU_AXI_QOS_PRIORITY + VIO1_ISP_W1_QOS);
44 }
45 return 0;
46}
47
Wadim Egorov40d4f792017-08-21 13:36:57 +020048static void rk3288_detect_reset_reason(void)
49{
50 struct rk3288_cru *cru = rockchip_get_cru();
51 const char *reason;
52
53 if (IS_ERR(cru))
54 return;
55
56 switch (cru->cru_glb_rst_st) {
57 case GLB_POR_RST:
58 reason = "POR";
59 break;
60 case FST_GLB_RST_ST:
61 case SND_GLB_RST_ST:
62 reason = "RST";
63 break;
64 case FST_GLB_TSADC_RST_ST:
65 case SND_GLB_TSADC_RST_ST:
66 reason = "THERMAL";
67 break;
68 case FST_GLB_WDT_RST_ST:
69 case SND_GLB_WDT_RST_ST:
70 reason = "WDOG";
71 break;
72 default:
73 reason = "unknown reset";
74 }
75
76 env_set("reset_reason", reason);
77
78 /*
79 * Clear cru_glb_rst_st, so we can determine the last reset cause
80 * for following resets.
81 */
82 rk_clrreg(&cru->cru_glb_rst_st, GLB_RST_ST_MASK);
83}
84
Jacob Chen67171e12016-09-19 18:46:28 +080085int board_late_init(void)
86{
87 setup_boot_mode();
Nickey Yang Nickey Yang9b832012016-12-29 10:47:30 +080088 rk3288_qos_init();
Wadim Egorov40d4f792017-08-21 13:36:57 +020089 rk3288_detect_reset_reason();
Jacob Chen67171e12016-09-19 18:46:28 +080090
91 return rk_board_late_init();
92}
93
Philipp Tomsichee14d292017-06-29 11:21:15 +020094#if !CONFIG_IS_ENABLED(ROCKCHIP_BACK_TO_BROM)
Simon Glass20b13e82016-11-13 14:22:14 -070095static int veyron_init(void)
96{
97 struct udevice *dev;
98 struct clk clk;
99 int ret;
100
101 ret = regulator_get_by_platname("vdd_arm", &dev);
Simon Glass6f06ef52017-05-31 17:57:27 -0600102 if (ret) {
103 debug("Cannot set regulator name\n");
Simon Glass20b13e82016-11-13 14:22:14 -0700104 return ret;
Simon Glass6f06ef52017-05-31 17:57:27 -0600105 }
Simon Glass20b13e82016-11-13 14:22:14 -0700106
107 /* Slowly raise to max CPU voltage to prevent overshoot */
108 ret = regulator_set_value(dev, 1200000);
109 if (ret)
110 return ret;
111 udelay(175); /* Must wait for voltage to stabilize, 2mV/us */
112 ret = regulator_set_value(dev, 1400000);
113 if (ret)
114 return ret;
115 udelay(100); /* Must wait for voltage to stabilize, 2mV/us */
116
117 ret = rockchip_get_clk(&clk.dev);
118 if (ret)
119 return ret;
120 clk.id = PLL_APLL;
121 ret = clk_set_rate(&clk, 1800000000);
122 if (IS_ERR_VALUE(ret))
123 return ret;
124
Carlo Caione0d4d5fd2018-06-11 20:00:48 +0100125 ret = regulators_enable_boot_on(false);
126 if (ret) {
127 debug("%s: Cannot enable boot on regulators\n", __func__);
128 return ret;
129 }
130
Simon Glass20b13e82016-11-13 14:22:14 -0700131 return 0;
132}
133#endif
134
Simon Glass2444dae2015-08-30 16:55:38 -0600135int board_init(void)
136{
Philipp Tomsichee14d292017-06-29 11:21:15 +0200137#if CONFIG_IS_ENABLED(ROCKCHIP_BACK_TO_BROM)
Xu Ziyuanb47ea792016-07-12 19:09:49 +0800138 struct udevice *pinctrl;
139 int ret;
140
Jacob Chen67171e12016-09-19 18:46:28 +0800141 /*
142 * We need to implement sdcard iomux here for the further
143 * initlization, otherwise, it'll hit sdcard command sending
144 * timeout exception.
145 */
Xu Ziyuanb47ea792016-07-12 19:09:49 +0800146 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl);
147 if (ret) {
148 debug("%s: Cannot find pinctrl device\n", __func__);
149 goto err;
150 }
151 ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_SDCARD);
152 if (ret) {
153 debug("%s: Failed to set up SD card\n", __func__);
154 goto err;
155 }
156
Simon Glass2444dae2015-08-30 16:55:38 -0600157 return 0;
Xu Ziyuanb47ea792016-07-12 19:09:49 +0800158err:
159 printf("board_init: Error %d\n", ret);
160
161 /* No way to report error here */
162 hang();
163
164 return -1;
165#else
Simon Glass20b13e82016-11-13 14:22:14 -0700166 int ret;
167
168 /* We do some SoC one time setting here */
169 if (!fdt_node_check_compatible(gd->fdt_blob, 0, "google,veyron")) {
170 ret = veyron_init();
171 if (ret)
172 return ret;
173 }
174
Xu Ziyuanb47ea792016-07-12 19:09:49 +0800175 return 0;
176#endif
Simon Glass2444dae2015-08-30 16:55:38 -0600177}
178
Simon Glass2444dae2015-08-30 16:55:38 -0600179#ifndef CONFIG_SYS_DCACHE_OFF
180void enable_caches(void)
181{
182 /* Enable D-cache. I-cache is already enabled in start.S */
183 dcache_enable();
184}
185#endif
Simon Glassad443b72016-01-21 19:45:06 -0700186
Xu Ziyuan266c8fa2016-07-15 00:26:59 +0800187#if defined(CONFIG_USB_GADGET) && defined(CONFIG_USB_GADGET_DWC2_OTG)
188#include <usb.h>
189#include <usb/dwc2_udc.h>
190
191static struct dwc2_plat_otg_data rk3288_otg_data = {
192 .rx_fifo_sz = 512,
193 .np_tx_fifo_sz = 16,
194 .tx_fifo_sz = 128,
195};
196
197int board_usb_init(int index, enum usb_init_type init)
198{
199 int node, phy_node;
200 const char *mode;
201 bool matched = false;
202 const void *blob = gd->fdt_blob;
203 u32 grf_phy_offset;
204
205 /* find the usb_otg node */
206 node = fdt_node_offset_by_compatible(blob, -1,
207 "rockchip,rk3288-usb");
208
209 while (node > 0) {
210 mode = fdt_getprop(blob, node, "dr_mode", NULL);
211 if (mode && strcmp(mode, "otg") == 0) {
212 matched = true;
213 break;
214 }
215
216 node = fdt_node_offset_by_compatible(blob, node,
217 "rockchip,rk3288-usb");
218 }
219 if (!matched) {
220 debug("Not found usb_otg device\n");
221 return -ENODEV;
222 }
223 rk3288_otg_data.regs_otg = fdtdec_get_addr(blob, node, "reg");
224
225 node = fdtdec_lookup_phandle(blob, node, "phys");
226 if (node <= 0) {
227 debug("Not found usb phy device\n");
228 return -ENODEV;
229 }
230
231 phy_node = fdt_parent_offset(blob, node);
232 if (phy_node <= 0) {
233 debug("Not found usb phy device\n");
234 return -ENODEV;
235 }
236
237 rk3288_otg_data.phy_of_node = phy_node;
238 grf_phy_offset = fdtdec_get_addr(blob, node, "reg");
239
240 /* find the grf node */
241 node = fdt_node_offset_by_compatible(blob, -1,
242 "rockchip,rk3288-grf");
243 if (node <= 0) {
244 debug("Not found grf device\n");
245 return -ENODEV;
246 }
247 rk3288_otg_data.regs_phy = grf_phy_offset +
248 fdtdec_get_addr(blob, node, "reg");
249
250 return dwc2_udc_probe(&rk3288_otg_data);
251}
252
253int board_usb_cleanup(int index, enum usb_init_type init)
254{
255 return 0;
256}
257#endif
258
Simon Glass74e53e02016-01-21 19:45:07 -0700259static int do_clock(cmd_tbl_t *cmdtp, int flag, int argc,
260 char * const argv[])
261{
Stephen Warren135aa952016-06-17 09:44:00 -0600262 static const struct {
263 char *name;
264 int id;
265 } clks[] = {
266 { "osc", CLK_OSC },
267 { "apll", CLK_ARM },
268 { "dpll", CLK_DDR },
269 { "cpll", CLK_CODEC },
270 { "gpll", CLK_GENERAL },
271#ifdef CONFIG_ROCKCHIP_RK3036
272 { "mpll", CLK_NEW },
273#else
274 { "npll", CLK_NEW },
275#endif
276 };
277 int ret, i;
Simon Glass74e53e02016-01-21 19:45:07 -0700278 struct udevice *dev;
279
Simon Glassc3aad6f2016-07-17 15:23:17 -0600280 ret = rockchip_get_clk(&dev);
Stephen Warren135aa952016-06-17 09:44:00 -0600281 if (ret) {
282 printf("clk-uclass not found\n");
283 return 0;
284 }
285
286 for (i = 0; i < ARRAY_SIZE(clks); i++) {
287 struct clk clk;
Simon Glass74e53e02016-01-21 19:45:07 -0700288 ulong rate;
289
Stephen Warren135aa952016-06-17 09:44:00 -0600290 clk.id = clks[i].id;
291 ret = clk_request(dev, &clk);
292 if (ret < 0)
293 continue;
294
295 rate = clk_get_rate(&clk);
296 printf("%s: %lu\n", clks[i].name, rate);
297
298 clk_free(&clk);
Simon Glass74e53e02016-01-21 19:45:07 -0700299 }
300
301 return 0;
302}
303
304U_BOOT_CMD(
305 clock, 2, 1, do_clock,
306 "display information about clocks",
307 ""
308);
Simon Glassfe974712017-05-31 17:57:33 -0600309
Simon Glassfe974712017-05-31 17:57:33 -0600310int board_early_init_f(void)
311{
Carlo Caione389167c2018-06-11 20:00:49 +0100312 const uintptr_t GRF_SOC_CON0 = 0xff770244;
313 const uintptr_t GRF_SOC_CON2 = 0xff77024c;
Simon Glassfe974712017-05-31 17:57:33 -0600314 struct udevice *pinctrl;
315 struct udevice *dev;
316 int ret;
317
318 /*
319 * This init is done in SPL, but when chain-loading U-Boot SPL will
320 * have been skipped. Allow the clock driver to check if it needs
321 * setting up.
322 */
323 ret = rockchip_get_clk(&dev);
324 if (ret) {
325 debug("CLK init failed: %d\n", ret);
326 return ret;
327 }
328 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl);
329 if (ret) {
330 debug("%s: Cannot find pinctrl device\n", __func__);
331 return ret;
332 }
333
334 /* Enable debug UART */
335 ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_UART_DBG);
336 if (ret) {
337 debug("%s: Failed to set up console UART\n", __func__);
338 return ret;
339 }
340 rk_setreg(GRF_SOC_CON2, 1 << 0);
341
Carlo Caione389167c2018-06-11 20:00:49 +0100342 /*
343 * Disable JTAG on sdmmc0 IO. The SDMMC won't work until this bit is
344 * cleared
345 */
346 rk_clrreg(GRF_SOC_CON0, 1 << 12);
347
Simon Glassfe974712017-05-31 17:57:33 -0600348 return 0;
349}