1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * StarFive DWMAC platform driver
4  *
5  * Copyright (C) 2021 Emil Renner Berthing <kernel@esmil.dk>
6  * Copyright (C) 2022 StarFive Technology Co., Ltd.
7  *
8  */
9 
10 #include <linux/mod_devicetable.h>
11 #include <linux/platform_device.h>
12 #include <linux/property.h>
13 #include <linux/mfd/syscon.h>
14 #include <linux/regmap.h>
15 
16 #include "stmmac_platform.h"
17 
18 #define STARFIVE_DWMAC_PHY_INFT_RGMII	0x1
19 #define STARFIVE_DWMAC_PHY_INFT_RMII	0x4
20 #define STARFIVE_DWMAC_PHY_INFT_FIELD	0x7U
21 
22 struct starfive_dwmac {
23 	struct device *dev;
24 	struct clk *clk_tx;
25 };
26 
27 static void starfive_dwmac_fix_mac_speed(void *priv, unsigned int speed, unsigned int mode)
28 {
29 	struct starfive_dwmac *dwmac = priv;
30 	unsigned long rate;
31 	int err;
32 
33 	rate = clk_get_rate(dwmac->clk_tx);
34 
35 	switch (speed) {
36 	case SPEED_1000:
37 		rate = 125000000;
38 		break;
39 	case SPEED_100:
40 		rate = 25000000;
41 		break;
42 	case SPEED_10:
43 		rate = 2500000;
44 		break;
45 	default:
46 		dev_err(dwmac->dev, "invalid speed %u\n", speed);
47 		break;
48 	}
49 
50 	err = clk_set_rate(dwmac->clk_tx, rate);
51 	if (err)
52 		dev_err(dwmac->dev, "failed to set tx rate %lu\n", rate);
53 }
54 
55 static int starfive_dwmac_set_mode(struct plat_stmmacenet_data *plat_dat)
56 {
57 	struct starfive_dwmac *dwmac = plat_dat->bsp_priv;
58 	struct regmap *regmap;
59 	unsigned int args[2];
60 	unsigned int mode;
61 	int err;
62 
63 	switch (plat_dat->mac_interface) {
64 	case PHY_INTERFACE_MODE_RMII:
65 		mode = STARFIVE_DWMAC_PHY_INFT_RMII;
66 		break;
67 
68 	case PHY_INTERFACE_MODE_RGMII:
69 	case PHY_INTERFACE_MODE_RGMII_ID:
70 		mode = STARFIVE_DWMAC_PHY_INFT_RGMII;
71 		break;
72 
73 	default:
74 		dev_err(dwmac->dev, "unsupported interface %d\n",
75 			plat_dat->mac_interface);
76 		return -EINVAL;
77 	}
78 
79 	regmap = syscon_regmap_lookup_by_phandle_args(dwmac->dev->of_node,
80 						      "starfive,syscon",
81 						      2, args);
82 	if (IS_ERR(regmap))
83 		return dev_err_probe(dwmac->dev, PTR_ERR(regmap), "getting the regmap failed\n");
84 
85 	/* args[0]:offset  args[1]: shift */
86 	err = regmap_update_bits(regmap, args[0],
87 				 STARFIVE_DWMAC_PHY_INFT_FIELD << args[1],
88 				 mode << args[1]);
89 	if (err)
90 		return dev_err_probe(dwmac->dev, err, "error setting phy mode\n");
91 
92 	return 0;
93 }
94 
95 static int starfive_dwmac_probe(struct platform_device *pdev)
96 {
97 	struct plat_stmmacenet_data *plat_dat;
98 	struct stmmac_resources stmmac_res;
99 	struct starfive_dwmac *dwmac;
100 	struct clk *clk_gtx;
101 	int err;
102 
103 	err = stmmac_get_platform_resources(pdev, &stmmac_res);
104 	if (err)
105 		return dev_err_probe(&pdev->dev, err,
106 				     "failed to get resources\n");
107 
108 	plat_dat = stmmac_probe_config_dt(pdev, stmmac_res.mac);
109 	if (IS_ERR(plat_dat))
110 		return dev_err_probe(&pdev->dev, PTR_ERR(plat_dat),
111 				     "dt configuration failed\n");
112 
113 	dwmac = devm_kzalloc(&pdev->dev, sizeof(*dwmac), GFP_KERNEL);
114 	if (!dwmac)
115 		return -ENOMEM;
116 
117 	dwmac->clk_tx = devm_clk_get_enabled(&pdev->dev, "tx");
118 	if (IS_ERR(dwmac->clk_tx))
119 		return dev_err_probe(&pdev->dev, PTR_ERR(dwmac->clk_tx),
120 				     "error getting tx clock\n");
121 
122 	clk_gtx = devm_clk_get_enabled(&pdev->dev, "gtx");
123 	if (IS_ERR(clk_gtx))
124 		return dev_err_probe(&pdev->dev, PTR_ERR(clk_gtx),
125 				     "error getting gtx clock\n");
126 
127 	/* Generally, the rgmii_tx clock is provided by the internal clock,
128 	 * which needs to match the corresponding clock frequency according
129 	 * to different speeds. If the rgmii_tx clock is provided by the
130 	 * external rgmii_rxin, there is no need to configure the clock
131 	 * internally, because rgmii_rxin will be adaptively adjusted.
132 	 */
133 	if (!device_property_read_bool(&pdev->dev, "starfive,tx-use-rgmii-clk"))
134 		plat_dat->fix_mac_speed = starfive_dwmac_fix_mac_speed;
135 
136 	dwmac->dev = &pdev->dev;
137 	plat_dat->bsp_priv = dwmac;
138 	plat_dat->dma_cfg->dche = true;
139 
140 	err = starfive_dwmac_set_mode(plat_dat);
141 	if (err)
142 		return err;
143 
144 	err = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
145 	if (err) {
146 		stmmac_remove_config_dt(pdev, plat_dat);
147 		return err;
148 	}
149 
150 	return 0;
151 }
152 
153 static const struct of_device_id starfive_dwmac_match[] = {
154 	{ .compatible = "starfive,jh7110-dwmac"	},
155 	{ /* sentinel */ }
156 };
157 MODULE_DEVICE_TABLE(of, starfive_dwmac_match);
158 
159 static struct platform_driver starfive_dwmac_driver = {
160 	.probe  = starfive_dwmac_probe,
161 	.remove_new = stmmac_pltfr_remove,
162 	.driver = {
163 		.name = "starfive-dwmac",
164 		.pm = &stmmac_pltfr_pm_ops,
165 		.of_match_table = starfive_dwmac_match,
166 	},
167 };
168 module_platform_driver(starfive_dwmac_driver);
169 
170 MODULE_LICENSE("GPL");
171 MODULE_DESCRIPTION("StarFive DWMAC platform driver");
172 MODULE_AUTHOR("Emil Renner Berthing <kernel@esmil.dk>");
173 MODULE_AUTHOR("Samin Guo <samin.guo@starfivetech.com>");
174