13c910ecbSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2566e8251SMartin Blumenstingl /*
37676693cSMartin Blumenstingl * Amlogic Meson8b, Meson8m2 and GXBB DWMAC glue layer
4566e8251SMartin Blumenstingl *
5566e8251SMartin Blumenstingl * Copyright (C) 2016 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
6566e8251SMartin Blumenstingl */
7566e8251SMartin Blumenstingl
83649abe4SMartin Blumenstingl #include <linux/bitfield.h>
9566e8251SMartin Blumenstingl #include <linux/clk.h>
10566e8251SMartin Blumenstingl #include <linux/clk-provider.h>
11566e8251SMartin Blumenstingl #include <linux/device.h>
12566e8251SMartin Blumenstingl #include <linux/ethtool.h>
13566e8251SMartin Blumenstingl #include <linux/io.h>
14566e8251SMartin Blumenstingl #include <linux/ioport.h>
15566e8251SMartin Blumenstingl #include <linux/module.h>
16*3d40aed8SRob Herring #include <linux/of.h>
17566e8251SMartin Blumenstingl #include <linux/of_net.h>
18566e8251SMartin Blumenstingl #include <linux/mfd/syscon.h>
19566e8251SMartin Blumenstingl #include <linux/platform_device.h>
20566e8251SMartin Blumenstingl #include <linux/stmmac.h>
21566e8251SMartin Blumenstingl
22566e8251SMartin Blumenstingl #include "stmmac_platform.h"
23566e8251SMartin Blumenstingl
24566e8251SMartin Blumenstingl #define PRG_ETH0 0x0
25566e8251SMartin Blumenstingl
26566e8251SMartin Blumenstingl #define PRG_ETH0_RGMII_MODE BIT(0)
27566e8251SMartin Blumenstingl
28efacb568SYixun Lan #define PRG_ETH0_EXT_PHY_MODE_MASK GENMASK(2, 0)
29efacb568SYixun Lan #define PRG_ETH0_EXT_RGMII_MODE 1
30efacb568SYixun Lan #define PRG_ETH0_EXT_RMII_MODE 4
31efacb568SYixun Lan
32566e8251SMartin Blumenstingl /* mux to choose between fclk_div2 (bit unset) and mpll2 (bit set) */
33566e8251SMartin Blumenstingl #define PRG_ETH0_CLK_M250_SEL_MASK GENMASK(4, 4)
34566e8251SMartin Blumenstingl
35889df203SMartin Blumenstingl /* TX clock delay in ns = "8ns / 4 * tx_dly_val" (where 8ns are exactly one
36889df203SMartin Blumenstingl * cycle of the 125MHz RGMII TX clock):
37889df203SMartin Blumenstingl * 0ns = 0x0, 2ns = 0x1, 4ns = 0x2, 6ns = 0x3
38889df203SMartin Blumenstingl */
39566e8251SMartin Blumenstingl #define PRG_ETH0_TXDLY_MASK GENMASK(6, 5)
40566e8251SMartin Blumenstingl
41566e8251SMartin Blumenstingl /* divider for the result of m250_sel */
42566e8251SMartin Blumenstingl #define PRG_ETH0_CLK_M250_DIV_SHIFT 7
43566e8251SMartin Blumenstingl #define PRG_ETH0_CLK_M250_DIV_WIDTH 3
44566e8251SMartin Blumenstingl
454f6a71b8SMartin Blumenstingl #define PRG_ETH0_RGMII_TX_CLK_EN 10
46566e8251SMartin Blumenstingl
47566e8251SMartin Blumenstingl #define PRG_ETH0_INVERTED_RMII_CLK BIT(11)
48566e8251SMartin Blumenstingl #define PRG_ETH0_TX_AND_PHY_REF_CLK BIT(12)
49566e8251SMartin Blumenstingl
50c92d1d23SMartin Blumenstingl /* Bypass (= 0, the signal from the GPIO input directly connects to the
51c92d1d23SMartin Blumenstingl * internal sampling) or enable (= 1) the internal logic for RXEN and RXD[3:0]
52c92d1d23SMartin Blumenstingl * timing tuning.
53c92d1d23SMartin Blumenstingl */
54c92d1d23SMartin Blumenstingl #define PRG_ETH0_ADJ_ENABLE BIT(13)
55c92d1d23SMartin Blumenstingl /* Controls whether the RXEN and RXD[3:0] signals should be aligned with the
56c92d1d23SMartin Blumenstingl * input RX rising/falling edge and sent to the Ethernet internals. This sets
57c92d1d23SMartin Blumenstingl * the automatically delay and skew automatically (internally).
58c92d1d23SMartin Blumenstingl */
59c92d1d23SMartin Blumenstingl #define PRG_ETH0_ADJ_SETUP BIT(14)
60c92d1d23SMartin Blumenstingl /* An internal counter based on the "timing-adjustment" clock. The counter is
61c92d1d23SMartin Blumenstingl * cleared on both, the falling and rising edge of the RX_CLK. This selects the
62c92d1d23SMartin Blumenstingl * delay (= the counter value) when to start sampling RXEN and RXD[3:0].
63c92d1d23SMartin Blumenstingl */
64c92d1d23SMartin Blumenstingl #define PRG_ETH0_ADJ_DELAY GENMASK(19, 15)
65c92d1d23SMartin Blumenstingl /* Adjusts the skew between each bit of RXEN and RXD[3:0]. If a signal has a
66c92d1d23SMartin Blumenstingl * large input delay, the bit for that signal (RXEN = bit 0, RXD[3] = bit 1,
67c92d1d23SMartin Blumenstingl * ...) can be configured to be 1 to compensate for a delay of about 1ns.
68c92d1d23SMartin Blumenstingl */
69c92d1d23SMartin Blumenstingl #define PRG_ETH0_ADJ_SKEW GENMASK(24, 20)
70c92d1d23SMartin Blumenstingl
71de94fc10SMartin Blumenstingl #define PRG_ETH1 0x4
72de94fc10SMartin Blumenstingl
73de94fc10SMartin Blumenstingl /* Defined for adding a delay to the input RX_CLK for better timing.
74de94fc10SMartin Blumenstingl * Each step is 200ps. These bits are used with external RGMII PHYs
75de94fc10SMartin Blumenstingl * because RGMII RX only has the small window. cfg_rxclk_dly can
76de94fc10SMartin Blumenstingl * adjust the window between RX_CLK and RX_DATA and improve the stability
77de94fc10SMartin Blumenstingl * of "rx data valid".
78de94fc10SMartin Blumenstingl */
79de94fc10SMartin Blumenstingl #define PRG_ETH1_CFG_RXCLK_DLY GENMASK(19, 16)
80de94fc10SMartin Blumenstingl
81efacb568SYixun Lan struct meson8b_dwmac;
82efacb568SYixun Lan
83efacb568SYixun Lan struct meson8b_dwmac_data {
84efacb568SYixun Lan int (*set_phy_mode)(struct meson8b_dwmac *dwmac);
85de94fc10SMartin Blumenstingl bool has_prg_eth1_rgmii_rx_delay;
86efacb568SYixun Lan };
87efacb568SYixun Lan
88566e8251SMartin Blumenstingl struct meson8b_dwmac {
89b756371eSMartin Blumenstingl struct device *dev;
90566e8251SMartin Blumenstingl void __iomem *regs;
91efacb568SYixun Lan
92efacb568SYixun Lan const struct meson8b_dwmac_data *data;
93566e8251SMartin Blumenstingl phy_interface_t phy_mode;
948076759dSMartin Blumenstingl struct clk *rgmii_tx_clk;
958076759dSMartin Blumenstingl u32 tx_delay_ns;
96140ddf06SMartin Blumenstingl u32 rx_delay_ps;
97e4227bffSMartin Blumenstingl struct clk *timing_adj_clk;
988076759dSMartin Blumenstingl };
99566e8251SMartin Blumenstingl
1008076759dSMartin Blumenstingl struct meson8b_dwmac_clk_configs {
101566e8251SMartin Blumenstingl struct clk_mux m250_mux;
102566e8251SMartin Blumenstingl struct clk_divider m250_div;
1034f6a71b8SMartin Blumenstingl struct clk_fixed_factor fixed_div2;
1044f6a71b8SMartin Blumenstingl struct clk_gate rgmii_tx_en;
105566e8251SMartin Blumenstingl };
106566e8251SMartin Blumenstingl
meson8b_dwmac_mask_bits(struct meson8b_dwmac * dwmac,u32 reg,u32 mask,u32 value)107566e8251SMartin Blumenstingl static void meson8b_dwmac_mask_bits(struct meson8b_dwmac *dwmac, u32 reg,
108566e8251SMartin Blumenstingl u32 mask, u32 value)
109566e8251SMartin Blumenstingl {
110566e8251SMartin Blumenstingl u32 data;
111566e8251SMartin Blumenstingl
112566e8251SMartin Blumenstingl data = readl(dwmac->regs + reg);
113566e8251SMartin Blumenstingl data &= ~mask;
114566e8251SMartin Blumenstingl data |= (value & mask);
115566e8251SMartin Blumenstingl
116566e8251SMartin Blumenstingl writel(data, dwmac->regs + reg);
117566e8251SMartin Blumenstingl }
118566e8251SMartin Blumenstingl
meson8b_dwmac_register_clk(struct meson8b_dwmac * dwmac,const char * name_suffix,const struct clk_parent_data * parents,int num_parents,const struct clk_ops * ops,struct clk_hw * hw)11911184a5fSMartin Blumenstingl static struct clk *meson8b_dwmac_register_clk(struct meson8b_dwmac *dwmac,
12011184a5fSMartin Blumenstingl const char *name_suffix,
12152660c0eSMartin Blumenstingl const struct clk_parent_data *parents,
12211184a5fSMartin Blumenstingl int num_parents,
12311184a5fSMartin Blumenstingl const struct clk_ops *ops,
12411184a5fSMartin Blumenstingl struct clk_hw *hw)
12511184a5fSMartin Blumenstingl {
12652660c0eSMartin Blumenstingl struct clk_init_data init = { };
12711184a5fSMartin Blumenstingl char clk_name[32];
12811184a5fSMartin Blumenstingl
129b756371eSMartin Blumenstingl snprintf(clk_name, sizeof(clk_name), "%s#%s", dev_name(dwmac->dev),
13011184a5fSMartin Blumenstingl name_suffix);
13111184a5fSMartin Blumenstingl
13211184a5fSMartin Blumenstingl init.name = clk_name;
13311184a5fSMartin Blumenstingl init.ops = ops;
13411184a5fSMartin Blumenstingl init.flags = CLK_SET_RATE_PARENT;
13552660c0eSMartin Blumenstingl init.parent_data = parents;
13611184a5fSMartin Blumenstingl init.num_parents = num_parents;
13711184a5fSMartin Blumenstingl
13811184a5fSMartin Blumenstingl hw->init = &init;
13911184a5fSMartin Blumenstingl
140b756371eSMartin Blumenstingl return devm_clk_register(dwmac->dev, hw);
14111184a5fSMartin Blumenstingl }
14211184a5fSMartin Blumenstingl
meson8b_init_rgmii_tx_clk(struct meson8b_dwmac * dwmac)14337512b42SMartin Blumenstingl static int meson8b_init_rgmii_tx_clk(struct meson8b_dwmac *dwmac)
144566e8251SMartin Blumenstingl {
14511184a5fSMartin Blumenstingl struct clk *clk;
146b756371eSMartin Blumenstingl struct device *dev = dwmac->dev;
14752660c0eSMartin Blumenstingl static const struct clk_parent_data mux_parents[] = {
14852660c0eSMartin Blumenstingl { .fw_name = "clkin0", },
149f87777a3SMartin Blumenstingl { .index = -1, },
15052660c0eSMartin Blumenstingl };
151bd6f4854SMartin Blumenstingl static const struct clk_div_table div_table[] = {
152bd6f4854SMartin Blumenstingl { .div = 2, .val = 2, },
153bd6f4854SMartin Blumenstingl { .div = 3, .val = 3, },
154bd6f4854SMartin Blumenstingl { .div = 4, .val = 4, },
155bd6f4854SMartin Blumenstingl { .div = 5, .val = 5, },
156bd6f4854SMartin Blumenstingl { .div = 6, .val = 6, },
157bd6f4854SMartin Blumenstingl { .div = 7, .val = 7, },
158f0212a5eSMarc Zyngier { /* end of array */ }
159bd6f4854SMartin Blumenstingl };
16052660c0eSMartin Blumenstingl struct meson8b_dwmac_clk_configs *clk_configs;
16152660c0eSMartin Blumenstingl struct clk_parent_data parent_data = { };
1628076759dSMartin Blumenstingl
1638076759dSMartin Blumenstingl clk_configs = devm_kzalloc(dev, sizeof(*clk_configs), GFP_KERNEL);
1648076759dSMartin Blumenstingl if (!clk_configs)
1658076759dSMartin Blumenstingl return -ENOMEM;
166566e8251SMartin Blumenstingl
1678076759dSMartin Blumenstingl clk_configs->m250_mux.reg = dwmac->regs + PRG_ETH0;
16882ca4c92SMartin Blumenstingl clk_configs->m250_mux.shift = __ffs(PRG_ETH0_CLK_M250_SEL_MASK);
16982ca4c92SMartin Blumenstingl clk_configs->m250_mux.mask = PRG_ETH0_CLK_M250_SEL_MASK >>
17082ca4c92SMartin Blumenstingl clk_configs->m250_mux.shift;
17152660c0eSMartin Blumenstingl clk = meson8b_dwmac_register_clk(dwmac, "m250_sel", mux_parents,
17252660c0eSMartin Blumenstingl ARRAY_SIZE(mux_parents), &clk_mux_ops,
1738076759dSMartin Blumenstingl &clk_configs->m250_mux.hw);
17411184a5fSMartin Blumenstingl if (WARN_ON(IS_ERR(clk)))
17511184a5fSMartin Blumenstingl return PTR_ERR(clk);
176566e8251SMartin Blumenstingl
17752660c0eSMartin Blumenstingl parent_data.hw = &clk_configs->m250_mux.hw;
1788076759dSMartin Blumenstingl clk_configs->m250_div.reg = dwmac->regs + PRG_ETH0;
1798076759dSMartin Blumenstingl clk_configs->m250_div.shift = PRG_ETH0_CLK_M250_DIV_SHIFT;
1808076759dSMartin Blumenstingl clk_configs->m250_div.width = PRG_ETH0_CLK_M250_DIV_WIDTH;
181bd6f4854SMartin Blumenstingl clk_configs->m250_div.table = div_table;
182bd6f4854SMartin Blumenstingl clk_configs->m250_div.flags = CLK_DIVIDER_ALLOW_ZERO |
183433c6cabSMartin Blumenstingl CLK_DIVIDER_ROUND_CLOSEST;
18452660c0eSMartin Blumenstingl clk = meson8b_dwmac_register_clk(dwmac, "m250_div", &parent_data, 1,
18511184a5fSMartin Blumenstingl &clk_divider_ops,
1868076759dSMartin Blumenstingl &clk_configs->m250_div.hw);
18711184a5fSMartin Blumenstingl if (WARN_ON(IS_ERR(clk)))
18811184a5fSMartin Blumenstingl return PTR_ERR(clk);
189566e8251SMartin Blumenstingl
19052660c0eSMartin Blumenstingl parent_data.hw = &clk_configs->m250_div.hw;
1918076759dSMartin Blumenstingl clk_configs->fixed_div2.mult = 1;
1928076759dSMartin Blumenstingl clk_configs->fixed_div2.div = 2;
19352660c0eSMartin Blumenstingl clk = meson8b_dwmac_register_clk(dwmac, "fixed_div2", &parent_data, 1,
19411184a5fSMartin Blumenstingl &clk_fixed_factor_ops,
1958076759dSMartin Blumenstingl &clk_configs->fixed_div2.hw);
19611184a5fSMartin Blumenstingl if (WARN_ON(IS_ERR(clk)))
19711184a5fSMartin Blumenstingl return PTR_ERR(clk);
198566e8251SMartin Blumenstingl
19952660c0eSMartin Blumenstingl parent_data.hw = &clk_configs->fixed_div2.hw;
2008076759dSMartin Blumenstingl clk_configs->rgmii_tx_en.reg = dwmac->regs + PRG_ETH0;
2018076759dSMartin Blumenstingl clk_configs->rgmii_tx_en.bit_idx = PRG_ETH0_RGMII_TX_CLK_EN;
20252660c0eSMartin Blumenstingl clk = meson8b_dwmac_register_clk(dwmac, "rgmii_tx_en", &parent_data, 1,
20311184a5fSMartin Blumenstingl &clk_gate_ops,
2048076759dSMartin Blumenstingl &clk_configs->rgmii_tx_en.hw);
20511184a5fSMartin Blumenstingl if (WARN_ON(IS_ERR(clk)))
20611184a5fSMartin Blumenstingl return PTR_ERR(clk);
20711184a5fSMartin Blumenstingl
20811184a5fSMartin Blumenstingl dwmac->rgmii_tx_clk = clk;
209566e8251SMartin Blumenstingl
210566e8251SMartin Blumenstingl return 0;
211566e8251SMartin Blumenstingl }
212566e8251SMartin Blumenstingl
meson8b_set_phy_mode(struct meson8b_dwmac * dwmac)213efacb568SYixun Lan static int meson8b_set_phy_mode(struct meson8b_dwmac *dwmac)
214efacb568SYixun Lan {
215efacb568SYixun Lan switch (dwmac->phy_mode) {
216efacb568SYixun Lan case PHY_INTERFACE_MODE_RGMII:
217efacb568SYixun Lan case PHY_INTERFACE_MODE_RGMII_RXID:
218efacb568SYixun Lan case PHY_INTERFACE_MODE_RGMII_ID:
219efacb568SYixun Lan case PHY_INTERFACE_MODE_RGMII_TXID:
220efacb568SYixun Lan /* enable RGMII mode */
221efacb568SYixun Lan meson8b_dwmac_mask_bits(dwmac, PRG_ETH0,
222efacb568SYixun Lan PRG_ETH0_RGMII_MODE,
223efacb568SYixun Lan PRG_ETH0_RGMII_MODE);
224efacb568SYixun Lan break;
225efacb568SYixun Lan case PHY_INTERFACE_MODE_RMII:
226efacb568SYixun Lan /* disable RGMII mode -> enables RMII mode */
227efacb568SYixun Lan meson8b_dwmac_mask_bits(dwmac, PRG_ETH0,
228efacb568SYixun Lan PRG_ETH0_RGMII_MODE, 0);
229efacb568SYixun Lan break;
230efacb568SYixun Lan default:
231efacb568SYixun Lan dev_err(dwmac->dev, "fail to set phy-mode %s\n",
232efacb568SYixun Lan phy_modes(dwmac->phy_mode));
233efacb568SYixun Lan return -EINVAL;
234efacb568SYixun Lan }
235efacb568SYixun Lan
236efacb568SYixun Lan return 0;
237efacb568SYixun Lan }
238efacb568SYixun Lan
meson_axg_set_phy_mode(struct meson8b_dwmac * dwmac)239efacb568SYixun Lan static int meson_axg_set_phy_mode(struct meson8b_dwmac *dwmac)
240efacb568SYixun Lan {
241efacb568SYixun Lan switch (dwmac->phy_mode) {
242efacb568SYixun Lan case PHY_INTERFACE_MODE_RGMII:
243efacb568SYixun Lan case PHY_INTERFACE_MODE_RGMII_RXID:
244efacb568SYixun Lan case PHY_INTERFACE_MODE_RGMII_ID:
245efacb568SYixun Lan case PHY_INTERFACE_MODE_RGMII_TXID:
246efacb568SYixun Lan /* enable RGMII mode */
247efacb568SYixun Lan meson8b_dwmac_mask_bits(dwmac, PRG_ETH0,
248efacb568SYixun Lan PRG_ETH0_EXT_PHY_MODE_MASK,
249efacb568SYixun Lan PRG_ETH0_EXT_RGMII_MODE);
250efacb568SYixun Lan break;
251efacb568SYixun Lan case PHY_INTERFACE_MODE_RMII:
252efacb568SYixun Lan /* disable RGMII mode -> enables RMII mode */
253efacb568SYixun Lan meson8b_dwmac_mask_bits(dwmac, PRG_ETH0,
254efacb568SYixun Lan PRG_ETH0_EXT_PHY_MODE_MASK,
255efacb568SYixun Lan PRG_ETH0_EXT_RMII_MODE);
256efacb568SYixun Lan break;
257efacb568SYixun Lan default:
258efacb568SYixun Lan dev_err(dwmac->dev, "fail to set phy-mode %s\n",
259efacb568SYixun Lan phy_modes(dwmac->phy_mode));
260efacb568SYixun Lan return -EINVAL;
261efacb568SYixun Lan }
262efacb568SYixun Lan
263efacb568SYixun Lan return 0;
264efacb568SYixun Lan }
265efacb568SYixun Lan
meson8b_clk_disable_unprepare(void * data)26643bb6100SSimon Horman static void meson8b_clk_disable_unprepare(void *data)
26743bb6100SSimon Horman {
26843bb6100SSimon Horman clk_disable_unprepare(data);
26943bb6100SSimon Horman }
27043bb6100SSimon Horman
meson8b_devm_clk_prepare_enable(struct meson8b_dwmac * dwmac,struct clk * clk)271a54dc4a4SMartin Blumenstingl static int meson8b_devm_clk_prepare_enable(struct meson8b_dwmac *dwmac,
272a54dc4a4SMartin Blumenstingl struct clk *clk)
273a54dc4a4SMartin Blumenstingl {
274a54dc4a4SMartin Blumenstingl int ret;
275a54dc4a4SMartin Blumenstingl
276a54dc4a4SMartin Blumenstingl ret = clk_prepare_enable(clk);
277a54dc4a4SMartin Blumenstingl if (ret)
278a54dc4a4SMartin Blumenstingl return ret;
279a54dc4a4SMartin Blumenstingl
280ed4314f7SRasmus Villemoes return devm_add_action_or_reset(dwmac->dev,
28143bb6100SSimon Horman meson8b_clk_disable_unprepare, clk);
282a54dc4a4SMartin Blumenstingl }
283a54dc4a4SMartin Blumenstingl
meson8b_init_rgmii_delays(struct meson8b_dwmac * dwmac)2847985244dSMartin Blumenstingl static int meson8b_init_rgmii_delays(struct meson8b_dwmac *dwmac)
285566e8251SMartin Blumenstingl {
286de94fc10SMartin Blumenstingl u32 tx_dly_config, rx_adj_config, cfg_rxclk_dly, delay_config;
287566e8251SMartin Blumenstingl int ret;
2889308c476SMartin Blumenstingl
289de94fc10SMartin Blumenstingl rx_adj_config = 0;
290de94fc10SMartin Blumenstingl cfg_rxclk_dly = 0;
2919308c476SMartin Blumenstingl tx_dly_config = FIELD_PREP(PRG_ETH0_TXDLY_MASK,
2929308c476SMartin Blumenstingl dwmac->tx_delay_ns >> 1);
2939308c476SMartin Blumenstingl
294de94fc10SMartin Blumenstingl if (dwmac->data->has_prg_eth1_rgmii_rx_delay)
295de94fc10SMartin Blumenstingl cfg_rxclk_dly = FIELD_PREP(PRG_ETH1_CFG_RXCLK_DLY,
296de94fc10SMartin Blumenstingl dwmac->rx_delay_ps / 200);
297de94fc10SMartin Blumenstingl else if (dwmac->rx_delay_ps == 2000)
298de94fc10SMartin Blumenstingl rx_adj_config = PRG_ETH0_ADJ_ENABLE | PRG_ETH0_ADJ_SETUP;
299566e8251SMartin Blumenstingl
300566e8251SMartin Blumenstingl switch (dwmac->phy_mode) {
301566e8251SMartin Blumenstingl case PHY_INTERFACE_MODE_RGMII:
302de94fc10SMartin Blumenstingl delay_config = tx_dly_config | rx_adj_config;
3039308c476SMartin Blumenstingl break;
304566e8251SMartin Blumenstingl case PHY_INTERFACE_MODE_RGMII_RXID:
3059308c476SMartin Blumenstingl delay_config = tx_dly_config;
306de94fc10SMartin Blumenstingl cfg_rxclk_dly = 0;
3079308c476SMartin Blumenstingl break;
308566e8251SMartin Blumenstingl case PHY_INTERFACE_MODE_RGMII_TXID:
309de94fc10SMartin Blumenstingl delay_config = rx_adj_config;
3109308c476SMartin Blumenstingl break;
3119308c476SMartin Blumenstingl case PHY_INTERFACE_MODE_RGMII_ID:
3129308c476SMartin Blumenstingl case PHY_INTERFACE_MODE_RMII:
3139308c476SMartin Blumenstingl delay_config = 0;
314de94fc10SMartin Blumenstingl cfg_rxclk_dly = 0;
3159308c476SMartin Blumenstingl break;
3169308c476SMartin Blumenstingl default:
3179308c476SMartin Blumenstingl dev_err(dwmac->dev, "unsupported phy-mode %s\n",
3189308c476SMartin Blumenstingl phy_modes(dwmac->phy_mode));
3199308c476SMartin Blumenstingl return -EINVAL;
3201c5825e6STom Rix }
3219308c476SMartin Blumenstingl
32202582288SMartin Blumenstingl if (delay_config & PRG_ETH0_ADJ_ENABLE) {
3239308c476SMartin Blumenstingl if (!dwmac->timing_adj_clk) {
3249308c476SMartin Blumenstingl dev_err(dwmac->dev,
3259308c476SMartin Blumenstingl "The timing-adjustment clock is mandatory for the RX delay re-timing\n");
3269308c476SMartin Blumenstingl return -EINVAL;
3279308c476SMartin Blumenstingl }
3289308c476SMartin Blumenstingl
3299308c476SMartin Blumenstingl /* The timing adjustment logic is driven by a separate clock */
3309308c476SMartin Blumenstingl ret = meson8b_devm_clk_prepare_enable(dwmac,
3319308c476SMartin Blumenstingl dwmac->timing_adj_clk);
3329308c476SMartin Blumenstingl if (ret) {
3339308c476SMartin Blumenstingl dev_err(dwmac->dev,
3349308c476SMartin Blumenstingl "Failed to enable the timing-adjustment clock\n");
3359308c476SMartin Blumenstingl return ret;
3369308c476SMartin Blumenstingl }
3379308c476SMartin Blumenstingl }
3389308c476SMartin Blumenstingl
3399308c476SMartin Blumenstingl meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, PRG_ETH0_TXDLY_MASK |
3409308c476SMartin Blumenstingl PRG_ETH0_ADJ_ENABLE | PRG_ETH0_ADJ_SETUP |
3419308c476SMartin Blumenstingl PRG_ETH0_ADJ_DELAY | PRG_ETH0_ADJ_SKEW,
3429308c476SMartin Blumenstingl delay_config);
3439308c476SMartin Blumenstingl
344de94fc10SMartin Blumenstingl meson8b_dwmac_mask_bits(dwmac, PRG_ETH1, PRG_ETH1_CFG_RXCLK_DLY,
345de94fc10SMartin Blumenstingl cfg_rxclk_dly);
346de94fc10SMartin Blumenstingl
3477985244dSMartin Blumenstingl return 0;
3487985244dSMartin Blumenstingl }
3497985244dSMartin Blumenstingl
meson8b_init_prg_eth(struct meson8b_dwmac * dwmac)3507985244dSMartin Blumenstingl static int meson8b_init_prg_eth(struct meson8b_dwmac *dwmac)
3517985244dSMartin Blumenstingl {
3527985244dSMartin Blumenstingl int ret;
3537985244dSMartin Blumenstingl
3549308c476SMartin Blumenstingl if (phy_interface_mode_is_rgmii(dwmac->phy_mode)) {
355566e8251SMartin Blumenstingl /* only relevant for RMII mode -> disable in RGMII mode */
356566e8251SMartin Blumenstingl meson8b_dwmac_mask_bits(dwmac, PRG_ETH0,
357566e8251SMartin Blumenstingl PRG_ETH0_INVERTED_RMII_CLK, 0);
358566e8251SMartin Blumenstingl
3594f6a71b8SMartin Blumenstingl /* Configure the 125MHz RGMII TX clock, the IP block changes
3604f6a71b8SMartin Blumenstingl * the output automatically (= without us having to configure
3614f6a71b8SMartin Blumenstingl * a register) based on the line-speed (125MHz for Gbit speeds,
3624f6a71b8SMartin Blumenstingl * 25MHz for 100Mbit/s and 2.5MHz for 10Mbit/s).
3634f6a71b8SMartin Blumenstingl */
36411184a5fSMartin Blumenstingl ret = clk_set_rate(dwmac->rgmii_tx_clk, 125 * 1000 * 1000);
36537512b42SMartin Blumenstingl if (ret) {
366b756371eSMartin Blumenstingl dev_err(dwmac->dev,
3674f6a71b8SMartin Blumenstingl "failed to set RGMII TX clock\n");
36837512b42SMartin Blumenstingl return ret;
36937512b42SMartin Blumenstingl }
37037512b42SMartin Blumenstingl
371a54dc4a4SMartin Blumenstingl ret = meson8b_devm_clk_prepare_enable(dwmac,
372a54dc4a4SMartin Blumenstingl dwmac->rgmii_tx_clk);
37337512b42SMartin Blumenstingl if (ret) {
374b756371eSMartin Blumenstingl dev_err(dwmac->dev,
3754f6a71b8SMartin Blumenstingl "failed to enable the RGMII TX clock\n");
37637512b42SMartin Blumenstingl return ret;
37737512b42SMartin Blumenstingl }
3789308c476SMartin Blumenstingl } else {
379566e8251SMartin Blumenstingl /* invert internal clk_rmii_i to generate 25/2.5 tx_rx_clk */
380566e8251SMartin Blumenstingl meson8b_dwmac_mask_bits(dwmac, PRG_ETH0,
381566e8251SMartin Blumenstingl PRG_ETH0_INVERTED_RMII_CLK,
382566e8251SMartin Blumenstingl PRG_ETH0_INVERTED_RMII_CLK);
383566e8251SMartin Blumenstingl }
384566e8251SMartin Blumenstingl
385566e8251SMartin Blumenstingl /* enable TX_CLK and PHY_REF_CLK generator */
386566e8251SMartin Blumenstingl meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, PRG_ETH0_TX_AND_PHY_REF_CLK,
387566e8251SMartin Blumenstingl PRG_ETH0_TX_AND_PHY_REF_CLK);
388566e8251SMartin Blumenstingl
389566e8251SMartin Blumenstingl return 0;
390566e8251SMartin Blumenstingl }
391566e8251SMartin Blumenstingl
meson8b_dwmac_probe(struct platform_device * pdev)392566e8251SMartin Blumenstingl static int meson8b_dwmac_probe(struct platform_device *pdev)
393566e8251SMartin Blumenstingl {
394566e8251SMartin Blumenstingl struct plat_stmmacenet_data *plat_dat;
395566e8251SMartin Blumenstingl struct stmmac_resources stmmac_res;
396566e8251SMartin Blumenstingl struct meson8b_dwmac *dwmac;
397566e8251SMartin Blumenstingl int ret;
398566e8251SMartin Blumenstingl
399566e8251SMartin Blumenstingl ret = stmmac_get_platform_resources(pdev, &stmmac_res);
400566e8251SMartin Blumenstingl if (ret)
401566e8251SMartin Blumenstingl return ret;
402566e8251SMartin Blumenstingl
40383216e39SMichael Walle plat_dat = stmmac_probe_config_dt(pdev, stmmac_res.mac);
404566e8251SMartin Blumenstingl if (IS_ERR(plat_dat))
405566e8251SMartin Blumenstingl return PTR_ERR(plat_dat);
406566e8251SMartin Blumenstingl
407566e8251SMartin Blumenstingl dwmac = devm_kzalloc(&pdev->dev, sizeof(*dwmac), GFP_KERNEL);
408d2ed0a77SJohan Hovold if (!dwmac) {
409d2ed0a77SJohan Hovold ret = -ENOMEM;
410d2ed0a77SJohan Hovold goto err_remove_config_dt;
411d2ed0a77SJohan Hovold }
412566e8251SMartin Blumenstingl
413efacb568SYixun Lan dwmac->data = (const struct meson8b_dwmac_data *)
414efacb568SYixun Lan of_device_get_match_data(&pdev->dev);
415760a6ed6SChristophe JAILLET if (!dwmac->data) {
416760a6ed6SChristophe JAILLET ret = -EINVAL;
417760a6ed6SChristophe JAILLET goto err_remove_config_dt;
418760a6ed6SChristophe JAILLET }
419999232a3SYueHaibing dwmac->regs = devm_platform_ioremap_resource(pdev, 1);
420d2ed0a77SJohan Hovold if (IS_ERR(dwmac->regs)) {
421d2ed0a77SJohan Hovold ret = PTR_ERR(dwmac->regs);
422d2ed0a77SJohan Hovold goto err_remove_config_dt;
423d2ed0a77SJohan Hovold }
424566e8251SMartin Blumenstingl
425b756371eSMartin Blumenstingl dwmac->dev = &pdev->dev;
4260c65b2b9SAndrew Lunn ret = of_get_phy_mode(pdev->dev.of_node, &dwmac->phy_mode);
4270c65b2b9SAndrew Lunn if (ret) {
428566e8251SMartin Blumenstingl dev_err(&pdev->dev, "missing phy-mode property\n");
429d2ed0a77SJohan Hovold goto err_remove_config_dt;
430566e8251SMartin Blumenstingl }
431566e8251SMartin Blumenstingl
432b765234eSMartin Blumenstingl /* use 2ns as fallback since this value was previously hardcoded */
433b765234eSMartin Blumenstingl if (of_property_read_u32(pdev->dev.of_node, "amlogic,tx-delay-ns",
434b765234eSMartin Blumenstingl &dwmac->tx_delay_ns))
435b765234eSMartin Blumenstingl dwmac->tx_delay_ns = 2;
436b765234eSMartin Blumenstingl
437140ddf06SMartin Blumenstingl /* RX delay defaults to 0ps since this is what many boards use */
438140ddf06SMartin Blumenstingl if (of_property_read_u32(pdev->dev.of_node, "rx-internal-delay-ps",
439140ddf06SMartin Blumenstingl &dwmac->rx_delay_ps)) {
440140ddf06SMartin Blumenstingl if (!of_property_read_u32(pdev->dev.of_node,
441140ddf06SMartin Blumenstingl "amlogic,rx-delay-ns",
442140ddf06SMartin Blumenstingl &dwmac->rx_delay_ps))
443140ddf06SMartin Blumenstingl /* convert ns to ps */
444140ddf06SMartin Blumenstingl dwmac->rx_delay_ps *= 1000;
445140ddf06SMartin Blumenstingl }
4469308c476SMartin Blumenstingl
447de94fc10SMartin Blumenstingl if (dwmac->data->has_prg_eth1_rgmii_rx_delay) {
4489e8789c8SMartin Blumenstingl if (dwmac->rx_delay_ps > 3000 || dwmac->rx_delay_ps % 200) {
449de94fc10SMartin Blumenstingl dev_err(dwmac->dev,
4509e8789c8SMartin Blumenstingl "The RGMII RX delay range is 0..3000ps in 200ps steps");
4519308c476SMartin Blumenstingl ret = -EINVAL;
4529308c476SMartin Blumenstingl goto err_remove_config_dt;
4539308c476SMartin Blumenstingl }
454de94fc10SMartin Blumenstingl } else {
4559e8789c8SMartin Blumenstingl if (dwmac->rx_delay_ps != 0 && dwmac->rx_delay_ps != 2000) {
456de94fc10SMartin Blumenstingl dev_err(dwmac->dev,
4579e8789c8SMartin Blumenstingl "The only allowed RGMII RX delays values are: 0ps, 2000ps");
458de94fc10SMartin Blumenstingl ret = -EINVAL;
459de94fc10SMartin Blumenstingl goto err_remove_config_dt;
460de94fc10SMartin Blumenstingl }
461de94fc10SMartin Blumenstingl }
4629308c476SMartin Blumenstingl
463e4227bffSMartin Blumenstingl dwmac->timing_adj_clk = devm_clk_get_optional(dwmac->dev,
464e4227bffSMartin Blumenstingl "timing-adjustment");
465e4227bffSMartin Blumenstingl if (IS_ERR(dwmac->timing_adj_clk)) {
466e4227bffSMartin Blumenstingl ret = PTR_ERR(dwmac->timing_adj_clk);
467e4227bffSMartin Blumenstingl goto err_remove_config_dt;
468e4227bffSMartin Blumenstingl }
469e4227bffSMartin Blumenstingl
4707985244dSMartin Blumenstingl ret = meson8b_init_rgmii_delays(dwmac);
4717985244dSMartin Blumenstingl if (ret)
4727985244dSMartin Blumenstingl goto err_remove_config_dt;
4737985244dSMartin Blumenstingl
47437512b42SMartin Blumenstingl ret = meson8b_init_rgmii_tx_clk(dwmac);
475566e8251SMartin Blumenstingl if (ret)
476d2ed0a77SJohan Hovold goto err_remove_config_dt;
477566e8251SMartin Blumenstingl
478efacb568SYixun Lan ret = dwmac->data->set_phy_mode(dwmac);
479efacb568SYixun Lan if (ret)
480efacb568SYixun Lan goto err_remove_config_dt;
481efacb568SYixun Lan
482566e8251SMartin Blumenstingl ret = meson8b_init_prg_eth(dwmac);
483566e8251SMartin Blumenstingl if (ret)
484d2ed0a77SJohan Hovold goto err_remove_config_dt;
485566e8251SMartin Blumenstingl
486566e8251SMartin Blumenstingl plat_dat->bsp_priv = dwmac;
487566e8251SMartin Blumenstingl
4885cc70bbcSJohan Hovold ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
4895cc70bbcSJohan Hovold if (ret)
49011184a5fSMartin Blumenstingl goto err_remove_config_dt;
4915cc70bbcSJohan Hovold
4925cc70bbcSJohan Hovold return 0;
4935cc70bbcSJohan Hovold
494d2ed0a77SJohan Hovold err_remove_config_dt:
495d2ed0a77SJohan Hovold stmmac_remove_config_dt(pdev, plat_dat);
4965cc70bbcSJohan Hovold
4975cc70bbcSJohan Hovold return ret;
498566e8251SMartin Blumenstingl }
499566e8251SMartin Blumenstingl
500efacb568SYixun Lan static const struct meson8b_dwmac_data meson8b_dwmac_data = {
501efacb568SYixun Lan .set_phy_mode = meson8b_set_phy_mode,
502de94fc10SMartin Blumenstingl .has_prg_eth1_rgmii_rx_delay = false,
503efacb568SYixun Lan };
504efacb568SYixun Lan
505efacb568SYixun Lan static const struct meson8b_dwmac_data meson_axg_dwmac_data = {
506efacb568SYixun Lan .set_phy_mode = meson_axg_set_phy_mode,
507de94fc10SMartin Blumenstingl .has_prg_eth1_rgmii_rx_delay = false,
508de94fc10SMartin Blumenstingl };
509de94fc10SMartin Blumenstingl
510de94fc10SMartin Blumenstingl static const struct meson8b_dwmac_data meson_g12a_dwmac_data = {
511de94fc10SMartin Blumenstingl .set_phy_mode = meson_axg_set_phy_mode,
512de94fc10SMartin Blumenstingl .has_prg_eth1_rgmii_rx_delay = true,
513efacb568SYixun Lan };
514efacb568SYixun Lan
515566e8251SMartin Blumenstingl static const struct of_device_id meson8b_dwmac_match[] = {
516efacb568SYixun Lan {
517efacb568SYixun Lan .compatible = "amlogic,meson8b-dwmac",
518efacb568SYixun Lan .data = &meson8b_dwmac_data,
519efacb568SYixun Lan },
520efacb568SYixun Lan {
521efacb568SYixun Lan .compatible = "amlogic,meson8m2-dwmac",
522efacb568SYixun Lan .data = &meson8b_dwmac_data,
523efacb568SYixun Lan },
524efacb568SYixun Lan {
525efacb568SYixun Lan .compatible = "amlogic,meson-gxbb-dwmac",
526efacb568SYixun Lan .data = &meson8b_dwmac_data,
527efacb568SYixun Lan },
528efacb568SYixun Lan {
529efacb568SYixun Lan .compatible = "amlogic,meson-axg-dwmac",
530efacb568SYixun Lan .data = &meson_axg_dwmac_data,
531efacb568SYixun Lan },
532a4f63342SMartin Blumenstingl {
533a4f63342SMartin Blumenstingl .compatible = "amlogic,meson-g12a-dwmac",
534de94fc10SMartin Blumenstingl .data = &meson_g12a_dwmac_data,
535a4f63342SMartin Blumenstingl },
536566e8251SMartin Blumenstingl { }
537566e8251SMartin Blumenstingl };
538566e8251SMartin Blumenstingl MODULE_DEVICE_TABLE(of, meson8b_dwmac_match);
539566e8251SMartin Blumenstingl
540566e8251SMartin Blumenstingl static struct platform_driver meson8b_dwmac_driver = {
541566e8251SMartin Blumenstingl .probe = meson8b_dwmac_probe,
5423246627fSUwe Kleine-König .remove_new = stmmac_pltfr_remove,
543566e8251SMartin Blumenstingl .driver = {
544566e8251SMartin Blumenstingl .name = "meson8b-dwmac",
545566e8251SMartin Blumenstingl .pm = &stmmac_pltfr_pm_ops,
546566e8251SMartin Blumenstingl .of_match_table = meson8b_dwmac_match,
547566e8251SMartin Blumenstingl },
548566e8251SMartin Blumenstingl };
549566e8251SMartin Blumenstingl module_platform_driver(meson8b_dwmac_driver);
550566e8251SMartin Blumenstingl
551566e8251SMartin Blumenstingl MODULE_AUTHOR("Martin Blumenstingl <martin.blumenstingl@googlemail.com>");
5527676693cSMartin Blumenstingl MODULE_DESCRIPTION("Amlogic Meson8b, Meson8m2 and GXBB DWMAC glue layer");
553566e8251SMartin Blumenstingl MODULE_LICENSE("GPL v2");
554