Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 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> |
Simon Glass | 1d91ba7 | 2019-11-14 12:57:37 -0700 | [diff] [blame] | 7 | #include <cpu_func.h> |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 8 | #include <dm.h> |
Sughosh Ganu | c1b8e8b | 2022-11-10 14:49:15 +0530 | [diff] [blame] | 9 | #include <efi_loader.h> |
Roman Kovalivskyi | 1bb1342 | 2020-07-28 23:35:32 +0300 | [diff] [blame] | 10 | #include <fastboot.h> |
Simon Glass | a7b5130 | 2019-11-14 12:57:46 -0700 | [diff] [blame] | 11 | #include <init.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Sughosh Ganu | c1b8e8b | 2022-11-10 14:49:15 +0530 | [diff] [blame] | 13 | #include <mmc.h> |
| 14 | #include <part.h> |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 15 | #include <ram.h> |
| 16 | #include <syscon.h> |
Sughosh Ganu | c1b8e8b | 2022-11-10 14:49:15 +0530 | [diff] [blame] | 17 | #include <uuid.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 18 | #include <asm/cache.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 19 | #include <asm/global_data.h> |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 20 | #include <asm/io.h> |
| 21 | #include <asm/arch-rockchip/boot_mode.h> |
| 22 | #include <asm/arch-rockchip/clock.h> |
| 23 | #include <asm/arch-rockchip/periph.h> |
Rohan Garg | cfdc192 | 2019-08-12 17:04:34 +0200 | [diff] [blame] | 24 | #include <asm/arch-rockchip/misc.h> |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 25 | #include <power/regulator.h> |
| 26 | |
| 27 | DECLARE_GLOBAL_DATA_PTR; |
| 28 | |
Sughosh Ganu | c1b8e8b | 2022-11-10 14:49:15 +0530 | [diff] [blame] | 29 | #if defined(CONFIG_EFI_HAVE_CAPSULE_SUPPORT) && defined(CONFIG_EFI_PARTITION) |
| 30 | |
| 31 | #define DFU_ALT_BUF_LEN SZ_1K |
| 32 | |
| 33 | static struct efi_fw_image *fw_images; |
| 34 | |
| 35 | static bool updatable_image(struct disk_partition *info) |
| 36 | { |
| 37 | int i; |
| 38 | bool ret = false; |
| 39 | efi_guid_t image_type_guid; |
| 40 | |
| 41 | uuid_str_to_bin(info->type_guid, image_type_guid.b, |
| 42 | UUID_STR_FORMAT_GUID); |
| 43 | |
| 44 | for (i = 0; i < num_image_type_guids; i++) { |
| 45 | if (!guidcmp(&fw_images[i].image_type_id, &image_type_guid)) { |
| 46 | ret = true; |
| 47 | break; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | return ret; |
| 52 | } |
| 53 | |
| 54 | static void set_image_index(struct disk_partition *info, int index) |
| 55 | { |
| 56 | int i; |
| 57 | efi_guid_t image_type_guid; |
| 58 | |
| 59 | uuid_str_to_bin(info->type_guid, image_type_guid.b, |
| 60 | UUID_STR_FORMAT_GUID); |
| 61 | |
| 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; |
| 65 | break; |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | static int get_mmc_desc(struct blk_desc **desc) |
| 71 | { |
| 72 | int ret; |
| 73 | struct mmc *mmc; |
| 74 | struct udevice *dev; |
| 75 | |
| 76 | /* |
| 77 | * For now the firmware images are assumed to |
| 78 | * be on the SD card |
| 79 | */ |
| 80 | ret = uclass_get_device(UCLASS_MMC, 1, &dev); |
| 81 | if (ret) |
| 82 | return -1; |
| 83 | |
| 84 | mmc = mmc_get_mmc_dev(dev); |
| 85 | if (!mmc) |
| 86 | return -ENODEV; |
| 87 | |
| 88 | if ((ret = mmc_init(mmc))) |
| 89 | return ret; |
| 90 | |
| 91 | *desc = mmc_get_blk_desc(mmc); |
| 92 | if (!*desc) |
| 93 | return -1; |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | void set_dfu_alt_info(char *interface, char *devstr) |
| 99 | { |
| 100 | const char *name; |
| 101 | bool first = true; |
| 102 | int p, len, devnum, ret; |
| 103 | char buf[DFU_ALT_BUF_LEN]; |
| 104 | struct disk_partition info; |
| 105 | struct blk_desc *desc = NULL; |
| 106 | |
| 107 | ret = get_mmc_desc(&desc); |
| 108 | if (ret) { |
| 109 | log_err("Unable to get mmc desc\n"); |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | memset(buf, 0, sizeof(buf)); |
| 114 | name = blk_get_uclass_name(desc->uclass_id); |
| 115 | devnum = desc->devnum; |
| 116 | len = strlen(buf); |
| 117 | |
| 118 | len += snprintf(buf + len, DFU_ALT_BUF_LEN - len, |
| 119 | "%s %d=", name, devnum); |
| 120 | |
| 121 | for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) { |
| 122 | if (part_get_info(desc, p, &info)) |
| 123 | continue; |
| 124 | |
| 125 | /* Add entry to dfu_alt_info only for updatable images */ |
| 126 | if (updatable_image(&info)) { |
| 127 | if (!first) |
| 128 | len += snprintf(buf + len, |
| 129 | DFU_ALT_BUF_LEN - len, ";"); |
| 130 | |
| 131 | len += snprintf(buf + len, DFU_ALT_BUF_LEN - len, |
| 132 | "%s%d_%s part %d %d", |
| 133 | name, devnum, info.name, devnum, p); |
| 134 | first = false; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | log_debug("dfu_alt_info => %s\n", buf); |
| 139 | env_set("dfu_alt_info", buf); |
| 140 | } |
| 141 | |
| 142 | static void gpt_capsule_update_setup(void) |
| 143 | { |
| 144 | int p, i, ret; |
| 145 | struct disk_partition info; |
| 146 | struct blk_desc *desc = NULL; |
| 147 | |
| 148 | fw_images = update_info.images; |
| 149 | rockchip_capsule_update_board_setup(); |
| 150 | |
| 151 | ret = get_mmc_desc(&desc); |
| 152 | if (ret) { |
| 153 | log_err("Unable to get mmc desc\n"); |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | for (p = 1, i = 1; p <= MAX_SEARCH_PARTITIONS; p++) { |
| 158 | if (part_get_info(desc, p, &info)) |
| 159 | continue; |
| 160 | |
| 161 | /* |
| 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. |
| 165 | */ |
| 166 | if (updatable_image(&info)) { |
| 167 | set_image_index(&info, i); |
| 168 | i++; |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | #endif /* CONFIG_EFI_HAVE_CAPSULE_SUPPORT && CONFIG_EFI_PARTITION */ |
| 173 | |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 174 | __weak int rk_board_late_init(void) |
| 175 | { |
Sughosh Ganu | c1b8e8b | 2022-11-10 14:49:15 +0530 | [diff] [blame] | 176 | #if defined(CONFIG_EFI_HAVE_CAPSULE_SUPPORT) && defined(CONFIG_EFI_PARTITION) |
| 177 | gpt_capsule_update_setup(); |
| 178 | #endif |
| 179 | |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | int board_late_init(void) |
| 184 | { |
| 185 | setup_boot_mode(); |
| 186 | |
| 187 | return rk_board_late_init(); |
| 188 | } |
| 189 | |
| 190 | int board_init(void) |
| 191 | { |
| 192 | int ret; |
| 193 | |
| 194 | #ifdef CONFIG_DM_REGULATOR |
| 195 | ret = regulators_enable_boot_on(false); |
| 196 | if (ret) |
| 197 | debug("%s: Cannot enable boot on regulator\n", __func__); |
| 198 | #endif |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | #if !defined(CONFIG_SYS_DCACHE_OFF) && !defined(CONFIG_ARM64) |
| 204 | void enable_caches(void) |
| 205 | { |
| 206 | /* Enable D-cache. I-cache is already enabled in start.S */ |
| 207 | dcache_enable(); |
| 208 | } |
| 209 | #endif |
| 210 | |
Jagan Teki | 886ecb2 | 2019-11-19 13:56:22 +0530 | [diff] [blame] | 211 | #if defined(CONFIG_USB_GADGET) |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 212 | #include <usb.h> |
Jagan Teki | 886ecb2 | 2019-11-19 13:56:22 +0530 | [diff] [blame] | 213 | |
| 214 | #if defined(CONFIG_USB_GADGET_DWC2_OTG) |
John Keeping | a47bf33 | 2023-04-12 12:52:52 +0100 | [diff] [blame] | 215 | #include <linux/usb/otg.h> |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 216 | #include <usb/dwc2_udc.h> |
| 217 | |
| 218 | static struct dwc2_plat_otg_data otg_data = { |
| 219 | .rx_fifo_sz = 512, |
| 220 | .np_tx_fifo_sz = 16, |
| 221 | .tx_fifo_sz = 128, |
| 222 | }; |
| 223 | |
| 224 | int board_usb_init(int index, enum usb_init_type init) |
| 225 | { |
Kever Yang | 45bda03 | 2019-10-16 17:13:31 +0800 | [diff] [blame] | 226 | ofnode node; |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 227 | bool matched = false; |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 228 | |
| 229 | /* find the usb_otg node */ |
Kever Yang | 45bda03 | 2019-10-16 17:13:31 +0800 | [diff] [blame] | 230 | node = ofnode_by_compatible(ofnode_null(), "snps,dwc2"); |
| 231 | while (ofnode_valid(node)) { |
John Keeping | a47bf33 | 2023-04-12 12:52:52 +0100 | [diff] [blame] | 232 | switch (usb_get_dr_mode(node)) { |
| 233 | case USB_DR_MODE_OTG: |
John Keeping | 3a0269b | 2023-04-12 12:52:53 +0100 | [diff] [blame] | 234 | case USB_DR_MODE_PERIPHERAL: |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 235 | matched = true; |
| 236 | break; |
John Keeping | a47bf33 | 2023-04-12 12:52:52 +0100 | [diff] [blame] | 237 | |
| 238 | default: |
| 239 | break; |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 240 | } |
| 241 | |
John Keeping | a47bf33 | 2023-04-12 12:52:52 +0100 | [diff] [blame] | 242 | if (matched) |
| 243 | break; |
| 244 | |
Kever Yang | 45bda03 | 2019-10-16 17:13:31 +0800 | [diff] [blame] | 245 | node = ofnode_by_compatible(node, "snps,dwc2"); |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 246 | } |
| 247 | if (!matched) { |
| 248 | debug("Not found usb_otg device\n"); |
| 249 | return -ENODEV; |
| 250 | } |
Kever Yang | 45bda03 | 2019-10-16 17:13:31 +0800 | [diff] [blame] | 251 | otg_data.regs_otg = ofnode_get_addr(node); |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 252 | |
Johan Jonker | aa65f3d | 2022-04-29 23:40:07 +0200 | [diff] [blame] | 253 | #ifdef CONFIG_ROCKCHIP_USB2_PHY |
Kever Yang | 6169a0d | 2019-10-16 17:13:32 +0800 | [diff] [blame] | 254 | int ret; |
| 255 | u32 phandle, offset; |
| 256 | ofnode phy_node; |
| 257 | |
| 258 | ret = ofnode_read_u32(node, "phys", &phandle); |
| 259 | if (ret) |
| 260 | return ret; |
| 261 | |
| 262 | node = ofnode_get_by_phandle(phandle); |
| 263 | if (!ofnode_valid(node)) { |
| 264 | debug("Not found usb phy device\n"); |
| 265 | return -ENODEV; |
| 266 | } |
| 267 | |
| 268 | phy_node = ofnode_get_parent(node); |
| 269 | if (!ofnode_valid(node)) { |
| 270 | debug("Not found usb phy device\n"); |
| 271 | return -ENODEV; |
| 272 | } |
| 273 | |
| 274 | otg_data.phy_of_node = phy_node; |
| 275 | ret = ofnode_read_u32(node, "reg", &offset); |
| 276 | if (ret) |
| 277 | return ret; |
| 278 | otg_data.regs_phy = offset + |
| 279 | (u32)syscon_get_first_range(ROCKCHIP_SYSCON_GRF); |
| 280 | #endif |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 281 | return dwc2_udc_probe(&otg_data); |
| 282 | } |
| 283 | |
| 284 | int board_usb_cleanup(int index, enum usb_init_type init) |
| 285 | { |
| 286 | return 0; |
| 287 | } |
Jagan Teki | 886ecb2 | 2019-11-19 13:56:22 +0530 | [diff] [blame] | 288 | #endif /* CONFIG_USB_GADGET_DWC2_OTG */ |
| 289 | |
| 290 | #if defined(CONFIG_USB_DWC3_GADGET) && !defined(CONFIG_DM_USB_GADGET) |
| 291 | #include <dwc3-uboot.h> |
| 292 | |
| 293 | static struct dwc3_device dwc3_device_data = { |
| 294 | .maximum_speed = USB_SPEED_HIGH, |
| 295 | .base = 0xfe800000, |
| 296 | .dr_mode = USB_DR_MODE_PERIPHERAL, |
| 297 | .index = 0, |
| 298 | .dis_u2_susphy_quirk = 1, |
| 299 | .hsphy_mode = USBPHY_INTERFACE_MODE_UTMIW, |
| 300 | }; |
| 301 | |
Marek BehĂșn | 35f307d | 2021-05-20 13:24:18 +0200 | [diff] [blame] | 302 | int usb_gadget_handle_interrupts(int index) |
Jagan Teki | 886ecb2 | 2019-11-19 13:56:22 +0530 | [diff] [blame] | 303 | { |
| 304 | dwc3_uboot_handle_interrupt(0); |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | int board_usb_init(int index, enum usb_init_type init) |
| 309 | { |
| 310 | return dwc3_uboot_init(&dwc3_device_data); |
| 311 | } |
| 312 | #endif /* CONFIG_USB_DWC3_GADGET */ |
| 313 | |
| 314 | #endif /* CONFIG_USB_GADGET */ |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 315 | |
Simon Glass | 96c0d2b | 2023-02-05 17:54:10 -0700 | [diff] [blame] | 316 | #if IS_ENABLED(CONFIG_FASTBOOT) |
Roman Kovalivskyi | 1bb1342 | 2020-07-28 23:35:32 +0300 | [diff] [blame] | 317 | int fastboot_set_reboot_flag(enum fastboot_reboot_reason reason) |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 318 | { |
Roman Kovalivskyi | 1bb1342 | 2020-07-28 23:35:32 +0300 | [diff] [blame] | 319 | if (reason != FASTBOOT_REBOOT_REASON_BOOTLOADER) |
| 320 | return -ENOTSUPP; |
| 321 | |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 322 | printf("Setting reboot to fastboot flag ...\n"); |
| 323 | /* Set boot mode to fastboot */ |
| 324 | writel(BOOT_FASTBOOT, CONFIG_ROCKCHIP_BOOT_MODE_REG); |
| 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | #endif |
Rohan Garg | cfdc192 | 2019-08-12 17:04:34 +0200 | [diff] [blame] | 329 | |
| 330 | #ifdef CONFIG_MISC_INIT_R |
| 331 | __weak int misc_init_r(void) |
| 332 | { |
Jonas Karlman | be56bb5 | 2023-02-22 22:44:41 +0000 | [diff] [blame] | 333 | const u32 cpuid_offset = CFG_CPUID_OFFSET; |
Rohan Garg | cfdc192 | 2019-08-12 17:04:34 +0200 | [diff] [blame] | 334 | const u32 cpuid_length = 0x10; |
| 335 | u8 cpuid[cpuid_length]; |
| 336 | int ret; |
| 337 | |
| 338 | ret = rockchip_cpuid_from_efuse(cpuid_offset, cpuid_length, cpuid); |
| 339 | if (ret) |
| 340 | return ret; |
| 341 | |
| 342 | ret = rockchip_cpuid_set(cpuid, cpuid_length); |
| 343 | if (ret) |
| 344 | return ret; |
| 345 | |
| 346 | ret = rockchip_setup_macaddr(); |
| 347 | |
| 348 | return ret; |
| 349 | } |
| 350 | #endif |