blob: ba4da72b3910df570647b5d224148c1d2c66601d [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>
Roman Kovalivskyi851737a2020-07-28 23:35:32 +03009#include <fastboot.h>
Simon Glass52559322019-11-14 12:57:46 -070010#include <init.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060011#include <log.h>
Kever Yang54f17fa2019-07-22 20:02:01 +080012#include <ram.h>
13#include <syscon.h>
Simon Glass90526e92020-05-10 11:39:56 -060014#include <asm/cache.h>
Kever Yang54f17fa2019-07-22 20:02:01 +080015#include <asm/io.h>
16#include <asm/arch-rockchip/boot_mode.h>
17#include <asm/arch-rockchip/clock.h>
18#include <asm/arch-rockchip/periph.h>
Rohan Garg04825382019-08-12 17:04:34 +020019#include <asm/arch-rockchip/misc.h>
Kever Yang54f17fa2019-07-22 20:02:01 +080020#include <power/regulator.h>
21
22DECLARE_GLOBAL_DATA_PTR;
23
24__weak int rk_board_late_init(void)
25{
26 return 0;
27}
28
29int board_late_init(void)
30{
31 setup_boot_mode();
32
33 return rk_board_late_init();
34}
35
36int board_init(void)
37{
38 int ret;
39
40#ifdef CONFIG_DM_REGULATOR
41 ret = regulators_enable_boot_on(false);
42 if (ret)
43 debug("%s: Cannot enable boot on regulator\n", __func__);
44#endif
45
46 return 0;
47}
48
49#if !defined(CONFIG_SYS_DCACHE_OFF) && !defined(CONFIG_ARM64)
50void enable_caches(void)
51{
52 /* Enable D-cache. I-cache is already enabled in start.S */
53 dcache_enable();
54}
55#endif
56
Jagan Tekic618bb02019-11-19 13:56:22 +053057#if defined(CONFIG_USB_GADGET)
Kever Yang54f17fa2019-07-22 20:02:01 +080058#include <usb.h>
Jagan Tekic618bb02019-11-19 13:56:22 +053059
60#if defined(CONFIG_USB_GADGET_DWC2_OTG)
Kever Yang54f17fa2019-07-22 20:02:01 +080061#include <usb/dwc2_udc.h>
62
63static struct dwc2_plat_otg_data otg_data = {
64 .rx_fifo_sz = 512,
65 .np_tx_fifo_sz = 16,
66 .tx_fifo_sz = 128,
67};
68
69int board_usb_init(int index, enum usb_init_type init)
70{
Kever Yange76943c2019-10-16 17:13:31 +080071 ofnode node;
Kever Yang54f17fa2019-07-22 20:02:01 +080072 const char *mode;
73 bool matched = false;
Kever Yang54f17fa2019-07-22 20:02:01 +080074
75 /* find the usb_otg node */
Kever Yange76943c2019-10-16 17:13:31 +080076 node = ofnode_by_compatible(ofnode_null(), "snps,dwc2");
77 while (ofnode_valid(node)) {
78 mode = ofnode_read_string(node, "dr_mode");
Kever Yang54f17fa2019-07-22 20:02:01 +080079 if (mode && strcmp(mode, "otg") == 0) {
80 matched = true;
81 break;
82 }
83
Kever Yange76943c2019-10-16 17:13:31 +080084 node = ofnode_by_compatible(node, "snps,dwc2");
Kever Yang54f17fa2019-07-22 20:02:01 +080085 }
86 if (!matched) {
87 debug("Not found usb_otg device\n");
88 return -ENODEV;
89 }
Kever Yange76943c2019-10-16 17:13:31 +080090 otg_data.regs_otg = ofnode_get_addr(node);
Kever Yang54f17fa2019-07-22 20:02:01 +080091
Kever Yang17224b32019-10-16 17:13:32 +080092#ifdef CONFIG_ROCKCHIP_RK3288
93 int ret;
94 u32 phandle, offset;
95 ofnode phy_node;
96
97 ret = ofnode_read_u32(node, "phys", &phandle);
98 if (ret)
99 return ret;
100
101 node = ofnode_get_by_phandle(phandle);
102 if (!ofnode_valid(node)) {
103 debug("Not found usb phy device\n");
104 return -ENODEV;
105 }
106
107 phy_node = ofnode_get_parent(node);
108 if (!ofnode_valid(node)) {
109 debug("Not found usb phy device\n");
110 return -ENODEV;
111 }
112
113 otg_data.phy_of_node = phy_node;
114 ret = ofnode_read_u32(node, "reg", &offset);
115 if (ret)
116 return ret;
117 otg_data.regs_phy = offset +
118 (u32)syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
119#endif
Kever Yang54f17fa2019-07-22 20:02:01 +0800120 return dwc2_udc_probe(&otg_data);
121}
122
123int board_usb_cleanup(int index, enum usb_init_type init)
124{
125 return 0;
126}
Jagan Tekic618bb02019-11-19 13:56:22 +0530127#endif /* CONFIG_USB_GADGET_DWC2_OTG */
128
129#if defined(CONFIG_USB_DWC3_GADGET) && !defined(CONFIG_DM_USB_GADGET)
130#include <dwc3-uboot.h>
131
132static struct dwc3_device dwc3_device_data = {
133 .maximum_speed = USB_SPEED_HIGH,
134 .base = 0xfe800000,
135 .dr_mode = USB_DR_MODE_PERIPHERAL,
136 .index = 0,
137 .dis_u2_susphy_quirk = 1,
138 .hsphy_mode = USBPHY_INTERFACE_MODE_UTMIW,
139};
140
141int usb_gadget_handle_interrupts(void)
142{
143 dwc3_uboot_handle_interrupt(0);
144 return 0;
145}
146
147int board_usb_init(int index, enum usb_init_type init)
148{
149 return dwc3_uboot_init(&dwc3_device_data);
150}
151#endif /* CONFIG_USB_DWC3_GADGET */
152
153#endif /* CONFIG_USB_GADGET */
Kever Yang54f17fa2019-07-22 20:02:01 +0800154
155#if CONFIG_IS_ENABLED(FASTBOOT)
Roman Kovalivskyi851737a2020-07-28 23:35:32 +0300156int fastboot_set_reboot_flag(enum fastboot_reboot_reason reason)
Kever Yang54f17fa2019-07-22 20:02:01 +0800157{
Roman Kovalivskyi851737a2020-07-28 23:35:32 +0300158 if (reason != FASTBOOT_REBOOT_REASON_BOOTLOADER)
159 return -ENOTSUPP;
160
Kever Yang54f17fa2019-07-22 20:02:01 +0800161 printf("Setting reboot to fastboot flag ...\n");
162 /* Set boot mode to fastboot */
163 writel(BOOT_FASTBOOT, CONFIG_ROCKCHIP_BOOT_MODE_REG);
164
165 return 0;
166}
167#endif
Rohan Garg04825382019-08-12 17:04:34 +0200168
169#ifdef CONFIG_MISC_INIT_R
170__weak int misc_init_r(void)
171{
172 const u32 cpuid_offset = 0x7;
173 const u32 cpuid_length = 0x10;
174 u8 cpuid[cpuid_length];
175 int ret;
176
177 ret = rockchip_cpuid_from_efuse(cpuid_offset, cpuid_length, cpuid);
178 if (ret)
179 return ret;
180
181 ret = rockchip_cpuid_set(cpuid, cpuid_length);
182 if (ret)
183 return ret;
184
185 ret = rockchip_setup_macaddr();
186
187 return ret;
188}
189#endif