1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2019 Rockchip Electronics Co., Ltd.
9 #include <efi_loader.h>
18 #include <asm/cache.h>
19 #include <asm/global_data.h>
21 #include <asm/arch-rockchip/boot_mode.h>
22 #include <asm/arch-rockchip/clock.h>
23 #include <asm/arch-rockchip/periph.h>
24 #include <asm/arch-rockchip/misc.h>
25 #include <power/regulator.h>
27 DECLARE_GLOBAL_DATA_PTR;
29 #if defined(CONFIG_EFI_HAVE_CAPSULE_SUPPORT) && defined(CONFIG_EFI_PARTITION)
31 #define DFU_ALT_BUF_LEN SZ_1K
33 static struct efi_fw_image *fw_images;
35 static bool updatable_image(struct disk_partition *info)
39 efi_guid_t image_type_guid;
41 uuid_str_to_bin(info->type_guid, image_type_guid.b,
42 UUID_STR_FORMAT_GUID);
44 for (i = 0; i < num_image_type_guids; i++) {
45 if (!guidcmp(&fw_images[i].image_type_id, &image_type_guid)) {
54 static void set_image_index(struct disk_partition *info, int index)
57 efi_guid_t image_type_guid;
59 uuid_str_to_bin(info->type_guid, image_type_guid.b,
60 UUID_STR_FORMAT_GUID);
62 for (i = 0; i < num_image_type_guids; i++) {
63 if (!guidcmp(&fw_images[i].image_type_id, &image_type_guid)) {
64 fw_images[i].image_index = index;
70 static int get_mmc_desc(struct blk_desc **desc)
77 * For now the firmware images are assumed to
80 ret = uclass_get_device(UCLASS_MMC, 1, &dev);
84 mmc = mmc_get_mmc_dev(dev);
88 if ((ret = mmc_init(mmc)))
91 *desc = mmc_get_blk_desc(mmc);
98 void set_dfu_alt_info(char *interface, char *devstr)
102 int p, len, devnum, ret;
103 char buf[DFU_ALT_BUF_LEN];
104 struct disk_partition info;
105 struct blk_desc *desc = NULL;
107 ret = get_mmc_desc(&desc);
109 log_err("Unable to get mmc desc\n");
113 memset(buf, 0, sizeof(buf));
114 name = blk_get_uclass_name(desc->uclass_id);
115 devnum = desc->devnum;
118 len += snprintf(buf + len, DFU_ALT_BUF_LEN - len,
119 "%s %d=", name, devnum);
121 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
122 if (part_get_info(desc, p, &info))
125 /* Add entry to dfu_alt_info only for updatable images */
126 if (updatable_image(&info)) {
128 len += snprintf(buf + len,
129 DFU_ALT_BUF_LEN - len, ";");
131 len += snprintf(buf + len, DFU_ALT_BUF_LEN - len,
132 "%s%d_%s part %d %d",
133 name, devnum, info.name, devnum, p);
138 log_debug("dfu_alt_info => %s\n", buf);
139 env_set("dfu_alt_info", buf);
142 static void gpt_capsule_update_setup(void)
145 struct disk_partition info;
146 struct blk_desc *desc = NULL;
148 fw_images = update_info.images;
149 rockchip_capsule_update_board_setup();
151 ret = get_mmc_desc(&desc);
153 log_err("Unable to get mmc desc\n");
157 for (p = 1, i = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
158 if (part_get_info(desc, p, &info))
162 * Since we have a GPT partitioned device, the updatable
163 * images could be stored in any order. Populate the
164 * image_index at runtime.
166 if (updatable_image(&info)) {
167 set_image_index(&info, i);
172 #endif /* CONFIG_EFI_HAVE_CAPSULE_SUPPORT && CONFIG_EFI_PARTITION */
174 __weak int rk_board_late_init(void)
176 #if defined(CONFIG_EFI_HAVE_CAPSULE_SUPPORT) && defined(CONFIG_EFI_PARTITION)
177 gpt_capsule_update_setup();
183 int board_late_init(void)
187 return rk_board_late_init();
194 #ifdef CONFIG_DM_REGULATOR
195 ret = regulators_enable_boot_on(false);
197 debug("%s: Cannot enable boot on regulator\n", __func__);
203 #if !defined(CONFIG_SYS_DCACHE_OFF) && !defined(CONFIG_ARM64)
204 void enable_caches(void)
206 /* Enable D-cache. I-cache is already enabled in start.S */
211 #if defined(CONFIG_USB_GADGET)
214 #if defined(CONFIG_USB_GADGET_DWC2_OTG)
215 #include <usb/dwc2_udc.h>
217 static struct dwc2_plat_otg_data otg_data = {
223 int board_usb_init(int index, enum usb_init_type init)
227 bool matched = false;
229 /* find the usb_otg node */
230 node = ofnode_by_compatible(ofnode_null(), "snps,dwc2");
231 while (ofnode_valid(node)) {
232 mode = ofnode_read_string(node, "dr_mode");
233 if (mode && strcmp(mode, "otg") == 0) {
238 node = ofnode_by_compatible(node, "snps,dwc2");
241 debug("Not found usb_otg device\n");
244 otg_data.regs_otg = ofnode_get_addr(node);
246 #ifdef CONFIG_ROCKCHIP_USB2_PHY
251 ret = ofnode_read_u32(node, "phys", &phandle);
255 node = ofnode_get_by_phandle(phandle);
256 if (!ofnode_valid(node)) {
257 debug("Not found usb phy device\n");
261 phy_node = ofnode_get_parent(node);
262 if (!ofnode_valid(node)) {
263 debug("Not found usb phy device\n");
267 otg_data.phy_of_node = phy_node;
268 ret = ofnode_read_u32(node, "reg", &offset);
271 otg_data.regs_phy = offset +
272 (u32)syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
274 return dwc2_udc_probe(&otg_data);
277 int board_usb_cleanup(int index, enum usb_init_type init)
281 #endif /* CONFIG_USB_GADGET_DWC2_OTG */
283 #if defined(CONFIG_USB_DWC3_GADGET) && !defined(CONFIG_DM_USB_GADGET)
284 #include <dwc3-uboot.h>
286 static struct dwc3_device dwc3_device_data = {
287 .maximum_speed = USB_SPEED_HIGH,
289 .dr_mode = USB_DR_MODE_PERIPHERAL,
291 .dis_u2_susphy_quirk = 1,
292 .hsphy_mode = USBPHY_INTERFACE_MODE_UTMIW,
295 int usb_gadget_handle_interrupts(int index)
297 dwc3_uboot_handle_interrupt(0);
301 int board_usb_init(int index, enum usb_init_type init)
303 return dwc3_uboot_init(&dwc3_device_data);
305 #endif /* CONFIG_USB_DWC3_GADGET */
307 #endif /* CONFIG_USB_GADGET */
309 #if IS_ENABLED(CONFIG_FASTBOOT)
310 int fastboot_set_reboot_flag(enum fastboot_reboot_reason reason)
312 if (reason != FASTBOOT_REBOOT_REASON_BOOTLOADER)
315 printf("Setting reboot to fastboot flag ...\n");
316 /* Set boot mode to fastboot */
317 writel(BOOT_FASTBOOT, CONFIG_ROCKCHIP_BOOT_MODE_REG);
323 #ifdef CONFIG_MISC_INIT_R
324 __weak int misc_init_r(void)
326 const u32 cpuid_offset = CFG_CPUID_OFFSET;
327 const u32 cpuid_length = 0x10;
328 u8 cpuid[cpuid_length];
331 ret = rockchip_cpuid_from_efuse(cpuid_offset, cpuid_length, cpuid);
335 ret = rockchip_cpuid_set(cpuid, cpuid_length);
339 ret = rockchip_setup_macaddr();