xref: /linux/drivers/net/ethernet/stmicro/stmmac/dwmac-spacemit.c (revision 0fc8f6200d2313278fbf4539bbab74677c685531)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Spacemit DWMAC platform driver
4  *
5  * Copyright (C) 2026 Inochi Amaoto <inochiama@gmail.com>
6  */
7 
8 #include <linux/clk.h>
9 #include <linux/math.h>
10 #include <linux/mod_devicetable.h>
11 #include <linux/module.h>
12 #include <linux/mfd/syscon.h>
13 #include <linux/of.h>
14 #include <linux/platform_device.h>
15 #include <linux/property.h>
16 #include <linux/regmap.h>
17 
18 #include "stmmac_platform.h"
19 
20 /* ctrl register bits */
21 #define CTRL_PHY_INTF_RGMII		BIT(3)
22 #define CTRL_PHY_INTF_MII		BIT(4)
23 #define CTRL_WAKE_IRQ_EN		BIT(9)
24 #define CTRL_PHY_IRQ_EN			BIT(12)
25 
26 /* dline register bits */
27 #define RGMII_RX_DLINE_EN		BIT(0)
28 #define RGMII_RX_DLINE_STEP		GENMASK(5, 4)
29 #define RGMII_RX_DLINE_CODE		GENMASK(15, 8)
30 #define RGMII_TX_DLINE_EN		BIT(16)
31 #define RGMII_TX_DLINE_STEP		GENMASK(21, 20)
32 #define RGMII_TX_DLINE_CODE		GENMASK(31, 24)
33 
34 #define MAX_DLINE_DELAY_CODE		0xff
35 #define MAX_WORKED_DELAY		2800
36 /* Note: the delay step value is at 0.1ps */
37 #define K3_DELAY_STEP			367
38 
39 struct spacmit_dwmac {
40 	struct regmap *apmu;
41 	unsigned int ctrl_offset;
42 	unsigned int dline_offset;
43 };
44 
45 static int spacemit_dwmac_set_delay(struct spacmit_dwmac *dwmac,
46 				    unsigned int tx_code, unsigned int rx_code)
47 {
48 	unsigned int mask, val;
49 
50 	mask = RGMII_TX_DLINE_STEP | RGMII_TX_DLINE_CODE | RGMII_TX_DLINE_EN |
51 	       RGMII_RX_DLINE_STEP | RGMII_RX_DLINE_CODE | RGMII_RX_DLINE_EN;
52 
53 	/*
54 	 * Since the delay step provided by config 0 is small enough, and
55 	 * it can cover the range of the valid delay, so there is no needed
56 	 * to use other step config.
57 	 */
58 	val = FIELD_PREP(RGMII_TX_DLINE_STEP, 0) |
59 	      FIELD_PREP(RGMII_TX_DLINE_CODE, tx_code) | RGMII_TX_DLINE_EN |
60 	      FIELD_PREP(RGMII_RX_DLINE_STEP, 0) |
61 	      FIELD_PREP(RGMII_RX_DLINE_CODE, rx_code) | RGMII_RX_DLINE_EN;
62 
63 	return regmap_update_bits(dwmac->apmu, dwmac->dline_offset,
64 				  mask, val);
65 }
66 
67 static int spacemit_dwmac_detected_delay_value(unsigned int delay)
68 {
69 	if (delay == 0)
70 		return 0;
71 
72 	if (delay > MAX_WORKED_DELAY)
73 		return -EINVAL;
74 
75 	/*
76 	 * Note K3 require a specific factor for calculate
77 	 * the delay, in this scenario it is 0.9. So the
78 	 * formula is code * step / 10 * 0.9
79 	 */
80 	return DIV_ROUND_CLOSEST(delay * 10 * 10, K3_DELAY_STEP * 9);
81 }
82 
83 static int spacemit_dwmac_fix_delay(struct spacmit_dwmac *dwmac,
84 				    unsigned int tx_delay,
85 				    unsigned int rx_delay)
86 {
87 	int rx_code;
88 	int tx_code;
89 
90 	rx_code = spacemit_dwmac_detected_delay_value(rx_delay);
91 	if (rx_code < 0)
92 		return rx_code;
93 
94 	tx_code = spacemit_dwmac_detected_delay_value(tx_delay);
95 	if (tx_code < 0)
96 		return tx_code;
97 
98 	return spacemit_dwmac_set_delay(dwmac, tx_code, rx_code);
99 }
100 
101 static int spacemit_dwmac_update_irq_config(struct spacmit_dwmac *dwmac,
102 					    struct stmmac_resources *stmmac_res)
103 {
104 	unsigned int val = stmmac_res->wol_irq >= 0 ? CTRL_WAKE_IRQ_EN : 0;
105 	unsigned int mask = CTRL_WAKE_IRQ_EN;
106 
107 	return regmap_update_bits(dwmac->apmu, dwmac->ctrl_offset,
108 				  mask, val);
109 }
110 
111 static void spacemit_get_interfaces(struct stmmac_priv *priv, void *bsp_priv,
112 				    unsigned long *interfaces)
113 {
114 	__set_bit(PHY_INTERFACE_MODE_MII, interfaces);
115 	__set_bit(PHY_INTERFACE_MODE_RMII, interfaces);
116 	phy_interface_set_rgmii(interfaces);
117 }
118 
119 static int spacemit_set_phy_intf_sel(void *bsp_priv, u8 phy_intf_sel)
120 {
121 	unsigned int mask = CTRL_PHY_INTF_MII | CTRL_PHY_INTF_RGMII;
122 	struct spacmit_dwmac *dwmac = bsp_priv;
123 	unsigned int val = 0;
124 
125 	switch (phy_intf_sel) {
126 	case PHY_INTF_SEL_GMII_MII:
127 		val = CTRL_PHY_INTF_MII;
128 		break;
129 
130 	case PHY_INTF_SEL_RMII:
131 		break;
132 
133 	case PHY_INTF_SEL_RGMII:
134 		val = CTRL_PHY_INTF_RGMII;
135 		break;
136 
137 	default:
138 		return -EINVAL;
139 	}
140 
141 	return regmap_update_bits(dwmac->apmu, dwmac->ctrl_offset,
142 				  mask, val);
143 }
144 
145 static int spacemit_dwmac_probe(struct platform_device *pdev)
146 {
147 	struct plat_stmmacenet_data *plat_dat;
148 	struct stmmac_resources stmmac_res;
149 	struct device *dev = &pdev->dev;
150 	struct spacmit_dwmac *dwmac;
151 	unsigned int offset[2];
152 	struct regmap *apmu;
153 	struct clk *clk_tx;
154 	u32 rx_delay = 0;
155 	u32 tx_delay = 0;
156 	int ret;
157 
158 	ret = stmmac_get_platform_resources(pdev, &stmmac_res);
159 	if (ret)
160 		return dev_err_probe(dev, ret,
161 				     "failed to get platform resources\n");
162 
163 	dwmac = devm_kzalloc(&pdev->dev, sizeof(*dwmac), GFP_KERNEL);
164 	if (!dwmac)
165 		return -ENOMEM;
166 
167 	plat_dat = devm_stmmac_probe_config_dt(pdev, stmmac_res.mac);
168 	if (IS_ERR(plat_dat))
169 		return dev_err_probe(dev, PTR_ERR(plat_dat),
170 				     "failed to parse DT parameters\n");
171 
172 	clk_tx = devm_clk_get_enabled(&pdev->dev, "tx");
173 	if (IS_ERR(clk_tx))
174 		return dev_err_probe(&pdev->dev, PTR_ERR(clk_tx),
175 				     "failed to get tx clock\n");
176 
177 	apmu = syscon_regmap_lookup_by_phandle_args(pdev->dev.of_node,
178 						    "spacemit,apmu", 2,
179 						    offset);
180 	if (IS_ERR(apmu))
181 		return dev_err_probe(dev, PTR_ERR(apmu),
182 				     "Failed to get apmu regmap\n");
183 
184 	dwmac->apmu = apmu;
185 	dwmac->ctrl_offset = offset[0];
186 	dwmac->dline_offset = offset[1];
187 
188 	ret = spacemit_dwmac_update_irq_config(dwmac, &stmmac_res);
189 	if (ret)
190 		return dev_err_probe(dev, ret, "Failed to configure irq config\n");
191 
192 	of_property_read_u32(pdev->dev.of_node, "tx-internal-delay-ps",
193 			     &tx_delay);
194 	of_property_read_u32(pdev->dev.of_node, "rx-internal-delay-ps",
195 			     &rx_delay);
196 
197 	plat_dat->get_interfaces = spacemit_get_interfaces;
198 	plat_dat->set_phy_intf_sel = spacemit_set_phy_intf_sel;
199 	plat_dat->bsp_priv = dwmac;
200 
201 	ret = spacemit_dwmac_fix_delay(dwmac, tx_delay, rx_delay);
202 	if (ret)
203 		return dev_err_probe(dev, ret, "Failed to configure delay\n");
204 
205 	return stmmac_dvr_probe(dev, plat_dat, &stmmac_res);
206 }
207 
208 static const struct of_device_id spacemit_dwmac_match[] = {
209 	{ .compatible = "spacemit,k3-dwmac" },
210 	{ /* sentinel */ }
211 };
212 MODULE_DEVICE_TABLE(of, spacemit_dwmac_match);
213 
214 static struct platform_driver spacemit_dwmac_driver = {
215 	.probe  = spacemit_dwmac_probe,
216 	.remove = stmmac_pltfr_remove,
217 	.driver = {
218 		.name = "spacemit-dwmac",
219 		.pm = &stmmac_pltfr_pm_ops,
220 		.of_match_table = spacemit_dwmac_match,
221 	},
222 };
223 module_platform_driver(spacemit_dwmac_driver);
224 
225 MODULE_AUTHOR("Inochi Amaoto <inochiama@gmail.com>");
226 MODULE_DESCRIPTION("Spacemit DWMAC platform driver");
227 MODULE_LICENSE("GPL");
228