Linux Audio

Check our new training course

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