1 /* Copyright OpenWrt.org (C) 2015.
2 * Copyright Altera Corporation (C) 2014. All rights reserved.
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.
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.
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/>.
16 * Adopted from dwmac-socfpga.c
17 * Based on code found in mach-oxnas.c
20 #include <linux/mfd/syscon.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>
30 #include <mach/hardware.h>
33 #include "stmmac_platform.h"
39 static int oxnas_gmac_init(struct platform_device *pdev, void *priv)
41 struct oxnas_gmac *bsp_priv = priv;
45 ret = device_reset(&pdev->dev);
49 if (IS_ERR(bsp_priv->clk))
50 return PTR_ERR(bsp_priv->clk);
51 clk_prepare_enable(bsp_priv->clk);
53 value = readl(SYS_CTRL_GMAC_CTRL);
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);
70 /* set tx & rx vardelay */
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);
81 static void oxnas_gmac_exit(struct platform_device *pdev, void *priv)
83 struct reset_control *rstc;
85 clk_disable_unprepare(priv);
86 devm_clk_put(&pdev->dev, priv);
88 rstc = reset_control_get(&pdev->dev, NULL);
90 reset_control_assert(rstc);
91 reset_control_put(rstc);
95 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,2,0)
96 static void *oxnas_gmac_probe(struct platform_device *pdev)
99 static int oxnas_gmac_probe(struct platform_device *pdev)
101 struct plat_stmmacenet_data *plat_dat;
102 struct stmmac_resources stmmac_res;
105 struct device *dev = &pdev->dev;
106 struct oxnas_gmac *bsp_priv;
108 bsp_priv = devm_kzalloc(dev, sizeof(*bsp_priv), GFP_KERNEL);
110 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,2,0)
111 return ERR_PTR(-ENOMEM);
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;
120 return PTR_ERR(bsp_priv->clk);
123 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,2,0)
126 ret = stmmac_get_platform_resources(pdev, &stmmac_res);
130 plat_dat = stmmac_probe_config_dt(pdev, &stmmac_res.mac);
131 if (IS_ERR(plat_dat))
132 return PTR_ERR(plat_dat);
134 plat_dat->bsp_priv = bsp_priv;
135 plat_dat->init = oxnas_gmac_init;
136 plat_dat->exit = oxnas_gmac_exit;
138 ret = oxnas_gmac_init(pdev, bsp_priv);
142 return stmmac_dvr_probe(dev, plat_dat, &stmmac_res);
146 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,2,0)
147 const struct stmmac_of_data oxnas_gmac_data = {
149 .setup = oxnas_gmac_probe,
150 .init = oxnas_gmac_init,
151 .exit = oxnas_gmac_exit,
154 static const struct of_device_id oxnas_gmac_match[] = {
155 { .compatible = "plxtech,nas782x-gmac" },
158 MODULE_DEVICE_TABLE(of, oxnas_gmac_match);
160 static struct platform_driver oxnas_gmac_driver = {
161 .probe = oxnas_gmac_probe,
162 .remove = stmmac_pltfr_remove,
164 .name = "oxnas-gmac",
165 .pm = &stmmac_pltfr_pm_ops,
166 .of_match_table = oxnas_gmac_match,
169 module_platform_driver(oxnas_gmac_driver);
172 MODULE_LICENSE("GPL v2");