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/platform_device.h>
11 #include <linux/property.h>
12 #include <linux/stmmac.h>
13
14 #include "dwmac4.h"
15 #include "stmmac.h"
16 #include "stmmac_platform.h"
17
18 struct intel_dwmac {
19 struct device *dev;
20 struct clk *tx_clk;
21 const struct intel_dwmac_data *data;
22 };
23
24 struct intel_dwmac_data {
25 unsigned long ptp_ref_clk_rate;
26 unsigned long tx_clk_rate;
27 bool tx_clk_en;
28 };
29
30 static const struct intel_dwmac_data kmb_data = {
31 .ptp_ref_clk_rate = 200000000,
32 .tx_clk_rate = 125000000,
33 .tx_clk_en = true,
34 };
35
36 static const struct of_device_id intel_eth_plat_match[] = {
37 { .compatible = "intel,keembay-dwmac", .data = &kmb_data },
38 { }
39 };
40 MODULE_DEVICE_TABLE(of, intel_eth_plat_match);
41
intel_eth_plat_probe(struct platform_device * pdev)42 static int intel_eth_plat_probe(struct platform_device *pdev)
43 {
44 struct plat_stmmacenet_data *plat_dat;
45 struct stmmac_resources stmmac_res;
46 struct intel_dwmac *dwmac;
47 unsigned long rate;
48 int ret;
49
50 ret = stmmac_get_platform_resources(pdev, &stmmac_res);
51 if (ret)
52 return ret;
53
54 plat_dat = devm_stmmac_probe_config_dt(pdev, stmmac_res.mac);
55 if (IS_ERR(plat_dat)) {
56 dev_err(&pdev->dev, "dt configuration failed\n");
57 return PTR_ERR(plat_dat);
58 }
59
60 dwmac = devm_kzalloc(&pdev->dev, sizeof(*dwmac), GFP_KERNEL);
61 if (!dwmac)
62 return -ENOMEM;
63
64 dwmac->dev = &pdev->dev;
65 dwmac->tx_clk = NULL;
66
67 /*
68 * This cannot return NULL at this point because the driver’s
69 * compatibility with the device has already been validated in
70 * platform_match().
71 */
72 dwmac->data = device_get_match_data(&pdev->dev);
73
74 /* Enable TX clock */
75 if (dwmac->data->tx_clk_en) {
76 dwmac->tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
77 if (IS_ERR(dwmac->tx_clk))
78 return PTR_ERR(dwmac->tx_clk);
79
80 ret = clk_prepare_enable(dwmac->tx_clk);
81 if (ret) {
82 dev_err(&pdev->dev,
83 "Failed to enable tx_clk\n");
84 return ret;
85 }
86
87 /* Check and configure TX clock rate */
88 rate = clk_get_rate(dwmac->tx_clk);
89 if (dwmac->data->tx_clk_rate &&
90 rate != dwmac->data->tx_clk_rate) {
91 rate = dwmac->data->tx_clk_rate;
92 ret = clk_set_rate(dwmac->tx_clk, rate);
93 if (ret) {
94 dev_err(&pdev->dev,
95 "Failed to set tx_clk\n");
96 goto err_tx_clk_disable;
97 }
98 }
99
100 /* Check and configure PTP ref clock rate */
101 rate = clk_get_rate(plat_dat->clk_ptp_ref);
102 if (dwmac->data->ptp_ref_clk_rate &&
103 rate != dwmac->data->ptp_ref_clk_rate) {
104 rate = dwmac->data->ptp_ref_clk_rate;
105 ret = clk_set_rate(plat_dat->clk_ptp_ref, rate);
106 if (ret) {
107 dev_err(&pdev->dev,
108 "Failed to set clk_ptp_ref\n");
109 goto err_tx_clk_disable;
110 }
111 }
112 }
113
114 plat_dat->clk_tx_i = dwmac->tx_clk;
115 plat_dat->set_clk_tx_rate = stmmac_set_clk_tx_rate;
116 plat_dat->bsp_priv = dwmac;
117
118 ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
119 if (ret)
120 goto err_tx_clk_disable;
121
122 return 0;
123
124 err_tx_clk_disable:
125 if (dwmac->data->tx_clk_en)
126 clk_disable_unprepare(dwmac->tx_clk);
127 return ret;
128 }
129
intel_eth_plat_remove(struct platform_device * pdev)130 static void intel_eth_plat_remove(struct platform_device *pdev)
131 {
132 struct intel_dwmac *dwmac = get_stmmac_bsp_priv(&pdev->dev);
133
134 stmmac_pltfr_remove(pdev);
135 if (dwmac->data->tx_clk_en)
136 clk_disable_unprepare(dwmac->tx_clk);
137 }
138
139 static struct platform_driver intel_eth_plat_driver = {
140 .probe = intel_eth_plat_probe,
141 .remove = intel_eth_plat_remove,
142 .driver = {
143 .name = "intel-eth-plat",
144 .pm = &stmmac_pltfr_pm_ops,
145 .of_match_table = intel_eth_plat_match,
146 },
147 };
148 module_platform_driver(intel_eth_plat_driver);
149
150 MODULE_LICENSE("GPL v2");
151 MODULE_DESCRIPTION("Intel DWMAC platform driver");
152