port.c 25.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
#include "port.h"
22
#include "serdes.h"
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return 0;
}

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

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

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

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

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

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

	return 0;
}

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

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

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

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

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

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

342 343 344
int mv88e6390x_port_set_cmode(struct mv88e6xxx_chip *chip, int port,
			      phy_interface_t mode)
{
345
	int lane;
346
	u16 cmode;
347
	u16 reg;
348 349 350 351 352 353 354 355 356 357
	int err;

	if (mode == PHY_INTERFACE_MODE_NA)
		return 0;

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

	switch (mode) {
	case PHY_INTERFACE_MODE_1000BASEX:
358
		cmode = MV88E6XXX_PORT_STS_CMODE_1000BASE_X;
359 360
		break;
	case PHY_INTERFACE_MODE_SGMII:
361
		cmode = MV88E6XXX_PORT_STS_CMODE_SGMII;
362 363
		break;
	case PHY_INTERFACE_MODE_2500BASEX:
364
		cmode = MV88E6XXX_PORT_STS_CMODE_2500BASEX;
365 366
		break;
	case PHY_INTERFACE_MODE_XGMII:
367
	case PHY_INTERFACE_MODE_XAUI:
368
		cmode = MV88E6XXX_PORT_STS_CMODE_XAUI;
369 370
		break;
	case PHY_INTERFACE_MODE_RXAUI:
371
		cmode = MV88E6XXX_PORT_STS_CMODE_RXAUI;
372 373 374 375 376
		break;
	default:
		cmode = 0;
	}

377 378 379 380
	/* cmode doesn't change, nothing to do for us */
	if (cmode == chip->ports[port].cmode)
		return 0;

381
	lane = mv88e6390x_serdes_get_lane(chip, port);
382
	if (lane < 0 && lane != -ENODEV)
383 384
		return lane;

385 386 387 388 389 390 391 392
	if (lane >= 0) {
		if (chip->ports[port].serdes_irq) {
			err = mv88e6390_serdes_irq_disable(chip, port, lane);
			if (err)
				return err;
		}

		err = mv88e6390x_serdes_power(chip, port, false);
393 394 395 396
		if (err)
			return err;
	}

397
	chip->ports[port].cmode = 0;
398

399
	if (cmode) {
400
		err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_STS, &reg);
401 402 403
		if (err)
			return err;

404
		reg &= ~MV88E6XXX_PORT_STS_CMODE_MASK;
405 406
		reg |= cmode;

407
		err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_STS, reg);
408 409
		if (err)
			return err;
410

411 412 413 414 415 416
		chip->ports[port].cmode = cmode;

		lane = mv88e6390x_serdes_get_lane(chip, port);
		if (lane < 0)
			return lane;

417
		err = mv88e6390x_serdes_power(chip, port, true);
418 419
		if (err)
			return err;
420 421 422 423 424 425

		if (chip->ports[port].serdes_irq) {
			err = mv88e6390_serdes_irq_enable(chip, port, lane);
			if (err)
				return err;
		}
426 427 428 429 430
	}

	return 0;
}

431
int mv88e6185_port_get_cmode(struct mv88e6xxx_chip *chip, int port, u8 *cmode)
432 433 434 435 436 437 438 439
{
	int err;
	u16 reg;

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

440 441 442
	*cmode = reg & MV88E6185_PORT_STS_CMODE_MASK;

	return 0;
443 444
}

445
int mv88e6352_port_get_cmode(struct mv88e6xxx_chip *chip, int port, u8 *cmode)
446 447 448 449
{
	int err;
	u16 reg;

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

454
	*cmode = reg & MV88E6XXX_PORT_STS_CMODE_MASK;
455 456 457 458

	return 0;
}

459
int mv88e6352_port_link_state(struct mv88e6xxx_chip *chip, int port,
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
			      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:
480
		if ((reg & MV88E6XXX_PORT_STS_CMODE_MASK) ==
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
		    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;
}

497 498 499 500
int mv88e6185_port_link_state(struct mv88e6xxx_chip *chip, int port,
			      struct phylink_link_state *state)
{
	if (state->interface == PHY_INTERFACE_MODE_1000BASEX) {
501
		u8 cmode = chip->ports[port].cmode;
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

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

533
/* Offset 0x02: Jamming Control
534 535 536 537 538
 *
 * 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.
 */
539 540
int mv88e6097_port_pause_limit(struct mv88e6xxx_chip *chip, int port, u8 in,
			       u8 out)
541
{
542 543
	return mv88e6xxx_port_write(chip, port, MV88E6097_PORT_JAM_CTL,
				    out << 8 | in);
544 545
}

546 547
int mv88e6390_port_pause_limit(struct mv88e6xxx_chip *chip, int port, u8 in,
			       u8 out)
548 549 550
{
	int err;

551 552 553
	err = mv88e6xxx_port_write(chip, port, MV88E6390_PORT_FLOW_CTL,
				   MV88E6390_PORT_FLOW_CTL_UPDATE |
				   MV88E6390_PORT_FLOW_CTL_LIMIT_IN | in);
554 555 556
	if (err)
		return err;

557 558 559
	return mv88e6xxx_port_write(chip, port, MV88E6390_PORT_FLOW_CTL,
				    MV88E6390_PORT_FLOW_CTL_UPDATE |
				    MV88E6390_PORT_FLOW_CTL_LIMIT_OUT | out);
560 561
}

562 563 564
/* Offset 0x04: Port Control Register */

static const char * const mv88e6xxx_port_state_names[] = {
565 566 567 568
	[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",
569 570 571 572 573 574 575
};

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

576
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
577 578 579
	if (err)
		return err;

580
	reg &= ~MV88E6XXX_PORT_CTL0_STATE_MASK;
581 582 583

	switch (state) {
	case BR_STATE_DISABLED:
584
		state = MV88E6XXX_PORT_CTL0_STATE_DISABLED;
585 586 587
		break;
	case BR_STATE_BLOCKING:
	case BR_STATE_LISTENING:
588
		state = MV88E6XXX_PORT_CTL0_STATE_BLOCKING;
589 590
		break;
	case BR_STATE_LEARNING:
591
		state = MV88E6XXX_PORT_CTL0_STATE_LEARNING;
592 593
		break;
	case BR_STATE_FORWARDING:
594
		state = MV88E6XXX_PORT_CTL0_STATE_FORWARDING;
595 596 597 598 599
		break;
	default:
		return -EINVAL;
	}

600 601
	reg |= state;

602
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
603 604 605
	if (err)
		return err;

606 607
	dev_dbg(chip->dev, "p%d: PortState set to %s\n", port,
		mv88e6xxx_port_state_names[state]);
608 609 610

	return 0;
}
611

612
int mv88e6xxx_port_set_egress_mode(struct mv88e6xxx_chip *chip, int port,
613
				   enum mv88e6xxx_egress_mode mode)
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
	reg &= ~MV88E6XXX_PORT_CTL0_EGRESS_MODE_MASK;
623 624 625

	switch (mode) {
	case MV88E6XXX_EGRESS_MODE_UNMODIFIED:
626
		reg |= MV88E6XXX_PORT_CTL0_EGRESS_MODE_UNMODIFIED;
627 628
		break;
	case MV88E6XXX_EGRESS_MODE_UNTAGGED:
629
		reg |= MV88E6XXX_PORT_CTL0_EGRESS_MODE_UNTAGGED;
630 631
		break;
	case MV88E6XXX_EGRESS_MODE_TAGGED:
632
		reg |= MV88E6XXX_PORT_CTL0_EGRESS_MODE_TAGGED;
633 634
		break;
	case MV88E6XXX_EGRESS_MODE_ETHERTYPE:
635
		reg |= MV88E6XXX_PORT_CTL0_EGRESS_MODE_ETHER_TYPE_DSA;
636 637 638 639
		break;
	default:
		return -EINVAL;
	}
640

641
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
642 643 644 645 646 647 648 649
}

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

650
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
651 652 653
	if (err)
		return err;

654
	reg &= ~MV88E6XXX_PORT_CTL0_FRAME_MODE_MASK;
655 656 657

	switch (mode) {
	case MV88E6XXX_FRAME_MODE_NORMAL:
658
		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_NORMAL;
659 660
		break;
	case MV88E6XXX_FRAME_MODE_DSA:
661
		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_DSA;
662 663 664 665 666
		break;
	default:
		return -EINVAL;
	}

667
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
668 669 670 671 672 673 674 675
}

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

676
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
677 678 679
	if (err)
		return err;

680
	reg &= ~MV88E6XXX_PORT_CTL0_FRAME_MODE_MASK;
681 682 683

	switch (mode) {
	case MV88E6XXX_FRAME_MODE_NORMAL:
684
		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_NORMAL;
685 686
		break;
	case MV88E6XXX_FRAME_MODE_DSA:
687
		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_DSA;
688 689
		break;
	case MV88E6XXX_FRAME_MODE_PROVIDER:
690
		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_PROVIDER;
691 692
		break;
	case MV88E6XXX_FRAME_MODE_ETHERTYPE:
693
		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_ETHER_TYPE_DSA;
694 695 696 697 698
		break;
	default:
		return -EINVAL;
	}

699
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
700 701
}

702 703
static int mv88e6185_port_set_forward_unknown(struct mv88e6xxx_chip *chip,
					      int port, bool unicast)
704 705 706 707
{
	int err;
	u16 reg;

708
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
709 710 711
	if (err)
		return err;

712
	if (unicast)
713
		reg |= MV88E6185_PORT_CTL0_FORWARD_UNKNOWN;
714
	else
715
		reg &= ~MV88E6185_PORT_CTL0_FORWARD_UNKNOWN;
716

717
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
718 719
}

720 721
int mv88e6352_port_set_egress_floods(struct mv88e6xxx_chip *chip, int port,
				     bool unicast, bool multicast)
722 723 724 725
{
	int err;
	u16 reg;

726
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
727 728 729
	if (err)
		return err;

730
	reg &= ~MV88E6352_PORT_CTL0_EGRESS_FLOODS_MASK;
731 732

	if (unicast && multicast)
733
		reg |= MV88E6352_PORT_CTL0_EGRESS_FLOODS_ALL_UNKNOWN_DA;
734
	else if (unicast)
735
		reg |= MV88E6352_PORT_CTL0_EGRESS_FLOODS_NO_UNKNOWN_MC_DA;
736
	else if (multicast)
737
		reg |= MV88E6352_PORT_CTL0_EGRESS_FLOODS_NO_UNKNOWN_UC_DA;
738
	else
739
		reg |= MV88E6352_PORT_CTL0_EGRESS_FLOODS_NO_UNKNOWN_DA;
740

741
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
742 743
}

744 745
/* Offset 0x05: Port Control 1 */

746 747 748 749 750 751
int mv88e6xxx_port_set_message_port(struct mv88e6xxx_chip *chip, int port,
				    bool message_port)
{
	u16 val;
	int err;

752
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL1, &val);
753 754 755 756
	if (err)
		return err;

	if (message_port)
757
		val |= MV88E6XXX_PORT_CTL1_MESSAGE_PORT;
758
	else
759
		val &= ~MV88E6XXX_PORT_CTL1_MESSAGE_PORT;
760

761
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL1, val);
762 763
}

764 765 766 767
/* Offset 0x06: Port Based VLAN Map */

int mv88e6xxx_port_set_vlan_map(struct mv88e6xxx_chip *chip, int port, u16 map)
{
768
	const u16 mask = mv88e6xxx_port_mask(chip);
769 770 771
	u16 reg;
	int err;

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

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

779
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_BASE_VLAN, reg);
780 781 782
	if (err)
		return err;

783
	dev_dbg(chip->dev, "p%d: VLANTable set to %.3x\n", port, map);
784 785 786

	return 0;
}
787 788 789 790 791 792 793 794

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 */
795
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_BASE_VLAN, &reg);
796 797 798 799 800 801 802
	if (err)
		return err;

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

	/* Port's default FID upper bits are located in reg 0x05, offset 0 */
	if (upper_mask) {
803 804
		err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL1,
					  &reg);
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
		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 */
824
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_BASE_VLAN, &reg);
825 826 827 828 829 830
	if (err)
		return err;

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

831
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_BASE_VLAN, reg);
832 833 834 835 836
	if (err)
		return err;

	/* Port's default FID upper bits are located in reg 0x05, offset 0 */
	if (upper_mask) {
837 838
		err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL1,
					  &reg);
839 840 841 842 843 844
		if (err)
			return err;

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

845 846
		err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL1,
					   reg);
847 848 849 850
		if (err)
			return err;
	}

851
	dev_dbg(chip->dev, "p%d: FID set to %u\n", port, fid);
852 853 854

	return 0;
}
855 856 857 858 859 860 861 862

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

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

863 864
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_DEFAULT_VLAN,
				  &reg);
865 866 867
	if (err)
		return err;

868
	*pvid = reg & MV88E6XXX_PORT_DEFAULT_VLAN_MASK;
869 870 871 872 873 874 875 876 877

	return 0;
}

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

878 879
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_DEFAULT_VLAN,
				  &reg);
880 881 882
	if (err)
		return err;

883 884
	reg &= ~MV88E6XXX_PORT_DEFAULT_VLAN_MASK;
	reg |= pvid & MV88E6XXX_PORT_DEFAULT_VLAN_MASK;
885

886 887
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_DEFAULT_VLAN,
				   reg);
888 889 890
	if (err)
		return err;

891
	dev_dbg(chip->dev, "p%d: DefaultVID set to %u\n", port, pvid);
892 893 894

	return 0;
}
895 896 897 898

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

static const char * const mv88e6xxx_port_8021q_mode_names[] = {
899 900 901 902
	[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",
903 904
};

905 906
static int mv88e6185_port_set_default_forward(struct mv88e6xxx_chip *chip,
					      int port, bool multicast)
907 908 909 910
{
	int err;
	u16 reg;

911
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
912 913 914
	if (err)
		return err;

915
	if (multicast)
916
		reg |= MV88E6XXX_PORT_CTL2_DEFAULT_FORWARD;
917
	else
918
		reg &= ~MV88E6XXX_PORT_CTL2_DEFAULT_FORWARD;
919

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

923 924 925 926 927 928 929 930 931 932 933 934
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);
}

935 936 937 938 939 940
int mv88e6095_port_set_upstream_port(struct mv88e6xxx_chip *chip, int port,
				     int upstream_port)
{
	int err;
	u16 reg;

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

945
	reg &= ~MV88E6095_PORT_CTL2_CPU_PORT_MASK;
946 947
	reg |= upstream_port;

948
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
949 950
}

951 952 953 954 955 956
int mv88e6xxx_port_set_8021q_mode(struct mv88e6xxx_chip *chip, int port,
				  u16 mode)
{
	u16 reg;
	int err;

957
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
958 959 960
	if (err)
		return err;

961 962
	reg &= ~MV88E6XXX_PORT_CTL2_8021Q_MODE_MASK;
	reg |= mode & MV88E6XXX_PORT_CTL2_8021Q_MODE_MASK;
963

964
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
965 966 967
	if (err)
		return err;

968 969
	dev_dbg(chip->dev, "p%d: 802.1QMode set to %s\n", port,
		mv88e6xxx_port_8021q_mode_names[mode]);
970 971 972

	return 0;
}
973

974 975 976 977 978
int mv88e6xxx_port_set_map_da(struct mv88e6xxx_chip *chip, int port)
{
	u16 reg;
	int err;

979
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
980 981 982
	if (err)
		return err;

983
	reg |= MV88E6XXX_PORT_CTL2_MAP_DA;
984

985
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
986 987
}

988 989
int mv88e6165_port_set_jumbo_size(struct mv88e6xxx_chip *chip, int port,
				  size_t size)
990 991 992 993
{
	u16 reg;
	int err;

994
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
995 996 997
	if (err)
		return err;

998
	reg &= ~MV88E6XXX_PORT_CTL2_JUMBO_MODE_MASK;
999 1000

	if (size <= 1522)
1001
		reg |= MV88E6XXX_PORT_CTL2_JUMBO_MODE_1522;
1002
	else if (size <= 2048)
1003
		reg |= MV88E6XXX_PORT_CTL2_JUMBO_MODE_2048;
1004
	else if (size <= 10240)
1005
		reg |= MV88E6XXX_PORT_CTL2_JUMBO_MODE_10240;
1006 1007
	else
		return -ERANGE;
1008

1009
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
1010 1011
}

1012 1013 1014 1015
/* Offset 0x09: Port Rate Control */

int mv88e6095_port_egress_rate_limiting(struct mv88e6xxx_chip *chip, int port)
{
1016 1017
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_EGRESS_RATE_CTL1,
				    0x0000);
1018 1019 1020 1021
}

int mv88e6097_port_egress_rate_limiting(struct mv88e6xxx_chip *chip, int port)
{
1022 1023
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_EGRESS_RATE_CTL1,
				    0x0001);
1024 1025
}

1026 1027 1028 1029
/* Offset 0x0C: Port ATU Control */

int mv88e6xxx_port_disable_learn_limit(struct mv88e6xxx_chip *chip, int port)
{
1030
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_ATU_CTL, 0);
1031 1032
}

1033 1034 1035 1036
/* Offset 0x0D: (Priority) Override Register */

int mv88e6xxx_port_disable_pri_override(struct mv88e6xxx_chip *chip, int port)
{
1037
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_PRI_OVERRIDE, 0);
1038 1039
}

1040 1041 1042 1043 1044
/* Offset 0x0f: Port Ether type */

int mv88e6351_port_set_ether_type(struct mv88e6xxx_chip *chip, int port,
				  u16 etype)
{
1045
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_ETH_TYPE, etype);
1046 1047
}

1048 1049 1050 1051 1052 1053 1054 1055 1056
/* 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 */
1057 1058 1059
	err = mv88e6xxx_port_write(chip, port,
				   MV88E6095_PORT_IEEE_PRIO_REMAP_0123,
				   0x3210);
1060 1061 1062
	if (err)
		return err;

1063 1064 1065
	return mv88e6xxx_port_write(chip, port,
				    MV88E6095_PORT_IEEE_PRIO_REMAP_4567,
				    0x7654);
1066 1067 1068
}

static int mv88e6xxx_port_ieeepmt_write(struct mv88e6xxx_chip *chip,
1069
					int port, u16 table, u8 ptr, u16 data)
1070 1071 1072
{
	u16 reg;

1073 1074 1075
	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);
1076

1077 1078
	return mv88e6xxx_port_write(chip, port,
				    MV88E6390_PORT_IEEE_PRIO_MAP_TABLE, reg);
1079 1080 1081 1082 1083
}

int mv88e6390_port_tag_remap(struct mv88e6xxx_chip *chip, int port)
{
	int err, i;
1084
	u16 table;
1085 1086

	for (i = 0; i <= 7; i++) {
1087 1088 1089
		table = MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_INGRESS_PCP;
		err = mv88e6xxx_port_ieeepmt_write(chip, port, table, i,
						   (i | i << 4));
1090 1091 1092
		if (err)
			return err;

1093 1094
		table = MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_EGRESS_GREEN_PCP;
		err = mv88e6xxx_port_ieeepmt_write(chip, port, table, i, i);
1095 1096 1097
		if (err)
			return err;

1098 1099
		table = MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_EGRESS_YELLOW_PCP;
		err = mv88e6xxx_port_ieeepmt_write(chip, port, table, i, i);
1100 1101 1102
		if (err)
			return err;

1103 1104
		table = MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_EGRESS_AVB_PCP;
		err = mv88e6xxx_port_ieeepmt_write(chip, port, table, i, i);
1105 1106 1107 1108 1109 1110
		if (err)
			return err;
	}

	return 0;
}