blob: 9b116d8d4e23bc08ef5ad68bd5c97712db195dd1 [file] [log] [blame]
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001/*
2 * net/dsa/mv88e6xxx.c - Marvell 88e6xxx switch chip support
3 * Copyright (c) 2008 Marvell Semiconductor
4 *
Vivien Didelotb8fee952015-08-13 12:52:19 -04005 * Copyright (c) 2015 CMC Electronics, Inc.
6 * Added support for VLAN Table Unit operations
7 *
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02008 * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch>
9 *
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 */
15
Barry Grussling19b2f972013-01-08 16:05:54 +000016#include <linux/delay.h>
Guenter Roeckdefb05b2015-03-26 18:36:38 -070017#include <linux/etherdevice.h>
Andrew Lunndea87022015-08-31 15:56:47 +020018#include <linux/ethtool.h>
Guenter Roeckfacd95b2015-03-26 18:36:35 -070019#include <linux/if_bridge.h>
Barry Grussling19b2f972013-01-08 16:05:54 +000020#include <linux/jiffies.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000021#include <linux/list.h>
Andrew Lunn14c7b3c2016-05-10 23:27:21 +020022#include <linux/mdio.h>
Paul Gortmaker2bbba272012-01-24 10:41:40 +000023#include <linux/module.h>
Vivien Didelotcaac8542016-06-20 13:14:09 -040024#include <linux/of_device.h>
Andrew Lunnb516d452016-06-04 21:17:06 +020025#include <linux/of_mdio.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000026#include <linux/netdevice.h>
Andrew Lunnc8c1b39a2015-11-20 03:56:24 +010027#include <linux/gpio/consumer.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000028#include <linux/phy.h>
Ben Hutchingsc8f0b862011-11-27 17:06:08 +000029#include <net/dsa.h>
Vivien Didelot1f36faf2015-10-08 11:35:13 -040030#include <net/switchdev.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000031#include "mv88e6xxx.h"
32
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -040033static void assert_reg_lock(struct mv88e6xxx_priv_state *ps)
Vivien Didelot3996a4f2015-10-30 18:56:45 -040034{
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -040035 if (unlikely(!mutex_is_locked(&ps->reg_lock))) {
36 dev_err(ps->dev, "Switch registers lock not held!\n");
Vivien Didelot3996a4f2015-10-30 18:56:45 -040037 dump_stack();
38 }
39}
40
Vivien Didelot914b32f2016-06-20 13:14:11 -040041/* The switch ADDR[4:1] configuration pins define the chip SMI device address
42 * (ADDR[0] is always zero, thus only even SMI addresses can be strapped).
43 *
44 * When ADDR is all zero, the chip uses Single-chip Addressing Mode, assuming it
45 * is the only device connected to the SMI master. In this mode it responds to
46 * all 32 possible SMI addresses, and thus maps directly the internal devices.
47 *
48 * When ADDR is non-zero, the chip uses Multi-chip Addressing Mode, allowing
49 * multiple devices to share the SMI interface. In this mode it responds to only
50 * 2 registers, used to indirectly access the internal SMI devices.
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000051 */
Vivien Didelot914b32f2016-06-20 13:14:11 -040052
53static int mv88e6xxx_smi_read(struct mv88e6xxx_priv_state *ps,
54 int addr, int reg, u16 *val)
55{
56 if (!ps->smi_ops)
57 return -EOPNOTSUPP;
58
59 return ps->smi_ops->read(ps, addr, reg, val);
60}
61
62static int mv88e6xxx_smi_write(struct mv88e6xxx_priv_state *ps,
63 int addr, int reg, u16 val)
64{
65 if (!ps->smi_ops)
66 return -EOPNOTSUPP;
67
68 return ps->smi_ops->write(ps, addr, reg, val);
69}
70
71static int mv88e6xxx_smi_single_chip_read(struct mv88e6xxx_priv_state *ps,
72 int addr, int reg, u16 *val)
73{
74 int ret;
75
76 ret = mdiobus_read_nested(ps->bus, addr, reg);
77 if (ret < 0)
78 return ret;
79
80 *val = ret & 0xffff;
81
82 return 0;
83}
84
85static int mv88e6xxx_smi_single_chip_write(struct mv88e6xxx_priv_state *ps,
86 int addr, int reg, u16 val)
87{
88 int ret;
89
90 ret = mdiobus_write_nested(ps->bus, addr, reg, val);
91 if (ret < 0)
92 return ret;
93
94 return 0;
95}
96
97static const struct mv88e6xxx_ops mv88e6xxx_smi_single_chip_ops = {
98 .read = mv88e6xxx_smi_single_chip_read,
99 .write = mv88e6xxx_smi_single_chip_write,
100};
101
102static int mv88e6xxx_smi_multi_chip_wait(struct mv88e6xxx_priv_state *ps)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000103{
104 int ret;
105 int i;
106
107 for (i = 0; i < 16; i++) {
Vivien Didelot914b32f2016-06-20 13:14:11 -0400108 ret = mdiobus_read_nested(ps->bus, ps->sw_addr, SMI_CMD);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000109 if (ret < 0)
110 return ret;
111
Andrew Lunncca8b132015-04-02 04:06:39 +0200112 if ((ret & SMI_CMD_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000113 return 0;
114 }
115
116 return -ETIMEDOUT;
117}
118
Vivien Didelot914b32f2016-06-20 13:14:11 -0400119static int mv88e6xxx_smi_multi_chip_read(struct mv88e6xxx_priv_state *ps,
120 int addr, int reg, u16 *val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000121{
122 int ret;
123
Barry Grussling3675c8d2013-01-08 16:05:53 +0000124 /* Wait for the bus to become free. */
Vivien Didelot914b32f2016-06-20 13:14:11 -0400125 ret = mv88e6xxx_smi_multi_chip_wait(ps);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000126 if (ret < 0)
127 return ret;
128
Barry Grussling3675c8d2013-01-08 16:05:53 +0000129 /* Transmit the read command. */
Vivien Didelot914b32f2016-06-20 13:14:11 -0400130 ret = mdiobus_write_nested(ps->bus, ps->sw_addr, SMI_CMD,
Neil Armstrong6e899e62015-10-22 10:37:53 +0200131 SMI_CMD_OP_22_READ | (addr << 5) | reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000132 if (ret < 0)
133 return ret;
134
Barry Grussling3675c8d2013-01-08 16:05:53 +0000135 /* Wait for the read command to complete. */
Vivien Didelot914b32f2016-06-20 13:14:11 -0400136 ret = mv88e6xxx_smi_multi_chip_wait(ps);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000137 if (ret < 0)
138 return ret;
139
Barry Grussling3675c8d2013-01-08 16:05:53 +0000140 /* Read the data. */
Vivien Didelot914b32f2016-06-20 13:14:11 -0400141 ret = mdiobus_read_nested(ps->bus, ps->sw_addr, SMI_DATA);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000142 if (ret < 0)
143 return ret;
144
Vivien Didelot914b32f2016-06-20 13:14:11 -0400145 *val = ret & 0xffff;
146
147 return 0;
148}
149
150static int mv88e6xxx_smi_multi_chip_write(struct mv88e6xxx_priv_state *ps,
151 int addr, int reg, u16 val)
152{
153 int ret;
154
155 /* Wait for the bus to become free. */
156 ret = mv88e6xxx_smi_multi_chip_wait(ps);
157 if (ret < 0)
158 return ret;
159
160 /* Transmit the data to write. */
161 ret = mdiobus_write_nested(ps->bus, ps->sw_addr, SMI_DATA, val);
162 if (ret < 0)
163 return ret;
164
165 /* Transmit the write command. */
166 ret = mdiobus_write_nested(ps->bus, ps->sw_addr, SMI_CMD,
167 SMI_CMD_OP_22_WRITE | (addr << 5) | reg);
168 if (ret < 0)
169 return ret;
170
171 /* Wait for the write command to complete. */
172 ret = mv88e6xxx_smi_multi_chip_wait(ps);
173 if (ret < 0)
174 return ret;
175
176 return 0;
177}
178
179static const struct mv88e6xxx_ops mv88e6xxx_smi_multi_chip_ops = {
180 .read = mv88e6xxx_smi_multi_chip_read,
181 .write = mv88e6xxx_smi_multi_chip_write,
182};
183
184static int mv88e6xxx_read(struct mv88e6xxx_priv_state *ps,
185 int addr, int reg, u16 *val)
186{
187 int err;
188
189 assert_reg_lock(ps);
190
191 err = mv88e6xxx_smi_read(ps, addr, reg, val);
192 if (err)
193 return err;
194
195 dev_dbg(ps->dev, "<- addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
196 addr, reg, *val);
197
198 return 0;
199}
200
201static int mv88e6xxx_write(struct mv88e6xxx_priv_state *ps,
202 int addr, int reg, u16 val)
203{
204 int err;
205
206 assert_reg_lock(ps);
207
208 err = mv88e6xxx_smi_write(ps, addr, reg, val);
209 if (err)
210 return err;
211
212 dev_dbg(ps->dev, "-> addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
213 addr, reg, val);
214
215 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000216}
217
Andrew Lunn158bc062016-04-28 21:24:06 -0400218static int _mv88e6xxx_reg_read(struct mv88e6xxx_priv_state *ps,
219 int addr, int reg)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000220{
Vivien Didelot914b32f2016-06-20 13:14:11 -0400221 u16 val;
222 int err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000223
Vivien Didelot914b32f2016-06-20 13:14:11 -0400224 err = mv88e6xxx_read(ps, addr, reg, &val);
225 if (err)
226 return err;
Vivien Didelot3996a4f2015-10-30 18:56:45 -0400227
Vivien Didelot914b32f2016-06-20 13:14:11 -0400228 return val;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000229}
230
Vivien Didelot57d32312016-06-20 13:13:58 -0400231static int mv88e6xxx_reg_read(struct mv88e6xxx_priv_state *ps, int addr,
232 int reg)
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700233{
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700234 int ret;
235
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -0400236 mutex_lock(&ps->reg_lock);
Andrew Lunn158bc062016-04-28 21:24:06 -0400237 ret = _mv88e6xxx_reg_read(ps, addr, reg);
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -0400238 mutex_unlock(&ps->reg_lock);
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700239
240 return ret;
241}
242
Andrew Lunn158bc062016-04-28 21:24:06 -0400243static int _mv88e6xxx_reg_write(struct mv88e6xxx_priv_state *ps, int addr,
244 int reg, u16 val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000245{
Vivien Didelot914b32f2016-06-20 13:14:11 -0400246 return mv88e6xxx_write(ps, addr, reg, val);
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700247}
248
Vivien Didelot57d32312016-06-20 13:13:58 -0400249static int mv88e6xxx_reg_write(struct mv88e6xxx_priv_state *ps, int addr,
250 int reg, u16 val)
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700251{
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700252 int ret;
253
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -0400254 mutex_lock(&ps->reg_lock);
Andrew Lunn158bc062016-04-28 21:24:06 -0400255 ret = _mv88e6xxx_reg_write(ps, addr, reg, val);
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -0400256 mutex_unlock(&ps->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000257
258 return ret;
259}
260
Vivien Didelot1d13a062016-05-09 13:22:43 -0400261static int mv88e6xxx_set_addr_direct(struct dsa_switch *ds, u8 *addr)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000262{
Andrew Lunn158bc062016-04-28 21:24:06 -0400263 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200264 int err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000265
Andrew Lunn158bc062016-04-28 21:24:06 -0400266 err = mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_MAC_01,
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200267 (addr[0] << 8) | addr[1]);
268 if (err)
269 return err;
270
Andrew Lunn158bc062016-04-28 21:24:06 -0400271 err = mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_MAC_23,
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200272 (addr[2] << 8) | addr[3]);
273 if (err)
274 return err;
275
Andrew Lunn158bc062016-04-28 21:24:06 -0400276 return mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_MAC_45,
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200277 (addr[4] << 8) | addr[5]);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000278}
279
Vivien Didelot1d13a062016-05-09 13:22:43 -0400280static int mv88e6xxx_set_addr_indirect(struct dsa_switch *ds, u8 *addr)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000281{
Andrew Lunn158bc062016-04-28 21:24:06 -0400282 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000283 int ret;
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200284 int i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000285
286 for (i = 0; i < 6; i++) {
287 int j;
288
Barry Grussling3675c8d2013-01-08 16:05:53 +0000289 /* Write the MAC address byte. */
Andrew Lunn158bc062016-04-28 21:24:06 -0400290 ret = mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SWITCH_MAC,
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200291 GLOBAL2_SWITCH_MAC_BUSY |
292 (i << 8) | addr[i]);
293 if (ret)
294 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000295
Barry Grussling3675c8d2013-01-08 16:05:53 +0000296 /* Wait for the write to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000297 for (j = 0; j < 16; j++) {
Andrew Lunn158bc062016-04-28 21:24:06 -0400298 ret = mv88e6xxx_reg_read(ps, REG_GLOBAL2,
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200299 GLOBAL2_SWITCH_MAC);
300 if (ret < 0)
301 return ret;
302
Andrew Lunncca8b132015-04-02 04:06:39 +0200303 if ((ret & GLOBAL2_SWITCH_MAC_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000304 break;
305 }
306 if (j == 16)
307 return -ETIMEDOUT;
308 }
309
310 return 0;
311}
312
Vivien Didelot57d32312016-06-20 13:13:58 -0400313static int mv88e6xxx_set_addr(struct dsa_switch *ds, u8 *addr)
Vivien Didelot1d13a062016-05-09 13:22:43 -0400314{
315 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
316
317 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_SWITCH_MAC))
318 return mv88e6xxx_set_addr_indirect(ds, addr);
319 else
320 return mv88e6xxx_set_addr_direct(ds, addr);
321}
322
Andrew Lunn03a4a542016-06-04 21:17:05 +0200323static int mv88e6xxx_mdio_read_direct(struct mv88e6xxx_priv_state *ps,
324 int addr, int regnum)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000325{
326 if (addr >= 0)
Andrew Lunn158bc062016-04-28 21:24:06 -0400327 return _mv88e6xxx_reg_read(ps, addr, regnum);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000328 return 0xffff;
329}
330
Andrew Lunn03a4a542016-06-04 21:17:05 +0200331static int mv88e6xxx_mdio_write_direct(struct mv88e6xxx_priv_state *ps,
332 int addr, int regnum, u16 val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000333{
334 if (addr >= 0)
Andrew Lunn158bc062016-04-28 21:24:06 -0400335 return _mv88e6xxx_reg_write(ps, addr, regnum, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000336 return 0;
337}
338
Andrew Lunn158bc062016-04-28 21:24:06 -0400339static int mv88e6xxx_ppu_disable(struct mv88e6xxx_priv_state *ps)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000340{
341 int ret;
Barry Grussling19b2f972013-01-08 16:05:54 +0000342 unsigned long timeout;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000343
Vivien Didelot8c9983a2016-05-09 13:22:39 -0400344 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_CONTROL);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200345 if (ret < 0)
346 return ret;
347
Vivien Didelot8c9983a2016-05-09 13:22:39 -0400348 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_CONTROL,
349 ret & ~GLOBAL_CONTROL_PPU_ENABLE);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200350 if (ret)
351 return ret;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000352
Barry Grussling19b2f972013-01-08 16:05:54 +0000353 timeout = jiffies + 1 * HZ;
354 while (time_before(jiffies, timeout)) {
Vivien Didelot8c9983a2016-05-09 13:22:39 -0400355 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_STATUS);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200356 if (ret < 0)
357 return ret;
358
Barry Grussling19b2f972013-01-08 16:05:54 +0000359 usleep_range(1000, 2000);
Andrew Lunncca8b132015-04-02 04:06:39 +0200360 if ((ret & GLOBAL_STATUS_PPU_MASK) !=
361 GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000362 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000363 }
364
365 return -ETIMEDOUT;
366}
367
Andrew Lunn158bc062016-04-28 21:24:06 -0400368static int mv88e6xxx_ppu_enable(struct mv88e6xxx_priv_state *ps)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000369{
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200370 int ret, err;
Barry Grussling19b2f972013-01-08 16:05:54 +0000371 unsigned long timeout;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000372
Vivien Didelot762eb672016-06-04 21:16:54 +0200373 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_CONTROL);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200374 if (ret < 0)
375 return ret;
376
Vivien Didelot762eb672016-06-04 21:16:54 +0200377 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_CONTROL,
378 ret | GLOBAL_CONTROL_PPU_ENABLE);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200379 if (err)
380 return err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000381
Barry Grussling19b2f972013-01-08 16:05:54 +0000382 timeout = jiffies + 1 * HZ;
383 while (time_before(jiffies, timeout)) {
Vivien Didelot762eb672016-06-04 21:16:54 +0200384 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_STATUS);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200385 if (ret < 0)
386 return ret;
387
Barry Grussling19b2f972013-01-08 16:05:54 +0000388 usleep_range(1000, 2000);
Andrew Lunncca8b132015-04-02 04:06:39 +0200389 if ((ret & GLOBAL_STATUS_PPU_MASK) ==
390 GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000391 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000392 }
393
394 return -ETIMEDOUT;
395}
396
397static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
398{
399 struct mv88e6xxx_priv_state *ps;
400
401 ps = container_of(ugly, struct mv88e6xxx_priv_state, ppu_work);
Vivien Didelot762eb672016-06-04 21:16:54 +0200402
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -0400403 mutex_lock(&ps->reg_lock);
Vivien Didelot762eb672016-06-04 21:16:54 +0200404
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000405 if (mutex_trylock(&ps->ppu_mutex)) {
Andrew Lunn158bc062016-04-28 21:24:06 -0400406 if (mv88e6xxx_ppu_enable(ps) == 0)
Barry Grussling85686582013-01-08 16:05:56 +0000407 ps->ppu_disabled = 0;
408 mutex_unlock(&ps->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000409 }
Vivien Didelot762eb672016-06-04 21:16:54 +0200410
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -0400411 mutex_unlock(&ps->reg_lock);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000412}
413
414static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
415{
416 struct mv88e6xxx_priv_state *ps = (void *)_ps;
417
418 schedule_work(&ps->ppu_work);
419}
420
Andrew Lunn158bc062016-04-28 21:24:06 -0400421static int mv88e6xxx_ppu_access_get(struct mv88e6xxx_priv_state *ps)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000422{
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000423 int ret;
424
425 mutex_lock(&ps->ppu_mutex);
426
Barry Grussling3675c8d2013-01-08 16:05:53 +0000427 /* If the PHY polling unit is enabled, disable it so that
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000428 * we can access the PHY registers. If it was already
429 * disabled, cancel the timer that is going to re-enable
430 * it.
431 */
432 if (!ps->ppu_disabled) {
Andrew Lunn158bc062016-04-28 21:24:06 -0400433 ret = mv88e6xxx_ppu_disable(ps);
Barry Grussling85686582013-01-08 16:05:56 +0000434 if (ret < 0) {
435 mutex_unlock(&ps->ppu_mutex);
436 return ret;
437 }
438 ps->ppu_disabled = 1;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000439 } else {
Barry Grussling85686582013-01-08 16:05:56 +0000440 del_timer(&ps->ppu_timer);
441 ret = 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000442 }
443
444 return ret;
445}
446
Andrew Lunn158bc062016-04-28 21:24:06 -0400447static void mv88e6xxx_ppu_access_put(struct mv88e6xxx_priv_state *ps)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000448{
Barry Grussling3675c8d2013-01-08 16:05:53 +0000449 /* Schedule a timer to re-enable the PHY polling unit. */
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000450 mod_timer(&ps->ppu_timer, jiffies + msecs_to_jiffies(10));
451 mutex_unlock(&ps->ppu_mutex);
452}
453
Vivien Didelot57d32312016-06-20 13:13:58 -0400454static void mv88e6xxx_ppu_state_init(struct mv88e6xxx_priv_state *ps)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000455{
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000456 mutex_init(&ps->ppu_mutex);
457 INIT_WORK(&ps->ppu_work, mv88e6xxx_ppu_reenable_work);
458 init_timer(&ps->ppu_timer);
459 ps->ppu_timer.data = (unsigned long)ps;
460 ps->ppu_timer.function = mv88e6xxx_ppu_reenable_timer;
461}
462
Andrew Lunn03a4a542016-06-04 21:17:05 +0200463static int mv88e6xxx_mdio_read_ppu(struct mv88e6xxx_priv_state *ps, int addr,
464 int regnum)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000465{
466 int ret;
467
Andrew Lunn158bc062016-04-28 21:24:06 -0400468 ret = mv88e6xxx_ppu_access_get(ps);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000469 if (ret >= 0) {
Vivien Didelot8c9983a2016-05-09 13:22:39 -0400470 ret = _mv88e6xxx_reg_read(ps, addr, regnum);
Andrew Lunn158bc062016-04-28 21:24:06 -0400471 mv88e6xxx_ppu_access_put(ps);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000472 }
473
474 return ret;
475}
476
Andrew Lunn03a4a542016-06-04 21:17:05 +0200477static int mv88e6xxx_mdio_write_ppu(struct mv88e6xxx_priv_state *ps, int addr,
478 int regnum, u16 val)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000479{
480 int ret;
481
Andrew Lunn158bc062016-04-28 21:24:06 -0400482 ret = mv88e6xxx_ppu_access_get(ps);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000483 if (ret >= 0) {
Vivien Didelot8c9983a2016-05-09 13:22:39 -0400484 ret = _mv88e6xxx_reg_write(ps, addr, regnum, val);
Andrew Lunn158bc062016-04-28 21:24:06 -0400485 mv88e6xxx_ppu_access_put(ps);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000486 }
487
488 return ret;
489}
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000490
Andrew Lunn158bc062016-04-28 21:24:06 -0400491static bool mv88e6xxx_6065_family(struct mv88e6xxx_priv_state *ps)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200492{
Vivien Didelot22356472016-04-17 13:24:00 -0400493 return ps->info->family == MV88E6XXX_FAMILY_6065;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200494}
495
Andrew Lunn158bc062016-04-28 21:24:06 -0400496static bool mv88e6xxx_6095_family(struct mv88e6xxx_priv_state *ps)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200497{
Vivien Didelot22356472016-04-17 13:24:00 -0400498 return ps->info->family == MV88E6XXX_FAMILY_6095;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200499}
500
Andrew Lunn158bc062016-04-28 21:24:06 -0400501static bool mv88e6xxx_6097_family(struct mv88e6xxx_priv_state *ps)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200502{
Vivien Didelot22356472016-04-17 13:24:00 -0400503 return ps->info->family == MV88E6XXX_FAMILY_6097;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200504}
505
Andrew Lunn158bc062016-04-28 21:24:06 -0400506static bool mv88e6xxx_6165_family(struct mv88e6xxx_priv_state *ps)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200507{
Vivien Didelot22356472016-04-17 13:24:00 -0400508 return ps->info->family == MV88E6XXX_FAMILY_6165;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200509}
510
Andrew Lunn158bc062016-04-28 21:24:06 -0400511static bool mv88e6xxx_6185_family(struct mv88e6xxx_priv_state *ps)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200512{
Vivien Didelot22356472016-04-17 13:24:00 -0400513 return ps->info->family == MV88E6XXX_FAMILY_6185;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200514}
515
Andrew Lunn158bc062016-04-28 21:24:06 -0400516static bool mv88e6xxx_6320_family(struct mv88e6xxx_priv_state *ps)
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700517{
Vivien Didelot22356472016-04-17 13:24:00 -0400518 return ps->info->family == MV88E6XXX_FAMILY_6320;
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700519}
520
Andrew Lunn158bc062016-04-28 21:24:06 -0400521static bool mv88e6xxx_6351_family(struct mv88e6xxx_priv_state *ps)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200522{
Vivien Didelot22356472016-04-17 13:24:00 -0400523 return ps->info->family == MV88E6XXX_FAMILY_6351;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200524}
525
Andrew Lunn158bc062016-04-28 21:24:06 -0400526static bool mv88e6xxx_6352_family(struct mv88e6xxx_priv_state *ps)
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200527{
Vivien Didelot22356472016-04-17 13:24:00 -0400528 return ps->info->family == MV88E6XXX_FAMILY_6352;
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200529}
530
Andrew Lunn158bc062016-04-28 21:24:06 -0400531static unsigned int mv88e6xxx_num_databases(struct mv88e6xxx_priv_state *ps)
Vivien Didelotf74df0b2016-03-31 16:53:43 -0400532{
Vivien Didelotcd5a2c82016-04-17 13:24:02 -0400533 return ps->info->num_databases;
Vivien Didelotf74df0b2016-03-31 16:53:43 -0400534}
535
Andrew Lunn158bc062016-04-28 21:24:06 -0400536static bool mv88e6xxx_has_fid_reg(struct mv88e6xxx_priv_state *ps)
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400537{
538 /* Does the device have dedicated FID registers for ATU and VTU ops? */
Andrew Lunn158bc062016-04-28 21:24:06 -0400539 if (mv88e6xxx_6097_family(ps) || mv88e6xxx_6165_family(ps) ||
540 mv88e6xxx_6351_family(ps) || mv88e6xxx_6352_family(ps))
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400541 return true;
542
543 return false;
544}
545
Andrew Lunndea87022015-08-31 15:56:47 +0200546/* We expect the switch to perform auto negotiation if there is a real
547 * phy. However, in the case of a fixed link phy, we force the port
548 * settings from the fixed link settings.
549 */
Vivien Didelotf81ec902016-05-09 13:22:58 -0400550static void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port,
551 struct phy_device *phydev)
Andrew Lunndea87022015-08-31 15:56:47 +0200552{
553 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunn49052872015-09-29 01:53:48 +0200554 u32 reg;
555 int ret;
Andrew Lunndea87022015-08-31 15:56:47 +0200556
557 if (!phy_is_pseudo_fixed_link(phydev))
558 return;
559
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -0400560 mutex_lock(&ps->reg_lock);
Andrew Lunndea87022015-08-31 15:56:47 +0200561
Andrew Lunn158bc062016-04-28 21:24:06 -0400562 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_PCS_CTRL);
Andrew Lunndea87022015-08-31 15:56:47 +0200563 if (ret < 0)
564 goto out;
565
566 reg = ret & ~(PORT_PCS_CTRL_LINK_UP |
567 PORT_PCS_CTRL_FORCE_LINK |
568 PORT_PCS_CTRL_DUPLEX_FULL |
569 PORT_PCS_CTRL_FORCE_DUPLEX |
570 PORT_PCS_CTRL_UNFORCED);
571
572 reg |= PORT_PCS_CTRL_FORCE_LINK;
573 if (phydev->link)
Vivien Didelot57d32312016-06-20 13:13:58 -0400574 reg |= PORT_PCS_CTRL_LINK_UP;
Andrew Lunndea87022015-08-31 15:56:47 +0200575
Andrew Lunn158bc062016-04-28 21:24:06 -0400576 if (mv88e6xxx_6065_family(ps) && phydev->speed > SPEED_100)
Andrew Lunndea87022015-08-31 15:56:47 +0200577 goto out;
578
579 switch (phydev->speed) {
580 case SPEED_1000:
581 reg |= PORT_PCS_CTRL_1000;
582 break;
583 case SPEED_100:
584 reg |= PORT_PCS_CTRL_100;
585 break;
586 case SPEED_10:
587 reg |= PORT_PCS_CTRL_10;
588 break;
589 default:
590 pr_info("Unknown speed");
591 goto out;
592 }
593
594 reg |= PORT_PCS_CTRL_FORCE_DUPLEX;
595 if (phydev->duplex == DUPLEX_FULL)
596 reg |= PORT_PCS_CTRL_DUPLEX_FULL;
597
Andrew Lunn158bc062016-04-28 21:24:06 -0400598 if ((mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps)) &&
Vivien Didelot009a2b92016-04-17 13:24:01 -0400599 (port >= ps->info->num_ports - 2)) {
Andrew Lunne7e72ac2015-08-31 15:56:51 +0200600 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
601 reg |= PORT_PCS_CTRL_RGMII_DELAY_RXCLK;
602 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
603 reg |= PORT_PCS_CTRL_RGMII_DELAY_TXCLK;
604 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
605 reg |= (PORT_PCS_CTRL_RGMII_DELAY_RXCLK |
606 PORT_PCS_CTRL_RGMII_DELAY_TXCLK);
607 }
Andrew Lunn158bc062016-04-28 21:24:06 -0400608 _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_PCS_CTRL, reg);
Andrew Lunndea87022015-08-31 15:56:47 +0200609
610out:
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -0400611 mutex_unlock(&ps->reg_lock);
Andrew Lunndea87022015-08-31 15:56:47 +0200612}
613
Andrew Lunn158bc062016-04-28 21:24:06 -0400614static int _mv88e6xxx_stats_wait(struct mv88e6xxx_priv_state *ps)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000615{
616 int ret;
617 int i;
618
619 for (i = 0; i < 10; i++) {
Andrew Lunn158bc062016-04-28 21:24:06 -0400620 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_STATS_OP);
Andrew Lunncca8b132015-04-02 04:06:39 +0200621 if ((ret & GLOBAL_STATS_OP_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000622 return 0;
623 }
624
625 return -ETIMEDOUT;
626}
627
Andrew Lunn158bc062016-04-28 21:24:06 -0400628static int _mv88e6xxx_stats_snapshot(struct mv88e6xxx_priv_state *ps,
629 int port)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000630{
631 int ret;
632
Andrew Lunn158bc062016-04-28 21:24:06 -0400633 if (mv88e6xxx_6320_family(ps) || mv88e6xxx_6352_family(ps))
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200634 port = (port + 1) << 5;
635
Barry Grussling3675c8d2013-01-08 16:05:53 +0000636 /* Snapshot the hardware statistics counters for this port. */
Andrew Lunn158bc062016-04-28 21:24:06 -0400637 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_STATS_OP,
Andrew Lunn31888232015-05-06 01:09:54 +0200638 GLOBAL_STATS_OP_CAPTURE_PORT |
639 GLOBAL_STATS_OP_HIST_RX_TX | port);
640 if (ret < 0)
641 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000642
Barry Grussling3675c8d2013-01-08 16:05:53 +0000643 /* Wait for the snapshotting to complete. */
Andrew Lunn158bc062016-04-28 21:24:06 -0400644 ret = _mv88e6xxx_stats_wait(ps);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000645 if (ret < 0)
646 return ret;
647
648 return 0;
649}
650
Andrew Lunn158bc062016-04-28 21:24:06 -0400651static void _mv88e6xxx_stats_read(struct mv88e6xxx_priv_state *ps,
652 int stat, u32 *val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000653{
654 u32 _val;
655 int ret;
656
657 *val = 0;
658
Andrew Lunn158bc062016-04-28 21:24:06 -0400659 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_STATS_OP,
Andrew Lunn31888232015-05-06 01:09:54 +0200660 GLOBAL_STATS_OP_READ_CAPTURED |
661 GLOBAL_STATS_OP_HIST_RX_TX | stat);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000662 if (ret < 0)
663 return;
664
Andrew Lunn158bc062016-04-28 21:24:06 -0400665 ret = _mv88e6xxx_stats_wait(ps);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000666 if (ret < 0)
667 return;
668
Andrew Lunn158bc062016-04-28 21:24:06 -0400669 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_STATS_COUNTER_32);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000670 if (ret < 0)
671 return;
672
673 _val = ret << 16;
674
Andrew Lunn158bc062016-04-28 21:24:06 -0400675 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_STATS_COUNTER_01);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000676 if (ret < 0)
677 return;
678
679 *val = _val | ret;
680}
681
Andrew Lunne413e7e2015-04-02 04:06:38 +0200682static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100683 { "in_good_octets", 8, 0x00, BANK0, },
684 { "in_bad_octets", 4, 0x02, BANK0, },
685 { "in_unicast", 4, 0x04, BANK0, },
686 { "in_broadcasts", 4, 0x06, BANK0, },
687 { "in_multicasts", 4, 0x07, BANK0, },
688 { "in_pause", 4, 0x16, BANK0, },
689 { "in_undersize", 4, 0x18, BANK0, },
690 { "in_fragments", 4, 0x19, BANK0, },
691 { "in_oversize", 4, 0x1a, BANK0, },
692 { "in_jabber", 4, 0x1b, BANK0, },
693 { "in_rx_error", 4, 0x1c, BANK0, },
694 { "in_fcs_error", 4, 0x1d, BANK0, },
695 { "out_octets", 8, 0x0e, BANK0, },
696 { "out_unicast", 4, 0x10, BANK0, },
697 { "out_broadcasts", 4, 0x13, BANK0, },
698 { "out_multicasts", 4, 0x12, BANK0, },
699 { "out_pause", 4, 0x15, BANK0, },
700 { "excessive", 4, 0x11, BANK0, },
701 { "collisions", 4, 0x1e, BANK0, },
702 { "deferred", 4, 0x05, BANK0, },
703 { "single", 4, 0x14, BANK0, },
704 { "multiple", 4, 0x17, BANK0, },
705 { "out_fcs_error", 4, 0x03, BANK0, },
706 { "late", 4, 0x1f, BANK0, },
707 { "hist_64bytes", 4, 0x08, BANK0, },
708 { "hist_65_127bytes", 4, 0x09, BANK0, },
709 { "hist_128_255bytes", 4, 0x0a, BANK0, },
710 { "hist_256_511bytes", 4, 0x0b, BANK0, },
711 { "hist_512_1023bytes", 4, 0x0c, BANK0, },
712 { "hist_1024_max_bytes", 4, 0x0d, BANK0, },
713 { "sw_in_discards", 4, 0x10, PORT, },
714 { "sw_in_filtered", 2, 0x12, PORT, },
715 { "sw_out_filtered", 2, 0x13, PORT, },
716 { "in_discards", 4, 0x00 | GLOBAL_STATS_OP_BANK_1, BANK1, },
717 { "in_filtered", 4, 0x01 | GLOBAL_STATS_OP_BANK_1, BANK1, },
718 { "in_accepted", 4, 0x02 | GLOBAL_STATS_OP_BANK_1, BANK1, },
719 { "in_bad_accepted", 4, 0x03 | GLOBAL_STATS_OP_BANK_1, BANK1, },
720 { "in_good_avb_class_a", 4, 0x04 | GLOBAL_STATS_OP_BANK_1, BANK1, },
721 { "in_good_avb_class_b", 4, 0x05 | GLOBAL_STATS_OP_BANK_1, BANK1, },
722 { "in_bad_avb_class_a", 4, 0x06 | GLOBAL_STATS_OP_BANK_1, BANK1, },
723 { "in_bad_avb_class_b", 4, 0x07 | GLOBAL_STATS_OP_BANK_1, BANK1, },
724 { "tcam_counter_0", 4, 0x08 | GLOBAL_STATS_OP_BANK_1, BANK1, },
725 { "tcam_counter_1", 4, 0x09 | GLOBAL_STATS_OP_BANK_1, BANK1, },
726 { "tcam_counter_2", 4, 0x0a | GLOBAL_STATS_OP_BANK_1, BANK1, },
727 { "tcam_counter_3", 4, 0x0b | GLOBAL_STATS_OP_BANK_1, BANK1, },
728 { "in_da_unknown", 4, 0x0e | GLOBAL_STATS_OP_BANK_1, BANK1, },
729 { "in_management", 4, 0x0f | GLOBAL_STATS_OP_BANK_1, BANK1, },
730 { "out_queue_0", 4, 0x10 | GLOBAL_STATS_OP_BANK_1, BANK1, },
731 { "out_queue_1", 4, 0x11 | GLOBAL_STATS_OP_BANK_1, BANK1, },
732 { "out_queue_2", 4, 0x12 | GLOBAL_STATS_OP_BANK_1, BANK1, },
733 { "out_queue_3", 4, 0x13 | GLOBAL_STATS_OP_BANK_1, BANK1, },
734 { "out_queue_4", 4, 0x14 | GLOBAL_STATS_OP_BANK_1, BANK1, },
735 { "out_queue_5", 4, 0x15 | GLOBAL_STATS_OP_BANK_1, BANK1, },
736 { "out_queue_6", 4, 0x16 | GLOBAL_STATS_OP_BANK_1, BANK1, },
737 { "out_queue_7", 4, 0x17 | GLOBAL_STATS_OP_BANK_1, BANK1, },
738 { "out_cut_through", 4, 0x18 | GLOBAL_STATS_OP_BANK_1, BANK1, },
739 { "out_octets_a", 4, 0x1a | GLOBAL_STATS_OP_BANK_1, BANK1, },
740 { "out_octets_b", 4, 0x1b | GLOBAL_STATS_OP_BANK_1, BANK1, },
741 { "out_management", 4, 0x1f | GLOBAL_STATS_OP_BANK_1, BANK1, },
Andrew Lunne413e7e2015-04-02 04:06:38 +0200742};
743
Andrew Lunn158bc062016-04-28 21:24:06 -0400744static bool mv88e6xxx_has_stat(struct mv88e6xxx_priv_state *ps,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100745 struct mv88e6xxx_hw_stat *stat)
Andrew Lunne413e7e2015-04-02 04:06:38 +0200746{
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100747 switch (stat->type) {
748 case BANK0:
Andrew Lunne413e7e2015-04-02 04:06:38 +0200749 return true;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100750 case BANK1:
Andrew Lunn158bc062016-04-28 21:24:06 -0400751 return mv88e6xxx_6320_family(ps);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100752 case PORT:
Andrew Lunn158bc062016-04-28 21:24:06 -0400753 return mv88e6xxx_6095_family(ps) ||
754 mv88e6xxx_6185_family(ps) ||
755 mv88e6xxx_6097_family(ps) ||
756 mv88e6xxx_6165_family(ps) ||
757 mv88e6xxx_6351_family(ps) ||
758 mv88e6xxx_6352_family(ps);
Andrew Lunne413e7e2015-04-02 04:06:38 +0200759 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100760 return false;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000761}
762
Andrew Lunn158bc062016-04-28 21:24:06 -0400763static uint64_t _mv88e6xxx_get_ethtool_stat(struct mv88e6xxx_priv_state *ps,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100764 struct mv88e6xxx_hw_stat *s,
Andrew Lunn80c46272015-06-20 18:42:30 +0200765 int port)
766{
Andrew Lunn80c46272015-06-20 18:42:30 +0200767 u32 low;
768 u32 high = 0;
769 int ret;
770 u64 value;
771
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100772 switch (s->type) {
773 case PORT:
Andrew Lunn158bc062016-04-28 21:24:06 -0400774 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), s->reg);
Andrew Lunn80c46272015-06-20 18:42:30 +0200775 if (ret < 0)
776 return UINT64_MAX;
777
778 low = ret;
779 if (s->sizeof_stat == 4) {
Andrew Lunn158bc062016-04-28 21:24:06 -0400780 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port),
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100781 s->reg + 1);
Andrew Lunn80c46272015-06-20 18:42:30 +0200782 if (ret < 0)
783 return UINT64_MAX;
784 high = ret;
785 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100786 break;
787 case BANK0:
788 case BANK1:
Andrew Lunn158bc062016-04-28 21:24:06 -0400789 _mv88e6xxx_stats_read(ps, s->reg, &low);
Andrew Lunn80c46272015-06-20 18:42:30 +0200790 if (s->sizeof_stat == 8)
Andrew Lunn158bc062016-04-28 21:24:06 -0400791 _mv88e6xxx_stats_read(ps, s->reg + 1, &high);
Andrew Lunn80c46272015-06-20 18:42:30 +0200792 }
793 value = (((u64)high) << 16) | low;
794 return value;
795}
796
Vivien Didelotf81ec902016-05-09 13:22:58 -0400797static void mv88e6xxx_get_strings(struct dsa_switch *ds, int port,
798 uint8_t *data)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100799{
Andrew Lunn158bc062016-04-28 21:24:06 -0400800 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100801 struct mv88e6xxx_hw_stat *stat;
802 int i, j;
803
804 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
805 stat = &mv88e6xxx_hw_stats[i];
Andrew Lunn158bc062016-04-28 21:24:06 -0400806 if (mv88e6xxx_has_stat(ps, stat)) {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100807 memcpy(data + j * ETH_GSTRING_LEN, stat->string,
808 ETH_GSTRING_LEN);
809 j++;
810 }
811 }
812}
813
Vivien Didelotf81ec902016-05-09 13:22:58 -0400814static int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100815{
Andrew Lunn158bc062016-04-28 21:24:06 -0400816 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100817 struct mv88e6xxx_hw_stat *stat;
818 int i, j;
819
820 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
821 stat = &mv88e6xxx_hw_stats[i];
Andrew Lunn158bc062016-04-28 21:24:06 -0400822 if (mv88e6xxx_has_stat(ps, stat))
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100823 j++;
824 }
825 return j;
826}
827
Vivien Didelotf81ec902016-05-09 13:22:58 -0400828static void mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, int port,
829 uint64_t *data)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000830{
Florian Fainellia22adce2014-04-28 11:14:28 -0700831 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100832 struct mv88e6xxx_hw_stat *stat;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000833 int ret;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100834 int i, j;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000835
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -0400836 mutex_lock(&ps->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000837
Andrew Lunn158bc062016-04-28 21:24:06 -0400838 ret = _mv88e6xxx_stats_snapshot(ps, port);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000839 if (ret < 0) {
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -0400840 mutex_unlock(&ps->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000841 return;
842 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100843 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
844 stat = &mv88e6xxx_hw_stats[i];
Andrew Lunn158bc062016-04-28 21:24:06 -0400845 if (mv88e6xxx_has_stat(ps, stat)) {
846 data[j] = _mv88e6xxx_get_ethtool_stat(ps, stat, port);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100847 j++;
848 }
849 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000850
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -0400851 mutex_unlock(&ps->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000852}
Ben Hutchings98e67302011-11-25 14:36:19 +0000853
Vivien Didelotf81ec902016-05-09 13:22:58 -0400854static int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700855{
856 return 32 * sizeof(u16);
857}
858
Vivien Didelotf81ec902016-05-09 13:22:58 -0400859static void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
860 struct ethtool_regs *regs, void *_p)
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700861{
Andrew Lunn158bc062016-04-28 21:24:06 -0400862 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700863 u16 *p = _p;
864 int i;
865
866 regs->version = 0;
867
868 memset(p, 0xff, 32 * sizeof(u16));
869
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -0400870 mutex_lock(&ps->reg_lock);
Vivien Didelot23062512016-05-09 13:22:45 -0400871
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700872 for (i = 0; i < 32; i++) {
873 int ret;
874
Vivien Didelot23062512016-05-09 13:22:45 -0400875 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), i);
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700876 if (ret >= 0)
877 p[i] = ret;
878 }
Vivien Didelot23062512016-05-09 13:22:45 -0400879
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -0400880 mutex_unlock(&ps->reg_lock);
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700881}
882
Andrew Lunn158bc062016-04-28 21:24:06 -0400883static int _mv88e6xxx_wait(struct mv88e6xxx_priv_state *ps, int reg, int offset,
Andrew Lunn3898c142015-05-06 01:09:53 +0200884 u16 mask)
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700885{
886 unsigned long timeout = jiffies + HZ / 10;
887
888 while (time_before(jiffies, timeout)) {
889 int ret;
890
Andrew Lunn158bc062016-04-28 21:24:06 -0400891 ret = _mv88e6xxx_reg_read(ps, reg, offset);
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700892 if (ret < 0)
893 return ret;
894 if (!(ret & mask))
895 return 0;
896
897 usleep_range(1000, 2000);
898 }
899 return -ETIMEDOUT;
900}
901
Andrew Lunn158bc062016-04-28 21:24:06 -0400902static int mv88e6xxx_wait(struct mv88e6xxx_priv_state *ps, int reg,
903 int offset, u16 mask)
Andrew Lunn3898c142015-05-06 01:09:53 +0200904{
Andrew Lunn3898c142015-05-06 01:09:53 +0200905 int ret;
906
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -0400907 mutex_lock(&ps->reg_lock);
Andrew Lunn158bc062016-04-28 21:24:06 -0400908 ret = _mv88e6xxx_wait(ps, reg, offset, mask);
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -0400909 mutex_unlock(&ps->reg_lock);
Andrew Lunn3898c142015-05-06 01:09:53 +0200910
911 return ret;
912}
913
Andrew Lunn03a4a542016-06-04 21:17:05 +0200914static int mv88e6xxx_mdio_wait(struct mv88e6xxx_priv_state *ps)
Andrew Lunn3898c142015-05-06 01:09:53 +0200915{
Andrew Lunn158bc062016-04-28 21:24:06 -0400916 return _mv88e6xxx_wait(ps, REG_GLOBAL2, GLOBAL2_SMI_OP,
Andrew Lunn3898c142015-05-06 01:09:53 +0200917 GLOBAL2_SMI_OP_BUSY);
918}
919
Vivien Didelotd24645b2016-05-09 13:22:41 -0400920static int mv88e6xxx_eeprom_load_wait(struct dsa_switch *ds)
Andrew Lunn3898c142015-05-06 01:09:53 +0200921{
Andrew Lunn158bc062016-04-28 21:24:06 -0400922 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
923
924 return mv88e6xxx_wait(ps, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
Andrew Lunn3898c142015-05-06 01:09:53 +0200925 GLOBAL2_EEPROM_OP_LOAD);
926}
927
Vivien Didelotd24645b2016-05-09 13:22:41 -0400928static int mv88e6xxx_eeprom_busy_wait(struct dsa_switch *ds)
Andrew Lunn3898c142015-05-06 01:09:53 +0200929{
Andrew Lunn158bc062016-04-28 21:24:06 -0400930 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
931
932 return mv88e6xxx_wait(ps, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
Andrew Lunn3898c142015-05-06 01:09:53 +0200933 GLOBAL2_EEPROM_OP_BUSY);
934}
935
Vivien Didelotd24645b2016-05-09 13:22:41 -0400936static int mv88e6xxx_read_eeprom_word(struct dsa_switch *ds, int addr)
937{
938 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
939 int ret;
940
941 mutex_lock(&ps->eeprom_mutex);
942
943 ret = mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
944 GLOBAL2_EEPROM_OP_READ |
945 (addr & GLOBAL2_EEPROM_OP_ADDR_MASK));
946 if (ret < 0)
947 goto error;
948
949 ret = mv88e6xxx_eeprom_busy_wait(ds);
950 if (ret < 0)
951 goto error;
952
953 ret = mv88e6xxx_reg_read(ps, REG_GLOBAL2, GLOBAL2_EEPROM_DATA);
954error:
955 mutex_unlock(&ps->eeprom_mutex);
956 return ret;
957}
958
Andrew Lunnf8cd8752016-05-10 23:27:25 +0200959static int mv88e6xxx_get_eeprom_len(struct dsa_switch *ds)
960{
961 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
962
963 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEPROM))
964 return ps->eeprom_len;
965
966 return 0;
967}
968
Vivien Didelotf81ec902016-05-09 13:22:58 -0400969static int mv88e6xxx_get_eeprom(struct dsa_switch *ds,
970 struct ethtool_eeprom *eeprom, u8 *data)
Vivien Didelotd24645b2016-05-09 13:22:41 -0400971{
972 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
973 int offset;
974 int len;
975 int ret;
976
977 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEPROM))
978 return -EOPNOTSUPP;
979
980 offset = eeprom->offset;
981 len = eeprom->len;
982 eeprom->len = 0;
983
984 eeprom->magic = 0xc3ec4951;
985
986 ret = mv88e6xxx_eeprom_load_wait(ds);
987 if (ret < 0)
988 return ret;
989
990 if (offset & 1) {
991 int word;
992
993 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
994 if (word < 0)
995 return word;
996
997 *data++ = (word >> 8) & 0xff;
998
999 offset++;
1000 len--;
1001 eeprom->len++;
1002 }
1003
1004 while (len >= 2) {
1005 int word;
1006
1007 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
1008 if (word < 0)
1009 return word;
1010
1011 *data++ = word & 0xff;
1012 *data++ = (word >> 8) & 0xff;
1013
1014 offset += 2;
1015 len -= 2;
1016 eeprom->len += 2;
1017 }
1018
1019 if (len) {
1020 int word;
1021
1022 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
1023 if (word < 0)
1024 return word;
1025
1026 *data++ = word & 0xff;
1027
1028 offset++;
1029 len--;
1030 eeprom->len++;
1031 }
1032
1033 return 0;
1034}
1035
1036static int mv88e6xxx_eeprom_is_readonly(struct dsa_switch *ds)
1037{
1038 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1039 int ret;
1040
1041 ret = mv88e6xxx_reg_read(ps, REG_GLOBAL2, GLOBAL2_EEPROM_OP);
1042 if (ret < 0)
1043 return ret;
1044
1045 if (!(ret & GLOBAL2_EEPROM_OP_WRITE_EN))
1046 return -EROFS;
1047
1048 return 0;
1049}
1050
1051static int mv88e6xxx_write_eeprom_word(struct dsa_switch *ds, int addr,
1052 u16 data)
1053{
1054 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1055 int ret;
1056
1057 mutex_lock(&ps->eeprom_mutex);
1058
1059 ret = mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_EEPROM_DATA, data);
1060 if (ret < 0)
1061 goto error;
1062
1063 ret = mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
1064 GLOBAL2_EEPROM_OP_WRITE |
1065 (addr & GLOBAL2_EEPROM_OP_ADDR_MASK));
1066 if (ret < 0)
1067 goto error;
1068
1069 ret = mv88e6xxx_eeprom_busy_wait(ds);
1070error:
1071 mutex_unlock(&ps->eeprom_mutex);
1072 return ret;
1073}
1074
Vivien Didelotf81ec902016-05-09 13:22:58 -04001075static int mv88e6xxx_set_eeprom(struct dsa_switch *ds,
1076 struct ethtool_eeprom *eeprom, u8 *data)
Vivien Didelotd24645b2016-05-09 13:22:41 -04001077{
1078 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1079 int offset;
1080 int ret;
1081 int len;
1082
1083 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEPROM))
1084 return -EOPNOTSUPP;
1085
1086 if (eeprom->magic != 0xc3ec4951)
1087 return -EINVAL;
1088
1089 ret = mv88e6xxx_eeprom_is_readonly(ds);
1090 if (ret)
1091 return ret;
1092
1093 offset = eeprom->offset;
1094 len = eeprom->len;
1095 eeprom->len = 0;
1096
1097 ret = mv88e6xxx_eeprom_load_wait(ds);
1098 if (ret < 0)
1099 return ret;
1100
1101 if (offset & 1) {
1102 int word;
1103
1104 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
1105 if (word < 0)
1106 return word;
1107
1108 word = (*data++ << 8) | (word & 0xff);
1109
1110 ret = mv88e6xxx_write_eeprom_word(ds, offset >> 1, word);
1111 if (ret < 0)
1112 return ret;
1113
1114 offset++;
1115 len--;
1116 eeprom->len++;
1117 }
1118
1119 while (len >= 2) {
1120 int word;
1121
1122 word = *data++;
1123 word |= *data++ << 8;
1124
1125 ret = mv88e6xxx_write_eeprom_word(ds, offset >> 1, word);
1126 if (ret < 0)
1127 return ret;
1128
1129 offset += 2;
1130 len -= 2;
1131 eeprom->len += 2;
1132 }
1133
1134 if (len) {
1135 int word;
1136
1137 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
1138 if (word < 0)
1139 return word;
1140
1141 word = (word & 0xff00) | *data++;
1142
1143 ret = mv88e6xxx_write_eeprom_word(ds, offset >> 1, word);
1144 if (ret < 0)
1145 return ret;
1146
1147 offset++;
1148 len--;
1149 eeprom->len++;
1150 }
1151
1152 return 0;
1153}
1154
Andrew Lunn158bc062016-04-28 21:24:06 -04001155static int _mv88e6xxx_atu_wait(struct mv88e6xxx_priv_state *ps)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001156{
Andrew Lunn158bc062016-04-28 21:24:06 -04001157 return _mv88e6xxx_wait(ps, REG_GLOBAL, GLOBAL_ATU_OP,
Andrew Lunncca8b132015-04-02 04:06:39 +02001158 GLOBAL_ATU_OP_BUSY);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001159}
1160
Andrew Lunn03a4a542016-06-04 21:17:05 +02001161static int mv88e6xxx_mdio_read_indirect(struct mv88e6xxx_priv_state *ps,
Andrew Lunn158bc062016-04-28 21:24:06 -04001162 int addr, int regnum)
Andrew Lunnf3044682015-02-14 19:17:50 +01001163{
1164 int ret;
1165
Andrew Lunn158bc062016-04-28 21:24:06 -04001166 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SMI_OP,
Andrew Lunn3898c142015-05-06 01:09:53 +02001167 GLOBAL2_SMI_OP_22_READ | (addr << 5) |
1168 regnum);
Andrew Lunnf3044682015-02-14 19:17:50 +01001169 if (ret < 0)
1170 return ret;
1171
Andrew Lunn03a4a542016-06-04 21:17:05 +02001172 ret = mv88e6xxx_mdio_wait(ps);
Andrew Lunn3898c142015-05-06 01:09:53 +02001173 if (ret < 0)
1174 return ret;
1175
Andrew Lunn158bc062016-04-28 21:24:06 -04001176 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL2, GLOBAL2_SMI_DATA);
1177
1178 return ret;
Andrew Lunnf3044682015-02-14 19:17:50 +01001179}
1180
Andrew Lunn03a4a542016-06-04 21:17:05 +02001181static int mv88e6xxx_mdio_write_indirect(struct mv88e6xxx_priv_state *ps,
Andrew Lunn158bc062016-04-28 21:24:06 -04001182 int addr, int regnum, u16 val)
Andrew Lunnf3044682015-02-14 19:17:50 +01001183{
Andrew Lunn3898c142015-05-06 01:09:53 +02001184 int ret;
Andrew Lunnf3044682015-02-14 19:17:50 +01001185
Andrew Lunn158bc062016-04-28 21:24:06 -04001186 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SMI_DATA, val);
Andrew Lunn3898c142015-05-06 01:09:53 +02001187 if (ret < 0)
1188 return ret;
1189
Andrew Lunn158bc062016-04-28 21:24:06 -04001190 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SMI_OP,
Andrew Lunn3898c142015-05-06 01:09:53 +02001191 GLOBAL2_SMI_OP_22_WRITE | (addr << 5) |
1192 regnum);
1193
Andrew Lunn03a4a542016-06-04 21:17:05 +02001194 return mv88e6xxx_mdio_wait(ps);
Andrew Lunnf3044682015-02-14 19:17:50 +01001195}
1196
Vivien Didelotf81ec902016-05-09 13:22:58 -04001197static int mv88e6xxx_get_eee(struct dsa_switch *ds, int port,
1198 struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -08001199{
Andrew Lunn2f40c692015-04-02 04:06:37 +02001200 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Guenter Roeck11b3b452015-03-06 22:23:51 -08001201 int reg;
1202
Vivien Didelotaadbdb82016-05-09 13:22:44 -04001203 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEE))
1204 return -EOPNOTSUPP;
1205
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04001206 mutex_lock(&ps->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001207
Andrew Lunn03a4a542016-06-04 21:17:05 +02001208 reg = mv88e6xxx_mdio_read_indirect(ps, port, 16);
Guenter Roeck11b3b452015-03-06 22:23:51 -08001209 if (reg < 0)
Andrew Lunn2f40c692015-04-02 04:06:37 +02001210 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001211
1212 e->eee_enabled = !!(reg & 0x0200);
1213 e->tx_lpi_enabled = !!(reg & 0x0100);
1214
Andrew Lunn158bc062016-04-28 21:24:06 -04001215 reg = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_STATUS);
Guenter Roeck11b3b452015-03-06 22:23:51 -08001216 if (reg < 0)
Andrew Lunn2f40c692015-04-02 04:06:37 +02001217 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001218
Andrew Lunncca8b132015-04-02 04:06:39 +02001219 e->eee_active = !!(reg & PORT_STATUS_EEE);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001220 reg = 0;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001221
Andrew Lunn2f40c692015-04-02 04:06:37 +02001222out:
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04001223 mutex_unlock(&ps->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001224 return reg;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001225}
1226
Vivien Didelotf81ec902016-05-09 13:22:58 -04001227static int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
1228 struct phy_device *phydev, struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -08001229{
Andrew Lunn2f40c692015-04-02 04:06:37 +02001230 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1231 int reg;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001232 int ret;
1233
Vivien Didelotaadbdb82016-05-09 13:22:44 -04001234 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEE))
1235 return -EOPNOTSUPP;
1236
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04001237 mutex_lock(&ps->reg_lock);
Guenter Roeck11b3b452015-03-06 22:23:51 -08001238
Andrew Lunn03a4a542016-06-04 21:17:05 +02001239 ret = mv88e6xxx_mdio_read_indirect(ps, port, 16);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001240 if (ret < 0)
1241 goto out;
1242
1243 reg = ret & ~0x0300;
1244 if (e->eee_enabled)
1245 reg |= 0x0200;
1246 if (e->tx_lpi_enabled)
1247 reg |= 0x0100;
1248
Andrew Lunn03a4a542016-06-04 21:17:05 +02001249 ret = mv88e6xxx_mdio_write_indirect(ps, port, 16, reg);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001250out:
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04001251 mutex_unlock(&ps->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001252
1253 return ret;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001254}
1255
Andrew Lunn158bc062016-04-28 21:24:06 -04001256static int _mv88e6xxx_atu_cmd(struct mv88e6xxx_priv_state *ps, u16 fid, u16 cmd)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001257{
1258 int ret;
1259
Andrew Lunn158bc062016-04-28 21:24:06 -04001260 if (mv88e6xxx_has_fid_reg(ps)) {
1261 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_FID, fid);
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001262 if (ret < 0)
1263 return ret;
Andrew Lunn158bc062016-04-28 21:24:06 -04001264 } else if (mv88e6xxx_num_databases(ps) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001265 /* ATU DBNum[7:4] are located in ATU Control 15:12 */
Andrew Lunn158bc062016-04-28 21:24:06 -04001266 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_ATU_CONTROL);
Vivien Didelot11ea8092016-03-31 16:53:44 -04001267 if (ret < 0)
1268 return ret;
1269
Andrew Lunn158bc062016-04-28 21:24:06 -04001270 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_CONTROL,
Vivien Didelot11ea8092016-03-31 16:53:44 -04001271 (ret & 0xfff) |
1272 ((fid << 8) & 0xf000));
1273 if (ret < 0)
1274 return ret;
1275
1276 /* ATU DBNum[3:0] are located in ATU Operation 3:0 */
1277 cmd |= fid & 0xf;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001278 }
1279
Andrew Lunn158bc062016-04-28 21:24:06 -04001280 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_OP, cmd);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001281 if (ret < 0)
1282 return ret;
1283
Andrew Lunn158bc062016-04-28 21:24:06 -04001284 return _mv88e6xxx_atu_wait(ps);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001285}
1286
Andrew Lunn158bc062016-04-28 21:24:06 -04001287static int _mv88e6xxx_atu_data_write(struct mv88e6xxx_priv_state *ps,
Vivien Didelot37705b72015-09-04 14:34:11 -04001288 struct mv88e6xxx_atu_entry *entry)
1289{
1290 u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK;
1291
1292 if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) {
1293 unsigned int mask, shift;
1294
1295 if (entry->trunk) {
1296 data |= GLOBAL_ATU_DATA_TRUNK;
1297 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
1298 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
1299 } else {
1300 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
1301 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
1302 }
1303
1304 data |= (entry->portv_trunkid << shift) & mask;
1305 }
1306
Andrew Lunn158bc062016-04-28 21:24:06 -04001307 return _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_DATA, data);
Vivien Didelot37705b72015-09-04 14:34:11 -04001308}
1309
Andrew Lunn158bc062016-04-28 21:24:06 -04001310static int _mv88e6xxx_atu_flush_move(struct mv88e6xxx_priv_state *ps,
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001311 struct mv88e6xxx_atu_entry *entry,
1312 bool static_too)
1313{
1314 int op;
1315 int err;
1316
Andrew Lunn158bc062016-04-28 21:24:06 -04001317 err = _mv88e6xxx_atu_wait(ps);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001318 if (err)
1319 return err;
1320
Andrew Lunn158bc062016-04-28 21:24:06 -04001321 err = _mv88e6xxx_atu_data_write(ps, entry);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001322 if (err)
1323 return err;
1324
1325 if (entry->fid) {
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001326 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB :
1327 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB;
1328 } else {
1329 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL :
1330 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC;
1331 }
1332
Andrew Lunn158bc062016-04-28 21:24:06 -04001333 return _mv88e6xxx_atu_cmd(ps, entry->fid, op);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001334}
1335
Andrew Lunn158bc062016-04-28 21:24:06 -04001336static int _mv88e6xxx_atu_flush(struct mv88e6xxx_priv_state *ps,
1337 u16 fid, bool static_too)
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001338{
1339 struct mv88e6xxx_atu_entry entry = {
1340 .fid = fid,
1341 .state = 0, /* EntryState bits must be 0 */
1342 };
1343
Andrew Lunn158bc062016-04-28 21:24:06 -04001344 return _mv88e6xxx_atu_flush_move(ps, &entry, static_too);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001345}
1346
Andrew Lunn158bc062016-04-28 21:24:06 -04001347static int _mv88e6xxx_atu_move(struct mv88e6xxx_priv_state *ps, u16 fid,
1348 int from_port, int to_port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001349{
1350 struct mv88e6xxx_atu_entry entry = {
1351 .trunk = false,
1352 .fid = fid,
1353 };
1354
1355 /* EntryState bits must be 0xF */
1356 entry.state = GLOBAL_ATU_DATA_STATE_MASK;
1357
1358 /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */
1359 entry.portv_trunkid = (to_port & 0x0f) << 4;
1360 entry.portv_trunkid |= from_port & 0x0f;
1361
Andrew Lunn158bc062016-04-28 21:24:06 -04001362 return _mv88e6xxx_atu_flush_move(ps, &entry, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001363}
1364
Andrew Lunn158bc062016-04-28 21:24:06 -04001365static int _mv88e6xxx_atu_remove(struct mv88e6xxx_priv_state *ps, u16 fid,
1366 int port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001367{
1368 /* Destination port 0xF means remove the entries */
Andrew Lunn158bc062016-04-28 21:24:06 -04001369 return _mv88e6xxx_atu_move(ps, fid, port, 0x0f, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001370}
1371
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001372static const char * const mv88e6xxx_port_state_names[] = {
1373 [PORT_CONTROL_STATE_DISABLED] = "Disabled",
1374 [PORT_CONTROL_STATE_BLOCKING] = "Blocking/Listening",
1375 [PORT_CONTROL_STATE_LEARNING] = "Learning",
1376 [PORT_CONTROL_STATE_FORWARDING] = "Forwarding",
1377};
1378
Andrew Lunn158bc062016-04-28 21:24:06 -04001379static int _mv88e6xxx_port_state(struct mv88e6xxx_priv_state *ps, int port,
1380 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001381{
Andrew Lunn158bc062016-04-28 21:24:06 -04001382 struct dsa_switch *ds = ps->ds;
Geert Uytterhoevenc3ffe6d2015-04-16 20:49:14 +02001383 int reg, ret = 0;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001384 u8 oldstate;
1385
Andrew Lunn158bc062016-04-28 21:24:06 -04001386 reg = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_CONTROL);
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001387 if (reg < 0)
1388 return reg;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001389
Andrew Lunncca8b132015-04-02 04:06:39 +02001390 oldstate = reg & PORT_CONTROL_STATE_MASK;
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001391
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001392 if (oldstate != state) {
1393 /* Flush forwarding database if we're moving a port
1394 * from Learning or Forwarding state to Disabled or
1395 * Blocking or Listening state.
1396 */
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001397 if ((oldstate == PORT_CONTROL_STATE_LEARNING ||
Vivien Didelot57d32312016-06-20 13:13:58 -04001398 oldstate == PORT_CONTROL_STATE_FORWARDING) &&
1399 (state == PORT_CONTROL_STATE_DISABLED ||
1400 state == PORT_CONTROL_STATE_BLOCKING)) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001401 ret = _mv88e6xxx_atu_remove(ps, 0, port, false);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001402 if (ret)
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001403 return ret;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001404 }
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001405
Andrew Lunncca8b132015-04-02 04:06:39 +02001406 reg = (reg & ~PORT_CONTROL_STATE_MASK) | state;
Andrew Lunn158bc062016-04-28 21:24:06 -04001407 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL,
Andrew Lunncca8b132015-04-02 04:06:39 +02001408 reg);
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001409 if (ret)
1410 return ret;
1411
Andrew Lunnc8b09802016-06-04 21:16:57 +02001412 netdev_dbg(ds->ports[port].netdev, "PortState %s (was %s)\n",
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001413 mv88e6xxx_port_state_names[state],
1414 mv88e6xxx_port_state_names[oldstate]);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001415 }
1416
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001417 return ret;
1418}
1419
Andrew Lunn158bc062016-04-28 21:24:06 -04001420static int _mv88e6xxx_port_based_vlan_map(struct mv88e6xxx_priv_state *ps,
1421 int port)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001422{
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001423 struct net_device *bridge = ps->ports[port].bridge_dev;
Vivien Didelot009a2b92016-04-17 13:24:01 -04001424 const u16 mask = (1 << ps->info->num_ports) - 1;
Andrew Lunn158bc062016-04-28 21:24:06 -04001425 struct dsa_switch *ds = ps->ds;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001426 u16 output_ports = 0;
Vivien Didelotede80982015-10-11 18:08:35 -04001427 int reg;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001428 int i;
1429
1430 /* allow CPU port or DSA link(s) to send frames to every port */
1431 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
1432 output_ports = mask;
1433 } else {
Vivien Didelot009a2b92016-04-17 13:24:01 -04001434 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001435 /* allow sending frames to every group member */
1436 if (bridge && ps->ports[i].bridge_dev == bridge)
1437 output_ports |= BIT(i);
1438
1439 /* allow sending frames to CPU port and DSA link(s) */
1440 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
1441 output_ports |= BIT(i);
1442 }
1443 }
1444
1445 /* prevent frames from going back out of the port they came in on */
1446 output_ports &= ~BIT(port);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001447
Andrew Lunn158bc062016-04-28 21:24:06 -04001448 reg = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_BASE_VLAN);
Vivien Didelotede80982015-10-11 18:08:35 -04001449 if (reg < 0)
1450 return reg;
1451
1452 reg &= ~mask;
1453 reg |= output_ports & mask;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001454
Andrew Lunn158bc062016-04-28 21:24:06 -04001455 return _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_BASE_VLAN, reg);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001456}
1457
Vivien Didelotf81ec902016-05-09 13:22:58 -04001458static void mv88e6xxx_port_stp_state_set(struct dsa_switch *ds, int port,
1459 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001460{
1461 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1462 int stp_state;
Vivien Didelot553eb542016-05-13 20:38:23 -04001463 int err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001464
Vivien Didelot936f2342016-05-09 13:22:46 -04001465 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_PORTSTATE))
1466 return;
1467
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001468 switch (state) {
1469 case BR_STATE_DISABLED:
Andrew Lunncca8b132015-04-02 04:06:39 +02001470 stp_state = PORT_CONTROL_STATE_DISABLED;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001471 break;
1472 case BR_STATE_BLOCKING:
1473 case BR_STATE_LISTENING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001474 stp_state = PORT_CONTROL_STATE_BLOCKING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001475 break;
1476 case BR_STATE_LEARNING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001477 stp_state = PORT_CONTROL_STATE_LEARNING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001478 break;
1479 case BR_STATE_FORWARDING:
1480 default:
Andrew Lunncca8b132015-04-02 04:06:39 +02001481 stp_state = PORT_CONTROL_STATE_FORWARDING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001482 break;
1483 }
1484
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04001485 mutex_lock(&ps->reg_lock);
Vivien Didelot553eb542016-05-13 20:38:23 -04001486 err = _mv88e6xxx_port_state(ps, port, stp_state);
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04001487 mutex_unlock(&ps->reg_lock);
Vivien Didelot553eb542016-05-13 20:38:23 -04001488
1489 if (err)
Andrew Lunnc8b09802016-06-04 21:16:57 +02001490 netdev_err(ds->ports[port].netdev,
1491 "failed to update state to %s\n",
Vivien Didelot553eb542016-05-13 20:38:23 -04001492 mv88e6xxx_port_state_names[stp_state]);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001493}
1494
Andrew Lunn158bc062016-04-28 21:24:06 -04001495static int _mv88e6xxx_port_pvid(struct mv88e6xxx_priv_state *ps, int port,
1496 u16 *new, u16 *old)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001497{
Andrew Lunn158bc062016-04-28 21:24:06 -04001498 struct dsa_switch *ds = ps->ds;
Vivien Didelot5da96032016-03-07 18:24:39 -05001499 u16 pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001500 int ret;
1501
Andrew Lunn158bc062016-04-28 21:24:06 -04001502 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_DEFAULT_VLAN);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001503 if (ret < 0)
1504 return ret;
1505
Vivien Didelot5da96032016-03-07 18:24:39 -05001506 pvid = ret & PORT_DEFAULT_VLAN_MASK;
1507
1508 if (new) {
1509 ret &= ~PORT_DEFAULT_VLAN_MASK;
1510 ret |= *new & PORT_DEFAULT_VLAN_MASK;
1511
Andrew Lunn158bc062016-04-28 21:24:06 -04001512 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Vivien Didelot5da96032016-03-07 18:24:39 -05001513 PORT_DEFAULT_VLAN, ret);
1514 if (ret < 0)
1515 return ret;
1516
Andrew Lunnc8b09802016-06-04 21:16:57 +02001517 netdev_dbg(ds->ports[port].netdev,
1518 "DefaultVID %d (was %d)\n", *new, pvid);
Vivien Didelot5da96032016-03-07 18:24:39 -05001519 }
1520
1521 if (old)
1522 *old = pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001523
1524 return 0;
1525}
1526
Andrew Lunn158bc062016-04-28 21:24:06 -04001527static int _mv88e6xxx_port_pvid_get(struct mv88e6xxx_priv_state *ps,
1528 int port, u16 *pvid)
Vivien Didelot5da96032016-03-07 18:24:39 -05001529{
Andrew Lunn158bc062016-04-28 21:24:06 -04001530 return _mv88e6xxx_port_pvid(ps, port, NULL, pvid);
Vivien Didelot5da96032016-03-07 18:24:39 -05001531}
1532
Andrew Lunn158bc062016-04-28 21:24:06 -04001533static int _mv88e6xxx_port_pvid_set(struct mv88e6xxx_priv_state *ps,
1534 int port, u16 pvid)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001535{
Andrew Lunn158bc062016-04-28 21:24:06 -04001536 return _mv88e6xxx_port_pvid(ps, port, &pvid, NULL);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001537}
1538
Andrew Lunn158bc062016-04-28 21:24:06 -04001539static int _mv88e6xxx_vtu_wait(struct mv88e6xxx_priv_state *ps)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001540{
Andrew Lunn158bc062016-04-28 21:24:06 -04001541 return _mv88e6xxx_wait(ps, REG_GLOBAL, GLOBAL_VTU_OP,
Vivien Didelot6b17e862015-08-13 12:52:18 -04001542 GLOBAL_VTU_OP_BUSY);
1543}
1544
Andrew Lunn158bc062016-04-28 21:24:06 -04001545static int _mv88e6xxx_vtu_cmd(struct mv88e6xxx_priv_state *ps, u16 op)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001546{
1547 int ret;
1548
Andrew Lunn158bc062016-04-28 21:24:06 -04001549 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_OP, op);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001550 if (ret < 0)
1551 return ret;
1552
Andrew Lunn158bc062016-04-28 21:24:06 -04001553 return _mv88e6xxx_vtu_wait(ps);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001554}
1555
Andrew Lunn158bc062016-04-28 21:24:06 -04001556static int _mv88e6xxx_vtu_stu_flush(struct mv88e6xxx_priv_state *ps)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001557{
1558 int ret;
1559
Andrew Lunn158bc062016-04-28 21:24:06 -04001560 ret = _mv88e6xxx_vtu_wait(ps);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001561 if (ret < 0)
1562 return ret;
1563
Andrew Lunn158bc062016-04-28 21:24:06 -04001564 return _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_FLUSH_ALL);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001565}
1566
Andrew Lunn158bc062016-04-28 21:24:06 -04001567static int _mv88e6xxx_vtu_stu_data_read(struct mv88e6xxx_priv_state *ps,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001568 struct mv88e6xxx_vtu_stu_entry *entry,
1569 unsigned int nibble_offset)
1570{
Vivien Didelotb8fee952015-08-13 12:52:19 -04001571 u16 regs[3];
1572 int i;
1573 int ret;
1574
1575 for (i = 0; i < 3; ++i) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001576 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001577 GLOBAL_VTU_DATA_0_3 + i);
1578 if (ret < 0)
1579 return ret;
1580
1581 regs[i] = ret;
1582 }
1583
Vivien Didelot009a2b92016-04-17 13:24:01 -04001584 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelotb8fee952015-08-13 12:52:19 -04001585 unsigned int shift = (i % 4) * 4 + nibble_offset;
1586 u16 reg = regs[i / 4];
1587
1588 entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
1589 }
1590
1591 return 0;
1592}
1593
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001594static int mv88e6xxx_vtu_data_read(struct mv88e6xxx_priv_state *ps,
1595 struct mv88e6xxx_vtu_stu_entry *entry)
1596{
1597 return _mv88e6xxx_vtu_stu_data_read(ps, entry, 0);
1598}
1599
1600static int mv88e6xxx_stu_data_read(struct mv88e6xxx_priv_state *ps,
1601 struct mv88e6xxx_vtu_stu_entry *entry)
1602{
1603 return _mv88e6xxx_vtu_stu_data_read(ps, entry, 2);
1604}
1605
Andrew Lunn158bc062016-04-28 21:24:06 -04001606static int _mv88e6xxx_vtu_stu_data_write(struct mv88e6xxx_priv_state *ps,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001607 struct mv88e6xxx_vtu_stu_entry *entry,
1608 unsigned int nibble_offset)
1609{
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001610 u16 regs[3] = { 0 };
1611 int i;
1612 int ret;
1613
Vivien Didelot009a2b92016-04-17 13:24:01 -04001614 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001615 unsigned int shift = (i % 4) * 4 + nibble_offset;
1616 u8 data = entry->data[i];
1617
1618 regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
1619 }
1620
1621 for (i = 0; i < 3; ++i) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001622 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001623 GLOBAL_VTU_DATA_0_3 + i, regs[i]);
1624 if (ret < 0)
1625 return ret;
1626 }
1627
1628 return 0;
1629}
1630
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001631static int mv88e6xxx_vtu_data_write(struct mv88e6xxx_priv_state *ps,
1632 struct mv88e6xxx_vtu_stu_entry *entry)
1633{
1634 return _mv88e6xxx_vtu_stu_data_write(ps, entry, 0);
1635}
1636
1637static int mv88e6xxx_stu_data_write(struct mv88e6xxx_priv_state *ps,
1638 struct mv88e6xxx_vtu_stu_entry *entry)
1639{
1640 return _mv88e6xxx_vtu_stu_data_write(ps, entry, 2);
1641}
1642
Andrew Lunn158bc062016-04-28 21:24:06 -04001643static int _mv88e6xxx_vtu_vid_write(struct mv88e6xxx_priv_state *ps, u16 vid)
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001644{
Andrew Lunn158bc062016-04-28 21:24:06 -04001645 return _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_VID,
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001646 vid & GLOBAL_VTU_VID_MASK);
1647}
1648
Andrew Lunn158bc062016-04-28 21:24:06 -04001649static int _mv88e6xxx_vtu_getnext(struct mv88e6xxx_priv_state *ps,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001650 struct mv88e6xxx_vtu_stu_entry *entry)
1651{
1652 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1653 int ret;
1654
Andrew Lunn158bc062016-04-28 21:24:06 -04001655 ret = _mv88e6xxx_vtu_wait(ps);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001656 if (ret < 0)
1657 return ret;
1658
Andrew Lunn158bc062016-04-28 21:24:06 -04001659 ret = _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_VTU_GET_NEXT);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001660 if (ret < 0)
1661 return ret;
1662
Andrew Lunn158bc062016-04-28 21:24:06 -04001663 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_VTU_VID);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001664 if (ret < 0)
1665 return ret;
1666
1667 next.vid = ret & GLOBAL_VTU_VID_MASK;
1668 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1669
1670 if (next.valid) {
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001671 ret = mv88e6xxx_vtu_data_read(ps, &next);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001672 if (ret < 0)
1673 return ret;
1674
Andrew Lunn158bc062016-04-28 21:24:06 -04001675 if (mv88e6xxx_has_fid_reg(ps)) {
1676 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001677 GLOBAL_VTU_FID);
1678 if (ret < 0)
1679 return ret;
1680
1681 next.fid = ret & GLOBAL_VTU_FID_MASK;
Andrew Lunn158bc062016-04-28 21:24:06 -04001682 } else if (mv88e6xxx_num_databases(ps) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001683 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1684 * VTU DBNum[3:0] are located in VTU Operation 3:0
1685 */
Andrew Lunn158bc062016-04-28 21:24:06 -04001686 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
Vivien Didelot11ea8092016-03-31 16:53:44 -04001687 GLOBAL_VTU_OP);
1688 if (ret < 0)
1689 return ret;
1690
1691 next.fid = (ret & 0xf00) >> 4;
1692 next.fid |= ret & 0xf;
Vivien Didelot2e7bd5e2016-03-31 16:53:41 -04001693 }
Vivien Didelotb8fee952015-08-13 12:52:19 -04001694
Vivien Didelotcb9b9022016-05-10 15:44:29 -04001695 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_STU)) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001696 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001697 GLOBAL_VTU_SID);
1698 if (ret < 0)
1699 return ret;
1700
1701 next.sid = ret & GLOBAL_VTU_SID_MASK;
1702 }
1703 }
1704
1705 *entry = next;
1706 return 0;
1707}
1708
Vivien Didelotf81ec902016-05-09 13:22:58 -04001709static int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
1710 struct switchdev_obj_port_vlan *vlan,
1711 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001712{
1713 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1714 struct mv88e6xxx_vtu_stu_entry next;
1715 u16 pvid;
1716 int err;
1717
Vivien Didelot54d77b52016-05-09 13:22:47 -04001718 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU))
1719 return -EOPNOTSUPP;
1720
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04001721 mutex_lock(&ps->reg_lock);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001722
Andrew Lunn158bc062016-04-28 21:24:06 -04001723 err = _mv88e6xxx_port_pvid_get(ps, port, &pvid);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001724 if (err)
1725 goto unlock;
1726
Andrew Lunn158bc062016-04-28 21:24:06 -04001727 err = _mv88e6xxx_vtu_vid_write(ps, GLOBAL_VTU_VID_MASK);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001728 if (err)
1729 goto unlock;
1730
1731 do {
Andrew Lunn158bc062016-04-28 21:24:06 -04001732 err = _mv88e6xxx_vtu_getnext(ps, &next);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001733 if (err)
1734 break;
1735
1736 if (!next.valid)
1737 break;
1738
1739 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1740 continue;
1741
1742 /* reinit and dump this VLAN obj */
Vivien Didelot57d32312016-06-20 13:13:58 -04001743 vlan->vid_begin = next.vid;
1744 vlan->vid_end = next.vid;
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001745 vlan->flags = 0;
1746
1747 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1748 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1749
1750 if (next.vid == pvid)
1751 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1752
1753 err = cb(&vlan->obj);
1754 if (err)
1755 break;
1756 } while (next.vid < GLOBAL_VTU_VID_MASK);
1757
1758unlock:
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04001759 mutex_unlock(&ps->reg_lock);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001760
1761 return err;
1762}
1763
Andrew Lunn158bc062016-04-28 21:24:06 -04001764static int _mv88e6xxx_vtu_loadpurge(struct mv88e6xxx_priv_state *ps,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001765 struct mv88e6xxx_vtu_stu_entry *entry)
1766{
Vivien Didelot11ea8092016-03-31 16:53:44 -04001767 u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001768 u16 reg = 0;
1769 int ret;
1770
Andrew Lunn158bc062016-04-28 21:24:06 -04001771 ret = _mv88e6xxx_vtu_wait(ps);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001772 if (ret < 0)
1773 return ret;
1774
1775 if (!entry->valid)
1776 goto loadpurge;
1777
1778 /* Write port member tags */
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001779 ret = mv88e6xxx_vtu_data_write(ps, entry);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001780 if (ret < 0)
1781 return ret;
1782
Vivien Didelotcb9b9022016-05-10 15:44:29 -04001783 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_STU)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001784 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Andrew Lunn158bc062016-04-28 21:24:06 -04001785 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_SID, reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001786 if (ret < 0)
1787 return ret;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001788 }
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001789
Andrew Lunn158bc062016-04-28 21:24:06 -04001790 if (mv88e6xxx_has_fid_reg(ps)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001791 reg = entry->fid & GLOBAL_VTU_FID_MASK;
Andrew Lunn158bc062016-04-28 21:24:06 -04001792 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_FID, reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001793 if (ret < 0)
1794 return ret;
Andrew Lunn158bc062016-04-28 21:24:06 -04001795 } else if (mv88e6xxx_num_databases(ps) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001796 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1797 * VTU DBNum[3:0] are located in VTU Operation 3:0
1798 */
1799 op |= (entry->fid & 0xf0) << 8;
1800 op |= entry->fid & 0xf;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001801 }
1802
1803 reg = GLOBAL_VTU_VID_VALID;
1804loadpurge:
1805 reg |= entry->vid & GLOBAL_VTU_VID_MASK;
Andrew Lunn158bc062016-04-28 21:24:06 -04001806 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_VID, reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001807 if (ret < 0)
1808 return ret;
1809
Andrew Lunn158bc062016-04-28 21:24:06 -04001810 return _mv88e6xxx_vtu_cmd(ps, op);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001811}
1812
Andrew Lunn158bc062016-04-28 21:24:06 -04001813static int _mv88e6xxx_stu_getnext(struct mv88e6xxx_priv_state *ps, u8 sid,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001814 struct mv88e6xxx_vtu_stu_entry *entry)
1815{
1816 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1817 int ret;
1818
Andrew Lunn158bc062016-04-28 21:24:06 -04001819 ret = _mv88e6xxx_vtu_wait(ps);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001820 if (ret < 0)
1821 return ret;
1822
Andrew Lunn158bc062016-04-28 21:24:06 -04001823 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_SID,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001824 sid & GLOBAL_VTU_SID_MASK);
1825 if (ret < 0)
1826 return ret;
1827
Andrew Lunn158bc062016-04-28 21:24:06 -04001828 ret = _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_STU_GET_NEXT);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001829 if (ret < 0)
1830 return ret;
1831
Andrew Lunn158bc062016-04-28 21:24:06 -04001832 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_VTU_SID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001833 if (ret < 0)
1834 return ret;
1835
1836 next.sid = ret & GLOBAL_VTU_SID_MASK;
1837
Andrew Lunn158bc062016-04-28 21:24:06 -04001838 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_VTU_VID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001839 if (ret < 0)
1840 return ret;
1841
1842 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1843
1844 if (next.valid) {
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001845 ret = mv88e6xxx_stu_data_read(ps, &next);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001846 if (ret < 0)
1847 return ret;
1848 }
1849
1850 *entry = next;
1851 return 0;
1852}
1853
Andrew Lunn158bc062016-04-28 21:24:06 -04001854static int _mv88e6xxx_stu_loadpurge(struct mv88e6xxx_priv_state *ps,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001855 struct mv88e6xxx_vtu_stu_entry *entry)
1856{
1857 u16 reg = 0;
1858 int ret;
1859
Andrew Lunn158bc062016-04-28 21:24:06 -04001860 ret = _mv88e6xxx_vtu_wait(ps);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001861 if (ret < 0)
1862 return ret;
1863
1864 if (!entry->valid)
1865 goto loadpurge;
1866
1867 /* Write port states */
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001868 ret = mv88e6xxx_stu_data_write(ps, entry);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001869 if (ret < 0)
1870 return ret;
1871
1872 reg = GLOBAL_VTU_VID_VALID;
1873loadpurge:
Andrew Lunn158bc062016-04-28 21:24:06 -04001874 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_VID, reg);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001875 if (ret < 0)
1876 return ret;
1877
1878 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Andrew Lunn158bc062016-04-28 21:24:06 -04001879 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_SID, reg);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001880 if (ret < 0)
1881 return ret;
1882
Andrew Lunn158bc062016-04-28 21:24:06 -04001883 return _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_STU_LOAD_PURGE);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001884}
1885
Andrew Lunn158bc062016-04-28 21:24:06 -04001886static int _mv88e6xxx_port_fid(struct mv88e6xxx_priv_state *ps, int port,
1887 u16 *new, u16 *old)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001888{
Andrew Lunn158bc062016-04-28 21:24:06 -04001889 struct dsa_switch *ds = ps->ds;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001890 u16 upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001891 u16 fid;
1892 int ret;
1893
Andrew Lunn158bc062016-04-28 21:24:06 -04001894 if (mv88e6xxx_num_databases(ps) == 4096)
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001895 upper_mask = 0xff;
Andrew Lunn158bc062016-04-28 21:24:06 -04001896 else if (mv88e6xxx_num_databases(ps) == 256)
Vivien Didelot11ea8092016-03-31 16:53:44 -04001897 upper_mask = 0xf;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001898 else
1899 return -EOPNOTSUPP;
1900
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001901 /* Port's default FID bits 3:0 are located in reg 0x06, offset 12 */
Andrew Lunn158bc062016-04-28 21:24:06 -04001902 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_BASE_VLAN);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001903 if (ret < 0)
1904 return ret;
1905
1906 fid = (ret & PORT_BASE_VLAN_FID_3_0_MASK) >> 12;
1907
1908 if (new) {
1909 ret &= ~PORT_BASE_VLAN_FID_3_0_MASK;
1910 ret |= (*new << 12) & PORT_BASE_VLAN_FID_3_0_MASK;
1911
Andrew Lunn158bc062016-04-28 21:24:06 -04001912 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_BASE_VLAN,
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001913 ret);
1914 if (ret < 0)
1915 return ret;
1916 }
1917
1918 /* Port's default FID bits 11:4 are located in reg 0x05, offset 0 */
Andrew Lunn158bc062016-04-28 21:24:06 -04001919 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_CONTROL_1);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001920 if (ret < 0)
1921 return ret;
1922
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001923 fid |= (ret & upper_mask) << 4;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001924
1925 if (new) {
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001926 ret &= ~upper_mask;
1927 ret |= (*new >> 4) & upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001928
Andrew Lunn158bc062016-04-28 21:24:06 -04001929 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL_1,
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001930 ret);
1931 if (ret < 0)
1932 return ret;
1933
Andrew Lunnc8b09802016-06-04 21:16:57 +02001934 netdev_dbg(ds->ports[port].netdev,
1935 "FID %d (was %d)\n", *new, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001936 }
1937
1938 if (old)
1939 *old = fid;
1940
1941 return 0;
1942}
1943
Andrew Lunn158bc062016-04-28 21:24:06 -04001944static int _mv88e6xxx_port_fid_get(struct mv88e6xxx_priv_state *ps,
1945 int port, u16 *fid)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001946{
Andrew Lunn158bc062016-04-28 21:24:06 -04001947 return _mv88e6xxx_port_fid(ps, port, NULL, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001948}
1949
Andrew Lunn158bc062016-04-28 21:24:06 -04001950static int _mv88e6xxx_port_fid_set(struct mv88e6xxx_priv_state *ps,
1951 int port, u16 fid)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001952{
Andrew Lunn158bc062016-04-28 21:24:06 -04001953 return _mv88e6xxx_port_fid(ps, port, &fid, NULL);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001954}
1955
Andrew Lunn158bc062016-04-28 21:24:06 -04001956static int _mv88e6xxx_fid_new(struct mv88e6xxx_priv_state *ps, u16 *fid)
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001957{
1958 DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
1959 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001960 int i, err;
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001961
1962 bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);
1963
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001964 /* Set every FID bit used by the (un)bridged ports */
Vivien Didelot009a2b92016-04-17 13:24:01 -04001965 for (i = 0; i < ps->info->num_ports; ++i) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001966 err = _mv88e6xxx_port_fid_get(ps, i, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001967 if (err)
1968 return err;
1969
1970 set_bit(*fid, fid_bitmap);
1971 }
1972
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001973 /* Set every FID bit used by the VLAN entries */
Andrew Lunn158bc062016-04-28 21:24:06 -04001974 err = _mv88e6xxx_vtu_vid_write(ps, GLOBAL_VTU_VID_MASK);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001975 if (err)
1976 return err;
1977
1978 do {
Andrew Lunn158bc062016-04-28 21:24:06 -04001979 err = _mv88e6xxx_vtu_getnext(ps, &vlan);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001980 if (err)
1981 return err;
1982
1983 if (!vlan.valid)
1984 break;
1985
1986 set_bit(vlan.fid, fid_bitmap);
1987 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
1988
1989 /* The reset value 0x000 is used to indicate that multiple address
1990 * databases are not needed. Return the next positive available.
1991 */
1992 *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1);
Andrew Lunn158bc062016-04-28 21:24:06 -04001993 if (unlikely(*fid >= mv88e6xxx_num_databases(ps)))
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001994 return -ENOSPC;
1995
1996 /* Clear the database */
Andrew Lunn158bc062016-04-28 21:24:06 -04001997 return _mv88e6xxx_atu_flush(ps, *fid, true);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001998}
1999
Andrew Lunn158bc062016-04-28 21:24:06 -04002000static int _mv88e6xxx_vtu_new(struct mv88e6xxx_priv_state *ps, u16 vid,
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05002001 struct mv88e6xxx_vtu_stu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002002{
Andrew Lunn158bc062016-04-28 21:24:06 -04002003 struct dsa_switch *ds = ps->ds;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002004 struct mv88e6xxx_vtu_stu_entry vlan = {
2005 .valid = true,
2006 .vid = vid,
2007 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002008 int i, err;
2009
Andrew Lunn158bc062016-04-28 21:24:06 -04002010 err = _mv88e6xxx_fid_new(ps, &vlan.fid);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002011 if (err)
2012 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002013
Vivien Didelot3d131f02015-11-03 10:52:52 -05002014 /* exclude all ports except the CPU and DSA ports */
Vivien Didelot009a2b92016-04-17 13:24:01 -04002015 for (i = 0; i < ps->info->num_ports; ++i)
Vivien Didelot3d131f02015-11-03 10:52:52 -05002016 vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)
2017 ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED
2018 : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002019
Andrew Lunn158bc062016-04-28 21:24:06 -04002020 if (mv88e6xxx_6097_family(ps) || mv88e6xxx_6165_family(ps) ||
2021 mv88e6xxx_6351_family(ps) || mv88e6xxx_6352_family(ps)) {
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002022 struct mv88e6xxx_vtu_stu_entry vstp;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002023
2024 /* Adding a VTU entry requires a valid STU entry. As VSTP is not
2025 * implemented, only one STU entry is needed to cover all VTU
2026 * entries. Thus, validate the SID 0.
2027 */
2028 vlan.sid = 0;
Andrew Lunn158bc062016-04-28 21:24:06 -04002029 err = _mv88e6xxx_stu_getnext(ps, GLOBAL_VTU_SID_MASK, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002030 if (err)
2031 return err;
2032
2033 if (vstp.sid != vlan.sid || !vstp.valid) {
2034 memset(&vstp, 0, sizeof(vstp));
2035 vstp.valid = true;
2036 vstp.sid = vlan.sid;
2037
Andrew Lunn158bc062016-04-28 21:24:06 -04002038 err = _mv88e6xxx_stu_loadpurge(ps, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002039 if (err)
2040 return err;
2041 }
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002042 }
2043
2044 *entry = vlan;
2045 return 0;
2046}
2047
Andrew Lunn158bc062016-04-28 21:24:06 -04002048static int _mv88e6xxx_vtu_get(struct mv88e6xxx_priv_state *ps, u16 vid,
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05002049 struct mv88e6xxx_vtu_stu_entry *entry, bool creat)
2050{
2051 int err;
2052
2053 if (!vid)
2054 return -EINVAL;
2055
Andrew Lunn158bc062016-04-28 21:24:06 -04002056 err = _mv88e6xxx_vtu_vid_write(ps, vid - 1);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05002057 if (err)
2058 return err;
2059
Andrew Lunn158bc062016-04-28 21:24:06 -04002060 err = _mv88e6xxx_vtu_getnext(ps, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05002061 if (err)
2062 return err;
2063
2064 if (entry->vid != vid || !entry->valid) {
2065 if (!creat)
2066 return -EOPNOTSUPP;
2067 /* -ENOENT would've been more appropriate, but switchdev expects
2068 * -EOPNOTSUPP to inform bridge about an eventual software VLAN.
2069 */
2070
Andrew Lunn158bc062016-04-28 21:24:06 -04002071 err = _mv88e6xxx_vtu_new(ps, vid, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05002072 }
2073
2074 return err;
2075}
2076
Vivien Didelotda9c3592016-02-12 12:09:40 -05002077static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
2078 u16 vid_begin, u16 vid_end)
2079{
2080 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2081 struct mv88e6xxx_vtu_stu_entry vlan;
2082 int i, err;
2083
2084 if (!vid_begin)
2085 return -EOPNOTSUPP;
2086
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04002087 mutex_lock(&ps->reg_lock);
Vivien Didelotda9c3592016-02-12 12:09:40 -05002088
Andrew Lunn158bc062016-04-28 21:24:06 -04002089 err = _mv88e6xxx_vtu_vid_write(ps, vid_begin - 1);
Vivien Didelotda9c3592016-02-12 12:09:40 -05002090 if (err)
2091 goto unlock;
2092
2093 do {
Andrew Lunn158bc062016-04-28 21:24:06 -04002094 err = _mv88e6xxx_vtu_getnext(ps, &vlan);
Vivien Didelotda9c3592016-02-12 12:09:40 -05002095 if (err)
2096 goto unlock;
2097
2098 if (!vlan.valid)
2099 break;
2100
2101 if (vlan.vid > vid_end)
2102 break;
2103
Vivien Didelot009a2b92016-04-17 13:24:01 -04002104 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelotda9c3592016-02-12 12:09:40 -05002105 if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i))
2106 continue;
2107
2108 if (vlan.data[i] ==
2109 GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
2110 continue;
2111
2112 if (ps->ports[i].bridge_dev ==
2113 ps->ports[port].bridge_dev)
2114 break; /* same bridge, check next VLAN */
2115
Andrew Lunnc8b09802016-06-04 21:16:57 +02002116 netdev_warn(ds->ports[port].netdev,
Vivien Didelotda9c3592016-02-12 12:09:40 -05002117 "hardware VLAN %d already used by %s\n",
2118 vlan.vid,
2119 netdev_name(ps->ports[i].bridge_dev));
2120 err = -EOPNOTSUPP;
2121 goto unlock;
2122 }
2123 } while (vlan.vid < vid_end);
2124
2125unlock:
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04002126 mutex_unlock(&ps->reg_lock);
Vivien Didelotda9c3592016-02-12 12:09:40 -05002127
2128 return err;
2129}
2130
Vivien Didelot214cdb92016-02-26 13:16:08 -05002131static const char * const mv88e6xxx_port_8021q_mode_names[] = {
2132 [PORT_CONTROL_2_8021Q_DISABLED] = "Disabled",
2133 [PORT_CONTROL_2_8021Q_FALLBACK] = "Fallback",
2134 [PORT_CONTROL_2_8021Q_CHECK] = "Check",
2135 [PORT_CONTROL_2_8021Q_SECURE] = "Secure",
2136};
2137
Vivien Didelotf81ec902016-05-09 13:22:58 -04002138static int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port,
2139 bool vlan_filtering)
Vivien Didelot214cdb92016-02-26 13:16:08 -05002140{
2141 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2142 u16 old, new = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE :
2143 PORT_CONTROL_2_8021Q_DISABLED;
2144 int ret;
2145
Vivien Didelot54d77b52016-05-09 13:22:47 -04002146 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU))
2147 return -EOPNOTSUPP;
2148
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04002149 mutex_lock(&ps->reg_lock);
Vivien Didelot214cdb92016-02-26 13:16:08 -05002150
Andrew Lunn158bc062016-04-28 21:24:06 -04002151 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_CONTROL_2);
Vivien Didelot214cdb92016-02-26 13:16:08 -05002152 if (ret < 0)
2153 goto unlock;
2154
2155 old = ret & PORT_CONTROL_2_8021Q_MASK;
2156
Vivien Didelot5220ef12016-03-07 18:24:52 -05002157 if (new != old) {
2158 ret &= ~PORT_CONTROL_2_8021Q_MASK;
2159 ret |= new & PORT_CONTROL_2_8021Q_MASK;
Vivien Didelot214cdb92016-02-26 13:16:08 -05002160
Andrew Lunn158bc062016-04-28 21:24:06 -04002161 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL_2,
Vivien Didelot5220ef12016-03-07 18:24:52 -05002162 ret);
2163 if (ret < 0)
2164 goto unlock;
Vivien Didelot214cdb92016-02-26 13:16:08 -05002165
Andrew Lunnc8b09802016-06-04 21:16:57 +02002166 netdev_dbg(ds->ports[port].netdev, "802.1Q Mode %s (was %s)\n",
Vivien Didelot5220ef12016-03-07 18:24:52 -05002167 mv88e6xxx_port_8021q_mode_names[new],
2168 mv88e6xxx_port_8021q_mode_names[old]);
2169 }
2170
2171 ret = 0;
Vivien Didelot214cdb92016-02-26 13:16:08 -05002172unlock:
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04002173 mutex_unlock(&ps->reg_lock);
Vivien Didelot214cdb92016-02-26 13:16:08 -05002174
2175 return ret;
2176}
2177
Vivien Didelot57d32312016-06-20 13:13:58 -04002178static int
2179mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
2180 const struct switchdev_obj_port_vlan *vlan,
2181 struct switchdev_trans *trans)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002182{
Vivien Didelot54d77b52016-05-09 13:22:47 -04002183 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelotda9c3592016-02-12 12:09:40 -05002184 int err;
2185
Vivien Didelot54d77b52016-05-09 13:22:47 -04002186 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU))
2187 return -EOPNOTSUPP;
2188
Vivien Didelotda9c3592016-02-12 12:09:40 -05002189 /* If the requested port doesn't belong to the same bridge as the VLAN
2190 * members, do not support it (yet) and fallback to software VLAN.
2191 */
2192 err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin,
2193 vlan->vid_end);
2194 if (err)
2195 return err;
2196
Vivien Didelot76e398a2015-11-01 12:33:55 -05002197 /* We don't need any dynamic resource from the kernel (yet),
2198 * so skip the prepare phase.
2199 */
2200 return 0;
2201}
2202
Andrew Lunn158bc062016-04-28 21:24:06 -04002203static int _mv88e6xxx_port_vlan_add(struct mv88e6xxx_priv_state *ps, int port,
2204 u16 vid, bool untagged)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002205{
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002206 struct mv88e6xxx_vtu_stu_entry vlan;
2207 int err;
2208
Andrew Lunn158bc062016-04-28 21:24:06 -04002209 err = _mv88e6xxx_vtu_get(ps, vid, &vlan, true);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002210 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002211 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002212
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002213 vlan.data[port] = untagged ?
2214 GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
2215 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
2216
Andrew Lunn158bc062016-04-28 21:24:06 -04002217 return _mv88e6xxx_vtu_loadpurge(ps, &vlan);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002218}
2219
Vivien Didelotf81ec902016-05-09 13:22:58 -04002220static void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
2221 const struct switchdev_obj_port_vlan *vlan,
2222 struct switchdev_trans *trans)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002223{
2224 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2225 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
2226 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
2227 u16 vid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05002228
Vivien Didelot54d77b52016-05-09 13:22:47 -04002229 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU))
2230 return;
2231
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04002232 mutex_lock(&ps->reg_lock);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002233
Vivien Didelot4d5770b2016-04-06 11:55:05 -04002234 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
Andrew Lunn158bc062016-04-28 21:24:06 -04002235 if (_mv88e6xxx_port_vlan_add(ps, port, vid, untagged))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002236 netdev_err(ds->ports[port].netdev,
2237 "failed to add VLAN %d%c\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04002238 vid, untagged ? 'u' : 't');
Vivien Didelot76e398a2015-11-01 12:33:55 -05002239
Andrew Lunn158bc062016-04-28 21:24:06 -04002240 if (pvid && _mv88e6xxx_port_pvid_set(ps, port, vlan->vid_end))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002241 netdev_err(ds->ports[port].netdev, "failed to set PVID %d\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04002242 vlan->vid_end);
2243
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04002244 mutex_unlock(&ps->reg_lock);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002245}
2246
Andrew Lunn158bc062016-04-28 21:24:06 -04002247static int _mv88e6xxx_port_vlan_del(struct mv88e6xxx_priv_state *ps,
2248 int port, u16 vid)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002249{
Andrew Lunn158bc062016-04-28 21:24:06 -04002250 struct dsa_switch *ds = ps->ds;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002251 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002252 int i, err;
2253
Andrew Lunn158bc062016-04-28 21:24:06 -04002254 err = _mv88e6xxx_vtu_get(ps, vid, &vlan, false);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002255 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002256 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002257
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05002258 /* Tell switchdev if this VLAN is handled in software */
2259 if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
Vivien Didelot3c06f082016-02-05 14:04:39 -05002260 return -EOPNOTSUPP;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002261
2262 vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
2263
2264 /* keep the VLAN unless all ports are excluded */
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002265 vlan.valid = false;
Vivien Didelot009a2b92016-04-17 13:24:01 -04002266 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelot3d131f02015-11-03 10:52:52 -05002267 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002268 continue;
2269
2270 if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002271 vlan.valid = true;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002272 break;
2273 }
2274 }
2275
Andrew Lunn158bc062016-04-28 21:24:06 -04002276 err = _mv88e6xxx_vtu_loadpurge(ps, &vlan);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002277 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002278 return err;
2279
Andrew Lunn158bc062016-04-28 21:24:06 -04002280 return _mv88e6xxx_atu_remove(ps, vlan.fid, port, false);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002281}
2282
Vivien Didelotf81ec902016-05-09 13:22:58 -04002283static int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
2284 const struct switchdev_obj_port_vlan *vlan)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002285{
2286 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2287 u16 pvid, vid;
2288 int err = 0;
2289
Vivien Didelot54d77b52016-05-09 13:22:47 -04002290 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU))
2291 return -EOPNOTSUPP;
2292
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04002293 mutex_lock(&ps->reg_lock);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002294
Andrew Lunn158bc062016-04-28 21:24:06 -04002295 err = _mv88e6xxx_port_pvid_get(ps, port, &pvid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002296 if (err)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002297 goto unlock;
2298
Vivien Didelot76e398a2015-11-01 12:33:55 -05002299 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002300 err = _mv88e6xxx_port_vlan_del(ps, port, vid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002301 if (err)
2302 goto unlock;
2303
2304 if (vid == pvid) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002305 err = _mv88e6xxx_port_pvid_set(ps, port, 0);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002306 if (err)
2307 goto unlock;
2308 }
2309 }
2310
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002311unlock:
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04002312 mutex_unlock(&ps->reg_lock);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002313
2314 return err;
2315}
2316
Andrew Lunn158bc062016-04-28 21:24:06 -04002317static int _mv88e6xxx_atu_mac_write(struct mv88e6xxx_priv_state *ps,
Vivien Didelotc5723ac2015-08-10 09:09:48 -04002318 const unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002319{
2320 int i, ret;
2321
2322 for (i = 0; i < 3; i++) {
Andrew Lunncca8b132015-04-02 04:06:39 +02002323 ret = _mv88e6xxx_reg_write(
Andrew Lunn158bc062016-04-28 21:24:06 -04002324 ps, REG_GLOBAL, GLOBAL_ATU_MAC_01 + i,
Andrew Lunncca8b132015-04-02 04:06:39 +02002325 (addr[i * 2] << 8) | addr[i * 2 + 1]);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002326 if (ret < 0)
2327 return ret;
2328 }
2329
2330 return 0;
2331}
2332
Andrew Lunn158bc062016-04-28 21:24:06 -04002333static int _mv88e6xxx_atu_mac_read(struct mv88e6xxx_priv_state *ps,
2334 unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002335{
2336 int i, ret;
2337
2338 for (i = 0; i < 3; i++) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002339 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
Andrew Lunncca8b132015-04-02 04:06:39 +02002340 GLOBAL_ATU_MAC_01 + i);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002341 if (ret < 0)
2342 return ret;
2343 addr[i * 2] = ret >> 8;
2344 addr[i * 2 + 1] = ret & 0xff;
2345 }
2346
2347 return 0;
2348}
2349
Andrew Lunn158bc062016-04-28 21:24:06 -04002350static int _mv88e6xxx_atu_load(struct mv88e6xxx_priv_state *ps,
Vivien Didelotfd231c82015-08-10 09:09:50 -04002351 struct mv88e6xxx_atu_entry *entry)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002352{
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002353 int ret;
2354
Andrew Lunn158bc062016-04-28 21:24:06 -04002355 ret = _mv88e6xxx_atu_wait(ps);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002356 if (ret < 0)
2357 return ret;
2358
Andrew Lunn158bc062016-04-28 21:24:06 -04002359 ret = _mv88e6xxx_atu_mac_write(ps, entry->mac);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002360 if (ret < 0)
2361 return ret;
2362
Andrew Lunn158bc062016-04-28 21:24:06 -04002363 ret = _mv88e6xxx_atu_data_write(ps, entry);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002364 if (ret < 0)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002365 return ret;
2366
Andrew Lunn158bc062016-04-28 21:24:06 -04002367 return _mv88e6xxx_atu_cmd(ps, entry->fid, GLOBAL_ATU_OP_LOAD_DB);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002368}
David S. Millercdf09692015-08-11 12:00:37 -07002369
Andrew Lunn158bc062016-04-28 21:24:06 -04002370static int _mv88e6xxx_port_fdb_load(struct mv88e6xxx_priv_state *ps, int port,
Vivien Didelotfd231c82015-08-10 09:09:50 -04002371 const unsigned char *addr, u16 vid,
2372 u8 state)
2373{
2374 struct mv88e6xxx_atu_entry entry = { 0 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002375 struct mv88e6xxx_vtu_stu_entry vlan;
2376 int err;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002377
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002378 /* Null VLAN ID corresponds to the port private database */
2379 if (vid == 0)
Andrew Lunn158bc062016-04-28 21:24:06 -04002380 err = _mv88e6xxx_port_fid_get(ps, port, &vlan.fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002381 else
Andrew Lunn158bc062016-04-28 21:24:06 -04002382 err = _mv88e6xxx_vtu_get(ps, vid, &vlan, false);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002383 if (err)
2384 return err;
2385
2386 entry.fid = vlan.fid;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002387 entry.state = state;
2388 ether_addr_copy(entry.mac, addr);
2389 if (state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2390 entry.trunk = false;
2391 entry.portv_trunkid = BIT(port);
2392 }
2393
Andrew Lunn158bc062016-04-28 21:24:06 -04002394 return _mv88e6xxx_atu_load(ps, &entry);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002395}
2396
Vivien Didelotf81ec902016-05-09 13:22:58 -04002397static int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
2398 const struct switchdev_obj_port_fdb *fdb,
2399 struct switchdev_trans *trans)
Vivien Didelot146a3202015-10-08 11:35:12 -04002400{
Vivien Didelot2672f822016-05-09 13:22:48 -04002401 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2402
2403 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_ATU))
2404 return -EOPNOTSUPP;
2405
Vivien Didelot146a3202015-10-08 11:35:12 -04002406 /* We don't need any dynamic resource from the kernel (yet),
2407 * so skip the prepare phase.
2408 */
2409 return 0;
2410}
2411
Vivien Didelotf81ec902016-05-09 13:22:58 -04002412static void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
2413 const struct switchdev_obj_port_fdb *fdb,
2414 struct switchdev_trans *trans)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002415{
Vivien Didelot1f36faf2015-10-08 11:35:13 -04002416 int state = is_multicast_ether_addr(fdb->addr) ?
David S. Millercdf09692015-08-11 12:00:37 -07002417 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2418 GLOBAL_ATU_DATA_STATE_UC_STATIC;
2419 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelot6630e232015-08-06 01:44:07 -04002420
Vivien Didelot2672f822016-05-09 13:22:48 -04002421 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_ATU))
2422 return;
2423
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04002424 mutex_lock(&ps->reg_lock);
Andrew Lunn158bc062016-04-28 21:24:06 -04002425 if (_mv88e6xxx_port_fdb_load(ps, port, fdb->addr, fdb->vid, state))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002426 netdev_err(ds->ports[port].netdev,
2427 "failed to load MAC address\n");
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04002428 mutex_unlock(&ps->reg_lock);
David S. Millercdf09692015-08-11 12:00:37 -07002429}
2430
Vivien Didelotf81ec902016-05-09 13:22:58 -04002431static int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
2432 const struct switchdev_obj_port_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -07002433{
2434 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2435 int ret;
2436
Vivien Didelot2672f822016-05-09 13:22:48 -04002437 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_ATU))
2438 return -EOPNOTSUPP;
2439
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04002440 mutex_lock(&ps->reg_lock);
Andrew Lunn158bc062016-04-28 21:24:06 -04002441 ret = _mv88e6xxx_port_fdb_load(ps, port, fdb->addr, fdb->vid,
David S. Millercdf09692015-08-11 12:00:37 -07002442 GLOBAL_ATU_DATA_STATE_UNUSED);
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04002443 mutex_unlock(&ps->reg_lock);
David S. Millercdf09692015-08-11 12:00:37 -07002444
2445 return ret;
2446}
2447
Andrew Lunn158bc062016-04-28 21:24:06 -04002448static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_priv_state *ps, u16 fid,
Vivien Didelot1d194042015-08-10 09:09:51 -04002449 struct mv88e6xxx_atu_entry *entry)
David S. Millercdf09692015-08-11 12:00:37 -07002450{
Vivien Didelot1d194042015-08-10 09:09:51 -04002451 struct mv88e6xxx_atu_entry next = { 0 };
2452 int ret;
2453
2454 next.fid = fid;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002455
Andrew Lunn158bc062016-04-28 21:24:06 -04002456 ret = _mv88e6xxx_atu_wait(ps);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002457 if (ret < 0)
2458 return ret;
2459
Andrew Lunn158bc062016-04-28 21:24:06 -04002460 ret = _mv88e6xxx_atu_cmd(ps, fid, GLOBAL_ATU_OP_GET_NEXT_DB);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002461 if (ret < 0)
2462 return ret;
2463
Andrew Lunn158bc062016-04-28 21:24:06 -04002464 ret = _mv88e6xxx_atu_mac_read(ps, next.mac);
Vivien Didelot1d194042015-08-10 09:09:51 -04002465 if (ret < 0)
2466 return ret;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002467
Andrew Lunn158bc062016-04-28 21:24:06 -04002468 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_ATU_DATA);
Vivien Didelot1d194042015-08-10 09:09:51 -04002469 if (ret < 0)
2470 return ret;
2471
2472 next.state = ret & GLOBAL_ATU_DATA_STATE_MASK;
2473 if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2474 unsigned int mask, shift;
2475
2476 if (ret & GLOBAL_ATU_DATA_TRUNK) {
2477 next.trunk = true;
2478 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
2479 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
2480 } else {
2481 next.trunk = false;
2482 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
2483 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
2484 }
2485
2486 next.portv_trunkid = (ret & mask) >> shift;
2487 }
2488
2489 *entry = next;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002490 return 0;
2491}
2492
Andrew Lunn158bc062016-04-28 21:24:06 -04002493static int _mv88e6xxx_port_fdb_dump_one(struct mv88e6xxx_priv_state *ps,
2494 u16 fid, u16 vid, int port,
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002495 struct switchdev_obj_port_fdb *fdb,
2496 int (*cb)(struct switchdev_obj *obj))
2497{
2498 struct mv88e6xxx_atu_entry addr = {
2499 .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2500 };
2501 int err;
2502
Andrew Lunn158bc062016-04-28 21:24:06 -04002503 err = _mv88e6xxx_atu_mac_write(ps, addr.mac);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002504 if (err)
2505 return err;
2506
2507 do {
Andrew Lunn158bc062016-04-28 21:24:06 -04002508 err = _mv88e6xxx_atu_getnext(ps, fid, &addr);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002509 if (err)
2510 break;
2511
2512 if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2513 break;
2514
2515 if (!addr.trunk && addr.portv_trunkid & BIT(port)) {
2516 bool is_static = addr.state ==
2517 (is_multicast_ether_addr(addr.mac) ?
2518 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2519 GLOBAL_ATU_DATA_STATE_UC_STATIC);
2520
2521 fdb->vid = vid;
2522 ether_addr_copy(fdb->addr, addr.mac);
2523 fdb->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
2524
2525 err = cb(&fdb->obj);
2526 if (err)
2527 break;
2528 }
2529 } while (!is_broadcast_ether_addr(addr.mac));
2530
2531 return err;
2532}
2533
Vivien Didelotf81ec902016-05-09 13:22:58 -04002534static int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
2535 struct switchdev_obj_port_fdb *fdb,
2536 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotf33475b2015-10-22 09:34:41 -04002537{
2538 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2539 struct mv88e6xxx_vtu_stu_entry vlan = {
2540 .vid = GLOBAL_VTU_VID_MASK, /* all ones */
2541 };
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002542 u16 fid;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002543 int err;
2544
Vivien Didelot2672f822016-05-09 13:22:48 -04002545 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_ATU))
2546 return -EOPNOTSUPP;
2547
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04002548 mutex_lock(&ps->reg_lock);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002549
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002550 /* Dump port's default Filtering Information Database (VLAN ID 0) */
Andrew Lunn158bc062016-04-28 21:24:06 -04002551 err = _mv88e6xxx_port_fid_get(ps, port, &fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002552 if (err)
2553 goto unlock;
2554
Andrew Lunn158bc062016-04-28 21:24:06 -04002555 err = _mv88e6xxx_port_fdb_dump_one(ps, fid, 0, port, fdb, cb);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002556 if (err)
2557 goto unlock;
2558
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002559 /* Dump VLANs' Filtering Information Databases */
Andrew Lunn158bc062016-04-28 21:24:06 -04002560 err = _mv88e6xxx_vtu_vid_write(ps, vlan.vid);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002561 if (err)
2562 goto unlock;
2563
2564 do {
Andrew Lunn158bc062016-04-28 21:24:06 -04002565 err = _mv88e6xxx_vtu_getnext(ps, &vlan);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002566 if (err)
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002567 break;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002568
2569 if (!vlan.valid)
2570 break;
2571
Andrew Lunn158bc062016-04-28 21:24:06 -04002572 err = _mv88e6xxx_port_fdb_dump_one(ps, vlan.fid, vlan.vid, port,
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002573 fdb, cb);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002574 if (err)
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002575 break;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002576 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
2577
2578unlock:
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04002579 mutex_unlock(&ps->reg_lock);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002580
2581 return err;
2582}
2583
Vivien Didelotf81ec902016-05-09 13:22:58 -04002584static int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
2585 struct net_device *bridge)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002586{
Vivien Didelota6692752016-02-12 12:09:39 -05002587 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Colin Ian King1d9619d2016-04-25 23:11:22 +01002588 int i, err = 0;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002589
Vivien Didelot936f2342016-05-09 13:22:46 -04002590 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VLANTABLE))
2591 return -EOPNOTSUPP;
2592
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04002593 mutex_lock(&ps->reg_lock);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002594
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002595 /* Assign the bridge and remap each port's VLANTable */
Vivien Didelota6692752016-02-12 12:09:39 -05002596 ps->ports[port].bridge_dev = bridge;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002597
Vivien Didelot009a2b92016-04-17 13:24:01 -04002598 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002599 if (ps->ports[i].bridge_dev == bridge) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002600 err = _mv88e6xxx_port_based_vlan_map(ps, i);
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002601 if (err)
2602 break;
2603 }
2604 }
2605
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04002606 mutex_unlock(&ps->reg_lock);
Vivien Didelota6692752016-02-12 12:09:39 -05002607
Vivien Didelot466dfa02016-02-26 13:16:05 -05002608 return err;
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002609}
2610
Vivien Didelotf81ec902016-05-09 13:22:58 -04002611static void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002612{
Vivien Didelota6692752016-02-12 12:09:39 -05002613 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002614 struct net_device *bridge = ps->ports[port].bridge_dev;
Vivien Didelot16bfa702016-03-13 16:21:33 -04002615 int i;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002616
Vivien Didelot936f2342016-05-09 13:22:46 -04002617 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VLANTABLE))
2618 return;
2619
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04002620 mutex_lock(&ps->reg_lock);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002621
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002622 /* Unassign the bridge and remap each port's VLANTable */
Vivien Didelota6692752016-02-12 12:09:39 -05002623 ps->ports[port].bridge_dev = NULL;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002624
Vivien Didelot009a2b92016-04-17 13:24:01 -04002625 for (i = 0; i < ps->info->num_ports; ++i)
Vivien Didelot16bfa702016-03-13 16:21:33 -04002626 if (i == port || ps->ports[i].bridge_dev == bridge)
Andrew Lunn158bc062016-04-28 21:24:06 -04002627 if (_mv88e6xxx_port_based_vlan_map(ps, i))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002628 netdev_warn(ds->ports[i].netdev,
2629 "failed to remap\n");
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002630
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04002631 mutex_unlock(&ps->reg_lock);
Vivien Didelot66d9cd02016-02-05 14:07:14 -05002632}
2633
Andrew Lunn03a4a542016-06-04 21:17:05 +02002634static int _mv88e6xxx_mdio_page_write(struct mv88e6xxx_priv_state *ps,
2635 int port, int page, int reg, int val)
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002636{
2637 int ret;
2638
Andrew Lunn03a4a542016-06-04 21:17:05 +02002639 ret = mv88e6xxx_mdio_write_indirect(ps, port, 0x16, page);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002640 if (ret < 0)
2641 goto restore_page_0;
2642
Andrew Lunn03a4a542016-06-04 21:17:05 +02002643 ret = mv88e6xxx_mdio_write_indirect(ps, port, reg, val);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002644restore_page_0:
Andrew Lunn03a4a542016-06-04 21:17:05 +02002645 mv88e6xxx_mdio_write_indirect(ps, port, 0x16, 0x0);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002646
2647 return ret;
2648}
2649
Andrew Lunn03a4a542016-06-04 21:17:05 +02002650static int _mv88e6xxx_mdio_page_read(struct mv88e6xxx_priv_state *ps,
2651 int port, int page, int reg)
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002652{
2653 int ret;
2654
Andrew Lunn03a4a542016-06-04 21:17:05 +02002655 ret = mv88e6xxx_mdio_write_indirect(ps, port, 0x16, page);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002656 if (ret < 0)
2657 goto restore_page_0;
2658
Andrew Lunn03a4a542016-06-04 21:17:05 +02002659 ret = mv88e6xxx_mdio_read_indirect(ps, port, reg);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002660restore_page_0:
Andrew Lunn03a4a542016-06-04 21:17:05 +02002661 mv88e6xxx_mdio_write_indirect(ps, port, 0x16, 0x0);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002662
2663 return ret;
2664}
2665
Vivien Didelot552238b2016-05-09 13:22:49 -04002666static int mv88e6xxx_switch_reset(struct mv88e6xxx_priv_state *ps)
2667{
2668 bool ppu_active = mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU_ACTIVE);
2669 u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
Andrew Lunn52638f72016-05-10 23:27:22 +02002670 struct gpio_desc *gpiod = ps->reset;
Vivien Didelot552238b2016-05-09 13:22:49 -04002671 unsigned long timeout;
2672 int ret;
2673 int i;
2674
2675 /* Set all ports to the disabled state. */
2676 for (i = 0; i < ps->info->num_ports; i++) {
2677 ret = _mv88e6xxx_reg_read(ps, REG_PORT(i), PORT_CONTROL);
2678 if (ret < 0)
2679 return ret;
2680
2681 ret = _mv88e6xxx_reg_write(ps, REG_PORT(i), PORT_CONTROL,
2682 ret & 0xfffc);
2683 if (ret)
2684 return ret;
2685 }
2686
2687 /* Wait for transmit queues to drain. */
2688 usleep_range(2000, 4000);
2689
2690 /* If there is a gpio connected to the reset pin, toggle it */
2691 if (gpiod) {
2692 gpiod_set_value_cansleep(gpiod, 1);
2693 usleep_range(10000, 20000);
2694 gpiod_set_value_cansleep(gpiod, 0);
2695 usleep_range(10000, 20000);
2696 }
2697
2698 /* Reset the switch. Keep the PPU active if requested. The PPU
2699 * needs to be active to support indirect phy register access
2700 * through global registers 0x18 and 0x19.
2701 */
2702 if (ppu_active)
2703 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, 0x04, 0xc000);
2704 else
2705 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, 0x04, 0xc400);
2706 if (ret)
2707 return ret;
2708
2709 /* Wait up to one second for reset to complete. */
2710 timeout = jiffies + 1 * HZ;
2711 while (time_before(jiffies, timeout)) {
2712 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, 0x00);
2713 if (ret < 0)
2714 return ret;
2715
2716 if ((ret & is_reset) == is_reset)
2717 break;
2718 usleep_range(1000, 2000);
2719 }
2720 if (time_after(jiffies, timeout))
2721 ret = -ETIMEDOUT;
2722 else
2723 ret = 0;
2724
2725 return ret;
2726}
2727
Andrew Lunn158bc062016-04-28 21:24:06 -04002728static int mv88e6xxx_power_on_serdes(struct mv88e6xxx_priv_state *ps)
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002729{
2730 int ret;
2731
Andrew Lunn03a4a542016-06-04 21:17:05 +02002732 ret = _mv88e6xxx_mdio_page_read(ps, REG_FIBER_SERDES,
2733 PAGE_FIBER_SERDES, MII_BMCR);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002734 if (ret < 0)
2735 return ret;
2736
2737 if (ret & BMCR_PDOWN) {
2738 ret &= ~BMCR_PDOWN;
Andrew Lunn03a4a542016-06-04 21:17:05 +02002739 ret = _mv88e6xxx_mdio_page_write(ps, REG_FIBER_SERDES,
2740 PAGE_FIBER_SERDES, MII_BMCR,
2741 ret);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002742 }
2743
2744 return ret;
2745}
2746
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002747static int mv88e6xxx_setup_port(struct mv88e6xxx_priv_state *ps, int port)
Guenter Roeckd827e882015-03-26 18:36:29 -07002748{
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002749 struct dsa_switch *ds = ps->ds;
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002750 int ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002751 u16 reg;
Guenter Roeckd827e882015-03-26 18:36:29 -07002752
Andrew Lunn158bc062016-04-28 21:24:06 -04002753 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2754 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2755 mv88e6xxx_6185_family(ps) || mv88e6xxx_6095_family(ps) ||
2756 mv88e6xxx_6065_family(ps) || mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002757 /* MAC Forcing register: don't force link, speed,
2758 * duplex or flow control state to any particular
2759 * values on physical ports, but force the CPU port
2760 * and all DSA ports to their maximum bandwidth and
2761 * full duplex.
2762 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002763 reg = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_PCS_CTRL);
Andrew Lunn60045cb2015-08-17 23:52:51 +02002764 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
Russell King53adc9e2015-09-21 21:42:59 +01002765 reg &= ~PORT_PCS_CTRL_UNFORCED;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002766 reg |= PORT_PCS_CTRL_FORCE_LINK |
2767 PORT_PCS_CTRL_LINK_UP |
2768 PORT_PCS_CTRL_DUPLEX_FULL |
2769 PORT_PCS_CTRL_FORCE_DUPLEX;
Andrew Lunn158bc062016-04-28 21:24:06 -04002770 if (mv88e6xxx_6065_family(ps))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002771 reg |= PORT_PCS_CTRL_100;
2772 else
2773 reg |= PORT_PCS_CTRL_1000;
2774 } else {
2775 reg |= PORT_PCS_CTRL_UNFORCED;
2776 }
2777
Andrew Lunn158bc062016-04-28 21:24:06 -04002778 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002779 PORT_PCS_CTRL, reg);
2780 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002781 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002782 }
2783
2784 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
2785 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
2786 * tunneling, determine priority by looking at 802.1p and IP
2787 * priority fields (IP prio has precedence), and set STP state
2788 * to Forwarding.
2789 *
2790 * If this is the CPU link, use DSA or EDSA tagging depending
2791 * on which tagging mode was configured.
2792 *
2793 * If this is a link to another switch, use DSA tagging mode.
2794 *
2795 * If this is the upstream port for this switch, enable
2796 * forwarding of unknown unicasts and multicasts.
2797 */
2798 reg = 0;
Andrew Lunn158bc062016-04-28 21:24:06 -04002799 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2800 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2801 mv88e6xxx_6095_family(ps) || mv88e6xxx_6065_family(ps) ||
2802 mv88e6xxx_6185_family(ps) || mv88e6xxx_6320_family(ps))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002803 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
2804 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
2805 PORT_CONTROL_STATE_FORWARDING;
2806 if (dsa_is_cpu_port(ds, port)) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002807 if (mv88e6xxx_6095_family(ps) || mv88e6xxx_6185_family(ps))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002808 reg |= PORT_CONTROL_DSA_TAG;
Andrew Lunn158bc062016-04-28 21:24:06 -04002809 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2810 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2811 mv88e6xxx_6320_family(ps)) {
Andrew Lunn5377b802016-06-04 21:17:02 +02002812 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA |
2813 PORT_CONTROL_FORWARD_UNKNOWN |
Andrew Lunnc047a1f2015-09-29 01:50:56 +02002814 PORT_CONTROL_FORWARD_UNKNOWN_MC;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002815 }
2816
Andrew Lunn158bc062016-04-28 21:24:06 -04002817 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2818 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2819 mv88e6xxx_6095_family(ps) || mv88e6xxx_6065_family(ps) ||
2820 mv88e6xxx_6185_family(ps) || mv88e6xxx_6320_family(ps)) {
Vivien Didelot57d32312016-06-20 13:13:58 -04002821 reg |= PORT_CONTROL_EGRESS_ADD_TAG;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002822 }
2823 }
Andrew Lunn6083ce72015-08-17 23:52:52 +02002824 if (dsa_is_dsa_port(ds, port)) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002825 if (mv88e6xxx_6095_family(ps) || mv88e6xxx_6185_family(ps))
Andrew Lunn6083ce72015-08-17 23:52:52 +02002826 reg |= PORT_CONTROL_DSA_TAG;
Andrew Lunn158bc062016-04-28 21:24:06 -04002827 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2828 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2829 mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002830 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunn6083ce72015-08-17 23:52:52 +02002831 }
2832
Andrew Lunn54d792f2015-05-06 01:09:47 +02002833 if (port == dsa_upstream_port(ds))
2834 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2835 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2836 }
2837 if (reg) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002838 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002839 PORT_CONTROL, reg);
2840 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002841 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002842 }
2843
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002844 /* If this port is connected to a SerDes, make sure the SerDes is not
2845 * powered down.
2846 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002847 if (mv88e6xxx_6352_family(ps)) {
2848 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_STATUS);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002849 if (ret < 0)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002850 return ret;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002851 ret &= PORT_STATUS_CMODE_MASK;
2852 if ((ret == PORT_STATUS_CMODE_100BASE_X) ||
2853 (ret == PORT_STATUS_CMODE_1000BASE_X) ||
2854 (ret == PORT_STATUS_CMODE_SGMII)) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002855 ret = mv88e6xxx_power_on_serdes(ps);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002856 if (ret < 0)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002857 return ret;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002858 }
2859 }
2860
Vivien Didelot8efdda42015-08-13 12:52:23 -04002861 /* Port Control 2: don't force a good FCS, set the maximum frame size to
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002862 * 10240 bytes, disable 802.1q tags checking, don't discard tagged or
Vivien Didelot8efdda42015-08-13 12:52:23 -04002863 * untagged frames on this port, do a destination address lookup on all
2864 * received packets as usual, disable ARP mirroring and don't send a
2865 * copy of all transmitted/received frames on this port to the CPU.
Andrew Lunn54d792f2015-05-06 01:09:47 +02002866 */
2867 reg = 0;
Andrew Lunn158bc062016-04-28 21:24:06 -04002868 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2869 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2870 mv88e6xxx_6095_family(ps) || mv88e6xxx_6320_family(ps) ||
2871 mv88e6xxx_6185_family(ps))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002872 reg = PORT_CONTROL_2_MAP_DA;
2873
Andrew Lunn158bc062016-04-28 21:24:06 -04002874 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2875 mv88e6xxx_6165_family(ps) || mv88e6xxx_6320_family(ps))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002876 reg |= PORT_CONTROL_2_JUMBO_10240;
2877
Andrew Lunn158bc062016-04-28 21:24:06 -04002878 if (mv88e6xxx_6095_family(ps) || mv88e6xxx_6185_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002879 /* Set the upstream port this port should use */
2880 reg |= dsa_upstream_port(ds);
2881 /* enable forwarding of unknown multicast addresses to
2882 * the upstream port
2883 */
2884 if (port == dsa_upstream_port(ds))
2885 reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
2886 }
2887
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002888 reg |= PORT_CONTROL_2_8021Q_DISABLED;
Vivien Didelot8efdda42015-08-13 12:52:23 -04002889
Andrew Lunn54d792f2015-05-06 01:09:47 +02002890 if (reg) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002891 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002892 PORT_CONTROL_2, reg);
2893 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002894 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002895 }
2896
2897 /* Port Association Vector: when learning source addresses
2898 * of packets, add the address to the address database using
2899 * a port bitmap that has only the bit for this port set and
2900 * the other bits clear.
2901 */
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002902 reg = 1 << port;
Vivien Didelot996ecb82016-04-14 14:42:08 -04002903 /* Disable learning for CPU port */
2904 if (dsa_is_cpu_port(ds, port))
Vivien Didelot65fa4022016-04-14 14:42:07 -04002905 reg = 0;
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002906
Andrew Lunn158bc062016-04-28 21:24:06 -04002907 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_ASSOC_VECTOR, reg);
Andrew Lunn54d792f2015-05-06 01:09:47 +02002908 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002909 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002910
2911 /* Egress rate control 2: disable egress rate control. */
Andrew Lunn158bc062016-04-28 21:24:06 -04002912 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_RATE_CONTROL_2,
Andrew Lunn54d792f2015-05-06 01:09:47 +02002913 0x0000);
2914 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002915 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002916
Andrew Lunn158bc062016-04-28 21:24:06 -04002917 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2918 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2919 mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002920 /* Do not limit the period of time that this port can
2921 * be paused for by the remote end or the period of
2922 * time that this port can pause the remote end.
2923 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002924 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002925 PORT_PAUSE_CTRL, 0x0000);
2926 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002927 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002928
2929 /* Port ATU control: disable limiting the number of
2930 * address database entries that this port is allowed
2931 * to use.
2932 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002933 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002934 PORT_ATU_CONTROL, 0x0000);
2935 /* Priority Override: disable DA, SA and VTU priority
2936 * override.
2937 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002938 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002939 PORT_PRI_OVERRIDE, 0x0000);
2940 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002941 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002942
2943 /* Port Ethertype: use the Ethertype DSA Ethertype
2944 * value.
2945 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002946 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002947 PORT_ETH_TYPE, ETH_P_EDSA);
2948 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002949 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002950 /* Tag Remap: use an identity 802.1p prio -> switch
2951 * prio mapping.
2952 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002953 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002954 PORT_TAG_REGMAP_0123, 0x3210);
2955 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002956 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002957
2958 /* Tag Remap 2: use an identity 802.1p prio -> switch
2959 * prio mapping.
2960 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002961 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002962 PORT_TAG_REGMAP_4567, 0x7654);
2963 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002964 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002965 }
2966
Andrew Lunn158bc062016-04-28 21:24:06 -04002967 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2968 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2969 mv88e6xxx_6185_family(ps) || mv88e6xxx_6095_family(ps) ||
2970 mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002971 /* Rate Control: disable ingress rate limiting. */
Andrew Lunn158bc062016-04-28 21:24:06 -04002972 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002973 PORT_RATE_CONTROL, 0x0001);
2974 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002975 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002976 }
2977
Guenter Roeck366f0a02015-03-26 18:36:30 -07002978 /* Port Control 1: disable trunking, disable sending
2979 * learning messages to this port.
Guenter Roeckd827e882015-03-26 18:36:29 -07002980 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002981 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL_1, 0x0000);
Guenter Roeckd827e882015-03-26 18:36:29 -07002982 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002983 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07002984
Vivien Didelot207afda2016-04-14 14:42:09 -04002985 /* Port based VLAN map: give each port the same default address
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002986 * database, and allow bidirectional communication between the
2987 * CPU and DSA port(s), and the other ports.
Guenter Roeckd827e882015-03-26 18:36:29 -07002988 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002989 ret = _mv88e6xxx_port_fid_set(ps, port, 0);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002990 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002991 return ret;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002992
Andrew Lunn158bc062016-04-28 21:24:06 -04002993 ret = _mv88e6xxx_port_based_vlan_map(ps, port);
Guenter Roeckd827e882015-03-26 18:36:29 -07002994 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002995 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07002996
2997 /* Default VLAN ID and priority: don't set a default VLAN
2998 * ID, and set the default packet priority to zero.
2999 */
Andrew Lunn158bc062016-04-28 21:24:06 -04003000 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_DEFAULT_VLAN,
Vivien Didelot47cf1e652015-04-20 17:43:26 -04003001 0x0000);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003002 if (ret)
3003 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07003004
Andrew Lunndbde9e62015-05-06 01:09:48 +02003005 return 0;
3006}
3007
Vivien Didelot08a01262016-05-09 13:22:50 -04003008static int mv88e6xxx_setup_global(struct mv88e6xxx_priv_state *ps)
3009{
Vivien Didelotb0745e872016-05-09 13:22:53 -04003010 struct dsa_switch *ds = ps->ds;
3011 u32 upstream_port = dsa_upstream_port(ds);
Vivien Didelot119477b2016-05-09 13:22:51 -04003012 u16 reg;
Vivien Didelot08a01262016-05-09 13:22:50 -04003013 int err;
3014 int i;
3015
Vivien Didelot119477b2016-05-09 13:22:51 -04003016 /* Enable the PHY Polling Unit if present, don't discard any packets,
3017 * and mask all interrupt sources.
3018 */
3019 reg = 0;
3020 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU) ||
3021 mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU_ACTIVE))
3022 reg |= GLOBAL_CONTROL_PPU_ENABLE;
3023
3024 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_CONTROL, reg);
3025 if (err)
3026 return err;
3027
Vivien Didelotb0745e872016-05-09 13:22:53 -04003028 /* Configure the upstream port, and configure it as the port to which
3029 * ingress and egress and ARP monitor frames are to be sent.
3030 */
3031 reg = upstream_port << GLOBAL_MONITOR_CONTROL_INGRESS_SHIFT |
3032 upstream_port << GLOBAL_MONITOR_CONTROL_EGRESS_SHIFT |
3033 upstream_port << GLOBAL_MONITOR_CONTROL_ARP_SHIFT;
3034 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_MONITOR_CONTROL, reg);
3035 if (err)
3036 return err;
3037
Vivien Didelot50484ff2016-05-09 13:22:54 -04003038 /* Disable remote management, and set the switch's DSA device number. */
3039 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_CONTROL_2,
3040 GLOBAL_CONTROL_2_MULTIPLE_CASCADE |
3041 (ds->index & 0x1f));
3042 if (err)
3043 return err;
3044
Vivien Didelot08a01262016-05-09 13:22:50 -04003045 /* Set the default address aging time to 5 minutes, and
3046 * enable address learn messages to be sent to all message
3047 * ports.
3048 */
3049 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_CONTROL,
3050 0x0140 | GLOBAL_ATU_CONTROL_LEARN2ALL);
3051 if (err)
3052 return err;
3053
3054 /* Configure the IP ToS mapping registers. */
3055 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_0, 0x0000);
3056 if (err)
3057 return err;
3058 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_1, 0x0000);
3059 if (err)
3060 return err;
3061 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_2, 0x5555);
3062 if (err)
3063 return err;
3064 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_3, 0x5555);
3065 if (err)
3066 return err;
3067 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_4, 0xaaaa);
3068 if (err)
3069 return err;
3070 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_5, 0xaaaa);
3071 if (err)
3072 return err;
3073 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_6, 0xffff);
3074 if (err)
3075 return err;
3076 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_7, 0xffff);
3077 if (err)
3078 return err;
3079
3080 /* Configure the IEEE 802.1p priority mapping register. */
3081 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IEEE_PRI, 0xfa41);
3082 if (err)
3083 return err;
3084
3085 /* Send all frames with destination addresses matching
3086 * 01:80:c2:00:00:0x to the CPU port.
3087 */
3088 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_MGMT_EN_0X, 0xffff);
3089 if (err)
3090 return err;
3091
3092 /* Ignore removed tag data on doubly tagged packets, disable
3093 * flow control messages, force flow control priority to the
3094 * highest, and send all special multicast frames to the CPU
3095 * port at the highest priority.
3096 */
3097 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SWITCH_MGMT,
3098 0x7 | GLOBAL2_SWITCH_MGMT_RSVD2CPU | 0x70 |
3099 GLOBAL2_SWITCH_MGMT_FORCE_FLOW_CTRL_PRI);
3100 if (err)
3101 return err;
3102
3103 /* Program the DSA routing table. */
3104 for (i = 0; i < 32; i++) {
3105 int nexthop = 0x1f;
3106
Andrew Lunn66472fc2016-06-04 21:17:00 +02003107 if (i != ds->index && i < DSA_MAX_SWITCHES)
3108 nexthop = ds->rtable[i] & 0x1f;
Vivien Didelot08a01262016-05-09 13:22:50 -04003109
3110 err = _mv88e6xxx_reg_write(
3111 ps, REG_GLOBAL2,
3112 GLOBAL2_DEVICE_MAPPING,
3113 GLOBAL2_DEVICE_MAPPING_UPDATE |
3114 (i << GLOBAL2_DEVICE_MAPPING_TARGET_SHIFT) | nexthop);
3115 if (err)
3116 return err;
3117 }
3118
3119 /* Clear all trunk masks. */
3120 for (i = 0; i < 8; i++) {
3121 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_TRUNK_MASK,
3122 0x8000 |
3123 (i << GLOBAL2_TRUNK_MASK_NUM_SHIFT) |
3124 ((1 << ps->info->num_ports) - 1));
3125 if (err)
3126 return err;
3127 }
3128
3129 /* Clear all trunk mappings. */
3130 for (i = 0; i < 16; i++) {
3131 err = _mv88e6xxx_reg_write(
3132 ps, REG_GLOBAL2,
3133 GLOBAL2_TRUNK_MAPPING,
3134 GLOBAL2_TRUNK_MAPPING_UPDATE |
3135 (i << GLOBAL2_TRUNK_MAPPING_ID_SHIFT));
3136 if (err)
3137 return err;
3138 }
3139
3140 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
3141 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
3142 mv88e6xxx_6320_family(ps)) {
3143 /* Send all frames with destination addresses matching
3144 * 01:80:c2:00:00:2x to the CPU port.
3145 */
3146 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2,
3147 GLOBAL2_MGMT_EN_2X, 0xffff);
3148 if (err)
3149 return err;
3150
3151 /* Initialise cross-chip port VLAN table to reset
3152 * defaults.
3153 */
3154 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2,
3155 GLOBAL2_PVT_ADDR, 0x9000);
3156 if (err)
3157 return err;
3158
3159 /* Clear the priority override table. */
3160 for (i = 0; i < 16; i++) {
3161 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2,
3162 GLOBAL2_PRIO_OVERRIDE,
3163 0x8000 | (i << 8));
3164 if (err)
3165 return err;
3166 }
3167 }
3168
3169 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
3170 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
3171 mv88e6xxx_6185_family(ps) || mv88e6xxx_6095_family(ps) ||
3172 mv88e6xxx_6320_family(ps)) {
3173 /* Disable ingress rate limiting by resetting all
3174 * ingress rate limit registers to their initial
3175 * state.
3176 */
3177 for (i = 0; i < ps->info->num_ports; i++) {
3178 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2,
3179 GLOBAL2_INGRESS_OP,
3180 0x9000 | (i << 8));
3181 if (err)
3182 return err;
3183 }
3184 }
3185
3186 /* Clear the statistics counters for all ports */
3187 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_STATS_OP,
3188 GLOBAL_STATS_OP_FLUSH_ALL);
3189 if (err)
3190 return err;
3191
3192 /* Wait for the flush to complete. */
3193 err = _mv88e6xxx_stats_wait(ps);
3194 if (err)
3195 return err;
3196
3197 /* Clear all ATU entries */
3198 err = _mv88e6xxx_atu_flush(ps, 0, true);
3199 if (err)
3200 return err;
3201
3202 /* Clear all the VTU and STU entries */
3203 err = _mv88e6xxx_vtu_stu_flush(ps);
3204 if (err < 0)
3205 return err;
3206
3207 return err;
3208}
3209
Vivien Didelotf81ec902016-05-09 13:22:58 -04003210static int mv88e6xxx_setup(struct dsa_switch *ds)
Guenter Roeckacdaffc2015-03-26 18:36:28 -07003211{
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003212 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelot552238b2016-05-09 13:22:49 -04003213 int err;
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003214 int i;
3215
3216 ps->ds = ds;
Andrew Lunnb516d452016-06-04 21:17:06 +02003217 ds->slave_mii_bus = ps->mdio_bus;
Vivien Didelot552238b2016-05-09 13:22:49 -04003218
Vivien Didelotd24645b2016-05-09 13:22:41 -04003219 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEPROM))
3220 mutex_init(&ps->eeprom_mutex);
3221
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04003222 mutex_lock(&ps->reg_lock);
Vivien Didelot552238b2016-05-09 13:22:49 -04003223
3224 err = mv88e6xxx_switch_reset(ps);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003225 if (err)
3226 goto unlock;
Andrew Lunn54d792f2015-05-06 01:09:47 +02003227
Vivien Didelot08a01262016-05-09 13:22:50 -04003228 err = mv88e6xxx_setup_global(ps);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003229 if (err)
3230 goto unlock;
3231
3232 for (i = 0; i < ps->info->num_ports; i++) {
3233 err = mv88e6xxx_setup_port(ps, i);
3234 if (err)
3235 goto unlock;
3236 }
Andrew Lunn54d792f2015-05-06 01:09:47 +02003237
Vivien Didelot6b17e862015-08-13 12:52:18 -04003238unlock:
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04003239 mutex_unlock(&ps->reg_lock);
Andrew Lunndb687a52015-06-20 21:31:29 +02003240
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003241 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02003242}
3243
Vivien Didelot57d32312016-06-20 13:13:58 -04003244static int mv88e6xxx_mdio_page_read(struct dsa_switch *ds, int port, int page,
3245 int reg)
Andrew Lunn491435852015-04-02 04:06:35 +02003246{
3247 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3248 int ret;
3249
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04003250 mutex_lock(&ps->reg_lock);
Andrew Lunn03a4a542016-06-04 21:17:05 +02003251 ret = _mv88e6xxx_mdio_page_read(ps, port, page, reg);
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04003252 mutex_unlock(&ps->reg_lock);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00003253
Andrew Lunn491435852015-04-02 04:06:35 +02003254 return ret;
3255}
3256
Vivien Didelot57d32312016-06-20 13:13:58 -04003257static int mv88e6xxx_mdio_page_write(struct dsa_switch *ds, int port, int page,
3258 int reg, int val)
Andrew Lunn491435852015-04-02 04:06:35 +02003259{
3260 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3261 int ret;
3262
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04003263 mutex_lock(&ps->reg_lock);
Andrew Lunn03a4a542016-06-04 21:17:05 +02003264 ret = _mv88e6xxx_mdio_page_write(ps, port, page, reg, val);
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04003265 mutex_unlock(&ps->reg_lock);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00003266
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003267 return ret;
3268}
3269
Andrew Lunn03a4a542016-06-04 21:17:05 +02003270static int mv88e6xxx_port_to_mdio_addr(struct mv88e6xxx_priv_state *ps,
3271 int port)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003272{
Vivien Didelot009a2b92016-04-17 13:24:01 -04003273 if (port >= 0 && port < ps->info->num_ports)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003274 return port;
3275 return -EINVAL;
3276}
3277
Andrew Lunnb516d452016-06-04 21:17:06 +02003278static int mv88e6xxx_mdio_read(struct mii_bus *bus, int port, int regnum)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003279{
Andrew Lunnb516d452016-06-04 21:17:06 +02003280 struct mv88e6xxx_priv_state *ps = bus->priv;
Andrew Lunn03a4a542016-06-04 21:17:05 +02003281 int addr = mv88e6xxx_port_to_mdio_addr(ps, port);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003282 int ret;
3283
3284 if (addr < 0)
Andrew Lunn158bc062016-04-28 21:24:06 -04003285 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003286
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04003287 mutex_lock(&ps->reg_lock);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003288
3289 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU))
Andrew Lunn03a4a542016-06-04 21:17:05 +02003290 ret = mv88e6xxx_mdio_read_ppu(ps, addr, regnum);
Vivien Didelot6d5834a2016-05-09 13:22:40 -04003291 else if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_SMI_PHY))
Andrew Lunn03a4a542016-06-04 21:17:05 +02003292 ret = mv88e6xxx_mdio_read_indirect(ps, addr, regnum);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003293 else
Andrew Lunn03a4a542016-06-04 21:17:05 +02003294 ret = mv88e6xxx_mdio_read_direct(ps, addr, regnum);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003295
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04003296 mutex_unlock(&ps->reg_lock);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003297 return ret;
3298}
3299
Andrew Lunnb516d452016-06-04 21:17:06 +02003300static int mv88e6xxx_mdio_write(struct mii_bus *bus, int port, int regnum,
Andrew Lunn03a4a542016-06-04 21:17:05 +02003301 u16 val)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003302{
Andrew Lunnb516d452016-06-04 21:17:06 +02003303 struct mv88e6xxx_priv_state *ps = bus->priv;
Andrew Lunn03a4a542016-06-04 21:17:05 +02003304 int addr = mv88e6xxx_port_to_mdio_addr(ps, port);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003305 int ret;
3306
3307 if (addr < 0)
Andrew Lunn158bc062016-04-28 21:24:06 -04003308 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003309
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04003310 mutex_lock(&ps->reg_lock);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003311
3312 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU))
Andrew Lunn03a4a542016-06-04 21:17:05 +02003313 ret = mv88e6xxx_mdio_write_ppu(ps, addr, regnum, val);
Vivien Didelot6d5834a2016-05-09 13:22:40 -04003314 else if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_SMI_PHY))
Andrew Lunn03a4a542016-06-04 21:17:05 +02003315 ret = mv88e6xxx_mdio_write_indirect(ps, addr, regnum, val);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003316 else
Andrew Lunn03a4a542016-06-04 21:17:05 +02003317 ret = mv88e6xxx_mdio_write_direct(ps, addr, regnum, val);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003318
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04003319 mutex_unlock(&ps->reg_lock);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003320 return ret;
3321}
3322
Andrew Lunnb516d452016-06-04 21:17:06 +02003323static int mv88e6xxx_mdio_register(struct mv88e6xxx_priv_state *ps,
3324 struct device_node *np)
3325{
3326 static int index;
3327 struct mii_bus *bus;
3328 int err;
3329
3330 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU))
3331 mv88e6xxx_ppu_state_init(ps);
3332
3333 if (np)
3334 ps->mdio_np = of_get_child_by_name(np, "mdio");
3335
3336 bus = devm_mdiobus_alloc(ps->dev);
3337 if (!bus)
3338 return -ENOMEM;
3339
3340 bus->priv = (void *)ps;
3341 if (np) {
3342 bus->name = np->full_name;
3343 snprintf(bus->id, MII_BUS_ID_SIZE, "%s", np->full_name);
3344 } else {
3345 bus->name = "mv88e6xxx SMI";
3346 snprintf(bus->id, MII_BUS_ID_SIZE, "mv88e6xxx-%d", index++);
3347 }
3348
3349 bus->read = mv88e6xxx_mdio_read;
3350 bus->write = mv88e6xxx_mdio_write;
3351 bus->parent = ps->dev;
3352
3353 if (ps->mdio_np)
3354 err = of_mdiobus_register(bus, ps->mdio_np);
3355 else
3356 err = mdiobus_register(bus);
3357 if (err) {
3358 dev_err(ps->dev, "Cannot register MDIO bus (%d)\n", err);
3359 goto out;
3360 }
3361 ps->mdio_bus = bus;
3362
3363 return 0;
3364
3365out:
3366 if (ps->mdio_np)
3367 of_node_put(ps->mdio_np);
3368
3369 return err;
3370}
3371
3372static void mv88e6xxx_mdio_unregister(struct mv88e6xxx_priv_state *ps)
3373
3374{
3375 struct mii_bus *bus = ps->mdio_bus;
3376
3377 mdiobus_unregister(bus);
3378
3379 if (ps->mdio_np)
3380 of_node_put(ps->mdio_np);
3381}
3382
Guenter Roeckc22995c2015-07-25 09:42:28 -07003383#ifdef CONFIG_NET_DSA_HWMON
3384
3385static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
3386{
3387 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3388 int ret;
3389 int val;
3390
3391 *temp = 0;
3392
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04003393 mutex_lock(&ps->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003394
Andrew Lunn03a4a542016-06-04 21:17:05 +02003395 ret = mv88e6xxx_mdio_write_direct(ps, 0x0, 0x16, 0x6);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003396 if (ret < 0)
3397 goto error;
3398
3399 /* Enable temperature sensor */
Andrew Lunn03a4a542016-06-04 21:17:05 +02003400 ret = mv88e6xxx_mdio_read_direct(ps, 0x0, 0x1a);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003401 if (ret < 0)
3402 goto error;
3403
Andrew Lunn03a4a542016-06-04 21:17:05 +02003404 ret = mv88e6xxx_mdio_write_direct(ps, 0x0, 0x1a, ret | (1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003405 if (ret < 0)
3406 goto error;
3407
3408 /* Wait for temperature to stabilize */
3409 usleep_range(10000, 12000);
3410
Andrew Lunn03a4a542016-06-04 21:17:05 +02003411 val = mv88e6xxx_mdio_read_direct(ps, 0x0, 0x1a);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003412 if (val < 0) {
3413 ret = val;
3414 goto error;
3415 }
3416
3417 /* Disable temperature sensor */
Andrew Lunn03a4a542016-06-04 21:17:05 +02003418 ret = mv88e6xxx_mdio_write_direct(ps, 0x0, 0x1a, ret & ~(1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003419 if (ret < 0)
3420 goto error;
3421
3422 *temp = ((val & 0x1f) - 5) * 5;
3423
3424error:
Andrew Lunn03a4a542016-06-04 21:17:05 +02003425 mv88e6xxx_mdio_write_direct(ps, 0x0, 0x16, 0x0);
Vivien Didelot9f8b3ee2016-06-20 13:14:05 -04003426 mutex_unlock(&ps->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003427 return ret;
3428}
3429
3430static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
3431{
Andrew Lunn158bc062016-04-28 21:24:06 -04003432 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3433 int phy = mv88e6xxx_6320_family(ps) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003434 int ret;
3435
3436 *temp = 0;
3437
Andrew Lunn03a4a542016-06-04 21:17:05 +02003438 ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 27);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003439 if (ret < 0)
3440 return ret;
3441
3442 *temp = (ret & 0xff) - 25;
3443
3444 return 0;
3445}
3446
Vivien Didelotf81ec902016-05-09 13:22:58 -04003447static int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003448{
Andrew Lunn158bc062016-04-28 21:24:06 -04003449 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3450
Vivien Didelot6594f612016-05-09 13:22:42 -04003451 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_TEMP))
3452 return -EOPNOTSUPP;
3453
Andrew Lunn158bc062016-04-28 21:24:06 -04003454 if (mv88e6xxx_6320_family(ps) || mv88e6xxx_6352_family(ps))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003455 return mv88e63xx_get_temp(ds, temp);
3456
3457 return mv88e61xx_get_temp(ds, temp);
3458}
3459
Vivien Didelotf81ec902016-05-09 13:22:58 -04003460static int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003461{
Andrew Lunn158bc062016-04-28 21:24:06 -04003462 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3463 int phy = mv88e6xxx_6320_family(ps) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003464 int ret;
3465
Vivien Didelot6594f612016-05-09 13:22:42 -04003466 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003467 return -EOPNOTSUPP;
3468
3469 *temp = 0;
3470
Andrew Lunn03a4a542016-06-04 21:17:05 +02003471 ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 26);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003472 if (ret < 0)
3473 return ret;
3474
3475 *temp = (((ret >> 8) & 0x1f) * 5) - 25;
3476
3477 return 0;
3478}
3479
Vivien Didelotf81ec902016-05-09 13:22:58 -04003480static int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003481{
Andrew Lunn158bc062016-04-28 21:24:06 -04003482 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3483 int phy = mv88e6xxx_6320_family(ps) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003484 int ret;
3485
Vivien Didelot6594f612016-05-09 13:22:42 -04003486 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003487 return -EOPNOTSUPP;
3488
Andrew Lunn03a4a542016-06-04 21:17:05 +02003489 ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 26);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003490 if (ret < 0)
3491 return ret;
3492 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
Andrew Lunn03a4a542016-06-04 21:17:05 +02003493 return mv88e6xxx_mdio_page_write(ds, phy, 6, 26,
3494 (ret & 0xe0ff) | (temp << 8));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003495}
3496
Vivien Didelotf81ec902016-05-09 13:22:58 -04003497static int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003498{
Andrew Lunn158bc062016-04-28 21:24:06 -04003499 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3500 int phy = mv88e6xxx_6320_family(ps) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003501 int ret;
3502
Vivien Didelot6594f612016-05-09 13:22:42 -04003503 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003504 return -EOPNOTSUPP;
3505
3506 *alarm = false;
3507
Andrew Lunn03a4a542016-06-04 21:17:05 +02003508 ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 26);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003509 if (ret < 0)
3510 return ret;
3511
3512 *alarm = !!(ret & 0x40);
3513
3514 return 0;
3515}
3516#endif /* CONFIG_NET_DSA_HWMON */
3517
Vivien Didelotf81ec902016-05-09 13:22:58 -04003518static const struct mv88e6xxx_info mv88e6xxx_table[] = {
3519 [MV88E6085] = {
3520 .prod_num = PORT_SWITCH_ID_PROD_NUM_6085,
3521 .family = MV88E6XXX_FAMILY_6097,
3522 .name = "Marvell 88E6085",
3523 .num_databases = 4096,
3524 .num_ports = 10,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003525 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003526 .flags = MV88E6XXX_FLAGS_FAMILY_6097,
3527 },
3528
3529 [MV88E6095] = {
3530 .prod_num = PORT_SWITCH_ID_PROD_NUM_6095,
3531 .family = MV88E6XXX_FAMILY_6095,
3532 .name = "Marvell 88E6095/88E6095F",
3533 .num_databases = 256,
3534 .num_ports = 11,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003535 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003536 .flags = MV88E6XXX_FLAGS_FAMILY_6095,
3537 },
3538
3539 [MV88E6123] = {
3540 .prod_num = PORT_SWITCH_ID_PROD_NUM_6123,
3541 .family = MV88E6XXX_FAMILY_6165,
3542 .name = "Marvell 88E6123",
3543 .num_databases = 4096,
3544 .num_ports = 3,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003545 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003546 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3547 },
3548
3549 [MV88E6131] = {
3550 .prod_num = PORT_SWITCH_ID_PROD_NUM_6131,
3551 .family = MV88E6XXX_FAMILY_6185,
3552 .name = "Marvell 88E6131",
3553 .num_databases = 256,
3554 .num_ports = 8,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003555 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003556 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3557 },
3558
3559 [MV88E6161] = {
3560 .prod_num = PORT_SWITCH_ID_PROD_NUM_6161,
3561 .family = MV88E6XXX_FAMILY_6165,
3562 .name = "Marvell 88E6161",
3563 .num_databases = 4096,
3564 .num_ports = 6,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003565 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003566 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3567 },
3568
3569 [MV88E6165] = {
3570 .prod_num = PORT_SWITCH_ID_PROD_NUM_6165,
3571 .family = MV88E6XXX_FAMILY_6165,
3572 .name = "Marvell 88E6165",
3573 .num_databases = 4096,
3574 .num_ports = 6,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003575 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003576 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3577 },
3578
3579 [MV88E6171] = {
3580 .prod_num = PORT_SWITCH_ID_PROD_NUM_6171,
3581 .family = MV88E6XXX_FAMILY_6351,
3582 .name = "Marvell 88E6171",
3583 .num_databases = 4096,
3584 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003585 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003586 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3587 },
3588
3589 [MV88E6172] = {
3590 .prod_num = PORT_SWITCH_ID_PROD_NUM_6172,
3591 .family = MV88E6XXX_FAMILY_6352,
3592 .name = "Marvell 88E6172",
3593 .num_databases = 4096,
3594 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003595 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003596 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3597 },
3598
3599 [MV88E6175] = {
3600 .prod_num = PORT_SWITCH_ID_PROD_NUM_6175,
3601 .family = MV88E6XXX_FAMILY_6351,
3602 .name = "Marvell 88E6175",
3603 .num_databases = 4096,
3604 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003605 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003606 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3607 },
3608
3609 [MV88E6176] = {
3610 .prod_num = PORT_SWITCH_ID_PROD_NUM_6176,
3611 .family = MV88E6XXX_FAMILY_6352,
3612 .name = "Marvell 88E6176",
3613 .num_databases = 4096,
3614 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003615 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003616 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3617 },
3618
3619 [MV88E6185] = {
3620 .prod_num = PORT_SWITCH_ID_PROD_NUM_6185,
3621 .family = MV88E6XXX_FAMILY_6185,
3622 .name = "Marvell 88E6185",
3623 .num_databases = 256,
3624 .num_ports = 10,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003625 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003626 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3627 },
3628
3629 [MV88E6240] = {
3630 .prod_num = PORT_SWITCH_ID_PROD_NUM_6240,
3631 .family = MV88E6XXX_FAMILY_6352,
3632 .name = "Marvell 88E6240",
3633 .num_databases = 4096,
3634 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003635 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003636 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3637 },
3638
3639 [MV88E6320] = {
3640 .prod_num = PORT_SWITCH_ID_PROD_NUM_6320,
3641 .family = MV88E6XXX_FAMILY_6320,
3642 .name = "Marvell 88E6320",
3643 .num_databases = 4096,
3644 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003645 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003646 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3647 },
3648
3649 [MV88E6321] = {
3650 .prod_num = PORT_SWITCH_ID_PROD_NUM_6321,
3651 .family = MV88E6XXX_FAMILY_6320,
3652 .name = "Marvell 88E6321",
3653 .num_databases = 4096,
3654 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003655 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003656 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3657 },
3658
3659 [MV88E6350] = {
3660 .prod_num = PORT_SWITCH_ID_PROD_NUM_6350,
3661 .family = MV88E6XXX_FAMILY_6351,
3662 .name = "Marvell 88E6350",
3663 .num_databases = 4096,
3664 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003665 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003666 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3667 },
3668
3669 [MV88E6351] = {
3670 .prod_num = PORT_SWITCH_ID_PROD_NUM_6351,
3671 .family = MV88E6XXX_FAMILY_6351,
3672 .name = "Marvell 88E6351",
3673 .num_databases = 4096,
3674 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003675 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003676 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3677 },
3678
3679 [MV88E6352] = {
3680 .prod_num = PORT_SWITCH_ID_PROD_NUM_6352,
3681 .family = MV88E6XXX_FAMILY_6352,
3682 .name = "Marvell 88E6352",
3683 .num_databases = 4096,
3684 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003685 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003686 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3687 },
3688};
3689
Vivien Didelot5f7c0362016-06-20 13:14:04 -04003690static const struct mv88e6xxx_info *mv88e6xxx_lookup_info(unsigned int prod_num)
Vivien Didelotb9b37712015-10-30 19:39:48 -04003691{
Vivien Didelota439c062016-04-17 13:23:58 -04003692 int i;
Vivien Didelotb9b37712015-10-30 19:39:48 -04003693
Vivien Didelot5f7c0362016-06-20 13:14:04 -04003694 for (i = 0; i < ARRAY_SIZE(mv88e6xxx_table); ++i)
3695 if (mv88e6xxx_table[i].prod_num == prod_num)
3696 return &mv88e6xxx_table[i];
Vivien Didelotb9b37712015-10-30 19:39:48 -04003697
Vivien Didelotb9b37712015-10-30 19:39:48 -04003698 return NULL;
3699}
3700
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003701static int mv88e6xxx_detect(struct mv88e6xxx_priv_state *ps)
3702{
3703 const struct mv88e6xxx_info *info;
3704 int id, prod_num, rev;
3705
Vivien Didelot9dddd472016-06-20 13:14:10 -04003706 id = mv88e6xxx_reg_read(ps, ps->info->port_base_addr, PORT_SWITCH_ID);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003707 if (id < 0)
3708 return id;
3709
3710 prod_num = (id & 0xfff0) >> 4;
3711 rev = id & 0x000f;
3712
3713 info = mv88e6xxx_lookup_info(prod_num);
3714 if (!info)
3715 return -ENODEV;
3716
Vivien Didelotcaac8542016-06-20 13:14:09 -04003717 /* Update the compatible info with the probed one */
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003718 ps->info = info;
3719
3720 dev_info(ps->dev, "switch 0x%x detected: %s, revision %u\n",
3721 ps->info->prod_num, ps->info->name, rev);
3722
3723 return 0;
3724}
3725
Vivien Didelot469d7292016-06-20 13:14:06 -04003726static struct mv88e6xxx_priv_state *mv88e6xxx_alloc_chip(struct device *dev)
3727{
3728 struct mv88e6xxx_priv_state *ps;
3729
3730 ps = devm_kzalloc(dev, sizeof(*ps), GFP_KERNEL);
3731 if (!ps)
3732 return NULL;
3733
3734 ps->dev = dev;
3735
3736 mutex_init(&ps->reg_lock);
3737
3738 return ps;
3739}
3740
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003741static int mv88e6xxx_smi_init(struct mv88e6xxx_priv_state *ps,
3742 struct mii_bus *bus, int sw_addr)
3743{
3744 /* ADDR[0] pin is unavailable externally and considered zero */
3745 if (sw_addr & 0x1)
3746 return -EINVAL;
3747
Vivien Didelot914b32f2016-06-20 13:14:11 -04003748 if (sw_addr == 0)
3749 ps->smi_ops = &mv88e6xxx_smi_single_chip_ops;
3750 else if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_MULTI_CHIP))
3751 ps->smi_ops = &mv88e6xxx_smi_multi_chip_ops;
3752 else
3753 return -EINVAL;
3754
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003755 ps->bus = bus;
3756 ps->sw_addr = sw_addr;
3757
3758 return 0;
3759}
3760
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02003761static const char *mv88e6xxx_drv_probe(struct device *dsa_dev,
3762 struct device *host_dev, int sw_addr,
3763 void **priv)
Andrew Lunna77d43f2016-04-13 02:40:42 +02003764{
3765 struct mv88e6xxx_priv_state *ps;
Vivien Didelota439c062016-04-17 13:23:58 -04003766 struct mii_bus *bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003767 int err;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003768
Vivien Didelota439c062016-04-17 13:23:58 -04003769 bus = dsa_host_dev_to_mii_bus(host_dev);
Andrew Lunnc1569132016-04-13 02:40:45 +02003770 if (!bus)
3771 return NULL;
3772
Vivien Didelot469d7292016-06-20 13:14:06 -04003773 ps = mv88e6xxx_alloc_chip(dsa_dev);
3774 if (!ps)
3775 return NULL;
3776
Vivien Didelotcaac8542016-06-20 13:14:09 -04003777 /* Legacy SMI probing will only support chips similar to 88E6085 */
3778 ps->info = &mv88e6xxx_table[MV88E6085];
3779
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003780 err = mv88e6xxx_smi_init(ps, bus, sw_addr);
3781 if (err)
3782 goto free;
3783
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003784 err = mv88e6xxx_detect(ps);
3785 if (err)
Vivien Didelot469d7292016-06-20 13:14:06 -04003786 goto free;
Vivien Didelota439c062016-04-17 13:23:58 -04003787
Andrew Lunnb516d452016-06-04 21:17:06 +02003788 err = mv88e6xxx_mdio_register(ps, NULL);
3789 if (err)
Vivien Didelot469d7292016-06-20 13:14:06 -04003790 goto free;
Andrew Lunnb516d452016-06-04 21:17:06 +02003791
Vivien Didelota439c062016-04-17 13:23:58 -04003792 *priv = ps;
3793
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003794 return ps->info->name;
Vivien Didelot469d7292016-06-20 13:14:06 -04003795free:
3796 devm_kfree(dsa_dev, ps);
3797
3798 return NULL;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003799}
3800
Vivien Didelot57d32312016-06-20 13:13:58 -04003801static struct dsa_switch_driver mv88e6xxx_switch_driver = {
Vivien Didelotf81ec902016-05-09 13:22:58 -04003802 .tag_protocol = DSA_TAG_PROTO_EDSA,
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02003803 .probe = mv88e6xxx_drv_probe,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003804 .setup = mv88e6xxx_setup,
3805 .set_addr = mv88e6xxx_set_addr,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003806 .adjust_link = mv88e6xxx_adjust_link,
3807 .get_strings = mv88e6xxx_get_strings,
3808 .get_ethtool_stats = mv88e6xxx_get_ethtool_stats,
3809 .get_sset_count = mv88e6xxx_get_sset_count,
3810 .set_eee = mv88e6xxx_set_eee,
3811 .get_eee = mv88e6xxx_get_eee,
3812#ifdef CONFIG_NET_DSA_HWMON
3813 .get_temp = mv88e6xxx_get_temp,
3814 .get_temp_limit = mv88e6xxx_get_temp_limit,
3815 .set_temp_limit = mv88e6xxx_set_temp_limit,
3816 .get_temp_alarm = mv88e6xxx_get_temp_alarm,
3817#endif
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003818 .get_eeprom_len = mv88e6xxx_get_eeprom_len,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003819 .get_eeprom = mv88e6xxx_get_eeprom,
3820 .set_eeprom = mv88e6xxx_set_eeprom,
3821 .get_regs_len = mv88e6xxx_get_regs_len,
3822 .get_regs = mv88e6xxx_get_regs,
3823 .port_bridge_join = mv88e6xxx_port_bridge_join,
3824 .port_bridge_leave = mv88e6xxx_port_bridge_leave,
3825 .port_stp_state_set = mv88e6xxx_port_stp_state_set,
3826 .port_vlan_filtering = mv88e6xxx_port_vlan_filtering,
3827 .port_vlan_prepare = mv88e6xxx_port_vlan_prepare,
3828 .port_vlan_add = mv88e6xxx_port_vlan_add,
3829 .port_vlan_del = mv88e6xxx_port_vlan_del,
3830 .port_vlan_dump = mv88e6xxx_port_vlan_dump,
3831 .port_fdb_prepare = mv88e6xxx_port_fdb_prepare,
3832 .port_fdb_add = mv88e6xxx_port_fdb_add,
3833 .port_fdb_del = mv88e6xxx_port_fdb_del,
3834 .port_fdb_dump = mv88e6xxx_port_fdb_dump,
3835};
3836
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003837static int mv88e6xxx_register_switch(struct mv88e6xxx_priv_state *ps,
3838 struct device_node *np)
3839{
3840 struct device *dev = ps->dev;
3841 struct dsa_switch *ds;
3842
3843 ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
3844 if (!ds)
3845 return -ENOMEM;
3846
3847 ds->dev = dev;
3848 ds->priv = ps;
3849 ds->drv = &mv88e6xxx_switch_driver;
3850
3851 dev_set_drvdata(dev, ds);
3852
3853 return dsa_register_switch(ds, np);
3854}
3855
3856static void mv88e6xxx_unregister_switch(struct mv88e6xxx_priv_state *ps)
3857{
3858 dsa_unregister_switch(ps->ds);
3859}
3860
Vivien Didelot57d32312016-06-20 13:13:58 -04003861static int mv88e6xxx_probe(struct mdio_device *mdiodev)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003862{
3863 struct device *dev = &mdiodev->dev;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003864 struct device_node *np = dev->of_node;
Vivien Didelotcaac8542016-06-20 13:14:09 -04003865 const struct mv88e6xxx_info *compat_info;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003866 struct mv88e6xxx_priv_state *ps;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003867 u32 eeprom_len;
Andrew Lunn52638f72016-05-10 23:27:22 +02003868 int err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003869
Vivien Didelotcaac8542016-06-20 13:14:09 -04003870 compat_info = of_device_get_match_data(dev);
3871 if (!compat_info)
3872 return -EINVAL;
3873
Vivien Didelot469d7292016-06-20 13:14:06 -04003874 ps = mv88e6xxx_alloc_chip(dev);
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003875 if (!ps)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003876 return -ENOMEM;
3877
Vivien Didelotcaac8542016-06-20 13:14:09 -04003878 ps->info = compat_info;
3879
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003880 err = mv88e6xxx_smi_init(ps, mdiodev->bus, mdiodev->addr);
3881 if (err)
3882 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003883
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003884 err = mv88e6xxx_detect(ps);
3885 if (err)
3886 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003887
Vivien Didelotc6d19ab2016-06-20 13:14:03 -04003888 ps->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_ASIS);
3889 if (IS_ERR(ps->reset))
3890 return PTR_ERR(ps->reset);
Andrew Lunn52638f72016-05-10 23:27:22 +02003891
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003892 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEPROM) &&
3893 !of_property_read_u32(np, "eeprom-length", &eeprom_len))
3894 ps->eeprom_len = eeprom_len;
3895
Vivien Didelotaa8ac392016-06-20 13:14:00 -04003896 err = mv88e6xxx_mdio_register(ps, np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003897 if (err)
3898 return err;
3899
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003900 err = mv88e6xxx_register_switch(ps, np);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02003901 if (err) {
3902 mv88e6xxx_mdio_unregister(ps);
3903 return err;
3904 }
3905
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003906 return 0;
3907}
3908
3909static void mv88e6xxx_remove(struct mdio_device *mdiodev)
3910{
3911 struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev);
3912 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3913
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003914 mv88e6xxx_unregister_switch(ps);
Andrew Lunnb516d452016-06-04 21:17:06 +02003915 mv88e6xxx_mdio_unregister(ps);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003916}
3917
3918static const struct of_device_id mv88e6xxx_of_match[] = {
Vivien Didelotcaac8542016-06-20 13:14:09 -04003919 {
3920 .compatible = "marvell,mv88e6085",
3921 .data = &mv88e6xxx_table[MV88E6085],
3922 },
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003923 { /* sentinel */ },
3924};
3925
3926MODULE_DEVICE_TABLE(of, mv88e6xxx_of_match);
3927
3928static struct mdio_driver mv88e6xxx_driver = {
3929 .probe = mv88e6xxx_probe,
3930 .remove = mv88e6xxx_remove,
3931 .mdiodrv.driver = {
3932 .name = "mv88e6085",
3933 .of_match_table = mv88e6xxx_of_match,
3934 },
3935};
3936
Ben Hutchings98e67302011-11-25 14:36:19 +00003937static int __init mv88e6xxx_init(void)
3938{
Vivien Didelotf81ec902016-05-09 13:22:58 -04003939 register_switch_driver(&mv88e6xxx_switch_driver);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003940 return mdio_driver_register(&mv88e6xxx_driver);
Ben Hutchings98e67302011-11-25 14:36:19 +00003941}
3942module_init(mv88e6xxx_init);
3943
3944static void __exit mv88e6xxx_cleanup(void)
3945{
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003946 mdio_driver_unregister(&mv88e6xxx_driver);
Vivien Didelotf81ec902016-05-09 13:22:58 -04003947 unregister_switch_driver(&mv88e6xxx_switch_driver);
Ben Hutchings98e67302011-11-25 14:36:19 +00003948}
3949module_exit(mv88e6xxx_cleanup);
Ben Hutchings3d825ed2011-11-25 14:37:16 +00003950
3951MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
3952MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
3953MODULE_LICENSE("GPL");