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 | |
| 20 | #include <dt-bindings/input/linux-event-codes.h> |
| 21 | #include <dm/of_access.h> |
| 22 | #include <dm/of.h> |
| 23 | #include <fdt_support.h> |
| 24 | #include <linux/errno.h> |
Caleb Connolly | 4f80e28 | 2024-04-03 14:07:46 +0200 | [diff] [blame] | 25 | #include <stdlib.h> |
Caleb Connolly | 2983ae8 | 2024-04-03 14:07:45 +0200 | [diff] [blame] | 26 | #include <time.h> |
| 27 | |
| 28 | /* U-Boot only supports USB high-speed mode on Qualcomm platforms with DWC3 |
| 29 | * USB controllers. Rather than requiring source level DT changes, we fix up |
| 30 | * DT here. This improves compatibility with upstream DT and simplifies the |
| 31 | * porting process for new devices. |
| 32 | */ |
| 33 | static int fixup_qcom_dwc3(struct device_node *glue_np) |
| 34 | { |
| 35 | struct device_node *dwc3; |
| 36 | int ret, len, hsphy_idx = 1; |
| 37 | const __be32 *phandles; |
| 38 | const char *second_phy_name; |
| 39 | |
| 40 | debug("Fixing up %s\n", glue_np->name); |
| 41 | |
| 42 | /* Tell the glue driver to configure the wrapper for high-speed only operation */ |
| 43 | ret = of_write_prop(glue_np, "qcom,select-utmi-as-pipe-clk", 0, NULL); |
| 44 | if (ret) { |
| 45 | log_err("Failed to add property 'qcom,select-utmi-as-pipe-clk': %d\n", ret); |
| 46 | return ret; |
| 47 | } |
| 48 | |
| 49 | /* Find the DWC3 node itself */ |
| 50 | dwc3 = of_find_compatible_node(glue_np, NULL, "snps,dwc3"); |
| 51 | if (!dwc3) { |
| 52 | log_err("Failed to find dwc3 node\n"); |
| 53 | return -ENOENT; |
| 54 | } |
| 55 | |
| 56 | phandles = of_get_property(dwc3, "phys", &len); |
| 57 | len /= sizeof(*phandles); |
| 58 | if (len == 1) { |
| 59 | log_debug("Only one phy, not a superspeed controller\n"); |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | /* Figure out if the superspeed phy is present and if so then which phy is it? */ |
| 64 | ret = of_property_read_string_index(dwc3, "phy-names", 1, &second_phy_name); |
| 65 | if (ret == -ENODATA) { |
| 66 | log_debug("Only one phy, not a super-speed controller\n"); |
| 67 | return 0; |
| 68 | } else if (ret) { |
| 69 | log_err("Failed to read second phy name: %d\n", ret); |
| 70 | return ret; |
| 71 | } |
| 72 | |
| 73 | if (!strncmp("usb3-phy", second_phy_name, strlen("usb3-phy"))) { |
| 74 | log_debug("Second phy isn't superspeed (is '%s') assuming first phy is SS\n", |
| 75 | second_phy_name); |
| 76 | hsphy_idx = 0; |
| 77 | } |
| 78 | |
| 79 | /* Overwrite the "phys" property to only contain the high-speed phy */ |
| 80 | ret = of_write_prop(dwc3, "phys", sizeof(*phandles), phandles + hsphy_idx); |
| 81 | if (ret) { |
| 82 | log_err("Failed to overwrite 'phys' property: %d\n", ret); |
| 83 | return ret; |
| 84 | } |
| 85 | |
| 86 | /* Overwrite "phy-names" to only contain a single entry */ |
| 87 | ret = of_write_prop(dwc3, "phy-names", strlen("usb2-phy"), "usb2-phy"); |
| 88 | if (ret) { |
| 89 | log_err("Failed to overwrite 'phy-names' property: %d\n", ret); |
| 90 | return ret; |
| 91 | } |
| 92 | |
| 93 | ret = of_write_prop(dwc3, "maximum-speed", strlen("high-speed"), "high-speed"); |
| 94 | if (ret) { |
| 95 | log_err("Failed to set 'maximum-speed' property: %d\n", ret); |
| 96 | return ret; |
| 97 | } |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | static void fixup_usb_nodes(void) |
| 103 | { |
| 104 | struct device_node *glue_np = NULL; |
| 105 | int ret; |
| 106 | |
| 107 | while ((glue_np = of_find_compatible_node(glue_np, NULL, "qcom,dwc3"))) { |
| 108 | ret = fixup_qcom_dwc3(glue_np); |
| 109 | if (ret) |
| 110 | log_warning("Failed to fixup node %s: %d\n", glue_np->name, ret); |
| 111 | } |
| 112 | } |
| 113 | |
Caleb Connolly | 4f80e28 | 2024-04-03 14:07:46 +0200 | [diff] [blame] | 114 | /* Remove all references to the rpmhpd device */ |
| 115 | static void fixup_power_domains(void) |
| 116 | { |
| 117 | struct device_node *pd = NULL, *np = NULL; |
| 118 | struct property *prop; |
| 119 | const __be32 *val; |
| 120 | |
| 121 | /* All Qualcomm platforms name the rpm(h)pd "power-controller" */ |
| 122 | for_each_of_allnodes(pd) { |
| 123 | if (pd->name && !strcmp("power-controller", pd->name)) |
| 124 | break; |
| 125 | } |
| 126 | |
| 127 | /* Sanity check that this is indeed a power domain controller */ |
| 128 | if (!of_find_property(pd, "#power-domain-cells", NULL)) { |
| 129 | log_err("Found power-controller but it doesn't have #power-domain-cells\n"); |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | /* Remove all references to the power domain controller */ |
| 134 | for_each_of_allnodes(np) { |
| 135 | if (!(prop = of_find_property(np, "power-domains", NULL))) |
| 136 | continue; |
| 137 | |
| 138 | val = prop->value; |
| 139 | if (val[0] == cpu_to_fdt32(pd->phandle)) |
| 140 | of_remove_property(np, prop); |
| 141 | } |
| 142 | } |
| 143 | |
Caleb Connolly | 2983ae8 | 2024-04-03 14:07:45 +0200 | [diff] [blame] | 144 | #define time_call(func, ...) \ |
| 145 | do { \ |
| 146 | u64 start = timer_get_us(); \ |
| 147 | func(__VA_ARGS__); \ |
| 148 | debug(#func " took %lluus\n", timer_get_us() - start); \ |
| 149 | } while (0) |
| 150 | |
| 151 | void qcom_of_fixup_nodes(void) |
| 152 | { |
| 153 | time_call(fixup_usb_nodes); |
Caleb Connolly | 4f80e28 | 2024-04-03 14:07:46 +0200 | [diff] [blame] | 154 | time_call(fixup_power_domains); |
Caleb Connolly | 2983ae8 | 2024-04-03 14:07:45 +0200 | [diff] [blame] | 155 | } |