GNU Linux-libre 4.14.262-gnu1
[releases.git] / drivers / net / ethernet / stmicro / stmmac / dwmac-meson8b.c
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_MASK      GENMASK(4, 4)
34
35 #define PRG_ETH0_TXDLY_SHIFT            5
36 #define PRG_ETH0_TXDLY_MASK             GENMASK(6, 5)
37
38 /* divider for the result of m250_sel */
39 #define PRG_ETH0_CLK_M250_DIV_SHIFT     7
40 #define PRG_ETH0_CLK_M250_DIV_WIDTH     3
41
42 /* divides the result of m25_sel by either 5 (bit unset) or 10 (bit set) */
43 #define PRG_ETH0_CLK_M25_DIV_SHIFT      10
44 #define PRG_ETH0_CLK_M25_DIV_WIDTH      1
45
46 #define PRG_ETH0_INVERTED_RMII_CLK      BIT(11)
47 #define PRG_ETH0_TX_AND_PHY_REF_CLK     BIT(12)
48
49 #define MUX_CLK_NUM_PARENTS             2
50
51 struct meson8b_dwmac {
52         struct platform_device  *pdev;
53
54         void __iomem            *regs;
55
56         phy_interface_t         phy_mode;
57
58         struct clk_mux          m250_mux;
59         struct clk              *m250_mux_clk;
60         struct clk              *m250_mux_parent[MUX_CLK_NUM_PARENTS];
61
62         struct clk_divider      m250_div;
63         struct clk              *m250_div_clk;
64
65         struct clk_divider      m25_div;
66         struct clk              *m25_div_clk;
67
68         u32                     tx_delay_ns;
69 };
70
71 static void meson8b_dwmac_mask_bits(struct meson8b_dwmac *dwmac, u32 reg,
72                                     u32 mask, u32 value)
73 {
74         u32 data;
75
76         data = readl(dwmac->regs + reg);
77         data &= ~mask;
78         data |= (value & mask);
79
80         writel(data, dwmac->regs + reg);
81 }
82
83 static int meson8b_init_clk(struct meson8b_dwmac *dwmac)
84 {
85         struct clk_init_data init;
86         int i, ret;
87         struct device *dev = &dwmac->pdev->dev;
88         char clk_name[32];
89         const char *clk_div_parents[1];
90         const char *mux_parent_names[MUX_CLK_NUM_PARENTS];
91         static const struct clk_div_table clk_25m_div_table[] = {
92                 { .val = 0, .div = 5 },
93                 { .val = 1, .div = 10 },
94                 { /* sentinel */ },
95         };
96
97         /* get the mux parents from DT */
98         for (i = 0; i < MUX_CLK_NUM_PARENTS; i++) {
99                 char name[16];
100
101                 snprintf(name, sizeof(name), "clkin%d", i);
102                 dwmac->m250_mux_parent[i] = devm_clk_get(dev, name);
103                 if (IS_ERR(dwmac->m250_mux_parent[i])) {
104                         ret = PTR_ERR(dwmac->m250_mux_parent[i]);
105                         if (ret != -EPROBE_DEFER)
106                                 dev_err(dev, "Missing clock %s\n", name);
107                         return ret;
108                 }
109
110                 mux_parent_names[i] =
111                         __clk_get_name(dwmac->m250_mux_parent[i]);
112         }
113
114         /* create the m250_mux */
115         snprintf(clk_name, sizeof(clk_name), "%s#m250_sel", dev_name(dev));
116         init.name = clk_name;
117         init.ops = &clk_mux_ops;
118         init.flags = CLK_SET_RATE_PARENT;
119         init.parent_names = mux_parent_names;
120         init.num_parents = MUX_CLK_NUM_PARENTS;
121
122         dwmac->m250_mux.reg = dwmac->regs + PRG_ETH0;
123         dwmac->m250_mux.shift = __ffs(PRG_ETH0_CLK_M250_SEL_MASK);
124         dwmac->m250_mux.mask = PRG_ETH0_CLK_M250_SEL_MASK >>
125                                dwmac->m250_mux.shift;
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 |
148                                 CLK_DIVIDER_ALLOW_ZERO |
149                                 CLK_DIVIDER_ROUND_CLOSEST;
150
151         dwmac->m250_div_clk = devm_clk_register(dev, &dwmac->m250_div.hw);
152         if (WARN_ON(IS_ERR(dwmac->m250_div_clk)))
153                 return PTR_ERR(dwmac->m250_div_clk);
154
155         /* create the m25_div */
156         snprintf(clk_name, sizeof(clk_name), "%s#m25_div", dev_name(dev));
157         init.name = devm_kstrdup(dev, clk_name, GFP_KERNEL);
158         init.ops = &clk_divider_ops;
159         init.flags = CLK_IS_BASIC | CLK_SET_RATE_PARENT;
160         clk_div_parents[0] = __clk_get_name(dwmac->m250_div_clk);
161         init.parent_names = clk_div_parents;
162         init.num_parents = ARRAY_SIZE(clk_div_parents);
163
164         dwmac->m25_div.reg = dwmac->regs + PRG_ETH0;
165         dwmac->m25_div.shift = PRG_ETH0_CLK_M25_DIV_SHIFT;
166         dwmac->m25_div.width = PRG_ETH0_CLK_M25_DIV_WIDTH;
167         dwmac->m25_div.table = clk_25m_div_table;
168         dwmac->m25_div.hw.init = &init;
169         dwmac->m25_div.flags = CLK_DIVIDER_ALLOW_ZERO;
170
171         dwmac->m25_div_clk = devm_clk_register(dev, &dwmac->m25_div.hw);
172         if (WARN_ON(IS_ERR(dwmac->m25_div_clk)))
173                 return PTR_ERR(dwmac->m25_div_clk);
174
175         return 0;
176 }
177
178 static int meson8b_init_prg_eth(struct meson8b_dwmac *dwmac)
179 {
180         int ret;
181         unsigned long clk_rate;
182         u8 tx_dly_val = 0;
183
184         switch (dwmac->phy_mode) {
185         case PHY_INTERFACE_MODE_RGMII:
186         case PHY_INTERFACE_MODE_RGMII_RXID:
187                 /* TX clock delay in ns = "8ns / 4 * tx_dly_val" (where
188                  * 8ns are exactly one cycle of the 125MHz RGMII TX clock):
189                  * 0ns = 0x0, 2ns = 0x1, 4ns = 0x2, 6ns = 0x3
190                  */
191                 tx_dly_val = dwmac->tx_delay_ns >> 1;
192                 /* fall through */
193
194         case PHY_INTERFACE_MODE_RGMII_ID:
195         case PHY_INTERFACE_MODE_RGMII_TXID:
196                 /* Generate a 25MHz clock for the PHY */
197                 clk_rate = 25 * 1000 * 1000;
198
199                 /* enable RGMII mode */
200                 meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, PRG_ETH0_RGMII_MODE,
201                                         PRG_ETH0_RGMII_MODE);
202
203                 /* only relevant for RMII mode -> disable in RGMII mode */
204                 meson8b_dwmac_mask_bits(dwmac, PRG_ETH0,
205                                         PRG_ETH0_INVERTED_RMII_CLK, 0);
206
207                 meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, PRG_ETH0_TXDLY_MASK,
208                                         tx_dly_val << PRG_ETH0_TXDLY_SHIFT);
209                 break;
210
211         case PHY_INTERFACE_MODE_RMII:
212                 /* Use the rate of the mux clock for the internal RMII PHY */
213                 clk_rate = clk_get_rate(dwmac->m250_mux_clk);
214
215                 /* disable RGMII mode -> enables RMII mode */
216                 meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, PRG_ETH0_RGMII_MODE,
217                                         0);
218
219                 /* invert internal clk_rmii_i to generate 25/2.5 tx_rx_clk */
220                 meson8b_dwmac_mask_bits(dwmac, PRG_ETH0,
221                                         PRG_ETH0_INVERTED_RMII_CLK,
222                                         PRG_ETH0_INVERTED_RMII_CLK);
223
224                 /* TX clock delay cannot be configured in RMII mode */
225                 meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, PRG_ETH0_TXDLY_MASK,
226                                         0);
227
228                 break;
229
230         default:
231                 dev_err(&dwmac->pdev->dev, "unsupported phy-mode %s\n",
232                         phy_modes(dwmac->phy_mode));
233                 return -EINVAL;
234         }
235
236         ret = clk_prepare_enable(dwmac->m25_div_clk);
237         if (ret) {
238                 dev_err(&dwmac->pdev->dev, "failed to enable the PHY clock\n");
239                 return ret;
240         }
241
242         ret = clk_set_rate(dwmac->m25_div_clk, clk_rate);
243         if (ret) {
244                 clk_disable_unprepare(dwmac->m25_div_clk);
245
246                 dev_err(&dwmac->pdev->dev, "failed to set PHY clock\n");
247                 return ret;
248         }
249
250         /* enable TX_CLK and PHY_REF_CLK generator */
251         meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, PRG_ETH0_TX_AND_PHY_REF_CLK,
252                                 PRG_ETH0_TX_AND_PHY_REF_CLK);
253
254         return 0;
255 }
256
257 static int meson8b_dwmac_probe(struct platform_device *pdev)
258 {
259         struct plat_stmmacenet_data *plat_dat;
260         struct stmmac_resources stmmac_res;
261         struct resource *res;
262         struct meson8b_dwmac *dwmac;
263         int ret;
264
265         ret = stmmac_get_platform_resources(pdev, &stmmac_res);
266         if (ret)
267                 return ret;
268
269         plat_dat = stmmac_probe_config_dt(pdev, &stmmac_res.mac);
270         if (IS_ERR(plat_dat))
271                 return PTR_ERR(plat_dat);
272
273         dwmac = devm_kzalloc(&pdev->dev, sizeof(*dwmac), GFP_KERNEL);
274         if (!dwmac) {
275                 ret = -ENOMEM;
276                 goto err_remove_config_dt;
277         }
278
279         res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
280         dwmac->regs = devm_ioremap_resource(&pdev->dev, res);
281         if (IS_ERR(dwmac->regs)) {
282                 ret = PTR_ERR(dwmac->regs);
283                 goto err_remove_config_dt;
284         }
285
286         dwmac->pdev = pdev;
287         dwmac->phy_mode = of_get_phy_mode(pdev->dev.of_node);
288         if ((int)dwmac->phy_mode < 0) {
289                 dev_err(&pdev->dev, "missing phy-mode property\n");
290                 ret = -EINVAL;
291                 goto err_remove_config_dt;
292         }
293
294         /* use 2ns as fallback since this value was previously hardcoded */
295         if (of_property_read_u32(pdev->dev.of_node, "amlogic,tx-delay-ns",
296                                  &dwmac->tx_delay_ns))
297                 dwmac->tx_delay_ns = 2;
298
299         ret = meson8b_init_clk(dwmac);
300         if (ret)
301                 goto err_remove_config_dt;
302
303         ret = meson8b_init_prg_eth(dwmac);
304         if (ret)
305                 goto err_remove_config_dt;
306
307         plat_dat->bsp_priv = dwmac;
308
309         ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
310         if (ret)
311                 goto err_clk_disable;
312
313         return 0;
314
315 err_clk_disable:
316         clk_disable_unprepare(dwmac->m25_div_clk);
317 err_remove_config_dt:
318         stmmac_remove_config_dt(pdev, plat_dat);
319
320         return ret;
321 }
322
323 static int meson8b_dwmac_remove(struct platform_device *pdev)
324 {
325         struct meson8b_dwmac *dwmac = get_stmmac_bsp_priv(&pdev->dev);
326
327         clk_disable_unprepare(dwmac->m25_div_clk);
328
329         return stmmac_pltfr_remove(pdev);
330 }
331
332 static const struct of_device_id meson8b_dwmac_match[] = {
333         { .compatible = "amlogic,meson8b-dwmac" },
334         { .compatible = "amlogic,meson-gxbb-dwmac" },
335         { }
336 };
337 MODULE_DEVICE_TABLE(of, meson8b_dwmac_match);
338
339 static struct platform_driver meson8b_dwmac_driver = {
340         .probe  = meson8b_dwmac_probe,
341         .remove = meson8b_dwmac_remove,
342         .driver = {
343                 .name           = "meson8b-dwmac",
344                 .pm             = &stmmac_pltfr_pm_ops,
345                 .of_match_table = meson8b_dwmac_match,
346         },
347 };
348 module_platform_driver(meson8b_dwmac_driver);
349
350 MODULE_AUTHOR("Martin Blumenstingl <martin.blumenstingl@googlemail.com>");
351 MODULE_DESCRIPTION("Amlogic Meson8b and GXBB DWMAC glue layer");
352 MODULE_LICENSE("GPL v2");