blob: 02d1b3529ee41984d326b60d93d2a39eeb2f8854 [file] [log] [blame]
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001/*
Vivien Didelot0d3cd4b2016-06-21 12:28:19 -04002 * Marvell 88e6xxx Ethernet switch single-chip support
3 *
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00004 * Copyright (c) 2008 Marvell Semiconductor
5 *
Vivien Didelotb8fee952015-08-13 12:52:19 -04006 * Copyright (c) 2015 CMC Electronics, Inc.
7 * Added support for VLAN Table Unit operations
8 *
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02009 * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch>
10 *
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 */
16
Barry Grussling19b2f972013-01-08 16:05:54 +000017#include <linux/delay.h>
Guenter Roeckdefb05b2015-03-26 18:36:38 -070018#include <linux/etherdevice.h>
Andrew Lunndea87022015-08-31 15:56:47 +020019#include <linux/ethtool.h>
Guenter Roeckfacd95b2015-03-26 18:36:35 -070020#include <linux/if_bridge.h>
Andrew Lunndc30c352016-10-16 19:56:49 +020021#include <linux/interrupt.h>
22#include <linux/irq.h>
23#include <linux/irqdomain.h>
Barry Grussling19b2f972013-01-08 16:05:54 +000024#include <linux/jiffies.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000025#include <linux/list.h>
Andrew Lunn14c7b3c2016-05-10 23:27:21 +020026#include <linux/mdio.h>
Paul Gortmaker2bbba272012-01-24 10:41:40 +000027#include <linux/module.h>
Vivien Didelotcaac8542016-06-20 13:14:09 -040028#include <linux/of_device.h>
Andrew Lunndc30c352016-10-16 19:56:49 +020029#include <linux/of_irq.h>
Andrew Lunnb516d452016-06-04 21:17:06 +020030#include <linux/of_mdio.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000031#include <linux/netdevice.h>
Andrew Lunnc8c1b39a2015-11-20 03:56:24 +010032#include <linux/gpio/consumer.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000033#include <linux/phy.h>
Ben Hutchingsc8f0b862011-11-27 17:06:08 +000034#include <net/dsa.h>
Vivien Didelot1f36faf2015-10-08 11:35:13 -040035#include <net/switchdev.h>
Vivien Didelotec561272016-09-02 14:45:33 -040036
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000037#include "mv88e6xxx.h"
Vivien Didelota935c052016-09-29 12:21:53 -040038#include "global1.h"
Vivien Didelotec561272016-09-02 14:45:33 -040039#include "global2.h"
Vivien Didelot18abed22016-11-04 03:23:26 +010040#include "port.h"
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000041
Vivien Didelotfad09c72016-06-21 12:28:20 -040042static void assert_reg_lock(struct mv88e6xxx_chip *chip)
Vivien Didelot3996a4f2015-10-30 18:56:45 -040043{
Vivien Didelotfad09c72016-06-21 12:28:20 -040044 if (unlikely(!mutex_is_locked(&chip->reg_lock))) {
45 dev_err(chip->dev, "Switch registers lock not held!\n");
Vivien Didelot3996a4f2015-10-30 18:56:45 -040046 dump_stack();
47 }
48}
49
Vivien Didelot914b32f2016-06-20 13:14:11 -040050/* The switch ADDR[4:1] configuration pins define the chip SMI device address
51 * (ADDR[0] is always zero, thus only even SMI addresses can be strapped).
52 *
53 * When ADDR is all zero, the chip uses Single-chip Addressing Mode, assuming it
54 * is the only device connected to the SMI master. In this mode it responds to
55 * all 32 possible SMI addresses, and thus maps directly the internal devices.
56 *
57 * When ADDR is non-zero, the chip uses Multi-chip Addressing Mode, allowing
58 * multiple devices to share the SMI interface. In this mode it responds to only
59 * 2 registers, used to indirectly access the internal SMI devices.
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000060 */
Vivien Didelot914b32f2016-06-20 13:14:11 -040061
Vivien Didelotfad09c72016-06-21 12:28:20 -040062static int mv88e6xxx_smi_read(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -040063 int addr, int reg, u16 *val)
64{
Vivien Didelotfad09c72016-06-21 12:28:20 -040065 if (!chip->smi_ops)
Vivien Didelot914b32f2016-06-20 13:14:11 -040066 return -EOPNOTSUPP;
67
Vivien Didelotfad09c72016-06-21 12:28:20 -040068 return chip->smi_ops->read(chip, addr, reg, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -040069}
70
Vivien Didelotfad09c72016-06-21 12:28:20 -040071static int mv88e6xxx_smi_write(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -040072 int addr, int reg, u16 val)
73{
Vivien Didelotfad09c72016-06-21 12:28:20 -040074 if (!chip->smi_ops)
Vivien Didelot914b32f2016-06-20 13:14:11 -040075 return -EOPNOTSUPP;
76
Vivien Didelotfad09c72016-06-21 12:28:20 -040077 return chip->smi_ops->write(chip, addr, reg, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -040078}
79
Vivien Didelotfad09c72016-06-21 12:28:20 -040080static int mv88e6xxx_smi_single_chip_read(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -040081 int addr, int reg, u16 *val)
82{
83 int ret;
84
Vivien Didelotfad09c72016-06-21 12:28:20 -040085 ret = mdiobus_read_nested(chip->bus, addr, reg);
Vivien Didelot914b32f2016-06-20 13:14:11 -040086 if (ret < 0)
87 return ret;
88
89 *val = ret & 0xffff;
90
91 return 0;
92}
93
Vivien Didelotfad09c72016-06-21 12:28:20 -040094static int mv88e6xxx_smi_single_chip_write(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -040095 int addr, int reg, u16 val)
96{
97 int ret;
98
Vivien Didelotfad09c72016-06-21 12:28:20 -040099 ret = mdiobus_write_nested(chip->bus, addr, reg, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400100 if (ret < 0)
101 return ret;
102
103 return 0;
104}
105
Vivien Didelotc08026a2016-09-29 12:21:59 -0400106static const struct mv88e6xxx_bus_ops mv88e6xxx_smi_single_chip_ops = {
Vivien Didelot914b32f2016-06-20 13:14:11 -0400107 .read = mv88e6xxx_smi_single_chip_read,
108 .write = mv88e6xxx_smi_single_chip_write,
109};
110
Vivien Didelotfad09c72016-06-21 12:28:20 -0400111static int mv88e6xxx_smi_multi_chip_wait(struct mv88e6xxx_chip *chip)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000112{
113 int ret;
114 int i;
115
116 for (i = 0; i < 16; i++) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400117 ret = mdiobus_read_nested(chip->bus, chip->sw_addr, SMI_CMD);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000118 if (ret < 0)
119 return ret;
120
Andrew Lunncca8b132015-04-02 04:06:39 +0200121 if ((ret & SMI_CMD_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000122 return 0;
123 }
124
125 return -ETIMEDOUT;
126}
127
Vivien Didelotfad09c72016-06-21 12:28:20 -0400128static int mv88e6xxx_smi_multi_chip_read(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -0400129 int addr, int reg, u16 *val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000130{
131 int ret;
132
Barry Grussling3675c8d2013-01-08 16:05:53 +0000133 /* Wait for the bus to become free. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400134 ret = mv88e6xxx_smi_multi_chip_wait(chip);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000135 if (ret < 0)
136 return ret;
137
Barry Grussling3675c8d2013-01-08 16:05:53 +0000138 /* Transmit the read command. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400139 ret = mdiobus_write_nested(chip->bus, chip->sw_addr, SMI_CMD,
Neil Armstrong6e899e62015-10-22 10:37:53 +0200140 SMI_CMD_OP_22_READ | (addr << 5) | reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000141 if (ret < 0)
142 return ret;
143
Barry Grussling3675c8d2013-01-08 16:05:53 +0000144 /* Wait for the read command to complete. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400145 ret = mv88e6xxx_smi_multi_chip_wait(chip);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000146 if (ret < 0)
147 return ret;
148
Barry Grussling3675c8d2013-01-08 16:05:53 +0000149 /* Read the data. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400150 ret = mdiobus_read_nested(chip->bus, chip->sw_addr, SMI_DATA);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000151 if (ret < 0)
152 return ret;
153
Vivien Didelot914b32f2016-06-20 13:14:11 -0400154 *val = ret & 0xffff;
155
156 return 0;
157}
158
Vivien Didelotfad09c72016-06-21 12:28:20 -0400159static int mv88e6xxx_smi_multi_chip_write(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -0400160 int addr, int reg, u16 val)
161{
162 int ret;
163
164 /* Wait for the bus to become free. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400165 ret = mv88e6xxx_smi_multi_chip_wait(chip);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400166 if (ret < 0)
167 return ret;
168
169 /* Transmit the data to write. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400170 ret = mdiobus_write_nested(chip->bus, chip->sw_addr, SMI_DATA, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400171 if (ret < 0)
172 return ret;
173
174 /* Transmit the write command. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400175 ret = mdiobus_write_nested(chip->bus, chip->sw_addr, SMI_CMD,
Vivien Didelot914b32f2016-06-20 13:14:11 -0400176 SMI_CMD_OP_22_WRITE | (addr << 5) | reg);
177 if (ret < 0)
178 return ret;
179
180 /* Wait for the write command to complete. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400181 ret = mv88e6xxx_smi_multi_chip_wait(chip);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400182 if (ret < 0)
183 return ret;
184
185 return 0;
186}
187
Vivien Didelotc08026a2016-09-29 12:21:59 -0400188static const struct mv88e6xxx_bus_ops mv88e6xxx_smi_multi_chip_ops = {
Vivien Didelot914b32f2016-06-20 13:14:11 -0400189 .read = mv88e6xxx_smi_multi_chip_read,
190 .write = mv88e6xxx_smi_multi_chip_write,
191};
192
Vivien Didelotec561272016-09-02 14:45:33 -0400193int mv88e6xxx_read(struct mv88e6xxx_chip *chip, int addr, int reg, u16 *val)
Vivien Didelot914b32f2016-06-20 13:14:11 -0400194{
195 int err;
196
Vivien Didelotfad09c72016-06-21 12:28:20 -0400197 assert_reg_lock(chip);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400198
Vivien Didelotfad09c72016-06-21 12:28:20 -0400199 err = mv88e6xxx_smi_read(chip, addr, reg, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400200 if (err)
201 return err;
202
Vivien Didelotfad09c72016-06-21 12:28:20 -0400203 dev_dbg(chip->dev, "<- addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
Vivien Didelot914b32f2016-06-20 13:14:11 -0400204 addr, reg, *val);
205
206 return 0;
207}
208
Vivien Didelotec561272016-09-02 14:45:33 -0400209int mv88e6xxx_write(struct mv88e6xxx_chip *chip, int addr, int reg, u16 val)
Vivien Didelot914b32f2016-06-20 13:14:11 -0400210{
211 int err;
212
Vivien Didelotfad09c72016-06-21 12:28:20 -0400213 assert_reg_lock(chip);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400214
Vivien Didelotfad09c72016-06-21 12:28:20 -0400215 err = mv88e6xxx_smi_write(chip, addr, reg, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400216 if (err)
217 return err;
218
Vivien Didelotfad09c72016-06-21 12:28:20 -0400219 dev_dbg(chip->dev, "-> addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
Vivien Didelot914b32f2016-06-20 13:14:11 -0400220 addr, reg, val);
221
222 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000223}
224
Vivien Didelote57e5e72016-08-15 17:19:00 -0400225static int mv88e6xxx_phy_read(struct mv88e6xxx_chip *chip, int phy,
226 int reg, u16 *val)
227{
228 int addr = phy; /* PHY devices addresses start at 0x0 */
229
Vivien Didelotb3469dd2016-09-29 12:22:00 -0400230 if (!chip->info->ops->phy_read)
Vivien Didelote57e5e72016-08-15 17:19:00 -0400231 return -EOPNOTSUPP;
232
Vivien Didelotb3469dd2016-09-29 12:22:00 -0400233 return chip->info->ops->phy_read(chip, addr, reg, val);
Vivien Didelote57e5e72016-08-15 17:19:00 -0400234}
235
236static int mv88e6xxx_phy_write(struct mv88e6xxx_chip *chip, int phy,
237 int reg, u16 val)
238{
239 int addr = phy; /* PHY devices addresses start at 0x0 */
240
Vivien Didelotb3469dd2016-09-29 12:22:00 -0400241 if (!chip->info->ops->phy_write)
Vivien Didelote57e5e72016-08-15 17:19:00 -0400242 return -EOPNOTSUPP;
243
Vivien Didelotb3469dd2016-09-29 12:22:00 -0400244 return chip->info->ops->phy_write(chip, addr, reg, val);
Vivien Didelote57e5e72016-08-15 17:19:00 -0400245}
246
Vivien Didelot09cb7df2016-08-15 17:19:01 -0400247static int mv88e6xxx_phy_page_get(struct mv88e6xxx_chip *chip, int phy, u8 page)
248{
249 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_PHY_PAGE))
250 return -EOPNOTSUPP;
251
252 return mv88e6xxx_phy_write(chip, phy, PHY_PAGE, page);
253}
254
255static void mv88e6xxx_phy_page_put(struct mv88e6xxx_chip *chip, int phy)
256{
257 int err;
258
259 /* Restore PHY page Copper 0x0 for access via the registered MDIO bus */
260 err = mv88e6xxx_phy_write(chip, phy, PHY_PAGE, PHY_PAGE_COPPER);
261 if (unlikely(err)) {
262 dev_err(chip->dev, "failed to restore PHY %d page Copper (%d)\n",
263 phy, err);
264 }
265}
266
267static int mv88e6xxx_phy_page_read(struct mv88e6xxx_chip *chip, int phy,
268 u8 page, int reg, u16 *val)
269{
270 int err;
271
272 /* There is no paging for registers 22 */
273 if (reg == PHY_PAGE)
274 return -EINVAL;
275
276 err = mv88e6xxx_phy_page_get(chip, phy, page);
277 if (!err) {
278 err = mv88e6xxx_phy_read(chip, phy, reg, val);
279 mv88e6xxx_phy_page_put(chip, phy);
280 }
281
282 return err;
283}
284
285static int mv88e6xxx_phy_page_write(struct mv88e6xxx_chip *chip, int phy,
286 u8 page, int reg, u16 val)
287{
288 int err;
289
290 /* There is no paging for registers 22 */
291 if (reg == PHY_PAGE)
292 return -EINVAL;
293
294 err = mv88e6xxx_phy_page_get(chip, phy, page);
295 if (!err) {
296 err = mv88e6xxx_phy_write(chip, phy, PHY_PAGE, page);
297 mv88e6xxx_phy_page_put(chip, phy);
298 }
299
300 return err;
301}
302
303static int mv88e6xxx_serdes_read(struct mv88e6xxx_chip *chip, int reg, u16 *val)
304{
305 return mv88e6xxx_phy_page_read(chip, ADDR_SERDES, SERDES_PAGE_FIBER,
306 reg, val);
307}
308
309static int mv88e6xxx_serdes_write(struct mv88e6xxx_chip *chip, int reg, u16 val)
310{
311 return mv88e6xxx_phy_page_write(chip, ADDR_SERDES, SERDES_PAGE_FIBER,
312 reg, val);
313}
314
Andrew Lunndc30c352016-10-16 19:56:49 +0200315static void mv88e6xxx_g1_irq_mask(struct irq_data *d)
316{
317 struct mv88e6xxx_chip *chip = irq_data_get_irq_chip_data(d);
318 unsigned int n = d->hwirq;
319
320 chip->g1_irq.masked |= (1 << n);
321}
322
323static void mv88e6xxx_g1_irq_unmask(struct irq_data *d)
324{
325 struct mv88e6xxx_chip *chip = irq_data_get_irq_chip_data(d);
326 unsigned int n = d->hwirq;
327
328 chip->g1_irq.masked &= ~(1 << n);
329}
330
331static irqreturn_t mv88e6xxx_g1_irq_thread_fn(int irq, void *dev_id)
332{
333 struct mv88e6xxx_chip *chip = dev_id;
334 unsigned int nhandled = 0;
335 unsigned int sub_irq;
336 unsigned int n;
337 u16 reg;
338 int err;
339
340 mutex_lock(&chip->reg_lock);
341 err = mv88e6xxx_g1_read(chip, GLOBAL_STATUS, &reg);
342 mutex_unlock(&chip->reg_lock);
343
344 if (err)
345 goto out;
346
347 for (n = 0; n < chip->g1_irq.nirqs; ++n) {
348 if (reg & (1 << n)) {
349 sub_irq = irq_find_mapping(chip->g1_irq.domain, n);
350 handle_nested_irq(sub_irq);
351 ++nhandled;
352 }
353 }
354out:
355 return (nhandled > 0 ? IRQ_HANDLED : IRQ_NONE);
356}
357
358static void mv88e6xxx_g1_irq_bus_lock(struct irq_data *d)
359{
360 struct mv88e6xxx_chip *chip = irq_data_get_irq_chip_data(d);
361
362 mutex_lock(&chip->reg_lock);
363}
364
365static void mv88e6xxx_g1_irq_bus_sync_unlock(struct irq_data *d)
366{
367 struct mv88e6xxx_chip *chip = irq_data_get_irq_chip_data(d);
368 u16 mask = GENMASK(chip->g1_irq.nirqs, 0);
369 u16 reg;
370 int err;
371
372 err = mv88e6xxx_g1_read(chip, GLOBAL_CONTROL, &reg);
373 if (err)
374 goto out;
375
376 reg &= ~mask;
377 reg |= (~chip->g1_irq.masked & mask);
378
379 err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL, reg);
380 if (err)
381 goto out;
382
383out:
384 mutex_unlock(&chip->reg_lock);
385}
386
387static struct irq_chip mv88e6xxx_g1_irq_chip = {
388 .name = "mv88e6xxx-g1",
389 .irq_mask = mv88e6xxx_g1_irq_mask,
390 .irq_unmask = mv88e6xxx_g1_irq_unmask,
391 .irq_bus_lock = mv88e6xxx_g1_irq_bus_lock,
392 .irq_bus_sync_unlock = mv88e6xxx_g1_irq_bus_sync_unlock,
393};
394
395static int mv88e6xxx_g1_irq_domain_map(struct irq_domain *d,
396 unsigned int irq,
397 irq_hw_number_t hwirq)
398{
399 struct mv88e6xxx_chip *chip = d->host_data;
400
401 irq_set_chip_data(irq, d->host_data);
402 irq_set_chip_and_handler(irq, &chip->g1_irq.chip, handle_level_irq);
403 irq_set_noprobe(irq);
404
405 return 0;
406}
407
408static const struct irq_domain_ops mv88e6xxx_g1_irq_domain_ops = {
409 .map = mv88e6xxx_g1_irq_domain_map,
410 .xlate = irq_domain_xlate_twocell,
411};
412
413static void mv88e6xxx_g1_irq_free(struct mv88e6xxx_chip *chip)
414{
415 int irq, virq;
Andrew Lunn3460a572016-11-20 20:14:16 +0100416 u16 mask;
417
418 mv88e6xxx_g1_read(chip, GLOBAL_CONTROL, &mask);
419 mask |= GENMASK(chip->g1_irq.nirqs, 0);
420 mv88e6xxx_g1_write(chip, GLOBAL_CONTROL, mask);
421
422 free_irq(chip->irq, chip);
Andrew Lunndc30c352016-10-16 19:56:49 +0200423
424 for (irq = 0; irq < 16; irq++) {
Andrew Lunna3db3d32016-11-20 20:14:14 +0100425 virq = irq_find_mapping(chip->g1_irq.domain, irq);
Andrew Lunndc30c352016-10-16 19:56:49 +0200426 irq_dispose_mapping(virq);
427 }
428
Andrew Lunna3db3d32016-11-20 20:14:14 +0100429 irq_domain_remove(chip->g1_irq.domain);
Andrew Lunndc30c352016-10-16 19:56:49 +0200430}
431
432static int mv88e6xxx_g1_irq_setup(struct mv88e6xxx_chip *chip)
433{
Andrew Lunn3dd0ef02016-11-20 20:14:17 +0100434 int err, irq, virq;
435 u16 reg, mask;
Andrew Lunndc30c352016-10-16 19:56:49 +0200436
437 chip->g1_irq.nirqs = chip->info->g1_irqs;
438 chip->g1_irq.domain = irq_domain_add_simple(
439 NULL, chip->g1_irq.nirqs, 0,
440 &mv88e6xxx_g1_irq_domain_ops, chip);
441 if (!chip->g1_irq.domain)
442 return -ENOMEM;
443
444 for (irq = 0; irq < chip->g1_irq.nirqs; irq++)
445 irq_create_mapping(chip->g1_irq.domain, irq);
446
447 chip->g1_irq.chip = mv88e6xxx_g1_irq_chip;
448 chip->g1_irq.masked = ~0;
449
Andrew Lunn3dd0ef02016-11-20 20:14:17 +0100450 err = mv88e6xxx_g1_read(chip, GLOBAL_CONTROL, &mask);
Andrew Lunndc30c352016-10-16 19:56:49 +0200451 if (err)
Andrew Lunn3dd0ef02016-11-20 20:14:17 +0100452 goto out_mapping;
Andrew Lunndc30c352016-10-16 19:56:49 +0200453
Andrew Lunn3dd0ef02016-11-20 20:14:17 +0100454 mask &= ~GENMASK(chip->g1_irq.nirqs, 0);
Andrew Lunndc30c352016-10-16 19:56:49 +0200455
Andrew Lunn3dd0ef02016-11-20 20:14:17 +0100456 err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL, mask);
Andrew Lunndc30c352016-10-16 19:56:49 +0200457 if (err)
Andrew Lunn3dd0ef02016-11-20 20:14:17 +0100458 goto out_disable;
Andrew Lunndc30c352016-10-16 19:56:49 +0200459
460 /* Reading the interrupt status clears (most of) them */
461 err = mv88e6xxx_g1_read(chip, GLOBAL_STATUS, &reg);
462 if (err)
Andrew Lunn3dd0ef02016-11-20 20:14:17 +0100463 goto out_disable;
Andrew Lunndc30c352016-10-16 19:56:49 +0200464
465 err = request_threaded_irq(chip->irq, NULL,
466 mv88e6xxx_g1_irq_thread_fn,
467 IRQF_ONESHOT | IRQF_TRIGGER_FALLING,
468 dev_name(chip->dev), chip);
469 if (err)
Andrew Lunn3dd0ef02016-11-20 20:14:17 +0100470 goto out_disable;
Andrew Lunndc30c352016-10-16 19:56:49 +0200471
472 return 0;
473
Andrew Lunn3dd0ef02016-11-20 20:14:17 +0100474out_disable:
475 mask |= GENMASK(chip->g1_irq.nirqs, 0);
476 mv88e6xxx_g1_write(chip, GLOBAL_CONTROL, mask);
477
478out_mapping:
479 for (irq = 0; irq < 16; irq++) {
480 virq = irq_find_mapping(chip->g1_irq.domain, irq);
481 irq_dispose_mapping(virq);
482 }
483
484 irq_domain_remove(chip->g1_irq.domain);
Andrew Lunndc30c352016-10-16 19:56:49 +0200485
486 return err;
487}
488
Vivien Didelotec561272016-09-02 14:45:33 -0400489int mv88e6xxx_wait(struct mv88e6xxx_chip *chip, int addr, int reg, u16 mask)
Vivien Didelot2d79af62016-08-15 17:18:57 -0400490{
Andrew Lunn6441e6692016-08-19 00:01:55 +0200491 int i;
Vivien Didelot2d79af62016-08-15 17:18:57 -0400492
Andrew Lunn6441e6692016-08-19 00:01:55 +0200493 for (i = 0; i < 16; i++) {
Vivien Didelot2d79af62016-08-15 17:18:57 -0400494 u16 val;
495 int err;
496
497 err = mv88e6xxx_read(chip, addr, reg, &val);
498 if (err)
499 return err;
500
501 if (!(val & mask))
502 return 0;
503
504 usleep_range(1000, 2000);
505 }
506
Andrew Lunn30853552016-08-19 00:01:57 +0200507 dev_err(chip->dev, "Timeout while waiting for switch\n");
Vivien Didelot2d79af62016-08-15 17:18:57 -0400508 return -ETIMEDOUT;
509}
510
Vivien Didelotf22ab642016-07-18 20:45:31 -0400511/* Indirect write to single pointer-data register with an Update bit */
Vivien Didelotec561272016-09-02 14:45:33 -0400512int mv88e6xxx_update(struct mv88e6xxx_chip *chip, int addr, int reg, u16 update)
Vivien Didelotf22ab642016-07-18 20:45:31 -0400513{
514 u16 val;
Andrew Lunn0f02b4f2016-08-19 00:01:56 +0200515 int err;
Vivien Didelotf22ab642016-07-18 20:45:31 -0400516
517 /* Wait until the previous operation is completed */
Andrew Lunn0f02b4f2016-08-19 00:01:56 +0200518 err = mv88e6xxx_wait(chip, addr, reg, BIT(15));
519 if (err)
520 return err;
Vivien Didelotf22ab642016-07-18 20:45:31 -0400521
522 /* Set the Update bit to trigger a write operation */
523 val = BIT(15) | update;
524
525 return mv88e6xxx_write(chip, addr, reg, val);
526}
527
Vivien Didelota935c052016-09-29 12:21:53 -0400528static int mv88e6xxx_ppu_disable(struct mv88e6xxx_chip *chip)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000529{
Vivien Didelot914b32f2016-06-20 13:14:11 -0400530 u16 val;
Vivien Didelota935c052016-09-29 12:21:53 -0400531 int i, err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000532
Vivien Didelota935c052016-09-29 12:21:53 -0400533 err = mv88e6xxx_g1_read(chip, GLOBAL_CONTROL, &val);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400534 if (err)
535 return err;
Vivien Didelot3996a4f2015-10-30 18:56:45 -0400536
Vivien Didelota935c052016-09-29 12:21:53 -0400537 err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL,
538 val & ~GLOBAL_CONTROL_PPU_ENABLE);
539 if (err)
540 return err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000541
Andrew Lunn6441e6692016-08-19 00:01:55 +0200542 for (i = 0; i < 16; i++) {
Vivien Didelota935c052016-09-29 12:21:53 -0400543 err = mv88e6xxx_g1_read(chip, GLOBAL_STATUS, &val);
544 if (err)
545 return err;
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200546
Barry Grussling19b2f972013-01-08 16:05:54 +0000547 usleep_range(1000, 2000);
Vivien Didelota935c052016-09-29 12:21:53 -0400548 if ((val & GLOBAL_STATUS_PPU_MASK) != GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000549 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000550 }
551
552 return -ETIMEDOUT;
553}
554
Vivien Didelotfad09c72016-06-21 12:28:20 -0400555static int mv88e6xxx_ppu_enable(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000556{
Vivien Didelota935c052016-09-29 12:21:53 -0400557 u16 val;
558 int i, err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000559
Vivien Didelota935c052016-09-29 12:21:53 -0400560 err = mv88e6xxx_g1_read(chip, GLOBAL_CONTROL, &val);
561 if (err)
562 return err;
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200563
Vivien Didelota935c052016-09-29 12:21:53 -0400564 err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL,
565 val | GLOBAL_CONTROL_PPU_ENABLE);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200566 if (err)
567 return err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000568
Andrew Lunn6441e6692016-08-19 00:01:55 +0200569 for (i = 0; i < 16; i++) {
Vivien Didelota935c052016-09-29 12:21:53 -0400570 err = mv88e6xxx_g1_read(chip, GLOBAL_STATUS, &val);
571 if (err)
572 return err;
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200573
Barry Grussling19b2f972013-01-08 16:05:54 +0000574 usleep_range(1000, 2000);
Vivien Didelota935c052016-09-29 12:21:53 -0400575 if ((val & GLOBAL_STATUS_PPU_MASK) == GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000576 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000577 }
578
579 return -ETIMEDOUT;
580}
581
582static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
583{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400584 struct mv88e6xxx_chip *chip;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000585
Vivien Didelotfad09c72016-06-21 12:28:20 -0400586 chip = container_of(ugly, struct mv88e6xxx_chip, ppu_work);
Vivien Didelot762eb672016-06-04 21:16:54 +0200587
Vivien Didelotfad09c72016-06-21 12:28:20 -0400588 mutex_lock(&chip->reg_lock);
Vivien Didelot762eb672016-06-04 21:16:54 +0200589
Vivien Didelotfad09c72016-06-21 12:28:20 -0400590 if (mutex_trylock(&chip->ppu_mutex)) {
591 if (mv88e6xxx_ppu_enable(chip) == 0)
592 chip->ppu_disabled = 0;
593 mutex_unlock(&chip->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000594 }
Vivien Didelot762eb672016-06-04 21:16:54 +0200595
Vivien Didelotfad09c72016-06-21 12:28:20 -0400596 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000597}
598
599static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
600{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400601 struct mv88e6xxx_chip *chip = (void *)_ps;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000602
Vivien Didelotfad09c72016-06-21 12:28:20 -0400603 schedule_work(&chip->ppu_work);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000604}
605
Vivien Didelotfad09c72016-06-21 12:28:20 -0400606static int mv88e6xxx_ppu_access_get(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000607{
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000608 int ret;
609
Vivien Didelotfad09c72016-06-21 12:28:20 -0400610 mutex_lock(&chip->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000611
Barry Grussling3675c8d2013-01-08 16:05:53 +0000612 /* If the PHY polling unit is enabled, disable it so that
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000613 * we can access the PHY registers. If it was already
614 * disabled, cancel the timer that is going to re-enable
615 * it.
616 */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400617 if (!chip->ppu_disabled) {
618 ret = mv88e6xxx_ppu_disable(chip);
Barry Grussling85686582013-01-08 16:05:56 +0000619 if (ret < 0) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400620 mutex_unlock(&chip->ppu_mutex);
Barry Grussling85686582013-01-08 16:05:56 +0000621 return ret;
622 }
Vivien Didelotfad09c72016-06-21 12:28:20 -0400623 chip->ppu_disabled = 1;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000624 } else {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400625 del_timer(&chip->ppu_timer);
Barry Grussling85686582013-01-08 16:05:56 +0000626 ret = 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000627 }
628
629 return ret;
630}
631
Vivien Didelotfad09c72016-06-21 12:28:20 -0400632static void mv88e6xxx_ppu_access_put(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000633{
Barry Grussling3675c8d2013-01-08 16:05:53 +0000634 /* Schedule a timer to re-enable the PHY polling unit. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400635 mod_timer(&chip->ppu_timer, jiffies + msecs_to_jiffies(10));
636 mutex_unlock(&chip->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000637}
638
Vivien Didelotfad09c72016-06-21 12:28:20 -0400639static void mv88e6xxx_ppu_state_init(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000640{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400641 mutex_init(&chip->ppu_mutex);
642 INIT_WORK(&chip->ppu_work, mv88e6xxx_ppu_reenable_work);
Wei Yongjun68497a82016-10-22 14:28:00 +0000643 setup_timer(&chip->ppu_timer, mv88e6xxx_ppu_reenable_timer,
644 (unsigned long)chip);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000645}
646
Andrew Lunn930188c2016-08-22 16:01:03 +0200647static void mv88e6xxx_ppu_state_destroy(struct mv88e6xxx_chip *chip)
648{
649 del_timer_sync(&chip->ppu_timer);
650}
651
Vivien Didelote57e5e72016-08-15 17:19:00 -0400652static int mv88e6xxx_phy_ppu_read(struct mv88e6xxx_chip *chip, int addr,
653 int reg, u16 *val)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000654{
Vivien Didelote57e5e72016-08-15 17:19:00 -0400655 int err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000656
Vivien Didelote57e5e72016-08-15 17:19:00 -0400657 err = mv88e6xxx_ppu_access_get(chip);
658 if (!err) {
659 err = mv88e6xxx_read(chip, addr, reg, val);
Vivien Didelotfad09c72016-06-21 12:28:20 -0400660 mv88e6xxx_ppu_access_put(chip);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000661 }
662
Vivien Didelote57e5e72016-08-15 17:19:00 -0400663 return err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000664}
665
Vivien Didelote57e5e72016-08-15 17:19:00 -0400666static int mv88e6xxx_phy_ppu_write(struct mv88e6xxx_chip *chip, int addr,
667 int reg, u16 val)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000668{
Vivien Didelote57e5e72016-08-15 17:19:00 -0400669 int err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000670
Vivien Didelote57e5e72016-08-15 17:19:00 -0400671 err = mv88e6xxx_ppu_access_get(chip);
672 if (!err) {
673 err = mv88e6xxx_write(chip, addr, reg, val);
Vivien Didelotfad09c72016-06-21 12:28:20 -0400674 mv88e6xxx_ppu_access_put(chip);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000675 }
676
Vivien Didelote57e5e72016-08-15 17:19:00 -0400677 return err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000678}
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000679
Vivien Didelotfad09c72016-06-21 12:28:20 -0400680static bool mv88e6xxx_6065_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200681{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400682 return chip->info->family == MV88E6XXX_FAMILY_6065;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200683}
684
Vivien Didelotfad09c72016-06-21 12:28:20 -0400685static bool mv88e6xxx_6095_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200686{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400687 return chip->info->family == MV88E6XXX_FAMILY_6095;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200688}
689
Vivien Didelotfad09c72016-06-21 12:28:20 -0400690static bool mv88e6xxx_6097_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200691{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400692 return chip->info->family == MV88E6XXX_FAMILY_6097;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200693}
694
Vivien Didelotfad09c72016-06-21 12:28:20 -0400695static bool mv88e6xxx_6165_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200696{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400697 return chip->info->family == MV88E6XXX_FAMILY_6165;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200698}
699
Vivien Didelotfad09c72016-06-21 12:28:20 -0400700static bool mv88e6xxx_6185_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200701{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400702 return chip->info->family == MV88E6XXX_FAMILY_6185;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200703}
704
Vivien Didelotfad09c72016-06-21 12:28:20 -0400705static bool mv88e6xxx_6320_family(struct mv88e6xxx_chip *chip)
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700706{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400707 return chip->info->family == MV88E6XXX_FAMILY_6320;
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700708}
709
Vivien Didelotfad09c72016-06-21 12:28:20 -0400710static bool mv88e6xxx_6351_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200711{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400712 return chip->info->family == MV88E6XXX_FAMILY_6351;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200713}
714
Vivien Didelotfad09c72016-06-21 12:28:20 -0400715static bool mv88e6xxx_6352_family(struct mv88e6xxx_chip *chip)
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200716{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400717 return chip->info->family == MV88E6XXX_FAMILY_6352;
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200718}
719
Vivien Didelotd78343d2016-11-04 03:23:36 +0100720static int mv88e6xxx_port_setup_mac(struct mv88e6xxx_chip *chip, int port,
721 int link, int speed, int duplex,
722 phy_interface_t mode)
723{
724 int err;
725
726 if (!chip->info->ops->port_set_link)
727 return 0;
728
729 /* Port's MAC control must not be changed unless the link is down */
730 err = chip->info->ops->port_set_link(chip, port, 0);
731 if (err)
732 return err;
733
734 if (chip->info->ops->port_set_speed) {
735 err = chip->info->ops->port_set_speed(chip, port, speed);
736 if (err && err != -EOPNOTSUPP)
737 goto restore_link;
738 }
739
740 if (chip->info->ops->port_set_duplex) {
741 err = chip->info->ops->port_set_duplex(chip, port, duplex);
742 if (err && err != -EOPNOTSUPP)
743 goto restore_link;
744 }
745
746 if (chip->info->ops->port_set_rgmii_delay) {
747 err = chip->info->ops->port_set_rgmii_delay(chip, port, mode);
748 if (err && err != -EOPNOTSUPP)
749 goto restore_link;
750 }
751
752 err = 0;
753restore_link:
754 if (chip->info->ops->port_set_link(chip, port, link))
755 netdev_err(chip->ds->ports[port].netdev,
756 "failed to restore MAC's link\n");
757
758 return err;
759}
760
Andrew Lunndea87022015-08-31 15:56:47 +0200761/* We expect the switch to perform auto negotiation if there is a real
762 * phy. However, in the case of a fixed link phy, we force the port
763 * settings from the fixed link settings.
764 */
Vivien Didelotf81ec902016-05-09 13:22:58 -0400765static void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port,
766 struct phy_device *phydev)
Andrew Lunndea87022015-08-31 15:56:47 +0200767{
Vivien Didelot04bed142016-08-31 18:06:13 -0400768 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200769 int err;
Andrew Lunndea87022015-08-31 15:56:47 +0200770
771 if (!phy_is_pseudo_fixed_link(phydev))
772 return;
773
Vivien Didelotfad09c72016-06-21 12:28:20 -0400774 mutex_lock(&chip->reg_lock);
Vivien Didelotd78343d2016-11-04 03:23:36 +0100775 err = mv88e6xxx_port_setup_mac(chip, port, phydev->link, phydev->speed,
776 phydev->duplex, phydev->interface);
Vivien Didelotfad09c72016-06-21 12:28:20 -0400777 mutex_unlock(&chip->reg_lock);
Vivien Didelotd78343d2016-11-04 03:23:36 +0100778
779 if (err && err != -EOPNOTSUPP)
780 netdev_err(ds->ports[port].netdev, "failed to configure MAC\n");
Andrew Lunndea87022015-08-31 15:56:47 +0200781}
782
Vivien Didelotfad09c72016-06-21 12:28:20 -0400783static int _mv88e6xxx_stats_wait(struct mv88e6xxx_chip *chip)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000784{
Vivien Didelota935c052016-09-29 12:21:53 -0400785 u16 val;
786 int i, err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000787
788 for (i = 0; i < 10; i++) {
Vivien Didelota935c052016-09-29 12:21:53 -0400789 err = mv88e6xxx_g1_read(chip, GLOBAL_STATS_OP, &val);
Andrew Lunn096eea02016-11-21 23:26:56 +0100790 if (err)
791 return err;
792
Vivien Didelota935c052016-09-29 12:21:53 -0400793 if ((val & GLOBAL_STATS_OP_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000794 return 0;
795 }
796
797 return -ETIMEDOUT;
798}
799
Vivien Didelotfad09c72016-06-21 12:28:20 -0400800static int _mv88e6xxx_stats_snapshot(struct mv88e6xxx_chip *chip, int port)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000801{
Vivien Didelota935c052016-09-29 12:21:53 -0400802 int err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000803
Vivien Didelotfad09c72016-06-21 12:28:20 -0400804 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200805 port = (port + 1) << 5;
806
Barry Grussling3675c8d2013-01-08 16:05:53 +0000807 /* Snapshot the hardware statistics counters for this port. */
Vivien Didelota935c052016-09-29 12:21:53 -0400808 err = mv88e6xxx_g1_write(chip, GLOBAL_STATS_OP,
809 GLOBAL_STATS_OP_CAPTURE_PORT |
810 GLOBAL_STATS_OP_HIST_RX_TX | port);
811 if (err)
812 return err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000813
Barry Grussling3675c8d2013-01-08 16:05:53 +0000814 /* Wait for the snapshotting to complete. */
Vivien Didelota935c052016-09-29 12:21:53 -0400815 return _mv88e6xxx_stats_wait(chip);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000816}
817
Vivien Didelotfad09c72016-06-21 12:28:20 -0400818static void _mv88e6xxx_stats_read(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -0400819 int stat, u32 *val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000820{
Vivien Didelota935c052016-09-29 12:21:53 -0400821 u32 value;
822 u16 reg;
823 int err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000824
825 *val = 0;
826
Vivien Didelota935c052016-09-29 12:21:53 -0400827 err = mv88e6xxx_g1_write(chip, GLOBAL_STATS_OP,
828 GLOBAL_STATS_OP_READ_CAPTURED |
829 GLOBAL_STATS_OP_HIST_RX_TX | stat);
830 if (err)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000831 return;
832
Vivien Didelota935c052016-09-29 12:21:53 -0400833 err = _mv88e6xxx_stats_wait(chip);
834 if (err)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000835 return;
836
Vivien Didelota935c052016-09-29 12:21:53 -0400837 err = mv88e6xxx_g1_read(chip, GLOBAL_STATS_COUNTER_32, &reg);
838 if (err)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000839 return;
840
Vivien Didelota935c052016-09-29 12:21:53 -0400841 value = reg << 16;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000842
Vivien Didelota935c052016-09-29 12:21:53 -0400843 err = mv88e6xxx_g1_read(chip, GLOBAL_STATS_COUNTER_01, &reg);
844 if (err)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000845 return;
846
Vivien Didelota935c052016-09-29 12:21:53 -0400847 *val = value | reg;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000848}
849
Andrew Lunne413e7e2015-04-02 04:06:38 +0200850static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100851 { "in_good_octets", 8, 0x00, BANK0, },
852 { "in_bad_octets", 4, 0x02, BANK0, },
853 { "in_unicast", 4, 0x04, BANK0, },
854 { "in_broadcasts", 4, 0x06, BANK0, },
855 { "in_multicasts", 4, 0x07, BANK0, },
856 { "in_pause", 4, 0x16, BANK0, },
857 { "in_undersize", 4, 0x18, BANK0, },
858 { "in_fragments", 4, 0x19, BANK0, },
859 { "in_oversize", 4, 0x1a, BANK0, },
860 { "in_jabber", 4, 0x1b, BANK0, },
861 { "in_rx_error", 4, 0x1c, BANK0, },
862 { "in_fcs_error", 4, 0x1d, BANK0, },
863 { "out_octets", 8, 0x0e, BANK0, },
864 { "out_unicast", 4, 0x10, BANK0, },
865 { "out_broadcasts", 4, 0x13, BANK0, },
866 { "out_multicasts", 4, 0x12, BANK0, },
867 { "out_pause", 4, 0x15, BANK0, },
868 { "excessive", 4, 0x11, BANK0, },
869 { "collisions", 4, 0x1e, BANK0, },
870 { "deferred", 4, 0x05, BANK0, },
871 { "single", 4, 0x14, BANK0, },
872 { "multiple", 4, 0x17, BANK0, },
873 { "out_fcs_error", 4, 0x03, BANK0, },
874 { "late", 4, 0x1f, BANK0, },
875 { "hist_64bytes", 4, 0x08, BANK0, },
876 { "hist_65_127bytes", 4, 0x09, BANK0, },
877 { "hist_128_255bytes", 4, 0x0a, BANK0, },
878 { "hist_256_511bytes", 4, 0x0b, BANK0, },
879 { "hist_512_1023bytes", 4, 0x0c, BANK0, },
880 { "hist_1024_max_bytes", 4, 0x0d, BANK0, },
881 { "sw_in_discards", 4, 0x10, PORT, },
882 { "sw_in_filtered", 2, 0x12, PORT, },
883 { "sw_out_filtered", 2, 0x13, PORT, },
884 { "in_discards", 4, 0x00 | GLOBAL_STATS_OP_BANK_1, BANK1, },
885 { "in_filtered", 4, 0x01 | GLOBAL_STATS_OP_BANK_1, BANK1, },
886 { "in_accepted", 4, 0x02 | GLOBAL_STATS_OP_BANK_1, BANK1, },
887 { "in_bad_accepted", 4, 0x03 | GLOBAL_STATS_OP_BANK_1, BANK1, },
888 { "in_good_avb_class_a", 4, 0x04 | GLOBAL_STATS_OP_BANK_1, BANK1, },
889 { "in_good_avb_class_b", 4, 0x05 | GLOBAL_STATS_OP_BANK_1, BANK1, },
890 { "in_bad_avb_class_a", 4, 0x06 | GLOBAL_STATS_OP_BANK_1, BANK1, },
891 { "in_bad_avb_class_b", 4, 0x07 | GLOBAL_STATS_OP_BANK_1, BANK1, },
892 { "tcam_counter_0", 4, 0x08 | GLOBAL_STATS_OP_BANK_1, BANK1, },
893 { "tcam_counter_1", 4, 0x09 | GLOBAL_STATS_OP_BANK_1, BANK1, },
894 { "tcam_counter_2", 4, 0x0a | GLOBAL_STATS_OP_BANK_1, BANK1, },
895 { "tcam_counter_3", 4, 0x0b | GLOBAL_STATS_OP_BANK_1, BANK1, },
896 { "in_da_unknown", 4, 0x0e | GLOBAL_STATS_OP_BANK_1, BANK1, },
897 { "in_management", 4, 0x0f | GLOBAL_STATS_OP_BANK_1, BANK1, },
898 { "out_queue_0", 4, 0x10 | GLOBAL_STATS_OP_BANK_1, BANK1, },
899 { "out_queue_1", 4, 0x11 | GLOBAL_STATS_OP_BANK_1, BANK1, },
900 { "out_queue_2", 4, 0x12 | GLOBAL_STATS_OP_BANK_1, BANK1, },
901 { "out_queue_3", 4, 0x13 | GLOBAL_STATS_OP_BANK_1, BANK1, },
902 { "out_queue_4", 4, 0x14 | GLOBAL_STATS_OP_BANK_1, BANK1, },
903 { "out_queue_5", 4, 0x15 | GLOBAL_STATS_OP_BANK_1, BANK1, },
904 { "out_queue_6", 4, 0x16 | GLOBAL_STATS_OP_BANK_1, BANK1, },
905 { "out_queue_7", 4, 0x17 | GLOBAL_STATS_OP_BANK_1, BANK1, },
906 { "out_cut_through", 4, 0x18 | GLOBAL_STATS_OP_BANK_1, BANK1, },
907 { "out_octets_a", 4, 0x1a | GLOBAL_STATS_OP_BANK_1, BANK1, },
908 { "out_octets_b", 4, 0x1b | GLOBAL_STATS_OP_BANK_1, BANK1, },
909 { "out_management", 4, 0x1f | GLOBAL_STATS_OP_BANK_1, BANK1, },
Andrew Lunne413e7e2015-04-02 04:06:38 +0200910};
911
Vivien Didelotfad09c72016-06-21 12:28:20 -0400912static bool mv88e6xxx_has_stat(struct mv88e6xxx_chip *chip,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100913 struct mv88e6xxx_hw_stat *stat)
Andrew Lunne413e7e2015-04-02 04:06:38 +0200914{
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100915 switch (stat->type) {
916 case BANK0:
Andrew Lunne413e7e2015-04-02 04:06:38 +0200917 return true;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100918 case BANK1:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400919 return mv88e6xxx_6320_family(chip);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100920 case PORT:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400921 return mv88e6xxx_6095_family(chip) ||
922 mv88e6xxx_6185_family(chip) ||
923 mv88e6xxx_6097_family(chip) ||
924 mv88e6xxx_6165_family(chip) ||
925 mv88e6xxx_6351_family(chip) ||
926 mv88e6xxx_6352_family(chip);
Andrew Lunne413e7e2015-04-02 04:06:38 +0200927 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100928 return false;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000929}
930
Vivien Didelotfad09c72016-06-21 12:28:20 -0400931static uint64_t _mv88e6xxx_get_ethtool_stat(struct mv88e6xxx_chip *chip,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100932 struct mv88e6xxx_hw_stat *s,
Andrew Lunn80c46272015-06-20 18:42:30 +0200933 int port)
934{
Andrew Lunn80c46272015-06-20 18:42:30 +0200935 u32 low;
936 u32 high = 0;
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200937 int err;
938 u16 reg;
Andrew Lunn80c46272015-06-20 18:42:30 +0200939 u64 value;
940
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100941 switch (s->type) {
942 case PORT:
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200943 err = mv88e6xxx_port_read(chip, port, s->reg, &reg);
944 if (err)
Andrew Lunn80c46272015-06-20 18:42:30 +0200945 return UINT64_MAX;
946
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200947 low = reg;
Andrew Lunn80c46272015-06-20 18:42:30 +0200948 if (s->sizeof_stat == 4) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200949 err = mv88e6xxx_port_read(chip, port, s->reg + 1, &reg);
950 if (err)
Andrew Lunn80c46272015-06-20 18:42:30 +0200951 return UINT64_MAX;
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200952 high = reg;
Andrew Lunn80c46272015-06-20 18:42:30 +0200953 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100954 break;
955 case BANK0:
956 case BANK1:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400957 _mv88e6xxx_stats_read(chip, s->reg, &low);
Andrew Lunn80c46272015-06-20 18:42:30 +0200958 if (s->sizeof_stat == 8)
Vivien Didelotfad09c72016-06-21 12:28:20 -0400959 _mv88e6xxx_stats_read(chip, s->reg + 1, &high);
Andrew Lunn80c46272015-06-20 18:42:30 +0200960 }
961 value = (((u64)high) << 16) | low;
962 return value;
963}
964
Vivien Didelotf81ec902016-05-09 13:22:58 -0400965static void mv88e6xxx_get_strings(struct dsa_switch *ds, int port,
966 uint8_t *data)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100967{
Vivien Didelot04bed142016-08-31 18:06:13 -0400968 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100969 struct mv88e6xxx_hw_stat *stat;
970 int i, j;
971
972 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
973 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400974 if (mv88e6xxx_has_stat(chip, stat)) {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100975 memcpy(data + j * ETH_GSTRING_LEN, stat->string,
976 ETH_GSTRING_LEN);
977 j++;
978 }
979 }
980}
981
Vivien Didelotf81ec902016-05-09 13:22:58 -0400982static int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100983{
Vivien Didelot04bed142016-08-31 18:06:13 -0400984 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100985 struct mv88e6xxx_hw_stat *stat;
986 int i, j;
987
988 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
989 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400990 if (mv88e6xxx_has_stat(chip, stat))
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100991 j++;
992 }
993 return j;
994}
995
Vivien Didelotf81ec902016-05-09 13:22:58 -0400996static void mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, int port,
997 uint64_t *data)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000998{
Vivien Didelot04bed142016-08-31 18:06:13 -0400999 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +01001000 struct mv88e6xxx_hw_stat *stat;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001001 int ret;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +01001002 int i, j;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001003
Vivien Didelotfad09c72016-06-21 12:28:20 -04001004 mutex_lock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001005
Vivien Didelotfad09c72016-06-21 12:28:20 -04001006 ret = _mv88e6xxx_stats_snapshot(chip, port);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001007 if (ret < 0) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001008 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001009 return;
1010 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +01001011 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
1012 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -04001013 if (mv88e6xxx_has_stat(chip, stat)) {
1014 data[j] = _mv88e6xxx_get_ethtool_stat(chip, stat, port);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +01001015 j++;
1016 }
1017 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001018
Vivien Didelotfad09c72016-06-21 12:28:20 -04001019 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001020}
Ben Hutchings98e67302011-11-25 14:36:19 +00001021
Vivien Didelotf81ec902016-05-09 13:22:58 -04001022static int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001023{
1024 return 32 * sizeof(u16);
1025}
1026
Vivien Didelotf81ec902016-05-09 13:22:58 -04001027static void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
1028 struct ethtool_regs *regs, void *_p)
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001029{
Vivien Didelot04bed142016-08-31 18:06:13 -04001030 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001031 int err;
1032 u16 reg;
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001033 u16 *p = _p;
1034 int i;
1035
1036 regs->version = 0;
1037
1038 memset(p, 0xff, 32 * sizeof(u16));
1039
Vivien Didelotfad09c72016-06-21 12:28:20 -04001040 mutex_lock(&chip->reg_lock);
Vivien Didelot23062512016-05-09 13:22:45 -04001041
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001042 for (i = 0; i < 32; i++) {
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001043
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001044 err = mv88e6xxx_port_read(chip, port, i, &reg);
1045 if (!err)
1046 p[i] = reg;
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001047 }
Vivien Didelot23062512016-05-09 13:22:45 -04001048
Vivien Didelotfad09c72016-06-21 12:28:20 -04001049 mutex_unlock(&chip->reg_lock);
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001050}
1051
Vivien Didelotfad09c72016-06-21 12:28:20 -04001052static int _mv88e6xxx_atu_wait(struct mv88e6xxx_chip *chip)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001053{
Vivien Didelota935c052016-09-29 12:21:53 -04001054 return mv88e6xxx_g1_wait(chip, GLOBAL_ATU_OP, GLOBAL_ATU_OP_BUSY);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001055}
1056
Vivien Didelotf81ec902016-05-09 13:22:58 -04001057static int mv88e6xxx_get_eee(struct dsa_switch *ds, int port,
1058 struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -08001059{
Vivien Didelot04bed142016-08-31 18:06:13 -04001060 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot9c938292016-08-15 17:19:02 -04001061 u16 reg;
1062 int err;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001063
Vivien Didelotfad09c72016-06-21 12:28:20 -04001064 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
Vivien Didelotaadbdb82016-05-09 13:22:44 -04001065 return -EOPNOTSUPP;
1066
Vivien Didelotfad09c72016-06-21 12:28:20 -04001067 mutex_lock(&chip->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001068
Vivien Didelot9c938292016-08-15 17:19:02 -04001069 err = mv88e6xxx_phy_read(chip, port, 16, &reg);
1070 if (err)
Andrew Lunn2f40c692015-04-02 04:06:37 +02001071 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001072
1073 e->eee_enabled = !!(reg & 0x0200);
1074 e->tx_lpi_enabled = !!(reg & 0x0100);
1075
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001076 err = mv88e6xxx_port_read(chip, port, PORT_STATUS, &reg);
Vivien Didelot9c938292016-08-15 17:19:02 -04001077 if (err)
Andrew Lunn2f40c692015-04-02 04:06:37 +02001078 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001079
Andrew Lunncca8b132015-04-02 04:06:39 +02001080 e->eee_active = !!(reg & PORT_STATUS_EEE);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001081out:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001082 mutex_unlock(&chip->reg_lock);
Vivien Didelot9c938292016-08-15 17:19:02 -04001083
1084 return err;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001085}
1086
Vivien Didelotf81ec902016-05-09 13:22:58 -04001087static int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
1088 struct phy_device *phydev, struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -08001089{
Vivien Didelot04bed142016-08-31 18:06:13 -04001090 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot9c938292016-08-15 17:19:02 -04001091 u16 reg;
1092 int err;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001093
Vivien Didelotfad09c72016-06-21 12:28:20 -04001094 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
Vivien Didelotaadbdb82016-05-09 13:22:44 -04001095 return -EOPNOTSUPP;
1096
Vivien Didelotfad09c72016-06-21 12:28:20 -04001097 mutex_lock(&chip->reg_lock);
Guenter Roeck11b3b452015-03-06 22:23:51 -08001098
Vivien Didelot9c938292016-08-15 17:19:02 -04001099 err = mv88e6xxx_phy_read(chip, port, 16, &reg);
1100 if (err)
Andrew Lunn2f40c692015-04-02 04:06:37 +02001101 goto out;
1102
Vivien Didelot9c938292016-08-15 17:19:02 -04001103 reg &= ~0x0300;
Andrew Lunn2f40c692015-04-02 04:06:37 +02001104 if (e->eee_enabled)
1105 reg |= 0x0200;
1106 if (e->tx_lpi_enabled)
1107 reg |= 0x0100;
1108
Vivien Didelot9c938292016-08-15 17:19:02 -04001109 err = mv88e6xxx_phy_write(chip, port, 16, reg);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001110out:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001111 mutex_unlock(&chip->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001112
Vivien Didelot9c938292016-08-15 17:19:02 -04001113 return err;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001114}
1115
Vivien Didelotfad09c72016-06-21 12:28:20 -04001116static int _mv88e6xxx_atu_cmd(struct mv88e6xxx_chip *chip, u16 fid, u16 cmd)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001117{
Vivien Didelota935c052016-09-29 12:21:53 -04001118 u16 val;
1119 int err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001120
Vivien Didelot6dc10bb2016-09-29 12:21:55 -04001121 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G1_ATU_FID)) {
Vivien Didelota935c052016-09-29 12:21:53 -04001122 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_FID, fid);
1123 if (err)
1124 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001125 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001126 /* ATU DBNum[7:4] are located in ATU Control 15:12 */
Vivien Didelota935c052016-09-29 12:21:53 -04001127 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_CONTROL, &val);
1128 if (err)
1129 return err;
Vivien Didelot11ea8092016-03-31 16:53:44 -04001130
Vivien Didelota935c052016-09-29 12:21:53 -04001131 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_CONTROL,
1132 (val & 0xfff) | ((fid << 8) & 0xf000));
1133 if (err)
1134 return err;
Vivien Didelot11ea8092016-03-31 16:53:44 -04001135
1136 /* ATU DBNum[3:0] are located in ATU Operation 3:0 */
1137 cmd |= fid & 0xf;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001138 }
1139
Vivien Didelota935c052016-09-29 12:21:53 -04001140 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_OP, cmd);
1141 if (err)
1142 return err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001143
Vivien Didelotfad09c72016-06-21 12:28:20 -04001144 return _mv88e6xxx_atu_wait(chip);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001145}
1146
Vivien Didelotfad09c72016-06-21 12:28:20 -04001147static int _mv88e6xxx_atu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot37705b72015-09-04 14:34:11 -04001148 struct mv88e6xxx_atu_entry *entry)
1149{
1150 u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK;
1151
1152 if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) {
1153 unsigned int mask, shift;
1154
1155 if (entry->trunk) {
1156 data |= GLOBAL_ATU_DATA_TRUNK;
1157 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
1158 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
1159 } else {
1160 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
1161 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
1162 }
1163
1164 data |= (entry->portv_trunkid << shift) & mask;
1165 }
1166
Vivien Didelota935c052016-09-29 12:21:53 -04001167 return mv88e6xxx_g1_write(chip, GLOBAL_ATU_DATA, data);
Vivien Didelot37705b72015-09-04 14:34:11 -04001168}
1169
Vivien Didelotfad09c72016-06-21 12:28:20 -04001170static int _mv88e6xxx_atu_flush_move(struct mv88e6xxx_chip *chip,
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001171 struct mv88e6xxx_atu_entry *entry,
1172 bool static_too)
1173{
1174 int op;
1175 int err;
1176
Vivien Didelotfad09c72016-06-21 12:28:20 -04001177 err = _mv88e6xxx_atu_wait(chip);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001178 if (err)
1179 return err;
1180
Vivien Didelotfad09c72016-06-21 12:28:20 -04001181 err = _mv88e6xxx_atu_data_write(chip, entry);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001182 if (err)
1183 return err;
1184
1185 if (entry->fid) {
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001186 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB :
1187 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB;
1188 } else {
1189 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL :
1190 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC;
1191 }
1192
Vivien Didelotfad09c72016-06-21 12:28:20 -04001193 return _mv88e6xxx_atu_cmd(chip, entry->fid, op);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001194}
1195
Vivien Didelotfad09c72016-06-21 12:28:20 -04001196static int _mv88e6xxx_atu_flush(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001197 u16 fid, bool static_too)
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001198{
1199 struct mv88e6xxx_atu_entry entry = {
1200 .fid = fid,
1201 .state = 0, /* EntryState bits must be 0 */
1202 };
1203
Vivien Didelotfad09c72016-06-21 12:28:20 -04001204 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001205}
1206
Vivien Didelotfad09c72016-06-21 12:28:20 -04001207static int _mv88e6xxx_atu_move(struct mv88e6xxx_chip *chip, u16 fid,
Andrew Lunn158bc062016-04-28 21:24:06 -04001208 int from_port, int to_port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001209{
1210 struct mv88e6xxx_atu_entry entry = {
1211 .trunk = false,
1212 .fid = fid,
1213 };
1214
1215 /* EntryState bits must be 0xF */
1216 entry.state = GLOBAL_ATU_DATA_STATE_MASK;
1217
1218 /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */
1219 entry.portv_trunkid = (to_port & 0x0f) << 4;
1220 entry.portv_trunkid |= from_port & 0x0f;
1221
Vivien Didelotfad09c72016-06-21 12:28:20 -04001222 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001223}
1224
Vivien Didelotfad09c72016-06-21 12:28:20 -04001225static int _mv88e6xxx_atu_remove(struct mv88e6xxx_chip *chip, u16 fid,
Andrew Lunn158bc062016-04-28 21:24:06 -04001226 int port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001227{
1228 /* Destination port 0xF means remove the entries */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001229 return _mv88e6xxx_atu_move(chip, fid, port, 0x0f, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001230}
1231
Vivien Didelotfad09c72016-06-21 12:28:20 -04001232static int _mv88e6xxx_port_based_vlan_map(struct mv88e6xxx_chip *chip, int port)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001233{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001234 struct net_device *bridge = chip->ports[port].bridge_dev;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001235 struct dsa_switch *ds = chip->ds;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001236 u16 output_ports = 0;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001237 int i;
1238
1239 /* allow CPU port or DSA link(s) to send frames to every port */
1240 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
Vivien Didelot5a7921f2016-11-04 03:23:28 +01001241 output_ports = ~0;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001242 } else {
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001243 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001244 /* allow sending frames to every group member */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001245 if (bridge && chip->ports[i].bridge_dev == bridge)
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001246 output_ports |= BIT(i);
1247
1248 /* allow sending frames to CPU port and DSA link(s) */
1249 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
1250 output_ports |= BIT(i);
1251 }
1252 }
1253
1254 /* prevent frames from going back out of the port they came in on */
1255 output_ports &= ~BIT(port);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001256
Vivien Didelot5a7921f2016-11-04 03:23:28 +01001257 return mv88e6xxx_port_set_vlan_map(chip, port, output_ports);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001258}
1259
Vivien Didelotf81ec902016-05-09 13:22:58 -04001260static void mv88e6xxx_port_stp_state_set(struct dsa_switch *ds, int port,
1261 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001262{
Vivien Didelot04bed142016-08-31 18:06:13 -04001263 struct mv88e6xxx_chip *chip = ds->priv;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001264 int stp_state;
Vivien Didelot553eb542016-05-13 20:38:23 -04001265 int err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001266
1267 switch (state) {
1268 case BR_STATE_DISABLED:
Andrew Lunncca8b132015-04-02 04:06:39 +02001269 stp_state = PORT_CONTROL_STATE_DISABLED;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001270 break;
1271 case BR_STATE_BLOCKING:
1272 case BR_STATE_LISTENING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001273 stp_state = PORT_CONTROL_STATE_BLOCKING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001274 break;
1275 case BR_STATE_LEARNING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001276 stp_state = PORT_CONTROL_STATE_LEARNING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001277 break;
1278 case BR_STATE_FORWARDING:
1279 default:
Andrew Lunncca8b132015-04-02 04:06:39 +02001280 stp_state = PORT_CONTROL_STATE_FORWARDING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001281 break;
1282 }
1283
Vivien Didelotfad09c72016-06-21 12:28:20 -04001284 mutex_lock(&chip->reg_lock);
Vivien Didelote28def332016-11-04 03:23:27 +01001285 err = mv88e6xxx_port_set_state(chip, port, stp_state);
Vivien Didelotfad09c72016-06-21 12:28:20 -04001286 mutex_unlock(&chip->reg_lock);
Vivien Didelot553eb542016-05-13 20:38:23 -04001287
1288 if (err)
Vivien Didelote28def332016-11-04 03:23:27 +01001289 netdev_err(ds->ports[port].netdev, "failed to update state\n");
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001290}
1291
Vivien Didelot749efcb2016-09-22 16:49:24 -04001292static void mv88e6xxx_port_fast_age(struct dsa_switch *ds, int port)
1293{
1294 struct mv88e6xxx_chip *chip = ds->priv;
1295 int err;
1296
1297 mutex_lock(&chip->reg_lock);
1298 err = _mv88e6xxx_atu_remove(chip, 0, port, false);
1299 mutex_unlock(&chip->reg_lock);
1300
1301 if (err)
1302 netdev_err(ds->ports[port].netdev, "failed to flush ATU\n");
1303}
1304
Vivien Didelotfad09c72016-06-21 12:28:20 -04001305static int _mv88e6xxx_vtu_wait(struct mv88e6xxx_chip *chip)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001306{
Vivien Didelota935c052016-09-29 12:21:53 -04001307 return mv88e6xxx_g1_wait(chip, GLOBAL_VTU_OP, GLOBAL_VTU_OP_BUSY);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001308}
1309
Vivien Didelotfad09c72016-06-21 12:28:20 -04001310static int _mv88e6xxx_vtu_cmd(struct mv88e6xxx_chip *chip, u16 op)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001311{
Vivien Didelota935c052016-09-29 12:21:53 -04001312 int err;
Vivien Didelot6b17e862015-08-13 12:52:18 -04001313
Vivien Didelota935c052016-09-29 12:21:53 -04001314 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_OP, op);
1315 if (err)
1316 return err;
Vivien Didelot6b17e862015-08-13 12:52:18 -04001317
Vivien Didelotfad09c72016-06-21 12:28:20 -04001318 return _mv88e6xxx_vtu_wait(chip);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001319}
1320
Vivien Didelotfad09c72016-06-21 12:28:20 -04001321static int _mv88e6xxx_vtu_stu_flush(struct mv88e6xxx_chip *chip)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001322{
1323 int ret;
1324
Vivien Didelotfad09c72016-06-21 12:28:20 -04001325 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001326 if (ret < 0)
1327 return ret;
1328
Vivien Didelotfad09c72016-06-21 12:28:20 -04001329 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_FLUSH_ALL);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001330}
1331
Vivien Didelotfad09c72016-06-21 12:28:20 -04001332static int _mv88e6xxx_vtu_stu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001333 struct mv88e6xxx_vtu_entry *entry,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001334 unsigned int nibble_offset)
1335{
Vivien Didelotb8fee952015-08-13 12:52:19 -04001336 u16 regs[3];
Vivien Didelota935c052016-09-29 12:21:53 -04001337 int i, err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001338
1339 for (i = 0; i < 3; ++i) {
Vivien Didelota935c052016-09-29 12:21:53 -04001340 u16 *reg = &regs[i];
Vivien Didelotb8fee952015-08-13 12:52:19 -04001341
Vivien Didelota935c052016-09-29 12:21:53 -04001342 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_DATA_0_3 + i, reg);
1343 if (err)
1344 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001345 }
1346
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001347 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotb8fee952015-08-13 12:52:19 -04001348 unsigned int shift = (i % 4) * 4 + nibble_offset;
1349 u16 reg = regs[i / 4];
1350
1351 entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
1352 }
1353
1354 return 0;
1355}
1356
Vivien Didelotfad09c72016-06-21 12:28:20 -04001357static int mv88e6xxx_vtu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001358 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001359{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001360 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 0);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001361}
1362
Vivien Didelotfad09c72016-06-21 12:28:20 -04001363static int mv88e6xxx_stu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001364 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001365{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001366 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 2);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001367}
1368
Vivien Didelotfad09c72016-06-21 12:28:20 -04001369static int _mv88e6xxx_vtu_stu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001370 struct mv88e6xxx_vtu_entry *entry,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001371 unsigned int nibble_offset)
1372{
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001373 u16 regs[3] = { 0 };
Vivien Didelota935c052016-09-29 12:21:53 -04001374 int i, err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001375
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001376 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001377 unsigned int shift = (i % 4) * 4 + nibble_offset;
1378 u8 data = entry->data[i];
1379
1380 regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
1381 }
1382
1383 for (i = 0; i < 3; ++i) {
Vivien Didelota935c052016-09-29 12:21:53 -04001384 u16 reg = regs[i];
1385
1386 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_DATA_0_3 + i, reg);
1387 if (err)
1388 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001389 }
1390
1391 return 0;
1392}
1393
Vivien Didelotfad09c72016-06-21 12:28:20 -04001394static int mv88e6xxx_vtu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001395 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001396{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001397 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 0);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001398}
1399
Vivien Didelotfad09c72016-06-21 12:28:20 -04001400static int mv88e6xxx_stu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001401 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001402{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001403 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 2);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001404}
1405
Vivien Didelotfad09c72016-06-21 12:28:20 -04001406static int _mv88e6xxx_vtu_vid_write(struct mv88e6xxx_chip *chip, u16 vid)
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001407{
Vivien Didelota935c052016-09-29 12:21:53 -04001408 return mv88e6xxx_g1_write(chip, GLOBAL_VTU_VID,
1409 vid & GLOBAL_VTU_VID_MASK);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001410}
1411
Vivien Didelotfad09c72016-06-21 12:28:20 -04001412static int _mv88e6xxx_vtu_getnext(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001413 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelotb8fee952015-08-13 12:52:19 -04001414{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001415 struct mv88e6xxx_vtu_entry next = { 0 };
Vivien Didelota935c052016-09-29 12:21:53 -04001416 u16 val;
1417 int err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001418
Vivien Didelota935c052016-09-29 12:21:53 -04001419 err = _mv88e6xxx_vtu_wait(chip);
1420 if (err)
1421 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001422
Vivien Didelota935c052016-09-29 12:21:53 -04001423 err = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_VTU_GET_NEXT);
1424 if (err)
1425 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001426
Vivien Didelota935c052016-09-29 12:21:53 -04001427 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_VID, &val);
1428 if (err)
1429 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001430
Vivien Didelota935c052016-09-29 12:21:53 -04001431 next.vid = val & GLOBAL_VTU_VID_MASK;
1432 next.valid = !!(val & GLOBAL_VTU_VID_VALID);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001433
1434 if (next.valid) {
Vivien Didelota935c052016-09-29 12:21:53 -04001435 err = mv88e6xxx_vtu_data_read(chip, &next);
1436 if (err)
1437 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001438
Vivien Didelot6dc10bb2016-09-29 12:21:55 -04001439 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G1_VTU_FID)) {
Vivien Didelota935c052016-09-29 12:21:53 -04001440 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_FID, &val);
1441 if (err)
1442 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001443
Vivien Didelota935c052016-09-29 12:21:53 -04001444 next.fid = val & GLOBAL_VTU_FID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001445 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001446 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1447 * VTU DBNum[3:0] are located in VTU Operation 3:0
1448 */
Vivien Didelota935c052016-09-29 12:21:53 -04001449 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_OP, &val);
1450 if (err)
1451 return err;
Vivien Didelot11ea8092016-03-31 16:53:44 -04001452
Vivien Didelota935c052016-09-29 12:21:53 -04001453 next.fid = (val & 0xf00) >> 4;
1454 next.fid |= val & 0xf;
Vivien Didelot2e7bd5e2016-03-31 16:53:41 -04001455 }
Vivien Didelotb8fee952015-08-13 12:52:19 -04001456
Vivien Didelotfad09c72016-06-21 12:28:20 -04001457 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
Vivien Didelota935c052016-09-29 12:21:53 -04001458 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_SID, &val);
1459 if (err)
1460 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001461
Vivien Didelota935c052016-09-29 12:21:53 -04001462 next.sid = val & GLOBAL_VTU_SID_MASK;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001463 }
1464 }
1465
1466 *entry = next;
1467 return 0;
1468}
1469
Vivien Didelotf81ec902016-05-09 13:22:58 -04001470static int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
1471 struct switchdev_obj_port_vlan *vlan,
1472 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001473{
Vivien Didelot04bed142016-08-31 18:06:13 -04001474 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001475 struct mv88e6xxx_vtu_entry next;
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001476 u16 pvid;
1477 int err;
1478
Vivien Didelotfad09c72016-06-21 12:28:20 -04001479 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001480 return -EOPNOTSUPP;
1481
Vivien Didelotfad09c72016-06-21 12:28:20 -04001482 mutex_lock(&chip->reg_lock);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001483
Vivien Didelot77064f32016-11-04 03:23:30 +01001484 err = mv88e6xxx_port_get_pvid(chip, port, &pvid);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001485 if (err)
1486 goto unlock;
1487
Vivien Didelotfad09c72016-06-21 12:28:20 -04001488 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001489 if (err)
1490 goto unlock;
1491
1492 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001493 err = _mv88e6xxx_vtu_getnext(chip, &next);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001494 if (err)
1495 break;
1496
1497 if (!next.valid)
1498 break;
1499
1500 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1501 continue;
1502
1503 /* reinit and dump this VLAN obj */
Vivien Didelot57d32312016-06-20 13:13:58 -04001504 vlan->vid_begin = next.vid;
1505 vlan->vid_end = next.vid;
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001506 vlan->flags = 0;
1507
1508 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1509 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1510
1511 if (next.vid == pvid)
1512 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1513
1514 err = cb(&vlan->obj);
1515 if (err)
1516 break;
1517 } while (next.vid < GLOBAL_VTU_VID_MASK);
1518
1519unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001520 mutex_unlock(&chip->reg_lock);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001521
1522 return err;
1523}
1524
Vivien Didelotfad09c72016-06-21 12:28:20 -04001525static int _mv88e6xxx_vtu_loadpurge(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001526 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001527{
Vivien Didelot11ea8092016-03-31 16:53:44 -04001528 u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001529 u16 reg = 0;
Vivien Didelota935c052016-09-29 12:21:53 -04001530 int err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001531
Vivien Didelota935c052016-09-29 12:21:53 -04001532 err = _mv88e6xxx_vtu_wait(chip);
1533 if (err)
1534 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001535
1536 if (!entry->valid)
1537 goto loadpurge;
1538
1539 /* Write port member tags */
Vivien Didelota935c052016-09-29 12:21:53 -04001540 err = mv88e6xxx_vtu_data_write(chip, entry);
1541 if (err)
1542 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001543
Vivien Didelotfad09c72016-06-21 12:28:20 -04001544 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001545 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Vivien Didelota935c052016-09-29 12:21:53 -04001546 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_SID, reg);
1547 if (err)
1548 return err;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001549 }
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001550
Vivien Didelot6dc10bb2016-09-29 12:21:55 -04001551 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G1_VTU_FID)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001552 reg = entry->fid & GLOBAL_VTU_FID_MASK;
Vivien Didelota935c052016-09-29 12:21:53 -04001553 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_FID, reg);
1554 if (err)
1555 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001556 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001557 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1558 * VTU DBNum[3:0] are located in VTU Operation 3:0
1559 */
1560 op |= (entry->fid & 0xf0) << 8;
1561 op |= entry->fid & 0xf;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001562 }
1563
1564 reg = GLOBAL_VTU_VID_VALID;
1565loadpurge:
1566 reg |= entry->vid & GLOBAL_VTU_VID_MASK;
Vivien Didelota935c052016-09-29 12:21:53 -04001567 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_VID, reg);
1568 if (err)
1569 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001570
Vivien Didelotfad09c72016-06-21 12:28:20 -04001571 return _mv88e6xxx_vtu_cmd(chip, op);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001572}
1573
Vivien Didelotfad09c72016-06-21 12:28:20 -04001574static int _mv88e6xxx_stu_getnext(struct mv88e6xxx_chip *chip, u8 sid,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001575 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001576{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001577 struct mv88e6xxx_vtu_entry next = { 0 };
Vivien Didelota935c052016-09-29 12:21:53 -04001578 u16 val;
1579 int err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001580
Vivien Didelota935c052016-09-29 12:21:53 -04001581 err = _mv88e6xxx_vtu_wait(chip);
1582 if (err)
1583 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001584
Vivien Didelota935c052016-09-29 12:21:53 -04001585 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_SID,
1586 sid & GLOBAL_VTU_SID_MASK);
1587 if (err)
1588 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001589
Vivien Didelota935c052016-09-29 12:21:53 -04001590 err = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_GET_NEXT);
1591 if (err)
1592 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001593
Vivien Didelota935c052016-09-29 12:21:53 -04001594 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_SID, &val);
1595 if (err)
1596 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001597
Vivien Didelota935c052016-09-29 12:21:53 -04001598 next.sid = val & GLOBAL_VTU_SID_MASK;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001599
Vivien Didelota935c052016-09-29 12:21:53 -04001600 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_VID, &val);
1601 if (err)
1602 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001603
Vivien Didelota935c052016-09-29 12:21:53 -04001604 next.valid = !!(val & GLOBAL_VTU_VID_VALID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001605
1606 if (next.valid) {
Vivien Didelota935c052016-09-29 12:21:53 -04001607 err = mv88e6xxx_stu_data_read(chip, &next);
1608 if (err)
1609 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001610 }
1611
1612 *entry = next;
1613 return 0;
1614}
1615
Vivien Didelotfad09c72016-06-21 12:28:20 -04001616static int _mv88e6xxx_stu_loadpurge(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001617 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001618{
1619 u16 reg = 0;
Vivien Didelota935c052016-09-29 12:21:53 -04001620 int err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001621
Vivien Didelota935c052016-09-29 12:21:53 -04001622 err = _mv88e6xxx_vtu_wait(chip);
1623 if (err)
1624 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001625
1626 if (!entry->valid)
1627 goto loadpurge;
1628
1629 /* Write port states */
Vivien Didelota935c052016-09-29 12:21:53 -04001630 err = mv88e6xxx_stu_data_write(chip, entry);
1631 if (err)
1632 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001633
1634 reg = GLOBAL_VTU_VID_VALID;
1635loadpurge:
Vivien Didelota935c052016-09-29 12:21:53 -04001636 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_VID, reg);
1637 if (err)
1638 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001639
1640 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Vivien Didelota935c052016-09-29 12:21:53 -04001641 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_SID, reg);
1642 if (err)
1643 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001644
Vivien Didelotfad09c72016-06-21 12:28:20 -04001645 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_LOAD_PURGE);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001646}
1647
Vivien Didelotfad09c72016-06-21 12:28:20 -04001648static int _mv88e6xxx_fid_new(struct mv88e6xxx_chip *chip, u16 *fid)
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001649{
1650 DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001651 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001652 int i, err;
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001653
1654 bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);
1655
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001656 /* Set every FID bit used by the (un)bridged ports */
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001657 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotb4e48c52016-11-04 03:23:29 +01001658 err = mv88e6xxx_port_get_fid(chip, i, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001659 if (err)
1660 return err;
1661
1662 set_bit(*fid, fid_bitmap);
1663 }
1664
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001665 /* Set every FID bit used by the VLAN entries */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001666 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001667 if (err)
1668 return err;
1669
1670 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001671 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001672 if (err)
1673 return err;
1674
1675 if (!vlan.valid)
1676 break;
1677
1678 set_bit(vlan.fid, fid_bitmap);
1679 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
1680
1681 /* The reset value 0x000 is used to indicate that multiple address
1682 * databases are not needed. Return the next positive available.
1683 */
1684 *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1);
Vivien Didelotfad09c72016-06-21 12:28:20 -04001685 if (unlikely(*fid >= mv88e6xxx_num_databases(chip)))
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001686 return -ENOSPC;
1687
1688 /* Clear the database */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001689 return _mv88e6xxx_atu_flush(chip, *fid, true);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001690}
1691
Vivien Didelotfad09c72016-06-21 12:28:20 -04001692static int _mv88e6xxx_vtu_new(struct mv88e6xxx_chip *chip, u16 vid,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001693 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001694{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001695 struct dsa_switch *ds = chip->ds;
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001696 struct mv88e6xxx_vtu_entry vlan = {
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001697 .valid = true,
1698 .vid = vid,
1699 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001700 int i, err;
1701
Vivien Didelotfad09c72016-06-21 12:28:20 -04001702 err = _mv88e6xxx_fid_new(chip, &vlan.fid);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001703 if (err)
1704 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001705
Vivien Didelot3d131f02015-11-03 10:52:52 -05001706 /* exclude all ports except the CPU and DSA ports */
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001707 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i)
Vivien Didelot3d131f02015-11-03 10:52:52 -05001708 vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)
1709 ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED
1710 : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001711
Vivien Didelotfad09c72016-06-21 12:28:20 -04001712 if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
1713 mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip)) {
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001714 struct mv88e6xxx_vtu_entry vstp;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001715
1716 /* Adding a VTU entry requires a valid STU entry. As VSTP is not
1717 * implemented, only one STU entry is needed to cover all VTU
1718 * entries. Thus, validate the SID 0.
1719 */
1720 vlan.sid = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001721 err = _mv88e6xxx_stu_getnext(chip, GLOBAL_VTU_SID_MASK, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001722 if (err)
1723 return err;
1724
1725 if (vstp.sid != vlan.sid || !vstp.valid) {
1726 memset(&vstp, 0, sizeof(vstp));
1727 vstp.valid = true;
1728 vstp.sid = vlan.sid;
1729
Vivien Didelotfad09c72016-06-21 12:28:20 -04001730 err = _mv88e6xxx_stu_loadpurge(chip, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001731 if (err)
1732 return err;
1733 }
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001734 }
1735
1736 *entry = vlan;
1737 return 0;
1738}
1739
Vivien Didelotfad09c72016-06-21 12:28:20 -04001740static int _mv88e6xxx_vtu_get(struct mv88e6xxx_chip *chip, u16 vid,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001741 struct mv88e6xxx_vtu_entry *entry, bool creat)
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001742{
1743 int err;
1744
1745 if (!vid)
1746 return -EINVAL;
1747
Vivien Didelotfad09c72016-06-21 12:28:20 -04001748 err = _mv88e6xxx_vtu_vid_write(chip, vid - 1);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001749 if (err)
1750 return err;
1751
Vivien Didelotfad09c72016-06-21 12:28:20 -04001752 err = _mv88e6xxx_vtu_getnext(chip, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001753 if (err)
1754 return err;
1755
1756 if (entry->vid != vid || !entry->valid) {
1757 if (!creat)
1758 return -EOPNOTSUPP;
1759 /* -ENOENT would've been more appropriate, but switchdev expects
1760 * -EOPNOTSUPP to inform bridge about an eventual software VLAN.
1761 */
1762
Vivien Didelotfad09c72016-06-21 12:28:20 -04001763 err = _mv88e6xxx_vtu_new(chip, vid, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001764 }
1765
1766 return err;
1767}
1768
Vivien Didelotda9c3592016-02-12 12:09:40 -05001769static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
1770 u16 vid_begin, u16 vid_end)
1771{
Vivien Didelot04bed142016-08-31 18:06:13 -04001772 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001773 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelotda9c3592016-02-12 12:09:40 -05001774 int i, err;
1775
1776 if (!vid_begin)
1777 return -EOPNOTSUPP;
1778
Vivien Didelotfad09c72016-06-21 12:28:20 -04001779 mutex_lock(&chip->reg_lock);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001780
Vivien Didelotfad09c72016-06-21 12:28:20 -04001781 err = _mv88e6xxx_vtu_vid_write(chip, vid_begin - 1);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001782 if (err)
1783 goto unlock;
1784
1785 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001786 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001787 if (err)
1788 goto unlock;
1789
1790 if (!vlan.valid)
1791 break;
1792
1793 if (vlan.vid > vid_end)
1794 break;
1795
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001796 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotda9c3592016-02-12 12:09:40 -05001797 if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i))
1798 continue;
1799
1800 if (vlan.data[i] ==
1801 GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1802 continue;
1803
Vivien Didelotfad09c72016-06-21 12:28:20 -04001804 if (chip->ports[i].bridge_dev ==
1805 chip->ports[port].bridge_dev)
Vivien Didelotda9c3592016-02-12 12:09:40 -05001806 break; /* same bridge, check next VLAN */
1807
Andrew Lunnc8b09802016-06-04 21:16:57 +02001808 netdev_warn(ds->ports[port].netdev,
Vivien Didelotda9c3592016-02-12 12:09:40 -05001809 "hardware VLAN %d already used by %s\n",
1810 vlan.vid,
Vivien Didelotfad09c72016-06-21 12:28:20 -04001811 netdev_name(chip->ports[i].bridge_dev));
Vivien Didelotda9c3592016-02-12 12:09:40 -05001812 err = -EOPNOTSUPP;
1813 goto unlock;
1814 }
1815 } while (vlan.vid < vid_end);
1816
1817unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001818 mutex_unlock(&chip->reg_lock);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001819
1820 return err;
1821}
1822
Vivien Didelotf81ec902016-05-09 13:22:58 -04001823static int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port,
1824 bool vlan_filtering)
Vivien Didelot214cdb92016-02-26 13:16:08 -05001825{
Vivien Didelot04bed142016-08-31 18:06:13 -04001826 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot385a0992016-11-04 03:23:31 +01001827 u16 mode = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE :
Vivien Didelot214cdb92016-02-26 13:16:08 -05001828 PORT_CONTROL_2_8021Q_DISABLED;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001829 int err;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001830
Vivien Didelotfad09c72016-06-21 12:28:20 -04001831 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001832 return -EOPNOTSUPP;
1833
Vivien Didelotfad09c72016-06-21 12:28:20 -04001834 mutex_lock(&chip->reg_lock);
Vivien Didelot385a0992016-11-04 03:23:31 +01001835 err = mv88e6xxx_port_set_8021q_mode(chip, port, mode);
Vivien Didelotfad09c72016-06-21 12:28:20 -04001836 mutex_unlock(&chip->reg_lock);
Vivien Didelot214cdb92016-02-26 13:16:08 -05001837
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001838 return err;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001839}
1840
Vivien Didelot57d32312016-06-20 13:13:58 -04001841static int
1842mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
1843 const struct switchdev_obj_port_vlan *vlan,
1844 struct switchdev_trans *trans)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001845{
Vivien Didelot04bed142016-08-31 18:06:13 -04001846 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotda9c3592016-02-12 12:09:40 -05001847 int err;
1848
Vivien Didelotfad09c72016-06-21 12:28:20 -04001849 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001850 return -EOPNOTSUPP;
1851
Vivien Didelotda9c3592016-02-12 12:09:40 -05001852 /* If the requested port doesn't belong to the same bridge as the VLAN
1853 * members, do not support it (yet) and fallback to software VLAN.
1854 */
1855 err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin,
1856 vlan->vid_end);
1857 if (err)
1858 return err;
1859
Vivien Didelot76e398a2015-11-01 12:33:55 -05001860 /* We don't need any dynamic resource from the kernel (yet),
1861 * so skip the prepare phase.
1862 */
1863 return 0;
1864}
1865
Vivien Didelotfad09c72016-06-21 12:28:20 -04001866static int _mv88e6xxx_port_vlan_add(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001867 u16 vid, bool untagged)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001868{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001869 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001870 int err;
1871
Vivien Didelotfad09c72016-06-21 12:28:20 -04001872 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, true);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001873 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001874 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001875
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001876 vlan.data[port] = untagged ?
1877 GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
1878 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
1879
Vivien Didelotfad09c72016-06-21 12:28:20 -04001880 return _mv88e6xxx_vtu_loadpurge(chip, &vlan);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001881}
1882
Vivien Didelotf81ec902016-05-09 13:22:58 -04001883static void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
1884 const struct switchdev_obj_port_vlan *vlan,
1885 struct switchdev_trans *trans)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001886{
Vivien Didelot04bed142016-08-31 18:06:13 -04001887 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001888 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1889 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1890 u16 vid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001891
Vivien Didelotfad09c72016-06-21 12:28:20 -04001892 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001893 return;
1894
Vivien Didelotfad09c72016-06-21 12:28:20 -04001895 mutex_lock(&chip->reg_lock);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001896
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001897 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
Vivien Didelotfad09c72016-06-21 12:28:20 -04001898 if (_mv88e6xxx_port_vlan_add(chip, port, vid, untagged))
Andrew Lunnc8b09802016-06-04 21:16:57 +02001899 netdev_err(ds->ports[port].netdev,
1900 "failed to add VLAN %d%c\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001901 vid, untagged ? 'u' : 't');
Vivien Didelot76e398a2015-11-01 12:33:55 -05001902
Vivien Didelot77064f32016-11-04 03:23:30 +01001903 if (pvid && mv88e6xxx_port_set_pvid(chip, port, vlan->vid_end))
Andrew Lunnc8b09802016-06-04 21:16:57 +02001904 netdev_err(ds->ports[port].netdev, "failed to set PVID %d\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001905 vlan->vid_end);
1906
Vivien Didelotfad09c72016-06-21 12:28:20 -04001907 mutex_unlock(&chip->reg_lock);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001908}
1909
Vivien Didelotfad09c72016-06-21 12:28:20 -04001910static int _mv88e6xxx_port_vlan_del(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001911 int port, u16 vid)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001912{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001913 struct dsa_switch *ds = chip->ds;
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001914 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001915 int i, err;
1916
Vivien Didelotfad09c72016-06-21 12:28:20 -04001917 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001918 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001919 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001920
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001921 /* Tell switchdev if this VLAN is handled in software */
1922 if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
Vivien Didelot3c06f082016-02-05 14:04:39 -05001923 return -EOPNOTSUPP;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001924
1925 vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
1926
1927 /* keep the VLAN unless all ports are excluded */
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001928 vlan.valid = false;
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001929 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelot3d131f02015-11-03 10:52:52 -05001930 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001931 continue;
1932
1933 if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001934 vlan.valid = true;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001935 break;
1936 }
1937 }
1938
Vivien Didelotfad09c72016-06-21 12:28:20 -04001939 err = _mv88e6xxx_vtu_loadpurge(chip, &vlan);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001940 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001941 return err;
1942
Vivien Didelotfad09c72016-06-21 12:28:20 -04001943 return _mv88e6xxx_atu_remove(chip, vlan.fid, port, false);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001944}
1945
Vivien Didelotf81ec902016-05-09 13:22:58 -04001946static int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
1947 const struct switchdev_obj_port_vlan *vlan)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001948{
Vivien Didelot04bed142016-08-31 18:06:13 -04001949 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001950 u16 pvid, vid;
1951 int err = 0;
1952
Vivien Didelotfad09c72016-06-21 12:28:20 -04001953 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001954 return -EOPNOTSUPP;
1955
Vivien Didelotfad09c72016-06-21 12:28:20 -04001956 mutex_lock(&chip->reg_lock);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001957
Vivien Didelot77064f32016-11-04 03:23:30 +01001958 err = mv88e6xxx_port_get_pvid(chip, port, &pvid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001959 if (err)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001960 goto unlock;
1961
Vivien Didelot76e398a2015-11-01 12:33:55 -05001962 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001963 err = _mv88e6xxx_port_vlan_del(chip, port, vid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001964 if (err)
1965 goto unlock;
1966
1967 if (vid == pvid) {
Vivien Didelot77064f32016-11-04 03:23:30 +01001968 err = mv88e6xxx_port_set_pvid(chip, port, 0);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001969 if (err)
1970 goto unlock;
1971 }
1972 }
1973
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001974unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001975 mutex_unlock(&chip->reg_lock);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001976
1977 return err;
1978}
1979
Vivien Didelotfad09c72016-06-21 12:28:20 -04001980static int _mv88e6xxx_atu_mac_write(struct mv88e6xxx_chip *chip,
Vivien Didelotc5723ac2015-08-10 09:09:48 -04001981 const unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001982{
Vivien Didelota935c052016-09-29 12:21:53 -04001983 int i, err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001984
1985 for (i = 0; i < 3; i++) {
Vivien Didelota935c052016-09-29 12:21:53 -04001986 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_MAC_01 + i,
1987 (addr[i * 2] << 8) | addr[i * 2 + 1]);
1988 if (err)
1989 return err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001990 }
1991
1992 return 0;
1993}
1994
Vivien Didelotfad09c72016-06-21 12:28:20 -04001995static int _mv88e6xxx_atu_mac_read(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001996 unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001997{
Vivien Didelota935c052016-09-29 12:21:53 -04001998 u16 val;
1999 int i, err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002000
2001 for (i = 0; i < 3; i++) {
Vivien Didelota935c052016-09-29 12:21:53 -04002002 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_MAC_01 + i, &val);
2003 if (err)
2004 return err;
2005
2006 addr[i * 2] = val >> 8;
2007 addr[i * 2 + 1] = val & 0xff;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002008 }
2009
2010 return 0;
2011}
2012
Vivien Didelotfad09c72016-06-21 12:28:20 -04002013static int _mv88e6xxx_atu_load(struct mv88e6xxx_chip *chip,
Vivien Didelotfd231c82015-08-10 09:09:50 -04002014 struct mv88e6xxx_atu_entry *entry)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002015{
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002016 int ret;
2017
Vivien Didelotfad09c72016-06-21 12:28:20 -04002018 ret = _mv88e6xxx_atu_wait(chip);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002019 if (ret < 0)
2020 return ret;
2021
Vivien Didelotfad09c72016-06-21 12:28:20 -04002022 ret = _mv88e6xxx_atu_mac_write(chip, entry->mac);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002023 if (ret < 0)
2024 return ret;
2025
Vivien Didelotfad09c72016-06-21 12:28:20 -04002026 ret = _mv88e6xxx_atu_data_write(chip, entry);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002027 if (ret < 0)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002028 return ret;
2029
Vivien Didelotfad09c72016-06-21 12:28:20 -04002030 return _mv88e6xxx_atu_cmd(chip, entry->fid, GLOBAL_ATU_OP_LOAD_DB);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002031}
David S. Millercdf09692015-08-11 12:00:37 -07002032
Vivien Didelot88472932016-09-19 19:56:11 -04002033static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
2034 struct mv88e6xxx_atu_entry *entry);
2035
2036static int mv88e6xxx_atu_get(struct mv88e6xxx_chip *chip, int fid,
2037 const u8 *addr, struct mv88e6xxx_atu_entry *entry)
2038{
2039 struct mv88e6xxx_atu_entry next;
2040 int err;
2041
2042 eth_broadcast_addr(next.mac);
2043
2044 err = _mv88e6xxx_atu_mac_write(chip, next.mac);
2045 if (err)
2046 return err;
2047
2048 do {
2049 err = _mv88e6xxx_atu_getnext(chip, fid, &next);
2050 if (err)
2051 return err;
2052
2053 if (next.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2054 break;
2055
2056 if (ether_addr_equal(next.mac, addr)) {
2057 *entry = next;
2058 return 0;
2059 }
2060 } while (!is_broadcast_ether_addr(next.mac));
2061
2062 memset(entry, 0, sizeof(*entry));
2063 entry->fid = fid;
2064 ether_addr_copy(entry->mac, addr);
2065
2066 return 0;
2067}
2068
Vivien Didelot83dabd12016-08-31 11:50:04 -04002069static int mv88e6xxx_port_db_load_purge(struct mv88e6xxx_chip *chip, int port,
2070 const unsigned char *addr, u16 vid,
2071 u8 state)
Vivien Didelotfd231c82015-08-10 09:09:50 -04002072{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04002073 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelot88472932016-09-19 19:56:11 -04002074 struct mv88e6xxx_atu_entry entry;
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002075 int err;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002076
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002077 /* Null VLAN ID corresponds to the port private database */
2078 if (vid == 0)
Vivien Didelotb4e48c52016-11-04 03:23:29 +01002079 err = mv88e6xxx_port_get_fid(chip, port, &vlan.fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002080 else
Vivien Didelotfad09c72016-06-21 12:28:20 -04002081 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002082 if (err)
2083 return err;
2084
Vivien Didelot88472932016-09-19 19:56:11 -04002085 err = mv88e6xxx_atu_get(chip, vlan.fid, addr, &entry);
2086 if (err)
2087 return err;
2088
2089 /* Purge the ATU entry only if no port is using it anymore */
2090 if (state == GLOBAL_ATU_DATA_STATE_UNUSED) {
2091 entry.portv_trunkid &= ~BIT(port);
2092 if (!entry.portv_trunkid)
2093 entry.state = GLOBAL_ATU_DATA_STATE_UNUSED;
2094 } else {
2095 entry.portv_trunkid |= BIT(port);
2096 entry.state = state;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002097 }
2098
Vivien Didelotfad09c72016-06-21 12:28:20 -04002099 return _mv88e6xxx_atu_load(chip, &entry);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002100}
2101
Vivien Didelotf81ec902016-05-09 13:22:58 -04002102static int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
2103 const struct switchdev_obj_port_fdb *fdb,
2104 struct switchdev_trans *trans)
Vivien Didelot146a3202015-10-08 11:35:12 -04002105{
2106 /* We don't need any dynamic resource from the kernel (yet),
2107 * so skip the prepare phase.
2108 */
2109 return 0;
2110}
2111
Vivien Didelotf81ec902016-05-09 13:22:58 -04002112static void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
2113 const struct switchdev_obj_port_fdb *fdb,
2114 struct switchdev_trans *trans)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002115{
Vivien Didelot04bed142016-08-31 18:06:13 -04002116 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot6630e232015-08-06 01:44:07 -04002117
Vivien Didelotfad09c72016-06-21 12:28:20 -04002118 mutex_lock(&chip->reg_lock);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002119 if (mv88e6xxx_port_db_load_purge(chip, port, fdb->addr, fdb->vid,
2120 GLOBAL_ATU_DATA_STATE_UC_STATIC))
2121 netdev_err(ds->ports[port].netdev, "failed to load unicast MAC address\n");
Vivien Didelotfad09c72016-06-21 12:28:20 -04002122 mutex_unlock(&chip->reg_lock);
David S. Millercdf09692015-08-11 12:00:37 -07002123}
2124
Vivien Didelotf81ec902016-05-09 13:22:58 -04002125static int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
2126 const struct switchdev_obj_port_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -07002127{
Vivien Didelot04bed142016-08-31 18:06:13 -04002128 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot83dabd12016-08-31 11:50:04 -04002129 int err;
David S. Millercdf09692015-08-11 12:00:37 -07002130
Vivien Didelotfad09c72016-06-21 12:28:20 -04002131 mutex_lock(&chip->reg_lock);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002132 err = mv88e6xxx_port_db_load_purge(chip, port, fdb->addr, fdb->vid,
2133 GLOBAL_ATU_DATA_STATE_UNUSED);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002134 mutex_unlock(&chip->reg_lock);
David S. Millercdf09692015-08-11 12:00:37 -07002135
Vivien Didelot83dabd12016-08-31 11:50:04 -04002136 return err;
David S. Millercdf09692015-08-11 12:00:37 -07002137}
2138
Vivien Didelotfad09c72016-06-21 12:28:20 -04002139static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
Vivien Didelot1d194042015-08-10 09:09:51 -04002140 struct mv88e6xxx_atu_entry *entry)
David S. Millercdf09692015-08-11 12:00:37 -07002141{
Vivien Didelot1d194042015-08-10 09:09:51 -04002142 struct mv88e6xxx_atu_entry next = { 0 };
Vivien Didelota935c052016-09-29 12:21:53 -04002143 u16 val;
2144 int err;
Vivien Didelot1d194042015-08-10 09:09:51 -04002145
2146 next.fid = fid;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002147
Vivien Didelota935c052016-09-29 12:21:53 -04002148 err = _mv88e6xxx_atu_wait(chip);
2149 if (err)
2150 return err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002151
Vivien Didelota935c052016-09-29 12:21:53 -04002152 err = _mv88e6xxx_atu_cmd(chip, fid, GLOBAL_ATU_OP_GET_NEXT_DB);
2153 if (err)
2154 return err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002155
Vivien Didelota935c052016-09-29 12:21:53 -04002156 err = _mv88e6xxx_atu_mac_read(chip, next.mac);
2157 if (err)
2158 return err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002159
Vivien Didelota935c052016-09-29 12:21:53 -04002160 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_DATA, &val);
2161 if (err)
2162 return err;
Vivien Didelot1d194042015-08-10 09:09:51 -04002163
Vivien Didelota935c052016-09-29 12:21:53 -04002164 next.state = val & GLOBAL_ATU_DATA_STATE_MASK;
Vivien Didelot1d194042015-08-10 09:09:51 -04002165 if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2166 unsigned int mask, shift;
2167
Vivien Didelota935c052016-09-29 12:21:53 -04002168 if (val & GLOBAL_ATU_DATA_TRUNK) {
Vivien Didelot1d194042015-08-10 09:09:51 -04002169 next.trunk = true;
2170 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
2171 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
2172 } else {
2173 next.trunk = false;
2174 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
2175 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
2176 }
2177
Vivien Didelota935c052016-09-29 12:21:53 -04002178 next.portv_trunkid = (val & mask) >> shift;
Vivien Didelot1d194042015-08-10 09:09:51 -04002179 }
2180
2181 *entry = next;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002182 return 0;
2183}
2184
Vivien Didelot83dabd12016-08-31 11:50:04 -04002185static int mv88e6xxx_port_db_dump_fid(struct mv88e6xxx_chip *chip,
2186 u16 fid, u16 vid, int port,
2187 struct switchdev_obj *obj,
2188 int (*cb)(struct switchdev_obj *obj))
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002189{
2190 struct mv88e6xxx_atu_entry addr = {
2191 .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2192 };
2193 int err;
2194
Vivien Didelotfad09c72016-06-21 12:28:20 -04002195 err = _mv88e6xxx_atu_mac_write(chip, addr.mac);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002196 if (err)
2197 return err;
2198
2199 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002200 err = _mv88e6xxx_atu_getnext(chip, fid, &addr);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002201 if (err)
Vivien Didelot83dabd12016-08-31 11:50:04 -04002202 return err;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002203
2204 if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2205 break;
2206
Vivien Didelot83dabd12016-08-31 11:50:04 -04002207 if (addr.trunk || (addr.portv_trunkid & BIT(port)) == 0)
2208 continue;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002209
Vivien Didelot83dabd12016-08-31 11:50:04 -04002210 if (obj->id == SWITCHDEV_OBJ_ID_PORT_FDB) {
2211 struct switchdev_obj_port_fdb *fdb;
2212
2213 if (!is_unicast_ether_addr(addr.mac))
2214 continue;
2215
2216 fdb = SWITCHDEV_OBJ_PORT_FDB(obj);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002217 fdb->vid = vid;
2218 ether_addr_copy(fdb->addr, addr.mac);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002219 if (addr.state == GLOBAL_ATU_DATA_STATE_UC_STATIC)
2220 fdb->ndm_state = NUD_NOARP;
2221 else
2222 fdb->ndm_state = NUD_REACHABLE;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04002223 } else if (obj->id == SWITCHDEV_OBJ_ID_PORT_MDB) {
2224 struct switchdev_obj_port_mdb *mdb;
2225
2226 if (!is_multicast_ether_addr(addr.mac))
2227 continue;
2228
2229 mdb = SWITCHDEV_OBJ_PORT_MDB(obj);
2230 mdb->vid = vid;
2231 ether_addr_copy(mdb->addr, addr.mac);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002232 } else {
2233 return -EOPNOTSUPP;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002234 }
Vivien Didelot83dabd12016-08-31 11:50:04 -04002235
2236 err = cb(obj);
2237 if (err)
2238 return err;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002239 } while (!is_broadcast_ether_addr(addr.mac));
2240
2241 return err;
2242}
2243
Vivien Didelot83dabd12016-08-31 11:50:04 -04002244static int mv88e6xxx_port_db_dump(struct mv88e6xxx_chip *chip, int port,
2245 struct switchdev_obj *obj,
2246 int (*cb)(struct switchdev_obj *obj))
2247{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04002248 struct mv88e6xxx_vtu_entry vlan = {
Vivien Didelot83dabd12016-08-31 11:50:04 -04002249 .vid = GLOBAL_VTU_VID_MASK, /* all ones */
2250 };
2251 u16 fid;
2252 int err;
2253
2254 /* Dump port's default Filtering Information Database (VLAN ID 0) */
Vivien Didelotb4e48c52016-11-04 03:23:29 +01002255 err = mv88e6xxx_port_get_fid(chip, port, &fid);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002256 if (err)
2257 return err;
2258
2259 err = mv88e6xxx_port_db_dump_fid(chip, fid, 0, port, obj, cb);
2260 if (err)
2261 return err;
2262
2263 /* Dump VLANs' Filtering Information Databases */
2264 err = _mv88e6xxx_vtu_vid_write(chip, vlan.vid);
2265 if (err)
2266 return err;
2267
2268 do {
2269 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
2270 if (err)
2271 return err;
2272
2273 if (!vlan.valid)
2274 break;
2275
2276 err = mv88e6xxx_port_db_dump_fid(chip, vlan.fid, vlan.vid, port,
2277 obj, cb);
2278 if (err)
2279 return err;
2280 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
2281
2282 return err;
2283}
2284
Vivien Didelotf81ec902016-05-09 13:22:58 -04002285static int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
2286 struct switchdev_obj_port_fdb *fdb,
2287 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotf33475b2015-10-22 09:34:41 -04002288{
Vivien Didelot04bed142016-08-31 18:06:13 -04002289 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002290 int err;
2291
Vivien Didelotfad09c72016-06-21 12:28:20 -04002292 mutex_lock(&chip->reg_lock);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002293 err = mv88e6xxx_port_db_dump(chip, port, &fdb->obj, cb);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002294 mutex_unlock(&chip->reg_lock);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002295
2296 return err;
2297}
2298
Vivien Didelotf81ec902016-05-09 13:22:58 -04002299static int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
2300 struct net_device *bridge)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002301{
Vivien Didelot04bed142016-08-31 18:06:13 -04002302 struct mv88e6xxx_chip *chip = ds->priv;
Colin Ian King1d9619d2016-04-25 23:11:22 +01002303 int i, err = 0;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002304
Vivien Didelotfad09c72016-06-21 12:28:20 -04002305 mutex_lock(&chip->reg_lock);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002306
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002307 /* Assign the bridge and remap each port's VLANTable */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002308 chip->ports[port].bridge_dev = bridge;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002309
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002310 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002311 if (chip->ports[i].bridge_dev == bridge) {
2312 err = _mv88e6xxx_port_based_vlan_map(chip, i);
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002313 if (err)
2314 break;
2315 }
2316 }
2317
Vivien Didelotfad09c72016-06-21 12:28:20 -04002318 mutex_unlock(&chip->reg_lock);
Vivien Didelota6692752016-02-12 12:09:39 -05002319
Vivien Didelot466dfa02016-02-26 13:16:05 -05002320 return err;
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002321}
2322
Vivien Didelotf81ec902016-05-09 13:22:58 -04002323static void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002324{
Vivien Didelot04bed142016-08-31 18:06:13 -04002325 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002326 struct net_device *bridge = chip->ports[port].bridge_dev;
Vivien Didelot16bfa702016-03-13 16:21:33 -04002327 int i;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002328
Vivien Didelotfad09c72016-06-21 12:28:20 -04002329 mutex_lock(&chip->reg_lock);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002330
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002331 /* Unassign the bridge and remap each port's VLANTable */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002332 chip->ports[port].bridge_dev = NULL;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002333
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002334 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002335 if (i == port || chip->ports[i].bridge_dev == bridge)
2336 if (_mv88e6xxx_port_based_vlan_map(chip, i))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002337 netdev_warn(ds->ports[i].netdev,
2338 "failed to remap\n");
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002339
Vivien Didelotfad09c72016-06-21 12:28:20 -04002340 mutex_unlock(&chip->reg_lock);
Vivien Didelot66d9cd02016-02-05 14:07:14 -05002341}
2342
Vivien Didelotfad09c72016-06-21 12:28:20 -04002343static int mv88e6xxx_switch_reset(struct mv88e6xxx_chip *chip)
Vivien Didelot552238b2016-05-09 13:22:49 -04002344{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002345 bool ppu_active = mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE);
Vivien Didelot552238b2016-05-09 13:22:49 -04002346 u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002347 struct gpio_desc *gpiod = chip->reset;
Vivien Didelot552238b2016-05-09 13:22:49 -04002348 unsigned long timeout;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002349 u16 reg;
Vivien Didelota935c052016-09-29 12:21:53 -04002350 int err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002351 int i;
2352
2353 /* Set all ports to the disabled state. */
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002354 for (i = 0; i < mv88e6xxx_num_ports(chip); i++) {
Vivien Didelote28def332016-11-04 03:23:27 +01002355 err = mv88e6xxx_port_set_state(chip, i,
2356 PORT_CONTROL_STATE_DISABLED);
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002357 if (err)
2358 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002359 }
2360
2361 /* Wait for transmit queues to drain. */
2362 usleep_range(2000, 4000);
2363
2364 /* If there is a gpio connected to the reset pin, toggle it */
2365 if (gpiod) {
2366 gpiod_set_value_cansleep(gpiod, 1);
2367 usleep_range(10000, 20000);
2368 gpiod_set_value_cansleep(gpiod, 0);
2369 usleep_range(10000, 20000);
2370 }
2371
2372 /* Reset the switch. Keep the PPU active if requested. The PPU
2373 * needs to be active to support indirect phy register access
2374 * through global registers 0x18 and 0x19.
2375 */
2376 if (ppu_active)
Vivien Didelota935c052016-09-29 12:21:53 -04002377 err = mv88e6xxx_g1_write(chip, 0x04, 0xc000);
Vivien Didelot552238b2016-05-09 13:22:49 -04002378 else
Vivien Didelota935c052016-09-29 12:21:53 -04002379 err = mv88e6xxx_g1_write(chip, 0x04, 0xc400);
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002380 if (err)
2381 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002382
2383 /* Wait up to one second for reset to complete. */
2384 timeout = jiffies + 1 * HZ;
2385 while (time_before(jiffies, timeout)) {
Vivien Didelota935c052016-09-29 12:21:53 -04002386 err = mv88e6xxx_g1_read(chip, 0x00, &reg);
2387 if (err)
2388 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002389
Vivien Didelota935c052016-09-29 12:21:53 -04002390 if ((reg & is_reset) == is_reset)
Vivien Didelot552238b2016-05-09 13:22:49 -04002391 break;
2392 usleep_range(1000, 2000);
2393 }
2394 if (time_after(jiffies, timeout))
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002395 err = -ETIMEDOUT;
Vivien Didelot552238b2016-05-09 13:22:49 -04002396 else
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002397 err = 0;
Vivien Didelot552238b2016-05-09 13:22:49 -04002398
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002399 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002400}
2401
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002402static int mv88e6xxx_serdes_power_on(struct mv88e6xxx_chip *chip)
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002403{
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002404 u16 val;
2405 int err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002406
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002407 /* Clear Power Down bit */
2408 err = mv88e6xxx_serdes_read(chip, MII_BMCR, &val);
2409 if (err)
2410 return err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002411
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002412 if (val & BMCR_PDOWN) {
2413 val &= ~BMCR_PDOWN;
2414 err = mv88e6xxx_serdes_write(chip, MII_BMCR, val);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002415 }
2416
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002417 return err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002418}
2419
Vivien Didelotfad09c72016-06-21 12:28:20 -04002420static int mv88e6xxx_setup_port(struct mv88e6xxx_chip *chip, int port)
Guenter Roeckd827e882015-03-26 18:36:29 -07002421{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002422 struct dsa_switch *ds = chip->ds;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002423 int err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002424 u16 reg;
Guenter Roeckd827e882015-03-26 18:36:29 -07002425
Vivien Didelotd78343d2016-11-04 03:23:36 +01002426 /* MAC Forcing register: don't force link, speed, duplex or flow control
2427 * state to any particular values on physical ports, but force the CPU
2428 * port and all DSA ports to their maximum bandwidth and full duplex.
2429 */
2430 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))
2431 err = mv88e6xxx_port_setup_mac(chip, port, LINK_FORCED_UP,
2432 SPEED_MAX, DUPLEX_FULL,
2433 PHY_INTERFACE_MODE_NA);
2434 else
2435 err = mv88e6xxx_port_setup_mac(chip, port, LINK_UNFORCED,
2436 SPEED_UNFORCED, DUPLEX_UNFORCED,
2437 PHY_INTERFACE_MODE_NA);
2438 if (err)
2439 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002440
2441 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
2442 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
2443 * tunneling, determine priority by looking at 802.1p and IP
2444 * priority fields (IP prio has precedence), and set STP state
2445 * to Forwarding.
2446 *
2447 * If this is the CPU link, use DSA or EDSA tagging depending
2448 * on which tagging mode was configured.
2449 *
2450 * If this is a link to another switch, use DSA tagging mode.
2451 *
2452 * If this is the upstream port for this switch, enable
2453 * forwarding of unknown unicasts and multicasts.
2454 */
2455 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002456 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2457 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2458 mv88e6xxx_6095_family(chip) || mv88e6xxx_6065_family(chip) ||
2459 mv88e6xxx_6185_family(chip) || mv88e6xxx_6320_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002460 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
2461 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
2462 PORT_CONTROL_STATE_FORWARDING;
2463 if (dsa_is_cpu_port(ds, port)) {
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002464 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA))
Andrew Lunn5377b802016-06-04 21:17:02 +02002465 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA |
Andrew Lunnc047a1f2015-09-29 01:50:56 +02002466 PORT_CONTROL_FORWARD_UNKNOWN_MC;
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002467 else
2468 reg |= PORT_CONTROL_DSA_TAG;
Jamie Lentinf027e0c2016-08-22 16:01:04 +02002469 reg |= PORT_CONTROL_EGRESS_ADD_TAG |
2470 PORT_CONTROL_FORWARD_UNKNOWN;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002471 }
Andrew Lunn6083ce72015-08-17 23:52:52 +02002472 if (dsa_is_dsa_port(ds, port)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002473 if (mv88e6xxx_6095_family(chip) ||
2474 mv88e6xxx_6185_family(chip))
Andrew Lunn6083ce72015-08-17 23:52:52 +02002475 reg |= PORT_CONTROL_DSA_TAG;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002476 if (mv88e6xxx_6352_family(chip) ||
2477 mv88e6xxx_6351_family(chip) ||
2478 mv88e6xxx_6165_family(chip) ||
2479 mv88e6xxx_6097_family(chip) ||
2480 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002481 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunn6083ce72015-08-17 23:52:52 +02002482 }
2483
Andrew Lunn54d792f2015-05-06 01:09:47 +02002484 if (port == dsa_upstream_port(ds))
2485 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2486 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2487 }
2488 if (reg) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002489 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL, reg);
2490 if (err)
2491 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002492 }
2493
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002494 /* If this port is connected to a SerDes, make sure the SerDes is not
2495 * powered down.
2496 */
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002497 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_SERDES)) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002498 err = mv88e6xxx_port_read(chip, port, PORT_STATUS, &reg);
2499 if (err)
2500 return err;
2501 reg &= PORT_STATUS_CMODE_MASK;
2502 if ((reg == PORT_STATUS_CMODE_100BASE_X) ||
2503 (reg == PORT_STATUS_CMODE_1000BASE_X) ||
2504 (reg == PORT_STATUS_CMODE_SGMII)) {
2505 err = mv88e6xxx_serdes_power_on(chip);
2506 if (err < 0)
2507 return err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002508 }
2509 }
2510
Vivien Didelot8efdda42015-08-13 12:52:23 -04002511 /* Port Control 2: don't force a good FCS, set the maximum frame size to
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002512 * 10240 bytes, disable 802.1q tags checking, don't discard tagged or
Vivien Didelot8efdda42015-08-13 12:52:23 -04002513 * untagged frames on this port, do a destination address lookup on all
2514 * received packets as usual, disable ARP mirroring and don't send a
2515 * copy of all transmitted/received frames on this port to the CPU.
Andrew Lunn54d792f2015-05-06 01:09:47 +02002516 */
2517 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002518 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2519 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2520 mv88e6xxx_6095_family(chip) || mv88e6xxx_6320_family(chip) ||
2521 mv88e6xxx_6185_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002522 reg = PORT_CONTROL_2_MAP_DA;
2523
Vivien Didelotfad09c72016-06-21 12:28:20 -04002524 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2525 mv88e6xxx_6165_family(chip) || mv88e6xxx_6320_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002526 reg |= PORT_CONTROL_2_JUMBO_10240;
2527
Vivien Didelotfad09c72016-06-21 12:28:20 -04002528 if (mv88e6xxx_6095_family(chip) || mv88e6xxx_6185_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002529 /* Set the upstream port this port should use */
2530 reg |= dsa_upstream_port(ds);
2531 /* enable forwarding of unknown multicast addresses to
2532 * the upstream port
2533 */
2534 if (port == dsa_upstream_port(ds))
2535 reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
2536 }
2537
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002538 reg |= PORT_CONTROL_2_8021Q_DISABLED;
Vivien Didelot8efdda42015-08-13 12:52:23 -04002539
Andrew Lunn54d792f2015-05-06 01:09:47 +02002540 if (reg) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002541 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_2, reg);
2542 if (err)
2543 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002544 }
2545
2546 /* Port Association Vector: when learning source addresses
2547 * of packets, add the address to the address database using
2548 * a port bitmap that has only the bit for this port set and
2549 * the other bits clear.
2550 */
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002551 reg = 1 << port;
Vivien Didelot996ecb82016-04-14 14:42:08 -04002552 /* Disable learning for CPU port */
2553 if (dsa_is_cpu_port(ds, port))
Vivien Didelot65fa4022016-04-14 14:42:07 -04002554 reg = 0;
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002555
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002556 err = mv88e6xxx_port_write(chip, port, PORT_ASSOC_VECTOR, reg);
2557 if (err)
2558 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002559
2560 /* Egress rate control 2: disable egress rate control. */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002561 err = mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL_2, 0x0000);
2562 if (err)
2563 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002564
Vivien Didelotfad09c72016-06-21 12:28:20 -04002565 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2566 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2567 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002568 /* Do not limit the period of time that this port can
2569 * be paused for by the remote end or the period of
2570 * time that this port can pause the remote end.
2571 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002572 err = mv88e6xxx_port_write(chip, port, PORT_PAUSE_CTRL, 0x0000);
2573 if (err)
2574 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002575
2576 /* Port ATU control: disable limiting the number of
2577 * address database entries that this port is allowed
2578 * to use.
2579 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002580 err = mv88e6xxx_port_write(chip, port, PORT_ATU_CONTROL,
2581 0x0000);
Andrew Lunn54d792f2015-05-06 01:09:47 +02002582 /* Priority Override: disable DA, SA and VTU priority
2583 * override.
2584 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002585 err = mv88e6xxx_port_write(chip, port, PORT_PRI_OVERRIDE,
2586 0x0000);
2587 if (err)
2588 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002589
2590 /* Port Ethertype: use the Ethertype DSA Ethertype
2591 * value.
2592 */
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002593 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA)) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002594 err = mv88e6xxx_port_write(chip, port, PORT_ETH_TYPE,
2595 ETH_P_EDSA);
2596 if (err)
2597 return err;
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002598 }
2599
Andrew Lunn54d792f2015-05-06 01:09:47 +02002600 /* Tag Remap: use an identity 802.1p prio -> switch
2601 * prio mapping.
2602 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002603 err = mv88e6xxx_port_write(chip, port, PORT_TAG_REGMAP_0123,
2604 0x3210);
2605 if (err)
2606 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002607
2608 /* Tag Remap 2: use an identity 802.1p prio -> switch
2609 * prio mapping.
2610 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002611 err = mv88e6xxx_port_write(chip, port, PORT_TAG_REGMAP_4567,
2612 0x7654);
2613 if (err)
2614 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002615 }
2616
Jamie Lentin1bc261f2016-08-22 22:47:08 +01002617 /* Rate Control: disable ingress rate limiting. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002618 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2619 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
Vivien Didelotfad09c72016-06-21 12:28:20 -04002620 mv88e6xxx_6320_family(chip)) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002621 err = mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL,
2622 0x0001);
2623 if (err)
2624 return err;
Jamie Lentin1bc261f2016-08-22 22:47:08 +01002625 } else if (mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip)) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002626 err = mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL,
2627 0x0000);
2628 if (err)
2629 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002630 }
2631
Guenter Roeck366f0a02015-03-26 18:36:30 -07002632 /* Port Control 1: disable trunking, disable sending
2633 * learning messages to this port.
Guenter Roeckd827e882015-03-26 18:36:29 -07002634 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002635 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_1, 0x0000);
2636 if (err)
2637 return err;
Guenter Roeckd827e882015-03-26 18:36:29 -07002638
Vivien Didelot207afda2016-04-14 14:42:09 -04002639 /* Port based VLAN map: give each port the same default address
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002640 * database, and allow bidirectional communication between the
2641 * CPU and DSA port(s), and the other ports.
Guenter Roeckd827e882015-03-26 18:36:29 -07002642 */
Vivien Didelotb4e48c52016-11-04 03:23:29 +01002643 err = mv88e6xxx_port_set_fid(chip, port, 0);
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002644 if (err)
2645 return err;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002646
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002647 err = _mv88e6xxx_port_based_vlan_map(chip, port);
2648 if (err)
2649 return err;
Guenter Roeckd827e882015-03-26 18:36:29 -07002650
2651 /* Default VLAN ID and priority: don't set a default VLAN
2652 * ID, and set the default packet priority to zero.
2653 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002654 return mv88e6xxx_port_write(chip, port, PORT_DEFAULT_VLAN, 0x0000);
Andrew Lunndbde9e62015-05-06 01:09:48 +02002655}
2656
Wei Yongjunaa0938c2016-10-18 15:53:37 +00002657static int mv88e6xxx_g1_set_switch_mac(struct mv88e6xxx_chip *chip, u8 *addr)
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002658{
2659 int err;
2660
Vivien Didelota935c052016-09-29 12:21:53 -04002661 err = mv88e6xxx_g1_write(chip, GLOBAL_MAC_01, (addr[0] << 8) | addr[1]);
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002662 if (err)
2663 return err;
2664
Vivien Didelota935c052016-09-29 12:21:53 -04002665 err = mv88e6xxx_g1_write(chip, GLOBAL_MAC_23, (addr[2] << 8) | addr[3]);
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002666 if (err)
2667 return err;
2668
Vivien Didelota935c052016-09-29 12:21:53 -04002669 err = mv88e6xxx_g1_write(chip, GLOBAL_MAC_45, (addr[4] << 8) | addr[5]);
2670 if (err)
2671 return err;
2672
2673 return 0;
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002674}
2675
Vivien Didelotacddbd22016-07-18 20:45:39 -04002676static int mv88e6xxx_g1_set_age_time(struct mv88e6xxx_chip *chip,
2677 unsigned int msecs)
2678{
2679 const unsigned int coeff = chip->info->age_time_coeff;
2680 const unsigned int min = 0x01 * coeff;
2681 const unsigned int max = 0xff * coeff;
2682 u8 age_time;
2683 u16 val;
2684 int err;
2685
2686 if (msecs < min || msecs > max)
2687 return -ERANGE;
2688
2689 /* Round to nearest multiple of coeff */
2690 age_time = (msecs + coeff / 2) / coeff;
2691
Vivien Didelota935c052016-09-29 12:21:53 -04002692 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_CONTROL, &val);
Vivien Didelotacddbd22016-07-18 20:45:39 -04002693 if (err)
2694 return err;
2695
2696 /* AgeTime is 11:4 bits */
2697 val &= ~0xff0;
2698 val |= age_time << 4;
2699
Vivien Didelota935c052016-09-29 12:21:53 -04002700 return mv88e6xxx_g1_write(chip, GLOBAL_ATU_CONTROL, val);
Vivien Didelotacddbd22016-07-18 20:45:39 -04002701}
2702
Vivien Didelot2cfcd962016-07-18 20:45:40 -04002703static int mv88e6xxx_set_ageing_time(struct dsa_switch *ds,
2704 unsigned int ageing_time)
2705{
Vivien Didelot04bed142016-08-31 18:06:13 -04002706 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot2cfcd962016-07-18 20:45:40 -04002707 int err;
2708
2709 mutex_lock(&chip->reg_lock);
2710 err = mv88e6xxx_g1_set_age_time(chip, ageing_time);
2711 mutex_unlock(&chip->reg_lock);
2712
2713 return err;
2714}
2715
Vivien Didelot97299342016-07-18 20:45:30 -04002716static int mv88e6xxx_g1_setup(struct mv88e6xxx_chip *chip)
Vivien Didelot08a01262016-05-09 13:22:50 -04002717{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002718 struct dsa_switch *ds = chip->ds;
Vivien Didelotb0745e872016-05-09 13:22:53 -04002719 u32 upstream_port = dsa_upstream_port(ds);
Vivien Didelot119477b2016-05-09 13:22:51 -04002720 u16 reg;
Vivien Didelot08a01262016-05-09 13:22:50 -04002721 int err;
Vivien Didelot08a01262016-05-09 13:22:50 -04002722
Vivien Didelot119477b2016-05-09 13:22:51 -04002723 /* Enable the PHY Polling Unit if present, don't discard any packets,
2724 * and mask all interrupt sources.
2725 */
Andrew Lunndc30c352016-10-16 19:56:49 +02002726 err = mv88e6xxx_g1_read(chip, GLOBAL_CONTROL, &reg);
2727 if (err < 0)
2728 return err;
2729
2730 reg &= ~GLOBAL_CONTROL_PPU_ENABLE;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002731 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU) ||
2732 mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE))
Vivien Didelot119477b2016-05-09 13:22:51 -04002733 reg |= GLOBAL_CONTROL_PPU_ENABLE;
2734
Vivien Didelota935c052016-09-29 12:21:53 -04002735 err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL, reg);
Vivien Didelot119477b2016-05-09 13:22:51 -04002736 if (err)
2737 return err;
2738
Vivien Didelotb0745e872016-05-09 13:22:53 -04002739 /* Configure the upstream port, and configure it as the port to which
2740 * ingress and egress and ARP monitor frames are to be sent.
2741 */
2742 reg = upstream_port << GLOBAL_MONITOR_CONTROL_INGRESS_SHIFT |
2743 upstream_port << GLOBAL_MONITOR_CONTROL_EGRESS_SHIFT |
2744 upstream_port << GLOBAL_MONITOR_CONTROL_ARP_SHIFT;
Vivien Didelota935c052016-09-29 12:21:53 -04002745 err = mv88e6xxx_g1_write(chip, GLOBAL_MONITOR_CONTROL, reg);
Vivien Didelotb0745e872016-05-09 13:22:53 -04002746 if (err)
2747 return err;
2748
Vivien Didelot50484ff2016-05-09 13:22:54 -04002749 /* Disable remote management, and set the switch's DSA device number. */
Vivien Didelota935c052016-09-29 12:21:53 -04002750 err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL_2,
2751 GLOBAL_CONTROL_2_MULTIPLE_CASCADE |
2752 (ds->index & 0x1f));
Vivien Didelot50484ff2016-05-09 13:22:54 -04002753 if (err)
2754 return err;
2755
Vivien Didelotacddbd22016-07-18 20:45:39 -04002756 /* Clear all the VTU and STU entries */
2757 err = _mv88e6xxx_vtu_stu_flush(chip);
2758 if (err < 0)
2759 return err;
2760
Vivien Didelot08a01262016-05-09 13:22:50 -04002761 /* Set the default address aging time to 5 minutes, and
2762 * enable address learn messages to be sent to all message
2763 * ports.
2764 */
Vivien Didelota935c052016-09-29 12:21:53 -04002765 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_CONTROL,
2766 GLOBAL_ATU_CONTROL_LEARN2ALL);
Vivien Didelot08a01262016-05-09 13:22:50 -04002767 if (err)
2768 return err;
2769
Vivien Didelotacddbd22016-07-18 20:45:39 -04002770 err = mv88e6xxx_g1_set_age_time(chip, 300000);
2771 if (err)
Vivien Didelot97299342016-07-18 20:45:30 -04002772 return err;
2773
2774 /* Clear all ATU entries */
2775 err = _mv88e6xxx_atu_flush(chip, 0, true);
2776 if (err)
2777 return err;
2778
Vivien Didelot08a01262016-05-09 13:22:50 -04002779 /* Configure the IP ToS mapping registers. */
Vivien Didelota935c052016-09-29 12:21:53 -04002780 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_0, 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_1, 0x0000);
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_2, 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_3, 0x5555);
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_4, 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_5, 0xaaaa);
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_6, 0xffff);
Vivien Didelot08a01262016-05-09 13:22:50 -04002799 if (err)
2800 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002801 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_7, 0xffff);
Vivien Didelot08a01262016-05-09 13:22:50 -04002802 if (err)
2803 return err;
2804
2805 /* Configure the IEEE 802.1p priority mapping register. */
Vivien Didelota935c052016-09-29 12:21:53 -04002806 err = mv88e6xxx_g1_write(chip, GLOBAL_IEEE_PRI, 0xfa41);
Vivien Didelot08a01262016-05-09 13:22:50 -04002807 if (err)
2808 return err;
2809
Vivien Didelot97299342016-07-18 20:45:30 -04002810 /* Clear the statistics counters for all ports */
Vivien Didelota935c052016-09-29 12:21:53 -04002811 err = mv88e6xxx_g1_write(chip, GLOBAL_STATS_OP,
2812 GLOBAL_STATS_OP_FLUSH_ALL);
Vivien Didelot97299342016-07-18 20:45:30 -04002813 if (err)
2814 return err;
2815
2816 /* Wait for the flush to complete. */
2817 err = _mv88e6xxx_stats_wait(chip);
2818 if (err)
2819 return err;
2820
2821 return 0;
2822}
2823
Vivien Didelotf81ec902016-05-09 13:22:58 -04002824static int mv88e6xxx_setup(struct dsa_switch *ds)
Guenter Roeckacdaffc2015-03-26 18:36:28 -07002825{
Vivien Didelot04bed142016-08-31 18:06:13 -04002826 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot552238b2016-05-09 13:22:49 -04002827 int err;
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002828 int i;
2829
Vivien Didelotfad09c72016-06-21 12:28:20 -04002830 chip->ds = ds;
2831 ds->slave_mii_bus = chip->mdio_bus;
Vivien Didelot552238b2016-05-09 13:22:49 -04002832
Vivien Didelotfad09c72016-06-21 12:28:20 -04002833 mutex_lock(&chip->reg_lock);
Vivien Didelot552238b2016-05-09 13:22:49 -04002834
Vivien Didelot97299342016-07-18 20:45:30 -04002835 /* Setup Switch Port Registers */
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002836 for (i = 0; i < mv88e6xxx_num_ports(chip); i++) {
Vivien Didelot97299342016-07-18 20:45:30 -04002837 err = mv88e6xxx_setup_port(chip, i);
2838 if (err)
2839 goto unlock;
2840 }
2841
2842 /* Setup Switch Global 1 Registers */
2843 err = mv88e6xxx_g1_setup(chip);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002844 if (err)
2845 goto unlock;
2846
Vivien Didelot97299342016-07-18 20:45:30 -04002847 /* Setup Switch Global 2 Registers */
2848 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_GLOBAL2)) {
2849 err = mv88e6xxx_g2_setup(chip);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002850 if (err)
2851 goto unlock;
2852 }
Andrew Lunn54d792f2015-05-06 01:09:47 +02002853
Vivien Didelot6b17e862015-08-13 12:52:18 -04002854unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002855 mutex_unlock(&chip->reg_lock);
Andrew Lunndb687a52015-06-20 21:31:29 +02002856
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002857 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002858}
2859
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002860static int mv88e6xxx_set_addr(struct dsa_switch *ds, u8 *addr)
2861{
Vivien Didelot04bed142016-08-31 18:06:13 -04002862 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002863 int err;
2864
Vivien Didelotb073d4e2016-09-29 12:22:01 -04002865 if (!chip->info->ops->set_switch_mac)
2866 return -EOPNOTSUPP;
2867
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002868 mutex_lock(&chip->reg_lock);
Vivien Didelotb073d4e2016-09-29 12:22:01 -04002869 err = chip->info->ops->set_switch_mac(chip, addr);
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002870 mutex_unlock(&chip->reg_lock);
2871
2872 return err;
2873}
2874
Vivien Didelote57e5e72016-08-15 17:19:00 -04002875static int mv88e6xxx_mdio_read(struct mii_bus *bus, int phy, int reg)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002876{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002877 struct mv88e6xxx_chip *chip = bus->priv;
Vivien Didelote57e5e72016-08-15 17:19:00 -04002878 u16 val;
2879 int err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002880
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002881 if (phy >= mv88e6xxx_num_ports(chip))
Andrew Lunn158bc062016-04-28 21:24:06 -04002882 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002883
Vivien Didelotfad09c72016-06-21 12:28:20 -04002884 mutex_lock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04002885 err = mv88e6xxx_phy_read(chip, phy, reg, &val);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002886 mutex_unlock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04002887
2888 return err ? err : val;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002889}
2890
Vivien Didelote57e5e72016-08-15 17:19:00 -04002891static int mv88e6xxx_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002892{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002893 struct mv88e6xxx_chip *chip = bus->priv;
Vivien Didelote57e5e72016-08-15 17:19:00 -04002894 int err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002895
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002896 if (phy >= mv88e6xxx_num_ports(chip))
Andrew Lunn158bc062016-04-28 21:24:06 -04002897 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002898
Vivien Didelotfad09c72016-06-21 12:28:20 -04002899 mutex_lock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04002900 err = mv88e6xxx_phy_write(chip, phy, reg, val);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002901 mutex_unlock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04002902
2903 return err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002904}
2905
Vivien Didelotfad09c72016-06-21 12:28:20 -04002906static int mv88e6xxx_mdio_register(struct mv88e6xxx_chip *chip,
Andrew Lunnb516d452016-06-04 21:17:06 +02002907 struct device_node *np)
2908{
2909 static int index;
2910 struct mii_bus *bus;
2911 int err;
2912
Andrew Lunnb516d452016-06-04 21:17:06 +02002913 if (np)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002914 chip->mdio_np = of_get_child_by_name(np, "mdio");
Andrew Lunnb516d452016-06-04 21:17:06 +02002915
Vivien Didelotfad09c72016-06-21 12:28:20 -04002916 bus = devm_mdiobus_alloc(chip->dev);
Andrew Lunnb516d452016-06-04 21:17:06 +02002917 if (!bus)
2918 return -ENOMEM;
2919
Vivien Didelotfad09c72016-06-21 12:28:20 -04002920 bus->priv = (void *)chip;
Andrew Lunnb516d452016-06-04 21:17:06 +02002921 if (np) {
2922 bus->name = np->full_name;
2923 snprintf(bus->id, MII_BUS_ID_SIZE, "%s", np->full_name);
2924 } else {
2925 bus->name = "mv88e6xxx SMI";
2926 snprintf(bus->id, MII_BUS_ID_SIZE, "mv88e6xxx-%d", index++);
2927 }
2928
2929 bus->read = mv88e6xxx_mdio_read;
2930 bus->write = mv88e6xxx_mdio_write;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002931 bus->parent = chip->dev;
Andrew Lunnb516d452016-06-04 21:17:06 +02002932
Vivien Didelotfad09c72016-06-21 12:28:20 -04002933 if (chip->mdio_np)
2934 err = of_mdiobus_register(bus, chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02002935 else
2936 err = mdiobus_register(bus);
2937 if (err) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002938 dev_err(chip->dev, "Cannot register MDIO bus (%d)\n", err);
Andrew Lunnb516d452016-06-04 21:17:06 +02002939 goto out;
2940 }
Vivien Didelotfad09c72016-06-21 12:28:20 -04002941 chip->mdio_bus = bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02002942
2943 return 0;
2944
2945out:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002946 if (chip->mdio_np)
2947 of_node_put(chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02002948
2949 return err;
2950}
2951
Vivien Didelotfad09c72016-06-21 12:28:20 -04002952static void mv88e6xxx_mdio_unregister(struct mv88e6xxx_chip *chip)
Andrew Lunnb516d452016-06-04 21:17:06 +02002953
2954{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002955 struct mii_bus *bus = chip->mdio_bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02002956
2957 mdiobus_unregister(bus);
2958
Vivien Didelotfad09c72016-06-21 12:28:20 -04002959 if (chip->mdio_np)
2960 of_node_put(chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02002961}
2962
Guenter Roeckc22995c2015-07-25 09:42:28 -07002963#ifdef CONFIG_NET_DSA_HWMON
2964
2965static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
2966{
Vivien Didelot04bed142016-08-31 18:06:13 -04002967 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot9c938292016-08-15 17:19:02 -04002968 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07002969 int ret;
Guenter Roeckc22995c2015-07-25 09:42:28 -07002970
2971 *temp = 0;
2972
Vivien Didelotfad09c72016-06-21 12:28:20 -04002973 mutex_lock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07002974
Vivien Didelot9c938292016-08-15 17:19:02 -04002975 ret = mv88e6xxx_phy_write(chip, 0x0, 0x16, 0x6);
Guenter Roeckc22995c2015-07-25 09:42:28 -07002976 if (ret < 0)
2977 goto error;
2978
2979 /* Enable temperature sensor */
Vivien Didelot9c938292016-08-15 17:19:02 -04002980 ret = mv88e6xxx_phy_read(chip, 0x0, 0x1a, &val);
Guenter Roeckc22995c2015-07-25 09:42:28 -07002981 if (ret < 0)
2982 goto error;
2983
Vivien Didelot9c938292016-08-15 17:19:02 -04002984 ret = mv88e6xxx_phy_write(chip, 0x0, 0x1a, val | (1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07002985 if (ret < 0)
2986 goto error;
2987
2988 /* Wait for temperature to stabilize */
2989 usleep_range(10000, 12000);
2990
Vivien Didelot9c938292016-08-15 17:19:02 -04002991 ret = mv88e6xxx_phy_read(chip, 0x0, 0x1a, &val);
2992 if (ret < 0)
Guenter Roeckc22995c2015-07-25 09:42:28 -07002993 goto error;
Guenter Roeckc22995c2015-07-25 09:42:28 -07002994
2995 /* Disable temperature sensor */
Vivien Didelot9c938292016-08-15 17:19:02 -04002996 ret = mv88e6xxx_phy_write(chip, 0x0, 0x1a, val & ~(1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07002997 if (ret < 0)
2998 goto error;
2999
3000 *temp = ((val & 0x1f) - 5) * 5;
3001
3002error:
Vivien Didelot9c938292016-08-15 17:19:02 -04003003 mv88e6xxx_phy_write(chip, 0x0, 0x16, 0x0);
Vivien Didelotfad09c72016-06-21 12:28:20 -04003004 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003005 return ret;
3006}
3007
3008static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
3009{
Vivien Didelot04bed142016-08-31 18:06:13 -04003010 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003011 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003012 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003013 int ret;
3014
3015 *temp = 0;
3016
Vivien Didelot9c938292016-08-15 17:19:02 -04003017 mutex_lock(&chip->reg_lock);
3018 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 27, &val);
3019 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003020 if (ret < 0)
3021 return ret;
3022
Vivien Didelot9c938292016-08-15 17:19:02 -04003023 *temp = (val & 0xff) - 25;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003024
3025 return 0;
3026}
3027
Vivien Didelotf81ec902016-05-09 13:22:58 -04003028static int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003029{
Vivien Didelot04bed142016-08-31 18:06:13 -04003030 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn158bc062016-04-28 21:24:06 -04003031
Vivien Didelotfad09c72016-06-21 12:28:20 -04003032 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP))
Vivien Didelot6594f612016-05-09 13:22:42 -04003033 return -EOPNOTSUPP;
3034
Vivien Didelotfad09c72016-06-21 12:28:20 -04003035 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003036 return mv88e63xx_get_temp(ds, temp);
3037
3038 return mv88e61xx_get_temp(ds, temp);
3039}
3040
Vivien Didelotf81ec902016-05-09 13:22:58 -04003041static int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003042{
Vivien Didelot04bed142016-08-31 18:06:13 -04003043 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003044 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003045 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003046 int ret;
3047
Vivien Didelotfad09c72016-06-21 12:28:20 -04003048 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003049 return -EOPNOTSUPP;
3050
3051 *temp = 0;
3052
Vivien Didelot9c938292016-08-15 17:19:02 -04003053 mutex_lock(&chip->reg_lock);
3054 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3055 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003056 if (ret < 0)
3057 return ret;
3058
Vivien Didelot9c938292016-08-15 17:19:02 -04003059 *temp = (((val >> 8) & 0x1f) * 5) - 25;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003060
3061 return 0;
3062}
3063
Vivien Didelotf81ec902016-05-09 13:22:58 -04003064static int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003065{
Vivien Didelot04bed142016-08-31 18:06:13 -04003066 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003067 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003068 u16 val;
3069 int err;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003070
Vivien Didelotfad09c72016-06-21 12:28:20 -04003071 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003072 return -EOPNOTSUPP;
3073
Vivien Didelot9c938292016-08-15 17:19:02 -04003074 mutex_lock(&chip->reg_lock);
3075 err = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3076 if (err)
3077 goto unlock;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003078 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
Vivien Didelot9c938292016-08-15 17:19:02 -04003079 err = mv88e6xxx_phy_page_write(chip, phy, 6, 26,
3080 (val & 0xe0ff) | (temp << 8));
3081unlock:
3082 mutex_unlock(&chip->reg_lock);
3083
3084 return err;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003085}
3086
Vivien Didelotf81ec902016-05-09 13:22:58 -04003087static int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003088{
Vivien Didelot04bed142016-08-31 18:06:13 -04003089 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003090 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003091 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003092 int ret;
3093
Vivien Didelotfad09c72016-06-21 12:28:20 -04003094 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003095 return -EOPNOTSUPP;
3096
3097 *alarm = false;
3098
Vivien Didelot9c938292016-08-15 17:19:02 -04003099 mutex_lock(&chip->reg_lock);
3100 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3101 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003102 if (ret < 0)
3103 return ret;
3104
Vivien Didelot9c938292016-08-15 17:19:02 -04003105 *alarm = !!(val & 0x40);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003106
3107 return 0;
3108}
3109#endif /* CONFIG_NET_DSA_HWMON */
3110
Vivien Didelot855b1932016-07-20 18:18:35 -04003111static int mv88e6xxx_get_eeprom_len(struct dsa_switch *ds)
3112{
Vivien Didelot04bed142016-08-31 18:06:13 -04003113 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot855b1932016-07-20 18:18:35 -04003114
3115 return chip->eeprom_len;
3116}
3117
Vivien Didelot855b1932016-07-20 18:18:35 -04003118static int mv88e6xxx_get_eeprom(struct dsa_switch *ds,
3119 struct ethtool_eeprom *eeprom, u8 *data)
3120{
Vivien Didelot04bed142016-08-31 18:06:13 -04003121 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot855b1932016-07-20 18:18:35 -04003122 int err;
3123
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003124 if (!chip->info->ops->get_eeprom)
3125 return -EOPNOTSUPP;
3126
Vivien Didelot855b1932016-07-20 18:18:35 -04003127 mutex_lock(&chip->reg_lock);
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003128 err = chip->info->ops->get_eeprom(chip, eeprom, data);
Vivien Didelot855b1932016-07-20 18:18:35 -04003129 mutex_unlock(&chip->reg_lock);
3130
3131 if (err)
3132 return err;
3133
3134 eeprom->magic = 0xc3ec4951;
3135
3136 return 0;
3137}
3138
Vivien Didelot855b1932016-07-20 18:18:35 -04003139static int mv88e6xxx_set_eeprom(struct dsa_switch *ds,
3140 struct ethtool_eeprom *eeprom, u8 *data)
3141{
Vivien Didelot04bed142016-08-31 18:06:13 -04003142 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot855b1932016-07-20 18:18:35 -04003143 int err;
3144
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003145 if (!chip->info->ops->set_eeprom)
3146 return -EOPNOTSUPP;
3147
Vivien Didelot855b1932016-07-20 18:18:35 -04003148 if (eeprom->magic != 0xc3ec4951)
3149 return -EINVAL;
3150
3151 mutex_lock(&chip->reg_lock);
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003152 err = chip->info->ops->set_eeprom(chip, eeprom, data);
Vivien Didelot855b1932016-07-20 18:18:35 -04003153 mutex_unlock(&chip->reg_lock);
3154
3155 return err;
3156}
3157
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003158static const struct mv88e6xxx_ops mv88e6085_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003159 .set_switch_mac = mv88e6xxx_g1_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003160 .phy_read = mv88e6xxx_phy_ppu_read,
3161 .phy_write = mv88e6xxx_phy_ppu_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003162 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003163 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003164 .port_set_speed = mv88e6185_port_set_speed,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003165};
3166
3167static const struct mv88e6xxx_ops mv88e6095_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003168 .set_switch_mac = mv88e6xxx_g1_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003169 .phy_read = mv88e6xxx_phy_ppu_read,
3170 .phy_write = mv88e6xxx_phy_ppu_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003171 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003172 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003173 .port_set_speed = mv88e6185_port_set_speed,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003174};
3175
3176static const struct mv88e6xxx_ops mv88e6123_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003177 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003178 .phy_read = mv88e6xxx_read,
3179 .phy_write = mv88e6xxx_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003180 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003181 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003182 .port_set_speed = mv88e6185_port_set_speed,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003183};
3184
3185static const struct mv88e6xxx_ops mv88e6131_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003186 .set_switch_mac = mv88e6xxx_g1_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003187 .phy_read = mv88e6xxx_phy_ppu_read,
3188 .phy_write = mv88e6xxx_phy_ppu_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003189 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003190 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003191 .port_set_speed = mv88e6185_port_set_speed,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003192};
3193
3194static const struct mv88e6xxx_ops mv88e6161_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003195 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003196 .phy_read = mv88e6xxx_read,
3197 .phy_write = mv88e6xxx_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003198 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003199 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003200 .port_set_speed = mv88e6185_port_set_speed,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003201};
3202
3203static const struct mv88e6xxx_ops mv88e6165_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003204 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003205 .phy_read = mv88e6xxx_read,
3206 .phy_write = mv88e6xxx_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003207 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003208 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003209 .port_set_speed = mv88e6185_port_set_speed,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003210};
3211
3212static const struct mv88e6xxx_ops mv88e6171_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003213 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003214 .phy_read = mv88e6xxx_g2_smi_phy_read,
3215 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003216 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003217 .port_set_duplex = mv88e6xxx_port_set_duplex,
Andrew Lunn94d66ae2016-11-10 15:44:01 +01003218 .port_set_rgmii_delay = mv88e6352_port_set_rgmii_delay,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003219 .port_set_speed = mv88e6185_port_set_speed,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003220};
3221
3222static const struct mv88e6xxx_ops mv88e6172_ops = {
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003223 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3224 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003225 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003226 .phy_read = mv88e6xxx_g2_smi_phy_read,
3227 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003228 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003229 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelota0a0f622016-11-04 03:23:34 +01003230 .port_set_rgmii_delay = mv88e6352_port_set_rgmii_delay,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003231 .port_set_speed = mv88e6352_port_set_speed,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003232};
3233
3234static const struct mv88e6xxx_ops mv88e6175_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003235 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003236 .phy_read = mv88e6xxx_g2_smi_phy_read,
3237 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003238 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003239 .port_set_duplex = mv88e6xxx_port_set_duplex,
Andrew Lunn94d66ae2016-11-10 15:44:01 +01003240 .port_set_rgmii_delay = mv88e6352_port_set_rgmii_delay,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003241 .port_set_speed = mv88e6185_port_set_speed,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003242};
3243
3244static const struct mv88e6xxx_ops mv88e6176_ops = {
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003245 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3246 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003247 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003248 .phy_read = mv88e6xxx_g2_smi_phy_read,
3249 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003250 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003251 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelota0a0f622016-11-04 03:23:34 +01003252 .port_set_rgmii_delay = mv88e6352_port_set_rgmii_delay,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003253 .port_set_speed = mv88e6352_port_set_speed,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003254};
3255
3256static const struct mv88e6xxx_ops mv88e6185_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003257 .set_switch_mac = mv88e6xxx_g1_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003258 .phy_read = mv88e6xxx_phy_ppu_read,
3259 .phy_write = mv88e6xxx_phy_ppu_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003260 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003261 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003262 .port_set_speed = mv88e6185_port_set_speed,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003263};
3264
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003265static const struct mv88e6xxx_ops mv88e6190_ops = {
3266 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
3267 .phy_read = mv88e6xxx_g2_smi_phy_read,
3268 .phy_write = mv88e6xxx_g2_smi_phy_write,
3269 .port_set_link = mv88e6xxx_port_set_link,
3270 .port_set_duplex = mv88e6xxx_port_set_duplex,
3271 .port_set_rgmii_delay = mv88e6390_port_set_rgmii_delay,
3272 .port_set_speed = mv88e6390_port_set_speed,
3273};
3274
3275static const struct mv88e6xxx_ops mv88e6190x_ops = {
3276 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
3277 .phy_read = mv88e6xxx_g2_smi_phy_read,
3278 .phy_write = mv88e6xxx_g2_smi_phy_write,
3279 .port_set_link = mv88e6xxx_port_set_link,
3280 .port_set_duplex = mv88e6xxx_port_set_duplex,
3281 .port_set_rgmii_delay = mv88e6390_port_set_rgmii_delay,
3282 .port_set_speed = mv88e6390x_port_set_speed,
3283};
3284
3285static const struct mv88e6xxx_ops mv88e6191_ops = {
3286 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
3287 .phy_read = mv88e6xxx_g2_smi_phy_read,
3288 .phy_write = mv88e6xxx_g2_smi_phy_write,
3289 .port_set_link = mv88e6xxx_port_set_link,
3290 .port_set_duplex = mv88e6xxx_port_set_duplex,
3291 .port_set_rgmii_delay = mv88e6390_port_set_rgmii_delay,
3292 .port_set_speed = mv88e6390_port_set_speed,
3293};
3294
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003295static const struct mv88e6xxx_ops mv88e6240_ops = {
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003296 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3297 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003298 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003299 .phy_read = mv88e6xxx_g2_smi_phy_read,
3300 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003301 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003302 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelota0a0f622016-11-04 03:23:34 +01003303 .port_set_rgmii_delay = mv88e6352_port_set_rgmii_delay,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003304 .port_set_speed = mv88e6352_port_set_speed,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003305};
3306
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003307static const struct mv88e6xxx_ops mv88e6290_ops = {
3308 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
3309 .phy_read = mv88e6xxx_g2_smi_phy_read,
3310 .phy_write = mv88e6xxx_g2_smi_phy_write,
3311 .port_set_link = mv88e6xxx_port_set_link,
3312 .port_set_duplex = mv88e6xxx_port_set_duplex,
3313 .port_set_rgmii_delay = mv88e6390_port_set_rgmii_delay,
3314 .port_set_speed = mv88e6390_port_set_speed,
3315};
3316
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003317static const struct mv88e6xxx_ops mv88e6320_ops = {
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003318 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3319 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003320 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003321 .phy_read = mv88e6xxx_g2_smi_phy_read,
3322 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003323 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003324 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003325 .port_set_speed = mv88e6185_port_set_speed,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003326};
3327
3328static const struct mv88e6xxx_ops mv88e6321_ops = {
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003329 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3330 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003331 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003332 .phy_read = mv88e6xxx_g2_smi_phy_read,
3333 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003334 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003335 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003336 .port_set_speed = mv88e6185_port_set_speed,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003337};
3338
3339static const struct mv88e6xxx_ops mv88e6350_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003340 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003341 .phy_read = mv88e6xxx_g2_smi_phy_read,
3342 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003343 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003344 .port_set_duplex = mv88e6xxx_port_set_duplex,
Andrew Lunn94d66ae2016-11-10 15:44:01 +01003345 .port_set_rgmii_delay = mv88e6352_port_set_rgmii_delay,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003346 .port_set_speed = mv88e6185_port_set_speed,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003347};
3348
3349static const struct mv88e6xxx_ops mv88e6351_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003350 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003351 .phy_read = mv88e6xxx_g2_smi_phy_read,
3352 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003353 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003354 .port_set_duplex = mv88e6xxx_port_set_duplex,
Andrew Lunn94d66ae2016-11-10 15:44:01 +01003355 .port_set_rgmii_delay = mv88e6352_port_set_rgmii_delay,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003356 .port_set_speed = mv88e6185_port_set_speed,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003357};
3358
3359static const struct mv88e6xxx_ops mv88e6352_ops = {
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003360 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3361 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003362 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003363 .phy_read = mv88e6xxx_g2_smi_phy_read,
3364 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003365 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelot7f1ae072016-11-04 03:23:33 +01003366 .port_set_duplex = mv88e6xxx_port_set_duplex,
Vivien Didelota0a0f622016-11-04 03:23:34 +01003367 .port_set_rgmii_delay = mv88e6352_port_set_rgmii_delay,
Vivien Didelot96a2b402016-11-04 03:23:35 +01003368 .port_set_speed = mv88e6352_port_set_speed,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003369};
3370
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003371static const struct mv88e6xxx_ops mv88e6390_ops = {
3372 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
3373 .phy_read = mv88e6xxx_g2_smi_phy_read,
3374 .phy_write = mv88e6xxx_g2_smi_phy_write,
3375 .port_set_link = mv88e6xxx_port_set_link,
3376 .port_set_duplex = mv88e6xxx_port_set_duplex,
3377 .port_set_rgmii_delay = mv88e6390_port_set_rgmii_delay,
3378 .port_set_speed = mv88e6390_port_set_speed,
3379};
3380
3381static const struct mv88e6xxx_ops mv88e6390x_ops = {
3382 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
3383 .phy_read = mv88e6xxx_g2_smi_phy_read,
3384 .phy_write = mv88e6xxx_g2_smi_phy_write,
3385 .port_set_link = mv88e6xxx_port_set_link,
3386 .port_set_duplex = mv88e6xxx_port_set_duplex,
3387 .port_set_rgmii_delay = mv88e6390_port_set_rgmii_delay,
3388 .port_set_speed = mv88e6390x_port_set_speed,
3389};
3390
3391static const struct mv88e6xxx_ops mv88e6391_ops = {
3392 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
3393 .phy_read = mv88e6xxx_g2_smi_phy_read,
3394 .phy_write = mv88e6xxx_g2_smi_phy_write,
3395 .port_set_link = mv88e6xxx_port_set_link,
3396 .port_set_duplex = mv88e6xxx_port_set_duplex,
3397 .port_set_rgmii_delay = mv88e6390_port_set_rgmii_delay,
3398 .port_set_speed = mv88e6390_port_set_speed,
3399};
3400
Vivien Didelotf81ec902016-05-09 13:22:58 -04003401static const struct mv88e6xxx_info mv88e6xxx_table[] = {
3402 [MV88E6085] = {
3403 .prod_num = PORT_SWITCH_ID_PROD_NUM_6085,
3404 .family = MV88E6XXX_FAMILY_6097,
3405 .name = "Marvell 88E6085",
3406 .num_databases = 4096,
3407 .num_ports = 10,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003408 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003409 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003410 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003411 .g1_irqs = 8,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003412 .flags = MV88E6XXX_FLAGS_FAMILY_6097,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003413 .ops = &mv88e6085_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003414 },
3415
3416 [MV88E6095] = {
3417 .prod_num = PORT_SWITCH_ID_PROD_NUM_6095,
3418 .family = MV88E6XXX_FAMILY_6095,
3419 .name = "Marvell 88E6095/88E6095F",
3420 .num_databases = 256,
3421 .num_ports = 11,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003422 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003423 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003424 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003425 .g1_irqs = 8,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003426 .flags = MV88E6XXX_FLAGS_FAMILY_6095,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003427 .ops = &mv88e6095_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003428 },
3429
3430 [MV88E6123] = {
3431 .prod_num = PORT_SWITCH_ID_PROD_NUM_6123,
3432 .family = MV88E6XXX_FAMILY_6165,
3433 .name = "Marvell 88E6123",
3434 .num_databases = 4096,
3435 .num_ports = 3,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003436 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003437 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003438 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003439 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003440 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003441 .ops = &mv88e6123_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003442 },
3443
3444 [MV88E6131] = {
3445 .prod_num = PORT_SWITCH_ID_PROD_NUM_6131,
3446 .family = MV88E6XXX_FAMILY_6185,
3447 .name = "Marvell 88E6131",
3448 .num_databases = 256,
3449 .num_ports = 8,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003450 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003451 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003452 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003453 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003454 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003455 .ops = &mv88e6131_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003456 },
3457
3458 [MV88E6161] = {
3459 .prod_num = PORT_SWITCH_ID_PROD_NUM_6161,
3460 .family = MV88E6XXX_FAMILY_6165,
3461 .name = "Marvell 88E6161",
3462 .num_databases = 4096,
3463 .num_ports = 6,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003464 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003465 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003466 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003467 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003468 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003469 .ops = &mv88e6161_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003470 },
3471
3472 [MV88E6165] = {
3473 .prod_num = PORT_SWITCH_ID_PROD_NUM_6165,
3474 .family = MV88E6XXX_FAMILY_6165,
3475 .name = "Marvell 88E6165",
3476 .num_databases = 4096,
3477 .num_ports = 6,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003478 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003479 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003480 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003481 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003482 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003483 .ops = &mv88e6165_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003484 },
3485
3486 [MV88E6171] = {
3487 .prod_num = PORT_SWITCH_ID_PROD_NUM_6171,
3488 .family = MV88E6XXX_FAMILY_6351,
3489 .name = "Marvell 88E6171",
3490 .num_databases = 4096,
3491 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003492 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003493 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003494 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003495 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003496 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003497 .ops = &mv88e6171_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003498 },
3499
3500 [MV88E6172] = {
3501 .prod_num = PORT_SWITCH_ID_PROD_NUM_6172,
3502 .family = MV88E6XXX_FAMILY_6352,
3503 .name = "Marvell 88E6172",
3504 .num_databases = 4096,
3505 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003506 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003507 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003508 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003509 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003510 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003511 .ops = &mv88e6172_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003512 },
3513
3514 [MV88E6175] = {
3515 .prod_num = PORT_SWITCH_ID_PROD_NUM_6175,
3516 .family = MV88E6XXX_FAMILY_6351,
3517 .name = "Marvell 88E6175",
3518 .num_databases = 4096,
3519 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003520 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003521 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003522 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003523 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003524 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003525 .ops = &mv88e6175_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003526 },
3527
3528 [MV88E6176] = {
3529 .prod_num = PORT_SWITCH_ID_PROD_NUM_6176,
3530 .family = MV88E6XXX_FAMILY_6352,
3531 .name = "Marvell 88E6176",
3532 .num_databases = 4096,
3533 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003534 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003535 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003536 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003537 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003538 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003539 .ops = &mv88e6176_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003540 },
3541
3542 [MV88E6185] = {
3543 .prod_num = PORT_SWITCH_ID_PROD_NUM_6185,
3544 .family = MV88E6XXX_FAMILY_6185,
3545 .name = "Marvell 88E6185",
3546 .num_databases = 256,
3547 .num_ports = 10,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003548 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003549 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003550 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003551 .g1_irqs = 8,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003552 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003553 .ops = &mv88e6185_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003554 },
3555
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003556 [MV88E6190] = {
3557 .prod_num = PORT_SWITCH_ID_PROD_NUM_6190,
3558 .family = MV88E6XXX_FAMILY_6390,
3559 .name = "Marvell 88E6190",
3560 .num_databases = 4096,
3561 .num_ports = 11, /* 10 + Z80 */
3562 .port_base_addr = 0x0,
3563 .global1_addr = 0x1b,
3564 .age_time_coeff = 15000,
3565 .g1_irqs = 9,
3566 .flags = MV88E6XXX_FLAGS_FAMILY_6390,
3567 .ops = &mv88e6190_ops,
3568 },
3569
3570 [MV88E6190X] = {
3571 .prod_num = PORT_SWITCH_ID_PROD_NUM_6190X,
3572 .family = MV88E6XXX_FAMILY_6390,
3573 .name = "Marvell 88E6190X",
3574 .num_databases = 4096,
3575 .num_ports = 11, /* 10 + Z80 */
3576 .port_base_addr = 0x0,
3577 .global1_addr = 0x1b,
3578 .age_time_coeff = 15000,
3579 .g1_irqs = 9,
3580 .flags = MV88E6XXX_FLAGS_FAMILY_6390,
3581 .ops = &mv88e6190x_ops,
3582 },
3583
3584 [MV88E6191] = {
3585 .prod_num = PORT_SWITCH_ID_PROD_NUM_6191,
3586 .family = MV88E6XXX_FAMILY_6390,
3587 .name = "Marvell 88E6191",
3588 .num_databases = 4096,
3589 .num_ports = 11, /* 10 + Z80 */
3590 .port_base_addr = 0x0,
3591 .global1_addr = 0x1b,
3592 .age_time_coeff = 15000,
3593 .flags = MV88E6XXX_FLAGS_FAMILY_6390,
3594 .ops = &mv88e6391_ops,
3595 },
3596
Vivien Didelotf81ec902016-05-09 13:22:58 -04003597 [MV88E6240] = {
3598 .prod_num = PORT_SWITCH_ID_PROD_NUM_6240,
3599 .family = MV88E6XXX_FAMILY_6352,
3600 .name = "Marvell 88E6240",
3601 .num_databases = 4096,
3602 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003603 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003604 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003605 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003606 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003607 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003608 .ops = &mv88e6240_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003609 },
3610
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003611 [MV88E6290] = {
3612 .prod_num = PORT_SWITCH_ID_PROD_NUM_6290,
3613 .family = MV88E6XXX_FAMILY_6390,
3614 .name = "Marvell 88E6290",
3615 .num_databases = 4096,
3616 .num_ports = 11, /* 10 + Z80 */
3617 .port_base_addr = 0x0,
3618 .global1_addr = 0x1b,
3619 .age_time_coeff = 15000,
3620 .g1_irqs = 9,
3621 .flags = MV88E6XXX_FLAGS_FAMILY_6390,
3622 .ops = &mv88e6290_ops,
3623 },
3624
Vivien Didelotf81ec902016-05-09 13:22:58 -04003625 [MV88E6320] = {
3626 .prod_num = PORT_SWITCH_ID_PROD_NUM_6320,
3627 .family = MV88E6XXX_FAMILY_6320,
3628 .name = "Marvell 88E6320",
3629 .num_databases = 4096,
3630 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003631 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003632 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003633 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003634 .g1_irqs = 8,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003635 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003636 .ops = &mv88e6320_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003637 },
3638
3639 [MV88E6321] = {
3640 .prod_num = PORT_SWITCH_ID_PROD_NUM_6321,
3641 .family = MV88E6XXX_FAMILY_6320,
3642 .name = "Marvell 88E6321",
3643 .num_databases = 4096,
3644 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003645 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003646 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003647 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003648 .g1_irqs = 8,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003649 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003650 .ops = &mv88e6321_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003651 },
3652
3653 [MV88E6350] = {
3654 .prod_num = PORT_SWITCH_ID_PROD_NUM_6350,
3655 .family = MV88E6XXX_FAMILY_6351,
3656 .name = "Marvell 88E6350",
3657 .num_databases = 4096,
3658 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003659 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003660 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003661 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003662 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003663 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003664 .ops = &mv88e6350_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003665 },
3666
3667 [MV88E6351] = {
3668 .prod_num = PORT_SWITCH_ID_PROD_NUM_6351,
3669 .family = MV88E6XXX_FAMILY_6351,
3670 .name = "Marvell 88E6351",
3671 .num_databases = 4096,
3672 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003673 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003674 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003675 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003676 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003677 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003678 .ops = &mv88e6351_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003679 },
3680
3681 [MV88E6352] = {
3682 .prod_num = PORT_SWITCH_ID_PROD_NUM_6352,
3683 .family = MV88E6XXX_FAMILY_6352,
3684 .name = "Marvell 88E6352",
3685 .num_databases = 4096,
3686 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003687 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003688 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003689 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003690 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003691 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003692 .ops = &mv88e6352_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003693 },
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01003694 [MV88E6390] = {
3695 .prod_num = PORT_SWITCH_ID_PROD_NUM_6390,
3696 .family = MV88E6XXX_FAMILY_6390,
3697 .name = "Marvell 88E6390",
3698 .num_databases = 4096,
3699 .num_ports = 11, /* 10 + Z80 */
3700 .port_base_addr = 0x0,
3701 .global1_addr = 0x1b,
3702 .age_time_coeff = 15000,
3703 .g1_irqs = 9,
3704 .flags = MV88E6XXX_FLAGS_FAMILY_6390,
3705 .ops = &mv88e6390_ops,
3706 },
3707 [MV88E6390X] = {
3708 .prod_num = PORT_SWITCH_ID_PROD_NUM_6390X,
3709 .family = MV88E6XXX_FAMILY_6390,
3710 .name = "Marvell 88E6390X",
3711 .num_databases = 4096,
3712 .num_ports = 11, /* 10 + Z80 */
3713 .port_base_addr = 0x0,
3714 .global1_addr = 0x1b,
3715 .age_time_coeff = 15000,
3716 .g1_irqs = 9,
3717 .flags = MV88E6XXX_FLAGS_FAMILY_6390,
3718 .ops = &mv88e6390x_ops,
3719 },
Vivien Didelotf81ec902016-05-09 13:22:58 -04003720};
3721
Vivien Didelot5f7c0362016-06-20 13:14:04 -04003722static const struct mv88e6xxx_info *mv88e6xxx_lookup_info(unsigned int prod_num)
Vivien Didelotb9b37712015-10-30 19:39:48 -04003723{
Vivien Didelota439c062016-04-17 13:23:58 -04003724 int i;
Vivien Didelotb9b37712015-10-30 19:39:48 -04003725
Vivien Didelot5f7c0362016-06-20 13:14:04 -04003726 for (i = 0; i < ARRAY_SIZE(mv88e6xxx_table); ++i)
3727 if (mv88e6xxx_table[i].prod_num == prod_num)
3728 return &mv88e6xxx_table[i];
Vivien Didelotb9b37712015-10-30 19:39:48 -04003729
Vivien Didelotb9b37712015-10-30 19:39:48 -04003730 return NULL;
3731}
3732
Vivien Didelotfad09c72016-06-21 12:28:20 -04003733static int mv88e6xxx_detect(struct mv88e6xxx_chip *chip)
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003734{
3735 const struct mv88e6xxx_info *info;
Vivien Didelot8f6345b2016-07-20 18:18:36 -04003736 unsigned int prod_num, rev;
3737 u16 id;
3738 int err;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003739
Vivien Didelot8f6345b2016-07-20 18:18:36 -04003740 mutex_lock(&chip->reg_lock);
3741 err = mv88e6xxx_port_read(chip, 0, PORT_SWITCH_ID, &id);
3742 mutex_unlock(&chip->reg_lock);
3743 if (err)
3744 return err;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003745
3746 prod_num = (id & 0xfff0) >> 4;
3747 rev = id & 0x000f;
3748
3749 info = mv88e6xxx_lookup_info(prod_num);
3750 if (!info)
3751 return -ENODEV;
3752
Vivien Didelotcaac8542016-06-20 13:14:09 -04003753 /* Update the compatible info with the probed one */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003754 chip->info = info;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003755
Vivien Didelotca070c12016-09-02 14:45:34 -04003756 err = mv88e6xxx_g2_require(chip);
3757 if (err)
3758 return err;
3759
Vivien Didelotfad09c72016-06-21 12:28:20 -04003760 dev_info(chip->dev, "switch 0x%x detected: %s, revision %u\n",
3761 chip->info->prod_num, chip->info->name, rev);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003762
3763 return 0;
3764}
3765
Vivien Didelotfad09c72016-06-21 12:28:20 -04003766static struct mv88e6xxx_chip *mv88e6xxx_alloc_chip(struct device *dev)
Vivien Didelot469d7292016-06-20 13:14:06 -04003767{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003768 struct mv88e6xxx_chip *chip;
Vivien Didelot469d7292016-06-20 13:14:06 -04003769
Vivien Didelotfad09c72016-06-21 12:28:20 -04003770 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
3771 if (!chip)
Vivien Didelot469d7292016-06-20 13:14:06 -04003772 return NULL;
3773
Vivien Didelotfad09c72016-06-21 12:28:20 -04003774 chip->dev = dev;
Vivien Didelot469d7292016-06-20 13:14:06 -04003775
Vivien Didelotfad09c72016-06-21 12:28:20 -04003776 mutex_init(&chip->reg_lock);
Vivien Didelot469d7292016-06-20 13:14:06 -04003777
Vivien Didelotfad09c72016-06-21 12:28:20 -04003778 return chip;
Vivien Didelot469d7292016-06-20 13:14:06 -04003779}
3780
Vivien Didelote57e5e72016-08-15 17:19:00 -04003781static void mv88e6xxx_phy_init(struct mv88e6xxx_chip *chip)
3782{
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003783 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU))
Vivien Didelote57e5e72016-08-15 17:19:00 -04003784 mv88e6xxx_ppu_state_init(chip);
Vivien Didelote57e5e72016-08-15 17:19:00 -04003785}
3786
Andrew Lunn930188c2016-08-22 16:01:03 +02003787static void mv88e6xxx_phy_destroy(struct mv88e6xxx_chip *chip)
3788{
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003789 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU))
Andrew Lunn930188c2016-08-22 16:01:03 +02003790 mv88e6xxx_ppu_state_destroy(chip);
Andrew Lunn930188c2016-08-22 16:01:03 +02003791}
3792
Vivien Didelotfad09c72016-06-21 12:28:20 -04003793static int mv88e6xxx_smi_init(struct mv88e6xxx_chip *chip,
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003794 struct mii_bus *bus, int sw_addr)
3795{
3796 /* ADDR[0] pin is unavailable externally and considered zero */
3797 if (sw_addr & 0x1)
3798 return -EINVAL;
3799
Vivien Didelot914b32f2016-06-20 13:14:11 -04003800 if (sw_addr == 0)
Vivien Didelotfad09c72016-06-21 12:28:20 -04003801 chip->smi_ops = &mv88e6xxx_smi_single_chip_ops;
Vivien Didelota0ffff22016-08-15 17:18:58 -04003802 else if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_MULTI_CHIP))
Vivien Didelotfad09c72016-06-21 12:28:20 -04003803 chip->smi_ops = &mv88e6xxx_smi_multi_chip_ops;
Vivien Didelot914b32f2016-06-20 13:14:11 -04003804 else
3805 return -EINVAL;
3806
Vivien Didelotfad09c72016-06-21 12:28:20 -04003807 chip->bus = bus;
3808 chip->sw_addr = sw_addr;
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003809
3810 return 0;
3811}
3812
Andrew Lunn7b314362016-08-22 16:01:01 +02003813static enum dsa_tag_protocol mv88e6xxx_get_tag_protocol(struct dsa_switch *ds)
3814{
Vivien Didelot04bed142016-08-31 18:06:13 -04003815 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02003816
3817 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA))
3818 return DSA_TAG_PROTO_EDSA;
3819
3820 return DSA_TAG_PROTO_DSA;
Andrew Lunn7b314362016-08-22 16:01:01 +02003821}
3822
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02003823static const char *mv88e6xxx_drv_probe(struct device *dsa_dev,
3824 struct device *host_dev, int sw_addr,
3825 void **priv)
Andrew Lunna77d43f2016-04-13 02:40:42 +02003826{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003827 struct mv88e6xxx_chip *chip;
Vivien Didelota439c062016-04-17 13:23:58 -04003828 struct mii_bus *bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003829 int err;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003830
Vivien Didelota439c062016-04-17 13:23:58 -04003831 bus = dsa_host_dev_to_mii_bus(host_dev);
Andrew Lunnc1569132016-04-13 02:40:45 +02003832 if (!bus)
3833 return NULL;
3834
Vivien Didelotfad09c72016-06-21 12:28:20 -04003835 chip = mv88e6xxx_alloc_chip(dsa_dev);
3836 if (!chip)
Vivien Didelot469d7292016-06-20 13:14:06 -04003837 return NULL;
3838
Vivien Didelotcaac8542016-06-20 13:14:09 -04003839 /* Legacy SMI probing will only support chips similar to 88E6085 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003840 chip->info = &mv88e6xxx_table[MV88E6085];
Vivien Didelotcaac8542016-06-20 13:14:09 -04003841
Vivien Didelotfad09c72016-06-21 12:28:20 -04003842 err = mv88e6xxx_smi_init(chip, bus, sw_addr);
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003843 if (err)
3844 goto free;
3845
Vivien Didelotfad09c72016-06-21 12:28:20 -04003846 err = mv88e6xxx_detect(chip);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003847 if (err)
Vivien Didelot469d7292016-06-20 13:14:06 -04003848 goto free;
Vivien Didelota439c062016-04-17 13:23:58 -04003849
Andrew Lunndc30c352016-10-16 19:56:49 +02003850 mutex_lock(&chip->reg_lock);
3851 err = mv88e6xxx_switch_reset(chip);
3852 mutex_unlock(&chip->reg_lock);
3853 if (err)
3854 goto free;
3855
Vivien Didelote57e5e72016-08-15 17:19:00 -04003856 mv88e6xxx_phy_init(chip);
3857
Vivien Didelotfad09c72016-06-21 12:28:20 -04003858 err = mv88e6xxx_mdio_register(chip, NULL);
Andrew Lunnb516d452016-06-04 21:17:06 +02003859 if (err)
Vivien Didelot469d7292016-06-20 13:14:06 -04003860 goto free;
Andrew Lunnb516d452016-06-04 21:17:06 +02003861
Vivien Didelotfad09c72016-06-21 12:28:20 -04003862 *priv = chip;
Vivien Didelota439c062016-04-17 13:23:58 -04003863
Vivien Didelotfad09c72016-06-21 12:28:20 -04003864 return chip->info->name;
Vivien Didelot469d7292016-06-20 13:14:06 -04003865free:
Vivien Didelotfad09c72016-06-21 12:28:20 -04003866 devm_kfree(dsa_dev, chip);
Vivien Didelot469d7292016-06-20 13:14:06 -04003867
3868 return NULL;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003869}
3870
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003871static int mv88e6xxx_port_mdb_prepare(struct dsa_switch *ds, int port,
3872 const struct switchdev_obj_port_mdb *mdb,
3873 struct switchdev_trans *trans)
3874{
3875 /* We don't need any dynamic resource from the kernel (yet),
3876 * so skip the prepare phase.
3877 */
3878
3879 return 0;
3880}
3881
3882static void mv88e6xxx_port_mdb_add(struct dsa_switch *ds, int port,
3883 const struct switchdev_obj_port_mdb *mdb,
3884 struct switchdev_trans *trans)
3885{
Vivien Didelot04bed142016-08-31 18:06:13 -04003886 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003887
3888 mutex_lock(&chip->reg_lock);
3889 if (mv88e6xxx_port_db_load_purge(chip, port, mdb->addr, mdb->vid,
3890 GLOBAL_ATU_DATA_STATE_MC_STATIC))
3891 netdev_err(ds->ports[port].netdev, "failed to load multicast MAC address\n");
3892 mutex_unlock(&chip->reg_lock);
3893}
3894
3895static int mv88e6xxx_port_mdb_del(struct dsa_switch *ds, int port,
3896 const struct switchdev_obj_port_mdb *mdb)
3897{
Vivien Didelot04bed142016-08-31 18:06:13 -04003898 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003899 int err;
3900
3901 mutex_lock(&chip->reg_lock);
3902 err = mv88e6xxx_port_db_load_purge(chip, port, mdb->addr, mdb->vid,
3903 GLOBAL_ATU_DATA_STATE_UNUSED);
3904 mutex_unlock(&chip->reg_lock);
3905
3906 return err;
3907}
3908
3909static int mv88e6xxx_port_mdb_dump(struct dsa_switch *ds, int port,
3910 struct switchdev_obj_port_mdb *mdb,
3911 int (*cb)(struct switchdev_obj *obj))
3912{
Vivien Didelot04bed142016-08-31 18:06:13 -04003913 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003914 int err;
3915
3916 mutex_lock(&chip->reg_lock);
3917 err = mv88e6xxx_port_db_dump(chip, port, &mdb->obj, cb);
3918 mutex_unlock(&chip->reg_lock);
3919
3920 return err;
3921}
3922
Vivien Didelot9d490b42016-08-23 12:38:56 -04003923static struct dsa_switch_ops mv88e6xxx_switch_ops = {
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02003924 .probe = mv88e6xxx_drv_probe,
Andrew Lunn7b314362016-08-22 16:01:01 +02003925 .get_tag_protocol = mv88e6xxx_get_tag_protocol,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003926 .setup = mv88e6xxx_setup,
3927 .set_addr = mv88e6xxx_set_addr,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003928 .adjust_link = mv88e6xxx_adjust_link,
3929 .get_strings = mv88e6xxx_get_strings,
3930 .get_ethtool_stats = mv88e6xxx_get_ethtool_stats,
3931 .get_sset_count = mv88e6xxx_get_sset_count,
3932 .set_eee = mv88e6xxx_set_eee,
3933 .get_eee = mv88e6xxx_get_eee,
3934#ifdef CONFIG_NET_DSA_HWMON
3935 .get_temp = mv88e6xxx_get_temp,
3936 .get_temp_limit = mv88e6xxx_get_temp_limit,
3937 .set_temp_limit = mv88e6xxx_set_temp_limit,
3938 .get_temp_alarm = mv88e6xxx_get_temp_alarm,
3939#endif
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003940 .get_eeprom_len = mv88e6xxx_get_eeprom_len,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003941 .get_eeprom = mv88e6xxx_get_eeprom,
3942 .set_eeprom = mv88e6xxx_set_eeprom,
3943 .get_regs_len = mv88e6xxx_get_regs_len,
3944 .get_regs = mv88e6xxx_get_regs,
Vivien Didelot2cfcd962016-07-18 20:45:40 -04003945 .set_ageing_time = mv88e6xxx_set_ageing_time,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003946 .port_bridge_join = mv88e6xxx_port_bridge_join,
3947 .port_bridge_leave = mv88e6xxx_port_bridge_leave,
3948 .port_stp_state_set = mv88e6xxx_port_stp_state_set,
Vivien Didelot749efcb2016-09-22 16:49:24 -04003949 .port_fast_age = mv88e6xxx_port_fast_age,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003950 .port_vlan_filtering = mv88e6xxx_port_vlan_filtering,
3951 .port_vlan_prepare = mv88e6xxx_port_vlan_prepare,
3952 .port_vlan_add = mv88e6xxx_port_vlan_add,
3953 .port_vlan_del = mv88e6xxx_port_vlan_del,
3954 .port_vlan_dump = mv88e6xxx_port_vlan_dump,
3955 .port_fdb_prepare = mv88e6xxx_port_fdb_prepare,
3956 .port_fdb_add = mv88e6xxx_port_fdb_add,
3957 .port_fdb_del = mv88e6xxx_port_fdb_del,
3958 .port_fdb_dump = mv88e6xxx_port_fdb_dump,
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003959 .port_mdb_prepare = mv88e6xxx_port_mdb_prepare,
3960 .port_mdb_add = mv88e6xxx_port_mdb_add,
3961 .port_mdb_del = mv88e6xxx_port_mdb_del,
3962 .port_mdb_dump = mv88e6xxx_port_mdb_dump,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003963};
3964
Vivien Didelotfad09c72016-06-21 12:28:20 -04003965static int mv88e6xxx_register_switch(struct mv88e6xxx_chip *chip,
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003966 struct device_node *np)
3967{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003968 struct device *dev = chip->dev;
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003969 struct dsa_switch *ds;
3970
3971 ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
3972 if (!ds)
3973 return -ENOMEM;
3974
3975 ds->dev = dev;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003976 ds->priv = chip;
Vivien Didelot9d490b42016-08-23 12:38:56 -04003977 ds->ops = &mv88e6xxx_switch_ops;
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003978
3979 dev_set_drvdata(dev, ds);
3980
3981 return dsa_register_switch(ds, np);
3982}
3983
Vivien Didelotfad09c72016-06-21 12:28:20 -04003984static void mv88e6xxx_unregister_switch(struct mv88e6xxx_chip *chip)
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003985{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003986 dsa_unregister_switch(chip->ds);
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003987}
3988
Vivien Didelot57d32312016-06-20 13:13:58 -04003989static int mv88e6xxx_probe(struct mdio_device *mdiodev)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003990{
3991 struct device *dev = &mdiodev->dev;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003992 struct device_node *np = dev->of_node;
Vivien Didelotcaac8542016-06-20 13:14:09 -04003993 const struct mv88e6xxx_info *compat_info;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003994 struct mv88e6xxx_chip *chip;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003995 u32 eeprom_len;
Andrew Lunn52638f72016-05-10 23:27:22 +02003996 int err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003997
Vivien Didelotcaac8542016-06-20 13:14:09 -04003998 compat_info = of_device_get_match_data(dev);
3999 if (!compat_info)
4000 return -EINVAL;
4001
Vivien Didelotfad09c72016-06-21 12:28:20 -04004002 chip = mv88e6xxx_alloc_chip(dev);
4003 if (!chip)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004004 return -ENOMEM;
4005
Vivien Didelotfad09c72016-06-21 12:28:20 -04004006 chip->info = compat_info;
Vivien Didelotcaac8542016-06-20 13:14:09 -04004007
Vivien Didelotfad09c72016-06-21 12:28:20 -04004008 err = mv88e6xxx_smi_init(chip, mdiodev->bus, mdiodev->addr);
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04004009 if (err)
4010 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004011
Andrew Lunnb4308f02016-11-21 23:26:55 +01004012 chip->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
4013 if (IS_ERR(chip->reset))
4014 return PTR_ERR(chip->reset);
4015
Vivien Didelotfad09c72016-06-21 12:28:20 -04004016 err = mv88e6xxx_detect(chip);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04004017 if (err)
4018 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004019
Vivien Didelote57e5e72016-08-15 17:19:00 -04004020 mv88e6xxx_phy_init(chip);
4021
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04004022 if (chip->info->ops->get_eeprom &&
Andrew Lunnf8cd8752016-05-10 23:27:25 +02004023 !of_property_read_u32(np, "eeprom-length", &eeprom_len))
Vivien Didelotfad09c72016-06-21 12:28:20 -04004024 chip->eeprom_len = eeprom_len;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02004025
Andrew Lunndc30c352016-10-16 19:56:49 +02004026 mutex_lock(&chip->reg_lock);
4027 err = mv88e6xxx_switch_reset(chip);
4028 mutex_unlock(&chip->reg_lock);
Andrew Lunnb516d452016-06-04 21:17:06 +02004029 if (err)
Andrew Lunndc30c352016-10-16 19:56:49 +02004030 goto out;
Andrew Lunnb516d452016-06-04 21:17:06 +02004031
Andrew Lunndc30c352016-10-16 19:56:49 +02004032 chip->irq = of_irq_get(np, 0);
4033 if (chip->irq == -EPROBE_DEFER) {
4034 err = chip->irq;
4035 goto out;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02004036 }
4037
Andrew Lunndc30c352016-10-16 19:56:49 +02004038 if (chip->irq > 0) {
4039 /* Has to be performed before the MDIO bus is created,
4040 * because the PHYs will link there interrupts to these
4041 * interrupt controllers
4042 */
4043 mutex_lock(&chip->reg_lock);
4044 err = mv88e6xxx_g1_irq_setup(chip);
4045 mutex_unlock(&chip->reg_lock);
4046
4047 if (err)
4048 goto out;
4049
4050 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_INT)) {
4051 err = mv88e6xxx_g2_irq_setup(chip);
4052 if (err)
4053 goto out_g1_irq;
4054 }
4055 }
4056
4057 err = mv88e6xxx_mdio_register(chip, np);
4058 if (err)
4059 goto out_g2_irq;
4060
4061 err = mv88e6xxx_register_switch(chip, np);
4062 if (err)
4063 goto out_mdio;
4064
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004065 return 0;
Andrew Lunndc30c352016-10-16 19:56:49 +02004066
4067out_mdio:
4068 mv88e6xxx_mdio_unregister(chip);
4069out_g2_irq:
Andrew Lunn467126442016-11-20 20:14:15 +01004070 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_INT) && chip->irq > 0)
Andrew Lunndc30c352016-10-16 19:56:49 +02004071 mv88e6xxx_g2_irq_free(chip);
4072out_g1_irq:
Andrew Lunn61f7c3f2016-11-20 20:14:19 +01004073 if (chip->irq > 0) {
4074 mutex_lock(&chip->reg_lock);
Andrew Lunn467126442016-11-20 20:14:15 +01004075 mv88e6xxx_g1_irq_free(chip);
Andrew Lunn61f7c3f2016-11-20 20:14:19 +01004076 mutex_unlock(&chip->reg_lock);
4077 }
Andrew Lunndc30c352016-10-16 19:56:49 +02004078out:
4079 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004080}
4081
4082static void mv88e6xxx_remove(struct mdio_device *mdiodev)
4083{
4084 struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev);
Vivien Didelot04bed142016-08-31 18:06:13 -04004085 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004086
Andrew Lunn930188c2016-08-22 16:01:03 +02004087 mv88e6xxx_phy_destroy(chip);
Vivien Didelotfad09c72016-06-21 12:28:20 -04004088 mv88e6xxx_unregister_switch(chip);
4089 mv88e6xxx_mdio_unregister(chip);
Andrew Lunndc30c352016-10-16 19:56:49 +02004090
Andrew Lunn467126442016-11-20 20:14:15 +01004091 if (chip->irq > 0) {
4092 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_INT))
4093 mv88e6xxx_g2_irq_free(chip);
4094 mv88e6xxx_g1_irq_free(chip);
4095 }
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004096}
4097
4098static const struct of_device_id mv88e6xxx_of_match[] = {
Vivien Didelotcaac8542016-06-20 13:14:09 -04004099 {
4100 .compatible = "marvell,mv88e6085",
4101 .data = &mv88e6xxx_table[MV88E6085],
4102 },
Andrew Lunn1a3b39e2016-11-21 23:26:57 +01004103 {
4104 .compatible = "marvell,mv88e6190",
4105 .data = &mv88e6xxx_table[MV88E6190],
4106 },
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004107 { /* sentinel */ },
4108};
4109
4110MODULE_DEVICE_TABLE(of, mv88e6xxx_of_match);
4111
4112static struct mdio_driver mv88e6xxx_driver = {
4113 .probe = mv88e6xxx_probe,
4114 .remove = mv88e6xxx_remove,
4115 .mdiodrv.driver = {
4116 .name = "mv88e6085",
4117 .of_match_table = mv88e6xxx_of_match,
4118 },
4119};
4120
Ben Hutchings98e67302011-11-25 14:36:19 +00004121static int __init mv88e6xxx_init(void)
4122{
Vivien Didelot9d490b42016-08-23 12:38:56 -04004123 register_switch_driver(&mv88e6xxx_switch_ops);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004124 return mdio_driver_register(&mv88e6xxx_driver);
Ben Hutchings98e67302011-11-25 14:36:19 +00004125}
4126module_init(mv88e6xxx_init);
4127
4128static void __exit mv88e6xxx_cleanup(void)
4129{
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004130 mdio_driver_unregister(&mv88e6xxx_driver);
Vivien Didelot9d490b42016-08-23 12:38:56 -04004131 unregister_switch_driver(&mv88e6xxx_switch_ops);
Ben Hutchings98e67302011-11-25 14:36:19 +00004132}
4133module_exit(mv88e6xxx_cleanup);
Ben Hutchings3d825ed2011-11-25 14:37:16 +00004134
4135MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
4136MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
4137MODULE_LICENSE("GPL");