aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/ethernet/stmicro/stmmac/dwmac-renesas-gbeth.c
blob: bc7bb975803ca08f71f97c2f4bea5d367a89a83b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// SPDX-License-Identifier: GPL-2.0+
/*
 * dwmac-renesas-gbeth.c - DWMAC Specific Glue layer for Renesas GBETH
 *
 * The Rx and Tx clocks are supplied as follows for the GBETH IP.
 *
 *                         Rx / Tx
 *   -------+------------- on / off -------
 *          |
 *          |            Rx-180 / Tx-180
 *          +---- not ---- on / off -------
 *
 * Copyright (C) 2025 Renesas Electronics Corporation
 */

#include <linux/clk.h>
#include <linux/device.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/pcs-rzn1-miic.h>
#include <linux/platform_device.h>
#include <linux/reset.h>
#include <linux/types.h>

#include "stmmac_platform.h"

/**
 * struct renesas_gbeth_of_data - OF data for Renesas GBETH
 *
 * @clks: Array of clock names
 * @num_clks: Number of clocks
 * @stmmac_flags: Flags for the stmmac driver
 * @handle_reset: Flag to indicate if reset control is
 *                handled by the glue driver or core driver.
 * @set_clk_tx_rate: Flag to indicate if Tx clock is fixed or
 *                   set_clk_tx_rate is needed.
 * @has_pcs: Flag to indicate if the MAC has a PCS
 */
struct renesas_gbeth_of_data {
	const char * const *clks;
	u8 num_clks;
	u32 stmmac_flags;
	bool handle_reset;
	bool set_clk_tx_rate;
	bool has_pcs;
};

struct renesas_gbeth {
	const struct renesas_gbeth_of_data *of_data;
	struct plat_stmmacenet_data *plat_dat;
	struct reset_control *rstc;
	struct device *dev;
};

static const char *const renesas_gbeth_clks[] = {
	"tx", "tx-180", "rx", "rx-180",
};

static const char *const renesas_gmac_clks[] = {
	"tx",
};

static int renesas_gmac_pcs_init(struct stmmac_priv *priv)
{
	struct device_node *np = priv->device->of_node;
	struct device_node *pcs_node;
	struct phylink_pcs *pcs;

	pcs_node = of_parse_phandle(np, "pcs-handle", 0);
	if (pcs_node) {
		pcs = miic_create(priv->device, pcs_node);
		of_node_put(pcs_node);
		if (IS_ERR(pcs))
			return PTR_ERR(pcs);

		priv->hw->phylink_pcs = pcs;
	}

	return 0;
}

static void renesas_gmac_pcs_exit(struct stmmac_priv *priv)
{
	if (priv->hw->phylink_pcs)
		miic_destroy(priv->hw->phylink_pcs);
}

static struct phylink_pcs *renesas_gmac_select_pcs(struct stmmac_priv *priv,
						   phy_interface_t interface)
{
	return priv->hw->phylink_pcs;
}

static int renesas_gbeth_init(struct platform_device *pdev, void *priv)
{
	struct plat_stmmacenet_data *plat_dat;
	struct renesas_gbeth *gbeth = priv;
	int ret;

	plat_dat = gbeth->plat_dat;

	ret = reset_control_deassert(gbeth->rstc);
	if (ret) {
		dev_err(gbeth->dev, "Reset deassert failed\n");
		return ret;
	}

	ret = clk_bulk_prepare_enable(plat_dat->num_clks,
				      plat_dat->clks);
	if (ret)
		reset_control_assert(gbeth->rstc);

	return ret;
}

static void renesas_gbeth_exit(struct platform_device *pdev, void *priv)
{
	struct plat_stmmacenet_data *plat_dat;
	struct renesas_gbeth *gbeth = priv;
	int ret;

	plat_dat = gbeth->plat_dat;

	clk_bulk_disable_unprepare(plat_dat->num_clks, plat_dat->clks);

	ret = reset_control_assert(gbeth->rstc);
	if (ret)
		dev_err(gbeth->dev, "Reset assert failed\n");
}

static int renesas_gbeth_probe(struct platform_device *pdev)
{
	const struct renesas_gbeth_of_data *of_data;
	struct plat_stmmacenet_data *plat_dat;
	struct stmmac_resources stmmac_res;
	struct device *dev = &pdev->dev;
	struct renesas_gbeth *gbeth;
	unsigned int i;
	int err;

	err = stmmac_get_platform_resources(pdev, &stmmac_res);
	if (err)
		return dev_err_probe(dev, err,
				     "failed to get resources\n");

	plat_dat = devm_stmmac_probe_config_dt(pdev, stmmac_res.mac);
	if (IS_ERR(plat_dat))
		return dev_err_probe(dev, PTR_ERR(plat_dat),
				     "dt configuration failed\n");

	gbeth = devm_kzalloc(dev, sizeof(*gbeth), GFP_KERNEL);
	if (!gbeth)
		return -ENOMEM;

	of_data = of_device_get_match_data(&pdev->dev);
	gbeth->of_data = of_data;

	plat_dat->num_clks = of_data->num_clks;
	plat_dat->clks = devm_kcalloc(dev, plat_dat->num_clks,
				      sizeof(*plat_dat->clks), GFP_KERNEL);
	if (!plat_dat->clks)
		return -ENOMEM;

	for (i = 0; i < plat_dat->num_clks; i++)
		plat_dat->clks[i].id = of_data->clks[i];

	err = devm_clk_bulk_get(dev, plat_dat->num_clks, plat_dat->clks);
	if (err < 0)
		return err;

	plat_dat->clk_tx_i = stmmac_pltfr_find_clk(plat_dat, "tx");
	if (!plat_dat->clk_tx_i)
		return dev_err_probe(dev, -EINVAL,
				     "error finding tx clock\n");

	if (of_data->handle_reset) {
		gbeth->rstc = devm_reset_control_get_exclusive(dev, NULL);
		if (IS_ERR(gbeth->rstc))
			return PTR_ERR(gbeth->rstc);
	}

	gbeth->dev = dev;
	gbeth->plat_dat = plat_dat;
	plat_dat->bsp_priv = gbeth;
	if (of_data->set_clk_tx_rate)
		plat_dat->set_clk_tx_rate = stmmac_set_clk_tx_rate;
	plat_dat->init = renesas_gbeth_init;
	plat_dat->exit = renesas_gbeth_exit;
	plat_dat->flags |= STMMAC_FLAG_EN_TX_LPI_CLK_PHY_CAP |
			   gbeth->of_data->stmmac_flags;
	if (of_data->has_pcs) {
		plat_dat->pcs_init = renesas_gmac_pcs_init;
		plat_dat->pcs_exit = renesas_gmac_pcs_exit;
		plat_dat->select_pcs = renesas_gmac_select_pcs;
	}

	return devm_stmmac_pltfr_probe(pdev, plat_dat, &stmmac_res);
}

static const struct renesas_gbeth_of_data renesas_gbeth_of_data = {
	.clks = renesas_gbeth_clks,
	.num_clks = ARRAY_SIZE(renesas_gbeth_clks),
	.handle_reset = true,
	.set_clk_tx_rate = true,
	.stmmac_flags = STMMAC_FLAG_HWTSTAMP_CORRECT_LATENCY |
			STMMAC_FLAG_SPH_DISABLE,
};

static const struct renesas_gbeth_of_data renesas_gmac_of_data = {
	.clks = renesas_gmac_clks,
	.num_clks = ARRAY_SIZE(renesas_gmac_clks),
	.stmmac_flags = STMMAC_FLAG_HWTSTAMP_CORRECT_LATENCY,
	.has_pcs = true,
};

static const struct of_device_id renesas_gbeth_match[] = {
	{ .compatible = "renesas,r9a09g077-gbeth", .data = &renesas_gmac_of_data },
	{ .compatible = "renesas,rzv2h-gbeth", .data = &renesas_gbeth_of_data },
	{ /* Sentinel */ }
};
MODULE_DEVICE_TABLE(of, renesas_gbeth_match);

static struct platform_driver renesas_gbeth_driver = {
	.probe  = renesas_gbeth_probe,
	.driver = {
		.name		= "renesas-gbeth",
		.pm		= &stmmac_pltfr_pm_ops,
		.of_match_table	= renesas_gbeth_match,
	},
};
module_platform_driver(renesas_gbeth_driver);

MODULE_AUTHOR("Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>");
MODULE_DESCRIPTION("Renesas GBETH DWMAC Specific Glue layer");
MODULE_LICENSE("GPL");