port.c 21.3 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/if_bridge.h>
16
#include <linux/phy.h>
17 18

#include "chip.h"
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#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);
}
36

37 38 39
/* Offset 0x01: MAC (or PCS or Physical) Control Register
 *
 * Link, Duplex and Flow Control have one force bit, one value bit.
40 41 42 43
 *
 * 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.
44 45
 */

46 47 48 49 50 51
static int mv88e6xxx_port_set_rgmii_delay(struct mv88e6xxx_chip *chip, int port,
					  phy_interface_t mode)
{
	u16 reg;
	int err;

52
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_MAC_CTL, &reg);
53 54 55
	if (err)
		return err;

56 57
	reg &= ~(MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_RXCLK |
		 MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_TXCLK);
58 59 60

	switch (mode) {
	case PHY_INTERFACE_MODE_RGMII_RXID:
61
		reg |= MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_RXCLK;
62 63
		break;
	case PHY_INTERFACE_MODE_RGMII_TXID:
64
		reg |= MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_TXCLK;
65 66
		break;
	case PHY_INTERFACE_MODE_RGMII_ID:
67 68
		reg |= MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_RXCLK |
			MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_TXCLK;
69
		break;
70
	case PHY_INTERFACE_MODE_RGMII:
71
		break;
72 73
	default:
		return 0;
74 75
	}

76
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_MAC_CTL, reg);
77 78 79
	if (err)
		return err;

80
	dev_dbg(chip->dev, "p%d: delay RXCLK %s, TXCLK %s\n", port,
81 82
		reg & MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_RXCLK ? "yes" : "no",
		reg & MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_TXCLK ? "yes" : "no");
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

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

105 106 107 108 109
int mv88e6xxx_port_set_link(struct mv88e6xxx_chip *chip, int port, int link)
{
	u16 reg;
	int err;

110
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_MAC_CTL, &reg);
111 112 113
	if (err)
		return err;

114 115
	reg &= ~(MV88E6XXX_PORT_MAC_CTL_FORCE_LINK |
		 MV88E6XXX_PORT_MAC_CTL_LINK_UP);
116 117 118

	switch (link) {
	case LINK_FORCED_DOWN:
119
		reg |= MV88E6XXX_PORT_MAC_CTL_FORCE_LINK;
120 121
		break;
	case LINK_FORCED_UP:
122 123
		reg |= MV88E6XXX_PORT_MAC_CTL_FORCE_LINK |
			MV88E6XXX_PORT_MAC_CTL_LINK_UP;
124 125 126 127 128 129 130 131
		break;
	case LINK_UNFORCED:
		/* normal link detection */
		break;
	default:
		return -EINVAL;
	}

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

136
	dev_dbg(chip->dev, "p%d: %s link %s\n", port,
137 138
		reg & MV88E6XXX_PORT_MAC_CTL_FORCE_LINK ? "Force" : "Unforce",
		reg & MV88E6XXX_PORT_MAC_CTL_LINK_UP ? "up" : "down");
139 140 141 142

	return 0;
}

143 144 145 146 147
int mv88e6xxx_port_set_duplex(struct mv88e6xxx_chip *chip, int port, int dup)
{
	u16 reg;
	int err;

148
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_MAC_CTL, &reg);
149 150 151
	if (err)
		return err;

152 153
	reg &= ~(MV88E6XXX_PORT_MAC_CTL_FORCE_DUPLEX |
		 MV88E6XXX_PORT_MAC_CTL_DUPLEX_FULL);
154 155 156

	switch (dup) {
	case DUPLEX_HALF:
157
		reg |= MV88E6XXX_PORT_MAC_CTL_FORCE_DUPLEX;
158 159
		break;
	case DUPLEX_FULL:
160 161
		reg |= MV88E6XXX_PORT_MAC_CTL_FORCE_DUPLEX |
			MV88E6XXX_PORT_MAC_CTL_DUPLEX_FULL;
162 163 164 165 166 167 168 169
		break;
	case DUPLEX_UNFORCED:
		/* normal duplex detection */
		break;
	default:
		return -EINVAL;
	}

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

174
	dev_dbg(chip->dev, "p%d: %s %s duplex\n", port,
175 176
		reg & MV88E6XXX_PORT_MAC_CTL_FORCE_DUPLEX ? "Force" : "Unforce",
		reg & MV88E6XXX_PORT_MAC_CTL_DUPLEX_FULL ? "full" : "half");
177 178 179 180

	return 0;
}

181 182 183 184 185 186 187 188
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:
189
		ctrl = MV88E6XXX_PORT_MAC_CTL_SPEED_10;
190 191
		break;
	case 100:
192
		ctrl = MV88E6XXX_PORT_MAC_CTL_SPEED_100;
193 194 195
		break;
	case 200:
		if (alt_bit)
196 197
			ctrl = MV88E6XXX_PORT_MAC_CTL_SPEED_100 |
				MV88E6390_PORT_MAC_CTL_ALTSPEED;
198
		else
199
			ctrl = MV88E6065_PORT_MAC_CTL_SPEED_200;
200 201
		break;
	case 1000:
202
		ctrl = MV88E6XXX_PORT_MAC_CTL_SPEED_1000;
203 204
		break;
	case 2500:
205 206
		ctrl = MV88E6390_PORT_MAC_CTL_SPEED_10000 |
			MV88E6390_PORT_MAC_CTL_ALTSPEED;
207 208 209 210
		break;
	case 10000:
		/* all bits set, fall through... */
	case SPEED_UNFORCED:
211
		ctrl = MV88E6XXX_PORT_MAC_CTL_SPEED_UNFORCED;
212 213 214 215 216
		break;
	default:
		return -EOPNOTSUPP;
	}

217
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_MAC_CTL, &reg);
218 219 220
	if (err)
		return err;

221
	reg &= ~MV88E6XXX_PORT_MAC_CTL_SPEED_MASK;
222
	if (alt_bit)
223
		reg &= ~MV88E6390_PORT_MAC_CTL_ALTSPEED;
224
	if (force_bit) {
225
		reg &= ~MV88E6390_PORT_MAC_CTL_FORCE_SPEED;
226
		if (speed != SPEED_UNFORCED)
227
			ctrl |= MV88E6390_PORT_MAC_CTL_FORCE_SPEED;
228 229 230
	}
	reg |= ctrl;

231
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_MAC_CTL, reg);
232 233 234 235
	if (err)
		return err;

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

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

316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
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:
331
		cmode = MV88E6XXX_PORT_STS_CMODE_1000BASE_X;
332 333
		break;
	case PHY_INTERFACE_MODE_SGMII:
334
		cmode = MV88E6XXX_PORT_STS_CMODE_SGMII;
335 336
		break;
	case PHY_INTERFACE_MODE_2500BASEX:
337
		cmode = MV88E6XXX_PORT_STS_CMODE_2500BASEX;
338 339
		break;
	case PHY_INTERFACE_MODE_XGMII:
340
		cmode = MV88E6XXX_PORT_STS_CMODE_XAUI;
341 342
		break;
	case PHY_INTERFACE_MODE_RXAUI:
343
		cmode = MV88E6XXX_PORT_STS_CMODE_RXAUI;
344 345 346 347 348 349
		break;
	default:
		cmode = 0;
	}

	if (cmode) {
350
		err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_STS, &reg);
351 352 353
		if (err)
			return err;

354
		reg &= ~MV88E6XXX_PORT_STS_CMODE_MASK;
355 356
		reg |= cmode;

357
		err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_STS, reg);
358 359 360 361 362 363 364 365 366 367 368 369
		if (err)
			return err;
	}

	return 0;
}

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

370
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_STS, &reg);
371 372 373
	if (err)
		return err;

374
	*cmode = reg & MV88E6XXX_PORT_STS_CMODE_MASK;
375 376 377 378

	return 0;
}

379
/* Offset 0x02: Jamming Control
380 381 382 383 384
 *
 * 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.
 */
385 386
int mv88e6097_port_pause_limit(struct mv88e6xxx_chip *chip, int port, u8 in,
			       u8 out)
387
{
388 389
	return mv88e6xxx_port_write(chip, port, MV88E6097_PORT_JAM_CTL,
				    out << 8 | in);
390 391
}

392 393
int mv88e6390_port_pause_limit(struct mv88e6xxx_chip *chip, int port, u8 in,
			       u8 out)
394 395 396
{
	int err;

397 398 399
	err = mv88e6xxx_port_write(chip, port, MV88E6390_PORT_FLOW_CTL,
				   MV88E6390_PORT_FLOW_CTL_UPDATE |
				   MV88E6390_PORT_FLOW_CTL_LIMIT_IN | in);
400 401 402
	if (err)
		return err;

403 404 405
	return mv88e6xxx_port_write(chip, port, MV88E6390_PORT_FLOW_CTL,
				    MV88E6390_PORT_FLOW_CTL_UPDATE |
				    MV88E6390_PORT_FLOW_CTL_LIMIT_OUT | out);
406 407
}

408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
/* Offset 0x04: Port Control Register */

static const char * const mv88e6xxx_port_state_names[] = {
	[PORT_CONTROL_STATE_DISABLED] = "Disabled",
	[PORT_CONTROL_STATE_BLOCKING] = "Blocking/Listening",
	[PORT_CONTROL_STATE_LEARNING] = "Learning",
	[PORT_CONTROL_STATE_FORWARDING] = "Forwarding",
};

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

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

	reg &= ~PORT_CONTROL_STATE_MASK;
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445

	switch (state) {
	case BR_STATE_DISABLED:
		state = PORT_CONTROL_STATE_DISABLED;
		break;
	case BR_STATE_BLOCKING:
	case BR_STATE_LISTENING:
		state = PORT_CONTROL_STATE_BLOCKING;
		break;
	case BR_STATE_LEARNING:
		state = PORT_CONTROL_STATE_LEARNING;
		break;
	case BR_STATE_FORWARDING:
		state = PORT_CONTROL_STATE_FORWARDING;
		break;
	default:
		return -EINVAL;
	}

446 447 448 449 450 451
	reg |= state;

	err = mv88e6xxx_port_write(chip, port, PORT_CONTROL, reg);
	if (err)
		return err;

452 453
	dev_dbg(chip->dev, "p%d: PortState set to %s\n", port,
		mv88e6xxx_port_state_names[state]);
454 455 456

	return 0;
}
457

458
int mv88e6xxx_port_set_egress_mode(struct mv88e6xxx_chip *chip, int port,
459
				   enum mv88e6xxx_egress_mode mode)
460 461 462 463 464 465 466 467 468
{
	int err;
	u16 reg;

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

	reg &= ~PORT_CONTROL_EGRESS_MASK;
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485

	switch (mode) {
	case MV88E6XXX_EGRESS_MODE_UNMODIFIED:
		reg |= PORT_CONTROL_EGRESS_UNMODIFIED;
		break;
	case MV88E6XXX_EGRESS_MODE_UNTAGGED:
		reg |= PORT_CONTROL_EGRESS_UNTAGGED;
		break;
	case MV88E6XXX_EGRESS_MODE_TAGGED:
		reg |= PORT_CONTROL_EGRESS_TAGGED;
		break;
	case MV88E6XXX_EGRESS_MODE_ETHERTYPE:
		reg |= PORT_CONTROL_EGRESS_ADD_TAG;
		break;
	default:
		return -EINVAL;
	}
486 487 488 489 490 491 492 493 494 495 496 497 498 499

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

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

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

500
	reg &= ~PORT_CONTROL_FRAME_MASK;
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

	switch (mode) {
	case MV88E6XXX_FRAME_MODE_NORMAL:
		reg |= PORT_CONTROL_FRAME_MODE_NORMAL;
		break;
	case MV88E6XXX_FRAME_MODE_DSA:
		reg |= PORT_CONTROL_FRAME_MODE_DSA;
		break;
	default:
		return -EINVAL;
	}

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

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

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

	reg &= ~PORT_CONTROL_FRAME_MASK;

	switch (mode) {
	case MV88E6XXX_FRAME_MODE_NORMAL:
		reg |= PORT_CONTROL_FRAME_MODE_NORMAL;
		break;
	case MV88E6XXX_FRAME_MODE_DSA:
		reg |= PORT_CONTROL_FRAME_MODE_DSA;
		break;
	case MV88E6XXX_FRAME_MODE_PROVIDER:
		reg |= PORT_CONTROL_FRAME_MODE_PROVIDER;
		break;
	case MV88E6XXX_FRAME_MODE_ETHERTYPE:
		reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA;
		break;
	default:
		return -EINVAL;
	}

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

548 549
static int mv88e6185_port_set_forward_unknown(struct mv88e6xxx_chip *chip,
					      int port, bool unicast)
550 551 552 553 554 555 556 557
{
	int err;
	u16 reg;

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

558
	if (unicast)
559 560 561 562 563 564 565
		reg |= PORT_CONTROL_FORWARD_UNKNOWN;
	else
		reg &= ~PORT_CONTROL_FORWARD_UNKNOWN;

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

566 567
int mv88e6352_port_set_egress_floods(struct mv88e6xxx_chip *chip, int port,
				     bool unicast, bool multicast)
568 569 570 571 572 573 574 575
{
	int err;
	u16 reg;

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

576 577 578 579 580 581 582 583
	reg &= ~PORT_CONTROL_EGRESS_FLOODS_MASK;

	if (unicast && multicast)
		reg |= PORT_CONTROL_EGRESS_FLOODS_ALL_UNKNOWN_DA;
	else if (unicast)
		reg |= PORT_CONTROL_EGRESS_FLOODS_NO_UNKNOWN_MC_DA;
	else if (multicast)
		reg |= PORT_CONTROL_EGRESS_FLOODS_NO_UNKNOWN_UC_DA;
584
	else
585
		reg |= PORT_CONTROL_EGRESS_FLOODS_NO_UNKNOWN_DA;
586 587 588 589

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

590 591
/* Offset 0x05: Port Control 1 */

592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
int mv88e6xxx_port_set_message_port(struct mv88e6xxx_chip *chip, int port,
				    bool message_port)
{
	u16 val;
	int err;

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

	if (message_port)
		val |= PORT_CONTROL_1_MESSAGE_PORT;
	else
		val &= ~PORT_CONTROL_1_MESSAGE_PORT;

	return mv88e6xxx_port_write(chip, port, PORT_CONTROL_1, val);
}

610 611 612 613
/* Offset 0x06: Port Based VLAN Map */

int mv88e6xxx_port_set_vlan_map(struct mv88e6xxx_chip *chip, int port, u16 map)
{
614
	const u16 mask = mv88e6xxx_port_mask(chip);
615 616 617 618 619 620 621 622 623 624 625 626 627 628
	u16 reg;
	int err;

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

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

	err = mv88e6xxx_port_write(chip, port, PORT_BASE_VLAN, reg);
	if (err)
		return err;

629
	dev_dbg(chip->dev, "p%d: VLANTable set to %.3x\n", port, map);
630 631 632

	return 0;
}
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693

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 */
	err = mv88e6xxx_port_read(chip, port, PORT_BASE_VLAN, &reg);
	if (err)
		return err;

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

	/* Port's default FID upper bits are located in reg 0x05, offset 0 */
	if (upper_mask) {
		err = mv88e6xxx_port_read(chip, port, PORT_CONTROL_1, &reg);
		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 */
	err = mv88e6xxx_port_read(chip, port, PORT_BASE_VLAN, &reg);
	if (err)
		return err;

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

	err = mv88e6xxx_port_write(chip, port, PORT_BASE_VLAN, reg);
	if (err)
		return err;

	/* Port's default FID upper bits are located in reg 0x05, offset 0 */
	if (upper_mask) {
		err = mv88e6xxx_port_read(chip, port, PORT_CONTROL_1, &reg);
		if (err)
			return err;

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

		err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_1, reg);
		if (err)
			return err;
	}

694
	dev_dbg(chip->dev, "p%d: FID set to %u\n", port, fid);
695 696 697

	return 0;
}
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730

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

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

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

	*pvid = reg & PORT_DEFAULT_VLAN_MASK;

	return 0;
}

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

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

	reg &= ~PORT_DEFAULT_VLAN_MASK;
	reg |= pvid & PORT_DEFAULT_VLAN_MASK;

	err = mv88e6xxx_port_write(chip, port, PORT_DEFAULT_VLAN, reg);
	if (err)
		return err;

731
	dev_dbg(chip->dev, "p%d: DefaultVID set to %u\n", port, pvid);
732 733 734

	return 0;
}
735 736 737 738 739 740 741 742 743 744

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

static const char * const mv88e6xxx_port_8021q_mode_names[] = {
	[PORT_CONTROL_2_8021Q_DISABLED] = "Disabled",
	[PORT_CONTROL_2_8021Q_FALLBACK] = "Fallback",
	[PORT_CONTROL_2_8021Q_CHECK] = "Check",
	[PORT_CONTROL_2_8021Q_SECURE] = "Secure",
};

745 746
static int mv88e6185_port_set_default_forward(struct mv88e6xxx_chip *chip,
					      int port, bool multicast)
747 748 749 750 751 752 753 754
{
	int err;
	u16 reg;

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

755 756
	if (multicast)
		reg |= PORT_CONTROL_2_DEFAULT_FORWARD;
757
	else
758
		reg &= ~PORT_CONTROL_2_DEFAULT_FORWARD;
759 760 761 762

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

763 764 765 766 767 768 769 770 771 772 773 774
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);
}

775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
int mv88e6095_port_set_upstream_port(struct mv88e6xxx_chip *chip, int port,
				     int upstream_port)
{
	int err;
	u16 reg;

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

	reg &= ~PORT_CONTROL_2_UPSTREAM_MASK;
	reg |= upstream_port;

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

791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
int mv88e6xxx_port_set_8021q_mode(struct mv88e6xxx_chip *chip, int port,
				  u16 mode)
{
	u16 reg;
	int err;

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

	reg &= ~PORT_CONTROL_2_8021Q_MASK;
	reg |= mode & PORT_CONTROL_2_8021Q_MASK;

	err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_2, reg);
	if (err)
		return err;

808 809
	dev_dbg(chip->dev, "p%d: 802.1QMode set to %s\n", port,
		mv88e6xxx_port_8021q_mode_names[mode]);
810 811 812

	return 0;
}
813

814 815 816 817 818 819 820 821 822 823 824 825 826 827
int mv88e6xxx_port_set_map_da(struct mv88e6xxx_chip *chip, int port)
{
	u16 reg;
	int err;

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

	reg |= PORT_CONTROL_2_MAP_DA;

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

828 829
int mv88e6165_port_set_jumbo_size(struct mv88e6xxx_chip *chip, int port,
				  size_t size)
830 831 832 833 834 835 836 837
{
	u16 reg;
	int err;

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

838 839 840 841 842 843 844 845 846 847
	reg &= ~PORT_CONTROL_2_JUMBO_MASK;

	if (size <= 1522)
		reg |= PORT_CONTROL_2_JUMBO_1522;
	else if (size <= 2048)
		reg |= PORT_CONTROL_2_JUMBO_2048;
	else if (size <= 10240)
		reg |= PORT_CONTROL_2_JUMBO_10240;
	else
		return -ERANGE;
848 849 850 851

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

852 853 854 855 856 857 858 859 860 861 862 863
/* Offset 0x09: Port Rate Control */

int mv88e6095_port_egress_rate_limiting(struct mv88e6xxx_chip *chip, int port)
{
	return mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL, 0x0000);
}

int mv88e6097_port_egress_rate_limiting(struct mv88e6xxx_chip *chip, int port)
{
	return mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL, 0x0001);
}

864 865 866 867 868 869 870
/* Offset 0x0C: Port ATU Control */

int mv88e6xxx_port_disable_learn_limit(struct mv88e6xxx_chip *chip, int port)
{
	return mv88e6xxx_port_write(chip, port, PORT_ATU_CONTROL, 0);
}

871 872 873 874 875 876 877
/* Offset 0x0D: (Priority) Override Register */

int mv88e6xxx_port_disable_pri_override(struct mv88e6xxx_chip *chip, int port)
{
	return mv88e6xxx_port_write(chip, port, PORT_PRI_OVERRIDE, 0);
}

878 879 880 881 882 883 884 885
/* Offset 0x0f: Port Ether type */

int mv88e6351_port_set_ether_type(struct mv88e6xxx_chip *chip, int port,
				  u16 etype)
{
	return mv88e6xxx_port_write(chip, port, PORT_ETH_TYPE, etype);
}

886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
/* 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 */
	err = mv88e6xxx_port_write(chip, port, PORT_TAG_REGMAP_0123, 0x3210);
	if (err)
		return err;

	return mv88e6xxx_port_write(chip, port, PORT_TAG_REGMAP_4567, 0x7654);
}

static int mv88e6xxx_port_ieeepmt_write(struct mv88e6xxx_chip *chip,
					int port, u16 table,
					u8 pointer, u16 data)
{
	u16 reg;

	reg = PORT_IEEE_PRIO_MAP_TABLE_UPDATE |
		table |
		(pointer << PORT_IEEE_PRIO_MAP_TABLE_POINTER_SHIFT) |
		data;

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

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

	for (i = 0; i <= 7; i++) {
		err = mv88e6xxx_port_ieeepmt_write(
			chip, port, PORT_IEEE_PRIO_MAP_TABLE_INGRESS_PCP,
			i, (i | i << 4));
		if (err)
			return err;

		err = mv88e6xxx_port_ieeepmt_write(
			chip, port, PORT_IEEE_PRIO_MAP_TABLE_EGRESS_GREEN_PCP,
			i, i);
		if (err)
			return err;

		err = mv88e6xxx_port_ieeepmt_write(
			chip, port, PORT_IEEE_PRIO_MAP_TABLE_EGRESS_YELLOW_PCP,
			i, i);
		if (err)
			return err;

		err = mv88e6xxx_port_ieeepmt_write(
			chip, port, PORT_IEEE_PRIO_MAP_TABLE_EGRESS_AVB_PCP,
			i, i);
		if (err)
			return err;
	}

	return 0;
}