1 // SPDX-License-Identifier: GPL-2.0
2 /* Intel DWMAC platform driver
4 * Copyright(C) 2020 Intel Corporation
7 #include <linux/ethtool.h>
8 #include <linux/module.h>
10 #include <linux/of_device.h>
11 #include <linux/platform_device.h>
12 #include <linux/stmmac.h>
16 #include "stmmac_platform.h"
21 const struct intel_dwmac_data *data;
24 struct intel_dwmac_data {
25 void (*fix_mac_speed)(void *priv, unsigned int speed);
26 unsigned long ptp_ref_clk_rate;
27 unsigned long tx_clk_rate;
31 static void kmb_eth_fix_mac_speed(void *priv, unsigned int speed)
33 struct intel_dwmac *dwmac = priv;
37 rate = clk_get_rate(dwmac->tx_clk);
53 dev_err(dwmac->dev, "Invalid speed\n");
57 ret = clk_set_rate(dwmac->tx_clk, rate);
59 dev_err(dwmac->dev, "Failed to configure tx clock rate\n");
62 static const struct intel_dwmac_data kmb_data = {
63 .fix_mac_speed = kmb_eth_fix_mac_speed,
64 .ptp_ref_clk_rate = 200000000,
65 .tx_clk_rate = 125000000,
69 static const struct of_device_id intel_eth_plat_match[] = {
70 { .compatible = "intel,keembay-dwmac", .data = &kmb_data },
73 MODULE_DEVICE_TABLE(of, intel_eth_plat_match);
75 static int intel_eth_plat_probe(struct platform_device *pdev)
77 struct net_device *ndev = platform_get_drvdata(pdev);
78 struct stmmac_priv *priv = netdev_priv(ndev);
79 struct plat_stmmacenet_data *plat_dat;
80 struct stmmac_resources stmmac_res;
81 const struct of_device_id *match;
82 struct intel_dwmac *dwmac;
86 plat_dat = priv->plat;
87 ret = stmmac_get_platform_resources(pdev, &stmmac_res);
91 plat_dat = stmmac_probe_config_dt(pdev, &stmmac_res.mac);
92 if (IS_ERR(plat_dat)) {
93 dev_err(&pdev->dev, "dt configuration failed\n");
94 return PTR_ERR(plat_dat);
97 dwmac = devm_kzalloc(&pdev->dev, sizeof(*dwmac), GFP_KERNEL);
100 goto err_remove_config_dt;
103 dwmac->dev = &pdev->dev;
104 dwmac->tx_clk = NULL;
106 match = of_match_device(intel_eth_plat_match, &pdev->dev);
107 if (match && match->data) {
108 dwmac->data = (const struct intel_dwmac_data *)match->data;
110 if (dwmac->data->fix_mac_speed)
111 plat_dat->fix_mac_speed = dwmac->data->fix_mac_speed;
113 /* Enable TX clock */
114 if (dwmac->data->tx_clk_en) {
115 dwmac->tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
116 if (IS_ERR(dwmac->tx_clk)) {
117 ret = PTR_ERR(dwmac->tx_clk);
118 goto err_remove_config_dt;
121 clk_prepare_enable(dwmac->tx_clk);
123 /* Check and configure TX clock rate */
124 rate = clk_get_rate(dwmac->tx_clk);
125 if (dwmac->data->tx_clk_rate &&
126 rate != dwmac->data->tx_clk_rate) {
127 rate = dwmac->data->tx_clk_rate;
128 ret = clk_set_rate(dwmac->tx_clk, rate);
131 "Failed to set tx_clk\n");
132 goto err_remove_config_dt;
137 /* Check and configure PTP ref clock rate */
138 rate = clk_get_rate(plat_dat->clk_ptp_ref);
139 if (dwmac->data->ptp_ref_clk_rate &&
140 rate != dwmac->data->ptp_ref_clk_rate) {
141 rate = dwmac->data->ptp_ref_clk_rate;
142 ret = clk_set_rate(plat_dat->clk_ptp_ref, rate);
145 "Failed to set clk_ptp_ref\n");
146 goto err_remove_config_dt;
151 plat_dat->bsp_priv = dwmac;
152 plat_dat->eee_usecs_rate = plat_dat->clk_ptp_rate;
154 if (plat_dat->eee_usecs_rate > 0) {
157 tx_lpi_usec = (plat_dat->eee_usecs_rate / 1000000) - 1;
158 writel(tx_lpi_usec, stmmac_res.addr + GMAC_1US_TIC_COUNTER);
161 ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
163 clk_disable_unprepare(dwmac->tx_clk);
164 goto err_remove_config_dt;
169 err_remove_config_dt:
170 stmmac_remove_config_dt(pdev, plat_dat);
175 static int intel_eth_plat_remove(struct platform_device *pdev)
177 struct intel_dwmac *dwmac = get_stmmac_bsp_priv(&pdev->dev);
180 ret = stmmac_pltfr_remove(pdev);
181 clk_disable_unprepare(dwmac->tx_clk);
186 static struct platform_driver intel_eth_plat_driver = {
187 .probe = intel_eth_plat_probe,
188 .remove = intel_eth_plat_remove,
190 .name = "intel-eth-plat",
191 .pm = &stmmac_pltfr_pm_ops,
192 .of_match_table = intel_eth_plat_match,
195 module_platform_driver(intel_eth_plat_driver);
197 MODULE_LICENSE("GPL v2");
198 MODULE_DESCRIPTION("Intel DWMAC platform driver");