blob: 4fdfed2dff1608143b18ebe45b7775ead6fc5409 [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
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>
25#include <time.h>
26
27/* U-Boot only supports USB high-speed mode on Qualcomm platforms with DWC3
28 * USB controllers. Rather than requiring source level DT changes, we fix up
29 * DT here. This improves compatibility with upstream DT and simplifies the
30 * porting process for new devices.
31 */
32static int fixup_qcom_dwc3(struct device_node *glue_np)
33{
34 struct device_node *dwc3;
35 int ret, len, hsphy_idx = 1;
36 const __be32 *phandles;
37 const char *second_phy_name;
38
39 debug("Fixing up %s\n", glue_np->name);
40
41 /* Tell the glue driver to configure the wrapper for high-speed only operation */
42 ret = of_write_prop(glue_np, "qcom,select-utmi-as-pipe-clk", 0, NULL);
43 if (ret) {
44 log_err("Failed to add property 'qcom,select-utmi-as-pipe-clk': %d\n", ret);
45 return ret;
46 }
47
48 /* Find the DWC3 node itself */
49 dwc3 = of_find_compatible_node(glue_np, NULL, "snps,dwc3");
50 if (!dwc3) {
51 log_err("Failed to find dwc3 node\n");
52 return -ENOENT;
53 }
54
55 phandles = of_get_property(dwc3, "phys", &len);
56 len /= sizeof(*phandles);
57 if (len == 1) {
58 log_debug("Only one phy, not a superspeed controller\n");
59 return 0;
60 }
61
62 /* Figure out if the superspeed phy is present and if so then which phy is it? */
63 ret = of_property_read_string_index(dwc3, "phy-names", 1, &second_phy_name);
64 if (ret == -ENODATA) {
65 log_debug("Only one phy, not a super-speed controller\n");
66 return 0;
67 } else if (ret) {
68 log_err("Failed to read second phy name: %d\n", ret);
69 return ret;
70 }
71
72 if (!strncmp("usb3-phy", second_phy_name, strlen("usb3-phy"))) {
73 log_debug("Second phy isn't superspeed (is '%s') assuming first phy is SS\n",
74 second_phy_name);
75 hsphy_idx = 0;
76 }
77
78 /* Overwrite the "phys" property to only contain the high-speed phy */
79 ret = of_write_prop(dwc3, "phys", sizeof(*phandles), phandles + hsphy_idx);
80 if (ret) {
81 log_err("Failed to overwrite 'phys' property: %d\n", ret);
82 return ret;
83 }
84
85 /* Overwrite "phy-names" to only contain a single entry */
86 ret = of_write_prop(dwc3, "phy-names", strlen("usb2-phy"), "usb2-phy");
87 if (ret) {
88 log_err("Failed to overwrite 'phy-names' property: %d\n", ret);
89 return ret;
90 }
91
92 ret = of_write_prop(dwc3, "maximum-speed", strlen("high-speed"), "high-speed");
93 if (ret) {
94 log_err("Failed to set 'maximum-speed' property: %d\n", ret);
95 return ret;
96 }
97
98 return 0;
99}
100
101static void fixup_usb_nodes(void)
102{
103 struct device_node *glue_np = NULL;
104 int ret;
105
106 while ((glue_np = of_find_compatible_node(glue_np, NULL, "qcom,dwc3"))) {
107 ret = fixup_qcom_dwc3(glue_np);
108 if (ret)
109 log_warning("Failed to fixup node %s: %d\n", glue_np->name, ret);
110 }
111}
112
113#define time_call(func, ...) \
114 do { \
115 u64 start = timer_get_us(); \
116 func(__VA_ARGS__); \
117 debug(#func " took %lluus\n", timer_get_us() - start); \
118 } while (0)
119
120void qcom_of_fixup_nodes(void)
121{
122 time_call(fixup_usb_nodes);
123}