sunxi: driver refresh for 3.13
[openwrt.git] / target / linux / sunxi / patches-3.13 / 153-7-stmmac-sunxi-extensions-for-a20.patch
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
5  A20 SoC's
6
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.
10
11 Allwinner's GMAC requires setting additional registers in the SoC's
12 clock control unit.
13
14 The exact version of the DWMAC IP that Allwinner uses is unknown,
15 thus the exact feature set is unknown.
16
17 Signed-off-by: Chen-Yu Tsai <wens@csie.org>
18 Signed-off-by: David S. Miller <davem@davemloft.net>
19 ---
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
29
30 diff --git a/Documentation/devicetree/bindings/net/allwinner,sun7i-a20-gmac.txt b/Documentation/devicetree/bindings/net/allwinner,sun7i-a20-gmac.txt
31 new file mode 100644
32 index 0000000..ea4d752
33 --- /dev/null
34 +++ b/Documentation/devicetree/bindings/net/allwinner,sun7i-a20-gmac.txt
35 @@ -0,0 +1,27 @@
36 +* Allwinner GMAC ethernet controller
37 +
38 +This device is a platform glue layer for stmmac.
39 +Please see stmmac.txt for the other unchanged properties.
40 +
41 +Required 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"
47 +
48 +Optional properties:
49 +- phy-supply: phandle to a regulator if the PHY needs one
50 +
51 +Examples:
52 +
53 +       gmac: ethernet@01c50000 {
54 +               compatible = "allwinner,sun7i-a20-gmac";
55 +               reg = <0x01c50000 0x10000>,
56 +                     <0x01c20164 0x4>;
57 +               interrupts = <0 85 1>;
58 +               interrupt-names = "macirq";
59 +               clocks = <&ahb_gates 49>, <&gmac_tx>;
60 +               clock-names = "stmmaceth", "allwinner_gmac_tx";
61 +               phy-mode = "mii";
62 +       };
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
68  
69           If unsure, say N.
70  
71 +config DWMAC_SUNXI
72 +       bool "Allwinner GMAC support"
73 +       depends on STMMAC_PLATFORM && ARCH_SUNXI
74 +       default y
75 +       ---help---
76 +         Support for Allwinner A20/A31 GMAC ethernet controllers.
77 +
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.
81 +
82  config STMMAC_PCI
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
89 @@ -1,6 +1,7 @@
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
98 new file mode 100644
99 index 0000000..771cd15f
100 --- /dev/null
101 +++ b/drivers/net/ethernet/stmicro/stmmac/dwmac-sunxi.c
102 @@ -0,0 +1,140 @@
103 +/**
104 + * dwmac-sunxi.c - Allwinner sunxi DWMAC specific glue layer
105 + *
106 + * Copyright (C) 2013 Chen-Yu Tsai
107 + *
108 + * Chen-Yu Tsai  <wens@csie.org>
109 + *
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.
114 + *
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.
119 + */
120 +
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>
126 +
127 +struct sunxi_priv_data {
128 +       int interface;
129 +       int clk_enabled;
130 +       struct clk *tx_clk;
131 +       struct regulator *regulator;
132 +};
133 +
134 +static void *sun7i_gmac_setup(struct platform_device *pdev)
135 +{
136 +       struct sunxi_priv_data *gmac;
137 +       struct device *dev = &pdev->dev;
138 +
139 +       gmac = devm_kzalloc(dev, sizeof(*gmac), GFP_KERNEL);
140 +       if (!gmac)
141 +               return ERR_PTR(-ENOMEM);
142 +
143 +       gmac->interface = of_get_phy_mode(dev->of_node);
144 +
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;
149 +       }
150 +
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;
158 +       }
159 +
160 +       return gmac;
161 +}
162 +
163 +#define SUN7I_GMAC_GMII_RGMII_RATE     125000000
164 +#define SUN7I_GMAC_MII_RATE            25000000
165 +
166 +static int sun7i_gmac_init(struct platform_device *pdev, void *priv)
167 +{
168 +       struct sunxi_priv_data *gmac = priv;
169 +       int ret;
170 +
171 +       if (gmac->regulator) {
172 +               ret = regulator_enable(gmac->regulator);
173 +               if (ret)
174 +                       return ret;
175 +       }
176 +
177 +       /* Set GMAC interface port mode
178 +        *
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.
182 +        */
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;
187 +       } else {
188 +               clk_set_rate(gmac->tx_clk, SUN7I_GMAC_MII_RATE);
189 +               clk_prepare(gmac->tx_clk);
190 +       }
191 +
192 +       return 0;
193 +}
194 +
195 +static void sun7i_gmac_exit(struct platform_device *pdev, void *priv)
196 +{
197 +       struct sunxi_priv_data *gmac = priv;
198 +
199 +       if (gmac->clk_enabled) {
200 +               clk_disable(gmac->tx_clk);
201 +               gmac->clk_enabled = 0;
202 +       }
203 +       clk_unprepare(gmac->tx_clk);
204 +
205 +       if (gmac->regulator)
206 +               regulator_disable(gmac->regulator);
207 +}
208 +
209 +static void sun7i_fix_speed(void *priv, unsigned int speed)
210 +{
211 +       struct sunxi_priv_data *gmac = priv;
212 +
213 +       /* only GMII mode requires us to reconfigure the clock lines */
214 +       if (gmac->interface != PHY_INTERFACE_MODE_GMII)
215 +               return;
216 +
217 +       if (gmac->clk_enabled) {
218 +               clk_disable(gmac->tx_clk);
219 +               gmac->clk_enabled = 0;
220 +       }
221 +       clk_unprepare(gmac->tx_clk);
222 +
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;
227 +       } else {
228 +               clk_set_rate(gmac->tx_clk, SUN7I_GMAC_MII_RATE);
229 +               clk_prepare(gmac->tx_clk);
230 +       }
231 +}
232 +
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 = {
236 +       .has_gmac = 1,
237 +       .tx_coe = 1,
238 +       .fix_mac_speed = sun7i_fix_speed,
239 +       .setup = sun7i_gmac_setup,
240 +       .init = sun7i_gmac_init,
241 +       .exit = sun7i_gmac_exit,
242 +};
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);
249  
250  #ifdef CONFIG_STMMAC_PLATFORM
251 +#ifdef CONFIG_DWMAC_SUNXI
252 +extern const struct stmmac_of_data sun7i_gmac_data;
253 +#endif
254  extern struct platform_driver stmmac_pltfr_driver;
255  static inline int stmmac_register_platform(void)
256  {
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
261 @@ -30,6 +30,9 @@
262  #include "stmmac.h"
263  
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},
267 +#endif
268         /* SoC specific glue layers should come before generic bindings */
269         { .compatible = "st,spear600-gmac"},
270         { .compatible = "snps,dwmac-3.610"},
271 -- 
272 1.8.5.5
273