aboutsummaryrefslogtreecommitdiff
path: root/drivers/phy/renesas/r8a779f0-ether-serdes.c
blob: bd1fdd3a667d379a7ca9f9d905cb61635a91babb (plain)
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// SPDX-License-Identifier: GPL-2.0
/* Renesas Ethernet SERDES device driver
 *
 * Copyright (C) 2022 Renesas Electronics Corporation
 */

#include <asm/io.h>
#include <clk-uclass.h>
#include <clk.h>
#include <common.h>
#include <div64.h>
#include <dm.h>
#include <dm/device_compat.h>
#include <dm/lists.h>
#include <dm/of_access.h>
#include <generic-phy.h>
#include <linux/bitfield.h>
#include <linux/bitops.h>
#include <linux/delay.h>
#include <linux/iopoll.h>
#include <log.h>
#include <reset.h>
#include <syscon.h>

#define R8A779F0_ETH_SERDES_NUM			3
#define R8A779F0_ETH_SERDES_OFFSET		0x0400
#define R8A779F0_ETH_SERDES_BANK_SELECT		0x03fc
#define R8A779F0_ETH_SERDES_TIMEOUT_US		100000
#define R8A779F0_ETH_SERDES_NUM_RETRY_LINKUP	3

struct r8a779f0_eth_serdes_drv_data;
struct r8a779f0_eth_serdes_channel {
	struct r8a779f0_eth_serdes_drv_data *dd;
	struct phy *phy;
	void __iomem *addr;
	phy_interface_t phy_interface;
	int speed;
	int index;
};

struct r8a779f0_eth_serdes_drv_data {
	void __iomem *addr;
	struct reset_ctl *reset;
	struct r8a779f0_eth_serdes_channel channel[R8A779F0_ETH_SERDES_NUM];
	bool initialized;
};

/*
 * The datasheet describes initialization procedure without any information
 * about registers' name/bits. So, this is all black magic to initialize
 * the hardware.
 */
static void r8a779f0_eth_serdes_write32(void __iomem *addr, u32 offs, u32 bank, u32 data)
{
	writel(bank, addr + R8A779F0_ETH_SERDES_BANK_SELECT);
	writel(data, addr + offs);
}

static int
r8a779f0_eth_serdes_reg_wait(struct r8a779f0_eth_serdes_channel *channel,
			     u32 offs, u32 bank, u32 mask, u32 expected)
{
	u32 val = 0;
	int ret;

	writel(bank, channel->addr + R8A779F0_ETH_SERDES_BANK_SELECT);

	ret = readl_poll_timeout(channel->addr + offs, val,
				 (val & mask) == expected,
				 R8A779F0_ETH_SERDES_TIMEOUT_US);
	if (ret)
		dev_dbg(channel->phy->dev,
			"%s: index %d, offs %x, bank %x, mask %x, expected %x\n",
			 __func__, channel->index, offs, bank, mask, expected);

	return ret;
}

static int
r8a779f0_eth_serdes_common_init_ram(struct r8a779f0_eth_serdes_drv_data *dd)
{
	struct r8a779f0_eth_serdes_channel *channel;
	int i, ret;

	for (i = 0; i < R8A779F0_ETH_SERDES_NUM; i++) {
		channel = &dd->channel[i];
		ret = r8a779f0_eth_serdes_reg_wait(channel, 0x026c, 0x180, BIT(0), 0x01);
		if (ret)
			return ret;
	}

	r8a779f0_eth_serdes_write32(dd->addr, 0x026c, 0x180, 0x03);

	return ret;
}

static int
r8a779f0_eth_serdes_common_setting(struct r8a779f0_eth_serdes_channel *channel)
{
	struct r8a779f0_eth_serdes_drv_data *dd = channel->dd;

	switch (channel->phy_interface) {
	case PHY_INTERFACE_MODE_SGMII:
		r8a779f0_eth_serdes_write32(dd->addr, 0x0244, 0x180, 0x0097);
		r8a779f0_eth_serdes_write32(dd->addr, 0x01d0, 0x180, 0x0060);
		r8a779f0_eth_serdes_write32(dd->addr, 0x01d8, 0x180, 0x2200);
		r8a779f0_eth_serdes_write32(dd->addr, 0x01d4, 0x180, 0x0000);
		r8a779f0_eth_serdes_write32(dd->addr, 0x01e0, 0x180, 0x003d);
		return 0;
	default:
		return -EOPNOTSUPP;
	}
}

static int
r8a779f0_eth_serdes_chan_setting(struct r8a779f0_eth_serdes_channel *channel)
{
	int ret;

	switch (channel->phy_interface) {
	case PHY_INTERFACE_MODE_SGMII:
		r8a779f0_eth_serdes_write32(channel->addr, 0x0000, 0x380, 0x2000);
		r8a779f0_eth_serdes_write32(channel->addr, 0x01c0, 0x180, 0x0011);
		r8a779f0_eth_serdes_write32(channel->addr, 0x0248, 0x180, 0x0540);
		r8a779f0_eth_serdes_write32(channel->addr, 0x0258, 0x180, 0x0015);
		r8a779f0_eth_serdes_write32(channel->addr, 0x0144, 0x180, 0x0100);
		r8a779f0_eth_serdes_write32(channel->addr, 0x01a0, 0x180, 0x0000);
		r8a779f0_eth_serdes_write32(channel->addr, 0x00d0, 0x180, 0x0002);
		r8a779f0_eth_serdes_write32(channel->addr, 0x0150, 0x180, 0x0003);
		r8a779f0_eth_serdes_write32(channel->addr, 0x00c8, 0x180, 0x0100);
		r8a779f0_eth_serdes_write32(channel->addr, 0x0148, 0x180, 0x0100);
		r8a779f0_eth_serdes_write32(channel->addr, 0x0174, 0x180, 0x0000);
		r8a779f0_eth_serdes_write32(channel->addr, 0x0160, 0x180, 0x0007);
		r8a779f0_eth_serdes_write32(channel->addr, 0x01ac, 0x180, 0x0000);
		r8a779f0_eth_serdes_write32(channel->addr, 0x00c4, 0x180, 0x0310);
		r8a779f0_eth_serdes_write32(channel->addr, 0x00c8, 0x180, 0x0101);
		ret = r8a779f0_eth_serdes_reg_wait(channel, 0x00c8, 0x0180, BIT(0), 0);
		if (ret)
			return ret;

		r8a779f0_eth_serdes_write32(channel->addr, 0x0148, 0x180, 0x0101);
		ret = r8a779f0_eth_serdes_reg_wait(channel, 0x0148, 0x0180, BIT(0), 0);
		if (ret)
			return ret;

		r8a779f0_eth_serdes_write32(channel->addr, 0x00c4, 0x180, 0x1310);
		r8a779f0_eth_serdes_write32(channel->addr, 0x00d8, 0x180, 0x1800);
		r8a779f0_eth_serdes_write32(channel->addr, 0x00dc, 0x180, 0x0000);
		r8a779f0_eth_serdes_write32(channel->addr, 0x001c, 0x300, 0x0001);
		r8a779f0_eth_serdes_write32(channel->addr, 0x0000, 0x380, 0x2100);
		ret = r8a779f0_eth_serdes_reg_wait(channel, 0x0000, 0x0380, BIT(8), 0);
		if (ret)
			return ret;

		if (channel->speed == 1000)
			r8a779f0_eth_serdes_write32(channel->addr, 0x0000, 0x1f00, 0x0140);
		else if (channel->speed == 100)
			r8a779f0_eth_serdes_write32(channel->addr, 0x0000, 0x1f00, 0x2100);

		/* For AN_ON */
		r8a779f0_eth_serdes_write32(channel->addr, 0x0004, 0x1f80, 0x0005);
		r8a779f0_eth_serdes_write32(channel->addr, 0x0028, 0x1f80, 0x07a1);
		r8a779f0_eth_serdes_write32(channel->addr, 0x0000, 0x1f80, 0x0208);
		break;
	default:
		return -EOPNOTSUPP;
	}

	return 0;
}

static int
r8a779f0_eth_serdes_chan_speed(struct r8a779f0_eth_serdes_channel *channel)
{
	int ret;

	switch (channel->phy_interface) {
	case PHY_INTERFACE_MODE_SGMII:
		/* For AN_ON */
		if (channel->speed == 1000)
			r8a779f0_eth_serdes_write32(channel->addr, 0x0000, 0x1f00, 0x1140);
		else if (channel->speed == 100)
			r8a779f0_eth_serdes_write32(channel->addr, 0x0000, 0x1f00, 0x3100);
		ret = r8a779f0_eth_serdes_reg_wait(channel, 0x0008, 0x1f80, BIT(0), 1);
		if (ret)
			return ret;
		r8a779f0_eth_serdes_write32(channel->addr, 0x0008, 0x1f80, 0x0000);
		break;
	default:
		return -EOPNOTSUPP;
	}

	return 0;
}

static int r8a779f0_eth_serdes_monitor_linkup(struct r8a779f0_eth_serdes_channel *channel)
{
	int i, ret;

	for (i = 0; i < R8A779F0_ETH_SERDES_NUM_RETRY_LINKUP; i++) {
		ret = r8a779f0_eth_serdes_reg_wait(channel, 0x0004, 0x300,
						   BIT(2), BIT(2));
		if (!ret)
			break;

		/* restart */
		r8a779f0_eth_serdes_write32(channel->addr, 0x0144, 0x180, 0x0100);
		udelay(1);
		r8a779f0_eth_serdes_write32(channel->addr, 0x0144, 0x180, 0x0000);
	}

	return ret;
}

static int r8a779f0_eth_serdes_hw_init(struct r8a779f0_eth_serdes_channel *channel)
{
	struct r8a779f0_eth_serdes_drv_data *dd = channel->dd;
	int i, ret;

	if (dd->initialized)
		return 0;

	ret = r8a779f0_eth_serdes_common_init_ram(dd);
	if (ret)
		return ret;

	for (i = 0; i < R8A779F0_ETH_SERDES_NUM; i++) {
		ret = r8a779f0_eth_serdes_reg_wait(&dd->channel[i], 0x0000,
						   0x300, BIT(15), 0);
		if (ret)
			return ret;
	}

	for (i = 0; i < R8A779F0_ETH_SERDES_NUM; i++)
		r8a779f0_eth_serdes_write32(dd->channel[i].addr, 0x03d4, 0x380, 0x0443);

	ret = r8a779f0_eth_serdes_common_setting(channel);
	if (ret)
		return ret;

	for (i = 0; i < R8A779F0_ETH_SERDES_NUM; i++)
		r8a779f0_eth_serdes_write32(dd->channel[i].addr, 0x03d0, 0x380, 0x0001);

	r8a779f0_eth_serdes_write32(dd->addr, 0x0000, 0x380, 0x8000);

	ret = r8a779f0_eth_serdes_common_init_ram(dd);
	if (ret)
		return ret;

	return r8a779f0_eth_serdes_reg_wait(&dd->channel[0], 0x0000, 0x380, BIT(15), 0);
}

static int r8a779f0_eth_serdes_init(struct phy *p)
{
	struct r8a779f0_eth_serdes_drv_data *dd = dev_get_priv(p->dev);
	struct r8a779f0_eth_serdes_channel *channel = dd->channel + p->id;
	int ret;

	ret = r8a779f0_eth_serdes_hw_init(channel);
	if (!ret)
		channel->dd->initialized = true;

	return ret;
}

static int r8a779f0_eth_serdes_hw_init_late(struct r8a779f0_eth_serdes_channel *channel)
{
	int ret;

	ret = r8a779f0_eth_serdes_chan_setting(channel);
	if (ret)
		return ret;

	ret = r8a779f0_eth_serdes_chan_speed(channel);
	if (ret)
		return ret;

	r8a779f0_eth_serdes_write32(channel->addr, 0x03c0, 0x380, 0x0000);

	r8a779f0_eth_serdes_write32(channel->addr, 0x03d0, 0x380, 0x0000);

	return r8a779f0_eth_serdes_monitor_linkup(channel);
}

static int r8a779f0_eth_serdes_power_on(struct phy *p)
{
	struct r8a779f0_eth_serdes_drv_data *dd = dev_get_priv(p->dev);
	struct r8a779f0_eth_serdes_channel *channel = dd->channel + p->id;

	return r8a779f0_eth_serdes_hw_init_late(channel);
}

static int r8a779f0_eth_serdes_set_mode(struct phy *p, enum phy_mode mode,
					int submode)
{
	struct r8a779f0_eth_serdes_drv_data *dd = dev_get_priv(p->dev);
	struct r8a779f0_eth_serdes_channel *channel = dd->channel + p->id;

	if (mode != PHY_MODE_ETHERNET)
		return -EOPNOTSUPP;

	switch (submode) {
	case PHY_INTERFACE_MODE_GMII:
	case PHY_INTERFACE_MODE_SGMII:
	case PHY_INTERFACE_MODE_USXGMII:
		channel->phy_interface = submode;
		return 0;
	default:
		return -EOPNOTSUPP;
	}
}

static int r8a779f0_eth_serdes_set_speed(struct phy *p, int speed)
{
	struct r8a779f0_eth_serdes_drv_data *dd = dev_get_priv(p->dev);
	struct r8a779f0_eth_serdes_channel *channel = dd->channel + p->id;

	channel->speed = speed;

	return 0;
}

static int r8a779f0_eth_serdes_of_xlate(struct phy *phy,
					struct ofnode_phandle_args *args)
{
	if (args->args_count < 1)
		return -ENODEV;

	if (args->args[0] >= R8A779F0_ETH_SERDES_NUM)
		return -ENODEV;

	phy->id = args->args[0];

	return 0;
}

static const struct phy_ops r8a779f0_eth_serdes_ops = {
	.init		= r8a779f0_eth_serdes_init,
	.power_on	= r8a779f0_eth_serdes_power_on,
	.set_mode	= r8a779f0_eth_serdes_set_mode,
	.set_speed	= r8a779f0_eth_serdes_set_speed,
	.of_xlate	= r8a779f0_eth_serdes_of_xlate,
};

static const struct udevice_id r8a779f0_eth_serdes_of_table[] = {
	{ .compatible = "renesas,r8a779f0-ether-serdes", },
	{ }
};

static int r8a779f0_eth_serdes_probe(struct udevice *dev)
{
	struct r8a779f0_eth_serdes_drv_data *dd = dev_get_priv(dev);
	int i;

	dd->addr = dev_read_addr_ptr(dev);
	if (!dd->addr)
		return -EINVAL;

	dd->reset = devm_reset_control_get(dev, NULL);
	if (IS_ERR(dd->reset))
		return PTR_ERR(dd->reset);

	reset_assert(dd->reset);
	reset_deassert(dd->reset);

	for (i = 0; i < R8A779F0_ETH_SERDES_NUM; i++) {
		struct r8a779f0_eth_serdes_channel *channel = &dd->channel[i];

		channel->addr = dd->addr + R8A779F0_ETH_SERDES_OFFSET * i;
		channel->dd = dd;
		channel->index = i;
	}

	return 0;
}

U_BOOT_DRIVER(r8a779f0_eth_serdes_driver_platform) = {
	.name		= "r8a779f0_eth_serdes",
	.id		= UCLASS_PHY,
	.of_match	= r8a779f0_eth_serdes_of_table,
	.probe		= r8a779f0_eth_serdes_probe,
	.ops		= &r8a779f0_eth_serdes_ops,
	.priv_auto	= sizeof(struct r8a779f0_eth_serdes_drv_data),
};