2 * dwmac-sunxi.c - Allwinner sunxi DWMAC specific glue layer
4 * Copyright (C) 2013 Chen-Yu Tsai
6 * Chen-Yu Tsai <wens@csie.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 #include <linux/stmmac.h>
20 #include <linux/clk.h>
21 #include <linux/module.h>
22 #include <linux/phy.h>
23 #include <linux/platform_device.h>
24 #include <linux/of_net.h>
25 #include <linux/regulator/consumer.h>
27 #include "stmmac_platform.h"
29 struct sunxi_priv_data {
33 struct regulator *regulator;
36 #define SUN7I_GMAC_GMII_RGMII_RATE 125000000
37 #define SUN7I_GMAC_MII_RATE 25000000
39 static int sun7i_gmac_init(struct platform_device *pdev, void *priv)
41 struct sunxi_priv_data *gmac = priv;
44 if (gmac->regulator) {
45 ret = regulator_enable(gmac->regulator);
50 /* Set GMAC interface port mode
52 * The GMAC TX clock lines are configured by setting the clock
53 * rate, which then uses the auto-reparenting feature of the
54 * clock driver, and enabling/disabling the clock.
56 if (phy_interface_mode_is_rgmii(gmac->interface)) {
57 clk_set_rate(gmac->tx_clk, SUN7I_GMAC_GMII_RGMII_RATE);
58 clk_prepare_enable(gmac->tx_clk);
59 gmac->clk_enabled = 1;
61 clk_set_rate(gmac->tx_clk, SUN7I_GMAC_MII_RATE);
62 ret = clk_prepare(gmac->tx_clk);
63 if (ret && gmac->regulator)
64 regulator_disable(gmac->regulator);
70 static void sun7i_gmac_exit(struct platform_device *pdev, void *priv)
72 struct sunxi_priv_data *gmac = priv;
74 if (gmac->clk_enabled) {
75 clk_disable(gmac->tx_clk);
76 gmac->clk_enabled = 0;
78 clk_unprepare(gmac->tx_clk);
81 regulator_disable(gmac->regulator);
84 static void sun7i_fix_speed(void *priv, unsigned int speed)
86 struct sunxi_priv_data *gmac = priv;
88 /* only GMII mode requires us to reconfigure the clock lines */
89 if (gmac->interface != PHY_INTERFACE_MODE_GMII)
92 if (gmac->clk_enabled) {
93 clk_disable(gmac->tx_clk);
94 gmac->clk_enabled = 0;
96 clk_unprepare(gmac->tx_clk);
99 clk_set_rate(gmac->tx_clk, SUN7I_GMAC_GMII_RGMII_RATE);
100 clk_prepare_enable(gmac->tx_clk);
101 gmac->clk_enabled = 1;
103 clk_set_rate(gmac->tx_clk, SUN7I_GMAC_MII_RATE);
104 clk_prepare(gmac->tx_clk);
108 static int sun7i_gmac_probe(struct platform_device *pdev)
110 struct plat_stmmacenet_data *plat_dat;
111 struct stmmac_resources stmmac_res;
112 struct sunxi_priv_data *gmac;
113 struct device *dev = &pdev->dev;
116 ret = stmmac_get_platform_resources(pdev, &stmmac_res);
120 plat_dat = stmmac_probe_config_dt(pdev, &stmmac_res.mac);
121 if (IS_ERR(plat_dat))
122 return PTR_ERR(plat_dat);
124 gmac = devm_kzalloc(dev, sizeof(*gmac), GFP_KERNEL);
127 goto err_remove_config_dt;
130 gmac->interface = of_get_phy_mode(dev->of_node);
132 gmac->tx_clk = devm_clk_get(dev, "allwinner_gmac_tx");
133 if (IS_ERR(gmac->tx_clk)) {
134 dev_err(dev, "could not get tx clock\n");
135 ret = PTR_ERR(gmac->tx_clk);
136 goto err_remove_config_dt;
139 /* Optional regulator for PHY */
140 gmac->regulator = devm_regulator_get_optional(dev, "phy");
141 if (IS_ERR(gmac->regulator)) {
142 if (PTR_ERR(gmac->regulator) == -EPROBE_DEFER) {
144 goto err_remove_config_dt;
146 dev_info(dev, "no regulator found\n");
147 gmac->regulator = NULL;
150 /* platform data specifying hardware features and callbacks.
151 * hardware features were copied from Allwinner drivers. */
152 plat_dat->tx_coe = 1;
153 plat_dat->has_gmac = true;
154 plat_dat->bsp_priv = gmac;
155 plat_dat->init = sun7i_gmac_init;
156 plat_dat->exit = sun7i_gmac_exit;
157 plat_dat->fix_mac_speed = sun7i_fix_speed;
159 ret = sun7i_gmac_init(pdev, plat_dat->bsp_priv);
161 goto err_remove_config_dt;
163 ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
170 sun7i_gmac_exit(pdev, plat_dat->bsp_priv);
171 err_remove_config_dt:
172 stmmac_remove_config_dt(pdev, plat_dat);
177 static const struct of_device_id sun7i_dwmac_match[] = {
178 { .compatible = "allwinner,sun7i-a20-gmac" },
181 MODULE_DEVICE_TABLE(of, sun7i_dwmac_match);
183 static struct platform_driver sun7i_dwmac_driver = {
184 .probe = sun7i_gmac_probe,
185 .remove = stmmac_pltfr_remove,
187 .name = "sun7i-dwmac",
188 .pm = &stmmac_pltfr_pm_ops,
189 .of_match_table = sun7i_dwmac_match,
192 module_platform_driver(sun7i_dwmac_driver);
194 MODULE_AUTHOR("Chen-Yu Tsai <wens@csie.org>");
195 MODULE_DESCRIPTION("Allwinner sunxi DWMAC specific glue layer");
196 MODULE_LICENSE("GPL");