port.c 23.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/bitfield.h>
16
#include <linux/if_bridge.h>
17
#include <linux/phy.h>
18
#include <linux/phylink.h>
19 20

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

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

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

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

58 59
	reg &= ~(MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_RXCLK |
		 MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_TXCLK);
60 61 62

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

78
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_MAC_CTL, reg);
79 80 81
	if (err)
		return err;

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

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

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

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

116 117
	reg &= ~(MV88E6XXX_PORT_MAC_CTL_FORCE_LINK |
		 MV88E6XXX_PORT_MAC_CTL_LINK_UP);
118 119 120

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

134
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_MAC_CTL, reg);
135 136 137
	if (err)
		return err;

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

	return 0;
}

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

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

154 155
	reg &= ~(MV88E6XXX_PORT_MAC_CTL_FORCE_DUPLEX |
		 MV88E6XXX_PORT_MAC_CTL_DUPLEX_FULL);
156 157 158

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

172
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_MAC_CTL, reg);
173 174 175
	if (err)
		return err;

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

	return 0;
}

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

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

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

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

	if (speed)
238
		dev_dbg(chip->dev, "p%d: Speed set to %d Mbps\n", port, speed);
239
	else
240
		dev_dbg(chip->dev, "p%d: Speed unforced\n", port);
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317

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

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

	if (cmode) {
353
		err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_STS, &reg);
354 355 356
		if (err)
			return err;

357
		reg &= ~MV88E6XXX_PORT_STS_CMODE_MASK;
358 359
		reg |= cmode;

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

	return 0;
}

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

373
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_STS, &reg);
374 375 376
	if (err)
		return err;

377
	*cmode = reg & MV88E6XXX_PORT_STS_CMODE_MASK;
378 379 380 381

	return 0;
}

382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
int mv88e6xxx_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;

	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:
		if ((reg &MV88E6XXX_PORT_STS_CMODE_MASK) ==
		    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;
}

420
/* Offset 0x02: Jamming Control
421 422 423 424 425
 *
 * 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.
 */
426 427
int mv88e6097_port_pause_limit(struct mv88e6xxx_chip *chip, int port, u8 in,
			       u8 out)
428
{
429 430
	return mv88e6xxx_port_write(chip, port, MV88E6097_PORT_JAM_CTL,
				    out << 8 | in);
431 432
}

433 434
int mv88e6390_port_pause_limit(struct mv88e6xxx_chip *chip, int port, u8 in,
			       u8 out)
435 436 437
{
	int err;

438 439 440
	err = mv88e6xxx_port_write(chip, port, MV88E6390_PORT_FLOW_CTL,
				   MV88E6390_PORT_FLOW_CTL_UPDATE |
				   MV88E6390_PORT_FLOW_CTL_LIMIT_IN | in);
441 442 443
	if (err)
		return err;

444 445 446
	return mv88e6xxx_port_write(chip, port, MV88E6390_PORT_FLOW_CTL,
				    MV88E6390_PORT_FLOW_CTL_UPDATE |
				    MV88E6390_PORT_FLOW_CTL_LIMIT_OUT | out);
447 448
}

449 450 451
/* Offset 0x04: Port Control Register */

static const char * const mv88e6xxx_port_state_names[] = {
452 453 454 455
	[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",
456 457 458 459 460 461 462
};

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

463
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
464 465 466
	if (err)
		return err;

467
	reg &= ~MV88E6XXX_PORT_CTL0_STATE_MASK;
468 469 470

	switch (state) {
	case BR_STATE_DISABLED:
471
		state = MV88E6XXX_PORT_CTL0_STATE_DISABLED;
472 473 474
		break;
	case BR_STATE_BLOCKING:
	case BR_STATE_LISTENING:
475
		state = MV88E6XXX_PORT_CTL0_STATE_BLOCKING;
476 477
		break;
	case BR_STATE_LEARNING:
478
		state = MV88E6XXX_PORT_CTL0_STATE_LEARNING;
479 480
		break;
	case BR_STATE_FORWARDING:
481
		state = MV88E6XXX_PORT_CTL0_STATE_FORWARDING;
482 483 484 485 486
		break;
	default:
		return -EINVAL;
	}

487 488
	reg |= state;

489
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
490 491 492
	if (err)
		return err;

493 494
	dev_dbg(chip->dev, "p%d: PortState set to %s\n", port,
		mv88e6xxx_port_state_names[state]);
495 496 497

	return 0;
}
498

499
int mv88e6xxx_port_set_egress_mode(struct mv88e6xxx_chip *chip, int port,
500
				   enum mv88e6xxx_egress_mode mode)
501 502 503 504
{
	int err;
	u16 reg;

505
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
506 507 508
	if (err)
		return err;

509
	reg &= ~MV88E6XXX_PORT_CTL0_EGRESS_MODE_MASK;
510 511 512

	switch (mode) {
	case MV88E6XXX_EGRESS_MODE_UNMODIFIED:
513
		reg |= MV88E6XXX_PORT_CTL0_EGRESS_MODE_UNMODIFIED;
514 515
		break;
	case MV88E6XXX_EGRESS_MODE_UNTAGGED:
516
		reg |= MV88E6XXX_PORT_CTL0_EGRESS_MODE_UNTAGGED;
517 518
		break;
	case MV88E6XXX_EGRESS_MODE_TAGGED:
519
		reg |= MV88E6XXX_PORT_CTL0_EGRESS_MODE_TAGGED;
520 521
		break;
	case MV88E6XXX_EGRESS_MODE_ETHERTYPE:
522
		reg |= MV88E6XXX_PORT_CTL0_EGRESS_MODE_ETHER_TYPE_DSA;
523 524 525 526
		break;
	default:
		return -EINVAL;
	}
527

528
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
529 530 531 532 533 534 535 536
}

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

537
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
538 539 540
	if (err)
		return err;

541
	reg &= ~MV88E6XXX_PORT_CTL0_FRAME_MODE_MASK;
542 543 544

	switch (mode) {
	case MV88E6XXX_FRAME_MODE_NORMAL:
545
		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_NORMAL;
546 547
		break;
	case MV88E6XXX_FRAME_MODE_DSA:
548
		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_DSA;
549 550 551 552 553
		break;
	default:
		return -EINVAL;
	}

554
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
555 556 557 558 559 560 561 562
}

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

563
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
564 565 566
	if (err)
		return err;

567
	reg &= ~MV88E6XXX_PORT_CTL0_FRAME_MODE_MASK;
568 569 570

	switch (mode) {
	case MV88E6XXX_FRAME_MODE_NORMAL:
571
		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_NORMAL;
572 573
		break;
	case MV88E6XXX_FRAME_MODE_DSA:
574
		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_DSA;
575 576
		break;
	case MV88E6XXX_FRAME_MODE_PROVIDER:
577
		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_PROVIDER;
578 579
		break;
	case MV88E6XXX_FRAME_MODE_ETHERTYPE:
580
		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_ETHER_TYPE_DSA;
581 582 583 584 585
		break;
	default:
		return -EINVAL;
	}

586
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
587 588
}

589 590
static int mv88e6185_port_set_forward_unknown(struct mv88e6xxx_chip *chip,
					      int port, bool unicast)
591 592 593 594
{
	int err;
	u16 reg;

595
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
596 597 598
	if (err)
		return err;

599
	if (unicast)
600
		reg |= MV88E6185_PORT_CTL0_FORWARD_UNKNOWN;
601
	else
602
		reg &= ~MV88E6185_PORT_CTL0_FORWARD_UNKNOWN;
603

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

607 608
int mv88e6352_port_set_egress_floods(struct mv88e6xxx_chip *chip, int port,
				     bool unicast, bool multicast)
609 610 611 612
{
	int err;
	u16 reg;

613
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
614 615 616
	if (err)
		return err;

617
	reg &= ~MV88E6352_PORT_CTL0_EGRESS_FLOODS_MASK;
618 619

	if (unicast && multicast)
620
		reg |= MV88E6352_PORT_CTL0_EGRESS_FLOODS_ALL_UNKNOWN_DA;
621
	else if (unicast)
622
		reg |= MV88E6352_PORT_CTL0_EGRESS_FLOODS_NO_UNKNOWN_MC_DA;
623
	else if (multicast)
624
		reg |= MV88E6352_PORT_CTL0_EGRESS_FLOODS_NO_UNKNOWN_UC_DA;
625
	else
626
		reg |= MV88E6352_PORT_CTL0_EGRESS_FLOODS_NO_UNKNOWN_DA;
627

628
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
629 630
}

631 632
/* Offset 0x05: Port Control 1 */

633 634 635 636 637 638
int mv88e6xxx_port_set_message_port(struct mv88e6xxx_chip *chip, int port,
				    bool message_port)
{
	u16 val;
	int err;

639
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL1, &val);
640 641 642 643
	if (err)
		return err;

	if (message_port)
644
		val |= MV88E6XXX_PORT_CTL1_MESSAGE_PORT;
645
	else
646
		val &= ~MV88E6XXX_PORT_CTL1_MESSAGE_PORT;
647

648
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL1, val);
649 650
}

651 652 653 654
/* Offset 0x06: Port Based VLAN Map */

int mv88e6xxx_port_set_vlan_map(struct mv88e6xxx_chip *chip, int port, u16 map)
{
655
	const u16 mask = mv88e6xxx_port_mask(chip);
656 657 658
	u16 reg;
	int err;

659
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_BASE_VLAN, &reg);
660 661 662 663 664 665
	if (err)
		return err;

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

666
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_BASE_VLAN, reg);
667 668 669
	if (err)
		return err;

670
	dev_dbg(chip->dev, "p%d: VLANTable set to %.3x\n", port, map);
671 672 673

	return 0;
}
674 675 676 677 678 679 680 681

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 */
682
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_BASE_VLAN, &reg);
683 684 685 686 687 688 689
	if (err)
		return err;

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

	/* Port's default FID upper bits are located in reg 0x05, offset 0 */
	if (upper_mask) {
690 691
		err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL1,
					  &reg);
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
		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 */
711
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_BASE_VLAN, &reg);
712 713 714 715 716 717
	if (err)
		return err;

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

718
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_BASE_VLAN, reg);
719 720 721 722 723
	if (err)
		return err;

	/* Port's default FID upper bits are located in reg 0x05, offset 0 */
	if (upper_mask) {
724 725
		err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL1,
					  &reg);
726 727 728 729 730 731
		if (err)
			return err;

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

732 733
		err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL1,
					   reg);
734 735 736 737
		if (err)
			return err;
	}

738
	dev_dbg(chip->dev, "p%d: FID set to %u\n", port, fid);
739 740 741

	return 0;
}
742 743 744 745 746 747 748 749

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

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

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

755
	*pvid = reg & MV88E6XXX_PORT_DEFAULT_VLAN_MASK;
756 757 758 759 760 761 762 763 764

	return 0;
}

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

765 766
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_DEFAULT_VLAN,
				  &reg);
767 768 769
	if (err)
		return err;

770 771
	reg &= ~MV88E6XXX_PORT_DEFAULT_VLAN_MASK;
	reg |= pvid & MV88E6XXX_PORT_DEFAULT_VLAN_MASK;
772

773 774
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_DEFAULT_VLAN,
				   reg);
775 776 777
	if (err)
		return err;

778
	dev_dbg(chip->dev, "p%d: DefaultVID set to %u\n", port, pvid);
779 780 781

	return 0;
}
782 783 784 785

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

static const char * const mv88e6xxx_port_8021q_mode_names[] = {
786 787 788 789
	[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",
790 791
};

792 793
static int mv88e6185_port_set_default_forward(struct mv88e6xxx_chip *chip,
					      int port, bool multicast)
794 795 796 797
{
	int err;
	u16 reg;

798
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
799 800 801
	if (err)
		return err;

802
	if (multicast)
803
		reg |= MV88E6XXX_PORT_CTL2_DEFAULT_FORWARD;
804
	else
805
		reg &= ~MV88E6XXX_PORT_CTL2_DEFAULT_FORWARD;
806

807
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
808 809
}

810 811 812 813 814 815 816 817 818 819 820 821
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);
}

822 823 824 825 826 827
int mv88e6095_port_set_upstream_port(struct mv88e6xxx_chip *chip, int port,
				     int upstream_port)
{
	int err;
	u16 reg;

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

832
	reg &= ~MV88E6095_PORT_CTL2_CPU_PORT_MASK;
833 834
	reg |= upstream_port;

835
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
836 837
}

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

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

848 849
	reg &= ~MV88E6XXX_PORT_CTL2_8021Q_MODE_MASK;
	reg |= mode & MV88E6XXX_PORT_CTL2_8021Q_MODE_MASK;
850

851
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
852 853 854
	if (err)
		return err;

855 856
	dev_dbg(chip->dev, "p%d: 802.1QMode set to %s\n", port,
		mv88e6xxx_port_8021q_mode_names[mode]);
857 858 859

	return 0;
}
860

861 862 863 864 865
int mv88e6xxx_port_set_map_da(struct mv88e6xxx_chip *chip, int port)
{
	u16 reg;
	int err;

866
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
867 868 869
	if (err)
		return err;

870
	reg |= MV88E6XXX_PORT_CTL2_MAP_DA;
871

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

875 876
int mv88e6165_port_set_jumbo_size(struct mv88e6xxx_chip *chip, int port,
				  size_t size)
877 878 879 880
{
	u16 reg;
	int err;

881
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
882 883 884
	if (err)
		return err;

885
	reg &= ~MV88E6XXX_PORT_CTL2_JUMBO_MODE_MASK;
886 887

	if (size <= 1522)
888
		reg |= MV88E6XXX_PORT_CTL2_JUMBO_MODE_1522;
889
	else if (size <= 2048)
890
		reg |= MV88E6XXX_PORT_CTL2_JUMBO_MODE_2048;
891
	else if (size <= 10240)
892
		reg |= MV88E6XXX_PORT_CTL2_JUMBO_MODE_10240;
893 894
	else
		return -ERANGE;
895

896
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
897 898
}

899 900 901 902
/* Offset 0x09: Port Rate Control */

int mv88e6095_port_egress_rate_limiting(struct mv88e6xxx_chip *chip, int port)
{
903 904
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_EGRESS_RATE_CTL1,
				    0x0000);
905 906 907 908
}

int mv88e6097_port_egress_rate_limiting(struct mv88e6xxx_chip *chip, int port)
{
909 910
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_EGRESS_RATE_CTL1,
				    0x0001);
911 912
}

913 914 915 916
/* Offset 0x0C: Port ATU Control */

int mv88e6xxx_port_disable_learn_limit(struct mv88e6xxx_chip *chip, int port)
{
917
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_ATU_CTL, 0);
918 919
}

920 921 922 923
/* Offset 0x0D: (Priority) Override Register */

int mv88e6xxx_port_disable_pri_override(struct mv88e6xxx_chip *chip, int port)
{
924
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_PRI_OVERRIDE, 0);
925 926
}

927 928 929 930 931
/* Offset 0x0f: Port Ether type */

int mv88e6351_port_set_ether_type(struct mv88e6xxx_chip *chip, int port,
				  u16 etype)
{
932
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_ETH_TYPE, etype);
933 934
}

935 936 937 938 939 940 941 942 943
/* 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 */
944 945 946
	err = mv88e6xxx_port_write(chip, port,
				   MV88E6095_PORT_IEEE_PRIO_REMAP_0123,
				   0x3210);
947 948 949
	if (err)
		return err;

950 951 952
	return mv88e6xxx_port_write(chip, port,
				    MV88E6095_PORT_IEEE_PRIO_REMAP_4567,
				    0x7654);
953 954 955
}

static int mv88e6xxx_port_ieeepmt_write(struct mv88e6xxx_chip *chip,
956
					int port, u16 table, u8 ptr, u16 data)
957 958 959
{
	u16 reg;

960 961 962
	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);
963

964 965
	return mv88e6xxx_port_write(chip, port,
				    MV88E6390_PORT_IEEE_PRIO_MAP_TABLE, reg);
966 967 968 969 970
}

int mv88e6390_port_tag_remap(struct mv88e6xxx_chip *chip, int port)
{
	int err, i;
971
	u16 table;
972 973

	for (i = 0; i <= 7; i++) {
974 975 976
		table = MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_INGRESS_PCP;
		err = mv88e6xxx_port_ieeepmt_write(chip, port, table, i,
						   (i | i << 4));
977 978 979
		if (err)
			return err;

980 981
		table = MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_EGRESS_GREEN_PCP;
		err = mv88e6xxx_port_ieeepmt_write(chip, port, table, i, i);
982 983 984
		if (err)
			return err;

985 986
		table = MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_EGRESS_YELLOW_PCP;
		err = mv88e6xxx_port_ieeepmt_write(chip, port, table, i, i);
987 988 989
		if (err)
			return err;

990 991
		table = MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_EGRESS_AVB_PCP;
		err = mv88e6xxx_port_ieeepmt_write(chip, port, table, i, i);
992 993 994 995 996 997
		if (err)
			return err;
	}

	return 0;
}