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> |
Roman Kovalivskyi | 1bb1342 | 2020-07-28 23:35:32 +0300 | [diff] [blame] | 9 | #include <fastboot.h> |
Simon Glass | a7b5130 | 2019-11-14 12:57:46 -0700 | [diff] [blame] | 10 | #include <init.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 12 | #include <ram.h> |
| 13 | #include <syscon.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 14 | #include <asm/cache.h> |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 15 | #include <asm/io.h> |
| 16 | #include <asm/arch-rockchip/boot_mode.h> |
| 17 | #include <asm/arch-rockchip/clock.h> |
| 18 | #include <asm/arch-rockchip/periph.h> |
Rohan Garg | cfdc192 | 2019-08-12 17:04:34 +0200 | [diff] [blame] | 19 | #include <asm/arch-rockchip/misc.h> |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 20 | #include <power/regulator.h> |
| 21 | |
| 22 | DECLARE_GLOBAL_DATA_PTR; |
| 23 | |
| 24 | __weak int rk_board_late_init(void) |
| 25 | { |
| 26 | return 0; |
| 27 | } |
| 28 | |
| 29 | int board_late_init(void) |
| 30 | { |
| 31 | setup_boot_mode(); |
| 32 | |
| 33 | return rk_board_late_init(); |
| 34 | } |
| 35 | |
| 36 | int board_init(void) |
| 37 | { |
| 38 | int ret; |
| 39 | |
| 40 | #ifdef CONFIG_DM_REGULATOR |
| 41 | ret = regulators_enable_boot_on(false); |
| 42 | if (ret) |
| 43 | debug("%s: Cannot enable boot on regulator\n", __func__); |
| 44 | #endif |
| 45 | |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | #if !defined(CONFIG_SYS_DCACHE_OFF) && !defined(CONFIG_ARM64) |
| 50 | void enable_caches(void) |
| 51 | { |
| 52 | /* Enable D-cache. I-cache is already enabled in start.S */ |
| 53 | dcache_enable(); |
| 54 | } |
| 55 | #endif |
| 56 | |
Jagan Teki | 886ecb2 | 2019-11-19 13:56:22 +0530 | [diff] [blame] | 57 | #if defined(CONFIG_USB_GADGET) |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 58 | #include <usb.h> |
Jagan Teki | 886ecb2 | 2019-11-19 13:56:22 +0530 | [diff] [blame] | 59 | |
| 60 | #if defined(CONFIG_USB_GADGET_DWC2_OTG) |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 61 | #include <usb/dwc2_udc.h> |
| 62 | |
| 63 | static struct dwc2_plat_otg_data otg_data = { |
| 64 | .rx_fifo_sz = 512, |
| 65 | .np_tx_fifo_sz = 16, |
| 66 | .tx_fifo_sz = 128, |
| 67 | }; |
| 68 | |
| 69 | int board_usb_init(int index, enum usb_init_type init) |
| 70 | { |
Kever Yang | 45bda03 | 2019-10-16 17:13:31 +0800 | [diff] [blame] | 71 | ofnode node; |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 72 | const char *mode; |
| 73 | bool matched = false; |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 74 | |
| 75 | /* find the usb_otg node */ |
Kever Yang | 45bda03 | 2019-10-16 17:13:31 +0800 | [diff] [blame] | 76 | node = ofnode_by_compatible(ofnode_null(), "snps,dwc2"); |
| 77 | while (ofnode_valid(node)) { |
| 78 | mode = ofnode_read_string(node, "dr_mode"); |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 79 | if (mode && strcmp(mode, "otg") == 0) { |
| 80 | matched = true; |
| 81 | break; |
| 82 | } |
| 83 | |
Kever Yang | 45bda03 | 2019-10-16 17:13:31 +0800 | [diff] [blame] | 84 | node = ofnode_by_compatible(node, "snps,dwc2"); |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 85 | } |
| 86 | if (!matched) { |
| 87 | debug("Not found usb_otg device\n"); |
| 88 | return -ENODEV; |
| 89 | } |
Kever Yang | 45bda03 | 2019-10-16 17:13:31 +0800 | [diff] [blame] | 90 | otg_data.regs_otg = ofnode_get_addr(node); |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 91 | |
Kever Yang | 6169a0d | 2019-10-16 17:13:32 +0800 | [diff] [blame] | 92 | #ifdef CONFIG_ROCKCHIP_RK3288 |
| 93 | int ret; |
| 94 | u32 phandle, offset; |
| 95 | ofnode phy_node; |
| 96 | |
| 97 | ret = ofnode_read_u32(node, "phys", &phandle); |
| 98 | if (ret) |
| 99 | return ret; |
| 100 | |
| 101 | node = ofnode_get_by_phandle(phandle); |
| 102 | if (!ofnode_valid(node)) { |
| 103 | debug("Not found usb phy device\n"); |
| 104 | return -ENODEV; |
| 105 | } |
| 106 | |
| 107 | phy_node = ofnode_get_parent(node); |
| 108 | if (!ofnode_valid(node)) { |
| 109 | debug("Not found usb phy device\n"); |
| 110 | return -ENODEV; |
| 111 | } |
| 112 | |
| 113 | otg_data.phy_of_node = phy_node; |
| 114 | ret = ofnode_read_u32(node, "reg", &offset); |
| 115 | if (ret) |
| 116 | return ret; |
| 117 | otg_data.regs_phy = offset + |
| 118 | (u32)syscon_get_first_range(ROCKCHIP_SYSCON_GRF); |
| 119 | #endif |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 120 | return dwc2_udc_probe(&otg_data); |
| 121 | } |
| 122 | |
| 123 | int board_usb_cleanup(int index, enum usb_init_type init) |
| 124 | { |
| 125 | return 0; |
| 126 | } |
Jagan Teki | 886ecb2 | 2019-11-19 13:56:22 +0530 | [diff] [blame] | 127 | #endif /* CONFIG_USB_GADGET_DWC2_OTG */ |
| 128 | |
| 129 | #if defined(CONFIG_USB_DWC3_GADGET) && !defined(CONFIG_DM_USB_GADGET) |
| 130 | #include <dwc3-uboot.h> |
| 131 | |
| 132 | static struct dwc3_device dwc3_device_data = { |
| 133 | .maximum_speed = USB_SPEED_HIGH, |
| 134 | .base = 0xfe800000, |
| 135 | .dr_mode = USB_DR_MODE_PERIPHERAL, |
| 136 | .index = 0, |
| 137 | .dis_u2_susphy_quirk = 1, |
| 138 | .hsphy_mode = USBPHY_INTERFACE_MODE_UTMIW, |
| 139 | }; |
| 140 | |
| 141 | int usb_gadget_handle_interrupts(void) |
| 142 | { |
| 143 | dwc3_uboot_handle_interrupt(0); |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | int board_usb_init(int index, enum usb_init_type init) |
| 148 | { |
| 149 | return dwc3_uboot_init(&dwc3_device_data); |
| 150 | } |
| 151 | #endif /* CONFIG_USB_DWC3_GADGET */ |
| 152 | |
| 153 | #endif /* CONFIG_USB_GADGET */ |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 154 | |
| 155 | #if CONFIG_IS_ENABLED(FASTBOOT) |
Roman Kovalivskyi | 1bb1342 | 2020-07-28 23:35:32 +0300 | [diff] [blame] | 156 | int fastboot_set_reboot_flag(enum fastboot_reboot_reason reason) |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 157 | { |
Roman Kovalivskyi | 1bb1342 | 2020-07-28 23:35:32 +0300 | [diff] [blame] | 158 | if (reason != FASTBOOT_REBOOT_REASON_BOOTLOADER) |
| 159 | return -ENOTSUPP; |
| 160 | |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 161 | printf("Setting reboot to fastboot flag ...\n"); |
| 162 | /* Set boot mode to fastboot */ |
| 163 | writel(BOOT_FASTBOOT, CONFIG_ROCKCHIP_BOOT_MODE_REG); |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | #endif |
Rohan Garg | cfdc192 | 2019-08-12 17:04:34 +0200 | [diff] [blame] | 168 | |
| 169 | #ifdef CONFIG_MISC_INIT_R |
| 170 | __weak int misc_init_r(void) |
| 171 | { |
| 172 | const u32 cpuid_offset = 0x7; |
| 173 | const u32 cpuid_length = 0x10; |
| 174 | u8 cpuid[cpuid_length]; |
| 175 | int ret; |
| 176 | |
| 177 | ret = rockchip_cpuid_from_efuse(cpuid_offset, cpuid_length, cpuid); |
| 178 | if (ret) |
| 179 | return ret; |
| 180 | |
| 181 | ret = rockchip_cpuid_set(cpuid, cpuid_length); |
| 182 | if (ret) |
| 183 | return ret; |
| 184 | |
| 185 | ret = rockchip_setup_macaddr(); |
| 186 | |
| 187 | return ret; |
| 188 | } |
| 189 | #endif |