blob: 83a37693a8db19e942dc1b79f9b47551e51596c2 [file] [log] [blame]
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001/*
Vivien Didelot0d3cd4b2016-06-21 12:28:19 -04002 * Marvell 88e6xxx Ethernet switch single-chip support
3 *
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00004 * Copyright (c) 2008 Marvell Semiconductor
5 *
Vivien Didelotb8fee952015-08-13 12:52:19 -04006 * Copyright (c) 2015 CMC Electronics, Inc.
7 * Added support for VLAN Table Unit operations
8 *
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02009 * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch>
10 *
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 */
16
Barry Grussling19b2f972013-01-08 16:05:54 +000017#include <linux/delay.h>
Guenter Roeckdefb05b2015-03-26 18:36:38 -070018#include <linux/etherdevice.h>
Andrew Lunndea87022015-08-31 15:56:47 +020019#include <linux/ethtool.h>
Guenter Roeckfacd95b2015-03-26 18:36:35 -070020#include <linux/if_bridge.h>
Barry Grussling19b2f972013-01-08 16:05:54 +000021#include <linux/jiffies.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000022#include <linux/list.h>
Andrew Lunn14c7b3c2016-05-10 23:27:21 +020023#include <linux/mdio.h>
Paul Gortmaker2bbba272012-01-24 10:41:40 +000024#include <linux/module.h>
Vivien Didelotcaac8542016-06-20 13:14:09 -040025#include <linux/of_device.h>
Andrew Lunnb516d452016-06-04 21:17:06 +020026#include <linux/of_mdio.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000027#include <linux/netdevice.h>
Andrew Lunnc8c1b39a2015-11-20 03:56:24 +010028#include <linux/gpio/consumer.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000029#include <linux/phy.h>
Ben Hutchingsc8f0b862011-11-27 17:06:08 +000030#include <net/dsa.h>
Vivien Didelot1f36faf2015-10-08 11:35:13 -040031#include <net/switchdev.h>
Vivien Didelotec561272016-09-02 14:45:33 -040032
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000033#include "mv88e6xxx.h"
Vivien Didelota935c052016-09-29 12:21:53 -040034#include "global1.h"
Vivien Didelotec561272016-09-02 14:45:33 -040035#include "global2.h"
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000036
Vivien Didelotfad09c72016-06-21 12:28:20 -040037static void assert_reg_lock(struct mv88e6xxx_chip *chip)
Vivien Didelot3996a4f2015-10-30 18:56:45 -040038{
Vivien Didelotfad09c72016-06-21 12:28:20 -040039 if (unlikely(!mutex_is_locked(&chip->reg_lock))) {
40 dev_err(chip->dev, "Switch registers lock not held!\n");
Vivien Didelot3996a4f2015-10-30 18:56:45 -040041 dump_stack();
42 }
43}
44
Vivien Didelot914b32f2016-06-20 13:14:11 -040045/* The switch ADDR[4:1] configuration pins define the chip SMI device address
46 * (ADDR[0] is always zero, thus only even SMI addresses can be strapped).
47 *
48 * When ADDR is all zero, the chip uses Single-chip Addressing Mode, assuming it
49 * is the only device connected to the SMI master. In this mode it responds to
50 * all 32 possible SMI addresses, and thus maps directly the internal devices.
51 *
52 * When ADDR is non-zero, the chip uses Multi-chip Addressing Mode, allowing
53 * multiple devices to share the SMI interface. In this mode it responds to only
54 * 2 registers, used to indirectly access the internal SMI devices.
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000055 */
Vivien Didelot914b32f2016-06-20 13:14:11 -040056
Vivien Didelotfad09c72016-06-21 12:28:20 -040057static int mv88e6xxx_smi_read(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -040058 int addr, int reg, u16 *val)
59{
Vivien Didelotfad09c72016-06-21 12:28:20 -040060 if (!chip->smi_ops)
Vivien Didelot914b32f2016-06-20 13:14:11 -040061 return -EOPNOTSUPP;
62
Vivien Didelotfad09c72016-06-21 12:28:20 -040063 return chip->smi_ops->read(chip, addr, reg, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -040064}
65
Vivien Didelotfad09c72016-06-21 12:28:20 -040066static int mv88e6xxx_smi_write(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -040067 int addr, int reg, u16 val)
68{
Vivien Didelotfad09c72016-06-21 12:28:20 -040069 if (!chip->smi_ops)
Vivien Didelot914b32f2016-06-20 13:14:11 -040070 return -EOPNOTSUPP;
71
Vivien Didelotfad09c72016-06-21 12:28:20 -040072 return chip->smi_ops->write(chip, addr, reg, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -040073}
74
Vivien Didelotfad09c72016-06-21 12:28:20 -040075static int mv88e6xxx_smi_single_chip_read(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -040076 int addr, int reg, u16 *val)
77{
78 int ret;
79
Vivien Didelotfad09c72016-06-21 12:28:20 -040080 ret = mdiobus_read_nested(chip->bus, addr, reg);
Vivien Didelot914b32f2016-06-20 13:14:11 -040081 if (ret < 0)
82 return ret;
83
84 *val = ret & 0xffff;
85
86 return 0;
87}
88
Vivien Didelotfad09c72016-06-21 12:28:20 -040089static int mv88e6xxx_smi_single_chip_write(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -040090 int addr, int reg, u16 val)
91{
92 int ret;
93
Vivien Didelotfad09c72016-06-21 12:28:20 -040094 ret = mdiobus_write_nested(chip->bus, addr, reg, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -040095 if (ret < 0)
96 return ret;
97
98 return 0;
99}
100
Vivien Didelotc08026a2016-09-29 12:21:59 -0400101static const struct mv88e6xxx_bus_ops mv88e6xxx_smi_single_chip_ops = {
Vivien Didelot914b32f2016-06-20 13:14:11 -0400102 .read = mv88e6xxx_smi_single_chip_read,
103 .write = mv88e6xxx_smi_single_chip_write,
104};
105
Vivien Didelotfad09c72016-06-21 12:28:20 -0400106static int mv88e6xxx_smi_multi_chip_wait(struct mv88e6xxx_chip *chip)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000107{
108 int ret;
109 int i;
110
111 for (i = 0; i < 16; i++) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400112 ret = mdiobus_read_nested(chip->bus, chip->sw_addr, SMI_CMD);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000113 if (ret < 0)
114 return ret;
115
Andrew Lunncca8b132015-04-02 04:06:39 +0200116 if ((ret & SMI_CMD_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000117 return 0;
118 }
119
120 return -ETIMEDOUT;
121}
122
Vivien Didelotfad09c72016-06-21 12:28:20 -0400123static int mv88e6xxx_smi_multi_chip_read(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -0400124 int addr, int reg, u16 *val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000125{
126 int ret;
127
Barry Grussling3675c8d2013-01-08 16:05:53 +0000128 /* Wait for the bus to become free. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400129 ret = mv88e6xxx_smi_multi_chip_wait(chip);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000130 if (ret < 0)
131 return ret;
132
Barry Grussling3675c8d2013-01-08 16:05:53 +0000133 /* Transmit the read command. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400134 ret = mdiobus_write_nested(chip->bus, chip->sw_addr, SMI_CMD,
Neil Armstrong6e899e62015-10-22 10:37:53 +0200135 SMI_CMD_OP_22_READ | (addr << 5) | reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000136 if (ret < 0)
137 return ret;
138
Barry Grussling3675c8d2013-01-08 16:05:53 +0000139 /* Wait for the read command to complete. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400140 ret = mv88e6xxx_smi_multi_chip_wait(chip);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000141 if (ret < 0)
142 return ret;
143
Barry Grussling3675c8d2013-01-08 16:05:53 +0000144 /* Read the data. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400145 ret = mdiobus_read_nested(chip->bus, chip->sw_addr, SMI_DATA);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000146 if (ret < 0)
147 return ret;
148
Vivien Didelot914b32f2016-06-20 13:14:11 -0400149 *val = ret & 0xffff;
150
151 return 0;
152}
153
Vivien Didelotfad09c72016-06-21 12:28:20 -0400154static int mv88e6xxx_smi_multi_chip_write(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -0400155 int addr, int reg, u16 val)
156{
157 int ret;
158
159 /* Wait for the bus to become free. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400160 ret = mv88e6xxx_smi_multi_chip_wait(chip);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400161 if (ret < 0)
162 return ret;
163
164 /* Transmit the data to write. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400165 ret = mdiobus_write_nested(chip->bus, chip->sw_addr, SMI_DATA, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400166 if (ret < 0)
167 return ret;
168
169 /* Transmit the write command. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400170 ret = mdiobus_write_nested(chip->bus, chip->sw_addr, SMI_CMD,
Vivien Didelot914b32f2016-06-20 13:14:11 -0400171 SMI_CMD_OP_22_WRITE | (addr << 5) | reg);
172 if (ret < 0)
173 return ret;
174
175 /* Wait for the write command to complete. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400176 ret = mv88e6xxx_smi_multi_chip_wait(chip);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400177 if (ret < 0)
178 return ret;
179
180 return 0;
181}
182
Vivien Didelotc08026a2016-09-29 12:21:59 -0400183static const struct mv88e6xxx_bus_ops mv88e6xxx_smi_multi_chip_ops = {
Vivien Didelot914b32f2016-06-20 13:14:11 -0400184 .read = mv88e6xxx_smi_multi_chip_read,
185 .write = mv88e6xxx_smi_multi_chip_write,
186};
187
Vivien Didelotec561272016-09-02 14:45:33 -0400188int mv88e6xxx_read(struct mv88e6xxx_chip *chip, int addr, int reg, u16 *val)
Vivien Didelot914b32f2016-06-20 13:14:11 -0400189{
190 int err;
191
Vivien Didelotfad09c72016-06-21 12:28:20 -0400192 assert_reg_lock(chip);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400193
Vivien Didelotfad09c72016-06-21 12:28:20 -0400194 err = mv88e6xxx_smi_read(chip, addr, reg, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400195 if (err)
196 return err;
197
Vivien Didelotfad09c72016-06-21 12:28:20 -0400198 dev_dbg(chip->dev, "<- addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
Vivien Didelot914b32f2016-06-20 13:14:11 -0400199 addr, reg, *val);
200
201 return 0;
202}
203
Vivien Didelotec561272016-09-02 14:45:33 -0400204int mv88e6xxx_write(struct mv88e6xxx_chip *chip, int addr, int reg, u16 val)
Vivien Didelot914b32f2016-06-20 13:14:11 -0400205{
206 int err;
207
Vivien Didelotfad09c72016-06-21 12:28:20 -0400208 assert_reg_lock(chip);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400209
Vivien Didelotfad09c72016-06-21 12:28:20 -0400210 err = mv88e6xxx_smi_write(chip, addr, reg, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400211 if (err)
212 return err;
213
Vivien Didelotfad09c72016-06-21 12:28:20 -0400214 dev_dbg(chip->dev, "-> addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
Vivien Didelot914b32f2016-06-20 13:14:11 -0400215 addr, reg, val);
216
217 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000218}
219
Wei Yongjunb3f5bf62016-09-25 15:43:02 +0000220static int mv88e6xxx_port_read(struct mv88e6xxx_chip *chip, int port, int reg,
221 u16 *val)
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200222{
223 int addr = chip->info->port_base_addr + port;
224
225 return mv88e6xxx_read(chip, addr, reg, val);
226}
227
Wei Yongjunb3f5bf62016-09-25 15:43:02 +0000228static int mv88e6xxx_port_write(struct mv88e6xxx_chip *chip, int port, int reg,
229 u16 val)
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200230{
231 int addr = chip->info->port_base_addr + port;
232
233 return mv88e6xxx_write(chip, addr, reg, val);
234}
235
Vivien Didelote57e5e72016-08-15 17:19:00 -0400236static int mv88e6xxx_phy_read(struct mv88e6xxx_chip *chip, int phy,
237 int reg, u16 *val)
238{
239 int addr = phy; /* PHY devices addresses start at 0x0 */
240
Vivien Didelotb3469dd2016-09-29 12:22:00 -0400241 if (!chip->info->ops->phy_read)
Vivien Didelote57e5e72016-08-15 17:19:00 -0400242 return -EOPNOTSUPP;
243
Vivien Didelotb3469dd2016-09-29 12:22:00 -0400244 return chip->info->ops->phy_read(chip, addr, reg, val);
Vivien Didelote57e5e72016-08-15 17:19:00 -0400245}
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
Vivien Didelotb3469dd2016-09-29 12:22:00 -0400252 if (!chip->info->ops->phy_write)
Vivien Didelote57e5e72016-08-15 17:19:00 -0400253 return -EOPNOTSUPP;
254
Vivien Didelotb3469dd2016-09-29 12:22:00 -0400255 return chip->info->ops->phy_write(chip, addr, reg, val);
Vivien Didelote57e5e72016-08-15 17:19:00 -0400256}
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 Didelotfad09c72016-06-21 12:28:20 -0400518static bool mv88e6xxx_6065_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200519{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400520 return chip->info->family == MV88E6XXX_FAMILY_6065;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200521}
522
Vivien Didelotfad09c72016-06-21 12:28:20 -0400523static bool mv88e6xxx_6095_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_6095;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200526}
527
Vivien Didelotfad09c72016-06-21 12:28:20 -0400528static bool mv88e6xxx_6097_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_6097;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200531}
532
Vivien Didelotfad09c72016-06-21 12:28:20 -0400533static bool mv88e6xxx_6165_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_6165;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200536}
537
Vivien Didelotfad09c72016-06-21 12:28:20 -0400538static bool mv88e6xxx_6185_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_6185;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200541}
542
Vivien Didelotfad09c72016-06-21 12:28:20 -0400543static bool mv88e6xxx_6320_family(struct mv88e6xxx_chip *chip)
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700544{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400545 return chip->info->family == MV88E6XXX_FAMILY_6320;
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700546}
547
Vivien Didelotfad09c72016-06-21 12:28:20 -0400548static bool mv88e6xxx_6351_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200549{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400550 return chip->info->family == MV88E6XXX_FAMILY_6351;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200551}
552
Vivien Didelotfad09c72016-06-21 12:28:20 -0400553static bool mv88e6xxx_6352_family(struct mv88e6xxx_chip *chip)
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200554{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400555 return chip->info->family == MV88E6XXX_FAMILY_6352;
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200556}
557
Andrew Lunndea87022015-08-31 15:56:47 +0200558/* We expect the switch to perform auto negotiation if there is a real
559 * phy. However, in the case of a fixed link phy, we force the port
560 * settings from the fixed link settings.
561 */
Vivien Didelotf81ec902016-05-09 13:22:58 -0400562static void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port,
563 struct phy_device *phydev)
Andrew Lunndea87022015-08-31 15:56:47 +0200564{
Vivien Didelot04bed142016-08-31 18:06:13 -0400565 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200566 u16 reg;
567 int err;
Andrew Lunndea87022015-08-31 15:56:47 +0200568
569 if (!phy_is_pseudo_fixed_link(phydev))
570 return;
571
Vivien Didelotfad09c72016-06-21 12:28:20 -0400572 mutex_lock(&chip->reg_lock);
Andrew Lunndea87022015-08-31 15:56:47 +0200573
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200574 err = mv88e6xxx_port_read(chip, port, PORT_PCS_CTRL, &reg);
575 if (err)
Andrew Lunndea87022015-08-31 15:56:47 +0200576 goto out;
577
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200578 reg &= ~(PORT_PCS_CTRL_LINK_UP |
579 PORT_PCS_CTRL_FORCE_LINK |
580 PORT_PCS_CTRL_DUPLEX_FULL |
581 PORT_PCS_CTRL_FORCE_DUPLEX |
582 PORT_PCS_CTRL_UNFORCED);
Andrew Lunndea87022015-08-31 15:56:47 +0200583
584 reg |= PORT_PCS_CTRL_FORCE_LINK;
585 if (phydev->link)
Vivien Didelot57d32312016-06-20 13:13:58 -0400586 reg |= PORT_PCS_CTRL_LINK_UP;
Andrew Lunndea87022015-08-31 15:56:47 +0200587
Vivien Didelotfad09c72016-06-21 12:28:20 -0400588 if (mv88e6xxx_6065_family(chip) && phydev->speed > SPEED_100)
Andrew Lunndea87022015-08-31 15:56:47 +0200589 goto out;
590
591 switch (phydev->speed) {
592 case SPEED_1000:
593 reg |= PORT_PCS_CTRL_1000;
594 break;
595 case SPEED_100:
596 reg |= PORT_PCS_CTRL_100;
597 break;
598 case SPEED_10:
599 reg |= PORT_PCS_CTRL_10;
600 break;
601 default:
602 pr_info("Unknown speed");
603 goto out;
604 }
605
606 reg |= PORT_PCS_CTRL_FORCE_DUPLEX;
607 if (phydev->duplex == DUPLEX_FULL)
608 reg |= PORT_PCS_CTRL_DUPLEX_FULL;
609
Vivien Didelotfad09c72016-06-21 12:28:20 -0400610 if ((mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip)) &&
Vivien Didelot370b4ff2016-09-29 12:21:57 -0400611 (port >= mv88e6xxx_num_ports(chip) - 2)) {
Andrew Lunne7e72ac2015-08-31 15:56:51 +0200612 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
613 reg |= PORT_PCS_CTRL_RGMII_DELAY_RXCLK;
614 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
615 reg |= PORT_PCS_CTRL_RGMII_DELAY_TXCLK;
616 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
617 reg |= (PORT_PCS_CTRL_RGMII_DELAY_RXCLK |
618 PORT_PCS_CTRL_RGMII_DELAY_TXCLK);
619 }
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200620 mv88e6xxx_port_write(chip, port, PORT_PCS_CTRL, reg);
Andrew Lunndea87022015-08-31 15:56:47 +0200621
622out:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400623 mutex_unlock(&chip->reg_lock);
Andrew Lunndea87022015-08-31 15:56:47 +0200624}
625
Vivien Didelotfad09c72016-06-21 12:28:20 -0400626static int _mv88e6xxx_stats_wait(struct mv88e6xxx_chip *chip)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000627{
Vivien Didelota935c052016-09-29 12:21:53 -0400628 u16 val;
629 int i, err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000630
631 for (i = 0; i < 10; i++) {
Vivien Didelota935c052016-09-29 12:21:53 -0400632 err = mv88e6xxx_g1_read(chip, GLOBAL_STATS_OP, &val);
633 if ((val & GLOBAL_STATS_OP_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000634 return 0;
635 }
636
637 return -ETIMEDOUT;
638}
639
Vivien Didelotfad09c72016-06-21 12:28:20 -0400640static int _mv88e6xxx_stats_snapshot(struct mv88e6xxx_chip *chip, int port)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000641{
Vivien Didelota935c052016-09-29 12:21:53 -0400642 int err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000643
Vivien Didelotfad09c72016-06-21 12:28:20 -0400644 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200645 port = (port + 1) << 5;
646
Barry Grussling3675c8d2013-01-08 16:05:53 +0000647 /* Snapshot the hardware statistics counters for this port. */
Vivien Didelota935c052016-09-29 12:21:53 -0400648 err = mv88e6xxx_g1_write(chip, GLOBAL_STATS_OP,
649 GLOBAL_STATS_OP_CAPTURE_PORT |
650 GLOBAL_STATS_OP_HIST_RX_TX | port);
651 if (err)
652 return err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000653
Barry Grussling3675c8d2013-01-08 16:05:53 +0000654 /* Wait for the snapshotting to complete. */
Vivien Didelota935c052016-09-29 12:21:53 -0400655 return _mv88e6xxx_stats_wait(chip);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000656}
657
Vivien Didelotfad09c72016-06-21 12:28:20 -0400658static void _mv88e6xxx_stats_read(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -0400659 int stat, u32 *val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000660{
Vivien Didelota935c052016-09-29 12:21:53 -0400661 u32 value;
662 u16 reg;
663 int err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000664
665 *val = 0;
666
Vivien Didelota935c052016-09-29 12:21:53 -0400667 err = mv88e6xxx_g1_write(chip, GLOBAL_STATS_OP,
668 GLOBAL_STATS_OP_READ_CAPTURED |
669 GLOBAL_STATS_OP_HIST_RX_TX | stat);
670 if (err)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000671 return;
672
Vivien Didelota935c052016-09-29 12:21:53 -0400673 err = _mv88e6xxx_stats_wait(chip);
674 if (err)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000675 return;
676
Vivien Didelota935c052016-09-29 12:21:53 -0400677 err = mv88e6xxx_g1_read(chip, GLOBAL_STATS_COUNTER_32, &reg);
678 if (err)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000679 return;
680
Vivien Didelota935c052016-09-29 12:21:53 -0400681 value = reg << 16;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000682
Vivien Didelota935c052016-09-29 12:21:53 -0400683 err = mv88e6xxx_g1_read(chip, GLOBAL_STATS_COUNTER_01, &reg);
684 if (err)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000685 return;
686
Vivien Didelota935c052016-09-29 12:21:53 -0400687 *val = value | reg;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000688}
689
Andrew Lunne413e7e2015-04-02 04:06:38 +0200690static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100691 { "in_good_octets", 8, 0x00, BANK0, },
692 { "in_bad_octets", 4, 0x02, BANK0, },
693 { "in_unicast", 4, 0x04, BANK0, },
694 { "in_broadcasts", 4, 0x06, BANK0, },
695 { "in_multicasts", 4, 0x07, BANK0, },
696 { "in_pause", 4, 0x16, BANK0, },
697 { "in_undersize", 4, 0x18, BANK0, },
698 { "in_fragments", 4, 0x19, BANK0, },
699 { "in_oversize", 4, 0x1a, BANK0, },
700 { "in_jabber", 4, 0x1b, BANK0, },
701 { "in_rx_error", 4, 0x1c, BANK0, },
702 { "in_fcs_error", 4, 0x1d, BANK0, },
703 { "out_octets", 8, 0x0e, BANK0, },
704 { "out_unicast", 4, 0x10, BANK0, },
705 { "out_broadcasts", 4, 0x13, BANK0, },
706 { "out_multicasts", 4, 0x12, BANK0, },
707 { "out_pause", 4, 0x15, BANK0, },
708 { "excessive", 4, 0x11, BANK0, },
709 { "collisions", 4, 0x1e, BANK0, },
710 { "deferred", 4, 0x05, BANK0, },
711 { "single", 4, 0x14, BANK0, },
712 { "multiple", 4, 0x17, BANK0, },
713 { "out_fcs_error", 4, 0x03, BANK0, },
714 { "late", 4, 0x1f, BANK0, },
715 { "hist_64bytes", 4, 0x08, BANK0, },
716 { "hist_65_127bytes", 4, 0x09, BANK0, },
717 { "hist_128_255bytes", 4, 0x0a, BANK0, },
718 { "hist_256_511bytes", 4, 0x0b, BANK0, },
719 { "hist_512_1023bytes", 4, 0x0c, BANK0, },
720 { "hist_1024_max_bytes", 4, 0x0d, BANK0, },
721 { "sw_in_discards", 4, 0x10, PORT, },
722 { "sw_in_filtered", 2, 0x12, PORT, },
723 { "sw_out_filtered", 2, 0x13, PORT, },
724 { "in_discards", 4, 0x00 | GLOBAL_STATS_OP_BANK_1, BANK1, },
725 { "in_filtered", 4, 0x01 | GLOBAL_STATS_OP_BANK_1, BANK1, },
726 { "in_accepted", 4, 0x02 | GLOBAL_STATS_OP_BANK_1, BANK1, },
727 { "in_bad_accepted", 4, 0x03 | GLOBAL_STATS_OP_BANK_1, BANK1, },
728 { "in_good_avb_class_a", 4, 0x04 | GLOBAL_STATS_OP_BANK_1, BANK1, },
729 { "in_good_avb_class_b", 4, 0x05 | GLOBAL_STATS_OP_BANK_1, BANK1, },
730 { "in_bad_avb_class_a", 4, 0x06 | GLOBAL_STATS_OP_BANK_1, BANK1, },
731 { "in_bad_avb_class_b", 4, 0x07 | GLOBAL_STATS_OP_BANK_1, BANK1, },
732 { "tcam_counter_0", 4, 0x08 | GLOBAL_STATS_OP_BANK_1, BANK1, },
733 { "tcam_counter_1", 4, 0x09 | GLOBAL_STATS_OP_BANK_1, BANK1, },
734 { "tcam_counter_2", 4, 0x0a | GLOBAL_STATS_OP_BANK_1, BANK1, },
735 { "tcam_counter_3", 4, 0x0b | GLOBAL_STATS_OP_BANK_1, BANK1, },
736 { "in_da_unknown", 4, 0x0e | GLOBAL_STATS_OP_BANK_1, BANK1, },
737 { "in_management", 4, 0x0f | GLOBAL_STATS_OP_BANK_1, BANK1, },
738 { "out_queue_0", 4, 0x10 | GLOBAL_STATS_OP_BANK_1, BANK1, },
739 { "out_queue_1", 4, 0x11 | GLOBAL_STATS_OP_BANK_1, BANK1, },
740 { "out_queue_2", 4, 0x12 | GLOBAL_STATS_OP_BANK_1, BANK1, },
741 { "out_queue_3", 4, 0x13 | GLOBAL_STATS_OP_BANK_1, BANK1, },
742 { "out_queue_4", 4, 0x14 | GLOBAL_STATS_OP_BANK_1, BANK1, },
743 { "out_queue_5", 4, 0x15 | GLOBAL_STATS_OP_BANK_1, BANK1, },
744 { "out_queue_6", 4, 0x16 | GLOBAL_STATS_OP_BANK_1, BANK1, },
745 { "out_queue_7", 4, 0x17 | GLOBAL_STATS_OP_BANK_1, BANK1, },
746 { "out_cut_through", 4, 0x18 | GLOBAL_STATS_OP_BANK_1, BANK1, },
747 { "out_octets_a", 4, 0x1a | GLOBAL_STATS_OP_BANK_1, BANK1, },
748 { "out_octets_b", 4, 0x1b | GLOBAL_STATS_OP_BANK_1, BANK1, },
749 { "out_management", 4, 0x1f | GLOBAL_STATS_OP_BANK_1, BANK1, },
Andrew Lunne413e7e2015-04-02 04:06:38 +0200750};
751
Vivien Didelotfad09c72016-06-21 12:28:20 -0400752static bool mv88e6xxx_has_stat(struct mv88e6xxx_chip *chip,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100753 struct mv88e6xxx_hw_stat *stat)
Andrew Lunne413e7e2015-04-02 04:06:38 +0200754{
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100755 switch (stat->type) {
756 case BANK0:
Andrew Lunne413e7e2015-04-02 04:06:38 +0200757 return true;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100758 case BANK1:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400759 return mv88e6xxx_6320_family(chip);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100760 case PORT:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400761 return mv88e6xxx_6095_family(chip) ||
762 mv88e6xxx_6185_family(chip) ||
763 mv88e6xxx_6097_family(chip) ||
764 mv88e6xxx_6165_family(chip) ||
765 mv88e6xxx_6351_family(chip) ||
766 mv88e6xxx_6352_family(chip);
Andrew Lunne413e7e2015-04-02 04:06:38 +0200767 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100768 return false;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000769}
770
Vivien Didelotfad09c72016-06-21 12:28:20 -0400771static uint64_t _mv88e6xxx_get_ethtool_stat(struct mv88e6xxx_chip *chip,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100772 struct mv88e6xxx_hw_stat *s,
Andrew Lunn80c46272015-06-20 18:42:30 +0200773 int port)
774{
Andrew Lunn80c46272015-06-20 18:42:30 +0200775 u32 low;
776 u32 high = 0;
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200777 int err;
778 u16 reg;
Andrew Lunn80c46272015-06-20 18:42:30 +0200779 u64 value;
780
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100781 switch (s->type) {
782 case PORT:
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200783 err = mv88e6xxx_port_read(chip, port, s->reg, &reg);
784 if (err)
Andrew Lunn80c46272015-06-20 18:42:30 +0200785 return UINT64_MAX;
786
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200787 low = reg;
Andrew Lunn80c46272015-06-20 18:42:30 +0200788 if (s->sizeof_stat == 4) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200789 err = mv88e6xxx_port_read(chip, port, s->reg + 1, &reg);
790 if (err)
Andrew Lunn80c46272015-06-20 18:42:30 +0200791 return UINT64_MAX;
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200792 high = reg;
Andrew Lunn80c46272015-06-20 18:42:30 +0200793 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100794 break;
795 case BANK0:
796 case BANK1:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400797 _mv88e6xxx_stats_read(chip, s->reg, &low);
Andrew Lunn80c46272015-06-20 18:42:30 +0200798 if (s->sizeof_stat == 8)
Vivien Didelotfad09c72016-06-21 12:28:20 -0400799 _mv88e6xxx_stats_read(chip, s->reg + 1, &high);
Andrew Lunn80c46272015-06-20 18:42:30 +0200800 }
801 value = (((u64)high) << 16) | low;
802 return value;
803}
804
Vivien Didelotf81ec902016-05-09 13:22:58 -0400805static void mv88e6xxx_get_strings(struct dsa_switch *ds, int port,
806 uint8_t *data)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100807{
Vivien Didelot04bed142016-08-31 18:06:13 -0400808 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100809 struct mv88e6xxx_hw_stat *stat;
810 int i, j;
811
812 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
813 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400814 if (mv88e6xxx_has_stat(chip, stat)) {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100815 memcpy(data + j * ETH_GSTRING_LEN, stat->string,
816 ETH_GSTRING_LEN);
817 j++;
818 }
819 }
820}
821
Vivien Didelotf81ec902016-05-09 13:22:58 -0400822static int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100823{
Vivien Didelot04bed142016-08-31 18:06:13 -0400824 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100825 struct mv88e6xxx_hw_stat *stat;
826 int i, j;
827
828 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
829 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400830 if (mv88e6xxx_has_stat(chip, stat))
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100831 j++;
832 }
833 return j;
834}
835
Vivien Didelotf81ec902016-05-09 13:22:58 -0400836static void mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, int port,
837 uint64_t *data)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000838{
Vivien Didelot04bed142016-08-31 18:06:13 -0400839 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100840 struct mv88e6xxx_hw_stat *stat;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000841 int ret;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100842 int i, j;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000843
Vivien Didelotfad09c72016-06-21 12:28:20 -0400844 mutex_lock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000845
Vivien Didelotfad09c72016-06-21 12:28:20 -0400846 ret = _mv88e6xxx_stats_snapshot(chip, port);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000847 if (ret < 0) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400848 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000849 return;
850 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100851 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
852 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400853 if (mv88e6xxx_has_stat(chip, stat)) {
854 data[j] = _mv88e6xxx_get_ethtool_stat(chip, stat, port);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100855 j++;
856 }
857 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000858
Vivien Didelotfad09c72016-06-21 12:28:20 -0400859 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000860}
Ben Hutchings98e67302011-11-25 14:36:19 +0000861
Vivien Didelotf81ec902016-05-09 13:22:58 -0400862static int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700863{
864 return 32 * sizeof(u16);
865}
866
Vivien Didelotf81ec902016-05-09 13:22:58 -0400867static void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
868 struct ethtool_regs *regs, void *_p)
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700869{
Vivien Didelot04bed142016-08-31 18:06:13 -0400870 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200871 int err;
872 u16 reg;
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700873 u16 *p = _p;
874 int i;
875
876 regs->version = 0;
877
878 memset(p, 0xff, 32 * sizeof(u16));
879
Vivien Didelotfad09c72016-06-21 12:28:20 -0400880 mutex_lock(&chip->reg_lock);
Vivien Didelot23062512016-05-09 13:22:45 -0400881
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700882 for (i = 0; i < 32; i++) {
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700883
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200884 err = mv88e6xxx_port_read(chip, port, i, &reg);
885 if (!err)
886 p[i] = reg;
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700887 }
Vivien Didelot23062512016-05-09 13:22:45 -0400888
Vivien Didelotfad09c72016-06-21 12:28:20 -0400889 mutex_unlock(&chip->reg_lock);
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700890}
891
Vivien Didelotfad09c72016-06-21 12:28:20 -0400892static int _mv88e6xxx_atu_wait(struct mv88e6xxx_chip *chip)
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700893{
Vivien Didelota935c052016-09-29 12:21:53 -0400894 return mv88e6xxx_g1_wait(chip, GLOBAL_ATU_OP, GLOBAL_ATU_OP_BUSY);
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700895}
896
Vivien Didelotf81ec902016-05-09 13:22:58 -0400897static int mv88e6xxx_get_eee(struct dsa_switch *ds, int port,
898 struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -0800899{
Vivien Didelot04bed142016-08-31 18:06:13 -0400900 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot9c938292016-08-15 17:19:02 -0400901 u16 reg;
902 int err;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800903
Vivien Didelotfad09c72016-06-21 12:28:20 -0400904 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
Vivien Didelotaadbdb82016-05-09 13:22:44 -0400905 return -EOPNOTSUPP;
906
Vivien Didelotfad09c72016-06-21 12:28:20 -0400907 mutex_lock(&chip->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200908
Vivien Didelot9c938292016-08-15 17:19:02 -0400909 err = mv88e6xxx_phy_read(chip, port, 16, &reg);
910 if (err)
Andrew Lunn2f40c692015-04-02 04:06:37 +0200911 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800912
913 e->eee_enabled = !!(reg & 0x0200);
914 e->tx_lpi_enabled = !!(reg & 0x0100);
915
Andrew Lunn0e7b9922016-09-21 01:40:31 +0200916 err = mv88e6xxx_port_read(chip, port, PORT_STATUS, &reg);
Vivien Didelot9c938292016-08-15 17:19:02 -0400917 if (err)
Andrew Lunn2f40c692015-04-02 04:06:37 +0200918 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800919
Andrew Lunncca8b132015-04-02 04:06:39 +0200920 e->eee_active = !!(reg & PORT_STATUS_EEE);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200921out:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400922 mutex_unlock(&chip->reg_lock);
Vivien Didelot9c938292016-08-15 17:19:02 -0400923
924 return err;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800925}
926
Vivien Didelotf81ec902016-05-09 13:22:58 -0400927static int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
928 struct phy_device *phydev, struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -0800929{
Vivien Didelot04bed142016-08-31 18:06:13 -0400930 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot9c938292016-08-15 17:19:02 -0400931 u16 reg;
932 int err;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800933
Vivien Didelotfad09c72016-06-21 12:28:20 -0400934 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
Vivien Didelotaadbdb82016-05-09 13:22:44 -0400935 return -EOPNOTSUPP;
936
Vivien Didelotfad09c72016-06-21 12:28:20 -0400937 mutex_lock(&chip->reg_lock);
Guenter Roeck11b3b452015-03-06 22:23:51 -0800938
Vivien Didelot9c938292016-08-15 17:19:02 -0400939 err = mv88e6xxx_phy_read(chip, port, 16, &reg);
940 if (err)
Andrew Lunn2f40c692015-04-02 04:06:37 +0200941 goto out;
942
Vivien Didelot9c938292016-08-15 17:19:02 -0400943 reg &= ~0x0300;
Andrew Lunn2f40c692015-04-02 04:06:37 +0200944 if (e->eee_enabled)
945 reg |= 0x0200;
946 if (e->tx_lpi_enabled)
947 reg |= 0x0100;
948
Vivien Didelot9c938292016-08-15 17:19:02 -0400949 err = mv88e6xxx_phy_write(chip, port, 16, reg);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200950out:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400951 mutex_unlock(&chip->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200952
Vivien Didelot9c938292016-08-15 17:19:02 -0400953 return err;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800954}
955
Vivien Didelotfad09c72016-06-21 12:28:20 -0400956static int _mv88e6xxx_atu_cmd(struct mv88e6xxx_chip *chip, u16 fid, u16 cmd)
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700957{
Vivien Didelota935c052016-09-29 12:21:53 -0400958 u16 val;
959 int err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700960
Vivien Didelot6dc10bb2016-09-29 12:21:55 -0400961 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G1_ATU_FID)) {
Vivien Didelota935c052016-09-29 12:21:53 -0400962 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_FID, fid);
963 if (err)
964 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -0400965 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -0400966 /* ATU DBNum[7:4] are located in ATU Control 15:12 */
Vivien Didelota935c052016-09-29 12:21:53 -0400967 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_CONTROL, &val);
968 if (err)
969 return err;
Vivien Didelot11ea8092016-03-31 16:53:44 -0400970
Vivien Didelota935c052016-09-29 12:21:53 -0400971 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_CONTROL,
972 (val & 0xfff) | ((fid << 8) & 0xf000));
973 if (err)
974 return err;
Vivien Didelot11ea8092016-03-31 16:53:44 -0400975
976 /* ATU DBNum[3:0] are located in ATU Operation 3:0 */
977 cmd |= fid & 0xf;
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400978 }
979
Vivien Didelota935c052016-09-29 12:21:53 -0400980 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_OP, cmd);
981 if (err)
982 return err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700983
Vivien Didelotfad09c72016-06-21 12:28:20 -0400984 return _mv88e6xxx_atu_wait(chip);
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700985}
986
Vivien Didelotfad09c72016-06-21 12:28:20 -0400987static int _mv88e6xxx_atu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot37705b72015-09-04 14:34:11 -0400988 struct mv88e6xxx_atu_entry *entry)
989{
990 u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK;
991
992 if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) {
993 unsigned int mask, shift;
994
995 if (entry->trunk) {
996 data |= GLOBAL_ATU_DATA_TRUNK;
997 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
998 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
999 } else {
1000 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
1001 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
1002 }
1003
1004 data |= (entry->portv_trunkid << shift) & mask;
1005 }
1006
Vivien Didelota935c052016-09-29 12:21:53 -04001007 return mv88e6xxx_g1_write(chip, GLOBAL_ATU_DATA, data);
Vivien Didelot37705b72015-09-04 14:34:11 -04001008}
1009
Vivien Didelotfad09c72016-06-21 12:28:20 -04001010static int _mv88e6xxx_atu_flush_move(struct mv88e6xxx_chip *chip,
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001011 struct mv88e6xxx_atu_entry *entry,
1012 bool static_too)
1013{
1014 int op;
1015 int err;
1016
Vivien Didelotfad09c72016-06-21 12:28:20 -04001017 err = _mv88e6xxx_atu_wait(chip);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001018 if (err)
1019 return err;
1020
Vivien Didelotfad09c72016-06-21 12:28:20 -04001021 err = _mv88e6xxx_atu_data_write(chip, entry);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001022 if (err)
1023 return err;
1024
1025 if (entry->fid) {
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001026 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB :
1027 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB;
1028 } else {
1029 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL :
1030 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC;
1031 }
1032
Vivien Didelotfad09c72016-06-21 12:28:20 -04001033 return _mv88e6xxx_atu_cmd(chip, entry->fid, op);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001034}
1035
Vivien Didelotfad09c72016-06-21 12:28:20 -04001036static int _mv88e6xxx_atu_flush(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001037 u16 fid, bool static_too)
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001038{
1039 struct mv88e6xxx_atu_entry entry = {
1040 .fid = fid,
1041 .state = 0, /* EntryState bits must be 0 */
1042 };
1043
Vivien Didelotfad09c72016-06-21 12:28:20 -04001044 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001045}
1046
Vivien Didelotfad09c72016-06-21 12:28:20 -04001047static int _mv88e6xxx_atu_move(struct mv88e6xxx_chip *chip, u16 fid,
Andrew Lunn158bc062016-04-28 21:24:06 -04001048 int from_port, int to_port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001049{
1050 struct mv88e6xxx_atu_entry entry = {
1051 .trunk = false,
1052 .fid = fid,
1053 };
1054
1055 /* EntryState bits must be 0xF */
1056 entry.state = GLOBAL_ATU_DATA_STATE_MASK;
1057
1058 /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */
1059 entry.portv_trunkid = (to_port & 0x0f) << 4;
1060 entry.portv_trunkid |= from_port & 0x0f;
1061
Vivien Didelotfad09c72016-06-21 12:28:20 -04001062 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001063}
1064
Vivien Didelotfad09c72016-06-21 12:28:20 -04001065static int _mv88e6xxx_atu_remove(struct mv88e6xxx_chip *chip, u16 fid,
Andrew Lunn158bc062016-04-28 21:24:06 -04001066 int port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001067{
1068 /* Destination port 0xF means remove the entries */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001069 return _mv88e6xxx_atu_move(chip, fid, port, 0x0f, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001070}
1071
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001072static const char * const mv88e6xxx_port_state_names[] = {
1073 [PORT_CONTROL_STATE_DISABLED] = "Disabled",
1074 [PORT_CONTROL_STATE_BLOCKING] = "Blocking/Listening",
1075 [PORT_CONTROL_STATE_LEARNING] = "Learning",
1076 [PORT_CONTROL_STATE_FORWARDING] = "Forwarding",
1077};
1078
Vivien Didelotfad09c72016-06-21 12:28:20 -04001079static int _mv88e6xxx_port_state(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001080 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001081{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001082 struct dsa_switch *ds = chip->ds;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001083 u16 reg;
1084 int err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001085 u8 oldstate;
1086
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001087 err = mv88e6xxx_port_read(chip, port, PORT_CONTROL, &reg);
1088 if (err)
1089 return err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001090
Andrew Lunncca8b132015-04-02 04:06:39 +02001091 oldstate = reg & PORT_CONTROL_STATE_MASK;
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001092
Vivien Didelot749efcb2016-09-22 16:49:24 -04001093 reg &= ~PORT_CONTROL_STATE_MASK;
1094 reg |= state;
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001095
Vivien Didelot749efcb2016-09-22 16:49:24 -04001096 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL, reg);
1097 if (err)
1098 return err;
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001099
Vivien Didelot749efcb2016-09-22 16:49:24 -04001100 netdev_dbg(ds->ports[port].netdev, "PortState %s (was %s)\n",
1101 mv88e6xxx_port_state_names[state],
1102 mv88e6xxx_port_state_names[oldstate]);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001103
Vivien Didelot749efcb2016-09-22 16:49:24 -04001104 return 0;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001105}
1106
Vivien Didelotfad09c72016-06-21 12:28:20 -04001107static int _mv88e6xxx_port_based_vlan_map(struct mv88e6xxx_chip *chip, int port)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001108{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001109 struct net_device *bridge = chip->ports[port].bridge_dev;
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001110 const u16 mask = (1 << mv88e6xxx_num_ports(chip)) - 1;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001111 struct dsa_switch *ds = chip->ds;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001112 u16 output_ports = 0;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001113 u16 reg;
1114 int err;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001115 int i;
1116
1117 /* allow CPU port or DSA link(s) to send frames to every port */
1118 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
1119 output_ports = mask;
1120 } else {
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001121 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001122 /* allow sending frames to every group member */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001123 if (bridge && chip->ports[i].bridge_dev == bridge)
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001124 output_ports |= BIT(i);
1125
1126 /* allow sending frames to CPU port and DSA link(s) */
1127 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
1128 output_ports |= BIT(i);
1129 }
1130 }
1131
1132 /* prevent frames from going back out of the port they came in on */
1133 output_ports &= ~BIT(port);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001134
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001135 err = mv88e6xxx_port_read(chip, port, PORT_BASE_VLAN, &reg);
1136 if (err)
1137 return err;
Vivien Didelotede80982015-10-11 18:08:35 -04001138
1139 reg &= ~mask;
1140 reg |= output_ports & mask;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001141
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001142 return mv88e6xxx_port_write(chip, port, PORT_BASE_VLAN, reg);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001143}
1144
Vivien Didelotf81ec902016-05-09 13:22:58 -04001145static void mv88e6xxx_port_stp_state_set(struct dsa_switch *ds, int port,
1146 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001147{
Vivien Didelot04bed142016-08-31 18:06:13 -04001148 struct mv88e6xxx_chip *chip = ds->priv;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001149 int stp_state;
Vivien Didelot553eb542016-05-13 20:38:23 -04001150 int err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001151
1152 switch (state) {
1153 case BR_STATE_DISABLED:
Andrew Lunncca8b132015-04-02 04:06:39 +02001154 stp_state = PORT_CONTROL_STATE_DISABLED;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001155 break;
1156 case BR_STATE_BLOCKING:
1157 case BR_STATE_LISTENING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001158 stp_state = PORT_CONTROL_STATE_BLOCKING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001159 break;
1160 case BR_STATE_LEARNING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001161 stp_state = PORT_CONTROL_STATE_LEARNING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001162 break;
1163 case BR_STATE_FORWARDING:
1164 default:
Andrew Lunncca8b132015-04-02 04:06:39 +02001165 stp_state = PORT_CONTROL_STATE_FORWARDING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001166 break;
1167 }
1168
Vivien Didelotfad09c72016-06-21 12:28:20 -04001169 mutex_lock(&chip->reg_lock);
1170 err = _mv88e6xxx_port_state(chip, port, stp_state);
1171 mutex_unlock(&chip->reg_lock);
Vivien Didelot553eb542016-05-13 20:38:23 -04001172
1173 if (err)
Andrew Lunnc8b09802016-06-04 21:16:57 +02001174 netdev_err(ds->ports[port].netdev,
1175 "failed to update state to %s\n",
Vivien Didelot553eb542016-05-13 20:38:23 -04001176 mv88e6xxx_port_state_names[stp_state]);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001177}
1178
Vivien Didelot749efcb2016-09-22 16:49:24 -04001179static void mv88e6xxx_port_fast_age(struct dsa_switch *ds, int port)
1180{
1181 struct mv88e6xxx_chip *chip = ds->priv;
1182 int err;
1183
1184 mutex_lock(&chip->reg_lock);
1185 err = _mv88e6xxx_atu_remove(chip, 0, port, false);
1186 mutex_unlock(&chip->reg_lock);
1187
1188 if (err)
1189 netdev_err(ds->ports[port].netdev, "failed to flush ATU\n");
1190}
1191
Vivien Didelotfad09c72016-06-21 12:28:20 -04001192static int _mv88e6xxx_port_pvid(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001193 u16 *new, u16 *old)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001194{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001195 struct dsa_switch *ds = chip->ds;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001196 u16 pvid, reg;
1197 int err;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001198
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001199 err = mv88e6xxx_port_read(chip, port, PORT_DEFAULT_VLAN, &reg);
1200 if (err)
1201 return err;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001202
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001203 pvid = reg & PORT_DEFAULT_VLAN_MASK;
Vivien Didelot5da96032016-03-07 18:24:39 -05001204
1205 if (new) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001206 reg &= ~PORT_DEFAULT_VLAN_MASK;
1207 reg |= *new & PORT_DEFAULT_VLAN_MASK;
Vivien Didelot5da96032016-03-07 18:24:39 -05001208
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001209 err = mv88e6xxx_port_write(chip, port, PORT_DEFAULT_VLAN, reg);
1210 if (err)
1211 return err;
Vivien Didelot5da96032016-03-07 18:24:39 -05001212
Andrew Lunnc8b09802016-06-04 21:16:57 +02001213 netdev_dbg(ds->ports[port].netdev,
1214 "DefaultVID %d (was %d)\n", *new, pvid);
Vivien Didelot5da96032016-03-07 18:24:39 -05001215 }
1216
1217 if (old)
1218 *old = pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001219
1220 return 0;
1221}
1222
Vivien Didelotfad09c72016-06-21 12:28:20 -04001223static int _mv88e6xxx_port_pvid_get(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001224 int port, u16 *pvid)
Vivien Didelot5da96032016-03-07 18:24:39 -05001225{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001226 return _mv88e6xxx_port_pvid(chip, port, NULL, pvid);
Vivien Didelot5da96032016-03-07 18:24:39 -05001227}
1228
Vivien Didelotfad09c72016-06-21 12:28:20 -04001229static int _mv88e6xxx_port_pvid_set(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001230 int port, u16 pvid)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001231{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001232 return _mv88e6xxx_port_pvid(chip, port, &pvid, NULL);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001233}
1234
Vivien Didelotfad09c72016-06-21 12:28:20 -04001235static int _mv88e6xxx_vtu_wait(struct mv88e6xxx_chip *chip)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001236{
Vivien Didelota935c052016-09-29 12:21:53 -04001237 return mv88e6xxx_g1_wait(chip, GLOBAL_VTU_OP, GLOBAL_VTU_OP_BUSY);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001238}
1239
Vivien Didelotfad09c72016-06-21 12:28:20 -04001240static int _mv88e6xxx_vtu_cmd(struct mv88e6xxx_chip *chip, u16 op)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001241{
Vivien Didelota935c052016-09-29 12:21:53 -04001242 int err;
Vivien Didelot6b17e862015-08-13 12:52:18 -04001243
Vivien Didelota935c052016-09-29 12:21:53 -04001244 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_OP, op);
1245 if (err)
1246 return err;
Vivien Didelot6b17e862015-08-13 12:52:18 -04001247
Vivien Didelotfad09c72016-06-21 12:28:20 -04001248 return _mv88e6xxx_vtu_wait(chip);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001249}
1250
Vivien Didelotfad09c72016-06-21 12:28:20 -04001251static int _mv88e6xxx_vtu_stu_flush(struct mv88e6xxx_chip *chip)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001252{
1253 int ret;
1254
Vivien Didelotfad09c72016-06-21 12:28:20 -04001255 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001256 if (ret < 0)
1257 return ret;
1258
Vivien Didelotfad09c72016-06-21 12:28:20 -04001259 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_FLUSH_ALL);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001260}
1261
Vivien Didelotfad09c72016-06-21 12:28:20 -04001262static int _mv88e6xxx_vtu_stu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001263 struct mv88e6xxx_vtu_entry *entry,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001264 unsigned int nibble_offset)
1265{
Vivien Didelotb8fee952015-08-13 12:52:19 -04001266 u16 regs[3];
Vivien Didelota935c052016-09-29 12:21:53 -04001267 int i, err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001268
1269 for (i = 0; i < 3; ++i) {
Vivien Didelota935c052016-09-29 12:21:53 -04001270 u16 *reg = &regs[i];
Vivien Didelotb8fee952015-08-13 12:52:19 -04001271
Vivien Didelota935c052016-09-29 12:21:53 -04001272 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_DATA_0_3 + i, reg);
1273 if (err)
1274 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001275 }
1276
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001277 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotb8fee952015-08-13 12:52:19 -04001278 unsigned int shift = (i % 4) * 4 + nibble_offset;
1279 u16 reg = regs[i / 4];
1280
1281 entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
1282 }
1283
1284 return 0;
1285}
1286
Vivien Didelotfad09c72016-06-21 12:28:20 -04001287static int mv88e6xxx_vtu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001288 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001289{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001290 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 0);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001291}
1292
Vivien Didelotfad09c72016-06-21 12:28:20 -04001293static int mv88e6xxx_stu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001294 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001295{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001296 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 2);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001297}
1298
Vivien Didelotfad09c72016-06-21 12:28:20 -04001299static int _mv88e6xxx_vtu_stu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001300 struct mv88e6xxx_vtu_entry *entry,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001301 unsigned int nibble_offset)
1302{
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001303 u16 regs[3] = { 0 };
Vivien Didelota935c052016-09-29 12:21:53 -04001304 int i, err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001305
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001306 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001307 unsigned int shift = (i % 4) * 4 + nibble_offset;
1308 u8 data = entry->data[i];
1309
1310 regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
1311 }
1312
1313 for (i = 0; i < 3; ++i) {
Vivien Didelota935c052016-09-29 12:21:53 -04001314 u16 reg = regs[i];
1315
1316 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_DATA_0_3 + i, reg);
1317 if (err)
1318 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001319 }
1320
1321 return 0;
1322}
1323
Vivien Didelotfad09c72016-06-21 12:28:20 -04001324static int mv88e6xxx_vtu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001325 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001326{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001327 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 0);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001328}
1329
Vivien Didelotfad09c72016-06-21 12:28:20 -04001330static int mv88e6xxx_stu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001331 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001332{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001333 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 2);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001334}
1335
Vivien Didelotfad09c72016-06-21 12:28:20 -04001336static int _mv88e6xxx_vtu_vid_write(struct mv88e6xxx_chip *chip, u16 vid)
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001337{
Vivien Didelota935c052016-09-29 12:21:53 -04001338 return mv88e6xxx_g1_write(chip, GLOBAL_VTU_VID,
1339 vid & GLOBAL_VTU_VID_MASK);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001340}
1341
Vivien Didelotfad09c72016-06-21 12:28:20 -04001342static int _mv88e6xxx_vtu_getnext(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001343 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelotb8fee952015-08-13 12:52:19 -04001344{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001345 struct mv88e6xxx_vtu_entry next = { 0 };
Vivien Didelota935c052016-09-29 12:21:53 -04001346 u16 val;
1347 int err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001348
Vivien Didelota935c052016-09-29 12:21:53 -04001349 err = _mv88e6xxx_vtu_wait(chip);
1350 if (err)
1351 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001352
Vivien Didelota935c052016-09-29 12:21:53 -04001353 err = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_VTU_GET_NEXT);
1354 if (err)
1355 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001356
Vivien Didelota935c052016-09-29 12:21:53 -04001357 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_VID, &val);
1358 if (err)
1359 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001360
Vivien Didelota935c052016-09-29 12:21:53 -04001361 next.vid = val & GLOBAL_VTU_VID_MASK;
1362 next.valid = !!(val & GLOBAL_VTU_VID_VALID);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001363
1364 if (next.valid) {
Vivien Didelota935c052016-09-29 12:21:53 -04001365 err = mv88e6xxx_vtu_data_read(chip, &next);
1366 if (err)
1367 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001368
Vivien Didelot6dc10bb2016-09-29 12:21:55 -04001369 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G1_VTU_FID)) {
Vivien Didelota935c052016-09-29 12:21:53 -04001370 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_FID, &val);
1371 if (err)
1372 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001373
Vivien Didelota935c052016-09-29 12:21:53 -04001374 next.fid = val & GLOBAL_VTU_FID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001375 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001376 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1377 * VTU DBNum[3:0] are located in VTU Operation 3:0
1378 */
Vivien Didelota935c052016-09-29 12:21:53 -04001379 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_OP, &val);
1380 if (err)
1381 return err;
Vivien Didelot11ea8092016-03-31 16:53:44 -04001382
Vivien Didelota935c052016-09-29 12:21:53 -04001383 next.fid = (val & 0xf00) >> 4;
1384 next.fid |= val & 0xf;
Vivien Didelot2e7bd5e2016-03-31 16:53:41 -04001385 }
Vivien Didelotb8fee952015-08-13 12:52:19 -04001386
Vivien Didelotfad09c72016-06-21 12:28:20 -04001387 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
Vivien Didelota935c052016-09-29 12:21:53 -04001388 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_SID, &val);
1389 if (err)
1390 return err;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001391
Vivien Didelota935c052016-09-29 12:21:53 -04001392 next.sid = val & GLOBAL_VTU_SID_MASK;
Vivien Didelotb8fee952015-08-13 12:52:19 -04001393 }
1394 }
1395
1396 *entry = next;
1397 return 0;
1398}
1399
Vivien Didelotf81ec902016-05-09 13:22:58 -04001400static int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
1401 struct switchdev_obj_port_vlan *vlan,
1402 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001403{
Vivien Didelot04bed142016-08-31 18:06:13 -04001404 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001405 struct mv88e6xxx_vtu_entry next;
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001406 u16 pvid;
1407 int err;
1408
Vivien Didelotfad09c72016-06-21 12:28:20 -04001409 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001410 return -EOPNOTSUPP;
1411
Vivien Didelotfad09c72016-06-21 12:28:20 -04001412 mutex_lock(&chip->reg_lock);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001413
Vivien Didelotfad09c72016-06-21 12:28:20 -04001414 err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001415 if (err)
1416 goto unlock;
1417
Vivien Didelotfad09c72016-06-21 12:28:20 -04001418 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001419 if (err)
1420 goto unlock;
1421
1422 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001423 err = _mv88e6xxx_vtu_getnext(chip, &next);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001424 if (err)
1425 break;
1426
1427 if (!next.valid)
1428 break;
1429
1430 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1431 continue;
1432
1433 /* reinit and dump this VLAN obj */
Vivien Didelot57d32312016-06-20 13:13:58 -04001434 vlan->vid_begin = next.vid;
1435 vlan->vid_end = next.vid;
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001436 vlan->flags = 0;
1437
1438 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1439 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1440
1441 if (next.vid == pvid)
1442 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1443
1444 err = cb(&vlan->obj);
1445 if (err)
1446 break;
1447 } while (next.vid < GLOBAL_VTU_VID_MASK);
1448
1449unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001450 mutex_unlock(&chip->reg_lock);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001451
1452 return err;
1453}
1454
Vivien Didelotfad09c72016-06-21 12:28:20 -04001455static int _mv88e6xxx_vtu_loadpurge(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001456 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001457{
Vivien Didelot11ea8092016-03-31 16:53:44 -04001458 u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001459 u16 reg = 0;
Vivien Didelota935c052016-09-29 12:21:53 -04001460 int err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001461
Vivien Didelota935c052016-09-29 12:21:53 -04001462 err = _mv88e6xxx_vtu_wait(chip);
1463 if (err)
1464 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001465
1466 if (!entry->valid)
1467 goto loadpurge;
1468
1469 /* Write port member tags */
Vivien Didelota935c052016-09-29 12:21:53 -04001470 err = mv88e6xxx_vtu_data_write(chip, entry);
1471 if (err)
1472 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001473
Vivien Didelotfad09c72016-06-21 12:28:20 -04001474 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001475 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Vivien Didelota935c052016-09-29 12:21:53 -04001476 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_SID, reg);
1477 if (err)
1478 return err;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001479 }
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001480
Vivien Didelot6dc10bb2016-09-29 12:21:55 -04001481 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G1_VTU_FID)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001482 reg = entry->fid & GLOBAL_VTU_FID_MASK;
Vivien Didelota935c052016-09-29 12:21:53 -04001483 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_FID, reg);
1484 if (err)
1485 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001486 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001487 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1488 * VTU DBNum[3:0] are located in VTU Operation 3:0
1489 */
1490 op |= (entry->fid & 0xf0) << 8;
1491 op |= entry->fid & 0xf;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001492 }
1493
1494 reg = GLOBAL_VTU_VID_VALID;
1495loadpurge:
1496 reg |= entry->vid & GLOBAL_VTU_VID_MASK;
Vivien Didelota935c052016-09-29 12:21:53 -04001497 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_VID, reg);
1498 if (err)
1499 return err;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001500
Vivien Didelotfad09c72016-06-21 12:28:20 -04001501 return _mv88e6xxx_vtu_cmd(chip, op);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001502}
1503
Vivien Didelotfad09c72016-06-21 12:28:20 -04001504static int _mv88e6xxx_stu_getnext(struct mv88e6xxx_chip *chip, u8 sid,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001505 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001506{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001507 struct mv88e6xxx_vtu_entry next = { 0 };
Vivien Didelota935c052016-09-29 12:21:53 -04001508 u16 val;
1509 int err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001510
Vivien Didelota935c052016-09-29 12:21:53 -04001511 err = _mv88e6xxx_vtu_wait(chip);
1512 if (err)
1513 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001514
Vivien Didelota935c052016-09-29 12:21:53 -04001515 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_SID,
1516 sid & GLOBAL_VTU_SID_MASK);
1517 if (err)
1518 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001519
Vivien Didelota935c052016-09-29 12:21:53 -04001520 err = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_GET_NEXT);
1521 if (err)
1522 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001523
Vivien Didelota935c052016-09-29 12:21:53 -04001524 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_SID, &val);
1525 if (err)
1526 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001527
Vivien Didelota935c052016-09-29 12:21:53 -04001528 next.sid = val & GLOBAL_VTU_SID_MASK;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001529
Vivien Didelota935c052016-09-29 12:21:53 -04001530 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_VID, &val);
1531 if (err)
1532 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001533
Vivien Didelota935c052016-09-29 12:21:53 -04001534 next.valid = !!(val & GLOBAL_VTU_VID_VALID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001535
1536 if (next.valid) {
Vivien Didelota935c052016-09-29 12:21:53 -04001537 err = mv88e6xxx_stu_data_read(chip, &next);
1538 if (err)
1539 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001540 }
1541
1542 *entry = next;
1543 return 0;
1544}
1545
Vivien Didelotfad09c72016-06-21 12:28:20 -04001546static int _mv88e6xxx_stu_loadpurge(struct mv88e6xxx_chip *chip,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001547 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001548{
1549 u16 reg = 0;
Vivien Didelota935c052016-09-29 12:21:53 -04001550 int err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001551
Vivien Didelota935c052016-09-29 12:21:53 -04001552 err = _mv88e6xxx_vtu_wait(chip);
1553 if (err)
1554 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001555
1556 if (!entry->valid)
1557 goto loadpurge;
1558
1559 /* Write port states */
Vivien Didelota935c052016-09-29 12:21:53 -04001560 err = mv88e6xxx_stu_data_write(chip, entry);
1561 if (err)
1562 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001563
1564 reg = GLOBAL_VTU_VID_VALID;
1565loadpurge:
Vivien Didelota935c052016-09-29 12:21:53 -04001566 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_VID, reg);
1567 if (err)
1568 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001569
1570 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Vivien Didelota935c052016-09-29 12:21:53 -04001571 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_SID, reg);
1572 if (err)
1573 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001574
Vivien Didelotfad09c72016-06-21 12:28:20 -04001575 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_LOAD_PURGE);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001576}
1577
Vivien Didelotfad09c72016-06-21 12:28:20 -04001578static int _mv88e6xxx_port_fid(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001579 u16 *new, u16 *old)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001580{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001581 struct dsa_switch *ds = chip->ds;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001582 u16 upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001583 u16 fid;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001584 u16 reg;
1585 int err;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001586
Vivien Didelotfad09c72016-06-21 12:28:20 -04001587 if (mv88e6xxx_num_databases(chip) == 4096)
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001588 upper_mask = 0xff;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001589 else if (mv88e6xxx_num_databases(chip) == 256)
Vivien Didelot11ea8092016-03-31 16:53:44 -04001590 upper_mask = 0xf;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001591 else
1592 return -EOPNOTSUPP;
1593
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001594 /* Port's default FID bits 3:0 are located in reg 0x06, offset 12 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001595 err = mv88e6xxx_port_read(chip, port, PORT_BASE_VLAN, &reg);
1596 if (err)
1597 return err;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001598
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001599 fid = (reg & PORT_BASE_VLAN_FID_3_0_MASK) >> 12;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001600
1601 if (new) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001602 reg &= ~PORT_BASE_VLAN_FID_3_0_MASK;
1603 reg |= (*new << 12) & PORT_BASE_VLAN_FID_3_0_MASK;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001604
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001605 err = mv88e6xxx_port_write(chip, port, PORT_BASE_VLAN, reg);
1606 if (err)
1607 return err;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001608 }
1609
1610 /* Port's default FID bits 11:4 are located in reg 0x05, offset 0 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001611 err = mv88e6xxx_port_read(chip, port, PORT_CONTROL_1, &reg);
1612 if (err)
1613 return err;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001614
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001615 fid |= (reg & upper_mask) << 4;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001616
1617 if (new) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001618 reg &= ~upper_mask;
1619 reg |= (*new >> 4) & upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001620
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001621 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_1, reg);
1622 if (err)
1623 return err;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001624
Andrew Lunnc8b09802016-06-04 21:16:57 +02001625 netdev_dbg(ds->ports[port].netdev,
1626 "FID %d (was %d)\n", *new, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001627 }
1628
1629 if (old)
1630 *old = fid;
1631
1632 return 0;
1633}
1634
Vivien Didelotfad09c72016-06-21 12:28:20 -04001635static int _mv88e6xxx_port_fid_get(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001636 int port, u16 *fid)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001637{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001638 return _mv88e6xxx_port_fid(chip, port, NULL, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001639}
1640
Vivien Didelotfad09c72016-06-21 12:28:20 -04001641static int _mv88e6xxx_port_fid_set(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001642 int port, u16 fid)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001643{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001644 return _mv88e6xxx_port_fid(chip, port, &fid, NULL);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001645}
1646
Vivien Didelotfad09c72016-06-21 12:28:20 -04001647static int _mv88e6xxx_fid_new(struct mv88e6xxx_chip *chip, u16 *fid)
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001648{
1649 DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001650 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001651 int i, err;
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001652
1653 bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);
1654
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001655 /* Set every FID bit used by the (un)bridged ports */
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001656 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001657 err = _mv88e6xxx_port_fid_get(chip, i, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001658 if (err)
1659 return err;
1660
1661 set_bit(*fid, fid_bitmap);
1662 }
1663
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001664 /* Set every FID bit used by the VLAN entries */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001665 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001666 if (err)
1667 return err;
1668
1669 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001670 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001671 if (err)
1672 return err;
1673
1674 if (!vlan.valid)
1675 break;
1676
1677 set_bit(vlan.fid, fid_bitmap);
1678 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
1679
1680 /* The reset value 0x000 is used to indicate that multiple address
1681 * databases are not needed. Return the next positive available.
1682 */
1683 *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1);
Vivien Didelotfad09c72016-06-21 12:28:20 -04001684 if (unlikely(*fid >= mv88e6xxx_num_databases(chip)))
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001685 return -ENOSPC;
1686
1687 /* Clear the database */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001688 return _mv88e6xxx_atu_flush(chip, *fid, true);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001689}
1690
Vivien Didelotfad09c72016-06-21 12:28:20 -04001691static int _mv88e6xxx_vtu_new(struct mv88e6xxx_chip *chip, u16 vid,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001692 struct mv88e6xxx_vtu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001693{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001694 struct dsa_switch *ds = chip->ds;
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001695 struct mv88e6xxx_vtu_entry vlan = {
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001696 .valid = true,
1697 .vid = vid,
1698 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001699 int i, err;
1700
Vivien Didelotfad09c72016-06-21 12:28:20 -04001701 err = _mv88e6xxx_fid_new(chip, &vlan.fid);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001702 if (err)
1703 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001704
Vivien Didelot3d131f02015-11-03 10:52:52 -05001705 /* exclude all ports except the CPU and DSA ports */
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001706 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i)
Vivien Didelot3d131f02015-11-03 10:52:52 -05001707 vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)
1708 ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED
1709 : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001710
Vivien Didelotfad09c72016-06-21 12:28:20 -04001711 if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
1712 mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip)) {
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001713 struct mv88e6xxx_vtu_entry vstp;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001714
1715 /* Adding a VTU entry requires a valid STU entry. As VSTP is not
1716 * implemented, only one STU entry is needed to cover all VTU
1717 * entries. Thus, validate the SID 0.
1718 */
1719 vlan.sid = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001720 err = _mv88e6xxx_stu_getnext(chip, GLOBAL_VTU_SID_MASK, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001721 if (err)
1722 return err;
1723
1724 if (vstp.sid != vlan.sid || !vstp.valid) {
1725 memset(&vstp, 0, sizeof(vstp));
1726 vstp.valid = true;
1727 vstp.sid = vlan.sid;
1728
Vivien Didelotfad09c72016-06-21 12:28:20 -04001729 err = _mv88e6xxx_stu_loadpurge(chip, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001730 if (err)
1731 return err;
1732 }
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001733 }
1734
1735 *entry = vlan;
1736 return 0;
1737}
1738
Vivien Didelotfad09c72016-06-21 12:28:20 -04001739static int _mv88e6xxx_vtu_get(struct mv88e6xxx_chip *chip, u16 vid,
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001740 struct mv88e6xxx_vtu_entry *entry, bool creat)
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001741{
1742 int err;
1743
1744 if (!vid)
1745 return -EINVAL;
1746
Vivien Didelotfad09c72016-06-21 12:28:20 -04001747 err = _mv88e6xxx_vtu_vid_write(chip, vid - 1);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001748 if (err)
1749 return err;
1750
Vivien Didelotfad09c72016-06-21 12:28:20 -04001751 err = _mv88e6xxx_vtu_getnext(chip, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001752 if (err)
1753 return err;
1754
1755 if (entry->vid != vid || !entry->valid) {
1756 if (!creat)
1757 return -EOPNOTSUPP;
1758 /* -ENOENT would've been more appropriate, but switchdev expects
1759 * -EOPNOTSUPP to inform bridge about an eventual software VLAN.
1760 */
1761
Vivien Didelotfad09c72016-06-21 12:28:20 -04001762 err = _mv88e6xxx_vtu_new(chip, vid, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001763 }
1764
1765 return err;
1766}
1767
Vivien Didelotda9c3592016-02-12 12:09:40 -05001768static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
1769 u16 vid_begin, u16 vid_end)
1770{
Vivien Didelot04bed142016-08-31 18:06:13 -04001771 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001772 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelotda9c3592016-02-12 12:09:40 -05001773 int i, err;
1774
1775 if (!vid_begin)
1776 return -EOPNOTSUPP;
1777
Vivien Didelotfad09c72016-06-21 12:28:20 -04001778 mutex_lock(&chip->reg_lock);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001779
Vivien Didelotfad09c72016-06-21 12:28:20 -04001780 err = _mv88e6xxx_vtu_vid_write(chip, vid_begin - 1);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001781 if (err)
1782 goto unlock;
1783
1784 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001785 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001786 if (err)
1787 goto unlock;
1788
1789 if (!vlan.valid)
1790 break;
1791
1792 if (vlan.vid > vid_end)
1793 break;
1794
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001795 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotda9c3592016-02-12 12:09:40 -05001796 if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i))
1797 continue;
1798
1799 if (vlan.data[i] ==
1800 GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1801 continue;
1802
Vivien Didelotfad09c72016-06-21 12:28:20 -04001803 if (chip->ports[i].bridge_dev ==
1804 chip->ports[port].bridge_dev)
Vivien Didelotda9c3592016-02-12 12:09:40 -05001805 break; /* same bridge, check next VLAN */
1806
Andrew Lunnc8b09802016-06-04 21:16:57 +02001807 netdev_warn(ds->ports[port].netdev,
Vivien Didelotda9c3592016-02-12 12:09:40 -05001808 "hardware VLAN %d already used by %s\n",
1809 vlan.vid,
Vivien Didelotfad09c72016-06-21 12:28:20 -04001810 netdev_name(chip->ports[i].bridge_dev));
Vivien Didelotda9c3592016-02-12 12:09:40 -05001811 err = -EOPNOTSUPP;
1812 goto unlock;
1813 }
1814 } while (vlan.vid < vid_end);
1815
1816unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001817 mutex_unlock(&chip->reg_lock);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001818
1819 return err;
1820}
1821
Vivien Didelot214cdb92016-02-26 13:16:08 -05001822static const char * const mv88e6xxx_port_8021q_mode_names[] = {
1823 [PORT_CONTROL_2_8021Q_DISABLED] = "Disabled",
1824 [PORT_CONTROL_2_8021Q_FALLBACK] = "Fallback",
1825 [PORT_CONTROL_2_8021Q_CHECK] = "Check",
1826 [PORT_CONTROL_2_8021Q_SECURE] = "Secure",
1827};
1828
Vivien Didelotf81ec902016-05-09 13:22:58 -04001829static int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port,
1830 bool vlan_filtering)
Vivien Didelot214cdb92016-02-26 13:16:08 -05001831{
Vivien Didelot04bed142016-08-31 18:06:13 -04001832 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001833 u16 old, new = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE :
1834 PORT_CONTROL_2_8021Q_DISABLED;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001835 u16 reg;
1836 int err;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001837
Vivien Didelotfad09c72016-06-21 12:28:20 -04001838 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001839 return -EOPNOTSUPP;
1840
Vivien Didelotfad09c72016-06-21 12:28:20 -04001841 mutex_lock(&chip->reg_lock);
Vivien Didelot214cdb92016-02-26 13:16:08 -05001842
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001843 err = mv88e6xxx_port_read(chip, port, PORT_CONTROL_2, &reg);
1844 if (err)
Vivien Didelot214cdb92016-02-26 13:16:08 -05001845 goto unlock;
1846
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001847 old = reg & PORT_CONTROL_2_8021Q_MASK;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001848
Vivien Didelot5220ef12016-03-07 18:24:52 -05001849 if (new != old) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001850 reg &= ~PORT_CONTROL_2_8021Q_MASK;
1851 reg |= new & PORT_CONTROL_2_8021Q_MASK;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001852
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001853 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_2, reg);
1854 if (err)
Vivien Didelot5220ef12016-03-07 18:24:52 -05001855 goto unlock;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001856
Andrew Lunnc8b09802016-06-04 21:16:57 +02001857 netdev_dbg(ds->ports[port].netdev, "802.1Q Mode %s (was %s)\n",
Vivien Didelot5220ef12016-03-07 18:24:52 -05001858 mv88e6xxx_port_8021q_mode_names[new],
1859 mv88e6xxx_port_8021q_mode_names[old]);
1860 }
1861
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001862 err = 0;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001863unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001864 mutex_unlock(&chip->reg_lock);
Vivien Didelot214cdb92016-02-26 13:16:08 -05001865
Andrew Lunn0e7b9922016-09-21 01:40:31 +02001866 return err;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001867}
1868
Vivien Didelot57d32312016-06-20 13:13:58 -04001869static int
1870mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
1871 const struct switchdev_obj_port_vlan *vlan,
1872 struct switchdev_trans *trans)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001873{
Vivien Didelot04bed142016-08-31 18:06:13 -04001874 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotda9c3592016-02-12 12:09:40 -05001875 int err;
1876
Vivien Didelotfad09c72016-06-21 12:28:20 -04001877 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001878 return -EOPNOTSUPP;
1879
Vivien Didelotda9c3592016-02-12 12:09:40 -05001880 /* If the requested port doesn't belong to the same bridge as the VLAN
1881 * members, do not support it (yet) and fallback to software VLAN.
1882 */
1883 err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin,
1884 vlan->vid_end);
1885 if (err)
1886 return err;
1887
Vivien Didelot76e398a2015-11-01 12:33:55 -05001888 /* We don't need any dynamic resource from the kernel (yet),
1889 * so skip the prepare phase.
1890 */
1891 return 0;
1892}
1893
Vivien Didelotfad09c72016-06-21 12:28:20 -04001894static int _mv88e6xxx_port_vlan_add(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001895 u16 vid, bool untagged)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001896{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001897 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001898 int err;
1899
Vivien Didelotfad09c72016-06-21 12:28:20 -04001900 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, true);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001901 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001902 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001903
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001904 vlan.data[port] = untagged ?
1905 GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
1906 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
1907
Vivien Didelotfad09c72016-06-21 12:28:20 -04001908 return _mv88e6xxx_vtu_loadpurge(chip, &vlan);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001909}
1910
Vivien Didelotf81ec902016-05-09 13:22:58 -04001911static void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
1912 const struct switchdev_obj_port_vlan *vlan,
1913 struct switchdev_trans *trans)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001914{
Vivien Didelot04bed142016-08-31 18:06:13 -04001915 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001916 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1917 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1918 u16 vid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001919
Vivien Didelotfad09c72016-06-21 12:28:20 -04001920 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001921 return;
1922
Vivien Didelotfad09c72016-06-21 12:28:20 -04001923 mutex_lock(&chip->reg_lock);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001924
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001925 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
Vivien Didelotfad09c72016-06-21 12:28:20 -04001926 if (_mv88e6xxx_port_vlan_add(chip, port, vid, untagged))
Andrew Lunnc8b09802016-06-04 21:16:57 +02001927 netdev_err(ds->ports[port].netdev,
1928 "failed to add VLAN %d%c\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001929 vid, untagged ? 'u' : 't');
Vivien Didelot76e398a2015-11-01 12:33:55 -05001930
Vivien Didelotfad09c72016-06-21 12:28:20 -04001931 if (pvid && _mv88e6xxx_port_pvid_set(chip, port, vlan->vid_end))
Andrew Lunnc8b09802016-06-04 21:16:57 +02001932 netdev_err(ds->ports[port].netdev, "failed to set PVID %d\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001933 vlan->vid_end);
1934
Vivien Didelotfad09c72016-06-21 12:28:20 -04001935 mutex_unlock(&chip->reg_lock);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001936}
1937
Vivien Didelotfad09c72016-06-21 12:28:20 -04001938static int _mv88e6xxx_port_vlan_del(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001939 int port, u16 vid)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001940{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001941 struct dsa_switch *ds = chip->ds;
Vivien Didelotb4e47c02016-09-29 12:21:58 -04001942 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001943 int i, err;
1944
Vivien Didelotfad09c72016-06-21 12:28:20 -04001945 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001946 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001947 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001948
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001949 /* Tell switchdev if this VLAN is handled in software */
1950 if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
Vivien Didelot3c06f082016-02-05 14:04:39 -05001951 return -EOPNOTSUPP;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001952
1953 vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
1954
1955 /* keep the VLAN unless all ports are excluded */
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001956 vlan.valid = false;
Vivien Didelot370b4ff2016-09-29 12:21:57 -04001957 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelot3d131f02015-11-03 10:52:52 -05001958 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001959 continue;
1960
1961 if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001962 vlan.valid = true;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001963 break;
1964 }
1965 }
1966
Vivien Didelotfad09c72016-06-21 12:28:20 -04001967 err = _mv88e6xxx_vtu_loadpurge(chip, &vlan);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001968 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001969 return err;
1970
Vivien Didelotfad09c72016-06-21 12:28:20 -04001971 return _mv88e6xxx_atu_remove(chip, vlan.fid, port, false);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001972}
1973
Vivien Didelotf81ec902016-05-09 13:22:58 -04001974static int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
1975 const struct switchdev_obj_port_vlan *vlan)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001976{
Vivien Didelot04bed142016-08-31 18:06:13 -04001977 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001978 u16 pvid, vid;
1979 int err = 0;
1980
Vivien Didelotfad09c72016-06-21 12:28:20 -04001981 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001982 return -EOPNOTSUPP;
1983
Vivien Didelotfad09c72016-06-21 12:28:20 -04001984 mutex_lock(&chip->reg_lock);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001985
Vivien Didelotfad09c72016-06-21 12:28:20 -04001986 err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001987 if (err)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001988 goto unlock;
1989
Vivien Didelot76e398a2015-11-01 12:33:55 -05001990 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001991 err = _mv88e6xxx_port_vlan_del(chip, port, vid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001992 if (err)
1993 goto unlock;
1994
1995 if (vid == pvid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001996 err = _mv88e6xxx_port_pvid_set(chip, port, 0);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001997 if (err)
1998 goto unlock;
1999 }
2000 }
2001
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002002unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002003 mutex_unlock(&chip->reg_lock);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002004
2005 return err;
2006}
2007
Vivien Didelotfad09c72016-06-21 12:28:20 -04002008static int _mv88e6xxx_atu_mac_write(struct mv88e6xxx_chip *chip,
Vivien Didelotc5723ac2015-08-10 09:09:48 -04002009 const unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002010{
Vivien Didelota935c052016-09-29 12:21:53 -04002011 int i, err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002012
2013 for (i = 0; i < 3; i++) {
Vivien Didelota935c052016-09-29 12:21:53 -04002014 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_MAC_01 + i,
2015 (addr[i * 2] << 8) | addr[i * 2 + 1]);
2016 if (err)
2017 return err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002018 }
2019
2020 return 0;
2021}
2022
Vivien Didelotfad09c72016-06-21 12:28:20 -04002023static int _mv88e6xxx_atu_mac_read(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04002024 unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002025{
Vivien Didelota935c052016-09-29 12:21:53 -04002026 u16 val;
2027 int i, err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002028
2029 for (i = 0; i < 3; i++) {
Vivien Didelota935c052016-09-29 12:21:53 -04002030 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_MAC_01 + i, &val);
2031 if (err)
2032 return err;
2033
2034 addr[i * 2] = val >> 8;
2035 addr[i * 2 + 1] = val & 0xff;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002036 }
2037
2038 return 0;
2039}
2040
Vivien Didelotfad09c72016-06-21 12:28:20 -04002041static int _mv88e6xxx_atu_load(struct mv88e6xxx_chip *chip,
Vivien Didelotfd231c82015-08-10 09:09:50 -04002042 struct mv88e6xxx_atu_entry *entry)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002043{
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002044 int ret;
2045
Vivien Didelotfad09c72016-06-21 12:28:20 -04002046 ret = _mv88e6xxx_atu_wait(chip);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002047 if (ret < 0)
2048 return ret;
2049
Vivien Didelotfad09c72016-06-21 12:28:20 -04002050 ret = _mv88e6xxx_atu_mac_write(chip, entry->mac);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002051 if (ret < 0)
2052 return ret;
2053
Vivien Didelotfad09c72016-06-21 12:28:20 -04002054 ret = _mv88e6xxx_atu_data_write(chip, entry);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002055 if (ret < 0)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002056 return ret;
2057
Vivien Didelotfad09c72016-06-21 12:28:20 -04002058 return _mv88e6xxx_atu_cmd(chip, entry->fid, GLOBAL_ATU_OP_LOAD_DB);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002059}
David S. Millercdf09692015-08-11 12:00:37 -07002060
Vivien Didelot88472932016-09-19 19:56:11 -04002061static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
2062 struct mv88e6xxx_atu_entry *entry);
2063
2064static int mv88e6xxx_atu_get(struct mv88e6xxx_chip *chip, int fid,
2065 const u8 *addr, struct mv88e6xxx_atu_entry *entry)
2066{
2067 struct mv88e6xxx_atu_entry next;
2068 int err;
2069
2070 eth_broadcast_addr(next.mac);
2071
2072 err = _mv88e6xxx_atu_mac_write(chip, next.mac);
2073 if (err)
2074 return err;
2075
2076 do {
2077 err = _mv88e6xxx_atu_getnext(chip, fid, &next);
2078 if (err)
2079 return err;
2080
2081 if (next.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2082 break;
2083
2084 if (ether_addr_equal(next.mac, addr)) {
2085 *entry = next;
2086 return 0;
2087 }
2088 } while (!is_broadcast_ether_addr(next.mac));
2089
2090 memset(entry, 0, sizeof(*entry));
2091 entry->fid = fid;
2092 ether_addr_copy(entry->mac, addr);
2093
2094 return 0;
2095}
2096
Vivien Didelot83dabd12016-08-31 11:50:04 -04002097static int mv88e6xxx_port_db_load_purge(struct mv88e6xxx_chip *chip, int port,
2098 const unsigned char *addr, u16 vid,
2099 u8 state)
Vivien Didelotfd231c82015-08-10 09:09:50 -04002100{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04002101 struct mv88e6xxx_vtu_entry vlan;
Vivien Didelot88472932016-09-19 19:56:11 -04002102 struct mv88e6xxx_atu_entry entry;
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002103 int err;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002104
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002105 /* Null VLAN ID corresponds to the port private database */
2106 if (vid == 0)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002107 err = _mv88e6xxx_port_fid_get(chip, port, &vlan.fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002108 else
Vivien Didelotfad09c72016-06-21 12:28:20 -04002109 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002110 if (err)
2111 return err;
2112
Vivien Didelot88472932016-09-19 19:56:11 -04002113 err = mv88e6xxx_atu_get(chip, vlan.fid, addr, &entry);
2114 if (err)
2115 return err;
2116
2117 /* Purge the ATU entry only if no port is using it anymore */
2118 if (state == GLOBAL_ATU_DATA_STATE_UNUSED) {
2119 entry.portv_trunkid &= ~BIT(port);
2120 if (!entry.portv_trunkid)
2121 entry.state = GLOBAL_ATU_DATA_STATE_UNUSED;
2122 } else {
2123 entry.portv_trunkid |= BIT(port);
2124 entry.state = state;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002125 }
2126
Vivien Didelotfad09c72016-06-21 12:28:20 -04002127 return _mv88e6xxx_atu_load(chip, &entry);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002128}
2129
Vivien Didelotf81ec902016-05-09 13:22:58 -04002130static int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
2131 const struct switchdev_obj_port_fdb *fdb,
2132 struct switchdev_trans *trans)
Vivien Didelot146a3202015-10-08 11:35:12 -04002133{
2134 /* We don't need any dynamic resource from the kernel (yet),
2135 * so skip the prepare phase.
2136 */
2137 return 0;
2138}
2139
Vivien Didelotf81ec902016-05-09 13:22:58 -04002140static void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
2141 const struct switchdev_obj_port_fdb *fdb,
2142 struct switchdev_trans *trans)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002143{
Vivien Didelot04bed142016-08-31 18:06:13 -04002144 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot6630e232015-08-06 01:44:07 -04002145
Vivien Didelotfad09c72016-06-21 12:28:20 -04002146 mutex_lock(&chip->reg_lock);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002147 if (mv88e6xxx_port_db_load_purge(chip, port, fdb->addr, fdb->vid,
2148 GLOBAL_ATU_DATA_STATE_UC_STATIC))
2149 netdev_err(ds->ports[port].netdev, "failed to load unicast MAC address\n");
Vivien Didelotfad09c72016-06-21 12:28:20 -04002150 mutex_unlock(&chip->reg_lock);
David S. Millercdf09692015-08-11 12:00:37 -07002151}
2152
Vivien Didelotf81ec902016-05-09 13:22:58 -04002153static int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
2154 const struct switchdev_obj_port_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -07002155{
Vivien Didelot04bed142016-08-31 18:06:13 -04002156 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot83dabd12016-08-31 11:50:04 -04002157 int err;
David S. Millercdf09692015-08-11 12:00:37 -07002158
Vivien Didelotfad09c72016-06-21 12:28:20 -04002159 mutex_lock(&chip->reg_lock);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002160 err = mv88e6xxx_port_db_load_purge(chip, port, fdb->addr, fdb->vid,
2161 GLOBAL_ATU_DATA_STATE_UNUSED);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002162 mutex_unlock(&chip->reg_lock);
David S. Millercdf09692015-08-11 12:00:37 -07002163
Vivien Didelot83dabd12016-08-31 11:50:04 -04002164 return err;
David S. Millercdf09692015-08-11 12:00:37 -07002165}
2166
Vivien Didelotfad09c72016-06-21 12:28:20 -04002167static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
Vivien Didelot1d194042015-08-10 09:09:51 -04002168 struct mv88e6xxx_atu_entry *entry)
David S. Millercdf09692015-08-11 12:00:37 -07002169{
Vivien Didelot1d194042015-08-10 09:09:51 -04002170 struct mv88e6xxx_atu_entry next = { 0 };
Vivien Didelota935c052016-09-29 12:21:53 -04002171 u16 val;
2172 int err;
Vivien Didelot1d194042015-08-10 09:09:51 -04002173
2174 next.fid = fid;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002175
Vivien Didelota935c052016-09-29 12:21:53 -04002176 err = _mv88e6xxx_atu_wait(chip);
2177 if (err)
2178 return err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002179
Vivien Didelota935c052016-09-29 12:21:53 -04002180 err = _mv88e6xxx_atu_cmd(chip, fid, GLOBAL_ATU_OP_GET_NEXT_DB);
2181 if (err)
2182 return err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002183
Vivien Didelota935c052016-09-29 12:21:53 -04002184 err = _mv88e6xxx_atu_mac_read(chip, next.mac);
2185 if (err)
2186 return err;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002187
Vivien Didelota935c052016-09-29 12:21:53 -04002188 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_DATA, &val);
2189 if (err)
2190 return err;
Vivien Didelot1d194042015-08-10 09:09:51 -04002191
Vivien Didelota935c052016-09-29 12:21:53 -04002192 next.state = val & GLOBAL_ATU_DATA_STATE_MASK;
Vivien Didelot1d194042015-08-10 09:09:51 -04002193 if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2194 unsigned int mask, shift;
2195
Vivien Didelota935c052016-09-29 12:21:53 -04002196 if (val & GLOBAL_ATU_DATA_TRUNK) {
Vivien Didelot1d194042015-08-10 09:09:51 -04002197 next.trunk = true;
2198 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
2199 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
2200 } else {
2201 next.trunk = false;
2202 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
2203 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
2204 }
2205
Vivien Didelota935c052016-09-29 12:21:53 -04002206 next.portv_trunkid = (val & mask) >> shift;
Vivien Didelot1d194042015-08-10 09:09:51 -04002207 }
2208
2209 *entry = next;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002210 return 0;
2211}
2212
Vivien Didelot83dabd12016-08-31 11:50:04 -04002213static int mv88e6xxx_port_db_dump_fid(struct mv88e6xxx_chip *chip,
2214 u16 fid, u16 vid, int port,
2215 struct switchdev_obj *obj,
2216 int (*cb)(struct switchdev_obj *obj))
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002217{
2218 struct mv88e6xxx_atu_entry addr = {
2219 .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2220 };
2221 int err;
2222
Vivien Didelotfad09c72016-06-21 12:28:20 -04002223 err = _mv88e6xxx_atu_mac_write(chip, addr.mac);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002224 if (err)
2225 return err;
2226
2227 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002228 err = _mv88e6xxx_atu_getnext(chip, fid, &addr);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002229 if (err)
Vivien Didelot83dabd12016-08-31 11:50:04 -04002230 return err;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002231
2232 if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2233 break;
2234
Vivien Didelot83dabd12016-08-31 11:50:04 -04002235 if (addr.trunk || (addr.portv_trunkid & BIT(port)) == 0)
2236 continue;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002237
Vivien Didelot83dabd12016-08-31 11:50:04 -04002238 if (obj->id == SWITCHDEV_OBJ_ID_PORT_FDB) {
2239 struct switchdev_obj_port_fdb *fdb;
2240
2241 if (!is_unicast_ether_addr(addr.mac))
2242 continue;
2243
2244 fdb = SWITCHDEV_OBJ_PORT_FDB(obj);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002245 fdb->vid = vid;
2246 ether_addr_copy(fdb->addr, addr.mac);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002247 if (addr.state == GLOBAL_ATU_DATA_STATE_UC_STATIC)
2248 fdb->ndm_state = NUD_NOARP;
2249 else
2250 fdb->ndm_state = NUD_REACHABLE;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04002251 } else if (obj->id == SWITCHDEV_OBJ_ID_PORT_MDB) {
2252 struct switchdev_obj_port_mdb *mdb;
2253
2254 if (!is_multicast_ether_addr(addr.mac))
2255 continue;
2256
2257 mdb = SWITCHDEV_OBJ_PORT_MDB(obj);
2258 mdb->vid = vid;
2259 ether_addr_copy(mdb->addr, addr.mac);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002260 } else {
2261 return -EOPNOTSUPP;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002262 }
Vivien Didelot83dabd12016-08-31 11:50:04 -04002263
2264 err = cb(obj);
2265 if (err)
2266 return err;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002267 } while (!is_broadcast_ether_addr(addr.mac));
2268
2269 return err;
2270}
2271
Vivien Didelot83dabd12016-08-31 11:50:04 -04002272static int mv88e6xxx_port_db_dump(struct mv88e6xxx_chip *chip, int port,
2273 struct switchdev_obj *obj,
2274 int (*cb)(struct switchdev_obj *obj))
2275{
Vivien Didelotb4e47c02016-09-29 12:21:58 -04002276 struct mv88e6xxx_vtu_entry vlan = {
Vivien Didelot83dabd12016-08-31 11:50:04 -04002277 .vid = GLOBAL_VTU_VID_MASK, /* all ones */
2278 };
2279 u16 fid;
2280 int err;
2281
2282 /* Dump port's default Filtering Information Database (VLAN ID 0) */
2283 err = _mv88e6xxx_port_fid_get(chip, port, &fid);
2284 if (err)
2285 return err;
2286
2287 err = mv88e6xxx_port_db_dump_fid(chip, fid, 0, port, obj, cb);
2288 if (err)
2289 return err;
2290
2291 /* Dump VLANs' Filtering Information Databases */
2292 err = _mv88e6xxx_vtu_vid_write(chip, vlan.vid);
2293 if (err)
2294 return err;
2295
2296 do {
2297 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
2298 if (err)
2299 return err;
2300
2301 if (!vlan.valid)
2302 break;
2303
2304 err = mv88e6xxx_port_db_dump_fid(chip, vlan.fid, vlan.vid, port,
2305 obj, cb);
2306 if (err)
2307 return err;
2308 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
2309
2310 return err;
2311}
2312
Vivien Didelotf81ec902016-05-09 13:22:58 -04002313static int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
2314 struct switchdev_obj_port_fdb *fdb,
2315 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotf33475b2015-10-22 09:34:41 -04002316{
Vivien Didelot04bed142016-08-31 18:06:13 -04002317 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002318 int err;
2319
Vivien Didelotfad09c72016-06-21 12:28:20 -04002320 mutex_lock(&chip->reg_lock);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002321 err = mv88e6xxx_port_db_dump(chip, port, &fdb->obj, cb);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002322 mutex_unlock(&chip->reg_lock);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002323
2324 return err;
2325}
2326
Vivien Didelotf81ec902016-05-09 13:22:58 -04002327static int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
2328 struct net_device *bridge)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002329{
Vivien Didelot04bed142016-08-31 18:06:13 -04002330 struct mv88e6xxx_chip *chip = ds->priv;
Colin Ian King1d9619d2016-04-25 23:11:22 +01002331 int i, err = 0;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002332
Vivien Didelotfad09c72016-06-21 12:28:20 -04002333 mutex_lock(&chip->reg_lock);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002334
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002335 /* Assign the bridge and remap each port's VLANTable */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002336 chip->ports[port].bridge_dev = bridge;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002337
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002338 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002339 if (chip->ports[i].bridge_dev == bridge) {
2340 err = _mv88e6xxx_port_based_vlan_map(chip, i);
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002341 if (err)
2342 break;
2343 }
2344 }
2345
Vivien Didelotfad09c72016-06-21 12:28:20 -04002346 mutex_unlock(&chip->reg_lock);
Vivien Didelota6692752016-02-12 12:09:39 -05002347
Vivien Didelot466dfa02016-02-26 13:16:05 -05002348 return err;
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002349}
2350
Vivien Didelotf81ec902016-05-09 13:22:58 -04002351static void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002352{
Vivien Didelot04bed142016-08-31 18:06:13 -04002353 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002354 struct net_device *bridge = chip->ports[port].bridge_dev;
Vivien Didelot16bfa702016-03-13 16:21:33 -04002355 int i;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002356
Vivien Didelotfad09c72016-06-21 12:28:20 -04002357 mutex_lock(&chip->reg_lock);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002358
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002359 /* Unassign the bridge and remap each port's VLANTable */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002360 chip->ports[port].bridge_dev = NULL;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002361
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002362 for (i = 0; i < mv88e6xxx_num_ports(chip); ++i)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002363 if (i == port || chip->ports[i].bridge_dev == bridge)
2364 if (_mv88e6xxx_port_based_vlan_map(chip, i))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002365 netdev_warn(ds->ports[i].netdev,
2366 "failed to remap\n");
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002367
Vivien Didelotfad09c72016-06-21 12:28:20 -04002368 mutex_unlock(&chip->reg_lock);
Vivien Didelot66d9cd02016-02-05 14:07:14 -05002369}
2370
Vivien Didelotfad09c72016-06-21 12:28:20 -04002371static int mv88e6xxx_switch_reset(struct mv88e6xxx_chip *chip)
Vivien Didelot552238b2016-05-09 13:22:49 -04002372{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002373 bool ppu_active = mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE);
Vivien Didelot552238b2016-05-09 13:22:49 -04002374 u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002375 struct gpio_desc *gpiod = chip->reset;
Vivien Didelot552238b2016-05-09 13:22:49 -04002376 unsigned long timeout;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002377 u16 reg;
Vivien Didelota935c052016-09-29 12:21:53 -04002378 int err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002379 int i;
2380
2381 /* Set all ports to the disabled state. */
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002382 for (i = 0; i < mv88e6xxx_num_ports(chip); i++) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002383 err = mv88e6xxx_port_read(chip, i, PORT_CONTROL, &reg);
2384 if (err)
2385 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002386
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002387 err = mv88e6xxx_port_write(chip, i, PORT_CONTROL,
2388 reg & 0xfffc);
2389 if (err)
2390 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002391 }
2392
2393 /* Wait for transmit queues to drain. */
2394 usleep_range(2000, 4000);
2395
2396 /* If there is a gpio connected to the reset pin, toggle it */
2397 if (gpiod) {
2398 gpiod_set_value_cansleep(gpiod, 1);
2399 usleep_range(10000, 20000);
2400 gpiod_set_value_cansleep(gpiod, 0);
2401 usleep_range(10000, 20000);
2402 }
2403
2404 /* Reset the switch. Keep the PPU active if requested. The PPU
2405 * needs to be active to support indirect phy register access
2406 * through global registers 0x18 and 0x19.
2407 */
2408 if (ppu_active)
Vivien Didelota935c052016-09-29 12:21:53 -04002409 err = mv88e6xxx_g1_write(chip, 0x04, 0xc000);
Vivien Didelot552238b2016-05-09 13:22:49 -04002410 else
Vivien Didelota935c052016-09-29 12:21:53 -04002411 err = mv88e6xxx_g1_write(chip, 0x04, 0xc400);
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002412 if (err)
2413 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002414
2415 /* Wait up to one second for reset to complete. */
2416 timeout = jiffies + 1 * HZ;
2417 while (time_before(jiffies, timeout)) {
Vivien Didelota935c052016-09-29 12:21:53 -04002418 err = mv88e6xxx_g1_read(chip, 0x00, &reg);
2419 if (err)
2420 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002421
Vivien Didelota935c052016-09-29 12:21:53 -04002422 if ((reg & is_reset) == is_reset)
Vivien Didelot552238b2016-05-09 13:22:49 -04002423 break;
2424 usleep_range(1000, 2000);
2425 }
2426 if (time_after(jiffies, timeout))
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002427 err = -ETIMEDOUT;
Vivien Didelot552238b2016-05-09 13:22:49 -04002428 else
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002429 err = 0;
Vivien Didelot552238b2016-05-09 13:22:49 -04002430
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002431 return err;
Vivien Didelot552238b2016-05-09 13:22:49 -04002432}
2433
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002434static int mv88e6xxx_serdes_power_on(struct mv88e6xxx_chip *chip)
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002435{
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002436 u16 val;
2437 int err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002438
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002439 /* Clear Power Down bit */
2440 err = mv88e6xxx_serdes_read(chip, MII_BMCR, &val);
2441 if (err)
2442 return err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002443
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002444 if (val & BMCR_PDOWN) {
2445 val &= ~BMCR_PDOWN;
2446 err = mv88e6xxx_serdes_write(chip, MII_BMCR, val);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002447 }
2448
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002449 return err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002450}
2451
Vivien Didelotfad09c72016-06-21 12:28:20 -04002452static int mv88e6xxx_setup_port(struct mv88e6xxx_chip *chip, int port)
Guenter Roeckd827e882015-03-26 18:36:29 -07002453{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002454 struct dsa_switch *ds = chip->ds;
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002455 int err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002456 u16 reg;
Guenter Roeckd827e882015-03-26 18:36:29 -07002457
Vivien Didelotfad09c72016-06-21 12:28:20 -04002458 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2459 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2460 mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip) ||
2461 mv88e6xxx_6065_family(chip) || mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002462 /* MAC Forcing register: don't force link, speed,
2463 * duplex or flow control state to any particular
2464 * values on physical ports, but force the CPU port
2465 * and all DSA ports to their maximum bandwidth and
2466 * full duplex.
2467 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002468 err = mv88e6xxx_port_read(chip, port, PORT_PCS_CTRL, &reg);
Andrew Lunn60045cb2015-08-17 23:52:51 +02002469 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
Russell King53adc9e2015-09-21 21:42:59 +01002470 reg &= ~PORT_PCS_CTRL_UNFORCED;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002471 reg |= PORT_PCS_CTRL_FORCE_LINK |
2472 PORT_PCS_CTRL_LINK_UP |
2473 PORT_PCS_CTRL_DUPLEX_FULL |
2474 PORT_PCS_CTRL_FORCE_DUPLEX;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002475 if (mv88e6xxx_6065_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002476 reg |= PORT_PCS_CTRL_100;
2477 else
2478 reg |= PORT_PCS_CTRL_1000;
2479 } else {
2480 reg |= PORT_PCS_CTRL_UNFORCED;
2481 }
2482
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002483 err = mv88e6xxx_port_write(chip, port, PORT_PCS_CTRL, reg);
2484 if (err)
2485 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002486 }
2487
2488 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
2489 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
2490 * tunneling, determine priority by looking at 802.1p and IP
2491 * priority fields (IP prio has precedence), and set STP state
2492 * to Forwarding.
2493 *
2494 * If this is the CPU link, use DSA or EDSA tagging depending
2495 * on which tagging mode was configured.
2496 *
2497 * If this is a link to another switch, use DSA tagging mode.
2498 *
2499 * If this is the upstream port for this switch, enable
2500 * forwarding of unknown unicasts and multicasts.
2501 */
2502 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002503 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2504 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2505 mv88e6xxx_6095_family(chip) || mv88e6xxx_6065_family(chip) ||
2506 mv88e6xxx_6185_family(chip) || mv88e6xxx_6320_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002507 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
2508 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
2509 PORT_CONTROL_STATE_FORWARDING;
2510 if (dsa_is_cpu_port(ds, port)) {
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002511 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA))
Andrew Lunn5377b802016-06-04 21:17:02 +02002512 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA |
Andrew Lunnc047a1f2015-09-29 01:50:56 +02002513 PORT_CONTROL_FORWARD_UNKNOWN_MC;
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002514 else
2515 reg |= PORT_CONTROL_DSA_TAG;
Jamie Lentinf027e0c2016-08-22 16:01:04 +02002516 reg |= PORT_CONTROL_EGRESS_ADD_TAG |
2517 PORT_CONTROL_FORWARD_UNKNOWN;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002518 }
Andrew Lunn6083ce72015-08-17 23:52:52 +02002519 if (dsa_is_dsa_port(ds, port)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002520 if (mv88e6xxx_6095_family(chip) ||
2521 mv88e6xxx_6185_family(chip))
Andrew Lunn6083ce72015-08-17 23:52:52 +02002522 reg |= PORT_CONTROL_DSA_TAG;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002523 if (mv88e6xxx_6352_family(chip) ||
2524 mv88e6xxx_6351_family(chip) ||
2525 mv88e6xxx_6165_family(chip) ||
2526 mv88e6xxx_6097_family(chip) ||
2527 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002528 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunn6083ce72015-08-17 23:52:52 +02002529 }
2530
Andrew Lunn54d792f2015-05-06 01:09:47 +02002531 if (port == dsa_upstream_port(ds))
2532 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2533 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2534 }
2535 if (reg) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002536 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL, reg);
2537 if (err)
2538 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002539 }
2540
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002541 /* If this port is connected to a SerDes, make sure the SerDes is not
2542 * powered down.
2543 */
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002544 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_SERDES)) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002545 err = mv88e6xxx_port_read(chip, port, PORT_STATUS, &reg);
2546 if (err)
2547 return err;
2548 reg &= PORT_STATUS_CMODE_MASK;
2549 if ((reg == PORT_STATUS_CMODE_100BASE_X) ||
2550 (reg == PORT_STATUS_CMODE_1000BASE_X) ||
2551 (reg == PORT_STATUS_CMODE_SGMII)) {
2552 err = mv88e6xxx_serdes_power_on(chip);
2553 if (err < 0)
2554 return err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002555 }
2556 }
2557
Vivien Didelot8efdda42015-08-13 12:52:23 -04002558 /* Port Control 2: don't force a good FCS, set the maximum frame size to
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002559 * 10240 bytes, disable 802.1q tags checking, don't discard tagged or
Vivien Didelot8efdda42015-08-13 12:52:23 -04002560 * untagged frames on this port, do a destination address lookup on all
2561 * received packets as usual, disable ARP mirroring and don't send a
2562 * copy of all transmitted/received frames on this port to the CPU.
Andrew Lunn54d792f2015-05-06 01:09:47 +02002563 */
2564 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002565 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2566 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2567 mv88e6xxx_6095_family(chip) || mv88e6xxx_6320_family(chip) ||
2568 mv88e6xxx_6185_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002569 reg = PORT_CONTROL_2_MAP_DA;
2570
Vivien Didelotfad09c72016-06-21 12:28:20 -04002571 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2572 mv88e6xxx_6165_family(chip) || mv88e6xxx_6320_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002573 reg |= PORT_CONTROL_2_JUMBO_10240;
2574
Vivien Didelotfad09c72016-06-21 12:28:20 -04002575 if (mv88e6xxx_6095_family(chip) || mv88e6xxx_6185_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002576 /* Set the upstream port this port should use */
2577 reg |= dsa_upstream_port(ds);
2578 /* enable forwarding of unknown multicast addresses to
2579 * the upstream port
2580 */
2581 if (port == dsa_upstream_port(ds))
2582 reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
2583 }
2584
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002585 reg |= PORT_CONTROL_2_8021Q_DISABLED;
Vivien Didelot8efdda42015-08-13 12:52:23 -04002586
Andrew Lunn54d792f2015-05-06 01:09:47 +02002587 if (reg) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002588 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_2, reg);
2589 if (err)
2590 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002591 }
2592
2593 /* Port Association Vector: when learning source addresses
2594 * of packets, add the address to the address database using
2595 * a port bitmap that has only the bit for this port set and
2596 * the other bits clear.
2597 */
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002598 reg = 1 << port;
Vivien Didelot996ecb82016-04-14 14:42:08 -04002599 /* Disable learning for CPU port */
2600 if (dsa_is_cpu_port(ds, port))
Vivien Didelot65fa4022016-04-14 14:42:07 -04002601 reg = 0;
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002602
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002603 err = mv88e6xxx_port_write(chip, port, PORT_ASSOC_VECTOR, reg);
2604 if (err)
2605 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002606
2607 /* Egress rate control 2: disable egress rate control. */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002608 err = mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL_2, 0x0000);
2609 if (err)
2610 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002611
Vivien Didelotfad09c72016-06-21 12:28:20 -04002612 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2613 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2614 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002615 /* Do not limit the period of time that this port can
2616 * be paused for by the remote end or the period of
2617 * time that this port can pause the remote end.
2618 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002619 err = mv88e6xxx_port_write(chip, port, PORT_PAUSE_CTRL, 0x0000);
2620 if (err)
2621 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002622
2623 /* Port ATU control: disable limiting the number of
2624 * address database entries that this port is allowed
2625 * to use.
2626 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002627 err = mv88e6xxx_port_write(chip, port, PORT_ATU_CONTROL,
2628 0x0000);
Andrew Lunn54d792f2015-05-06 01:09:47 +02002629 /* Priority Override: disable DA, SA and VTU priority
2630 * override.
2631 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002632 err = mv88e6xxx_port_write(chip, port, PORT_PRI_OVERRIDE,
2633 0x0000);
2634 if (err)
2635 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002636
2637 /* Port Ethertype: use the Ethertype DSA Ethertype
2638 * value.
2639 */
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002640 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA)) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002641 err = mv88e6xxx_port_write(chip, port, PORT_ETH_TYPE,
2642 ETH_P_EDSA);
2643 if (err)
2644 return err;
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002645 }
2646
Andrew Lunn54d792f2015-05-06 01:09:47 +02002647 /* Tag Remap: use an identity 802.1p prio -> switch
2648 * prio mapping.
2649 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002650 err = mv88e6xxx_port_write(chip, port, PORT_TAG_REGMAP_0123,
2651 0x3210);
2652 if (err)
2653 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002654
2655 /* Tag Remap 2: use an identity 802.1p prio -> switch
2656 * prio mapping.
2657 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002658 err = mv88e6xxx_port_write(chip, port, PORT_TAG_REGMAP_4567,
2659 0x7654);
2660 if (err)
2661 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002662 }
2663
Jamie Lentin1bc261f2016-08-22 22:47:08 +01002664 /* Rate Control: disable ingress rate limiting. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002665 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2666 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
Vivien Didelotfad09c72016-06-21 12:28:20 -04002667 mv88e6xxx_6320_family(chip)) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002668 err = mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL,
2669 0x0001);
2670 if (err)
2671 return err;
Jamie Lentin1bc261f2016-08-22 22:47:08 +01002672 } else if (mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip)) {
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002673 err = mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL,
2674 0x0000);
2675 if (err)
2676 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002677 }
2678
Guenter Roeck366f0a02015-03-26 18:36:30 -07002679 /* Port Control 1: disable trunking, disable sending
2680 * learning messages to this port.
Guenter Roeckd827e882015-03-26 18:36:29 -07002681 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002682 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_1, 0x0000);
2683 if (err)
2684 return err;
Guenter Roeckd827e882015-03-26 18:36:29 -07002685
Vivien Didelot207afda2016-04-14 14:42:09 -04002686 /* Port based VLAN map: give each port the same default address
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002687 * database, and allow bidirectional communication between the
2688 * CPU and DSA port(s), and the other ports.
Guenter Roeckd827e882015-03-26 18:36:29 -07002689 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002690 err = _mv88e6xxx_port_fid_set(chip, port, 0);
2691 if (err)
2692 return err;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002693
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002694 err = _mv88e6xxx_port_based_vlan_map(chip, port);
2695 if (err)
2696 return err;
Guenter Roeckd827e882015-03-26 18:36:29 -07002697
2698 /* Default VLAN ID and priority: don't set a default VLAN
2699 * ID, and set the default packet priority to zero.
2700 */
Andrew Lunn0e7b9922016-09-21 01:40:31 +02002701 return mv88e6xxx_port_write(chip, port, PORT_DEFAULT_VLAN, 0x0000);
Andrew Lunndbde9e62015-05-06 01:09:48 +02002702}
2703
Vivien Didelota935c052016-09-29 12:21:53 -04002704int mv88e6xxx_g1_set_switch_mac(struct mv88e6xxx_chip *chip, u8 *addr)
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002705{
2706 int err;
2707
Vivien Didelota935c052016-09-29 12:21:53 -04002708 err = mv88e6xxx_g1_write(chip, GLOBAL_MAC_01, (addr[0] << 8) | addr[1]);
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002709 if (err)
2710 return err;
2711
Vivien Didelota935c052016-09-29 12:21:53 -04002712 err = mv88e6xxx_g1_write(chip, GLOBAL_MAC_23, (addr[2] << 8) | addr[3]);
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002713 if (err)
2714 return err;
2715
Vivien Didelota935c052016-09-29 12:21:53 -04002716 err = mv88e6xxx_g1_write(chip, GLOBAL_MAC_45, (addr[4] << 8) | addr[5]);
2717 if (err)
2718 return err;
2719
2720 return 0;
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002721}
2722
Vivien Didelotacddbd22016-07-18 20:45:39 -04002723static int mv88e6xxx_g1_set_age_time(struct mv88e6xxx_chip *chip,
2724 unsigned int msecs)
2725{
2726 const unsigned int coeff = chip->info->age_time_coeff;
2727 const unsigned int min = 0x01 * coeff;
2728 const unsigned int max = 0xff * coeff;
2729 u8 age_time;
2730 u16 val;
2731 int err;
2732
2733 if (msecs < min || msecs > max)
2734 return -ERANGE;
2735
2736 /* Round to nearest multiple of coeff */
2737 age_time = (msecs + coeff / 2) / coeff;
2738
Vivien Didelota935c052016-09-29 12:21:53 -04002739 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_CONTROL, &val);
Vivien Didelotacddbd22016-07-18 20:45:39 -04002740 if (err)
2741 return err;
2742
2743 /* AgeTime is 11:4 bits */
2744 val &= ~0xff0;
2745 val |= age_time << 4;
2746
Vivien Didelota935c052016-09-29 12:21:53 -04002747 return mv88e6xxx_g1_write(chip, GLOBAL_ATU_CONTROL, val);
Vivien Didelotacddbd22016-07-18 20:45:39 -04002748}
2749
Vivien Didelot2cfcd962016-07-18 20:45:40 -04002750static int mv88e6xxx_set_ageing_time(struct dsa_switch *ds,
2751 unsigned int ageing_time)
2752{
Vivien Didelot04bed142016-08-31 18:06:13 -04002753 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot2cfcd962016-07-18 20:45:40 -04002754 int err;
2755
2756 mutex_lock(&chip->reg_lock);
2757 err = mv88e6xxx_g1_set_age_time(chip, ageing_time);
2758 mutex_unlock(&chip->reg_lock);
2759
2760 return err;
2761}
2762
Vivien Didelot97299342016-07-18 20:45:30 -04002763static int mv88e6xxx_g1_setup(struct mv88e6xxx_chip *chip)
Vivien Didelot08a01262016-05-09 13:22:50 -04002764{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002765 struct dsa_switch *ds = chip->ds;
Vivien Didelotb0745e872016-05-09 13:22:53 -04002766 u32 upstream_port = dsa_upstream_port(ds);
Vivien Didelot119477b2016-05-09 13:22:51 -04002767 u16 reg;
Vivien Didelot08a01262016-05-09 13:22:50 -04002768 int err;
Vivien Didelot08a01262016-05-09 13:22:50 -04002769
Vivien Didelot119477b2016-05-09 13:22:51 -04002770 /* Enable the PHY Polling Unit if present, don't discard any packets,
2771 * and mask all interrupt sources.
2772 */
2773 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002774 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU) ||
2775 mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE))
Vivien Didelot119477b2016-05-09 13:22:51 -04002776 reg |= GLOBAL_CONTROL_PPU_ENABLE;
2777
Vivien Didelota935c052016-09-29 12:21:53 -04002778 err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL, reg);
Vivien Didelot119477b2016-05-09 13:22:51 -04002779 if (err)
2780 return err;
2781
Vivien Didelotb0745e872016-05-09 13:22:53 -04002782 /* Configure the upstream port, and configure it as the port to which
2783 * ingress and egress and ARP monitor frames are to be sent.
2784 */
2785 reg = upstream_port << GLOBAL_MONITOR_CONTROL_INGRESS_SHIFT |
2786 upstream_port << GLOBAL_MONITOR_CONTROL_EGRESS_SHIFT |
2787 upstream_port << GLOBAL_MONITOR_CONTROL_ARP_SHIFT;
Vivien Didelota935c052016-09-29 12:21:53 -04002788 err = mv88e6xxx_g1_write(chip, GLOBAL_MONITOR_CONTROL, reg);
Vivien Didelotb0745e872016-05-09 13:22:53 -04002789 if (err)
2790 return err;
2791
Vivien Didelot50484ff2016-05-09 13:22:54 -04002792 /* Disable remote management, and set the switch's DSA device number. */
Vivien Didelota935c052016-09-29 12:21:53 -04002793 err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL_2,
2794 GLOBAL_CONTROL_2_MULTIPLE_CASCADE |
2795 (ds->index & 0x1f));
Vivien Didelot50484ff2016-05-09 13:22:54 -04002796 if (err)
2797 return err;
2798
Vivien Didelotacddbd22016-07-18 20:45:39 -04002799 /* Clear all the VTU and STU entries */
2800 err = _mv88e6xxx_vtu_stu_flush(chip);
2801 if (err < 0)
2802 return err;
2803
Vivien Didelot08a01262016-05-09 13:22:50 -04002804 /* Set the default address aging time to 5 minutes, and
2805 * enable address learn messages to be sent to all message
2806 * ports.
2807 */
Vivien Didelota935c052016-09-29 12:21:53 -04002808 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_CONTROL,
2809 GLOBAL_ATU_CONTROL_LEARN2ALL);
Vivien Didelot08a01262016-05-09 13:22:50 -04002810 if (err)
2811 return err;
2812
Vivien Didelotacddbd22016-07-18 20:45:39 -04002813 err = mv88e6xxx_g1_set_age_time(chip, 300000);
2814 if (err)
Vivien Didelot97299342016-07-18 20:45:30 -04002815 return err;
2816
2817 /* Clear all ATU entries */
2818 err = _mv88e6xxx_atu_flush(chip, 0, true);
2819 if (err)
2820 return err;
2821
Vivien Didelot08a01262016-05-09 13:22:50 -04002822 /* Configure the IP ToS mapping registers. */
Vivien Didelota935c052016-09-29 12:21:53 -04002823 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_0, 0x0000);
Vivien Didelot08a01262016-05-09 13:22:50 -04002824 if (err)
2825 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002826 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_1, 0x0000);
Vivien Didelot08a01262016-05-09 13:22:50 -04002827 if (err)
2828 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002829 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_2, 0x5555);
Vivien Didelot08a01262016-05-09 13:22:50 -04002830 if (err)
2831 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002832 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_3, 0x5555);
Vivien Didelot08a01262016-05-09 13:22:50 -04002833 if (err)
2834 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002835 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_4, 0xaaaa);
Vivien Didelot08a01262016-05-09 13:22:50 -04002836 if (err)
2837 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002838 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_5, 0xaaaa);
Vivien Didelot08a01262016-05-09 13:22:50 -04002839 if (err)
2840 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002841 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_6, 0xffff);
Vivien Didelot08a01262016-05-09 13:22:50 -04002842 if (err)
2843 return err;
Vivien Didelota935c052016-09-29 12:21:53 -04002844 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_7, 0xffff);
Vivien Didelot08a01262016-05-09 13:22:50 -04002845 if (err)
2846 return err;
2847
2848 /* Configure the IEEE 802.1p priority mapping register. */
Vivien Didelota935c052016-09-29 12:21:53 -04002849 err = mv88e6xxx_g1_write(chip, GLOBAL_IEEE_PRI, 0xfa41);
Vivien Didelot08a01262016-05-09 13:22:50 -04002850 if (err)
2851 return err;
2852
Vivien Didelot97299342016-07-18 20:45:30 -04002853 /* Clear the statistics counters for all ports */
Vivien Didelota935c052016-09-29 12:21:53 -04002854 err = mv88e6xxx_g1_write(chip, GLOBAL_STATS_OP,
2855 GLOBAL_STATS_OP_FLUSH_ALL);
Vivien Didelot97299342016-07-18 20:45:30 -04002856 if (err)
2857 return err;
2858
2859 /* Wait for the flush to complete. */
2860 err = _mv88e6xxx_stats_wait(chip);
2861 if (err)
2862 return err;
2863
2864 return 0;
2865}
2866
Vivien Didelotf81ec902016-05-09 13:22:58 -04002867static int mv88e6xxx_setup(struct dsa_switch *ds)
Guenter Roeckacdaffc2015-03-26 18:36:28 -07002868{
Vivien Didelot04bed142016-08-31 18:06:13 -04002869 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot552238b2016-05-09 13:22:49 -04002870 int err;
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002871 int i;
2872
Vivien Didelotfad09c72016-06-21 12:28:20 -04002873 chip->ds = ds;
2874 ds->slave_mii_bus = chip->mdio_bus;
Vivien Didelot552238b2016-05-09 13:22:49 -04002875
Vivien Didelotfad09c72016-06-21 12:28:20 -04002876 mutex_lock(&chip->reg_lock);
Vivien Didelot552238b2016-05-09 13:22:49 -04002877
Vivien Didelotfad09c72016-06-21 12:28:20 -04002878 err = mv88e6xxx_switch_reset(chip);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002879 if (err)
2880 goto unlock;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002881
Vivien Didelot97299342016-07-18 20:45:30 -04002882 /* Setup Switch Port Registers */
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002883 for (i = 0; i < mv88e6xxx_num_ports(chip); i++) {
Vivien Didelot97299342016-07-18 20:45:30 -04002884 err = mv88e6xxx_setup_port(chip, i);
2885 if (err)
2886 goto unlock;
2887 }
2888
2889 /* Setup Switch Global 1 Registers */
2890 err = mv88e6xxx_g1_setup(chip);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002891 if (err)
2892 goto unlock;
2893
Vivien Didelot97299342016-07-18 20:45:30 -04002894 /* Setup Switch Global 2 Registers */
2895 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_GLOBAL2)) {
2896 err = mv88e6xxx_g2_setup(chip);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002897 if (err)
2898 goto unlock;
2899 }
Andrew Lunn54d792f2015-05-06 01:09:47 +02002900
Vivien Didelot6b17e862015-08-13 12:52:18 -04002901unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002902 mutex_unlock(&chip->reg_lock);
Andrew Lunndb687a52015-06-20 21:31:29 +02002903
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002904 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002905}
2906
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002907static int mv88e6xxx_set_addr(struct dsa_switch *ds, u8 *addr)
2908{
Vivien Didelot04bed142016-08-31 18:06:13 -04002909 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002910 int err;
2911
2912 mutex_lock(&chip->reg_lock);
2913
2914 /* Has an indirect Switch MAC/WoL/WoF register in Global 2? */
2915 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_SWITCH_MAC))
2916 err = mv88e6xxx_g2_set_switch_mac(chip, addr);
2917 else
2918 err = mv88e6xxx_g1_set_switch_mac(chip, addr);
2919
2920 mutex_unlock(&chip->reg_lock);
2921
2922 return err;
2923}
2924
Vivien Didelote57e5e72016-08-15 17:19:00 -04002925static int mv88e6xxx_mdio_read(struct mii_bus *bus, int phy, int reg)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002926{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002927 struct mv88e6xxx_chip *chip = bus->priv;
Vivien Didelote57e5e72016-08-15 17:19:00 -04002928 u16 val;
2929 int err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002930
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002931 if (phy >= mv88e6xxx_num_ports(chip))
Andrew Lunn158bc062016-04-28 21:24:06 -04002932 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002933
Vivien Didelotfad09c72016-06-21 12:28:20 -04002934 mutex_lock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04002935 err = mv88e6xxx_phy_read(chip, phy, reg, &val);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002936 mutex_unlock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04002937
2938 return err ? err : val;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002939}
2940
Vivien Didelote57e5e72016-08-15 17:19:00 -04002941static int mv88e6xxx_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002942{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002943 struct mv88e6xxx_chip *chip = bus->priv;
Vivien Didelote57e5e72016-08-15 17:19:00 -04002944 int err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002945
Vivien Didelot370b4ff2016-09-29 12:21:57 -04002946 if (phy >= mv88e6xxx_num_ports(chip))
Andrew Lunn158bc062016-04-28 21:24:06 -04002947 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002948
Vivien Didelotfad09c72016-06-21 12:28:20 -04002949 mutex_lock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04002950 err = mv88e6xxx_phy_write(chip, phy, reg, val);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002951 mutex_unlock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04002952
2953 return err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002954}
2955
Vivien Didelotfad09c72016-06-21 12:28:20 -04002956static int mv88e6xxx_mdio_register(struct mv88e6xxx_chip *chip,
Andrew Lunnb516d452016-06-04 21:17:06 +02002957 struct device_node *np)
2958{
2959 static int index;
2960 struct mii_bus *bus;
2961 int err;
2962
Andrew Lunnb516d452016-06-04 21:17:06 +02002963 if (np)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002964 chip->mdio_np = of_get_child_by_name(np, "mdio");
Andrew Lunnb516d452016-06-04 21:17:06 +02002965
Vivien Didelotfad09c72016-06-21 12:28:20 -04002966 bus = devm_mdiobus_alloc(chip->dev);
Andrew Lunnb516d452016-06-04 21:17:06 +02002967 if (!bus)
2968 return -ENOMEM;
2969
Vivien Didelotfad09c72016-06-21 12:28:20 -04002970 bus->priv = (void *)chip;
Andrew Lunnb516d452016-06-04 21:17:06 +02002971 if (np) {
2972 bus->name = np->full_name;
2973 snprintf(bus->id, MII_BUS_ID_SIZE, "%s", np->full_name);
2974 } else {
2975 bus->name = "mv88e6xxx SMI";
2976 snprintf(bus->id, MII_BUS_ID_SIZE, "mv88e6xxx-%d", index++);
2977 }
2978
2979 bus->read = mv88e6xxx_mdio_read;
2980 bus->write = mv88e6xxx_mdio_write;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002981 bus->parent = chip->dev;
Andrew Lunnb516d452016-06-04 21:17:06 +02002982
Vivien Didelotfad09c72016-06-21 12:28:20 -04002983 if (chip->mdio_np)
2984 err = of_mdiobus_register(bus, chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02002985 else
2986 err = mdiobus_register(bus);
2987 if (err) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002988 dev_err(chip->dev, "Cannot register MDIO bus (%d)\n", err);
Andrew Lunnb516d452016-06-04 21:17:06 +02002989 goto out;
2990 }
Vivien Didelotfad09c72016-06-21 12:28:20 -04002991 chip->mdio_bus = bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02002992
2993 return 0;
2994
2995out:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002996 if (chip->mdio_np)
2997 of_node_put(chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02002998
2999 return err;
3000}
3001
Vivien Didelotfad09c72016-06-21 12:28:20 -04003002static void mv88e6xxx_mdio_unregister(struct mv88e6xxx_chip *chip)
Andrew Lunnb516d452016-06-04 21:17:06 +02003003
3004{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003005 struct mii_bus *bus = chip->mdio_bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003006
3007 mdiobus_unregister(bus);
3008
Vivien Didelotfad09c72016-06-21 12:28:20 -04003009 if (chip->mdio_np)
3010 of_node_put(chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003011}
3012
Guenter Roeckc22995c2015-07-25 09:42:28 -07003013#ifdef CONFIG_NET_DSA_HWMON
3014
3015static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
3016{
Vivien Didelot04bed142016-08-31 18:06:13 -04003017 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot9c938292016-08-15 17:19:02 -04003018 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003019 int ret;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003020
3021 *temp = 0;
3022
Vivien Didelotfad09c72016-06-21 12:28:20 -04003023 mutex_lock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003024
Vivien Didelot9c938292016-08-15 17:19:02 -04003025 ret = mv88e6xxx_phy_write(chip, 0x0, 0x16, 0x6);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003026 if (ret < 0)
3027 goto error;
3028
3029 /* Enable temperature sensor */
Vivien Didelot9c938292016-08-15 17:19:02 -04003030 ret = mv88e6xxx_phy_read(chip, 0x0, 0x1a, &val);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003031 if (ret < 0)
3032 goto error;
3033
Vivien Didelot9c938292016-08-15 17:19:02 -04003034 ret = mv88e6xxx_phy_write(chip, 0x0, 0x1a, val | (1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003035 if (ret < 0)
3036 goto error;
3037
3038 /* Wait for temperature to stabilize */
3039 usleep_range(10000, 12000);
3040
Vivien Didelot9c938292016-08-15 17:19:02 -04003041 ret = mv88e6xxx_phy_read(chip, 0x0, 0x1a, &val);
3042 if (ret < 0)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003043 goto error;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003044
3045 /* Disable temperature sensor */
Vivien Didelot9c938292016-08-15 17:19:02 -04003046 ret = mv88e6xxx_phy_write(chip, 0x0, 0x1a, val & ~(1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003047 if (ret < 0)
3048 goto error;
3049
3050 *temp = ((val & 0x1f) - 5) * 5;
3051
3052error:
Vivien Didelot9c938292016-08-15 17:19:02 -04003053 mv88e6xxx_phy_write(chip, 0x0, 0x16, 0x0);
Vivien Didelotfad09c72016-06-21 12:28:20 -04003054 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003055 return ret;
3056}
3057
3058static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
3059{
Vivien Didelot04bed142016-08-31 18:06:13 -04003060 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003061 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003062 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003063 int ret;
3064
3065 *temp = 0;
3066
Vivien Didelot9c938292016-08-15 17:19:02 -04003067 mutex_lock(&chip->reg_lock);
3068 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 27, &val);
3069 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003070 if (ret < 0)
3071 return ret;
3072
Vivien Didelot9c938292016-08-15 17:19:02 -04003073 *temp = (val & 0xff) - 25;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003074
3075 return 0;
3076}
3077
Vivien Didelotf81ec902016-05-09 13:22:58 -04003078static int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003079{
Vivien Didelot04bed142016-08-31 18:06:13 -04003080 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn158bc062016-04-28 21:24:06 -04003081
Vivien Didelotfad09c72016-06-21 12:28:20 -04003082 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP))
Vivien Didelot6594f612016-05-09 13:22:42 -04003083 return -EOPNOTSUPP;
3084
Vivien Didelotfad09c72016-06-21 12:28:20 -04003085 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003086 return mv88e63xx_get_temp(ds, temp);
3087
3088 return mv88e61xx_get_temp(ds, temp);
3089}
3090
Vivien Didelotf81ec902016-05-09 13:22:58 -04003091static int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003092{
Vivien Didelot04bed142016-08-31 18:06:13 -04003093 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003094 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003095 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003096 int ret;
3097
Vivien Didelotfad09c72016-06-21 12:28:20 -04003098 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003099 return -EOPNOTSUPP;
3100
3101 *temp = 0;
3102
Vivien Didelot9c938292016-08-15 17:19:02 -04003103 mutex_lock(&chip->reg_lock);
3104 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3105 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003106 if (ret < 0)
3107 return ret;
3108
Vivien Didelot9c938292016-08-15 17:19:02 -04003109 *temp = (((val >> 8) & 0x1f) * 5) - 25;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003110
3111 return 0;
3112}
3113
Vivien Didelotf81ec902016-05-09 13:22:58 -04003114static int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003115{
Vivien Didelot04bed142016-08-31 18:06:13 -04003116 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003117 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003118 u16 val;
3119 int err;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003120
Vivien Didelotfad09c72016-06-21 12:28:20 -04003121 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003122 return -EOPNOTSUPP;
3123
Vivien Didelot9c938292016-08-15 17:19:02 -04003124 mutex_lock(&chip->reg_lock);
3125 err = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3126 if (err)
3127 goto unlock;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003128 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
Vivien Didelot9c938292016-08-15 17:19:02 -04003129 err = mv88e6xxx_phy_page_write(chip, phy, 6, 26,
3130 (val & 0xe0ff) | (temp << 8));
3131unlock:
3132 mutex_unlock(&chip->reg_lock);
3133
3134 return err;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003135}
3136
Vivien Didelotf81ec902016-05-09 13:22:58 -04003137static int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003138{
Vivien Didelot04bed142016-08-31 18:06:13 -04003139 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003140 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003141 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003142 int ret;
3143
Vivien Didelotfad09c72016-06-21 12:28:20 -04003144 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003145 return -EOPNOTSUPP;
3146
3147 *alarm = false;
3148
Vivien Didelot9c938292016-08-15 17:19:02 -04003149 mutex_lock(&chip->reg_lock);
3150 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3151 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003152 if (ret < 0)
3153 return ret;
3154
Vivien Didelot9c938292016-08-15 17:19:02 -04003155 *alarm = !!(val & 0x40);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003156
3157 return 0;
3158}
3159#endif /* CONFIG_NET_DSA_HWMON */
3160
Vivien Didelot855b1932016-07-20 18:18:35 -04003161static int mv88e6xxx_get_eeprom_len(struct dsa_switch *ds)
3162{
Vivien Didelot04bed142016-08-31 18:06:13 -04003163 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot855b1932016-07-20 18:18:35 -04003164
3165 return chip->eeprom_len;
3166}
3167
Vivien Didelot855b1932016-07-20 18:18:35 -04003168static int mv88e6xxx_get_eeprom(struct dsa_switch *ds,
3169 struct ethtool_eeprom *eeprom, u8 *data)
3170{
Vivien Didelot04bed142016-08-31 18:06:13 -04003171 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot855b1932016-07-20 18:18:35 -04003172 int err;
3173
3174 mutex_lock(&chip->reg_lock);
3175
3176 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16))
Vivien Didelotec561272016-09-02 14:45:33 -04003177 err = mv88e6xxx_g2_get_eeprom16(chip, eeprom, data);
Vivien Didelot855b1932016-07-20 18:18:35 -04003178 else
3179 err = -EOPNOTSUPP;
3180
3181 mutex_unlock(&chip->reg_lock);
3182
3183 if (err)
3184 return err;
3185
3186 eeprom->magic = 0xc3ec4951;
3187
3188 return 0;
3189}
3190
Vivien Didelot855b1932016-07-20 18:18:35 -04003191static int mv88e6xxx_set_eeprom(struct dsa_switch *ds,
3192 struct ethtool_eeprom *eeprom, u8 *data)
3193{
Vivien Didelot04bed142016-08-31 18:06:13 -04003194 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot855b1932016-07-20 18:18:35 -04003195 int err;
3196
3197 if (eeprom->magic != 0xc3ec4951)
3198 return -EINVAL;
3199
3200 mutex_lock(&chip->reg_lock);
3201
3202 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16))
Vivien Didelotec561272016-09-02 14:45:33 -04003203 err = mv88e6xxx_g2_set_eeprom16(chip, eeprom, data);
Vivien Didelot855b1932016-07-20 18:18:35 -04003204 else
3205 err = -EOPNOTSUPP;
3206
3207 mutex_unlock(&chip->reg_lock);
3208
3209 return err;
3210}
3211
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003212static const struct mv88e6xxx_ops mv88e6085_ops = {
3213 .phy_read = mv88e6xxx_phy_ppu_read,
3214 .phy_write = mv88e6xxx_phy_ppu_write,
3215};
3216
3217static const struct mv88e6xxx_ops mv88e6095_ops = {
3218 .phy_read = mv88e6xxx_phy_ppu_read,
3219 .phy_write = mv88e6xxx_phy_ppu_write,
3220};
3221
3222static const struct mv88e6xxx_ops mv88e6123_ops = {
3223 .phy_read = mv88e6xxx_read,
3224 .phy_write = mv88e6xxx_write,
3225};
3226
3227static const struct mv88e6xxx_ops mv88e6131_ops = {
3228 .phy_read = mv88e6xxx_phy_ppu_read,
3229 .phy_write = mv88e6xxx_phy_ppu_write,
3230};
3231
3232static const struct mv88e6xxx_ops mv88e6161_ops = {
3233 .phy_read = mv88e6xxx_read,
3234 .phy_write = mv88e6xxx_write,
3235};
3236
3237static const struct mv88e6xxx_ops mv88e6165_ops = {
3238 .phy_read = mv88e6xxx_read,
3239 .phy_write = mv88e6xxx_write,
3240};
3241
3242static const struct mv88e6xxx_ops mv88e6171_ops = {
3243 .phy_read = mv88e6xxx_g2_smi_phy_read,
3244 .phy_write = mv88e6xxx_g2_smi_phy_write,
3245};
3246
3247static const struct mv88e6xxx_ops mv88e6172_ops = {
3248 .phy_read = mv88e6xxx_g2_smi_phy_read,
3249 .phy_write = mv88e6xxx_g2_smi_phy_write,
3250};
3251
3252static const struct mv88e6xxx_ops mv88e6175_ops = {
3253 .phy_read = mv88e6xxx_g2_smi_phy_read,
3254 .phy_write = mv88e6xxx_g2_smi_phy_write,
3255};
3256
3257static const struct mv88e6xxx_ops mv88e6176_ops = {
3258 .phy_read = mv88e6xxx_g2_smi_phy_read,
3259 .phy_write = mv88e6xxx_g2_smi_phy_write,
3260};
3261
3262static const struct mv88e6xxx_ops mv88e6185_ops = {
3263 .phy_read = mv88e6xxx_phy_ppu_read,
3264 .phy_write = mv88e6xxx_phy_ppu_write,
3265};
3266
3267static const struct mv88e6xxx_ops mv88e6240_ops = {
3268 .phy_read = mv88e6xxx_g2_smi_phy_read,
3269 .phy_write = mv88e6xxx_g2_smi_phy_write,
3270};
3271
3272static const struct mv88e6xxx_ops mv88e6320_ops = {
3273 .phy_read = mv88e6xxx_g2_smi_phy_read,
3274 .phy_write = mv88e6xxx_g2_smi_phy_write,
3275};
3276
3277static const struct mv88e6xxx_ops mv88e6321_ops = {
3278 .phy_read = mv88e6xxx_g2_smi_phy_read,
3279 .phy_write = mv88e6xxx_g2_smi_phy_write,
3280};
3281
3282static const struct mv88e6xxx_ops mv88e6350_ops = {
3283 .phy_read = mv88e6xxx_g2_smi_phy_read,
3284 .phy_write = mv88e6xxx_g2_smi_phy_write,
3285};
3286
3287static const struct mv88e6xxx_ops mv88e6351_ops = {
3288 .phy_read = mv88e6xxx_g2_smi_phy_read,
3289 .phy_write = mv88e6xxx_g2_smi_phy_write,
3290};
3291
3292static const struct mv88e6xxx_ops mv88e6352_ops = {
3293 .phy_read = mv88e6xxx_g2_smi_phy_read,
3294 .phy_write = mv88e6xxx_g2_smi_phy_write,
3295};
3296
Vivien Didelotf81ec902016-05-09 13:22:58 -04003297static const struct mv88e6xxx_info mv88e6xxx_table[] = {
3298 [MV88E6085] = {
3299 .prod_num = PORT_SWITCH_ID_PROD_NUM_6085,
3300 .family = MV88E6XXX_FAMILY_6097,
3301 .name = "Marvell 88E6085",
3302 .num_databases = 4096,
3303 .num_ports = 10,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003304 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003305 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003306 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003307 .flags = MV88E6XXX_FLAGS_FAMILY_6097,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003308 .ops = &mv88e6085_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003309 },
3310
3311 [MV88E6095] = {
3312 .prod_num = PORT_SWITCH_ID_PROD_NUM_6095,
3313 .family = MV88E6XXX_FAMILY_6095,
3314 .name = "Marvell 88E6095/88E6095F",
3315 .num_databases = 256,
3316 .num_ports = 11,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003317 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003318 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003319 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003320 .flags = MV88E6XXX_FLAGS_FAMILY_6095,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003321 .ops = &mv88e6095_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003322 },
3323
3324 [MV88E6123] = {
3325 .prod_num = PORT_SWITCH_ID_PROD_NUM_6123,
3326 .family = MV88E6XXX_FAMILY_6165,
3327 .name = "Marvell 88E6123",
3328 .num_databases = 4096,
3329 .num_ports = 3,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003330 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003331 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003332 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003333 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003334 .ops = &mv88e6123_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003335 },
3336
3337 [MV88E6131] = {
3338 .prod_num = PORT_SWITCH_ID_PROD_NUM_6131,
3339 .family = MV88E6XXX_FAMILY_6185,
3340 .name = "Marvell 88E6131",
3341 .num_databases = 256,
3342 .num_ports = 8,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003343 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003344 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003345 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003346 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003347 .ops = &mv88e6131_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003348 },
3349
3350 [MV88E6161] = {
3351 .prod_num = PORT_SWITCH_ID_PROD_NUM_6161,
3352 .family = MV88E6XXX_FAMILY_6165,
3353 .name = "Marvell 88E6161",
3354 .num_databases = 4096,
3355 .num_ports = 6,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003356 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003357 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003358 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003359 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003360 .ops = &mv88e6161_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003361 },
3362
3363 [MV88E6165] = {
3364 .prod_num = PORT_SWITCH_ID_PROD_NUM_6165,
3365 .family = MV88E6XXX_FAMILY_6165,
3366 .name = "Marvell 88E6165",
3367 .num_databases = 4096,
3368 .num_ports = 6,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003369 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003370 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003371 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003372 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003373 .ops = &mv88e6165_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003374 },
3375
3376 [MV88E6171] = {
3377 .prod_num = PORT_SWITCH_ID_PROD_NUM_6171,
3378 .family = MV88E6XXX_FAMILY_6351,
3379 .name = "Marvell 88E6171",
3380 .num_databases = 4096,
3381 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003382 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003383 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003384 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003385 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003386 .ops = &mv88e6171_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003387 },
3388
3389 [MV88E6172] = {
3390 .prod_num = PORT_SWITCH_ID_PROD_NUM_6172,
3391 .family = MV88E6XXX_FAMILY_6352,
3392 .name = "Marvell 88E6172",
3393 .num_databases = 4096,
3394 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003395 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003396 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003397 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003398 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003399 .ops = &mv88e6172_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003400 },
3401
3402 [MV88E6175] = {
3403 .prod_num = PORT_SWITCH_ID_PROD_NUM_6175,
3404 .family = MV88E6XXX_FAMILY_6351,
3405 .name = "Marvell 88E6175",
3406 .num_databases = 4096,
3407 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003408 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003409 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003410 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003411 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003412 .ops = &mv88e6175_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003413 },
3414
3415 [MV88E6176] = {
3416 .prod_num = PORT_SWITCH_ID_PROD_NUM_6176,
3417 .family = MV88E6XXX_FAMILY_6352,
3418 .name = "Marvell 88E6176",
3419 .num_databases = 4096,
3420 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003421 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003422 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003423 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003424 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003425 .ops = &mv88e6176_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003426 },
3427
3428 [MV88E6185] = {
3429 .prod_num = PORT_SWITCH_ID_PROD_NUM_6185,
3430 .family = MV88E6XXX_FAMILY_6185,
3431 .name = "Marvell 88E6185",
3432 .num_databases = 256,
3433 .num_ports = 10,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003434 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003435 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003436 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003437 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003438 .ops = &mv88e6185_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003439 },
3440
3441 [MV88E6240] = {
3442 .prod_num = PORT_SWITCH_ID_PROD_NUM_6240,
3443 .family = MV88E6XXX_FAMILY_6352,
3444 .name = "Marvell 88E6240",
3445 .num_databases = 4096,
3446 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003447 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003448 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003449 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003450 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003451 .ops = &mv88e6240_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003452 },
3453
3454 [MV88E6320] = {
3455 .prod_num = PORT_SWITCH_ID_PROD_NUM_6320,
3456 .family = MV88E6XXX_FAMILY_6320,
3457 .name = "Marvell 88E6320",
3458 .num_databases = 4096,
3459 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003460 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003461 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003462 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003463 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003464 .ops = &mv88e6320_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003465 },
3466
3467 [MV88E6321] = {
3468 .prod_num = PORT_SWITCH_ID_PROD_NUM_6321,
3469 .family = MV88E6XXX_FAMILY_6320,
3470 .name = "Marvell 88E6321",
3471 .num_databases = 4096,
3472 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003473 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003474 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003475 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003476 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003477 .ops = &mv88e6321_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003478 },
3479
3480 [MV88E6350] = {
3481 .prod_num = PORT_SWITCH_ID_PROD_NUM_6350,
3482 .family = MV88E6XXX_FAMILY_6351,
3483 .name = "Marvell 88E6350",
3484 .num_databases = 4096,
3485 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003486 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003487 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003488 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003489 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003490 .ops = &mv88e6350_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003491 },
3492
3493 [MV88E6351] = {
3494 .prod_num = PORT_SWITCH_ID_PROD_NUM_6351,
3495 .family = MV88E6XXX_FAMILY_6351,
3496 .name = "Marvell 88E6351",
3497 .num_databases = 4096,
3498 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003499 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003500 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003501 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003502 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003503 .ops = &mv88e6351_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003504 },
3505
3506 [MV88E6352] = {
3507 .prod_num = PORT_SWITCH_ID_PROD_NUM_6352,
3508 .family = MV88E6XXX_FAMILY_6352,
3509 .name = "Marvell 88E6352",
3510 .num_databases = 4096,
3511 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003512 .port_base_addr = 0x10,
Vivien Didelota935c052016-09-29 12:21:53 -04003513 .global1_addr = 0x1b,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003514 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003515 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003516 .ops = &mv88e6352_ops,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003517 },
3518};
3519
Vivien Didelot5f7c0362016-06-20 13:14:04 -04003520static const struct mv88e6xxx_info *mv88e6xxx_lookup_info(unsigned int prod_num)
Vivien Didelotb9b37712015-10-30 19:39:48 -04003521{
Vivien Didelota439c062016-04-17 13:23:58 -04003522 int i;
Vivien Didelotb9b37712015-10-30 19:39:48 -04003523
Vivien Didelot5f7c0362016-06-20 13:14:04 -04003524 for (i = 0; i < ARRAY_SIZE(mv88e6xxx_table); ++i)
3525 if (mv88e6xxx_table[i].prod_num == prod_num)
3526 return &mv88e6xxx_table[i];
Vivien Didelotb9b37712015-10-30 19:39:48 -04003527
Vivien Didelotb9b37712015-10-30 19:39:48 -04003528 return NULL;
3529}
3530
Vivien Didelotfad09c72016-06-21 12:28:20 -04003531static int mv88e6xxx_detect(struct mv88e6xxx_chip *chip)
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003532{
3533 const struct mv88e6xxx_info *info;
Vivien Didelot8f6345b2016-07-20 18:18:36 -04003534 unsigned int prod_num, rev;
3535 u16 id;
3536 int err;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003537
Vivien Didelot8f6345b2016-07-20 18:18:36 -04003538 mutex_lock(&chip->reg_lock);
3539 err = mv88e6xxx_port_read(chip, 0, PORT_SWITCH_ID, &id);
3540 mutex_unlock(&chip->reg_lock);
3541 if (err)
3542 return err;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003543
3544 prod_num = (id & 0xfff0) >> 4;
3545 rev = id & 0x000f;
3546
3547 info = mv88e6xxx_lookup_info(prod_num);
3548 if (!info)
3549 return -ENODEV;
3550
Vivien Didelotcaac8542016-06-20 13:14:09 -04003551 /* Update the compatible info with the probed one */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003552 chip->info = info;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003553
Vivien Didelotca070c12016-09-02 14:45:34 -04003554 err = mv88e6xxx_g2_require(chip);
3555 if (err)
3556 return err;
3557
Vivien Didelotfad09c72016-06-21 12:28:20 -04003558 dev_info(chip->dev, "switch 0x%x detected: %s, revision %u\n",
3559 chip->info->prod_num, chip->info->name, rev);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003560
3561 return 0;
3562}
3563
Vivien Didelotfad09c72016-06-21 12:28:20 -04003564static struct mv88e6xxx_chip *mv88e6xxx_alloc_chip(struct device *dev)
Vivien Didelot469d7292016-06-20 13:14:06 -04003565{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003566 struct mv88e6xxx_chip *chip;
Vivien Didelot469d7292016-06-20 13:14:06 -04003567
Vivien Didelotfad09c72016-06-21 12:28:20 -04003568 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
3569 if (!chip)
Vivien Didelot469d7292016-06-20 13:14:06 -04003570 return NULL;
3571
Vivien Didelotfad09c72016-06-21 12:28:20 -04003572 chip->dev = dev;
Vivien Didelot469d7292016-06-20 13:14:06 -04003573
Vivien Didelotfad09c72016-06-21 12:28:20 -04003574 mutex_init(&chip->reg_lock);
Vivien Didelot469d7292016-06-20 13:14:06 -04003575
Vivien Didelotfad09c72016-06-21 12:28:20 -04003576 return chip;
Vivien Didelot469d7292016-06-20 13:14:06 -04003577}
3578
Vivien Didelote57e5e72016-08-15 17:19:00 -04003579static void mv88e6xxx_phy_init(struct mv88e6xxx_chip *chip)
3580{
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003581 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU))
Vivien Didelote57e5e72016-08-15 17:19:00 -04003582 mv88e6xxx_ppu_state_init(chip);
Vivien Didelote57e5e72016-08-15 17:19:00 -04003583}
3584
Andrew Lunn930188c2016-08-22 16:01:03 +02003585static void mv88e6xxx_phy_destroy(struct mv88e6xxx_chip *chip)
3586{
Vivien Didelotb3469dd2016-09-29 12:22:00 -04003587 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU))
Andrew Lunn930188c2016-08-22 16:01:03 +02003588 mv88e6xxx_ppu_state_destroy(chip);
Andrew Lunn930188c2016-08-22 16:01:03 +02003589}
3590
Vivien Didelotfad09c72016-06-21 12:28:20 -04003591static int mv88e6xxx_smi_init(struct mv88e6xxx_chip *chip,
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003592 struct mii_bus *bus, int sw_addr)
3593{
3594 /* ADDR[0] pin is unavailable externally and considered zero */
3595 if (sw_addr & 0x1)
3596 return -EINVAL;
3597
Vivien Didelot914b32f2016-06-20 13:14:11 -04003598 if (sw_addr == 0)
Vivien Didelotfad09c72016-06-21 12:28:20 -04003599 chip->smi_ops = &mv88e6xxx_smi_single_chip_ops;
Vivien Didelota0ffff22016-08-15 17:18:58 -04003600 else if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_MULTI_CHIP))
Vivien Didelotfad09c72016-06-21 12:28:20 -04003601 chip->smi_ops = &mv88e6xxx_smi_multi_chip_ops;
Vivien Didelot914b32f2016-06-20 13:14:11 -04003602 else
3603 return -EINVAL;
3604
Vivien Didelotfad09c72016-06-21 12:28:20 -04003605 chip->bus = bus;
3606 chip->sw_addr = sw_addr;
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003607
3608 return 0;
3609}
3610
Andrew Lunn7b314362016-08-22 16:01:01 +02003611static enum dsa_tag_protocol mv88e6xxx_get_tag_protocol(struct dsa_switch *ds)
3612{
Vivien Didelot04bed142016-08-31 18:06:13 -04003613 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02003614
3615 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA))
3616 return DSA_TAG_PROTO_EDSA;
3617
3618 return DSA_TAG_PROTO_DSA;
Andrew Lunn7b314362016-08-22 16:01:01 +02003619}
3620
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02003621static const char *mv88e6xxx_drv_probe(struct device *dsa_dev,
3622 struct device *host_dev, int sw_addr,
3623 void **priv)
Andrew Lunna77d43f2016-04-13 02:40:42 +02003624{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003625 struct mv88e6xxx_chip *chip;
Vivien Didelota439c062016-04-17 13:23:58 -04003626 struct mii_bus *bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003627 int err;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003628
Vivien Didelota439c062016-04-17 13:23:58 -04003629 bus = dsa_host_dev_to_mii_bus(host_dev);
Andrew Lunnc1569132016-04-13 02:40:45 +02003630 if (!bus)
3631 return NULL;
3632
Vivien Didelotfad09c72016-06-21 12:28:20 -04003633 chip = mv88e6xxx_alloc_chip(dsa_dev);
3634 if (!chip)
Vivien Didelot469d7292016-06-20 13:14:06 -04003635 return NULL;
3636
Vivien Didelotcaac8542016-06-20 13:14:09 -04003637 /* Legacy SMI probing will only support chips similar to 88E6085 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003638 chip->info = &mv88e6xxx_table[MV88E6085];
Vivien Didelotcaac8542016-06-20 13:14:09 -04003639
Vivien Didelotfad09c72016-06-21 12:28:20 -04003640 err = mv88e6xxx_smi_init(chip, bus, sw_addr);
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003641 if (err)
3642 goto free;
3643
Vivien Didelotfad09c72016-06-21 12:28:20 -04003644 err = mv88e6xxx_detect(chip);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003645 if (err)
Vivien Didelot469d7292016-06-20 13:14:06 -04003646 goto free;
Vivien Didelota439c062016-04-17 13:23:58 -04003647
Vivien Didelote57e5e72016-08-15 17:19:00 -04003648 mv88e6xxx_phy_init(chip);
3649
Vivien Didelotfad09c72016-06-21 12:28:20 -04003650 err = mv88e6xxx_mdio_register(chip, NULL);
Andrew Lunnb516d452016-06-04 21:17:06 +02003651 if (err)
Vivien Didelot469d7292016-06-20 13:14:06 -04003652 goto free;
Andrew Lunnb516d452016-06-04 21:17:06 +02003653
Vivien Didelotfad09c72016-06-21 12:28:20 -04003654 *priv = chip;
Vivien Didelota439c062016-04-17 13:23:58 -04003655
Vivien Didelotfad09c72016-06-21 12:28:20 -04003656 return chip->info->name;
Vivien Didelot469d7292016-06-20 13:14:06 -04003657free:
Vivien Didelotfad09c72016-06-21 12:28:20 -04003658 devm_kfree(dsa_dev, chip);
Vivien Didelot469d7292016-06-20 13:14:06 -04003659
3660 return NULL;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003661}
3662
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003663static int mv88e6xxx_port_mdb_prepare(struct dsa_switch *ds, int port,
3664 const struct switchdev_obj_port_mdb *mdb,
3665 struct switchdev_trans *trans)
3666{
3667 /* We don't need any dynamic resource from the kernel (yet),
3668 * so skip the prepare phase.
3669 */
3670
3671 return 0;
3672}
3673
3674static void mv88e6xxx_port_mdb_add(struct dsa_switch *ds, int port,
3675 const struct switchdev_obj_port_mdb *mdb,
3676 struct switchdev_trans *trans)
3677{
Vivien Didelot04bed142016-08-31 18:06:13 -04003678 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003679
3680 mutex_lock(&chip->reg_lock);
3681 if (mv88e6xxx_port_db_load_purge(chip, port, mdb->addr, mdb->vid,
3682 GLOBAL_ATU_DATA_STATE_MC_STATIC))
3683 netdev_err(ds->ports[port].netdev, "failed to load multicast MAC address\n");
3684 mutex_unlock(&chip->reg_lock);
3685}
3686
3687static int mv88e6xxx_port_mdb_del(struct dsa_switch *ds, int port,
3688 const struct switchdev_obj_port_mdb *mdb)
3689{
Vivien Didelot04bed142016-08-31 18:06:13 -04003690 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003691 int err;
3692
3693 mutex_lock(&chip->reg_lock);
3694 err = mv88e6xxx_port_db_load_purge(chip, port, mdb->addr, mdb->vid,
3695 GLOBAL_ATU_DATA_STATE_UNUSED);
3696 mutex_unlock(&chip->reg_lock);
3697
3698 return err;
3699}
3700
3701static int mv88e6xxx_port_mdb_dump(struct dsa_switch *ds, int port,
3702 struct switchdev_obj_port_mdb *mdb,
3703 int (*cb)(struct switchdev_obj *obj))
3704{
Vivien Didelot04bed142016-08-31 18:06:13 -04003705 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003706 int err;
3707
3708 mutex_lock(&chip->reg_lock);
3709 err = mv88e6xxx_port_db_dump(chip, port, &mdb->obj, cb);
3710 mutex_unlock(&chip->reg_lock);
3711
3712 return err;
3713}
3714
Vivien Didelot9d490b42016-08-23 12:38:56 -04003715static struct dsa_switch_ops mv88e6xxx_switch_ops = {
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02003716 .probe = mv88e6xxx_drv_probe,
Andrew Lunn7b314362016-08-22 16:01:01 +02003717 .get_tag_protocol = mv88e6xxx_get_tag_protocol,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003718 .setup = mv88e6xxx_setup,
3719 .set_addr = mv88e6xxx_set_addr,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003720 .adjust_link = mv88e6xxx_adjust_link,
3721 .get_strings = mv88e6xxx_get_strings,
3722 .get_ethtool_stats = mv88e6xxx_get_ethtool_stats,
3723 .get_sset_count = mv88e6xxx_get_sset_count,
3724 .set_eee = mv88e6xxx_set_eee,
3725 .get_eee = mv88e6xxx_get_eee,
3726#ifdef CONFIG_NET_DSA_HWMON
3727 .get_temp = mv88e6xxx_get_temp,
3728 .get_temp_limit = mv88e6xxx_get_temp_limit,
3729 .set_temp_limit = mv88e6xxx_set_temp_limit,
3730 .get_temp_alarm = mv88e6xxx_get_temp_alarm,
3731#endif
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003732 .get_eeprom_len = mv88e6xxx_get_eeprom_len,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003733 .get_eeprom = mv88e6xxx_get_eeprom,
3734 .set_eeprom = mv88e6xxx_set_eeprom,
3735 .get_regs_len = mv88e6xxx_get_regs_len,
3736 .get_regs = mv88e6xxx_get_regs,
Vivien Didelot2cfcd962016-07-18 20:45:40 -04003737 .set_ageing_time = mv88e6xxx_set_ageing_time,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003738 .port_bridge_join = mv88e6xxx_port_bridge_join,
3739 .port_bridge_leave = mv88e6xxx_port_bridge_leave,
3740 .port_stp_state_set = mv88e6xxx_port_stp_state_set,
Vivien Didelot749efcb2016-09-22 16:49:24 -04003741 .port_fast_age = mv88e6xxx_port_fast_age,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003742 .port_vlan_filtering = mv88e6xxx_port_vlan_filtering,
3743 .port_vlan_prepare = mv88e6xxx_port_vlan_prepare,
3744 .port_vlan_add = mv88e6xxx_port_vlan_add,
3745 .port_vlan_del = mv88e6xxx_port_vlan_del,
3746 .port_vlan_dump = mv88e6xxx_port_vlan_dump,
3747 .port_fdb_prepare = mv88e6xxx_port_fdb_prepare,
3748 .port_fdb_add = mv88e6xxx_port_fdb_add,
3749 .port_fdb_del = mv88e6xxx_port_fdb_del,
3750 .port_fdb_dump = mv88e6xxx_port_fdb_dump,
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003751 .port_mdb_prepare = mv88e6xxx_port_mdb_prepare,
3752 .port_mdb_add = mv88e6xxx_port_mdb_add,
3753 .port_mdb_del = mv88e6xxx_port_mdb_del,
3754 .port_mdb_dump = mv88e6xxx_port_mdb_dump,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003755};
3756
Vivien Didelotfad09c72016-06-21 12:28:20 -04003757static int mv88e6xxx_register_switch(struct mv88e6xxx_chip *chip,
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003758 struct device_node *np)
3759{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003760 struct device *dev = chip->dev;
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003761 struct dsa_switch *ds;
3762
3763 ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
3764 if (!ds)
3765 return -ENOMEM;
3766
3767 ds->dev = dev;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003768 ds->priv = chip;
Vivien Didelot9d490b42016-08-23 12:38:56 -04003769 ds->ops = &mv88e6xxx_switch_ops;
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003770
3771 dev_set_drvdata(dev, ds);
3772
3773 return dsa_register_switch(ds, np);
3774}
3775
Vivien Didelotfad09c72016-06-21 12:28:20 -04003776static void mv88e6xxx_unregister_switch(struct mv88e6xxx_chip *chip)
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003777{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003778 dsa_unregister_switch(chip->ds);
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003779}
3780
Vivien Didelot57d32312016-06-20 13:13:58 -04003781static int mv88e6xxx_probe(struct mdio_device *mdiodev)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003782{
3783 struct device *dev = &mdiodev->dev;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003784 struct device_node *np = dev->of_node;
Vivien Didelotcaac8542016-06-20 13:14:09 -04003785 const struct mv88e6xxx_info *compat_info;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003786 struct mv88e6xxx_chip *chip;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003787 u32 eeprom_len;
Andrew Lunn52638f72016-05-10 23:27:22 +02003788 int err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003789
Vivien Didelotcaac8542016-06-20 13:14:09 -04003790 compat_info = of_device_get_match_data(dev);
3791 if (!compat_info)
3792 return -EINVAL;
3793
Vivien Didelotfad09c72016-06-21 12:28:20 -04003794 chip = mv88e6xxx_alloc_chip(dev);
3795 if (!chip)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003796 return -ENOMEM;
3797
Vivien Didelotfad09c72016-06-21 12:28:20 -04003798 chip->info = compat_info;
Vivien Didelotcaac8542016-06-20 13:14:09 -04003799
Vivien Didelotfad09c72016-06-21 12:28:20 -04003800 err = mv88e6xxx_smi_init(chip, mdiodev->bus, mdiodev->addr);
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003801 if (err)
3802 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003803
Vivien Didelotfad09c72016-06-21 12:28:20 -04003804 err = mv88e6xxx_detect(chip);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003805 if (err)
3806 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003807
Vivien Didelote57e5e72016-08-15 17:19:00 -04003808 mv88e6xxx_phy_init(chip);
3809
Vivien Didelotfad09c72016-06-21 12:28:20 -04003810 chip->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_ASIS);
3811 if (IS_ERR(chip->reset))
3812 return PTR_ERR(chip->reset);
Andrew Lunn52638f72016-05-10 23:27:22 +02003813
Vivien Didelot855b1932016-07-20 18:18:35 -04003814 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16) &&
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003815 !of_property_read_u32(np, "eeprom-length", &eeprom_len))
Vivien Didelotfad09c72016-06-21 12:28:20 -04003816 chip->eeprom_len = eeprom_len;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003817
Vivien Didelotfad09c72016-06-21 12:28:20 -04003818 err = mv88e6xxx_mdio_register(chip, np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003819 if (err)
3820 return err;
3821
Vivien Didelotfad09c72016-06-21 12:28:20 -04003822 err = mv88e6xxx_register_switch(chip, np);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02003823 if (err) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04003824 mv88e6xxx_mdio_unregister(chip);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02003825 return err;
3826 }
3827
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003828 return 0;
3829}
3830
3831static void mv88e6xxx_remove(struct mdio_device *mdiodev)
3832{
3833 struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev);
Vivien Didelot04bed142016-08-31 18:06:13 -04003834 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003835
Andrew Lunn930188c2016-08-22 16:01:03 +02003836 mv88e6xxx_phy_destroy(chip);
Vivien Didelotfad09c72016-06-21 12:28:20 -04003837 mv88e6xxx_unregister_switch(chip);
3838 mv88e6xxx_mdio_unregister(chip);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003839}
3840
3841static const struct of_device_id mv88e6xxx_of_match[] = {
Vivien Didelotcaac8542016-06-20 13:14:09 -04003842 {
3843 .compatible = "marvell,mv88e6085",
3844 .data = &mv88e6xxx_table[MV88E6085],
3845 },
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003846 { /* sentinel */ },
3847};
3848
3849MODULE_DEVICE_TABLE(of, mv88e6xxx_of_match);
3850
3851static struct mdio_driver mv88e6xxx_driver = {
3852 .probe = mv88e6xxx_probe,
3853 .remove = mv88e6xxx_remove,
3854 .mdiodrv.driver = {
3855 .name = "mv88e6085",
3856 .of_match_table = mv88e6xxx_of_match,
3857 },
3858};
3859
Ben Hutchings98e67302011-11-25 14:36:19 +00003860static int __init mv88e6xxx_init(void)
3861{
Vivien Didelot9d490b42016-08-23 12:38:56 -04003862 register_switch_driver(&mv88e6xxx_switch_ops);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003863 return mdio_driver_register(&mv88e6xxx_driver);
Ben Hutchings98e67302011-11-25 14:36:19 +00003864}
3865module_init(mv88e6xxx_init);
3866
3867static void __exit mv88e6xxx_cleanup(void)
3868{
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003869 mdio_driver_unregister(&mv88e6xxx_driver);
Vivien Didelot9d490b42016-08-23 12:38:56 -04003870 unregister_switch_driver(&mv88e6xxx_switch_ops);
Ben Hutchings98e67302011-11-25 14:36:19 +00003871}
3872module_exit(mv88e6xxx_cleanup);
Ben Hutchings3d825ed2011-11-25 14:37:16 +00003873
3874MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
3875MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
3876MODULE_LICENSE("GPL");