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