blob: 5f2193949f8761b478645f559e41612575713f2a [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
424 for (irq = 0; irq < 16; 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
Vivien Didelotfad09c72016-06-21 12:28:20 -0400783static int _mv88e6xxx_stats_wait(struct mv88e6xxx_chip *chip)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000784{
Vivien Didelota935c052016-09-29 12:21:53 -0400785 u16 val;
786 int i, err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000787
788 for (i = 0; i < 10; i++) {
Vivien Didelota935c052016-09-29 12:21:53 -0400789 err = mv88e6xxx_g1_read(chip, GLOBAL_STATS_OP, &val);
Andrew Lunn096eea02016-11-21 23:26:56 +0100790 if (err)
791 return err;
792
Vivien Didelota935c052016-09-29 12:21:53 -0400793 if ((val & GLOBAL_STATS_OP_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000794 return 0;
795 }
796
797 return -ETIMEDOUT;
798}
799
Andrew Lunna605a0f2016-11-21 23:26:58 +0100800static int mv88e6xxx_stats_snapshot(struct mv88e6xxx_chip *chip, int port)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000801{
Andrew Lunna605a0f2016-11-21 23:26:58 +0100802 if (!chip->info->ops->stats_snapshot)
803 return -EOPNOTSUPP;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000804
Andrew Lunna605a0f2016-11-21 23:26:58 +0100805 return chip->info->ops->stats_snapshot(chip, port);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000806}
807
Vivien Didelotfad09c72016-06-21 12:28:20 -0400808static void _mv88e6xxx_stats_read(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -0400809 int stat, u32 *val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000810{
Vivien Didelota935c052016-09-29 12:21:53 -0400811 u32 value;
812 u16 reg;
813 int err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000814
815 *val = 0;
816
Vivien Didelota935c052016-09-29 12:21:53 -0400817 err = mv88e6xxx_g1_write(chip, GLOBAL_STATS_OP,
Andrew Lunne0d8b612016-11-21 23:27:04 +0100818 GLOBAL_STATS_OP_READ_CAPTURED | stat);
Vivien Didelota935c052016-09-29 12:21:53 -0400819 if (err)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000820 return;
821
Vivien Didelota935c052016-09-29 12:21:53 -0400822 err = _mv88e6xxx_stats_wait(chip);
823 if (err)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000824 return;
825
Vivien Didelota935c052016-09-29 12:21:53 -0400826 err = mv88e6xxx_g1_read(chip, GLOBAL_STATS_COUNTER_32, &reg);
827 if (err)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000828 return;
829
Vivien Didelota935c052016-09-29 12:21:53 -0400830 value = reg << 16;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000831
Vivien Didelota935c052016-09-29 12:21:53 -0400832 err = mv88e6xxx_g1_read(chip, GLOBAL_STATS_COUNTER_01, &reg);
833 if (err)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000834 return;
835
Vivien Didelota935c052016-09-29 12:21:53 -0400836 *val = value | reg;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000837}
838
Andrew Lunne413e7e2015-04-02 04:06:38 +0200839static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
Andrew Lunndfafe442016-11-21 23:27:02 +0100840 { "in_good_octets", 8, 0x00, STATS_TYPE_BANK0, },
841 { "in_bad_octets", 4, 0x02, STATS_TYPE_BANK0, },
842 { "in_unicast", 4, 0x04, STATS_TYPE_BANK0, },
843 { "in_broadcasts", 4, 0x06, STATS_TYPE_BANK0, },
844 { "in_multicasts", 4, 0x07, STATS_TYPE_BANK0, },
845 { "in_pause", 4, 0x16, STATS_TYPE_BANK0, },
846 { "in_undersize", 4, 0x18, STATS_TYPE_BANK0, },
847 { "in_fragments", 4, 0x19, STATS_TYPE_BANK0, },
848 { "in_oversize", 4, 0x1a, STATS_TYPE_BANK0, },
849 { "in_jabber", 4, 0x1b, STATS_TYPE_BANK0, },
850 { "in_rx_error", 4, 0x1c, STATS_TYPE_BANK0, },
851 { "in_fcs_error", 4, 0x1d, STATS_TYPE_BANK0, },
852 { "out_octets", 8, 0x0e, STATS_TYPE_BANK0, },
853 { "out_unicast", 4, 0x10, STATS_TYPE_BANK0, },
854 { "out_broadcasts", 4, 0x13, STATS_TYPE_BANK0, },
855 { "out_multicasts", 4, 0x12, STATS_TYPE_BANK0, },
856 { "out_pause", 4, 0x15, STATS_TYPE_BANK0, },
857 { "excessive", 4, 0x11, STATS_TYPE_BANK0, },
858 { "collisions", 4, 0x1e, STATS_TYPE_BANK0, },
859 { "deferred", 4, 0x05, STATS_TYPE_BANK0, },
860 { "single", 4, 0x14, STATS_TYPE_BANK0, },
861 { "multiple", 4, 0x17, STATS_TYPE_BANK0, },
862 { "out_fcs_error", 4, 0x03, STATS_TYPE_BANK0, },
863 { "late", 4, 0x1f, STATS_TYPE_BANK0, },
864 { "hist_64bytes", 4, 0x08, STATS_TYPE_BANK0, },
865 { "hist_65_127bytes", 4, 0x09, STATS_TYPE_BANK0, },
866 { "hist_128_255bytes", 4, 0x0a, STATS_TYPE_BANK0, },
867 { "hist_256_511bytes", 4, 0x0b, STATS_TYPE_BANK0, },
868 { "hist_512_1023bytes", 4, 0x0c, STATS_TYPE_BANK0, },
869 { "hist_1024_max_bytes", 4, 0x0d, STATS_TYPE_BANK0, },
870 { "sw_in_discards", 4, 0x10, STATS_TYPE_PORT, },
871 { "sw_in_filtered", 2, 0x12, STATS_TYPE_PORT, },
872 { "sw_out_filtered", 2, 0x13, STATS_TYPE_PORT, },
873 { "in_discards", 4, 0x00, STATS_TYPE_BANK1, },
874 { "in_filtered", 4, 0x01, STATS_TYPE_BANK1, },
875 { "in_accepted", 4, 0x02, STATS_TYPE_BANK1, },
876 { "in_bad_accepted", 4, 0x03, STATS_TYPE_BANK1, },
877 { "in_good_avb_class_a", 4, 0x04, STATS_TYPE_BANK1, },
878 { "in_good_avb_class_b", 4, 0x05, STATS_TYPE_BANK1, },
879 { "in_bad_avb_class_a", 4, 0x06, STATS_TYPE_BANK1, },
880 { "in_bad_avb_class_b", 4, 0x07, STATS_TYPE_BANK1, },
881 { "tcam_counter_0", 4, 0x08, STATS_TYPE_BANK1, },
882 { "tcam_counter_1", 4, 0x09, STATS_TYPE_BANK1, },
883 { "tcam_counter_2", 4, 0x0a, STATS_TYPE_BANK1, },
884 { "tcam_counter_3", 4, 0x0b, STATS_TYPE_BANK1, },
885 { "in_da_unknown", 4, 0x0e, STATS_TYPE_BANK1, },
886 { "in_management", 4, 0x0f, STATS_TYPE_BANK1, },
887 { "out_queue_0", 4, 0x10, STATS_TYPE_BANK1, },
888 { "out_queue_1", 4, 0x11, STATS_TYPE_BANK1, },
889 { "out_queue_2", 4, 0x12, STATS_TYPE_BANK1, },
890 { "out_queue_3", 4, 0x13, STATS_TYPE_BANK1, },
891 { "out_queue_4", 4, 0x14, STATS_TYPE_BANK1, },
892 { "out_queue_5", 4, 0x15, STATS_TYPE_BANK1, },
893 { "out_queue_6", 4, 0x16, STATS_TYPE_BANK1, },
894 { "out_queue_7", 4, 0x17, STATS_TYPE_BANK1, },
895 { "out_cut_through", 4, 0x18, STATS_TYPE_BANK1, },
896 { "out_octets_a", 4, 0x1a, STATS_TYPE_BANK1, },
897 { "out_octets_b", 4, 0x1b, STATS_TYPE_BANK1, },
898 { "out_management", 4, 0x1f, STATS_TYPE_BANK1, },
Andrew Lunne413e7e2015-04-02 04:06:38 +0200899};
900
Vivien Didelotfad09c72016-06-21 12:28:20 -0400901static uint64_t _mv88e6xxx_get_ethtool_stat(struct mv88e6xxx_chip *chip,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100902 struct mv88e6xxx_hw_stat *s,
Andrew Lunne0d8b612016-11-21 23:27:04 +0100903 int port, u16 bank1_select,
904 u16 histogram)
Andrew Lunn80c46272015-06-20 18:42:30 +0200905{
Andrew Lunn80c46272015-06-20 18:42:30 +0200906 u32 low;
907 u32 high = 0;
Andrew Lunndfafe442016-11-21 23:27:02 +0100908 u16 reg = 0;
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200909 int err;
Andrew Lunn80c46272015-06-20 18:42:30 +0200910 u64 value;
911
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100912 switch (s->type) {
Andrew Lunndfafe442016-11-21 23:27:02 +0100913 case STATS_TYPE_PORT:
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200914 err = mv88e6xxx_port_read(chip, port, s->reg, &reg);
915 if (err)
Andrew Lunn80c46272015-06-20 18:42:30 +0200916 return UINT64_MAX;
917
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200918 low = reg;
Andrew Lunn80c46272015-06-20 18:42:30 +0200919 if (s->sizeof_stat == 4) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200920 err = mv88e6xxx_port_read(chip, port, s->reg + 1, &reg);
921 if (err)
Andrew Lunn80c46272015-06-20 18:42:30 +0200922 return UINT64_MAX;
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200923 high = reg;
Andrew Lunn80c46272015-06-20 18:42:30 +0200924 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100925 break;
Andrew Lunndfafe442016-11-21 23:27:02 +0100926 case STATS_TYPE_BANK1:
Andrew Lunne0d8b612016-11-21 23:27:04 +0100927 reg = bank1_select;
Andrew Lunndfafe442016-11-21 23:27:02 +0100928 /* fall through */
929 case STATS_TYPE_BANK0:
Andrew Lunne0d8b612016-11-21 23:27:04 +0100930 reg |= s->reg | histogram;
Andrew Lunndfafe442016-11-21 23:27:02 +0100931 _mv88e6xxx_stats_read(chip, reg, &low);
Andrew Lunn80c46272015-06-20 18:42:30 +0200932 if (s->sizeof_stat == 8)
Andrew Lunndfafe442016-11-21 23:27:02 +0100933 _mv88e6xxx_stats_read(chip, reg + 1, &high);
Andrew Lunn80c46272015-06-20 18:42:30 +0200934 }
935 value = (((u64)high) << 16) | low;
936 return value;
937}
938
Andrew Lunndfafe442016-11-21 23:27:02 +0100939static void mv88e6xxx_stats_get_strings(struct mv88e6xxx_chip *chip,
940 uint8_t *data, int types)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100941{
942 struct mv88e6xxx_hw_stat *stat;
943 int i, j;
944
945 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
946 stat = &mv88e6xxx_hw_stats[i];
Andrew Lunndfafe442016-11-21 23:27:02 +0100947 if (stat->type & types) {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100948 memcpy(data + j * ETH_GSTRING_LEN, stat->string,
949 ETH_GSTRING_LEN);
950 j++;
951 }
952 }
953}
954
Andrew Lunndfafe442016-11-21 23:27:02 +0100955static void mv88e6095_stats_get_strings(struct mv88e6xxx_chip *chip,
956 uint8_t *data)
957{
958 mv88e6xxx_stats_get_strings(chip, data,
959 STATS_TYPE_BANK0 | STATS_TYPE_PORT);
960}
961
962static void mv88e6320_stats_get_strings(struct mv88e6xxx_chip *chip,
963 uint8_t *data)
964{
965 mv88e6xxx_stats_get_strings(chip, data,
966 STATS_TYPE_BANK0 | STATS_TYPE_BANK1);
967}
968
969static void mv88e6xxx_get_strings(struct dsa_switch *ds, int port,
970 uint8_t *data)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100971{
Vivien Didelot04bed142016-08-31 18:06:13 -0400972 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunndfafe442016-11-21 23:27:02 +0100973
974 if (chip->info->ops->stats_get_strings)
975 chip->info->ops->stats_get_strings(chip, data);
976}
977
978static int mv88e6xxx_stats_get_sset_count(struct mv88e6xxx_chip *chip,
979 int types)
980{
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100981 struct mv88e6xxx_hw_stat *stat;
982 int i, j;
983
984 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
985 stat = &mv88e6xxx_hw_stats[i];
Andrew Lunndfafe442016-11-21 23:27:02 +0100986 if (stat->type & types)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100987 j++;
988 }
989 return j;
990}
991
Andrew Lunndfafe442016-11-21 23:27:02 +0100992static int mv88e6095_stats_get_sset_count(struct mv88e6xxx_chip *chip)
993{
994 return mv88e6xxx_stats_get_sset_count(chip, STATS_TYPE_BANK0 |
995 STATS_TYPE_PORT);
996}
997
998static int mv88e6320_stats_get_sset_count(struct mv88e6xxx_chip *chip)
999{
1000 return mv88e6xxx_stats_get_sset_count(chip, STATS_TYPE_BANK0 |
1001 STATS_TYPE_BANK1);
1002}
1003
1004static int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
1005{
1006 struct mv88e6xxx_chip *chip = ds->priv;
1007
1008 if (chip->info->ops->stats_get_sset_count)
1009 return chip->info->ops->stats_get_sset_count(chip);
1010
1011 return 0;
1012}
1013
Andrew Lunn052f9472016-11-21 23:27:03 +01001014static void mv88e6xxx_stats_get_stats(struct mv88e6xxx_chip *chip, int port,
Andrew Lunne0d8b612016-11-21 23:27:04 +01001015 uint64_t *data, int types,
1016 u16 bank1_select, u16 histogram)
Andrew Lunn052f9472016-11-21 23:27:03 +01001017{
1018 struct mv88e6xxx_hw_stat *stat;
1019 int i, j;
1020
1021 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
1022 stat = &mv88e6xxx_hw_stats[i];
1023 if (stat->type & types) {
Andrew Lunne0d8b612016-11-21 23:27:04 +01001024 data[j] = _mv88e6xxx_get_ethtool_stat(chip, stat, port,
1025 bank1_select,
1026 histogram);
Andrew Lunn052f9472016-11-21 23:27:03 +01001027 j++;
1028 }
1029 }
1030}
1031
1032static void mv88e6095_stats_get_stats(struct mv88e6xxx_chip *chip, int port,
1033 uint64_t *data)
1034{
1035 return mv88e6xxx_stats_get_stats(chip, port, data,
Andrew Lunne0d8b612016-11-21 23:27:04 +01001036 STATS_TYPE_BANK0 | STATS_TYPE_PORT,
1037 0, GLOBAL_STATS_OP_HIST_RX_TX);
Andrew Lunn052f9472016-11-21 23:27:03 +01001038}
1039
1040static void mv88e6320_stats_get_stats(struct mv88e6xxx_chip *chip, int port,
1041 uint64_t *data)
1042{
1043 return mv88e6xxx_stats_get_stats(chip, port, data,
Andrew Lunne0d8b612016-11-21 23:27:04 +01001044 STATS_TYPE_BANK0 | STATS_TYPE_BANK1,
1045 GLOBAL_STATS_OP_BANK_1_BIT_9,
1046 GLOBAL_STATS_OP_HIST_RX_TX);
1047}
1048
1049static void mv88e6390_stats_get_stats(struct mv88e6xxx_chip *chip, int port,
1050 uint64_t *data)
1051{
1052 return mv88e6xxx_stats_get_stats(chip, port, data,
1053 STATS_TYPE_BANK0 | STATS_TYPE_BANK1,
1054 GLOBAL_STATS_OP_BANK_1_BIT_10, 0);
Andrew Lunn052f9472016-11-21 23:27:03 +01001055}
1056
1057static void mv88e6xxx_get_stats(struct mv88e6xxx_chip *chip, int port,
1058 uint64_t *data)
1059{
1060 if (chip->info->ops->stats_get_stats)
1061 chip->info->ops->stats_get_stats(chip, port, data);
1062}
1063
Vivien Didelotf81ec902016-05-09 13:22:58 -04001064static void mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, int port,
1065 uint64_t *data)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001066{
Vivien Didelot04bed142016-08-31 18:06:13 -04001067 struct mv88e6xxx_chip *chip = ds->priv;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001068 int ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001069
Vivien Didelotfad09c72016-06-21 12:28:20 -04001070 mutex_lock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001071
Andrew Lunna605a0f2016-11-21 23:26:58 +01001072 ret = mv88e6xxx_stats_snapshot(chip, port);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001073 if (ret < 0) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001074 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001075 return;
1076 }
Andrew Lunn052f9472016-11-21 23:27:03 +01001077
1078 mv88e6xxx_get_stats(chip, port, data);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001079
Vivien Didelotfad09c72016-06-21 12:28:20 -04001080 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001081}
Ben Hutchings98e67302011-11-25 14:36:19 +00001082
Andrew Lunnde2273872016-11-21 23:27:01 +01001083static int mv88e6xxx_stats_set_histogram(struct mv88e6xxx_chip *chip)
1084{
1085 if (chip->info->ops->stats_set_histogram)
1086 return chip->info->ops->stats_set_histogram(chip);
1087
1088 return 0;
1089}
1090
Vivien Didelotf81ec902016-05-09 13:22:58 -04001091static int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001092{
1093 return 32 * sizeof(u16);
1094}
1095
Vivien Didelotf81ec902016-05-09 13:22:58 -04001096static void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
1097 struct ethtool_regs *regs, void *_p)
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001098{
Vivien Didelot04bed142016-08-31 18:06:13 -04001099 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001100 int err;
1101 u16 reg;
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001102 u16 *p = _p;
1103 int i;
1104
1105 regs->version = 0;
1106
1107 memset(p, 0xff, 32 * sizeof(u16));
1108
Vivien Didelotfad09c72016-06-21 12:28:20 -04001109 mutex_lock(&chip->reg_lock);
Vivien Didelot23062512016-05-09 13:22:45 -04001110
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001111 for (i = 0; i < 32; i++) {
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001112
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001113 err = mv88e6xxx_port_read(chip, port, i, &reg);
1114 if (!err)
1115 p[i] = reg;
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001116 }
Vivien Didelot23062512016-05-09 13:22:45 -04001117
Vivien Didelotfad09c72016-06-21 12:28:20 -04001118 mutex_unlock(&chip->reg_lock);
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001119}
1120
Vivien Didelotfad09c72016-06-21 12:28:20 -04001121static int _mv88e6xxx_atu_wait(struct mv88e6xxx_chip *chip)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001122{
Vivien Didelota935c052016-09-29 12:21:53 -04001123 return mv88e6xxx_g1_wait(chip, GLOBAL_ATU_OP, GLOBAL_ATU_OP_BUSY);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001124}
1125
Vivien Didelotf81ec902016-05-09 13:22:58 -04001126static int mv88e6xxx_get_eee(struct dsa_switch *ds, int port,
1127 struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -08001128{
Vivien Didelot04bed142016-08-31 18:06:13 -04001129 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot9c938292016-08-15 17:19:02 -04001130 u16 reg;
1131 int err;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001132
Vivien Didelotfad09c72016-06-21 12:28:20 -04001133 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
Vivien Didelotaadbdb82016-05-09 13:22:44 -04001134 return -EOPNOTSUPP;
1135
Vivien Didelotfad09c72016-06-21 12:28:20 -04001136 mutex_lock(&chip->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001137
Vivien Didelot9c938292016-08-15 17:19:02 -04001138 err = mv88e6xxx_phy_read(chip, port, 16, &reg);
1139 if (err)
Andrew Lunn2f40c692015-04-02 04:06:37 +02001140 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001141
1142 e->eee_enabled = !!(reg & 0x0200);
1143 e->tx_lpi_enabled = !!(reg & 0x0100);
1144
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001145 err = mv88e6xxx_port_read(chip, port, PORT_STATUS, &reg);
Vivien Didelot9c938292016-08-15 17:19:02 -04001146 if (err)
Andrew Lunn2f40c692015-04-02 04:06:37 +02001147 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001148
Andrew Lunncca8b132015-04-02 04:06:39 +02001149 e->eee_active = !!(reg & PORT_STATUS_EEE);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001150out:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001151 mutex_unlock(&chip->reg_lock);
Vivien Didelot9c938292016-08-15 17:19:02 -04001152
1153 return err;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001154}
1155
Vivien Didelotf81ec902016-05-09 13:22:58 -04001156static int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
1157 struct phy_device *phydev, struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -08001158{
Vivien Didelot04bed142016-08-31 18:06:13 -04001159 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot9c938292016-08-15 17:19:02 -04001160 u16 reg;
1161 int err;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001162
Vivien Didelotfad09c72016-06-21 12:28:20 -04001163 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
Vivien Didelotaadbdb82016-05-09 13:22:44 -04001164 return -EOPNOTSUPP;
1165
Vivien Didelotfad09c72016-06-21 12:28:20 -04001166 mutex_lock(&chip->reg_lock);
Guenter Roeck11b3b452015-03-06 22:23:51 -08001167
Vivien Didelot9c938292016-08-15 17:19:02 -04001168 err = mv88e6xxx_phy_read(chip, port, 16, &reg);
1169 if (err)
Andrew Lunn2f40c692015-04-02 04:06:37 +02001170 goto out;
1171
Vivien Didelot9c938292016-08-15 17:19:02 -04001172 reg &= ~0x0300;
Andrew Lunn2f40c692015-04-02 04:06:37 +02001173 if (e->eee_enabled)
1174 reg |= 0x0200;
1175 if (e->tx_lpi_enabled)
1176 reg |= 0x0100;
1177
Vivien Didelot9c938292016-08-15 17:19:02 -04001178 err = mv88e6xxx_phy_write(chip, port, 16, reg);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001179out:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001180 mutex_unlock(&chip->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001181
Vivien Didelot9c938292016-08-15 17:19:02 -04001182 return err;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001183}
1184
Vivien Didelotfad09c72016-06-21 12:28:20 -04001185static int _mv88e6xxx_atu_cmd(struct mv88e6xxx_chip *chip, u16 fid, u16 cmd)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001186{
Vivien Didelota935c052016-09-29 12:21:53 -04001187 u16 val;
1188 int err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001189
Vivien Didelot6dc10bb2016-09-29 12:21:55 -04001190 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G1_ATU_FID)) {
Vivien Didelota935c052016-09-29 12:21:53 -04001191 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_FID, fid);
1192 if (err)
1193 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001194 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001195 /* ATU DBNum[7:4] are located in ATU Control 15:12 */
Vivien Didelota935c052016-09-29 12:21:53 -04001196 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_CONTROL, &val);
1197 if (err)
1198 return err;
Vivien Didelot11ea8092016-03-31 16:53:44 -04001199
Vivien Didelota935c052016-09-29 12:21:53 -04001200 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_CONTROL,
1201 (val & 0xfff) | ((fid << 8) & 0xf000));
1202 if (err)
1203 return err;
Vivien Didelot11ea8092016-03-31 16:53:44 -04001204
1205 /* ATU DBNum[3:0] are located in ATU Operation 3:0 */
1206 cmd |= fid & 0xf;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001207 }
1208
Vivien Didelota935c052016-09-29 12:21:53 -04001209 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_OP, cmd);
1210 if (err)
1211 return err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001212
Vivien Didelotfad09c72016-06-21 12:28:20 -04001213 return _mv88e6xxx_atu_wait(chip);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001214}
1215
Vivien Didelotfad09c72016-06-21 12:28:20 -04001216static int _mv88e6xxx_atu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot37705b72015-09-04 14:34:11 -04001217 struct mv88e6xxx_atu_entry *entry)
1218{
1219 u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK;
1220
1221 if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) {
1222 unsigned int mask, shift;
1223
1224 if (entry->trunk) {
1225 data |= GLOBAL_ATU_DATA_TRUNK;
1226 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
1227 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
1228 } else {
1229 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
1230 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
1231 }
1232
1233 data |= (entry->portv_trunkid << shift) & mask;
1234 }
1235
Vivien Didelota935c052016-09-29 12:21:53 -04001236 return mv88e6xxx_g1_write(chip, GLOBAL_ATU_DATA, data);
Vivien Didelot37705b72015-09-04 14:34:11 -04001237}
1238
Vivien Didelotfad09c72016-06-21 12:28:20 -04001239static int _mv88e6xxx_atu_flush_move(struct mv88e6xxx_chip *chip,
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001240 struct mv88e6xxx_atu_entry *entry,
1241 bool static_too)
1242{
1243 int op;
1244 int err;
1245
Vivien Didelotfad09c72016-06-21 12:28:20 -04001246 err = _mv88e6xxx_atu_wait(chip);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001247 if (err)
1248 return err;
1249
Vivien Didelotfad09c72016-06-21 12:28:20 -04001250 err = _mv88e6xxx_atu_data_write(chip, entry);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001251 if (err)
1252 return err;
1253
1254 if (entry->fid) {
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001255 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB :
1256 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB;
1257 } else {
1258 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL :
1259 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC;
1260 }
1261
Vivien Didelotfad09c72016-06-21 12:28:20 -04001262 return _mv88e6xxx_atu_cmd(chip, entry->fid, op);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001263}
1264
Vivien Didelotfad09c72016-06-21 12:28:20 -04001265static int _mv88e6xxx_atu_flush(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001266 u16 fid, bool static_too)
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001267{
1268 struct mv88e6xxx_atu_entry entry = {
1269 .fid = fid,
1270 .state = 0, /* EntryState bits must be 0 */
1271 };
1272
Vivien Didelotfad09c72016-06-21 12:28:20 -04001273 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001274}
1275
Vivien Didelotfad09c72016-06-21 12:28:20 -04001276static int _mv88e6xxx_atu_move(struct mv88e6xxx_chip *chip, u16 fid,
Andrew Lunn158bc062016-04-28 21:24:06 -04001277 int from_port, int to_port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001278{
1279 struct mv88e6xxx_atu_entry entry = {
1280 .trunk = false,
1281 .fid = fid,
1282 };
1283
1284 /* EntryState bits must be 0xF */
1285 entry.state = GLOBAL_ATU_DATA_STATE_MASK;
1286
1287 /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */
1288 entry.portv_trunkid = (to_port & 0x0f) << 4;
1289 entry.portv_trunkid |= from_port & 0x0f;
1290
Vivien Didelotfad09c72016-06-21 12:28:20 -04001291 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001292}
1293
Vivien Didelotfad09c72016-06-21 12:28:20 -04001294static int _mv88e6xxx_atu_remove(struct mv88e6xxx_chip *chip, u16 fid,
Andrew Lunn158bc062016-04-28 21:24:06 -04001295 int port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001296{
1297 /* Destination port 0xF means remove the entries */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001298 return _mv88e6xxx_atu_move(chip, fid, port, 0x0f, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001299}
1300
Vivien Didelotfad09c72016-06-21 12:28:20 -04001301static int _mv88e6xxx_port_based_vlan_map(struct mv88e6xxx_chip *chip, int port)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001302{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001303 struct net_device *bridge = chip->ports[port].bridge_dev;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001304 struct dsa_switch *ds = chip->ds;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001305 u16 output_ports = 0;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001306 int i;
1307
1308 /* allow CPU port or DSA link(s) to send frames to every port */
1309 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
Vivien Didelot5a7921f2016-11-04 03:23:28 +01001310 output_ports = ~0;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001311 } else {
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001312 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001313 /* allow sending frames to every group member */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001314 if (bridge && chip->ports[i].bridge_dev == bridge)
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001315 output_ports |= BIT(i);
1316
1317 /* allow sending frames to CPU port and DSA link(s) */
1318 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
1319 output_ports |= BIT(i);
1320 }
1321 }
1322
1323 /* prevent frames from going back out of the port they came in on */
1324 output_ports &= ~BIT(port);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001325
Vivien Didelot5a7921f2016-11-04 03:23:28 +01001326 return mv88e6xxx_port_set_vlan_map(chip, port, output_ports);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001327}
1328
Vivien Didelotf81ec902016-05-09 13:22:58 -04001329static void mv88e6xxx_port_stp_state_set(struct dsa_switch *ds, int port,
1330 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001331{
Vivien Didelot04bed142016-08-31 18:06:13 -04001332 struct mv88e6xxx_chip *chip = ds->priv;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001333 int stp_state;
Vivien Didelot553eb542016-05-13 20:38:23 -04001334 int err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001335
1336 switch (state) {
1337 case BR_STATE_DISABLED:
Andrew Lunncca8b132015-04-02 04:06:39 +02001338 stp_state = PORT_CONTROL_STATE_DISABLED;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001339 break;
1340 case BR_STATE_BLOCKING:
1341 case BR_STATE_LISTENING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001342 stp_state = PORT_CONTROL_STATE_BLOCKING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001343 break;
1344 case BR_STATE_LEARNING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001345 stp_state = PORT_CONTROL_STATE_LEARNING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001346 break;
1347 case BR_STATE_FORWARDING:
1348 default:
Andrew Lunncca8b132015-04-02 04:06:39 +02001349 stp_state = PORT_CONTROL_STATE_FORWARDING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001350 break;
1351 }
1352
Vivien Didelotfad09c72016-06-21 12:28:20 -04001353 mutex_lock(&chip->reg_lock);
Vivien Didelote28def332016-11-04 03:23:27 +01001354 err = mv88e6xxx_port_set_state(chip, port, stp_state);
Vivien Didelotfad09c72016-06-21 12:28:20 -04001355 mutex_unlock(&chip->reg_lock);
Vivien Didelot553eb542016-05-13 20:38:23 -04001356
1357 if (err)
Vivien Didelote28def332016-11-04 03:23:27 +01001358 netdev_err(ds->ports[port].netdev, "failed to update state\n");
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001359}
1360
Vivien Didelot749efcb2016-09-22 16:49:24 -04001361static void mv88e6xxx_port_fast_age(struct dsa_switch *ds, int port)
1362{
1363 struct mv88e6xxx_chip *chip = ds->priv;
1364 int err;
1365
1366 mutex_lock(&chip->reg_lock);
1367 err = _mv88e6xxx_atu_remove(chip, 0, port, false);
1368 mutex_unlock(&chip->reg_lock);
1369
1370 if (err)
1371 netdev_err(ds->ports[port].netdev, "failed to flush ATU\n");
1372}
1373
Vivien Didelotfad09c72016-06-21 12:28:20 -04001374static int _mv88e6xxx_vtu_wait(struct mv88e6xxx_chip *chip)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001375{
Vivien Didelota935c052016-09-29 12:21:53 -04001376 return mv88e6xxx_g1_wait(chip, GLOBAL_VTU_OP, GLOBAL_VTU_OP_BUSY);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001377}
1378
Vivien Didelotfad09c72016-06-21 12:28:20 -04001379static int _mv88e6xxx_vtu_cmd(struct mv88e6xxx_chip *chip, u16 op)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001380{
Vivien Didelota935c052016-09-29 12:21:53 -04001381 int err;
Vivien Didelot6b17e862015-08-13 12:52:18 -04001382
Vivien Didelota935c052016-09-29 12:21:53 -04001383 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_OP, op);
1384 if (err)
1385 return err;
Vivien Didelot6b17e862015-08-13 12:52:18 -04001386
Vivien Didelotfad09c72016-06-21 12:28:20 -04001387 return _mv88e6xxx_vtu_wait(chip);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001388}
1389
Vivien Didelotfad09c72016-06-21 12:28:20 -04001390static int _mv88e6xxx_vtu_stu_flush(struct mv88e6xxx_chip *chip)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001391{
1392 int ret;
1393
Vivien Didelotfad09c72016-06-21 12:28:20 -04001394 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001395 if (ret < 0)
1396 return ret;
1397
Vivien Didelotfad09c72016-06-21 12:28:20 -04001398 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_FLUSH_ALL);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001399}
1400
Vivien Didelotfad09c72016-06-21 12:28:20 -04001401static int _mv88e6xxx_vtu_stu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001402 struct mv88e6xxx_vtu_entry *entry,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001403 unsigned int nibble_offset)
1404{
Vivien Didelotb8fee952015-08-13 12:52:19 -04001405 u16 regs[3];
Vivien Didelota935c052016-09-29 12:21:53 -04001406 int i, err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001407
1408 for (i = 0; i < 3; ++i) {
Vivien Didelota935c052016-09-29 12:21:53 -04001409 u16 *reg = &regs[i];
Vivien Didelotb8fee952015-08-13 12:52:19 -04001410
Vivien Didelota935c052016-09-29 12:21:53 -04001411 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_DATA_0_3 + i, reg);
1412 if (err)
1413 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001414 }
1415
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001416 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotb8fee952015-08-13 12:52:19 -04001417 unsigned int shift = (i % 4) * 4 + nibble_offset;
1418 u16 reg = regs[i / 4];
1419
1420 entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
1421 }
1422
1423 return 0;
1424}
1425
Vivien Didelotfad09c72016-06-21 12:28:20 -04001426static int mv88e6xxx_vtu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001427 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001428{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001429 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 0);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001430}
1431
Vivien Didelotfad09c72016-06-21 12:28:20 -04001432static int mv88e6xxx_stu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001433 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001434{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001435 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 2);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001436}
1437
Vivien Didelotfad09c72016-06-21 12:28:20 -04001438static int _mv88e6xxx_vtu_stu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001439 struct mv88e6xxx_vtu_entry *entry,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001440 unsigned int nibble_offset)
1441{
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001442 u16 regs[3] = { 0 };
Vivien Didelota935c052016-09-29 12:21:53 -04001443 int i, err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001444
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001445 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001446 unsigned int shift = (i % 4) * 4 + nibble_offset;
1447 u8 data = entry->data[i];
1448
1449 regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
1450 }
1451
1452 for (i = 0; i < 3; ++i) {
Vivien Didelota935c052016-09-29 12:21:53 -04001453 u16 reg = regs[i];
1454
1455 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_DATA_0_3 + i, reg);
1456 if (err)
1457 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001458 }
1459
1460 return 0;
1461}
1462
Vivien Didelotfad09c72016-06-21 12:28:20 -04001463static int mv88e6xxx_vtu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001464 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001465{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001466 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 0);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001467}
1468
Vivien Didelotfad09c72016-06-21 12:28:20 -04001469static int mv88e6xxx_stu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001470 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001471{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001472 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 2);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001473}
1474
Vivien Didelotfad09c72016-06-21 12:28:20 -04001475static int _mv88e6xxx_vtu_vid_write(struct mv88e6xxx_chip *chip, u16 vid)
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001476{
Vivien Didelota935c052016-09-29 12:21:53 -04001477 return mv88e6xxx_g1_write(chip, GLOBAL_VTU_VID,
1478 vid & GLOBAL_VTU_VID_MASK);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001479}
1480
Vivien Didelotfad09c72016-06-21 12:28:20 -04001481static int _mv88e6xxx_vtu_getnext(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001482 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelotb8fee952015-08-13 12:52:19 -04001483{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001484 struct mv88e6xxx_vtu_entry next = { 0 };
Vivien Didelota935c052016-09-29 12:21:53 -04001485 u16 val;
1486 int err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001487
Vivien Didelota935c052016-09-29 12:21:53 -04001488 err = _mv88e6xxx_vtu_wait(chip);
1489 if (err)
1490 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001491
Vivien Didelota935c052016-09-29 12:21:53 -04001492 err = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_VTU_GET_NEXT);
1493 if (err)
1494 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001495
Vivien Didelota935c052016-09-29 12:21:53 -04001496 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_VID, &val);
1497 if (err)
1498 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001499
Vivien Didelota935c052016-09-29 12:21:53 -04001500 next.vid = val & GLOBAL_VTU_VID_MASK;
1501 next.valid = !!(val & GLOBAL_VTU_VID_VALID);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001502
1503 if (next.valid) {
Vivien Didelota935c052016-09-29 12:21:53 -04001504 err = mv88e6xxx_vtu_data_read(chip, &next);
1505 if (err)
1506 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001507
Vivien Didelot6dc10bb2016-09-29 12:21:55 -04001508 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G1_VTU_FID)) {
Vivien Didelota935c052016-09-29 12:21:53 -04001509 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_FID, &val);
1510 if (err)
1511 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001512
Vivien Didelota935c052016-09-29 12:21:53 -04001513 next.fid = val & GLOBAL_VTU_FID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001514 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001515 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1516 * VTU DBNum[3:0] are located in VTU Operation 3:0
1517 */
Vivien Didelota935c052016-09-29 12:21:53 -04001518 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_OP, &val);
1519 if (err)
1520 return err;
Vivien Didelot11ea8092016-03-31 16:53:44 -04001521
Vivien Didelota935c052016-09-29 12:21:53 -04001522 next.fid = (val & 0xf00) >> 4;
1523 next.fid |= val & 0xf;
Vivien Didelot2e7bd5e2016-03-31 16:53:41 -04001524 }
Vivien Didelotb8fee952015-08-13 12:52:19 -04001525
Vivien Didelotfad09c72016-06-21 12:28:20 -04001526 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
Vivien Didelota935c052016-09-29 12:21:53 -04001527 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_SID, &val);
1528 if (err)
1529 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001530
Vivien Didelota935c052016-09-29 12:21:53 -04001531 next.sid = val & GLOBAL_VTU_SID_MASK;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001532 }
1533 }
1534
1535 *entry = next;
1536 return 0;
1537}
1538
Vivien Didelotf81ec902016-05-09 13:22:58 -04001539static int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
1540 struct switchdev_obj_port_vlan *vlan,
1541 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001542{
Vivien Didelot04bed142016-08-31 18:06:13 -04001543 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001544 struct mv88e6xxx_vtu_entry next;
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001545 u16 pvid;
1546 int err;
1547
Vivien Didelotfad09c72016-06-21 12:28:20 -04001548 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001549 return -EOPNOTSUPP;
1550
Vivien Didelotfad09c72016-06-21 12:28:20 -04001551 mutex_lock(&chip->reg_lock);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001552
Vivien Didelot77064f32016-11-04 03:23:30 +01001553 err = mv88e6xxx_port_get_pvid(chip, port, &pvid);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001554 if (err)
1555 goto unlock;
1556
Vivien Didelotfad09c72016-06-21 12:28:20 -04001557 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001558 if (err)
1559 goto unlock;
1560
1561 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001562 err = _mv88e6xxx_vtu_getnext(chip, &next);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001563 if (err)
1564 break;
1565
1566 if (!next.valid)
1567 break;
1568
1569 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1570 continue;
1571
1572 /* reinit and dump this VLAN obj */
Vivien Didelot57d32312016-06-20 13:13:58 -04001573 vlan->vid_begin = next.vid;
1574 vlan->vid_end = next.vid;
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001575 vlan->flags = 0;
1576
1577 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1578 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1579
1580 if (next.vid == pvid)
1581 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1582
1583 err = cb(&vlan->obj);
1584 if (err)
1585 break;
1586 } while (next.vid < GLOBAL_VTU_VID_MASK);
1587
1588unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001589 mutex_unlock(&chip->reg_lock);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001590
1591 return err;
1592}
1593
Vivien Didelotfad09c72016-06-21 12:28:20 -04001594static int _mv88e6xxx_vtu_loadpurge(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001595 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001596{
Vivien Didelot11ea8092016-03-31 16:53:44 -04001597 u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001598 u16 reg = 0;
Vivien Didelota935c052016-09-29 12:21:53 -04001599 int err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001600
Vivien Didelota935c052016-09-29 12:21:53 -04001601 err = _mv88e6xxx_vtu_wait(chip);
1602 if (err)
1603 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001604
1605 if (!entry->valid)
1606 goto loadpurge;
1607
1608 /* Write port member tags */
Vivien Didelota935c052016-09-29 12:21:53 -04001609 err = mv88e6xxx_vtu_data_write(chip, entry);
1610 if (err)
1611 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001612
Vivien Didelotfad09c72016-06-21 12:28:20 -04001613 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001614 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Vivien Didelota935c052016-09-29 12:21:53 -04001615 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_SID, reg);
1616 if (err)
1617 return err;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001618 }
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001619
Vivien Didelot6dc10bb2016-09-29 12:21:55 -04001620 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G1_VTU_FID)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001621 reg = entry->fid & GLOBAL_VTU_FID_MASK;
Vivien Didelota935c052016-09-29 12:21:53 -04001622 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_FID, reg);
1623 if (err)
1624 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001625 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001626 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1627 * VTU DBNum[3:0] are located in VTU Operation 3:0
1628 */
1629 op |= (entry->fid & 0xf0) << 8;
1630 op |= entry->fid & 0xf;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001631 }
1632
1633 reg = GLOBAL_VTU_VID_VALID;
1634loadpurge:
1635 reg |= entry->vid & GLOBAL_VTU_VID_MASK;
Vivien Didelota935c052016-09-29 12:21:53 -04001636 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_VID, reg);
1637 if (err)
1638 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001639
Vivien Didelotfad09c72016-06-21 12:28:20 -04001640 return _mv88e6xxx_vtu_cmd(chip, op);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001641}
1642
Vivien Didelotfad09c72016-06-21 12:28:20 -04001643static int _mv88e6xxx_stu_getnext(struct mv88e6xxx_chip *chip, u8 sid,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001644 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001645{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001646 struct mv88e6xxx_vtu_entry next = { 0 };
Vivien Didelota935c052016-09-29 12:21:53 -04001647 u16 val;
1648 int err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001649
Vivien Didelota935c052016-09-29 12:21:53 -04001650 err = _mv88e6xxx_vtu_wait(chip);
1651 if (err)
1652 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001653
Vivien Didelota935c052016-09-29 12:21:53 -04001654 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_SID,
1655 sid & GLOBAL_VTU_SID_MASK);
1656 if (err)
1657 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001658
Vivien Didelota935c052016-09-29 12:21:53 -04001659 err = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_GET_NEXT);
1660 if (err)
1661 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001662
Vivien Didelota935c052016-09-29 12:21:53 -04001663 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_SID, &val);
1664 if (err)
1665 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001666
Vivien Didelota935c052016-09-29 12:21:53 -04001667 next.sid = val & GLOBAL_VTU_SID_MASK;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001668
Vivien Didelota935c052016-09-29 12:21:53 -04001669 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_VID, &val);
1670 if (err)
1671 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001672
Vivien Didelota935c052016-09-29 12:21:53 -04001673 next.valid = !!(val & GLOBAL_VTU_VID_VALID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001674
1675 if (next.valid) {
Vivien Didelota935c052016-09-29 12:21:53 -04001676 err = mv88e6xxx_stu_data_read(chip, &next);
1677 if (err)
1678 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001679 }
1680
1681 *entry = next;
1682 return 0;
1683}
1684
Vivien Didelotfad09c72016-06-21 12:28:20 -04001685static int _mv88e6xxx_stu_loadpurge(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001686 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001687{
1688 u16 reg = 0;
Vivien Didelota935c052016-09-29 12:21:53 -04001689 int err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001690
Vivien Didelota935c052016-09-29 12:21:53 -04001691 err = _mv88e6xxx_vtu_wait(chip);
1692 if (err)
1693 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001694
1695 if (!entry->valid)
1696 goto loadpurge;
1697
1698 /* Write port states */
Vivien Didelota935c052016-09-29 12:21:53 -04001699 err = mv88e6xxx_stu_data_write(chip, entry);
1700 if (err)
1701 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001702
1703 reg = GLOBAL_VTU_VID_VALID;
1704loadpurge:
Vivien Didelota935c052016-09-29 12:21:53 -04001705 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_VID, reg);
1706 if (err)
1707 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001708
1709 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Vivien Didelota935c052016-09-29 12:21:53 -04001710 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_SID, reg);
1711 if (err)
1712 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001713
Vivien Didelotfad09c72016-06-21 12:28:20 -04001714 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_LOAD_PURGE);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001715}
1716
Vivien Didelotfad09c72016-06-21 12:28:20 -04001717static int _mv88e6xxx_fid_new(struct mv88e6xxx_chip *chip, u16 *fid)
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001718{
1719 DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001720 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001721 int i, err;
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001722
1723 bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);
1724
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001725 /* Set every FID bit used by the (un)bridged ports */
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001726 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotb4e48c52016-11-04 03:23:29 +01001727 err = mv88e6xxx_port_get_fid(chip, i, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001728 if (err)
1729 return err;
1730
1731 set_bit(*fid, fid_bitmap);
1732 }
1733
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001734 /* Set every FID bit used by the VLAN entries */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001735 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001736 if (err)
1737 return err;
1738
1739 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001740 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001741 if (err)
1742 return err;
1743
1744 if (!vlan.valid)
1745 break;
1746
1747 set_bit(vlan.fid, fid_bitmap);
1748 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
1749
1750 /* The reset value 0x000 is used to indicate that multiple address
1751 * databases are not needed. Return the next positive available.
1752 */
1753 *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1);
Vivien Didelotfad09c72016-06-21 12:28:20 -04001754 if (unlikely(*fid >= mv88e6xxx_num_databases(chip)))
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001755 return -ENOSPC;
1756
1757 /* Clear the database */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001758 return _mv88e6xxx_atu_flush(chip, *fid, true);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001759}
1760
Vivien Didelotfad09c72016-06-21 12:28:20 -04001761static int _mv88e6xxx_vtu_new(struct mv88e6xxx_chip *chip, u16 vid,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001762 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001763{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001764 struct dsa_switch *ds = chip->ds;
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001765 struct mv88e6xxx_vtu_entry vlan = {
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001766 .valid = true,
1767 .vid = vid,
1768 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001769 int i, err;
1770
Vivien Didelotfad09c72016-06-21 12:28:20 -04001771 err = _mv88e6xxx_fid_new(chip, &vlan.fid);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001772 if (err)
1773 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001774
Vivien Didelot3d131f02015-11-03 10:52:52 -05001775 /* exclude all ports except the CPU and DSA ports */
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001776 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i)
Vivien Didelot3d131f02015-11-03 10:52:52 -05001777 vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)
1778 ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED
1779 : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001780
Vivien Didelotfad09c72016-06-21 12:28:20 -04001781 if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
1782 mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip)) {
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001783 struct mv88e6xxx_vtu_entry vstp;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001784
1785 /* Adding a VTU entry requires a valid STU entry. As VSTP is not
1786 * implemented, only one STU entry is needed to cover all VTU
1787 * entries. Thus, validate the SID 0.
1788 */
1789 vlan.sid = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001790 err = _mv88e6xxx_stu_getnext(chip, GLOBAL_VTU_SID_MASK, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001791 if (err)
1792 return err;
1793
1794 if (vstp.sid != vlan.sid || !vstp.valid) {
1795 memset(&vstp, 0, sizeof(vstp));
1796 vstp.valid = true;
1797 vstp.sid = vlan.sid;
1798
Vivien Didelotfad09c72016-06-21 12:28:20 -04001799 err = _mv88e6xxx_stu_loadpurge(chip, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001800 if (err)
1801 return err;
1802 }
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001803 }
1804
1805 *entry = vlan;
1806 return 0;
1807}
1808
Vivien Didelotfad09c72016-06-21 12:28:20 -04001809static int _mv88e6xxx_vtu_get(struct mv88e6xxx_chip *chip, u16 vid,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001810 struct mv88e6xxx_vtu_entry *entry, bool creat)
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001811{
1812 int err;
1813
1814 if (!vid)
1815 return -EINVAL;
1816
Vivien Didelotfad09c72016-06-21 12:28:20 -04001817 err = _mv88e6xxx_vtu_vid_write(chip, vid - 1);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001818 if (err)
1819 return err;
1820
Vivien Didelotfad09c72016-06-21 12:28:20 -04001821 err = _mv88e6xxx_vtu_getnext(chip, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001822 if (err)
1823 return err;
1824
1825 if (entry->vid != vid || !entry->valid) {
1826 if (!creat)
1827 return -EOPNOTSUPP;
1828 /* -ENOENT would've been more appropriate, but switchdev expects
1829 * -EOPNOTSUPP to inform bridge about an eventual software VLAN.
1830 */
1831
Vivien Didelotfad09c72016-06-21 12:28:20 -04001832 err = _mv88e6xxx_vtu_new(chip, vid, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001833 }
1834
1835 return err;
1836}
1837
Vivien Didelotda9c3592016-02-12 12:09:40 -05001838static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
1839 u16 vid_begin, u16 vid_end)
1840{
Vivien Didelot04bed142016-08-31 18:06:13 -04001841 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001842 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelotda9c3592016-02-12 12:09:40 -05001843 int i, err;
1844
1845 if (!vid_begin)
1846 return -EOPNOTSUPP;
1847
Vivien Didelotfad09c72016-06-21 12:28:20 -04001848 mutex_lock(&chip->reg_lock);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001849
Vivien Didelotfad09c72016-06-21 12:28:20 -04001850 err = _mv88e6xxx_vtu_vid_write(chip, vid_begin - 1);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001851 if (err)
1852 goto unlock;
1853
1854 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001855 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001856 if (err)
1857 goto unlock;
1858
1859 if (!vlan.valid)
1860 break;
1861
1862 if (vlan.vid > vid_end)
1863 break;
1864
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001865 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotda9c3592016-02-12 12:09:40 -05001866 if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i))
1867 continue;
1868
1869 if (vlan.data[i] ==
1870 GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1871 continue;
1872
Vivien Didelotfad09c72016-06-21 12:28:20 -04001873 if (chip->ports[i].bridge_dev ==
1874 chip->ports[port].bridge_dev)
Vivien Didelotda9c3592016-02-12 12:09:40 -05001875 break; /* same bridge, check next VLAN */
1876
Andrew Lunnc8b09802016-06-04 21:16:57 +02001877 netdev_warn(ds->ports[port].netdev,
Vivien Didelotda9c3592016-02-12 12:09:40 -05001878 "hardware VLAN %d already used by %s\n",
1879 vlan.vid,
Vivien Didelotfad09c72016-06-21 12:28:20 -04001880 netdev_name(chip->ports[i].bridge_dev));
Vivien Didelotda9c3592016-02-12 12:09:40 -05001881 err = -EOPNOTSUPP;
1882 goto unlock;
1883 }
1884 } while (vlan.vid < vid_end);
1885
1886unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001887 mutex_unlock(&chip->reg_lock);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001888
1889 return err;
1890}
1891
Vivien Didelotf81ec902016-05-09 13:22:58 -04001892static int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port,
1893 bool vlan_filtering)
Vivien Didelot214cdb92016-02-26 13:16:08 -05001894{
Vivien Didelot04bed142016-08-31 18:06:13 -04001895 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot385a0992016-11-04 03:23:31 +01001896 u16 mode = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE :
Vivien Didelot214cdb92016-02-26 13:16:08 -05001897 PORT_CONTROL_2_8021Q_DISABLED;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001898 int err;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001899
Vivien Didelotfad09c72016-06-21 12:28:20 -04001900 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001901 return -EOPNOTSUPP;
1902
Vivien Didelotfad09c72016-06-21 12:28:20 -04001903 mutex_lock(&chip->reg_lock);
Vivien Didelot385a0992016-11-04 03:23:31 +01001904 err = mv88e6xxx_port_set_8021q_mode(chip, port, mode);
Vivien Didelotfad09c72016-06-21 12:28:20 -04001905 mutex_unlock(&chip->reg_lock);
Vivien Didelot214cdb92016-02-26 13:16:08 -05001906
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001907 return err;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001908}
1909
Vivien Didelot57d32312016-06-20 13:13:58 -04001910static int
1911mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
1912 const struct switchdev_obj_port_vlan *vlan,
1913 struct switchdev_trans *trans)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001914{
Vivien Didelot04bed142016-08-31 18:06:13 -04001915 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotda9c3592016-02-12 12:09:40 -05001916 int err;
1917
Vivien Didelotfad09c72016-06-21 12:28:20 -04001918 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001919 return -EOPNOTSUPP;
1920
Vivien Didelotda9c3592016-02-12 12:09:40 -05001921 /* If the requested port doesn't belong to the same bridge as the VLAN
1922 * members, do not support it (yet) and fallback to software VLAN.
1923 */
1924 err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin,
1925 vlan->vid_end);
1926 if (err)
1927 return err;
1928
Vivien Didelot76e398a2015-11-01 12:33:55 -05001929 /* We don't need any dynamic resource from the kernel (yet),
1930 * so skip the prepare phase.
1931 */
1932 return 0;
1933}
1934
Vivien Didelotfad09c72016-06-21 12:28:20 -04001935static int _mv88e6xxx_port_vlan_add(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001936 u16 vid, bool untagged)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001937{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001938 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001939 int err;
1940
Vivien Didelotfad09c72016-06-21 12:28:20 -04001941 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, true);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001942 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001943 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001944
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001945 vlan.data[port] = untagged ?
1946 GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
1947 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
1948
Vivien Didelotfad09c72016-06-21 12:28:20 -04001949 return _mv88e6xxx_vtu_loadpurge(chip, &vlan);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001950}
1951
Vivien Didelotf81ec902016-05-09 13:22:58 -04001952static void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
1953 const struct switchdev_obj_port_vlan *vlan,
1954 struct switchdev_trans *trans)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001955{
Vivien Didelot04bed142016-08-31 18:06:13 -04001956 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001957 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1958 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1959 u16 vid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001960
Vivien Didelotfad09c72016-06-21 12:28:20 -04001961 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001962 return;
1963
Vivien Didelotfad09c72016-06-21 12:28:20 -04001964 mutex_lock(&chip->reg_lock);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001965
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001966 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
Vivien Didelotfad09c72016-06-21 12:28:20 -04001967 if (_mv88e6xxx_port_vlan_add(chip, port, vid, untagged))
Andrew Lunnc8b09802016-06-04 21:16:57 +02001968 netdev_err(ds->ports[port].netdev,
1969 "failed to add VLAN %d%c\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001970 vid, untagged ? 'u' : 't');
Vivien Didelot76e398a2015-11-01 12:33:55 -05001971
Vivien Didelot77064f32016-11-04 03:23:30 +01001972 if (pvid && mv88e6xxx_port_set_pvid(chip, port, vlan->vid_end))
Andrew Lunnc8b09802016-06-04 21:16:57 +02001973 netdev_err(ds->ports[port].netdev, "failed to set PVID %d\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001974 vlan->vid_end);
1975
Vivien Didelotfad09c72016-06-21 12:28:20 -04001976 mutex_unlock(&chip->reg_lock);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001977}
1978
Vivien Didelotfad09c72016-06-21 12:28:20 -04001979static int _mv88e6xxx_port_vlan_del(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001980 int port, u16 vid)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001981{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001982 struct dsa_switch *ds = chip->ds;
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001983 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001984 int i, err;
1985
Vivien Didelotfad09c72016-06-21 12:28:20 -04001986 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001987 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001988 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001989
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001990 /* Tell switchdev if this VLAN is handled in software */
1991 if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
Vivien Didelot3c06f082016-02-05 14:04:39 -05001992 return -EOPNOTSUPP;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001993
1994 vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
1995
1996 /* keep the VLAN unless all ports are excluded */
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001997 vlan.valid = false;
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001998 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelot3d131f02015-11-03 10:52:52 -05001999 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002000 continue;
2001
2002 if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002003 vlan.valid = true;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002004 break;
2005 }
2006 }
2007
Vivien Didelotfad09c72016-06-21 12:28:20 -04002008 err = _mv88e6xxx_vtu_loadpurge(chip, &vlan);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002009 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002010 return err;
2011
Vivien Didelotfad09c72016-06-21 12:28:20 -04002012 return _mv88e6xxx_atu_remove(chip, vlan.fid, port, false);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002013}
2014
Vivien Didelotf81ec902016-05-09 13:22:58 -04002015static int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
2016 const struct switchdev_obj_port_vlan *vlan)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002017{
Vivien Didelot04bed142016-08-31 18:06:13 -04002018 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot76e398a2015-11-01 12:33:55 -05002019 u16 pvid, vid;
2020 int err = 0;
2021
Vivien Didelotfad09c72016-06-21 12:28:20 -04002022 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04002023 return -EOPNOTSUPP;
2024
Vivien Didelotfad09c72016-06-21 12:28:20 -04002025 mutex_lock(&chip->reg_lock);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002026
Vivien Didelot77064f32016-11-04 03:23:30 +01002027 err = mv88e6xxx_port_get_pvid(chip, port, &pvid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002028 if (err)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002029 goto unlock;
2030
Vivien Didelot76e398a2015-11-01 12:33:55 -05002031 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002032 err = _mv88e6xxx_port_vlan_del(chip, port, vid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002033 if (err)
2034 goto unlock;
2035
2036 if (vid == pvid) {
Vivien Didelot77064f32016-11-04 03:23:30 +01002037 err = mv88e6xxx_port_set_pvid(chip, port, 0);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002038 if (err)
2039 goto unlock;
2040 }
2041 }
2042
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002043unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002044 mutex_unlock(&chip->reg_lock);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002045
2046 return err;
2047}
2048
Vivien Didelotfad09c72016-06-21 12:28:20 -04002049static int _mv88e6xxx_atu_mac_write(struct mv88e6xxx_chip *chip,
Vivien Didelotc5723ac2015-08-10 09:09:48 -04002050 const unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002051{
Vivien Didelota935c052016-09-29 12:21:53 -04002052 int i, err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002053
2054 for (i = 0; i < 3; i++) {
Vivien Didelota935c052016-09-29 12:21:53 -04002055 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_MAC_01 + i,
2056 (addr[i * 2] << 8) | addr[i * 2 + 1]);
2057 if (err)
2058 return err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002059 }
2060
2061 return 0;
2062}
2063
Vivien Didelotfad09c72016-06-21 12:28:20 -04002064static int _mv88e6xxx_atu_mac_read(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04002065 unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002066{
Vivien Didelota935c052016-09-29 12:21:53 -04002067 u16 val;
2068 int i, err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002069
2070 for (i = 0; i < 3; i++) {
Vivien Didelota935c052016-09-29 12:21:53 -04002071 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_MAC_01 + i, &val);
2072 if (err)
2073 return err;
2074
2075 addr[i * 2] = val >> 8;
2076 addr[i * 2 + 1] = val & 0xff;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002077 }
2078
2079 return 0;
2080}
2081
Vivien Didelotfad09c72016-06-21 12:28:20 -04002082static int _mv88e6xxx_atu_load(struct mv88e6xxx_chip *chip,
Vivien Didelotfd231c82015-08-10 09:09:50 -04002083 struct mv88e6xxx_atu_entry *entry)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002084{
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002085 int ret;
2086
Vivien Didelotfad09c72016-06-21 12:28:20 -04002087 ret = _mv88e6xxx_atu_wait(chip);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002088 if (ret < 0)
2089 return ret;
2090
Vivien Didelotfad09c72016-06-21 12:28:20 -04002091 ret = _mv88e6xxx_atu_mac_write(chip, entry->mac);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002092 if (ret < 0)
2093 return ret;
2094
Vivien Didelotfad09c72016-06-21 12:28:20 -04002095 ret = _mv88e6xxx_atu_data_write(chip, entry);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002096 if (ret < 0)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002097 return ret;
2098
Vivien Didelotfad09c72016-06-21 12:28:20 -04002099 return _mv88e6xxx_atu_cmd(chip, entry->fid, GLOBAL_ATU_OP_LOAD_DB);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002100}
David S. Millercdf09692015-08-11 12:00:37 -07002101
Vivien Didelot88472932016-09-19 19:56:11 -04002102static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
2103 struct mv88e6xxx_atu_entry *entry);
2104
2105static int mv88e6xxx_atu_get(struct mv88e6xxx_chip *chip, int fid,
2106 const u8 *addr, struct mv88e6xxx_atu_entry *entry)
2107{
2108 struct mv88e6xxx_atu_entry next;
2109 int err;
2110
2111 eth_broadcast_addr(next.mac);
2112
2113 err = _mv88e6xxx_atu_mac_write(chip, next.mac);
2114 if (err)
2115 return err;
2116
2117 do {
2118 err = _mv88e6xxx_atu_getnext(chip, fid, &next);
2119 if (err)
2120 return err;
2121
2122 if (next.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2123 break;
2124
2125 if (ether_addr_equal(next.mac, addr)) {
2126 *entry = next;
2127 return 0;
2128 }
2129 } while (!is_broadcast_ether_addr(next.mac));
2130
2131 memset(entry, 0, sizeof(*entry));
2132 entry->fid = fid;
2133 ether_addr_copy(entry->mac, addr);
2134
2135 return 0;
2136}
2137
Vivien Didelot83dabd12016-08-31 11:50:04 -04002138static int mv88e6xxx_port_db_load_purge(struct mv88e6xxx_chip *chip, int port,
2139 const unsigned char *addr, u16 vid,
2140 u8 state)
Vivien Didelotfd231c82015-08-10 09:09:50 -04002141{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04002142 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelot88472932016-09-19 19:56:11 -04002143 struct mv88e6xxx_atu_entry entry;
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002144 int err;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002145
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002146 /* Null VLAN ID corresponds to the port private database */
2147 if (vid == 0)
Vivien Didelotb4e48c52016-11-04 03:23:29 +01002148 err = mv88e6xxx_port_get_fid(chip, port, &vlan.fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002149 else
Vivien Didelotfad09c72016-06-21 12:28:20 -04002150 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002151 if (err)
2152 return err;
2153
Vivien Didelot88472932016-09-19 19:56:11 -04002154 err = mv88e6xxx_atu_get(chip, vlan.fid, addr, &entry);
2155 if (err)
2156 return err;
2157
2158 /* Purge the ATU entry only if no port is using it anymore */
2159 if (state == GLOBAL_ATU_DATA_STATE_UNUSED) {
2160 entry.portv_trunkid &= ~BIT(port);
2161 if (!entry.portv_trunkid)
2162 entry.state = GLOBAL_ATU_DATA_STATE_UNUSED;
2163 } else {
2164 entry.portv_trunkid |= BIT(port);
2165 entry.state = state;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002166 }
2167
Vivien Didelotfad09c72016-06-21 12:28:20 -04002168 return _mv88e6xxx_atu_load(chip, &entry);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002169}
2170
Vivien Didelotf81ec902016-05-09 13:22:58 -04002171static int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
2172 const struct switchdev_obj_port_fdb *fdb,
2173 struct switchdev_trans *trans)
Vivien Didelot146a3202015-10-08 11:35:12 -04002174{
2175 /* We don't need any dynamic resource from the kernel (yet),
2176 * so skip the prepare phase.
2177 */
2178 return 0;
2179}
2180
Vivien Didelotf81ec902016-05-09 13:22:58 -04002181static void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
2182 const struct switchdev_obj_port_fdb *fdb,
2183 struct switchdev_trans *trans)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002184{
Vivien Didelot04bed142016-08-31 18:06:13 -04002185 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot6630e232015-08-06 01:44:07 -04002186
Vivien Didelotfad09c72016-06-21 12:28:20 -04002187 mutex_lock(&chip->reg_lock);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002188 if (mv88e6xxx_port_db_load_purge(chip, port, fdb->addr, fdb->vid,
2189 GLOBAL_ATU_DATA_STATE_UC_STATIC))
2190 netdev_err(ds->ports[port].netdev, "failed to load unicast MAC address\n");
Vivien Didelotfad09c72016-06-21 12:28:20 -04002191 mutex_unlock(&chip->reg_lock);
David S. Millercdf09692015-08-11 12:00:37 -07002192}
2193
Vivien Didelotf81ec902016-05-09 13:22:58 -04002194static int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
2195 const struct switchdev_obj_port_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -07002196{
Vivien Didelot04bed142016-08-31 18:06:13 -04002197 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot83dabd12016-08-31 11:50:04 -04002198 int err;
David S. Millercdf09692015-08-11 12:00:37 -07002199
Vivien Didelotfad09c72016-06-21 12:28:20 -04002200 mutex_lock(&chip->reg_lock);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002201 err = mv88e6xxx_port_db_load_purge(chip, port, fdb->addr, fdb->vid,
2202 GLOBAL_ATU_DATA_STATE_UNUSED);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002203 mutex_unlock(&chip->reg_lock);
David S. Millercdf09692015-08-11 12:00:37 -07002204
Vivien Didelot83dabd12016-08-31 11:50:04 -04002205 return err;
David S. Millercdf09692015-08-11 12:00:37 -07002206}
2207
Vivien Didelotfad09c72016-06-21 12:28:20 -04002208static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
Vivien Didelot1d194042015-08-10 09:09:51 -04002209 struct mv88e6xxx_atu_entry *entry)
David S. Millercdf09692015-08-11 12:00:37 -07002210{
Vivien Didelot1d194042015-08-10 09:09:51 -04002211 struct mv88e6xxx_atu_entry next = { 0 };
Vivien Didelota935c052016-09-29 12:21:53 -04002212 u16 val;
2213 int err;
Vivien Didelot1d194042015-08-10 09:09:51 -04002214
2215 next.fid = fid;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002216
Vivien Didelota935c052016-09-29 12:21:53 -04002217 err = _mv88e6xxx_atu_wait(chip);
2218 if (err)
2219 return err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002220
Vivien Didelota935c052016-09-29 12:21:53 -04002221 err = _mv88e6xxx_atu_cmd(chip, fid, GLOBAL_ATU_OP_GET_NEXT_DB);
2222 if (err)
2223 return err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002224
Vivien Didelota935c052016-09-29 12:21:53 -04002225 err = _mv88e6xxx_atu_mac_read(chip, next.mac);
2226 if (err)
2227 return err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002228
Vivien Didelota935c052016-09-29 12:21:53 -04002229 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_DATA, &val);
2230 if (err)
2231 return err;
Vivien Didelot1d194042015-08-10 09:09:51 -04002232
Vivien Didelota935c052016-09-29 12:21:53 -04002233 next.state = val & GLOBAL_ATU_DATA_STATE_MASK;
Vivien Didelot1d194042015-08-10 09:09:51 -04002234 if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2235 unsigned int mask, shift;
2236
Vivien Didelota935c052016-09-29 12:21:53 -04002237 if (val & GLOBAL_ATU_DATA_TRUNK) {
Vivien Didelot1d194042015-08-10 09:09:51 -04002238 next.trunk = true;
2239 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
2240 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
2241 } else {
2242 next.trunk = false;
2243 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
2244 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
2245 }
2246
Vivien Didelota935c052016-09-29 12:21:53 -04002247 next.portv_trunkid = (val & mask) >> shift;
Vivien Didelot1d194042015-08-10 09:09:51 -04002248 }
2249
2250 *entry = next;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002251 return 0;
2252}
2253
Vivien Didelot83dabd12016-08-31 11:50:04 -04002254static int mv88e6xxx_port_db_dump_fid(struct mv88e6xxx_chip *chip,
2255 u16 fid, u16 vid, int port,
2256 struct switchdev_obj *obj,
2257 int (*cb)(struct switchdev_obj *obj))
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002258{
2259 struct mv88e6xxx_atu_entry addr = {
2260 .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2261 };
2262 int err;
2263
Vivien Didelotfad09c72016-06-21 12:28:20 -04002264 err = _mv88e6xxx_atu_mac_write(chip, addr.mac);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002265 if (err)
2266 return err;
2267
2268 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002269 err = _mv88e6xxx_atu_getnext(chip, fid, &addr);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002270 if (err)
Vivien Didelot83dabd12016-08-31 11:50:04 -04002271 return err;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002272
2273 if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2274 break;
2275
Vivien Didelot83dabd12016-08-31 11:50:04 -04002276 if (addr.trunk || (addr.portv_trunkid & BIT(port)) == 0)
2277 continue;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002278
Vivien Didelot83dabd12016-08-31 11:50:04 -04002279 if (obj->id == SWITCHDEV_OBJ_ID_PORT_FDB) {
2280 struct switchdev_obj_port_fdb *fdb;
2281
2282 if (!is_unicast_ether_addr(addr.mac))
2283 continue;
2284
2285 fdb = SWITCHDEV_OBJ_PORT_FDB(obj);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002286 fdb->vid = vid;
2287 ether_addr_copy(fdb->addr, addr.mac);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002288 if (addr.state == GLOBAL_ATU_DATA_STATE_UC_STATIC)
2289 fdb->ndm_state = NUD_NOARP;
2290 else
2291 fdb->ndm_state = NUD_REACHABLE;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04002292 } else if (obj->id == SWITCHDEV_OBJ_ID_PORT_MDB) {
2293 struct switchdev_obj_port_mdb *mdb;
2294
2295 if (!is_multicast_ether_addr(addr.mac))
2296 continue;
2297
2298 mdb = SWITCHDEV_OBJ_PORT_MDB(obj);
2299 mdb->vid = vid;
2300 ether_addr_copy(mdb->addr, addr.mac);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002301 } else {
2302 return -EOPNOTSUPP;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002303 }
Vivien Didelot83dabd12016-08-31 11:50:04 -04002304
2305 err = cb(obj);
2306 if (err)
2307 return err;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002308 } while (!is_broadcast_ether_addr(addr.mac));
2309
2310 return err;
2311}
2312
Vivien Didelot83dabd12016-08-31 11:50:04 -04002313static int mv88e6xxx_port_db_dump(struct mv88e6xxx_chip *chip, int port,
2314 struct switchdev_obj *obj,
2315 int (*cb)(struct switchdev_obj *obj))
2316{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04002317 struct mv88e6xxx_vtu_entry vlan = {
Vivien Didelot83dabd12016-08-31 11:50:04 -04002318 .vid = GLOBAL_VTU_VID_MASK, /* all ones */
2319 };
2320 u16 fid;
2321 int err;
2322
2323 /* Dump port's default Filtering Information Database (VLAN ID 0) */
Vivien Didelotb4e48c52016-11-04 03:23:29 +01002324 err = mv88e6xxx_port_get_fid(chip, port, &fid);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002325 if (err)
2326 return err;
2327
2328 err = mv88e6xxx_port_db_dump_fid(chip, fid, 0, port, obj, cb);
2329 if (err)
2330 return err;
2331
2332 /* Dump VLANs' Filtering Information Databases */
2333 err = _mv88e6xxx_vtu_vid_write(chip, vlan.vid);
2334 if (err)
2335 return err;
2336
2337 do {
2338 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
2339 if (err)
2340 return err;
2341
2342 if (!vlan.valid)
2343 break;
2344
2345 err = mv88e6xxx_port_db_dump_fid(chip, vlan.fid, vlan.vid, port,
2346 obj, cb);
2347 if (err)
2348 return err;
2349 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
2350
2351 return err;
2352}
2353
Vivien Didelotf81ec902016-05-09 13:22:58 -04002354static int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
2355 struct switchdev_obj_port_fdb *fdb,
2356 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotf33475b2015-10-22 09:34:41 -04002357{
Vivien Didelot04bed142016-08-31 18:06:13 -04002358 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002359 int err;
2360
Vivien Didelotfad09c72016-06-21 12:28:20 -04002361 mutex_lock(&chip->reg_lock);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002362 err = mv88e6xxx_port_db_dump(chip, port, &fdb->obj, cb);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002363 mutex_unlock(&chip->reg_lock);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002364
2365 return err;
2366}
2367
Vivien Didelotf81ec902016-05-09 13:22:58 -04002368static int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
2369 struct net_device *bridge)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002370{
Vivien Didelot04bed142016-08-31 18:06:13 -04002371 struct mv88e6xxx_chip *chip = ds->priv;
Colin Ian King1d9619d2016-04-25 23:11:22 +01002372 int i, err = 0;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002373
Vivien Didelotfad09c72016-06-21 12:28:20 -04002374 mutex_lock(&chip->reg_lock);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002375
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002376 /* Assign the bridge and remap each port's VLANTable */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002377 chip->ports[port].bridge_dev = bridge;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002378
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002379 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002380 if (chip->ports[i].bridge_dev == bridge) {
2381 err = _mv88e6xxx_port_based_vlan_map(chip, i);
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002382 if (err)
2383 break;
2384 }
2385 }
2386
Vivien Didelotfad09c72016-06-21 12:28:20 -04002387 mutex_unlock(&chip->reg_lock);
Vivien Didelota6692752016-02-12 12:09:39 -05002388
Vivien Didelot466dfa02016-02-26 13:16:05 -05002389 return err;
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002390}
2391
Vivien Didelotf81ec902016-05-09 13:22:58 -04002392static void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002393{
Vivien Didelot04bed142016-08-31 18:06:13 -04002394 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002395 struct net_device *bridge = chip->ports[port].bridge_dev;
Vivien Didelot16bfa702016-03-13 16:21:33 -04002396 int i;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002397
Vivien Didelotfad09c72016-06-21 12:28:20 -04002398 mutex_lock(&chip->reg_lock);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002399
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002400 /* Unassign the bridge and remap each port's VLANTable */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002401 chip->ports[port].bridge_dev = NULL;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002402
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002403 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002404 if (i == port || chip->ports[i].bridge_dev == bridge)
2405 if (_mv88e6xxx_port_based_vlan_map(chip, i))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002406 netdev_warn(ds->ports[i].netdev,
2407 "failed to remap\n");
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002408
Vivien Didelotfad09c72016-06-21 12:28:20 -04002409 mutex_unlock(&chip->reg_lock);
Vivien Didelot66d9cd02016-02-05 14:07:14 -05002410}
2411
Vivien Didelotfad09c72016-06-21 12:28:20 -04002412static int mv88e6xxx_switch_reset(struct mv88e6xxx_chip *chip)
Vivien Didelot552238b2016-05-09 13:22:49 -04002413{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002414 bool ppu_active = mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE);
Vivien Didelot552238b2016-05-09 13:22:49 -04002415 u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002416 struct gpio_desc *gpiod = chip->reset;
Vivien Didelot552238b2016-05-09 13:22:49 -04002417 unsigned long timeout;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002418 u16 reg;
Vivien Didelota935c052016-09-29 12:21:53 -04002419 int err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002420 int i;
2421
2422 /* Set all ports to the disabled state. */
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002423 for (i = 0; i < mv88e6xxx_num_ports(chip); i++) {
Vivien Didelote28def332016-11-04 03:23:27 +01002424 err = mv88e6xxx_port_set_state(chip, i,
2425 PORT_CONTROL_STATE_DISABLED);
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002426 if (err)
2427 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002428 }
2429
2430 /* Wait for transmit queues to drain. */
2431 usleep_range(2000, 4000);
2432
2433 /* If there is a gpio connected to the reset pin, toggle it */
2434 if (gpiod) {
2435 gpiod_set_value_cansleep(gpiod, 1);
2436 usleep_range(10000, 20000);
2437 gpiod_set_value_cansleep(gpiod, 0);
2438 usleep_range(10000, 20000);
2439 }
2440
2441 /* Reset the switch. Keep the PPU active if requested. The PPU
2442 * needs to be active to support indirect phy register access
2443 * through global registers 0x18 and 0x19.
2444 */
2445 if (ppu_active)
Vivien Didelota935c052016-09-29 12:21:53 -04002446 err = mv88e6xxx_g1_write(chip, 0x04, 0xc000);
Vivien Didelot552238b2016-05-09 13:22:49 -04002447 else
Vivien Didelota935c052016-09-29 12:21:53 -04002448 err = mv88e6xxx_g1_write(chip, 0x04, 0xc400);
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002449 if (err)
2450 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002451
2452 /* Wait up to one second for reset to complete. */
2453 timeout = jiffies + 1 * HZ;
2454 while (time_before(jiffies, timeout)) {
Vivien Didelota935c052016-09-29 12:21:53 -04002455 err = mv88e6xxx_g1_read(chip, 0x00, &reg);
2456 if (err)
2457 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002458
Vivien Didelota935c052016-09-29 12:21:53 -04002459 if ((reg & is_reset) == is_reset)
Vivien Didelot552238b2016-05-09 13:22:49 -04002460 break;
2461 usleep_range(1000, 2000);
2462 }
2463 if (time_after(jiffies, timeout))
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002464 err = -ETIMEDOUT;
Vivien Didelot552238b2016-05-09 13:22:49 -04002465 else
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002466 err = 0;
Vivien Didelot552238b2016-05-09 13:22:49 -04002467
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002468 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002469}
2470
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002471static int mv88e6xxx_serdes_power_on(struct mv88e6xxx_chip *chip)
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002472{
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002473 u16 val;
2474 int err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002475
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002476 /* Clear Power Down bit */
2477 err = mv88e6xxx_serdes_read(chip, MII_BMCR, &val);
2478 if (err)
2479 return err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002480
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002481 if (val & BMCR_PDOWN) {
2482 val &= ~BMCR_PDOWN;
2483 err = mv88e6xxx_serdes_write(chip, MII_BMCR, val);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002484 }
2485
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002486 return err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002487}
2488
Vivien Didelotfad09c72016-06-21 12:28:20 -04002489static int mv88e6xxx_setup_port(struct mv88e6xxx_chip *chip, int port)
Guenter Roeckd827e882015-03-26 18:36:29 -07002490{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002491 struct dsa_switch *ds = chip->ds;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002492 int err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002493 u16 reg;
Guenter Roeckd827e882015-03-26 18:36:29 -07002494
Vivien Didelotd78343d2016-11-04 03:23:36 +01002495 /* MAC Forcing register: don't force link, speed, duplex or flow control
2496 * state to any particular values on physical ports, but force the CPU
2497 * port and all DSA ports to their maximum bandwidth and full duplex.
2498 */
2499 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))
2500 err = mv88e6xxx_port_setup_mac(chip, port, LINK_FORCED_UP,
2501 SPEED_MAX, DUPLEX_FULL,
2502 PHY_INTERFACE_MODE_NA);
2503 else
2504 err = mv88e6xxx_port_setup_mac(chip, port, LINK_UNFORCED,
2505 SPEED_UNFORCED, DUPLEX_UNFORCED,
2506 PHY_INTERFACE_MODE_NA);
2507 if (err)
2508 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002509
2510 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
2511 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
2512 * tunneling, determine priority by looking at 802.1p and IP
2513 * priority fields (IP prio has precedence), and set STP state
2514 * to Forwarding.
2515 *
2516 * If this is the CPU link, use DSA or EDSA tagging depending
2517 * on which tagging mode was configured.
2518 *
2519 * If this is a link to another switch, use DSA tagging mode.
2520 *
2521 * If this is the upstream port for this switch, enable
2522 * forwarding of unknown unicasts and multicasts.
2523 */
2524 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002525 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2526 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2527 mv88e6xxx_6095_family(chip) || mv88e6xxx_6065_family(chip) ||
2528 mv88e6xxx_6185_family(chip) || mv88e6xxx_6320_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002529 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
2530 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
2531 PORT_CONTROL_STATE_FORWARDING;
2532 if (dsa_is_cpu_port(ds, port)) {
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002533 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA))
Andrew Lunn5377b802016-06-04 21:17:02 +02002534 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA |
Andrew Lunnc047a1f2015-09-29 01:50:56 +02002535 PORT_CONTROL_FORWARD_UNKNOWN_MC;
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002536 else
2537 reg |= PORT_CONTROL_DSA_TAG;
Jamie Lentinf027e0c2016-08-22 16:01:04 +02002538 reg |= PORT_CONTROL_EGRESS_ADD_TAG |
2539 PORT_CONTROL_FORWARD_UNKNOWN;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002540 }
Andrew Lunn6083ce72015-08-17 23:52:52 +02002541 if (dsa_is_dsa_port(ds, port)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002542 if (mv88e6xxx_6095_family(chip) ||
2543 mv88e6xxx_6185_family(chip))
Andrew Lunn6083ce72015-08-17 23:52:52 +02002544 reg |= PORT_CONTROL_DSA_TAG;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002545 if (mv88e6xxx_6352_family(chip) ||
2546 mv88e6xxx_6351_family(chip) ||
2547 mv88e6xxx_6165_family(chip) ||
2548 mv88e6xxx_6097_family(chip) ||
2549 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002550 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunn6083ce72015-08-17 23:52:52 +02002551 }
2552
Andrew Lunn54d792f2015-05-06 01:09:47 +02002553 if (port == dsa_upstream_port(ds))
2554 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2555 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2556 }
2557 if (reg) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002558 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL, reg);
2559 if (err)
2560 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002561 }
2562
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002563 /* If this port is connected to a SerDes, make sure the SerDes is not
2564 * powered down.
2565 */
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002566 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_SERDES)) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002567 err = mv88e6xxx_port_read(chip, port, PORT_STATUS, &reg);
2568 if (err)
2569 return err;
2570 reg &= PORT_STATUS_CMODE_MASK;
2571 if ((reg == PORT_STATUS_CMODE_100BASE_X) ||
2572 (reg == PORT_STATUS_CMODE_1000BASE_X) ||
2573 (reg == PORT_STATUS_CMODE_SGMII)) {
2574 err = mv88e6xxx_serdes_power_on(chip);
2575 if (err < 0)
2576 return err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002577 }
2578 }
2579
Vivien Didelot8efdda42015-08-13 12:52:23 -04002580 /* Port Control 2: don't force a good FCS, set the maximum frame size to
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002581 * 10240 bytes, disable 802.1q tags checking, don't discard tagged or
Vivien Didelot8efdda42015-08-13 12:52:23 -04002582 * untagged frames on this port, do a destination address lookup on all
2583 * received packets as usual, disable ARP mirroring and don't send a
2584 * copy of all transmitted/received frames on this port to the CPU.
Andrew Lunn54d792f2015-05-06 01:09:47 +02002585 */
2586 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002587 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2588 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2589 mv88e6xxx_6095_family(chip) || mv88e6xxx_6320_family(chip) ||
2590 mv88e6xxx_6185_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002591 reg = PORT_CONTROL_2_MAP_DA;
2592
Vivien Didelotfad09c72016-06-21 12:28:20 -04002593 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2594 mv88e6xxx_6165_family(chip) || mv88e6xxx_6320_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002595 reg |= PORT_CONTROL_2_JUMBO_10240;
2596
Vivien Didelotfad09c72016-06-21 12:28:20 -04002597 if (mv88e6xxx_6095_family(chip) || mv88e6xxx_6185_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002598 /* Set the upstream port this port should use */
2599 reg |= dsa_upstream_port(ds);
2600 /* enable forwarding of unknown multicast addresses to
2601 * the upstream port
2602 */
2603 if (port == dsa_upstream_port(ds))
2604 reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
2605 }
2606
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002607 reg |= PORT_CONTROL_2_8021Q_DISABLED;
Vivien Didelot8efdda42015-08-13 12:52:23 -04002608
Andrew Lunn54d792f2015-05-06 01:09:47 +02002609 if (reg) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002610 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_2, reg);
2611 if (err)
2612 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002613 }
2614
2615 /* Port Association Vector: when learning source addresses
2616 * of packets, add the address to the address database using
2617 * a port bitmap that has only the bit for this port set and
2618 * the other bits clear.
2619 */
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002620 reg = 1 << port;
Vivien Didelot996ecb82016-04-14 14:42:08 -04002621 /* Disable learning for CPU port */
2622 if (dsa_is_cpu_port(ds, port))
Vivien Didelot65fa4022016-04-14 14:42:07 -04002623 reg = 0;
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002624
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002625 err = mv88e6xxx_port_write(chip, port, PORT_ASSOC_VECTOR, reg);
2626 if (err)
2627 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002628
2629 /* Egress rate control 2: disable egress rate control. */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002630 err = mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL_2, 0x0000);
2631 if (err)
2632 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002633
Vivien Didelotfad09c72016-06-21 12:28:20 -04002634 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2635 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2636 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002637 /* Do not limit the period of time that this port can
2638 * be paused for by the remote end or the period of
2639 * time that this port can pause the remote end.
2640 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002641 err = mv88e6xxx_port_write(chip, port, PORT_PAUSE_CTRL, 0x0000);
2642 if (err)
2643 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002644
2645 /* Port ATU control: disable limiting the number of
2646 * address database entries that this port is allowed
2647 * to use.
2648 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002649 err = mv88e6xxx_port_write(chip, port, PORT_ATU_CONTROL,
2650 0x0000);
Andrew Lunn54d792f2015-05-06 01:09:47 +02002651 /* Priority Override: disable DA, SA and VTU priority
2652 * override.
2653 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002654 err = mv88e6xxx_port_write(chip, port, PORT_PRI_OVERRIDE,
2655 0x0000);
2656 if (err)
2657 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002658
2659 /* Port Ethertype: use the Ethertype DSA Ethertype
2660 * value.
2661 */
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002662 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA)) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002663 err = mv88e6xxx_port_write(chip, port, PORT_ETH_TYPE,
2664 ETH_P_EDSA);
2665 if (err)
2666 return err;
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002667 }
2668
Andrew Lunn54d792f2015-05-06 01:09:47 +02002669 /* Tag Remap: use an identity 802.1p prio -> switch
2670 * prio mapping.
2671 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002672 err = mv88e6xxx_port_write(chip, port, PORT_TAG_REGMAP_0123,
2673 0x3210);
2674 if (err)
2675 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002676
2677 /* Tag Remap 2: use an identity 802.1p prio -> switch
2678 * prio mapping.
2679 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002680 err = mv88e6xxx_port_write(chip, port, PORT_TAG_REGMAP_4567,
2681 0x7654);
2682 if (err)
2683 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002684 }
2685
Jamie Lentin1bc261f2016-08-22 22:47:08 +01002686 /* Rate Control: disable ingress rate limiting. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002687 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2688 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
Vivien Didelotfad09c72016-06-21 12:28:20 -04002689 mv88e6xxx_6320_family(chip)) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002690 err = mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL,
2691 0x0001);
2692 if (err)
2693 return err;
Jamie Lentin1bc261f2016-08-22 22:47:08 +01002694 } else if (mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip)) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002695 err = mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL,
2696 0x0000);
2697 if (err)
2698 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002699 }
2700
Guenter Roeck366f0a02015-03-26 18:36:30 -07002701 /* Port Control 1: disable trunking, disable sending
2702 * learning messages to this port.
Guenter Roeckd827e882015-03-26 18:36:29 -07002703 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002704 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_1, 0x0000);
2705 if (err)
2706 return err;
Guenter Roeckd827e882015-03-26 18:36:29 -07002707
Vivien Didelot207afda2016-04-14 14:42:09 -04002708 /* Port based VLAN map: give each port the same default address
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002709 * database, and allow bidirectional communication between the
2710 * CPU and DSA port(s), and the other ports.
Guenter Roeckd827e882015-03-26 18:36:29 -07002711 */
Vivien Didelotb4e48c52016-11-04 03:23:29 +01002712 err = mv88e6xxx_port_set_fid(chip, port, 0);
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002713 if (err)
2714 return err;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002715
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002716 err = _mv88e6xxx_port_based_vlan_map(chip, port);
2717 if (err)
2718 return err;
Guenter Roeckd827e882015-03-26 18:36:29 -07002719
2720 /* Default VLAN ID and priority: don't set a default VLAN
2721 * ID, and set the default packet priority to zero.
2722 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002723 return mv88e6xxx_port_write(chip, port, PORT_DEFAULT_VLAN, 0x0000);
Andrew Lunndbde9e62015-05-06 01:09:48 +02002724}
2725
Wei Yongjunaa0938c2016-10-18 15:53:37 +00002726static int mv88e6xxx_g1_set_switch_mac(struct mv88e6xxx_chip *chip, u8 *addr)
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002727{
2728 int err;
2729
Vivien Didelota935c052016-09-29 12:21:53 -04002730 err = mv88e6xxx_g1_write(chip, GLOBAL_MAC_01, (addr[0] << 8) | addr[1]);
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002731 if (err)
2732 return err;
2733
Vivien Didelota935c052016-09-29 12:21:53 -04002734 err = mv88e6xxx_g1_write(chip, GLOBAL_MAC_23, (addr[2] << 8) | addr[3]);
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002735 if (err)
2736 return err;
2737
Vivien Didelota935c052016-09-29 12:21:53 -04002738 err = mv88e6xxx_g1_write(chip, GLOBAL_MAC_45, (addr[4] << 8) | addr[5]);
2739 if (err)
2740 return err;
2741
2742 return 0;
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002743}
2744
Vivien Didelotacddbd22016-07-18 20:45:39 -04002745static int mv88e6xxx_g1_set_age_time(struct mv88e6xxx_chip *chip,
2746 unsigned int msecs)
2747{
2748 const unsigned int coeff = chip->info->age_time_coeff;
2749 const unsigned int min = 0x01 * coeff;
2750 const unsigned int max = 0xff * coeff;
2751 u8 age_time;
2752 u16 val;
2753 int err;
2754
2755 if (msecs < min || msecs > max)
2756 return -ERANGE;
2757
2758 /* Round to nearest multiple of coeff */
2759 age_time = (msecs + coeff / 2) / coeff;
2760
Vivien Didelota935c052016-09-29 12:21:53 -04002761 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_CONTROL, &val);
Vivien Didelotacddbd22016-07-18 20:45:39 -04002762 if (err)
2763 return err;
2764
2765 /* AgeTime is 11:4 bits */
2766 val &= ~0xff0;
2767 val |= age_time << 4;
2768
Vivien Didelota935c052016-09-29 12:21:53 -04002769 return mv88e6xxx_g1_write(chip, GLOBAL_ATU_CONTROL, val);
Vivien Didelotacddbd22016-07-18 20:45:39 -04002770}
2771
Vivien Didelot2cfcd962016-07-18 20:45:40 -04002772static int mv88e6xxx_set_ageing_time(struct dsa_switch *ds,
2773 unsigned int ageing_time)
2774{
Vivien Didelot04bed142016-08-31 18:06:13 -04002775 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot2cfcd962016-07-18 20:45:40 -04002776 int err;
2777
2778 mutex_lock(&chip->reg_lock);
2779 err = mv88e6xxx_g1_set_age_time(chip, ageing_time);
2780 mutex_unlock(&chip->reg_lock);
2781
2782 return err;
2783}
2784
Vivien Didelot97299342016-07-18 20:45:30 -04002785static int mv88e6xxx_g1_setup(struct mv88e6xxx_chip *chip)
Vivien Didelot08a01262016-05-09 13:22:50 -04002786{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002787 struct dsa_switch *ds = chip->ds;
Vivien Didelotb0745e872016-05-09 13:22:53 -04002788 u32 upstream_port = dsa_upstream_port(ds);
Vivien Didelot119477b2016-05-09 13:22:51 -04002789 u16 reg;
Vivien Didelot08a01262016-05-09 13:22:50 -04002790 int err;
Vivien Didelot08a01262016-05-09 13:22:50 -04002791
Vivien Didelot119477b2016-05-09 13:22:51 -04002792 /* Enable the PHY Polling Unit if present, don't discard any packets,
2793 * and mask all interrupt sources.
2794 */
Andrew Lunndc30c352016-10-16 19:56:49 +02002795 err = mv88e6xxx_g1_read(chip, GLOBAL_CONTROL, &reg);
2796 if (err < 0)
2797 return err;
2798
2799 reg &= ~GLOBAL_CONTROL_PPU_ENABLE;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002800 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU) ||
2801 mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE))
Vivien Didelot119477b2016-05-09 13:22:51 -04002802 reg |= GLOBAL_CONTROL_PPU_ENABLE;
2803
Vivien Didelota935c052016-09-29 12:21:53 -04002804 err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL, reg);
Vivien Didelot119477b2016-05-09 13:22:51 -04002805 if (err)
2806 return err;
2807
Vivien Didelotb0745e872016-05-09 13:22:53 -04002808 /* Configure the upstream port, and configure it as the port to which
2809 * ingress and egress and ARP monitor frames are to be sent.
2810 */
2811 reg = upstream_port << GLOBAL_MONITOR_CONTROL_INGRESS_SHIFT |
2812 upstream_port << GLOBAL_MONITOR_CONTROL_EGRESS_SHIFT |
2813 upstream_port << GLOBAL_MONITOR_CONTROL_ARP_SHIFT;
Vivien Didelota935c052016-09-29 12:21:53 -04002814 err = mv88e6xxx_g1_write(chip, GLOBAL_MONITOR_CONTROL, reg);
Vivien Didelotb0745e872016-05-09 13:22:53 -04002815 if (err)
2816 return err;
2817
Vivien Didelot50484ff2016-05-09 13:22:54 -04002818 /* Disable remote management, and set the switch's DSA device number. */
Vivien Didelota935c052016-09-29 12:21:53 -04002819 err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL_2,
2820 GLOBAL_CONTROL_2_MULTIPLE_CASCADE |
2821 (ds->index & 0x1f));
Vivien Didelot50484ff2016-05-09 13:22:54 -04002822 if (err)
2823 return err;
2824
Vivien Didelotacddbd22016-07-18 20:45:39 -04002825 /* Clear all the VTU and STU entries */
2826 err = _mv88e6xxx_vtu_stu_flush(chip);
2827 if (err < 0)
2828 return err;
2829
Vivien Didelot08a01262016-05-09 13:22:50 -04002830 /* Set the default address aging time to 5 minutes, and
2831 * enable address learn messages to be sent to all message
2832 * ports.
2833 */
Vivien Didelota935c052016-09-29 12:21:53 -04002834 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_CONTROL,
2835 GLOBAL_ATU_CONTROL_LEARN2ALL);
Vivien Didelot08a01262016-05-09 13:22:50 -04002836 if (err)
2837 return err;
2838
Vivien Didelotacddbd22016-07-18 20:45:39 -04002839 err = mv88e6xxx_g1_set_age_time(chip, 300000);
2840 if (err)
Vivien Didelot97299342016-07-18 20:45:30 -04002841 return err;
2842
2843 /* Clear all ATU entries */
2844 err = _mv88e6xxx_atu_flush(chip, 0, true);
2845 if (err)
2846 return err;
2847
Vivien Didelot08a01262016-05-09 13:22:50 -04002848 /* Configure the IP ToS mapping registers. */
Vivien Didelota935c052016-09-29 12:21:53 -04002849 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_0, 0x0000);
Vivien Didelot08a01262016-05-09 13:22:50 -04002850 if (err)
2851 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002852 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_1, 0x0000);
Vivien Didelot08a01262016-05-09 13:22:50 -04002853 if (err)
2854 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002855 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_2, 0x5555);
Vivien Didelot08a01262016-05-09 13:22:50 -04002856 if (err)
2857 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002858 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_3, 0x5555);
Vivien Didelot08a01262016-05-09 13:22:50 -04002859 if (err)
2860 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002861 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_4, 0xaaaa);
Vivien Didelot08a01262016-05-09 13:22:50 -04002862 if (err)
2863 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002864 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_5, 0xaaaa);
Vivien Didelot08a01262016-05-09 13:22:50 -04002865 if (err)
2866 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002867 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_6, 0xffff);
Vivien Didelot08a01262016-05-09 13:22:50 -04002868 if (err)
2869 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002870 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_7, 0xffff);
Vivien Didelot08a01262016-05-09 13:22:50 -04002871 if (err)
2872 return err;
2873
2874 /* Configure the IEEE 802.1p priority mapping register. */
Vivien Didelota935c052016-09-29 12:21:53 -04002875 err = mv88e6xxx_g1_write(chip, GLOBAL_IEEE_PRI, 0xfa41);
Vivien Didelot08a01262016-05-09 13:22:50 -04002876 if (err)
2877 return err;
2878
Andrew Lunnde2273872016-11-21 23:27:01 +01002879 /* Initialize the statistics unit */
2880 err = mv88e6xxx_stats_set_histogram(chip);
2881 if (err)
2882 return err;
2883
Vivien Didelot97299342016-07-18 20:45:30 -04002884 /* Clear the statistics counters for all ports */
Vivien Didelota935c052016-09-29 12:21:53 -04002885 err = mv88e6xxx_g1_write(chip, GLOBAL_STATS_OP,
2886 GLOBAL_STATS_OP_FLUSH_ALL);
Vivien Didelot97299342016-07-18 20:45:30 -04002887 if (err)
2888 return err;
2889
2890 /* Wait for the flush to complete. */
2891 err = _mv88e6xxx_stats_wait(chip);
2892 if (err)
2893 return err;
2894
2895 return 0;
2896}
2897
Vivien Didelotf81ec902016-05-09 13:22:58 -04002898static int mv88e6xxx_setup(struct dsa_switch *ds)
Guenter Roeckacdaffc2015-03-26 18:36:28 -07002899{
Vivien Didelot04bed142016-08-31 18:06:13 -04002900 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot552238b2016-05-09 13:22:49 -04002901 int err;
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002902 int i;
2903
Vivien Didelotfad09c72016-06-21 12:28:20 -04002904 chip->ds = ds;
2905 ds->slave_mii_bus = chip->mdio_bus;
Vivien Didelot552238b2016-05-09 13:22:49 -04002906
Vivien Didelotfad09c72016-06-21 12:28:20 -04002907 mutex_lock(&chip->reg_lock);
Vivien Didelot552238b2016-05-09 13:22:49 -04002908
Vivien Didelot97299342016-07-18 20:45:30 -04002909 /* Setup Switch Port Registers */
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002910 for (i = 0; i < mv88e6xxx_num_ports(chip); i++) {
Vivien Didelot97299342016-07-18 20:45:30 -04002911 err = mv88e6xxx_setup_port(chip, i);
2912 if (err)
2913 goto unlock;
2914 }
2915
2916 /* Setup Switch Global 1 Registers */
2917 err = mv88e6xxx_g1_setup(chip);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002918 if (err)
2919 goto unlock;
2920
Vivien Didelot97299342016-07-18 20:45:30 -04002921 /* Setup Switch Global 2 Registers */
2922 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_GLOBAL2)) {
2923 err = mv88e6xxx_g2_setup(chip);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002924 if (err)
2925 goto unlock;
2926 }
Andrew Lunn54d792f2015-05-06 01:09:47 +02002927
Vivien Didelot6b17e862015-08-13 12:52:18 -04002928unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002929 mutex_unlock(&chip->reg_lock);
Andrew Lunndb687a52015-06-20 21:31:29 +02002930
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002931 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002932}
2933
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002934static int mv88e6xxx_set_addr(struct dsa_switch *ds, u8 *addr)
2935{
Vivien Didelot04bed142016-08-31 18:06:13 -04002936 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002937 int err;
2938
Vivien Didelotb073d4e2016-09-29 12:22:01 -04002939 if (!chip->info->ops->set_switch_mac)
2940 return -EOPNOTSUPP;
2941
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002942 mutex_lock(&chip->reg_lock);
Vivien Didelotb073d4e2016-09-29 12:22:01 -04002943 err = chip->info->ops->set_switch_mac(chip, addr);
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002944 mutex_unlock(&chip->reg_lock);
2945
2946 return err;
2947}
2948
Vivien Didelote57e5e72016-08-15 17:19:00 -04002949static int mv88e6xxx_mdio_read(struct mii_bus *bus, int phy, int reg)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002950{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002951 struct mv88e6xxx_chip *chip = bus->priv;
Vivien Didelote57e5e72016-08-15 17:19:00 -04002952 u16 val;
2953 int err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002954
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002955 if (phy >= mv88e6xxx_num_ports(chip))
Andrew Lunn158bc062016-04-28 21:24:06 -04002956 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002957
Vivien Didelotfad09c72016-06-21 12:28:20 -04002958 mutex_lock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04002959 err = mv88e6xxx_phy_read(chip, phy, reg, &val);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002960 mutex_unlock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04002961
2962 return err ? err : val;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002963}
2964
Vivien Didelote57e5e72016-08-15 17:19:00 -04002965static int mv88e6xxx_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002966{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002967 struct mv88e6xxx_chip *chip = bus->priv;
Vivien Didelote57e5e72016-08-15 17:19:00 -04002968 int err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002969
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002970 if (phy >= mv88e6xxx_num_ports(chip))
Andrew Lunn158bc062016-04-28 21:24:06 -04002971 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002972
Vivien Didelotfad09c72016-06-21 12:28:20 -04002973 mutex_lock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04002974 err = mv88e6xxx_phy_write(chip, phy, reg, val);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002975 mutex_unlock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04002976
2977 return err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002978}
2979
Vivien Didelotfad09c72016-06-21 12:28:20 -04002980static int mv88e6xxx_mdio_register(struct mv88e6xxx_chip *chip,
Andrew Lunnb516d452016-06-04 21:17:06 +02002981 struct device_node *np)
2982{
2983 static int index;
2984 struct mii_bus *bus;
2985 int err;
2986
Andrew Lunnb516d452016-06-04 21:17:06 +02002987 if (np)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002988 chip->mdio_np = of_get_child_by_name(np, "mdio");
Andrew Lunnb516d452016-06-04 21:17:06 +02002989
Vivien Didelotfad09c72016-06-21 12:28:20 -04002990 bus = devm_mdiobus_alloc(chip->dev);
Andrew Lunnb516d452016-06-04 21:17:06 +02002991 if (!bus)
2992 return -ENOMEM;
2993
Vivien Didelotfad09c72016-06-21 12:28:20 -04002994 bus->priv = (void *)chip;
Andrew Lunnb516d452016-06-04 21:17:06 +02002995 if (np) {
2996 bus->name = np->full_name;
2997 snprintf(bus->id, MII_BUS_ID_SIZE, "%s", np->full_name);
2998 } else {
2999 bus->name = "mv88e6xxx SMI";
3000 snprintf(bus->id, MII_BUS_ID_SIZE, "mv88e6xxx-%d", index++);
3001 }
3002
3003 bus->read = mv88e6xxx_mdio_read;
3004 bus->write = mv88e6xxx_mdio_write;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003005 bus->parent = chip->dev;
Andrew Lunnb516d452016-06-04 21:17:06 +02003006
Vivien Didelotfad09c72016-06-21 12:28:20 -04003007 if (chip->mdio_np)
3008 err = of_mdiobus_register(bus, chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003009 else
3010 err = mdiobus_register(bus);
3011 if (err) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04003012 dev_err(chip->dev, "Cannot register MDIO bus (%d)\n", err);
Andrew Lunnb516d452016-06-04 21:17:06 +02003013 goto out;
3014 }
Vivien Didelotfad09c72016-06-21 12:28:20 -04003015 chip->mdio_bus = bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003016
3017 return 0;
3018
3019out:
Vivien Didelotfad09c72016-06-21 12:28:20 -04003020 if (chip->mdio_np)
3021 of_node_put(chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003022
3023 return err;
3024}
3025
Vivien Didelotfad09c72016-06-21 12:28:20 -04003026static void mv88e6xxx_mdio_unregister(struct mv88e6xxx_chip *chip)
Andrew Lunnb516d452016-06-04 21:17:06 +02003027
3028{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003029 struct mii_bus *bus = chip->mdio_bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003030
3031 mdiobus_unregister(bus);
3032
Vivien Didelotfad09c72016-06-21 12:28:20 -04003033 if (chip->mdio_np)
3034 of_node_put(chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003035}
3036
Guenter Roeckc22995c2015-07-25 09:42:28 -07003037#ifdef CONFIG_NET_DSA_HWMON
3038
3039static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
3040{
Vivien Didelot04bed142016-08-31 18:06:13 -04003041 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot9c938292016-08-15 17:19:02 -04003042 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003043 int ret;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003044
3045 *temp = 0;
3046
Vivien Didelotfad09c72016-06-21 12:28:20 -04003047 mutex_lock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003048
Vivien Didelot9c938292016-08-15 17:19:02 -04003049 ret = mv88e6xxx_phy_write(chip, 0x0, 0x16, 0x6);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003050 if (ret < 0)
3051 goto error;
3052
3053 /* Enable temperature sensor */
Vivien Didelot9c938292016-08-15 17:19:02 -04003054 ret = mv88e6xxx_phy_read(chip, 0x0, 0x1a, &val);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003055 if (ret < 0)
3056 goto error;
3057
Vivien Didelot9c938292016-08-15 17:19:02 -04003058 ret = mv88e6xxx_phy_write(chip, 0x0, 0x1a, val | (1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003059 if (ret < 0)
3060 goto error;
3061
3062 /* Wait for temperature to stabilize */
3063 usleep_range(10000, 12000);
3064
Vivien Didelot9c938292016-08-15 17:19:02 -04003065 ret = mv88e6xxx_phy_read(chip, 0x0, 0x1a, &val);
3066 if (ret < 0)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003067 goto error;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003068
3069 /* Disable temperature sensor */
Vivien Didelot9c938292016-08-15 17:19:02 -04003070 ret = mv88e6xxx_phy_write(chip, 0x0, 0x1a, val & ~(1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003071 if (ret < 0)
3072 goto error;
3073
3074 *temp = ((val & 0x1f) - 5) * 5;
3075
3076error:
Vivien Didelot9c938292016-08-15 17:19:02 -04003077 mv88e6xxx_phy_write(chip, 0x0, 0x16, 0x0);
Vivien Didelotfad09c72016-06-21 12:28:20 -04003078 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003079 return ret;
3080}
3081
3082static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
3083{
Vivien Didelot04bed142016-08-31 18:06:13 -04003084 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003085 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003086 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003087 int ret;
3088
3089 *temp = 0;
3090
Vivien Didelot9c938292016-08-15 17:19:02 -04003091 mutex_lock(&chip->reg_lock);
3092 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 27, &val);
3093 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003094 if (ret < 0)
3095 return ret;
3096
Vivien Didelot9c938292016-08-15 17:19:02 -04003097 *temp = (val & 0xff) - 25;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003098
3099 return 0;
3100}
3101
Vivien Didelotf81ec902016-05-09 13:22:58 -04003102static int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003103{
Vivien Didelot04bed142016-08-31 18:06:13 -04003104 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn158bc062016-04-28 21:24:06 -04003105
Vivien Didelotfad09c72016-06-21 12:28:20 -04003106 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP))
Vivien Didelot6594f612016-05-09 13:22:42 -04003107 return -EOPNOTSUPP;
3108
Vivien Didelotfad09c72016-06-21 12:28:20 -04003109 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003110 return mv88e63xx_get_temp(ds, temp);
3111
3112 return mv88e61xx_get_temp(ds, temp);
3113}
3114
Vivien Didelotf81ec902016-05-09 13:22:58 -04003115static int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003116{
Vivien Didelot04bed142016-08-31 18:06:13 -04003117 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003118 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003119 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003120 int ret;
3121
Vivien Didelotfad09c72016-06-21 12:28:20 -04003122 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003123 return -EOPNOTSUPP;
3124
3125 *temp = 0;
3126
Vivien Didelot9c938292016-08-15 17:19:02 -04003127 mutex_lock(&chip->reg_lock);
3128 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3129 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003130 if (ret < 0)
3131 return ret;
3132
Vivien Didelot9c938292016-08-15 17:19:02 -04003133 *temp = (((val >> 8) & 0x1f) * 5) - 25;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003134
3135 return 0;
3136}
3137
Vivien Didelotf81ec902016-05-09 13:22:58 -04003138static int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003139{
Vivien Didelot04bed142016-08-31 18:06:13 -04003140 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003141 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003142 u16 val;
3143 int err;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003144
Vivien Didelotfad09c72016-06-21 12:28:20 -04003145 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003146 return -EOPNOTSUPP;
3147
Vivien Didelot9c938292016-08-15 17:19:02 -04003148 mutex_lock(&chip->reg_lock);
3149 err = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3150 if (err)
3151 goto unlock;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003152 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
Vivien Didelot9c938292016-08-15 17:19:02 -04003153 err = mv88e6xxx_phy_page_write(chip, phy, 6, 26,
3154 (val & 0xe0ff) | (temp << 8));
3155unlock:
3156 mutex_unlock(&chip->reg_lock);
3157
3158 return err;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003159}
3160
Vivien Didelotf81ec902016-05-09 13:22:58 -04003161static int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003162{
Vivien Didelot04bed142016-08-31 18:06:13 -04003163 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003164 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003165 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003166 int ret;
3167
Vivien Didelotfad09c72016-06-21 12:28:20 -04003168 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003169 return -EOPNOTSUPP;
3170
3171 *alarm = false;
3172
Vivien Didelot9c938292016-08-15 17:19:02 -04003173 mutex_lock(&chip->reg_lock);
3174 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3175 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003176 if (ret < 0)
3177 return ret;
3178
Vivien Didelot9c938292016-08-15 17:19:02 -04003179 *alarm = !!(val & 0x40);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003180
3181 return 0;
3182}
3183#endif /* CONFIG_NET_DSA_HWMON */
3184
Vivien Didelot855b1932016-07-20 18:18:35 -04003185static int mv88e6xxx_get_eeprom_len(struct dsa_switch *ds)
3186{
Vivien Didelot04bed142016-08-31 18:06:13 -04003187 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot855b1932016-07-20 18:18:35 -04003188
3189 return chip->eeprom_len;
3190}
3191
Vivien Didelot855b1932016-07-20 18:18:35 -04003192static int mv88e6xxx_get_eeprom(struct dsa_switch *ds,
3193 struct ethtool_eeprom *eeprom, u8 *data)
3194{
Vivien Didelot04bed142016-08-31 18:06:13 -04003195 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot855b1932016-07-20 18:18:35 -04003196 int err;
3197
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003198 if (!chip->info->ops->get_eeprom)
3199 return -EOPNOTSUPP;
3200
Vivien Didelot855b1932016-07-20 18:18:35 -04003201 mutex_lock(&chip->reg_lock);
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003202 err = chip->info->ops->get_eeprom(chip, eeprom, data);
Vivien Didelot855b1932016-07-20 18:18:35 -04003203 mutex_unlock(&chip->reg_lock);
3204
3205 if (err)
3206 return err;
3207
3208 eeprom->magic = 0xc3ec4951;
3209
3210 return 0;
3211}
3212
Vivien Didelot855b1932016-07-20 18:18:35 -04003213static int mv88e6xxx_set_eeprom(struct dsa_switch *ds,
3214 struct ethtool_eeprom *eeprom, u8 *data)
3215{
Vivien Didelot04bed142016-08-31 18:06:13 -04003216 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot855b1932016-07-20 18:18:35 -04003217 int err;
3218
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003219 if (!chip->info->ops->set_eeprom)
3220 return -EOPNOTSUPP;
3221
Vivien Didelot855b1932016-07-20 18:18:35 -04003222 if (eeprom->magic != 0xc3ec4951)
3223 return -EINVAL;
3224
3225 mutex_lock(&chip->reg_lock);
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003226 err = chip->info->ops->set_eeprom(chip, eeprom, data);
Vivien Didelot855b1932016-07-20 18:18:35 -04003227 mutex_unlock(&chip->reg_lock);
3228
3229 return err;
3230}
3231
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003232static const struct mv88e6xxx_ops mv88e6085_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003233 /* MV88E6XXX_FAMILY_6097 */
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003234 .set_switch_mac = mv88e6xxx_g1_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003235 .phy_read = mv88e6xxx_phy_ppu_read,
3236 .phy_write = mv88e6xxx_phy_ppu_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003237 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003238 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003239 .port_set_speed = mv88e6185_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003240 .stats_snapshot = mv88e6xxx_g1_stats_snapshot,
Andrew Lunndfafe442016-11-21 23:27:02 +01003241 .stats_get_sset_count = mv88e6095_stats_get_sset_count,
3242 .stats_get_strings = mv88e6095_stats_get_strings,
Andrew Lunn052f9472016-11-21 23:27:03 +01003243 .stats_get_stats = mv88e6095_stats_get_stats,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003244};
3245
3246static const struct mv88e6xxx_ops mv88e6095_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003247 /* MV88E6XXX_FAMILY_6095 */
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003248 .set_switch_mac = mv88e6xxx_g1_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003249 .phy_read = mv88e6xxx_phy_ppu_read,
3250 .phy_write = mv88e6xxx_phy_ppu_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003251 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003252 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003253 .port_set_speed = mv88e6185_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003254 .stats_snapshot = mv88e6xxx_g1_stats_snapshot,
Andrew Lunndfafe442016-11-21 23:27:02 +01003255 .stats_get_sset_count = mv88e6095_stats_get_sset_count,
3256 .stats_get_strings = mv88e6095_stats_get_strings,
Andrew Lunn052f9472016-11-21 23:27:03 +01003257 .stats_get_stats = mv88e6095_stats_get_stats,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003258};
3259
3260static const struct mv88e6xxx_ops mv88e6123_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003261 /* MV88E6XXX_FAMILY_6165 */
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003262 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003263 .phy_read = mv88e6xxx_read,
3264 .phy_write = mv88e6xxx_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003265 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003266 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003267 .port_set_speed = mv88e6185_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003268 .stats_snapshot = mv88e6xxx_g1_stats_snapshot,
Andrew Lunndfafe442016-11-21 23:27:02 +01003269 .stats_get_sset_count = mv88e6095_stats_get_sset_count,
3270 .stats_get_strings = mv88e6095_stats_get_strings,
Andrew Lunn052f9472016-11-21 23:27:03 +01003271 .stats_get_stats = mv88e6095_stats_get_stats,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003272};
3273
3274static const struct mv88e6xxx_ops mv88e6131_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003275 /* MV88E6XXX_FAMILY_6185 */
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003276 .set_switch_mac = mv88e6xxx_g1_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003277 .phy_read = mv88e6xxx_phy_ppu_read,
3278 .phy_write = mv88e6xxx_phy_ppu_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003279 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003280 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003281 .port_set_speed = mv88e6185_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003282 .stats_snapshot = mv88e6xxx_g1_stats_snapshot,
Andrew Lunndfafe442016-11-21 23:27:02 +01003283 .stats_get_sset_count = mv88e6095_stats_get_sset_count,
3284 .stats_get_strings = mv88e6095_stats_get_strings,
Andrew Lunn052f9472016-11-21 23:27:03 +01003285 .stats_get_stats = mv88e6095_stats_get_stats,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003286};
3287
3288static const struct mv88e6xxx_ops mv88e6161_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003289 /* MV88E6XXX_FAMILY_6165 */
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003290 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003291 .phy_read = mv88e6xxx_read,
3292 .phy_write = mv88e6xxx_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003293 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003294 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003295 .port_set_speed = mv88e6185_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003296 .stats_snapshot = mv88e6xxx_g1_stats_snapshot,
Andrew Lunndfafe442016-11-21 23:27:02 +01003297 .stats_get_sset_count = mv88e6095_stats_get_sset_count,
3298 .stats_get_strings = mv88e6095_stats_get_strings,
Andrew Lunn052f9472016-11-21 23:27:03 +01003299 .stats_get_stats = mv88e6095_stats_get_stats,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003300};
3301
3302static const struct mv88e6xxx_ops mv88e6165_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003303 /* MV88E6XXX_FAMILY_6165 */
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003304 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003305 .phy_read = mv88e6xxx_read,
3306 .phy_write = mv88e6xxx_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003307 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003308 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003309 .port_set_speed = mv88e6185_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003310 .stats_snapshot = mv88e6xxx_g1_stats_snapshot,
Andrew Lunndfafe442016-11-21 23:27:02 +01003311 .stats_get_sset_count = mv88e6095_stats_get_sset_count,
3312 .stats_get_strings = mv88e6095_stats_get_strings,
Andrew Lunn052f9472016-11-21 23:27:03 +01003313 .stats_get_stats = mv88e6095_stats_get_stats,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003314};
3315
3316static const struct mv88e6xxx_ops mv88e6171_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003317 /* MV88E6XXX_FAMILY_6351 */
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003318 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003319 .phy_read = mv88e6xxx_g2_smi_phy_read,
3320 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003321 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003322 .port_set_duplex = mv88e6xxx_port_set_duplex,
Andrew Lunn94d66ae2016-11-10 15:44:01 +01003323 .port_set_rgmii_delay = mv88e6352_port_set_rgmii_delay,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003324 .port_set_speed = mv88e6185_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003325 .stats_snapshot = mv88e6320_g1_stats_snapshot,
Andrew Lunndfafe442016-11-21 23:27:02 +01003326 .stats_get_sset_count = mv88e6095_stats_get_sset_count,
3327 .stats_get_strings = mv88e6095_stats_get_strings,
Andrew Lunn052f9472016-11-21 23:27:03 +01003328 .stats_get_stats = mv88e6095_stats_get_stats,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003329};
3330
3331static const struct mv88e6xxx_ops mv88e6172_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003332 /* MV88E6XXX_FAMILY_6352 */
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003333 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3334 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003335 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003336 .phy_read = mv88e6xxx_g2_smi_phy_read,
3337 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003338 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003339 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelota0a0f622016-11-04 03:23:34 +01003340 .port_set_rgmii_delay = mv88e6352_port_set_rgmii_delay,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003341 .port_set_speed = mv88e6352_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003342 .stats_snapshot = mv88e6320_g1_stats_snapshot,
Andrew Lunndfafe442016-11-21 23:27:02 +01003343 .stats_get_sset_count = mv88e6095_stats_get_sset_count,
3344 .stats_get_strings = mv88e6095_stats_get_strings,
Andrew Lunn052f9472016-11-21 23:27:03 +01003345 .stats_get_stats = mv88e6095_stats_get_stats,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003346};
3347
3348static const struct mv88e6xxx_ops mv88e6175_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003349 /* MV88E6XXX_FAMILY_6351 */
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003350 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003351 .phy_read = mv88e6xxx_g2_smi_phy_read,
3352 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003353 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003354 .port_set_duplex = mv88e6xxx_port_set_duplex,
Andrew Lunn94d66ae2016-11-10 15:44:01 +01003355 .port_set_rgmii_delay = mv88e6352_port_set_rgmii_delay,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003356 .port_set_speed = mv88e6185_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003357 .stats_snapshot = mv88e6320_g1_stats_snapshot,
Andrew Lunndfafe442016-11-21 23:27:02 +01003358 .stats_get_sset_count = mv88e6095_stats_get_sset_count,
3359 .stats_get_strings = mv88e6095_stats_get_strings,
Andrew Lunn052f9472016-11-21 23:27:03 +01003360 .stats_get_stats = mv88e6095_stats_get_stats,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003361};
3362
3363static const struct mv88e6xxx_ops mv88e6176_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003364 /* MV88E6XXX_FAMILY_6352 */
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003365 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3366 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003367 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003368 .phy_read = mv88e6xxx_g2_smi_phy_read,
3369 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003370 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003371 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelota0a0f622016-11-04 03:23:34 +01003372 .port_set_rgmii_delay = mv88e6352_port_set_rgmii_delay,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003373 .port_set_speed = mv88e6352_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003374 .stats_snapshot = mv88e6320_g1_stats_snapshot,
Andrew Lunndfafe442016-11-21 23:27:02 +01003375 .stats_get_sset_count = mv88e6095_stats_get_sset_count,
3376 .stats_get_strings = mv88e6095_stats_get_strings,
Andrew Lunn052f9472016-11-21 23:27:03 +01003377 .stats_get_stats = mv88e6095_stats_get_stats,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003378};
3379
3380static const struct mv88e6xxx_ops mv88e6185_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003381 /* MV88E6XXX_FAMILY_6185 */
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003382 .set_switch_mac = mv88e6xxx_g1_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003383 .phy_read = mv88e6xxx_phy_ppu_read,
3384 .phy_write = mv88e6xxx_phy_ppu_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003385 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003386 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003387 .port_set_speed = mv88e6185_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003388 .stats_snapshot = mv88e6xxx_g1_stats_snapshot,
Andrew Lunndfafe442016-11-21 23:27:02 +01003389 .stats_get_sset_count = mv88e6095_stats_get_sset_count,
3390 .stats_get_strings = mv88e6095_stats_get_strings,
Andrew Lunn052f9472016-11-21 23:27:03 +01003391 .stats_get_stats = mv88e6095_stats_get_stats,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003392};
3393
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003394static const struct mv88e6xxx_ops mv88e6190_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003395 /* MV88E6XXX_FAMILY_6390 */
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003396 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
3397 .phy_read = mv88e6xxx_g2_smi_phy_read,
3398 .phy_write = mv88e6xxx_g2_smi_phy_write,
3399 .port_set_link = mv88e6xxx_port_set_link,
3400 .port_set_duplex = mv88e6xxx_port_set_duplex,
3401 .port_set_rgmii_delay = mv88e6390_port_set_rgmii_delay,
3402 .port_set_speed = mv88e6390_port_set_speed,
Andrew Lunn79523472016-11-21 23:27:00 +01003403 .stats_snapshot = mv88e6390_g1_stats_snapshot,
Andrew Lunnde2273872016-11-21 23:27:01 +01003404 .stats_set_histogram = mv88e6390_g1_stats_set_histogram,
Andrew Lunndfafe442016-11-21 23:27:02 +01003405 .stats_get_sset_count = mv88e6320_stats_get_sset_count,
3406 .stats_get_strings = mv88e6320_stats_get_strings,
Andrew Lunne0d8b612016-11-21 23:27:04 +01003407 .stats_get_stats = mv88e6390_stats_get_stats,
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003408};
3409
3410static const struct mv88e6xxx_ops mv88e6190x_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003411 /* MV88E6XXX_FAMILY_6390 */
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003412 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
3413 .phy_read = mv88e6xxx_g2_smi_phy_read,
3414 .phy_write = mv88e6xxx_g2_smi_phy_write,
3415 .port_set_link = mv88e6xxx_port_set_link,
3416 .port_set_duplex = mv88e6xxx_port_set_duplex,
3417 .port_set_rgmii_delay = mv88e6390_port_set_rgmii_delay,
3418 .port_set_speed = mv88e6390x_port_set_speed,
Andrew Lunn79523472016-11-21 23:27:00 +01003419 .stats_snapshot = mv88e6390_g1_stats_snapshot,
Andrew Lunnde2273872016-11-21 23:27:01 +01003420 .stats_set_histogram = mv88e6390_g1_stats_set_histogram,
Andrew Lunndfafe442016-11-21 23:27:02 +01003421 .stats_get_sset_count = mv88e6320_stats_get_sset_count,
3422 .stats_get_strings = mv88e6320_stats_get_strings,
Andrew Lunne0d8b612016-11-21 23:27:04 +01003423 .stats_get_stats = mv88e6390_stats_get_stats,
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003424};
3425
3426static const struct mv88e6xxx_ops mv88e6191_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003427 /* MV88E6XXX_FAMILY_6390 */
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003428 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
3429 .phy_read = mv88e6xxx_g2_smi_phy_read,
3430 .phy_write = mv88e6xxx_g2_smi_phy_write,
3431 .port_set_link = mv88e6xxx_port_set_link,
3432 .port_set_duplex = mv88e6xxx_port_set_duplex,
3433 .port_set_rgmii_delay = mv88e6390_port_set_rgmii_delay,
3434 .port_set_speed = mv88e6390_port_set_speed,
Andrew Lunn79523472016-11-21 23:27:00 +01003435 .stats_snapshot = mv88e6390_g1_stats_snapshot,
Andrew Lunnde2273872016-11-21 23:27:01 +01003436 .stats_set_histogram = mv88e6390_g1_stats_set_histogram,
Andrew Lunndfafe442016-11-21 23:27:02 +01003437 .stats_get_sset_count = mv88e6320_stats_get_sset_count,
3438 .stats_get_strings = mv88e6320_stats_get_strings,
Andrew Lunne0d8b612016-11-21 23:27:04 +01003439 .stats_get_stats = mv88e6390_stats_get_stats,
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003440};
3441
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003442static const struct mv88e6xxx_ops mv88e6240_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003443 /* MV88E6XXX_FAMILY_6352 */
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003444 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3445 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003446 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003447 .phy_read = mv88e6xxx_g2_smi_phy_read,
3448 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003449 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003450 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelota0a0f622016-11-04 03:23:34 +01003451 .port_set_rgmii_delay = mv88e6352_port_set_rgmii_delay,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003452 .port_set_speed = mv88e6352_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003453 .stats_snapshot = mv88e6320_g1_stats_snapshot,
Andrew Lunndfafe442016-11-21 23:27:02 +01003454 .stats_get_sset_count = mv88e6095_stats_get_sset_count,
3455 .stats_get_strings = mv88e6095_stats_get_strings,
Andrew Lunn052f9472016-11-21 23:27:03 +01003456 .stats_get_stats = mv88e6095_stats_get_stats,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003457};
3458
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003459static const struct mv88e6xxx_ops mv88e6290_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003460 /* MV88E6XXX_FAMILY_6390 */
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003461 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
3462 .phy_read = mv88e6xxx_g2_smi_phy_read,
3463 .phy_write = mv88e6xxx_g2_smi_phy_write,
3464 .port_set_link = mv88e6xxx_port_set_link,
3465 .port_set_duplex = mv88e6xxx_port_set_duplex,
3466 .port_set_rgmii_delay = mv88e6390_port_set_rgmii_delay,
3467 .port_set_speed = mv88e6390_port_set_speed,
Andrew Lunn79523472016-11-21 23:27:00 +01003468 .stats_snapshot = mv88e6390_g1_stats_snapshot,
Andrew Lunnde2273872016-11-21 23:27:01 +01003469 .stats_set_histogram = mv88e6390_g1_stats_set_histogram,
Andrew Lunndfafe442016-11-21 23:27:02 +01003470 .stats_get_sset_count = mv88e6320_stats_get_sset_count,
3471 .stats_get_strings = mv88e6320_stats_get_strings,
Andrew Lunne0d8b612016-11-21 23:27:04 +01003472 .stats_get_stats = mv88e6390_stats_get_stats,
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003473};
3474
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003475static const struct mv88e6xxx_ops mv88e6320_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003476 /* MV88E6XXX_FAMILY_6320 */
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003477 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3478 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003479 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003480 .phy_read = mv88e6xxx_g2_smi_phy_read,
3481 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003482 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003483 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003484 .port_set_speed = mv88e6185_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003485 .stats_snapshot = mv88e6320_g1_stats_snapshot,
Andrew Lunndfafe442016-11-21 23:27:02 +01003486 .stats_get_sset_count = mv88e6320_stats_get_sset_count,
3487 .stats_get_strings = mv88e6320_stats_get_strings,
Andrew Lunn052f9472016-11-21 23:27:03 +01003488 .stats_get_stats = mv88e6320_stats_get_stats,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003489};
3490
3491static const struct mv88e6xxx_ops mv88e6321_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003492 /* MV88E6XXX_FAMILY_6321 */
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003493 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3494 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003495 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003496 .phy_read = mv88e6xxx_g2_smi_phy_read,
3497 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003498 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003499 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003500 .port_set_speed = mv88e6185_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003501 .stats_snapshot = mv88e6320_g1_stats_snapshot,
Andrew Lunndfafe442016-11-21 23:27:02 +01003502 .stats_get_sset_count = mv88e6320_stats_get_sset_count,
3503 .stats_get_strings = mv88e6320_stats_get_strings,
Andrew Lunn052f9472016-11-21 23:27:03 +01003504 .stats_get_stats = mv88e6320_stats_get_stats,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003505};
3506
3507static const struct mv88e6xxx_ops mv88e6350_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003508 /* MV88E6XXX_FAMILY_6351 */
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003509 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003510 .phy_read = mv88e6xxx_g2_smi_phy_read,
3511 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003512 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003513 .port_set_duplex = mv88e6xxx_port_set_duplex,
Andrew Lunn94d66ae2016-11-10 15:44:01 +01003514 .port_set_rgmii_delay = mv88e6352_port_set_rgmii_delay,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003515 .port_set_speed = mv88e6185_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003516 .stats_snapshot = mv88e6320_g1_stats_snapshot,
Andrew Lunndfafe442016-11-21 23:27:02 +01003517 .stats_get_sset_count = mv88e6095_stats_get_sset_count,
3518 .stats_get_strings = mv88e6095_stats_get_strings,
Andrew Lunn052f9472016-11-21 23:27:03 +01003519 .stats_get_stats = mv88e6095_stats_get_stats,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003520};
3521
3522static const struct mv88e6xxx_ops mv88e6351_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003523 /* MV88E6XXX_FAMILY_6351 */
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003524 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003525 .phy_read = mv88e6xxx_g2_smi_phy_read,
3526 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003527 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003528 .port_set_duplex = mv88e6xxx_port_set_duplex,
Andrew Lunn94d66ae2016-11-10 15:44:01 +01003529 .port_set_rgmii_delay = mv88e6352_port_set_rgmii_delay,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003530 .port_set_speed = mv88e6185_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003531 .stats_snapshot = mv88e6320_g1_stats_snapshot,
Andrew Lunndfafe442016-11-21 23:27:02 +01003532 .stats_get_sset_count = mv88e6095_stats_get_sset_count,
3533 .stats_get_strings = mv88e6095_stats_get_strings,
Andrew Lunn052f9472016-11-21 23:27:03 +01003534 .stats_get_stats = mv88e6095_stats_get_stats,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003535};
3536
3537static const struct mv88e6xxx_ops mv88e6352_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003538 /* MV88E6XXX_FAMILY_6352 */
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003539 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3540 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003541 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003542 .phy_read = mv88e6xxx_g2_smi_phy_read,
3543 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003544 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003545 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelota0a0f622016-11-04 03:23:34 +01003546 .port_set_rgmii_delay = mv88e6352_port_set_rgmii_delay,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003547 .port_set_speed = mv88e6352_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003548 .stats_snapshot = mv88e6320_g1_stats_snapshot,
Andrew Lunndfafe442016-11-21 23:27:02 +01003549 .stats_get_sset_count = mv88e6095_stats_get_sset_count,
3550 .stats_get_strings = mv88e6095_stats_get_strings,
Andrew Lunn052f9472016-11-21 23:27:03 +01003551 .stats_get_stats = mv88e6095_stats_get_stats,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003552};
3553
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003554static const struct mv88e6xxx_ops mv88e6390_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003555 /* MV88E6XXX_FAMILY_6390 */
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003556 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
3557 .phy_read = mv88e6xxx_g2_smi_phy_read,
3558 .phy_write = mv88e6xxx_g2_smi_phy_write,
3559 .port_set_link = mv88e6xxx_port_set_link,
3560 .port_set_duplex = mv88e6xxx_port_set_duplex,
3561 .port_set_rgmii_delay = mv88e6390_port_set_rgmii_delay,
3562 .port_set_speed = mv88e6390_port_set_speed,
Andrew Lunn79523472016-11-21 23:27:00 +01003563 .stats_snapshot = mv88e6390_g1_stats_snapshot,
Andrew Lunnde2273872016-11-21 23:27:01 +01003564 .stats_set_histogram = mv88e6390_g1_stats_set_histogram,
Andrew Lunndfafe442016-11-21 23:27:02 +01003565 .stats_get_sset_count = mv88e6320_stats_get_sset_count,
3566 .stats_get_strings = mv88e6320_stats_get_strings,
Andrew Lunne0d8b612016-11-21 23:27:04 +01003567 .stats_get_stats = mv88e6390_stats_get_stats,
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003568};
3569
3570static const struct mv88e6xxx_ops mv88e6390x_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003571 /* MV88E6XXX_FAMILY_6390 */
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003572 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
3573 .phy_read = mv88e6xxx_g2_smi_phy_read,
3574 .phy_write = mv88e6xxx_g2_smi_phy_write,
3575 .port_set_link = mv88e6xxx_port_set_link,
3576 .port_set_duplex = mv88e6xxx_port_set_duplex,
3577 .port_set_rgmii_delay = mv88e6390_port_set_rgmii_delay,
3578 .port_set_speed = mv88e6390x_port_set_speed,
Andrew Lunn79523472016-11-21 23:27:00 +01003579 .stats_snapshot = mv88e6390_g1_stats_snapshot,
Andrew Lunnde2273872016-11-21 23:27:01 +01003580 .stats_set_histogram = mv88e6390_g1_stats_set_histogram,
Andrew Lunndfafe442016-11-21 23:27:02 +01003581 .stats_get_sset_count = mv88e6320_stats_get_sset_count,
3582 .stats_get_strings = mv88e6320_stats_get_strings,
Andrew Lunne0d8b612016-11-21 23:27:04 +01003583 .stats_get_stats = mv88e6390_stats_get_stats,
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003584};
3585
3586static const struct mv88e6xxx_ops mv88e6391_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003587 /* MV88E6XXX_FAMILY_6390 */
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003588 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
3589 .phy_read = mv88e6xxx_g2_smi_phy_read,
3590 .phy_write = mv88e6xxx_g2_smi_phy_write,
3591 .port_set_link = mv88e6xxx_port_set_link,
3592 .port_set_duplex = mv88e6xxx_port_set_duplex,
3593 .port_set_rgmii_delay = mv88e6390_port_set_rgmii_delay,
3594 .port_set_speed = mv88e6390_port_set_speed,
Andrew Lunn79523472016-11-21 23:27:00 +01003595 .stats_snapshot = mv88e6390_g1_stats_snapshot,
Andrew Lunnde2273872016-11-21 23:27:01 +01003596 .stats_set_histogram = mv88e6390_g1_stats_set_histogram,
Andrew Lunndfafe442016-11-21 23:27:02 +01003597 .stats_get_sset_count = mv88e6320_stats_get_sset_count,
3598 .stats_get_strings = mv88e6320_stats_get_strings,
Andrew Lunne0d8b612016-11-21 23:27:04 +01003599 .stats_get_stats = mv88e6390_stats_get_stats,
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003600};
3601
Vivien Didelotf81ec902016-05-09 13:22:58 -04003602static const struct mv88e6xxx_info mv88e6xxx_table[] = {
3603 [MV88E6085] = {
3604 .prod_num = PORT_SWITCH_ID_PROD_NUM_6085,
3605 .family = MV88E6XXX_FAMILY_6097,
3606 .name = "Marvell 88E6085",
3607 .num_databases = 4096,
3608 .num_ports = 10,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003609 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003610 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003611 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003612 .g1_irqs = 8,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003613 .flags = MV88E6XXX_FLAGS_FAMILY_6097,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003614 .ops = &mv88e6085_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003615 },
3616
3617 [MV88E6095] = {
3618 .prod_num = PORT_SWITCH_ID_PROD_NUM_6095,
3619 .family = MV88E6XXX_FAMILY_6095,
3620 .name = "Marvell 88E6095/88E6095F",
3621 .num_databases = 256,
3622 .num_ports = 11,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003623 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003624 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003625 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003626 .g1_irqs = 8,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003627 .flags = MV88E6XXX_FLAGS_FAMILY_6095,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003628 .ops = &mv88e6095_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003629 },
3630
3631 [MV88E6123] = {
3632 .prod_num = PORT_SWITCH_ID_PROD_NUM_6123,
3633 .family = MV88E6XXX_FAMILY_6165,
3634 .name = "Marvell 88E6123",
3635 .num_databases = 4096,
3636 .num_ports = 3,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003637 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003638 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003639 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003640 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003641 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003642 .ops = &mv88e6123_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003643 },
3644
3645 [MV88E6131] = {
3646 .prod_num = PORT_SWITCH_ID_PROD_NUM_6131,
3647 .family = MV88E6XXX_FAMILY_6185,
3648 .name = "Marvell 88E6131",
3649 .num_databases = 256,
3650 .num_ports = 8,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003651 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003652 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003653 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003654 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003655 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003656 .ops = &mv88e6131_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003657 },
3658
3659 [MV88E6161] = {
3660 .prod_num = PORT_SWITCH_ID_PROD_NUM_6161,
3661 .family = MV88E6XXX_FAMILY_6165,
3662 .name = "Marvell 88E6161",
3663 .num_databases = 4096,
3664 .num_ports = 6,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003665 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003666 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003667 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003668 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003669 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003670 .ops = &mv88e6161_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003671 },
3672
3673 [MV88E6165] = {
3674 .prod_num = PORT_SWITCH_ID_PROD_NUM_6165,
3675 .family = MV88E6XXX_FAMILY_6165,
3676 .name = "Marvell 88E6165",
3677 .num_databases = 4096,
3678 .num_ports = 6,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003679 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003680 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003681 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003682 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003683 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003684 .ops = &mv88e6165_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003685 },
3686
3687 [MV88E6171] = {
3688 .prod_num = PORT_SWITCH_ID_PROD_NUM_6171,
3689 .family = MV88E6XXX_FAMILY_6351,
3690 .name = "Marvell 88E6171",
3691 .num_databases = 4096,
3692 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003693 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003694 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003695 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003696 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003697 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003698 .ops = &mv88e6171_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003699 },
3700
3701 [MV88E6172] = {
3702 .prod_num = PORT_SWITCH_ID_PROD_NUM_6172,
3703 .family = MV88E6XXX_FAMILY_6352,
3704 .name = "Marvell 88E6172",
3705 .num_databases = 4096,
3706 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003707 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003708 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003709 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003710 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003711 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003712 .ops = &mv88e6172_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003713 },
3714
3715 [MV88E6175] = {
3716 .prod_num = PORT_SWITCH_ID_PROD_NUM_6175,
3717 .family = MV88E6XXX_FAMILY_6351,
3718 .name = "Marvell 88E6175",
3719 .num_databases = 4096,
3720 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003721 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003722 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003723 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003724 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003725 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003726 .ops = &mv88e6175_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003727 },
3728
3729 [MV88E6176] = {
3730 .prod_num = PORT_SWITCH_ID_PROD_NUM_6176,
3731 .family = MV88E6XXX_FAMILY_6352,
3732 .name = "Marvell 88E6176",
3733 .num_databases = 4096,
3734 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003735 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003736 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003737 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003738 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003739 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003740 .ops = &mv88e6176_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003741 },
3742
3743 [MV88E6185] = {
3744 .prod_num = PORT_SWITCH_ID_PROD_NUM_6185,
3745 .family = MV88E6XXX_FAMILY_6185,
3746 .name = "Marvell 88E6185",
3747 .num_databases = 256,
3748 .num_ports = 10,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003749 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003750 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003751 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003752 .g1_irqs = 8,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003753 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003754 .ops = &mv88e6185_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003755 },
3756
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003757 [MV88E6190] = {
3758 .prod_num = PORT_SWITCH_ID_PROD_NUM_6190,
3759 .family = MV88E6XXX_FAMILY_6390,
3760 .name = "Marvell 88E6190",
3761 .num_databases = 4096,
3762 .num_ports = 11, /* 10 + Z80 */
3763 .port_base_addr = 0x0,
3764 .global1_addr = 0x1b,
3765 .age_time_coeff = 15000,
3766 .g1_irqs = 9,
3767 .flags = MV88E6XXX_FLAGS_FAMILY_6390,
3768 .ops = &mv88e6190_ops,
3769 },
3770
3771 [MV88E6190X] = {
3772 .prod_num = PORT_SWITCH_ID_PROD_NUM_6190X,
3773 .family = MV88E6XXX_FAMILY_6390,
3774 .name = "Marvell 88E6190X",
3775 .num_databases = 4096,
3776 .num_ports = 11, /* 10 + Z80 */
3777 .port_base_addr = 0x0,
3778 .global1_addr = 0x1b,
3779 .age_time_coeff = 15000,
3780 .g1_irqs = 9,
3781 .flags = MV88E6XXX_FLAGS_FAMILY_6390,
3782 .ops = &mv88e6190x_ops,
3783 },
3784
3785 [MV88E6191] = {
3786 .prod_num = PORT_SWITCH_ID_PROD_NUM_6191,
3787 .family = MV88E6XXX_FAMILY_6390,
3788 .name = "Marvell 88E6191",
3789 .num_databases = 4096,
3790 .num_ports = 11, /* 10 + Z80 */
3791 .port_base_addr = 0x0,
3792 .global1_addr = 0x1b,
3793 .age_time_coeff = 15000,
3794 .flags = MV88E6XXX_FLAGS_FAMILY_6390,
3795 .ops = &mv88e6391_ops,
3796 },
3797
Vivien Didelotf81ec902016-05-09 13:22:58 -04003798 [MV88E6240] = {
3799 .prod_num = PORT_SWITCH_ID_PROD_NUM_6240,
3800 .family = MV88E6XXX_FAMILY_6352,
3801 .name = "Marvell 88E6240",
3802 .num_databases = 4096,
3803 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003804 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003805 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003806 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003807 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003808 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003809 .ops = &mv88e6240_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003810 },
3811
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003812 [MV88E6290] = {
3813 .prod_num = PORT_SWITCH_ID_PROD_NUM_6290,
3814 .family = MV88E6XXX_FAMILY_6390,
3815 .name = "Marvell 88E6290",
3816 .num_databases = 4096,
3817 .num_ports = 11, /* 10 + Z80 */
3818 .port_base_addr = 0x0,
3819 .global1_addr = 0x1b,
3820 .age_time_coeff = 15000,
3821 .g1_irqs = 9,
3822 .flags = MV88E6XXX_FLAGS_FAMILY_6390,
3823 .ops = &mv88e6290_ops,
3824 },
3825
Vivien Didelotf81ec902016-05-09 13:22:58 -04003826 [MV88E6320] = {
3827 .prod_num = PORT_SWITCH_ID_PROD_NUM_6320,
3828 .family = MV88E6XXX_FAMILY_6320,
3829 .name = "Marvell 88E6320",
3830 .num_databases = 4096,
3831 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003832 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003833 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003834 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003835 .g1_irqs = 8,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003836 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003837 .ops = &mv88e6320_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003838 },
3839
3840 [MV88E6321] = {
3841 .prod_num = PORT_SWITCH_ID_PROD_NUM_6321,
3842 .family = MV88E6XXX_FAMILY_6320,
3843 .name = "Marvell 88E6321",
3844 .num_databases = 4096,
3845 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003846 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003847 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003848 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003849 .g1_irqs = 8,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003850 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003851 .ops = &mv88e6321_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003852 },
3853
3854 [MV88E6350] = {
3855 .prod_num = PORT_SWITCH_ID_PROD_NUM_6350,
3856 .family = MV88E6XXX_FAMILY_6351,
3857 .name = "Marvell 88E6350",
3858 .num_databases = 4096,
3859 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003860 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003861 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003862 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003863 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003864 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003865 .ops = &mv88e6350_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003866 },
3867
3868 [MV88E6351] = {
3869 .prod_num = PORT_SWITCH_ID_PROD_NUM_6351,
3870 .family = MV88E6XXX_FAMILY_6351,
3871 .name = "Marvell 88E6351",
3872 .num_databases = 4096,
3873 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003874 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003875 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003876 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003877 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003878 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003879 .ops = &mv88e6351_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003880 },
3881
3882 [MV88E6352] = {
3883 .prod_num = PORT_SWITCH_ID_PROD_NUM_6352,
3884 .family = MV88E6XXX_FAMILY_6352,
3885 .name = "Marvell 88E6352",
3886 .num_databases = 4096,
3887 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003888 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003889 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003890 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003891 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003892 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003893 .ops = &mv88e6352_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003894 },
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003895 [MV88E6390] = {
3896 .prod_num = PORT_SWITCH_ID_PROD_NUM_6390,
3897 .family = MV88E6XXX_FAMILY_6390,
3898 .name = "Marvell 88E6390",
3899 .num_databases = 4096,
3900 .num_ports = 11, /* 10 + Z80 */
3901 .port_base_addr = 0x0,
3902 .global1_addr = 0x1b,
3903 .age_time_coeff = 15000,
3904 .g1_irqs = 9,
3905 .flags = MV88E6XXX_FLAGS_FAMILY_6390,
3906 .ops = &mv88e6390_ops,
3907 },
3908 [MV88E6390X] = {
3909 .prod_num = PORT_SWITCH_ID_PROD_NUM_6390X,
3910 .family = MV88E6XXX_FAMILY_6390,
3911 .name = "Marvell 88E6390X",
3912 .num_databases = 4096,
3913 .num_ports = 11, /* 10 + Z80 */
3914 .port_base_addr = 0x0,
3915 .global1_addr = 0x1b,
3916 .age_time_coeff = 15000,
3917 .g1_irqs = 9,
3918 .flags = MV88E6XXX_FLAGS_FAMILY_6390,
3919 .ops = &mv88e6390x_ops,
3920 },
Vivien Didelotf81ec902016-05-09 13:22:58 -04003921};
3922
Vivien Didelot5f7c0362016-06-20 13:14:04 -04003923static const struct mv88e6xxx_info *mv88e6xxx_lookup_info(unsigned int prod_num)
Vivien Didelotb9b37712015-10-30 19:39:48 -04003924{
Vivien Didelota439c062016-04-17 13:23:58 -04003925 int i;
Vivien Didelotb9b37712015-10-30 19:39:48 -04003926
Vivien Didelot5f7c0362016-06-20 13:14:04 -04003927 for (i = 0; i < ARRAY_SIZE(mv88e6xxx_table); ++i)
3928 if (mv88e6xxx_table[i].prod_num == prod_num)
3929 return &mv88e6xxx_table[i];
Vivien Didelotb9b37712015-10-30 19:39:48 -04003930
Vivien Didelotb9b37712015-10-30 19:39:48 -04003931 return NULL;
3932}
3933
Vivien Didelotfad09c72016-06-21 12:28:20 -04003934static int mv88e6xxx_detect(struct mv88e6xxx_chip *chip)
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003935{
3936 const struct mv88e6xxx_info *info;
Vivien Didelot8f6345b2016-07-20 18:18:36 -04003937 unsigned int prod_num, rev;
3938 u16 id;
3939 int err;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003940
Vivien Didelot8f6345b2016-07-20 18:18:36 -04003941 mutex_lock(&chip->reg_lock);
3942 err = mv88e6xxx_port_read(chip, 0, PORT_SWITCH_ID, &id);
3943 mutex_unlock(&chip->reg_lock);
3944 if (err)
3945 return err;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003946
3947 prod_num = (id & 0xfff0) >> 4;
3948 rev = id & 0x000f;
3949
3950 info = mv88e6xxx_lookup_info(prod_num);
3951 if (!info)
3952 return -ENODEV;
3953
Vivien Didelotcaac8542016-06-20 13:14:09 -04003954 /* Update the compatible info with the probed one */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003955 chip->info = info;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003956
Vivien Didelotca070c12016-09-02 14:45:34 -04003957 err = mv88e6xxx_g2_require(chip);
3958 if (err)
3959 return err;
3960
Vivien Didelotfad09c72016-06-21 12:28:20 -04003961 dev_info(chip->dev, "switch 0x%x detected: %s, revision %u\n",
3962 chip->info->prod_num, chip->info->name, rev);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003963
3964 return 0;
3965}
3966
Vivien Didelotfad09c72016-06-21 12:28:20 -04003967static struct mv88e6xxx_chip *mv88e6xxx_alloc_chip(struct device *dev)
Vivien Didelot469d7292016-06-20 13:14:06 -04003968{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003969 struct mv88e6xxx_chip *chip;
Vivien Didelot469d7292016-06-20 13:14:06 -04003970
Vivien Didelotfad09c72016-06-21 12:28:20 -04003971 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
3972 if (!chip)
Vivien Didelot469d7292016-06-20 13:14:06 -04003973 return NULL;
3974
Vivien Didelotfad09c72016-06-21 12:28:20 -04003975 chip->dev = dev;
Vivien Didelot469d7292016-06-20 13:14:06 -04003976
Vivien Didelotfad09c72016-06-21 12:28:20 -04003977 mutex_init(&chip->reg_lock);
Vivien Didelot469d7292016-06-20 13:14:06 -04003978
Vivien Didelotfad09c72016-06-21 12:28:20 -04003979 return chip;
Vivien Didelot469d7292016-06-20 13:14:06 -04003980}
3981
Vivien Didelote57e5e72016-08-15 17:19:00 -04003982static void mv88e6xxx_phy_init(struct mv88e6xxx_chip *chip)
3983{
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003984 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU))
Vivien Didelote57e5e72016-08-15 17:19:00 -04003985 mv88e6xxx_ppu_state_init(chip);
Vivien Didelote57e5e72016-08-15 17:19:00 -04003986}
3987
Andrew Lunn930188c2016-08-22 16:01:03 +02003988static void mv88e6xxx_phy_destroy(struct mv88e6xxx_chip *chip)
3989{
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003990 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU))
Andrew Lunn930188c2016-08-22 16:01:03 +02003991 mv88e6xxx_ppu_state_destroy(chip);
Andrew Lunn930188c2016-08-22 16:01:03 +02003992}
3993
Vivien Didelotfad09c72016-06-21 12:28:20 -04003994static int mv88e6xxx_smi_init(struct mv88e6xxx_chip *chip,
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003995 struct mii_bus *bus, int sw_addr)
3996{
3997 /* ADDR[0] pin is unavailable externally and considered zero */
3998 if (sw_addr & 0x1)
3999 return -EINVAL;
4000
Vivien Didelot914b32f2016-06-20 13:14:11 -04004001 if (sw_addr == 0)
Vivien Didelotfad09c72016-06-21 12:28:20 -04004002 chip->smi_ops = &mv88e6xxx_smi_single_chip_ops;
Vivien Didelota0ffff22016-08-15 17:18:58 -04004003 else if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_MULTI_CHIP))
Vivien Didelotfad09c72016-06-21 12:28:20 -04004004 chip->smi_ops = &mv88e6xxx_smi_multi_chip_ops;
Vivien Didelot914b32f2016-06-20 13:14:11 -04004005 else
4006 return -EINVAL;
4007
Vivien Didelotfad09c72016-06-21 12:28:20 -04004008 chip->bus = bus;
4009 chip->sw_addr = sw_addr;
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04004010
4011 return 0;
4012}
4013
Andrew Lunn7b314362016-08-22 16:01:01 +02004014static enum dsa_tag_protocol mv88e6xxx_get_tag_protocol(struct dsa_switch *ds)
4015{
Vivien Didelot04bed142016-08-31 18:06:13 -04004016 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02004017
4018 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA))
4019 return DSA_TAG_PROTO_EDSA;
4020
4021 return DSA_TAG_PROTO_DSA;
Andrew Lunn7b314362016-08-22 16:01:01 +02004022}
4023
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02004024static const char *mv88e6xxx_drv_probe(struct device *dsa_dev,
4025 struct device *host_dev, int sw_addr,
4026 void **priv)
Andrew Lunna77d43f2016-04-13 02:40:42 +02004027{
Vivien Didelotfad09c72016-06-21 12:28:20 -04004028 struct mv88e6xxx_chip *chip;
Vivien Didelota439c062016-04-17 13:23:58 -04004029 struct mii_bus *bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02004030 int err;
Andrew Lunna77d43f2016-04-13 02:40:42 +02004031
Vivien Didelota439c062016-04-17 13:23:58 -04004032 bus = dsa_host_dev_to_mii_bus(host_dev);
Andrew Lunnc1569132016-04-13 02:40:45 +02004033 if (!bus)
4034 return NULL;
4035
Vivien Didelotfad09c72016-06-21 12:28:20 -04004036 chip = mv88e6xxx_alloc_chip(dsa_dev);
4037 if (!chip)
Vivien Didelot469d7292016-06-20 13:14:06 -04004038 return NULL;
4039
Vivien Didelotcaac8542016-06-20 13:14:09 -04004040 /* Legacy SMI probing will only support chips similar to 88E6085 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04004041 chip->info = &mv88e6xxx_table[MV88E6085];
Vivien Didelotcaac8542016-06-20 13:14:09 -04004042
Vivien Didelotfad09c72016-06-21 12:28:20 -04004043 err = mv88e6xxx_smi_init(chip, bus, sw_addr);
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04004044 if (err)
4045 goto free;
4046
Vivien Didelotfad09c72016-06-21 12:28:20 -04004047 err = mv88e6xxx_detect(chip);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04004048 if (err)
Vivien Didelot469d7292016-06-20 13:14:06 -04004049 goto free;
Vivien Didelota439c062016-04-17 13:23:58 -04004050
Andrew Lunndc30c352016-10-16 19:56:49 +02004051 mutex_lock(&chip->reg_lock);
4052 err = mv88e6xxx_switch_reset(chip);
4053 mutex_unlock(&chip->reg_lock);
4054 if (err)
4055 goto free;
4056
Vivien Didelote57e5e72016-08-15 17:19:00 -04004057 mv88e6xxx_phy_init(chip);
4058
Vivien Didelotfad09c72016-06-21 12:28:20 -04004059 err = mv88e6xxx_mdio_register(chip, NULL);
Andrew Lunnb516d452016-06-04 21:17:06 +02004060 if (err)
Vivien Didelot469d7292016-06-20 13:14:06 -04004061 goto free;
Andrew Lunnb516d452016-06-04 21:17:06 +02004062
Vivien Didelotfad09c72016-06-21 12:28:20 -04004063 *priv = chip;
Vivien Didelota439c062016-04-17 13:23:58 -04004064
Vivien Didelotfad09c72016-06-21 12:28:20 -04004065 return chip->info->name;
Vivien Didelot469d7292016-06-20 13:14:06 -04004066free:
Vivien Didelotfad09c72016-06-21 12:28:20 -04004067 devm_kfree(dsa_dev, chip);
Vivien Didelot469d7292016-06-20 13:14:06 -04004068
4069 return NULL;
Andrew Lunna77d43f2016-04-13 02:40:42 +02004070}
4071
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04004072static int mv88e6xxx_port_mdb_prepare(struct dsa_switch *ds, int port,
4073 const struct switchdev_obj_port_mdb *mdb,
4074 struct switchdev_trans *trans)
4075{
4076 /* We don't need any dynamic resource from the kernel (yet),
4077 * so skip the prepare phase.
4078 */
4079
4080 return 0;
4081}
4082
4083static void mv88e6xxx_port_mdb_add(struct dsa_switch *ds, int port,
4084 const struct switchdev_obj_port_mdb *mdb,
4085 struct switchdev_trans *trans)
4086{
Vivien Didelot04bed142016-08-31 18:06:13 -04004087 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04004088
4089 mutex_lock(&chip->reg_lock);
4090 if (mv88e6xxx_port_db_load_purge(chip, port, mdb->addr, mdb->vid,
4091 GLOBAL_ATU_DATA_STATE_MC_STATIC))
4092 netdev_err(ds->ports[port].netdev, "failed to load multicast MAC address\n");
4093 mutex_unlock(&chip->reg_lock);
4094}
4095
4096static int mv88e6xxx_port_mdb_del(struct dsa_switch *ds, int port,
4097 const struct switchdev_obj_port_mdb *mdb)
4098{
Vivien Didelot04bed142016-08-31 18:06:13 -04004099 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04004100 int err;
4101
4102 mutex_lock(&chip->reg_lock);
4103 err = mv88e6xxx_port_db_load_purge(chip, port, mdb->addr, mdb->vid,
4104 GLOBAL_ATU_DATA_STATE_UNUSED);
4105 mutex_unlock(&chip->reg_lock);
4106
4107 return err;
4108}
4109
4110static int mv88e6xxx_port_mdb_dump(struct dsa_switch *ds, int port,
4111 struct switchdev_obj_port_mdb *mdb,
4112 int (*cb)(struct switchdev_obj *obj))
4113{
Vivien Didelot04bed142016-08-31 18:06:13 -04004114 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04004115 int err;
4116
4117 mutex_lock(&chip->reg_lock);
4118 err = mv88e6xxx_port_db_dump(chip, port, &mdb->obj, cb);
4119 mutex_unlock(&chip->reg_lock);
4120
4121 return err;
4122}
4123
Vivien Didelot9d490b42016-08-23 12:38:56 -04004124static struct dsa_switch_ops mv88e6xxx_switch_ops = {
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02004125 .probe = mv88e6xxx_drv_probe,
Andrew Lunn7b314362016-08-22 16:01:01 +02004126 .get_tag_protocol = mv88e6xxx_get_tag_protocol,
Vivien Didelotf81ec902016-05-09 13:22:58 -04004127 .setup = mv88e6xxx_setup,
4128 .set_addr = mv88e6xxx_set_addr,
Vivien Didelotf81ec902016-05-09 13:22:58 -04004129 .adjust_link = mv88e6xxx_adjust_link,
4130 .get_strings = mv88e6xxx_get_strings,
4131 .get_ethtool_stats = mv88e6xxx_get_ethtool_stats,
4132 .get_sset_count = mv88e6xxx_get_sset_count,
4133 .set_eee = mv88e6xxx_set_eee,
4134 .get_eee = mv88e6xxx_get_eee,
4135#ifdef CONFIG_NET_DSA_HWMON
4136 .get_temp = mv88e6xxx_get_temp,
4137 .get_temp_limit = mv88e6xxx_get_temp_limit,
4138 .set_temp_limit = mv88e6xxx_set_temp_limit,
4139 .get_temp_alarm = mv88e6xxx_get_temp_alarm,
4140#endif
Andrew Lunnf8cd8752016-05-10 23:27:25 +02004141 .get_eeprom_len = mv88e6xxx_get_eeprom_len,
Vivien Didelotf81ec902016-05-09 13:22:58 -04004142 .get_eeprom = mv88e6xxx_get_eeprom,
4143 .set_eeprom = mv88e6xxx_set_eeprom,
4144 .get_regs_len = mv88e6xxx_get_regs_len,
4145 .get_regs = mv88e6xxx_get_regs,
Vivien Didelot2cfcd962016-07-18 20:45:40 -04004146 .set_ageing_time = mv88e6xxx_set_ageing_time,
Vivien Didelotf81ec902016-05-09 13:22:58 -04004147 .port_bridge_join = mv88e6xxx_port_bridge_join,
4148 .port_bridge_leave = mv88e6xxx_port_bridge_leave,
4149 .port_stp_state_set = mv88e6xxx_port_stp_state_set,
Vivien Didelot749efcb2016-09-22 16:49:24 -04004150 .port_fast_age = mv88e6xxx_port_fast_age,
Vivien Didelotf81ec902016-05-09 13:22:58 -04004151 .port_vlan_filtering = mv88e6xxx_port_vlan_filtering,
4152 .port_vlan_prepare = mv88e6xxx_port_vlan_prepare,
4153 .port_vlan_add = mv88e6xxx_port_vlan_add,
4154 .port_vlan_del = mv88e6xxx_port_vlan_del,
4155 .port_vlan_dump = mv88e6xxx_port_vlan_dump,
4156 .port_fdb_prepare = mv88e6xxx_port_fdb_prepare,
4157 .port_fdb_add = mv88e6xxx_port_fdb_add,
4158 .port_fdb_del = mv88e6xxx_port_fdb_del,
4159 .port_fdb_dump = mv88e6xxx_port_fdb_dump,
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04004160 .port_mdb_prepare = mv88e6xxx_port_mdb_prepare,
4161 .port_mdb_add = mv88e6xxx_port_mdb_add,
4162 .port_mdb_del = mv88e6xxx_port_mdb_del,
4163 .port_mdb_dump = mv88e6xxx_port_mdb_dump,
Vivien Didelotf81ec902016-05-09 13:22:58 -04004164};
4165
Vivien Didelotfad09c72016-06-21 12:28:20 -04004166static int mv88e6xxx_register_switch(struct mv88e6xxx_chip *chip,
Vivien Didelotb7e66a52016-06-20 13:14:02 -04004167 struct device_node *np)
4168{
Vivien Didelotfad09c72016-06-21 12:28:20 -04004169 struct device *dev = chip->dev;
Vivien Didelotb7e66a52016-06-20 13:14:02 -04004170 struct dsa_switch *ds;
4171
4172 ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
4173 if (!ds)
4174 return -ENOMEM;
4175
4176 ds->dev = dev;
Vivien Didelotfad09c72016-06-21 12:28:20 -04004177 ds->priv = chip;
Vivien Didelot9d490b42016-08-23 12:38:56 -04004178 ds->ops = &mv88e6xxx_switch_ops;
Vivien Didelotb7e66a52016-06-20 13:14:02 -04004179
4180 dev_set_drvdata(dev, ds);
4181
4182 return dsa_register_switch(ds, np);
4183}
4184
Vivien Didelotfad09c72016-06-21 12:28:20 -04004185static void mv88e6xxx_unregister_switch(struct mv88e6xxx_chip *chip)
Vivien Didelotb7e66a52016-06-20 13:14:02 -04004186{
Vivien Didelotfad09c72016-06-21 12:28:20 -04004187 dsa_unregister_switch(chip->ds);
Vivien Didelotb7e66a52016-06-20 13:14:02 -04004188}
4189
Vivien Didelot57d32312016-06-20 13:13:58 -04004190static int mv88e6xxx_probe(struct mdio_device *mdiodev)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004191{
4192 struct device *dev = &mdiodev->dev;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02004193 struct device_node *np = dev->of_node;
Vivien Didelotcaac8542016-06-20 13:14:09 -04004194 const struct mv88e6xxx_info *compat_info;
Vivien Didelotfad09c72016-06-21 12:28:20 -04004195 struct mv88e6xxx_chip *chip;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02004196 u32 eeprom_len;
Andrew Lunn52638f72016-05-10 23:27:22 +02004197 int err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004198
Vivien Didelotcaac8542016-06-20 13:14:09 -04004199 compat_info = of_device_get_match_data(dev);
4200 if (!compat_info)
4201 return -EINVAL;
4202
Vivien Didelotfad09c72016-06-21 12:28:20 -04004203 chip = mv88e6xxx_alloc_chip(dev);
4204 if (!chip)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004205 return -ENOMEM;
4206
Vivien Didelotfad09c72016-06-21 12:28:20 -04004207 chip->info = compat_info;
Vivien Didelotcaac8542016-06-20 13:14:09 -04004208
Vivien Didelotfad09c72016-06-21 12:28:20 -04004209 err = mv88e6xxx_smi_init(chip, mdiodev->bus, mdiodev->addr);
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04004210 if (err)
4211 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004212
Andrew Lunnb4308f02016-11-21 23:26:55 +01004213 chip->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
4214 if (IS_ERR(chip->reset))
4215 return PTR_ERR(chip->reset);
4216
Vivien Didelotfad09c72016-06-21 12:28:20 -04004217 err = mv88e6xxx_detect(chip);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04004218 if (err)
4219 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004220
Vivien Didelote57e5e72016-08-15 17:19:00 -04004221 mv88e6xxx_phy_init(chip);
4222
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04004223 if (chip->info->ops->get_eeprom &&
Andrew Lunnf8cd8752016-05-10 23:27:25 +02004224 !of_property_read_u32(np, "eeprom-length", &eeprom_len))
Vivien Didelotfad09c72016-06-21 12:28:20 -04004225 chip->eeprom_len = eeprom_len;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02004226
Andrew Lunndc30c352016-10-16 19:56:49 +02004227 mutex_lock(&chip->reg_lock);
4228 err = mv88e6xxx_switch_reset(chip);
4229 mutex_unlock(&chip->reg_lock);
Andrew Lunnb516d452016-06-04 21:17:06 +02004230 if (err)
Andrew Lunndc30c352016-10-16 19:56:49 +02004231 goto out;
Andrew Lunnb516d452016-06-04 21:17:06 +02004232
Andrew Lunndc30c352016-10-16 19:56:49 +02004233 chip->irq = of_irq_get(np, 0);
4234 if (chip->irq == -EPROBE_DEFER) {
4235 err = chip->irq;
4236 goto out;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02004237 }
4238
Andrew Lunndc30c352016-10-16 19:56:49 +02004239 if (chip->irq > 0) {
4240 /* Has to be performed before the MDIO bus is created,
4241 * because the PHYs will link there interrupts to these
4242 * interrupt controllers
4243 */
4244 mutex_lock(&chip->reg_lock);
4245 err = mv88e6xxx_g1_irq_setup(chip);
4246 mutex_unlock(&chip->reg_lock);
4247
4248 if (err)
4249 goto out;
4250
4251 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_INT)) {
4252 err = mv88e6xxx_g2_irq_setup(chip);
4253 if (err)
4254 goto out_g1_irq;
4255 }
4256 }
4257
4258 err = mv88e6xxx_mdio_register(chip, np);
4259 if (err)
4260 goto out_g2_irq;
4261
4262 err = mv88e6xxx_register_switch(chip, np);
4263 if (err)
4264 goto out_mdio;
4265
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004266 return 0;
Andrew Lunndc30c352016-10-16 19:56:49 +02004267
4268out_mdio:
4269 mv88e6xxx_mdio_unregister(chip);
4270out_g2_irq:
Andrew Lunn467126442016-11-20 20:14:15 +01004271 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_INT) && chip->irq > 0)
Andrew Lunndc30c352016-10-16 19:56:49 +02004272 mv88e6xxx_g2_irq_free(chip);
4273out_g1_irq:
Andrew Lunn61f7c3f2016-11-20 20:14:19 +01004274 if (chip->irq > 0) {
4275 mutex_lock(&chip->reg_lock);
Andrew Lunn467126442016-11-20 20:14:15 +01004276 mv88e6xxx_g1_irq_free(chip);
Andrew Lunn61f7c3f2016-11-20 20:14:19 +01004277 mutex_unlock(&chip->reg_lock);
4278 }
Andrew Lunndc30c352016-10-16 19:56:49 +02004279out:
4280 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004281}
4282
4283static void mv88e6xxx_remove(struct mdio_device *mdiodev)
4284{
4285 struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev);
Vivien Didelot04bed142016-08-31 18:06:13 -04004286 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004287
Andrew Lunn930188c2016-08-22 16:01:03 +02004288 mv88e6xxx_phy_destroy(chip);
Vivien Didelotfad09c72016-06-21 12:28:20 -04004289 mv88e6xxx_unregister_switch(chip);
4290 mv88e6xxx_mdio_unregister(chip);
Andrew Lunndc30c352016-10-16 19:56:49 +02004291
Andrew Lunn467126442016-11-20 20:14:15 +01004292 if (chip->irq > 0) {
4293 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_INT))
4294 mv88e6xxx_g2_irq_free(chip);
4295 mv88e6xxx_g1_irq_free(chip);
4296 }
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004297}
4298
4299static const struct of_device_id mv88e6xxx_of_match[] = {
Vivien Didelotcaac8542016-06-20 13:14:09 -04004300 {
4301 .compatible = "marvell,mv88e6085",
4302 .data = &mv88e6xxx_table[MV88E6085],
4303 },
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01004304 {
4305 .compatible = "marvell,mv88e6190",
4306 .data = &mv88e6xxx_table[MV88E6190],
4307 },
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004308 { /* sentinel */ },
4309};
4310
4311MODULE_DEVICE_TABLE(of, mv88e6xxx_of_match);
4312
4313static struct mdio_driver mv88e6xxx_driver = {
4314 .probe = mv88e6xxx_probe,
4315 .remove = mv88e6xxx_remove,
4316 .mdiodrv.driver = {
4317 .name = "mv88e6085",
4318 .of_match_table = mv88e6xxx_of_match,
4319 },
4320};
4321
Ben Hutchings98e67302011-11-25 14:36:19 +00004322static int __init mv88e6xxx_init(void)
4323{
Vivien Didelot9d490b42016-08-23 12:38:56 -04004324 register_switch_driver(&mv88e6xxx_switch_ops);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004325 return mdio_driver_register(&mv88e6xxx_driver);
Ben Hutchings98e67302011-11-25 14:36:19 +00004326}
4327module_init(mv88e6xxx_init);
4328
4329static void __exit mv88e6xxx_cleanup(void)
4330{
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004331 mdio_driver_unregister(&mv88e6xxx_driver);
Vivien Didelot9d490b42016-08-23 12:38:56 -04004332 unregister_switch_driver(&mv88e6xxx_switch_ops);
Ben Hutchings98e67302011-11-25 14:36:19 +00004333}
4334module_exit(mv88e6xxx_cleanup);
Ben Hutchings3d825ed2011-11-25 14:37:16 +00004335
4336MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
4337MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
4338MODULE_LICENSE("GPL");