blob: c228cb1bb1713121c57bac2922bafb1bee46b862 [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,
818 GLOBAL_STATS_OP_READ_CAPTURED |
819 GLOBAL_STATS_OP_HIST_RX_TX | stat);
820 if (err)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000821 return;
822
Vivien Didelota935c052016-09-29 12:21:53 -0400823 err = _mv88e6xxx_stats_wait(chip);
824 if (err)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000825 return;
826
Vivien Didelota935c052016-09-29 12:21:53 -0400827 err = mv88e6xxx_g1_read(chip, GLOBAL_STATS_COUNTER_32, &reg);
828 if (err)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000829 return;
830
Vivien Didelota935c052016-09-29 12:21:53 -0400831 value = reg << 16;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000832
Vivien Didelota935c052016-09-29 12:21:53 -0400833 err = mv88e6xxx_g1_read(chip, GLOBAL_STATS_COUNTER_01, &reg);
834 if (err)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000835 return;
836
Vivien Didelota935c052016-09-29 12:21:53 -0400837 *val = value | reg;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000838}
839
Andrew Lunne413e7e2015-04-02 04:06:38 +0200840static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100841 { "in_good_octets", 8, 0x00, BANK0, },
842 { "in_bad_octets", 4, 0x02, BANK0, },
843 { "in_unicast", 4, 0x04, BANK0, },
844 { "in_broadcasts", 4, 0x06, BANK0, },
845 { "in_multicasts", 4, 0x07, BANK0, },
846 { "in_pause", 4, 0x16, BANK0, },
847 { "in_undersize", 4, 0x18, BANK0, },
848 { "in_fragments", 4, 0x19, BANK0, },
849 { "in_oversize", 4, 0x1a, BANK0, },
850 { "in_jabber", 4, 0x1b, BANK0, },
851 { "in_rx_error", 4, 0x1c, BANK0, },
852 { "in_fcs_error", 4, 0x1d, BANK0, },
853 { "out_octets", 8, 0x0e, BANK0, },
854 { "out_unicast", 4, 0x10, BANK0, },
855 { "out_broadcasts", 4, 0x13, BANK0, },
856 { "out_multicasts", 4, 0x12, BANK0, },
857 { "out_pause", 4, 0x15, BANK0, },
858 { "excessive", 4, 0x11, BANK0, },
859 { "collisions", 4, 0x1e, BANK0, },
860 { "deferred", 4, 0x05, BANK0, },
861 { "single", 4, 0x14, BANK0, },
862 { "multiple", 4, 0x17, BANK0, },
863 { "out_fcs_error", 4, 0x03, BANK0, },
864 { "late", 4, 0x1f, BANK0, },
865 { "hist_64bytes", 4, 0x08, BANK0, },
866 { "hist_65_127bytes", 4, 0x09, BANK0, },
867 { "hist_128_255bytes", 4, 0x0a, BANK0, },
868 { "hist_256_511bytes", 4, 0x0b, BANK0, },
869 { "hist_512_1023bytes", 4, 0x0c, BANK0, },
870 { "hist_1024_max_bytes", 4, 0x0d, BANK0, },
871 { "sw_in_discards", 4, 0x10, PORT, },
872 { "sw_in_filtered", 2, 0x12, PORT, },
873 { "sw_out_filtered", 2, 0x13, PORT, },
874 { "in_discards", 4, 0x00 | GLOBAL_STATS_OP_BANK_1, BANK1, },
875 { "in_filtered", 4, 0x01 | GLOBAL_STATS_OP_BANK_1, BANK1, },
876 { "in_accepted", 4, 0x02 | GLOBAL_STATS_OP_BANK_1, BANK1, },
877 { "in_bad_accepted", 4, 0x03 | GLOBAL_STATS_OP_BANK_1, BANK1, },
878 { "in_good_avb_class_a", 4, 0x04 | GLOBAL_STATS_OP_BANK_1, BANK1, },
879 { "in_good_avb_class_b", 4, 0x05 | GLOBAL_STATS_OP_BANK_1, BANK1, },
880 { "in_bad_avb_class_a", 4, 0x06 | GLOBAL_STATS_OP_BANK_1, BANK1, },
881 { "in_bad_avb_class_b", 4, 0x07 | GLOBAL_STATS_OP_BANK_1, BANK1, },
882 { "tcam_counter_0", 4, 0x08 | GLOBAL_STATS_OP_BANK_1, BANK1, },
883 { "tcam_counter_1", 4, 0x09 | GLOBAL_STATS_OP_BANK_1, BANK1, },
884 { "tcam_counter_2", 4, 0x0a | GLOBAL_STATS_OP_BANK_1, BANK1, },
885 { "tcam_counter_3", 4, 0x0b | GLOBAL_STATS_OP_BANK_1, BANK1, },
886 { "in_da_unknown", 4, 0x0e | GLOBAL_STATS_OP_BANK_1, BANK1, },
887 { "in_management", 4, 0x0f | GLOBAL_STATS_OP_BANK_1, BANK1, },
888 { "out_queue_0", 4, 0x10 | GLOBAL_STATS_OP_BANK_1, BANK1, },
889 { "out_queue_1", 4, 0x11 | GLOBAL_STATS_OP_BANK_1, BANK1, },
890 { "out_queue_2", 4, 0x12 | GLOBAL_STATS_OP_BANK_1, BANK1, },
891 { "out_queue_3", 4, 0x13 | GLOBAL_STATS_OP_BANK_1, BANK1, },
892 { "out_queue_4", 4, 0x14 | GLOBAL_STATS_OP_BANK_1, BANK1, },
893 { "out_queue_5", 4, 0x15 | GLOBAL_STATS_OP_BANK_1, BANK1, },
894 { "out_queue_6", 4, 0x16 | GLOBAL_STATS_OP_BANK_1, BANK1, },
895 { "out_queue_7", 4, 0x17 | GLOBAL_STATS_OP_BANK_1, BANK1, },
896 { "out_cut_through", 4, 0x18 | GLOBAL_STATS_OP_BANK_1, BANK1, },
897 { "out_octets_a", 4, 0x1a | GLOBAL_STATS_OP_BANK_1, BANK1, },
898 { "out_octets_b", 4, 0x1b | GLOBAL_STATS_OP_BANK_1, BANK1, },
899 { "out_management", 4, 0x1f | GLOBAL_STATS_OP_BANK_1, BANK1, },
Andrew Lunne413e7e2015-04-02 04:06:38 +0200900};
901
Vivien Didelotfad09c72016-06-21 12:28:20 -0400902static bool mv88e6xxx_has_stat(struct mv88e6xxx_chip *chip,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100903 struct mv88e6xxx_hw_stat *stat)
Andrew Lunne413e7e2015-04-02 04:06:38 +0200904{
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100905 switch (stat->type) {
906 case BANK0:
Andrew Lunne413e7e2015-04-02 04:06:38 +0200907 return true;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100908 case BANK1:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400909 return mv88e6xxx_6320_family(chip);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100910 case PORT:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400911 return mv88e6xxx_6095_family(chip) ||
912 mv88e6xxx_6185_family(chip) ||
913 mv88e6xxx_6097_family(chip) ||
914 mv88e6xxx_6165_family(chip) ||
915 mv88e6xxx_6351_family(chip) ||
916 mv88e6xxx_6352_family(chip);
Andrew Lunne413e7e2015-04-02 04:06:38 +0200917 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100918 return false;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000919}
920
Vivien Didelotfad09c72016-06-21 12:28:20 -0400921static uint64_t _mv88e6xxx_get_ethtool_stat(struct mv88e6xxx_chip *chip,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100922 struct mv88e6xxx_hw_stat *s,
Andrew Lunn80c46272015-06-20 18:42:30 +0200923 int port)
924{
Andrew Lunn80c46272015-06-20 18:42:30 +0200925 u32 low;
926 u32 high = 0;
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200927 int err;
928 u16 reg;
Andrew Lunn80c46272015-06-20 18:42:30 +0200929 u64 value;
930
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100931 switch (s->type) {
932 case PORT:
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200933 err = mv88e6xxx_port_read(chip, port, s->reg, &reg);
934 if (err)
Andrew Lunn80c46272015-06-20 18:42:30 +0200935 return UINT64_MAX;
936
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200937 low = reg;
Andrew Lunn80c46272015-06-20 18:42:30 +0200938 if (s->sizeof_stat == 4) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200939 err = mv88e6xxx_port_read(chip, port, s->reg + 1, &reg);
940 if (err)
Andrew Lunn80c46272015-06-20 18:42:30 +0200941 return UINT64_MAX;
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200942 high = reg;
Andrew Lunn80c46272015-06-20 18:42:30 +0200943 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100944 break;
945 case BANK0:
946 case BANK1:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400947 _mv88e6xxx_stats_read(chip, s->reg, &low);
Andrew Lunn80c46272015-06-20 18:42:30 +0200948 if (s->sizeof_stat == 8)
Vivien Didelotfad09c72016-06-21 12:28:20 -0400949 _mv88e6xxx_stats_read(chip, s->reg + 1, &high);
Andrew Lunn80c46272015-06-20 18:42:30 +0200950 }
951 value = (((u64)high) << 16) | low;
952 return value;
953}
954
Vivien Didelotf81ec902016-05-09 13:22:58 -0400955static void mv88e6xxx_get_strings(struct dsa_switch *ds, int port,
956 uint8_t *data)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100957{
Vivien Didelot04bed142016-08-31 18:06:13 -0400958 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100959 struct mv88e6xxx_hw_stat *stat;
960 int i, j;
961
962 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
963 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400964 if (mv88e6xxx_has_stat(chip, stat)) {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100965 memcpy(data + j * ETH_GSTRING_LEN, stat->string,
966 ETH_GSTRING_LEN);
967 j++;
968 }
969 }
970}
971
Vivien Didelotf81ec902016-05-09 13:22:58 -0400972static int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100973{
Vivien Didelot04bed142016-08-31 18:06:13 -0400974 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100975 struct mv88e6xxx_hw_stat *stat;
976 int i, j;
977
978 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
979 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400980 if (mv88e6xxx_has_stat(chip, stat))
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100981 j++;
982 }
983 return j;
984}
985
Vivien Didelotf81ec902016-05-09 13:22:58 -0400986static void mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, int port,
987 uint64_t *data)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000988{
Vivien Didelot04bed142016-08-31 18:06:13 -0400989 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100990 struct mv88e6xxx_hw_stat *stat;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000991 int ret;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100992 int i, j;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000993
Vivien Didelotfad09c72016-06-21 12:28:20 -0400994 mutex_lock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000995
Andrew Lunna605a0f2016-11-21 23:26:58 +0100996 ret = mv88e6xxx_stats_snapshot(chip, port);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000997 if (ret < 0) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400998 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000999 return;
1000 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +01001001 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
1002 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -04001003 if (mv88e6xxx_has_stat(chip, stat)) {
1004 data[j] = _mv88e6xxx_get_ethtool_stat(chip, stat, port);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +01001005 j++;
1006 }
1007 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001008
Vivien Didelotfad09c72016-06-21 12:28:20 -04001009 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001010}
Ben Hutchings98e67302011-11-25 14:36:19 +00001011
Vivien Didelotf81ec902016-05-09 13:22:58 -04001012static int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001013{
1014 return 32 * sizeof(u16);
1015}
1016
Vivien Didelotf81ec902016-05-09 13:22:58 -04001017static void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
1018 struct ethtool_regs *regs, void *_p)
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001019{
Vivien Didelot04bed142016-08-31 18:06:13 -04001020 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001021 int err;
1022 u16 reg;
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001023 u16 *p = _p;
1024 int i;
1025
1026 regs->version = 0;
1027
1028 memset(p, 0xff, 32 * sizeof(u16));
1029
Vivien Didelotfad09c72016-06-21 12:28:20 -04001030 mutex_lock(&chip->reg_lock);
Vivien Didelot23062512016-05-09 13:22:45 -04001031
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001032 for (i = 0; i < 32; i++) {
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001033
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001034 err = mv88e6xxx_port_read(chip, port, i, &reg);
1035 if (!err)
1036 p[i] = reg;
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001037 }
Vivien Didelot23062512016-05-09 13:22:45 -04001038
Vivien Didelotfad09c72016-06-21 12:28:20 -04001039 mutex_unlock(&chip->reg_lock);
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001040}
1041
Vivien Didelotfad09c72016-06-21 12:28:20 -04001042static int _mv88e6xxx_atu_wait(struct mv88e6xxx_chip *chip)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001043{
Vivien Didelota935c052016-09-29 12:21:53 -04001044 return mv88e6xxx_g1_wait(chip, GLOBAL_ATU_OP, GLOBAL_ATU_OP_BUSY);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001045}
1046
Vivien Didelotf81ec902016-05-09 13:22:58 -04001047static int mv88e6xxx_get_eee(struct dsa_switch *ds, int port,
1048 struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -08001049{
Vivien Didelot04bed142016-08-31 18:06:13 -04001050 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot9c938292016-08-15 17:19:02 -04001051 u16 reg;
1052 int err;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001053
Vivien Didelotfad09c72016-06-21 12:28:20 -04001054 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
Vivien Didelotaadbdb82016-05-09 13:22:44 -04001055 return -EOPNOTSUPP;
1056
Vivien Didelotfad09c72016-06-21 12:28:20 -04001057 mutex_lock(&chip->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001058
Vivien Didelot9c938292016-08-15 17:19:02 -04001059 err = mv88e6xxx_phy_read(chip, port, 16, &reg);
1060 if (err)
Andrew Lunn2f40c692015-04-02 04:06:37 +02001061 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001062
1063 e->eee_enabled = !!(reg & 0x0200);
1064 e->tx_lpi_enabled = !!(reg & 0x0100);
1065
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001066 err = mv88e6xxx_port_read(chip, port, PORT_STATUS, &reg);
Vivien Didelot9c938292016-08-15 17:19:02 -04001067 if (err)
Andrew Lunn2f40c692015-04-02 04:06:37 +02001068 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001069
Andrew Lunncca8b132015-04-02 04:06:39 +02001070 e->eee_active = !!(reg & PORT_STATUS_EEE);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001071out:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001072 mutex_unlock(&chip->reg_lock);
Vivien Didelot9c938292016-08-15 17:19:02 -04001073
1074 return err;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001075}
1076
Vivien Didelotf81ec902016-05-09 13:22:58 -04001077static int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
1078 struct phy_device *phydev, struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -08001079{
Vivien Didelot04bed142016-08-31 18:06:13 -04001080 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot9c938292016-08-15 17:19:02 -04001081 u16 reg;
1082 int err;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001083
Vivien Didelotfad09c72016-06-21 12:28:20 -04001084 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
Vivien Didelotaadbdb82016-05-09 13:22:44 -04001085 return -EOPNOTSUPP;
1086
Vivien Didelotfad09c72016-06-21 12:28:20 -04001087 mutex_lock(&chip->reg_lock);
Guenter Roeck11b3b452015-03-06 22:23:51 -08001088
Vivien Didelot9c938292016-08-15 17:19:02 -04001089 err = mv88e6xxx_phy_read(chip, port, 16, &reg);
1090 if (err)
Andrew Lunn2f40c692015-04-02 04:06:37 +02001091 goto out;
1092
Vivien Didelot9c938292016-08-15 17:19:02 -04001093 reg &= ~0x0300;
Andrew Lunn2f40c692015-04-02 04:06:37 +02001094 if (e->eee_enabled)
1095 reg |= 0x0200;
1096 if (e->tx_lpi_enabled)
1097 reg |= 0x0100;
1098
Vivien Didelot9c938292016-08-15 17:19:02 -04001099 err = mv88e6xxx_phy_write(chip, port, 16, reg);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001100out:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001101 mutex_unlock(&chip->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001102
Vivien Didelot9c938292016-08-15 17:19:02 -04001103 return err;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001104}
1105
Vivien Didelotfad09c72016-06-21 12:28:20 -04001106static int _mv88e6xxx_atu_cmd(struct mv88e6xxx_chip *chip, u16 fid, u16 cmd)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001107{
Vivien Didelota935c052016-09-29 12:21:53 -04001108 u16 val;
1109 int err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001110
Vivien Didelot6dc10bb2016-09-29 12:21:55 -04001111 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G1_ATU_FID)) {
Vivien Didelota935c052016-09-29 12:21:53 -04001112 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_FID, fid);
1113 if (err)
1114 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001115 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001116 /* ATU DBNum[7:4] are located in ATU Control 15:12 */
Vivien Didelota935c052016-09-29 12:21:53 -04001117 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_CONTROL, &val);
1118 if (err)
1119 return err;
Vivien Didelot11ea8092016-03-31 16:53:44 -04001120
Vivien Didelota935c052016-09-29 12:21:53 -04001121 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_CONTROL,
1122 (val & 0xfff) | ((fid << 8) & 0xf000));
1123 if (err)
1124 return err;
Vivien Didelot11ea8092016-03-31 16:53:44 -04001125
1126 /* ATU DBNum[3:0] are located in ATU Operation 3:0 */
1127 cmd |= fid & 0xf;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001128 }
1129
Vivien Didelota935c052016-09-29 12:21:53 -04001130 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_OP, cmd);
1131 if (err)
1132 return err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001133
Vivien Didelotfad09c72016-06-21 12:28:20 -04001134 return _mv88e6xxx_atu_wait(chip);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001135}
1136
Vivien Didelotfad09c72016-06-21 12:28:20 -04001137static int _mv88e6xxx_atu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot37705b72015-09-04 14:34:11 -04001138 struct mv88e6xxx_atu_entry *entry)
1139{
1140 u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK;
1141
1142 if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) {
1143 unsigned int mask, shift;
1144
1145 if (entry->trunk) {
1146 data |= GLOBAL_ATU_DATA_TRUNK;
1147 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
1148 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
1149 } else {
1150 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
1151 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
1152 }
1153
1154 data |= (entry->portv_trunkid << shift) & mask;
1155 }
1156
Vivien Didelota935c052016-09-29 12:21:53 -04001157 return mv88e6xxx_g1_write(chip, GLOBAL_ATU_DATA, data);
Vivien Didelot37705b72015-09-04 14:34:11 -04001158}
1159
Vivien Didelotfad09c72016-06-21 12:28:20 -04001160static int _mv88e6xxx_atu_flush_move(struct mv88e6xxx_chip *chip,
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001161 struct mv88e6xxx_atu_entry *entry,
1162 bool static_too)
1163{
1164 int op;
1165 int err;
1166
Vivien Didelotfad09c72016-06-21 12:28:20 -04001167 err = _mv88e6xxx_atu_wait(chip);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001168 if (err)
1169 return err;
1170
Vivien Didelotfad09c72016-06-21 12:28:20 -04001171 err = _mv88e6xxx_atu_data_write(chip, entry);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001172 if (err)
1173 return err;
1174
1175 if (entry->fid) {
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001176 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB :
1177 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB;
1178 } else {
1179 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL :
1180 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC;
1181 }
1182
Vivien Didelotfad09c72016-06-21 12:28:20 -04001183 return _mv88e6xxx_atu_cmd(chip, entry->fid, op);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001184}
1185
Vivien Didelotfad09c72016-06-21 12:28:20 -04001186static int _mv88e6xxx_atu_flush(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001187 u16 fid, bool static_too)
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001188{
1189 struct mv88e6xxx_atu_entry entry = {
1190 .fid = fid,
1191 .state = 0, /* EntryState bits must be 0 */
1192 };
1193
Vivien Didelotfad09c72016-06-21 12:28:20 -04001194 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001195}
1196
Vivien Didelotfad09c72016-06-21 12:28:20 -04001197static int _mv88e6xxx_atu_move(struct mv88e6xxx_chip *chip, u16 fid,
Andrew Lunn158bc062016-04-28 21:24:06 -04001198 int from_port, int to_port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001199{
1200 struct mv88e6xxx_atu_entry entry = {
1201 .trunk = false,
1202 .fid = fid,
1203 };
1204
1205 /* EntryState bits must be 0xF */
1206 entry.state = GLOBAL_ATU_DATA_STATE_MASK;
1207
1208 /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */
1209 entry.portv_trunkid = (to_port & 0x0f) << 4;
1210 entry.portv_trunkid |= from_port & 0x0f;
1211
Vivien Didelotfad09c72016-06-21 12:28:20 -04001212 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001213}
1214
Vivien Didelotfad09c72016-06-21 12:28:20 -04001215static int _mv88e6xxx_atu_remove(struct mv88e6xxx_chip *chip, u16 fid,
Andrew Lunn158bc062016-04-28 21:24:06 -04001216 int port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001217{
1218 /* Destination port 0xF means remove the entries */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001219 return _mv88e6xxx_atu_move(chip, fid, port, 0x0f, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001220}
1221
Vivien Didelotfad09c72016-06-21 12:28:20 -04001222static int _mv88e6xxx_port_based_vlan_map(struct mv88e6xxx_chip *chip, int port)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001223{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001224 struct net_device *bridge = chip->ports[port].bridge_dev;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001225 struct dsa_switch *ds = chip->ds;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001226 u16 output_ports = 0;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001227 int i;
1228
1229 /* allow CPU port or DSA link(s) to send frames to every port */
1230 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
Vivien Didelot5a7921f2016-11-04 03:23:28 +01001231 output_ports = ~0;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001232 } else {
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001233 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001234 /* allow sending frames to every group member */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001235 if (bridge && chip->ports[i].bridge_dev == bridge)
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001236 output_ports |= BIT(i);
1237
1238 /* allow sending frames to CPU port and DSA link(s) */
1239 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
1240 output_ports |= BIT(i);
1241 }
1242 }
1243
1244 /* prevent frames from going back out of the port they came in on */
1245 output_ports &= ~BIT(port);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001246
Vivien Didelot5a7921f2016-11-04 03:23:28 +01001247 return mv88e6xxx_port_set_vlan_map(chip, port, output_ports);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001248}
1249
Vivien Didelotf81ec902016-05-09 13:22:58 -04001250static void mv88e6xxx_port_stp_state_set(struct dsa_switch *ds, int port,
1251 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001252{
Vivien Didelot04bed142016-08-31 18:06:13 -04001253 struct mv88e6xxx_chip *chip = ds->priv;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001254 int stp_state;
Vivien Didelot553eb542016-05-13 20:38:23 -04001255 int err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001256
1257 switch (state) {
1258 case BR_STATE_DISABLED:
Andrew Lunncca8b132015-04-02 04:06:39 +02001259 stp_state = PORT_CONTROL_STATE_DISABLED;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001260 break;
1261 case BR_STATE_BLOCKING:
1262 case BR_STATE_LISTENING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001263 stp_state = PORT_CONTROL_STATE_BLOCKING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001264 break;
1265 case BR_STATE_LEARNING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001266 stp_state = PORT_CONTROL_STATE_LEARNING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001267 break;
1268 case BR_STATE_FORWARDING:
1269 default:
Andrew Lunncca8b132015-04-02 04:06:39 +02001270 stp_state = PORT_CONTROL_STATE_FORWARDING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001271 break;
1272 }
1273
Vivien Didelotfad09c72016-06-21 12:28:20 -04001274 mutex_lock(&chip->reg_lock);
Vivien Didelote28def332016-11-04 03:23:27 +01001275 err = mv88e6xxx_port_set_state(chip, port, stp_state);
Vivien Didelotfad09c72016-06-21 12:28:20 -04001276 mutex_unlock(&chip->reg_lock);
Vivien Didelot553eb542016-05-13 20:38:23 -04001277
1278 if (err)
Vivien Didelote28def332016-11-04 03:23:27 +01001279 netdev_err(ds->ports[port].netdev, "failed to update state\n");
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001280}
1281
Vivien Didelot749efcb2016-09-22 16:49:24 -04001282static void mv88e6xxx_port_fast_age(struct dsa_switch *ds, int port)
1283{
1284 struct mv88e6xxx_chip *chip = ds->priv;
1285 int err;
1286
1287 mutex_lock(&chip->reg_lock);
1288 err = _mv88e6xxx_atu_remove(chip, 0, port, false);
1289 mutex_unlock(&chip->reg_lock);
1290
1291 if (err)
1292 netdev_err(ds->ports[port].netdev, "failed to flush ATU\n");
1293}
1294
Vivien Didelotfad09c72016-06-21 12:28:20 -04001295static int _mv88e6xxx_vtu_wait(struct mv88e6xxx_chip *chip)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001296{
Vivien Didelota935c052016-09-29 12:21:53 -04001297 return mv88e6xxx_g1_wait(chip, GLOBAL_VTU_OP, GLOBAL_VTU_OP_BUSY);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001298}
1299
Vivien Didelotfad09c72016-06-21 12:28:20 -04001300static int _mv88e6xxx_vtu_cmd(struct mv88e6xxx_chip *chip, u16 op)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001301{
Vivien Didelota935c052016-09-29 12:21:53 -04001302 int err;
Vivien Didelot6b17e862015-08-13 12:52:18 -04001303
Vivien Didelota935c052016-09-29 12:21:53 -04001304 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_OP, op);
1305 if (err)
1306 return err;
Vivien Didelot6b17e862015-08-13 12:52:18 -04001307
Vivien Didelotfad09c72016-06-21 12:28:20 -04001308 return _mv88e6xxx_vtu_wait(chip);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001309}
1310
Vivien Didelotfad09c72016-06-21 12:28:20 -04001311static int _mv88e6xxx_vtu_stu_flush(struct mv88e6xxx_chip *chip)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001312{
1313 int ret;
1314
Vivien Didelotfad09c72016-06-21 12:28:20 -04001315 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001316 if (ret < 0)
1317 return ret;
1318
Vivien Didelotfad09c72016-06-21 12:28:20 -04001319 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_FLUSH_ALL);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001320}
1321
Vivien Didelotfad09c72016-06-21 12:28:20 -04001322static int _mv88e6xxx_vtu_stu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001323 struct mv88e6xxx_vtu_entry *entry,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001324 unsigned int nibble_offset)
1325{
Vivien Didelotb8fee952015-08-13 12:52:19 -04001326 u16 regs[3];
Vivien Didelota935c052016-09-29 12:21:53 -04001327 int i, err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001328
1329 for (i = 0; i < 3; ++i) {
Vivien Didelota935c052016-09-29 12:21:53 -04001330 u16 *reg = &regs[i];
Vivien Didelotb8fee952015-08-13 12:52:19 -04001331
Vivien Didelota935c052016-09-29 12:21:53 -04001332 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_DATA_0_3 + i, reg);
1333 if (err)
1334 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001335 }
1336
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001337 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotb8fee952015-08-13 12:52:19 -04001338 unsigned int shift = (i % 4) * 4 + nibble_offset;
1339 u16 reg = regs[i / 4];
1340
1341 entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
1342 }
1343
1344 return 0;
1345}
1346
Vivien Didelotfad09c72016-06-21 12:28:20 -04001347static int mv88e6xxx_vtu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001348 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001349{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001350 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 0);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001351}
1352
Vivien Didelotfad09c72016-06-21 12:28:20 -04001353static int mv88e6xxx_stu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001354 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001355{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001356 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 2);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001357}
1358
Vivien Didelotfad09c72016-06-21 12:28:20 -04001359static int _mv88e6xxx_vtu_stu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001360 struct mv88e6xxx_vtu_entry *entry,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001361 unsigned int nibble_offset)
1362{
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001363 u16 regs[3] = { 0 };
Vivien Didelota935c052016-09-29 12:21:53 -04001364 int i, err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001365
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001366 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001367 unsigned int shift = (i % 4) * 4 + nibble_offset;
1368 u8 data = entry->data[i];
1369
1370 regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
1371 }
1372
1373 for (i = 0; i < 3; ++i) {
Vivien Didelota935c052016-09-29 12:21:53 -04001374 u16 reg = regs[i];
1375
1376 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_DATA_0_3 + i, reg);
1377 if (err)
1378 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001379 }
1380
1381 return 0;
1382}
1383
Vivien Didelotfad09c72016-06-21 12:28:20 -04001384static int mv88e6xxx_vtu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001385 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001386{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001387 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 0);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001388}
1389
Vivien Didelotfad09c72016-06-21 12:28:20 -04001390static int mv88e6xxx_stu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001391 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001392{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001393 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 2);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001394}
1395
Vivien Didelotfad09c72016-06-21 12:28:20 -04001396static int _mv88e6xxx_vtu_vid_write(struct mv88e6xxx_chip *chip, u16 vid)
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001397{
Vivien Didelota935c052016-09-29 12:21:53 -04001398 return mv88e6xxx_g1_write(chip, GLOBAL_VTU_VID,
1399 vid & GLOBAL_VTU_VID_MASK);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001400}
1401
Vivien Didelotfad09c72016-06-21 12:28:20 -04001402static int _mv88e6xxx_vtu_getnext(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001403 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelotb8fee952015-08-13 12:52:19 -04001404{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001405 struct mv88e6xxx_vtu_entry next = { 0 };
Vivien Didelota935c052016-09-29 12:21:53 -04001406 u16 val;
1407 int err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001408
Vivien Didelota935c052016-09-29 12:21:53 -04001409 err = _mv88e6xxx_vtu_wait(chip);
1410 if (err)
1411 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001412
Vivien Didelota935c052016-09-29 12:21:53 -04001413 err = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_VTU_GET_NEXT);
1414 if (err)
1415 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001416
Vivien Didelota935c052016-09-29 12:21:53 -04001417 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_VID, &val);
1418 if (err)
1419 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001420
Vivien Didelota935c052016-09-29 12:21:53 -04001421 next.vid = val & GLOBAL_VTU_VID_MASK;
1422 next.valid = !!(val & GLOBAL_VTU_VID_VALID);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001423
1424 if (next.valid) {
Vivien Didelota935c052016-09-29 12:21:53 -04001425 err = mv88e6xxx_vtu_data_read(chip, &next);
1426 if (err)
1427 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001428
Vivien Didelot6dc10bb2016-09-29 12:21:55 -04001429 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G1_VTU_FID)) {
Vivien Didelota935c052016-09-29 12:21:53 -04001430 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_FID, &val);
1431 if (err)
1432 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001433
Vivien Didelota935c052016-09-29 12:21:53 -04001434 next.fid = val & GLOBAL_VTU_FID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001435 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001436 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1437 * VTU DBNum[3:0] are located in VTU Operation 3:0
1438 */
Vivien Didelota935c052016-09-29 12:21:53 -04001439 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_OP, &val);
1440 if (err)
1441 return err;
Vivien Didelot11ea8092016-03-31 16:53:44 -04001442
Vivien Didelota935c052016-09-29 12:21:53 -04001443 next.fid = (val & 0xf00) >> 4;
1444 next.fid |= val & 0xf;
Vivien Didelot2e7bd5e2016-03-31 16:53:41 -04001445 }
Vivien Didelotb8fee952015-08-13 12:52:19 -04001446
Vivien Didelotfad09c72016-06-21 12:28:20 -04001447 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
Vivien Didelota935c052016-09-29 12:21:53 -04001448 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_SID, &val);
1449 if (err)
1450 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001451
Vivien Didelota935c052016-09-29 12:21:53 -04001452 next.sid = val & GLOBAL_VTU_SID_MASK;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001453 }
1454 }
1455
1456 *entry = next;
1457 return 0;
1458}
1459
Vivien Didelotf81ec902016-05-09 13:22:58 -04001460static int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
1461 struct switchdev_obj_port_vlan *vlan,
1462 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001463{
Vivien Didelot04bed142016-08-31 18:06:13 -04001464 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001465 struct mv88e6xxx_vtu_entry next;
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001466 u16 pvid;
1467 int err;
1468
Vivien Didelotfad09c72016-06-21 12:28:20 -04001469 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001470 return -EOPNOTSUPP;
1471
Vivien Didelotfad09c72016-06-21 12:28:20 -04001472 mutex_lock(&chip->reg_lock);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001473
Vivien Didelot77064f32016-11-04 03:23:30 +01001474 err = mv88e6xxx_port_get_pvid(chip, port, &pvid);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001475 if (err)
1476 goto unlock;
1477
Vivien Didelotfad09c72016-06-21 12:28:20 -04001478 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001479 if (err)
1480 goto unlock;
1481
1482 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001483 err = _mv88e6xxx_vtu_getnext(chip, &next);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001484 if (err)
1485 break;
1486
1487 if (!next.valid)
1488 break;
1489
1490 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1491 continue;
1492
1493 /* reinit and dump this VLAN obj */
Vivien Didelot57d32312016-06-20 13:13:58 -04001494 vlan->vid_begin = next.vid;
1495 vlan->vid_end = next.vid;
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001496 vlan->flags = 0;
1497
1498 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1499 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1500
1501 if (next.vid == pvid)
1502 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1503
1504 err = cb(&vlan->obj);
1505 if (err)
1506 break;
1507 } while (next.vid < GLOBAL_VTU_VID_MASK);
1508
1509unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001510 mutex_unlock(&chip->reg_lock);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001511
1512 return err;
1513}
1514
Vivien Didelotfad09c72016-06-21 12:28:20 -04001515static int _mv88e6xxx_vtu_loadpurge(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001516 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001517{
Vivien Didelot11ea8092016-03-31 16:53:44 -04001518 u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001519 u16 reg = 0;
Vivien Didelota935c052016-09-29 12:21:53 -04001520 int err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001521
Vivien Didelota935c052016-09-29 12:21:53 -04001522 err = _mv88e6xxx_vtu_wait(chip);
1523 if (err)
1524 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001525
1526 if (!entry->valid)
1527 goto loadpurge;
1528
1529 /* Write port member tags */
Vivien Didelota935c052016-09-29 12:21:53 -04001530 err = mv88e6xxx_vtu_data_write(chip, entry);
1531 if (err)
1532 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001533
Vivien Didelotfad09c72016-06-21 12:28:20 -04001534 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001535 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Vivien Didelota935c052016-09-29 12:21:53 -04001536 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_SID, reg);
1537 if (err)
1538 return err;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001539 }
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001540
Vivien Didelot6dc10bb2016-09-29 12:21:55 -04001541 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G1_VTU_FID)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001542 reg = entry->fid & GLOBAL_VTU_FID_MASK;
Vivien Didelota935c052016-09-29 12:21:53 -04001543 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_FID, reg);
1544 if (err)
1545 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001546 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001547 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1548 * VTU DBNum[3:0] are located in VTU Operation 3:0
1549 */
1550 op |= (entry->fid & 0xf0) << 8;
1551 op |= entry->fid & 0xf;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001552 }
1553
1554 reg = GLOBAL_VTU_VID_VALID;
1555loadpurge:
1556 reg |= entry->vid & GLOBAL_VTU_VID_MASK;
Vivien Didelota935c052016-09-29 12:21:53 -04001557 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_VID, reg);
1558 if (err)
1559 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001560
Vivien Didelotfad09c72016-06-21 12:28:20 -04001561 return _mv88e6xxx_vtu_cmd(chip, op);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001562}
1563
Vivien Didelotfad09c72016-06-21 12:28:20 -04001564static int _mv88e6xxx_stu_getnext(struct mv88e6xxx_chip *chip, u8 sid,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001565 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001566{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001567 struct mv88e6xxx_vtu_entry next = { 0 };
Vivien Didelota935c052016-09-29 12:21:53 -04001568 u16 val;
1569 int err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001570
Vivien Didelota935c052016-09-29 12:21:53 -04001571 err = _mv88e6xxx_vtu_wait(chip);
1572 if (err)
1573 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001574
Vivien Didelota935c052016-09-29 12:21:53 -04001575 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_SID,
1576 sid & GLOBAL_VTU_SID_MASK);
1577 if (err)
1578 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001579
Vivien Didelota935c052016-09-29 12:21:53 -04001580 err = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_GET_NEXT);
1581 if (err)
1582 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001583
Vivien Didelota935c052016-09-29 12:21:53 -04001584 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_SID, &val);
1585 if (err)
1586 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001587
Vivien Didelota935c052016-09-29 12:21:53 -04001588 next.sid = val & GLOBAL_VTU_SID_MASK;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001589
Vivien Didelota935c052016-09-29 12:21:53 -04001590 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_VID, &val);
1591 if (err)
1592 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001593
Vivien Didelota935c052016-09-29 12:21:53 -04001594 next.valid = !!(val & GLOBAL_VTU_VID_VALID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001595
1596 if (next.valid) {
Vivien Didelota935c052016-09-29 12:21:53 -04001597 err = mv88e6xxx_stu_data_read(chip, &next);
1598 if (err)
1599 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001600 }
1601
1602 *entry = next;
1603 return 0;
1604}
1605
Vivien Didelotfad09c72016-06-21 12:28:20 -04001606static int _mv88e6xxx_stu_loadpurge(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001607 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001608{
1609 u16 reg = 0;
Vivien Didelota935c052016-09-29 12:21:53 -04001610 int err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001611
Vivien Didelota935c052016-09-29 12:21:53 -04001612 err = _mv88e6xxx_vtu_wait(chip);
1613 if (err)
1614 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001615
1616 if (!entry->valid)
1617 goto loadpurge;
1618
1619 /* Write port states */
Vivien Didelota935c052016-09-29 12:21:53 -04001620 err = mv88e6xxx_stu_data_write(chip, entry);
1621 if (err)
1622 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001623
1624 reg = GLOBAL_VTU_VID_VALID;
1625loadpurge:
Vivien Didelota935c052016-09-29 12:21:53 -04001626 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_VID, reg);
1627 if (err)
1628 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001629
1630 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Vivien Didelota935c052016-09-29 12:21:53 -04001631 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_SID, reg);
1632 if (err)
1633 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001634
Vivien Didelotfad09c72016-06-21 12:28:20 -04001635 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_LOAD_PURGE);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001636}
1637
Vivien Didelotfad09c72016-06-21 12:28:20 -04001638static int _mv88e6xxx_fid_new(struct mv88e6xxx_chip *chip, u16 *fid)
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001639{
1640 DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001641 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001642 int i, err;
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001643
1644 bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);
1645
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001646 /* Set every FID bit used by the (un)bridged ports */
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001647 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotb4e48c52016-11-04 03:23:29 +01001648 err = mv88e6xxx_port_get_fid(chip, i, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001649 if (err)
1650 return err;
1651
1652 set_bit(*fid, fid_bitmap);
1653 }
1654
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001655 /* Set every FID bit used by the VLAN entries */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001656 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001657 if (err)
1658 return err;
1659
1660 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001661 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001662 if (err)
1663 return err;
1664
1665 if (!vlan.valid)
1666 break;
1667
1668 set_bit(vlan.fid, fid_bitmap);
1669 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
1670
1671 /* The reset value 0x000 is used to indicate that multiple address
1672 * databases are not needed. Return the next positive available.
1673 */
1674 *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1);
Vivien Didelotfad09c72016-06-21 12:28:20 -04001675 if (unlikely(*fid >= mv88e6xxx_num_databases(chip)))
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001676 return -ENOSPC;
1677
1678 /* Clear the database */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001679 return _mv88e6xxx_atu_flush(chip, *fid, true);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001680}
1681
Vivien Didelotfad09c72016-06-21 12:28:20 -04001682static int _mv88e6xxx_vtu_new(struct mv88e6xxx_chip *chip, u16 vid,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001683 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001684{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001685 struct dsa_switch *ds = chip->ds;
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001686 struct mv88e6xxx_vtu_entry vlan = {
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001687 .valid = true,
1688 .vid = vid,
1689 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001690 int i, err;
1691
Vivien Didelotfad09c72016-06-21 12:28:20 -04001692 err = _mv88e6xxx_fid_new(chip, &vlan.fid);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001693 if (err)
1694 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001695
Vivien Didelot3d131f02015-11-03 10:52:52 -05001696 /* exclude all ports except the CPU and DSA ports */
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001697 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i)
Vivien Didelot3d131f02015-11-03 10:52:52 -05001698 vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)
1699 ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED
1700 : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001701
Vivien Didelotfad09c72016-06-21 12:28:20 -04001702 if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
1703 mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip)) {
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001704 struct mv88e6xxx_vtu_entry vstp;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001705
1706 /* Adding a VTU entry requires a valid STU entry. As VSTP is not
1707 * implemented, only one STU entry is needed to cover all VTU
1708 * entries. Thus, validate the SID 0.
1709 */
1710 vlan.sid = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001711 err = _mv88e6xxx_stu_getnext(chip, GLOBAL_VTU_SID_MASK, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001712 if (err)
1713 return err;
1714
1715 if (vstp.sid != vlan.sid || !vstp.valid) {
1716 memset(&vstp, 0, sizeof(vstp));
1717 vstp.valid = true;
1718 vstp.sid = vlan.sid;
1719
Vivien Didelotfad09c72016-06-21 12:28:20 -04001720 err = _mv88e6xxx_stu_loadpurge(chip, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001721 if (err)
1722 return err;
1723 }
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001724 }
1725
1726 *entry = vlan;
1727 return 0;
1728}
1729
Vivien Didelotfad09c72016-06-21 12:28:20 -04001730static int _mv88e6xxx_vtu_get(struct mv88e6xxx_chip *chip, u16 vid,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001731 struct mv88e6xxx_vtu_entry *entry, bool creat)
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001732{
1733 int err;
1734
1735 if (!vid)
1736 return -EINVAL;
1737
Vivien Didelotfad09c72016-06-21 12:28:20 -04001738 err = _mv88e6xxx_vtu_vid_write(chip, vid - 1);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001739 if (err)
1740 return err;
1741
Vivien Didelotfad09c72016-06-21 12:28:20 -04001742 err = _mv88e6xxx_vtu_getnext(chip, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001743 if (err)
1744 return err;
1745
1746 if (entry->vid != vid || !entry->valid) {
1747 if (!creat)
1748 return -EOPNOTSUPP;
1749 /* -ENOENT would've been more appropriate, but switchdev expects
1750 * -EOPNOTSUPP to inform bridge about an eventual software VLAN.
1751 */
1752
Vivien Didelotfad09c72016-06-21 12:28:20 -04001753 err = _mv88e6xxx_vtu_new(chip, vid, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001754 }
1755
1756 return err;
1757}
1758
Vivien Didelotda9c3592016-02-12 12:09:40 -05001759static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
1760 u16 vid_begin, u16 vid_end)
1761{
Vivien Didelot04bed142016-08-31 18:06:13 -04001762 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001763 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelotda9c3592016-02-12 12:09:40 -05001764 int i, err;
1765
1766 if (!vid_begin)
1767 return -EOPNOTSUPP;
1768
Vivien Didelotfad09c72016-06-21 12:28:20 -04001769 mutex_lock(&chip->reg_lock);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001770
Vivien Didelotfad09c72016-06-21 12:28:20 -04001771 err = _mv88e6xxx_vtu_vid_write(chip, vid_begin - 1);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001772 if (err)
1773 goto unlock;
1774
1775 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001776 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001777 if (err)
1778 goto unlock;
1779
1780 if (!vlan.valid)
1781 break;
1782
1783 if (vlan.vid > vid_end)
1784 break;
1785
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001786 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotda9c3592016-02-12 12:09:40 -05001787 if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i))
1788 continue;
1789
1790 if (vlan.data[i] ==
1791 GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1792 continue;
1793
Vivien Didelotfad09c72016-06-21 12:28:20 -04001794 if (chip->ports[i].bridge_dev ==
1795 chip->ports[port].bridge_dev)
Vivien Didelotda9c3592016-02-12 12:09:40 -05001796 break; /* same bridge, check next VLAN */
1797
Andrew Lunnc8b09802016-06-04 21:16:57 +02001798 netdev_warn(ds->ports[port].netdev,
Vivien Didelotda9c3592016-02-12 12:09:40 -05001799 "hardware VLAN %d already used by %s\n",
1800 vlan.vid,
Vivien Didelotfad09c72016-06-21 12:28:20 -04001801 netdev_name(chip->ports[i].bridge_dev));
Vivien Didelotda9c3592016-02-12 12:09:40 -05001802 err = -EOPNOTSUPP;
1803 goto unlock;
1804 }
1805 } while (vlan.vid < vid_end);
1806
1807unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001808 mutex_unlock(&chip->reg_lock);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001809
1810 return err;
1811}
1812
Vivien Didelotf81ec902016-05-09 13:22:58 -04001813static int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port,
1814 bool vlan_filtering)
Vivien Didelot214cdb92016-02-26 13:16:08 -05001815{
Vivien Didelot04bed142016-08-31 18:06:13 -04001816 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot385a0992016-11-04 03:23:31 +01001817 u16 mode = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE :
Vivien Didelot214cdb92016-02-26 13:16:08 -05001818 PORT_CONTROL_2_8021Q_DISABLED;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001819 int err;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001820
Vivien Didelotfad09c72016-06-21 12:28:20 -04001821 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001822 return -EOPNOTSUPP;
1823
Vivien Didelotfad09c72016-06-21 12:28:20 -04001824 mutex_lock(&chip->reg_lock);
Vivien Didelot385a0992016-11-04 03:23:31 +01001825 err = mv88e6xxx_port_set_8021q_mode(chip, port, mode);
Vivien Didelotfad09c72016-06-21 12:28:20 -04001826 mutex_unlock(&chip->reg_lock);
Vivien Didelot214cdb92016-02-26 13:16:08 -05001827
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001828 return err;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001829}
1830
Vivien Didelot57d32312016-06-20 13:13:58 -04001831static int
1832mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
1833 const struct switchdev_obj_port_vlan *vlan,
1834 struct switchdev_trans *trans)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001835{
Vivien Didelot04bed142016-08-31 18:06:13 -04001836 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotda9c3592016-02-12 12:09:40 -05001837 int err;
1838
Vivien Didelotfad09c72016-06-21 12:28:20 -04001839 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001840 return -EOPNOTSUPP;
1841
Vivien Didelotda9c3592016-02-12 12:09:40 -05001842 /* If the requested port doesn't belong to the same bridge as the VLAN
1843 * members, do not support it (yet) and fallback to software VLAN.
1844 */
1845 err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin,
1846 vlan->vid_end);
1847 if (err)
1848 return err;
1849
Vivien Didelot76e398a2015-11-01 12:33:55 -05001850 /* We don't need any dynamic resource from the kernel (yet),
1851 * so skip the prepare phase.
1852 */
1853 return 0;
1854}
1855
Vivien Didelotfad09c72016-06-21 12:28:20 -04001856static int _mv88e6xxx_port_vlan_add(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001857 u16 vid, bool untagged)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001858{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001859 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001860 int err;
1861
Vivien Didelotfad09c72016-06-21 12:28:20 -04001862 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, true);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001863 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001864 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001865
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001866 vlan.data[port] = untagged ?
1867 GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
1868 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
1869
Vivien Didelotfad09c72016-06-21 12:28:20 -04001870 return _mv88e6xxx_vtu_loadpurge(chip, &vlan);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001871}
1872
Vivien Didelotf81ec902016-05-09 13:22:58 -04001873static void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
1874 const struct switchdev_obj_port_vlan *vlan,
1875 struct switchdev_trans *trans)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001876{
Vivien Didelot04bed142016-08-31 18:06:13 -04001877 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001878 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1879 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1880 u16 vid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001881
Vivien Didelotfad09c72016-06-21 12:28:20 -04001882 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001883 return;
1884
Vivien Didelotfad09c72016-06-21 12:28:20 -04001885 mutex_lock(&chip->reg_lock);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001886
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001887 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
Vivien Didelotfad09c72016-06-21 12:28:20 -04001888 if (_mv88e6xxx_port_vlan_add(chip, port, vid, untagged))
Andrew Lunnc8b09802016-06-04 21:16:57 +02001889 netdev_err(ds->ports[port].netdev,
1890 "failed to add VLAN %d%c\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001891 vid, untagged ? 'u' : 't');
Vivien Didelot76e398a2015-11-01 12:33:55 -05001892
Vivien Didelot77064f32016-11-04 03:23:30 +01001893 if (pvid && mv88e6xxx_port_set_pvid(chip, port, vlan->vid_end))
Andrew Lunnc8b09802016-06-04 21:16:57 +02001894 netdev_err(ds->ports[port].netdev, "failed to set PVID %d\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001895 vlan->vid_end);
1896
Vivien Didelotfad09c72016-06-21 12:28:20 -04001897 mutex_unlock(&chip->reg_lock);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001898}
1899
Vivien Didelotfad09c72016-06-21 12:28:20 -04001900static int _mv88e6xxx_port_vlan_del(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001901 int port, u16 vid)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001902{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001903 struct dsa_switch *ds = chip->ds;
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001904 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001905 int i, err;
1906
Vivien Didelotfad09c72016-06-21 12:28:20 -04001907 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001908 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001909 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001910
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001911 /* Tell switchdev if this VLAN is handled in software */
1912 if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
Vivien Didelot3c06f082016-02-05 14:04:39 -05001913 return -EOPNOTSUPP;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001914
1915 vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
1916
1917 /* keep the VLAN unless all ports are excluded */
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001918 vlan.valid = false;
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001919 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelot3d131f02015-11-03 10:52:52 -05001920 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001921 continue;
1922
1923 if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001924 vlan.valid = true;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001925 break;
1926 }
1927 }
1928
Vivien Didelotfad09c72016-06-21 12:28:20 -04001929 err = _mv88e6xxx_vtu_loadpurge(chip, &vlan);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001930 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001931 return err;
1932
Vivien Didelotfad09c72016-06-21 12:28:20 -04001933 return _mv88e6xxx_atu_remove(chip, vlan.fid, port, false);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001934}
1935
Vivien Didelotf81ec902016-05-09 13:22:58 -04001936static int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
1937 const struct switchdev_obj_port_vlan *vlan)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001938{
Vivien Didelot04bed142016-08-31 18:06:13 -04001939 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001940 u16 pvid, vid;
1941 int err = 0;
1942
Vivien Didelotfad09c72016-06-21 12:28:20 -04001943 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001944 return -EOPNOTSUPP;
1945
Vivien Didelotfad09c72016-06-21 12:28:20 -04001946 mutex_lock(&chip->reg_lock);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001947
Vivien Didelot77064f32016-11-04 03:23:30 +01001948 err = mv88e6xxx_port_get_pvid(chip, port, &pvid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001949 if (err)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001950 goto unlock;
1951
Vivien Didelot76e398a2015-11-01 12:33:55 -05001952 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001953 err = _mv88e6xxx_port_vlan_del(chip, port, vid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001954 if (err)
1955 goto unlock;
1956
1957 if (vid == pvid) {
Vivien Didelot77064f32016-11-04 03:23:30 +01001958 err = mv88e6xxx_port_set_pvid(chip, port, 0);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001959 if (err)
1960 goto unlock;
1961 }
1962 }
1963
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001964unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001965 mutex_unlock(&chip->reg_lock);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001966
1967 return err;
1968}
1969
Vivien Didelotfad09c72016-06-21 12:28:20 -04001970static int _mv88e6xxx_atu_mac_write(struct mv88e6xxx_chip *chip,
Vivien Didelotc5723ac2015-08-10 09:09:48 -04001971 const unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001972{
Vivien Didelota935c052016-09-29 12:21:53 -04001973 int i, err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001974
1975 for (i = 0; i < 3; i++) {
Vivien Didelota935c052016-09-29 12:21:53 -04001976 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_MAC_01 + i,
1977 (addr[i * 2] << 8) | addr[i * 2 + 1]);
1978 if (err)
1979 return err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001980 }
1981
1982 return 0;
1983}
1984
Vivien Didelotfad09c72016-06-21 12:28:20 -04001985static int _mv88e6xxx_atu_mac_read(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001986 unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001987{
Vivien Didelota935c052016-09-29 12:21:53 -04001988 u16 val;
1989 int i, err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001990
1991 for (i = 0; i < 3; i++) {
Vivien Didelota935c052016-09-29 12:21:53 -04001992 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_MAC_01 + i, &val);
1993 if (err)
1994 return err;
1995
1996 addr[i * 2] = val >> 8;
1997 addr[i * 2 + 1] = val & 0xff;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001998 }
1999
2000 return 0;
2001}
2002
Vivien Didelotfad09c72016-06-21 12:28:20 -04002003static int _mv88e6xxx_atu_load(struct mv88e6xxx_chip *chip,
Vivien Didelotfd231c82015-08-10 09:09:50 -04002004 struct mv88e6xxx_atu_entry *entry)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002005{
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002006 int ret;
2007
Vivien Didelotfad09c72016-06-21 12:28:20 -04002008 ret = _mv88e6xxx_atu_wait(chip);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002009 if (ret < 0)
2010 return ret;
2011
Vivien Didelotfad09c72016-06-21 12:28:20 -04002012 ret = _mv88e6xxx_atu_mac_write(chip, entry->mac);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002013 if (ret < 0)
2014 return ret;
2015
Vivien Didelotfad09c72016-06-21 12:28:20 -04002016 ret = _mv88e6xxx_atu_data_write(chip, entry);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002017 if (ret < 0)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002018 return ret;
2019
Vivien Didelotfad09c72016-06-21 12:28:20 -04002020 return _mv88e6xxx_atu_cmd(chip, entry->fid, GLOBAL_ATU_OP_LOAD_DB);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002021}
David S. Millercdf09692015-08-11 12:00:37 -07002022
Vivien Didelot88472932016-09-19 19:56:11 -04002023static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
2024 struct mv88e6xxx_atu_entry *entry);
2025
2026static int mv88e6xxx_atu_get(struct mv88e6xxx_chip *chip, int fid,
2027 const u8 *addr, struct mv88e6xxx_atu_entry *entry)
2028{
2029 struct mv88e6xxx_atu_entry next;
2030 int err;
2031
2032 eth_broadcast_addr(next.mac);
2033
2034 err = _mv88e6xxx_atu_mac_write(chip, next.mac);
2035 if (err)
2036 return err;
2037
2038 do {
2039 err = _mv88e6xxx_atu_getnext(chip, fid, &next);
2040 if (err)
2041 return err;
2042
2043 if (next.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2044 break;
2045
2046 if (ether_addr_equal(next.mac, addr)) {
2047 *entry = next;
2048 return 0;
2049 }
2050 } while (!is_broadcast_ether_addr(next.mac));
2051
2052 memset(entry, 0, sizeof(*entry));
2053 entry->fid = fid;
2054 ether_addr_copy(entry->mac, addr);
2055
2056 return 0;
2057}
2058
Vivien Didelot83dabd12016-08-31 11:50:04 -04002059static int mv88e6xxx_port_db_load_purge(struct mv88e6xxx_chip *chip, int port,
2060 const unsigned char *addr, u16 vid,
2061 u8 state)
Vivien Didelotfd231c82015-08-10 09:09:50 -04002062{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04002063 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelot88472932016-09-19 19:56:11 -04002064 struct mv88e6xxx_atu_entry entry;
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002065 int err;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002066
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002067 /* Null VLAN ID corresponds to the port private database */
2068 if (vid == 0)
Vivien Didelotb4e48c52016-11-04 03:23:29 +01002069 err = mv88e6xxx_port_get_fid(chip, port, &vlan.fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002070 else
Vivien Didelotfad09c72016-06-21 12:28:20 -04002071 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002072 if (err)
2073 return err;
2074
Vivien Didelot88472932016-09-19 19:56:11 -04002075 err = mv88e6xxx_atu_get(chip, vlan.fid, addr, &entry);
2076 if (err)
2077 return err;
2078
2079 /* Purge the ATU entry only if no port is using it anymore */
2080 if (state == GLOBAL_ATU_DATA_STATE_UNUSED) {
2081 entry.portv_trunkid &= ~BIT(port);
2082 if (!entry.portv_trunkid)
2083 entry.state = GLOBAL_ATU_DATA_STATE_UNUSED;
2084 } else {
2085 entry.portv_trunkid |= BIT(port);
2086 entry.state = state;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002087 }
2088
Vivien Didelotfad09c72016-06-21 12:28:20 -04002089 return _mv88e6xxx_atu_load(chip, &entry);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002090}
2091
Vivien Didelotf81ec902016-05-09 13:22:58 -04002092static int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
2093 const struct switchdev_obj_port_fdb *fdb,
2094 struct switchdev_trans *trans)
Vivien Didelot146a3202015-10-08 11:35:12 -04002095{
2096 /* We don't need any dynamic resource from the kernel (yet),
2097 * so skip the prepare phase.
2098 */
2099 return 0;
2100}
2101
Vivien Didelotf81ec902016-05-09 13:22:58 -04002102static void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
2103 const struct switchdev_obj_port_fdb *fdb,
2104 struct switchdev_trans *trans)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002105{
Vivien Didelot04bed142016-08-31 18:06:13 -04002106 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot6630e232015-08-06 01:44:07 -04002107
Vivien Didelotfad09c72016-06-21 12:28:20 -04002108 mutex_lock(&chip->reg_lock);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002109 if (mv88e6xxx_port_db_load_purge(chip, port, fdb->addr, fdb->vid,
2110 GLOBAL_ATU_DATA_STATE_UC_STATIC))
2111 netdev_err(ds->ports[port].netdev, "failed to load unicast MAC address\n");
Vivien Didelotfad09c72016-06-21 12:28:20 -04002112 mutex_unlock(&chip->reg_lock);
David S. Millercdf09692015-08-11 12:00:37 -07002113}
2114
Vivien Didelotf81ec902016-05-09 13:22:58 -04002115static int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
2116 const struct switchdev_obj_port_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -07002117{
Vivien Didelot04bed142016-08-31 18:06:13 -04002118 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot83dabd12016-08-31 11:50:04 -04002119 int err;
David S. Millercdf09692015-08-11 12:00:37 -07002120
Vivien Didelotfad09c72016-06-21 12:28:20 -04002121 mutex_lock(&chip->reg_lock);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002122 err = mv88e6xxx_port_db_load_purge(chip, port, fdb->addr, fdb->vid,
2123 GLOBAL_ATU_DATA_STATE_UNUSED);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002124 mutex_unlock(&chip->reg_lock);
David S. Millercdf09692015-08-11 12:00:37 -07002125
Vivien Didelot83dabd12016-08-31 11:50:04 -04002126 return err;
David S. Millercdf09692015-08-11 12:00:37 -07002127}
2128
Vivien Didelotfad09c72016-06-21 12:28:20 -04002129static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
Vivien Didelot1d194042015-08-10 09:09:51 -04002130 struct mv88e6xxx_atu_entry *entry)
David S. Millercdf09692015-08-11 12:00:37 -07002131{
Vivien Didelot1d194042015-08-10 09:09:51 -04002132 struct mv88e6xxx_atu_entry next = { 0 };
Vivien Didelota935c052016-09-29 12:21:53 -04002133 u16 val;
2134 int err;
Vivien Didelot1d194042015-08-10 09:09:51 -04002135
2136 next.fid = fid;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002137
Vivien Didelota935c052016-09-29 12:21:53 -04002138 err = _mv88e6xxx_atu_wait(chip);
2139 if (err)
2140 return err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002141
Vivien Didelota935c052016-09-29 12:21:53 -04002142 err = _mv88e6xxx_atu_cmd(chip, fid, GLOBAL_ATU_OP_GET_NEXT_DB);
2143 if (err)
2144 return err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002145
Vivien Didelota935c052016-09-29 12:21:53 -04002146 err = _mv88e6xxx_atu_mac_read(chip, next.mac);
2147 if (err)
2148 return err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002149
Vivien Didelota935c052016-09-29 12:21:53 -04002150 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_DATA, &val);
2151 if (err)
2152 return err;
Vivien Didelot1d194042015-08-10 09:09:51 -04002153
Vivien Didelota935c052016-09-29 12:21:53 -04002154 next.state = val & GLOBAL_ATU_DATA_STATE_MASK;
Vivien Didelot1d194042015-08-10 09:09:51 -04002155 if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2156 unsigned int mask, shift;
2157
Vivien Didelota935c052016-09-29 12:21:53 -04002158 if (val & GLOBAL_ATU_DATA_TRUNK) {
Vivien Didelot1d194042015-08-10 09:09:51 -04002159 next.trunk = true;
2160 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
2161 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
2162 } else {
2163 next.trunk = false;
2164 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
2165 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
2166 }
2167
Vivien Didelota935c052016-09-29 12:21:53 -04002168 next.portv_trunkid = (val & mask) >> shift;
Vivien Didelot1d194042015-08-10 09:09:51 -04002169 }
2170
2171 *entry = next;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002172 return 0;
2173}
2174
Vivien Didelot83dabd12016-08-31 11:50:04 -04002175static int mv88e6xxx_port_db_dump_fid(struct mv88e6xxx_chip *chip,
2176 u16 fid, u16 vid, int port,
2177 struct switchdev_obj *obj,
2178 int (*cb)(struct switchdev_obj *obj))
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002179{
2180 struct mv88e6xxx_atu_entry addr = {
2181 .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2182 };
2183 int err;
2184
Vivien Didelotfad09c72016-06-21 12:28:20 -04002185 err = _mv88e6xxx_atu_mac_write(chip, addr.mac);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002186 if (err)
2187 return err;
2188
2189 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002190 err = _mv88e6xxx_atu_getnext(chip, fid, &addr);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002191 if (err)
Vivien Didelot83dabd12016-08-31 11:50:04 -04002192 return err;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002193
2194 if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2195 break;
2196
Vivien Didelot83dabd12016-08-31 11:50:04 -04002197 if (addr.trunk || (addr.portv_trunkid & BIT(port)) == 0)
2198 continue;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002199
Vivien Didelot83dabd12016-08-31 11:50:04 -04002200 if (obj->id == SWITCHDEV_OBJ_ID_PORT_FDB) {
2201 struct switchdev_obj_port_fdb *fdb;
2202
2203 if (!is_unicast_ether_addr(addr.mac))
2204 continue;
2205
2206 fdb = SWITCHDEV_OBJ_PORT_FDB(obj);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002207 fdb->vid = vid;
2208 ether_addr_copy(fdb->addr, addr.mac);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002209 if (addr.state == GLOBAL_ATU_DATA_STATE_UC_STATIC)
2210 fdb->ndm_state = NUD_NOARP;
2211 else
2212 fdb->ndm_state = NUD_REACHABLE;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04002213 } else if (obj->id == SWITCHDEV_OBJ_ID_PORT_MDB) {
2214 struct switchdev_obj_port_mdb *mdb;
2215
2216 if (!is_multicast_ether_addr(addr.mac))
2217 continue;
2218
2219 mdb = SWITCHDEV_OBJ_PORT_MDB(obj);
2220 mdb->vid = vid;
2221 ether_addr_copy(mdb->addr, addr.mac);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002222 } else {
2223 return -EOPNOTSUPP;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002224 }
Vivien Didelot83dabd12016-08-31 11:50:04 -04002225
2226 err = cb(obj);
2227 if (err)
2228 return err;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002229 } while (!is_broadcast_ether_addr(addr.mac));
2230
2231 return err;
2232}
2233
Vivien Didelot83dabd12016-08-31 11:50:04 -04002234static int mv88e6xxx_port_db_dump(struct mv88e6xxx_chip *chip, int port,
2235 struct switchdev_obj *obj,
2236 int (*cb)(struct switchdev_obj *obj))
2237{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04002238 struct mv88e6xxx_vtu_entry vlan = {
Vivien Didelot83dabd12016-08-31 11:50:04 -04002239 .vid = GLOBAL_VTU_VID_MASK, /* all ones */
2240 };
2241 u16 fid;
2242 int err;
2243
2244 /* Dump port's default Filtering Information Database (VLAN ID 0) */
Vivien Didelotb4e48c52016-11-04 03:23:29 +01002245 err = mv88e6xxx_port_get_fid(chip, port, &fid);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002246 if (err)
2247 return err;
2248
2249 err = mv88e6xxx_port_db_dump_fid(chip, fid, 0, port, obj, cb);
2250 if (err)
2251 return err;
2252
2253 /* Dump VLANs' Filtering Information Databases */
2254 err = _mv88e6xxx_vtu_vid_write(chip, vlan.vid);
2255 if (err)
2256 return err;
2257
2258 do {
2259 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
2260 if (err)
2261 return err;
2262
2263 if (!vlan.valid)
2264 break;
2265
2266 err = mv88e6xxx_port_db_dump_fid(chip, vlan.fid, vlan.vid, port,
2267 obj, cb);
2268 if (err)
2269 return err;
2270 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
2271
2272 return err;
2273}
2274
Vivien Didelotf81ec902016-05-09 13:22:58 -04002275static int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
2276 struct switchdev_obj_port_fdb *fdb,
2277 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotf33475b2015-10-22 09:34:41 -04002278{
Vivien Didelot04bed142016-08-31 18:06:13 -04002279 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002280 int err;
2281
Vivien Didelotfad09c72016-06-21 12:28:20 -04002282 mutex_lock(&chip->reg_lock);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002283 err = mv88e6xxx_port_db_dump(chip, port, &fdb->obj, cb);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002284 mutex_unlock(&chip->reg_lock);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002285
2286 return err;
2287}
2288
Vivien Didelotf81ec902016-05-09 13:22:58 -04002289static int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
2290 struct net_device *bridge)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002291{
Vivien Didelot04bed142016-08-31 18:06:13 -04002292 struct mv88e6xxx_chip *chip = ds->priv;
Colin Ian King1d9619d2016-04-25 23:11:22 +01002293 int i, err = 0;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002294
Vivien Didelotfad09c72016-06-21 12:28:20 -04002295 mutex_lock(&chip->reg_lock);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002296
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002297 /* Assign the bridge and remap each port's VLANTable */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002298 chip->ports[port].bridge_dev = bridge;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002299
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002300 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002301 if (chip->ports[i].bridge_dev == bridge) {
2302 err = _mv88e6xxx_port_based_vlan_map(chip, i);
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002303 if (err)
2304 break;
2305 }
2306 }
2307
Vivien Didelotfad09c72016-06-21 12:28:20 -04002308 mutex_unlock(&chip->reg_lock);
Vivien Didelota6692752016-02-12 12:09:39 -05002309
Vivien Didelot466dfa02016-02-26 13:16:05 -05002310 return err;
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002311}
2312
Vivien Didelotf81ec902016-05-09 13:22:58 -04002313static void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002314{
Vivien Didelot04bed142016-08-31 18:06:13 -04002315 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002316 struct net_device *bridge = chip->ports[port].bridge_dev;
Vivien Didelot16bfa702016-03-13 16:21:33 -04002317 int i;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002318
Vivien Didelotfad09c72016-06-21 12:28:20 -04002319 mutex_lock(&chip->reg_lock);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002320
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002321 /* Unassign the bridge and remap each port's VLANTable */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002322 chip->ports[port].bridge_dev = NULL;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002323
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002324 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002325 if (i == port || chip->ports[i].bridge_dev == bridge)
2326 if (_mv88e6xxx_port_based_vlan_map(chip, i))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002327 netdev_warn(ds->ports[i].netdev,
2328 "failed to remap\n");
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002329
Vivien Didelotfad09c72016-06-21 12:28:20 -04002330 mutex_unlock(&chip->reg_lock);
Vivien Didelot66d9cd02016-02-05 14:07:14 -05002331}
2332
Vivien Didelotfad09c72016-06-21 12:28:20 -04002333static int mv88e6xxx_switch_reset(struct mv88e6xxx_chip *chip)
Vivien Didelot552238b2016-05-09 13:22:49 -04002334{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002335 bool ppu_active = mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE);
Vivien Didelot552238b2016-05-09 13:22:49 -04002336 u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002337 struct gpio_desc *gpiod = chip->reset;
Vivien Didelot552238b2016-05-09 13:22:49 -04002338 unsigned long timeout;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002339 u16 reg;
Vivien Didelota935c052016-09-29 12:21:53 -04002340 int err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002341 int i;
2342
2343 /* Set all ports to the disabled state. */
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002344 for (i = 0; i < mv88e6xxx_num_ports(chip); i++) {
Vivien Didelote28def332016-11-04 03:23:27 +01002345 err = mv88e6xxx_port_set_state(chip, i,
2346 PORT_CONTROL_STATE_DISABLED);
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002347 if (err)
2348 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002349 }
2350
2351 /* Wait for transmit queues to drain. */
2352 usleep_range(2000, 4000);
2353
2354 /* If there is a gpio connected to the reset pin, toggle it */
2355 if (gpiod) {
2356 gpiod_set_value_cansleep(gpiod, 1);
2357 usleep_range(10000, 20000);
2358 gpiod_set_value_cansleep(gpiod, 0);
2359 usleep_range(10000, 20000);
2360 }
2361
2362 /* Reset the switch. Keep the PPU active if requested. The PPU
2363 * needs to be active to support indirect phy register access
2364 * through global registers 0x18 and 0x19.
2365 */
2366 if (ppu_active)
Vivien Didelota935c052016-09-29 12:21:53 -04002367 err = mv88e6xxx_g1_write(chip, 0x04, 0xc000);
Vivien Didelot552238b2016-05-09 13:22:49 -04002368 else
Vivien Didelota935c052016-09-29 12:21:53 -04002369 err = mv88e6xxx_g1_write(chip, 0x04, 0xc400);
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002370 if (err)
2371 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002372
2373 /* Wait up to one second for reset to complete. */
2374 timeout = jiffies + 1 * HZ;
2375 while (time_before(jiffies, timeout)) {
Vivien Didelota935c052016-09-29 12:21:53 -04002376 err = mv88e6xxx_g1_read(chip, 0x00, &reg);
2377 if (err)
2378 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002379
Vivien Didelota935c052016-09-29 12:21:53 -04002380 if ((reg & is_reset) == is_reset)
Vivien Didelot552238b2016-05-09 13:22:49 -04002381 break;
2382 usleep_range(1000, 2000);
2383 }
2384 if (time_after(jiffies, timeout))
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002385 err = -ETIMEDOUT;
Vivien Didelot552238b2016-05-09 13:22:49 -04002386 else
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002387 err = 0;
Vivien Didelot552238b2016-05-09 13:22:49 -04002388
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002389 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002390}
2391
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002392static int mv88e6xxx_serdes_power_on(struct mv88e6xxx_chip *chip)
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002393{
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002394 u16 val;
2395 int err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002396
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002397 /* Clear Power Down bit */
2398 err = mv88e6xxx_serdes_read(chip, MII_BMCR, &val);
2399 if (err)
2400 return err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002401
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002402 if (val & BMCR_PDOWN) {
2403 val &= ~BMCR_PDOWN;
2404 err = mv88e6xxx_serdes_write(chip, MII_BMCR, val);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002405 }
2406
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002407 return err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002408}
2409
Vivien Didelotfad09c72016-06-21 12:28:20 -04002410static int mv88e6xxx_setup_port(struct mv88e6xxx_chip *chip, int port)
Guenter Roeckd827e882015-03-26 18:36:29 -07002411{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002412 struct dsa_switch *ds = chip->ds;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002413 int err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002414 u16 reg;
Guenter Roeckd827e882015-03-26 18:36:29 -07002415
Vivien Didelotd78343d2016-11-04 03:23:36 +01002416 /* MAC Forcing register: don't force link, speed, duplex or flow control
2417 * state to any particular values on physical ports, but force the CPU
2418 * port and all DSA ports to their maximum bandwidth and full duplex.
2419 */
2420 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))
2421 err = mv88e6xxx_port_setup_mac(chip, port, LINK_FORCED_UP,
2422 SPEED_MAX, DUPLEX_FULL,
2423 PHY_INTERFACE_MODE_NA);
2424 else
2425 err = mv88e6xxx_port_setup_mac(chip, port, LINK_UNFORCED,
2426 SPEED_UNFORCED, DUPLEX_UNFORCED,
2427 PHY_INTERFACE_MODE_NA);
2428 if (err)
2429 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002430
2431 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
2432 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
2433 * tunneling, determine priority by looking at 802.1p and IP
2434 * priority fields (IP prio has precedence), and set STP state
2435 * to Forwarding.
2436 *
2437 * If this is the CPU link, use DSA or EDSA tagging depending
2438 * on which tagging mode was configured.
2439 *
2440 * If this is a link to another switch, use DSA tagging mode.
2441 *
2442 * If this is the upstream port for this switch, enable
2443 * forwarding of unknown unicasts and multicasts.
2444 */
2445 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002446 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2447 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2448 mv88e6xxx_6095_family(chip) || mv88e6xxx_6065_family(chip) ||
2449 mv88e6xxx_6185_family(chip) || mv88e6xxx_6320_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002450 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
2451 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
2452 PORT_CONTROL_STATE_FORWARDING;
2453 if (dsa_is_cpu_port(ds, port)) {
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002454 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA))
Andrew Lunn5377b802016-06-04 21:17:02 +02002455 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA |
Andrew Lunnc047a1f2015-09-29 01:50:56 +02002456 PORT_CONTROL_FORWARD_UNKNOWN_MC;
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002457 else
2458 reg |= PORT_CONTROL_DSA_TAG;
Jamie Lentinf027e0c2016-08-22 16:01:04 +02002459 reg |= PORT_CONTROL_EGRESS_ADD_TAG |
2460 PORT_CONTROL_FORWARD_UNKNOWN;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002461 }
Andrew Lunn6083ce72015-08-17 23:52:52 +02002462 if (dsa_is_dsa_port(ds, port)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002463 if (mv88e6xxx_6095_family(chip) ||
2464 mv88e6xxx_6185_family(chip))
Andrew Lunn6083ce72015-08-17 23:52:52 +02002465 reg |= PORT_CONTROL_DSA_TAG;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002466 if (mv88e6xxx_6352_family(chip) ||
2467 mv88e6xxx_6351_family(chip) ||
2468 mv88e6xxx_6165_family(chip) ||
2469 mv88e6xxx_6097_family(chip) ||
2470 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002471 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunn6083ce72015-08-17 23:52:52 +02002472 }
2473
Andrew Lunn54d792f2015-05-06 01:09:47 +02002474 if (port == dsa_upstream_port(ds))
2475 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2476 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2477 }
2478 if (reg) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002479 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL, reg);
2480 if (err)
2481 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002482 }
2483
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002484 /* If this port is connected to a SerDes, make sure the SerDes is not
2485 * powered down.
2486 */
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002487 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_SERDES)) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002488 err = mv88e6xxx_port_read(chip, port, PORT_STATUS, &reg);
2489 if (err)
2490 return err;
2491 reg &= PORT_STATUS_CMODE_MASK;
2492 if ((reg == PORT_STATUS_CMODE_100BASE_X) ||
2493 (reg == PORT_STATUS_CMODE_1000BASE_X) ||
2494 (reg == PORT_STATUS_CMODE_SGMII)) {
2495 err = mv88e6xxx_serdes_power_on(chip);
2496 if (err < 0)
2497 return err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002498 }
2499 }
2500
Vivien Didelot8efdda42015-08-13 12:52:23 -04002501 /* Port Control 2: don't force a good FCS, set the maximum frame size to
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002502 * 10240 bytes, disable 802.1q tags checking, don't discard tagged or
Vivien Didelot8efdda42015-08-13 12:52:23 -04002503 * untagged frames on this port, do a destination address lookup on all
2504 * received packets as usual, disable ARP mirroring and don't send a
2505 * copy of all transmitted/received frames on this port to the CPU.
Andrew Lunn54d792f2015-05-06 01:09:47 +02002506 */
2507 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002508 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2509 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2510 mv88e6xxx_6095_family(chip) || mv88e6xxx_6320_family(chip) ||
2511 mv88e6xxx_6185_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002512 reg = PORT_CONTROL_2_MAP_DA;
2513
Vivien Didelotfad09c72016-06-21 12:28:20 -04002514 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2515 mv88e6xxx_6165_family(chip) || mv88e6xxx_6320_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002516 reg |= PORT_CONTROL_2_JUMBO_10240;
2517
Vivien Didelotfad09c72016-06-21 12:28:20 -04002518 if (mv88e6xxx_6095_family(chip) || mv88e6xxx_6185_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002519 /* Set the upstream port this port should use */
2520 reg |= dsa_upstream_port(ds);
2521 /* enable forwarding of unknown multicast addresses to
2522 * the upstream port
2523 */
2524 if (port == dsa_upstream_port(ds))
2525 reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
2526 }
2527
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002528 reg |= PORT_CONTROL_2_8021Q_DISABLED;
Vivien Didelot8efdda42015-08-13 12:52:23 -04002529
Andrew Lunn54d792f2015-05-06 01:09:47 +02002530 if (reg) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002531 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_2, reg);
2532 if (err)
2533 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002534 }
2535
2536 /* Port Association Vector: when learning source addresses
2537 * of packets, add the address to the address database using
2538 * a port bitmap that has only the bit for this port set and
2539 * the other bits clear.
2540 */
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002541 reg = 1 << port;
Vivien Didelot996ecb82016-04-14 14:42:08 -04002542 /* Disable learning for CPU port */
2543 if (dsa_is_cpu_port(ds, port))
Vivien Didelot65fa4022016-04-14 14:42:07 -04002544 reg = 0;
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002545
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002546 err = mv88e6xxx_port_write(chip, port, PORT_ASSOC_VECTOR, reg);
2547 if (err)
2548 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002549
2550 /* Egress rate control 2: disable egress rate control. */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002551 err = mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL_2, 0x0000);
2552 if (err)
2553 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002554
Vivien Didelotfad09c72016-06-21 12:28:20 -04002555 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2556 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2557 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002558 /* Do not limit the period of time that this port can
2559 * be paused for by the remote end or the period of
2560 * time that this port can pause the remote end.
2561 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002562 err = mv88e6xxx_port_write(chip, port, PORT_PAUSE_CTRL, 0x0000);
2563 if (err)
2564 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002565
2566 /* Port ATU control: disable limiting the number of
2567 * address database entries that this port is allowed
2568 * to use.
2569 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002570 err = mv88e6xxx_port_write(chip, port, PORT_ATU_CONTROL,
2571 0x0000);
Andrew Lunn54d792f2015-05-06 01:09:47 +02002572 /* Priority Override: disable DA, SA and VTU priority
2573 * override.
2574 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002575 err = mv88e6xxx_port_write(chip, port, PORT_PRI_OVERRIDE,
2576 0x0000);
2577 if (err)
2578 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002579
2580 /* Port Ethertype: use the Ethertype DSA Ethertype
2581 * value.
2582 */
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002583 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA)) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002584 err = mv88e6xxx_port_write(chip, port, PORT_ETH_TYPE,
2585 ETH_P_EDSA);
2586 if (err)
2587 return err;
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002588 }
2589
Andrew Lunn54d792f2015-05-06 01:09:47 +02002590 /* Tag Remap: use an identity 802.1p prio -> switch
2591 * prio mapping.
2592 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002593 err = mv88e6xxx_port_write(chip, port, PORT_TAG_REGMAP_0123,
2594 0x3210);
2595 if (err)
2596 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002597
2598 /* Tag Remap 2: use an identity 802.1p prio -> switch
2599 * prio mapping.
2600 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002601 err = mv88e6xxx_port_write(chip, port, PORT_TAG_REGMAP_4567,
2602 0x7654);
2603 if (err)
2604 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002605 }
2606
Jamie Lentin1bc261f2016-08-22 22:47:08 +01002607 /* Rate Control: disable ingress rate limiting. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002608 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2609 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
Vivien Didelotfad09c72016-06-21 12:28:20 -04002610 mv88e6xxx_6320_family(chip)) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002611 err = mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL,
2612 0x0001);
2613 if (err)
2614 return err;
Jamie Lentin1bc261f2016-08-22 22:47:08 +01002615 } else if (mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip)) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002616 err = mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL,
2617 0x0000);
2618 if (err)
2619 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002620 }
2621
Guenter Roeck366f0a02015-03-26 18:36:30 -07002622 /* Port Control 1: disable trunking, disable sending
2623 * learning messages to this port.
Guenter Roeckd827e882015-03-26 18:36:29 -07002624 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002625 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_1, 0x0000);
2626 if (err)
2627 return err;
Guenter Roeckd827e882015-03-26 18:36:29 -07002628
Vivien Didelot207afda2016-04-14 14:42:09 -04002629 /* Port based VLAN map: give each port the same default address
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002630 * database, and allow bidirectional communication between the
2631 * CPU and DSA port(s), and the other ports.
Guenter Roeckd827e882015-03-26 18:36:29 -07002632 */
Vivien Didelotb4e48c52016-11-04 03:23:29 +01002633 err = mv88e6xxx_port_set_fid(chip, port, 0);
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002634 if (err)
2635 return err;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002636
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002637 err = _mv88e6xxx_port_based_vlan_map(chip, port);
2638 if (err)
2639 return err;
Guenter Roeckd827e882015-03-26 18:36:29 -07002640
2641 /* Default VLAN ID and priority: don't set a default VLAN
2642 * ID, and set the default packet priority to zero.
2643 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002644 return mv88e6xxx_port_write(chip, port, PORT_DEFAULT_VLAN, 0x0000);
Andrew Lunndbde9e62015-05-06 01:09:48 +02002645}
2646
Wei Yongjunaa0938c2016-10-18 15:53:37 +00002647static int mv88e6xxx_g1_set_switch_mac(struct mv88e6xxx_chip *chip, u8 *addr)
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002648{
2649 int err;
2650
Vivien Didelota935c052016-09-29 12:21:53 -04002651 err = mv88e6xxx_g1_write(chip, GLOBAL_MAC_01, (addr[0] << 8) | addr[1]);
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002652 if (err)
2653 return err;
2654
Vivien Didelota935c052016-09-29 12:21:53 -04002655 err = mv88e6xxx_g1_write(chip, GLOBAL_MAC_23, (addr[2] << 8) | addr[3]);
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002656 if (err)
2657 return err;
2658
Vivien Didelota935c052016-09-29 12:21:53 -04002659 err = mv88e6xxx_g1_write(chip, GLOBAL_MAC_45, (addr[4] << 8) | addr[5]);
2660 if (err)
2661 return err;
2662
2663 return 0;
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002664}
2665
Vivien Didelotacddbd22016-07-18 20:45:39 -04002666static int mv88e6xxx_g1_set_age_time(struct mv88e6xxx_chip *chip,
2667 unsigned int msecs)
2668{
2669 const unsigned int coeff = chip->info->age_time_coeff;
2670 const unsigned int min = 0x01 * coeff;
2671 const unsigned int max = 0xff * coeff;
2672 u8 age_time;
2673 u16 val;
2674 int err;
2675
2676 if (msecs < min || msecs > max)
2677 return -ERANGE;
2678
2679 /* Round to nearest multiple of coeff */
2680 age_time = (msecs + coeff / 2) / coeff;
2681
Vivien Didelota935c052016-09-29 12:21:53 -04002682 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_CONTROL, &val);
Vivien Didelotacddbd22016-07-18 20:45:39 -04002683 if (err)
2684 return err;
2685
2686 /* AgeTime is 11:4 bits */
2687 val &= ~0xff0;
2688 val |= age_time << 4;
2689
Vivien Didelota935c052016-09-29 12:21:53 -04002690 return mv88e6xxx_g1_write(chip, GLOBAL_ATU_CONTROL, val);
Vivien Didelotacddbd22016-07-18 20:45:39 -04002691}
2692
Vivien Didelot2cfcd962016-07-18 20:45:40 -04002693static int mv88e6xxx_set_ageing_time(struct dsa_switch *ds,
2694 unsigned int ageing_time)
2695{
Vivien Didelot04bed142016-08-31 18:06:13 -04002696 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot2cfcd962016-07-18 20:45:40 -04002697 int err;
2698
2699 mutex_lock(&chip->reg_lock);
2700 err = mv88e6xxx_g1_set_age_time(chip, ageing_time);
2701 mutex_unlock(&chip->reg_lock);
2702
2703 return err;
2704}
2705
Vivien Didelot97299342016-07-18 20:45:30 -04002706static int mv88e6xxx_g1_setup(struct mv88e6xxx_chip *chip)
Vivien Didelot08a01262016-05-09 13:22:50 -04002707{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002708 struct dsa_switch *ds = chip->ds;
Vivien Didelotb0745e872016-05-09 13:22:53 -04002709 u32 upstream_port = dsa_upstream_port(ds);
Vivien Didelot119477b2016-05-09 13:22:51 -04002710 u16 reg;
Vivien Didelot08a01262016-05-09 13:22:50 -04002711 int err;
Vivien Didelot08a01262016-05-09 13:22:50 -04002712
Vivien Didelot119477b2016-05-09 13:22:51 -04002713 /* Enable the PHY Polling Unit if present, don't discard any packets,
2714 * and mask all interrupt sources.
2715 */
Andrew Lunndc30c352016-10-16 19:56:49 +02002716 err = mv88e6xxx_g1_read(chip, GLOBAL_CONTROL, &reg);
2717 if (err < 0)
2718 return err;
2719
2720 reg &= ~GLOBAL_CONTROL_PPU_ENABLE;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002721 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU) ||
2722 mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE))
Vivien Didelot119477b2016-05-09 13:22:51 -04002723 reg |= GLOBAL_CONTROL_PPU_ENABLE;
2724
Vivien Didelota935c052016-09-29 12:21:53 -04002725 err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL, reg);
Vivien Didelot119477b2016-05-09 13:22:51 -04002726 if (err)
2727 return err;
2728
Vivien Didelotb0745e872016-05-09 13:22:53 -04002729 /* Configure the upstream port, and configure it as the port to which
2730 * ingress and egress and ARP monitor frames are to be sent.
2731 */
2732 reg = upstream_port << GLOBAL_MONITOR_CONTROL_INGRESS_SHIFT |
2733 upstream_port << GLOBAL_MONITOR_CONTROL_EGRESS_SHIFT |
2734 upstream_port << GLOBAL_MONITOR_CONTROL_ARP_SHIFT;
Vivien Didelota935c052016-09-29 12:21:53 -04002735 err = mv88e6xxx_g1_write(chip, GLOBAL_MONITOR_CONTROL, reg);
Vivien Didelotb0745e872016-05-09 13:22:53 -04002736 if (err)
2737 return err;
2738
Vivien Didelot50484ff2016-05-09 13:22:54 -04002739 /* Disable remote management, and set the switch's DSA device number. */
Vivien Didelota935c052016-09-29 12:21:53 -04002740 err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL_2,
2741 GLOBAL_CONTROL_2_MULTIPLE_CASCADE |
2742 (ds->index & 0x1f));
Vivien Didelot50484ff2016-05-09 13:22:54 -04002743 if (err)
2744 return err;
2745
Vivien Didelotacddbd22016-07-18 20:45:39 -04002746 /* Clear all the VTU and STU entries */
2747 err = _mv88e6xxx_vtu_stu_flush(chip);
2748 if (err < 0)
2749 return err;
2750
Vivien Didelot08a01262016-05-09 13:22:50 -04002751 /* Set the default address aging time to 5 minutes, and
2752 * enable address learn messages to be sent to all message
2753 * ports.
2754 */
Vivien Didelota935c052016-09-29 12:21:53 -04002755 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_CONTROL,
2756 GLOBAL_ATU_CONTROL_LEARN2ALL);
Vivien Didelot08a01262016-05-09 13:22:50 -04002757 if (err)
2758 return err;
2759
Vivien Didelotacddbd22016-07-18 20:45:39 -04002760 err = mv88e6xxx_g1_set_age_time(chip, 300000);
2761 if (err)
Vivien Didelot97299342016-07-18 20:45:30 -04002762 return err;
2763
2764 /* Clear all ATU entries */
2765 err = _mv88e6xxx_atu_flush(chip, 0, true);
2766 if (err)
2767 return err;
2768
Vivien Didelot08a01262016-05-09 13:22:50 -04002769 /* Configure the IP ToS mapping registers. */
Vivien Didelota935c052016-09-29 12:21:53 -04002770 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_0, 0x0000);
Vivien Didelot08a01262016-05-09 13:22:50 -04002771 if (err)
2772 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002773 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_1, 0x0000);
Vivien Didelot08a01262016-05-09 13:22:50 -04002774 if (err)
2775 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002776 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_2, 0x5555);
Vivien Didelot08a01262016-05-09 13:22:50 -04002777 if (err)
2778 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002779 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_3, 0x5555);
Vivien Didelot08a01262016-05-09 13:22:50 -04002780 if (err)
2781 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002782 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_4, 0xaaaa);
Vivien Didelot08a01262016-05-09 13:22:50 -04002783 if (err)
2784 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002785 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_5, 0xaaaa);
Vivien Didelot08a01262016-05-09 13:22:50 -04002786 if (err)
2787 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002788 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_6, 0xffff);
Vivien Didelot08a01262016-05-09 13:22:50 -04002789 if (err)
2790 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002791 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_7, 0xffff);
Vivien Didelot08a01262016-05-09 13:22:50 -04002792 if (err)
2793 return err;
2794
2795 /* Configure the IEEE 802.1p priority mapping register. */
Vivien Didelota935c052016-09-29 12:21:53 -04002796 err = mv88e6xxx_g1_write(chip, GLOBAL_IEEE_PRI, 0xfa41);
Vivien Didelot08a01262016-05-09 13:22:50 -04002797 if (err)
2798 return err;
2799
Vivien Didelot97299342016-07-18 20:45:30 -04002800 /* Clear the statistics counters for all ports */
Vivien Didelota935c052016-09-29 12:21:53 -04002801 err = mv88e6xxx_g1_write(chip, GLOBAL_STATS_OP,
2802 GLOBAL_STATS_OP_FLUSH_ALL);
Vivien Didelot97299342016-07-18 20:45:30 -04002803 if (err)
2804 return err;
2805
2806 /* Wait for the flush to complete. */
2807 err = _mv88e6xxx_stats_wait(chip);
2808 if (err)
2809 return err;
2810
2811 return 0;
2812}
2813
Vivien Didelotf81ec902016-05-09 13:22:58 -04002814static int mv88e6xxx_setup(struct dsa_switch *ds)
Guenter Roeckacdaffc2015-03-26 18:36:28 -07002815{
Vivien Didelot04bed142016-08-31 18:06:13 -04002816 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot552238b2016-05-09 13:22:49 -04002817 int err;
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002818 int i;
2819
Vivien Didelotfad09c72016-06-21 12:28:20 -04002820 chip->ds = ds;
2821 ds->slave_mii_bus = chip->mdio_bus;
Vivien Didelot552238b2016-05-09 13:22:49 -04002822
Vivien Didelotfad09c72016-06-21 12:28:20 -04002823 mutex_lock(&chip->reg_lock);
Vivien Didelot552238b2016-05-09 13:22:49 -04002824
Vivien Didelot97299342016-07-18 20:45:30 -04002825 /* Setup Switch Port Registers */
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002826 for (i = 0; i < mv88e6xxx_num_ports(chip); i++) {
Vivien Didelot97299342016-07-18 20:45:30 -04002827 err = mv88e6xxx_setup_port(chip, i);
2828 if (err)
2829 goto unlock;
2830 }
2831
2832 /* Setup Switch Global 1 Registers */
2833 err = mv88e6xxx_g1_setup(chip);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002834 if (err)
2835 goto unlock;
2836
Vivien Didelot97299342016-07-18 20:45:30 -04002837 /* Setup Switch Global 2 Registers */
2838 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_GLOBAL2)) {
2839 err = mv88e6xxx_g2_setup(chip);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002840 if (err)
2841 goto unlock;
2842 }
Andrew Lunn54d792f2015-05-06 01:09:47 +02002843
Vivien Didelot6b17e862015-08-13 12:52:18 -04002844unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002845 mutex_unlock(&chip->reg_lock);
Andrew Lunndb687a52015-06-20 21:31:29 +02002846
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002847 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002848}
2849
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002850static int mv88e6xxx_set_addr(struct dsa_switch *ds, u8 *addr)
2851{
Vivien Didelot04bed142016-08-31 18:06:13 -04002852 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002853 int err;
2854
Vivien Didelotb073d4e2016-09-29 12:22:01 -04002855 if (!chip->info->ops->set_switch_mac)
2856 return -EOPNOTSUPP;
2857
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002858 mutex_lock(&chip->reg_lock);
Vivien Didelotb073d4e2016-09-29 12:22:01 -04002859 err = chip->info->ops->set_switch_mac(chip, addr);
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002860 mutex_unlock(&chip->reg_lock);
2861
2862 return err;
2863}
2864
Vivien Didelote57e5e72016-08-15 17:19:00 -04002865static int mv88e6xxx_mdio_read(struct mii_bus *bus, int phy, int reg)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002866{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002867 struct mv88e6xxx_chip *chip = bus->priv;
Vivien Didelote57e5e72016-08-15 17:19:00 -04002868 u16 val;
2869 int err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002870
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002871 if (phy >= mv88e6xxx_num_ports(chip))
Andrew Lunn158bc062016-04-28 21:24:06 -04002872 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002873
Vivien Didelotfad09c72016-06-21 12:28:20 -04002874 mutex_lock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04002875 err = mv88e6xxx_phy_read(chip, phy, reg, &val);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002876 mutex_unlock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04002877
2878 return err ? err : val;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002879}
2880
Vivien Didelote57e5e72016-08-15 17:19:00 -04002881static int mv88e6xxx_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002882{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002883 struct mv88e6xxx_chip *chip = bus->priv;
Vivien Didelote57e5e72016-08-15 17:19:00 -04002884 int err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002885
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002886 if (phy >= mv88e6xxx_num_ports(chip))
Andrew Lunn158bc062016-04-28 21:24:06 -04002887 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002888
Vivien Didelotfad09c72016-06-21 12:28:20 -04002889 mutex_lock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04002890 err = mv88e6xxx_phy_write(chip, phy, reg, val);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002891 mutex_unlock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04002892
2893 return err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002894}
2895
Vivien Didelotfad09c72016-06-21 12:28:20 -04002896static int mv88e6xxx_mdio_register(struct mv88e6xxx_chip *chip,
Andrew Lunnb516d452016-06-04 21:17:06 +02002897 struct device_node *np)
2898{
2899 static int index;
2900 struct mii_bus *bus;
2901 int err;
2902
Andrew Lunnb516d452016-06-04 21:17:06 +02002903 if (np)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002904 chip->mdio_np = of_get_child_by_name(np, "mdio");
Andrew Lunnb516d452016-06-04 21:17:06 +02002905
Vivien Didelotfad09c72016-06-21 12:28:20 -04002906 bus = devm_mdiobus_alloc(chip->dev);
Andrew Lunnb516d452016-06-04 21:17:06 +02002907 if (!bus)
2908 return -ENOMEM;
2909
Vivien Didelotfad09c72016-06-21 12:28:20 -04002910 bus->priv = (void *)chip;
Andrew Lunnb516d452016-06-04 21:17:06 +02002911 if (np) {
2912 bus->name = np->full_name;
2913 snprintf(bus->id, MII_BUS_ID_SIZE, "%s", np->full_name);
2914 } else {
2915 bus->name = "mv88e6xxx SMI";
2916 snprintf(bus->id, MII_BUS_ID_SIZE, "mv88e6xxx-%d", index++);
2917 }
2918
2919 bus->read = mv88e6xxx_mdio_read;
2920 bus->write = mv88e6xxx_mdio_write;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002921 bus->parent = chip->dev;
Andrew Lunnb516d452016-06-04 21:17:06 +02002922
Vivien Didelotfad09c72016-06-21 12:28:20 -04002923 if (chip->mdio_np)
2924 err = of_mdiobus_register(bus, chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02002925 else
2926 err = mdiobus_register(bus);
2927 if (err) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002928 dev_err(chip->dev, "Cannot register MDIO bus (%d)\n", err);
Andrew Lunnb516d452016-06-04 21:17:06 +02002929 goto out;
2930 }
Vivien Didelotfad09c72016-06-21 12:28:20 -04002931 chip->mdio_bus = bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02002932
2933 return 0;
2934
2935out:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002936 if (chip->mdio_np)
2937 of_node_put(chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02002938
2939 return err;
2940}
2941
Vivien Didelotfad09c72016-06-21 12:28:20 -04002942static void mv88e6xxx_mdio_unregister(struct mv88e6xxx_chip *chip)
Andrew Lunnb516d452016-06-04 21:17:06 +02002943
2944{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002945 struct mii_bus *bus = chip->mdio_bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02002946
2947 mdiobus_unregister(bus);
2948
Vivien Didelotfad09c72016-06-21 12:28:20 -04002949 if (chip->mdio_np)
2950 of_node_put(chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02002951}
2952
Guenter Roeckc22995c2015-07-25 09:42:28 -07002953#ifdef CONFIG_NET_DSA_HWMON
2954
2955static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
2956{
Vivien Didelot04bed142016-08-31 18:06:13 -04002957 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot9c938292016-08-15 17:19:02 -04002958 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07002959 int ret;
Guenter Roeckc22995c2015-07-25 09:42:28 -07002960
2961 *temp = 0;
2962
Vivien Didelotfad09c72016-06-21 12:28:20 -04002963 mutex_lock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07002964
Vivien Didelot9c938292016-08-15 17:19:02 -04002965 ret = mv88e6xxx_phy_write(chip, 0x0, 0x16, 0x6);
Guenter Roeckc22995c2015-07-25 09:42:28 -07002966 if (ret < 0)
2967 goto error;
2968
2969 /* Enable temperature sensor */
Vivien Didelot9c938292016-08-15 17:19:02 -04002970 ret = mv88e6xxx_phy_read(chip, 0x0, 0x1a, &val);
Guenter Roeckc22995c2015-07-25 09:42:28 -07002971 if (ret < 0)
2972 goto error;
2973
Vivien Didelot9c938292016-08-15 17:19:02 -04002974 ret = mv88e6xxx_phy_write(chip, 0x0, 0x1a, val | (1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07002975 if (ret < 0)
2976 goto error;
2977
2978 /* Wait for temperature to stabilize */
2979 usleep_range(10000, 12000);
2980
Vivien Didelot9c938292016-08-15 17:19:02 -04002981 ret = mv88e6xxx_phy_read(chip, 0x0, 0x1a, &val);
2982 if (ret < 0)
Guenter Roeckc22995c2015-07-25 09:42:28 -07002983 goto error;
Guenter Roeckc22995c2015-07-25 09:42:28 -07002984
2985 /* Disable temperature sensor */
Vivien Didelot9c938292016-08-15 17:19:02 -04002986 ret = mv88e6xxx_phy_write(chip, 0x0, 0x1a, val & ~(1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07002987 if (ret < 0)
2988 goto error;
2989
2990 *temp = ((val & 0x1f) - 5) * 5;
2991
2992error:
Vivien Didelot9c938292016-08-15 17:19:02 -04002993 mv88e6xxx_phy_write(chip, 0x0, 0x16, 0x0);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002994 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07002995 return ret;
2996}
2997
2998static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
2999{
Vivien Didelot04bed142016-08-31 18:06:13 -04003000 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003001 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003002 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003003 int ret;
3004
3005 *temp = 0;
3006
Vivien Didelot9c938292016-08-15 17:19:02 -04003007 mutex_lock(&chip->reg_lock);
3008 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 27, &val);
3009 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003010 if (ret < 0)
3011 return ret;
3012
Vivien Didelot9c938292016-08-15 17:19:02 -04003013 *temp = (val & 0xff) - 25;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003014
3015 return 0;
3016}
3017
Vivien Didelotf81ec902016-05-09 13:22:58 -04003018static int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003019{
Vivien Didelot04bed142016-08-31 18:06:13 -04003020 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn158bc062016-04-28 21:24:06 -04003021
Vivien Didelotfad09c72016-06-21 12:28:20 -04003022 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP))
Vivien Didelot6594f612016-05-09 13:22:42 -04003023 return -EOPNOTSUPP;
3024
Vivien Didelotfad09c72016-06-21 12:28:20 -04003025 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003026 return mv88e63xx_get_temp(ds, temp);
3027
3028 return mv88e61xx_get_temp(ds, temp);
3029}
3030
Vivien Didelotf81ec902016-05-09 13:22:58 -04003031static int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003032{
Vivien Didelot04bed142016-08-31 18:06:13 -04003033 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003034 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003035 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003036 int ret;
3037
Vivien Didelotfad09c72016-06-21 12:28:20 -04003038 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003039 return -EOPNOTSUPP;
3040
3041 *temp = 0;
3042
Vivien Didelot9c938292016-08-15 17:19:02 -04003043 mutex_lock(&chip->reg_lock);
3044 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3045 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003046 if (ret < 0)
3047 return ret;
3048
Vivien Didelot9c938292016-08-15 17:19:02 -04003049 *temp = (((val >> 8) & 0x1f) * 5) - 25;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003050
3051 return 0;
3052}
3053
Vivien Didelotf81ec902016-05-09 13:22:58 -04003054static int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003055{
Vivien Didelot04bed142016-08-31 18:06:13 -04003056 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003057 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003058 u16 val;
3059 int err;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003060
Vivien Didelotfad09c72016-06-21 12:28:20 -04003061 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003062 return -EOPNOTSUPP;
3063
Vivien Didelot9c938292016-08-15 17:19:02 -04003064 mutex_lock(&chip->reg_lock);
3065 err = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3066 if (err)
3067 goto unlock;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003068 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
Vivien Didelot9c938292016-08-15 17:19:02 -04003069 err = mv88e6xxx_phy_page_write(chip, phy, 6, 26,
3070 (val & 0xe0ff) | (temp << 8));
3071unlock:
3072 mutex_unlock(&chip->reg_lock);
3073
3074 return err;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003075}
3076
Vivien Didelotf81ec902016-05-09 13:22:58 -04003077static int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003078{
Vivien Didelot04bed142016-08-31 18:06:13 -04003079 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003080 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003081 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003082 int ret;
3083
Vivien Didelotfad09c72016-06-21 12:28:20 -04003084 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003085 return -EOPNOTSUPP;
3086
3087 *alarm = false;
3088
Vivien Didelot9c938292016-08-15 17:19:02 -04003089 mutex_lock(&chip->reg_lock);
3090 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3091 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003092 if (ret < 0)
3093 return ret;
3094
Vivien Didelot9c938292016-08-15 17:19:02 -04003095 *alarm = !!(val & 0x40);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003096
3097 return 0;
3098}
3099#endif /* CONFIG_NET_DSA_HWMON */
3100
Vivien Didelot855b1932016-07-20 18:18:35 -04003101static int mv88e6xxx_get_eeprom_len(struct dsa_switch *ds)
3102{
Vivien Didelot04bed142016-08-31 18:06:13 -04003103 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot855b1932016-07-20 18:18:35 -04003104
3105 return chip->eeprom_len;
3106}
3107
Vivien Didelot855b1932016-07-20 18:18:35 -04003108static int mv88e6xxx_get_eeprom(struct dsa_switch *ds,
3109 struct ethtool_eeprom *eeprom, u8 *data)
3110{
Vivien Didelot04bed142016-08-31 18:06:13 -04003111 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot855b1932016-07-20 18:18:35 -04003112 int err;
3113
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003114 if (!chip->info->ops->get_eeprom)
3115 return -EOPNOTSUPP;
3116
Vivien Didelot855b1932016-07-20 18:18:35 -04003117 mutex_lock(&chip->reg_lock);
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003118 err = chip->info->ops->get_eeprom(chip, eeprom, data);
Vivien Didelot855b1932016-07-20 18:18:35 -04003119 mutex_unlock(&chip->reg_lock);
3120
3121 if (err)
3122 return err;
3123
3124 eeprom->magic = 0xc3ec4951;
3125
3126 return 0;
3127}
3128
Vivien Didelot855b1932016-07-20 18:18:35 -04003129static int mv88e6xxx_set_eeprom(struct dsa_switch *ds,
3130 struct ethtool_eeprom *eeprom, u8 *data)
3131{
Vivien Didelot04bed142016-08-31 18:06:13 -04003132 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot855b1932016-07-20 18:18:35 -04003133 int err;
3134
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003135 if (!chip->info->ops->set_eeprom)
3136 return -EOPNOTSUPP;
3137
Vivien Didelot855b1932016-07-20 18:18:35 -04003138 if (eeprom->magic != 0xc3ec4951)
3139 return -EINVAL;
3140
3141 mutex_lock(&chip->reg_lock);
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003142 err = chip->info->ops->set_eeprom(chip, eeprom, data);
Vivien Didelot855b1932016-07-20 18:18:35 -04003143 mutex_unlock(&chip->reg_lock);
3144
3145 return err;
3146}
3147
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003148static const struct mv88e6xxx_ops mv88e6085_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003149 /* MV88E6XXX_FAMILY_6097 */
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003150 .set_switch_mac = mv88e6xxx_g1_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003151 .phy_read = mv88e6xxx_phy_ppu_read,
3152 .phy_write = mv88e6xxx_phy_ppu_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003153 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003154 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003155 .port_set_speed = mv88e6185_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003156 .stats_snapshot = mv88e6xxx_g1_stats_snapshot,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003157};
3158
3159static const struct mv88e6xxx_ops mv88e6095_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003160 /* MV88E6XXX_FAMILY_6095 */
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003161 .set_switch_mac = mv88e6xxx_g1_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003162 .phy_read = mv88e6xxx_phy_ppu_read,
3163 .phy_write = mv88e6xxx_phy_ppu_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003164 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003165 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003166 .port_set_speed = mv88e6185_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003167 .stats_snapshot = mv88e6xxx_g1_stats_snapshot,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003168};
3169
3170static const struct mv88e6xxx_ops mv88e6123_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003171 /* MV88E6XXX_FAMILY_6165 */
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003172 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003173 .phy_read = mv88e6xxx_read,
3174 .phy_write = mv88e6xxx_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003175 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003176 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003177 .port_set_speed = mv88e6185_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003178 .stats_snapshot = mv88e6xxx_g1_stats_snapshot,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003179};
3180
3181static const struct mv88e6xxx_ops mv88e6131_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003182 /* MV88E6XXX_FAMILY_6185 */
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003183 .set_switch_mac = mv88e6xxx_g1_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003184 .phy_read = mv88e6xxx_phy_ppu_read,
3185 .phy_write = mv88e6xxx_phy_ppu_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003186 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003187 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003188 .port_set_speed = mv88e6185_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003189 .stats_snapshot = mv88e6xxx_g1_stats_snapshot,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003190};
3191
3192static const struct mv88e6xxx_ops mv88e6161_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003193 /* MV88E6XXX_FAMILY_6165 */
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003194 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003195 .phy_read = mv88e6xxx_read,
3196 .phy_write = mv88e6xxx_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003197 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003198 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003199 .port_set_speed = mv88e6185_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003200 .stats_snapshot = mv88e6xxx_g1_stats_snapshot,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003201};
3202
3203static const struct mv88e6xxx_ops mv88e6165_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003204 /* MV88E6XXX_FAMILY_6165 */
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003205 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003206 .phy_read = mv88e6xxx_read,
3207 .phy_write = mv88e6xxx_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003208 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003209 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003210 .port_set_speed = mv88e6185_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003211 .stats_snapshot = mv88e6xxx_g1_stats_snapshot,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003212};
3213
3214static const struct mv88e6xxx_ops mv88e6171_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003215 /* MV88E6XXX_FAMILY_6351 */
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003216 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003217 .phy_read = mv88e6xxx_g2_smi_phy_read,
3218 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003219 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003220 .port_set_duplex = mv88e6xxx_port_set_duplex,
Andrew Lunn94d66ae2016-11-10 15:44:01 +01003221 .port_set_rgmii_delay = mv88e6352_port_set_rgmii_delay,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003222 .port_set_speed = mv88e6185_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003223 .stats_snapshot = mv88e6320_g1_stats_snapshot,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003224};
3225
3226static const struct mv88e6xxx_ops mv88e6172_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003227 /* MV88E6XXX_FAMILY_6352 */
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003228 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3229 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003230 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003231 .phy_read = mv88e6xxx_g2_smi_phy_read,
3232 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003233 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003234 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelota0a0f622016-11-04 03:23:34 +01003235 .port_set_rgmii_delay = mv88e6352_port_set_rgmii_delay,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003236 .port_set_speed = mv88e6352_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003237 .stats_snapshot = mv88e6320_g1_stats_snapshot,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003238};
3239
3240static const struct mv88e6xxx_ops mv88e6175_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003241 /* MV88E6XXX_FAMILY_6351 */
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003242 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003243 .phy_read = mv88e6xxx_g2_smi_phy_read,
3244 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003245 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003246 .port_set_duplex = mv88e6xxx_port_set_duplex,
Andrew Lunn94d66ae2016-11-10 15:44:01 +01003247 .port_set_rgmii_delay = mv88e6352_port_set_rgmii_delay,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003248 .port_set_speed = mv88e6185_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003249 .stats_snapshot = mv88e6320_g1_stats_snapshot,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003250};
3251
3252static const struct mv88e6xxx_ops mv88e6176_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003253 /* MV88E6XXX_FAMILY_6352 */
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003254 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3255 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003256 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003257 .phy_read = mv88e6xxx_g2_smi_phy_read,
3258 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003259 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003260 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelota0a0f622016-11-04 03:23:34 +01003261 .port_set_rgmii_delay = mv88e6352_port_set_rgmii_delay,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003262 .port_set_speed = mv88e6352_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003263 .stats_snapshot = mv88e6320_g1_stats_snapshot,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003264};
3265
3266static const struct mv88e6xxx_ops mv88e6185_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003267 /* MV88E6XXX_FAMILY_6185 */
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003268 .set_switch_mac = mv88e6xxx_g1_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003269 .phy_read = mv88e6xxx_phy_ppu_read,
3270 .phy_write = mv88e6xxx_phy_ppu_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003271 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003272 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003273 .port_set_speed = mv88e6185_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003274 .stats_snapshot = mv88e6xxx_g1_stats_snapshot,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003275};
3276
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003277static const struct mv88e6xxx_ops mv88e6190_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003278 /* MV88E6XXX_FAMILY_6390 */
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003279 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
3280 .phy_read = mv88e6xxx_g2_smi_phy_read,
3281 .phy_write = mv88e6xxx_g2_smi_phy_write,
3282 .port_set_link = mv88e6xxx_port_set_link,
3283 .port_set_duplex = mv88e6xxx_port_set_duplex,
3284 .port_set_rgmii_delay = mv88e6390_port_set_rgmii_delay,
3285 .port_set_speed = mv88e6390_port_set_speed,
Andrew Lunn79523472016-11-21 23:27:00 +01003286 .stats_snapshot = mv88e6390_g1_stats_snapshot,
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003287};
3288
3289static const struct mv88e6xxx_ops mv88e6190x_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003290 /* MV88E6XXX_FAMILY_6390 */
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003291 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
3292 .phy_read = mv88e6xxx_g2_smi_phy_read,
3293 .phy_write = mv88e6xxx_g2_smi_phy_write,
3294 .port_set_link = mv88e6xxx_port_set_link,
3295 .port_set_duplex = mv88e6xxx_port_set_duplex,
3296 .port_set_rgmii_delay = mv88e6390_port_set_rgmii_delay,
3297 .port_set_speed = mv88e6390x_port_set_speed,
Andrew Lunn79523472016-11-21 23:27:00 +01003298 .stats_snapshot = mv88e6390_g1_stats_snapshot,
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003299};
3300
3301static const struct mv88e6xxx_ops mv88e6191_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003302 /* MV88E6XXX_FAMILY_6390 */
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003303 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
3304 .phy_read = mv88e6xxx_g2_smi_phy_read,
3305 .phy_write = mv88e6xxx_g2_smi_phy_write,
3306 .port_set_link = mv88e6xxx_port_set_link,
3307 .port_set_duplex = mv88e6xxx_port_set_duplex,
3308 .port_set_rgmii_delay = mv88e6390_port_set_rgmii_delay,
3309 .port_set_speed = mv88e6390_port_set_speed,
Andrew Lunn79523472016-11-21 23:27:00 +01003310 .stats_snapshot = mv88e6390_g1_stats_snapshot,
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003311};
3312
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003313static const struct mv88e6xxx_ops mv88e6240_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003314 /* MV88E6XXX_FAMILY_6352 */
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003315 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3316 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003317 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003318 .phy_read = mv88e6xxx_g2_smi_phy_read,
3319 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003320 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003321 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelota0a0f622016-11-04 03:23:34 +01003322 .port_set_rgmii_delay = mv88e6352_port_set_rgmii_delay,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003323 .port_set_speed = mv88e6352_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003324 .stats_snapshot = mv88e6320_g1_stats_snapshot,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003325};
3326
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003327static const struct mv88e6xxx_ops mv88e6290_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003328 /* MV88E6XXX_FAMILY_6390 */
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003329 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
3330 .phy_read = mv88e6xxx_g2_smi_phy_read,
3331 .phy_write = mv88e6xxx_g2_smi_phy_write,
3332 .port_set_link = mv88e6xxx_port_set_link,
3333 .port_set_duplex = mv88e6xxx_port_set_duplex,
3334 .port_set_rgmii_delay = mv88e6390_port_set_rgmii_delay,
3335 .port_set_speed = mv88e6390_port_set_speed,
Andrew Lunn79523472016-11-21 23:27:00 +01003336 .stats_snapshot = mv88e6390_g1_stats_snapshot,
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003337};
3338
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003339static const struct mv88e6xxx_ops mv88e6320_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003340 /* MV88E6XXX_FAMILY_6320 */
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003341 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3342 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003343 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003344 .phy_read = mv88e6xxx_g2_smi_phy_read,
3345 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003346 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003347 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003348 .port_set_speed = mv88e6185_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003349 .stats_snapshot = mv88e6320_g1_stats_snapshot,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003350};
3351
3352static const struct mv88e6xxx_ops mv88e6321_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003353 /* MV88E6XXX_FAMILY_6321 */
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003354 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3355 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003356 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003357 .phy_read = mv88e6xxx_g2_smi_phy_read,
3358 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003359 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003360 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003361 .port_set_speed = mv88e6185_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003362 .stats_snapshot = mv88e6320_g1_stats_snapshot,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003363};
3364
3365static const struct mv88e6xxx_ops mv88e6350_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003366 /* MV88E6XXX_FAMILY_6351 */
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,
Andrew Lunn94d66ae2016-11-10 15:44:01 +01003372 .port_set_rgmii_delay = mv88e6352_port_set_rgmii_delay,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003373 .port_set_speed = mv88e6185_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003374 .stats_snapshot = mv88e6320_g1_stats_snapshot,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003375};
3376
3377static const struct mv88e6xxx_ops mv88e6351_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003378 /* MV88E6XXX_FAMILY_6351 */
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003379 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003380 .phy_read = mv88e6xxx_g2_smi_phy_read,
3381 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003382 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003383 .port_set_duplex = mv88e6xxx_port_set_duplex,
Andrew Lunn94d66ae2016-11-10 15:44:01 +01003384 .port_set_rgmii_delay = mv88e6352_port_set_rgmii_delay,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003385 .port_set_speed = mv88e6185_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003386 .stats_snapshot = mv88e6320_g1_stats_snapshot,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003387};
3388
3389static const struct mv88e6xxx_ops mv88e6352_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003390 /* MV88E6XXX_FAMILY_6352 */
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003391 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3392 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003393 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003394 .phy_read = mv88e6xxx_g2_smi_phy_read,
3395 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003396 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003397 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelota0a0f622016-11-04 03:23:34 +01003398 .port_set_rgmii_delay = mv88e6352_port_set_rgmii_delay,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003399 .port_set_speed = mv88e6352_port_set_speed,
Andrew Lunna605a0f2016-11-21 23:26:58 +01003400 .stats_snapshot = mv88e6320_g1_stats_snapshot,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003401};
3402
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003403static const struct mv88e6xxx_ops mv88e6390_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003404 /* MV88E6XXX_FAMILY_6390 */
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003405 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
3406 .phy_read = mv88e6xxx_g2_smi_phy_read,
3407 .phy_write = mv88e6xxx_g2_smi_phy_write,
3408 .port_set_link = mv88e6xxx_port_set_link,
3409 .port_set_duplex = mv88e6xxx_port_set_duplex,
3410 .port_set_rgmii_delay = mv88e6390_port_set_rgmii_delay,
3411 .port_set_speed = mv88e6390_port_set_speed,
Andrew Lunn79523472016-11-21 23:27:00 +01003412 .stats_snapshot = mv88e6390_g1_stats_snapshot,
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003413};
3414
3415static const struct mv88e6xxx_ops mv88e6390x_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003416 /* MV88E6XXX_FAMILY_6390 */
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003417 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
3418 .phy_read = mv88e6xxx_g2_smi_phy_read,
3419 .phy_write = mv88e6xxx_g2_smi_phy_write,
3420 .port_set_link = mv88e6xxx_port_set_link,
3421 .port_set_duplex = mv88e6xxx_port_set_duplex,
3422 .port_set_rgmii_delay = mv88e6390_port_set_rgmii_delay,
3423 .port_set_speed = mv88e6390x_port_set_speed,
Andrew Lunn79523472016-11-21 23:27:00 +01003424 .stats_snapshot = mv88e6390_g1_stats_snapshot,
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003425};
3426
3427static const struct mv88e6xxx_ops mv88e6391_ops = {
Andrew Lunn4b325d82016-11-21 23:26:59 +01003428 /* MV88E6XXX_FAMILY_6390 */
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003429 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
3430 .phy_read = mv88e6xxx_g2_smi_phy_read,
3431 .phy_write = mv88e6xxx_g2_smi_phy_write,
3432 .port_set_link = mv88e6xxx_port_set_link,
3433 .port_set_duplex = mv88e6xxx_port_set_duplex,
3434 .port_set_rgmii_delay = mv88e6390_port_set_rgmii_delay,
3435 .port_set_speed = mv88e6390_port_set_speed,
Andrew Lunn79523472016-11-21 23:27:00 +01003436 .stats_snapshot = mv88e6390_g1_stats_snapshot,
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003437};
3438
Vivien Didelotf81ec902016-05-09 13:22:58 -04003439static const struct mv88e6xxx_info mv88e6xxx_table[] = {
3440 [MV88E6085] = {
3441 .prod_num = PORT_SWITCH_ID_PROD_NUM_6085,
3442 .family = MV88E6XXX_FAMILY_6097,
3443 .name = "Marvell 88E6085",
3444 .num_databases = 4096,
3445 .num_ports = 10,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003446 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003447 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003448 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003449 .g1_irqs = 8,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003450 .flags = MV88E6XXX_FLAGS_FAMILY_6097,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003451 .ops = &mv88e6085_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003452 },
3453
3454 [MV88E6095] = {
3455 .prod_num = PORT_SWITCH_ID_PROD_NUM_6095,
3456 .family = MV88E6XXX_FAMILY_6095,
3457 .name = "Marvell 88E6095/88E6095F",
3458 .num_databases = 256,
3459 .num_ports = 11,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003460 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003461 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003462 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003463 .g1_irqs = 8,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003464 .flags = MV88E6XXX_FLAGS_FAMILY_6095,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003465 .ops = &mv88e6095_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003466 },
3467
3468 [MV88E6123] = {
3469 .prod_num = PORT_SWITCH_ID_PROD_NUM_6123,
3470 .family = MV88E6XXX_FAMILY_6165,
3471 .name = "Marvell 88E6123",
3472 .num_databases = 4096,
3473 .num_ports = 3,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003474 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003475 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003476 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003477 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003478 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003479 .ops = &mv88e6123_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003480 },
3481
3482 [MV88E6131] = {
3483 .prod_num = PORT_SWITCH_ID_PROD_NUM_6131,
3484 .family = MV88E6XXX_FAMILY_6185,
3485 .name = "Marvell 88E6131",
3486 .num_databases = 256,
3487 .num_ports = 8,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003488 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003489 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003490 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003491 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003492 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003493 .ops = &mv88e6131_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003494 },
3495
3496 [MV88E6161] = {
3497 .prod_num = PORT_SWITCH_ID_PROD_NUM_6161,
3498 .family = MV88E6XXX_FAMILY_6165,
3499 .name = "Marvell 88E6161",
3500 .num_databases = 4096,
3501 .num_ports = 6,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003502 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003503 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003504 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003505 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003506 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003507 .ops = &mv88e6161_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003508 },
3509
3510 [MV88E6165] = {
3511 .prod_num = PORT_SWITCH_ID_PROD_NUM_6165,
3512 .family = MV88E6XXX_FAMILY_6165,
3513 .name = "Marvell 88E6165",
3514 .num_databases = 4096,
3515 .num_ports = 6,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003516 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003517 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003518 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003519 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003520 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003521 .ops = &mv88e6165_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003522 },
3523
3524 [MV88E6171] = {
3525 .prod_num = PORT_SWITCH_ID_PROD_NUM_6171,
3526 .family = MV88E6XXX_FAMILY_6351,
3527 .name = "Marvell 88E6171",
3528 .num_databases = 4096,
3529 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003530 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003531 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003532 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003533 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003534 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003535 .ops = &mv88e6171_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003536 },
3537
3538 [MV88E6172] = {
3539 .prod_num = PORT_SWITCH_ID_PROD_NUM_6172,
3540 .family = MV88E6XXX_FAMILY_6352,
3541 .name = "Marvell 88E6172",
3542 .num_databases = 4096,
3543 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003544 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003545 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003546 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003547 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003548 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003549 .ops = &mv88e6172_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003550 },
3551
3552 [MV88E6175] = {
3553 .prod_num = PORT_SWITCH_ID_PROD_NUM_6175,
3554 .family = MV88E6XXX_FAMILY_6351,
3555 .name = "Marvell 88E6175",
3556 .num_databases = 4096,
3557 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003558 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003559 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003560 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003561 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003562 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003563 .ops = &mv88e6175_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003564 },
3565
3566 [MV88E6176] = {
3567 .prod_num = PORT_SWITCH_ID_PROD_NUM_6176,
3568 .family = MV88E6XXX_FAMILY_6352,
3569 .name = "Marvell 88E6176",
3570 .num_databases = 4096,
3571 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003572 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003573 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003574 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003575 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003576 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003577 .ops = &mv88e6176_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003578 },
3579
3580 [MV88E6185] = {
3581 .prod_num = PORT_SWITCH_ID_PROD_NUM_6185,
3582 .family = MV88E6XXX_FAMILY_6185,
3583 .name = "Marvell 88E6185",
3584 .num_databases = 256,
3585 .num_ports = 10,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003586 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003587 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003588 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003589 .g1_irqs = 8,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003590 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003591 .ops = &mv88e6185_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003592 },
3593
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003594 [MV88E6190] = {
3595 .prod_num = PORT_SWITCH_ID_PROD_NUM_6190,
3596 .family = MV88E6XXX_FAMILY_6390,
3597 .name = "Marvell 88E6190",
3598 .num_databases = 4096,
3599 .num_ports = 11, /* 10 + Z80 */
3600 .port_base_addr = 0x0,
3601 .global1_addr = 0x1b,
3602 .age_time_coeff = 15000,
3603 .g1_irqs = 9,
3604 .flags = MV88E6XXX_FLAGS_FAMILY_6390,
3605 .ops = &mv88e6190_ops,
3606 },
3607
3608 [MV88E6190X] = {
3609 .prod_num = PORT_SWITCH_ID_PROD_NUM_6190X,
3610 .family = MV88E6XXX_FAMILY_6390,
3611 .name = "Marvell 88E6190X",
3612 .num_databases = 4096,
3613 .num_ports = 11, /* 10 + Z80 */
3614 .port_base_addr = 0x0,
3615 .global1_addr = 0x1b,
3616 .age_time_coeff = 15000,
3617 .g1_irqs = 9,
3618 .flags = MV88E6XXX_FLAGS_FAMILY_6390,
3619 .ops = &mv88e6190x_ops,
3620 },
3621
3622 [MV88E6191] = {
3623 .prod_num = PORT_SWITCH_ID_PROD_NUM_6191,
3624 .family = MV88E6XXX_FAMILY_6390,
3625 .name = "Marvell 88E6191",
3626 .num_databases = 4096,
3627 .num_ports = 11, /* 10 + Z80 */
3628 .port_base_addr = 0x0,
3629 .global1_addr = 0x1b,
3630 .age_time_coeff = 15000,
3631 .flags = MV88E6XXX_FLAGS_FAMILY_6390,
3632 .ops = &mv88e6391_ops,
3633 },
3634
Vivien Didelotf81ec902016-05-09 13:22:58 -04003635 [MV88E6240] = {
3636 .prod_num = PORT_SWITCH_ID_PROD_NUM_6240,
3637 .family = MV88E6XXX_FAMILY_6352,
3638 .name = "Marvell 88E6240",
3639 .num_databases = 4096,
3640 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003641 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003642 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003643 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003644 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003645 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003646 .ops = &mv88e6240_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003647 },
3648
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003649 [MV88E6290] = {
3650 .prod_num = PORT_SWITCH_ID_PROD_NUM_6290,
3651 .family = MV88E6XXX_FAMILY_6390,
3652 .name = "Marvell 88E6290",
3653 .num_databases = 4096,
3654 .num_ports = 11, /* 10 + Z80 */
3655 .port_base_addr = 0x0,
3656 .global1_addr = 0x1b,
3657 .age_time_coeff = 15000,
3658 .g1_irqs = 9,
3659 .flags = MV88E6XXX_FLAGS_FAMILY_6390,
3660 .ops = &mv88e6290_ops,
3661 },
3662
Vivien Didelotf81ec902016-05-09 13:22:58 -04003663 [MV88E6320] = {
3664 .prod_num = PORT_SWITCH_ID_PROD_NUM_6320,
3665 .family = MV88E6XXX_FAMILY_6320,
3666 .name = "Marvell 88E6320",
3667 .num_databases = 4096,
3668 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003669 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003670 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003671 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003672 .g1_irqs = 8,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003673 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003674 .ops = &mv88e6320_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003675 },
3676
3677 [MV88E6321] = {
3678 .prod_num = PORT_SWITCH_ID_PROD_NUM_6321,
3679 .family = MV88E6XXX_FAMILY_6320,
3680 .name = "Marvell 88E6321",
3681 .num_databases = 4096,
3682 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003683 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003684 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003685 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003686 .g1_irqs = 8,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003687 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003688 .ops = &mv88e6321_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003689 },
3690
3691 [MV88E6350] = {
3692 .prod_num = PORT_SWITCH_ID_PROD_NUM_6350,
3693 .family = MV88E6XXX_FAMILY_6351,
3694 .name = "Marvell 88E6350",
3695 .num_databases = 4096,
3696 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003697 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003698 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003699 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003700 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003701 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003702 .ops = &mv88e6350_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003703 },
3704
3705 [MV88E6351] = {
3706 .prod_num = PORT_SWITCH_ID_PROD_NUM_6351,
3707 .family = MV88E6XXX_FAMILY_6351,
3708 .name = "Marvell 88E6351",
3709 .num_databases = 4096,
3710 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003711 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003712 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003713 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003714 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003715 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003716 .ops = &mv88e6351_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003717 },
3718
3719 [MV88E6352] = {
3720 .prod_num = PORT_SWITCH_ID_PROD_NUM_6352,
3721 .family = MV88E6XXX_FAMILY_6352,
3722 .name = "Marvell 88E6352",
3723 .num_databases = 4096,
3724 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003725 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003726 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003727 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003728 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003729 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003730 .ops = &mv88e6352_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003731 },
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003732 [MV88E6390] = {
3733 .prod_num = PORT_SWITCH_ID_PROD_NUM_6390,
3734 .family = MV88E6XXX_FAMILY_6390,
3735 .name = "Marvell 88E6390",
3736 .num_databases = 4096,
3737 .num_ports = 11, /* 10 + Z80 */
3738 .port_base_addr = 0x0,
3739 .global1_addr = 0x1b,
3740 .age_time_coeff = 15000,
3741 .g1_irqs = 9,
3742 .flags = MV88E6XXX_FLAGS_FAMILY_6390,
3743 .ops = &mv88e6390_ops,
3744 },
3745 [MV88E6390X] = {
3746 .prod_num = PORT_SWITCH_ID_PROD_NUM_6390X,
3747 .family = MV88E6XXX_FAMILY_6390,
3748 .name = "Marvell 88E6390X",
3749 .num_databases = 4096,
3750 .num_ports = 11, /* 10 + Z80 */
3751 .port_base_addr = 0x0,
3752 .global1_addr = 0x1b,
3753 .age_time_coeff = 15000,
3754 .g1_irqs = 9,
3755 .flags = MV88E6XXX_FLAGS_FAMILY_6390,
3756 .ops = &mv88e6390x_ops,
3757 },
Vivien Didelotf81ec902016-05-09 13:22:58 -04003758};
3759
Vivien Didelot5f7c0362016-06-20 13:14:04 -04003760static const struct mv88e6xxx_info *mv88e6xxx_lookup_info(unsigned int prod_num)
Vivien Didelotb9b37712015-10-30 19:39:48 -04003761{
Vivien Didelota439c062016-04-17 13:23:58 -04003762 int i;
Vivien Didelotb9b37712015-10-30 19:39:48 -04003763
Vivien Didelot5f7c0362016-06-20 13:14:04 -04003764 for (i = 0; i < ARRAY_SIZE(mv88e6xxx_table); ++i)
3765 if (mv88e6xxx_table[i].prod_num == prod_num)
3766 return &mv88e6xxx_table[i];
Vivien Didelotb9b37712015-10-30 19:39:48 -04003767
Vivien Didelotb9b37712015-10-30 19:39:48 -04003768 return NULL;
3769}
3770
Vivien Didelotfad09c72016-06-21 12:28:20 -04003771static int mv88e6xxx_detect(struct mv88e6xxx_chip *chip)
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003772{
3773 const struct mv88e6xxx_info *info;
Vivien Didelot8f6345b2016-07-20 18:18:36 -04003774 unsigned int prod_num, rev;
3775 u16 id;
3776 int err;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003777
Vivien Didelot8f6345b2016-07-20 18:18:36 -04003778 mutex_lock(&chip->reg_lock);
3779 err = mv88e6xxx_port_read(chip, 0, PORT_SWITCH_ID, &id);
3780 mutex_unlock(&chip->reg_lock);
3781 if (err)
3782 return err;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003783
3784 prod_num = (id & 0xfff0) >> 4;
3785 rev = id & 0x000f;
3786
3787 info = mv88e6xxx_lookup_info(prod_num);
3788 if (!info)
3789 return -ENODEV;
3790
Vivien Didelotcaac8542016-06-20 13:14:09 -04003791 /* Update the compatible info with the probed one */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003792 chip->info = info;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003793
Vivien Didelotca070c12016-09-02 14:45:34 -04003794 err = mv88e6xxx_g2_require(chip);
3795 if (err)
3796 return err;
3797
Vivien Didelotfad09c72016-06-21 12:28:20 -04003798 dev_info(chip->dev, "switch 0x%x detected: %s, revision %u\n",
3799 chip->info->prod_num, chip->info->name, rev);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003800
3801 return 0;
3802}
3803
Vivien Didelotfad09c72016-06-21 12:28:20 -04003804static struct mv88e6xxx_chip *mv88e6xxx_alloc_chip(struct device *dev)
Vivien Didelot469d7292016-06-20 13:14:06 -04003805{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003806 struct mv88e6xxx_chip *chip;
Vivien Didelot469d7292016-06-20 13:14:06 -04003807
Vivien Didelotfad09c72016-06-21 12:28:20 -04003808 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
3809 if (!chip)
Vivien Didelot469d7292016-06-20 13:14:06 -04003810 return NULL;
3811
Vivien Didelotfad09c72016-06-21 12:28:20 -04003812 chip->dev = dev;
Vivien Didelot469d7292016-06-20 13:14:06 -04003813
Vivien Didelotfad09c72016-06-21 12:28:20 -04003814 mutex_init(&chip->reg_lock);
Vivien Didelot469d7292016-06-20 13:14:06 -04003815
Vivien Didelotfad09c72016-06-21 12:28:20 -04003816 return chip;
Vivien Didelot469d7292016-06-20 13:14:06 -04003817}
3818
Vivien Didelote57e5e72016-08-15 17:19:00 -04003819static void mv88e6xxx_phy_init(struct mv88e6xxx_chip *chip)
3820{
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003821 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU))
Vivien Didelote57e5e72016-08-15 17:19:00 -04003822 mv88e6xxx_ppu_state_init(chip);
Vivien Didelote57e5e72016-08-15 17:19:00 -04003823}
3824
Andrew Lunn930188c2016-08-22 16:01:03 +02003825static void mv88e6xxx_phy_destroy(struct mv88e6xxx_chip *chip)
3826{
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003827 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU))
Andrew Lunn930188c2016-08-22 16:01:03 +02003828 mv88e6xxx_ppu_state_destroy(chip);
Andrew Lunn930188c2016-08-22 16:01:03 +02003829}
3830
Vivien Didelotfad09c72016-06-21 12:28:20 -04003831static int mv88e6xxx_smi_init(struct mv88e6xxx_chip *chip,
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003832 struct mii_bus *bus, int sw_addr)
3833{
3834 /* ADDR[0] pin is unavailable externally and considered zero */
3835 if (sw_addr & 0x1)
3836 return -EINVAL;
3837
Vivien Didelot914b32f2016-06-20 13:14:11 -04003838 if (sw_addr == 0)
Vivien Didelotfad09c72016-06-21 12:28:20 -04003839 chip->smi_ops = &mv88e6xxx_smi_single_chip_ops;
Vivien Didelota0ffff22016-08-15 17:18:58 -04003840 else if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_MULTI_CHIP))
Vivien Didelotfad09c72016-06-21 12:28:20 -04003841 chip->smi_ops = &mv88e6xxx_smi_multi_chip_ops;
Vivien Didelot914b32f2016-06-20 13:14:11 -04003842 else
3843 return -EINVAL;
3844
Vivien Didelotfad09c72016-06-21 12:28:20 -04003845 chip->bus = bus;
3846 chip->sw_addr = sw_addr;
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003847
3848 return 0;
3849}
3850
Andrew Lunn7b314362016-08-22 16:01:01 +02003851static enum dsa_tag_protocol mv88e6xxx_get_tag_protocol(struct dsa_switch *ds)
3852{
Vivien Didelot04bed142016-08-31 18:06:13 -04003853 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02003854
3855 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA))
3856 return DSA_TAG_PROTO_EDSA;
3857
3858 return DSA_TAG_PROTO_DSA;
Andrew Lunn7b314362016-08-22 16:01:01 +02003859}
3860
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02003861static const char *mv88e6xxx_drv_probe(struct device *dsa_dev,
3862 struct device *host_dev, int sw_addr,
3863 void **priv)
Andrew Lunna77d43f2016-04-13 02:40:42 +02003864{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003865 struct mv88e6xxx_chip *chip;
Vivien Didelota439c062016-04-17 13:23:58 -04003866 struct mii_bus *bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003867 int err;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003868
Vivien Didelota439c062016-04-17 13:23:58 -04003869 bus = dsa_host_dev_to_mii_bus(host_dev);
Andrew Lunnc1569132016-04-13 02:40:45 +02003870 if (!bus)
3871 return NULL;
3872
Vivien Didelotfad09c72016-06-21 12:28:20 -04003873 chip = mv88e6xxx_alloc_chip(dsa_dev);
3874 if (!chip)
Vivien Didelot469d7292016-06-20 13:14:06 -04003875 return NULL;
3876
Vivien Didelotcaac8542016-06-20 13:14:09 -04003877 /* Legacy SMI probing will only support chips similar to 88E6085 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003878 chip->info = &mv88e6xxx_table[MV88E6085];
Vivien Didelotcaac8542016-06-20 13:14:09 -04003879
Vivien Didelotfad09c72016-06-21 12:28:20 -04003880 err = mv88e6xxx_smi_init(chip, bus, sw_addr);
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003881 if (err)
3882 goto free;
3883
Vivien Didelotfad09c72016-06-21 12:28:20 -04003884 err = mv88e6xxx_detect(chip);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003885 if (err)
Vivien Didelot469d7292016-06-20 13:14:06 -04003886 goto free;
Vivien Didelota439c062016-04-17 13:23:58 -04003887
Andrew Lunndc30c352016-10-16 19:56:49 +02003888 mutex_lock(&chip->reg_lock);
3889 err = mv88e6xxx_switch_reset(chip);
3890 mutex_unlock(&chip->reg_lock);
3891 if (err)
3892 goto free;
3893
Vivien Didelote57e5e72016-08-15 17:19:00 -04003894 mv88e6xxx_phy_init(chip);
3895
Vivien Didelotfad09c72016-06-21 12:28:20 -04003896 err = mv88e6xxx_mdio_register(chip, NULL);
Andrew Lunnb516d452016-06-04 21:17:06 +02003897 if (err)
Vivien Didelot469d7292016-06-20 13:14:06 -04003898 goto free;
Andrew Lunnb516d452016-06-04 21:17:06 +02003899
Vivien Didelotfad09c72016-06-21 12:28:20 -04003900 *priv = chip;
Vivien Didelota439c062016-04-17 13:23:58 -04003901
Vivien Didelotfad09c72016-06-21 12:28:20 -04003902 return chip->info->name;
Vivien Didelot469d7292016-06-20 13:14:06 -04003903free:
Vivien Didelotfad09c72016-06-21 12:28:20 -04003904 devm_kfree(dsa_dev, chip);
Vivien Didelot469d7292016-06-20 13:14:06 -04003905
3906 return NULL;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003907}
3908
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003909static int mv88e6xxx_port_mdb_prepare(struct dsa_switch *ds, int port,
3910 const struct switchdev_obj_port_mdb *mdb,
3911 struct switchdev_trans *trans)
3912{
3913 /* We don't need any dynamic resource from the kernel (yet),
3914 * so skip the prepare phase.
3915 */
3916
3917 return 0;
3918}
3919
3920static void mv88e6xxx_port_mdb_add(struct dsa_switch *ds, int port,
3921 const struct switchdev_obj_port_mdb *mdb,
3922 struct switchdev_trans *trans)
3923{
Vivien Didelot04bed142016-08-31 18:06:13 -04003924 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003925
3926 mutex_lock(&chip->reg_lock);
3927 if (mv88e6xxx_port_db_load_purge(chip, port, mdb->addr, mdb->vid,
3928 GLOBAL_ATU_DATA_STATE_MC_STATIC))
3929 netdev_err(ds->ports[port].netdev, "failed to load multicast MAC address\n");
3930 mutex_unlock(&chip->reg_lock);
3931}
3932
3933static int mv88e6xxx_port_mdb_del(struct dsa_switch *ds, int port,
3934 const struct switchdev_obj_port_mdb *mdb)
3935{
Vivien Didelot04bed142016-08-31 18:06:13 -04003936 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003937 int err;
3938
3939 mutex_lock(&chip->reg_lock);
3940 err = mv88e6xxx_port_db_load_purge(chip, port, mdb->addr, mdb->vid,
3941 GLOBAL_ATU_DATA_STATE_UNUSED);
3942 mutex_unlock(&chip->reg_lock);
3943
3944 return err;
3945}
3946
3947static int mv88e6xxx_port_mdb_dump(struct dsa_switch *ds, int port,
3948 struct switchdev_obj_port_mdb *mdb,
3949 int (*cb)(struct switchdev_obj *obj))
3950{
Vivien Didelot04bed142016-08-31 18:06:13 -04003951 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003952 int err;
3953
3954 mutex_lock(&chip->reg_lock);
3955 err = mv88e6xxx_port_db_dump(chip, port, &mdb->obj, cb);
3956 mutex_unlock(&chip->reg_lock);
3957
3958 return err;
3959}
3960
Vivien Didelot9d490b42016-08-23 12:38:56 -04003961static struct dsa_switch_ops mv88e6xxx_switch_ops = {
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02003962 .probe = mv88e6xxx_drv_probe,
Andrew Lunn7b314362016-08-22 16:01:01 +02003963 .get_tag_protocol = mv88e6xxx_get_tag_protocol,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003964 .setup = mv88e6xxx_setup,
3965 .set_addr = mv88e6xxx_set_addr,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003966 .adjust_link = mv88e6xxx_adjust_link,
3967 .get_strings = mv88e6xxx_get_strings,
3968 .get_ethtool_stats = mv88e6xxx_get_ethtool_stats,
3969 .get_sset_count = mv88e6xxx_get_sset_count,
3970 .set_eee = mv88e6xxx_set_eee,
3971 .get_eee = mv88e6xxx_get_eee,
3972#ifdef CONFIG_NET_DSA_HWMON
3973 .get_temp = mv88e6xxx_get_temp,
3974 .get_temp_limit = mv88e6xxx_get_temp_limit,
3975 .set_temp_limit = mv88e6xxx_set_temp_limit,
3976 .get_temp_alarm = mv88e6xxx_get_temp_alarm,
3977#endif
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003978 .get_eeprom_len = mv88e6xxx_get_eeprom_len,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003979 .get_eeprom = mv88e6xxx_get_eeprom,
3980 .set_eeprom = mv88e6xxx_set_eeprom,
3981 .get_regs_len = mv88e6xxx_get_regs_len,
3982 .get_regs = mv88e6xxx_get_regs,
Vivien Didelot2cfcd962016-07-18 20:45:40 -04003983 .set_ageing_time = mv88e6xxx_set_ageing_time,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003984 .port_bridge_join = mv88e6xxx_port_bridge_join,
3985 .port_bridge_leave = mv88e6xxx_port_bridge_leave,
3986 .port_stp_state_set = mv88e6xxx_port_stp_state_set,
Vivien Didelot749efcb2016-09-22 16:49:24 -04003987 .port_fast_age = mv88e6xxx_port_fast_age,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003988 .port_vlan_filtering = mv88e6xxx_port_vlan_filtering,
3989 .port_vlan_prepare = mv88e6xxx_port_vlan_prepare,
3990 .port_vlan_add = mv88e6xxx_port_vlan_add,
3991 .port_vlan_del = mv88e6xxx_port_vlan_del,
3992 .port_vlan_dump = mv88e6xxx_port_vlan_dump,
3993 .port_fdb_prepare = mv88e6xxx_port_fdb_prepare,
3994 .port_fdb_add = mv88e6xxx_port_fdb_add,
3995 .port_fdb_del = mv88e6xxx_port_fdb_del,
3996 .port_fdb_dump = mv88e6xxx_port_fdb_dump,
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003997 .port_mdb_prepare = mv88e6xxx_port_mdb_prepare,
3998 .port_mdb_add = mv88e6xxx_port_mdb_add,
3999 .port_mdb_del = mv88e6xxx_port_mdb_del,
4000 .port_mdb_dump = mv88e6xxx_port_mdb_dump,
Vivien Didelotf81ec902016-05-09 13:22:58 -04004001};
4002
Vivien Didelotfad09c72016-06-21 12:28:20 -04004003static int mv88e6xxx_register_switch(struct mv88e6xxx_chip *chip,
Vivien Didelotb7e66a52016-06-20 13:14:02 -04004004 struct device_node *np)
4005{
Vivien Didelotfad09c72016-06-21 12:28:20 -04004006 struct device *dev = chip->dev;
Vivien Didelotb7e66a52016-06-20 13:14:02 -04004007 struct dsa_switch *ds;
4008
4009 ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
4010 if (!ds)
4011 return -ENOMEM;
4012
4013 ds->dev = dev;
Vivien Didelotfad09c72016-06-21 12:28:20 -04004014 ds->priv = chip;
Vivien Didelot9d490b42016-08-23 12:38:56 -04004015 ds->ops = &mv88e6xxx_switch_ops;
Vivien Didelotb7e66a52016-06-20 13:14:02 -04004016
4017 dev_set_drvdata(dev, ds);
4018
4019 return dsa_register_switch(ds, np);
4020}
4021
Vivien Didelotfad09c72016-06-21 12:28:20 -04004022static void mv88e6xxx_unregister_switch(struct mv88e6xxx_chip *chip)
Vivien Didelotb7e66a52016-06-20 13:14:02 -04004023{
Vivien Didelotfad09c72016-06-21 12:28:20 -04004024 dsa_unregister_switch(chip->ds);
Vivien Didelotb7e66a52016-06-20 13:14:02 -04004025}
4026
Vivien Didelot57d32312016-06-20 13:13:58 -04004027static int mv88e6xxx_probe(struct mdio_device *mdiodev)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004028{
4029 struct device *dev = &mdiodev->dev;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02004030 struct device_node *np = dev->of_node;
Vivien Didelotcaac8542016-06-20 13:14:09 -04004031 const struct mv88e6xxx_info *compat_info;
Vivien Didelotfad09c72016-06-21 12:28:20 -04004032 struct mv88e6xxx_chip *chip;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02004033 u32 eeprom_len;
Andrew Lunn52638f72016-05-10 23:27:22 +02004034 int err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004035
Vivien Didelotcaac8542016-06-20 13:14:09 -04004036 compat_info = of_device_get_match_data(dev);
4037 if (!compat_info)
4038 return -EINVAL;
4039
Vivien Didelotfad09c72016-06-21 12:28:20 -04004040 chip = mv88e6xxx_alloc_chip(dev);
4041 if (!chip)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004042 return -ENOMEM;
4043
Vivien Didelotfad09c72016-06-21 12:28:20 -04004044 chip->info = compat_info;
Vivien Didelotcaac8542016-06-20 13:14:09 -04004045
Vivien Didelotfad09c72016-06-21 12:28:20 -04004046 err = mv88e6xxx_smi_init(chip, mdiodev->bus, mdiodev->addr);
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04004047 if (err)
4048 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004049
Andrew Lunnb4308f02016-11-21 23:26:55 +01004050 chip->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
4051 if (IS_ERR(chip->reset))
4052 return PTR_ERR(chip->reset);
4053
Vivien Didelotfad09c72016-06-21 12:28:20 -04004054 err = mv88e6xxx_detect(chip);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04004055 if (err)
4056 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004057
Vivien Didelote57e5e72016-08-15 17:19:00 -04004058 mv88e6xxx_phy_init(chip);
4059
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04004060 if (chip->info->ops->get_eeprom &&
Andrew Lunnf8cd8752016-05-10 23:27:25 +02004061 !of_property_read_u32(np, "eeprom-length", &eeprom_len))
Vivien Didelotfad09c72016-06-21 12:28:20 -04004062 chip->eeprom_len = eeprom_len;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02004063
Andrew Lunndc30c352016-10-16 19:56:49 +02004064 mutex_lock(&chip->reg_lock);
4065 err = mv88e6xxx_switch_reset(chip);
4066 mutex_unlock(&chip->reg_lock);
Andrew Lunnb516d452016-06-04 21:17:06 +02004067 if (err)
Andrew Lunndc30c352016-10-16 19:56:49 +02004068 goto out;
Andrew Lunnb516d452016-06-04 21:17:06 +02004069
Andrew Lunndc30c352016-10-16 19:56:49 +02004070 chip->irq = of_irq_get(np, 0);
4071 if (chip->irq == -EPROBE_DEFER) {
4072 err = chip->irq;
4073 goto out;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02004074 }
4075
Andrew Lunndc30c352016-10-16 19:56:49 +02004076 if (chip->irq > 0) {
4077 /* Has to be performed before the MDIO bus is created,
4078 * because the PHYs will link there interrupts to these
4079 * interrupt controllers
4080 */
4081 mutex_lock(&chip->reg_lock);
4082 err = mv88e6xxx_g1_irq_setup(chip);
4083 mutex_unlock(&chip->reg_lock);
4084
4085 if (err)
4086 goto out;
4087
4088 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_INT)) {
4089 err = mv88e6xxx_g2_irq_setup(chip);
4090 if (err)
4091 goto out_g1_irq;
4092 }
4093 }
4094
4095 err = mv88e6xxx_mdio_register(chip, np);
4096 if (err)
4097 goto out_g2_irq;
4098
4099 err = mv88e6xxx_register_switch(chip, np);
4100 if (err)
4101 goto out_mdio;
4102
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004103 return 0;
Andrew Lunndc30c352016-10-16 19:56:49 +02004104
4105out_mdio:
4106 mv88e6xxx_mdio_unregister(chip);
4107out_g2_irq:
Andrew Lunn467126442016-11-20 20:14:15 +01004108 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_INT) && chip->irq > 0)
Andrew Lunndc30c352016-10-16 19:56:49 +02004109 mv88e6xxx_g2_irq_free(chip);
4110out_g1_irq:
Andrew Lunn61f7c3f2016-11-20 20:14:19 +01004111 if (chip->irq > 0) {
4112 mutex_lock(&chip->reg_lock);
Andrew Lunn467126442016-11-20 20:14:15 +01004113 mv88e6xxx_g1_irq_free(chip);
Andrew Lunn61f7c3f2016-11-20 20:14:19 +01004114 mutex_unlock(&chip->reg_lock);
4115 }
Andrew Lunndc30c352016-10-16 19:56:49 +02004116out:
4117 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004118}
4119
4120static void mv88e6xxx_remove(struct mdio_device *mdiodev)
4121{
4122 struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev);
Vivien Didelot04bed142016-08-31 18:06:13 -04004123 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004124
Andrew Lunn930188c2016-08-22 16:01:03 +02004125 mv88e6xxx_phy_destroy(chip);
Vivien Didelotfad09c72016-06-21 12:28:20 -04004126 mv88e6xxx_unregister_switch(chip);
4127 mv88e6xxx_mdio_unregister(chip);
Andrew Lunndc30c352016-10-16 19:56:49 +02004128
Andrew Lunn467126442016-11-20 20:14:15 +01004129 if (chip->irq > 0) {
4130 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_INT))
4131 mv88e6xxx_g2_irq_free(chip);
4132 mv88e6xxx_g1_irq_free(chip);
4133 }
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004134}
4135
4136static const struct of_device_id mv88e6xxx_of_match[] = {
Vivien Didelotcaac8542016-06-20 13:14:09 -04004137 {
4138 .compatible = "marvell,mv88e6085",
4139 .data = &mv88e6xxx_table[MV88E6085],
4140 },
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01004141 {
4142 .compatible = "marvell,mv88e6190",
4143 .data = &mv88e6xxx_table[MV88E6190],
4144 },
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004145 { /* sentinel */ },
4146};
4147
4148MODULE_DEVICE_TABLE(of, mv88e6xxx_of_match);
4149
4150static struct mdio_driver mv88e6xxx_driver = {
4151 .probe = mv88e6xxx_probe,
4152 .remove = mv88e6xxx_remove,
4153 .mdiodrv.driver = {
4154 .name = "mv88e6085",
4155 .of_match_table = mv88e6xxx_of_match,
4156 },
4157};
4158
Ben Hutchings98e67302011-11-25 14:36:19 +00004159static int __init mv88e6xxx_init(void)
4160{
Vivien Didelot9d490b42016-08-23 12:38:56 -04004161 register_switch_driver(&mv88e6xxx_switch_ops);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004162 return mdio_driver_register(&mv88e6xxx_driver);
Ben Hutchings98e67302011-11-25 14:36:19 +00004163}
4164module_init(mv88e6xxx_init);
4165
4166static void __exit mv88e6xxx_cleanup(void)
4167{
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004168 mdio_driver_unregister(&mv88e6xxx_driver);
Vivien Didelot9d490b42016-08-23 12:38:56 -04004169 unregister_switch_driver(&mv88e6xxx_switch_ops);
Ben Hutchings98e67302011-11-25 14:36:19 +00004170}
4171module_exit(mv88e6xxx_cleanup);
Ben Hutchings3d825ed2011-11-25 14:37:16 +00004172
4173MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
4174MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
4175MODULE_LICENSE("GPL");