oxnas: move gmac soc glue from mach to stmmac driver
[openwrt.git] / target / linux / oxnas / files / drivers / net / ethernet / stmicro / stmmac / dwmac-oxnas.c
1 /* Copyright OpenWrt.org (C) 2015.
2  * Copyright Altera Corporation (C) 2014. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License, version 2,
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Adopted from dwmac-socfpga.c
17  * Based on code found in mach-oxnas.c
18  */
19
20 #include <linux/mfd/syscon.h>
21 #include <linux/of.h>
22 #include <linux/of_address.h>
23 #include <linux/of_net.h>
24 #include <linux/phy.h>
25 #include <linux/regmap.h>
26 #include <linux/reset.h>
27 #include <linux/stmmac.h>
28 #include <linux/version.h>
29
30 #include <mach/hardware.h>
31
32 #include "stmmac.h"
33 #include "stmmac_platform.h"
34
35 struct oxnas_gmac {
36         struct clk *clk;
37 };
38
39 static int oxnas_gmac_init(struct platform_device *pdev, void *priv)
40 {
41         struct oxnas_gmac *bsp_priv = priv;
42         int ret = 0;
43         unsigned value;
44
45         ret = device_reset(&pdev->dev);
46         if (ret)
47                 return ret;
48
49         if (IS_ERR(bsp_priv->clk))
50                 return PTR_ERR(bsp_priv->clk);
51         clk_prepare_enable(bsp_priv->clk);
52
53         value = readl(SYS_CTRL_GMAC_CTRL);
54
55         /* Enable GMII_GTXCLK to follow GMII_REFCLK, required for gigabit PHY */
56         value |= BIT(SYS_CTRL_GMAC_CKEN_GTX);
57         /* Use simple mux for 25/125 Mhz clock switching */
58         value |= BIT(SYS_CTRL_GMAC_SIMPLE_MUX);
59         /* set auto switch tx clock source */
60         value |= BIT(SYS_CTRL_GMAC_AUTO_TX_SOURCE);
61         /* enable tx & rx vardelay */
62         value |= BIT(SYS_CTRL_GMAC_CKEN_TX_OUT);
63         value |= BIT(SYS_CTRL_GMAC_CKEN_TXN_OUT);
64         value |= BIT(SYS_CTRL_GMAC_CKEN_TX_IN);
65         value |= BIT(SYS_CTRL_GMAC_CKEN_RX_OUT);
66         value |= BIT(SYS_CTRL_GMAC_CKEN_RXN_OUT);
67         value |= BIT(SYS_CTRL_GMAC_CKEN_RX_IN);
68         writel(value, SYS_CTRL_GMAC_CTRL);
69
70         /* set tx & rx vardelay */
71         value = 0;
72         value |= SYS_CTRL_GMAC_TX_VARDELAY(4);
73         value |= SYS_CTRL_GMAC_TXN_VARDELAY(2);
74         value |= SYS_CTRL_GMAC_RX_VARDELAY(10);
75         value |= SYS_CTRL_GMAC_RXN_VARDELAY(8);
76         writel(value, SYS_CTRL_GMAC_DELAY_CTRL);
77
78         return 0;
79 }
80
81 static void oxnas_gmac_exit(struct platform_device *pdev, void *priv)
82 {
83         struct reset_control *rstc;
84
85         clk_disable_unprepare(priv);
86         devm_clk_put(&pdev->dev, priv);
87
88         rstc = reset_control_get(&pdev->dev, NULL);
89         if (!IS_ERR(rstc)) {
90                 reset_control_assert(rstc);
91                 reset_control_put(rstc);
92         }
93 }
94
95 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,2,0)
96 static void *oxnas_gmac_probe(struct platform_device *pdev)
97 {
98 #else
99 static int oxnas_gmac_probe(struct platform_device *pdev)
100 {
101         struct plat_stmmacenet_data *plat_dat;
102         struct stmmac_resources stmmac_res;
103         int                     ret;
104 #endif
105         struct device           *dev = &pdev->dev;
106         struct oxnas_gmac       *bsp_priv;
107
108         bsp_priv = devm_kzalloc(dev, sizeof(*bsp_priv), GFP_KERNEL);
109         if (!bsp_priv)
110 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,2,0)
111                 return ERR_PTR(-ENOMEM);
112 #else
113                 return -ENOMEM;
114 #endif
115         bsp_priv->clk = devm_clk_get(dev, "gmac");
116         if (IS_ERR(bsp_priv->clk))
117 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,2,0)
118                 return bsp_priv->clk;
119 #else
120                 return PTR_ERR(bsp_priv->clk);
121 #endif
122
123 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,2,0)
124         return bsp_priv;
125 #else
126         ret = stmmac_get_platform_resources(pdev, &stmmac_res);
127         if (ret)
128                 return ret;
129
130         plat_dat = stmmac_probe_config_dt(pdev, &stmmac_res.mac);
131         if (IS_ERR(plat_dat))
132                 return PTR_ERR(plat_dat);
133
134         plat_dat->bsp_priv = bsp_priv;
135         plat_dat->init = oxnas_gmac_init;
136         plat_dat->exit = oxnas_gmac_exit;
137
138         ret = oxnas_gmac_init(pdev, bsp_priv);
139         if (ret)
140                 return ret;
141
142         return stmmac_dvr_probe(dev, plat_dat, &stmmac_res);
143 #endif
144 }
145
146 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,2,0)
147 const struct stmmac_of_data oxnas_gmac_data = {
148         .has_gmac = 1,
149         .setup = oxnas_gmac_probe,
150         .init = oxnas_gmac_init,
151         .exit = oxnas_gmac_exit,
152 };
153 #else
154 static const struct of_device_id oxnas_gmac_match[] = {
155         { .compatible = "plxtech,nas782x-gmac" },
156         { }
157 };
158 MODULE_DEVICE_TABLE(of, oxnas_gmac_match);
159
160 static struct platform_driver oxnas_gmac_driver = {
161         .probe  = oxnas_gmac_probe,
162         .remove = stmmac_pltfr_remove,
163         .driver = {
164                 .name           = "oxnas-gmac",
165                 .pm             = &stmmac_pltfr_pm_ops,
166                 .of_match_table = oxnas_gmac_match,
167         },
168 };
169 module_platform_driver(oxnas_gmac_driver);
170 #endif
171
172 MODULE_LICENSE("GPL v2");