port.c 22.1 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
/* Offset 0x04: Port Control Register */

static const char * const mv88e6xxx_port_state_names[] = {
411 412 413 414
	[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",
415 416 417 418 419 420 421
};

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

422
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
423 424 425
	if (err)
		return err;

426
	reg &= ~MV88E6XXX_PORT_CTL0_STATE_MASK;
427 428 429

	switch (state) {
	case BR_STATE_DISABLED:
430
		state = MV88E6XXX_PORT_CTL0_STATE_DISABLED;
431 432 433
		break;
	case BR_STATE_BLOCKING:
	case BR_STATE_LISTENING:
434
		state = MV88E6XXX_PORT_CTL0_STATE_BLOCKING;
435 436
		break;
	case BR_STATE_LEARNING:
437
		state = MV88E6XXX_PORT_CTL0_STATE_LEARNING;
438 439
		break;
	case BR_STATE_FORWARDING:
440
		state = MV88E6XXX_PORT_CTL0_STATE_FORWARDING;
441 442 443 444 445
		break;
	default:
		return -EINVAL;
	}

446 447
	reg |= state;

448
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
449 450 451
	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
{
	int err;
	u16 reg;

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

468
	reg &= ~MV88E6XXX_PORT_CTL0_EGRESS_MODE_MASK;
469 470 471

	switch (mode) {
	case MV88E6XXX_EGRESS_MODE_UNMODIFIED:
472
		reg |= MV88E6XXX_PORT_CTL0_EGRESS_MODE_UNMODIFIED;
473 474
		break;
	case MV88E6XXX_EGRESS_MODE_UNTAGGED:
475
		reg |= MV88E6XXX_PORT_CTL0_EGRESS_MODE_UNTAGGED;
476 477
		break;
	case MV88E6XXX_EGRESS_MODE_TAGGED:
478
		reg |= MV88E6XXX_PORT_CTL0_EGRESS_MODE_TAGGED;
479 480
		break;
	case MV88E6XXX_EGRESS_MODE_ETHERTYPE:
481
		reg |= MV88E6XXX_PORT_CTL0_EGRESS_MODE_ETHER_TYPE_DSA;
482 483 484 485
		break;
	default:
		return -EINVAL;
	}
486

487
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
488 489 490 491 492 493 494 495
}

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

496
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
497 498 499
	if (err)
		return err;

500
	reg &= ~MV88E6XXX_PORT_CTL0_FRAME_MODE_MASK;
501 502 503

	switch (mode) {
	case MV88E6XXX_FRAME_MODE_NORMAL:
504
		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_NORMAL;
505 506
		break;
	case MV88E6XXX_FRAME_MODE_DSA:
507
		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_DSA;
508 509 510 511 512
		break;
	default:
		return -EINVAL;
	}

513
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
514 515 516 517 518 519 520 521
}

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

522
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
523 524 525
	if (err)
		return err;

526
	reg &= ~MV88E6XXX_PORT_CTL0_FRAME_MODE_MASK;
527 528 529

	switch (mode) {
	case MV88E6XXX_FRAME_MODE_NORMAL:
530
		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_NORMAL;
531 532
		break;
	case MV88E6XXX_FRAME_MODE_DSA:
533
		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_DSA;
534 535
		break;
	case MV88E6XXX_FRAME_MODE_PROVIDER:
536
		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_PROVIDER;
537 538
		break;
	case MV88E6XXX_FRAME_MODE_ETHERTYPE:
539
		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_ETHER_TYPE_DSA;
540 541 542 543 544
		break;
	default:
		return -EINVAL;
	}

545
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
546 547
}

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

554
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
555 556 557
	if (err)
		return err;

558
	if (unicast)
559
		reg |= MV88E6185_PORT_CTL0_FORWARD_UNKNOWN;
560
	else
561
		reg &= ~MV88E6185_PORT_CTL0_FORWARD_UNKNOWN;
562

563
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
564 565
}

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

572
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
573 574 575
	if (err)
		return err;

576
	reg &= ~MV88E6352_PORT_CTL0_EGRESS_FLOODS_MASK;
577 578

	if (unicast && multicast)
579
		reg |= MV88E6352_PORT_CTL0_EGRESS_FLOODS_ALL_UNKNOWN_DA;
580
	else if (unicast)
581
		reg |= MV88E6352_PORT_CTL0_EGRESS_FLOODS_NO_UNKNOWN_MC_DA;
582
	else if (multicast)
583
		reg |= MV88E6352_PORT_CTL0_EGRESS_FLOODS_NO_UNKNOWN_UC_DA;
584
	else
585
		reg |= MV88E6352_PORT_CTL0_EGRESS_FLOODS_NO_UNKNOWN_DA;
586

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

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

592 593 594 595 596 597
int mv88e6xxx_port_set_message_port(struct mv88e6xxx_chip *chip, int port,
				    bool message_port)
{
	u16 val;
	int err;

598
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL1, &val);
599 600 601 602
	if (err)
		return err;

	if (message_port)
603
		val |= MV88E6XXX_PORT_CTL1_MESSAGE_PORT;
604
	else
605
		val &= ~MV88E6XXX_PORT_CTL1_MESSAGE_PORT;
606

607
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL1, val);
608 609
}

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
	u16 reg;
	int err;

618
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_BASE_VLAN, &reg);
619 620 621 622 623 624
	if (err)
		return err;

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

625
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_BASE_VLAN, reg);
626 627 628
	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

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 */
641
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_BASE_VLAN, &reg);
642 643 644 645 646 647 648
	if (err)
		return err;

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

	/* Port's default FID upper bits are located in reg 0x05, offset 0 */
	if (upper_mask) {
649 650
		err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL1,
					  &reg);
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
		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 */
670
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_BASE_VLAN, &reg);
671 672 673 674 675 676
	if (err)
		return err;

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

677
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_BASE_VLAN, reg);
678 679 680 681 682
	if (err)
		return err;

	/* Port's default FID upper bits are located in reg 0x05, offset 0 */
	if (upper_mask) {
683 684
		err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL1,
					  &reg);
685 686 687 688 689 690
		if (err)
			return err;

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

691 692
		err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL1,
					   reg);
693 694 695 696
		if (err)
			return err;
	}

697
	dev_dbg(chip->dev, "p%d: FID set to %u\n", port, fid);
698 699 700

	return 0;
}
701 702 703 704 705 706 707 708

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

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

709 710
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_DEFAULT_VLAN,
				  &reg);
711 712 713
	if (err)
		return err;

714
	*pvid = reg & MV88E6XXX_PORT_DEFAULT_VLAN_MASK;
715 716 717 718 719 720 721 722 723

	return 0;
}

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

724 725
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_DEFAULT_VLAN,
				  &reg);
726 727 728
	if (err)
		return err;

729 730
	reg &= ~MV88E6XXX_PORT_DEFAULT_VLAN_MASK;
	reg |= pvid & MV88E6XXX_PORT_DEFAULT_VLAN_MASK;
731

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

737
	dev_dbg(chip->dev, "p%d: DefaultVID set to %u\n", port, pvid);
738 739 740

	return 0;
}
741 742 743 744

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

static const char * const mv88e6xxx_port_8021q_mode_names[] = {
745 746 747 748
	[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",
749 750
};

751 752
static int mv88e6185_port_set_default_forward(struct mv88e6xxx_chip *chip,
					      int port, bool multicast)
753 754 755 756
{
	int err;
	u16 reg;

757
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
758 759 760
	if (err)
		return err;

761
	if (multicast)
762
		reg |= MV88E6XXX_PORT_CTL2_DEFAULT_FORWARD;
763
	else
764
		reg &= ~MV88E6XXX_PORT_CTL2_DEFAULT_FORWARD;
765

766
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
767 768
}

769 770 771 772 773 774 775 776 777 778 779 780
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);
}

781 782 783 784 785 786
int mv88e6095_port_set_upstream_port(struct mv88e6xxx_chip *chip, int port,
				     int upstream_port)
{
	int err;
	u16 reg;

787
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
788 789 790
	if (err)
		return err;

791
	reg &= ~MV88E6095_PORT_CTL2_CPU_PORT_MASK;
792 793
	reg |= upstream_port;

794
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
795 796
}

797 798 799 800 801 802
int mv88e6xxx_port_set_8021q_mode(struct mv88e6xxx_chip *chip, int port,
				  u16 mode)
{
	u16 reg;
	int err;

803
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
804 805 806
	if (err)
		return err;

807 808
	reg &= ~MV88E6XXX_PORT_CTL2_8021Q_MODE_MASK;
	reg |= mode & MV88E6XXX_PORT_CTL2_8021Q_MODE_MASK;
809

810
	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
811 812 813
	if (err)
		return err;

814 815
	dev_dbg(chip->dev, "p%d: 802.1QMode set to %s\n", port,
		mv88e6xxx_port_8021q_mode_names[mode]);
816 817 818

	return 0;
}
819

820 821 822 823 824
int mv88e6xxx_port_set_map_da(struct mv88e6xxx_chip *chip, int port)
{
	u16 reg;
	int err;

825
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
826 827 828
	if (err)
		return err;

829
	reg |= MV88E6XXX_PORT_CTL2_MAP_DA;
830

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

834 835
int mv88e6165_port_set_jumbo_size(struct mv88e6xxx_chip *chip, int port,
				  size_t size)
836 837 838 839
{
	u16 reg;
	int err;

840
	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
841 842 843
	if (err)
		return err;

844
	reg &= ~MV88E6XXX_PORT_CTL2_JUMBO_MODE_MASK;
845 846

	if (size <= 1522)
847
		reg |= MV88E6XXX_PORT_CTL2_JUMBO_MODE_1522;
848
	else if (size <= 2048)
849
		reg |= MV88E6XXX_PORT_CTL2_JUMBO_MODE_2048;
850
	else if (size <= 10240)
851
		reg |= MV88E6XXX_PORT_CTL2_JUMBO_MODE_10240;
852 853
	else
		return -ERANGE;
854

855
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
856 857
}

858 859 860 861
/* Offset 0x09: Port Rate Control */

int mv88e6095_port_egress_rate_limiting(struct mv88e6xxx_chip *chip, int port)
{
862 863
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_EGRESS_RATE_CTL1,
				    0x0000);
864 865 866 867
}

int mv88e6097_port_egress_rate_limiting(struct mv88e6xxx_chip *chip, int port)
{
868 869
	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_EGRESS_RATE_CTL1,
				    0x0001);
870 871
}

872 873 874 875 876 877 878
/* 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);
}

879 880 881 882 883 884 885
/* 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);
}

886 887 888 889 890 891 892 893
/* 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);
}

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 948 949 950 951 952 953 954 955
/* 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;
}