blob: ce2f7ff8066e0c2b413e8d3518307811378d9700 [file] [log] [blame]
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001/*
Vivien Didelot0d3cd4b2016-06-21 12:28:19 -04002 * Marvell 88e6xxx Ethernet switch single-chip support
3 *
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00004 * Copyright (c) 2008 Marvell Semiconductor
5 *
Vivien Didelotb8fee952015-08-13 12:52:19 -04006 * Copyright (c) 2015 CMC Electronics, Inc.
7 * Added support for VLAN Table Unit operations
8 *
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02009 * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch>
10 *
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 */
16
Barry Grussling19b2f972013-01-08 16:05:54 +000017#include <linux/delay.h>
Guenter Roeckdefb05b2015-03-26 18:36:38 -070018#include <linux/etherdevice.h>
Andrew Lunndea87022015-08-31 15:56:47 +020019#include <linux/ethtool.h>
Guenter Roeckfacd95b2015-03-26 18:36:35 -070020#include <linux/if_bridge.h>
Andrew Lunndc30c352016-10-16 19:56:49 +020021#include <linux/interrupt.h>
22#include <linux/irq.h>
23#include <linux/irqdomain.h>
Barry Grussling19b2f972013-01-08 16:05:54 +000024#include <linux/jiffies.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000025#include <linux/list.h>
Andrew Lunn14c7b3c2016-05-10 23:27:21 +020026#include <linux/mdio.h>
Paul Gortmaker2bbba272012-01-24 10:41:40 +000027#include <linux/module.h>
Vivien Didelotcaac8542016-06-20 13:14:09 -040028#include <linux/of_device.h>
Andrew Lunndc30c352016-10-16 19:56:49 +020029#include <linux/of_irq.h>
Andrew Lunnb516d452016-06-04 21:17:06 +020030#include <linux/of_mdio.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000031#include <linux/netdevice.h>
Andrew Lunnc8c1b39a2015-11-20 03:56:24 +010032#include <linux/gpio/consumer.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000033#include <linux/phy.h>
Ben Hutchingsc8f0b862011-11-27 17:06:08 +000034#include <net/dsa.h>
Vivien Didelot1f36faf2015-10-08 11:35:13 -040035#include <net/switchdev.h>
Vivien Didelotec561272016-09-02 14:45:33 -040036
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000037#include "mv88e6xxx.h"
Vivien Didelota935c052016-09-29 12:21:53 -040038#include "global1.h"
Vivien Didelotec561272016-09-02 14:45:33 -040039#include "global2.h"
Vivien Didelot18abed22016-11-04 03:23:26 +010040#include "port.h"
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000041
Vivien Didelotfad09c72016-06-21 12:28:20 -040042static void assert_reg_lock(struct mv88e6xxx_chip *chip)
Vivien Didelot3996a4f2015-10-30 18:56:45 -040043{
Vivien Didelotfad09c72016-06-21 12:28:20 -040044 if (unlikely(!mutex_is_locked(&chip->reg_lock))) {
45 dev_err(chip->dev, "Switch registers lock not held!\n");
Vivien Didelot3996a4f2015-10-30 18:56:45 -040046 dump_stack();
47 }
48}
49
Vivien Didelot914b32f2016-06-20 13:14:11 -040050/* The switch ADDR[4:1] configuration pins define the chip SMI device address
51 * (ADDR[0] is always zero, thus only even SMI addresses can be strapped).
52 *
53 * When ADDR is all zero, the chip uses Single-chip Addressing Mode, assuming it
54 * is the only device connected to the SMI master. In this mode it responds to
55 * all 32 possible SMI addresses, and thus maps directly the internal devices.
56 *
57 * When ADDR is non-zero, the chip uses Multi-chip Addressing Mode, allowing
58 * multiple devices to share the SMI interface. In this mode it responds to only
59 * 2 registers, used to indirectly access the internal SMI devices.
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000060 */
Vivien Didelot914b32f2016-06-20 13:14:11 -040061
Vivien Didelotfad09c72016-06-21 12:28:20 -040062static int mv88e6xxx_smi_read(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -040063 int addr, int reg, u16 *val)
64{
Vivien Didelotfad09c72016-06-21 12:28:20 -040065 if (!chip->smi_ops)
Vivien Didelot914b32f2016-06-20 13:14:11 -040066 return -EOPNOTSUPP;
67
Vivien Didelotfad09c72016-06-21 12:28:20 -040068 return chip->smi_ops->read(chip, addr, reg, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -040069}
70
Vivien Didelotfad09c72016-06-21 12:28:20 -040071static int mv88e6xxx_smi_write(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -040072 int addr, int reg, u16 val)
73{
Vivien Didelotfad09c72016-06-21 12:28:20 -040074 if (!chip->smi_ops)
Vivien Didelot914b32f2016-06-20 13:14:11 -040075 return -EOPNOTSUPP;
76
Vivien Didelotfad09c72016-06-21 12:28:20 -040077 return chip->smi_ops->write(chip, addr, reg, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -040078}
79
Vivien Didelotfad09c72016-06-21 12:28:20 -040080static int mv88e6xxx_smi_single_chip_read(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -040081 int addr, int reg, u16 *val)
82{
83 int ret;
84
Vivien Didelotfad09c72016-06-21 12:28:20 -040085 ret = mdiobus_read_nested(chip->bus, addr, reg);
Vivien Didelot914b32f2016-06-20 13:14:11 -040086 if (ret < 0)
87 return ret;
88
89 *val = ret & 0xffff;
90
91 return 0;
92}
93
Vivien Didelotfad09c72016-06-21 12:28:20 -040094static int mv88e6xxx_smi_single_chip_write(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -040095 int addr, int reg, u16 val)
96{
97 int ret;
98
Vivien Didelotfad09c72016-06-21 12:28:20 -040099 ret = mdiobus_write_nested(chip->bus, addr, reg, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400100 if (ret < 0)
101 return ret;
102
103 return 0;
104}
105
Vivien Didelotc08026a2016-09-29 12:21:59 -0400106static const struct mv88e6xxx_bus_ops mv88e6xxx_smi_single_chip_ops = {
Vivien Didelot914b32f2016-06-20 13:14:11 -0400107 .read = mv88e6xxx_smi_single_chip_read,
108 .write = mv88e6xxx_smi_single_chip_write,
109};
110
Vivien Didelotfad09c72016-06-21 12:28:20 -0400111static int mv88e6xxx_smi_multi_chip_wait(struct mv88e6xxx_chip *chip)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000112{
113 int ret;
114 int i;
115
116 for (i = 0; i < 16; i++) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400117 ret = mdiobus_read_nested(chip->bus, chip->sw_addr, SMI_CMD);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000118 if (ret < 0)
119 return ret;
120
Andrew Lunncca8b132015-04-02 04:06:39 +0200121 if ((ret & SMI_CMD_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000122 return 0;
123 }
124
125 return -ETIMEDOUT;
126}
127
Vivien Didelotfad09c72016-06-21 12:28:20 -0400128static int mv88e6xxx_smi_multi_chip_read(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -0400129 int addr, int reg, u16 *val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000130{
131 int ret;
132
Barry Grussling3675c8d2013-01-08 16:05:53 +0000133 /* Wait for the bus to become free. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400134 ret = mv88e6xxx_smi_multi_chip_wait(chip);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000135 if (ret < 0)
136 return ret;
137
Barry Grussling3675c8d2013-01-08 16:05:53 +0000138 /* Transmit the read command. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400139 ret = mdiobus_write_nested(chip->bus, chip->sw_addr, SMI_CMD,
Neil Armstrong6e899e62015-10-22 10:37:53 +0200140 SMI_CMD_OP_22_READ | (addr << 5) | reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000141 if (ret < 0)
142 return ret;
143
Barry Grussling3675c8d2013-01-08 16:05:53 +0000144 /* Wait for the read command to complete. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400145 ret = mv88e6xxx_smi_multi_chip_wait(chip);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000146 if (ret < 0)
147 return ret;
148
Barry Grussling3675c8d2013-01-08 16:05:53 +0000149 /* Read the data. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400150 ret = mdiobus_read_nested(chip->bus, chip->sw_addr, SMI_DATA);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000151 if (ret < 0)
152 return ret;
153
Vivien Didelot914b32f2016-06-20 13:14:11 -0400154 *val = ret & 0xffff;
155
156 return 0;
157}
158
Vivien Didelotfad09c72016-06-21 12:28:20 -0400159static int mv88e6xxx_smi_multi_chip_write(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -0400160 int addr, int reg, u16 val)
161{
162 int ret;
163
164 /* Wait for the bus to become free. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400165 ret = mv88e6xxx_smi_multi_chip_wait(chip);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400166 if (ret < 0)
167 return ret;
168
169 /* Transmit the data to write. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400170 ret = mdiobus_write_nested(chip->bus, chip->sw_addr, SMI_DATA, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400171 if (ret < 0)
172 return ret;
173
174 /* Transmit the write command. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400175 ret = mdiobus_write_nested(chip->bus, chip->sw_addr, SMI_CMD,
Vivien Didelot914b32f2016-06-20 13:14:11 -0400176 SMI_CMD_OP_22_WRITE | (addr << 5) | reg);
177 if (ret < 0)
178 return ret;
179
180 /* Wait for the write command to complete. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400181 ret = mv88e6xxx_smi_multi_chip_wait(chip);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400182 if (ret < 0)
183 return ret;
184
185 return 0;
186}
187
Vivien Didelotc08026a2016-09-29 12:21:59 -0400188static const struct mv88e6xxx_bus_ops mv88e6xxx_smi_multi_chip_ops = {
Vivien Didelot914b32f2016-06-20 13:14:11 -0400189 .read = mv88e6xxx_smi_multi_chip_read,
190 .write = mv88e6xxx_smi_multi_chip_write,
191};
192
Vivien Didelotec561272016-09-02 14:45:33 -0400193int mv88e6xxx_read(struct mv88e6xxx_chip *chip, int addr, int reg, u16 *val)
Vivien Didelot914b32f2016-06-20 13:14:11 -0400194{
195 int err;
196
Vivien Didelotfad09c72016-06-21 12:28:20 -0400197 assert_reg_lock(chip);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400198
Vivien Didelotfad09c72016-06-21 12:28:20 -0400199 err = mv88e6xxx_smi_read(chip, addr, reg, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400200 if (err)
201 return err;
202
Vivien Didelotfad09c72016-06-21 12:28:20 -0400203 dev_dbg(chip->dev, "<- addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
Vivien Didelot914b32f2016-06-20 13:14:11 -0400204 addr, reg, *val);
205
206 return 0;
207}
208
Vivien Didelotec561272016-09-02 14:45:33 -0400209int mv88e6xxx_write(struct mv88e6xxx_chip *chip, int addr, int reg, u16 val)
Vivien Didelot914b32f2016-06-20 13:14:11 -0400210{
211 int err;
212
Vivien Didelotfad09c72016-06-21 12:28:20 -0400213 assert_reg_lock(chip);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400214
Vivien Didelotfad09c72016-06-21 12:28:20 -0400215 err = mv88e6xxx_smi_write(chip, addr, reg, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400216 if (err)
217 return err;
218
Vivien Didelotfad09c72016-06-21 12:28:20 -0400219 dev_dbg(chip->dev, "-> addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
Vivien Didelot914b32f2016-06-20 13:14:11 -0400220 addr, reg, val);
221
222 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000223}
224
Vivien Didelote57e5e72016-08-15 17:19:00 -0400225static int mv88e6xxx_phy_read(struct mv88e6xxx_chip *chip, int phy,
226 int reg, u16 *val)
227{
228 int addr = phy; /* PHY devices addresses start at 0x0 */
229
Vivien Didelotb3469dd2016-09-29 12:22:00 -0400230 if (!chip->info->ops->phy_read)
Vivien Didelote57e5e72016-08-15 17:19:00 -0400231 return -EOPNOTSUPP;
232
Vivien Didelotb3469dd2016-09-29 12:22:00 -0400233 return chip->info->ops->phy_read(chip, addr, reg, val);
Vivien Didelote57e5e72016-08-15 17:19:00 -0400234}
235
236static int mv88e6xxx_phy_write(struct mv88e6xxx_chip *chip, int phy,
237 int reg, u16 val)
238{
239 int addr = phy; /* PHY devices addresses start at 0x0 */
240
Vivien Didelotb3469dd2016-09-29 12:22:00 -0400241 if (!chip->info->ops->phy_write)
Vivien Didelote57e5e72016-08-15 17:19:00 -0400242 return -EOPNOTSUPP;
243
Vivien Didelotb3469dd2016-09-29 12:22:00 -0400244 return chip->info->ops->phy_write(chip, addr, reg, val);
Vivien Didelote57e5e72016-08-15 17:19:00 -0400245}
246
Vivien Didelot09cb7df2016-08-15 17:19:01 -0400247static int mv88e6xxx_phy_page_get(struct mv88e6xxx_chip *chip, int phy, u8 page)
248{
249 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_PHY_PAGE))
250 return -EOPNOTSUPP;
251
252 return mv88e6xxx_phy_write(chip, phy, PHY_PAGE, page);
253}
254
255static void mv88e6xxx_phy_page_put(struct mv88e6xxx_chip *chip, int phy)
256{
257 int err;
258
259 /* Restore PHY page Copper 0x0 for access via the registered MDIO bus */
260 err = mv88e6xxx_phy_write(chip, phy, PHY_PAGE, PHY_PAGE_COPPER);
261 if (unlikely(err)) {
262 dev_err(chip->dev, "failed to restore PHY %d page Copper (%d)\n",
263 phy, err);
264 }
265}
266
267static int mv88e6xxx_phy_page_read(struct mv88e6xxx_chip *chip, int phy,
268 u8 page, int reg, u16 *val)
269{
270 int err;
271
272 /* There is no paging for registers 22 */
273 if (reg == PHY_PAGE)
274 return -EINVAL;
275
276 err = mv88e6xxx_phy_page_get(chip, phy, page);
277 if (!err) {
278 err = mv88e6xxx_phy_read(chip, phy, reg, val);
279 mv88e6xxx_phy_page_put(chip, phy);
280 }
281
282 return err;
283}
284
285static int mv88e6xxx_phy_page_write(struct mv88e6xxx_chip *chip, int phy,
286 u8 page, int reg, u16 val)
287{
288 int err;
289
290 /* There is no paging for registers 22 */
291 if (reg == PHY_PAGE)
292 return -EINVAL;
293
294 err = mv88e6xxx_phy_page_get(chip, phy, page);
295 if (!err) {
296 err = mv88e6xxx_phy_write(chip, phy, PHY_PAGE, page);
297 mv88e6xxx_phy_page_put(chip, phy);
298 }
299
300 return err;
301}
302
303static int mv88e6xxx_serdes_read(struct mv88e6xxx_chip *chip, int reg, u16 *val)
304{
305 return mv88e6xxx_phy_page_read(chip, ADDR_SERDES, SERDES_PAGE_FIBER,
306 reg, val);
307}
308
309static int mv88e6xxx_serdes_write(struct mv88e6xxx_chip *chip, int reg, u16 val)
310{
311 return mv88e6xxx_phy_page_write(chip, ADDR_SERDES, SERDES_PAGE_FIBER,
312 reg, val);
313}
314
Andrew Lunndc30c352016-10-16 19:56:49 +0200315static void mv88e6xxx_g1_irq_mask(struct irq_data *d)
316{
317 struct mv88e6xxx_chip *chip = irq_data_get_irq_chip_data(d);
318 unsigned int n = d->hwirq;
319
320 chip->g1_irq.masked |= (1 << n);
321}
322
323static void mv88e6xxx_g1_irq_unmask(struct irq_data *d)
324{
325 struct mv88e6xxx_chip *chip = irq_data_get_irq_chip_data(d);
326 unsigned int n = d->hwirq;
327
328 chip->g1_irq.masked &= ~(1 << n);
329}
330
331static irqreturn_t mv88e6xxx_g1_irq_thread_fn(int irq, void *dev_id)
332{
333 struct mv88e6xxx_chip *chip = dev_id;
334 unsigned int nhandled = 0;
335 unsigned int sub_irq;
336 unsigned int n;
337 u16 reg;
338 int err;
339
340 mutex_lock(&chip->reg_lock);
341 err = mv88e6xxx_g1_read(chip, GLOBAL_STATUS, &reg);
342 mutex_unlock(&chip->reg_lock);
343
344 if (err)
345 goto out;
346
347 for (n = 0; n < chip->g1_irq.nirqs; ++n) {
348 if (reg & (1 << n)) {
349 sub_irq = irq_find_mapping(chip->g1_irq.domain, n);
350 handle_nested_irq(sub_irq);
351 ++nhandled;
352 }
353 }
354out:
355 return (nhandled > 0 ? IRQ_HANDLED : IRQ_NONE);
356}
357
358static void mv88e6xxx_g1_irq_bus_lock(struct irq_data *d)
359{
360 struct mv88e6xxx_chip *chip = irq_data_get_irq_chip_data(d);
361
362 mutex_lock(&chip->reg_lock);
363}
364
365static void mv88e6xxx_g1_irq_bus_sync_unlock(struct irq_data *d)
366{
367 struct mv88e6xxx_chip *chip = irq_data_get_irq_chip_data(d);
368 u16 mask = GENMASK(chip->g1_irq.nirqs, 0);
369 u16 reg;
370 int err;
371
372 err = mv88e6xxx_g1_read(chip, GLOBAL_CONTROL, &reg);
373 if (err)
374 goto out;
375
376 reg &= ~mask;
377 reg |= (~chip->g1_irq.masked & mask);
378
379 err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL, reg);
380 if (err)
381 goto out;
382
383out:
384 mutex_unlock(&chip->reg_lock);
385}
386
387static struct irq_chip mv88e6xxx_g1_irq_chip = {
388 .name = "mv88e6xxx-g1",
389 .irq_mask = mv88e6xxx_g1_irq_mask,
390 .irq_unmask = mv88e6xxx_g1_irq_unmask,
391 .irq_bus_lock = mv88e6xxx_g1_irq_bus_lock,
392 .irq_bus_sync_unlock = mv88e6xxx_g1_irq_bus_sync_unlock,
393};
394
395static int mv88e6xxx_g1_irq_domain_map(struct irq_domain *d,
396 unsigned int irq,
397 irq_hw_number_t hwirq)
398{
399 struct mv88e6xxx_chip *chip = d->host_data;
400
401 irq_set_chip_data(irq, d->host_data);
402 irq_set_chip_and_handler(irq, &chip->g1_irq.chip, handle_level_irq);
403 irq_set_noprobe(irq);
404
405 return 0;
406}
407
408static const struct irq_domain_ops mv88e6xxx_g1_irq_domain_ops = {
409 .map = mv88e6xxx_g1_irq_domain_map,
410 .xlate = irq_domain_xlate_twocell,
411};
412
413static void mv88e6xxx_g1_irq_free(struct mv88e6xxx_chip *chip)
414{
415 int irq, virq;
Andrew Lunn3460a572016-11-20 20:14:16 +0100416 u16 mask;
417
418 mv88e6xxx_g1_read(chip, GLOBAL_CONTROL, &mask);
419 mask |= GENMASK(chip->g1_irq.nirqs, 0);
420 mv88e6xxx_g1_write(chip, GLOBAL_CONTROL, mask);
421
422 free_irq(chip->irq, chip);
Andrew Lunndc30c352016-10-16 19:56:49 +0200423
Andreas Färber5edef2f2016-11-27 23:26:28 +0100424 for (irq = 0; irq < chip->g1_irq.nirqs; irq++) {
Andrew Lunna3db3d32016-11-20 20:14:14 +0100425 virq = irq_find_mapping(chip->g1_irq.domain, irq);
Andrew Lunndc30c352016-10-16 19:56:49 +0200426 irq_dispose_mapping(virq);
427 }
428
Andrew Lunna3db3d32016-11-20 20:14:14 +0100429 irq_domain_remove(chip->g1_irq.domain);
Andrew Lunndc30c352016-10-16 19:56:49 +0200430}
431
432static int mv88e6xxx_g1_irq_setup(struct mv88e6xxx_chip *chip)
433{
Andrew Lunn3dd0ef02016-11-20 20:14:17 +0100434 int err, irq, virq;
435 u16 reg, mask;
Andrew Lunndc30c352016-10-16 19:56:49 +0200436
437 chip->g1_irq.nirqs = chip->info->g1_irqs;
438 chip->g1_irq.domain = irq_domain_add_simple(
439 NULL, chip->g1_irq.nirqs, 0,
440 &mv88e6xxx_g1_irq_domain_ops, chip);
441 if (!chip->g1_irq.domain)
442 return -ENOMEM;
443
444 for (irq = 0; irq < chip->g1_irq.nirqs; irq++)
445 irq_create_mapping(chip->g1_irq.domain, irq);
446
447 chip->g1_irq.chip = mv88e6xxx_g1_irq_chip;
448 chip->g1_irq.masked = ~0;
449
Andrew Lunn3dd0ef02016-11-20 20:14:17 +0100450 err = mv88e6xxx_g1_read(chip, GLOBAL_CONTROL, &mask);
Andrew Lunndc30c352016-10-16 19:56:49 +0200451 if (err)
Andrew Lunn3dd0ef02016-11-20 20:14:17 +0100452 goto out_mapping;
Andrew Lunndc30c352016-10-16 19:56:49 +0200453
Andrew Lunn3dd0ef02016-11-20 20:14:17 +0100454 mask &= ~GENMASK(chip->g1_irq.nirqs, 0);
Andrew Lunndc30c352016-10-16 19:56:49 +0200455
Andrew Lunn3dd0ef02016-11-20 20:14:17 +0100456 err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL, mask);
Andrew Lunndc30c352016-10-16 19:56:49 +0200457 if (err)
Andrew Lunn3dd0ef02016-11-20 20:14:17 +0100458 goto out_disable;
Andrew Lunndc30c352016-10-16 19:56:49 +0200459
460 /* Reading the interrupt status clears (most of) them */
461 err = mv88e6xxx_g1_read(chip, GLOBAL_STATUS, &reg);
462 if (err)
Andrew Lunn3dd0ef02016-11-20 20:14:17 +0100463 goto out_disable;
Andrew Lunndc30c352016-10-16 19:56:49 +0200464
465 err = request_threaded_irq(chip->irq, NULL,
466 mv88e6xxx_g1_irq_thread_fn,
467 IRQF_ONESHOT | IRQF_TRIGGER_FALLING,
468 dev_name(chip->dev), chip);
469 if (err)
Andrew Lunn3dd0ef02016-11-20 20:14:17 +0100470 goto out_disable;
Andrew Lunndc30c352016-10-16 19:56:49 +0200471
472 return 0;
473
Andrew Lunn3dd0ef02016-11-20 20:14:17 +0100474out_disable:
475 mask |= GENMASK(chip->g1_irq.nirqs, 0);
476 mv88e6xxx_g1_write(chip, GLOBAL_CONTROL, mask);
477
478out_mapping:
479 for (irq = 0; irq < 16; irq++) {
480 virq = irq_find_mapping(chip->g1_irq.domain, irq);
481 irq_dispose_mapping(virq);
482 }
483
484 irq_domain_remove(chip->g1_irq.domain);
Andrew Lunndc30c352016-10-16 19:56:49 +0200485
486 return err;
487}
488
Vivien Didelotec561272016-09-02 14:45:33 -0400489int mv88e6xxx_wait(struct mv88e6xxx_chip *chip, int addr, int reg, u16 mask)
Vivien Didelot2d79af62016-08-15 17:18:57 -0400490{
Andrew Lunn6441e6692016-08-19 00:01:55 +0200491 int i;
Vivien Didelot2d79af62016-08-15 17:18:57 -0400492
Andrew Lunn6441e6692016-08-19 00:01:55 +0200493 for (i = 0; i < 16; i++) {
Vivien Didelot2d79af62016-08-15 17:18:57 -0400494 u16 val;
495 int err;
496
497 err = mv88e6xxx_read(chip, addr, reg, &val);
498 if (err)
499 return err;
500
501 if (!(val & mask))
502 return 0;
503
504 usleep_range(1000, 2000);
505 }
506
Andrew Lunn30853552016-08-19 00:01:57 +0200507 dev_err(chip->dev, "Timeout while waiting for switch\n");
Vivien Didelot2d79af62016-08-15 17:18:57 -0400508 return -ETIMEDOUT;
509}
510
Vivien Didelotf22ab642016-07-18 20:45:31 -0400511/* Indirect write to single pointer-data register with an Update bit */
Vivien Didelotec561272016-09-02 14:45:33 -0400512int mv88e6xxx_update(struct mv88e6xxx_chip *chip, int addr, int reg, u16 update)
Vivien Didelotf22ab642016-07-18 20:45:31 -0400513{
514 u16 val;
Andrew Lunn0f02b4f2016-08-19 00:01:56 +0200515 int err;
Vivien Didelotf22ab642016-07-18 20:45:31 -0400516
517 /* Wait until the previous operation is completed */
Andrew Lunn0f02b4f2016-08-19 00:01:56 +0200518 err = mv88e6xxx_wait(chip, addr, reg, BIT(15));
519 if (err)
520 return err;
Vivien Didelotf22ab642016-07-18 20:45:31 -0400521
522 /* Set the Update bit to trigger a write operation */
523 val = BIT(15) | update;
524
525 return mv88e6xxx_write(chip, addr, reg, val);
526}
527
Vivien Didelota935c052016-09-29 12:21:53 -0400528static int mv88e6xxx_ppu_disable(struct mv88e6xxx_chip *chip)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000529{
Vivien Didelot914b32f2016-06-20 13:14:11 -0400530 u16 val;
Vivien Didelota935c052016-09-29 12:21:53 -0400531 int i, err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000532
Vivien Didelota935c052016-09-29 12:21:53 -0400533 err = mv88e6xxx_g1_read(chip, GLOBAL_CONTROL, &val);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400534 if (err)
535 return err;
Vivien Didelot3996a4f2015-10-30 18:56:45 -0400536
Vivien Didelota935c052016-09-29 12:21:53 -0400537 err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL,
538 val & ~GLOBAL_CONTROL_PPU_ENABLE);
539 if (err)
540 return err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000541
Andrew Lunn6441e6692016-08-19 00:01:55 +0200542 for (i = 0; i < 16; i++) {
Vivien Didelota935c052016-09-29 12:21:53 -0400543 err = mv88e6xxx_g1_read(chip, GLOBAL_STATUS, &val);
544 if (err)
545 return err;
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200546
Barry Grussling19b2f972013-01-08 16:05:54 +0000547 usleep_range(1000, 2000);
Vivien Didelota935c052016-09-29 12:21:53 -0400548 if ((val & GLOBAL_STATUS_PPU_MASK) != GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000549 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000550 }
551
552 return -ETIMEDOUT;
553}
554
Vivien Didelotfad09c72016-06-21 12:28:20 -0400555static int mv88e6xxx_ppu_enable(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000556{
Vivien Didelota935c052016-09-29 12:21:53 -0400557 u16 val;
558 int i, err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000559
Vivien Didelota935c052016-09-29 12:21:53 -0400560 err = mv88e6xxx_g1_read(chip, GLOBAL_CONTROL, &val);
561 if (err)
562 return err;
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200563
Vivien Didelota935c052016-09-29 12:21:53 -0400564 err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL,
565 val | GLOBAL_CONTROL_PPU_ENABLE);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200566 if (err)
567 return err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000568
Andrew Lunn6441e6692016-08-19 00:01:55 +0200569 for (i = 0; i < 16; i++) {
Vivien Didelota935c052016-09-29 12:21:53 -0400570 err = mv88e6xxx_g1_read(chip, GLOBAL_STATUS, &val);
571 if (err)
572 return err;
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200573
Barry Grussling19b2f972013-01-08 16:05:54 +0000574 usleep_range(1000, 2000);
Vivien Didelota935c052016-09-29 12:21:53 -0400575 if ((val & GLOBAL_STATUS_PPU_MASK) == GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000576 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000577 }
578
579 return -ETIMEDOUT;
580}
581
582static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
583{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400584 struct mv88e6xxx_chip *chip;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000585
Vivien Didelotfad09c72016-06-21 12:28:20 -0400586 chip = container_of(ugly, struct mv88e6xxx_chip, ppu_work);
Vivien Didelot762eb672016-06-04 21:16:54 +0200587
Vivien Didelotfad09c72016-06-21 12:28:20 -0400588 mutex_lock(&chip->reg_lock);
Vivien Didelot762eb672016-06-04 21:16:54 +0200589
Vivien Didelotfad09c72016-06-21 12:28:20 -0400590 if (mutex_trylock(&chip->ppu_mutex)) {
591 if (mv88e6xxx_ppu_enable(chip) == 0)
592 chip->ppu_disabled = 0;
593 mutex_unlock(&chip->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000594 }
Vivien Didelot762eb672016-06-04 21:16:54 +0200595
Vivien Didelotfad09c72016-06-21 12:28:20 -0400596 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000597}
598
599static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
600{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400601 struct mv88e6xxx_chip *chip = (void *)_ps;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000602
Vivien Didelotfad09c72016-06-21 12:28:20 -0400603 schedule_work(&chip->ppu_work);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000604}
605
Vivien Didelotfad09c72016-06-21 12:28:20 -0400606static int mv88e6xxx_ppu_access_get(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000607{
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000608 int ret;
609
Vivien Didelotfad09c72016-06-21 12:28:20 -0400610 mutex_lock(&chip->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000611
Barry Grussling3675c8d2013-01-08 16:05:53 +0000612 /* If the PHY polling unit is enabled, disable it so that
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000613 * we can access the PHY registers. If it was already
614 * disabled, cancel the timer that is going to re-enable
615 * it.
616 */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400617 if (!chip->ppu_disabled) {
618 ret = mv88e6xxx_ppu_disable(chip);
Barry Grussling85686582013-01-08 16:05:56 +0000619 if (ret < 0) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400620 mutex_unlock(&chip->ppu_mutex);
Barry Grussling85686582013-01-08 16:05:56 +0000621 return ret;
622 }
Vivien Didelotfad09c72016-06-21 12:28:20 -0400623 chip->ppu_disabled = 1;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000624 } else {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400625 del_timer(&chip->ppu_timer);
Barry Grussling85686582013-01-08 16:05:56 +0000626 ret = 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000627 }
628
629 return ret;
630}
631
Vivien Didelotfad09c72016-06-21 12:28:20 -0400632static void mv88e6xxx_ppu_access_put(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000633{
Barry Grussling3675c8d2013-01-08 16:05:53 +0000634 /* Schedule a timer to re-enable the PHY polling unit. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400635 mod_timer(&chip->ppu_timer, jiffies + msecs_to_jiffies(10));
636 mutex_unlock(&chip->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000637}
638
Vivien Didelotfad09c72016-06-21 12:28:20 -0400639static void mv88e6xxx_ppu_state_init(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000640{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400641 mutex_init(&chip->ppu_mutex);
642 INIT_WORK(&chip->ppu_work, mv88e6xxx_ppu_reenable_work);
Wei Yongjun68497a82016-10-22 14:28:00 +0000643 setup_timer(&chip->ppu_timer, mv88e6xxx_ppu_reenable_timer,
644 (unsigned long)chip);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000645}
646
Andrew Lunn930188c2016-08-22 16:01:03 +0200647static void mv88e6xxx_ppu_state_destroy(struct mv88e6xxx_chip *chip)
648{
649 del_timer_sync(&chip->ppu_timer);
650}
651
Vivien Didelote57e5e72016-08-15 17:19:00 -0400652static int mv88e6xxx_phy_ppu_read(struct mv88e6xxx_chip *chip, int addr,
653 int reg, u16 *val)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000654{
Vivien Didelote57e5e72016-08-15 17:19:00 -0400655 int err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000656
Vivien Didelote57e5e72016-08-15 17:19:00 -0400657 err = mv88e6xxx_ppu_access_get(chip);
658 if (!err) {
659 err = mv88e6xxx_read(chip, addr, reg, val);
Vivien Didelotfad09c72016-06-21 12:28:20 -0400660 mv88e6xxx_ppu_access_put(chip);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000661 }
662
Vivien Didelote57e5e72016-08-15 17:19:00 -0400663 return err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000664}
665
Vivien Didelote57e5e72016-08-15 17:19:00 -0400666static int mv88e6xxx_phy_ppu_write(struct mv88e6xxx_chip *chip, int addr,
667 int reg, u16 val)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000668{
Vivien Didelote57e5e72016-08-15 17:19:00 -0400669 int err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000670
Vivien Didelote57e5e72016-08-15 17:19:00 -0400671 err = mv88e6xxx_ppu_access_get(chip);
672 if (!err) {
673 err = mv88e6xxx_write(chip, addr, reg, val);
Vivien Didelotfad09c72016-06-21 12:28:20 -0400674 mv88e6xxx_ppu_access_put(chip);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000675 }
676
Vivien Didelote57e5e72016-08-15 17:19:00 -0400677 return err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000678}
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000679
Vivien Didelotfad09c72016-06-21 12:28:20 -0400680static bool mv88e6xxx_6065_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200681{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400682 return chip->info->family == MV88E6XXX_FAMILY_6065;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200683}
684
Vivien Didelotfad09c72016-06-21 12:28:20 -0400685static bool mv88e6xxx_6095_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200686{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400687 return chip->info->family == MV88E6XXX_FAMILY_6095;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200688}
689
Vivien Didelotfad09c72016-06-21 12:28:20 -0400690static bool mv88e6xxx_6097_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200691{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400692 return chip->info->family == MV88E6XXX_FAMILY_6097;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200693}
694
Vivien Didelotfad09c72016-06-21 12:28:20 -0400695static bool mv88e6xxx_6165_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200696{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400697 return chip->info->family == MV88E6XXX_FAMILY_6165;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200698}
699
Vivien Didelotfad09c72016-06-21 12:28:20 -0400700static bool mv88e6xxx_6185_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200701{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400702 return chip->info->family == MV88E6XXX_FAMILY_6185;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200703}
704
Vivien Didelotfad09c72016-06-21 12:28:20 -0400705static bool mv88e6xxx_6320_family(struct mv88e6xxx_chip *chip)
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700706{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400707 return chip->info->family == MV88E6XXX_FAMILY_6320;
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700708}
709
Vivien Didelotfad09c72016-06-21 12:28:20 -0400710static bool mv88e6xxx_6351_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200711{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400712 return chip->info->family == MV88E6XXX_FAMILY_6351;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200713}
714
Vivien Didelotfad09c72016-06-21 12:28:20 -0400715static bool mv88e6xxx_6352_family(struct mv88e6xxx_chip *chip)
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200716{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400717 return chip->info->family == MV88E6XXX_FAMILY_6352;
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200718}
719
Vivien Didelotd78343d2016-11-04 03:23:36 +0100720static int mv88e6xxx_port_setup_mac(struct mv88e6xxx_chip *chip, int port,
721 int link, int speed, int duplex,
722 phy_interface_t mode)
723{
724 int err;
725
726 if (!chip->info->ops->port_set_link)
727 return 0;
728
729 /* Port's MAC control must not be changed unless the link is down */
730 err = chip->info->ops->port_set_link(chip, port, 0);
731 if (err)
732 return err;
733
734 if (chip->info->ops->port_set_speed) {
735 err = chip->info->ops->port_set_speed(chip, port, speed);
736 if (err && err != -EOPNOTSUPP)
737 goto restore_link;
738 }
739
740 if (chip->info->ops->port_set_duplex) {
741 err = chip->info->ops->port_set_duplex(chip, port, duplex);
742 if (err && err != -EOPNOTSUPP)
743 goto restore_link;
744 }
745
746 if (chip->info->ops->port_set_rgmii_delay) {
747 err = chip->info->ops->port_set_rgmii_delay(chip, port, mode);
748 if (err && err != -EOPNOTSUPP)
749 goto restore_link;
750 }
751
752 err = 0;
753restore_link:
754 if (chip->info->ops->port_set_link(chip, port, link))
755 netdev_err(chip->ds->ports[port].netdev,
756 "failed to restore MAC's link\n");
757
758 return err;
759}
760
Andrew Lunndea87022015-08-31 15:56:47 +0200761/* We expect the switch to perform auto negotiation if there is a real
762 * phy. However, in the case of a fixed link phy, we force the port
763 * settings from the fixed link settings.
764 */
Vivien Didelotf81ec902016-05-09 13:22:58 -0400765static void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port,
766 struct phy_device *phydev)
Andrew Lunndea87022015-08-31 15:56:47 +0200767{
Vivien Didelot04bed142016-08-31 18:06:13 -0400768 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200769 int err;
Andrew Lunndea87022015-08-31 15:56:47 +0200770
771 if (!phy_is_pseudo_fixed_link(phydev))
772 return;
773
Vivien Didelotfad09c72016-06-21 12:28:20 -0400774 mutex_lock(&chip->reg_lock);
Vivien Didelotd78343d2016-11-04 03:23:36 +0100775 err = mv88e6xxx_port_setup_mac(chip, port, phydev->link, phydev->speed,
776 phydev->duplex, phydev->interface);
Vivien Didelotfad09c72016-06-21 12:28:20 -0400777 mutex_unlock(&chip->reg_lock);
Vivien Didelotd78343d2016-11-04 03:23:36 +0100778
779 if (err && err != -EOPNOTSUPP)
780 netdev_err(ds->ports[port].netdev, "failed to configure MAC\n");
Andrew Lunndea87022015-08-31 15:56:47 +0200781}
782
Andrew Lunna605a0f2016-11-21 23:26:58 +0100783static int mv88e6xxx_stats_snapshot(struct mv88e6xxx_chip *chip, int port)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000784{
Andrew Lunna605a0f2016-11-21 23:26:58 +0100785 if (!chip->info->ops->stats_snapshot)
786 return -EOPNOTSUPP;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000787
Andrew Lunna605a0f2016-11-21 23:26:58 +0100788 return chip->info->ops->stats_snapshot(chip, port);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000789}
790
Andrew Lunne413e7e2015-04-02 04:06:38 +0200791static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
Andrew Lunndfafe442016-11-21 23:27:02 +0100792 { "in_good_octets", 8, 0x00, STATS_TYPE_BANK0, },
793 { "in_bad_octets", 4, 0x02, STATS_TYPE_BANK0, },
794 { "in_unicast", 4, 0x04, STATS_TYPE_BANK0, },
795 { "in_broadcasts", 4, 0x06, STATS_TYPE_BANK0, },
796 { "in_multicasts", 4, 0x07, STATS_TYPE_BANK0, },
797 { "in_pause", 4, 0x16, STATS_TYPE_BANK0, },
798 { "in_undersize", 4, 0x18, STATS_TYPE_BANK0, },
799 { "in_fragments", 4, 0x19, STATS_TYPE_BANK0, },
800 { "in_oversize", 4, 0x1a, STATS_TYPE_BANK0, },
801 { "in_jabber", 4, 0x1b, STATS_TYPE_BANK0, },
802 { "in_rx_error", 4, 0x1c, STATS_TYPE_BANK0, },
803 { "in_fcs_error", 4, 0x1d, STATS_TYPE_BANK0, },
804 { "out_octets", 8, 0x0e, STATS_TYPE_BANK0, },
805 { "out_unicast", 4, 0x10, STATS_TYPE_BANK0, },
806 { "out_broadcasts", 4, 0x13, STATS_TYPE_BANK0, },
807 { "out_multicasts", 4, 0x12, STATS_TYPE_BANK0, },
808 { "out_pause", 4, 0x15, STATS_TYPE_BANK0, },
809 { "excessive", 4, 0x11, STATS_TYPE_BANK0, },
810 { "collisions", 4, 0x1e, STATS_TYPE_BANK0, },
811 { "deferred", 4, 0x05, STATS_TYPE_BANK0, },
812 { "single", 4, 0x14, STATS_TYPE_BANK0, },
813 { "multiple", 4, 0x17, STATS_TYPE_BANK0, },
814 { "out_fcs_error", 4, 0x03, STATS_TYPE_BANK0, },
815 { "late", 4, 0x1f, STATS_TYPE_BANK0, },
816 { "hist_64bytes", 4, 0x08, STATS_TYPE_BANK0, },
817 { "hist_65_127bytes", 4, 0x09, STATS_TYPE_BANK0, },
818 { "hist_128_255bytes", 4, 0x0a, STATS_TYPE_BANK0, },
819 { "hist_256_511bytes", 4, 0x0b, STATS_TYPE_BANK0, },
820 { "hist_512_1023bytes", 4, 0x0c, STATS_TYPE_BANK0, },
821 { "hist_1024_max_bytes", 4, 0x0d, STATS_TYPE_BANK0, },
822 { "sw_in_discards", 4, 0x10, STATS_TYPE_PORT, },
823 { "sw_in_filtered", 2, 0x12, STATS_TYPE_PORT, },
824 { "sw_out_filtered", 2, 0x13, STATS_TYPE_PORT, },
825 { "in_discards", 4, 0x00, STATS_TYPE_BANK1, },
826 { "in_filtered", 4, 0x01, STATS_TYPE_BANK1, },
827 { "in_accepted", 4, 0x02, STATS_TYPE_BANK1, },
828 { "in_bad_accepted", 4, 0x03, STATS_TYPE_BANK1, },
829 { "in_good_avb_class_a", 4, 0x04, STATS_TYPE_BANK1, },
830 { "in_good_avb_class_b", 4, 0x05, STATS_TYPE_BANK1, },
831 { "in_bad_avb_class_a", 4, 0x06, STATS_TYPE_BANK1, },
832 { "in_bad_avb_class_b", 4, 0x07, STATS_TYPE_BANK1, },
833 { "tcam_counter_0", 4, 0x08, STATS_TYPE_BANK1, },
834 { "tcam_counter_1", 4, 0x09, STATS_TYPE_BANK1, },
835 { "tcam_counter_2", 4, 0x0a, STATS_TYPE_BANK1, },
836 { "tcam_counter_3", 4, 0x0b, STATS_TYPE_BANK1, },
837 { "in_da_unknown", 4, 0x0e, STATS_TYPE_BANK1, },
838 { "in_management", 4, 0x0f, STATS_TYPE_BANK1, },
839 { "out_queue_0", 4, 0x10, STATS_TYPE_BANK1, },
840 { "out_queue_1", 4, 0x11, STATS_TYPE_BANK1, },
841 { "out_queue_2", 4, 0x12, STATS_TYPE_BANK1, },
842 { "out_queue_3", 4, 0x13, STATS_TYPE_BANK1, },
843 { "out_queue_4", 4, 0x14, STATS_TYPE_BANK1, },
844 { "out_queue_5", 4, 0x15, STATS_TYPE_BANK1, },
845 { "out_queue_6", 4, 0x16, STATS_TYPE_BANK1, },
846 { "out_queue_7", 4, 0x17, STATS_TYPE_BANK1, },
847 { "out_cut_through", 4, 0x18, STATS_TYPE_BANK1, },
848 { "out_octets_a", 4, 0x1a, STATS_TYPE_BANK1, },
849 { "out_octets_b", 4, 0x1b, STATS_TYPE_BANK1, },
850 { "out_management", 4, 0x1f, STATS_TYPE_BANK1, },
Andrew Lunne413e7e2015-04-02 04:06:38 +0200851};
852
Vivien Didelotfad09c72016-06-21 12:28:20 -0400853static uint64_t _mv88e6xxx_get_ethtool_stat(struct mv88e6xxx_chip *chip,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100854 struct mv88e6xxx_hw_stat *s,
Andrew Lunne0d8b612016-11-21 23:27:04 +0100855 int port, u16 bank1_select,
856 u16 histogram)
Andrew Lunn80c46272015-06-20 18:42:30 +0200857{
Andrew Lunn80c46272015-06-20 18:42:30 +0200858 u32 low;
859 u32 high = 0;
Andrew Lunndfafe442016-11-21 23:27:02 +0100860 u16 reg = 0;
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200861 int err;
Andrew Lunn80c46272015-06-20 18:42:30 +0200862 u64 value;
863
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100864 switch (s->type) {
Andrew Lunndfafe442016-11-21 23:27:02 +0100865 case STATS_TYPE_PORT:
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200866 err = mv88e6xxx_port_read(chip, port, s->reg, &reg);
867 if (err)
Andrew Lunn80c46272015-06-20 18:42:30 +0200868 return UINT64_MAX;
869
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200870 low = reg;
Andrew Lunn80c46272015-06-20 18:42:30 +0200871 if (s->sizeof_stat == 4) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200872 err = mv88e6xxx_port_read(chip, port, s->reg + 1, &reg);
873 if (err)
Andrew Lunn80c46272015-06-20 18:42:30 +0200874 return UINT64_MAX;
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200875 high = reg;
Andrew Lunn80c46272015-06-20 18:42:30 +0200876 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100877 break;
Andrew Lunndfafe442016-11-21 23:27:02 +0100878 case STATS_TYPE_BANK1:
Andrew Lunne0d8b612016-11-21 23:27:04 +0100879 reg = bank1_select;
Andrew Lunndfafe442016-11-21 23:27:02 +0100880 /* fall through */
881 case STATS_TYPE_BANK0:
Andrew Lunne0d8b612016-11-21 23:27:04 +0100882 reg |= s->reg | histogram;
Andrew Lunn7f9ef3a2016-11-21 23:27:05 +0100883 mv88e6xxx_g1_stats_read(chip, reg, &low);
Andrew Lunn80c46272015-06-20 18:42:30 +0200884 if (s->sizeof_stat == 8)
Andrew Lunn7f9ef3a2016-11-21 23:27:05 +0100885 mv88e6xxx_g1_stats_read(chip, reg + 1, &high);
Andrew Lunn80c46272015-06-20 18:42:30 +0200886 }
887 value = (((u64)high) << 16) | low;
888 return value;
889}
890
Andrew Lunndfafe442016-11-21 23:27:02 +0100891static void mv88e6xxx_stats_get_strings(struct mv88e6xxx_chip *chip,
892 uint8_t *data, int types)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100893{
894 struct mv88e6xxx_hw_stat *stat;
895 int i, j;
896
897 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
898 stat = &mv88e6xxx_hw_stats[i];
Andrew Lunndfafe442016-11-21 23:27:02 +0100899 if (stat->type & types) {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100900 memcpy(data + j * ETH_GSTRING_LEN, stat->string,
901 ETH_GSTRING_LEN);
902 j++;
903 }
904 }
905}
906
Andrew Lunndfafe442016-11-21 23:27:02 +0100907static void mv88e6095_stats_get_strings(struct mv88e6xxx_chip *chip,
908 uint8_t *data)
909{
910 mv88e6xxx_stats_get_strings(chip, data,
911 STATS_TYPE_BANK0 | STATS_TYPE_PORT);
912}
913
914static void mv88e6320_stats_get_strings(struct mv88e6xxx_chip *chip,
915 uint8_t *data)
916{
917 mv88e6xxx_stats_get_strings(chip, data,
918 STATS_TYPE_BANK0 | STATS_TYPE_BANK1);
919}
920
921static void mv88e6xxx_get_strings(struct dsa_switch *ds, int port,
922 uint8_t *data)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100923{
Vivien Didelot04bed142016-08-31 18:06:13 -0400924 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunndfafe442016-11-21 23:27:02 +0100925
926 if (chip->info->ops->stats_get_strings)
927 chip->info->ops->stats_get_strings(chip, data);
928}
929
930static int mv88e6xxx_stats_get_sset_count(struct mv88e6xxx_chip *chip,
931 int types)
932{
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100933 struct mv88e6xxx_hw_stat *stat;
934 int i, j;
935
936 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
937 stat = &mv88e6xxx_hw_stats[i];
Andrew Lunndfafe442016-11-21 23:27:02 +0100938 if (stat->type & types)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100939 j++;
940 }
941 return j;
942}
943
Andrew Lunndfafe442016-11-21 23:27:02 +0100944static int mv88e6095_stats_get_sset_count(struct mv88e6xxx_chip *chip)
945{
946 return mv88e6xxx_stats_get_sset_count(chip, STATS_TYPE_BANK0 |
947 STATS_TYPE_PORT);
948}
949
950static int mv88e6320_stats_get_sset_count(struct mv88e6xxx_chip *chip)
951{
952 return mv88e6xxx_stats_get_sset_count(chip, STATS_TYPE_BANK0 |
953 STATS_TYPE_BANK1);
954}
955
956static int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
957{
958 struct mv88e6xxx_chip *chip = ds->priv;
959
960 if (chip->info->ops->stats_get_sset_count)
961 return chip->info->ops->stats_get_sset_count(chip);
962
963 return 0;
964}
965
Andrew Lunn052f9472016-11-21 23:27:03 +0100966static void mv88e6xxx_stats_get_stats(struct mv88e6xxx_chip *chip, int port,
Andrew Lunne0d8b612016-11-21 23:27:04 +0100967 uint64_t *data, int types,
968 u16 bank1_select, u16 histogram)
Andrew Lunn052f9472016-11-21 23:27:03 +0100969{
970 struct mv88e6xxx_hw_stat *stat;
971 int i, j;
972
973 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
974 stat = &mv88e6xxx_hw_stats[i];
975 if (stat->type & types) {
Andrew Lunne0d8b612016-11-21 23:27:04 +0100976 data[j] = _mv88e6xxx_get_ethtool_stat(chip, stat, port,
977 bank1_select,
978 histogram);
Andrew Lunn052f9472016-11-21 23:27:03 +0100979 j++;
980 }
981 }
982}
983
984static void mv88e6095_stats_get_stats(struct mv88e6xxx_chip *chip, int port,
985 uint64_t *data)
986{
987 return mv88e6xxx_stats_get_stats(chip, port, data,
Andrew Lunne0d8b612016-11-21 23:27:04 +0100988 STATS_TYPE_BANK0 | STATS_TYPE_PORT,
989 0, GLOBAL_STATS_OP_HIST_RX_TX);
Andrew Lunn052f9472016-11-21 23:27:03 +0100990}
991
992static void mv88e6320_stats_get_stats(struct mv88e6xxx_chip *chip, int port,
993 uint64_t *data)
994{
995 return mv88e6xxx_stats_get_stats(chip, port, data,
Andrew Lunne0d8b612016-11-21 23:27:04 +0100996 STATS_TYPE_BANK0 | STATS_TYPE_BANK1,
997 GLOBAL_STATS_OP_BANK_1_BIT_9,
998 GLOBAL_STATS_OP_HIST_RX_TX);
999}
1000
1001static void mv88e6390_stats_get_stats(struct mv88e6xxx_chip *chip, int port,
1002 uint64_t *data)
1003{
1004 return mv88e6xxx_stats_get_stats(chip, port, data,
1005 STATS_TYPE_BANK0 | STATS_TYPE_BANK1,
1006 GLOBAL_STATS_OP_BANK_1_BIT_10, 0);
Andrew Lunn052f9472016-11-21 23:27:03 +01001007}
1008
1009static void mv88e6xxx_get_stats(struct mv88e6xxx_chip *chip, int port,
1010 uint64_t *data)
1011{
1012 if (chip->info->ops->stats_get_stats)
1013 chip->info->ops->stats_get_stats(chip, port, data);
1014}
1015
Vivien Didelotf81ec902016-05-09 13:22:58 -04001016static void mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, int port,
1017 uint64_t *data)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001018{
Vivien Didelot04bed142016-08-31 18:06:13 -04001019 struct mv88e6xxx_chip *chip = ds->priv;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001020 int ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001021
Vivien Didelotfad09c72016-06-21 12:28:20 -04001022 mutex_lock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001023
Andrew Lunna605a0f2016-11-21 23:26:58 +01001024 ret = mv88e6xxx_stats_snapshot(chip, port);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001025 if (ret < 0) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001026 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001027 return;
1028 }
Andrew Lunn052f9472016-11-21 23:27:03 +01001029
1030 mv88e6xxx_get_stats(chip, port, data);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001031
Vivien Didelotfad09c72016-06-21 12:28:20 -04001032 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001033}
Ben Hutchings98e67302011-11-25 14:36:19 +00001034
Andrew Lunnde2273872016-11-21 23:27:01 +01001035static int mv88e6xxx_stats_set_histogram(struct mv88e6xxx_chip *chip)
1036{
1037 if (chip->info->ops->stats_set_histogram)
1038 return chip->info->ops->stats_set_histogram(chip);
1039
1040 return 0;
1041}
1042
Vivien Didelotf81ec902016-05-09 13:22:58 -04001043static int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001044{
1045 return 32 * sizeof(u16);
1046}
1047
Vivien Didelotf81ec902016-05-09 13:22:58 -04001048static void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
1049 struct ethtool_regs *regs, void *_p)
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001050{
Vivien Didelot04bed142016-08-31 18:06:13 -04001051 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001052 int err;
1053 u16 reg;
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001054 u16 *p = _p;
1055 int i;
1056
1057 regs->version = 0;
1058
1059 memset(p, 0xff, 32 * sizeof(u16));
1060
Vivien Didelotfad09c72016-06-21 12:28:20 -04001061 mutex_lock(&chip->reg_lock);
Vivien Didelot23062512016-05-09 13:22:45 -04001062
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001063 for (i = 0; i < 32; i++) {
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001064
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001065 err = mv88e6xxx_port_read(chip, port, i, &reg);
1066 if (!err)
1067 p[i] = reg;
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001068 }
Vivien Didelot23062512016-05-09 13:22:45 -04001069
Vivien Didelotfad09c72016-06-21 12:28:20 -04001070 mutex_unlock(&chip->reg_lock);
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001071}
1072
Vivien Didelotfad09c72016-06-21 12:28:20 -04001073static int _mv88e6xxx_atu_wait(struct mv88e6xxx_chip *chip)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001074{
Vivien Didelota935c052016-09-29 12:21:53 -04001075 return mv88e6xxx_g1_wait(chip, GLOBAL_ATU_OP, GLOBAL_ATU_OP_BUSY);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001076}
1077
Vivien Didelotf81ec902016-05-09 13:22:58 -04001078static int mv88e6xxx_get_eee(struct dsa_switch *ds, int port,
1079 struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -08001080{
Vivien Didelot04bed142016-08-31 18:06:13 -04001081 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot9c938292016-08-15 17:19:02 -04001082 u16 reg;
1083 int err;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001084
Vivien Didelotfad09c72016-06-21 12:28:20 -04001085 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
Vivien Didelotaadbdb82016-05-09 13:22:44 -04001086 return -EOPNOTSUPP;
1087
Vivien Didelotfad09c72016-06-21 12:28:20 -04001088 mutex_lock(&chip->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001089
Vivien Didelot9c938292016-08-15 17:19:02 -04001090 err = mv88e6xxx_phy_read(chip, port, 16, &reg);
1091 if (err)
Andrew Lunn2f40c692015-04-02 04:06:37 +02001092 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001093
1094 e->eee_enabled = !!(reg & 0x0200);
1095 e->tx_lpi_enabled = !!(reg & 0x0100);
1096
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001097 err = mv88e6xxx_port_read(chip, port, PORT_STATUS, &reg);
Vivien Didelot9c938292016-08-15 17:19:02 -04001098 if (err)
Andrew Lunn2f40c692015-04-02 04:06:37 +02001099 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001100
Andrew Lunncca8b132015-04-02 04:06:39 +02001101 e->eee_active = !!(reg & PORT_STATUS_EEE);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001102out:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001103 mutex_unlock(&chip->reg_lock);
Vivien Didelot9c938292016-08-15 17:19:02 -04001104
1105 return err;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001106}
1107
Vivien Didelotf81ec902016-05-09 13:22:58 -04001108static int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
1109 struct phy_device *phydev, struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -08001110{
Vivien Didelot04bed142016-08-31 18:06:13 -04001111 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot9c938292016-08-15 17:19:02 -04001112 u16 reg;
1113 int err;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001114
Vivien Didelotfad09c72016-06-21 12:28:20 -04001115 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
Vivien Didelotaadbdb82016-05-09 13:22:44 -04001116 return -EOPNOTSUPP;
1117
Vivien Didelotfad09c72016-06-21 12:28:20 -04001118 mutex_lock(&chip->reg_lock);
Guenter Roeck11b3b452015-03-06 22:23:51 -08001119
Vivien Didelot9c938292016-08-15 17:19:02 -04001120 err = mv88e6xxx_phy_read(chip, port, 16, &reg);
1121 if (err)
Andrew Lunn2f40c692015-04-02 04:06:37 +02001122 goto out;
1123
Vivien Didelot9c938292016-08-15 17:19:02 -04001124 reg &= ~0x0300;
Andrew Lunn2f40c692015-04-02 04:06:37 +02001125 if (e->eee_enabled)
1126 reg |= 0x0200;
1127 if (e->tx_lpi_enabled)
1128 reg |= 0x0100;
1129
Vivien Didelot9c938292016-08-15 17:19:02 -04001130 err = mv88e6xxx_phy_write(chip, port, 16, reg);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001131out:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001132 mutex_unlock(&chip->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001133
Vivien Didelot9c938292016-08-15 17:19:02 -04001134 return err;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001135}
1136
Vivien Didelotfad09c72016-06-21 12:28:20 -04001137static int _mv88e6xxx_atu_cmd(struct mv88e6xxx_chip *chip, u16 fid, u16 cmd)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001138{
Vivien Didelota935c052016-09-29 12:21:53 -04001139 u16 val;
1140 int err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001141
Vivien Didelot6dc10bb2016-09-29 12:21:55 -04001142 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G1_ATU_FID)) {
Vivien Didelota935c052016-09-29 12:21:53 -04001143 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_FID, fid);
1144 if (err)
1145 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001146 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001147 /* ATU DBNum[7:4] are located in ATU Control 15:12 */
Vivien Didelota935c052016-09-29 12:21:53 -04001148 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_CONTROL, &val);
1149 if (err)
1150 return err;
Vivien Didelot11ea8092016-03-31 16:53:44 -04001151
Vivien Didelota935c052016-09-29 12:21:53 -04001152 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_CONTROL,
1153 (val & 0xfff) | ((fid << 8) & 0xf000));
1154 if (err)
1155 return err;
Vivien Didelot11ea8092016-03-31 16:53:44 -04001156
1157 /* ATU DBNum[3:0] are located in ATU Operation 3:0 */
1158 cmd |= fid & 0xf;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001159 }
1160
Vivien Didelota935c052016-09-29 12:21:53 -04001161 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_OP, cmd);
1162 if (err)
1163 return err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001164
Vivien Didelotfad09c72016-06-21 12:28:20 -04001165 return _mv88e6xxx_atu_wait(chip);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001166}
1167
Vivien Didelotfad09c72016-06-21 12:28:20 -04001168static int _mv88e6xxx_atu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot37705b72015-09-04 14:34:11 -04001169 struct mv88e6xxx_atu_entry *entry)
1170{
1171 u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK;
1172
1173 if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) {
1174 unsigned int mask, shift;
1175
1176 if (entry->trunk) {
1177 data |= GLOBAL_ATU_DATA_TRUNK;
1178 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
1179 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
1180 } else {
1181 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
1182 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
1183 }
1184
1185 data |= (entry->portv_trunkid << shift) & mask;
1186 }
1187
Vivien Didelota935c052016-09-29 12:21:53 -04001188 return mv88e6xxx_g1_write(chip, GLOBAL_ATU_DATA, data);
Vivien Didelot37705b72015-09-04 14:34:11 -04001189}
1190
Vivien Didelotfad09c72016-06-21 12:28:20 -04001191static int _mv88e6xxx_atu_flush_move(struct mv88e6xxx_chip *chip,
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001192 struct mv88e6xxx_atu_entry *entry,
1193 bool static_too)
1194{
1195 int op;
1196 int err;
1197
Vivien Didelotfad09c72016-06-21 12:28:20 -04001198 err = _mv88e6xxx_atu_wait(chip);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001199 if (err)
1200 return err;
1201
Vivien Didelotfad09c72016-06-21 12:28:20 -04001202 err = _mv88e6xxx_atu_data_write(chip, entry);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001203 if (err)
1204 return err;
1205
1206 if (entry->fid) {
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001207 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB :
1208 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB;
1209 } else {
1210 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL :
1211 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC;
1212 }
1213
Vivien Didelotfad09c72016-06-21 12:28:20 -04001214 return _mv88e6xxx_atu_cmd(chip, entry->fid, op);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001215}
1216
Vivien Didelotfad09c72016-06-21 12:28:20 -04001217static int _mv88e6xxx_atu_flush(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001218 u16 fid, bool static_too)
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001219{
1220 struct mv88e6xxx_atu_entry entry = {
1221 .fid = fid,
1222 .state = 0, /* EntryState bits must be 0 */
1223 };
1224
Vivien Didelotfad09c72016-06-21 12:28:20 -04001225 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001226}
1227
Vivien Didelotfad09c72016-06-21 12:28:20 -04001228static int _mv88e6xxx_atu_move(struct mv88e6xxx_chip *chip, u16 fid,
Andrew Lunn158bc062016-04-28 21:24:06 -04001229 int from_port, int to_port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001230{
1231 struct mv88e6xxx_atu_entry entry = {
1232 .trunk = false,
1233 .fid = fid,
1234 };
1235
1236 /* EntryState bits must be 0xF */
1237 entry.state = GLOBAL_ATU_DATA_STATE_MASK;
1238
1239 /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */
1240 entry.portv_trunkid = (to_port & 0x0f) << 4;
1241 entry.portv_trunkid |= from_port & 0x0f;
1242
Vivien Didelotfad09c72016-06-21 12:28:20 -04001243 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001244}
1245
Vivien Didelotfad09c72016-06-21 12:28:20 -04001246static int _mv88e6xxx_atu_remove(struct mv88e6xxx_chip *chip, u16 fid,
Andrew Lunn158bc062016-04-28 21:24:06 -04001247 int port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001248{
1249 /* Destination port 0xF means remove the entries */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001250 return _mv88e6xxx_atu_move(chip, fid, port, 0x0f, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001251}
1252
Vivien Didelotfad09c72016-06-21 12:28:20 -04001253static int _mv88e6xxx_port_based_vlan_map(struct mv88e6xxx_chip *chip, int port)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001254{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001255 struct net_device *bridge = chip->ports[port].bridge_dev;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001256 struct dsa_switch *ds = chip->ds;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001257 u16 output_ports = 0;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001258 int i;
1259
1260 /* allow CPU port or DSA link(s) to send frames to every port */
1261 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
Vivien Didelot5a7921f2016-11-04 03:23:28 +01001262 output_ports = ~0;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001263 } else {
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001264 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001265 /* allow sending frames to every group member */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001266 if (bridge && chip->ports[i].bridge_dev == bridge)
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001267 output_ports |= BIT(i);
1268
1269 /* allow sending frames to CPU port and DSA link(s) */
1270 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
1271 output_ports |= BIT(i);
1272 }
1273 }
1274
1275 /* prevent frames from going back out of the port they came in on */
1276 output_ports &= ~BIT(port);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001277
Vivien Didelot5a7921f2016-11-04 03:23:28 +01001278 return mv88e6xxx_port_set_vlan_map(chip, port, output_ports);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001279}
1280
Vivien Didelotf81ec902016-05-09 13:22:58 -04001281static void mv88e6xxx_port_stp_state_set(struct dsa_switch *ds, int port,
1282 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001283{
Vivien Didelot04bed142016-08-31 18:06:13 -04001284 struct mv88e6xxx_chip *chip = ds->priv;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001285 int stp_state;
Vivien Didelot553eb542016-05-13 20:38:23 -04001286 int err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001287
1288 switch (state) {
1289 case BR_STATE_DISABLED:
Andrew Lunncca8b132015-04-02 04:06:39 +02001290 stp_state = PORT_CONTROL_STATE_DISABLED;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001291 break;
1292 case BR_STATE_BLOCKING:
1293 case BR_STATE_LISTENING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001294 stp_state = PORT_CONTROL_STATE_BLOCKING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001295 break;
1296 case BR_STATE_LEARNING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001297 stp_state = PORT_CONTROL_STATE_LEARNING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001298 break;
1299 case BR_STATE_FORWARDING:
1300 default:
Andrew Lunncca8b132015-04-02 04:06:39 +02001301 stp_state = PORT_CONTROL_STATE_FORWARDING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001302 break;
1303 }
1304
Vivien Didelotfad09c72016-06-21 12:28:20 -04001305 mutex_lock(&chip->reg_lock);
Vivien Didelote28def332016-11-04 03:23:27 +01001306 err = mv88e6xxx_port_set_state(chip, port, stp_state);
Vivien Didelotfad09c72016-06-21 12:28:20 -04001307 mutex_unlock(&chip->reg_lock);
Vivien Didelot553eb542016-05-13 20:38:23 -04001308
1309 if (err)
Vivien Didelote28def332016-11-04 03:23:27 +01001310 netdev_err(ds->ports[port].netdev, "failed to update state\n");
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001311}
1312
Vivien Didelot749efcb2016-09-22 16:49:24 -04001313static void mv88e6xxx_port_fast_age(struct dsa_switch *ds, int port)
1314{
1315 struct mv88e6xxx_chip *chip = ds->priv;
1316 int err;
1317
1318 mutex_lock(&chip->reg_lock);
1319 err = _mv88e6xxx_atu_remove(chip, 0, port, false);
1320 mutex_unlock(&chip->reg_lock);
1321
1322 if (err)
1323 netdev_err(ds->ports[port].netdev, "failed to flush ATU\n");
1324}
1325
Vivien Didelotfad09c72016-06-21 12:28:20 -04001326static int _mv88e6xxx_vtu_wait(struct mv88e6xxx_chip *chip)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001327{
Vivien Didelota935c052016-09-29 12:21:53 -04001328 return mv88e6xxx_g1_wait(chip, GLOBAL_VTU_OP, GLOBAL_VTU_OP_BUSY);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001329}
1330
Vivien Didelotfad09c72016-06-21 12:28:20 -04001331static int _mv88e6xxx_vtu_cmd(struct mv88e6xxx_chip *chip, u16 op)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001332{
Vivien Didelota935c052016-09-29 12:21:53 -04001333 int err;
Vivien Didelot6b17e862015-08-13 12:52:18 -04001334
Vivien Didelota935c052016-09-29 12:21:53 -04001335 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_OP, op);
1336 if (err)
1337 return err;
Vivien Didelot6b17e862015-08-13 12:52:18 -04001338
Vivien Didelotfad09c72016-06-21 12:28:20 -04001339 return _mv88e6xxx_vtu_wait(chip);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001340}
1341
Vivien Didelotfad09c72016-06-21 12:28:20 -04001342static int _mv88e6xxx_vtu_stu_flush(struct mv88e6xxx_chip *chip)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001343{
1344 int ret;
1345
Vivien Didelotfad09c72016-06-21 12:28:20 -04001346 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001347 if (ret < 0)
1348 return ret;
1349
Vivien Didelotfad09c72016-06-21 12:28:20 -04001350 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_FLUSH_ALL);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001351}
1352
Vivien Didelotfad09c72016-06-21 12:28:20 -04001353static int _mv88e6xxx_vtu_stu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001354 struct mv88e6xxx_vtu_entry *entry,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001355 unsigned int nibble_offset)
1356{
Vivien Didelotb8fee952015-08-13 12:52:19 -04001357 u16 regs[3];
Vivien Didelota935c052016-09-29 12:21:53 -04001358 int i, err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001359
1360 for (i = 0; i < 3; ++i) {
Vivien Didelota935c052016-09-29 12:21:53 -04001361 u16 *reg = &regs[i];
Vivien Didelotb8fee952015-08-13 12:52:19 -04001362
Vivien Didelota935c052016-09-29 12:21:53 -04001363 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_DATA_0_3 + i, reg);
1364 if (err)
1365 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001366 }
1367
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001368 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotb8fee952015-08-13 12:52:19 -04001369 unsigned int shift = (i % 4) * 4 + nibble_offset;
1370 u16 reg = regs[i / 4];
1371
1372 entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
1373 }
1374
1375 return 0;
1376}
1377
Vivien Didelotfad09c72016-06-21 12:28:20 -04001378static int mv88e6xxx_vtu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001379 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001380{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001381 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 0);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001382}
1383
Vivien Didelotfad09c72016-06-21 12:28:20 -04001384static int mv88e6xxx_stu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001385 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001386{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001387 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 2);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001388}
1389
Vivien Didelotfad09c72016-06-21 12:28:20 -04001390static int _mv88e6xxx_vtu_stu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001391 struct mv88e6xxx_vtu_entry *entry,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001392 unsigned int nibble_offset)
1393{
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001394 u16 regs[3] = { 0 };
Vivien Didelota935c052016-09-29 12:21:53 -04001395 int i, err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001396
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001397 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001398 unsigned int shift = (i % 4) * 4 + nibble_offset;
1399 u8 data = entry->data[i];
1400
1401 regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
1402 }
1403
1404 for (i = 0; i < 3; ++i) {
Vivien Didelota935c052016-09-29 12:21:53 -04001405 u16 reg = regs[i];
1406
1407 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_DATA_0_3 + i, reg);
1408 if (err)
1409 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001410 }
1411
1412 return 0;
1413}
1414
Vivien Didelotfad09c72016-06-21 12:28:20 -04001415static int mv88e6xxx_vtu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001416 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001417{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001418 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 0);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001419}
1420
Vivien Didelotfad09c72016-06-21 12:28:20 -04001421static int mv88e6xxx_stu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001422 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001423{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001424 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 2);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001425}
1426
Vivien Didelotfad09c72016-06-21 12:28:20 -04001427static int _mv88e6xxx_vtu_vid_write(struct mv88e6xxx_chip *chip, u16 vid)
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001428{
Vivien Didelota935c052016-09-29 12:21:53 -04001429 return mv88e6xxx_g1_write(chip, GLOBAL_VTU_VID,
1430 vid & GLOBAL_VTU_VID_MASK);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001431}
1432
Vivien Didelotfad09c72016-06-21 12:28:20 -04001433static int _mv88e6xxx_vtu_getnext(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001434 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelotb8fee952015-08-13 12:52:19 -04001435{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001436 struct mv88e6xxx_vtu_entry next = { 0 };
Vivien Didelota935c052016-09-29 12:21:53 -04001437 u16 val;
1438 int err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001439
Vivien Didelota935c052016-09-29 12:21:53 -04001440 err = _mv88e6xxx_vtu_wait(chip);
1441 if (err)
1442 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001443
Vivien Didelota935c052016-09-29 12:21:53 -04001444 err = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_VTU_GET_NEXT);
1445 if (err)
1446 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001447
Vivien Didelota935c052016-09-29 12:21:53 -04001448 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_VID, &val);
1449 if (err)
1450 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001451
Vivien Didelota935c052016-09-29 12:21:53 -04001452 next.vid = val & GLOBAL_VTU_VID_MASK;
1453 next.valid = !!(val & GLOBAL_VTU_VID_VALID);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001454
1455 if (next.valid) {
Vivien Didelota935c052016-09-29 12:21:53 -04001456 err = mv88e6xxx_vtu_data_read(chip, &next);
1457 if (err)
1458 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001459
Vivien Didelot6dc10bb2016-09-29 12:21:55 -04001460 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G1_VTU_FID)) {
Vivien Didelota935c052016-09-29 12:21:53 -04001461 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_FID, &val);
1462 if (err)
1463 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001464
Vivien Didelota935c052016-09-29 12:21:53 -04001465 next.fid = val & GLOBAL_VTU_FID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001466 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001467 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1468 * VTU DBNum[3:0] are located in VTU Operation 3:0
1469 */
Vivien Didelota935c052016-09-29 12:21:53 -04001470 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_OP, &val);
1471 if (err)
1472 return err;
Vivien Didelot11ea8092016-03-31 16:53:44 -04001473
Vivien Didelota935c052016-09-29 12:21:53 -04001474 next.fid = (val & 0xf00) >> 4;
1475 next.fid |= val & 0xf;
Vivien Didelot2e7bd5e2016-03-31 16:53:41 -04001476 }
Vivien Didelotb8fee952015-08-13 12:52:19 -04001477
Vivien Didelotfad09c72016-06-21 12:28:20 -04001478 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
Vivien Didelota935c052016-09-29 12:21:53 -04001479 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_SID, &val);
1480 if (err)
1481 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001482
Vivien Didelota935c052016-09-29 12:21:53 -04001483 next.sid = val & GLOBAL_VTU_SID_MASK;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001484 }
1485 }
1486
1487 *entry = next;
1488 return 0;
1489}
1490
Vivien Didelotf81ec902016-05-09 13:22:58 -04001491static int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
1492 struct switchdev_obj_port_vlan *vlan,
1493 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001494{
Vivien Didelot04bed142016-08-31 18:06:13 -04001495 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001496 struct mv88e6xxx_vtu_entry next;
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001497 u16 pvid;
1498 int err;
1499
Vivien Didelotfad09c72016-06-21 12:28:20 -04001500 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001501 return -EOPNOTSUPP;
1502
Vivien Didelotfad09c72016-06-21 12:28:20 -04001503 mutex_lock(&chip->reg_lock);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001504
Vivien Didelot77064f32016-11-04 03:23:30 +01001505 err = mv88e6xxx_port_get_pvid(chip, port, &pvid);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001506 if (err)
1507 goto unlock;
1508
Vivien Didelotfad09c72016-06-21 12:28:20 -04001509 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001510 if (err)
1511 goto unlock;
1512
1513 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001514 err = _mv88e6xxx_vtu_getnext(chip, &next);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001515 if (err)
1516 break;
1517
1518 if (!next.valid)
1519 break;
1520
1521 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1522 continue;
1523
1524 /* reinit and dump this VLAN obj */
Vivien Didelot57d32312016-06-20 13:13:58 -04001525 vlan->vid_begin = next.vid;
1526 vlan->vid_end = next.vid;
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001527 vlan->flags = 0;
1528
1529 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1530 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1531
1532 if (next.vid == pvid)
1533 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1534
1535 err = cb(&vlan->obj);
1536 if (err)
1537 break;
1538 } while (next.vid < GLOBAL_VTU_VID_MASK);
1539
1540unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001541 mutex_unlock(&chip->reg_lock);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001542
1543 return err;
1544}
1545
Vivien Didelotfad09c72016-06-21 12:28:20 -04001546static int _mv88e6xxx_vtu_loadpurge(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001547 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001548{
Vivien Didelot11ea8092016-03-31 16:53:44 -04001549 u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001550 u16 reg = 0;
Vivien Didelota935c052016-09-29 12:21:53 -04001551 int err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001552
Vivien Didelota935c052016-09-29 12:21:53 -04001553 err = _mv88e6xxx_vtu_wait(chip);
1554 if (err)
1555 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001556
1557 if (!entry->valid)
1558 goto loadpurge;
1559
1560 /* Write port member tags */
Vivien Didelota935c052016-09-29 12:21:53 -04001561 err = mv88e6xxx_vtu_data_write(chip, entry);
1562 if (err)
1563 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001564
Vivien Didelotfad09c72016-06-21 12:28:20 -04001565 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001566 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Vivien Didelota935c052016-09-29 12:21:53 -04001567 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_SID, reg);
1568 if (err)
1569 return err;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001570 }
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001571
Vivien Didelot6dc10bb2016-09-29 12:21:55 -04001572 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G1_VTU_FID)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001573 reg = entry->fid & GLOBAL_VTU_FID_MASK;
Vivien Didelota935c052016-09-29 12:21:53 -04001574 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_FID, reg);
1575 if (err)
1576 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001577 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001578 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1579 * VTU DBNum[3:0] are located in VTU Operation 3:0
1580 */
1581 op |= (entry->fid & 0xf0) << 8;
1582 op |= entry->fid & 0xf;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001583 }
1584
1585 reg = GLOBAL_VTU_VID_VALID;
1586loadpurge:
1587 reg |= entry->vid & GLOBAL_VTU_VID_MASK;
Vivien Didelota935c052016-09-29 12:21:53 -04001588 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_VID, reg);
1589 if (err)
1590 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001591
Vivien Didelotfad09c72016-06-21 12:28:20 -04001592 return _mv88e6xxx_vtu_cmd(chip, op);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001593}
1594
Vivien Didelotfad09c72016-06-21 12:28:20 -04001595static int _mv88e6xxx_stu_getnext(struct mv88e6xxx_chip *chip, u8 sid,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001596 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001597{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001598 struct mv88e6xxx_vtu_entry next = { 0 };
Vivien Didelota935c052016-09-29 12:21:53 -04001599 u16 val;
1600 int err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001601
Vivien Didelota935c052016-09-29 12:21:53 -04001602 err = _mv88e6xxx_vtu_wait(chip);
1603 if (err)
1604 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001605
Vivien Didelota935c052016-09-29 12:21:53 -04001606 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_SID,
1607 sid & GLOBAL_VTU_SID_MASK);
1608 if (err)
1609 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001610
Vivien Didelota935c052016-09-29 12:21:53 -04001611 err = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_GET_NEXT);
1612 if (err)
1613 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001614
Vivien Didelota935c052016-09-29 12:21:53 -04001615 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_SID, &val);
1616 if (err)
1617 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001618
Vivien Didelota935c052016-09-29 12:21:53 -04001619 next.sid = val & GLOBAL_VTU_SID_MASK;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001620
Vivien Didelota935c052016-09-29 12:21:53 -04001621 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_VID, &val);
1622 if (err)
1623 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001624
Vivien Didelota935c052016-09-29 12:21:53 -04001625 next.valid = !!(val & GLOBAL_VTU_VID_VALID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001626
1627 if (next.valid) {
Vivien Didelota935c052016-09-29 12:21:53 -04001628 err = mv88e6xxx_stu_data_read(chip, &next);
1629 if (err)
1630 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001631 }
1632
1633 *entry = next;
1634 return 0;
1635}
1636
Vivien Didelotfad09c72016-06-21 12:28:20 -04001637static int _mv88e6xxx_stu_loadpurge(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001638 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001639{
1640 u16 reg = 0;
Vivien Didelota935c052016-09-29 12:21:53 -04001641 int err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001642
Vivien Didelota935c052016-09-29 12:21:53 -04001643 err = _mv88e6xxx_vtu_wait(chip);
1644 if (err)
1645 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001646
1647 if (!entry->valid)
1648 goto loadpurge;
1649
1650 /* Write port states */
Vivien Didelota935c052016-09-29 12:21:53 -04001651 err = mv88e6xxx_stu_data_write(chip, entry);
1652 if (err)
1653 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001654
1655 reg = GLOBAL_VTU_VID_VALID;
1656loadpurge:
Vivien Didelota935c052016-09-29 12:21:53 -04001657 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_VID, reg);
1658 if (err)
1659 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001660
1661 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Vivien Didelota935c052016-09-29 12:21:53 -04001662 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_SID, reg);
1663 if (err)
1664 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001665
Vivien Didelotfad09c72016-06-21 12:28:20 -04001666 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_LOAD_PURGE);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001667}
1668
Vivien Didelotfad09c72016-06-21 12:28:20 -04001669static int _mv88e6xxx_fid_new(struct mv88e6xxx_chip *chip, u16 *fid)
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001670{
1671 DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001672 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001673 int i, err;
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001674
1675 bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);
1676
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001677 /* Set every FID bit used by the (un)bridged ports */
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001678 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotb4e48c52016-11-04 03:23:29 +01001679 err = mv88e6xxx_port_get_fid(chip, i, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001680 if (err)
1681 return err;
1682
1683 set_bit(*fid, fid_bitmap);
1684 }
1685
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001686 /* Set every FID bit used by the VLAN entries */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001687 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001688 if (err)
1689 return err;
1690
1691 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001692 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001693 if (err)
1694 return err;
1695
1696 if (!vlan.valid)
1697 break;
1698
1699 set_bit(vlan.fid, fid_bitmap);
1700 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
1701
1702 /* The reset value 0x000 is used to indicate that multiple address
1703 * databases are not needed. Return the next positive available.
1704 */
1705 *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1);
Vivien Didelotfad09c72016-06-21 12:28:20 -04001706 if (unlikely(*fid >= mv88e6xxx_num_databases(chip)))
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001707 return -ENOSPC;
1708
1709 /* Clear the database */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001710 return _mv88e6xxx_atu_flush(chip, *fid, true);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001711}
1712
Vivien Didelotfad09c72016-06-21 12:28:20 -04001713static int _mv88e6xxx_vtu_new(struct mv88e6xxx_chip *chip, u16 vid,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001714 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001715{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001716 struct dsa_switch *ds = chip->ds;
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001717 struct mv88e6xxx_vtu_entry vlan = {
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001718 .valid = true,
1719 .vid = vid,
1720 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001721 int i, err;
1722
Vivien Didelotfad09c72016-06-21 12:28:20 -04001723 err = _mv88e6xxx_fid_new(chip, &vlan.fid);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001724 if (err)
1725 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001726
Vivien Didelot3d131f02015-11-03 10:52:52 -05001727 /* exclude all ports except the CPU and DSA ports */
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001728 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i)
Vivien Didelot3d131f02015-11-03 10:52:52 -05001729 vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)
1730 ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED
1731 : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001732
Vivien Didelotfad09c72016-06-21 12:28:20 -04001733 if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
1734 mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip)) {
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001735 struct mv88e6xxx_vtu_entry vstp;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001736
1737 /* Adding a VTU entry requires a valid STU entry. As VSTP is not
1738 * implemented, only one STU entry is needed to cover all VTU
1739 * entries. Thus, validate the SID 0.
1740 */
1741 vlan.sid = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001742 err = _mv88e6xxx_stu_getnext(chip, GLOBAL_VTU_SID_MASK, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001743 if (err)
1744 return err;
1745
1746 if (vstp.sid != vlan.sid || !vstp.valid) {
1747 memset(&vstp, 0, sizeof(vstp));
1748 vstp.valid = true;
1749 vstp.sid = vlan.sid;
1750
Vivien Didelotfad09c72016-06-21 12:28:20 -04001751 err = _mv88e6xxx_stu_loadpurge(chip, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001752 if (err)
1753 return err;
1754 }
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001755 }
1756
1757 *entry = vlan;
1758 return 0;
1759}
1760
Vivien Didelotfad09c72016-06-21 12:28:20 -04001761static int _mv88e6xxx_vtu_get(struct mv88e6xxx_chip *chip, u16 vid,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001762 struct mv88e6xxx_vtu_entry *entry, bool creat)
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001763{
1764 int err;
1765
1766 if (!vid)
1767 return -EINVAL;
1768
Vivien Didelotfad09c72016-06-21 12:28:20 -04001769 err = _mv88e6xxx_vtu_vid_write(chip, vid - 1);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001770 if (err)
1771 return err;
1772
Vivien Didelotfad09c72016-06-21 12:28:20 -04001773 err = _mv88e6xxx_vtu_getnext(chip, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001774 if (err)
1775 return err;
1776
1777 if (entry->vid != vid || !entry->valid) {
1778 if (!creat)
1779 return -EOPNOTSUPP;
1780 /* -ENOENT would've been more appropriate, but switchdev expects
1781 * -EOPNOTSUPP to inform bridge about an eventual software VLAN.
1782 */
1783
Vivien Didelotfad09c72016-06-21 12:28:20 -04001784 err = _mv88e6xxx_vtu_new(chip, vid, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001785 }
1786
1787 return err;
1788}
1789
Vivien Didelotda9c3592016-02-12 12:09:40 -05001790static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
1791 u16 vid_begin, u16 vid_end)
1792{
Vivien Didelot04bed142016-08-31 18:06:13 -04001793 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001794 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelotda9c3592016-02-12 12:09:40 -05001795 int i, err;
1796
1797 if (!vid_begin)
1798 return -EOPNOTSUPP;
1799
Vivien Didelotfad09c72016-06-21 12:28:20 -04001800 mutex_lock(&chip->reg_lock);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001801
Vivien Didelotfad09c72016-06-21 12:28:20 -04001802 err = _mv88e6xxx_vtu_vid_write(chip, vid_begin - 1);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001803 if (err)
1804 goto unlock;
1805
1806 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001807 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001808 if (err)
1809 goto unlock;
1810
1811 if (!vlan.valid)
1812 break;
1813
1814 if (vlan.vid > vid_end)
1815 break;
1816
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001817 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotda9c3592016-02-12 12:09:40 -05001818 if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i))
1819 continue;
1820
1821 if (vlan.data[i] ==
1822 GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1823 continue;
1824
Vivien Didelotfad09c72016-06-21 12:28:20 -04001825 if (chip->ports[i].bridge_dev ==
1826 chip->ports[port].bridge_dev)
Vivien Didelotda9c3592016-02-12 12:09:40 -05001827 break; /* same bridge, check next VLAN */
1828
Andrew Lunnc8b09802016-06-04 21:16:57 +02001829 netdev_warn(ds->ports[port].netdev,
Vivien Didelotda9c3592016-02-12 12:09:40 -05001830 "hardware VLAN %d already used by %s\n",
1831 vlan.vid,
Vivien Didelotfad09c72016-06-21 12:28:20 -04001832 netdev_name(chip->ports[i].bridge_dev));
Vivien Didelotda9c3592016-02-12 12:09:40 -05001833 err = -EOPNOTSUPP;
1834 goto unlock;
1835 }
1836 } while (vlan.vid < vid_end);
1837
1838unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001839 mutex_unlock(&chip->reg_lock);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001840
1841 return err;
1842}
1843
Vivien Didelotf81ec902016-05-09 13:22:58 -04001844static int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port,
1845 bool vlan_filtering)
Vivien Didelot214cdb92016-02-26 13:16:08 -05001846{
Vivien Didelot04bed142016-08-31 18:06:13 -04001847 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot385a0992016-11-04 03:23:31 +01001848 u16 mode = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE :
Vivien Didelot214cdb92016-02-26 13:16:08 -05001849 PORT_CONTROL_2_8021Q_DISABLED;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001850 int err;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001851
Vivien Didelotfad09c72016-06-21 12:28:20 -04001852 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001853 return -EOPNOTSUPP;
1854
Vivien Didelotfad09c72016-06-21 12:28:20 -04001855 mutex_lock(&chip->reg_lock);
Vivien Didelot385a0992016-11-04 03:23:31 +01001856 err = mv88e6xxx_port_set_8021q_mode(chip, port, mode);
Vivien Didelotfad09c72016-06-21 12:28:20 -04001857 mutex_unlock(&chip->reg_lock);
Vivien Didelot214cdb92016-02-26 13:16:08 -05001858
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001859 return err;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001860}
1861
Vivien Didelot57d32312016-06-20 13:13:58 -04001862static int
1863mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
1864 const struct switchdev_obj_port_vlan *vlan,
1865 struct switchdev_trans *trans)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001866{
Vivien Didelot04bed142016-08-31 18:06:13 -04001867 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotda9c3592016-02-12 12:09:40 -05001868 int err;
1869
Vivien Didelotfad09c72016-06-21 12:28:20 -04001870 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001871 return -EOPNOTSUPP;
1872
Vivien Didelotda9c3592016-02-12 12:09:40 -05001873 /* If the requested port doesn't belong to the same bridge as the VLAN
1874 * members, do not support it (yet) and fallback to software VLAN.
1875 */
1876 err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin,
1877 vlan->vid_end);
1878 if (err)
1879 return err;
1880
Vivien Didelot76e398a2015-11-01 12:33:55 -05001881 /* We don't need any dynamic resource from the kernel (yet),
1882 * so skip the prepare phase.
1883 */
1884 return 0;
1885}
1886
Vivien Didelotfad09c72016-06-21 12:28:20 -04001887static int _mv88e6xxx_port_vlan_add(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001888 u16 vid, bool untagged)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001889{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001890 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001891 int err;
1892
Vivien Didelotfad09c72016-06-21 12:28:20 -04001893 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, true);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001894 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001895 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001896
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001897 vlan.data[port] = untagged ?
1898 GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
1899 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
1900
Vivien Didelotfad09c72016-06-21 12:28:20 -04001901 return _mv88e6xxx_vtu_loadpurge(chip, &vlan);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001902}
1903
Vivien Didelotf81ec902016-05-09 13:22:58 -04001904static void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
1905 const struct switchdev_obj_port_vlan *vlan,
1906 struct switchdev_trans *trans)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001907{
Vivien Didelot04bed142016-08-31 18:06:13 -04001908 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001909 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1910 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1911 u16 vid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001912
Vivien Didelotfad09c72016-06-21 12:28:20 -04001913 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001914 return;
1915
Vivien Didelotfad09c72016-06-21 12:28:20 -04001916 mutex_lock(&chip->reg_lock);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001917
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001918 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
Vivien Didelotfad09c72016-06-21 12:28:20 -04001919 if (_mv88e6xxx_port_vlan_add(chip, port, vid, untagged))
Andrew Lunnc8b09802016-06-04 21:16:57 +02001920 netdev_err(ds->ports[port].netdev,
1921 "failed to add VLAN %d%c\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001922 vid, untagged ? 'u' : 't');
Vivien Didelot76e398a2015-11-01 12:33:55 -05001923
Vivien Didelot77064f32016-11-04 03:23:30 +01001924 if (pvid && mv88e6xxx_port_set_pvid(chip, port, vlan->vid_end))
Andrew Lunnc8b09802016-06-04 21:16:57 +02001925 netdev_err(ds->ports[port].netdev, "failed to set PVID %d\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001926 vlan->vid_end);
1927
Vivien Didelotfad09c72016-06-21 12:28:20 -04001928 mutex_unlock(&chip->reg_lock);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001929}
1930
Vivien Didelotfad09c72016-06-21 12:28:20 -04001931static int _mv88e6xxx_port_vlan_del(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001932 int port, u16 vid)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001933{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001934 struct dsa_switch *ds = chip->ds;
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001935 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001936 int i, err;
1937
Vivien Didelotfad09c72016-06-21 12:28:20 -04001938 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001939 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001940 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001941
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001942 /* Tell switchdev if this VLAN is handled in software */
1943 if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
Vivien Didelot3c06f082016-02-05 14:04:39 -05001944 return -EOPNOTSUPP;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001945
1946 vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
1947
1948 /* keep the VLAN unless all ports are excluded */
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001949 vlan.valid = false;
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001950 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelot3d131f02015-11-03 10:52:52 -05001951 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001952 continue;
1953
1954 if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001955 vlan.valid = true;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001956 break;
1957 }
1958 }
1959
Vivien Didelotfad09c72016-06-21 12:28:20 -04001960 err = _mv88e6xxx_vtu_loadpurge(chip, &vlan);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001961 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001962 return err;
1963
Vivien Didelotfad09c72016-06-21 12:28:20 -04001964 return _mv88e6xxx_atu_remove(chip, vlan.fid, port, false);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001965}
1966
Vivien Didelotf81ec902016-05-09 13:22:58 -04001967static int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
1968 const struct switchdev_obj_port_vlan *vlan)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001969{
Vivien Didelot04bed142016-08-31 18:06:13 -04001970 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001971 u16 pvid, vid;
1972 int err = 0;
1973
Vivien Didelotfad09c72016-06-21 12:28:20 -04001974 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001975 return -EOPNOTSUPP;
1976
Vivien Didelotfad09c72016-06-21 12:28:20 -04001977 mutex_lock(&chip->reg_lock);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001978
Vivien Didelot77064f32016-11-04 03:23:30 +01001979 err = mv88e6xxx_port_get_pvid(chip, port, &pvid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001980 if (err)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001981 goto unlock;
1982
Vivien Didelot76e398a2015-11-01 12:33:55 -05001983 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001984 err = _mv88e6xxx_port_vlan_del(chip, port, vid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001985 if (err)
1986 goto unlock;
1987
1988 if (vid == pvid) {
Vivien Didelot77064f32016-11-04 03:23:30 +01001989 err = mv88e6xxx_port_set_pvid(chip, port, 0);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001990 if (err)
1991 goto unlock;
1992 }
1993 }
1994
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001995unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001996 mutex_unlock(&chip->reg_lock);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001997
1998 return err;
1999}
2000
Vivien Didelotfad09c72016-06-21 12:28:20 -04002001static int _mv88e6xxx_atu_mac_write(struct mv88e6xxx_chip *chip,
Vivien Didelotc5723ac2015-08-10 09:09:48 -04002002 const unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002003{
Vivien Didelota935c052016-09-29 12:21:53 -04002004 int i, err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002005
2006 for (i = 0; i < 3; i++) {
Vivien Didelota935c052016-09-29 12:21:53 -04002007 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_MAC_01 + i,
2008 (addr[i * 2] << 8) | addr[i * 2 + 1]);
2009 if (err)
2010 return err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002011 }
2012
2013 return 0;
2014}
2015
Vivien Didelotfad09c72016-06-21 12:28:20 -04002016static int _mv88e6xxx_atu_mac_read(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04002017 unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002018{
Vivien Didelota935c052016-09-29 12:21:53 -04002019 u16 val;
2020 int i, err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002021
2022 for (i = 0; i < 3; i++) {
Vivien Didelota935c052016-09-29 12:21:53 -04002023 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_MAC_01 + i, &val);
2024 if (err)
2025 return err;
2026
2027 addr[i * 2] = val >> 8;
2028 addr[i * 2 + 1] = val & 0xff;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002029 }
2030
2031 return 0;
2032}
2033
Vivien Didelotfad09c72016-06-21 12:28:20 -04002034static int _mv88e6xxx_atu_load(struct mv88e6xxx_chip *chip,
Vivien Didelotfd231c82015-08-10 09:09:50 -04002035 struct mv88e6xxx_atu_entry *entry)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002036{
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002037 int ret;
2038
Vivien Didelotfad09c72016-06-21 12:28:20 -04002039 ret = _mv88e6xxx_atu_wait(chip);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002040 if (ret < 0)
2041 return ret;
2042
Vivien Didelotfad09c72016-06-21 12:28:20 -04002043 ret = _mv88e6xxx_atu_mac_write(chip, entry->mac);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002044 if (ret < 0)
2045 return ret;
2046
Vivien Didelotfad09c72016-06-21 12:28:20 -04002047 ret = _mv88e6xxx_atu_data_write(chip, entry);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002048 if (ret < 0)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002049 return ret;
2050
Vivien Didelotfad09c72016-06-21 12:28:20 -04002051 return _mv88e6xxx_atu_cmd(chip, entry->fid, GLOBAL_ATU_OP_LOAD_DB);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002052}
David S. Millercdf09692015-08-11 12:00:37 -07002053
Vivien Didelot88472932016-09-19 19:56:11 -04002054static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
2055 struct mv88e6xxx_atu_entry *entry);
2056
2057static int mv88e6xxx_atu_get(struct mv88e6xxx_chip *chip, int fid,
2058 const u8 *addr, struct mv88e6xxx_atu_entry *entry)
2059{
2060 struct mv88e6xxx_atu_entry next;
2061 int err;
2062
2063 eth_broadcast_addr(next.mac);
2064
2065 err = _mv88e6xxx_atu_mac_write(chip, next.mac);
2066 if (err)
2067 return err;
2068
2069 do {
2070 err = _mv88e6xxx_atu_getnext(chip, fid, &next);
2071 if (err)
2072 return err;
2073
2074 if (next.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2075 break;
2076
2077 if (ether_addr_equal(next.mac, addr)) {
2078 *entry = next;
2079 return 0;
2080 }
2081 } while (!is_broadcast_ether_addr(next.mac));
2082
2083 memset(entry, 0, sizeof(*entry));
2084 entry->fid = fid;
2085 ether_addr_copy(entry->mac, addr);
2086
2087 return 0;
2088}
2089
Vivien Didelot83dabd12016-08-31 11:50:04 -04002090static int mv88e6xxx_port_db_load_purge(struct mv88e6xxx_chip *chip, int port,
2091 const unsigned char *addr, u16 vid,
2092 u8 state)
Vivien Didelotfd231c82015-08-10 09:09:50 -04002093{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04002094 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelot88472932016-09-19 19:56:11 -04002095 struct mv88e6xxx_atu_entry entry;
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002096 int err;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002097
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002098 /* Null VLAN ID corresponds to the port private database */
2099 if (vid == 0)
Vivien Didelotb4e48c52016-11-04 03:23:29 +01002100 err = mv88e6xxx_port_get_fid(chip, port, &vlan.fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002101 else
Vivien Didelotfad09c72016-06-21 12:28:20 -04002102 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002103 if (err)
2104 return err;
2105
Vivien Didelot88472932016-09-19 19:56:11 -04002106 err = mv88e6xxx_atu_get(chip, vlan.fid, addr, &entry);
2107 if (err)
2108 return err;
2109
2110 /* Purge the ATU entry only if no port is using it anymore */
2111 if (state == GLOBAL_ATU_DATA_STATE_UNUSED) {
2112 entry.portv_trunkid &= ~BIT(port);
2113 if (!entry.portv_trunkid)
2114 entry.state = GLOBAL_ATU_DATA_STATE_UNUSED;
2115 } else {
2116 entry.portv_trunkid |= BIT(port);
2117 entry.state = state;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002118 }
2119
Vivien Didelotfad09c72016-06-21 12:28:20 -04002120 return _mv88e6xxx_atu_load(chip, &entry);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002121}
2122
Vivien Didelotf81ec902016-05-09 13:22:58 -04002123static int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
2124 const struct switchdev_obj_port_fdb *fdb,
2125 struct switchdev_trans *trans)
Vivien Didelot146a3202015-10-08 11:35:12 -04002126{
2127 /* We don't need any dynamic resource from the kernel (yet),
2128 * so skip the prepare phase.
2129 */
2130 return 0;
2131}
2132
Vivien Didelotf81ec902016-05-09 13:22:58 -04002133static void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
2134 const struct switchdev_obj_port_fdb *fdb,
2135 struct switchdev_trans *trans)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002136{
Vivien Didelot04bed142016-08-31 18:06:13 -04002137 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot6630e232015-08-06 01:44:07 -04002138
Vivien Didelotfad09c72016-06-21 12:28:20 -04002139 mutex_lock(&chip->reg_lock);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002140 if (mv88e6xxx_port_db_load_purge(chip, port, fdb->addr, fdb->vid,
2141 GLOBAL_ATU_DATA_STATE_UC_STATIC))
2142 netdev_err(ds->ports[port].netdev, "failed to load unicast MAC address\n");
Vivien Didelotfad09c72016-06-21 12:28:20 -04002143 mutex_unlock(&chip->reg_lock);
David S. Millercdf09692015-08-11 12:00:37 -07002144}
2145
Vivien Didelotf81ec902016-05-09 13:22:58 -04002146static int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
2147 const struct switchdev_obj_port_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -07002148{
Vivien Didelot04bed142016-08-31 18:06:13 -04002149 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot83dabd12016-08-31 11:50:04 -04002150 int err;
David S. Millercdf09692015-08-11 12:00:37 -07002151
Vivien Didelotfad09c72016-06-21 12:28:20 -04002152 mutex_lock(&chip->reg_lock);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002153 err = mv88e6xxx_port_db_load_purge(chip, port, fdb->addr, fdb->vid,
2154 GLOBAL_ATU_DATA_STATE_UNUSED);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002155 mutex_unlock(&chip->reg_lock);
David S. Millercdf09692015-08-11 12:00:37 -07002156
Vivien Didelot83dabd12016-08-31 11:50:04 -04002157 return err;
David S. Millercdf09692015-08-11 12:00:37 -07002158}
2159
Vivien Didelotfad09c72016-06-21 12:28:20 -04002160static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
Vivien Didelot1d194042015-08-10 09:09:51 -04002161 struct mv88e6xxx_atu_entry *entry)
David S. Millercdf09692015-08-11 12:00:37 -07002162{
Vivien Didelot1d194042015-08-10 09:09:51 -04002163 struct mv88e6xxx_atu_entry next = { 0 };
Vivien Didelota935c052016-09-29 12:21:53 -04002164 u16 val;
2165 int err;
Vivien Didelot1d194042015-08-10 09:09:51 -04002166
2167 next.fid = fid;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002168
Vivien Didelota935c052016-09-29 12:21:53 -04002169 err = _mv88e6xxx_atu_wait(chip);
2170 if (err)
2171 return err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002172
Vivien Didelota935c052016-09-29 12:21:53 -04002173 err = _mv88e6xxx_atu_cmd(chip, fid, GLOBAL_ATU_OP_GET_NEXT_DB);
2174 if (err)
2175 return err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002176
Vivien Didelota935c052016-09-29 12:21:53 -04002177 err = _mv88e6xxx_atu_mac_read(chip, next.mac);
2178 if (err)
2179 return err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002180
Vivien Didelota935c052016-09-29 12:21:53 -04002181 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_DATA, &val);
2182 if (err)
2183 return err;
Vivien Didelot1d194042015-08-10 09:09:51 -04002184
Vivien Didelota935c052016-09-29 12:21:53 -04002185 next.state = val & GLOBAL_ATU_DATA_STATE_MASK;
Vivien Didelot1d194042015-08-10 09:09:51 -04002186 if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2187 unsigned int mask, shift;
2188
Vivien Didelota935c052016-09-29 12:21:53 -04002189 if (val & GLOBAL_ATU_DATA_TRUNK) {
Vivien Didelot1d194042015-08-10 09:09:51 -04002190 next.trunk = true;
2191 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
2192 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
2193 } else {
2194 next.trunk = false;
2195 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
2196 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
2197 }
2198
Vivien Didelota935c052016-09-29 12:21:53 -04002199 next.portv_trunkid = (val & mask) >> shift;
Vivien Didelot1d194042015-08-10 09:09:51 -04002200 }
2201
2202 *entry = next;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002203 return 0;
2204}
2205
Vivien Didelot83dabd12016-08-31 11:50:04 -04002206static int mv88e6xxx_port_db_dump_fid(struct mv88e6xxx_chip *chip,
2207 u16 fid, u16 vid, int port,
2208 struct switchdev_obj *obj,
2209 int (*cb)(struct switchdev_obj *obj))
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002210{
2211 struct mv88e6xxx_atu_entry addr = {
2212 .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2213 };
2214 int err;
2215
Vivien Didelotfad09c72016-06-21 12:28:20 -04002216 err = _mv88e6xxx_atu_mac_write(chip, addr.mac);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002217 if (err)
2218 return err;
2219
2220 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002221 err = _mv88e6xxx_atu_getnext(chip, fid, &addr);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002222 if (err)
Vivien Didelot83dabd12016-08-31 11:50:04 -04002223 return err;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002224
2225 if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2226 break;
2227
Vivien Didelot83dabd12016-08-31 11:50:04 -04002228 if (addr.trunk || (addr.portv_trunkid & BIT(port)) == 0)
2229 continue;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002230
Vivien Didelot83dabd12016-08-31 11:50:04 -04002231 if (obj->id == SWITCHDEV_OBJ_ID_PORT_FDB) {
2232 struct switchdev_obj_port_fdb *fdb;
2233
2234 if (!is_unicast_ether_addr(addr.mac))
2235 continue;
2236
2237 fdb = SWITCHDEV_OBJ_PORT_FDB(obj);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002238 fdb->vid = vid;
2239 ether_addr_copy(fdb->addr, addr.mac);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002240 if (addr.state == GLOBAL_ATU_DATA_STATE_UC_STATIC)
2241 fdb->ndm_state = NUD_NOARP;
2242 else
2243 fdb->ndm_state = NUD_REACHABLE;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04002244 } else if (obj->id == SWITCHDEV_OBJ_ID_PORT_MDB) {
2245 struct switchdev_obj_port_mdb *mdb;
2246
2247 if (!is_multicast_ether_addr(addr.mac))
2248 continue;
2249
2250 mdb = SWITCHDEV_OBJ_PORT_MDB(obj);
2251 mdb->vid = vid;
2252 ether_addr_copy(mdb->addr, addr.mac);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002253 } else {
2254 return -EOPNOTSUPP;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002255 }
Vivien Didelot83dabd12016-08-31 11:50:04 -04002256
2257 err = cb(obj);
2258 if (err)
2259 return err;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002260 } while (!is_broadcast_ether_addr(addr.mac));
2261
2262 return err;
2263}
2264
Vivien Didelot83dabd12016-08-31 11:50:04 -04002265static int mv88e6xxx_port_db_dump(struct mv88e6xxx_chip *chip, int port,
2266 struct switchdev_obj *obj,
2267 int (*cb)(struct switchdev_obj *obj))
2268{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04002269 struct mv88e6xxx_vtu_entry vlan = {
Vivien Didelot83dabd12016-08-31 11:50:04 -04002270 .vid = GLOBAL_VTU_VID_MASK, /* all ones */
2271 };
2272 u16 fid;
2273 int err;
2274
2275 /* Dump port's default Filtering Information Database (VLAN ID 0) */
Vivien Didelotb4e48c52016-11-04 03:23:29 +01002276 err = mv88e6xxx_port_get_fid(chip, port, &fid);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002277 if (err)
2278 return err;
2279
2280 err = mv88e6xxx_port_db_dump_fid(chip, fid, 0, port, obj, cb);
2281 if (err)
2282 return err;
2283
2284 /* Dump VLANs' Filtering Information Databases */
2285 err = _mv88e6xxx_vtu_vid_write(chip, vlan.vid);
2286 if (err)
2287 return err;
2288
2289 do {
2290 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
2291 if (err)
2292 return err;
2293
2294 if (!vlan.valid)
2295 break;
2296
2297 err = mv88e6xxx_port_db_dump_fid(chip, vlan.fid, vlan.vid, port,
2298 obj, cb);
2299 if (err)
2300 return err;
2301 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
2302
2303 return err;
2304}
2305
Vivien Didelotf81ec902016-05-09 13:22:58 -04002306static int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
2307 struct switchdev_obj_port_fdb *fdb,
2308 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotf33475b2015-10-22 09:34:41 -04002309{
Vivien Didelot04bed142016-08-31 18:06:13 -04002310 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002311 int err;
2312
Vivien Didelotfad09c72016-06-21 12:28:20 -04002313 mutex_lock(&chip->reg_lock);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002314 err = mv88e6xxx_port_db_dump(chip, port, &fdb->obj, cb);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002315 mutex_unlock(&chip->reg_lock);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002316
2317 return err;
2318}
2319
Vivien Didelotf81ec902016-05-09 13:22:58 -04002320static int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
2321 struct net_device *bridge)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002322{
Vivien Didelot04bed142016-08-31 18:06:13 -04002323 struct mv88e6xxx_chip *chip = ds->priv;
Colin Ian King1d9619d2016-04-25 23:11:22 +01002324 int i, err = 0;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002325
Vivien Didelotfad09c72016-06-21 12:28:20 -04002326 mutex_lock(&chip->reg_lock);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002327
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002328 /* Assign the bridge and remap each port's VLANTable */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002329 chip->ports[port].bridge_dev = bridge;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002330
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002331 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002332 if (chip->ports[i].bridge_dev == bridge) {
2333 err = _mv88e6xxx_port_based_vlan_map(chip, i);
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002334 if (err)
2335 break;
2336 }
2337 }
2338
Vivien Didelotfad09c72016-06-21 12:28:20 -04002339 mutex_unlock(&chip->reg_lock);
Vivien Didelota6692752016-02-12 12:09:39 -05002340
Vivien Didelot466dfa02016-02-26 13:16:05 -05002341 return err;
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002342}
2343
Vivien Didelotf81ec902016-05-09 13:22:58 -04002344static void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002345{
Vivien Didelot04bed142016-08-31 18:06:13 -04002346 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002347 struct net_device *bridge = chip->ports[port].bridge_dev;
Vivien Didelot16bfa702016-03-13 16:21:33 -04002348 int i;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002349
Vivien Didelotfad09c72016-06-21 12:28:20 -04002350 mutex_lock(&chip->reg_lock);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002351
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002352 /* Unassign the bridge and remap each port's VLANTable */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002353 chip->ports[port].bridge_dev = NULL;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002354
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002355 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002356 if (i == port || chip->ports[i].bridge_dev == bridge)
2357 if (_mv88e6xxx_port_based_vlan_map(chip, i))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002358 netdev_warn(ds->ports[i].netdev,
2359 "failed to remap\n");
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002360
Vivien Didelotfad09c72016-06-21 12:28:20 -04002361 mutex_unlock(&chip->reg_lock);
Vivien Didelot66d9cd02016-02-05 14:07:14 -05002362}
2363
Vivien Didelotfad09c72016-06-21 12:28:20 -04002364static int mv88e6xxx_switch_reset(struct mv88e6xxx_chip *chip)
Vivien Didelot552238b2016-05-09 13:22:49 -04002365{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002366 bool ppu_active = mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE);
Vivien Didelot552238b2016-05-09 13:22:49 -04002367 u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002368 struct gpio_desc *gpiod = chip->reset;
Vivien Didelot552238b2016-05-09 13:22:49 -04002369 unsigned long timeout;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002370 u16 reg;
Vivien Didelota935c052016-09-29 12:21:53 -04002371 int err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002372 int i;
2373
2374 /* Set all ports to the disabled state. */
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002375 for (i = 0; i < mv88e6xxx_num_ports(chip); i++) {
Vivien Didelote28def332016-11-04 03:23:27 +01002376 err = mv88e6xxx_port_set_state(chip, i,
2377 PORT_CONTROL_STATE_DISABLED);
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002378 if (err)
2379 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002380 }
2381
2382 /* Wait for transmit queues to drain. */
2383 usleep_range(2000, 4000);
2384
2385 /* If there is a gpio connected to the reset pin, toggle it */
2386 if (gpiod) {
2387 gpiod_set_value_cansleep(gpiod, 1);
2388 usleep_range(10000, 20000);
2389 gpiod_set_value_cansleep(gpiod, 0);
2390 usleep_range(10000, 20000);
2391 }
2392
2393 /* Reset the switch. Keep the PPU active if requested. The PPU
2394 * needs to be active to support indirect phy register access
2395 * through global registers 0x18 and 0x19.
2396 */
2397 if (ppu_active)
Vivien Didelota935c052016-09-29 12:21:53 -04002398 err = mv88e6xxx_g1_write(chip, 0x04, 0xc000);
Vivien Didelot552238b2016-05-09 13:22:49 -04002399 else
Vivien Didelota935c052016-09-29 12:21:53 -04002400 err = mv88e6xxx_g1_write(chip, 0x04, 0xc400);
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002401 if (err)
2402 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002403
2404 /* Wait up to one second for reset to complete. */
2405 timeout = jiffies + 1 * HZ;
2406 while (time_before(jiffies, timeout)) {
Vivien Didelota935c052016-09-29 12:21:53 -04002407 err = mv88e6xxx_g1_read(chip, 0x00, &reg);
2408 if (err)
2409 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002410
Vivien Didelota935c052016-09-29 12:21:53 -04002411 if ((reg & is_reset) == is_reset)
Vivien Didelot552238b2016-05-09 13:22:49 -04002412 break;
2413 usleep_range(1000, 2000);
2414 }
2415 if (time_after(jiffies, timeout))
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002416 err = -ETIMEDOUT;
Vivien Didelot552238b2016-05-09 13:22:49 -04002417 else
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002418 err = 0;
Vivien Didelot552238b2016-05-09 13:22:49 -04002419
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002420 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002421}
2422
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002423static int mv88e6xxx_serdes_power_on(struct mv88e6xxx_chip *chip)
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002424{
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002425 u16 val;
2426 int err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002427
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002428 /* Clear Power Down bit */
2429 err = mv88e6xxx_serdes_read(chip, MII_BMCR, &val);
2430 if (err)
2431 return err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002432
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002433 if (val & BMCR_PDOWN) {
2434 val &= ~BMCR_PDOWN;
2435 err = mv88e6xxx_serdes_write(chip, MII_BMCR, val);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002436 }
2437
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002438 return err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002439}
2440
Vivien Didelotfad09c72016-06-21 12:28:20 -04002441static int mv88e6xxx_setup_port(struct mv88e6xxx_chip *chip, int port)
Guenter Roeckd827e882015-03-26 18:36:29 -07002442{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002443 struct dsa_switch *ds = chip->ds;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002444 int err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002445 u16 reg;
Guenter Roeckd827e882015-03-26 18:36:29 -07002446
Vivien Didelotd78343d2016-11-04 03:23:36 +01002447 /* MAC Forcing register: don't force link, speed, duplex or flow control
2448 * state to any particular values on physical ports, but force the CPU
2449 * port and all DSA ports to their maximum bandwidth and full duplex.
2450 */
2451 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))
2452 err = mv88e6xxx_port_setup_mac(chip, port, LINK_FORCED_UP,
2453 SPEED_MAX, DUPLEX_FULL,
2454 PHY_INTERFACE_MODE_NA);
2455 else
2456 err = mv88e6xxx_port_setup_mac(chip, port, LINK_UNFORCED,
2457 SPEED_UNFORCED, DUPLEX_UNFORCED,
2458 PHY_INTERFACE_MODE_NA);
2459 if (err)
2460 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002461
2462 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
2463 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
2464 * tunneling, determine priority by looking at 802.1p and IP
2465 * priority fields (IP prio has precedence), and set STP state
2466 * to Forwarding.
2467 *
2468 * If this is the CPU link, use DSA or EDSA tagging depending
2469 * on which tagging mode was configured.
2470 *
2471 * If this is a link to another switch, use DSA tagging mode.
2472 *
2473 * If this is the upstream port for this switch, enable
2474 * forwarding of unknown unicasts and multicasts.
2475 */
2476 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002477 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2478 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2479 mv88e6xxx_6095_family(chip) || mv88e6xxx_6065_family(chip) ||
2480 mv88e6xxx_6185_family(chip) || mv88e6xxx_6320_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002481 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
2482 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
2483 PORT_CONTROL_STATE_FORWARDING;
2484 if (dsa_is_cpu_port(ds, port)) {
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002485 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA))
Andrew Lunn5377b802016-06-04 21:17:02 +02002486 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA |
Andrew Lunnc047a1f2015-09-29 01:50:56 +02002487 PORT_CONTROL_FORWARD_UNKNOWN_MC;
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002488 else
2489 reg |= PORT_CONTROL_DSA_TAG;
Jamie Lentinf027e0c2016-08-22 16:01:04 +02002490 reg |= PORT_CONTROL_EGRESS_ADD_TAG |
2491 PORT_CONTROL_FORWARD_UNKNOWN;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002492 }
Andrew Lunn6083ce72015-08-17 23:52:52 +02002493 if (dsa_is_dsa_port(ds, port)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002494 if (mv88e6xxx_6095_family(chip) ||
2495 mv88e6xxx_6185_family(chip))
Andrew Lunn6083ce72015-08-17 23:52:52 +02002496 reg |= PORT_CONTROL_DSA_TAG;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002497 if (mv88e6xxx_6352_family(chip) ||
2498 mv88e6xxx_6351_family(chip) ||
2499 mv88e6xxx_6165_family(chip) ||
2500 mv88e6xxx_6097_family(chip) ||
2501 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002502 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunn6083ce72015-08-17 23:52:52 +02002503 }
2504
Andrew Lunn54d792f2015-05-06 01:09:47 +02002505 if (port == dsa_upstream_port(ds))
2506 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2507 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2508 }
2509 if (reg) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002510 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL, reg);
2511 if (err)
2512 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002513 }
2514
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002515 /* If this port is connected to a SerDes, make sure the SerDes is not
2516 * powered down.
2517 */
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002518 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_SERDES)) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002519 err = mv88e6xxx_port_read(chip, port, PORT_STATUS, &reg);
2520 if (err)
2521 return err;
2522 reg &= PORT_STATUS_CMODE_MASK;
2523 if ((reg == PORT_STATUS_CMODE_100BASE_X) ||
2524 (reg == PORT_STATUS_CMODE_1000BASE_X) ||
2525 (reg == PORT_STATUS_CMODE_SGMII)) {
2526 err = mv88e6xxx_serdes_power_on(chip);
2527 if (err < 0)
2528 return err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002529 }
2530 }
2531
Vivien Didelot8efdda42015-08-13 12:52:23 -04002532 /* Port Control 2: don't force a good FCS, set the maximum frame size to
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002533 * 10240 bytes, disable 802.1q tags checking, don't discard tagged or
Vivien Didelot8efdda42015-08-13 12:52:23 -04002534 * untagged frames on this port, do a destination address lookup on all
2535 * received packets as usual, disable ARP mirroring and don't send a
2536 * copy of all transmitted/received frames on this port to the CPU.
Andrew Lunn54d792f2015-05-06 01:09:47 +02002537 */
2538 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002539 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2540 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2541 mv88e6xxx_6095_family(chip) || mv88e6xxx_6320_family(chip) ||
2542 mv88e6xxx_6185_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002543 reg = PORT_CONTROL_2_MAP_DA;
2544
Vivien Didelotfad09c72016-06-21 12:28:20 -04002545 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2546 mv88e6xxx_6165_family(chip) || mv88e6xxx_6320_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002547 reg |= PORT_CONTROL_2_JUMBO_10240;
2548
Vivien Didelotfad09c72016-06-21 12:28:20 -04002549 if (mv88e6xxx_6095_family(chip) || mv88e6xxx_6185_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002550 /* Set the upstream port this port should use */
2551 reg |= dsa_upstream_port(ds);
2552 /* enable forwarding of unknown multicast addresses to
2553 * the upstream port
2554 */
2555 if (port == dsa_upstream_port(ds))
2556 reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
2557 }
2558
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002559 reg |= PORT_CONTROL_2_8021Q_DISABLED;
Vivien Didelot8efdda42015-08-13 12:52:23 -04002560
Andrew Lunn54d792f2015-05-06 01:09:47 +02002561 if (reg) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002562 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_2, reg);
2563 if (err)
2564 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002565 }
2566
2567 /* Port Association Vector: when learning source addresses
2568 * of packets, add the address to the address database using
2569 * a port bitmap that has only the bit for this port set and
2570 * the other bits clear.
2571 */
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002572 reg = 1 << port;
Vivien Didelot996ecb82016-04-14 14:42:08 -04002573 /* Disable learning for CPU port */
2574 if (dsa_is_cpu_port(ds, port))
Vivien Didelot65fa4022016-04-14 14:42:07 -04002575 reg = 0;
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002576
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002577 err = mv88e6xxx_port_write(chip, port, PORT_ASSOC_VECTOR, reg);
2578 if (err)
2579 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002580
2581 /* Egress rate control 2: disable egress rate control. */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002582 err = mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL_2, 0x0000);
2583 if (err)
2584 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002585
Vivien Didelotfad09c72016-06-21 12:28:20 -04002586 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2587 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2588 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002589 /* Do not limit the period of time that this port can
2590 * be paused for by the remote end or the period of
2591 * time that this port can pause the remote end.
2592 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002593 err = mv88e6xxx_port_write(chip, port, PORT_PAUSE_CTRL, 0x0000);
2594 if (err)
2595 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002596
2597 /* Port ATU control: disable limiting the number of
2598 * address database entries that this port is allowed
2599 * to use.
2600 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002601 err = mv88e6xxx_port_write(chip, port, PORT_ATU_CONTROL,
2602 0x0000);
Andrew Lunn54d792f2015-05-06 01:09:47 +02002603 /* Priority Override: disable DA, SA and VTU priority
2604 * override.
2605 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002606 err = mv88e6xxx_port_write(chip, port, PORT_PRI_OVERRIDE,
2607 0x0000);
2608 if (err)
2609 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002610
2611 /* Port Ethertype: use the Ethertype DSA Ethertype
2612 * value.
2613 */
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002614 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA)) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002615 err = mv88e6xxx_port_write(chip, port, PORT_ETH_TYPE,
2616 ETH_P_EDSA);
2617 if (err)
2618 return err;
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002619 }
2620
Andrew Lunn54d792f2015-05-06 01:09:47 +02002621 /* Tag Remap: use an identity 802.1p prio -> switch
2622 * prio mapping.
2623 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002624 err = mv88e6xxx_port_write(chip, port, PORT_TAG_REGMAP_0123,
2625 0x3210);
2626 if (err)
2627 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002628
2629 /* Tag Remap 2: use an identity 802.1p prio -> switch
2630 * prio mapping.
2631 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002632 err = mv88e6xxx_port_write(chip, port, PORT_TAG_REGMAP_4567,
2633 0x7654);
2634 if (err)
2635 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002636 }
2637
Jamie Lentin1bc261f2016-08-22 22:47:08 +01002638 /* Rate Control: disable ingress rate limiting. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002639 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2640 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
Vivien Didelotfad09c72016-06-21 12:28:20 -04002641 mv88e6xxx_6320_family(chip)) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002642 err = mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL,
2643 0x0001);
2644 if (err)
2645 return err;
Jamie Lentin1bc261f2016-08-22 22:47:08 +01002646 } else if (mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip)) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002647 err = mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL,
2648 0x0000);
2649 if (err)
2650 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002651 }
2652
Guenter Roeck366f0a02015-03-26 18:36:30 -07002653 /* Port Control 1: disable trunking, disable sending
2654 * learning messages to this port.
Guenter Roeckd827e882015-03-26 18:36:29 -07002655 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002656 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_1, 0x0000);
2657 if (err)
2658 return err;
Guenter Roeckd827e882015-03-26 18:36:29 -07002659
Vivien Didelot207afda2016-04-14 14:42:09 -04002660 /* Port based VLAN map: give each port the same default address
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002661 * database, and allow bidirectional communication between the
2662 * CPU and DSA port(s), and the other ports.
Guenter Roeckd827e882015-03-26 18:36:29 -07002663 */
Vivien Didelotb4e48c52016-11-04 03:23:29 +01002664 err = mv88e6xxx_port_set_fid(chip, port, 0);
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002665 if (err)
2666 return err;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002667
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002668 err = _mv88e6xxx_port_based_vlan_map(chip, port);
2669 if (err)
2670 return err;
Guenter Roeckd827e882015-03-26 18:36:29 -07002671
2672 /* Default VLAN ID and priority: don't set a default VLAN
2673 * ID, and set the default packet priority to zero.
2674 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002675 return mv88e6xxx_port_write(chip, port, PORT_DEFAULT_VLAN, 0x0000);
Andrew Lunndbde9e62015-05-06 01:09:48 +02002676}
2677
Wei Yongjunaa0938c2016-10-18 15:53:37 +00002678static int mv88e6xxx_g1_set_switch_mac(struct mv88e6xxx_chip *chip, u8 *addr)
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002679{
2680 int err;
2681
Vivien Didelota935c052016-09-29 12:21:53 -04002682 err = mv88e6xxx_g1_write(chip, GLOBAL_MAC_01, (addr[0] << 8) | addr[1]);
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002683 if (err)
2684 return err;
2685
Vivien Didelota935c052016-09-29 12:21:53 -04002686 err = mv88e6xxx_g1_write(chip, GLOBAL_MAC_23, (addr[2] << 8) | addr[3]);
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002687 if (err)
2688 return err;
2689
Vivien Didelota935c052016-09-29 12:21:53 -04002690 err = mv88e6xxx_g1_write(chip, GLOBAL_MAC_45, (addr[4] << 8) | addr[5]);
2691 if (err)
2692 return err;
2693
2694 return 0;
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002695}
2696
Vivien Didelotacddbd22016-07-18 20:45:39 -04002697static int mv88e6xxx_g1_set_age_time(struct mv88e6xxx_chip *chip,
2698 unsigned int msecs)
2699{
2700 const unsigned int coeff = chip->info->age_time_coeff;
2701 const unsigned int min = 0x01 * coeff;
2702 const unsigned int max = 0xff * coeff;
2703 u8 age_time;
2704 u16 val;
2705 int err;
2706
2707 if (msecs < min || msecs > max)
2708 return -ERANGE;
2709
2710 /* Round to nearest multiple of coeff */
2711 age_time = (msecs + coeff / 2) / coeff;
2712
Vivien Didelota935c052016-09-29 12:21:53 -04002713 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_CONTROL, &val);
Vivien Didelotacddbd22016-07-18 20:45:39 -04002714 if (err)
2715 return err;
2716
2717 /* AgeTime is 11:4 bits */
2718 val &= ~0xff0;
2719 val |= age_time << 4;
2720
Vivien Didelota935c052016-09-29 12:21:53 -04002721 return mv88e6xxx_g1_write(chip, GLOBAL_ATU_CONTROL, val);
Vivien Didelotacddbd22016-07-18 20:45:39 -04002722}
2723
Vivien Didelot2cfcd962016-07-18 20:45:40 -04002724static int mv88e6xxx_set_ageing_time(struct dsa_switch *ds,
2725 unsigned int ageing_time)
2726{
Vivien Didelot04bed142016-08-31 18:06:13 -04002727 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot2cfcd962016-07-18 20:45:40 -04002728 int err;
2729
2730 mutex_lock(&chip->reg_lock);
2731 err = mv88e6xxx_g1_set_age_time(chip, ageing_time);
2732 mutex_unlock(&chip->reg_lock);
2733
2734 return err;
2735}
2736
Vivien Didelot97299342016-07-18 20:45:30 -04002737static int mv88e6xxx_g1_setup(struct mv88e6xxx_chip *chip)
Vivien Didelot08a01262016-05-09 13:22:50 -04002738{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002739 struct dsa_switch *ds = chip->ds;
Vivien Didelotb0745e872016-05-09 13:22:53 -04002740 u32 upstream_port = dsa_upstream_port(ds);
Vivien Didelot119477b2016-05-09 13:22:51 -04002741 u16 reg;
Vivien Didelot08a01262016-05-09 13:22:50 -04002742 int err;
Vivien Didelot08a01262016-05-09 13:22:50 -04002743
Vivien Didelot119477b2016-05-09 13:22:51 -04002744 /* Enable the PHY Polling Unit if present, don't discard any packets,
2745 * and mask all interrupt sources.
2746 */
Andrew Lunndc30c352016-10-16 19:56:49 +02002747 err = mv88e6xxx_g1_read(chip, GLOBAL_CONTROL, &reg);
2748 if (err < 0)
2749 return err;
2750
2751 reg &= ~GLOBAL_CONTROL_PPU_ENABLE;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002752 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU) ||
2753 mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE))
Vivien Didelot119477b2016-05-09 13:22:51 -04002754 reg |= GLOBAL_CONTROL_PPU_ENABLE;
2755
Vivien Didelota935c052016-09-29 12:21:53 -04002756 err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL, reg);
Vivien Didelot119477b2016-05-09 13:22:51 -04002757 if (err)
2758 return err;
2759
Vivien Didelotb0745e872016-05-09 13:22:53 -04002760 /* Configure the upstream port, and configure it as the port to which
2761 * ingress and egress and ARP monitor frames are to be sent.
2762 */
2763 reg = upstream_port << GLOBAL_MONITOR_CONTROL_INGRESS_SHIFT |
2764 upstream_port << GLOBAL_MONITOR_CONTROL_EGRESS_SHIFT |
2765 upstream_port << GLOBAL_MONITOR_CONTROL_ARP_SHIFT;
Vivien Didelota935c052016-09-29 12:21:53 -04002766 err = mv88e6xxx_g1_write(chip, GLOBAL_MONITOR_CONTROL, reg);
Vivien Didelotb0745e872016-05-09 13:22:53 -04002767 if (err)
2768 return err;
2769
Vivien Didelot50484ff2016-05-09 13:22:54 -04002770 /* Disable remote management, and set the switch's DSA device number. */
Vivien Didelota935c052016-09-29 12:21:53 -04002771 err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL_2,
2772 GLOBAL_CONTROL_2_MULTIPLE_CASCADE |
2773 (ds->index & 0x1f));
Vivien Didelot50484ff2016-05-09 13:22:54 -04002774 if (err)
2775 return err;
2776
Vivien Didelotacddbd22016-07-18 20:45:39 -04002777 /* Clear all the VTU and STU entries */
2778 err = _mv88e6xxx_vtu_stu_flush(chip);
2779 if (err < 0)
2780 return err;
2781
Vivien Didelot08a01262016-05-09 13:22:50 -04002782 /* Set the default address aging time to 5 minutes, and
2783 * enable address learn messages to be sent to all message
2784 * ports.
2785 */
Vivien Didelota935c052016-09-29 12:21:53 -04002786 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_CONTROL,
2787 GLOBAL_ATU_CONTROL_LEARN2ALL);
Vivien Didelot08a01262016-05-09 13:22:50 -04002788 if (err)
2789 return err;
2790
Vivien Didelotacddbd22016-07-18 20:45:39 -04002791 err = mv88e6xxx_g1_set_age_time(chip, 300000);
2792 if (err)
Vivien Didelot97299342016-07-18 20:45:30 -04002793 return err;
2794
2795 /* Clear all ATU entries */
2796 err = _mv88e6xxx_atu_flush(chip, 0, true);
2797 if (err)
2798 return err;
2799
Vivien Didelot08a01262016-05-09 13:22:50 -04002800 /* Configure the IP ToS mapping registers. */
Vivien Didelota935c052016-09-29 12:21:53 -04002801 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_0, 0x0000);
Vivien Didelot08a01262016-05-09 13:22:50 -04002802 if (err)
2803 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002804 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_1, 0x0000);
Vivien Didelot08a01262016-05-09 13:22:50 -04002805 if (err)
2806 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002807 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_2, 0x5555);
Vivien Didelot08a01262016-05-09 13:22:50 -04002808 if (err)
2809 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002810 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_3, 0x5555);
Vivien Didelot08a01262016-05-09 13:22:50 -04002811 if (err)
2812 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002813 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_4, 0xaaaa);
Vivien Didelot08a01262016-05-09 13:22:50 -04002814 if (err)
2815 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002816 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_5, 0xaaaa);
Vivien Didelot08a01262016-05-09 13:22:50 -04002817 if (err)
2818 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002819 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_6, 0xffff);
Vivien Didelot08a01262016-05-09 13:22:50 -04002820 if (err)
2821 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002822 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_7, 0xffff);
Vivien Didelot08a01262016-05-09 13:22:50 -04002823 if (err)
2824 return err;
2825
2826 /* Configure the IEEE 802.1p priority mapping register. */
Vivien Didelota935c052016-09-29 12:21:53 -04002827 err = mv88e6xxx_g1_write(chip, GLOBAL_IEEE_PRI, 0xfa41);
Vivien Didelot08a01262016-05-09 13:22:50 -04002828 if (err)
2829 return err;
2830
Andrew Lunnde2273872016-11-21 23:27:01 +01002831 /* Initialize the statistics unit */
2832 err = mv88e6xxx_stats_set_histogram(chip);
2833 if (err)
2834 return err;
2835
Vivien Didelot97299342016-07-18 20:45:30 -04002836 /* Clear the statistics counters for all ports */
Vivien Didelota935c052016-09-29 12:21:53 -04002837 err = mv88e6xxx_g1_write(chip, GLOBAL_STATS_OP,
2838 GLOBAL_STATS_OP_FLUSH_ALL);
Vivien Didelot97299342016-07-18 20:45:30 -04002839 if (err)
2840 return err;
2841
2842 /* Wait for the flush to complete. */
Andrew Lunn7f9ef3a2016-11-21 23:27:05 +01002843 err = mv88e6xxx_g1_stats_wait(chip);
Vivien Didelot97299342016-07-18 20:45:30 -04002844 if (err)
2845 return err;
2846
2847 return 0;
2848}
2849
Vivien Didelotf81ec902016-05-09 13:22:58 -04002850static int mv88e6xxx_setup(struct dsa_switch *ds)
Guenter Roeckacdaffc2015-03-26 18:36:28 -07002851{
Vivien Didelot04bed142016-08-31 18:06:13 -04002852 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot552238b2016-05-09 13:22:49 -04002853 int err;
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002854 int i;
2855
Vivien Didelotfad09c72016-06-21 12:28:20 -04002856 chip->ds = ds;
2857 ds->slave_mii_bus = chip->mdio_bus;
Vivien Didelot552238b2016-05-09 13:22:49 -04002858
Vivien Didelotfad09c72016-06-21 12:28:20 -04002859 mutex_lock(&chip->reg_lock);
Vivien Didelot552238b2016-05-09 13:22:49 -04002860
Vivien Didelot97299342016-07-18 20:45:30 -04002861 /* Setup Switch Port Registers */
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002862 for (i = 0; i < mv88e6xxx_num_ports(chip); i++) {
Vivien Didelot97299342016-07-18 20:45:30 -04002863 err = mv88e6xxx_setup_port(chip, i);
2864 if (err)
2865 goto unlock;
2866 }
2867
2868 /* Setup Switch Global 1 Registers */
2869 err = mv88e6xxx_g1_setup(chip);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002870 if (err)
2871 goto unlock;
2872
Vivien Didelot97299342016-07-18 20:45:30 -04002873 /* Setup Switch Global 2 Registers */
2874 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_GLOBAL2)) {
2875 err = mv88e6xxx_g2_setup(chip);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002876 if (err)
2877 goto unlock;
2878 }
Andrew Lunn54d792f2015-05-06 01:09:47 +02002879
Vivien Didelot6b17e862015-08-13 12:52:18 -04002880unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002881 mutex_unlock(&chip->reg_lock);
Andrew Lunndb687a52015-06-20 21:31:29 +02002882
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002883 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002884}
2885
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002886static int mv88e6xxx_set_addr(struct dsa_switch *ds, u8 *addr)
2887{
Vivien Didelot04bed142016-08-31 18:06:13 -04002888 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002889 int err;
2890
Vivien Didelotb073d4e2016-09-29 12:22:01 -04002891 if (!chip->info->ops->set_switch_mac)
2892 return -EOPNOTSUPP;
2893
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002894 mutex_lock(&chip->reg_lock);
Vivien Didelotb073d4e2016-09-29 12:22:01 -04002895 err = chip->info->ops->set_switch_mac(chip, addr);
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002896 mutex_unlock(&chip->reg_lock);
2897
2898 return err;
2899}
2900
Vivien Didelote57e5e72016-08-15 17:19:00 -04002901static int mv88e6xxx_mdio_read(struct mii_bus *bus, int phy, int reg)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002902{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002903 struct mv88e6xxx_chip *chip = bus->priv;
Vivien Didelote57e5e72016-08-15 17:19:00 -04002904 u16 val;
2905 int err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002906
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002907 if (phy >= mv88e6xxx_num_ports(chip))
Andrew Lunn158bc062016-04-28 21:24:06 -04002908 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002909
Vivien Didelotfad09c72016-06-21 12:28:20 -04002910 mutex_lock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04002911 err = mv88e6xxx_phy_read(chip, phy, reg, &val);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002912 mutex_unlock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04002913
2914 return err ? err : val;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002915}
2916
Vivien Didelote57e5e72016-08-15 17:19:00 -04002917static int mv88e6xxx_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002918{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002919 struct mv88e6xxx_chip *chip = bus->priv;
Vivien Didelote57e5e72016-08-15 17:19:00 -04002920 int err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002921
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002922 if (phy >= mv88e6xxx_num_ports(chip))
Andrew Lunn158bc062016-04-28 21:24:06 -04002923 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002924
Vivien Didelotfad09c72016-06-21 12:28:20 -04002925 mutex_lock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04002926 err = mv88e6xxx_phy_write(chip, phy, reg, val);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002927 mutex_unlock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04002928
2929 return err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002930}
2931
Vivien Didelotfad09c72016-06-21 12:28:20 -04002932static int mv88e6xxx_mdio_register(struct mv88e6xxx_chip *chip,
Andrew Lunnb516d452016-06-04 21:17:06 +02002933 struct device_node *np)
2934{
2935 static int index;
2936 struct mii_bus *bus;
2937 int err;
2938
Andrew Lunnb516d452016-06-04 21:17:06 +02002939 if (np)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002940 chip->mdio_np = of_get_child_by_name(np, "mdio");
Andrew Lunnb516d452016-06-04 21:17:06 +02002941
Vivien Didelotfad09c72016-06-21 12:28:20 -04002942 bus = devm_mdiobus_alloc(chip->dev);
Andrew Lunnb516d452016-06-04 21:17:06 +02002943 if (!bus)
2944 return -ENOMEM;
2945
Vivien Didelotfad09c72016-06-21 12:28:20 -04002946 bus->priv = (void *)chip;
Andrew Lunnb516d452016-06-04 21:17:06 +02002947 if (np) {
2948 bus->name = np->full_name;
2949 snprintf(bus->id, MII_BUS_ID_SIZE, "%s", np->full_name);
2950 } else {
2951 bus->name = "mv88e6xxx SMI";
2952 snprintf(bus->id, MII_BUS_ID_SIZE, "mv88e6xxx-%d", index++);
2953 }
2954
2955 bus->read = mv88e6xxx_mdio_read;
2956 bus->write = mv88e6xxx_mdio_write;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002957 bus->parent = chip->dev;
Andrew Lunnb516d452016-06-04 21:17:06 +02002958
Vivien Didelotfad09c72016-06-21 12:28:20 -04002959 if (chip->mdio_np)
2960 err = of_mdiobus_register(bus, chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02002961 else
2962 err = mdiobus_register(bus);
2963 if (err) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002964 dev_err(chip->dev, "Cannot register MDIO bus (%d)\n", err);
Andrew Lunnb516d452016-06-04 21:17:06 +02002965 goto out;
2966 }
Vivien Didelotfad09c72016-06-21 12:28:20 -04002967 chip->mdio_bus = bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02002968
2969 return 0;
2970
2971out:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002972 if (chip->mdio_np)
2973 of_node_put(chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02002974
2975 return err;
2976}
2977
Vivien Didelotfad09c72016-06-21 12:28:20 -04002978static void mv88e6xxx_mdio_unregister(struct mv88e6xxx_chip *chip)
Andrew Lunnb516d452016-06-04 21:17:06 +02002979
2980{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002981 struct mii_bus *bus = chip->mdio_bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02002982
2983 mdiobus_unregister(bus);
2984
Vivien Didelotfad09c72016-06-21 12:28:20 -04002985 if (chip->mdio_np)
2986 of_node_put(chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02002987}
2988
Guenter Roeckc22995c2015-07-25 09:42:28 -07002989#ifdef CONFIG_NET_DSA_HWMON
2990
2991static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
2992{
Vivien Didelot04bed142016-08-31 18:06:13 -04002993 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot9c938292016-08-15 17:19:02 -04002994 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07002995 int ret;
Guenter Roeckc22995c2015-07-25 09:42:28 -07002996
2997 *temp = 0;
2998
Vivien Didelotfad09c72016-06-21 12:28:20 -04002999 mutex_lock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003000
Vivien Didelot9c938292016-08-15 17:19:02 -04003001 ret = mv88e6xxx_phy_write(chip, 0x0, 0x16, 0x6);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003002 if (ret < 0)
3003 goto error;
3004
3005 /* Enable temperature sensor */
Vivien Didelot9c938292016-08-15 17:19:02 -04003006 ret = mv88e6xxx_phy_read(chip, 0x0, 0x1a, &val);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003007 if (ret < 0)
3008 goto error;
3009
Vivien Didelot9c938292016-08-15 17:19:02 -04003010 ret = mv88e6xxx_phy_write(chip, 0x0, 0x1a, val | (1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003011 if (ret < 0)
3012 goto error;
3013
3014 /* Wait for temperature to stabilize */
3015 usleep_range(10000, 12000);
3016
Vivien Didelot9c938292016-08-15 17:19:02 -04003017 ret = mv88e6xxx_phy_read(chip, 0x0, 0x1a, &val);
3018 if (ret < 0)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003019 goto error;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003020
3021 /* Disable temperature sensor */
Vivien Didelot9c938292016-08-15 17:19:02 -04003022 ret = mv88e6xxx_phy_write(chip, 0x0, 0x1a, val & ~(1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003023 if (ret < 0)
3024 goto error;
3025
3026 *temp = ((val & 0x1f) - 5) * 5;
3027
3028error:
Vivien Didelot9c938292016-08-15 17:19:02 -04003029 mv88e6xxx_phy_write(chip, 0x0, 0x16, 0x0);
Vivien Didelotfad09c72016-06-21 12:28:20 -04003030 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003031 return ret;
3032}
3033
3034static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
3035{
Vivien Didelot04bed142016-08-31 18:06:13 -04003036 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003037 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003038 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003039 int ret;
3040
3041 *temp = 0;
3042
Vivien Didelot9c938292016-08-15 17:19:02 -04003043 mutex_lock(&chip->reg_lock);
3044 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 27, &val);
3045 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003046 if (ret < 0)
3047 return ret;
3048
Vivien Didelot9c938292016-08-15 17:19:02 -04003049 *temp = (val & 0xff) - 25;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003050
3051 return 0;
3052}
3053
Vivien Didelotf81ec902016-05-09 13:22:58 -04003054static int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003055{
Vivien Didelot04bed142016-08-31 18:06:13 -04003056 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn158bc062016-04-28 21:24:06 -04003057
Vivien Didelotfad09c72016-06-21 12:28:20 -04003058 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP))
Vivien Didelot6594f612016-05-09 13:22:42 -04003059 return -EOPNOTSUPP;
3060
Vivien Didelotfad09c72016-06-21 12:28:20 -04003061 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003062 return mv88e63xx_get_temp(ds, temp);
3063
3064 return mv88e61xx_get_temp(ds, temp);
3065}
3066
Vivien Didelotf81ec902016-05-09 13:22:58 -04003067static int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003068{
Vivien Didelot04bed142016-08-31 18:06:13 -04003069 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003070 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003071 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003072 int ret;
3073
Vivien Didelotfad09c72016-06-21 12:28:20 -04003074 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003075 return -EOPNOTSUPP;
3076
3077 *temp = 0;
3078
Vivien Didelot9c938292016-08-15 17:19:02 -04003079 mutex_lock(&chip->reg_lock);
3080 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3081 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003082 if (ret < 0)
3083 return ret;
3084
Vivien Didelot9c938292016-08-15 17:19:02 -04003085 *temp = (((val >> 8) & 0x1f) * 5) - 25;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003086
3087 return 0;
3088}
3089
Vivien Didelotf81ec902016-05-09 13:22:58 -04003090static int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003091{
Vivien Didelot04bed142016-08-31 18:06:13 -04003092 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003093 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003094 u16 val;
3095 int err;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003096
Vivien Didelotfad09c72016-06-21 12:28:20 -04003097 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003098 return -EOPNOTSUPP;
3099
Vivien Didelot9c938292016-08-15 17:19:02 -04003100 mutex_lock(&chip->reg_lock);
3101 err = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3102 if (err)
3103 goto unlock;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003104 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
Vivien Didelot9c938292016-08-15 17:19:02 -04003105 err = mv88e6xxx_phy_page_write(chip, phy, 6, 26,
3106 (val & 0xe0ff) | (temp << 8));
3107unlock:
3108 mutex_unlock(&chip->reg_lock);
3109
3110 return err;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003111}
3112
Vivien Didelotf81ec902016-05-09 13:22:58 -04003113static int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003114{
Vivien Didelot04bed142016-08-31 18:06:13 -04003115 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003116 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003117 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003118 int ret;
3119
Vivien Didelotfad09c72016-06-21 12:28:20 -04003120 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003121 return -EOPNOTSUPP;
3122
3123 *alarm = false;
3124
Vivien Didelot9c938292016-08-15 17:19:02 -04003125 mutex_lock(&chip->reg_lock);
3126 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3127 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003128 if (ret < 0)
3129 return ret;
3130
Vivien Didelot9c938292016-08-15 17:19:02 -04003131 *alarm = !!(val & 0x40);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003132
3133 return 0;
3134}
3135#endif /* CONFIG_NET_DSA_HWMON */
3136
Vivien Didelot855b1932016-07-20 18:18:35 -04003137static int mv88e6xxx_get_eeprom_len(struct dsa_switch *ds)
3138{
Vivien Didelot04bed142016-08-31 18:06:13 -04003139 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot855b1932016-07-20 18:18:35 -04003140
3141 return chip->eeprom_len;
3142}
3143
Vivien Didelot855b1932016-07-20 18:18:35 -04003144static int mv88e6xxx_get_eeprom(struct dsa_switch *ds,
3145 struct ethtool_eeprom *eeprom, u8 *data)
3146{
Vivien Didelot04bed142016-08-31 18:06:13 -04003147 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot855b1932016-07-20 18:18:35 -04003148 int err;
3149
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003150 if (!chip->info->ops->get_eeprom)
3151 return -EOPNOTSUPP;
3152
Vivien Didelot855b1932016-07-20 18:18:35 -04003153 mutex_lock(&chip->reg_lock);
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003154 err = chip->info->ops->get_eeprom(chip, eeprom, data);
Vivien Didelot855b1932016-07-20 18:18:35 -04003155 mutex_unlock(&chip->reg_lock);
3156
3157 if (err)
3158 return err;
3159
3160 eeprom->magic = 0xc3ec4951;
3161
3162 return 0;
3163}
3164
Vivien Didelot855b1932016-07-20 18:18:35 -04003165static int mv88e6xxx_set_eeprom(struct dsa_switch *ds,
3166 struct ethtool_eeprom *eeprom, u8 *data)
3167{
Vivien Didelot04bed142016-08-31 18:06:13 -04003168 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot855b1932016-07-20 18:18:35 -04003169 int err;
3170
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003171 if (!chip->info->ops->set_eeprom)
3172 return -EOPNOTSUPP;
3173
Vivien Didelot855b1932016-07-20 18:18:35 -04003174 if (eeprom->magic != 0xc3ec4951)
3175 return -EINVAL;
3176
3177 mutex_lock(&chip->reg_lock);
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003178 err = chip->info->ops->set_eeprom(chip, eeprom, data);
Vivien Didelot855b1932016-07-20 18:18:35 -04003179 mutex_unlock(&chip->reg_lock);
3180
3181 return err;
3182}
3183
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003184static const struct mv88e6xxx_ops mv88e6085_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003185 /* MV88E6XXX_FAMILY_6097 */
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003186 .set_switch_mac = mv88e6xxx_g1_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003187 .phy_read = mv88e6xxx_phy_ppu_read,
3188 .phy_write = mv88e6xxx_phy_ppu_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003189 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003190 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003191 .port_set_speed = mv88e6185_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003192 .stats_snapshot = mv88e6xxx_g1_stats_snapshot,
Andrew Lunndfafe442016-11-21 23:27:02 +01003193 .stats_get_sset_count = mv88e6095_stats_get_sset_count,
3194 .stats_get_strings = mv88e6095_stats_get_strings,
Andrew Lunn052f9472016-11-21 23:27:03 +01003195 .stats_get_stats = mv88e6095_stats_get_stats,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003196};
3197
3198static const struct mv88e6xxx_ops mv88e6095_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003199 /* MV88E6XXX_FAMILY_6095 */
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003200 .set_switch_mac = mv88e6xxx_g1_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003201 .phy_read = mv88e6xxx_phy_ppu_read,
3202 .phy_write = mv88e6xxx_phy_ppu_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003203 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003204 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003205 .port_set_speed = mv88e6185_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003206 .stats_snapshot = mv88e6xxx_g1_stats_snapshot,
Andrew Lunndfafe442016-11-21 23:27:02 +01003207 .stats_get_sset_count = mv88e6095_stats_get_sset_count,
3208 .stats_get_strings = mv88e6095_stats_get_strings,
Andrew Lunn052f9472016-11-21 23:27:03 +01003209 .stats_get_stats = mv88e6095_stats_get_stats,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003210};
3211
Stefan Eichenberger7d381a02016-11-22 17:47:21 +01003212static const struct mv88e6xxx_ops mv88e6097_ops = {
Stefan Eichenberger15da3cc2016-11-25 09:41:30 +01003213 /* MV88E6XXX_FAMILY_6097 */
Stefan Eichenberger7d381a02016-11-22 17:47:21 +01003214 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
3215 .phy_read = mv88e6xxx_g2_smi_phy_read,
3216 .phy_write = mv88e6xxx_g2_smi_phy_write,
3217 .port_set_link = mv88e6xxx_port_set_link,
3218 .port_set_duplex = mv88e6xxx_port_set_duplex,
3219 .port_set_speed = mv88e6185_port_set_speed,
3220 .stats_snapshot = mv88e6xxx_g1_stats_snapshot,
3221 .stats_get_sset_count = mv88e6095_stats_get_sset_count,
3222 .stats_get_strings = mv88e6095_stats_get_strings,
3223 .stats_get_stats = mv88e6095_stats_get_stats,
3224};
3225
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003226static const struct mv88e6xxx_ops mv88e6123_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003227 /* MV88E6XXX_FAMILY_6165 */
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003228 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003229 .phy_read = mv88e6xxx_read,
3230 .phy_write = mv88e6xxx_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003231 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003232 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003233 .port_set_speed = mv88e6185_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003234 .stats_snapshot = mv88e6xxx_g1_stats_snapshot,
Andrew Lunndfafe442016-11-21 23:27:02 +01003235 .stats_get_sset_count = mv88e6095_stats_get_sset_count,
3236 .stats_get_strings = mv88e6095_stats_get_strings,
Andrew Lunn052f9472016-11-21 23:27:03 +01003237 .stats_get_stats = mv88e6095_stats_get_stats,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003238};
3239
3240static const struct mv88e6xxx_ops mv88e6131_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003241 /* MV88E6XXX_FAMILY_6185 */
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003242 .set_switch_mac = mv88e6xxx_g1_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003243 .phy_read = mv88e6xxx_phy_ppu_read,
3244 .phy_write = mv88e6xxx_phy_ppu_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003245 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003246 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003247 .port_set_speed = mv88e6185_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003248 .stats_snapshot = mv88e6xxx_g1_stats_snapshot,
Andrew Lunndfafe442016-11-21 23:27:02 +01003249 .stats_get_sset_count = mv88e6095_stats_get_sset_count,
3250 .stats_get_strings = mv88e6095_stats_get_strings,
Andrew Lunn052f9472016-11-21 23:27:03 +01003251 .stats_get_stats = mv88e6095_stats_get_stats,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003252};
3253
3254static const struct mv88e6xxx_ops mv88e6161_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003255 /* MV88E6XXX_FAMILY_6165 */
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003256 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003257 .phy_read = mv88e6xxx_read,
3258 .phy_write = mv88e6xxx_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003259 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003260 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003261 .port_set_speed = mv88e6185_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003262 .stats_snapshot = mv88e6xxx_g1_stats_snapshot,
Andrew Lunndfafe442016-11-21 23:27:02 +01003263 .stats_get_sset_count = mv88e6095_stats_get_sset_count,
3264 .stats_get_strings = mv88e6095_stats_get_strings,
Andrew Lunn052f9472016-11-21 23:27:03 +01003265 .stats_get_stats = mv88e6095_stats_get_stats,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003266};
3267
3268static const struct mv88e6xxx_ops mv88e6165_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003269 /* MV88E6XXX_FAMILY_6165 */
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003270 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003271 .phy_read = mv88e6xxx_read,
3272 .phy_write = mv88e6xxx_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003273 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003274 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003275 .port_set_speed = mv88e6185_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003276 .stats_snapshot = mv88e6xxx_g1_stats_snapshot,
Andrew Lunndfafe442016-11-21 23:27:02 +01003277 .stats_get_sset_count = mv88e6095_stats_get_sset_count,
3278 .stats_get_strings = mv88e6095_stats_get_strings,
Andrew Lunn052f9472016-11-21 23:27:03 +01003279 .stats_get_stats = mv88e6095_stats_get_stats,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003280};
3281
3282static const struct mv88e6xxx_ops mv88e6171_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003283 /* MV88E6XXX_FAMILY_6351 */
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003284 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003285 .phy_read = mv88e6xxx_g2_smi_phy_read,
3286 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003287 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003288 .port_set_duplex = mv88e6xxx_port_set_duplex,
Andrew Lunn94d66ae2016-11-10 15:44:01 +01003289 .port_set_rgmii_delay = mv88e6352_port_set_rgmii_delay,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003290 .port_set_speed = mv88e6185_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003291 .stats_snapshot = mv88e6320_g1_stats_snapshot,
Andrew Lunndfafe442016-11-21 23:27:02 +01003292 .stats_get_sset_count = mv88e6095_stats_get_sset_count,
3293 .stats_get_strings = mv88e6095_stats_get_strings,
Andrew Lunn052f9472016-11-21 23:27:03 +01003294 .stats_get_stats = mv88e6095_stats_get_stats,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003295};
3296
3297static const struct mv88e6xxx_ops mv88e6172_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003298 /* MV88E6XXX_FAMILY_6352 */
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003299 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3300 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003301 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003302 .phy_read = mv88e6xxx_g2_smi_phy_read,
3303 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003304 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003305 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelota0a0f622016-11-04 03:23:34 +01003306 .port_set_rgmii_delay = mv88e6352_port_set_rgmii_delay,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003307 .port_set_speed = mv88e6352_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003308 .stats_snapshot = mv88e6320_g1_stats_snapshot,
Andrew Lunndfafe442016-11-21 23:27:02 +01003309 .stats_get_sset_count = mv88e6095_stats_get_sset_count,
3310 .stats_get_strings = mv88e6095_stats_get_strings,
Andrew Lunn052f9472016-11-21 23:27:03 +01003311 .stats_get_stats = mv88e6095_stats_get_stats,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003312};
3313
3314static const struct mv88e6xxx_ops mv88e6175_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003315 /* MV88E6XXX_FAMILY_6351 */
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003316 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003317 .phy_read = mv88e6xxx_g2_smi_phy_read,
3318 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003319 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003320 .port_set_duplex = mv88e6xxx_port_set_duplex,
Andrew Lunn94d66ae2016-11-10 15:44:01 +01003321 .port_set_rgmii_delay = mv88e6352_port_set_rgmii_delay,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003322 .port_set_speed = mv88e6185_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003323 .stats_snapshot = mv88e6320_g1_stats_snapshot,
Andrew Lunndfafe442016-11-21 23:27:02 +01003324 .stats_get_sset_count = mv88e6095_stats_get_sset_count,
3325 .stats_get_strings = mv88e6095_stats_get_strings,
Andrew Lunn052f9472016-11-21 23:27:03 +01003326 .stats_get_stats = mv88e6095_stats_get_stats,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003327};
3328
3329static const struct mv88e6xxx_ops mv88e6176_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003330 /* MV88E6XXX_FAMILY_6352 */
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003331 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3332 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003333 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003334 .phy_read = mv88e6xxx_g2_smi_phy_read,
3335 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003336 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003337 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelota0a0f622016-11-04 03:23:34 +01003338 .port_set_rgmii_delay = mv88e6352_port_set_rgmii_delay,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003339 .port_set_speed = mv88e6352_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003340 .stats_snapshot = mv88e6320_g1_stats_snapshot,
Andrew Lunndfafe442016-11-21 23:27:02 +01003341 .stats_get_sset_count = mv88e6095_stats_get_sset_count,
3342 .stats_get_strings = mv88e6095_stats_get_strings,
Andrew Lunn052f9472016-11-21 23:27:03 +01003343 .stats_get_stats = mv88e6095_stats_get_stats,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003344};
3345
3346static const struct mv88e6xxx_ops mv88e6185_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003347 /* MV88E6XXX_FAMILY_6185 */
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003348 .set_switch_mac = mv88e6xxx_g1_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003349 .phy_read = mv88e6xxx_phy_ppu_read,
3350 .phy_write = mv88e6xxx_phy_ppu_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003351 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003352 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003353 .port_set_speed = mv88e6185_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003354 .stats_snapshot = mv88e6xxx_g1_stats_snapshot,
Andrew Lunndfafe442016-11-21 23:27:02 +01003355 .stats_get_sset_count = mv88e6095_stats_get_sset_count,
3356 .stats_get_strings = mv88e6095_stats_get_strings,
Andrew Lunn052f9472016-11-21 23:27:03 +01003357 .stats_get_stats = mv88e6095_stats_get_stats,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003358};
3359
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003360static const struct mv88e6xxx_ops mv88e6190_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003361 /* MV88E6XXX_FAMILY_6390 */
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003362 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
3363 .phy_read = mv88e6xxx_g2_smi_phy_read,
3364 .phy_write = mv88e6xxx_g2_smi_phy_write,
3365 .port_set_link = mv88e6xxx_port_set_link,
3366 .port_set_duplex = mv88e6xxx_port_set_duplex,
3367 .port_set_rgmii_delay = mv88e6390_port_set_rgmii_delay,
3368 .port_set_speed = mv88e6390_port_set_speed,
Andrew Lunn79523472016-11-21 23:27:00 +01003369 .stats_snapshot = mv88e6390_g1_stats_snapshot,
Andrew Lunnde2273872016-11-21 23:27:01 +01003370 .stats_set_histogram = mv88e6390_g1_stats_set_histogram,
Andrew Lunndfafe442016-11-21 23:27:02 +01003371 .stats_get_sset_count = mv88e6320_stats_get_sset_count,
3372 .stats_get_strings = mv88e6320_stats_get_strings,
Andrew Lunne0d8b612016-11-21 23:27:04 +01003373 .stats_get_stats = mv88e6390_stats_get_stats,
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003374};
3375
3376static const struct mv88e6xxx_ops mv88e6190x_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003377 /* MV88E6XXX_FAMILY_6390 */
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003378 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
3379 .phy_read = mv88e6xxx_g2_smi_phy_read,
3380 .phy_write = mv88e6xxx_g2_smi_phy_write,
3381 .port_set_link = mv88e6xxx_port_set_link,
3382 .port_set_duplex = mv88e6xxx_port_set_duplex,
3383 .port_set_rgmii_delay = mv88e6390_port_set_rgmii_delay,
3384 .port_set_speed = mv88e6390x_port_set_speed,
Andrew Lunn79523472016-11-21 23:27:00 +01003385 .stats_snapshot = mv88e6390_g1_stats_snapshot,
Andrew Lunnde2273872016-11-21 23:27:01 +01003386 .stats_set_histogram = mv88e6390_g1_stats_set_histogram,
Andrew Lunndfafe442016-11-21 23:27:02 +01003387 .stats_get_sset_count = mv88e6320_stats_get_sset_count,
3388 .stats_get_strings = mv88e6320_stats_get_strings,
Andrew Lunne0d8b612016-11-21 23:27:04 +01003389 .stats_get_stats = mv88e6390_stats_get_stats,
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003390};
3391
3392static const struct mv88e6xxx_ops mv88e6191_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003393 /* MV88E6XXX_FAMILY_6390 */
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003394 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
3395 .phy_read = mv88e6xxx_g2_smi_phy_read,
3396 .phy_write = mv88e6xxx_g2_smi_phy_write,
3397 .port_set_link = mv88e6xxx_port_set_link,
3398 .port_set_duplex = mv88e6xxx_port_set_duplex,
3399 .port_set_rgmii_delay = mv88e6390_port_set_rgmii_delay,
3400 .port_set_speed = mv88e6390_port_set_speed,
Andrew Lunn79523472016-11-21 23:27:00 +01003401 .stats_snapshot = mv88e6390_g1_stats_snapshot,
Andrew Lunnde2273872016-11-21 23:27:01 +01003402 .stats_set_histogram = mv88e6390_g1_stats_set_histogram,
Andrew Lunndfafe442016-11-21 23:27:02 +01003403 .stats_get_sset_count = mv88e6320_stats_get_sset_count,
3404 .stats_get_strings = mv88e6320_stats_get_strings,
Andrew Lunne0d8b612016-11-21 23:27:04 +01003405 .stats_get_stats = mv88e6390_stats_get_stats,
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003406};
3407
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003408static const struct mv88e6xxx_ops mv88e6240_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003409 /* MV88E6XXX_FAMILY_6352 */
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003410 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3411 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003412 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003413 .phy_read = mv88e6xxx_g2_smi_phy_read,
3414 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003415 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003416 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelota0a0f622016-11-04 03:23:34 +01003417 .port_set_rgmii_delay = mv88e6352_port_set_rgmii_delay,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003418 .port_set_speed = mv88e6352_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003419 .stats_snapshot = mv88e6320_g1_stats_snapshot,
Andrew Lunndfafe442016-11-21 23:27:02 +01003420 .stats_get_sset_count = mv88e6095_stats_get_sset_count,
3421 .stats_get_strings = mv88e6095_stats_get_strings,
Andrew Lunn052f9472016-11-21 23:27:03 +01003422 .stats_get_stats = mv88e6095_stats_get_stats,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003423};
3424
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003425static const struct mv88e6xxx_ops mv88e6290_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003426 /* MV88E6XXX_FAMILY_6390 */
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003427 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
3428 .phy_read = mv88e6xxx_g2_smi_phy_read,
3429 .phy_write = mv88e6xxx_g2_smi_phy_write,
3430 .port_set_link = mv88e6xxx_port_set_link,
3431 .port_set_duplex = mv88e6xxx_port_set_duplex,
3432 .port_set_rgmii_delay = mv88e6390_port_set_rgmii_delay,
3433 .port_set_speed = mv88e6390_port_set_speed,
Andrew Lunn79523472016-11-21 23:27:00 +01003434 .stats_snapshot = mv88e6390_g1_stats_snapshot,
Andrew Lunnde2273872016-11-21 23:27:01 +01003435 .stats_set_histogram = mv88e6390_g1_stats_set_histogram,
Andrew Lunndfafe442016-11-21 23:27:02 +01003436 .stats_get_sset_count = mv88e6320_stats_get_sset_count,
3437 .stats_get_strings = mv88e6320_stats_get_strings,
Andrew Lunne0d8b612016-11-21 23:27:04 +01003438 .stats_get_stats = mv88e6390_stats_get_stats,
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003439};
3440
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003441static const struct mv88e6xxx_ops mv88e6320_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003442 /* MV88E6XXX_FAMILY_6320 */
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003443 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3444 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003445 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003446 .phy_read = mv88e6xxx_g2_smi_phy_read,
3447 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003448 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003449 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003450 .port_set_speed = mv88e6185_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003451 .stats_snapshot = mv88e6320_g1_stats_snapshot,
Andrew Lunndfafe442016-11-21 23:27:02 +01003452 .stats_get_sset_count = mv88e6320_stats_get_sset_count,
3453 .stats_get_strings = mv88e6320_stats_get_strings,
Andrew Lunn052f9472016-11-21 23:27:03 +01003454 .stats_get_stats = mv88e6320_stats_get_stats,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003455};
3456
3457static const struct mv88e6xxx_ops mv88e6321_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003458 /* MV88E6XXX_FAMILY_6321 */
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003459 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3460 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003461 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003462 .phy_read = mv88e6xxx_g2_smi_phy_read,
3463 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003464 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003465 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003466 .port_set_speed = mv88e6185_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003467 .stats_snapshot = mv88e6320_g1_stats_snapshot,
Andrew Lunndfafe442016-11-21 23:27:02 +01003468 .stats_get_sset_count = mv88e6320_stats_get_sset_count,
3469 .stats_get_strings = mv88e6320_stats_get_strings,
Andrew Lunn052f9472016-11-21 23:27:03 +01003470 .stats_get_stats = mv88e6320_stats_get_stats,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003471};
3472
3473static const struct mv88e6xxx_ops mv88e6350_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003474 /* MV88E6XXX_FAMILY_6351 */
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003475 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003476 .phy_read = mv88e6xxx_g2_smi_phy_read,
3477 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003478 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003479 .port_set_duplex = mv88e6xxx_port_set_duplex,
Andrew Lunn94d66ae2016-11-10 15:44:01 +01003480 .port_set_rgmii_delay = mv88e6352_port_set_rgmii_delay,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003481 .port_set_speed = mv88e6185_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003482 .stats_snapshot = mv88e6320_g1_stats_snapshot,
Andrew Lunndfafe442016-11-21 23:27:02 +01003483 .stats_get_sset_count = mv88e6095_stats_get_sset_count,
3484 .stats_get_strings = mv88e6095_stats_get_strings,
Andrew Lunn052f9472016-11-21 23:27:03 +01003485 .stats_get_stats = mv88e6095_stats_get_stats,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003486};
3487
3488static const struct mv88e6xxx_ops mv88e6351_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003489 /* MV88E6XXX_FAMILY_6351 */
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003490 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003491 .phy_read = mv88e6xxx_g2_smi_phy_read,
3492 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003493 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003494 .port_set_duplex = mv88e6xxx_port_set_duplex,
Andrew Lunn94d66ae2016-11-10 15:44:01 +01003495 .port_set_rgmii_delay = mv88e6352_port_set_rgmii_delay,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003496 .port_set_speed = mv88e6185_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003497 .stats_snapshot = mv88e6320_g1_stats_snapshot,
Andrew Lunndfafe442016-11-21 23:27:02 +01003498 .stats_get_sset_count = mv88e6095_stats_get_sset_count,
3499 .stats_get_strings = mv88e6095_stats_get_strings,
Andrew Lunn052f9472016-11-21 23:27:03 +01003500 .stats_get_stats = mv88e6095_stats_get_stats,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003501};
3502
3503static const struct mv88e6xxx_ops mv88e6352_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003504 /* MV88E6XXX_FAMILY_6352 */
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003505 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3506 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003507 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003508 .phy_read = mv88e6xxx_g2_smi_phy_read,
3509 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003510 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003511 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelota0a0f622016-11-04 03:23:34 +01003512 .port_set_rgmii_delay = mv88e6352_port_set_rgmii_delay,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003513 .port_set_speed = mv88e6352_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003514 .stats_snapshot = mv88e6320_g1_stats_snapshot,
Andrew Lunndfafe442016-11-21 23:27:02 +01003515 .stats_get_sset_count = mv88e6095_stats_get_sset_count,
3516 .stats_get_strings = mv88e6095_stats_get_strings,
Andrew Lunn052f9472016-11-21 23:27:03 +01003517 .stats_get_stats = mv88e6095_stats_get_stats,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003518};
3519
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003520static const struct mv88e6xxx_ops mv88e6390_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003521 /* MV88E6XXX_FAMILY_6390 */
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003522 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
3523 .phy_read = mv88e6xxx_g2_smi_phy_read,
3524 .phy_write = mv88e6xxx_g2_smi_phy_write,
3525 .port_set_link = mv88e6xxx_port_set_link,
3526 .port_set_duplex = mv88e6xxx_port_set_duplex,
3527 .port_set_rgmii_delay = mv88e6390_port_set_rgmii_delay,
3528 .port_set_speed = mv88e6390_port_set_speed,
Andrew Lunn79523472016-11-21 23:27:00 +01003529 .stats_snapshot = mv88e6390_g1_stats_snapshot,
Andrew Lunnde2273872016-11-21 23:27:01 +01003530 .stats_set_histogram = mv88e6390_g1_stats_set_histogram,
Andrew Lunndfafe442016-11-21 23:27:02 +01003531 .stats_get_sset_count = mv88e6320_stats_get_sset_count,
3532 .stats_get_strings = mv88e6320_stats_get_strings,
Andrew Lunne0d8b612016-11-21 23:27:04 +01003533 .stats_get_stats = mv88e6390_stats_get_stats,
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003534};
3535
3536static const struct mv88e6xxx_ops mv88e6390x_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003537 /* MV88E6XXX_FAMILY_6390 */
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003538 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
3539 .phy_read = mv88e6xxx_g2_smi_phy_read,
3540 .phy_write = mv88e6xxx_g2_smi_phy_write,
3541 .port_set_link = mv88e6xxx_port_set_link,
3542 .port_set_duplex = mv88e6xxx_port_set_duplex,
3543 .port_set_rgmii_delay = mv88e6390_port_set_rgmii_delay,
3544 .port_set_speed = mv88e6390x_port_set_speed,
Andrew Lunn79523472016-11-21 23:27:00 +01003545 .stats_snapshot = mv88e6390_g1_stats_snapshot,
Andrew Lunnde2273872016-11-21 23:27:01 +01003546 .stats_set_histogram = mv88e6390_g1_stats_set_histogram,
Andrew Lunndfafe442016-11-21 23:27:02 +01003547 .stats_get_sset_count = mv88e6320_stats_get_sset_count,
3548 .stats_get_strings = mv88e6320_stats_get_strings,
Andrew Lunne0d8b612016-11-21 23:27:04 +01003549 .stats_get_stats = mv88e6390_stats_get_stats,
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003550};
3551
3552static const struct mv88e6xxx_ops mv88e6391_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003553 /* MV88E6XXX_FAMILY_6390 */
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003554 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
3555 .phy_read = mv88e6xxx_g2_smi_phy_read,
3556 .phy_write = mv88e6xxx_g2_smi_phy_write,
3557 .port_set_link = mv88e6xxx_port_set_link,
3558 .port_set_duplex = mv88e6xxx_port_set_duplex,
3559 .port_set_rgmii_delay = mv88e6390_port_set_rgmii_delay,
3560 .port_set_speed = mv88e6390_port_set_speed,
Andrew Lunn79523472016-11-21 23:27:00 +01003561 .stats_snapshot = mv88e6390_g1_stats_snapshot,
Andrew Lunnde2273872016-11-21 23:27:01 +01003562 .stats_set_histogram = mv88e6390_g1_stats_set_histogram,
Andrew Lunndfafe442016-11-21 23:27:02 +01003563 .stats_get_sset_count = mv88e6320_stats_get_sset_count,
3564 .stats_get_strings = mv88e6320_stats_get_strings,
Andrew Lunne0d8b612016-11-21 23:27:04 +01003565 .stats_get_stats = mv88e6390_stats_get_stats,
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003566};
3567
Vivien Didelotf81ec902016-05-09 13:22:58 -04003568static const struct mv88e6xxx_info mv88e6xxx_table[] = {
3569 [MV88E6085] = {
3570 .prod_num = PORT_SWITCH_ID_PROD_NUM_6085,
3571 .family = MV88E6XXX_FAMILY_6097,
3572 .name = "Marvell 88E6085",
3573 .num_databases = 4096,
3574 .num_ports = 10,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003575 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003576 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003577 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003578 .g1_irqs = 8,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003579 .flags = MV88E6XXX_FLAGS_FAMILY_6097,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003580 .ops = &mv88e6085_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003581 },
3582
3583 [MV88E6095] = {
3584 .prod_num = PORT_SWITCH_ID_PROD_NUM_6095,
3585 .family = MV88E6XXX_FAMILY_6095,
3586 .name = "Marvell 88E6095/88E6095F",
3587 .num_databases = 256,
3588 .num_ports = 11,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003589 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003590 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003591 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003592 .g1_irqs = 8,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003593 .flags = MV88E6XXX_FLAGS_FAMILY_6095,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003594 .ops = &mv88e6095_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003595 },
3596
Stefan Eichenberger7d381a02016-11-22 17:47:21 +01003597 [MV88E6097] = {
3598 .prod_num = PORT_SWITCH_ID_PROD_NUM_6097,
3599 .family = MV88E6XXX_FAMILY_6097,
3600 .name = "Marvell 88E6097/88E6097F",
3601 .num_databases = 4096,
3602 .num_ports = 11,
3603 .port_base_addr = 0x10,
3604 .global1_addr = 0x1b,
3605 .age_time_coeff = 15000,
Stefan Eichenbergerc5341782016-11-25 09:41:29 +01003606 .g1_irqs = 8,
Stefan Eichenberger7d381a02016-11-22 17:47:21 +01003607 .flags = MV88E6XXX_FLAGS_FAMILY_6097,
3608 .ops = &mv88e6097_ops,
3609 },
3610
Vivien Didelotf81ec902016-05-09 13:22:58 -04003611 [MV88E6123] = {
3612 .prod_num = PORT_SWITCH_ID_PROD_NUM_6123,
3613 .family = MV88E6XXX_FAMILY_6165,
3614 .name = "Marvell 88E6123",
3615 .num_databases = 4096,
3616 .num_ports = 3,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003617 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003618 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003619 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003620 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003621 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003622 .ops = &mv88e6123_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003623 },
3624
3625 [MV88E6131] = {
3626 .prod_num = PORT_SWITCH_ID_PROD_NUM_6131,
3627 .family = MV88E6XXX_FAMILY_6185,
3628 .name = "Marvell 88E6131",
3629 .num_databases = 256,
3630 .num_ports = 8,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003631 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003632 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003633 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003634 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003635 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003636 .ops = &mv88e6131_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003637 },
3638
3639 [MV88E6161] = {
3640 .prod_num = PORT_SWITCH_ID_PROD_NUM_6161,
3641 .family = MV88E6XXX_FAMILY_6165,
3642 .name = "Marvell 88E6161",
3643 .num_databases = 4096,
3644 .num_ports = 6,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003645 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003646 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003647 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003648 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003649 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003650 .ops = &mv88e6161_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003651 },
3652
3653 [MV88E6165] = {
3654 .prod_num = PORT_SWITCH_ID_PROD_NUM_6165,
3655 .family = MV88E6XXX_FAMILY_6165,
3656 .name = "Marvell 88E6165",
3657 .num_databases = 4096,
3658 .num_ports = 6,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003659 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003660 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003661 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003662 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003663 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003664 .ops = &mv88e6165_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003665 },
3666
3667 [MV88E6171] = {
3668 .prod_num = PORT_SWITCH_ID_PROD_NUM_6171,
3669 .family = MV88E6XXX_FAMILY_6351,
3670 .name = "Marvell 88E6171",
3671 .num_databases = 4096,
3672 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003673 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003674 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003675 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003676 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003677 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003678 .ops = &mv88e6171_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003679 },
3680
3681 [MV88E6172] = {
3682 .prod_num = PORT_SWITCH_ID_PROD_NUM_6172,
3683 .family = MV88E6XXX_FAMILY_6352,
3684 .name = "Marvell 88E6172",
3685 .num_databases = 4096,
3686 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003687 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003688 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003689 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003690 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003691 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003692 .ops = &mv88e6172_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003693 },
3694
3695 [MV88E6175] = {
3696 .prod_num = PORT_SWITCH_ID_PROD_NUM_6175,
3697 .family = MV88E6XXX_FAMILY_6351,
3698 .name = "Marvell 88E6175",
3699 .num_databases = 4096,
3700 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003701 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003702 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003703 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003704 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003705 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003706 .ops = &mv88e6175_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003707 },
3708
3709 [MV88E6176] = {
3710 .prod_num = PORT_SWITCH_ID_PROD_NUM_6176,
3711 .family = MV88E6XXX_FAMILY_6352,
3712 .name = "Marvell 88E6176",
3713 .num_databases = 4096,
3714 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003715 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003716 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003717 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003718 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003719 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003720 .ops = &mv88e6176_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003721 },
3722
3723 [MV88E6185] = {
3724 .prod_num = PORT_SWITCH_ID_PROD_NUM_6185,
3725 .family = MV88E6XXX_FAMILY_6185,
3726 .name = "Marvell 88E6185",
3727 .num_databases = 256,
3728 .num_ports = 10,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003729 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003730 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003731 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003732 .g1_irqs = 8,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003733 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003734 .ops = &mv88e6185_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003735 },
3736
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003737 [MV88E6190] = {
3738 .prod_num = PORT_SWITCH_ID_PROD_NUM_6190,
3739 .family = MV88E6XXX_FAMILY_6390,
3740 .name = "Marvell 88E6190",
3741 .num_databases = 4096,
3742 .num_ports = 11, /* 10 + Z80 */
3743 .port_base_addr = 0x0,
3744 .global1_addr = 0x1b,
3745 .age_time_coeff = 15000,
3746 .g1_irqs = 9,
3747 .flags = MV88E6XXX_FLAGS_FAMILY_6390,
3748 .ops = &mv88e6190_ops,
3749 },
3750
3751 [MV88E6190X] = {
3752 .prod_num = PORT_SWITCH_ID_PROD_NUM_6190X,
3753 .family = MV88E6XXX_FAMILY_6390,
3754 .name = "Marvell 88E6190X",
3755 .num_databases = 4096,
3756 .num_ports = 11, /* 10 + Z80 */
3757 .port_base_addr = 0x0,
3758 .global1_addr = 0x1b,
3759 .age_time_coeff = 15000,
3760 .g1_irqs = 9,
3761 .flags = MV88E6XXX_FLAGS_FAMILY_6390,
3762 .ops = &mv88e6190x_ops,
3763 },
3764
3765 [MV88E6191] = {
3766 .prod_num = PORT_SWITCH_ID_PROD_NUM_6191,
3767 .family = MV88E6XXX_FAMILY_6390,
3768 .name = "Marvell 88E6191",
3769 .num_databases = 4096,
3770 .num_ports = 11, /* 10 + Z80 */
3771 .port_base_addr = 0x0,
3772 .global1_addr = 0x1b,
3773 .age_time_coeff = 15000,
3774 .flags = MV88E6XXX_FLAGS_FAMILY_6390,
3775 .ops = &mv88e6391_ops,
3776 },
3777
Vivien Didelotf81ec902016-05-09 13:22:58 -04003778 [MV88E6240] = {
3779 .prod_num = PORT_SWITCH_ID_PROD_NUM_6240,
3780 .family = MV88E6XXX_FAMILY_6352,
3781 .name = "Marvell 88E6240",
3782 .num_databases = 4096,
3783 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003784 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003785 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003786 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003787 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003788 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003789 .ops = &mv88e6240_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003790 },
3791
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003792 [MV88E6290] = {
3793 .prod_num = PORT_SWITCH_ID_PROD_NUM_6290,
3794 .family = MV88E6XXX_FAMILY_6390,
3795 .name = "Marvell 88E6290",
3796 .num_databases = 4096,
3797 .num_ports = 11, /* 10 + Z80 */
3798 .port_base_addr = 0x0,
3799 .global1_addr = 0x1b,
3800 .age_time_coeff = 15000,
3801 .g1_irqs = 9,
3802 .flags = MV88E6XXX_FLAGS_FAMILY_6390,
3803 .ops = &mv88e6290_ops,
3804 },
3805
Vivien Didelotf81ec902016-05-09 13:22:58 -04003806 [MV88E6320] = {
3807 .prod_num = PORT_SWITCH_ID_PROD_NUM_6320,
3808 .family = MV88E6XXX_FAMILY_6320,
3809 .name = "Marvell 88E6320",
3810 .num_databases = 4096,
3811 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003812 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003813 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003814 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003815 .g1_irqs = 8,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003816 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003817 .ops = &mv88e6320_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003818 },
3819
3820 [MV88E6321] = {
3821 .prod_num = PORT_SWITCH_ID_PROD_NUM_6321,
3822 .family = MV88E6XXX_FAMILY_6320,
3823 .name = "Marvell 88E6321",
3824 .num_databases = 4096,
3825 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003826 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003827 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003828 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003829 .g1_irqs = 8,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003830 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003831 .ops = &mv88e6321_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003832 },
3833
3834 [MV88E6350] = {
3835 .prod_num = PORT_SWITCH_ID_PROD_NUM_6350,
3836 .family = MV88E6XXX_FAMILY_6351,
3837 .name = "Marvell 88E6350",
3838 .num_databases = 4096,
3839 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003840 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003841 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003842 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003843 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003844 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003845 .ops = &mv88e6350_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003846 },
3847
3848 [MV88E6351] = {
3849 .prod_num = PORT_SWITCH_ID_PROD_NUM_6351,
3850 .family = MV88E6XXX_FAMILY_6351,
3851 .name = "Marvell 88E6351",
3852 .num_databases = 4096,
3853 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003854 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003855 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003856 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003857 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003858 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003859 .ops = &mv88e6351_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003860 },
3861
3862 [MV88E6352] = {
3863 .prod_num = PORT_SWITCH_ID_PROD_NUM_6352,
3864 .family = MV88E6XXX_FAMILY_6352,
3865 .name = "Marvell 88E6352",
3866 .num_databases = 4096,
3867 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003868 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003869 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003870 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003871 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003872 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003873 .ops = &mv88e6352_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003874 },
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003875 [MV88E6390] = {
3876 .prod_num = PORT_SWITCH_ID_PROD_NUM_6390,
3877 .family = MV88E6XXX_FAMILY_6390,
3878 .name = "Marvell 88E6390",
3879 .num_databases = 4096,
3880 .num_ports = 11, /* 10 + Z80 */
3881 .port_base_addr = 0x0,
3882 .global1_addr = 0x1b,
3883 .age_time_coeff = 15000,
3884 .g1_irqs = 9,
3885 .flags = MV88E6XXX_FLAGS_FAMILY_6390,
3886 .ops = &mv88e6390_ops,
3887 },
3888 [MV88E6390X] = {
3889 .prod_num = PORT_SWITCH_ID_PROD_NUM_6390X,
3890 .family = MV88E6XXX_FAMILY_6390,
3891 .name = "Marvell 88E6390X",
3892 .num_databases = 4096,
3893 .num_ports = 11, /* 10 + Z80 */
3894 .port_base_addr = 0x0,
3895 .global1_addr = 0x1b,
3896 .age_time_coeff = 15000,
3897 .g1_irqs = 9,
3898 .flags = MV88E6XXX_FLAGS_FAMILY_6390,
3899 .ops = &mv88e6390x_ops,
3900 },
Vivien Didelotf81ec902016-05-09 13:22:58 -04003901};
3902
Vivien Didelot5f7c0362016-06-20 13:14:04 -04003903static const struct mv88e6xxx_info *mv88e6xxx_lookup_info(unsigned int prod_num)
Vivien Didelotb9b37712015-10-30 19:39:48 -04003904{
Vivien Didelota439c062016-04-17 13:23:58 -04003905 int i;
Vivien Didelotb9b37712015-10-30 19:39:48 -04003906
Vivien Didelot5f7c0362016-06-20 13:14:04 -04003907 for (i = 0; i < ARRAY_SIZE(mv88e6xxx_table); ++i)
3908 if (mv88e6xxx_table[i].prod_num == prod_num)
3909 return &mv88e6xxx_table[i];
Vivien Didelotb9b37712015-10-30 19:39:48 -04003910
Vivien Didelotb9b37712015-10-30 19:39:48 -04003911 return NULL;
3912}
3913
Vivien Didelotfad09c72016-06-21 12:28:20 -04003914static int mv88e6xxx_detect(struct mv88e6xxx_chip *chip)
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003915{
3916 const struct mv88e6xxx_info *info;
Vivien Didelot8f6345b2016-07-20 18:18:36 -04003917 unsigned int prod_num, rev;
3918 u16 id;
3919 int err;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003920
Vivien Didelot8f6345b2016-07-20 18:18:36 -04003921 mutex_lock(&chip->reg_lock);
3922 err = mv88e6xxx_port_read(chip, 0, PORT_SWITCH_ID, &id);
3923 mutex_unlock(&chip->reg_lock);
3924 if (err)
3925 return err;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003926
3927 prod_num = (id & 0xfff0) >> 4;
3928 rev = id & 0x000f;
3929
3930 info = mv88e6xxx_lookup_info(prod_num);
3931 if (!info)
3932 return -ENODEV;
3933
Vivien Didelotcaac8542016-06-20 13:14:09 -04003934 /* Update the compatible info with the probed one */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003935 chip->info = info;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003936
Vivien Didelotca070c12016-09-02 14:45:34 -04003937 err = mv88e6xxx_g2_require(chip);
3938 if (err)
3939 return err;
3940
Vivien Didelotfad09c72016-06-21 12:28:20 -04003941 dev_info(chip->dev, "switch 0x%x detected: %s, revision %u\n",
3942 chip->info->prod_num, chip->info->name, rev);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003943
3944 return 0;
3945}
3946
Vivien Didelotfad09c72016-06-21 12:28:20 -04003947static struct mv88e6xxx_chip *mv88e6xxx_alloc_chip(struct device *dev)
Vivien Didelot469d7292016-06-20 13:14:06 -04003948{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003949 struct mv88e6xxx_chip *chip;
Vivien Didelot469d7292016-06-20 13:14:06 -04003950
Vivien Didelotfad09c72016-06-21 12:28:20 -04003951 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
3952 if (!chip)
Vivien Didelot469d7292016-06-20 13:14:06 -04003953 return NULL;
3954
Vivien Didelotfad09c72016-06-21 12:28:20 -04003955 chip->dev = dev;
Vivien Didelot469d7292016-06-20 13:14:06 -04003956
Vivien Didelotfad09c72016-06-21 12:28:20 -04003957 mutex_init(&chip->reg_lock);
Vivien Didelot469d7292016-06-20 13:14:06 -04003958
Vivien Didelotfad09c72016-06-21 12:28:20 -04003959 return chip;
Vivien Didelot469d7292016-06-20 13:14:06 -04003960}
3961
Vivien Didelote57e5e72016-08-15 17:19:00 -04003962static void mv88e6xxx_phy_init(struct mv88e6xxx_chip *chip)
3963{
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003964 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU))
Vivien Didelote57e5e72016-08-15 17:19:00 -04003965 mv88e6xxx_ppu_state_init(chip);
Vivien Didelote57e5e72016-08-15 17:19:00 -04003966}
3967
Andrew Lunn930188c2016-08-22 16:01:03 +02003968static void mv88e6xxx_phy_destroy(struct mv88e6xxx_chip *chip)
3969{
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003970 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU))
Andrew Lunn930188c2016-08-22 16:01:03 +02003971 mv88e6xxx_ppu_state_destroy(chip);
Andrew Lunn930188c2016-08-22 16:01:03 +02003972}
3973
Vivien Didelotfad09c72016-06-21 12:28:20 -04003974static int mv88e6xxx_smi_init(struct mv88e6xxx_chip *chip,
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003975 struct mii_bus *bus, int sw_addr)
3976{
3977 /* ADDR[0] pin is unavailable externally and considered zero */
3978 if (sw_addr & 0x1)
3979 return -EINVAL;
3980
Vivien Didelot914b32f2016-06-20 13:14:11 -04003981 if (sw_addr == 0)
Vivien Didelotfad09c72016-06-21 12:28:20 -04003982 chip->smi_ops = &mv88e6xxx_smi_single_chip_ops;
Vivien Didelota0ffff22016-08-15 17:18:58 -04003983 else if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_MULTI_CHIP))
Vivien Didelotfad09c72016-06-21 12:28:20 -04003984 chip->smi_ops = &mv88e6xxx_smi_multi_chip_ops;
Vivien Didelot914b32f2016-06-20 13:14:11 -04003985 else
3986 return -EINVAL;
3987
Vivien Didelotfad09c72016-06-21 12:28:20 -04003988 chip->bus = bus;
3989 chip->sw_addr = sw_addr;
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003990
3991 return 0;
3992}
3993
Andrew Lunn7b314362016-08-22 16:01:01 +02003994static enum dsa_tag_protocol mv88e6xxx_get_tag_protocol(struct dsa_switch *ds)
3995{
Vivien Didelot04bed142016-08-31 18:06:13 -04003996 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02003997
3998 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA))
3999 return DSA_TAG_PROTO_EDSA;
4000
4001 return DSA_TAG_PROTO_DSA;
Andrew Lunn7b314362016-08-22 16:01:01 +02004002}
4003
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02004004static const char *mv88e6xxx_drv_probe(struct device *dsa_dev,
4005 struct device *host_dev, int sw_addr,
4006 void **priv)
Andrew Lunna77d43f2016-04-13 02:40:42 +02004007{
Vivien Didelotfad09c72016-06-21 12:28:20 -04004008 struct mv88e6xxx_chip *chip;
Vivien Didelota439c062016-04-17 13:23:58 -04004009 struct mii_bus *bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02004010 int err;
Andrew Lunna77d43f2016-04-13 02:40:42 +02004011
Vivien Didelota439c062016-04-17 13:23:58 -04004012 bus = dsa_host_dev_to_mii_bus(host_dev);
Andrew Lunnc1569132016-04-13 02:40:45 +02004013 if (!bus)
4014 return NULL;
4015
Vivien Didelotfad09c72016-06-21 12:28:20 -04004016 chip = mv88e6xxx_alloc_chip(dsa_dev);
4017 if (!chip)
Vivien Didelot469d7292016-06-20 13:14:06 -04004018 return NULL;
4019
Vivien Didelotcaac8542016-06-20 13:14:09 -04004020 /* Legacy SMI probing will only support chips similar to 88E6085 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04004021 chip->info = &mv88e6xxx_table[MV88E6085];
Vivien Didelotcaac8542016-06-20 13:14:09 -04004022
Vivien Didelotfad09c72016-06-21 12:28:20 -04004023 err = mv88e6xxx_smi_init(chip, bus, sw_addr);
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04004024 if (err)
4025 goto free;
4026
Vivien Didelotfad09c72016-06-21 12:28:20 -04004027 err = mv88e6xxx_detect(chip);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04004028 if (err)
Vivien Didelot469d7292016-06-20 13:14:06 -04004029 goto free;
Vivien Didelota439c062016-04-17 13:23:58 -04004030
Andrew Lunndc30c352016-10-16 19:56:49 +02004031 mutex_lock(&chip->reg_lock);
4032 err = mv88e6xxx_switch_reset(chip);
4033 mutex_unlock(&chip->reg_lock);
4034 if (err)
4035 goto free;
4036
Vivien Didelote57e5e72016-08-15 17:19:00 -04004037 mv88e6xxx_phy_init(chip);
4038
Vivien Didelotfad09c72016-06-21 12:28:20 -04004039 err = mv88e6xxx_mdio_register(chip, NULL);
Andrew Lunnb516d452016-06-04 21:17:06 +02004040 if (err)
Vivien Didelot469d7292016-06-20 13:14:06 -04004041 goto free;
Andrew Lunnb516d452016-06-04 21:17:06 +02004042
Vivien Didelotfad09c72016-06-21 12:28:20 -04004043 *priv = chip;
Vivien Didelota439c062016-04-17 13:23:58 -04004044
Vivien Didelotfad09c72016-06-21 12:28:20 -04004045 return chip->info->name;
Vivien Didelot469d7292016-06-20 13:14:06 -04004046free:
Vivien Didelotfad09c72016-06-21 12:28:20 -04004047 devm_kfree(dsa_dev, chip);
Vivien Didelot469d7292016-06-20 13:14:06 -04004048
4049 return NULL;
Andrew Lunna77d43f2016-04-13 02:40:42 +02004050}
4051
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04004052static int mv88e6xxx_port_mdb_prepare(struct dsa_switch *ds, int port,
4053 const struct switchdev_obj_port_mdb *mdb,
4054 struct switchdev_trans *trans)
4055{
4056 /* We don't need any dynamic resource from the kernel (yet),
4057 * so skip the prepare phase.
4058 */
4059
4060 return 0;
4061}
4062
4063static void mv88e6xxx_port_mdb_add(struct dsa_switch *ds, int port,
4064 const struct switchdev_obj_port_mdb *mdb,
4065 struct switchdev_trans *trans)
4066{
Vivien Didelot04bed142016-08-31 18:06:13 -04004067 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04004068
4069 mutex_lock(&chip->reg_lock);
4070 if (mv88e6xxx_port_db_load_purge(chip, port, mdb->addr, mdb->vid,
4071 GLOBAL_ATU_DATA_STATE_MC_STATIC))
4072 netdev_err(ds->ports[port].netdev, "failed to load multicast MAC address\n");
4073 mutex_unlock(&chip->reg_lock);
4074}
4075
4076static int mv88e6xxx_port_mdb_del(struct dsa_switch *ds, int port,
4077 const struct switchdev_obj_port_mdb *mdb)
4078{
Vivien Didelot04bed142016-08-31 18:06:13 -04004079 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04004080 int err;
4081
4082 mutex_lock(&chip->reg_lock);
4083 err = mv88e6xxx_port_db_load_purge(chip, port, mdb->addr, mdb->vid,
4084 GLOBAL_ATU_DATA_STATE_UNUSED);
4085 mutex_unlock(&chip->reg_lock);
4086
4087 return err;
4088}
4089
4090static int mv88e6xxx_port_mdb_dump(struct dsa_switch *ds, int port,
4091 struct switchdev_obj_port_mdb *mdb,
4092 int (*cb)(struct switchdev_obj *obj))
4093{
Vivien Didelot04bed142016-08-31 18:06:13 -04004094 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04004095 int err;
4096
4097 mutex_lock(&chip->reg_lock);
4098 err = mv88e6xxx_port_db_dump(chip, port, &mdb->obj, cb);
4099 mutex_unlock(&chip->reg_lock);
4100
4101 return err;
4102}
4103
Vivien Didelot9d490b42016-08-23 12:38:56 -04004104static struct dsa_switch_ops mv88e6xxx_switch_ops = {
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02004105 .probe = mv88e6xxx_drv_probe,
Andrew Lunn7b314362016-08-22 16:01:01 +02004106 .get_tag_protocol = mv88e6xxx_get_tag_protocol,
Vivien Didelotf81ec902016-05-09 13:22:58 -04004107 .setup = mv88e6xxx_setup,
4108 .set_addr = mv88e6xxx_set_addr,
Vivien Didelotf81ec902016-05-09 13:22:58 -04004109 .adjust_link = mv88e6xxx_adjust_link,
4110 .get_strings = mv88e6xxx_get_strings,
4111 .get_ethtool_stats = mv88e6xxx_get_ethtool_stats,
4112 .get_sset_count = mv88e6xxx_get_sset_count,
4113 .set_eee = mv88e6xxx_set_eee,
4114 .get_eee = mv88e6xxx_get_eee,
4115#ifdef CONFIG_NET_DSA_HWMON
4116 .get_temp = mv88e6xxx_get_temp,
4117 .get_temp_limit = mv88e6xxx_get_temp_limit,
4118 .set_temp_limit = mv88e6xxx_set_temp_limit,
4119 .get_temp_alarm = mv88e6xxx_get_temp_alarm,
4120#endif
Andrew Lunnf8cd8752016-05-10 23:27:25 +02004121 .get_eeprom_len = mv88e6xxx_get_eeprom_len,
Vivien Didelotf81ec902016-05-09 13:22:58 -04004122 .get_eeprom = mv88e6xxx_get_eeprom,
4123 .set_eeprom = mv88e6xxx_set_eeprom,
4124 .get_regs_len = mv88e6xxx_get_regs_len,
4125 .get_regs = mv88e6xxx_get_regs,
Vivien Didelot2cfcd962016-07-18 20:45:40 -04004126 .set_ageing_time = mv88e6xxx_set_ageing_time,
Vivien Didelotf81ec902016-05-09 13:22:58 -04004127 .port_bridge_join = mv88e6xxx_port_bridge_join,
4128 .port_bridge_leave = mv88e6xxx_port_bridge_leave,
4129 .port_stp_state_set = mv88e6xxx_port_stp_state_set,
Vivien Didelot749efcb2016-09-22 16:49:24 -04004130 .port_fast_age = mv88e6xxx_port_fast_age,
Vivien Didelotf81ec902016-05-09 13:22:58 -04004131 .port_vlan_filtering = mv88e6xxx_port_vlan_filtering,
4132 .port_vlan_prepare = mv88e6xxx_port_vlan_prepare,
4133 .port_vlan_add = mv88e6xxx_port_vlan_add,
4134 .port_vlan_del = mv88e6xxx_port_vlan_del,
4135 .port_vlan_dump = mv88e6xxx_port_vlan_dump,
4136 .port_fdb_prepare = mv88e6xxx_port_fdb_prepare,
4137 .port_fdb_add = mv88e6xxx_port_fdb_add,
4138 .port_fdb_del = mv88e6xxx_port_fdb_del,
4139 .port_fdb_dump = mv88e6xxx_port_fdb_dump,
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04004140 .port_mdb_prepare = mv88e6xxx_port_mdb_prepare,
4141 .port_mdb_add = mv88e6xxx_port_mdb_add,
4142 .port_mdb_del = mv88e6xxx_port_mdb_del,
4143 .port_mdb_dump = mv88e6xxx_port_mdb_dump,
Vivien Didelotf81ec902016-05-09 13:22:58 -04004144};
4145
Vivien Didelotfad09c72016-06-21 12:28:20 -04004146static int mv88e6xxx_register_switch(struct mv88e6xxx_chip *chip,
Vivien Didelotb7e66a52016-06-20 13:14:02 -04004147 struct device_node *np)
4148{
Vivien Didelotfad09c72016-06-21 12:28:20 -04004149 struct device *dev = chip->dev;
Vivien Didelotb7e66a52016-06-20 13:14:02 -04004150 struct dsa_switch *ds;
4151
4152 ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
4153 if (!ds)
4154 return -ENOMEM;
4155
4156 ds->dev = dev;
Vivien Didelotfad09c72016-06-21 12:28:20 -04004157 ds->priv = chip;
Vivien Didelot9d490b42016-08-23 12:38:56 -04004158 ds->ops = &mv88e6xxx_switch_ops;
Vivien Didelotb7e66a52016-06-20 13:14:02 -04004159
4160 dev_set_drvdata(dev, ds);
4161
4162 return dsa_register_switch(ds, np);
4163}
4164
Vivien Didelotfad09c72016-06-21 12:28:20 -04004165static void mv88e6xxx_unregister_switch(struct mv88e6xxx_chip *chip)
Vivien Didelotb7e66a52016-06-20 13:14:02 -04004166{
Vivien Didelotfad09c72016-06-21 12:28:20 -04004167 dsa_unregister_switch(chip->ds);
Vivien Didelotb7e66a52016-06-20 13:14:02 -04004168}
4169
Vivien Didelot57d32312016-06-20 13:13:58 -04004170static int mv88e6xxx_probe(struct mdio_device *mdiodev)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004171{
4172 struct device *dev = &mdiodev->dev;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02004173 struct device_node *np = dev->of_node;
Vivien Didelotcaac8542016-06-20 13:14:09 -04004174 const struct mv88e6xxx_info *compat_info;
Vivien Didelotfad09c72016-06-21 12:28:20 -04004175 struct mv88e6xxx_chip *chip;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02004176 u32 eeprom_len;
Andrew Lunn52638f72016-05-10 23:27:22 +02004177 int err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004178
Vivien Didelotcaac8542016-06-20 13:14:09 -04004179 compat_info = of_device_get_match_data(dev);
4180 if (!compat_info)
4181 return -EINVAL;
4182
Vivien Didelotfad09c72016-06-21 12:28:20 -04004183 chip = mv88e6xxx_alloc_chip(dev);
4184 if (!chip)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004185 return -ENOMEM;
4186
Vivien Didelotfad09c72016-06-21 12:28:20 -04004187 chip->info = compat_info;
Vivien Didelotcaac8542016-06-20 13:14:09 -04004188
Vivien Didelotfad09c72016-06-21 12:28:20 -04004189 err = mv88e6xxx_smi_init(chip, mdiodev->bus, mdiodev->addr);
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04004190 if (err)
4191 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004192
Andrew Lunnb4308f02016-11-21 23:26:55 +01004193 chip->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
4194 if (IS_ERR(chip->reset))
4195 return PTR_ERR(chip->reset);
4196
Vivien Didelotfad09c72016-06-21 12:28:20 -04004197 err = mv88e6xxx_detect(chip);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04004198 if (err)
4199 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004200
Vivien Didelote57e5e72016-08-15 17:19:00 -04004201 mv88e6xxx_phy_init(chip);
4202
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04004203 if (chip->info->ops->get_eeprom &&
Andrew Lunnf8cd8752016-05-10 23:27:25 +02004204 !of_property_read_u32(np, "eeprom-length", &eeprom_len))
Vivien Didelotfad09c72016-06-21 12:28:20 -04004205 chip->eeprom_len = eeprom_len;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02004206
Andrew Lunndc30c352016-10-16 19:56:49 +02004207 mutex_lock(&chip->reg_lock);
4208 err = mv88e6xxx_switch_reset(chip);
4209 mutex_unlock(&chip->reg_lock);
Andrew Lunnb516d452016-06-04 21:17:06 +02004210 if (err)
Andrew Lunndc30c352016-10-16 19:56:49 +02004211 goto out;
Andrew Lunnb516d452016-06-04 21:17:06 +02004212
Andrew Lunndc30c352016-10-16 19:56:49 +02004213 chip->irq = of_irq_get(np, 0);
4214 if (chip->irq == -EPROBE_DEFER) {
4215 err = chip->irq;
4216 goto out;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02004217 }
4218
Andrew Lunndc30c352016-10-16 19:56:49 +02004219 if (chip->irq > 0) {
4220 /* Has to be performed before the MDIO bus is created,
4221 * because the PHYs will link there interrupts to these
4222 * interrupt controllers
4223 */
4224 mutex_lock(&chip->reg_lock);
4225 err = mv88e6xxx_g1_irq_setup(chip);
4226 mutex_unlock(&chip->reg_lock);
4227
4228 if (err)
4229 goto out;
4230
4231 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_INT)) {
4232 err = mv88e6xxx_g2_irq_setup(chip);
4233 if (err)
4234 goto out_g1_irq;
4235 }
4236 }
4237
4238 err = mv88e6xxx_mdio_register(chip, np);
4239 if (err)
4240 goto out_g2_irq;
4241
4242 err = mv88e6xxx_register_switch(chip, np);
4243 if (err)
4244 goto out_mdio;
4245
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004246 return 0;
Andrew Lunndc30c352016-10-16 19:56:49 +02004247
4248out_mdio:
4249 mv88e6xxx_mdio_unregister(chip);
4250out_g2_irq:
Andrew Lunn467126442016-11-20 20:14:15 +01004251 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_INT) && chip->irq > 0)
Andrew Lunndc30c352016-10-16 19:56:49 +02004252 mv88e6xxx_g2_irq_free(chip);
4253out_g1_irq:
Andrew Lunn61f7c3f2016-11-20 20:14:19 +01004254 if (chip->irq > 0) {
4255 mutex_lock(&chip->reg_lock);
Andrew Lunn467126442016-11-20 20:14:15 +01004256 mv88e6xxx_g1_irq_free(chip);
Andrew Lunn61f7c3f2016-11-20 20:14:19 +01004257 mutex_unlock(&chip->reg_lock);
4258 }
Andrew Lunndc30c352016-10-16 19:56:49 +02004259out:
4260 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004261}
4262
4263static void mv88e6xxx_remove(struct mdio_device *mdiodev)
4264{
4265 struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev);
Vivien Didelot04bed142016-08-31 18:06:13 -04004266 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004267
Andrew Lunn930188c2016-08-22 16:01:03 +02004268 mv88e6xxx_phy_destroy(chip);
Vivien Didelotfad09c72016-06-21 12:28:20 -04004269 mv88e6xxx_unregister_switch(chip);
4270 mv88e6xxx_mdio_unregister(chip);
Andrew Lunndc30c352016-10-16 19:56:49 +02004271
Andrew Lunn467126442016-11-20 20:14:15 +01004272 if (chip->irq > 0) {
4273 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_INT))
4274 mv88e6xxx_g2_irq_free(chip);
4275 mv88e6xxx_g1_irq_free(chip);
4276 }
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004277}
4278
4279static const struct of_device_id mv88e6xxx_of_match[] = {
Vivien Didelotcaac8542016-06-20 13:14:09 -04004280 {
4281 .compatible = "marvell,mv88e6085",
4282 .data = &mv88e6xxx_table[MV88E6085],
4283 },
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01004284 {
4285 .compatible = "marvell,mv88e6190",
4286 .data = &mv88e6xxx_table[MV88E6190],
4287 },
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004288 { /* sentinel */ },
4289};
4290
4291MODULE_DEVICE_TABLE(of, mv88e6xxx_of_match);
4292
4293static struct mdio_driver mv88e6xxx_driver = {
4294 .probe = mv88e6xxx_probe,
4295 .remove = mv88e6xxx_remove,
4296 .mdiodrv.driver = {
4297 .name = "mv88e6085",
4298 .of_match_table = mv88e6xxx_of_match,
4299 },
4300};
4301
Ben Hutchings98e67302011-11-25 14:36:19 +00004302static int __init mv88e6xxx_init(void)
4303{
Vivien Didelot9d490b42016-08-23 12:38:56 -04004304 register_switch_driver(&mv88e6xxx_switch_ops);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004305 return mdio_driver_register(&mv88e6xxx_driver);
Ben Hutchings98e67302011-11-25 14:36:19 +00004306}
4307module_init(mv88e6xxx_init);
4308
4309static void __exit mv88e6xxx_cleanup(void)
4310{
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004311 mdio_driver_unregister(&mv88e6xxx_driver);
Vivien Didelot9d490b42016-08-23 12:38:56 -04004312 unregister_switch_driver(&mv88e6xxx_switch_ops);
Ben Hutchings98e67302011-11-25 14:36:19 +00004313}
4314module_exit(mv88e6xxx_cleanup);
Ben Hutchings3d825ed2011-11-25 14:37:16 +00004315
4316MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
4317MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
4318MODULE_LICENSE("GPL");