blob: 55368dd43b661d4c14e6cb3ff302b55633e99b71 [file] [log] [blame]
Caleb Connolly2983ae82024-04-03 14:07:45 +02001// 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 Connolly965a98d2024-04-18 18:25:49 +010020#define pr_fmt(fmt) "of_fixup: " fmt
21
Caleb Connolly2983ae82024-04-03 14:07:45 +020022#include <dt-bindings/input/linux-event-codes.h>
23#include <dm/of_access.h>
24#include <dm/of.h>
25#include <fdt_support.h>
26#include <linux/errno.h>
Caleb Connolly4f80e282024-04-03 14:07:46 +020027#include <stdlib.h>
Caleb Connolly2983ae82024-04-03 14:07:45 +020028#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 */
35static int fixup_qcom_dwc3(struct device_node *glue_np)
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
75 if (!strncmp("usb3-phy", second_phy_name, strlen("usb3-phy"))) {
76 log_debug("Second phy isn't superspeed (is '%s') assuming first phy is SS\n",
77 second_phy_name);
78 hsphy_idx = 0;
79 }
80
81 /* Overwrite the "phys" property to only contain the high-speed phy */
82 ret = of_write_prop(dwc3, "phys", sizeof(*phandles), phandles + hsphy_idx);
83 if (ret) {
84 log_err("Failed to overwrite 'phys' property: %d\n", ret);
85 return ret;
86 }
87
88 /* Overwrite "phy-names" to only contain a single entry */
89 ret = of_write_prop(dwc3, "phy-names", strlen("usb2-phy"), "usb2-phy");
90 if (ret) {
91 log_err("Failed to overwrite 'phy-names' property: %d\n", ret);
92 return ret;
93 }
94
95 ret = of_write_prop(dwc3, "maximum-speed", strlen("high-speed"), "high-speed");
96 if (ret) {
97 log_err("Failed to set 'maximum-speed' property: %d\n", ret);
98 return ret;
99 }
100
101 return 0;
102}
103
104static void fixup_usb_nodes(void)
105{
106 struct device_node *glue_np = NULL;
107 int ret;
108
109 while ((glue_np = of_find_compatible_node(glue_np, NULL, "qcom,dwc3"))) {
110 ret = fixup_qcom_dwc3(glue_np);
111 if (ret)
112 log_warning("Failed to fixup node %s: %d\n", glue_np->name, ret);
113 }
114}
115
Caleb Connolly4f80e282024-04-03 14:07:46 +0200116/* Remove all references to the rpmhpd device */
117static void fixup_power_domains(void)
118{
119 struct device_node *pd = NULL, *np = NULL;
120 struct property *prop;
121 const __be32 *val;
122
123 /* All Qualcomm platforms name the rpm(h)pd "power-controller" */
124 for_each_of_allnodes(pd) {
125 if (pd->name && !strcmp("power-controller", pd->name))
126 break;
127 }
128
129 /* Sanity check that this is indeed a power domain controller */
130 if (!of_find_property(pd, "#power-domain-cells", NULL)) {
131 log_err("Found power-controller but it doesn't have #power-domain-cells\n");
132 return;
133 }
134
135 /* Remove all references to the power domain controller */
136 for_each_of_allnodes(np) {
137 if (!(prop = of_find_property(np, "power-domains", NULL)))
138 continue;
139
140 val = prop->value;
141 if (val[0] == cpu_to_fdt32(pd->phandle))
142 of_remove_property(np, prop);
143 }
144}
145
Caleb Connolly2983ae82024-04-03 14:07:45 +0200146#define time_call(func, ...) \
147 do { \
148 u64 start = timer_get_us(); \
149 func(__VA_ARGS__); \
150 debug(#func " took %lluus\n", timer_get_us() - start); \
151 } while (0)
152
153void qcom_of_fixup_nodes(void)
154{
155 time_call(fixup_usb_nodes);
Caleb Connolly4f80e282024-04-03 14:07:46 +0200156 time_call(fixup_power_domains);
Caleb Connolly2983ae82024-04-03 14:07:45 +0200157}
Caleb Connolly965a98d2024-04-18 18:25:49 +0100158
159int ft_board_setup(void *blob, struct bd_info __maybe_unused *bd)
160{
161 struct fdt_header *fdt = blob;
162 int node;
163
164 /* We only want to do this fix-up for the RB1 board, quick return for all others */
165 if (!fdt_node_check_compatible(fdt, 0, "qcom,qrb4210-rb2"))
166 return 0;
167
168 fdt_for_each_node_by_compatible(node, blob, 0, "snps,dwc3") {
169 log_debug("%s: Setting 'dr_mode' to OTG\n", fdt_get_name(blob, node, NULL));
170 fdt_setprop_string(fdt, node, "dr_mode", "otg");
171 break;
172 }
173
174 return 0;
175}