Thomas Gleixner | 3c910ec | 2019-06-01 10:09:00 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 2 | /* |
Martin Blumenstingl | 7676693 | 2018-03-30 01:00:35 +0200 | [diff] [blame] | 3 | * Amlogic Meson8b, Meson8m2 and GXBB DWMAC glue layer |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2016 Martin Blumenstingl <martin.blumenstingl@googlemail.com> |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
Martin Blumenstingl | 3649abe | 2020-05-12 23:10:58 +0200 | [diff] [blame] | 8 | #include <linux/bitfield.h> |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 9 | #include <linux/clk.h> |
| 10 | #include <linux/clk-provider.h> |
| 11 | #include <linux/device.h> |
| 12 | #include <linux/ethtool.h> |
| 13 | #include <linux/io.h> |
| 14 | #include <linux/ioport.h> |
| 15 | #include <linux/module.h> |
Yixun Lan | efacb56 | 2018-04-28 10:21:11 +0000 | [diff] [blame] | 16 | #include <linux/of_device.h> |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 17 | #include <linux/of_net.h> |
| 18 | #include <linux/mfd/syscon.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/stmmac.h> |
| 21 | |
| 22 | #include "stmmac_platform.h" |
| 23 | |
| 24 | #define PRG_ETH0 0x0 |
| 25 | |
| 26 | #define PRG_ETH0_RGMII_MODE BIT(0) |
| 27 | |
Yixun Lan | efacb56 | 2018-04-28 10:21:11 +0000 | [diff] [blame] | 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 |
| 31 | |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 32 | /* mux to choose between fclk_div2 (bit unset) and mpll2 (bit set) */ |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 33 | #define PRG_ETH0_CLK_M250_SEL_MASK GENMASK(4, 4) |
| 34 | |
Martin Blumenstingl | 889df20 | 2020-05-12 23:10:59 +0200 | [diff] [blame] | 35 | /* TX clock delay in ns = "8ns / 4 * tx_dly_val" (where 8ns are exactly one |
| 36 | * cycle of the 125MHz RGMII TX clock): |
| 37 | * 0ns = 0x0, 2ns = 0x1, 4ns = 0x2, 6ns = 0x3 |
| 38 | */ |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 39 | #define PRG_ETH0_TXDLY_MASK GENMASK(6, 5) |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 40 | |
| 41 | /* divider for the result of m250_sel */ |
| 42 | #define PRG_ETH0_CLK_M250_DIV_SHIFT 7 |
| 43 | #define PRG_ETH0_CLK_M250_DIV_WIDTH 3 |
| 44 | |
Martin Blumenstingl | 4f6a71b | 2018-01-15 18:10:13 +0100 | [diff] [blame] | 45 | #define PRG_ETH0_RGMII_TX_CLK_EN 10 |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 46 | |
| 47 | #define PRG_ETH0_INVERTED_RMII_CLK BIT(11) |
| 48 | #define PRG_ETH0_TX_AND_PHY_REF_CLK BIT(12) |
| 49 | |
Martin Blumenstingl | c92d1d2 | 2020-05-12 23:11:00 +0200 | [diff] [blame] | 50 | /* Bypass (= 0, the signal from the GPIO input directly connects to the |
| 51 | * internal sampling) or enable (= 1) the internal logic for RXEN and RXD[3:0] |
| 52 | * timing tuning. |
| 53 | */ |
| 54 | #define PRG_ETH0_ADJ_ENABLE BIT(13) |
| 55 | /* Controls whether the RXEN and RXD[3:0] signals should be aligned with the |
| 56 | * input RX rising/falling edge and sent to the Ethernet internals. This sets |
| 57 | * the automatically delay and skew automatically (internally). |
| 58 | */ |
| 59 | #define PRG_ETH0_ADJ_SETUP BIT(14) |
| 60 | /* An internal counter based on the "timing-adjustment" clock. The counter is |
| 61 | * cleared on both, the falling and rising edge of the RX_CLK. This selects the |
| 62 | * delay (= the counter value) when to start sampling RXEN and RXD[3:0]. |
| 63 | */ |
| 64 | #define PRG_ETH0_ADJ_DELAY GENMASK(19, 15) |
| 65 | /* Adjusts the skew between each bit of RXEN and RXD[3:0]. If a signal has a |
| 66 | * large input delay, the bit for that signal (RXEN = bit 0, RXD[3] = bit 1, |
| 67 | * ...) can be configured to be 1 to compensate for a delay of about 1ns. |
| 68 | */ |
| 69 | #define PRG_ETH0_ADJ_SKEW GENMASK(24, 20) |
| 70 | |
Yixun Lan | efacb56 | 2018-04-28 10:21:11 +0000 | [diff] [blame] | 71 | struct meson8b_dwmac; |
| 72 | |
| 73 | struct meson8b_dwmac_data { |
| 74 | int (*set_phy_mode)(struct meson8b_dwmac *dwmac); |
| 75 | }; |
| 76 | |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 77 | struct meson8b_dwmac { |
Yixun Lan | efacb56 | 2018-04-28 10:21:11 +0000 | [diff] [blame] | 78 | struct device *dev; |
| 79 | void __iomem *regs; |
| 80 | |
| 81 | const struct meson8b_dwmac_data *data; |
| 82 | phy_interface_t phy_mode; |
| 83 | struct clk *rgmii_tx_clk; |
| 84 | u32 tx_delay_ns; |
Martin Blumenstingl | 140ddf0 | 2021-01-06 14:42:49 +0100 | [diff] [blame] | 85 | u32 rx_delay_ps; |
Martin Blumenstingl | e4227bf | 2020-05-12 23:11:01 +0200 | [diff] [blame] | 86 | struct clk *timing_adj_clk; |
Martin Blumenstingl | 8076759 | 2018-02-17 15:08:20 +0100 | [diff] [blame] | 87 | }; |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 88 | |
Martin Blumenstingl | 8076759 | 2018-02-17 15:08:20 +0100 | [diff] [blame] | 89 | struct meson8b_dwmac_clk_configs { |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 90 | struct clk_mux m250_mux; |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 91 | struct clk_divider m250_div; |
Martin Blumenstingl | 4f6a71b | 2018-01-15 18:10:13 +0100 | [diff] [blame] | 92 | struct clk_fixed_factor fixed_div2; |
Martin Blumenstingl | 4f6a71b | 2018-01-15 18:10:13 +0100 | [diff] [blame] | 93 | struct clk_gate rgmii_tx_en; |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | static void meson8b_dwmac_mask_bits(struct meson8b_dwmac *dwmac, u32 reg, |
| 97 | u32 mask, u32 value) |
| 98 | { |
| 99 | u32 data; |
| 100 | |
| 101 | data = readl(dwmac->regs + reg); |
| 102 | data &= ~mask; |
| 103 | data |= (value & mask); |
| 104 | |
| 105 | writel(data, dwmac->regs + reg); |
| 106 | } |
| 107 | |
Martin Blumenstingl | 11184a5 | 2018-02-17 15:08:18 +0100 | [diff] [blame] | 108 | static struct clk *meson8b_dwmac_register_clk(struct meson8b_dwmac *dwmac, |
| 109 | const char *name_suffix, |
Martin Blumenstingl | 52660c0 | 2020-06-25 20:21:42 +0200 | [diff] [blame] | 110 | const struct clk_parent_data *parents, |
Martin Blumenstingl | 11184a5 | 2018-02-17 15:08:18 +0100 | [diff] [blame] | 111 | int num_parents, |
| 112 | const struct clk_ops *ops, |
| 113 | struct clk_hw *hw) |
| 114 | { |
Martin Blumenstingl | 52660c0 | 2020-06-25 20:21:42 +0200 | [diff] [blame] | 115 | struct clk_init_data init = { }; |
Martin Blumenstingl | 11184a5 | 2018-02-17 15:08:18 +0100 | [diff] [blame] | 116 | char clk_name[32]; |
| 117 | |
Martin Blumenstingl | b756371 | 2018-02-17 15:08:19 +0100 | [diff] [blame] | 118 | snprintf(clk_name, sizeof(clk_name), "%s#%s", dev_name(dwmac->dev), |
Martin Blumenstingl | 11184a5 | 2018-02-17 15:08:18 +0100 | [diff] [blame] | 119 | name_suffix); |
| 120 | |
| 121 | init.name = clk_name; |
| 122 | init.ops = ops; |
| 123 | init.flags = CLK_SET_RATE_PARENT; |
Martin Blumenstingl | 52660c0 | 2020-06-25 20:21:42 +0200 | [diff] [blame] | 124 | init.parent_data = parents; |
Martin Blumenstingl | 11184a5 | 2018-02-17 15:08:18 +0100 | [diff] [blame] | 125 | init.num_parents = num_parents; |
| 126 | |
| 127 | hw->init = &init; |
| 128 | |
Martin Blumenstingl | b756371 | 2018-02-17 15:08:19 +0100 | [diff] [blame] | 129 | return devm_clk_register(dwmac->dev, hw); |
Martin Blumenstingl | 11184a5 | 2018-02-17 15:08:18 +0100 | [diff] [blame] | 130 | } |
| 131 | |
Martin Blumenstingl | 37512b4 | 2018-01-15 18:10:12 +0100 | [diff] [blame] | 132 | static int meson8b_init_rgmii_tx_clk(struct meson8b_dwmac *dwmac) |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 133 | { |
Martin Blumenstingl | 11184a5 | 2018-02-17 15:08:18 +0100 | [diff] [blame] | 134 | struct clk *clk; |
Martin Blumenstingl | b756371 | 2018-02-17 15:08:19 +0100 | [diff] [blame] | 135 | struct device *dev = dwmac->dev; |
Martin Blumenstingl | 52660c0 | 2020-06-25 20:21:42 +0200 | [diff] [blame] | 136 | static const struct clk_parent_data mux_parents[] = { |
| 137 | { .fw_name = "clkin0", }, |
Martin Blumenstingl | f87777a | 2020-12-19 14:50:36 +0100 | [diff] [blame] | 138 | { .index = -1, }, |
Martin Blumenstingl | 52660c0 | 2020-06-25 20:21:42 +0200 | [diff] [blame] | 139 | }; |
Martin Blumenstingl | bd6f485 | 2019-12-26 20:01:01 +0100 | [diff] [blame] | 140 | static const struct clk_div_table div_table[] = { |
| 141 | { .div = 2, .val = 2, }, |
| 142 | { .div = 3, .val = 3, }, |
| 143 | { .div = 4, .val = 4, }, |
| 144 | { .div = 5, .val = 5, }, |
| 145 | { .div = 6, .val = 6, }, |
| 146 | { .div = 7, .val = 7, }, |
Marc Zyngier | f0212a5 | 2020-04-18 19:14:57 +0100 | [diff] [blame] | 147 | { /* end of array */ } |
Martin Blumenstingl | bd6f485 | 2019-12-26 20:01:01 +0100 | [diff] [blame] | 148 | }; |
Martin Blumenstingl | 52660c0 | 2020-06-25 20:21:42 +0200 | [diff] [blame] | 149 | struct meson8b_dwmac_clk_configs *clk_configs; |
| 150 | struct clk_parent_data parent_data = { }; |
Martin Blumenstingl | 8076759 | 2018-02-17 15:08:20 +0100 | [diff] [blame] | 151 | |
| 152 | clk_configs = devm_kzalloc(dev, sizeof(*clk_configs), GFP_KERNEL); |
| 153 | if (!clk_configs) |
| 154 | return -ENOMEM; |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 155 | |
Martin Blumenstingl | 8076759 | 2018-02-17 15:08:20 +0100 | [diff] [blame] | 156 | clk_configs->m250_mux.reg = dwmac->regs + PRG_ETH0; |
Martin Blumenstingl | 82ca4c9 | 2020-12-05 22:32:07 +0100 | [diff] [blame] | 157 | clk_configs->m250_mux.shift = __ffs(PRG_ETH0_CLK_M250_SEL_MASK); |
| 158 | clk_configs->m250_mux.mask = PRG_ETH0_CLK_M250_SEL_MASK >> |
| 159 | clk_configs->m250_mux.shift; |
Martin Blumenstingl | 52660c0 | 2020-06-25 20:21:42 +0200 | [diff] [blame] | 160 | clk = meson8b_dwmac_register_clk(dwmac, "m250_sel", mux_parents, |
| 161 | ARRAY_SIZE(mux_parents), &clk_mux_ops, |
Martin Blumenstingl | 8076759 | 2018-02-17 15:08:20 +0100 | [diff] [blame] | 162 | &clk_configs->m250_mux.hw); |
Martin Blumenstingl | 11184a5 | 2018-02-17 15:08:18 +0100 | [diff] [blame] | 163 | if (WARN_ON(IS_ERR(clk))) |
| 164 | return PTR_ERR(clk); |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 165 | |
Martin Blumenstingl | 52660c0 | 2020-06-25 20:21:42 +0200 | [diff] [blame] | 166 | parent_data.hw = &clk_configs->m250_mux.hw; |
Martin Blumenstingl | 8076759 | 2018-02-17 15:08:20 +0100 | [diff] [blame] | 167 | clk_configs->m250_div.reg = dwmac->regs + PRG_ETH0; |
| 168 | clk_configs->m250_div.shift = PRG_ETH0_CLK_M250_DIV_SHIFT; |
| 169 | clk_configs->m250_div.width = PRG_ETH0_CLK_M250_DIV_WIDTH; |
Martin Blumenstingl | bd6f485 | 2019-12-26 20:01:01 +0100 | [diff] [blame] | 170 | clk_configs->m250_div.table = div_table; |
| 171 | clk_configs->m250_div.flags = CLK_DIVIDER_ALLOW_ZERO | |
| 172 | CLK_DIVIDER_ROUND_CLOSEST; |
Martin Blumenstingl | 52660c0 | 2020-06-25 20:21:42 +0200 | [diff] [blame] | 173 | clk = meson8b_dwmac_register_clk(dwmac, "m250_div", &parent_data, 1, |
Martin Blumenstingl | 11184a5 | 2018-02-17 15:08:18 +0100 | [diff] [blame] | 174 | &clk_divider_ops, |
Martin Blumenstingl | 8076759 | 2018-02-17 15:08:20 +0100 | [diff] [blame] | 175 | &clk_configs->m250_div.hw); |
Martin Blumenstingl | 11184a5 | 2018-02-17 15:08:18 +0100 | [diff] [blame] | 176 | if (WARN_ON(IS_ERR(clk))) |
| 177 | return PTR_ERR(clk); |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 178 | |
Martin Blumenstingl | 52660c0 | 2020-06-25 20:21:42 +0200 | [diff] [blame] | 179 | parent_data.hw = &clk_configs->m250_div.hw; |
Martin Blumenstingl | 8076759 | 2018-02-17 15:08:20 +0100 | [diff] [blame] | 180 | clk_configs->fixed_div2.mult = 1; |
| 181 | clk_configs->fixed_div2.div = 2; |
Martin Blumenstingl | 52660c0 | 2020-06-25 20:21:42 +0200 | [diff] [blame] | 182 | clk = meson8b_dwmac_register_clk(dwmac, "fixed_div2", &parent_data, 1, |
Martin Blumenstingl | 11184a5 | 2018-02-17 15:08:18 +0100 | [diff] [blame] | 183 | &clk_fixed_factor_ops, |
Martin Blumenstingl | 8076759 | 2018-02-17 15:08:20 +0100 | [diff] [blame] | 184 | &clk_configs->fixed_div2.hw); |
Martin Blumenstingl | 11184a5 | 2018-02-17 15:08:18 +0100 | [diff] [blame] | 185 | if (WARN_ON(IS_ERR(clk))) |
| 186 | return PTR_ERR(clk); |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 187 | |
Martin Blumenstingl | 52660c0 | 2020-06-25 20:21:42 +0200 | [diff] [blame] | 188 | parent_data.hw = &clk_configs->fixed_div2.hw; |
Martin Blumenstingl | 8076759 | 2018-02-17 15:08:20 +0100 | [diff] [blame] | 189 | clk_configs->rgmii_tx_en.reg = dwmac->regs + PRG_ETH0; |
| 190 | clk_configs->rgmii_tx_en.bit_idx = PRG_ETH0_RGMII_TX_CLK_EN; |
Martin Blumenstingl | 52660c0 | 2020-06-25 20:21:42 +0200 | [diff] [blame] | 191 | clk = meson8b_dwmac_register_clk(dwmac, "rgmii_tx_en", &parent_data, 1, |
Martin Blumenstingl | 11184a5 | 2018-02-17 15:08:18 +0100 | [diff] [blame] | 192 | &clk_gate_ops, |
Martin Blumenstingl | 8076759 | 2018-02-17 15:08:20 +0100 | [diff] [blame] | 193 | &clk_configs->rgmii_tx_en.hw); |
Martin Blumenstingl | 11184a5 | 2018-02-17 15:08:18 +0100 | [diff] [blame] | 194 | if (WARN_ON(IS_ERR(clk))) |
| 195 | return PTR_ERR(clk); |
Martin Blumenstingl | 4f6a71b | 2018-01-15 18:10:13 +0100 | [diff] [blame] | 196 | |
Martin Blumenstingl | 11184a5 | 2018-02-17 15:08:18 +0100 | [diff] [blame] | 197 | dwmac->rgmii_tx_clk = clk; |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
Yixun Lan | efacb56 | 2018-04-28 10:21:11 +0000 | [diff] [blame] | 202 | static int meson8b_set_phy_mode(struct meson8b_dwmac *dwmac) |
| 203 | { |
| 204 | switch (dwmac->phy_mode) { |
| 205 | case PHY_INTERFACE_MODE_RGMII: |
| 206 | case PHY_INTERFACE_MODE_RGMII_RXID: |
| 207 | case PHY_INTERFACE_MODE_RGMII_ID: |
| 208 | case PHY_INTERFACE_MODE_RGMII_TXID: |
| 209 | /* enable RGMII mode */ |
| 210 | meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, |
| 211 | PRG_ETH0_RGMII_MODE, |
| 212 | PRG_ETH0_RGMII_MODE); |
| 213 | break; |
| 214 | case PHY_INTERFACE_MODE_RMII: |
| 215 | /* disable RGMII mode -> enables RMII mode */ |
| 216 | meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, |
| 217 | PRG_ETH0_RGMII_MODE, 0); |
| 218 | break; |
| 219 | default: |
| 220 | dev_err(dwmac->dev, "fail to set phy-mode %s\n", |
| 221 | phy_modes(dwmac->phy_mode)); |
| 222 | return -EINVAL; |
| 223 | } |
| 224 | |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | static int meson_axg_set_phy_mode(struct meson8b_dwmac *dwmac) |
| 229 | { |
| 230 | switch (dwmac->phy_mode) { |
| 231 | case PHY_INTERFACE_MODE_RGMII: |
| 232 | case PHY_INTERFACE_MODE_RGMII_RXID: |
| 233 | case PHY_INTERFACE_MODE_RGMII_ID: |
| 234 | case PHY_INTERFACE_MODE_RGMII_TXID: |
| 235 | /* enable RGMII mode */ |
| 236 | meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, |
| 237 | PRG_ETH0_EXT_PHY_MODE_MASK, |
| 238 | PRG_ETH0_EXT_RGMII_MODE); |
| 239 | break; |
| 240 | case PHY_INTERFACE_MODE_RMII: |
| 241 | /* disable RGMII mode -> enables RMII mode */ |
| 242 | meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, |
| 243 | PRG_ETH0_EXT_PHY_MODE_MASK, |
| 244 | PRG_ETH0_EXT_RMII_MODE); |
| 245 | break; |
| 246 | default: |
| 247 | dev_err(dwmac->dev, "fail to set phy-mode %s\n", |
| 248 | phy_modes(dwmac->phy_mode)); |
| 249 | return -EINVAL; |
| 250 | } |
| 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | |
Martin Blumenstingl | a54dc4a | 2020-05-12 23:11:02 +0200 | [diff] [blame] | 255 | static int meson8b_devm_clk_prepare_enable(struct meson8b_dwmac *dwmac, |
| 256 | struct clk *clk) |
| 257 | { |
| 258 | int ret; |
| 259 | |
| 260 | ret = clk_prepare_enable(clk); |
| 261 | if (ret) |
| 262 | return ret; |
| 263 | |
| 264 | devm_add_action_or_reset(dwmac->dev, |
| 265 | (void(*)(void *))clk_disable_unprepare, |
| 266 | dwmac->rgmii_tx_clk); |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
Martin Blumenstingl | 7985244 | 2021-01-06 14:42:50 +0100 | [diff] [blame^] | 271 | static int meson8b_init_rgmii_delays(struct meson8b_dwmac *dwmac) |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 272 | { |
Martin Blumenstingl | 9308c476 | 2020-05-12 23:11:03 +0200 | [diff] [blame] | 273 | u32 tx_dly_config, rx_dly_config, delay_config; |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 274 | int ret; |
Martin Blumenstingl | 9308c476 | 2020-05-12 23:11:03 +0200 | [diff] [blame] | 275 | |
| 276 | tx_dly_config = FIELD_PREP(PRG_ETH0_TXDLY_MASK, |
| 277 | dwmac->tx_delay_ns >> 1); |
| 278 | |
Martin Blumenstingl | 140ddf0 | 2021-01-06 14:42:49 +0100 | [diff] [blame] | 279 | if (dwmac->rx_delay_ps == 2000) |
Martin Blumenstingl | 9308c476 | 2020-05-12 23:11:03 +0200 | [diff] [blame] | 280 | rx_dly_config = PRG_ETH0_ADJ_ENABLE | PRG_ETH0_ADJ_SETUP; |
| 281 | else |
| 282 | rx_dly_config = 0; |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 283 | |
| 284 | switch (dwmac->phy_mode) { |
| 285 | case PHY_INTERFACE_MODE_RGMII: |
Martin Blumenstingl | 9308c476 | 2020-05-12 23:11:03 +0200 | [diff] [blame] | 286 | delay_config = tx_dly_config | rx_dly_config; |
| 287 | break; |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 288 | case PHY_INTERFACE_MODE_RGMII_RXID: |
Martin Blumenstingl | 9308c476 | 2020-05-12 23:11:03 +0200 | [diff] [blame] | 289 | delay_config = tx_dly_config; |
| 290 | break; |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 291 | case PHY_INTERFACE_MODE_RGMII_TXID: |
Martin Blumenstingl | 9308c476 | 2020-05-12 23:11:03 +0200 | [diff] [blame] | 292 | delay_config = rx_dly_config; |
| 293 | break; |
| 294 | case PHY_INTERFACE_MODE_RGMII_ID: |
| 295 | case PHY_INTERFACE_MODE_RMII: |
| 296 | delay_config = 0; |
| 297 | break; |
| 298 | default: |
| 299 | dev_err(dwmac->dev, "unsupported phy-mode %s\n", |
| 300 | phy_modes(dwmac->phy_mode)); |
| 301 | return -EINVAL; |
Tom Rix | 1c5825e | 2020-11-01 06:07:20 -0800 | [diff] [blame] | 302 | } |
Martin Blumenstingl | 9308c476 | 2020-05-12 23:11:03 +0200 | [diff] [blame] | 303 | |
Martin Blumenstingl | 0258228 | 2021-01-06 14:42:48 +0100 | [diff] [blame] | 304 | if (delay_config & PRG_ETH0_ADJ_ENABLE) { |
Martin Blumenstingl | 9308c476 | 2020-05-12 23:11:03 +0200 | [diff] [blame] | 305 | if (!dwmac->timing_adj_clk) { |
| 306 | dev_err(dwmac->dev, |
| 307 | "The timing-adjustment clock is mandatory for the RX delay re-timing\n"); |
| 308 | return -EINVAL; |
| 309 | } |
| 310 | |
| 311 | /* The timing adjustment logic is driven by a separate clock */ |
| 312 | ret = meson8b_devm_clk_prepare_enable(dwmac, |
| 313 | dwmac->timing_adj_clk); |
| 314 | if (ret) { |
| 315 | dev_err(dwmac->dev, |
| 316 | "Failed to enable the timing-adjustment clock\n"); |
| 317 | return ret; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, PRG_ETH0_TXDLY_MASK | |
| 322 | PRG_ETH0_ADJ_ENABLE | PRG_ETH0_ADJ_SETUP | |
| 323 | PRG_ETH0_ADJ_DELAY | PRG_ETH0_ADJ_SKEW, |
| 324 | delay_config); |
| 325 | |
Martin Blumenstingl | 7985244 | 2021-01-06 14:42:50 +0100 | [diff] [blame^] | 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | static int meson8b_init_prg_eth(struct meson8b_dwmac *dwmac) |
| 330 | { |
| 331 | int ret; |
| 332 | |
Martin Blumenstingl | 9308c476 | 2020-05-12 23:11:03 +0200 | [diff] [blame] | 333 | if (phy_interface_mode_is_rgmii(dwmac->phy_mode)) { |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 334 | /* only relevant for RMII mode -> disable in RGMII mode */ |
| 335 | meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, |
| 336 | PRG_ETH0_INVERTED_RMII_CLK, 0); |
| 337 | |
Martin Blumenstingl | 4f6a71b | 2018-01-15 18:10:13 +0100 | [diff] [blame] | 338 | /* Configure the 125MHz RGMII TX clock, the IP block changes |
| 339 | * the output automatically (= without us having to configure |
| 340 | * a register) based on the line-speed (125MHz for Gbit speeds, |
| 341 | * 25MHz for 100Mbit/s and 2.5MHz for 10Mbit/s). |
| 342 | */ |
Martin Blumenstingl | 11184a5 | 2018-02-17 15:08:18 +0100 | [diff] [blame] | 343 | ret = clk_set_rate(dwmac->rgmii_tx_clk, 125 * 1000 * 1000); |
Martin Blumenstingl | 37512b4 | 2018-01-15 18:10:12 +0100 | [diff] [blame] | 344 | if (ret) { |
Martin Blumenstingl | b756371 | 2018-02-17 15:08:19 +0100 | [diff] [blame] | 345 | dev_err(dwmac->dev, |
Martin Blumenstingl | 4f6a71b | 2018-01-15 18:10:13 +0100 | [diff] [blame] | 346 | "failed to set RGMII TX clock\n"); |
Martin Blumenstingl | 37512b4 | 2018-01-15 18:10:12 +0100 | [diff] [blame] | 347 | return ret; |
| 348 | } |
| 349 | |
Martin Blumenstingl | a54dc4a | 2020-05-12 23:11:02 +0200 | [diff] [blame] | 350 | ret = meson8b_devm_clk_prepare_enable(dwmac, |
| 351 | dwmac->rgmii_tx_clk); |
Martin Blumenstingl | 37512b4 | 2018-01-15 18:10:12 +0100 | [diff] [blame] | 352 | if (ret) { |
Martin Blumenstingl | b756371 | 2018-02-17 15:08:19 +0100 | [diff] [blame] | 353 | dev_err(dwmac->dev, |
Martin Blumenstingl | 4f6a71b | 2018-01-15 18:10:13 +0100 | [diff] [blame] | 354 | "failed to enable the RGMII TX clock\n"); |
Martin Blumenstingl | 37512b4 | 2018-01-15 18:10:12 +0100 | [diff] [blame] | 355 | return ret; |
| 356 | } |
Martin Blumenstingl | 9308c476 | 2020-05-12 23:11:03 +0200 | [diff] [blame] | 357 | } else { |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 358 | /* invert internal clk_rmii_i to generate 25/2.5 tx_rx_clk */ |
| 359 | meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, |
| 360 | PRG_ETH0_INVERTED_RMII_CLK, |
| 361 | PRG_ETH0_INVERTED_RMII_CLK); |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 362 | } |
| 363 | |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 364 | /* enable TX_CLK and PHY_REF_CLK generator */ |
| 365 | meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, PRG_ETH0_TX_AND_PHY_REF_CLK, |
| 366 | PRG_ETH0_TX_AND_PHY_REF_CLK); |
| 367 | |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | static int meson8b_dwmac_probe(struct platform_device *pdev) |
| 372 | { |
| 373 | struct plat_stmmacenet_data *plat_dat; |
| 374 | struct stmmac_resources stmmac_res; |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 375 | struct meson8b_dwmac *dwmac; |
| 376 | int ret; |
| 377 | |
| 378 | ret = stmmac_get_platform_resources(pdev, &stmmac_res); |
| 379 | if (ret) |
| 380 | return ret; |
| 381 | |
| 382 | plat_dat = stmmac_probe_config_dt(pdev, &stmmac_res.mac); |
| 383 | if (IS_ERR(plat_dat)) |
| 384 | return PTR_ERR(plat_dat); |
| 385 | |
| 386 | dwmac = devm_kzalloc(&pdev->dev, sizeof(*dwmac), GFP_KERNEL); |
Johan Hovold | d2ed0a7 | 2016-11-30 15:29:55 +0100 | [diff] [blame] | 387 | if (!dwmac) { |
| 388 | ret = -ENOMEM; |
| 389 | goto err_remove_config_dt; |
| 390 | } |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 391 | |
Yixun Lan | efacb56 | 2018-04-28 10:21:11 +0000 | [diff] [blame] | 392 | dwmac->data = (const struct meson8b_dwmac_data *) |
| 393 | of_device_get_match_data(&pdev->dev); |
Christophe JAILLET | 760a6ed | 2018-06-11 19:52:27 +0200 | [diff] [blame] | 394 | if (!dwmac->data) { |
| 395 | ret = -EINVAL; |
| 396 | goto err_remove_config_dt; |
| 397 | } |
YueHaibing | 999232a | 2019-08-21 21:51:30 +0800 | [diff] [blame] | 398 | dwmac->regs = devm_platform_ioremap_resource(pdev, 1); |
Johan Hovold | d2ed0a7 | 2016-11-30 15:29:55 +0100 | [diff] [blame] | 399 | if (IS_ERR(dwmac->regs)) { |
| 400 | ret = PTR_ERR(dwmac->regs); |
| 401 | goto err_remove_config_dt; |
| 402 | } |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 403 | |
Martin Blumenstingl | b756371 | 2018-02-17 15:08:19 +0100 | [diff] [blame] | 404 | dwmac->dev = &pdev->dev; |
Andrew Lunn | 0c65b2b | 2019-11-04 02:40:33 +0100 | [diff] [blame] | 405 | ret = of_get_phy_mode(pdev->dev.of_node, &dwmac->phy_mode); |
| 406 | if (ret) { |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 407 | dev_err(&pdev->dev, "missing phy-mode property\n"); |
Johan Hovold | d2ed0a7 | 2016-11-30 15:29:55 +0100 | [diff] [blame] | 408 | goto err_remove_config_dt; |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 409 | } |
| 410 | |
Martin Blumenstingl | b765234 | 2017-01-22 23:02:46 +0100 | [diff] [blame] | 411 | /* use 2ns as fallback since this value was previously hardcoded */ |
| 412 | if (of_property_read_u32(pdev->dev.of_node, "amlogic,tx-delay-ns", |
| 413 | &dwmac->tx_delay_ns)) |
| 414 | dwmac->tx_delay_ns = 2; |
| 415 | |
Martin Blumenstingl | 140ddf0 | 2021-01-06 14:42:49 +0100 | [diff] [blame] | 416 | /* RX delay defaults to 0ps since this is what many boards use */ |
| 417 | if (of_property_read_u32(pdev->dev.of_node, "rx-internal-delay-ps", |
| 418 | &dwmac->rx_delay_ps)) { |
| 419 | if (!of_property_read_u32(pdev->dev.of_node, |
| 420 | "amlogic,rx-delay-ns", |
| 421 | &dwmac->rx_delay_ps)) |
| 422 | /* convert ns to ps */ |
| 423 | dwmac->rx_delay_ps *= 1000; |
| 424 | } |
Martin Blumenstingl | 9308c476 | 2020-05-12 23:11:03 +0200 | [diff] [blame] | 425 | |
Martin Blumenstingl | 140ddf0 | 2021-01-06 14:42:49 +0100 | [diff] [blame] | 426 | if (dwmac->rx_delay_ps != 0 && dwmac->rx_delay_ps != 2000) { |
Martin Blumenstingl | 9308c476 | 2020-05-12 23:11:03 +0200 | [diff] [blame] | 427 | dev_err(&pdev->dev, |
Martin Blumenstingl | 140ddf0 | 2021-01-06 14:42:49 +0100 | [diff] [blame] | 428 | "The only allowed RX delays values are: 0ps, 2000ps"); |
Martin Blumenstingl | 9308c476 | 2020-05-12 23:11:03 +0200 | [diff] [blame] | 429 | ret = -EINVAL; |
| 430 | goto err_remove_config_dt; |
| 431 | } |
| 432 | |
Martin Blumenstingl | e4227bf | 2020-05-12 23:11:01 +0200 | [diff] [blame] | 433 | dwmac->timing_adj_clk = devm_clk_get_optional(dwmac->dev, |
| 434 | "timing-adjustment"); |
| 435 | if (IS_ERR(dwmac->timing_adj_clk)) { |
| 436 | ret = PTR_ERR(dwmac->timing_adj_clk); |
| 437 | goto err_remove_config_dt; |
| 438 | } |
| 439 | |
Martin Blumenstingl | 7985244 | 2021-01-06 14:42:50 +0100 | [diff] [blame^] | 440 | ret = meson8b_init_rgmii_delays(dwmac); |
| 441 | if (ret) |
| 442 | goto err_remove_config_dt; |
| 443 | |
Martin Blumenstingl | 37512b4 | 2018-01-15 18:10:12 +0100 | [diff] [blame] | 444 | ret = meson8b_init_rgmii_tx_clk(dwmac); |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 445 | if (ret) |
Johan Hovold | d2ed0a7 | 2016-11-30 15:29:55 +0100 | [diff] [blame] | 446 | goto err_remove_config_dt; |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 447 | |
Yixun Lan | efacb56 | 2018-04-28 10:21:11 +0000 | [diff] [blame] | 448 | ret = dwmac->data->set_phy_mode(dwmac); |
| 449 | if (ret) |
| 450 | goto err_remove_config_dt; |
| 451 | |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 452 | ret = meson8b_init_prg_eth(dwmac); |
| 453 | if (ret) |
Johan Hovold | d2ed0a7 | 2016-11-30 15:29:55 +0100 | [diff] [blame] | 454 | goto err_remove_config_dt; |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 455 | |
| 456 | plat_dat->bsp_priv = dwmac; |
| 457 | |
Johan Hovold | 5cc70bb | 2016-11-30 15:29:53 +0100 | [diff] [blame] | 458 | ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res); |
| 459 | if (ret) |
Martin Blumenstingl | 11184a5 | 2018-02-17 15:08:18 +0100 | [diff] [blame] | 460 | goto err_remove_config_dt; |
Johan Hovold | 5cc70bb | 2016-11-30 15:29:53 +0100 | [diff] [blame] | 461 | |
| 462 | return 0; |
| 463 | |
Johan Hovold | d2ed0a7 | 2016-11-30 15:29:55 +0100 | [diff] [blame] | 464 | err_remove_config_dt: |
| 465 | stmmac_remove_config_dt(pdev, plat_dat); |
Johan Hovold | 5cc70bb | 2016-11-30 15:29:53 +0100 | [diff] [blame] | 466 | |
| 467 | return ret; |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 468 | } |
| 469 | |
Yixun Lan | efacb56 | 2018-04-28 10:21:11 +0000 | [diff] [blame] | 470 | static const struct meson8b_dwmac_data meson8b_dwmac_data = { |
| 471 | .set_phy_mode = meson8b_set_phy_mode, |
| 472 | }; |
| 473 | |
| 474 | static const struct meson8b_dwmac_data meson_axg_dwmac_data = { |
| 475 | .set_phy_mode = meson_axg_set_phy_mode, |
| 476 | }; |
| 477 | |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 478 | static const struct of_device_id meson8b_dwmac_match[] = { |
Yixun Lan | efacb56 | 2018-04-28 10:21:11 +0000 | [diff] [blame] | 479 | { |
| 480 | .compatible = "amlogic,meson8b-dwmac", |
| 481 | .data = &meson8b_dwmac_data, |
| 482 | }, |
| 483 | { |
| 484 | .compatible = "amlogic,meson8m2-dwmac", |
| 485 | .data = &meson8b_dwmac_data, |
| 486 | }, |
| 487 | { |
| 488 | .compatible = "amlogic,meson-gxbb-dwmac", |
| 489 | .data = &meson8b_dwmac_data, |
| 490 | }, |
| 491 | { |
| 492 | .compatible = "amlogic,meson-axg-dwmac", |
| 493 | .data = &meson_axg_dwmac_data, |
| 494 | }, |
Martin Blumenstingl | a4f6334 | 2020-06-20 21:26:41 +0200 | [diff] [blame] | 495 | { |
| 496 | .compatible = "amlogic,meson-g12a-dwmac", |
| 497 | .data = &meson_axg_dwmac_data, |
| 498 | }, |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 499 | { } |
| 500 | }; |
| 501 | MODULE_DEVICE_TABLE(of, meson8b_dwmac_match); |
| 502 | |
| 503 | static struct platform_driver meson8b_dwmac_driver = { |
| 504 | .probe = meson8b_dwmac_probe, |
Martin Blumenstingl | 11184a5 | 2018-02-17 15:08:18 +0100 | [diff] [blame] | 505 | .remove = stmmac_pltfr_remove, |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 506 | .driver = { |
| 507 | .name = "meson8b-dwmac", |
| 508 | .pm = &stmmac_pltfr_pm_ops, |
| 509 | .of_match_table = meson8b_dwmac_match, |
| 510 | }, |
| 511 | }; |
| 512 | module_platform_driver(meson8b_dwmac_driver); |
| 513 | |
| 514 | MODULE_AUTHOR("Martin Blumenstingl <martin.blumenstingl@googlemail.com>"); |
Martin Blumenstingl | 7676693 | 2018-03-30 01:00:35 +0200 | [diff] [blame] | 515 | MODULE_DESCRIPTION("Amlogic Meson8b, Meson8m2 and GXBB DWMAC glue layer"); |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 516 | MODULE_LICENSE("GPL v2"); |