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