blob: 062b9128a58d772ecc1b72daeaca2a73e89af570 [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);
790 if ((val & GLOBAL_STATS_OP_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000791 return 0;
792 }
793
794 return -ETIMEDOUT;
795}
796
Vivien Didelotfad09c72016-06-21 12:28:20 -0400797static int _mv88e6xxx_stats_snapshot(struct mv88e6xxx_chip *chip, int port)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000798{
Vivien Didelota935c052016-09-29 12:21:53 -0400799 int err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000800
Vivien Didelotfad09c72016-06-21 12:28:20 -0400801 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200802 port = (port + 1) << 5;
803
Barry Grussling3675c8d2013-01-08 16:05:53 +0000804 /* Snapshot the hardware statistics counters for this port. */
Vivien Didelota935c052016-09-29 12:21:53 -0400805 err = mv88e6xxx_g1_write(chip, GLOBAL_STATS_OP,
806 GLOBAL_STATS_OP_CAPTURE_PORT |
807 GLOBAL_STATS_OP_HIST_RX_TX | port);
808 if (err)
809 return err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000810
Barry Grussling3675c8d2013-01-08 16:05:53 +0000811 /* Wait for the snapshotting to complete. */
Vivien Didelota935c052016-09-29 12:21:53 -0400812 return _mv88e6xxx_stats_wait(chip);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000813}
814
Vivien Didelotfad09c72016-06-21 12:28:20 -0400815static void _mv88e6xxx_stats_read(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -0400816 int stat, u32 *val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000817{
Vivien Didelota935c052016-09-29 12:21:53 -0400818 u32 value;
819 u16 reg;
820 int err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000821
822 *val = 0;
823
Vivien Didelota935c052016-09-29 12:21:53 -0400824 err = mv88e6xxx_g1_write(chip, GLOBAL_STATS_OP,
825 GLOBAL_STATS_OP_READ_CAPTURED |
826 GLOBAL_STATS_OP_HIST_RX_TX | stat);
827 if (err)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000828 return;
829
Vivien Didelota935c052016-09-29 12:21:53 -0400830 err = _mv88e6xxx_stats_wait(chip);
831 if (err)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000832 return;
833
Vivien Didelota935c052016-09-29 12:21:53 -0400834 err = mv88e6xxx_g1_read(chip, GLOBAL_STATS_COUNTER_32, &reg);
835 if (err)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000836 return;
837
Vivien Didelota935c052016-09-29 12:21:53 -0400838 value = reg << 16;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000839
Vivien Didelota935c052016-09-29 12:21:53 -0400840 err = mv88e6xxx_g1_read(chip, GLOBAL_STATS_COUNTER_01, &reg);
841 if (err)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000842 return;
843
Vivien Didelota935c052016-09-29 12:21:53 -0400844 *val = value | reg;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000845}
846
Andrew Lunne413e7e2015-04-02 04:06:38 +0200847static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100848 { "in_good_octets", 8, 0x00, BANK0, },
849 { "in_bad_octets", 4, 0x02, BANK0, },
850 { "in_unicast", 4, 0x04, BANK0, },
851 { "in_broadcasts", 4, 0x06, BANK0, },
852 { "in_multicasts", 4, 0x07, BANK0, },
853 { "in_pause", 4, 0x16, BANK0, },
854 { "in_undersize", 4, 0x18, BANK0, },
855 { "in_fragments", 4, 0x19, BANK0, },
856 { "in_oversize", 4, 0x1a, BANK0, },
857 { "in_jabber", 4, 0x1b, BANK0, },
858 { "in_rx_error", 4, 0x1c, BANK0, },
859 { "in_fcs_error", 4, 0x1d, BANK0, },
860 { "out_octets", 8, 0x0e, BANK0, },
861 { "out_unicast", 4, 0x10, BANK0, },
862 { "out_broadcasts", 4, 0x13, BANK0, },
863 { "out_multicasts", 4, 0x12, BANK0, },
864 { "out_pause", 4, 0x15, BANK0, },
865 { "excessive", 4, 0x11, BANK0, },
866 { "collisions", 4, 0x1e, BANK0, },
867 { "deferred", 4, 0x05, BANK0, },
868 { "single", 4, 0x14, BANK0, },
869 { "multiple", 4, 0x17, BANK0, },
870 { "out_fcs_error", 4, 0x03, BANK0, },
871 { "late", 4, 0x1f, BANK0, },
872 { "hist_64bytes", 4, 0x08, BANK0, },
873 { "hist_65_127bytes", 4, 0x09, BANK0, },
874 { "hist_128_255bytes", 4, 0x0a, BANK0, },
875 { "hist_256_511bytes", 4, 0x0b, BANK0, },
876 { "hist_512_1023bytes", 4, 0x0c, BANK0, },
877 { "hist_1024_max_bytes", 4, 0x0d, BANK0, },
878 { "sw_in_discards", 4, 0x10, PORT, },
879 { "sw_in_filtered", 2, 0x12, PORT, },
880 { "sw_out_filtered", 2, 0x13, PORT, },
881 { "in_discards", 4, 0x00 | GLOBAL_STATS_OP_BANK_1, BANK1, },
882 { "in_filtered", 4, 0x01 | GLOBAL_STATS_OP_BANK_1, BANK1, },
883 { "in_accepted", 4, 0x02 | GLOBAL_STATS_OP_BANK_1, BANK1, },
884 { "in_bad_accepted", 4, 0x03 | GLOBAL_STATS_OP_BANK_1, BANK1, },
885 { "in_good_avb_class_a", 4, 0x04 | GLOBAL_STATS_OP_BANK_1, BANK1, },
886 { "in_good_avb_class_b", 4, 0x05 | GLOBAL_STATS_OP_BANK_1, BANK1, },
887 { "in_bad_avb_class_a", 4, 0x06 | GLOBAL_STATS_OP_BANK_1, BANK1, },
888 { "in_bad_avb_class_b", 4, 0x07 | GLOBAL_STATS_OP_BANK_1, BANK1, },
889 { "tcam_counter_0", 4, 0x08 | GLOBAL_STATS_OP_BANK_1, BANK1, },
890 { "tcam_counter_1", 4, 0x09 | GLOBAL_STATS_OP_BANK_1, BANK1, },
891 { "tcam_counter_2", 4, 0x0a | GLOBAL_STATS_OP_BANK_1, BANK1, },
892 { "tcam_counter_3", 4, 0x0b | GLOBAL_STATS_OP_BANK_1, BANK1, },
893 { "in_da_unknown", 4, 0x0e | GLOBAL_STATS_OP_BANK_1, BANK1, },
894 { "in_management", 4, 0x0f | GLOBAL_STATS_OP_BANK_1, BANK1, },
895 { "out_queue_0", 4, 0x10 | GLOBAL_STATS_OP_BANK_1, BANK1, },
896 { "out_queue_1", 4, 0x11 | GLOBAL_STATS_OP_BANK_1, BANK1, },
897 { "out_queue_2", 4, 0x12 | GLOBAL_STATS_OP_BANK_1, BANK1, },
898 { "out_queue_3", 4, 0x13 | GLOBAL_STATS_OP_BANK_1, BANK1, },
899 { "out_queue_4", 4, 0x14 | GLOBAL_STATS_OP_BANK_1, BANK1, },
900 { "out_queue_5", 4, 0x15 | GLOBAL_STATS_OP_BANK_1, BANK1, },
901 { "out_queue_6", 4, 0x16 | GLOBAL_STATS_OP_BANK_1, BANK1, },
902 { "out_queue_7", 4, 0x17 | GLOBAL_STATS_OP_BANK_1, BANK1, },
903 { "out_cut_through", 4, 0x18 | GLOBAL_STATS_OP_BANK_1, BANK1, },
904 { "out_octets_a", 4, 0x1a | GLOBAL_STATS_OP_BANK_1, BANK1, },
905 { "out_octets_b", 4, 0x1b | GLOBAL_STATS_OP_BANK_1, BANK1, },
906 { "out_management", 4, 0x1f | GLOBAL_STATS_OP_BANK_1, BANK1, },
Andrew Lunne413e7e2015-04-02 04:06:38 +0200907};
908
Vivien Didelotfad09c72016-06-21 12:28:20 -0400909static bool mv88e6xxx_has_stat(struct mv88e6xxx_chip *chip,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100910 struct mv88e6xxx_hw_stat *stat)
Andrew Lunne413e7e2015-04-02 04:06:38 +0200911{
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100912 switch (stat->type) {
913 case BANK0:
Andrew Lunne413e7e2015-04-02 04:06:38 +0200914 return true;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100915 case BANK1:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400916 return mv88e6xxx_6320_family(chip);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100917 case PORT:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400918 return mv88e6xxx_6095_family(chip) ||
919 mv88e6xxx_6185_family(chip) ||
920 mv88e6xxx_6097_family(chip) ||
921 mv88e6xxx_6165_family(chip) ||
922 mv88e6xxx_6351_family(chip) ||
923 mv88e6xxx_6352_family(chip);
Andrew Lunne413e7e2015-04-02 04:06:38 +0200924 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100925 return false;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000926}
927
Vivien Didelotfad09c72016-06-21 12:28:20 -0400928static uint64_t _mv88e6xxx_get_ethtool_stat(struct mv88e6xxx_chip *chip,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100929 struct mv88e6xxx_hw_stat *s,
Andrew Lunn80c46272015-06-20 18:42:30 +0200930 int port)
931{
Andrew Lunn80c46272015-06-20 18:42:30 +0200932 u32 low;
933 u32 high = 0;
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200934 int err;
935 u16 reg;
Andrew Lunn80c46272015-06-20 18:42:30 +0200936 u64 value;
937
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100938 switch (s->type) {
939 case PORT:
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200940 err = mv88e6xxx_port_read(chip, port, s->reg, &reg);
941 if (err)
Andrew Lunn80c46272015-06-20 18:42:30 +0200942 return UINT64_MAX;
943
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200944 low = reg;
Andrew Lunn80c46272015-06-20 18:42:30 +0200945 if (s->sizeof_stat == 4) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200946 err = mv88e6xxx_port_read(chip, port, s->reg + 1, &reg);
947 if (err)
Andrew Lunn80c46272015-06-20 18:42:30 +0200948 return UINT64_MAX;
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200949 high = reg;
Andrew Lunn80c46272015-06-20 18:42:30 +0200950 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100951 break;
952 case BANK0:
953 case BANK1:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400954 _mv88e6xxx_stats_read(chip, s->reg, &low);
Andrew Lunn80c46272015-06-20 18:42:30 +0200955 if (s->sizeof_stat == 8)
Vivien Didelotfad09c72016-06-21 12:28:20 -0400956 _mv88e6xxx_stats_read(chip, s->reg + 1, &high);
Andrew Lunn80c46272015-06-20 18:42:30 +0200957 }
958 value = (((u64)high) << 16) | low;
959 return value;
960}
961
Vivien Didelotf81ec902016-05-09 13:22:58 -0400962static void mv88e6xxx_get_strings(struct dsa_switch *ds, int port,
963 uint8_t *data)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100964{
Vivien Didelot04bed142016-08-31 18:06:13 -0400965 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100966 struct mv88e6xxx_hw_stat *stat;
967 int i, j;
968
969 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
970 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400971 if (mv88e6xxx_has_stat(chip, stat)) {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100972 memcpy(data + j * ETH_GSTRING_LEN, stat->string,
973 ETH_GSTRING_LEN);
974 j++;
975 }
976 }
977}
978
Vivien Didelotf81ec902016-05-09 13:22:58 -0400979static int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100980{
Vivien Didelot04bed142016-08-31 18:06:13 -0400981 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100982 struct mv88e6xxx_hw_stat *stat;
983 int i, j;
984
985 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
986 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400987 if (mv88e6xxx_has_stat(chip, stat))
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100988 j++;
989 }
990 return j;
991}
992
Vivien Didelotf81ec902016-05-09 13:22:58 -0400993static void mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, int port,
994 uint64_t *data)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000995{
Vivien Didelot04bed142016-08-31 18:06:13 -0400996 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100997 struct mv88e6xxx_hw_stat *stat;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000998 int ret;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100999 int i, j;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001000
Vivien Didelotfad09c72016-06-21 12:28:20 -04001001 mutex_lock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001002
Vivien Didelotfad09c72016-06-21 12:28:20 -04001003 ret = _mv88e6xxx_stats_snapshot(chip, port);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001004 if (ret < 0) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001005 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001006 return;
1007 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +01001008 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
1009 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -04001010 if (mv88e6xxx_has_stat(chip, stat)) {
1011 data[j] = _mv88e6xxx_get_ethtool_stat(chip, stat, port);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +01001012 j++;
1013 }
1014 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001015
Vivien Didelotfad09c72016-06-21 12:28:20 -04001016 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001017}
Ben Hutchings98e67302011-11-25 14:36:19 +00001018
Vivien Didelotf81ec902016-05-09 13:22:58 -04001019static int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001020{
1021 return 32 * sizeof(u16);
1022}
1023
Vivien Didelotf81ec902016-05-09 13:22:58 -04001024static void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
1025 struct ethtool_regs *regs, void *_p)
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001026{
Vivien Didelot04bed142016-08-31 18:06:13 -04001027 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001028 int err;
1029 u16 reg;
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001030 u16 *p = _p;
1031 int i;
1032
1033 regs->version = 0;
1034
1035 memset(p, 0xff, 32 * sizeof(u16));
1036
Vivien Didelotfad09c72016-06-21 12:28:20 -04001037 mutex_lock(&chip->reg_lock);
Vivien Didelot23062512016-05-09 13:22:45 -04001038
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001039 for (i = 0; i < 32; i++) {
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001040
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001041 err = mv88e6xxx_port_read(chip, port, i, &reg);
1042 if (!err)
1043 p[i] = reg;
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001044 }
Vivien Didelot23062512016-05-09 13:22:45 -04001045
Vivien Didelotfad09c72016-06-21 12:28:20 -04001046 mutex_unlock(&chip->reg_lock);
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001047}
1048
Vivien Didelotfad09c72016-06-21 12:28:20 -04001049static int _mv88e6xxx_atu_wait(struct mv88e6xxx_chip *chip)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001050{
Vivien Didelota935c052016-09-29 12:21:53 -04001051 return mv88e6xxx_g1_wait(chip, GLOBAL_ATU_OP, GLOBAL_ATU_OP_BUSY);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001052}
1053
Vivien Didelotf81ec902016-05-09 13:22:58 -04001054static int mv88e6xxx_get_eee(struct dsa_switch *ds, int port,
1055 struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -08001056{
Vivien Didelot04bed142016-08-31 18:06:13 -04001057 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot9c938292016-08-15 17:19:02 -04001058 u16 reg;
1059 int err;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001060
Vivien Didelotfad09c72016-06-21 12:28:20 -04001061 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
Vivien Didelotaadbdb82016-05-09 13:22:44 -04001062 return -EOPNOTSUPP;
1063
Vivien Didelotfad09c72016-06-21 12:28:20 -04001064 mutex_lock(&chip->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001065
Vivien Didelot9c938292016-08-15 17:19:02 -04001066 err = mv88e6xxx_phy_read(chip, port, 16, &reg);
1067 if (err)
Andrew Lunn2f40c692015-04-02 04:06:37 +02001068 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001069
1070 e->eee_enabled = !!(reg & 0x0200);
1071 e->tx_lpi_enabled = !!(reg & 0x0100);
1072
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001073 err = mv88e6xxx_port_read(chip, port, PORT_STATUS, &reg);
Vivien Didelot9c938292016-08-15 17:19:02 -04001074 if (err)
Andrew Lunn2f40c692015-04-02 04:06:37 +02001075 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001076
Andrew Lunncca8b132015-04-02 04:06:39 +02001077 e->eee_active = !!(reg & PORT_STATUS_EEE);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001078out:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001079 mutex_unlock(&chip->reg_lock);
Vivien Didelot9c938292016-08-15 17:19:02 -04001080
1081 return err;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001082}
1083
Vivien Didelotf81ec902016-05-09 13:22:58 -04001084static int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
1085 struct phy_device *phydev, struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -08001086{
Vivien Didelot04bed142016-08-31 18:06:13 -04001087 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot9c938292016-08-15 17:19:02 -04001088 u16 reg;
1089 int err;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001090
Vivien Didelotfad09c72016-06-21 12:28:20 -04001091 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
Vivien Didelotaadbdb82016-05-09 13:22:44 -04001092 return -EOPNOTSUPP;
1093
Vivien Didelotfad09c72016-06-21 12:28:20 -04001094 mutex_lock(&chip->reg_lock);
Guenter Roeck11b3b452015-03-06 22:23:51 -08001095
Vivien Didelot9c938292016-08-15 17:19:02 -04001096 err = mv88e6xxx_phy_read(chip, port, 16, &reg);
1097 if (err)
Andrew Lunn2f40c692015-04-02 04:06:37 +02001098 goto out;
1099
Vivien Didelot9c938292016-08-15 17:19:02 -04001100 reg &= ~0x0300;
Andrew Lunn2f40c692015-04-02 04:06:37 +02001101 if (e->eee_enabled)
1102 reg |= 0x0200;
1103 if (e->tx_lpi_enabled)
1104 reg |= 0x0100;
1105
Vivien Didelot9c938292016-08-15 17:19:02 -04001106 err = mv88e6xxx_phy_write(chip, port, 16, reg);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001107out:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001108 mutex_unlock(&chip->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001109
Vivien Didelot9c938292016-08-15 17:19:02 -04001110 return err;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001111}
1112
Vivien Didelotfad09c72016-06-21 12:28:20 -04001113static int _mv88e6xxx_atu_cmd(struct mv88e6xxx_chip *chip, u16 fid, u16 cmd)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001114{
Vivien Didelota935c052016-09-29 12:21:53 -04001115 u16 val;
1116 int err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001117
Vivien Didelot6dc10bb2016-09-29 12:21:55 -04001118 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G1_ATU_FID)) {
Vivien Didelota935c052016-09-29 12:21:53 -04001119 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_FID, fid);
1120 if (err)
1121 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001122 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001123 /* ATU DBNum[7:4] are located in ATU Control 15:12 */
Vivien Didelota935c052016-09-29 12:21:53 -04001124 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_CONTROL, &val);
1125 if (err)
1126 return err;
Vivien Didelot11ea8092016-03-31 16:53:44 -04001127
Vivien Didelota935c052016-09-29 12:21:53 -04001128 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_CONTROL,
1129 (val & 0xfff) | ((fid << 8) & 0xf000));
1130 if (err)
1131 return err;
Vivien Didelot11ea8092016-03-31 16:53:44 -04001132
1133 /* ATU DBNum[3:0] are located in ATU Operation 3:0 */
1134 cmd |= fid & 0xf;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001135 }
1136
Vivien Didelota935c052016-09-29 12:21:53 -04001137 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_OP, cmd);
1138 if (err)
1139 return err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001140
Vivien Didelotfad09c72016-06-21 12:28:20 -04001141 return _mv88e6xxx_atu_wait(chip);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001142}
1143
Vivien Didelotfad09c72016-06-21 12:28:20 -04001144static int _mv88e6xxx_atu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot37705b72015-09-04 14:34:11 -04001145 struct mv88e6xxx_atu_entry *entry)
1146{
1147 u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK;
1148
1149 if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) {
1150 unsigned int mask, shift;
1151
1152 if (entry->trunk) {
1153 data |= GLOBAL_ATU_DATA_TRUNK;
1154 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
1155 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
1156 } else {
1157 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
1158 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
1159 }
1160
1161 data |= (entry->portv_trunkid << shift) & mask;
1162 }
1163
Vivien Didelota935c052016-09-29 12:21:53 -04001164 return mv88e6xxx_g1_write(chip, GLOBAL_ATU_DATA, data);
Vivien Didelot37705b72015-09-04 14:34:11 -04001165}
1166
Vivien Didelotfad09c72016-06-21 12:28:20 -04001167static int _mv88e6xxx_atu_flush_move(struct mv88e6xxx_chip *chip,
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001168 struct mv88e6xxx_atu_entry *entry,
1169 bool static_too)
1170{
1171 int op;
1172 int err;
1173
Vivien Didelotfad09c72016-06-21 12:28:20 -04001174 err = _mv88e6xxx_atu_wait(chip);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001175 if (err)
1176 return err;
1177
Vivien Didelotfad09c72016-06-21 12:28:20 -04001178 err = _mv88e6xxx_atu_data_write(chip, entry);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001179 if (err)
1180 return err;
1181
1182 if (entry->fid) {
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001183 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB :
1184 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB;
1185 } else {
1186 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL :
1187 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC;
1188 }
1189
Vivien Didelotfad09c72016-06-21 12:28:20 -04001190 return _mv88e6xxx_atu_cmd(chip, entry->fid, op);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001191}
1192
Vivien Didelotfad09c72016-06-21 12:28:20 -04001193static int _mv88e6xxx_atu_flush(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001194 u16 fid, bool static_too)
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001195{
1196 struct mv88e6xxx_atu_entry entry = {
1197 .fid = fid,
1198 .state = 0, /* EntryState bits must be 0 */
1199 };
1200
Vivien Didelotfad09c72016-06-21 12:28:20 -04001201 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001202}
1203
Vivien Didelotfad09c72016-06-21 12:28:20 -04001204static int _mv88e6xxx_atu_move(struct mv88e6xxx_chip *chip, u16 fid,
Andrew Lunn158bc062016-04-28 21:24:06 -04001205 int from_port, int to_port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001206{
1207 struct mv88e6xxx_atu_entry entry = {
1208 .trunk = false,
1209 .fid = fid,
1210 };
1211
1212 /* EntryState bits must be 0xF */
1213 entry.state = GLOBAL_ATU_DATA_STATE_MASK;
1214
1215 /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */
1216 entry.portv_trunkid = (to_port & 0x0f) << 4;
1217 entry.portv_trunkid |= from_port & 0x0f;
1218
Vivien Didelotfad09c72016-06-21 12:28:20 -04001219 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001220}
1221
Vivien Didelotfad09c72016-06-21 12:28:20 -04001222static int _mv88e6xxx_atu_remove(struct mv88e6xxx_chip *chip, u16 fid,
Andrew Lunn158bc062016-04-28 21:24:06 -04001223 int port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001224{
1225 /* Destination port 0xF means remove the entries */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001226 return _mv88e6xxx_atu_move(chip, fid, port, 0x0f, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001227}
1228
Vivien Didelotfad09c72016-06-21 12:28:20 -04001229static int _mv88e6xxx_port_based_vlan_map(struct mv88e6xxx_chip *chip, int port)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001230{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001231 struct net_device *bridge = chip->ports[port].bridge_dev;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001232 struct dsa_switch *ds = chip->ds;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001233 u16 output_ports = 0;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001234 int i;
1235
1236 /* allow CPU port or DSA link(s) to send frames to every port */
1237 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
Vivien Didelot5a7921f2016-11-04 03:23:28 +01001238 output_ports = ~0;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001239 } else {
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001240 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001241 /* allow sending frames to every group member */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001242 if (bridge && chip->ports[i].bridge_dev == bridge)
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001243 output_ports |= BIT(i);
1244
1245 /* allow sending frames to CPU port and DSA link(s) */
1246 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
1247 output_ports |= BIT(i);
1248 }
1249 }
1250
1251 /* prevent frames from going back out of the port they came in on */
1252 output_ports &= ~BIT(port);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001253
Vivien Didelot5a7921f2016-11-04 03:23:28 +01001254 return mv88e6xxx_port_set_vlan_map(chip, port, output_ports);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001255}
1256
Vivien Didelotf81ec902016-05-09 13:22:58 -04001257static void mv88e6xxx_port_stp_state_set(struct dsa_switch *ds, int port,
1258 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001259{
Vivien Didelot04bed142016-08-31 18:06:13 -04001260 struct mv88e6xxx_chip *chip = ds->priv;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001261 int stp_state;
Vivien Didelot553eb542016-05-13 20:38:23 -04001262 int err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001263
1264 switch (state) {
1265 case BR_STATE_DISABLED:
Andrew Lunncca8b132015-04-02 04:06:39 +02001266 stp_state = PORT_CONTROL_STATE_DISABLED;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001267 break;
1268 case BR_STATE_BLOCKING:
1269 case BR_STATE_LISTENING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001270 stp_state = PORT_CONTROL_STATE_BLOCKING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001271 break;
1272 case BR_STATE_LEARNING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001273 stp_state = PORT_CONTROL_STATE_LEARNING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001274 break;
1275 case BR_STATE_FORWARDING:
1276 default:
Andrew Lunncca8b132015-04-02 04:06:39 +02001277 stp_state = PORT_CONTROL_STATE_FORWARDING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001278 break;
1279 }
1280
Vivien Didelotfad09c72016-06-21 12:28:20 -04001281 mutex_lock(&chip->reg_lock);
Vivien Didelote28def332016-11-04 03:23:27 +01001282 err = mv88e6xxx_port_set_state(chip, port, stp_state);
Vivien Didelotfad09c72016-06-21 12:28:20 -04001283 mutex_unlock(&chip->reg_lock);
Vivien Didelot553eb542016-05-13 20:38:23 -04001284
1285 if (err)
Vivien Didelote28def332016-11-04 03:23:27 +01001286 netdev_err(ds->ports[port].netdev, "failed to update state\n");
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001287}
1288
Vivien Didelot749efcb2016-09-22 16:49:24 -04001289static void mv88e6xxx_port_fast_age(struct dsa_switch *ds, int port)
1290{
1291 struct mv88e6xxx_chip *chip = ds->priv;
1292 int err;
1293
1294 mutex_lock(&chip->reg_lock);
1295 err = _mv88e6xxx_atu_remove(chip, 0, port, false);
1296 mutex_unlock(&chip->reg_lock);
1297
1298 if (err)
1299 netdev_err(ds->ports[port].netdev, "failed to flush ATU\n");
1300}
1301
Vivien Didelotfad09c72016-06-21 12:28:20 -04001302static int _mv88e6xxx_vtu_wait(struct mv88e6xxx_chip *chip)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001303{
Vivien Didelota935c052016-09-29 12:21:53 -04001304 return mv88e6xxx_g1_wait(chip, GLOBAL_VTU_OP, GLOBAL_VTU_OP_BUSY);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001305}
1306
Vivien Didelotfad09c72016-06-21 12:28:20 -04001307static int _mv88e6xxx_vtu_cmd(struct mv88e6xxx_chip *chip, u16 op)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001308{
Vivien Didelota935c052016-09-29 12:21:53 -04001309 int err;
Vivien Didelot6b17e862015-08-13 12:52:18 -04001310
Vivien Didelota935c052016-09-29 12:21:53 -04001311 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_OP, op);
1312 if (err)
1313 return err;
Vivien Didelot6b17e862015-08-13 12:52:18 -04001314
Vivien Didelotfad09c72016-06-21 12:28:20 -04001315 return _mv88e6xxx_vtu_wait(chip);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001316}
1317
Vivien Didelotfad09c72016-06-21 12:28:20 -04001318static int _mv88e6xxx_vtu_stu_flush(struct mv88e6xxx_chip *chip)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001319{
1320 int ret;
1321
Vivien Didelotfad09c72016-06-21 12:28:20 -04001322 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001323 if (ret < 0)
1324 return ret;
1325
Vivien Didelotfad09c72016-06-21 12:28:20 -04001326 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_FLUSH_ALL);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001327}
1328
Vivien Didelotfad09c72016-06-21 12:28:20 -04001329static int _mv88e6xxx_vtu_stu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001330 struct mv88e6xxx_vtu_entry *entry,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001331 unsigned int nibble_offset)
1332{
Vivien Didelotb8fee952015-08-13 12:52:19 -04001333 u16 regs[3];
Vivien Didelota935c052016-09-29 12:21:53 -04001334 int i, err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001335
1336 for (i = 0; i < 3; ++i) {
Vivien Didelota935c052016-09-29 12:21:53 -04001337 u16 *reg = &regs[i];
Vivien Didelotb8fee952015-08-13 12:52:19 -04001338
Vivien Didelota935c052016-09-29 12:21:53 -04001339 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_DATA_0_3 + i, reg);
1340 if (err)
1341 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001342 }
1343
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001344 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotb8fee952015-08-13 12:52:19 -04001345 unsigned int shift = (i % 4) * 4 + nibble_offset;
1346 u16 reg = regs[i / 4];
1347
1348 entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
1349 }
1350
1351 return 0;
1352}
1353
Vivien Didelotfad09c72016-06-21 12:28:20 -04001354static int mv88e6xxx_vtu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001355 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001356{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001357 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 0);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001358}
1359
Vivien Didelotfad09c72016-06-21 12:28:20 -04001360static int mv88e6xxx_stu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001361 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001362{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001363 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 2);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001364}
1365
Vivien Didelotfad09c72016-06-21 12:28:20 -04001366static int _mv88e6xxx_vtu_stu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001367 struct mv88e6xxx_vtu_entry *entry,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001368 unsigned int nibble_offset)
1369{
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001370 u16 regs[3] = { 0 };
Vivien Didelota935c052016-09-29 12:21:53 -04001371 int i, err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001372
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001373 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001374 unsigned int shift = (i % 4) * 4 + nibble_offset;
1375 u8 data = entry->data[i];
1376
1377 regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
1378 }
1379
1380 for (i = 0; i < 3; ++i) {
Vivien Didelota935c052016-09-29 12:21:53 -04001381 u16 reg = regs[i];
1382
1383 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_DATA_0_3 + i, reg);
1384 if (err)
1385 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001386 }
1387
1388 return 0;
1389}
1390
Vivien Didelotfad09c72016-06-21 12:28:20 -04001391static int mv88e6xxx_vtu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001392 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001393{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001394 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 0);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001395}
1396
Vivien Didelotfad09c72016-06-21 12:28:20 -04001397static int mv88e6xxx_stu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001398 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001399{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001400 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 2);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001401}
1402
Vivien Didelotfad09c72016-06-21 12:28:20 -04001403static int _mv88e6xxx_vtu_vid_write(struct mv88e6xxx_chip *chip, u16 vid)
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001404{
Vivien Didelota935c052016-09-29 12:21:53 -04001405 return mv88e6xxx_g1_write(chip, GLOBAL_VTU_VID,
1406 vid & GLOBAL_VTU_VID_MASK);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001407}
1408
Vivien Didelotfad09c72016-06-21 12:28:20 -04001409static int _mv88e6xxx_vtu_getnext(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001410 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelotb8fee952015-08-13 12:52:19 -04001411{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001412 struct mv88e6xxx_vtu_entry next = { 0 };
Vivien Didelota935c052016-09-29 12:21:53 -04001413 u16 val;
1414 int err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001415
Vivien Didelota935c052016-09-29 12:21:53 -04001416 err = _mv88e6xxx_vtu_wait(chip);
1417 if (err)
1418 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001419
Vivien Didelota935c052016-09-29 12:21:53 -04001420 err = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_VTU_GET_NEXT);
1421 if (err)
1422 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001423
Vivien Didelota935c052016-09-29 12:21:53 -04001424 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_VID, &val);
1425 if (err)
1426 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001427
Vivien Didelota935c052016-09-29 12:21:53 -04001428 next.vid = val & GLOBAL_VTU_VID_MASK;
1429 next.valid = !!(val & GLOBAL_VTU_VID_VALID);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001430
1431 if (next.valid) {
Vivien Didelota935c052016-09-29 12:21:53 -04001432 err = mv88e6xxx_vtu_data_read(chip, &next);
1433 if (err)
1434 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001435
Vivien Didelot6dc10bb2016-09-29 12:21:55 -04001436 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G1_VTU_FID)) {
Vivien Didelota935c052016-09-29 12:21:53 -04001437 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_FID, &val);
1438 if (err)
1439 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001440
Vivien Didelota935c052016-09-29 12:21:53 -04001441 next.fid = val & GLOBAL_VTU_FID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001442 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001443 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1444 * VTU DBNum[3:0] are located in VTU Operation 3:0
1445 */
Vivien Didelota935c052016-09-29 12:21:53 -04001446 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_OP, &val);
1447 if (err)
1448 return err;
Vivien Didelot11ea8092016-03-31 16:53:44 -04001449
Vivien Didelota935c052016-09-29 12:21:53 -04001450 next.fid = (val & 0xf00) >> 4;
1451 next.fid |= val & 0xf;
Vivien Didelot2e7bd5e2016-03-31 16:53:41 -04001452 }
Vivien Didelotb8fee952015-08-13 12:52:19 -04001453
Vivien Didelotfad09c72016-06-21 12:28:20 -04001454 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
Vivien Didelota935c052016-09-29 12:21:53 -04001455 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_SID, &val);
1456 if (err)
1457 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001458
Vivien Didelota935c052016-09-29 12:21:53 -04001459 next.sid = val & GLOBAL_VTU_SID_MASK;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001460 }
1461 }
1462
1463 *entry = next;
1464 return 0;
1465}
1466
Vivien Didelotf81ec902016-05-09 13:22:58 -04001467static int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
1468 struct switchdev_obj_port_vlan *vlan,
1469 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001470{
Vivien Didelot04bed142016-08-31 18:06:13 -04001471 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001472 struct mv88e6xxx_vtu_entry next;
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001473 u16 pvid;
1474 int err;
1475
Vivien Didelotfad09c72016-06-21 12:28:20 -04001476 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001477 return -EOPNOTSUPP;
1478
Vivien Didelotfad09c72016-06-21 12:28:20 -04001479 mutex_lock(&chip->reg_lock);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001480
Vivien Didelot77064f32016-11-04 03:23:30 +01001481 err = mv88e6xxx_port_get_pvid(chip, port, &pvid);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001482 if (err)
1483 goto unlock;
1484
Vivien Didelotfad09c72016-06-21 12:28:20 -04001485 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001486 if (err)
1487 goto unlock;
1488
1489 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001490 err = _mv88e6xxx_vtu_getnext(chip, &next);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001491 if (err)
1492 break;
1493
1494 if (!next.valid)
1495 break;
1496
1497 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1498 continue;
1499
1500 /* reinit and dump this VLAN obj */
Vivien Didelot57d32312016-06-20 13:13:58 -04001501 vlan->vid_begin = next.vid;
1502 vlan->vid_end = next.vid;
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001503 vlan->flags = 0;
1504
1505 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1506 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1507
1508 if (next.vid == pvid)
1509 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1510
1511 err = cb(&vlan->obj);
1512 if (err)
1513 break;
1514 } while (next.vid < GLOBAL_VTU_VID_MASK);
1515
1516unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001517 mutex_unlock(&chip->reg_lock);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001518
1519 return err;
1520}
1521
Vivien Didelotfad09c72016-06-21 12:28:20 -04001522static int _mv88e6xxx_vtu_loadpurge(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001523 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001524{
Vivien Didelot11ea8092016-03-31 16:53:44 -04001525 u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001526 u16 reg = 0;
Vivien Didelota935c052016-09-29 12:21:53 -04001527 int err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001528
Vivien Didelota935c052016-09-29 12:21:53 -04001529 err = _mv88e6xxx_vtu_wait(chip);
1530 if (err)
1531 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001532
1533 if (!entry->valid)
1534 goto loadpurge;
1535
1536 /* Write port member tags */
Vivien Didelota935c052016-09-29 12:21:53 -04001537 err = mv88e6xxx_vtu_data_write(chip, entry);
1538 if (err)
1539 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001540
Vivien Didelotfad09c72016-06-21 12:28:20 -04001541 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001542 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Vivien Didelota935c052016-09-29 12:21:53 -04001543 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_SID, reg);
1544 if (err)
1545 return err;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001546 }
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001547
Vivien Didelot6dc10bb2016-09-29 12:21:55 -04001548 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G1_VTU_FID)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001549 reg = entry->fid & GLOBAL_VTU_FID_MASK;
Vivien Didelota935c052016-09-29 12:21:53 -04001550 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_FID, reg);
1551 if (err)
1552 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001553 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001554 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1555 * VTU DBNum[3:0] are located in VTU Operation 3:0
1556 */
1557 op |= (entry->fid & 0xf0) << 8;
1558 op |= entry->fid & 0xf;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001559 }
1560
1561 reg = GLOBAL_VTU_VID_VALID;
1562loadpurge:
1563 reg |= entry->vid & GLOBAL_VTU_VID_MASK;
Vivien Didelota935c052016-09-29 12:21:53 -04001564 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_VID, reg);
1565 if (err)
1566 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001567
Vivien Didelotfad09c72016-06-21 12:28:20 -04001568 return _mv88e6xxx_vtu_cmd(chip, op);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001569}
1570
Vivien Didelotfad09c72016-06-21 12:28:20 -04001571static int _mv88e6xxx_stu_getnext(struct mv88e6xxx_chip *chip, u8 sid,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001572 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001573{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001574 struct mv88e6xxx_vtu_entry next = { 0 };
Vivien Didelota935c052016-09-29 12:21:53 -04001575 u16 val;
1576 int err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001577
Vivien Didelota935c052016-09-29 12:21:53 -04001578 err = _mv88e6xxx_vtu_wait(chip);
1579 if (err)
1580 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001581
Vivien Didelota935c052016-09-29 12:21:53 -04001582 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_SID,
1583 sid & GLOBAL_VTU_SID_MASK);
1584 if (err)
1585 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001586
Vivien Didelota935c052016-09-29 12:21:53 -04001587 err = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_GET_NEXT);
1588 if (err)
1589 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001590
Vivien Didelota935c052016-09-29 12:21:53 -04001591 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_SID, &val);
1592 if (err)
1593 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001594
Vivien Didelota935c052016-09-29 12:21:53 -04001595 next.sid = val & GLOBAL_VTU_SID_MASK;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001596
Vivien Didelota935c052016-09-29 12:21:53 -04001597 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_VID, &val);
1598 if (err)
1599 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001600
Vivien Didelota935c052016-09-29 12:21:53 -04001601 next.valid = !!(val & GLOBAL_VTU_VID_VALID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001602
1603 if (next.valid) {
Vivien Didelota935c052016-09-29 12:21:53 -04001604 err = mv88e6xxx_stu_data_read(chip, &next);
1605 if (err)
1606 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001607 }
1608
1609 *entry = next;
1610 return 0;
1611}
1612
Vivien Didelotfad09c72016-06-21 12:28:20 -04001613static int _mv88e6xxx_stu_loadpurge(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001614 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001615{
1616 u16 reg = 0;
Vivien Didelota935c052016-09-29 12:21:53 -04001617 int err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001618
Vivien Didelota935c052016-09-29 12:21:53 -04001619 err = _mv88e6xxx_vtu_wait(chip);
1620 if (err)
1621 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001622
1623 if (!entry->valid)
1624 goto loadpurge;
1625
1626 /* Write port states */
Vivien Didelota935c052016-09-29 12:21:53 -04001627 err = mv88e6xxx_stu_data_write(chip, entry);
1628 if (err)
1629 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001630
1631 reg = GLOBAL_VTU_VID_VALID;
1632loadpurge:
Vivien Didelota935c052016-09-29 12:21:53 -04001633 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_VID, reg);
1634 if (err)
1635 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001636
1637 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Vivien Didelota935c052016-09-29 12:21:53 -04001638 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_SID, reg);
1639 if (err)
1640 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001641
Vivien Didelotfad09c72016-06-21 12:28:20 -04001642 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_LOAD_PURGE);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001643}
1644
Vivien Didelotfad09c72016-06-21 12:28:20 -04001645static int _mv88e6xxx_fid_new(struct mv88e6xxx_chip *chip, u16 *fid)
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001646{
1647 DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001648 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001649 int i, err;
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001650
1651 bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);
1652
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001653 /* Set every FID bit used by the (un)bridged ports */
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001654 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotb4e48c52016-11-04 03:23:29 +01001655 err = mv88e6xxx_port_get_fid(chip, i, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001656 if (err)
1657 return err;
1658
1659 set_bit(*fid, fid_bitmap);
1660 }
1661
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001662 /* Set every FID bit used by the VLAN entries */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001663 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001664 if (err)
1665 return err;
1666
1667 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001668 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001669 if (err)
1670 return err;
1671
1672 if (!vlan.valid)
1673 break;
1674
1675 set_bit(vlan.fid, fid_bitmap);
1676 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
1677
1678 /* The reset value 0x000 is used to indicate that multiple address
1679 * databases are not needed. Return the next positive available.
1680 */
1681 *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1);
Vivien Didelotfad09c72016-06-21 12:28:20 -04001682 if (unlikely(*fid >= mv88e6xxx_num_databases(chip)))
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001683 return -ENOSPC;
1684
1685 /* Clear the database */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001686 return _mv88e6xxx_atu_flush(chip, *fid, true);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001687}
1688
Vivien Didelotfad09c72016-06-21 12:28:20 -04001689static int _mv88e6xxx_vtu_new(struct mv88e6xxx_chip *chip, u16 vid,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001690 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001691{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001692 struct dsa_switch *ds = chip->ds;
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001693 struct mv88e6xxx_vtu_entry vlan = {
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001694 .valid = true,
1695 .vid = vid,
1696 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001697 int i, err;
1698
Vivien Didelotfad09c72016-06-21 12:28:20 -04001699 err = _mv88e6xxx_fid_new(chip, &vlan.fid);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001700 if (err)
1701 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001702
Vivien Didelot3d131f02015-11-03 10:52:52 -05001703 /* exclude all ports except the CPU and DSA ports */
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001704 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i)
Vivien Didelot3d131f02015-11-03 10:52:52 -05001705 vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)
1706 ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED
1707 : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001708
Vivien Didelotfad09c72016-06-21 12:28:20 -04001709 if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
1710 mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip)) {
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001711 struct mv88e6xxx_vtu_entry vstp;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001712
1713 /* Adding a VTU entry requires a valid STU entry. As VSTP is not
1714 * implemented, only one STU entry is needed to cover all VTU
1715 * entries. Thus, validate the SID 0.
1716 */
1717 vlan.sid = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001718 err = _mv88e6xxx_stu_getnext(chip, GLOBAL_VTU_SID_MASK, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001719 if (err)
1720 return err;
1721
1722 if (vstp.sid != vlan.sid || !vstp.valid) {
1723 memset(&vstp, 0, sizeof(vstp));
1724 vstp.valid = true;
1725 vstp.sid = vlan.sid;
1726
Vivien Didelotfad09c72016-06-21 12:28:20 -04001727 err = _mv88e6xxx_stu_loadpurge(chip, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001728 if (err)
1729 return err;
1730 }
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001731 }
1732
1733 *entry = vlan;
1734 return 0;
1735}
1736
Vivien Didelotfad09c72016-06-21 12:28:20 -04001737static int _mv88e6xxx_vtu_get(struct mv88e6xxx_chip *chip, u16 vid,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001738 struct mv88e6xxx_vtu_entry *entry, bool creat)
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001739{
1740 int err;
1741
1742 if (!vid)
1743 return -EINVAL;
1744
Vivien Didelotfad09c72016-06-21 12:28:20 -04001745 err = _mv88e6xxx_vtu_vid_write(chip, vid - 1);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001746 if (err)
1747 return err;
1748
Vivien Didelotfad09c72016-06-21 12:28:20 -04001749 err = _mv88e6xxx_vtu_getnext(chip, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001750 if (err)
1751 return err;
1752
1753 if (entry->vid != vid || !entry->valid) {
1754 if (!creat)
1755 return -EOPNOTSUPP;
1756 /* -ENOENT would've been more appropriate, but switchdev expects
1757 * -EOPNOTSUPP to inform bridge about an eventual software VLAN.
1758 */
1759
Vivien Didelotfad09c72016-06-21 12:28:20 -04001760 err = _mv88e6xxx_vtu_new(chip, vid, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001761 }
1762
1763 return err;
1764}
1765
Vivien Didelotda9c3592016-02-12 12:09:40 -05001766static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
1767 u16 vid_begin, u16 vid_end)
1768{
Vivien Didelot04bed142016-08-31 18:06:13 -04001769 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001770 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelotda9c3592016-02-12 12:09:40 -05001771 int i, err;
1772
1773 if (!vid_begin)
1774 return -EOPNOTSUPP;
1775
Vivien Didelotfad09c72016-06-21 12:28:20 -04001776 mutex_lock(&chip->reg_lock);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001777
Vivien Didelotfad09c72016-06-21 12:28:20 -04001778 err = _mv88e6xxx_vtu_vid_write(chip, vid_begin - 1);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001779 if (err)
1780 goto unlock;
1781
1782 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001783 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001784 if (err)
1785 goto unlock;
1786
1787 if (!vlan.valid)
1788 break;
1789
1790 if (vlan.vid > vid_end)
1791 break;
1792
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001793 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotda9c3592016-02-12 12:09:40 -05001794 if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i))
1795 continue;
1796
1797 if (vlan.data[i] ==
1798 GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1799 continue;
1800
Vivien Didelotfad09c72016-06-21 12:28:20 -04001801 if (chip->ports[i].bridge_dev ==
1802 chip->ports[port].bridge_dev)
Vivien Didelotda9c3592016-02-12 12:09:40 -05001803 break; /* same bridge, check next VLAN */
1804
Andrew Lunnc8b09802016-06-04 21:16:57 +02001805 netdev_warn(ds->ports[port].netdev,
Vivien Didelotda9c3592016-02-12 12:09:40 -05001806 "hardware VLAN %d already used by %s\n",
1807 vlan.vid,
Vivien Didelotfad09c72016-06-21 12:28:20 -04001808 netdev_name(chip->ports[i].bridge_dev));
Vivien Didelotda9c3592016-02-12 12:09:40 -05001809 err = -EOPNOTSUPP;
1810 goto unlock;
1811 }
1812 } while (vlan.vid < vid_end);
1813
1814unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001815 mutex_unlock(&chip->reg_lock);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001816
1817 return err;
1818}
1819
Vivien Didelotf81ec902016-05-09 13:22:58 -04001820static int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port,
1821 bool vlan_filtering)
Vivien Didelot214cdb92016-02-26 13:16:08 -05001822{
Vivien Didelot04bed142016-08-31 18:06:13 -04001823 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot385a0992016-11-04 03:23:31 +01001824 u16 mode = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE :
Vivien Didelot214cdb92016-02-26 13:16:08 -05001825 PORT_CONTROL_2_8021Q_DISABLED;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001826 int err;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001827
Vivien Didelotfad09c72016-06-21 12:28:20 -04001828 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001829 return -EOPNOTSUPP;
1830
Vivien Didelotfad09c72016-06-21 12:28:20 -04001831 mutex_lock(&chip->reg_lock);
Vivien Didelot385a0992016-11-04 03:23:31 +01001832 err = mv88e6xxx_port_set_8021q_mode(chip, port, mode);
Vivien Didelotfad09c72016-06-21 12:28:20 -04001833 mutex_unlock(&chip->reg_lock);
Vivien Didelot214cdb92016-02-26 13:16:08 -05001834
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001835 return err;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001836}
1837
Vivien Didelot57d32312016-06-20 13:13:58 -04001838static int
1839mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
1840 const struct switchdev_obj_port_vlan *vlan,
1841 struct switchdev_trans *trans)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001842{
Vivien Didelot04bed142016-08-31 18:06:13 -04001843 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotda9c3592016-02-12 12:09:40 -05001844 int err;
1845
Vivien Didelotfad09c72016-06-21 12:28:20 -04001846 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001847 return -EOPNOTSUPP;
1848
Vivien Didelotda9c3592016-02-12 12:09:40 -05001849 /* If the requested port doesn't belong to the same bridge as the VLAN
1850 * members, do not support it (yet) and fallback to software VLAN.
1851 */
1852 err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin,
1853 vlan->vid_end);
1854 if (err)
1855 return err;
1856
Vivien Didelot76e398a2015-11-01 12:33:55 -05001857 /* We don't need any dynamic resource from the kernel (yet),
1858 * so skip the prepare phase.
1859 */
1860 return 0;
1861}
1862
Vivien Didelotfad09c72016-06-21 12:28:20 -04001863static int _mv88e6xxx_port_vlan_add(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001864 u16 vid, bool untagged)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001865{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001866 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001867 int err;
1868
Vivien Didelotfad09c72016-06-21 12:28:20 -04001869 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, true);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001870 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001871 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001872
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001873 vlan.data[port] = untagged ?
1874 GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
1875 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
1876
Vivien Didelotfad09c72016-06-21 12:28:20 -04001877 return _mv88e6xxx_vtu_loadpurge(chip, &vlan);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001878}
1879
Vivien Didelotf81ec902016-05-09 13:22:58 -04001880static void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
1881 const struct switchdev_obj_port_vlan *vlan,
1882 struct switchdev_trans *trans)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001883{
Vivien Didelot04bed142016-08-31 18:06:13 -04001884 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001885 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1886 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1887 u16 vid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001888
Vivien Didelotfad09c72016-06-21 12:28:20 -04001889 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001890 return;
1891
Vivien Didelotfad09c72016-06-21 12:28:20 -04001892 mutex_lock(&chip->reg_lock);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001893
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001894 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
Vivien Didelotfad09c72016-06-21 12:28:20 -04001895 if (_mv88e6xxx_port_vlan_add(chip, port, vid, untagged))
Andrew Lunnc8b09802016-06-04 21:16:57 +02001896 netdev_err(ds->ports[port].netdev,
1897 "failed to add VLAN %d%c\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001898 vid, untagged ? 'u' : 't');
Vivien Didelot76e398a2015-11-01 12:33:55 -05001899
Vivien Didelot77064f32016-11-04 03:23:30 +01001900 if (pvid && mv88e6xxx_port_set_pvid(chip, port, vlan->vid_end))
Andrew Lunnc8b09802016-06-04 21:16:57 +02001901 netdev_err(ds->ports[port].netdev, "failed to set PVID %d\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001902 vlan->vid_end);
1903
Vivien Didelotfad09c72016-06-21 12:28:20 -04001904 mutex_unlock(&chip->reg_lock);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001905}
1906
Vivien Didelotfad09c72016-06-21 12:28:20 -04001907static int _mv88e6xxx_port_vlan_del(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001908 int port, u16 vid)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001909{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001910 struct dsa_switch *ds = chip->ds;
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001911 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001912 int i, err;
1913
Vivien Didelotfad09c72016-06-21 12:28:20 -04001914 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001915 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001916 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001917
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001918 /* Tell switchdev if this VLAN is handled in software */
1919 if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
Vivien Didelot3c06f082016-02-05 14:04:39 -05001920 return -EOPNOTSUPP;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001921
1922 vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
1923
1924 /* keep the VLAN unless all ports are excluded */
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001925 vlan.valid = false;
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001926 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelot3d131f02015-11-03 10:52:52 -05001927 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001928 continue;
1929
1930 if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001931 vlan.valid = true;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001932 break;
1933 }
1934 }
1935
Vivien Didelotfad09c72016-06-21 12:28:20 -04001936 err = _mv88e6xxx_vtu_loadpurge(chip, &vlan);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001937 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001938 return err;
1939
Vivien Didelotfad09c72016-06-21 12:28:20 -04001940 return _mv88e6xxx_atu_remove(chip, vlan.fid, port, false);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001941}
1942
Vivien Didelotf81ec902016-05-09 13:22:58 -04001943static int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
1944 const struct switchdev_obj_port_vlan *vlan)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001945{
Vivien Didelot04bed142016-08-31 18:06:13 -04001946 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001947 u16 pvid, vid;
1948 int err = 0;
1949
Vivien Didelotfad09c72016-06-21 12:28:20 -04001950 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001951 return -EOPNOTSUPP;
1952
Vivien Didelotfad09c72016-06-21 12:28:20 -04001953 mutex_lock(&chip->reg_lock);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001954
Vivien Didelot77064f32016-11-04 03:23:30 +01001955 err = mv88e6xxx_port_get_pvid(chip, port, &pvid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001956 if (err)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001957 goto unlock;
1958
Vivien Didelot76e398a2015-11-01 12:33:55 -05001959 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001960 err = _mv88e6xxx_port_vlan_del(chip, port, vid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001961 if (err)
1962 goto unlock;
1963
1964 if (vid == pvid) {
Vivien Didelot77064f32016-11-04 03:23:30 +01001965 err = mv88e6xxx_port_set_pvid(chip, port, 0);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001966 if (err)
1967 goto unlock;
1968 }
1969 }
1970
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001971unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001972 mutex_unlock(&chip->reg_lock);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001973
1974 return err;
1975}
1976
Vivien Didelotfad09c72016-06-21 12:28:20 -04001977static int _mv88e6xxx_atu_mac_write(struct mv88e6xxx_chip *chip,
Vivien Didelotc5723ac2015-08-10 09:09:48 -04001978 const unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001979{
Vivien Didelota935c052016-09-29 12:21:53 -04001980 int i, err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001981
1982 for (i = 0; i < 3; i++) {
Vivien Didelota935c052016-09-29 12:21:53 -04001983 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_MAC_01 + i,
1984 (addr[i * 2] << 8) | addr[i * 2 + 1]);
1985 if (err)
1986 return err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001987 }
1988
1989 return 0;
1990}
1991
Vivien Didelotfad09c72016-06-21 12:28:20 -04001992static int _mv88e6xxx_atu_mac_read(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001993 unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001994{
Vivien Didelota935c052016-09-29 12:21:53 -04001995 u16 val;
1996 int i, err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001997
1998 for (i = 0; i < 3; i++) {
Vivien Didelota935c052016-09-29 12:21:53 -04001999 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_MAC_01 + i, &val);
2000 if (err)
2001 return err;
2002
2003 addr[i * 2] = val >> 8;
2004 addr[i * 2 + 1] = val & 0xff;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002005 }
2006
2007 return 0;
2008}
2009
Vivien Didelotfad09c72016-06-21 12:28:20 -04002010static int _mv88e6xxx_atu_load(struct mv88e6xxx_chip *chip,
Vivien Didelotfd231c82015-08-10 09:09:50 -04002011 struct mv88e6xxx_atu_entry *entry)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002012{
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002013 int ret;
2014
Vivien Didelotfad09c72016-06-21 12:28:20 -04002015 ret = _mv88e6xxx_atu_wait(chip);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002016 if (ret < 0)
2017 return ret;
2018
Vivien Didelotfad09c72016-06-21 12:28:20 -04002019 ret = _mv88e6xxx_atu_mac_write(chip, entry->mac);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002020 if (ret < 0)
2021 return ret;
2022
Vivien Didelotfad09c72016-06-21 12:28:20 -04002023 ret = _mv88e6xxx_atu_data_write(chip, entry);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002024 if (ret < 0)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002025 return ret;
2026
Vivien Didelotfad09c72016-06-21 12:28:20 -04002027 return _mv88e6xxx_atu_cmd(chip, entry->fid, GLOBAL_ATU_OP_LOAD_DB);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002028}
David S. Millercdf09692015-08-11 12:00:37 -07002029
Vivien Didelot88472932016-09-19 19:56:11 -04002030static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
2031 struct mv88e6xxx_atu_entry *entry);
2032
2033static int mv88e6xxx_atu_get(struct mv88e6xxx_chip *chip, int fid,
2034 const u8 *addr, struct mv88e6xxx_atu_entry *entry)
2035{
2036 struct mv88e6xxx_atu_entry next;
2037 int err;
2038
2039 eth_broadcast_addr(next.mac);
2040
2041 err = _mv88e6xxx_atu_mac_write(chip, next.mac);
2042 if (err)
2043 return err;
2044
2045 do {
2046 err = _mv88e6xxx_atu_getnext(chip, fid, &next);
2047 if (err)
2048 return err;
2049
2050 if (next.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2051 break;
2052
2053 if (ether_addr_equal(next.mac, addr)) {
2054 *entry = next;
2055 return 0;
2056 }
2057 } while (!is_broadcast_ether_addr(next.mac));
2058
2059 memset(entry, 0, sizeof(*entry));
2060 entry->fid = fid;
2061 ether_addr_copy(entry->mac, addr);
2062
2063 return 0;
2064}
2065
Vivien Didelot83dabd12016-08-31 11:50:04 -04002066static int mv88e6xxx_port_db_load_purge(struct mv88e6xxx_chip *chip, int port,
2067 const unsigned char *addr, u16 vid,
2068 u8 state)
Vivien Didelotfd231c82015-08-10 09:09:50 -04002069{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04002070 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelot88472932016-09-19 19:56:11 -04002071 struct mv88e6xxx_atu_entry entry;
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002072 int err;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002073
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002074 /* Null VLAN ID corresponds to the port private database */
2075 if (vid == 0)
Vivien Didelotb4e48c52016-11-04 03:23:29 +01002076 err = mv88e6xxx_port_get_fid(chip, port, &vlan.fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002077 else
Vivien Didelotfad09c72016-06-21 12:28:20 -04002078 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002079 if (err)
2080 return err;
2081
Vivien Didelot88472932016-09-19 19:56:11 -04002082 err = mv88e6xxx_atu_get(chip, vlan.fid, addr, &entry);
2083 if (err)
2084 return err;
2085
2086 /* Purge the ATU entry only if no port is using it anymore */
2087 if (state == GLOBAL_ATU_DATA_STATE_UNUSED) {
2088 entry.portv_trunkid &= ~BIT(port);
2089 if (!entry.portv_trunkid)
2090 entry.state = GLOBAL_ATU_DATA_STATE_UNUSED;
2091 } else {
2092 entry.portv_trunkid |= BIT(port);
2093 entry.state = state;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002094 }
2095
Vivien Didelotfad09c72016-06-21 12:28:20 -04002096 return _mv88e6xxx_atu_load(chip, &entry);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002097}
2098
Vivien Didelotf81ec902016-05-09 13:22:58 -04002099static int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
2100 const struct switchdev_obj_port_fdb *fdb,
2101 struct switchdev_trans *trans)
Vivien Didelot146a3202015-10-08 11:35:12 -04002102{
2103 /* We don't need any dynamic resource from the kernel (yet),
2104 * so skip the prepare phase.
2105 */
2106 return 0;
2107}
2108
Vivien Didelotf81ec902016-05-09 13:22:58 -04002109static void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
2110 const struct switchdev_obj_port_fdb *fdb,
2111 struct switchdev_trans *trans)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002112{
Vivien Didelot04bed142016-08-31 18:06:13 -04002113 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot6630e232015-08-06 01:44:07 -04002114
Vivien Didelotfad09c72016-06-21 12:28:20 -04002115 mutex_lock(&chip->reg_lock);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002116 if (mv88e6xxx_port_db_load_purge(chip, port, fdb->addr, fdb->vid,
2117 GLOBAL_ATU_DATA_STATE_UC_STATIC))
2118 netdev_err(ds->ports[port].netdev, "failed to load unicast MAC address\n");
Vivien Didelotfad09c72016-06-21 12:28:20 -04002119 mutex_unlock(&chip->reg_lock);
David S. Millercdf09692015-08-11 12:00:37 -07002120}
2121
Vivien Didelotf81ec902016-05-09 13:22:58 -04002122static int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
2123 const struct switchdev_obj_port_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -07002124{
Vivien Didelot04bed142016-08-31 18:06:13 -04002125 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot83dabd12016-08-31 11:50:04 -04002126 int err;
David S. Millercdf09692015-08-11 12:00:37 -07002127
Vivien Didelotfad09c72016-06-21 12:28:20 -04002128 mutex_lock(&chip->reg_lock);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002129 err = mv88e6xxx_port_db_load_purge(chip, port, fdb->addr, fdb->vid,
2130 GLOBAL_ATU_DATA_STATE_UNUSED);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002131 mutex_unlock(&chip->reg_lock);
David S. Millercdf09692015-08-11 12:00:37 -07002132
Vivien Didelot83dabd12016-08-31 11:50:04 -04002133 return err;
David S. Millercdf09692015-08-11 12:00:37 -07002134}
2135
Vivien Didelotfad09c72016-06-21 12:28:20 -04002136static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
Vivien Didelot1d194042015-08-10 09:09:51 -04002137 struct mv88e6xxx_atu_entry *entry)
David S. Millercdf09692015-08-11 12:00:37 -07002138{
Vivien Didelot1d194042015-08-10 09:09:51 -04002139 struct mv88e6xxx_atu_entry next = { 0 };
Vivien Didelota935c052016-09-29 12:21:53 -04002140 u16 val;
2141 int err;
Vivien Didelot1d194042015-08-10 09:09:51 -04002142
2143 next.fid = fid;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002144
Vivien Didelota935c052016-09-29 12:21:53 -04002145 err = _mv88e6xxx_atu_wait(chip);
2146 if (err)
2147 return err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002148
Vivien Didelota935c052016-09-29 12:21:53 -04002149 err = _mv88e6xxx_atu_cmd(chip, fid, GLOBAL_ATU_OP_GET_NEXT_DB);
2150 if (err)
2151 return err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002152
Vivien Didelota935c052016-09-29 12:21:53 -04002153 err = _mv88e6xxx_atu_mac_read(chip, next.mac);
2154 if (err)
2155 return err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002156
Vivien Didelota935c052016-09-29 12:21:53 -04002157 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_DATA, &val);
2158 if (err)
2159 return err;
Vivien Didelot1d194042015-08-10 09:09:51 -04002160
Vivien Didelota935c052016-09-29 12:21:53 -04002161 next.state = val & GLOBAL_ATU_DATA_STATE_MASK;
Vivien Didelot1d194042015-08-10 09:09:51 -04002162 if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2163 unsigned int mask, shift;
2164
Vivien Didelota935c052016-09-29 12:21:53 -04002165 if (val & GLOBAL_ATU_DATA_TRUNK) {
Vivien Didelot1d194042015-08-10 09:09:51 -04002166 next.trunk = true;
2167 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
2168 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
2169 } else {
2170 next.trunk = false;
2171 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
2172 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
2173 }
2174
Vivien Didelota935c052016-09-29 12:21:53 -04002175 next.portv_trunkid = (val & mask) >> shift;
Vivien Didelot1d194042015-08-10 09:09:51 -04002176 }
2177
2178 *entry = next;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002179 return 0;
2180}
2181
Vivien Didelot83dabd12016-08-31 11:50:04 -04002182static int mv88e6xxx_port_db_dump_fid(struct mv88e6xxx_chip *chip,
2183 u16 fid, u16 vid, int port,
2184 struct switchdev_obj *obj,
2185 int (*cb)(struct switchdev_obj *obj))
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002186{
2187 struct mv88e6xxx_atu_entry addr = {
2188 .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2189 };
2190 int err;
2191
Vivien Didelotfad09c72016-06-21 12:28:20 -04002192 err = _mv88e6xxx_atu_mac_write(chip, addr.mac);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002193 if (err)
2194 return err;
2195
2196 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002197 err = _mv88e6xxx_atu_getnext(chip, fid, &addr);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002198 if (err)
Vivien Didelot83dabd12016-08-31 11:50:04 -04002199 return err;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002200
2201 if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2202 break;
2203
Vivien Didelot83dabd12016-08-31 11:50:04 -04002204 if (addr.trunk || (addr.portv_trunkid & BIT(port)) == 0)
2205 continue;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002206
Vivien Didelot83dabd12016-08-31 11:50:04 -04002207 if (obj->id == SWITCHDEV_OBJ_ID_PORT_FDB) {
2208 struct switchdev_obj_port_fdb *fdb;
2209
2210 if (!is_unicast_ether_addr(addr.mac))
2211 continue;
2212
2213 fdb = SWITCHDEV_OBJ_PORT_FDB(obj);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002214 fdb->vid = vid;
2215 ether_addr_copy(fdb->addr, addr.mac);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002216 if (addr.state == GLOBAL_ATU_DATA_STATE_UC_STATIC)
2217 fdb->ndm_state = NUD_NOARP;
2218 else
2219 fdb->ndm_state = NUD_REACHABLE;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04002220 } else if (obj->id == SWITCHDEV_OBJ_ID_PORT_MDB) {
2221 struct switchdev_obj_port_mdb *mdb;
2222
2223 if (!is_multicast_ether_addr(addr.mac))
2224 continue;
2225
2226 mdb = SWITCHDEV_OBJ_PORT_MDB(obj);
2227 mdb->vid = vid;
2228 ether_addr_copy(mdb->addr, addr.mac);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002229 } else {
2230 return -EOPNOTSUPP;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002231 }
Vivien Didelot83dabd12016-08-31 11:50:04 -04002232
2233 err = cb(obj);
2234 if (err)
2235 return err;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002236 } while (!is_broadcast_ether_addr(addr.mac));
2237
2238 return err;
2239}
2240
Vivien Didelot83dabd12016-08-31 11:50:04 -04002241static int mv88e6xxx_port_db_dump(struct mv88e6xxx_chip *chip, int port,
2242 struct switchdev_obj *obj,
2243 int (*cb)(struct switchdev_obj *obj))
2244{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04002245 struct mv88e6xxx_vtu_entry vlan = {
Vivien Didelot83dabd12016-08-31 11:50:04 -04002246 .vid = GLOBAL_VTU_VID_MASK, /* all ones */
2247 };
2248 u16 fid;
2249 int err;
2250
2251 /* Dump port's default Filtering Information Database (VLAN ID 0) */
Vivien Didelotb4e48c52016-11-04 03:23:29 +01002252 err = mv88e6xxx_port_get_fid(chip, port, &fid);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002253 if (err)
2254 return err;
2255
2256 err = mv88e6xxx_port_db_dump_fid(chip, fid, 0, port, obj, cb);
2257 if (err)
2258 return err;
2259
2260 /* Dump VLANs' Filtering Information Databases */
2261 err = _mv88e6xxx_vtu_vid_write(chip, vlan.vid);
2262 if (err)
2263 return err;
2264
2265 do {
2266 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
2267 if (err)
2268 return err;
2269
2270 if (!vlan.valid)
2271 break;
2272
2273 err = mv88e6xxx_port_db_dump_fid(chip, vlan.fid, vlan.vid, port,
2274 obj, cb);
2275 if (err)
2276 return err;
2277 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
2278
2279 return err;
2280}
2281
Vivien Didelotf81ec902016-05-09 13:22:58 -04002282static int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
2283 struct switchdev_obj_port_fdb *fdb,
2284 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotf33475b2015-10-22 09:34:41 -04002285{
Vivien Didelot04bed142016-08-31 18:06:13 -04002286 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002287 int err;
2288
Vivien Didelotfad09c72016-06-21 12:28:20 -04002289 mutex_lock(&chip->reg_lock);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002290 err = mv88e6xxx_port_db_dump(chip, port, &fdb->obj, cb);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002291 mutex_unlock(&chip->reg_lock);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002292
2293 return err;
2294}
2295
Vivien Didelotf81ec902016-05-09 13:22:58 -04002296static int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
2297 struct net_device *bridge)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002298{
Vivien Didelot04bed142016-08-31 18:06:13 -04002299 struct mv88e6xxx_chip *chip = ds->priv;
Colin Ian King1d9619d2016-04-25 23:11:22 +01002300 int i, err = 0;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002301
Vivien Didelotfad09c72016-06-21 12:28:20 -04002302 mutex_lock(&chip->reg_lock);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002303
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002304 /* Assign the bridge and remap each port's VLANTable */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002305 chip->ports[port].bridge_dev = bridge;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002306
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002307 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002308 if (chip->ports[i].bridge_dev == bridge) {
2309 err = _mv88e6xxx_port_based_vlan_map(chip, i);
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002310 if (err)
2311 break;
2312 }
2313 }
2314
Vivien Didelotfad09c72016-06-21 12:28:20 -04002315 mutex_unlock(&chip->reg_lock);
Vivien Didelota6692752016-02-12 12:09:39 -05002316
Vivien Didelot466dfa02016-02-26 13:16:05 -05002317 return err;
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002318}
2319
Vivien Didelotf81ec902016-05-09 13:22:58 -04002320static void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002321{
Vivien Didelot04bed142016-08-31 18:06:13 -04002322 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002323 struct net_device *bridge = chip->ports[port].bridge_dev;
Vivien Didelot16bfa702016-03-13 16:21:33 -04002324 int i;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002325
Vivien Didelotfad09c72016-06-21 12:28:20 -04002326 mutex_lock(&chip->reg_lock);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002327
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002328 /* Unassign the bridge and remap each port's VLANTable */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002329 chip->ports[port].bridge_dev = NULL;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002330
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002331 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002332 if (i == port || chip->ports[i].bridge_dev == bridge)
2333 if (_mv88e6xxx_port_based_vlan_map(chip, i))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002334 netdev_warn(ds->ports[i].netdev,
2335 "failed to remap\n");
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002336
Vivien Didelotfad09c72016-06-21 12:28:20 -04002337 mutex_unlock(&chip->reg_lock);
Vivien Didelot66d9cd02016-02-05 14:07:14 -05002338}
2339
Vivien Didelotfad09c72016-06-21 12:28:20 -04002340static int mv88e6xxx_switch_reset(struct mv88e6xxx_chip *chip)
Vivien Didelot552238b2016-05-09 13:22:49 -04002341{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002342 bool ppu_active = mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE);
Vivien Didelot552238b2016-05-09 13:22:49 -04002343 u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002344 struct gpio_desc *gpiod = chip->reset;
Vivien Didelot552238b2016-05-09 13:22:49 -04002345 unsigned long timeout;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002346 u16 reg;
Vivien Didelota935c052016-09-29 12:21:53 -04002347 int err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002348 int i;
2349
2350 /* Set all ports to the disabled state. */
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002351 for (i = 0; i < mv88e6xxx_num_ports(chip); i++) {
Vivien Didelote28def332016-11-04 03:23:27 +01002352 err = mv88e6xxx_port_set_state(chip, i,
2353 PORT_CONTROL_STATE_DISABLED);
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002354 if (err)
2355 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002356 }
2357
2358 /* Wait for transmit queues to drain. */
2359 usleep_range(2000, 4000);
2360
2361 /* If there is a gpio connected to the reset pin, toggle it */
2362 if (gpiod) {
2363 gpiod_set_value_cansleep(gpiod, 1);
2364 usleep_range(10000, 20000);
2365 gpiod_set_value_cansleep(gpiod, 0);
2366 usleep_range(10000, 20000);
2367 }
2368
2369 /* Reset the switch. Keep the PPU active if requested. The PPU
2370 * needs to be active to support indirect phy register access
2371 * through global registers 0x18 and 0x19.
2372 */
2373 if (ppu_active)
Vivien Didelota935c052016-09-29 12:21:53 -04002374 err = mv88e6xxx_g1_write(chip, 0x04, 0xc000);
Vivien Didelot552238b2016-05-09 13:22:49 -04002375 else
Vivien Didelota935c052016-09-29 12:21:53 -04002376 err = mv88e6xxx_g1_write(chip, 0x04, 0xc400);
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002377 if (err)
2378 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002379
2380 /* Wait up to one second for reset to complete. */
2381 timeout = jiffies + 1 * HZ;
2382 while (time_before(jiffies, timeout)) {
Vivien Didelota935c052016-09-29 12:21:53 -04002383 err = mv88e6xxx_g1_read(chip, 0x00, &reg);
2384 if (err)
2385 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002386
Vivien Didelota935c052016-09-29 12:21:53 -04002387 if ((reg & is_reset) == is_reset)
Vivien Didelot552238b2016-05-09 13:22:49 -04002388 break;
2389 usleep_range(1000, 2000);
2390 }
2391 if (time_after(jiffies, timeout))
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002392 err = -ETIMEDOUT;
Vivien Didelot552238b2016-05-09 13:22:49 -04002393 else
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002394 err = 0;
Vivien Didelot552238b2016-05-09 13:22:49 -04002395
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002396 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002397}
2398
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002399static int mv88e6xxx_serdes_power_on(struct mv88e6xxx_chip *chip)
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002400{
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002401 u16 val;
2402 int err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002403
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002404 /* Clear Power Down bit */
2405 err = mv88e6xxx_serdes_read(chip, MII_BMCR, &val);
2406 if (err)
2407 return err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002408
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002409 if (val & BMCR_PDOWN) {
2410 val &= ~BMCR_PDOWN;
2411 err = mv88e6xxx_serdes_write(chip, MII_BMCR, val);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002412 }
2413
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002414 return err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002415}
2416
Vivien Didelotfad09c72016-06-21 12:28:20 -04002417static int mv88e6xxx_setup_port(struct mv88e6xxx_chip *chip, int port)
Guenter Roeckd827e882015-03-26 18:36:29 -07002418{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002419 struct dsa_switch *ds = chip->ds;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002420 int err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002421 u16 reg;
Guenter Roeckd827e882015-03-26 18:36:29 -07002422
Vivien Didelotd78343d2016-11-04 03:23:36 +01002423 /* MAC Forcing register: don't force link, speed, duplex or flow control
2424 * state to any particular values on physical ports, but force the CPU
2425 * port and all DSA ports to their maximum bandwidth and full duplex.
2426 */
2427 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))
2428 err = mv88e6xxx_port_setup_mac(chip, port, LINK_FORCED_UP,
2429 SPEED_MAX, DUPLEX_FULL,
2430 PHY_INTERFACE_MODE_NA);
2431 else
2432 err = mv88e6xxx_port_setup_mac(chip, port, LINK_UNFORCED,
2433 SPEED_UNFORCED, DUPLEX_UNFORCED,
2434 PHY_INTERFACE_MODE_NA);
2435 if (err)
2436 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002437
2438 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
2439 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
2440 * tunneling, determine priority by looking at 802.1p and IP
2441 * priority fields (IP prio has precedence), and set STP state
2442 * to Forwarding.
2443 *
2444 * If this is the CPU link, use DSA or EDSA tagging depending
2445 * on which tagging mode was configured.
2446 *
2447 * If this is a link to another switch, use DSA tagging mode.
2448 *
2449 * If this is the upstream port for this switch, enable
2450 * forwarding of unknown unicasts and multicasts.
2451 */
2452 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002453 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2454 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2455 mv88e6xxx_6095_family(chip) || mv88e6xxx_6065_family(chip) ||
2456 mv88e6xxx_6185_family(chip) || mv88e6xxx_6320_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002457 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
2458 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
2459 PORT_CONTROL_STATE_FORWARDING;
2460 if (dsa_is_cpu_port(ds, port)) {
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002461 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA))
Andrew Lunn5377b802016-06-04 21:17:02 +02002462 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA |
Andrew Lunnc047a1f2015-09-29 01:50:56 +02002463 PORT_CONTROL_FORWARD_UNKNOWN_MC;
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002464 else
2465 reg |= PORT_CONTROL_DSA_TAG;
Jamie Lentinf027e0c2016-08-22 16:01:04 +02002466 reg |= PORT_CONTROL_EGRESS_ADD_TAG |
2467 PORT_CONTROL_FORWARD_UNKNOWN;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002468 }
Andrew Lunn6083ce72015-08-17 23:52:52 +02002469 if (dsa_is_dsa_port(ds, port)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002470 if (mv88e6xxx_6095_family(chip) ||
2471 mv88e6xxx_6185_family(chip))
Andrew Lunn6083ce72015-08-17 23:52:52 +02002472 reg |= PORT_CONTROL_DSA_TAG;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002473 if (mv88e6xxx_6352_family(chip) ||
2474 mv88e6xxx_6351_family(chip) ||
2475 mv88e6xxx_6165_family(chip) ||
2476 mv88e6xxx_6097_family(chip) ||
2477 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002478 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunn6083ce72015-08-17 23:52:52 +02002479 }
2480
Andrew Lunn54d792f2015-05-06 01:09:47 +02002481 if (port == dsa_upstream_port(ds))
2482 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2483 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2484 }
2485 if (reg) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002486 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL, reg);
2487 if (err)
2488 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002489 }
2490
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002491 /* If this port is connected to a SerDes, make sure the SerDes is not
2492 * powered down.
2493 */
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002494 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_SERDES)) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002495 err = mv88e6xxx_port_read(chip, port, PORT_STATUS, &reg);
2496 if (err)
2497 return err;
2498 reg &= PORT_STATUS_CMODE_MASK;
2499 if ((reg == PORT_STATUS_CMODE_100BASE_X) ||
2500 (reg == PORT_STATUS_CMODE_1000BASE_X) ||
2501 (reg == PORT_STATUS_CMODE_SGMII)) {
2502 err = mv88e6xxx_serdes_power_on(chip);
2503 if (err < 0)
2504 return err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002505 }
2506 }
2507
Vivien Didelot8efdda42015-08-13 12:52:23 -04002508 /* Port Control 2: don't force a good FCS, set the maximum frame size to
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002509 * 10240 bytes, disable 802.1q tags checking, don't discard tagged or
Vivien Didelot8efdda42015-08-13 12:52:23 -04002510 * untagged frames on this port, do a destination address lookup on all
2511 * received packets as usual, disable ARP mirroring and don't send a
2512 * copy of all transmitted/received frames on this port to the CPU.
Andrew Lunn54d792f2015-05-06 01:09:47 +02002513 */
2514 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002515 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2516 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2517 mv88e6xxx_6095_family(chip) || mv88e6xxx_6320_family(chip) ||
2518 mv88e6xxx_6185_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002519 reg = PORT_CONTROL_2_MAP_DA;
2520
Vivien Didelotfad09c72016-06-21 12:28:20 -04002521 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2522 mv88e6xxx_6165_family(chip) || mv88e6xxx_6320_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002523 reg |= PORT_CONTROL_2_JUMBO_10240;
2524
Vivien Didelotfad09c72016-06-21 12:28:20 -04002525 if (mv88e6xxx_6095_family(chip) || mv88e6xxx_6185_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002526 /* Set the upstream port this port should use */
2527 reg |= dsa_upstream_port(ds);
2528 /* enable forwarding of unknown multicast addresses to
2529 * the upstream port
2530 */
2531 if (port == dsa_upstream_port(ds))
2532 reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
2533 }
2534
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002535 reg |= PORT_CONTROL_2_8021Q_DISABLED;
Vivien Didelot8efdda42015-08-13 12:52:23 -04002536
Andrew Lunn54d792f2015-05-06 01:09:47 +02002537 if (reg) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002538 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_2, reg);
2539 if (err)
2540 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002541 }
2542
2543 /* Port Association Vector: when learning source addresses
2544 * of packets, add the address to the address database using
2545 * a port bitmap that has only the bit for this port set and
2546 * the other bits clear.
2547 */
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002548 reg = 1 << port;
Vivien Didelot996ecb82016-04-14 14:42:08 -04002549 /* Disable learning for CPU port */
2550 if (dsa_is_cpu_port(ds, port))
Vivien Didelot65fa4022016-04-14 14:42:07 -04002551 reg = 0;
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002552
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002553 err = mv88e6xxx_port_write(chip, port, PORT_ASSOC_VECTOR, reg);
2554 if (err)
2555 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002556
2557 /* Egress rate control 2: disable egress rate control. */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002558 err = mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL_2, 0x0000);
2559 if (err)
2560 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002561
Vivien Didelotfad09c72016-06-21 12:28:20 -04002562 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2563 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2564 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002565 /* Do not limit the period of time that this port can
2566 * be paused for by the remote end or the period of
2567 * time that this port can pause the remote end.
2568 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002569 err = mv88e6xxx_port_write(chip, port, PORT_PAUSE_CTRL, 0x0000);
2570 if (err)
2571 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002572
2573 /* Port ATU control: disable limiting the number of
2574 * address database entries that this port is allowed
2575 * to use.
2576 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002577 err = mv88e6xxx_port_write(chip, port, PORT_ATU_CONTROL,
2578 0x0000);
Andrew Lunn54d792f2015-05-06 01:09:47 +02002579 /* Priority Override: disable DA, SA and VTU priority
2580 * override.
2581 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002582 err = mv88e6xxx_port_write(chip, port, PORT_PRI_OVERRIDE,
2583 0x0000);
2584 if (err)
2585 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002586
2587 /* Port Ethertype: use the Ethertype DSA Ethertype
2588 * value.
2589 */
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002590 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA)) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002591 err = mv88e6xxx_port_write(chip, port, PORT_ETH_TYPE,
2592 ETH_P_EDSA);
2593 if (err)
2594 return err;
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002595 }
2596
Andrew Lunn54d792f2015-05-06 01:09:47 +02002597 /* Tag Remap: use an identity 802.1p prio -> switch
2598 * prio mapping.
2599 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002600 err = mv88e6xxx_port_write(chip, port, PORT_TAG_REGMAP_0123,
2601 0x3210);
2602 if (err)
2603 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002604
2605 /* Tag Remap 2: use an identity 802.1p prio -> switch
2606 * prio mapping.
2607 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002608 err = mv88e6xxx_port_write(chip, port, PORT_TAG_REGMAP_4567,
2609 0x7654);
2610 if (err)
2611 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002612 }
2613
Jamie Lentin1bc261f2016-08-22 22:47:08 +01002614 /* Rate Control: disable ingress rate limiting. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002615 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2616 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
Vivien Didelotfad09c72016-06-21 12:28:20 -04002617 mv88e6xxx_6320_family(chip)) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002618 err = mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL,
2619 0x0001);
2620 if (err)
2621 return err;
Jamie Lentin1bc261f2016-08-22 22:47:08 +01002622 } else if (mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip)) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002623 err = mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL,
2624 0x0000);
2625 if (err)
2626 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002627 }
2628
Guenter Roeck366f0a02015-03-26 18:36:30 -07002629 /* Port Control 1: disable trunking, disable sending
2630 * learning messages to this port.
Guenter Roeckd827e882015-03-26 18:36:29 -07002631 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002632 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_1, 0x0000);
2633 if (err)
2634 return err;
Guenter Roeckd827e882015-03-26 18:36:29 -07002635
Vivien Didelot207afda2016-04-14 14:42:09 -04002636 /* Port based VLAN map: give each port the same default address
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002637 * database, and allow bidirectional communication between the
2638 * CPU and DSA port(s), and the other ports.
Guenter Roeckd827e882015-03-26 18:36:29 -07002639 */
Vivien Didelotb4e48c52016-11-04 03:23:29 +01002640 err = mv88e6xxx_port_set_fid(chip, port, 0);
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002641 if (err)
2642 return err;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002643
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002644 err = _mv88e6xxx_port_based_vlan_map(chip, port);
2645 if (err)
2646 return err;
Guenter Roeckd827e882015-03-26 18:36:29 -07002647
2648 /* Default VLAN ID and priority: don't set a default VLAN
2649 * ID, and set the default packet priority to zero.
2650 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002651 return mv88e6xxx_port_write(chip, port, PORT_DEFAULT_VLAN, 0x0000);
Andrew Lunndbde9e62015-05-06 01:09:48 +02002652}
2653
Wei Yongjunaa0938c2016-10-18 15:53:37 +00002654static int mv88e6xxx_g1_set_switch_mac(struct mv88e6xxx_chip *chip, u8 *addr)
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002655{
2656 int err;
2657
Vivien Didelota935c052016-09-29 12:21:53 -04002658 err = mv88e6xxx_g1_write(chip, GLOBAL_MAC_01, (addr[0] << 8) | addr[1]);
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002659 if (err)
2660 return err;
2661
Vivien Didelota935c052016-09-29 12:21:53 -04002662 err = mv88e6xxx_g1_write(chip, GLOBAL_MAC_23, (addr[2] << 8) | addr[3]);
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002663 if (err)
2664 return err;
2665
Vivien Didelota935c052016-09-29 12:21:53 -04002666 err = mv88e6xxx_g1_write(chip, GLOBAL_MAC_45, (addr[4] << 8) | addr[5]);
2667 if (err)
2668 return err;
2669
2670 return 0;
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002671}
2672
Vivien Didelotacddbd22016-07-18 20:45:39 -04002673static int mv88e6xxx_g1_set_age_time(struct mv88e6xxx_chip *chip,
2674 unsigned int msecs)
2675{
2676 const unsigned int coeff = chip->info->age_time_coeff;
2677 const unsigned int min = 0x01 * coeff;
2678 const unsigned int max = 0xff * coeff;
2679 u8 age_time;
2680 u16 val;
2681 int err;
2682
2683 if (msecs < min || msecs > max)
2684 return -ERANGE;
2685
2686 /* Round to nearest multiple of coeff */
2687 age_time = (msecs + coeff / 2) / coeff;
2688
Vivien Didelota935c052016-09-29 12:21:53 -04002689 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_CONTROL, &val);
Vivien Didelotacddbd22016-07-18 20:45:39 -04002690 if (err)
2691 return err;
2692
2693 /* AgeTime is 11:4 bits */
2694 val &= ~0xff0;
2695 val |= age_time << 4;
2696
Vivien Didelota935c052016-09-29 12:21:53 -04002697 return mv88e6xxx_g1_write(chip, GLOBAL_ATU_CONTROL, val);
Vivien Didelotacddbd22016-07-18 20:45:39 -04002698}
2699
Vivien Didelot2cfcd962016-07-18 20:45:40 -04002700static int mv88e6xxx_set_ageing_time(struct dsa_switch *ds,
2701 unsigned int ageing_time)
2702{
Vivien Didelot04bed142016-08-31 18:06:13 -04002703 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot2cfcd962016-07-18 20:45:40 -04002704 int err;
2705
2706 mutex_lock(&chip->reg_lock);
2707 err = mv88e6xxx_g1_set_age_time(chip, ageing_time);
2708 mutex_unlock(&chip->reg_lock);
2709
2710 return err;
2711}
2712
Vivien Didelot97299342016-07-18 20:45:30 -04002713static int mv88e6xxx_g1_setup(struct mv88e6xxx_chip *chip)
Vivien Didelot08a01262016-05-09 13:22:50 -04002714{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002715 struct dsa_switch *ds = chip->ds;
Vivien Didelotb0745e872016-05-09 13:22:53 -04002716 u32 upstream_port = dsa_upstream_port(ds);
Vivien Didelot119477b2016-05-09 13:22:51 -04002717 u16 reg;
Vivien Didelot08a01262016-05-09 13:22:50 -04002718 int err;
Vivien Didelot08a01262016-05-09 13:22:50 -04002719
Vivien Didelot119477b2016-05-09 13:22:51 -04002720 /* Enable the PHY Polling Unit if present, don't discard any packets,
2721 * and mask all interrupt sources.
2722 */
Andrew Lunndc30c352016-10-16 19:56:49 +02002723 err = mv88e6xxx_g1_read(chip, GLOBAL_CONTROL, &reg);
2724 if (err < 0)
2725 return err;
2726
2727 reg &= ~GLOBAL_CONTROL_PPU_ENABLE;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002728 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU) ||
2729 mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE))
Vivien Didelot119477b2016-05-09 13:22:51 -04002730 reg |= GLOBAL_CONTROL_PPU_ENABLE;
2731
Vivien Didelota935c052016-09-29 12:21:53 -04002732 err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL, reg);
Vivien Didelot119477b2016-05-09 13:22:51 -04002733 if (err)
2734 return err;
2735
Vivien Didelotb0745e872016-05-09 13:22:53 -04002736 /* Configure the upstream port, and configure it as the port to which
2737 * ingress and egress and ARP monitor frames are to be sent.
2738 */
2739 reg = upstream_port << GLOBAL_MONITOR_CONTROL_INGRESS_SHIFT |
2740 upstream_port << GLOBAL_MONITOR_CONTROL_EGRESS_SHIFT |
2741 upstream_port << GLOBAL_MONITOR_CONTROL_ARP_SHIFT;
Vivien Didelota935c052016-09-29 12:21:53 -04002742 err = mv88e6xxx_g1_write(chip, GLOBAL_MONITOR_CONTROL, reg);
Vivien Didelotb0745e872016-05-09 13:22:53 -04002743 if (err)
2744 return err;
2745
Vivien Didelot50484ff2016-05-09 13:22:54 -04002746 /* Disable remote management, and set the switch's DSA device number. */
Vivien Didelota935c052016-09-29 12:21:53 -04002747 err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL_2,
2748 GLOBAL_CONTROL_2_MULTIPLE_CASCADE |
2749 (ds->index & 0x1f));
Vivien Didelot50484ff2016-05-09 13:22:54 -04002750 if (err)
2751 return err;
2752
Vivien Didelotacddbd22016-07-18 20:45:39 -04002753 /* Clear all the VTU and STU entries */
2754 err = _mv88e6xxx_vtu_stu_flush(chip);
2755 if (err < 0)
2756 return err;
2757
Vivien Didelot08a01262016-05-09 13:22:50 -04002758 /* Set the default address aging time to 5 minutes, and
2759 * enable address learn messages to be sent to all message
2760 * ports.
2761 */
Vivien Didelota935c052016-09-29 12:21:53 -04002762 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_CONTROL,
2763 GLOBAL_ATU_CONTROL_LEARN2ALL);
Vivien Didelot08a01262016-05-09 13:22:50 -04002764 if (err)
2765 return err;
2766
Vivien Didelotacddbd22016-07-18 20:45:39 -04002767 err = mv88e6xxx_g1_set_age_time(chip, 300000);
2768 if (err)
Vivien Didelot97299342016-07-18 20:45:30 -04002769 return err;
2770
2771 /* Clear all ATU entries */
2772 err = _mv88e6xxx_atu_flush(chip, 0, true);
2773 if (err)
2774 return err;
2775
Vivien Didelot08a01262016-05-09 13:22:50 -04002776 /* Configure the IP ToS mapping registers. */
Vivien Didelota935c052016-09-29 12:21:53 -04002777 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_0, 0x0000);
Vivien Didelot08a01262016-05-09 13:22:50 -04002778 if (err)
2779 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002780 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_1, 0x0000);
Vivien Didelot08a01262016-05-09 13:22:50 -04002781 if (err)
2782 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002783 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_2, 0x5555);
Vivien Didelot08a01262016-05-09 13:22:50 -04002784 if (err)
2785 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002786 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_3, 0x5555);
Vivien Didelot08a01262016-05-09 13:22:50 -04002787 if (err)
2788 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002789 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_4, 0xaaaa);
Vivien Didelot08a01262016-05-09 13:22:50 -04002790 if (err)
2791 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002792 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_5, 0xaaaa);
Vivien Didelot08a01262016-05-09 13:22:50 -04002793 if (err)
2794 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002795 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_6, 0xffff);
Vivien Didelot08a01262016-05-09 13:22:50 -04002796 if (err)
2797 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002798 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_7, 0xffff);
Vivien Didelot08a01262016-05-09 13:22:50 -04002799 if (err)
2800 return err;
2801
2802 /* Configure the IEEE 802.1p priority mapping register. */
Vivien Didelota935c052016-09-29 12:21:53 -04002803 err = mv88e6xxx_g1_write(chip, GLOBAL_IEEE_PRI, 0xfa41);
Vivien Didelot08a01262016-05-09 13:22:50 -04002804 if (err)
2805 return err;
2806
Vivien Didelot97299342016-07-18 20:45:30 -04002807 /* Clear the statistics counters for all ports */
Vivien Didelota935c052016-09-29 12:21:53 -04002808 err = mv88e6xxx_g1_write(chip, GLOBAL_STATS_OP,
2809 GLOBAL_STATS_OP_FLUSH_ALL);
Vivien Didelot97299342016-07-18 20:45:30 -04002810 if (err)
2811 return err;
2812
2813 /* Wait for the flush to complete. */
2814 err = _mv88e6xxx_stats_wait(chip);
2815 if (err)
2816 return err;
2817
2818 return 0;
2819}
2820
Vivien Didelotf81ec902016-05-09 13:22:58 -04002821static int mv88e6xxx_setup(struct dsa_switch *ds)
Guenter Roeckacdaffc2015-03-26 18:36:28 -07002822{
Vivien Didelot04bed142016-08-31 18:06:13 -04002823 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot552238b2016-05-09 13:22:49 -04002824 int err;
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002825 int i;
2826
Vivien Didelotfad09c72016-06-21 12:28:20 -04002827 chip->ds = ds;
2828 ds->slave_mii_bus = chip->mdio_bus;
Vivien Didelot552238b2016-05-09 13:22:49 -04002829
Vivien Didelotfad09c72016-06-21 12:28:20 -04002830 mutex_lock(&chip->reg_lock);
Vivien Didelot552238b2016-05-09 13:22:49 -04002831
Vivien Didelot97299342016-07-18 20:45:30 -04002832 /* Setup Switch Port Registers */
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002833 for (i = 0; i < mv88e6xxx_num_ports(chip); i++) {
Vivien Didelot97299342016-07-18 20:45:30 -04002834 err = mv88e6xxx_setup_port(chip, i);
2835 if (err)
2836 goto unlock;
2837 }
2838
2839 /* Setup Switch Global 1 Registers */
2840 err = mv88e6xxx_g1_setup(chip);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002841 if (err)
2842 goto unlock;
2843
Vivien Didelot97299342016-07-18 20:45:30 -04002844 /* Setup Switch Global 2 Registers */
2845 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_GLOBAL2)) {
2846 err = mv88e6xxx_g2_setup(chip);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002847 if (err)
2848 goto unlock;
2849 }
Andrew Lunn54d792f2015-05-06 01:09:47 +02002850
Vivien Didelot6b17e862015-08-13 12:52:18 -04002851unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002852 mutex_unlock(&chip->reg_lock);
Andrew Lunndb687a52015-06-20 21:31:29 +02002853
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002854 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002855}
2856
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002857static int mv88e6xxx_set_addr(struct dsa_switch *ds, u8 *addr)
2858{
Vivien Didelot04bed142016-08-31 18:06:13 -04002859 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002860 int err;
2861
Vivien Didelotb073d4e2016-09-29 12:22:01 -04002862 if (!chip->info->ops->set_switch_mac)
2863 return -EOPNOTSUPP;
2864
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002865 mutex_lock(&chip->reg_lock);
Vivien Didelotb073d4e2016-09-29 12:22:01 -04002866 err = chip->info->ops->set_switch_mac(chip, addr);
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002867 mutex_unlock(&chip->reg_lock);
2868
2869 return err;
2870}
2871
Vivien Didelote57e5e72016-08-15 17:19:00 -04002872static int mv88e6xxx_mdio_read(struct mii_bus *bus, int phy, int reg)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002873{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002874 struct mv88e6xxx_chip *chip = bus->priv;
Vivien Didelote57e5e72016-08-15 17:19:00 -04002875 u16 val;
2876 int err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002877
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002878 if (phy >= mv88e6xxx_num_ports(chip))
Andrew Lunn158bc062016-04-28 21:24:06 -04002879 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002880
Vivien Didelotfad09c72016-06-21 12:28:20 -04002881 mutex_lock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04002882 err = mv88e6xxx_phy_read(chip, phy, reg, &val);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002883 mutex_unlock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04002884
2885 return err ? err : val;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002886}
2887
Vivien Didelote57e5e72016-08-15 17:19:00 -04002888static int mv88e6xxx_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002889{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002890 struct mv88e6xxx_chip *chip = bus->priv;
Vivien Didelote57e5e72016-08-15 17:19:00 -04002891 int err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002892
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002893 if (phy >= mv88e6xxx_num_ports(chip))
Andrew Lunn158bc062016-04-28 21:24:06 -04002894 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002895
Vivien Didelotfad09c72016-06-21 12:28:20 -04002896 mutex_lock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04002897 err = mv88e6xxx_phy_write(chip, phy, reg, val);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002898 mutex_unlock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04002899
2900 return err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002901}
2902
Vivien Didelotfad09c72016-06-21 12:28:20 -04002903static int mv88e6xxx_mdio_register(struct mv88e6xxx_chip *chip,
Andrew Lunnb516d452016-06-04 21:17:06 +02002904 struct device_node *np)
2905{
2906 static int index;
2907 struct mii_bus *bus;
2908 int err;
2909
Andrew Lunnb516d452016-06-04 21:17:06 +02002910 if (np)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002911 chip->mdio_np = of_get_child_by_name(np, "mdio");
Andrew Lunnb516d452016-06-04 21:17:06 +02002912
Vivien Didelotfad09c72016-06-21 12:28:20 -04002913 bus = devm_mdiobus_alloc(chip->dev);
Andrew Lunnb516d452016-06-04 21:17:06 +02002914 if (!bus)
2915 return -ENOMEM;
2916
Vivien Didelotfad09c72016-06-21 12:28:20 -04002917 bus->priv = (void *)chip;
Andrew Lunnb516d452016-06-04 21:17:06 +02002918 if (np) {
2919 bus->name = np->full_name;
2920 snprintf(bus->id, MII_BUS_ID_SIZE, "%s", np->full_name);
2921 } else {
2922 bus->name = "mv88e6xxx SMI";
2923 snprintf(bus->id, MII_BUS_ID_SIZE, "mv88e6xxx-%d", index++);
2924 }
2925
2926 bus->read = mv88e6xxx_mdio_read;
2927 bus->write = mv88e6xxx_mdio_write;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002928 bus->parent = chip->dev;
Andrew Lunnb516d452016-06-04 21:17:06 +02002929
Vivien Didelotfad09c72016-06-21 12:28:20 -04002930 if (chip->mdio_np)
2931 err = of_mdiobus_register(bus, chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02002932 else
2933 err = mdiobus_register(bus);
2934 if (err) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002935 dev_err(chip->dev, "Cannot register MDIO bus (%d)\n", err);
Andrew Lunnb516d452016-06-04 21:17:06 +02002936 goto out;
2937 }
Vivien Didelotfad09c72016-06-21 12:28:20 -04002938 chip->mdio_bus = bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02002939
2940 return 0;
2941
2942out:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002943 if (chip->mdio_np)
2944 of_node_put(chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02002945
2946 return err;
2947}
2948
Vivien Didelotfad09c72016-06-21 12:28:20 -04002949static void mv88e6xxx_mdio_unregister(struct mv88e6xxx_chip *chip)
Andrew Lunnb516d452016-06-04 21:17:06 +02002950
2951{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002952 struct mii_bus *bus = chip->mdio_bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02002953
2954 mdiobus_unregister(bus);
2955
Vivien Didelotfad09c72016-06-21 12:28:20 -04002956 if (chip->mdio_np)
2957 of_node_put(chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02002958}
2959
Guenter Roeckc22995c2015-07-25 09:42:28 -07002960#ifdef CONFIG_NET_DSA_HWMON
2961
2962static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
2963{
Vivien Didelot04bed142016-08-31 18:06:13 -04002964 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot9c938292016-08-15 17:19:02 -04002965 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07002966 int ret;
Guenter Roeckc22995c2015-07-25 09:42:28 -07002967
2968 *temp = 0;
2969
Vivien Didelotfad09c72016-06-21 12:28:20 -04002970 mutex_lock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07002971
Vivien Didelot9c938292016-08-15 17:19:02 -04002972 ret = mv88e6xxx_phy_write(chip, 0x0, 0x16, 0x6);
Guenter Roeckc22995c2015-07-25 09:42:28 -07002973 if (ret < 0)
2974 goto error;
2975
2976 /* Enable temperature sensor */
Vivien Didelot9c938292016-08-15 17:19:02 -04002977 ret = mv88e6xxx_phy_read(chip, 0x0, 0x1a, &val);
Guenter Roeckc22995c2015-07-25 09:42:28 -07002978 if (ret < 0)
2979 goto error;
2980
Vivien Didelot9c938292016-08-15 17:19:02 -04002981 ret = mv88e6xxx_phy_write(chip, 0x0, 0x1a, val | (1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07002982 if (ret < 0)
2983 goto error;
2984
2985 /* Wait for temperature to stabilize */
2986 usleep_range(10000, 12000);
2987
Vivien Didelot9c938292016-08-15 17:19:02 -04002988 ret = mv88e6xxx_phy_read(chip, 0x0, 0x1a, &val);
2989 if (ret < 0)
Guenter Roeckc22995c2015-07-25 09:42:28 -07002990 goto error;
Guenter Roeckc22995c2015-07-25 09:42:28 -07002991
2992 /* Disable temperature sensor */
Vivien Didelot9c938292016-08-15 17:19:02 -04002993 ret = mv88e6xxx_phy_write(chip, 0x0, 0x1a, val & ~(1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07002994 if (ret < 0)
2995 goto error;
2996
2997 *temp = ((val & 0x1f) - 5) * 5;
2998
2999error:
Vivien Didelot9c938292016-08-15 17:19:02 -04003000 mv88e6xxx_phy_write(chip, 0x0, 0x16, 0x0);
Vivien Didelotfad09c72016-06-21 12:28:20 -04003001 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003002 return ret;
3003}
3004
3005static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
3006{
Vivien Didelot04bed142016-08-31 18:06:13 -04003007 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003008 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003009 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003010 int ret;
3011
3012 *temp = 0;
3013
Vivien Didelot9c938292016-08-15 17:19:02 -04003014 mutex_lock(&chip->reg_lock);
3015 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 27, &val);
3016 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003017 if (ret < 0)
3018 return ret;
3019
Vivien Didelot9c938292016-08-15 17:19:02 -04003020 *temp = (val & 0xff) - 25;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003021
3022 return 0;
3023}
3024
Vivien Didelotf81ec902016-05-09 13:22:58 -04003025static int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003026{
Vivien Didelot04bed142016-08-31 18:06:13 -04003027 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn158bc062016-04-28 21:24:06 -04003028
Vivien Didelotfad09c72016-06-21 12:28:20 -04003029 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP))
Vivien Didelot6594f612016-05-09 13:22:42 -04003030 return -EOPNOTSUPP;
3031
Vivien Didelotfad09c72016-06-21 12:28:20 -04003032 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003033 return mv88e63xx_get_temp(ds, temp);
3034
3035 return mv88e61xx_get_temp(ds, temp);
3036}
3037
Vivien Didelotf81ec902016-05-09 13:22:58 -04003038static int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003039{
Vivien Didelot04bed142016-08-31 18:06:13 -04003040 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003041 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003042 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003043 int ret;
3044
Vivien Didelotfad09c72016-06-21 12:28:20 -04003045 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003046 return -EOPNOTSUPP;
3047
3048 *temp = 0;
3049
Vivien Didelot9c938292016-08-15 17:19:02 -04003050 mutex_lock(&chip->reg_lock);
3051 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3052 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003053 if (ret < 0)
3054 return ret;
3055
Vivien Didelot9c938292016-08-15 17:19:02 -04003056 *temp = (((val >> 8) & 0x1f) * 5) - 25;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003057
3058 return 0;
3059}
3060
Vivien Didelotf81ec902016-05-09 13:22:58 -04003061static int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003062{
Vivien Didelot04bed142016-08-31 18:06:13 -04003063 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003064 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003065 u16 val;
3066 int err;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003067
Vivien Didelotfad09c72016-06-21 12:28:20 -04003068 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003069 return -EOPNOTSUPP;
3070
Vivien Didelot9c938292016-08-15 17:19:02 -04003071 mutex_lock(&chip->reg_lock);
3072 err = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3073 if (err)
3074 goto unlock;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003075 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
Vivien Didelot9c938292016-08-15 17:19:02 -04003076 err = mv88e6xxx_phy_page_write(chip, phy, 6, 26,
3077 (val & 0xe0ff) | (temp << 8));
3078unlock:
3079 mutex_unlock(&chip->reg_lock);
3080
3081 return err;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003082}
3083
Vivien Didelotf81ec902016-05-09 13:22:58 -04003084static int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003085{
Vivien Didelot04bed142016-08-31 18:06:13 -04003086 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003087 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003088 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003089 int ret;
3090
Vivien Didelotfad09c72016-06-21 12:28:20 -04003091 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003092 return -EOPNOTSUPP;
3093
3094 *alarm = false;
3095
Vivien Didelot9c938292016-08-15 17:19:02 -04003096 mutex_lock(&chip->reg_lock);
3097 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3098 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003099 if (ret < 0)
3100 return ret;
3101
Vivien Didelot9c938292016-08-15 17:19:02 -04003102 *alarm = !!(val & 0x40);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003103
3104 return 0;
3105}
3106#endif /* CONFIG_NET_DSA_HWMON */
3107
Vivien Didelot855b1932016-07-20 18:18:35 -04003108static int mv88e6xxx_get_eeprom_len(struct dsa_switch *ds)
3109{
Vivien Didelot04bed142016-08-31 18:06:13 -04003110 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot855b1932016-07-20 18:18:35 -04003111
3112 return chip->eeprom_len;
3113}
3114
Vivien Didelot855b1932016-07-20 18:18:35 -04003115static int mv88e6xxx_get_eeprom(struct dsa_switch *ds,
3116 struct ethtool_eeprom *eeprom, u8 *data)
3117{
Vivien Didelot04bed142016-08-31 18:06:13 -04003118 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot855b1932016-07-20 18:18:35 -04003119 int err;
3120
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003121 if (!chip->info->ops->get_eeprom)
3122 return -EOPNOTSUPP;
3123
Vivien Didelot855b1932016-07-20 18:18:35 -04003124 mutex_lock(&chip->reg_lock);
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003125 err = chip->info->ops->get_eeprom(chip, eeprom, data);
Vivien Didelot855b1932016-07-20 18:18:35 -04003126 mutex_unlock(&chip->reg_lock);
3127
3128 if (err)
3129 return err;
3130
3131 eeprom->magic = 0xc3ec4951;
3132
3133 return 0;
3134}
3135
Vivien Didelot855b1932016-07-20 18:18:35 -04003136static int mv88e6xxx_set_eeprom(struct dsa_switch *ds,
3137 struct ethtool_eeprom *eeprom, u8 *data)
3138{
Vivien Didelot04bed142016-08-31 18:06:13 -04003139 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot855b1932016-07-20 18:18:35 -04003140 int err;
3141
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003142 if (!chip->info->ops->set_eeprom)
3143 return -EOPNOTSUPP;
3144
Vivien Didelot855b1932016-07-20 18:18:35 -04003145 if (eeprom->magic != 0xc3ec4951)
3146 return -EINVAL;
3147
3148 mutex_lock(&chip->reg_lock);
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003149 err = chip->info->ops->set_eeprom(chip, eeprom, data);
Vivien Didelot855b1932016-07-20 18:18:35 -04003150 mutex_unlock(&chip->reg_lock);
3151
3152 return err;
3153}
3154
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003155static const struct mv88e6xxx_ops mv88e6085_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003156 .set_switch_mac = mv88e6xxx_g1_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003157 .phy_read = mv88e6xxx_phy_ppu_read,
3158 .phy_write = mv88e6xxx_phy_ppu_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003159 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003160 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003161 .port_set_speed = mv88e6185_port_set_speed,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003162};
3163
3164static const struct mv88e6xxx_ops mv88e6095_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003165 .set_switch_mac = mv88e6xxx_g1_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003166 .phy_read = mv88e6xxx_phy_ppu_read,
3167 .phy_write = mv88e6xxx_phy_ppu_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003168 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003169 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003170 .port_set_speed = mv88e6185_port_set_speed,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003171};
3172
3173static const struct mv88e6xxx_ops mv88e6123_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003174 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003175 .phy_read = mv88e6xxx_read,
3176 .phy_write = mv88e6xxx_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003177 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003178 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003179 .port_set_speed = mv88e6185_port_set_speed,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003180};
3181
3182static const struct mv88e6xxx_ops mv88e6131_ops = {
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,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003189};
3190
3191static const struct mv88e6xxx_ops mv88e6161_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003192 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003193 .phy_read = mv88e6xxx_read,
3194 .phy_write = mv88e6xxx_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003195 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003196 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003197 .port_set_speed = mv88e6185_port_set_speed,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003198};
3199
3200static const struct mv88e6xxx_ops mv88e6165_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003201 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003202 .phy_read = mv88e6xxx_read,
3203 .phy_write = mv88e6xxx_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003204 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003205 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003206 .port_set_speed = mv88e6185_port_set_speed,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003207};
3208
3209static const struct mv88e6xxx_ops mv88e6171_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003210 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003211 .phy_read = mv88e6xxx_g2_smi_phy_read,
3212 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003213 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003214 .port_set_duplex = mv88e6xxx_port_set_duplex,
Andrew Lunn94d66ae2016-11-10 15:44:01 +01003215 .port_set_rgmii_delay = mv88e6352_port_set_rgmii_delay,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003216 .port_set_speed = mv88e6185_port_set_speed,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003217};
3218
3219static const struct mv88e6xxx_ops mv88e6172_ops = {
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003220 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3221 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003222 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003223 .phy_read = mv88e6xxx_g2_smi_phy_read,
3224 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003225 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003226 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelota0a0f622016-11-04 03:23:34 +01003227 .port_set_rgmii_delay = mv88e6352_port_set_rgmii_delay,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003228 .port_set_speed = mv88e6352_port_set_speed,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003229};
3230
3231static const struct mv88e6xxx_ops mv88e6175_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003232 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003233 .phy_read = mv88e6xxx_g2_smi_phy_read,
3234 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003235 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003236 .port_set_duplex = mv88e6xxx_port_set_duplex,
Andrew Lunn94d66ae2016-11-10 15:44:01 +01003237 .port_set_rgmii_delay = mv88e6352_port_set_rgmii_delay,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003238 .port_set_speed = mv88e6185_port_set_speed,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003239};
3240
3241static const struct mv88e6xxx_ops mv88e6176_ops = {
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003242 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3243 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003244 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003245 .phy_read = mv88e6xxx_g2_smi_phy_read,
3246 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003247 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003248 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelota0a0f622016-11-04 03:23:34 +01003249 .port_set_rgmii_delay = mv88e6352_port_set_rgmii_delay,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003250 .port_set_speed = mv88e6352_port_set_speed,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003251};
3252
3253static const struct mv88e6xxx_ops mv88e6185_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003254 .set_switch_mac = mv88e6xxx_g1_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003255 .phy_read = mv88e6xxx_phy_ppu_read,
3256 .phy_write = mv88e6xxx_phy_ppu_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003257 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003258 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003259 .port_set_speed = mv88e6185_port_set_speed,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003260};
3261
3262static const struct mv88e6xxx_ops mv88e6240_ops = {
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003263 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3264 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003265 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003266 .phy_read = mv88e6xxx_g2_smi_phy_read,
3267 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003268 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003269 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelota0a0f622016-11-04 03:23:34 +01003270 .port_set_rgmii_delay = mv88e6352_port_set_rgmii_delay,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003271 .port_set_speed = mv88e6352_port_set_speed,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003272};
3273
3274static const struct mv88e6xxx_ops mv88e6320_ops = {
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003275 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3276 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003277 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003278 .phy_read = mv88e6xxx_g2_smi_phy_read,
3279 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003280 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003281 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003282 .port_set_speed = mv88e6185_port_set_speed,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003283};
3284
3285static const struct mv88e6xxx_ops mv88e6321_ops = {
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003286 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3287 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003288 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003289 .phy_read = mv88e6xxx_g2_smi_phy_read,
3290 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003291 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003292 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003293 .port_set_speed = mv88e6185_port_set_speed,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003294};
3295
3296static const struct mv88e6xxx_ops mv88e6350_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003297 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003298 .phy_read = mv88e6xxx_g2_smi_phy_read,
3299 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003300 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003301 .port_set_duplex = mv88e6xxx_port_set_duplex,
Andrew Lunn94d66ae2016-11-10 15:44:01 +01003302 .port_set_rgmii_delay = mv88e6352_port_set_rgmii_delay,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003303 .port_set_speed = mv88e6185_port_set_speed,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003304};
3305
3306static const struct mv88e6xxx_ops mv88e6351_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003307 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003308 .phy_read = mv88e6xxx_g2_smi_phy_read,
3309 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003310 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003311 .port_set_duplex = mv88e6xxx_port_set_duplex,
Andrew Lunn94d66ae2016-11-10 15:44:01 +01003312 .port_set_rgmii_delay = mv88e6352_port_set_rgmii_delay,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003313 .port_set_speed = mv88e6185_port_set_speed,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003314};
3315
3316static const struct mv88e6xxx_ops mv88e6352_ops = {
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003317 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3318 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003319 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003320 .phy_read = mv88e6xxx_g2_smi_phy_read,
3321 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003322 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003323 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelota0a0f622016-11-04 03:23:34 +01003324 .port_set_rgmii_delay = mv88e6352_port_set_rgmii_delay,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003325 .port_set_speed = mv88e6352_port_set_speed,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003326};
3327
Vivien Didelotf81ec902016-05-09 13:22:58 -04003328static const struct mv88e6xxx_info mv88e6xxx_table[] = {
3329 [MV88E6085] = {
3330 .prod_num = PORT_SWITCH_ID_PROD_NUM_6085,
3331 .family = MV88E6XXX_FAMILY_6097,
3332 .name = "Marvell 88E6085",
3333 .num_databases = 4096,
3334 .num_ports = 10,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003335 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003336 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003337 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003338 .g1_irqs = 8,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003339 .flags = MV88E6XXX_FLAGS_FAMILY_6097,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003340 .ops = &mv88e6085_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003341 },
3342
3343 [MV88E6095] = {
3344 .prod_num = PORT_SWITCH_ID_PROD_NUM_6095,
3345 .family = MV88E6XXX_FAMILY_6095,
3346 .name = "Marvell 88E6095/88E6095F",
3347 .num_databases = 256,
3348 .num_ports = 11,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003349 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003350 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003351 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003352 .g1_irqs = 8,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003353 .flags = MV88E6XXX_FLAGS_FAMILY_6095,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003354 .ops = &mv88e6095_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003355 },
3356
3357 [MV88E6123] = {
3358 .prod_num = PORT_SWITCH_ID_PROD_NUM_6123,
3359 .family = MV88E6XXX_FAMILY_6165,
3360 .name = "Marvell 88E6123",
3361 .num_databases = 4096,
3362 .num_ports = 3,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003363 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003364 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003365 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003366 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003367 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003368 .ops = &mv88e6123_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003369 },
3370
3371 [MV88E6131] = {
3372 .prod_num = PORT_SWITCH_ID_PROD_NUM_6131,
3373 .family = MV88E6XXX_FAMILY_6185,
3374 .name = "Marvell 88E6131",
3375 .num_databases = 256,
3376 .num_ports = 8,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003377 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003378 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003379 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003380 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003381 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003382 .ops = &mv88e6131_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003383 },
3384
3385 [MV88E6161] = {
3386 .prod_num = PORT_SWITCH_ID_PROD_NUM_6161,
3387 .family = MV88E6XXX_FAMILY_6165,
3388 .name = "Marvell 88E6161",
3389 .num_databases = 4096,
3390 .num_ports = 6,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003391 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003392 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003393 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003394 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003395 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003396 .ops = &mv88e6161_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003397 },
3398
3399 [MV88E6165] = {
3400 .prod_num = PORT_SWITCH_ID_PROD_NUM_6165,
3401 .family = MV88E6XXX_FAMILY_6165,
3402 .name = "Marvell 88E6165",
3403 .num_databases = 4096,
3404 .num_ports = 6,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003405 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003406 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003407 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003408 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003409 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003410 .ops = &mv88e6165_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003411 },
3412
3413 [MV88E6171] = {
3414 .prod_num = PORT_SWITCH_ID_PROD_NUM_6171,
3415 .family = MV88E6XXX_FAMILY_6351,
3416 .name = "Marvell 88E6171",
3417 .num_databases = 4096,
3418 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003419 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003420 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003421 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003422 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003423 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003424 .ops = &mv88e6171_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003425 },
3426
3427 [MV88E6172] = {
3428 .prod_num = PORT_SWITCH_ID_PROD_NUM_6172,
3429 .family = MV88E6XXX_FAMILY_6352,
3430 .name = "Marvell 88E6172",
3431 .num_databases = 4096,
3432 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003433 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003434 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003435 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003436 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003437 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003438 .ops = &mv88e6172_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003439 },
3440
3441 [MV88E6175] = {
3442 .prod_num = PORT_SWITCH_ID_PROD_NUM_6175,
3443 .family = MV88E6XXX_FAMILY_6351,
3444 .name = "Marvell 88E6175",
3445 .num_databases = 4096,
3446 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003447 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003448 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003449 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003450 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003451 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003452 .ops = &mv88e6175_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003453 },
3454
3455 [MV88E6176] = {
3456 .prod_num = PORT_SWITCH_ID_PROD_NUM_6176,
3457 .family = MV88E6XXX_FAMILY_6352,
3458 .name = "Marvell 88E6176",
3459 .num_databases = 4096,
3460 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003461 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003462 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003463 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003464 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003465 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003466 .ops = &mv88e6176_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003467 },
3468
3469 [MV88E6185] = {
3470 .prod_num = PORT_SWITCH_ID_PROD_NUM_6185,
3471 .family = MV88E6XXX_FAMILY_6185,
3472 .name = "Marvell 88E6185",
3473 .num_databases = 256,
3474 .num_ports = 10,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003475 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003476 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003477 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003478 .g1_irqs = 8,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003479 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003480 .ops = &mv88e6185_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003481 },
3482
3483 [MV88E6240] = {
3484 .prod_num = PORT_SWITCH_ID_PROD_NUM_6240,
3485 .family = MV88E6XXX_FAMILY_6352,
3486 .name = "Marvell 88E6240",
3487 .num_databases = 4096,
3488 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003489 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003490 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003491 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003492 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003493 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003494 .ops = &mv88e6240_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003495 },
3496
3497 [MV88E6320] = {
3498 .prod_num = PORT_SWITCH_ID_PROD_NUM_6320,
3499 .family = MV88E6XXX_FAMILY_6320,
3500 .name = "Marvell 88E6320",
3501 .num_databases = 4096,
3502 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003503 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003504 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003505 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003506 .g1_irqs = 8,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003507 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003508 .ops = &mv88e6320_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003509 },
3510
3511 [MV88E6321] = {
3512 .prod_num = PORT_SWITCH_ID_PROD_NUM_6321,
3513 .family = MV88E6XXX_FAMILY_6320,
3514 .name = "Marvell 88E6321",
3515 .num_databases = 4096,
3516 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003517 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003518 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003519 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003520 .g1_irqs = 8,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003521 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003522 .ops = &mv88e6321_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003523 },
3524
3525 [MV88E6350] = {
3526 .prod_num = PORT_SWITCH_ID_PROD_NUM_6350,
3527 .family = MV88E6XXX_FAMILY_6351,
3528 .name = "Marvell 88E6350",
3529 .num_databases = 4096,
3530 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003531 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003532 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003533 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003534 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003535 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003536 .ops = &mv88e6350_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003537 },
3538
3539 [MV88E6351] = {
3540 .prod_num = PORT_SWITCH_ID_PROD_NUM_6351,
3541 .family = MV88E6XXX_FAMILY_6351,
3542 .name = "Marvell 88E6351",
3543 .num_databases = 4096,
3544 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003545 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003546 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003547 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003548 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003549 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003550 .ops = &mv88e6351_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003551 },
3552
3553 [MV88E6352] = {
3554 .prod_num = PORT_SWITCH_ID_PROD_NUM_6352,
3555 .family = MV88E6XXX_FAMILY_6352,
3556 .name = "Marvell 88E6352",
3557 .num_databases = 4096,
3558 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003559 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003560 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003561 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003562 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003563 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003564 .ops = &mv88e6352_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003565 },
3566};
3567
Vivien Didelot5f7c0362016-06-20 13:14:04 -04003568static const struct mv88e6xxx_info *mv88e6xxx_lookup_info(unsigned int prod_num)
Vivien Didelotb9b37712015-10-30 19:39:48 -04003569{
Vivien Didelota439c062016-04-17 13:23:58 -04003570 int i;
Vivien Didelotb9b37712015-10-30 19:39:48 -04003571
Vivien Didelot5f7c0362016-06-20 13:14:04 -04003572 for (i = 0; i < ARRAY_SIZE(mv88e6xxx_table); ++i)
3573 if (mv88e6xxx_table[i].prod_num == prod_num)
3574 return &mv88e6xxx_table[i];
Vivien Didelotb9b37712015-10-30 19:39:48 -04003575
Vivien Didelotb9b37712015-10-30 19:39:48 -04003576 return NULL;
3577}
3578
Vivien Didelotfad09c72016-06-21 12:28:20 -04003579static int mv88e6xxx_detect(struct mv88e6xxx_chip *chip)
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003580{
3581 const struct mv88e6xxx_info *info;
Vivien Didelot8f6345b2016-07-20 18:18:36 -04003582 unsigned int prod_num, rev;
3583 u16 id;
3584 int err;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003585
Vivien Didelot8f6345b2016-07-20 18:18:36 -04003586 mutex_lock(&chip->reg_lock);
3587 err = mv88e6xxx_port_read(chip, 0, PORT_SWITCH_ID, &id);
3588 mutex_unlock(&chip->reg_lock);
3589 if (err)
3590 return err;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003591
3592 prod_num = (id & 0xfff0) >> 4;
3593 rev = id & 0x000f;
3594
3595 info = mv88e6xxx_lookup_info(prod_num);
3596 if (!info)
3597 return -ENODEV;
3598
Vivien Didelotcaac8542016-06-20 13:14:09 -04003599 /* Update the compatible info with the probed one */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003600 chip->info = info;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003601
Vivien Didelotca070c12016-09-02 14:45:34 -04003602 err = mv88e6xxx_g2_require(chip);
3603 if (err)
3604 return err;
3605
Vivien Didelotfad09c72016-06-21 12:28:20 -04003606 dev_info(chip->dev, "switch 0x%x detected: %s, revision %u\n",
3607 chip->info->prod_num, chip->info->name, rev);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003608
3609 return 0;
3610}
3611
Vivien Didelotfad09c72016-06-21 12:28:20 -04003612static struct mv88e6xxx_chip *mv88e6xxx_alloc_chip(struct device *dev)
Vivien Didelot469d7292016-06-20 13:14:06 -04003613{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003614 struct mv88e6xxx_chip *chip;
Vivien Didelot469d7292016-06-20 13:14:06 -04003615
Vivien Didelotfad09c72016-06-21 12:28:20 -04003616 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
3617 if (!chip)
Vivien Didelot469d7292016-06-20 13:14:06 -04003618 return NULL;
3619
Vivien Didelotfad09c72016-06-21 12:28:20 -04003620 chip->dev = dev;
Vivien Didelot469d7292016-06-20 13:14:06 -04003621
Vivien Didelotfad09c72016-06-21 12:28:20 -04003622 mutex_init(&chip->reg_lock);
Vivien Didelot469d7292016-06-20 13:14:06 -04003623
Vivien Didelotfad09c72016-06-21 12:28:20 -04003624 return chip;
Vivien Didelot469d7292016-06-20 13:14:06 -04003625}
3626
Vivien Didelote57e5e72016-08-15 17:19:00 -04003627static void mv88e6xxx_phy_init(struct mv88e6xxx_chip *chip)
3628{
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003629 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU))
Vivien Didelote57e5e72016-08-15 17:19:00 -04003630 mv88e6xxx_ppu_state_init(chip);
Vivien Didelote57e5e72016-08-15 17:19:00 -04003631}
3632
Andrew Lunn930188c2016-08-22 16:01:03 +02003633static void mv88e6xxx_phy_destroy(struct mv88e6xxx_chip *chip)
3634{
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003635 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU))
Andrew Lunn930188c2016-08-22 16:01:03 +02003636 mv88e6xxx_ppu_state_destroy(chip);
Andrew Lunn930188c2016-08-22 16:01:03 +02003637}
3638
Vivien Didelotfad09c72016-06-21 12:28:20 -04003639static int mv88e6xxx_smi_init(struct mv88e6xxx_chip *chip,
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003640 struct mii_bus *bus, int sw_addr)
3641{
3642 /* ADDR[0] pin is unavailable externally and considered zero */
3643 if (sw_addr & 0x1)
3644 return -EINVAL;
3645
Vivien Didelot914b32f2016-06-20 13:14:11 -04003646 if (sw_addr == 0)
Vivien Didelotfad09c72016-06-21 12:28:20 -04003647 chip->smi_ops = &mv88e6xxx_smi_single_chip_ops;
Vivien Didelota0ffff22016-08-15 17:18:58 -04003648 else if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_MULTI_CHIP))
Vivien Didelotfad09c72016-06-21 12:28:20 -04003649 chip->smi_ops = &mv88e6xxx_smi_multi_chip_ops;
Vivien Didelot914b32f2016-06-20 13:14:11 -04003650 else
3651 return -EINVAL;
3652
Vivien Didelotfad09c72016-06-21 12:28:20 -04003653 chip->bus = bus;
3654 chip->sw_addr = sw_addr;
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003655
3656 return 0;
3657}
3658
Andrew Lunn7b314362016-08-22 16:01:01 +02003659static enum dsa_tag_protocol mv88e6xxx_get_tag_protocol(struct dsa_switch *ds)
3660{
Vivien Didelot04bed142016-08-31 18:06:13 -04003661 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02003662
3663 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA))
3664 return DSA_TAG_PROTO_EDSA;
3665
3666 return DSA_TAG_PROTO_DSA;
Andrew Lunn7b314362016-08-22 16:01:01 +02003667}
3668
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02003669static const char *mv88e6xxx_drv_probe(struct device *dsa_dev,
3670 struct device *host_dev, int sw_addr,
3671 void **priv)
Andrew Lunna77d43f2016-04-13 02:40:42 +02003672{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003673 struct mv88e6xxx_chip *chip;
Vivien Didelota439c062016-04-17 13:23:58 -04003674 struct mii_bus *bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003675 int err;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003676
Vivien Didelota439c062016-04-17 13:23:58 -04003677 bus = dsa_host_dev_to_mii_bus(host_dev);
Andrew Lunnc1569132016-04-13 02:40:45 +02003678 if (!bus)
3679 return NULL;
3680
Vivien Didelotfad09c72016-06-21 12:28:20 -04003681 chip = mv88e6xxx_alloc_chip(dsa_dev);
3682 if (!chip)
Vivien Didelot469d7292016-06-20 13:14:06 -04003683 return NULL;
3684
Vivien Didelotcaac8542016-06-20 13:14:09 -04003685 /* Legacy SMI probing will only support chips similar to 88E6085 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003686 chip->info = &mv88e6xxx_table[MV88E6085];
Vivien Didelotcaac8542016-06-20 13:14:09 -04003687
Vivien Didelotfad09c72016-06-21 12:28:20 -04003688 err = mv88e6xxx_smi_init(chip, bus, sw_addr);
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003689 if (err)
3690 goto free;
3691
Vivien Didelotfad09c72016-06-21 12:28:20 -04003692 err = mv88e6xxx_detect(chip);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003693 if (err)
Vivien Didelot469d7292016-06-20 13:14:06 -04003694 goto free;
Vivien Didelota439c062016-04-17 13:23:58 -04003695
Andrew Lunndc30c352016-10-16 19:56:49 +02003696 mutex_lock(&chip->reg_lock);
3697 err = mv88e6xxx_switch_reset(chip);
3698 mutex_unlock(&chip->reg_lock);
3699 if (err)
3700 goto free;
3701
Vivien Didelote57e5e72016-08-15 17:19:00 -04003702 mv88e6xxx_phy_init(chip);
3703
Vivien Didelotfad09c72016-06-21 12:28:20 -04003704 err = mv88e6xxx_mdio_register(chip, NULL);
Andrew Lunnb516d452016-06-04 21:17:06 +02003705 if (err)
Vivien Didelot469d7292016-06-20 13:14:06 -04003706 goto free;
Andrew Lunnb516d452016-06-04 21:17:06 +02003707
Vivien Didelotfad09c72016-06-21 12:28:20 -04003708 *priv = chip;
Vivien Didelota439c062016-04-17 13:23:58 -04003709
Vivien Didelotfad09c72016-06-21 12:28:20 -04003710 return chip->info->name;
Vivien Didelot469d7292016-06-20 13:14:06 -04003711free:
Vivien Didelotfad09c72016-06-21 12:28:20 -04003712 devm_kfree(dsa_dev, chip);
Vivien Didelot469d7292016-06-20 13:14:06 -04003713
3714 return NULL;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003715}
3716
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003717static int mv88e6xxx_port_mdb_prepare(struct dsa_switch *ds, int port,
3718 const struct switchdev_obj_port_mdb *mdb,
3719 struct switchdev_trans *trans)
3720{
3721 /* We don't need any dynamic resource from the kernel (yet),
3722 * so skip the prepare phase.
3723 */
3724
3725 return 0;
3726}
3727
3728static void mv88e6xxx_port_mdb_add(struct dsa_switch *ds, int port,
3729 const struct switchdev_obj_port_mdb *mdb,
3730 struct switchdev_trans *trans)
3731{
Vivien Didelot04bed142016-08-31 18:06:13 -04003732 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003733
3734 mutex_lock(&chip->reg_lock);
3735 if (mv88e6xxx_port_db_load_purge(chip, port, mdb->addr, mdb->vid,
3736 GLOBAL_ATU_DATA_STATE_MC_STATIC))
3737 netdev_err(ds->ports[port].netdev, "failed to load multicast MAC address\n");
3738 mutex_unlock(&chip->reg_lock);
3739}
3740
3741static int mv88e6xxx_port_mdb_del(struct dsa_switch *ds, int port,
3742 const struct switchdev_obj_port_mdb *mdb)
3743{
Vivien Didelot04bed142016-08-31 18:06:13 -04003744 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003745 int err;
3746
3747 mutex_lock(&chip->reg_lock);
3748 err = mv88e6xxx_port_db_load_purge(chip, port, mdb->addr, mdb->vid,
3749 GLOBAL_ATU_DATA_STATE_UNUSED);
3750 mutex_unlock(&chip->reg_lock);
3751
3752 return err;
3753}
3754
3755static int mv88e6xxx_port_mdb_dump(struct dsa_switch *ds, int port,
3756 struct switchdev_obj_port_mdb *mdb,
3757 int (*cb)(struct switchdev_obj *obj))
3758{
Vivien Didelot04bed142016-08-31 18:06:13 -04003759 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003760 int err;
3761
3762 mutex_lock(&chip->reg_lock);
3763 err = mv88e6xxx_port_db_dump(chip, port, &mdb->obj, cb);
3764 mutex_unlock(&chip->reg_lock);
3765
3766 return err;
3767}
3768
Vivien Didelot9d490b42016-08-23 12:38:56 -04003769static struct dsa_switch_ops mv88e6xxx_switch_ops = {
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02003770 .probe = mv88e6xxx_drv_probe,
Andrew Lunn7b314362016-08-22 16:01:01 +02003771 .get_tag_protocol = mv88e6xxx_get_tag_protocol,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003772 .setup = mv88e6xxx_setup,
3773 .set_addr = mv88e6xxx_set_addr,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003774 .adjust_link = mv88e6xxx_adjust_link,
3775 .get_strings = mv88e6xxx_get_strings,
3776 .get_ethtool_stats = mv88e6xxx_get_ethtool_stats,
3777 .get_sset_count = mv88e6xxx_get_sset_count,
3778 .set_eee = mv88e6xxx_set_eee,
3779 .get_eee = mv88e6xxx_get_eee,
3780#ifdef CONFIG_NET_DSA_HWMON
3781 .get_temp = mv88e6xxx_get_temp,
3782 .get_temp_limit = mv88e6xxx_get_temp_limit,
3783 .set_temp_limit = mv88e6xxx_set_temp_limit,
3784 .get_temp_alarm = mv88e6xxx_get_temp_alarm,
3785#endif
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003786 .get_eeprom_len = mv88e6xxx_get_eeprom_len,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003787 .get_eeprom = mv88e6xxx_get_eeprom,
3788 .set_eeprom = mv88e6xxx_set_eeprom,
3789 .get_regs_len = mv88e6xxx_get_regs_len,
3790 .get_regs = mv88e6xxx_get_regs,
Vivien Didelot2cfcd962016-07-18 20:45:40 -04003791 .set_ageing_time = mv88e6xxx_set_ageing_time,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003792 .port_bridge_join = mv88e6xxx_port_bridge_join,
3793 .port_bridge_leave = mv88e6xxx_port_bridge_leave,
3794 .port_stp_state_set = mv88e6xxx_port_stp_state_set,
Vivien Didelot749efcb2016-09-22 16:49:24 -04003795 .port_fast_age = mv88e6xxx_port_fast_age,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003796 .port_vlan_filtering = mv88e6xxx_port_vlan_filtering,
3797 .port_vlan_prepare = mv88e6xxx_port_vlan_prepare,
3798 .port_vlan_add = mv88e6xxx_port_vlan_add,
3799 .port_vlan_del = mv88e6xxx_port_vlan_del,
3800 .port_vlan_dump = mv88e6xxx_port_vlan_dump,
3801 .port_fdb_prepare = mv88e6xxx_port_fdb_prepare,
3802 .port_fdb_add = mv88e6xxx_port_fdb_add,
3803 .port_fdb_del = mv88e6xxx_port_fdb_del,
3804 .port_fdb_dump = mv88e6xxx_port_fdb_dump,
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003805 .port_mdb_prepare = mv88e6xxx_port_mdb_prepare,
3806 .port_mdb_add = mv88e6xxx_port_mdb_add,
3807 .port_mdb_del = mv88e6xxx_port_mdb_del,
3808 .port_mdb_dump = mv88e6xxx_port_mdb_dump,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003809};
3810
Vivien Didelotfad09c72016-06-21 12:28:20 -04003811static int mv88e6xxx_register_switch(struct mv88e6xxx_chip *chip,
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003812 struct device_node *np)
3813{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003814 struct device *dev = chip->dev;
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003815 struct dsa_switch *ds;
3816
3817 ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
3818 if (!ds)
3819 return -ENOMEM;
3820
3821 ds->dev = dev;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003822 ds->priv = chip;
Vivien Didelot9d490b42016-08-23 12:38:56 -04003823 ds->ops = &mv88e6xxx_switch_ops;
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003824
3825 dev_set_drvdata(dev, ds);
3826
3827 return dsa_register_switch(ds, np);
3828}
3829
Vivien Didelotfad09c72016-06-21 12:28:20 -04003830static void mv88e6xxx_unregister_switch(struct mv88e6xxx_chip *chip)
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003831{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003832 dsa_unregister_switch(chip->ds);
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003833}
3834
Vivien Didelot57d32312016-06-20 13:13:58 -04003835static int mv88e6xxx_probe(struct mdio_device *mdiodev)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003836{
3837 struct device *dev = &mdiodev->dev;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003838 struct device_node *np = dev->of_node;
Vivien Didelotcaac8542016-06-20 13:14:09 -04003839 const struct mv88e6xxx_info *compat_info;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003840 struct mv88e6xxx_chip *chip;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003841 u32 eeprom_len;
Andrew Lunn52638f72016-05-10 23:27:22 +02003842 int err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003843
Vivien Didelotcaac8542016-06-20 13:14:09 -04003844 compat_info = of_device_get_match_data(dev);
3845 if (!compat_info)
3846 return -EINVAL;
3847
Vivien Didelotfad09c72016-06-21 12:28:20 -04003848 chip = mv88e6xxx_alloc_chip(dev);
3849 if (!chip)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003850 return -ENOMEM;
3851
Vivien Didelotfad09c72016-06-21 12:28:20 -04003852 chip->info = compat_info;
Vivien Didelotcaac8542016-06-20 13:14:09 -04003853
Vivien Didelotfad09c72016-06-21 12:28:20 -04003854 err = mv88e6xxx_smi_init(chip, mdiodev->bus, mdiodev->addr);
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003855 if (err)
3856 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003857
Andrew Lunnb4308f02016-11-21 23:26:55 +01003858 chip->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
3859 if (IS_ERR(chip->reset))
3860 return PTR_ERR(chip->reset);
3861
Vivien Didelotfad09c72016-06-21 12:28:20 -04003862 err = mv88e6xxx_detect(chip);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003863 if (err)
3864 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003865
Vivien Didelote57e5e72016-08-15 17:19:00 -04003866 mv88e6xxx_phy_init(chip);
3867
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003868 if (chip->info->ops->get_eeprom &&
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003869 !of_property_read_u32(np, "eeprom-length", &eeprom_len))
Vivien Didelotfad09c72016-06-21 12:28:20 -04003870 chip->eeprom_len = eeprom_len;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003871
Andrew Lunndc30c352016-10-16 19:56:49 +02003872 mutex_lock(&chip->reg_lock);
3873 err = mv88e6xxx_switch_reset(chip);
3874 mutex_unlock(&chip->reg_lock);
Andrew Lunnb516d452016-06-04 21:17:06 +02003875 if (err)
Andrew Lunndc30c352016-10-16 19:56:49 +02003876 goto out;
Andrew Lunnb516d452016-06-04 21:17:06 +02003877
Andrew Lunndc30c352016-10-16 19:56:49 +02003878 chip->irq = of_irq_get(np, 0);
3879 if (chip->irq == -EPROBE_DEFER) {
3880 err = chip->irq;
3881 goto out;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02003882 }
3883
Andrew Lunndc30c352016-10-16 19:56:49 +02003884 if (chip->irq > 0) {
3885 /* Has to be performed before the MDIO bus is created,
3886 * because the PHYs will link there interrupts to these
3887 * interrupt controllers
3888 */
3889 mutex_lock(&chip->reg_lock);
3890 err = mv88e6xxx_g1_irq_setup(chip);
3891 mutex_unlock(&chip->reg_lock);
3892
3893 if (err)
3894 goto out;
3895
3896 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_INT)) {
3897 err = mv88e6xxx_g2_irq_setup(chip);
3898 if (err)
3899 goto out_g1_irq;
3900 }
3901 }
3902
3903 err = mv88e6xxx_mdio_register(chip, np);
3904 if (err)
3905 goto out_g2_irq;
3906
3907 err = mv88e6xxx_register_switch(chip, np);
3908 if (err)
3909 goto out_mdio;
3910
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003911 return 0;
Andrew Lunndc30c352016-10-16 19:56:49 +02003912
3913out_mdio:
3914 mv88e6xxx_mdio_unregister(chip);
3915out_g2_irq:
Andrew Lunn467126442016-11-20 20:14:15 +01003916 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_INT) && chip->irq > 0)
Andrew Lunndc30c352016-10-16 19:56:49 +02003917 mv88e6xxx_g2_irq_free(chip);
3918out_g1_irq:
Andrew Lunn61f7c3f2016-11-20 20:14:19 +01003919 if (chip->irq > 0) {
3920 mutex_lock(&chip->reg_lock);
Andrew Lunn467126442016-11-20 20:14:15 +01003921 mv88e6xxx_g1_irq_free(chip);
Andrew Lunn61f7c3f2016-11-20 20:14:19 +01003922 mutex_unlock(&chip->reg_lock);
3923 }
Andrew Lunndc30c352016-10-16 19:56:49 +02003924out:
3925 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003926}
3927
3928static void mv88e6xxx_remove(struct mdio_device *mdiodev)
3929{
3930 struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev);
Vivien Didelot04bed142016-08-31 18:06:13 -04003931 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003932
Andrew Lunn930188c2016-08-22 16:01:03 +02003933 mv88e6xxx_phy_destroy(chip);
Vivien Didelotfad09c72016-06-21 12:28:20 -04003934 mv88e6xxx_unregister_switch(chip);
3935 mv88e6xxx_mdio_unregister(chip);
Andrew Lunndc30c352016-10-16 19:56:49 +02003936
Andrew Lunn467126442016-11-20 20:14:15 +01003937 if (chip->irq > 0) {
3938 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_INT))
3939 mv88e6xxx_g2_irq_free(chip);
3940 mv88e6xxx_g1_irq_free(chip);
3941 }
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003942}
3943
3944static const struct of_device_id mv88e6xxx_of_match[] = {
Vivien Didelotcaac8542016-06-20 13:14:09 -04003945 {
3946 .compatible = "marvell,mv88e6085",
3947 .data = &mv88e6xxx_table[MV88E6085],
3948 },
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003949 { /* sentinel */ },
3950};
3951
3952MODULE_DEVICE_TABLE(of, mv88e6xxx_of_match);
3953
3954static struct mdio_driver mv88e6xxx_driver = {
3955 .probe = mv88e6xxx_probe,
3956 .remove = mv88e6xxx_remove,
3957 .mdiodrv.driver = {
3958 .name = "mv88e6085",
3959 .of_match_table = mv88e6xxx_of_match,
3960 },
3961};
3962
Ben Hutchings98e67302011-11-25 14:36:19 +00003963static int __init mv88e6xxx_init(void)
3964{
Vivien Didelot9d490b42016-08-23 12:38:56 -04003965 register_switch_driver(&mv88e6xxx_switch_ops);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003966 return mdio_driver_register(&mv88e6xxx_driver);
Ben Hutchings98e67302011-11-25 14:36:19 +00003967}
3968module_init(mv88e6xxx_init);
3969
3970static void __exit mv88e6xxx_cleanup(void)
3971{
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003972 mdio_driver_unregister(&mv88e6xxx_driver);
Vivien Didelot9d490b42016-08-23 12:38:56 -04003973 unregister_switch_driver(&mv88e6xxx_switch_ops);
Ben Hutchings98e67302011-11-25 14:36:19 +00003974}
3975module_exit(mv88e6xxx_cleanup);
Ben Hutchings3d825ed2011-11-25 14:37:16 +00003976
3977MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
3978MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
3979MODULE_LICENSE("GPL");