1 // SPDX-License-Identifier: GPL-2.0
2 /* Intel DWMAC platform driver
3  *
4  * Copyright(C) 2020 Intel Corporation
5  */
6 
7 #include <linux/ethtool.h>
8 #include <linux/module.h>
9 #include <linux/of.h>
10 #include <linux/of_device.h>
11 #include <linux/platform_device.h>
12 #include <linux/stmmac.h>
13 
14 #include "stmmac.h"
15 #include "stmmac_platform.h"
16 
17 struct intel_dwmac {
18 	struct device *dev;
19 	struct clk *tx_clk;
20 	const struct intel_dwmac_data *data;
21 };
22 
23 struct intel_dwmac_data {
24 	void (*fix_mac_speed)(void *priv, unsigned int speed);
25 	unsigned long ptp_ref_clk_rate;
26 	unsigned long tx_clk_rate;
27 	bool tx_clk_en;
28 };
29 
30 static void kmb_eth_fix_mac_speed(void *priv, unsigned int speed)
31 {
32 	struct intel_dwmac *dwmac = priv;
33 	unsigned long rate;
34 	int ret;
35 
36 	rate = clk_get_rate(dwmac->tx_clk);
37 
38 	switch (speed) {
39 	case SPEED_1000:
40 		rate = 125000000;
41 		break;
42 
43 	case SPEED_100:
44 		rate = 25000000;
45 		break;
46 
47 	case SPEED_10:
48 		rate = 2500000;
49 		break;
50 
51 	default:
52 		dev_err(dwmac->dev, "Invalid speed\n");
53 		break;
54 	}
55 
56 	ret = clk_set_rate(dwmac->tx_clk, rate);
57 	if (ret)
58 		dev_err(dwmac->dev, "Failed to configure tx clock rate\n");
59 }
60 
61 static const struct intel_dwmac_data kmb_data = {
62 	.fix_mac_speed = kmb_eth_fix_mac_speed,
63 	.ptp_ref_clk_rate = 200000000,
64 	.tx_clk_rate = 125000000,
65 	.tx_clk_en = true,
66 };
67 
68 static const struct of_device_id intel_eth_plat_match[] = {
69 	{ .compatible = "intel,keembay-dwmac", .data = &kmb_data },
70 	{ }
71 };
72 MODULE_DEVICE_TABLE(of, intel_eth_plat_match);
73 
74 static int intel_eth_plat_probe(struct platform_device *pdev)
75 {
76 	struct net_device *ndev = platform_get_drvdata(pdev);
77 	struct stmmac_priv *priv = netdev_priv(ndev);
78 	struct plat_stmmacenet_data *plat_dat;
79 	struct stmmac_resources stmmac_res;
80 	const struct of_device_id *match;
81 	struct intel_dwmac *dwmac;
82 	unsigned long rate;
83 	int ret;
84 
85 	plat_dat = priv->plat;
86 	ret = stmmac_get_platform_resources(pdev, &stmmac_res);
87 	if (ret)
88 		return ret;
89 
90 	plat_dat = stmmac_probe_config_dt(pdev, &stmmac_res.mac);
91 	if (IS_ERR(plat_dat)) {
92 		dev_err(&pdev->dev, "dt configuration failed\n");
93 		return PTR_ERR(plat_dat);
94 	}
95 
96 	dwmac = devm_kzalloc(&pdev->dev, sizeof(*dwmac), GFP_KERNEL);
97 	if (!dwmac) {
98 		ret = -ENOMEM;
99 		goto err_remove_config_dt;
100 	}
101 
102 	dwmac->dev = &pdev->dev;
103 	dwmac->tx_clk = NULL;
104 
105 	match = of_match_device(intel_eth_plat_match, &pdev->dev);
106 	if (match && match->data) {
107 		dwmac->data = (const struct intel_dwmac_data *)match->data;
108 
109 		if (dwmac->data->fix_mac_speed)
110 			plat_dat->fix_mac_speed = dwmac->data->fix_mac_speed;
111 
112 		/* Enable TX clock */
113 		if (dwmac->data->tx_clk_en) {
114 			dwmac->tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
115 			if (IS_ERR(dwmac->tx_clk))
116 				goto err_remove_config_dt;
117 
118 			clk_prepare_enable(dwmac->tx_clk);
119 
120 			/* Check and configure TX clock rate */
121 			rate = clk_get_rate(dwmac->tx_clk);
122 			if (dwmac->data->tx_clk_rate &&
123 			    rate != dwmac->data->tx_clk_rate) {
124 				rate = dwmac->data->tx_clk_rate;
125 				ret = clk_set_rate(dwmac->tx_clk, rate);
126 				if (ret) {
127 					dev_err(&pdev->dev,
128 						"Failed to set tx_clk\n");
129 					return ret;
130 				}
131 			}
132 		}
133 
134 		/* Check and configure PTP ref clock rate */
135 		rate = clk_get_rate(plat_dat->clk_ptp_ref);
136 		if (dwmac->data->ptp_ref_clk_rate &&
137 		    rate != dwmac->data->ptp_ref_clk_rate) {
138 			rate = dwmac->data->ptp_ref_clk_rate;
139 			ret = clk_set_rate(plat_dat->clk_ptp_ref, rate);
140 			if (ret) {
141 				dev_err(&pdev->dev,
142 					"Failed to set clk_ptp_ref\n");
143 				return ret;
144 			}
145 		}
146 	}
147 
148 	plat_dat->bsp_priv = dwmac;
149 
150 	ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
151 	if (ret) {
152 		if (dwmac->tx_clk)
153 			clk_disable_unprepare(dwmac->tx_clk);
154 
155 		goto err_remove_config_dt;
156 	}
157 
158 	return 0;
159 
160 err_remove_config_dt:
161 	stmmac_remove_config_dt(pdev, plat_dat);
162 
163 	return ret;
164 }
165 
166 static int intel_eth_plat_remove(struct platform_device *pdev)
167 {
168 	struct intel_dwmac *dwmac = get_stmmac_bsp_priv(&pdev->dev);
169 	int ret;
170 
171 	ret = stmmac_pltfr_remove(pdev);
172 
173 	if (dwmac->tx_clk)
174 		clk_disable_unprepare(dwmac->tx_clk);
175 
176 	return ret;
177 }
178 
179 static struct platform_driver intel_eth_plat_driver = {
180 	.probe  = intel_eth_plat_probe,
181 	.remove = intel_eth_plat_remove,
182 	.driver = {
183 		.name		= "intel-eth-plat",
184 		.pm		= &stmmac_pltfr_pm_ops,
185 		.of_match_table = intel_eth_plat_match,
186 	},
187 };
188 module_platform_driver(intel_eth_plat_driver);
189 
190 MODULE_LICENSE("GPL v2");
191 MODULE_DESCRIPTION("Intel DWMAC platform driver");
192