e6a240dbdae9ead4dcc737ae575fc8304eff8fb8
[openwrt/staging/svanheule.git] /
1 From bdce82e960d1205d118662f575cec39379984e34 Mon Sep 17 00:00:00 2001
2 From: Christian Marangi <ansuelsmth@gmail.com>
3 Date: Wed, 31 Jan 2024 03:26:04 +0100
4 Subject: [PATCH] net: mdio: ipq4019: add support for clock-frequency property
5
6 The IPQ4019 MDIO internally divide the clock feed by AHB based on the
7 MDIO_MODE reg. On reset or power up, the default value for the
8 divider is 0xff that reflect the divider set to /256.
9
10 This makes the MDC run at a very low rate, that is, considering AHB is
11 always fixed to 100Mhz, a value of 390KHz.
12
13 This hasn't have been a problem as MDIO wasn't used for time sensitive
14 operation, it is now that on IPQ807x is usually mounted with PHY that
15 requires MDIO to load their firmware (example Aquantia PHY).
16
17 To handle this problem and permit to set the correct designed MDC
18 frequency for the SoC add support for the standard "clock-frequency"
19 property for the MDIO node.
20
21 The divider supports value from /1 to /256 and the common value are to
22 set it to /16 to reflect 6.25Mhz or to /8 on newer platform to reflect
23 12.5Mhz.
24
25 To scan if the requested rate is supported by the divider, loop with
26 each supported divider and stop when the requested rate match the final
27 rate with the current divider. An error is returned if the rate doesn't
28 match any value.
29
30 On MDIO reset, the divider is restored to the requested value to prevent
31 any kind of downclocking caused by the divider reverting to a default
32 value.
33
34 To follow 802.3 spec of 2.5MHz of default value, if divider is set at
35 /256 and "clock-frequency" is not set in DT, assume nobody set the
36 divider and try to find the closest MDC rate to 2.5MHz. (in the case of
37 AHB set to 100MHz, it's 1.5625MHz)
38
39 While at is also document other bits of the MDIO_MODE reg to have a
40 clear idea of what is actually applied there.
41
42 Documentation of some BITs is skipped as they are marked as reserved and
43 their usage is not clear (RES 11:9 GENPHY 16:13 RES1 19:17)
44
45 Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
46 Reviewed-by: Andrew Lunn <andrew@lunn.ch>
47 Signed-off-by: David S. Miller <davem@davemloft.net>
48 ---
49 drivers/net/mdio/mdio-ipq4019.c | 109 ++++++++++++++++++++++++++++++--
50 1 file changed, 103 insertions(+), 6 deletions(-)
51
52 --- a/drivers/net/mdio/mdio-ipq4019.c
53 +++ b/drivers/net/mdio/mdio-ipq4019.c
54 @@ -14,6 +14,20 @@
55 #include <linux/clk.h>
56
57 #define MDIO_MODE_REG 0x40
58 +#define MDIO_MODE_MDC_MODE BIT(12)
59 +/* 0 = Clause 22, 1 = Clause 45 */
60 +#define MDIO_MODE_C45 BIT(8)
61 +#define MDIO_MODE_DIV_MASK GENMASK(7, 0)
62 +#define MDIO_MODE_DIV(x) FIELD_PREP(MDIO_MODE_DIV_MASK, (x) - 1)
63 +#define MDIO_MODE_DIV_1 0x0
64 +#define MDIO_MODE_DIV_2 0x1
65 +#define MDIO_MODE_DIV_4 0x3
66 +#define MDIO_MODE_DIV_8 0x7
67 +#define MDIO_MODE_DIV_16 0xf
68 +#define MDIO_MODE_DIV_32 0x1f
69 +#define MDIO_MODE_DIV_64 0x3f
70 +#define MDIO_MODE_DIV_128 0x7f
71 +#define MDIO_MODE_DIV_256 0xff
72 #define MDIO_ADDR_REG 0x44
73 #define MDIO_DATA_WRITE_REG 0x48
74 #define MDIO_DATA_READ_REG 0x4c
75 @@ -26,9 +40,6 @@
76 #define MDIO_CMD_ACCESS_CODE_C45_WRITE 1
77 #define MDIO_CMD_ACCESS_CODE_C45_READ 2
78
79 -/* 0 = Clause 22, 1 = Clause 45 */
80 -#define MDIO_MODE_C45 BIT(8)
81 -
82 #define IPQ4019_MDIO_TIMEOUT 10000
83 #define IPQ4019_MDIO_SLEEP 10
84
85 @@ -41,6 +52,7 @@ struct ipq4019_mdio_data {
86 void __iomem *membase;
87 void __iomem *eth_ldo_rdy;
88 struct clk *mdio_clk;
89 + unsigned int mdc_rate;
90 };
91
92 static int ipq4019_mdio_wait_busy(struct mii_bus *bus)
93 @@ -179,6 +191,38 @@ static int ipq4019_mdio_write(struct mii
94 return 0;
95 }
96
97 +static int ipq4019_mdio_set_div(struct ipq4019_mdio_data *priv)
98 +{
99 + unsigned long ahb_rate;
100 + int div;
101 + u32 val;
102 +
103 + /* If we don't have a clock for AHB use the fixed value */
104 + ahb_rate = IPQ_MDIO_CLK_RATE;
105 + if (priv->mdio_clk)
106 + ahb_rate = clk_get_rate(priv->mdio_clk);
107 +
108 + /* MDC rate is ahb_rate/(MDIO_MODE_DIV + 1)
109 + * While supported, internal documentation doesn't
110 + * assure correct functionality of the MDIO bus
111 + * with divider of 1, 2 or 4.
112 + */
113 + for (div = 8; div <= 256; div *= 2) {
114 + /* The requested rate is supported by the div */
115 + if (priv->mdc_rate == DIV_ROUND_UP(ahb_rate, div)) {
116 + val = readl(priv->membase + MDIO_MODE_REG);
117 + val &= ~MDIO_MODE_DIV_MASK;
118 + val |= MDIO_MODE_DIV(div);
119 + writel(val, priv->membase + MDIO_MODE_REG);
120 +
121 + return 0;
122 + }
123 + }
124 +
125 + /* The requested rate is not supported */
126 + return -EINVAL;
127 +}
128 +
129 static int ipq_mdio_reset(struct mii_bus *bus)
130 {
131 struct ipq4019_mdio_data *priv = bus->priv;
132 @@ -201,10 +245,58 @@ static int ipq_mdio_reset(struct mii_bus
133 return ret;
134
135 ret = clk_prepare_enable(priv->mdio_clk);
136 - if (ret == 0)
137 - mdelay(10);
138 + if (ret)
139 + return ret;
140 +
141 + mdelay(10);
142
143 - return ret;
144 + /* Restore MDC rate */
145 + return ipq4019_mdio_set_div(priv);
146 +}
147 +
148 +static void ipq4019_mdio_select_mdc_rate(struct platform_device *pdev,
149 + struct ipq4019_mdio_data *priv)
150 +{
151 + unsigned long ahb_rate;
152 + int div;
153 + u32 val;
154 +
155 + /* MDC rate defined in DT, we don't have to decide a default value */
156 + if (!of_property_read_u32(pdev->dev.of_node, "clock-frequency",
157 + &priv->mdc_rate))
158 + return;
159 +
160 + /* If we don't have a clock for AHB use the fixed value */
161 + ahb_rate = IPQ_MDIO_CLK_RATE;
162 + if (priv->mdio_clk)
163 + ahb_rate = clk_get_rate(priv->mdio_clk);
164 +
165 + /* Check what is the current div set */
166 + val = readl(priv->membase + MDIO_MODE_REG);
167 + div = FIELD_GET(MDIO_MODE_DIV_MASK, val);
168 +
169 + /* div is not set to the default value of /256
170 + * Probably someone changed that (bootloader, other drivers)
171 + * Keep this and don't overwrite it.
172 + */
173 + if (div != MDIO_MODE_DIV_256) {
174 + priv->mdc_rate = DIV_ROUND_UP(ahb_rate, div + 1);
175 + return;
176 + }
177 +
178 + /* If div is /256 assume nobody have set this value and
179 + * try to find one MDC rate that is close the 802.3 spec of
180 + * 2.5MHz
181 + */
182 + for (div = 256; div >= 8; div /= 2) {
183 + /* Stop as soon as we found a divider that
184 + * reached the closest value to 2.5MHz
185 + */
186 + if (DIV_ROUND_UP(ahb_rate, div) > 2500000)
187 + break;
188 +
189 + priv->mdc_rate = DIV_ROUND_UP(ahb_rate, div);
190 + }
191 }
192
193 static int ipq4019_mdio_probe(struct platform_device *pdev)
194 @@ -228,6 +320,11 @@ static int ipq4019_mdio_probe(struct pla
195 if (IS_ERR(priv->mdio_clk))
196 return PTR_ERR(priv->mdio_clk);
197
198 + ipq4019_mdio_select_mdc_rate(pdev, priv);
199 + ret = ipq4019_mdio_set_div(priv);
200 + if (ret)
201 + return ret;
202 +
203 /* The platform resource is provided on the chipset IPQ5018 */
204 /* This resource is optional */
205 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);