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