blob: 98dee2c631639550dff96b0f64d20f4f2e657277 [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
101static const struct mv88e6xxx_ops mv88e6xxx_smi_single_chip_ops = {
102 .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
183static const struct mv88e6xxx_ops mv88e6xxx_smi_multi_chip_ops = {
184 .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 Didelote57e5e72016-08-15 17:19:00 -0400518static const struct mv88e6xxx_ops mv88e6xxx_phy_ppu_ops = {
519 .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
Vivien Didelotfad09c72016-06-21 12:28:20 -0400563static unsigned int mv88e6xxx_num_databases(struct mv88e6xxx_chip *chip)
Vivien Didelotf74df0b2016-03-31 16:53:43 -0400564{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400565 return chip->info->num_databases;
Vivien Didelotf74df0b2016-03-31 16:53:43 -0400566}
567
Vivien Didelotfad09c72016-06-21 12:28:20 -0400568static bool mv88e6xxx_has_fid_reg(struct mv88e6xxx_chip *chip)
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400569{
570 /* Does the device have dedicated FID registers for ATU and VTU ops? */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400571 if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
572 mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip))
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400573 return true;
574
575 return false;
576}
577
Andrew Lunndea87022015-08-31 15:56:47 +0200578/* We expect the switch to perform auto negotiation if there is a real
579 * phy. However, in the case of a fixed link phy, we force the port
580 * settings from the fixed link settings.
581 */
Vivien Didelotf81ec902016-05-09 13:22:58 -0400582static void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port,
583 struct phy_device *phydev)
Andrew Lunndea87022015-08-31 15:56:47 +0200584{
Vivien Didelot04bed142016-08-31 18:06:13 -0400585 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200586 u16 reg;
587 int err;
Andrew Lunndea87022015-08-31 15:56:47 +0200588
589 if (!phy_is_pseudo_fixed_link(phydev))
590 return;
591
Vivien Didelotfad09c72016-06-21 12:28:20 -0400592 mutex_lock(&chip->reg_lock);
Andrew Lunndea87022015-08-31 15:56:47 +0200593
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200594 err = mv88e6xxx_port_read(chip, port, PORT_PCS_CTRL, &reg);
595 if (err)
Andrew Lunndea87022015-08-31 15:56:47 +0200596 goto out;
597
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200598 reg &= ~(PORT_PCS_CTRL_LINK_UP |
599 PORT_PCS_CTRL_FORCE_LINK |
600 PORT_PCS_CTRL_DUPLEX_FULL |
601 PORT_PCS_CTRL_FORCE_DUPLEX |
602 PORT_PCS_CTRL_UNFORCED);
Andrew Lunndea87022015-08-31 15:56:47 +0200603
604 reg |= PORT_PCS_CTRL_FORCE_LINK;
605 if (phydev->link)
Vivien Didelot57d32312016-06-20 13:13:58 -0400606 reg |= PORT_PCS_CTRL_LINK_UP;
Andrew Lunndea87022015-08-31 15:56:47 +0200607
Vivien Didelotfad09c72016-06-21 12:28:20 -0400608 if (mv88e6xxx_6065_family(chip) && phydev->speed > SPEED_100)
Andrew Lunndea87022015-08-31 15:56:47 +0200609 goto out;
610
611 switch (phydev->speed) {
612 case SPEED_1000:
613 reg |= PORT_PCS_CTRL_1000;
614 break;
615 case SPEED_100:
616 reg |= PORT_PCS_CTRL_100;
617 break;
618 case SPEED_10:
619 reg |= PORT_PCS_CTRL_10;
620 break;
621 default:
622 pr_info("Unknown speed");
623 goto out;
624 }
625
626 reg |= PORT_PCS_CTRL_FORCE_DUPLEX;
627 if (phydev->duplex == DUPLEX_FULL)
628 reg |= PORT_PCS_CTRL_DUPLEX_FULL;
629
Vivien Didelotfad09c72016-06-21 12:28:20 -0400630 if ((mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip)) &&
631 (port >= chip->info->num_ports - 2)) {
Andrew Lunne7e72ac2015-08-31 15:56:51 +0200632 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
633 reg |= PORT_PCS_CTRL_RGMII_DELAY_RXCLK;
634 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
635 reg |= PORT_PCS_CTRL_RGMII_DELAY_TXCLK;
636 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
637 reg |= (PORT_PCS_CTRL_RGMII_DELAY_RXCLK |
638 PORT_PCS_CTRL_RGMII_DELAY_TXCLK);
639 }
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200640 mv88e6xxx_port_write(chip, port, PORT_PCS_CTRL, reg);
Andrew Lunndea87022015-08-31 15:56:47 +0200641
642out:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400643 mutex_unlock(&chip->reg_lock);
Andrew Lunndea87022015-08-31 15:56:47 +0200644}
645
Vivien Didelotfad09c72016-06-21 12:28:20 -0400646static int _mv88e6xxx_stats_wait(struct mv88e6xxx_chip *chip)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000647{
Vivien Didelota935c052016-09-29 12:21:53 -0400648 u16 val;
649 int i, err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000650
651 for (i = 0; i < 10; i++) {
Vivien Didelota935c052016-09-29 12:21:53 -0400652 err = mv88e6xxx_g1_read(chip, GLOBAL_STATS_OP, &val);
653 if ((val & GLOBAL_STATS_OP_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000654 return 0;
655 }
656
657 return -ETIMEDOUT;
658}
659
Vivien Didelotfad09c72016-06-21 12:28:20 -0400660static int _mv88e6xxx_stats_snapshot(struct mv88e6xxx_chip *chip, int port)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000661{
Vivien Didelota935c052016-09-29 12:21:53 -0400662 int err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000663
Vivien Didelotfad09c72016-06-21 12:28:20 -0400664 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200665 port = (port + 1) << 5;
666
Barry Grussling3675c8d2013-01-08 16:05:53 +0000667 /* Snapshot the hardware statistics counters for this port. */
Vivien Didelota935c052016-09-29 12:21:53 -0400668 err = mv88e6xxx_g1_write(chip, GLOBAL_STATS_OP,
669 GLOBAL_STATS_OP_CAPTURE_PORT |
670 GLOBAL_STATS_OP_HIST_RX_TX | port);
671 if (err)
672 return err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000673
Barry Grussling3675c8d2013-01-08 16:05:53 +0000674 /* Wait for the snapshotting to complete. */
Vivien Didelota935c052016-09-29 12:21:53 -0400675 return _mv88e6xxx_stats_wait(chip);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000676}
677
Vivien Didelotfad09c72016-06-21 12:28:20 -0400678static void _mv88e6xxx_stats_read(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -0400679 int stat, u32 *val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000680{
Vivien Didelota935c052016-09-29 12:21:53 -0400681 u32 value;
682 u16 reg;
683 int err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000684
685 *val = 0;
686
Vivien Didelota935c052016-09-29 12:21:53 -0400687 err = mv88e6xxx_g1_write(chip, GLOBAL_STATS_OP,
688 GLOBAL_STATS_OP_READ_CAPTURED |
689 GLOBAL_STATS_OP_HIST_RX_TX | stat);
690 if (err)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000691 return;
692
Vivien Didelota935c052016-09-29 12:21:53 -0400693 err = _mv88e6xxx_stats_wait(chip);
694 if (err)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000695 return;
696
Vivien Didelota935c052016-09-29 12:21:53 -0400697 err = mv88e6xxx_g1_read(chip, GLOBAL_STATS_COUNTER_32, &reg);
698 if (err)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000699 return;
700
Vivien Didelota935c052016-09-29 12:21:53 -0400701 value = reg << 16;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000702
Vivien Didelota935c052016-09-29 12:21:53 -0400703 err = mv88e6xxx_g1_read(chip, GLOBAL_STATS_COUNTER_01, &reg);
704 if (err)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000705 return;
706
Vivien Didelota935c052016-09-29 12:21:53 -0400707 *val = value | reg;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000708}
709
Andrew Lunne413e7e2015-04-02 04:06:38 +0200710static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100711 { "in_good_octets", 8, 0x00, BANK0, },
712 { "in_bad_octets", 4, 0x02, BANK0, },
713 { "in_unicast", 4, 0x04, BANK0, },
714 { "in_broadcasts", 4, 0x06, BANK0, },
715 { "in_multicasts", 4, 0x07, BANK0, },
716 { "in_pause", 4, 0x16, BANK0, },
717 { "in_undersize", 4, 0x18, BANK0, },
718 { "in_fragments", 4, 0x19, BANK0, },
719 { "in_oversize", 4, 0x1a, BANK0, },
720 { "in_jabber", 4, 0x1b, BANK0, },
721 { "in_rx_error", 4, 0x1c, BANK0, },
722 { "in_fcs_error", 4, 0x1d, BANK0, },
723 { "out_octets", 8, 0x0e, BANK0, },
724 { "out_unicast", 4, 0x10, BANK0, },
725 { "out_broadcasts", 4, 0x13, BANK0, },
726 { "out_multicasts", 4, 0x12, BANK0, },
727 { "out_pause", 4, 0x15, BANK0, },
728 { "excessive", 4, 0x11, BANK0, },
729 { "collisions", 4, 0x1e, BANK0, },
730 { "deferred", 4, 0x05, BANK0, },
731 { "single", 4, 0x14, BANK0, },
732 { "multiple", 4, 0x17, BANK0, },
733 { "out_fcs_error", 4, 0x03, BANK0, },
734 { "late", 4, 0x1f, BANK0, },
735 { "hist_64bytes", 4, 0x08, BANK0, },
736 { "hist_65_127bytes", 4, 0x09, BANK0, },
737 { "hist_128_255bytes", 4, 0x0a, BANK0, },
738 { "hist_256_511bytes", 4, 0x0b, BANK0, },
739 { "hist_512_1023bytes", 4, 0x0c, BANK0, },
740 { "hist_1024_max_bytes", 4, 0x0d, BANK0, },
741 { "sw_in_discards", 4, 0x10, PORT, },
742 { "sw_in_filtered", 2, 0x12, PORT, },
743 { "sw_out_filtered", 2, 0x13, PORT, },
744 { "in_discards", 4, 0x00 | GLOBAL_STATS_OP_BANK_1, BANK1, },
745 { "in_filtered", 4, 0x01 | GLOBAL_STATS_OP_BANK_1, BANK1, },
746 { "in_accepted", 4, 0x02 | GLOBAL_STATS_OP_BANK_1, BANK1, },
747 { "in_bad_accepted", 4, 0x03 | GLOBAL_STATS_OP_BANK_1, BANK1, },
748 { "in_good_avb_class_a", 4, 0x04 | GLOBAL_STATS_OP_BANK_1, BANK1, },
749 { "in_good_avb_class_b", 4, 0x05 | GLOBAL_STATS_OP_BANK_1, BANK1, },
750 { "in_bad_avb_class_a", 4, 0x06 | GLOBAL_STATS_OP_BANK_1, BANK1, },
751 { "in_bad_avb_class_b", 4, 0x07 | GLOBAL_STATS_OP_BANK_1, BANK1, },
752 { "tcam_counter_0", 4, 0x08 | GLOBAL_STATS_OP_BANK_1, BANK1, },
753 { "tcam_counter_1", 4, 0x09 | GLOBAL_STATS_OP_BANK_1, BANK1, },
754 { "tcam_counter_2", 4, 0x0a | GLOBAL_STATS_OP_BANK_1, BANK1, },
755 { "tcam_counter_3", 4, 0x0b | GLOBAL_STATS_OP_BANK_1, BANK1, },
756 { "in_da_unknown", 4, 0x0e | GLOBAL_STATS_OP_BANK_1, BANK1, },
757 { "in_management", 4, 0x0f | GLOBAL_STATS_OP_BANK_1, BANK1, },
758 { "out_queue_0", 4, 0x10 | GLOBAL_STATS_OP_BANK_1, BANK1, },
759 { "out_queue_1", 4, 0x11 | GLOBAL_STATS_OP_BANK_1, BANK1, },
760 { "out_queue_2", 4, 0x12 | GLOBAL_STATS_OP_BANK_1, BANK1, },
761 { "out_queue_3", 4, 0x13 | GLOBAL_STATS_OP_BANK_1, BANK1, },
762 { "out_queue_4", 4, 0x14 | GLOBAL_STATS_OP_BANK_1, BANK1, },
763 { "out_queue_5", 4, 0x15 | GLOBAL_STATS_OP_BANK_1, BANK1, },
764 { "out_queue_6", 4, 0x16 | GLOBAL_STATS_OP_BANK_1, BANK1, },
765 { "out_queue_7", 4, 0x17 | GLOBAL_STATS_OP_BANK_1, BANK1, },
766 { "out_cut_through", 4, 0x18 | GLOBAL_STATS_OP_BANK_1, BANK1, },
767 { "out_octets_a", 4, 0x1a | GLOBAL_STATS_OP_BANK_1, BANK1, },
768 { "out_octets_b", 4, 0x1b | GLOBAL_STATS_OP_BANK_1, BANK1, },
769 { "out_management", 4, 0x1f | GLOBAL_STATS_OP_BANK_1, BANK1, },
Andrew Lunne413e7e2015-04-02 04:06:38 +0200770};
771
Vivien Didelotfad09c72016-06-21 12:28:20 -0400772static bool mv88e6xxx_has_stat(struct mv88e6xxx_chip *chip,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100773 struct mv88e6xxx_hw_stat *stat)
Andrew Lunne413e7e2015-04-02 04:06:38 +0200774{
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100775 switch (stat->type) {
776 case BANK0:
Andrew Lunne413e7e2015-04-02 04:06:38 +0200777 return true;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100778 case BANK1:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400779 return mv88e6xxx_6320_family(chip);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100780 case PORT:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400781 return mv88e6xxx_6095_family(chip) ||
782 mv88e6xxx_6185_family(chip) ||
783 mv88e6xxx_6097_family(chip) ||
784 mv88e6xxx_6165_family(chip) ||
785 mv88e6xxx_6351_family(chip) ||
786 mv88e6xxx_6352_family(chip);
Andrew Lunne413e7e2015-04-02 04:06:38 +0200787 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100788 return false;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000789}
790
Vivien Didelotfad09c72016-06-21 12:28:20 -0400791static uint64_t _mv88e6xxx_get_ethtool_stat(struct mv88e6xxx_chip *chip,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100792 struct mv88e6xxx_hw_stat *s,
Andrew Lunn80c46272015-06-20 18:42:30 +0200793 int port)
794{
Andrew Lunn80c46272015-06-20 18:42:30 +0200795 u32 low;
796 u32 high = 0;
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200797 int err;
798 u16 reg;
Andrew Lunn80c46272015-06-20 18:42:30 +0200799 u64 value;
800
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100801 switch (s->type) {
802 case PORT:
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200803 err = mv88e6xxx_port_read(chip, port, s->reg, &reg);
804 if (err)
Andrew Lunn80c46272015-06-20 18:42:30 +0200805 return UINT64_MAX;
806
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200807 low = reg;
Andrew Lunn80c46272015-06-20 18:42:30 +0200808 if (s->sizeof_stat == 4) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200809 err = mv88e6xxx_port_read(chip, port, s->reg + 1, &reg);
810 if (err)
Andrew Lunn80c46272015-06-20 18:42:30 +0200811 return UINT64_MAX;
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200812 high = reg;
Andrew Lunn80c46272015-06-20 18:42:30 +0200813 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100814 break;
815 case BANK0:
816 case BANK1:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400817 _mv88e6xxx_stats_read(chip, s->reg, &low);
Andrew Lunn80c46272015-06-20 18:42:30 +0200818 if (s->sizeof_stat == 8)
Vivien Didelotfad09c72016-06-21 12:28:20 -0400819 _mv88e6xxx_stats_read(chip, s->reg + 1, &high);
Andrew Lunn80c46272015-06-20 18:42:30 +0200820 }
821 value = (((u64)high) << 16) | low;
822 return value;
823}
824
Vivien Didelotf81ec902016-05-09 13:22:58 -0400825static void mv88e6xxx_get_strings(struct dsa_switch *ds, int port,
826 uint8_t *data)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100827{
Vivien Didelot04bed142016-08-31 18:06:13 -0400828 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100829 struct mv88e6xxx_hw_stat *stat;
830 int i, j;
831
832 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
833 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400834 if (mv88e6xxx_has_stat(chip, stat)) {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100835 memcpy(data + j * ETH_GSTRING_LEN, stat->string,
836 ETH_GSTRING_LEN);
837 j++;
838 }
839 }
840}
841
Vivien Didelotf81ec902016-05-09 13:22:58 -0400842static int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100843{
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;
846 int i, j;
847
848 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
849 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400850 if (mv88e6xxx_has_stat(chip, stat))
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100851 j++;
852 }
853 return j;
854}
855
Vivien Didelotf81ec902016-05-09 13:22:58 -0400856static void mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, int port,
857 uint64_t *data)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000858{
Vivien Didelot04bed142016-08-31 18:06:13 -0400859 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100860 struct mv88e6xxx_hw_stat *stat;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000861 int ret;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100862 int i, j;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000863
Vivien Didelotfad09c72016-06-21 12:28:20 -0400864 mutex_lock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000865
Vivien Didelotfad09c72016-06-21 12:28:20 -0400866 ret = _mv88e6xxx_stats_snapshot(chip, port);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000867 if (ret < 0) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400868 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000869 return;
870 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100871 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
872 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400873 if (mv88e6xxx_has_stat(chip, stat)) {
874 data[j] = _mv88e6xxx_get_ethtool_stat(chip, stat, port);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100875 j++;
876 }
877 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000878
Vivien Didelotfad09c72016-06-21 12:28:20 -0400879 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000880}
Ben Hutchings98e67302011-11-25 14:36:19 +0000881
Vivien Didelotf81ec902016-05-09 13:22:58 -0400882static int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700883{
884 return 32 * sizeof(u16);
885}
886
Vivien Didelotf81ec902016-05-09 13:22:58 -0400887static void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
888 struct ethtool_regs *regs, void *_p)
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700889{
Vivien Didelot04bed142016-08-31 18:06:13 -0400890 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200891 int err;
892 u16 reg;
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700893 u16 *p = _p;
894 int i;
895
896 regs->version = 0;
897
898 memset(p, 0xff, 32 * sizeof(u16));
899
Vivien Didelotfad09c72016-06-21 12:28:20 -0400900 mutex_lock(&chip->reg_lock);
Vivien Didelot23062512016-05-09 13:22:45 -0400901
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700902 for (i = 0; i < 32; i++) {
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700903
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200904 err = mv88e6xxx_port_read(chip, port, i, &reg);
905 if (!err)
906 p[i] = reg;
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700907 }
Vivien Didelot23062512016-05-09 13:22:45 -0400908
Vivien Didelotfad09c72016-06-21 12:28:20 -0400909 mutex_unlock(&chip->reg_lock);
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700910}
911
Vivien Didelotfad09c72016-06-21 12:28:20 -0400912static int _mv88e6xxx_atu_wait(struct mv88e6xxx_chip *chip)
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700913{
Vivien Didelota935c052016-09-29 12:21:53 -0400914 return mv88e6xxx_g1_wait(chip, GLOBAL_ATU_OP, GLOBAL_ATU_OP_BUSY);
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700915}
916
Vivien Didelotf81ec902016-05-09 13:22:58 -0400917static int mv88e6xxx_get_eee(struct dsa_switch *ds, int port,
918 struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -0800919{
Vivien Didelot04bed142016-08-31 18:06:13 -0400920 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot9c938292016-08-15 17:19:02 -0400921 u16 reg;
922 int err;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800923
Vivien Didelotfad09c72016-06-21 12:28:20 -0400924 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
Vivien Didelotaadbdb82016-05-09 13:22:44 -0400925 return -EOPNOTSUPP;
926
Vivien Didelotfad09c72016-06-21 12:28:20 -0400927 mutex_lock(&chip->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200928
Vivien Didelot9c938292016-08-15 17:19:02 -0400929 err = mv88e6xxx_phy_read(chip, port, 16, &reg);
930 if (err)
Andrew Lunn2f40c692015-04-02 04:06:37 +0200931 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800932
933 e->eee_enabled = !!(reg & 0x0200);
934 e->tx_lpi_enabled = !!(reg & 0x0100);
935
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200936 err = mv88e6xxx_port_read(chip, port, PORT_STATUS, &reg);
Vivien Didelot9c938292016-08-15 17:19:02 -0400937 if (err)
Andrew Lunn2f40c692015-04-02 04:06:37 +0200938 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800939
Andrew Lunncca8b132015-04-02 04:06:39 +0200940 e->eee_active = !!(reg & PORT_STATUS_EEE);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200941out:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400942 mutex_unlock(&chip->reg_lock);
Vivien Didelot9c938292016-08-15 17:19:02 -0400943
944 return err;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800945}
946
Vivien Didelotf81ec902016-05-09 13:22:58 -0400947static int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
948 struct phy_device *phydev, struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -0800949{
Vivien Didelot04bed142016-08-31 18:06:13 -0400950 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot9c938292016-08-15 17:19:02 -0400951 u16 reg;
952 int err;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800953
Vivien Didelotfad09c72016-06-21 12:28:20 -0400954 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
Vivien Didelotaadbdb82016-05-09 13:22:44 -0400955 return -EOPNOTSUPP;
956
Vivien Didelotfad09c72016-06-21 12:28:20 -0400957 mutex_lock(&chip->reg_lock);
Guenter Roeck11b3b452015-03-06 22:23:51 -0800958
Vivien Didelot9c938292016-08-15 17:19:02 -0400959 err = mv88e6xxx_phy_read(chip, port, 16, &reg);
960 if (err)
Andrew Lunn2f40c692015-04-02 04:06:37 +0200961 goto out;
962
Vivien Didelot9c938292016-08-15 17:19:02 -0400963 reg &= ~0x0300;
Andrew Lunn2f40c692015-04-02 04:06:37 +0200964 if (e->eee_enabled)
965 reg |= 0x0200;
966 if (e->tx_lpi_enabled)
967 reg |= 0x0100;
968
Vivien Didelot9c938292016-08-15 17:19:02 -0400969 err = mv88e6xxx_phy_write(chip, port, 16, reg);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200970out:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400971 mutex_unlock(&chip->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200972
Vivien Didelot9c938292016-08-15 17:19:02 -0400973 return err;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800974}
975
Vivien Didelotfad09c72016-06-21 12:28:20 -0400976static int _mv88e6xxx_atu_cmd(struct mv88e6xxx_chip *chip, u16 fid, u16 cmd)
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700977{
Vivien Didelota935c052016-09-29 12:21:53 -0400978 u16 val;
979 int err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700980
Vivien Didelotfad09c72016-06-21 12:28:20 -0400981 if (mv88e6xxx_has_fid_reg(chip)) {
Vivien Didelota935c052016-09-29 12:21:53 -0400982 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_FID, fid);
983 if (err)
984 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -0400985 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -0400986 /* ATU DBNum[7:4] are located in ATU Control 15:12 */
Vivien Didelota935c052016-09-29 12:21:53 -0400987 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_CONTROL, &val);
988 if (err)
989 return err;
Vivien Didelot11ea8092016-03-31 16:53:44 -0400990
Vivien Didelota935c052016-09-29 12:21:53 -0400991 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_CONTROL,
992 (val & 0xfff) | ((fid << 8) & 0xf000));
993 if (err)
994 return err;
Vivien Didelot11ea8092016-03-31 16:53:44 -0400995
996 /* ATU DBNum[3:0] are located in ATU Operation 3:0 */
997 cmd |= fid & 0xf;
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400998 }
999
Vivien Didelota935c052016-09-29 12:21:53 -04001000 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_OP, cmd);
1001 if (err)
1002 return err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001003
Vivien Didelotfad09c72016-06-21 12:28:20 -04001004 return _mv88e6xxx_atu_wait(chip);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001005}
1006
Vivien Didelotfad09c72016-06-21 12:28:20 -04001007static int _mv88e6xxx_atu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot37705b72015-09-04 14:34:11 -04001008 struct mv88e6xxx_atu_entry *entry)
1009{
1010 u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK;
1011
1012 if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) {
1013 unsigned int mask, shift;
1014
1015 if (entry->trunk) {
1016 data |= GLOBAL_ATU_DATA_TRUNK;
1017 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
1018 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
1019 } else {
1020 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
1021 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
1022 }
1023
1024 data |= (entry->portv_trunkid << shift) & mask;
1025 }
1026
Vivien Didelota935c052016-09-29 12:21:53 -04001027 return mv88e6xxx_g1_write(chip, GLOBAL_ATU_DATA, data);
Vivien Didelot37705b72015-09-04 14:34:11 -04001028}
1029
Vivien Didelotfad09c72016-06-21 12:28:20 -04001030static int _mv88e6xxx_atu_flush_move(struct mv88e6xxx_chip *chip,
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001031 struct mv88e6xxx_atu_entry *entry,
1032 bool static_too)
1033{
1034 int op;
1035 int err;
1036
Vivien Didelotfad09c72016-06-21 12:28:20 -04001037 err = _mv88e6xxx_atu_wait(chip);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001038 if (err)
1039 return err;
1040
Vivien Didelotfad09c72016-06-21 12:28:20 -04001041 err = _mv88e6xxx_atu_data_write(chip, entry);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001042 if (err)
1043 return err;
1044
1045 if (entry->fid) {
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001046 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB :
1047 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB;
1048 } else {
1049 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL :
1050 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC;
1051 }
1052
Vivien Didelotfad09c72016-06-21 12:28:20 -04001053 return _mv88e6xxx_atu_cmd(chip, entry->fid, op);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001054}
1055
Vivien Didelotfad09c72016-06-21 12:28:20 -04001056static int _mv88e6xxx_atu_flush(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001057 u16 fid, bool static_too)
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001058{
1059 struct mv88e6xxx_atu_entry entry = {
1060 .fid = fid,
1061 .state = 0, /* EntryState bits must be 0 */
1062 };
1063
Vivien Didelotfad09c72016-06-21 12:28:20 -04001064 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001065}
1066
Vivien Didelotfad09c72016-06-21 12:28:20 -04001067static int _mv88e6xxx_atu_move(struct mv88e6xxx_chip *chip, u16 fid,
Andrew Lunn158bc062016-04-28 21:24:06 -04001068 int from_port, int to_port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001069{
1070 struct mv88e6xxx_atu_entry entry = {
1071 .trunk = false,
1072 .fid = fid,
1073 };
1074
1075 /* EntryState bits must be 0xF */
1076 entry.state = GLOBAL_ATU_DATA_STATE_MASK;
1077
1078 /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */
1079 entry.portv_trunkid = (to_port & 0x0f) << 4;
1080 entry.portv_trunkid |= from_port & 0x0f;
1081
Vivien Didelotfad09c72016-06-21 12:28:20 -04001082 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001083}
1084
Vivien Didelotfad09c72016-06-21 12:28:20 -04001085static int _mv88e6xxx_atu_remove(struct mv88e6xxx_chip *chip, u16 fid,
Andrew Lunn158bc062016-04-28 21:24:06 -04001086 int port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001087{
1088 /* Destination port 0xF means remove the entries */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001089 return _mv88e6xxx_atu_move(chip, fid, port, 0x0f, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001090}
1091
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001092static const char * const mv88e6xxx_port_state_names[] = {
1093 [PORT_CONTROL_STATE_DISABLED] = "Disabled",
1094 [PORT_CONTROL_STATE_BLOCKING] = "Blocking/Listening",
1095 [PORT_CONTROL_STATE_LEARNING] = "Learning",
1096 [PORT_CONTROL_STATE_FORWARDING] = "Forwarding",
1097};
1098
Vivien Didelotfad09c72016-06-21 12:28:20 -04001099static int _mv88e6xxx_port_state(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001100 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001101{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001102 struct dsa_switch *ds = chip->ds;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001103 u16 reg;
1104 int err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001105 u8 oldstate;
1106
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001107 err = mv88e6xxx_port_read(chip, port, PORT_CONTROL, &reg);
1108 if (err)
1109 return err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001110
Andrew Lunncca8b132015-04-02 04:06:39 +02001111 oldstate = reg & PORT_CONTROL_STATE_MASK;
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001112
Vivien Didelot749efcb2016-09-22 16:49:24 -04001113 reg &= ~PORT_CONTROL_STATE_MASK;
1114 reg |= state;
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001115
Vivien Didelot749efcb2016-09-22 16:49:24 -04001116 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL, reg);
1117 if (err)
1118 return err;
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001119
Vivien Didelot749efcb2016-09-22 16:49:24 -04001120 netdev_dbg(ds->ports[port].netdev, "PortState %s (was %s)\n",
1121 mv88e6xxx_port_state_names[state],
1122 mv88e6xxx_port_state_names[oldstate]);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001123
Vivien Didelot749efcb2016-09-22 16:49:24 -04001124 return 0;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001125}
1126
Vivien Didelotfad09c72016-06-21 12:28:20 -04001127static int _mv88e6xxx_port_based_vlan_map(struct mv88e6xxx_chip *chip, int port)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001128{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001129 struct net_device *bridge = chip->ports[port].bridge_dev;
1130 const u16 mask = (1 << chip->info->num_ports) - 1;
1131 struct dsa_switch *ds = chip->ds;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001132 u16 output_ports = 0;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001133 u16 reg;
1134 int err;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001135 int i;
1136
1137 /* allow CPU port or DSA link(s) to send frames to every port */
1138 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
1139 output_ports = mask;
1140 } else {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001141 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001142 /* allow sending frames to every group member */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001143 if (bridge && chip->ports[i].bridge_dev == bridge)
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001144 output_ports |= BIT(i);
1145
1146 /* allow sending frames to CPU port and DSA link(s) */
1147 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
1148 output_ports |= BIT(i);
1149 }
1150 }
1151
1152 /* prevent frames from going back out of the port they came in on */
1153 output_ports &= ~BIT(port);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001154
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001155 err = mv88e6xxx_port_read(chip, port, PORT_BASE_VLAN, &reg);
1156 if (err)
1157 return err;
Vivien Didelotede80982015-10-11 18:08:35 -04001158
1159 reg &= ~mask;
1160 reg |= output_ports & mask;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001161
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001162 return mv88e6xxx_port_write(chip, port, PORT_BASE_VLAN, reg);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001163}
1164
Vivien Didelotf81ec902016-05-09 13:22:58 -04001165static void mv88e6xxx_port_stp_state_set(struct dsa_switch *ds, int port,
1166 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001167{
Vivien Didelot04bed142016-08-31 18:06:13 -04001168 struct mv88e6xxx_chip *chip = ds->priv;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001169 int stp_state;
Vivien Didelot553eb542016-05-13 20:38:23 -04001170 int err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001171
1172 switch (state) {
1173 case BR_STATE_DISABLED:
Andrew Lunncca8b132015-04-02 04:06:39 +02001174 stp_state = PORT_CONTROL_STATE_DISABLED;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001175 break;
1176 case BR_STATE_BLOCKING:
1177 case BR_STATE_LISTENING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001178 stp_state = PORT_CONTROL_STATE_BLOCKING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001179 break;
1180 case BR_STATE_LEARNING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001181 stp_state = PORT_CONTROL_STATE_LEARNING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001182 break;
1183 case BR_STATE_FORWARDING:
1184 default:
Andrew Lunncca8b132015-04-02 04:06:39 +02001185 stp_state = PORT_CONTROL_STATE_FORWARDING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001186 break;
1187 }
1188
Vivien Didelotfad09c72016-06-21 12:28:20 -04001189 mutex_lock(&chip->reg_lock);
1190 err = _mv88e6xxx_port_state(chip, port, stp_state);
1191 mutex_unlock(&chip->reg_lock);
Vivien Didelot553eb542016-05-13 20:38:23 -04001192
1193 if (err)
Andrew Lunnc8b09802016-06-04 21:16:57 +02001194 netdev_err(ds->ports[port].netdev,
1195 "failed to update state to %s\n",
Vivien Didelot553eb542016-05-13 20:38:23 -04001196 mv88e6xxx_port_state_names[stp_state]);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001197}
1198
Vivien Didelot749efcb2016-09-22 16:49:24 -04001199static void mv88e6xxx_port_fast_age(struct dsa_switch *ds, int port)
1200{
1201 struct mv88e6xxx_chip *chip = ds->priv;
1202 int err;
1203
1204 mutex_lock(&chip->reg_lock);
1205 err = _mv88e6xxx_atu_remove(chip, 0, port, false);
1206 mutex_unlock(&chip->reg_lock);
1207
1208 if (err)
1209 netdev_err(ds->ports[port].netdev, "failed to flush ATU\n");
1210}
1211
Vivien Didelotfad09c72016-06-21 12:28:20 -04001212static int _mv88e6xxx_port_pvid(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001213 u16 *new, u16 *old)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001214{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001215 struct dsa_switch *ds = chip->ds;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001216 u16 pvid, reg;
1217 int err;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001218
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001219 err = mv88e6xxx_port_read(chip, port, PORT_DEFAULT_VLAN, &reg);
1220 if (err)
1221 return err;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001222
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001223 pvid = reg & PORT_DEFAULT_VLAN_MASK;
Vivien Didelot5da96032016-03-07 18:24:39 -05001224
1225 if (new) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001226 reg &= ~PORT_DEFAULT_VLAN_MASK;
1227 reg |= *new & PORT_DEFAULT_VLAN_MASK;
Vivien Didelot5da96032016-03-07 18:24:39 -05001228
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001229 err = mv88e6xxx_port_write(chip, port, PORT_DEFAULT_VLAN, reg);
1230 if (err)
1231 return err;
Vivien Didelot5da96032016-03-07 18:24:39 -05001232
Andrew Lunnc8b09802016-06-04 21:16:57 +02001233 netdev_dbg(ds->ports[port].netdev,
1234 "DefaultVID %d (was %d)\n", *new, pvid);
Vivien Didelot5da96032016-03-07 18:24:39 -05001235 }
1236
1237 if (old)
1238 *old = pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001239
1240 return 0;
1241}
1242
Vivien Didelotfad09c72016-06-21 12:28:20 -04001243static int _mv88e6xxx_port_pvid_get(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001244 int port, u16 *pvid)
Vivien Didelot5da96032016-03-07 18:24:39 -05001245{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001246 return _mv88e6xxx_port_pvid(chip, port, NULL, pvid);
Vivien Didelot5da96032016-03-07 18:24:39 -05001247}
1248
Vivien Didelotfad09c72016-06-21 12:28:20 -04001249static int _mv88e6xxx_port_pvid_set(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001250 int port, u16 pvid)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001251{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001252 return _mv88e6xxx_port_pvid(chip, port, &pvid, NULL);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001253}
1254
Vivien Didelotfad09c72016-06-21 12:28:20 -04001255static int _mv88e6xxx_vtu_wait(struct mv88e6xxx_chip *chip)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001256{
Vivien Didelota935c052016-09-29 12:21:53 -04001257 return mv88e6xxx_g1_wait(chip, GLOBAL_VTU_OP, GLOBAL_VTU_OP_BUSY);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001258}
1259
Vivien Didelotfad09c72016-06-21 12:28:20 -04001260static int _mv88e6xxx_vtu_cmd(struct mv88e6xxx_chip *chip, u16 op)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001261{
Vivien Didelota935c052016-09-29 12:21:53 -04001262 int err;
Vivien Didelot6b17e862015-08-13 12:52:18 -04001263
Vivien Didelota935c052016-09-29 12:21:53 -04001264 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_OP, op);
1265 if (err)
1266 return err;
Vivien Didelot6b17e862015-08-13 12:52:18 -04001267
Vivien Didelotfad09c72016-06-21 12:28:20 -04001268 return _mv88e6xxx_vtu_wait(chip);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001269}
1270
Vivien Didelotfad09c72016-06-21 12:28:20 -04001271static int _mv88e6xxx_vtu_stu_flush(struct mv88e6xxx_chip *chip)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001272{
1273 int ret;
1274
Vivien Didelotfad09c72016-06-21 12:28:20 -04001275 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001276 if (ret < 0)
1277 return ret;
1278
Vivien Didelotfad09c72016-06-21 12:28:20 -04001279 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_FLUSH_ALL);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001280}
1281
Vivien Didelotfad09c72016-06-21 12:28:20 -04001282static int _mv88e6xxx_vtu_stu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001283 struct mv88e6xxx_vtu_stu_entry *entry,
1284 unsigned int nibble_offset)
1285{
Vivien Didelotb8fee952015-08-13 12:52:19 -04001286 u16 regs[3];
Vivien Didelota935c052016-09-29 12:21:53 -04001287 int i, err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001288
1289 for (i = 0; i < 3; ++i) {
Vivien Didelota935c052016-09-29 12:21:53 -04001290 u16 *reg = &regs[i];
Vivien Didelotb8fee952015-08-13 12:52:19 -04001291
Vivien Didelota935c052016-09-29 12:21:53 -04001292 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_DATA_0_3 + i, reg);
1293 if (err)
1294 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001295 }
1296
Vivien Didelotfad09c72016-06-21 12:28:20 -04001297 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelotb8fee952015-08-13 12:52:19 -04001298 unsigned int shift = (i % 4) * 4 + nibble_offset;
1299 u16 reg = regs[i / 4];
1300
1301 entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
1302 }
1303
1304 return 0;
1305}
1306
Vivien Didelotfad09c72016-06-21 12:28:20 -04001307static int mv88e6xxx_vtu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001308 struct mv88e6xxx_vtu_stu_entry *entry)
1309{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001310 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 0);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001311}
1312
Vivien Didelotfad09c72016-06-21 12:28:20 -04001313static int mv88e6xxx_stu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001314 struct mv88e6xxx_vtu_stu_entry *entry)
1315{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001316 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 2);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001317}
1318
Vivien Didelotfad09c72016-06-21 12:28:20 -04001319static int _mv88e6xxx_vtu_stu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001320 struct mv88e6xxx_vtu_stu_entry *entry,
1321 unsigned int nibble_offset)
1322{
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001323 u16 regs[3] = { 0 };
Vivien Didelota935c052016-09-29 12:21:53 -04001324 int i, err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001325
Vivien Didelotfad09c72016-06-21 12:28:20 -04001326 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001327 unsigned int shift = (i % 4) * 4 + nibble_offset;
1328 u8 data = entry->data[i];
1329
1330 regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
1331 }
1332
1333 for (i = 0; i < 3; ++i) {
Vivien Didelota935c052016-09-29 12:21:53 -04001334 u16 reg = regs[i];
1335
1336 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_DATA_0_3 + i, reg);
1337 if (err)
1338 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001339 }
1340
1341 return 0;
1342}
1343
Vivien Didelotfad09c72016-06-21 12:28:20 -04001344static int mv88e6xxx_vtu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001345 struct mv88e6xxx_vtu_stu_entry *entry)
1346{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001347 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 0);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001348}
1349
Vivien Didelotfad09c72016-06-21 12:28:20 -04001350static int mv88e6xxx_stu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001351 struct mv88e6xxx_vtu_stu_entry *entry)
1352{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001353 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 2);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001354}
1355
Vivien Didelotfad09c72016-06-21 12:28:20 -04001356static int _mv88e6xxx_vtu_vid_write(struct mv88e6xxx_chip *chip, u16 vid)
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001357{
Vivien Didelota935c052016-09-29 12:21:53 -04001358 return mv88e6xxx_g1_write(chip, GLOBAL_VTU_VID,
1359 vid & GLOBAL_VTU_VID_MASK);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001360}
1361
Vivien Didelotfad09c72016-06-21 12:28:20 -04001362static int _mv88e6xxx_vtu_getnext(struct mv88e6xxx_chip *chip,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001363 struct mv88e6xxx_vtu_stu_entry *entry)
1364{
1365 struct mv88e6xxx_vtu_stu_entry next = { 0 };
Vivien Didelota935c052016-09-29 12:21:53 -04001366 u16 val;
1367 int err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001368
Vivien Didelota935c052016-09-29 12:21:53 -04001369 err = _mv88e6xxx_vtu_wait(chip);
1370 if (err)
1371 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001372
Vivien Didelota935c052016-09-29 12:21:53 -04001373 err = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_VTU_GET_NEXT);
1374 if (err)
1375 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001376
Vivien Didelota935c052016-09-29 12:21:53 -04001377 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_VID, &val);
1378 if (err)
1379 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001380
Vivien Didelota935c052016-09-29 12:21:53 -04001381 next.vid = val & GLOBAL_VTU_VID_MASK;
1382 next.valid = !!(val & GLOBAL_VTU_VID_VALID);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001383
1384 if (next.valid) {
Vivien Didelota935c052016-09-29 12:21:53 -04001385 err = mv88e6xxx_vtu_data_read(chip, &next);
1386 if (err)
1387 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001388
Vivien Didelotfad09c72016-06-21 12:28:20 -04001389 if (mv88e6xxx_has_fid_reg(chip)) {
Vivien Didelota935c052016-09-29 12:21:53 -04001390 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_FID, &val);
1391 if (err)
1392 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001393
Vivien Didelota935c052016-09-29 12:21:53 -04001394 next.fid = val & GLOBAL_VTU_FID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001395 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001396 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1397 * VTU DBNum[3:0] are located in VTU Operation 3:0
1398 */
Vivien Didelota935c052016-09-29 12:21:53 -04001399 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_OP, &val);
1400 if (err)
1401 return err;
Vivien Didelot11ea8092016-03-31 16:53:44 -04001402
Vivien Didelota935c052016-09-29 12:21:53 -04001403 next.fid = (val & 0xf00) >> 4;
1404 next.fid |= val & 0xf;
Vivien Didelot2e7bd5e2016-03-31 16:53:41 -04001405 }
Vivien Didelotb8fee952015-08-13 12:52:19 -04001406
Vivien Didelotfad09c72016-06-21 12:28:20 -04001407 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
Vivien Didelota935c052016-09-29 12:21:53 -04001408 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_SID, &val);
1409 if (err)
1410 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001411
Vivien Didelota935c052016-09-29 12:21:53 -04001412 next.sid = val & GLOBAL_VTU_SID_MASK;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001413 }
1414 }
1415
1416 *entry = next;
1417 return 0;
1418}
1419
Vivien Didelotf81ec902016-05-09 13:22:58 -04001420static int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
1421 struct switchdev_obj_port_vlan *vlan,
1422 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001423{
Vivien Didelot04bed142016-08-31 18:06:13 -04001424 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001425 struct mv88e6xxx_vtu_stu_entry next;
1426 u16 pvid;
1427 int err;
1428
Vivien Didelotfad09c72016-06-21 12:28:20 -04001429 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001430 return -EOPNOTSUPP;
1431
Vivien Didelotfad09c72016-06-21 12:28:20 -04001432 mutex_lock(&chip->reg_lock);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001433
Vivien Didelotfad09c72016-06-21 12:28:20 -04001434 err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001435 if (err)
1436 goto unlock;
1437
Vivien Didelotfad09c72016-06-21 12:28:20 -04001438 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001439 if (err)
1440 goto unlock;
1441
1442 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001443 err = _mv88e6xxx_vtu_getnext(chip, &next);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001444 if (err)
1445 break;
1446
1447 if (!next.valid)
1448 break;
1449
1450 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1451 continue;
1452
1453 /* reinit and dump this VLAN obj */
Vivien Didelot57d32312016-06-20 13:13:58 -04001454 vlan->vid_begin = next.vid;
1455 vlan->vid_end = next.vid;
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001456 vlan->flags = 0;
1457
1458 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1459 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1460
1461 if (next.vid == pvid)
1462 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1463
1464 err = cb(&vlan->obj);
1465 if (err)
1466 break;
1467 } while (next.vid < GLOBAL_VTU_VID_MASK);
1468
1469unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001470 mutex_unlock(&chip->reg_lock);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001471
1472 return err;
1473}
1474
Vivien Didelotfad09c72016-06-21 12:28:20 -04001475static int _mv88e6xxx_vtu_loadpurge(struct mv88e6xxx_chip *chip,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001476 struct mv88e6xxx_vtu_stu_entry *entry)
1477{
Vivien Didelot11ea8092016-03-31 16:53:44 -04001478 u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001479 u16 reg = 0;
Vivien Didelota935c052016-09-29 12:21:53 -04001480 int err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001481
Vivien Didelota935c052016-09-29 12:21:53 -04001482 err = _mv88e6xxx_vtu_wait(chip);
1483 if (err)
1484 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001485
1486 if (!entry->valid)
1487 goto loadpurge;
1488
1489 /* Write port member tags */
Vivien Didelota935c052016-09-29 12:21:53 -04001490 err = mv88e6xxx_vtu_data_write(chip, entry);
1491 if (err)
1492 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001493
Vivien Didelotfad09c72016-06-21 12:28:20 -04001494 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001495 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Vivien Didelota935c052016-09-29 12:21:53 -04001496 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_SID, reg);
1497 if (err)
1498 return err;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001499 }
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001500
Vivien Didelotfad09c72016-06-21 12:28:20 -04001501 if (mv88e6xxx_has_fid_reg(chip)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001502 reg = entry->fid & GLOBAL_VTU_FID_MASK;
Vivien Didelota935c052016-09-29 12:21:53 -04001503 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_FID, reg);
1504 if (err)
1505 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001506 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001507 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1508 * VTU DBNum[3:0] are located in VTU Operation 3:0
1509 */
1510 op |= (entry->fid & 0xf0) << 8;
1511 op |= entry->fid & 0xf;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001512 }
1513
1514 reg = GLOBAL_VTU_VID_VALID;
1515loadpurge:
1516 reg |= entry->vid & GLOBAL_VTU_VID_MASK;
Vivien Didelota935c052016-09-29 12:21:53 -04001517 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_VID, reg);
1518 if (err)
1519 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001520
Vivien Didelotfad09c72016-06-21 12:28:20 -04001521 return _mv88e6xxx_vtu_cmd(chip, op);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001522}
1523
Vivien Didelotfad09c72016-06-21 12:28:20 -04001524static int _mv88e6xxx_stu_getnext(struct mv88e6xxx_chip *chip, u8 sid,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001525 struct mv88e6xxx_vtu_stu_entry *entry)
1526{
1527 struct mv88e6xxx_vtu_stu_entry next = { 0 };
Vivien Didelota935c052016-09-29 12:21:53 -04001528 u16 val;
1529 int err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001530
Vivien Didelota935c052016-09-29 12:21:53 -04001531 err = _mv88e6xxx_vtu_wait(chip);
1532 if (err)
1533 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001534
Vivien Didelota935c052016-09-29 12:21:53 -04001535 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_SID,
1536 sid & GLOBAL_VTU_SID_MASK);
1537 if (err)
1538 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001539
Vivien Didelota935c052016-09-29 12:21:53 -04001540 err = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_GET_NEXT);
1541 if (err)
1542 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001543
Vivien Didelota935c052016-09-29 12:21:53 -04001544 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_SID, &val);
1545 if (err)
1546 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001547
Vivien Didelota935c052016-09-29 12:21:53 -04001548 next.sid = val & GLOBAL_VTU_SID_MASK;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001549
Vivien Didelota935c052016-09-29 12:21:53 -04001550 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_VID, &val);
1551 if (err)
1552 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001553
Vivien Didelota935c052016-09-29 12:21:53 -04001554 next.valid = !!(val & GLOBAL_VTU_VID_VALID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001555
1556 if (next.valid) {
Vivien Didelota935c052016-09-29 12:21:53 -04001557 err = mv88e6xxx_stu_data_read(chip, &next);
1558 if (err)
1559 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001560 }
1561
1562 *entry = next;
1563 return 0;
1564}
1565
Vivien Didelotfad09c72016-06-21 12:28:20 -04001566static int _mv88e6xxx_stu_loadpurge(struct mv88e6xxx_chip *chip,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001567 struct mv88e6xxx_vtu_stu_entry *entry)
1568{
1569 u16 reg = 0;
Vivien Didelota935c052016-09-29 12:21:53 -04001570 int err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001571
Vivien Didelota935c052016-09-29 12:21:53 -04001572 err = _mv88e6xxx_vtu_wait(chip);
1573 if (err)
1574 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001575
1576 if (!entry->valid)
1577 goto loadpurge;
1578
1579 /* Write port states */
Vivien Didelota935c052016-09-29 12:21:53 -04001580 err = mv88e6xxx_stu_data_write(chip, entry);
1581 if (err)
1582 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001583
1584 reg = GLOBAL_VTU_VID_VALID;
1585loadpurge:
Vivien Didelota935c052016-09-29 12:21:53 -04001586 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_VID, reg);
1587 if (err)
1588 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001589
1590 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Vivien Didelota935c052016-09-29 12:21:53 -04001591 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_SID, reg);
1592 if (err)
1593 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001594
Vivien Didelotfad09c72016-06-21 12:28:20 -04001595 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_LOAD_PURGE);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001596}
1597
Vivien Didelotfad09c72016-06-21 12:28:20 -04001598static int _mv88e6xxx_port_fid(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001599 u16 *new, u16 *old)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001600{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001601 struct dsa_switch *ds = chip->ds;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001602 u16 upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001603 u16 fid;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001604 u16 reg;
1605 int err;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001606
Vivien Didelotfad09c72016-06-21 12:28:20 -04001607 if (mv88e6xxx_num_databases(chip) == 4096)
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001608 upper_mask = 0xff;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001609 else if (mv88e6xxx_num_databases(chip) == 256)
Vivien Didelot11ea8092016-03-31 16:53:44 -04001610 upper_mask = 0xf;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001611 else
1612 return -EOPNOTSUPP;
1613
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001614 /* Port's default FID bits 3:0 are located in reg 0x06, offset 12 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001615 err = mv88e6xxx_port_read(chip, port, PORT_BASE_VLAN, &reg);
1616 if (err)
1617 return err;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001618
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001619 fid = (reg & PORT_BASE_VLAN_FID_3_0_MASK) >> 12;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001620
1621 if (new) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001622 reg &= ~PORT_BASE_VLAN_FID_3_0_MASK;
1623 reg |= (*new << 12) & PORT_BASE_VLAN_FID_3_0_MASK;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001624
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001625 err = mv88e6xxx_port_write(chip, port, PORT_BASE_VLAN, reg);
1626 if (err)
1627 return err;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001628 }
1629
1630 /* Port's default FID bits 11:4 are located in reg 0x05, offset 0 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001631 err = mv88e6xxx_port_read(chip, port, PORT_CONTROL_1, &reg);
1632 if (err)
1633 return err;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001634
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001635 fid |= (reg & upper_mask) << 4;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001636
1637 if (new) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001638 reg &= ~upper_mask;
1639 reg |= (*new >> 4) & upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001640
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001641 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_1, reg);
1642 if (err)
1643 return err;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001644
Andrew Lunnc8b09802016-06-04 21:16:57 +02001645 netdev_dbg(ds->ports[port].netdev,
1646 "FID %d (was %d)\n", *new, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001647 }
1648
1649 if (old)
1650 *old = fid;
1651
1652 return 0;
1653}
1654
Vivien Didelotfad09c72016-06-21 12:28:20 -04001655static int _mv88e6xxx_port_fid_get(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001656 int port, u16 *fid)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001657{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001658 return _mv88e6xxx_port_fid(chip, port, NULL, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001659}
1660
Vivien Didelotfad09c72016-06-21 12:28:20 -04001661static int _mv88e6xxx_port_fid_set(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001662 int port, u16 fid)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001663{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001664 return _mv88e6xxx_port_fid(chip, port, &fid, NULL);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001665}
1666
Vivien Didelotfad09c72016-06-21 12:28:20 -04001667static int _mv88e6xxx_fid_new(struct mv88e6xxx_chip *chip, u16 *fid)
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001668{
1669 DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
1670 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001671 int i, err;
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001672
1673 bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);
1674
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001675 /* Set every FID bit used by the (un)bridged ports */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001676 for (i = 0; i < chip->info->num_ports; ++i) {
1677 err = _mv88e6xxx_port_fid_get(chip, i, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001678 if (err)
1679 return err;
1680
1681 set_bit(*fid, fid_bitmap);
1682 }
1683
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001684 /* Set every FID bit used by the VLAN entries */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001685 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001686 if (err)
1687 return err;
1688
1689 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001690 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001691 if (err)
1692 return err;
1693
1694 if (!vlan.valid)
1695 break;
1696
1697 set_bit(vlan.fid, fid_bitmap);
1698 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
1699
1700 /* The reset value 0x000 is used to indicate that multiple address
1701 * databases are not needed. Return the next positive available.
1702 */
1703 *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1);
Vivien Didelotfad09c72016-06-21 12:28:20 -04001704 if (unlikely(*fid >= mv88e6xxx_num_databases(chip)))
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001705 return -ENOSPC;
1706
1707 /* Clear the database */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001708 return _mv88e6xxx_atu_flush(chip, *fid, true);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001709}
1710
Vivien Didelotfad09c72016-06-21 12:28:20 -04001711static int _mv88e6xxx_vtu_new(struct mv88e6xxx_chip *chip, u16 vid,
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001712 struct mv88e6xxx_vtu_stu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001713{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001714 struct dsa_switch *ds = chip->ds;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001715 struct mv88e6xxx_vtu_stu_entry vlan = {
1716 .valid = true,
1717 .vid = vid,
1718 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001719 int i, err;
1720
Vivien Didelotfad09c72016-06-21 12:28:20 -04001721 err = _mv88e6xxx_fid_new(chip, &vlan.fid);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001722 if (err)
1723 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001724
Vivien Didelot3d131f02015-11-03 10:52:52 -05001725 /* exclude all ports except the CPU and DSA ports */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001726 for (i = 0; i < chip->info->num_ports; ++i)
Vivien Didelot3d131f02015-11-03 10:52:52 -05001727 vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)
1728 ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED
1729 : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001730
Vivien Didelotfad09c72016-06-21 12:28:20 -04001731 if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
1732 mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip)) {
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001733 struct mv88e6xxx_vtu_stu_entry vstp;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001734
1735 /* Adding a VTU entry requires a valid STU entry. As VSTP is not
1736 * implemented, only one STU entry is needed to cover all VTU
1737 * entries. Thus, validate the SID 0.
1738 */
1739 vlan.sid = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001740 err = _mv88e6xxx_stu_getnext(chip, GLOBAL_VTU_SID_MASK, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001741 if (err)
1742 return err;
1743
1744 if (vstp.sid != vlan.sid || !vstp.valid) {
1745 memset(&vstp, 0, sizeof(vstp));
1746 vstp.valid = true;
1747 vstp.sid = vlan.sid;
1748
Vivien Didelotfad09c72016-06-21 12:28:20 -04001749 err = _mv88e6xxx_stu_loadpurge(chip, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001750 if (err)
1751 return err;
1752 }
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001753 }
1754
1755 *entry = vlan;
1756 return 0;
1757}
1758
Vivien Didelotfad09c72016-06-21 12:28:20 -04001759static int _mv88e6xxx_vtu_get(struct mv88e6xxx_chip *chip, u16 vid,
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001760 struct mv88e6xxx_vtu_stu_entry *entry, bool creat)
1761{
1762 int err;
1763
1764 if (!vid)
1765 return -EINVAL;
1766
Vivien Didelotfad09c72016-06-21 12:28:20 -04001767 err = _mv88e6xxx_vtu_vid_write(chip, vid - 1);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001768 if (err)
1769 return err;
1770
Vivien Didelotfad09c72016-06-21 12:28:20 -04001771 err = _mv88e6xxx_vtu_getnext(chip, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001772 if (err)
1773 return err;
1774
1775 if (entry->vid != vid || !entry->valid) {
1776 if (!creat)
1777 return -EOPNOTSUPP;
1778 /* -ENOENT would've been more appropriate, but switchdev expects
1779 * -EOPNOTSUPP to inform bridge about an eventual software VLAN.
1780 */
1781
Vivien Didelotfad09c72016-06-21 12:28:20 -04001782 err = _mv88e6xxx_vtu_new(chip, vid, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001783 }
1784
1785 return err;
1786}
1787
Vivien Didelotda9c3592016-02-12 12:09:40 -05001788static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
1789 u16 vid_begin, u16 vid_end)
1790{
Vivien Didelot04bed142016-08-31 18:06:13 -04001791 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotda9c3592016-02-12 12:09:40 -05001792 struct mv88e6xxx_vtu_stu_entry vlan;
1793 int i, err;
1794
1795 if (!vid_begin)
1796 return -EOPNOTSUPP;
1797
Vivien Didelotfad09c72016-06-21 12:28:20 -04001798 mutex_lock(&chip->reg_lock);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001799
Vivien Didelotfad09c72016-06-21 12:28:20 -04001800 err = _mv88e6xxx_vtu_vid_write(chip, vid_begin - 1);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001801 if (err)
1802 goto unlock;
1803
1804 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001805 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001806 if (err)
1807 goto unlock;
1808
1809 if (!vlan.valid)
1810 break;
1811
1812 if (vlan.vid > vid_end)
1813 break;
1814
Vivien Didelotfad09c72016-06-21 12:28:20 -04001815 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelotda9c3592016-02-12 12:09:40 -05001816 if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i))
1817 continue;
1818
1819 if (vlan.data[i] ==
1820 GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1821 continue;
1822
Vivien Didelotfad09c72016-06-21 12:28:20 -04001823 if (chip->ports[i].bridge_dev ==
1824 chip->ports[port].bridge_dev)
Vivien Didelotda9c3592016-02-12 12:09:40 -05001825 break; /* same bridge, check next VLAN */
1826
Andrew Lunnc8b09802016-06-04 21:16:57 +02001827 netdev_warn(ds->ports[port].netdev,
Vivien Didelotda9c3592016-02-12 12:09:40 -05001828 "hardware VLAN %d already used by %s\n",
1829 vlan.vid,
Vivien Didelotfad09c72016-06-21 12:28:20 -04001830 netdev_name(chip->ports[i].bridge_dev));
Vivien Didelotda9c3592016-02-12 12:09:40 -05001831 err = -EOPNOTSUPP;
1832 goto unlock;
1833 }
1834 } while (vlan.vid < vid_end);
1835
1836unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001837 mutex_unlock(&chip->reg_lock);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001838
1839 return err;
1840}
1841
Vivien Didelot214cdb92016-02-26 13:16:08 -05001842static const char * const mv88e6xxx_port_8021q_mode_names[] = {
1843 [PORT_CONTROL_2_8021Q_DISABLED] = "Disabled",
1844 [PORT_CONTROL_2_8021Q_FALLBACK] = "Fallback",
1845 [PORT_CONTROL_2_8021Q_CHECK] = "Check",
1846 [PORT_CONTROL_2_8021Q_SECURE] = "Secure",
1847};
1848
Vivien Didelotf81ec902016-05-09 13:22:58 -04001849static int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port,
1850 bool vlan_filtering)
Vivien Didelot214cdb92016-02-26 13:16:08 -05001851{
Vivien Didelot04bed142016-08-31 18:06:13 -04001852 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001853 u16 old, new = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE :
1854 PORT_CONTROL_2_8021Q_DISABLED;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001855 u16 reg;
1856 int err;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001857
Vivien Didelotfad09c72016-06-21 12:28:20 -04001858 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001859 return -EOPNOTSUPP;
1860
Vivien Didelotfad09c72016-06-21 12:28:20 -04001861 mutex_lock(&chip->reg_lock);
Vivien Didelot214cdb92016-02-26 13:16:08 -05001862
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001863 err = mv88e6xxx_port_read(chip, port, PORT_CONTROL_2, &reg);
1864 if (err)
Vivien Didelot214cdb92016-02-26 13:16:08 -05001865 goto unlock;
1866
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001867 old = reg & PORT_CONTROL_2_8021Q_MASK;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001868
Vivien Didelot5220ef12016-03-07 18:24:52 -05001869 if (new != old) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001870 reg &= ~PORT_CONTROL_2_8021Q_MASK;
1871 reg |= new & PORT_CONTROL_2_8021Q_MASK;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001872
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001873 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_2, reg);
1874 if (err)
Vivien Didelot5220ef12016-03-07 18:24:52 -05001875 goto unlock;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001876
Andrew Lunnc8b09802016-06-04 21:16:57 +02001877 netdev_dbg(ds->ports[port].netdev, "802.1Q Mode %s (was %s)\n",
Vivien Didelot5220ef12016-03-07 18:24:52 -05001878 mv88e6xxx_port_8021q_mode_names[new],
1879 mv88e6xxx_port_8021q_mode_names[old]);
1880 }
1881
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001882 err = 0;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001883unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001884 mutex_unlock(&chip->reg_lock);
Vivien Didelot214cdb92016-02-26 13:16:08 -05001885
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001886 return err;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001887}
1888
Vivien Didelot57d32312016-06-20 13:13:58 -04001889static int
1890mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
1891 const struct switchdev_obj_port_vlan *vlan,
1892 struct switchdev_trans *trans)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001893{
Vivien Didelot04bed142016-08-31 18:06:13 -04001894 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotda9c3592016-02-12 12:09:40 -05001895 int err;
1896
Vivien Didelotfad09c72016-06-21 12:28:20 -04001897 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001898 return -EOPNOTSUPP;
1899
Vivien Didelotda9c3592016-02-12 12:09:40 -05001900 /* If the requested port doesn't belong to the same bridge as the VLAN
1901 * members, do not support it (yet) and fallback to software VLAN.
1902 */
1903 err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin,
1904 vlan->vid_end);
1905 if (err)
1906 return err;
1907
Vivien Didelot76e398a2015-11-01 12:33:55 -05001908 /* We don't need any dynamic resource from the kernel (yet),
1909 * so skip the prepare phase.
1910 */
1911 return 0;
1912}
1913
Vivien Didelotfad09c72016-06-21 12:28:20 -04001914static int _mv88e6xxx_port_vlan_add(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001915 u16 vid, bool untagged)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001916{
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001917 struct mv88e6xxx_vtu_stu_entry vlan;
1918 int err;
1919
Vivien Didelotfad09c72016-06-21 12:28:20 -04001920 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, true);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001921 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001922 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001923
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001924 vlan.data[port] = untagged ?
1925 GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
1926 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
1927
Vivien Didelotfad09c72016-06-21 12:28:20 -04001928 return _mv88e6xxx_vtu_loadpurge(chip, &vlan);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001929}
1930
Vivien Didelotf81ec902016-05-09 13:22:58 -04001931static void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
1932 const struct switchdev_obj_port_vlan *vlan,
1933 struct switchdev_trans *trans)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001934{
Vivien Didelot04bed142016-08-31 18:06:13 -04001935 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001936 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1937 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1938 u16 vid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001939
Vivien Didelotfad09c72016-06-21 12:28:20 -04001940 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001941 return;
1942
Vivien Didelotfad09c72016-06-21 12:28:20 -04001943 mutex_lock(&chip->reg_lock);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001944
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001945 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
Vivien Didelotfad09c72016-06-21 12:28:20 -04001946 if (_mv88e6xxx_port_vlan_add(chip, port, vid, untagged))
Andrew Lunnc8b09802016-06-04 21:16:57 +02001947 netdev_err(ds->ports[port].netdev,
1948 "failed to add VLAN %d%c\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001949 vid, untagged ? 'u' : 't');
Vivien Didelot76e398a2015-11-01 12:33:55 -05001950
Vivien Didelotfad09c72016-06-21 12:28:20 -04001951 if (pvid && _mv88e6xxx_port_pvid_set(chip, port, vlan->vid_end))
Andrew Lunnc8b09802016-06-04 21:16:57 +02001952 netdev_err(ds->ports[port].netdev, "failed to set PVID %d\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001953 vlan->vid_end);
1954
Vivien Didelotfad09c72016-06-21 12:28:20 -04001955 mutex_unlock(&chip->reg_lock);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001956}
1957
Vivien Didelotfad09c72016-06-21 12:28:20 -04001958static int _mv88e6xxx_port_vlan_del(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001959 int port, u16 vid)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001960{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001961 struct dsa_switch *ds = chip->ds;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001962 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001963 int i, err;
1964
Vivien Didelotfad09c72016-06-21 12:28:20 -04001965 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001966 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001967 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001968
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001969 /* Tell switchdev if this VLAN is handled in software */
1970 if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
Vivien Didelot3c06f082016-02-05 14:04:39 -05001971 return -EOPNOTSUPP;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001972
1973 vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
1974
1975 /* keep the VLAN unless all ports are excluded */
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001976 vlan.valid = false;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001977 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelot3d131f02015-11-03 10:52:52 -05001978 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001979 continue;
1980
1981 if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001982 vlan.valid = true;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001983 break;
1984 }
1985 }
1986
Vivien Didelotfad09c72016-06-21 12:28:20 -04001987 err = _mv88e6xxx_vtu_loadpurge(chip, &vlan);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001988 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001989 return err;
1990
Vivien Didelotfad09c72016-06-21 12:28:20 -04001991 return _mv88e6xxx_atu_remove(chip, vlan.fid, port, false);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001992}
1993
Vivien Didelotf81ec902016-05-09 13:22:58 -04001994static int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
1995 const struct switchdev_obj_port_vlan *vlan)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001996{
Vivien Didelot04bed142016-08-31 18:06:13 -04001997 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001998 u16 pvid, vid;
1999 int err = 0;
2000
Vivien Didelotfad09c72016-06-21 12:28:20 -04002001 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04002002 return -EOPNOTSUPP;
2003
Vivien Didelotfad09c72016-06-21 12:28:20 -04002004 mutex_lock(&chip->reg_lock);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002005
Vivien Didelotfad09c72016-06-21 12:28:20 -04002006 err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002007 if (err)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002008 goto unlock;
2009
Vivien Didelot76e398a2015-11-01 12:33:55 -05002010 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002011 err = _mv88e6xxx_port_vlan_del(chip, port, vid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002012 if (err)
2013 goto unlock;
2014
2015 if (vid == pvid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002016 err = _mv88e6xxx_port_pvid_set(chip, port, 0);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002017 if (err)
2018 goto unlock;
2019 }
2020 }
2021
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002022unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002023 mutex_unlock(&chip->reg_lock);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002024
2025 return err;
2026}
2027
Vivien Didelotfad09c72016-06-21 12:28:20 -04002028static int _mv88e6xxx_atu_mac_write(struct mv88e6xxx_chip *chip,
Vivien Didelotc5723ac2015-08-10 09:09:48 -04002029 const unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002030{
Vivien Didelota935c052016-09-29 12:21:53 -04002031 int i, err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002032
2033 for (i = 0; i < 3; i++) {
Vivien Didelota935c052016-09-29 12:21:53 -04002034 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_MAC_01 + i,
2035 (addr[i * 2] << 8) | addr[i * 2 + 1]);
2036 if (err)
2037 return err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002038 }
2039
2040 return 0;
2041}
2042
Vivien Didelotfad09c72016-06-21 12:28:20 -04002043static int _mv88e6xxx_atu_mac_read(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04002044 unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002045{
Vivien Didelota935c052016-09-29 12:21:53 -04002046 u16 val;
2047 int i, err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002048
2049 for (i = 0; i < 3; i++) {
Vivien Didelota935c052016-09-29 12:21:53 -04002050 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_MAC_01 + i, &val);
2051 if (err)
2052 return err;
2053
2054 addr[i * 2] = val >> 8;
2055 addr[i * 2 + 1] = val & 0xff;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002056 }
2057
2058 return 0;
2059}
2060
Vivien Didelotfad09c72016-06-21 12:28:20 -04002061static int _mv88e6xxx_atu_load(struct mv88e6xxx_chip *chip,
Vivien Didelotfd231c82015-08-10 09:09:50 -04002062 struct mv88e6xxx_atu_entry *entry)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002063{
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002064 int ret;
2065
Vivien Didelotfad09c72016-06-21 12:28:20 -04002066 ret = _mv88e6xxx_atu_wait(chip);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002067 if (ret < 0)
2068 return ret;
2069
Vivien Didelotfad09c72016-06-21 12:28:20 -04002070 ret = _mv88e6xxx_atu_mac_write(chip, entry->mac);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002071 if (ret < 0)
2072 return ret;
2073
Vivien Didelotfad09c72016-06-21 12:28:20 -04002074 ret = _mv88e6xxx_atu_data_write(chip, entry);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002075 if (ret < 0)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002076 return ret;
2077
Vivien Didelotfad09c72016-06-21 12:28:20 -04002078 return _mv88e6xxx_atu_cmd(chip, entry->fid, GLOBAL_ATU_OP_LOAD_DB);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002079}
David S. Millercdf09692015-08-11 12:00:37 -07002080
Vivien Didelot88472932016-09-19 19:56:11 -04002081static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
2082 struct mv88e6xxx_atu_entry *entry);
2083
2084static int mv88e6xxx_atu_get(struct mv88e6xxx_chip *chip, int fid,
2085 const u8 *addr, struct mv88e6xxx_atu_entry *entry)
2086{
2087 struct mv88e6xxx_atu_entry next;
2088 int err;
2089
2090 eth_broadcast_addr(next.mac);
2091
2092 err = _mv88e6xxx_atu_mac_write(chip, next.mac);
2093 if (err)
2094 return err;
2095
2096 do {
2097 err = _mv88e6xxx_atu_getnext(chip, fid, &next);
2098 if (err)
2099 return err;
2100
2101 if (next.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2102 break;
2103
2104 if (ether_addr_equal(next.mac, addr)) {
2105 *entry = next;
2106 return 0;
2107 }
2108 } while (!is_broadcast_ether_addr(next.mac));
2109
2110 memset(entry, 0, sizeof(*entry));
2111 entry->fid = fid;
2112 ether_addr_copy(entry->mac, addr);
2113
2114 return 0;
2115}
2116
Vivien Didelot83dabd12016-08-31 11:50:04 -04002117static int mv88e6xxx_port_db_load_purge(struct mv88e6xxx_chip *chip, int port,
2118 const unsigned char *addr, u16 vid,
2119 u8 state)
Vivien Didelotfd231c82015-08-10 09:09:50 -04002120{
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002121 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot88472932016-09-19 19:56:11 -04002122 struct mv88e6xxx_atu_entry entry;
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002123 int err;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002124
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002125 /* Null VLAN ID corresponds to the port private database */
2126 if (vid == 0)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002127 err = _mv88e6xxx_port_fid_get(chip, port, &vlan.fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002128 else
Vivien Didelotfad09c72016-06-21 12:28:20 -04002129 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002130 if (err)
2131 return err;
2132
Vivien Didelot88472932016-09-19 19:56:11 -04002133 err = mv88e6xxx_atu_get(chip, vlan.fid, addr, &entry);
2134 if (err)
2135 return err;
2136
2137 /* Purge the ATU entry only if no port is using it anymore */
2138 if (state == GLOBAL_ATU_DATA_STATE_UNUSED) {
2139 entry.portv_trunkid &= ~BIT(port);
2140 if (!entry.portv_trunkid)
2141 entry.state = GLOBAL_ATU_DATA_STATE_UNUSED;
2142 } else {
2143 entry.portv_trunkid |= BIT(port);
2144 entry.state = state;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002145 }
2146
Vivien Didelotfad09c72016-06-21 12:28:20 -04002147 return _mv88e6xxx_atu_load(chip, &entry);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002148}
2149
Vivien Didelotf81ec902016-05-09 13:22:58 -04002150static int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
2151 const struct switchdev_obj_port_fdb *fdb,
2152 struct switchdev_trans *trans)
Vivien Didelot146a3202015-10-08 11:35:12 -04002153{
2154 /* We don't need any dynamic resource from the kernel (yet),
2155 * so skip the prepare phase.
2156 */
2157 return 0;
2158}
2159
Vivien Didelotf81ec902016-05-09 13:22:58 -04002160static void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
2161 const struct switchdev_obj_port_fdb *fdb,
2162 struct switchdev_trans *trans)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002163{
Vivien Didelot04bed142016-08-31 18:06:13 -04002164 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot6630e232015-08-06 01:44:07 -04002165
Vivien Didelotfad09c72016-06-21 12:28:20 -04002166 mutex_lock(&chip->reg_lock);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002167 if (mv88e6xxx_port_db_load_purge(chip, port, fdb->addr, fdb->vid,
2168 GLOBAL_ATU_DATA_STATE_UC_STATIC))
2169 netdev_err(ds->ports[port].netdev, "failed to load unicast MAC address\n");
Vivien Didelotfad09c72016-06-21 12:28:20 -04002170 mutex_unlock(&chip->reg_lock);
David S. Millercdf09692015-08-11 12:00:37 -07002171}
2172
Vivien Didelotf81ec902016-05-09 13:22:58 -04002173static int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
2174 const struct switchdev_obj_port_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -07002175{
Vivien Didelot04bed142016-08-31 18:06:13 -04002176 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot83dabd12016-08-31 11:50:04 -04002177 int err;
David S. Millercdf09692015-08-11 12:00:37 -07002178
Vivien Didelotfad09c72016-06-21 12:28:20 -04002179 mutex_lock(&chip->reg_lock);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002180 err = mv88e6xxx_port_db_load_purge(chip, port, fdb->addr, fdb->vid,
2181 GLOBAL_ATU_DATA_STATE_UNUSED);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002182 mutex_unlock(&chip->reg_lock);
David S. Millercdf09692015-08-11 12:00:37 -07002183
Vivien Didelot83dabd12016-08-31 11:50:04 -04002184 return err;
David S. Millercdf09692015-08-11 12:00:37 -07002185}
2186
Vivien Didelotfad09c72016-06-21 12:28:20 -04002187static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
Vivien Didelot1d194042015-08-10 09:09:51 -04002188 struct mv88e6xxx_atu_entry *entry)
David S. Millercdf09692015-08-11 12:00:37 -07002189{
Vivien Didelot1d194042015-08-10 09:09:51 -04002190 struct mv88e6xxx_atu_entry next = { 0 };
Vivien Didelota935c052016-09-29 12:21:53 -04002191 u16 val;
2192 int err;
Vivien Didelot1d194042015-08-10 09:09:51 -04002193
2194 next.fid = fid;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002195
Vivien Didelota935c052016-09-29 12:21:53 -04002196 err = _mv88e6xxx_atu_wait(chip);
2197 if (err)
2198 return err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002199
Vivien Didelota935c052016-09-29 12:21:53 -04002200 err = _mv88e6xxx_atu_cmd(chip, fid, GLOBAL_ATU_OP_GET_NEXT_DB);
2201 if (err)
2202 return err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002203
Vivien Didelota935c052016-09-29 12:21:53 -04002204 err = _mv88e6xxx_atu_mac_read(chip, next.mac);
2205 if (err)
2206 return err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002207
Vivien Didelota935c052016-09-29 12:21:53 -04002208 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_DATA, &val);
2209 if (err)
2210 return err;
Vivien Didelot1d194042015-08-10 09:09:51 -04002211
Vivien Didelota935c052016-09-29 12:21:53 -04002212 next.state = val & GLOBAL_ATU_DATA_STATE_MASK;
Vivien Didelot1d194042015-08-10 09:09:51 -04002213 if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2214 unsigned int mask, shift;
2215
Vivien Didelota935c052016-09-29 12:21:53 -04002216 if (val & GLOBAL_ATU_DATA_TRUNK) {
Vivien Didelot1d194042015-08-10 09:09:51 -04002217 next.trunk = true;
2218 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
2219 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
2220 } else {
2221 next.trunk = false;
2222 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
2223 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
2224 }
2225
Vivien Didelota935c052016-09-29 12:21:53 -04002226 next.portv_trunkid = (val & mask) >> shift;
Vivien Didelot1d194042015-08-10 09:09:51 -04002227 }
2228
2229 *entry = next;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002230 return 0;
2231}
2232
Vivien Didelot83dabd12016-08-31 11:50:04 -04002233static int mv88e6xxx_port_db_dump_fid(struct mv88e6xxx_chip *chip,
2234 u16 fid, u16 vid, int port,
2235 struct switchdev_obj *obj,
2236 int (*cb)(struct switchdev_obj *obj))
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002237{
2238 struct mv88e6xxx_atu_entry addr = {
2239 .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2240 };
2241 int err;
2242
Vivien Didelotfad09c72016-06-21 12:28:20 -04002243 err = _mv88e6xxx_atu_mac_write(chip, addr.mac);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002244 if (err)
2245 return err;
2246
2247 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002248 err = _mv88e6xxx_atu_getnext(chip, fid, &addr);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002249 if (err)
Vivien Didelot83dabd12016-08-31 11:50:04 -04002250 return err;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002251
2252 if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2253 break;
2254
Vivien Didelot83dabd12016-08-31 11:50:04 -04002255 if (addr.trunk || (addr.portv_trunkid & BIT(port)) == 0)
2256 continue;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002257
Vivien Didelot83dabd12016-08-31 11:50:04 -04002258 if (obj->id == SWITCHDEV_OBJ_ID_PORT_FDB) {
2259 struct switchdev_obj_port_fdb *fdb;
2260
2261 if (!is_unicast_ether_addr(addr.mac))
2262 continue;
2263
2264 fdb = SWITCHDEV_OBJ_PORT_FDB(obj);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002265 fdb->vid = vid;
2266 ether_addr_copy(fdb->addr, addr.mac);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002267 if (addr.state == GLOBAL_ATU_DATA_STATE_UC_STATIC)
2268 fdb->ndm_state = NUD_NOARP;
2269 else
2270 fdb->ndm_state = NUD_REACHABLE;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04002271 } else if (obj->id == SWITCHDEV_OBJ_ID_PORT_MDB) {
2272 struct switchdev_obj_port_mdb *mdb;
2273
2274 if (!is_multicast_ether_addr(addr.mac))
2275 continue;
2276
2277 mdb = SWITCHDEV_OBJ_PORT_MDB(obj);
2278 mdb->vid = vid;
2279 ether_addr_copy(mdb->addr, addr.mac);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002280 } else {
2281 return -EOPNOTSUPP;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002282 }
Vivien Didelot83dabd12016-08-31 11:50:04 -04002283
2284 err = cb(obj);
2285 if (err)
2286 return err;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002287 } while (!is_broadcast_ether_addr(addr.mac));
2288
2289 return err;
2290}
2291
Vivien Didelot83dabd12016-08-31 11:50:04 -04002292static int mv88e6xxx_port_db_dump(struct mv88e6xxx_chip *chip, int port,
2293 struct switchdev_obj *obj,
2294 int (*cb)(struct switchdev_obj *obj))
2295{
2296 struct mv88e6xxx_vtu_stu_entry vlan = {
2297 .vid = GLOBAL_VTU_VID_MASK, /* all ones */
2298 };
2299 u16 fid;
2300 int err;
2301
2302 /* Dump port's default Filtering Information Database (VLAN ID 0) */
2303 err = _mv88e6xxx_port_fid_get(chip, port, &fid);
2304 if (err)
2305 return err;
2306
2307 err = mv88e6xxx_port_db_dump_fid(chip, fid, 0, port, obj, cb);
2308 if (err)
2309 return err;
2310
2311 /* Dump VLANs' Filtering Information Databases */
2312 err = _mv88e6xxx_vtu_vid_write(chip, vlan.vid);
2313 if (err)
2314 return err;
2315
2316 do {
2317 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
2318 if (err)
2319 return err;
2320
2321 if (!vlan.valid)
2322 break;
2323
2324 err = mv88e6xxx_port_db_dump_fid(chip, vlan.fid, vlan.vid, port,
2325 obj, cb);
2326 if (err)
2327 return err;
2328 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
2329
2330 return err;
2331}
2332
Vivien Didelotf81ec902016-05-09 13:22:58 -04002333static int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
2334 struct switchdev_obj_port_fdb *fdb,
2335 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotf33475b2015-10-22 09:34:41 -04002336{
Vivien Didelot04bed142016-08-31 18:06:13 -04002337 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002338 int err;
2339
Vivien Didelotfad09c72016-06-21 12:28:20 -04002340 mutex_lock(&chip->reg_lock);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002341 err = mv88e6xxx_port_db_dump(chip, port, &fdb->obj, cb);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002342 mutex_unlock(&chip->reg_lock);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002343
2344 return err;
2345}
2346
Vivien Didelotf81ec902016-05-09 13:22:58 -04002347static int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
2348 struct net_device *bridge)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002349{
Vivien Didelot04bed142016-08-31 18:06:13 -04002350 struct mv88e6xxx_chip *chip = ds->priv;
Colin Ian King1d9619d2016-04-25 23:11:22 +01002351 int i, err = 0;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002352
Vivien Didelotfad09c72016-06-21 12:28:20 -04002353 mutex_lock(&chip->reg_lock);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002354
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002355 /* Assign the bridge and remap each port's VLANTable */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002356 chip->ports[port].bridge_dev = bridge;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002357
Vivien Didelotfad09c72016-06-21 12:28:20 -04002358 for (i = 0; i < chip->info->num_ports; ++i) {
2359 if (chip->ports[i].bridge_dev == bridge) {
2360 err = _mv88e6xxx_port_based_vlan_map(chip, i);
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002361 if (err)
2362 break;
2363 }
2364 }
2365
Vivien Didelotfad09c72016-06-21 12:28:20 -04002366 mutex_unlock(&chip->reg_lock);
Vivien Didelota6692752016-02-12 12:09:39 -05002367
Vivien Didelot466dfa02016-02-26 13:16:05 -05002368 return err;
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002369}
2370
Vivien Didelotf81ec902016-05-09 13:22:58 -04002371static void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002372{
Vivien Didelot04bed142016-08-31 18:06:13 -04002373 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002374 struct net_device *bridge = chip->ports[port].bridge_dev;
Vivien Didelot16bfa702016-03-13 16:21:33 -04002375 int i;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002376
Vivien Didelotfad09c72016-06-21 12:28:20 -04002377 mutex_lock(&chip->reg_lock);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002378
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002379 /* Unassign the bridge and remap each port's VLANTable */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002380 chip->ports[port].bridge_dev = NULL;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002381
Vivien Didelotfad09c72016-06-21 12:28:20 -04002382 for (i = 0; i < chip->info->num_ports; ++i)
2383 if (i == port || chip->ports[i].bridge_dev == bridge)
2384 if (_mv88e6xxx_port_based_vlan_map(chip, i))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002385 netdev_warn(ds->ports[i].netdev,
2386 "failed to remap\n");
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002387
Vivien Didelotfad09c72016-06-21 12:28:20 -04002388 mutex_unlock(&chip->reg_lock);
Vivien Didelot66d9cd02016-02-05 14:07:14 -05002389}
2390
Vivien Didelotfad09c72016-06-21 12:28:20 -04002391static int mv88e6xxx_switch_reset(struct mv88e6xxx_chip *chip)
Vivien Didelot552238b2016-05-09 13:22:49 -04002392{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002393 bool ppu_active = mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE);
Vivien Didelot552238b2016-05-09 13:22:49 -04002394 u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002395 struct gpio_desc *gpiod = chip->reset;
Vivien Didelot552238b2016-05-09 13:22:49 -04002396 unsigned long timeout;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002397 u16 reg;
Vivien Didelota935c052016-09-29 12:21:53 -04002398 int err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002399 int i;
2400
2401 /* Set all ports to the disabled state. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002402 for (i = 0; i < chip->info->num_ports; i++) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002403 err = mv88e6xxx_port_read(chip, i, PORT_CONTROL, &reg);
2404 if (err)
2405 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002406
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002407 err = mv88e6xxx_port_write(chip, i, PORT_CONTROL,
2408 reg & 0xfffc);
2409 if (err)
2410 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002411 }
2412
2413 /* Wait for transmit queues to drain. */
2414 usleep_range(2000, 4000);
2415
2416 /* If there is a gpio connected to the reset pin, toggle it */
2417 if (gpiod) {
2418 gpiod_set_value_cansleep(gpiod, 1);
2419 usleep_range(10000, 20000);
2420 gpiod_set_value_cansleep(gpiod, 0);
2421 usleep_range(10000, 20000);
2422 }
2423
2424 /* Reset the switch. Keep the PPU active if requested. The PPU
2425 * needs to be active to support indirect phy register access
2426 * through global registers 0x18 and 0x19.
2427 */
2428 if (ppu_active)
Vivien Didelota935c052016-09-29 12:21:53 -04002429 err = mv88e6xxx_g1_write(chip, 0x04, 0xc000);
Vivien Didelot552238b2016-05-09 13:22:49 -04002430 else
Vivien Didelota935c052016-09-29 12:21:53 -04002431 err = mv88e6xxx_g1_write(chip, 0x04, 0xc400);
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002432 if (err)
2433 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002434
2435 /* Wait up to one second for reset to complete. */
2436 timeout = jiffies + 1 * HZ;
2437 while (time_before(jiffies, timeout)) {
Vivien Didelota935c052016-09-29 12:21:53 -04002438 err = mv88e6xxx_g1_read(chip, 0x00, &reg);
2439 if (err)
2440 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002441
Vivien Didelota935c052016-09-29 12:21:53 -04002442 if ((reg & is_reset) == is_reset)
Vivien Didelot552238b2016-05-09 13:22:49 -04002443 break;
2444 usleep_range(1000, 2000);
2445 }
2446 if (time_after(jiffies, timeout))
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002447 err = -ETIMEDOUT;
Vivien Didelot552238b2016-05-09 13:22:49 -04002448 else
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002449 err = 0;
Vivien Didelot552238b2016-05-09 13:22:49 -04002450
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002451 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002452}
2453
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002454static int mv88e6xxx_serdes_power_on(struct mv88e6xxx_chip *chip)
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002455{
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002456 u16 val;
2457 int err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002458
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002459 /* Clear Power Down bit */
2460 err = mv88e6xxx_serdes_read(chip, MII_BMCR, &val);
2461 if (err)
2462 return err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002463
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002464 if (val & BMCR_PDOWN) {
2465 val &= ~BMCR_PDOWN;
2466 err = mv88e6xxx_serdes_write(chip, MII_BMCR, val);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002467 }
2468
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002469 return err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002470}
2471
Vivien Didelotfad09c72016-06-21 12:28:20 -04002472static int mv88e6xxx_setup_port(struct mv88e6xxx_chip *chip, int port)
Guenter Roeckd827e882015-03-26 18:36:29 -07002473{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002474 struct dsa_switch *ds = chip->ds;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002475 int err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002476 u16 reg;
Guenter Roeckd827e882015-03-26 18:36:29 -07002477
Vivien Didelotfad09c72016-06-21 12:28:20 -04002478 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2479 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2480 mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip) ||
2481 mv88e6xxx_6065_family(chip) || mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002482 /* MAC Forcing register: don't force link, speed,
2483 * duplex or flow control state to any particular
2484 * values on physical ports, but force the CPU port
2485 * and all DSA ports to their maximum bandwidth and
2486 * full duplex.
2487 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002488 err = mv88e6xxx_port_read(chip, port, PORT_PCS_CTRL, &reg);
Andrew Lunn60045cb2015-08-17 23:52:51 +02002489 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
Russell King53adc9e2015-09-21 21:42:59 +01002490 reg &= ~PORT_PCS_CTRL_UNFORCED;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002491 reg |= PORT_PCS_CTRL_FORCE_LINK |
2492 PORT_PCS_CTRL_LINK_UP |
2493 PORT_PCS_CTRL_DUPLEX_FULL |
2494 PORT_PCS_CTRL_FORCE_DUPLEX;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002495 if (mv88e6xxx_6065_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002496 reg |= PORT_PCS_CTRL_100;
2497 else
2498 reg |= PORT_PCS_CTRL_1000;
2499 } else {
2500 reg |= PORT_PCS_CTRL_UNFORCED;
2501 }
2502
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002503 err = mv88e6xxx_port_write(chip, port, PORT_PCS_CTRL, reg);
2504 if (err)
2505 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002506 }
2507
2508 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
2509 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
2510 * tunneling, determine priority by looking at 802.1p and IP
2511 * priority fields (IP prio has precedence), and set STP state
2512 * to Forwarding.
2513 *
2514 * If this is the CPU link, use DSA or EDSA tagging depending
2515 * on which tagging mode was configured.
2516 *
2517 * If this is a link to another switch, use DSA tagging mode.
2518 *
2519 * If this is the upstream port for this switch, enable
2520 * forwarding of unknown unicasts and multicasts.
2521 */
2522 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002523 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2524 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2525 mv88e6xxx_6095_family(chip) || mv88e6xxx_6065_family(chip) ||
2526 mv88e6xxx_6185_family(chip) || mv88e6xxx_6320_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002527 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
2528 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
2529 PORT_CONTROL_STATE_FORWARDING;
2530 if (dsa_is_cpu_port(ds, port)) {
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002531 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA))
Andrew Lunn5377b802016-06-04 21:17:02 +02002532 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA |
Andrew Lunnc047a1f2015-09-29 01:50:56 +02002533 PORT_CONTROL_FORWARD_UNKNOWN_MC;
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002534 else
2535 reg |= PORT_CONTROL_DSA_TAG;
Jamie Lentinf027e0c2016-08-22 16:01:04 +02002536 reg |= PORT_CONTROL_EGRESS_ADD_TAG |
2537 PORT_CONTROL_FORWARD_UNKNOWN;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002538 }
Andrew Lunn6083ce72015-08-17 23:52:52 +02002539 if (dsa_is_dsa_port(ds, port)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002540 if (mv88e6xxx_6095_family(chip) ||
2541 mv88e6xxx_6185_family(chip))
Andrew Lunn6083ce72015-08-17 23:52:52 +02002542 reg |= PORT_CONTROL_DSA_TAG;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002543 if (mv88e6xxx_6352_family(chip) ||
2544 mv88e6xxx_6351_family(chip) ||
2545 mv88e6xxx_6165_family(chip) ||
2546 mv88e6xxx_6097_family(chip) ||
2547 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002548 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunn6083ce72015-08-17 23:52:52 +02002549 }
2550
Andrew Lunn54d792f2015-05-06 01:09:47 +02002551 if (port == dsa_upstream_port(ds))
2552 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2553 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2554 }
2555 if (reg) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002556 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL, reg);
2557 if (err)
2558 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002559 }
2560
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002561 /* If this port is connected to a SerDes, make sure the SerDes is not
2562 * powered down.
2563 */
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002564 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_SERDES)) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002565 err = mv88e6xxx_port_read(chip, port, PORT_STATUS, &reg);
2566 if (err)
2567 return err;
2568 reg &= PORT_STATUS_CMODE_MASK;
2569 if ((reg == PORT_STATUS_CMODE_100BASE_X) ||
2570 (reg == PORT_STATUS_CMODE_1000BASE_X) ||
2571 (reg == PORT_STATUS_CMODE_SGMII)) {
2572 err = mv88e6xxx_serdes_power_on(chip);
2573 if (err < 0)
2574 return err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002575 }
2576 }
2577
Vivien Didelot8efdda42015-08-13 12:52:23 -04002578 /* Port Control 2: don't force a good FCS, set the maximum frame size to
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002579 * 10240 bytes, disable 802.1q tags checking, don't discard tagged or
Vivien Didelot8efdda42015-08-13 12:52:23 -04002580 * untagged frames on this port, do a destination address lookup on all
2581 * received packets as usual, disable ARP mirroring and don't send a
2582 * copy of all transmitted/received frames on this port to the CPU.
Andrew Lunn54d792f2015-05-06 01:09:47 +02002583 */
2584 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002585 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2586 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2587 mv88e6xxx_6095_family(chip) || mv88e6xxx_6320_family(chip) ||
2588 mv88e6xxx_6185_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002589 reg = PORT_CONTROL_2_MAP_DA;
2590
Vivien Didelotfad09c72016-06-21 12:28:20 -04002591 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2592 mv88e6xxx_6165_family(chip) || mv88e6xxx_6320_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002593 reg |= PORT_CONTROL_2_JUMBO_10240;
2594
Vivien Didelotfad09c72016-06-21 12:28:20 -04002595 if (mv88e6xxx_6095_family(chip) || mv88e6xxx_6185_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002596 /* Set the upstream port this port should use */
2597 reg |= dsa_upstream_port(ds);
2598 /* enable forwarding of unknown multicast addresses to
2599 * the upstream port
2600 */
2601 if (port == dsa_upstream_port(ds))
2602 reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
2603 }
2604
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002605 reg |= PORT_CONTROL_2_8021Q_DISABLED;
Vivien Didelot8efdda42015-08-13 12:52:23 -04002606
Andrew Lunn54d792f2015-05-06 01:09:47 +02002607 if (reg) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002608 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_2, reg);
2609 if (err)
2610 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002611 }
2612
2613 /* Port Association Vector: when learning source addresses
2614 * of packets, add the address to the address database using
2615 * a port bitmap that has only the bit for this port set and
2616 * the other bits clear.
2617 */
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002618 reg = 1 << port;
Vivien Didelot996ecb82016-04-14 14:42:08 -04002619 /* Disable learning for CPU port */
2620 if (dsa_is_cpu_port(ds, port))
Vivien Didelot65fa4022016-04-14 14:42:07 -04002621 reg = 0;
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002622
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002623 err = mv88e6xxx_port_write(chip, port, PORT_ASSOC_VECTOR, reg);
2624 if (err)
2625 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002626
2627 /* Egress rate control 2: disable egress rate control. */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002628 err = mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL_2, 0x0000);
2629 if (err)
2630 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002631
Vivien Didelotfad09c72016-06-21 12:28:20 -04002632 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2633 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2634 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002635 /* Do not limit the period of time that this port can
2636 * be paused for by the remote end or the period of
2637 * time that this port can pause the remote end.
2638 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002639 err = mv88e6xxx_port_write(chip, port, PORT_PAUSE_CTRL, 0x0000);
2640 if (err)
2641 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002642
2643 /* Port ATU control: disable limiting the number of
2644 * address database entries that this port is allowed
2645 * to use.
2646 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002647 err = mv88e6xxx_port_write(chip, port, PORT_ATU_CONTROL,
2648 0x0000);
Andrew Lunn54d792f2015-05-06 01:09:47 +02002649 /* Priority Override: disable DA, SA and VTU priority
2650 * override.
2651 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002652 err = mv88e6xxx_port_write(chip, port, PORT_PRI_OVERRIDE,
2653 0x0000);
2654 if (err)
2655 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002656
2657 /* Port Ethertype: use the Ethertype DSA Ethertype
2658 * value.
2659 */
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002660 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA)) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002661 err = mv88e6xxx_port_write(chip, port, PORT_ETH_TYPE,
2662 ETH_P_EDSA);
2663 if (err)
2664 return err;
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002665 }
2666
Andrew Lunn54d792f2015-05-06 01:09:47 +02002667 /* Tag Remap: use an identity 802.1p prio -> switch
2668 * prio mapping.
2669 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002670 err = mv88e6xxx_port_write(chip, port, PORT_TAG_REGMAP_0123,
2671 0x3210);
2672 if (err)
2673 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002674
2675 /* Tag Remap 2: use an identity 802.1p prio -> switch
2676 * prio mapping.
2677 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002678 err = mv88e6xxx_port_write(chip, port, PORT_TAG_REGMAP_4567,
2679 0x7654);
2680 if (err)
2681 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002682 }
2683
Jamie Lentin1bc261f2016-08-22 22:47:08 +01002684 /* Rate Control: disable ingress rate limiting. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002685 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2686 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
Vivien Didelotfad09c72016-06-21 12:28:20 -04002687 mv88e6xxx_6320_family(chip)) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002688 err = mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL,
2689 0x0001);
2690 if (err)
2691 return err;
Jamie Lentin1bc261f2016-08-22 22:47:08 +01002692 } else if (mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip)) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002693 err = mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL,
2694 0x0000);
2695 if (err)
2696 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002697 }
2698
Guenter Roeck366f0a02015-03-26 18:36:30 -07002699 /* Port Control 1: disable trunking, disable sending
2700 * learning messages to this port.
Guenter Roeckd827e882015-03-26 18:36:29 -07002701 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002702 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_1, 0x0000);
2703 if (err)
2704 return err;
Guenter Roeckd827e882015-03-26 18:36:29 -07002705
Vivien Didelot207afda2016-04-14 14:42:09 -04002706 /* Port based VLAN map: give each port the same default address
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002707 * database, and allow bidirectional communication between the
2708 * CPU and DSA port(s), and the other ports.
Guenter Roeckd827e882015-03-26 18:36:29 -07002709 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002710 err = _mv88e6xxx_port_fid_set(chip, port, 0);
2711 if (err)
2712 return err;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002713
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002714 err = _mv88e6xxx_port_based_vlan_map(chip, port);
2715 if (err)
2716 return err;
Guenter Roeckd827e882015-03-26 18:36:29 -07002717
2718 /* Default VLAN ID and priority: don't set a default VLAN
2719 * ID, and set the default packet priority to zero.
2720 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002721 return mv88e6xxx_port_write(chip, port, PORT_DEFAULT_VLAN, 0x0000);
Andrew Lunndbde9e62015-05-06 01:09:48 +02002722}
2723
Vivien Didelota935c052016-09-29 12:21:53 -04002724int mv88e6xxx_g1_set_switch_mac(struct mv88e6xxx_chip *chip, u8 *addr)
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002725{
2726 int err;
2727
Vivien Didelota935c052016-09-29 12:21:53 -04002728 err = mv88e6xxx_g1_write(chip, GLOBAL_MAC_01, (addr[0] << 8) | addr[1]);
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002729 if (err)
2730 return err;
2731
Vivien Didelota935c052016-09-29 12:21:53 -04002732 err = mv88e6xxx_g1_write(chip, GLOBAL_MAC_23, (addr[2] << 8) | addr[3]);
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002733 if (err)
2734 return err;
2735
Vivien Didelota935c052016-09-29 12:21:53 -04002736 err = mv88e6xxx_g1_write(chip, GLOBAL_MAC_45, (addr[4] << 8) | addr[5]);
2737 if (err)
2738 return err;
2739
2740 return 0;
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002741}
2742
Vivien Didelotacddbd22016-07-18 20:45:39 -04002743static int mv88e6xxx_g1_set_age_time(struct mv88e6xxx_chip *chip,
2744 unsigned int msecs)
2745{
2746 const unsigned int coeff = chip->info->age_time_coeff;
2747 const unsigned int min = 0x01 * coeff;
2748 const unsigned int max = 0xff * coeff;
2749 u8 age_time;
2750 u16 val;
2751 int err;
2752
2753 if (msecs < min || msecs > max)
2754 return -ERANGE;
2755
2756 /* Round to nearest multiple of coeff */
2757 age_time = (msecs + coeff / 2) / coeff;
2758
Vivien Didelota935c052016-09-29 12:21:53 -04002759 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_CONTROL, &val);
Vivien Didelotacddbd22016-07-18 20:45:39 -04002760 if (err)
2761 return err;
2762
2763 /* AgeTime is 11:4 bits */
2764 val &= ~0xff0;
2765 val |= age_time << 4;
2766
Vivien Didelota935c052016-09-29 12:21:53 -04002767 return mv88e6xxx_g1_write(chip, GLOBAL_ATU_CONTROL, val);
Vivien Didelotacddbd22016-07-18 20:45:39 -04002768}
2769
Vivien Didelot2cfcd962016-07-18 20:45:40 -04002770static int mv88e6xxx_set_ageing_time(struct dsa_switch *ds,
2771 unsigned int ageing_time)
2772{
Vivien Didelot04bed142016-08-31 18:06:13 -04002773 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot2cfcd962016-07-18 20:45:40 -04002774 int err;
2775
2776 mutex_lock(&chip->reg_lock);
2777 err = mv88e6xxx_g1_set_age_time(chip, ageing_time);
2778 mutex_unlock(&chip->reg_lock);
2779
2780 return err;
2781}
2782
Vivien Didelot97299342016-07-18 20:45:30 -04002783static int mv88e6xxx_g1_setup(struct mv88e6xxx_chip *chip)
Vivien Didelot08a01262016-05-09 13:22:50 -04002784{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002785 struct dsa_switch *ds = chip->ds;
Vivien Didelotb0745e872016-05-09 13:22:53 -04002786 u32 upstream_port = dsa_upstream_port(ds);
Vivien Didelot119477b2016-05-09 13:22:51 -04002787 u16 reg;
Vivien Didelot08a01262016-05-09 13:22:50 -04002788 int err;
Vivien Didelot08a01262016-05-09 13:22:50 -04002789
Vivien Didelot119477b2016-05-09 13:22:51 -04002790 /* Enable the PHY Polling Unit if present, don't discard any packets,
2791 * and mask all interrupt sources.
2792 */
2793 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002794 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU) ||
2795 mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE))
Vivien Didelot119477b2016-05-09 13:22:51 -04002796 reg |= GLOBAL_CONTROL_PPU_ENABLE;
2797
Vivien Didelota935c052016-09-29 12:21:53 -04002798 err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL, reg);
Vivien Didelot119477b2016-05-09 13:22:51 -04002799 if (err)
2800 return err;
2801
Vivien Didelotb0745e872016-05-09 13:22:53 -04002802 /* Configure the upstream port, and configure it as the port to which
2803 * ingress and egress and ARP monitor frames are to be sent.
2804 */
2805 reg = upstream_port << GLOBAL_MONITOR_CONTROL_INGRESS_SHIFT |
2806 upstream_port << GLOBAL_MONITOR_CONTROL_EGRESS_SHIFT |
2807 upstream_port << GLOBAL_MONITOR_CONTROL_ARP_SHIFT;
Vivien Didelota935c052016-09-29 12:21:53 -04002808 err = mv88e6xxx_g1_write(chip, GLOBAL_MONITOR_CONTROL, reg);
Vivien Didelotb0745e872016-05-09 13:22:53 -04002809 if (err)
2810 return err;
2811
Vivien Didelot50484ff2016-05-09 13:22:54 -04002812 /* Disable remote management, and set the switch's DSA device number. */
Vivien Didelota935c052016-09-29 12:21:53 -04002813 err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL_2,
2814 GLOBAL_CONTROL_2_MULTIPLE_CASCADE |
2815 (ds->index & 0x1f));
Vivien Didelot50484ff2016-05-09 13:22:54 -04002816 if (err)
2817 return err;
2818
Vivien Didelotacddbd22016-07-18 20:45:39 -04002819 /* Clear all the VTU and STU entries */
2820 err = _mv88e6xxx_vtu_stu_flush(chip);
2821 if (err < 0)
2822 return err;
2823
Vivien Didelot08a01262016-05-09 13:22:50 -04002824 /* Set the default address aging time to 5 minutes, and
2825 * enable address learn messages to be sent to all message
2826 * ports.
2827 */
Vivien Didelota935c052016-09-29 12:21:53 -04002828 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_CONTROL,
2829 GLOBAL_ATU_CONTROL_LEARN2ALL);
Vivien Didelot08a01262016-05-09 13:22:50 -04002830 if (err)
2831 return err;
2832
Vivien Didelotacddbd22016-07-18 20:45:39 -04002833 err = mv88e6xxx_g1_set_age_time(chip, 300000);
2834 if (err)
Vivien Didelot97299342016-07-18 20:45:30 -04002835 return err;
2836
2837 /* Clear all ATU entries */
2838 err = _mv88e6xxx_atu_flush(chip, 0, true);
2839 if (err)
2840 return err;
2841
Vivien Didelot08a01262016-05-09 13:22:50 -04002842 /* Configure the IP ToS mapping registers. */
Vivien Didelota935c052016-09-29 12:21:53 -04002843 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_0, 0x0000);
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_1, 0x0000);
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_2, 0x5555);
Vivien Didelot08a01262016-05-09 13:22:50 -04002850 if (err)
2851 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002852 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_3, 0x5555);
Vivien Didelot08a01262016-05-09 13:22:50 -04002853 if (err)
2854 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002855 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_4, 0xaaaa);
Vivien Didelot08a01262016-05-09 13:22:50 -04002856 if (err)
2857 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002858 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_5, 0xaaaa);
Vivien Didelot08a01262016-05-09 13:22:50 -04002859 if (err)
2860 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002861 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_6, 0xffff);
Vivien Didelot08a01262016-05-09 13:22:50 -04002862 if (err)
2863 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002864 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_7, 0xffff);
Vivien Didelot08a01262016-05-09 13:22:50 -04002865 if (err)
2866 return err;
2867
2868 /* Configure the IEEE 802.1p priority mapping register. */
Vivien Didelota935c052016-09-29 12:21:53 -04002869 err = mv88e6xxx_g1_write(chip, GLOBAL_IEEE_PRI, 0xfa41);
Vivien Didelot08a01262016-05-09 13:22:50 -04002870 if (err)
2871 return err;
2872
Vivien Didelot97299342016-07-18 20:45:30 -04002873 /* Clear the statistics counters for all ports */
Vivien Didelota935c052016-09-29 12:21:53 -04002874 err = mv88e6xxx_g1_write(chip, GLOBAL_STATS_OP,
2875 GLOBAL_STATS_OP_FLUSH_ALL);
Vivien Didelot97299342016-07-18 20:45:30 -04002876 if (err)
2877 return err;
2878
2879 /* Wait for the flush to complete. */
2880 err = _mv88e6xxx_stats_wait(chip);
2881 if (err)
2882 return err;
2883
2884 return 0;
2885}
2886
Vivien Didelotf81ec902016-05-09 13:22:58 -04002887static int mv88e6xxx_setup(struct dsa_switch *ds)
Guenter Roeckacdaffc2015-03-26 18:36:28 -07002888{
Vivien Didelot04bed142016-08-31 18:06:13 -04002889 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot552238b2016-05-09 13:22:49 -04002890 int err;
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002891 int i;
2892
Vivien Didelotfad09c72016-06-21 12:28:20 -04002893 chip->ds = ds;
2894 ds->slave_mii_bus = chip->mdio_bus;
Vivien Didelot552238b2016-05-09 13:22:49 -04002895
Vivien Didelotfad09c72016-06-21 12:28:20 -04002896 mutex_lock(&chip->reg_lock);
Vivien Didelot552238b2016-05-09 13:22:49 -04002897
Vivien Didelotfad09c72016-06-21 12:28:20 -04002898 err = mv88e6xxx_switch_reset(chip);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002899 if (err)
2900 goto unlock;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002901
Vivien Didelot97299342016-07-18 20:45:30 -04002902 /* Setup Switch Port Registers */
2903 for (i = 0; i < chip->info->num_ports; i++) {
2904 err = mv88e6xxx_setup_port(chip, i);
2905 if (err)
2906 goto unlock;
2907 }
2908
2909 /* Setup Switch Global 1 Registers */
2910 err = mv88e6xxx_g1_setup(chip);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002911 if (err)
2912 goto unlock;
2913
Vivien Didelot97299342016-07-18 20:45:30 -04002914 /* Setup Switch Global 2 Registers */
2915 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_GLOBAL2)) {
2916 err = mv88e6xxx_g2_setup(chip);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002917 if (err)
2918 goto unlock;
2919 }
Andrew Lunn54d792f2015-05-06 01:09:47 +02002920
Vivien Didelot6b17e862015-08-13 12:52:18 -04002921unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002922 mutex_unlock(&chip->reg_lock);
Andrew Lunndb687a52015-06-20 21:31:29 +02002923
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002924 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002925}
2926
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002927static int mv88e6xxx_set_addr(struct dsa_switch *ds, u8 *addr)
2928{
Vivien Didelot04bed142016-08-31 18:06:13 -04002929 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002930 int err;
2931
2932 mutex_lock(&chip->reg_lock);
2933
2934 /* Has an indirect Switch MAC/WoL/WoF register in Global 2? */
2935 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_SWITCH_MAC))
2936 err = mv88e6xxx_g2_set_switch_mac(chip, addr);
2937 else
2938 err = mv88e6xxx_g1_set_switch_mac(chip, addr);
2939
2940 mutex_unlock(&chip->reg_lock);
2941
2942 return err;
2943}
2944
Vivien Didelote57e5e72016-08-15 17:19:00 -04002945static int mv88e6xxx_mdio_read(struct mii_bus *bus, int phy, int reg)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002946{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002947 struct mv88e6xxx_chip *chip = bus->priv;
Vivien Didelote57e5e72016-08-15 17:19:00 -04002948 u16 val;
2949 int err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002950
Vivien Didelote57e5e72016-08-15 17:19:00 -04002951 if (phy >= chip->info->num_ports)
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_read(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 ? err : val;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002959}
2960
Vivien Didelote57e5e72016-08-15 17:19:00 -04002961static int mv88e6xxx_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002962{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002963 struct mv88e6xxx_chip *chip = bus->priv;
Vivien Didelote57e5e72016-08-15 17:19:00 -04002964 int err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002965
Vivien Didelote57e5e72016-08-15 17:19:00 -04002966 if (phy >= chip->info->num_ports)
Andrew Lunn158bc062016-04-28 21:24:06 -04002967 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002968
Vivien Didelotfad09c72016-06-21 12:28:20 -04002969 mutex_lock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04002970 err = mv88e6xxx_phy_write(chip, phy, reg, val);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002971 mutex_unlock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04002972
2973 return err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002974}
2975
Vivien Didelotfad09c72016-06-21 12:28:20 -04002976static int mv88e6xxx_mdio_register(struct mv88e6xxx_chip *chip,
Andrew Lunnb516d452016-06-04 21:17:06 +02002977 struct device_node *np)
2978{
2979 static int index;
2980 struct mii_bus *bus;
2981 int err;
2982
Andrew Lunnb516d452016-06-04 21:17:06 +02002983 if (np)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002984 chip->mdio_np = of_get_child_by_name(np, "mdio");
Andrew Lunnb516d452016-06-04 21:17:06 +02002985
Vivien Didelotfad09c72016-06-21 12:28:20 -04002986 bus = devm_mdiobus_alloc(chip->dev);
Andrew Lunnb516d452016-06-04 21:17:06 +02002987 if (!bus)
2988 return -ENOMEM;
2989
Vivien Didelotfad09c72016-06-21 12:28:20 -04002990 bus->priv = (void *)chip;
Andrew Lunnb516d452016-06-04 21:17:06 +02002991 if (np) {
2992 bus->name = np->full_name;
2993 snprintf(bus->id, MII_BUS_ID_SIZE, "%s", np->full_name);
2994 } else {
2995 bus->name = "mv88e6xxx SMI";
2996 snprintf(bus->id, MII_BUS_ID_SIZE, "mv88e6xxx-%d", index++);
2997 }
2998
2999 bus->read = mv88e6xxx_mdio_read;
3000 bus->write = mv88e6xxx_mdio_write;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003001 bus->parent = chip->dev;
Andrew Lunnb516d452016-06-04 21:17:06 +02003002
Vivien Didelotfad09c72016-06-21 12:28:20 -04003003 if (chip->mdio_np)
3004 err = of_mdiobus_register(bus, chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003005 else
3006 err = mdiobus_register(bus);
3007 if (err) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04003008 dev_err(chip->dev, "Cannot register MDIO bus (%d)\n", err);
Andrew Lunnb516d452016-06-04 21:17:06 +02003009 goto out;
3010 }
Vivien Didelotfad09c72016-06-21 12:28:20 -04003011 chip->mdio_bus = bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003012
3013 return 0;
3014
3015out:
Vivien Didelotfad09c72016-06-21 12:28:20 -04003016 if (chip->mdio_np)
3017 of_node_put(chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003018
3019 return err;
3020}
3021
Vivien Didelotfad09c72016-06-21 12:28:20 -04003022static void mv88e6xxx_mdio_unregister(struct mv88e6xxx_chip *chip)
Andrew Lunnb516d452016-06-04 21:17:06 +02003023
3024{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003025 struct mii_bus *bus = chip->mdio_bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003026
3027 mdiobus_unregister(bus);
3028
Vivien Didelotfad09c72016-06-21 12:28:20 -04003029 if (chip->mdio_np)
3030 of_node_put(chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003031}
3032
Guenter Roeckc22995c2015-07-25 09:42:28 -07003033#ifdef CONFIG_NET_DSA_HWMON
3034
3035static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
3036{
Vivien Didelot04bed142016-08-31 18:06:13 -04003037 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot9c938292016-08-15 17:19:02 -04003038 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003039 int ret;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003040
3041 *temp = 0;
3042
Vivien Didelotfad09c72016-06-21 12:28:20 -04003043 mutex_lock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003044
Vivien Didelot9c938292016-08-15 17:19:02 -04003045 ret = mv88e6xxx_phy_write(chip, 0x0, 0x16, 0x6);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003046 if (ret < 0)
3047 goto error;
3048
3049 /* Enable temperature sensor */
Vivien Didelot9c938292016-08-15 17:19:02 -04003050 ret = mv88e6xxx_phy_read(chip, 0x0, 0x1a, &val);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003051 if (ret < 0)
3052 goto error;
3053
Vivien Didelot9c938292016-08-15 17:19:02 -04003054 ret = mv88e6xxx_phy_write(chip, 0x0, 0x1a, val | (1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003055 if (ret < 0)
3056 goto error;
3057
3058 /* Wait for temperature to stabilize */
3059 usleep_range(10000, 12000);
3060
Vivien Didelot9c938292016-08-15 17:19:02 -04003061 ret = mv88e6xxx_phy_read(chip, 0x0, 0x1a, &val);
3062 if (ret < 0)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003063 goto error;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003064
3065 /* Disable temperature sensor */
Vivien Didelot9c938292016-08-15 17:19:02 -04003066 ret = mv88e6xxx_phy_write(chip, 0x0, 0x1a, val & ~(1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003067 if (ret < 0)
3068 goto error;
3069
3070 *temp = ((val & 0x1f) - 5) * 5;
3071
3072error:
Vivien Didelot9c938292016-08-15 17:19:02 -04003073 mv88e6xxx_phy_write(chip, 0x0, 0x16, 0x0);
Vivien Didelotfad09c72016-06-21 12:28:20 -04003074 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003075 return ret;
3076}
3077
3078static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
3079{
Vivien Didelot04bed142016-08-31 18:06:13 -04003080 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003081 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003082 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003083 int ret;
3084
3085 *temp = 0;
3086
Vivien Didelot9c938292016-08-15 17:19:02 -04003087 mutex_lock(&chip->reg_lock);
3088 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 27, &val);
3089 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003090 if (ret < 0)
3091 return ret;
3092
Vivien Didelot9c938292016-08-15 17:19:02 -04003093 *temp = (val & 0xff) - 25;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003094
3095 return 0;
3096}
3097
Vivien Didelotf81ec902016-05-09 13:22:58 -04003098static int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003099{
Vivien Didelot04bed142016-08-31 18:06:13 -04003100 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn158bc062016-04-28 21:24:06 -04003101
Vivien Didelotfad09c72016-06-21 12:28:20 -04003102 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP))
Vivien Didelot6594f612016-05-09 13:22:42 -04003103 return -EOPNOTSUPP;
3104
Vivien Didelotfad09c72016-06-21 12:28:20 -04003105 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003106 return mv88e63xx_get_temp(ds, temp);
3107
3108 return mv88e61xx_get_temp(ds, temp);
3109}
3110
Vivien Didelotf81ec902016-05-09 13:22:58 -04003111static int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003112{
Vivien Didelot04bed142016-08-31 18:06:13 -04003113 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003114 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003115 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003116 int ret;
3117
Vivien Didelotfad09c72016-06-21 12:28:20 -04003118 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003119 return -EOPNOTSUPP;
3120
3121 *temp = 0;
3122
Vivien Didelot9c938292016-08-15 17:19:02 -04003123 mutex_lock(&chip->reg_lock);
3124 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3125 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003126 if (ret < 0)
3127 return ret;
3128
Vivien Didelot9c938292016-08-15 17:19:02 -04003129 *temp = (((val >> 8) & 0x1f) * 5) - 25;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003130
3131 return 0;
3132}
3133
Vivien Didelotf81ec902016-05-09 13:22:58 -04003134static int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003135{
Vivien Didelot04bed142016-08-31 18:06:13 -04003136 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003137 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003138 u16 val;
3139 int err;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003140
Vivien Didelotfad09c72016-06-21 12:28:20 -04003141 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003142 return -EOPNOTSUPP;
3143
Vivien Didelot9c938292016-08-15 17:19:02 -04003144 mutex_lock(&chip->reg_lock);
3145 err = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3146 if (err)
3147 goto unlock;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003148 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
Vivien Didelot9c938292016-08-15 17:19:02 -04003149 err = mv88e6xxx_phy_page_write(chip, phy, 6, 26,
3150 (val & 0xe0ff) | (temp << 8));
3151unlock:
3152 mutex_unlock(&chip->reg_lock);
3153
3154 return err;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003155}
3156
Vivien Didelotf81ec902016-05-09 13:22:58 -04003157static int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003158{
Vivien Didelot04bed142016-08-31 18:06:13 -04003159 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003160 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003161 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003162 int ret;
3163
Vivien Didelotfad09c72016-06-21 12:28:20 -04003164 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003165 return -EOPNOTSUPP;
3166
3167 *alarm = false;
3168
Vivien Didelot9c938292016-08-15 17:19:02 -04003169 mutex_lock(&chip->reg_lock);
3170 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3171 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003172 if (ret < 0)
3173 return ret;
3174
Vivien Didelot9c938292016-08-15 17:19:02 -04003175 *alarm = !!(val & 0x40);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003176
3177 return 0;
3178}
3179#endif /* CONFIG_NET_DSA_HWMON */
3180
Vivien Didelot855b1932016-07-20 18:18:35 -04003181static int mv88e6xxx_get_eeprom_len(struct dsa_switch *ds)
3182{
Vivien Didelot04bed142016-08-31 18:06:13 -04003183 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot855b1932016-07-20 18:18:35 -04003184
3185 return chip->eeprom_len;
3186}
3187
Vivien Didelot855b1932016-07-20 18:18:35 -04003188static int mv88e6xxx_get_eeprom(struct dsa_switch *ds,
3189 struct ethtool_eeprom *eeprom, u8 *data)
3190{
Vivien Didelot04bed142016-08-31 18:06:13 -04003191 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot855b1932016-07-20 18:18:35 -04003192 int err;
3193
3194 mutex_lock(&chip->reg_lock);
3195
3196 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16))
Vivien Didelotec561272016-09-02 14:45:33 -04003197 err = mv88e6xxx_g2_get_eeprom16(chip, eeprom, data);
Vivien Didelot855b1932016-07-20 18:18:35 -04003198 else
3199 err = -EOPNOTSUPP;
3200
3201 mutex_unlock(&chip->reg_lock);
3202
3203 if (err)
3204 return err;
3205
3206 eeprom->magic = 0xc3ec4951;
3207
3208 return 0;
3209}
3210
Vivien Didelot855b1932016-07-20 18:18:35 -04003211static int mv88e6xxx_set_eeprom(struct dsa_switch *ds,
3212 struct ethtool_eeprom *eeprom, u8 *data)
3213{
Vivien Didelot04bed142016-08-31 18:06:13 -04003214 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot855b1932016-07-20 18:18:35 -04003215 int err;
3216
3217 if (eeprom->magic != 0xc3ec4951)
3218 return -EINVAL;
3219
3220 mutex_lock(&chip->reg_lock);
3221
3222 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16))
Vivien Didelotec561272016-09-02 14:45:33 -04003223 err = mv88e6xxx_g2_set_eeprom16(chip, eeprom, data);
Vivien Didelot855b1932016-07-20 18:18:35 -04003224 else
3225 err = -EOPNOTSUPP;
3226
3227 mutex_unlock(&chip->reg_lock);
3228
3229 return err;
3230}
3231
Vivien Didelotf81ec902016-05-09 13:22:58 -04003232static const struct mv88e6xxx_info mv88e6xxx_table[] = {
3233 [MV88E6085] = {
3234 .prod_num = PORT_SWITCH_ID_PROD_NUM_6085,
3235 .family = MV88E6XXX_FAMILY_6097,
3236 .name = "Marvell 88E6085",
3237 .num_databases = 4096,
3238 .num_ports = 10,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003239 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003240 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003241 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003242 .flags = MV88E6XXX_FLAGS_FAMILY_6097,
3243 },
3244
3245 [MV88E6095] = {
3246 .prod_num = PORT_SWITCH_ID_PROD_NUM_6095,
3247 .family = MV88E6XXX_FAMILY_6095,
3248 .name = "Marvell 88E6095/88E6095F",
3249 .num_databases = 256,
3250 .num_ports = 11,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003251 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003252 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003253 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003254 .flags = MV88E6XXX_FLAGS_FAMILY_6095,
3255 },
3256
3257 [MV88E6123] = {
3258 .prod_num = PORT_SWITCH_ID_PROD_NUM_6123,
3259 .family = MV88E6XXX_FAMILY_6165,
3260 .name = "Marvell 88E6123",
3261 .num_databases = 4096,
3262 .num_ports = 3,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003263 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003264 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003265 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003266 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3267 },
3268
3269 [MV88E6131] = {
3270 .prod_num = PORT_SWITCH_ID_PROD_NUM_6131,
3271 .family = MV88E6XXX_FAMILY_6185,
3272 .name = "Marvell 88E6131",
3273 .num_databases = 256,
3274 .num_ports = 8,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003275 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003276 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003277 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003278 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3279 },
3280
3281 [MV88E6161] = {
3282 .prod_num = PORT_SWITCH_ID_PROD_NUM_6161,
3283 .family = MV88E6XXX_FAMILY_6165,
3284 .name = "Marvell 88E6161",
3285 .num_databases = 4096,
3286 .num_ports = 6,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003287 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003288 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003289 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003290 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3291 },
3292
3293 [MV88E6165] = {
3294 .prod_num = PORT_SWITCH_ID_PROD_NUM_6165,
3295 .family = MV88E6XXX_FAMILY_6165,
3296 .name = "Marvell 88E6165",
3297 .num_databases = 4096,
3298 .num_ports = 6,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003299 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003300 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003301 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003302 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3303 },
3304
3305 [MV88E6171] = {
3306 .prod_num = PORT_SWITCH_ID_PROD_NUM_6171,
3307 .family = MV88E6XXX_FAMILY_6351,
3308 .name = "Marvell 88E6171",
3309 .num_databases = 4096,
3310 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003311 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003312 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003313 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003314 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3315 },
3316
3317 [MV88E6172] = {
3318 .prod_num = PORT_SWITCH_ID_PROD_NUM_6172,
3319 .family = MV88E6XXX_FAMILY_6352,
3320 .name = "Marvell 88E6172",
3321 .num_databases = 4096,
3322 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003323 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003324 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003325 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003326 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3327 },
3328
3329 [MV88E6175] = {
3330 .prod_num = PORT_SWITCH_ID_PROD_NUM_6175,
3331 .family = MV88E6XXX_FAMILY_6351,
3332 .name = "Marvell 88E6175",
3333 .num_databases = 4096,
3334 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003335 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003336 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003337 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003338 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3339 },
3340
3341 [MV88E6176] = {
3342 .prod_num = PORT_SWITCH_ID_PROD_NUM_6176,
3343 .family = MV88E6XXX_FAMILY_6352,
3344 .name = "Marvell 88E6176",
3345 .num_databases = 4096,
3346 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003347 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003348 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003349 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003350 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3351 },
3352
3353 [MV88E6185] = {
3354 .prod_num = PORT_SWITCH_ID_PROD_NUM_6185,
3355 .family = MV88E6XXX_FAMILY_6185,
3356 .name = "Marvell 88E6185",
3357 .num_databases = 256,
3358 .num_ports = 10,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003359 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003360 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003361 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003362 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3363 },
3364
3365 [MV88E6240] = {
3366 .prod_num = PORT_SWITCH_ID_PROD_NUM_6240,
3367 .family = MV88E6XXX_FAMILY_6352,
3368 .name = "Marvell 88E6240",
3369 .num_databases = 4096,
3370 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003371 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003372 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003373 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003374 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3375 },
3376
3377 [MV88E6320] = {
3378 .prod_num = PORT_SWITCH_ID_PROD_NUM_6320,
3379 .family = MV88E6XXX_FAMILY_6320,
3380 .name = "Marvell 88E6320",
3381 .num_databases = 4096,
3382 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003383 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003384 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003385 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003386 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3387 },
3388
3389 [MV88E6321] = {
3390 .prod_num = PORT_SWITCH_ID_PROD_NUM_6321,
3391 .family = MV88E6XXX_FAMILY_6320,
3392 .name = "Marvell 88E6321",
3393 .num_databases = 4096,
3394 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003395 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003396 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003397 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003398 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3399 },
3400
3401 [MV88E6350] = {
3402 .prod_num = PORT_SWITCH_ID_PROD_NUM_6350,
3403 .family = MV88E6XXX_FAMILY_6351,
3404 .name = "Marvell 88E6350",
3405 .num_databases = 4096,
3406 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003407 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003408 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003409 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003410 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3411 },
3412
3413 [MV88E6351] = {
3414 .prod_num = PORT_SWITCH_ID_PROD_NUM_6351,
3415 .family = MV88E6XXX_FAMILY_6351,
3416 .name = "Marvell 88E6351",
3417 .num_databases = 4096,
3418 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003419 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003420 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003421 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003422 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3423 },
3424
3425 [MV88E6352] = {
3426 .prod_num = PORT_SWITCH_ID_PROD_NUM_6352,
3427 .family = MV88E6XXX_FAMILY_6352,
3428 .name = "Marvell 88E6352",
3429 .num_databases = 4096,
3430 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003431 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003432 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003433 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003434 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3435 },
3436};
3437
Vivien Didelot5f7c0362016-06-20 13:14:04 -04003438static const struct mv88e6xxx_info *mv88e6xxx_lookup_info(unsigned int prod_num)
Vivien Didelotb9b37712015-10-30 19:39:48 -04003439{
Vivien Didelota439c062016-04-17 13:23:58 -04003440 int i;
Vivien Didelotb9b37712015-10-30 19:39:48 -04003441
Vivien Didelot5f7c0362016-06-20 13:14:04 -04003442 for (i = 0; i < ARRAY_SIZE(mv88e6xxx_table); ++i)
3443 if (mv88e6xxx_table[i].prod_num == prod_num)
3444 return &mv88e6xxx_table[i];
Vivien Didelotb9b37712015-10-30 19:39:48 -04003445
Vivien Didelotb9b37712015-10-30 19:39:48 -04003446 return NULL;
3447}
3448
Vivien Didelotfad09c72016-06-21 12:28:20 -04003449static int mv88e6xxx_detect(struct mv88e6xxx_chip *chip)
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003450{
3451 const struct mv88e6xxx_info *info;
Vivien Didelot8f6345b2016-07-20 18:18:36 -04003452 unsigned int prod_num, rev;
3453 u16 id;
3454 int err;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003455
Vivien Didelot8f6345b2016-07-20 18:18:36 -04003456 mutex_lock(&chip->reg_lock);
3457 err = mv88e6xxx_port_read(chip, 0, PORT_SWITCH_ID, &id);
3458 mutex_unlock(&chip->reg_lock);
3459 if (err)
3460 return err;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003461
3462 prod_num = (id & 0xfff0) >> 4;
3463 rev = id & 0x000f;
3464
3465 info = mv88e6xxx_lookup_info(prod_num);
3466 if (!info)
3467 return -ENODEV;
3468
Vivien Didelotcaac8542016-06-20 13:14:09 -04003469 /* Update the compatible info with the probed one */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003470 chip->info = info;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003471
Vivien Didelotca070c12016-09-02 14:45:34 -04003472 err = mv88e6xxx_g2_require(chip);
3473 if (err)
3474 return err;
3475
Vivien Didelotfad09c72016-06-21 12:28:20 -04003476 dev_info(chip->dev, "switch 0x%x detected: %s, revision %u\n",
3477 chip->info->prod_num, chip->info->name, rev);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003478
3479 return 0;
3480}
3481
Vivien Didelotfad09c72016-06-21 12:28:20 -04003482static struct mv88e6xxx_chip *mv88e6xxx_alloc_chip(struct device *dev)
Vivien Didelot469d7292016-06-20 13:14:06 -04003483{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003484 struct mv88e6xxx_chip *chip;
Vivien Didelot469d7292016-06-20 13:14:06 -04003485
Vivien Didelotfad09c72016-06-21 12:28:20 -04003486 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
3487 if (!chip)
Vivien Didelot469d7292016-06-20 13:14:06 -04003488 return NULL;
3489
Vivien Didelotfad09c72016-06-21 12:28:20 -04003490 chip->dev = dev;
Vivien Didelot469d7292016-06-20 13:14:06 -04003491
Vivien Didelotfad09c72016-06-21 12:28:20 -04003492 mutex_init(&chip->reg_lock);
Vivien Didelot469d7292016-06-20 13:14:06 -04003493
Vivien Didelotfad09c72016-06-21 12:28:20 -04003494 return chip;
Vivien Didelot469d7292016-06-20 13:14:06 -04003495}
3496
Vivien Didelotec561272016-09-02 14:45:33 -04003497static const struct mv88e6xxx_ops mv88e6xxx_g2_smi_phy_ops = {
3498 .read = mv88e6xxx_g2_smi_phy_read,
3499 .write = mv88e6xxx_g2_smi_phy_write,
3500};
3501
Vivien Didelote57e5e72016-08-15 17:19:00 -04003502static const struct mv88e6xxx_ops mv88e6xxx_phy_ops = {
3503 .read = mv88e6xxx_read,
3504 .write = mv88e6xxx_write,
3505};
3506
3507static void mv88e6xxx_phy_init(struct mv88e6xxx_chip *chip)
3508{
3509 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_SMI_PHY)) {
3510 chip->phy_ops = &mv88e6xxx_g2_smi_phy_ops;
3511 } else if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU)) {
3512 chip->phy_ops = &mv88e6xxx_phy_ppu_ops;
3513 mv88e6xxx_ppu_state_init(chip);
3514 } else {
3515 chip->phy_ops = &mv88e6xxx_phy_ops;
3516 }
3517}
3518
Andrew Lunn930188c2016-08-22 16:01:03 +02003519static void mv88e6xxx_phy_destroy(struct mv88e6xxx_chip *chip)
3520{
3521 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU)) {
3522 mv88e6xxx_ppu_state_destroy(chip);
3523 }
3524}
3525
Vivien Didelotfad09c72016-06-21 12:28:20 -04003526static int mv88e6xxx_smi_init(struct mv88e6xxx_chip *chip,
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003527 struct mii_bus *bus, int sw_addr)
3528{
3529 /* ADDR[0] pin is unavailable externally and considered zero */
3530 if (sw_addr & 0x1)
3531 return -EINVAL;
3532
Vivien Didelot914b32f2016-06-20 13:14:11 -04003533 if (sw_addr == 0)
Vivien Didelotfad09c72016-06-21 12:28:20 -04003534 chip->smi_ops = &mv88e6xxx_smi_single_chip_ops;
Vivien Didelota0ffff22016-08-15 17:18:58 -04003535 else if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_MULTI_CHIP))
Vivien Didelotfad09c72016-06-21 12:28:20 -04003536 chip->smi_ops = &mv88e6xxx_smi_multi_chip_ops;
Vivien Didelot914b32f2016-06-20 13:14:11 -04003537 else
3538 return -EINVAL;
3539
Vivien Didelotfad09c72016-06-21 12:28:20 -04003540 chip->bus = bus;
3541 chip->sw_addr = sw_addr;
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003542
3543 return 0;
3544}
3545
Andrew Lunn7b314362016-08-22 16:01:01 +02003546static enum dsa_tag_protocol mv88e6xxx_get_tag_protocol(struct dsa_switch *ds)
3547{
Vivien Didelot04bed142016-08-31 18:06:13 -04003548 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02003549
3550 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA))
3551 return DSA_TAG_PROTO_EDSA;
3552
3553 return DSA_TAG_PROTO_DSA;
Andrew Lunn7b314362016-08-22 16:01:01 +02003554}
3555
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02003556static const char *mv88e6xxx_drv_probe(struct device *dsa_dev,
3557 struct device *host_dev, int sw_addr,
3558 void **priv)
Andrew Lunna77d43f2016-04-13 02:40:42 +02003559{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003560 struct mv88e6xxx_chip *chip;
Vivien Didelota439c062016-04-17 13:23:58 -04003561 struct mii_bus *bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003562 int err;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003563
Vivien Didelota439c062016-04-17 13:23:58 -04003564 bus = dsa_host_dev_to_mii_bus(host_dev);
Andrew Lunnc1569132016-04-13 02:40:45 +02003565 if (!bus)
3566 return NULL;
3567
Vivien Didelotfad09c72016-06-21 12:28:20 -04003568 chip = mv88e6xxx_alloc_chip(dsa_dev);
3569 if (!chip)
Vivien Didelot469d7292016-06-20 13:14:06 -04003570 return NULL;
3571
Vivien Didelotcaac8542016-06-20 13:14:09 -04003572 /* Legacy SMI probing will only support chips similar to 88E6085 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003573 chip->info = &mv88e6xxx_table[MV88E6085];
Vivien Didelotcaac8542016-06-20 13:14:09 -04003574
Vivien Didelotfad09c72016-06-21 12:28:20 -04003575 err = mv88e6xxx_smi_init(chip, bus, sw_addr);
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003576 if (err)
3577 goto free;
3578
Vivien Didelotfad09c72016-06-21 12:28:20 -04003579 err = mv88e6xxx_detect(chip);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003580 if (err)
Vivien Didelot469d7292016-06-20 13:14:06 -04003581 goto free;
Vivien Didelota439c062016-04-17 13:23:58 -04003582
Vivien Didelote57e5e72016-08-15 17:19:00 -04003583 mv88e6xxx_phy_init(chip);
3584
Vivien Didelotfad09c72016-06-21 12:28:20 -04003585 err = mv88e6xxx_mdio_register(chip, NULL);
Andrew Lunnb516d452016-06-04 21:17:06 +02003586 if (err)
Vivien Didelot469d7292016-06-20 13:14:06 -04003587 goto free;
Andrew Lunnb516d452016-06-04 21:17:06 +02003588
Vivien Didelotfad09c72016-06-21 12:28:20 -04003589 *priv = chip;
Vivien Didelota439c062016-04-17 13:23:58 -04003590
Vivien Didelotfad09c72016-06-21 12:28:20 -04003591 return chip->info->name;
Vivien Didelot469d7292016-06-20 13:14:06 -04003592free:
Vivien Didelotfad09c72016-06-21 12:28:20 -04003593 devm_kfree(dsa_dev, chip);
Vivien Didelot469d7292016-06-20 13:14:06 -04003594
3595 return NULL;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003596}
3597
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003598static int mv88e6xxx_port_mdb_prepare(struct dsa_switch *ds, int port,
3599 const struct switchdev_obj_port_mdb *mdb,
3600 struct switchdev_trans *trans)
3601{
3602 /* We don't need any dynamic resource from the kernel (yet),
3603 * so skip the prepare phase.
3604 */
3605
3606 return 0;
3607}
3608
3609static void mv88e6xxx_port_mdb_add(struct dsa_switch *ds, int port,
3610 const struct switchdev_obj_port_mdb *mdb,
3611 struct switchdev_trans *trans)
3612{
Vivien Didelot04bed142016-08-31 18:06:13 -04003613 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003614
3615 mutex_lock(&chip->reg_lock);
3616 if (mv88e6xxx_port_db_load_purge(chip, port, mdb->addr, mdb->vid,
3617 GLOBAL_ATU_DATA_STATE_MC_STATIC))
3618 netdev_err(ds->ports[port].netdev, "failed to load multicast MAC address\n");
3619 mutex_unlock(&chip->reg_lock);
3620}
3621
3622static int mv88e6xxx_port_mdb_del(struct dsa_switch *ds, int port,
3623 const struct switchdev_obj_port_mdb *mdb)
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_load_purge(chip, port, mdb->addr, mdb->vid,
3630 GLOBAL_ATU_DATA_STATE_UNUSED);
3631 mutex_unlock(&chip->reg_lock);
3632
3633 return err;
3634}
3635
3636static int mv88e6xxx_port_mdb_dump(struct dsa_switch *ds, int port,
3637 struct switchdev_obj_port_mdb *mdb,
3638 int (*cb)(struct switchdev_obj *obj))
3639{
Vivien Didelot04bed142016-08-31 18:06:13 -04003640 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003641 int err;
3642
3643 mutex_lock(&chip->reg_lock);
3644 err = mv88e6xxx_port_db_dump(chip, port, &mdb->obj, cb);
3645 mutex_unlock(&chip->reg_lock);
3646
3647 return err;
3648}
3649
Vivien Didelot9d490b42016-08-23 12:38:56 -04003650static struct dsa_switch_ops mv88e6xxx_switch_ops = {
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02003651 .probe = mv88e6xxx_drv_probe,
Andrew Lunn7b314362016-08-22 16:01:01 +02003652 .get_tag_protocol = mv88e6xxx_get_tag_protocol,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003653 .setup = mv88e6xxx_setup,
3654 .set_addr = mv88e6xxx_set_addr,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003655 .adjust_link = mv88e6xxx_adjust_link,
3656 .get_strings = mv88e6xxx_get_strings,
3657 .get_ethtool_stats = mv88e6xxx_get_ethtool_stats,
3658 .get_sset_count = mv88e6xxx_get_sset_count,
3659 .set_eee = mv88e6xxx_set_eee,
3660 .get_eee = mv88e6xxx_get_eee,
3661#ifdef CONFIG_NET_DSA_HWMON
3662 .get_temp = mv88e6xxx_get_temp,
3663 .get_temp_limit = mv88e6xxx_get_temp_limit,
3664 .set_temp_limit = mv88e6xxx_set_temp_limit,
3665 .get_temp_alarm = mv88e6xxx_get_temp_alarm,
3666#endif
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003667 .get_eeprom_len = mv88e6xxx_get_eeprom_len,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003668 .get_eeprom = mv88e6xxx_get_eeprom,
3669 .set_eeprom = mv88e6xxx_set_eeprom,
3670 .get_regs_len = mv88e6xxx_get_regs_len,
3671 .get_regs = mv88e6xxx_get_regs,
Vivien Didelot2cfcd962016-07-18 20:45:40 -04003672 .set_ageing_time = mv88e6xxx_set_ageing_time,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003673 .port_bridge_join = mv88e6xxx_port_bridge_join,
3674 .port_bridge_leave = mv88e6xxx_port_bridge_leave,
3675 .port_stp_state_set = mv88e6xxx_port_stp_state_set,
Vivien Didelot749efcb2016-09-22 16:49:24 -04003676 .port_fast_age = mv88e6xxx_port_fast_age,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003677 .port_vlan_filtering = mv88e6xxx_port_vlan_filtering,
3678 .port_vlan_prepare = mv88e6xxx_port_vlan_prepare,
3679 .port_vlan_add = mv88e6xxx_port_vlan_add,
3680 .port_vlan_del = mv88e6xxx_port_vlan_del,
3681 .port_vlan_dump = mv88e6xxx_port_vlan_dump,
3682 .port_fdb_prepare = mv88e6xxx_port_fdb_prepare,
3683 .port_fdb_add = mv88e6xxx_port_fdb_add,
3684 .port_fdb_del = mv88e6xxx_port_fdb_del,
3685 .port_fdb_dump = mv88e6xxx_port_fdb_dump,
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003686 .port_mdb_prepare = mv88e6xxx_port_mdb_prepare,
3687 .port_mdb_add = mv88e6xxx_port_mdb_add,
3688 .port_mdb_del = mv88e6xxx_port_mdb_del,
3689 .port_mdb_dump = mv88e6xxx_port_mdb_dump,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003690};
3691
Vivien Didelotfad09c72016-06-21 12:28:20 -04003692static int mv88e6xxx_register_switch(struct mv88e6xxx_chip *chip,
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003693 struct device_node *np)
3694{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003695 struct device *dev = chip->dev;
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003696 struct dsa_switch *ds;
3697
3698 ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
3699 if (!ds)
3700 return -ENOMEM;
3701
3702 ds->dev = dev;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003703 ds->priv = chip;
Vivien Didelot9d490b42016-08-23 12:38:56 -04003704 ds->ops = &mv88e6xxx_switch_ops;
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003705
3706 dev_set_drvdata(dev, ds);
3707
3708 return dsa_register_switch(ds, np);
3709}
3710
Vivien Didelotfad09c72016-06-21 12:28:20 -04003711static void mv88e6xxx_unregister_switch(struct mv88e6xxx_chip *chip)
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003712{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003713 dsa_unregister_switch(chip->ds);
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003714}
3715
Vivien Didelot57d32312016-06-20 13:13:58 -04003716static int mv88e6xxx_probe(struct mdio_device *mdiodev)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003717{
3718 struct device *dev = &mdiodev->dev;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003719 struct device_node *np = dev->of_node;
Vivien Didelotcaac8542016-06-20 13:14:09 -04003720 const struct mv88e6xxx_info *compat_info;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003721 struct mv88e6xxx_chip *chip;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003722 u32 eeprom_len;
Andrew Lunn52638f72016-05-10 23:27:22 +02003723 int err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003724
Vivien Didelotcaac8542016-06-20 13:14:09 -04003725 compat_info = of_device_get_match_data(dev);
3726 if (!compat_info)
3727 return -EINVAL;
3728
Vivien Didelotfad09c72016-06-21 12:28:20 -04003729 chip = mv88e6xxx_alloc_chip(dev);
3730 if (!chip)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003731 return -ENOMEM;
3732
Vivien Didelotfad09c72016-06-21 12:28:20 -04003733 chip->info = compat_info;
Vivien Didelotcaac8542016-06-20 13:14:09 -04003734
Vivien Didelotfad09c72016-06-21 12:28:20 -04003735 err = mv88e6xxx_smi_init(chip, mdiodev->bus, mdiodev->addr);
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003736 if (err)
3737 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003738
Vivien Didelotfad09c72016-06-21 12:28:20 -04003739 err = mv88e6xxx_detect(chip);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003740 if (err)
3741 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003742
Vivien Didelote57e5e72016-08-15 17:19:00 -04003743 mv88e6xxx_phy_init(chip);
3744
Vivien Didelotfad09c72016-06-21 12:28:20 -04003745 chip->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_ASIS);
3746 if (IS_ERR(chip->reset))
3747 return PTR_ERR(chip->reset);
Andrew Lunn52638f72016-05-10 23:27:22 +02003748
Vivien Didelot855b1932016-07-20 18:18:35 -04003749 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16) &&
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003750 !of_property_read_u32(np, "eeprom-length", &eeprom_len))
Vivien Didelotfad09c72016-06-21 12:28:20 -04003751 chip->eeprom_len = eeprom_len;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003752
Vivien Didelotfad09c72016-06-21 12:28:20 -04003753 err = mv88e6xxx_mdio_register(chip, np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003754 if (err)
3755 return err;
3756
Vivien Didelotfad09c72016-06-21 12:28:20 -04003757 err = mv88e6xxx_register_switch(chip, np);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02003758 if (err) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04003759 mv88e6xxx_mdio_unregister(chip);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02003760 return err;
3761 }
3762
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003763 return 0;
3764}
3765
3766static void mv88e6xxx_remove(struct mdio_device *mdiodev)
3767{
3768 struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev);
Vivien Didelot04bed142016-08-31 18:06:13 -04003769 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003770
Andrew Lunn930188c2016-08-22 16:01:03 +02003771 mv88e6xxx_phy_destroy(chip);
Vivien Didelotfad09c72016-06-21 12:28:20 -04003772 mv88e6xxx_unregister_switch(chip);
3773 mv88e6xxx_mdio_unregister(chip);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003774}
3775
3776static const struct of_device_id mv88e6xxx_of_match[] = {
Vivien Didelotcaac8542016-06-20 13:14:09 -04003777 {
3778 .compatible = "marvell,mv88e6085",
3779 .data = &mv88e6xxx_table[MV88E6085],
3780 },
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003781 { /* sentinel */ },
3782};
3783
3784MODULE_DEVICE_TABLE(of, mv88e6xxx_of_match);
3785
3786static struct mdio_driver mv88e6xxx_driver = {
3787 .probe = mv88e6xxx_probe,
3788 .remove = mv88e6xxx_remove,
3789 .mdiodrv.driver = {
3790 .name = "mv88e6085",
3791 .of_match_table = mv88e6xxx_of_match,
3792 },
3793};
3794
Ben Hutchings98e67302011-11-25 14:36:19 +00003795static int __init mv88e6xxx_init(void)
3796{
Vivien Didelot9d490b42016-08-23 12:38:56 -04003797 register_switch_driver(&mv88e6xxx_switch_ops);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003798 return mdio_driver_register(&mv88e6xxx_driver);
Ben Hutchings98e67302011-11-25 14:36:19 +00003799}
3800module_init(mv88e6xxx_init);
3801
3802static void __exit mv88e6xxx_cleanup(void)
3803{
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003804 mdio_driver_unregister(&mv88e6xxx_driver);
Vivien Didelot9d490b42016-08-23 12:38:56 -04003805 unregister_switch_driver(&mv88e6xxx_switch_ops);
Ben Hutchings98e67302011-11-25 14:36:19 +00003806}
3807module_exit(mv88e6xxx_cleanup);
Ben Hutchings3d825ed2011-11-25 14:37:16 +00003808
3809MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
3810MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
3811MODULE_LICENSE("GPL");