Linux 4.16.11
[linux/fpc-iii.git] / drivers / net / dsa / mv88e6xxx / port.c
blob6315774d72b3304d5ccd1729cb150c183bbac19e
1 /*
2 * Marvell 88E6xxx Switch Port Registers support
4 * Copyright (c) 2008 Marvell Semiconductor
6 * Copyright (c) 2016-2017 Savoir-faire Linux Inc.
7 * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
15 #include <linux/bitfield.h>
16 #include <linux/if_bridge.h>
17 #include <linux/phy.h>
19 #include "chip.h"
20 #include "port.h"
22 int mv88e6xxx_port_read(struct mv88e6xxx_chip *chip, int port, int reg,
23 u16 *val)
25 int addr = chip->info->port_base_addr + port;
27 return mv88e6xxx_read(chip, addr, reg, val);
30 int mv88e6xxx_port_write(struct mv88e6xxx_chip *chip, int port, int reg,
31 u16 val)
33 int addr = chip->info->port_base_addr + port;
35 return mv88e6xxx_write(chip, addr, reg, val);
38 /* Offset 0x01: MAC (or PCS or Physical) Control Register
40 * Link, Duplex and Flow Control have one force bit, one value bit.
42 * For port's MAC speed, ForceSpd (or SpdValue) bits 1:0 program the value.
43 * Alternative values require the 200BASE (or AltSpeed) bit 12 set.
44 * Newer chips need a ForcedSpd bit 13 set to consider the value.
47 static int mv88e6xxx_port_set_rgmii_delay(struct mv88e6xxx_chip *chip, int port,
48 phy_interface_t mode)
50 u16 reg;
51 int err;
53 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_MAC_CTL, &reg);
54 if (err)
55 return err;
57 reg &= ~(MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_RXCLK |
58 MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_TXCLK);
60 switch (mode) {
61 case PHY_INTERFACE_MODE_RGMII_RXID:
62 reg |= MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_RXCLK;
63 break;
64 case PHY_INTERFACE_MODE_RGMII_TXID:
65 reg |= MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_TXCLK;
66 break;
67 case PHY_INTERFACE_MODE_RGMII_ID:
68 reg |= MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_RXCLK |
69 MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_TXCLK;
70 break;
71 case PHY_INTERFACE_MODE_RGMII:
72 break;
73 default:
74 return 0;
77 err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_MAC_CTL, reg);
78 if (err)
79 return err;
81 dev_dbg(chip->dev, "p%d: delay RXCLK %s, TXCLK %s\n", port,
82 reg & MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_RXCLK ? "yes" : "no",
83 reg & MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_TXCLK ? "yes" : "no");
85 return 0;
88 int mv88e6352_port_set_rgmii_delay(struct mv88e6xxx_chip *chip, int port,
89 phy_interface_t mode)
91 if (port < 5)
92 return -EOPNOTSUPP;
94 return mv88e6xxx_port_set_rgmii_delay(chip, port, mode);
97 int mv88e6390_port_set_rgmii_delay(struct mv88e6xxx_chip *chip, int port,
98 phy_interface_t mode)
100 if (port != 0)
101 return -EOPNOTSUPP;
103 return mv88e6xxx_port_set_rgmii_delay(chip, port, mode);
106 int mv88e6xxx_port_set_link(struct mv88e6xxx_chip *chip, int port, int link)
108 u16 reg;
109 int err;
111 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_MAC_CTL, &reg);
112 if (err)
113 return err;
115 reg &= ~(MV88E6XXX_PORT_MAC_CTL_FORCE_LINK |
116 MV88E6XXX_PORT_MAC_CTL_LINK_UP);
118 switch (link) {
119 case LINK_FORCED_DOWN:
120 reg |= MV88E6XXX_PORT_MAC_CTL_FORCE_LINK;
121 break;
122 case LINK_FORCED_UP:
123 reg |= MV88E6XXX_PORT_MAC_CTL_FORCE_LINK |
124 MV88E6XXX_PORT_MAC_CTL_LINK_UP;
125 break;
126 case LINK_UNFORCED:
127 /* normal link detection */
128 break;
129 default:
130 return -EINVAL;
133 err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_MAC_CTL, reg);
134 if (err)
135 return err;
137 dev_dbg(chip->dev, "p%d: %s link %s\n", port,
138 reg & MV88E6XXX_PORT_MAC_CTL_FORCE_LINK ? "Force" : "Unforce",
139 reg & MV88E6XXX_PORT_MAC_CTL_LINK_UP ? "up" : "down");
141 return 0;
144 int mv88e6xxx_port_set_duplex(struct mv88e6xxx_chip *chip, int port, int dup)
146 u16 reg;
147 int err;
149 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_MAC_CTL, &reg);
150 if (err)
151 return err;
153 reg &= ~(MV88E6XXX_PORT_MAC_CTL_FORCE_DUPLEX |
154 MV88E6XXX_PORT_MAC_CTL_DUPLEX_FULL);
156 switch (dup) {
157 case DUPLEX_HALF:
158 reg |= MV88E6XXX_PORT_MAC_CTL_FORCE_DUPLEX;
159 break;
160 case DUPLEX_FULL:
161 reg |= MV88E6XXX_PORT_MAC_CTL_FORCE_DUPLEX |
162 MV88E6XXX_PORT_MAC_CTL_DUPLEX_FULL;
163 break;
164 case DUPLEX_UNFORCED:
165 /* normal duplex detection */
166 break;
167 default:
168 return -EINVAL;
171 err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_MAC_CTL, reg);
172 if (err)
173 return err;
175 dev_dbg(chip->dev, "p%d: %s %s duplex\n", port,
176 reg & MV88E6XXX_PORT_MAC_CTL_FORCE_DUPLEX ? "Force" : "Unforce",
177 reg & MV88E6XXX_PORT_MAC_CTL_DUPLEX_FULL ? "full" : "half");
179 return 0;
182 static int mv88e6xxx_port_set_speed(struct mv88e6xxx_chip *chip, int port,
183 int speed, bool alt_bit, bool force_bit)
185 u16 reg, ctrl;
186 int err;
188 switch (speed) {
189 case 10:
190 ctrl = MV88E6XXX_PORT_MAC_CTL_SPEED_10;
191 break;
192 case 100:
193 ctrl = MV88E6XXX_PORT_MAC_CTL_SPEED_100;
194 break;
195 case 200:
196 if (alt_bit)
197 ctrl = MV88E6XXX_PORT_MAC_CTL_SPEED_100 |
198 MV88E6390_PORT_MAC_CTL_ALTSPEED;
199 else
200 ctrl = MV88E6065_PORT_MAC_CTL_SPEED_200;
201 break;
202 case 1000:
203 ctrl = MV88E6XXX_PORT_MAC_CTL_SPEED_1000;
204 break;
205 case 2500:
206 ctrl = MV88E6390_PORT_MAC_CTL_SPEED_10000 |
207 MV88E6390_PORT_MAC_CTL_ALTSPEED;
208 break;
209 case 10000:
210 /* all bits set, fall through... */
211 case SPEED_UNFORCED:
212 ctrl = MV88E6XXX_PORT_MAC_CTL_SPEED_UNFORCED;
213 break;
214 default:
215 return -EOPNOTSUPP;
218 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_MAC_CTL, &reg);
219 if (err)
220 return err;
222 reg &= ~MV88E6XXX_PORT_MAC_CTL_SPEED_MASK;
223 if (alt_bit)
224 reg &= ~MV88E6390_PORT_MAC_CTL_ALTSPEED;
225 if (force_bit) {
226 reg &= ~MV88E6390_PORT_MAC_CTL_FORCE_SPEED;
227 if (speed != SPEED_UNFORCED)
228 ctrl |= MV88E6390_PORT_MAC_CTL_FORCE_SPEED;
230 reg |= ctrl;
232 err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_MAC_CTL, reg);
233 if (err)
234 return err;
236 if (speed)
237 dev_dbg(chip->dev, "p%d: Speed set to %d Mbps\n", port, speed);
238 else
239 dev_dbg(chip->dev, "p%d: Speed unforced\n", port);
241 return 0;
244 /* Support 10, 100, 200 Mbps (e.g. 88E6065 family) */
245 int mv88e6065_port_set_speed(struct mv88e6xxx_chip *chip, int port, int speed)
247 if (speed == SPEED_MAX)
248 speed = 200;
250 if (speed > 200)
251 return -EOPNOTSUPP;
253 /* Setting 200 Mbps on port 0 to 3 selects 100 Mbps */
254 return mv88e6xxx_port_set_speed(chip, port, speed, false, false);
257 /* Support 10, 100, 1000 Mbps (e.g. 88E6185 family) */
258 int mv88e6185_port_set_speed(struct mv88e6xxx_chip *chip, int port, int speed)
260 if (speed == SPEED_MAX)
261 speed = 1000;
263 if (speed == 200 || speed > 1000)
264 return -EOPNOTSUPP;
266 return mv88e6xxx_port_set_speed(chip, port, speed, false, false);
269 /* Support 10, 100, 200, 1000 Mbps (e.g. 88E6352 family) */
270 int mv88e6352_port_set_speed(struct mv88e6xxx_chip *chip, int port, int speed)
272 if (speed == SPEED_MAX)
273 speed = 1000;
275 if (speed > 1000)
276 return -EOPNOTSUPP;
278 if (speed == 200 && port < 5)
279 return -EOPNOTSUPP;
281 return mv88e6xxx_port_set_speed(chip, port, speed, true, false);
284 /* Support 10, 100, 200, 1000, 2500 Mbps (e.g. 88E6390) */
285 int mv88e6390_port_set_speed(struct mv88e6xxx_chip *chip, int port, int speed)
287 if (speed == SPEED_MAX)
288 speed = port < 9 ? 1000 : 2500;
290 if (speed > 2500)
291 return -EOPNOTSUPP;
293 if (speed == 200 && port != 0)
294 return -EOPNOTSUPP;
296 if (speed == 2500 && port < 9)
297 return -EOPNOTSUPP;
299 return mv88e6xxx_port_set_speed(chip, port, speed, true, true);
302 /* Support 10, 100, 200, 1000, 2500, 10000 Mbps (e.g. 88E6190X) */
303 int mv88e6390x_port_set_speed(struct mv88e6xxx_chip *chip, int port, int speed)
305 if (speed == SPEED_MAX)
306 speed = port < 9 ? 1000 : 10000;
308 if (speed == 200 && port != 0)
309 return -EOPNOTSUPP;
311 if (speed >= 2500 && port < 9)
312 return -EOPNOTSUPP;
314 return mv88e6xxx_port_set_speed(chip, port, speed, true, true);
317 int mv88e6390x_port_set_cmode(struct mv88e6xxx_chip *chip, int port,
318 phy_interface_t mode)
320 u16 reg;
321 u16 cmode;
322 int err;
324 if (mode == PHY_INTERFACE_MODE_NA)
325 return 0;
327 if (port != 9 && port != 10)
328 return -EOPNOTSUPP;
330 switch (mode) {
331 case PHY_INTERFACE_MODE_1000BASEX:
332 cmode = MV88E6XXX_PORT_STS_CMODE_1000BASE_X;
333 break;
334 case PHY_INTERFACE_MODE_SGMII:
335 cmode = MV88E6XXX_PORT_STS_CMODE_SGMII;
336 break;
337 case PHY_INTERFACE_MODE_2500BASEX:
338 cmode = MV88E6XXX_PORT_STS_CMODE_2500BASEX;
339 break;
340 case PHY_INTERFACE_MODE_XGMII:
341 case PHY_INTERFACE_MODE_XAUI:
342 cmode = MV88E6XXX_PORT_STS_CMODE_XAUI;
343 break;
344 case PHY_INTERFACE_MODE_RXAUI:
345 cmode = MV88E6XXX_PORT_STS_CMODE_RXAUI;
346 break;
347 default:
348 cmode = 0;
351 if (cmode) {
352 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_STS, &reg);
353 if (err)
354 return err;
356 reg &= ~MV88E6XXX_PORT_STS_CMODE_MASK;
357 reg |= cmode;
359 err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_STS, reg);
360 if (err)
361 return err;
364 return 0;
367 int mv88e6xxx_port_get_cmode(struct mv88e6xxx_chip *chip, int port, u8 *cmode)
369 int err;
370 u16 reg;
372 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_STS, &reg);
373 if (err)
374 return err;
376 *cmode = reg & MV88E6XXX_PORT_STS_CMODE_MASK;
378 return 0;
381 /* Offset 0x02: Jamming Control
383 * Do not limit the period of time that this port can be paused for by
384 * the remote end or the period of time that this port can pause the
385 * remote end.
387 int mv88e6097_port_pause_limit(struct mv88e6xxx_chip *chip, int port, u8 in,
388 u8 out)
390 return mv88e6xxx_port_write(chip, port, MV88E6097_PORT_JAM_CTL,
391 out << 8 | in);
394 int mv88e6390_port_pause_limit(struct mv88e6xxx_chip *chip, int port, u8 in,
395 u8 out)
397 int err;
399 err = mv88e6xxx_port_write(chip, port, MV88E6390_PORT_FLOW_CTL,
400 MV88E6390_PORT_FLOW_CTL_UPDATE |
401 MV88E6390_PORT_FLOW_CTL_LIMIT_IN | in);
402 if (err)
403 return err;
405 return mv88e6xxx_port_write(chip, port, MV88E6390_PORT_FLOW_CTL,
406 MV88E6390_PORT_FLOW_CTL_UPDATE |
407 MV88E6390_PORT_FLOW_CTL_LIMIT_OUT | out);
410 /* Offset 0x04: Port Control Register */
412 static const char * const mv88e6xxx_port_state_names[] = {
413 [MV88E6XXX_PORT_CTL0_STATE_DISABLED] = "Disabled",
414 [MV88E6XXX_PORT_CTL0_STATE_BLOCKING] = "Blocking/Listening",
415 [MV88E6XXX_PORT_CTL0_STATE_LEARNING] = "Learning",
416 [MV88E6XXX_PORT_CTL0_STATE_FORWARDING] = "Forwarding",
419 int mv88e6xxx_port_set_state(struct mv88e6xxx_chip *chip, int port, u8 state)
421 u16 reg;
422 int err;
424 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
425 if (err)
426 return err;
428 reg &= ~MV88E6XXX_PORT_CTL0_STATE_MASK;
430 switch (state) {
431 case BR_STATE_DISABLED:
432 state = MV88E6XXX_PORT_CTL0_STATE_DISABLED;
433 break;
434 case BR_STATE_BLOCKING:
435 case BR_STATE_LISTENING:
436 state = MV88E6XXX_PORT_CTL0_STATE_BLOCKING;
437 break;
438 case BR_STATE_LEARNING:
439 state = MV88E6XXX_PORT_CTL0_STATE_LEARNING;
440 break;
441 case BR_STATE_FORWARDING:
442 state = MV88E6XXX_PORT_CTL0_STATE_FORWARDING;
443 break;
444 default:
445 return -EINVAL;
448 reg |= state;
450 err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
451 if (err)
452 return err;
454 dev_dbg(chip->dev, "p%d: PortState set to %s\n", port,
455 mv88e6xxx_port_state_names[state]);
457 return 0;
460 int mv88e6xxx_port_set_egress_mode(struct mv88e6xxx_chip *chip, int port,
461 enum mv88e6xxx_egress_mode mode)
463 int err;
464 u16 reg;
466 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
467 if (err)
468 return err;
470 reg &= ~MV88E6XXX_PORT_CTL0_EGRESS_MODE_MASK;
472 switch (mode) {
473 case MV88E6XXX_EGRESS_MODE_UNMODIFIED:
474 reg |= MV88E6XXX_PORT_CTL0_EGRESS_MODE_UNMODIFIED;
475 break;
476 case MV88E6XXX_EGRESS_MODE_UNTAGGED:
477 reg |= MV88E6XXX_PORT_CTL0_EGRESS_MODE_UNTAGGED;
478 break;
479 case MV88E6XXX_EGRESS_MODE_TAGGED:
480 reg |= MV88E6XXX_PORT_CTL0_EGRESS_MODE_TAGGED;
481 break;
482 case MV88E6XXX_EGRESS_MODE_ETHERTYPE:
483 reg |= MV88E6XXX_PORT_CTL0_EGRESS_MODE_ETHER_TYPE_DSA;
484 break;
485 default:
486 return -EINVAL;
489 return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
492 int mv88e6085_port_set_frame_mode(struct mv88e6xxx_chip *chip, int port,
493 enum mv88e6xxx_frame_mode mode)
495 int err;
496 u16 reg;
498 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
499 if (err)
500 return err;
502 reg &= ~MV88E6XXX_PORT_CTL0_FRAME_MODE_MASK;
504 switch (mode) {
505 case MV88E6XXX_FRAME_MODE_NORMAL:
506 reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_NORMAL;
507 break;
508 case MV88E6XXX_FRAME_MODE_DSA:
509 reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_DSA;
510 break;
511 default:
512 return -EINVAL;
515 return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
518 int mv88e6351_port_set_frame_mode(struct mv88e6xxx_chip *chip, int port,
519 enum mv88e6xxx_frame_mode mode)
521 int err;
522 u16 reg;
524 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
525 if (err)
526 return err;
528 reg &= ~MV88E6XXX_PORT_CTL0_FRAME_MODE_MASK;
530 switch (mode) {
531 case MV88E6XXX_FRAME_MODE_NORMAL:
532 reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_NORMAL;
533 break;
534 case MV88E6XXX_FRAME_MODE_DSA:
535 reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_DSA;
536 break;
537 case MV88E6XXX_FRAME_MODE_PROVIDER:
538 reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_PROVIDER;
539 break;
540 case MV88E6XXX_FRAME_MODE_ETHERTYPE:
541 reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_ETHER_TYPE_DSA;
542 break;
543 default:
544 return -EINVAL;
547 return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
550 static int mv88e6185_port_set_forward_unknown(struct mv88e6xxx_chip *chip,
551 int port, bool unicast)
553 int err;
554 u16 reg;
556 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
557 if (err)
558 return err;
560 if (unicast)
561 reg |= MV88E6185_PORT_CTL0_FORWARD_UNKNOWN;
562 else
563 reg &= ~MV88E6185_PORT_CTL0_FORWARD_UNKNOWN;
565 return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
568 int mv88e6352_port_set_egress_floods(struct mv88e6xxx_chip *chip, int port,
569 bool unicast, bool multicast)
571 int err;
572 u16 reg;
574 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
575 if (err)
576 return err;
578 reg &= ~MV88E6352_PORT_CTL0_EGRESS_FLOODS_MASK;
580 if (unicast && multicast)
581 reg |= MV88E6352_PORT_CTL0_EGRESS_FLOODS_ALL_UNKNOWN_DA;
582 else if (unicast)
583 reg |= MV88E6352_PORT_CTL0_EGRESS_FLOODS_NO_UNKNOWN_MC_DA;
584 else if (multicast)
585 reg |= MV88E6352_PORT_CTL0_EGRESS_FLOODS_NO_UNKNOWN_UC_DA;
586 else
587 reg |= MV88E6352_PORT_CTL0_EGRESS_FLOODS_NO_UNKNOWN_DA;
589 return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
592 /* Offset 0x05: Port Control 1 */
594 int mv88e6xxx_port_set_message_port(struct mv88e6xxx_chip *chip, int port,
595 bool message_port)
597 u16 val;
598 int err;
600 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL1, &val);
601 if (err)
602 return err;
604 if (message_port)
605 val |= MV88E6XXX_PORT_CTL1_MESSAGE_PORT;
606 else
607 val &= ~MV88E6XXX_PORT_CTL1_MESSAGE_PORT;
609 return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL1, val);
612 /* Offset 0x06: Port Based VLAN Map */
614 int mv88e6xxx_port_set_vlan_map(struct mv88e6xxx_chip *chip, int port, u16 map)
616 const u16 mask = mv88e6xxx_port_mask(chip);
617 u16 reg;
618 int err;
620 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_BASE_VLAN, &reg);
621 if (err)
622 return err;
624 reg &= ~mask;
625 reg |= map & mask;
627 err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_BASE_VLAN, reg);
628 if (err)
629 return err;
631 dev_dbg(chip->dev, "p%d: VLANTable set to %.3x\n", port, map);
633 return 0;
636 int mv88e6xxx_port_get_fid(struct mv88e6xxx_chip *chip, int port, u16 *fid)
638 const u16 upper_mask = (mv88e6xxx_num_databases(chip) - 1) >> 4;
639 u16 reg;
640 int err;
642 /* Port's default FID lower 4 bits are located in reg 0x06, offset 12 */
643 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_BASE_VLAN, &reg);
644 if (err)
645 return err;
647 *fid = (reg & 0xf000) >> 12;
649 /* Port's default FID upper bits are located in reg 0x05, offset 0 */
650 if (upper_mask) {
651 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL1,
652 &reg);
653 if (err)
654 return err;
656 *fid |= (reg & upper_mask) << 4;
659 return 0;
662 int mv88e6xxx_port_set_fid(struct mv88e6xxx_chip *chip, int port, u16 fid)
664 const u16 upper_mask = (mv88e6xxx_num_databases(chip) - 1) >> 4;
665 u16 reg;
666 int err;
668 if (fid >= mv88e6xxx_num_databases(chip))
669 return -EINVAL;
671 /* Port's default FID lower 4 bits are located in reg 0x06, offset 12 */
672 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_BASE_VLAN, &reg);
673 if (err)
674 return err;
676 reg &= 0x0fff;
677 reg |= (fid & 0x000f) << 12;
679 err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_BASE_VLAN, reg);
680 if (err)
681 return err;
683 /* Port's default FID upper bits are located in reg 0x05, offset 0 */
684 if (upper_mask) {
685 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL1,
686 &reg);
687 if (err)
688 return err;
690 reg &= ~upper_mask;
691 reg |= (fid >> 4) & upper_mask;
693 err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL1,
694 reg);
695 if (err)
696 return err;
699 dev_dbg(chip->dev, "p%d: FID set to %u\n", port, fid);
701 return 0;
704 /* Offset 0x07: Default Port VLAN ID & Priority */
706 int mv88e6xxx_port_get_pvid(struct mv88e6xxx_chip *chip, int port, u16 *pvid)
708 u16 reg;
709 int err;
711 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_DEFAULT_VLAN,
712 &reg);
713 if (err)
714 return err;
716 *pvid = reg & MV88E6XXX_PORT_DEFAULT_VLAN_MASK;
718 return 0;
721 int mv88e6xxx_port_set_pvid(struct mv88e6xxx_chip *chip, int port, u16 pvid)
723 u16 reg;
724 int err;
726 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_DEFAULT_VLAN,
727 &reg);
728 if (err)
729 return err;
731 reg &= ~MV88E6XXX_PORT_DEFAULT_VLAN_MASK;
732 reg |= pvid & MV88E6XXX_PORT_DEFAULT_VLAN_MASK;
734 err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_DEFAULT_VLAN,
735 reg);
736 if (err)
737 return err;
739 dev_dbg(chip->dev, "p%d: DefaultVID set to %u\n", port, pvid);
741 return 0;
744 /* Offset 0x08: Port Control 2 Register */
746 static const char * const mv88e6xxx_port_8021q_mode_names[] = {
747 [MV88E6XXX_PORT_CTL2_8021Q_MODE_DISABLED] = "Disabled",
748 [MV88E6XXX_PORT_CTL2_8021Q_MODE_FALLBACK] = "Fallback",
749 [MV88E6XXX_PORT_CTL2_8021Q_MODE_CHECK] = "Check",
750 [MV88E6XXX_PORT_CTL2_8021Q_MODE_SECURE] = "Secure",
753 static int mv88e6185_port_set_default_forward(struct mv88e6xxx_chip *chip,
754 int port, bool multicast)
756 int err;
757 u16 reg;
759 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
760 if (err)
761 return err;
763 if (multicast)
764 reg |= MV88E6XXX_PORT_CTL2_DEFAULT_FORWARD;
765 else
766 reg &= ~MV88E6XXX_PORT_CTL2_DEFAULT_FORWARD;
768 return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
771 int mv88e6185_port_set_egress_floods(struct mv88e6xxx_chip *chip, int port,
772 bool unicast, bool multicast)
774 int err;
776 err = mv88e6185_port_set_forward_unknown(chip, port, unicast);
777 if (err)
778 return err;
780 return mv88e6185_port_set_default_forward(chip, port, multicast);
783 int mv88e6095_port_set_upstream_port(struct mv88e6xxx_chip *chip, int port,
784 int upstream_port)
786 int err;
787 u16 reg;
789 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
790 if (err)
791 return err;
793 reg &= ~MV88E6095_PORT_CTL2_CPU_PORT_MASK;
794 reg |= upstream_port;
796 return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
799 int mv88e6xxx_port_set_8021q_mode(struct mv88e6xxx_chip *chip, int port,
800 u16 mode)
802 u16 reg;
803 int err;
805 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
806 if (err)
807 return err;
809 reg &= ~MV88E6XXX_PORT_CTL2_8021Q_MODE_MASK;
810 reg |= mode & MV88E6XXX_PORT_CTL2_8021Q_MODE_MASK;
812 err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
813 if (err)
814 return err;
816 dev_dbg(chip->dev, "p%d: 802.1QMode set to %s\n", port,
817 mv88e6xxx_port_8021q_mode_names[mode]);
819 return 0;
822 int mv88e6xxx_port_set_map_da(struct mv88e6xxx_chip *chip, int port)
824 u16 reg;
825 int err;
827 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
828 if (err)
829 return err;
831 reg |= MV88E6XXX_PORT_CTL2_MAP_DA;
833 return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
836 int mv88e6165_port_set_jumbo_size(struct mv88e6xxx_chip *chip, int port,
837 size_t size)
839 u16 reg;
840 int err;
842 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
843 if (err)
844 return err;
846 reg &= ~MV88E6XXX_PORT_CTL2_JUMBO_MODE_MASK;
848 if (size <= 1522)
849 reg |= MV88E6XXX_PORT_CTL2_JUMBO_MODE_1522;
850 else if (size <= 2048)
851 reg |= MV88E6XXX_PORT_CTL2_JUMBO_MODE_2048;
852 else if (size <= 10240)
853 reg |= MV88E6XXX_PORT_CTL2_JUMBO_MODE_10240;
854 else
855 return -ERANGE;
857 return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
860 /* Offset 0x09: Port Rate Control */
862 int mv88e6095_port_egress_rate_limiting(struct mv88e6xxx_chip *chip, int port)
864 return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_EGRESS_RATE_CTL1,
865 0x0000);
868 int mv88e6097_port_egress_rate_limiting(struct mv88e6xxx_chip *chip, int port)
870 return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_EGRESS_RATE_CTL1,
871 0x0001);
874 /* Offset 0x0C: Port ATU Control */
876 int mv88e6xxx_port_disable_learn_limit(struct mv88e6xxx_chip *chip, int port)
878 return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_ATU_CTL, 0);
881 /* Offset 0x0D: (Priority) Override Register */
883 int mv88e6xxx_port_disable_pri_override(struct mv88e6xxx_chip *chip, int port)
885 return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_PRI_OVERRIDE, 0);
888 /* Offset 0x0f: Port Ether type */
890 int mv88e6351_port_set_ether_type(struct mv88e6xxx_chip *chip, int port,
891 u16 etype)
893 return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_ETH_TYPE, etype);
896 /* Offset 0x18: Port IEEE Priority Remapping Registers [0-3]
897 * Offset 0x19: Port IEEE Priority Remapping Registers [4-7]
900 int mv88e6095_port_tag_remap(struct mv88e6xxx_chip *chip, int port)
902 int err;
904 /* Use a direct priority mapping for all IEEE tagged frames */
905 err = mv88e6xxx_port_write(chip, port,
906 MV88E6095_PORT_IEEE_PRIO_REMAP_0123,
907 0x3210);
908 if (err)
909 return err;
911 return mv88e6xxx_port_write(chip, port,
912 MV88E6095_PORT_IEEE_PRIO_REMAP_4567,
913 0x7654);
916 static int mv88e6xxx_port_ieeepmt_write(struct mv88e6xxx_chip *chip,
917 int port, u16 table, u8 ptr, u16 data)
919 u16 reg;
921 reg = MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_UPDATE | table |
922 (ptr << __bf_shf(MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_PTR_MASK)) |
923 (data & MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_DATA_MASK);
925 return mv88e6xxx_port_write(chip, port,
926 MV88E6390_PORT_IEEE_PRIO_MAP_TABLE, reg);
929 int mv88e6390_port_tag_remap(struct mv88e6xxx_chip *chip, int port)
931 int err, i;
932 u16 table;
934 for (i = 0; i <= 7; i++) {
935 table = MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_INGRESS_PCP;
936 err = mv88e6xxx_port_ieeepmt_write(chip, port, table, i,
937 (i | i << 4));
938 if (err)
939 return err;
941 table = MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_EGRESS_GREEN_PCP;
942 err = mv88e6xxx_port_ieeepmt_write(chip, port, table, i, i);
943 if (err)
944 return err;
946 table = MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_EGRESS_YELLOW_PCP;
947 err = mv88e6xxx_port_ieeepmt_write(chip, port, table, i, i);
948 if (err)
949 return err;
951 table = MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_EGRESS_AVB_PCP;
952 err = mv88e6xxx_port_ieeepmt_write(chip, port, table, i, i);
953 if (err)
954 return err;
957 return 0;