port.c 22.7 KB
Newer Older
1 2 3 4 5
/*
 * Marvell 88E6xxx Switch Port Registers support
 *
 * Copyright (c) 2008 Marvell Semiconductor
 *
V
Vivien Didelot 已提交
6 7
 * Copyright (c) 2016-2017 Savoir-faire Linux Inc.
 *	Vivien Didelot <vivien.didelot@savoirfairelinux.com>
8 9 10 11 12 13 14
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

15
#include <linux/bitfield.h>
16
#include <linux/if_bridge.h>
17
#include <linux/phy.h>
18 19

#include "chip.h"
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#include "port.h"

int mv88e6xxx_port_read(struct mv88e6xxx_chip *chip, int port, int reg,
			u16 *val)
{
	int addr = chip->info->port_base_addr + port;

	return mv88e6xxx_read(chip, addr, reg, val);
}

int mv88e6xxx_port_write(struct mv88e6xxx_chip *chip, int port, int reg,
			 u16 val)
{
	int addr = chip->info->port_base_addr + port;

	return mv88e6xxx_write(chip, addr, reg, val);
}
37

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
/* Offset 0x00: Port Status Register */

int mv88e6xxx_port_status_eee(struct mv88e6xxx_chip *chip, int port,
			      struct ethtool_eee *eee)
{
	u16 val;
	int err;

	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_STS, &val);
	if (err)
		return err;

	eee->eee_active = !!(val & MV88E6352_PORT_STS_EEE);

	return 0;
}

55 56 57
/* Offset 0x01: MAC (or PCS or Physical) Control Register
 *
 * Link, Duplex and Flow Control have one force bit, one value bit.
58 59 60 61
 *
 * For port's MAC speed, ForceSpd (or SpdValue) bits 1:0 program the value.
 * Alternative values require the 200BASE (or AltSpeed) bit 12 set.
 * Newer chips need a ForcedSpd bit 13 set to consider the value.
62 63
 */

64 65 66 67 68 69
static int mv88e6xxx_port_set_rgmii_delay(struct mv88e6xxx_chip *chip, int port,
					  phy_interface_t mode)
{
	u16 reg;
	int err;

70
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_MAC_CTL, &reg);
71 72 73
	if (err)
		return err;

74 75
	reg &= ~(MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_RXCLK |
		 MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_TXCLK);
76 77 78

	switch (mode) {
	case PHY_INTERFACE_MODE_RGMII_RXID:
79
		reg |= MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_RXCLK;
80 81
		break;
	case PHY_INTERFACE_MODE_RGMII_TXID:
82
		reg |= MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_TXCLK;
83 84
		break;
	case PHY_INTERFACE_MODE_RGMII_ID:
85 86
		reg |= MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_RXCLK |
			MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_TXCLK;
87
		break;
88
	case PHY_INTERFACE_MODE_RGMII:
89
		break;
90 91
	default:
		return 0;
92 93
	}

94
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_MAC_CTL, reg);
95 96 97
	if (err)
		return err;

98
	dev_dbg(chip->dev, "p%d: delay RXCLK %s, TXCLK %s\n", port,
99 100
		reg & MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_RXCLK ? "yes" : "no",
		reg & MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_TXCLK ? "yes" : "no");
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

	return 0;
}

int mv88e6352_port_set_rgmii_delay(struct mv88e6xxx_chip *chip, int port,
				   phy_interface_t mode)
{
	if (port < 5)
		return -EOPNOTSUPP;

	return mv88e6xxx_port_set_rgmii_delay(chip, port, mode);
}

int mv88e6390_port_set_rgmii_delay(struct mv88e6xxx_chip *chip, int port,
				   phy_interface_t mode)
{
	if (port != 0)
		return -EOPNOTSUPP;

	return mv88e6xxx_port_set_rgmii_delay(chip, port, mode);
}

123 124 125 126 127
int mv88e6xxx_port_set_link(struct mv88e6xxx_chip *chip, int port, int link)
{
	u16 reg;
	int err;

128
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_MAC_CTL, &reg);
129 130 131
	if (err)
		return err;

132 133
	reg &= ~(MV88E6XXX_PORT_MAC_CTL_FORCE_LINK |
		 MV88E6XXX_PORT_MAC_CTL_LINK_UP);
134 135 136

	switch (link) {
	case LINK_FORCED_DOWN:
137
		reg |= MV88E6XXX_PORT_MAC_CTL_FORCE_LINK;
138 139
		break;
	case LINK_FORCED_UP:
140 141
		reg |= MV88E6XXX_PORT_MAC_CTL_FORCE_LINK |
			MV88E6XXX_PORT_MAC_CTL_LINK_UP;
142 143 144 145 146 147 148 149
		break;
	case LINK_UNFORCED:
		/* normal link detection */
		break;
	default:
		return -EINVAL;
	}

150
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_MAC_CTL, reg);
151 152 153
	if (err)
		return err;

154
	dev_dbg(chip->dev, "p%d: %s link %s\n", port,
155 156
		reg & MV88E6XXX_PORT_MAC_CTL_FORCE_LINK ? "Force" : "Unforce",
		reg & MV88E6XXX_PORT_MAC_CTL_LINK_UP ? "up" : "down");
157 158 159 160

	return 0;
}

161 162 163 164 165
int mv88e6xxx_port_set_duplex(struct mv88e6xxx_chip *chip, int port, int dup)
{
	u16 reg;
	int err;

166
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_MAC_CTL, &reg);
167 168 169
	if (err)
		return err;

170 171
	reg &= ~(MV88E6XXX_PORT_MAC_CTL_FORCE_DUPLEX |
		 MV88E6XXX_PORT_MAC_CTL_DUPLEX_FULL);
172 173 174

	switch (dup) {
	case DUPLEX_HALF:
175
		reg |= MV88E6XXX_PORT_MAC_CTL_FORCE_DUPLEX;
176 177
		break;
	case DUPLEX_FULL:
178 179
		reg |= MV88E6XXX_PORT_MAC_CTL_FORCE_DUPLEX |
			MV88E6XXX_PORT_MAC_CTL_DUPLEX_FULL;
180 181 182 183 184 185 186 187
		break;
	case DUPLEX_UNFORCED:
		/* normal duplex detection */
		break;
	default:
		return -EINVAL;
	}

188
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_MAC_CTL, reg);
189 190 191
	if (err)
		return err;

192
	dev_dbg(chip->dev, "p%d: %s %s duplex\n", port,
193 194
		reg & MV88E6XXX_PORT_MAC_CTL_FORCE_DUPLEX ? "Force" : "Unforce",
		reg & MV88E6XXX_PORT_MAC_CTL_DUPLEX_FULL ? "full" : "half");
195 196 197 198

	return 0;
}

199 200 201 202 203 204 205 206
static int mv88e6xxx_port_set_speed(struct mv88e6xxx_chip *chip, int port,
				    int speed, bool alt_bit, bool force_bit)
{
	u16 reg, ctrl;
	int err;

	switch (speed) {
	case 10:
207
		ctrl = MV88E6XXX_PORT_MAC_CTL_SPEED_10;
208 209
		break;
	case 100:
210
		ctrl = MV88E6XXX_PORT_MAC_CTL_SPEED_100;
211 212 213
		break;
	case 200:
		if (alt_bit)
214 215
			ctrl = MV88E6XXX_PORT_MAC_CTL_SPEED_100 |
				MV88E6390_PORT_MAC_CTL_ALTSPEED;
216
		else
217
			ctrl = MV88E6065_PORT_MAC_CTL_SPEED_200;
218 219
		break;
	case 1000:
220
		ctrl = MV88E6XXX_PORT_MAC_CTL_SPEED_1000;
221 222
		break;
	case 2500:
223 224
		ctrl = MV88E6390_PORT_MAC_CTL_SPEED_10000 |
			MV88E6390_PORT_MAC_CTL_ALTSPEED;
225 226 227 228
		break;
	case 10000:
		/* all bits set, fall through... */
	case SPEED_UNFORCED:
229
		ctrl = MV88E6XXX_PORT_MAC_CTL_SPEED_UNFORCED;
230 231 232 233 234
		break;
	default:
		return -EOPNOTSUPP;
	}

235
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_MAC_CTL, &reg);
236 237 238
	if (err)
		return err;

239
	reg &= ~MV88E6XXX_PORT_MAC_CTL_SPEED_MASK;
240
	if (alt_bit)
241
		reg &= ~MV88E6390_PORT_MAC_CTL_ALTSPEED;
242
	if (force_bit) {
243
		reg &= ~MV88E6390_PORT_MAC_CTL_FORCE_SPEED;
244
		if (speed != SPEED_UNFORCED)
245
			ctrl |= MV88E6390_PORT_MAC_CTL_FORCE_SPEED;
246 247 248
	}
	reg |= ctrl;

249
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_MAC_CTL, reg);
250 251 252 253
	if (err)
		return err;

	if (speed)
254
		dev_dbg(chip->dev, "p%d: Speed set to %d Mbps\n", port, speed);
255
	else
256
		dev_dbg(chip->dev, "p%d: Speed unforced\n", port);
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

	return 0;
}

/* Support 10, 100, 200 Mbps (e.g. 88E6065 family) */
int mv88e6065_port_set_speed(struct mv88e6xxx_chip *chip, int port, int speed)
{
	if (speed == SPEED_MAX)
		speed = 200;

	if (speed > 200)
		return -EOPNOTSUPP;

	/* Setting 200 Mbps on port 0 to 3 selects 100 Mbps */
	return mv88e6xxx_port_set_speed(chip, port, speed, false, false);
}

/* Support 10, 100, 1000 Mbps (e.g. 88E6185 family) */
int mv88e6185_port_set_speed(struct mv88e6xxx_chip *chip, int port, int speed)
{
	if (speed == SPEED_MAX)
		speed = 1000;

	if (speed == 200 || speed > 1000)
		return -EOPNOTSUPP;

	return mv88e6xxx_port_set_speed(chip, port, speed, false, false);
}

/* Support 10, 100, 200, 1000 Mbps (e.g. 88E6352 family) */
int mv88e6352_port_set_speed(struct mv88e6xxx_chip *chip, int port, int speed)
{
	if (speed == SPEED_MAX)
		speed = 1000;

	if (speed > 1000)
		return -EOPNOTSUPP;

	if (speed == 200 && port < 5)
		return -EOPNOTSUPP;

	return mv88e6xxx_port_set_speed(chip, port, speed, true, false);
}

/* Support 10, 100, 200, 1000, 2500 Mbps (e.g. 88E6390) */
int mv88e6390_port_set_speed(struct mv88e6xxx_chip *chip, int port, int speed)
{
	if (speed == SPEED_MAX)
		speed = port < 9 ? 1000 : 2500;

	if (speed > 2500)
		return -EOPNOTSUPP;

	if (speed == 200 && port != 0)
		return -EOPNOTSUPP;

	if (speed == 2500 && port < 9)
		return -EOPNOTSUPP;

	return mv88e6xxx_port_set_speed(chip, port, speed, true, true);
}

/* Support 10, 100, 200, 1000, 2500, 10000 Mbps (e.g. 88E6190X) */
int mv88e6390x_port_set_speed(struct mv88e6xxx_chip *chip, int port, int speed)
{
	if (speed == SPEED_MAX)
		speed = port < 9 ? 1000 : 10000;

	if (speed == 200 && port != 0)
		return -EOPNOTSUPP;

	if (speed >= 2500 && port < 9)
		return -EOPNOTSUPP;

	return mv88e6xxx_port_set_speed(chip, port, speed, true, true);
}

334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
int mv88e6390x_port_set_cmode(struct mv88e6xxx_chip *chip, int port,
			      phy_interface_t mode)
{
	u16 reg;
	u16 cmode;
	int err;

	if (mode == PHY_INTERFACE_MODE_NA)
		return 0;

	if (port != 9 && port != 10)
		return -EOPNOTSUPP;

	switch (mode) {
	case PHY_INTERFACE_MODE_1000BASEX:
349
		cmode = MV88E6XXX_PORT_STS_CMODE_1000BASE_X;
350 351
		break;
	case PHY_INTERFACE_MODE_SGMII:
352
		cmode = MV88E6XXX_PORT_STS_CMODE_SGMII;
353 354
		break;
	case PHY_INTERFACE_MODE_2500BASEX:
355
		cmode = MV88E6XXX_PORT_STS_CMODE_2500BASEX;
356 357
		break;
	case PHY_INTERFACE_MODE_XGMII:
358
		cmode = MV88E6XXX_PORT_STS_CMODE_XAUI;
359 360
		break;
	case PHY_INTERFACE_MODE_RXAUI:
361
		cmode = MV88E6XXX_PORT_STS_CMODE_RXAUI;
362 363 364 365 366 367
		break;
	default:
		cmode = 0;
	}

	if (cmode) {
368
		err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_STS, &reg);
369 370 371
		if (err)
			return err;

372
		reg &= ~MV88E6XXX_PORT_STS_CMODE_MASK;
373 374
		reg |= cmode;

375
		err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_STS, reg);
376 377 378 379 380 381 382 383 384 385 386 387
		if (err)
			return err;
	}

	return 0;
}

int mv88e6xxx_port_get_cmode(struct mv88e6xxx_chip *chip, int port, u8 *cmode)
{
	int err;
	u16 reg;

388
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_STS, &reg);
389 390 391
	if (err)
		return err;

392
	*cmode = reg & MV88E6XXX_PORT_STS_CMODE_MASK;
393 394 395 396

	return 0;
}

397
/* Offset 0x02: Jamming Control
398 399 400 401 402
 *
 * Do not limit the period of time that this port can be paused for by
 * the remote end or the period of time that this port can pause the
 * remote end.
 */
403 404
int mv88e6097_port_pause_limit(struct mv88e6xxx_chip *chip, int port, u8 in,
			       u8 out)
405
{
406 407
	return mv88e6xxx_port_write(chip, port, MV88E6097_PORT_JAM_CTL,
				    out << 8 | in);
408 409
}

410 411
int mv88e6390_port_pause_limit(struct mv88e6xxx_chip *chip, int port, u8 in,
			       u8 out)
412 413 414
{
	int err;

415 416 417
	err = mv88e6xxx_port_write(chip, port, MV88E6390_PORT_FLOW_CTL,
				   MV88E6390_PORT_FLOW_CTL_UPDATE |
				   MV88E6390_PORT_FLOW_CTL_LIMIT_IN | in);
418 419 420
	if (err)
		return err;

421 422 423
	return mv88e6xxx_port_write(chip, port, MV88E6390_PORT_FLOW_CTL,
				    MV88E6390_PORT_FLOW_CTL_UPDATE |
				    MV88E6390_PORT_FLOW_CTL_LIMIT_OUT | out);
424 425
}

426 427 428
/* Offset 0x04: Port Control Register */

static const char * const mv88e6xxx_port_state_names[] = {
429 430 431 432
	[MV88E6XXX_PORT_CTL0_STATE_DISABLED] = "Disabled",
	[MV88E6XXX_PORT_CTL0_STATE_BLOCKING] = "Blocking/Listening",
	[MV88E6XXX_PORT_CTL0_STATE_LEARNING] = "Learning",
	[MV88E6XXX_PORT_CTL0_STATE_FORWARDING] = "Forwarding",
433 434 435 436 437 438 439
};

int mv88e6xxx_port_set_state(struct mv88e6xxx_chip *chip, int port, u8 state)
{
	u16 reg;
	int err;

440
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
441 442 443
	if (err)
		return err;

444
	reg &= ~MV88E6XXX_PORT_CTL0_STATE_MASK;
445 446 447

	switch (state) {
	case BR_STATE_DISABLED:
448
		state = MV88E6XXX_PORT_CTL0_STATE_DISABLED;
449 450 451
		break;
	case BR_STATE_BLOCKING:
	case BR_STATE_LISTENING:
452
		state = MV88E6XXX_PORT_CTL0_STATE_BLOCKING;
453 454
		break;
	case BR_STATE_LEARNING:
455
		state = MV88E6XXX_PORT_CTL0_STATE_LEARNING;
456 457
		break;
	case BR_STATE_FORWARDING:
458
		state = MV88E6XXX_PORT_CTL0_STATE_FORWARDING;
459 460 461 462 463
		break;
	default:
		return -EINVAL;
	}

464 465
	reg |= state;

466
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
467 468 469
	if (err)
		return err;

470 471
	dev_dbg(chip->dev, "p%d: PortState set to %s\n", port,
		mv88e6xxx_port_state_names[state]);
472 473 474

	return 0;
}
475

476
int mv88e6xxx_port_set_egress_mode(struct mv88e6xxx_chip *chip, int port,
477
				   enum mv88e6xxx_egress_mode mode)
478 479 480 481
{
	int err;
	u16 reg;

482
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
483 484 485
	if (err)
		return err;

486
	reg &= ~MV88E6XXX_PORT_CTL0_EGRESS_MODE_MASK;
487 488 489

	switch (mode) {
	case MV88E6XXX_EGRESS_MODE_UNMODIFIED:
490
		reg |= MV88E6XXX_PORT_CTL0_EGRESS_MODE_UNMODIFIED;
491 492
		break;
	case MV88E6XXX_EGRESS_MODE_UNTAGGED:
493
		reg |= MV88E6XXX_PORT_CTL0_EGRESS_MODE_UNTAGGED;
494 495
		break;
	case MV88E6XXX_EGRESS_MODE_TAGGED:
496
		reg |= MV88E6XXX_PORT_CTL0_EGRESS_MODE_TAGGED;
497 498
		break;
	case MV88E6XXX_EGRESS_MODE_ETHERTYPE:
499
		reg |= MV88E6XXX_PORT_CTL0_EGRESS_MODE_ETHER_TYPE_DSA;
500 501 502 503
		break;
	default:
		return -EINVAL;
	}
504

505
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
506 507 508 509 510 511 512 513
}

int mv88e6085_port_set_frame_mode(struct mv88e6xxx_chip *chip, int port,
				  enum mv88e6xxx_frame_mode mode)
{
	int err;
	u16 reg;

514
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
515 516 517
	if (err)
		return err;

518
	reg &= ~MV88E6XXX_PORT_CTL0_FRAME_MODE_MASK;
519 520 521

	switch (mode) {
	case MV88E6XXX_FRAME_MODE_NORMAL:
522
		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_NORMAL;
523 524
		break;
	case MV88E6XXX_FRAME_MODE_DSA:
525
		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_DSA;
526 527 528 529 530
		break;
	default:
		return -EINVAL;
	}

531
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
532 533 534 535 536 537 538 539
}

int mv88e6351_port_set_frame_mode(struct mv88e6xxx_chip *chip, int port,
				  enum mv88e6xxx_frame_mode mode)
{
	int err;
	u16 reg;

540
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
541 542 543
	if (err)
		return err;

544
	reg &= ~MV88E6XXX_PORT_CTL0_FRAME_MODE_MASK;
545 546 547

	switch (mode) {
	case MV88E6XXX_FRAME_MODE_NORMAL:
548
		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_NORMAL;
549 550
		break;
	case MV88E6XXX_FRAME_MODE_DSA:
551
		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_DSA;
552 553
		break;
	case MV88E6XXX_FRAME_MODE_PROVIDER:
554
		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_PROVIDER;
555 556
		break;
	case MV88E6XXX_FRAME_MODE_ETHERTYPE:
557
		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_ETHER_TYPE_DSA;
558 559 560 561 562
		break;
	default:
		return -EINVAL;
	}

563
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
564 565
}

566 567
static int mv88e6185_port_set_forward_unknown(struct mv88e6xxx_chip *chip,
					      int port, bool unicast)
568 569 570 571
{
	int err;
	u16 reg;

572
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
573 574 575
	if (err)
		return err;

576
	if (unicast)
577
		reg |= MV88E6185_PORT_CTL0_FORWARD_UNKNOWN;
578
	else
579
		reg &= ~MV88E6185_PORT_CTL0_FORWARD_UNKNOWN;
580

581
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
582 583
}

584 585
int mv88e6352_port_set_egress_floods(struct mv88e6xxx_chip *chip, int port,
				     bool unicast, bool multicast)
586 587 588 589
{
	int err;
	u16 reg;

590
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
591 592 593
	if (err)
		return err;

594
	reg &= ~MV88E6352_PORT_CTL0_EGRESS_FLOODS_MASK;
595 596

	if (unicast && multicast)
597
		reg |= MV88E6352_PORT_CTL0_EGRESS_FLOODS_ALL_UNKNOWN_DA;
598
	else if (unicast)
599
		reg |= MV88E6352_PORT_CTL0_EGRESS_FLOODS_NO_UNKNOWN_MC_DA;
600
	else if (multicast)
601
		reg |= MV88E6352_PORT_CTL0_EGRESS_FLOODS_NO_UNKNOWN_UC_DA;
602
	else
603
		reg |= MV88E6352_PORT_CTL0_EGRESS_FLOODS_NO_UNKNOWN_DA;
604

605
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
606 607
}

608 609
/* Offset 0x05: Port Control 1 */

610 611 612 613 614 615
int mv88e6xxx_port_set_message_port(struct mv88e6xxx_chip *chip, int port,
				    bool message_port)
{
	u16 val;
	int err;

616
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL1, &val);
617 618 619 620
	if (err)
		return err;

	if (message_port)
621
		val |= MV88E6XXX_PORT_CTL1_MESSAGE_PORT;
622
	else
623
		val &= ~MV88E6XXX_PORT_CTL1_MESSAGE_PORT;
624

625
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL1, val);
626 627
}

628 629 630 631
/* Offset 0x06: Port Based VLAN Map */

int mv88e6xxx_port_set_vlan_map(struct mv88e6xxx_chip *chip, int port, u16 map)
{
632
	const u16 mask = mv88e6xxx_port_mask(chip);
633 634 635
	u16 reg;
	int err;

636
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_BASE_VLAN, &reg);
637 638 639 640 641 642
	if (err)
		return err;

	reg &= ~mask;
	reg |= map & mask;

643
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_BASE_VLAN, reg);
644 645 646
	if (err)
		return err;

647
	dev_dbg(chip->dev, "p%d: VLANTable set to %.3x\n", port, map);
648 649 650

	return 0;
}
651 652 653 654 655 656 657 658

int mv88e6xxx_port_get_fid(struct mv88e6xxx_chip *chip, int port, u16 *fid)
{
	const u16 upper_mask = (mv88e6xxx_num_databases(chip) - 1) >> 4;
	u16 reg;
	int err;

	/* Port's default FID lower 4 bits are located in reg 0x06, offset 12 */
659
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_BASE_VLAN, &reg);
660 661 662 663 664 665 666
	if (err)
		return err;

	*fid = (reg & 0xf000) >> 12;

	/* Port's default FID upper bits are located in reg 0x05, offset 0 */
	if (upper_mask) {
667 668
		err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL1,
					  &reg);
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
		if (err)
			return err;

		*fid |= (reg & upper_mask) << 4;
	}

	return 0;
}

int mv88e6xxx_port_set_fid(struct mv88e6xxx_chip *chip, int port, u16 fid)
{
	const u16 upper_mask = (mv88e6xxx_num_databases(chip) - 1) >> 4;
	u16 reg;
	int err;

	if (fid >= mv88e6xxx_num_databases(chip))
		return -EINVAL;

	/* Port's default FID lower 4 bits are located in reg 0x06, offset 12 */
688
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_BASE_VLAN, &reg);
689 690 691 692 693 694
	if (err)
		return err;

	reg &= 0x0fff;
	reg |= (fid & 0x000f) << 12;

695
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_BASE_VLAN, reg);
696 697 698 699 700
	if (err)
		return err;

	/* Port's default FID upper bits are located in reg 0x05, offset 0 */
	if (upper_mask) {
701 702
		err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL1,
					  &reg);
703 704 705 706 707 708
		if (err)
			return err;

		reg &= ~upper_mask;
		reg |= (fid >> 4) & upper_mask;

709 710
		err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL1,
					   reg);
711 712 713 714
		if (err)
			return err;
	}

715
	dev_dbg(chip->dev, "p%d: FID set to %u\n", port, fid);
716 717 718

	return 0;
}
719 720 721 722 723 724 725 726

/* Offset 0x07: Default Port VLAN ID & Priority */

int mv88e6xxx_port_get_pvid(struct mv88e6xxx_chip *chip, int port, u16 *pvid)
{
	u16 reg;
	int err;

727 728
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_DEFAULT_VLAN,
				  &reg);
729 730 731
	if (err)
		return err;

732
	*pvid = reg & MV88E6XXX_PORT_DEFAULT_VLAN_MASK;
733 734 735 736 737 738 739 740 741

	return 0;
}

int mv88e6xxx_port_set_pvid(struct mv88e6xxx_chip *chip, int port, u16 pvid)
{
	u16 reg;
	int err;

742 743
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_DEFAULT_VLAN,
				  &reg);
744 745 746
	if (err)
		return err;

747 748
	reg &= ~MV88E6XXX_PORT_DEFAULT_VLAN_MASK;
	reg |= pvid & MV88E6XXX_PORT_DEFAULT_VLAN_MASK;
749

750 751
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_DEFAULT_VLAN,
				   reg);
752 753 754
	if (err)
		return err;

755
	dev_dbg(chip->dev, "p%d: DefaultVID set to %u\n", port, pvid);
756 757 758

	return 0;
}
759 760 761 762

/* Offset 0x08: Port Control 2 Register */

static const char * const mv88e6xxx_port_8021q_mode_names[] = {
763 764 765 766
	[MV88E6XXX_PORT_CTL2_8021Q_MODE_DISABLED] = "Disabled",
	[MV88E6XXX_PORT_CTL2_8021Q_MODE_FALLBACK] = "Fallback",
	[MV88E6XXX_PORT_CTL2_8021Q_MODE_CHECK] = "Check",
	[MV88E6XXX_PORT_CTL2_8021Q_MODE_SECURE] = "Secure",
767 768
};

769 770
static int mv88e6185_port_set_default_forward(struct mv88e6xxx_chip *chip,
					      int port, bool multicast)
771 772 773 774
{
	int err;
	u16 reg;

775
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
776 777 778
	if (err)
		return err;

779
	if (multicast)
780
		reg |= MV88E6XXX_PORT_CTL2_DEFAULT_FORWARD;
781
	else
782
		reg &= ~MV88E6XXX_PORT_CTL2_DEFAULT_FORWARD;
783

784
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
785 786
}

787 788 789 790 791 792 793 794 795 796 797 798
int mv88e6185_port_set_egress_floods(struct mv88e6xxx_chip *chip, int port,
				     bool unicast, bool multicast)
{
	int err;

	err = mv88e6185_port_set_forward_unknown(chip, port, unicast);
	if (err)
		return err;

	return mv88e6185_port_set_default_forward(chip, port, multicast);
}

799 800 801 802 803 804
int mv88e6095_port_set_upstream_port(struct mv88e6xxx_chip *chip, int port,
				     int upstream_port)
{
	int err;
	u16 reg;

805
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
806 807 808
	if (err)
		return err;

809
	reg &= ~MV88E6095_PORT_CTL2_CPU_PORT_MASK;
810 811
	reg |= upstream_port;

812
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
813 814
}

815 816 817 818 819 820
int mv88e6xxx_port_set_8021q_mode(struct mv88e6xxx_chip *chip, int port,
				  u16 mode)
{
	u16 reg;
	int err;

821
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
822 823 824
	if (err)
		return err;

825 826
	reg &= ~MV88E6XXX_PORT_CTL2_8021Q_MODE_MASK;
	reg |= mode & MV88E6XXX_PORT_CTL2_8021Q_MODE_MASK;
827

828
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
829 830 831
	if (err)
		return err;

832 833
	dev_dbg(chip->dev, "p%d: 802.1QMode set to %s\n", port,
		mv88e6xxx_port_8021q_mode_names[mode]);
834 835 836

	return 0;
}
837

838 839 840 841 842
int mv88e6xxx_port_set_map_da(struct mv88e6xxx_chip *chip, int port)
{
	u16 reg;
	int err;

843
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
844 845 846
	if (err)
		return err;

847
	reg |= MV88E6XXX_PORT_CTL2_MAP_DA;
848

849
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
850 851
}

852 853
int mv88e6165_port_set_jumbo_size(struct mv88e6xxx_chip *chip, int port,
				  size_t size)
854 855 856 857
{
	u16 reg;
	int err;

858
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
859 860 861
	if (err)
		return err;

862
	reg &= ~MV88E6XXX_PORT_CTL2_JUMBO_MODE_MASK;
863 864

	if (size <= 1522)
865
		reg |= MV88E6XXX_PORT_CTL2_JUMBO_MODE_1522;
866
	else if (size <= 2048)
867
		reg |= MV88E6XXX_PORT_CTL2_JUMBO_MODE_2048;
868
	else if (size <= 10240)
869
		reg |= MV88E6XXX_PORT_CTL2_JUMBO_MODE_10240;
870 871
	else
		return -ERANGE;
872

873
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
874 875
}

876 877 878 879
/* Offset 0x09: Port Rate Control */

int mv88e6095_port_egress_rate_limiting(struct mv88e6xxx_chip *chip, int port)
{
880 881
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_EGRESS_RATE_CTL1,
				    0x0000);
882 883 884 885
}

int mv88e6097_port_egress_rate_limiting(struct mv88e6xxx_chip *chip, int port)
{
886 887
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_EGRESS_RATE_CTL1,
				    0x0001);
888 889
}

890 891 892 893
/* Offset 0x0C: Port ATU Control */

int mv88e6xxx_port_disable_learn_limit(struct mv88e6xxx_chip *chip, int port)
{
894
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_ATU_CTL, 0);
895 896
}

897 898 899 900
/* Offset 0x0D: (Priority) Override Register */

int mv88e6xxx_port_disable_pri_override(struct mv88e6xxx_chip *chip, int port)
{
901
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_PRI_OVERRIDE, 0);
902 903
}

904 905 906 907 908
/* Offset 0x0f: Port Ether type */

int mv88e6351_port_set_ether_type(struct mv88e6xxx_chip *chip, int port,
				  u16 etype)
{
909
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_ETH_TYPE, etype);
910 911
}

912 913 914 915 916 917 918 919 920
/* Offset 0x18: Port IEEE Priority Remapping Registers [0-3]
 * Offset 0x19: Port IEEE Priority Remapping Registers [4-7]
 */

int mv88e6095_port_tag_remap(struct mv88e6xxx_chip *chip, int port)
{
	int err;

	/* Use a direct priority mapping for all IEEE tagged frames */
921 922 923
	err = mv88e6xxx_port_write(chip, port,
				   MV88E6095_PORT_IEEE_PRIO_REMAP_0123,
				   0x3210);
924 925 926
	if (err)
		return err;

927 928 929
	return mv88e6xxx_port_write(chip, port,
				    MV88E6095_PORT_IEEE_PRIO_REMAP_4567,
				    0x7654);
930 931 932
}

static int mv88e6xxx_port_ieeepmt_write(struct mv88e6xxx_chip *chip,
933
					int port, u16 table, u8 ptr, u16 data)
934 935 936
{
	u16 reg;

937 938 939
	reg = MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_UPDATE | table |
		(ptr << __bf_shf(MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_PTR_MASK)) |
		(data & MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_DATA_MASK);
940

941 942
	return mv88e6xxx_port_write(chip, port,
				    MV88E6390_PORT_IEEE_PRIO_MAP_TABLE, reg);
943 944 945 946 947
}

int mv88e6390_port_tag_remap(struct mv88e6xxx_chip *chip, int port)
{
	int err, i;
948
	u16 table;
949 950

	for (i = 0; i <= 7; i++) {
951 952 953
		table = MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_INGRESS_PCP;
		err = mv88e6xxx_port_ieeepmt_write(chip, port, table, i,
						   (i | i << 4));
954 955 956
		if (err)
			return err;

957 958
		table = MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_EGRESS_GREEN_PCP;
		err = mv88e6xxx_port_ieeepmt_write(chip, port, table, i, i);
959 960 961
		if (err)
			return err;

962 963
		table = MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_EGRESS_YELLOW_PCP;
		err = mv88e6xxx_port_ieeepmt_write(chip, port, table, i, i);
964 965 966
		if (err)
			return err;

967 968
		table = MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_EGRESS_AVB_PCP;
		err = mv88e6xxx_port_ieeepmt_write(chip, port, table, i, i);
969 970 971 972 973 974
		if (err)
			return err;
	}

	return 0;
}