blob: cc43e6f09c5dd0d3e80f26f4aaeff58ac637859f [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;
416
417 for (irq = 0; irq < 16; irq++) {
418 virq = irq_find_mapping(chip->g2_irq.domain, irq);
419 irq_dispose_mapping(virq);
420 }
421
422 irq_domain_remove(chip->g2_irq.domain);
423}
424
425static int mv88e6xxx_g1_irq_setup(struct mv88e6xxx_chip *chip)
426{
427 int err, irq;
428 u16 reg;
429
430 chip->g1_irq.nirqs = chip->info->g1_irqs;
431 chip->g1_irq.domain = irq_domain_add_simple(
432 NULL, chip->g1_irq.nirqs, 0,
433 &mv88e6xxx_g1_irq_domain_ops, chip);
434 if (!chip->g1_irq.domain)
435 return -ENOMEM;
436
437 for (irq = 0; irq < chip->g1_irq.nirqs; irq++)
438 irq_create_mapping(chip->g1_irq.domain, irq);
439
440 chip->g1_irq.chip = mv88e6xxx_g1_irq_chip;
441 chip->g1_irq.masked = ~0;
442
443 err = mv88e6xxx_g1_read(chip, GLOBAL_CONTROL, &reg);
444 if (err)
445 goto out;
446
447 reg &= ~GENMASK(chip->g1_irq.nirqs, 0);
448
449 err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL, reg);
450 if (err)
451 goto out;
452
453 /* Reading the interrupt status clears (most of) them */
454 err = mv88e6xxx_g1_read(chip, GLOBAL_STATUS, &reg);
455 if (err)
456 goto out;
457
458 err = request_threaded_irq(chip->irq, NULL,
459 mv88e6xxx_g1_irq_thread_fn,
460 IRQF_ONESHOT | IRQF_TRIGGER_FALLING,
461 dev_name(chip->dev), chip);
462 if (err)
463 goto out;
464
465 return 0;
466
467out:
468 mv88e6xxx_g1_irq_free(chip);
469
470 return err;
471}
472
Vivien Didelotec561272016-09-02 14:45:33 -0400473int mv88e6xxx_wait(struct mv88e6xxx_chip *chip, int addr, int reg, u16 mask)
Vivien Didelot2d79af62016-08-15 17:18:57 -0400474{
Andrew Lunn6441e6692016-08-19 00:01:55 +0200475 int i;
Vivien Didelot2d79af62016-08-15 17:18:57 -0400476
Andrew Lunn6441e6692016-08-19 00:01:55 +0200477 for (i = 0; i < 16; i++) {
Vivien Didelot2d79af62016-08-15 17:18:57 -0400478 u16 val;
479 int err;
480
481 err = mv88e6xxx_read(chip, addr, reg, &val);
482 if (err)
483 return err;
484
485 if (!(val & mask))
486 return 0;
487
488 usleep_range(1000, 2000);
489 }
490
Andrew Lunn30853552016-08-19 00:01:57 +0200491 dev_err(chip->dev, "Timeout while waiting for switch\n");
Vivien Didelot2d79af62016-08-15 17:18:57 -0400492 return -ETIMEDOUT;
493}
494
Vivien Didelotf22ab642016-07-18 20:45:31 -0400495/* Indirect write to single pointer-data register with an Update bit */
Vivien Didelotec561272016-09-02 14:45:33 -0400496int mv88e6xxx_update(struct mv88e6xxx_chip *chip, int addr, int reg, u16 update)
Vivien Didelotf22ab642016-07-18 20:45:31 -0400497{
498 u16 val;
Andrew Lunn0f02b4f2016-08-19 00:01:56 +0200499 int err;
Vivien Didelotf22ab642016-07-18 20:45:31 -0400500
501 /* Wait until the previous operation is completed */
Andrew Lunn0f02b4f2016-08-19 00:01:56 +0200502 err = mv88e6xxx_wait(chip, addr, reg, BIT(15));
503 if (err)
504 return err;
Vivien Didelotf22ab642016-07-18 20:45:31 -0400505
506 /* Set the Update bit to trigger a write operation */
507 val = BIT(15) | update;
508
509 return mv88e6xxx_write(chip, addr, reg, val);
510}
511
Vivien Didelota935c052016-09-29 12:21:53 -0400512static int mv88e6xxx_ppu_disable(struct mv88e6xxx_chip *chip)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000513{
Vivien Didelot914b32f2016-06-20 13:14:11 -0400514 u16 val;
Vivien Didelota935c052016-09-29 12:21:53 -0400515 int i, err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000516
Vivien Didelota935c052016-09-29 12:21:53 -0400517 err = mv88e6xxx_g1_read(chip, GLOBAL_CONTROL, &val);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400518 if (err)
519 return err;
Vivien Didelot3996a4f2015-10-30 18:56:45 -0400520
Vivien Didelota935c052016-09-29 12:21:53 -0400521 err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL,
522 val & ~GLOBAL_CONTROL_PPU_ENABLE);
523 if (err)
524 return err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000525
Andrew Lunn6441e6692016-08-19 00:01:55 +0200526 for (i = 0; i < 16; i++) {
Vivien Didelota935c052016-09-29 12:21:53 -0400527 err = mv88e6xxx_g1_read(chip, GLOBAL_STATUS, &val);
528 if (err)
529 return err;
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200530
Barry Grussling19b2f972013-01-08 16:05:54 +0000531 usleep_range(1000, 2000);
Vivien Didelota935c052016-09-29 12:21:53 -0400532 if ((val & GLOBAL_STATUS_PPU_MASK) != GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000533 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000534 }
535
536 return -ETIMEDOUT;
537}
538
Vivien Didelotfad09c72016-06-21 12:28:20 -0400539static int mv88e6xxx_ppu_enable(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000540{
Vivien Didelota935c052016-09-29 12:21:53 -0400541 u16 val;
542 int i, err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000543
Vivien Didelota935c052016-09-29 12:21:53 -0400544 err = mv88e6xxx_g1_read(chip, GLOBAL_CONTROL, &val);
545 if (err)
546 return err;
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200547
Vivien Didelota935c052016-09-29 12:21:53 -0400548 err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL,
549 val | GLOBAL_CONTROL_PPU_ENABLE);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200550 if (err)
551 return err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000552
Andrew Lunn6441e6692016-08-19 00:01:55 +0200553 for (i = 0; i < 16; i++) {
Vivien Didelota935c052016-09-29 12:21:53 -0400554 err = mv88e6xxx_g1_read(chip, GLOBAL_STATUS, &val);
555 if (err)
556 return err;
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200557
Barry Grussling19b2f972013-01-08 16:05:54 +0000558 usleep_range(1000, 2000);
Vivien Didelota935c052016-09-29 12:21:53 -0400559 if ((val & GLOBAL_STATUS_PPU_MASK) == GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000560 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000561 }
562
563 return -ETIMEDOUT;
564}
565
566static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
567{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400568 struct mv88e6xxx_chip *chip;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000569
Vivien Didelotfad09c72016-06-21 12:28:20 -0400570 chip = container_of(ugly, struct mv88e6xxx_chip, ppu_work);
Vivien Didelot762eb672016-06-04 21:16:54 +0200571
Vivien Didelotfad09c72016-06-21 12:28:20 -0400572 mutex_lock(&chip->reg_lock);
Vivien Didelot762eb672016-06-04 21:16:54 +0200573
Vivien Didelotfad09c72016-06-21 12:28:20 -0400574 if (mutex_trylock(&chip->ppu_mutex)) {
575 if (mv88e6xxx_ppu_enable(chip) == 0)
576 chip->ppu_disabled = 0;
577 mutex_unlock(&chip->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000578 }
Vivien Didelot762eb672016-06-04 21:16:54 +0200579
Vivien Didelotfad09c72016-06-21 12:28:20 -0400580 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000581}
582
583static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
584{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400585 struct mv88e6xxx_chip *chip = (void *)_ps;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000586
Vivien Didelotfad09c72016-06-21 12:28:20 -0400587 schedule_work(&chip->ppu_work);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000588}
589
Vivien Didelotfad09c72016-06-21 12:28:20 -0400590static int mv88e6xxx_ppu_access_get(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000591{
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000592 int ret;
593
Vivien Didelotfad09c72016-06-21 12:28:20 -0400594 mutex_lock(&chip->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000595
Barry Grussling3675c8d2013-01-08 16:05:53 +0000596 /* If the PHY polling unit is enabled, disable it so that
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000597 * we can access the PHY registers. If it was already
598 * disabled, cancel the timer that is going to re-enable
599 * it.
600 */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400601 if (!chip->ppu_disabled) {
602 ret = mv88e6xxx_ppu_disable(chip);
Barry Grussling85686582013-01-08 16:05:56 +0000603 if (ret < 0) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400604 mutex_unlock(&chip->ppu_mutex);
Barry Grussling85686582013-01-08 16:05:56 +0000605 return ret;
606 }
Vivien Didelotfad09c72016-06-21 12:28:20 -0400607 chip->ppu_disabled = 1;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000608 } else {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400609 del_timer(&chip->ppu_timer);
Barry Grussling85686582013-01-08 16:05:56 +0000610 ret = 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000611 }
612
613 return ret;
614}
615
Vivien Didelotfad09c72016-06-21 12:28:20 -0400616static void mv88e6xxx_ppu_access_put(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000617{
Barry Grussling3675c8d2013-01-08 16:05:53 +0000618 /* Schedule a timer to re-enable the PHY polling unit. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400619 mod_timer(&chip->ppu_timer, jiffies + msecs_to_jiffies(10));
620 mutex_unlock(&chip->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000621}
622
Vivien Didelotfad09c72016-06-21 12:28:20 -0400623static void mv88e6xxx_ppu_state_init(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000624{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400625 mutex_init(&chip->ppu_mutex);
626 INIT_WORK(&chip->ppu_work, mv88e6xxx_ppu_reenable_work);
Wei Yongjun68497a82016-10-22 14:28:00 +0000627 setup_timer(&chip->ppu_timer, mv88e6xxx_ppu_reenable_timer,
628 (unsigned long)chip);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000629}
630
Andrew Lunn930188c2016-08-22 16:01:03 +0200631static void mv88e6xxx_ppu_state_destroy(struct mv88e6xxx_chip *chip)
632{
633 del_timer_sync(&chip->ppu_timer);
634}
635
Vivien Didelote57e5e72016-08-15 17:19:00 -0400636static int mv88e6xxx_phy_ppu_read(struct mv88e6xxx_chip *chip, int addr,
637 int reg, u16 *val)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000638{
Vivien Didelote57e5e72016-08-15 17:19:00 -0400639 int err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000640
Vivien Didelote57e5e72016-08-15 17:19:00 -0400641 err = mv88e6xxx_ppu_access_get(chip);
642 if (!err) {
643 err = mv88e6xxx_read(chip, addr, reg, val);
Vivien Didelotfad09c72016-06-21 12:28:20 -0400644 mv88e6xxx_ppu_access_put(chip);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000645 }
646
Vivien Didelote57e5e72016-08-15 17:19:00 -0400647 return err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000648}
649
Vivien Didelote57e5e72016-08-15 17:19:00 -0400650static int mv88e6xxx_phy_ppu_write(struct mv88e6xxx_chip *chip, int addr,
651 int reg, u16 val)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000652{
Vivien Didelote57e5e72016-08-15 17:19:00 -0400653 int err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000654
Vivien Didelote57e5e72016-08-15 17:19:00 -0400655 err = mv88e6xxx_ppu_access_get(chip);
656 if (!err) {
657 err = mv88e6xxx_write(chip, addr, reg, val);
Vivien Didelotfad09c72016-06-21 12:28:20 -0400658 mv88e6xxx_ppu_access_put(chip);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000659 }
660
Vivien Didelote57e5e72016-08-15 17:19:00 -0400661 return err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000662}
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000663
Vivien Didelotfad09c72016-06-21 12:28:20 -0400664static bool mv88e6xxx_6065_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200665{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400666 return chip->info->family == MV88E6XXX_FAMILY_6065;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200667}
668
Vivien Didelotfad09c72016-06-21 12:28:20 -0400669static bool mv88e6xxx_6095_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200670{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400671 return chip->info->family == MV88E6XXX_FAMILY_6095;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200672}
673
Vivien Didelotfad09c72016-06-21 12:28:20 -0400674static bool mv88e6xxx_6097_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200675{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400676 return chip->info->family == MV88E6XXX_FAMILY_6097;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200677}
678
Vivien Didelotfad09c72016-06-21 12:28:20 -0400679static bool mv88e6xxx_6165_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200680{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400681 return chip->info->family == MV88E6XXX_FAMILY_6165;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200682}
683
Vivien Didelotfad09c72016-06-21 12:28:20 -0400684static bool mv88e6xxx_6185_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200685{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400686 return chip->info->family == MV88E6XXX_FAMILY_6185;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200687}
688
Vivien Didelotfad09c72016-06-21 12:28:20 -0400689static bool mv88e6xxx_6320_family(struct mv88e6xxx_chip *chip)
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700690{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400691 return chip->info->family == MV88E6XXX_FAMILY_6320;
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700692}
693
Vivien Didelotfad09c72016-06-21 12:28:20 -0400694static bool mv88e6xxx_6351_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200695{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400696 return chip->info->family == MV88E6XXX_FAMILY_6351;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200697}
698
Vivien Didelotfad09c72016-06-21 12:28:20 -0400699static bool mv88e6xxx_6352_family(struct mv88e6xxx_chip *chip)
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200700{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400701 return chip->info->family == MV88E6XXX_FAMILY_6352;
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200702}
703
Andrew Lunndea87022015-08-31 15:56:47 +0200704/* We expect the switch to perform auto negotiation if there is a real
705 * phy. However, in the case of a fixed link phy, we force the port
706 * settings from the fixed link settings.
707 */
Vivien Didelotf81ec902016-05-09 13:22:58 -0400708static void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port,
709 struct phy_device *phydev)
Andrew Lunndea87022015-08-31 15:56:47 +0200710{
Vivien Didelot04bed142016-08-31 18:06:13 -0400711 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200712 u16 reg;
713 int err;
Andrew Lunndea87022015-08-31 15:56:47 +0200714
715 if (!phy_is_pseudo_fixed_link(phydev))
716 return;
717
Vivien Didelotfad09c72016-06-21 12:28:20 -0400718 mutex_lock(&chip->reg_lock);
Andrew Lunndea87022015-08-31 15:56:47 +0200719
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200720 err = mv88e6xxx_port_read(chip, port, PORT_PCS_CTRL, &reg);
721 if (err)
Andrew Lunndea87022015-08-31 15:56:47 +0200722 goto out;
723
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200724 reg &= ~(PORT_PCS_CTRL_LINK_UP |
725 PORT_PCS_CTRL_FORCE_LINK |
726 PORT_PCS_CTRL_DUPLEX_FULL |
727 PORT_PCS_CTRL_FORCE_DUPLEX |
728 PORT_PCS_CTRL_UNFORCED);
Andrew Lunndea87022015-08-31 15:56:47 +0200729
730 reg |= PORT_PCS_CTRL_FORCE_LINK;
731 if (phydev->link)
Vivien Didelot57d32312016-06-20 13:13:58 -0400732 reg |= PORT_PCS_CTRL_LINK_UP;
Andrew Lunndea87022015-08-31 15:56:47 +0200733
Vivien Didelotfad09c72016-06-21 12:28:20 -0400734 if (mv88e6xxx_6065_family(chip) && phydev->speed > SPEED_100)
Andrew Lunndea87022015-08-31 15:56:47 +0200735 goto out;
736
737 switch (phydev->speed) {
738 case SPEED_1000:
739 reg |= PORT_PCS_CTRL_1000;
740 break;
741 case SPEED_100:
742 reg |= PORT_PCS_CTRL_100;
743 break;
744 case SPEED_10:
745 reg |= PORT_PCS_CTRL_10;
746 break;
747 default:
748 pr_info("Unknown speed");
749 goto out;
750 }
751
752 reg |= PORT_PCS_CTRL_FORCE_DUPLEX;
753 if (phydev->duplex == DUPLEX_FULL)
754 reg |= PORT_PCS_CTRL_DUPLEX_FULL;
755
Vivien Didelotfad09c72016-06-21 12:28:20 -0400756 if ((mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip)) &&
Vivien Didelot370b4ff2016-09-29 12:21:57 -0400757 (port >= mv88e6xxx_num_ports(chip) - 2)) {
Andrew Lunne7e72ac2015-08-31 15:56:51 +0200758 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
759 reg |= PORT_PCS_CTRL_RGMII_DELAY_RXCLK;
760 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
761 reg |= PORT_PCS_CTRL_RGMII_DELAY_TXCLK;
762 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
763 reg |= (PORT_PCS_CTRL_RGMII_DELAY_RXCLK |
764 PORT_PCS_CTRL_RGMII_DELAY_TXCLK);
765 }
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200766 mv88e6xxx_port_write(chip, port, PORT_PCS_CTRL, reg);
Andrew Lunndea87022015-08-31 15:56:47 +0200767
768out:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400769 mutex_unlock(&chip->reg_lock);
Andrew Lunndea87022015-08-31 15:56:47 +0200770}
771
Vivien Didelotfad09c72016-06-21 12:28:20 -0400772static int _mv88e6xxx_stats_wait(struct mv88e6xxx_chip *chip)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000773{
Vivien Didelota935c052016-09-29 12:21:53 -0400774 u16 val;
775 int i, err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000776
777 for (i = 0; i < 10; i++) {
Vivien Didelota935c052016-09-29 12:21:53 -0400778 err = mv88e6xxx_g1_read(chip, GLOBAL_STATS_OP, &val);
779 if ((val & GLOBAL_STATS_OP_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000780 return 0;
781 }
782
783 return -ETIMEDOUT;
784}
785
Vivien Didelotfad09c72016-06-21 12:28:20 -0400786static int _mv88e6xxx_stats_snapshot(struct mv88e6xxx_chip *chip, int port)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000787{
Vivien Didelota935c052016-09-29 12:21:53 -0400788 int err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000789
Vivien Didelotfad09c72016-06-21 12:28:20 -0400790 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200791 port = (port + 1) << 5;
792
Barry Grussling3675c8d2013-01-08 16:05:53 +0000793 /* Snapshot the hardware statistics counters for this port. */
Vivien Didelota935c052016-09-29 12:21:53 -0400794 err = mv88e6xxx_g1_write(chip, GLOBAL_STATS_OP,
795 GLOBAL_STATS_OP_CAPTURE_PORT |
796 GLOBAL_STATS_OP_HIST_RX_TX | port);
797 if (err)
798 return err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000799
Barry Grussling3675c8d2013-01-08 16:05:53 +0000800 /* Wait for the snapshotting to complete. */
Vivien Didelota935c052016-09-29 12:21:53 -0400801 return _mv88e6xxx_stats_wait(chip);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000802}
803
Vivien Didelotfad09c72016-06-21 12:28:20 -0400804static void _mv88e6xxx_stats_read(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -0400805 int stat, u32 *val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000806{
Vivien Didelota935c052016-09-29 12:21:53 -0400807 u32 value;
808 u16 reg;
809 int err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000810
811 *val = 0;
812
Vivien Didelota935c052016-09-29 12:21:53 -0400813 err = mv88e6xxx_g1_write(chip, GLOBAL_STATS_OP,
814 GLOBAL_STATS_OP_READ_CAPTURED |
815 GLOBAL_STATS_OP_HIST_RX_TX | stat);
816 if (err)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000817 return;
818
Vivien Didelota935c052016-09-29 12:21:53 -0400819 err = _mv88e6xxx_stats_wait(chip);
820 if (err)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000821 return;
822
Vivien Didelota935c052016-09-29 12:21:53 -0400823 err = mv88e6xxx_g1_read(chip, GLOBAL_STATS_COUNTER_32, &reg);
824 if (err)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000825 return;
826
Vivien Didelota935c052016-09-29 12:21:53 -0400827 value = reg << 16;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000828
Vivien Didelota935c052016-09-29 12:21:53 -0400829 err = mv88e6xxx_g1_read(chip, GLOBAL_STATS_COUNTER_01, &reg);
830 if (err)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000831 return;
832
Vivien Didelota935c052016-09-29 12:21:53 -0400833 *val = value | reg;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000834}
835
Andrew Lunne413e7e2015-04-02 04:06:38 +0200836static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100837 { "in_good_octets", 8, 0x00, BANK0, },
838 { "in_bad_octets", 4, 0x02, BANK0, },
839 { "in_unicast", 4, 0x04, BANK0, },
840 { "in_broadcasts", 4, 0x06, BANK0, },
841 { "in_multicasts", 4, 0x07, BANK0, },
842 { "in_pause", 4, 0x16, BANK0, },
843 { "in_undersize", 4, 0x18, BANK0, },
844 { "in_fragments", 4, 0x19, BANK0, },
845 { "in_oversize", 4, 0x1a, BANK0, },
846 { "in_jabber", 4, 0x1b, BANK0, },
847 { "in_rx_error", 4, 0x1c, BANK0, },
848 { "in_fcs_error", 4, 0x1d, BANK0, },
849 { "out_octets", 8, 0x0e, BANK0, },
850 { "out_unicast", 4, 0x10, BANK0, },
851 { "out_broadcasts", 4, 0x13, BANK0, },
852 { "out_multicasts", 4, 0x12, BANK0, },
853 { "out_pause", 4, 0x15, BANK0, },
854 { "excessive", 4, 0x11, BANK0, },
855 { "collisions", 4, 0x1e, BANK0, },
856 { "deferred", 4, 0x05, BANK0, },
857 { "single", 4, 0x14, BANK0, },
858 { "multiple", 4, 0x17, BANK0, },
859 { "out_fcs_error", 4, 0x03, BANK0, },
860 { "late", 4, 0x1f, BANK0, },
861 { "hist_64bytes", 4, 0x08, BANK0, },
862 { "hist_65_127bytes", 4, 0x09, BANK0, },
863 { "hist_128_255bytes", 4, 0x0a, BANK0, },
864 { "hist_256_511bytes", 4, 0x0b, BANK0, },
865 { "hist_512_1023bytes", 4, 0x0c, BANK0, },
866 { "hist_1024_max_bytes", 4, 0x0d, BANK0, },
867 { "sw_in_discards", 4, 0x10, PORT, },
868 { "sw_in_filtered", 2, 0x12, PORT, },
869 { "sw_out_filtered", 2, 0x13, PORT, },
870 { "in_discards", 4, 0x00 | GLOBAL_STATS_OP_BANK_1, BANK1, },
871 { "in_filtered", 4, 0x01 | GLOBAL_STATS_OP_BANK_1, BANK1, },
872 { "in_accepted", 4, 0x02 | GLOBAL_STATS_OP_BANK_1, BANK1, },
873 { "in_bad_accepted", 4, 0x03 | GLOBAL_STATS_OP_BANK_1, BANK1, },
874 { "in_good_avb_class_a", 4, 0x04 | GLOBAL_STATS_OP_BANK_1, BANK1, },
875 { "in_good_avb_class_b", 4, 0x05 | GLOBAL_STATS_OP_BANK_1, BANK1, },
876 { "in_bad_avb_class_a", 4, 0x06 | GLOBAL_STATS_OP_BANK_1, BANK1, },
877 { "in_bad_avb_class_b", 4, 0x07 | GLOBAL_STATS_OP_BANK_1, BANK1, },
878 { "tcam_counter_0", 4, 0x08 | GLOBAL_STATS_OP_BANK_1, BANK1, },
879 { "tcam_counter_1", 4, 0x09 | GLOBAL_STATS_OP_BANK_1, BANK1, },
880 { "tcam_counter_2", 4, 0x0a | GLOBAL_STATS_OP_BANK_1, BANK1, },
881 { "tcam_counter_3", 4, 0x0b | GLOBAL_STATS_OP_BANK_1, BANK1, },
882 { "in_da_unknown", 4, 0x0e | GLOBAL_STATS_OP_BANK_1, BANK1, },
883 { "in_management", 4, 0x0f | GLOBAL_STATS_OP_BANK_1, BANK1, },
884 { "out_queue_0", 4, 0x10 | GLOBAL_STATS_OP_BANK_1, BANK1, },
885 { "out_queue_1", 4, 0x11 | GLOBAL_STATS_OP_BANK_1, BANK1, },
886 { "out_queue_2", 4, 0x12 | GLOBAL_STATS_OP_BANK_1, BANK1, },
887 { "out_queue_3", 4, 0x13 | GLOBAL_STATS_OP_BANK_1, BANK1, },
888 { "out_queue_4", 4, 0x14 | GLOBAL_STATS_OP_BANK_1, BANK1, },
889 { "out_queue_5", 4, 0x15 | GLOBAL_STATS_OP_BANK_1, BANK1, },
890 { "out_queue_6", 4, 0x16 | GLOBAL_STATS_OP_BANK_1, BANK1, },
891 { "out_queue_7", 4, 0x17 | GLOBAL_STATS_OP_BANK_1, BANK1, },
892 { "out_cut_through", 4, 0x18 | GLOBAL_STATS_OP_BANK_1, BANK1, },
893 { "out_octets_a", 4, 0x1a | GLOBAL_STATS_OP_BANK_1, BANK1, },
894 { "out_octets_b", 4, 0x1b | GLOBAL_STATS_OP_BANK_1, BANK1, },
895 { "out_management", 4, 0x1f | GLOBAL_STATS_OP_BANK_1, BANK1, },
Andrew Lunne413e7e2015-04-02 04:06:38 +0200896};
897
Vivien Didelotfad09c72016-06-21 12:28:20 -0400898static bool mv88e6xxx_has_stat(struct mv88e6xxx_chip *chip,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100899 struct mv88e6xxx_hw_stat *stat)
Andrew Lunne413e7e2015-04-02 04:06:38 +0200900{
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100901 switch (stat->type) {
902 case BANK0:
Andrew Lunne413e7e2015-04-02 04:06:38 +0200903 return true;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100904 case BANK1:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400905 return mv88e6xxx_6320_family(chip);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100906 case PORT:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400907 return mv88e6xxx_6095_family(chip) ||
908 mv88e6xxx_6185_family(chip) ||
909 mv88e6xxx_6097_family(chip) ||
910 mv88e6xxx_6165_family(chip) ||
911 mv88e6xxx_6351_family(chip) ||
912 mv88e6xxx_6352_family(chip);
Andrew Lunne413e7e2015-04-02 04:06:38 +0200913 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100914 return false;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000915}
916
Vivien Didelotfad09c72016-06-21 12:28:20 -0400917static uint64_t _mv88e6xxx_get_ethtool_stat(struct mv88e6xxx_chip *chip,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100918 struct mv88e6xxx_hw_stat *s,
Andrew Lunn80c46272015-06-20 18:42:30 +0200919 int port)
920{
Andrew Lunn80c46272015-06-20 18:42:30 +0200921 u32 low;
922 u32 high = 0;
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200923 int err;
924 u16 reg;
Andrew Lunn80c46272015-06-20 18:42:30 +0200925 u64 value;
926
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100927 switch (s->type) {
928 case PORT:
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200929 err = mv88e6xxx_port_read(chip, port, s->reg, &reg);
930 if (err)
Andrew Lunn80c46272015-06-20 18:42:30 +0200931 return UINT64_MAX;
932
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200933 low = reg;
Andrew Lunn80c46272015-06-20 18:42:30 +0200934 if (s->sizeof_stat == 4) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200935 err = mv88e6xxx_port_read(chip, port, s->reg + 1, &reg);
936 if (err)
Andrew Lunn80c46272015-06-20 18:42:30 +0200937 return UINT64_MAX;
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200938 high = reg;
Andrew Lunn80c46272015-06-20 18:42:30 +0200939 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100940 break;
941 case BANK0:
942 case BANK1:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400943 _mv88e6xxx_stats_read(chip, s->reg, &low);
Andrew Lunn80c46272015-06-20 18:42:30 +0200944 if (s->sizeof_stat == 8)
Vivien Didelotfad09c72016-06-21 12:28:20 -0400945 _mv88e6xxx_stats_read(chip, s->reg + 1, &high);
Andrew Lunn80c46272015-06-20 18:42:30 +0200946 }
947 value = (((u64)high) << 16) | low;
948 return value;
949}
950
Vivien Didelotf81ec902016-05-09 13:22:58 -0400951static void mv88e6xxx_get_strings(struct dsa_switch *ds, int port,
952 uint8_t *data)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100953{
Vivien Didelot04bed142016-08-31 18:06:13 -0400954 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100955 struct mv88e6xxx_hw_stat *stat;
956 int i, j;
957
958 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
959 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400960 if (mv88e6xxx_has_stat(chip, stat)) {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100961 memcpy(data + j * ETH_GSTRING_LEN, stat->string,
962 ETH_GSTRING_LEN);
963 j++;
964 }
965 }
966}
967
Vivien Didelotf81ec902016-05-09 13:22:58 -0400968static int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100969{
Vivien Didelot04bed142016-08-31 18:06:13 -0400970 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100971 struct mv88e6xxx_hw_stat *stat;
972 int i, j;
973
974 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
975 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400976 if (mv88e6xxx_has_stat(chip, stat))
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100977 j++;
978 }
979 return j;
980}
981
Vivien Didelotf81ec902016-05-09 13:22:58 -0400982static void mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, int port,
983 uint64_t *data)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000984{
Vivien Didelot04bed142016-08-31 18:06:13 -0400985 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100986 struct mv88e6xxx_hw_stat *stat;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000987 int ret;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100988 int i, j;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000989
Vivien Didelotfad09c72016-06-21 12:28:20 -0400990 mutex_lock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000991
Vivien Didelotfad09c72016-06-21 12:28:20 -0400992 ret = _mv88e6xxx_stats_snapshot(chip, port);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000993 if (ret < 0) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400994 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000995 return;
996 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100997 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
998 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400999 if (mv88e6xxx_has_stat(chip, stat)) {
1000 data[j] = _mv88e6xxx_get_ethtool_stat(chip, stat, port);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +01001001 j++;
1002 }
1003 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001004
Vivien Didelotfad09c72016-06-21 12:28:20 -04001005 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001006}
Ben Hutchings98e67302011-11-25 14:36:19 +00001007
Vivien Didelotf81ec902016-05-09 13:22:58 -04001008static int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001009{
1010 return 32 * sizeof(u16);
1011}
1012
Vivien Didelotf81ec902016-05-09 13:22:58 -04001013static void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
1014 struct ethtool_regs *regs, void *_p)
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001015{
Vivien Didelot04bed142016-08-31 18:06:13 -04001016 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001017 int err;
1018 u16 reg;
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001019 u16 *p = _p;
1020 int i;
1021
1022 regs->version = 0;
1023
1024 memset(p, 0xff, 32 * sizeof(u16));
1025
Vivien Didelotfad09c72016-06-21 12:28:20 -04001026 mutex_lock(&chip->reg_lock);
Vivien Didelot23062512016-05-09 13:22:45 -04001027
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001028 for (i = 0; i < 32; i++) {
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001029
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001030 err = mv88e6xxx_port_read(chip, port, i, &reg);
1031 if (!err)
1032 p[i] = reg;
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001033 }
Vivien Didelot23062512016-05-09 13:22:45 -04001034
Vivien Didelotfad09c72016-06-21 12:28:20 -04001035 mutex_unlock(&chip->reg_lock);
Guenter Roecka1ab91f2014-10-29 10:45:05 -07001036}
1037
Vivien Didelotfad09c72016-06-21 12:28:20 -04001038static int _mv88e6xxx_atu_wait(struct mv88e6xxx_chip *chip)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001039{
Vivien Didelota935c052016-09-29 12:21:53 -04001040 return mv88e6xxx_g1_wait(chip, GLOBAL_ATU_OP, GLOBAL_ATU_OP_BUSY);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001041}
1042
Vivien Didelotf81ec902016-05-09 13:22:58 -04001043static int mv88e6xxx_get_eee(struct dsa_switch *ds, int port,
1044 struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -08001045{
Vivien Didelot04bed142016-08-31 18:06:13 -04001046 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot9c938292016-08-15 17:19:02 -04001047 u16 reg;
1048 int err;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001049
Vivien Didelotfad09c72016-06-21 12:28:20 -04001050 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
Vivien Didelotaadbdb82016-05-09 13:22:44 -04001051 return -EOPNOTSUPP;
1052
Vivien Didelotfad09c72016-06-21 12:28:20 -04001053 mutex_lock(&chip->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001054
Vivien Didelot9c938292016-08-15 17:19:02 -04001055 err = mv88e6xxx_phy_read(chip, port, 16, &reg);
1056 if (err)
Andrew Lunn2f40c692015-04-02 04:06:37 +02001057 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001058
1059 e->eee_enabled = !!(reg & 0x0200);
1060 e->tx_lpi_enabled = !!(reg & 0x0100);
1061
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001062 err = mv88e6xxx_port_read(chip, port, PORT_STATUS, &reg);
Vivien Didelot9c938292016-08-15 17:19:02 -04001063 if (err)
Andrew Lunn2f40c692015-04-02 04:06:37 +02001064 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001065
Andrew Lunncca8b132015-04-02 04:06:39 +02001066 e->eee_active = !!(reg & PORT_STATUS_EEE);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001067out:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001068 mutex_unlock(&chip->reg_lock);
Vivien Didelot9c938292016-08-15 17:19:02 -04001069
1070 return err;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001071}
1072
Vivien Didelotf81ec902016-05-09 13:22:58 -04001073static int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
1074 struct phy_device *phydev, struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -08001075{
Vivien Didelot04bed142016-08-31 18:06:13 -04001076 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot9c938292016-08-15 17:19:02 -04001077 u16 reg;
1078 int err;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001079
Vivien Didelotfad09c72016-06-21 12:28:20 -04001080 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
Vivien Didelotaadbdb82016-05-09 13:22:44 -04001081 return -EOPNOTSUPP;
1082
Vivien Didelotfad09c72016-06-21 12:28:20 -04001083 mutex_lock(&chip->reg_lock);
Guenter Roeck11b3b452015-03-06 22:23:51 -08001084
Vivien Didelot9c938292016-08-15 17:19:02 -04001085 err = mv88e6xxx_phy_read(chip, port, 16, &reg);
1086 if (err)
Andrew Lunn2f40c692015-04-02 04:06:37 +02001087 goto out;
1088
Vivien Didelot9c938292016-08-15 17:19:02 -04001089 reg &= ~0x0300;
Andrew Lunn2f40c692015-04-02 04:06:37 +02001090 if (e->eee_enabled)
1091 reg |= 0x0200;
1092 if (e->tx_lpi_enabled)
1093 reg |= 0x0100;
1094
Vivien Didelot9c938292016-08-15 17:19:02 -04001095 err = mv88e6xxx_phy_write(chip, port, 16, reg);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001096out:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001097 mutex_unlock(&chip->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001098
Vivien Didelot9c938292016-08-15 17:19:02 -04001099 return err;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001100}
1101
Vivien Didelotfad09c72016-06-21 12:28:20 -04001102static int _mv88e6xxx_atu_cmd(struct mv88e6xxx_chip *chip, u16 fid, u16 cmd)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001103{
Vivien Didelota935c052016-09-29 12:21:53 -04001104 u16 val;
1105 int err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001106
Vivien Didelot6dc10bb2016-09-29 12:21:55 -04001107 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G1_ATU_FID)) {
Vivien Didelota935c052016-09-29 12:21:53 -04001108 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_FID, fid);
1109 if (err)
1110 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001111 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001112 /* ATU DBNum[7:4] are located in ATU Control 15:12 */
Vivien Didelota935c052016-09-29 12:21:53 -04001113 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_CONTROL, &val);
1114 if (err)
1115 return err;
Vivien Didelot11ea8092016-03-31 16:53:44 -04001116
Vivien Didelota935c052016-09-29 12:21:53 -04001117 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_CONTROL,
1118 (val & 0xfff) | ((fid << 8) & 0xf000));
1119 if (err)
1120 return err;
Vivien Didelot11ea8092016-03-31 16:53:44 -04001121
1122 /* ATU DBNum[3:0] are located in ATU Operation 3:0 */
1123 cmd |= fid & 0xf;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001124 }
1125
Vivien Didelota935c052016-09-29 12:21:53 -04001126 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_OP, cmd);
1127 if (err)
1128 return err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001129
Vivien Didelotfad09c72016-06-21 12:28:20 -04001130 return _mv88e6xxx_atu_wait(chip);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001131}
1132
Vivien Didelotfad09c72016-06-21 12:28:20 -04001133static int _mv88e6xxx_atu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot37705b72015-09-04 14:34:11 -04001134 struct mv88e6xxx_atu_entry *entry)
1135{
1136 u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK;
1137
1138 if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) {
1139 unsigned int mask, shift;
1140
1141 if (entry->trunk) {
1142 data |= GLOBAL_ATU_DATA_TRUNK;
1143 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
1144 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
1145 } else {
1146 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
1147 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
1148 }
1149
1150 data |= (entry->portv_trunkid << shift) & mask;
1151 }
1152
Vivien Didelota935c052016-09-29 12:21:53 -04001153 return mv88e6xxx_g1_write(chip, GLOBAL_ATU_DATA, data);
Vivien Didelot37705b72015-09-04 14:34:11 -04001154}
1155
Vivien Didelotfad09c72016-06-21 12:28:20 -04001156static int _mv88e6xxx_atu_flush_move(struct mv88e6xxx_chip *chip,
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001157 struct mv88e6xxx_atu_entry *entry,
1158 bool static_too)
1159{
1160 int op;
1161 int err;
1162
Vivien Didelotfad09c72016-06-21 12:28:20 -04001163 err = _mv88e6xxx_atu_wait(chip);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001164 if (err)
1165 return err;
1166
Vivien Didelotfad09c72016-06-21 12:28:20 -04001167 err = _mv88e6xxx_atu_data_write(chip, entry);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001168 if (err)
1169 return err;
1170
1171 if (entry->fid) {
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001172 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB :
1173 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB;
1174 } else {
1175 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL :
1176 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC;
1177 }
1178
Vivien Didelotfad09c72016-06-21 12:28:20 -04001179 return _mv88e6xxx_atu_cmd(chip, entry->fid, op);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001180}
1181
Vivien Didelotfad09c72016-06-21 12:28:20 -04001182static int _mv88e6xxx_atu_flush(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001183 u16 fid, bool static_too)
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001184{
1185 struct mv88e6xxx_atu_entry entry = {
1186 .fid = fid,
1187 .state = 0, /* EntryState bits must be 0 */
1188 };
1189
Vivien Didelotfad09c72016-06-21 12:28:20 -04001190 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001191}
1192
Vivien Didelotfad09c72016-06-21 12:28:20 -04001193static int _mv88e6xxx_atu_move(struct mv88e6xxx_chip *chip, u16 fid,
Andrew Lunn158bc062016-04-28 21:24:06 -04001194 int from_port, int to_port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001195{
1196 struct mv88e6xxx_atu_entry entry = {
1197 .trunk = false,
1198 .fid = fid,
1199 };
1200
1201 /* EntryState bits must be 0xF */
1202 entry.state = GLOBAL_ATU_DATA_STATE_MASK;
1203
1204 /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */
1205 entry.portv_trunkid = (to_port & 0x0f) << 4;
1206 entry.portv_trunkid |= from_port & 0x0f;
1207
Vivien Didelotfad09c72016-06-21 12:28:20 -04001208 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001209}
1210
Vivien Didelotfad09c72016-06-21 12:28:20 -04001211static int _mv88e6xxx_atu_remove(struct mv88e6xxx_chip *chip, u16 fid,
Andrew Lunn158bc062016-04-28 21:24:06 -04001212 int port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001213{
1214 /* Destination port 0xF means remove the entries */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001215 return _mv88e6xxx_atu_move(chip, fid, port, 0x0f, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001216}
1217
Vivien Didelotfad09c72016-06-21 12:28:20 -04001218static int _mv88e6xxx_port_based_vlan_map(struct mv88e6xxx_chip *chip, int port)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001219{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001220 struct net_device *bridge = chip->ports[port].bridge_dev;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001221 struct dsa_switch *ds = chip->ds;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001222 u16 output_ports = 0;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001223 int i;
1224
1225 /* allow CPU port or DSA link(s) to send frames to every port */
1226 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
Vivien Didelot5a7921f2016-11-04 03:23:28 +01001227 output_ports = ~0;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001228 } else {
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001229 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001230 /* allow sending frames to every group member */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001231 if (bridge && chip->ports[i].bridge_dev == bridge)
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001232 output_ports |= BIT(i);
1233
1234 /* allow sending frames to CPU port and DSA link(s) */
1235 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
1236 output_ports |= BIT(i);
1237 }
1238 }
1239
1240 /* prevent frames from going back out of the port they came in on */
1241 output_ports &= ~BIT(port);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001242
Vivien Didelot5a7921f2016-11-04 03:23:28 +01001243 return mv88e6xxx_port_set_vlan_map(chip, port, output_ports);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001244}
1245
Vivien Didelotf81ec902016-05-09 13:22:58 -04001246static void mv88e6xxx_port_stp_state_set(struct dsa_switch *ds, int port,
1247 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001248{
Vivien Didelot04bed142016-08-31 18:06:13 -04001249 struct mv88e6xxx_chip *chip = ds->priv;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001250 int stp_state;
Vivien Didelot553eb542016-05-13 20:38:23 -04001251 int err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001252
1253 switch (state) {
1254 case BR_STATE_DISABLED:
Andrew Lunncca8b132015-04-02 04:06:39 +02001255 stp_state = PORT_CONTROL_STATE_DISABLED;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001256 break;
1257 case BR_STATE_BLOCKING:
1258 case BR_STATE_LISTENING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001259 stp_state = PORT_CONTROL_STATE_BLOCKING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001260 break;
1261 case BR_STATE_LEARNING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001262 stp_state = PORT_CONTROL_STATE_LEARNING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001263 break;
1264 case BR_STATE_FORWARDING:
1265 default:
Andrew Lunncca8b132015-04-02 04:06:39 +02001266 stp_state = PORT_CONTROL_STATE_FORWARDING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001267 break;
1268 }
1269
Vivien Didelotfad09c72016-06-21 12:28:20 -04001270 mutex_lock(&chip->reg_lock);
Vivien Didelote28def332016-11-04 03:23:27 +01001271 err = mv88e6xxx_port_set_state(chip, port, stp_state);
Vivien Didelotfad09c72016-06-21 12:28:20 -04001272 mutex_unlock(&chip->reg_lock);
Vivien Didelot553eb542016-05-13 20:38:23 -04001273
1274 if (err)
Vivien Didelote28def332016-11-04 03:23:27 +01001275 netdev_err(ds->ports[port].netdev, "failed to update state\n");
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001276}
1277
Vivien Didelot749efcb2016-09-22 16:49:24 -04001278static void mv88e6xxx_port_fast_age(struct dsa_switch *ds, int port)
1279{
1280 struct mv88e6xxx_chip *chip = ds->priv;
1281 int err;
1282
1283 mutex_lock(&chip->reg_lock);
1284 err = _mv88e6xxx_atu_remove(chip, 0, port, false);
1285 mutex_unlock(&chip->reg_lock);
1286
1287 if (err)
1288 netdev_err(ds->ports[port].netdev, "failed to flush ATU\n");
1289}
1290
Vivien Didelotfad09c72016-06-21 12:28:20 -04001291static int _mv88e6xxx_vtu_wait(struct mv88e6xxx_chip *chip)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001292{
Vivien Didelota935c052016-09-29 12:21:53 -04001293 return mv88e6xxx_g1_wait(chip, GLOBAL_VTU_OP, GLOBAL_VTU_OP_BUSY);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001294}
1295
Vivien Didelotfad09c72016-06-21 12:28:20 -04001296static int _mv88e6xxx_vtu_cmd(struct mv88e6xxx_chip *chip, u16 op)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001297{
Vivien Didelota935c052016-09-29 12:21:53 -04001298 int err;
Vivien Didelot6b17e862015-08-13 12:52:18 -04001299
Vivien Didelota935c052016-09-29 12:21:53 -04001300 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_OP, op);
1301 if (err)
1302 return err;
Vivien Didelot6b17e862015-08-13 12:52:18 -04001303
Vivien Didelotfad09c72016-06-21 12:28:20 -04001304 return _mv88e6xxx_vtu_wait(chip);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001305}
1306
Vivien Didelotfad09c72016-06-21 12:28:20 -04001307static int _mv88e6xxx_vtu_stu_flush(struct mv88e6xxx_chip *chip)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001308{
1309 int ret;
1310
Vivien Didelotfad09c72016-06-21 12:28:20 -04001311 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001312 if (ret < 0)
1313 return ret;
1314
Vivien Didelotfad09c72016-06-21 12:28:20 -04001315 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_FLUSH_ALL);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001316}
1317
Vivien Didelotfad09c72016-06-21 12:28:20 -04001318static int _mv88e6xxx_vtu_stu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001319 struct mv88e6xxx_vtu_entry *entry,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001320 unsigned int nibble_offset)
1321{
Vivien Didelotb8fee952015-08-13 12:52:19 -04001322 u16 regs[3];
Vivien Didelota935c052016-09-29 12:21:53 -04001323 int i, err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001324
1325 for (i = 0; i < 3; ++i) {
Vivien Didelota935c052016-09-29 12:21:53 -04001326 u16 *reg = &regs[i];
Vivien Didelotb8fee952015-08-13 12:52:19 -04001327
Vivien Didelota935c052016-09-29 12:21:53 -04001328 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_DATA_0_3 + i, reg);
1329 if (err)
1330 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001331 }
1332
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001333 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotb8fee952015-08-13 12:52:19 -04001334 unsigned int shift = (i % 4) * 4 + nibble_offset;
1335 u16 reg = regs[i / 4];
1336
1337 entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
1338 }
1339
1340 return 0;
1341}
1342
Vivien Didelotfad09c72016-06-21 12:28:20 -04001343static int mv88e6xxx_vtu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001344 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001345{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001346 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 0);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001347}
1348
Vivien Didelotfad09c72016-06-21 12:28:20 -04001349static int mv88e6xxx_stu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001350 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001351{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001352 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 2);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001353}
1354
Vivien Didelotfad09c72016-06-21 12:28:20 -04001355static int _mv88e6xxx_vtu_stu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001356 struct mv88e6xxx_vtu_entry *entry,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001357 unsigned int nibble_offset)
1358{
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001359 u16 regs[3] = { 0 };
Vivien Didelota935c052016-09-29 12:21:53 -04001360 int i, err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001361
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001362 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001363 unsigned int shift = (i % 4) * 4 + nibble_offset;
1364 u8 data = entry->data[i];
1365
1366 regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
1367 }
1368
1369 for (i = 0; i < 3; ++i) {
Vivien Didelota935c052016-09-29 12:21:53 -04001370 u16 reg = regs[i];
1371
1372 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_DATA_0_3 + i, reg);
1373 if (err)
1374 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001375 }
1376
1377 return 0;
1378}
1379
Vivien Didelotfad09c72016-06-21 12:28:20 -04001380static int mv88e6xxx_vtu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001381 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001382{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001383 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 0);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001384}
1385
Vivien Didelotfad09c72016-06-21 12:28:20 -04001386static int mv88e6xxx_stu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001387 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001388{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001389 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 2);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001390}
1391
Vivien Didelotfad09c72016-06-21 12:28:20 -04001392static int _mv88e6xxx_vtu_vid_write(struct mv88e6xxx_chip *chip, u16 vid)
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001393{
Vivien Didelota935c052016-09-29 12:21:53 -04001394 return mv88e6xxx_g1_write(chip, GLOBAL_VTU_VID,
1395 vid & GLOBAL_VTU_VID_MASK);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001396}
1397
Vivien Didelotfad09c72016-06-21 12:28:20 -04001398static int _mv88e6xxx_vtu_getnext(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001399 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelotb8fee952015-08-13 12:52:19 -04001400{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001401 struct mv88e6xxx_vtu_entry next = { 0 };
Vivien Didelota935c052016-09-29 12:21:53 -04001402 u16 val;
1403 int err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001404
Vivien Didelota935c052016-09-29 12:21:53 -04001405 err = _mv88e6xxx_vtu_wait(chip);
1406 if (err)
1407 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001408
Vivien Didelota935c052016-09-29 12:21:53 -04001409 err = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_VTU_GET_NEXT);
1410 if (err)
1411 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001412
Vivien Didelota935c052016-09-29 12:21:53 -04001413 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_VID, &val);
1414 if (err)
1415 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001416
Vivien Didelota935c052016-09-29 12:21:53 -04001417 next.vid = val & GLOBAL_VTU_VID_MASK;
1418 next.valid = !!(val & GLOBAL_VTU_VID_VALID);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001419
1420 if (next.valid) {
Vivien Didelota935c052016-09-29 12:21:53 -04001421 err = mv88e6xxx_vtu_data_read(chip, &next);
1422 if (err)
1423 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001424
Vivien Didelot6dc10bb2016-09-29 12:21:55 -04001425 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G1_VTU_FID)) {
Vivien Didelota935c052016-09-29 12:21:53 -04001426 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_FID, &val);
1427 if (err)
1428 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001429
Vivien Didelota935c052016-09-29 12:21:53 -04001430 next.fid = val & GLOBAL_VTU_FID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001431 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001432 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1433 * VTU DBNum[3:0] are located in VTU Operation 3:0
1434 */
Vivien Didelota935c052016-09-29 12:21:53 -04001435 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_OP, &val);
1436 if (err)
1437 return err;
Vivien Didelot11ea8092016-03-31 16:53:44 -04001438
Vivien Didelota935c052016-09-29 12:21:53 -04001439 next.fid = (val & 0xf00) >> 4;
1440 next.fid |= val & 0xf;
Vivien Didelot2e7bd5e2016-03-31 16:53:41 -04001441 }
Vivien Didelotb8fee952015-08-13 12:52:19 -04001442
Vivien Didelotfad09c72016-06-21 12:28:20 -04001443 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
Vivien Didelota935c052016-09-29 12:21:53 -04001444 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_SID, &val);
1445 if (err)
1446 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001447
Vivien Didelota935c052016-09-29 12:21:53 -04001448 next.sid = val & GLOBAL_VTU_SID_MASK;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001449 }
1450 }
1451
1452 *entry = next;
1453 return 0;
1454}
1455
Vivien Didelotf81ec902016-05-09 13:22:58 -04001456static int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
1457 struct switchdev_obj_port_vlan *vlan,
1458 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001459{
Vivien Didelot04bed142016-08-31 18:06:13 -04001460 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001461 struct mv88e6xxx_vtu_entry next;
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001462 u16 pvid;
1463 int err;
1464
Vivien Didelotfad09c72016-06-21 12:28:20 -04001465 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001466 return -EOPNOTSUPP;
1467
Vivien Didelotfad09c72016-06-21 12:28:20 -04001468 mutex_lock(&chip->reg_lock);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001469
Vivien Didelot77064f32016-11-04 03:23:30 +01001470 err = mv88e6xxx_port_get_pvid(chip, port, &pvid);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001471 if (err)
1472 goto unlock;
1473
Vivien Didelotfad09c72016-06-21 12:28:20 -04001474 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001475 if (err)
1476 goto unlock;
1477
1478 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001479 err = _mv88e6xxx_vtu_getnext(chip, &next);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001480 if (err)
1481 break;
1482
1483 if (!next.valid)
1484 break;
1485
1486 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1487 continue;
1488
1489 /* reinit and dump this VLAN obj */
Vivien Didelot57d32312016-06-20 13:13:58 -04001490 vlan->vid_begin = next.vid;
1491 vlan->vid_end = next.vid;
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001492 vlan->flags = 0;
1493
1494 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1495 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1496
1497 if (next.vid == pvid)
1498 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1499
1500 err = cb(&vlan->obj);
1501 if (err)
1502 break;
1503 } while (next.vid < GLOBAL_VTU_VID_MASK);
1504
1505unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001506 mutex_unlock(&chip->reg_lock);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001507
1508 return err;
1509}
1510
Vivien Didelotfad09c72016-06-21 12:28:20 -04001511static int _mv88e6xxx_vtu_loadpurge(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001512 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001513{
Vivien Didelot11ea8092016-03-31 16:53:44 -04001514 u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001515 u16 reg = 0;
Vivien Didelota935c052016-09-29 12:21:53 -04001516 int err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001517
Vivien Didelota935c052016-09-29 12:21:53 -04001518 err = _mv88e6xxx_vtu_wait(chip);
1519 if (err)
1520 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001521
1522 if (!entry->valid)
1523 goto loadpurge;
1524
1525 /* Write port member tags */
Vivien Didelota935c052016-09-29 12:21:53 -04001526 err = mv88e6xxx_vtu_data_write(chip, entry);
1527 if (err)
1528 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001529
Vivien Didelotfad09c72016-06-21 12:28:20 -04001530 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001531 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Vivien Didelota935c052016-09-29 12:21:53 -04001532 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_SID, reg);
1533 if (err)
1534 return err;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001535 }
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001536
Vivien Didelot6dc10bb2016-09-29 12:21:55 -04001537 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G1_VTU_FID)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001538 reg = entry->fid & GLOBAL_VTU_FID_MASK;
Vivien Didelota935c052016-09-29 12:21:53 -04001539 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_FID, reg);
1540 if (err)
1541 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001542 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001543 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1544 * VTU DBNum[3:0] are located in VTU Operation 3:0
1545 */
1546 op |= (entry->fid & 0xf0) << 8;
1547 op |= entry->fid & 0xf;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001548 }
1549
1550 reg = GLOBAL_VTU_VID_VALID;
1551loadpurge:
1552 reg |= entry->vid & GLOBAL_VTU_VID_MASK;
Vivien Didelota935c052016-09-29 12:21:53 -04001553 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_VID, reg);
1554 if (err)
1555 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001556
Vivien Didelotfad09c72016-06-21 12:28:20 -04001557 return _mv88e6xxx_vtu_cmd(chip, op);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001558}
1559
Vivien Didelotfad09c72016-06-21 12:28:20 -04001560static int _mv88e6xxx_stu_getnext(struct mv88e6xxx_chip *chip, u8 sid,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001561 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001562{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001563 struct mv88e6xxx_vtu_entry next = { 0 };
Vivien Didelota935c052016-09-29 12:21:53 -04001564 u16 val;
1565 int err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001566
Vivien Didelota935c052016-09-29 12:21:53 -04001567 err = _mv88e6xxx_vtu_wait(chip);
1568 if (err)
1569 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001570
Vivien Didelota935c052016-09-29 12:21:53 -04001571 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_SID,
1572 sid & GLOBAL_VTU_SID_MASK);
1573 if (err)
1574 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001575
Vivien Didelota935c052016-09-29 12:21:53 -04001576 err = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_GET_NEXT);
1577 if (err)
1578 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001579
Vivien Didelota935c052016-09-29 12:21:53 -04001580 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_SID, &val);
1581 if (err)
1582 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001583
Vivien Didelota935c052016-09-29 12:21:53 -04001584 next.sid = val & GLOBAL_VTU_SID_MASK;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001585
Vivien Didelota935c052016-09-29 12:21:53 -04001586 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_VID, &val);
1587 if (err)
1588 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001589
Vivien Didelota935c052016-09-29 12:21:53 -04001590 next.valid = !!(val & GLOBAL_VTU_VID_VALID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001591
1592 if (next.valid) {
Vivien Didelota935c052016-09-29 12:21:53 -04001593 err = mv88e6xxx_stu_data_read(chip, &next);
1594 if (err)
1595 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001596 }
1597
1598 *entry = next;
1599 return 0;
1600}
1601
Vivien Didelotfad09c72016-06-21 12:28:20 -04001602static int _mv88e6xxx_stu_loadpurge(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001603 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001604{
1605 u16 reg = 0;
Vivien Didelota935c052016-09-29 12:21:53 -04001606 int err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001607
Vivien Didelota935c052016-09-29 12:21:53 -04001608 err = _mv88e6xxx_vtu_wait(chip);
1609 if (err)
1610 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001611
1612 if (!entry->valid)
1613 goto loadpurge;
1614
1615 /* Write port states */
Vivien Didelota935c052016-09-29 12:21:53 -04001616 err = mv88e6xxx_stu_data_write(chip, entry);
1617 if (err)
1618 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001619
1620 reg = GLOBAL_VTU_VID_VALID;
1621loadpurge:
Vivien Didelota935c052016-09-29 12:21:53 -04001622 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_VID, reg);
1623 if (err)
1624 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001625
1626 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Vivien Didelota935c052016-09-29 12:21:53 -04001627 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_SID, reg);
1628 if (err)
1629 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001630
Vivien Didelotfad09c72016-06-21 12:28:20 -04001631 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_LOAD_PURGE);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001632}
1633
Vivien Didelotfad09c72016-06-21 12:28:20 -04001634static int _mv88e6xxx_fid_new(struct mv88e6xxx_chip *chip, u16 *fid)
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001635{
1636 DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001637 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001638 int i, err;
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001639
1640 bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);
1641
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001642 /* Set every FID bit used by the (un)bridged ports */
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001643 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotb4e48c52016-11-04 03:23:29 +01001644 err = mv88e6xxx_port_get_fid(chip, i, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001645 if (err)
1646 return err;
1647
1648 set_bit(*fid, fid_bitmap);
1649 }
1650
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001651 /* Set every FID bit used by the VLAN entries */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001652 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001653 if (err)
1654 return err;
1655
1656 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001657 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001658 if (err)
1659 return err;
1660
1661 if (!vlan.valid)
1662 break;
1663
1664 set_bit(vlan.fid, fid_bitmap);
1665 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
1666
1667 /* The reset value 0x000 is used to indicate that multiple address
1668 * databases are not needed. Return the next positive available.
1669 */
1670 *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1);
Vivien Didelotfad09c72016-06-21 12:28:20 -04001671 if (unlikely(*fid >= mv88e6xxx_num_databases(chip)))
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001672 return -ENOSPC;
1673
1674 /* Clear the database */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001675 return _mv88e6xxx_atu_flush(chip, *fid, true);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001676}
1677
Vivien Didelotfad09c72016-06-21 12:28:20 -04001678static int _mv88e6xxx_vtu_new(struct mv88e6xxx_chip *chip, u16 vid,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001679 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001680{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001681 struct dsa_switch *ds = chip->ds;
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001682 struct mv88e6xxx_vtu_entry vlan = {
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001683 .valid = true,
1684 .vid = vid,
1685 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001686 int i, err;
1687
Vivien Didelotfad09c72016-06-21 12:28:20 -04001688 err = _mv88e6xxx_fid_new(chip, &vlan.fid);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001689 if (err)
1690 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001691
Vivien Didelot3d131f02015-11-03 10:52:52 -05001692 /* exclude all ports except the CPU and DSA ports */
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001693 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i)
Vivien Didelot3d131f02015-11-03 10:52:52 -05001694 vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)
1695 ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED
1696 : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001697
Vivien Didelotfad09c72016-06-21 12:28:20 -04001698 if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
1699 mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip)) {
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001700 struct mv88e6xxx_vtu_entry vstp;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001701
1702 /* Adding a VTU entry requires a valid STU entry. As VSTP is not
1703 * implemented, only one STU entry is needed to cover all VTU
1704 * entries. Thus, validate the SID 0.
1705 */
1706 vlan.sid = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001707 err = _mv88e6xxx_stu_getnext(chip, GLOBAL_VTU_SID_MASK, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001708 if (err)
1709 return err;
1710
1711 if (vstp.sid != vlan.sid || !vstp.valid) {
1712 memset(&vstp, 0, sizeof(vstp));
1713 vstp.valid = true;
1714 vstp.sid = vlan.sid;
1715
Vivien Didelotfad09c72016-06-21 12:28:20 -04001716 err = _mv88e6xxx_stu_loadpurge(chip, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001717 if (err)
1718 return err;
1719 }
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001720 }
1721
1722 *entry = vlan;
1723 return 0;
1724}
1725
Vivien Didelotfad09c72016-06-21 12:28:20 -04001726static int _mv88e6xxx_vtu_get(struct mv88e6xxx_chip *chip, u16 vid,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001727 struct mv88e6xxx_vtu_entry *entry, bool creat)
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001728{
1729 int err;
1730
1731 if (!vid)
1732 return -EINVAL;
1733
Vivien Didelotfad09c72016-06-21 12:28:20 -04001734 err = _mv88e6xxx_vtu_vid_write(chip, vid - 1);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001735 if (err)
1736 return err;
1737
Vivien Didelotfad09c72016-06-21 12:28:20 -04001738 err = _mv88e6xxx_vtu_getnext(chip, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001739 if (err)
1740 return err;
1741
1742 if (entry->vid != vid || !entry->valid) {
1743 if (!creat)
1744 return -EOPNOTSUPP;
1745 /* -ENOENT would've been more appropriate, but switchdev expects
1746 * -EOPNOTSUPP to inform bridge about an eventual software VLAN.
1747 */
1748
Vivien Didelotfad09c72016-06-21 12:28:20 -04001749 err = _mv88e6xxx_vtu_new(chip, vid, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001750 }
1751
1752 return err;
1753}
1754
Vivien Didelotda9c3592016-02-12 12:09:40 -05001755static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
1756 u16 vid_begin, u16 vid_end)
1757{
Vivien Didelot04bed142016-08-31 18:06:13 -04001758 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001759 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelotda9c3592016-02-12 12:09:40 -05001760 int i, err;
1761
1762 if (!vid_begin)
1763 return -EOPNOTSUPP;
1764
Vivien Didelotfad09c72016-06-21 12:28:20 -04001765 mutex_lock(&chip->reg_lock);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001766
Vivien Didelotfad09c72016-06-21 12:28:20 -04001767 err = _mv88e6xxx_vtu_vid_write(chip, vid_begin - 1);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001768 if (err)
1769 goto unlock;
1770
1771 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001772 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001773 if (err)
1774 goto unlock;
1775
1776 if (!vlan.valid)
1777 break;
1778
1779 if (vlan.vid > vid_end)
1780 break;
1781
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001782 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotda9c3592016-02-12 12:09:40 -05001783 if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i))
1784 continue;
1785
1786 if (vlan.data[i] ==
1787 GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1788 continue;
1789
Vivien Didelotfad09c72016-06-21 12:28:20 -04001790 if (chip->ports[i].bridge_dev ==
1791 chip->ports[port].bridge_dev)
Vivien Didelotda9c3592016-02-12 12:09:40 -05001792 break; /* same bridge, check next VLAN */
1793
Andrew Lunnc8b09802016-06-04 21:16:57 +02001794 netdev_warn(ds->ports[port].netdev,
Vivien Didelotda9c3592016-02-12 12:09:40 -05001795 "hardware VLAN %d already used by %s\n",
1796 vlan.vid,
Vivien Didelotfad09c72016-06-21 12:28:20 -04001797 netdev_name(chip->ports[i].bridge_dev));
Vivien Didelotda9c3592016-02-12 12:09:40 -05001798 err = -EOPNOTSUPP;
1799 goto unlock;
1800 }
1801 } while (vlan.vid < vid_end);
1802
1803unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001804 mutex_unlock(&chip->reg_lock);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001805
1806 return err;
1807}
1808
Vivien Didelotf81ec902016-05-09 13:22:58 -04001809static int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port,
1810 bool vlan_filtering)
Vivien Didelot214cdb92016-02-26 13:16:08 -05001811{
Vivien Didelot04bed142016-08-31 18:06:13 -04001812 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot385a0992016-11-04 03:23:31 +01001813 u16 mode = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE :
Vivien Didelot214cdb92016-02-26 13:16:08 -05001814 PORT_CONTROL_2_8021Q_DISABLED;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001815 int err;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001816
Vivien Didelotfad09c72016-06-21 12:28:20 -04001817 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001818 return -EOPNOTSUPP;
1819
Vivien Didelotfad09c72016-06-21 12:28:20 -04001820 mutex_lock(&chip->reg_lock);
Vivien Didelot385a0992016-11-04 03:23:31 +01001821 err = mv88e6xxx_port_set_8021q_mode(chip, port, mode);
Vivien Didelotfad09c72016-06-21 12:28:20 -04001822 mutex_unlock(&chip->reg_lock);
Vivien Didelot214cdb92016-02-26 13:16:08 -05001823
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001824 return err;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001825}
1826
Vivien Didelot57d32312016-06-20 13:13:58 -04001827static int
1828mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
1829 const struct switchdev_obj_port_vlan *vlan,
1830 struct switchdev_trans *trans)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001831{
Vivien Didelot04bed142016-08-31 18:06:13 -04001832 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotda9c3592016-02-12 12:09:40 -05001833 int err;
1834
Vivien Didelotfad09c72016-06-21 12:28:20 -04001835 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001836 return -EOPNOTSUPP;
1837
Vivien Didelotda9c3592016-02-12 12:09:40 -05001838 /* If the requested port doesn't belong to the same bridge as the VLAN
1839 * members, do not support it (yet) and fallback to software VLAN.
1840 */
1841 err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin,
1842 vlan->vid_end);
1843 if (err)
1844 return err;
1845
Vivien Didelot76e398a2015-11-01 12:33:55 -05001846 /* We don't need any dynamic resource from the kernel (yet),
1847 * so skip the prepare phase.
1848 */
1849 return 0;
1850}
1851
Vivien Didelotfad09c72016-06-21 12:28:20 -04001852static int _mv88e6xxx_port_vlan_add(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001853 u16 vid, bool untagged)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001854{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001855 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001856 int err;
1857
Vivien Didelotfad09c72016-06-21 12:28:20 -04001858 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, true);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001859 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001860 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001861
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001862 vlan.data[port] = untagged ?
1863 GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
1864 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
1865
Vivien Didelotfad09c72016-06-21 12:28:20 -04001866 return _mv88e6xxx_vtu_loadpurge(chip, &vlan);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001867}
1868
Vivien Didelotf81ec902016-05-09 13:22:58 -04001869static void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
1870 const struct switchdev_obj_port_vlan *vlan,
1871 struct switchdev_trans *trans)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001872{
Vivien Didelot04bed142016-08-31 18:06:13 -04001873 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001874 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1875 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1876 u16 vid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001877
Vivien Didelotfad09c72016-06-21 12:28:20 -04001878 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001879 return;
1880
Vivien Didelotfad09c72016-06-21 12:28:20 -04001881 mutex_lock(&chip->reg_lock);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001882
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001883 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
Vivien Didelotfad09c72016-06-21 12:28:20 -04001884 if (_mv88e6xxx_port_vlan_add(chip, port, vid, untagged))
Andrew Lunnc8b09802016-06-04 21:16:57 +02001885 netdev_err(ds->ports[port].netdev,
1886 "failed to add VLAN %d%c\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001887 vid, untagged ? 'u' : 't');
Vivien Didelot76e398a2015-11-01 12:33:55 -05001888
Vivien Didelot77064f32016-11-04 03:23:30 +01001889 if (pvid && mv88e6xxx_port_set_pvid(chip, port, vlan->vid_end))
Andrew Lunnc8b09802016-06-04 21:16:57 +02001890 netdev_err(ds->ports[port].netdev, "failed to set PVID %d\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001891 vlan->vid_end);
1892
Vivien Didelotfad09c72016-06-21 12:28:20 -04001893 mutex_unlock(&chip->reg_lock);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001894}
1895
Vivien Didelotfad09c72016-06-21 12:28:20 -04001896static int _mv88e6xxx_port_vlan_del(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001897 int port, u16 vid)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001898{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001899 struct dsa_switch *ds = chip->ds;
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001900 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001901 int i, err;
1902
Vivien Didelotfad09c72016-06-21 12:28:20 -04001903 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001904 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001905 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001906
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001907 /* Tell switchdev if this VLAN is handled in software */
1908 if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
Vivien Didelot3c06f082016-02-05 14:04:39 -05001909 return -EOPNOTSUPP;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001910
1911 vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
1912
1913 /* keep the VLAN unless all ports are excluded */
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001914 vlan.valid = false;
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001915 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelot3d131f02015-11-03 10:52:52 -05001916 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001917 continue;
1918
1919 if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001920 vlan.valid = true;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001921 break;
1922 }
1923 }
1924
Vivien Didelotfad09c72016-06-21 12:28:20 -04001925 err = _mv88e6xxx_vtu_loadpurge(chip, &vlan);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001926 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001927 return err;
1928
Vivien Didelotfad09c72016-06-21 12:28:20 -04001929 return _mv88e6xxx_atu_remove(chip, vlan.fid, port, false);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001930}
1931
Vivien Didelotf81ec902016-05-09 13:22:58 -04001932static int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
1933 const struct switchdev_obj_port_vlan *vlan)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001934{
Vivien Didelot04bed142016-08-31 18:06:13 -04001935 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001936 u16 pvid, vid;
1937 int err = 0;
1938
Vivien Didelotfad09c72016-06-21 12:28:20 -04001939 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001940 return -EOPNOTSUPP;
1941
Vivien Didelotfad09c72016-06-21 12:28:20 -04001942 mutex_lock(&chip->reg_lock);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001943
Vivien Didelot77064f32016-11-04 03:23:30 +01001944 err = mv88e6xxx_port_get_pvid(chip, port, &pvid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001945 if (err)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001946 goto unlock;
1947
Vivien Didelot76e398a2015-11-01 12:33:55 -05001948 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001949 err = _mv88e6xxx_port_vlan_del(chip, port, vid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001950 if (err)
1951 goto unlock;
1952
1953 if (vid == pvid) {
Vivien Didelot77064f32016-11-04 03:23:30 +01001954 err = mv88e6xxx_port_set_pvid(chip, port, 0);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001955 if (err)
1956 goto unlock;
1957 }
1958 }
1959
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001960unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001961 mutex_unlock(&chip->reg_lock);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001962
1963 return err;
1964}
1965
Vivien Didelotfad09c72016-06-21 12:28:20 -04001966static int _mv88e6xxx_atu_mac_write(struct mv88e6xxx_chip *chip,
Vivien Didelotc5723ac2015-08-10 09:09:48 -04001967 const unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001968{
Vivien Didelota935c052016-09-29 12:21:53 -04001969 int i, err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001970
1971 for (i = 0; i < 3; i++) {
Vivien Didelota935c052016-09-29 12:21:53 -04001972 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_MAC_01 + i,
1973 (addr[i * 2] << 8) | addr[i * 2 + 1]);
1974 if (err)
1975 return err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001976 }
1977
1978 return 0;
1979}
1980
Vivien Didelotfad09c72016-06-21 12:28:20 -04001981static int _mv88e6xxx_atu_mac_read(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001982 unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001983{
Vivien Didelota935c052016-09-29 12:21:53 -04001984 u16 val;
1985 int i, err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001986
1987 for (i = 0; i < 3; i++) {
Vivien Didelota935c052016-09-29 12:21:53 -04001988 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_MAC_01 + i, &val);
1989 if (err)
1990 return err;
1991
1992 addr[i * 2] = val >> 8;
1993 addr[i * 2 + 1] = val & 0xff;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001994 }
1995
1996 return 0;
1997}
1998
Vivien Didelotfad09c72016-06-21 12:28:20 -04001999static int _mv88e6xxx_atu_load(struct mv88e6xxx_chip *chip,
Vivien Didelotfd231c82015-08-10 09:09:50 -04002000 struct mv88e6xxx_atu_entry *entry)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002001{
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002002 int ret;
2003
Vivien Didelotfad09c72016-06-21 12:28:20 -04002004 ret = _mv88e6xxx_atu_wait(chip);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002005 if (ret < 0)
2006 return ret;
2007
Vivien Didelotfad09c72016-06-21 12:28:20 -04002008 ret = _mv88e6xxx_atu_mac_write(chip, entry->mac);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002009 if (ret < 0)
2010 return ret;
2011
Vivien Didelotfad09c72016-06-21 12:28:20 -04002012 ret = _mv88e6xxx_atu_data_write(chip, entry);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002013 if (ret < 0)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002014 return ret;
2015
Vivien Didelotfad09c72016-06-21 12:28:20 -04002016 return _mv88e6xxx_atu_cmd(chip, entry->fid, GLOBAL_ATU_OP_LOAD_DB);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002017}
David S. Millercdf09692015-08-11 12:00:37 -07002018
Vivien Didelot88472932016-09-19 19:56:11 -04002019static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
2020 struct mv88e6xxx_atu_entry *entry);
2021
2022static int mv88e6xxx_atu_get(struct mv88e6xxx_chip *chip, int fid,
2023 const u8 *addr, struct mv88e6xxx_atu_entry *entry)
2024{
2025 struct mv88e6xxx_atu_entry next;
2026 int err;
2027
2028 eth_broadcast_addr(next.mac);
2029
2030 err = _mv88e6xxx_atu_mac_write(chip, next.mac);
2031 if (err)
2032 return err;
2033
2034 do {
2035 err = _mv88e6xxx_atu_getnext(chip, fid, &next);
2036 if (err)
2037 return err;
2038
2039 if (next.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2040 break;
2041
2042 if (ether_addr_equal(next.mac, addr)) {
2043 *entry = next;
2044 return 0;
2045 }
2046 } while (!is_broadcast_ether_addr(next.mac));
2047
2048 memset(entry, 0, sizeof(*entry));
2049 entry->fid = fid;
2050 ether_addr_copy(entry->mac, addr);
2051
2052 return 0;
2053}
2054
Vivien Didelot83dabd12016-08-31 11:50:04 -04002055static int mv88e6xxx_port_db_load_purge(struct mv88e6xxx_chip *chip, int port,
2056 const unsigned char *addr, u16 vid,
2057 u8 state)
Vivien Didelotfd231c82015-08-10 09:09:50 -04002058{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04002059 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelot88472932016-09-19 19:56:11 -04002060 struct mv88e6xxx_atu_entry entry;
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002061 int err;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002062
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002063 /* Null VLAN ID corresponds to the port private database */
2064 if (vid == 0)
Vivien Didelotb4e48c52016-11-04 03:23:29 +01002065 err = mv88e6xxx_port_get_fid(chip, port, &vlan.fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002066 else
Vivien Didelotfad09c72016-06-21 12:28:20 -04002067 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002068 if (err)
2069 return err;
2070
Vivien Didelot88472932016-09-19 19:56:11 -04002071 err = mv88e6xxx_atu_get(chip, vlan.fid, addr, &entry);
2072 if (err)
2073 return err;
2074
2075 /* Purge the ATU entry only if no port is using it anymore */
2076 if (state == GLOBAL_ATU_DATA_STATE_UNUSED) {
2077 entry.portv_trunkid &= ~BIT(port);
2078 if (!entry.portv_trunkid)
2079 entry.state = GLOBAL_ATU_DATA_STATE_UNUSED;
2080 } else {
2081 entry.portv_trunkid |= BIT(port);
2082 entry.state = state;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002083 }
2084
Vivien Didelotfad09c72016-06-21 12:28:20 -04002085 return _mv88e6xxx_atu_load(chip, &entry);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002086}
2087
Vivien Didelotf81ec902016-05-09 13:22:58 -04002088static int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
2089 const struct switchdev_obj_port_fdb *fdb,
2090 struct switchdev_trans *trans)
Vivien Didelot146a3202015-10-08 11:35:12 -04002091{
2092 /* We don't need any dynamic resource from the kernel (yet),
2093 * so skip the prepare phase.
2094 */
2095 return 0;
2096}
2097
Vivien Didelotf81ec902016-05-09 13:22:58 -04002098static void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
2099 const struct switchdev_obj_port_fdb *fdb,
2100 struct switchdev_trans *trans)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002101{
Vivien Didelot04bed142016-08-31 18:06:13 -04002102 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot6630e232015-08-06 01:44:07 -04002103
Vivien Didelotfad09c72016-06-21 12:28:20 -04002104 mutex_lock(&chip->reg_lock);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002105 if (mv88e6xxx_port_db_load_purge(chip, port, fdb->addr, fdb->vid,
2106 GLOBAL_ATU_DATA_STATE_UC_STATIC))
2107 netdev_err(ds->ports[port].netdev, "failed to load unicast MAC address\n");
Vivien Didelotfad09c72016-06-21 12:28:20 -04002108 mutex_unlock(&chip->reg_lock);
David S. Millercdf09692015-08-11 12:00:37 -07002109}
2110
Vivien Didelotf81ec902016-05-09 13:22:58 -04002111static int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
2112 const struct switchdev_obj_port_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -07002113{
Vivien Didelot04bed142016-08-31 18:06:13 -04002114 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot83dabd12016-08-31 11:50:04 -04002115 int err;
David S. Millercdf09692015-08-11 12:00:37 -07002116
Vivien Didelotfad09c72016-06-21 12:28:20 -04002117 mutex_lock(&chip->reg_lock);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002118 err = mv88e6xxx_port_db_load_purge(chip, port, fdb->addr, fdb->vid,
2119 GLOBAL_ATU_DATA_STATE_UNUSED);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002120 mutex_unlock(&chip->reg_lock);
David S. Millercdf09692015-08-11 12:00:37 -07002121
Vivien Didelot83dabd12016-08-31 11:50:04 -04002122 return err;
David S. Millercdf09692015-08-11 12:00:37 -07002123}
2124
Vivien Didelotfad09c72016-06-21 12:28:20 -04002125static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
Vivien Didelot1d194042015-08-10 09:09:51 -04002126 struct mv88e6xxx_atu_entry *entry)
David S. Millercdf09692015-08-11 12:00:37 -07002127{
Vivien Didelot1d194042015-08-10 09:09:51 -04002128 struct mv88e6xxx_atu_entry next = { 0 };
Vivien Didelota935c052016-09-29 12:21:53 -04002129 u16 val;
2130 int err;
Vivien Didelot1d194042015-08-10 09:09:51 -04002131
2132 next.fid = fid;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002133
Vivien Didelota935c052016-09-29 12:21:53 -04002134 err = _mv88e6xxx_atu_wait(chip);
2135 if (err)
2136 return err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002137
Vivien Didelota935c052016-09-29 12:21:53 -04002138 err = _mv88e6xxx_atu_cmd(chip, fid, GLOBAL_ATU_OP_GET_NEXT_DB);
2139 if (err)
2140 return err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002141
Vivien Didelota935c052016-09-29 12:21:53 -04002142 err = _mv88e6xxx_atu_mac_read(chip, next.mac);
2143 if (err)
2144 return err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002145
Vivien Didelota935c052016-09-29 12:21:53 -04002146 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_DATA, &val);
2147 if (err)
2148 return err;
Vivien Didelot1d194042015-08-10 09:09:51 -04002149
Vivien Didelota935c052016-09-29 12:21:53 -04002150 next.state = val & GLOBAL_ATU_DATA_STATE_MASK;
Vivien Didelot1d194042015-08-10 09:09:51 -04002151 if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2152 unsigned int mask, shift;
2153
Vivien Didelota935c052016-09-29 12:21:53 -04002154 if (val & GLOBAL_ATU_DATA_TRUNK) {
Vivien Didelot1d194042015-08-10 09:09:51 -04002155 next.trunk = true;
2156 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
2157 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
2158 } else {
2159 next.trunk = false;
2160 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
2161 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
2162 }
2163
Vivien Didelota935c052016-09-29 12:21:53 -04002164 next.portv_trunkid = (val & mask) >> shift;
Vivien Didelot1d194042015-08-10 09:09:51 -04002165 }
2166
2167 *entry = next;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002168 return 0;
2169}
2170
Vivien Didelot83dabd12016-08-31 11:50:04 -04002171static int mv88e6xxx_port_db_dump_fid(struct mv88e6xxx_chip *chip,
2172 u16 fid, u16 vid, int port,
2173 struct switchdev_obj *obj,
2174 int (*cb)(struct switchdev_obj *obj))
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002175{
2176 struct mv88e6xxx_atu_entry addr = {
2177 .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2178 };
2179 int err;
2180
Vivien Didelotfad09c72016-06-21 12:28:20 -04002181 err = _mv88e6xxx_atu_mac_write(chip, addr.mac);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002182 if (err)
2183 return err;
2184
2185 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002186 err = _mv88e6xxx_atu_getnext(chip, fid, &addr);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002187 if (err)
Vivien Didelot83dabd12016-08-31 11:50:04 -04002188 return err;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002189
2190 if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2191 break;
2192
Vivien Didelot83dabd12016-08-31 11:50:04 -04002193 if (addr.trunk || (addr.portv_trunkid & BIT(port)) == 0)
2194 continue;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002195
Vivien Didelot83dabd12016-08-31 11:50:04 -04002196 if (obj->id == SWITCHDEV_OBJ_ID_PORT_FDB) {
2197 struct switchdev_obj_port_fdb *fdb;
2198
2199 if (!is_unicast_ether_addr(addr.mac))
2200 continue;
2201
2202 fdb = SWITCHDEV_OBJ_PORT_FDB(obj);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002203 fdb->vid = vid;
2204 ether_addr_copy(fdb->addr, addr.mac);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002205 if (addr.state == GLOBAL_ATU_DATA_STATE_UC_STATIC)
2206 fdb->ndm_state = NUD_NOARP;
2207 else
2208 fdb->ndm_state = NUD_REACHABLE;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04002209 } else if (obj->id == SWITCHDEV_OBJ_ID_PORT_MDB) {
2210 struct switchdev_obj_port_mdb *mdb;
2211
2212 if (!is_multicast_ether_addr(addr.mac))
2213 continue;
2214
2215 mdb = SWITCHDEV_OBJ_PORT_MDB(obj);
2216 mdb->vid = vid;
2217 ether_addr_copy(mdb->addr, addr.mac);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002218 } else {
2219 return -EOPNOTSUPP;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002220 }
Vivien Didelot83dabd12016-08-31 11:50:04 -04002221
2222 err = cb(obj);
2223 if (err)
2224 return err;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002225 } while (!is_broadcast_ether_addr(addr.mac));
2226
2227 return err;
2228}
2229
Vivien Didelot83dabd12016-08-31 11:50:04 -04002230static int mv88e6xxx_port_db_dump(struct mv88e6xxx_chip *chip, int port,
2231 struct switchdev_obj *obj,
2232 int (*cb)(struct switchdev_obj *obj))
2233{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04002234 struct mv88e6xxx_vtu_entry vlan = {
Vivien Didelot83dabd12016-08-31 11:50:04 -04002235 .vid = GLOBAL_VTU_VID_MASK, /* all ones */
2236 };
2237 u16 fid;
2238 int err;
2239
2240 /* Dump port's default Filtering Information Database (VLAN ID 0) */
Vivien Didelotb4e48c52016-11-04 03:23:29 +01002241 err = mv88e6xxx_port_get_fid(chip, port, &fid);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002242 if (err)
2243 return err;
2244
2245 err = mv88e6xxx_port_db_dump_fid(chip, fid, 0, port, obj, cb);
2246 if (err)
2247 return err;
2248
2249 /* Dump VLANs' Filtering Information Databases */
2250 err = _mv88e6xxx_vtu_vid_write(chip, vlan.vid);
2251 if (err)
2252 return err;
2253
2254 do {
2255 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
2256 if (err)
2257 return err;
2258
2259 if (!vlan.valid)
2260 break;
2261
2262 err = mv88e6xxx_port_db_dump_fid(chip, vlan.fid, vlan.vid, port,
2263 obj, cb);
2264 if (err)
2265 return err;
2266 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
2267
2268 return err;
2269}
2270
Vivien Didelotf81ec902016-05-09 13:22:58 -04002271static int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
2272 struct switchdev_obj_port_fdb *fdb,
2273 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotf33475b2015-10-22 09:34:41 -04002274{
Vivien Didelot04bed142016-08-31 18:06:13 -04002275 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002276 int err;
2277
Vivien Didelotfad09c72016-06-21 12:28:20 -04002278 mutex_lock(&chip->reg_lock);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002279 err = mv88e6xxx_port_db_dump(chip, port, &fdb->obj, cb);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002280 mutex_unlock(&chip->reg_lock);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002281
2282 return err;
2283}
2284
Vivien Didelotf81ec902016-05-09 13:22:58 -04002285static int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
2286 struct net_device *bridge)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002287{
Vivien Didelot04bed142016-08-31 18:06:13 -04002288 struct mv88e6xxx_chip *chip = ds->priv;
Colin Ian King1d9619d2016-04-25 23:11:22 +01002289 int i, err = 0;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002290
Vivien Didelotfad09c72016-06-21 12:28:20 -04002291 mutex_lock(&chip->reg_lock);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002292
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002293 /* Assign the bridge and remap each port's VLANTable */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002294 chip->ports[port].bridge_dev = bridge;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002295
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002296 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002297 if (chip->ports[i].bridge_dev == bridge) {
2298 err = _mv88e6xxx_port_based_vlan_map(chip, i);
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002299 if (err)
2300 break;
2301 }
2302 }
2303
Vivien Didelotfad09c72016-06-21 12:28:20 -04002304 mutex_unlock(&chip->reg_lock);
Vivien Didelota6692752016-02-12 12:09:39 -05002305
Vivien Didelot466dfa02016-02-26 13:16:05 -05002306 return err;
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002307}
2308
Vivien Didelotf81ec902016-05-09 13:22:58 -04002309static void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002310{
Vivien Didelot04bed142016-08-31 18:06:13 -04002311 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002312 struct net_device *bridge = chip->ports[port].bridge_dev;
Vivien Didelot16bfa702016-03-13 16:21:33 -04002313 int i;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002314
Vivien Didelotfad09c72016-06-21 12:28:20 -04002315 mutex_lock(&chip->reg_lock);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002316
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002317 /* Unassign the bridge and remap each port's VLANTable */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002318 chip->ports[port].bridge_dev = NULL;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002319
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002320 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002321 if (i == port || chip->ports[i].bridge_dev == bridge)
2322 if (_mv88e6xxx_port_based_vlan_map(chip, i))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002323 netdev_warn(ds->ports[i].netdev,
2324 "failed to remap\n");
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002325
Vivien Didelotfad09c72016-06-21 12:28:20 -04002326 mutex_unlock(&chip->reg_lock);
Vivien Didelot66d9cd02016-02-05 14:07:14 -05002327}
2328
Vivien Didelotfad09c72016-06-21 12:28:20 -04002329static int mv88e6xxx_switch_reset(struct mv88e6xxx_chip *chip)
Vivien Didelot552238b2016-05-09 13:22:49 -04002330{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002331 bool ppu_active = mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE);
Vivien Didelot552238b2016-05-09 13:22:49 -04002332 u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002333 struct gpio_desc *gpiod = chip->reset;
Vivien Didelot552238b2016-05-09 13:22:49 -04002334 unsigned long timeout;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002335 u16 reg;
Vivien Didelota935c052016-09-29 12:21:53 -04002336 int err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002337 int i;
2338
2339 /* Set all ports to the disabled state. */
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002340 for (i = 0; i < mv88e6xxx_num_ports(chip); i++) {
Vivien Didelote28def332016-11-04 03:23:27 +01002341 err = mv88e6xxx_port_set_state(chip, i,
2342 PORT_CONTROL_STATE_DISABLED);
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002343 if (err)
2344 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002345 }
2346
2347 /* Wait for transmit queues to drain. */
2348 usleep_range(2000, 4000);
2349
2350 /* If there is a gpio connected to the reset pin, toggle it */
2351 if (gpiod) {
2352 gpiod_set_value_cansleep(gpiod, 1);
2353 usleep_range(10000, 20000);
2354 gpiod_set_value_cansleep(gpiod, 0);
2355 usleep_range(10000, 20000);
2356 }
2357
2358 /* Reset the switch. Keep the PPU active if requested. The PPU
2359 * needs to be active to support indirect phy register access
2360 * through global registers 0x18 and 0x19.
2361 */
2362 if (ppu_active)
Vivien Didelota935c052016-09-29 12:21:53 -04002363 err = mv88e6xxx_g1_write(chip, 0x04, 0xc000);
Vivien Didelot552238b2016-05-09 13:22:49 -04002364 else
Vivien Didelota935c052016-09-29 12:21:53 -04002365 err = mv88e6xxx_g1_write(chip, 0x04, 0xc400);
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002366 if (err)
2367 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002368
2369 /* Wait up to one second for reset to complete. */
2370 timeout = jiffies + 1 * HZ;
2371 while (time_before(jiffies, timeout)) {
Vivien Didelota935c052016-09-29 12:21:53 -04002372 err = mv88e6xxx_g1_read(chip, 0x00, &reg);
2373 if (err)
2374 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002375
Vivien Didelota935c052016-09-29 12:21:53 -04002376 if ((reg & is_reset) == is_reset)
Vivien Didelot552238b2016-05-09 13:22:49 -04002377 break;
2378 usleep_range(1000, 2000);
2379 }
2380 if (time_after(jiffies, timeout))
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002381 err = -ETIMEDOUT;
Vivien Didelot552238b2016-05-09 13:22:49 -04002382 else
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002383 err = 0;
Vivien Didelot552238b2016-05-09 13:22:49 -04002384
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002385 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002386}
2387
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002388static int mv88e6xxx_serdes_power_on(struct mv88e6xxx_chip *chip)
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002389{
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002390 u16 val;
2391 int err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002392
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002393 /* Clear Power Down bit */
2394 err = mv88e6xxx_serdes_read(chip, MII_BMCR, &val);
2395 if (err)
2396 return err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002397
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002398 if (val & BMCR_PDOWN) {
2399 val &= ~BMCR_PDOWN;
2400 err = mv88e6xxx_serdes_write(chip, MII_BMCR, val);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002401 }
2402
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002403 return err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002404}
2405
Vivien Didelotfad09c72016-06-21 12:28:20 -04002406static int mv88e6xxx_setup_port(struct mv88e6xxx_chip *chip, int port)
Guenter Roeckd827e882015-03-26 18:36:29 -07002407{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002408 struct dsa_switch *ds = chip->ds;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002409 int err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002410 u16 reg;
Guenter Roeckd827e882015-03-26 18:36:29 -07002411
Vivien Didelotfad09c72016-06-21 12:28:20 -04002412 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2413 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2414 mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip) ||
2415 mv88e6xxx_6065_family(chip) || mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002416 /* MAC Forcing register: don't force link, speed,
2417 * duplex or flow control state to any particular
2418 * values on physical ports, but force the CPU port
2419 * and all DSA ports to their maximum bandwidth and
2420 * full duplex.
2421 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002422 err = mv88e6xxx_port_read(chip, port, PORT_PCS_CTRL, &reg);
Andrew Lunn60045cb2015-08-17 23:52:51 +02002423 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
Russell King53adc9e2015-09-21 21:42:59 +01002424 reg &= ~PORT_PCS_CTRL_UNFORCED;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002425 reg |= PORT_PCS_CTRL_FORCE_LINK |
2426 PORT_PCS_CTRL_LINK_UP |
2427 PORT_PCS_CTRL_DUPLEX_FULL |
2428 PORT_PCS_CTRL_FORCE_DUPLEX;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002429 if (mv88e6xxx_6065_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002430 reg |= PORT_PCS_CTRL_100;
2431 else
2432 reg |= PORT_PCS_CTRL_1000;
2433 } else {
2434 reg |= PORT_PCS_CTRL_UNFORCED;
2435 }
2436
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002437 err = mv88e6xxx_port_write(chip, port, PORT_PCS_CTRL, reg);
2438 if (err)
2439 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002440 }
2441
2442 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
2443 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
2444 * tunneling, determine priority by looking at 802.1p and IP
2445 * priority fields (IP prio has precedence), and set STP state
2446 * to Forwarding.
2447 *
2448 * If this is the CPU link, use DSA or EDSA tagging depending
2449 * on which tagging mode was configured.
2450 *
2451 * If this is a link to another switch, use DSA tagging mode.
2452 *
2453 * If this is the upstream port for this switch, enable
2454 * forwarding of unknown unicasts and multicasts.
2455 */
2456 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002457 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2458 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2459 mv88e6xxx_6095_family(chip) || mv88e6xxx_6065_family(chip) ||
2460 mv88e6xxx_6185_family(chip) || mv88e6xxx_6320_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002461 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
2462 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
2463 PORT_CONTROL_STATE_FORWARDING;
2464 if (dsa_is_cpu_port(ds, port)) {
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002465 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA))
Andrew Lunn5377b802016-06-04 21:17:02 +02002466 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA |
Andrew Lunnc047a1f2015-09-29 01:50:56 +02002467 PORT_CONTROL_FORWARD_UNKNOWN_MC;
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002468 else
2469 reg |= PORT_CONTROL_DSA_TAG;
Jamie Lentinf027e0c2016-08-22 16:01:04 +02002470 reg |= PORT_CONTROL_EGRESS_ADD_TAG |
2471 PORT_CONTROL_FORWARD_UNKNOWN;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002472 }
Andrew Lunn6083ce72015-08-17 23:52:52 +02002473 if (dsa_is_dsa_port(ds, port)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002474 if (mv88e6xxx_6095_family(chip) ||
2475 mv88e6xxx_6185_family(chip))
Andrew Lunn6083ce72015-08-17 23:52:52 +02002476 reg |= PORT_CONTROL_DSA_TAG;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002477 if (mv88e6xxx_6352_family(chip) ||
2478 mv88e6xxx_6351_family(chip) ||
2479 mv88e6xxx_6165_family(chip) ||
2480 mv88e6xxx_6097_family(chip) ||
2481 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002482 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunn6083ce72015-08-17 23:52:52 +02002483 }
2484
Andrew Lunn54d792f2015-05-06 01:09:47 +02002485 if (port == dsa_upstream_port(ds))
2486 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2487 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2488 }
2489 if (reg) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002490 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL, reg);
2491 if (err)
2492 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002493 }
2494
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002495 /* If this port is connected to a SerDes, make sure the SerDes is not
2496 * powered down.
2497 */
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002498 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_SERDES)) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002499 err = mv88e6xxx_port_read(chip, port, PORT_STATUS, &reg);
2500 if (err)
2501 return err;
2502 reg &= PORT_STATUS_CMODE_MASK;
2503 if ((reg == PORT_STATUS_CMODE_100BASE_X) ||
2504 (reg == PORT_STATUS_CMODE_1000BASE_X) ||
2505 (reg == PORT_STATUS_CMODE_SGMII)) {
2506 err = mv88e6xxx_serdes_power_on(chip);
2507 if (err < 0)
2508 return err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002509 }
2510 }
2511
Vivien Didelot8efdda42015-08-13 12:52:23 -04002512 /* Port Control 2: don't force a good FCS, set the maximum frame size to
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002513 * 10240 bytes, disable 802.1q tags checking, don't discard tagged or
Vivien Didelot8efdda42015-08-13 12:52:23 -04002514 * untagged frames on this port, do a destination address lookup on all
2515 * received packets as usual, disable ARP mirroring and don't send a
2516 * copy of all transmitted/received frames on this port to the CPU.
Andrew Lunn54d792f2015-05-06 01:09:47 +02002517 */
2518 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002519 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2520 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2521 mv88e6xxx_6095_family(chip) || mv88e6xxx_6320_family(chip) ||
2522 mv88e6xxx_6185_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002523 reg = PORT_CONTROL_2_MAP_DA;
2524
Vivien Didelotfad09c72016-06-21 12:28:20 -04002525 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2526 mv88e6xxx_6165_family(chip) || mv88e6xxx_6320_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002527 reg |= PORT_CONTROL_2_JUMBO_10240;
2528
Vivien Didelotfad09c72016-06-21 12:28:20 -04002529 if (mv88e6xxx_6095_family(chip) || mv88e6xxx_6185_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002530 /* Set the upstream port this port should use */
2531 reg |= dsa_upstream_port(ds);
2532 /* enable forwarding of unknown multicast addresses to
2533 * the upstream port
2534 */
2535 if (port == dsa_upstream_port(ds))
2536 reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
2537 }
2538
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002539 reg |= PORT_CONTROL_2_8021Q_DISABLED;
Vivien Didelot8efdda42015-08-13 12:52:23 -04002540
Andrew Lunn54d792f2015-05-06 01:09:47 +02002541 if (reg) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002542 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_2, reg);
2543 if (err)
2544 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002545 }
2546
2547 /* Port Association Vector: when learning source addresses
2548 * of packets, add the address to the address database using
2549 * a port bitmap that has only the bit for this port set and
2550 * the other bits clear.
2551 */
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002552 reg = 1 << port;
Vivien Didelot996ecb82016-04-14 14:42:08 -04002553 /* Disable learning for CPU port */
2554 if (dsa_is_cpu_port(ds, port))
Vivien Didelot65fa4022016-04-14 14:42:07 -04002555 reg = 0;
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002556
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002557 err = mv88e6xxx_port_write(chip, port, PORT_ASSOC_VECTOR, reg);
2558 if (err)
2559 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002560
2561 /* Egress rate control 2: disable egress rate control. */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002562 err = mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL_2, 0x0000);
2563 if (err)
2564 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002565
Vivien Didelotfad09c72016-06-21 12:28:20 -04002566 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2567 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2568 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002569 /* Do not limit the period of time that this port can
2570 * be paused for by the remote end or the period of
2571 * time that this port can pause the remote end.
2572 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002573 err = mv88e6xxx_port_write(chip, port, PORT_PAUSE_CTRL, 0x0000);
2574 if (err)
2575 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002576
2577 /* Port ATU control: disable limiting the number of
2578 * address database entries that this port is allowed
2579 * to use.
2580 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002581 err = mv88e6xxx_port_write(chip, port, PORT_ATU_CONTROL,
2582 0x0000);
Andrew Lunn54d792f2015-05-06 01:09:47 +02002583 /* Priority Override: disable DA, SA and VTU priority
2584 * override.
2585 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002586 err = mv88e6xxx_port_write(chip, port, PORT_PRI_OVERRIDE,
2587 0x0000);
2588 if (err)
2589 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002590
2591 /* Port Ethertype: use the Ethertype DSA Ethertype
2592 * value.
2593 */
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002594 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA)) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002595 err = mv88e6xxx_port_write(chip, port, PORT_ETH_TYPE,
2596 ETH_P_EDSA);
2597 if (err)
2598 return err;
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002599 }
2600
Andrew Lunn54d792f2015-05-06 01:09:47 +02002601 /* Tag Remap: use an identity 802.1p prio -> switch
2602 * prio mapping.
2603 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002604 err = mv88e6xxx_port_write(chip, port, PORT_TAG_REGMAP_0123,
2605 0x3210);
2606 if (err)
2607 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002608
2609 /* Tag Remap 2: use an identity 802.1p prio -> switch
2610 * prio mapping.
2611 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002612 err = mv88e6xxx_port_write(chip, port, PORT_TAG_REGMAP_4567,
2613 0x7654);
2614 if (err)
2615 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002616 }
2617
Jamie Lentin1bc261f2016-08-22 22:47:08 +01002618 /* Rate Control: disable ingress rate limiting. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002619 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2620 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
Vivien Didelotfad09c72016-06-21 12:28:20 -04002621 mv88e6xxx_6320_family(chip)) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002622 err = mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL,
2623 0x0001);
2624 if (err)
2625 return err;
Jamie Lentin1bc261f2016-08-22 22:47:08 +01002626 } else if (mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip)) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002627 err = mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL,
2628 0x0000);
2629 if (err)
2630 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002631 }
2632
Guenter Roeck366f0a02015-03-26 18:36:30 -07002633 /* Port Control 1: disable trunking, disable sending
2634 * learning messages to this port.
Guenter Roeckd827e882015-03-26 18:36:29 -07002635 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002636 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_1, 0x0000);
2637 if (err)
2638 return err;
Guenter Roeckd827e882015-03-26 18:36:29 -07002639
Vivien Didelot207afda2016-04-14 14:42:09 -04002640 /* Port based VLAN map: give each port the same default address
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002641 * database, and allow bidirectional communication between the
2642 * CPU and DSA port(s), and the other ports.
Guenter Roeckd827e882015-03-26 18:36:29 -07002643 */
Vivien Didelotb4e48c52016-11-04 03:23:29 +01002644 err = mv88e6xxx_port_set_fid(chip, port, 0);
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002645 if (err)
2646 return err;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002647
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002648 err = _mv88e6xxx_port_based_vlan_map(chip, port);
2649 if (err)
2650 return err;
Guenter Roeckd827e882015-03-26 18:36:29 -07002651
2652 /* Default VLAN ID and priority: don't set a default VLAN
2653 * ID, and set the default packet priority to zero.
2654 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002655 return mv88e6xxx_port_write(chip, port, PORT_DEFAULT_VLAN, 0x0000);
Andrew Lunndbde9e62015-05-06 01:09:48 +02002656}
2657
Wei Yongjunaa0938c2016-10-18 15:53:37 +00002658static int mv88e6xxx_g1_set_switch_mac(struct mv88e6xxx_chip *chip, u8 *addr)
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002659{
2660 int err;
2661
Vivien Didelota935c052016-09-29 12:21:53 -04002662 err = mv88e6xxx_g1_write(chip, GLOBAL_MAC_01, (addr[0] << 8) | addr[1]);
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002663 if (err)
2664 return err;
2665
Vivien Didelota935c052016-09-29 12:21:53 -04002666 err = mv88e6xxx_g1_write(chip, GLOBAL_MAC_23, (addr[2] << 8) | addr[3]);
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002667 if (err)
2668 return err;
2669
Vivien Didelota935c052016-09-29 12:21:53 -04002670 err = mv88e6xxx_g1_write(chip, GLOBAL_MAC_45, (addr[4] << 8) | addr[5]);
2671 if (err)
2672 return err;
2673
2674 return 0;
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002675}
2676
Vivien Didelotacddbd22016-07-18 20:45:39 -04002677static int mv88e6xxx_g1_set_age_time(struct mv88e6xxx_chip *chip,
2678 unsigned int msecs)
2679{
2680 const unsigned int coeff = chip->info->age_time_coeff;
2681 const unsigned int min = 0x01 * coeff;
2682 const unsigned int max = 0xff * coeff;
2683 u8 age_time;
2684 u16 val;
2685 int err;
2686
2687 if (msecs < min || msecs > max)
2688 return -ERANGE;
2689
2690 /* Round to nearest multiple of coeff */
2691 age_time = (msecs + coeff / 2) / coeff;
2692
Vivien Didelota935c052016-09-29 12:21:53 -04002693 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_CONTROL, &val);
Vivien Didelotacddbd22016-07-18 20:45:39 -04002694 if (err)
2695 return err;
2696
2697 /* AgeTime is 11:4 bits */
2698 val &= ~0xff0;
2699 val |= age_time << 4;
2700
Vivien Didelota935c052016-09-29 12:21:53 -04002701 return mv88e6xxx_g1_write(chip, GLOBAL_ATU_CONTROL, val);
Vivien Didelotacddbd22016-07-18 20:45:39 -04002702}
2703
Vivien Didelot2cfcd962016-07-18 20:45:40 -04002704static int mv88e6xxx_set_ageing_time(struct dsa_switch *ds,
2705 unsigned int ageing_time)
2706{
Vivien Didelot04bed142016-08-31 18:06:13 -04002707 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot2cfcd962016-07-18 20:45:40 -04002708 int err;
2709
2710 mutex_lock(&chip->reg_lock);
2711 err = mv88e6xxx_g1_set_age_time(chip, ageing_time);
2712 mutex_unlock(&chip->reg_lock);
2713
2714 return err;
2715}
2716
Vivien Didelot97299342016-07-18 20:45:30 -04002717static int mv88e6xxx_g1_setup(struct mv88e6xxx_chip *chip)
Vivien Didelot08a01262016-05-09 13:22:50 -04002718{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002719 struct dsa_switch *ds = chip->ds;
Vivien Didelotb0745e872016-05-09 13:22:53 -04002720 u32 upstream_port = dsa_upstream_port(ds);
Vivien Didelot119477b2016-05-09 13:22:51 -04002721 u16 reg;
Vivien Didelot08a01262016-05-09 13:22:50 -04002722 int err;
Vivien Didelot08a01262016-05-09 13:22:50 -04002723
Vivien Didelot119477b2016-05-09 13:22:51 -04002724 /* Enable the PHY Polling Unit if present, don't discard any packets,
2725 * and mask all interrupt sources.
2726 */
Andrew Lunndc30c352016-10-16 19:56:49 +02002727 err = mv88e6xxx_g1_read(chip, GLOBAL_CONTROL, &reg);
2728 if (err < 0)
2729 return err;
2730
2731 reg &= ~GLOBAL_CONTROL_PPU_ENABLE;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002732 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU) ||
2733 mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE))
Vivien Didelot119477b2016-05-09 13:22:51 -04002734 reg |= GLOBAL_CONTROL_PPU_ENABLE;
2735
Vivien Didelota935c052016-09-29 12:21:53 -04002736 err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL, reg);
Vivien Didelot119477b2016-05-09 13:22:51 -04002737 if (err)
2738 return err;
2739
Vivien Didelotb0745e872016-05-09 13:22:53 -04002740 /* Configure the upstream port, and configure it as the port to which
2741 * ingress and egress and ARP monitor frames are to be sent.
2742 */
2743 reg = upstream_port << GLOBAL_MONITOR_CONTROL_INGRESS_SHIFT |
2744 upstream_port << GLOBAL_MONITOR_CONTROL_EGRESS_SHIFT |
2745 upstream_port << GLOBAL_MONITOR_CONTROL_ARP_SHIFT;
Vivien Didelota935c052016-09-29 12:21:53 -04002746 err = mv88e6xxx_g1_write(chip, GLOBAL_MONITOR_CONTROL, reg);
Vivien Didelotb0745e872016-05-09 13:22:53 -04002747 if (err)
2748 return err;
2749
Vivien Didelot50484ff2016-05-09 13:22:54 -04002750 /* Disable remote management, and set the switch's DSA device number. */
Vivien Didelota935c052016-09-29 12:21:53 -04002751 err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL_2,
2752 GLOBAL_CONTROL_2_MULTIPLE_CASCADE |
2753 (ds->index & 0x1f));
Vivien Didelot50484ff2016-05-09 13:22:54 -04002754 if (err)
2755 return err;
2756
Vivien Didelotacddbd22016-07-18 20:45:39 -04002757 /* Clear all the VTU and STU entries */
2758 err = _mv88e6xxx_vtu_stu_flush(chip);
2759 if (err < 0)
2760 return err;
2761
Vivien Didelot08a01262016-05-09 13:22:50 -04002762 /* Set the default address aging time to 5 minutes, and
2763 * enable address learn messages to be sent to all message
2764 * ports.
2765 */
Vivien Didelota935c052016-09-29 12:21:53 -04002766 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_CONTROL,
2767 GLOBAL_ATU_CONTROL_LEARN2ALL);
Vivien Didelot08a01262016-05-09 13:22:50 -04002768 if (err)
2769 return err;
2770
Vivien Didelotacddbd22016-07-18 20:45:39 -04002771 err = mv88e6xxx_g1_set_age_time(chip, 300000);
2772 if (err)
Vivien Didelot97299342016-07-18 20:45:30 -04002773 return err;
2774
2775 /* Clear all ATU entries */
2776 err = _mv88e6xxx_atu_flush(chip, 0, true);
2777 if (err)
2778 return err;
2779
Vivien Didelot08a01262016-05-09 13:22:50 -04002780 /* Configure the IP ToS mapping registers. */
Vivien Didelota935c052016-09-29 12:21:53 -04002781 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_0, 0x0000);
Vivien Didelot08a01262016-05-09 13:22:50 -04002782 if (err)
2783 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002784 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_1, 0x0000);
Vivien Didelot08a01262016-05-09 13:22:50 -04002785 if (err)
2786 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002787 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_2, 0x5555);
Vivien Didelot08a01262016-05-09 13:22:50 -04002788 if (err)
2789 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002790 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_3, 0x5555);
Vivien Didelot08a01262016-05-09 13:22:50 -04002791 if (err)
2792 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002793 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_4, 0xaaaa);
Vivien Didelot08a01262016-05-09 13:22:50 -04002794 if (err)
2795 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002796 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_5, 0xaaaa);
Vivien Didelot08a01262016-05-09 13:22:50 -04002797 if (err)
2798 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002799 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_6, 0xffff);
Vivien Didelot08a01262016-05-09 13:22:50 -04002800 if (err)
2801 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002802 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_7, 0xffff);
Vivien Didelot08a01262016-05-09 13:22:50 -04002803 if (err)
2804 return err;
2805
2806 /* Configure the IEEE 802.1p priority mapping register. */
Vivien Didelota935c052016-09-29 12:21:53 -04002807 err = mv88e6xxx_g1_write(chip, GLOBAL_IEEE_PRI, 0xfa41);
Vivien Didelot08a01262016-05-09 13:22:50 -04002808 if (err)
2809 return err;
2810
Vivien Didelot97299342016-07-18 20:45:30 -04002811 /* Clear the statistics counters for all ports */
Vivien Didelota935c052016-09-29 12:21:53 -04002812 err = mv88e6xxx_g1_write(chip, GLOBAL_STATS_OP,
2813 GLOBAL_STATS_OP_FLUSH_ALL);
Vivien Didelot97299342016-07-18 20:45:30 -04002814 if (err)
2815 return err;
2816
2817 /* Wait for the flush to complete. */
2818 err = _mv88e6xxx_stats_wait(chip);
2819 if (err)
2820 return err;
2821
2822 return 0;
2823}
2824
Vivien Didelotf81ec902016-05-09 13:22:58 -04002825static int mv88e6xxx_setup(struct dsa_switch *ds)
Guenter Roeckacdaffc2015-03-26 18:36:28 -07002826{
Vivien Didelot04bed142016-08-31 18:06:13 -04002827 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot552238b2016-05-09 13:22:49 -04002828 int err;
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002829 int i;
2830
Vivien Didelotfad09c72016-06-21 12:28:20 -04002831 chip->ds = ds;
2832 ds->slave_mii_bus = chip->mdio_bus;
Vivien Didelot552238b2016-05-09 13:22:49 -04002833
Vivien Didelotfad09c72016-06-21 12:28:20 -04002834 mutex_lock(&chip->reg_lock);
Vivien Didelot552238b2016-05-09 13:22:49 -04002835
Vivien Didelot97299342016-07-18 20:45:30 -04002836 /* Setup Switch Port Registers */
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002837 for (i = 0; i < mv88e6xxx_num_ports(chip); i++) {
Vivien Didelot97299342016-07-18 20:45:30 -04002838 err = mv88e6xxx_setup_port(chip, i);
2839 if (err)
2840 goto unlock;
2841 }
2842
2843 /* Setup Switch Global 1 Registers */
2844 err = mv88e6xxx_g1_setup(chip);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002845 if (err)
2846 goto unlock;
2847
Vivien Didelot97299342016-07-18 20:45:30 -04002848 /* Setup Switch Global 2 Registers */
2849 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_GLOBAL2)) {
2850 err = mv88e6xxx_g2_setup(chip);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002851 if (err)
2852 goto unlock;
2853 }
Andrew Lunn54d792f2015-05-06 01:09:47 +02002854
Vivien Didelot6b17e862015-08-13 12:52:18 -04002855unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002856 mutex_unlock(&chip->reg_lock);
Andrew Lunndb687a52015-06-20 21:31:29 +02002857
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002858 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002859}
2860
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002861static int mv88e6xxx_set_addr(struct dsa_switch *ds, u8 *addr)
2862{
Vivien Didelot04bed142016-08-31 18:06:13 -04002863 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002864 int err;
2865
Vivien Didelotb073d4e2016-09-29 12:22:01 -04002866 if (!chip->info->ops->set_switch_mac)
2867 return -EOPNOTSUPP;
2868
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002869 mutex_lock(&chip->reg_lock);
Vivien Didelotb073d4e2016-09-29 12:22:01 -04002870 err = chip->info->ops->set_switch_mac(chip, addr);
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002871 mutex_unlock(&chip->reg_lock);
2872
2873 return err;
2874}
2875
Vivien Didelote57e5e72016-08-15 17:19:00 -04002876static int mv88e6xxx_mdio_read(struct mii_bus *bus, int phy, int reg)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002877{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002878 struct mv88e6xxx_chip *chip = bus->priv;
Vivien Didelote57e5e72016-08-15 17:19:00 -04002879 u16 val;
2880 int err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002881
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002882 if (phy >= mv88e6xxx_num_ports(chip))
Andrew Lunn158bc062016-04-28 21:24:06 -04002883 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002884
Vivien Didelotfad09c72016-06-21 12:28:20 -04002885 mutex_lock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04002886 err = mv88e6xxx_phy_read(chip, phy, reg, &val);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002887 mutex_unlock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04002888
2889 return err ? err : val;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002890}
2891
Vivien Didelote57e5e72016-08-15 17:19:00 -04002892static int mv88e6xxx_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002893{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002894 struct mv88e6xxx_chip *chip = bus->priv;
Vivien Didelote57e5e72016-08-15 17:19:00 -04002895 int err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002896
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002897 if (phy >= mv88e6xxx_num_ports(chip))
Andrew Lunn158bc062016-04-28 21:24:06 -04002898 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002899
Vivien Didelotfad09c72016-06-21 12:28:20 -04002900 mutex_lock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04002901 err = mv88e6xxx_phy_write(chip, phy, reg, val);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002902 mutex_unlock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04002903
2904 return err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002905}
2906
Vivien Didelotfad09c72016-06-21 12:28:20 -04002907static int mv88e6xxx_mdio_register(struct mv88e6xxx_chip *chip,
Andrew Lunnb516d452016-06-04 21:17:06 +02002908 struct device_node *np)
2909{
2910 static int index;
2911 struct mii_bus *bus;
2912 int err;
2913
Andrew Lunnb516d452016-06-04 21:17:06 +02002914 if (np)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002915 chip->mdio_np = of_get_child_by_name(np, "mdio");
Andrew Lunnb516d452016-06-04 21:17:06 +02002916
Vivien Didelotfad09c72016-06-21 12:28:20 -04002917 bus = devm_mdiobus_alloc(chip->dev);
Andrew Lunnb516d452016-06-04 21:17:06 +02002918 if (!bus)
2919 return -ENOMEM;
2920
Vivien Didelotfad09c72016-06-21 12:28:20 -04002921 bus->priv = (void *)chip;
Andrew Lunnb516d452016-06-04 21:17:06 +02002922 if (np) {
2923 bus->name = np->full_name;
2924 snprintf(bus->id, MII_BUS_ID_SIZE, "%s", np->full_name);
2925 } else {
2926 bus->name = "mv88e6xxx SMI";
2927 snprintf(bus->id, MII_BUS_ID_SIZE, "mv88e6xxx-%d", index++);
2928 }
2929
2930 bus->read = mv88e6xxx_mdio_read;
2931 bus->write = mv88e6xxx_mdio_write;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002932 bus->parent = chip->dev;
Andrew Lunnb516d452016-06-04 21:17:06 +02002933
Vivien Didelotfad09c72016-06-21 12:28:20 -04002934 if (chip->mdio_np)
2935 err = of_mdiobus_register(bus, chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02002936 else
2937 err = mdiobus_register(bus);
2938 if (err) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002939 dev_err(chip->dev, "Cannot register MDIO bus (%d)\n", err);
Andrew Lunnb516d452016-06-04 21:17:06 +02002940 goto out;
2941 }
Vivien Didelotfad09c72016-06-21 12:28:20 -04002942 chip->mdio_bus = bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02002943
2944 return 0;
2945
2946out:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002947 if (chip->mdio_np)
2948 of_node_put(chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02002949
2950 return err;
2951}
2952
Vivien Didelotfad09c72016-06-21 12:28:20 -04002953static void mv88e6xxx_mdio_unregister(struct mv88e6xxx_chip *chip)
Andrew Lunnb516d452016-06-04 21:17:06 +02002954
2955{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002956 struct mii_bus *bus = chip->mdio_bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02002957
2958 mdiobus_unregister(bus);
2959
Vivien Didelotfad09c72016-06-21 12:28:20 -04002960 if (chip->mdio_np)
2961 of_node_put(chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02002962}
2963
Guenter Roeckc22995c2015-07-25 09:42:28 -07002964#ifdef CONFIG_NET_DSA_HWMON
2965
2966static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
2967{
Vivien Didelot04bed142016-08-31 18:06:13 -04002968 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot9c938292016-08-15 17:19:02 -04002969 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07002970 int ret;
Guenter Roeckc22995c2015-07-25 09:42:28 -07002971
2972 *temp = 0;
2973
Vivien Didelotfad09c72016-06-21 12:28:20 -04002974 mutex_lock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07002975
Vivien Didelot9c938292016-08-15 17:19:02 -04002976 ret = mv88e6xxx_phy_write(chip, 0x0, 0x16, 0x6);
Guenter Roeckc22995c2015-07-25 09:42:28 -07002977 if (ret < 0)
2978 goto error;
2979
2980 /* Enable temperature sensor */
Vivien Didelot9c938292016-08-15 17:19:02 -04002981 ret = mv88e6xxx_phy_read(chip, 0x0, 0x1a, &val);
Guenter Roeckc22995c2015-07-25 09:42:28 -07002982 if (ret < 0)
2983 goto error;
2984
Vivien Didelot9c938292016-08-15 17:19:02 -04002985 ret = mv88e6xxx_phy_write(chip, 0x0, 0x1a, val | (1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07002986 if (ret < 0)
2987 goto error;
2988
2989 /* Wait for temperature to stabilize */
2990 usleep_range(10000, 12000);
2991
Vivien Didelot9c938292016-08-15 17:19:02 -04002992 ret = mv88e6xxx_phy_read(chip, 0x0, 0x1a, &val);
2993 if (ret < 0)
Guenter Roeckc22995c2015-07-25 09:42:28 -07002994 goto error;
Guenter Roeckc22995c2015-07-25 09:42:28 -07002995
2996 /* Disable temperature sensor */
Vivien Didelot9c938292016-08-15 17:19:02 -04002997 ret = mv88e6xxx_phy_write(chip, 0x0, 0x1a, val & ~(1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07002998 if (ret < 0)
2999 goto error;
3000
3001 *temp = ((val & 0x1f) - 5) * 5;
3002
3003error:
Vivien Didelot9c938292016-08-15 17:19:02 -04003004 mv88e6xxx_phy_write(chip, 0x0, 0x16, 0x0);
Vivien Didelotfad09c72016-06-21 12:28:20 -04003005 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003006 return ret;
3007}
3008
3009static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
3010{
Vivien Didelot04bed142016-08-31 18:06:13 -04003011 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003012 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003013 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003014 int ret;
3015
3016 *temp = 0;
3017
Vivien Didelot9c938292016-08-15 17:19:02 -04003018 mutex_lock(&chip->reg_lock);
3019 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 27, &val);
3020 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003021 if (ret < 0)
3022 return ret;
3023
Vivien Didelot9c938292016-08-15 17:19:02 -04003024 *temp = (val & 0xff) - 25;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003025
3026 return 0;
3027}
3028
Vivien Didelotf81ec902016-05-09 13:22:58 -04003029static int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003030{
Vivien Didelot04bed142016-08-31 18:06:13 -04003031 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn158bc062016-04-28 21:24:06 -04003032
Vivien Didelotfad09c72016-06-21 12:28:20 -04003033 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP))
Vivien Didelot6594f612016-05-09 13:22:42 -04003034 return -EOPNOTSUPP;
3035
Vivien Didelotfad09c72016-06-21 12:28:20 -04003036 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003037 return mv88e63xx_get_temp(ds, temp);
3038
3039 return mv88e61xx_get_temp(ds, temp);
3040}
3041
Vivien Didelotf81ec902016-05-09 13:22:58 -04003042static int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003043{
Vivien Didelot04bed142016-08-31 18:06:13 -04003044 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003045 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003046 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003047 int ret;
3048
Vivien Didelotfad09c72016-06-21 12:28:20 -04003049 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003050 return -EOPNOTSUPP;
3051
3052 *temp = 0;
3053
Vivien Didelot9c938292016-08-15 17:19:02 -04003054 mutex_lock(&chip->reg_lock);
3055 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3056 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003057 if (ret < 0)
3058 return ret;
3059
Vivien Didelot9c938292016-08-15 17:19:02 -04003060 *temp = (((val >> 8) & 0x1f) * 5) - 25;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003061
3062 return 0;
3063}
3064
Vivien Didelotf81ec902016-05-09 13:22:58 -04003065static int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003066{
Vivien Didelot04bed142016-08-31 18:06:13 -04003067 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003068 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003069 u16 val;
3070 int err;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003071
Vivien Didelotfad09c72016-06-21 12:28:20 -04003072 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003073 return -EOPNOTSUPP;
3074
Vivien Didelot9c938292016-08-15 17:19:02 -04003075 mutex_lock(&chip->reg_lock);
3076 err = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3077 if (err)
3078 goto unlock;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003079 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
Vivien Didelot9c938292016-08-15 17:19:02 -04003080 err = mv88e6xxx_phy_page_write(chip, phy, 6, 26,
3081 (val & 0xe0ff) | (temp << 8));
3082unlock:
3083 mutex_unlock(&chip->reg_lock);
3084
3085 return err;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003086}
3087
Vivien Didelotf81ec902016-05-09 13:22:58 -04003088static int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003089{
Vivien Didelot04bed142016-08-31 18:06:13 -04003090 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003091 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003092 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003093 int ret;
3094
Vivien Didelotfad09c72016-06-21 12:28:20 -04003095 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003096 return -EOPNOTSUPP;
3097
3098 *alarm = false;
3099
Vivien Didelot9c938292016-08-15 17:19:02 -04003100 mutex_lock(&chip->reg_lock);
3101 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3102 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003103 if (ret < 0)
3104 return ret;
3105
Vivien Didelot9c938292016-08-15 17:19:02 -04003106 *alarm = !!(val & 0x40);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003107
3108 return 0;
3109}
3110#endif /* CONFIG_NET_DSA_HWMON */
3111
Vivien Didelot855b1932016-07-20 18:18:35 -04003112static int mv88e6xxx_get_eeprom_len(struct dsa_switch *ds)
3113{
Vivien Didelot04bed142016-08-31 18:06:13 -04003114 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot855b1932016-07-20 18:18:35 -04003115
3116 return chip->eeprom_len;
3117}
3118
Vivien Didelot855b1932016-07-20 18:18:35 -04003119static int mv88e6xxx_get_eeprom(struct dsa_switch *ds,
3120 struct ethtool_eeprom *eeprom, u8 *data)
3121{
Vivien Didelot04bed142016-08-31 18:06:13 -04003122 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot855b1932016-07-20 18:18:35 -04003123 int err;
3124
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003125 if (!chip->info->ops->get_eeprom)
3126 return -EOPNOTSUPP;
3127
Vivien Didelot855b1932016-07-20 18:18:35 -04003128 mutex_lock(&chip->reg_lock);
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003129 err = chip->info->ops->get_eeprom(chip, eeprom, data);
Vivien Didelot855b1932016-07-20 18:18:35 -04003130 mutex_unlock(&chip->reg_lock);
3131
3132 if (err)
3133 return err;
3134
3135 eeprom->magic = 0xc3ec4951;
3136
3137 return 0;
3138}
3139
Vivien Didelot855b1932016-07-20 18:18:35 -04003140static int mv88e6xxx_set_eeprom(struct dsa_switch *ds,
3141 struct ethtool_eeprom *eeprom, u8 *data)
3142{
Vivien Didelot04bed142016-08-31 18:06:13 -04003143 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot855b1932016-07-20 18:18:35 -04003144 int err;
3145
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003146 if (!chip->info->ops->set_eeprom)
3147 return -EOPNOTSUPP;
3148
Vivien Didelot855b1932016-07-20 18:18:35 -04003149 if (eeprom->magic != 0xc3ec4951)
3150 return -EINVAL;
3151
3152 mutex_lock(&chip->reg_lock);
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003153 err = chip->info->ops->set_eeprom(chip, eeprom, data);
Vivien Didelot855b1932016-07-20 18:18:35 -04003154 mutex_unlock(&chip->reg_lock);
3155
3156 return err;
3157}
3158
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003159static const struct mv88e6xxx_ops mv88e6085_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003160 .set_switch_mac = mv88e6xxx_g1_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003161 .phy_read = mv88e6xxx_phy_ppu_read,
3162 .phy_write = mv88e6xxx_phy_ppu_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003163 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003164};
3165
3166static const struct mv88e6xxx_ops mv88e6095_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003167 .set_switch_mac = mv88e6xxx_g1_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003168 .phy_read = mv88e6xxx_phy_ppu_read,
3169 .phy_write = mv88e6xxx_phy_ppu_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003170 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003171};
3172
3173static const struct mv88e6xxx_ops mv88e6123_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003174 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003175 .phy_read = mv88e6xxx_read,
3176 .phy_write = mv88e6xxx_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003177 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003178};
3179
3180static const struct mv88e6xxx_ops mv88e6131_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003181 .set_switch_mac = mv88e6xxx_g1_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003182 .phy_read = mv88e6xxx_phy_ppu_read,
3183 .phy_write = mv88e6xxx_phy_ppu_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003184 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003185};
3186
3187static const struct mv88e6xxx_ops mv88e6161_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003188 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003189 .phy_read = mv88e6xxx_read,
3190 .phy_write = mv88e6xxx_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003191 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003192};
3193
3194static const struct mv88e6xxx_ops mv88e6165_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 Didelotb3469dd2016-09-29 12:22:00 -04003199};
3200
3201static const struct mv88e6xxx_ops mv88e6171_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003202 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003203 .phy_read = mv88e6xxx_g2_smi_phy_read,
3204 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003205 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003206};
3207
3208static const struct mv88e6xxx_ops mv88e6172_ops = {
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003209 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3210 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003211 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003212 .phy_read = mv88e6xxx_g2_smi_phy_read,
3213 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003214 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003215};
3216
3217static const struct mv88e6xxx_ops mv88e6175_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003218 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003219 .phy_read = mv88e6xxx_g2_smi_phy_read,
3220 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003221 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003222};
3223
3224static const struct mv88e6xxx_ops mv88e6176_ops = {
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003225 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3226 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003227 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003228 .phy_read = mv88e6xxx_g2_smi_phy_read,
3229 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003230 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003231};
3232
3233static const struct mv88e6xxx_ops mv88e6185_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003234 .set_switch_mac = mv88e6xxx_g1_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003235 .phy_read = mv88e6xxx_phy_ppu_read,
3236 .phy_write = mv88e6xxx_phy_ppu_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003237 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003238};
3239
3240static const struct mv88e6xxx_ops mv88e6240_ops = {
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003241 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3242 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003243 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003244 .phy_read = mv88e6xxx_g2_smi_phy_read,
3245 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003246 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003247};
3248
3249static const struct mv88e6xxx_ops mv88e6320_ops = {
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003250 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3251 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003252 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003253 .phy_read = mv88e6xxx_g2_smi_phy_read,
3254 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003255 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003256};
3257
3258static const struct mv88e6xxx_ops mv88e6321_ops = {
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003259 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3260 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003261 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003262 .phy_read = mv88e6xxx_g2_smi_phy_read,
3263 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003264 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003265};
3266
3267static const struct mv88e6xxx_ops mv88e6350_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003268 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003269 .phy_read = mv88e6xxx_g2_smi_phy_read,
3270 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003271 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003272};
3273
3274static const struct mv88e6xxx_ops mv88e6351_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003275 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003276 .phy_read = mv88e6xxx_g2_smi_phy_read,
3277 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003278 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003279};
3280
3281static const struct mv88e6xxx_ops mv88e6352_ops = {
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003282 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3283 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003284 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003285 .phy_read = mv88e6xxx_g2_smi_phy_read,
3286 .phy_write = mv88e6xxx_g2_smi_phy_write,
Vivien Didelot08ef7f12016-11-04 03:23:32 +01003287 .port_set_link = mv88e6xxx_port_set_link,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003288};
3289
Vivien Didelotf81ec902016-05-09 13:22:58 -04003290static const struct mv88e6xxx_info mv88e6xxx_table[] = {
3291 [MV88E6085] = {
3292 .prod_num = PORT_SWITCH_ID_PROD_NUM_6085,
3293 .family = MV88E6XXX_FAMILY_6097,
3294 .name = "Marvell 88E6085",
3295 .num_databases = 4096,
3296 .num_ports = 10,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003297 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003298 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003299 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003300 .g1_irqs = 8,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003301 .flags = MV88E6XXX_FLAGS_FAMILY_6097,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003302 .ops = &mv88e6085_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003303 },
3304
3305 [MV88E6095] = {
3306 .prod_num = PORT_SWITCH_ID_PROD_NUM_6095,
3307 .family = MV88E6XXX_FAMILY_6095,
3308 .name = "Marvell 88E6095/88E6095F",
3309 .num_databases = 256,
3310 .num_ports = 11,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003311 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003312 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003313 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003314 .g1_irqs = 8,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003315 .flags = MV88E6XXX_FLAGS_FAMILY_6095,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003316 .ops = &mv88e6095_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003317 },
3318
3319 [MV88E6123] = {
3320 .prod_num = PORT_SWITCH_ID_PROD_NUM_6123,
3321 .family = MV88E6XXX_FAMILY_6165,
3322 .name = "Marvell 88E6123",
3323 .num_databases = 4096,
3324 .num_ports = 3,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003325 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003326 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003327 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003328 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003329 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003330 .ops = &mv88e6123_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003331 },
3332
3333 [MV88E6131] = {
3334 .prod_num = PORT_SWITCH_ID_PROD_NUM_6131,
3335 .family = MV88E6XXX_FAMILY_6185,
3336 .name = "Marvell 88E6131",
3337 .num_databases = 256,
3338 .num_ports = 8,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003339 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003340 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003341 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003342 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003343 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003344 .ops = &mv88e6131_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003345 },
3346
3347 [MV88E6161] = {
3348 .prod_num = PORT_SWITCH_ID_PROD_NUM_6161,
3349 .family = MV88E6XXX_FAMILY_6165,
3350 .name = "Marvell 88E6161",
3351 .num_databases = 4096,
3352 .num_ports = 6,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003353 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003354 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003355 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003356 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003357 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003358 .ops = &mv88e6161_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003359 },
3360
3361 [MV88E6165] = {
3362 .prod_num = PORT_SWITCH_ID_PROD_NUM_6165,
3363 .family = MV88E6XXX_FAMILY_6165,
3364 .name = "Marvell 88E6165",
3365 .num_databases = 4096,
3366 .num_ports = 6,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003367 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003368 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003369 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003370 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003371 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003372 .ops = &mv88e6165_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003373 },
3374
3375 [MV88E6171] = {
3376 .prod_num = PORT_SWITCH_ID_PROD_NUM_6171,
3377 .family = MV88E6XXX_FAMILY_6351,
3378 .name = "Marvell 88E6171",
3379 .num_databases = 4096,
3380 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003381 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003382 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003383 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003384 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003385 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003386 .ops = &mv88e6171_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003387 },
3388
3389 [MV88E6172] = {
3390 .prod_num = PORT_SWITCH_ID_PROD_NUM_6172,
3391 .family = MV88E6XXX_FAMILY_6352,
3392 .name = "Marvell 88E6172",
3393 .num_databases = 4096,
3394 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003395 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003396 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003397 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003398 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003399 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003400 .ops = &mv88e6172_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003401 },
3402
3403 [MV88E6175] = {
3404 .prod_num = PORT_SWITCH_ID_PROD_NUM_6175,
3405 .family = MV88E6XXX_FAMILY_6351,
3406 .name = "Marvell 88E6175",
3407 .num_databases = 4096,
3408 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003409 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003410 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003411 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003412 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003413 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003414 .ops = &mv88e6175_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003415 },
3416
3417 [MV88E6176] = {
3418 .prod_num = PORT_SWITCH_ID_PROD_NUM_6176,
3419 .family = MV88E6XXX_FAMILY_6352,
3420 .name = "Marvell 88E6176",
3421 .num_databases = 4096,
3422 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003423 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003424 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003425 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003426 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003427 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003428 .ops = &mv88e6176_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003429 },
3430
3431 [MV88E6185] = {
3432 .prod_num = PORT_SWITCH_ID_PROD_NUM_6185,
3433 .family = MV88E6XXX_FAMILY_6185,
3434 .name = "Marvell 88E6185",
3435 .num_databases = 256,
3436 .num_ports = 10,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003437 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003438 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003439 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003440 .g1_irqs = 8,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003441 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003442 .ops = &mv88e6185_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003443 },
3444
3445 [MV88E6240] = {
3446 .prod_num = PORT_SWITCH_ID_PROD_NUM_6240,
3447 .family = MV88E6XXX_FAMILY_6352,
3448 .name = "Marvell 88E6240",
3449 .num_databases = 4096,
3450 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003451 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003452 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003453 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003454 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003455 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003456 .ops = &mv88e6240_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003457 },
3458
3459 [MV88E6320] = {
3460 .prod_num = PORT_SWITCH_ID_PROD_NUM_6320,
3461 .family = MV88E6XXX_FAMILY_6320,
3462 .name = "Marvell 88E6320",
3463 .num_databases = 4096,
3464 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003465 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003466 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003467 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003468 .g1_irqs = 8,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003469 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003470 .ops = &mv88e6320_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003471 },
3472
3473 [MV88E6321] = {
3474 .prod_num = PORT_SWITCH_ID_PROD_NUM_6321,
3475 .family = MV88E6XXX_FAMILY_6320,
3476 .name = "Marvell 88E6321",
3477 .num_databases = 4096,
3478 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003479 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003480 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003481 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003482 .g1_irqs = 8,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003483 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003484 .ops = &mv88e6321_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003485 },
3486
3487 [MV88E6350] = {
3488 .prod_num = PORT_SWITCH_ID_PROD_NUM_6350,
3489 .family = MV88E6XXX_FAMILY_6351,
3490 .name = "Marvell 88E6350",
3491 .num_databases = 4096,
3492 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003493 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003494 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003495 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003496 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003497 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003498 .ops = &mv88e6350_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003499 },
3500
3501 [MV88E6351] = {
3502 .prod_num = PORT_SWITCH_ID_PROD_NUM_6351,
3503 .family = MV88E6XXX_FAMILY_6351,
3504 .name = "Marvell 88E6351",
3505 .num_databases = 4096,
3506 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003507 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003508 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003509 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003510 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003511 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003512 .ops = &mv88e6351_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003513 },
3514
3515 [MV88E6352] = {
3516 .prod_num = PORT_SWITCH_ID_PROD_NUM_6352,
3517 .family = MV88E6XXX_FAMILY_6352,
3518 .name = "Marvell 88E6352",
3519 .num_databases = 4096,
3520 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003521 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003522 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003523 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003524 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003525 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003526 .ops = &mv88e6352_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003527 },
3528};
3529
Vivien Didelot5f7c0362016-06-20 13:14:04 -04003530static const struct mv88e6xxx_info *mv88e6xxx_lookup_info(unsigned int prod_num)
Vivien Didelotb9b37712015-10-30 19:39:48 -04003531{
Vivien Didelota439c062016-04-17 13:23:58 -04003532 int i;
Vivien Didelotb9b37712015-10-30 19:39:48 -04003533
Vivien Didelot5f7c0362016-06-20 13:14:04 -04003534 for (i = 0; i < ARRAY_SIZE(mv88e6xxx_table); ++i)
3535 if (mv88e6xxx_table[i].prod_num == prod_num)
3536 return &mv88e6xxx_table[i];
Vivien Didelotb9b37712015-10-30 19:39:48 -04003537
Vivien Didelotb9b37712015-10-30 19:39:48 -04003538 return NULL;
3539}
3540
Vivien Didelotfad09c72016-06-21 12:28:20 -04003541static int mv88e6xxx_detect(struct mv88e6xxx_chip *chip)
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003542{
3543 const struct mv88e6xxx_info *info;
Vivien Didelot8f6345b2016-07-20 18:18:36 -04003544 unsigned int prod_num, rev;
3545 u16 id;
3546 int err;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003547
Vivien Didelot8f6345b2016-07-20 18:18:36 -04003548 mutex_lock(&chip->reg_lock);
3549 err = mv88e6xxx_port_read(chip, 0, PORT_SWITCH_ID, &id);
3550 mutex_unlock(&chip->reg_lock);
3551 if (err)
3552 return err;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003553
3554 prod_num = (id & 0xfff0) >> 4;
3555 rev = id & 0x000f;
3556
3557 info = mv88e6xxx_lookup_info(prod_num);
3558 if (!info)
3559 return -ENODEV;
3560
Vivien Didelotcaac8542016-06-20 13:14:09 -04003561 /* Update the compatible info with the probed one */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003562 chip->info = info;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003563
Vivien Didelotca070c12016-09-02 14:45:34 -04003564 err = mv88e6xxx_g2_require(chip);
3565 if (err)
3566 return err;
3567
Vivien Didelotfad09c72016-06-21 12:28:20 -04003568 dev_info(chip->dev, "switch 0x%x detected: %s, revision %u\n",
3569 chip->info->prod_num, chip->info->name, rev);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003570
3571 return 0;
3572}
3573
Vivien Didelotfad09c72016-06-21 12:28:20 -04003574static struct mv88e6xxx_chip *mv88e6xxx_alloc_chip(struct device *dev)
Vivien Didelot469d7292016-06-20 13:14:06 -04003575{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003576 struct mv88e6xxx_chip *chip;
Vivien Didelot469d7292016-06-20 13:14:06 -04003577
Vivien Didelotfad09c72016-06-21 12:28:20 -04003578 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
3579 if (!chip)
Vivien Didelot469d7292016-06-20 13:14:06 -04003580 return NULL;
3581
Vivien Didelotfad09c72016-06-21 12:28:20 -04003582 chip->dev = dev;
Vivien Didelot469d7292016-06-20 13:14:06 -04003583
Vivien Didelotfad09c72016-06-21 12:28:20 -04003584 mutex_init(&chip->reg_lock);
Vivien Didelot469d7292016-06-20 13:14:06 -04003585
Vivien Didelotfad09c72016-06-21 12:28:20 -04003586 return chip;
Vivien Didelot469d7292016-06-20 13:14:06 -04003587}
3588
Vivien Didelote57e5e72016-08-15 17:19:00 -04003589static void mv88e6xxx_phy_init(struct mv88e6xxx_chip *chip)
3590{
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003591 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU))
Vivien Didelote57e5e72016-08-15 17:19:00 -04003592 mv88e6xxx_ppu_state_init(chip);
Vivien Didelote57e5e72016-08-15 17:19:00 -04003593}
3594
Andrew Lunn930188c2016-08-22 16:01:03 +02003595static void mv88e6xxx_phy_destroy(struct mv88e6xxx_chip *chip)
3596{
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003597 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU))
Andrew Lunn930188c2016-08-22 16:01:03 +02003598 mv88e6xxx_ppu_state_destroy(chip);
Andrew Lunn930188c2016-08-22 16:01:03 +02003599}
3600
Vivien Didelotfad09c72016-06-21 12:28:20 -04003601static int mv88e6xxx_smi_init(struct mv88e6xxx_chip *chip,
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003602 struct mii_bus *bus, int sw_addr)
3603{
3604 /* ADDR[0] pin is unavailable externally and considered zero */
3605 if (sw_addr & 0x1)
3606 return -EINVAL;
3607
Vivien Didelot914b32f2016-06-20 13:14:11 -04003608 if (sw_addr == 0)
Vivien Didelotfad09c72016-06-21 12:28:20 -04003609 chip->smi_ops = &mv88e6xxx_smi_single_chip_ops;
Vivien Didelota0ffff22016-08-15 17:18:58 -04003610 else if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_MULTI_CHIP))
Vivien Didelotfad09c72016-06-21 12:28:20 -04003611 chip->smi_ops = &mv88e6xxx_smi_multi_chip_ops;
Vivien Didelot914b32f2016-06-20 13:14:11 -04003612 else
3613 return -EINVAL;
3614
Vivien Didelotfad09c72016-06-21 12:28:20 -04003615 chip->bus = bus;
3616 chip->sw_addr = sw_addr;
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003617
3618 return 0;
3619}
3620
Andrew Lunn7b314362016-08-22 16:01:01 +02003621static enum dsa_tag_protocol mv88e6xxx_get_tag_protocol(struct dsa_switch *ds)
3622{
Vivien Didelot04bed142016-08-31 18:06:13 -04003623 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02003624
3625 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA))
3626 return DSA_TAG_PROTO_EDSA;
3627
3628 return DSA_TAG_PROTO_DSA;
Andrew Lunn7b314362016-08-22 16:01:01 +02003629}
3630
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02003631static const char *mv88e6xxx_drv_probe(struct device *dsa_dev,
3632 struct device *host_dev, int sw_addr,
3633 void **priv)
Andrew Lunna77d43f2016-04-13 02:40:42 +02003634{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003635 struct mv88e6xxx_chip *chip;
Vivien Didelota439c062016-04-17 13:23:58 -04003636 struct mii_bus *bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003637 int err;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003638
Vivien Didelota439c062016-04-17 13:23:58 -04003639 bus = dsa_host_dev_to_mii_bus(host_dev);
Andrew Lunnc1569132016-04-13 02:40:45 +02003640 if (!bus)
3641 return NULL;
3642
Vivien Didelotfad09c72016-06-21 12:28:20 -04003643 chip = mv88e6xxx_alloc_chip(dsa_dev);
3644 if (!chip)
Vivien Didelot469d7292016-06-20 13:14:06 -04003645 return NULL;
3646
Vivien Didelotcaac8542016-06-20 13:14:09 -04003647 /* Legacy SMI probing will only support chips similar to 88E6085 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003648 chip->info = &mv88e6xxx_table[MV88E6085];
Vivien Didelotcaac8542016-06-20 13:14:09 -04003649
Vivien Didelotfad09c72016-06-21 12:28:20 -04003650 err = mv88e6xxx_smi_init(chip, bus, sw_addr);
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003651 if (err)
3652 goto free;
3653
Vivien Didelotfad09c72016-06-21 12:28:20 -04003654 err = mv88e6xxx_detect(chip);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003655 if (err)
Vivien Didelot469d7292016-06-20 13:14:06 -04003656 goto free;
Vivien Didelota439c062016-04-17 13:23:58 -04003657
Andrew Lunndc30c352016-10-16 19:56:49 +02003658 mutex_lock(&chip->reg_lock);
3659 err = mv88e6xxx_switch_reset(chip);
3660 mutex_unlock(&chip->reg_lock);
3661 if (err)
3662 goto free;
3663
Vivien Didelote57e5e72016-08-15 17:19:00 -04003664 mv88e6xxx_phy_init(chip);
3665
Vivien Didelotfad09c72016-06-21 12:28:20 -04003666 err = mv88e6xxx_mdio_register(chip, NULL);
Andrew Lunnb516d452016-06-04 21:17:06 +02003667 if (err)
Vivien Didelot469d7292016-06-20 13:14:06 -04003668 goto free;
Andrew Lunnb516d452016-06-04 21:17:06 +02003669
Vivien Didelotfad09c72016-06-21 12:28:20 -04003670 *priv = chip;
Vivien Didelota439c062016-04-17 13:23:58 -04003671
Vivien Didelotfad09c72016-06-21 12:28:20 -04003672 return chip->info->name;
Vivien Didelot469d7292016-06-20 13:14:06 -04003673free:
Vivien Didelotfad09c72016-06-21 12:28:20 -04003674 devm_kfree(dsa_dev, chip);
Vivien Didelot469d7292016-06-20 13:14:06 -04003675
3676 return NULL;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003677}
3678
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003679static int mv88e6xxx_port_mdb_prepare(struct dsa_switch *ds, int port,
3680 const struct switchdev_obj_port_mdb *mdb,
3681 struct switchdev_trans *trans)
3682{
3683 /* We don't need any dynamic resource from the kernel (yet),
3684 * so skip the prepare phase.
3685 */
3686
3687 return 0;
3688}
3689
3690static void mv88e6xxx_port_mdb_add(struct dsa_switch *ds, int port,
3691 const struct switchdev_obj_port_mdb *mdb,
3692 struct switchdev_trans *trans)
3693{
Vivien Didelot04bed142016-08-31 18:06:13 -04003694 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003695
3696 mutex_lock(&chip->reg_lock);
3697 if (mv88e6xxx_port_db_load_purge(chip, port, mdb->addr, mdb->vid,
3698 GLOBAL_ATU_DATA_STATE_MC_STATIC))
3699 netdev_err(ds->ports[port].netdev, "failed to load multicast MAC address\n");
3700 mutex_unlock(&chip->reg_lock);
3701}
3702
3703static int mv88e6xxx_port_mdb_del(struct dsa_switch *ds, int port,
3704 const struct switchdev_obj_port_mdb *mdb)
3705{
Vivien Didelot04bed142016-08-31 18:06:13 -04003706 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003707 int err;
3708
3709 mutex_lock(&chip->reg_lock);
3710 err = mv88e6xxx_port_db_load_purge(chip, port, mdb->addr, mdb->vid,
3711 GLOBAL_ATU_DATA_STATE_UNUSED);
3712 mutex_unlock(&chip->reg_lock);
3713
3714 return err;
3715}
3716
3717static int mv88e6xxx_port_mdb_dump(struct dsa_switch *ds, int port,
3718 struct switchdev_obj_port_mdb *mdb,
3719 int (*cb)(struct switchdev_obj *obj))
3720{
Vivien Didelot04bed142016-08-31 18:06:13 -04003721 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003722 int err;
3723
3724 mutex_lock(&chip->reg_lock);
3725 err = mv88e6xxx_port_db_dump(chip, port, &mdb->obj, cb);
3726 mutex_unlock(&chip->reg_lock);
3727
3728 return err;
3729}
3730
Vivien Didelot9d490b42016-08-23 12:38:56 -04003731static struct dsa_switch_ops mv88e6xxx_switch_ops = {
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02003732 .probe = mv88e6xxx_drv_probe,
Andrew Lunn7b314362016-08-22 16:01:01 +02003733 .get_tag_protocol = mv88e6xxx_get_tag_protocol,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003734 .setup = mv88e6xxx_setup,
3735 .set_addr = mv88e6xxx_set_addr,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003736 .adjust_link = mv88e6xxx_adjust_link,
3737 .get_strings = mv88e6xxx_get_strings,
3738 .get_ethtool_stats = mv88e6xxx_get_ethtool_stats,
3739 .get_sset_count = mv88e6xxx_get_sset_count,
3740 .set_eee = mv88e6xxx_set_eee,
3741 .get_eee = mv88e6xxx_get_eee,
3742#ifdef CONFIG_NET_DSA_HWMON
3743 .get_temp = mv88e6xxx_get_temp,
3744 .get_temp_limit = mv88e6xxx_get_temp_limit,
3745 .set_temp_limit = mv88e6xxx_set_temp_limit,
3746 .get_temp_alarm = mv88e6xxx_get_temp_alarm,
3747#endif
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003748 .get_eeprom_len = mv88e6xxx_get_eeprom_len,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003749 .get_eeprom = mv88e6xxx_get_eeprom,
3750 .set_eeprom = mv88e6xxx_set_eeprom,
3751 .get_regs_len = mv88e6xxx_get_regs_len,
3752 .get_regs = mv88e6xxx_get_regs,
Vivien Didelot2cfcd962016-07-18 20:45:40 -04003753 .set_ageing_time = mv88e6xxx_set_ageing_time,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003754 .port_bridge_join = mv88e6xxx_port_bridge_join,
3755 .port_bridge_leave = mv88e6xxx_port_bridge_leave,
3756 .port_stp_state_set = mv88e6xxx_port_stp_state_set,
Vivien Didelot749efcb2016-09-22 16:49:24 -04003757 .port_fast_age = mv88e6xxx_port_fast_age,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003758 .port_vlan_filtering = mv88e6xxx_port_vlan_filtering,
3759 .port_vlan_prepare = mv88e6xxx_port_vlan_prepare,
3760 .port_vlan_add = mv88e6xxx_port_vlan_add,
3761 .port_vlan_del = mv88e6xxx_port_vlan_del,
3762 .port_vlan_dump = mv88e6xxx_port_vlan_dump,
3763 .port_fdb_prepare = mv88e6xxx_port_fdb_prepare,
3764 .port_fdb_add = mv88e6xxx_port_fdb_add,
3765 .port_fdb_del = mv88e6xxx_port_fdb_del,
3766 .port_fdb_dump = mv88e6xxx_port_fdb_dump,
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003767 .port_mdb_prepare = mv88e6xxx_port_mdb_prepare,
3768 .port_mdb_add = mv88e6xxx_port_mdb_add,
3769 .port_mdb_del = mv88e6xxx_port_mdb_del,
3770 .port_mdb_dump = mv88e6xxx_port_mdb_dump,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003771};
3772
Vivien Didelotfad09c72016-06-21 12:28:20 -04003773static int mv88e6xxx_register_switch(struct mv88e6xxx_chip *chip,
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003774 struct device_node *np)
3775{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003776 struct device *dev = chip->dev;
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003777 struct dsa_switch *ds;
3778
3779 ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
3780 if (!ds)
3781 return -ENOMEM;
3782
3783 ds->dev = dev;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003784 ds->priv = chip;
Vivien Didelot9d490b42016-08-23 12:38:56 -04003785 ds->ops = &mv88e6xxx_switch_ops;
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003786
3787 dev_set_drvdata(dev, ds);
3788
3789 return dsa_register_switch(ds, np);
3790}
3791
Vivien Didelotfad09c72016-06-21 12:28:20 -04003792static void mv88e6xxx_unregister_switch(struct mv88e6xxx_chip *chip)
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003793{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003794 dsa_unregister_switch(chip->ds);
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003795}
3796
Vivien Didelot57d32312016-06-20 13:13:58 -04003797static int mv88e6xxx_probe(struct mdio_device *mdiodev)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003798{
3799 struct device *dev = &mdiodev->dev;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003800 struct device_node *np = dev->of_node;
Vivien Didelotcaac8542016-06-20 13:14:09 -04003801 const struct mv88e6xxx_info *compat_info;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003802 struct mv88e6xxx_chip *chip;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003803 u32 eeprom_len;
Andrew Lunn52638f72016-05-10 23:27:22 +02003804 int err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003805
Vivien Didelotcaac8542016-06-20 13:14:09 -04003806 compat_info = of_device_get_match_data(dev);
3807 if (!compat_info)
3808 return -EINVAL;
3809
Vivien Didelotfad09c72016-06-21 12:28:20 -04003810 chip = mv88e6xxx_alloc_chip(dev);
3811 if (!chip)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003812 return -ENOMEM;
3813
Vivien Didelotfad09c72016-06-21 12:28:20 -04003814 chip->info = compat_info;
Vivien Didelotcaac8542016-06-20 13:14:09 -04003815
Vivien Didelotfad09c72016-06-21 12:28:20 -04003816 err = mv88e6xxx_smi_init(chip, mdiodev->bus, mdiodev->addr);
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003817 if (err)
3818 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003819
Vivien Didelotfad09c72016-06-21 12:28:20 -04003820 err = mv88e6xxx_detect(chip);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003821 if (err)
3822 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003823
Vivien Didelote57e5e72016-08-15 17:19:00 -04003824 mv88e6xxx_phy_init(chip);
3825
Vivien Didelotfad09c72016-06-21 12:28:20 -04003826 chip->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_ASIS);
3827 if (IS_ERR(chip->reset))
3828 return PTR_ERR(chip->reset);
Andrew Lunn52638f72016-05-10 23:27:22 +02003829
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003830 if (chip->info->ops->get_eeprom &&
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003831 !of_property_read_u32(np, "eeprom-length", &eeprom_len))
Vivien Didelotfad09c72016-06-21 12:28:20 -04003832 chip->eeprom_len = eeprom_len;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003833
Andrew Lunndc30c352016-10-16 19:56:49 +02003834 mutex_lock(&chip->reg_lock);
3835 err = mv88e6xxx_switch_reset(chip);
3836 mutex_unlock(&chip->reg_lock);
Andrew Lunnb516d452016-06-04 21:17:06 +02003837 if (err)
Andrew Lunndc30c352016-10-16 19:56:49 +02003838 goto out;
Andrew Lunnb516d452016-06-04 21:17:06 +02003839
Andrew Lunndc30c352016-10-16 19:56:49 +02003840 chip->irq = of_irq_get(np, 0);
3841 if (chip->irq == -EPROBE_DEFER) {
3842 err = chip->irq;
3843 goto out;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02003844 }
3845
Andrew Lunndc30c352016-10-16 19:56:49 +02003846 if (chip->irq > 0) {
3847 /* Has to be performed before the MDIO bus is created,
3848 * because the PHYs will link there interrupts to these
3849 * interrupt controllers
3850 */
3851 mutex_lock(&chip->reg_lock);
3852 err = mv88e6xxx_g1_irq_setup(chip);
3853 mutex_unlock(&chip->reg_lock);
3854
3855 if (err)
3856 goto out;
3857
3858 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_INT)) {
3859 err = mv88e6xxx_g2_irq_setup(chip);
3860 if (err)
3861 goto out_g1_irq;
3862 }
3863 }
3864
3865 err = mv88e6xxx_mdio_register(chip, np);
3866 if (err)
3867 goto out_g2_irq;
3868
3869 err = mv88e6xxx_register_switch(chip, np);
3870 if (err)
3871 goto out_mdio;
3872
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003873 return 0;
Andrew Lunndc30c352016-10-16 19:56:49 +02003874
3875out_mdio:
3876 mv88e6xxx_mdio_unregister(chip);
3877out_g2_irq:
3878 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_INT))
3879 mv88e6xxx_g2_irq_free(chip);
3880out_g1_irq:
3881 mv88e6xxx_g1_irq_free(chip);
3882out:
3883 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003884}
3885
3886static void mv88e6xxx_remove(struct mdio_device *mdiodev)
3887{
3888 struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev);
Vivien Didelot04bed142016-08-31 18:06:13 -04003889 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003890
Andrew Lunn930188c2016-08-22 16:01:03 +02003891 mv88e6xxx_phy_destroy(chip);
Vivien Didelotfad09c72016-06-21 12:28:20 -04003892 mv88e6xxx_unregister_switch(chip);
3893 mv88e6xxx_mdio_unregister(chip);
Andrew Lunndc30c352016-10-16 19:56:49 +02003894
3895 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_INT))
3896 mv88e6xxx_g2_irq_free(chip);
3897 mv88e6xxx_g1_irq_free(chip);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003898}
3899
3900static const struct of_device_id mv88e6xxx_of_match[] = {
Vivien Didelotcaac8542016-06-20 13:14:09 -04003901 {
3902 .compatible = "marvell,mv88e6085",
3903 .data = &mv88e6xxx_table[MV88E6085],
3904 },
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003905 { /* sentinel */ },
3906};
3907
3908MODULE_DEVICE_TABLE(of, mv88e6xxx_of_match);
3909
3910static struct mdio_driver mv88e6xxx_driver = {
3911 .probe = mv88e6xxx_probe,
3912 .remove = mv88e6xxx_remove,
3913 .mdiodrv.driver = {
3914 .name = "mv88e6085",
3915 .of_match_table = mv88e6xxx_of_match,
3916 },
3917};
3918
Ben Hutchings98e67302011-11-25 14:36:19 +00003919static int __init mv88e6xxx_init(void)
3920{
Vivien Didelot9d490b42016-08-23 12:38:56 -04003921 register_switch_driver(&mv88e6xxx_switch_ops);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003922 return mdio_driver_register(&mv88e6xxx_driver);
Ben Hutchings98e67302011-11-25 14:36:19 +00003923}
3924module_init(mv88e6xxx_init);
3925
3926static void __exit mv88e6xxx_cleanup(void)
3927{
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003928 mdio_driver_unregister(&mv88e6xxx_driver);
Vivien Didelot9d490b42016-08-23 12:38:56 -04003929 unregister_switch_driver(&mv88e6xxx_switch_ops);
Ben Hutchings98e67302011-11-25 14:36:19 +00003930}
3931module_exit(mv88e6xxx_cleanup);
Ben Hutchings3d825ed2011-11-25 14:37:16 +00003932
3933MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
3934MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
3935MODULE_LICENSE("GPL");