Caleb Connolly | 2983ae8 | 2024-04-03 14:07:45 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * OF_LIVE devicetree fixup. |
| 4 | * |
| 5 | * This file implements runtime fixups for Qualcomm DT to improve |
| 6 | * compatibility with U-Boot. This includes adjusting the USB nodes |
Caleb Connolly | 9dfd2b2 | 2025-04-11 14:47:42 +0200 | [diff] [blame] | 7 | * to only use USB high-speed. |
Caleb Connolly | 2983ae8 | 2024-04-03 14:07:45 +0200 | [diff] [blame] | 8 | * |
| 9 | * We use OF_LIVE for this rather than early FDT fixup for a couple |
| 10 | * of reasons: it has a much nicer API, is most likely more efficient, |
| 11 | * and our changes are only applied to U-Boot. This allows us to use a |
| 12 | * DT designed for Linux, run U-Boot with a modified version, and then |
| 13 | * boot Linux with the original FDT. |
| 14 | * |
| 15 | * Copyright (c) 2024 Linaro Ltd. |
| 16 | * Author: Caleb Connolly <caleb.connolly@linaro.org> |
| 17 | */ |
| 18 | |
Caleb Connolly | 965a98d | 2024-04-18 18:25:49 +0100 | [diff] [blame] | 19 | #define pr_fmt(fmt) "of_fixup: " fmt |
| 20 | |
Caleb Connolly | 2983ae8 | 2024-04-03 14:07:45 +0200 | [diff] [blame] | 21 | #include <dt-bindings/input/linux-event-codes.h> |
| 22 | #include <dm/of_access.h> |
| 23 | #include <dm/of.h> |
Caleb Connolly | c60b3f4 | 2025-04-11 14:47:39 +0200 | [diff] [blame] | 24 | #include <event.h> |
Caleb Connolly | 2983ae8 | 2024-04-03 14:07:45 +0200 | [diff] [blame] | 25 | #include <fdt_support.h> |
| 26 | #include <linux/errno.h> |
Caleb Connolly | 4f80e28 | 2024-04-03 14:07:46 +0200 | [diff] [blame] | 27 | #include <stdlib.h> |
Caleb Connolly | 2983ae8 | 2024-04-03 14:07:45 +0200 | [diff] [blame] | 28 | #include <time.h> |
| 29 | |
| 30 | /* U-Boot only supports USB high-speed mode on Qualcomm platforms with DWC3 |
| 31 | * USB controllers. Rather than requiring source level DT changes, we fix up |
| 32 | * DT here. This improves compatibility with upstream DT and simplifies the |
| 33 | * porting process for new devices. |
| 34 | */ |
Caleb Connolly | c60b3f4 | 2025-04-11 14:47:39 +0200 | [diff] [blame] | 35 | static int fixup_qcom_dwc3(struct device_node *root, struct device_node *glue_np) |
Caleb Connolly | 2983ae8 | 2024-04-03 14:07:45 +0200 | [diff] [blame] | 36 | { |
| 37 | struct device_node *dwc3; |
| 38 | int ret, len, hsphy_idx = 1; |
| 39 | const __be32 *phandles; |
| 40 | const char *second_phy_name; |
| 41 | |
| 42 | debug("Fixing up %s\n", glue_np->name); |
| 43 | |
| 44 | /* Tell the glue driver to configure the wrapper for high-speed only operation */ |
| 45 | ret = of_write_prop(glue_np, "qcom,select-utmi-as-pipe-clk", 0, NULL); |
| 46 | if (ret) { |
| 47 | log_err("Failed to add property 'qcom,select-utmi-as-pipe-clk': %d\n", ret); |
| 48 | return ret; |
| 49 | } |
| 50 | |
| 51 | /* Find the DWC3 node itself */ |
| 52 | dwc3 = of_find_compatible_node(glue_np, NULL, "snps,dwc3"); |
| 53 | if (!dwc3) { |
| 54 | log_err("Failed to find dwc3 node\n"); |
| 55 | return -ENOENT; |
| 56 | } |
| 57 | |
| 58 | phandles = of_get_property(dwc3, "phys", &len); |
| 59 | len /= sizeof(*phandles); |
| 60 | if (len == 1) { |
| 61 | log_debug("Only one phy, not a superspeed controller\n"); |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | /* Figure out if the superspeed phy is present and if so then which phy is it? */ |
| 66 | ret = of_property_read_string_index(dwc3, "phy-names", 1, &second_phy_name); |
| 67 | if (ret == -ENODATA) { |
| 68 | log_debug("Only one phy, not a super-speed controller\n"); |
| 69 | return 0; |
| 70 | } else if (ret) { |
| 71 | log_err("Failed to read second phy name: %d\n", ret); |
| 72 | return ret; |
| 73 | } |
| 74 | |
Caleb Connolly | 0e54dc9 | 2025-04-11 14:47:41 +0200 | [diff] [blame] | 75 | /* |
| 76 | * Determine which phy is the superspeed phy by checking the name of the second phy |
| 77 | * since it is typically the superspeed one. |
| 78 | */ |
| 79 | if (!strncmp("usb3-phy", second_phy_name, strlen("usb3-phy"))) |
Caleb Connolly | 2983ae8 | 2024-04-03 14:07:45 +0200 | [diff] [blame] | 80 | hsphy_idx = 0; |
Caleb Connolly | 2983ae8 | 2024-04-03 14:07:45 +0200 | [diff] [blame] | 81 | |
| 82 | /* Overwrite the "phys" property to only contain the high-speed phy */ |
| 83 | ret = of_write_prop(dwc3, "phys", sizeof(*phandles), phandles + hsphy_idx); |
| 84 | if (ret) { |
| 85 | log_err("Failed to overwrite 'phys' property: %d\n", ret); |
| 86 | return ret; |
| 87 | } |
| 88 | |
| 89 | /* Overwrite "phy-names" to only contain a single entry */ |
Rui Miguel Silva | 3e5447f | 2025-02-27 09:45:49 +0000 | [diff] [blame] | 90 | ret = of_write_prop(dwc3, "phy-names", strlen("usb2-phy") + 1, "usb2-phy"); |
Caleb Connolly | 2983ae8 | 2024-04-03 14:07:45 +0200 | [diff] [blame] | 91 | if (ret) { |
| 92 | log_err("Failed to overwrite 'phy-names' property: %d\n", ret); |
| 93 | return ret; |
| 94 | } |
| 95 | |
Rui Miguel Silva | 3e5447f | 2025-02-27 09:45:49 +0000 | [diff] [blame] | 96 | ret = of_write_prop(dwc3, "maximum-speed", strlen("high-speed") + 1, "high-speed"); |
Caleb Connolly | 2983ae8 | 2024-04-03 14:07:45 +0200 | [diff] [blame] | 97 | if (ret) { |
| 98 | log_err("Failed to set 'maximum-speed' property: %d\n", ret); |
| 99 | return ret; |
| 100 | } |
| 101 | |
Caleb Connolly | 72b91cb | 2025-04-11 14:47:43 +0200 | [diff] [blame] | 102 | /* |
| 103 | * The RB1/2 boards only have a single USB controller and it's muxed between the type-C port |
| 104 | * and a USB hub. Since we can't do OTG in U-Boot properly we prefer to put it into host mode. |
| 105 | */ |
| 106 | if (of_device_is_compatible(root, "qcom,qrb4210-rb2", NULL, NULL) || |
| 107 | of_device_is_compatible(root, "qcom,qrb2210-rb1", NULL, NULL)) { |
| 108 | ret = of_write_prop(dwc3, "dr_mode", sizeof("host"), "host"); |
| 109 | if (ret) { |
| 110 | log_err("Failed to set 'dr_mode' property: %d\n", ret); |
| 111 | return ret; |
| 112 | } |
| 113 | } |
| 114 | |
Caleb Connolly | 2983ae8 | 2024-04-03 14:07:45 +0200 | [diff] [blame] | 115 | return 0; |
| 116 | } |
| 117 | |
Caleb Connolly | c60b3f4 | 2025-04-11 14:47:39 +0200 | [diff] [blame] | 118 | static void fixup_usb_nodes(struct device_node *root) |
Caleb Connolly | 2983ae8 | 2024-04-03 14:07:45 +0200 | [diff] [blame] | 119 | { |
Caleb Connolly | c60b3f4 | 2025-04-11 14:47:39 +0200 | [diff] [blame] | 120 | struct device_node *glue_np = root; |
Caleb Connolly | 2983ae8 | 2024-04-03 14:07:45 +0200 | [diff] [blame] | 121 | int ret; |
| 122 | |
| 123 | while ((glue_np = of_find_compatible_node(glue_np, NULL, "qcom,dwc3"))) { |
Caleb Connolly | a3926f8 | 2025-04-11 14:47:40 +0200 | [diff] [blame] | 124 | if (!of_device_is_available(glue_np)) |
| 125 | continue; |
| 126 | ret = fixup_qcom_dwc3(root, glue_np); |
Caleb Connolly | 2983ae8 | 2024-04-03 14:07:45 +0200 | [diff] [blame] | 127 | if (ret) |
| 128 | log_warning("Failed to fixup node %s: %d\n", glue_np->name, ret); |
| 129 | } |
| 130 | } |
| 131 | |
Caleb Connolly | 4f80e28 | 2024-04-03 14:07:46 +0200 | [diff] [blame] | 132 | /* Remove all references to the rpmhpd device */ |
Caleb Connolly | c60b3f4 | 2025-04-11 14:47:39 +0200 | [diff] [blame] | 133 | static void fixup_power_domains(struct device_node *root) |
Caleb Connolly | 4f80e28 | 2024-04-03 14:07:46 +0200 | [diff] [blame] | 134 | { |
| 135 | struct device_node *pd = NULL, *np = NULL; |
| 136 | struct property *prop; |
| 137 | const __be32 *val; |
| 138 | |
| 139 | /* All Qualcomm platforms name the rpm(h)pd "power-controller" */ |
Caleb Connolly | c60b3f4 | 2025-04-11 14:47:39 +0200 | [diff] [blame] | 140 | for_each_of_allnodes_from(root, pd) { |
Caleb Connolly | 4f80e28 | 2024-04-03 14:07:46 +0200 | [diff] [blame] | 141 | if (pd->name && !strcmp("power-controller", pd->name)) |
| 142 | break; |
| 143 | } |
| 144 | |
| 145 | /* Sanity check that this is indeed a power domain controller */ |
| 146 | if (!of_find_property(pd, "#power-domain-cells", NULL)) { |
| 147 | log_err("Found power-controller but it doesn't have #power-domain-cells\n"); |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | /* Remove all references to the power domain controller */ |
Caleb Connolly | c60b3f4 | 2025-04-11 14:47:39 +0200 | [diff] [blame] | 152 | for_each_of_allnodes_from(root, np) { |
Caleb Connolly | 4f80e28 | 2024-04-03 14:07:46 +0200 | [diff] [blame] | 153 | if (!(prop = of_find_property(np, "power-domains", NULL))) |
| 154 | continue; |
| 155 | |
| 156 | val = prop->value; |
| 157 | if (val[0] == cpu_to_fdt32(pd->phandle)) |
| 158 | of_remove_property(np, prop); |
| 159 | } |
| 160 | } |
| 161 | |
Caleb Connolly | 2983ae8 | 2024-04-03 14:07:45 +0200 | [diff] [blame] | 162 | #define time_call(func, ...) \ |
| 163 | do { \ |
| 164 | u64 start = timer_get_us(); \ |
| 165 | func(__VA_ARGS__); \ |
| 166 | debug(#func " took %lluus\n", timer_get_us() - start); \ |
| 167 | } while (0) |
| 168 | |
Caleb Connolly | c60b3f4 | 2025-04-11 14:47:39 +0200 | [diff] [blame] | 169 | static int qcom_of_fixup_nodes(void * __maybe_unused ctx, struct event *event) |
Caleb Connolly | 2983ae8 | 2024-04-03 14:07:45 +0200 | [diff] [blame] | 170 | { |
Caleb Connolly | c60b3f4 | 2025-04-11 14:47:39 +0200 | [diff] [blame] | 171 | struct device_node *root = event->data.of_live_built.root; |
| 172 | |
| 173 | time_call(fixup_usb_nodes, root); |
| 174 | time_call(fixup_power_domains, root); |
| 175 | |
| 176 | return 0; |
Caleb Connolly | 2983ae8 | 2024-04-03 14:07:45 +0200 | [diff] [blame] | 177 | } |
Caleb Connolly | 965a98d | 2024-04-18 18:25:49 +0100 | [diff] [blame] | 178 | |
Caleb Connolly | c60b3f4 | 2025-04-11 14:47:39 +0200 | [diff] [blame] | 179 | EVENT_SPY_FULL(EVT_OF_LIVE_BUILT, qcom_of_fixup_nodes); |
| 180 | |
Caleb Connolly | 72b91cb | 2025-04-11 14:47:43 +0200 | [diff] [blame] | 181 | int ft_board_setup(void __maybe_unused *blob, struct bd_info __maybe_unused *bd) |
Caleb Connolly | 965a98d | 2024-04-18 18:25:49 +0100 | [diff] [blame] | 182 | { |
Caleb Connolly | 965a98d | 2024-04-18 18:25:49 +0100 | [diff] [blame] | 183 | return 0; |
| 184 | } |