blob: 14b9e89ea352fb79f61af2c3b62a6e6997dc3fb8 [file] [log] [blame]
Kever Yangbb337732019-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 Glass1d91ba72019-11-14 12:57:37 -07007#include <cpu_func.h>
Kever Yangbb337732019-07-22 20:02:01 +08008#include <dm.h>
Simon Glassa7b51302019-11-14 12:57:46 -07009#include <init.h>
Kever Yangbb337732019-07-22 20:02:01 +080010#include <ram.h>
11#include <syscon.h>
12#include <asm/io.h>
13#include <asm/arch-rockchip/boot_mode.h>
14#include <asm/arch-rockchip/clock.h>
15#include <asm/arch-rockchip/periph.h>
Rohan Gargcfdc1922019-08-12 17:04:34 +020016#include <asm/arch-rockchip/misc.h>
Kever Yangbb337732019-07-22 20:02:01 +080017#include <power/regulator.h>
18
19DECLARE_GLOBAL_DATA_PTR;
20
21__weak int rk_board_late_init(void)
22{
23 return 0;
24}
25
26int board_late_init(void)
27{
28 setup_boot_mode();
29
30 return rk_board_late_init();
31}
32
33int board_init(void)
34{
35 int ret;
36
37#ifdef CONFIG_DM_REGULATOR
38 ret = regulators_enable_boot_on(false);
39 if (ret)
40 debug("%s: Cannot enable boot on regulator\n", __func__);
41#endif
42
43 return 0;
44}
45
46#if !defined(CONFIG_SYS_DCACHE_OFF) && !defined(CONFIG_ARM64)
47void enable_caches(void)
48{
49 /* Enable D-cache. I-cache is already enabled in start.S */
50 dcache_enable();
51}
52#endif
53
Jagan Teki886ecb22019-11-19 13:56:22 +053054#if defined(CONFIG_USB_GADGET)
Kever Yangbb337732019-07-22 20:02:01 +080055#include <usb.h>
Jagan Teki886ecb22019-11-19 13:56:22 +053056
57#if defined(CONFIG_USB_GADGET_DWC2_OTG)
Kever Yangbb337732019-07-22 20:02:01 +080058#include <usb/dwc2_udc.h>
59
60static struct dwc2_plat_otg_data otg_data = {
61 .rx_fifo_sz = 512,
62 .np_tx_fifo_sz = 16,
63 .tx_fifo_sz = 128,
64};
65
66int board_usb_init(int index, enum usb_init_type init)
67{
Kever Yang45bda032019-10-16 17:13:31 +080068 ofnode node;
Kever Yangbb337732019-07-22 20:02:01 +080069 const char *mode;
70 bool matched = false;
Kever Yangbb337732019-07-22 20:02:01 +080071
72 /* find the usb_otg node */
Kever Yang45bda032019-10-16 17:13:31 +080073 node = ofnode_by_compatible(ofnode_null(), "snps,dwc2");
74 while (ofnode_valid(node)) {
75 mode = ofnode_read_string(node, "dr_mode");
Kever Yangbb337732019-07-22 20:02:01 +080076 if (mode && strcmp(mode, "otg") == 0) {
77 matched = true;
78 break;
79 }
80
Kever Yang45bda032019-10-16 17:13:31 +080081 node = ofnode_by_compatible(node, "snps,dwc2");
Kever Yangbb337732019-07-22 20:02:01 +080082 }
83 if (!matched) {
84 debug("Not found usb_otg device\n");
85 return -ENODEV;
86 }
Kever Yang45bda032019-10-16 17:13:31 +080087 otg_data.regs_otg = ofnode_get_addr(node);
Kever Yangbb337732019-07-22 20:02:01 +080088
Kever Yang6169a0d2019-10-16 17:13:32 +080089#ifdef CONFIG_ROCKCHIP_RK3288
90 int ret;
91 u32 phandle, offset;
92 ofnode phy_node;
93
94 ret = ofnode_read_u32(node, "phys", &phandle);
95 if (ret)
96 return ret;
97
98 node = ofnode_get_by_phandle(phandle);
99 if (!ofnode_valid(node)) {
100 debug("Not found usb phy device\n");
101 return -ENODEV;
102 }
103
104 phy_node = ofnode_get_parent(node);
105 if (!ofnode_valid(node)) {
106 debug("Not found usb phy device\n");
107 return -ENODEV;
108 }
109
110 otg_data.phy_of_node = phy_node;
111 ret = ofnode_read_u32(node, "reg", &offset);
112 if (ret)
113 return ret;
114 otg_data.regs_phy = offset +
115 (u32)syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
116#endif
Kever Yangbb337732019-07-22 20:02:01 +0800117 return dwc2_udc_probe(&otg_data);
118}
119
120int board_usb_cleanup(int index, enum usb_init_type init)
121{
122 return 0;
123}
Jagan Teki886ecb22019-11-19 13:56:22 +0530124#endif /* CONFIG_USB_GADGET_DWC2_OTG */
125
126#if defined(CONFIG_USB_DWC3_GADGET) && !defined(CONFIG_DM_USB_GADGET)
127#include <dwc3-uboot.h>
128
129static struct dwc3_device dwc3_device_data = {
130 .maximum_speed = USB_SPEED_HIGH,
131 .base = 0xfe800000,
132 .dr_mode = USB_DR_MODE_PERIPHERAL,
133 .index = 0,
134 .dis_u2_susphy_quirk = 1,
135 .hsphy_mode = USBPHY_INTERFACE_MODE_UTMIW,
136};
137
138int usb_gadget_handle_interrupts(void)
139{
140 dwc3_uboot_handle_interrupt(0);
141 return 0;
142}
143
144int board_usb_init(int index, enum usb_init_type init)
145{
146 return dwc3_uboot_init(&dwc3_device_data);
147}
148#endif /* CONFIG_USB_DWC3_GADGET */
149
150#endif /* CONFIG_USB_GADGET */
Kever Yangbb337732019-07-22 20:02:01 +0800151
152#if CONFIG_IS_ENABLED(FASTBOOT)
153int fastboot_set_reboot_flag(void)
154{
155 printf("Setting reboot to fastboot flag ...\n");
156 /* Set boot mode to fastboot */
157 writel(BOOT_FASTBOOT, CONFIG_ROCKCHIP_BOOT_MODE_REG);
158
159 return 0;
160}
161#endif
Rohan Gargcfdc1922019-08-12 17:04:34 +0200162
163#ifdef CONFIG_MISC_INIT_R
164__weak int misc_init_r(void)
165{
166 const u32 cpuid_offset = 0x7;
167 const u32 cpuid_length = 0x10;
168 u8 cpuid[cpuid_length];
169 int ret;
170
171 ret = rockchip_cpuid_from_efuse(cpuid_offset, cpuid_length, cpuid);
172 if (ret)
173 return ret;
174
175 ret = rockchip_cpuid_set(cpuid, cpuid_length);
176 if (ret)
177 return ret;
178
179 ret = rockchip_setup_macaddr();
180
181 return ret;
182}
183#endif