1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * dwmac-renesas-gbeth.c - DWMAC Specific Glue layer for Renesas GBETH
4  *
5  * The Rx and Tx clocks are supplied as follows for the GBETH IP.
6  *
7  *                         Rx / Tx
8  *   -------+------------- on / off -------
9  *          |
10  *          |            Rx-180 / Tx-180
11  *          +---- not ---- on / off -------
12  *
13  * Copyright (C) 2025 Renesas Electronics Corporation
14  */
15 
16 #include <linux/clk.h>
17 #include <linux/device.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/reset.h>
21 
22 #include "stmmac_platform.h"
23 
24 struct renesas_gbeth {
25 	struct plat_stmmacenet_data *plat_dat;
26 	struct reset_control *rstc;
27 	struct device *dev;
28 };
29 
30 static const char *const renesas_gbeth_clks[] = {
31 	"tx", "tx-180", "rx", "rx-180",
32 };
33 
34 static int renesas_gbeth_init(struct platform_device *pdev, void *priv)
35 {
36 	struct plat_stmmacenet_data *plat_dat;
37 	struct renesas_gbeth *gbeth = priv;
38 	int ret;
39 
40 	plat_dat = gbeth->plat_dat;
41 
42 	ret = reset_control_deassert(gbeth->rstc);
43 	if (ret) {
44 		dev_err(gbeth->dev, "Reset deassert failed\n");
45 		return ret;
46 	}
47 
48 	ret = clk_bulk_prepare_enable(plat_dat->num_clks,
49 				      plat_dat->clks);
50 	if (ret)
51 		reset_control_assert(gbeth->rstc);
52 
53 	return ret;
54 }
55 
56 static void renesas_gbeth_exit(struct platform_device *pdev, void *priv)
57 {
58 	struct plat_stmmacenet_data *plat_dat;
59 	struct renesas_gbeth *gbeth = priv;
60 	int ret;
61 
62 	plat_dat = gbeth->plat_dat;
63 
64 	clk_bulk_disable_unprepare(plat_dat->num_clks, plat_dat->clks);
65 
66 	ret = reset_control_assert(gbeth->rstc);
67 	if (ret)
68 		dev_err(gbeth->dev, "Reset assert failed\n");
69 }
70 
71 static int renesas_gbeth_probe(struct platform_device *pdev)
72 {
73 	struct plat_stmmacenet_data *plat_dat;
74 	struct stmmac_resources stmmac_res;
75 	struct device *dev = &pdev->dev;
76 	struct renesas_gbeth *gbeth;
77 	unsigned int i;
78 	int err;
79 
80 	err = stmmac_get_platform_resources(pdev, &stmmac_res);
81 	if (err)
82 		return dev_err_probe(dev, err,
83 				     "failed to get resources\n");
84 
85 	plat_dat = devm_stmmac_probe_config_dt(pdev, stmmac_res.mac);
86 	if (IS_ERR(plat_dat))
87 		return dev_err_probe(dev, PTR_ERR(plat_dat),
88 				     "dt configuration failed\n");
89 
90 	gbeth = devm_kzalloc(dev, sizeof(*gbeth), GFP_KERNEL);
91 	if (!gbeth)
92 		return -ENOMEM;
93 
94 	plat_dat->num_clks = ARRAY_SIZE(renesas_gbeth_clks);
95 	plat_dat->clks = devm_kcalloc(dev, plat_dat->num_clks,
96 				      sizeof(*plat_dat->clks), GFP_KERNEL);
97 	if (!plat_dat->clks)
98 		return -ENOMEM;
99 
100 	for (i = 0; i < plat_dat->num_clks; i++)
101 		plat_dat->clks[i].id = renesas_gbeth_clks[i];
102 
103 	err = devm_clk_bulk_get(dev, plat_dat->num_clks, plat_dat->clks);
104 	if (err < 0)
105 		return err;
106 
107 	plat_dat->clk_tx_i = stmmac_pltfr_find_clk(plat_dat, "tx");
108 	if (!plat_dat->clk_tx_i)
109 		return dev_err_probe(dev, -EINVAL,
110 				     "error finding tx clock\n");
111 
112 	gbeth->rstc = devm_reset_control_get_exclusive(dev, NULL);
113 	if (IS_ERR(gbeth->rstc))
114 		return PTR_ERR(gbeth->rstc);
115 
116 	gbeth->dev = dev;
117 	gbeth->plat_dat = plat_dat;
118 	plat_dat->bsp_priv = gbeth;
119 	plat_dat->set_clk_tx_rate = stmmac_set_clk_tx_rate;
120 	plat_dat->init = renesas_gbeth_init;
121 	plat_dat->exit = renesas_gbeth_exit;
122 	plat_dat->flags |= STMMAC_FLAG_HWTSTAMP_CORRECT_LATENCY |
123 			   STMMAC_FLAG_EN_TX_LPI_CLK_PHY_CAP |
124 			   STMMAC_FLAG_SPH_DISABLE;
125 
126 	return devm_stmmac_pltfr_probe(pdev, plat_dat, &stmmac_res);
127 }
128 
129 static const struct of_device_id renesas_gbeth_match[] = {
130 	{ .compatible = "renesas,rzv2h-gbeth", },
131 	{ /* Sentinel */ }
132 };
133 MODULE_DEVICE_TABLE(of, renesas_gbeth_match);
134 
135 static struct platform_driver renesas_gbeth_driver = {
136 	.probe  = renesas_gbeth_probe,
137 	.driver = {
138 		.name		= "renesas-gbeth",
139 		.of_match_table	= renesas_gbeth_match,
140 	},
141 };
142 module_platform_driver(renesas_gbeth_driver);
143 
144 MODULE_AUTHOR("Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>");
145 MODULE_DESCRIPTION("Renesas GBETH DWMAC Specific Glue layer");
146 MODULE_LICENSE("GPL");
147