1 From af0bd4e9ba809391f275d0c094ac0bfbfbb3f430 Mon Sep 17 00:00:00 2001
2 From: Chen-Yu Tsai <wens@csie.org>
3 Date: Fri, 17 Jan 2014 21:24:47 +0800
4 Subject: [PATCH] net: stmmac: sunxi platform extensions for GMAC in Allwinner
7 The Allwinner A20 has an ethernet controller that seems to be
8 an early version of Synopsys DesignWare MAC 10/100/1000 Universal,
9 which is supported by the stmmac driver.
11 Allwinner's GMAC requires setting additional registers in the SoC's
14 The exact version of the DWMAC IP that Allwinner uses is unknown,
15 thus the exact feature set is unknown.
17 Signed-off-by: Chen-Yu Tsai <wens@csie.org>
18 Signed-off-by: David S. Miller <davem@davemloft.net>
20 .../bindings/net/allwinner,sun7i-a20-gmac.txt | 27 ++++
21 drivers/net/ethernet/stmicro/stmmac/Kconfig | 11 ++
22 drivers/net/ethernet/stmicro/stmmac/Makefile | 1 +
23 drivers/net/ethernet/stmicro/stmmac/dwmac-sunxi.c | 140 +++++++++++++++++++++
24 drivers/net/ethernet/stmicro/stmmac/stmmac.h | 3 +
25 .../net/ethernet/stmicro/stmmac/stmmac_platform.c | 3 +
26 6 files changed, 185 insertions(+)
27 create mode 100644 Documentation/devicetree/bindings/net/allwinner,sun7i-a20-gmac.txt
28 create mode 100644 drivers/net/ethernet/stmicro/stmmac/dwmac-sunxi.c
30 diff --git a/Documentation/devicetree/bindings/net/allwinner,sun7i-a20-gmac.txt b/Documentation/devicetree/bindings/net/allwinner,sun7i-a20-gmac.txt
32 index 0000000..ea4d752
34 +++ b/Documentation/devicetree/bindings/net/allwinner,sun7i-a20-gmac.txt
36 +* Allwinner GMAC ethernet controller
38 +This device is a platform glue layer for stmmac.
39 +Please see stmmac.txt for the other unchanged properties.
42 + - compatible: Should be "allwinner,sun7i-a20-gmac"
43 + - clocks: Should contain the GMAC main clock, and tx clock
44 + The tx clock type should be "allwinner,sun7i-a20-gmac-clk"
45 + - clock-names: Should contain the clock names "stmmaceth",
46 + and "allwinner_gmac_tx"
49 +- phy-supply: phandle to a regulator if the PHY needs one
53 + gmac: ethernet@01c50000 {
54 + compatible = "allwinner,sun7i-a20-gmac";
55 + reg = <0x01c50000 0x10000>,
57 + interrupts = <0 85 1>;
58 + interrupt-names = "macirq";
59 + clocks = <&ahb_gates 49>, <&gmac_tx>;
60 + clock-names = "stmmaceth", "allwinner_gmac_tx";
63 diff --git a/drivers/net/ethernet/stmicro/stmmac/Kconfig b/drivers/net/ethernet/stmicro/stmmac/Kconfig
64 index b59d1ef..e2f202e 100644
65 --- a/drivers/net/ethernet/stmicro/stmmac/Kconfig
66 +++ b/drivers/net/ethernet/stmicro/stmmac/Kconfig
67 @@ -26,6 +26,17 @@ config STMMAC_PLATFORM
72 + bool "Allwinner GMAC support"
73 + depends on STMMAC_PLATFORM && ARCH_SUNXI
76 + Support for Allwinner A20/A31 GMAC ethernet controllers.
78 + This selects Allwinner SoC glue layer support for the
79 + stmmac device driver. This driver is used for A20/A31
80 + GMAC ethernet controller.
83 bool "STMMAC PCI bus support"
84 depends on STMMAC_ETH && PCI
85 diff --git a/drivers/net/ethernet/stmicro/stmmac/Makefile b/drivers/net/ethernet/stmicro/stmmac/Makefile
86 index 356a9dd..ecadece 100644
87 --- a/drivers/net/ethernet/stmicro/stmmac/Makefile
88 +++ b/drivers/net/ethernet/stmicro/stmmac/Makefile
90 obj-$(CONFIG_STMMAC_ETH) += stmmac.o
91 stmmac-$(CONFIG_STMMAC_PLATFORM) += stmmac_platform.o
92 stmmac-$(CONFIG_STMMAC_PCI) += stmmac_pci.o
93 +stmmac-$(CONFIG_DWMAC_SUNXI) += dwmac-sunxi.o
94 stmmac-objs:= stmmac_main.o stmmac_ethtool.o stmmac_mdio.o ring_mode.o \
95 chain_mode.o dwmac_lib.o dwmac1000_core.o dwmac1000_dma.o \
96 dwmac100_core.o dwmac100_dma.o enh_desc.o norm_desc.o \
97 diff --git a/drivers/net/ethernet/stmicro/stmmac/dwmac-sunxi.c b/drivers/net/ethernet/stmicro/stmmac/dwmac-sunxi.c
99 index 0000000..771cd15f
101 +++ b/drivers/net/ethernet/stmicro/stmmac/dwmac-sunxi.c
104 + * dwmac-sunxi.c - Allwinner sunxi DWMAC specific glue layer
106 + * Copyright (C) 2013 Chen-Yu Tsai
108 + * Chen-Yu Tsai <wens@csie.org>
110 + * This program is free software; you can redistribute it and/or modify
111 + * it under the terms of the GNU General Public License as published by
112 + * the Free Software Foundation; either version 2 of the License, or
113 + * (at your option) any later version.
115 + * This program is distributed in the hope that it will be useful,
116 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
117 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
118 + * GNU General Public License for more details.
121 +#include <linux/stmmac.h>
122 +#include <linux/clk.h>
123 +#include <linux/phy.h>
124 +#include <linux/of_net.h>
125 +#include <linux/regulator/consumer.h>
127 +struct sunxi_priv_data {
130 + struct clk *tx_clk;
131 + struct regulator *regulator;
134 +static void *sun7i_gmac_setup(struct platform_device *pdev)
136 + struct sunxi_priv_data *gmac;
137 + struct device *dev = &pdev->dev;
139 + gmac = devm_kzalloc(dev, sizeof(*gmac), GFP_KERNEL);
141 + return ERR_PTR(-ENOMEM);
143 + gmac->interface = of_get_phy_mode(dev->of_node);
145 + gmac->tx_clk = devm_clk_get(dev, "allwinner_gmac_tx");
146 + if (IS_ERR(gmac->tx_clk)) {
147 + dev_err(dev, "could not get tx clock\n");
148 + return gmac->tx_clk;
151 + /* Optional regulator for PHY */
152 + gmac->regulator = devm_regulator_get_optional(dev, "phy");
153 + if (IS_ERR(gmac->regulator)) {
154 + if (PTR_ERR(gmac->regulator) == -EPROBE_DEFER)
155 + return ERR_PTR(-EPROBE_DEFER);
156 + dev_info(dev, "no regulator found\n");
157 + gmac->regulator = NULL;
163 +#define SUN7I_GMAC_GMII_RGMII_RATE 125000000
164 +#define SUN7I_GMAC_MII_RATE 25000000
166 +static int sun7i_gmac_init(struct platform_device *pdev, void *priv)
168 + struct sunxi_priv_data *gmac = priv;
171 + if (gmac->regulator) {
172 + ret = regulator_enable(gmac->regulator);
177 + /* Set GMAC interface port mode
179 + * The GMAC TX clock lines are configured by setting the clock
180 + * rate, which then uses the auto-reparenting feature of the
181 + * clock driver, and enabling/disabling the clock.
183 + if (gmac->interface == PHY_INTERFACE_MODE_RGMII) {
184 + clk_set_rate(gmac->tx_clk, SUN7I_GMAC_GMII_RGMII_RATE);
185 + clk_prepare_enable(gmac->tx_clk);
186 + gmac->clk_enabled = 1;
188 + clk_set_rate(gmac->tx_clk, SUN7I_GMAC_MII_RATE);
189 + clk_prepare(gmac->tx_clk);
195 +static void sun7i_gmac_exit(struct platform_device *pdev, void *priv)
197 + struct sunxi_priv_data *gmac = priv;
199 + if (gmac->clk_enabled) {
200 + clk_disable(gmac->tx_clk);
201 + gmac->clk_enabled = 0;
203 + clk_unprepare(gmac->tx_clk);
205 + if (gmac->regulator)
206 + regulator_disable(gmac->regulator);
209 +static void sun7i_fix_speed(void *priv, unsigned int speed)
211 + struct sunxi_priv_data *gmac = priv;
213 + /* only GMII mode requires us to reconfigure the clock lines */
214 + if (gmac->interface != PHY_INTERFACE_MODE_GMII)
217 + if (gmac->clk_enabled) {
218 + clk_disable(gmac->tx_clk);
219 + gmac->clk_enabled = 0;
221 + clk_unprepare(gmac->tx_clk);
223 + if (speed == 1000) {
224 + clk_set_rate(gmac->tx_clk, SUN7I_GMAC_GMII_RGMII_RATE);
225 + clk_prepare_enable(gmac->tx_clk);
226 + gmac->clk_enabled = 1;
228 + clk_set_rate(gmac->tx_clk, SUN7I_GMAC_MII_RATE);
229 + clk_prepare(gmac->tx_clk);
233 +/* of_data specifying hardware features and callbacks.
234 + * hardware features were copied from Allwinner drivers. */
235 +const struct stmmac_of_data sun7i_gmac_data = {
238 + .fix_mac_speed = sun7i_fix_speed,
239 + .setup = sun7i_gmac_setup,
240 + .init = sun7i_gmac_init,
241 + .exit = sun7i_gmac_exit,
243 diff --git a/drivers/net/ethernet/stmicro/stmmac/stmmac.h b/drivers/net/ethernet/stmicro/stmmac/stmmac.h
244 index c1c141f..d9af26e 100644
245 --- a/drivers/net/ethernet/stmicro/stmmac/stmmac.h
246 +++ b/drivers/net/ethernet/stmicro/stmmac/stmmac.h
247 @@ -130,6 +130,9 @@ struct stmmac_priv *stmmac_dvr_probe(struct device *device,
248 bool stmmac_eee_init(struct stmmac_priv *priv);
250 #ifdef CONFIG_STMMAC_PLATFORM
251 +#ifdef CONFIG_DWMAC_SUNXI
252 +extern const struct stmmac_of_data sun7i_gmac_data;
254 extern struct platform_driver stmmac_pltfr_driver;
255 static inline int stmmac_register_platform(void)
257 diff --git a/drivers/net/ethernet/stmicro/stmmac/stmmac_platform.c b/drivers/net/ethernet/stmicro/stmmac/stmmac_platform.c
258 index bf119db..9d4baa8 100644
259 --- a/drivers/net/ethernet/stmicro/stmmac/stmmac_platform.c
260 +++ b/drivers/net/ethernet/stmicro/stmmac/stmmac_platform.c
264 static const struct of_device_id stmmac_dt_ids[] = {
265 +#ifdef CONFIG_DWMAC_SUNXI
266 + { .compatible = "allwinner,sun7i-a20-gmac", .data = &sun7i_gmac_data},
268 /* SoC specific glue layers should come before generic bindings */
269 { .compatible = "st,spear600-gmac"},
270 { .compatible = "snps,dwmac-3.610"},