Merge tag 'rpi-next-2020.04' of https://gitlab.denx.de/u-boot/custodians/u-boot-raspb...
[oweals/u-boot.git] / arch / arm / mach-rockchip / board.c
1 // 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 <cpu_func.h>
8 #include <dm.h>
9 #include <init.h>
10 #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>
16 #include <asm/arch-rockchip/misc.h>
17 #include <power/regulator.h>
18
19 DECLARE_GLOBAL_DATA_PTR;
20
21 __weak int rk_board_late_init(void)
22 {
23         return 0;
24 }
25
26 int board_late_init(void)
27 {
28         setup_boot_mode();
29
30         return rk_board_late_init();
31 }
32
33 int 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)
47 void enable_caches(void)
48 {
49         /* Enable D-cache. I-cache is already enabled in start.S */
50         dcache_enable();
51 }
52 #endif
53
54 #if defined(CONFIG_USB_GADGET)
55 #include <usb.h>
56
57 #if defined(CONFIG_USB_GADGET_DWC2_OTG)
58 #include <usb/dwc2_udc.h>
59
60 static 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
66 int board_usb_init(int index, enum usb_init_type init)
67 {
68         ofnode node;
69         const char *mode;
70         bool matched = false;
71
72         /* find the usb_otg node */
73         node = ofnode_by_compatible(ofnode_null(), "snps,dwc2");
74         while (ofnode_valid(node)) {
75                 mode = ofnode_read_string(node, "dr_mode");
76                 if (mode && strcmp(mode, "otg") == 0) {
77                         matched = true;
78                         break;
79                 }
80
81                 node = ofnode_by_compatible(node, "snps,dwc2");
82         }
83         if (!matched) {
84                 debug("Not found usb_otg device\n");
85                 return -ENODEV;
86         }
87         otg_data.regs_otg = ofnode_get_addr(node);
88
89 #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
117         return dwc2_udc_probe(&otg_data);
118 }
119
120 int board_usb_cleanup(int index, enum usb_init_type init)
121 {
122         return 0;
123 }
124 #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
129 static 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
138 int usb_gadget_handle_interrupts(void)
139 {
140         dwc3_uboot_handle_interrupt(0);
141         return 0;
142 }
143
144 int 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 */
151
152 #if CONFIG_IS_ENABLED(FASTBOOT)
153 int 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
162
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