blob: ad31d3ed3acaf084828ac2ac3a930e576b56a68a [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>
Barry Grussling19b2f972013-01-08 16:05:54 +000021#include <linux/jiffies.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000022#include <linux/list.h>
Andrew Lunn14c7b3c2016-05-10 23:27:21 +020023#include <linux/mdio.h>
Paul Gortmaker2bbba272012-01-24 10:41:40 +000024#include <linux/module.h>
Vivien Didelotcaac8542016-06-20 13:14:09 -040025#include <linux/of_device.h>
Andrew Lunnb516d452016-06-04 21:17:06 +020026#include <linux/of_mdio.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000027#include <linux/netdevice.h>
Andrew Lunnc8c1b39a2015-11-20 03:56:24 +010028#include <linux/gpio/consumer.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000029#include <linux/phy.h>
Ben Hutchingsc8f0b862011-11-27 17:06:08 +000030#include <net/dsa.h>
Vivien Didelot1f36faf2015-10-08 11:35:13 -040031#include <net/switchdev.h>
Vivien Didelotec561272016-09-02 14:45:33 -040032
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000033#include "mv88e6xxx.h"
Vivien Didelota935c052016-09-29 12:21:53 -040034#include "global1.h"
Vivien Didelotec561272016-09-02 14:45:33 -040035#include "global2.h"
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000036
Vivien Didelotfad09c72016-06-21 12:28:20 -040037static void assert_reg_lock(struct mv88e6xxx_chip *chip)
Vivien Didelot3996a4f2015-10-30 18:56:45 -040038{
Vivien Didelotfad09c72016-06-21 12:28:20 -040039 if (unlikely(!mutex_is_locked(&chip->reg_lock))) {
40 dev_err(chip->dev, "Switch registers lock not held!\n");
Vivien Didelot3996a4f2015-10-30 18:56:45 -040041 dump_stack();
42 }
43}
44
Vivien Didelot914b32f2016-06-20 13:14:11 -040045/* The switch ADDR[4:1] configuration pins define the chip SMI device address
46 * (ADDR[0] is always zero, thus only even SMI addresses can be strapped).
47 *
48 * When ADDR is all zero, the chip uses Single-chip Addressing Mode, assuming it
49 * is the only device connected to the SMI master. In this mode it responds to
50 * all 32 possible SMI addresses, and thus maps directly the internal devices.
51 *
52 * When ADDR is non-zero, the chip uses Multi-chip Addressing Mode, allowing
53 * multiple devices to share the SMI interface. In this mode it responds to only
54 * 2 registers, used to indirectly access the internal SMI devices.
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000055 */
Vivien Didelot914b32f2016-06-20 13:14:11 -040056
Vivien Didelotfad09c72016-06-21 12:28:20 -040057static int mv88e6xxx_smi_read(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -040058 int addr, int reg, u16 *val)
59{
Vivien Didelotfad09c72016-06-21 12:28:20 -040060 if (!chip->smi_ops)
Vivien Didelot914b32f2016-06-20 13:14:11 -040061 return -EOPNOTSUPP;
62
Vivien Didelotfad09c72016-06-21 12:28:20 -040063 return chip->smi_ops->read(chip, addr, reg, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -040064}
65
Vivien Didelotfad09c72016-06-21 12:28:20 -040066static int mv88e6xxx_smi_write(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -040067 int addr, int reg, u16 val)
68{
Vivien Didelotfad09c72016-06-21 12:28:20 -040069 if (!chip->smi_ops)
Vivien Didelot914b32f2016-06-20 13:14:11 -040070 return -EOPNOTSUPP;
71
Vivien Didelotfad09c72016-06-21 12:28:20 -040072 return chip->smi_ops->write(chip, addr, reg, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -040073}
74
Vivien Didelotfad09c72016-06-21 12:28:20 -040075static int mv88e6xxx_smi_single_chip_read(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -040076 int addr, int reg, u16 *val)
77{
78 int ret;
79
Vivien Didelotfad09c72016-06-21 12:28:20 -040080 ret = mdiobus_read_nested(chip->bus, addr, reg);
Vivien Didelot914b32f2016-06-20 13:14:11 -040081 if (ret < 0)
82 return ret;
83
84 *val = ret & 0xffff;
85
86 return 0;
87}
88
Vivien Didelotfad09c72016-06-21 12:28:20 -040089static int mv88e6xxx_smi_single_chip_write(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -040090 int addr, int reg, u16 val)
91{
92 int ret;
93
Vivien Didelotfad09c72016-06-21 12:28:20 -040094 ret = mdiobus_write_nested(chip->bus, addr, reg, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -040095 if (ret < 0)
96 return ret;
97
98 return 0;
99}
100
Vivien Didelotc08026a2016-09-29 12:21:59 -0400101static const struct mv88e6xxx_bus_ops mv88e6xxx_smi_single_chip_ops = {
Vivien Didelot914b32f2016-06-20 13:14:11 -0400102 .read = mv88e6xxx_smi_single_chip_read,
103 .write = mv88e6xxx_smi_single_chip_write,
104};
105
Vivien Didelotfad09c72016-06-21 12:28:20 -0400106static int mv88e6xxx_smi_multi_chip_wait(struct mv88e6xxx_chip *chip)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000107{
108 int ret;
109 int i;
110
111 for (i = 0; i < 16; i++) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400112 ret = mdiobus_read_nested(chip->bus, chip->sw_addr, SMI_CMD);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000113 if (ret < 0)
114 return ret;
115
Andrew Lunncca8b132015-04-02 04:06:39 +0200116 if ((ret & SMI_CMD_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000117 return 0;
118 }
119
120 return -ETIMEDOUT;
121}
122
Vivien Didelotfad09c72016-06-21 12:28:20 -0400123static int mv88e6xxx_smi_multi_chip_read(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -0400124 int addr, int reg, u16 *val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000125{
126 int ret;
127
Barry Grussling3675c8d2013-01-08 16:05:53 +0000128 /* Wait for the bus to become free. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400129 ret = mv88e6xxx_smi_multi_chip_wait(chip);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000130 if (ret < 0)
131 return ret;
132
Barry Grussling3675c8d2013-01-08 16:05:53 +0000133 /* Transmit the read command. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400134 ret = mdiobus_write_nested(chip->bus, chip->sw_addr, SMI_CMD,
Neil Armstrong6e899e62015-10-22 10:37:53 +0200135 SMI_CMD_OP_22_READ | (addr << 5) | reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000136 if (ret < 0)
137 return ret;
138
Barry Grussling3675c8d2013-01-08 16:05:53 +0000139 /* Wait for the read command to complete. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400140 ret = mv88e6xxx_smi_multi_chip_wait(chip);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000141 if (ret < 0)
142 return ret;
143
Barry Grussling3675c8d2013-01-08 16:05:53 +0000144 /* Read the data. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400145 ret = mdiobus_read_nested(chip->bus, chip->sw_addr, SMI_DATA);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000146 if (ret < 0)
147 return ret;
148
Vivien Didelot914b32f2016-06-20 13:14:11 -0400149 *val = ret & 0xffff;
150
151 return 0;
152}
153
Vivien Didelotfad09c72016-06-21 12:28:20 -0400154static int mv88e6xxx_smi_multi_chip_write(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -0400155 int addr, int reg, u16 val)
156{
157 int ret;
158
159 /* Wait for the bus to become free. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400160 ret = mv88e6xxx_smi_multi_chip_wait(chip);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400161 if (ret < 0)
162 return ret;
163
164 /* Transmit the data to write. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400165 ret = mdiobus_write_nested(chip->bus, chip->sw_addr, SMI_DATA, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400166 if (ret < 0)
167 return ret;
168
169 /* Transmit the write command. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400170 ret = mdiobus_write_nested(chip->bus, chip->sw_addr, SMI_CMD,
Vivien Didelot914b32f2016-06-20 13:14:11 -0400171 SMI_CMD_OP_22_WRITE | (addr << 5) | reg);
172 if (ret < 0)
173 return ret;
174
175 /* Wait for the write command to complete. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400176 ret = mv88e6xxx_smi_multi_chip_wait(chip);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400177 if (ret < 0)
178 return ret;
179
180 return 0;
181}
182
Vivien Didelotc08026a2016-09-29 12:21:59 -0400183static const struct mv88e6xxx_bus_ops mv88e6xxx_smi_multi_chip_ops = {
Vivien Didelot914b32f2016-06-20 13:14:11 -0400184 .read = mv88e6xxx_smi_multi_chip_read,
185 .write = mv88e6xxx_smi_multi_chip_write,
186};
187
Vivien Didelotec561272016-09-02 14:45:33 -0400188int mv88e6xxx_read(struct mv88e6xxx_chip *chip, int addr, int reg, u16 *val)
Vivien Didelot914b32f2016-06-20 13:14:11 -0400189{
190 int err;
191
Vivien Didelotfad09c72016-06-21 12:28:20 -0400192 assert_reg_lock(chip);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400193
Vivien Didelotfad09c72016-06-21 12:28:20 -0400194 err = mv88e6xxx_smi_read(chip, addr, reg, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400195 if (err)
196 return err;
197
Vivien Didelotfad09c72016-06-21 12:28:20 -0400198 dev_dbg(chip->dev, "<- addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
Vivien Didelot914b32f2016-06-20 13:14:11 -0400199 addr, reg, *val);
200
201 return 0;
202}
203
Vivien Didelotec561272016-09-02 14:45:33 -0400204int mv88e6xxx_write(struct mv88e6xxx_chip *chip, int addr, int reg, u16 val)
Vivien Didelot914b32f2016-06-20 13:14:11 -0400205{
206 int err;
207
Vivien Didelotfad09c72016-06-21 12:28:20 -0400208 assert_reg_lock(chip);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400209
Vivien Didelotfad09c72016-06-21 12:28:20 -0400210 err = mv88e6xxx_smi_write(chip, addr, reg, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400211 if (err)
212 return err;
213
Vivien Didelotfad09c72016-06-21 12:28:20 -0400214 dev_dbg(chip->dev, "-> addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
Vivien Didelot914b32f2016-06-20 13:14:11 -0400215 addr, reg, val);
216
217 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000218}
219
Wei Yongjunb3f5bf62016-09-25 15:43:02 +0000220static int mv88e6xxx_port_read(struct mv88e6xxx_chip *chip, int port, int reg,
221 u16 *val)
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200222{
223 int addr = chip->info->port_base_addr + port;
224
225 return mv88e6xxx_read(chip, addr, reg, val);
226}
227
Wei Yongjunb3f5bf62016-09-25 15:43:02 +0000228static int mv88e6xxx_port_write(struct mv88e6xxx_chip *chip, int port, int reg,
229 u16 val)
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200230{
231 int addr = chip->info->port_base_addr + port;
232
233 return mv88e6xxx_write(chip, addr, reg, val);
234}
235
Vivien Didelote57e5e72016-08-15 17:19:00 -0400236static int mv88e6xxx_phy_read(struct mv88e6xxx_chip *chip, int phy,
237 int reg, u16 *val)
238{
239 int addr = phy; /* PHY devices addresses start at 0x0 */
240
241 if (!chip->phy_ops)
242 return -EOPNOTSUPP;
243
244 return chip->phy_ops->read(chip, addr, reg, val);
245}
246
247static int mv88e6xxx_phy_write(struct mv88e6xxx_chip *chip, int phy,
248 int reg, u16 val)
249{
250 int addr = phy; /* PHY devices addresses start at 0x0 */
251
252 if (!chip->phy_ops)
253 return -EOPNOTSUPP;
254
255 return chip->phy_ops->write(chip, addr, reg, val);
256}
257
Vivien Didelot09cb7df2016-08-15 17:19:01 -0400258static int mv88e6xxx_phy_page_get(struct mv88e6xxx_chip *chip, int phy, u8 page)
259{
260 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_PHY_PAGE))
261 return -EOPNOTSUPP;
262
263 return mv88e6xxx_phy_write(chip, phy, PHY_PAGE, page);
264}
265
266static void mv88e6xxx_phy_page_put(struct mv88e6xxx_chip *chip, int phy)
267{
268 int err;
269
270 /* Restore PHY page Copper 0x0 for access via the registered MDIO bus */
271 err = mv88e6xxx_phy_write(chip, phy, PHY_PAGE, PHY_PAGE_COPPER);
272 if (unlikely(err)) {
273 dev_err(chip->dev, "failed to restore PHY %d page Copper (%d)\n",
274 phy, err);
275 }
276}
277
278static int mv88e6xxx_phy_page_read(struct mv88e6xxx_chip *chip, int phy,
279 u8 page, int reg, u16 *val)
280{
281 int err;
282
283 /* There is no paging for registers 22 */
284 if (reg == PHY_PAGE)
285 return -EINVAL;
286
287 err = mv88e6xxx_phy_page_get(chip, phy, page);
288 if (!err) {
289 err = mv88e6xxx_phy_read(chip, phy, reg, val);
290 mv88e6xxx_phy_page_put(chip, phy);
291 }
292
293 return err;
294}
295
296static int mv88e6xxx_phy_page_write(struct mv88e6xxx_chip *chip, int phy,
297 u8 page, int reg, u16 val)
298{
299 int err;
300
301 /* There is no paging for registers 22 */
302 if (reg == PHY_PAGE)
303 return -EINVAL;
304
305 err = mv88e6xxx_phy_page_get(chip, phy, page);
306 if (!err) {
307 err = mv88e6xxx_phy_write(chip, phy, PHY_PAGE, page);
308 mv88e6xxx_phy_page_put(chip, phy);
309 }
310
311 return err;
312}
313
314static int mv88e6xxx_serdes_read(struct mv88e6xxx_chip *chip, int reg, u16 *val)
315{
316 return mv88e6xxx_phy_page_read(chip, ADDR_SERDES, SERDES_PAGE_FIBER,
317 reg, val);
318}
319
320static int mv88e6xxx_serdes_write(struct mv88e6xxx_chip *chip, int reg, u16 val)
321{
322 return mv88e6xxx_phy_page_write(chip, ADDR_SERDES, SERDES_PAGE_FIBER,
323 reg, val);
324}
325
Vivien Didelotec561272016-09-02 14:45:33 -0400326int mv88e6xxx_wait(struct mv88e6xxx_chip *chip, int addr, int reg, u16 mask)
Vivien Didelot2d79af62016-08-15 17:18:57 -0400327{
Andrew Lunn6441e6692016-08-19 00:01:55 +0200328 int i;
Vivien Didelot2d79af62016-08-15 17:18:57 -0400329
Andrew Lunn6441e6692016-08-19 00:01:55 +0200330 for (i = 0; i < 16; i++) {
Vivien Didelot2d79af62016-08-15 17:18:57 -0400331 u16 val;
332 int err;
333
334 err = mv88e6xxx_read(chip, addr, reg, &val);
335 if (err)
336 return err;
337
338 if (!(val & mask))
339 return 0;
340
341 usleep_range(1000, 2000);
342 }
343
Andrew Lunn30853552016-08-19 00:01:57 +0200344 dev_err(chip->dev, "Timeout while waiting for switch\n");
Vivien Didelot2d79af62016-08-15 17:18:57 -0400345 return -ETIMEDOUT;
346}
347
Vivien Didelotf22ab642016-07-18 20:45:31 -0400348/* Indirect write to single pointer-data register with an Update bit */
Vivien Didelotec561272016-09-02 14:45:33 -0400349int mv88e6xxx_update(struct mv88e6xxx_chip *chip, int addr, int reg, u16 update)
Vivien Didelotf22ab642016-07-18 20:45:31 -0400350{
351 u16 val;
Andrew Lunn0f02b4f2016-08-19 00:01:56 +0200352 int err;
Vivien Didelotf22ab642016-07-18 20:45:31 -0400353
354 /* Wait until the previous operation is completed */
Andrew Lunn0f02b4f2016-08-19 00:01:56 +0200355 err = mv88e6xxx_wait(chip, addr, reg, BIT(15));
356 if (err)
357 return err;
Vivien Didelotf22ab642016-07-18 20:45:31 -0400358
359 /* Set the Update bit to trigger a write operation */
360 val = BIT(15) | update;
361
362 return mv88e6xxx_write(chip, addr, reg, val);
363}
364
Vivien Didelota935c052016-09-29 12:21:53 -0400365static int mv88e6xxx_ppu_disable(struct mv88e6xxx_chip *chip)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000366{
Vivien Didelot914b32f2016-06-20 13:14:11 -0400367 u16 val;
Vivien Didelota935c052016-09-29 12:21:53 -0400368 int i, err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000369
Vivien Didelota935c052016-09-29 12:21:53 -0400370 err = mv88e6xxx_g1_read(chip, GLOBAL_CONTROL, &val);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400371 if (err)
372 return err;
Vivien Didelot3996a4f2015-10-30 18:56:45 -0400373
Vivien Didelota935c052016-09-29 12:21:53 -0400374 err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL,
375 val & ~GLOBAL_CONTROL_PPU_ENABLE);
376 if (err)
377 return err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000378
Andrew Lunn6441e6692016-08-19 00:01:55 +0200379 for (i = 0; i < 16; i++) {
Vivien Didelota935c052016-09-29 12:21:53 -0400380 err = mv88e6xxx_g1_read(chip, GLOBAL_STATUS, &val);
381 if (err)
382 return err;
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200383
Barry Grussling19b2f972013-01-08 16:05:54 +0000384 usleep_range(1000, 2000);
Vivien Didelota935c052016-09-29 12:21:53 -0400385 if ((val & GLOBAL_STATUS_PPU_MASK) != GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000386 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000387 }
388
389 return -ETIMEDOUT;
390}
391
Vivien Didelotfad09c72016-06-21 12:28:20 -0400392static int mv88e6xxx_ppu_enable(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000393{
Vivien Didelota935c052016-09-29 12:21:53 -0400394 u16 val;
395 int i, err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000396
Vivien Didelota935c052016-09-29 12:21:53 -0400397 err = mv88e6xxx_g1_read(chip, GLOBAL_CONTROL, &val);
398 if (err)
399 return err;
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200400
Vivien Didelota935c052016-09-29 12:21:53 -0400401 err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL,
402 val | GLOBAL_CONTROL_PPU_ENABLE);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200403 if (err)
404 return err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000405
Andrew Lunn6441e6692016-08-19 00:01:55 +0200406 for (i = 0; i < 16; i++) {
Vivien Didelota935c052016-09-29 12:21:53 -0400407 err = mv88e6xxx_g1_read(chip, GLOBAL_STATUS, &val);
408 if (err)
409 return err;
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200410
Barry Grussling19b2f972013-01-08 16:05:54 +0000411 usleep_range(1000, 2000);
Vivien Didelota935c052016-09-29 12:21:53 -0400412 if ((val & GLOBAL_STATUS_PPU_MASK) == GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000413 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000414 }
415
416 return -ETIMEDOUT;
417}
418
419static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
420{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400421 struct mv88e6xxx_chip *chip;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000422
Vivien Didelotfad09c72016-06-21 12:28:20 -0400423 chip = container_of(ugly, struct mv88e6xxx_chip, ppu_work);
Vivien Didelot762eb672016-06-04 21:16:54 +0200424
Vivien Didelotfad09c72016-06-21 12:28:20 -0400425 mutex_lock(&chip->reg_lock);
Vivien Didelot762eb672016-06-04 21:16:54 +0200426
Vivien Didelotfad09c72016-06-21 12:28:20 -0400427 if (mutex_trylock(&chip->ppu_mutex)) {
428 if (mv88e6xxx_ppu_enable(chip) == 0)
429 chip->ppu_disabled = 0;
430 mutex_unlock(&chip->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000431 }
Vivien Didelot762eb672016-06-04 21:16:54 +0200432
Vivien Didelotfad09c72016-06-21 12:28:20 -0400433 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000434}
435
436static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
437{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400438 struct mv88e6xxx_chip *chip = (void *)_ps;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000439
Vivien Didelotfad09c72016-06-21 12:28:20 -0400440 schedule_work(&chip->ppu_work);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000441}
442
Vivien Didelotfad09c72016-06-21 12:28:20 -0400443static int mv88e6xxx_ppu_access_get(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000444{
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000445 int ret;
446
Vivien Didelotfad09c72016-06-21 12:28:20 -0400447 mutex_lock(&chip->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000448
Barry Grussling3675c8d2013-01-08 16:05:53 +0000449 /* If the PHY polling unit is enabled, disable it so that
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000450 * we can access the PHY registers. If it was already
451 * disabled, cancel the timer that is going to re-enable
452 * it.
453 */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400454 if (!chip->ppu_disabled) {
455 ret = mv88e6xxx_ppu_disable(chip);
Barry Grussling85686582013-01-08 16:05:56 +0000456 if (ret < 0) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400457 mutex_unlock(&chip->ppu_mutex);
Barry Grussling85686582013-01-08 16:05:56 +0000458 return ret;
459 }
Vivien Didelotfad09c72016-06-21 12:28:20 -0400460 chip->ppu_disabled = 1;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000461 } else {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400462 del_timer(&chip->ppu_timer);
Barry Grussling85686582013-01-08 16:05:56 +0000463 ret = 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000464 }
465
466 return ret;
467}
468
Vivien Didelotfad09c72016-06-21 12:28:20 -0400469static void mv88e6xxx_ppu_access_put(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000470{
Barry Grussling3675c8d2013-01-08 16:05:53 +0000471 /* Schedule a timer to re-enable the PHY polling unit. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400472 mod_timer(&chip->ppu_timer, jiffies + msecs_to_jiffies(10));
473 mutex_unlock(&chip->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000474}
475
Vivien Didelotfad09c72016-06-21 12:28:20 -0400476static void mv88e6xxx_ppu_state_init(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000477{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400478 mutex_init(&chip->ppu_mutex);
479 INIT_WORK(&chip->ppu_work, mv88e6xxx_ppu_reenable_work);
480 init_timer(&chip->ppu_timer);
481 chip->ppu_timer.data = (unsigned long)chip;
482 chip->ppu_timer.function = mv88e6xxx_ppu_reenable_timer;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000483}
484
Andrew Lunn930188c2016-08-22 16:01:03 +0200485static void mv88e6xxx_ppu_state_destroy(struct mv88e6xxx_chip *chip)
486{
487 del_timer_sync(&chip->ppu_timer);
488}
489
Vivien Didelote57e5e72016-08-15 17:19:00 -0400490static int mv88e6xxx_phy_ppu_read(struct mv88e6xxx_chip *chip, int addr,
491 int reg, u16 *val)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000492{
Vivien Didelote57e5e72016-08-15 17:19:00 -0400493 int err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000494
Vivien Didelote57e5e72016-08-15 17:19:00 -0400495 err = mv88e6xxx_ppu_access_get(chip);
496 if (!err) {
497 err = mv88e6xxx_read(chip, addr, reg, val);
Vivien Didelotfad09c72016-06-21 12:28:20 -0400498 mv88e6xxx_ppu_access_put(chip);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000499 }
500
Vivien Didelote57e5e72016-08-15 17:19:00 -0400501 return err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000502}
503
Vivien Didelote57e5e72016-08-15 17:19:00 -0400504static int mv88e6xxx_phy_ppu_write(struct mv88e6xxx_chip *chip, int addr,
505 int reg, u16 val)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000506{
Vivien Didelote57e5e72016-08-15 17:19:00 -0400507 int err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000508
Vivien Didelote57e5e72016-08-15 17:19:00 -0400509 err = mv88e6xxx_ppu_access_get(chip);
510 if (!err) {
511 err = mv88e6xxx_write(chip, addr, reg, val);
Vivien Didelotfad09c72016-06-21 12:28:20 -0400512 mv88e6xxx_ppu_access_put(chip);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000513 }
514
Vivien Didelote57e5e72016-08-15 17:19:00 -0400515 return err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000516}
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000517
Vivien Didelotc08026a2016-09-29 12:21:59 -0400518static const struct mv88e6xxx_bus_ops mv88e6xxx_phy_ppu_ops = {
Vivien Didelote57e5e72016-08-15 17:19:00 -0400519 .read = mv88e6xxx_phy_ppu_read,
520 .write = mv88e6xxx_phy_ppu_write,
521};
522
Vivien Didelotfad09c72016-06-21 12:28:20 -0400523static bool mv88e6xxx_6065_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200524{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400525 return chip->info->family == MV88E6XXX_FAMILY_6065;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200526}
527
Vivien Didelotfad09c72016-06-21 12:28:20 -0400528static bool mv88e6xxx_6095_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200529{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400530 return chip->info->family == MV88E6XXX_FAMILY_6095;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200531}
532
Vivien Didelotfad09c72016-06-21 12:28:20 -0400533static bool mv88e6xxx_6097_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200534{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400535 return chip->info->family == MV88E6XXX_FAMILY_6097;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200536}
537
Vivien Didelotfad09c72016-06-21 12:28:20 -0400538static bool mv88e6xxx_6165_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200539{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400540 return chip->info->family == MV88E6XXX_FAMILY_6165;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200541}
542
Vivien Didelotfad09c72016-06-21 12:28:20 -0400543static bool mv88e6xxx_6185_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200544{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400545 return chip->info->family == MV88E6XXX_FAMILY_6185;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200546}
547
Vivien Didelotfad09c72016-06-21 12:28:20 -0400548static bool mv88e6xxx_6320_family(struct mv88e6xxx_chip *chip)
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700549{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400550 return chip->info->family == MV88E6XXX_FAMILY_6320;
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700551}
552
Vivien Didelotfad09c72016-06-21 12:28:20 -0400553static bool mv88e6xxx_6351_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200554{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400555 return chip->info->family == MV88E6XXX_FAMILY_6351;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200556}
557
Vivien Didelotfad09c72016-06-21 12:28:20 -0400558static bool mv88e6xxx_6352_family(struct mv88e6xxx_chip *chip)
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200559{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400560 return chip->info->family == MV88E6XXX_FAMILY_6352;
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200561}
562
Andrew Lunndea87022015-08-31 15:56:47 +0200563/* We expect the switch to perform auto negotiation if there is a real
564 * phy. However, in the case of a fixed link phy, we force the port
565 * settings from the fixed link settings.
566 */
Vivien Didelotf81ec902016-05-09 13:22:58 -0400567static void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port,
568 struct phy_device *phydev)
Andrew Lunndea87022015-08-31 15:56:47 +0200569{
Vivien Didelot04bed142016-08-31 18:06:13 -0400570 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200571 u16 reg;
572 int err;
Andrew Lunndea87022015-08-31 15:56:47 +0200573
574 if (!phy_is_pseudo_fixed_link(phydev))
575 return;
576
Vivien Didelotfad09c72016-06-21 12:28:20 -0400577 mutex_lock(&chip->reg_lock);
Andrew Lunndea87022015-08-31 15:56:47 +0200578
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200579 err = mv88e6xxx_port_read(chip, port, PORT_PCS_CTRL, &reg);
580 if (err)
Andrew Lunndea87022015-08-31 15:56:47 +0200581 goto out;
582
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200583 reg &= ~(PORT_PCS_CTRL_LINK_UP |
584 PORT_PCS_CTRL_FORCE_LINK |
585 PORT_PCS_CTRL_DUPLEX_FULL |
586 PORT_PCS_CTRL_FORCE_DUPLEX |
587 PORT_PCS_CTRL_UNFORCED);
Andrew Lunndea87022015-08-31 15:56:47 +0200588
589 reg |= PORT_PCS_CTRL_FORCE_LINK;
590 if (phydev->link)
Vivien Didelot57d32312016-06-20 13:13:58 -0400591 reg |= PORT_PCS_CTRL_LINK_UP;
Andrew Lunndea87022015-08-31 15:56:47 +0200592
Vivien Didelotfad09c72016-06-21 12:28:20 -0400593 if (mv88e6xxx_6065_family(chip) && phydev->speed > SPEED_100)
Andrew Lunndea87022015-08-31 15:56:47 +0200594 goto out;
595
596 switch (phydev->speed) {
597 case SPEED_1000:
598 reg |= PORT_PCS_CTRL_1000;
599 break;
600 case SPEED_100:
601 reg |= PORT_PCS_CTRL_100;
602 break;
603 case SPEED_10:
604 reg |= PORT_PCS_CTRL_10;
605 break;
606 default:
607 pr_info("Unknown speed");
608 goto out;
609 }
610
611 reg |= PORT_PCS_CTRL_FORCE_DUPLEX;
612 if (phydev->duplex == DUPLEX_FULL)
613 reg |= PORT_PCS_CTRL_DUPLEX_FULL;
614
Vivien Didelotfad09c72016-06-21 12:28:20 -0400615 if ((mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip)) &&
Vivien Didelot370b4ff2016-09-29 12:21:57 -0400616 (port >= mv88e6xxx_num_ports(chip) - 2)) {
Andrew Lunne7e72ac2015-08-31 15:56:51 +0200617 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
618 reg |= PORT_PCS_CTRL_RGMII_DELAY_RXCLK;
619 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
620 reg |= PORT_PCS_CTRL_RGMII_DELAY_TXCLK;
621 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
622 reg |= (PORT_PCS_CTRL_RGMII_DELAY_RXCLK |
623 PORT_PCS_CTRL_RGMII_DELAY_TXCLK);
624 }
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200625 mv88e6xxx_port_write(chip, port, PORT_PCS_CTRL, reg);
Andrew Lunndea87022015-08-31 15:56:47 +0200626
627out:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400628 mutex_unlock(&chip->reg_lock);
Andrew Lunndea87022015-08-31 15:56:47 +0200629}
630
Vivien Didelotfad09c72016-06-21 12:28:20 -0400631static int _mv88e6xxx_stats_wait(struct mv88e6xxx_chip *chip)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000632{
Vivien Didelota935c052016-09-29 12:21:53 -0400633 u16 val;
634 int i, err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000635
636 for (i = 0; i < 10; i++) {
Vivien Didelota935c052016-09-29 12:21:53 -0400637 err = mv88e6xxx_g1_read(chip, GLOBAL_STATS_OP, &val);
638 if ((val & GLOBAL_STATS_OP_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000639 return 0;
640 }
641
642 return -ETIMEDOUT;
643}
644
Vivien Didelotfad09c72016-06-21 12:28:20 -0400645static int _mv88e6xxx_stats_snapshot(struct mv88e6xxx_chip *chip, int port)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000646{
Vivien Didelota935c052016-09-29 12:21:53 -0400647 int err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000648
Vivien Didelotfad09c72016-06-21 12:28:20 -0400649 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200650 port = (port + 1) << 5;
651
Barry Grussling3675c8d2013-01-08 16:05:53 +0000652 /* Snapshot the hardware statistics counters for this port. */
Vivien Didelota935c052016-09-29 12:21:53 -0400653 err = mv88e6xxx_g1_write(chip, GLOBAL_STATS_OP,
654 GLOBAL_STATS_OP_CAPTURE_PORT |
655 GLOBAL_STATS_OP_HIST_RX_TX | port);
656 if (err)
657 return err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000658
Barry Grussling3675c8d2013-01-08 16:05:53 +0000659 /* Wait for the snapshotting to complete. */
Vivien Didelota935c052016-09-29 12:21:53 -0400660 return _mv88e6xxx_stats_wait(chip);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000661}
662
Vivien Didelotfad09c72016-06-21 12:28:20 -0400663static void _mv88e6xxx_stats_read(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -0400664 int stat, u32 *val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000665{
Vivien Didelota935c052016-09-29 12:21:53 -0400666 u32 value;
667 u16 reg;
668 int err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000669
670 *val = 0;
671
Vivien Didelota935c052016-09-29 12:21:53 -0400672 err = mv88e6xxx_g1_write(chip, GLOBAL_STATS_OP,
673 GLOBAL_STATS_OP_READ_CAPTURED |
674 GLOBAL_STATS_OP_HIST_RX_TX | stat);
675 if (err)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000676 return;
677
Vivien Didelota935c052016-09-29 12:21:53 -0400678 err = _mv88e6xxx_stats_wait(chip);
679 if (err)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000680 return;
681
Vivien Didelota935c052016-09-29 12:21:53 -0400682 err = mv88e6xxx_g1_read(chip, GLOBAL_STATS_COUNTER_32, &reg);
683 if (err)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000684 return;
685
Vivien Didelota935c052016-09-29 12:21:53 -0400686 value = reg << 16;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000687
Vivien Didelota935c052016-09-29 12:21:53 -0400688 err = mv88e6xxx_g1_read(chip, GLOBAL_STATS_COUNTER_01, &reg);
689 if (err)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000690 return;
691
Vivien Didelota935c052016-09-29 12:21:53 -0400692 *val = value | reg;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000693}
694
Andrew Lunne413e7e2015-04-02 04:06:38 +0200695static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100696 { "in_good_octets", 8, 0x00, BANK0, },
697 { "in_bad_octets", 4, 0x02, BANK0, },
698 { "in_unicast", 4, 0x04, BANK0, },
699 { "in_broadcasts", 4, 0x06, BANK0, },
700 { "in_multicasts", 4, 0x07, BANK0, },
701 { "in_pause", 4, 0x16, BANK0, },
702 { "in_undersize", 4, 0x18, BANK0, },
703 { "in_fragments", 4, 0x19, BANK0, },
704 { "in_oversize", 4, 0x1a, BANK0, },
705 { "in_jabber", 4, 0x1b, BANK0, },
706 { "in_rx_error", 4, 0x1c, BANK0, },
707 { "in_fcs_error", 4, 0x1d, BANK0, },
708 { "out_octets", 8, 0x0e, BANK0, },
709 { "out_unicast", 4, 0x10, BANK0, },
710 { "out_broadcasts", 4, 0x13, BANK0, },
711 { "out_multicasts", 4, 0x12, BANK0, },
712 { "out_pause", 4, 0x15, BANK0, },
713 { "excessive", 4, 0x11, BANK0, },
714 { "collisions", 4, 0x1e, BANK0, },
715 { "deferred", 4, 0x05, BANK0, },
716 { "single", 4, 0x14, BANK0, },
717 { "multiple", 4, 0x17, BANK0, },
718 { "out_fcs_error", 4, 0x03, BANK0, },
719 { "late", 4, 0x1f, BANK0, },
720 { "hist_64bytes", 4, 0x08, BANK0, },
721 { "hist_65_127bytes", 4, 0x09, BANK0, },
722 { "hist_128_255bytes", 4, 0x0a, BANK0, },
723 { "hist_256_511bytes", 4, 0x0b, BANK0, },
724 { "hist_512_1023bytes", 4, 0x0c, BANK0, },
725 { "hist_1024_max_bytes", 4, 0x0d, BANK0, },
726 { "sw_in_discards", 4, 0x10, PORT, },
727 { "sw_in_filtered", 2, 0x12, PORT, },
728 { "sw_out_filtered", 2, 0x13, PORT, },
729 { "in_discards", 4, 0x00 | GLOBAL_STATS_OP_BANK_1, BANK1, },
730 { "in_filtered", 4, 0x01 | GLOBAL_STATS_OP_BANK_1, BANK1, },
731 { "in_accepted", 4, 0x02 | GLOBAL_STATS_OP_BANK_1, BANK1, },
732 { "in_bad_accepted", 4, 0x03 | GLOBAL_STATS_OP_BANK_1, BANK1, },
733 { "in_good_avb_class_a", 4, 0x04 | GLOBAL_STATS_OP_BANK_1, BANK1, },
734 { "in_good_avb_class_b", 4, 0x05 | GLOBAL_STATS_OP_BANK_1, BANK1, },
735 { "in_bad_avb_class_a", 4, 0x06 | GLOBAL_STATS_OP_BANK_1, BANK1, },
736 { "in_bad_avb_class_b", 4, 0x07 | GLOBAL_STATS_OP_BANK_1, BANK1, },
737 { "tcam_counter_0", 4, 0x08 | GLOBAL_STATS_OP_BANK_1, BANK1, },
738 { "tcam_counter_1", 4, 0x09 | GLOBAL_STATS_OP_BANK_1, BANK1, },
739 { "tcam_counter_2", 4, 0x0a | GLOBAL_STATS_OP_BANK_1, BANK1, },
740 { "tcam_counter_3", 4, 0x0b | GLOBAL_STATS_OP_BANK_1, BANK1, },
741 { "in_da_unknown", 4, 0x0e | GLOBAL_STATS_OP_BANK_1, BANK1, },
742 { "in_management", 4, 0x0f | GLOBAL_STATS_OP_BANK_1, BANK1, },
743 { "out_queue_0", 4, 0x10 | GLOBAL_STATS_OP_BANK_1, BANK1, },
744 { "out_queue_1", 4, 0x11 | GLOBAL_STATS_OP_BANK_1, BANK1, },
745 { "out_queue_2", 4, 0x12 | GLOBAL_STATS_OP_BANK_1, BANK1, },
746 { "out_queue_3", 4, 0x13 | GLOBAL_STATS_OP_BANK_1, BANK1, },
747 { "out_queue_4", 4, 0x14 | GLOBAL_STATS_OP_BANK_1, BANK1, },
748 { "out_queue_5", 4, 0x15 | GLOBAL_STATS_OP_BANK_1, BANK1, },
749 { "out_queue_6", 4, 0x16 | GLOBAL_STATS_OP_BANK_1, BANK1, },
750 { "out_queue_7", 4, 0x17 | GLOBAL_STATS_OP_BANK_1, BANK1, },
751 { "out_cut_through", 4, 0x18 | GLOBAL_STATS_OP_BANK_1, BANK1, },
752 { "out_octets_a", 4, 0x1a | GLOBAL_STATS_OP_BANK_1, BANK1, },
753 { "out_octets_b", 4, 0x1b | GLOBAL_STATS_OP_BANK_1, BANK1, },
754 { "out_management", 4, 0x1f | GLOBAL_STATS_OP_BANK_1, BANK1, },
Andrew Lunne413e7e2015-04-02 04:06:38 +0200755};
756
Vivien Didelotfad09c72016-06-21 12:28:20 -0400757static bool mv88e6xxx_has_stat(struct mv88e6xxx_chip *chip,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100758 struct mv88e6xxx_hw_stat *stat)
Andrew Lunne413e7e2015-04-02 04:06:38 +0200759{
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100760 switch (stat->type) {
761 case BANK0:
Andrew Lunne413e7e2015-04-02 04:06:38 +0200762 return true;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100763 case BANK1:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400764 return mv88e6xxx_6320_family(chip);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100765 case PORT:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400766 return mv88e6xxx_6095_family(chip) ||
767 mv88e6xxx_6185_family(chip) ||
768 mv88e6xxx_6097_family(chip) ||
769 mv88e6xxx_6165_family(chip) ||
770 mv88e6xxx_6351_family(chip) ||
771 mv88e6xxx_6352_family(chip);
Andrew Lunne413e7e2015-04-02 04:06:38 +0200772 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100773 return false;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000774}
775
Vivien Didelotfad09c72016-06-21 12:28:20 -0400776static uint64_t _mv88e6xxx_get_ethtool_stat(struct mv88e6xxx_chip *chip,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100777 struct mv88e6xxx_hw_stat *s,
Andrew Lunn80c46272015-06-20 18:42:30 +0200778 int port)
779{
Andrew Lunn80c46272015-06-20 18:42:30 +0200780 u32 low;
781 u32 high = 0;
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200782 int err;
783 u16 reg;
Andrew Lunn80c46272015-06-20 18:42:30 +0200784 u64 value;
785
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100786 switch (s->type) {
787 case PORT:
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200788 err = mv88e6xxx_port_read(chip, port, s->reg, &reg);
789 if (err)
Andrew Lunn80c46272015-06-20 18:42:30 +0200790 return UINT64_MAX;
791
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200792 low = reg;
Andrew Lunn80c46272015-06-20 18:42:30 +0200793 if (s->sizeof_stat == 4) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200794 err = mv88e6xxx_port_read(chip, port, s->reg + 1, &reg);
795 if (err)
Andrew Lunn80c46272015-06-20 18:42:30 +0200796 return UINT64_MAX;
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200797 high = reg;
Andrew Lunn80c46272015-06-20 18:42:30 +0200798 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100799 break;
800 case BANK0:
801 case BANK1:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400802 _mv88e6xxx_stats_read(chip, s->reg, &low);
Andrew Lunn80c46272015-06-20 18:42:30 +0200803 if (s->sizeof_stat == 8)
Vivien Didelotfad09c72016-06-21 12:28:20 -0400804 _mv88e6xxx_stats_read(chip, s->reg + 1, &high);
Andrew Lunn80c46272015-06-20 18:42:30 +0200805 }
806 value = (((u64)high) << 16) | low;
807 return value;
808}
809
Vivien Didelotf81ec902016-05-09 13:22:58 -0400810static void mv88e6xxx_get_strings(struct dsa_switch *ds, int port,
811 uint8_t *data)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100812{
Vivien Didelot04bed142016-08-31 18:06:13 -0400813 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100814 struct mv88e6xxx_hw_stat *stat;
815 int i, j;
816
817 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
818 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400819 if (mv88e6xxx_has_stat(chip, stat)) {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100820 memcpy(data + j * ETH_GSTRING_LEN, stat->string,
821 ETH_GSTRING_LEN);
822 j++;
823 }
824 }
825}
826
Vivien Didelotf81ec902016-05-09 13:22:58 -0400827static int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100828{
Vivien Didelot04bed142016-08-31 18:06:13 -0400829 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100830 struct mv88e6xxx_hw_stat *stat;
831 int i, j;
832
833 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
834 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400835 if (mv88e6xxx_has_stat(chip, stat))
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100836 j++;
837 }
838 return j;
839}
840
Vivien Didelotf81ec902016-05-09 13:22:58 -0400841static void mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, int port,
842 uint64_t *data)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000843{
Vivien Didelot04bed142016-08-31 18:06:13 -0400844 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100845 struct mv88e6xxx_hw_stat *stat;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000846 int ret;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100847 int i, j;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000848
Vivien Didelotfad09c72016-06-21 12:28:20 -0400849 mutex_lock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000850
Vivien Didelotfad09c72016-06-21 12:28:20 -0400851 ret = _mv88e6xxx_stats_snapshot(chip, port);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000852 if (ret < 0) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400853 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000854 return;
855 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100856 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
857 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400858 if (mv88e6xxx_has_stat(chip, stat)) {
859 data[j] = _mv88e6xxx_get_ethtool_stat(chip, stat, port);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100860 j++;
861 }
862 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000863
Vivien Didelotfad09c72016-06-21 12:28:20 -0400864 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000865}
Ben Hutchings98e67302011-11-25 14:36:19 +0000866
Vivien Didelotf81ec902016-05-09 13:22:58 -0400867static int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700868{
869 return 32 * sizeof(u16);
870}
871
Vivien Didelotf81ec902016-05-09 13:22:58 -0400872static void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
873 struct ethtool_regs *regs, void *_p)
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700874{
Vivien Didelot04bed142016-08-31 18:06:13 -0400875 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200876 int err;
877 u16 reg;
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700878 u16 *p = _p;
879 int i;
880
881 regs->version = 0;
882
883 memset(p, 0xff, 32 * sizeof(u16));
884
Vivien Didelotfad09c72016-06-21 12:28:20 -0400885 mutex_lock(&chip->reg_lock);
Vivien Didelot23062512016-05-09 13:22:45 -0400886
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700887 for (i = 0; i < 32; i++) {
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700888
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200889 err = mv88e6xxx_port_read(chip, port, i, &reg);
890 if (!err)
891 p[i] = reg;
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700892 }
Vivien Didelot23062512016-05-09 13:22:45 -0400893
Vivien Didelotfad09c72016-06-21 12:28:20 -0400894 mutex_unlock(&chip->reg_lock);
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700895}
896
Vivien Didelotfad09c72016-06-21 12:28:20 -0400897static int _mv88e6xxx_atu_wait(struct mv88e6xxx_chip *chip)
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700898{
Vivien Didelota935c052016-09-29 12:21:53 -0400899 return mv88e6xxx_g1_wait(chip, GLOBAL_ATU_OP, GLOBAL_ATU_OP_BUSY);
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700900}
901
Vivien Didelotf81ec902016-05-09 13:22:58 -0400902static int mv88e6xxx_get_eee(struct dsa_switch *ds, int port,
903 struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -0800904{
Vivien Didelot04bed142016-08-31 18:06:13 -0400905 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot9c938292016-08-15 17:19:02 -0400906 u16 reg;
907 int err;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800908
Vivien Didelotfad09c72016-06-21 12:28:20 -0400909 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
Vivien Didelotaadbdb82016-05-09 13:22:44 -0400910 return -EOPNOTSUPP;
911
Vivien Didelotfad09c72016-06-21 12:28:20 -0400912 mutex_lock(&chip->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200913
Vivien Didelot9c938292016-08-15 17:19:02 -0400914 err = mv88e6xxx_phy_read(chip, port, 16, &reg);
915 if (err)
Andrew Lunn2f40c692015-04-02 04:06:37 +0200916 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800917
918 e->eee_enabled = !!(reg & 0x0200);
919 e->tx_lpi_enabled = !!(reg & 0x0100);
920
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200921 err = mv88e6xxx_port_read(chip, port, PORT_STATUS, &reg);
Vivien Didelot9c938292016-08-15 17:19:02 -0400922 if (err)
Andrew Lunn2f40c692015-04-02 04:06:37 +0200923 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800924
Andrew Lunncca8b132015-04-02 04:06:39 +0200925 e->eee_active = !!(reg & PORT_STATUS_EEE);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200926out:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400927 mutex_unlock(&chip->reg_lock);
Vivien Didelot9c938292016-08-15 17:19:02 -0400928
929 return err;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800930}
931
Vivien Didelotf81ec902016-05-09 13:22:58 -0400932static int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
933 struct phy_device *phydev, struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -0800934{
Vivien Didelot04bed142016-08-31 18:06:13 -0400935 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot9c938292016-08-15 17:19:02 -0400936 u16 reg;
937 int err;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800938
Vivien Didelotfad09c72016-06-21 12:28:20 -0400939 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
Vivien Didelotaadbdb82016-05-09 13:22:44 -0400940 return -EOPNOTSUPP;
941
Vivien Didelotfad09c72016-06-21 12:28:20 -0400942 mutex_lock(&chip->reg_lock);
Guenter Roeck11b3b452015-03-06 22:23:51 -0800943
Vivien Didelot9c938292016-08-15 17:19:02 -0400944 err = mv88e6xxx_phy_read(chip, port, 16, &reg);
945 if (err)
Andrew Lunn2f40c692015-04-02 04:06:37 +0200946 goto out;
947
Vivien Didelot9c938292016-08-15 17:19:02 -0400948 reg &= ~0x0300;
Andrew Lunn2f40c692015-04-02 04:06:37 +0200949 if (e->eee_enabled)
950 reg |= 0x0200;
951 if (e->tx_lpi_enabled)
952 reg |= 0x0100;
953
Vivien Didelot9c938292016-08-15 17:19:02 -0400954 err = mv88e6xxx_phy_write(chip, port, 16, reg);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200955out:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400956 mutex_unlock(&chip->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200957
Vivien Didelot9c938292016-08-15 17:19:02 -0400958 return err;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800959}
960
Vivien Didelotfad09c72016-06-21 12:28:20 -0400961static int _mv88e6xxx_atu_cmd(struct mv88e6xxx_chip *chip, u16 fid, u16 cmd)
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700962{
Vivien Didelota935c052016-09-29 12:21:53 -0400963 u16 val;
964 int err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700965
Vivien Didelot6dc10bb2016-09-29 12:21:55 -0400966 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G1_ATU_FID)) {
Vivien Didelota935c052016-09-29 12:21:53 -0400967 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_FID, fid);
968 if (err)
969 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -0400970 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -0400971 /* ATU DBNum[7:4] are located in ATU Control 15:12 */
Vivien Didelota935c052016-09-29 12:21:53 -0400972 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_CONTROL, &val);
973 if (err)
974 return err;
Vivien Didelot11ea8092016-03-31 16:53:44 -0400975
Vivien Didelota935c052016-09-29 12:21:53 -0400976 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_CONTROL,
977 (val & 0xfff) | ((fid << 8) & 0xf000));
978 if (err)
979 return err;
Vivien Didelot11ea8092016-03-31 16:53:44 -0400980
981 /* ATU DBNum[3:0] are located in ATU Operation 3:0 */
982 cmd |= fid & 0xf;
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400983 }
984
Vivien Didelota935c052016-09-29 12:21:53 -0400985 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_OP, cmd);
986 if (err)
987 return err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700988
Vivien Didelotfad09c72016-06-21 12:28:20 -0400989 return _mv88e6xxx_atu_wait(chip);
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700990}
991
Vivien Didelotfad09c72016-06-21 12:28:20 -0400992static int _mv88e6xxx_atu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot37705b72015-09-04 14:34:11 -0400993 struct mv88e6xxx_atu_entry *entry)
994{
995 u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK;
996
997 if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) {
998 unsigned int mask, shift;
999
1000 if (entry->trunk) {
1001 data |= GLOBAL_ATU_DATA_TRUNK;
1002 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
1003 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
1004 } else {
1005 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
1006 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
1007 }
1008
1009 data |= (entry->portv_trunkid << shift) & mask;
1010 }
1011
Vivien Didelota935c052016-09-29 12:21:53 -04001012 return mv88e6xxx_g1_write(chip, GLOBAL_ATU_DATA, data);
Vivien Didelot37705b72015-09-04 14:34:11 -04001013}
1014
Vivien Didelotfad09c72016-06-21 12:28:20 -04001015static int _mv88e6xxx_atu_flush_move(struct mv88e6xxx_chip *chip,
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001016 struct mv88e6xxx_atu_entry *entry,
1017 bool static_too)
1018{
1019 int op;
1020 int err;
1021
Vivien Didelotfad09c72016-06-21 12:28:20 -04001022 err = _mv88e6xxx_atu_wait(chip);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001023 if (err)
1024 return err;
1025
Vivien Didelotfad09c72016-06-21 12:28:20 -04001026 err = _mv88e6xxx_atu_data_write(chip, entry);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001027 if (err)
1028 return err;
1029
1030 if (entry->fid) {
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001031 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB :
1032 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB;
1033 } else {
1034 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL :
1035 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC;
1036 }
1037
Vivien Didelotfad09c72016-06-21 12:28:20 -04001038 return _mv88e6xxx_atu_cmd(chip, entry->fid, op);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001039}
1040
Vivien Didelotfad09c72016-06-21 12:28:20 -04001041static int _mv88e6xxx_atu_flush(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001042 u16 fid, bool static_too)
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001043{
1044 struct mv88e6xxx_atu_entry entry = {
1045 .fid = fid,
1046 .state = 0, /* EntryState bits must be 0 */
1047 };
1048
Vivien Didelotfad09c72016-06-21 12:28:20 -04001049 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001050}
1051
Vivien Didelotfad09c72016-06-21 12:28:20 -04001052static int _mv88e6xxx_atu_move(struct mv88e6xxx_chip *chip, u16 fid,
Andrew Lunn158bc062016-04-28 21:24:06 -04001053 int from_port, int to_port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001054{
1055 struct mv88e6xxx_atu_entry entry = {
1056 .trunk = false,
1057 .fid = fid,
1058 };
1059
1060 /* EntryState bits must be 0xF */
1061 entry.state = GLOBAL_ATU_DATA_STATE_MASK;
1062
1063 /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */
1064 entry.portv_trunkid = (to_port & 0x0f) << 4;
1065 entry.portv_trunkid |= from_port & 0x0f;
1066
Vivien Didelotfad09c72016-06-21 12:28:20 -04001067 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001068}
1069
Vivien Didelotfad09c72016-06-21 12:28:20 -04001070static int _mv88e6xxx_atu_remove(struct mv88e6xxx_chip *chip, u16 fid,
Andrew Lunn158bc062016-04-28 21:24:06 -04001071 int port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001072{
1073 /* Destination port 0xF means remove the entries */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001074 return _mv88e6xxx_atu_move(chip, fid, port, 0x0f, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001075}
1076
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001077static const char * const mv88e6xxx_port_state_names[] = {
1078 [PORT_CONTROL_STATE_DISABLED] = "Disabled",
1079 [PORT_CONTROL_STATE_BLOCKING] = "Blocking/Listening",
1080 [PORT_CONTROL_STATE_LEARNING] = "Learning",
1081 [PORT_CONTROL_STATE_FORWARDING] = "Forwarding",
1082};
1083
Vivien Didelotfad09c72016-06-21 12:28:20 -04001084static int _mv88e6xxx_port_state(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001085 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001086{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001087 struct dsa_switch *ds = chip->ds;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001088 u16 reg;
1089 int err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001090 u8 oldstate;
1091
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001092 err = mv88e6xxx_port_read(chip, port, PORT_CONTROL, &reg);
1093 if (err)
1094 return err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001095
Andrew Lunncca8b132015-04-02 04:06:39 +02001096 oldstate = reg & PORT_CONTROL_STATE_MASK;
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001097
Vivien Didelot749efcb2016-09-22 16:49:24 -04001098 reg &= ~PORT_CONTROL_STATE_MASK;
1099 reg |= state;
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001100
Vivien Didelot749efcb2016-09-22 16:49:24 -04001101 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL, reg);
1102 if (err)
1103 return err;
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001104
Vivien Didelot749efcb2016-09-22 16:49:24 -04001105 netdev_dbg(ds->ports[port].netdev, "PortState %s (was %s)\n",
1106 mv88e6xxx_port_state_names[state],
1107 mv88e6xxx_port_state_names[oldstate]);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001108
Vivien Didelot749efcb2016-09-22 16:49:24 -04001109 return 0;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001110}
1111
Vivien Didelotfad09c72016-06-21 12:28:20 -04001112static int _mv88e6xxx_port_based_vlan_map(struct mv88e6xxx_chip *chip, int port)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001113{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001114 struct net_device *bridge = chip->ports[port].bridge_dev;
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001115 const u16 mask = (1 << mv88e6xxx_num_ports(chip)) - 1;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001116 struct dsa_switch *ds = chip->ds;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001117 u16 output_ports = 0;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001118 u16 reg;
1119 int err;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001120 int i;
1121
1122 /* allow CPU port or DSA link(s) to send frames to every port */
1123 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
1124 output_ports = mask;
1125 } else {
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001126 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001127 /* allow sending frames to every group member */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001128 if (bridge && chip->ports[i].bridge_dev == bridge)
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001129 output_ports |= BIT(i);
1130
1131 /* allow sending frames to CPU port and DSA link(s) */
1132 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
1133 output_ports |= BIT(i);
1134 }
1135 }
1136
1137 /* prevent frames from going back out of the port they came in on */
1138 output_ports &= ~BIT(port);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001139
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001140 err = mv88e6xxx_port_read(chip, port, PORT_BASE_VLAN, &reg);
1141 if (err)
1142 return err;
Vivien Didelotede80982015-10-11 18:08:35 -04001143
1144 reg &= ~mask;
1145 reg |= output_ports & mask;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001146
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001147 return mv88e6xxx_port_write(chip, port, PORT_BASE_VLAN, reg);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001148}
1149
Vivien Didelotf81ec902016-05-09 13:22:58 -04001150static void mv88e6xxx_port_stp_state_set(struct dsa_switch *ds, int port,
1151 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001152{
Vivien Didelot04bed142016-08-31 18:06:13 -04001153 struct mv88e6xxx_chip *chip = ds->priv;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001154 int stp_state;
Vivien Didelot553eb542016-05-13 20:38:23 -04001155 int err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001156
1157 switch (state) {
1158 case BR_STATE_DISABLED:
Andrew Lunncca8b132015-04-02 04:06:39 +02001159 stp_state = PORT_CONTROL_STATE_DISABLED;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001160 break;
1161 case BR_STATE_BLOCKING:
1162 case BR_STATE_LISTENING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001163 stp_state = PORT_CONTROL_STATE_BLOCKING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001164 break;
1165 case BR_STATE_LEARNING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001166 stp_state = PORT_CONTROL_STATE_LEARNING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001167 break;
1168 case BR_STATE_FORWARDING:
1169 default:
Andrew Lunncca8b132015-04-02 04:06:39 +02001170 stp_state = PORT_CONTROL_STATE_FORWARDING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001171 break;
1172 }
1173
Vivien Didelotfad09c72016-06-21 12:28:20 -04001174 mutex_lock(&chip->reg_lock);
1175 err = _mv88e6xxx_port_state(chip, port, stp_state);
1176 mutex_unlock(&chip->reg_lock);
Vivien Didelot553eb542016-05-13 20:38:23 -04001177
1178 if (err)
Andrew Lunnc8b09802016-06-04 21:16:57 +02001179 netdev_err(ds->ports[port].netdev,
1180 "failed to update state to %s\n",
Vivien Didelot553eb542016-05-13 20:38:23 -04001181 mv88e6xxx_port_state_names[stp_state]);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001182}
1183
Vivien Didelot749efcb2016-09-22 16:49:24 -04001184static void mv88e6xxx_port_fast_age(struct dsa_switch *ds, int port)
1185{
1186 struct mv88e6xxx_chip *chip = ds->priv;
1187 int err;
1188
1189 mutex_lock(&chip->reg_lock);
1190 err = _mv88e6xxx_atu_remove(chip, 0, port, false);
1191 mutex_unlock(&chip->reg_lock);
1192
1193 if (err)
1194 netdev_err(ds->ports[port].netdev, "failed to flush ATU\n");
1195}
1196
Vivien Didelotfad09c72016-06-21 12:28:20 -04001197static int _mv88e6xxx_port_pvid(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001198 u16 *new, u16 *old)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001199{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001200 struct dsa_switch *ds = chip->ds;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001201 u16 pvid, reg;
1202 int err;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001203
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001204 err = mv88e6xxx_port_read(chip, port, PORT_DEFAULT_VLAN, &reg);
1205 if (err)
1206 return err;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001207
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001208 pvid = reg & PORT_DEFAULT_VLAN_MASK;
Vivien Didelot5da96032016-03-07 18:24:39 -05001209
1210 if (new) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001211 reg &= ~PORT_DEFAULT_VLAN_MASK;
1212 reg |= *new & PORT_DEFAULT_VLAN_MASK;
Vivien Didelot5da96032016-03-07 18:24:39 -05001213
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001214 err = mv88e6xxx_port_write(chip, port, PORT_DEFAULT_VLAN, reg);
1215 if (err)
1216 return err;
Vivien Didelot5da96032016-03-07 18:24:39 -05001217
Andrew Lunnc8b09802016-06-04 21:16:57 +02001218 netdev_dbg(ds->ports[port].netdev,
1219 "DefaultVID %d (was %d)\n", *new, pvid);
Vivien Didelot5da96032016-03-07 18:24:39 -05001220 }
1221
1222 if (old)
1223 *old = pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001224
1225 return 0;
1226}
1227
Vivien Didelotfad09c72016-06-21 12:28:20 -04001228static int _mv88e6xxx_port_pvid_get(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001229 int port, u16 *pvid)
Vivien Didelot5da96032016-03-07 18:24:39 -05001230{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001231 return _mv88e6xxx_port_pvid(chip, port, NULL, pvid);
Vivien Didelot5da96032016-03-07 18:24:39 -05001232}
1233
Vivien Didelotfad09c72016-06-21 12:28:20 -04001234static int _mv88e6xxx_port_pvid_set(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001235 int port, u16 pvid)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001236{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001237 return _mv88e6xxx_port_pvid(chip, port, &pvid, NULL);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001238}
1239
Vivien Didelotfad09c72016-06-21 12:28:20 -04001240static int _mv88e6xxx_vtu_wait(struct mv88e6xxx_chip *chip)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001241{
Vivien Didelota935c052016-09-29 12:21:53 -04001242 return mv88e6xxx_g1_wait(chip, GLOBAL_VTU_OP, GLOBAL_VTU_OP_BUSY);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001243}
1244
Vivien Didelotfad09c72016-06-21 12:28:20 -04001245static int _mv88e6xxx_vtu_cmd(struct mv88e6xxx_chip *chip, u16 op)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001246{
Vivien Didelota935c052016-09-29 12:21:53 -04001247 int err;
Vivien Didelot6b17e862015-08-13 12:52:18 -04001248
Vivien Didelota935c052016-09-29 12:21:53 -04001249 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_OP, op);
1250 if (err)
1251 return err;
Vivien Didelot6b17e862015-08-13 12:52:18 -04001252
Vivien Didelotfad09c72016-06-21 12:28:20 -04001253 return _mv88e6xxx_vtu_wait(chip);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001254}
1255
Vivien Didelotfad09c72016-06-21 12:28:20 -04001256static int _mv88e6xxx_vtu_stu_flush(struct mv88e6xxx_chip *chip)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001257{
1258 int ret;
1259
Vivien Didelotfad09c72016-06-21 12:28:20 -04001260 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001261 if (ret < 0)
1262 return ret;
1263
Vivien Didelotfad09c72016-06-21 12:28:20 -04001264 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_FLUSH_ALL);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001265}
1266
Vivien Didelotfad09c72016-06-21 12:28:20 -04001267static int _mv88e6xxx_vtu_stu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001268 struct mv88e6xxx_vtu_entry *entry,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001269 unsigned int nibble_offset)
1270{
Vivien Didelotb8fee952015-08-13 12:52:19 -04001271 u16 regs[3];
Vivien Didelota935c052016-09-29 12:21:53 -04001272 int i, err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001273
1274 for (i = 0; i < 3; ++i) {
Vivien Didelota935c052016-09-29 12:21:53 -04001275 u16 *reg = &regs[i];
Vivien Didelotb8fee952015-08-13 12:52:19 -04001276
Vivien Didelota935c052016-09-29 12:21:53 -04001277 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_DATA_0_3 + i, reg);
1278 if (err)
1279 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001280 }
1281
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001282 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotb8fee952015-08-13 12:52:19 -04001283 unsigned int shift = (i % 4) * 4 + nibble_offset;
1284 u16 reg = regs[i / 4];
1285
1286 entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
1287 }
1288
1289 return 0;
1290}
1291
Vivien Didelotfad09c72016-06-21 12:28:20 -04001292static int mv88e6xxx_vtu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001293 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001294{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001295 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 0);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001296}
1297
Vivien Didelotfad09c72016-06-21 12:28:20 -04001298static int mv88e6xxx_stu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001299 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001300{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001301 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 2);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001302}
1303
Vivien Didelotfad09c72016-06-21 12:28:20 -04001304static int _mv88e6xxx_vtu_stu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001305 struct mv88e6xxx_vtu_entry *entry,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001306 unsigned int nibble_offset)
1307{
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001308 u16 regs[3] = { 0 };
Vivien Didelota935c052016-09-29 12:21:53 -04001309 int i, err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001310
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001311 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001312 unsigned int shift = (i % 4) * 4 + nibble_offset;
1313 u8 data = entry->data[i];
1314
1315 regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
1316 }
1317
1318 for (i = 0; i < 3; ++i) {
Vivien Didelota935c052016-09-29 12:21:53 -04001319 u16 reg = regs[i];
1320
1321 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_DATA_0_3 + i, reg);
1322 if (err)
1323 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001324 }
1325
1326 return 0;
1327}
1328
Vivien Didelotfad09c72016-06-21 12:28:20 -04001329static int mv88e6xxx_vtu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001330 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001331{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001332 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 0);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001333}
1334
Vivien Didelotfad09c72016-06-21 12:28:20 -04001335static int mv88e6xxx_stu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001336 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001337{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001338 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 2);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001339}
1340
Vivien Didelotfad09c72016-06-21 12:28:20 -04001341static int _mv88e6xxx_vtu_vid_write(struct mv88e6xxx_chip *chip, u16 vid)
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001342{
Vivien Didelota935c052016-09-29 12:21:53 -04001343 return mv88e6xxx_g1_write(chip, GLOBAL_VTU_VID,
1344 vid & GLOBAL_VTU_VID_MASK);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001345}
1346
Vivien Didelotfad09c72016-06-21 12:28:20 -04001347static int _mv88e6xxx_vtu_getnext(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001348 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelotb8fee952015-08-13 12:52:19 -04001349{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001350 struct mv88e6xxx_vtu_entry next = { 0 };
Vivien Didelota935c052016-09-29 12:21:53 -04001351 u16 val;
1352 int err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001353
Vivien Didelota935c052016-09-29 12:21:53 -04001354 err = _mv88e6xxx_vtu_wait(chip);
1355 if (err)
1356 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001357
Vivien Didelota935c052016-09-29 12:21:53 -04001358 err = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_VTU_GET_NEXT);
1359 if (err)
1360 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001361
Vivien Didelota935c052016-09-29 12:21:53 -04001362 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_VID, &val);
1363 if (err)
1364 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001365
Vivien Didelota935c052016-09-29 12:21:53 -04001366 next.vid = val & GLOBAL_VTU_VID_MASK;
1367 next.valid = !!(val & GLOBAL_VTU_VID_VALID);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001368
1369 if (next.valid) {
Vivien Didelota935c052016-09-29 12:21:53 -04001370 err = mv88e6xxx_vtu_data_read(chip, &next);
1371 if (err)
1372 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001373
Vivien Didelot6dc10bb2016-09-29 12:21:55 -04001374 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G1_VTU_FID)) {
Vivien Didelota935c052016-09-29 12:21:53 -04001375 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_FID, &val);
1376 if (err)
1377 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001378
Vivien Didelota935c052016-09-29 12:21:53 -04001379 next.fid = val & GLOBAL_VTU_FID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001380 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001381 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1382 * VTU DBNum[3:0] are located in VTU Operation 3:0
1383 */
Vivien Didelota935c052016-09-29 12:21:53 -04001384 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_OP, &val);
1385 if (err)
1386 return err;
Vivien Didelot11ea8092016-03-31 16:53:44 -04001387
Vivien Didelota935c052016-09-29 12:21:53 -04001388 next.fid = (val & 0xf00) >> 4;
1389 next.fid |= val & 0xf;
Vivien Didelot2e7bd5e2016-03-31 16:53:41 -04001390 }
Vivien Didelotb8fee952015-08-13 12:52:19 -04001391
Vivien Didelotfad09c72016-06-21 12:28:20 -04001392 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
Vivien Didelota935c052016-09-29 12:21:53 -04001393 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_SID, &val);
1394 if (err)
1395 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001396
Vivien Didelota935c052016-09-29 12:21:53 -04001397 next.sid = val & GLOBAL_VTU_SID_MASK;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001398 }
1399 }
1400
1401 *entry = next;
1402 return 0;
1403}
1404
Vivien Didelotf81ec902016-05-09 13:22:58 -04001405static int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
1406 struct switchdev_obj_port_vlan *vlan,
1407 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001408{
Vivien Didelot04bed142016-08-31 18:06:13 -04001409 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001410 struct mv88e6xxx_vtu_entry next;
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001411 u16 pvid;
1412 int err;
1413
Vivien Didelotfad09c72016-06-21 12:28:20 -04001414 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001415 return -EOPNOTSUPP;
1416
Vivien Didelotfad09c72016-06-21 12:28:20 -04001417 mutex_lock(&chip->reg_lock);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001418
Vivien Didelotfad09c72016-06-21 12:28:20 -04001419 err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001420 if (err)
1421 goto unlock;
1422
Vivien Didelotfad09c72016-06-21 12:28:20 -04001423 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001424 if (err)
1425 goto unlock;
1426
1427 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001428 err = _mv88e6xxx_vtu_getnext(chip, &next);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001429 if (err)
1430 break;
1431
1432 if (!next.valid)
1433 break;
1434
1435 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1436 continue;
1437
1438 /* reinit and dump this VLAN obj */
Vivien Didelot57d32312016-06-20 13:13:58 -04001439 vlan->vid_begin = next.vid;
1440 vlan->vid_end = next.vid;
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001441 vlan->flags = 0;
1442
1443 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1444 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1445
1446 if (next.vid == pvid)
1447 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1448
1449 err = cb(&vlan->obj);
1450 if (err)
1451 break;
1452 } while (next.vid < GLOBAL_VTU_VID_MASK);
1453
1454unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001455 mutex_unlock(&chip->reg_lock);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001456
1457 return err;
1458}
1459
Vivien Didelotfad09c72016-06-21 12:28:20 -04001460static int _mv88e6xxx_vtu_loadpurge(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001461 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001462{
Vivien Didelot11ea8092016-03-31 16:53:44 -04001463 u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001464 u16 reg = 0;
Vivien Didelota935c052016-09-29 12:21:53 -04001465 int err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001466
Vivien Didelota935c052016-09-29 12:21:53 -04001467 err = _mv88e6xxx_vtu_wait(chip);
1468 if (err)
1469 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001470
1471 if (!entry->valid)
1472 goto loadpurge;
1473
1474 /* Write port member tags */
Vivien Didelota935c052016-09-29 12:21:53 -04001475 err = mv88e6xxx_vtu_data_write(chip, entry);
1476 if (err)
1477 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001478
Vivien Didelotfad09c72016-06-21 12:28:20 -04001479 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001480 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Vivien Didelota935c052016-09-29 12:21:53 -04001481 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_SID, reg);
1482 if (err)
1483 return err;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001484 }
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001485
Vivien Didelot6dc10bb2016-09-29 12:21:55 -04001486 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G1_VTU_FID)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001487 reg = entry->fid & GLOBAL_VTU_FID_MASK;
Vivien Didelota935c052016-09-29 12:21:53 -04001488 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_FID, reg);
1489 if (err)
1490 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001491 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001492 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1493 * VTU DBNum[3:0] are located in VTU Operation 3:0
1494 */
1495 op |= (entry->fid & 0xf0) << 8;
1496 op |= entry->fid & 0xf;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001497 }
1498
1499 reg = GLOBAL_VTU_VID_VALID;
1500loadpurge:
1501 reg |= entry->vid & GLOBAL_VTU_VID_MASK;
Vivien Didelota935c052016-09-29 12:21:53 -04001502 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_VID, reg);
1503 if (err)
1504 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001505
Vivien Didelotfad09c72016-06-21 12:28:20 -04001506 return _mv88e6xxx_vtu_cmd(chip, op);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001507}
1508
Vivien Didelotfad09c72016-06-21 12:28:20 -04001509static int _mv88e6xxx_stu_getnext(struct mv88e6xxx_chip *chip, u8 sid,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001510 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001511{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001512 struct mv88e6xxx_vtu_entry next = { 0 };
Vivien Didelota935c052016-09-29 12:21:53 -04001513 u16 val;
1514 int err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001515
Vivien Didelota935c052016-09-29 12:21:53 -04001516 err = _mv88e6xxx_vtu_wait(chip);
1517 if (err)
1518 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001519
Vivien Didelota935c052016-09-29 12:21:53 -04001520 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_SID,
1521 sid & GLOBAL_VTU_SID_MASK);
1522 if (err)
1523 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001524
Vivien Didelota935c052016-09-29 12:21:53 -04001525 err = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_GET_NEXT);
1526 if (err)
1527 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001528
Vivien Didelota935c052016-09-29 12:21:53 -04001529 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_SID, &val);
1530 if (err)
1531 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001532
Vivien Didelota935c052016-09-29 12:21:53 -04001533 next.sid = val & GLOBAL_VTU_SID_MASK;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001534
Vivien Didelota935c052016-09-29 12:21:53 -04001535 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_VID, &val);
1536 if (err)
1537 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001538
Vivien Didelota935c052016-09-29 12:21:53 -04001539 next.valid = !!(val & GLOBAL_VTU_VID_VALID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001540
1541 if (next.valid) {
Vivien Didelota935c052016-09-29 12:21:53 -04001542 err = mv88e6xxx_stu_data_read(chip, &next);
1543 if (err)
1544 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001545 }
1546
1547 *entry = next;
1548 return 0;
1549}
1550
Vivien Didelotfad09c72016-06-21 12:28:20 -04001551static int _mv88e6xxx_stu_loadpurge(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001552 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001553{
1554 u16 reg = 0;
Vivien Didelota935c052016-09-29 12:21:53 -04001555 int err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001556
Vivien Didelota935c052016-09-29 12:21:53 -04001557 err = _mv88e6xxx_vtu_wait(chip);
1558 if (err)
1559 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001560
1561 if (!entry->valid)
1562 goto loadpurge;
1563
1564 /* Write port states */
Vivien Didelota935c052016-09-29 12:21:53 -04001565 err = mv88e6xxx_stu_data_write(chip, entry);
1566 if (err)
1567 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001568
1569 reg = GLOBAL_VTU_VID_VALID;
1570loadpurge:
Vivien Didelota935c052016-09-29 12:21:53 -04001571 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_VID, reg);
1572 if (err)
1573 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001574
1575 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Vivien Didelota935c052016-09-29 12:21:53 -04001576 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_SID, reg);
1577 if (err)
1578 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001579
Vivien Didelotfad09c72016-06-21 12:28:20 -04001580 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_LOAD_PURGE);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001581}
1582
Vivien Didelotfad09c72016-06-21 12:28:20 -04001583static int _mv88e6xxx_port_fid(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001584 u16 *new, u16 *old)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001585{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001586 struct dsa_switch *ds = chip->ds;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001587 u16 upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001588 u16 fid;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001589 u16 reg;
1590 int err;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001591
Vivien Didelotfad09c72016-06-21 12:28:20 -04001592 if (mv88e6xxx_num_databases(chip) == 4096)
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001593 upper_mask = 0xff;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001594 else if (mv88e6xxx_num_databases(chip) == 256)
Vivien Didelot11ea8092016-03-31 16:53:44 -04001595 upper_mask = 0xf;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001596 else
1597 return -EOPNOTSUPP;
1598
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001599 /* Port's default FID bits 3:0 are located in reg 0x06, offset 12 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001600 err = mv88e6xxx_port_read(chip, port, PORT_BASE_VLAN, &reg);
1601 if (err)
1602 return err;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001603
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001604 fid = (reg & PORT_BASE_VLAN_FID_3_0_MASK) >> 12;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001605
1606 if (new) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001607 reg &= ~PORT_BASE_VLAN_FID_3_0_MASK;
1608 reg |= (*new << 12) & PORT_BASE_VLAN_FID_3_0_MASK;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001609
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001610 err = mv88e6xxx_port_write(chip, port, PORT_BASE_VLAN, reg);
1611 if (err)
1612 return err;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001613 }
1614
1615 /* Port's default FID bits 11:4 are located in reg 0x05, offset 0 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001616 err = mv88e6xxx_port_read(chip, port, PORT_CONTROL_1, &reg);
1617 if (err)
1618 return err;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001619
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001620 fid |= (reg & upper_mask) << 4;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001621
1622 if (new) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001623 reg &= ~upper_mask;
1624 reg |= (*new >> 4) & upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001625
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001626 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_1, reg);
1627 if (err)
1628 return err;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001629
Andrew Lunnc8b09802016-06-04 21:16:57 +02001630 netdev_dbg(ds->ports[port].netdev,
1631 "FID %d (was %d)\n", *new, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001632 }
1633
1634 if (old)
1635 *old = fid;
1636
1637 return 0;
1638}
1639
Vivien Didelotfad09c72016-06-21 12:28:20 -04001640static int _mv88e6xxx_port_fid_get(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001641 int port, u16 *fid)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001642{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001643 return _mv88e6xxx_port_fid(chip, port, NULL, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001644}
1645
Vivien Didelotfad09c72016-06-21 12:28:20 -04001646static int _mv88e6xxx_port_fid_set(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001647 int port, u16 fid)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001648{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001649 return _mv88e6xxx_port_fid(chip, port, &fid, NULL);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001650}
1651
Vivien Didelotfad09c72016-06-21 12:28:20 -04001652static int _mv88e6xxx_fid_new(struct mv88e6xxx_chip *chip, u16 *fid)
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001653{
1654 DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001655 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001656 int i, err;
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001657
1658 bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);
1659
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001660 /* Set every FID bit used by the (un)bridged ports */
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001661 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001662 err = _mv88e6xxx_port_fid_get(chip, i, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001663 if (err)
1664 return err;
1665
1666 set_bit(*fid, fid_bitmap);
1667 }
1668
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001669 /* Set every FID bit used by the VLAN entries */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001670 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001671 if (err)
1672 return err;
1673
1674 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001675 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001676 if (err)
1677 return err;
1678
1679 if (!vlan.valid)
1680 break;
1681
1682 set_bit(vlan.fid, fid_bitmap);
1683 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
1684
1685 /* The reset value 0x000 is used to indicate that multiple address
1686 * databases are not needed. Return the next positive available.
1687 */
1688 *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1);
Vivien Didelotfad09c72016-06-21 12:28:20 -04001689 if (unlikely(*fid >= mv88e6xxx_num_databases(chip)))
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001690 return -ENOSPC;
1691
1692 /* Clear the database */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001693 return _mv88e6xxx_atu_flush(chip, *fid, true);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001694}
1695
Vivien Didelotfad09c72016-06-21 12:28:20 -04001696static int _mv88e6xxx_vtu_new(struct mv88e6xxx_chip *chip, u16 vid,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001697 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001698{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001699 struct dsa_switch *ds = chip->ds;
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001700 struct mv88e6xxx_vtu_entry vlan = {
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001701 .valid = true,
1702 .vid = vid,
1703 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001704 int i, err;
1705
Vivien Didelotfad09c72016-06-21 12:28:20 -04001706 err = _mv88e6xxx_fid_new(chip, &vlan.fid);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001707 if (err)
1708 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001709
Vivien Didelot3d131f02015-11-03 10:52:52 -05001710 /* exclude all ports except the CPU and DSA ports */
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001711 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i)
Vivien Didelot3d131f02015-11-03 10:52:52 -05001712 vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)
1713 ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED
1714 : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001715
Vivien Didelotfad09c72016-06-21 12:28:20 -04001716 if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
1717 mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip)) {
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001718 struct mv88e6xxx_vtu_entry vstp;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001719
1720 /* Adding a VTU entry requires a valid STU entry. As VSTP is not
1721 * implemented, only one STU entry is needed to cover all VTU
1722 * entries. Thus, validate the SID 0.
1723 */
1724 vlan.sid = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001725 err = _mv88e6xxx_stu_getnext(chip, GLOBAL_VTU_SID_MASK, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001726 if (err)
1727 return err;
1728
1729 if (vstp.sid != vlan.sid || !vstp.valid) {
1730 memset(&vstp, 0, sizeof(vstp));
1731 vstp.valid = true;
1732 vstp.sid = vlan.sid;
1733
Vivien Didelotfad09c72016-06-21 12:28:20 -04001734 err = _mv88e6xxx_stu_loadpurge(chip, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001735 if (err)
1736 return err;
1737 }
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001738 }
1739
1740 *entry = vlan;
1741 return 0;
1742}
1743
Vivien Didelotfad09c72016-06-21 12:28:20 -04001744static int _mv88e6xxx_vtu_get(struct mv88e6xxx_chip *chip, u16 vid,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001745 struct mv88e6xxx_vtu_entry *entry, bool creat)
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001746{
1747 int err;
1748
1749 if (!vid)
1750 return -EINVAL;
1751
Vivien Didelotfad09c72016-06-21 12:28:20 -04001752 err = _mv88e6xxx_vtu_vid_write(chip, vid - 1);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001753 if (err)
1754 return err;
1755
Vivien Didelotfad09c72016-06-21 12:28:20 -04001756 err = _mv88e6xxx_vtu_getnext(chip, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001757 if (err)
1758 return err;
1759
1760 if (entry->vid != vid || !entry->valid) {
1761 if (!creat)
1762 return -EOPNOTSUPP;
1763 /* -ENOENT would've been more appropriate, but switchdev expects
1764 * -EOPNOTSUPP to inform bridge about an eventual software VLAN.
1765 */
1766
Vivien Didelotfad09c72016-06-21 12:28:20 -04001767 err = _mv88e6xxx_vtu_new(chip, vid, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001768 }
1769
1770 return err;
1771}
1772
Vivien Didelotda9c3592016-02-12 12:09:40 -05001773static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
1774 u16 vid_begin, u16 vid_end)
1775{
Vivien Didelot04bed142016-08-31 18:06:13 -04001776 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001777 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelotda9c3592016-02-12 12:09:40 -05001778 int i, err;
1779
1780 if (!vid_begin)
1781 return -EOPNOTSUPP;
1782
Vivien Didelotfad09c72016-06-21 12:28:20 -04001783 mutex_lock(&chip->reg_lock);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001784
Vivien Didelotfad09c72016-06-21 12:28:20 -04001785 err = _mv88e6xxx_vtu_vid_write(chip, vid_begin - 1);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001786 if (err)
1787 goto unlock;
1788
1789 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001790 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001791 if (err)
1792 goto unlock;
1793
1794 if (!vlan.valid)
1795 break;
1796
1797 if (vlan.vid > vid_end)
1798 break;
1799
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001800 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotda9c3592016-02-12 12:09:40 -05001801 if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i))
1802 continue;
1803
1804 if (vlan.data[i] ==
1805 GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1806 continue;
1807
Vivien Didelotfad09c72016-06-21 12:28:20 -04001808 if (chip->ports[i].bridge_dev ==
1809 chip->ports[port].bridge_dev)
Vivien Didelotda9c3592016-02-12 12:09:40 -05001810 break; /* same bridge, check next VLAN */
1811
Andrew Lunnc8b09802016-06-04 21:16:57 +02001812 netdev_warn(ds->ports[port].netdev,
Vivien Didelotda9c3592016-02-12 12:09:40 -05001813 "hardware VLAN %d already used by %s\n",
1814 vlan.vid,
Vivien Didelotfad09c72016-06-21 12:28:20 -04001815 netdev_name(chip->ports[i].bridge_dev));
Vivien Didelotda9c3592016-02-12 12:09:40 -05001816 err = -EOPNOTSUPP;
1817 goto unlock;
1818 }
1819 } while (vlan.vid < vid_end);
1820
1821unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001822 mutex_unlock(&chip->reg_lock);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001823
1824 return err;
1825}
1826
Vivien Didelot214cdb92016-02-26 13:16:08 -05001827static const char * const mv88e6xxx_port_8021q_mode_names[] = {
1828 [PORT_CONTROL_2_8021Q_DISABLED] = "Disabled",
1829 [PORT_CONTROL_2_8021Q_FALLBACK] = "Fallback",
1830 [PORT_CONTROL_2_8021Q_CHECK] = "Check",
1831 [PORT_CONTROL_2_8021Q_SECURE] = "Secure",
1832};
1833
Vivien Didelotf81ec902016-05-09 13:22:58 -04001834static int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port,
1835 bool vlan_filtering)
Vivien Didelot214cdb92016-02-26 13:16:08 -05001836{
Vivien Didelot04bed142016-08-31 18:06:13 -04001837 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001838 u16 old, new = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE :
1839 PORT_CONTROL_2_8021Q_DISABLED;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001840 u16 reg;
1841 int err;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001842
Vivien Didelotfad09c72016-06-21 12:28:20 -04001843 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001844 return -EOPNOTSUPP;
1845
Vivien Didelotfad09c72016-06-21 12:28:20 -04001846 mutex_lock(&chip->reg_lock);
Vivien Didelot214cdb92016-02-26 13:16:08 -05001847
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001848 err = mv88e6xxx_port_read(chip, port, PORT_CONTROL_2, &reg);
1849 if (err)
Vivien Didelot214cdb92016-02-26 13:16:08 -05001850 goto unlock;
1851
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001852 old = reg & PORT_CONTROL_2_8021Q_MASK;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001853
Vivien Didelot5220ef12016-03-07 18:24:52 -05001854 if (new != old) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001855 reg &= ~PORT_CONTROL_2_8021Q_MASK;
1856 reg |= new & PORT_CONTROL_2_8021Q_MASK;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001857
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001858 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_2, reg);
1859 if (err)
Vivien Didelot5220ef12016-03-07 18:24:52 -05001860 goto unlock;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001861
Andrew Lunnc8b09802016-06-04 21:16:57 +02001862 netdev_dbg(ds->ports[port].netdev, "802.1Q Mode %s (was %s)\n",
Vivien Didelot5220ef12016-03-07 18:24:52 -05001863 mv88e6xxx_port_8021q_mode_names[new],
1864 mv88e6xxx_port_8021q_mode_names[old]);
1865 }
1866
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001867 err = 0;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001868unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001869 mutex_unlock(&chip->reg_lock);
Vivien Didelot214cdb92016-02-26 13:16:08 -05001870
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001871 return err;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001872}
1873
Vivien Didelot57d32312016-06-20 13:13:58 -04001874static int
1875mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
1876 const struct switchdev_obj_port_vlan *vlan,
1877 struct switchdev_trans *trans)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001878{
Vivien Didelot04bed142016-08-31 18:06:13 -04001879 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotda9c3592016-02-12 12:09:40 -05001880 int err;
1881
Vivien Didelotfad09c72016-06-21 12:28:20 -04001882 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001883 return -EOPNOTSUPP;
1884
Vivien Didelotda9c3592016-02-12 12:09:40 -05001885 /* If the requested port doesn't belong to the same bridge as the VLAN
1886 * members, do not support it (yet) and fallback to software VLAN.
1887 */
1888 err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin,
1889 vlan->vid_end);
1890 if (err)
1891 return err;
1892
Vivien Didelot76e398a2015-11-01 12:33:55 -05001893 /* We don't need any dynamic resource from the kernel (yet),
1894 * so skip the prepare phase.
1895 */
1896 return 0;
1897}
1898
Vivien Didelotfad09c72016-06-21 12:28:20 -04001899static int _mv88e6xxx_port_vlan_add(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001900 u16 vid, bool untagged)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001901{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001902 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001903 int err;
1904
Vivien Didelotfad09c72016-06-21 12:28:20 -04001905 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, true);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001906 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001907 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001908
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001909 vlan.data[port] = untagged ?
1910 GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
1911 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
1912
Vivien Didelotfad09c72016-06-21 12:28:20 -04001913 return _mv88e6xxx_vtu_loadpurge(chip, &vlan);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001914}
1915
Vivien Didelotf81ec902016-05-09 13:22:58 -04001916static void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
1917 const struct switchdev_obj_port_vlan *vlan,
1918 struct switchdev_trans *trans)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001919{
Vivien Didelot04bed142016-08-31 18:06:13 -04001920 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001921 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1922 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1923 u16 vid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001924
Vivien Didelotfad09c72016-06-21 12:28:20 -04001925 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001926 return;
1927
Vivien Didelotfad09c72016-06-21 12:28:20 -04001928 mutex_lock(&chip->reg_lock);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001929
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001930 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
Vivien Didelotfad09c72016-06-21 12:28:20 -04001931 if (_mv88e6xxx_port_vlan_add(chip, port, vid, untagged))
Andrew Lunnc8b09802016-06-04 21:16:57 +02001932 netdev_err(ds->ports[port].netdev,
1933 "failed to add VLAN %d%c\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001934 vid, untagged ? 'u' : 't');
Vivien Didelot76e398a2015-11-01 12:33:55 -05001935
Vivien Didelotfad09c72016-06-21 12:28:20 -04001936 if (pvid && _mv88e6xxx_port_pvid_set(chip, port, vlan->vid_end))
Andrew Lunnc8b09802016-06-04 21:16:57 +02001937 netdev_err(ds->ports[port].netdev, "failed to set PVID %d\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001938 vlan->vid_end);
1939
Vivien Didelotfad09c72016-06-21 12:28:20 -04001940 mutex_unlock(&chip->reg_lock);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001941}
1942
Vivien Didelotfad09c72016-06-21 12:28:20 -04001943static int _mv88e6xxx_port_vlan_del(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001944 int port, u16 vid)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001945{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001946 struct dsa_switch *ds = chip->ds;
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001947 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001948 int i, err;
1949
Vivien Didelotfad09c72016-06-21 12:28:20 -04001950 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001951 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001952 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001953
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001954 /* Tell switchdev if this VLAN is handled in software */
1955 if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
Vivien Didelot3c06f082016-02-05 14:04:39 -05001956 return -EOPNOTSUPP;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001957
1958 vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
1959
1960 /* keep the VLAN unless all ports are excluded */
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001961 vlan.valid = false;
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001962 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelot3d131f02015-11-03 10:52:52 -05001963 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001964 continue;
1965
1966 if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001967 vlan.valid = true;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001968 break;
1969 }
1970 }
1971
Vivien Didelotfad09c72016-06-21 12:28:20 -04001972 err = _mv88e6xxx_vtu_loadpurge(chip, &vlan);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001973 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001974 return err;
1975
Vivien Didelotfad09c72016-06-21 12:28:20 -04001976 return _mv88e6xxx_atu_remove(chip, vlan.fid, port, false);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001977}
1978
Vivien Didelotf81ec902016-05-09 13:22:58 -04001979static int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
1980 const struct switchdev_obj_port_vlan *vlan)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001981{
Vivien Didelot04bed142016-08-31 18:06:13 -04001982 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001983 u16 pvid, vid;
1984 int err = 0;
1985
Vivien Didelotfad09c72016-06-21 12:28:20 -04001986 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001987 return -EOPNOTSUPP;
1988
Vivien Didelotfad09c72016-06-21 12:28:20 -04001989 mutex_lock(&chip->reg_lock);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001990
Vivien Didelotfad09c72016-06-21 12:28:20 -04001991 err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001992 if (err)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001993 goto unlock;
1994
Vivien Didelot76e398a2015-11-01 12:33:55 -05001995 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001996 err = _mv88e6xxx_port_vlan_del(chip, port, vid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001997 if (err)
1998 goto unlock;
1999
2000 if (vid == pvid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002001 err = _mv88e6xxx_port_pvid_set(chip, port, 0);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002002 if (err)
2003 goto unlock;
2004 }
2005 }
2006
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002007unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002008 mutex_unlock(&chip->reg_lock);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002009
2010 return err;
2011}
2012
Vivien Didelotfad09c72016-06-21 12:28:20 -04002013static int _mv88e6xxx_atu_mac_write(struct mv88e6xxx_chip *chip,
Vivien Didelotc5723ac2015-08-10 09:09:48 -04002014 const unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002015{
Vivien Didelota935c052016-09-29 12:21:53 -04002016 int i, err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002017
2018 for (i = 0; i < 3; i++) {
Vivien Didelota935c052016-09-29 12:21:53 -04002019 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_MAC_01 + i,
2020 (addr[i * 2] << 8) | addr[i * 2 + 1]);
2021 if (err)
2022 return err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002023 }
2024
2025 return 0;
2026}
2027
Vivien Didelotfad09c72016-06-21 12:28:20 -04002028static int _mv88e6xxx_atu_mac_read(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04002029 unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002030{
Vivien Didelota935c052016-09-29 12:21:53 -04002031 u16 val;
2032 int i, err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002033
2034 for (i = 0; i < 3; i++) {
Vivien Didelota935c052016-09-29 12:21:53 -04002035 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_MAC_01 + i, &val);
2036 if (err)
2037 return err;
2038
2039 addr[i * 2] = val >> 8;
2040 addr[i * 2 + 1] = val & 0xff;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002041 }
2042
2043 return 0;
2044}
2045
Vivien Didelotfad09c72016-06-21 12:28:20 -04002046static int _mv88e6xxx_atu_load(struct mv88e6xxx_chip *chip,
Vivien Didelotfd231c82015-08-10 09:09:50 -04002047 struct mv88e6xxx_atu_entry *entry)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002048{
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002049 int ret;
2050
Vivien Didelotfad09c72016-06-21 12:28:20 -04002051 ret = _mv88e6xxx_atu_wait(chip);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002052 if (ret < 0)
2053 return ret;
2054
Vivien Didelotfad09c72016-06-21 12:28:20 -04002055 ret = _mv88e6xxx_atu_mac_write(chip, entry->mac);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002056 if (ret < 0)
2057 return ret;
2058
Vivien Didelotfad09c72016-06-21 12:28:20 -04002059 ret = _mv88e6xxx_atu_data_write(chip, entry);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002060 if (ret < 0)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002061 return ret;
2062
Vivien Didelotfad09c72016-06-21 12:28:20 -04002063 return _mv88e6xxx_atu_cmd(chip, entry->fid, GLOBAL_ATU_OP_LOAD_DB);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002064}
David S. Millercdf09692015-08-11 12:00:37 -07002065
Vivien Didelot88472932016-09-19 19:56:11 -04002066static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
2067 struct mv88e6xxx_atu_entry *entry);
2068
2069static int mv88e6xxx_atu_get(struct mv88e6xxx_chip *chip, int fid,
2070 const u8 *addr, struct mv88e6xxx_atu_entry *entry)
2071{
2072 struct mv88e6xxx_atu_entry next;
2073 int err;
2074
2075 eth_broadcast_addr(next.mac);
2076
2077 err = _mv88e6xxx_atu_mac_write(chip, next.mac);
2078 if (err)
2079 return err;
2080
2081 do {
2082 err = _mv88e6xxx_atu_getnext(chip, fid, &next);
2083 if (err)
2084 return err;
2085
2086 if (next.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2087 break;
2088
2089 if (ether_addr_equal(next.mac, addr)) {
2090 *entry = next;
2091 return 0;
2092 }
2093 } while (!is_broadcast_ether_addr(next.mac));
2094
2095 memset(entry, 0, sizeof(*entry));
2096 entry->fid = fid;
2097 ether_addr_copy(entry->mac, addr);
2098
2099 return 0;
2100}
2101
Vivien Didelot83dabd12016-08-31 11:50:04 -04002102static int mv88e6xxx_port_db_load_purge(struct mv88e6xxx_chip *chip, int port,
2103 const unsigned char *addr, u16 vid,
2104 u8 state)
Vivien Didelotfd231c82015-08-10 09:09:50 -04002105{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04002106 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelot88472932016-09-19 19:56:11 -04002107 struct mv88e6xxx_atu_entry entry;
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002108 int err;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002109
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002110 /* Null VLAN ID corresponds to the port private database */
2111 if (vid == 0)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002112 err = _mv88e6xxx_port_fid_get(chip, port, &vlan.fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002113 else
Vivien Didelotfad09c72016-06-21 12:28:20 -04002114 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002115 if (err)
2116 return err;
2117
Vivien Didelot88472932016-09-19 19:56:11 -04002118 err = mv88e6xxx_atu_get(chip, vlan.fid, addr, &entry);
2119 if (err)
2120 return err;
2121
2122 /* Purge the ATU entry only if no port is using it anymore */
2123 if (state == GLOBAL_ATU_DATA_STATE_UNUSED) {
2124 entry.portv_trunkid &= ~BIT(port);
2125 if (!entry.portv_trunkid)
2126 entry.state = GLOBAL_ATU_DATA_STATE_UNUSED;
2127 } else {
2128 entry.portv_trunkid |= BIT(port);
2129 entry.state = state;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002130 }
2131
Vivien Didelotfad09c72016-06-21 12:28:20 -04002132 return _mv88e6xxx_atu_load(chip, &entry);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002133}
2134
Vivien Didelotf81ec902016-05-09 13:22:58 -04002135static int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
2136 const struct switchdev_obj_port_fdb *fdb,
2137 struct switchdev_trans *trans)
Vivien Didelot146a3202015-10-08 11:35:12 -04002138{
2139 /* We don't need any dynamic resource from the kernel (yet),
2140 * so skip the prepare phase.
2141 */
2142 return 0;
2143}
2144
Vivien Didelotf81ec902016-05-09 13:22:58 -04002145static void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
2146 const struct switchdev_obj_port_fdb *fdb,
2147 struct switchdev_trans *trans)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002148{
Vivien Didelot04bed142016-08-31 18:06:13 -04002149 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot6630e232015-08-06 01:44:07 -04002150
Vivien Didelotfad09c72016-06-21 12:28:20 -04002151 mutex_lock(&chip->reg_lock);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002152 if (mv88e6xxx_port_db_load_purge(chip, port, fdb->addr, fdb->vid,
2153 GLOBAL_ATU_DATA_STATE_UC_STATIC))
2154 netdev_err(ds->ports[port].netdev, "failed to load unicast MAC address\n");
Vivien Didelotfad09c72016-06-21 12:28:20 -04002155 mutex_unlock(&chip->reg_lock);
David S. Millercdf09692015-08-11 12:00:37 -07002156}
2157
Vivien Didelotf81ec902016-05-09 13:22:58 -04002158static int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
2159 const struct switchdev_obj_port_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -07002160{
Vivien Didelot04bed142016-08-31 18:06:13 -04002161 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot83dabd12016-08-31 11:50:04 -04002162 int err;
David S. Millercdf09692015-08-11 12:00:37 -07002163
Vivien Didelotfad09c72016-06-21 12:28:20 -04002164 mutex_lock(&chip->reg_lock);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002165 err = mv88e6xxx_port_db_load_purge(chip, port, fdb->addr, fdb->vid,
2166 GLOBAL_ATU_DATA_STATE_UNUSED);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002167 mutex_unlock(&chip->reg_lock);
David S. Millercdf09692015-08-11 12:00:37 -07002168
Vivien Didelot83dabd12016-08-31 11:50:04 -04002169 return err;
David S. Millercdf09692015-08-11 12:00:37 -07002170}
2171
Vivien Didelotfad09c72016-06-21 12:28:20 -04002172static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
Vivien Didelot1d194042015-08-10 09:09:51 -04002173 struct mv88e6xxx_atu_entry *entry)
David S. Millercdf09692015-08-11 12:00:37 -07002174{
Vivien Didelot1d194042015-08-10 09:09:51 -04002175 struct mv88e6xxx_atu_entry next = { 0 };
Vivien Didelota935c052016-09-29 12:21:53 -04002176 u16 val;
2177 int err;
Vivien Didelot1d194042015-08-10 09:09:51 -04002178
2179 next.fid = fid;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002180
Vivien Didelota935c052016-09-29 12:21:53 -04002181 err = _mv88e6xxx_atu_wait(chip);
2182 if (err)
2183 return err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002184
Vivien Didelota935c052016-09-29 12:21:53 -04002185 err = _mv88e6xxx_atu_cmd(chip, fid, GLOBAL_ATU_OP_GET_NEXT_DB);
2186 if (err)
2187 return err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002188
Vivien Didelota935c052016-09-29 12:21:53 -04002189 err = _mv88e6xxx_atu_mac_read(chip, next.mac);
2190 if (err)
2191 return err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002192
Vivien Didelota935c052016-09-29 12:21:53 -04002193 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_DATA, &val);
2194 if (err)
2195 return err;
Vivien Didelot1d194042015-08-10 09:09:51 -04002196
Vivien Didelota935c052016-09-29 12:21:53 -04002197 next.state = val & GLOBAL_ATU_DATA_STATE_MASK;
Vivien Didelot1d194042015-08-10 09:09:51 -04002198 if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2199 unsigned int mask, shift;
2200
Vivien Didelota935c052016-09-29 12:21:53 -04002201 if (val & GLOBAL_ATU_DATA_TRUNK) {
Vivien Didelot1d194042015-08-10 09:09:51 -04002202 next.trunk = true;
2203 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
2204 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
2205 } else {
2206 next.trunk = false;
2207 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
2208 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
2209 }
2210
Vivien Didelota935c052016-09-29 12:21:53 -04002211 next.portv_trunkid = (val & mask) >> shift;
Vivien Didelot1d194042015-08-10 09:09:51 -04002212 }
2213
2214 *entry = next;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002215 return 0;
2216}
2217
Vivien Didelot83dabd12016-08-31 11:50:04 -04002218static int mv88e6xxx_port_db_dump_fid(struct mv88e6xxx_chip *chip,
2219 u16 fid, u16 vid, int port,
2220 struct switchdev_obj *obj,
2221 int (*cb)(struct switchdev_obj *obj))
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002222{
2223 struct mv88e6xxx_atu_entry addr = {
2224 .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2225 };
2226 int err;
2227
Vivien Didelotfad09c72016-06-21 12:28:20 -04002228 err = _mv88e6xxx_atu_mac_write(chip, addr.mac);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002229 if (err)
2230 return err;
2231
2232 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002233 err = _mv88e6xxx_atu_getnext(chip, fid, &addr);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002234 if (err)
Vivien Didelot83dabd12016-08-31 11:50:04 -04002235 return err;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002236
2237 if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2238 break;
2239
Vivien Didelot83dabd12016-08-31 11:50:04 -04002240 if (addr.trunk || (addr.portv_trunkid & BIT(port)) == 0)
2241 continue;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002242
Vivien Didelot83dabd12016-08-31 11:50:04 -04002243 if (obj->id == SWITCHDEV_OBJ_ID_PORT_FDB) {
2244 struct switchdev_obj_port_fdb *fdb;
2245
2246 if (!is_unicast_ether_addr(addr.mac))
2247 continue;
2248
2249 fdb = SWITCHDEV_OBJ_PORT_FDB(obj);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002250 fdb->vid = vid;
2251 ether_addr_copy(fdb->addr, addr.mac);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002252 if (addr.state == GLOBAL_ATU_DATA_STATE_UC_STATIC)
2253 fdb->ndm_state = NUD_NOARP;
2254 else
2255 fdb->ndm_state = NUD_REACHABLE;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04002256 } else if (obj->id == SWITCHDEV_OBJ_ID_PORT_MDB) {
2257 struct switchdev_obj_port_mdb *mdb;
2258
2259 if (!is_multicast_ether_addr(addr.mac))
2260 continue;
2261
2262 mdb = SWITCHDEV_OBJ_PORT_MDB(obj);
2263 mdb->vid = vid;
2264 ether_addr_copy(mdb->addr, addr.mac);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002265 } else {
2266 return -EOPNOTSUPP;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002267 }
Vivien Didelot83dabd12016-08-31 11:50:04 -04002268
2269 err = cb(obj);
2270 if (err)
2271 return err;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002272 } while (!is_broadcast_ether_addr(addr.mac));
2273
2274 return err;
2275}
2276
Vivien Didelot83dabd12016-08-31 11:50:04 -04002277static int mv88e6xxx_port_db_dump(struct mv88e6xxx_chip *chip, int port,
2278 struct switchdev_obj *obj,
2279 int (*cb)(struct switchdev_obj *obj))
2280{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04002281 struct mv88e6xxx_vtu_entry vlan = {
Vivien Didelot83dabd12016-08-31 11:50:04 -04002282 .vid = GLOBAL_VTU_VID_MASK, /* all ones */
2283 };
2284 u16 fid;
2285 int err;
2286
2287 /* Dump port's default Filtering Information Database (VLAN ID 0) */
2288 err = _mv88e6xxx_port_fid_get(chip, port, &fid);
2289 if (err)
2290 return err;
2291
2292 err = mv88e6xxx_port_db_dump_fid(chip, fid, 0, port, obj, cb);
2293 if (err)
2294 return err;
2295
2296 /* Dump VLANs' Filtering Information Databases */
2297 err = _mv88e6xxx_vtu_vid_write(chip, vlan.vid);
2298 if (err)
2299 return err;
2300
2301 do {
2302 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
2303 if (err)
2304 return err;
2305
2306 if (!vlan.valid)
2307 break;
2308
2309 err = mv88e6xxx_port_db_dump_fid(chip, vlan.fid, vlan.vid, port,
2310 obj, cb);
2311 if (err)
2312 return err;
2313 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
2314
2315 return err;
2316}
2317
Vivien Didelotf81ec902016-05-09 13:22:58 -04002318static int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
2319 struct switchdev_obj_port_fdb *fdb,
2320 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotf33475b2015-10-22 09:34:41 -04002321{
Vivien Didelot04bed142016-08-31 18:06:13 -04002322 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002323 int err;
2324
Vivien Didelotfad09c72016-06-21 12:28:20 -04002325 mutex_lock(&chip->reg_lock);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002326 err = mv88e6xxx_port_db_dump(chip, port, &fdb->obj, cb);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002327 mutex_unlock(&chip->reg_lock);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002328
2329 return err;
2330}
2331
Vivien Didelotf81ec902016-05-09 13:22:58 -04002332static int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
2333 struct net_device *bridge)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002334{
Vivien Didelot04bed142016-08-31 18:06:13 -04002335 struct mv88e6xxx_chip *chip = ds->priv;
Colin Ian King1d9619d2016-04-25 23:11:22 +01002336 int i, err = 0;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002337
Vivien Didelotfad09c72016-06-21 12:28:20 -04002338 mutex_lock(&chip->reg_lock);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002339
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002340 /* Assign the bridge and remap each port's VLANTable */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002341 chip->ports[port].bridge_dev = bridge;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002342
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002343 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002344 if (chip->ports[i].bridge_dev == bridge) {
2345 err = _mv88e6xxx_port_based_vlan_map(chip, i);
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002346 if (err)
2347 break;
2348 }
2349 }
2350
Vivien Didelotfad09c72016-06-21 12:28:20 -04002351 mutex_unlock(&chip->reg_lock);
Vivien Didelota6692752016-02-12 12:09:39 -05002352
Vivien Didelot466dfa02016-02-26 13:16:05 -05002353 return err;
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002354}
2355
Vivien Didelotf81ec902016-05-09 13:22:58 -04002356static void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002357{
Vivien Didelot04bed142016-08-31 18:06:13 -04002358 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002359 struct net_device *bridge = chip->ports[port].bridge_dev;
Vivien Didelot16bfa702016-03-13 16:21:33 -04002360 int i;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002361
Vivien Didelotfad09c72016-06-21 12:28:20 -04002362 mutex_lock(&chip->reg_lock);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002363
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002364 /* Unassign the bridge and remap each port's VLANTable */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002365 chip->ports[port].bridge_dev = NULL;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002366
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002367 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002368 if (i == port || chip->ports[i].bridge_dev == bridge)
2369 if (_mv88e6xxx_port_based_vlan_map(chip, i))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002370 netdev_warn(ds->ports[i].netdev,
2371 "failed to remap\n");
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002372
Vivien Didelotfad09c72016-06-21 12:28:20 -04002373 mutex_unlock(&chip->reg_lock);
Vivien Didelot66d9cd02016-02-05 14:07:14 -05002374}
2375
Vivien Didelotfad09c72016-06-21 12:28:20 -04002376static int mv88e6xxx_switch_reset(struct mv88e6xxx_chip *chip)
Vivien Didelot552238b2016-05-09 13:22:49 -04002377{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002378 bool ppu_active = mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE);
Vivien Didelot552238b2016-05-09 13:22:49 -04002379 u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002380 struct gpio_desc *gpiod = chip->reset;
Vivien Didelot552238b2016-05-09 13:22:49 -04002381 unsigned long timeout;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002382 u16 reg;
Vivien Didelota935c052016-09-29 12:21:53 -04002383 int err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002384 int i;
2385
2386 /* Set all ports to the disabled state. */
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002387 for (i = 0; i < mv88e6xxx_num_ports(chip); i++) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002388 err = mv88e6xxx_port_read(chip, i, PORT_CONTROL, &reg);
2389 if (err)
2390 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002391
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002392 err = mv88e6xxx_port_write(chip, i, PORT_CONTROL,
2393 reg & 0xfffc);
2394 if (err)
2395 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002396 }
2397
2398 /* Wait for transmit queues to drain. */
2399 usleep_range(2000, 4000);
2400
2401 /* If there is a gpio connected to the reset pin, toggle it */
2402 if (gpiod) {
2403 gpiod_set_value_cansleep(gpiod, 1);
2404 usleep_range(10000, 20000);
2405 gpiod_set_value_cansleep(gpiod, 0);
2406 usleep_range(10000, 20000);
2407 }
2408
2409 /* Reset the switch. Keep the PPU active if requested. The PPU
2410 * needs to be active to support indirect phy register access
2411 * through global registers 0x18 and 0x19.
2412 */
2413 if (ppu_active)
Vivien Didelota935c052016-09-29 12:21:53 -04002414 err = mv88e6xxx_g1_write(chip, 0x04, 0xc000);
Vivien Didelot552238b2016-05-09 13:22:49 -04002415 else
Vivien Didelota935c052016-09-29 12:21:53 -04002416 err = mv88e6xxx_g1_write(chip, 0x04, 0xc400);
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002417 if (err)
2418 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002419
2420 /* Wait up to one second for reset to complete. */
2421 timeout = jiffies + 1 * HZ;
2422 while (time_before(jiffies, timeout)) {
Vivien Didelota935c052016-09-29 12:21:53 -04002423 err = mv88e6xxx_g1_read(chip, 0x00, &reg);
2424 if (err)
2425 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002426
Vivien Didelota935c052016-09-29 12:21:53 -04002427 if ((reg & is_reset) == is_reset)
Vivien Didelot552238b2016-05-09 13:22:49 -04002428 break;
2429 usleep_range(1000, 2000);
2430 }
2431 if (time_after(jiffies, timeout))
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002432 err = -ETIMEDOUT;
Vivien Didelot552238b2016-05-09 13:22:49 -04002433 else
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002434 err = 0;
Vivien Didelot552238b2016-05-09 13:22:49 -04002435
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002436 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002437}
2438
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002439static int mv88e6xxx_serdes_power_on(struct mv88e6xxx_chip *chip)
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002440{
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002441 u16 val;
2442 int err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002443
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002444 /* Clear Power Down bit */
2445 err = mv88e6xxx_serdes_read(chip, MII_BMCR, &val);
2446 if (err)
2447 return err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002448
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002449 if (val & BMCR_PDOWN) {
2450 val &= ~BMCR_PDOWN;
2451 err = mv88e6xxx_serdes_write(chip, MII_BMCR, val);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002452 }
2453
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002454 return err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002455}
2456
Vivien Didelotfad09c72016-06-21 12:28:20 -04002457static int mv88e6xxx_setup_port(struct mv88e6xxx_chip *chip, int port)
Guenter Roeckd827e882015-03-26 18:36:29 -07002458{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002459 struct dsa_switch *ds = chip->ds;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002460 int err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002461 u16 reg;
Guenter Roeckd827e882015-03-26 18:36:29 -07002462
Vivien Didelotfad09c72016-06-21 12:28:20 -04002463 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2464 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2465 mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip) ||
2466 mv88e6xxx_6065_family(chip) || mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002467 /* MAC Forcing register: don't force link, speed,
2468 * duplex or flow control state to any particular
2469 * values on physical ports, but force the CPU port
2470 * and all DSA ports to their maximum bandwidth and
2471 * full duplex.
2472 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002473 err = mv88e6xxx_port_read(chip, port, PORT_PCS_CTRL, &reg);
Andrew Lunn60045cb2015-08-17 23:52:51 +02002474 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
Russell King53adc9e2015-09-21 21:42:59 +01002475 reg &= ~PORT_PCS_CTRL_UNFORCED;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002476 reg |= PORT_PCS_CTRL_FORCE_LINK |
2477 PORT_PCS_CTRL_LINK_UP |
2478 PORT_PCS_CTRL_DUPLEX_FULL |
2479 PORT_PCS_CTRL_FORCE_DUPLEX;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002480 if (mv88e6xxx_6065_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002481 reg |= PORT_PCS_CTRL_100;
2482 else
2483 reg |= PORT_PCS_CTRL_1000;
2484 } else {
2485 reg |= PORT_PCS_CTRL_UNFORCED;
2486 }
2487
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002488 err = mv88e6xxx_port_write(chip, port, PORT_PCS_CTRL, reg);
2489 if (err)
2490 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002491 }
2492
2493 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
2494 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
2495 * tunneling, determine priority by looking at 802.1p and IP
2496 * priority fields (IP prio has precedence), and set STP state
2497 * to Forwarding.
2498 *
2499 * If this is the CPU link, use DSA or EDSA tagging depending
2500 * on which tagging mode was configured.
2501 *
2502 * If this is a link to another switch, use DSA tagging mode.
2503 *
2504 * If this is the upstream port for this switch, enable
2505 * forwarding of unknown unicasts and multicasts.
2506 */
2507 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002508 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2509 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2510 mv88e6xxx_6095_family(chip) || mv88e6xxx_6065_family(chip) ||
2511 mv88e6xxx_6185_family(chip) || mv88e6xxx_6320_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002512 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
2513 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
2514 PORT_CONTROL_STATE_FORWARDING;
2515 if (dsa_is_cpu_port(ds, port)) {
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002516 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA))
Andrew Lunn5377b802016-06-04 21:17:02 +02002517 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA |
Andrew Lunnc047a1f2015-09-29 01:50:56 +02002518 PORT_CONTROL_FORWARD_UNKNOWN_MC;
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002519 else
2520 reg |= PORT_CONTROL_DSA_TAG;
Jamie Lentinf027e0c2016-08-22 16:01:04 +02002521 reg |= PORT_CONTROL_EGRESS_ADD_TAG |
2522 PORT_CONTROL_FORWARD_UNKNOWN;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002523 }
Andrew Lunn6083ce72015-08-17 23:52:52 +02002524 if (dsa_is_dsa_port(ds, port)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002525 if (mv88e6xxx_6095_family(chip) ||
2526 mv88e6xxx_6185_family(chip))
Andrew Lunn6083ce72015-08-17 23:52:52 +02002527 reg |= PORT_CONTROL_DSA_TAG;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002528 if (mv88e6xxx_6352_family(chip) ||
2529 mv88e6xxx_6351_family(chip) ||
2530 mv88e6xxx_6165_family(chip) ||
2531 mv88e6xxx_6097_family(chip) ||
2532 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002533 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunn6083ce72015-08-17 23:52:52 +02002534 }
2535
Andrew Lunn54d792f2015-05-06 01:09:47 +02002536 if (port == dsa_upstream_port(ds))
2537 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2538 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2539 }
2540 if (reg) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002541 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL, reg);
2542 if (err)
2543 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002544 }
2545
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002546 /* If this port is connected to a SerDes, make sure the SerDes is not
2547 * powered down.
2548 */
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002549 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_SERDES)) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002550 err = mv88e6xxx_port_read(chip, port, PORT_STATUS, &reg);
2551 if (err)
2552 return err;
2553 reg &= PORT_STATUS_CMODE_MASK;
2554 if ((reg == PORT_STATUS_CMODE_100BASE_X) ||
2555 (reg == PORT_STATUS_CMODE_1000BASE_X) ||
2556 (reg == PORT_STATUS_CMODE_SGMII)) {
2557 err = mv88e6xxx_serdes_power_on(chip);
2558 if (err < 0)
2559 return err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002560 }
2561 }
2562
Vivien Didelot8efdda42015-08-13 12:52:23 -04002563 /* Port Control 2: don't force a good FCS, set the maximum frame size to
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002564 * 10240 bytes, disable 802.1q tags checking, don't discard tagged or
Vivien Didelot8efdda42015-08-13 12:52:23 -04002565 * untagged frames on this port, do a destination address lookup on all
2566 * received packets as usual, disable ARP mirroring and don't send a
2567 * copy of all transmitted/received frames on this port to the CPU.
Andrew Lunn54d792f2015-05-06 01:09:47 +02002568 */
2569 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002570 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2571 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2572 mv88e6xxx_6095_family(chip) || mv88e6xxx_6320_family(chip) ||
2573 mv88e6xxx_6185_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002574 reg = PORT_CONTROL_2_MAP_DA;
2575
Vivien Didelotfad09c72016-06-21 12:28:20 -04002576 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2577 mv88e6xxx_6165_family(chip) || mv88e6xxx_6320_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002578 reg |= PORT_CONTROL_2_JUMBO_10240;
2579
Vivien Didelotfad09c72016-06-21 12:28:20 -04002580 if (mv88e6xxx_6095_family(chip) || mv88e6xxx_6185_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002581 /* Set the upstream port this port should use */
2582 reg |= dsa_upstream_port(ds);
2583 /* enable forwarding of unknown multicast addresses to
2584 * the upstream port
2585 */
2586 if (port == dsa_upstream_port(ds))
2587 reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
2588 }
2589
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002590 reg |= PORT_CONTROL_2_8021Q_DISABLED;
Vivien Didelot8efdda42015-08-13 12:52:23 -04002591
Andrew Lunn54d792f2015-05-06 01:09:47 +02002592 if (reg) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002593 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_2, reg);
2594 if (err)
2595 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002596 }
2597
2598 /* Port Association Vector: when learning source addresses
2599 * of packets, add the address to the address database using
2600 * a port bitmap that has only the bit for this port set and
2601 * the other bits clear.
2602 */
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002603 reg = 1 << port;
Vivien Didelot996ecb82016-04-14 14:42:08 -04002604 /* Disable learning for CPU port */
2605 if (dsa_is_cpu_port(ds, port))
Vivien Didelot65fa4022016-04-14 14:42:07 -04002606 reg = 0;
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002607
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002608 err = mv88e6xxx_port_write(chip, port, PORT_ASSOC_VECTOR, reg);
2609 if (err)
2610 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002611
2612 /* Egress rate control 2: disable egress rate control. */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002613 err = mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL_2, 0x0000);
2614 if (err)
2615 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002616
Vivien Didelotfad09c72016-06-21 12:28:20 -04002617 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2618 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2619 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002620 /* Do not limit the period of time that this port can
2621 * be paused for by the remote end or the period of
2622 * time that this port can pause the remote end.
2623 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002624 err = mv88e6xxx_port_write(chip, port, PORT_PAUSE_CTRL, 0x0000);
2625 if (err)
2626 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002627
2628 /* Port ATU control: disable limiting the number of
2629 * address database entries that this port is allowed
2630 * to use.
2631 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002632 err = mv88e6xxx_port_write(chip, port, PORT_ATU_CONTROL,
2633 0x0000);
Andrew Lunn54d792f2015-05-06 01:09:47 +02002634 /* Priority Override: disable DA, SA and VTU priority
2635 * override.
2636 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002637 err = mv88e6xxx_port_write(chip, port, PORT_PRI_OVERRIDE,
2638 0x0000);
2639 if (err)
2640 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002641
2642 /* Port Ethertype: use the Ethertype DSA Ethertype
2643 * value.
2644 */
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002645 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA)) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002646 err = mv88e6xxx_port_write(chip, port, PORT_ETH_TYPE,
2647 ETH_P_EDSA);
2648 if (err)
2649 return err;
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002650 }
2651
Andrew Lunn54d792f2015-05-06 01:09:47 +02002652 /* Tag Remap: use an identity 802.1p prio -> switch
2653 * prio mapping.
2654 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002655 err = mv88e6xxx_port_write(chip, port, PORT_TAG_REGMAP_0123,
2656 0x3210);
2657 if (err)
2658 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002659
2660 /* Tag Remap 2: use an identity 802.1p prio -> switch
2661 * prio mapping.
2662 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002663 err = mv88e6xxx_port_write(chip, port, PORT_TAG_REGMAP_4567,
2664 0x7654);
2665 if (err)
2666 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002667 }
2668
Jamie Lentin1bc261f2016-08-22 22:47:08 +01002669 /* Rate Control: disable ingress rate limiting. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002670 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2671 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
Vivien Didelotfad09c72016-06-21 12:28:20 -04002672 mv88e6xxx_6320_family(chip)) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002673 err = mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL,
2674 0x0001);
2675 if (err)
2676 return err;
Jamie Lentin1bc261f2016-08-22 22:47:08 +01002677 } else if (mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip)) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002678 err = mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL,
2679 0x0000);
2680 if (err)
2681 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002682 }
2683
Guenter Roeck366f0a02015-03-26 18:36:30 -07002684 /* Port Control 1: disable trunking, disable sending
2685 * learning messages to this port.
Guenter Roeckd827e882015-03-26 18:36:29 -07002686 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002687 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_1, 0x0000);
2688 if (err)
2689 return err;
Guenter Roeckd827e882015-03-26 18:36:29 -07002690
Vivien Didelot207afda2016-04-14 14:42:09 -04002691 /* Port based VLAN map: give each port the same default address
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002692 * database, and allow bidirectional communication between the
2693 * CPU and DSA port(s), and the other ports.
Guenter Roeckd827e882015-03-26 18:36:29 -07002694 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002695 err = _mv88e6xxx_port_fid_set(chip, port, 0);
2696 if (err)
2697 return err;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002698
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002699 err = _mv88e6xxx_port_based_vlan_map(chip, port);
2700 if (err)
2701 return err;
Guenter Roeckd827e882015-03-26 18:36:29 -07002702
2703 /* Default VLAN ID and priority: don't set a default VLAN
2704 * ID, and set the default packet priority to zero.
2705 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002706 return mv88e6xxx_port_write(chip, port, PORT_DEFAULT_VLAN, 0x0000);
Andrew Lunndbde9e62015-05-06 01:09:48 +02002707}
2708
Vivien Didelota935c052016-09-29 12:21:53 -04002709int mv88e6xxx_g1_set_switch_mac(struct mv88e6xxx_chip *chip, u8 *addr)
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002710{
2711 int err;
2712
Vivien Didelota935c052016-09-29 12:21:53 -04002713 err = mv88e6xxx_g1_write(chip, GLOBAL_MAC_01, (addr[0] << 8) | addr[1]);
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002714 if (err)
2715 return err;
2716
Vivien Didelota935c052016-09-29 12:21:53 -04002717 err = mv88e6xxx_g1_write(chip, GLOBAL_MAC_23, (addr[2] << 8) | addr[3]);
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002718 if (err)
2719 return err;
2720
Vivien Didelota935c052016-09-29 12:21:53 -04002721 err = mv88e6xxx_g1_write(chip, GLOBAL_MAC_45, (addr[4] << 8) | addr[5]);
2722 if (err)
2723 return err;
2724
2725 return 0;
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002726}
2727
Vivien Didelotacddbd22016-07-18 20:45:39 -04002728static int mv88e6xxx_g1_set_age_time(struct mv88e6xxx_chip *chip,
2729 unsigned int msecs)
2730{
2731 const unsigned int coeff = chip->info->age_time_coeff;
2732 const unsigned int min = 0x01 * coeff;
2733 const unsigned int max = 0xff * coeff;
2734 u8 age_time;
2735 u16 val;
2736 int err;
2737
2738 if (msecs < min || msecs > max)
2739 return -ERANGE;
2740
2741 /* Round to nearest multiple of coeff */
2742 age_time = (msecs + coeff / 2) / coeff;
2743
Vivien Didelota935c052016-09-29 12:21:53 -04002744 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_CONTROL, &val);
Vivien Didelotacddbd22016-07-18 20:45:39 -04002745 if (err)
2746 return err;
2747
2748 /* AgeTime is 11:4 bits */
2749 val &= ~0xff0;
2750 val |= age_time << 4;
2751
Vivien Didelota935c052016-09-29 12:21:53 -04002752 return mv88e6xxx_g1_write(chip, GLOBAL_ATU_CONTROL, val);
Vivien Didelotacddbd22016-07-18 20:45:39 -04002753}
2754
Vivien Didelot2cfcd962016-07-18 20:45:40 -04002755static int mv88e6xxx_set_ageing_time(struct dsa_switch *ds,
2756 unsigned int ageing_time)
2757{
Vivien Didelot04bed142016-08-31 18:06:13 -04002758 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot2cfcd962016-07-18 20:45:40 -04002759 int err;
2760
2761 mutex_lock(&chip->reg_lock);
2762 err = mv88e6xxx_g1_set_age_time(chip, ageing_time);
2763 mutex_unlock(&chip->reg_lock);
2764
2765 return err;
2766}
2767
Vivien Didelot97299342016-07-18 20:45:30 -04002768static int mv88e6xxx_g1_setup(struct mv88e6xxx_chip *chip)
Vivien Didelot08a01262016-05-09 13:22:50 -04002769{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002770 struct dsa_switch *ds = chip->ds;
Vivien Didelotb0745e872016-05-09 13:22:53 -04002771 u32 upstream_port = dsa_upstream_port(ds);
Vivien Didelot119477b2016-05-09 13:22:51 -04002772 u16 reg;
Vivien Didelot08a01262016-05-09 13:22:50 -04002773 int err;
Vivien Didelot08a01262016-05-09 13:22:50 -04002774
Vivien Didelot119477b2016-05-09 13:22:51 -04002775 /* Enable the PHY Polling Unit if present, don't discard any packets,
2776 * and mask all interrupt sources.
2777 */
2778 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002779 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU) ||
2780 mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE))
Vivien Didelot119477b2016-05-09 13:22:51 -04002781 reg |= GLOBAL_CONTROL_PPU_ENABLE;
2782
Vivien Didelota935c052016-09-29 12:21:53 -04002783 err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL, reg);
Vivien Didelot119477b2016-05-09 13:22:51 -04002784 if (err)
2785 return err;
2786
Vivien Didelotb0745e872016-05-09 13:22:53 -04002787 /* Configure the upstream port, and configure it as the port to which
2788 * ingress and egress and ARP monitor frames are to be sent.
2789 */
2790 reg = upstream_port << GLOBAL_MONITOR_CONTROL_INGRESS_SHIFT |
2791 upstream_port << GLOBAL_MONITOR_CONTROL_EGRESS_SHIFT |
2792 upstream_port << GLOBAL_MONITOR_CONTROL_ARP_SHIFT;
Vivien Didelota935c052016-09-29 12:21:53 -04002793 err = mv88e6xxx_g1_write(chip, GLOBAL_MONITOR_CONTROL, reg);
Vivien Didelotb0745e872016-05-09 13:22:53 -04002794 if (err)
2795 return err;
2796
Vivien Didelot50484ff2016-05-09 13:22:54 -04002797 /* Disable remote management, and set the switch's DSA device number. */
Vivien Didelota935c052016-09-29 12:21:53 -04002798 err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL_2,
2799 GLOBAL_CONTROL_2_MULTIPLE_CASCADE |
2800 (ds->index & 0x1f));
Vivien Didelot50484ff2016-05-09 13:22:54 -04002801 if (err)
2802 return err;
2803
Vivien Didelotacddbd22016-07-18 20:45:39 -04002804 /* Clear all the VTU and STU entries */
2805 err = _mv88e6xxx_vtu_stu_flush(chip);
2806 if (err < 0)
2807 return err;
2808
Vivien Didelot08a01262016-05-09 13:22:50 -04002809 /* Set the default address aging time to 5 minutes, and
2810 * enable address learn messages to be sent to all message
2811 * ports.
2812 */
Vivien Didelota935c052016-09-29 12:21:53 -04002813 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_CONTROL,
2814 GLOBAL_ATU_CONTROL_LEARN2ALL);
Vivien Didelot08a01262016-05-09 13:22:50 -04002815 if (err)
2816 return err;
2817
Vivien Didelotacddbd22016-07-18 20:45:39 -04002818 err = mv88e6xxx_g1_set_age_time(chip, 300000);
2819 if (err)
Vivien Didelot97299342016-07-18 20:45:30 -04002820 return err;
2821
2822 /* Clear all ATU entries */
2823 err = _mv88e6xxx_atu_flush(chip, 0, true);
2824 if (err)
2825 return err;
2826
Vivien Didelot08a01262016-05-09 13:22:50 -04002827 /* Configure the IP ToS mapping registers. */
Vivien Didelota935c052016-09-29 12:21:53 -04002828 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_0, 0x0000);
Vivien Didelot08a01262016-05-09 13:22:50 -04002829 if (err)
2830 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002831 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_1, 0x0000);
Vivien Didelot08a01262016-05-09 13:22:50 -04002832 if (err)
2833 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002834 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_2, 0x5555);
Vivien Didelot08a01262016-05-09 13:22:50 -04002835 if (err)
2836 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002837 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_3, 0x5555);
Vivien Didelot08a01262016-05-09 13:22:50 -04002838 if (err)
2839 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002840 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_4, 0xaaaa);
Vivien Didelot08a01262016-05-09 13:22:50 -04002841 if (err)
2842 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002843 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_5, 0xaaaa);
Vivien Didelot08a01262016-05-09 13:22:50 -04002844 if (err)
2845 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002846 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_6, 0xffff);
Vivien Didelot08a01262016-05-09 13:22:50 -04002847 if (err)
2848 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002849 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_7, 0xffff);
Vivien Didelot08a01262016-05-09 13:22:50 -04002850 if (err)
2851 return err;
2852
2853 /* Configure the IEEE 802.1p priority mapping register. */
Vivien Didelota935c052016-09-29 12:21:53 -04002854 err = mv88e6xxx_g1_write(chip, GLOBAL_IEEE_PRI, 0xfa41);
Vivien Didelot08a01262016-05-09 13:22:50 -04002855 if (err)
2856 return err;
2857
Vivien Didelot97299342016-07-18 20:45:30 -04002858 /* Clear the statistics counters for all ports */
Vivien Didelota935c052016-09-29 12:21:53 -04002859 err = mv88e6xxx_g1_write(chip, GLOBAL_STATS_OP,
2860 GLOBAL_STATS_OP_FLUSH_ALL);
Vivien Didelot97299342016-07-18 20:45:30 -04002861 if (err)
2862 return err;
2863
2864 /* Wait for the flush to complete. */
2865 err = _mv88e6xxx_stats_wait(chip);
2866 if (err)
2867 return err;
2868
2869 return 0;
2870}
2871
Vivien Didelotf81ec902016-05-09 13:22:58 -04002872static int mv88e6xxx_setup(struct dsa_switch *ds)
Guenter Roeckacdaffc2015-03-26 18:36:28 -07002873{
Vivien Didelot04bed142016-08-31 18:06:13 -04002874 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot552238b2016-05-09 13:22:49 -04002875 int err;
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002876 int i;
2877
Vivien Didelotfad09c72016-06-21 12:28:20 -04002878 chip->ds = ds;
2879 ds->slave_mii_bus = chip->mdio_bus;
Vivien Didelot552238b2016-05-09 13:22:49 -04002880
Vivien Didelotfad09c72016-06-21 12:28:20 -04002881 mutex_lock(&chip->reg_lock);
Vivien Didelot552238b2016-05-09 13:22:49 -04002882
Vivien Didelotfad09c72016-06-21 12:28:20 -04002883 err = mv88e6xxx_switch_reset(chip);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002884 if (err)
2885 goto unlock;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002886
Vivien Didelot97299342016-07-18 20:45:30 -04002887 /* Setup Switch Port Registers */
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002888 for (i = 0; i < mv88e6xxx_num_ports(chip); i++) {
Vivien Didelot97299342016-07-18 20:45:30 -04002889 err = mv88e6xxx_setup_port(chip, i);
2890 if (err)
2891 goto unlock;
2892 }
2893
2894 /* Setup Switch Global 1 Registers */
2895 err = mv88e6xxx_g1_setup(chip);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002896 if (err)
2897 goto unlock;
2898
Vivien Didelot97299342016-07-18 20:45:30 -04002899 /* Setup Switch Global 2 Registers */
2900 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_GLOBAL2)) {
2901 err = mv88e6xxx_g2_setup(chip);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002902 if (err)
2903 goto unlock;
2904 }
Andrew Lunn54d792f2015-05-06 01:09:47 +02002905
Vivien Didelot6b17e862015-08-13 12:52:18 -04002906unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002907 mutex_unlock(&chip->reg_lock);
Andrew Lunndb687a52015-06-20 21:31:29 +02002908
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002909 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002910}
2911
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002912static int mv88e6xxx_set_addr(struct dsa_switch *ds, u8 *addr)
2913{
Vivien Didelot04bed142016-08-31 18:06:13 -04002914 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002915 int err;
2916
2917 mutex_lock(&chip->reg_lock);
2918
2919 /* Has an indirect Switch MAC/WoL/WoF register in Global 2? */
2920 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_SWITCH_MAC))
2921 err = mv88e6xxx_g2_set_switch_mac(chip, addr);
2922 else
2923 err = mv88e6xxx_g1_set_switch_mac(chip, addr);
2924
2925 mutex_unlock(&chip->reg_lock);
2926
2927 return err;
2928}
2929
Vivien Didelote57e5e72016-08-15 17:19:00 -04002930static int mv88e6xxx_mdio_read(struct mii_bus *bus, int phy, int reg)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002931{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002932 struct mv88e6xxx_chip *chip = bus->priv;
Vivien Didelote57e5e72016-08-15 17:19:00 -04002933 u16 val;
2934 int err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002935
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002936 if (phy >= mv88e6xxx_num_ports(chip))
Andrew Lunn158bc062016-04-28 21:24:06 -04002937 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002938
Vivien Didelotfad09c72016-06-21 12:28:20 -04002939 mutex_lock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04002940 err = mv88e6xxx_phy_read(chip, phy, reg, &val);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002941 mutex_unlock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04002942
2943 return err ? err : val;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002944}
2945
Vivien Didelote57e5e72016-08-15 17:19:00 -04002946static int mv88e6xxx_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002947{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002948 struct mv88e6xxx_chip *chip = bus->priv;
Vivien Didelote57e5e72016-08-15 17:19:00 -04002949 int err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002950
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002951 if (phy >= mv88e6xxx_num_ports(chip))
Andrew Lunn158bc062016-04-28 21:24:06 -04002952 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002953
Vivien Didelotfad09c72016-06-21 12:28:20 -04002954 mutex_lock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04002955 err = mv88e6xxx_phy_write(chip, phy, reg, val);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002956 mutex_unlock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04002957
2958 return err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002959}
2960
Vivien Didelotfad09c72016-06-21 12:28:20 -04002961static int mv88e6xxx_mdio_register(struct mv88e6xxx_chip *chip,
Andrew Lunnb516d452016-06-04 21:17:06 +02002962 struct device_node *np)
2963{
2964 static int index;
2965 struct mii_bus *bus;
2966 int err;
2967
Andrew Lunnb516d452016-06-04 21:17:06 +02002968 if (np)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002969 chip->mdio_np = of_get_child_by_name(np, "mdio");
Andrew Lunnb516d452016-06-04 21:17:06 +02002970
Vivien Didelotfad09c72016-06-21 12:28:20 -04002971 bus = devm_mdiobus_alloc(chip->dev);
Andrew Lunnb516d452016-06-04 21:17:06 +02002972 if (!bus)
2973 return -ENOMEM;
2974
Vivien Didelotfad09c72016-06-21 12:28:20 -04002975 bus->priv = (void *)chip;
Andrew Lunnb516d452016-06-04 21:17:06 +02002976 if (np) {
2977 bus->name = np->full_name;
2978 snprintf(bus->id, MII_BUS_ID_SIZE, "%s", np->full_name);
2979 } else {
2980 bus->name = "mv88e6xxx SMI";
2981 snprintf(bus->id, MII_BUS_ID_SIZE, "mv88e6xxx-%d", index++);
2982 }
2983
2984 bus->read = mv88e6xxx_mdio_read;
2985 bus->write = mv88e6xxx_mdio_write;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002986 bus->parent = chip->dev;
Andrew Lunnb516d452016-06-04 21:17:06 +02002987
Vivien Didelotfad09c72016-06-21 12:28:20 -04002988 if (chip->mdio_np)
2989 err = of_mdiobus_register(bus, chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02002990 else
2991 err = mdiobus_register(bus);
2992 if (err) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002993 dev_err(chip->dev, "Cannot register MDIO bus (%d)\n", err);
Andrew Lunnb516d452016-06-04 21:17:06 +02002994 goto out;
2995 }
Vivien Didelotfad09c72016-06-21 12:28:20 -04002996 chip->mdio_bus = bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02002997
2998 return 0;
2999
3000out:
Vivien Didelotfad09c72016-06-21 12:28:20 -04003001 if (chip->mdio_np)
3002 of_node_put(chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003003
3004 return err;
3005}
3006
Vivien Didelotfad09c72016-06-21 12:28:20 -04003007static void mv88e6xxx_mdio_unregister(struct mv88e6xxx_chip *chip)
Andrew Lunnb516d452016-06-04 21:17:06 +02003008
3009{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003010 struct mii_bus *bus = chip->mdio_bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003011
3012 mdiobus_unregister(bus);
3013
Vivien Didelotfad09c72016-06-21 12:28:20 -04003014 if (chip->mdio_np)
3015 of_node_put(chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003016}
3017
Guenter Roeckc22995c2015-07-25 09:42:28 -07003018#ifdef CONFIG_NET_DSA_HWMON
3019
3020static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
3021{
Vivien Didelot04bed142016-08-31 18:06:13 -04003022 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot9c938292016-08-15 17:19:02 -04003023 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003024 int ret;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003025
3026 *temp = 0;
3027
Vivien Didelotfad09c72016-06-21 12:28:20 -04003028 mutex_lock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003029
Vivien Didelot9c938292016-08-15 17:19:02 -04003030 ret = mv88e6xxx_phy_write(chip, 0x0, 0x16, 0x6);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003031 if (ret < 0)
3032 goto error;
3033
3034 /* Enable temperature sensor */
Vivien Didelot9c938292016-08-15 17:19:02 -04003035 ret = mv88e6xxx_phy_read(chip, 0x0, 0x1a, &val);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003036 if (ret < 0)
3037 goto error;
3038
Vivien Didelot9c938292016-08-15 17:19:02 -04003039 ret = mv88e6xxx_phy_write(chip, 0x0, 0x1a, val | (1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003040 if (ret < 0)
3041 goto error;
3042
3043 /* Wait for temperature to stabilize */
3044 usleep_range(10000, 12000);
3045
Vivien Didelot9c938292016-08-15 17:19:02 -04003046 ret = mv88e6xxx_phy_read(chip, 0x0, 0x1a, &val);
3047 if (ret < 0)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003048 goto error;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003049
3050 /* Disable temperature sensor */
Vivien Didelot9c938292016-08-15 17:19:02 -04003051 ret = mv88e6xxx_phy_write(chip, 0x0, 0x1a, val & ~(1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003052 if (ret < 0)
3053 goto error;
3054
3055 *temp = ((val & 0x1f) - 5) * 5;
3056
3057error:
Vivien Didelot9c938292016-08-15 17:19:02 -04003058 mv88e6xxx_phy_write(chip, 0x0, 0x16, 0x0);
Vivien Didelotfad09c72016-06-21 12:28:20 -04003059 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003060 return ret;
3061}
3062
3063static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
3064{
Vivien Didelot04bed142016-08-31 18:06:13 -04003065 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003066 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003067 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003068 int ret;
3069
3070 *temp = 0;
3071
Vivien Didelot9c938292016-08-15 17:19:02 -04003072 mutex_lock(&chip->reg_lock);
3073 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 27, &val);
3074 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003075 if (ret < 0)
3076 return ret;
3077
Vivien Didelot9c938292016-08-15 17:19:02 -04003078 *temp = (val & 0xff) - 25;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003079
3080 return 0;
3081}
3082
Vivien Didelotf81ec902016-05-09 13:22:58 -04003083static int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003084{
Vivien Didelot04bed142016-08-31 18:06:13 -04003085 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn158bc062016-04-28 21:24:06 -04003086
Vivien Didelotfad09c72016-06-21 12:28:20 -04003087 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP))
Vivien Didelot6594f612016-05-09 13:22:42 -04003088 return -EOPNOTSUPP;
3089
Vivien Didelotfad09c72016-06-21 12:28:20 -04003090 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003091 return mv88e63xx_get_temp(ds, temp);
3092
3093 return mv88e61xx_get_temp(ds, temp);
3094}
3095
Vivien Didelotf81ec902016-05-09 13:22:58 -04003096static int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003097{
Vivien Didelot04bed142016-08-31 18:06:13 -04003098 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003099 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003100 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003101 int ret;
3102
Vivien Didelotfad09c72016-06-21 12:28:20 -04003103 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003104 return -EOPNOTSUPP;
3105
3106 *temp = 0;
3107
Vivien Didelot9c938292016-08-15 17:19:02 -04003108 mutex_lock(&chip->reg_lock);
3109 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3110 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003111 if (ret < 0)
3112 return ret;
3113
Vivien Didelot9c938292016-08-15 17:19:02 -04003114 *temp = (((val >> 8) & 0x1f) * 5) - 25;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003115
3116 return 0;
3117}
3118
Vivien Didelotf81ec902016-05-09 13:22:58 -04003119static int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003120{
Vivien Didelot04bed142016-08-31 18:06:13 -04003121 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003122 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003123 u16 val;
3124 int err;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003125
Vivien Didelotfad09c72016-06-21 12:28:20 -04003126 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003127 return -EOPNOTSUPP;
3128
Vivien Didelot9c938292016-08-15 17:19:02 -04003129 mutex_lock(&chip->reg_lock);
3130 err = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3131 if (err)
3132 goto unlock;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003133 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
Vivien Didelot9c938292016-08-15 17:19:02 -04003134 err = mv88e6xxx_phy_page_write(chip, phy, 6, 26,
3135 (val & 0xe0ff) | (temp << 8));
3136unlock:
3137 mutex_unlock(&chip->reg_lock);
3138
3139 return err;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003140}
3141
Vivien Didelotf81ec902016-05-09 13:22:58 -04003142static int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003143{
Vivien Didelot04bed142016-08-31 18:06:13 -04003144 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003145 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003146 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003147 int ret;
3148
Vivien Didelotfad09c72016-06-21 12:28:20 -04003149 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003150 return -EOPNOTSUPP;
3151
3152 *alarm = false;
3153
Vivien Didelot9c938292016-08-15 17:19:02 -04003154 mutex_lock(&chip->reg_lock);
3155 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3156 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003157 if (ret < 0)
3158 return ret;
3159
Vivien Didelot9c938292016-08-15 17:19:02 -04003160 *alarm = !!(val & 0x40);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003161
3162 return 0;
3163}
3164#endif /* CONFIG_NET_DSA_HWMON */
3165
Vivien Didelot855b1932016-07-20 18:18:35 -04003166static int mv88e6xxx_get_eeprom_len(struct dsa_switch *ds)
3167{
Vivien Didelot04bed142016-08-31 18:06:13 -04003168 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot855b1932016-07-20 18:18:35 -04003169
3170 return chip->eeprom_len;
3171}
3172
Vivien Didelot855b1932016-07-20 18:18:35 -04003173static int mv88e6xxx_get_eeprom(struct dsa_switch *ds,
3174 struct ethtool_eeprom *eeprom, u8 *data)
3175{
Vivien Didelot04bed142016-08-31 18:06:13 -04003176 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot855b1932016-07-20 18:18:35 -04003177 int err;
3178
3179 mutex_lock(&chip->reg_lock);
3180
3181 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16))
Vivien Didelotec561272016-09-02 14:45:33 -04003182 err = mv88e6xxx_g2_get_eeprom16(chip, eeprom, data);
Vivien Didelot855b1932016-07-20 18:18:35 -04003183 else
3184 err = -EOPNOTSUPP;
3185
3186 mutex_unlock(&chip->reg_lock);
3187
3188 if (err)
3189 return err;
3190
3191 eeprom->magic = 0xc3ec4951;
3192
3193 return 0;
3194}
3195
Vivien Didelot855b1932016-07-20 18:18:35 -04003196static int mv88e6xxx_set_eeprom(struct dsa_switch *ds,
3197 struct ethtool_eeprom *eeprom, u8 *data)
3198{
Vivien Didelot04bed142016-08-31 18:06:13 -04003199 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot855b1932016-07-20 18:18:35 -04003200 int err;
3201
3202 if (eeprom->magic != 0xc3ec4951)
3203 return -EINVAL;
3204
3205 mutex_lock(&chip->reg_lock);
3206
3207 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16))
Vivien Didelotec561272016-09-02 14:45:33 -04003208 err = mv88e6xxx_g2_set_eeprom16(chip, eeprom, data);
Vivien Didelot855b1932016-07-20 18:18:35 -04003209 else
3210 err = -EOPNOTSUPP;
3211
3212 mutex_unlock(&chip->reg_lock);
3213
3214 return err;
3215}
3216
Vivien Didelotf81ec902016-05-09 13:22:58 -04003217static const struct mv88e6xxx_info mv88e6xxx_table[] = {
3218 [MV88E6085] = {
3219 .prod_num = PORT_SWITCH_ID_PROD_NUM_6085,
3220 .family = MV88E6XXX_FAMILY_6097,
3221 .name = "Marvell 88E6085",
3222 .num_databases = 4096,
3223 .num_ports = 10,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003224 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003225 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003226 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003227 .flags = MV88E6XXX_FLAGS_FAMILY_6097,
3228 },
3229
3230 [MV88E6095] = {
3231 .prod_num = PORT_SWITCH_ID_PROD_NUM_6095,
3232 .family = MV88E6XXX_FAMILY_6095,
3233 .name = "Marvell 88E6095/88E6095F",
3234 .num_databases = 256,
3235 .num_ports = 11,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003236 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003237 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003238 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003239 .flags = MV88E6XXX_FLAGS_FAMILY_6095,
3240 },
3241
3242 [MV88E6123] = {
3243 .prod_num = PORT_SWITCH_ID_PROD_NUM_6123,
3244 .family = MV88E6XXX_FAMILY_6165,
3245 .name = "Marvell 88E6123",
3246 .num_databases = 4096,
3247 .num_ports = 3,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003248 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003249 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003250 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003251 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3252 },
3253
3254 [MV88E6131] = {
3255 .prod_num = PORT_SWITCH_ID_PROD_NUM_6131,
3256 .family = MV88E6XXX_FAMILY_6185,
3257 .name = "Marvell 88E6131",
3258 .num_databases = 256,
3259 .num_ports = 8,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003260 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003261 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003262 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003263 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3264 },
3265
3266 [MV88E6161] = {
3267 .prod_num = PORT_SWITCH_ID_PROD_NUM_6161,
3268 .family = MV88E6XXX_FAMILY_6165,
3269 .name = "Marvell 88E6161",
3270 .num_databases = 4096,
3271 .num_ports = 6,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003272 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003273 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003274 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003275 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3276 },
3277
3278 [MV88E6165] = {
3279 .prod_num = PORT_SWITCH_ID_PROD_NUM_6165,
3280 .family = MV88E6XXX_FAMILY_6165,
3281 .name = "Marvell 88E6165",
3282 .num_databases = 4096,
3283 .num_ports = 6,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003284 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003285 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003286 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003287 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3288 },
3289
3290 [MV88E6171] = {
3291 .prod_num = PORT_SWITCH_ID_PROD_NUM_6171,
3292 .family = MV88E6XXX_FAMILY_6351,
3293 .name = "Marvell 88E6171",
3294 .num_databases = 4096,
3295 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003296 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003297 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003298 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003299 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3300 },
3301
3302 [MV88E6172] = {
3303 .prod_num = PORT_SWITCH_ID_PROD_NUM_6172,
3304 .family = MV88E6XXX_FAMILY_6352,
3305 .name = "Marvell 88E6172",
3306 .num_databases = 4096,
3307 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003308 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003309 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003310 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003311 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3312 },
3313
3314 [MV88E6175] = {
3315 .prod_num = PORT_SWITCH_ID_PROD_NUM_6175,
3316 .family = MV88E6XXX_FAMILY_6351,
3317 .name = "Marvell 88E6175",
3318 .num_databases = 4096,
3319 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003320 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003321 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003322 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003323 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3324 },
3325
3326 [MV88E6176] = {
3327 .prod_num = PORT_SWITCH_ID_PROD_NUM_6176,
3328 .family = MV88E6XXX_FAMILY_6352,
3329 .name = "Marvell 88E6176",
3330 .num_databases = 4096,
3331 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003332 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003333 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003334 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003335 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3336 },
3337
3338 [MV88E6185] = {
3339 .prod_num = PORT_SWITCH_ID_PROD_NUM_6185,
3340 .family = MV88E6XXX_FAMILY_6185,
3341 .name = "Marvell 88E6185",
3342 .num_databases = 256,
3343 .num_ports = 10,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003344 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003345 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003346 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003347 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3348 },
3349
3350 [MV88E6240] = {
3351 .prod_num = PORT_SWITCH_ID_PROD_NUM_6240,
3352 .family = MV88E6XXX_FAMILY_6352,
3353 .name = "Marvell 88E6240",
3354 .num_databases = 4096,
3355 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003356 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003357 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003358 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003359 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3360 },
3361
3362 [MV88E6320] = {
3363 .prod_num = PORT_SWITCH_ID_PROD_NUM_6320,
3364 .family = MV88E6XXX_FAMILY_6320,
3365 .name = "Marvell 88E6320",
3366 .num_databases = 4096,
3367 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003368 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003369 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003370 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003371 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3372 },
3373
3374 [MV88E6321] = {
3375 .prod_num = PORT_SWITCH_ID_PROD_NUM_6321,
3376 .family = MV88E6XXX_FAMILY_6320,
3377 .name = "Marvell 88E6321",
3378 .num_databases = 4096,
3379 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003380 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003381 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003382 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003383 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3384 },
3385
3386 [MV88E6350] = {
3387 .prod_num = PORT_SWITCH_ID_PROD_NUM_6350,
3388 .family = MV88E6XXX_FAMILY_6351,
3389 .name = "Marvell 88E6350",
3390 .num_databases = 4096,
3391 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003392 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003393 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003394 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003395 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3396 },
3397
3398 [MV88E6351] = {
3399 .prod_num = PORT_SWITCH_ID_PROD_NUM_6351,
3400 .family = MV88E6XXX_FAMILY_6351,
3401 .name = "Marvell 88E6351",
3402 .num_databases = 4096,
3403 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003404 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003405 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003406 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003407 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3408 },
3409
3410 [MV88E6352] = {
3411 .prod_num = PORT_SWITCH_ID_PROD_NUM_6352,
3412 .family = MV88E6XXX_FAMILY_6352,
3413 .name = "Marvell 88E6352",
3414 .num_databases = 4096,
3415 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003416 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003417 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003418 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003419 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3420 },
3421};
3422
Vivien Didelot5f7c0362016-06-20 13:14:04 -04003423static const struct mv88e6xxx_info *mv88e6xxx_lookup_info(unsigned int prod_num)
Vivien Didelotb9b37712015-10-30 19:39:48 -04003424{
Vivien Didelota439c062016-04-17 13:23:58 -04003425 int i;
Vivien Didelotb9b37712015-10-30 19:39:48 -04003426
Vivien Didelot5f7c0362016-06-20 13:14:04 -04003427 for (i = 0; i < ARRAY_SIZE(mv88e6xxx_table); ++i)
3428 if (mv88e6xxx_table[i].prod_num == prod_num)
3429 return &mv88e6xxx_table[i];
Vivien Didelotb9b37712015-10-30 19:39:48 -04003430
Vivien Didelotb9b37712015-10-30 19:39:48 -04003431 return NULL;
3432}
3433
Vivien Didelotfad09c72016-06-21 12:28:20 -04003434static int mv88e6xxx_detect(struct mv88e6xxx_chip *chip)
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003435{
3436 const struct mv88e6xxx_info *info;
Vivien Didelot8f6345b2016-07-20 18:18:36 -04003437 unsigned int prod_num, rev;
3438 u16 id;
3439 int err;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003440
Vivien Didelot8f6345b2016-07-20 18:18:36 -04003441 mutex_lock(&chip->reg_lock);
3442 err = mv88e6xxx_port_read(chip, 0, PORT_SWITCH_ID, &id);
3443 mutex_unlock(&chip->reg_lock);
3444 if (err)
3445 return err;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003446
3447 prod_num = (id & 0xfff0) >> 4;
3448 rev = id & 0x000f;
3449
3450 info = mv88e6xxx_lookup_info(prod_num);
3451 if (!info)
3452 return -ENODEV;
3453
Vivien Didelotcaac8542016-06-20 13:14:09 -04003454 /* Update the compatible info with the probed one */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003455 chip->info = info;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003456
Vivien Didelotca070c12016-09-02 14:45:34 -04003457 err = mv88e6xxx_g2_require(chip);
3458 if (err)
3459 return err;
3460
Vivien Didelotfad09c72016-06-21 12:28:20 -04003461 dev_info(chip->dev, "switch 0x%x detected: %s, revision %u\n",
3462 chip->info->prod_num, chip->info->name, rev);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003463
3464 return 0;
3465}
3466
Vivien Didelotfad09c72016-06-21 12:28:20 -04003467static struct mv88e6xxx_chip *mv88e6xxx_alloc_chip(struct device *dev)
Vivien Didelot469d7292016-06-20 13:14:06 -04003468{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003469 struct mv88e6xxx_chip *chip;
Vivien Didelot469d7292016-06-20 13:14:06 -04003470
Vivien Didelotfad09c72016-06-21 12:28:20 -04003471 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
3472 if (!chip)
Vivien Didelot469d7292016-06-20 13:14:06 -04003473 return NULL;
3474
Vivien Didelotfad09c72016-06-21 12:28:20 -04003475 chip->dev = dev;
Vivien Didelot469d7292016-06-20 13:14:06 -04003476
Vivien Didelotfad09c72016-06-21 12:28:20 -04003477 mutex_init(&chip->reg_lock);
Vivien Didelot469d7292016-06-20 13:14:06 -04003478
Vivien Didelotfad09c72016-06-21 12:28:20 -04003479 return chip;
Vivien Didelot469d7292016-06-20 13:14:06 -04003480}
3481
Vivien Didelotc08026a2016-09-29 12:21:59 -04003482static const struct mv88e6xxx_bus_ops mv88e6xxx_g2_smi_phy_ops = {
Vivien Didelotec561272016-09-02 14:45:33 -04003483 .read = mv88e6xxx_g2_smi_phy_read,
3484 .write = mv88e6xxx_g2_smi_phy_write,
3485};
3486
Vivien Didelotc08026a2016-09-29 12:21:59 -04003487static const struct mv88e6xxx_bus_ops mv88e6xxx_phy_ops = {
Vivien Didelote57e5e72016-08-15 17:19:00 -04003488 .read = mv88e6xxx_read,
3489 .write = mv88e6xxx_write,
3490};
3491
3492static void mv88e6xxx_phy_init(struct mv88e6xxx_chip *chip)
3493{
3494 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_SMI_PHY)) {
3495 chip->phy_ops = &mv88e6xxx_g2_smi_phy_ops;
3496 } else if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU)) {
3497 chip->phy_ops = &mv88e6xxx_phy_ppu_ops;
3498 mv88e6xxx_ppu_state_init(chip);
3499 } else {
3500 chip->phy_ops = &mv88e6xxx_phy_ops;
3501 }
3502}
3503
Andrew Lunn930188c2016-08-22 16:01:03 +02003504static void mv88e6xxx_phy_destroy(struct mv88e6xxx_chip *chip)
3505{
3506 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU)) {
3507 mv88e6xxx_ppu_state_destroy(chip);
3508 }
3509}
3510
Vivien Didelotfad09c72016-06-21 12:28:20 -04003511static int mv88e6xxx_smi_init(struct mv88e6xxx_chip *chip,
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003512 struct mii_bus *bus, int sw_addr)
3513{
3514 /* ADDR[0] pin is unavailable externally and considered zero */
3515 if (sw_addr & 0x1)
3516 return -EINVAL;
3517
Vivien Didelot914b32f2016-06-20 13:14:11 -04003518 if (sw_addr == 0)
Vivien Didelotfad09c72016-06-21 12:28:20 -04003519 chip->smi_ops = &mv88e6xxx_smi_single_chip_ops;
Vivien Didelota0ffff22016-08-15 17:18:58 -04003520 else if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_MULTI_CHIP))
Vivien Didelotfad09c72016-06-21 12:28:20 -04003521 chip->smi_ops = &mv88e6xxx_smi_multi_chip_ops;
Vivien Didelot914b32f2016-06-20 13:14:11 -04003522 else
3523 return -EINVAL;
3524
Vivien Didelotfad09c72016-06-21 12:28:20 -04003525 chip->bus = bus;
3526 chip->sw_addr = sw_addr;
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003527
3528 return 0;
3529}
3530
Andrew Lunn7b314362016-08-22 16:01:01 +02003531static enum dsa_tag_protocol mv88e6xxx_get_tag_protocol(struct dsa_switch *ds)
3532{
Vivien Didelot04bed142016-08-31 18:06:13 -04003533 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02003534
3535 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA))
3536 return DSA_TAG_PROTO_EDSA;
3537
3538 return DSA_TAG_PROTO_DSA;
Andrew Lunn7b314362016-08-22 16:01:01 +02003539}
3540
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02003541static const char *mv88e6xxx_drv_probe(struct device *dsa_dev,
3542 struct device *host_dev, int sw_addr,
3543 void **priv)
Andrew Lunna77d43f2016-04-13 02:40:42 +02003544{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003545 struct mv88e6xxx_chip *chip;
Vivien Didelota439c062016-04-17 13:23:58 -04003546 struct mii_bus *bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003547 int err;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003548
Vivien Didelota439c062016-04-17 13:23:58 -04003549 bus = dsa_host_dev_to_mii_bus(host_dev);
Andrew Lunnc1569132016-04-13 02:40:45 +02003550 if (!bus)
3551 return NULL;
3552
Vivien Didelotfad09c72016-06-21 12:28:20 -04003553 chip = mv88e6xxx_alloc_chip(dsa_dev);
3554 if (!chip)
Vivien Didelot469d7292016-06-20 13:14:06 -04003555 return NULL;
3556
Vivien Didelotcaac8542016-06-20 13:14:09 -04003557 /* Legacy SMI probing will only support chips similar to 88E6085 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003558 chip->info = &mv88e6xxx_table[MV88E6085];
Vivien Didelotcaac8542016-06-20 13:14:09 -04003559
Vivien Didelotfad09c72016-06-21 12:28:20 -04003560 err = mv88e6xxx_smi_init(chip, bus, sw_addr);
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003561 if (err)
3562 goto free;
3563
Vivien Didelotfad09c72016-06-21 12:28:20 -04003564 err = mv88e6xxx_detect(chip);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003565 if (err)
Vivien Didelot469d7292016-06-20 13:14:06 -04003566 goto free;
Vivien Didelota439c062016-04-17 13:23:58 -04003567
Vivien Didelote57e5e72016-08-15 17:19:00 -04003568 mv88e6xxx_phy_init(chip);
3569
Vivien Didelotfad09c72016-06-21 12:28:20 -04003570 err = mv88e6xxx_mdio_register(chip, NULL);
Andrew Lunnb516d452016-06-04 21:17:06 +02003571 if (err)
Vivien Didelot469d7292016-06-20 13:14:06 -04003572 goto free;
Andrew Lunnb516d452016-06-04 21:17:06 +02003573
Vivien Didelotfad09c72016-06-21 12:28:20 -04003574 *priv = chip;
Vivien Didelota439c062016-04-17 13:23:58 -04003575
Vivien Didelotfad09c72016-06-21 12:28:20 -04003576 return chip->info->name;
Vivien Didelot469d7292016-06-20 13:14:06 -04003577free:
Vivien Didelotfad09c72016-06-21 12:28:20 -04003578 devm_kfree(dsa_dev, chip);
Vivien Didelot469d7292016-06-20 13:14:06 -04003579
3580 return NULL;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003581}
3582
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003583static int mv88e6xxx_port_mdb_prepare(struct dsa_switch *ds, int port,
3584 const struct switchdev_obj_port_mdb *mdb,
3585 struct switchdev_trans *trans)
3586{
3587 /* We don't need any dynamic resource from the kernel (yet),
3588 * so skip the prepare phase.
3589 */
3590
3591 return 0;
3592}
3593
3594static void mv88e6xxx_port_mdb_add(struct dsa_switch *ds, int port,
3595 const struct switchdev_obj_port_mdb *mdb,
3596 struct switchdev_trans *trans)
3597{
Vivien Didelot04bed142016-08-31 18:06:13 -04003598 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003599
3600 mutex_lock(&chip->reg_lock);
3601 if (mv88e6xxx_port_db_load_purge(chip, port, mdb->addr, mdb->vid,
3602 GLOBAL_ATU_DATA_STATE_MC_STATIC))
3603 netdev_err(ds->ports[port].netdev, "failed to load multicast MAC address\n");
3604 mutex_unlock(&chip->reg_lock);
3605}
3606
3607static int mv88e6xxx_port_mdb_del(struct dsa_switch *ds, int port,
3608 const struct switchdev_obj_port_mdb *mdb)
3609{
Vivien Didelot04bed142016-08-31 18:06:13 -04003610 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003611 int err;
3612
3613 mutex_lock(&chip->reg_lock);
3614 err = mv88e6xxx_port_db_load_purge(chip, port, mdb->addr, mdb->vid,
3615 GLOBAL_ATU_DATA_STATE_UNUSED);
3616 mutex_unlock(&chip->reg_lock);
3617
3618 return err;
3619}
3620
3621static int mv88e6xxx_port_mdb_dump(struct dsa_switch *ds, int port,
3622 struct switchdev_obj_port_mdb *mdb,
3623 int (*cb)(struct switchdev_obj *obj))
3624{
Vivien Didelot04bed142016-08-31 18:06:13 -04003625 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003626 int err;
3627
3628 mutex_lock(&chip->reg_lock);
3629 err = mv88e6xxx_port_db_dump(chip, port, &mdb->obj, cb);
3630 mutex_unlock(&chip->reg_lock);
3631
3632 return err;
3633}
3634
Vivien Didelot9d490b42016-08-23 12:38:56 -04003635static struct dsa_switch_ops mv88e6xxx_switch_ops = {
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02003636 .probe = mv88e6xxx_drv_probe,
Andrew Lunn7b314362016-08-22 16:01:01 +02003637 .get_tag_protocol = mv88e6xxx_get_tag_protocol,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003638 .setup = mv88e6xxx_setup,
3639 .set_addr = mv88e6xxx_set_addr,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003640 .adjust_link = mv88e6xxx_adjust_link,
3641 .get_strings = mv88e6xxx_get_strings,
3642 .get_ethtool_stats = mv88e6xxx_get_ethtool_stats,
3643 .get_sset_count = mv88e6xxx_get_sset_count,
3644 .set_eee = mv88e6xxx_set_eee,
3645 .get_eee = mv88e6xxx_get_eee,
3646#ifdef CONFIG_NET_DSA_HWMON
3647 .get_temp = mv88e6xxx_get_temp,
3648 .get_temp_limit = mv88e6xxx_get_temp_limit,
3649 .set_temp_limit = mv88e6xxx_set_temp_limit,
3650 .get_temp_alarm = mv88e6xxx_get_temp_alarm,
3651#endif
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003652 .get_eeprom_len = mv88e6xxx_get_eeprom_len,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003653 .get_eeprom = mv88e6xxx_get_eeprom,
3654 .set_eeprom = mv88e6xxx_set_eeprom,
3655 .get_regs_len = mv88e6xxx_get_regs_len,
3656 .get_regs = mv88e6xxx_get_regs,
Vivien Didelot2cfcd962016-07-18 20:45:40 -04003657 .set_ageing_time = mv88e6xxx_set_ageing_time,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003658 .port_bridge_join = mv88e6xxx_port_bridge_join,
3659 .port_bridge_leave = mv88e6xxx_port_bridge_leave,
3660 .port_stp_state_set = mv88e6xxx_port_stp_state_set,
Vivien Didelot749efcb2016-09-22 16:49:24 -04003661 .port_fast_age = mv88e6xxx_port_fast_age,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003662 .port_vlan_filtering = mv88e6xxx_port_vlan_filtering,
3663 .port_vlan_prepare = mv88e6xxx_port_vlan_prepare,
3664 .port_vlan_add = mv88e6xxx_port_vlan_add,
3665 .port_vlan_del = mv88e6xxx_port_vlan_del,
3666 .port_vlan_dump = mv88e6xxx_port_vlan_dump,
3667 .port_fdb_prepare = mv88e6xxx_port_fdb_prepare,
3668 .port_fdb_add = mv88e6xxx_port_fdb_add,
3669 .port_fdb_del = mv88e6xxx_port_fdb_del,
3670 .port_fdb_dump = mv88e6xxx_port_fdb_dump,
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003671 .port_mdb_prepare = mv88e6xxx_port_mdb_prepare,
3672 .port_mdb_add = mv88e6xxx_port_mdb_add,
3673 .port_mdb_del = mv88e6xxx_port_mdb_del,
3674 .port_mdb_dump = mv88e6xxx_port_mdb_dump,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003675};
3676
Vivien Didelotfad09c72016-06-21 12:28:20 -04003677static int mv88e6xxx_register_switch(struct mv88e6xxx_chip *chip,
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003678 struct device_node *np)
3679{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003680 struct device *dev = chip->dev;
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003681 struct dsa_switch *ds;
3682
3683 ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
3684 if (!ds)
3685 return -ENOMEM;
3686
3687 ds->dev = dev;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003688 ds->priv = chip;
Vivien Didelot9d490b42016-08-23 12:38:56 -04003689 ds->ops = &mv88e6xxx_switch_ops;
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003690
3691 dev_set_drvdata(dev, ds);
3692
3693 return dsa_register_switch(ds, np);
3694}
3695
Vivien Didelotfad09c72016-06-21 12:28:20 -04003696static void mv88e6xxx_unregister_switch(struct mv88e6xxx_chip *chip)
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003697{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003698 dsa_unregister_switch(chip->ds);
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003699}
3700
Vivien Didelot57d32312016-06-20 13:13:58 -04003701static int mv88e6xxx_probe(struct mdio_device *mdiodev)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003702{
3703 struct device *dev = &mdiodev->dev;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003704 struct device_node *np = dev->of_node;
Vivien Didelotcaac8542016-06-20 13:14:09 -04003705 const struct mv88e6xxx_info *compat_info;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003706 struct mv88e6xxx_chip *chip;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003707 u32 eeprom_len;
Andrew Lunn52638f72016-05-10 23:27:22 +02003708 int err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003709
Vivien Didelotcaac8542016-06-20 13:14:09 -04003710 compat_info = of_device_get_match_data(dev);
3711 if (!compat_info)
3712 return -EINVAL;
3713
Vivien Didelotfad09c72016-06-21 12:28:20 -04003714 chip = mv88e6xxx_alloc_chip(dev);
3715 if (!chip)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003716 return -ENOMEM;
3717
Vivien Didelotfad09c72016-06-21 12:28:20 -04003718 chip->info = compat_info;
Vivien Didelotcaac8542016-06-20 13:14:09 -04003719
Vivien Didelotfad09c72016-06-21 12:28:20 -04003720 err = mv88e6xxx_smi_init(chip, mdiodev->bus, mdiodev->addr);
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003721 if (err)
3722 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003723
Vivien Didelotfad09c72016-06-21 12:28:20 -04003724 err = mv88e6xxx_detect(chip);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003725 if (err)
3726 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003727
Vivien Didelote57e5e72016-08-15 17:19:00 -04003728 mv88e6xxx_phy_init(chip);
3729
Vivien Didelotfad09c72016-06-21 12:28:20 -04003730 chip->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_ASIS);
3731 if (IS_ERR(chip->reset))
3732 return PTR_ERR(chip->reset);
Andrew Lunn52638f72016-05-10 23:27:22 +02003733
Vivien Didelot855b1932016-07-20 18:18:35 -04003734 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16) &&
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003735 !of_property_read_u32(np, "eeprom-length", &eeprom_len))
Vivien Didelotfad09c72016-06-21 12:28:20 -04003736 chip->eeprom_len = eeprom_len;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003737
Vivien Didelotfad09c72016-06-21 12:28:20 -04003738 err = mv88e6xxx_mdio_register(chip, np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003739 if (err)
3740 return err;
3741
Vivien Didelotfad09c72016-06-21 12:28:20 -04003742 err = mv88e6xxx_register_switch(chip, np);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02003743 if (err) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04003744 mv88e6xxx_mdio_unregister(chip);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02003745 return err;
3746 }
3747
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003748 return 0;
3749}
3750
3751static void mv88e6xxx_remove(struct mdio_device *mdiodev)
3752{
3753 struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev);
Vivien Didelot04bed142016-08-31 18:06:13 -04003754 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003755
Andrew Lunn930188c2016-08-22 16:01:03 +02003756 mv88e6xxx_phy_destroy(chip);
Vivien Didelotfad09c72016-06-21 12:28:20 -04003757 mv88e6xxx_unregister_switch(chip);
3758 mv88e6xxx_mdio_unregister(chip);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003759}
3760
3761static const struct of_device_id mv88e6xxx_of_match[] = {
Vivien Didelotcaac8542016-06-20 13:14:09 -04003762 {
3763 .compatible = "marvell,mv88e6085",
3764 .data = &mv88e6xxx_table[MV88E6085],
3765 },
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003766 { /* sentinel */ },
3767};
3768
3769MODULE_DEVICE_TABLE(of, mv88e6xxx_of_match);
3770
3771static struct mdio_driver mv88e6xxx_driver = {
3772 .probe = mv88e6xxx_probe,
3773 .remove = mv88e6xxx_remove,
3774 .mdiodrv.driver = {
3775 .name = "mv88e6085",
3776 .of_match_table = mv88e6xxx_of_match,
3777 },
3778};
3779
Ben Hutchings98e67302011-11-25 14:36:19 +00003780static int __init mv88e6xxx_init(void)
3781{
Vivien Didelot9d490b42016-08-23 12:38:56 -04003782 register_switch_driver(&mv88e6xxx_switch_ops);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003783 return mdio_driver_register(&mv88e6xxx_driver);
Ben Hutchings98e67302011-11-25 14:36:19 +00003784}
3785module_init(mv88e6xxx_init);
3786
3787static void __exit mv88e6xxx_cleanup(void)
3788{
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003789 mdio_driver_unregister(&mv88e6xxx_driver);
Vivien Didelot9d490b42016-08-23 12:38:56 -04003790 unregister_switch_driver(&mv88e6xxx_switch_ops);
Ben Hutchings98e67302011-11-25 14:36:19 +00003791}
3792module_exit(mv88e6xxx_cleanup);
Ben Hutchings3d825ed2011-11-25 14:37:16 +00003793
3794MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
3795MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
3796MODULE_LICENSE("GPL");