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