port.c 23.8 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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
/* 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);
}

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

71 72 73 74 75 76
static int mv88e6xxx_port_set_rgmii_delay(struct mv88e6xxx_chip *chip, int port,
					  phy_interface_t mode)
{
	u16 reg;
	int err;

77
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_MAC_CTL, &reg);
78 79 80
	if (err)
		return err;

81 82
	reg &= ~(MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_RXCLK |
		 MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_TXCLK);
83 84 85

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

101
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_MAC_CTL, reg);
102 103 104
	if (err)
		return err;

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

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

130 131 132 133 134
int mv88e6xxx_port_set_link(struct mv88e6xxx_chip *chip, int port, int link)
{
	u16 reg;
	int err;

135
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_MAC_CTL, &reg);
136 137 138
	if (err)
		return err;

139 140
	reg &= ~(MV88E6XXX_PORT_MAC_CTL_FORCE_LINK |
		 MV88E6XXX_PORT_MAC_CTL_LINK_UP);
141 142 143

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

157
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_MAC_CTL, reg);
158 159 160
	if (err)
		return err;

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

	return 0;
}

168 169 170 171 172
int mv88e6xxx_port_set_duplex(struct mv88e6xxx_chip *chip, int port, int dup)
{
	u16 reg;
	int err;

173
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_MAC_CTL, &reg);
174 175 176
	if (err)
		return err;

177 178
	reg &= ~(MV88E6XXX_PORT_MAC_CTL_FORCE_DUPLEX |
		 MV88E6XXX_PORT_MAC_CTL_DUPLEX_FULL);
179 180 181

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

195
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_MAC_CTL, reg);
196 197 198
	if (err)
		return err;

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

	return 0;
}

206 207 208 209 210 211 212 213
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:
214
		ctrl = MV88E6XXX_PORT_MAC_CTL_SPEED_10;
215 216
		break;
	case 100:
217
		ctrl = MV88E6XXX_PORT_MAC_CTL_SPEED_100;
218 219 220
		break;
	case 200:
		if (alt_bit)
221 222
			ctrl = MV88E6XXX_PORT_MAC_CTL_SPEED_100 |
				MV88E6390_PORT_MAC_CTL_ALTSPEED;
223
		else
224
			ctrl = MV88E6065_PORT_MAC_CTL_SPEED_200;
225 226
		break;
	case 1000:
227
		ctrl = MV88E6XXX_PORT_MAC_CTL_SPEED_1000;
228 229
		break;
	case 2500:
230 231
		ctrl = MV88E6390_PORT_MAC_CTL_SPEED_10000 |
			MV88E6390_PORT_MAC_CTL_ALTSPEED;
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 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340

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

341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
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:
356
		cmode = MV88E6XXX_PORT_STS_CMODE_1000BASE_X;
357 358
		break;
	case PHY_INTERFACE_MODE_SGMII:
359
		cmode = MV88E6XXX_PORT_STS_CMODE_SGMII;
360 361
		break;
	case PHY_INTERFACE_MODE_2500BASEX:
362
		cmode = MV88E6XXX_PORT_STS_CMODE_2500BASEX;
363 364
		break;
	case PHY_INTERFACE_MODE_XGMII:
365
	case PHY_INTERFACE_MODE_XAUI:
366
		cmode = MV88E6XXX_PORT_STS_CMODE_XAUI;
367 368
		break;
	case PHY_INTERFACE_MODE_RXAUI:
369
		cmode = MV88E6XXX_PORT_STS_CMODE_RXAUI;
370 371 372 373 374 375
		break;
	default:
		cmode = 0;
	}

	if (cmode) {
376
		err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_STS, &reg);
377 378 379
		if (err)
			return err;

380
		reg &= ~MV88E6XXX_PORT_STS_CMODE_MASK;
381 382
		reg |= cmode;

383
		err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_STS, reg);
384 385 386 387 388 389 390 391 392 393 394 395
		if (err)
			return err;
	}

	return 0;
}

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

396
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_STS, &reg);
397 398 399
	if (err)
		return err;

400
	*cmode = reg & MV88E6XXX_PORT_STS_CMODE_MASK;
401 402 403 404

	return 0;
}

405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
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;
}

443
/* Offset 0x02: Jamming Control
444 445 446 447 448
 *
 * 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.
 */
449 450
int mv88e6097_port_pause_limit(struct mv88e6xxx_chip *chip, int port, u8 in,
			       u8 out)
451
{
452 453
	return mv88e6xxx_port_write(chip, port, MV88E6097_PORT_JAM_CTL,
				    out << 8 | in);
454 455
}

456 457
int mv88e6390_port_pause_limit(struct mv88e6xxx_chip *chip, int port, u8 in,
			       u8 out)
458 459 460
{
	int err;

461 462 463
	err = mv88e6xxx_port_write(chip, port, MV88E6390_PORT_FLOW_CTL,
				   MV88E6390_PORT_FLOW_CTL_UPDATE |
				   MV88E6390_PORT_FLOW_CTL_LIMIT_IN | in);
464 465 466
	if (err)
		return err;

467 468 469
	return mv88e6xxx_port_write(chip, port, MV88E6390_PORT_FLOW_CTL,
				    MV88E6390_PORT_FLOW_CTL_UPDATE |
				    MV88E6390_PORT_FLOW_CTL_LIMIT_OUT | out);
470 471
}

472 473 474
/* Offset 0x04: Port Control Register */

static const char * const mv88e6xxx_port_state_names[] = {
475 476 477 478
	[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",
479 480 481 482 483 484 485
};

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

486
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
487 488 489
	if (err)
		return err;

490
	reg &= ~MV88E6XXX_PORT_CTL0_STATE_MASK;
491 492 493

	switch (state) {
	case BR_STATE_DISABLED:
494
		state = MV88E6XXX_PORT_CTL0_STATE_DISABLED;
495 496 497
		break;
	case BR_STATE_BLOCKING:
	case BR_STATE_LISTENING:
498
		state = MV88E6XXX_PORT_CTL0_STATE_BLOCKING;
499 500
		break;
	case BR_STATE_LEARNING:
501
		state = MV88E6XXX_PORT_CTL0_STATE_LEARNING;
502 503
		break;
	case BR_STATE_FORWARDING:
504
		state = MV88E6XXX_PORT_CTL0_STATE_FORWARDING;
505 506 507 508 509
		break;
	default:
		return -EINVAL;
	}

510 511
	reg |= state;

512
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
513 514 515
	if (err)
		return err;

516 517
	dev_dbg(chip->dev, "p%d: PortState set to %s\n", port,
		mv88e6xxx_port_state_names[state]);
518 519 520

	return 0;
}
521

522
int mv88e6xxx_port_set_egress_mode(struct mv88e6xxx_chip *chip, int port,
523
				   enum mv88e6xxx_egress_mode mode)
524 525 526 527
{
	int err;
	u16 reg;

528
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
529 530 531
	if (err)
		return err;

532
	reg &= ~MV88E6XXX_PORT_CTL0_EGRESS_MODE_MASK;
533 534 535

	switch (mode) {
	case MV88E6XXX_EGRESS_MODE_UNMODIFIED:
536
		reg |= MV88E6XXX_PORT_CTL0_EGRESS_MODE_UNMODIFIED;
537 538
		break;
	case MV88E6XXX_EGRESS_MODE_UNTAGGED:
539
		reg |= MV88E6XXX_PORT_CTL0_EGRESS_MODE_UNTAGGED;
540 541
		break;
	case MV88E6XXX_EGRESS_MODE_TAGGED:
542
		reg |= MV88E6XXX_PORT_CTL0_EGRESS_MODE_TAGGED;
543 544
		break;
	case MV88E6XXX_EGRESS_MODE_ETHERTYPE:
545
		reg |= MV88E6XXX_PORT_CTL0_EGRESS_MODE_ETHER_TYPE_DSA;
546 547 548 549
		break;
	default:
		return -EINVAL;
	}
550

551
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
552 553 554 555 556 557 558 559
}

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

560
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
561 562 563
	if (err)
		return err;

564
	reg &= ~MV88E6XXX_PORT_CTL0_FRAME_MODE_MASK;
565 566 567

	switch (mode) {
	case MV88E6XXX_FRAME_MODE_NORMAL:
568
		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_NORMAL;
569 570
		break;
	case MV88E6XXX_FRAME_MODE_DSA:
571
		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_DSA;
572 573 574 575 576
		break;
	default:
		return -EINVAL;
	}

577
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
578 579 580 581 582 583 584 585
}

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

586
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
587 588 589
	if (err)
		return err;

590
	reg &= ~MV88E6XXX_PORT_CTL0_FRAME_MODE_MASK;
591 592 593

	switch (mode) {
	case MV88E6XXX_FRAME_MODE_NORMAL:
594
		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_NORMAL;
595 596
		break;
	case MV88E6XXX_FRAME_MODE_DSA:
597
		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_DSA;
598 599
		break;
	case MV88E6XXX_FRAME_MODE_PROVIDER:
600
		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_PROVIDER;
601 602
		break;
	case MV88E6XXX_FRAME_MODE_ETHERTYPE:
603
		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_ETHER_TYPE_DSA;
604 605 606 607 608
		break;
	default:
		return -EINVAL;
	}

609
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
610 611
}

612 613
static int mv88e6185_port_set_forward_unknown(struct mv88e6xxx_chip *chip,
					      int port, bool unicast)
614 615 616 617
{
	int err;
	u16 reg;

618
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
619 620 621
	if (err)
		return err;

622
	if (unicast)
623
		reg |= MV88E6185_PORT_CTL0_FORWARD_UNKNOWN;
624
	else
625
		reg &= ~MV88E6185_PORT_CTL0_FORWARD_UNKNOWN;
626

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

630 631
int mv88e6352_port_set_egress_floods(struct mv88e6xxx_chip *chip, int port,
				     bool unicast, bool multicast)
632 633 634 635
{
	int err;
	u16 reg;

636
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
637 638 639
	if (err)
		return err;

640
	reg &= ~MV88E6352_PORT_CTL0_EGRESS_FLOODS_MASK;
641 642

	if (unicast && multicast)
643
		reg |= MV88E6352_PORT_CTL0_EGRESS_FLOODS_ALL_UNKNOWN_DA;
644
	else if (unicast)
645
		reg |= MV88E6352_PORT_CTL0_EGRESS_FLOODS_NO_UNKNOWN_MC_DA;
646
	else if (multicast)
647
		reg |= MV88E6352_PORT_CTL0_EGRESS_FLOODS_NO_UNKNOWN_UC_DA;
648
	else
649
		reg |= MV88E6352_PORT_CTL0_EGRESS_FLOODS_NO_UNKNOWN_DA;
650

651
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
652 653
}

654 655
/* Offset 0x05: Port Control 1 */

656 657 658 659 660 661
int mv88e6xxx_port_set_message_port(struct mv88e6xxx_chip *chip, int port,
				    bool message_port)
{
	u16 val;
	int err;

662
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL1, &val);
663 664 665 666
	if (err)
		return err;

	if (message_port)
667
		val |= MV88E6XXX_PORT_CTL1_MESSAGE_PORT;
668
	else
669
		val &= ~MV88E6XXX_PORT_CTL1_MESSAGE_PORT;
670

671
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL1, val);
672 673
}

674 675 676 677
/* Offset 0x06: Port Based VLAN Map */

int mv88e6xxx_port_set_vlan_map(struct mv88e6xxx_chip *chip, int port, u16 map)
{
678
	const u16 mask = mv88e6xxx_port_mask(chip);
679 680 681
	u16 reg;
	int err;

682
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_BASE_VLAN, &reg);
683 684 685 686 687 688
	if (err)
		return err;

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

689
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_BASE_VLAN, reg);
690 691 692
	if (err)
		return err;

693
	dev_dbg(chip->dev, "p%d: VLANTable set to %.3x\n", port, map);
694 695 696

	return 0;
}
697 698 699 700 701 702 703 704

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 */
705
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_BASE_VLAN, &reg);
706 707 708 709 710 711 712
	if (err)
		return err;

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

	/* Port's default FID upper bits are located in reg 0x05, offset 0 */
	if (upper_mask) {
713 714
		err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL1,
					  &reg);
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
		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 */
734
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_BASE_VLAN, &reg);
735 736 737 738 739 740
	if (err)
		return err;

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

741
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_BASE_VLAN, reg);
742 743 744 745 746
	if (err)
		return err;

	/* Port's default FID upper bits are located in reg 0x05, offset 0 */
	if (upper_mask) {
747 748
		err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL1,
					  &reg);
749 750 751 752 753 754
		if (err)
			return err;

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

755 756
		err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL1,
					   reg);
757 758 759 760
		if (err)
			return err;
	}

761
	dev_dbg(chip->dev, "p%d: FID set to %u\n", port, fid);
762 763 764

	return 0;
}
765 766 767 768 769 770 771 772

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

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

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

778
	*pvid = reg & MV88E6XXX_PORT_DEFAULT_VLAN_MASK;
779 780 781 782 783 784 785 786 787

	return 0;
}

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

788 789
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_DEFAULT_VLAN,
				  &reg);
790 791 792
	if (err)
		return err;

793 794
	reg &= ~MV88E6XXX_PORT_DEFAULT_VLAN_MASK;
	reg |= pvid & MV88E6XXX_PORT_DEFAULT_VLAN_MASK;
795

796 797
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_DEFAULT_VLAN,
				   reg);
798 799 800
	if (err)
		return err;

801
	dev_dbg(chip->dev, "p%d: DefaultVID set to %u\n", port, pvid);
802 803 804

	return 0;
}
805 806 807 808

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

static const char * const mv88e6xxx_port_8021q_mode_names[] = {
809 810 811 812
	[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",
813 814
};

815 816
static int mv88e6185_port_set_default_forward(struct mv88e6xxx_chip *chip,
					      int port, bool multicast)
817 818 819 820
{
	int err;
	u16 reg;

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

825
	if (multicast)
826
		reg |= MV88E6XXX_PORT_CTL2_DEFAULT_FORWARD;
827
	else
828
		reg &= ~MV88E6XXX_PORT_CTL2_DEFAULT_FORWARD;
829

830
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
831 832
}

833 834 835 836 837 838 839 840 841 842 843 844
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);
}

845 846 847 848 849 850
int mv88e6095_port_set_upstream_port(struct mv88e6xxx_chip *chip, int port,
				     int upstream_port)
{
	int err;
	u16 reg;

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

855
	reg &= ~MV88E6095_PORT_CTL2_CPU_PORT_MASK;
856 857
	reg |= upstream_port;

858
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
859 860
}

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

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

871 872
	reg &= ~MV88E6XXX_PORT_CTL2_8021Q_MODE_MASK;
	reg |= mode & MV88E6XXX_PORT_CTL2_8021Q_MODE_MASK;
873

874
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
875 876 877
	if (err)
		return err;

878 879
	dev_dbg(chip->dev, "p%d: 802.1QMode set to %s\n", port,
		mv88e6xxx_port_8021q_mode_names[mode]);
880 881 882

	return 0;
}
883

884 885 886 887 888
int mv88e6xxx_port_set_map_da(struct mv88e6xxx_chip *chip, int port)
{
	u16 reg;
	int err;

889
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
890 891 892
	if (err)
		return err;

893
	reg |= MV88E6XXX_PORT_CTL2_MAP_DA;
894

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

898 899
int mv88e6165_port_set_jumbo_size(struct mv88e6xxx_chip *chip, int port,
				  size_t size)
900 901 902 903
{
	u16 reg;
	int err;

904
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
905 906 907
	if (err)
		return err;

908
	reg &= ~MV88E6XXX_PORT_CTL2_JUMBO_MODE_MASK;
909 910

	if (size <= 1522)
911
		reg |= MV88E6XXX_PORT_CTL2_JUMBO_MODE_1522;
912
	else if (size <= 2048)
913
		reg |= MV88E6XXX_PORT_CTL2_JUMBO_MODE_2048;
914
	else if (size <= 10240)
915
		reg |= MV88E6XXX_PORT_CTL2_JUMBO_MODE_10240;
916 917
	else
		return -ERANGE;
918

919
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
920 921
}

922 923 924 925
/* Offset 0x09: Port Rate Control */

int mv88e6095_port_egress_rate_limiting(struct mv88e6xxx_chip *chip, int port)
{
926 927
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_EGRESS_RATE_CTL1,
				    0x0000);
928 929 930 931
}

int mv88e6097_port_egress_rate_limiting(struct mv88e6xxx_chip *chip, int port)
{
932 933
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_EGRESS_RATE_CTL1,
				    0x0001);
934 935
}

936 937 938 939
/* Offset 0x0C: Port ATU Control */

int mv88e6xxx_port_disable_learn_limit(struct mv88e6xxx_chip *chip, int port)
{
940
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_ATU_CTL, 0);
941 942
}

943 944 945 946
/* Offset 0x0D: (Priority) Override Register */

int mv88e6xxx_port_disable_pri_override(struct mv88e6xxx_chip *chip, int port)
{
947
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_PRI_OVERRIDE, 0);
948 949
}

950 951 952 953 954
/* Offset 0x0f: Port Ether type */

int mv88e6351_port_set_ether_type(struct mv88e6xxx_chip *chip, int port,
				  u16 etype)
{
955
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_ETH_TYPE, etype);
956 957
}

958 959 960 961 962 963 964 965 966
/* 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 */
967 968 969
	err = mv88e6xxx_port_write(chip, port,
				   MV88E6095_PORT_IEEE_PRIO_REMAP_0123,
				   0x3210);
970 971 972
	if (err)
		return err;

973 974 975
	return mv88e6xxx_port_write(chip, port,
				    MV88E6095_PORT_IEEE_PRIO_REMAP_4567,
				    0x7654);
976 977 978
}

static int mv88e6xxx_port_ieeepmt_write(struct mv88e6xxx_chip *chip,
979
					int port, u16 table, u8 ptr, u16 data)
980 981 982
{
	u16 reg;

983 984 985
	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);
986

987 988
	return mv88e6xxx_port_write(chip, port,
				    MV88E6390_PORT_IEEE_PRIO_MAP_TABLE, reg);
989 990 991 992 993
}

int mv88e6390_port_tag_remap(struct mv88e6xxx_chip *chip, int port)
{
	int err, i;
994
	u16 table;
995 996

	for (i = 0; i <= 7; i++) {
997 998 999
		table = MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_INGRESS_PCP;
		err = mv88e6xxx_port_ieeepmt_write(chip, port, table, i,
						   (i | i << 4));
1000 1001 1002
		if (err)
			return err;

1003 1004
		table = MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_EGRESS_GREEN_PCP;
		err = mv88e6xxx_port_ieeepmt_write(chip, port, table, i, i);
1005 1006 1007
		if (err)
			return err;

1008 1009
		table = MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_EGRESS_YELLOW_PCP;
		err = mv88e6xxx_port_ieeepmt_write(chip, port, table, i, i);
1010 1011 1012
		if (err)
			return err;

1013 1014
		table = MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_EGRESS_AVB_PCP;
		err = mv88e6xxx_port_ieeepmt_write(chip, port, table, i, i);
1015 1016 1017 1018 1019 1020
		if (err)
			return err;
	}

	return 0;
}