common: Move some cache and MMU functions out of common.h
[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 <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>
15 #include <asm/arch-rockchip/misc.h>
16 #include <power/regulator.h>
17
18 DECLARE_GLOBAL_DATA_PTR;
19
20 __weak int rk_board_late_init(void)
21 {
22         return 0;
23 }
24
25 int board_late_init(void)
26 {
27         setup_boot_mode();
28
29         return rk_board_late_init();
30 }
31
32 int 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)
46 void enable_caches(void)
47 {
48         /* Enable D-cache. I-cache is already enabled in start.S */
49         dcache_enable();
50 }
51 #endif
52
53 #if defined(CONFIG_USB_GADGET)
54 #include <usb.h>
55
56 #if defined(CONFIG_USB_GADGET_DWC2_OTG)
57 #include <usb/dwc2_udc.h>
58
59 static 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
65 int board_usb_init(int index, enum usb_init_type init)
66 {
67         ofnode node;
68         const char *mode;
69         bool matched = false;
70
71         /* find the usb_otg node */
72         node = ofnode_by_compatible(ofnode_null(), "snps,dwc2");
73         while (ofnode_valid(node)) {
74                 mode = ofnode_read_string(node, "dr_mode");
75                 if (mode && strcmp(mode, "otg") == 0) {
76                         matched = true;
77                         break;
78                 }
79
80                 node = ofnode_by_compatible(node, "snps,dwc2");
81         }
82         if (!matched) {
83                 debug("Not found usb_otg device\n");
84                 return -ENODEV;
85         }
86         otg_data.regs_otg = ofnode_get_addr(node);
87
88 #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
116         return dwc2_udc_probe(&otg_data);
117 }
118
119 int board_usb_cleanup(int index, enum usb_init_type init)
120 {
121         return 0;
122 }
123 #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
128 static 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
137 int usb_gadget_handle_interrupts(void)
138 {
139         dwc3_uboot_handle_interrupt(0);
140         return 0;
141 }
142
143 int 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 */
150
151 #if CONFIG_IS_ENABLED(FASTBOOT)
152 int 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
161
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