blob: 9ee1be20a946263ee734a044c55999577f3bccff [file] [log] [blame]
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001/*
2 * net/dsa/mv88e6xxx.c - Marvell 88e6xxx switch chip support
3 * Copyright (c) 2008 Marvell Semiconductor
4 *
Vivien Didelotb8fee952015-08-13 12:52:19 -04005 * Copyright (c) 2015 CMC Electronics, Inc.
6 * Added support for VLAN Table Unit operations
7 *
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
Barry Grussling19b2f972013-01-08 16:05:54 +000014#include <linux/delay.h>
Guenter Roeckdefb05b2015-03-26 18:36:38 -070015#include <linux/etherdevice.h>
Andrew Lunndea87022015-08-31 15:56:47 +020016#include <linux/ethtool.h>
Guenter Roeckfacd95b2015-03-26 18:36:35 -070017#include <linux/if_bridge.h>
Barry Grussling19b2f972013-01-08 16:05:54 +000018#include <linux/jiffies.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000019#include <linux/list.h>
Paul Gortmaker2bbba272012-01-24 10:41:40 +000020#include <linux/module.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000021#include <linux/netdevice.h>
22#include <linux/phy.h>
Ben Hutchingsc8f0b862011-11-27 17:06:08 +000023#include <net/dsa.h>
Vivien Didelot1f36faf2015-10-08 11:35:13 -040024#include <net/switchdev.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000025#include "mv88e6xxx.h"
26
Barry Grussling3675c8d2013-01-08 16:05:53 +000027/* If the switch's ADDR[4:0] strap pins are strapped to zero, it will
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000028 * use all 32 SMI bus addresses on its SMI bus, and all switch registers
29 * will be directly accessible on some {device address,register address}
30 * pair. If the ADDR[4:0] pins are not strapped to zero, the switch
31 * will only respond to SMI transactions to that specific address, and
32 * an indirect addressing mechanism needs to be used to access its
33 * registers.
34 */
35static int mv88e6xxx_reg_wait_ready(struct mii_bus *bus, int sw_addr)
36{
37 int ret;
38 int i;
39
40 for (i = 0; i < 16; i++) {
Neil Armstrong6e899e62015-10-22 10:37:53 +020041 ret = mdiobus_read_nested(bus, sw_addr, SMI_CMD);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000042 if (ret < 0)
43 return ret;
44
Andrew Lunncca8b132015-04-02 04:06:39 +020045 if ((ret & SMI_CMD_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000046 return 0;
47 }
48
49 return -ETIMEDOUT;
50}
51
52int __mv88e6xxx_reg_read(struct mii_bus *bus, int sw_addr, int addr, int reg)
53{
54 int ret;
55
56 if (sw_addr == 0)
Neil Armstrong6e899e62015-10-22 10:37:53 +020057 return mdiobus_read_nested(bus, addr, reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000058
Barry Grussling3675c8d2013-01-08 16:05:53 +000059 /* Wait for the bus to become free. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000060 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
61 if (ret < 0)
62 return ret;
63
Barry Grussling3675c8d2013-01-08 16:05:53 +000064 /* Transmit the read command. */
Neil Armstrong6e899e62015-10-22 10:37:53 +020065 ret = mdiobus_write_nested(bus, sw_addr, SMI_CMD,
66 SMI_CMD_OP_22_READ | (addr << 5) | reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000067 if (ret < 0)
68 return ret;
69
Barry Grussling3675c8d2013-01-08 16:05:53 +000070 /* Wait for the read command to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000071 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
72 if (ret < 0)
73 return ret;
74
Barry Grussling3675c8d2013-01-08 16:05:53 +000075 /* Read the data. */
Neil Armstrong6e899e62015-10-22 10:37:53 +020076 ret = mdiobus_read_nested(bus, sw_addr, SMI_DATA);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000077 if (ret < 0)
78 return ret;
79
80 return ret & 0xffff;
81}
82
Guenter Roeck8d6d09e2015-03-26 18:36:31 -070083/* Must be called with SMI mutex held */
84static int _mv88e6xxx_reg_read(struct dsa_switch *ds, int addr, int reg)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000085{
Guenter Roeckb184e492014-10-17 12:30:58 -070086 struct mii_bus *bus = dsa_host_dev_to_mii_bus(ds->master_dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000087 int ret;
88
Guenter Roeckb184e492014-10-17 12:30:58 -070089 if (bus == NULL)
90 return -EINVAL;
91
Guenter Roeckb184e492014-10-17 12:30:58 -070092 ret = __mv88e6xxx_reg_read(bus, ds->pd->sw_addr, addr, reg);
Vivien Didelotbb92ea52015-01-23 16:10:36 -050093 if (ret < 0)
94 return ret;
95
96 dev_dbg(ds->master_dev, "<- addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
97 addr, reg, ret);
98
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000099 return ret;
100}
101
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700102int mv88e6xxx_reg_read(struct dsa_switch *ds, int addr, int reg)
103{
104 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
105 int ret;
106
107 mutex_lock(&ps->smi_mutex);
108 ret = _mv88e6xxx_reg_read(ds, addr, reg);
109 mutex_unlock(&ps->smi_mutex);
110
111 return ret;
112}
113
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000114int __mv88e6xxx_reg_write(struct mii_bus *bus, int sw_addr, int addr,
115 int reg, u16 val)
116{
117 int ret;
118
119 if (sw_addr == 0)
Neil Armstrong6e899e62015-10-22 10:37:53 +0200120 return mdiobus_write_nested(bus, addr, reg, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000121
Barry Grussling3675c8d2013-01-08 16:05:53 +0000122 /* Wait for the bus to become free. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000123 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
124 if (ret < 0)
125 return ret;
126
Barry Grussling3675c8d2013-01-08 16:05:53 +0000127 /* Transmit the data to write. */
Neil Armstrong6e899e62015-10-22 10:37:53 +0200128 ret = mdiobus_write_nested(bus, sw_addr, SMI_DATA, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000129 if (ret < 0)
130 return ret;
131
Barry Grussling3675c8d2013-01-08 16:05:53 +0000132 /* Transmit the write command. */
Neil Armstrong6e899e62015-10-22 10:37:53 +0200133 ret = mdiobus_write_nested(bus, sw_addr, SMI_CMD,
134 SMI_CMD_OP_22_WRITE | (addr << 5) | reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000135 if (ret < 0)
136 return ret;
137
Barry Grussling3675c8d2013-01-08 16:05:53 +0000138 /* Wait for the write command to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000139 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
140 if (ret < 0)
141 return ret;
142
143 return 0;
144}
145
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700146/* Must be called with SMI mutex held */
147static int _mv88e6xxx_reg_write(struct dsa_switch *ds, int addr, int reg,
148 u16 val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000149{
Guenter Roeckb184e492014-10-17 12:30:58 -0700150 struct mii_bus *bus = dsa_host_dev_to_mii_bus(ds->master_dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000151
Guenter Roeckb184e492014-10-17 12:30:58 -0700152 if (bus == NULL)
153 return -EINVAL;
154
Vivien Didelotbb92ea52015-01-23 16:10:36 -0500155 dev_dbg(ds->master_dev, "-> addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
156 addr, reg, val);
157
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700158 return __mv88e6xxx_reg_write(bus, ds->pd->sw_addr, addr, reg, val);
159}
160
161int mv88e6xxx_reg_write(struct dsa_switch *ds, int addr, int reg, u16 val)
162{
163 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
164 int ret;
165
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000166 mutex_lock(&ps->smi_mutex);
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700167 ret = _mv88e6xxx_reg_write(ds, addr, reg, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000168 mutex_unlock(&ps->smi_mutex);
169
170 return ret;
171}
172
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000173int mv88e6xxx_set_addr_direct(struct dsa_switch *ds, u8 *addr)
174{
Andrew Lunncca8b132015-04-02 04:06:39 +0200175 REG_WRITE(REG_GLOBAL, GLOBAL_MAC_01, (addr[0] << 8) | addr[1]);
176 REG_WRITE(REG_GLOBAL, GLOBAL_MAC_23, (addr[2] << 8) | addr[3]);
177 REG_WRITE(REG_GLOBAL, GLOBAL_MAC_45, (addr[4] << 8) | addr[5]);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000178
179 return 0;
180}
181
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000182int mv88e6xxx_set_addr_indirect(struct dsa_switch *ds, u8 *addr)
183{
184 int i;
185 int ret;
186
187 for (i = 0; i < 6; i++) {
188 int j;
189
Barry Grussling3675c8d2013-01-08 16:05:53 +0000190 /* Write the MAC address byte. */
Andrew Lunncca8b132015-04-02 04:06:39 +0200191 REG_WRITE(REG_GLOBAL2, GLOBAL2_SWITCH_MAC,
192 GLOBAL2_SWITCH_MAC_BUSY | (i << 8) | addr[i]);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000193
Barry Grussling3675c8d2013-01-08 16:05:53 +0000194 /* Wait for the write to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000195 for (j = 0; j < 16; j++) {
Andrew Lunncca8b132015-04-02 04:06:39 +0200196 ret = REG_READ(REG_GLOBAL2, GLOBAL2_SWITCH_MAC);
197 if ((ret & GLOBAL2_SWITCH_MAC_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000198 break;
199 }
200 if (j == 16)
201 return -ETIMEDOUT;
202 }
203
204 return 0;
205}
206
Andrew Lunn3898c142015-05-06 01:09:53 +0200207/* Must be called with SMI mutex held */
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +0200208static int _mv88e6xxx_phy_read(struct dsa_switch *ds, int addr, int regnum)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000209{
210 if (addr >= 0)
Andrew Lunn3898c142015-05-06 01:09:53 +0200211 return _mv88e6xxx_reg_read(ds, addr, regnum);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000212 return 0xffff;
213}
214
Andrew Lunn3898c142015-05-06 01:09:53 +0200215/* Must be called with SMI mutex held */
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +0200216static int _mv88e6xxx_phy_write(struct dsa_switch *ds, int addr, int regnum,
217 u16 val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000218{
219 if (addr >= 0)
Andrew Lunn3898c142015-05-06 01:09:53 +0200220 return _mv88e6xxx_reg_write(ds, addr, regnum, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000221 return 0;
222}
223
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000224#ifdef CONFIG_NET_DSA_MV88E6XXX_NEED_PPU
225static int mv88e6xxx_ppu_disable(struct dsa_switch *ds)
226{
227 int ret;
Barry Grussling19b2f972013-01-08 16:05:54 +0000228 unsigned long timeout;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000229
Andrew Lunncca8b132015-04-02 04:06:39 +0200230 ret = REG_READ(REG_GLOBAL, GLOBAL_CONTROL);
231 REG_WRITE(REG_GLOBAL, GLOBAL_CONTROL,
232 ret & ~GLOBAL_CONTROL_PPU_ENABLE);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000233
Barry Grussling19b2f972013-01-08 16:05:54 +0000234 timeout = jiffies + 1 * HZ;
235 while (time_before(jiffies, timeout)) {
Andrew Lunncca8b132015-04-02 04:06:39 +0200236 ret = REG_READ(REG_GLOBAL, GLOBAL_STATUS);
Barry Grussling19b2f972013-01-08 16:05:54 +0000237 usleep_range(1000, 2000);
Andrew Lunncca8b132015-04-02 04:06:39 +0200238 if ((ret & GLOBAL_STATUS_PPU_MASK) !=
239 GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000240 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000241 }
242
243 return -ETIMEDOUT;
244}
245
246static int mv88e6xxx_ppu_enable(struct dsa_switch *ds)
247{
248 int ret;
Barry Grussling19b2f972013-01-08 16:05:54 +0000249 unsigned long timeout;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000250
Andrew Lunncca8b132015-04-02 04:06:39 +0200251 ret = REG_READ(REG_GLOBAL, GLOBAL_CONTROL);
252 REG_WRITE(REG_GLOBAL, GLOBAL_CONTROL, ret | GLOBAL_CONTROL_PPU_ENABLE);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000253
Barry Grussling19b2f972013-01-08 16:05:54 +0000254 timeout = jiffies + 1 * HZ;
255 while (time_before(jiffies, timeout)) {
Andrew Lunncca8b132015-04-02 04:06:39 +0200256 ret = REG_READ(REG_GLOBAL, GLOBAL_STATUS);
Barry Grussling19b2f972013-01-08 16:05:54 +0000257 usleep_range(1000, 2000);
Andrew Lunncca8b132015-04-02 04:06:39 +0200258 if ((ret & GLOBAL_STATUS_PPU_MASK) ==
259 GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000260 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000261 }
262
263 return -ETIMEDOUT;
264}
265
266static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
267{
268 struct mv88e6xxx_priv_state *ps;
269
270 ps = container_of(ugly, struct mv88e6xxx_priv_state, ppu_work);
271 if (mutex_trylock(&ps->ppu_mutex)) {
Barry Grussling85686582013-01-08 16:05:56 +0000272 struct dsa_switch *ds = ((struct dsa_switch *)ps) - 1;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000273
Barry Grussling85686582013-01-08 16:05:56 +0000274 if (mv88e6xxx_ppu_enable(ds) == 0)
275 ps->ppu_disabled = 0;
276 mutex_unlock(&ps->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000277 }
278}
279
280static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
281{
282 struct mv88e6xxx_priv_state *ps = (void *)_ps;
283
284 schedule_work(&ps->ppu_work);
285}
286
287static int mv88e6xxx_ppu_access_get(struct dsa_switch *ds)
288{
Florian Fainellia22adce2014-04-28 11:14:28 -0700289 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000290 int ret;
291
292 mutex_lock(&ps->ppu_mutex);
293
Barry Grussling3675c8d2013-01-08 16:05:53 +0000294 /* If the PHY polling unit is enabled, disable it so that
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000295 * we can access the PHY registers. If it was already
296 * disabled, cancel the timer that is going to re-enable
297 * it.
298 */
299 if (!ps->ppu_disabled) {
Barry Grussling85686582013-01-08 16:05:56 +0000300 ret = mv88e6xxx_ppu_disable(ds);
301 if (ret < 0) {
302 mutex_unlock(&ps->ppu_mutex);
303 return ret;
304 }
305 ps->ppu_disabled = 1;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000306 } else {
Barry Grussling85686582013-01-08 16:05:56 +0000307 del_timer(&ps->ppu_timer);
308 ret = 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000309 }
310
311 return ret;
312}
313
314static void mv88e6xxx_ppu_access_put(struct dsa_switch *ds)
315{
Florian Fainellia22adce2014-04-28 11:14:28 -0700316 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000317
Barry Grussling3675c8d2013-01-08 16:05:53 +0000318 /* Schedule a timer to re-enable the PHY polling unit. */
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000319 mod_timer(&ps->ppu_timer, jiffies + msecs_to_jiffies(10));
320 mutex_unlock(&ps->ppu_mutex);
321}
322
323void mv88e6xxx_ppu_state_init(struct dsa_switch *ds)
324{
Florian Fainellia22adce2014-04-28 11:14:28 -0700325 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000326
327 mutex_init(&ps->ppu_mutex);
328 INIT_WORK(&ps->ppu_work, mv88e6xxx_ppu_reenable_work);
329 init_timer(&ps->ppu_timer);
330 ps->ppu_timer.data = (unsigned long)ps;
331 ps->ppu_timer.function = mv88e6xxx_ppu_reenable_timer;
332}
333
334int mv88e6xxx_phy_read_ppu(struct dsa_switch *ds, int addr, int regnum)
335{
336 int ret;
337
338 ret = mv88e6xxx_ppu_access_get(ds);
339 if (ret >= 0) {
Barry Grussling85686582013-01-08 16:05:56 +0000340 ret = mv88e6xxx_reg_read(ds, addr, regnum);
341 mv88e6xxx_ppu_access_put(ds);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000342 }
343
344 return ret;
345}
346
347int mv88e6xxx_phy_write_ppu(struct dsa_switch *ds, int addr,
348 int regnum, u16 val)
349{
350 int ret;
351
352 ret = mv88e6xxx_ppu_access_get(ds);
353 if (ret >= 0) {
Barry Grussling85686582013-01-08 16:05:56 +0000354 ret = mv88e6xxx_reg_write(ds, addr, regnum, val);
355 mv88e6xxx_ppu_access_put(ds);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000356 }
357
358 return ret;
359}
360#endif
361
Andrew Lunn54d792f2015-05-06 01:09:47 +0200362static bool mv88e6xxx_6065_family(struct dsa_switch *ds)
363{
364 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
365
366 switch (ps->id) {
367 case PORT_SWITCH_ID_6031:
368 case PORT_SWITCH_ID_6061:
369 case PORT_SWITCH_ID_6035:
370 case PORT_SWITCH_ID_6065:
371 return true;
372 }
373 return false;
374}
375
376static bool mv88e6xxx_6095_family(struct dsa_switch *ds)
377{
378 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
379
380 switch (ps->id) {
381 case PORT_SWITCH_ID_6092:
382 case PORT_SWITCH_ID_6095:
383 return true;
384 }
385 return false;
386}
387
388static bool mv88e6xxx_6097_family(struct dsa_switch *ds)
389{
390 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
391
392 switch (ps->id) {
393 case PORT_SWITCH_ID_6046:
394 case PORT_SWITCH_ID_6085:
395 case PORT_SWITCH_ID_6096:
396 case PORT_SWITCH_ID_6097:
397 return true;
398 }
399 return false;
400}
401
402static bool mv88e6xxx_6165_family(struct dsa_switch *ds)
403{
404 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
405
406 switch (ps->id) {
407 case PORT_SWITCH_ID_6123:
408 case PORT_SWITCH_ID_6161:
409 case PORT_SWITCH_ID_6165:
410 return true;
411 }
412 return false;
413}
414
415static bool mv88e6xxx_6185_family(struct dsa_switch *ds)
416{
417 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
418
419 switch (ps->id) {
420 case PORT_SWITCH_ID_6121:
421 case PORT_SWITCH_ID_6122:
422 case PORT_SWITCH_ID_6152:
423 case PORT_SWITCH_ID_6155:
424 case PORT_SWITCH_ID_6182:
425 case PORT_SWITCH_ID_6185:
426 case PORT_SWITCH_ID_6108:
427 case PORT_SWITCH_ID_6131:
428 return true;
429 }
430 return false;
431}
432
Guenter Roeckc22995c2015-07-25 09:42:28 -0700433static bool mv88e6xxx_6320_family(struct dsa_switch *ds)
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700434{
435 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
436
437 switch (ps->id) {
438 case PORT_SWITCH_ID_6320:
439 case PORT_SWITCH_ID_6321:
440 return true;
441 }
442 return false;
443}
444
Andrew Lunn54d792f2015-05-06 01:09:47 +0200445static bool mv88e6xxx_6351_family(struct dsa_switch *ds)
446{
447 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
448
449 switch (ps->id) {
450 case PORT_SWITCH_ID_6171:
451 case PORT_SWITCH_ID_6175:
452 case PORT_SWITCH_ID_6350:
453 case PORT_SWITCH_ID_6351:
454 return true;
455 }
456 return false;
457}
458
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200459static bool mv88e6xxx_6352_family(struct dsa_switch *ds)
460{
461 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
462
463 switch (ps->id) {
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200464 case PORT_SWITCH_ID_6172:
465 case PORT_SWITCH_ID_6176:
Andrew Lunn54d792f2015-05-06 01:09:47 +0200466 case PORT_SWITCH_ID_6240:
467 case PORT_SWITCH_ID_6352:
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200468 return true;
469 }
470 return false;
471}
472
Andrew Lunndea87022015-08-31 15:56:47 +0200473/* We expect the switch to perform auto negotiation if there is a real
474 * phy. However, in the case of a fixed link phy, we force the port
475 * settings from the fixed link settings.
476 */
477void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port,
478 struct phy_device *phydev)
479{
480 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunn49052872015-09-29 01:53:48 +0200481 u32 reg;
482 int ret;
Andrew Lunndea87022015-08-31 15:56:47 +0200483
484 if (!phy_is_pseudo_fixed_link(phydev))
485 return;
486
487 mutex_lock(&ps->smi_mutex);
488
489 ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_PCS_CTRL);
490 if (ret < 0)
491 goto out;
492
493 reg = ret & ~(PORT_PCS_CTRL_LINK_UP |
494 PORT_PCS_CTRL_FORCE_LINK |
495 PORT_PCS_CTRL_DUPLEX_FULL |
496 PORT_PCS_CTRL_FORCE_DUPLEX |
497 PORT_PCS_CTRL_UNFORCED);
498
499 reg |= PORT_PCS_CTRL_FORCE_LINK;
500 if (phydev->link)
501 reg |= PORT_PCS_CTRL_LINK_UP;
502
503 if (mv88e6xxx_6065_family(ds) && phydev->speed > SPEED_100)
504 goto out;
505
506 switch (phydev->speed) {
507 case SPEED_1000:
508 reg |= PORT_PCS_CTRL_1000;
509 break;
510 case SPEED_100:
511 reg |= PORT_PCS_CTRL_100;
512 break;
513 case SPEED_10:
514 reg |= PORT_PCS_CTRL_10;
515 break;
516 default:
517 pr_info("Unknown speed");
518 goto out;
519 }
520
521 reg |= PORT_PCS_CTRL_FORCE_DUPLEX;
522 if (phydev->duplex == DUPLEX_FULL)
523 reg |= PORT_PCS_CTRL_DUPLEX_FULL;
524
Andrew Lunne7e72ac2015-08-31 15:56:51 +0200525 if ((mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds)) &&
526 (port >= ps->num_ports - 2)) {
527 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
528 reg |= PORT_PCS_CTRL_RGMII_DELAY_RXCLK;
529 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
530 reg |= PORT_PCS_CTRL_RGMII_DELAY_TXCLK;
531 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
532 reg |= (PORT_PCS_CTRL_RGMII_DELAY_RXCLK |
533 PORT_PCS_CTRL_RGMII_DELAY_TXCLK);
534 }
Andrew Lunndea87022015-08-31 15:56:47 +0200535 _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_PCS_CTRL, reg);
536
537out:
538 mutex_unlock(&ps->smi_mutex);
539}
540
Andrew Lunn31888232015-05-06 01:09:54 +0200541/* Must be called with SMI mutex held */
542static int _mv88e6xxx_stats_wait(struct dsa_switch *ds)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000543{
544 int ret;
545 int i;
546
547 for (i = 0; i < 10; i++) {
Andrew Lunn31888232015-05-06 01:09:54 +0200548 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATS_OP);
Andrew Lunncca8b132015-04-02 04:06:39 +0200549 if ((ret & GLOBAL_STATS_OP_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000550 return 0;
551 }
552
553 return -ETIMEDOUT;
554}
555
Andrew Lunn31888232015-05-06 01:09:54 +0200556/* Must be called with SMI mutex held */
557static int _mv88e6xxx_stats_snapshot(struct dsa_switch *ds, int port)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000558{
559 int ret;
560
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700561 if (mv88e6xxx_6320_family(ds) || mv88e6xxx_6352_family(ds))
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200562 port = (port + 1) << 5;
563
Barry Grussling3675c8d2013-01-08 16:05:53 +0000564 /* Snapshot the hardware statistics counters for this port. */
Andrew Lunn31888232015-05-06 01:09:54 +0200565 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_STATS_OP,
566 GLOBAL_STATS_OP_CAPTURE_PORT |
567 GLOBAL_STATS_OP_HIST_RX_TX | port);
568 if (ret < 0)
569 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000570
Barry Grussling3675c8d2013-01-08 16:05:53 +0000571 /* Wait for the snapshotting to complete. */
Andrew Lunn31888232015-05-06 01:09:54 +0200572 ret = _mv88e6xxx_stats_wait(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000573 if (ret < 0)
574 return ret;
575
576 return 0;
577}
578
Andrew Lunn31888232015-05-06 01:09:54 +0200579/* Must be called with SMI mutex held */
580static void _mv88e6xxx_stats_read(struct dsa_switch *ds, int stat, u32 *val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000581{
582 u32 _val;
583 int ret;
584
585 *val = 0;
586
Andrew Lunn31888232015-05-06 01:09:54 +0200587 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_STATS_OP,
588 GLOBAL_STATS_OP_READ_CAPTURED |
589 GLOBAL_STATS_OP_HIST_RX_TX | stat);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000590 if (ret < 0)
591 return;
592
Andrew Lunn31888232015-05-06 01:09:54 +0200593 ret = _mv88e6xxx_stats_wait(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000594 if (ret < 0)
595 return;
596
Andrew Lunn31888232015-05-06 01:09:54 +0200597 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATS_COUNTER_32);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000598 if (ret < 0)
599 return;
600
601 _val = ret << 16;
602
Andrew Lunn31888232015-05-06 01:09:54 +0200603 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATS_COUNTER_01);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000604 if (ret < 0)
605 return;
606
607 *val = _val | ret;
608}
609
Andrew Lunne413e7e2015-04-02 04:06:38 +0200610static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
611 { "in_good_octets", 8, 0x00, },
612 { "in_bad_octets", 4, 0x02, },
613 { "in_unicast", 4, 0x04, },
614 { "in_broadcasts", 4, 0x06, },
615 { "in_multicasts", 4, 0x07, },
616 { "in_pause", 4, 0x16, },
617 { "in_undersize", 4, 0x18, },
618 { "in_fragments", 4, 0x19, },
619 { "in_oversize", 4, 0x1a, },
620 { "in_jabber", 4, 0x1b, },
621 { "in_rx_error", 4, 0x1c, },
622 { "in_fcs_error", 4, 0x1d, },
623 { "out_octets", 8, 0x0e, },
624 { "out_unicast", 4, 0x10, },
625 { "out_broadcasts", 4, 0x13, },
626 { "out_multicasts", 4, 0x12, },
627 { "out_pause", 4, 0x15, },
628 { "excessive", 4, 0x11, },
629 { "collisions", 4, 0x1e, },
630 { "deferred", 4, 0x05, },
631 { "single", 4, 0x14, },
632 { "multiple", 4, 0x17, },
633 { "out_fcs_error", 4, 0x03, },
634 { "late", 4, 0x1f, },
635 { "hist_64bytes", 4, 0x08, },
636 { "hist_65_127bytes", 4, 0x09, },
637 { "hist_128_255bytes", 4, 0x0a, },
638 { "hist_256_511bytes", 4, 0x0b, },
639 { "hist_512_1023bytes", 4, 0x0c, },
640 { "hist_1024_max_bytes", 4, 0x0d, },
641 /* Not all devices have the following counters */
642 { "sw_in_discards", 4, 0x110, },
643 { "sw_in_filtered", 2, 0x112, },
644 { "sw_out_filtered", 2, 0x113, },
645
646};
647
648static bool have_sw_in_discards(struct dsa_switch *ds)
649{
650 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
651
652 switch (ps->id) {
Andrew Lunncca8b132015-04-02 04:06:39 +0200653 case PORT_SWITCH_ID_6095: case PORT_SWITCH_ID_6161:
654 case PORT_SWITCH_ID_6165: case PORT_SWITCH_ID_6171:
655 case PORT_SWITCH_ID_6172: case PORT_SWITCH_ID_6176:
656 case PORT_SWITCH_ID_6182: case PORT_SWITCH_ID_6185:
657 case PORT_SWITCH_ID_6352:
Andrew Lunne413e7e2015-04-02 04:06:38 +0200658 return true;
659 default:
660 return false;
661 }
662}
663
664static void _mv88e6xxx_get_strings(struct dsa_switch *ds,
665 int nr_stats,
666 struct mv88e6xxx_hw_stat *stats,
667 int port, uint8_t *data)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000668{
669 int i;
670
671 for (i = 0; i < nr_stats; i++) {
672 memcpy(data + i * ETH_GSTRING_LEN,
673 stats[i].string, ETH_GSTRING_LEN);
674 }
675}
676
Andrew Lunn80c46272015-06-20 18:42:30 +0200677static uint64_t _mv88e6xxx_get_ethtool_stat(struct dsa_switch *ds,
678 int stat,
679 struct mv88e6xxx_hw_stat *stats,
680 int port)
681{
682 struct mv88e6xxx_hw_stat *s = stats + stat;
683 u32 low;
684 u32 high = 0;
685 int ret;
686 u64 value;
687
688 if (s->reg >= 0x100) {
689 ret = _mv88e6xxx_reg_read(ds, REG_PORT(port),
690 s->reg - 0x100);
691 if (ret < 0)
692 return UINT64_MAX;
693
694 low = ret;
695 if (s->sizeof_stat == 4) {
696 ret = _mv88e6xxx_reg_read(ds, REG_PORT(port),
697 s->reg - 0x100 + 1);
698 if (ret < 0)
699 return UINT64_MAX;
700 high = ret;
701 }
702 } else {
703 _mv88e6xxx_stats_read(ds, s->reg, &low);
704 if (s->sizeof_stat == 8)
705 _mv88e6xxx_stats_read(ds, s->reg + 1, &high);
706 }
707 value = (((u64)high) << 16) | low;
708 return value;
709}
710
Andrew Lunne413e7e2015-04-02 04:06:38 +0200711static void _mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds,
712 int nr_stats,
713 struct mv88e6xxx_hw_stat *stats,
714 int port, uint64_t *data)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000715{
Florian Fainellia22adce2014-04-28 11:14:28 -0700716 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000717 int ret;
718 int i;
719
Andrew Lunn31888232015-05-06 01:09:54 +0200720 mutex_lock(&ps->smi_mutex);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000721
Andrew Lunn31888232015-05-06 01:09:54 +0200722 ret = _mv88e6xxx_stats_snapshot(ds, port);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000723 if (ret < 0) {
Andrew Lunn31888232015-05-06 01:09:54 +0200724 mutex_unlock(&ps->smi_mutex);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000725 return;
726 }
727
Barry Grussling3675c8d2013-01-08 16:05:53 +0000728 /* Read each of the counters. */
Andrew Lunn80c46272015-06-20 18:42:30 +0200729 for (i = 0; i < nr_stats; i++)
730 data[i] = _mv88e6xxx_get_ethtool_stat(ds, i, stats, port);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000731
Andrew Lunn31888232015-05-06 01:09:54 +0200732 mutex_unlock(&ps->smi_mutex);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000733}
Ben Hutchings98e67302011-11-25 14:36:19 +0000734
Andrew Lunne413e7e2015-04-02 04:06:38 +0200735/* All the statistics in the table */
736void
737mv88e6xxx_get_strings(struct dsa_switch *ds, int port, uint8_t *data)
738{
739 if (have_sw_in_discards(ds))
740 _mv88e6xxx_get_strings(ds, ARRAY_SIZE(mv88e6xxx_hw_stats),
741 mv88e6xxx_hw_stats, port, data);
742 else
743 _mv88e6xxx_get_strings(ds, ARRAY_SIZE(mv88e6xxx_hw_stats) - 3,
744 mv88e6xxx_hw_stats, port, data);
745}
746
747int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
748{
749 if (have_sw_in_discards(ds))
750 return ARRAY_SIZE(mv88e6xxx_hw_stats);
751 return ARRAY_SIZE(mv88e6xxx_hw_stats) - 3;
752}
753
754void
755mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds,
756 int port, uint64_t *data)
757{
758 if (have_sw_in_discards(ds))
759 _mv88e6xxx_get_ethtool_stats(
760 ds, ARRAY_SIZE(mv88e6xxx_hw_stats),
761 mv88e6xxx_hw_stats, port, data);
762 else
763 _mv88e6xxx_get_ethtool_stats(
764 ds, ARRAY_SIZE(mv88e6xxx_hw_stats) - 3,
765 mv88e6xxx_hw_stats, port, data);
766}
767
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700768int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
769{
770 return 32 * sizeof(u16);
771}
772
773void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
774 struct ethtool_regs *regs, void *_p)
775{
776 u16 *p = _p;
777 int i;
778
779 regs->version = 0;
780
781 memset(p, 0xff, 32 * sizeof(u16));
782
783 for (i = 0; i < 32; i++) {
784 int ret;
785
786 ret = mv88e6xxx_reg_read(ds, REG_PORT(port), i);
787 if (ret >= 0)
788 p[i] = ret;
789 }
790}
791
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700792/* Must be called with SMI lock held */
Andrew Lunn3898c142015-05-06 01:09:53 +0200793static int _mv88e6xxx_wait(struct dsa_switch *ds, int reg, int offset,
794 u16 mask)
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700795{
796 unsigned long timeout = jiffies + HZ / 10;
797
798 while (time_before(jiffies, timeout)) {
799 int ret;
800
801 ret = _mv88e6xxx_reg_read(ds, reg, offset);
802 if (ret < 0)
803 return ret;
804 if (!(ret & mask))
805 return 0;
806
807 usleep_range(1000, 2000);
808 }
809 return -ETIMEDOUT;
810}
811
Andrew Lunn3898c142015-05-06 01:09:53 +0200812static int mv88e6xxx_wait(struct dsa_switch *ds, int reg, int offset, u16 mask)
813{
814 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
815 int ret;
816
817 mutex_lock(&ps->smi_mutex);
818 ret = _mv88e6xxx_wait(ds, reg, offset, mask);
819 mutex_unlock(&ps->smi_mutex);
820
821 return ret;
822}
823
824static int _mv88e6xxx_phy_wait(struct dsa_switch *ds)
825{
826 return _mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_SMI_OP,
827 GLOBAL2_SMI_OP_BUSY);
828}
829
830int mv88e6xxx_eeprom_load_wait(struct dsa_switch *ds)
831{
832 return mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
833 GLOBAL2_EEPROM_OP_LOAD);
834}
835
836int mv88e6xxx_eeprom_busy_wait(struct dsa_switch *ds)
837{
838 return mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
839 GLOBAL2_EEPROM_OP_BUSY);
840}
841
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700842/* Must be called with SMI lock held */
843static int _mv88e6xxx_atu_wait(struct dsa_switch *ds)
844{
Andrew Lunncca8b132015-04-02 04:06:39 +0200845 return _mv88e6xxx_wait(ds, REG_GLOBAL, GLOBAL_ATU_OP,
846 GLOBAL_ATU_OP_BUSY);
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700847}
848
Andrew Lunn3898c142015-05-06 01:09:53 +0200849/* Must be called with SMI mutex held */
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +0200850static int _mv88e6xxx_phy_read_indirect(struct dsa_switch *ds, int addr,
851 int regnum)
Andrew Lunnf3044682015-02-14 19:17:50 +0100852{
853 int ret;
854
Andrew Lunn3898c142015-05-06 01:09:53 +0200855 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_SMI_OP,
856 GLOBAL2_SMI_OP_22_READ | (addr << 5) |
857 regnum);
Andrew Lunnf3044682015-02-14 19:17:50 +0100858 if (ret < 0)
859 return ret;
860
Andrew Lunn3898c142015-05-06 01:09:53 +0200861 ret = _mv88e6xxx_phy_wait(ds);
862 if (ret < 0)
863 return ret;
864
865 return _mv88e6xxx_reg_read(ds, REG_GLOBAL2, GLOBAL2_SMI_DATA);
Andrew Lunnf3044682015-02-14 19:17:50 +0100866}
867
Andrew Lunn3898c142015-05-06 01:09:53 +0200868/* Must be called with SMI mutex held */
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +0200869static int _mv88e6xxx_phy_write_indirect(struct dsa_switch *ds, int addr,
870 int regnum, u16 val)
Andrew Lunnf3044682015-02-14 19:17:50 +0100871{
Andrew Lunn3898c142015-05-06 01:09:53 +0200872 int ret;
Andrew Lunnf3044682015-02-14 19:17:50 +0100873
Andrew Lunn3898c142015-05-06 01:09:53 +0200874 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_SMI_DATA, val);
875 if (ret < 0)
876 return ret;
877
878 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_SMI_OP,
879 GLOBAL2_SMI_OP_22_WRITE | (addr << 5) |
880 regnum);
881
882 return _mv88e6xxx_phy_wait(ds);
Andrew Lunnf3044682015-02-14 19:17:50 +0100883}
884
Guenter Roeck11b3b452015-03-06 22:23:51 -0800885int mv88e6xxx_get_eee(struct dsa_switch *ds, int port, struct ethtool_eee *e)
886{
Andrew Lunn2f40c692015-04-02 04:06:37 +0200887 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Guenter Roeck11b3b452015-03-06 22:23:51 -0800888 int reg;
889
Andrew Lunn3898c142015-05-06 01:09:53 +0200890 mutex_lock(&ps->smi_mutex);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200891
892 reg = _mv88e6xxx_phy_read_indirect(ds, port, 16);
Guenter Roeck11b3b452015-03-06 22:23:51 -0800893 if (reg < 0)
Andrew Lunn2f40c692015-04-02 04:06:37 +0200894 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800895
896 e->eee_enabled = !!(reg & 0x0200);
897 e->tx_lpi_enabled = !!(reg & 0x0100);
898
Andrew Lunn3898c142015-05-06 01:09:53 +0200899 reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_STATUS);
Guenter Roeck11b3b452015-03-06 22:23:51 -0800900 if (reg < 0)
Andrew Lunn2f40c692015-04-02 04:06:37 +0200901 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800902
Andrew Lunncca8b132015-04-02 04:06:39 +0200903 e->eee_active = !!(reg & PORT_STATUS_EEE);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200904 reg = 0;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800905
Andrew Lunn2f40c692015-04-02 04:06:37 +0200906out:
Andrew Lunn3898c142015-05-06 01:09:53 +0200907 mutex_unlock(&ps->smi_mutex);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200908 return reg;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800909}
910
911int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
912 struct phy_device *phydev, struct ethtool_eee *e)
913{
Andrew Lunn2f40c692015-04-02 04:06:37 +0200914 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
915 int reg;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800916 int ret;
917
Andrew Lunn3898c142015-05-06 01:09:53 +0200918 mutex_lock(&ps->smi_mutex);
Guenter Roeck11b3b452015-03-06 22:23:51 -0800919
Andrew Lunn2f40c692015-04-02 04:06:37 +0200920 ret = _mv88e6xxx_phy_read_indirect(ds, port, 16);
921 if (ret < 0)
922 goto out;
923
924 reg = ret & ~0x0300;
925 if (e->eee_enabled)
926 reg |= 0x0200;
927 if (e->tx_lpi_enabled)
928 reg |= 0x0100;
929
930 ret = _mv88e6xxx_phy_write_indirect(ds, port, 16, reg);
931out:
Andrew Lunn3898c142015-05-06 01:09:53 +0200932 mutex_unlock(&ps->smi_mutex);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200933
934 return ret;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800935}
936
Vivien Didelot70cc99d2015-09-04 14:34:10 -0400937static int _mv88e6xxx_atu_cmd(struct dsa_switch *ds, u16 cmd)
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700938{
939 int ret;
940
Andrew Lunncca8b132015-04-02 04:06:39 +0200941 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_OP, cmd);
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700942 if (ret < 0)
943 return ret;
944
945 return _mv88e6xxx_atu_wait(ds);
946}
947
Vivien Didelot37705b72015-09-04 14:34:11 -0400948static int _mv88e6xxx_atu_data_write(struct dsa_switch *ds,
949 struct mv88e6xxx_atu_entry *entry)
950{
951 u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK;
952
953 if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) {
954 unsigned int mask, shift;
955
956 if (entry->trunk) {
957 data |= GLOBAL_ATU_DATA_TRUNK;
958 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
959 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
960 } else {
961 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
962 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
963 }
964
965 data |= (entry->portv_trunkid << shift) & mask;
966 }
967
968 return _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_DATA, data);
969}
970
Vivien Didelot7fb5e752015-09-04 14:34:12 -0400971static int _mv88e6xxx_atu_flush_move(struct dsa_switch *ds,
972 struct mv88e6xxx_atu_entry *entry,
973 bool static_too)
974{
975 int op;
976 int err;
977
978 err = _mv88e6xxx_atu_wait(ds);
979 if (err)
980 return err;
981
982 err = _mv88e6xxx_atu_data_write(ds, entry);
983 if (err)
984 return err;
985
986 if (entry->fid) {
987 err = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_FID,
988 entry->fid);
989 if (err)
990 return err;
991
992 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB :
993 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB;
994 } else {
995 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL :
996 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC;
997 }
998
999 return _mv88e6xxx_atu_cmd(ds, op);
1000}
1001
1002static int _mv88e6xxx_atu_flush(struct dsa_switch *ds, u16 fid, bool static_too)
1003{
1004 struct mv88e6xxx_atu_entry entry = {
1005 .fid = fid,
1006 .state = 0, /* EntryState bits must be 0 */
1007 };
1008
1009 return _mv88e6xxx_atu_flush_move(ds, &entry, static_too);
1010}
1011
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001012static int _mv88e6xxx_atu_move(struct dsa_switch *ds, u16 fid, int from_port,
1013 int to_port, bool static_too)
1014{
1015 struct mv88e6xxx_atu_entry entry = {
1016 .trunk = false,
1017 .fid = fid,
1018 };
1019
1020 /* EntryState bits must be 0xF */
1021 entry.state = GLOBAL_ATU_DATA_STATE_MASK;
1022
1023 /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */
1024 entry.portv_trunkid = (to_port & 0x0f) << 4;
1025 entry.portv_trunkid |= from_port & 0x0f;
1026
1027 return _mv88e6xxx_atu_flush_move(ds, &entry, static_too);
1028}
1029
1030static int _mv88e6xxx_atu_remove(struct dsa_switch *ds, u16 fid, int port,
1031 bool static_too)
1032{
1033 /* Destination port 0xF means remove the entries */
1034 return _mv88e6xxx_atu_move(ds, fid, port, 0x0f, static_too);
1035}
1036
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001037static int mv88e6xxx_set_port_state(struct dsa_switch *ds, int port, u8 state)
1038{
1039 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Geert Uytterhoevenc3ffe6d2015-04-16 20:49:14 +02001040 int reg, ret = 0;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001041 u8 oldstate;
1042
1043 mutex_lock(&ps->smi_mutex);
1044
Andrew Lunncca8b132015-04-02 04:06:39 +02001045 reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_CONTROL);
Guenter Roeck538cc282015-04-15 22:12:42 -07001046 if (reg < 0) {
1047 ret = reg;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001048 goto abort;
Guenter Roeck538cc282015-04-15 22:12:42 -07001049 }
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001050
Andrew Lunncca8b132015-04-02 04:06:39 +02001051 oldstate = reg & PORT_CONTROL_STATE_MASK;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001052 if (oldstate != state) {
1053 /* Flush forwarding database if we're moving a port
1054 * from Learning or Forwarding state to Disabled or
1055 * Blocking or Listening state.
1056 */
Andrew Lunncca8b132015-04-02 04:06:39 +02001057 if (oldstate >= PORT_CONTROL_STATE_LEARNING &&
1058 state <= PORT_CONTROL_STATE_BLOCKING) {
Vivien Didelot2b8157b2015-09-04 14:34:16 -04001059 ret = _mv88e6xxx_atu_remove(ds, 0, port, false);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001060 if (ret)
1061 goto abort;
1062 }
Andrew Lunncca8b132015-04-02 04:06:39 +02001063 reg = (reg & ~PORT_CONTROL_STATE_MASK) | state;
1064 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_CONTROL,
1065 reg);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001066 }
1067
1068abort:
1069 mutex_unlock(&ps->smi_mutex);
1070 return ret;
1071}
1072
Vivien Didelotede80982015-10-11 18:08:35 -04001073static int _mv88e6xxx_port_vlan_map_set(struct dsa_switch *ds, int port,
1074 u16 output_ports)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001075{
1076 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelotede80982015-10-11 18:08:35 -04001077 const u16 mask = (1 << ps->num_ports) - 1;
1078 int reg;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001079
Vivien Didelotede80982015-10-11 18:08:35 -04001080 reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_BASE_VLAN);
1081 if (reg < 0)
1082 return reg;
1083
1084 reg &= ~mask;
1085 reg |= output_ports & mask;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001086
Andrew Lunncca8b132015-04-02 04:06:39 +02001087 return _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_BASE_VLAN, reg);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001088}
1089
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001090int mv88e6xxx_port_stp_update(struct dsa_switch *ds, int port, u8 state)
1091{
1092 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1093 int stp_state;
1094
1095 switch (state) {
1096 case BR_STATE_DISABLED:
Andrew Lunncca8b132015-04-02 04:06:39 +02001097 stp_state = PORT_CONTROL_STATE_DISABLED;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001098 break;
1099 case BR_STATE_BLOCKING:
1100 case BR_STATE_LISTENING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001101 stp_state = PORT_CONTROL_STATE_BLOCKING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001102 break;
1103 case BR_STATE_LEARNING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001104 stp_state = PORT_CONTROL_STATE_LEARNING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001105 break;
1106 case BR_STATE_FORWARDING:
1107 default:
Andrew Lunncca8b132015-04-02 04:06:39 +02001108 stp_state = PORT_CONTROL_STATE_FORWARDING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001109 break;
1110 }
1111
1112 netdev_dbg(ds->ports[port], "port state %d [%d]\n", state, stp_state);
1113
1114 /* mv88e6xxx_port_stp_update may be called with softirqs disabled,
1115 * so we can not update the port state directly but need to schedule it.
1116 */
1117 ps->port_state[port] = stp_state;
1118 set_bit(port, &ps->port_state_update_mask);
1119 schedule_work(&ps->bridge_work);
1120
1121 return 0;
1122}
1123
Vivien Didelot76e398a2015-11-01 12:33:55 -05001124static int _mv88e6xxx_port_pvid_get(struct dsa_switch *ds, int port, u16 *pvid)
1125{
1126 int ret;
1127
1128 ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_DEFAULT_VLAN);
1129 if (ret < 0)
1130 return ret;
1131
1132 *pvid = ret & PORT_DEFAULT_VLAN_MASK;
1133
1134 return 0;
1135}
1136
Vivien Didelotb8fee952015-08-13 12:52:19 -04001137int mv88e6xxx_port_pvid_get(struct dsa_switch *ds, int port, u16 *pvid)
1138{
1139 int ret;
1140
1141 ret = mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_DEFAULT_VLAN);
1142 if (ret < 0)
1143 return ret;
1144
1145 *pvid = ret & PORT_DEFAULT_VLAN_MASK;
1146
1147 return 0;
1148}
1149
Vivien Didelot76e398a2015-11-01 12:33:55 -05001150static int _mv88e6xxx_port_pvid_set(struct dsa_switch *ds, int port, u16 pvid)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001151{
Vivien Didelot76e398a2015-11-01 12:33:55 -05001152 return _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_DEFAULT_VLAN,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001153 pvid & PORT_DEFAULT_VLAN_MASK);
1154}
1155
Vivien Didelot6b17e862015-08-13 12:52:18 -04001156static int _mv88e6xxx_vtu_wait(struct dsa_switch *ds)
1157{
1158 return _mv88e6xxx_wait(ds, REG_GLOBAL, GLOBAL_VTU_OP,
1159 GLOBAL_VTU_OP_BUSY);
1160}
1161
1162static int _mv88e6xxx_vtu_cmd(struct dsa_switch *ds, u16 op)
1163{
1164 int ret;
1165
1166 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_OP, op);
1167 if (ret < 0)
1168 return ret;
1169
1170 return _mv88e6xxx_vtu_wait(ds);
1171}
1172
1173static int _mv88e6xxx_vtu_stu_flush(struct dsa_switch *ds)
1174{
1175 int ret;
1176
1177 ret = _mv88e6xxx_vtu_wait(ds);
1178 if (ret < 0)
1179 return ret;
1180
1181 return _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_FLUSH_ALL);
1182}
1183
Vivien Didelotb8fee952015-08-13 12:52:19 -04001184static int _mv88e6xxx_vtu_stu_data_read(struct dsa_switch *ds,
1185 struct mv88e6xxx_vtu_stu_entry *entry,
1186 unsigned int nibble_offset)
1187{
1188 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1189 u16 regs[3];
1190 int i;
1191 int ret;
1192
1193 for (i = 0; i < 3; ++i) {
1194 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL,
1195 GLOBAL_VTU_DATA_0_3 + i);
1196 if (ret < 0)
1197 return ret;
1198
1199 regs[i] = ret;
1200 }
1201
1202 for (i = 0; i < ps->num_ports; ++i) {
1203 unsigned int shift = (i % 4) * 4 + nibble_offset;
1204 u16 reg = regs[i / 4];
1205
1206 entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
1207 }
1208
1209 return 0;
1210}
1211
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001212static int _mv88e6xxx_vtu_stu_data_write(struct dsa_switch *ds,
1213 struct mv88e6xxx_vtu_stu_entry *entry,
1214 unsigned int nibble_offset)
1215{
1216 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1217 u16 regs[3] = { 0 };
1218 int i;
1219 int ret;
1220
1221 for (i = 0; i < ps->num_ports; ++i) {
1222 unsigned int shift = (i % 4) * 4 + nibble_offset;
1223 u8 data = entry->data[i];
1224
1225 regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
1226 }
1227
1228 for (i = 0; i < 3; ++i) {
1229 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL,
1230 GLOBAL_VTU_DATA_0_3 + i, regs[i]);
1231 if (ret < 0)
1232 return ret;
1233 }
1234
1235 return 0;
1236}
1237
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001238static int _mv88e6xxx_vtu_vid_write(struct dsa_switch *ds, u16 vid)
1239{
1240 return _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_VID,
1241 vid & GLOBAL_VTU_VID_MASK);
1242}
1243
1244static int _mv88e6xxx_vtu_getnext(struct dsa_switch *ds,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001245 struct mv88e6xxx_vtu_stu_entry *entry)
1246{
1247 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1248 int ret;
1249
1250 ret = _mv88e6xxx_vtu_wait(ds);
1251 if (ret < 0)
1252 return ret;
1253
Vivien Didelotb8fee952015-08-13 12:52:19 -04001254 ret = _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_VTU_GET_NEXT);
1255 if (ret < 0)
1256 return ret;
1257
1258 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_VTU_VID);
1259 if (ret < 0)
1260 return ret;
1261
1262 next.vid = ret & GLOBAL_VTU_VID_MASK;
1263 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1264
1265 if (next.valid) {
1266 ret = _mv88e6xxx_vtu_stu_data_read(ds, &next, 0);
1267 if (ret < 0)
1268 return ret;
1269
1270 if (mv88e6xxx_6097_family(ds) || mv88e6xxx_6165_family(ds) ||
1271 mv88e6xxx_6351_family(ds) || mv88e6xxx_6352_family(ds)) {
1272 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL,
1273 GLOBAL_VTU_FID);
1274 if (ret < 0)
1275 return ret;
1276
1277 next.fid = ret & GLOBAL_VTU_FID_MASK;
1278
1279 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL,
1280 GLOBAL_VTU_SID);
1281 if (ret < 0)
1282 return ret;
1283
1284 next.sid = ret & GLOBAL_VTU_SID_MASK;
1285 }
1286 }
1287
1288 *entry = next;
1289 return 0;
1290}
1291
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001292static int _mv88e6xxx_vtu_loadpurge(struct dsa_switch *ds,
1293 struct mv88e6xxx_vtu_stu_entry *entry)
1294{
1295 u16 reg = 0;
1296 int ret;
1297
1298 ret = _mv88e6xxx_vtu_wait(ds);
1299 if (ret < 0)
1300 return ret;
1301
1302 if (!entry->valid)
1303 goto loadpurge;
1304
1305 /* Write port member tags */
1306 ret = _mv88e6xxx_vtu_stu_data_write(ds, entry, 0);
1307 if (ret < 0)
1308 return ret;
1309
1310 if (mv88e6xxx_6097_family(ds) || mv88e6xxx_6165_family(ds) ||
1311 mv88e6xxx_6351_family(ds) || mv88e6xxx_6352_family(ds)) {
1312 reg = entry->sid & GLOBAL_VTU_SID_MASK;
1313 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_SID, reg);
1314 if (ret < 0)
1315 return ret;
1316
1317 reg = entry->fid & GLOBAL_VTU_FID_MASK;
1318 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_FID, reg);
1319 if (ret < 0)
1320 return ret;
1321 }
1322
1323 reg = GLOBAL_VTU_VID_VALID;
1324loadpurge:
1325 reg |= entry->vid & GLOBAL_VTU_VID_MASK;
1326 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_VID, reg);
1327 if (ret < 0)
1328 return ret;
1329
1330 return _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_VTU_LOAD_PURGE);
1331}
1332
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001333static int _mv88e6xxx_stu_getnext(struct dsa_switch *ds, u8 sid,
1334 struct mv88e6xxx_vtu_stu_entry *entry)
1335{
1336 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1337 int ret;
1338
1339 ret = _mv88e6xxx_vtu_wait(ds);
1340 if (ret < 0)
1341 return ret;
1342
1343 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_SID,
1344 sid & GLOBAL_VTU_SID_MASK);
1345 if (ret < 0)
1346 return ret;
1347
1348 ret = _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_STU_GET_NEXT);
1349 if (ret < 0)
1350 return ret;
1351
1352 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_VTU_SID);
1353 if (ret < 0)
1354 return ret;
1355
1356 next.sid = ret & GLOBAL_VTU_SID_MASK;
1357
1358 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_VTU_VID);
1359 if (ret < 0)
1360 return ret;
1361
1362 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1363
1364 if (next.valid) {
1365 ret = _mv88e6xxx_vtu_stu_data_read(ds, &next, 2);
1366 if (ret < 0)
1367 return ret;
1368 }
1369
1370 *entry = next;
1371 return 0;
1372}
1373
1374static int _mv88e6xxx_stu_loadpurge(struct dsa_switch *ds,
1375 struct mv88e6xxx_vtu_stu_entry *entry)
1376{
1377 u16 reg = 0;
1378 int ret;
1379
1380 ret = _mv88e6xxx_vtu_wait(ds);
1381 if (ret < 0)
1382 return ret;
1383
1384 if (!entry->valid)
1385 goto loadpurge;
1386
1387 /* Write port states */
1388 ret = _mv88e6xxx_vtu_stu_data_write(ds, entry, 2);
1389 if (ret < 0)
1390 return ret;
1391
1392 reg = GLOBAL_VTU_VID_VALID;
1393loadpurge:
1394 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_VID, reg);
1395 if (ret < 0)
1396 return ret;
1397
1398 reg = entry->sid & GLOBAL_VTU_SID_MASK;
1399 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_SID, reg);
1400 if (ret < 0)
1401 return ret;
1402
1403 return _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_STU_LOAD_PURGE);
1404}
1405
1406static int _mv88e6xxx_vlan_init(struct dsa_switch *ds, u16 vid,
1407 struct mv88e6xxx_vtu_stu_entry *entry)
1408{
1409 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1410 struct mv88e6xxx_vtu_stu_entry vlan = {
1411 .valid = true,
1412 .vid = vid,
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001413 .fid = vid, /* We use one FID per VLAN */
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001414 };
1415 int i;
1416
1417 /* exclude all ports except the CPU */
1418 for (i = 0; i < ps->num_ports; ++i)
1419 vlan.data[i] = dsa_is_cpu_port(ds, i) ?
1420 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED :
1421 GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
1422
1423 if (mv88e6xxx_6097_family(ds) || mv88e6xxx_6165_family(ds) ||
1424 mv88e6xxx_6351_family(ds) || mv88e6xxx_6352_family(ds)) {
1425 struct mv88e6xxx_vtu_stu_entry vstp;
1426 int err;
1427
1428 /* Adding a VTU entry requires a valid STU entry. As VSTP is not
1429 * implemented, only one STU entry is needed to cover all VTU
1430 * entries. Thus, validate the SID 0.
1431 */
1432 vlan.sid = 0;
1433 err = _mv88e6xxx_stu_getnext(ds, GLOBAL_VTU_SID_MASK, &vstp);
1434 if (err)
1435 return err;
1436
1437 if (vstp.sid != vlan.sid || !vstp.valid) {
1438 memset(&vstp, 0, sizeof(vstp));
1439 vstp.valid = true;
1440 vstp.sid = vlan.sid;
1441
1442 err = _mv88e6xxx_stu_loadpurge(ds, &vstp);
1443 if (err)
1444 return err;
1445 }
1446
Vivien Didelot7c400012015-09-04 14:34:14 -04001447 /* Clear all MAC addresses from the new database */
1448 err = _mv88e6xxx_atu_flush(ds, vlan.fid, true);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001449 if (err)
1450 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001451 }
1452
1453 *entry = vlan;
1454 return 0;
1455}
1456
Vivien Didelot76e398a2015-11-01 12:33:55 -05001457int mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
1458 const struct switchdev_obj_port_vlan *vlan,
1459 struct switchdev_trans *trans)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001460{
Vivien Didelot76e398a2015-11-01 12:33:55 -05001461 /* We don't need any dynamic resource from the kernel (yet),
1462 * so skip the prepare phase.
1463 */
1464 return 0;
1465}
1466
1467static int _mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port, u16 vid,
1468 bool untagged)
1469{
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001470 struct mv88e6xxx_vtu_stu_entry vlan;
1471 int err;
1472
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001473 err = _mv88e6xxx_vtu_vid_write(ds, vid - 1);
1474 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001475 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001476
1477 err = _mv88e6xxx_vtu_getnext(ds, &vlan);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001478 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001479 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001480
1481 if (vlan.vid != vid || !vlan.valid) {
1482 err = _mv88e6xxx_vlan_init(ds, vid, &vlan);
1483 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001484 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001485 }
1486
1487 vlan.data[port] = untagged ?
1488 GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
1489 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
1490
Vivien Didelot76e398a2015-11-01 12:33:55 -05001491 return _mv88e6xxx_vtu_loadpurge(ds, &vlan);
1492}
1493
1494int mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
1495 const struct switchdev_obj_port_vlan *vlan,
1496 struct switchdev_trans *trans)
1497{
1498 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1499 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1500 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1501 u16 vid;
1502 int err = 0;
1503
1504 mutex_lock(&ps->smi_mutex);
1505
1506 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
1507 err = _mv88e6xxx_port_vlan_add(ds, port, vid, untagged);
1508 if (err)
1509 goto unlock;
1510 }
1511
1512 /* no PVID with ranges, otherwise it's a bug */
1513 if (pvid)
1514 err = _mv88e6xxx_port_pvid_set(ds, port, vid);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001515unlock:
1516 mutex_unlock(&ps->smi_mutex);
1517
1518 return err;
1519}
1520
Vivien Didelot76e398a2015-11-01 12:33:55 -05001521static int _mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port, u16 vid)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001522{
1523 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1524 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001525 int i, err;
1526
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001527 err = _mv88e6xxx_vtu_vid_write(ds, vid - 1);
1528 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001529 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001530
1531 err = _mv88e6xxx_vtu_getnext(ds, &vlan);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001532 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001533 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001534
1535 if (vlan.vid != vid || !vlan.valid ||
Vivien Didelot76e398a2015-11-01 12:33:55 -05001536 vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1537 return -ENOENT;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001538
1539 vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
1540
1541 /* keep the VLAN unless all ports are excluded */
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001542 vlan.valid = false;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001543 for (i = 0; i < ps->num_ports; ++i) {
1544 if (dsa_is_cpu_port(ds, i))
1545 continue;
1546
1547 if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001548 vlan.valid = true;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001549 break;
1550 }
1551 }
1552
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001553 err = _mv88e6xxx_vtu_loadpurge(ds, &vlan);
1554 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001555 return err;
1556
1557 return _mv88e6xxx_atu_remove(ds, vlan.fid, port, false);
1558}
1559
1560int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
1561 const struct switchdev_obj_port_vlan *vlan)
1562{
1563 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1564 u16 pvid, vid;
1565 int err = 0;
1566
1567 mutex_lock(&ps->smi_mutex);
1568
1569 err = _mv88e6xxx_port_pvid_get(ds, port, &pvid);
1570 if (err)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001571 goto unlock;
1572
Vivien Didelot76e398a2015-11-01 12:33:55 -05001573 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
1574 err = _mv88e6xxx_port_vlan_del(ds, port, vid);
1575 if (err)
1576 goto unlock;
1577
1578 if (vid == pvid) {
1579 err = _mv88e6xxx_port_pvid_set(ds, port, 0);
1580 if (err)
1581 goto unlock;
1582 }
1583 }
1584
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001585unlock:
1586 mutex_unlock(&ps->smi_mutex);
1587
1588 return err;
1589}
1590
Vivien Didelotb8fee952015-08-13 12:52:19 -04001591int mv88e6xxx_vlan_getnext(struct dsa_switch *ds, u16 *vid,
1592 unsigned long *ports, unsigned long *untagged)
1593{
1594 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1595 struct mv88e6xxx_vtu_stu_entry next;
1596 int port;
1597 int err;
1598
1599 if (*vid == 4095)
1600 return -ENOENT;
1601
1602 mutex_lock(&ps->smi_mutex);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001603 err = _mv88e6xxx_vtu_vid_write(ds, *vid);
1604 if (err)
1605 goto unlock;
1606
1607 err = _mv88e6xxx_vtu_getnext(ds, &next);
1608unlock:
Vivien Didelotb8fee952015-08-13 12:52:19 -04001609 mutex_unlock(&ps->smi_mutex);
1610
1611 if (err)
1612 return err;
1613
1614 if (!next.valid)
1615 return -ENOENT;
1616
1617 *vid = next.vid;
1618
1619 for (port = 0; port < ps->num_ports; ++port) {
1620 clear_bit(port, ports);
1621 clear_bit(port, untagged);
1622
1623 if (dsa_is_cpu_port(ds, port))
1624 continue;
1625
1626 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED ||
1627 next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1628 set_bit(port, ports);
1629
1630 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1631 set_bit(port, untagged);
1632 }
1633
1634 return 0;
1635}
1636
Vivien Didelotc5723ac2015-08-10 09:09:48 -04001637static int _mv88e6xxx_atu_mac_write(struct dsa_switch *ds,
1638 const unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001639{
1640 int i, ret;
1641
1642 for (i = 0; i < 3; i++) {
Andrew Lunncca8b132015-04-02 04:06:39 +02001643 ret = _mv88e6xxx_reg_write(
1644 ds, REG_GLOBAL, GLOBAL_ATU_MAC_01 + i,
1645 (addr[i * 2] << 8) | addr[i * 2 + 1]);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001646 if (ret < 0)
1647 return ret;
1648 }
1649
1650 return 0;
1651}
1652
Vivien Didelotc5723ac2015-08-10 09:09:48 -04001653static int _mv88e6xxx_atu_mac_read(struct dsa_switch *ds, unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001654{
1655 int i, ret;
1656
1657 for (i = 0; i < 3; i++) {
Andrew Lunncca8b132015-04-02 04:06:39 +02001658 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL,
1659 GLOBAL_ATU_MAC_01 + i);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001660 if (ret < 0)
1661 return ret;
1662 addr[i * 2] = ret >> 8;
1663 addr[i * 2 + 1] = ret & 0xff;
1664 }
1665
1666 return 0;
1667}
1668
Vivien Didelotfd231c82015-08-10 09:09:50 -04001669static int _mv88e6xxx_atu_load(struct dsa_switch *ds,
1670 struct mv88e6xxx_atu_entry *entry)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001671{
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001672 int ret;
1673
1674 ret = _mv88e6xxx_atu_wait(ds);
1675 if (ret < 0)
1676 return ret;
1677
Vivien Didelotfd231c82015-08-10 09:09:50 -04001678 ret = _mv88e6xxx_atu_mac_write(ds, entry->mac);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001679 if (ret < 0)
1680 return ret;
1681
Vivien Didelot37705b72015-09-04 14:34:11 -04001682 ret = _mv88e6xxx_atu_data_write(ds, entry);
Vivien Didelotfd231c82015-08-10 09:09:50 -04001683 if (ret < 0)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001684 return ret;
1685
Vivien Didelot70cc99d2015-09-04 14:34:10 -04001686 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_FID, entry->fid);
1687 if (ret < 0)
1688 return ret;
1689
1690 return _mv88e6xxx_atu_cmd(ds, GLOBAL_ATU_OP_LOAD_DB);
Vivien Didelotfd231c82015-08-10 09:09:50 -04001691}
David S. Millercdf09692015-08-11 12:00:37 -07001692
Vivien Didelotfd231c82015-08-10 09:09:50 -04001693static int _mv88e6xxx_port_fdb_load(struct dsa_switch *ds, int port,
1694 const unsigned char *addr, u16 vid,
1695 u8 state)
1696{
1697 struct mv88e6xxx_atu_entry entry = { 0 };
Vivien Didelotfd231c82015-08-10 09:09:50 -04001698
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001699 entry.fid = vid; /* We use one FID per VLAN */
Vivien Didelotfd231c82015-08-10 09:09:50 -04001700 entry.state = state;
1701 ether_addr_copy(entry.mac, addr);
1702 if (state != GLOBAL_ATU_DATA_STATE_UNUSED) {
1703 entry.trunk = false;
1704 entry.portv_trunkid = BIT(port);
1705 }
1706
1707 return _mv88e6xxx_atu_load(ds, &entry);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001708}
1709
Vivien Didelot146a3202015-10-08 11:35:12 -04001710int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
1711 const struct switchdev_obj_port_fdb *fdb,
1712 struct switchdev_trans *trans)
1713{
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001714 /* We don't use per-port FDB */
1715 if (fdb->vid == 0)
1716 return -EOPNOTSUPP;
1717
Vivien Didelot146a3202015-10-08 11:35:12 -04001718 /* We don't need any dynamic resource from the kernel (yet),
1719 * so skip the prepare phase.
1720 */
1721 return 0;
1722}
1723
David S. Millercdf09692015-08-11 12:00:37 -07001724int mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
Vivien Didelot1f36faf2015-10-08 11:35:13 -04001725 const struct switchdev_obj_port_fdb *fdb,
1726 struct switchdev_trans *trans)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001727{
Vivien Didelot1f36faf2015-10-08 11:35:13 -04001728 int state = is_multicast_ether_addr(fdb->addr) ?
David S. Millercdf09692015-08-11 12:00:37 -07001729 GLOBAL_ATU_DATA_STATE_MC_STATIC :
1730 GLOBAL_ATU_DATA_STATE_UC_STATIC;
1731 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelot6630e232015-08-06 01:44:07 -04001732 int ret;
1733
David S. Millercdf09692015-08-11 12:00:37 -07001734 mutex_lock(&ps->smi_mutex);
Vivien Didelot1f36faf2015-10-08 11:35:13 -04001735 ret = _mv88e6xxx_port_fdb_load(ds, port, fdb->addr, fdb->vid, state);
David S. Millercdf09692015-08-11 12:00:37 -07001736 mutex_unlock(&ps->smi_mutex);
1737
1738 return ret;
1739}
1740
1741int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
Vivien Didelot8057b3e2015-10-08 11:35:14 -04001742 const struct switchdev_obj_port_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -07001743{
1744 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1745 int ret;
1746
1747 mutex_lock(&ps->smi_mutex);
Vivien Didelot8057b3e2015-10-08 11:35:14 -04001748 ret = _mv88e6xxx_port_fdb_load(ds, port, fdb->addr, fdb->vid,
David S. Millercdf09692015-08-11 12:00:37 -07001749 GLOBAL_ATU_DATA_STATE_UNUSED);
1750 mutex_unlock(&ps->smi_mutex);
1751
1752 return ret;
1753}
1754
Vivien Didelot1d194042015-08-10 09:09:51 -04001755static int _mv88e6xxx_atu_getnext(struct dsa_switch *ds, u16 fid,
Vivien Didelot1d194042015-08-10 09:09:51 -04001756 struct mv88e6xxx_atu_entry *entry)
David S. Millercdf09692015-08-11 12:00:37 -07001757{
Vivien Didelot1d194042015-08-10 09:09:51 -04001758 struct mv88e6xxx_atu_entry next = { 0 };
1759 int ret;
1760
1761 next.fid = fid;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001762
1763 ret = _mv88e6xxx_atu_wait(ds);
1764 if (ret < 0)
1765 return ret;
1766
Vivien Didelot70cc99d2015-09-04 14:34:10 -04001767 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_FID, fid);
1768 if (ret < 0)
1769 return ret;
1770
1771 ret = _mv88e6xxx_atu_cmd(ds, GLOBAL_ATU_OP_GET_NEXT_DB);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001772 if (ret < 0)
1773 return ret;
1774
Vivien Didelot1d194042015-08-10 09:09:51 -04001775 ret = _mv88e6xxx_atu_mac_read(ds, next.mac);
1776 if (ret < 0)
1777 return ret;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001778
Vivien Didelot1d194042015-08-10 09:09:51 -04001779 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_ATU_DATA);
1780 if (ret < 0)
1781 return ret;
1782
1783 next.state = ret & GLOBAL_ATU_DATA_STATE_MASK;
1784 if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
1785 unsigned int mask, shift;
1786
1787 if (ret & GLOBAL_ATU_DATA_TRUNK) {
1788 next.trunk = true;
1789 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
1790 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
1791 } else {
1792 next.trunk = false;
1793 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
1794 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
1795 }
1796
1797 next.portv_trunkid = (ret & mask) >> shift;
1798 }
1799
1800 *entry = next;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001801 return 0;
1802}
1803
Vivien Didelotf33475b2015-10-22 09:34:41 -04001804int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
1805 struct switchdev_obj_port_fdb *fdb,
1806 int (*cb)(struct switchdev_obj *obj))
1807{
1808 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1809 struct mv88e6xxx_vtu_stu_entry vlan = {
1810 .vid = GLOBAL_VTU_VID_MASK, /* all ones */
1811 };
1812 int err;
1813
1814 mutex_lock(&ps->smi_mutex);
1815
1816 err = _mv88e6xxx_vtu_vid_write(ds, vlan.vid);
1817 if (err)
1818 goto unlock;
1819
1820 do {
1821 struct mv88e6xxx_atu_entry addr = {
1822 .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
1823 };
1824
1825 err = _mv88e6xxx_vtu_getnext(ds, &vlan);
1826 if (err)
1827 goto unlock;
1828
1829 if (!vlan.valid)
1830 break;
1831
1832 err = _mv88e6xxx_atu_mac_write(ds, addr.mac);
1833 if (err)
1834 goto unlock;
1835
1836 do {
1837 err = _mv88e6xxx_atu_getnext(ds, vlan.fid, &addr);
1838 if (err)
1839 goto unlock;
1840
1841 if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
1842 break;
1843
1844 if (!addr.trunk && addr.portv_trunkid & BIT(port)) {
1845 bool is_static = addr.state ==
1846 (is_multicast_ether_addr(addr.mac) ?
1847 GLOBAL_ATU_DATA_STATE_MC_STATIC :
1848 GLOBAL_ATU_DATA_STATE_UC_STATIC);
1849
1850 fdb->vid = vlan.vid;
1851 ether_addr_copy(fdb->addr, addr.mac);
1852 fdb->ndm_state = is_static ? NUD_NOARP :
1853 NUD_REACHABLE;
1854
1855 err = cb(&fdb->obj);
1856 if (err)
1857 goto unlock;
1858 }
1859 } while (!is_broadcast_ether_addr(addr.mac));
1860
1861 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
1862
1863unlock:
1864 mutex_unlock(&ps->smi_mutex);
1865
1866 return err;
1867}
1868
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001869static void mv88e6xxx_bridge_work(struct work_struct *work)
1870{
1871 struct mv88e6xxx_priv_state *ps;
1872 struct dsa_switch *ds;
1873 int port;
1874
1875 ps = container_of(work, struct mv88e6xxx_priv_state, bridge_work);
1876 ds = ((struct dsa_switch *)ps) - 1;
1877
1878 while (ps->port_state_update_mask) {
1879 port = __ffs(ps->port_state_update_mask);
1880 clear_bit(port, &ps->port_state_update_mask);
1881 mv88e6xxx_set_port_state(ds, port, ps->port_state[port]);
1882 }
1883}
1884
Andrew Lunndbde9e62015-05-06 01:09:48 +02001885static int mv88e6xxx_setup_port(struct dsa_switch *ds, int port)
Guenter Roeckd827e882015-03-26 18:36:29 -07001886{
1887 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001888 int ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02001889 u16 reg;
Guenter Roeckd827e882015-03-26 18:36:29 -07001890
1891 mutex_lock(&ps->smi_mutex);
1892
Andrew Lunn54d792f2015-05-06 01:09:47 +02001893 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
1894 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
1895 mv88e6xxx_6185_family(ds) || mv88e6xxx_6095_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07001896 mv88e6xxx_6065_family(ds) || mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02001897 /* MAC Forcing register: don't force link, speed,
1898 * duplex or flow control state to any particular
1899 * values on physical ports, but force the CPU port
1900 * and all DSA ports to their maximum bandwidth and
1901 * full duplex.
1902 */
1903 reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_PCS_CTRL);
Andrew Lunn60045cb2015-08-17 23:52:51 +02001904 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
Russell King53adc9e2015-09-21 21:42:59 +01001905 reg &= ~PORT_PCS_CTRL_UNFORCED;
Andrew Lunn54d792f2015-05-06 01:09:47 +02001906 reg |= PORT_PCS_CTRL_FORCE_LINK |
1907 PORT_PCS_CTRL_LINK_UP |
1908 PORT_PCS_CTRL_DUPLEX_FULL |
1909 PORT_PCS_CTRL_FORCE_DUPLEX;
1910 if (mv88e6xxx_6065_family(ds))
1911 reg |= PORT_PCS_CTRL_100;
1912 else
1913 reg |= PORT_PCS_CTRL_1000;
1914 } else {
1915 reg |= PORT_PCS_CTRL_UNFORCED;
1916 }
1917
1918 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
1919 PORT_PCS_CTRL, reg);
1920 if (ret)
1921 goto abort;
1922 }
1923
1924 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
1925 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
1926 * tunneling, determine priority by looking at 802.1p and IP
1927 * priority fields (IP prio has precedence), and set STP state
1928 * to Forwarding.
1929 *
1930 * If this is the CPU link, use DSA or EDSA tagging depending
1931 * on which tagging mode was configured.
1932 *
1933 * If this is a link to another switch, use DSA tagging mode.
1934 *
1935 * If this is the upstream port for this switch, enable
1936 * forwarding of unknown unicasts and multicasts.
1937 */
1938 reg = 0;
1939 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
1940 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
1941 mv88e6xxx_6095_family(ds) || mv88e6xxx_6065_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07001942 mv88e6xxx_6185_family(ds) || mv88e6xxx_6320_family(ds))
Andrew Lunn54d792f2015-05-06 01:09:47 +02001943 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
1944 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
1945 PORT_CONTROL_STATE_FORWARDING;
1946 if (dsa_is_cpu_port(ds, port)) {
1947 if (mv88e6xxx_6095_family(ds) || mv88e6xxx_6185_family(ds))
1948 reg |= PORT_CONTROL_DSA_TAG;
1949 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07001950 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
1951 mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02001952 if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA)
1953 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA;
1954 else
1955 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunnc047a1f2015-09-29 01:50:56 +02001956 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
1957 PORT_CONTROL_FORWARD_UNKNOWN_MC;
Andrew Lunn54d792f2015-05-06 01:09:47 +02001958 }
1959
1960 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
1961 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
1962 mv88e6xxx_6095_family(ds) || mv88e6xxx_6065_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07001963 mv88e6xxx_6185_family(ds) || mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02001964 if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA)
1965 reg |= PORT_CONTROL_EGRESS_ADD_TAG;
1966 }
1967 }
Andrew Lunn6083ce72015-08-17 23:52:52 +02001968 if (dsa_is_dsa_port(ds, port)) {
1969 if (mv88e6xxx_6095_family(ds) || mv88e6xxx_6185_family(ds))
1970 reg |= PORT_CONTROL_DSA_TAG;
1971 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
1972 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
1973 mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02001974 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunn6083ce72015-08-17 23:52:52 +02001975 }
1976
Andrew Lunn54d792f2015-05-06 01:09:47 +02001977 if (port == dsa_upstream_port(ds))
1978 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
1979 PORT_CONTROL_FORWARD_UNKNOWN_MC;
1980 }
1981 if (reg) {
1982 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
1983 PORT_CONTROL, reg);
1984 if (ret)
1985 goto abort;
1986 }
1987
Vivien Didelot8efdda42015-08-13 12:52:23 -04001988 /* Port Control 2: don't force a good FCS, set the maximum frame size to
1989 * 10240 bytes, enable secure 802.1q tags, don't discard tagged or
1990 * untagged frames on this port, do a destination address lookup on all
1991 * received packets as usual, disable ARP mirroring and don't send a
1992 * copy of all transmitted/received frames on this port to the CPU.
Andrew Lunn54d792f2015-05-06 01:09:47 +02001993 */
1994 reg = 0;
1995 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
1996 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07001997 mv88e6xxx_6095_family(ds) || mv88e6xxx_6320_family(ds))
Andrew Lunn54d792f2015-05-06 01:09:47 +02001998 reg = PORT_CONTROL_2_MAP_DA;
1999
2000 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002001 mv88e6xxx_6165_family(ds) || mv88e6xxx_6320_family(ds))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002002 reg |= PORT_CONTROL_2_JUMBO_10240;
2003
2004 if (mv88e6xxx_6095_family(ds) || mv88e6xxx_6185_family(ds)) {
2005 /* Set the upstream port this port should use */
2006 reg |= dsa_upstream_port(ds);
2007 /* enable forwarding of unknown multicast addresses to
2008 * the upstream port
2009 */
2010 if (port == dsa_upstream_port(ds))
2011 reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
2012 }
2013
Vivien Didelot5fe7f682015-10-11 18:08:38 -04002014 reg |= PORT_CONTROL_2_8021Q_SECURE;
Vivien Didelot8efdda42015-08-13 12:52:23 -04002015
Andrew Lunn54d792f2015-05-06 01:09:47 +02002016 if (reg) {
2017 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2018 PORT_CONTROL_2, reg);
2019 if (ret)
2020 goto abort;
2021 }
2022
2023 /* Port Association Vector: when learning source addresses
2024 * of packets, add the address to the address database using
2025 * a port bitmap that has only the bit for this port set and
2026 * the other bits clear.
2027 */
2028 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_ASSOC_VECTOR,
2029 1 << port);
2030 if (ret)
2031 goto abort;
2032
2033 /* Egress rate control 2: disable egress rate control. */
2034 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_RATE_CONTROL_2,
2035 0x0000);
2036 if (ret)
2037 goto abort;
2038
2039 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002040 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2041 mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002042 /* Do not limit the period of time that this port can
2043 * be paused for by the remote end or the period of
2044 * time that this port can pause the remote end.
2045 */
2046 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2047 PORT_PAUSE_CTRL, 0x0000);
2048 if (ret)
2049 goto abort;
2050
2051 /* Port ATU control: disable limiting the number of
2052 * address database entries that this port is allowed
2053 * to use.
2054 */
2055 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2056 PORT_ATU_CONTROL, 0x0000);
2057 /* Priority Override: disable DA, SA and VTU priority
2058 * override.
2059 */
2060 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2061 PORT_PRI_OVERRIDE, 0x0000);
2062 if (ret)
2063 goto abort;
2064
2065 /* Port Ethertype: use the Ethertype DSA Ethertype
2066 * value.
2067 */
2068 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2069 PORT_ETH_TYPE, ETH_P_EDSA);
2070 if (ret)
2071 goto abort;
2072 /* Tag Remap: use an identity 802.1p prio -> switch
2073 * prio mapping.
2074 */
2075 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2076 PORT_TAG_REGMAP_0123, 0x3210);
2077 if (ret)
2078 goto abort;
2079
2080 /* Tag Remap 2: use an identity 802.1p prio -> switch
2081 * prio mapping.
2082 */
2083 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2084 PORT_TAG_REGMAP_4567, 0x7654);
2085 if (ret)
2086 goto abort;
2087 }
2088
2089 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2090 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002091 mv88e6xxx_6185_family(ds) || mv88e6xxx_6095_family(ds) ||
2092 mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002093 /* Rate Control: disable ingress rate limiting. */
2094 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2095 PORT_RATE_CONTROL, 0x0001);
2096 if (ret)
2097 goto abort;
2098 }
2099
Guenter Roeck366f0a02015-03-26 18:36:30 -07002100 /* Port Control 1: disable trunking, disable sending
2101 * learning messages to this port.
Guenter Roeckd827e882015-03-26 18:36:29 -07002102 */
Vivien Didelot614f03f2015-04-20 17:19:23 -04002103 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_CONTROL_1, 0x0000);
Guenter Roeckd827e882015-03-26 18:36:29 -07002104 if (ret)
2105 goto abort;
2106
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002107 /* Port based VLAN map: do not give each port its own address
Vivien Didelot5fe7f682015-10-11 18:08:38 -04002108 * database, and allow every port to egress frames on all other ports.
Guenter Roeckd827e882015-03-26 18:36:29 -07002109 */
Vivien Didelot5fe7f682015-10-11 18:08:38 -04002110 reg = BIT(ps->num_ports) - 1; /* all ports */
Vivien Didelotede80982015-10-11 18:08:35 -04002111 ret = _mv88e6xxx_port_vlan_map_set(ds, port, reg & ~port);
Guenter Roeckd827e882015-03-26 18:36:29 -07002112 if (ret)
2113 goto abort;
2114
2115 /* Default VLAN ID and priority: don't set a default VLAN
2116 * ID, and set the default packet priority to zero.
2117 */
Vivien Didelot47cf1e652015-04-20 17:43:26 -04002118 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_DEFAULT_VLAN,
2119 0x0000);
Guenter Roeckd827e882015-03-26 18:36:29 -07002120abort:
2121 mutex_unlock(&ps->smi_mutex);
2122 return ret;
2123}
2124
Andrew Lunndbde9e62015-05-06 01:09:48 +02002125int mv88e6xxx_setup_ports(struct dsa_switch *ds)
2126{
2127 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2128 int ret;
2129 int i;
2130
2131 for (i = 0; i < ps->num_ports; i++) {
2132 ret = mv88e6xxx_setup_port(ds, i);
2133 if (ret < 0)
2134 return ret;
2135 }
2136 return 0;
2137}
2138
Guenter Roeckacdaffc2015-03-26 18:36:28 -07002139int mv88e6xxx_setup_common(struct dsa_switch *ds)
2140{
2141 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2142
2143 mutex_init(&ps->smi_mutex);
Guenter Roeckacdaffc2015-03-26 18:36:28 -07002144
Andrew Lunncca8b132015-04-02 04:06:39 +02002145 ps->id = REG_READ(REG_PORT(0), PORT_SWITCH_ID) & 0xfff0;
Andrew Lunna8f064c2015-03-26 18:36:40 -07002146
Guenter Roeckfacd95b2015-03-26 18:36:35 -07002147 INIT_WORK(&ps->bridge_work, mv88e6xxx_bridge_work);
2148
Guenter Roeckacdaffc2015-03-26 18:36:28 -07002149 return 0;
2150}
2151
Andrew Lunn54d792f2015-05-06 01:09:47 +02002152int mv88e6xxx_setup_global(struct dsa_switch *ds)
2153{
2154 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelot24751e22015-08-03 09:17:44 -04002155 int ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002156 int i;
2157
2158 /* Set the default address aging time to 5 minutes, and
2159 * enable address learn messages to be sent to all message
2160 * ports.
2161 */
2162 REG_WRITE(REG_GLOBAL, GLOBAL_ATU_CONTROL,
2163 0x0140 | GLOBAL_ATU_CONTROL_LEARN2ALL);
2164
2165 /* Configure the IP ToS mapping registers. */
2166 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_0, 0x0000);
2167 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_1, 0x0000);
2168 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_2, 0x5555);
2169 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_3, 0x5555);
2170 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_4, 0xaaaa);
2171 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_5, 0xaaaa);
2172 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_6, 0xffff);
2173 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_7, 0xffff);
2174
2175 /* Configure the IEEE 802.1p priority mapping register. */
2176 REG_WRITE(REG_GLOBAL, GLOBAL_IEEE_PRI, 0xfa41);
2177
2178 /* Send all frames with destination addresses matching
2179 * 01:80:c2:00:00:0x to the CPU port.
2180 */
2181 REG_WRITE(REG_GLOBAL2, GLOBAL2_MGMT_EN_0X, 0xffff);
2182
2183 /* Ignore removed tag data on doubly tagged packets, disable
2184 * flow control messages, force flow control priority to the
2185 * highest, and send all special multicast frames to the CPU
2186 * port at the highest priority.
2187 */
2188 REG_WRITE(REG_GLOBAL2, GLOBAL2_SWITCH_MGMT,
2189 0x7 | GLOBAL2_SWITCH_MGMT_RSVD2CPU | 0x70 |
2190 GLOBAL2_SWITCH_MGMT_FORCE_FLOW_CTRL_PRI);
2191
2192 /* Program the DSA routing table. */
2193 for (i = 0; i < 32; i++) {
2194 int nexthop = 0x1f;
2195
2196 if (ds->pd->rtable &&
2197 i != ds->index && i < ds->dst->pd->nr_chips)
2198 nexthop = ds->pd->rtable[i] & 0x1f;
2199
2200 REG_WRITE(REG_GLOBAL2, GLOBAL2_DEVICE_MAPPING,
2201 GLOBAL2_DEVICE_MAPPING_UPDATE |
2202 (i << GLOBAL2_DEVICE_MAPPING_TARGET_SHIFT) |
2203 nexthop);
2204 }
2205
2206 /* Clear all trunk masks. */
2207 for (i = 0; i < 8; i++)
2208 REG_WRITE(REG_GLOBAL2, GLOBAL2_TRUNK_MASK,
2209 0x8000 | (i << GLOBAL2_TRUNK_MASK_NUM_SHIFT) |
2210 ((1 << ps->num_ports) - 1));
2211
2212 /* Clear all trunk mappings. */
2213 for (i = 0; i < 16; i++)
2214 REG_WRITE(REG_GLOBAL2, GLOBAL2_TRUNK_MAPPING,
2215 GLOBAL2_TRUNK_MAPPING_UPDATE |
2216 (i << GLOBAL2_TRUNK_MAPPING_ID_SHIFT));
2217
2218 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002219 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2220 mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002221 /* Send all frames with destination addresses matching
2222 * 01:80:c2:00:00:2x to the CPU port.
2223 */
2224 REG_WRITE(REG_GLOBAL2, GLOBAL2_MGMT_EN_2X, 0xffff);
2225
2226 /* Initialise cross-chip port VLAN table to reset
2227 * defaults.
2228 */
2229 REG_WRITE(REG_GLOBAL2, GLOBAL2_PVT_ADDR, 0x9000);
2230
2231 /* Clear the priority override table. */
2232 for (i = 0; i < 16; i++)
2233 REG_WRITE(REG_GLOBAL2, GLOBAL2_PRIO_OVERRIDE,
2234 0x8000 | (i << 8));
2235 }
2236
2237 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2238 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002239 mv88e6xxx_6185_family(ds) || mv88e6xxx_6095_family(ds) ||
2240 mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002241 /* Disable ingress rate limiting by resetting all
2242 * ingress rate limit registers to their initial
2243 * state.
2244 */
2245 for (i = 0; i < ps->num_ports; i++)
2246 REG_WRITE(REG_GLOBAL2, GLOBAL2_INGRESS_OP,
2247 0x9000 | (i << 8));
2248 }
2249
Andrew Lunndb687a52015-06-20 21:31:29 +02002250 /* Clear the statistics counters for all ports */
2251 REG_WRITE(REG_GLOBAL, GLOBAL_STATS_OP, GLOBAL_STATS_OP_FLUSH_ALL);
2252
2253 /* Wait for the flush to complete. */
Vivien Didelot24751e22015-08-03 09:17:44 -04002254 mutex_lock(&ps->smi_mutex);
2255 ret = _mv88e6xxx_stats_wait(ds);
Vivien Didelot6b17e862015-08-13 12:52:18 -04002256 if (ret < 0)
2257 goto unlock;
2258
Vivien Didelotc161d0a2015-09-04 14:34:13 -04002259 /* Clear all ATU entries */
2260 ret = _mv88e6xxx_atu_flush(ds, 0, true);
2261 if (ret < 0)
2262 goto unlock;
2263
Vivien Didelot6b17e862015-08-13 12:52:18 -04002264 /* Clear all the VTU and STU entries */
2265 ret = _mv88e6xxx_vtu_stu_flush(ds);
2266unlock:
Vivien Didelot24751e22015-08-03 09:17:44 -04002267 mutex_unlock(&ps->smi_mutex);
Andrew Lunndb687a52015-06-20 21:31:29 +02002268
Vivien Didelot24751e22015-08-03 09:17:44 -04002269 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002270}
2271
Andrew Lunn143a8302015-04-02 04:06:34 +02002272int mv88e6xxx_switch_reset(struct dsa_switch *ds, bool ppu_active)
2273{
2274 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2275 u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
2276 unsigned long timeout;
2277 int ret;
2278 int i;
2279
2280 /* Set all ports to the disabled state. */
2281 for (i = 0; i < ps->num_ports; i++) {
Andrew Lunncca8b132015-04-02 04:06:39 +02002282 ret = REG_READ(REG_PORT(i), PORT_CONTROL);
2283 REG_WRITE(REG_PORT(i), PORT_CONTROL, ret & 0xfffc);
Andrew Lunn143a8302015-04-02 04:06:34 +02002284 }
2285
2286 /* Wait for transmit queues to drain. */
2287 usleep_range(2000, 4000);
2288
2289 /* Reset the switch. Keep the PPU active if requested. The PPU
2290 * needs to be active to support indirect phy register access
2291 * through global registers 0x18 and 0x19.
2292 */
2293 if (ppu_active)
2294 REG_WRITE(REG_GLOBAL, 0x04, 0xc000);
2295 else
2296 REG_WRITE(REG_GLOBAL, 0x04, 0xc400);
2297
2298 /* Wait up to one second for reset to complete. */
2299 timeout = jiffies + 1 * HZ;
2300 while (time_before(jiffies, timeout)) {
2301 ret = REG_READ(REG_GLOBAL, 0x00);
2302 if ((ret & is_reset) == is_reset)
2303 break;
2304 usleep_range(1000, 2000);
2305 }
2306 if (time_after(jiffies, timeout))
2307 return -ETIMEDOUT;
2308
2309 return 0;
2310}
2311
Andrew Lunn491435852015-04-02 04:06:35 +02002312int mv88e6xxx_phy_page_read(struct dsa_switch *ds, int port, int page, int reg)
2313{
2314 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2315 int ret;
2316
Andrew Lunn3898c142015-05-06 01:09:53 +02002317 mutex_lock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002318 ret = _mv88e6xxx_phy_write_indirect(ds, port, 0x16, page);
Andrew Lunn491435852015-04-02 04:06:35 +02002319 if (ret < 0)
2320 goto error;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002321 ret = _mv88e6xxx_phy_read_indirect(ds, port, reg);
Andrew Lunn491435852015-04-02 04:06:35 +02002322error:
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002323 _mv88e6xxx_phy_write_indirect(ds, port, 0x16, 0x0);
Andrew Lunn3898c142015-05-06 01:09:53 +02002324 mutex_unlock(&ps->smi_mutex);
Andrew Lunn491435852015-04-02 04:06:35 +02002325 return ret;
2326}
2327
2328int mv88e6xxx_phy_page_write(struct dsa_switch *ds, int port, int page,
2329 int reg, int val)
2330{
2331 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2332 int ret;
2333
Andrew Lunn3898c142015-05-06 01:09:53 +02002334 mutex_lock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002335 ret = _mv88e6xxx_phy_write_indirect(ds, port, 0x16, page);
Andrew Lunn491435852015-04-02 04:06:35 +02002336 if (ret < 0)
2337 goto error;
2338
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002339 ret = _mv88e6xxx_phy_write_indirect(ds, port, reg, val);
Andrew Lunn491435852015-04-02 04:06:35 +02002340error:
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002341 _mv88e6xxx_phy_write_indirect(ds, port, 0x16, 0x0);
Andrew Lunn3898c142015-05-06 01:09:53 +02002342 mutex_unlock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002343 return ret;
2344}
2345
2346static int mv88e6xxx_port_to_phy_addr(struct dsa_switch *ds, int port)
2347{
2348 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2349
2350 if (port >= 0 && port < ps->num_ports)
2351 return port;
2352 return -EINVAL;
2353}
2354
2355int
2356mv88e6xxx_phy_read(struct dsa_switch *ds, int port, int regnum)
2357{
2358 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2359 int addr = mv88e6xxx_port_to_phy_addr(ds, port);
2360 int ret;
2361
2362 if (addr < 0)
2363 return addr;
2364
Andrew Lunn3898c142015-05-06 01:09:53 +02002365 mutex_lock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002366 ret = _mv88e6xxx_phy_read(ds, addr, regnum);
Andrew Lunn3898c142015-05-06 01:09:53 +02002367 mutex_unlock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002368 return ret;
2369}
2370
2371int
2372mv88e6xxx_phy_write(struct dsa_switch *ds, int port, int regnum, u16 val)
2373{
2374 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2375 int addr = mv88e6xxx_port_to_phy_addr(ds, port);
2376 int ret;
2377
2378 if (addr < 0)
2379 return addr;
2380
Andrew Lunn3898c142015-05-06 01:09:53 +02002381 mutex_lock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002382 ret = _mv88e6xxx_phy_write(ds, addr, regnum, val);
Andrew Lunn3898c142015-05-06 01:09:53 +02002383 mutex_unlock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002384 return ret;
2385}
2386
2387int
2388mv88e6xxx_phy_read_indirect(struct dsa_switch *ds, int port, int regnum)
2389{
2390 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2391 int addr = mv88e6xxx_port_to_phy_addr(ds, port);
2392 int ret;
2393
2394 if (addr < 0)
2395 return addr;
2396
Andrew Lunn3898c142015-05-06 01:09:53 +02002397 mutex_lock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002398 ret = _mv88e6xxx_phy_read_indirect(ds, addr, regnum);
Andrew Lunn3898c142015-05-06 01:09:53 +02002399 mutex_unlock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002400 return ret;
2401}
2402
2403int
2404mv88e6xxx_phy_write_indirect(struct dsa_switch *ds, int port, int regnum,
2405 u16 val)
2406{
2407 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2408 int addr = mv88e6xxx_port_to_phy_addr(ds, port);
2409 int ret;
2410
2411 if (addr < 0)
2412 return addr;
2413
Andrew Lunn3898c142015-05-06 01:09:53 +02002414 mutex_lock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002415 ret = _mv88e6xxx_phy_write_indirect(ds, addr, regnum, val);
Andrew Lunn3898c142015-05-06 01:09:53 +02002416 mutex_unlock(&ps->smi_mutex);
Andrew Lunn491435852015-04-02 04:06:35 +02002417 return ret;
2418}
2419
Guenter Roeckc22995c2015-07-25 09:42:28 -07002420#ifdef CONFIG_NET_DSA_HWMON
2421
2422static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
2423{
2424 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2425 int ret;
2426 int val;
2427
2428 *temp = 0;
2429
2430 mutex_lock(&ps->smi_mutex);
2431
2432 ret = _mv88e6xxx_phy_write(ds, 0x0, 0x16, 0x6);
2433 if (ret < 0)
2434 goto error;
2435
2436 /* Enable temperature sensor */
2437 ret = _mv88e6xxx_phy_read(ds, 0x0, 0x1a);
2438 if (ret < 0)
2439 goto error;
2440
2441 ret = _mv88e6xxx_phy_write(ds, 0x0, 0x1a, ret | (1 << 5));
2442 if (ret < 0)
2443 goto error;
2444
2445 /* Wait for temperature to stabilize */
2446 usleep_range(10000, 12000);
2447
2448 val = _mv88e6xxx_phy_read(ds, 0x0, 0x1a);
2449 if (val < 0) {
2450 ret = val;
2451 goto error;
2452 }
2453
2454 /* Disable temperature sensor */
2455 ret = _mv88e6xxx_phy_write(ds, 0x0, 0x1a, ret & ~(1 << 5));
2456 if (ret < 0)
2457 goto error;
2458
2459 *temp = ((val & 0x1f) - 5) * 5;
2460
2461error:
2462 _mv88e6xxx_phy_write(ds, 0x0, 0x16, 0x0);
2463 mutex_unlock(&ps->smi_mutex);
2464 return ret;
2465}
2466
2467static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
2468{
2469 int phy = mv88e6xxx_6320_family(ds) ? 3 : 0;
2470 int ret;
2471
2472 *temp = 0;
2473
2474 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 27);
2475 if (ret < 0)
2476 return ret;
2477
2478 *temp = (ret & 0xff) - 25;
2479
2480 return 0;
2481}
2482
2483int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
2484{
2485 if (mv88e6xxx_6320_family(ds) || mv88e6xxx_6352_family(ds))
2486 return mv88e63xx_get_temp(ds, temp);
2487
2488 return mv88e61xx_get_temp(ds, temp);
2489}
2490
2491int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
2492{
2493 int phy = mv88e6xxx_6320_family(ds) ? 3 : 0;
2494 int ret;
2495
2496 if (!mv88e6xxx_6320_family(ds) && !mv88e6xxx_6352_family(ds))
2497 return -EOPNOTSUPP;
2498
2499 *temp = 0;
2500
2501 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26);
2502 if (ret < 0)
2503 return ret;
2504
2505 *temp = (((ret >> 8) & 0x1f) * 5) - 25;
2506
2507 return 0;
2508}
2509
2510int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
2511{
2512 int phy = mv88e6xxx_6320_family(ds) ? 3 : 0;
2513 int ret;
2514
2515 if (!mv88e6xxx_6320_family(ds) && !mv88e6xxx_6352_family(ds))
2516 return -EOPNOTSUPP;
2517
2518 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26);
2519 if (ret < 0)
2520 return ret;
2521 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
2522 return mv88e6xxx_phy_page_write(ds, phy, 6, 26,
2523 (ret & 0xe0ff) | (temp << 8));
2524}
2525
2526int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
2527{
2528 int phy = mv88e6xxx_6320_family(ds) ? 3 : 0;
2529 int ret;
2530
2531 if (!mv88e6xxx_6320_family(ds) && !mv88e6xxx_6352_family(ds))
2532 return -EOPNOTSUPP;
2533
2534 *alarm = false;
2535
2536 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26);
2537 if (ret < 0)
2538 return ret;
2539
2540 *alarm = !!(ret & 0x40);
2541
2542 return 0;
2543}
2544#endif /* CONFIG_NET_DSA_HWMON */
2545
Ben Hutchings98e67302011-11-25 14:36:19 +00002546static int __init mv88e6xxx_init(void)
2547{
2548#if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
2549 register_switch_driver(&mv88e6131_switch_driver);
2550#endif
2551#if IS_ENABLED(CONFIG_NET_DSA_MV88E6123_61_65)
2552 register_switch_driver(&mv88e6123_61_65_switch_driver);
2553#endif
Guenter Roeck3ad50cc2014-10-29 10:44:56 -07002554#if IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
2555 register_switch_driver(&mv88e6352_switch_driver);
2556#endif
Andrew Lunn42f27252014-09-12 23:58:44 +02002557#if IS_ENABLED(CONFIG_NET_DSA_MV88E6171)
2558 register_switch_driver(&mv88e6171_switch_driver);
2559#endif
Ben Hutchings98e67302011-11-25 14:36:19 +00002560 return 0;
2561}
2562module_init(mv88e6xxx_init);
2563
2564static void __exit mv88e6xxx_cleanup(void)
2565{
Andrew Lunn42f27252014-09-12 23:58:44 +02002566#if IS_ENABLED(CONFIG_NET_DSA_MV88E6171)
2567 unregister_switch_driver(&mv88e6171_switch_driver);
2568#endif
Vivien Didelot4212b542015-05-01 10:43:52 -04002569#if IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
2570 unregister_switch_driver(&mv88e6352_switch_driver);
2571#endif
Ben Hutchings98e67302011-11-25 14:36:19 +00002572#if IS_ENABLED(CONFIG_NET_DSA_MV88E6123_61_65)
2573 unregister_switch_driver(&mv88e6123_61_65_switch_driver);
2574#endif
2575#if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
2576 unregister_switch_driver(&mv88e6131_switch_driver);
2577#endif
2578}
2579module_exit(mv88e6xxx_cleanup);
Ben Hutchings3d825ed2011-11-25 14:37:16 +00002580
2581MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
2582MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
2583MODULE_LICENSE("GPL");