blob: 2073f7b991a65746d5f0c089f53f410ab1c7a5e0 [file] [log] [blame]
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001/*
Vivien Didelot0d3cd4b2016-06-21 12:28:19 -04002 * Marvell 88e6xxx Ethernet switch single-chip support
3 *
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00004 * Copyright (c) 2008 Marvell Semiconductor
5 *
Vivien Didelotb8fee952015-08-13 12:52:19 -04006 * Copyright (c) 2015 CMC Electronics, Inc.
7 * Added support for VLAN Table Unit operations
8 *
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02009 * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch>
10 *
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 */
16
Barry Grussling19b2f972013-01-08 16:05:54 +000017#include <linux/delay.h>
Guenter Roeckdefb05b2015-03-26 18:36:38 -070018#include <linux/etherdevice.h>
Andrew Lunndea87022015-08-31 15:56:47 +020019#include <linux/ethtool.h>
Guenter Roeckfacd95b2015-03-26 18:36:35 -070020#include <linux/if_bridge.h>
Barry Grussling19b2f972013-01-08 16:05:54 +000021#include <linux/jiffies.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000022#include <linux/list.h>
Andrew Lunn14c7b3c2016-05-10 23:27:21 +020023#include <linux/mdio.h>
Paul Gortmaker2bbba272012-01-24 10:41:40 +000024#include <linux/module.h>
Vivien Didelotcaac8542016-06-20 13:14:09 -040025#include <linux/of_device.h>
Andrew Lunnb516d452016-06-04 21:17:06 +020026#include <linux/of_mdio.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000027#include <linux/netdevice.h>
Andrew Lunnc8c1b39a2015-11-20 03:56:24 +010028#include <linux/gpio/consumer.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000029#include <linux/phy.h>
Ben Hutchingsc8f0b862011-11-27 17:06:08 +000030#include <net/dsa.h>
Vivien Didelot1f36faf2015-10-08 11:35:13 -040031#include <net/switchdev.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000032#include "mv88e6xxx.h"
33
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -040034static void assert_reg_lock(struct mv88e6xxx_priv_state *ps)
Vivien Didelot3996a4f2015-10-30 18:56:45 -040035{
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -040036 if (unlikely(!mutex_is_locked(&ps->reg_lock))) {
37 dev_err(ps->dev, "Switch registers lock not held!\n");
Vivien Didelot3996a4f2015-10-30 18:56:45 -040038 dump_stack();
39 }
40}
41
Vivien Didelot914b32f2016-06-20 13:14:11 -040042/* The switch ADDR[4:1] configuration pins define the chip SMI device address
43 * (ADDR[0] is always zero, thus only even SMI addresses can be strapped).
44 *
45 * When ADDR is all zero, the chip uses Single-chip Addressing Mode, assuming it
46 * is the only device connected to the SMI master. In this mode it responds to
47 * all 32 possible SMI addresses, and thus maps directly the internal devices.
48 *
49 * When ADDR is non-zero, the chip uses Multi-chip Addressing Mode, allowing
50 * multiple devices to share the SMI interface. In this mode it responds to only
51 * 2 registers, used to indirectly access the internal SMI devices.
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000052 */
Vivien Didelot914b32f2016-06-20 13:14:11 -040053
54static int mv88e6xxx_smi_read(struct mv88e6xxx_priv_state *ps,
55 int addr, int reg, u16 *val)
56{
57 if (!ps->smi_ops)
58 return -EOPNOTSUPP;
59
60 return ps->smi_ops->read(ps, addr, reg, val);
61}
62
63static int mv88e6xxx_smi_write(struct mv88e6xxx_priv_state *ps,
64 int addr, int reg, u16 val)
65{
66 if (!ps->smi_ops)
67 return -EOPNOTSUPP;
68
69 return ps->smi_ops->write(ps, addr, reg, val);
70}
71
72static int mv88e6xxx_smi_single_chip_read(struct mv88e6xxx_priv_state *ps,
73 int addr, int reg, u16 *val)
74{
75 int ret;
76
77 ret = mdiobus_read_nested(ps->bus, addr, reg);
78 if (ret < 0)
79 return ret;
80
81 *val = ret & 0xffff;
82
83 return 0;
84}
85
86static int mv88e6xxx_smi_single_chip_write(struct mv88e6xxx_priv_state *ps,
87 int addr, int reg, u16 val)
88{
89 int ret;
90
91 ret = mdiobus_write_nested(ps->bus, addr, reg, val);
92 if (ret < 0)
93 return ret;
94
95 return 0;
96}
97
98static const struct mv88e6xxx_ops mv88e6xxx_smi_single_chip_ops = {
99 .read = mv88e6xxx_smi_single_chip_read,
100 .write = mv88e6xxx_smi_single_chip_write,
101};
102
103static int mv88e6xxx_smi_multi_chip_wait(struct mv88e6xxx_priv_state *ps)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000104{
105 int ret;
106 int i;
107
108 for (i = 0; i < 16; i++) {
Vivien Didelot914b32f2016-06-20 13:14:11 -0400109 ret = mdiobus_read_nested(ps->bus, ps->sw_addr, SMI_CMD);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000110 if (ret < 0)
111 return ret;
112
Andrew Lunncca8b132015-04-02 04:06:39 +0200113 if ((ret & SMI_CMD_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000114 return 0;
115 }
116
117 return -ETIMEDOUT;
118}
119
Vivien Didelot914b32f2016-06-20 13:14:11 -0400120static int mv88e6xxx_smi_multi_chip_read(struct mv88e6xxx_priv_state *ps,
121 int addr, int reg, u16 *val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000122{
123 int ret;
124
Barry Grussling3675c8d2013-01-08 16:05:53 +0000125 /* Wait for the bus to become free. */
Vivien Didelot914b32f2016-06-20 13:14:11 -0400126 ret = mv88e6xxx_smi_multi_chip_wait(ps);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000127 if (ret < 0)
128 return ret;
129
Barry Grussling3675c8d2013-01-08 16:05:53 +0000130 /* Transmit the read command. */
Vivien Didelot914b32f2016-06-20 13:14:11 -0400131 ret = mdiobus_write_nested(ps->bus, ps->sw_addr, SMI_CMD,
Neil Armstrong6e899e62015-10-22 10:37:53 +0200132 SMI_CMD_OP_22_READ | (addr << 5) | reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000133 if (ret < 0)
134 return ret;
135
Barry Grussling3675c8d2013-01-08 16:05:53 +0000136 /* Wait for the read command to complete. */
Vivien Didelot914b32f2016-06-20 13:14:11 -0400137 ret = mv88e6xxx_smi_multi_chip_wait(ps);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000138 if (ret < 0)
139 return ret;
140
Barry Grussling3675c8d2013-01-08 16:05:53 +0000141 /* Read the data. */
Vivien Didelot914b32f2016-06-20 13:14:11 -0400142 ret = mdiobus_read_nested(ps->bus, ps->sw_addr, SMI_DATA);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000143 if (ret < 0)
144 return ret;
145
Vivien Didelot914b32f2016-06-20 13:14:11 -0400146 *val = ret & 0xffff;
147
148 return 0;
149}
150
151static int mv88e6xxx_smi_multi_chip_write(struct mv88e6xxx_priv_state *ps,
152 int addr, int reg, u16 val)
153{
154 int ret;
155
156 /* Wait for the bus to become free. */
157 ret = mv88e6xxx_smi_multi_chip_wait(ps);
158 if (ret < 0)
159 return ret;
160
161 /* Transmit the data to write. */
162 ret = mdiobus_write_nested(ps->bus, ps->sw_addr, SMI_DATA, val);
163 if (ret < 0)
164 return ret;
165
166 /* Transmit the write command. */
167 ret = mdiobus_write_nested(ps->bus, ps->sw_addr, SMI_CMD,
168 SMI_CMD_OP_22_WRITE | (addr << 5) | reg);
169 if (ret < 0)
170 return ret;
171
172 /* Wait for the write command to complete. */
173 ret = mv88e6xxx_smi_multi_chip_wait(ps);
174 if (ret < 0)
175 return ret;
176
177 return 0;
178}
179
180static const struct mv88e6xxx_ops mv88e6xxx_smi_multi_chip_ops = {
181 .read = mv88e6xxx_smi_multi_chip_read,
182 .write = mv88e6xxx_smi_multi_chip_write,
183};
184
185static int mv88e6xxx_read(struct mv88e6xxx_priv_state *ps,
186 int addr, int reg, u16 *val)
187{
188 int err;
189
190 assert_reg_lock(ps);
191
192 err = mv88e6xxx_smi_read(ps, addr, reg, val);
193 if (err)
194 return err;
195
196 dev_dbg(ps->dev, "<- addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
197 addr, reg, *val);
198
199 return 0;
200}
201
202static int mv88e6xxx_write(struct mv88e6xxx_priv_state *ps,
203 int addr, int reg, u16 val)
204{
205 int err;
206
207 assert_reg_lock(ps);
208
209 err = mv88e6xxx_smi_write(ps, addr, reg, val);
210 if (err)
211 return err;
212
213 dev_dbg(ps->dev, "-> addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
214 addr, reg, val);
215
216 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000217}
218
Andrew Lunn158bc062016-04-28 21:24:06 -0400219static int _mv88e6xxx_reg_read(struct mv88e6xxx_priv_state *ps,
220 int addr, int reg)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000221{
Vivien Didelot914b32f2016-06-20 13:14:11 -0400222 u16 val;
223 int err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000224
Vivien Didelot914b32f2016-06-20 13:14:11 -0400225 err = mv88e6xxx_read(ps, addr, reg, &val);
226 if (err)
227 return err;
Vivien Didelot3996a4f2015-10-30 18:56:45 -0400228
Vivien Didelot914b32f2016-06-20 13:14:11 -0400229 return val;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000230}
231
Vivien Didelot57d32312016-06-20 13:13:58 -0400232static int mv88e6xxx_reg_read(struct mv88e6xxx_priv_state *ps, int addr,
233 int reg)
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700234{
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700235 int ret;
236
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -0400237 mutex_lock(&ps->reg_lock);
Andrew Lunn158bc062016-04-28 21:24:06 -0400238 ret = _mv88e6xxx_reg_read(ps, addr, reg);
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -0400239 mutex_unlock(&ps->reg_lock);
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700240
241 return ret;
242}
243
Andrew Lunn158bc062016-04-28 21:24:06 -0400244static int _mv88e6xxx_reg_write(struct mv88e6xxx_priv_state *ps, int addr,
245 int reg, u16 val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000246{
Vivien Didelot914b32f2016-06-20 13:14:11 -0400247 return mv88e6xxx_write(ps, addr, reg, val);
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700248}
249
Vivien Didelot57d32312016-06-20 13:13:58 -0400250static int mv88e6xxx_reg_write(struct mv88e6xxx_priv_state *ps, int addr,
251 int reg, u16 val)
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700252{
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700253 int ret;
254
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -0400255 mutex_lock(&ps->reg_lock);
Andrew Lunn158bc062016-04-28 21:24:06 -0400256 ret = _mv88e6xxx_reg_write(ps, addr, reg, val);
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -0400257 mutex_unlock(&ps->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000258
259 return ret;
260}
261
Vivien Didelot1d13a062016-05-09 13:22:43 -0400262static int mv88e6xxx_set_addr_direct(struct dsa_switch *ds, u8 *addr)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000263{
Andrew Lunn158bc062016-04-28 21:24:06 -0400264 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200265 int err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000266
Andrew Lunn158bc062016-04-28 21:24:06 -0400267 err = mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_MAC_01,
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200268 (addr[0] << 8) | addr[1]);
269 if (err)
270 return err;
271
Andrew Lunn158bc062016-04-28 21:24:06 -0400272 err = mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_MAC_23,
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200273 (addr[2] << 8) | addr[3]);
274 if (err)
275 return err;
276
Andrew Lunn158bc062016-04-28 21:24:06 -0400277 return mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_MAC_45,
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200278 (addr[4] << 8) | addr[5]);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000279}
280
Vivien Didelot1d13a062016-05-09 13:22:43 -0400281static int mv88e6xxx_set_addr_indirect(struct dsa_switch *ds, u8 *addr)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000282{
Andrew Lunn158bc062016-04-28 21:24:06 -0400283 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000284 int ret;
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200285 int i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000286
287 for (i = 0; i < 6; i++) {
288 int j;
289
Barry Grussling3675c8d2013-01-08 16:05:53 +0000290 /* Write the MAC address byte. */
Andrew Lunn158bc062016-04-28 21:24:06 -0400291 ret = mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SWITCH_MAC,
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200292 GLOBAL2_SWITCH_MAC_BUSY |
293 (i << 8) | addr[i]);
294 if (ret)
295 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000296
Barry Grussling3675c8d2013-01-08 16:05:53 +0000297 /* Wait for the write to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000298 for (j = 0; j < 16; j++) {
Andrew Lunn158bc062016-04-28 21:24:06 -0400299 ret = mv88e6xxx_reg_read(ps, REG_GLOBAL2,
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200300 GLOBAL2_SWITCH_MAC);
301 if (ret < 0)
302 return ret;
303
Andrew Lunncca8b132015-04-02 04:06:39 +0200304 if ((ret & GLOBAL2_SWITCH_MAC_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000305 break;
306 }
307 if (j == 16)
308 return -ETIMEDOUT;
309 }
310
311 return 0;
312}
313
Vivien Didelot57d32312016-06-20 13:13:58 -0400314static int mv88e6xxx_set_addr(struct dsa_switch *ds, u8 *addr)
Vivien Didelot1d13a062016-05-09 13:22:43 -0400315{
316 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
317
318 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_SWITCH_MAC))
319 return mv88e6xxx_set_addr_indirect(ds, addr);
320 else
321 return mv88e6xxx_set_addr_direct(ds, addr);
322}
323
Andrew Lunn03a4a542016-06-04 21:17:05 +0200324static int mv88e6xxx_mdio_read_direct(struct mv88e6xxx_priv_state *ps,
325 int addr, int regnum)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000326{
327 if (addr >= 0)
Andrew Lunn158bc062016-04-28 21:24:06 -0400328 return _mv88e6xxx_reg_read(ps, addr, regnum);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000329 return 0xffff;
330}
331
Andrew Lunn03a4a542016-06-04 21:17:05 +0200332static int mv88e6xxx_mdio_write_direct(struct mv88e6xxx_priv_state *ps,
333 int addr, int regnum, u16 val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000334{
335 if (addr >= 0)
Andrew Lunn158bc062016-04-28 21:24:06 -0400336 return _mv88e6xxx_reg_write(ps, addr, regnum, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000337 return 0;
338}
339
Andrew Lunn158bc062016-04-28 21:24:06 -0400340static int mv88e6xxx_ppu_disable(struct mv88e6xxx_priv_state *ps)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000341{
342 int ret;
Barry Grussling19b2f972013-01-08 16:05:54 +0000343 unsigned long timeout;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000344
Vivien Didelot8c9983a2016-05-09 13:22:39 -0400345 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_CONTROL);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200346 if (ret < 0)
347 return ret;
348
Vivien Didelot8c9983a2016-05-09 13:22:39 -0400349 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_CONTROL,
350 ret & ~GLOBAL_CONTROL_PPU_ENABLE);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200351 if (ret)
352 return ret;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000353
Barry Grussling19b2f972013-01-08 16:05:54 +0000354 timeout = jiffies + 1 * HZ;
355 while (time_before(jiffies, timeout)) {
Vivien Didelot8c9983a2016-05-09 13:22:39 -0400356 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_STATUS);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200357 if (ret < 0)
358 return ret;
359
Barry Grussling19b2f972013-01-08 16:05:54 +0000360 usleep_range(1000, 2000);
Andrew Lunncca8b132015-04-02 04:06:39 +0200361 if ((ret & GLOBAL_STATUS_PPU_MASK) !=
362 GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000363 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000364 }
365
366 return -ETIMEDOUT;
367}
368
Andrew Lunn158bc062016-04-28 21:24:06 -0400369static int mv88e6xxx_ppu_enable(struct mv88e6xxx_priv_state *ps)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000370{
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200371 int ret, err;
Barry Grussling19b2f972013-01-08 16:05:54 +0000372 unsigned long timeout;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000373
Vivien Didelot762eb672016-06-04 21:16:54 +0200374 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_CONTROL);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200375 if (ret < 0)
376 return ret;
377
Vivien Didelot762eb672016-06-04 21:16:54 +0200378 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_CONTROL,
379 ret | GLOBAL_CONTROL_PPU_ENABLE);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200380 if (err)
381 return err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000382
Barry Grussling19b2f972013-01-08 16:05:54 +0000383 timeout = jiffies + 1 * HZ;
384 while (time_before(jiffies, timeout)) {
Vivien Didelot762eb672016-06-04 21:16:54 +0200385 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_STATUS);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200386 if (ret < 0)
387 return ret;
388
Barry Grussling19b2f972013-01-08 16:05:54 +0000389 usleep_range(1000, 2000);
Andrew Lunncca8b132015-04-02 04:06:39 +0200390 if ((ret & GLOBAL_STATUS_PPU_MASK) ==
391 GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000392 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000393 }
394
395 return -ETIMEDOUT;
396}
397
398static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
399{
400 struct mv88e6xxx_priv_state *ps;
401
402 ps = container_of(ugly, struct mv88e6xxx_priv_state, ppu_work);
Vivien Didelot762eb672016-06-04 21:16:54 +0200403
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -0400404 mutex_lock(&ps->reg_lock);
Vivien Didelot762eb672016-06-04 21:16:54 +0200405
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000406 if (mutex_trylock(&ps->ppu_mutex)) {
Andrew Lunn158bc062016-04-28 21:24:06 -0400407 if (mv88e6xxx_ppu_enable(ps) == 0)
Barry Grussling85686582013-01-08 16:05:56 +0000408 ps->ppu_disabled = 0;
409 mutex_unlock(&ps->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000410 }
Vivien Didelot762eb672016-06-04 21:16:54 +0200411
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -0400412 mutex_unlock(&ps->reg_lock);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000413}
414
415static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
416{
417 struct mv88e6xxx_priv_state *ps = (void *)_ps;
418
419 schedule_work(&ps->ppu_work);
420}
421
Andrew Lunn158bc062016-04-28 21:24:06 -0400422static int mv88e6xxx_ppu_access_get(struct mv88e6xxx_priv_state *ps)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000423{
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000424 int ret;
425
426 mutex_lock(&ps->ppu_mutex);
427
Barry Grussling3675c8d2013-01-08 16:05:53 +0000428 /* If the PHY polling unit is enabled, disable it so that
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000429 * we can access the PHY registers. If it was already
430 * disabled, cancel the timer that is going to re-enable
431 * it.
432 */
433 if (!ps->ppu_disabled) {
Andrew Lunn158bc062016-04-28 21:24:06 -0400434 ret = mv88e6xxx_ppu_disable(ps);
Barry Grussling85686582013-01-08 16:05:56 +0000435 if (ret < 0) {
436 mutex_unlock(&ps->ppu_mutex);
437 return ret;
438 }
439 ps->ppu_disabled = 1;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000440 } else {
Barry Grussling85686582013-01-08 16:05:56 +0000441 del_timer(&ps->ppu_timer);
442 ret = 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000443 }
444
445 return ret;
446}
447
Andrew Lunn158bc062016-04-28 21:24:06 -0400448static void mv88e6xxx_ppu_access_put(struct mv88e6xxx_priv_state *ps)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000449{
Barry Grussling3675c8d2013-01-08 16:05:53 +0000450 /* Schedule a timer to re-enable the PHY polling unit. */
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000451 mod_timer(&ps->ppu_timer, jiffies + msecs_to_jiffies(10));
452 mutex_unlock(&ps->ppu_mutex);
453}
454
Vivien Didelot57d32312016-06-20 13:13:58 -0400455static void mv88e6xxx_ppu_state_init(struct mv88e6xxx_priv_state *ps)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000456{
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000457 mutex_init(&ps->ppu_mutex);
458 INIT_WORK(&ps->ppu_work, mv88e6xxx_ppu_reenable_work);
459 init_timer(&ps->ppu_timer);
460 ps->ppu_timer.data = (unsigned long)ps;
461 ps->ppu_timer.function = mv88e6xxx_ppu_reenable_timer;
462}
463
Andrew Lunn03a4a542016-06-04 21:17:05 +0200464static int mv88e6xxx_mdio_read_ppu(struct mv88e6xxx_priv_state *ps, int addr,
465 int regnum)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000466{
467 int ret;
468
Andrew Lunn158bc062016-04-28 21:24:06 -0400469 ret = mv88e6xxx_ppu_access_get(ps);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000470 if (ret >= 0) {
Vivien Didelot8c9983a2016-05-09 13:22:39 -0400471 ret = _mv88e6xxx_reg_read(ps, addr, regnum);
Andrew Lunn158bc062016-04-28 21:24:06 -0400472 mv88e6xxx_ppu_access_put(ps);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000473 }
474
475 return ret;
476}
477
Andrew Lunn03a4a542016-06-04 21:17:05 +0200478static int mv88e6xxx_mdio_write_ppu(struct mv88e6xxx_priv_state *ps, int addr,
479 int regnum, u16 val)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000480{
481 int ret;
482
Andrew Lunn158bc062016-04-28 21:24:06 -0400483 ret = mv88e6xxx_ppu_access_get(ps);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000484 if (ret >= 0) {
Vivien Didelot8c9983a2016-05-09 13:22:39 -0400485 ret = _mv88e6xxx_reg_write(ps, addr, regnum, val);
Andrew Lunn158bc062016-04-28 21:24:06 -0400486 mv88e6xxx_ppu_access_put(ps);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000487 }
488
489 return ret;
490}
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000491
Andrew Lunn158bc062016-04-28 21:24:06 -0400492static bool mv88e6xxx_6065_family(struct mv88e6xxx_priv_state *ps)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200493{
Vivien Didelot22356472016-04-17 13:24:00 -0400494 return ps->info->family == MV88E6XXX_FAMILY_6065;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200495}
496
Andrew Lunn158bc062016-04-28 21:24:06 -0400497static bool mv88e6xxx_6095_family(struct mv88e6xxx_priv_state *ps)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200498{
Vivien Didelot22356472016-04-17 13:24:00 -0400499 return ps->info->family == MV88E6XXX_FAMILY_6095;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200500}
501
Andrew Lunn158bc062016-04-28 21:24:06 -0400502static bool mv88e6xxx_6097_family(struct mv88e6xxx_priv_state *ps)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200503{
Vivien Didelot22356472016-04-17 13:24:00 -0400504 return ps->info->family == MV88E6XXX_FAMILY_6097;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200505}
506
Andrew Lunn158bc062016-04-28 21:24:06 -0400507static bool mv88e6xxx_6165_family(struct mv88e6xxx_priv_state *ps)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200508{
Vivien Didelot22356472016-04-17 13:24:00 -0400509 return ps->info->family == MV88E6XXX_FAMILY_6165;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200510}
511
Andrew Lunn158bc062016-04-28 21:24:06 -0400512static bool mv88e6xxx_6185_family(struct mv88e6xxx_priv_state *ps)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200513{
Vivien Didelot22356472016-04-17 13:24:00 -0400514 return ps->info->family == MV88E6XXX_FAMILY_6185;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200515}
516
Andrew Lunn158bc062016-04-28 21:24:06 -0400517static bool mv88e6xxx_6320_family(struct mv88e6xxx_priv_state *ps)
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700518{
Vivien Didelot22356472016-04-17 13:24:00 -0400519 return ps->info->family == MV88E6XXX_FAMILY_6320;
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700520}
521
Andrew Lunn158bc062016-04-28 21:24:06 -0400522static bool mv88e6xxx_6351_family(struct mv88e6xxx_priv_state *ps)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200523{
Vivien Didelot22356472016-04-17 13:24:00 -0400524 return ps->info->family == MV88E6XXX_FAMILY_6351;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200525}
526
Andrew Lunn158bc062016-04-28 21:24:06 -0400527static bool mv88e6xxx_6352_family(struct mv88e6xxx_priv_state *ps)
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200528{
Vivien Didelot22356472016-04-17 13:24:00 -0400529 return ps->info->family == MV88E6XXX_FAMILY_6352;
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200530}
531
Andrew Lunn158bc062016-04-28 21:24:06 -0400532static unsigned int mv88e6xxx_num_databases(struct mv88e6xxx_priv_state *ps)
Vivien Didelotf74df0b2016-03-31 16:53:43 -0400533{
Vivien Didelotcd5a2c82016-04-17 13:24:02 -0400534 return ps->info->num_databases;
Vivien Didelotf74df0b2016-03-31 16:53:43 -0400535}
536
Andrew Lunn158bc062016-04-28 21:24:06 -0400537static bool mv88e6xxx_has_fid_reg(struct mv88e6xxx_priv_state *ps)
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400538{
539 /* Does the device have dedicated FID registers for ATU and VTU ops? */
Andrew Lunn158bc062016-04-28 21:24:06 -0400540 if (mv88e6xxx_6097_family(ps) || mv88e6xxx_6165_family(ps) ||
541 mv88e6xxx_6351_family(ps) || mv88e6xxx_6352_family(ps))
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400542 return true;
543
544 return false;
545}
546
Andrew Lunndea87022015-08-31 15:56:47 +0200547/* We expect the switch to perform auto negotiation if there is a real
548 * phy. However, in the case of a fixed link phy, we force the port
549 * settings from the fixed link settings.
550 */
Vivien Didelotf81ec902016-05-09 13:22:58 -0400551static void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port,
552 struct phy_device *phydev)
Andrew Lunndea87022015-08-31 15:56:47 +0200553{
554 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunn49052872015-09-29 01:53:48 +0200555 u32 reg;
556 int ret;
Andrew Lunndea87022015-08-31 15:56:47 +0200557
558 if (!phy_is_pseudo_fixed_link(phydev))
559 return;
560
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -0400561 mutex_lock(&ps->reg_lock);
Andrew Lunndea87022015-08-31 15:56:47 +0200562
Andrew Lunn158bc062016-04-28 21:24:06 -0400563 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_PCS_CTRL);
Andrew Lunndea87022015-08-31 15:56:47 +0200564 if (ret < 0)
565 goto out;
566
567 reg = ret & ~(PORT_PCS_CTRL_LINK_UP |
568 PORT_PCS_CTRL_FORCE_LINK |
569 PORT_PCS_CTRL_DUPLEX_FULL |
570 PORT_PCS_CTRL_FORCE_DUPLEX |
571 PORT_PCS_CTRL_UNFORCED);
572
573 reg |= PORT_PCS_CTRL_FORCE_LINK;
574 if (phydev->link)
Vivien Didelot57d32312016-06-20 13:13:58 -0400575 reg |= PORT_PCS_CTRL_LINK_UP;
Andrew Lunndea87022015-08-31 15:56:47 +0200576
Andrew Lunn158bc062016-04-28 21:24:06 -0400577 if (mv88e6xxx_6065_family(ps) && phydev->speed > SPEED_100)
Andrew Lunndea87022015-08-31 15:56:47 +0200578 goto out;
579
580 switch (phydev->speed) {
581 case SPEED_1000:
582 reg |= PORT_PCS_CTRL_1000;
583 break;
584 case SPEED_100:
585 reg |= PORT_PCS_CTRL_100;
586 break;
587 case SPEED_10:
588 reg |= PORT_PCS_CTRL_10;
589 break;
590 default:
591 pr_info("Unknown speed");
592 goto out;
593 }
594
595 reg |= PORT_PCS_CTRL_FORCE_DUPLEX;
596 if (phydev->duplex == DUPLEX_FULL)
597 reg |= PORT_PCS_CTRL_DUPLEX_FULL;
598
Andrew Lunn158bc062016-04-28 21:24:06 -0400599 if ((mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps)) &&
Vivien Didelot009a2b92016-04-17 13:24:01 -0400600 (port >= ps->info->num_ports - 2)) {
Andrew Lunne7e72ac2015-08-31 15:56:51 +0200601 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
602 reg |= PORT_PCS_CTRL_RGMII_DELAY_RXCLK;
603 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
604 reg |= PORT_PCS_CTRL_RGMII_DELAY_TXCLK;
605 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
606 reg |= (PORT_PCS_CTRL_RGMII_DELAY_RXCLK |
607 PORT_PCS_CTRL_RGMII_DELAY_TXCLK);
608 }
Andrew Lunn158bc062016-04-28 21:24:06 -0400609 _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_PCS_CTRL, reg);
Andrew Lunndea87022015-08-31 15:56:47 +0200610
611out:
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -0400612 mutex_unlock(&ps->reg_lock);
Andrew Lunndea87022015-08-31 15:56:47 +0200613}
614
Andrew Lunn158bc062016-04-28 21:24:06 -0400615static int _mv88e6xxx_stats_wait(struct mv88e6xxx_priv_state *ps)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000616{
617 int ret;
618 int i;
619
620 for (i = 0; i < 10; i++) {
Andrew Lunn158bc062016-04-28 21:24:06 -0400621 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_STATS_OP);
Andrew Lunncca8b132015-04-02 04:06:39 +0200622 if ((ret & GLOBAL_STATS_OP_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000623 return 0;
624 }
625
626 return -ETIMEDOUT;
627}
628
Andrew Lunn158bc062016-04-28 21:24:06 -0400629static int _mv88e6xxx_stats_snapshot(struct mv88e6xxx_priv_state *ps,
630 int port)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000631{
632 int ret;
633
Andrew Lunn158bc062016-04-28 21:24:06 -0400634 if (mv88e6xxx_6320_family(ps) || mv88e6xxx_6352_family(ps))
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200635 port = (port + 1) << 5;
636
Barry Grussling3675c8d2013-01-08 16:05:53 +0000637 /* Snapshot the hardware statistics counters for this port. */
Andrew Lunn158bc062016-04-28 21:24:06 -0400638 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_STATS_OP,
Andrew Lunn31888232015-05-06 01:09:54 +0200639 GLOBAL_STATS_OP_CAPTURE_PORT |
640 GLOBAL_STATS_OP_HIST_RX_TX | port);
641 if (ret < 0)
642 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000643
Barry Grussling3675c8d2013-01-08 16:05:53 +0000644 /* Wait for the snapshotting to complete. */
Andrew Lunn158bc062016-04-28 21:24:06 -0400645 ret = _mv88e6xxx_stats_wait(ps);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000646 if (ret < 0)
647 return ret;
648
649 return 0;
650}
651
Andrew Lunn158bc062016-04-28 21:24:06 -0400652static void _mv88e6xxx_stats_read(struct mv88e6xxx_priv_state *ps,
653 int stat, u32 *val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000654{
655 u32 _val;
656 int ret;
657
658 *val = 0;
659
Andrew Lunn158bc062016-04-28 21:24:06 -0400660 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_STATS_OP,
Andrew Lunn31888232015-05-06 01:09:54 +0200661 GLOBAL_STATS_OP_READ_CAPTURED |
662 GLOBAL_STATS_OP_HIST_RX_TX | stat);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000663 if (ret < 0)
664 return;
665
Andrew Lunn158bc062016-04-28 21:24:06 -0400666 ret = _mv88e6xxx_stats_wait(ps);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000667 if (ret < 0)
668 return;
669
Andrew Lunn158bc062016-04-28 21:24:06 -0400670 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_STATS_COUNTER_32);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000671 if (ret < 0)
672 return;
673
674 _val = ret << 16;
675
Andrew Lunn158bc062016-04-28 21:24:06 -0400676 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_STATS_COUNTER_01);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000677 if (ret < 0)
678 return;
679
680 *val = _val | ret;
681}
682
Andrew Lunne413e7e2015-04-02 04:06:38 +0200683static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100684 { "in_good_octets", 8, 0x00, BANK0, },
685 { "in_bad_octets", 4, 0x02, BANK0, },
686 { "in_unicast", 4, 0x04, BANK0, },
687 { "in_broadcasts", 4, 0x06, BANK0, },
688 { "in_multicasts", 4, 0x07, BANK0, },
689 { "in_pause", 4, 0x16, BANK0, },
690 { "in_undersize", 4, 0x18, BANK0, },
691 { "in_fragments", 4, 0x19, BANK0, },
692 { "in_oversize", 4, 0x1a, BANK0, },
693 { "in_jabber", 4, 0x1b, BANK0, },
694 { "in_rx_error", 4, 0x1c, BANK0, },
695 { "in_fcs_error", 4, 0x1d, BANK0, },
696 { "out_octets", 8, 0x0e, BANK0, },
697 { "out_unicast", 4, 0x10, BANK0, },
698 { "out_broadcasts", 4, 0x13, BANK0, },
699 { "out_multicasts", 4, 0x12, BANK0, },
700 { "out_pause", 4, 0x15, BANK0, },
701 { "excessive", 4, 0x11, BANK0, },
702 { "collisions", 4, 0x1e, BANK0, },
703 { "deferred", 4, 0x05, BANK0, },
704 { "single", 4, 0x14, BANK0, },
705 { "multiple", 4, 0x17, BANK0, },
706 { "out_fcs_error", 4, 0x03, BANK0, },
707 { "late", 4, 0x1f, BANK0, },
708 { "hist_64bytes", 4, 0x08, BANK0, },
709 { "hist_65_127bytes", 4, 0x09, BANK0, },
710 { "hist_128_255bytes", 4, 0x0a, BANK0, },
711 { "hist_256_511bytes", 4, 0x0b, BANK0, },
712 { "hist_512_1023bytes", 4, 0x0c, BANK0, },
713 { "hist_1024_max_bytes", 4, 0x0d, BANK0, },
714 { "sw_in_discards", 4, 0x10, PORT, },
715 { "sw_in_filtered", 2, 0x12, PORT, },
716 { "sw_out_filtered", 2, 0x13, PORT, },
717 { "in_discards", 4, 0x00 | GLOBAL_STATS_OP_BANK_1, BANK1, },
718 { "in_filtered", 4, 0x01 | GLOBAL_STATS_OP_BANK_1, BANK1, },
719 { "in_accepted", 4, 0x02 | GLOBAL_STATS_OP_BANK_1, BANK1, },
720 { "in_bad_accepted", 4, 0x03 | GLOBAL_STATS_OP_BANK_1, BANK1, },
721 { "in_good_avb_class_a", 4, 0x04 | GLOBAL_STATS_OP_BANK_1, BANK1, },
722 { "in_good_avb_class_b", 4, 0x05 | GLOBAL_STATS_OP_BANK_1, BANK1, },
723 { "in_bad_avb_class_a", 4, 0x06 | GLOBAL_STATS_OP_BANK_1, BANK1, },
724 { "in_bad_avb_class_b", 4, 0x07 | GLOBAL_STATS_OP_BANK_1, BANK1, },
725 { "tcam_counter_0", 4, 0x08 | GLOBAL_STATS_OP_BANK_1, BANK1, },
726 { "tcam_counter_1", 4, 0x09 | GLOBAL_STATS_OP_BANK_1, BANK1, },
727 { "tcam_counter_2", 4, 0x0a | GLOBAL_STATS_OP_BANK_1, BANK1, },
728 { "tcam_counter_3", 4, 0x0b | GLOBAL_STATS_OP_BANK_1, BANK1, },
729 { "in_da_unknown", 4, 0x0e | GLOBAL_STATS_OP_BANK_1, BANK1, },
730 { "in_management", 4, 0x0f | GLOBAL_STATS_OP_BANK_1, BANK1, },
731 { "out_queue_0", 4, 0x10 | GLOBAL_STATS_OP_BANK_1, BANK1, },
732 { "out_queue_1", 4, 0x11 | GLOBAL_STATS_OP_BANK_1, BANK1, },
733 { "out_queue_2", 4, 0x12 | GLOBAL_STATS_OP_BANK_1, BANK1, },
734 { "out_queue_3", 4, 0x13 | GLOBAL_STATS_OP_BANK_1, BANK1, },
735 { "out_queue_4", 4, 0x14 | GLOBAL_STATS_OP_BANK_1, BANK1, },
736 { "out_queue_5", 4, 0x15 | GLOBAL_STATS_OP_BANK_1, BANK1, },
737 { "out_queue_6", 4, 0x16 | GLOBAL_STATS_OP_BANK_1, BANK1, },
738 { "out_queue_7", 4, 0x17 | GLOBAL_STATS_OP_BANK_1, BANK1, },
739 { "out_cut_through", 4, 0x18 | GLOBAL_STATS_OP_BANK_1, BANK1, },
740 { "out_octets_a", 4, 0x1a | GLOBAL_STATS_OP_BANK_1, BANK1, },
741 { "out_octets_b", 4, 0x1b | GLOBAL_STATS_OP_BANK_1, BANK1, },
742 { "out_management", 4, 0x1f | GLOBAL_STATS_OP_BANK_1, BANK1, },
Andrew Lunne413e7e2015-04-02 04:06:38 +0200743};
744
Andrew Lunn158bc062016-04-28 21:24:06 -0400745static bool mv88e6xxx_has_stat(struct mv88e6xxx_priv_state *ps,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100746 struct mv88e6xxx_hw_stat *stat)
Andrew Lunne413e7e2015-04-02 04:06:38 +0200747{
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100748 switch (stat->type) {
749 case BANK0:
Andrew Lunne413e7e2015-04-02 04:06:38 +0200750 return true;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100751 case BANK1:
Andrew Lunn158bc062016-04-28 21:24:06 -0400752 return mv88e6xxx_6320_family(ps);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100753 case PORT:
Andrew Lunn158bc062016-04-28 21:24:06 -0400754 return mv88e6xxx_6095_family(ps) ||
755 mv88e6xxx_6185_family(ps) ||
756 mv88e6xxx_6097_family(ps) ||
757 mv88e6xxx_6165_family(ps) ||
758 mv88e6xxx_6351_family(ps) ||
759 mv88e6xxx_6352_family(ps);
Andrew Lunne413e7e2015-04-02 04:06:38 +0200760 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100761 return false;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000762}
763
Andrew Lunn158bc062016-04-28 21:24:06 -0400764static uint64_t _mv88e6xxx_get_ethtool_stat(struct mv88e6xxx_priv_state *ps,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100765 struct mv88e6xxx_hw_stat *s,
Andrew Lunn80c46272015-06-20 18:42:30 +0200766 int port)
767{
Andrew Lunn80c46272015-06-20 18:42:30 +0200768 u32 low;
769 u32 high = 0;
770 int ret;
771 u64 value;
772
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100773 switch (s->type) {
774 case PORT:
Andrew Lunn158bc062016-04-28 21:24:06 -0400775 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), s->reg);
Andrew Lunn80c46272015-06-20 18:42:30 +0200776 if (ret < 0)
777 return UINT64_MAX;
778
779 low = ret;
780 if (s->sizeof_stat == 4) {
Andrew Lunn158bc062016-04-28 21:24:06 -0400781 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port),
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100782 s->reg + 1);
Andrew Lunn80c46272015-06-20 18:42:30 +0200783 if (ret < 0)
784 return UINT64_MAX;
785 high = ret;
786 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100787 break;
788 case BANK0:
789 case BANK1:
Andrew Lunn158bc062016-04-28 21:24:06 -0400790 _mv88e6xxx_stats_read(ps, s->reg, &low);
Andrew Lunn80c46272015-06-20 18:42:30 +0200791 if (s->sizeof_stat == 8)
Andrew Lunn158bc062016-04-28 21:24:06 -0400792 _mv88e6xxx_stats_read(ps, s->reg + 1, &high);
Andrew Lunn80c46272015-06-20 18:42:30 +0200793 }
794 value = (((u64)high) << 16) | low;
795 return value;
796}
797
Vivien Didelotf81ec902016-05-09 13:22:58 -0400798static void mv88e6xxx_get_strings(struct dsa_switch *ds, int port,
799 uint8_t *data)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100800{
Andrew Lunn158bc062016-04-28 21:24:06 -0400801 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100802 struct mv88e6xxx_hw_stat *stat;
803 int i, j;
804
805 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
806 stat = &mv88e6xxx_hw_stats[i];
Andrew Lunn158bc062016-04-28 21:24:06 -0400807 if (mv88e6xxx_has_stat(ps, stat)) {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100808 memcpy(data + j * ETH_GSTRING_LEN, stat->string,
809 ETH_GSTRING_LEN);
810 j++;
811 }
812 }
813}
814
Vivien Didelotf81ec902016-05-09 13:22:58 -0400815static int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100816{
Andrew Lunn158bc062016-04-28 21:24:06 -0400817 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100818 struct mv88e6xxx_hw_stat *stat;
819 int i, j;
820
821 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
822 stat = &mv88e6xxx_hw_stats[i];
Andrew Lunn158bc062016-04-28 21:24:06 -0400823 if (mv88e6xxx_has_stat(ps, stat))
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100824 j++;
825 }
826 return j;
827}
828
Vivien Didelotf81ec902016-05-09 13:22:58 -0400829static void mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, int port,
830 uint64_t *data)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000831{
Florian Fainellia22adce2014-04-28 11:14:28 -0700832 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100833 struct mv88e6xxx_hw_stat *stat;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000834 int ret;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100835 int i, j;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000836
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -0400837 mutex_lock(&ps->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000838
Andrew Lunn158bc062016-04-28 21:24:06 -0400839 ret = _mv88e6xxx_stats_snapshot(ps, port);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000840 if (ret < 0) {
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -0400841 mutex_unlock(&ps->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000842 return;
843 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100844 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
845 stat = &mv88e6xxx_hw_stats[i];
Andrew Lunn158bc062016-04-28 21:24:06 -0400846 if (mv88e6xxx_has_stat(ps, stat)) {
847 data[j] = _mv88e6xxx_get_ethtool_stat(ps, stat, port);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100848 j++;
849 }
850 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000851
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -0400852 mutex_unlock(&ps->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000853}
Ben Hutchings98e67302011-11-25 14:36:19 +0000854
Vivien Didelotf81ec902016-05-09 13:22:58 -0400855static int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700856{
857 return 32 * sizeof(u16);
858}
859
Vivien Didelotf81ec902016-05-09 13:22:58 -0400860static void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
861 struct ethtool_regs *regs, void *_p)
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700862{
Andrew Lunn158bc062016-04-28 21:24:06 -0400863 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700864 u16 *p = _p;
865 int i;
866
867 regs->version = 0;
868
869 memset(p, 0xff, 32 * sizeof(u16));
870
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -0400871 mutex_lock(&ps->reg_lock);
Vivien Didelot23062512016-05-09 13:22:45 -0400872
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700873 for (i = 0; i < 32; i++) {
874 int ret;
875
Vivien Didelot23062512016-05-09 13:22:45 -0400876 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), i);
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700877 if (ret >= 0)
878 p[i] = ret;
879 }
Vivien Didelot23062512016-05-09 13:22:45 -0400880
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -0400881 mutex_unlock(&ps->reg_lock);
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700882}
883
Andrew Lunn158bc062016-04-28 21:24:06 -0400884static int _mv88e6xxx_wait(struct mv88e6xxx_priv_state *ps, int reg, int offset,
Andrew Lunn3898c142015-05-06 01:09:53 +0200885 u16 mask)
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700886{
887 unsigned long timeout = jiffies + HZ / 10;
888
889 while (time_before(jiffies, timeout)) {
890 int ret;
891
Andrew Lunn158bc062016-04-28 21:24:06 -0400892 ret = _mv88e6xxx_reg_read(ps, reg, offset);
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700893 if (ret < 0)
894 return ret;
895 if (!(ret & mask))
896 return 0;
897
898 usleep_range(1000, 2000);
899 }
900 return -ETIMEDOUT;
901}
902
Andrew Lunn158bc062016-04-28 21:24:06 -0400903static int mv88e6xxx_wait(struct mv88e6xxx_priv_state *ps, int reg,
904 int offset, u16 mask)
Andrew Lunn3898c142015-05-06 01:09:53 +0200905{
Andrew Lunn3898c142015-05-06 01:09:53 +0200906 int ret;
907
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -0400908 mutex_lock(&ps->reg_lock);
Andrew Lunn158bc062016-04-28 21:24:06 -0400909 ret = _mv88e6xxx_wait(ps, reg, offset, mask);
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -0400910 mutex_unlock(&ps->reg_lock);
Andrew Lunn3898c142015-05-06 01:09:53 +0200911
912 return ret;
913}
914
Andrew Lunn03a4a542016-06-04 21:17:05 +0200915static int mv88e6xxx_mdio_wait(struct mv88e6xxx_priv_state *ps)
Andrew Lunn3898c142015-05-06 01:09:53 +0200916{
Andrew Lunn158bc062016-04-28 21:24:06 -0400917 return _mv88e6xxx_wait(ps, REG_GLOBAL2, GLOBAL2_SMI_OP,
Andrew Lunn3898c142015-05-06 01:09:53 +0200918 GLOBAL2_SMI_OP_BUSY);
919}
920
Vivien Didelotd24645b2016-05-09 13:22:41 -0400921static int mv88e6xxx_eeprom_load_wait(struct dsa_switch *ds)
Andrew Lunn3898c142015-05-06 01:09:53 +0200922{
Andrew Lunn158bc062016-04-28 21:24:06 -0400923 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
924
925 return mv88e6xxx_wait(ps, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
Andrew Lunn3898c142015-05-06 01:09:53 +0200926 GLOBAL2_EEPROM_OP_LOAD);
927}
928
Vivien Didelotd24645b2016-05-09 13:22:41 -0400929static int mv88e6xxx_eeprom_busy_wait(struct dsa_switch *ds)
Andrew Lunn3898c142015-05-06 01:09:53 +0200930{
Andrew Lunn158bc062016-04-28 21:24:06 -0400931 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
932
933 return mv88e6xxx_wait(ps, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
Andrew Lunn3898c142015-05-06 01:09:53 +0200934 GLOBAL2_EEPROM_OP_BUSY);
935}
936
Vivien Didelotd24645b2016-05-09 13:22:41 -0400937static int mv88e6xxx_read_eeprom_word(struct dsa_switch *ds, int addr)
938{
939 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
940 int ret;
941
942 mutex_lock(&ps->eeprom_mutex);
943
944 ret = mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
945 GLOBAL2_EEPROM_OP_READ |
946 (addr & GLOBAL2_EEPROM_OP_ADDR_MASK));
947 if (ret < 0)
948 goto error;
949
950 ret = mv88e6xxx_eeprom_busy_wait(ds);
951 if (ret < 0)
952 goto error;
953
954 ret = mv88e6xxx_reg_read(ps, REG_GLOBAL2, GLOBAL2_EEPROM_DATA);
955error:
956 mutex_unlock(&ps->eeprom_mutex);
957 return ret;
958}
959
Andrew Lunnf8cd8752016-05-10 23:27:25 +0200960static int mv88e6xxx_get_eeprom_len(struct dsa_switch *ds)
961{
962 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
963
964 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEPROM))
965 return ps->eeprom_len;
966
967 return 0;
968}
969
Vivien Didelotf81ec902016-05-09 13:22:58 -0400970static int mv88e6xxx_get_eeprom(struct dsa_switch *ds,
971 struct ethtool_eeprom *eeprom, u8 *data)
Vivien Didelotd24645b2016-05-09 13:22:41 -0400972{
973 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
974 int offset;
975 int len;
976 int ret;
977
978 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEPROM))
979 return -EOPNOTSUPP;
980
981 offset = eeprom->offset;
982 len = eeprom->len;
983 eeprom->len = 0;
984
985 eeprom->magic = 0xc3ec4951;
986
987 ret = mv88e6xxx_eeprom_load_wait(ds);
988 if (ret < 0)
989 return ret;
990
991 if (offset & 1) {
992 int word;
993
994 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
995 if (word < 0)
996 return word;
997
998 *data++ = (word >> 8) & 0xff;
999
1000 offset++;
1001 len--;
1002 eeprom->len++;
1003 }
1004
1005 while (len >= 2) {
1006 int word;
1007
1008 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
1009 if (word < 0)
1010 return word;
1011
1012 *data++ = word & 0xff;
1013 *data++ = (word >> 8) & 0xff;
1014
1015 offset += 2;
1016 len -= 2;
1017 eeprom->len += 2;
1018 }
1019
1020 if (len) {
1021 int word;
1022
1023 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
1024 if (word < 0)
1025 return word;
1026
1027 *data++ = word & 0xff;
1028
1029 offset++;
1030 len--;
1031 eeprom->len++;
1032 }
1033
1034 return 0;
1035}
1036
1037static int mv88e6xxx_eeprom_is_readonly(struct dsa_switch *ds)
1038{
1039 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1040 int ret;
1041
1042 ret = mv88e6xxx_reg_read(ps, REG_GLOBAL2, GLOBAL2_EEPROM_OP);
1043 if (ret < 0)
1044 return ret;
1045
1046 if (!(ret & GLOBAL2_EEPROM_OP_WRITE_EN))
1047 return -EROFS;
1048
1049 return 0;
1050}
1051
1052static int mv88e6xxx_write_eeprom_word(struct dsa_switch *ds, int addr,
1053 u16 data)
1054{
1055 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1056 int ret;
1057
1058 mutex_lock(&ps->eeprom_mutex);
1059
1060 ret = mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_EEPROM_DATA, data);
1061 if (ret < 0)
1062 goto error;
1063
1064 ret = mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
1065 GLOBAL2_EEPROM_OP_WRITE |
1066 (addr & GLOBAL2_EEPROM_OP_ADDR_MASK));
1067 if (ret < 0)
1068 goto error;
1069
1070 ret = mv88e6xxx_eeprom_busy_wait(ds);
1071error:
1072 mutex_unlock(&ps->eeprom_mutex);
1073 return ret;
1074}
1075
Vivien Didelotf81ec902016-05-09 13:22:58 -04001076static int mv88e6xxx_set_eeprom(struct dsa_switch *ds,
1077 struct ethtool_eeprom *eeprom, u8 *data)
Vivien Didelotd24645b2016-05-09 13:22:41 -04001078{
1079 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1080 int offset;
1081 int ret;
1082 int len;
1083
1084 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEPROM))
1085 return -EOPNOTSUPP;
1086
1087 if (eeprom->magic != 0xc3ec4951)
1088 return -EINVAL;
1089
1090 ret = mv88e6xxx_eeprom_is_readonly(ds);
1091 if (ret)
1092 return ret;
1093
1094 offset = eeprom->offset;
1095 len = eeprom->len;
1096 eeprom->len = 0;
1097
1098 ret = mv88e6xxx_eeprom_load_wait(ds);
1099 if (ret < 0)
1100 return ret;
1101
1102 if (offset & 1) {
1103 int word;
1104
1105 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
1106 if (word < 0)
1107 return word;
1108
1109 word = (*data++ << 8) | (word & 0xff);
1110
1111 ret = mv88e6xxx_write_eeprom_word(ds, offset >> 1, word);
1112 if (ret < 0)
1113 return ret;
1114
1115 offset++;
1116 len--;
1117 eeprom->len++;
1118 }
1119
1120 while (len >= 2) {
1121 int word;
1122
1123 word = *data++;
1124 word |= *data++ << 8;
1125
1126 ret = mv88e6xxx_write_eeprom_word(ds, offset >> 1, word);
1127 if (ret < 0)
1128 return ret;
1129
1130 offset += 2;
1131 len -= 2;
1132 eeprom->len += 2;
1133 }
1134
1135 if (len) {
1136 int word;
1137
1138 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
1139 if (word < 0)
1140 return word;
1141
1142 word = (word & 0xff00) | *data++;
1143
1144 ret = mv88e6xxx_write_eeprom_word(ds, offset >> 1, word);
1145 if (ret < 0)
1146 return ret;
1147
1148 offset++;
1149 len--;
1150 eeprom->len++;
1151 }
1152
1153 return 0;
1154}
1155
Andrew Lunn158bc062016-04-28 21:24:06 -04001156static int _mv88e6xxx_atu_wait(struct mv88e6xxx_priv_state *ps)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001157{
Andrew Lunn158bc062016-04-28 21:24:06 -04001158 return _mv88e6xxx_wait(ps, REG_GLOBAL, GLOBAL_ATU_OP,
Andrew Lunncca8b132015-04-02 04:06:39 +02001159 GLOBAL_ATU_OP_BUSY);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001160}
1161
Andrew Lunn03a4a542016-06-04 21:17:05 +02001162static int mv88e6xxx_mdio_read_indirect(struct mv88e6xxx_priv_state *ps,
Andrew Lunn158bc062016-04-28 21:24:06 -04001163 int addr, int regnum)
Andrew Lunnf3044682015-02-14 19:17:50 +01001164{
1165 int ret;
1166
Andrew Lunn158bc062016-04-28 21:24:06 -04001167 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SMI_OP,
Andrew Lunn3898c142015-05-06 01:09:53 +02001168 GLOBAL2_SMI_OP_22_READ | (addr << 5) |
1169 regnum);
Andrew Lunnf3044682015-02-14 19:17:50 +01001170 if (ret < 0)
1171 return ret;
1172
Andrew Lunn03a4a542016-06-04 21:17:05 +02001173 ret = mv88e6xxx_mdio_wait(ps);
Andrew Lunn3898c142015-05-06 01:09:53 +02001174 if (ret < 0)
1175 return ret;
1176
Andrew Lunn158bc062016-04-28 21:24:06 -04001177 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL2, GLOBAL2_SMI_DATA);
1178
1179 return ret;
Andrew Lunnf3044682015-02-14 19:17:50 +01001180}
1181
Andrew Lunn03a4a542016-06-04 21:17:05 +02001182static int mv88e6xxx_mdio_write_indirect(struct mv88e6xxx_priv_state *ps,
Andrew Lunn158bc062016-04-28 21:24:06 -04001183 int addr, int regnum, u16 val)
Andrew Lunnf3044682015-02-14 19:17:50 +01001184{
Andrew Lunn3898c142015-05-06 01:09:53 +02001185 int ret;
Andrew Lunnf3044682015-02-14 19:17:50 +01001186
Andrew Lunn158bc062016-04-28 21:24:06 -04001187 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SMI_DATA, val);
Andrew Lunn3898c142015-05-06 01:09:53 +02001188 if (ret < 0)
1189 return ret;
1190
Andrew Lunn158bc062016-04-28 21:24:06 -04001191 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SMI_OP,
Andrew Lunn3898c142015-05-06 01:09:53 +02001192 GLOBAL2_SMI_OP_22_WRITE | (addr << 5) |
1193 regnum);
1194
Andrew Lunn03a4a542016-06-04 21:17:05 +02001195 return mv88e6xxx_mdio_wait(ps);
Andrew Lunnf3044682015-02-14 19:17:50 +01001196}
1197
Vivien Didelotf81ec902016-05-09 13:22:58 -04001198static int mv88e6xxx_get_eee(struct dsa_switch *ds, int port,
1199 struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -08001200{
Andrew Lunn2f40c692015-04-02 04:06:37 +02001201 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Guenter Roeck11b3b452015-03-06 22:23:51 -08001202 int reg;
1203
Vivien Didelotaadbdb82016-05-09 13:22:44 -04001204 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEE))
1205 return -EOPNOTSUPP;
1206
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04001207 mutex_lock(&ps->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001208
Andrew Lunn03a4a542016-06-04 21:17:05 +02001209 reg = mv88e6xxx_mdio_read_indirect(ps, port, 16);
Guenter Roeck11b3b452015-03-06 22:23:51 -08001210 if (reg < 0)
Andrew Lunn2f40c692015-04-02 04:06:37 +02001211 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001212
1213 e->eee_enabled = !!(reg & 0x0200);
1214 e->tx_lpi_enabled = !!(reg & 0x0100);
1215
Andrew Lunn158bc062016-04-28 21:24:06 -04001216 reg = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_STATUS);
Guenter Roeck11b3b452015-03-06 22:23:51 -08001217 if (reg < 0)
Andrew Lunn2f40c692015-04-02 04:06:37 +02001218 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001219
Andrew Lunncca8b132015-04-02 04:06:39 +02001220 e->eee_active = !!(reg & PORT_STATUS_EEE);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001221 reg = 0;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001222
Andrew Lunn2f40c692015-04-02 04:06:37 +02001223out:
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04001224 mutex_unlock(&ps->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001225 return reg;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001226}
1227
Vivien Didelotf81ec902016-05-09 13:22:58 -04001228static int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
1229 struct phy_device *phydev, struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -08001230{
Andrew Lunn2f40c692015-04-02 04:06:37 +02001231 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1232 int reg;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001233 int ret;
1234
Vivien Didelotaadbdb82016-05-09 13:22:44 -04001235 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEE))
1236 return -EOPNOTSUPP;
1237
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04001238 mutex_lock(&ps->reg_lock);
Guenter Roeck11b3b452015-03-06 22:23:51 -08001239
Andrew Lunn03a4a542016-06-04 21:17:05 +02001240 ret = mv88e6xxx_mdio_read_indirect(ps, port, 16);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001241 if (ret < 0)
1242 goto out;
1243
1244 reg = ret & ~0x0300;
1245 if (e->eee_enabled)
1246 reg |= 0x0200;
1247 if (e->tx_lpi_enabled)
1248 reg |= 0x0100;
1249
Andrew Lunn03a4a542016-06-04 21:17:05 +02001250 ret = mv88e6xxx_mdio_write_indirect(ps, port, 16, reg);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001251out:
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04001252 mutex_unlock(&ps->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001253
1254 return ret;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001255}
1256
Andrew Lunn158bc062016-04-28 21:24:06 -04001257static int _mv88e6xxx_atu_cmd(struct mv88e6xxx_priv_state *ps, u16 fid, u16 cmd)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001258{
1259 int ret;
1260
Andrew Lunn158bc062016-04-28 21:24:06 -04001261 if (mv88e6xxx_has_fid_reg(ps)) {
1262 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_FID, fid);
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001263 if (ret < 0)
1264 return ret;
Andrew Lunn158bc062016-04-28 21:24:06 -04001265 } else if (mv88e6xxx_num_databases(ps) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001266 /* ATU DBNum[7:4] are located in ATU Control 15:12 */
Andrew Lunn158bc062016-04-28 21:24:06 -04001267 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_ATU_CONTROL);
Vivien Didelot11ea8092016-03-31 16:53:44 -04001268 if (ret < 0)
1269 return ret;
1270
Andrew Lunn158bc062016-04-28 21:24:06 -04001271 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_CONTROL,
Vivien Didelot11ea8092016-03-31 16:53:44 -04001272 (ret & 0xfff) |
1273 ((fid << 8) & 0xf000));
1274 if (ret < 0)
1275 return ret;
1276
1277 /* ATU DBNum[3:0] are located in ATU Operation 3:0 */
1278 cmd |= fid & 0xf;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001279 }
1280
Andrew Lunn158bc062016-04-28 21:24:06 -04001281 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_OP, cmd);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001282 if (ret < 0)
1283 return ret;
1284
Andrew Lunn158bc062016-04-28 21:24:06 -04001285 return _mv88e6xxx_atu_wait(ps);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001286}
1287
Andrew Lunn158bc062016-04-28 21:24:06 -04001288static int _mv88e6xxx_atu_data_write(struct mv88e6xxx_priv_state *ps,
Vivien Didelot37705b72015-09-04 14:34:11 -04001289 struct mv88e6xxx_atu_entry *entry)
1290{
1291 u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK;
1292
1293 if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) {
1294 unsigned int mask, shift;
1295
1296 if (entry->trunk) {
1297 data |= GLOBAL_ATU_DATA_TRUNK;
1298 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
1299 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
1300 } else {
1301 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
1302 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
1303 }
1304
1305 data |= (entry->portv_trunkid << shift) & mask;
1306 }
1307
Andrew Lunn158bc062016-04-28 21:24:06 -04001308 return _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_DATA, data);
Vivien Didelot37705b72015-09-04 14:34:11 -04001309}
1310
Andrew Lunn158bc062016-04-28 21:24:06 -04001311static int _mv88e6xxx_atu_flush_move(struct mv88e6xxx_priv_state *ps,
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001312 struct mv88e6xxx_atu_entry *entry,
1313 bool static_too)
1314{
1315 int op;
1316 int err;
1317
Andrew Lunn158bc062016-04-28 21:24:06 -04001318 err = _mv88e6xxx_atu_wait(ps);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001319 if (err)
1320 return err;
1321
Andrew Lunn158bc062016-04-28 21:24:06 -04001322 err = _mv88e6xxx_atu_data_write(ps, entry);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001323 if (err)
1324 return err;
1325
1326 if (entry->fid) {
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001327 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB :
1328 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB;
1329 } else {
1330 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL :
1331 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC;
1332 }
1333
Andrew Lunn158bc062016-04-28 21:24:06 -04001334 return _mv88e6xxx_atu_cmd(ps, entry->fid, op);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001335}
1336
Andrew Lunn158bc062016-04-28 21:24:06 -04001337static int _mv88e6xxx_atu_flush(struct mv88e6xxx_priv_state *ps,
1338 u16 fid, bool static_too)
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001339{
1340 struct mv88e6xxx_atu_entry entry = {
1341 .fid = fid,
1342 .state = 0, /* EntryState bits must be 0 */
1343 };
1344
Andrew Lunn158bc062016-04-28 21:24:06 -04001345 return _mv88e6xxx_atu_flush_move(ps, &entry, static_too);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001346}
1347
Andrew Lunn158bc062016-04-28 21:24:06 -04001348static int _mv88e6xxx_atu_move(struct mv88e6xxx_priv_state *ps, u16 fid,
1349 int from_port, int to_port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001350{
1351 struct mv88e6xxx_atu_entry entry = {
1352 .trunk = false,
1353 .fid = fid,
1354 };
1355
1356 /* EntryState bits must be 0xF */
1357 entry.state = GLOBAL_ATU_DATA_STATE_MASK;
1358
1359 /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */
1360 entry.portv_trunkid = (to_port & 0x0f) << 4;
1361 entry.portv_trunkid |= from_port & 0x0f;
1362
Andrew Lunn158bc062016-04-28 21:24:06 -04001363 return _mv88e6xxx_atu_flush_move(ps, &entry, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001364}
1365
Andrew Lunn158bc062016-04-28 21:24:06 -04001366static int _mv88e6xxx_atu_remove(struct mv88e6xxx_priv_state *ps, u16 fid,
1367 int port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001368{
1369 /* Destination port 0xF means remove the entries */
Andrew Lunn158bc062016-04-28 21:24:06 -04001370 return _mv88e6xxx_atu_move(ps, fid, port, 0x0f, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001371}
1372
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001373static const char * const mv88e6xxx_port_state_names[] = {
1374 [PORT_CONTROL_STATE_DISABLED] = "Disabled",
1375 [PORT_CONTROL_STATE_BLOCKING] = "Blocking/Listening",
1376 [PORT_CONTROL_STATE_LEARNING] = "Learning",
1377 [PORT_CONTROL_STATE_FORWARDING] = "Forwarding",
1378};
1379
Andrew Lunn158bc062016-04-28 21:24:06 -04001380static int _mv88e6xxx_port_state(struct mv88e6xxx_priv_state *ps, int port,
1381 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001382{
Andrew Lunn158bc062016-04-28 21:24:06 -04001383 struct dsa_switch *ds = ps->ds;
Geert Uytterhoevenc3ffe6d2015-04-16 20:49:14 +02001384 int reg, ret = 0;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001385 u8 oldstate;
1386
Andrew Lunn158bc062016-04-28 21:24:06 -04001387 reg = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_CONTROL);
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001388 if (reg < 0)
1389 return reg;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001390
Andrew Lunncca8b132015-04-02 04:06:39 +02001391 oldstate = reg & PORT_CONTROL_STATE_MASK;
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001392
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001393 if (oldstate != state) {
1394 /* Flush forwarding database if we're moving a port
1395 * from Learning or Forwarding state to Disabled or
1396 * Blocking or Listening state.
1397 */
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001398 if ((oldstate == PORT_CONTROL_STATE_LEARNING ||
Vivien Didelot57d32312016-06-20 13:13:58 -04001399 oldstate == PORT_CONTROL_STATE_FORWARDING) &&
1400 (state == PORT_CONTROL_STATE_DISABLED ||
1401 state == PORT_CONTROL_STATE_BLOCKING)) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001402 ret = _mv88e6xxx_atu_remove(ps, 0, port, false);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001403 if (ret)
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001404 return ret;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001405 }
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001406
Andrew Lunncca8b132015-04-02 04:06:39 +02001407 reg = (reg & ~PORT_CONTROL_STATE_MASK) | state;
Andrew Lunn158bc062016-04-28 21:24:06 -04001408 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL,
Andrew Lunncca8b132015-04-02 04:06:39 +02001409 reg);
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001410 if (ret)
1411 return ret;
1412
Andrew Lunnc8b09802016-06-04 21:16:57 +02001413 netdev_dbg(ds->ports[port].netdev, "PortState %s (was %s)\n",
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001414 mv88e6xxx_port_state_names[state],
1415 mv88e6xxx_port_state_names[oldstate]);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001416 }
1417
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001418 return ret;
1419}
1420
Andrew Lunn158bc062016-04-28 21:24:06 -04001421static int _mv88e6xxx_port_based_vlan_map(struct mv88e6xxx_priv_state *ps,
1422 int port)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001423{
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001424 struct net_device *bridge = ps->ports[port].bridge_dev;
Vivien Didelot009a2b92016-04-17 13:24:01 -04001425 const u16 mask = (1 << ps->info->num_ports) - 1;
Andrew Lunn158bc062016-04-28 21:24:06 -04001426 struct dsa_switch *ds = ps->ds;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001427 u16 output_ports = 0;
Vivien Didelotede80982015-10-11 18:08:35 -04001428 int reg;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001429 int i;
1430
1431 /* allow CPU port or DSA link(s) to send frames to every port */
1432 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
1433 output_ports = mask;
1434 } else {
Vivien Didelot009a2b92016-04-17 13:24:01 -04001435 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001436 /* allow sending frames to every group member */
1437 if (bridge && ps->ports[i].bridge_dev == bridge)
1438 output_ports |= BIT(i);
1439
1440 /* allow sending frames to CPU port and DSA link(s) */
1441 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
1442 output_ports |= BIT(i);
1443 }
1444 }
1445
1446 /* prevent frames from going back out of the port they came in on */
1447 output_ports &= ~BIT(port);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001448
Andrew Lunn158bc062016-04-28 21:24:06 -04001449 reg = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_BASE_VLAN);
Vivien Didelotede80982015-10-11 18:08:35 -04001450 if (reg < 0)
1451 return reg;
1452
1453 reg &= ~mask;
1454 reg |= output_ports & mask;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001455
Andrew Lunn158bc062016-04-28 21:24:06 -04001456 return _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_BASE_VLAN, reg);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001457}
1458
Vivien Didelotf81ec902016-05-09 13:22:58 -04001459static void mv88e6xxx_port_stp_state_set(struct dsa_switch *ds, int port,
1460 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001461{
1462 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1463 int stp_state;
Vivien Didelot553eb542016-05-13 20:38:23 -04001464 int err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001465
Vivien Didelot936f2342016-05-09 13:22:46 -04001466 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_PORTSTATE))
1467 return;
1468
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001469 switch (state) {
1470 case BR_STATE_DISABLED:
Andrew Lunncca8b132015-04-02 04:06:39 +02001471 stp_state = PORT_CONTROL_STATE_DISABLED;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001472 break;
1473 case BR_STATE_BLOCKING:
1474 case BR_STATE_LISTENING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001475 stp_state = PORT_CONTROL_STATE_BLOCKING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001476 break;
1477 case BR_STATE_LEARNING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001478 stp_state = PORT_CONTROL_STATE_LEARNING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001479 break;
1480 case BR_STATE_FORWARDING:
1481 default:
Andrew Lunncca8b132015-04-02 04:06:39 +02001482 stp_state = PORT_CONTROL_STATE_FORWARDING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001483 break;
1484 }
1485
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04001486 mutex_lock(&ps->reg_lock);
Vivien Didelot553eb542016-05-13 20:38:23 -04001487 err = _mv88e6xxx_port_state(ps, port, stp_state);
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04001488 mutex_unlock(&ps->reg_lock);
Vivien Didelot553eb542016-05-13 20:38:23 -04001489
1490 if (err)
Andrew Lunnc8b09802016-06-04 21:16:57 +02001491 netdev_err(ds->ports[port].netdev,
1492 "failed to update state to %s\n",
Vivien Didelot553eb542016-05-13 20:38:23 -04001493 mv88e6xxx_port_state_names[stp_state]);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001494}
1495
Andrew Lunn158bc062016-04-28 21:24:06 -04001496static int _mv88e6xxx_port_pvid(struct mv88e6xxx_priv_state *ps, int port,
1497 u16 *new, u16 *old)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001498{
Andrew Lunn158bc062016-04-28 21:24:06 -04001499 struct dsa_switch *ds = ps->ds;
Vivien Didelot5da96032016-03-07 18:24:39 -05001500 u16 pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001501 int ret;
1502
Andrew Lunn158bc062016-04-28 21:24:06 -04001503 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_DEFAULT_VLAN);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001504 if (ret < 0)
1505 return ret;
1506
Vivien Didelot5da96032016-03-07 18:24:39 -05001507 pvid = ret & PORT_DEFAULT_VLAN_MASK;
1508
1509 if (new) {
1510 ret &= ~PORT_DEFAULT_VLAN_MASK;
1511 ret |= *new & PORT_DEFAULT_VLAN_MASK;
1512
Andrew Lunn158bc062016-04-28 21:24:06 -04001513 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Vivien Didelot5da96032016-03-07 18:24:39 -05001514 PORT_DEFAULT_VLAN, ret);
1515 if (ret < 0)
1516 return ret;
1517
Andrew Lunnc8b09802016-06-04 21:16:57 +02001518 netdev_dbg(ds->ports[port].netdev,
1519 "DefaultVID %d (was %d)\n", *new, pvid);
Vivien Didelot5da96032016-03-07 18:24:39 -05001520 }
1521
1522 if (old)
1523 *old = pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001524
1525 return 0;
1526}
1527
Andrew Lunn158bc062016-04-28 21:24:06 -04001528static int _mv88e6xxx_port_pvid_get(struct mv88e6xxx_priv_state *ps,
1529 int port, u16 *pvid)
Vivien Didelot5da96032016-03-07 18:24:39 -05001530{
Andrew Lunn158bc062016-04-28 21:24:06 -04001531 return _mv88e6xxx_port_pvid(ps, port, NULL, pvid);
Vivien Didelot5da96032016-03-07 18:24:39 -05001532}
1533
Andrew Lunn158bc062016-04-28 21:24:06 -04001534static int _mv88e6xxx_port_pvid_set(struct mv88e6xxx_priv_state *ps,
1535 int port, u16 pvid)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001536{
Andrew Lunn158bc062016-04-28 21:24:06 -04001537 return _mv88e6xxx_port_pvid(ps, port, &pvid, NULL);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001538}
1539
Andrew Lunn158bc062016-04-28 21:24:06 -04001540static int _mv88e6xxx_vtu_wait(struct mv88e6xxx_priv_state *ps)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001541{
Andrew Lunn158bc062016-04-28 21:24:06 -04001542 return _mv88e6xxx_wait(ps, REG_GLOBAL, GLOBAL_VTU_OP,
Vivien Didelot6b17e862015-08-13 12:52:18 -04001543 GLOBAL_VTU_OP_BUSY);
1544}
1545
Andrew Lunn158bc062016-04-28 21:24:06 -04001546static int _mv88e6xxx_vtu_cmd(struct mv88e6xxx_priv_state *ps, u16 op)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001547{
1548 int ret;
1549
Andrew Lunn158bc062016-04-28 21:24:06 -04001550 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_OP, op);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001551 if (ret < 0)
1552 return ret;
1553
Andrew Lunn158bc062016-04-28 21:24:06 -04001554 return _mv88e6xxx_vtu_wait(ps);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001555}
1556
Andrew Lunn158bc062016-04-28 21:24:06 -04001557static int _mv88e6xxx_vtu_stu_flush(struct mv88e6xxx_priv_state *ps)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001558{
1559 int ret;
1560
Andrew Lunn158bc062016-04-28 21:24:06 -04001561 ret = _mv88e6xxx_vtu_wait(ps);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001562 if (ret < 0)
1563 return ret;
1564
Andrew Lunn158bc062016-04-28 21:24:06 -04001565 return _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_FLUSH_ALL);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001566}
1567
Andrew Lunn158bc062016-04-28 21:24:06 -04001568static int _mv88e6xxx_vtu_stu_data_read(struct mv88e6xxx_priv_state *ps,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001569 struct mv88e6xxx_vtu_stu_entry *entry,
1570 unsigned int nibble_offset)
1571{
Vivien Didelotb8fee952015-08-13 12:52:19 -04001572 u16 regs[3];
1573 int i;
1574 int ret;
1575
1576 for (i = 0; i < 3; ++i) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001577 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001578 GLOBAL_VTU_DATA_0_3 + i);
1579 if (ret < 0)
1580 return ret;
1581
1582 regs[i] = ret;
1583 }
1584
Vivien Didelot009a2b92016-04-17 13:24:01 -04001585 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelotb8fee952015-08-13 12:52:19 -04001586 unsigned int shift = (i % 4) * 4 + nibble_offset;
1587 u16 reg = regs[i / 4];
1588
1589 entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
1590 }
1591
1592 return 0;
1593}
1594
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001595static int mv88e6xxx_vtu_data_read(struct mv88e6xxx_priv_state *ps,
1596 struct mv88e6xxx_vtu_stu_entry *entry)
1597{
1598 return _mv88e6xxx_vtu_stu_data_read(ps, entry, 0);
1599}
1600
1601static int mv88e6xxx_stu_data_read(struct mv88e6xxx_priv_state *ps,
1602 struct mv88e6xxx_vtu_stu_entry *entry)
1603{
1604 return _mv88e6xxx_vtu_stu_data_read(ps, entry, 2);
1605}
1606
Andrew Lunn158bc062016-04-28 21:24:06 -04001607static int _mv88e6xxx_vtu_stu_data_write(struct mv88e6xxx_priv_state *ps,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001608 struct mv88e6xxx_vtu_stu_entry *entry,
1609 unsigned int nibble_offset)
1610{
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001611 u16 regs[3] = { 0 };
1612 int i;
1613 int ret;
1614
Vivien Didelot009a2b92016-04-17 13:24:01 -04001615 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001616 unsigned int shift = (i % 4) * 4 + nibble_offset;
1617 u8 data = entry->data[i];
1618
1619 regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
1620 }
1621
1622 for (i = 0; i < 3; ++i) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001623 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001624 GLOBAL_VTU_DATA_0_3 + i, regs[i]);
1625 if (ret < 0)
1626 return ret;
1627 }
1628
1629 return 0;
1630}
1631
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001632static int mv88e6xxx_vtu_data_write(struct mv88e6xxx_priv_state *ps,
1633 struct mv88e6xxx_vtu_stu_entry *entry)
1634{
1635 return _mv88e6xxx_vtu_stu_data_write(ps, entry, 0);
1636}
1637
1638static int mv88e6xxx_stu_data_write(struct mv88e6xxx_priv_state *ps,
1639 struct mv88e6xxx_vtu_stu_entry *entry)
1640{
1641 return _mv88e6xxx_vtu_stu_data_write(ps, entry, 2);
1642}
1643
Andrew Lunn158bc062016-04-28 21:24:06 -04001644static int _mv88e6xxx_vtu_vid_write(struct mv88e6xxx_priv_state *ps, u16 vid)
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001645{
Andrew Lunn158bc062016-04-28 21:24:06 -04001646 return _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_VID,
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001647 vid & GLOBAL_VTU_VID_MASK);
1648}
1649
Andrew Lunn158bc062016-04-28 21:24:06 -04001650static int _mv88e6xxx_vtu_getnext(struct mv88e6xxx_priv_state *ps,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001651 struct mv88e6xxx_vtu_stu_entry *entry)
1652{
1653 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1654 int ret;
1655
Andrew Lunn158bc062016-04-28 21:24:06 -04001656 ret = _mv88e6xxx_vtu_wait(ps);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001657 if (ret < 0)
1658 return ret;
1659
Andrew Lunn158bc062016-04-28 21:24:06 -04001660 ret = _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_VTU_GET_NEXT);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001661 if (ret < 0)
1662 return ret;
1663
Andrew Lunn158bc062016-04-28 21:24:06 -04001664 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_VTU_VID);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001665 if (ret < 0)
1666 return ret;
1667
1668 next.vid = ret & GLOBAL_VTU_VID_MASK;
1669 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1670
1671 if (next.valid) {
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001672 ret = mv88e6xxx_vtu_data_read(ps, &next);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001673 if (ret < 0)
1674 return ret;
1675
Andrew Lunn158bc062016-04-28 21:24:06 -04001676 if (mv88e6xxx_has_fid_reg(ps)) {
1677 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001678 GLOBAL_VTU_FID);
1679 if (ret < 0)
1680 return ret;
1681
1682 next.fid = ret & GLOBAL_VTU_FID_MASK;
Andrew Lunn158bc062016-04-28 21:24:06 -04001683 } else if (mv88e6xxx_num_databases(ps) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001684 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1685 * VTU DBNum[3:0] are located in VTU Operation 3:0
1686 */
Andrew Lunn158bc062016-04-28 21:24:06 -04001687 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
Vivien Didelot11ea8092016-03-31 16:53:44 -04001688 GLOBAL_VTU_OP);
1689 if (ret < 0)
1690 return ret;
1691
1692 next.fid = (ret & 0xf00) >> 4;
1693 next.fid |= ret & 0xf;
Vivien Didelot2e7bd5e2016-03-31 16:53:41 -04001694 }
Vivien Didelotb8fee952015-08-13 12:52:19 -04001695
Vivien Didelotcb9b9022016-05-10 15:44:29 -04001696 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_STU)) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001697 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001698 GLOBAL_VTU_SID);
1699 if (ret < 0)
1700 return ret;
1701
1702 next.sid = ret & GLOBAL_VTU_SID_MASK;
1703 }
1704 }
1705
1706 *entry = next;
1707 return 0;
1708}
1709
Vivien Didelotf81ec902016-05-09 13:22:58 -04001710static int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
1711 struct switchdev_obj_port_vlan *vlan,
1712 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001713{
1714 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1715 struct mv88e6xxx_vtu_stu_entry next;
1716 u16 pvid;
1717 int err;
1718
Vivien Didelot54d77b52016-05-09 13:22:47 -04001719 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU))
1720 return -EOPNOTSUPP;
1721
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04001722 mutex_lock(&ps->reg_lock);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001723
Andrew Lunn158bc062016-04-28 21:24:06 -04001724 err = _mv88e6xxx_port_pvid_get(ps, port, &pvid);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001725 if (err)
1726 goto unlock;
1727
Andrew Lunn158bc062016-04-28 21:24:06 -04001728 err = _mv88e6xxx_vtu_vid_write(ps, GLOBAL_VTU_VID_MASK);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001729 if (err)
1730 goto unlock;
1731
1732 do {
Andrew Lunn158bc062016-04-28 21:24:06 -04001733 err = _mv88e6xxx_vtu_getnext(ps, &next);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001734 if (err)
1735 break;
1736
1737 if (!next.valid)
1738 break;
1739
1740 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1741 continue;
1742
1743 /* reinit and dump this VLAN obj */
Vivien Didelot57d32312016-06-20 13:13:58 -04001744 vlan->vid_begin = next.vid;
1745 vlan->vid_end = next.vid;
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001746 vlan->flags = 0;
1747
1748 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1749 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1750
1751 if (next.vid == pvid)
1752 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1753
1754 err = cb(&vlan->obj);
1755 if (err)
1756 break;
1757 } while (next.vid < GLOBAL_VTU_VID_MASK);
1758
1759unlock:
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04001760 mutex_unlock(&ps->reg_lock);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001761
1762 return err;
1763}
1764
Andrew Lunn158bc062016-04-28 21:24:06 -04001765static int _mv88e6xxx_vtu_loadpurge(struct mv88e6xxx_priv_state *ps,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001766 struct mv88e6xxx_vtu_stu_entry *entry)
1767{
Vivien Didelot11ea8092016-03-31 16:53:44 -04001768 u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001769 u16 reg = 0;
1770 int ret;
1771
Andrew Lunn158bc062016-04-28 21:24:06 -04001772 ret = _mv88e6xxx_vtu_wait(ps);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001773 if (ret < 0)
1774 return ret;
1775
1776 if (!entry->valid)
1777 goto loadpurge;
1778
1779 /* Write port member tags */
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001780 ret = mv88e6xxx_vtu_data_write(ps, entry);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001781 if (ret < 0)
1782 return ret;
1783
Vivien Didelotcb9b9022016-05-10 15:44:29 -04001784 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_STU)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001785 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Andrew Lunn158bc062016-04-28 21:24:06 -04001786 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_SID, reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001787 if (ret < 0)
1788 return ret;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001789 }
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001790
Andrew Lunn158bc062016-04-28 21:24:06 -04001791 if (mv88e6xxx_has_fid_reg(ps)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001792 reg = entry->fid & GLOBAL_VTU_FID_MASK;
Andrew Lunn158bc062016-04-28 21:24:06 -04001793 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_FID, reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001794 if (ret < 0)
1795 return ret;
Andrew Lunn158bc062016-04-28 21:24:06 -04001796 } else if (mv88e6xxx_num_databases(ps) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001797 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1798 * VTU DBNum[3:0] are located in VTU Operation 3:0
1799 */
1800 op |= (entry->fid & 0xf0) << 8;
1801 op |= entry->fid & 0xf;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001802 }
1803
1804 reg = GLOBAL_VTU_VID_VALID;
1805loadpurge:
1806 reg |= entry->vid & GLOBAL_VTU_VID_MASK;
Andrew Lunn158bc062016-04-28 21:24:06 -04001807 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_VID, reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001808 if (ret < 0)
1809 return ret;
1810
Andrew Lunn158bc062016-04-28 21:24:06 -04001811 return _mv88e6xxx_vtu_cmd(ps, op);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001812}
1813
Andrew Lunn158bc062016-04-28 21:24:06 -04001814static int _mv88e6xxx_stu_getnext(struct mv88e6xxx_priv_state *ps, u8 sid,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001815 struct mv88e6xxx_vtu_stu_entry *entry)
1816{
1817 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1818 int ret;
1819
Andrew Lunn158bc062016-04-28 21:24:06 -04001820 ret = _mv88e6xxx_vtu_wait(ps);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001821 if (ret < 0)
1822 return ret;
1823
Andrew Lunn158bc062016-04-28 21:24:06 -04001824 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_SID,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001825 sid & GLOBAL_VTU_SID_MASK);
1826 if (ret < 0)
1827 return ret;
1828
Andrew Lunn158bc062016-04-28 21:24:06 -04001829 ret = _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_STU_GET_NEXT);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001830 if (ret < 0)
1831 return ret;
1832
Andrew Lunn158bc062016-04-28 21:24:06 -04001833 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_VTU_SID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001834 if (ret < 0)
1835 return ret;
1836
1837 next.sid = ret & GLOBAL_VTU_SID_MASK;
1838
Andrew Lunn158bc062016-04-28 21:24:06 -04001839 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_VTU_VID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001840 if (ret < 0)
1841 return ret;
1842
1843 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1844
1845 if (next.valid) {
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001846 ret = mv88e6xxx_stu_data_read(ps, &next);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001847 if (ret < 0)
1848 return ret;
1849 }
1850
1851 *entry = next;
1852 return 0;
1853}
1854
Andrew Lunn158bc062016-04-28 21:24:06 -04001855static int _mv88e6xxx_stu_loadpurge(struct mv88e6xxx_priv_state *ps,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001856 struct mv88e6xxx_vtu_stu_entry *entry)
1857{
1858 u16 reg = 0;
1859 int ret;
1860
Andrew Lunn158bc062016-04-28 21:24:06 -04001861 ret = _mv88e6xxx_vtu_wait(ps);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001862 if (ret < 0)
1863 return ret;
1864
1865 if (!entry->valid)
1866 goto loadpurge;
1867
1868 /* Write port states */
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001869 ret = mv88e6xxx_stu_data_write(ps, entry);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001870 if (ret < 0)
1871 return ret;
1872
1873 reg = GLOBAL_VTU_VID_VALID;
1874loadpurge:
Andrew Lunn158bc062016-04-28 21:24:06 -04001875 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_VID, reg);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001876 if (ret < 0)
1877 return ret;
1878
1879 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Andrew Lunn158bc062016-04-28 21:24:06 -04001880 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_SID, reg);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001881 if (ret < 0)
1882 return ret;
1883
Andrew Lunn158bc062016-04-28 21:24:06 -04001884 return _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_STU_LOAD_PURGE);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001885}
1886
Andrew Lunn158bc062016-04-28 21:24:06 -04001887static int _mv88e6xxx_port_fid(struct mv88e6xxx_priv_state *ps, int port,
1888 u16 *new, u16 *old)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001889{
Andrew Lunn158bc062016-04-28 21:24:06 -04001890 struct dsa_switch *ds = ps->ds;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001891 u16 upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001892 u16 fid;
1893 int ret;
1894
Andrew Lunn158bc062016-04-28 21:24:06 -04001895 if (mv88e6xxx_num_databases(ps) == 4096)
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001896 upper_mask = 0xff;
Andrew Lunn158bc062016-04-28 21:24:06 -04001897 else if (mv88e6xxx_num_databases(ps) == 256)
Vivien Didelot11ea8092016-03-31 16:53:44 -04001898 upper_mask = 0xf;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001899 else
1900 return -EOPNOTSUPP;
1901
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001902 /* Port's default FID bits 3:0 are located in reg 0x06, offset 12 */
Andrew Lunn158bc062016-04-28 21:24:06 -04001903 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_BASE_VLAN);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001904 if (ret < 0)
1905 return ret;
1906
1907 fid = (ret & PORT_BASE_VLAN_FID_3_0_MASK) >> 12;
1908
1909 if (new) {
1910 ret &= ~PORT_BASE_VLAN_FID_3_0_MASK;
1911 ret |= (*new << 12) & PORT_BASE_VLAN_FID_3_0_MASK;
1912
Andrew Lunn158bc062016-04-28 21:24:06 -04001913 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_BASE_VLAN,
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001914 ret);
1915 if (ret < 0)
1916 return ret;
1917 }
1918
1919 /* Port's default FID bits 11:4 are located in reg 0x05, offset 0 */
Andrew Lunn158bc062016-04-28 21:24:06 -04001920 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_CONTROL_1);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001921 if (ret < 0)
1922 return ret;
1923
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001924 fid |= (ret & upper_mask) << 4;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001925
1926 if (new) {
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001927 ret &= ~upper_mask;
1928 ret |= (*new >> 4) & upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001929
Andrew Lunn158bc062016-04-28 21:24:06 -04001930 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL_1,
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001931 ret);
1932 if (ret < 0)
1933 return ret;
1934
Andrew Lunnc8b09802016-06-04 21:16:57 +02001935 netdev_dbg(ds->ports[port].netdev,
1936 "FID %d (was %d)\n", *new, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001937 }
1938
1939 if (old)
1940 *old = fid;
1941
1942 return 0;
1943}
1944
Andrew Lunn158bc062016-04-28 21:24:06 -04001945static int _mv88e6xxx_port_fid_get(struct mv88e6xxx_priv_state *ps,
1946 int port, u16 *fid)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001947{
Andrew Lunn158bc062016-04-28 21:24:06 -04001948 return _mv88e6xxx_port_fid(ps, port, NULL, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001949}
1950
Andrew Lunn158bc062016-04-28 21:24:06 -04001951static int _mv88e6xxx_port_fid_set(struct mv88e6xxx_priv_state *ps,
1952 int port, u16 fid)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001953{
Andrew Lunn158bc062016-04-28 21:24:06 -04001954 return _mv88e6xxx_port_fid(ps, port, &fid, NULL);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001955}
1956
Andrew Lunn158bc062016-04-28 21:24:06 -04001957static int _mv88e6xxx_fid_new(struct mv88e6xxx_priv_state *ps, u16 *fid)
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001958{
1959 DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
1960 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001961 int i, err;
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001962
1963 bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);
1964
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001965 /* Set every FID bit used by the (un)bridged ports */
Vivien Didelot009a2b92016-04-17 13:24:01 -04001966 for (i = 0; i < ps->info->num_ports; ++i) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001967 err = _mv88e6xxx_port_fid_get(ps, i, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001968 if (err)
1969 return err;
1970
1971 set_bit(*fid, fid_bitmap);
1972 }
1973
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001974 /* Set every FID bit used by the VLAN entries */
Andrew Lunn158bc062016-04-28 21:24:06 -04001975 err = _mv88e6xxx_vtu_vid_write(ps, GLOBAL_VTU_VID_MASK);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001976 if (err)
1977 return err;
1978
1979 do {
Andrew Lunn158bc062016-04-28 21:24:06 -04001980 err = _mv88e6xxx_vtu_getnext(ps, &vlan);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001981 if (err)
1982 return err;
1983
1984 if (!vlan.valid)
1985 break;
1986
1987 set_bit(vlan.fid, fid_bitmap);
1988 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
1989
1990 /* The reset value 0x000 is used to indicate that multiple address
1991 * databases are not needed. Return the next positive available.
1992 */
1993 *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1);
Andrew Lunn158bc062016-04-28 21:24:06 -04001994 if (unlikely(*fid >= mv88e6xxx_num_databases(ps)))
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001995 return -ENOSPC;
1996
1997 /* Clear the database */
Andrew Lunn158bc062016-04-28 21:24:06 -04001998 return _mv88e6xxx_atu_flush(ps, *fid, true);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001999}
2000
Andrew Lunn158bc062016-04-28 21:24:06 -04002001static int _mv88e6xxx_vtu_new(struct mv88e6xxx_priv_state *ps, u16 vid,
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05002002 struct mv88e6xxx_vtu_stu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002003{
Andrew Lunn158bc062016-04-28 21:24:06 -04002004 struct dsa_switch *ds = ps->ds;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002005 struct mv88e6xxx_vtu_stu_entry vlan = {
2006 .valid = true,
2007 .vid = vid,
2008 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002009 int i, err;
2010
Andrew Lunn158bc062016-04-28 21:24:06 -04002011 err = _mv88e6xxx_fid_new(ps, &vlan.fid);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002012 if (err)
2013 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002014
Vivien Didelot3d131f02015-11-03 10:52:52 -05002015 /* exclude all ports except the CPU and DSA ports */
Vivien Didelot009a2b92016-04-17 13:24:01 -04002016 for (i = 0; i < ps->info->num_ports; ++i)
Vivien Didelot3d131f02015-11-03 10:52:52 -05002017 vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)
2018 ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED
2019 : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002020
Andrew Lunn158bc062016-04-28 21:24:06 -04002021 if (mv88e6xxx_6097_family(ps) || mv88e6xxx_6165_family(ps) ||
2022 mv88e6xxx_6351_family(ps) || mv88e6xxx_6352_family(ps)) {
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002023 struct mv88e6xxx_vtu_stu_entry vstp;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002024
2025 /* Adding a VTU entry requires a valid STU entry. As VSTP is not
2026 * implemented, only one STU entry is needed to cover all VTU
2027 * entries. Thus, validate the SID 0.
2028 */
2029 vlan.sid = 0;
Andrew Lunn158bc062016-04-28 21:24:06 -04002030 err = _mv88e6xxx_stu_getnext(ps, GLOBAL_VTU_SID_MASK, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002031 if (err)
2032 return err;
2033
2034 if (vstp.sid != vlan.sid || !vstp.valid) {
2035 memset(&vstp, 0, sizeof(vstp));
2036 vstp.valid = true;
2037 vstp.sid = vlan.sid;
2038
Andrew Lunn158bc062016-04-28 21:24:06 -04002039 err = _mv88e6xxx_stu_loadpurge(ps, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002040 if (err)
2041 return err;
2042 }
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002043 }
2044
2045 *entry = vlan;
2046 return 0;
2047}
2048
Andrew Lunn158bc062016-04-28 21:24:06 -04002049static int _mv88e6xxx_vtu_get(struct mv88e6xxx_priv_state *ps, u16 vid,
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05002050 struct mv88e6xxx_vtu_stu_entry *entry, bool creat)
2051{
2052 int err;
2053
2054 if (!vid)
2055 return -EINVAL;
2056
Andrew Lunn158bc062016-04-28 21:24:06 -04002057 err = _mv88e6xxx_vtu_vid_write(ps, vid - 1);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05002058 if (err)
2059 return err;
2060
Andrew Lunn158bc062016-04-28 21:24:06 -04002061 err = _mv88e6xxx_vtu_getnext(ps, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05002062 if (err)
2063 return err;
2064
2065 if (entry->vid != vid || !entry->valid) {
2066 if (!creat)
2067 return -EOPNOTSUPP;
2068 /* -ENOENT would've been more appropriate, but switchdev expects
2069 * -EOPNOTSUPP to inform bridge about an eventual software VLAN.
2070 */
2071
Andrew Lunn158bc062016-04-28 21:24:06 -04002072 err = _mv88e6xxx_vtu_new(ps, vid, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05002073 }
2074
2075 return err;
2076}
2077
Vivien Didelotda9c3592016-02-12 12:09:40 -05002078static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
2079 u16 vid_begin, u16 vid_end)
2080{
2081 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2082 struct mv88e6xxx_vtu_stu_entry vlan;
2083 int i, err;
2084
2085 if (!vid_begin)
2086 return -EOPNOTSUPP;
2087
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04002088 mutex_lock(&ps->reg_lock);
Vivien Didelotda9c3592016-02-12 12:09:40 -05002089
Andrew Lunn158bc062016-04-28 21:24:06 -04002090 err = _mv88e6xxx_vtu_vid_write(ps, vid_begin - 1);
Vivien Didelotda9c3592016-02-12 12:09:40 -05002091 if (err)
2092 goto unlock;
2093
2094 do {
Andrew Lunn158bc062016-04-28 21:24:06 -04002095 err = _mv88e6xxx_vtu_getnext(ps, &vlan);
Vivien Didelotda9c3592016-02-12 12:09:40 -05002096 if (err)
2097 goto unlock;
2098
2099 if (!vlan.valid)
2100 break;
2101
2102 if (vlan.vid > vid_end)
2103 break;
2104
Vivien Didelot009a2b92016-04-17 13:24:01 -04002105 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelotda9c3592016-02-12 12:09:40 -05002106 if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i))
2107 continue;
2108
2109 if (vlan.data[i] ==
2110 GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
2111 continue;
2112
2113 if (ps->ports[i].bridge_dev ==
2114 ps->ports[port].bridge_dev)
2115 break; /* same bridge, check next VLAN */
2116
Andrew Lunnc8b09802016-06-04 21:16:57 +02002117 netdev_warn(ds->ports[port].netdev,
Vivien Didelotda9c3592016-02-12 12:09:40 -05002118 "hardware VLAN %d already used by %s\n",
2119 vlan.vid,
2120 netdev_name(ps->ports[i].bridge_dev));
2121 err = -EOPNOTSUPP;
2122 goto unlock;
2123 }
2124 } while (vlan.vid < vid_end);
2125
2126unlock:
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04002127 mutex_unlock(&ps->reg_lock);
Vivien Didelotda9c3592016-02-12 12:09:40 -05002128
2129 return err;
2130}
2131
Vivien Didelot214cdb92016-02-26 13:16:08 -05002132static const char * const mv88e6xxx_port_8021q_mode_names[] = {
2133 [PORT_CONTROL_2_8021Q_DISABLED] = "Disabled",
2134 [PORT_CONTROL_2_8021Q_FALLBACK] = "Fallback",
2135 [PORT_CONTROL_2_8021Q_CHECK] = "Check",
2136 [PORT_CONTROL_2_8021Q_SECURE] = "Secure",
2137};
2138
Vivien Didelotf81ec902016-05-09 13:22:58 -04002139static int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port,
2140 bool vlan_filtering)
Vivien Didelot214cdb92016-02-26 13:16:08 -05002141{
2142 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2143 u16 old, new = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE :
2144 PORT_CONTROL_2_8021Q_DISABLED;
2145 int ret;
2146
Vivien Didelot54d77b52016-05-09 13:22:47 -04002147 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU))
2148 return -EOPNOTSUPP;
2149
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04002150 mutex_lock(&ps->reg_lock);
Vivien Didelot214cdb92016-02-26 13:16:08 -05002151
Andrew Lunn158bc062016-04-28 21:24:06 -04002152 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_CONTROL_2);
Vivien Didelot214cdb92016-02-26 13:16:08 -05002153 if (ret < 0)
2154 goto unlock;
2155
2156 old = ret & PORT_CONTROL_2_8021Q_MASK;
2157
Vivien Didelot5220ef12016-03-07 18:24:52 -05002158 if (new != old) {
2159 ret &= ~PORT_CONTROL_2_8021Q_MASK;
2160 ret |= new & PORT_CONTROL_2_8021Q_MASK;
Vivien Didelot214cdb92016-02-26 13:16:08 -05002161
Andrew Lunn158bc062016-04-28 21:24:06 -04002162 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL_2,
Vivien Didelot5220ef12016-03-07 18:24:52 -05002163 ret);
2164 if (ret < 0)
2165 goto unlock;
Vivien Didelot214cdb92016-02-26 13:16:08 -05002166
Andrew Lunnc8b09802016-06-04 21:16:57 +02002167 netdev_dbg(ds->ports[port].netdev, "802.1Q Mode %s (was %s)\n",
Vivien Didelot5220ef12016-03-07 18:24:52 -05002168 mv88e6xxx_port_8021q_mode_names[new],
2169 mv88e6xxx_port_8021q_mode_names[old]);
2170 }
2171
2172 ret = 0;
Vivien Didelot214cdb92016-02-26 13:16:08 -05002173unlock:
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04002174 mutex_unlock(&ps->reg_lock);
Vivien Didelot214cdb92016-02-26 13:16:08 -05002175
2176 return ret;
2177}
2178
Vivien Didelot57d32312016-06-20 13:13:58 -04002179static int
2180mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
2181 const struct switchdev_obj_port_vlan *vlan,
2182 struct switchdev_trans *trans)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002183{
Vivien Didelot54d77b52016-05-09 13:22:47 -04002184 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelotda9c3592016-02-12 12:09:40 -05002185 int err;
2186
Vivien Didelot54d77b52016-05-09 13:22:47 -04002187 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU))
2188 return -EOPNOTSUPP;
2189
Vivien Didelotda9c3592016-02-12 12:09:40 -05002190 /* If the requested port doesn't belong to the same bridge as the VLAN
2191 * members, do not support it (yet) and fallback to software VLAN.
2192 */
2193 err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin,
2194 vlan->vid_end);
2195 if (err)
2196 return err;
2197
Vivien Didelot76e398a2015-11-01 12:33:55 -05002198 /* We don't need any dynamic resource from the kernel (yet),
2199 * so skip the prepare phase.
2200 */
2201 return 0;
2202}
2203
Andrew Lunn158bc062016-04-28 21:24:06 -04002204static int _mv88e6xxx_port_vlan_add(struct mv88e6xxx_priv_state *ps, int port,
2205 u16 vid, bool untagged)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002206{
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002207 struct mv88e6xxx_vtu_stu_entry vlan;
2208 int err;
2209
Andrew Lunn158bc062016-04-28 21:24:06 -04002210 err = _mv88e6xxx_vtu_get(ps, vid, &vlan, true);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002211 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002212 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002213
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002214 vlan.data[port] = untagged ?
2215 GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
2216 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
2217
Andrew Lunn158bc062016-04-28 21:24:06 -04002218 return _mv88e6xxx_vtu_loadpurge(ps, &vlan);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002219}
2220
Vivien Didelotf81ec902016-05-09 13:22:58 -04002221static void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
2222 const struct switchdev_obj_port_vlan *vlan,
2223 struct switchdev_trans *trans)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002224{
2225 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2226 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
2227 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
2228 u16 vid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05002229
Vivien Didelot54d77b52016-05-09 13:22:47 -04002230 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU))
2231 return;
2232
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04002233 mutex_lock(&ps->reg_lock);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002234
Vivien Didelot4d5770b2016-04-06 11:55:05 -04002235 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
Andrew Lunn158bc062016-04-28 21:24:06 -04002236 if (_mv88e6xxx_port_vlan_add(ps, port, vid, untagged))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002237 netdev_err(ds->ports[port].netdev,
2238 "failed to add VLAN %d%c\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04002239 vid, untagged ? 'u' : 't');
Vivien Didelot76e398a2015-11-01 12:33:55 -05002240
Andrew Lunn158bc062016-04-28 21:24:06 -04002241 if (pvid && _mv88e6xxx_port_pvid_set(ps, port, vlan->vid_end))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002242 netdev_err(ds->ports[port].netdev, "failed to set PVID %d\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04002243 vlan->vid_end);
2244
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04002245 mutex_unlock(&ps->reg_lock);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002246}
2247
Andrew Lunn158bc062016-04-28 21:24:06 -04002248static int _mv88e6xxx_port_vlan_del(struct mv88e6xxx_priv_state *ps,
2249 int port, u16 vid)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002250{
Andrew Lunn158bc062016-04-28 21:24:06 -04002251 struct dsa_switch *ds = ps->ds;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002252 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002253 int i, err;
2254
Andrew Lunn158bc062016-04-28 21:24:06 -04002255 err = _mv88e6xxx_vtu_get(ps, vid, &vlan, false);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002256 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002257 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002258
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05002259 /* Tell switchdev if this VLAN is handled in software */
2260 if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
Vivien Didelot3c06f082016-02-05 14:04:39 -05002261 return -EOPNOTSUPP;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002262
2263 vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
2264
2265 /* keep the VLAN unless all ports are excluded */
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002266 vlan.valid = false;
Vivien Didelot009a2b92016-04-17 13:24:01 -04002267 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelot3d131f02015-11-03 10:52:52 -05002268 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002269 continue;
2270
2271 if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002272 vlan.valid = true;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002273 break;
2274 }
2275 }
2276
Andrew Lunn158bc062016-04-28 21:24:06 -04002277 err = _mv88e6xxx_vtu_loadpurge(ps, &vlan);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002278 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002279 return err;
2280
Andrew Lunn158bc062016-04-28 21:24:06 -04002281 return _mv88e6xxx_atu_remove(ps, vlan.fid, port, false);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002282}
2283
Vivien Didelotf81ec902016-05-09 13:22:58 -04002284static int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
2285 const struct switchdev_obj_port_vlan *vlan)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002286{
2287 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2288 u16 pvid, vid;
2289 int err = 0;
2290
Vivien Didelot54d77b52016-05-09 13:22:47 -04002291 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU))
2292 return -EOPNOTSUPP;
2293
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04002294 mutex_lock(&ps->reg_lock);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002295
Andrew Lunn158bc062016-04-28 21:24:06 -04002296 err = _mv88e6xxx_port_pvid_get(ps, port, &pvid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002297 if (err)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002298 goto unlock;
2299
Vivien Didelot76e398a2015-11-01 12:33:55 -05002300 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002301 err = _mv88e6xxx_port_vlan_del(ps, port, vid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002302 if (err)
2303 goto unlock;
2304
2305 if (vid == pvid) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002306 err = _mv88e6xxx_port_pvid_set(ps, port, 0);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002307 if (err)
2308 goto unlock;
2309 }
2310 }
2311
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002312unlock:
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04002313 mutex_unlock(&ps->reg_lock);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002314
2315 return err;
2316}
2317
Andrew Lunn158bc062016-04-28 21:24:06 -04002318static int _mv88e6xxx_atu_mac_write(struct mv88e6xxx_priv_state *ps,
Vivien Didelotc5723ac2015-08-10 09:09:48 -04002319 const unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002320{
2321 int i, ret;
2322
2323 for (i = 0; i < 3; i++) {
Andrew Lunncca8b132015-04-02 04:06:39 +02002324 ret = _mv88e6xxx_reg_write(
Andrew Lunn158bc062016-04-28 21:24:06 -04002325 ps, REG_GLOBAL, GLOBAL_ATU_MAC_01 + i,
Andrew Lunncca8b132015-04-02 04:06:39 +02002326 (addr[i * 2] << 8) | addr[i * 2 + 1]);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002327 if (ret < 0)
2328 return ret;
2329 }
2330
2331 return 0;
2332}
2333
Andrew Lunn158bc062016-04-28 21:24:06 -04002334static int _mv88e6xxx_atu_mac_read(struct mv88e6xxx_priv_state *ps,
2335 unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002336{
2337 int i, ret;
2338
2339 for (i = 0; i < 3; i++) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002340 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
Andrew Lunncca8b132015-04-02 04:06:39 +02002341 GLOBAL_ATU_MAC_01 + i);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002342 if (ret < 0)
2343 return ret;
2344 addr[i * 2] = ret >> 8;
2345 addr[i * 2 + 1] = ret & 0xff;
2346 }
2347
2348 return 0;
2349}
2350
Andrew Lunn158bc062016-04-28 21:24:06 -04002351static int _mv88e6xxx_atu_load(struct mv88e6xxx_priv_state *ps,
Vivien Didelotfd231c82015-08-10 09:09:50 -04002352 struct mv88e6xxx_atu_entry *entry)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002353{
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002354 int ret;
2355
Andrew Lunn158bc062016-04-28 21:24:06 -04002356 ret = _mv88e6xxx_atu_wait(ps);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002357 if (ret < 0)
2358 return ret;
2359
Andrew Lunn158bc062016-04-28 21:24:06 -04002360 ret = _mv88e6xxx_atu_mac_write(ps, entry->mac);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002361 if (ret < 0)
2362 return ret;
2363
Andrew Lunn158bc062016-04-28 21:24:06 -04002364 ret = _mv88e6xxx_atu_data_write(ps, entry);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002365 if (ret < 0)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002366 return ret;
2367
Andrew Lunn158bc062016-04-28 21:24:06 -04002368 return _mv88e6xxx_atu_cmd(ps, entry->fid, GLOBAL_ATU_OP_LOAD_DB);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002369}
David S. Millercdf09692015-08-11 12:00:37 -07002370
Andrew Lunn158bc062016-04-28 21:24:06 -04002371static int _mv88e6xxx_port_fdb_load(struct mv88e6xxx_priv_state *ps, int port,
Vivien Didelotfd231c82015-08-10 09:09:50 -04002372 const unsigned char *addr, u16 vid,
2373 u8 state)
2374{
2375 struct mv88e6xxx_atu_entry entry = { 0 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002376 struct mv88e6xxx_vtu_stu_entry vlan;
2377 int err;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002378
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002379 /* Null VLAN ID corresponds to the port private database */
2380 if (vid == 0)
Andrew Lunn158bc062016-04-28 21:24:06 -04002381 err = _mv88e6xxx_port_fid_get(ps, port, &vlan.fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002382 else
Andrew Lunn158bc062016-04-28 21:24:06 -04002383 err = _mv88e6xxx_vtu_get(ps, vid, &vlan, false);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002384 if (err)
2385 return err;
2386
2387 entry.fid = vlan.fid;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002388 entry.state = state;
2389 ether_addr_copy(entry.mac, addr);
2390 if (state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2391 entry.trunk = false;
2392 entry.portv_trunkid = BIT(port);
2393 }
2394
Andrew Lunn158bc062016-04-28 21:24:06 -04002395 return _mv88e6xxx_atu_load(ps, &entry);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002396}
2397
Vivien Didelotf81ec902016-05-09 13:22:58 -04002398static int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
2399 const struct switchdev_obj_port_fdb *fdb,
2400 struct switchdev_trans *trans)
Vivien Didelot146a3202015-10-08 11:35:12 -04002401{
Vivien Didelot2672f822016-05-09 13:22:48 -04002402 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2403
2404 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_ATU))
2405 return -EOPNOTSUPP;
2406
Vivien Didelot146a3202015-10-08 11:35:12 -04002407 /* We don't need any dynamic resource from the kernel (yet),
2408 * so skip the prepare phase.
2409 */
2410 return 0;
2411}
2412
Vivien Didelotf81ec902016-05-09 13:22:58 -04002413static void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
2414 const struct switchdev_obj_port_fdb *fdb,
2415 struct switchdev_trans *trans)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002416{
Vivien Didelot1f36faf2015-10-08 11:35:13 -04002417 int state = is_multicast_ether_addr(fdb->addr) ?
David S. Millercdf09692015-08-11 12:00:37 -07002418 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2419 GLOBAL_ATU_DATA_STATE_UC_STATIC;
2420 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelot6630e232015-08-06 01:44:07 -04002421
Vivien Didelot2672f822016-05-09 13:22:48 -04002422 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_ATU))
2423 return;
2424
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04002425 mutex_lock(&ps->reg_lock);
Andrew Lunn158bc062016-04-28 21:24:06 -04002426 if (_mv88e6xxx_port_fdb_load(ps, port, fdb->addr, fdb->vid, state))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002427 netdev_err(ds->ports[port].netdev,
2428 "failed to load MAC address\n");
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04002429 mutex_unlock(&ps->reg_lock);
David S. Millercdf09692015-08-11 12:00:37 -07002430}
2431
Vivien Didelotf81ec902016-05-09 13:22:58 -04002432static int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
2433 const struct switchdev_obj_port_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -07002434{
2435 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2436 int ret;
2437
Vivien Didelot2672f822016-05-09 13:22:48 -04002438 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_ATU))
2439 return -EOPNOTSUPP;
2440
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04002441 mutex_lock(&ps->reg_lock);
Andrew Lunn158bc062016-04-28 21:24:06 -04002442 ret = _mv88e6xxx_port_fdb_load(ps, port, fdb->addr, fdb->vid,
David S. Millercdf09692015-08-11 12:00:37 -07002443 GLOBAL_ATU_DATA_STATE_UNUSED);
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04002444 mutex_unlock(&ps->reg_lock);
David S. Millercdf09692015-08-11 12:00:37 -07002445
2446 return ret;
2447}
2448
Andrew Lunn158bc062016-04-28 21:24:06 -04002449static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_priv_state *ps, u16 fid,
Vivien Didelot1d194042015-08-10 09:09:51 -04002450 struct mv88e6xxx_atu_entry *entry)
David S. Millercdf09692015-08-11 12:00:37 -07002451{
Vivien Didelot1d194042015-08-10 09:09:51 -04002452 struct mv88e6xxx_atu_entry next = { 0 };
2453 int ret;
2454
2455 next.fid = fid;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002456
Andrew Lunn158bc062016-04-28 21:24:06 -04002457 ret = _mv88e6xxx_atu_wait(ps);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002458 if (ret < 0)
2459 return ret;
2460
Andrew Lunn158bc062016-04-28 21:24:06 -04002461 ret = _mv88e6xxx_atu_cmd(ps, fid, GLOBAL_ATU_OP_GET_NEXT_DB);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002462 if (ret < 0)
2463 return ret;
2464
Andrew Lunn158bc062016-04-28 21:24:06 -04002465 ret = _mv88e6xxx_atu_mac_read(ps, next.mac);
Vivien Didelot1d194042015-08-10 09:09:51 -04002466 if (ret < 0)
2467 return ret;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002468
Andrew Lunn158bc062016-04-28 21:24:06 -04002469 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_ATU_DATA);
Vivien Didelot1d194042015-08-10 09:09:51 -04002470 if (ret < 0)
2471 return ret;
2472
2473 next.state = ret & GLOBAL_ATU_DATA_STATE_MASK;
2474 if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2475 unsigned int mask, shift;
2476
2477 if (ret & GLOBAL_ATU_DATA_TRUNK) {
2478 next.trunk = true;
2479 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
2480 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
2481 } else {
2482 next.trunk = false;
2483 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
2484 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
2485 }
2486
2487 next.portv_trunkid = (ret & mask) >> shift;
2488 }
2489
2490 *entry = next;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002491 return 0;
2492}
2493
Andrew Lunn158bc062016-04-28 21:24:06 -04002494static int _mv88e6xxx_port_fdb_dump_one(struct mv88e6xxx_priv_state *ps,
2495 u16 fid, u16 vid, int port,
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002496 struct switchdev_obj_port_fdb *fdb,
2497 int (*cb)(struct switchdev_obj *obj))
2498{
2499 struct mv88e6xxx_atu_entry addr = {
2500 .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2501 };
2502 int err;
2503
Andrew Lunn158bc062016-04-28 21:24:06 -04002504 err = _mv88e6xxx_atu_mac_write(ps, addr.mac);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002505 if (err)
2506 return err;
2507
2508 do {
Andrew Lunn158bc062016-04-28 21:24:06 -04002509 err = _mv88e6xxx_atu_getnext(ps, fid, &addr);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002510 if (err)
2511 break;
2512
2513 if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2514 break;
2515
2516 if (!addr.trunk && addr.portv_trunkid & BIT(port)) {
2517 bool is_static = addr.state ==
2518 (is_multicast_ether_addr(addr.mac) ?
2519 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2520 GLOBAL_ATU_DATA_STATE_UC_STATIC);
2521
2522 fdb->vid = vid;
2523 ether_addr_copy(fdb->addr, addr.mac);
2524 fdb->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
2525
2526 err = cb(&fdb->obj);
2527 if (err)
2528 break;
2529 }
2530 } while (!is_broadcast_ether_addr(addr.mac));
2531
2532 return err;
2533}
2534
Vivien Didelotf81ec902016-05-09 13:22:58 -04002535static int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
2536 struct switchdev_obj_port_fdb *fdb,
2537 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotf33475b2015-10-22 09:34:41 -04002538{
2539 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2540 struct mv88e6xxx_vtu_stu_entry vlan = {
2541 .vid = GLOBAL_VTU_VID_MASK, /* all ones */
2542 };
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002543 u16 fid;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002544 int err;
2545
Vivien Didelot2672f822016-05-09 13:22:48 -04002546 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_ATU))
2547 return -EOPNOTSUPP;
2548
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04002549 mutex_lock(&ps->reg_lock);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002550
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002551 /* Dump port's default Filtering Information Database (VLAN ID 0) */
Andrew Lunn158bc062016-04-28 21:24:06 -04002552 err = _mv88e6xxx_port_fid_get(ps, port, &fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002553 if (err)
2554 goto unlock;
2555
Andrew Lunn158bc062016-04-28 21:24:06 -04002556 err = _mv88e6xxx_port_fdb_dump_one(ps, fid, 0, port, fdb, cb);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002557 if (err)
2558 goto unlock;
2559
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002560 /* Dump VLANs' Filtering Information Databases */
Andrew Lunn158bc062016-04-28 21:24:06 -04002561 err = _mv88e6xxx_vtu_vid_write(ps, vlan.vid);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002562 if (err)
2563 goto unlock;
2564
2565 do {
Andrew Lunn158bc062016-04-28 21:24:06 -04002566 err = _mv88e6xxx_vtu_getnext(ps, &vlan);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002567 if (err)
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002568 break;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002569
2570 if (!vlan.valid)
2571 break;
2572
Andrew Lunn158bc062016-04-28 21:24:06 -04002573 err = _mv88e6xxx_port_fdb_dump_one(ps, vlan.fid, vlan.vid, port,
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002574 fdb, cb);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002575 if (err)
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002576 break;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002577 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
2578
2579unlock:
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04002580 mutex_unlock(&ps->reg_lock);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002581
2582 return err;
2583}
2584
Vivien Didelotf81ec902016-05-09 13:22:58 -04002585static int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
2586 struct net_device *bridge)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002587{
Vivien Didelota6692752016-02-12 12:09:39 -05002588 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Colin Ian King1d9619d2016-04-25 23:11:22 +01002589 int i, err = 0;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002590
Vivien Didelot936f2342016-05-09 13:22:46 -04002591 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VLANTABLE))
2592 return -EOPNOTSUPP;
2593
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04002594 mutex_lock(&ps->reg_lock);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002595
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002596 /* Assign the bridge and remap each port's VLANTable */
Vivien Didelota6692752016-02-12 12:09:39 -05002597 ps->ports[port].bridge_dev = bridge;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002598
Vivien Didelot009a2b92016-04-17 13:24:01 -04002599 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002600 if (ps->ports[i].bridge_dev == bridge) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002601 err = _mv88e6xxx_port_based_vlan_map(ps, i);
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002602 if (err)
2603 break;
2604 }
2605 }
2606
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04002607 mutex_unlock(&ps->reg_lock);
Vivien Didelota6692752016-02-12 12:09:39 -05002608
Vivien Didelot466dfa02016-02-26 13:16:05 -05002609 return err;
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002610}
2611
Vivien Didelotf81ec902016-05-09 13:22:58 -04002612static void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002613{
Vivien Didelota6692752016-02-12 12:09:39 -05002614 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002615 struct net_device *bridge = ps->ports[port].bridge_dev;
Vivien Didelot16bfa702016-03-13 16:21:33 -04002616 int i;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002617
Vivien Didelot936f2342016-05-09 13:22:46 -04002618 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VLANTABLE))
2619 return;
2620
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04002621 mutex_lock(&ps->reg_lock);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002622
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002623 /* Unassign the bridge and remap each port's VLANTable */
Vivien Didelota6692752016-02-12 12:09:39 -05002624 ps->ports[port].bridge_dev = NULL;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002625
Vivien Didelot009a2b92016-04-17 13:24:01 -04002626 for (i = 0; i < ps->info->num_ports; ++i)
Vivien Didelot16bfa702016-03-13 16:21:33 -04002627 if (i == port || ps->ports[i].bridge_dev == bridge)
Andrew Lunn158bc062016-04-28 21:24:06 -04002628 if (_mv88e6xxx_port_based_vlan_map(ps, i))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002629 netdev_warn(ds->ports[i].netdev,
2630 "failed to remap\n");
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002631
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04002632 mutex_unlock(&ps->reg_lock);
Vivien Didelot66d9cd02016-02-05 14:07:14 -05002633}
2634
Andrew Lunn03a4a542016-06-04 21:17:05 +02002635static int _mv88e6xxx_mdio_page_write(struct mv88e6xxx_priv_state *ps,
2636 int port, int page, int reg, int val)
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002637{
2638 int ret;
2639
Andrew Lunn03a4a542016-06-04 21:17:05 +02002640 ret = mv88e6xxx_mdio_write_indirect(ps, port, 0x16, page);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002641 if (ret < 0)
2642 goto restore_page_0;
2643
Andrew Lunn03a4a542016-06-04 21:17:05 +02002644 ret = mv88e6xxx_mdio_write_indirect(ps, port, reg, val);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002645restore_page_0:
Andrew Lunn03a4a542016-06-04 21:17:05 +02002646 mv88e6xxx_mdio_write_indirect(ps, port, 0x16, 0x0);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002647
2648 return ret;
2649}
2650
Andrew Lunn03a4a542016-06-04 21:17:05 +02002651static int _mv88e6xxx_mdio_page_read(struct mv88e6xxx_priv_state *ps,
2652 int port, int page, int reg)
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002653{
2654 int ret;
2655
Andrew Lunn03a4a542016-06-04 21:17:05 +02002656 ret = mv88e6xxx_mdio_write_indirect(ps, port, 0x16, page);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002657 if (ret < 0)
2658 goto restore_page_0;
2659
Andrew Lunn03a4a542016-06-04 21:17:05 +02002660 ret = mv88e6xxx_mdio_read_indirect(ps, port, reg);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002661restore_page_0:
Andrew Lunn03a4a542016-06-04 21:17:05 +02002662 mv88e6xxx_mdio_write_indirect(ps, port, 0x16, 0x0);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002663
2664 return ret;
2665}
2666
Vivien Didelot552238b2016-05-09 13:22:49 -04002667static int mv88e6xxx_switch_reset(struct mv88e6xxx_priv_state *ps)
2668{
2669 bool ppu_active = mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU_ACTIVE);
2670 u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
Andrew Lunn52638f72016-05-10 23:27:22 +02002671 struct gpio_desc *gpiod = ps->reset;
Vivien Didelot552238b2016-05-09 13:22:49 -04002672 unsigned long timeout;
2673 int ret;
2674 int i;
2675
2676 /* Set all ports to the disabled state. */
2677 for (i = 0; i < ps->info->num_ports; i++) {
2678 ret = _mv88e6xxx_reg_read(ps, REG_PORT(i), PORT_CONTROL);
2679 if (ret < 0)
2680 return ret;
2681
2682 ret = _mv88e6xxx_reg_write(ps, REG_PORT(i), PORT_CONTROL,
2683 ret & 0xfffc);
2684 if (ret)
2685 return ret;
2686 }
2687
2688 /* Wait for transmit queues to drain. */
2689 usleep_range(2000, 4000);
2690
2691 /* If there is a gpio connected to the reset pin, toggle it */
2692 if (gpiod) {
2693 gpiod_set_value_cansleep(gpiod, 1);
2694 usleep_range(10000, 20000);
2695 gpiod_set_value_cansleep(gpiod, 0);
2696 usleep_range(10000, 20000);
2697 }
2698
2699 /* Reset the switch. Keep the PPU active if requested. The PPU
2700 * needs to be active to support indirect phy register access
2701 * through global registers 0x18 and 0x19.
2702 */
2703 if (ppu_active)
2704 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, 0x04, 0xc000);
2705 else
2706 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, 0x04, 0xc400);
2707 if (ret)
2708 return ret;
2709
2710 /* Wait up to one second for reset to complete. */
2711 timeout = jiffies + 1 * HZ;
2712 while (time_before(jiffies, timeout)) {
2713 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, 0x00);
2714 if (ret < 0)
2715 return ret;
2716
2717 if ((ret & is_reset) == is_reset)
2718 break;
2719 usleep_range(1000, 2000);
2720 }
2721 if (time_after(jiffies, timeout))
2722 ret = -ETIMEDOUT;
2723 else
2724 ret = 0;
2725
2726 return ret;
2727}
2728
Andrew Lunn158bc062016-04-28 21:24:06 -04002729static int mv88e6xxx_power_on_serdes(struct mv88e6xxx_priv_state *ps)
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002730{
2731 int ret;
2732
Andrew Lunn03a4a542016-06-04 21:17:05 +02002733 ret = _mv88e6xxx_mdio_page_read(ps, REG_FIBER_SERDES,
2734 PAGE_FIBER_SERDES, MII_BMCR);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002735 if (ret < 0)
2736 return ret;
2737
2738 if (ret & BMCR_PDOWN) {
2739 ret &= ~BMCR_PDOWN;
Andrew Lunn03a4a542016-06-04 21:17:05 +02002740 ret = _mv88e6xxx_mdio_page_write(ps, REG_FIBER_SERDES,
2741 PAGE_FIBER_SERDES, MII_BMCR,
2742 ret);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002743 }
2744
2745 return ret;
2746}
2747
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002748static int mv88e6xxx_setup_port(struct mv88e6xxx_priv_state *ps, int port)
Guenter Roeckd827e882015-03-26 18:36:29 -07002749{
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002750 struct dsa_switch *ds = ps->ds;
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002751 int ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002752 u16 reg;
Guenter Roeckd827e882015-03-26 18:36:29 -07002753
Andrew Lunn158bc062016-04-28 21:24:06 -04002754 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2755 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2756 mv88e6xxx_6185_family(ps) || mv88e6xxx_6095_family(ps) ||
2757 mv88e6xxx_6065_family(ps) || mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002758 /* MAC Forcing register: don't force link, speed,
2759 * duplex or flow control state to any particular
2760 * values on physical ports, but force the CPU port
2761 * and all DSA ports to their maximum bandwidth and
2762 * full duplex.
2763 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002764 reg = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_PCS_CTRL);
Andrew Lunn60045cb2015-08-17 23:52:51 +02002765 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
Russell King53adc9e2015-09-21 21:42:59 +01002766 reg &= ~PORT_PCS_CTRL_UNFORCED;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002767 reg |= PORT_PCS_CTRL_FORCE_LINK |
2768 PORT_PCS_CTRL_LINK_UP |
2769 PORT_PCS_CTRL_DUPLEX_FULL |
2770 PORT_PCS_CTRL_FORCE_DUPLEX;
Andrew Lunn158bc062016-04-28 21:24:06 -04002771 if (mv88e6xxx_6065_family(ps))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002772 reg |= PORT_PCS_CTRL_100;
2773 else
2774 reg |= PORT_PCS_CTRL_1000;
2775 } else {
2776 reg |= PORT_PCS_CTRL_UNFORCED;
2777 }
2778
Andrew Lunn158bc062016-04-28 21:24:06 -04002779 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002780 PORT_PCS_CTRL, reg);
2781 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002782 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002783 }
2784
2785 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
2786 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
2787 * tunneling, determine priority by looking at 802.1p and IP
2788 * priority fields (IP prio has precedence), and set STP state
2789 * to Forwarding.
2790 *
2791 * If this is the CPU link, use DSA or EDSA tagging depending
2792 * on which tagging mode was configured.
2793 *
2794 * If this is a link to another switch, use DSA tagging mode.
2795 *
2796 * If this is the upstream port for this switch, enable
2797 * forwarding of unknown unicasts and multicasts.
2798 */
2799 reg = 0;
Andrew Lunn158bc062016-04-28 21:24:06 -04002800 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2801 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2802 mv88e6xxx_6095_family(ps) || mv88e6xxx_6065_family(ps) ||
2803 mv88e6xxx_6185_family(ps) || mv88e6xxx_6320_family(ps))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002804 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
2805 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
2806 PORT_CONTROL_STATE_FORWARDING;
2807 if (dsa_is_cpu_port(ds, port)) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002808 if (mv88e6xxx_6095_family(ps) || mv88e6xxx_6185_family(ps))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002809 reg |= PORT_CONTROL_DSA_TAG;
Andrew Lunn158bc062016-04-28 21:24:06 -04002810 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2811 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2812 mv88e6xxx_6320_family(ps)) {
Andrew Lunn5377b802016-06-04 21:17:02 +02002813 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA |
2814 PORT_CONTROL_FORWARD_UNKNOWN |
Andrew Lunnc047a1f2015-09-29 01:50:56 +02002815 PORT_CONTROL_FORWARD_UNKNOWN_MC;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002816 }
2817
Andrew Lunn158bc062016-04-28 21:24:06 -04002818 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2819 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2820 mv88e6xxx_6095_family(ps) || mv88e6xxx_6065_family(ps) ||
2821 mv88e6xxx_6185_family(ps) || mv88e6xxx_6320_family(ps)) {
Vivien Didelot57d32312016-06-20 13:13:58 -04002822 reg |= PORT_CONTROL_EGRESS_ADD_TAG;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002823 }
2824 }
Andrew Lunn6083ce72015-08-17 23:52:52 +02002825 if (dsa_is_dsa_port(ds, port)) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002826 if (mv88e6xxx_6095_family(ps) || mv88e6xxx_6185_family(ps))
Andrew Lunn6083ce72015-08-17 23:52:52 +02002827 reg |= PORT_CONTROL_DSA_TAG;
Andrew Lunn158bc062016-04-28 21:24:06 -04002828 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2829 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2830 mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002831 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunn6083ce72015-08-17 23:52:52 +02002832 }
2833
Andrew Lunn54d792f2015-05-06 01:09:47 +02002834 if (port == dsa_upstream_port(ds))
2835 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2836 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2837 }
2838 if (reg) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002839 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002840 PORT_CONTROL, reg);
2841 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002842 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002843 }
2844
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002845 /* If this port is connected to a SerDes, make sure the SerDes is not
2846 * powered down.
2847 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002848 if (mv88e6xxx_6352_family(ps)) {
2849 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_STATUS);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002850 if (ret < 0)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002851 return ret;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002852 ret &= PORT_STATUS_CMODE_MASK;
2853 if ((ret == PORT_STATUS_CMODE_100BASE_X) ||
2854 (ret == PORT_STATUS_CMODE_1000BASE_X) ||
2855 (ret == PORT_STATUS_CMODE_SGMII)) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002856 ret = mv88e6xxx_power_on_serdes(ps);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002857 if (ret < 0)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002858 return ret;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002859 }
2860 }
2861
Vivien Didelot8efdda42015-08-13 12:52:23 -04002862 /* Port Control 2: don't force a good FCS, set the maximum frame size to
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002863 * 10240 bytes, disable 802.1q tags checking, don't discard tagged or
Vivien Didelot8efdda42015-08-13 12:52:23 -04002864 * untagged frames on this port, do a destination address lookup on all
2865 * received packets as usual, disable ARP mirroring and don't send a
2866 * copy of all transmitted/received frames on this port to the CPU.
Andrew Lunn54d792f2015-05-06 01:09:47 +02002867 */
2868 reg = 0;
Andrew Lunn158bc062016-04-28 21:24:06 -04002869 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2870 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2871 mv88e6xxx_6095_family(ps) || mv88e6xxx_6320_family(ps) ||
2872 mv88e6xxx_6185_family(ps))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002873 reg = PORT_CONTROL_2_MAP_DA;
2874
Andrew Lunn158bc062016-04-28 21:24:06 -04002875 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2876 mv88e6xxx_6165_family(ps) || mv88e6xxx_6320_family(ps))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002877 reg |= PORT_CONTROL_2_JUMBO_10240;
2878
Andrew Lunn158bc062016-04-28 21:24:06 -04002879 if (mv88e6xxx_6095_family(ps) || mv88e6xxx_6185_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002880 /* Set the upstream port this port should use */
2881 reg |= dsa_upstream_port(ds);
2882 /* enable forwarding of unknown multicast addresses to
2883 * the upstream port
2884 */
2885 if (port == dsa_upstream_port(ds))
2886 reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
2887 }
2888
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002889 reg |= PORT_CONTROL_2_8021Q_DISABLED;
Vivien Didelot8efdda42015-08-13 12:52:23 -04002890
Andrew Lunn54d792f2015-05-06 01:09:47 +02002891 if (reg) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002892 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002893 PORT_CONTROL_2, reg);
2894 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002895 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002896 }
2897
2898 /* Port Association Vector: when learning source addresses
2899 * of packets, add the address to the address database using
2900 * a port bitmap that has only the bit for this port set and
2901 * the other bits clear.
2902 */
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002903 reg = 1 << port;
Vivien Didelot996ecb82016-04-14 14:42:08 -04002904 /* Disable learning for CPU port */
2905 if (dsa_is_cpu_port(ds, port))
Vivien Didelot65fa4022016-04-14 14:42:07 -04002906 reg = 0;
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002907
Andrew Lunn158bc062016-04-28 21:24:06 -04002908 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_ASSOC_VECTOR, reg);
Andrew Lunn54d792f2015-05-06 01:09:47 +02002909 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002910 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002911
2912 /* Egress rate control 2: disable egress rate control. */
Andrew Lunn158bc062016-04-28 21:24:06 -04002913 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_RATE_CONTROL_2,
Andrew Lunn54d792f2015-05-06 01:09:47 +02002914 0x0000);
2915 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002916 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002917
Andrew Lunn158bc062016-04-28 21:24:06 -04002918 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2919 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2920 mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002921 /* Do not limit the period of time that this port can
2922 * be paused for by the remote end or the period of
2923 * time that this port can pause the remote end.
2924 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002925 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002926 PORT_PAUSE_CTRL, 0x0000);
2927 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002928 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002929
2930 /* Port ATU control: disable limiting the number of
2931 * address database entries that this port is allowed
2932 * to use.
2933 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002934 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002935 PORT_ATU_CONTROL, 0x0000);
2936 /* Priority Override: disable DA, SA and VTU priority
2937 * override.
2938 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002939 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002940 PORT_PRI_OVERRIDE, 0x0000);
2941 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002942 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002943
2944 /* Port Ethertype: use the Ethertype DSA Ethertype
2945 * value.
2946 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002947 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002948 PORT_ETH_TYPE, ETH_P_EDSA);
2949 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002950 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002951 /* Tag Remap: use an identity 802.1p prio -> switch
2952 * prio mapping.
2953 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002954 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002955 PORT_TAG_REGMAP_0123, 0x3210);
2956 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002957 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002958
2959 /* Tag Remap 2: use an identity 802.1p prio -> switch
2960 * prio mapping.
2961 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002962 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002963 PORT_TAG_REGMAP_4567, 0x7654);
2964 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002965 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002966 }
2967
Andrew Lunn158bc062016-04-28 21:24:06 -04002968 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2969 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2970 mv88e6xxx_6185_family(ps) || mv88e6xxx_6095_family(ps) ||
2971 mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002972 /* Rate Control: disable ingress rate limiting. */
Andrew Lunn158bc062016-04-28 21:24:06 -04002973 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002974 PORT_RATE_CONTROL, 0x0001);
2975 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002976 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002977 }
2978
Guenter Roeck366f0a02015-03-26 18:36:30 -07002979 /* Port Control 1: disable trunking, disable sending
2980 * learning messages to this port.
Guenter Roeckd827e882015-03-26 18:36:29 -07002981 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002982 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL_1, 0x0000);
Guenter Roeckd827e882015-03-26 18:36:29 -07002983 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002984 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07002985
Vivien Didelot207afda2016-04-14 14:42:09 -04002986 /* Port based VLAN map: give each port the same default address
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002987 * database, and allow bidirectional communication between the
2988 * CPU and DSA port(s), and the other ports.
Guenter Roeckd827e882015-03-26 18:36:29 -07002989 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002990 ret = _mv88e6xxx_port_fid_set(ps, port, 0);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002991 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002992 return ret;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002993
Andrew Lunn158bc062016-04-28 21:24:06 -04002994 ret = _mv88e6xxx_port_based_vlan_map(ps, port);
Guenter Roeckd827e882015-03-26 18:36:29 -07002995 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002996 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07002997
2998 /* Default VLAN ID and priority: don't set a default VLAN
2999 * ID, and set the default packet priority to zero.
3000 */
Andrew Lunn158bc062016-04-28 21:24:06 -04003001 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_DEFAULT_VLAN,
Vivien Didelot47cf1e652015-04-20 17:43:26 -04003002 0x0000);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003003 if (ret)
3004 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07003005
Andrew Lunndbde9e62015-05-06 01:09:48 +02003006 return 0;
3007}
3008
Vivien Didelot08a01262016-05-09 13:22:50 -04003009static int mv88e6xxx_setup_global(struct mv88e6xxx_priv_state *ps)
3010{
Vivien Didelotb0745e872016-05-09 13:22:53 -04003011 struct dsa_switch *ds = ps->ds;
3012 u32 upstream_port = dsa_upstream_port(ds);
Vivien Didelot119477b2016-05-09 13:22:51 -04003013 u16 reg;
Vivien Didelot08a01262016-05-09 13:22:50 -04003014 int err;
3015 int i;
3016
Vivien Didelot119477b2016-05-09 13:22:51 -04003017 /* Enable the PHY Polling Unit if present, don't discard any packets,
3018 * and mask all interrupt sources.
3019 */
3020 reg = 0;
3021 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU) ||
3022 mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU_ACTIVE))
3023 reg |= GLOBAL_CONTROL_PPU_ENABLE;
3024
3025 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_CONTROL, reg);
3026 if (err)
3027 return err;
3028
Vivien Didelotb0745e872016-05-09 13:22:53 -04003029 /* Configure the upstream port, and configure it as the port to which
3030 * ingress and egress and ARP monitor frames are to be sent.
3031 */
3032 reg = upstream_port << GLOBAL_MONITOR_CONTROL_INGRESS_SHIFT |
3033 upstream_port << GLOBAL_MONITOR_CONTROL_EGRESS_SHIFT |
3034 upstream_port << GLOBAL_MONITOR_CONTROL_ARP_SHIFT;
3035 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_MONITOR_CONTROL, reg);
3036 if (err)
3037 return err;
3038
Vivien Didelot50484ff2016-05-09 13:22:54 -04003039 /* Disable remote management, and set the switch's DSA device number. */
3040 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_CONTROL_2,
3041 GLOBAL_CONTROL_2_MULTIPLE_CASCADE |
3042 (ds->index & 0x1f));
3043 if (err)
3044 return err;
3045
Vivien Didelot08a01262016-05-09 13:22:50 -04003046 /* Set the default address aging time to 5 minutes, and
3047 * enable address learn messages to be sent to all message
3048 * ports.
3049 */
3050 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_CONTROL,
3051 0x0140 | GLOBAL_ATU_CONTROL_LEARN2ALL);
3052 if (err)
3053 return err;
3054
3055 /* Configure the IP ToS mapping registers. */
3056 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_0, 0x0000);
3057 if (err)
3058 return err;
3059 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_1, 0x0000);
3060 if (err)
3061 return err;
3062 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_2, 0x5555);
3063 if (err)
3064 return err;
3065 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_3, 0x5555);
3066 if (err)
3067 return err;
3068 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_4, 0xaaaa);
3069 if (err)
3070 return err;
3071 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_5, 0xaaaa);
3072 if (err)
3073 return err;
3074 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_6, 0xffff);
3075 if (err)
3076 return err;
3077 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_7, 0xffff);
3078 if (err)
3079 return err;
3080
3081 /* Configure the IEEE 802.1p priority mapping register. */
3082 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IEEE_PRI, 0xfa41);
3083 if (err)
3084 return err;
3085
3086 /* Send all frames with destination addresses matching
3087 * 01:80:c2:00:00:0x to the CPU port.
3088 */
3089 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_MGMT_EN_0X, 0xffff);
3090 if (err)
3091 return err;
3092
3093 /* Ignore removed tag data on doubly tagged packets, disable
3094 * flow control messages, force flow control priority to the
3095 * highest, and send all special multicast frames to the CPU
3096 * port at the highest priority.
3097 */
3098 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SWITCH_MGMT,
3099 0x7 | GLOBAL2_SWITCH_MGMT_RSVD2CPU | 0x70 |
3100 GLOBAL2_SWITCH_MGMT_FORCE_FLOW_CTRL_PRI);
3101 if (err)
3102 return err;
3103
3104 /* Program the DSA routing table. */
3105 for (i = 0; i < 32; i++) {
3106 int nexthop = 0x1f;
3107
Andrew Lunn66472fc2016-06-04 21:17:00 +02003108 if (i != ds->index && i < DSA_MAX_SWITCHES)
3109 nexthop = ds->rtable[i] & 0x1f;
Vivien Didelot08a01262016-05-09 13:22:50 -04003110
3111 err = _mv88e6xxx_reg_write(
3112 ps, REG_GLOBAL2,
3113 GLOBAL2_DEVICE_MAPPING,
3114 GLOBAL2_DEVICE_MAPPING_UPDATE |
3115 (i << GLOBAL2_DEVICE_MAPPING_TARGET_SHIFT) | nexthop);
3116 if (err)
3117 return err;
3118 }
3119
3120 /* Clear all trunk masks. */
3121 for (i = 0; i < 8; i++) {
3122 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_TRUNK_MASK,
3123 0x8000 |
3124 (i << GLOBAL2_TRUNK_MASK_NUM_SHIFT) |
3125 ((1 << ps->info->num_ports) - 1));
3126 if (err)
3127 return err;
3128 }
3129
3130 /* Clear all trunk mappings. */
3131 for (i = 0; i < 16; i++) {
3132 err = _mv88e6xxx_reg_write(
3133 ps, REG_GLOBAL2,
3134 GLOBAL2_TRUNK_MAPPING,
3135 GLOBAL2_TRUNK_MAPPING_UPDATE |
3136 (i << GLOBAL2_TRUNK_MAPPING_ID_SHIFT));
3137 if (err)
3138 return err;
3139 }
3140
3141 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
3142 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
3143 mv88e6xxx_6320_family(ps)) {
3144 /* Send all frames with destination addresses matching
3145 * 01:80:c2:00:00:2x to the CPU port.
3146 */
3147 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2,
3148 GLOBAL2_MGMT_EN_2X, 0xffff);
3149 if (err)
3150 return err;
3151
3152 /* Initialise cross-chip port VLAN table to reset
3153 * defaults.
3154 */
3155 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2,
3156 GLOBAL2_PVT_ADDR, 0x9000);
3157 if (err)
3158 return err;
3159
3160 /* Clear the priority override table. */
3161 for (i = 0; i < 16; i++) {
3162 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2,
3163 GLOBAL2_PRIO_OVERRIDE,
3164 0x8000 | (i << 8));
3165 if (err)
3166 return err;
3167 }
3168 }
3169
3170 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
3171 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
3172 mv88e6xxx_6185_family(ps) || mv88e6xxx_6095_family(ps) ||
3173 mv88e6xxx_6320_family(ps)) {
3174 /* Disable ingress rate limiting by resetting all
3175 * ingress rate limit registers to their initial
3176 * state.
3177 */
3178 for (i = 0; i < ps->info->num_ports; i++) {
3179 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2,
3180 GLOBAL2_INGRESS_OP,
3181 0x9000 | (i << 8));
3182 if (err)
3183 return err;
3184 }
3185 }
3186
3187 /* Clear the statistics counters for all ports */
3188 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_STATS_OP,
3189 GLOBAL_STATS_OP_FLUSH_ALL);
3190 if (err)
3191 return err;
3192
3193 /* Wait for the flush to complete. */
3194 err = _mv88e6xxx_stats_wait(ps);
3195 if (err)
3196 return err;
3197
3198 /* Clear all ATU entries */
3199 err = _mv88e6xxx_atu_flush(ps, 0, true);
3200 if (err)
3201 return err;
3202
3203 /* Clear all the VTU and STU entries */
3204 err = _mv88e6xxx_vtu_stu_flush(ps);
3205 if (err < 0)
3206 return err;
3207
3208 return err;
3209}
3210
Vivien Didelotf81ec902016-05-09 13:22:58 -04003211static int mv88e6xxx_setup(struct dsa_switch *ds)
Guenter Roeckacdaffc2015-03-26 18:36:28 -07003212{
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003213 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelot552238b2016-05-09 13:22:49 -04003214 int err;
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003215 int i;
3216
3217 ps->ds = ds;
Andrew Lunnb516d452016-06-04 21:17:06 +02003218 ds->slave_mii_bus = ps->mdio_bus;
Vivien Didelot552238b2016-05-09 13:22:49 -04003219
Vivien Didelotd24645b2016-05-09 13:22:41 -04003220 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEPROM))
3221 mutex_init(&ps->eeprom_mutex);
3222
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04003223 mutex_lock(&ps->reg_lock);
Vivien Didelot552238b2016-05-09 13:22:49 -04003224
3225 err = mv88e6xxx_switch_reset(ps);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003226 if (err)
3227 goto unlock;
Andrew Lunn54d792f2015-05-06 01:09:47 +02003228
Vivien Didelot08a01262016-05-09 13:22:50 -04003229 err = mv88e6xxx_setup_global(ps);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003230 if (err)
3231 goto unlock;
3232
3233 for (i = 0; i < ps->info->num_ports; i++) {
3234 err = mv88e6xxx_setup_port(ps, i);
3235 if (err)
3236 goto unlock;
3237 }
Andrew Lunn54d792f2015-05-06 01:09:47 +02003238
Vivien Didelot6b17e862015-08-13 12:52:18 -04003239unlock:
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04003240 mutex_unlock(&ps->reg_lock);
Andrew Lunndb687a52015-06-20 21:31:29 +02003241
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003242 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02003243}
3244
Vivien Didelot57d32312016-06-20 13:13:58 -04003245static int mv88e6xxx_mdio_page_read(struct dsa_switch *ds, int port, int page,
3246 int reg)
Andrew Lunn491435852015-04-02 04:06:35 +02003247{
3248 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3249 int ret;
3250
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04003251 mutex_lock(&ps->reg_lock);
Andrew Lunn03a4a542016-06-04 21:17:05 +02003252 ret = _mv88e6xxx_mdio_page_read(ps, port, page, reg);
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04003253 mutex_unlock(&ps->reg_lock);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00003254
Andrew Lunn491435852015-04-02 04:06:35 +02003255 return ret;
3256}
3257
Vivien Didelot57d32312016-06-20 13:13:58 -04003258static int mv88e6xxx_mdio_page_write(struct dsa_switch *ds, int port, int page,
3259 int reg, int val)
Andrew Lunn491435852015-04-02 04:06:35 +02003260{
3261 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3262 int ret;
3263
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04003264 mutex_lock(&ps->reg_lock);
Andrew Lunn03a4a542016-06-04 21:17:05 +02003265 ret = _mv88e6xxx_mdio_page_write(ps, port, page, reg, val);
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04003266 mutex_unlock(&ps->reg_lock);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00003267
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003268 return ret;
3269}
3270
Andrew Lunn03a4a542016-06-04 21:17:05 +02003271static int mv88e6xxx_port_to_mdio_addr(struct mv88e6xxx_priv_state *ps,
3272 int port)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003273{
Vivien Didelot009a2b92016-04-17 13:24:01 -04003274 if (port >= 0 && port < ps->info->num_ports)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003275 return port;
3276 return -EINVAL;
3277}
3278
Andrew Lunnb516d452016-06-04 21:17:06 +02003279static int mv88e6xxx_mdio_read(struct mii_bus *bus, int port, int regnum)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003280{
Andrew Lunnb516d452016-06-04 21:17:06 +02003281 struct mv88e6xxx_priv_state *ps = bus->priv;
Andrew Lunn03a4a542016-06-04 21:17:05 +02003282 int addr = mv88e6xxx_port_to_mdio_addr(ps, port);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003283 int ret;
3284
3285 if (addr < 0)
Andrew Lunn158bc062016-04-28 21:24:06 -04003286 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003287
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04003288 mutex_lock(&ps->reg_lock);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003289
3290 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU))
Andrew Lunn03a4a542016-06-04 21:17:05 +02003291 ret = mv88e6xxx_mdio_read_ppu(ps, addr, regnum);
Vivien Didelot6d5834a2016-05-09 13:22:40 -04003292 else if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_SMI_PHY))
Andrew Lunn03a4a542016-06-04 21:17:05 +02003293 ret = mv88e6xxx_mdio_read_indirect(ps, addr, regnum);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003294 else
Andrew Lunn03a4a542016-06-04 21:17:05 +02003295 ret = mv88e6xxx_mdio_read_direct(ps, addr, regnum);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003296
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04003297 mutex_unlock(&ps->reg_lock);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003298 return ret;
3299}
3300
Andrew Lunnb516d452016-06-04 21:17:06 +02003301static int mv88e6xxx_mdio_write(struct mii_bus *bus, int port, int regnum,
Andrew Lunn03a4a542016-06-04 21:17:05 +02003302 u16 val)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003303{
Andrew Lunnb516d452016-06-04 21:17:06 +02003304 struct mv88e6xxx_priv_state *ps = bus->priv;
Andrew Lunn03a4a542016-06-04 21:17:05 +02003305 int addr = mv88e6xxx_port_to_mdio_addr(ps, port);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003306 int ret;
3307
3308 if (addr < 0)
Andrew Lunn158bc062016-04-28 21:24:06 -04003309 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003310
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04003311 mutex_lock(&ps->reg_lock);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003312
3313 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU))
Andrew Lunn03a4a542016-06-04 21:17:05 +02003314 ret = mv88e6xxx_mdio_write_ppu(ps, addr, regnum, val);
Vivien Didelot6d5834a2016-05-09 13:22:40 -04003315 else if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_SMI_PHY))
Andrew Lunn03a4a542016-06-04 21:17:05 +02003316 ret = mv88e6xxx_mdio_write_indirect(ps, addr, regnum, val);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003317 else
Andrew Lunn03a4a542016-06-04 21:17:05 +02003318 ret = mv88e6xxx_mdio_write_direct(ps, addr, regnum, val);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003319
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04003320 mutex_unlock(&ps->reg_lock);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003321 return ret;
3322}
3323
Andrew Lunnb516d452016-06-04 21:17:06 +02003324static int mv88e6xxx_mdio_register(struct mv88e6xxx_priv_state *ps,
3325 struct device_node *np)
3326{
3327 static int index;
3328 struct mii_bus *bus;
3329 int err;
3330
3331 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU))
3332 mv88e6xxx_ppu_state_init(ps);
3333
3334 if (np)
3335 ps->mdio_np = of_get_child_by_name(np, "mdio");
3336
3337 bus = devm_mdiobus_alloc(ps->dev);
3338 if (!bus)
3339 return -ENOMEM;
3340
3341 bus->priv = (void *)ps;
3342 if (np) {
3343 bus->name = np->full_name;
3344 snprintf(bus->id, MII_BUS_ID_SIZE, "%s", np->full_name);
3345 } else {
3346 bus->name = "mv88e6xxx SMI";
3347 snprintf(bus->id, MII_BUS_ID_SIZE, "mv88e6xxx-%d", index++);
3348 }
3349
3350 bus->read = mv88e6xxx_mdio_read;
3351 bus->write = mv88e6xxx_mdio_write;
3352 bus->parent = ps->dev;
3353
3354 if (ps->mdio_np)
3355 err = of_mdiobus_register(bus, ps->mdio_np);
3356 else
3357 err = mdiobus_register(bus);
3358 if (err) {
3359 dev_err(ps->dev, "Cannot register MDIO bus (%d)\n", err);
3360 goto out;
3361 }
3362 ps->mdio_bus = bus;
3363
3364 return 0;
3365
3366out:
3367 if (ps->mdio_np)
3368 of_node_put(ps->mdio_np);
3369
3370 return err;
3371}
3372
3373static void mv88e6xxx_mdio_unregister(struct mv88e6xxx_priv_state *ps)
3374
3375{
3376 struct mii_bus *bus = ps->mdio_bus;
3377
3378 mdiobus_unregister(bus);
3379
3380 if (ps->mdio_np)
3381 of_node_put(ps->mdio_np);
3382}
3383
Guenter Roeckc22995c2015-07-25 09:42:28 -07003384#ifdef CONFIG_NET_DSA_HWMON
3385
3386static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
3387{
3388 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3389 int ret;
3390 int val;
3391
3392 *temp = 0;
3393
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04003394 mutex_lock(&ps->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003395
Andrew Lunn03a4a542016-06-04 21:17:05 +02003396 ret = mv88e6xxx_mdio_write_direct(ps, 0x0, 0x16, 0x6);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003397 if (ret < 0)
3398 goto error;
3399
3400 /* Enable temperature sensor */
Andrew Lunn03a4a542016-06-04 21:17:05 +02003401 ret = mv88e6xxx_mdio_read_direct(ps, 0x0, 0x1a);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003402 if (ret < 0)
3403 goto error;
3404
Andrew Lunn03a4a542016-06-04 21:17:05 +02003405 ret = mv88e6xxx_mdio_write_direct(ps, 0x0, 0x1a, ret | (1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003406 if (ret < 0)
3407 goto error;
3408
3409 /* Wait for temperature to stabilize */
3410 usleep_range(10000, 12000);
3411
Andrew Lunn03a4a542016-06-04 21:17:05 +02003412 val = mv88e6xxx_mdio_read_direct(ps, 0x0, 0x1a);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003413 if (val < 0) {
3414 ret = val;
3415 goto error;
3416 }
3417
3418 /* Disable temperature sensor */
Andrew Lunn03a4a542016-06-04 21:17:05 +02003419 ret = mv88e6xxx_mdio_write_direct(ps, 0x0, 0x1a, ret & ~(1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003420 if (ret < 0)
3421 goto error;
3422
3423 *temp = ((val & 0x1f) - 5) * 5;
3424
3425error:
Andrew Lunn03a4a542016-06-04 21:17:05 +02003426 mv88e6xxx_mdio_write_direct(ps, 0x0, 0x16, 0x0);
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04003427 mutex_unlock(&ps->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003428 return ret;
3429}
3430
3431static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
3432{
Andrew Lunn158bc062016-04-28 21:24:06 -04003433 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3434 int phy = mv88e6xxx_6320_family(ps) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003435 int ret;
3436
3437 *temp = 0;
3438
Andrew Lunn03a4a542016-06-04 21:17:05 +02003439 ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 27);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003440 if (ret < 0)
3441 return ret;
3442
3443 *temp = (ret & 0xff) - 25;
3444
3445 return 0;
3446}
3447
Vivien Didelotf81ec902016-05-09 13:22:58 -04003448static int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003449{
Andrew Lunn158bc062016-04-28 21:24:06 -04003450 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3451
Vivien Didelot6594f612016-05-09 13:22:42 -04003452 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_TEMP))
3453 return -EOPNOTSUPP;
3454
Andrew Lunn158bc062016-04-28 21:24:06 -04003455 if (mv88e6xxx_6320_family(ps) || mv88e6xxx_6352_family(ps))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003456 return mv88e63xx_get_temp(ds, temp);
3457
3458 return mv88e61xx_get_temp(ds, temp);
3459}
3460
Vivien Didelotf81ec902016-05-09 13:22:58 -04003461static int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003462{
Andrew Lunn158bc062016-04-28 21:24:06 -04003463 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3464 int phy = mv88e6xxx_6320_family(ps) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003465 int ret;
3466
Vivien Didelot6594f612016-05-09 13:22:42 -04003467 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003468 return -EOPNOTSUPP;
3469
3470 *temp = 0;
3471
Andrew Lunn03a4a542016-06-04 21:17:05 +02003472 ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 26);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003473 if (ret < 0)
3474 return ret;
3475
3476 *temp = (((ret >> 8) & 0x1f) * 5) - 25;
3477
3478 return 0;
3479}
3480
Vivien Didelotf81ec902016-05-09 13:22:58 -04003481static int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003482{
Andrew Lunn158bc062016-04-28 21:24:06 -04003483 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3484 int phy = mv88e6xxx_6320_family(ps) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003485 int ret;
3486
Vivien Didelot6594f612016-05-09 13:22:42 -04003487 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003488 return -EOPNOTSUPP;
3489
Andrew Lunn03a4a542016-06-04 21:17:05 +02003490 ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 26);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003491 if (ret < 0)
3492 return ret;
3493 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
Andrew Lunn03a4a542016-06-04 21:17:05 +02003494 return mv88e6xxx_mdio_page_write(ds, phy, 6, 26,
3495 (ret & 0xe0ff) | (temp << 8));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003496}
3497
Vivien Didelotf81ec902016-05-09 13:22:58 -04003498static int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003499{
Andrew Lunn158bc062016-04-28 21:24:06 -04003500 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3501 int phy = mv88e6xxx_6320_family(ps) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003502 int ret;
3503
Vivien Didelot6594f612016-05-09 13:22:42 -04003504 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003505 return -EOPNOTSUPP;
3506
3507 *alarm = false;
3508
Andrew Lunn03a4a542016-06-04 21:17:05 +02003509 ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 26);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003510 if (ret < 0)
3511 return ret;
3512
3513 *alarm = !!(ret & 0x40);
3514
3515 return 0;
3516}
3517#endif /* CONFIG_NET_DSA_HWMON */
3518
Vivien Didelotf81ec902016-05-09 13:22:58 -04003519static const struct mv88e6xxx_info mv88e6xxx_table[] = {
3520 [MV88E6085] = {
3521 .prod_num = PORT_SWITCH_ID_PROD_NUM_6085,
3522 .family = MV88E6XXX_FAMILY_6097,
3523 .name = "Marvell 88E6085",
3524 .num_databases = 4096,
3525 .num_ports = 10,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003526 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003527 .flags = MV88E6XXX_FLAGS_FAMILY_6097,
3528 },
3529
3530 [MV88E6095] = {
3531 .prod_num = PORT_SWITCH_ID_PROD_NUM_6095,
3532 .family = MV88E6XXX_FAMILY_6095,
3533 .name = "Marvell 88E6095/88E6095F",
3534 .num_databases = 256,
3535 .num_ports = 11,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003536 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003537 .flags = MV88E6XXX_FLAGS_FAMILY_6095,
3538 },
3539
3540 [MV88E6123] = {
3541 .prod_num = PORT_SWITCH_ID_PROD_NUM_6123,
3542 .family = MV88E6XXX_FAMILY_6165,
3543 .name = "Marvell 88E6123",
3544 .num_databases = 4096,
3545 .num_ports = 3,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003546 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003547 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3548 },
3549
3550 [MV88E6131] = {
3551 .prod_num = PORT_SWITCH_ID_PROD_NUM_6131,
3552 .family = MV88E6XXX_FAMILY_6185,
3553 .name = "Marvell 88E6131",
3554 .num_databases = 256,
3555 .num_ports = 8,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003556 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003557 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3558 },
3559
3560 [MV88E6161] = {
3561 .prod_num = PORT_SWITCH_ID_PROD_NUM_6161,
3562 .family = MV88E6XXX_FAMILY_6165,
3563 .name = "Marvell 88E6161",
3564 .num_databases = 4096,
3565 .num_ports = 6,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003566 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003567 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3568 },
3569
3570 [MV88E6165] = {
3571 .prod_num = PORT_SWITCH_ID_PROD_NUM_6165,
3572 .family = MV88E6XXX_FAMILY_6165,
3573 .name = "Marvell 88E6165",
3574 .num_databases = 4096,
3575 .num_ports = 6,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003576 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003577 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3578 },
3579
3580 [MV88E6171] = {
3581 .prod_num = PORT_SWITCH_ID_PROD_NUM_6171,
3582 .family = MV88E6XXX_FAMILY_6351,
3583 .name = "Marvell 88E6171",
3584 .num_databases = 4096,
3585 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003586 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003587 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3588 },
3589
3590 [MV88E6172] = {
3591 .prod_num = PORT_SWITCH_ID_PROD_NUM_6172,
3592 .family = MV88E6XXX_FAMILY_6352,
3593 .name = "Marvell 88E6172",
3594 .num_databases = 4096,
3595 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003596 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003597 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3598 },
3599
3600 [MV88E6175] = {
3601 .prod_num = PORT_SWITCH_ID_PROD_NUM_6175,
3602 .family = MV88E6XXX_FAMILY_6351,
3603 .name = "Marvell 88E6175",
3604 .num_databases = 4096,
3605 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003606 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003607 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3608 },
3609
3610 [MV88E6176] = {
3611 .prod_num = PORT_SWITCH_ID_PROD_NUM_6176,
3612 .family = MV88E6XXX_FAMILY_6352,
3613 .name = "Marvell 88E6176",
3614 .num_databases = 4096,
3615 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003616 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003617 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3618 },
3619
3620 [MV88E6185] = {
3621 .prod_num = PORT_SWITCH_ID_PROD_NUM_6185,
3622 .family = MV88E6XXX_FAMILY_6185,
3623 .name = "Marvell 88E6185",
3624 .num_databases = 256,
3625 .num_ports = 10,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003626 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003627 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3628 },
3629
3630 [MV88E6240] = {
3631 .prod_num = PORT_SWITCH_ID_PROD_NUM_6240,
3632 .family = MV88E6XXX_FAMILY_6352,
3633 .name = "Marvell 88E6240",
3634 .num_databases = 4096,
3635 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003636 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003637 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3638 },
3639
3640 [MV88E6320] = {
3641 .prod_num = PORT_SWITCH_ID_PROD_NUM_6320,
3642 .family = MV88E6XXX_FAMILY_6320,
3643 .name = "Marvell 88E6320",
3644 .num_databases = 4096,
3645 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003646 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003647 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3648 },
3649
3650 [MV88E6321] = {
3651 .prod_num = PORT_SWITCH_ID_PROD_NUM_6321,
3652 .family = MV88E6XXX_FAMILY_6320,
3653 .name = "Marvell 88E6321",
3654 .num_databases = 4096,
3655 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003656 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003657 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3658 },
3659
3660 [MV88E6350] = {
3661 .prod_num = PORT_SWITCH_ID_PROD_NUM_6350,
3662 .family = MV88E6XXX_FAMILY_6351,
3663 .name = "Marvell 88E6350",
3664 .num_databases = 4096,
3665 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003666 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003667 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3668 },
3669
3670 [MV88E6351] = {
3671 .prod_num = PORT_SWITCH_ID_PROD_NUM_6351,
3672 .family = MV88E6XXX_FAMILY_6351,
3673 .name = "Marvell 88E6351",
3674 .num_databases = 4096,
3675 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003676 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003677 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3678 },
3679
3680 [MV88E6352] = {
3681 .prod_num = PORT_SWITCH_ID_PROD_NUM_6352,
3682 .family = MV88E6XXX_FAMILY_6352,
3683 .name = "Marvell 88E6352",
3684 .num_databases = 4096,
3685 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003686 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003687 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3688 },
3689};
3690
Vivien Didelot5f7c0362016-06-20 13:14:04 -04003691static const struct mv88e6xxx_info *mv88e6xxx_lookup_info(unsigned int prod_num)
Vivien Didelotb9b37712015-10-30 19:39:48 -04003692{
Vivien Didelota439c062016-04-17 13:23:58 -04003693 int i;
Vivien Didelotb9b37712015-10-30 19:39:48 -04003694
Vivien Didelot5f7c0362016-06-20 13:14:04 -04003695 for (i = 0; i < ARRAY_SIZE(mv88e6xxx_table); ++i)
3696 if (mv88e6xxx_table[i].prod_num == prod_num)
3697 return &mv88e6xxx_table[i];
Vivien Didelotb9b37712015-10-30 19:39:48 -04003698
Vivien Didelotb9b37712015-10-30 19:39:48 -04003699 return NULL;
3700}
3701
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003702static int mv88e6xxx_detect(struct mv88e6xxx_priv_state *ps)
3703{
3704 const struct mv88e6xxx_info *info;
3705 int id, prod_num, rev;
3706
Vivien Didelot9dddd472016-06-20 13:14:10 -04003707 id = mv88e6xxx_reg_read(ps, ps->info->port_base_addr, PORT_SWITCH_ID);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003708 if (id < 0)
3709 return id;
3710
3711 prod_num = (id & 0xfff0) >> 4;
3712 rev = id & 0x000f;
3713
3714 info = mv88e6xxx_lookup_info(prod_num);
3715 if (!info)
3716 return -ENODEV;
3717
Vivien Didelotcaac8542016-06-20 13:14:09 -04003718 /* Update the compatible info with the probed one */
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003719 ps->info = info;
3720
3721 dev_info(ps->dev, "switch 0x%x detected: %s, revision %u\n",
3722 ps->info->prod_num, ps->info->name, rev);
3723
3724 return 0;
3725}
3726
Vivien Didelot469d7292016-06-20 13:14:06 -04003727static struct mv88e6xxx_priv_state *mv88e6xxx_alloc_chip(struct device *dev)
3728{
3729 struct mv88e6xxx_priv_state *ps;
3730
3731 ps = devm_kzalloc(dev, sizeof(*ps), GFP_KERNEL);
3732 if (!ps)
3733 return NULL;
3734
3735 ps->dev = dev;
3736
3737 mutex_init(&ps->reg_lock);
3738
3739 return ps;
3740}
3741
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003742static int mv88e6xxx_smi_init(struct mv88e6xxx_priv_state *ps,
3743 struct mii_bus *bus, int sw_addr)
3744{
3745 /* ADDR[0] pin is unavailable externally and considered zero */
3746 if (sw_addr & 0x1)
3747 return -EINVAL;
3748
Vivien Didelot914b32f2016-06-20 13:14:11 -04003749 if (sw_addr == 0)
3750 ps->smi_ops = &mv88e6xxx_smi_single_chip_ops;
3751 else if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_MULTI_CHIP))
3752 ps->smi_ops = &mv88e6xxx_smi_multi_chip_ops;
3753 else
3754 return -EINVAL;
3755
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003756 ps->bus = bus;
3757 ps->sw_addr = sw_addr;
3758
3759 return 0;
3760}
3761
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02003762static const char *mv88e6xxx_drv_probe(struct device *dsa_dev,
3763 struct device *host_dev, int sw_addr,
3764 void **priv)
Andrew Lunna77d43f2016-04-13 02:40:42 +02003765{
3766 struct mv88e6xxx_priv_state *ps;
Vivien Didelota439c062016-04-17 13:23:58 -04003767 struct mii_bus *bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003768 int err;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003769
Vivien Didelota439c062016-04-17 13:23:58 -04003770 bus = dsa_host_dev_to_mii_bus(host_dev);
Andrew Lunnc1569132016-04-13 02:40:45 +02003771 if (!bus)
3772 return NULL;
3773
Vivien Didelot469d7292016-06-20 13:14:06 -04003774 ps = mv88e6xxx_alloc_chip(dsa_dev);
3775 if (!ps)
3776 return NULL;
3777
Vivien Didelotcaac8542016-06-20 13:14:09 -04003778 /* Legacy SMI probing will only support chips similar to 88E6085 */
3779 ps->info = &mv88e6xxx_table[MV88E6085];
3780
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003781 err = mv88e6xxx_smi_init(ps, bus, sw_addr);
3782 if (err)
3783 goto free;
3784
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003785 err = mv88e6xxx_detect(ps);
3786 if (err)
Vivien Didelot469d7292016-06-20 13:14:06 -04003787 goto free;
Vivien Didelota439c062016-04-17 13:23:58 -04003788
Andrew Lunnb516d452016-06-04 21:17:06 +02003789 err = mv88e6xxx_mdio_register(ps, NULL);
3790 if (err)
Vivien Didelot469d7292016-06-20 13:14:06 -04003791 goto free;
Andrew Lunnb516d452016-06-04 21:17:06 +02003792
Vivien Didelota439c062016-04-17 13:23:58 -04003793 *priv = ps;
3794
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003795 return ps->info->name;
Vivien Didelot469d7292016-06-20 13:14:06 -04003796free:
3797 devm_kfree(dsa_dev, ps);
3798
3799 return NULL;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003800}
3801
Vivien Didelot57d32312016-06-20 13:13:58 -04003802static struct dsa_switch_driver mv88e6xxx_switch_driver = {
Vivien Didelotf81ec902016-05-09 13:22:58 -04003803 .tag_protocol = DSA_TAG_PROTO_EDSA,
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02003804 .probe = mv88e6xxx_drv_probe,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003805 .setup = mv88e6xxx_setup,
3806 .set_addr = mv88e6xxx_set_addr,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003807 .adjust_link = mv88e6xxx_adjust_link,
3808 .get_strings = mv88e6xxx_get_strings,
3809 .get_ethtool_stats = mv88e6xxx_get_ethtool_stats,
3810 .get_sset_count = mv88e6xxx_get_sset_count,
3811 .set_eee = mv88e6xxx_set_eee,
3812 .get_eee = mv88e6xxx_get_eee,
3813#ifdef CONFIG_NET_DSA_HWMON
3814 .get_temp = mv88e6xxx_get_temp,
3815 .get_temp_limit = mv88e6xxx_get_temp_limit,
3816 .set_temp_limit = mv88e6xxx_set_temp_limit,
3817 .get_temp_alarm = mv88e6xxx_get_temp_alarm,
3818#endif
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003819 .get_eeprom_len = mv88e6xxx_get_eeprom_len,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003820 .get_eeprom = mv88e6xxx_get_eeprom,
3821 .set_eeprom = mv88e6xxx_set_eeprom,
3822 .get_regs_len = mv88e6xxx_get_regs_len,
3823 .get_regs = mv88e6xxx_get_regs,
3824 .port_bridge_join = mv88e6xxx_port_bridge_join,
3825 .port_bridge_leave = mv88e6xxx_port_bridge_leave,
3826 .port_stp_state_set = mv88e6xxx_port_stp_state_set,
3827 .port_vlan_filtering = mv88e6xxx_port_vlan_filtering,
3828 .port_vlan_prepare = mv88e6xxx_port_vlan_prepare,
3829 .port_vlan_add = mv88e6xxx_port_vlan_add,
3830 .port_vlan_del = mv88e6xxx_port_vlan_del,
3831 .port_vlan_dump = mv88e6xxx_port_vlan_dump,
3832 .port_fdb_prepare = mv88e6xxx_port_fdb_prepare,
3833 .port_fdb_add = mv88e6xxx_port_fdb_add,
3834 .port_fdb_del = mv88e6xxx_port_fdb_del,
3835 .port_fdb_dump = mv88e6xxx_port_fdb_dump,
3836};
3837
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003838static int mv88e6xxx_register_switch(struct mv88e6xxx_priv_state *ps,
3839 struct device_node *np)
3840{
3841 struct device *dev = ps->dev;
3842 struct dsa_switch *ds;
3843
3844 ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
3845 if (!ds)
3846 return -ENOMEM;
3847
3848 ds->dev = dev;
3849 ds->priv = ps;
3850 ds->drv = &mv88e6xxx_switch_driver;
3851
3852 dev_set_drvdata(dev, ds);
3853
3854 return dsa_register_switch(ds, np);
3855}
3856
3857static void mv88e6xxx_unregister_switch(struct mv88e6xxx_priv_state *ps)
3858{
3859 dsa_unregister_switch(ps->ds);
3860}
3861
Vivien Didelot57d32312016-06-20 13:13:58 -04003862static int mv88e6xxx_probe(struct mdio_device *mdiodev)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003863{
3864 struct device *dev = &mdiodev->dev;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003865 struct device_node *np = dev->of_node;
Vivien Didelotcaac8542016-06-20 13:14:09 -04003866 const struct mv88e6xxx_info *compat_info;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003867 struct mv88e6xxx_priv_state *ps;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003868 u32 eeprom_len;
Andrew Lunn52638f72016-05-10 23:27:22 +02003869 int err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003870
Vivien Didelotcaac8542016-06-20 13:14:09 -04003871 compat_info = of_device_get_match_data(dev);
3872 if (!compat_info)
3873 return -EINVAL;
3874
Vivien Didelot469d7292016-06-20 13:14:06 -04003875 ps = mv88e6xxx_alloc_chip(dev);
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003876 if (!ps)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003877 return -ENOMEM;
3878
Vivien Didelotcaac8542016-06-20 13:14:09 -04003879 ps->info = compat_info;
3880
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003881 err = mv88e6xxx_smi_init(ps, mdiodev->bus, mdiodev->addr);
3882 if (err)
3883 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003884
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003885 err = mv88e6xxx_detect(ps);
3886 if (err)
3887 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003888
Vivien Didelotc6d19ab2016-06-20 13:14:03 -04003889 ps->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_ASIS);
3890 if (IS_ERR(ps->reset))
3891 return PTR_ERR(ps->reset);
Andrew Lunn52638f72016-05-10 23:27:22 +02003892
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003893 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEPROM) &&
3894 !of_property_read_u32(np, "eeprom-length", &eeprom_len))
3895 ps->eeprom_len = eeprom_len;
3896
Vivien Didelotaa8ac392016-06-20 13:14:00 -04003897 err = mv88e6xxx_mdio_register(ps, np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003898 if (err)
3899 return err;
3900
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003901 err = mv88e6xxx_register_switch(ps, np);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02003902 if (err) {
3903 mv88e6xxx_mdio_unregister(ps);
3904 return err;
3905 }
3906
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003907 return 0;
3908}
3909
3910static void mv88e6xxx_remove(struct mdio_device *mdiodev)
3911{
3912 struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev);
3913 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3914
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003915 mv88e6xxx_unregister_switch(ps);
Andrew Lunnb516d452016-06-04 21:17:06 +02003916 mv88e6xxx_mdio_unregister(ps);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003917}
3918
3919static const struct of_device_id mv88e6xxx_of_match[] = {
Vivien Didelotcaac8542016-06-20 13:14:09 -04003920 {
3921 .compatible = "marvell,mv88e6085",
3922 .data = &mv88e6xxx_table[MV88E6085],
3923 },
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003924 { /* sentinel */ },
3925};
3926
3927MODULE_DEVICE_TABLE(of, mv88e6xxx_of_match);
3928
3929static struct mdio_driver mv88e6xxx_driver = {
3930 .probe = mv88e6xxx_probe,
3931 .remove = mv88e6xxx_remove,
3932 .mdiodrv.driver = {
3933 .name = "mv88e6085",
3934 .of_match_table = mv88e6xxx_of_match,
3935 },
3936};
3937
Ben Hutchings98e67302011-11-25 14:36:19 +00003938static int __init mv88e6xxx_init(void)
3939{
Vivien Didelotf81ec902016-05-09 13:22:58 -04003940 register_switch_driver(&mv88e6xxx_switch_driver);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003941 return mdio_driver_register(&mv88e6xxx_driver);
Ben Hutchings98e67302011-11-25 14:36:19 +00003942}
3943module_init(mv88e6xxx_init);
3944
3945static void __exit mv88e6xxx_cleanup(void)
3946{
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003947 mdio_driver_unregister(&mv88e6xxx_driver);
Vivien Didelotf81ec902016-05-09 13:22:58 -04003948 unregister_switch_driver(&mv88e6xxx_switch_driver);
Ben Hutchings98e67302011-11-25 14:36:19 +00003949}
3950module_exit(mv88e6xxx_cleanup);
Ben Hutchings3d825ed2011-11-25 14:37:16 +00003951
3952MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
3953MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
3954MODULE_LICENSE("GPL");