Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Giuseppe CAVALLARO | 732fdf0 | 2014-11-18 09:47:01 +0100 | [diff] [blame] | 2 | /* |
Chen-Yu Tsai | af0bd4e | 2014-01-17 21:24:47 +0800 | [diff] [blame] | 3 | * dwmac-sunxi.c - Allwinner sunxi DWMAC specific glue layer |
| 4 | * |
| 5 | * Copyright (C) 2013 Chen-Yu Tsai |
| 6 | * |
| 7 | * Chen-Yu Tsai <wens@csie.org> |
Chen-Yu Tsai | af0bd4e | 2014-01-17 21:24:47 +0800 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/stmmac.h> |
| 11 | #include <linux/clk.h> |
Joachim Eastwood | 4198b7d | 2015-05-14 12:11:05 +0200 | [diff] [blame] | 12 | #include <linux/module.h> |
Chen-Yu Tsai | af0bd4e | 2014-01-17 21:24:47 +0800 | [diff] [blame] | 13 | #include <linux/phy.h> |
Joachim Eastwood | 4198b7d | 2015-05-14 12:11:05 +0200 | [diff] [blame] | 14 | #include <linux/platform_device.h> |
Chen-Yu Tsai | af0bd4e | 2014-01-17 21:24:47 +0800 | [diff] [blame] | 15 | #include <linux/of_net.h> |
| 16 | #include <linux/regulator/consumer.h> |
| 17 | |
Andy Shevchenko | f10f9fb | 2014-11-07 16:46:42 +0200 | [diff] [blame] | 18 | #include "stmmac_platform.h" |
| 19 | |
Chen-Yu Tsai | af0bd4e | 2014-01-17 21:24:47 +0800 | [diff] [blame] | 20 | struct sunxi_priv_data { |
| 21 | int interface; |
| 22 | int clk_enabled; |
| 23 | struct clk *tx_clk; |
| 24 | struct regulator *regulator; |
| 25 | }; |
| 26 | |
Chen-Yu Tsai | af0bd4e | 2014-01-17 21:24:47 +0800 | [diff] [blame] | 27 | #define SUN7I_GMAC_GMII_RGMII_RATE 125000000 |
| 28 | #define SUN7I_GMAC_MII_RATE 25000000 |
| 29 | |
| 30 | static int sun7i_gmac_init(struct platform_device *pdev, void *priv) |
| 31 | { |
| 32 | struct sunxi_priv_data *gmac = priv; |
| 33 | int ret; |
| 34 | |
| 35 | if (gmac->regulator) { |
| 36 | ret = regulator_enable(gmac->regulator); |
| 37 | if (ret) |
| 38 | return ret; |
| 39 | } |
| 40 | |
| 41 | /* Set GMAC interface port mode |
| 42 | * |
| 43 | * The GMAC TX clock lines are configured by setting the clock |
| 44 | * rate, which then uses the auto-reparenting feature of the |
| 45 | * clock driver, and enabling/disabling the clock. |
| 46 | */ |
| 47 | if (gmac->interface == PHY_INTERFACE_MODE_RGMII) { |
| 48 | clk_set_rate(gmac->tx_clk, SUN7I_GMAC_GMII_RGMII_RATE); |
| 49 | clk_prepare_enable(gmac->tx_clk); |
| 50 | gmac->clk_enabled = 1; |
| 51 | } else { |
| 52 | clk_set_rate(gmac->tx_clk, SUN7I_GMAC_MII_RATE); |
Kangjie Lu | f86a3b8 | 2018-12-25 20:57:14 -0600 | [diff] [blame] | 53 | ret = clk_prepare(gmac->tx_clk); |
| 54 | if (ret) |
| 55 | return ret; |
Chen-Yu Tsai | af0bd4e | 2014-01-17 21:24:47 +0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | static void sun7i_gmac_exit(struct platform_device *pdev, void *priv) |
| 62 | { |
| 63 | struct sunxi_priv_data *gmac = priv; |
| 64 | |
| 65 | if (gmac->clk_enabled) { |
| 66 | clk_disable(gmac->tx_clk); |
| 67 | gmac->clk_enabled = 0; |
| 68 | } |
| 69 | clk_unprepare(gmac->tx_clk); |
| 70 | |
| 71 | if (gmac->regulator) |
| 72 | regulator_disable(gmac->regulator); |
| 73 | } |
| 74 | |
| 75 | static void sun7i_fix_speed(void *priv, unsigned int speed) |
| 76 | { |
| 77 | struct sunxi_priv_data *gmac = priv; |
| 78 | |
| 79 | /* only GMII mode requires us to reconfigure the clock lines */ |
| 80 | if (gmac->interface != PHY_INTERFACE_MODE_GMII) |
| 81 | return; |
| 82 | |
| 83 | if (gmac->clk_enabled) { |
| 84 | clk_disable(gmac->tx_clk); |
| 85 | gmac->clk_enabled = 0; |
| 86 | } |
| 87 | clk_unprepare(gmac->tx_clk); |
| 88 | |
| 89 | if (speed == 1000) { |
| 90 | clk_set_rate(gmac->tx_clk, SUN7I_GMAC_GMII_RGMII_RATE); |
| 91 | clk_prepare_enable(gmac->tx_clk); |
| 92 | gmac->clk_enabled = 1; |
| 93 | } else { |
| 94 | clk_set_rate(gmac->tx_clk, SUN7I_GMAC_MII_RATE); |
| 95 | clk_prepare(gmac->tx_clk); |
| 96 | } |
| 97 | } |
| 98 | |
Joachim Eastwood | 9a9e9a1 | 2015-07-29 00:08:54 +0200 | [diff] [blame] | 99 | static int sun7i_gmac_probe(struct platform_device *pdev) |
Joachim Eastwood | 22caae0 | 2015-07-29 00:08:53 +0200 | [diff] [blame] | 100 | { |
Joachim Eastwood | 9a9e9a1 | 2015-07-29 00:08:54 +0200 | [diff] [blame] | 101 | struct plat_stmmacenet_data *plat_dat; |
| 102 | struct stmmac_resources stmmac_res; |
Joachim Eastwood | 22caae0 | 2015-07-29 00:08:53 +0200 | [diff] [blame] | 103 | struct sunxi_priv_data *gmac; |
| 104 | struct device *dev = &pdev->dev; |
Joachim Eastwood | 9a9e9a1 | 2015-07-29 00:08:54 +0200 | [diff] [blame] | 105 | int ret; |
| 106 | |
| 107 | ret = stmmac_get_platform_resources(pdev, &stmmac_res); |
| 108 | if (ret) |
| 109 | return ret; |
| 110 | |
| 111 | plat_dat = stmmac_probe_config_dt(pdev, &stmmac_res.mac); |
| 112 | if (IS_ERR(plat_dat)) |
| 113 | return PTR_ERR(plat_dat); |
Joachim Eastwood | 22caae0 | 2015-07-29 00:08:53 +0200 | [diff] [blame] | 114 | |
| 115 | gmac = devm_kzalloc(dev, sizeof(*gmac), GFP_KERNEL); |
Johan Hovold | d2ed0a7 | 2016-11-30 15:29:55 +0100 | [diff] [blame] | 116 | if (!gmac) { |
| 117 | ret = -ENOMEM; |
| 118 | goto err_remove_config_dt; |
| 119 | } |
Joachim Eastwood | 22caae0 | 2015-07-29 00:08:53 +0200 | [diff] [blame] | 120 | |
| 121 | gmac->interface = of_get_phy_mode(dev->of_node); |
| 122 | |
| 123 | gmac->tx_clk = devm_clk_get(dev, "allwinner_gmac_tx"); |
| 124 | if (IS_ERR(gmac->tx_clk)) { |
| 125 | dev_err(dev, "could not get tx clock\n"); |
Johan Hovold | d2ed0a7 | 2016-11-30 15:29:55 +0100 | [diff] [blame] | 126 | ret = PTR_ERR(gmac->tx_clk); |
| 127 | goto err_remove_config_dt; |
Joachim Eastwood | 22caae0 | 2015-07-29 00:08:53 +0200 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /* Optional regulator for PHY */ |
| 131 | gmac->regulator = devm_regulator_get_optional(dev, "phy"); |
| 132 | if (IS_ERR(gmac->regulator)) { |
Johan Hovold | d2ed0a7 | 2016-11-30 15:29:55 +0100 | [diff] [blame] | 133 | if (PTR_ERR(gmac->regulator) == -EPROBE_DEFER) { |
| 134 | ret = -EPROBE_DEFER; |
| 135 | goto err_remove_config_dt; |
| 136 | } |
Joachim Eastwood | 22caae0 | 2015-07-29 00:08:53 +0200 | [diff] [blame] | 137 | dev_info(dev, "no regulator found\n"); |
| 138 | gmac->regulator = NULL; |
| 139 | } |
| 140 | |
Joachim Eastwood | 9a9e9a1 | 2015-07-29 00:08:54 +0200 | [diff] [blame] | 141 | /* platform data specifying hardware features and callbacks. |
| 142 | * hardware features were copied from Allwinner drivers. */ |
| 143 | plat_dat->tx_coe = 1; |
| 144 | plat_dat->has_gmac = true; |
| 145 | plat_dat->bsp_priv = gmac; |
| 146 | plat_dat->init = sun7i_gmac_init; |
| 147 | plat_dat->exit = sun7i_gmac_exit; |
| 148 | plat_dat->fix_mac_speed = sun7i_fix_speed; |
| 149 | |
| 150 | ret = sun7i_gmac_init(pdev, plat_dat->bsp_priv); |
| 151 | if (ret) |
Johan Hovold | d2ed0a7 | 2016-11-30 15:29:55 +0100 | [diff] [blame] | 152 | goto err_remove_config_dt; |
Joachim Eastwood | 9a9e9a1 | 2015-07-29 00:08:54 +0200 | [diff] [blame] | 153 | |
Chen-Yu Tsai | d856c16 | 2015-12-11 18:03:49 +0800 | [diff] [blame] | 154 | ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res); |
| 155 | if (ret) |
Johan Hovold | d2ed0a7 | 2016-11-30 15:29:55 +0100 | [diff] [blame] | 156 | goto err_gmac_exit; |
| 157 | |
| 158 | return 0; |
| 159 | |
| 160 | err_gmac_exit: |
| 161 | sun7i_gmac_exit(pdev, plat_dat->bsp_priv); |
| 162 | err_remove_config_dt: |
| 163 | stmmac_remove_config_dt(pdev, plat_dat); |
Chen-Yu Tsai | d856c16 | 2015-12-11 18:03:49 +0800 | [diff] [blame] | 164 | |
| 165 | return ret; |
Joachim Eastwood | 22caae0 | 2015-07-29 00:08:53 +0200 | [diff] [blame] | 166 | } |
| 167 | |
Joachim Eastwood | 4198b7d | 2015-05-14 12:11:05 +0200 | [diff] [blame] | 168 | static const struct of_device_id sun7i_dwmac_match[] = { |
Joachim Eastwood | 9a9e9a1 | 2015-07-29 00:08:54 +0200 | [diff] [blame] | 169 | { .compatible = "allwinner,sun7i-a20-gmac" }, |
Joachim Eastwood | 4198b7d | 2015-05-14 12:11:05 +0200 | [diff] [blame] | 170 | { } |
| 171 | }; |
| 172 | MODULE_DEVICE_TABLE(of, sun7i_dwmac_match); |
| 173 | |
| 174 | static struct platform_driver sun7i_dwmac_driver = { |
Joachim Eastwood | 9a9e9a1 | 2015-07-29 00:08:54 +0200 | [diff] [blame] | 175 | .probe = sun7i_gmac_probe, |
Joachim Eastwood | 4198b7d | 2015-05-14 12:11:05 +0200 | [diff] [blame] | 176 | .remove = stmmac_pltfr_remove, |
| 177 | .driver = { |
| 178 | .name = "sun7i-dwmac", |
| 179 | .pm = &stmmac_pltfr_pm_ops, |
| 180 | .of_match_table = sun7i_dwmac_match, |
| 181 | }, |
| 182 | }; |
| 183 | module_platform_driver(sun7i_dwmac_driver); |
| 184 | |
| 185 | MODULE_AUTHOR("Chen-Yu Tsai <wens@csie.org>"); |
| 186 | MODULE_DESCRIPTION("Allwinner sunxi DWMAC specific glue layer"); |
| 187 | MODULE_LICENSE("GPL"); |