Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Amlogic Meson8b and GXBB DWMAC glue layer |
| 3 | * |
| 4 | * Copyright (C) 2016 Martin Blumenstingl <martin.blumenstingl@googlemail.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * You should have received a copy of the GNU General Public License |
| 11 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/clk.h> |
| 15 | #include <linux/clk-provider.h> |
| 16 | #include <linux/device.h> |
| 17 | #include <linux/ethtool.h> |
| 18 | #include <linux/io.h> |
| 19 | #include <linux/ioport.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/of_net.h> |
| 22 | #include <linux/mfd/syscon.h> |
| 23 | #include <linux/platform_device.h> |
| 24 | #include <linux/stmmac.h> |
| 25 | |
| 26 | #include "stmmac_platform.h" |
| 27 | |
| 28 | #define PRG_ETH0 0x0 |
| 29 | |
| 30 | #define PRG_ETH0_RGMII_MODE BIT(0) |
| 31 | |
| 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) |
| 35 | |
| 36 | #define PRG_ETH0_TXDLY_SHIFT 5 |
| 37 | #define PRG_ETH0_TXDLY_MASK GENMASK(6, 5) |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 38 | |
| 39 | /* divider for the result of m250_sel */ |
| 40 | #define PRG_ETH0_CLK_M250_DIV_SHIFT 7 |
| 41 | #define PRG_ETH0_CLK_M250_DIV_WIDTH 3 |
| 42 | |
| 43 | /* divides the result of m25_sel by either 5 (bit unset) or 10 (bit set) */ |
| 44 | #define PRG_ETH0_CLK_M25_DIV_SHIFT 10 |
| 45 | #define PRG_ETH0_CLK_M25_DIV_WIDTH 1 |
| 46 | |
| 47 | #define PRG_ETH0_INVERTED_RMII_CLK BIT(11) |
| 48 | #define PRG_ETH0_TX_AND_PHY_REF_CLK BIT(12) |
| 49 | |
| 50 | #define MUX_CLK_NUM_PARENTS 2 |
| 51 | |
| 52 | struct meson8b_dwmac { |
| 53 | struct platform_device *pdev; |
| 54 | |
| 55 | void __iomem *regs; |
| 56 | |
| 57 | phy_interface_t phy_mode; |
| 58 | |
| 59 | struct clk_mux m250_mux; |
| 60 | struct clk *m250_mux_clk; |
| 61 | struct clk *m250_mux_parent[MUX_CLK_NUM_PARENTS]; |
| 62 | |
| 63 | struct clk_divider m250_div; |
| 64 | struct clk *m250_div_clk; |
| 65 | |
| 66 | struct clk_divider m25_div; |
| 67 | struct clk *m25_div_clk; |
Martin Blumenstingl | b765234 | 2017-01-22 23:02:46 +0100 | [diff] [blame] | 68 | |
| 69 | u32 tx_delay_ns; |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | static void meson8b_dwmac_mask_bits(struct meson8b_dwmac *dwmac, u32 reg, |
| 73 | u32 mask, u32 value) |
| 74 | { |
| 75 | u32 data; |
| 76 | |
| 77 | data = readl(dwmac->regs + reg); |
| 78 | data &= ~mask; |
| 79 | data |= (value & mask); |
| 80 | |
| 81 | writel(data, dwmac->regs + reg); |
| 82 | } |
| 83 | |
Martin Blumenstingl | 37512b4 | 2018-01-15 18:10:12 +0100 | [diff] [blame^] | 84 | static int meson8b_init_rgmii_tx_clk(struct meson8b_dwmac *dwmac) |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 85 | { |
| 86 | struct clk_init_data init; |
| 87 | int i, ret; |
| 88 | struct device *dev = &dwmac->pdev->dev; |
| 89 | char clk_name[32]; |
| 90 | const char *clk_div_parents[1]; |
| 91 | const char *mux_parent_names[MUX_CLK_NUM_PARENTS]; |
Arvind Yadav | 22eac91 | 2017-08-28 11:22:20 +0530 | [diff] [blame] | 92 | static const struct clk_div_table clk_25m_div_table[] = { |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 93 | { .val = 0, .div = 5 }, |
| 94 | { .val = 1, .div = 10 }, |
| 95 | { /* sentinel */ }, |
| 96 | }; |
| 97 | |
| 98 | /* get the mux parents from DT */ |
| 99 | for (i = 0; i < MUX_CLK_NUM_PARENTS; i++) { |
| 100 | char name[16]; |
| 101 | |
| 102 | snprintf(name, sizeof(name), "clkin%d", i); |
| 103 | dwmac->m250_mux_parent[i] = devm_clk_get(dev, name); |
| 104 | if (IS_ERR(dwmac->m250_mux_parent[i])) { |
| 105 | ret = PTR_ERR(dwmac->m250_mux_parent[i]); |
| 106 | if (ret != -EPROBE_DEFER) |
| 107 | dev_err(dev, "Missing clock %s\n", name); |
| 108 | return ret; |
| 109 | } |
| 110 | |
| 111 | mux_parent_names[i] = |
| 112 | __clk_get_name(dwmac->m250_mux_parent[i]); |
| 113 | } |
| 114 | |
| 115 | /* create the m250_mux */ |
| 116 | snprintf(clk_name, sizeof(clk_name), "%s#m250_sel", dev_name(dev)); |
| 117 | init.name = clk_name; |
| 118 | init.ops = &clk_mux_ops; |
| 119 | init.flags = 0; |
| 120 | init.parent_names = mux_parent_names; |
| 121 | init.num_parents = MUX_CLK_NUM_PARENTS; |
| 122 | |
| 123 | dwmac->m250_mux.reg = dwmac->regs + PRG_ETH0; |
| 124 | dwmac->m250_mux.shift = PRG_ETH0_CLK_M250_SEL_SHIFT; |
| 125 | dwmac->m250_mux.mask = PRG_ETH0_CLK_M250_SEL_MASK; |
| 126 | dwmac->m250_mux.flags = 0; |
| 127 | dwmac->m250_mux.table = NULL; |
| 128 | dwmac->m250_mux.hw.init = &init; |
| 129 | |
| 130 | dwmac->m250_mux_clk = devm_clk_register(dev, &dwmac->m250_mux.hw); |
| 131 | if (WARN_ON(IS_ERR(dwmac->m250_mux_clk))) |
| 132 | return PTR_ERR(dwmac->m250_mux_clk); |
| 133 | |
| 134 | /* create the m250_div */ |
| 135 | snprintf(clk_name, sizeof(clk_name), "%s#m250_div", dev_name(dev)); |
| 136 | init.name = devm_kstrdup(dev, clk_name, GFP_KERNEL); |
| 137 | init.ops = &clk_divider_ops; |
| 138 | init.flags = CLK_SET_RATE_PARENT; |
| 139 | clk_div_parents[0] = __clk_get_name(dwmac->m250_mux_clk); |
| 140 | init.parent_names = clk_div_parents; |
| 141 | init.num_parents = ARRAY_SIZE(clk_div_parents); |
| 142 | |
| 143 | dwmac->m250_div.reg = dwmac->regs + PRG_ETH0; |
| 144 | dwmac->m250_div.shift = PRG_ETH0_CLK_M250_DIV_SHIFT; |
| 145 | dwmac->m250_div.width = PRG_ETH0_CLK_M250_DIV_WIDTH; |
| 146 | dwmac->m250_div.hw.init = &init; |
| 147 | dwmac->m250_div.flags = CLK_DIVIDER_ONE_BASED | CLK_DIVIDER_ALLOW_ZERO; |
| 148 | |
| 149 | dwmac->m250_div_clk = devm_clk_register(dev, &dwmac->m250_div.hw); |
| 150 | if (WARN_ON(IS_ERR(dwmac->m250_div_clk))) |
| 151 | return PTR_ERR(dwmac->m250_div_clk); |
| 152 | |
| 153 | /* create the m25_div */ |
| 154 | snprintf(clk_name, sizeof(clk_name), "%s#m25_div", dev_name(dev)); |
| 155 | init.name = devm_kstrdup(dev, clk_name, GFP_KERNEL); |
| 156 | init.ops = &clk_divider_ops; |
| 157 | init.flags = CLK_IS_BASIC | CLK_SET_RATE_PARENT; |
| 158 | clk_div_parents[0] = __clk_get_name(dwmac->m250_div_clk); |
| 159 | init.parent_names = clk_div_parents; |
| 160 | init.num_parents = ARRAY_SIZE(clk_div_parents); |
| 161 | |
| 162 | dwmac->m25_div.reg = dwmac->regs + PRG_ETH0; |
| 163 | dwmac->m25_div.shift = PRG_ETH0_CLK_M25_DIV_SHIFT; |
| 164 | dwmac->m25_div.width = PRG_ETH0_CLK_M25_DIV_WIDTH; |
| 165 | dwmac->m25_div.table = clk_25m_div_table; |
| 166 | dwmac->m25_div.hw.init = &init; |
| 167 | dwmac->m25_div.flags = CLK_DIVIDER_ALLOW_ZERO; |
| 168 | |
| 169 | dwmac->m25_div_clk = devm_clk_register(dev, &dwmac->m25_div.hw); |
| 170 | if (WARN_ON(IS_ERR(dwmac->m25_div_clk))) |
| 171 | return PTR_ERR(dwmac->m25_div_clk); |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | static int meson8b_init_prg_eth(struct meson8b_dwmac *dwmac) |
| 177 | { |
| 178 | int ret; |
Heiner Kallweit | d6db61a | 2017-02-01 20:19:25 +0100 | [diff] [blame] | 179 | u8 tx_dly_val = 0; |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 180 | |
| 181 | switch (dwmac->phy_mode) { |
| 182 | case PHY_INTERFACE_MODE_RGMII: |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 183 | case PHY_INTERFACE_MODE_RGMII_RXID: |
Heiner Kallweit | d6db61a | 2017-02-01 20:19:25 +0100 | [diff] [blame] | 184 | /* TX clock delay in ns = "8ns / 4 * tx_dly_val" (where |
| 185 | * 8ns are exactly one cycle of the 125MHz RGMII TX clock): |
| 186 | * 0ns = 0x0, 2ns = 0x1, 4ns = 0x2, 6ns = 0x3 |
| 187 | */ |
| 188 | tx_dly_val = dwmac->tx_delay_ns >> 1; |
| 189 | /* fall through */ |
| 190 | |
| 191 | case PHY_INTERFACE_MODE_RGMII_ID: |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 192 | case PHY_INTERFACE_MODE_RGMII_TXID: |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 193 | /* enable RGMII mode */ |
| 194 | meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, PRG_ETH0_RGMII_MODE, |
| 195 | PRG_ETH0_RGMII_MODE); |
| 196 | |
| 197 | /* only relevant for RMII mode -> disable in RGMII mode */ |
| 198 | meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, |
| 199 | PRG_ETH0_INVERTED_RMII_CLK, 0); |
| 200 | |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 201 | meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, PRG_ETH0_TXDLY_MASK, |
Martin Blumenstingl | b765234 | 2017-01-22 23:02:46 +0100 | [diff] [blame] | 202 | tx_dly_val << PRG_ETH0_TXDLY_SHIFT); |
Martin Blumenstingl | 37512b4 | 2018-01-15 18:10:12 +0100 | [diff] [blame^] | 203 | |
| 204 | ret = clk_prepare_enable(dwmac->m25_div_clk); |
| 205 | if (ret) { |
| 206 | dev_err(&dwmac->pdev->dev, "failed to enable the PHY clock\n"); |
| 207 | return ret; |
| 208 | } |
| 209 | |
| 210 | /* Generate the 25MHz RGMII clock for the PHY */ |
| 211 | ret = clk_set_rate(dwmac->m25_div_clk, 25 * 1000 * 1000); |
| 212 | if (ret) { |
| 213 | clk_disable_unprepare(dwmac->m25_div_clk); |
| 214 | |
| 215 | dev_err(&dwmac->pdev->dev, "failed to set PHY clock\n"); |
| 216 | return ret; |
| 217 | } |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 218 | break; |
| 219 | |
| 220 | case PHY_INTERFACE_MODE_RMII: |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 221 | /* disable RGMII mode -> enables RMII mode */ |
| 222 | meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, PRG_ETH0_RGMII_MODE, |
| 223 | 0); |
| 224 | |
| 225 | /* invert internal clk_rmii_i to generate 25/2.5 tx_rx_clk */ |
| 226 | meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, |
| 227 | PRG_ETH0_INVERTED_RMII_CLK, |
| 228 | PRG_ETH0_INVERTED_RMII_CLK); |
| 229 | |
| 230 | /* TX clock delay cannot be configured in RMII mode */ |
| 231 | meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, PRG_ETH0_TXDLY_MASK, |
| 232 | 0); |
| 233 | |
| 234 | break; |
| 235 | |
| 236 | default: |
| 237 | dev_err(&dwmac->pdev->dev, "unsupported phy-mode %s\n", |
| 238 | phy_modes(dwmac->phy_mode)); |
| 239 | return -EINVAL; |
| 240 | } |
| 241 | |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 242 | /* enable TX_CLK and PHY_REF_CLK generator */ |
| 243 | meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, PRG_ETH0_TX_AND_PHY_REF_CLK, |
| 244 | PRG_ETH0_TX_AND_PHY_REF_CLK); |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | static int meson8b_dwmac_probe(struct platform_device *pdev) |
| 250 | { |
| 251 | struct plat_stmmacenet_data *plat_dat; |
| 252 | struct stmmac_resources stmmac_res; |
| 253 | struct resource *res; |
| 254 | struct meson8b_dwmac *dwmac; |
| 255 | int ret; |
| 256 | |
| 257 | ret = stmmac_get_platform_resources(pdev, &stmmac_res); |
| 258 | if (ret) |
| 259 | return ret; |
| 260 | |
| 261 | plat_dat = stmmac_probe_config_dt(pdev, &stmmac_res.mac); |
| 262 | if (IS_ERR(plat_dat)) |
| 263 | return PTR_ERR(plat_dat); |
| 264 | |
| 265 | dwmac = devm_kzalloc(&pdev->dev, sizeof(*dwmac), GFP_KERNEL); |
Johan Hovold | d2ed0a7 | 2016-11-30 15:29:55 +0100 | [diff] [blame] | 266 | if (!dwmac) { |
| 267 | ret = -ENOMEM; |
| 268 | goto err_remove_config_dt; |
| 269 | } |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 270 | |
| 271 | res = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 272 | dwmac->regs = devm_ioremap_resource(&pdev->dev, res); |
Johan Hovold | d2ed0a7 | 2016-11-30 15:29:55 +0100 | [diff] [blame] | 273 | if (IS_ERR(dwmac->regs)) { |
| 274 | ret = PTR_ERR(dwmac->regs); |
| 275 | goto err_remove_config_dt; |
| 276 | } |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 277 | |
| 278 | dwmac->pdev = pdev; |
| 279 | dwmac->phy_mode = of_get_phy_mode(pdev->dev.of_node); |
| 280 | if (dwmac->phy_mode < 0) { |
| 281 | dev_err(&pdev->dev, "missing phy-mode property\n"); |
Johan Hovold | d2ed0a7 | 2016-11-30 15:29:55 +0100 | [diff] [blame] | 282 | ret = -EINVAL; |
| 283 | goto err_remove_config_dt; |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 284 | } |
| 285 | |
Martin Blumenstingl | b765234 | 2017-01-22 23:02:46 +0100 | [diff] [blame] | 286 | /* use 2ns as fallback since this value was previously hardcoded */ |
| 287 | if (of_property_read_u32(pdev->dev.of_node, "amlogic,tx-delay-ns", |
| 288 | &dwmac->tx_delay_ns)) |
| 289 | dwmac->tx_delay_ns = 2; |
| 290 | |
Martin Blumenstingl | 37512b4 | 2018-01-15 18:10:12 +0100 | [diff] [blame^] | 291 | ret = meson8b_init_rgmii_tx_clk(dwmac); |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 292 | if (ret) |
Johan Hovold | d2ed0a7 | 2016-11-30 15:29:55 +0100 | [diff] [blame] | 293 | goto err_remove_config_dt; |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 294 | |
| 295 | ret = meson8b_init_prg_eth(dwmac); |
| 296 | if (ret) |
Johan Hovold | d2ed0a7 | 2016-11-30 15:29:55 +0100 | [diff] [blame] | 297 | goto err_remove_config_dt; |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 298 | |
| 299 | plat_dat->bsp_priv = dwmac; |
| 300 | |
Johan Hovold | 5cc70bb | 2016-11-30 15:29:53 +0100 | [diff] [blame] | 301 | ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res); |
| 302 | if (ret) |
| 303 | goto err_clk_disable; |
| 304 | |
| 305 | return 0; |
| 306 | |
| 307 | err_clk_disable: |
Martin Blumenstingl | 37512b4 | 2018-01-15 18:10:12 +0100 | [diff] [blame^] | 308 | if (phy_interface_mode_is_rgmii(dwmac->phy_mode)) |
| 309 | clk_disable_unprepare(dwmac->m25_div_clk); |
Johan Hovold | d2ed0a7 | 2016-11-30 15:29:55 +0100 | [diff] [blame] | 310 | err_remove_config_dt: |
| 311 | stmmac_remove_config_dt(pdev, plat_dat); |
Johan Hovold | 5cc70bb | 2016-11-30 15:29:53 +0100 | [diff] [blame] | 312 | |
| 313 | return ret; |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | static int meson8b_dwmac_remove(struct platform_device *pdev) |
| 317 | { |
| 318 | struct meson8b_dwmac *dwmac = get_stmmac_bsp_priv(&pdev->dev); |
| 319 | |
Martin Blumenstingl | 37512b4 | 2018-01-15 18:10:12 +0100 | [diff] [blame^] | 320 | if (phy_interface_mode_is_rgmii(dwmac->phy_mode)) |
| 321 | clk_disable_unprepare(dwmac->m25_div_clk); |
Martin Blumenstingl | 566e825 | 2016-09-06 23:38:46 +0200 | [diff] [blame] | 322 | |
| 323 | return stmmac_pltfr_remove(pdev); |
| 324 | } |
| 325 | |
| 326 | static const struct of_device_id meson8b_dwmac_match[] = { |
| 327 | { .compatible = "amlogic,meson8b-dwmac" }, |
| 328 | { .compatible = "amlogic,meson-gxbb-dwmac" }, |
| 329 | { } |
| 330 | }; |
| 331 | MODULE_DEVICE_TABLE(of, meson8b_dwmac_match); |
| 332 | |
| 333 | static struct platform_driver meson8b_dwmac_driver = { |
| 334 | .probe = meson8b_dwmac_probe, |
| 335 | .remove = meson8b_dwmac_remove, |
| 336 | .driver = { |
| 337 | .name = "meson8b-dwmac", |
| 338 | .pm = &stmmac_pltfr_pm_ops, |
| 339 | .of_match_table = meson8b_dwmac_match, |
| 340 | }, |
| 341 | }; |
| 342 | module_platform_driver(meson8b_dwmac_driver); |
| 343 | |
| 344 | MODULE_AUTHOR("Martin Blumenstingl <martin.blumenstingl@googlemail.com>"); |
| 345 | MODULE_DESCRIPTION("Amlogic Meson8b and GXBB DWMAC glue layer"); |
| 346 | MODULE_LICENSE("GPL v2"); |