xref: /linux/drivers/net/ethernet/stmicro/stmmac/dwmac-nuvoton.c (revision fcee7d82f27d6a8b1ddc5bbefda59b4e441e9bc0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Nuvoton DWMAC specific glue layer
4  *
5  * Copyright (C) 2025 Nuvoton Technology Corp.
6  *
7  * Author: Joey Lu <a0987203069@gmail.com>
8  */
9 
10 #include <linux/mfd/syscon.h>
11 #include <linux/mod_devicetable.h>
12 #include <linux/of.h>
13 #include <linux/of_net.h>
14 #include <linux/platform_device.h>
15 #include <linux/regmap.h>
16 #include <linux/stmmac.h>
17 
18 #include "stmmac_platform.h"
19 
20 #define NVT_REG_SYS_GMAC0MISCR  0x108
21 #define NVT_REG_SYS_GMAC1MISCR  0x10C
22 
23 #define NVT_MISCR_RMII          BIT(0)
24 
25 /* Two thousand picoseconds are evenly mapped to a 4-bit field,
26  * resulting in each step being 2000/15 picoseconds.
27  */
28 #define NVT_PATH_DELAY_STEP     134
29 #define NVT_TX_DELAY_MASK       GENMASK(19, 16)
30 #define NVT_RX_DELAY_MASK       GENMASK(23, 20)
31 
32 struct nvt_priv_data {
33 	struct device *dev;
34 	struct regmap *regmap;
35 	u32 macid;
36 };
37 
nvt_gmac_get_delay(struct device * dev,const char * property)38 static int nvt_gmac_get_delay(struct device *dev, const char *property)
39 {
40 	u32 arg;
41 
42 	if (of_property_read_u32(dev->of_node, property, &arg))
43 		return 0;
44 
45 	if (arg > 2000)
46 		return -EINVAL;
47 
48 	if (arg == 2000)
49 		return 15;
50 
51 	return arg / NVT_PATH_DELAY_STEP;
52 }
53 
nvt_set_phy_intf_sel(void * bsp_priv,u8 phy_intf_sel)54 static int nvt_set_phy_intf_sel(void *bsp_priv, u8 phy_intf_sel)
55 {
56 	struct nvt_priv_data *priv = bsp_priv;
57 	u32 reg, val;
58 	int ret;
59 
60 	if (phy_intf_sel == PHY_INTF_SEL_RGMII) {
61 		ret = nvt_gmac_get_delay(priv->dev, "rx-internal-delay-ps");
62 		if (ret < 0)
63 			return ret;
64 		val = FIELD_PREP(NVT_RX_DELAY_MASK, ret);
65 
66 		ret = nvt_gmac_get_delay(priv->dev, "tx-internal-delay-ps");
67 		if (ret < 0)
68 			return ret;
69 		val |= FIELD_PREP(NVT_TX_DELAY_MASK, ret);
70 	} else if (phy_intf_sel == PHY_INTF_SEL_RMII) {
71 		val = NVT_MISCR_RMII;
72 	} else {
73 		return -EINVAL;
74 	}
75 
76 	reg = (priv->macid == 0) ? NVT_REG_SYS_GMAC0MISCR : NVT_REG_SYS_GMAC1MISCR;
77 	regmap_update_bits(priv->regmap, reg,
78 			   NVT_RX_DELAY_MASK | NVT_TX_DELAY_MASK | NVT_MISCR_RMII, val);
79 
80 	return 0;
81 }
82 
nvt_gmac_probe(struct platform_device * pdev)83 static int nvt_gmac_probe(struct platform_device *pdev)
84 {
85 	struct plat_stmmacenet_data *plat_dat;
86 	struct stmmac_resources stmmac_res;
87 	struct device *dev = &pdev->dev;
88 	struct nvt_priv_data *priv;
89 	int ret;
90 
91 	ret = stmmac_get_platform_resources(pdev, &stmmac_res);
92 	if (ret)
93 		return dev_err_probe(dev, ret, "Failed to get platform resources\n");
94 
95 	plat_dat = devm_stmmac_probe_config_dt(pdev, stmmac_res.mac);
96 	if (IS_ERR(plat_dat))
97 		return dev_err_probe(dev, PTR_ERR(plat_dat), "Failed to get platform data\n");
98 
99 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
100 	if (!priv)
101 		return dev_err_probe(dev, -ENOMEM, "Failed to allocate private data\n");
102 
103 	priv->dev = dev;
104 
105 	priv->regmap = syscon_regmap_lookup_by_phandle_args(dev->of_node, "nuvoton,sys",
106 							    1, &priv->macid);
107 	if (IS_ERR(priv->regmap))
108 		return dev_err_probe(dev, PTR_ERR(priv->regmap), "Failed to get sys register\n");
109 
110 	if (priv->macid > 1)
111 		return dev_err_probe(dev, -EINVAL, "Invalid sys arguments\n");
112 
113 	plat_dat->bsp_priv = priv;
114 	plat_dat->set_phy_intf_sel = nvt_set_phy_intf_sel;
115 
116 	return stmmac_pltfr_probe(pdev, plat_dat, &stmmac_res);
117 }
118 
119 static const struct of_device_id nvt_dwmac_match[] = {
120 	{ .compatible = "nuvoton,ma35d1-dwmac"},
121 	{ }
122 };
123 MODULE_DEVICE_TABLE(of, nvt_dwmac_match);
124 
125 static struct platform_driver nvt_dwmac_driver = {
126 	.probe  = nvt_gmac_probe,
127 	.remove = stmmac_pltfr_remove,
128 	.driver = {
129 		.name           = "nuvoton-dwmac",
130 		.pm		= &stmmac_pltfr_pm_ops,
131 		.of_match_table = nvt_dwmac_match,
132 	},
133 };
134 module_platform_driver(nvt_dwmac_driver);
135 
136 MODULE_AUTHOR("Joey Lu <a0987203069@gmail.com>");
137 MODULE_DESCRIPTION("Nuvoton DWMAC specific glue layer");
138 MODULE_LICENSE("GPL");
139