xref: /linux/drivers/net/dsa/mv88e6xxx/port.c (revision 75b1a8f9d62e50f05d0e4e9f3c8bcde32527ffc1)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Marvell 88E6xxx Switch Port Registers support
4  *
5  * Copyright (c) 2008 Marvell Semiconductor
6  *
7  * Copyright (c) 2016-2017 Savoir-faire Linux Inc.
8  *	Vivien Didelot <vivien.didelot@savoirfairelinux.com>
9  */
10 
11 #include <linux/bitfield.h>
12 #include <linux/if_bridge.h>
13 #include <linux/phy.h>
14 #include <linux/phylink.h>
15 
16 #include "chip.h"
17 #include "port.h"
18 #include "serdes.h"
19 
20 int mv88e6xxx_port_read(struct mv88e6xxx_chip *chip, int port, int reg,
21 			u16 *val)
22 {
23 	int addr = chip->info->port_base_addr + port;
24 
25 	return mv88e6xxx_read(chip, addr, reg, val);
26 }
27 
28 int mv88e6xxx_port_write(struct mv88e6xxx_chip *chip, int port, int reg,
29 			 u16 val)
30 {
31 	int addr = chip->info->port_base_addr + port;
32 
33 	return mv88e6xxx_write(chip, addr, reg, val);
34 }
35 
36 /* Offset 0x00: MAC (or PCS or Physical) Status Register
37  *
38  * For most devices, this is read only. However the 6185 has the MyPause
39  * bit read/write.
40  */
41 int mv88e6185_port_set_pause(struct mv88e6xxx_chip *chip, int port,
42 			     int pause)
43 {
44 	u16 reg;
45 	int err;
46 
47 	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_STS, &reg);
48 	if (err)
49 		return err;
50 
51 	if (pause)
52 		reg |= MV88E6XXX_PORT_STS_MY_PAUSE;
53 	else
54 		reg &= ~MV88E6XXX_PORT_STS_MY_PAUSE;
55 
56 	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_STS, reg);
57 }
58 
59 /* Offset 0x01: MAC (or PCS or Physical) Control Register
60  *
61  * Link, Duplex and Flow Control have one force bit, one value bit.
62  *
63  * For port's MAC speed, ForceSpd (or SpdValue) bits 1:0 program the value.
64  * Alternative values require the 200BASE (or AltSpeed) bit 12 set.
65  * Newer chips need a ForcedSpd bit 13 set to consider the value.
66  */
67 
68 static int mv88e6xxx_port_set_rgmii_delay(struct mv88e6xxx_chip *chip, int port,
69 					  phy_interface_t mode)
70 {
71 	u16 reg;
72 	int err;
73 
74 	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_MAC_CTL, &reg);
75 	if (err)
76 		return err;
77 
78 	reg &= ~(MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_RXCLK |
79 		 MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_TXCLK);
80 
81 	switch (mode) {
82 	case PHY_INTERFACE_MODE_RGMII_RXID:
83 		reg |= MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_RXCLK;
84 		break;
85 	case PHY_INTERFACE_MODE_RGMII_TXID:
86 		reg |= MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_TXCLK;
87 		break;
88 	case PHY_INTERFACE_MODE_RGMII_ID:
89 		reg |= MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_RXCLK |
90 			MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_TXCLK;
91 		break;
92 	case PHY_INTERFACE_MODE_RGMII:
93 		break;
94 	default:
95 		return 0;
96 	}
97 
98 	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_MAC_CTL, reg);
99 	if (err)
100 		return err;
101 
102 	dev_dbg(chip->dev, "p%d: delay RXCLK %s, TXCLK %s\n", port,
103 		reg & MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_RXCLK ? "yes" : "no",
104 		reg & MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_TXCLK ? "yes" : "no");
105 
106 	return 0;
107 }
108 
109 int mv88e6352_port_set_rgmii_delay(struct mv88e6xxx_chip *chip, int port,
110 				   phy_interface_t mode)
111 {
112 	if (port < 5)
113 		return -EOPNOTSUPP;
114 
115 	return mv88e6xxx_port_set_rgmii_delay(chip, port, mode);
116 }
117 
118 int mv88e6390_port_set_rgmii_delay(struct mv88e6xxx_chip *chip, int port,
119 				   phy_interface_t mode)
120 {
121 	if (port != 0)
122 		return -EOPNOTSUPP;
123 
124 	return mv88e6xxx_port_set_rgmii_delay(chip, port, mode);
125 }
126 
127 int mv88e6xxx_port_set_link(struct mv88e6xxx_chip *chip, int port, int link)
128 {
129 	u16 reg;
130 	int err;
131 
132 	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_MAC_CTL, &reg);
133 	if (err)
134 		return err;
135 
136 	reg &= ~(MV88E6XXX_PORT_MAC_CTL_FORCE_LINK |
137 		 MV88E6XXX_PORT_MAC_CTL_LINK_UP);
138 
139 	switch (link) {
140 	case LINK_FORCED_DOWN:
141 		reg |= MV88E6XXX_PORT_MAC_CTL_FORCE_LINK;
142 		break;
143 	case LINK_FORCED_UP:
144 		reg |= MV88E6XXX_PORT_MAC_CTL_FORCE_LINK |
145 			MV88E6XXX_PORT_MAC_CTL_LINK_UP;
146 		break;
147 	case LINK_UNFORCED:
148 		/* normal link detection */
149 		break;
150 	default:
151 		return -EINVAL;
152 	}
153 
154 	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_MAC_CTL, reg);
155 	if (err)
156 		return err;
157 
158 	dev_dbg(chip->dev, "p%d: %s link %s\n", port,
159 		reg & MV88E6XXX_PORT_MAC_CTL_FORCE_LINK ? "Force" : "Unforce",
160 		reg & MV88E6XXX_PORT_MAC_CTL_LINK_UP ? "up" : "down");
161 
162 	return 0;
163 }
164 
165 int mv88e6xxx_port_sync_link(struct mv88e6xxx_chip *chip, int port, unsigned int mode, bool isup)
166 {
167 	const struct mv88e6xxx_ops *ops = chip->info->ops;
168 	int err = 0;
169 	int link;
170 
171 	if (isup)
172 		link = LINK_FORCED_UP;
173 	else
174 		link = LINK_FORCED_DOWN;
175 
176 	if (ops->port_set_link)
177 		err = ops->port_set_link(chip, port, link);
178 
179 	return err;
180 }
181 
182 int mv88e6185_port_sync_link(struct mv88e6xxx_chip *chip, int port, unsigned int mode, bool isup)
183 {
184 	const struct mv88e6xxx_ops *ops = chip->info->ops;
185 	int err = 0;
186 	int link;
187 
188 	if (mode == MLO_AN_INBAND)
189 		link = LINK_UNFORCED;
190 	else if (isup)
191 		link = LINK_FORCED_UP;
192 	else
193 		link = LINK_FORCED_DOWN;
194 
195 	if (ops->port_set_link)
196 		err = ops->port_set_link(chip, port, link);
197 
198 	return err;
199 }
200 
201 static int mv88e6xxx_port_set_speed_duplex(struct mv88e6xxx_chip *chip,
202 					   int port, int speed, bool alt_bit,
203 					   bool force_bit, int duplex)
204 {
205 	u16 reg, ctrl;
206 	int err;
207 
208 	switch (speed) {
209 	case 10:
210 		ctrl = MV88E6XXX_PORT_MAC_CTL_SPEED_10;
211 		break;
212 	case 100:
213 		ctrl = MV88E6XXX_PORT_MAC_CTL_SPEED_100;
214 		break;
215 	case 200:
216 		if (alt_bit)
217 			ctrl = MV88E6XXX_PORT_MAC_CTL_SPEED_100 |
218 				MV88E6390_PORT_MAC_CTL_ALTSPEED;
219 		else
220 			ctrl = MV88E6065_PORT_MAC_CTL_SPEED_200;
221 		break;
222 	case 1000:
223 		ctrl = MV88E6XXX_PORT_MAC_CTL_SPEED_1000;
224 		break;
225 	case 2500:
226 		if (alt_bit)
227 			ctrl = MV88E6390_PORT_MAC_CTL_SPEED_10000 |
228 				MV88E6390_PORT_MAC_CTL_ALTSPEED;
229 		else
230 			ctrl = MV88E6390_PORT_MAC_CTL_SPEED_10000;
231 		break;
232 	case 10000:
233 		/* all bits set, fall through... */
234 	case SPEED_UNFORCED:
235 		ctrl = MV88E6XXX_PORT_MAC_CTL_SPEED_UNFORCED;
236 		break;
237 	default:
238 		return -EOPNOTSUPP;
239 	}
240 
241 	switch (duplex) {
242 	case DUPLEX_HALF:
243 		ctrl |= MV88E6XXX_PORT_MAC_CTL_FORCE_DUPLEX;
244 		break;
245 	case DUPLEX_FULL:
246 		ctrl |= MV88E6XXX_PORT_MAC_CTL_FORCE_DUPLEX |
247 			MV88E6XXX_PORT_MAC_CTL_DUPLEX_FULL;
248 		break;
249 	case DUPLEX_UNFORCED:
250 		/* normal duplex detection */
251 		break;
252 	default:
253 		return -EOPNOTSUPP;
254 	}
255 
256 	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_MAC_CTL, &reg);
257 	if (err)
258 		return err;
259 
260 	reg &= ~(MV88E6XXX_PORT_MAC_CTL_SPEED_MASK |
261 		 MV88E6XXX_PORT_MAC_CTL_FORCE_DUPLEX |
262 		 MV88E6XXX_PORT_MAC_CTL_DUPLEX_FULL);
263 
264 	if (alt_bit)
265 		reg &= ~MV88E6390_PORT_MAC_CTL_ALTSPEED;
266 	if (force_bit) {
267 		reg &= ~MV88E6390_PORT_MAC_CTL_FORCE_SPEED;
268 		if (speed != SPEED_UNFORCED)
269 			ctrl |= MV88E6390_PORT_MAC_CTL_FORCE_SPEED;
270 	}
271 	reg |= ctrl;
272 
273 	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_MAC_CTL, reg);
274 	if (err)
275 		return err;
276 
277 	if (speed)
278 		dev_dbg(chip->dev, "p%d: Speed set to %d Mbps\n", port, speed);
279 	else
280 		dev_dbg(chip->dev, "p%d: Speed unforced\n", port);
281 	dev_dbg(chip->dev, "p%d: %s %s duplex\n", port,
282 		reg & MV88E6XXX_PORT_MAC_CTL_FORCE_DUPLEX ? "Force" : "Unforce",
283 		reg & MV88E6XXX_PORT_MAC_CTL_DUPLEX_FULL ? "full" : "half");
284 
285 	return 0;
286 }
287 
288 /* Support 10, 100, 200 Mbps (e.g. 88E6065 family) */
289 int mv88e6065_port_set_speed_duplex(struct mv88e6xxx_chip *chip, int port,
290 				    int speed, int duplex)
291 {
292 	if (speed == SPEED_MAX)
293 		speed = 200;
294 
295 	if (speed > 200)
296 		return -EOPNOTSUPP;
297 
298 	/* Setting 200 Mbps on port 0 to 3 selects 100 Mbps */
299 	return mv88e6xxx_port_set_speed_duplex(chip, port, speed, false, false,
300 					       duplex);
301 }
302 
303 /* Support 10, 100, 1000 Mbps (e.g. 88E6185 family) */
304 int mv88e6185_port_set_speed_duplex(struct mv88e6xxx_chip *chip, int port,
305 				    int speed, int duplex)
306 {
307 	if (speed == SPEED_MAX)
308 		speed = 1000;
309 
310 	if (speed == 200 || speed > 1000)
311 		return -EOPNOTSUPP;
312 
313 	return mv88e6xxx_port_set_speed_duplex(chip, port, speed, false, false,
314 					       duplex);
315 }
316 
317 /* Support 10, 100 Mbps (e.g. 88E6250 family) */
318 int mv88e6250_port_set_speed_duplex(struct mv88e6xxx_chip *chip, int port,
319 				    int speed, int duplex)
320 {
321 	if (speed == SPEED_MAX)
322 		speed = 100;
323 
324 	if (speed > 100)
325 		return -EOPNOTSUPP;
326 
327 	return mv88e6xxx_port_set_speed_duplex(chip, port, speed, false, false,
328 					       duplex);
329 }
330 
331 /* Support 10, 100, 200, 1000, 2500 Mbps (e.g. 88E6341) */
332 int mv88e6341_port_set_speed_duplex(struct mv88e6xxx_chip *chip, int port,
333 				    int speed, int duplex)
334 {
335 	if (speed == SPEED_MAX)
336 		speed = port < 5 ? 1000 : 2500;
337 
338 	if (speed > 2500)
339 		return -EOPNOTSUPP;
340 
341 	if (speed == 200 && port != 0)
342 		return -EOPNOTSUPP;
343 
344 	if (speed == 2500 && port < 5)
345 		return -EOPNOTSUPP;
346 
347 	return mv88e6xxx_port_set_speed_duplex(chip, port, speed, !port, true,
348 					       duplex);
349 }
350 
351 phy_interface_t mv88e6341_port_max_speed_mode(int port)
352 {
353 	if (port == 5)
354 		return PHY_INTERFACE_MODE_2500BASEX;
355 
356 	return PHY_INTERFACE_MODE_NA;
357 }
358 
359 /* Support 10, 100, 200, 1000 Mbps (e.g. 88E6352 family) */
360 int mv88e6352_port_set_speed_duplex(struct mv88e6xxx_chip *chip, int port,
361 				    int speed, int duplex)
362 {
363 	if (speed == SPEED_MAX)
364 		speed = 1000;
365 
366 	if (speed > 1000)
367 		return -EOPNOTSUPP;
368 
369 	if (speed == 200 && port < 5)
370 		return -EOPNOTSUPP;
371 
372 	return mv88e6xxx_port_set_speed_duplex(chip, port, speed, true, false,
373 					       duplex);
374 }
375 
376 /* Support 10, 100, 200, 1000, 2500 Mbps (e.g. 88E6390) */
377 int mv88e6390_port_set_speed_duplex(struct mv88e6xxx_chip *chip, int port,
378 				    int speed, int duplex)
379 {
380 	if (speed == SPEED_MAX)
381 		speed = port < 9 ? 1000 : 2500;
382 
383 	if (speed > 2500)
384 		return -EOPNOTSUPP;
385 
386 	if (speed == 200 && port != 0)
387 		return -EOPNOTSUPP;
388 
389 	if (speed == 2500 && port < 9)
390 		return -EOPNOTSUPP;
391 
392 	return mv88e6xxx_port_set_speed_duplex(chip, port, speed, true, true,
393 					       duplex);
394 }
395 
396 phy_interface_t mv88e6390_port_max_speed_mode(int port)
397 {
398 	if (port == 9 || port == 10)
399 		return PHY_INTERFACE_MODE_2500BASEX;
400 
401 	return PHY_INTERFACE_MODE_NA;
402 }
403 
404 /* Support 10, 100, 200, 1000, 2500, 10000 Mbps (e.g. 88E6190X) */
405 int mv88e6390x_port_set_speed_duplex(struct mv88e6xxx_chip *chip, int port,
406 				     int speed, int duplex)
407 {
408 	if (speed == SPEED_MAX)
409 		speed = port < 9 ? 1000 : 10000;
410 
411 	if (speed == 200 && port != 0)
412 		return -EOPNOTSUPP;
413 
414 	if (speed >= 2500 && port < 9)
415 		return -EOPNOTSUPP;
416 
417 	return mv88e6xxx_port_set_speed_duplex(chip, port, speed, true, true,
418 					       duplex);
419 }
420 
421 phy_interface_t mv88e6390x_port_max_speed_mode(int port)
422 {
423 	if (port == 9 || port == 10)
424 		return PHY_INTERFACE_MODE_XAUI;
425 
426 	return PHY_INTERFACE_MODE_NA;
427 }
428 
429 static int mv88e6xxx_port_set_cmode(struct mv88e6xxx_chip *chip, int port,
430 				    phy_interface_t mode, bool force)
431 {
432 	u8 lane;
433 	u16 cmode;
434 	u16 reg;
435 	int err;
436 
437 	/* Default to a slow mode, so freeing up SERDES interfaces for
438 	 * other ports which might use them for SFPs.
439 	 */
440 	if (mode == PHY_INTERFACE_MODE_NA)
441 		mode = PHY_INTERFACE_MODE_1000BASEX;
442 
443 	switch (mode) {
444 	case PHY_INTERFACE_MODE_1000BASEX:
445 		cmode = MV88E6XXX_PORT_STS_CMODE_1000BASEX;
446 		break;
447 	case PHY_INTERFACE_MODE_SGMII:
448 		cmode = MV88E6XXX_PORT_STS_CMODE_SGMII;
449 		break;
450 	case PHY_INTERFACE_MODE_2500BASEX:
451 		cmode = MV88E6XXX_PORT_STS_CMODE_2500BASEX;
452 		break;
453 	case PHY_INTERFACE_MODE_XGMII:
454 	case PHY_INTERFACE_MODE_XAUI:
455 		cmode = MV88E6XXX_PORT_STS_CMODE_XAUI;
456 		break;
457 	case PHY_INTERFACE_MODE_RXAUI:
458 		cmode = MV88E6XXX_PORT_STS_CMODE_RXAUI;
459 		break;
460 	default:
461 		cmode = 0;
462 	}
463 
464 	/* cmode doesn't change, nothing to do for us unless forced */
465 	if (cmode == chip->ports[port].cmode && !force)
466 		return 0;
467 
468 	lane = mv88e6xxx_serdes_get_lane(chip, port);
469 	if (lane) {
470 		if (chip->ports[port].serdes_irq) {
471 			err = mv88e6xxx_serdes_irq_disable(chip, port, lane);
472 			if (err)
473 				return err;
474 		}
475 
476 		err = mv88e6xxx_serdes_power_down(chip, port, lane);
477 		if (err)
478 			return err;
479 	}
480 
481 	chip->ports[port].cmode = 0;
482 
483 	if (cmode) {
484 		err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_STS, &reg);
485 		if (err)
486 			return err;
487 
488 		reg &= ~MV88E6XXX_PORT_STS_CMODE_MASK;
489 		reg |= cmode;
490 
491 		err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_STS, reg);
492 		if (err)
493 			return err;
494 
495 		chip->ports[port].cmode = cmode;
496 
497 		lane = mv88e6xxx_serdes_get_lane(chip, port);
498 		if (!lane)
499 			return -ENODEV;
500 
501 		err = mv88e6xxx_serdes_power_up(chip, port, lane);
502 		if (err)
503 			return err;
504 
505 		if (chip->ports[port].serdes_irq) {
506 			err = mv88e6xxx_serdes_irq_enable(chip, port, lane);
507 			if (err)
508 				return err;
509 		}
510 	}
511 
512 	return 0;
513 }
514 
515 int mv88e6390x_port_set_cmode(struct mv88e6xxx_chip *chip, int port,
516 			      phy_interface_t mode)
517 {
518 	if (port != 9 && port != 10)
519 		return -EOPNOTSUPP;
520 
521 	return mv88e6xxx_port_set_cmode(chip, port, mode, false);
522 }
523 
524 int mv88e6390_port_set_cmode(struct mv88e6xxx_chip *chip, int port,
525 			     phy_interface_t mode)
526 {
527 	if (port != 9 && port != 10)
528 		return -EOPNOTSUPP;
529 
530 	switch (mode) {
531 	case PHY_INTERFACE_MODE_NA:
532 		return 0;
533 	case PHY_INTERFACE_MODE_XGMII:
534 	case PHY_INTERFACE_MODE_XAUI:
535 	case PHY_INTERFACE_MODE_RXAUI:
536 		return -EINVAL;
537 	default:
538 		break;
539 	}
540 
541 	return mv88e6xxx_port_set_cmode(chip, port, mode, false);
542 }
543 
544 static int mv88e6341_port_set_cmode_writable(struct mv88e6xxx_chip *chip,
545 					     int port)
546 {
547 	int err, addr;
548 	u16 reg, bits;
549 
550 	if (port != 5)
551 		return -EOPNOTSUPP;
552 
553 	addr = chip->info->port_base_addr + port;
554 
555 	err = mv88e6xxx_port_hidden_read(chip, 0x7, addr, 0, &reg);
556 	if (err)
557 		return err;
558 
559 	bits = MV88E6341_PORT_RESERVED_1A_FORCE_CMODE |
560 	       MV88E6341_PORT_RESERVED_1A_SGMII_AN;
561 
562 	if ((reg & bits) == bits)
563 		return 0;
564 
565 	reg |= bits;
566 	return mv88e6xxx_port_hidden_write(chip, 0x7, addr, 0, reg);
567 }
568 
569 int mv88e6341_port_set_cmode(struct mv88e6xxx_chip *chip, int port,
570 			     phy_interface_t mode)
571 {
572 	int err;
573 
574 	if (port != 5)
575 		return -EOPNOTSUPP;
576 
577 	switch (mode) {
578 	case PHY_INTERFACE_MODE_NA:
579 		return 0;
580 	case PHY_INTERFACE_MODE_XGMII:
581 	case PHY_INTERFACE_MODE_XAUI:
582 	case PHY_INTERFACE_MODE_RXAUI:
583 		return -EINVAL;
584 	default:
585 		break;
586 	}
587 
588 	err = mv88e6341_port_set_cmode_writable(chip, port);
589 	if (err)
590 		return err;
591 
592 	return mv88e6xxx_port_set_cmode(chip, port, mode, true);
593 }
594 
595 int mv88e6185_port_get_cmode(struct mv88e6xxx_chip *chip, int port, u8 *cmode)
596 {
597 	int err;
598 	u16 reg;
599 
600 	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_STS, &reg);
601 	if (err)
602 		return err;
603 
604 	*cmode = reg & MV88E6185_PORT_STS_CMODE_MASK;
605 
606 	return 0;
607 }
608 
609 int mv88e6352_port_get_cmode(struct mv88e6xxx_chip *chip, int port, u8 *cmode)
610 {
611 	int err;
612 	u16 reg;
613 
614 	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_STS, &reg);
615 	if (err)
616 		return err;
617 
618 	*cmode = reg & MV88E6XXX_PORT_STS_CMODE_MASK;
619 
620 	return 0;
621 }
622 
623 /* Offset 0x02: Jamming Control
624  *
625  * Do not limit the period of time that this port can be paused for by
626  * the remote end or the period of time that this port can pause the
627  * remote end.
628  */
629 int mv88e6097_port_pause_limit(struct mv88e6xxx_chip *chip, int port, u8 in,
630 			       u8 out)
631 {
632 	return mv88e6xxx_port_write(chip, port, MV88E6097_PORT_JAM_CTL,
633 				    out << 8 | in);
634 }
635 
636 int mv88e6390_port_pause_limit(struct mv88e6xxx_chip *chip, int port, u8 in,
637 			       u8 out)
638 {
639 	int err;
640 
641 	err = mv88e6xxx_port_write(chip, port, MV88E6390_PORT_FLOW_CTL,
642 				   MV88E6390_PORT_FLOW_CTL_UPDATE |
643 				   MV88E6390_PORT_FLOW_CTL_LIMIT_IN | in);
644 	if (err)
645 		return err;
646 
647 	return mv88e6xxx_port_write(chip, port, MV88E6390_PORT_FLOW_CTL,
648 				    MV88E6390_PORT_FLOW_CTL_UPDATE |
649 				    MV88E6390_PORT_FLOW_CTL_LIMIT_OUT | out);
650 }
651 
652 /* Offset 0x04: Port Control Register */
653 
654 static const char * const mv88e6xxx_port_state_names[] = {
655 	[MV88E6XXX_PORT_CTL0_STATE_DISABLED] = "Disabled",
656 	[MV88E6XXX_PORT_CTL0_STATE_BLOCKING] = "Blocking/Listening",
657 	[MV88E6XXX_PORT_CTL0_STATE_LEARNING] = "Learning",
658 	[MV88E6XXX_PORT_CTL0_STATE_FORWARDING] = "Forwarding",
659 };
660 
661 int mv88e6xxx_port_set_state(struct mv88e6xxx_chip *chip, int port, u8 state)
662 {
663 	u16 reg;
664 	int err;
665 
666 	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
667 	if (err)
668 		return err;
669 
670 	reg &= ~MV88E6XXX_PORT_CTL0_STATE_MASK;
671 
672 	switch (state) {
673 	case BR_STATE_DISABLED:
674 		state = MV88E6XXX_PORT_CTL0_STATE_DISABLED;
675 		break;
676 	case BR_STATE_BLOCKING:
677 	case BR_STATE_LISTENING:
678 		state = MV88E6XXX_PORT_CTL0_STATE_BLOCKING;
679 		break;
680 	case BR_STATE_LEARNING:
681 		state = MV88E6XXX_PORT_CTL0_STATE_LEARNING;
682 		break;
683 	case BR_STATE_FORWARDING:
684 		state = MV88E6XXX_PORT_CTL0_STATE_FORWARDING;
685 		break;
686 	default:
687 		return -EINVAL;
688 	}
689 
690 	reg |= state;
691 
692 	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
693 	if (err)
694 		return err;
695 
696 	dev_dbg(chip->dev, "p%d: PortState set to %s\n", port,
697 		mv88e6xxx_port_state_names[state]);
698 
699 	return 0;
700 }
701 
702 int mv88e6xxx_port_set_egress_mode(struct mv88e6xxx_chip *chip, int port,
703 				   enum mv88e6xxx_egress_mode mode)
704 {
705 	int err;
706 	u16 reg;
707 
708 	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
709 	if (err)
710 		return err;
711 
712 	reg &= ~MV88E6XXX_PORT_CTL0_EGRESS_MODE_MASK;
713 
714 	switch (mode) {
715 	case MV88E6XXX_EGRESS_MODE_UNMODIFIED:
716 		reg |= MV88E6XXX_PORT_CTL0_EGRESS_MODE_UNMODIFIED;
717 		break;
718 	case MV88E6XXX_EGRESS_MODE_UNTAGGED:
719 		reg |= MV88E6XXX_PORT_CTL0_EGRESS_MODE_UNTAGGED;
720 		break;
721 	case MV88E6XXX_EGRESS_MODE_TAGGED:
722 		reg |= MV88E6XXX_PORT_CTL0_EGRESS_MODE_TAGGED;
723 		break;
724 	case MV88E6XXX_EGRESS_MODE_ETHERTYPE:
725 		reg |= MV88E6XXX_PORT_CTL0_EGRESS_MODE_ETHER_TYPE_DSA;
726 		break;
727 	default:
728 		return -EINVAL;
729 	}
730 
731 	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
732 }
733 
734 int mv88e6085_port_set_frame_mode(struct mv88e6xxx_chip *chip, int port,
735 				  enum mv88e6xxx_frame_mode mode)
736 {
737 	int err;
738 	u16 reg;
739 
740 	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
741 	if (err)
742 		return err;
743 
744 	reg &= ~MV88E6XXX_PORT_CTL0_FRAME_MODE_MASK;
745 
746 	switch (mode) {
747 	case MV88E6XXX_FRAME_MODE_NORMAL:
748 		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_NORMAL;
749 		break;
750 	case MV88E6XXX_FRAME_MODE_DSA:
751 		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_DSA;
752 		break;
753 	default:
754 		return -EINVAL;
755 	}
756 
757 	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
758 }
759 
760 int mv88e6351_port_set_frame_mode(struct mv88e6xxx_chip *chip, int port,
761 				  enum mv88e6xxx_frame_mode mode)
762 {
763 	int err;
764 	u16 reg;
765 
766 	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
767 	if (err)
768 		return err;
769 
770 	reg &= ~MV88E6XXX_PORT_CTL0_FRAME_MODE_MASK;
771 
772 	switch (mode) {
773 	case MV88E6XXX_FRAME_MODE_NORMAL:
774 		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_NORMAL;
775 		break;
776 	case MV88E6XXX_FRAME_MODE_DSA:
777 		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_DSA;
778 		break;
779 	case MV88E6XXX_FRAME_MODE_PROVIDER:
780 		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_PROVIDER;
781 		break;
782 	case MV88E6XXX_FRAME_MODE_ETHERTYPE:
783 		reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_ETHER_TYPE_DSA;
784 		break;
785 	default:
786 		return -EINVAL;
787 	}
788 
789 	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
790 }
791 
792 static int mv88e6185_port_set_forward_unknown(struct mv88e6xxx_chip *chip,
793 					      int port, bool unicast)
794 {
795 	int err;
796 	u16 reg;
797 
798 	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
799 	if (err)
800 		return err;
801 
802 	if (unicast)
803 		reg |= MV88E6185_PORT_CTL0_FORWARD_UNKNOWN;
804 	else
805 		reg &= ~MV88E6185_PORT_CTL0_FORWARD_UNKNOWN;
806 
807 	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
808 }
809 
810 int mv88e6352_port_set_egress_floods(struct mv88e6xxx_chip *chip, int port,
811 				     bool unicast, bool multicast)
812 {
813 	int err;
814 	u16 reg;
815 
816 	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
817 	if (err)
818 		return err;
819 
820 	reg &= ~MV88E6352_PORT_CTL0_EGRESS_FLOODS_MASK;
821 
822 	if (unicast && multicast)
823 		reg |= MV88E6352_PORT_CTL0_EGRESS_FLOODS_ALL_UNKNOWN_DA;
824 	else if (unicast)
825 		reg |= MV88E6352_PORT_CTL0_EGRESS_FLOODS_NO_UNKNOWN_MC_DA;
826 	else if (multicast)
827 		reg |= MV88E6352_PORT_CTL0_EGRESS_FLOODS_NO_UNKNOWN_UC_DA;
828 	else
829 		reg |= MV88E6352_PORT_CTL0_EGRESS_FLOODS_NO_UNKNOWN_DA;
830 
831 	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
832 }
833 
834 /* Offset 0x05: Port Control 1 */
835 
836 int mv88e6xxx_port_set_message_port(struct mv88e6xxx_chip *chip, int port,
837 				    bool message_port)
838 {
839 	u16 val;
840 	int err;
841 
842 	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL1, &val);
843 	if (err)
844 		return err;
845 
846 	if (message_port)
847 		val |= MV88E6XXX_PORT_CTL1_MESSAGE_PORT;
848 	else
849 		val &= ~MV88E6XXX_PORT_CTL1_MESSAGE_PORT;
850 
851 	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL1, val);
852 }
853 
854 /* Offset 0x06: Port Based VLAN Map */
855 
856 int mv88e6xxx_port_set_vlan_map(struct mv88e6xxx_chip *chip, int port, u16 map)
857 {
858 	const u16 mask = mv88e6xxx_port_mask(chip);
859 	u16 reg;
860 	int err;
861 
862 	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_BASE_VLAN, &reg);
863 	if (err)
864 		return err;
865 
866 	reg &= ~mask;
867 	reg |= map & mask;
868 
869 	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_BASE_VLAN, reg);
870 	if (err)
871 		return err;
872 
873 	dev_dbg(chip->dev, "p%d: VLANTable set to %.3x\n", port, map);
874 
875 	return 0;
876 }
877 
878 int mv88e6xxx_port_get_fid(struct mv88e6xxx_chip *chip, int port, u16 *fid)
879 {
880 	const u16 upper_mask = (mv88e6xxx_num_databases(chip) - 1) >> 4;
881 	u16 reg;
882 	int err;
883 
884 	/* Port's default FID lower 4 bits are located in reg 0x06, offset 12 */
885 	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_BASE_VLAN, &reg);
886 	if (err)
887 		return err;
888 
889 	*fid = (reg & 0xf000) >> 12;
890 
891 	/* Port's default FID upper bits are located in reg 0x05, offset 0 */
892 	if (upper_mask) {
893 		err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL1,
894 					  &reg);
895 		if (err)
896 			return err;
897 
898 		*fid |= (reg & upper_mask) << 4;
899 	}
900 
901 	return 0;
902 }
903 
904 int mv88e6xxx_port_set_fid(struct mv88e6xxx_chip *chip, int port, u16 fid)
905 {
906 	const u16 upper_mask = (mv88e6xxx_num_databases(chip) - 1) >> 4;
907 	u16 reg;
908 	int err;
909 
910 	if (fid >= mv88e6xxx_num_databases(chip))
911 		return -EINVAL;
912 
913 	/* Port's default FID lower 4 bits are located in reg 0x06, offset 12 */
914 	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_BASE_VLAN, &reg);
915 	if (err)
916 		return err;
917 
918 	reg &= 0x0fff;
919 	reg |= (fid & 0x000f) << 12;
920 
921 	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_BASE_VLAN, reg);
922 	if (err)
923 		return err;
924 
925 	/* Port's default FID upper bits are located in reg 0x05, offset 0 */
926 	if (upper_mask) {
927 		err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL1,
928 					  &reg);
929 		if (err)
930 			return err;
931 
932 		reg &= ~upper_mask;
933 		reg |= (fid >> 4) & upper_mask;
934 
935 		err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL1,
936 					   reg);
937 		if (err)
938 			return err;
939 	}
940 
941 	dev_dbg(chip->dev, "p%d: FID set to %u\n", port, fid);
942 
943 	return 0;
944 }
945 
946 /* Offset 0x07: Default Port VLAN ID & Priority */
947 
948 int mv88e6xxx_port_get_pvid(struct mv88e6xxx_chip *chip, int port, u16 *pvid)
949 {
950 	u16 reg;
951 	int err;
952 
953 	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_DEFAULT_VLAN,
954 				  &reg);
955 	if (err)
956 		return err;
957 
958 	*pvid = reg & MV88E6XXX_PORT_DEFAULT_VLAN_MASK;
959 
960 	return 0;
961 }
962 
963 int mv88e6xxx_port_set_pvid(struct mv88e6xxx_chip *chip, int port, u16 pvid)
964 {
965 	u16 reg;
966 	int err;
967 
968 	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_DEFAULT_VLAN,
969 				  &reg);
970 	if (err)
971 		return err;
972 
973 	reg &= ~MV88E6XXX_PORT_DEFAULT_VLAN_MASK;
974 	reg |= pvid & MV88E6XXX_PORT_DEFAULT_VLAN_MASK;
975 
976 	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_DEFAULT_VLAN,
977 				   reg);
978 	if (err)
979 		return err;
980 
981 	dev_dbg(chip->dev, "p%d: DefaultVID set to %u\n", port, pvid);
982 
983 	return 0;
984 }
985 
986 /* Offset 0x08: Port Control 2 Register */
987 
988 static const char * const mv88e6xxx_port_8021q_mode_names[] = {
989 	[MV88E6XXX_PORT_CTL2_8021Q_MODE_DISABLED] = "Disabled",
990 	[MV88E6XXX_PORT_CTL2_8021Q_MODE_FALLBACK] = "Fallback",
991 	[MV88E6XXX_PORT_CTL2_8021Q_MODE_CHECK] = "Check",
992 	[MV88E6XXX_PORT_CTL2_8021Q_MODE_SECURE] = "Secure",
993 };
994 
995 static int mv88e6185_port_set_default_forward(struct mv88e6xxx_chip *chip,
996 					      int port, bool multicast)
997 {
998 	int err;
999 	u16 reg;
1000 
1001 	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
1002 	if (err)
1003 		return err;
1004 
1005 	if (multicast)
1006 		reg |= MV88E6XXX_PORT_CTL2_DEFAULT_FORWARD;
1007 	else
1008 		reg &= ~MV88E6XXX_PORT_CTL2_DEFAULT_FORWARD;
1009 
1010 	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
1011 }
1012 
1013 int mv88e6185_port_set_egress_floods(struct mv88e6xxx_chip *chip, int port,
1014 				     bool unicast, bool multicast)
1015 {
1016 	int err;
1017 
1018 	err = mv88e6185_port_set_forward_unknown(chip, port, unicast);
1019 	if (err)
1020 		return err;
1021 
1022 	return mv88e6185_port_set_default_forward(chip, port, multicast);
1023 }
1024 
1025 int mv88e6095_port_set_upstream_port(struct mv88e6xxx_chip *chip, int port,
1026 				     int upstream_port)
1027 {
1028 	int err;
1029 	u16 reg;
1030 
1031 	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
1032 	if (err)
1033 		return err;
1034 
1035 	reg &= ~MV88E6095_PORT_CTL2_CPU_PORT_MASK;
1036 	reg |= upstream_port;
1037 
1038 	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
1039 }
1040 
1041 int mv88e6xxx_port_set_mirror(struct mv88e6xxx_chip *chip, int port,
1042 			      enum mv88e6xxx_egress_direction direction,
1043 			      bool mirror)
1044 {
1045 	bool *mirror_port;
1046 	u16 reg;
1047 	u16 bit;
1048 	int err;
1049 
1050 	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
1051 	if (err)
1052 		return err;
1053 
1054 	switch (direction) {
1055 	case MV88E6XXX_EGRESS_DIR_INGRESS:
1056 		bit = MV88E6XXX_PORT_CTL2_INGRESS_MONITOR;
1057 		mirror_port = &chip->ports[port].mirror_ingress;
1058 		break;
1059 	case MV88E6XXX_EGRESS_DIR_EGRESS:
1060 		bit = MV88E6XXX_PORT_CTL2_EGRESS_MONITOR;
1061 		mirror_port = &chip->ports[port].mirror_egress;
1062 		break;
1063 	default:
1064 		return -EINVAL;
1065 	}
1066 
1067 	reg &= ~bit;
1068 	if (mirror)
1069 		reg |= bit;
1070 
1071 	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
1072 	if (!err)
1073 		*mirror_port = mirror;
1074 
1075 	return err;
1076 }
1077 
1078 int mv88e6xxx_port_set_8021q_mode(struct mv88e6xxx_chip *chip, int port,
1079 				  u16 mode)
1080 {
1081 	u16 reg;
1082 	int err;
1083 
1084 	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
1085 	if (err)
1086 		return err;
1087 
1088 	reg &= ~MV88E6XXX_PORT_CTL2_8021Q_MODE_MASK;
1089 	reg |= mode & MV88E6XXX_PORT_CTL2_8021Q_MODE_MASK;
1090 
1091 	err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
1092 	if (err)
1093 		return err;
1094 
1095 	dev_dbg(chip->dev, "p%d: 802.1QMode set to %s\n", port,
1096 		mv88e6xxx_port_8021q_mode_names[mode]);
1097 
1098 	return 0;
1099 }
1100 
1101 int mv88e6xxx_port_set_map_da(struct mv88e6xxx_chip *chip, int port)
1102 {
1103 	u16 reg;
1104 	int err;
1105 
1106 	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
1107 	if (err)
1108 		return err;
1109 
1110 	reg |= MV88E6XXX_PORT_CTL2_MAP_DA;
1111 
1112 	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
1113 }
1114 
1115 int mv88e6165_port_set_jumbo_size(struct mv88e6xxx_chip *chip, int port,
1116 				  size_t size)
1117 {
1118 	u16 reg;
1119 	int err;
1120 
1121 	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
1122 	if (err)
1123 		return err;
1124 
1125 	reg &= ~MV88E6XXX_PORT_CTL2_JUMBO_MODE_MASK;
1126 
1127 	if (size <= 1522)
1128 		reg |= MV88E6XXX_PORT_CTL2_JUMBO_MODE_1522;
1129 	else if (size <= 2048)
1130 		reg |= MV88E6XXX_PORT_CTL2_JUMBO_MODE_2048;
1131 	else if (size <= 10240)
1132 		reg |= MV88E6XXX_PORT_CTL2_JUMBO_MODE_10240;
1133 	else
1134 		return -ERANGE;
1135 
1136 	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
1137 }
1138 
1139 /* Offset 0x09: Port Rate Control */
1140 
1141 int mv88e6095_port_egress_rate_limiting(struct mv88e6xxx_chip *chip, int port)
1142 {
1143 	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_EGRESS_RATE_CTL1,
1144 				    0x0000);
1145 }
1146 
1147 int mv88e6097_port_egress_rate_limiting(struct mv88e6xxx_chip *chip, int port)
1148 {
1149 	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_EGRESS_RATE_CTL1,
1150 				    0x0001);
1151 }
1152 
1153 /* Offset 0x0C: Port ATU Control */
1154 
1155 int mv88e6xxx_port_disable_learn_limit(struct mv88e6xxx_chip *chip, int port)
1156 {
1157 	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_ATU_CTL, 0);
1158 }
1159 
1160 /* Offset 0x0D: (Priority) Override Register */
1161 
1162 int mv88e6xxx_port_disable_pri_override(struct mv88e6xxx_chip *chip, int port)
1163 {
1164 	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_PRI_OVERRIDE, 0);
1165 }
1166 
1167 /* Offset 0x0f: Port Ether type */
1168 
1169 int mv88e6351_port_set_ether_type(struct mv88e6xxx_chip *chip, int port,
1170 				  u16 etype)
1171 {
1172 	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_ETH_TYPE, etype);
1173 }
1174 
1175 /* Offset 0x18: Port IEEE Priority Remapping Registers [0-3]
1176  * Offset 0x19: Port IEEE Priority Remapping Registers [4-7]
1177  */
1178 
1179 int mv88e6095_port_tag_remap(struct mv88e6xxx_chip *chip, int port)
1180 {
1181 	int err;
1182 
1183 	/* Use a direct priority mapping for all IEEE tagged frames */
1184 	err = mv88e6xxx_port_write(chip, port,
1185 				   MV88E6095_PORT_IEEE_PRIO_REMAP_0123,
1186 				   0x3210);
1187 	if (err)
1188 		return err;
1189 
1190 	return mv88e6xxx_port_write(chip, port,
1191 				    MV88E6095_PORT_IEEE_PRIO_REMAP_4567,
1192 				    0x7654);
1193 }
1194 
1195 static int mv88e6xxx_port_ieeepmt_write(struct mv88e6xxx_chip *chip,
1196 					int port, u16 table, u8 ptr, u16 data)
1197 {
1198 	u16 reg;
1199 
1200 	reg = MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_UPDATE | table |
1201 		(ptr << __bf_shf(MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_PTR_MASK)) |
1202 		(data & MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_DATA_MASK);
1203 
1204 	return mv88e6xxx_port_write(chip, port,
1205 				    MV88E6390_PORT_IEEE_PRIO_MAP_TABLE, reg);
1206 }
1207 
1208 int mv88e6390_port_tag_remap(struct mv88e6xxx_chip *chip, int port)
1209 {
1210 	int err, i;
1211 	u16 table;
1212 
1213 	for (i = 0; i <= 7; i++) {
1214 		table = MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_INGRESS_PCP;
1215 		err = mv88e6xxx_port_ieeepmt_write(chip, port, table, i,
1216 						   (i | i << 4));
1217 		if (err)
1218 			return err;
1219 
1220 		table = MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_EGRESS_GREEN_PCP;
1221 		err = mv88e6xxx_port_ieeepmt_write(chip, port, table, i, i);
1222 		if (err)
1223 			return err;
1224 
1225 		table = MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_EGRESS_YELLOW_PCP;
1226 		err = mv88e6xxx_port_ieeepmt_write(chip, port, table, i, i);
1227 		if (err)
1228 			return err;
1229 
1230 		table = MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_EGRESS_AVB_PCP;
1231 		err = mv88e6xxx_port_ieeepmt_write(chip, port, table, i, i);
1232 		if (err)
1233 			return err;
1234 	}
1235 
1236 	return 0;
1237 }
1238 
1239 /* Offset 0x0E: Policy Control Register */
1240 
1241 int mv88e6352_port_set_policy(struct mv88e6xxx_chip *chip, int port,
1242 			      enum mv88e6xxx_policy_mapping mapping,
1243 			      enum mv88e6xxx_policy_action action)
1244 {
1245 	u16 reg, mask, val;
1246 	int shift;
1247 	int err;
1248 
1249 	switch (mapping) {
1250 	case MV88E6XXX_POLICY_MAPPING_DA:
1251 		shift = __bf_shf(MV88E6XXX_PORT_POLICY_CTL_DA_MASK);
1252 		mask = MV88E6XXX_PORT_POLICY_CTL_DA_MASK;
1253 		break;
1254 	case MV88E6XXX_POLICY_MAPPING_SA:
1255 		shift = __bf_shf(MV88E6XXX_PORT_POLICY_CTL_SA_MASK);
1256 		mask = MV88E6XXX_PORT_POLICY_CTL_SA_MASK;
1257 		break;
1258 	case MV88E6XXX_POLICY_MAPPING_VTU:
1259 		shift = __bf_shf(MV88E6XXX_PORT_POLICY_CTL_VTU_MASK);
1260 		mask = MV88E6XXX_PORT_POLICY_CTL_VTU_MASK;
1261 		break;
1262 	case MV88E6XXX_POLICY_MAPPING_ETYPE:
1263 		shift = __bf_shf(MV88E6XXX_PORT_POLICY_CTL_ETYPE_MASK);
1264 		mask = MV88E6XXX_PORT_POLICY_CTL_ETYPE_MASK;
1265 		break;
1266 	case MV88E6XXX_POLICY_MAPPING_PPPOE:
1267 		shift = __bf_shf(MV88E6XXX_PORT_POLICY_CTL_PPPOE_MASK);
1268 		mask = MV88E6XXX_PORT_POLICY_CTL_PPPOE_MASK;
1269 		break;
1270 	case MV88E6XXX_POLICY_MAPPING_VBAS:
1271 		shift = __bf_shf(MV88E6XXX_PORT_POLICY_CTL_VBAS_MASK);
1272 		mask = MV88E6XXX_PORT_POLICY_CTL_VBAS_MASK;
1273 		break;
1274 	case MV88E6XXX_POLICY_MAPPING_OPT82:
1275 		shift = __bf_shf(MV88E6XXX_PORT_POLICY_CTL_OPT82_MASK);
1276 		mask = MV88E6XXX_PORT_POLICY_CTL_OPT82_MASK;
1277 		break;
1278 	case MV88E6XXX_POLICY_MAPPING_UDP:
1279 		shift = __bf_shf(MV88E6XXX_PORT_POLICY_CTL_UDP_MASK);
1280 		mask = MV88E6XXX_PORT_POLICY_CTL_UDP_MASK;
1281 		break;
1282 	default:
1283 		return -EOPNOTSUPP;
1284 	}
1285 
1286 	switch (action) {
1287 	case MV88E6XXX_POLICY_ACTION_NORMAL:
1288 		val = MV88E6XXX_PORT_POLICY_CTL_NORMAL;
1289 		break;
1290 	case MV88E6XXX_POLICY_ACTION_MIRROR:
1291 		val = MV88E6XXX_PORT_POLICY_CTL_MIRROR;
1292 		break;
1293 	case MV88E6XXX_POLICY_ACTION_TRAP:
1294 		val = MV88E6XXX_PORT_POLICY_CTL_TRAP;
1295 		break;
1296 	case MV88E6XXX_POLICY_ACTION_DISCARD:
1297 		val = MV88E6XXX_PORT_POLICY_CTL_DISCARD;
1298 		break;
1299 	default:
1300 		return -EOPNOTSUPP;
1301 	}
1302 
1303 	err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_POLICY_CTL, &reg);
1304 	if (err)
1305 		return err;
1306 
1307 	reg &= ~mask;
1308 	reg |= (val << shift) & mask;
1309 
1310 	return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_POLICY_CTL, reg);
1311 }
1312