1 // SPDX-License-Identifier: GPL-2.0-only
3 * Amlogic Meson8b, Meson8m2 and GXBB DWMAC glue layer
5 * Copyright (C) 2016 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
8 #include <linux/bitfield.h>
10 #include <linux/clk-provider.h>
11 #include <linux/device.h>
12 #include <linux/ethtool.h>
14 #include <linux/ioport.h>
15 #include <linux/module.h>
16 #include <linux/of_device.h>
17 #include <linux/of_net.h>
18 #include <linux/mfd/syscon.h>
19 #include <linux/platform_device.h>
20 #include <linux/stmmac.h>
22 #include "stmmac_platform.h"
26 #define PRG_ETH0_RGMII_MODE BIT(0)
28 #define PRG_ETH0_EXT_PHY_MODE_MASK GENMASK(2, 0)
29 #define PRG_ETH0_EXT_RGMII_MODE 1
30 #define PRG_ETH0_EXT_RMII_MODE 4
32 /* mux to choose between fclk_div2 (bit unset) and mpll2 (bit set) */
33 #define PRG_ETH0_CLK_M250_SEL_SHIFT 4
34 #define PRG_ETH0_CLK_M250_SEL_MASK GENMASK(4, 4)
36 /* TX clock delay in ns = "8ns / 4 * tx_dly_val" (where 8ns are exactly one
37 * cycle of the 125MHz RGMII TX clock):
38 * 0ns = 0x0, 2ns = 0x1, 4ns = 0x2, 6ns = 0x3
40 #define PRG_ETH0_TXDLY_MASK GENMASK(6, 5)
42 /* divider for the result of m250_sel */
43 #define PRG_ETH0_CLK_M250_DIV_SHIFT 7
44 #define PRG_ETH0_CLK_M250_DIV_WIDTH 3
46 #define PRG_ETH0_RGMII_TX_CLK_EN 10
48 #define PRG_ETH0_INVERTED_RMII_CLK BIT(11)
49 #define PRG_ETH0_TX_AND_PHY_REF_CLK BIT(12)
51 /* Bypass (= 0, the signal from the GPIO input directly connects to the
52 * internal sampling) or enable (= 1) the internal logic for RXEN and RXD[3:0]
55 #define PRG_ETH0_ADJ_ENABLE BIT(13)
56 /* Controls whether the RXEN and RXD[3:0] signals should be aligned with the
57 * input RX rising/falling edge and sent to the Ethernet internals. This sets
58 * the automatically delay and skew automatically (internally).
60 #define PRG_ETH0_ADJ_SETUP BIT(14)
61 /* An internal counter based on the "timing-adjustment" clock. The counter is
62 * cleared on both, the falling and rising edge of the RX_CLK. This selects the
63 * delay (= the counter value) when to start sampling RXEN and RXD[3:0].
65 #define PRG_ETH0_ADJ_DELAY GENMASK(19, 15)
66 /* Adjusts the skew between each bit of RXEN and RXD[3:0]. If a signal has a
67 * large input delay, the bit for that signal (RXEN = bit 0, RXD[3] = bit 1,
68 * ...) can be configured to be 1 to compensate for a delay of about 1ns.
70 #define PRG_ETH0_ADJ_SKEW GENMASK(24, 20)
72 #define MUX_CLK_NUM_PARENTS 2
76 struct meson8b_dwmac_data {
77 int (*set_phy_mode)(struct meson8b_dwmac *dwmac);
80 struct meson8b_dwmac {
84 const struct meson8b_dwmac_data *data;
85 phy_interface_t phy_mode;
86 struct clk *rgmii_tx_clk;
89 struct clk *timing_adj_clk;
92 struct meson8b_dwmac_clk_configs {
93 struct clk_mux m250_mux;
94 struct clk_divider m250_div;
95 struct clk_fixed_factor fixed_div2;
96 struct clk_gate rgmii_tx_en;
99 static void meson8b_dwmac_mask_bits(struct meson8b_dwmac *dwmac, u32 reg,
104 data = readl(dwmac->regs + reg);
106 data |= (value & mask);
108 writel(data, dwmac->regs + reg);
111 static struct clk *meson8b_dwmac_register_clk(struct meson8b_dwmac *dwmac,
112 const char *name_suffix,
113 const char **parent_names,
115 const struct clk_ops *ops,
118 struct clk_init_data init;
121 snprintf(clk_name, sizeof(clk_name), "%s#%s", dev_name(dwmac->dev),
124 init.name = clk_name;
126 init.flags = CLK_SET_RATE_PARENT;
127 init.parent_names = parent_names;
128 init.num_parents = num_parents;
132 return devm_clk_register(dwmac->dev, hw);
135 static int meson8b_init_rgmii_tx_clk(struct meson8b_dwmac *dwmac)
139 struct device *dev = dwmac->dev;
140 const char *parent_name, *mux_parent_names[MUX_CLK_NUM_PARENTS];
141 struct meson8b_dwmac_clk_configs *clk_configs;
142 static const struct clk_div_table div_table[] = {
143 { .div = 2, .val = 2, },
144 { .div = 3, .val = 3, },
145 { .div = 4, .val = 4, },
146 { .div = 5, .val = 5, },
147 { .div = 6, .val = 6, },
148 { .div = 7, .val = 7, },
149 { /* end of array */ }
152 clk_configs = devm_kzalloc(dev, sizeof(*clk_configs), GFP_KERNEL);
156 /* get the mux parents from DT */
157 for (i = 0; i < MUX_CLK_NUM_PARENTS; i++) {
160 snprintf(name, sizeof(name), "clkin%d", i);
161 clk = devm_clk_get(dev, name);
164 if (ret != -EPROBE_DEFER)
165 dev_err(dev, "Missing clock %s\n", name);
169 mux_parent_names[i] = __clk_get_name(clk);
172 clk_configs->m250_mux.reg = dwmac->regs + PRG_ETH0;
173 clk_configs->m250_mux.shift = PRG_ETH0_CLK_M250_SEL_SHIFT;
174 clk_configs->m250_mux.mask = PRG_ETH0_CLK_M250_SEL_MASK;
175 clk = meson8b_dwmac_register_clk(dwmac, "m250_sel", mux_parent_names,
176 MUX_CLK_NUM_PARENTS, &clk_mux_ops,
177 &clk_configs->m250_mux.hw);
178 if (WARN_ON(IS_ERR(clk)))
181 parent_name = __clk_get_name(clk);
182 clk_configs->m250_div.reg = dwmac->regs + PRG_ETH0;
183 clk_configs->m250_div.shift = PRG_ETH0_CLK_M250_DIV_SHIFT;
184 clk_configs->m250_div.width = PRG_ETH0_CLK_M250_DIV_WIDTH;
185 clk_configs->m250_div.table = div_table;
186 clk_configs->m250_div.flags = CLK_DIVIDER_ALLOW_ZERO |
187 CLK_DIVIDER_ROUND_CLOSEST;
188 clk = meson8b_dwmac_register_clk(dwmac, "m250_div", &parent_name, 1,
190 &clk_configs->m250_div.hw);
191 if (WARN_ON(IS_ERR(clk)))
194 parent_name = __clk_get_name(clk);
195 clk_configs->fixed_div2.mult = 1;
196 clk_configs->fixed_div2.div = 2;
197 clk = meson8b_dwmac_register_clk(dwmac, "fixed_div2", &parent_name, 1,
198 &clk_fixed_factor_ops,
199 &clk_configs->fixed_div2.hw);
200 if (WARN_ON(IS_ERR(clk)))
203 parent_name = __clk_get_name(clk);
204 clk_configs->rgmii_tx_en.reg = dwmac->regs + PRG_ETH0;
205 clk_configs->rgmii_tx_en.bit_idx = PRG_ETH0_RGMII_TX_CLK_EN;
206 clk = meson8b_dwmac_register_clk(dwmac, "rgmii_tx_en", &parent_name, 1,
208 &clk_configs->rgmii_tx_en.hw);
209 if (WARN_ON(IS_ERR(clk)))
212 dwmac->rgmii_tx_clk = clk;
217 static int meson8b_set_phy_mode(struct meson8b_dwmac *dwmac)
219 switch (dwmac->phy_mode) {
220 case PHY_INTERFACE_MODE_RGMII:
221 case PHY_INTERFACE_MODE_RGMII_RXID:
222 case PHY_INTERFACE_MODE_RGMII_ID:
223 case PHY_INTERFACE_MODE_RGMII_TXID:
224 /* enable RGMII mode */
225 meson8b_dwmac_mask_bits(dwmac, PRG_ETH0,
227 PRG_ETH0_RGMII_MODE);
229 case PHY_INTERFACE_MODE_RMII:
230 /* disable RGMII mode -> enables RMII mode */
231 meson8b_dwmac_mask_bits(dwmac, PRG_ETH0,
232 PRG_ETH0_RGMII_MODE, 0);
235 dev_err(dwmac->dev, "fail to set phy-mode %s\n",
236 phy_modes(dwmac->phy_mode));
243 static int meson_axg_set_phy_mode(struct meson8b_dwmac *dwmac)
245 switch (dwmac->phy_mode) {
246 case PHY_INTERFACE_MODE_RGMII:
247 case PHY_INTERFACE_MODE_RGMII_RXID:
248 case PHY_INTERFACE_MODE_RGMII_ID:
249 case PHY_INTERFACE_MODE_RGMII_TXID:
250 /* enable RGMII mode */
251 meson8b_dwmac_mask_bits(dwmac, PRG_ETH0,
252 PRG_ETH0_EXT_PHY_MODE_MASK,
253 PRG_ETH0_EXT_RGMII_MODE);
255 case PHY_INTERFACE_MODE_RMII:
256 /* disable RGMII mode -> enables RMII mode */
257 meson8b_dwmac_mask_bits(dwmac, PRG_ETH0,
258 PRG_ETH0_EXT_PHY_MODE_MASK,
259 PRG_ETH0_EXT_RMII_MODE);
262 dev_err(dwmac->dev, "fail to set phy-mode %s\n",
263 phy_modes(dwmac->phy_mode));
270 static int meson8b_devm_clk_prepare_enable(struct meson8b_dwmac *dwmac,
275 ret = clk_prepare_enable(clk);
279 devm_add_action_or_reset(dwmac->dev,
280 (void(*)(void *))clk_disable_unprepare,
281 dwmac->rgmii_tx_clk);
286 static int meson8b_init_prg_eth(struct meson8b_dwmac *dwmac)
288 u32 tx_dly_config, rx_dly_config, delay_config;
291 tx_dly_config = FIELD_PREP(PRG_ETH0_TXDLY_MASK,
292 dwmac->tx_delay_ns >> 1);
294 if (dwmac->rx_delay_ns == 2)
295 rx_dly_config = PRG_ETH0_ADJ_ENABLE | PRG_ETH0_ADJ_SETUP;
299 switch (dwmac->phy_mode) {
300 case PHY_INTERFACE_MODE_RGMII:
301 delay_config = tx_dly_config | rx_dly_config;
303 case PHY_INTERFACE_MODE_RGMII_RXID:
304 delay_config = tx_dly_config;
306 case PHY_INTERFACE_MODE_RGMII_TXID:
307 delay_config = rx_dly_config;
309 case PHY_INTERFACE_MODE_RGMII_ID:
310 case PHY_INTERFACE_MODE_RMII:
314 dev_err(dwmac->dev, "unsupported phy-mode %s\n",
315 phy_modes(dwmac->phy_mode));
319 if (rx_dly_config & PRG_ETH0_ADJ_ENABLE) {
320 if (!dwmac->timing_adj_clk) {
322 "The timing-adjustment clock is mandatory for the RX delay re-timing\n");
326 /* The timing adjustment logic is driven by a separate clock */
327 ret = meson8b_devm_clk_prepare_enable(dwmac,
328 dwmac->timing_adj_clk);
331 "Failed to enable the timing-adjustment clock\n");
336 meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, PRG_ETH0_TXDLY_MASK |
337 PRG_ETH0_ADJ_ENABLE | PRG_ETH0_ADJ_SETUP |
338 PRG_ETH0_ADJ_DELAY | PRG_ETH0_ADJ_SKEW,
341 if (phy_interface_mode_is_rgmii(dwmac->phy_mode)) {
342 /* only relevant for RMII mode -> disable in RGMII mode */
343 meson8b_dwmac_mask_bits(dwmac, PRG_ETH0,
344 PRG_ETH0_INVERTED_RMII_CLK, 0);
346 /* Configure the 125MHz RGMII TX clock, the IP block changes
347 * the output automatically (= without us having to configure
348 * a register) based on the line-speed (125MHz for Gbit speeds,
349 * 25MHz for 100Mbit/s and 2.5MHz for 10Mbit/s).
351 ret = clk_set_rate(dwmac->rgmii_tx_clk, 125 * 1000 * 1000);
354 "failed to set RGMII TX clock\n");
358 ret = meson8b_devm_clk_prepare_enable(dwmac,
359 dwmac->rgmii_tx_clk);
362 "failed to enable the RGMII TX clock\n");
366 /* invert internal clk_rmii_i to generate 25/2.5 tx_rx_clk */
367 meson8b_dwmac_mask_bits(dwmac, PRG_ETH0,
368 PRG_ETH0_INVERTED_RMII_CLK,
369 PRG_ETH0_INVERTED_RMII_CLK);
372 /* enable TX_CLK and PHY_REF_CLK generator */
373 meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, PRG_ETH0_TX_AND_PHY_REF_CLK,
374 PRG_ETH0_TX_AND_PHY_REF_CLK);
379 static int meson8b_dwmac_probe(struct platform_device *pdev)
381 struct plat_stmmacenet_data *plat_dat;
382 struct stmmac_resources stmmac_res;
383 struct meson8b_dwmac *dwmac;
386 ret = stmmac_get_platform_resources(pdev, &stmmac_res);
390 plat_dat = stmmac_probe_config_dt(pdev, &stmmac_res.mac);
391 if (IS_ERR(plat_dat))
392 return PTR_ERR(plat_dat);
394 dwmac = devm_kzalloc(&pdev->dev, sizeof(*dwmac), GFP_KERNEL);
397 goto err_remove_config_dt;
400 dwmac->data = (const struct meson8b_dwmac_data *)
401 of_device_get_match_data(&pdev->dev);
404 goto err_remove_config_dt;
406 dwmac->regs = devm_platform_ioremap_resource(pdev, 1);
407 if (IS_ERR(dwmac->regs)) {
408 ret = PTR_ERR(dwmac->regs);
409 goto err_remove_config_dt;
412 dwmac->dev = &pdev->dev;
413 ret = of_get_phy_mode(pdev->dev.of_node, &dwmac->phy_mode);
415 dev_err(&pdev->dev, "missing phy-mode property\n");
416 goto err_remove_config_dt;
419 /* use 2ns as fallback since this value was previously hardcoded */
420 if (of_property_read_u32(pdev->dev.of_node, "amlogic,tx-delay-ns",
421 &dwmac->tx_delay_ns))
422 dwmac->tx_delay_ns = 2;
424 /* use 0ns as fallback since this is what most boards actually use */
425 if (of_property_read_u32(pdev->dev.of_node, "amlogic,rx-delay-ns",
426 &dwmac->rx_delay_ns))
427 dwmac->rx_delay_ns = 0;
429 if (dwmac->rx_delay_ns != 0 && dwmac->rx_delay_ns != 2) {
431 "The only allowed RX delays values are: 0ns, 2ns");
433 goto err_remove_config_dt;
436 dwmac->timing_adj_clk = devm_clk_get_optional(dwmac->dev,
437 "timing-adjustment");
438 if (IS_ERR(dwmac->timing_adj_clk)) {
439 ret = PTR_ERR(dwmac->timing_adj_clk);
440 goto err_remove_config_dt;
443 ret = meson8b_init_rgmii_tx_clk(dwmac);
445 goto err_remove_config_dt;
447 ret = dwmac->data->set_phy_mode(dwmac);
449 goto err_remove_config_dt;
451 ret = meson8b_init_prg_eth(dwmac);
453 goto err_remove_config_dt;
455 plat_dat->bsp_priv = dwmac;
457 ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
459 goto err_remove_config_dt;
463 err_remove_config_dt:
464 stmmac_remove_config_dt(pdev, plat_dat);
469 static const struct meson8b_dwmac_data meson8b_dwmac_data = {
470 .set_phy_mode = meson8b_set_phy_mode,
473 static const struct meson8b_dwmac_data meson_axg_dwmac_data = {
474 .set_phy_mode = meson_axg_set_phy_mode,
477 static const struct of_device_id meson8b_dwmac_match[] = {
479 .compatible = "amlogic,meson8b-dwmac",
480 .data = &meson8b_dwmac_data,
483 .compatible = "amlogic,meson8m2-dwmac",
484 .data = &meson8b_dwmac_data,
487 .compatible = "amlogic,meson-gxbb-dwmac",
488 .data = &meson8b_dwmac_data,
491 .compatible = "amlogic,meson-axg-dwmac",
492 .data = &meson_axg_dwmac_data,
496 MODULE_DEVICE_TABLE(of, meson8b_dwmac_match);
498 static struct platform_driver meson8b_dwmac_driver = {
499 .probe = meson8b_dwmac_probe,
500 .remove = stmmac_pltfr_remove,
502 .name = "meson8b-dwmac",
503 .pm = &stmmac_pltfr_pm_ops,
504 .of_match_table = meson8b_dwmac_match,
507 module_platform_driver(meson8b_dwmac_driver);
509 MODULE_AUTHOR("Martin Blumenstingl <martin.blumenstingl@googlemail.com>");
510 MODULE_DESCRIPTION("Amlogic Meson8b, Meson8m2 and GXBB DWMAC glue layer");
511 MODULE_LICENSE("GPL v2");