blob: 6e684f3d377c3152a72fb16a7a2531b7d2c2296a [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 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 */
10
Andrew Lunn87c8cef2015-06-20 18:42:28 +020011#include <linux/debugfs.h>
Barry Grussling19b2f972013-01-08 16:05:54 +000012#include <linux/delay.h>
Guenter Roeckdefb05b2015-03-26 18:36:38 -070013#include <linux/etherdevice.h>
Guenter Roeckfacd95b2015-03-26 18:36:35 -070014#include <linux/if_bridge.h>
Barry Grussling19b2f972013-01-08 16:05:54 +000015#include <linux/jiffies.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000016#include <linux/list.h>
Paul Gortmaker2bbba272012-01-24 10:41:40 +000017#include <linux/module.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000018#include <linux/netdevice.h>
19#include <linux/phy.h>
Andrew Lunn87c8cef2015-06-20 18:42:28 +020020#include <linux/seq_file.h>
Ben Hutchingsc8f0b862011-11-27 17:06:08 +000021#include <net/dsa.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000022#include "mv88e6xxx.h"
23
Andrew Lunn16fe24f2015-05-06 01:09:55 +020024/* MDIO bus access can be nested in the case of PHYs connected to the
25 * internal MDIO bus of the switch, which is accessed via MDIO bus of
26 * the Ethernet interface. Avoid lockdep false positives by using
27 * mutex_lock_nested().
28 */
29static int mv88e6xxx_mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
30{
31 int ret;
32
33 mutex_lock_nested(&bus->mdio_lock, SINGLE_DEPTH_NESTING);
34 ret = bus->read(bus, addr, regnum);
35 mutex_unlock(&bus->mdio_lock);
36
37 return ret;
38}
39
40static int mv88e6xxx_mdiobus_write(struct mii_bus *bus, int addr, u32 regnum,
41 u16 val)
42{
43 int ret;
44
45 mutex_lock_nested(&bus->mdio_lock, SINGLE_DEPTH_NESTING);
46 ret = bus->write(bus, addr, regnum, val);
47 mutex_unlock(&bus->mdio_lock);
48
49 return ret;
50}
51
Barry Grussling3675c8d2013-01-08 16:05:53 +000052/* If the switch's ADDR[4:0] strap pins are strapped to zero, it will
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000053 * use all 32 SMI bus addresses on its SMI bus, and all switch registers
54 * will be directly accessible on some {device address,register address}
55 * pair. If the ADDR[4:0] pins are not strapped to zero, the switch
56 * will only respond to SMI transactions to that specific address, and
57 * an indirect addressing mechanism needs to be used to access its
58 * registers.
59 */
60static int mv88e6xxx_reg_wait_ready(struct mii_bus *bus, int sw_addr)
61{
62 int ret;
63 int i;
64
65 for (i = 0; i < 16; i++) {
Andrew Lunn16fe24f2015-05-06 01:09:55 +020066 ret = mv88e6xxx_mdiobus_read(bus, sw_addr, SMI_CMD);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000067 if (ret < 0)
68 return ret;
69
Andrew Lunncca8b132015-04-02 04:06:39 +020070 if ((ret & SMI_CMD_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000071 return 0;
72 }
73
74 return -ETIMEDOUT;
75}
76
77int __mv88e6xxx_reg_read(struct mii_bus *bus, int sw_addr, int addr, int reg)
78{
79 int ret;
80
81 if (sw_addr == 0)
Andrew Lunn16fe24f2015-05-06 01:09:55 +020082 return mv88e6xxx_mdiobus_read(bus, addr, reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000083
Barry Grussling3675c8d2013-01-08 16:05:53 +000084 /* Wait for the bus to become free. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000085 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
86 if (ret < 0)
87 return ret;
88
Barry Grussling3675c8d2013-01-08 16:05:53 +000089 /* Transmit the read command. */
Andrew Lunn16fe24f2015-05-06 01:09:55 +020090 ret = mv88e6xxx_mdiobus_write(bus, sw_addr, SMI_CMD,
91 SMI_CMD_OP_22_READ | (addr << 5) | reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000092 if (ret < 0)
93 return ret;
94
Barry Grussling3675c8d2013-01-08 16:05:53 +000095 /* Wait for the read command to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000096 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
97 if (ret < 0)
98 return ret;
99
Barry Grussling3675c8d2013-01-08 16:05:53 +0000100 /* Read the data. */
Andrew Lunn16fe24f2015-05-06 01:09:55 +0200101 ret = mv88e6xxx_mdiobus_read(bus, sw_addr, SMI_DATA);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000102 if (ret < 0)
103 return ret;
104
105 return ret & 0xffff;
106}
107
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700108/* Must be called with SMI mutex held */
109static int _mv88e6xxx_reg_read(struct dsa_switch *ds, int addr, int reg)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000110{
Guenter Roeckb184e492014-10-17 12:30:58 -0700111 struct mii_bus *bus = dsa_host_dev_to_mii_bus(ds->master_dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000112 int ret;
113
Guenter Roeckb184e492014-10-17 12:30:58 -0700114 if (bus == NULL)
115 return -EINVAL;
116
Guenter Roeckb184e492014-10-17 12:30:58 -0700117 ret = __mv88e6xxx_reg_read(bus, ds->pd->sw_addr, addr, reg);
Vivien Didelotbb92ea52015-01-23 16:10:36 -0500118 if (ret < 0)
119 return ret;
120
121 dev_dbg(ds->master_dev, "<- addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
122 addr, reg, ret);
123
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000124 return ret;
125}
126
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700127int mv88e6xxx_reg_read(struct dsa_switch *ds, int addr, int reg)
128{
129 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
130 int ret;
131
132 mutex_lock(&ps->smi_mutex);
133 ret = _mv88e6xxx_reg_read(ds, addr, reg);
134 mutex_unlock(&ps->smi_mutex);
135
136 return ret;
137}
138
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000139int __mv88e6xxx_reg_write(struct mii_bus *bus, int sw_addr, int addr,
140 int reg, u16 val)
141{
142 int ret;
143
144 if (sw_addr == 0)
Andrew Lunn16fe24f2015-05-06 01:09:55 +0200145 return mv88e6xxx_mdiobus_write(bus, addr, reg, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000146
Barry Grussling3675c8d2013-01-08 16:05:53 +0000147 /* Wait for the bus to become free. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000148 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
149 if (ret < 0)
150 return ret;
151
Barry Grussling3675c8d2013-01-08 16:05:53 +0000152 /* Transmit the data to write. */
Andrew Lunn16fe24f2015-05-06 01:09:55 +0200153 ret = mv88e6xxx_mdiobus_write(bus, sw_addr, SMI_DATA, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000154 if (ret < 0)
155 return ret;
156
Barry Grussling3675c8d2013-01-08 16:05:53 +0000157 /* Transmit the write command. */
Andrew Lunn16fe24f2015-05-06 01:09:55 +0200158 ret = mv88e6xxx_mdiobus_write(bus, sw_addr, SMI_CMD,
159 SMI_CMD_OP_22_WRITE | (addr << 5) | reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000160 if (ret < 0)
161 return ret;
162
Barry Grussling3675c8d2013-01-08 16:05:53 +0000163 /* Wait for the write command to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000164 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
165 if (ret < 0)
166 return ret;
167
168 return 0;
169}
170
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700171/* Must be called with SMI mutex held */
172static int _mv88e6xxx_reg_write(struct dsa_switch *ds, int addr, int reg,
173 u16 val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000174{
Guenter Roeckb184e492014-10-17 12:30:58 -0700175 struct mii_bus *bus = dsa_host_dev_to_mii_bus(ds->master_dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000176
Guenter Roeckb184e492014-10-17 12:30:58 -0700177 if (bus == NULL)
178 return -EINVAL;
179
Vivien Didelotbb92ea52015-01-23 16:10:36 -0500180 dev_dbg(ds->master_dev, "-> addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
181 addr, reg, val);
182
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700183 return __mv88e6xxx_reg_write(bus, ds->pd->sw_addr, addr, reg, val);
184}
185
186int mv88e6xxx_reg_write(struct dsa_switch *ds, int addr, int reg, u16 val)
187{
188 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
189 int ret;
190
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000191 mutex_lock(&ps->smi_mutex);
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700192 ret = _mv88e6xxx_reg_write(ds, addr, reg, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000193 mutex_unlock(&ps->smi_mutex);
194
195 return ret;
196}
197
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000198int mv88e6xxx_set_addr_direct(struct dsa_switch *ds, u8 *addr)
199{
Andrew Lunncca8b132015-04-02 04:06:39 +0200200 REG_WRITE(REG_GLOBAL, GLOBAL_MAC_01, (addr[0] << 8) | addr[1]);
201 REG_WRITE(REG_GLOBAL, GLOBAL_MAC_23, (addr[2] << 8) | addr[3]);
202 REG_WRITE(REG_GLOBAL, GLOBAL_MAC_45, (addr[4] << 8) | addr[5]);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000203
204 return 0;
205}
206
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000207int mv88e6xxx_set_addr_indirect(struct dsa_switch *ds, u8 *addr)
208{
209 int i;
210 int ret;
211
212 for (i = 0; i < 6; i++) {
213 int j;
214
Barry Grussling3675c8d2013-01-08 16:05:53 +0000215 /* Write the MAC address byte. */
Andrew Lunncca8b132015-04-02 04:06:39 +0200216 REG_WRITE(REG_GLOBAL2, GLOBAL2_SWITCH_MAC,
217 GLOBAL2_SWITCH_MAC_BUSY | (i << 8) | addr[i]);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000218
Barry Grussling3675c8d2013-01-08 16:05:53 +0000219 /* Wait for the write to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000220 for (j = 0; j < 16; j++) {
Andrew Lunncca8b132015-04-02 04:06:39 +0200221 ret = REG_READ(REG_GLOBAL2, GLOBAL2_SWITCH_MAC);
222 if ((ret & GLOBAL2_SWITCH_MAC_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000223 break;
224 }
225 if (j == 16)
226 return -ETIMEDOUT;
227 }
228
229 return 0;
230}
231
Andrew Lunn3898c142015-05-06 01:09:53 +0200232/* Must be called with SMI mutex held */
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +0200233static int _mv88e6xxx_phy_read(struct dsa_switch *ds, int addr, int regnum)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000234{
235 if (addr >= 0)
Andrew Lunn3898c142015-05-06 01:09:53 +0200236 return _mv88e6xxx_reg_read(ds, addr, regnum);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000237 return 0xffff;
238}
239
Andrew Lunn3898c142015-05-06 01:09:53 +0200240/* Must be called with SMI mutex held */
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +0200241static int _mv88e6xxx_phy_write(struct dsa_switch *ds, int addr, int regnum,
242 u16 val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000243{
244 if (addr >= 0)
Andrew Lunn3898c142015-05-06 01:09:53 +0200245 return _mv88e6xxx_reg_write(ds, addr, regnum, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000246 return 0;
247}
248
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000249#ifdef CONFIG_NET_DSA_MV88E6XXX_NEED_PPU
250static int mv88e6xxx_ppu_disable(struct dsa_switch *ds)
251{
252 int ret;
Barry Grussling19b2f972013-01-08 16:05:54 +0000253 unsigned long timeout;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000254
Andrew Lunncca8b132015-04-02 04:06:39 +0200255 ret = REG_READ(REG_GLOBAL, GLOBAL_CONTROL);
256 REG_WRITE(REG_GLOBAL, GLOBAL_CONTROL,
257 ret & ~GLOBAL_CONTROL_PPU_ENABLE);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000258
Barry Grussling19b2f972013-01-08 16:05:54 +0000259 timeout = jiffies + 1 * HZ;
260 while (time_before(jiffies, timeout)) {
Andrew Lunncca8b132015-04-02 04:06:39 +0200261 ret = REG_READ(REG_GLOBAL, GLOBAL_STATUS);
Barry Grussling19b2f972013-01-08 16:05:54 +0000262 usleep_range(1000, 2000);
Andrew Lunncca8b132015-04-02 04:06:39 +0200263 if ((ret & GLOBAL_STATUS_PPU_MASK) !=
264 GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000265 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000266 }
267
268 return -ETIMEDOUT;
269}
270
271static int mv88e6xxx_ppu_enable(struct dsa_switch *ds)
272{
273 int ret;
Barry Grussling19b2f972013-01-08 16:05:54 +0000274 unsigned long timeout;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000275
Andrew Lunncca8b132015-04-02 04:06:39 +0200276 ret = REG_READ(REG_GLOBAL, GLOBAL_CONTROL);
277 REG_WRITE(REG_GLOBAL, GLOBAL_CONTROL, ret | GLOBAL_CONTROL_PPU_ENABLE);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000278
Barry Grussling19b2f972013-01-08 16:05:54 +0000279 timeout = jiffies + 1 * HZ;
280 while (time_before(jiffies, timeout)) {
Andrew Lunncca8b132015-04-02 04:06:39 +0200281 ret = REG_READ(REG_GLOBAL, GLOBAL_STATUS);
Barry Grussling19b2f972013-01-08 16:05:54 +0000282 usleep_range(1000, 2000);
Andrew Lunncca8b132015-04-02 04:06:39 +0200283 if ((ret & GLOBAL_STATUS_PPU_MASK) ==
284 GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000285 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000286 }
287
288 return -ETIMEDOUT;
289}
290
291static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
292{
293 struct mv88e6xxx_priv_state *ps;
294
295 ps = container_of(ugly, struct mv88e6xxx_priv_state, ppu_work);
296 if (mutex_trylock(&ps->ppu_mutex)) {
Barry Grussling85686582013-01-08 16:05:56 +0000297 struct dsa_switch *ds = ((struct dsa_switch *)ps) - 1;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000298
Barry Grussling85686582013-01-08 16:05:56 +0000299 if (mv88e6xxx_ppu_enable(ds) == 0)
300 ps->ppu_disabled = 0;
301 mutex_unlock(&ps->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000302 }
303}
304
305static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
306{
307 struct mv88e6xxx_priv_state *ps = (void *)_ps;
308
309 schedule_work(&ps->ppu_work);
310}
311
312static int mv88e6xxx_ppu_access_get(struct dsa_switch *ds)
313{
Florian Fainellia22adce2014-04-28 11:14:28 -0700314 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000315 int ret;
316
317 mutex_lock(&ps->ppu_mutex);
318
Barry Grussling3675c8d2013-01-08 16:05:53 +0000319 /* If the PHY polling unit is enabled, disable it so that
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000320 * we can access the PHY registers. If it was already
321 * disabled, cancel the timer that is going to re-enable
322 * it.
323 */
324 if (!ps->ppu_disabled) {
Barry Grussling85686582013-01-08 16:05:56 +0000325 ret = mv88e6xxx_ppu_disable(ds);
326 if (ret < 0) {
327 mutex_unlock(&ps->ppu_mutex);
328 return ret;
329 }
330 ps->ppu_disabled = 1;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000331 } else {
Barry Grussling85686582013-01-08 16:05:56 +0000332 del_timer(&ps->ppu_timer);
333 ret = 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000334 }
335
336 return ret;
337}
338
339static void mv88e6xxx_ppu_access_put(struct dsa_switch *ds)
340{
Florian Fainellia22adce2014-04-28 11:14:28 -0700341 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000342
Barry Grussling3675c8d2013-01-08 16:05:53 +0000343 /* Schedule a timer to re-enable the PHY polling unit. */
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000344 mod_timer(&ps->ppu_timer, jiffies + msecs_to_jiffies(10));
345 mutex_unlock(&ps->ppu_mutex);
346}
347
348void mv88e6xxx_ppu_state_init(struct dsa_switch *ds)
349{
Florian Fainellia22adce2014-04-28 11:14:28 -0700350 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000351
352 mutex_init(&ps->ppu_mutex);
353 INIT_WORK(&ps->ppu_work, mv88e6xxx_ppu_reenable_work);
354 init_timer(&ps->ppu_timer);
355 ps->ppu_timer.data = (unsigned long)ps;
356 ps->ppu_timer.function = mv88e6xxx_ppu_reenable_timer;
357}
358
359int mv88e6xxx_phy_read_ppu(struct dsa_switch *ds, int addr, int regnum)
360{
361 int ret;
362
363 ret = mv88e6xxx_ppu_access_get(ds);
364 if (ret >= 0) {
Barry Grussling85686582013-01-08 16:05:56 +0000365 ret = mv88e6xxx_reg_read(ds, addr, regnum);
366 mv88e6xxx_ppu_access_put(ds);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000367 }
368
369 return ret;
370}
371
372int mv88e6xxx_phy_write_ppu(struct dsa_switch *ds, int addr,
373 int regnum, u16 val)
374{
375 int ret;
376
377 ret = mv88e6xxx_ppu_access_get(ds);
378 if (ret >= 0) {
Barry Grussling85686582013-01-08 16:05:56 +0000379 ret = mv88e6xxx_reg_write(ds, addr, regnum, val);
380 mv88e6xxx_ppu_access_put(ds);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000381 }
382
383 return ret;
384}
385#endif
386
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000387void mv88e6xxx_poll_link(struct dsa_switch *ds)
388{
389 int i;
390
391 for (i = 0; i < DSA_MAX_PORTS; i++) {
392 struct net_device *dev;
Ingo Molnar2a9e7972008-11-25 16:50:49 -0800393 int uninitialized_var(port_status);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000394 int link;
395 int speed;
396 int duplex;
397 int fc;
398
399 dev = ds->ports[i];
400 if (dev == NULL)
401 continue;
402
403 link = 0;
404 if (dev->flags & IFF_UP) {
Andrew Lunncca8b132015-04-02 04:06:39 +0200405 port_status = mv88e6xxx_reg_read(ds, REG_PORT(i),
406 PORT_STATUS);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000407 if (port_status < 0)
408 continue;
409
Andrew Lunncca8b132015-04-02 04:06:39 +0200410 link = !!(port_status & PORT_STATUS_LINK);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000411 }
412
413 if (!link) {
414 if (netif_carrier_ok(dev)) {
Barry Grusslingab381a92013-01-08 16:05:55 +0000415 netdev_info(dev, "link down\n");
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000416 netif_carrier_off(dev);
417 }
418 continue;
419 }
420
Andrew Lunncca8b132015-04-02 04:06:39 +0200421 switch (port_status & PORT_STATUS_SPEED_MASK) {
422 case PORT_STATUS_SPEED_10:
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000423 speed = 10;
424 break;
Andrew Lunncca8b132015-04-02 04:06:39 +0200425 case PORT_STATUS_SPEED_100:
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000426 speed = 100;
427 break;
Andrew Lunncca8b132015-04-02 04:06:39 +0200428 case PORT_STATUS_SPEED_1000:
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000429 speed = 1000;
430 break;
431 default:
432 speed = -1;
433 break;
434 }
Andrew Lunncca8b132015-04-02 04:06:39 +0200435 duplex = (port_status & PORT_STATUS_DUPLEX) ? 1 : 0;
436 fc = (port_status & PORT_STATUS_PAUSE_EN) ? 1 : 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000437
438 if (!netif_carrier_ok(dev)) {
Barry Grusslingab381a92013-01-08 16:05:55 +0000439 netdev_info(dev,
440 "link up, %d Mb/s, %s duplex, flow control %sabled\n",
441 speed,
442 duplex ? "full" : "half",
443 fc ? "en" : "dis");
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000444 netif_carrier_on(dev);
445 }
446 }
447}
448
Andrew Lunn54d792f2015-05-06 01:09:47 +0200449static bool mv88e6xxx_6065_family(struct dsa_switch *ds)
450{
451 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
452
453 switch (ps->id) {
454 case PORT_SWITCH_ID_6031:
455 case PORT_SWITCH_ID_6061:
456 case PORT_SWITCH_ID_6035:
457 case PORT_SWITCH_ID_6065:
458 return true;
459 }
460 return false;
461}
462
463static bool mv88e6xxx_6095_family(struct dsa_switch *ds)
464{
465 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
466
467 switch (ps->id) {
468 case PORT_SWITCH_ID_6092:
469 case PORT_SWITCH_ID_6095:
470 return true;
471 }
472 return false;
473}
474
475static bool mv88e6xxx_6097_family(struct dsa_switch *ds)
476{
477 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
478
479 switch (ps->id) {
480 case PORT_SWITCH_ID_6046:
481 case PORT_SWITCH_ID_6085:
482 case PORT_SWITCH_ID_6096:
483 case PORT_SWITCH_ID_6097:
484 return true;
485 }
486 return false;
487}
488
489static bool mv88e6xxx_6165_family(struct dsa_switch *ds)
490{
491 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
492
493 switch (ps->id) {
494 case PORT_SWITCH_ID_6123:
495 case PORT_SWITCH_ID_6161:
496 case PORT_SWITCH_ID_6165:
497 return true;
498 }
499 return false;
500}
501
502static bool mv88e6xxx_6185_family(struct dsa_switch *ds)
503{
504 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
505
506 switch (ps->id) {
507 case PORT_SWITCH_ID_6121:
508 case PORT_SWITCH_ID_6122:
509 case PORT_SWITCH_ID_6152:
510 case PORT_SWITCH_ID_6155:
511 case PORT_SWITCH_ID_6182:
512 case PORT_SWITCH_ID_6185:
513 case PORT_SWITCH_ID_6108:
514 case PORT_SWITCH_ID_6131:
515 return true;
516 }
517 return false;
518}
519
520static bool mv88e6xxx_6351_family(struct dsa_switch *ds)
521{
522 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
523
524 switch (ps->id) {
525 case PORT_SWITCH_ID_6171:
526 case PORT_SWITCH_ID_6175:
527 case PORT_SWITCH_ID_6350:
528 case PORT_SWITCH_ID_6351:
529 return true;
530 }
531 return false;
532}
533
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200534static bool mv88e6xxx_6352_family(struct dsa_switch *ds)
535{
536 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
537
538 switch (ps->id) {
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200539 case PORT_SWITCH_ID_6172:
540 case PORT_SWITCH_ID_6176:
Andrew Lunn54d792f2015-05-06 01:09:47 +0200541 case PORT_SWITCH_ID_6240:
542 case PORT_SWITCH_ID_6352:
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200543 return true;
544 }
545 return false;
546}
547
Andrew Lunn31888232015-05-06 01:09:54 +0200548/* Must be called with SMI mutex held */
549static int _mv88e6xxx_stats_wait(struct dsa_switch *ds)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000550{
551 int ret;
552 int i;
553
554 for (i = 0; i < 10; i++) {
Andrew Lunn31888232015-05-06 01:09:54 +0200555 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATS_OP);
Andrew Lunncca8b132015-04-02 04:06:39 +0200556 if ((ret & GLOBAL_STATS_OP_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000557 return 0;
558 }
559
560 return -ETIMEDOUT;
561}
562
Andrew Lunn31888232015-05-06 01:09:54 +0200563/* Must be called with SMI mutex held */
564static int _mv88e6xxx_stats_snapshot(struct dsa_switch *ds, int port)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000565{
566 int ret;
567
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200568 if (mv88e6xxx_6352_family(ds))
569 port = (port + 1) << 5;
570
Barry Grussling3675c8d2013-01-08 16:05:53 +0000571 /* Snapshot the hardware statistics counters for this port. */
Andrew Lunn31888232015-05-06 01:09:54 +0200572 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_STATS_OP,
573 GLOBAL_STATS_OP_CAPTURE_PORT |
574 GLOBAL_STATS_OP_HIST_RX_TX | port);
575 if (ret < 0)
576 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000577
Barry Grussling3675c8d2013-01-08 16:05:53 +0000578 /* Wait for the snapshotting to complete. */
Andrew Lunn31888232015-05-06 01:09:54 +0200579 ret = _mv88e6xxx_stats_wait(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000580 if (ret < 0)
581 return ret;
582
583 return 0;
584}
585
Andrew Lunn31888232015-05-06 01:09:54 +0200586/* Must be called with SMI mutex held */
587static void _mv88e6xxx_stats_read(struct dsa_switch *ds, int stat, u32 *val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000588{
589 u32 _val;
590 int ret;
591
592 *val = 0;
593
Andrew Lunn31888232015-05-06 01:09:54 +0200594 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_STATS_OP,
595 GLOBAL_STATS_OP_READ_CAPTURED |
596 GLOBAL_STATS_OP_HIST_RX_TX | stat);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000597 if (ret < 0)
598 return;
599
Andrew Lunn31888232015-05-06 01:09:54 +0200600 ret = _mv88e6xxx_stats_wait(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000601 if (ret < 0)
602 return;
603
Andrew Lunn31888232015-05-06 01:09:54 +0200604 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATS_COUNTER_32);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000605 if (ret < 0)
606 return;
607
608 _val = ret << 16;
609
Andrew Lunn31888232015-05-06 01:09:54 +0200610 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATS_COUNTER_01);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000611 if (ret < 0)
612 return;
613
614 *val = _val | ret;
615}
616
Andrew Lunne413e7e2015-04-02 04:06:38 +0200617static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
618 { "in_good_octets", 8, 0x00, },
619 { "in_bad_octets", 4, 0x02, },
620 { "in_unicast", 4, 0x04, },
621 { "in_broadcasts", 4, 0x06, },
622 { "in_multicasts", 4, 0x07, },
623 { "in_pause", 4, 0x16, },
624 { "in_undersize", 4, 0x18, },
625 { "in_fragments", 4, 0x19, },
626 { "in_oversize", 4, 0x1a, },
627 { "in_jabber", 4, 0x1b, },
628 { "in_rx_error", 4, 0x1c, },
629 { "in_fcs_error", 4, 0x1d, },
630 { "out_octets", 8, 0x0e, },
631 { "out_unicast", 4, 0x10, },
632 { "out_broadcasts", 4, 0x13, },
633 { "out_multicasts", 4, 0x12, },
634 { "out_pause", 4, 0x15, },
635 { "excessive", 4, 0x11, },
636 { "collisions", 4, 0x1e, },
637 { "deferred", 4, 0x05, },
638 { "single", 4, 0x14, },
639 { "multiple", 4, 0x17, },
640 { "out_fcs_error", 4, 0x03, },
641 { "late", 4, 0x1f, },
642 { "hist_64bytes", 4, 0x08, },
643 { "hist_65_127bytes", 4, 0x09, },
644 { "hist_128_255bytes", 4, 0x0a, },
645 { "hist_256_511bytes", 4, 0x0b, },
646 { "hist_512_1023bytes", 4, 0x0c, },
647 { "hist_1024_max_bytes", 4, 0x0d, },
648 /* Not all devices have the following counters */
649 { "sw_in_discards", 4, 0x110, },
650 { "sw_in_filtered", 2, 0x112, },
651 { "sw_out_filtered", 2, 0x113, },
652
653};
654
655static bool have_sw_in_discards(struct dsa_switch *ds)
656{
657 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
658
659 switch (ps->id) {
Andrew Lunncca8b132015-04-02 04:06:39 +0200660 case PORT_SWITCH_ID_6095: case PORT_SWITCH_ID_6161:
661 case PORT_SWITCH_ID_6165: case PORT_SWITCH_ID_6171:
662 case PORT_SWITCH_ID_6172: case PORT_SWITCH_ID_6176:
663 case PORT_SWITCH_ID_6182: case PORT_SWITCH_ID_6185:
664 case PORT_SWITCH_ID_6352:
Andrew Lunne413e7e2015-04-02 04:06:38 +0200665 return true;
666 default:
667 return false;
668 }
669}
670
671static void _mv88e6xxx_get_strings(struct dsa_switch *ds,
672 int nr_stats,
673 struct mv88e6xxx_hw_stat *stats,
674 int port, uint8_t *data)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000675{
676 int i;
677
678 for (i = 0; i < nr_stats; i++) {
679 memcpy(data + i * ETH_GSTRING_LEN,
680 stats[i].string, ETH_GSTRING_LEN);
681 }
682}
683
Andrew Lunne413e7e2015-04-02 04:06:38 +0200684static void _mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds,
685 int nr_stats,
686 struct mv88e6xxx_hw_stat *stats,
687 int port, uint64_t *data)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000688{
Florian Fainellia22adce2014-04-28 11:14:28 -0700689 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000690 int ret;
691 int i;
692
Andrew Lunn31888232015-05-06 01:09:54 +0200693 mutex_lock(&ps->smi_mutex);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000694
Andrew Lunn31888232015-05-06 01:09:54 +0200695 ret = _mv88e6xxx_stats_snapshot(ds, port);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000696 if (ret < 0) {
Andrew Lunn31888232015-05-06 01:09:54 +0200697 mutex_unlock(&ps->smi_mutex);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000698 return;
699 }
700
Barry Grussling3675c8d2013-01-08 16:05:53 +0000701 /* Read each of the counters. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000702 for (i = 0; i < nr_stats; i++) {
703 struct mv88e6xxx_hw_stat *s = stats + i;
704 u32 low;
Guenter Roeck17ee3e02014-10-29 10:45:07 -0700705 u32 high = 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000706
Guenter Roeck17ee3e02014-10-29 10:45:07 -0700707 if (s->reg >= 0x100) {
Andrew Lunn80edb722015-06-05 01:10:09 +0200708 ret = _mv88e6xxx_reg_read(ds, REG_PORT(port),
709 s->reg - 0x100);
Guenter Roeck17ee3e02014-10-29 10:45:07 -0700710 if (ret < 0)
711 goto error;
712 low = ret;
713 if (s->sizeof_stat == 4) {
Andrew Lunn31888232015-05-06 01:09:54 +0200714 ret = _mv88e6xxx_reg_read(ds, REG_PORT(port),
715 s->reg - 0x100 + 1);
Guenter Roeck17ee3e02014-10-29 10:45:07 -0700716 if (ret < 0)
717 goto error;
718 high = ret;
719 }
720 data[i] = (((u64)high) << 16) | low;
721 continue;
722 }
Andrew Lunn31888232015-05-06 01:09:54 +0200723 _mv88e6xxx_stats_read(ds, s->reg, &low);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000724 if (s->sizeof_stat == 8)
Andrew Lunn31888232015-05-06 01:09:54 +0200725 _mv88e6xxx_stats_read(ds, s->reg + 1, &high);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000726
727 data[i] = (((u64)high) << 32) | low;
728 }
Guenter Roeck17ee3e02014-10-29 10:45:07 -0700729error:
Andrew Lunn31888232015-05-06 01:09:54 +0200730 mutex_unlock(&ps->smi_mutex);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000731}
Ben Hutchings98e67302011-11-25 14:36:19 +0000732
Andrew Lunne413e7e2015-04-02 04:06:38 +0200733/* All the statistics in the table */
734void
735mv88e6xxx_get_strings(struct dsa_switch *ds, int port, uint8_t *data)
736{
737 if (have_sw_in_discards(ds))
738 _mv88e6xxx_get_strings(ds, ARRAY_SIZE(mv88e6xxx_hw_stats),
739 mv88e6xxx_hw_stats, port, data);
740 else
741 _mv88e6xxx_get_strings(ds, ARRAY_SIZE(mv88e6xxx_hw_stats) - 3,
742 mv88e6xxx_hw_stats, port, data);
743}
744
745int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
746{
747 if (have_sw_in_discards(ds))
748 return ARRAY_SIZE(mv88e6xxx_hw_stats);
749 return ARRAY_SIZE(mv88e6xxx_hw_stats) - 3;
750}
751
752void
753mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds,
754 int port, uint64_t *data)
755{
756 if (have_sw_in_discards(ds))
757 _mv88e6xxx_get_ethtool_stats(
758 ds, ARRAY_SIZE(mv88e6xxx_hw_stats),
759 mv88e6xxx_hw_stats, port, data);
760 else
761 _mv88e6xxx_get_ethtool_stats(
762 ds, ARRAY_SIZE(mv88e6xxx_hw_stats) - 3,
763 mv88e6xxx_hw_stats, port, data);
764}
765
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700766int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
767{
768 return 32 * sizeof(u16);
769}
770
771void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
772 struct ethtool_regs *regs, void *_p)
773{
774 u16 *p = _p;
775 int i;
776
777 regs->version = 0;
778
779 memset(p, 0xff, 32 * sizeof(u16));
780
781 for (i = 0; i < 32; i++) {
782 int ret;
783
784 ret = mv88e6xxx_reg_read(ds, REG_PORT(port), i);
785 if (ret >= 0)
786 p[i] = ret;
787 }
788}
789
Andrew Lunneaa23762014-11-15 22:24:51 +0100790#ifdef CONFIG_NET_DSA_HWMON
791
792int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
793{
794 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
795 int ret;
796 int val;
797
798 *temp = 0;
799
Andrew Lunn3898c142015-05-06 01:09:53 +0200800 mutex_lock(&ps->smi_mutex);
Andrew Lunneaa23762014-11-15 22:24:51 +0100801
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +0200802 ret = _mv88e6xxx_phy_write(ds, 0x0, 0x16, 0x6);
Andrew Lunneaa23762014-11-15 22:24:51 +0100803 if (ret < 0)
804 goto error;
805
806 /* Enable temperature sensor */
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +0200807 ret = _mv88e6xxx_phy_read(ds, 0x0, 0x1a);
Andrew Lunneaa23762014-11-15 22:24:51 +0100808 if (ret < 0)
809 goto error;
810
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +0200811 ret = _mv88e6xxx_phy_write(ds, 0x0, 0x1a, ret | (1 << 5));
Andrew Lunneaa23762014-11-15 22:24:51 +0100812 if (ret < 0)
813 goto error;
814
815 /* Wait for temperature to stabilize */
816 usleep_range(10000, 12000);
817
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +0200818 val = _mv88e6xxx_phy_read(ds, 0x0, 0x1a);
Andrew Lunneaa23762014-11-15 22:24:51 +0100819 if (val < 0) {
820 ret = val;
821 goto error;
822 }
823
824 /* Disable temperature sensor */
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +0200825 ret = _mv88e6xxx_phy_write(ds, 0x0, 0x1a, ret & ~(1 << 5));
Andrew Lunneaa23762014-11-15 22:24:51 +0100826 if (ret < 0)
827 goto error;
828
829 *temp = ((val & 0x1f) - 5) * 5;
830
831error:
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +0200832 _mv88e6xxx_phy_write(ds, 0x0, 0x16, 0x0);
Andrew Lunn3898c142015-05-06 01:09:53 +0200833 mutex_unlock(&ps->smi_mutex);
Andrew Lunneaa23762014-11-15 22:24:51 +0100834 return ret;
835}
836#endif /* CONFIG_NET_DSA_HWMON */
837
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700838/* Must be called with SMI lock held */
Andrew Lunn3898c142015-05-06 01:09:53 +0200839static int _mv88e6xxx_wait(struct dsa_switch *ds, int reg, int offset,
840 u16 mask)
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700841{
842 unsigned long timeout = jiffies + HZ / 10;
843
844 while (time_before(jiffies, timeout)) {
845 int ret;
846
847 ret = _mv88e6xxx_reg_read(ds, reg, offset);
848 if (ret < 0)
849 return ret;
850 if (!(ret & mask))
851 return 0;
852
853 usleep_range(1000, 2000);
854 }
855 return -ETIMEDOUT;
856}
857
Andrew Lunn3898c142015-05-06 01:09:53 +0200858static int mv88e6xxx_wait(struct dsa_switch *ds, int reg, int offset, u16 mask)
859{
860 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
861 int ret;
862
863 mutex_lock(&ps->smi_mutex);
864 ret = _mv88e6xxx_wait(ds, reg, offset, mask);
865 mutex_unlock(&ps->smi_mutex);
866
867 return ret;
868}
869
870static int _mv88e6xxx_phy_wait(struct dsa_switch *ds)
871{
872 return _mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_SMI_OP,
873 GLOBAL2_SMI_OP_BUSY);
874}
875
876int mv88e6xxx_eeprom_load_wait(struct dsa_switch *ds)
877{
878 return mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
879 GLOBAL2_EEPROM_OP_LOAD);
880}
881
882int mv88e6xxx_eeprom_busy_wait(struct dsa_switch *ds)
883{
884 return mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
885 GLOBAL2_EEPROM_OP_BUSY);
886}
887
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700888/* Must be called with SMI lock held */
889static int _mv88e6xxx_atu_wait(struct dsa_switch *ds)
890{
Andrew Lunncca8b132015-04-02 04:06:39 +0200891 return _mv88e6xxx_wait(ds, REG_GLOBAL, GLOBAL_ATU_OP,
892 GLOBAL_ATU_OP_BUSY);
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700893}
894
Andrew Lunn3898c142015-05-06 01:09:53 +0200895/* Must be called with SMI mutex held */
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +0200896static int _mv88e6xxx_phy_read_indirect(struct dsa_switch *ds, int addr,
897 int regnum)
Andrew Lunnf3044682015-02-14 19:17:50 +0100898{
899 int ret;
900
Andrew Lunn3898c142015-05-06 01:09:53 +0200901 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_SMI_OP,
902 GLOBAL2_SMI_OP_22_READ | (addr << 5) |
903 regnum);
Andrew Lunnf3044682015-02-14 19:17:50 +0100904 if (ret < 0)
905 return ret;
906
Andrew Lunn3898c142015-05-06 01:09:53 +0200907 ret = _mv88e6xxx_phy_wait(ds);
908 if (ret < 0)
909 return ret;
910
911 return _mv88e6xxx_reg_read(ds, REG_GLOBAL2, GLOBAL2_SMI_DATA);
Andrew Lunnf3044682015-02-14 19:17:50 +0100912}
913
Andrew Lunn3898c142015-05-06 01:09:53 +0200914/* Must be called with SMI mutex held */
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +0200915static int _mv88e6xxx_phy_write_indirect(struct dsa_switch *ds, int addr,
916 int regnum, u16 val)
Andrew Lunnf3044682015-02-14 19:17:50 +0100917{
Andrew Lunn3898c142015-05-06 01:09:53 +0200918 int ret;
Andrew Lunnf3044682015-02-14 19:17:50 +0100919
Andrew Lunn3898c142015-05-06 01:09:53 +0200920 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_SMI_DATA, val);
921 if (ret < 0)
922 return ret;
923
924 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_SMI_OP,
925 GLOBAL2_SMI_OP_22_WRITE | (addr << 5) |
926 regnum);
927
928 return _mv88e6xxx_phy_wait(ds);
Andrew Lunnf3044682015-02-14 19:17:50 +0100929}
930
Guenter Roeck11b3b452015-03-06 22:23:51 -0800931int mv88e6xxx_get_eee(struct dsa_switch *ds, int port, struct ethtool_eee *e)
932{
Andrew Lunn2f40c692015-04-02 04:06:37 +0200933 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Guenter Roeck11b3b452015-03-06 22:23:51 -0800934 int reg;
935
Andrew Lunn3898c142015-05-06 01:09:53 +0200936 mutex_lock(&ps->smi_mutex);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200937
938 reg = _mv88e6xxx_phy_read_indirect(ds, port, 16);
Guenter Roeck11b3b452015-03-06 22:23:51 -0800939 if (reg < 0)
Andrew Lunn2f40c692015-04-02 04:06:37 +0200940 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800941
942 e->eee_enabled = !!(reg & 0x0200);
943 e->tx_lpi_enabled = !!(reg & 0x0100);
944
Andrew Lunn3898c142015-05-06 01:09:53 +0200945 reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_STATUS);
Guenter Roeck11b3b452015-03-06 22:23:51 -0800946 if (reg < 0)
Andrew Lunn2f40c692015-04-02 04:06:37 +0200947 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800948
Andrew Lunncca8b132015-04-02 04:06:39 +0200949 e->eee_active = !!(reg & PORT_STATUS_EEE);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200950 reg = 0;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800951
Andrew Lunn2f40c692015-04-02 04:06:37 +0200952out:
Andrew Lunn3898c142015-05-06 01:09:53 +0200953 mutex_unlock(&ps->smi_mutex);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200954 return reg;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800955}
956
957int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
958 struct phy_device *phydev, struct ethtool_eee *e)
959{
Andrew Lunn2f40c692015-04-02 04:06:37 +0200960 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
961 int reg;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800962 int ret;
963
Andrew Lunn3898c142015-05-06 01:09:53 +0200964 mutex_lock(&ps->smi_mutex);
Guenter Roeck11b3b452015-03-06 22:23:51 -0800965
Andrew Lunn2f40c692015-04-02 04:06:37 +0200966 ret = _mv88e6xxx_phy_read_indirect(ds, port, 16);
967 if (ret < 0)
968 goto out;
969
970 reg = ret & ~0x0300;
971 if (e->eee_enabled)
972 reg |= 0x0200;
973 if (e->tx_lpi_enabled)
974 reg |= 0x0100;
975
976 ret = _mv88e6xxx_phy_write_indirect(ds, port, 16, reg);
977out:
Andrew Lunn3898c142015-05-06 01:09:53 +0200978 mutex_unlock(&ps->smi_mutex);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200979
980 return ret;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800981}
982
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700983static int _mv88e6xxx_atu_cmd(struct dsa_switch *ds, int fid, u16 cmd)
984{
985 int ret;
986
987 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, 0x01, fid);
988 if (ret < 0)
989 return ret;
990
Andrew Lunncca8b132015-04-02 04:06:39 +0200991 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_OP, cmd);
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700992 if (ret < 0)
993 return ret;
994
995 return _mv88e6xxx_atu_wait(ds);
996}
997
998static int _mv88e6xxx_flush_fid(struct dsa_switch *ds, int fid)
999{
1000 int ret;
1001
1002 ret = _mv88e6xxx_atu_wait(ds);
1003 if (ret < 0)
1004 return ret;
1005
Andrew Lunncca8b132015-04-02 04:06:39 +02001006 return _mv88e6xxx_atu_cmd(ds, fid, GLOBAL_ATU_OP_FLUSH_NON_STATIC_DB);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001007}
1008
1009static int mv88e6xxx_set_port_state(struct dsa_switch *ds, int port, u8 state)
1010{
1011 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Geert Uytterhoevenc3ffe6d2015-04-16 20:49:14 +02001012 int reg, ret = 0;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001013 u8 oldstate;
1014
1015 mutex_lock(&ps->smi_mutex);
1016
Andrew Lunncca8b132015-04-02 04:06:39 +02001017 reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_CONTROL);
Guenter Roeck538cc282015-04-15 22:12:42 -07001018 if (reg < 0) {
1019 ret = reg;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001020 goto abort;
Guenter Roeck538cc282015-04-15 22:12:42 -07001021 }
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001022
Andrew Lunncca8b132015-04-02 04:06:39 +02001023 oldstate = reg & PORT_CONTROL_STATE_MASK;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001024 if (oldstate != state) {
1025 /* Flush forwarding database if we're moving a port
1026 * from Learning or Forwarding state to Disabled or
1027 * Blocking or Listening state.
1028 */
Andrew Lunncca8b132015-04-02 04:06:39 +02001029 if (oldstate >= PORT_CONTROL_STATE_LEARNING &&
1030 state <= PORT_CONTROL_STATE_BLOCKING) {
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001031 ret = _mv88e6xxx_flush_fid(ds, ps->fid[port]);
1032 if (ret)
1033 goto abort;
1034 }
Andrew Lunncca8b132015-04-02 04:06:39 +02001035 reg = (reg & ~PORT_CONTROL_STATE_MASK) | state;
1036 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_CONTROL,
1037 reg);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001038 }
1039
1040abort:
1041 mutex_unlock(&ps->smi_mutex);
1042 return ret;
1043}
1044
1045/* Must be called with smi lock held */
1046static int _mv88e6xxx_update_port_config(struct dsa_switch *ds, int port)
1047{
1048 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1049 u8 fid = ps->fid[port];
1050 u16 reg = fid << 12;
1051
1052 if (dsa_is_cpu_port(ds, port))
1053 reg |= ds->phys_port_mask;
1054 else
1055 reg |= (ps->bridge_mask[fid] |
1056 (1 << dsa_upstream_port(ds))) & ~(1 << port);
1057
Andrew Lunncca8b132015-04-02 04:06:39 +02001058 return _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_BASE_VLAN, reg);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001059}
1060
1061/* Must be called with smi lock held */
1062static int _mv88e6xxx_update_bridge_config(struct dsa_switch *ds, int fid)
1063{
1064 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1065 int port;
1066 u32 mask;
1067 int ret;
1068
1069 mask = ds->phys_port_mask;
1070 while (mask) {
1071 port = __ffs(mask);
1072 mask &= ~(1 << port);
1073 if (ps->fid[port] != fid)
1074 continue;
1075
1076 ret = _mv88e6xxx_update_port_config(ds, port);
1077 if (ret)
1078 return ret;
1079 }
1080
1081 return _mv88e6xxx_flush_fid(ds, fid);
1082}
1083
1084/* Bridge handling functions */
1085
1086int mv88e6xxx_join_bridge(struct dsa_switch *ds, int port, u32 br_port_mask)
1087{
1088 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1089 int ret = 0;
1090 u32 nmask;
1091 int fid;
1092
1093 /* If the bridge group is not empty, join that group.
1094 * Otherwise create a new group.
1095 */
1096 fid = ps->fid[port];
1097 nmask = br_port_mask & ~(1 << port);
1098 if (nmask)
1099 fid = ps->fid[__ffs(nmask)];
1100
1101 nmask = ps->bridge_mask[fid] | (1 << port);
1102 if (nmask != br_port_mask) {
1103 netdev_err(ds->ports[port],
1104 "join: Bridge port mask mismatch fid=%d mask=0x%x expected 0x%x\n",
1105 fid, br_port_mask, nmask);
1106 return -EINVAL;
1107 }
1108
1109 mutex_lock(&ps->smi_mutex);
1110
1111 ps->bridge_mask[fid] = br_port_mask;
1112
1113 if (fid != ps->fid[port]) {
1114 ps->fid_mask |= 1 << ps->fid[port];
1115 ps->fid[port] = fid;
1116 ret = _mv88e6xxx_update_bridge_config(ds, fid);
1117 }
1118
1119 mutex_unlock(&ps->smi_mutex);
1120
1121 return ret;
1122}
1123
1124int mv88e6xxx_leave_bridge(struct dsa_switch *ds, int port, u32 br_port_mask)
1125{
1126 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1127 u8 fid, newfid;
1128 int ret;
1129
1130 fid = ps->fid[port];
1131
1132 if (ps->bridge_mask[fid] != br_port_mask) {
1133 netdev_err(ds->ports[port],
1134 "leave: Bridge port mask mismatch fid=%d mask=0x%x expected 0x%x\n",
1135 fid, br_port_mask, ps->bridge_mask[fid]);
1136 return -EINVAL;
1137 }
1138
1139 /* If the port was the last port of a bridge, we are done.
1140 * Otherwise assign a new fid to the port, and fix up
1141 * the bridge configuration.
1142 */
1143 if (br_port_mask == (1 << port))
1144 return 0;
1145
1146 mutex_lock(&ps->smi_mutex);
1147
1148 newfid = __ffs(ps->fid_mask);
1149 ps->fid[port] = newfid;
1150 ps->fid_mask &= (1 << newfid);
1151 ps->bridge_mask[fid] &= ~(1 << port);
1152 ps->bridge_mask[newfid] = 1 << port;
1153
1154 ret = _mv88e6xxx_update_bridge_config(ds, fid);
1155 if (!ret)
1156 ret = _mv88e6xxx_update_bridge_config(ds, newfid);
1157
1158 mutex_unlock(&ps->smi_mutex);
1159
1160 return ret;
1161}
1162
1163int mv88e6xxx_port_stp_update(struct dsa_switch *ds, int port, u8 state)
1164{
1165 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1166 int stp_state;
1167
1168 switch (state) {
1169 case BR_STATE_DISABLED:
Andrew Lunncca8b132015-04-02 04:06:39 +02001170 stp_state = PORT_CONTROL_STATE_DISABLED;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001171 break;
1172 case BR_STATE_BLOCKING:
1173 case BR_STATE_LISTENING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001174 stp_state = PORT_CONTROL_STATE_BLOCKING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001175 break;
1176 case BR_STATE_LEARNING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001177 stp_state = PORT_CONTROL_STATE_LEARNING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001178 break;
1179 case BR_STATE_FORWARDING:
1180 default:
Andrew Lunncca8b132015-04-02 04:06:39 +02001181 stp_state = PORT_CONTROL_STATE_FORWARDING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001182 break;
1183 }
1184
1185 netdev_dbg(ds->ports[port], "port state %d [%d]\n", state, stp_state);
1186
1187 /* mv88e6xxx_port_stp_update may be called with softirqs disabled,
1188 * so we can not update the port state directly but need to schedule it.
1189 */
1190 ps->port_state[port] = stp_state;
1191 set_bit(port, &ps->port_state_update_mask);
1192 schedule_work(&ps->bridge_work);
1193
1194 return 0;
1195}
1196
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001197static int __mv88e6xxx_write_addr(struct dsa_switch *ds,
1198 const unsigned char *addr)
1199{
1200 int i, ret;
1201
1202 for (i = 0; i < 3; i++) {
Andrew Lunncca8b132015-04-02 04:06:39 +02001203 ret = _mv88e6xxx_reg_write(
1204 ds, REG_GLOBAL, GLOBAL_ATU_MAC_01 + i,
1205 (addr[i * 2] << 8) | addr[i * 2 + 1]);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001206 if (ret < 0)
1207 return ret;
1208 }
1209
1210 return 0;
1211}
1212
1213static int __mv88e6xxx_read_addr(struct dsa_switch *ds, unsigned char *addr)
1214{
1215 int i, ret;
1216
1217 for (i = 0; i < 3; i++) {
Andrew Lunncca8b132015-04-02 04:06:39 +02001218 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL,
1219 GLOBAL_ATU_MAC_01 + i);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001220 if (ret < 0)
1221 return ret;
1222 addr[i * 2] = ret >> 8;
1223 addr[i * 2 + 1] = ret & 0xff;
1224 }
1225
1226 return 0;
1227}
1228
1229static int __mv88e6xxx_port_fdb_cmd(struct dsa_switch *ds, int port,
1230 const unsigned char *addr, int state)
1231{
1232 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1233 u8 fid = ps->fid[port];
1234 int ret;
1235
1236 ret = _mv88e6xxx_atu_wait(ds);
1237 if (ret < 0)
1238 return ret;
1239
1240 ret = __mv88e6xxx_write_addr(ds, addr);
1241 if (ret < 0)
1242 return ret;
1243
Andrew Lunncca8b132015-04-02 04:06:39 +02001244 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_DATA,
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001245 (0x10 << port) | state);
1246 if (ret)
1247 return ret;
1248
Andrew Lunncca8b132015-04-02 04:06:39 +02001249 ret = _mv88e6xxx_atu_cmd(ds, fid, GLOBAL_ATU_OP_LOAD_DB);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001250
1251 return ret;
1252}
1253
1254int mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
1255 const unsigned char *addr, u16 vid)
1256{
1257 int state = is_multicast_ether_addr(addr) ?
Andrew Lunncca8b132015-04-02 04:06:39 +02001258 GLOBAL_ATU_DATA_STATE_MC_STATIC :
1259 GLOBAL_ATU_DATA_STATE_UC_STATIC;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001260 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1261 int ret;
1262
1263 mutex_lock(&ps->smi_mutex);
1264 ret = __mv88e6xxx_port_fdb_cmd(ds, port, addr, state);
1265 mutex_unlock(&ps->smi_mutex);
1266
1267 return ret;
1268}
1269
1270int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
1271 const unsigned char *addr, u16 vid)
1272{
1273 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1274 int ret;
1275
1276 mutex_lock(&ps->smi_mutex);
Andrew Lunncca8b132015-04-02 04:06:39 +02001277 ret = __mv88e6xxx_port_fdb_cmd(ds, port, addr,
1278 GLOBAL_ATU_DATA_STATE_UNUSED);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001279 mutex_unlock(&ps->smi_mutex);
1280
1281 return ret;
1282}
1283
1284static int __mv88e6xxx_port_getnext(struct dsa_switch *ds, int port,
1285 unsigned char *addr, bool *is_static)
1286{
1287 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1288 u8 fid = ps->fid[port];
1289 int ret, state;
1290
1291 ret = _mv88e6xxx_atu_wait(ds);
1292 if (ret < 0)
1293 return ret;
1294
1295 ret = __mv88e6xxx_write_addr(ds, addr);
1296 if (ret < 0)
1297 return ret;
1298
1299 do {
Andrew Lunncca8b132015-04-02 04:06:39 +02001300 ret = _mv88e6xxx_atu_cmd(ds, fid, GLOBAL_ATU_OP_GET_NEXT_DB);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001301 if (ret < 0)
1302 return ret;
1303
Andrew Lunncca8b132015-04-02 04:06:39 +02001304 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_ATU_DATA);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001305 if (ret < 0)
1306 return ret;
Andrew Lunncca8b132015-04-02 04:06:39 +02001307 state = ret & GLOBAL_ATU_DATA_STATE_MASK;
1308 if (state == GLOBAL_ATU_DATA_STATE_UNUSED)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001309 return -ENOENT;
1310 } while (!(((ret >> 4) & 0xff) & (1 << port)));
1311
1312 ret = __mv88e6xxx_read_addr(ds, addr);
1313 if (ret < 0)
1314 return ret;
1315
1316 *is_static = state == (is_multicast_ether_addr(addr) ?
Andrew Lunncca8b132015-04-02 04:06:39 +02001317 GLOBAL_ATU_DATA_STATE_MC_STATIC :
1318 GLOBAL_ATU_DATA_STATE_UC_STATIC);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001319
1320 return 0;
1321}
1322
1323/* get next entry for port */
1324int mv88e6xxx_port_fdb_getnext(struct dsa_switch *ds, int port,
1325 unsigned char *addr, bool *is_static)
1326{
1327 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1328 int ret;
1329
1330 mutex_lock(&ps->smi_mutex);
1331 ret = __mv88e6xxx_port_getnext(ds, port, addr, is_static);
1332 mutex_unlock(&ps->smi_mutex);
1333
1334 return ret;
1335}
1336
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001337static void mv88e6xxx_bridge_work(struct work_struct *work)
1338{
1339 struct mv88e6xxx_priv_state *ps;
1340 struct dsa_switch *ds;
1341 int port;
1342
1343 ps = container_of(work, struct mv88e6xxx_priv_state, bridge_work);
1344 ds = ((struct dsa_switch *)ps) - 1;
1345
1346 while (ps->port_state_update_mask) {
1347 port = __ffs(ps->port_state_update_mask);
1348 clear_bit(port, &ps->port_state_update_mask);
1349 mv88e6xxx_set_port_state(ds, port, ps->port_state[port]);
1350 }
1351}
1352
Andrew Lunndbde9e62015-05-06 01:09:48 +02001353static int mv88e6xxx_setup_port(struct dsa_switch *ds, int port)
Guenter Roeckd827e882015-03-26 18:36:29 -07001354{
1355 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001356 int ret, fid;
Andrew Lunn54d792f2015-05-06 01:09:47 +02001357 u16 reg;
Guenter Roeckd827e882015-03-26 18:36:29 -07001358
1359 mutex_lock(&ps->smi_mutex);
1360
Andrew Lunn54d792f2015-05-06 01:09:47 +02001361 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
1362 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
1363 mv88e6xxx_6185_family(ds) || mv88e6xxx_6095_family(ds) ||
1364 mv88e6xxx_6065_family(ds)) {
1365 /* MAC Forcing register: don't force link, speed,
1366 * duplex or flow control state to any particular
1367 * values on physical ports, but force the CPU port
1368 * and all DSA ports to their maximum bandwidth and
1369 * full duplex.
1370 */
1371 reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_PCS_CTRL);
1372 if (dsa_is_cpu_port(ds, port) ||
1373 ds->dsa_port_mask & (1 << port)) {
1374 reg |= PORT_PCS_CTRL_FORCE_LINK |
1375 PORT_PCS_CTRL_LINK_UP |
1376 PORT_PCS_CTRL_DUPLEX_FULL |
1377 PORT_PCS_CTRL_FORCE_DUPLEX;
1378 if (mv88e6xxx_6065_family(ds))
1379 reg |= PORT_PCS_CTRL_100;
1380 else
1381 reg |= PORT_PCS_CTRL_1000;
1382 } else {
1383 reg |= PORT_PCS_CTRL_UNFORCED;
1384 }
1385
1386 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
1387 PORT_PCS_CTRL, reg);
1388 if (ret)
1389 goto abort;
1390 }
1391
1392 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
1393 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
1394 * tunneling, determine priority by looking at 802.1p and IP
1395 * priority fields (IP prio has precedence), and set STP state
1396 * to Forwarding.
1397 *
1398 * If this is the CPU link, use DSA or EDSA tagging depending
1399 * on which tagging mode was configured.
1400 *
1401 * If this is a link to another switch, use DSA tagging mode.
1402 *
1403 * If this is the upstream port for this switch, enable
1404 * forwarding of unknown unicasts and multicasts.
1405 */
1406 reg = 0;
1407 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
1408 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
1409 mv88e6xxx_6095_family(ds) || mv88e6xxx_6065_family(ds) ||
1410 mv88e6xxx_6185_family(ds))
1411 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
1412 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
1413 PORT_CONTROL_STATE_FORWARDING;
1414 if (dsa_is_cpu_port(ds, port)) {
1415 if (mv88e6xxx_6095_family(ds) || mv88e6xxx_6185_family(ds))
1416 reg |= PORT_CONTROL_DSA_TAG;
1417 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
1418 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds)) {
1419 if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA)
1420 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA;
1421 else
1422 reg |= PORT_CONTROL_FRAME_MODE_DSA;
1423 }
1424
1425 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
1426 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
1427 mv88e6xxx_6095_family(ds) || mv88e6xxx_6065_family(ds) ||
1428 mv88e6xxx_6185_family(ds)) {
1429 if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA)
1430 reg |= PORT_CONTROL_EGRESS_ADD_TAG;
1431 }
1432 }
1433 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
1434 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
1435 mv88e6xxx_6095_family(ds) || mv88e6xxx_6065_family(ds)) {
1436 if (ds->dsa_port_mask & (1 << port))
1437 reg |= PORT_CONTROL_FRAME_MODE_DSA;
1438 if (port == dsa_upstream_port(ds))
1439 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
1440 PORT_CONTROL_FORWARD_UNKNOWN_MC;
1441 }
1442 if (reg) {
1443 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
1444 PORT_CONTROL, reg);
1445 if (ret)
1446 goto abort;
1447 }
1448
1449 /* Port Control 2: don't force a good FCS, set the maximum
1450 * frame size to 10240 bytes, don't let the switch add or
1451 * strip 802.1q tags, don't discard tagged or untagged frames
1452 * on this port, do a destination address lookup on all
1453 * received packets as usual, disable ARP mirroring and don't
1454 * send a copy of all transmitted/received frames on this port
1455 * to the CPU.
1456 */
1457 reg = 0;
1458 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
1459 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
1460 mv88e6xxx_6095_family(ds))
1461 reg = PORT_CONTROL_2_MAP_DA;
1462
1463 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
1464 mv88e6xxx_6165_family(ds))
1465 reg |= PORT_CONTROL_2_JUMBO_10240;
1466
1467 if (mv88e6xxx_6095_family(ds) || mv88e6xxx_6185_family(ds)) {
1468 /* Set the upstream port this port should use */
1469 reg |= dsa_upstream_port(ds);
1470 /* enable forwarding of unknown multicast addresses to
1471 * the upstream port
1472 */
1473 if (port == dsa_upstream_port(ds))
1474 reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
1475 }
1476
1477 if (reg) {
1478 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
1479 PORT_CONTROL_2, reg);
1480 if (ret)
1481 goto abort;
1482 }
1483
1484 /* Port Association Vector: when learning source addresses
1485 * of packets, add the address to the address database using
1486 * a port bitmap that has only the bit for this port set and
1487 * the other bits clear.
1488 */
1489 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_ASSOC_VECTOR,
1490 1 << port);
1491 if (ret)
1492 goto abort;
1493
1494 /* Egress rate control 2: disable egress rate control. */
1495 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_RATE_CONTROL_2,
1496 0x0000);
1497 if (ret)
1498 goto abort;
1499
1500 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
1501 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds)) {
1502 /* Do not limit the period of time that this port can
1503 * be paused for by the remote end or the period of
1504 * time that this port can pause the remote end.
1505 */
1506 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
1507 PORT_PAUSE_CTRL, 0x0000);
1508 if (ret)
1509 goto abort;
1510
1511 /* Port ATU control: disable limiting the number of
1512 * address database entries that this port is allowed
1513 * to use.
1514 */
1515 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
1516 PORT_ATU_CONTROL, 0x0000);
1517 /* Priority Override: disable DA, SA and VTU priority
1518 * override.
1519 */
1520 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
1521 PORT_PRI_OVERRIDE, 0x0000);
1522 if (ret)
1523 goto abort;
1524
1525 /* Port Ethertype: use the Ethertype DSA Ethertype
1526 * value.
1527 */
1528 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
1529 PORT_ETH_TYPE, ETH_P_EDSA);
1530 if (ret)
1531 goto abort;
1532 /* Tag Remap: use an identity 802.1p prio -> switch
1533 * prio mapping.
1534 */
1535 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
1536 PORT_TAG_REGMAP_0123, 0x3210);
1537 if (ret)
1538 goto abort;
1539
1540 /* Tag Remap 2: use an identity 802.1p prio -> switch
1541 * prio mapping.
1542 */
1543 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
1544 PORT_TAG_REGMAP_4567, 0x7654);
1545 if (ret)
1546 goto abort;
1547 }
1548
1549 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
1550 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
1551 mv88e6xxx_6185_family(ds) || mv88e6xxx_6095_family(ds)) {
1552 /* Rate Control: disable ingress rate limiting. */
1553 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
1554 PORT_RATE_CONTROL, 0x0001);
1555 if (ret)
1556 goto abort;
1557 }
1558
Guenter Roeck366f0a02015-03-26 18:36:30 -07001559 /* Port Control 1: disable trunking, disable sending
1560 * learning messages to this port.
Guenter Roeckd827e882015-03-26 18:36:29 -07001561 */
Vivien Didelot614f03f2015-04-20 17:19:23 -04001562 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_CONTROL_1, 0x0000);
Guenter Roeckd827e882015-03-26 18:36:29 -07001563 if (ret)
1564 goto abort;
1565
1566 /* Port based VLAN map: give each port its own address
1567 * database, allow the CPU port to talk to each of the 'real'
1568 * ports, and allow each of the 'real' ports to only talk to
1569 * the upstream port.
1570 */
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001571 fid = __ffs(ps->fid_mask);
1572 ps->fid[port] = fid;
1573 ps->fid_mask &= ~(1 << fid);
Guenter Roeckd827e882015-03-26 18:36:29 -07001574
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001575 if (!dsa_is_cpu_port(ds, port))
1576 ps->bridge_mask[fid] = 1 << port;
1577
1578 ret = _mv88e6xxx_update_port_config(ds, port);
Guenter Roeckd827e882015-03-26 18:36:29 -07001579 if (ret)
1580 goto abort;
1581
1582 /* Default VLAN ID and priority: don't set a default VLAN
1583 * ID, and set the default packet priority to zero.
1584 */
Vivien Didelot47cf1e652015-04-20 17:43:26 -04001585 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_DEFAULT_VLAN,
1586 0x0000);
Guenter Roeckd827e882015-03-26 18:36:29 -07001587abort:
1588 mutex_unlock(&ps->smi_mutex);
1589 return ret;
1590}
1591
Andrew Lunndbde9e62015-05-06 01:09:48 +02001592int mv88e6xxx_setup_ports(struct dsa_switch *ds)
1593{
1594 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1595 int ret;
1596 int i;
1597
1598 for (i = 0; i < ps->num_ports; i++) {
1599 ret = mv88e6xxx_setup_port(ds, i);
1600 if (ret < 0)
1601 return ret;
1602 }
1603 return 0;
1604}
1605
Andrew Lunn87c8cef2015-06-20 18:42:28 +02001606static int mv88e6xxx_regs_show(struct seq_file *s, void *p)
1607{
1608 struct dsa_switch *ds = s->private;
1609
1610 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1611 int reg, port;
1612
1613 seq_puts(s, " GLOBAL GLOBAL2 ");
1614 for (port = 0 ; port < ps->num_ports; port++)
1615 seq_printf(s, " %2d ", port);
1616 seq_puts(s, "\n");
1617
1618 for (reg = 0; reg < 32; reg++) {
1619 seq_printf(s, "%2x: ", reg);
1620 seq_printf(s, " %4x %4x ",
1621 mv88e6xxx_reg_read(ds, REG_GLOBAL, reg),
1622 mv88e6xxx_reg_read(ds, REG_GLOBAL2, reg));
1623
1624 for (port = 0 ; port < ps->num_ports; port++)
1625 seq_printf(s, "%4x ",
1626 mv88e6xxx_reg_read(ds, REG_PORT(port), reg));
1627 seq_puts(s, "\n");
1628 }
1629
1630 return 0;
1631}
1632
1633static int mv88e6xxx_regs_open(struct inode *inode, struct file *file)
1634{
1635 return single_open(file, mv88e6xxx_regs_show, inode->i_private);
1636}
1637
1638static const struct file_operations mv88e6xxx_regs_fops = {
1639 .open = mv88e6xxx_regs_open,
1640 .read = seq_read,
1641 .llseek = no_llseek,
1642 .release = single_release,
1643 .owner = THIS_MODULE,
1644};
1645
Andrew Lunn8a0a2652015-06-20 18:42:29 +02001646static void mv88e6xxx_atu_show_header(struct seq_file *s)
1647{
1648 seq_puts(s, "DB T/P Vec State Addr\n");
1649}
1650
1651static void mv88e6xxx_atu_show_entry(struct seq_file *s, int dbnum,
1652 unsigned char *addr, int data)
1653{
1654 bool trunk = !!(data & GLOBAL_ATU_DATA_TRUNK);
1655 int portvec = ((data & GLOBAL_ATU_DATA_PORT_VECTOR_MASK) >>
1656 GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT);
1657 int state = data & GLOBAL_ATU_DATA_STATE_MASK;
1658
1659 seq_printf(s, "%03x %5s %10pb %x %pM\n",
1660 dbnum, (trunk ? "Trunk" : "Port"), &portvec, state, addr);
1661}
1662
1663static int mv88e6xxx_atu_show_db(struct seq_file *s, struct dsa_switch *ds,
1664 int dbnum)
1665{
1666 unsigned char bcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1667 unsigned char addr[6];
1668 int ret, data, state;
1669
1670 ret = __mv88e6xxx_write_addr(ds, bcast);
1671 if (ret < 0)
1672 return ret;
1673
1674 do {
1675 ret = _mv88e6xxx_atu_cmd(ds, dbnum, GLOBAL_ATU_OP_GET_NEXT_DB);
1676 if (ret < 0)
1677 return ret;
1678 data = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_ATU_DATA);
1679 if (data < 0)
1680 return data;
1681
1682 state = data & GLOBAL_ATU_DATA_STATE_MASK;
1683 if (state == GLOBAL_ATU_DATA_STATE_UNUSED)
1684 break;
1685 ret = __mv88e6xxx_read_addr(ds, addr);
1686 if (ret < 0)
1687 return ret;
1688 mv88e6xxx_atu_show_entry(s, dbnum, addr, data);
1689 } while (state != GLOBAL_ATU_DATA_STATE_UNUSED);
1690
1691 return 0;
1692}
1693
1694static int mv88e6xxx_atu_show(struct seq_file *s, void *p)
1695{
1696 struct dsa_switch *ds = s->private;
1697 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1698 int dbnum;
1699
1700 mv88e6xxx_atu_show_header(s);
1701
1702 for (dbnum = 0; dbnum < 255; dbnum++) {
1703 mutex_lock(&ps->smi_mutex);
1704 mv88e6xxx_atu_show_db(s, ds, dbnum);
1705 mutex_unlock(&ps->smi_mutex);
1706 }
1707
1708 return 0;
1709}
1710
1711static int mv88e6xxx_atu_open(struct inode *inode, struct file *file)
1712{
1713 return single_open(file, mv88e6xxx_atu_show, inode->i_private);
1714}
1715
1716static const struct file_operations mv88e6xxx_atu_fops = {
1717 .open = mv88e6xxx_atu_open,
1718 .read = seq_read,
1719 .llseek = no_llseek,
1720 .release = single_release,
1721 .owner = THIS_MODULE,
1722};
1723
Guenter Roeckacdaffc2015-03-26 18:36:28 -07001724int mv88e6xxx_setup_common(struct dsa_switch *ds)
1725{
1726 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunn87c8cef2015-06-20 18:42:28 +02001727 char *name;
Guenter Roeckacdaffc2015-03-26 18:36:28 -07001728
1729 mutex_init(&ps->smi_mutex);
Guenter Roeckacdaffc2015-03-26 18:36:28 -07001730
Andrew Lunncca8b132015-04-02 04:06:39 +02001731 ps->id = REG_READ(REG_PORT(0), PORT_SWITCH_ID) & 0xfff0;
Andrew Lunna8f064c2015-03-26 18:36:40 -07001732
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001733 ps->fid_mask = (1 << DSA_MAX_PORTS) - 1;
1734
1735 INIT_WORK(&ps->bridge_work, mv88e6xxx_bridge_work);
1736
Andrew Lunn87c8cef2015-06-20 18:42:28 +02001737 name = kasprintf(GFP_KERNEL, "dsa%d", ds->index);
1738 ps->dbgfs = debugfs_create_dir(name, NULL);
1739 kfree(name);
1740
1741 debugfs_create_file("regs", S_IRUGO, ps->dbgfs, ds,
1742 &mv88e6xxx_regs_fops);
1743
Andrew Lunn8a0a2652015-06-20 18:42:29 +02001744 debugfs_create_file("atu", S_IRUGO, ps->dbgfs, ds,
1745 &mv88e6xxx_atu_fops);
1746
Guenter Roeckacdaffc2015-03-26 18:36:28 -07001747 return 0;
1748}
1749
Andrew Lunn54d792f2015-05-06 01:09:47 +02001750int mv88e6xxx_setup_global(struct dsa_switch *ds)
1751{
1752 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1753 int i;
1754
1755 /* Set the default address aging time to 5 minutes, and
1756 * enable address learn messages to be sent to all message
1757 * ports.
1758 */
1759 REG_WRITE(REG_GLOBAL, GLOBAL_ATU_CONTROL,
1760 0x0140 | GLOBAL_ATU_CONTROL_LEARN2ALL);
1761
1762 /* Configure the IP ToS mapping registers. */
1763 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_0, 0x0000);
1764 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_1, 0x0000);
1765 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_2, 0x5555);
1766 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_3, 0x5555);
1767 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_4, 0xaaaa);
1768 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_5, 0xaaaa);
1769 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_6, 0xffff);
1770 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_7, 0xffff);
1771
1772 /* Configure the IEEE 802.1p priority mapping register. */
1773 REG_WRITE(REG_GLOBAL, GLOBAL_IEEE_PRI, 0xfa41);
1774
1775 /* Send all frames with destination addresses matching
1776 * 01:80:c2:00:00:0x to the CPU port.
1777 */
1778 REG_WRITE(REG_GLOBAL2, GLOBAL2_MGMT_EN_0X, 0xffff);
1779
1780 /* Ignore removed tag data on doubly tagged packets, disable
1781 * flow control messages, force flow control priority to the
1782 * highest, and send all special multicast frames to the CPU
1783 * port at the highest priority.
1784 */
1785 REG_WRITE(REG_GLOBAL2, GLOBAL2_SWITCH_MGMT,
1786 0x7 | GLOBAL2_SWITCH_MGMT_RSVD2CPU | 0x70 |
1787 GLOBAL2_SWITCH_MGMT_FORCE_FLOW_CTRL_PRI);
1788
1789 /* Program the DSA routing table. */
1790 for (i = 0; i < 32; i++) {
1791 int nexthop = 0x1f;
1792
1793 if (ds->pd->rtable &&
1794 i != ds->index && i < ds->dst->pd->nr_chips)
1795 nexthop = ds->pd->rtable[i] & 0x1f;
1796
1797 REG_WRITE(REG_GLOBAL2, GLOBAL2_DEVICE_MAPPING,
1798 GLOBAL2_DEVICE_MAPPING_UPDATE |
1799 (i << GLOBAL2_DEVICE_MAPPING_TARGET_SHIFT) |
1800 nexthop);
1801 }
1802
1803 /* Clear all trunk masks. */
1804 for (i = 0; i < 8; i++)
1805 REG_WRITE(REG_GLOBAL2, GLOBAL2_TRUNK_MASK,
1806 0x8000 | (i << GLOBAL2_TRUNK_MASK_NUM_SHIFT) |
1807 ((1 << ps->num_ports) - 1));
1808
1809 /* Clear all trunk mappings. */
1810 for (i = 0; i < 16; i++)
1811 REG_WRITE(REG_GLOBAL2, GLOBAL2_TRUNK_MAPPING,
1812 GLOBAL2_TRUNK_MAPPING_UPDATE |
1813 (i << GLOBAL2_TRUNK_MAPPING_ID_SHIFT));
1814
1815 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
1816 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds)) {
1817 /* Send all frames with destination addresses matching
1818 * 01:80:c2:00:00:2x to the CPU port.
1819 */
1820 REG_WRITE(REG_GLOBAL2, GLOBAL2_MGMT_EN_2X, 0xffff);
1821
1822 /* Initialise cross-chip port VLAN table to reset
1823 * defaults.
1824 */
1825 REG_WRITE(REG_GLOBAL2, GLOBAL2_PVT_ADDR, 0x9000);
1826
1827 /* Clear the priority override table. */
1828 for (i = 0; i < 16; i++)
1829 REG_WRITE(REG_GLOBAL2, GLOBAL2_PRIO_OVERRIDE,
1830 0x8000 | (i << 8));
1831 }
1832
1833 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
1834 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
1835 mv88e6xxx_6185_family(ds) || mv88e6xxx_6095_family(ds)) {
1836 /* Disable ingress rate limiting by resetting all
1837 * ingress rate limit registers to their initial
1838 * state.
1839 */
1840 for (i = 0; i < ps->num_ports; i++)
1841 REG_WRITE(REG_GLOBAL2, GLOBAL2_INGRESS_OP,
1842 0x9000 | (i << 8));
1843 }
1844
1845 return 0;
1846}
1847
Andrew Lunn143a8302015-04-02 04:06:34 +02001848int mv88e6xxx_switch_reset(struct dsa_switch *ds, bool ppu_active)
1849{
1850 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1851 u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
1852 unsigned long timeout;
1853 int ret;
1854 int i;
1855
1856 /* Set all ports to the disabled state. */
1857 for (i = 0; i < ps->num_ports; i++) {
Andrew Lunncca8b132015-04-02 04:06:39 +02001858 ret = REG_READ(REG_PORT(i), PORT_CONTROL);
1859 REG_WRITE(REG_PORT(i), PORT_CONTROL, ret & 0xfffc);
Andrew Lunn143a8302015-04-02 04:06:34 +02001860 }
1861
1862 /* Wait for transmit queues to drain. */
1863 usleep_range(2000, 4000);
1864
1865 /* Reset the switch. Keep the PPU active if requested. The PPU
1866 * needs to be active to support indirect phy register access
1867 * through global registers 0x18 and 0x19.
1868 */
1869 if (ppu_active)
1870 REG_WRITE(REG_GLOBAL, 0x04, 0xc000);
1871 else
1872 REG_WRITE(REG_GLOBAL, 0x04, 0xc400);
1873
1874 /* Wait up to one second for reset to complete. */
1875 timeout = jiffies + 1 * HZ;
1876 while (time_before(jiffies, timeout)) {
1877 ret = REG_READ(REG_GLOBAL, 0x00);
1878 if ((ret & is_reset) == is_reset)
1879 break;
1880 usleep_range(1000, 2000);
1881 }
1882 if (time_after(jiffies, timeout))
1883 return -ETIMEDOUT;
1884
1885 return 0;
1886}
1887
Andrew Lunn491435852015-04-02 04:06:35 +02001888int mv88e6xxx_phy_page_read(struct dsa_switch *ds, int port, int page, int reg)
1889{
1890 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1891 int ret;
1892
Andrew Lunn3898c142015-05-06 01:09:53 +02001893 mutex_lock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02001894 ret = _mv88e6xxx_phy_write_indirect(ds, port, 0x16, page);
Andrew Lunn491435852015-04-02 04:06:35 +02001895 if (ret < 0)
1896 goto error;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02001897 ret = _mv88e6xxx_phy_read_indirect(ds, port, reg);
Andrew Lunn491435852015-04-02 04:06:35 +02001898error:
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02001899 _mv88e6xxx_phy_write_indirect(ds, port, 0x16, 0x0);
Andrew Lunn3898c142015-05-06 01:09:53 +02001900 mutex_unlock(&ps->smi_mutex);
Andrew Lunn491435852015-04-02 04:06:35 +02001901 return ret;
1902}
1903
1904int mv88e6xxx_phy_page_write(struct dsa_switch *ds, int port, int page,
1905 int reg, int val)
1906{
1907 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1908 int ret;
1909
Andrew Lunn3898c142015-05-06 01:09:53 +02001910 mutex_lock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02001911 ret = _mv88e6xxx_phy_write_indirect(ds, port, 0x16, page);
Andrew Lunn491435852015-04-02 04:06:35 +02001912 if (ret < 0)
1913 goto error;
1914
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02001915 ret = _mv88e6xxx_phy_write_indirect(ds, port, reg, val);
Andrew Lunn491435852015-04-02 04:06:35 +02001916error:
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02001917 _mv88e6xxx_phy_write_indirect(ds, port, 0x16, 0x0);
Andrew Lunn3898c142015-05-06 01:09:53 +02001918 mutex_unlock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02001919 return ret;
1920}
1921
1922static int mv88e6xxx_port_to_phy_addr(struct dsa_switch *ds, int port)
1923{
1924 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1925
1926 if (port >= 0 && port < ps->num_ports)
1927 return port;
1928 return -EINVAL;
1929}
1930
1931int
1932mv88e6xxx_phy_read(struct dsa_switch *ds, int port, int regnum)
1933{
1934 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1935 int addr = mv88e6xxx_port_to_phy_addr(ds, port);
1936 int ret;
1937
1938 if (addr < 0)
1939 return addr;
1940
Andrew Lunn3898c142015-05-06 01:09:53 +02001941 mutex_lock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02001942 ret = _mv88e6xxx_phy_read(ds, addr, regnum);
Andrew Lunn3898c142015-05-06 01:09:53 +02001943 mutex_unlock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02001944 return ret;
1945}
1946
1947int
1948mv88e6xxx_phy_write(struct dsa_switch *ds, int port, int regnum, u16 val)
1949{
1950 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1951 int addr = mv88e6xxx_port_to_phy_addr(ds, port);
1952 int ret;
1953
1954 if (addr < 0)
1955 return addr;
1956
Andrew Lunn3898c142015-05-06 01:09:53 +02001957 mutex_lock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02001958 ret = _mv88e6xxx_phy_write(ds, addr, regnum, val);
Andrew Lunn3898c142015-05-06 01:09:53 +02001959 mutex_unlock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02001960 return ret;
1961}
1962
1963int
1964mv88e6xxx_phy_read_indirect(struct dsa_switch *ds, int port, int regnum)
1965{
1966 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1967 int addr = mv88e6xxx_port_to_phy_addr(ds, port);
1968 int ret;
1969
1970 if (addr < 0)
1971 return addr;
1972
Andrew Lunn3898c142015-05-06 01:09:53 +02001973 mutex_lock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02001974 ret = _mv88e6xxx_phy_read_indirect(ds, addr, regnum);
Andrew Lunn3898c142015-05-06 01:09:53 +02001975 mutex_unlock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02001976 return ret;
1977}
1978
1979int
1980mv88e6xxx_phy_write_indirect(struct dsa_switch *ds, int port, int regnum,
1981 u16 val)
1982{
1983 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1984 int addr = mv88e6xxx_port_to_phy_addr(ds, port);
1985 int ret;
1986
1987 if (addr < 0)
1988 return addr;
1989
Andrew Lunn3898c142015-05-06 01:09:53 +02001990 mutex_lock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02001991 ret = _mv88e6xxx_phy_write_indirect(ds, addr, regnum, val);
Andrew Lunn3898c142015-05-06 01:09:53 +02001992 mutex_unlock(&ps->smi_mutex);
Andrew Lunn491435852015-04-02 04:06:35 +02001993 return ret;
1994}
1995
Ben Hutchings98e67302011-11-25 14:36:19 +00001996static int __init mv88e6xxx_init(void)
1997{
1998#if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
1999 register_switch_driver(&mv88e6131_switch_driver);
2000#endif
2001#if IS_ENABLED(CONFIG_NET_DSA_MV88E6123_61_65)
2002 register_switch_driver(&mv88e6123_61_65_switch_driver);
2003#endif
Guenter Roeck3ad50cc2014-10-29 10:44:56 -07002004#if IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
2005 register_switch_driver(&mv88e6352_switch_driver);
2006#endif
Andrew Lunn42f27252014-09-12 23:58:44 +02002007#if IS_ENABLED(CONFIG_NET_DSA_MV88E6171)
2008 register_switch_driver(&mv88e6171_switch_driver);
2009#endif
Ben Hutchings98e67302011-11-25 14:36:19 +00002010 return 0;
2011}
2012module_init(mv88e6xxx_init);
2013
2014static void __exit mv88e6xxx_cleanup(void)
2015{
Andrew Lunn42f27252014-09-12 23:58:44 +02002016#if IS_ENABLED(CONFIG_NET_DSA_MV88E6171)
2017 unregister_switch_driver(&mv88e6171_switch_driver);
2018#endif
Vivien Didelot4212b542015-05-01 10:43:52 -04002019#if IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
2020 unregister_switch_driver(&mv88e6352_switch_driver);
2021#endif
Ben Hutchings98e67302011-11-25 14:36:19 +00002022#if IS_ENABLED(CONFIG_NET_DSA_MV88E6123_61_65)
2023 unregister_switch_driver(&mv88e6123_61_65_switch_driver);
2024#endif
2025#if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
2026 unregister_switch_driver(&mv88e6131_switch_driver);
2027#endif
2028}
2029module_exit(mv88e6xxx_cleanup);
Ben Hutchings3d825ed2011-11-25 14:37:16 +00002030
2031MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
2032MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
2033MODULE_LICENSE("GPL");