summaryrefslogtreecommitdiff
path: root/drivers/net/phy/b53.c
blob: 185a2cddaba661eb30fe769d6cf85b286f1bdbc7 (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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/*
 * Copyright (C) 2017
 * Broadcom
 * Florian Fainelli <f.fainelli@gmail.com>
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

/*
 * PHY driver for Broadcom BCM53xx (roboswitch) Ethernet switches.
 *
 * This driver configures the b53 for basic use as a PHY. The switch supports
 * vendor tags and VLAN configuration that can affect the switching decisions.
 * This driver uses a simple configuration in which all ports are only allowed
 * to send frames to the CPU port and receive frames from the CPU port this
 * providing port isolation (no cross talk).
 *
 * The configuration determines which PHY ports to activate using the
 * CONFIG_B53_PHY_PORTS bitmask. Set bit N will active port N and so on.
 *
 * This driver was written primarily for the Lamobo R1 platform using a BCM53152
 * switch but the BCM53xx being largely register compatible, extending it to
 * cover other switches would be trivial.
 */

#include <common.h>

#include <errno.h>
#include <malloc.h>
#include <miiphy.h>
#include <netdev.h>

/* Pseudo-PHY address (non configurable) to access internal registers */
#define BRCM_PSEUDO_PHY_ADDR		30

/* Maximum number of ports possible */
#define B53_N_PORTS			9

#define B53_CTRL_PAGE			0x00 /* Control */
#define B53_MGMT_PAGE			0x02 /* Management Mode */
/* Port VLAN Page */
#define B53_PVLAN_PAGE			0x31

/* Control Page registers */
#define B53_PORT_CTRL(i)		(0x00 + (i))
#define   PORT_CTRL_RX_DISABLE		BIT(0)
#define   PORT_CTRL_TX_DISABLE		BIT(1)
#define   PORT_CTRL_RX_BCST_EN		BIT(2) /* Broadcast RX (P8 only) */
#define   PORT_CTRL_RX_MCST_EN		BIT(3) /* Multicast RX (P8 only) */
#define   PORT_CTRL_RX_UCST_EN		BIT(4) /* Unicast RX (P8 only) */

/* Switch Mode Control Register (8 bit) */
#define B53_SWITCH_MODE			0x0b
#define   SM_SW_FWD_MODE		BIT(0)	/* 1 = Managed Mode */
#define   SM_SW_FWD_EN			BIT(1)	/* Forwarding Enable */

/* IMP Port state override register (8 bit) */
#define B53_PORT_OVERRIDE_CTRL		0x0e
#define   PORT_OVERRIDE_LINK		BIT(0)
#define   PORT_OVERRIDE_FULL_DUPLEX	BIT(1) /* 0 = Half Duplex */
#define   PORT_OVERRIDE_SPEED_S		2
#define   PORT_OVERRIDE_SPEED_10M	(0 << PORT_OVERRIDE_SPEED_S)
#define   PORT_OVERRIDE_SPEED_100M	(1 << PORT_OVERRIDE_SPEED_S)
#define   PORT_OVERRIDE_SPEED_1000M	(2 << PORT_OVERRIDE_SPEED_S)
/* BCM5325 only */
#define   PORT_OVERRIDE_RV_MII_25	BIT(4)
#define   PORT_OVERRIDE_RX_FLOW		BIT(4)
#define   PORT_OVERRIDE_TX_FLOW		BIT(5)
/* BCM5301X only, requires setting 1000M */
#define   PORT_OVERRIDE_SPEED_2000M	BIT(6)
#define   PORT_OVERRIDE_EN		BIT(7) /* Use the register contents */

#define B53_RGMII_CTRL_IMP		0x60
#define   RGMII_CTRL_ENABLE_GMII	BIT(7)
#define   RGMII_CTRL_TIMING_SEL		BIT(2)
#define   RGMII_CTRL_DLL_RXC		BIT(1)
#define   RGMII_CTRL_DLL_TXC		BIT(0)

/* Switch control (8 bit) */
#define B53_SWITCH_CTRL			0x22
#define  B53_MII_DUMB_FWDG_EN		BIT(6)

/* Software reset register (8 bit) */
#define B53_SOFTRESET			0x79
#define   SW_RST			BIT(7)
#define   EN_CH_RST			BIT(6)
#define   EN_SW_RST			BIT(4)

/* Fast Aging Control register (8 bit) */
#define B53_FAST_AGE_CTRL		0x88
#define   FAST_AGE_STATIC		BIT(0)
#define   FAST_AGE_DYNAMIC		BIT(1)
#define   FAST_AGE_PORT			BIT(2)
#define   FAST_AGE_VLAN			BIT(3)
#define   FAST_AGE_STP			BIT(4)
#define   FAST_AGE_MC			BIT(5)
#define   FAST_AGE_DONE			BIT(7)

/* Port VLAN mask (16 bit) IMP port is always 8, also on 5325 & co */
#define B53_PVLAN_PORT_MASK(i)		((i) * 2)

/* MII registers */
#define REG_MII_PAGE    0x10    /* MII Page register */
#define REG_MII_ADDR    0x11    /* MII Address register */
#define REG_MII_DATA0   0x18    /* MII Data register 0 */
#define REG_MII_DATA1   0x19    /* MII Data register 1 */
#define REG_MII_DATA2   0x1a    /* MII Data register 2 */
#define REG_MII_DATA3   0x1b    /* MII Data register 3 */

#define REG_MII_PAGE_ENABLE     BIT(0)
#define REG_MII_ADDR_WRITE      BIT(0)
#define REG_MII_ADDR_READ       BIT(1)

struct b53_device {
	struct mii_dev	*bus;
	unsigned int cpu_port;
};

static int b53_mdio_op(struct mii_dev *bus, u8 page, u8 reg, u16 op)
{
	int ret;
	int i;
	u16 v;

	/* set page number */
	v = (page << 8) | REG_MII_PAGE_ENABLE;
	ret = bus->write(bus, BRCM_PSEUDO_PHY_ADDR, MDIO_DEVAD_NONE,
			 REG_MII_PAGE, v);
	if (ret)
		return ret;

	/* set register address */
	v = (reg << 8) | op;
	ret = bus->write(bus, BRCM_PSEUDO_PHY_ADDR, MDIO_DEVAD_NONE,
			 REG_MII_ADDR, v);
	if (ret)
		return ret;

	/* check if operation completed */
	for (i = 0; i < 5; ++i) {
		v = bus->read(bus, BRCM_PSEUDO_PHY_ADDR, MDIO_DEVAD_NONE,
			      REG_MII_ADDR);
		if (!(v & (REG_MII_ADDR_WRITE | REG_MII_ADDR_READ)))
			break;

		udelay(100);
	}

	if (i == 5)
		return -EIO;

	return 0;
}

static int b53_mdio_read8(struct mii_dev *bus, u8 page, u8 reg, u8 *val)
{
	int ret;

	ret = b53_mdio_op(bus, page, reg, REG_MII_ADDR_READ);
	if (ret)
		return ret;

	*val = bus->read(bus, BRCM_PSEUDO_PHY_ADDR, MDIO_DEVAD_NONE,
			 REG_MII_DATA0) & 0xff;

	return 0;
}

static int b53_mdio_read16(struct mii_dev *bus, u8 page, u8 reg, u16 *val)
{
	int ret;

	ret = b53_mdio_op(bus, page, reg, REG_MII_ADDR_READ);
	if (ret)
		return ret;

	*val = bus->read(bus, BRCM_PSEUDO_PHY_ADDR, MDIO_DEVAD_NONE,
			 REG_MII_DATA0);

	return 0;
}

static int b53_mdio_read32(struct mii_dev *bus, u8 page, u8 reg, u32 *val)
{
	int ret;

	ret = b53_mdio_op(bus, page, reg, REG_MII_ADDR_READ);
	if (ret)
		return ret;

	*val = bus->read(bus, BRCM_PSEUDO_PHY_ADDR, MDIO_DEVAD_NONE,
			 REG_MII_DATA0);
	*val |= bus->read(bus, BRCM_PSEUDO_PHY_ADDR, MDIO_DEVAD_NONE,
			  REG_MII_DATA1) << 16;

	return 0;
}

static int b53_mdio_read48(struct mii_dev *bus, u8 page, u8 reg, u64 *val)
{
	u64 temp = 0;
	int i;
	int ret;

	ret = b53_mdio_op(bus, page, reg, REG_MII_ADDR_READ);
	if (ret)
		return ret;

	for (i = 2; i >= 0; i--) {
		temp <<= 16;
		temp |= bus->read(bus, BRCM_PSEUDO_PHY_ADDR, MDIO_DEVAD_NONE,
				  REG_MII_DATA0 + i);
	}

	*val = temp;

	return 0;
}

static int b53_mdio_read64(struct mii_dev *bus, u8 page, u8 reg, u64 *val)
{
	u64 temp = 0;
	int i;
	int ret;

	ret = b53_mdio_op(bus, page, reg, REG_MII_ADDR_READ);
	if (ret)
		return ret;

	for (i = 3; i >= 0; i--) {
		temp <<= 16;
		temp |= bus->read(bus, BRCM_PSEUDO_PHY_ADDR, MDIO_DEVAD_NONE,
				  REG_MII_DATA0 + i);
	}

	*val = temp;

	return 0;
}

static int b53_mdio_write8(struct mii_dev *bus, u8 page, u8 reg, u8 value)
{
	int ret;

	ret = bus->write(bus, BRCM_PSEUDO_PHY_ADDR, MDIO_DEVAD_NONE,
			 REG_MII_DATA0, value);
	if (ret)
		return ret;

	return b53_mdio_op(bus, page, reg, REG_MII_ADDR_WRITE);
}

static int b53_mdio_write16(struct mii_dev *bus, u8 page, u8 reg,
			    u16 value)
{
	int ret;

	ret = bus->write(bus, BRCM_PSEUDO_PHY_ADDR, MDIO_DEVAD_NONE,
			 REG_MII_DATA0, value);
	if (ret)
		return ret;

	return b53_mdio_op(bus, page, reg, REG_MII_ADDR_WRITE);
}

static int b53_mdio_write32(struct mii_dev *bus, u8 page, u8 reg,
			    u32 value)
{
	unsigned int i;
	u32 temp = value;

	for (i = 0; i < 2; i++) {
		int ret = bus->write(bus, BRCM_PSEUDO_PHY_ADDR,
				     MDIO_DEVAD_NONE,
				     REG_MII_DATA0 + i, temp & 0xffff);
		if (ret)
			return ret;
		temp >>= 16;
	}

	return b53_mdio_op(bus, page, reg, REG_MII_ADDR_WRITE);
}

static int b53_mdio_write48(struct mii_dev *bus, u8 page, u8 reg,
			    u64 value)
{
	unsigned int i;
	u64 temp = value;

	for (i = 0; i < 3; i++) {
		int ret = bus->write(bus, BRCM_PSEUDO_PHY_ADDR,
				     MDIO_DEVAD_NONE,
				     REG_MII_DATA0 + i, temp & 0xffff);
		if (ret)
			return ret;
		temp >>= 16;
	}

	return b53_mdio_op(bus, page, reg, REG_MII_ADDR_WRITE);
}

static int b53_mdio_write64(struct mii_dev *bus, u8 page, u8 reg,
			    u64 value)
{
	unsigned int i;
	u64 temp = value;

	for (i = 0; i < 4; i++) {
		int ret = bus->write(bus, BRCM_PSEUDO_PHY_ADDR,
				     MDIO_DEVAD_NONE,
				     REG_MII_DATA0 + i, temp & 0xffff);
		if (ret)
			return ret;
		temp >>= 16;
	}

	return b53_mdio_op(bus, page, reg, REG_MII_ADDR_WRITE);
}

static inline int b53_read8(struct b53_device *dev, u8 page,
			    u8 reg, u8 *value)
{
	return b53_mdio_read8(dev->bus, page, reg, value);
}

static inline int b53_read16(struct b53_device *dev, u8 page,
			     u8 reg, u16 *value)
{
	return b53_mdio_read16(dev->bus, page, reg, value);
}

static inline int b53_read32(struct b53_device *dev, u8 page,
			     u8 reg, u32 *value)
{
	return b53_mdio_read32(dev->bus, page, reg, value);
}

static inline int b53_read48(struct b53_device *dev, u8 page,
			     u8 reg, u64 *value)
{
	return b53_mdio_read48(dev->bus, page, reg, value);
}

static inline int b53_read64(struct b53_device *dev, u8 page,
			     u8 reg, u64 *value)
{
	return b53_mdio_read64(dev->bus, page, reg, value);
}

static inline int b53_write8(struct b53_device *dev, u8 page,
			     u8 reg, u8 value)
{
	return b53_mdio_write8(dev->bus, page, reg, value);
}

static inline int b53_write16(struct b53_device *dev, u8 page,
			      u8 reg, u16 value)
{
	return b53_mdio_write16(dev->bus, page, reg, value);
}

static inline int b53_write32(struct b53_device *dev, u8 page,
			      u8 reg, u32 value)
{
	return b53_mdio_write32(dev->bus, page, reg, value);
}

static inline int b53_write48(struct b53_device *dev, u8 page,
			      u8 reg, u64 value)
{
	return b53_mdio_write48(dev->bus, page, reg, value);
}

static inline int b53_write64(struct b53_device *dev, u8 page,
			      u8 reg, u64 value)
{
	return b53_mdio_write64(dev->bus, page, reg, value);
}

static int b53_flush_arl(struct b53_device *dev, u8 mask)
{
	unsigned int i;

	b53_write8(dev, B53_CTRL_PAGE, B53_FAST_AGE_CTRL,
		   FAST_AGE_DONE | FAST_AGE_DYNAMIC | mask);

	for (i = 0; i < 10; i++) {
		u8 fast_age_ctrl;

		b53_read8(dev, B53_CTRL_PAGE, B53_FAST_AGE_CTRL,
			  &fast_age_ctrl);

		if (!(fast_age_ctrl & FAST_AGE_DONE))
			goto out;

		mdelay(1);
	}

	return -ETIMEDOUT;
out:
	/* Only age dynamic entries (default behavior) */
	b53_write8(dev, B53_CTRL_PAGE, B53_FAST_AGE_CTRL, FAST_AGE_DYNAMIC);
	return 0;
}

static int b53_switch_reset(struct phy_device *phydev)
{
	struct b53_device *dev = phydev->priv;
	unsigned int timeout = 1000;
	u8 mgmt;
	u8 reg;

	b53_read8(dev, B53_CTRL_PAGE, B53_SOFTRESET, &reg);
	reg |= SW_RST | EN_SW_RST | EN_CH_RST;
	b53_write8(dev, B53_CTRL_PAGE, B53_SOFTRESET, reg);

	do {
		b53_read8(dev, B53_CTRL_PAGE, B53_SOFTRESET, &reg);
		if (!(reg & SW_RST))
			break;

		mdelay(1);
	} while (timeout-- > 0);

	if (timeout == 0)
		return -ETIMEDOUT;

	b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);

	if (!(mgmt & SM_SW_FWD_EN)) {
		mgmt &= ~SM_SW_FWD_MODE;
		mgmt |= SM_SW_FWD_EN;

		b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, mgmt);
		b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);

		if (!(mgmt & SM_SW_FWD_EN)) {
			printf("Failed to enable switch!\n");
			return -EINVAL;
		}
	}

	/* Include IMP port in dumb forwarding mode when no tagging protocol
	 * is configured
	 */
	b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_CTRL, &mgmt);
	mgmt |= B53_MII_DUMB_FWDG_EN;
	b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_CTRL, mgmt);

	return b53_flush_arl(dev, FAST_AGE_STATIC);
}

static void b53_enable_cpu_port(struct phy_device *phydev)
{
	struct b53_device *dev = phydev->priv;
	u8 port_ctrl;

	port_ctrl = PORT_CTRL_RX_BCST_EN |
		    PORT_CTRL_RX_MCST_EN |
		    PORT_CTRL_RX_UCST_EN;
	b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(dev->cpu_port), port_ctrl);

	port_ctrl = PORT_OVERRIDE_EN | PORT_OVERRIDE_LINK |
		    PORT_OVERRIDE_FULL_DUPLEX | PORT_OVERRIDE_SPEED_1000M;
	b53_write8(dev, B53_CTRL_PAGE, B53_PORT_OVERRIDE_CTRL, port_ctrl);

	b53_read8(dev, B53_CTRL_PAGE, B53_RGMII_CTRL_IMP, &port_ctrl);
}

static void b53_imp_vlan_setup(struct b53_device *dev, int cpu_port)
{
	unsigned int port;
	u16 pvlan;

	/* Enable the IMP port to be in the same VLAN as the other ports
	 * on a per-port basis such that we only have Port i and IMP in
	 * the same VLAN.
	 */
	for (port = 0; port < B53_N_PORTS; port++) {
		if (!((1 << port) & CONFIG_B53_PHY_PORTS))
			continue;

		b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port),
			   &pvlan);
		pvlan |= BIT(cpu_port);
		b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port),
			    pvlan);
	}
}

static int b53_port_enable(struct phy_device *phydev, unsigned int port)
{
	struct b53_device *dev = phydev->priv;
	unsigned int cpu_port = dev->cpu_port;
	u16 pvlan;

	/* Clear the Rx and Tx disable bits and set to no spanning tree */
	b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), 0);

	/* Set this port, and only this one to be in the default VLAN */
	b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), &pvlan);
	pvlan &= ~0x1ff;
	pvlan |= BIT(port);
	b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), pvlan);

	b53_imp_vlan_setup(dev, cpu_port);

	return 0;
}

static int b53_switch_init(struct phy_device *phydev)
{
	static int init;
	int ret;

	if (init)
		return 0;

	ret = b53_switch_reset(phydev);
	if (ret < 0)
		return ret;

	b53_enable_cpu_port(phydev);

	init = 1;

	return 0;
}

static int b53_probe(struct phy_device *phydev)
{
	struct b53_device *dev;
	int ret;

	dev = malloc(sizeof(*dev));
	if (!dev)
		return -ENOMEM;

	memset(dev, 0, sizeof(*dev));

	phydev->priv = dev;
	dev->bus = phydev->bus;
	dev->cpu_port = CONFIG_B53_CPU_PORT;

	ret = b53_switch_reset(phydev);
	if (ret < 0)
		return ret;

	return 0;
}

static int b53_phy_config(struct phy_device *phydev)
{
	unsigned int port;
	int res;

	res = b53_switch_init(phydev);
	if (res < 0)
		return res;

	for (port = 0; port < B53_N_PORTS; port++) {
		if (!((1 << port) & CONFIG_B53_PHY_PORTS))
			continue;

		res = b53_port_enable(phydev, port);
		if (res < 0) {
			printf("Error enabling port %i\n", port);
			continue;
		}

		res = genphy_config_aneg(phydev);
		if (res < 0) {
			printf("Error setting PHY %i autoneg\n", port);
			continue;
		}

		res = 0;
	}

	return res;
}

static int b53_phy_startup(struct phy_device *phydev)
{
	unsigned int port;
	int res;

	for (port = 0; port < B53_N_PORTS; port++) {
		if (!((1 << port) & CONFIG_B53_PHY_PORTS))
			continue;

		phydev->addr = port;

		res = genphy_startup(phydev);
		if (res < 0)
			continue;
		else
			break;
	}

	/* Since we are connected directly to the switch, hardcode the link
	 * parameters to match those of the CPU port configured in
	 * b53_enable_cpu_port, we cannot be dependent on the user-facing port
	 * settings (e.g: 100Mbits/sec would not work here)
	 */
	phydev->speed = 1000;
	phydev->duplex = 1;
	phydev->link = 1;

	return 0;
}

static struct phy_driver b53_driver = {
	.name = "Broadcom BCM53125",
	.uid = 0x03625c00,
	.mask = 0xfffffc00,
	.features = PHY_GBIT_FEATURES,
	.probe = b53_probe,
	.config = b53_phy_config,
	.startup = b53_phy_startup,
	.shutdown = &genphy_shutdown,
};

int phy_b53_init(void)
{
	phy_register(&b53_driver);

	return 0;
}