blob: b32b2425606562fb8a201f8973a2dbe783f8e8c6 [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 Didelot2d9deae2016-03-07 18:24:17 -05001218static const char * const mv88e6xxx_port_state_names[] = {
1219 [PORT_CONTROL_STATE_DISABLED] = "Disabled",
1220 [PORT_CONTROL_STATE_BLOCKING] = "Blocking/Listening",
1221 [PORT_CONTROL_STATE_LEARNING] = "Learning",
1222 [PORT_CONTROL_STATE_FORWARDING] = "Forwarding",
1223};
1224
Vivien Didelotfad09c72016-06-21 12:28:20 -04001225static int _mv88e6xxx_port_state(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001226 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001227{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001228 struct dsa_switch *ds = chip->ds;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001229 u16 reg;
1230 int err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001231 u8 oldstate;
1232
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001233 err = mv88e6xxx_port_read(chip, port, PORT_CONTROL, &reg);
1234 if (err)
1235 return err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001236
Andrew Lunncca8b132015-04-02 04:06:39 +02001237 oldstate = reg & PORT_CONTROL_STATE_MASK;
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001238
Vivien Didelot749efcb2016-09-22 16:49:24 -04001239 reg &= ~PORT_CONTROL_STATE_MASK;
1240 reg |= state;
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001241
Vivien Didelot749efcb2016-09-22 16:49:24 -04001242 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL, reg);
1243 if (err)
1244 return err;
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001245
Vivien Didelot749efcb2016-09-22 16:49:24 -04001246 netdev_dbg(ds->ports[port].netdev, "PortState %s (was %s)\n",
1247 mv88e6xxx_port_state_names[state],
1248 mv88e6xxx_port_state_names[oldstate]);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001249
Vivien Didelot749efcb2016-09-22 16:49:24 -04001250 return 0;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001251}
1252
Vivien Didelotfad09c72016-06-21 12:28:20 -04001253static int _mv88e6xxx_port_based_vlan_map(struct mv88e6xxx_chip *chip, int port)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001254{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001255 struct net_device *bridge = chip->ports[port].bridge_dev;
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001256 const u16 mask = (1 << mv88e6xxx_num_ports(chip)) - 1;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001257 struct dsa_switch *ds = chip->ds;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001258 u16 output_ports = 0;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001259 u16 reg;
1260 int err;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001261 int i;
1262
1263 /* allow CPU port or DSA link(s) to send frames to every port */
1264 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
1265 output_ports = mask;
1266 } else {
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001267 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001268 /* allow sending frames to every group member */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001269 if (bridge && chip->ports[i].bridge_dev == bridge)
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001270 output_ports |= BIT(i);
1271
1272 /* allow sending frames to CPU port and DSA link(s) */
1273 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
1274 output_ports |= BIT(i);
1275 }
1276 }
1277
1278 /* prevent frames from going back out of the port they came in on */
1279 output_ports &= ~BIT(port);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001280
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001281 err = mv88e6xxx_port_read(chip, port, PORT_BASE_VLAN, &reg);
1282 if (err)
1283 return err;
Vivien Didelotede80982015-10-11 18:08:35 -04001284
1285 reg &= ~mask;
1286 reg |= output_ports & mask;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001287
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001288 return mv88e6xxx_port_write(chip, port, PORT_BASE_VLAN, reg);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001289}
1290
Vivien Didelotf81ec902016-05-09 13:22:58 -04001291static void mv88e6xxx_port_stp_state_set(struct dsa_switch *ds, int port,
1292 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001293{
Vivien Didelot04bed142016-08-31 18:06:13 -04001294 struct mv88e6xxx_chip *chip = ds->priv;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001295 int stp_state;
Vivien Didelot553eb542016-05-13 20:38:23 -04001296 int err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001297
1298 switch (state) {
1299 case BR_STATE_DISABLED:
Andrew Lunncca8b132015-04-02 04:06:39 +02001300 stp_state = PORT_CONTROL_STATE_DISABLED;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001301 break;
1302 case BR_STATE_BLOCKING:
1303 case BR_STATE_LISTENING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001304 stp_state = PORT_CONTROL_STATE_BLOCKING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001305 break;
1306 case BR_STATE_LEARNING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001307 stp_state = PORT_CONTROL_STATE_LEARNING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001308 break;
1309 case BR_STATE_FORWARDING:
1310 default:
Andrew Lunncca8b132015-04-02 04:06:39 +02001311 stp_state = PORT_CONTROL_STATE_FORWARDING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001312 break;
1313 }
1314
Vivien Didelotfad09c72016-06-21 12:28:20 -04001315 mutex_lock(&chip->reg_lock);
1316 err = _mv88e6xxx_port_state(chip, port, stp_state);
1317 mutex_unlock(&chip->reg_lock);
Vivien Didelot553eb542016-05-13 20:38:23 -04001318
1319 if (err)
Andrew Lunnc8b09802016-06-04 21:16:57 +02001320 netdev_err(ds->ports[port].netdev,
1321 "failed to update state to %s\n",
Vivien Didelot553eb542016-05-13 20:38:23 -04001322 mv88e6xxx_port_state_names[stp_state]);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001323}
1324
Vivien Didelot749efcb2016-09-22 16:49:24 -04001325static void mv88e6xxx_port_fast_age(struct dsa_switch *ds, int port)
1326{
1327 struct mv88e6xxx_chip *chip = ds->priv;
1328 int err;
1329
1330 mutex_lock(&chip->reg_lock);
1331 err = _mv88e6xxx_atu_remove(chip, 0, port, false);
1332 mutex_unlock(&chip->reg_lock);
1333
1334 if (err)
1335 netdev_err(ds->ports[port].netdev, "failed to flush ATU\n");
1336}
1337
Vivien Didelotfad09c72016-06-21 12:28:20 -04001338static int _mv88e6xxx_port_pvid(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001339 u16 *new, u16 *old)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001340{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001341 struct dsa_switch *ds = chip->ds;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001342 u16 pvid, reg;
1343 int err;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001344
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001345 err = mv88e6xxx_port_read(chip, port, PORT_DEFAULT_VLAN, &reg);
1346 if (err)
1347 return err;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001348
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001349 pvid = reg & PORT_DEFAULT_VLAN_MASK;
Vivien Didelot5da96032016-03-07 18:24:39 -05001350
1351 if (new) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001352 reg &= ~PORT_DEFAULT_VLAN_MASK;
1353 reg |= *new & PORT_DEFAULT_VLAN_MASK;
Vivien Didelot5da96032016-03-07 18:24:39 -05001354
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001355 err = mv88e6xxx_port_write(chip, port, PORT_DEFAULT_VLAN, reg);
1356 if (err)
1357 return err;
Vivien Didelot5da96032016-03-07 18:24:39 -05001358
Andrew Lunnc8b09802016-06-04 21:16:57 +02001359 netdev_dbg(ds->ports[port].netdev,
1360 "DefaultVID %d (was %d)\n", *new, pvid);
Vivien Didelot5da96032016-03-07 18:24:39 -05001361 }
1362
1363 if (old)
1364 *old = pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001365
1366 return 0;
1367}
1368
Vivien Didelotfad09c72016-06-21 12:28:20 -04001369static int _mv88e6xxx_port_pvid_get(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001370 int port, u16 *pvid)
Vivien Didelot5da96032016-03-07 18:24:39 -05001371{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001372 return _mv88e6xxx_port_pvid(chip, port, NULL, pvid);
Vivien Didelot5da96032016-03-07 18:24:39 -05001373}
1374
Vivien Didelotfad09c72016-06-21 12:28:20 -04001375static int _mv88e6xxx_port_pvid_set(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001376 int port, u16 pvid)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001377{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001378 return _mv88e6xxx_port_pvid(chip, port, &pvid, NULL);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001379}
1380
Vivien Didelotfad09c72016-06-21 12:28:20 -04001381static int _mv88e6xxx_vtu_wait(struct mv88e6xxx_chip *chip)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001382{
Vivien Didelota935c052016-09-29 12:21:53 -04001383 return mv88e6xxx_g1_wait(chip, GLOBAL_VTU_OP, GLOBAL_VTU_OP_BUSY);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001384}
1385
Vivien Didelotfad09c72016-06-21 12:28:20 -04001386static int _mv88e6xxx_vtu_cmd(struct mv88e6xxx_chip *chip, u16 op)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001387{
Vivien Didelota935c052016-09-29 12:21:53 -04001388 int err;
Vivien Didelot6b17e862015-08-13 12:52:18 -04001389
Vivien Didelota935c052016-09-29 12:21:53 -04001390 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_OP, op);
1391 if (err)
1392 return err;
Vivien Didelot6b17e862015-08-13 12:52:18 -04001393
Vivien Didelotfad09c72016-06-21 12:28:20 -04001394 return _mv88e6xxx_vtu_wait(chip);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001395}
1396
Vivien Didelotfad09c72016-06-21 12:28:20 -04001397static int _mv88e6xxx_vtu_stu_flush(struct mv88e6xxx_chip *chip)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001398{
1399 int ret;
1400
Vivien Didelotfad09c72016-06-21 12:28:20 -04001401 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001402 if (ret < 0)
1403 return ret;
1404
Vivien Didelotfad09c72016-06-21 12:28:20 -04001405 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_FLUSH_ALL);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001406}
1407
Vivien Didelotfad09c72016-06-21 12:28:20 -04001408static int _mv88e6xxx_vtu_stu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001409 struct mv88e6xxx_vtu_entry *entry,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001410 unsigned int nibble_offset)
1411{
Vivien Didelotb8fee952015-08-13 12:52:19 -04001412 u16 regs[3];
Vivien Didelota935c052016-09-29 12:21:53 -04001413 int i, err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001414
1415 for (i = 0; i < 3; ++i) {
Vivien Didelota935c052016-09-29 12:21:53 -04001416 u16 *reg = &regs[i];
Vivien Didelotb8fee952015-08-13 12:52:19 -04001417
Vivien Didelota935c052016-09-29 12:21:53 -04001418 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_DATA_0_3 + i, reg);
1419 if (err)
1420 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001421 }
1422
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001423 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotb8fee952015-08-13 12:52:19 -04001424 unsigned int shift = (i % 4) * 4 + nibble_offset;
1425 u16 reg = regs[i / 4];
1426
1427 entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
1428 }
1429
1430 return 0;
1431}
1432
Vivien Didelotfad09c72016-06-21 12:28:20 -04001433static int mv88e6xxx_vtu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001434 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001435{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001436 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 0);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001437}
1438
Vivien Didelotfad09c72016-06-21 12:28:20 -04001439static int mv88e6xxx_stu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001440 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001441{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001442 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 2);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001443}
1444
Vivien Didelotfad09c72016-06-21 12:28:20 -04001445static int _mv88e6xxx_vtu_stu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001446 struct mv88e6xxx_vtu_entry *entry,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001447 unsigned int nibble_offset)
1448{
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001449 u16 regs[3] = { 0 };
Vivien Didelota935c052016-09-29 12:21:53 -04001450 int i, err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001451
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001452 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001453 unsigned int shift = (i % 4) * 4 + nibble_offset;
1454 u8 data = entry->data[i];
1455
1456 regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
1457 }
1458
1459 for (i = 0; i < 3; ++i) {
Vivien Didelota935c052016-09-29 12:21:53 -04001460 u16 reg = regs[i];
1461
1462 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_DATA_0_3 + i, reg);
1463 if (err)
1464 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001465 }
1466
1467 return 0;
1468}
1469
Vivien Didelotfad09c72016-06-21 12:28:20 -04001470static int mv88e6xxx_vtu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001471 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001472{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001473 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 0);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001474}
1475
Vivien Didelotfad09c72016-06-21 12:28:20 -04001476static int mv88e6xxx_stu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001477 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001478{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001479 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 2);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001480}
1481
Vivien Didelotfad09c72016-06-21 12:28:20 -04001482static int _mv88e6xxx_vtu_vid_write(struct mv88e6xxx_chip *chip, u16 vid)
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001483{
Vivien Didelota935c052016-09-29 12:21:53 -04001484 return mv88e6xxx_g1_write(chip, GLOBAL_VTU_VID,
1485 vid & GLOBAL_VTU_VID_MASK);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001486}
1487
Vivien Didelotfad09c72016-06-21 12:28:20 -04001488static int _mv88e6xxx_vtu_getnext(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001489 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelotb8fee952015-08-13 12:52:19 -04001490{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001491 struct mv88e6xxx_vtu_entry next = { 0 };
Vivien Didelota935c052016-09-29 12:21:53 -04001492 u16 val;
1493 int err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001494
Vivien Didelota935c052016-09-29 12:21:53 -04001495 err = _mv88e6xxx_vtu_wait(chip);
1496 if (err)
1497 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001498
Vivien Didelota935c052016-09-29 12:21:53 -04001499 err = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_VTU_GET_NEXT);
1500 if (err)
1501 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001502
Vivien Didelota935c052016-09-29 12:21:53 -04001503 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_VID, &val);
1504 if (err)
1505 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001506
Vivien Didelota935c052016-09-29 12:21:53 -04001507 next.vid = val & GLOBAL_VTU_VID_MASK;
1508 next.valid = !!(val & GLOBAL_VTU_VID_VALID);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001509
1510 if (next.valid) {
Vivien Didelota935c052016-09-29 12:21:53 -04001511 err = mv88e6xxx_vtu_data_read(chip, &next);
1512 if (err)
1513 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001514
Vivien Didelot6dc10bb2016-09-29 12:21:55 -04001515 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G1_VTU_FID)) {
Vivien Didelota935c052016-09-29 12:21:53 -04001516 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_FID, &val);
1517 if (err)
1518 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001519
Vivien Didelota935c052016-09-29 12:21:53 -04001520 next.fid = val & GLOBAL_VTU_FID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001521 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001522 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1523 * VTU DBNum[3:0] are located in VTU Operation 3:0
1524 */
Vivien Didelota935c052016-09-29 12:21:53 -04001525 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_OP, &val);
1526 if (err)
1527 return err;
Vivien Didelot11ea8092016-03-31 16:53:44 -04001528
Vivien Didelota935c052016-09-29 12:21:53 -04001529 next.fid = (val & 0xf00) >> 4;
1530 next.fid |= val & 0xf;
Vivien Didelot2e7bd5e2016-03-31 16:53:41 -04001531 }
Vivien Didelotb8fee952015-08-13 12:52:19 -04001532
Vivien Didelotfad09c72016-06-21 12:28:20 -04001533 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
Vivien Didelota935c052016-09-29 12:21:53 -04001534 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_SID, &val);
1535 if (err)
1536 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001537
Vivien Didelota935c052016-09-29 12:21:53 -04001538 next.sid = val & GLOBAL_VTU_SID_MASK;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001539 }
1540 }
1541
1542 *entry = next;
1543 return 0;
1544}
1545
Vivien Didelotf81ec902016-05-09 13:22:58 -04001546static int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
1547 struct switchdev_obj_port_vlan *vlan,
1548 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001549{
Vivien Didelot04bed142016-08-31 18:06:13 -04001550 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001551 struct mv88e6xxx_vtu_entry next;
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001552 u16 pvid;
1553 int err;
1554
Vivien Didelotfad09c72016-06-21 12:28:20 -04001555 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001556 return -EOPNOTSUPP;
1557
Vivien Didelotfad09c72016-06-21 12:28:20 -04001558 mutex_lock(&chip->reg_lock);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001559
Vivien Didelotfad09c72016-06-21 12:28:20 -04001560 err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001561 if (err)
1562 goto unlock;
1563
Vivien Didelotfad09c72016-06-21 12:28:20 -04001564 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001565 if (err)
1566 goto unlock;
1567
1568 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001569 err = _mv88e6xxx_vtu_getnext(chip, &next);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001570 if (err)
1571 break;
1572
1573 if (!next.valid)
1574 break;
1575
1576 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1577 continue;
1578
1579 /* reinit and dump this VLAN obj */
Vivien Didelot57d32312016-06-20 13:13:58 -04001580 vlan->vid_begin = next.vid;
1581 vlan->vid_end = next.vid;
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001582 vlan->flags = 0;
1583
1584 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1585 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1586
1587 if (next.vid == pvid)
1588 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1589
1590 err = cb(&vlan->obj);
1591 if (err)
1592 break;
1593 } while (next.vid < GLOBAL_VTU_VID_MASK);
1594
1595unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001596 mutex_unlock(&chip->reg_lock);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001597
1598 return err;
1599}
1600
Vivien Didelotfad09c72016-06-21 12:28:20 -04001601static int _mv88e6xxx_vtu_loadpurge(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001602 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001603{
Vivien Didelot11ea8092016-03-31 16:53:44 -04001604 u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001605 u16 reg = 0;
Vivien Didelota935c052016-09-29 12:21:53 -04001606 int err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001607
Vivien Didelota935c052016-09-29 12:21:53 -04001608 err = _mv88e6xxx_vtu_wait(chip);
1609 if (err)
1610 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001611
1612 if (!entry->valid)
1613 goto loadpurge;
1614
1615 /* Write port member tags */
Vivien Didelota935c052016-09-29 12:21:53 -04001616 err = mv88e6xxx_vtu_data_write(chip, entry);
1617 if (err)
1618 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001619
Vivien Didelotfad09c72016-06-21 12:28:20 -04001620 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001621 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Vivien Didelota935c052016-09-29 12:21:53 -04001622 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_SID, reg);
1623 if (err)
1624 return err;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001625 }
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001626
Vivien Didelot6dc10bb2016-09-29 12:21:55 -04001627 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G1_VTU_FID)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001628 reg = entry->fid & GLOBAL_VTU_FID_MASK;
Vivien Didelota935c052016-09-29 12:21:53 -04001629 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_FID, reg);
1630 if (err)
1631 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001632 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001633 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1634 * VTU DBNum[3:0] are located in VTU Operation 3:0
1635 */
1636 op |= (entry->fid & 0xf0) << 8;
1637 op |= entry->fid & 0xf;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001638 }
1639
1640 reg = GLOBAL_VTU_VID_VALID;
1641loadpurge:
1642 reg |= entry->vid & GLOBAL_VTU_VID_MASK;
Vivien Didelota935c052016-09-29 12:21:53 -04001643 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_VID, reg);
1644 if (err)
1645 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001646
Vivien Didelotfad09c72016-06-21 12:28:20 -04001647 return _mv88e6xxx_vtu_cmd(chip, op);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001648}
1649
Vivien Didelotfad09c72016-06-21 12:28:20 -04001650static int _mv88e6xxx_stu_getnext(struct mv88e6xxx_chip *chip, u8 sid,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001651 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001652{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001653 struct mv88e6xxx_vtu_entry next = { 0 };
Vivien Didelota935c052016-09-29 12:21:53 -04001654 u16 val;
1655 int err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001656
Vivien Didelota935c052016-09-29 12:21:53 -04001657 err = _mv88e6xxx_vtu_wait(chip);
1658 if (err)
1659 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001660
Vivien Didelota935c052016-09-29 12:21:53 -04001661 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_SID,
1662 sid & GLOBAL_VTU_SID_MASK);
1663 if (err)
1664 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001665
Vivien Didelota935c052016-09-29 12:21:53 -04001666 err = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_GET_NEXT);
1667 if (err)
1668 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001669
Vivien Didelota935c052016-09-29 12:21:53 -04001670 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_SID, &val);
1671 if (err)
1672 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001673
Vivien Didelota935c052016-09-29 12:21:53 -04001674 next.sid = val & GLOBAL_VTU_SID_MASK;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001675
Vivien Didelota935c052016-09-29 12:21:53 -04001676 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_VID, &val);
1677 if (err)
1678 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001679
Vivien Didelota935c052016-09-29 12:21:53 -04001680 next.valid = !!(val & GLOBAL_VTU_VID_VALID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001681
1682 if (next.valid) {
Vivien Didelota935c052016-09-29 12:21:53 -04001683 err = mv88e6xxx_stu_data_read(chip, &next);
1684 if (err)
1685 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001686 }
1687
1688 *entry = next;
1689 return 0;
1690}
1691
Vivien Didelotfad09c72016-06-21 12:28:20 -04001692static int _mv88e6xxx_stu_loadpurge(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001693 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001694{
1695 u16 reg = 0;
Vivien Didelota935c052016-09-29 12:21:53 -04001696 int err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001697
Vivien Didelota935c052016-09-29 12:21:53 -04001698 err = _mv88e6xxx_vtu_wait(chip);
1699 if (err)
1700 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001701
1702 if (!entry->valid)
1703 goto loadpurge;
1704
1705 /* Write port states */
Vivien Didelota935c052016-09-29 12:21:53 -04001706 err = mv88e6xxx_stu_data_write(chip, entry);
1707 if (err)
1708 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001709
1710 reg = GLOBAL_VTU_VID_VALID;
1711loadpurge:
Vivien Didelota935c052016-09-29 12:21:53 -04001712 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_VID, reg);
1713 if (err)
1714 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001715
1716 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Vivien Didelota935c052016-09-29 12:21:53 -04001717 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_SID, reg);
1718 if (err)
1719 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001720
Vivien Didelotfad09c72016-06-21 12:28:20 -04001721 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_LOAD_PURGE);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001722}
1723
Vivien Didelotfad09c72016-06-21 12:28:20 -04001724static int _mv88e6xxx_port_fid(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001725 u16 *new, u16 *old)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001726{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001727 struct dsa_switch *ds = chip->ds;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001728 u16 upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001729 u16 fid;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001730 u16 reg;
1731 int err;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001732
Vivien Didelotfad09c72016-06-21 12:28:20 -04001733 if (mv88e6xxx_num_databases(chip) == 4096)
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001734 upper_mask = 0xff;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001735 else if (mv88e6xxx_num_databases(chip) == 256)
Vivien Didelot11ea8092016-03-31 16:53:44 -04001736 upper_mask = 0xf;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001737 else
1738 return -EOPNOTSUPP;
1739
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001740 /* Port's default FID bits 3:0 are located in reg 0x06, offset 12 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001741 err = mv88e6xxx_port_read(chip, port, PORT_BASE_VLAN, &reg);
1742 if (err)
1743 return err;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001744
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001745 fid = (reg & PORT_BASE_VLAN_FID_3_0_MASK) >> 12;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001746
1747 if (new) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001748 reg &= ~PORT_BASE_VLAN_FID_3_0_MASK;
1749 reg |= (*new << 12) & PORT_BASE_VLAN_FID_3_0_MASK;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001750
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001751 err = mv88e6xxx_port_write(chip, port, PORT_BASE_VLAN, reg);
1752 if (err)
1753 return err;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001754 }
1755
1756 /* Port's default FID bits 11:4 are located in reg 0x05, offset 0 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001757 err = mv88e6xxx_port_read(chip, port, PORT_CONTROL_1, &reg);
1758 if (err)
1759 return err;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001760
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001761 fid |= (reg & upper_mask) << 4;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001762
1763 if (new) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001764 reg &= ~upper_mask;
1765 reg |= (*new >> 4) & upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001766
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001767 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_1, reg);
1768 if (err)
1769 return err;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001770
Andrew Lunnc8b09802016-06-04 21:16:57 +02001771 netdev_dbg(ds->ports[port].netdev,
1772 "FID %d (was %d)\n", *new, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001773 }
1774
1775 if (old)
1776 *old = fid;
1777
1778 return 0;
1779}
1780
Vivien Didelotfad09c72016-06-21 12:28:20 -04001781static int _mv88e6xxx_port_fid_get(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001782 int port, u16 *fid)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001783{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001784 return _mv88e6xxx_port_fid(chip, port, NULL, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001785}
1786
Vivien Didelotfad09c72016-06-21 12:28:20 -04001787static int _mv88e6xxx_port_fid_set(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001788 int port, u16 fid)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001789{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001790 return _mv88e6xxx_port_fid(chip, port, &fid, NULL);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001791}
1792
Vivien Didelotfad09c72016-06-21 12:28:20 -04001793static int _mv88e6xxx_fid_new(struct mv88e6xxx_chip *chip, u16 *fid)
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001794{
1795 DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001796 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001797 int i, err;
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001798
1799 bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);
1800
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001801 /* Set every FID bit used by the (un)bridged ports */
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001802 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001803 err = _mv88e6xxx_port_fid_get(chip, i, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001804 if (err)
1805 return err;
1806
1807 set_bit(*fid, fid_bitmap);
1808 }
1809
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001810 /* Set every FID bit used by the VLAN entries */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001811 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001812 if (err)
1813 return err;
1814
1815 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001816 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001817 if (err)
1818 return err;
1819
1820 if (!vlan.valid)
1821 break;
1822
1823 set_bit(vlan.fid, fid_bitmap);
1824 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
1825
1826 /* The reset value 0x000 is used to indicate that multiple address
1827 * databases are not needed. Return the next positive available.
1828 */
1829 *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1);
Vivien Didelotfad09c72016-06-21 12:28:20 -04001830 if (unlikely(*fid >= mv88e6xxx_num_databases(chip)))
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001831 return -ENOSPC;
1832
1833 /* Clear the database */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001834 return _mv88e6xxx_atu_flush(chip, *fid, true);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001835}
1836
Vivien Didelotfad09c72016-06-21 12:28:20 -04001837static int _mv88e6xxx_vtu_new(struct mv88e6xxx_chip *chip, u16 vid,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001838 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001839{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001840 struct dsa_switch *ds = chip->ds;
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001841 struct mv88e6xxx_vtu_entry vlan = {
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001842 .valid = true,
1843 .vid = vid,
1844 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001845 int i, err;
1846
Vivien Didelotfad09c72016-06-21 12:28:20 -04001847 err = _mv88e6xxx_fid_new(chip, &vlan.fid);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001848 if (err)
1849 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001850
Vivien Didelot3d131f02015-11-03 10:52:52 -05001851 /* exclude all ports except the CPU and DSA ports */
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001852 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i)
Vivien Didelot3d131f02015-11-03 10:52:52 -05001853 vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)
1854 ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED
1855 : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001856
Vivien Didelotfad09c72016-06-21 12:28:20 -04001857 if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
1858 mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip)) {
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001859 struct mv88e6xxx_vtu_entry vstp;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001860
1861 /* Adding a VTU entry requires a valid STU entry. As VSTP is not
1862 * implemented, only one STU entry is needed to cover all VTU
1863 * entries. Thus, validate the SID 0.
1864 */
1865 vlan.sid = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001866 err = _mv88e6xxx_stu_getnext(chip, GLOBAL_VTU_SID_MASK, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001867 if (err)
1868 return err;
1869
1870 if (vstp.sid != vlan.sid || !vstp.valid) {
1871 memset(&vstp, 0, sizeof(vstp));
1872 vstp.valid = true;
1873 vstp.sid = vlan.sid;
1874
Vivien Didelotfad09c72016-06-21 12:28:20 -04001875 err = _mv88e6xxx_stu_loadpurge(chip, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001876 if (err)
1877 return err;
1878 }
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001879 }
1880
1881 *entry = vlan;
1882 return 0;
1883}
1884
Vivien Didelotfad09c72016-06-21 12:28:20 -04001885static int _mv88e6xxx_vtu_get(struct mv88e6xxx_chip *chip, u16 vid,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001886 struct mv88e6xxx_vtu_entry *entry, bool creat)
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001887{
1888 int err;
1889
1890 if (!vid)
1891 return -EINVAL;
1892
Vivien Didelotfad09c72016-06-21 12:28:20 -04001893 err = _mv88e6xxx_vtu_vid_write(chip, vid - 1);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001894 if (err)
1895 return err;
1896
Vivien Didelotfad09c72016-06-21 12:28:20 -04001897 err = _mv88e6xxx_vtu_getnext(chip, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001898 if (err)
1899 return err;
1900
1901 if (entry->vid != vid || !entry->valid) {
1902 if (!creat)
1903 return -EOPNOTSUPP;
1904 /* -ENOENT would've been more appropriate, but switchdev expects
1905 * -EOPNOTSUPP to inform bridge about an eventual software VLAN.
1906 */
1907
Vivien Didelotfad09c72016-06-21 12:28:20 -04001908 err = _mv88e6xxx_vtu_new(chip, vid, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001909 }
1910
1911 return err;
1912}
1913
Vivien Didelotda9c3592016-02-12 12:09:40 -05001914static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
1915 u16 vid_begin, u16 vid_end)
1916{
Vivien Didelot04bed142016-08-31 18:06:13 -04001917 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001918 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelotda9c3592016-02-12 12:09:40 -05001919 int i, err;
1920
1921 if (!vid_begin)
1922 return -EOPNOTSUPP;
1923
Vivien Didelotfad09c72016-06-21 12:28:20 -04001924 mutex_lock(&chip->reg_lock);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001925
Vivien Didelotfad09c72016-06-21 12:28:20 -04001926 err = _mv88e6xxx_vtu_vid_write(chip, vid_begin - 1);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001927 if (err)
1928 goto unlock;
1929
1930 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001931 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001932 if (err)
1933 goto unlock;
1934
1935 if (!vlan.valid)
1936 break;
1937
1938 if (vlan.vid > vid_end)
1939 break;
1940
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001941 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotda9c3592016-02-12 12:09:40 -05001942 if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i))
1943 continue;
1944
1945 if (vlan.data[i] ==
1946 GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1947 continue;
1948
Vivien Didelotfad09c72016-06-21 12:28:20 -04001949 if (chip->ports[i].bridge_dev ==
1950 chip->ports[port].bridge_dev)
Vivien Didelotda9c3592016-02-12 12:09:40 -05001951 break; /* same bridge, check next VLAN */
1952
Andrew Lunnc8b09802016-06-04 21:16:57 +02001953 netdev_warn(ds->ports[port].netdev,
Vivien Didelotda9c3592016-02-12 12:09:40 -05001954 "hardware VLAN %d already used by %s\n",
1955 vlan.vid,
Vivien Didelotfad09c72016-06-21 12:28:20 -04001956 netdev_name(chip->ports[i].bridge_dev));
Vivien Didelotda9c3592016-02-12 12:09:40 -05001957 err = -EOPNOTSUPP;
1958 goto unlock;
1959 }
1960 } while (vlan.vid < vid_end);
1961
1962unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001963 mutex_unlock(&chip->reg_lock);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001964
1965 return err;
1966}
1967
Vivien Didelot214cdb92016-02-26 13:16:08 -05001968static const char * const mv88e6xxx_port_8021q_mode_names[] = {
1969 [PORT_CONTROL_2_8021Q_DISABLED] = "Disabled",
1970 [PORT_CONTROL_2_8021Q_FALLBACK] = "Fallback",
1971 [PORT_CONTROL_2_8021Q_CHECK] = "Check",
1972 [PORT_CONTROL_2_8021Q_SECURE] = "Secure",
1973};
1974
Vivien Didelotf81ec902016-05-09 13:22:58 -04001975static int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port,
1976 bool vlan_filtering)
Vivien Didelot214cdb92016-02-26 13:16:08 -05001977{
Vivien Didelot04bed142016-08-31 18:06:13 -04001978 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001979 u16 old, new = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE :
1980 PORT_CONTROL_2_8021Q_DISABLED;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001981 u16 reg;
1982 int err;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001983
Vivien Didelotfad09c72016-06-21 12:28:20 -04001984 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001985 return -EOPNOTSUPP;
1986
Vivien Didelotfad09c72016-06-21 12:28:20 -04001987 mutex_lock(&chip->reg_lock);
Vivien Didelot214cdb92016-02-26 13:16:08 -05001988
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001989 err = mv88e6xxx_port_read(chip, port, PORT_CONTROL_2, &reg);
1990 if (err)
Vivien Didelot214cdb92016-02-26 13:16:08 -05001991 goto unlock;
1992
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001993 old = reg & PORT_CONTROL_2_8021Q_MASK;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001994
Vivien Didelot5220ef12016-03-07 18:24:52 -05001995 if (new != old) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001996 reg &= ~PORT_CONTROL_2_8021Q_MASK;
1997 reg |= new & PORT_CONTROL_2_8021Q_MASK;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001998
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001999 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_2, reg);
2000 if (err)
Vivien Didelot5220ef12016-03-07 18:24:52 -05002001 goto unlock;
Vivien Didelot214cdb92016-02-26 13:16:08 -05002002
Andrew Lunnc8b09802016-06-04 21:16:57 +02002003 netdev_dbg(ds->ports[port].netdev, "802.1Q Mode %s (was %s)\n",
Vivien Didelot5220ef12016-03-07 18:24:52 -05002004 mv88e6xxx_port_8021q_mode_names[new],
2005 mv88e6xxx_port_8021q_mode_names[old]);
2006 }
2007
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002008 err = 0;
Vivien Didelot214cdb92016-02-26 13:16:08 -05002009unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002010 mutex_unlock(&chip->reg_lock);
Vivien Didelot214cdb92016-02-26 13:16:08 -05002011
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002012 return err;
Vivien Didelot214cdb92016-02-26 13:16:08 -05002013}
2014
Vivien Didelot57d32312016-06-20 13:13:58 -04002015static int
2016mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
2017 const struct switchdev_obj_port_vlan *vlan,
2018 struct switchdev_trans *trans)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002019{
Vivien Didelot04bed142016-08-31 18:06:13 -04002020 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotda9c3592016-02-12 12:09:40 -05002021 int err;
2022
Vivien Didelotfad09c72016-06-21 12:28:20 -04002023 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04002024 return -EOPNOTSUPP;
2025
Vivien Didelotda9c3592016-02-12 12:09:40 -05002026 /* If the requested port doesn't belong to the same bridge as the VLAN
2027 * members, do not support it (yet) and fallback to software VLAN.
2028 */
2029 err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin,
2030 vlan->vid_end);
2031 if (err)
2032 return err;
2033
Vivien Didelot76e398a2015-11-01 12:33:55 -05002034 /* We don't need any dynamic resource from the kernel (yet),
2035 * so skip the prepare phase.
2036 */
2037 return 0;
2038}
2039
Vivien Didelotfad09c72016-06-21 12:28:20 -04002040static int _mv88e6xxx_port_vlan_add(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04002041 u16 vid, bool untagged)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002042{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04002043 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002044 int err;
2045
Vivien Didelotfad09c72016-06-21 12:28:20 -04002046 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, true);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002047 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002048 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002049
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002050 vlan.data[port] = untagged ?
2051 GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
2052 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
2053
Vivien Didelotfad09c72016-06-21 12:28:20 -04002054 return _mv88e6xxx_vtu_loadpurge(chip, &vlan);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002055}
2056
Vivien Didelotf81ec902016-05-09 13:22:58 -04002057static void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
2058 const struct switchdev_obj_port_vlan *vlan,
2059 struct switchdev_trans *trans)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002060{
Vivien Didelot04bed142016-08-31 18:06:13 -04002061 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot76e398a2015-11-01 12:33:55 -05002062 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
2063 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
2064 u16 vid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05002065
Vivien Didelotfad09c72016-06-21 12:28:20 -04002066 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04002067 return;
2068
Vivien Didelotfad09c72016-06-21 12:28:20 -04002069 mutex_lock(&chip->reg_lock);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002070
Vivien Didelot4d5770b2016-04-06 11:55:05 -04002071 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002072 if (_mv88e6xxx_port_vlan_add(chip, port, vid, untagged))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002073 netdev_err(ds->ports[port].netdev,
2074 "failed to add VLAN %d%c\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04002075 vid, untagged ? 'u' : 't');
Vivien Didelot76e398a2015-11-01 12:33:55 -05002076
Vivien Didelotfad09c72016-06-21 12:28:20 -04002077 if (pvid && _mv88e6xxx_port_pvid_set(chip, port, vlan->vid_end))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002078 netdev_err(ds->ports[port].netdev, "failed to set PVID %d\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04002079 vlan->vid_end);
2080
Vivien Didelotfad09c72016-06-21 12:28:20 -04002081 mutex_unlock(&chip->reg_lock);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002082}
2083
Vivien Didelotfad09c72016-06-21 12:28:20 -04002084static int _mv88e6xxx_port_vlan_del(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04002085 int port, u16 vid)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002086{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002087 struct dsa_switch *ds = chip->ds;
Vivien Didelotb4e47c02016-09-29 12:21:58 -04002088 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002089 int i, err;
2090
Vivien Didelotfad09c72016-06-21 12:28:20 -04002091 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002092 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002093 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002094
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05002095 /* Tell switchdev if this VLAN is handled in software */
2096 if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
Vivien Didelot3c06f082016-02-05 14:04:39 -05002097 return -EOPNOTSUPP;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002098
2099 vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
2100
2101 /* keep the VLAN unless all ports are excluded */
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002102 vlan.valid = false;
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002103 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelot3d131f02015-11-03 10:52:52 -05002104 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002105 continue;
2106
2107 if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002108 vlan.valid = true;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002109 break;
2110 }
2111 }
2112
Vivien Didelotfad09c72016-06-21 12:28:20 -04002113 err = _mv88e6xxx_vtu_loadpurge(chip, &vlan);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002114 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002115 return err;
2116
Vivien Didelotfad09c72016-06-21 12:28:20 -04002117 return _mv88e6xxx_atu_remove(chip, vlan.fid, port, false);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002118}
2119
Vivien Didelotf81ec902016-05-09 13:22:58 -04002120static int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
2121 const struct switchdev_obj_port_vlan *vlan)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002122{
Vivien Didelot04bed142016-08-31 18:06:13 -04002123 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot76e398a2015-11-01 12:33:55 -05002124 u16 pvid, vid;
2125 int err = 0;
2126
Vivien Didelotfad09c72016-06-21 12:28:20 -04002127 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04002128 return -EOPNOTSUPP;
2129
Vivien Didelotfad09c72016-06-21 12:28:20 -04002130 mutex_lock(&chip->reg_lock);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002131
Vivien Didelotfad09c72016-06-21 12:28:20 -04002132 err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002133 if (err)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002134 goto unlock;
2135
Vivien Didelot76e398a2015-11-01 12:33:55 -05002136 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002137 err = _mv88e6xxx_port_vlan_del(chip, port, vid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002138 if (err)
2139 goto unlock;
2140
2141 if (vid == pvid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002142 err = _mv88e6xxx_port_pvid_set(chip, port, 0);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002143 if (err)
2144 goto unlock;
2145 }
2146 }
2147
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002148unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002149 mutex_unlock(&chip->reg_lock);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002150
2151 return err;
2152}
2153
Vivien Didelotfad09c72016-06-21 12:28:20 -04002154static int _mv88e6xxx_atu_mac_write(struct mv88e6xxx_chip *chip,
Vivien Didelotc5723ac2015-08-10 09:09:48 -04002155 const unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002156{
Vivien Didelota935c052016-09-29 12:21:53 -04002157 int i, err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002158
2159 for (i = 0; i < 3; i++) {
Vivien Didelota935c052016-09-29 12:21:53 -04002160 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_MAC_01 + i,
2161 (addr[i * 2] << 8) | addr[i * 2 + 1]);
2162 if (err)
2163 return err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002164 }
2165
2166 return 0;
2167}
2168
Vivien Didelotfad09c72016-06-21 12:28:20 -04002169static int _mv88e6xxx_atu_mac_read(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04002170 unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002171{
Vivien Didelota935c052016-09-29 12:21:53 -04002172 u16 val;
2173 int i, err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002174
2175 for (i = 0; i < 3; i++) {
Vivien Didelota935c052016-09-29 12:21:53 -04002176 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_MAC_01 + i, &val);
2177 if (err)
2178 return err;
2179
2180 addr[i * 2] = val >> 8;
2181 addr[i * 2 + 1] = val & 0xff;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002182 }
2183
2184 return 0;
2185}
2186
Vivien Didelotfad09c72016-06-21 12:28:20 -04002187static int _mv88e6xxx_atu_load(struct mv88e6xxx_chip *chip,
Vivien Didelotfd231c82015-08-10 09:09:50 -04002188 struct mv88e6xxx_atu_entry *entry)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002189{
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002190 int ret;
2191
Vivien Didelotfad09c72016-06-21 12:28:20 -04002192 ret = _mv88e6xxx_atu_wait(chip);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002193 if (ret < 0)
2194 return ret;
2195
Vivien Didelotfad09c72016-06-21 12:28:20 -04002196 ret = _mv88e6xxx_atu_mac_write(chip, entry->mac);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002197 if (ret < 0)
2198 return ret;
2199
Vivien Didelotfad09c72016-06-21 12:28:20 -04002200 ret = _mv88e6xxx_atu_data_write(chip, entry);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002201 if (ret < 0)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002202 return ret;
2203
Vivien Didelotfad09c72016-06-21 12:28:20 -04002204 return _mv88e6xxx_atu_cmd(chip, entry->fid, GLOBAL_ATU_OP_LOAD_DB);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002205}
David S. Millercdf09692015-08-11 12:00:37 -07002206
Vivien Didelot88472932016-09-19 19:56:11 -04002207static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
2208 struct mv88e6xxx_atu_entry *entry);
2209
2210static int mv88e6xxx_atu_get(struct mv88e6xxx_chip *chip, int fid,
2211 const u8 *addr, struct mv88e6xxx_atu_entry *entry)
2212{
2213 struct mv88e6xxx_atu_entry next;
2214 int err;
2215
2216 eth_broadcast_addr(next.mac);
2217
2218 err = _mv88e6xxx_atu_mac_write(chip, next.mac);
2219 if (err)
2220 return err;
2221
2222 do {
2223 err = _mv88e6xxx_atu_getnext(chip, fid, &next);
2224 if (err)
2225 return err;
2226
2227 if (next.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2228 break;
2229
2230 if (ether_addr_equal(next.mac, addr)) {
2231 *entry = next;
2232 return 0;
2233 }
2234 } while (!is_broadcast_ether_addr(next.mac));
2235
2236 memset(entry, 0, sizeof(*entry));
2237 entry->fid = fid;
2238 ether_addr_copy(entry->mac, addr);
2239
2240 return 0;
2241}
2242
Vivien Didelot83dabd12016-08-31 11:50:04 -04002243static int mv88e6xxx_port_db_load_purge(struct mv88e6xxx_chip *chip, int port,
2244 const unsigned char *addr, u16 vid,
2245 u8 state)
Vivien Didelotfd231c82015-08-10 09:09:50 -04002246{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04002247 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelot88472932016-09-19 19:56:11 -04002248 struct mv88e6xxx_atu_entry entry;
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002249 int err;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002250
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002251 /* Null VLAN ID corresponds to the port private database */
2252 if (vid == 0)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002253 err = _mv88e6xxx_port_fid_get(chip, port, &vlan.fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002254 else
Vivien Didelotfad09c72016-06-21 12:28:20 -04002255 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002256 if (err)
2257 return err;
2258
Vivien Didelot88472932016-09-19 19:56:11 -04002259 err = mv88e6xxx_atu_get(chip, vlan.fid, addr, &entry);
2260 if (err)
2261 return err;
2262
2263 /* Purge the ATU entry only if no port is using it anymore */
2264 if (state == GLOBAL_ATU_DATA_STATE_UNUSED) {
2265 entry.portv_trunkid &= ~BIT(port);
2266 if (!entry.portv_trunkid)
2267 entry.state = GLOBAL_ATU_DATA_STATE_UNUSED;
2268 } else {
2269 entry.portv_trunkid |= BIT(port);
2270 entry.state = state;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002271 }
2272
Vivien Didelotfad09c72016-06-21 12:28:20 -04002273 return _mv88e6xxx_atu_load(chip, &entry);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002274}
2275
Vivien Didelotf81ec902016-05-09 13:22:58 -04002276static int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
2277 const struct switchdev_obj_port_fdb *fdb,
2278 struct switchdev_trans *trans)
Vivien Didelot146a3202015-10-08 11:35:12 -04002279{
2280 /* We don't need any dynamic resource from the kernel (yet),
2281 * so skip the prepare phase.
2282 */
2283 return 0;
2284}
2285
Vivien Didelotf81ec902016-05-09 13:22:58 -04002286static void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
2287 const struct switchdev_obj_port_fdb *fdb,
2288 struct switchdev_trans *trans)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002289{
Vivien Didelot04bed142016-08-31 18:06:13 -04002290 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot6630e232015-08-06 01:44:07 -04002291
Vivien Didelotfad09c72016-06-21 12:28:20 -04002292 mutex_lock(&chip->reg_lock);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002293 if (mv88e6xxx_port_db_load_purge(chip, port, fdb->addr, fdb->vid,
2294 GLOBAL_ATU_DATA_STATE_UC_STATIC))
2295 netdev_err(ds->ports[port].netdev, "failed to load unicast MAC address\n");
Vivien Didelotfad09c72016-06-21 12:28:20 -04002296 mutex_unlock(&chip->reg_lock);
David S. Millercdf09692015-08-11 12:00:37 -07002297}
2298
Vivien Didelotf81ec902016-05-09 13:22:58 -04002299static int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
2300 const struct switchdev_obj_port_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -07002301{
Vivien Didelot04bed142016-08-31 18:06:13 -04002302 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot83dabd12016-08-31 11:50:04 -04002303 int err;
David S. Millercdf09692015-08-11 12:00:37 -07002304
Vivien Didelotfad09c72016-06-21 12:28:20 -04002305 mutex_lock(&chip->reg_lock);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002306 err = mv88e6xxx_port_db_load_purge(chip, port, fdb->addr, fdb->vid,
2307 GLOBAL_ATU_DATA_STATE_UNUSED);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002308 mutex_unlock(&chip->reg_lock);
David S. Millercdf09692015-08-11 12:00:37 -07002309
Vivien Didelot83dabd12016-08-31 11:50:04 -04002310 return err;
David S. Millercdf09692015-08-11 12:00:37 -07002311}
2312
Vivien Didelotfad09c72016-06-21 12:28:20 -04002313static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
Vivien Didelot1d194042015-08-10 09:09:51 -04002314 struct mv88e6xxx_atu_entry *entry)
David S. Millercdf09692015-08-11 12:00:37 -07002315{
Vivien Didelot1d194042015-08-10 09:09:51 -04002316 struct mv88e6xxx_atu_entry next = { 0 };
Vivien Didelota935c052016-09-29 12:21:53 -04002317 u16 val;
2318 int err;
Vivien Didelot1d194042015-08-10 09:09:51 -04002319
2320 next.fid = fid;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002321
Vivien Didelota935c052016-09-29 12:21:53 -04002322 err = _mv88e6xxx_atu_wait(chip);
2323 if (err)
2324 return err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002325
Vivien Didelota935c052016-09-29 12:21:53 -04002326 err = _mv88e6xxx_atu_cmd(chip, fid, GLOBAL_ATU_OP_GET_NEXT_DB);
2327 if (err)
2328 return err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002329
Vivien Didelota935c052016-09-29 12:21:53 -04002330 err = _mv88e6xxx_atu_mac_read(chip, next.mac);
2331 if (err)
2332 return err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002333
Vivien Didelota935c052016-09-29 12:21:53 -04002334 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_DATA, &val);
2335 if (err)
2336 return err;
Vivien Didelot1d194042015-08-10 09:09:51 -04002337
Vivien Didelota935c052016-09-29 12:21:53 -04002338 next.state = val & GLOBAL_ATU_DATA_STATE_MASK;
Vivien Didelot1d194042015-08-10 09:09:51 -04002339 if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2340 unsigned int mask, shift;
2341
Vivien Didelota935c052016-09-29 12:21:53 -04002342 if (val & GLOBAL_ATU_DATA_TRUNK) {
Vivien Didelot1d194042015-08-10 09:09:51 -04002343 next.trunk = true;
2344 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
2345 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
2346 } else {
2347 next.trunk = false;
2348 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
2349 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
2350 }
2351
Vivien Didelota935c052016-09-29 12:21:53 -04002352 next.portv_trunkid = (val & mask) >> shift;
Vivien Didelot1d194042015-08-10 09:09:51 -04002353 }
2354
2355 *entry = next;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002356 return 0;
2357}
2358
Vivien Didelot83dabd12016-08-31 11:50:04 -04002359static int mv88e6xxx_port_db_dump_fid(struct mv88e6xxx_chip *chip,
2360 u16 fid, u16 vid, int port,
2361 struct switchdev_obj *obj,
2362 int (*cb)(struct switchdev_obj *obj))
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002363{
2364 struct mv88e6xxx_atu_entry addr = {
2365 .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2366 };
2367 int err;
2368
Vivien Didelotfad09c72016-06-21 12:28:20 -04002369 err = _mv88e6xxx_atu_mac_write(chip, addr.mac);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002370 if (err)
2371 return err;
2372
2373 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002374 err = _mv88e6xxx_atu_getnext(chip, fid, &addr);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002375 if (err)
Vivien Didelot83dabd12016-08-31 11:50:04 -04002376 return err;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002377
2378 if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2379 break;
2380
Vivien Didelot83dabd12016-08-31 11:50:04 -04002381 if (addr.trunk || (addr.portv_trunkid & BIT(port)) == 0)
2382 continue;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002383
Vivien Didelot83dabd12016-08-31 11:50:04 -04002384 if (obj->id == SWITCHDEV_OBJ_ID_PORT_FDB) {
2385 struct switchdev_obj_port_fdb *fdb;
2386
2387 if (!is_unicast_ether_addr(addr.mac))
2388 continue;
2389
2390 fdb = SWITCHDEV_OBJ_PORT_FDB(obj);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002391 fdb->vid = vid;
2392 ether_addr_copy(fdb->addr, addr.mac);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002393 if (addr.state == GLOBAL_ATU_DATA_STATE_UC_STATIC)
2394 fdb->ndm_state = NUD_NOARP;
2395 else
2396 fdb->ndm_state = NUD_REACHABLE;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04002397 } else if (obj->id == SWITCHDEV_OBJ_ID_PORT_MDB) {
2398 struct switchdev_obj_port_mdb *mdb;
2399
2400 if (!is_multicast_ether_addr(addr.mac))
2401 continue;
2402
2403 mdb = SWITCHDEV_OBJ_PORT_MDB(obj);
2404 mdb->vid = vid;
2405 ether_addr_copy(mdb->addr, addr.mac);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002406 } else {
2407 return -EOPNOTSUPP;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002408 }
Vivien Didelot83dabd12016-08-31 11:50:04 -04002409
2410 err = cb(obj);
2411 if (err)
2412 return err;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002413 } while (!is_broadcast_ether_addr(addr.mac));
2414
2415 return err;
2416}
2417
Vivien Didelot83dabd12016-08-31 11:50:04 -04002418static int mv88e6xxx_port_db_dump(struct mv88e6xxx_chip *chip, int port,
2419 struct switchdev_obj *obj,
2420 int (*cb)(struct switchdev_obj *obj))
2421{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04002422 struct mv88e6xxx_vtu_entry vlan = {
Vivien Didelot83dabd12016-08-31 11:50:04 -04002423 .vid = GLOBAL_VTU_VID_MASK, /* all ones */
2424 };
2425 u16 fid;
2426 int err;
2427
2428 /* Dump port's default Filtering Information Database (VLAN ID 0) */
2429 err = _mv88e6xxx_port_fid_get(chip, port, &fid);
2430 if (err)
2431 return err;
2432
2433 err = mv88e6xxx_port_db_dump_fid(chip, fid, 0, port, obj, cb);
2434 if (err)
2435 return err;
2436
2437 /* Dump VLANs' Filtering Information Databases */
2438 err = _mv88e6xxx_vtu_vid_write(chip, vlan.vid);
2439 if (err)
2440 return err;
2441
2442 do {
2443 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
2444 if (err)
2445 return err;
2446
2447 if (!vlan.valid)
2448 break;
2449
2450 err = mv88e6xxx_port_db_dump_fid(chip, vlan.fid, vlan.vid, port,
2451 obj, cb);
2452 if (err)
2453 return err;
2454 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
2455
2456 return err;
2457}
2458
Vivien Didelotf81ec902016-05-09 13:22:58 -04002459static int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
2460 struct switchdev_obj_port_fdb *fdb,
2461 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotf33475b2015-10-22 09:34:41 -04002462{
Vivien Didelot04bed142016-08-31 18:06:13 -04002463 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002464 int err;
2465
Vivien Didelotfad09c72016-06-21 12:28:20 -04002466 mutex_lock(&chip->reg_lock);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002467 err = mv88e6xxx_port_db_dump(chip, port, &fdb->obj, cb);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002468 mutex_unlock(&chip->reg_lock);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002469
2470 return err;
2471}
2472
Vivien Didelotf81ec902016-05-09 13:22:58 -04002473static int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
2474 struct net_device *bridge)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002475{
Vivien Didelot04bed142016-08-31 18:06:13 -04002476 struct mv88e6xxx_chip *chip = ds->priv;
Colin Ian King1d9619d2016-04-25 23:11:22 +01002477 int i, err = 0;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002478
Vivien Didelotfad09c72016-06-21 12:28:20 -04002479 mutex_lock(&chip->reg_lock);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002480
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002481 /* Assign the bridge and remap each port's VLANTable */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002482 chip->ports[port].bridge_dev = bridge;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002483
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002484 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002485 if (chip->ports[i].bridge_dev == bridge) {
2486 err = _mv88e6xxx_port_based_vlan_map(chip, i);
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002487 if (err)
2488 break;
2489 }
2490 }
2491
Vivien Didelotfad09c72016-06-21 12:28:20 -04002492 mutex_unlock(&chip->reg_lock);
Vivien Didelota6692752016-02-12 12:09:39 -05002493
Vivien Didelot466dfa02016-02-26 13:16:05 -05002494 return err;
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002495}
2496
Vivien Didelotf81ec902016-05-09 13:22:58 -04002497static void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002498{
Vivien Didelot04bed142016-08-31 18:06:13 -04002499 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002500 struct net_device *bridge = chip->ports[port].bridge_dev;
Vivien Didelot16bfa702016-03-13 16:21:33 -04002501 int i;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002502
Vivien Didelotfad09c72016-06-21 12:28:20 -04002503 mutex_lock(&chip->reg_lock);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002504
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002505 /* Unassign the bridge and remap each port's VLANTable */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002506 chip->ports[port].bridge_dev = NULL;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002507
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002508 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002509 if (i == port || chip->ports[i].bridge_dev == bridge)
2510 if (_mv88e6xxx_port_based_vlan_map(chip, i))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002511 netdev_warn(ds->ports[i].netdev,
2512 "failed to remap\n");
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002513
Vivien Didelotfad09c72016-06-21 12:28:20 -04002514 mutex_unlock(&chip->reg_lock);
Vivien Didelot66d9cd02016-02-05 14:07:14 -05002515}
2516
Vivien Didelotfad09c72016-06-21 12:28:20 -04002517static int mv88e6xxx_switch_reset(struct mv88e6xxx_chip *chip)
Vivien Didelot552238b2016-05-09 13:22:49 -04002518{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002519 bool ppu_active = mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE);
Vivien Didelot552238b2016-05-09 13:22:49 -04002520 u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002521 struct gpio_desc *gpiod = chip->reset;
Vivien Didelot552238b2016-05-09 13:22:49 -04002522 unsigned long timeout;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002523 u16 reg;
Vivien Didelota935c052016-09-29 12:21:53 -04002524 int err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002525 int i;
2526
2527 /* Set all ports to the disabled state. */
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002528 for (i = 0; i < mv88e6xxx_num_ports(chip); i++) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002529 err = mv88e6xxx_port_read(chip, i, PORT_CONTROL, &reg);
2530 if (err)
2531 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002532
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002533 err = mv88e6xxx_port_write(chip, i, PORT_CONTROL,
2534 reg & 0xfffc);
2535 if (err)
2536 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002537 }
2538
2539 /* Wait for transmit queues to drain. */
2540 usleep_range(2000, 4000);
2541
2542 /* If there is a gpio connected to the reset pin, toggle it */
2543 if (gpiod) {
2544 gpiod_set_value_cansleep(gpiod, 1);
2545 usleep_range(10000, 20000);
2546 gpiod_set_value_cansleep(gpiod, 0);
2547 usleep_range(10000, 20000);
2548 }
2549
2550 /* Reset the switch. Keep the PPU active if requested. The PPU
2551 * needs to be active to support indirect phy register access
2552 * through global registers 0x18 and 0x19.
2553 */
2554 if (ppu_active)
Vivien Didelota935c052016-09-29 12:21:53 -04002555 err = mv88e6xxx_g1_write(chip, 0x04, 0xc000);
Vivien Didelot552238b2016-05-09 13:22:49 -04002556 else
Vivien Didelota935c052016-09-29 12:21:53 -04002557 err = mv88e6xxx_g1_write(chip, 0x04, 0xc400);
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002558 if (err)
2559 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002560
2561 /* Wait up to one second for reset to complete. */
2562 timeout = jiffies + 1 * HZ;
2563 while (time_before(jiffies, timeout)) {
Vivien Didelota935c052016-09-29 12:21:53 -04002564 err = mv88e6xxx_g1_read(chip, 0x00, &reg);
2565 if (err)
2566 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002567
Vivien Didelota935c052016-09-29 12:21:53 -04002568 if ((reg & is_reset) == is_reset)
Vivien Didelot552238b2016-05-09 13:22:49 -04002569 break;
2570 usleep_range(1000, 2000);
2571 }
2572 if (time_after(jiffies, timeout))
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002573 err = -ETIMEDOUT;
Vivien Didelot552238b2016-05-09 13:22:49 -04002574 else
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002575 err = 0;
Vivien Didelot552238b2016-05-09 13:22:49 -04002576
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002577 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002578}
2579
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002580static int mv88e6xxx_serdes_power_on(struct mv88e6xxx_chip *chip)
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002581{
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002582 u16 val;
2583 int err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002584
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002585 /* Clear Power Down bit */
2586 err = mv88e6xxx_serdes_read(chip, MII_BMCR, &val);
2587 if (err)
2588 return err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002589
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002590 if (val & BMCR_PDOWN) {
2591 val &= ~BMCR_PDOWN;
2592 err = mv88e6xxx_serdes_write(chip, MII_BMCR, val);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002593 }
2594
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002595 return err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002596}
2597
Vivien Didelotfad09c72016-06-21 12:28:20 -04002598static int mv88e6xxx_setup_port(struct mv88e6xxx_chip *chip, int port)
Guenter Roeckd827e882015-03-26 18:36:29 -07002599{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002600 struct dsa_switch *ds = chip->ds;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002601 int err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002602 u16 reg;
Guenter Roeckd827e882015-03-26 18:36:29 -07002603
Vivien Didelotfad09c72016-06-21 12:28:20 -04002604 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2605 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2606 mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip) ||
2607 mv88e6xxx_6065_family(chip) || mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002608 /* MAC Forcing register: don't force link, speed,
2609 * duplex or flow control state to any particular
2610 * values on physical ports, but force the CPU port
2611 * and all DSA ports to their maximum bandwidth and
2612 * full duplex.
2613 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002614 err = mv88e6xxx_port_read(chip, port, PORT_PCS_CTRL, &reg);
Andrew Lunn60045cb2015-08-17 23:52:51 +02002615 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
Russell King53adc9e2015-09-21 21:42:59 +01002616 reg &= ~PORT_PCS_CTRL_UNFORCED;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002617 reg |= PORT_PCS_CTRL_FORCE_LINK |
2618 PORT_PCS_CTRL_LINK_UP |
2619 PORT_PCS_CTRL_DUPLEX_FULL |
2620 PORT_PCS_CTRL_FORCE_DUPLEX;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002621 if (mv88e6xxx_6065_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002622 reg |= PORT_PCS_CTRL_100;
2623 else
2624 reg |= PORT_PCS_CTRL_1000;
2625 } else {
2626 reg |= PORT_PCS_CTRL_UNFORCED;
2627 }
2628
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002629 err = mv88e6xxx_port_write(chip, port, PORT_PCS_CTRL, reg);
2630 if (err)
2631 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002632 }
2633
2634 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
2635 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
2636 * tunneling, determine priority by looking at 802.1p and IP
2637 * priority fields (IP prio has precedence), and set STP state
2638 * to Forwarding.
2639 *
2640 * If this is the CPU link, use DSA or EDSA tagging depending
2641 * on which tagging mode was configured.
2642 *
2643 * If this is a link to another switch, use DSA tagging mode.
2644 *
2645 * If this is the upstream port for this switch, enable
2646 * forwarding of unknown unicasts and multicasts.
2647 */
2648 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002649 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2650 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2651 mv88e6xxx_6095_family(chip) || mv88e6xxx_6065_family(chip) ||
2652 mv88e6xxx_6185_family(chip) || mv88e6xxx_6320_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002653 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
2654 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
2655 PORT_CONTROL_STATE_FORWARDING;
2656 if (dsa_is_cpu_port(ds, port)) {
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002657 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA))
Andrew Lunn5377b802016-06-04 21:17:02 +02002658 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA |
Andrew Lunnc047a1f2015-09-29 01:50:56 +02002659 PORT_CONTROL_FORWARD_UNKNOWN_MC;
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002660 else
2661 reg |= PORT_CONTROL_DSA_TAG;
Jamie Lentinf027e0c2016-08-22 16:01:04 +02002662 reg |= PORT_CONTROL_EGRESS_ADD_TAG |
2663 PORT_CONTROL_FORWARD_UNKNOWN;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002664 }
Andrew Lunn6083ce72015-08-17 23:52:52 +02002665 if (dsa_is_dsa_port(ds, port)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002666 if (mv88e6xxx_6095_family(chip) ||
2667 mv88e6xxx_6185_family(chip))
Andrew Lunn6083ce72015-08-17 23:52:52 +02002668 reg |= PORT_CONTROL_DSA_TAG;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002669 if (mv88e6xxx_6352_family(chip) ||
2670 mv88e6xxx_6351_family(chip) ||
2671 mv88e6xxx_6165_family(chip) ||
2672 mv88e6xxx_6097_family(chip) ||
2673 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002674 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunn6083ce72015-08-17 23:52:52 +02002675 }
2676
Andrew Lunn54d792f2015-05-06 01:09:47 +02002677 if (port == dsa_upstream_port(ds))
2678 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2679 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2680 }
2681 if (reg) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002682 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL, reg);
2683 if (err)
2684 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002685 }
2686
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002687 /* If this port is connected to a SerDes, make sure the SerDes is not
2688 * powered down.
2689 */
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002690 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_SERDES)) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002691 err = mv88e6xxx_port_read(chip, port, PORT_STATUS, &reg);
2692 if (err)
2693 return err;
2694 reg &= PORT_STATUS_CMODE_MASK;
2695 if ((reg == PORT_STATUS_CMODE_100BASE_X) ||
2696 (reg == PORT_STATUS_CMODE_1000BASE_X) ||
2697 (reg == PORT_STATUS_CMODE_SGMII)) {
2698 err = mv88e6xxx_serdes_power_on(chip);
2699 if (err < 0)
2700 return err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002701 }
2702 }
2703
Vivien Didelot8efdda42015-08-13 12:52:23 -04002704 /* Port Control 2: don't force a good FCS, set the maximum frame size to
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002705 * 10240 bytes, disable 802.1q tags checking, don't discard tagged or
Vivien Didelot8efdda42015-08-13 12:52:23 -04002706 * untagged frames on this port, do a destination address lookup on all
2707 * received packets as usual, disable ARP mirroring and don't send a
2708 * copy of all transmitted/received frames on this port to the CPU.
Andrew Lunn54d792f2015-05-06 01:09:47 +02002709 */
2710 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002711 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2712 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2713 mv88e6xxx_6095_family(chip) || mv88e6xxx_6320_family(chip) ||
2714 mv88e6xxx_6185_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002715 reg = PORT_CONTROL_2_MAP_DA;
2716
Vivien Didelotfad09c72016-06-21 12:28:20 -04002717 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2718 mv88e6xxx_6165_family(chip) || mv88e6xxx_6320_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002719 reg |= PORT_CONTROL_2_JUMBO_10240;
2720
Vivien Didelotfad09c72016-06-21 12:28:20 -04002721 if (mv88e6xxx_6095_family(chip) || mv88e6xxx_6185_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002722 /* Set the upstream port this port should use */
2723 reg |= dsa_upstream_port(ds);
2724 /* enable forwarding of unknown multicast addresses to
2725 * the upstream port
2726 */
2727 if (port == dsa_upstream_port(ds))
2728 reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
2729 }
2730
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002731 reg |= PORT_CONTROL_2_8021Q_DISABLED;
Vivien Didelot8efdda42015-08-13 12:52:23 -04002732
Andrew Lunn54d792f2015-05-06 01:09:47 +02002733 if (reg) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002734 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_2, reg);
2735 if (err)
2736 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002737 }
2738
2739 /* Port Association Vector: when learning source addresses
2740 * of packets, add the address to the address database using
2741 * a port bitmap that has only the bit for this port set and
2742 * the other bits clear.
2743 */
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002744 reg = 1 << port;
Vivien Didelot996ecb82016-04-14 14:42:08 -04002745 /* Disable learning for CPU port */
2746 if (dsa_is_cpu_port(ds, port))
Vivien Didelot65fa4022016-04-14 14:42:07 -04002747 reg = 0;
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002748
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002749 err = mv88e6xxx_port_write(chip, port, PORT_ASSOC_VECTOR, reg);
2750 if (err)
2751 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002752
2753 /* Egress rate control 2: disable egress rate control. */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002754 err = mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL_2, 0x0000);
2755 if (err)
2756 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002757
Vivien Didelotfad09c72016-06-21 12:28:20 -04002758 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2759 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2760 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002761 /* Do not limit the period of time that this port can
2762 * be paused for by the remote end or the period of
2763 * time that this port can pause the remote end.
2764 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002765 err = mv88e6xxx_port_write(chip, port, PORT_PAUSE_CTRL, 0x0000);
2766 if (err)
2767 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002768
2769 /* Port ATU control: disable limiting the number of
2770 * address database entries that this port is allowed
2771 * to use.
2772 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002773 err = mv88e6xxx_port_write(chip, port, PORT_ATU_CONTROL,
2774 0x0000);
Andrew Lunn54d792f2015-05-06 01:09:47 +02002775 /* Priority Override: disable DA, SA and VTU priority
2776 * override.
2777 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002778 err = mv88e6xxx_port_write(chip, port, PORT_PRI_OVERRIDE,
2779 0x0000);
2780 if (err)
2781 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002782
2783 /* Port Ethertype: use the Ethertype DSA Ethertype
2784 * value.
2785 */
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002786 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA)) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002787 err = mv88e6xxx_port_write(chip, port, PORT_ETH_TYPE,
2788 ETH_P_EDSA);
2789 if (err)
2790 return err;
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002791 }
2792
Andrew Lunn54d792f2015-05-06 01:09:47 +02002793 /* Tag Remap: use an identity 802.1p prio -> switch
2794 * prio mapping.
2795 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002796 err = mv88e6xxx_port_write(chip, port, PORT_TAG_REGMAP_0123,
2797 0x3210);
2798 if (err)
2799 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002800
2801 /* Tag Remap 2: use an identity 802.1p prio -> switch
2802 * prio mapping.
2803 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002804 err = mv88e6xxx_port_write(chip, port, PORT_TAG_REGMAP_4567,
2805 0x7654);
2806 if (err)
2807 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002808 }
2809
Jamie Lentin1bc261f2016-08-22 22:47:08 +01002810 /* Rate Control: disable ingress rate limiting. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002811 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2812 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
Vivien Didelotfad09c72016-06-21 12:28:20 -04002813 mv88e6xxx_6320_family(chip)) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002814 err = mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL,
2815 0x0001);
2816 if (err)
2817 return err;
Jamie Lentin1bc261f2016-08-22 22:47:08 +01002818 } else if (mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip)) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002819 err = mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL,
2820 0x0000);
2821 if (err)
2822 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002823 }
2824
Guenter Roeck366f0a02015-03-26 18:36:30 -07002825 /* Port Control 1: disable trunking, disable sending
2826 * learning messages to this port.
Guenter Roeckd827e882015-03-26 18:36:29 -07002827 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002828 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_1, 0x0000);
2829 if (err)
2830 return err;
Guenter Roeckd827e882015-03-26 18:36:29 -07002831
Vivien Didelot207afda2016-04-14 14:42:09 -04002832 /* Port based VLAN map: give each port the same default address
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002833 * database, and allow bidirectional communication between the
2834 * CPU and DSA port(s), and the other ports.
Guenter Roeckd827e882015-03-26 18:36:29 -07002835 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002836 err = _mv88e6xxx_port_fid_set(chip, port, 0);
2837 if (err)
2838 return err;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002839
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002840 err = _mv88e6xxx_port_based_vlan_map(chip, port);
2841 if (err)
2842 return err;
Guenter Roeckd827e882015-03-26 18:36:29 -07002843
2844 /* Default VLAN ID and priority: don't set a default VLAN
2845 * ID, and set the default packet priority to zero.
2846 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002847 return mv88e6xxx_port_write(chip, port, PORT_DEFAULT_VLAN, 0x0000);
Andrew Lunndbde9e62015-05-06 01:09:48 +02002848}
2849
Wei Yongjunaa0938c2016-10-18 15:53:37 +00002850static int mv88e6xxx_g1_set_switch_mac(struct mv88e6xxx_chip *chip, u8 *addr)
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002851{
2852 int err;
2853
Vivien Didelota935c052016-09-29 12:21:53 -04002854 err = mv88e6xxx_g1_write(chip, GLOBAL_MAC_01, (addr[0] << 8) | addr[1]);
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002855 if (err)
2856 return err;
2857
Vivien Didelota935c052016-09-29 12:21:53 -04002858 err = mv88e6xxx_g1_write(chip, GLOBAL_MAC_23, (addr[2] << 8) | addr[3]);
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002859 if (err)
2860 return err;
2861
Vivien Didelota935c052016-09-29 12:21:53 -04002862 err = mv88e6xxx_g1_write(chip, GLOBAL_MAC_45, (addr[4] << 8) | addr[5]);
2863 if (err)
2864 return err;
2865
2866 return 0;
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002867}
2868
Vivien Didelotacddbd22016-07-18 20:45:39 -04002869static int mv88e6xxx_g1_set_age_time(struct mv88e6xxx_chip *chip,
2870 unsigned int msecs)
2871{
2872 const unsigned int coeff = chip->info->age_time_coeff;
2873 const unsigned int min = 0x01 * coeff;
2874 const unsigned int max = 0xff * coeff;
2875 u8 age_time;
2876 u16 val;
2877 int err;
2878
2879 if (msecs < min || msecs > max)
2880 return -ERANGE;
2881
2882 /* Round to nearest multiple of coeff */
2883 age_time = (msecs + coeff / 2) / coeff;
2884
Vivien Didelota935c052016-09-29 12:21:53 -04002885 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_CONTROL, &val);
Vivien Didelotacddbd22016-07-18 20:45:39 -04002886 if (err)
2887 return err;
2888
2889 /* AgeTime is 11:4 bits */
2890 val &= ~0xff0;
2891 val |= age_time << 4;
2892
Vivien Didelota935c052016-09-29 12:21:53 -04002893 return mv88e6xxx_g1_write(chip, GLOBAL_ATU_CONTROL, val);
Vivien Didelotacddbd22016-07-18 20:45:39 -04002894}
2895
Vivien Didelot2cfcd962016-07-18 20:45:40 -04002896static int mv88e6xxx_set_ageing_time(struct dsa_switch *ds,
2897 unsigned int ageing_time)
2898{
Vivien Didelot04bed142016-08-31 18:06:13 -04002899 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot2cfcd962016-07-18 20:45:40 -04002900 int err;
2901
2902 mutex_lock(&chip->reg_lock);
2903 err = mv88e6xxx_g1_set_age_time(chip, ageing_time);
2904 mutex_unlock(&chip->reg_lock);
2905
2906 return err;
2907}
2908
Vivien Didelot97299342016-07-18 20:45:30 -04002909static int mv88e6xxx_g1_setup(struct mv88e6xxx_chip *chip)
Vivien Didelot08a01262016-05-09 13:22:50 -04002910{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002911 struct dsa_switch *ds = chip->ds;
Vivien Didelotb0745e872016-05-09 13:22:53 -04002912 u32 upstream_port = dsa_upstream_port(ds);
Vivien Didelot119477b2016-05-09 13:22:51 -04002913 u16 reg;
Vivien Didelot08a01262016-05-09 13:22:50 -04002914 int err;
Vivien Didelot08a01262016-05-09 13:22:50 -04002915
Vivien Didelot119477b2016-05-09 13:22:51 -04002916 /* Enable the PHY Polling Unit if present, don't discard any packets,
2917 * and mask all interrupt sources.
2918 */
Andrew Lunndc30c352016-10-16 19:56:49 +02002919 err = mv88e6xxx_g1_read(chip, GLOBAL_CONTROL, &reg);
2920 if (err < 0)
2921 return err;
2922
2923 reg &= ~GLOBAL_CONTROL_PPU_ENABLE;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002924 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU) ||
2925 mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE))
Vivien Didelot119477b2016-05-09 13:22:51 -04002926 reg |= GLOBAL_CONTROL_PPU_ENABLE;
2927
Vivien Didelota935c052016-09-29 12:21:53 -04002928 err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL, reg);
Vivien Didelot119477b2016-05-09 13:22:51 -04002929 if (err)
2930 return err;
2931
Vivien Didelotb0745e872016-05-09 13:22:53 -04002932 /* Configure the upstream port, and configure it as the port to which
2933 * ingress and egress and ARP monitor frames are to be sent.
2934 */
2935 reg = upstream_port << GLOBAL_MONITOR_CONTROL_INGRESS_SHIFT |
2936 upstream_port << GLOBAL_MONITOR_CONTROL_EGRESS_SHIFT |
2937 upstream_port << GLOBAL_MONITOR_CONTROL_ARP_SHIFT;
Vivien Didelota935c052016-09-29 12:21:53 -04002938 err = mv88e6xxx_g1_write(chip, GLOBAL_MONITOR_CONTROL, reg);
Vivien Didelotb0745e872016-05-09 13:22:53 -04002939 if (err)
2940 return err;
2941
Vivien Didelot50484ff2016-05-09 13:22:54 -04002942 /* Disable remote management, and set the switch's DSA device number. */
Vivien Didelota935c052016-09-29 12:21:53 -04002943 err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL_2,
2944 GLOBAL_CONTROL_2_MULTIPLE_CASCADE |
2945 (ds->index & 0x1f));
Vivien Didelot50484ff2016-05-09 13:22:54 -04002946 if (err)
2947 return err;
2948
Vivien Didelotacddbd22016-07-18 20:45:39 -04002949 /* Clear all the VTU and STU entries */
2950 err = _mv88e6xxx_vtu_stu_flush(chip);
2951 if (err < 0)
2952 return err;
2953
Vivien Didelot08a01262016-05-09 13:22:50 -04002954 /* Set the default address aging time to 5 minutes, and
2955 * enable address learn messages to be sent to all message
2956 * ports.
2957 */
Vivien Didelota935c052016-09-29 12:21:53 -04002958 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_CONTROL,
2959 GLOBAL_ATU_CONTROL_LEARN2ALL);
Vivien Didelot08a01262016-05-09 13:22:50 -04002960 if (err)
2961 return err;
2962
Vivien Didelotacddbd22016-07-18 20:45:39 -04002963 err = mv88e6xxx_g1_set_age_time(chip, 300000);
2964 if (err)
Vivien Didelot97299342016-07-18 20:45:30 -04002965 return err;
2966
2967 /* Clear all ATU entries */
2968 err = _mv88e6xxx_atu_flush(chip, 0, true);
2969 if (err)
2970 return err;
2971
Vivien Didelot08a01262016-05-09 13:22:50 -04002972 /* Configure the IP ToS mapping registers. */
Vivien Didelota935c052016-09-29 12:21:53 -04002973 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_0, 0x0000);
Vivien Didelot08a01262016-05-09 13:22:50 -04002974 if (err)
2975 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002976 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_1, 0x0000);
Vivien Didelot08a01262016-05-09 13:22:50 -04002977 if (err)
2978 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002979 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_2, 0x5555);
Vivien Didelot08a01262016-05-09 13:22:50 -04002980 if (err)
2981 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002982 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_3, 0x5555);
Vivien Didelot08a01262016-05-09 13:22:50 -04002983 if (err)
2984 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002985 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_4, 0xaaaa);
Vivien Didelot08a01262016-05-09 13:22:50 -04002986 if (err)
2987 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002988 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_5, 0xaaaa);
Vivien Didelot08a01262016-05-09 13:22:50 -04002989 if (err)
2990 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002991 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_6, 0xffff);
Vivien Didelot08a01262016-05-09 13:22:50 -04002992 if (err)
2993 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002994 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_7, 0xffff);
Vivien Didelot08a01262016-05-09 13:22:50 -04002995 if (err)
2996 return err;
2997
2998 /* Configure the IEEE 802.1p priority mapping register. */
Vivien Didelota935c052016-09-29 12:21:53 -04002999 err = mv88e6xxx_g1_write(chip, GLOBAL_IEEE_PRI, 0xfa41);
Vivien Didelot08a01262016-05-09 13:22:50 -04003000 if (err)
3001 return err;
3002
Vivien Didelot97299342016-07-18 20:45:30 -04003003 /* Clear the statistics counters for all ports */
Vivien Didelota935c052016-09-29 12:21:53 -04003004 err = mv88e6xxx_g1_write(chip, GLOBAL_STATS_OP,
3005 GLOBAL_STATS_OP_FLUSH_ALL);
Vivien Didelot97299342016-07-18 20:45:30 -04003006 if (err)
3007 return err;
3008
3009 /* Wait for the flush to complete. */
3010 err = _mv88e6xxx_stats_wait(chip);
3011 if (err)
3012 return err;
3013
3014 return 0;
3015}
3016
Vivien Didelotf81ec902016-05-09 13:22:58 -04003017static int mv88e6xxx_setup(struct dsa_switch *ds)
Guenter Roeckacdaffc2015-03-26 18:36:28 -07003018{
Vivien Didelot04bed142016-08-31 18:06:13 -04003019 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot552238b2016-05-09 13:22:49 -04003020 int err;
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003021 int i;
3022
Vivien Didelotfad09c72016-06-21 12:28:20 -04003023 chip->ds = ds;
3024 ds->slave_mii_bus = chip->mdio_bus;
Vivien Didelot552238b2016-05-09 13:22:49 -04003025
Vivien Didelotfad09c72016-06-21 12:28:20 -04003026 mutex_lock(&chip->reg_lock);
Vivien Didelot552238b2016-05-09 13:22:49 -04003027
Vivien Didelot97299342016-07-18 20:45:30 -04003028 /* Setup Switch Port Registers */
Vivien Didelot370b4ff2016-09-29 12:21:57 -04003029 for (i = 0; i < mv88e6xxx_num_ports(chip); i++) {
Vivien Didelot97299342016-07-18 20:45:30 -04003030 err = mv88e6xxx_setup_port(chip, i);
3031 if (err)
3032 goto unlock;
3033 }
3034
3035 /* Setup Switch Global 1 Registers */
3036 err = mv88e6xxx_g1_setup(chip);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003037 if (err)
3038 goto unlock;
3039
Vivien Didelot97299342016-07-18 20:45:30 -04003040 /* Setup Switch Global 2 Registers */
3041 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_GLOBAL2)) {
3042 err = mv88e6xxx_g2_setup(chip);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003043 if (err)
3044 goto unlock;
3045 }
Andrew Lunn54d792f2015-05-06 01:09:47 +02003046
Vivien Didelot6b17e862015-08-13 12:52:18 -04003047unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04003048 mutex_unlock(&chip->reg_lock);
Andrew Lunndb687a52015-06-20 21:31:29 +02003049
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003050 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02003051}
3052
Vivien Didelot3b4caa12016-07-18 20:45:34 -04003053static int mv88e6xxx_set_addr(struct dsa_switch *ds, u8 *addr)
3054{
Vivien Didelot04bed142016-08-31 18:06:13 -04003055 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot3b4caa12016-07-18 20:45:34 -04003056 int err;
3057
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003058 if (!chip->info->ops->set_switch_mac)
3059 return -EOPNOTSUPP;
3060
Vivien Didelot3b4caa12016-07-18 20:45:34 -04003061 mutex_lock(&chip->reg_lock);
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003062 err = chip->info->ops->set_switch_mac(chip, addr);
Vivien Didelot3b4caa12016-07-18 20:45:34 -04003063 mutex_unlock(&chip->reg_lock);
3064
3065 return err;
3066}
3067
Vivien Didelote57e5e72016-08-15 17:19:00 -04003068static int mv88e6xxx_mdio_read(struct mii_bus *bus, int phy, int reg)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003069{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003070 struct mv88e6xxx_chip *chip = bus->priv;
Vivien Didelote57e5e72016-08-15 17:19:00 -04003071 u16 val;
3072 int err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003073
Vivien Didelot370b4ff2016-09-29 12:21:57 -04003074 if (phy >= mv88e6xxx_num_ports(chip))
Andrew Lunn158bc062016-04-28 21:24:06 -04003075 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003076
Vivien Didelotfad09c72016-06-21 12:28:20 -04003077 mutex_lock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04003078 err = mv88e6xxx_phy_read(chip, phy, reg, &val);
Vivien Didelotfad09c72016-06-21 12:28:20 -04003079 mutex_unlock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04003080
3081 return err ? err : val;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003082}
3083
Vivien Didelote57e5e72016-08-15 17:19:00 -04003084static int mv88e6xxx_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003085{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003086 struct mv88e6xxx_chip *chip = bus->priv;
Vivien Didelote57e5e72016-08-15 17:19:00 -04003087 int err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003088
Vivien Didelot370b4ff2016-09-29 12:21:57 -04003089 if (phy >= mv88e6xxx_num_ports(chip))
Andrew Lunn158bc062016-04-28 21:24:06 -04003090 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003091
Vivien Didelotfad09c72016-06-21 12:28:20 -04003092 mutex_lock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04003093 err = mv88e6xxx_phy_write(chip, phy, reg, val);
Vivien Didelotfad09c72016-06-21 12:28:20 -04003094 mutex_unlock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04003095
3096 return err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003097}
3098
Vivien Didelotfad09c72016-06-21 12:28:20 -04003099static int mv88e6xxx_mdio_register(struct mv88e6xxx_chip *chip,
Andrew Lunnb516d452016-06-04 21:17:06 +02003100 struct device_node *np)
3101{
3102 static int index;
3103 struct mii_bus *bus;
3104 int err;
3105
Andrew Lunnb516d452016-06-04 21:17:06 +02003106 if (np)
Vivien Didelotfad09c72016-06-21 12:28:20 -04003107 chip->mdio_np = of_get_child_by_name(np, "mdio");
Andrew Lunnb516d452016-06-04 21:17:06 +02003108
Vivien Didelotfad09c72016-06-21 12:28:20 -04003109 bus = devm_mdiobus_alloc(chip->dev);
Andrew Lunnb516d452016-06-04 21:17:06 +02003110 if (!bus)
3111 return -ENOMEM;
3112
Vivien Didelotfad09c72016-06-21 12:28:20 -04003113 bus->priv = (void *)chip;
Andrew Lunnb516d452016-06-04 21:17:06 +02003114 if (np) {
3115 bus->name = np->full_name;
3116 snprintf(bus->id, MII_BUS_ID_SIZE, "%s", np->full_name);
3117 } else {
3118 bus->name = "mv88e6xxx SMI";
3119 snprintf(bus->id, MII_BUS_ID_SIZE, "mv88e6xxx-%d", index++);
3120 }
3121
3122 bus->read = mv88e6xxx_mdio_read;
3123 bus->write = mv88e6xxx_mdio_write;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003124 bus->parent = chip->dev;
Andrew Lunnb516d452016-06-04 21:17:06 +02003125
Vivien Didelotfad09c72016-06-21 12:28:20 -04003126 if (chip->mdio_np)
3127 err = of_mdiobus_register(bus, chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003128 else
3129 err = mdiobus_register(bus);
3130 if (err) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04003131 dev_err(chip->dev, "Cannot register MDIO bus (%d)\n", err);
Andrew Lunnb516d452016-06-04 21:17:06 +02003132 goto out;
3133 }
Vivien Didelotfad09c72016-06-21 12:28:20 -04003134 chip->mdio_bus = bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003135
3136 return 0;
3137
3138out:
Vivien Didelotfad09c72016-06-21 12:28:20 -04003139 if (chip->mdio_np)
3140 of_node_put(chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003141
3142 return err;
3143}
3144
Vivien Didelotfad09c72016-06-21 12:28:20 -04003145static void mv88e6xxx_mdio_unregister(struct mv88e6xxx_chip *chip)
Andrew Lunnb516d452016-06-04 21:17:06 +02003146
3147{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003148 struct mii_bus *bus = chip->mdio_bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003149
3150 mdiobus_unregister(bus);
3151
Vivien Didelotfad09c72016-06-21 12:28:20 -04003152 if (chip->mdio_np)
3153 of_node_put(chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003154}
3155
Guenter Roeckc22995c2015-07-25 09:42:28 -07003156#ifdef CONFIG_NET_DSA_HWMON
3157
3158static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
3159{
Vivien Didelot04bed142016-08-31 18:06:13 -04003160 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot9c938292016-08-15 17:19:02 -04003161 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003162 int ret;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003163
3164 *temp = 0;
3165
Vivien Didelotfad09c72016-06-21 12:28:20 -04003166 mutex_lock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003167
Vivien Didelot9c938292016-08-15 17:19:02 -04003168 ret = mv88e6xxx_phy_write(chip, 0x0, 0x16, 0x6);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003169 if (ret < 0)
3170 goto error;
3171
3172 /* Enable temperature sensor */
Vivien Didelot9c938292016-08-15 17:19:02 -04003173 ret = mv88e6xxx_phy_read(chip, 0x0, 0x1a, &val);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003174 if (ret < 0)
3175 goto error;
3176
Vivien Didelot9c938292016-08-15 17:19:02 -04003177 ret = mv88e6xxx_phy_write(chip, 0x0, 0x1a, val | (1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003178 if (ret < 0)
3179 goto error;
3180
3181 /* Wait for temperature to stabilize */
3182 usleep_range(10000, 12000);
3183
Vivien Didelot9c938292016-08-15 17:19:02 -04003184 ret = mv88e6xxx_phy_read(chip, 0x0, 0x1a, &val);
3185 if (ret < 0)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003186 goto error;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003187
3188 /* Disable temperature sensor */
Vivien Didelot9c938292016-08-15 17:19:02 -04003189 ret = mv88e6xxx_phy_write(chip, 0x0, 0x1a, val & ~(1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003190 if (ret < 0)
3191 goto error;
3192
3193 *temp = ((val & 0x1f) - 5) * 5;
3194
3195error:
Vivien Didelot9c938292016-08-15 17:19:02 -04003196 mv88e6xxx_phy_write(chip, 0x0, 0x16, 0x0);
Vivien Didelotfad09c72016-06-21 12:28:20 -04003197 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003198 return ret;
3199}
3200
3201static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
3202{
Vivien Didelot04bed142016-08-31 18:06:13 -04003203 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003204 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003205 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003206 int ret;
3207
3208 *temp = 0;
3209
Vivien Didelot9c938292016-08-15 17:19:02 -04003210 mutex_lock(&chip->reg_lock);
3211 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 27, &val);
3212 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003213 if (ret < 0)
3214 return ret;
3215
Vivien Didelot9c938292016-08-15 17:19:02 -04003216 *temp = (val & 0xff) - 25;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003217
3218 return 0;
3219}
3220
Vivien Didelotf81ec902016-05-09 13:22:58 -04003221static int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003222{
Vivien Didelot04bed142016-08-31 18:06:13 -04003223 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn158bc062016-04-28 21:24:06 -04003224
Vivien Didelotfad09c72016-06-21 12:28:20 -04003225 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP))
Vivien Didelot6594f612016-05-09 13:22:42 -04003226 return -EOPNOTSUPP;
3227
Vivien Didelotfad09c72016-06-21 12:28:20 -04003228 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003229 return mv88e63xx_get_temp(ds, temp);
3230
3231 return mv88e61xx_get_temp(ds, temp);
3232}
3233
Vivien Didelotf81ec902016-05-09 13:22:58 -04003234static int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003235{
Vivien Didelot04bed142016-08-31 18:06:13 -04003236 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003237 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003238 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003239 int ret;
3240
Vivien Didelotfad09c72016-06-21 12:28:20 -04003241 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003242 return -EOPNOTSUPP;
3243
3244 *temp = 0;
3245
Vivien Didelot9c938292016-08-15 17:19:02 -04003246 mutex_lock(&chip->reg_lock);
3247 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3248 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003249 if (ret < 0)
3250 return ret;
3251
Vivien Didelot9c938292016-08-15 17:19:02 -04003252 *temp = (((val >> 8) & 0x1f) * 5) - 25;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003253
3254 return 0;
3255}
3256
Vivien Didelotf81ec902016-05-09 13:22:58 -04003257static int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003258{
Vivien Didelot04bed142016-08-31 18:06:13 -04003259 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003260 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003261 u16 val;
3262 int err;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003263
Vivien Didelotfad09c72016-06-21 12:28:20 -04003264 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003265 return -EOPNOTSUPP;
3266
Vivien Didelot9c938292016-08-15 17:19:02 -04003267 mutex_lock(&chip->reg_lock);
3268 err = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3269 if (err)
3270 goto unlock;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003271 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
Vivien Didelot9c938292016-08-15 17:19:02 -04003272 err = mv88e6xxx_phy_page_write(chip, phy, 6, 26,
3273 (val & 0xe0ff) | (temp << 8));
3274unlock:
3275 mutex_unlock(&chip->reg_lock);
3276
3277 return err;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003278}
3279
Vivien Didelotf81ec902016-05-09 13:22:58 -04003280static int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003281{
Vivien Didelot04bed142016-08-31 18:06:13 -04003282 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003283 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003284 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003285 int ret;
3286
Vivien Didelotfad09c72016-06-21 12:28:20 -04003287 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003288 return -EOPNOTSUPP;
3289
3290 *alarm = false;
3291
Vivien Didelot9c938292016-08-15 17:19:02 -04003292 mutex_lock(&chip->reg_lock);
3293 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3294 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003295 if (ret < 0)
3296 return ret;
3297
Vivien Didelot9c938292016-08-15 17:19:02 -04003298 *alarm = !!(val & 0x40);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003299
3300 return 0;
3301}
3302#endif /* CONFIG_NET_DSA_HWMON */
3303
Vivien Didelot855b1932016-07-20 18:18:35 -04003304static int mv88e6xxx_get_eeprom_len(struct dsa_switch *ds)
3305{
Vivien Didelot04bed142016-08-31 18:06:13 -04003306 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot855b1932016-07-20 18:18:35 -04003307
3308 return chip->eeprom_len;
3309}
3310
Vivien Didelot855b1932016-07-20 18:18:35 -04003311static int mv88e6xxx_get_eeprom(struct dsa_switch *ds,
3312 struct ethtool_eeprom *eeprom, u8 *data)
3313{
Vivien Didelot04bed142016-08-31 18:06:13 -04003314 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot855b1932016-07-20 18:18:35 -04003315 int err;
3316
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003317 if (!chip->info->ops->get_eeprom)
3318 return -EOPNOTSUPP;
3319
Vivien Didelot855b1932016-07-20 18:18:35 -04003320 mutex_lock(&chip->reg_lock);
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003321 err = chip->info->ops->get_eeprom(chip, eeprom, data);
Vivien Didelot855b1932016-07-20 18:18:35 -04003322 mutex_unlock(&chip->reg_lock);
3323
3324 if (err)
3325 return err;
3326
3327 eeprom->magic = 0xc3ec4951;
3328
3329 return 0;
3330}
3331
Vivien Didelot855b1932016-07-20 18:18:35 -04003332static int mv88e6xxx_set_eeprom(struct dsa_switch *ds,
3333 struct ethtool_eeprom *eeprom, u8 *data)
3334{
Vivien Didelot04bed142016-08-31 18:06:13 -04003335 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot855b1932016-07-20 18:18:35 -04003336 int err;
3337
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003338 if (!chip->info->ops->set_eeprom)
3339 return -EOPNOTSUPP;
3340
Vivien Didelot855b1932016-07-20 18:18:35 -04003341 if (eeprom->magic != 0xc3ec4951)
3342 return -EINVAL;
3343
3344 mutex_lock(&chip->reg_lock);
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003345 err = chip->info->ops->set_eeprom(chip, eeprom, data);
Vivien Didelot855b1932016-07-20 18:18:35 -04003346 mutex_unlock(&chip->reg_lock);
3347
3348 return err;
3349}
3350
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003351static const struct mv88e6xxx_ops mv88e6085_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003352 .set_switch_mac = mv88e6xxx_g1_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003353 .phy_read = mv88e6xxx_phy_ppu_read,
3354 .phy_write = mv88e6xxx_phy_ppu_write,
3355};
3356
3357static const struct mv88e6xxx_ops mv88e6095_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003358 .set_switch_mac = mv88e6xxx_g1_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003359 .phy_read = mv88e6xxx_phy_ppu_read,
3360 .phy_write = mv88e6xxx_phy_ppu_write,
3361};
3362
3363static const struct mv88e6xxx_ops mv88e6123_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003364 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003365 .phy_read = mv88e6xxx_read,
3366 .phy_write = mv88e6xxx_write,
3367};
3368
3369static const struct mv88e6xxx_ops mv88e6131_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003370 .set_switch_mac = mv88e6xxx_g1_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003371 .phy_read = mv88e6xxx_phy_ppu_read,
3372 .phy_write = mv88e6xxx_phy_ppu_write,
3373};
3374
3375static const struct mv88e6xxx_ops mv88e6161_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003376 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003377 .phy_read = mv88e6xxx_read,
3378 .phy_write = mv88e6xxx_write,
3379};
3380
3381static const struct mv88e6xxx_ops mv88e6165_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003382 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003383 .phy_read = mv88e6xxx_read,
3384 .phy_write = mv88e6xxx_write,
3385};
3386
3387static const struct mv88e6xxx_ops mv88e6171_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003388 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003389 .phy_read = mv88e6xxx_g2_smi_phy_read,
3390 .phy_write = mv88e6xxx_g2_smi_phy_write,
3391};
3392
3393static const struct mv88e6xxx_ops mv88e6172_ops = {
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003394 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3395 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003396 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003397 .phy_read = mv88e6xxx_g2_smi_phy_read,
3398 .phy_write = mv88e6xxx_g2_smi_phy_write,
3399};
3400
3401static const struct mv88e6xxx_ops mv88e6175_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003402 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003403 .phy_read = mv88e6xxx_g2_smi_phy_read,
3404 .phy_write = mv88e6xxx_g2_smi_phy_write,
3405};
3406
3407static const struct mv88e6xxx_ops mv88e6176_ops = {
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003408 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3409 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003410 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003411 .phy_read = mv88e6xxx_g2_smi_phy_read,
3412 .phy_write = mv88e6xxx_g2_smi_phy_write,
3413};
3414
3415static const struct mv88e6xxx_ops mv88e6185_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003416 .set_switch_mac = mv88e6xxx_g1_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003417 .phy_read = mv88e6xxx_phy_ppu_read,
3418 .phy_write = mv88e6xxx_phy_ppu_write,
3419};
3420
3421static const struct mv88e6xxx_ops mv88e6240_ops = {
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003422 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3423 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003424 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003425 .phy_read = mv88e6xxx_g2_smi_phy_read,
3426 .phy_write = mv88e6xxx_g2_smi_phy_write,
3427};
3428
3429static const struct mv88e6xxx_ops mv88e6320_ops = {
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003430 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3431 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003432 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003433 .phy_read = mv88e6xxx_g2_smi_phy_read,
3434 .phy_write = mv88e6xxx_g2_smi_phy_write,
3435};
3436
3437static const struct mv88e6xxx_ops mv88e6321_ops = {
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003438 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3439 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003440 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003441 .phy_read = mv88e6xxx_g2_smi_phy_read,
3442 .phy_write = mv88e6xxx_g2_smi_phy_write,
3443};
3444
3445static const struct mv88e6xxx_ops mv88e6350_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003446 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003447 .phy_read = mv88e6xxx_g2_smi_phy_read,
3448 .phy_write = mv88e6xxx_g2_smi_phy_write,
3449};
3450
3451static const struct mv88e6xxx_ops mv88e6351_ops = {
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003452 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003453 .phy_read = mv88e6xxx_g2_smi_phy_read,
3454 .phy_write = mv88e6xxx_g2_smi_phy_write,
3455};
3456
3457static const struct mv88e6xxx_ops mv88e6352_ops = {
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04003458 .get_eeprom = mv88e6xxx_g2_get_eeprom16,
3459 .set_eeprom = mv88e6xxx_g2_set_eeprom16,
Vivien Didelotb073d4e2016-09-29 12:22:01 -04003460 .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003461 .phy_read = mv88e6xxx_g2_smi_phy_read,
3462 .phy_write = mv88e6xxx_g2_smi_phy_write,
3463};
3464
Vivien Didelotf81ec902016-05-09 13:22:58 -04003465static const struct mv88e6xxx_info mv88e6xxx_table[] = {
3466 [MV88E6085] = {
3467 .prod_num = PORT_SWITCH_ID_PROD_NUM_6085,
3468 .family = MV88E6XXX_FAMILY_6097,
3469 .name = "Marvell 88E6085",
3470 .num_databases = 4096,
3471 .num_ports = 10,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003472 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003473 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003474 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003475 .g1_irqs = 8,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003476 .flags = MV88E6XXX_FLAGS_FAMILY_6097,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003477 .ops = &mv88e6085_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003478 },
3479
3480 [MV88E6095] = {
3481 .prod_num = PORT_SWITCH_ID_PROD_NUM_6095,
3482 .family = MV88E6XXX_FAMILY_6095,
3483 .name = "Marvell 88E6095/88E6095F",
3484 .num_databases = 256,
3485 .num_ports = 11,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003486 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003487 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003488 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003489 .g1_irqs = 8,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003490 .flags = MV88E6XXX_FLAGS_FAMILY_6095,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003491 .ops = &mv88e6095_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003492 },
3493
3494 [MV88E6123] = {
3495 .prod_num = PORT_SWITCH_ID_PROD_NUM_6123,
3496 .family = MV88E6XXX_FAMILY_6165,
3497 .name = "Marvell 88E6123",
3498 .num_databases = 4096,
3499 .num_ports = 3,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003500 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003501 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003502 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003503 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003504 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003505 .ops = &mv88e6123_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003506 },
3507
3508 [MV88E6131] = {
3509 .prod_num = PORT_SWITCH_ID_PROD_NUM_6131,
3510 .family = MV88E6XXX_FAMILY_6185,
3511 .name = "Marvell 88E6131",
3512 .num_databases = 256,
3513 .num_ports = 8,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003514 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003515 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003516 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003517 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003518 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003519 .ops = &mv88e6131_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003520 },
3521
3522 [MV88E6161] = {
3523 .prod_num = PORT_SWITCH_ID_PROD_NUM_6161,
3524 .family = MV88E6XXX_FAMILY_6165,
3525 .name = "Marvell 88E6161",
3526 .num_databases = 4096,
3527 .num_ports = 6,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003528 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003529 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003530 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003531 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003532 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003533 .ops = &mv88e6161_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003534 },
3535
3536 [MV88E6165] = {
3537 .prod_num = PORT_SWITCH_ID_PROD_NUM_6165,
3538 .family = MV88E6XXX_FAMILY_6165,
3539 .name = "Marvell 88E6165",
3540 .num_databases = 4096,
3541 .num_ports = 6,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003542 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003543 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003544 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003545 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003546 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003547 .ops = &mv88e6165_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003548 },
3549
3550 [MV88E6171] = {
3551 .prod_num = PORT_SWITCH_ID_PROD_NUM_6171,
3552 .family = MV88E6XXX_FAMILY_6351,
3553 .name = "Marvell 88E6171",
3554 .num_databases = 4096,
3555 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003556 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003557 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003558 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003559 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003560 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003561 .ops = &mv88e6171_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003562 },
3563
3564 [MV88E6172] = {
3565 .prod_num = PORT_SWITCH_ID_PROD_NUM_6172,
3566 .family = MV88E6XXX_FAMILY_6352,
3567 .name = "Marvell 88E6172",
3568 .num_databases = 4096,
3569 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003570 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003571 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003572 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003573 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003574 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003575 .ops = &mv88e6172_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003576 },
3577
3578 [MV88E6175] = {
3579 .prod_num = PORT_SWITCH_ID_PROD_NUM_6175,
3580 .family = MV88E6XXX_FAMILY_6351,
3581 .name = "Marvell 88E6175",
3582 .num_databases = 4096,
3583 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003584 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003585 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003586 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003587 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003588 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003589 .ops = &mv88e6175_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003590 },
3591
3592 [MV88E6176] = {
3593 .prod_num = PORT_SWITCH_ID_PROD_NUM_6176,
3594 .family = MV88E6XXX_FAMILY_6352,
3595 .name = "Marvell 88E6176",
3596 .num_databases = 4096,
3597 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003598 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003599 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003600 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003601 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003602 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003603 .ops = &mv88e6176_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003604 },
3605
3606 [MV88E6185] = {
3607 .prod_num = PORT_SWITCH_ID_PROD_NUM_6185,
3608 .family = MV88E6XXX_FAMILY_6185,
3609 .name = "Marvell 88E6185",
3610 .num_databases = 256,
3611 .num_ports = 10,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003612 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003613 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003614 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003615 .g1_irqs = 8,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003616 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003617 .ops = &mv88e6185_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003618 },
3619
3620 [MV88E6240] = {
3621 .prod_num = PORT_SWITCH_ID_PROD_NUM_6240,
3622 .family = MV88E6XXX_FAMILY_6352,
3623 .name = "Marvell 88E6240",
3624 .num_databases = 4096,
3625 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003626 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003627 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003628 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003629 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003630 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003631 .ops = &mv88e6240_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003632 },
3633
3634 [MV88E6320] = {
3635 .prod_num = PORT_SWITCH_ID_PROD_NUM_6320,
3636 .family = MV88E6XXX_FAMILY_6320,
3637 .name = "Marvell 88E6320",
3638 .num_databases = 4096,
3639 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003640 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003641 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003642 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003643 .g1_irqs = 8,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003644 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003645 .ops = &mv88e6320_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003646 },
3647
3648 [MV88E6321] = {
3649 .prod_num = PORT_SWITCH_ID_PROD_NUM_6321,
3650 .family = MV88E6XXX_FAMILY_6320,
3651 .name = "Marvell 88E6321",
3652 .num_databases = 4096,
3653 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003654 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003655 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003656 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003657 .g1_irqs = 8,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003658 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003659 .ops = &mv88e6321_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003660 },
3661
3662 [MV88E6350] = {
3663 .prod_num = PORT_SWITCH_ID_PROD_NUM_6350,
3664 .family = MV88E6XXX_FAMILY_6351,
3665 .name = "Marvell 88E6350",
3666 .num_databases = 4096,
3667 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003668 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003669 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003670 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003671 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003672 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003673 .ops = &mv88e6350_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003674 },
3675
3676 [MV88E6351] = {
3677 .prod_num = PORT_SWITCH_ID_PROD_NUM_6351,
3678 .family = MV88E6XXX_FAMILY_6351,
3679 .name = "Marvell 88E6351",
3680 .num_databases = 4096,
3681 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003682 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003683 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003684 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003685 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003686 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003687 .ops = &mv88e6351_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003688 },
3689
3690 [MV88E6352] = {
3691 .prod_num = PORT_SWITCH_ID_PROD_NUM_6352,
3692 .family = MV88E6XXX_FAMILY_6352,
3693 .name = "Marvell 88E6352",
3694 .num_databases = 4096,
3695 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003696 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003697 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003698 .age_time_coeff = 15000,
Andrew Lunndc30c352016-10-16 19:56:49 +02003699 .g1_irqs = 9,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003700 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003701 .ops = &mv88e6352_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003702 },
3703};
3704
Vivien Didelot5f7c0362016-06-20 13:14:04 -04003705static const struct mv88e6xxx_info *mv88e6xxx_lookup_info(unsigned int prod_num)
Vivien Didelotb9b37712015-10-30 19:39:48 -04003706{
Vivien Didelota439c062016-04-17 13:23:58 -04003707 int i;
Vivien Didelotb9b37712015-10-30 19:39:48 -04003708
Vivien Didelot5f7c0362016-06-20 13:14:04 -04003709 for (i = 0; i < ARRAY_SIZE(mv88e6xxx_table); ++i)
3710 if (mv88e6xxx_table[i].prod_num == prod_num)
3711 return &mv88e6xxx_table[i];
Vivien Didelotb9b37712015-10-30 19:39:48 -04003712
Vivien Didelotb9b37712015-10-30 19:39:48 -04003713 return NULL;
3714}
3715
Vivien Didelotfad09c72016-06-21 12:28:20 -04003716static int mv88e6xxx_detect(struct mv88e6xxx_chip *chip)
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003717{
3718 const struct mv88e6xxx_info *info;
Vivien Didelot8f6345b2016-07-20 18:18:36 -04003719 unsigned int prod_num, rev;
3720 u16 id;
3721 int err;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003722
Vivien Didelot8f6345b2016-07-20 18:18:36 -04003723 mutex_lock(&chip->reg_lock);
3724 err = mv88e6xxx_port_read(chip, 0, PORT_SWITCH_ID, &id);
3725 mutex_unlock(&chip->reg_lock);
3726 if (err)
3727 return err;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003728
3729 prod_num = (id & 0xfff0) >> 4;
3730 rev = id & 0x000f;
3731
3732 info = mv88e6xxx_lookup_info(prod_num);
3733 if (!info)
3734 return -ENODEV;
3735
Vivien Didelotcaac8542016-06-20 13:14:09 -04003736 /* Update the compatible info with the probed one */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003737 chip->info = info;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003738
Vivien Didelotca070c12016-09-02 14:45:34 -04003739 err = mv88e6xxx_g2_require(chip);
3740 if (err)
3741 return err;
3742
Vivien Didelotfad09c72016-06-21 12:28:20 -04003743 dev_info(chip->dev, "switch 0x%x detected: %s, revision %u\n",
3744 chip->info->prod_num, chip->info->name, rev);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003745
3746 return 0;
3747}
3748
Vivien Didelotfad09c72016-06-21 12:28:20 -04003749static struct mv88e6xxx_chip *mv88e6xxx_alloc_chip(struct device *dev)
Vivien Didelot469d7292016-06-20 13:14:06 -04003750{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003751 struct mv88e6xxx_chip *chip;
Vivien Didelot469d7292016-06-20 13:14:06 -04003752
Vivien Didelotfad09c72016-06-21 12:28:20 -04003753 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
3754 if (!chip)
Vivien Didelot469d7292016-06-20 13:14:06 -04003755 return NULL;
3756
Vivien Didelotfad09c72016-06-21 12:28:20 -04003757 chip->dev = dev;
Vivien Didelot469d7292016-06-20 13:14:06 -04003758
Vivien Didelotfad09c72016-06-21 12:28:20 -04003759 mutex_init(&chip->reg_lock);
Vivien Didelot469d7292016-06-20 13:14:06 -04003760
Vivien Didelotfad09c72016-06-21 12:28:20 -04003761 return chip;
Vivien Didelot469d7292016-06-20 13:14:06 -04003762}
3763
Vivien Didelote57e5e72016-08-15 17:19:00 -04003764static void mv88e6xxx_phy_init(struct mv88e6xxx_chip *chip)
3765{
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003766 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU))
Vivien Didelote57e5e72016-08-15 17:19:00 -04003767 mv88e6xxx_ppu_state_init(chip);
Vivien Didelote57e5e72016-08-15 17:19:00 -04003768}
3769
Andrew Lunn930188c2016-08-22 16:01:03 +02003770static void mv88e6xxx_phy_destroy(struct mv88e6xxx_chip *chip)
3771{
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003772 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU))
Andrew Lunn930188c2016-08-22 16:01:03 +02003773 mv88e6xxx_ppu_state_destroy(chip);
Andrew Lunn930188c2016-08-22 16:01:03 +02003774}
3775
Vivien Didelotfad09c72016-06-21 12:28:20 -04003776static int mv88e6xxx_smi_init(struct mv88e6xxx_chip *chip,
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003777 struct mii_bus *bus, int sw_addr)
3778{
3779 /* ADDR[0] pin is unavailable externally and considered zero */
3780 if (sw_addr & 0x1)
3781 return -EINVAL;
3782
Vivien Didelot914b32f2016-06-20 13:14:11 -04003783 if (sw_addr == 0)
Vivien Didelotfad09c72016-06-21 12:28:20 -04003784 chip->smi_ops = &mv88e6xxx_smi_single_chip_ops;
Vivien Didelota0ffff22016-08-15 17:18:58 -04003785 else if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_MULTI_CHIP))
Vivien Didelotfad09c72016-06-21 12:28:20 -04003786 chip->smi_ops = &mv88e6xxx_smi_multi_chip_ops;
Vivien Didelot914b32f2016-06-20 13:14:11 -04003787 else
3788 return -EINVAL;
3789
Vivien Didelotfad09c72016-06-21 12:28:20 -04003790 chip->bus = bus;
3791 chip->sw_addr = sw_addr;
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003792
3793 return 0;
3794}
3795
Andrew Lunn7b314362016-08-22 16:01:01 +02003796static enum dsa_tag_protocol mv88e6xxx_get_tag_protocol(struct dsa_switch *ds)
3797{
Vivien Didelot04bed142016-08-31 18:06:13 -04003798 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02003799
3800 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA))
3801 return DSA_TAG_PROTO_EDSA;
3802
3803 return DSA_TAG_PROTO_DSA;
Andrew Lunn7b314362016-08-22 16:01:01 +02003804}
3805
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02003806static const char *mv88e6xxx_drv_probe(struct device *dsa_dev,
3807 struct device *host_dev, int sw_addr,
3808 void **priv)
Andrew Lunna77d43f2016-04-13 02:40:42 +02003809{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003810 struct mv88e6xxx_chip *chip;
Vivien Didelota439c062016-04-17 13:23:58 -04003811 struct mii_bus *bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003812 int err;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003813
Vivien Didelota439c062016-04-17 13:23:58 -04003814 bus = dsa_host_dev_to_mii_bus(host_dev);
Andrew Lunnc1569132016-04-13 02:40:45 +02003815 if (!bus)
3816 return NULL;
3817
Vivien Didelotfad09c72016-06-21 12:28:20 -04003818 chip = mv88e6xxx_alloc_chip(dsa_dev);
3819 if (!chip)
Vivien Didelot469d7292016-06-20 13:14:06 -04003820 return NULL;
3821
Vivien Didelotcaac8542016-06-20 13:14:09 -04003822 /* Legacy SMI probing will only support chips similar to 88E6085 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003823 chip->info = &mv88e6xxx_table[MV88E6085];
Vivien Didelotcaac8542016-06-20 13:14:09 -04003824
Vivien Didelotfad09c72016-06-21 12:28:20 -04003825 err = mv88e6xxx_smi_init(chip, bus, sw_addr);
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003826 if (err)
3827 goto free;
3828
Vivien Didelotfad09c72016-06-21 12:28:20 -04003829 err = mv88e6xxx_detect(chip);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003830 if (err)
Vivien Didelot469d7292016-06-20 13:14:06 -04003831 goto free;
Vivien Didelota439c062016-04-17 13:23:58 -04003832
Andrew Lunndc30c352016-10-16 19:56:49 +02003833 mutex_lock(&chip->reg_lock);
3834 err = mv88e6xxx_switch_reset(chip);
3835 mutex_unlock(&chip->reg_lock);
3836 if (err)
3837 goto free;
3838
Vivien Didelote57e5e72016-08-15 17:19:00 -04003839 mv88e6xxx_phy_init(chip);
3840
Vivien Didelotfad09c72016-06-21 12:28:20 -04003841 err = mv88e6xxx_mdio_register(chip, NULL);
Andrew Lunnb516d452016-06-04 21:17:06 +02003842 if (err)
Vivien Didelot469d7292016-06-20 13:14:06 -04003843 goto free;
Andrew Lunnb516d452016-06-04 21:17:06 +02003844
Vivien Didelotfad09c72016-06-21 12:28:20 -04003845 *priv = chip;
Vivien Didelota439c062016-04-17 13:23:58 -04003846
Vivien Didelotfad09c72016-06-21 12:28:20 -04003847 return chip->info->name;
Vivien Didelot469d7292016-06-20 13:14:06 -04003848free:
Vivien Didelotfad09c72016-06-21 12:28:20 -04003849 devm_kfree(dsa_dev, chip);
Vivien Didelot469d7292016-06-20 13:14:06 -04003850
3851 return NULL;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003852}
3853
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003854static int mv88e6xxx_port_mdb_prepare(struct dsa_switch *ds, int port,
3855 const struct switchdev_obj_port_mdb *mdb,
3856 struct switchdev_trans *trans)
3857{
3858 /* We don't need any dynamic resource from the kernel (yet),
3859 * so skip the prepare phase.
3860 */
3861
3862 return 0;
3863}
3864
3865static void mv88e6xxx_port_mdb_add(struct dsa_switch *ds, int port,
3866 const struct switchdev_obj_port_mdb *mdb,
3867 struct switchdev_trans *trans)
3868{
Vivien Didelot04bed142016-08-31 18:06:13 -04003869 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003870
3871 mutex_lock(&chip->reg_lock);
3872 if (mv88e6xxx_port_db_load_purge(chip, port, mdb->addr, mdb->vid,
3873 GLOBAL_ATU_DATA_STATE_MC_STATIC))
3874 netdev_err(ds->ports[port].netdev, "failed to load multicast MAC address\n");
3875 mutex_unlock(&chip->reg_lock);
3876}
3877
3878static int mv88e6xxx_port_mdb_del(struct dsa_switch *ds, int port,
3879 const struct switchdev_obj_port_mdb *mdb)
3880{
Vivien Didelot04bed142016-08-31 18:06:13 -04003881 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003882 int err;
3883
3884 mutex_lock(&chip->reg_lock);
3885 err = mv88e6xxx_port_db_load_purge(chip, port, mdb->addr, mdb->vid,
3886 GLOBAL_ATU_DATA_STATE_UNUSED);
3887 mutex_unlock(&chip->reg_lock);
3888
3889 return err;
3890}
3891
3892static int mv88e6xxx_port_mdb_dump(struct dsa_switch *ds, int port,
3893 struct switchdev_obj_port_mdb *mdb,
3894 int (*cb)(struct switchdev_obj *obj))
3895{
Vivien Didelot04bed142016-08-31 18:06:13 -04003896 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003897 int err;
3898
3899 mutex_lock(&chip->reg_lock);
3900 err = mv88e6xxx_port_db_dump(chip, port, &mdb->obj, cb);
3901 mutex_unlock(&chip->reg_lock);
3902
3903 return err;
3904}
3905
Vivien Didelot9d490b42016-08-23 12:38:56 -04003906static struct dsa_switch_ops mv88e6xxx_switch_ops = {
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02003907 .probe = mv88e6xxx_drv_probe,
Andrew Lunn7b314362016-08-22 16:01:01 +02003908 .get_tag_protocol = mv88e6xxx_get_tag_protocol,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003909 .setup = mv88e6xxx_setup,
3910 .set_addr = mv88e6xxx_set_addr,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003911 .adjust_link = mv88e6xxx_adjust_link,
3912 .get_strings = mv88e6xxx_get_strings,
3913 .get_ethtool_stats = mv88e6xxx_get_ethtool_stats,
3914 .get_sset_count = mv88e6xxx_get_sset_count,
3915 .set_eee = mv88e6xxx_set_eee,
3916 .get_eee = mv88e6xxx_get_eee,
3917#ifdef CONFIG_NET_DSA_HWMON
3918 .get_temp = mv88e6xxx_get_temp,
3919 .get_temp_limit = mv88e6xxx_get_temp_limit,
3920 .set_temp_limit = mv88e6xxx_set_temp_limit,
3921 .get_temp_alarm = mv88e6xxx_get_temp_alarm,
3922#endif
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003923 .get_eeprom_len = mv88e6xxx_get_eeprom_len,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003924 .get_eeprom = mv88e6xxx_get_eeprom,
3925 .set_eeprom = mv88e6xxx_set_eeprom,
3926 .get_regs_len = mv88e6xxx_get_regs_len,
3927 .get_regs = mv88e6xxx_get_regs,
Vivien Didelot2cfcd962016-07-18 20:45:40 -04003928 .set_ageing_time = mv88e6xxx_set_ageing_time,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003929 .port_bridge_join = mv88e6xxx_port_bridge_join,
3930 .port_bridge_leave = mv88e6xxx_port_bridge_leave,
3931 .port_stp_state_set = mv88e6xxx_port_stp_state_set,
Vivien Didelot749efcb2016-09-22 16:49:24 -04003932 .port_fast_age = mv88e6xxx_port_fast_age,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003933 .port_vlan_filtering = mv88e6xxx_port_vlan_filtering,
3934 .port_vlan_prepare = mv88e6xxx_port_vlan_prepare,
3935 .port_vlan_add = mv88e6xxx_port_vlan_add,
3936 .port_vlan_del = mv88e6xxx_port_vlan_del,
3937 .port_vlan_dump = mv88e6xxx_port_vlan_dump,
3938 .port_fdb_prepare = mv88e6xxx_port_fdb_prepare,
3939 .port_fdb_add = mv88e6xxx_port_fdb_add,
3940 .port_fdb_del = mv88e6xxx_port_fdb_del,
3941 .port_fdb_dump = mv88e6xxx_port_fdb_dump,
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003942 .port_mdb_prepare = mv88e6xxx_port_mdb_prepare,
3943 .port_mdb_add = mv88e6xxx_port_mdb_add,
3944 .port_mdb_del = mv88e6xxx_port_mdb_del,
3945 .port_mdb_dump = mv88e6xxx_port_mdb_dump,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003946};
3947
Vivien Didelotfad09c72016-06-21 12:28:20 -04003948static int mv88e6xxx_register_switch(struct mv88e6xxx_chip *chip,
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003949 struct device_node *np)
3950{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003951 struct device *dev = chip->dev;
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003952 struct dsa_switch *ds;
3953
3954 ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
3955 if (!ds)
3956 return -ENOMEM;
3957
3958 ds->dev = dev;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003959 ds->priv = chip;
Vivien Didelot9d490b42016-08-23 12:38:56 -04003960 ds->ops = &mv88e6xxx_switch_ops;
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003961
3962 dev_set_drvdata(dev, ds);
3963
3964 return dsa_register_switch(ds, np);
3965}
3966
Vivien Didelotfad09c72016-06-21 12:28:20 -04003967static void mv88e6xxx_unregister_switch(struct mv88e6xxx_chip *chip)
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003968{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003969 dsa_unregister_switch(chip->ds);
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003970}
3971
Vivien Didelot57d32312016-06-20 13:13:58 -04003972static int mv88e6xxx_probe(struct mdio_device *mdiodev)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003973{
3974 struct device *dev = &mdiodev->dev;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003975 struct device_node *np = dev->of_node;
Vivien Didelotcaac8542016-06-20 13:14:09 -04003976 const struct mv88e6xxx_info *compat_info;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003977 struct mv88e6xxx_chip *chip;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003978 u32 eeprom_len;
Andrew Lunn52638f72016-05-10 23:27:22 +02003979 int err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003980
Vivien Didelotcaac8542016-06-20 13:14:09 -04003981 compat_info = of_device_get_match_data(dev);
3982 if (!compat_info)
3983 return -EINVAL;
3984
Vivien Didelotfad09c72016-06-21 12:28:20 -04003985 chip = mv88e6xxx_alloc_chip(dev);
3986 if (!chip)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003987 return -ENOMEM;
3988
Vivien Didelotfad09c72016-06-21 12:28:20 -04003989 chip->info = compat_info;
Vivien Didelotcaac8542016-06-20 13:14:09 -04003990
Vivien Didelotfad09c72016-06-21 12:28:20 -04003991 err = mv88e6xxx_smi_init(chip, mdiodev->bus, mdiodev->addr);
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003992 if (err)
3993 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003994
Vivien Didelotfad09c72016-06-21 12:28:20 -04003995 err = mv88e6xxx_detect(chip);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003996 if (err)
3997 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003998
Vivien Didelote57e5e72016-08-15 17:19:00 -04003999 mv88e6xxx_phy_init(chip);
4000
Vivien Didelotfad09c72016-06-21 12:28:20 -04004001 chip->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_ASIS);
4002 if (IS_ERR(chip->reset))
4003 return PTR_ERR(chip->reset);
Andrew Lunn52638f72016-05-10 23:27:22 +02004004
Vivien Didelotee4dc2e72016-09-29 12:22:02 -04004005 if (chip->info->ops->get_eeprom &&
Andrew Lunnf8cd8752016-05-10 23:27:25 +02004006 !of_property_read_u32(np, "eeprom-length", &eeprom_len))
Vivien Didelotfad09c72016-06-21 12:28:20 -04004007 chip->eeprom_len = eeprom_len;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02004008
Andrew Lunndc30c352016-10-16 19:56:49 +02004009 mutex_lock(&chip->reg_lock);
4010 err = mv88e6xxx_switch_reset(chip);
4011 mutex_unlock(&chip->reg_lock);
Andrew Lunnb516d452016-06-04 21:17:06 +02004012 if (err)
Andrew Lunndc30c352016-10-16 19:56:49 +02004013 goto out;
Andrew Lunnb516d452016-06-04 21:17:06 +02004014
Andrew Lunndc30c352016-10-16 19:56:49 +02004015 chip->irq = of_irq_get(np, 0);
4016 if (chip->irq == -EPROBE_DEFER) {
4017 err = chip->irq;
4018 goto out;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02004019 }
4020
Andrew Lunndc30c352016-10-16 19:56:49 +02004021 if (chip->irq > 0) {
4022 /* Has to be performed before the MDIO bus is created,
4023 * because the PHYs will link there interrupts to these
4024 * interrupt controllers
4025 */
4026 mutex_lock(&chip->reg_lock);
4027 err = mv88e6xxx_g1_irq_setup(chip);
4028 mutex_unlock(&chip->reg_lock);
4029
4030 if (err)
4031 goto out;
4032
4033 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_INT)) {
4034 err = mv88e6xxx_g2_irq_setup(chip);
4035 if (err)
4036 goto out_g1_irq;
4037 }
4038 }
4039
4040 err = mv88e6xxx_mdio_register(chip, np);
4041 if (err)
4042 goto out_g2_irq;
4043
4044 err = mv88e6xxx_register_switch(chip, np);
4045 if (err)
4046 goto out_mdio;
4047
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004048 return 0;
Andrew Lunndc30c352016-10-16 19:56:49 +02004049
4050out_mdio:
4051 mv88e6xxx_mdio_unregister(chip);
4052out_g2_irq:
4053 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_INT))
4054 mv88e6xxx_g2_irq_free(chip);
4055out_g1_irq:
4056 mv88e6xxx_g1_irq_free(chip);
4057out:
4058 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004059}
4060
4061static void mv88e6xxx_remove(struct mdio_device *mdiodev)
4062{
4063 struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev);
Vivien Didelot04bed142016-08-31 18:06:13 -04004064 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004065
Andrew Lunn930188c2016-08-22 16:01:03 +02004066 mv88e6xxx_phy_destroy(chip);
Vivien Didelotfad09c72016-06-21 12:28:20 -04004067 mv88e6xxx_unregister_switch(chip);
4068 mv88e6xxx_mdio_unregister(chip);
Andrew Lunndc30c352016-10-16 19:56:49 +02004069
4070 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_INT))
4071 mv88e6xxx_g2_irq_free(chip);
4072 mv88e6xxx_g1_irq_free(chip);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004073}
4074
4075static const struct of_device_id mv88e6xxx_of_match[] = {
Vivien Didelotcaac8542016-06-20 13:14:09 -04004076 {
4077 .compatible = "marvell,mv88e6085",
4078 .data = &mv88e6xxx_table[MV88E6085],
4079 },
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004080 { /* sentinel */ },
4081};
4082
4083MODULE_DEVICE_TABLE(of, mv88e6xxx_of_match);
4084
4085static struct mdio_driver mv88e6xxx_driver = {
4086 .probe = mv88e6xxx_probe,
4087 .remove = mv88e6xxx_remove,
4088 .mdiodrv.driver = {
4089 .name = "mv88e6085",
4090 .of_match_table = mv88e6xxx_of_match,
4091 },
4092};
4093
Ben Hutchings98e67302011-11-25 14:36:19 +00004094static int __init mv88e6xxx_init(void)
4095{
Vivien Didelot9d490b42016-08-23 12:38:56 -04004096 register_switch_driver(&mv88e6xxx_switch_ops);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004097 return mdio_driver_register(&mv88e6xxx_driver);
Ben Hutchings98e67302011-11-25 14:36:19 +00004098}
4099module_init(mv88e6xxx_init);
4100
4101static void __exit mv88e6xxx_cleanup(void)
4102{
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004103 mdio_driver_unregister(&mv88e6xxx_driver);
Vivien Didelot9d490b42016-08-23 12:38:56 -04004104 unregister_switch_driver(&mv88e6xxx_switch_ops);
Ben Hutchings98e67302011-11-25 14:36:19 +00004105}
4106module_exit(mv88e6xxx_cleanup);
Ben Hutchings3d825ed2011-11-25 14:37:16 +00004107
4108MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
4109MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
4110MODULE_LICENSE("GPL");