port.c 34.1 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
2 3 4 5 6
/*
 * Marvell 88E6xxx Switch Port Registers support
 *
 * Copyright (c) 2008 Marvell Semiconductor
 *
V
Vivien Didelot 已提交
7 8
 * Copyright (c) 2016-2017 Savoir-faire Linux Inc.
 *	Vivien Didelot <vivien.didelot@savoirfairelinux.com>
9 10
 */

11
#include <linux/bitfield.h>
12
#include <linux/if_bridge.h>
13
#include <linux/phy.h>
14
#include <linux/phylink.h>
15 16

#include "chip.h"
17
#include "port.h"
18
#include "serdes.h"
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

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);
}
35

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
/* Offset 0x00: MAC (or PCS or Physical) Status Register
 *
 * For most devices, this is read only. However the 6185 has the MyPause
 * bit read/write.
 */
int mv88e6185_port_set_pause(struct mv88e6xxx_chip *chip, int port,
			     int pause)
{
	u16 reg;
	int err;

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

	if (pause)
		reg |= MV88E6XXX_PORT_STS_MY_PAUSE;
	else
		reg &= ~MV88E6XXX_PORT_STS_MY_PAUSE;

	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_STS, reg);
}

59 60 61
/* Offset 0x01: MAC (or PCS or Physical) Control Register
 *
 * Link, Duplex and Flow Control have one force bit, one value bit.
62 63 64 65
 *
 * 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.
66 67
 */

68 69 70 71 72 73
static int mv88e6xxx_port_set_rgmii_delay(struct mv88e6xxx_chip *chip, int port,
					  phy_interface_t mode)
{
	u16 reg;
	int err;

74
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_MAC_CTL, &reg);
75 76 77
	if (err)
		return err;

78 79
	reg &= ~(MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_RXCLK |
		 MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_TXCLK);
80 81 82

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

98
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_MAC_CTL, reg);
99 100 101
	if (err)
		return err;

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

	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);
}

127 128 129 130 131
int mv88e6xxx_port_set_link(struct mv88e6xxx_chip *chip, int port, int link)
{
	u16 reg;
	int err;

132
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_MAC_CTL, &reg);
133 134 135
	if (err)
		return err;

136 137
	reg &= ~(MV88E6XXX_PORT_MAC_CTL_FORCE_LINK |
		 MV88E6XXX_PORT_MAC_CTL_LINK_UP);
138 139 140

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

154
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_MAC_CTL, reg);
155 156 157
	if (err)
		return err;

158
	dev_dbg(chip->dev, "p%d: %s link %s\n", port,
159 160
		reg & MV88E6XXX_PORT_MAC_CTL_FORCE_LINK ? "Force" : "Unforce",
		reg & MV88E6XXX_PORT_MAC_CTL_LINK_UP ? "up" : "down");
161 162 163 164

	return 0;
}

165 166 167 168 169
int mv88e6xxx_port_set_duplex(struct mv88e6xxx_chip *chip, int port, int dup)
{
	u16 reg;
	int err;

170
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_MAC_CTL, &reg);
171 172 173
	if (err)
		return err;

174 175
	reg &= ~(MV88E6XXX_PORT_MAC_CTL_FORCE_DUPLEX |
		 MV88E6XXX_PORT_MAC_CTL_DUPLEX_FULL);
176 177 178

	switch (dup) {
	case DUPLEX_HALF:
179
		reg |= MV88E6XXX_PORT_MAC_CTL_FORCE_DUPLEX;
180 181
		break;
	case DUPLEX_FULL:
182 183
		reg |= MV88E6XXX_PORT_MAC_CTL_FORCE_DUPLEX |
			MV88E6XXX_PORT_MAC_CTL_DUPLEX_FULL;
184 185 186 187 188
		break;
	case DUPLEX_UNFORCED:
		/* normal duplex detection */
		break;
	default:
189
		return -EOPNOTSUPP;
190 191
	}

192
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_MAC_CTL, reg);
193 194 195
	if (err)
		return err;

196
	dev_dbg(chip->dev, "p%d: %s %s duplex\n", port,
197 198
		reg & MV88E6XXX_PORT_MAC_CTL_FORCE_DUPLEX ? "Force" : "Unforce",
		reg & MV88E6XXX_PORT_MAC_CTL_DUPLEX_FULL ? "full" : "half");
199 200 201 202

	return 0;
}

203 204 205 206 207 208 209 210
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:
211
		ctrl = MV88E6XXX_PORT_MAC_CTL_SPEED_10;
212 213
		break;
	case 100:
214
		ctrl = MV88E6XXX_PORT_MAC_CTL_SPEED_100;
215 216 217
		break;
	case 200:
		if (alt_bit)
218 219
			ctrl = MV88E6XXX_PORT_MAC_CTL_SPEED_100 |
				MV88E6390_PORT_MAC_CTL_ALTSPEED;
220
		else
221
			ctrl = MV88E6065_PORT_MAC_CTL_SPEED_200;
222 223
		break;
	case 1000:
224
		ctrl = MV88E6XXX_PORT_MAC_CTL_SPEED_1000;
225 226
		break;
	case 2500:
227 228 229 230 231
		if (alt_bit)
			ctrl = MV88E6390_PORT_MAC_CTL_SPEED_10000 |
				MV88E6390_PORT_MAC_CTL_ALTSPEED;
		else
			ctrl = MV88E6390_PORT_MAC_CTL_SPEED_10000;
232 233 234 235
		break;
	case 10000:
		/* all bits set, fall through... */
	case SPEED_UNFORCED:
236
		ctrl = MV88E6XXX_PORT_MAC_CTL_SPEED_UNFORCED;
237 238 239 240 241
		break;
	default:
		return -EOPNOTSUPP;
	}

242
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_MAC_CTL, &reg);
243 244 245
	if (err)
		return err;

246
	reg &= ~MV88E6XXX_PORT_MAC_CTL_SPEED_MASK;
247
	if (alt_bit)
248
		reg &= ~MV88E6390_PORT_MAC_CTL_ALTSPEED;
249
	if (force_bit) {
250
		reg &= ~MV88E6390_PORT_MAC_CTL_FORCE_SPEED;
251
		if (speed != SPEED_UNFORCED)
252
			ctrl |= MV88E6390_PORT_MAC_CTL_FORCE_SPEED;
253 254 255
	}
	reg |= ctrl;

256
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_MAC_CTL, reg);
257 258 259 260
	if (err)
		return err;

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

	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);
}

293 294 295 296 297 298 299 300 301 302 303 304
/* Support 10, 100 Mbps (e.g. 88E6250 family) */
int mv88e6250_port_set_speed(struct mv88e6xxx_chip *chip, int port, int speed)
{
	if (speed == SPEED_MAX)
		speed = 100;

	if (speed > 100)
		return -EOPNOTSUPP;

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

305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
/* Support 10, 100, 200, 1000, 2500 Mbps (e.g. 88E6341) */
int mv88e6341_port_set_speed(struct mv88e6xxx_chip *chip, int port, int speed)
{
	if (speed == SPEED_MAX)
		speed = port < 5 ? 1000 : 2500;

	if (speed > 2500)
		return -EOPNOTSUPP;

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

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

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

323 324 325 326 327 328 329 330
phy_interface_t mv88e6341_port_max_speed_mode(int port)
{
	if (port == 5)
		return PHY_INTERFACE_MODE_2500BASEX;

	return PHY_INTERFACE_MODE_NA;
}

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
/* 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);
}

364 365 366 367 368 369 370 371
phy_interface_t mv88e6390_port_max_speed_mode(int port)
{
	if (port == 9 || port == 10)
		return PHY_INTERFACE_MODE_2500BASEX;

	return PHY_INTERFACE_MODE_NA;
}

372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
/* 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);
}

387 388 389 390 391 392 393 394
phy_interface_t mv88e6390x_port_max_speed_mode(int port)
{
	if (port == 9 || port == 10)
		return PHY_INTERFACE_MODE_XAUI;

	return PHY_INTERFACE_MODE_NA;
}

395 396
static int mv88e6xxx_port_set_cmode(struct mv88e6xxx_chip *chip, int port,
				    phy_interface_t mode)
397
{
398
	u8 lane;
399
	u16 cmode;
400
	u16 reg;
401 402
	int err;

403 404 405 406 407 408
	/* Default to a slow mode, so freeing up SERDES interfaces for
	 * other ports which might use them for SFPs.
	 */
	if (mode == PHY_INTERFACE_MODE_NA)
		mode = PHY_INTERFACE_MODE_1000BASEX;

409 410
	switch (mode) {
	case PHY_INTERFACE_MODE_1000BASEX:
411
		cmode = MV88E6XXX_PORT_STS_CMODE_1000BASEX;
412 413
		break;
	case PHY_INTERFACE_MODE_SGMII:
414
		cmode = MV88E6XXX_PORT_STS_CMODE_SGMII;
415 416
		break;
	case PHY_INTERFACE_MODE_2500BASEX:
417
		cmode = MV88E6XXX_PORT_STS_CMODE_2500BASEX;
418 419
		break;
	case PHY_INTERFACE_MODE_XGMII:
420
	case PHY_INTERFACE_MODE_XAUI:
421
		cmode = MV88E6XXX_PORT_STS_CMODE_XAUI;
422 423
		break;
	case PHY_INTERFACE_MODE_RXAUI:
424
		cmode = MV88E6XXX_PORT_STS_CMODE_RXAUI;
425 426 427 428 429
		break;
	default:
		cmode = 0;
	}

430 431 432 433
	/* cmode doesn't change, nothing to do for us */
	if (cmode == chip->ports[port].cmode)
		return 0;

434 435
	lane = mv88e6xxx_serdes_get_lane(chip, port);
	if (lane) {
436
		if (chip->ports[port].serdes_irq) {
437
			err = mv88e6xxx_serdes_irq_disable(chip, port, lane);
438 439 440 441
			if (err)
				return err;
		}

442
		err = mv88e6xxx_serdes_power_down(chip, port, lane);
443 444 445 446
		if (err)
			return err;
	}

447
	chip->ports[port].cmode = 0;
448

449
	if (cmode) {
450
		err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_STS, &reg);
451 452 453
		if (err)
			return err;

454
		reg &= ~MV88E6XXX_PORT_STS_CMODE_MASK;
455 456
		reg |= cmode;

457
		err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_STS, reg);
458 459
		if (err)
			return err;
460

461 462
		chip->ports[port].cmode = cmode;

463 464 465
		lane = mv88e6xxx_serdes_get_lane(chip, port);
		if (!lane)
			return -ENODEV;
466

467
		err = mv88e6xxx_serdes_power_up(chip, port, lane);
468 469
		if (err)
			return err;
470 471

		if (chip->ports[port].serdes_irq) {
472
			err = mv88e6xxx_serdes_irq_enable(chip, port, lane);
473 474 475
			if (err)
				return err;
		}
476 477 478 479 480
	}

	return 0;
}

481 482 483 484 485 486 487 488 489
int mv88e6390x_port_set_cmode(struct mv88e6xxx_chip *chip, int port,
			      phy_interface_t mode)
{
	if (port != 9 && port != 10)
		return -EOPNOTSUPP;

	return mv88e6xxx_port_set_cmode(chip, port, mode);
}

490 491 492
int mv88e6390_port_set_cmode(struct mv88e6xxx_chip *chip, int port,
			     phy_interface_t mode)
{
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
	if (port != 9 && port != 10)
		return -EOPNOTSUPP;

	switch (mode) {
	case PHY_INTERFACE_MODE_NA:
		return 0;
	case PHY_INTERFACE_MODE_XGMII:
	case PHY_INTERFACE_MODE_XAUI:
	case PHY_INTERFACE_MODE_RXAUI:
		return -EINVAL;
	default:
		break;
	}

	return mv88e6xxx_port_set_cmode(chip, port, mode);
}

510 511
static int mv88e6341_port_set_cmode_writable(struct mv88e6xxx_chip *chip,
					     int port)
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
{
	int err, addr;
	u16 reg, bits;

	if (port != 5)
		return -EOPNOTSUPP;

	addr = chip->info->port_base_addr + port;

	err = mv88e6xxx_port_hidden_read(chip, 0x7, addr, 0, &reg);
	if (err)
		return err;

	bits = MV88E6341_PORT_RESERVED_1A_FORCE_CMODE |
	       MV88E6341_PORT_RESERVED_1A_SGMII_AN;

	if ((reg & bits) == bits)
		return 0;

	reg |= bits;
	return mv88e6xxx_port_hidden_write(chip, 0x7, addr, 0, reg);
}

int mv88e6341_port_set_cmode(struct mv88e6xxx_chip *chip, int port,
			     phy_interface_t mode)
{
538 539
	int err;

540 541 542
	if (port != 5)
		return -EOPNOTSUPP;

543
	switch (mode) {
544 545
	case PHY_INTERFACE_MODE_NA:
		return 0;
546 547 548 549 550 551 552 553
	case PHY_INTERFACE_MODE_XGMII:
	case PHY_INTERFACE_MODE_XAUI:
	case PHY_INTERFACE_MODE_RXAUI:
		return -EINVAL;
	default:
		break;
	}

554 555 556 557
	err = mv88e6341_port_set_cmode_writable(chip, port);
	if (err)
		return err;

558
	return mv88e6xxx_port_set_cmode(chip, port, mode);
559 560
}

561
int mv88e6185_port_get_cmode(struct mv88e6xxx_chip *chip, int port, u8 *cmode)
562 563 564 565 566 567 568 569
{
	int err;
	u16 reg;

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

570 571 572
	*cmode = reg & MV88E6185_PORT_STS_CMODE_MASK;

	return 0;
573 574
}

575
int mv88e6352_port_get_cmode(struct mv88e6xxx_chip *chip, int port, u8 *cmode)
576 577 578 579
{
	int err;
	u16 reg;

580
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_STS, &reg);
581 582 583
	if (err)
		return err;

584
	*cmode = reg & MV88E6XXX_PORT_STS_CMODE_MASK;
585 586 587 588

	return 0;
}

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 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
int mv88e6250_port_link_state(struct mv88e6xxx_chip *chip, int port,
			      struct phylink_link_state *state)
{
	int err;
	u16 reg;

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

	if (port < 5) {
		switch (reg & MV88E6250_PORT_STS_PORTMODE_MASK) {
		case MV88E6250_PORT_STS_PORTMODE_PHY_10_HALF:
			state->speed = SPEED_10;
			state->duplex = DUPLEX_HALF;
			break;
		case MV88E6250_PORT_STS_PORTMODE_PHY_100_HALF:
			state->speed = SPEED_100;
			state->duplex = DUPLEX_HALF;
			break;
		case MV88E6250_PORT_STS_PORTMODE_PHY_10_FULL:
			state->speed = SPEED_10;
			state->duplex = DUPLEX_FULL;
			break;
		case MV88E6250_PORT_STS_PORTMODE_PHY_100_FULL:
			state->speed = SPEED_100;
			state->duplex = DUPLEX_FULL;
			break;
		default:
			state->speed = SPEED_UNKNOWN;
			state->duplex = DUPLEX_UNKNOWN;
			break;
		}
	} else {
		switch (reg & MV88E6250_PORT_STS_PORTMODE_MASK) {
		case MV88E6250_PORT_STS_PORTMODE_MII_10_HALF:
			state->speed = SPEED_10;
			state->duplex = DUPLEX_HALF;
			break;
		case MV88E6250_PORT_STS_PORTMODE_MII_100_HALF:
			state->speed = SPEED_100;
			state->duplex = DUPLEX_HALF;
			break;
		case MV88E6250_PORT_STS_PORTMODE_MII_10_FULL:
			state->speed = SPEED_10;
			state->duplex = DUPLEX_FULL;
			break;
		case MV88E6250_PORT_STS_PORTMODE_MII_100_FULL:
			state->speed = SPEED_100;
			state->duplex = DUPLEX_FULL;
			break;
		default:
			state->speed = SPEED_UNKNOWN;
			state->duplex = DUPLEX_UNKNOWN;
			break;
		}
	}

	state->link = !!(reg & MV88E6250_PORT_STS_LINK);
	state->an_enabled = 1;
	state->an_complete = state->link;
650
	state->interface = PHY_INTERFACE_MODE_NA;
651 652 653 654

	return 0;
}

655
int mv88e6352_port_link_state(struct mv88e6xxx_chip *chip, int port,
656 657 658 659 660
			      struct phylink_link_state *state)
{
	int err;
	u16 reg;

661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
	switch (chip->ports[port].cmode) {
	case MV88E6XXX_PORT_STS_CMODE_RGMII:
		err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_MAC_CTL,
					  &reg);
		if (err)
			return err;

		if ((reg & MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_RXCLK) &&
		    (reg & MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_TXCLK))
			state->interface = PHY_INTERFACE_MODE_RGMII_ID;
		else if (reg & MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_RXCLK)
			state->interface = PHY_INTERFACE_MODE_RGMII_RXID;
		else if (reg & MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_TXCLK)
			state->interface = PHY_INTERFACE_MODE_RGMII_TXID;
		else
			state->interface = PHY_INTERFACE_MODE_RGMII;
		break;
678
	case MV88E6XXX_PORT_STS_CMODE_1000BASEX:
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
		state->interface = PHY_INTERFACE_MODE_1000BASEX;
		break;
	case MV88E6XXX_PORT_STS_CMODE_SGMII:
		state->interface = PHY_INTERFACE_MODE_SGMII;
		break;
	case MV88E6XXX_PORT_STS_CMODE_2500BASEX:
		state->interface = PHY_INTERFACE_MODE_2500BASEX;
		break;
	case MV88E6XXX_PORT_STS_CMODE_XAUI:
		state->interface = PHY_INTERFACE_MODE_XAUI;
		break;
	case MV88E6XXX_PORT_STS_CMODE_RXAUI:
		state->interface = PHY_INTERFACE_MODE_RXAUI;
		break;
	default:
		/* we do not support other cmode values here */
		state->interface = PHY_INTERFACE_MODE_NA;
	}

698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_STS, &reg);
	if (err)
		return err;

	switch (reg & MV88E6XXX_PORT_STS_SPEED_MASK) {
	case MV88E6XXX_PORT_STS_SPEED_10:
		state->speed = SPEED_10;
		break;
	case MV88E6XXX_PORT_STS_SPEED_100:
		state->speed = SPEED_100;
		break;
	case MV88E6XXX_PORT_STS_SPEED_1000:
		state->speed = SPEED_1000;
		break;
	case MV88E6XXX_PORT_STS_SPEED_10000:
713
		if ((reg & MV88E6XXX_PORT_STS_CMODE_MASK) ==
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
		    MV88E6XXX_PORT_STS_CMODE_2500BASEX)
			state->speed = SPEED_2500;
		else
			state->speed = SPEED_10000;
		break;
	}

	state->duplex = reg & MV88E6XXX_PORT_STS_DUPLEX ?
			DUPLEX_FULL : DUPLEX_HALF;
	state->link = !!(reg & MV88E6XXX_PORT_STS_LINK);
	state->an_enabled = 1;
	state->an_complete = state->link;

	return 0;
}

730 731 732 733
int mv88e6185_port_link_state(struct mv88e6xxx_chip *chip, int port,
			      struct phylink_link_state *state)
{
	if (state->interface == PHY_INTERFACE_MODE_1000BASEX) {
734
		u8 cmode = chip->ports[port].cmode;
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765

		/* When a port is in "Cross-chip serdes" mode, it uses
		 * 1000Base-X full duplex mode, but there is no automatic
		 * link detection. Use the sync OK status for link (as it
		 * would do for 1000Base-X mode.)
		 */
		if (cmode == MV88E6185_PORT_STS_CMODE_SERDES) {
			u16 mac;
			int err;

			err = mv88e6xxx_port_read(chip, port,
						  MV88E6XXX_PORT_MAC_CTL, &mac);
			if (err)
				return err;

			state->link = !!(mac & MV88E6185_PORT_MAC_CTL_SYNC_OK);
			state->an_enabled = 1;
			state->an_complete =
				!!(mac & MV88E6185_PORT_MAC_CTL_AN_DONE);
			state->duplex =
				state->link ? DUPLEX_FULL : DUPLEX_UNKNOWN;
			state->speed =
				state->link ? SPEED_1000 : SPEED_UNKNOWN;

			return 0;
		}
	}

	return mv88e6352_port_link_state(chip, port, state);
}

766
/* Offset 0x02: Jamming Control
767 768 769 770 771
 *
 * 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.
 */
772 773
int mv88e6097_port_pause_limit(struct mv88e6xxx_chip *chip, int port, u8 in,
			       u8 out)
774
{
775 776
	return mv88e6xxx_port_write(chip, port, MV88E6097_PORT_JAM_CTL,
				    out << 8 | in);
777 778
}

779 780
int mv88e6390_port_pause_limit(struct mv88e6xxx_chip *chip, int port, u8 in,
			       u8 out)
781 782 783
{
	int err;

784 785 786
	err = mv88e6xxx_port_write(chip, port, MV88E6390_PORT_FLOW_CTL,
				   MV88E6390_PORT_FLOW_CTL_UPDATE |
				   MV88E6390_PORT_FLOW_CTL_LIMIT_IN | in);
787 788 789
	if (err)
		return err;

790 791 792
	return mv88e6xxx_port_write(chip, port, MV88E6390_PORT_FLOW_CTL,
				    MV88E6390_PORT_FLOW_CTL_UPDATE |
				    MV88E6390_PORT_FLOW_CTL_LIMIT_OUT | out);
793 794
}

795 796 797
/* Offset 0x04: Port Control Register */

static const char * const mv88e6xxx_port_state_names[] = {
798 799 800 801
	[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",
802 803 804 805 806 807 808
};

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

809
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
810 811 812
	if (err)
		return err;

813
	reg &= ~MV88E6XXX_PORT_CTL0_STATE_MASK;
814 815 816

	switch (state) {
	case BR_STATE_DISABLED:
817
		state = MV88E6XXX_PORT_CTL0_STATE_DISABLED;
818 819 820
		break;
	case BR_STATE_BLOCKING:
	case BR_STATE_LISTENING:
821
		state = MV88E6XXX_PORT_CTL0_STATE_BLOCKING;
822 823
		break;
	case BR_STATE_LEARNING:
824
		state = MV88E6XXX_PORT_CTL0_STATE_LEARNING;
825 826
		break;
	case BR_STATE_FORWARDING:
827
		state = MV88E6XXX_PORT_CTL0_STATE_FORWARDING;
828 829 830 831 832
		break;
	default:
		return -EINVAL;
	}

833 834
	reg |= state;

835
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
836 837 838
	if (err)
		return err;

839 840
	dev_dbg(chip->dev, "p%d: PortState set to %s\n", port,
		mv88e6xxx_port_state_names[state]);
841 842 843

	return 0;
}
844

845
int mv88e6xxx_port_set_egress_mode(struct mv88e6xxx_chip *chip, int port,
846
				   enum mv88e6xxx_egress_mode mode)
847 848 849 850
{
	int err;
	u16 reg;

851
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
852 853 854
	if (err)
		return err;

855
	reg &= ~MV88E6XXX_PORT_CTL0_EGRESS_MODE_MASK;
856 857 858

	switch (mode) {
	case MV88E6XXX_EGRESS_MODE_UNMODIFIED:
859
		reg |= MV88E6XXX_PORT_CTL0_EGRESS_MODE_UNMODIFIED;
860 861
		break;
	case MV88E6XXX_EGRESS_MODE_UNTAGGED:
862
		reg |= MV88E6XXX_PORT_CTL0_EGRESS_MODE_UNTAGGED;
863 864
		break;
	case MV88E6XXX_EGRESS_MODE_TAGGED:
865
		reg |= MV88E6XXX_PORT_CTL0_EGRESS_MODE_TAGGED;
866 867
		break;
	case MV88E6XXX_EGRESS_MODE_ETHERTYPE:
868
		reg |= MV88E6XXX_PORT_CTL0_EGRESS_MODE_ETHER_TYPE_DSA;
869 870 871 872
		break;
	default:
		return -EINVAL;
	}
873

874
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
875 876 877 878 879 880 881 882
}

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

883
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
884 885 886
	if (err)
		return err;

887
	reg &= ~MV88E6XXX_PORT_CTL0_FRAME_MODE_MASK;
888 889 890

	switch (mode) {
	case MV88E6XXX_FRAME_MODE_NORMAL:
891
		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_NORMAL;
892 893
		break;
	case MV88E6XXX_FRAME_MODE_DSA:
894
		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_DSA;
895 896 897 898 899
		break;
	default:
		return -EINVAL;
	}

900
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
901 902 903 904 905 906 907 908
}

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

909
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
910 911 912
	if (err)
		return err;

913
	reg &= ~MV88E6XXX_PORT_CTL0_FRAME_MODE_MASK;
914 915 916

	switch (mode) {
	case MV88E6XXX_FRAME_MODE_NORMAL:
917
		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_NORMAL;
918 919
		break;
	case MV88E6XXX_FRAME_MODE_DSA:
920
		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_DSA;
921 922
		break;
	case MV88E6XXX_FRAME_MODE_PROVIDER:
923
		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_PROVIDER;
924 925
		break;
	case MV88E6XXX_FRAME_MODE_ETHERTYPE:
926
		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_ETHER_TYPE_DSA;
927 928 929 930 931
		break;
	default:
		return -EINVAL;
	}

932
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
933 934
}

935 936
static int mv88e6185_port_set_forward_unknown(struct mv88e6xxx_chip *chip,
					      int port, bool unicast)
937 938 939 940
{
	int err;
	u16 reg;

941
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
942 943 944
	if (err)
		return err;

945
	if (unicast)
946
		reg |= MV88E6185_PORT_CTL0_FORWARD_UNKNOWN;
947
	else
948
		reg &= ~MV88E6185_PORT_CTL0_FORWARD_UNKNOWN;
949

950
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
951 952
}

953 954
int mv88e6352_port_set_egress_floods(struct mv88e6xxx_chip *chip, int port,
				     bool unicast, bool multicast)
955 956 957 958
{
	int err;
	u16 reg;

959
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
960 961 962
	if (err)
		return err;

963
	reg &= ~MV88E6352_PORT_CTL0_EGRESS_FLOODS_MASK;
964 965

	if (unicast && multicast)
966
		reg |= MV88E6352_PORT_CTL0_EGRESS_FLOODS_ALL_UNKNOWN_DA;
967
	else if (unicast)
968
		reg |= MV88E6352_PORT_CTL0_EGRESS_FLOODS_NO_UNKNOWN_MC_DA;
969
	else if (multicast)
970
		reg |= MV88E6352_PORT_CTL0_EGRESS_FLOODS_NO_UNKNOWN_UC_DA;
971
	else
972
		reg |= MV88E6352_PORT_CTL0_EGRESS_FLOODS_NO_UNKNOWN_DA;
973

974
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
975 976
}

977 978
/* Offset 0x05: Port Control 1 */

979 980 981 982 983 984
int mv88e6xxx_port_set_message_port(struct mv88e6xxx_chip *chip, int port,
				    bool message_port)
{
	u16 val;
	int err;

985
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL1, &val);
986 987 988 989
	if (err)
		return err;

	if (message_port)
990
		val |= MV88E6XXX_PORT_CTL1_MESSAGE_PORT;
991
	else
992
		val &= ~MV88E6XXX_PORT_CTL1_MESSAGE_PORT;
993

994
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL1, val);
995 996
}

997 998 999 1000
/* Offset 0x06: Port Based VLAN Map */

int mv88e6xxx_port_set_vlan_map(struct mv88e6xxx_chip *chip, int port, u16 map)
{
1001
	const u16 mask = mv88e6xxx_port_mask(chip);
1002 1003 1004
	u16 reg;
	int err;

1005
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_BASE_VLAN, &reg);
1006 1007 1008 1009 1010 1011
	if (err)
		return err;

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

1012
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_BASE_VLAN, reg);
1013 1014 1015
	if (err)
		return err;

1016
	dev_dbg(chip->dev, "p%d: VLANTable set to %.3x\n", port, map);
1017 1018 1019

	return 0;
}
1020 1021 1022 1023 1024 1025 1026 1027

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 */
1028
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_BASE_VLAN, &reg);
1029 1030 1031 1032 1033 1034 1035
	if (err)
		return err;

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

	/* Port's default FID upper bits are located in reg 0x05, offset 0 */
	if (upper_mask) {
1036 1037
		err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL1,
					  &reg);
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
		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 */
1057
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_BASE_VLAN, &reg);
1058 1059 1060 1061 1062 1063
	if (err)
		return err;

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

1064
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_BASE_VLAN, reg);
1065 1066 1067 1068 1069
	if (err)
		return err;

	/* Port's default FID upper bits are located in reg 0x05, offset 0 */
	if (upper_mask) {
1070 1071
		err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL1,
					  &reg);
1072 1073 1074 1075 1076 1077
		if (err)
			return err;

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

1078 1079
		err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL1,
					   reg);
1080 1081 1082 1083
		if (err)
			return err;
	}

1084
	dev_dbg(chip->dev, "p%d: FID set to %u\n", port, fid);
1085 1086 1087

	return 0;
}
1088 1089 1090 1091 1092 1093 1094 1095

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

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

1096 1097
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_DEFAULT_VLAN,
				  &reg);
1098 1099 1100
	if (err)
		return err;

1101
	*pvid = reg & MV88E6XXX_PORT_DEFAULT_VLAN_MASK;
1102 1103 1104 1105 1106 1107 1108 1109 1110

	return 0;
}

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

1111 1112
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_DEFAULT_VLAN,
				  &reg);
1113 1114 1115
	if (err)
		return err;

1116 1117
	reg &= ~MV88E6XXX_PORT_DEFAULT_VLAN_MASK;
	reg |= pvid & MV88E6XXX_PORT_DEFAULT_VLAN_MASK;
1118

1119 1120
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_DEFAULT_VLAN,
				   reg);
1121 1122 1123
	if (err)
		return err;

1124
	dev_dbg(chip->dev, "p%d: DefaultVID set to %u\n", port, pvid);
1125 1126 1127

	return 0;
}
1128 1129 1130 1131

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

static const char * const mv88e6xxx_port_8021q_mode_names[] = {
1132 1133 1134 1135
	[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",
1136 1137
};

1138 1139
static int mv88e6185_port_set_default_forward(struct mv88e6xxx_chip *chip,
					      int port, bool multicast)
1140 1141 1142 1143
{
	int err;
	u16 reg;

1144
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
1145 1146 1147
	if (err)
		return err;

1148
	if (multicast)
1149
		reg |= MV88E6XXX_PORT_CTL2_DEFAULT_FORWARD;
1150
	else
1151
		reg &= ~MV88E6XXX_PORT_CTL2_DEFAULT_FORWARD;
1152

1153
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
1154 1155
}

1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
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);
}

1168 1169 1170 1171 1172 1173
int mv88e6095_port_set_upstream_port(struct mv88e6xxx_chip *chip, int port,
				     int upstream_port)
{
	int err;
	u16 reg;

1174
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
1175 1176 1177
	if (err)
		return err;

1178
	reg &= ~MV88E6095_PORT_CTL2_CPU_PORT_MASK;
1179 1180
	reg |= upstream_port;

1181
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
1182 1183
}

1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
int mv88e6xxx_port_set_mirror(struct mv88e6xxx_chip *chip, int port,
			      enum mv88e6xxx_egress_direction direction,
			      bool mirror)
{
	bool *mirror_port;
	u16 reg;
	u16 bit;
	int err;

	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
	if (err)
		return err;

	switch (direction) {
	case MV88E6XXX_EGRESS_DIR_INGRESS:
		bit = MV88E6XXX_PORT_CTL2_INGRESS_MONITOR;
		mirror_port = &chip->ports[port].mirror_ingress;
		break;
	case MV88E6XXX_EGRESS_DIR_EGRESS:
		bit = MV88E6XXX_PORT_CTL2_EGRESS_MONITOR;
		mirror_port = &chip->ports[port].mirror_egress;
		break;
	default:
		return -EINVAL;
	}

	reg &= ~bit;
	if (mirror)
		reg |= bit;

	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
	if (!err)
		*mirror_port = mirror;

	return err;
}

1221 1222 1223 1224 1225 1226
int mv88e6xxx_port_set_8021q_mode(struct mv88e6xxx_chip *chip, int port,
				  u16 mode)
{
	u16 reg;
	int err;

1227
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
1228 1229 1230
	if (err)
		return err;

1231 1232
	reg &= ~MV88E6XXX_PORT_CTL2_8021Q_MODE_MASK;
	reg |= mode & MV88E6XXX_PORT_CTL2_8021Q_MODE_MASK;
1233

1234
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
1235 1236 1237
	if (err)
		return err;

1238 1239
	dev_dbg(chip->dev, "p%d: 802.1QMode set to %s\n", port,
		mv88e6xxx_port_8021q_mode_names[mode]);
1240 1241 1242

	return 0;
}
1243

1244 1245 1246 1247 1248
int mv88e6xxx_port_set_map_da(struct mv88e6xxx_chip *chip, int port)
{
	u16 reg;
	int err;

1249
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
1250 1251 1252
	if (err)
		return err;

1253
	reg |= MV88E6XXX_PORT_CTL2_MAP_DA;
1254

1255
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
1256 1257
}

1258 1259
int mv88e6165_port_set_jumbo_size(struct mv88e6xxx_chip *chip, int port,
				  size_t size)
1260 1261 1262 1263
{
	u16 reg;
	int err;

1264
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
1265 1266 1267
	if (err)
		return err;

1268
	reg &= ~MV88E6XXX_PORT_CTL2_JUMBO_MODE_MASK;
1269 1270

	if (size <= 1522)
1271
		reg |= MV88E6XXX_PORT_CTL2_JUMBO_MODE_1522;
1272
	else if (size <= 2048)
1273
		reg |= MV88E6XXX_PORT_CTL2_JUMBO_MODE_2048;
1274
	else if (size <= 10240)
1275
		reg |= MV88E6XXX_PORT_CTL2_JUMBO_MODE_10240;
1276 1277
	else
		return -ERANGE;
1278

1279
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
1280 1281
}

1282 1283 1284 1285
/* Offset 0x09: Port Rate Control */

int mv88e6095_port_egress_rate_limiting(struct mv88e6xxx_chip *chip, int port)
{
1286 1287
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_EGRESS_RATE_CTL1,
				    0x0000);
1288 1289 1290 1291
}

int mv88e6097_port_egress_rate_limiting(struct mv88e6xxx_chip *chip, int port)
{
1292 1293
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_EGRESS_RATE_CTL1,
				    0x0001);
1294 1295
}

1296 1297 1298 1299
/* Offset 0x0C: Port ATU Control */

int mv88e6xxx_port_disable_learn_limit(struct mv88e6xxx_chip *chip, int port)
{
1300
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_ATU_CTL, 0);
1301 1302
}

1303 1304 1305 1306
/* Offset 0x0D: (Priority) Override Register */

int mv88e6xxx_port_disable_pri_override(struct mv88e6xxx_chip *chip, int port)
{
1307
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_PRI_OVERRIDE, 0);
1308 1309
}

1310 1311 1312 1313 1314
/* Offset 0x0f: Port Ether type */

int mv88e6351_port_set_ether_type(struct mv88e6xxx_chip *chip, int port,
				  u16 etype)
{
1315
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_ETH_TYPE, etype);
1316 1317
}

1318 1319 1320 1321 1322 1323 1324 1325 1326
/* 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 */
1327 1328 1329
	err = mv88e6xxx_port_write(chip, port,
				   MV88E6095_PORT_IEEE_PRIO_REMAP_0123,
				   0x3210);
1330 1331 1332
	if (err)
		return err;

1333 1334 1335
	return mv88e6xxx_port_write(chip, port,
				    MV88E6095_PORT_IEEE_PRIO_REMAP_4567,
				    0x7654);
1336 1337 1338
}

static int mv88e6xxx_port_ieeepmt_write(struct mv88e6xxx_chip *chip,
1339
					int port, u16 table, u8 ptr, u16 data)
1340 1341 1342
{
	u16 reg;

1343 1344 1345
	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);
1346

1347 1348
	return mv88e6xxx_port_write(chip, port,
				    MV88E6390_PORT_IEEE_PRIO_MAP_TABLE, reg);
1349 1350 1351 1352 1353
}

int mv88e6390_port_tag_remap(struct mv88e6xxx_chip *chip, int port)
{
	int err, i;
1354
	u16 table;
1355 1356

	for (i = 0; i <= 7; i++) {
1357 1358 1359
		table = MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_INGRESS_PCP;
		err = mv88e6xxx_port_ieeepmt_write(chip, port, table, i,
						   (i | i << 4));
1360 1361 1362
		if (err)
			return err;

1363 1364
		table = MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_EGRESS_GREEN_PCP;
		err = mv88e6xxx_port_ieeepmt_write(chip, port, table, i, i);
1365 1366 1367
		if (err)
			return err;

1368 1369
		table = MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_EGRESS_YELLOW_PCP;
		err = mv88e6xxx_port_ieeepmt_write(chip, port, table, i, i);
1370 1371 1372
		if (err)
			return err;

1373 1374
		table = MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_EGRESS_AVB_PCP;
		err = mv88e6xxx_port_ieeepmt_write(chip, port, table, i, i);
1375 1376 1377 1378 1379 1380
		if (err)
			return err;
	}

	return 0;
}
1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454

/* Offset 0x0E: Policy Control Register */

int mv88e6352_port_set_policy(struct mv88e6xxx_chip *chip, int port,
			      enum mv88e6xxx_policy_mapping mapping,
			      enum mv88e6xxx_policy_action action)
{
	u16 reg, mask, val;
	int shift;
	int err;

	switch (mapping) {
	case MV88E6XXX_POLICY_MAPPING_DA:
		shift = __bf_shf(MV88E6XXX_PORT_POLICY_CTL_DA_MASK);
		mask = MV88E6XXX_PORT_POLICY_CTL_DA_MASK;
		break;
	case MV88E6XXX_POLICY_MAPPING_SA:
		shift = __bf_shf(MV88E6XXX_PORT_POLICY_CTL_SA_MASK);
		mask = MV88E6XXX_PORT_POLICY_CTL_SA_MASK;
		break;
	case MV88E6XXX_POLICY_MAPPING_VTU:
		shift = __bf_shf(MV88E6XXX_PORT_POLICY_CTL_VTU_MASK);
		mask = MV88E6XXX_PORT_POLICY_CTL_VTU_MASK;
		break;
	case MV88E6XXX_POLICY_MAPPING_ETYPE:
		shift = __bf_shf(MV88E6XXX_PORT_POLICY_CTL_ETYPE_MASK);
		mask = MV88E6XXX_PORT_POLICY_CTL_ETYPE_MASK;
		break;
	case MV88E6XXX_POLICY_MAPPING_PPPOE:
		shift = __bf_shf(MV88E6XXX_PORT_POLICY_CTL_PPPOE_MASK);
		mask = MV88E6XXX_PORT_POLICY_CTL_PPPOE_MASK;
		break;
	case MV88E6XXX_POLICY_MAPPING_VBAS:
		shift = __bf_shf(MV88E6XXX_PORT_POLICY_CTL_VBAS_MASK);
		mask = MV88E6XXX_PORT_POLICY_CTL_VBAS_MASK;
		break;
	case MV88E6XXX_POLICY_MAPPING_OPT82:
		shift = __bf_shf(MV88E6XXX_PORT_POLICY_CTL_OPT82_MASK);
		mask = MV88E6XXX_PORT_POLICY_CTL_OPT82_MASK;
		break;
	case MV88E6XXX_POLICY_MAPPING_UDP:
		shift = __bf_shf(MV88E6XXX_PORT_POLICY_CTL_UDP_MASK);
		mask = MV88E6XXX_PORT_POLICY_CTL_UDP_MASK;
		break;
	default:
		return -EOPNOTSUPP;
	}

	switch (action) {
	case MV88E6XXX_POLICY_ACTION_NORMAL:
		val = MV88E6XXX_PORT_POLICY_CTL_NORMAL;
		break;
	case MV88E6XXX_POLICY_ACTION_MIRROR:
		val = MV88E6XXX_PORT_POLICY_CTL_MIRROR;
		break;
	case MV88E6XXX_POLICY_ACTION_TRAP:
		val = MV88E6XXX_PORT_POLICY_CTL_TRAP;
		break;
	case MV88E6XXX_POLICY_ACTION_DISCARD:
		val = MV88E6XXX_PORT_POLICY_CTL_DISCARD;
		break;
	default:
		return -EOPNOTSUPP;
	}

	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_POLICY_CTL, &reg);
	if (err)
		return err;

	reg &= ~mask;
	reg |= (val << shift) & mask;

	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_POLICY_CTL, reg);
}