blob: 451028c57af8affc42716b8f1aed6cc3bce0c508 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Vivien Didelot18abed22016-11-04 03:23:26 +01002/*
3 * Marvell 88E6xxx Switch Port Registers support
4 *
5 * Copyright (c) 2008 Marvell Semiconductor
6 *
Vivien Didelot4333d612017-03-28 15:10:36 -04007 * Copyright (c) 2016-2017 Savoir-faire Linux Inc.
8 * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
Vivien Didelot18abed22016-11-04 03:23:26 +01009 */
10
Vivien Didelotddcbabf2017-06-17 23:07:14 -040011#include <linux/bitfield.h>
Vivien Didelotf894c292017-06-08 18:34:10 -040012#include <linux/if_bridge.h>
Andrew Lunnf39908d2017-02-04 20:02:50 +010013#include <linux/phy.h>
Russell Kingc9a23562018-05-10 13:17:35 -070014#include <linux/phylink.h>
Vivien Didelot4d5f2ba72017-06-02 17:06:15 -040015
16#include "chip.h"
Pavana Sharmade776d02021-03-17 14:46:42 +010017#include "global2.h"
Vivien Didelot18abed22016-11-04 03:23:26 +010018#include "port.h"
Andrew Lunn364e9d72018-08-09 15:38:46 +020019#include "serdes.h"
Vivien Didelot18abed22016-11-04 03:23:26 +010020
21int mv88e6xxx_port_read(struct mv88e6xxx_chip *chip, int port, int reg,
22 u16 *val)
23{
24 int addr = chip->info->port_base_addr + port;
25
26 return mv88e6xxx_read(chip, addr, reg, val);
27}
28
Pavana Sharmade776d02021-03-17 14:46:42 +010029int mv88e6xxx_port_wait_bit(struct mv88e6xxx_chip *chip, int port, int reg,
30 int bit, int val)
31{
32 int addr = chip->info->port_base_addr + port;
33
34 return mv88e6xxx_wait_bit(chip, addr, reg, bit, val);
35}
36
Vivien Didelot18abed22016-11-04 03:23:26 +010037int mv88e6xxx_port_write(struct mv88e6xxx_chip *chip, int port, int reg,
38 u16 val)
39{
40 int addr = chip->info->port_base_addr + port;
41
42 return mv88e6xxx_write(chip, addr, reg, val);
43}
Vivien Didelote28def332016-11-04 03:23:27 +010044
Andrew Lunn54186b92018-08-09 15:38:37 +020045/* Offset 0x00: MAC (or PCS or Physical) Status Register
46 *
47 * For most devices, this is read only. However the 6185 has the MyPause
48 * bit read/write.
49 */
50int mv88e6185_port_set_pause(struct mv88e6xxx_chip *chip, int port,
51 int pause)
52{
53 u16 reg;
54 int err;
55
56 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_STS, &reg);
57 if (err)
58 return err;
59
60 if (pause)
61 reg |= MV88E6XXX_PORT_STS_MY_PAUSE;
62 else
63 reg &= ~MV88E6XXX_PORT_STS_MY_PAUSE;
64
65 return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_STS, reg);
66}
67
Vivien Didelot08ef7f12016-11-04 03:23:32 +010068/* Offset 0x01: MAC (or PCS or Physical) Control Register
69 *
70 * Link, Duplex and Flow Control have one force bit, one value bit.
Vivien Didelot96a2b402016-11-04 03:23:35 +010071 *
72 * For port's MAC speed, ForceSpd (or SpdValue) bits 1:0 program the value.
73 * Alternative values require the 200BASE (or AltSpeed) bit 12 set.
74 * Newer chips need a ForcedSpd bit 13 set to consider the value.
Vivien Didelot08ef7f12016-11-04 03:23:32 +010075 */
76
Vivien Didelota0a0f622016-11-04 03:23:34 +010077static int mv88e6xxx_port_set_rgmii_delay(struct mv88e6xxx_chip *chip, int port,
78 phy_interface_t mode)
79{
80 u16 reg;
81 int err;
82
Vivien Didelot5ee55572017-06-12 12:37:34 -040083 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_MAC_CTL, &reg);
Vivien Didelota0a0f622016-11-04 03:23:34 +010084 if (err)
85 return err;
86
Vivien Didelot5ee55572017-06-12 12:37:34 -040087 reg &= ~(MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_RXCLK |
88 MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_TXCLK);
Vivien Didelota0a0f622016-11-04 03:23:34 +010089
90 switch (mode) {
91 case PHY_INTERFACE_MODE_RGMII_RXID:
Vivien Didelot5ee55572017-06-12 12:37:34 -040092 reg |= MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_RXCLK;
Vivien Didelota0a0f622016-11-04 03:23:34 +010093 break;
94 case PHY_INTERFACE_MODE_RGMII_TXID:
Vivien Didelot5ee55572017-06-12 12:37:34 -040095 reg |= MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_TXCLK;
Vivien Didelota0a0f622016-11-04 03:23:34 +010096 break;
97 case PHY_INTERFACE_MODE_RGMII_ID:
Vivien Didelot5ee55572017-06-12 12:37:34 -040098 reg |= MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_RXCLK |
99 MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_TXCLK;
Vivien Didelota0a0f622016-11-04 03:23:34 +0100100 break;
Andrew Lunnfedf1862016-11-10 15:44:00 +0100101 case PHY_INTERFACE_MODE_RGMII:
Vivien Didelota0a0f622016-11-04 03:23:34 +0100102 break;
Andrew Lunnfedf1862016-11-10 15:44:00 +0100103 default:
104 return 0;
Vivien Didelota0a0f622016-11-04 03:23:34 +0100105 }
106
Vivien Didelot5ee55572017-06-12 12:37:34 -0400107 err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_MAC_CTL, reg);
Vivien Didelota0a0f622016-11-04 03:23:34 +0100108 if (err)
109 return err;
110
Vivien Didelot774439e52017-06-08 18:34:08 -0400111 dev_dbg(chip->dev, "p%d: delay RXCLK %s, TXCLK %s\n", port,
Vivien Didelot5ee55572017-06-12 12:37:34 -0400112 reg & MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_RXCLK ? "yes" : "no",
113 reg & MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_TXCLK ? "yes" : "no");
Vivien Didelota0a0f622016-11-04 03:23:34 +0100114
115 return 0;
116}
117
118int mv88e6352_port_set_rgmii_delay(struct mv88e6xxx_chip *chip, int port,
119 phy_interface_t mode)
120{
121 if (port < 5)
122 return -EOPNOTSUPP;
123
124 return mv88e6xxx_port_set_rgmii_delay(chip, port, mode);
125}
126
127int mv88e6390_port_set_rgmii_delay(struct mv88e6xxx_chip *chip, int port,
128 phy_interface_t mode)
129{
130 if (port != 0)
131 return -EOPNOTSUPP;
132
133 return mv88e6xxx_port_set_rgmii_delay(chip, port, mode);
134}
135
Vivien Didelot08ef7f12016-11-04 03:23:32 +0100136int mv88e6xxx_port_set_link(struct mv88e6xxx_chip *chip, int port, int link)
137{
138 u16 reg;
139 int err;
140
Vivien Didelot5ee55572017-06-12 12:37:34 -0400141 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_MAC_CTL, &reg);
Vivien Didelot08ef7f12016-11-04 03:23:32 +0100142 if (err)
143 return err;
144
Vivien Didelot5ee55572017-06-12 12:37:34 -0400145 reg &= ~(MV88E6XXX_PORT_MAC_CTL_FORCE_LINK |
146 MV88E6XXX_PORT_MAC_CTL_LINK_UP);
Vivien Didelot08ef7f12016-11-04 03:23:32 +0100147
148 switch (link) {
149 case LINK_FORCED_DOWN:
Vivien Didelot5ee55572017-06-12 12:37:34 -0400150 reg |= MV88E6XXX_PORT_MAC_CTL_FORCE_LINK;
Vivien Didelot08ef7f12016-11-04 03:23:32 +0100151 break;
152 case LINK_FORCED_UP:
Vivien Didelot5ee55572017-06-12 12:37:34 -0400153 reg |= MV88E6XXX_PORT_MAC_CTL_FORCE_LINK |
154 MV88E6XXX_PORT_MAC_CTL_LINK_UP;
Vivien Didelot08ef7f12016-11-04 03:23:32 +0100155 break;
156 case LINK_UNFORCED:
157 /* normal link detection */
158 break;
159 default:
160 return -EINVAL;
161 }
162
Vivien Didelot5ee55572017-06-12 12:37:34 -0400163 err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_MAC_CTL, reg);
Vivien Didelot08ef7f12016-11-04 03:23:32 +0100164 if (err)
165 return err;
166
Vivien Didelot774439e52017-06-08 18:34:08 -0400167 dev_dbg(chip->dev, "p%d: %s link %s\n", port,
Vivien Didelot5ee55572017-06-12 12:37:34 -0400168 reg & MV88E6XXX_PORT_MAC_CTL_FORCE_LINK ? "Force" : "Unforce",
169 reg & MV88E6XXX_PORT_MAC_CTL_LINK_UP ? "up" : "down");
Vivien Didelot08ef7f12016-11-04 03:23:32 +0100170
171 return 0;
172}
173
Chris Packham4efe76622020-11-24 17:34:37 +1300174int mv88e6xxx_port_sync_link(struct mv88e6xxx_chip *chip, int port, unsigned int mode, bool isup)
175{
176 const struct mv88e6xxx_ops *ops = chip->info->ops;
177 int err = 0;
178 int link;
179
180 if (isup)
181 link = LINK_FORCED_UP;
182 else
183 link = LINK_FORCED_DOWN;
184
185 if (ops->port_set_link)
186 err = ops->port_set_link(chip, port, link);
187
188 return err;
189}
190
191int mv88e6185_port_sync_link(struct mv88e6xxx_chip *chip, int port, unsigned int mode, bool isup)
192{
193 const struct mv88e6xxx_ops *ops = chip->info->ops;
194 int err = 0;
195 int link;
196
197 if (mode == MLO_AN_INBAND)
198 link = LINK_UNFORCED;
199 else if (isup)
200 link = LINK_FORCED_UP;
201 else
202 link = LINK_FORCED_DOWN;
203
204 if (ops->port_set_link)
205 err = ops->port_set_link(chip, port, link);
206
207 return err;
208}
209
Russell Kingf365c6f2020-03-14 10:15:53 +0000210static int mv88e6xxx_port_set_speed_duplex(struct mv88e6xxx_chip *chip,
211 int port, int speed, bool alt_bit,
212 bool force_bit, int duplex)
Vivien Didelot96a2b402016-11-04 03:23:35 +0100213{
214 u16 reg, ctrl;
215 int err;
216
217 switch (speed) {
218 case 10:
Vivien Didelot5ee55572017-06-12 12:37:34 -0400219 ctrl = MV88E6XXX_PORT_MAC_CTL_SPEED_10;
Vivien Didelot96a2b402016-11-04 03:23:35 +0100220 break;
221 case 100:
Vivien Didelot5ee55572017-06-12 12:37:34 -0400222 ctrl = MV88E6XXX_PORT_MAC_CTL_SPEED_100;
Vivien Didelot96a2b402016-11-04 03:23:35 +0100223 break;
224 case 200:
225 if (alt_bit)
Vivien Didelot5ee55572017-06-12 12:37:34 -0400226 ctrl = MV88E6XXX_PORT_MAC_CTL_SPEED_100 |
227 MV88E6390_PORT_MAC_CTL_ALTSPEED;
Vivien Didelot96a2b402016-11-04 03:23:35 +0100228 else
Vivien Didelot5ee55572017-06-12 12:37:34 -0400229 ctrl = MV88E6065_PORT_MAC_CTL_SPEED_200;
Vivien Didelot96a2b402016-11-04 03:23:35 +0100230 break;
231 case 1000:
Vivien Didelot5ee55572017-06-12 12:37:34 -0400232 ctrl = MV88E6XXX_PORT_MAC_CTL_SPEED_1000;
Vivien Didelot96a2b402016-11-04 03:23:35 +0100233 break;
234 case 2500:
Marek Behún26422342018-10-13 14:40:31 +0200235 if (alt_bit)
236 ctrl = MV88E6390_PORT_MAC_CTL_SPEED_10000 |
237 MV88E6390_PORT_MAC_CTL_ALTSPEED;
238 else
239 ctrl = MV88E6390_PORT_MAC_CTL_SPEED_10000;
Vivien Didelot96a2b402016-11-04 03:23:35 +0100240 break;
241 case 10000:
242 /* all bits set, fall through... */
243 case SPEED_UNFORCED:
Vivien Didelot5ee55572017-06-12 12:37:34 -0400244 ctrl = MV88E6XXX_PORT_MAC_CTL_SPEED_UNFORCED;
Vivien Didelot96a2b402016-11-04 03:23:35 +0100245 break;
246 default:
247 return -EOPNOTSUPP;
248 }
249
Russell Kingf365c6f2020-03-14 10:15:53 +0000250 switch (duplex) {
251 case DUPLEX_HALF:
252 ctrl |= MV88E6XXX_PORT_MAC_CTL_FORCE_DUPLEX;
253 break;
254 case DUPLEX_FULL:
255 ctrl |= MV88E6XXX_PORT_MAC_CTL_FORCE_DUPLEX |
256 MV88E6XXX_PORT_MAC_CTL_DUPLEX_FULL;
257 break;
258 case DUPLEX_UNFORCED:
259 /* normal duplex detection */
260 break;
261 default:
262 return -EOPNOTSUPP;
263 }
264
Vivien Didelot5ee55572017-06-12 12:37:34 -0400265 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_MAC_CTL, &reg);
Vivien Didelot96a2b402016-11-04 03:23:35 +0100266 if (err)
267 return err;
268
Russell Kingf365c6f2020-03-14 10:15:53 +0000269 reg &= ~(MV88E6XXX_PORT_MAC_CTL_SPEED_MASK |
270 MV88E6XXX_PORT_MAC_CTL_FORCE_DUPLEX |
271 MV88E6XXX_PORT_MAC_CTL_DUPLEX_FULL);
272
Vivien Didelot96a2b402016-11-04 03:23:35 +0100273 if (alt_bit)
Vivien Didelot5ee55572017-06-12 12:37:34 -0400274 reg &= ~MV88E6390_PORT_MAC_CTL_ALTSPEED;
Vivien Didelot96a2b402016-11-04 03:23:35 +0100275 if (force_bit) {
Vivien Didelot5ee55572017-06-12 12:37:34 -0400276 reg &= ~MV88E6390_PORT_MAC_CTL_FORCE_SPEED;
Andrew Lunn0b6e3d02016-11-16 04:26:48 +0100277 if (speed != SPEED_UNFORCED)
Vivien Didelot5ee55572017-06-12 12:37:34 -0400278 ctrl |= MV88E6390_PORT_MAC_CTL_FORCE_SPEED;
Vivien Didelot96a2b402016-11-04 03:23:35 +0100279 }
280 reg |= ctrl;
281
Vivien Didelot5ee55572017-06-12 12:37:34 -0400282 err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_MAC_CTL, reg);
Vivien Didelot96a2b402016-11-04 03:23:35 +0100283 if (err)
284 return err;
285
286 if (speed)
Vivien Didelot774439e52017-06-08 18:34:08 -0400287 dev_dbg(chip->dev, "p%d: Speed set to %d Mbps\n", port, speed);
Vivien Didelot96a2b402016-11-04 03:23:35 +0100288 else
Vivien Didelot774439e52017-06-08 18:34:08 -0400289 dev_dbg(chip->dev, "p%d: Speed unforced\n", port);
Russell Kingf365c6f2020-03-14 10:15:53 +0000290 dev_dbg(chip->dev, "p%d: %s %s duplex\n", port,
291 reg & MV88E6XXX_PORT_MAC_CTL_FORCE_DUPLEX ? "Force" : "Unforce",
292 reg & MV88E6XXX_PORT_MAC_CTL_DUPLEX_FULL ? "full" : "half");
Vivien Didelot96a2b402016-11-04 03:23:35 +0100293
294 return 0;
295}
296
297/* Support 10, 100, 200 Mbps (e.g. 88E6065 family) */
Russell Kingf365c6f2020-03-14 10:15:53 +0000298int mv88e6065_port_set_speed_duplex(struct mv88e6xxx_chip *chip, int port,
299 int speed, int duplex)
Vivien Didelot96a2b402016-11-04 03:23:35 +0100300{
301 if (speed == SPEED_MAX)
302 speed = 200;
303
304 if (speed > 200)
305 return -EOPNOTSUPP;
306
307 /* Setting 200 Mbps on port 0 to 3 selects 100 Mbps */
Russell Kingf365c6f2020-03-14 10:15:53 +0000308 return mv88e6xxx_port_set_speed_duplex(chip, port, speed, false, false,
309 duplex);
Vivien Didelot96a2b402016-11-04 03:23:35 +0100310}
311
312/* Support 10, 100, 1000 Mbps (e.g. 88E6185 family) */
Russell Kingf365c6f2020-03-14 10:15:53 +0000313int mv88e6185_port_set_speed_duplex(struct mv88e6xxx_chip *chip, int port,
314 int speed, int duplex)
Vivien Didelot96a2b402016-11-04 03:23:35 +0100315{
316 if (speed == SPEED_MAX)
317 speed = 1000;
318
319 if (speed == 200 || speed > 1000)
320 return -EOPNOTSUPP;
321
Russell Kingf365c6f2020-03-14 10:15:53 +0000322 return mv88e6xxx_port_set_speed_duplex(chip, port, speed, false, false,
323 duplex);
Vivien Didelot96a2b402016-11-04 03:23:35 +0100324}
325
Rasmus Villemoesa528e5b2019-06-04 07:34:29 +0000326/* Support 10, 100 Mbps (e.g. 88E6250 family) */
Russell Kingf365c6f2020-03-14 10:15:53 +0000327int mv88e6250_port_set_speed_duplex(struct mv88e6xxx_chip *chip, int port,
328 int speed, int duplex)
Rasmus Villemoesa528e5b2019-06-04 07:34:29 +0000329{
330 if (speed == SPEED_MAX)
331 speed = 100;
332
333 if (speed > 100)
334 return -EOPNOTSUPP;
335
Russell Kingf365c6f2020-03-14 10:15:53 +0000336 return mv88e6xxx_port_set_speed_duplex(chip, port, speed, false, false,
337 duplex);
Rasmus Villemoesa528e5b2019-06-04 07:34:29 +0000338}
339
Marek Behún26422342018-10-13 14:40:31 +0200340/* Support 10, 100, 200, 1000, 2500 Mbps (e.g. 88E6341) */
Russell Kingf365c6f2020-03-14 10:15:53 +0000341int mv88e6341_port_set_speed_duplex(struct mv88e6xxx_chip *chip, int port,
342 int speed, int duplex)
Marek Behún26422342018-10-13 14:40:31 +0200343{
344 if (speed == SPEED_MAX)
345 speed = port < 5 ? 1000 : 2500;
346
347 if (speed > 2500)
348 return -EOPNOTSUPP;
349
350 if (speed == 200 && port != 0)
351 return -EOPNOTSUPP;
352
353 if (speed == 2500 && port < 5)
354 return -EOPNOTSUPP;
355
Russell Kingf365c6f2020-03-14 10:15:53 +0000356 return mv88e6xxx_port_set_speed_duplex(chip, port, speed, !port, true,
357 duplex);
Marek Behún26422342018-10-13 14:40:31 +0200358}
359
Andrew Lunn7cbbee02019-03-08 01:21:27 +0100360phy_interface_t mv88e6341_port_max_speed_mode(int port)
361{
362 if (port == 5)
363 return PHY_INTERFACE_MODE_2500BASEX;
364
365 return PHY_INTERFACE_MODE_NA;
366}
367
Vivien Didelot96a2b402016-11-04 03:23:35 +0100368/* Support 10, 100, 200, 1000 Mbps (e.g. 88E6352 family) */
Russell Kingf365c6f2020-03-14 10:15:53 +0000369int mv88e6352_port_set_speed_duplex(struct mv88e6xxx_chip *chip, int port,
370 int speed, int duplex)
Vivien Didelot96a2b402016-11-04 03:23:35 +0100371{
372 if (speed == SPEED_MAX)
373 speed = 1000;
374
375 if (speed > 1000)
376 return -EOPNOTSUPP;
377
378 if (speed == 200 && port < 5)
379 return -EOPNOTSUPP;
380
Russell Kingf365c6f2020-03-14 10:15:53 +0000381 return mv88e6xxx_port_set_speed_duplex(chip, port, speed, true, false,
382 duplex);
Vivien Didelot96a2b402016-11-04 03:23:35 +0100383}
384
385/* Support 10, 100, 200, 1000, 2500 Mbps (e.g. 88E6390) */
Russell Kingf365c6f2020-03-14 10:15:53 +0000386int mv88e6390_port_set_speed_duplex(struct mv88e6xxx_chip *chip, int port,
387 int speed, int duplex)
Vivien Didelot96a2b402016-11-04 03:23:35 +0100388{
389 if (speed == SPEED_MAX)
390 speed = port < 9 ? 1000 : 2500;
391
392 if (speed > 2500)
393 return -EOPNOTSUPP;
394
395 if (speed == 200 && port != 0)
396 return -EOPNOTSUPP;
397
398 if (speed == 2500 && port < 9)
399 return -EOPNOTSUPP;
400
Russell Kingf365c6f2020-03-14 10:15:53 +0000401 return mv88e6xxx_port_set_speed_duplex(chip, port, speed, true, true,
402 duplex);
Vivien Didelot96a2b402016-11-04 03:23:35 +0100403}
404
Andrew Lunn7cbbee02019-03-08 01:21:27 +0100405phy_interface_t mv88e6390_port_max_speed_mode(int port)
406{
407 if (port == 9 || port == 10)
408 return PHY_INTERFACE_MODE_2500BASEX;
409
410 return PHY_INTERFACE_MODE_NA;
411}
412
Vivien Didelot96a2b402016-11-04 03:23:35 +0100413/* Support 10, 100, 200, 1000, 2500, 10000 Mbps (e.g. 88E6190X) */
Russell Kingf365c6f2020-03-14 10:15:53 +0000414int mv88e6390x_port_set_speed_duplex(struct mv88e6xxx_chip *chip, int port,
415 int speed, int duplex)
Vivien Didelot96a2b402016-11-04 03:23:35 +0100416{
417 if (speed == SPEED_MAX)
418 speed = port < 9 ? 1000 : 10000;
419
420 if (speed == 200 && port != 0)
421 return -EOPNOTSUPP;
422
423 if (speed >= 2500 && port < 9)
424 return -EOPNOTSUPP;
425
Russell Kingf365c6f2020-03-14 10:15:53 +0000426 return mv88e6xxx_port_set_speed_duplex(chip, port, speed, true, true,
427 duplex);
Vivien Didelot96a2b402016-11-04 03:23:35 +0100428}
429
Andrew Lunn7cbbee02019-03-08 01:21:27 +0100430phy_interface_t mv88e6390x_port_max_speed_mode(int port)
431{
432 if (port == 9 || port == 10)
433 return PHY_INTERFACE_MODE_XAUI;
434
435 return PHY_INTERFACE_MODE_NA;
436}
437
Pavana Sharmade776d02021-03-17 14:46:42 +0100438/* Support 10, 100, 200, 1000, 2500, 5000, 10000 Mbps (e.g. 88E6393X)
439 * Function mv88e6xxx_port_set_speed_duplex() can't be used as the register
440 * values for speeds 2500 & 5000 conflict.
441 */
442int mv88e6393x_port_set_speed_duplex(struct mv88e6xxx_chip *chip, int port,
443 int speed, int duplex)
444{
445 u16 reg, ctrl;
446 int err;
447
448 if (speed == SPEED_MAX)
449 speed = (port > 0 && port < 9) ? 1000 : 10000;
450
451 if (speed == 200 && port != 0)
452 return -EOPNOTSUPP;
453
454 if (speed >= 2500 && port > 0 && port < 9)
455 return -EOPNOTSUPP;
456
457 switch (speed) {
458 case 10:
459 ctrl = MV88E6XXX_PORT_MAC_CTL_SPEED_10;
460 break;
461 case 100:
462 ctrl = MV88E6XXX_PORT_MAC_CTL_SPEED_100;
463 break;
464 case 200:
465 ctrl = MV88E6XXX_PORT_MAC_CTL_SPEED_100 |
466 MV88E6390_PORT_MAC_CTL_ALTSPEED;
467 break;
468 case 1000:
469 ctrl = MV88E6XXX_PORT_MAC_CTL_SPEED_1000;
470 break;
471 case 2500:
472 ctrl = MV88E6XXX_PORT_MAC_CTL_SPEED_1000 |
473 MV88E6390_PORT_MAC_CTL_ALTSPEED;
474 break;
475 case 5000:
476 ctrl = MV88E6390_PORT_MAC_CTL_SPEED_10000 |
477 MV88E6390_PORT_MAC_CTL_ALTSPEED;
478 break;
479 case 10000:
480 case SPEED_UNFORCED:
481 ctrl = MV88E6XXX_PORT_MAC_CTL_SPEED_UNFORCED;
482 break;
483 default:
484 return -EOPNOTSUPP;
485 }
486
487 switch (duplex) {
488 case DUPLEX_HALF:
489 ctrl |= MV88E6XXX_PORT_MAC_CTL_FORCE_DUPLEX;
490 break;
491 case DUPLEX_FULL:
492 ctrl |= MV88E6XXX_PORT_MAC_CTL_FORCE_DUPLEX |
493 MV88E6XXX_PORT_MAC_CTL_DUPLEX_FULL;
494 break;
495 case DUPLEX_UNFORCED:
496 /* normal duplex detection */
497 break;
498 default:
499 return -EOPNOTSUPP;
500 }
501
502 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_MAC_CTL, &reg);
503 if (err)
504 return err;
505
506 reg &= ~(MV88E6XXX_PORT_MAC_CTL_SPEED_MASK |
507 MV88E6390_PORT_MAC_CTL_ALTSPEED |
508 MV88E6390_PORT_MAC_CTL_FORCE_SPEED);
509
510 if (speed != SPEED_UNFORCED)
511 reg |= MV88E6390_PORT_MAC_CTL_FORCE_SPEED;
512
513 reg |= ctrl;
514
515 err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_MAC_CTL, reg);
516 if (err)
517 return err;
518
519 if (speed)
520 dev_dbg(chip->dev, "p%d: Speed set to %d Mbps\n", port, speed);
521 else
522 dev_dbg(chip->dev, "p%d: Speed unforced\n", port);
523 dev_dbg(chip->dev, "p%d: %s %s duplex\n", port,
524 reg & MV88E6XXX_PORT_MAC_CTL_FORCE_DUPLEX ? "Force" : "Unforce",
525 reg & MV88E6XXX_PORT_MAC_CTL_DUPLEX_FULL ? "full" : "half");
526
527 return 0;
528}
529
530phy_interface_t mv88e6393x_port_max_speed_mode(int port)
531{
532 if (port == 0 || port == 9 || port == 10)
533 return PHY_INTERFACE_MODE_10GBASER;
534
535 return PHY_INTERFACE_MODE_NA;
536}
537
Marek Behún7a3007d2019-08-26 23:31:55 +0200538static int mv88e6xxx_port_set_cmode(struct mv88e6xxx_chip *chip, int port,
Baruch Siachf7a48b62019-12-19 11:48:22 +0200539 phy_interface_t mode, bool force)
Andrew Lunnf39908d2017-02-04 20:02:50 +0100540{
Andrew Lunnf39908d2017-02-04 20:02:50 +0100541 u16 cmode;
Pavana Sharma193c5b22021-03-17 14:46:40 +0100542 int lane;
Andrew Lunn734447d2018-08-09 15:38:49 +0200543 u16 reg;
Andrew Lunnf39908d2017-02-04 20:02:50 +0100544 int err;
545
Andrew Lunn787799a2018-11-11 00:32:16 +0100546 /* Default to a slow mode, so freeing up SERDES interfaces for
547 * other ports which might use them for SFPs.
548 */
549 if (mode == PHY_INTERFACE_MODE_NA)
550 mode = PHY_INTERFACE_MODE_1000BASEX;
551
Andrew Lunnf39908d2017-02-04 20:02:50 +0100552 switch (mode) {
553 case PHY_INTERFACE_MODE_1000BASEX:
Marek Behún3bbb8862019-08-26 23:31:54 +0200554 cmode = MV88E6XXX_PORT_STS_CMODE_1000BASEX;
Andrew Lunnf39908d2017-02-04 20:02:50 +0100555 break;
556 case PHY_INTERFACE_MODE_SGMII:
Vivien Didelot5f83dc92017-06-12 12:37:33 -0400557 cmode = MV88E6XXX_PORT_STS_CMODE_SGMII;
Andrew Lunnf39908d2017-02-04 20:02:50 +0100558 break;
559 case PHY_INTERFACE_MODE_2500BASEX:
Vivien Didelot5f83dc92017-06-12 12:37:33 -0400560 cmode = MV88E6XXX_PORT_STS_CMODE_2500BASEX;
Andrew Lunnf39908d2017-02-04 20:02:50 +0100561 break;
Pavana Sharmade776d02021-03-17 14:46:42 +0100562 case PHY_INTERFACE_MODE_5GBASER:
563 cmode = MV88E6393X_PORT_STS_CMODE_5GBASER;
564 break;
Andrew Lunnf39908d2017-02-04 20:02:50 +0100565 case PHY_INTERFACE_MODE_XGMII:
Russell King2e51a8d2017-12-12 09:29:46 +0000566 case PHY_INTERFACE_MODE_XAUI:
Vivien Didelot5f83dc92017-06-12 12:37:33 -0400567 cmode = MV88E6XXX_PORT_STS_CMODE_XAUI;
Andrew Lunnf39908d2017-02-04 20:02:50 +0100568 break;
569 case PHY_INTERFACE_MODE_RXAUI:
Vivien Didelot5f83dc92017-06-12 12:37:33 -0400570 cmode = MV88E6XXX_PORT_STS_CMODE_RXAUI;
Andrew Lunnf39908d2017-02-04 20:02:50 +0100571 break;
Pavana Sharmade776d02021-03-17 14:46:42 +0100572 case PHY_INTERFACE_MODE_10GBASER:
573 cmode = MV88E6393X_PORT_STS_CMODE_10GBASER;
574 break;
Andrew Lunnf39908d2017-02-04 20:02:50 +0100575 default:
576 cmode = 0;
577 }
578
Baruch Siachf7a48b62019-12-19 11:48:22 +0200579 /* cmode doesn't change, nothing to do for us unless forced */
580 if (cmode == chip->ports[port].cmode && !force)
Heiner Kallweited8fe202019-02-28 07:39:15 +0100581 return 0;
582
Vivien Didelot5122d4e2019-08-31 16:18:30 -0400583 lane = mv88e6xxx_serdes_get_lane(chip, port);
Pavana Sharma193c5b22021-03-17 14:46:40 +0100584 if (lane >= 0) {
Heiner Kallweit5ceaeb92019-03-23 19:41:32 +0100585 if (chip->ports[port].serdes_irq) {
Vivien Didelot61a46b42019-08-31 16:18:34 -0400586 err = mv88e6xxx_serdes_irq_disable(chip, port, lane);
Heiner Kallweit5ceaeb92019-03-23 19:41:32 +0100587 if (err)
588 return err;
589 }
590
Vivien Didelotdc272f62019-08-31 16:18:33 -0400591 err = mv88e6xxx_serdes_power_down(chip, port, lane);
Andrew Lunn734447d2018-08-09 15:38:49 +0200592 if (err)
593 return err;
594 }
595
Heiner Kallweit5ceaeb92019-03-23 19:41:32 +0100596 chip->ports[port].cmode = 0;
Andrew Lunn364e9d72018-08-09 15:38:46 +0200597
Andrew Lunnf39908d2017-02-04 20:02:50 +0100598 if (cmode) {
Vivien Didelot5f83dc92017-06-12 12:37:33 -0400599 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_STS, &reg);
Andrew Lunnf39908d2017-02-04 20:02:50 +0100600 if (err)
601 return err;
602
Vivien Didelot5f83dc92017-06-12 12:37:33 -0400603 reg &= ~MV88E6XXX_PORT_STS_CMODE_MASK;
Andrew Lunnf39908d2017-02-04 20:02:50 +0100604 reg |= cmode;
605
Vivien Didelot5f83dc92017-06-12 12:37:33 -0400606 err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_STS, reg);
Andrew Lunnf39908d2017-02-04 20:02:50 +0100607 if (err)
608 return err;
Andrew Lunn364e9d72018-08-09 15:38:46 +0200609
Heiner Kallweit5ceaeb92019-03-23 19:41:32 +0100610 chip->ports[port].cmode = cmode;
611
Vivien Didelot5122d4e2019-08-31 16:18:30 -0400612 lane = mv88e6xxx_serdes_get_lane(chip, port);
Pavana Sharma193c5b22021-03-17 14:46:40 +0100613 if (lane < 0)
614 return lane;
Heiner Kallweit5ceaeb92019-03-23 19:41:32 +0100615
Vivien Didelotdc272f62019-08-31 16:18:33 -0400616 err = mv88e6xxx_serdes_power_up(chip, port, lane);
Andrew Lunn364e9d72018-08-09 15:38:46 +0200617 if (err)
618 return err;
Andrew Lunn734447d2018-08-09 15:38:49 +0200619
620 if (chip->ports[port].serdes_irq) {
Vivien Didelot61a46b42019-08-31 16:18:34 -0400621 err = mv88e6xxx_serdes_irq_enable(chip, port, lane);
Andrew Lunn734447d2018-08-09 15:38:49 +0200622 if (err)
623 return err;
624 }
Andrew Lunnf39908d2017-02-04 20:02:50 +0100625 }
626
627 return 0;
628}
629
Marek Behún7a3007d2019-08-26 23:31:55 +0200630int mv88e6390x_port_set_cmode(struct mv88e6xxx_chip *chip, int port,
631 phy_interface_t mode)
632{
633 if (port != 9 && port != 10)
634 return -EOPNOTSUPP;
635
Baruch Siachf7a48b62019-12-19 11:48:22 +0200636 return mv88e6xxx_port_set_cmode(chip, port, mode, false);
Marek Behún7a3007d2019-08-26 23:31:55 +0200637}
638
Andrew Lunnfdc71ee2018-11-11 00:32:15 +0100639int mv88e6390_port_set_cmode(struct mv88e6xxx_chip *chip, int port,
640 phy_interface_t mode)
641{
Marek Behún7a3007d2019-08-26 23:31:55 +0200642 if (port != 9 && port != 10)
643 return -EOPNOTSUPP;
644
Andrew Lunnfdc71ee2018-11-11 00:32:15 +0100645 switch (mode) {
Marek Behún65b034c2019-02-25 12:39:54 +0100646 case PHY_INTERFACE_MODE_NA:
647 return 0;
Andrew Lunnfdc71ee2018-11-11 00:32:15 +0100648 case PHY_INTERFACE_MODE_XGMII:
649 case PHY_INTERFACE_MODE_XAUI:
650 case PHY_INTERFACE_MODE_RXAUI:
651 return -EINVAL;
652 default:
653 break;
654 }
655
Baruch Siachf7a48b62019-12-19 11:48:22 +0200656 return mv88e6xxx_port_set_cmode(chip, port, mode, false);
Marek Behún7a3007d2019-08-26 23:31:55 +0200657}
658
Pavana Sharmade776d02021-03-17 14:46:42 +0100659int mv88e6393x_port_set_cmode(struct mv88e6xxx_chip *chip, int port,
660 phy_interface_t mode)
661{
662 int err;
663 u16 reg;
664
665 if (port != 0 && port != 9 && port != 10)
666 return -EOPNOTSUPP;
667
668 /* mv88e6393x errata 4.5: EEE should be disabled on SERDES ports */
669 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_MAC_CTL, &reg);
670 if (err)
671 return err;
672
673 reg &= ~MV88E6XXX_PORT_MAC_CTL_EEE;
674 reg |= MV88E6XXX_PORT_MAC_CTL_FORCE_EEE;
675 err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_MAC_CTL, reg);
676 if (err)
677 return err;
678
679 return mv88e6xxx_port_set_cmode(chip, port, mode, false);
680}
681
Vivien Didelot5d24da1e2019-08-28 12:26:59 -0400682static int mv88e6341_port_set_cmode_writable(struct mv88e6xxx_chip *chip,
683 int port)
Marek Behún7a3007d2019-08-26 23:31:55 +0200684{
685 int err, addr;
686 u16 reg, bits;
687
688 if (port != 5)
689 return -EOPNOTSUPP;
690
691 addr = chip->info->port_base_addr + port;
692
693 err = mv88e6xxx_port_hidden_read(chip, 0x7, addr, 0, &reg);
694 if (err)
695 return err;
696
697 bits = MV88E6341_PORT_RESERVED_1A_FORCE_CMODE |
698 MV88E6341_PORT_RESERVED_1A_SGMII_AN;
699
700 if ((reg & bits) == bits)
701 return 0;
702
703 reg |= bits;
704 return mv88e6xxx_port_hidden_write(chip, 0x7, addr, 0, reg);
705}
706
707int mv88e6341_port_set_cmode(struct mv88e6xxx_chip *chip, int port,
708 phy_interface_t mode)
709{
Vivien Didelot5d24da1e2019-08-28 12:26:59 -0400710 int err;
711
Marek Behún7a3007d2019-08-26 23:31:55 +0200712 if (port != 5)
713 return -EOPNOTSUPP;
714
715 switch (mode) {
716 case PHY_INTERFACE_MODE_NA:
717 return 0;
718 case PHY_INTERFACE_MODE_XGMII:
719 case PHY_INTERFACE_MODE_XAUI:
720 case PHY_INTERFACE_MODE_RXAUI:
721 return -EINVAL;
722 default:
723 break;
724 }
725
Vivien Didelot5d24da1e2019-08-28 12:26:59 -0400726 err = mv88e6341_port_set_cmode_writable(chip, port);
727 if (err)
728 return err;
729
Baruch Siachf7a48b62019-12-19 11:48:22 +0200730 return mv88e6xxx_port_set_cmode(chip, port, mode, true);
Andrew Lunnfdc71ee2018-11-11 00:32:15 +0100731}
732
Andrew Lunn2d2e1dd2018-08-09 15:38:45 +0200733int mv88e6185_port_get_cmode(struct mv88e6xxx_chip *chip, int port, u8 *cmode)
Russell King6c422e32018-08-09 15:38:39 +0200734{
735 int err;
736 u16 reg;
737
738 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_STS, &reg);
739 if (err)
740 return err;
741
Andrew Lunn2d2e1dd2018-08-09 15:38:45 +0200742 *cmode = reg & MV88E6185_PORT_STS_CMODE_MASK;
743
744 return 0;
Russell King6c422e32018-08-09 15:38:39 +0200745}
746
Andrew Lunn2d2e1dd2018-08-09 15:38:45 +0200747int mv88e6352_port_get_cmode(struct mv88e6xxx_chip *chip, int port, u8 *cmode)
Andrew Lunnf39908d2017-02-04 20:02:50 +0100748{
749 int err;
750 u16 reg;
751
Vivien Didelot5f83dc92017-06-12 12:37:33 -0400752 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_STS, &reg);
Andrew Lunnf39908d2017-02-04 20:02:50 +0100753 if (err)
754 return err;
755
Vivien Didelot5f83dc92017-06-12 12:37:33 -0400756 *cmode = reg & MV88E6XXX_PORT_STS_CMODE_MASK;
Andrew Lunnf39908d2017-02-04 20:02:50 +0100757
758 return 0;
759}
760
Vivien Didelot6c96bbf2017-06-12 12:37:35 -0400761/* Offset 0x02: Jamming Control
Andrew Lunnb35d322a2016-12-03 04:45:19 +0100762 *
763 * Do not limit the period of time that this port can be paused for by
764 * the remote end or the period of time that this port can pause the
765 * remote end.
766 */
Vivien Didelot08984322017-06-08 18:34:12 -0400767int mv88e6097_port_pause_limit(struct mv88e6xxx_chip *chip, int port, u8 in,
768 u8 out)
Andrew Lunnb35d322a2016-12-03 04:45:19 +0100769{
Vivien Didelot6c96bbf2017-06-12 12:37:35 -0400770 return mv88e6xxx_port_write(chip, port, MV88E6097_PORT_JAM_CTL,
771 out << 8 | in);
Andrew Lunnb35d322a2016-12-03 04:45:19 +0100772}
773
Vivien Didelot08984322017-06-08 18:34:12 -0400774int mv88e6390_port_pause_limit(struct mv88e6xxx_chip *chip, int port, u8 in,
775 u8 out)
Andrew Lunn3ce0e652016-12-03 04:45:20 +0100776{
777 int err;
778
Vivien Didelot6c96bbf2017-06-12 12:37:35 -0400779 err = mv88e6xxx_port_write(chip, port, MV88E6390_PORT_FLOW_CTL,
780 MV88E6390_PORT_FLOW_CTL_UPDATE |
781 MV88E6390_PORT_FLOW_CTL_LIMIT_IN | in);
Andrew Lunn3ce0e652016-12-03 04:45:20 +0100782 if (err)
783 return err;
784
Vivien Didelot6c96bbf2017-06-12 12:37:35 -0400785 return mv88e6xxx_port_write(chip, port, MV88E6390_PORT_FLOW_CTL,
786 MV88E6390_PORT_FLOW_CTL_UPDATE |
787 MV88E6390_PORT_FLOW_CTL_LIMIT_OUT | out);
Andrew Lunn3ce0e652016-12-03 04:45:20 +0100788}
789
Vivien Didelote28def332016-11-04 03:23:27 +0100790/* Offset 0x04: Port Control Register */
791
792static const char * const mv88e6xxx_port_state_names[] = {
Vivien Didelota89b433be2017-06-12 12:37:37 -0400793 [MV88E6XXX_PORT_CTL0_STATE_DISABLED] = "Disabled",
794 [MV88E6XXX_PORT_CTL0_STATE_BLOCKING] = "Blocking/Listening",
795 [MV88E6XXX_PORT_CTL0_STATE_LEARNING] = "Learning",
796 [MV88E6XXX_PORT_CTL0_STATE_FORWARDING] = "Forwarding",
Vivien Didelote28def332016-11-04 03:23:27 +0100797};
798
799int mv88e6xxx_port_set_state(struct mv88e6xxx_chip *chip, int port, u8 state)
800{
801 u16 reg;
802 int err;
803
Vivien Didelota89b433be2017-06-12 12:37:37 -0400804 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
Vivien Didelote28def332016-11-04 03:23:27 +0100805 if (err)
806 return err;
807
Vivien Didelota89b433be2017-06-12 12:37:37 -0400808 reg &= ~MV88E6XXX_PORT_CTL0_STATE_MASK;
Vivien Didelotf894c292017-06-08 18:34:10 -0400809
810 switch (state) {
811 case BR_STATE_DISABLED:
Vivien Didelota89b433be2017-06-12 12:37:37 -0400812 state = MV88E6XXX_PORT_CTL0_STATE_DISABLED;
Vivien Didelotf894c292017-06-08 18:34:10 -0400813 break;
814 case BR_STATE_BLOCKING:
815 case BR_STATE_LISTENING:
Vivien Didelota89b433be2017-06-12 12:37:37 -0400816 state = MV88E6XXX_PORT_CTL0_STATE_BLOCKING;
Vivien Didelotf894c292017-06-08 18:34:10 -0400817 break;
818 case BR_STATE_LEARNING:
Vivien Didelota89b433be2017-06-12 12:37:37 -0400819 state = MV88E6XXX_PORT_CTL0_STATE_LEARNING;
Vivien Didelotf894c292017-06-08 18:34:10 -0400820 break;
821 case BR_STATE_FORWARDING:
Vivien Didelota89b433be2017-06-12 12:37:37 -0400822 state = MV88E6XXX_PORT_CTL0_STATE_FORWARDING;
Vivien Didelotf894c292017-06-08 18:34:10 -0400823 break;
824 default:
825 return -EINVAL;
826 }
827
Vivien Didelote28def332016-11-04 03:23:27 +0100828 reg |= state;
829
Vivien Didelota89b433be2017-06-12 12:37:37 -0400830 err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
Vivien Didelote28def332016-11-04 03:23:27 +0100831 if (err)
832 return err;
833
Vivien Didelot774439e52017-06-08 18:34:08 -0400834 dev_dbg(chip->dev, "p%d: PortState set to %s\n", port,
835 mv88e6xxx_port_state_names[state]);
Vivien Didelote28def332016-11-04 03:23:27 +0100836
837 return 0;
838}
Vivien Didelot5a7921f2016-11-04 03:23:28 +0100839
Andrew Lunn56995cb2016-12-03 04:35:19 +0100840int mv88e6xxx_port_set_egress_mode(struct mv88e6xxx_chip *chip, int port,
Vivien Didelot31bef4e2017-06-08 18:34:09 -0400841 enum mv88e6xxx_egress_mode mode)
Andrew Lunn56995cb2016-12-03 04:35:19 +0100842{
843 int err;
844 u16 reg;
845
Vivien Didelota89b433be2017-06-12 12:37:37 -0400846 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
Andrew Lunn56995cb2016-12-03 04:35:19 +0100847 if (err)
848 return err;
849
Vivien Didelota89b433be2017-06-12 12:37:37 -0400850 reg &= ~MV88E6XXX_PORT_CTL0_EGRESS_MODE_MASK;
Vivien Didelot31bef4e2017-06-08 18:34:09 -0400851
852 switch (mode) {
853 case MV88E6XXX_EGRESS_MODE_UNMODIFIED:
Vivien Didelota89b433be2017-06-12 12:37:37 -0400854 reg |= MV88E6XXX_PORT_CTL0_EGRESS_MODE_UNMODIFIED;
Vivien Didelot31bef4e2017-06-08 18:34:09 -0400855 break;
856 case MV88E6XXX_EGRESS_MODE_UNTAGGED:
Vivien Didelota89b433be2017-06-12 12:37:37 -0400857 reg |= MV88E6XXX_PORT_CTL0_EGRESS_MODE_UNTAGGED;
Vivien Didelot31bef4e2017-06-08 18:34:09 -0400858 break;
859 case MV88E6XXX_EGRESS_MODE_TAGGED:
Vivien Didelota89b433be2017-06-12 12:37:37 -0400860 reg |= MV88E6XXX_PORT_CTL0_EGRESS_MODE_TAGGED;
Vivien Didelot31bef4e2017-06-08 18:34:09 -0400861 break;
862 case MV88E6XXX_EGRESS_MODE_ETHERTYPE:
Vivien Didelota89b433be2017-06-12 12:37:37 -0400863 reg |= MV88E6XXX_PORT_CTL0_EGRESS_MODE_ETHER_TYPE_DSA;
Vivien Didelot31bef4e2017-06-08 18:34:09 -0400864 break;
865 default:
866 return -EINVAL;
867 }
Andrew Lunn56995cb2016-12-03 04:35:19 +0100868
Vivien Didelota89b433be2017-06-12 12:37:37 -0400869 return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
Andrew Lunn56995cb2016-12-03 04:35:19 +0100870}
871
872int mv88e6085_port_set_frame_mode(struct mv88e6xxx_chip *chip, int port,
873 enum mv88e6xxx_frame_mode mode)
874{
875 int err;
876 u16 reg;
877
Vivien Didelota89b433be2017-06-12 12:37:37 -0400878 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
Andrew Lunn56995cb2016-12-03 04:35:19 +0100879 if (err)
880 return err;
881
Vivien Didelota89b433be2017-06-12 12:37:37 -0400882 reg &= ~MV88E6XXX_PORT_CTL0_FRAME_MODE_MASK;
Andrew Lunn56995cb2016-12-03 04:35:19 +0100883
884 switch (mode) {
885 case MV88E6XXX_FRAME_MODE_NORMAL:
Vivien Didelota89b433be2017-06-12 12:37:37 -0400886 reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_NORMAL;
Andrew Lunn56995cb2016-12-03 04:35:19 +0100887 break;
888 case MV88E6XXX_FRAME_MODE_DSA:
Vivien Didelota89b433be2017-06-12 12:37:37 -0400889 reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_DSA;
Andrew Lunn56995cb2016-12-03 04:35:19 +0100890 break;
891 default:
892 return -EINVAL;
893 }
894
Vivien Didelota89b433be2017-06-12 12:37:37 -0400895 return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
Andrew Lunn56995cb2016-12-03 04:35:19 +0100896}
897
898int mv88e6351_port_set_frame_mode(struct mv88e6xxx_chip *chip, int port,
899 enum mv88e6xxx_frame_mode mode)
900{
901 int err;
902 u16 reg;
903
Vivien Didelota89b433be2017-06-12 12:37:37 -0400904 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
Andrew Lunn56995cb2016-12-03 04:35:19 +0100905 if (err)
906 return err;
907
Vivien Didelota89b433be2017-06-12 12:37:37 -0400908 reg &= ~MV88E6XXX_PORT_CTL0_FRAME_MODE_MASK;
Andrew Lunn56995cb2016-12-03 04:35:19 +0100909
910 switch (mode) {
911 case MV88E6XXX_FRAME_MODE_NORMAL:
Vivien Didelota89b433be2017-06-12 12:37:37 -0400912 reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_NORMAL;
Andrew Lunn56995cb2016-12-03 04:35:19 +0100913 break;
914 case MV88E6XXX_FRAME_MODE_DSA:
Vivien Didelota89b433be2017-06-12 12:37:37 -0400915 reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_DSA;
Andrew Lunn56995cb2016-12-03 04:35:19 +0100916 break;
917 case MV88E6XXX_FRAME_MODE_PROVIDER:
Vivien Didelota89b433be2017-06-12 12:37:37 -0400918 reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_PROVIDER;
Andrew Lunn56995cb2016-12-03 04:35:19 +0100919 break;
920 case MV88E6XXX_FRAME_MODE_ETHERTYPE:
Vivien Didelota89b433be2017-06-12 12:37:37 -0400921 reg |= MV88E6XXX_PORT_CTL0_FRAME_MODE_ETHER_TYPE_DSA;
Andrew Lunn56995cb2016-12-03 04:35:19 +0100922 break;
923 default:
924 return -EINVAL;
925 }
926
Vivien Didelota89b433be2017-06-12 12:37:37 -0400927 return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
Andrew Lunn56995cb2016-12-03 04:35:19 +0100928}
929
Vladimir Olteana8b659e2021-02-12 17:15:56 +0200930int mv88e6185_port_set_forward_unknown(struct mv88e6xxx_chip *chip,
931 int port, bool unicast)
Andrew Lunn56995cb2016-12-03 04:35:19 +0100932{
933 int err;
934 u16 reg;
935
Vivien Didelota89b433be2017-06-12 12:37:37 -0400936 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
Andrew Lunn56995cb2016-12-03 04:35:19 +0100937 if (err)
938 return err;
939
Vivien Didelot601aeed2017-03-11 16:13:00 -0500940 if (unicast)
Vivien Didelota89b433be2017-06-12 12:37:37 -0400941 reg |= MV88E6185_PORT_CTL0_FORWARD_UNKNOWN;
Andrew Lunn56995cb2016-12-03 04:35:19 +0100942 else
Vivien Didelota89b433be2017-06-12 12:37:37 -0400943 reg &= ~MV88E6185_PORT_CTL0_FORWARD_UNKNOWN;
Andrew Lunn56995cb2016-12-03 04:35:19 +0100944
Vivien Didelota89b433be2017-06-12 12:37:37 -0400945 return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
Andrew Lunn56995cb2016-12-03 04:35:19 +0100946}
947
Vladimir Olteana8b659e2021-02-12 17:15:56 +0200948int mv88e6352_port_set_ucast_flood(struct mv88e6xxx_chip *chip, int port,
949 bool unicast)
Andrew Lunn56995cb2016-12-03 04:35:19 +0100950{
951 int err;
952 u16 reg;
953
Vivien Didelota89b433be2017-06-12 12:37:37 -0400954 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
Andrew Lunn56995cb2016-12-03 04:35:19 +0100955 if (err)
956 return err;
957
Vladimir Olteana8b659e2021-02-12 17:15:56 +0200958 if (unicast)
959 reg |= MV88E6352_PORT_CTL0_EGRESS_FLOODS_UC;
Andrew Lunn56995cb2016-12-03 04:35:19 +0100960 else
Vladimir Olteana8b659e2021-02-12 17:15:56 +0200961 reg &= ~MV88E6352_PORT_CTL0_EGRESS_FLOODS_UC;
962
963 return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
964}
965
966int mv88e6352_port_set_mcast_flood(struct mv88e6xxx_chip *chip, int port,
967 bool multicast)
968{
969 int err;
970 u16 reg;
971
972 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL0, &reg);
973 if (err)
974 return err;
975
976 if (multicast)
977 reg |= MV88E6352_PORT_CTL0_EGRESS_FLOODS_MC;
978 else
979 reg &= ~MV88E6352_PORT_CTL0_EGRESS_FLOODS_MC;
Andrew Lunn56995cb2016-12-03 04:35:19 +0100980
Vivien Didelota89b433be2017-06-12 12:37:37 -0400981 return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL0, reg);
Andrew Lunn56995cb2016-12-03 04:35:19 +0100982}
983
Vivien Didelotb4e48c52016-11-04 03:23:29 +0100984/* Offset 0x05: Port Control 1 */
985
Vivien Didelotea698f42017-03-11 16:12:50 -0500986int mv88e6xxx_port_set_message_port(struct mv88e6xxx_chip *chip, int port,
987 bool message_port)
988{
989 u16 val;
990 int err;
991
Vivien Didelotcd985bb2017-06-12 12:37:38 -0400992 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL1, &val);
Vivien Didelotea698f42017-03-11 16:12:50 -0500993 if (err)
994 return err;
995
996 if (message_port)
Vivien Didelotcd985bb2017-06-12 12:37:38 -0400997 val |= MV88E6XXX_PORT_CTL1_MESSAGE_PORT;
Vivien Didelotea698f42017-03-11 16:12:50 -0500998 else
Vivien Didelotcd985bb2017-06-12 12:37:38 -0400999 val &= ~MV88E6XXX_PORT_CTL1_MESSAGE_PORT;
Vivien Didelotea698f42017-03-11 16:12:50 -05001000
Vivien Didelotcd985bb2017-06-12 12:37:38 -04001001 return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL1, val);
Vivien Didelotea698f42017-03-11 16:12:50 -05001002}
1003
Tobias Waldekranz57e661a2021-01-13 09:42:54 +01001004int mv88e6xxx_port_set_trunk(struct mv88e6xxx_chip *chip, int port,
1005 bool trunk, u8 id)
1006{
1007 u16 val;
1008 int err;
1009
1010 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL1, &val);
1011 if (err)
1012 return err;
1013
1014 val &= ~MV88E6XXX_PORT_CTL1_TRUNK_ID_MASK;
1015
1016 if (trunk)
1017 val |= MV88E6XXX_PORT_CTL1_TRUNK_PORT |
1018 (id << MV88E6XXX_PORT_CTL1_TRUNK_ID_SHIFT);
1019 else
1020 val &= ~MV88E6XXX_PORT_CTL1_TRUNK_PORT;
1021
1022 return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL1, val);
1023}
1024
Vivien Didelot5a7921f2016-11-04 03:23:28 +01001025/* Offset 0x06: Port Based VLAN Map */
1026
1027int mv88e6xxx_port_set_vlan_map(struct mv88e6xxx_chip *chip, int port, u16 map)
1028{
Vivien Didelot4d294af2017-03-11 16:12:47 -05001029 const u16 mask = mv88e6xxx_port_mask(chip);
Vivien Didelot5a7921f2016-11-04 03:23:28 +01001030 u16 reg;
1031 int err;
1032
Vivien Didelot7e5cc5f2017-06-12 12:37:39 -04001033 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_BASE_VLAN, &reg);
Vivien Didelot5a7921f2016-11-04 03:23:28 +01001034 if (err)
1035 return err;
1036
1037 reg &= ~mask;
1038 reg |= map & mask;
1039
Vivien Didelot7e5cc5f2017-06-12 12:37:39 -04001040 err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_BASE_VLAN, reg);
Vivien Didelot5a7921f2016-11-04 03:23:28 +01001041 if (err)
1042 return err;
1043
Vivien Didelot774439e52017-06-08 18:34:08 -04001044 dev_dbg(chip->dev, "p%d: VLANTable set to %.3x\n", port, map);
Vivien Didelot5a7921f2016-11-04 03:23:28 +01001045
1046 return 0;
1047}
Vivien Didelotb4e48c52016-11-04 03:23:29 +01001048
1049int mv88e6xxx_port_get_fid(struct mv88e6xxx_chip *chip, int port, u16 *fid)
1050{
1051 const u16 upper_mask = (mv88e6xxx_num_databases(chip) - 1) >> 4;
1052 u16 reg;
1053 int err;
1054
1055 /* Port's default FID lower 4 bits are located in reg 0x06, offset 12 */
Vivien Didelot7e5cc5f2017-06-12 12:37:39 -04001056 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_BASE_VLAN, &reg);
Vivien Didelotb4e48c52016-11-04 03:23:29 +01001057 if (err)
1058 return err;
1059
1060 *fid = (reg & 0xf000) >> 12;
1061
1062 /* Port's default FID upper bits are located in reg 0x05, offset 0 */
1063 if (upper_mask) {
Vivien Didelotcd985bb2017-06-12 12:37:38 -04001064 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL1,
1065 &reg);
Vivien Didelotb4e48c52016-11-04 03:23:29 +01001066 if (err)
1067 return err;
1068
1069 *fid |= (reg & upper_mask) << 4;
1070 }
1071
1072 return 0;
1073}
1074
1075int mv88e6xxx_port_set_fid(struct mv88e6xxx_chip *chip, int port, u16 fid)
1076{
1077 const u16 upper_mask = (mv88e6xxx_num_databases(chip) - 1) >> 4;
1078 u16 reg;
1079 int err;
1080
1081 if (fid >= mv88e6xxx_num_databases(chip))
1082 return -EINVAL;
1083
1084 /* Port's default FID lower 4 bits are located in reg 0x06, offset 12 */
Vivien Didelot7e5cc5f2017-06-12 12:37:39 -04001085 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_BASE_VLAN, &reg);
Vivien Didelotb4e48c52016-11-04 03:23:29 +01001086 if (err)
1087 return err;
1088
1089 reg &= 0x0fff;
1090 reg |= (fid & 0x000f) << 12;
1091
Vivien Didelot7e5cc5f2017-06-12 12:37:39 -04001092 err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_BASE_VLAN, reg);
Vivien Didelotb4e48c52016-11-04 03:23:29 +01001093 if (err)
1094 return err;
1095
1096 /* Port's default FID upper bits are located in reg 0x05, offset 0 */
1097 if (upper_mask) {
Vivien Didelotcd985bb2017-06-12 12:37:38 -04001098 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL1,
1099 &reg);
Vivien Didelotb4e48c52016-11-04 03:23:29 +01001100 if (err)
1101 return err;
1102
1103 reg &= ~upper_mask;
1104 reg |= (fid >> 4) & upper_mask;
1105
Vivien Didelotcd985bb2017-06-12 12:37:38 -04001106 err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL1,
1107 reg);
Vivien Didelotb4e48c52016-11-04 03:23:29 +01001108 if (err)
1109 return err;
1110 }
1111
Vivien Didelot774439e52017-06-08 18:34:08 -04001112 dev_dbg(chip->dev, "p%d: FID set to %u\n", port, fid);
Vivien Didelotb4e48c52016-11-04 03:23:29 +01001113
1114 return 0;
1115}
Vivien Didelot77064f32016-11-04 03:23:30 +01001116
1117/* Offset 0x07: Default Port VLAN ID & Priority */
1118
1119int mv88e6xxx_port_get_pvid(struct mv88e6xxx_chip *chip, int port, u16 *pvid)
1120{
1121 u16 reg;
1122 int err;
1123
Vivien Didelotb7929fb2017-06-12 12:37:40 -04001124 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_DEFAULT_VLAN,
1125 &reg);
Vivien Didelot77064f32016-11-04 03:23:30 +01001126 if (err)
1127 return err;
1128
Vivien Didelotb7929fb2017-06-12 12:37:40 -04001129 *pvid = reg & MV88E6XXX_PORT_DEFAULT_VLAN_MASK;
Vivien Didelot77064f32016-11-04 03:23:30 +01001130
1131 return 0;
1132}
1133
1134int mv88e6xxx_port_set_pvid(struct mv88e6xxx_chip *chip, int port, u16 pvid)
1135{
1136 u16 reg;
1137 int err;
1138
Vivien Didelotb7929fb2017-06-12 12:37:40 -04001139 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_DEFAULT_VLAN,
1140 &reg);
Vivien Didelot77064f32016-11-04 03:23:30 +01001141 if (err)
1142 return err;
1143
Vivien Didelotb7929fb2017-06-12 12:37:40 -04001144 reg &= ~MV88E6XXX_PORT_DEFAULT_VLAN_MASK;
1145 reg |= pvid & MV88E6XXX_PORT_DEFAULT_VLAN_MASK;
Vivien Didelot77064f32016-11-04 03:23:30 +01001146
Vivien Didelotb7929fb2017-06-12 12:37:40 -04001147 err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_DEFAULT_VLAN,
1148 reg);
Vivien Didelot77064f32016-11-04 03:23:30 +01001149 if (err)
1150 return err;
1151
Vivien Didelot774439e52017-06-08 18:34:08 -04001152 dev_dbg(chip->dev, "p%d: DefaultVID set to %u\n", port, pvid);
Vivien Didelot77064f32016-11-04 03:23:30 +01001153
1154 return 0;
1155}
Vivien Didelot385a0992016-11-04 03:23:31 +01001156
1157/* Offset 0x08: Port Control 2 Register */
1158
1159static const char * const mv88e6xxx_port_8021q_mode_names[] = {
Vivien Didelot81c6edb2017-06-12 12:37:41 -04001160 [MV88E6XXX_PORT_CTL2_8021Q_MODE_DISABLED] = "Disabled",
1161 [MV88E6XXX_PORT_CTL2_8021Q_MODE_FALLBACK] = "Fallback",
1162 [MV88E6XXX_PORT_CTL2_8021Q_MODE_CHECK] = "Check",
1163 [MV88E6XXX_PORT_CTL2_8021Q_MODE_SECURE] = "Secure",
Vivien Didelot385a0992016-11-04 03:23:31 +01001164};
1165
Vladimir Olteana8b659e2021-02-12 17:15:56 +02001166int mv88e6185_port_set_default_forward(struct mv88e6xxx_chip *chip,
1167 int port, bool multicast)
Andrew Lunna23b2962017-02-04 20:15:28 +01001168{
1169 int err;
1170 u16 reg;
1171
Vivien Didelot81c6edb2017-06-12 12:37:41 -04001172 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
Andrew Lunna23b2962017-02-04 20:15:28 +01001173 if (err)
1174 return err;
1175
Vivien Didelot601aeed2017-03-11 16:13:00 -05001176 if (multicast)
Vivien Didelot81c6edb2017-06-12 12:37:41 -04001177 reg |= MV88E6XXX_PORT_CTL2_DEFAULT_FORWARD;
Andrew Lunna23b2962017-02-04 20:15:28 +01001178 else
Vivien Didelot81c6edb2017-06-12 12:37:41 -04001179 reg &= ~MV88E6XXX_PORT_CTL2_DEFAULT_FORWARD;
Andrew Lunna23b2962017-02-04 20:15:28 +01001180
Vivien Didelot81c6edb2017-06-12 12:37:41 -04001181 return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
Andrew Lunna23b2962017-02-04 20:15:28 +01001182}
1183
1184int mv88e6095_port_set_upstream_port(struct mv88e6xxx_chip *chip, int port,
1185 int upstream_port)
1186{
1187 int err;
1188 u16 reg;
1189
Vivien Didelot81c6edb2017-06-12 12:37:41 -04001190 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
Andrew Lunna23b2962017-02-04 20:15:28 +01001191 if (err)
1192 return err;
1193
Vivien Didelot81c6edb2017-06-12 12:37:41 -04001194 reg &= ~MV88E6095_PORT_CTL2_CPU_PORT_MASK;
Andrew Lunna23b2962017-02-04 20:15:28 +01001195 reg |= upstream_port;
1196
Vivien Didelot81c6edb2017-06-12 12:37:41 -04001197 return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
Andrew Lunna23b2962017-02-04 20:15:28 +01001198}
1199
Iwan R Timmerf0942e02019-11-07 22:11:14 +01001200int mv88e6xxx_port_set_mirror(struct mv88e6xxx_chip *chip, int port,
1201 enum mv88e6xxx_egress_direction direction,
1202 bool mirror)
1203{
1204 bool *mirror_port;
1205 u16 reg;
1206 u16 bit;
1207 int err;
1208
1209 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
1210 if (err)
1211 return err;
1212
1213 switch (direction) {
1214 case MV88E6XXX_EGRESS_DIR_INGRESS:
1215 bit = MV88E6XXX_PORT_CTL2_INGRESS_MONITOR;
1216 mirror_port = &chip->ports[port].mirror_ingress;
1217 break;
1218 case MV88E6XXX_EGRESS_DIR_EGRESS:
1219 bit = MV88E6XXX_PORT_CTL2_EGRESS_MONITOR;
1220 mirror_port = &chip->ports[port].mirror_egress;
1221 break;
1222 default:
1223 return -EINVAL;
1224 }
1225
1226 reg &= ~bit;
1227 if (mirror)
1228 reg |= bit;
1229
1230 err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
1231 if (!err)
1232 *mirror_port = mirror;
1233
1234 return err;
1235}
1236
Vivien Didelot385a0992016-11-04 03:23:31 +01001237int mv88e6xxx_port_set_8021q_mode(struct mv88e6xxx_chip *chip, int port,
1238 u16 mode)
1239{
1240 u16 reg;
1241 int err;
1242
Vivien Didelot81c6edb2017-06-12 12:37:41 -04001243 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
Vivien Didelot385a0992016-11-04 03:23:31 +01001244 if (err)
1245 return err;
1246
Vivien Didelot81c6edb2017-06-12 12:37:41 -04001247 reg &= ~MV88E6XXX_PORT_CTL2_8021Q_MODE_MASK;
1248 reg |= mode & MV88E6XXX_PORT_CTL2_8021Q_MODE_MASK;
Vivien Didelot385a0992016-11-04 03:23:31 +01001249
Vivien Didelot81c6edb2017-06-12 12:37:41 -04001250 err = mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
Vivien Didelot385a0992016-11-04 03:23:31 +01001251 if (err)
1252 return err;
1253
Vivien Didelot774439e52017-06-08 18:34:08 -04001254 dev_dbg(chip->dev, "p%d: 802.1QMode set to %s\n", port,
1255 mv88e6xxx_port_8021q_mode_names[mode]);
Vivien Didelot385a0992016-11-04 03:23:31 +01001256
1257 return 0;
1258}
Andrew Lunnef0a7312016-12-03 04:35:16 +01001259
Andrew Lunna23b2962017-02-04 20:15:28 +01001260int mv88e6xxx_port_set_map_da(struct mv88e6xxx_chip *chip, int port)
1261{
1262 u16 reg;
1263 int err;
1264
Vivien Didelot81c6edb2017-06-12 12:37:41 -04001265 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
Andrew Lunna23b2962017-02-04 20:15:28 +01001266 if (err)
1267 return err;
1268
Vivien Didelot81c6edb2017-06-12 12:37:41 -04001269 reg |= MV88E6XXX_PORT_CTL2_MAP_DA;
Andrew Lunna23b2962017-02-04 20:15:28 +01001270
Vivien Didelot81c6edb2017-06-12 12:37:41 -04001271 return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
Andrew Lunna23b2962017-02-04 20:15:28 +01001272}
1273
Vivien Didelotcd782652017-06-08 18:34:13 -04001274int mv88e6165_port_set_jumbo_size(struct mv88e6xxx_chip *chip, int port,
1275 size_t size)
Andrew Lunn5f436662016-12-03 04:45:17 +01001276{
1277 u16 reg;
1278 int err;
1279
Andrew Lunnb92ce2f2021-09-26 19:41:25 +02001280 size += VLAN_ETH_HLEN + ETH_FCS_LEN;
1281
Vivien Didelot81c6edb2017-06-12 12:37:41 -04001282 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_CTL2, &reg);
Andrew Lunn5f436662016-12-03 04:45:17 +01001283 if (err)
1284 return err;
1285
Vivien Didelot81c6edb2017-06-12 12:37:41 -04001286 reg &= ~MV88E6XXX_PORT_CTL2_JUMBO_MODE_MASK;
Vivien Didelotcd782652017-06-08 18:34:13 -04001287
1288 if (size <= 1522)
Vivien Didelot81c6edb2017-06-12 12:37:41 -04001289 reg |= MV88E6XXX_PORT_CTL2_JUMBO_MODE_1522;
Vivien Didelotcd782652017-06-08 18:34:13 -04001290 else if (size <= 2048)
Vivien Didelot81c6edb2017-06-12 12:37:41 -04001291 reg |= MV88E6XXX_PORT_CTL2_JUMBO_MODE_2048;
Vivien Didelotcd782652017-06-08 18:34:13 -04001292 else if (size <= 10240)
Vivien Didelot81c6edb2017-06-12 12:37:41 -04001293 reg |= MV88E6XXX_PORT_CTL2_JUMBO_MODE_10240;
Vivien Didelotcd782652017-06-08 18:34:13 -04001294 else
1295 return -ERANGE;
Andrew Lunn5f436662016-12-03 04:45:17 +01001296
Vivien Didelot81c6edb2017-06-12 12:37:41 -04001297 return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_CTL2, reg);
Andrew Lunn5f436662016-12-03 04:45:17 +01001298}
1299
Andrew Lunnef70b112016-12-03 04:45:18 +01001300/* Offset 0x09: Port Rate Control */
1301
1302int mv88e6095_port_egress_rate_limiting(struct mv88e6xxx_chip *chip, int port)
1303{
Vivien Didelot2cb8cb12017-06-12 12:37:42 -04001304 return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_EGRESS_RATE_CTL1,
1305 0x0000);
Andrew Lunnef70b112016-12-03 04:45:18 +01001306}
1307
1308int mv88e6097_port_egress_rate_limiting(struct mv88e6xxx_chip *chip, int port)
1309{
Vivien Didelot2cb8cb12017-06-12 12:37:42 -04001310 return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_EGRESS_RATE_CTL1,
1311 0x0001);
Andrew Lunnef70b112016-12-03 04:45:18 +01001312}
1313
Tobias Waldekranz041bd542021-03-18 20:25:39 +01001314/* Offset 0x0B: Port Association Vector */
1315
1316int mv88e6xxx_port_set_assoc_vector(struct mv88e6xxx_chip *chip, int port,
1317 u16 pav)
1318{
1319 u16 reg, mask;
1320 int err;
1321
1322 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_ASSOC_VECTOR,
1323 &reg);
1324 if (err)
1325 return err;
1326
1327 mask = mv88e6xxx_port_mask(chip);
1328 reg &= ~mask;
1329 reg |= pav & mask;
1330
1331 return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_ASSOC_VECTOR,
1332 reg);
1333}
1334
Vivien Didelotc8c94892017-03-11 16:13:01 -05001335/* Offset 0x0C: Port ATU Control */
1336
1337int mv88e6xxx_port_disable_learn_limit(struct mv88e6xxx_chip *chip, int port)
1338{
Vivien Didelotb8109592017-06-12 12:37:45 -04001339 return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_ATU_CTL, 0);
Vivien Didelotc8c94892017-03-11 16:13:01 -05001340}
1341
Vivien Didelot9dbfb4e2017-03-11 16:13:02 -05001342/* Offset 0x0D: (Priority) Override Register */
1343
1344int mv88e6xxx_port_disable_pri_override(struct mv88e6xxx_chip *chip, int port)
1345{
Vivien Didelotb8109592017-06-12 12:37:45 -04001346 return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_PRI_OVERRIDE, 0);
Vivien Didelot9dbfb4e2017-03-11 16:13:02 -05001347}
1348
Pavana Sharmade776d02021-03-17 14:46:42 +01001349/* Offset 0x0E: Policy & MGMT Control Register for FAMILY 6191X 6193X 6393X */
1350
Marek Behún6584b262021-03-17 14:46:43 +01001351static int mv88e6393x_port_policy_read(struct mv88e6xxx_chip *chip, int port,
1352 u16 pointer, u8 *data)
1353{
1354 u16 reg;
1355 int err;
1356
1357 err = mv88e6xxx_port_write(chip, port, MV88E6393X_PORT_POLICY_MGMT_CTL,
1358 pointer);
1359 if (err)
1360 return err;
1361
1362 err = mv88e6xxx_port_read(chip, port, MV88E6393X_PORT_POLICY_MGMT_CTL,
1363 &reg);
1364 if (err)
1365 return err;
1366
1367 *data = reg;
1368
1369 return 0;
1370}
1371
Pavana Sharmade776d02021-03-17 14:46:42 +01001372static int mv88e6393x_port_policy_write(struct mv88e6xxx_chip *chip, int port,
1373 u16 pointer, u8 data)
1374{
1375 u16 reg;
1376
1377 reg = MV88E6393X_PORT_POLICY_MGMT_CTL_UPDATE | pointer | data;
1378
1379 return mv88e6xxx_port_write(chip, port, MV88E6393X_PORT_POLICY_MGMT_CTL,
1380 reg);
1381}
1382
1383static int mv88e6393x_port_policy_write_all(struct mv88e6xxx_chip *chip,
1384 u16 pointer, u8 data)
1385{
1386 int err, port;
1387
1388 for (port = 0; port < mv88e6xxx_num_ports(chip); port++) {
1389 if (dsa_is_unused_port(chip->ds, port))
1390 continue;
1391
1392 err = mv88e6393x_port_policy_write(chip, port, pointer, data);
1393 if (err)
1394 return err;
1395 }
1396
1397 return 0;
1398}
1399
1400int mv88e6393x_set_egress_port(struct mv88e6xxx_chip *chip,
1401 enum mv88e6xxx_egress_direction direction,
1402 int port)
1403{
1404 u16 ptr;
1405 int err;
1406
1407 switch (direction) {
1408 case MV88E6XXX_EGRESS_DIR_INGRESS:
1409 ptr = MV88E6393X_PORT_POLICY_MGMT_CTL_PTR_INGRESS_DEST;
1410 err = mv88e6393x_port_policy_write_all(chip, ptr, port);
1411 if (err)
1412 return err;
1413 break;
1414 case MV88E6XXX_EGRESS_DIR_EGRESS:
1415 ptr = MV88E6393X_G2_EGRESS_MONITOR_DEST;
1416 err = mv88e6xxx_g2_write(chip, ptr, port);
1417 if (err)
1418 return err;
1419 break;
1420 }
1421
1422 return 0;
1423}
1424
1425int mv88e6393x_port_set_upstream_port(struct mv88e6xxx_chip *chip, int port,
1426 int upstream_port)
1427{
1428 u16 ptr = MV88E6393X_PORT_POLICY_MGMT_CTL_PTR_CPU_DEST;
1429 u8 data = MV88E6393X_PORT_POLICY_MGMT_CTL_CPU_DEST_MGMTPRI |
1430 upstream_port;
1431
1432 return mv88e6393x_port_policy_write(chip, port, ptr, data);
1433}
1434
1435int mv88e6393x_port_mgmt_rsvd2cpu(struct mv88e6xxx_chip *chip)
1436{
1437 u16 ptr;
1438 int err;
1439
1440 /* Consider the frames with reserved multicast destination
1441 * addresses matching 01:80:c2:00:00:00 and
1442 * 01:80:c2:00:00:02 as MGMT.
1443 */
1444 ptr = MV88E6393X_PORT_POLICY_MGMT_CTL_PTR_01C280000000XLO;
1445 err = mv88e6393x_port_policy_write_all(chip, ptr, 0xff);
1446 if (err)
1447 return err;
1448
1449 ptr = MV88E6393X_PORT_POLICY_MGMT_CTL_PTR_01C280000000XHI;
1450 err = mv88e6393x_port_policy_write_all(chip, ptr, 0xff);
1451 if (err)
1452 return err;
1453
1454 ptr = MV88E6393X_PORT_POLICY_MGMT_CTL_PTR_01C280000002XLO;
1455 err = mv88e6393x_port_policy_write_all(chip, ptr, 0xff);
1456 if (err)
1457 return err;
1458
1459 ptr = MV88E6393X_PORT_POLICY_MGMT_CTL_PTR_01C280000002XHI;
1460 err = mv88e6393x_port_policy_write_all(chip, ptr, 0xff);
1461 if (err)
1462 return err;
1463
1464 return 0;
1465}
1466
1467/* Offset 0x10 & 0x11: EPC */
1468
1469static int mv88e6393x_port_epc_wait_ready(struct mv88e6xxx_chip *chip, int port)
1470{
1471 int bit = __bf_shf(MV88E6393X_PORT_EPC_CMD_BUSY);
1472
1473 return mv88e6xxx_port_wait_bit(chip, port, MV88E6393X_PORT_EPC_CMD, bit, 0);
1474}
1475
1476/* Port Ether type for 6393X family */
1477
1478int mv88e6393x_port_set_ether_type(struct mv88e6xxx_chip *chip, int port,
1479 u16 etype)
1480{
1481 u16 val;
1482 int err;
1483
1484 err = mv88e6393x_port_epc_wait_ready(chip, port);
1485 if (err)
1486 return err;
1487
1488 err = mv88e6xxx_port_write(chip, port, MV88E6393X_PORT_EPC_DATA, etype);
1489 if (err)
1490 return err;
1491
1492 val = MV88E6393X_PORT_EPC_CMD_BUSY |
1493 MV88E6393X_PORT_EPC_CMD_WRITE |
1494 MV88E6393X_PORT_EPC_INDEX_PORT_ETYPE;
1495
1496 return mv88e6xxx_port_write(chip, port, MV88E6393X_PORT_EPC_CMD, val);
1497}
1498
Andrew Lunn56995cb2016-12-03 04:35:19 +01001499/* Offset 0x0f: Port Ether type */
1500
1501int mv88e6351_port_set_ether_type(struct mv88e6xxx_chip *chip, int port,
1502 u16 etype)
1503{
Vivien Didelotb8109592017-06-12 12:37:45 -04001504 return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_ETH_TYPE, etype);
Andrew Lunn56995cb2016-12-03 04:35:19 +01001505}
1506
Andrew Lunnef0a7312016-12-03 04:35:16 +01001507/* Offset 0x18: Port IEEE Priority Remapping Registers [0-3]
1508 * Offset 0x19: Port IEEE Priority Remapping Registers [4-7]
1509 */
1510
1511int mv88e6095_port_tag_remap(struct mv88e6xxx_chip *chip, int port)
1512{
1513 int err;
1514
1515 /* Use a direct priority mapping for all IEEE tagged frames */
Vivien Didelot8009df92017-06-12 12:37:44 -04001516 err = mv88e6xxx_port_write(chip, port,
1517 MV88E6095_PORT_IEEE_PRIO_REMAP_0123,
1518 0x3210);
Andrew Lunnef0a7312016-12-03 04:35:16 +01001519 if (err)
1520 return err;
1521
Vivien Didelot8009df92017-06-12 12:37:44 -04001522 return mv88e6xxx_port_write(chip, port,
1523 MV88E6095_PORT_IEEE_PRIO_REMAP_4567,
1524 0x7654);
Andrew Lunnef0a7312016-12-03 04:35:16 +01001525}
1526
1527static int mv88e6xxx_port_ieeepmt_write(struct mv88e6xxx_chip *chip,
Vivien Didelotddcbabf2017-06-17 23:07:14 -04001528 int port, u16 table, u8 ptr, u16 data)
Andrew Lunnef0a7312016-12-03 04:35:16 +01001529{
1530 u16 reg;
1531
Vivien Didelotddcbabf2017-06-17 23:07:14 -04001532 reg = MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_UPDATE | table |
1533 (ptr << __bf_shf(MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_PTR_MASK)) |
1534 (data & MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_DATA_MASK);
Andrew Lunnef0a7312016-12-03 04:35:16 +01001535
Vivien Didelot8009df92017-06-12 12:37:44 -04001536 return mv88e6xxx_port_write(chip, port,
1537 MV88E6390_PORT_IEEE_PRIO_MAP_TABLE, reg);
Andrew Lunnef0a7312016-12-03 04:35:16 +01001538}
1539
1540int mv88e6390_port_tag_remap(struct mv88e6xxx_chip *chip, int port)
1541{
1542 int err, i;
Vivien Didelot8009df92017-06-12 12:37:44 -04001543 u16 table;
Andrew Lunnef0a7312016-12-03 04:35:16 +01001544
1545 for (i = 0; i <= 7; i++) {
Vivien Didelot8009df92017-06-12 12:37:44 -04001546 table = MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_INGRESS_PCP;
1547 err = mv88e6xxx_port_ieeepmt_write(chip, port, table, i,
1548 (i | i << 4));
Andrew Lunnef0a7312016-12-03 04:35:16 +01001549 if (err)
1550 return err;
1551
Vivien Didelot8009df92017-06-12 12:37:44 -04001552 table = MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_EGRESS_GREEN_PCP;
1553 err = mv88e6xxx_port_ieeepmt_write(chip, port, table, i, i);
Andrew Lunnef0a7312016-12-03 04:35:16 +01001554 if (err)
1555 return err;
1556
Vivien Didelot8009df92017-06-12 12:37:44 -04001557 table = MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_EGRESS_YELLOW_PCP;
1558 err = mv88e6xxx_port_ieeepmt_write(chip, port, table, i, i);
Andrew Lunnef0a7312016-12-03 04:35:16 +01001559 if (err)
1560 return err;
1561
Vivien Didelot8009df92017-06-12 12:37:44 -04001562 table = MV88E6390_PORT_IEEE_PRIO_MAP_TABLE_EGRESS_AVB_PCP;
1563 err = mv88e6xxx_port_ieeepmt_write(chip, port, table, i, i);
Andrew Lunnef0a7312016-12-03 04:35:16 +01001564 if (err)
1565 return err;
1566 }
1567
1568 return 0;
1569}
Vivien Didelotf3a2cd32019-09-07 16:00:48 -04001570
1571/* Offset 0x0E: Policy Control Register */
1572
Marek Behún6584b262021-03-17 14:46:43 +01001573static int
1574mv88e6xxx_port_policy_mapping_get_pos(enum mv88e6xxx_policy_mapping mapping,
1575 enum mv88e6xxx_policy_action action,
1576 u16 *mask, u16 *val, int *shift)
1577{
1578 switch (mapping) {
1579 case MV88E6XXX_POLICY_MAPPING_DA:
1580 *shift = __bf_shf(MV88E6XXX_PORT_POLICY_CTL_DA_MASK);
1581 *mask = MV88E6XXX_PORT_POLICY_CTL_DA_MASK;
1582 break;
1583 case MV88E6XXX_POLICY_MAPPING_SA:
1584 *shift = __bf_shf(MV88E6XXX_PORT_POLICY_CTL_SA_MASK);
1585 *mask = MV88E6XXX_PORT_POLICY_CTL_SA_MASK;
1586 break;
1587 case MV88E6XXX_POLICY_MAPPING_VTU:
1588 *shift = __bf_shf(MV88E6XXX_PORT_POLICY_CTL_VTU_MASK);
1589 *mask = MV88E6XXX_PORT_POLICY_CTL_VTU_MASK;
1590 break;
1591 case MV88E6XXX_POLICY_MAPPING_ETYPE:
1592 *shift = __bf_shf(MV88E6XXX_PORT_POLICY_CTL_ETYPE_MASK);
1593 *mask = MV88E6XXX_PORT_POLICY_CTL_ETYPE_MASK;
1594 break;
1595 case MV88E6XXX_POLICY_MAPPING_PPPOE:
1596 *shift = __bf_shf(MV88E6XXX_PORT_POLICY_CTL_PPPOE_MASK);
1597 *mask = MV88E6XXX_PORT_POLICY_CTL_PPPOE_MASK;
1598 break;
1599 case MV88E6XXX_POLICY_MAPPING_VBAS:
1600 *shift = __bf_shf(MV88E6XXX_PORT_POLICY_CTL_VBAS_MASK);
1601 *mask = MV88E6XXX_PORT_POLICY_CTL_VBAS_MASK;
1602 break;
1603 case MV88E6XXX_POLICY_MAPPING_OPT82:
1604 *shift = __bf_shf(MV88E6XXX_PORT_POLICY_CTL_OPT82_MASK);
1605 *mask = MV88E6XXX_PORT_POLICY_CTL_OPT82_MASK;
1606 break;
1607 case MV88E6XXX_POLICY_MAPPING_UDP:
1608 *shift = __bf_shf(MV88E6XXX_PORT_POLICY_CTL_UDP_MASK);
1609 *mask = MV88E6XXX_PORT_POLICY_CTL_UDP_MASK;
1610 break;
1611 default:
1612 return -EOPNOTSUPP;
1613 }
1614
1615 switch (action) {
1616 case MV88E6XXX_POLICY_ACTION_NORMAL:
1617 *val = MV88E6XXX_PORT_POLICY_CTL_NORMAL;
1618 break;
1619 case MV88E6XXX_POLICY_ACTION_MIRROR:
1620 *val = MV88E6XXX_PORT_POLICY_CTL_MIRROR;
1621 break;
1622 case MV88E6XXX_POLICY_ACTION_TRAP:
1623 *val = MV88E6XXX_PORT_POLICY_CTL_TRAP;
1624 break;
1625 case MV88E6XXX_POLICY_ACTION_DISCARD:
1626 *val = MV88E6XXX_PORT_POLICY_CTL_DISCARD;
1627 break;
1628 default:
1629 return -EOPNOTSUPP;
1630 }
1631
1632 return 0;
1633}
1634
Vivien Didelotf3a2cd32019-09-07 16:00:48 -04001635int mv88e6352_port_set_policy(struct mv88e6xxx_chip *chip, int port,
1636 enum mv88e6xxx_policy_mapping mapping,
1637 enum mv88e6xxx_policy_action action)
1638{
1639 u16 reg, mask, val;
1640 int shift;
1641 int err;
1642
Marek Behún6584b262021-03-17 14:46:43 +01001643 err = mv88e6xxx_port_policy_mapping_get_pos(mapping, action, &mask,
1644 &val, &shift);
1645 if (err)
1646 return err;
Vivien Didelotf3a2cd32019-09-07 16:00:48 -04001647
1648 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_POLICY_CTL, &reg);
1649 if (err)
1650 return err;
1651
1652 reg &= ~mask;
1653 reg |= (val << shift) & mask;
1654
1655 return mv88e6xxx_port_write(chip, port, MV88E6XXX_PORT_POLICY_CTL, reg);
1656}
Marek Behún6584b262021-03-17 14:46:43 +01001657
1658int mv88e6393x_port_set_policy(struct mv88e6xxx_chip *chip, int port,
1659 enum mv88e6xxx_policy_mapping mapping,
1660 enum mv88e6xxx_policy_action action)
1661{
1662 u16 mask, val;
1663 int shift;
1664 int err;
1665 u16 ptr;
1666 u8 reg;
1667
1668 err = mv88e6xxx_port_policy_mapping_get_pos(mapping, action, &mask,
1669 &val, &shift);
1670 if (err)
1671 return err;
1672
1673 /* The 16-bit Port Policy CTL register from older chips is on 6393x
1674 * changed to Port Policy MGMT CTL, which can access more data, but
1675 * indirectly. The original 16-bit value is divided into two 8-bit
1676 * registers.
1677 */
1678 ptr = shift / 8;
1679 shift %= 8;
1680 mask >>= ptr * 8;
1681
1682 err = mv88e6393x_port_policy_read(chip, port, ptr, &reg);
1683 if (err)
1684 return err;
1685
1686 reg &= ~mask;
1687 reg |= (val << shift) & mask;
1688
1689 return mv88e6393x_port_policy_write(chip, port, ptr, reg);
1690}