0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/stmmac.h>
0011 #include <linux/clk.h>
0012 #include <linux/module.h>
0013 #include <linux/phy.h>
0014 #include <linux/platform_device.h>
0015 #include <linux/of_net.h>
0016 #include <linux/regulator/consumer.h>
0017
0018 #include "stmmac_platform.h"
0019
0020 struct sunxi_priv_data {
0021 phy_interface_t interface;
0022 int clk_enabled;
0023 struct clk *tx_clk;
0024 struct regulator *regulator;
0025 };
0026
0027 #define SUN7I_GMAC_GMII_RGMII_RATE 125000000
0028 #define SUN7I_GMAC_MII_RATE 25000000
0029
0030 static int sun7i_gmac_init(struct platform_device *pdev, void *priv)
0031 {
0032 struct sunxi_priv_data *gmac = priv;
0033 int ret = 0;
0034
0035 if (gmac->regulator) {
0036 ret = regulator_enable(gmac->regulator);
0037 if (ret)
0038 return ret;
0039 }
0040
0041
0042
0043
0044
0045
0046
0047 if (phy_interface_mode_is_rgmii(gmac->interface)) {
0048 clk_set_rate(gmac->tx_clk, SUN7I_GMAC_GMII_RGMII_RATE);
0049 clk_prepare_enable(gmac->tx_clk);
0050 gmac->clk_enabled = 1;
0051 } else {
0052 clk_set_rate(gmac->tx_clk, SUN7I_GMAC_MII_RATE);
0053 ret = clk_prepare(gmac->tx_clk);
0054 if (ret && gmac->regulator)
0055 regulator_disable(gmac->regulator);
0056 }
0057
0058 return ret;
0059 }
0060
0061 static void sun7i_gmac_exit(struct platform_device *pdev, void *priv)
0062 {
0063 struct sunxi_priv_data *gmac = priv;
0064
0065 if (gmac->clk_enabled) {
0066 clk_disable(gmac->tx_clk);
0067 gmac->clk_enabled = 0;
0068 }
0069 clk_unprepare(gmac->tx_clk);
0070
0071 if (gmac->regulator)
0072 regulator_disable(gmac->regulator);
0073 }
0074
0075 static void sun7i_fix_speed(void *priv, unsigned int speed)
0076 {
0077 struct sunxi_priv_data *gmac = priv;
0078
0079
0080 if (gmac->interface != PHY_INTERFACE_MODE_GMII)
0081 return;
0082
0083 if (gmac->clk_enabled) {
0084 clk_disable(gmac->tx_clk);
0085 gmac->clk_enabled = 0;
0086 }
0087 clk_unprepare(gmac->tx_clk);
0088
0089 if (speed == 1000) {
0090 clk_set_rate(gmac->tx_clk, SUN7I_GMAC_GMII_RGMII_RATE);
0091 clk_prepare_enable(gmac->tx_clk);
0092 gmac->clk_enabled = 1;
0093 } else {
0094 clk_set_rate(gmac->tx_clk, SUN7I_GMAC_MII_RATE);
0095 clk_prepare(gmac->tx_clk);
0096 }
0097 }
0098
0099 static int sun7i_gmac_probe(struct platform_device *pdev)
0100 {
0101 struct plat_stmmacenet_data *plat_dat;
0102 struct stmmac_resources stmmac_res;
0103 struct sunxi_priv_data *gmac;
0104 struct device *dev = &pdev->dev;
0105 int ret;
0106
0107 ret = stmmac_get_platform_resources(pdev, &stmmac_res);
0108 if (ret)
0109 return ret;
0110
0111 plat_dat = stmmac_probe_config_dt(pdev, stmmac_res.mac);
0112 if (IS_ERR(plat_dat))
0113 return PTR_ERR(plat_dat);
0114
0115 gmac = devm_kzalloc(dev, sizeof(*gmac), GFP_KERNEL);
0116 if (!gmac) {
0117 ret = -ENOMEM;
0118 goto err_remove_config_dt;
0119 }
0120
0121 ret = of_get_phy_mode(dev->of_node, &gmac->interface);
0122 if (ret && ret != -ENODEV) {
0123 dev_err(dev, "Can't get phy-mode\n");
0124 goto err_remove_config_dt;
0125 }
0126
0127 gmac->tx_clk = devm_clk_get(dev, "allwinner_gmac_tx");
0128 if (IS_ERR(gmac->tx_clk)) {
0129 dev_err(dev, "could not get tx clock\n");
0130 ret = PTR_ERR(gmac->tx_clk);
0131 goto err_remove_config_dt;
0132 }
0133
0134
0135 gmac->regulator = devm_regulator_get_optional(dev, "phy");
0136 if (IS_ERR(gmac->regulator)) {
0137 if (PTR_ERR(gmac->regulator) == -EPROBE_DEFER) {
0138 ret = -EPROBE_DEFER;
0139 goto err_remove_config_dt;
0140 }
0141 dev_info(dev, "no regulator found\n");
0142 gmac->regulator = NULL;
0143 }
0144
0145
0146
0147 plat_dat->tx_coe = 1;
0148 plat_dat->has_gmac = true;
0149 plat_dat->bsp_priv = gmac;
0150 plat_dat->init = sun7i_gmac_init;
0151 plat_dat->exit = sun7i_gmac_exit;
0152 plat_dat->fix_mac_speed = sun7i_fix_speed;
0153 plat_dat->tx_fifo_size = 4096;
0154 plat_dat->rx_fifo_size = 16384;
0155
0156 ret = sun7i_gmac_init(pdev, plat_dat->bsp_priv);
0157 if (ret)
0158 goto err_remove_config_dt;
0159
0160 ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
0161 if (ret)
0162 goto err_gmac_exit;
0163
0164 return 0;
0165
0166 err_gmac_exit:
0167 sun7i_gmac_exit(pdev, plat_dat->bsp_priv);
0168 err_remove_config_dt:
0169 stmmac_remove_config_dt(pdev, plat_dat);
0170
0171 return ret;
0172 }
0173
0174 static const struct of_device_id sun7i_dwmac_match[] = {
0175 { .compatible = "allwinner,sun7i-a20-gmac" },
0176 { }
0177 };
0178 MODULE_DEVICE_TABLE(of, sun7i_dwmac_match);
0179
0180 static struct platform_driver sun7i_dwmac_driver = {
0181 .probe = sun7i_gmac_probe,
0182 .remove = stmmac_pltfr_remove,
0183 .driver = {
0184 .name = "sun7i-dwmac",
0185 .pm = &stmmac_pltfr_pm_ops,
0186 .of_match_table = sun7i_dwmac_match,
0187 },
0188 };
0189 module_platform_driver(sun7i_dwmac_driver);
0190
0191 MODULE_AUTHOR("Chen-Yu Tsai <wens@csie.org>");
0192 MODULE_DESCRIPTION("Allwinner sunxi DWMAC specific glue layer");
0193 MODULE_LICENSE("GPL");