blob: 628dc967aaeab026339b512a94af58a88e2063a8 [file] [log] [blame]
Kever Yang54f17fa2019-07-22 20:02:01 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2019 Rockchip Electronics Co., Ltd.
4 */
5#include <common.h>
6#include <clk.h>
Simon Glass9edefc22019-11-14 12:57:37 -07007#include <cpu_func.h>
Kever Yang54f17fa2019-07-22 20:02:01 +08008#include <dm.h>
9#include <ram.h>
10#include <syscon.h>
11#include <asm/io.h>
12#include <asm/arch-rockchip/boot_mode.h>
13#include <asm/arch-rockchip/clock.h>
14#include <asm/arch-rockchip/periph.h>
Rohan Garg04825382019-08-12 17:04:34 +020015#include <asm/arch-rockchip/misc.h>
Kever Yang54f17fa2019-07-22 20:02:01 +080016#include <power/regulator.h>
17
18DECLARE_GLOBAL_DATA_PTR;
19
20__weak int rk_board_late_init(void)
21{
22 return 0;
23}
24
25int board_late_init(void)
26{
27 setup_boot_mode();
28
29 return rk_board_late_init();
30}
31
32int board_init(void)
33{
34 int ret;
35
36#ifdef CONFIG_DM_REGULATOR
37 ret = regulators_enable_boot_on(false);
38 if (ret)
39 debug("%s: Cannot enable boot on regulator\n", __func__);
40#endif
41
42 return 0;
43}
44
45#if !defined(CONFIG_SYS_DCACHE_OFF) && !defined(CONFIG_ARM64)
46void enable_caches(void)
47{
48 /* Enable D-cache. I-cache is already enabled in start.S */
49 dcache_enable();
50}
51#endif
52
Jagan Tekic618bb02019-11-19 13:56:22 +053053#if defined(CONFIG_USB_GADGET)
Kever Yang54f17fa2019-07-22 20:02:01 +080054#include <usb.h>
Jagan Tekic618bb02019-11-19 13:56:22 +053055
56#if defined(CONFIG_USB_GADGET_DWC2_OTG)
Kever Yang54f17fa2019-07-22 20:02:01 +080057#include <usb/dwc2_udc.h>
58
59static struct dwc2_plat_otg_data otg_data = {
60 .rx_fifo_sz = 512,
61 .np_tx_fifo_sz = 16,
62 .tx_fifo_sz = 128,
63};
64
65int board_usb_init(int index, enum usb_init_type init)
66{
Kever Yange76943c2019-10-16 17:13:31 +080067 ofnode node;
Kever Yang54f17fa2019-07-22 20:02:01 +080068 const char *mode;
69 bool matched = false;
Kever Yang54f17fa2019-07-22 20:02:01 +080070
71 /* find the usb_otg node */
Kever Yange76943c2019-10-16 17:13:31 +080072 node = ofnode_by_compatible(ofnode_null(), "snps,dwc2");
73 while (ofnode_valid(node)) {
74 mode = ofnode_read_string(node, "dr_mode");
Kever Yang54f17fa2019-07-22 20:02:01 +080075 if (mode && strcmp(mode, "otg") == 0) {
76 matched = true;
77 break;
78 }
79
Kever Yange76943c2019-10-16 17:13:31 +080080 node = ofnode_by_compatible(node, "snps,dwc2");
Kever Yang54f17fa2019-07-22 20:02:01 +080081 }
82 if (!matched) {
83 debug("Not found usb_otg device\n");
84 return -ENODEV;
85 }
Kever Yange76943c2019-10-16 17:13:31 +080086 otg_data.regs_otg = ofnode_get_addr(node);
Kever Yang54f17fa2019-07-22 20:02:01 +080087
Kever Yang17224b32019-10-16 17:13:32 +080088#ifdef CONFIG_ROCKCHIP_RK3288
89 int ret;
90 u32 phandle, offset;
91 ofnode phy_node;
92
93 ret = ofnode_read_u32(node, "phys", &phandle);
94 if (ret)
95 return ret;
96
97 node = ofnode_get_by_phandle(phandle);
98 if (!ofnode_valid(node)) {
99 debug("Not found usb phy device\n");
100 return -ENODEV;
101 }
102
103 phy_node = ofnode_get_parent(node);
104 if (!ofnode_valid(node)) {
105 debug("Not found usb phy device\n");
106 return -ENODEV;
107 }
108
109 otg_data.phy_of_node = phy_node;
110 ret = ofnode_read_u32(node, "reg", &offset);
111 if (ret)
112 return ret;
113 otg_data.regs_phy = offset +
114 (u32)syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
115#endif
Kever Yang54f17fa2019-07-22 20:02:01 +0800116 return dwc2_udc_probe(&otg_data);
117}
118
119int board_usb_cleanup(int index, enum usb_init_type init)
120{
121 return 0;
122}
Jagan Tekic618bb02019-11-19 13:56:22 +0530123#endif /* CONFIG_USB_GADGET_DWC2_OTG */
124
125#if defined(CONFIG_USB_DWC3_GADGET) && !defined(CONFIG_DM_USB_GADGET)
126#include <dwc3-uboot.h>
127
128static struct dwc3_device dwc3_device_data = {
129 .maximum_speed = USB_SPEED_HIGH,
130 .base = 0xfe800000,
131 .dr_mode = USB_DR_MODE_PERIPHERAL,
132 .index = 0,
133 .dis_u2_susphy_quirk = 1,
134 .hsphy_mode = USBPHY_INTERFACE_MODE_UTMIW,
135};
136
137int usb_gadget_handle_interrupts(void)
138{
139 dwc3_uboot_handle_interrupt(0);
140 return 0;
141}
142
143int board_usb_init(int index, enum usb_init_type init)
144{
145 return dwc3_uboot_init(&dwc3_device_data);
146}
147#endif /* CONFIG_USB_DWC3_GADGET */
148
149#endif /* CONFIG_USB_GADGET */
Kever Yang54f17fa2019-07-22 20:02:01 +0800150
151#if CONFIG_IS_ENABLED(FASTBOOT)
152int fastboot_set_reboot_flag(void)
153{
154 printf("Setting reboot to fastboot flag ...\n");
155 /* Set boot mode to fastboot */
156 writel(BOOT_FASTBOOT, CONFIG_ROCKCHIP_BOOT_MODE_REG);
157
158 return 0;
159}
160#endif
Rohan Garg04825382019-08-12 17:04:34 +0200161
162#ifdef CONFIG_MISC_INIT_R
163__weak int misc_init_r(void)
164{
165 const u32 cpuid_offset = 0x7;
166 const u32 cpuid_length = 0x10;
167 u8 cpuid[cpuid_length];
168 int ret;
169
170 ret = rockchip_cpuid_from_efuse(cpuid_offset, cpuid_length, cpuid);
171 if (ret)
172 return ret;
173
174 ret = rockchip_cpuid_set(cpuid, cpuid_length);
175 if (ret)
176 return ret;
177
178 ret = rockchip_setup_macaddr();
179
180 return ret;
181}
182#endif