blob: fc09b26f9b49fa15c0a82f9fbee816dd0cdaa0c3 [file] [log] [blame]
Vivien Didelot18abed22016-11-04 03:23:26 +01001/*
2 * Marvell 88E6xxx Switch Port Registers support
3 *
4 * Copyright (c) 2008 Marvell Semiconductor
5 *
Vivien Didelot4333d612017-03-28 15:10:36 -04006 * Copyright (c) 2016-2017 Savoir-faire Linux Inc.
7 * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
Vivien Didelot18abed22016-11-04 03:23:26 +01008 *
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.
13 */
14
Andrew Lunnf39908d2017-02-04 20:02:50 +010015#include <linux/phy.h>
Vivien Didelot4d5f2ba72017-06-02 17:06:15 -040016
17#include "chip.h"
Vivien Didelot18abed22016-11-04 03:23:26 +010018#include "port.h"
19
20int 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
28int 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}
Vivien Didelote28def332016-11-04 03:23:27 +010035
Vivien Didelot08ef7f12016-11-04 03:23:32 +010036/* Offset 0x01: MAC (or PCS or Physical) Control Register
37 *
38 * Link, Duplex and Flow Control have one force bit, one value bit.
Vivien Didelot96a2b402016-11-04 03:23:35 +010039 *
40 * For port's MAC speed, ForceSpd (or SpdValue) bits 1:0 program the value.
41 * Alternative values require the 200BASE (or AltSpeed) bit 12 set.
42 * Newer chips need a ForcedSpd bit 13 set to consider the value.
Vivien Didelot08ef7f12016-11-04 03:23:32 +010043 */
44
Vivien Didelota0a0f622016-11-04 03:23:34 +010045static int mv88e6xxx_port_set_rgmii_delay(struct mv88e6xxx_chip *chip, int port,
46 phy_interface_t mode)
47{
48 u16 reg;
49 int err;
50
51 err = mv88e6xxx_port_read(chip, port, PORT_PCS_CTRL, &reg);
52 if (err)
53 return err;
54
55 reg &= ~(PORT_PCS_CTRL_RGMII_DELAY_RXCLK |
56 PORT_PCS_CTRL_RGMII_DELAY_TXCLK);
57
58 switch (mode) {
59 case PHY_INTERFACE_MODE_RGMII_RXID:
60 reg |= PORT_PCS_CTRL_RGMII_DELAY_RXCLK;
61 break;
62 case PHY_INTERFACE_MODE_RGMII_TXID:
63 reg |= PORT_PCS_CTRL_RGMII_DELAY_TXCLK;
64 break;
65 case PHY_INTERFACE_MODE_RGMII_ID:
66 reg |= PORT_PCS_CTRL_RGMII_DELAY_RXCLK |
67 PORT_PCS_CTRL_RGMII_DELAY_TXCLK;
68 break;
Andrew Lunnfedf1862016-11-10 15:44:00 +010069 case PHY_INTERFACE_MODE_RGMII:
Vivien Didelota0a0f622016-11-04 03:23:34 +010070 break;
Andrew Lunnfedf1862016-11-10 15:44:00 +010071 default:
72 return 0;
Vivien Didelota0a0f622016-11-04 03:23:34 +010073 }
74
75 err = mv88e6xxx_port_write(chip, port, PORT_PCS_CTRL, reg);
76 if (err)
77 return err;
78
Vivien Didelot774439e52017-06-08 18:34:08 -040079 dev_dbg(chip->dev, "p%d: delay RXCLK %s, TXCLK %s\n", port,
80 reg & PORT_PCS_CTRL_RGMII_DELAY_RXCLK ? "yes" : "no",
81 reg & PORT_PCS_CTRL_RGMII_DELAY_TXCLK ? "yes" : "no");
Vivien Didelota0a0f622016-11-04 03:23:34 +010082
83 return 0;
84}
85
86int mv88e6352_port_set_rgmii_delay(struct mv88e6xxx_chip *chip, int port,
87 phy_interface_t mode)
88{
89 if (port < 5)
90 return -EOPNOTSUPP;
91
92 return mv88e6xxx_port_set_rgmii_delay(chip, port, mode);
93}
94
95int mv88e6390_port_set_rgmii_delay(struct mv88e6xxx_chip *chip, int port,
96 phy_interface_t mode)
97{
98 if (port != 0)
99 return -EOPNOTSUPP;
100
101 return mv88e6xxx_port_set_rgmii_delay(chip, port, mode);
102}
103
Vivien Didelot08ef7f12016-11-04 03:23:32 +0100104int mv88e6xxx_port_set_link(struct mv88e6xxx_chip *chip, int port, int link)
105{
106 u16 reg;
107 int err;
108
109 err = mv88e6xxx_port_read(chip, port, PORT_PCS_CTRL, &reg);
110 if (err)
111 return err;
112
113 reg &= ~(PORT_PCS_CTRL_FORCE_LINK | PORT_PCS_CTRL_LINK_UP);
114
115 switch (link) {
116 case LINK_FORCED_DOWN:
117 reg |= PORT_PCS_CTRL_FORCE_LINK;
118 break;
119 case LINK_FORCED_UP:
120 reg |= PORT_PCS_CTRL_FORCE_LINK | PORT_PCS_CTRL_LINK_UP;
121 break;
122 case LINK_UNFORCED:
123 /* normal link detection */
124 break;
125 default:
126 return -EINVAL;
127 }
128
129 err = mv88e6xxx_port_write(chip, port, PORT_PCS_CTRL, reg);
130 if (err)
131 return err;
132
Vivien Didelot774439e52017-06-08 18:34:08 -0400133 dev_dbg(chip->dev, "p%d: %s link %s\n", port,
134 reg & PORT_PCS_CTRL_FORCE_LINK ? "Force" : "Unforce",
135 reg & PORT_PCS_CTRL_LINK_UP ? "up" : "down");
Vivien Didelot08ef7f12016-11-04 03:23:32 +0100136
137 return 0;
138}
139
Vivien Didelot7f1ae072016-11-04 03:23:33 +0100140int mv88e6xxx_port_set_duplex(struct mv88e6xxx_chip *chip, int port, int dup)
141{
142 u16 reg;
143 int err;
144
145 err = mv88e6xxx_port_read(chip, port, PORT_PCS_CTRL, &reg);
146 if (err)
147 return err;
148
149 reg &= ~(PORT_PCS_CTRL_FORCE_DUPLEX | PORT_PCS_CTRL_DUPLEX_FULL);
150
151 switch (dup) {
152 case DUPLEX_HALF:
153 reg |= PORT_PCS_CTRL_FORCE_DUPLEX;
154 break;
155 case DUPLEX_FULL:
156 reg |= PORT_PCS_CTRL_FORCE_DUPLEX | PORT_PCS_CTRL_DUPLEX_FULL;
157 break;
158 case DUPLEX_UNFORCED:
159 /* normal duplex detection */
160 break;
161 default:
162 return -EINVAL;
163 }
164
165 err = mv88e6xxx_port_write(chip, port, PORT_PCS_CTRL, reg);
166 if (err)
167 return err;
168
Vivien Didelot774439e52017-06-08 18:34:08 -0400169 dev_dbg(chip->dev, "p%d: %s %s duplex\n", port,
170 reg & PORT_PCS_CTRL_FORCE_DUPLEX ? "Force" : "Unforce",
171 reg & PORT_PCS_CTRL_DUPLEX_FULL ? "full" : "half");
Vivien Didelot7f1ae072016-11-04 03:23:33 +0100172
173 return 0;
174}
175
Vivien Didelot96a2b402016-11-04 03:23:35 +0100176static int mv88e6xxx_port_set_speed(struct mv88e6xxx_chip *chip, int port,
177 int speed, bool alt_bit, bool force_bit)
178{
179 u16 reg, ctrl;
180 int err;
181
182 switch (speed) {
183 case 10:
184 ctrl = PORT_PCS_CTRL_SPEED_10;
185 break;
186 case 100:
187 ctrl = PORT_PCS_CTRL_SPEED_100;
188 break;
189 case 200:
190 if (alt_bit)
191 ctrl = PORT_PCS_CTRL_SPEED_100 | PORT_PCS_CTRL_ALTSPEED;
192 else
193 ctrl = PORT_PCS_CTRL_SPEED_200;
194 break;
195 case 1000:
196 ctrl = PORT_PCS_CTRL_SPEED_1000;
197 break;
198 case 2500:
Andrew Lunn740117a2017-02-02 00:46:16 +0100199 ctrl = PORT_PCS_CTRL_SPEED_10000 | PORT_PCS_CTRL_ALTSPEED;
Vivien Didelot96a2b402016-11-04 03:23:35 +0100200 break;
201 case 10000:
202 /* all bits set, fall through... */
203 case SPEED_UNFORCED:
204 ctrl = PORT_PCS_CTRL_SPEED_UNFORCED;
205 break;
206 default:
207 return -EOPNOTSUPP;
208 }
209
210 err = mv88e6xxx_port_read(chip, port, PORT_PCS_CTRL, &reg);
211 if (err)
212 return err;
213
214 reg &= ~PORT_PCS_CTRL_SPEED_MASK;
215 if (alt_bit)
216 reg &= ~PORT_PCS_CTRL_ALTSPEED;
217 if (force_bit) {
218 reg &= ~PORT_PCS_CTRL_FORCE_SPEED;
Andrew Lunn0b6e3d02016-11-16 04:26:48 +0100219 if (speed != SPEED_UNFORCED)
Vivien Didelot96a2b402016-11-04 03:23:35 +0100220 ctrl |= PORT_PCS_CTRL_FORCE_SPEED;
221 }
222 reg |= ctrl;
223
224 err = mv88e6xxx_port_write(chip, port, PORT_PCS_CTRL, reg);
225 if (err)
226 return err;
227
228 if (speed)
Vivien Didelot774439e52017-06-08 18:34:08 -0400229 dev_dbg(chip->dev, "p%d: Speed set to %d Mbps\n", port, speed);
Vivien Didelot96a2b402016-11-04 03:23:35 +0100230 else
Vivien Didelot774439e52017-06-08 18:34:08 -0400231 dev_dbg(chip->dev, "p%d: Speed unforced\n", port);
Vivien Didelot96a2b402016-11-04 03:23:35 +0100232
233 return 0;
234}
235
236/* Support 10, 100, 200 Mbps (e.g. 88E6065 family) */
237int mv88e6065_port_set_speed(struct mv88e6xxx_chip *chip, int port, int speed)
238{
239 if (speed == SPEED_MAX)
240 speed = 200;
241
242 if (speed > 200)
243 return -EOPNOTSUPP;
244
245 /* Setting 200 Mbps on port 0 to 3 selects 100 Mbps */
246 return mv88e6xxx_port_set_speed(chip, port, speed, false, false);
247}
248
249/* Support 10, 100, 1000 Mbps (e.g. 88E6185 family) */
250int mv88e6185_port_set_speed(struct mv88e6xxx_chip *chip, int port, int speed)
251{
252 if (speed == SPEED_MAX)
253 speed = 1000;
254
255 if (speed == 200 || speed > 1000)
256 return -EOPNOTSUPP;
257
258 return mv88e6xxx_port_set_speed(chip, port, speed, false, false);
259}
260
261/* Support 10, 100, 200, 1000 Mbps (e.g. 88E6352 family) */
262int mv88e6352_port_set_speed(struct mv88e6xxx_chip *chip, int port, int speed)
263{
264 if (speed == SPEED_MAX)
265 speed = 1000;
266
267 if (speed > 1000)
268 return -EOPNOTSUPP;
269
270 if (speed == 200 && port < 5)
271 return -EOPNOTSUPP;
272
273 return mv88e6xxx_port_set_speed(chip, port, speed, true, false);
274}
275
276/* Support 10, 100, 200, 1000, 2500 Mbps (e.g. 88E6390) */
277int mv88e6390_port_set_speed(struct mv88e6xxx_chip *chip, int port, int speed)
278{
279 if (speed == SPEED_MAX)
280 speed = port < 9 ? 1000 : 2500;
281
282 if (speed > 2500)
283 return -EOPNOTSUPP;
284
285 if (speed == 200 && port != 0)
286 return -EOPNOTSUPP;
287
288 if (speed == 2500 && port < 9)
289 return -EOPNOTSUPP;
290
291 return mv88e6xxx_port_set_speed(chip, port, speed, true, true);
292}
293
294/* Support 10, 100, 200, 1000, 2500, 10000 Mbps (e.g. 88E6190X) */
295int mv88e6390x_port_set_speed(struct mv88e6xxx_chip *chip, int port, int speed)
296{
297 if (speed == SPEED_MAX)
298 speed = port < 9 ? 1000 : 10000;
299
300 if (speed == 200 && port != 0)
301 return -EOPNOTSUPP;
302
303 if (speed >= 2500 && port < 9)
304 return -EOPNOTSUPP;
305
306 return mv88e6xxx_port_set_speed(chip, port, speed, true, true);
307}
308
Andrew Lunnf39908d2017-02-04 20:02:50 +0100309int mv88e6390x_port_set_cmode(struct mv88e6xxx_chip *chip, int port,
310 phy_interface_t mode)
311{
312 u16 reg;
313 u16 cmode;
314 int err;
315
316 if (mode == PHY_INTERFACE_MODE_NA)
317 return 0;
318
319 if (port != 9 && port != 10)
320 return -EOPNOTSUPP;
321
322 switch (mode) {
323 case PHY_INTERFACE_MODE_1000BASEX:
324 cmode = PORT_STATUS_CMODE_1000BASE_X;
325 break;
326 case PHY_INTERFACE_MODE_SGMII:
327 cmode = PORT_STATUS_CMODE_SGMII;
328 break;
329 case PHY_INTERFACE_MODE_2500BASEX:
330 cmode = PORT_STATUS_CMODE_2500BASEX;
331 break;
332 case PHY_INTERFACE_MODE_XGMII:
333 cmode = PORT_STATUS_CMODE_XAUI;
334 break;
335 case PHY_INTERFACE_MODE_RXAUI:
336 cmode = PORT_STATUS_CMODE_RXAUI;
337 break;
338 default:
339 cmode = 0;
340 }
341
342 if (cmode) {
343 err = mv88e6xxx_port_read(chip, port, PORT_STATUS, &reg);
344 if (err)
345 return err;
346
347 reg &= ~PORT_STATUS_CMODE_MASK;
348 reg |= cmode;
349
350 err = mv88e6xxx_port_write(chip, port, PORT_STATUS, reg);
351 if (err)
352 return err;
353 }
354
355 return 0;
356}
357
358int mv88e6xxx_port_get_cmode(struct mv88e6xxx_chip *chip, int port, u8 *cmode)
359{
360 int err;
361 u16 reg;
362
363 err = mv88e6xxx_port_read(chip, port, PORT_STATUS, &reg);
364 if (err)
365 return err;
366
367 *cmode = reg & PORT_STATUS_CMODE_MASK;
368
369 return 0;
370}
371
Andrew Lunnb35d322a2016-12-03 04:45:19 +0100372/* Offset 0x02: Pause Control
373 *
374 * Do not limit the period of time that this port can be paused for by
375 * the remote end or the period of time that this port can pause the
376 * remote end.
377 */
378int mv88e6097_port_pause_config(struct mv88e6xxx_chip *chip, int port)
379{
380 return mv88e6xxx_port_write(chip, port, PORT_PAUSE_CTRL, 0x0000);
381}
382
Andrew Lunn3ce0e652016-12-03 04:45:20 +0100383int mv88e6390_port_pause_config(struct mv88e6xxx_chip *chip, int port)
384{
385 int err;
386
387 err = mv88e6xxx_port_write(chip, port, PORT_PAUSE_CTRL,
388 PORT_FLOW_CTRL_LIMIT_IN | 0);
389 if (err)
390 return err;
391
392 return mv88e6xxx_port_write(chip, port, PORT_PAUSE_CTRL,
393 PORT_FLOW_CTRL_LIMIT_OUT | 0);
394}
395
Vivien Didelote28def332016-11-04 03:23:27 +0100396/* Offset 0x04: Port Control Register */
397
398static const char * const mv88e6xxx_port_state_names[] = {
399 [PORT_CONTROL_STATE_DISABLED] = "Disabled",
400 [PORT_CONTROL_STATE_BLOCKING] = "Blocking/Listening",
401 [PORT_CONTROL_STATE_LEARNING] = "Learning",
402 [PORT_CONTROL_STATE_FORWARDING] = "Forwarding",
403};
404
405int mv88e6xxx_port_set_state(struct mv88e6xxx_chip *chip, int port, u8 state)
406{
407 u16 reg;
408 int err;
409
410 err = mv88e6xxx_port_read(chip, port, PORT_CONTROL, &reg);
411 if (err)
412 return err;
413
414 reg &= ~PORT_CONTROL_STATE_MASK;
415 reg |= state;
416
417 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL, reg);
418 if (err)
419 return err;
420
Vivien Didelot774439e52017-06-08 18:34:08 -0400421 dev_dbg(chip->dev, "p%d: PortState set to %s\n", port,
422 mv88e6xxx_port_state_names[state]);
Vivien Didelote28def332016-11-04 03:23:27 +0100423
424 return 0;
425}
Vivien Didelot5a7921f2016-11-04 03:23:28 +0100426
Andrew Lunn56995cb2016-12-03 04:35:19 +0100427int mv88e6xxx_port_set_egress_mode(struct mv88e6xxx_chip *chip, int port,
428 u16 mode)
429{
430 int err;
431 u16 reg;
432
433 err = mv88e6xxx_port_read(chip, port, PORT_CONTROL, &reg);
434 if (err)
435 return err;
436
437 reg &= ~PORT_CONTROL_EGRESS_MASK;
438 reg |= mode;
439
440 return mv88e6xxx_port_write(chip, port, PORT_CONTROL, reg);
441}
442
443int mv88e6085_port_set_frame_mode(struct mv88e6xxx_chip *chip, int port,
444 enum mv88e6xxx_frame_mode mode)
445{
446 int err;
447 u16 reg;
448
449 err = mv88e6xxx_port_read(chip, port, PORT_CONTROL, &reg);
450 if (err)
451 return err;
452
Vivien Didelot5461bd42017-06-05 18:17:16 -0400453 reg &= ~PORT_CONTROL_FRAME_MASK;
Andrew Lunn56995cb2016-12-03 04:35:19 +0100454
455 switch (mode) {
456 case MV88E6XXX_FRAME_MODE_NORMAL:
457 reg |= PORT_CONTROL_FRAME_MODE_NORMAL;
458 break;
459 case MV88E6XXX_FRAME_MODE_DSA:
460 reg |= PORT_CONTROL_FRAME_MODE_DSA;
461 break;
462 default:
463 return -EINVAL;
464 }
465
466 return mv88e6xxx_port_write(chip, port, PORT_CONTROL, reg);
467}
468
469int mv88e6351_port_set_frame_mode(struct mv88e6xxx_chip *chip, int port,
470 enum mv88e6xxx_frame_mode mode)
471{
472 int err;
473 u16 reg;
474
475 err = mv88e6xxx_port_read(chip, port, PORT_CONTROL, &reg);
476 if (err)
477 return err;
478
479 reg &= ~PORT_CONTROL_FRAME_MASK;
480
481 switch (mode) {
482 case MV88E6XXX_FRAME_MODE_NORMAL:
483 reg |= PORT_CONTROL_FRAME_MODE_NORMAL;
484 break;
485 case MV88E6XXX_FRAME_MODE_DSA:
486 reg |= PORT_CONTROL_FRAME_MODE_DSA;
487 break;
488 case MV88E6XXX_FRAME_MODE_PROVIDER:
489 reg |= PORT_CONTROL_FRAME_MODE_PROVIDER;
490 break;
491 case MV88E6XXX_FRAME_MODE_ETHERTYPE:
492 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA;
493 break;
494 default:
495 return -EINVAL;
496 }
497
498 return mv88e6xxx_port_write(chip, port, PORT_CONTROL, reg);
499}
500
Vivien Didelot601aeed2017-03-11 16:13:00 -0500501static int mv88e6185_port_set_forward_unknown(struct mv88e6xxx_chip *chip,
502 int port, bool unicast)
Andrew Lunn56995cb2016-12-03 04:35:19 +0100503{
504 int err;
505 u16 reg;
506
507 err = mv88e6xxx_port_read(chip, port, PORT_CONTROL, &reg);
508 if (err)
509 return err;
510
Vivien Didelot601aeed2017-03-11 16:13:00 -0500511 if (unicast)
Andrew Lunn56995cb2016-12-03 04:35:19 +0100512 reg |= PORT_CONTROL_FORWARD_UNKNOWN;
513 else
514 reg &= ~PORT_CONTROL_FORWARD_UNKNOWN;
515
516 return mv88e6xxx_port_write(chip, port, PORT_CONTROL, reg);
517}
518
Vivien Didelot601aeed2017-03-11 16:13:00 -0500519int mv88e6352_port_set_egress_floods(struct mv88e6xxx_chip *chip, int port,
520 bool unicast, bool multicast)
Andrew Lunn56995cb2016-12-03 04:35:19 +0100521{
522 int err;
523 u16 reg;
524
525 err = mv88e6xxx_port_read(chip, port, PORT_CONTROL, &reg);
526 if (err)
527 return err;
528
Vivien Didelot601aeed2017-03-11 16:13:00 -0500529 reg &= ~PORT_CONTROL_EGRESS_FLOODS_MASK;
530
531 if (unicast && multicast)
532 reg |= PORT_CONTROL_EGRESS_FLOODS_ALL_UNKNOWN_DA;
533 else if (unicast)
534 reg |= PORT_CONTROL_EGRESS_FLOODS_NO_UNKNOWN_MC_DA;
535 else if (multicast)
536 reg |= PORT_CONTROL_EGRESS_FLOODS_NO_UNKNOWN_UC_DA;
Andrew Lunn56995cb2016-12-03 04:35:19 +0100537 else
Vivien Didelot601aeed2017-03-11 16:13:00 -0500538 reg |= PORT_CONTROL_EGRESS_FLOODS_NO_UNKNOWN_DA;
Andrew Lunn56995cb2016-12-03 04:35:19 +0100539
540 return mv88e6xxx_port_write(chip, port, PORT_CONTROL, reg);
541}
542
Vivien Didelotb4e48c52016-11-04 03:23:29 +0100543/* Offset 0x05: Port Control 1 */
544
Vivien Didelotea698f42017-03-11 16:12:50 -0500545int mv88e6xxx_port_set_message_port(struct mv88e6xxx_chip *chip, int port,
546 bool message_port)
547{
548 u16 val;
549 int err;
550
551 err = mv88e6xxx_port_read(chip, port, PORT_CONTROL_1, &val);
552 if (err)
553 return err;
554
555 if (message_port)
556 val |= PORT_CONTROL_1_MESSAGE_PORT;
557 else
558 val &= ~PORT_CONTROL_1_MESSAGE_PORT;
559
560 return mv88e6xxx_port_write(chip, port, PORT_CONTROL_1, val);
561}
562
Vivien Didelot5a7921f2016-11-04 03:23:28 +0100563/* Offset 0x06: Port Based VLAN Map */
564
565int mv88e6xxx_port_set_vlan_map(struct mv88e6xxx_chip *chip, int port, u16 map)
566{
Vivien Didelot4d294af2017-03-11 16:12:47 -0500567 const u16 mask = mv88e6xxx_port_mask(chip);
Vivien Didelot5a7921f2016-11-04 03:23:28 +0100568 u16 reg;
569 int err;
570
571 err = mv88e6xxx_port_read(chip, port, PORT_BASE_VLAN, &reg);
572 if (err)
573 return err;
574
575 reg &= ~mask;
576 reg |= map & mask;
577
578 err = mv88e6xxx_port_write(chip, port, PORT_BASE_VLAN, reg);
579 if (err)
580 return err;
581
Vivien Didelot774439e52017-06-08 18:34:08 -0400582 dev_dbg(chip->dev, "p%d: VLANTable set to %.3x\n", port, map);
Vivien Didelot5a7921f2016-11-04 03:23:28 +0100583
584 return 0;
585}
Vivien Didelotb4e48c52016-11-04 03:23:29 +0100586
587int mv88e6xxx_port_get_fid(struct mv88e6xxx_chip *chip, int port, u16 *fid)
588{
589 const u16 upper_mask = (mv88e6xxx_num_databases(chip) - 1) >> 4;
590 u16 reg;
591 int err;
592
593 /* Port's default FID lower 4 bits are located in reg 0x06, offset 12 */
594 err = mv88e6xxx_port_read(chip, port, PORT_BASE_VLAN, &reg);
595 if (err)
596 return err;
597
598 *fid = (reg & 0xf000) >> 12;
599
600 /* Port's default FID upper bits are located in reg 0x05, offset 0 */
601 if (upper_mask) {
602 err = mv88e6xxx_port_read(chip, port, PORT_CONTROL_1, &reg);
603 if (err)
604 return err;
605
606 *fid |= (reg & upper_mask) << 4;
607 }
608
609 return 0;
610}
611
612int mv88e6xxx_port_set_fid(struct mv88e6xxx_chip *chip, int port, u16 fid)
613{
614 const u16 upper_mask = (mv88e6xxx_num_databases(chip) - 1) >> 4;
615 u16 reg;
616 int err;
617
618 if (fid >= mv88e6xxx_num_databases(chip))
619 return -EINVAL;
620
621 /* Port's default FID lower 4 bits are located in reg 0x06, offset 12 */
622 err = mv88e6xxx_port_read(chip, port, PORT_BASE_VLAN, &reg);
623 if (err)
624 return err;
625
626 reg &= 0x0fff;
627 reg |= (fid & 0x000f) << 12;
628
629 err = mv88e6xxx_port_write(chip, port, PORT_BASE_VLAN, reg);
630 if (err)
631 return err;
632
633 /* Port's default FID upper bits are located in reg 0x05, offset 0 */
634 if (upper_mask) {
635 err = mv88e6xxx_port_read(chip, port, PORT_CONTROL_1, &reg);
636 if (err)
637 return err;
638
639 reg &= ~upper_mask;
640 reg |= (fid >> 4) & upper_mask;
641
642 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_1, reg);
643 if (err)
644 return err;
645 }
646
Vivien Didelot774439e52017-06-08 18:34:08 -0400647 dev_dbg(chip->dev, "p%d: FID set to %u\n", port, fid);
Vivien Didelotb4e48c52016-11-04 03:23:29 +0100648
649 return 0;
650}
Vivien Didelot77064f32016-11-04 03:23:30 +0100651
652/* Offset 0x07: Default Port VLAN ID & Priority */
653
654int mv88e6xxx_port_get_pvid(struct mv88e6xxx_chip *chip, int port, u16 *pvid)
655{
656 u16 reg;
657 int err;
658
659 err = mv88e6xxx_port_read(chip, port, PORT_DEFAULT_VLAN, &reg);
660 if (err)
661 return err;
662
663 *pvid = reg & PORT_DEFAULT_VLAN_MASK;
664
665 return 0;
666}
667
668int mv88e6xxx_port_set_pvid(struct mv88e6xxx_chip *chip, int port, u16 pvid)
669{
670 u16 reg;
671 int err;
672
673 err = mv88e6xxx_port_read(chip, port, PORT_DEFAULT_VLAN, &reg);
674 if (err)
675 return err;
676
677 reg &= ~PORT_DEFAULT_VLAN_MASK;
678 reg |= pvid & PORT_DEFAULT_VLAN_MASK;
679
680 err = mv88e6xxx_port_write(chip, port, PORT_DEFAULT_VLAN, reg);
681 if (err)
682 return err;
683
Vivien Didelot774439e52017-06-08 18:34:08 -0400684 dev_dbg(chip->dev, "p%d: DefaultVID set to %u\n", port, pvid);
Vivien Didelot77064f32016-11-04 03:23:30 +0100685
686 return 0;
687}
Vivien Didelot385a0992016-11-04 03:23:31 +0100688
689/* Offset 0x08: Port Control 2 Register */
690
691static const char * const mv88e6xxx_port_8021q_mode_names[] = {
692 [PORT_CONTROL_2_8021Q_DISABLED] = "Disabled",
693 [PORT_CONTROL_2_8021Q_FALLBACK] = "Fallback",
694 [PORT_CONTROL_2_8021Q_CHECK] = "Check",
695 [PORT_CONTROL_2_8021Q_SECURE] = "Secure",
696};
697
Vivien Didelot601aeed2017-03-11 16:13:00 -0500698static int mv88e6185_port_set_default_forward(struct mv88e6xxx_chip *chip,
699 int port, bool multicast)
Andrew Lunna23b2962017-02-04 20:15:28 +0100700{
701 int err;
702 u16 reg;
703
704 err = mv88e6xxx_port_read(chip, port, PORT_CONTROL_2, &reg);
705 if (err)
706 return err;
707
Vivien Didelot601aeed2017-03-11 16:13:00 -0500708 if (multicast)
709 reg |= PORT_CONTROL_2_DEFAULT_FORWARD;
Andrew Lunna23b2962017-02-04 20:15:28 +0100710 else
Vivien Didelot601aeed2017-03-11 16:13:00 -0500711 reg &= ~PORT_CONTROL_2_DEFAULT_FORWARD;
Andrew Lunna23b2962017-02-04 20:15:28 +0100712
713 return mv88e6xxx_port_write(chip, port, PORT_CONTROL_2, reg);
714}
715
Vivien Didelot601aeed2017-03-11 16:13:00 -0500716int mv88e6185_port_set_egress_floods(struct mv88e6xxx_chip *chip, int port,
717 bool unicast, bool multicast)
718{
719 int err;
720
721 err = mv88e6185_port_set_forward_unknown(chip, port, unicast);
722 if (err)
723 return err;
724
725 return mv88e6185_port_set_default_forward(chip, port, multicast);
726}
727
Andrew Lunna23b2962017-02-04 20:15:28 +0100728int mv88e6095_port_set_upstream_port(struct mv88e6xxx_chip *chip, int port,
729 int upstream_port)
730{
731 int err;
732 u16 reg;
733
734 err = mv88e6xxx_port_read(chip, port, PORT_CONTROL_2, &reg);
735 if (err)
736 return err;
737
738 reg &= ~PORT_CONTROL_2_UPSTREAM_MASK;
739 reg |= upstream_port;
740
741 return mv88e6xxx_port_write(chip, port, PORT_CONTROL_2, reg);
742}
743
Vivien Didelot385a0992016-11-04 03:23:31 +0100744int mv88e6xxx_port_set_8021q_mode(struct mv88e6xxx_chip *chip, int port,
745 u16 mode)
746{
747 u16 reg;
748 int err;
749
750 err = mv88e6xxx_port_read(chip, port, PORT_CONTROL_2, &reg);
751 if (err)
752 return err;
753
754 reg &= ~PORT_CONTROL_2_8021Q_MASK;
755 reg |= mode & PORT_CONTROL_2_8021Q_MASK;
756
757 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_2, reg);
758 if (err)
759 return err;
760
Vivien Didelot774439e52017-06-08 18:34:08 -0400761 dev_dbg(chip->dev, "p%d: 802.1QMode set to %s\n", port,
762 mv88e6xxx_port_8021q_mode_names[mode]);
Vivien Didelot385a0992016-11-04 03:23:31 +0100763
764 return 0;
765}
Andrew Lunnef0a7312016-12-03 04:35:16 +0100766
Andrew Lunna23b2962017-02-04 20:15:28 +0100767int mv88e6xxx_port_set_map_da(struct mv88e6xxx_chip *chip, int port)
768{
769 u16 reg;
770 int err;
771
772 err = mv88e6xxx_port_read(chip, port, PORT_CONTROL_2, &reg);
773 if (err)
774 return err;
775
776 reg |= PORT_CONTROL_2_MAP_DA;
777
778 return mv88e6xxx_port_write(chip, port, PORT_CONTROL_2, reg);
779}
780
Andrew Lunn5f436662016-12-03 04:45:17 +0100781int mv88e6165_port_jumbo_config(struct mv88e6xxx_chip *chip, int port)
782{
783 u16 reg;
784 int err;
785
786 err = mv88e6xxx_port_read(chip, port, PORT_CONTROL_2, &reg);
787 if (err)
788 return err;
789
790 reg |= PORT_CONTROL_2_JUMBO_10240;
791
792 return mv88e6xxx_port_write(chip, port, PORT_CONTROL_2, reg);
793}
794
Andrew Lunnef70b112016-12-03 04:45:18 +0100795/* Offset 0x09: Port Rate Control */
796
797int mv88e6095_port_egress_rate_limiting(struct mv88e6xxx_chip *chip, int port)
798{
799 return mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL, 0x0000);
800}
801
802int mv88e6097_port_egress_rate_limiting(struct mv88e6xxx_chip *chip, int port)
803{
804 return mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL, 0x0001);
805}
806
Vivien Didelotc8c94892017-03-11 16:13:01 -0500807/* Offset 0x0C: Port ATU Control */
808
809int mv88e6xxx_port_disable_learn_limit(struct mv88e6xxx_chip *chip, int port)
810{
811 return mv88e6xxx_port_write(chip, port, PORT_ATU_CONTROL, 0);
812}
813
Vivien Didelot9dbfb4e2017-03-11 16:13:02 -0500814/* Offset 0x0D: (Priority) Override Register */
815
816int mv88e6xxx_port_disable_pri_override(struct mv88e6xxx_chip *chip, int port)
817{
818 return mv88e6xxx_port_write(chip, port, PORT_PRI_OVERRIDE, 0);
819}
820
Andrew Lunn56995cb2016-12-03 04:35:19 +0100821/* Offset 0x0f: Port Ether type */
822
823int mv88e6351_port_set_ether_type(struct mv88e6xxx_chip *chip, int port,
824 u16 etype)
825{
826 return mv88e6xxx_port_write(chip, port, PORT_ETH_TYPE, etype);
827}
828
Andrew Lunnef0a7312016-12-03 04:35:16 +0100829/* Offset 0x18: Port IEEE Priority Remapping Registers [0-3]
830 * Offset 0x19: Port IEEE Priority Remapping Registers [4-7]
831 */
832
833int mv88e6095_port_tag_remap(struct mv88e6xxx_chip *chip, int port)
834{
835 int err;
836
837 /* Use a direct priority mapping for all IEEE tagged frames */
838 err = mv88e6xxx_port_write(chip, port, PORT_TAG_REGMAP_0123, 0x3210);
839 if (err)
840 return err;
841
842 return mv88e6xxx_port_write(chip, port, PORT_TAG_REGMAP_4567, 0x7654);
843}
844
845static int mv88e6xxx_port_ieeepmt_write(struct mv88e6xxx_chip *chip,
846 int port, u16 table,
847 u8 pointer, u16 data)
848{
849 u16 reg;
850
851 reg = PORT_IEEE_PRIO_MAP_TABLE_UPDATE |
852 table |
853 (pointer << PORT_IEEE_PRIO_MAP_TABLE_POINTER_SHIFT) |
854 data;
855
856 return mv88e6xxx_port_write(chip, port, PORT_IEEE_PRIO_MAP_TABLE, reg);
857}
858
859int mv88e6390_port_tag_remap(struct mv88e6xxx_chip *chip, int port)
860{
861 int err, i;
862
863 for (i = 0; i <= 7; i++) {
864 err = mv88e6xxx_port_ieeepmt_write(
865 chip, port, PORT_IEEE_PRIO_MAP_TABLE_INGRESS_PCP,
866 i, (i | i << 4));
867 if (err)
868 return err;
869
870 err = mv88e6xxx_port_ieeepmt_write(
871 chip, port, PORT_IEEE_PRIO_MAP_TABLE_EGRESS_GREEN_PCP,
872 i, i);
873 if (err)
874 return err;
875
876 err = mv88e6xxx_port_ieeepmt_write(
877 chip, port, PORT_IEEE_PRIO_MAP_TABLE_EGRESS_YELLOW_PCP,
878 i, i);
879 if (err)
880 return err;
881
882 err = mv88e6xxx_port_ieeepmt_write(
883 chip, port, PORT_IEEE_PRIO_MAP_TABLE_EGRESS_AVB_PCP,
884 i, i);
885 if (err)
886 return err;
887 }
888
889 return 0;
890}