blob: 7cb794094a70665ea009f1b7e2160cead7186012 [file] [log] [blame]
Martin Blumenstingl566e8252016-09-06 23:38:46 +02001/*
Martin Blumenstingl76766932018-03-30 01:00:35 +02002 * Amlogic Meson8b, Meson8m2 and GXBB DWMAC glue layer
Martin Blumenstingl566e8252016-09-06 23:38:46 +02003 *
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 Blumenstingl566e8252016-09-06 23:38:46 +020038
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
Martin Blumenstingl4f6a71b2018-01-15 18:10:13 +010043#define PRG_ETH0_RGMII_TX_CLK_EN 10
Martin Blumenstingl566e8252016-09-06 23:38:46 +020044
45#define PRG_ETH0_INVERTED_RMII_CLK BIT(11)
46#define PRG_ETH0_TX_AND_PHY_REF_CLK BIT(12)
47
48#define MUX_CLK_NUM_PARENTS 2
49
50struct meson8b_dwmac {
Martin Blumenstinglb7563712018-02-17 15:08:19 +010051 struct device *dev;
Martin Blumenstingl566e8252016-09-06 23:38:46 +020052 void __iomem *regs;
Martin Blumenstingl566e8252016-09-06 23:38:46 +020053 phy_interface_t phy_mode;
Martin Blumenstingl80767592018-02-17 15:08:20 +010054 struct clk *rgmii_tx_clk;
55 u32 tx_delay_ns;
56};
Martin Blumenstingl566e8252016-09-06 23:38:46 +020057
Martin Blumenstingl80767592018-02-17 15:08:20 +010058struct meson8b_dwmac_clk_configs {
Martin Blumenstingl566e8252016-09-06 23:38:46 +020059 struct clk_mux m250_mux;
Martin Blumenstingl566e8252016-09-06 23:38:46 +020060 struct clk_divider m250_div;
Martin Blumenstingl4f6a71b2018-01-15 18:10:13 +010061 struct clk_fixed_factor fixed_div2;
Martin Blumenstingl4f6a71b2018-01-15 18:10:13 +010062 struct clk_gate rgmii_tx_en;
Martin Blumenstingl566e8252016-09-06 23:38:46 +020063};
64
65static void meson8b_dwmac_mask_bits(struct meson8b_dwmac *dwmac, u32 reg,
66 u32 mask, u32 value)
67{
68 u32 data;
69
70 data = readl(dwmac->regs + reg);
71 data &= ~mask;
72 data |= (value & mask);
73
74 writel(data, dwmac->regs + reg);
75}
76
Martin Blumenstingl11184a52018-02-17 15:08:18 +010077static struct clk *meson8b_dwmac_register_clk(struct meson8b_dwmac *dwmac,
78 const char *name_suffix,
79 const char **parent_names,
80 int num_parents,
81 const struct clk_ops *ops,
82 struct clk_hw *hw)
83{
Martin Blumenstingl11184a52018-02-17 15:08:18 +010084 struct clk_init_data init;
85 char clk_name[32];
86
Martin Blumenstinglb7563712018-02-17 15:08:19 +010087 snprintf(clk_name, sizeof(clk_name), "%s#%s", dev_name(dwmac->dev),
Martin Blumenstingl11184a52018-02-17 15:08:18 +010088 name_suffix);
89
90 init.name = clk_name;
91 init.ops = ops;
92 init.flags = CLK_SET_RATE_PARENT;
93 init.parent_names = parent_names;
94 init.num_parents = num_parents;
95
96 hw->init = &init;
97
Martin Blumenstinglb7563712018-02-17 15:08:19 +010098 return devm_clk_register(dwmac->dev, hw);
Martin Blumenstingl11184a52018-02-17 15:08:18 +010099}
100
Martin Blumenstingl37512b42018-01-15 18:10:12 +0100101static int meson8b_init_rgmii_tx_clk(struct meson8b_dwmac *dwmac)
Martin Blumenstingl566e8252016-09-06 23:38:46 +0200102{
Martin Blumenstingl566e8252016-09-06 23:38:46 +0200103 int i, ret;
Martin Blumenstingl11184a52018-02-17 15:08:18 +0100104 struct clk *clk;
Martin Blumenstinglb7563712018-02-17 15:08:19 +0100105 struct device *dev = dwmac->dev;
Martin Blumenstingl11184a52018-02-17 15:08:18 +0100106 const char *parent_name, *mux_parent_names[MUX_CLK_NUM_PARENTS];
Martin Blumenstingl80767592018-02-17 15:08:20 +0100107 struct meson8b_dwmac_clk_configs *clk_configs;
108
109 clk_configs = devm_kzalloc(dev, sizeof(*clk_configs), GFP_KERNEL);
110 if (!clk_configs)
111 return -ENOMEM;
Martin Blumenstingl566e8252016-09-06 23:38:46 +0200112
113 /* get the mux parents from DT */
114 for (i = 0; i < MUX_CLK_NUM_PARENTS; i++) {
115 char name[16];
116
117 snprintf(name, sizeof(name), "clkin%d", i);
Martin Blumenstingl11184a52018-02-17 15:08:18 +0100118 clk = devm_clk_get(dev, name);
119 if (IS_ERR(clk)) {
120 ret = PTR_ERR(clk);
Martin Blumenstingl566e8252016-09-06 23:38:46 +0200121 if (ret != -EPROBE_DEFER)
122 dev_err(dev, "Missing clock %s\n", name);
123 return ret;
124 }
125
Martin Blumenstingl11184a52018-02-17 15:08:18 +0100126 mux_parent_names[i] = __clk_get_name(clk);
Martin Blumenstingl566e8252016-09-06 23:38:46 +0200127 }
128
Martin Blumenstingl80767592018-02-17 15:08:20 +0100129 clk_configs->m250_mux.reg = dwmac->regs + PRG_ETH0;
130 clk_configs->m250_mux.shift = PRG_ETH0_CLK_M250_SEL_SHIFT;
131 clk_configs->m250_mux.mask = PRG_ETH0_CLK_M250_SEL_MASK;
Martin Blumenstingl11184a52018-02-17 15:08:18 +0100132 clk = meson8b_dwmac_register_clk(dwmac, "m250_sel", mux_parent_names,
133 MUX_CLK_NUM_PARENTS, &clk_mux_ops,
Martin Blumenstingl80767592018-02-17 15:08:20 +0100134 &clk_configs->m250_mux.hw);
Martin Blumenstingl11184a52018-02-17 15:08:18 +0100135 if (WARN_ON(IS_ERR(clk)))
136 return PTR_ERR(clk);
Martin Blumenstingl566e8252016-09-06 23:38:46 +0200137
Martin Blumenstingl11184a52018-02-17 15:08:18 +0100138 parent_name = __clk_get_name(clk);
Martin Blumenstingl80767592018-02-17 15:08:20 +0100139 clk_configs->m250_div.reg = dwmac->regs + PRG_ETH0;
140 clk_configs->m250_div.shift = PRG_ETH0_CLK_M250_DIV_SHIFT;
141 clk_configs->m250_div.width = PRG_ETH0_CLK_M250_DIV_WIDTH;
142 clk_configs->m250_div.flags = CLK_DIVIDER_ONE_BASED |
Martin Blumenstingl433c6ca2018-01-15 18:10:14 +0100143 CLK_DIVIDER_ALLOW_ZERO |
144 CLK_DIVIDER_ROUND_CLOSEST;
Martin Blumenstingl11184a52018-02-17 15:08:18 +0100145 clk = meson8b_dwmac_register_clk(dwmac, "m250_div", &parent_name, 1,
146 &clk_divider_ops,
Martin Blumenstingl80767592018-02-17 15:08:20 +0100147 &clk_configs->m250_div.hw);
Martin Blumenstingl11184a52018-02-17 15:08:18 +0100148 if (WARN_ON(IS_ERR(clk)))
149 return PTR_ERR(clk);
Martin Blumenstingl566e8252016-09-06 23:38:46 +0200150
Martin Blumenstingl11184a52018-02-17 15:08:18 +0100151 parent_name = __clk_get_name(clk);
Martin Blumenstingl80767592018-02-17 15:08:20 +0100152 clk_configs->fixed_div2.mult = 1;
153 clk_configs->fixed_div2.div = 2;
Martin Blumenstingl11184a52018-02-17 15:08:18 +0100154 clk = meson8b_dwmac_register_clk(dwmac, "fixed_div2", &parent_name, 1,
155 &clk_fixed_factor_ops,
Martin Blumenstingl80767592018-02-17 15:08:20 +0100156 &clk_configs->fixed_div2.hw);
Martin Blumenstingl11184a52018-02-17 15:08:18 +0100157 if (WARN_ON(IS_ERR(clk)))
158 return PTR_ERR(clk);
Martin Blumenstingl566e8252016-09-06 23:38:46 +0200159
Martin Blumenstingl11184a52018-02-17 15:08:18 +0100160 parent_name = __clk_get_name(clk);
Martin Blumenstingl80767592018-02-17 15:08:20 +0100161 clk_configs->rgmii_tx_en.reg = dwmac->regs + PRG_ETH0;
162 clk_configs->rgmii_tx_en.bit_idx = PRG_ETH0_RGMII_TX_CLK_EN;
Martin Blumenstingl11184a52018-02-17 15:08:18 +0100163 clk = meson8b_dwmac_register_clk(dwmac, "rgmii_tx_en", &parent_name, 1,
164 &clk_gate_ops,
Martin Blumenstingl80767592018-02-17 15:08:20 +0100165 &clk_configs->rgmii_tx_en.hw);
Martin Blumenstingl11184a52018-02-17 15:08:18 +0100166 if (WARN_ON(IS_ERR(clk)))
167 return PTR_ERR(clk);
Martin Blumenstingl4f6a71b2018-01-15 18:10:13 +0100168
Martin Blumenstingl11184a52018-02-17 15:08:18 +0100169 dwmac->rgmii_tx_clk = clk;
Martin Blumenstingl566e8252016-09-06 23:38:46 +0200170
171 return 0;
172}
173
174static int meson8b_init_prg_eth(struct meson8b_dwmac *dwmac)
175{
176 int ret;
Heiner Kallweitd6db61a2017-02-01 20:19:25 +0100177 u8 tx_dly_val = 0;
Martin Blumenstingl566e8252016-09-06 23:38:46 +0200178
179 switch (dwmac->phy_mode) {
180 case PHY_INTERFACE_MODE_RGMII:
Martin Blumenstingl566e8252016-09-06 23:38:46 +0200181 case PHY_INTERFACE_MODE_RGMII_RXID:
Heiner Kallweitd6db61a2017-02-01 20:19:25 +0100182 /* TX clock delay in ns = "8ns / 4 * tx_dly_val" (where
183 * 8ns are exactly one cycle of the 125MHz RGMII TX clock):
184 * 0ns = 0x0, 2ns = 0x1, 4ns = 0x2, 6ns = 0x3
185 */
186 tx_dly_val = dwmac->tx_delay_ns >> 1;
187 /* fall through */
188
189 case PHY_INTERFACE_MODE_RGMII_ID:
Martin Blumenstingl566e8252016-09-06 23:38:46 +0200190 case PHY_INTERFACE_MODE_RGMII_TXID:
Martin Blumenstingl566e8252016-09-06 23:38:46 +0200191 /* enable RGMII mode */
192 meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, PRG_ETH0_RGMII_MODE,
193 PRG_ETH0_RGMII_MODE);
194
195 /* only relevant for RMII mode -> disable in RGMII mode */
196 meson8b_dwmac_mask_bits(dwmac, PRG_ETH0,
197 PRG_ETH0_INVERTED_RMII_CLK, 0);
198
Martin Blumenstingl566e8252016-09-06 23:38:46 +0200199 meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, PRG_ETH0_TXDLY_MASK,
Martin Blumenstinglb7652342017-01-22 23:02:46 +0100200 tx_dly_val << PRG_ETH0_TXDLY_SHIFT);
Martin Blumenstingl37512b42018-01-15 18:10:12 +0100201
Martin Blumenstingl4f6a71b2018-01-15 18:10:13 +0100202 /* Configure the 125MHz RGMII TX clock, the IP block changes
203 * the output automatically (= without us having to configure
204 * a register) based on the line-speed (125MHz for Gbit speeds,
205 * 25MHz for 100Mbit/s and 2.5MHz for 10Mbit/s).
206 */
Martin Blumenstingl11184a52018-02-17 15:08:18 +0100207 ret = clk_set_rate(dwmac->rgmii_tx_clk, 125 * 1000 * 1000);
Martin Blumenstingl37512b42018-01-15 18:10:12 +0100208 if (ret) {
Martin Blumenstinglb7563712018-02-17 15:08:19 +0100209 dev_err(dwmac->dev,
Martin Blumenstingl4f6a71b2018-01-15 18:10:13 +0100210 "failed to set RGMII TX clock\n");
Martin Blumenstingl37512b42018-01-15 18:10:12 +0100211 return ret;
212 }
213
Martin Blumenstingl11184a52018-02-17 15:08:18 +0100214 ret = clk_prepare_enable(dwmac->rgmii_tx_clk);
Martin Blumenstingl37512b42018-01-15 18:10:12 +0100215 if (ret) {
Martin Blumenstinglb7563712018-02-17 15:08:19 +0100216 dev_err(dwmac->dev,
Martin Blumenstingl4f6a71b2018-01-15 18:10:13 +0100217 "failed to enable the RGMII TX clock\n");
Martin Blumenstingl37512b42018-01-15 18:10:12 +0100218 return ret;
219 }
Martin Blumenstingl11184a52018-02-17 15:08:18 +0100220
Martin Blumenstinglb7563712018-02-17 15:08:19 +0100221 devm_add_action_or_reset(dwmac->dev,
Martin Blumenstingl11184a52018-02-17 15:08:18 +0100222 (void(*)(void *))clk_disable_unprepare,
223 dwmac->rgmii_tx_clk);
Martin Blumenstingl566e8252016-09-06 23:38:46 +0200224 break;
225
226 case PHY_INTERFACE_MODE_RMII:
Martin Blumenstingl566e8252016-09-06 23:38:46 +0200227 /* disable RGMII mode -> enables RMII mode */
228 meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, PRG_ETH0_RGMII_MODE,
229 0);
230
231 /* invert internal clk_rmii_i to generate 25/2.5 tx_rx_clk */
232 meson8b_dwmac_mask_bits(dwmac, PRG_ETH0,
233 PRG_ETH0_INVERTED_RMII_CLK,
234 PRG_ETH0_INVERTED_RMII_CLK);
235
236 /* TX clock delay cannot be configured in RMII mode */
237 meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, PRG_ETH0_TXDLY_MASK,
238 0);
239
240 break;
241
242 default:
Martin Blumenstinglb7563712018-02-17 15:08:19 +0100243 dev_err(dwmac->dev, "unsupported phy-mode %s\n",
Martin Blumenstingl566e8252016-09-06 23:38:46 +0200244 phy_modes(dwmac->phy_mode));
245 return -EINVAL;
246 }
247
Martin Blumenstingl566e8252016-09-06 23:38:46 +0200248 /* enable TX_CLK and PHY_REF_CLK generator */
249 meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, PRG_ETH0_TX_AND_PHY_REF_CLK,
250 PRG_ETH0_TX_AND_PHY_REF_CLK);
251
252 return 0;
253}
254
255static int meson8b_dwmac_probe(struct platform_device *pdev)
256{
257 struct plat_stmmacenet_data *plat_dat;
258 struct stmmac_resources stmmac_res;
259 struct resource *res;
260 struct meson8b_dwmac *dwmac;
261 int ret;
262
263 ret = stmmac_get_platform_resources(pdev, &stmmac_res);
264 if (ret)
265 return ret;
266
267 plat_dat = stmmac_probe_config_dt(pdev, &stmmac_res.mac);
268 if (IS_ERR(plat_dat))
269 return PTR_ERR(plat_dat);
270
271 dwmac = devm_kzalloc(&pdev->dev, sizeof(*dwmac), GFP_KERNEL);
Johan Hovoldd2ed0a72016-11-30 15:29:55 +0100272 if (!dwmac) {
273 ret = -ENOMEM;
274 goto err_remove_config_dt;
275 }
Martin Blumenstingl566e8252016-09-06 23:38:46 +0200276
277 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
278 dwmac->regs = devm_ioremap_resource(&pdev->dev, res);
Johan Hovoldd2ed0a72016-11-30 15:29:55 +0100279 if (IS_ERR(dwmac->regs)) {
280 ret = PTR_ERR(dwmac->regs);
281 goto err_remove_config_dt;
282 }
Martin Blumenstingl566e8252016-09-06 23:38:46 +0200283
Martin Blumenstinglb7563712018-02-17 15:08:19 +0100284 dwmac->dev = &pdev->dev;
Martin Blumenstingl566e8252016-09-06 23:38:46 +0200285 dwmac->phy_mode = of_get_phy_mode(pdev->dev.of_node);
286 if (dwmac->phy_mode < 0) {
287 dev_err(&pdev->dev, "missing phy-mode property\n");
Johan Hovoldd2ed0a72016-11-30 15:29:55 +0100288 ret = -EINVAL;
289 goto err_remove_config_dt;
Martin Blumenstingl566e8252016-09-06 23:38:46 +0200290 }
291
Martin Blumenstinglb7652342017-01-22 23:02:46 +0100292 /* use 2ns as fallback since this value was previously hardcoded */
293 if (of_property_read_u32(pdev->dev.of_node, "amlogic,tx-delay-ns",
294 &dwmac->tx_delay_ns))
295 dwmac->tx_delay_ns = 2;
296
Martin Blumenstingl37512b42018-01-15 18:10:12 +0100297 ret = meson8b_init_rgmii_tx_clk(dwmac);
Martin Blumenstingl566e8252016-09-06 23:38:46 +0200298 if (ret)
Johan Hovoldd2ed0a72016-11-30 15:29:55 +0100299 goto err_remove_config_dt;
Martin Blumenstingl566e8252016-09-06 23:38:46 +0200300
301 ret = meson8b_init_prg_eth(dwmac);
302 if (ret)
Johan Hovoldd2ed0a72016-11-30 15:29:55 +0100303 goto err_remove_config_dt;
Martin Blumenstingl566e8252016-09-06 23:38:46 +0200304
305 plat_dat->bsp_priv = dwmac;
306
Johan Hovold5cc70bb2016-11-30 15:29:53 +0100307 ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
308 if (ret)
Martin Blumenstingl11184a52018-02-17 15:08:18 +0100309 goto err_remove_config_dt;
Johan Hovold5cc70bb2016-11-30 15:29:53 +0100310
311 return 0;
312
Johan Hovoldd2ed0a72016-11-30 15:29:55 +0100313err_remove_config_dt:
314 stmmac_remove_config_dt(pdev, plat_dat);
Johan Hovold5cc70bb2016-11-30 15:29:53 +0100315
316 return ret;
Martin Blumenstingl566e8252016-09-06 23:38:46 +0200317}
318
Martin Blumenstingl566e8252016-09-06 23:38:46 +0200319static const struct of_device_id meson8b_dwmac_match[] = {
320 { .compatible = "amlogic,meson8b-dwmac" },
Martin Blumenstingl76766932018-03-30 01:00:35 +0200321 { .compatible = "amlogic,meson8m2-dwmac" },
Martin Blumenstingl566e8252016-09-06 23:38:46 +0200322 { .compatible = "amlogic,meson-gxbb-dwmac" },
323 { }
324};
325MODULE_DEVICE_TABLE(of, meson8b_dwmac_match);
326
327static struct platform_driver meson8b_dwmac_driver = {
328 .probe = meson8b_dwmac_probe,
Martin Blumenstingl11184a52018-02-17 15:08:18 +0100329 .remove = stmmac_pltfr_remove,
Martin Blumenstingl566e8252016-09-06 23:38:46 +0200330 .driver = {
331 .name = "meson8b-dwmac",
332 .pm = &stmmac_pltfr_pm_ops,
333 .of_match_table = meson8b_dwmac_match,
334 },
335};
336module_platform_driver(meson8b_dwmac_driver);
337
338MODULE_AUTHOR("Martin Blumenstingl <martin.blumenstingl@googlemail.com>");
Martin Blumenstingl76766932018-03-30 01:00:35 +0200339MODULE_DESCRIPTION("Amlogic Meson8b, Meson8m2 and GXBB DWMAC glue layer");
Martin Blumenstingl566e8252016-09-06 23:38:46 +0200340MODULE_LICENSE("GPL v2");