blob: 745e1588d83c362112756d9dc06dadde490c0946 [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 Didelotec561272016-09-02 14:45:33 -040034#include "global2.h"
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000035
Vivien Didelotfad09c72016-06-21 12:28:20 -040036static void assert_reg_lock(struct mv88e6xxx_chip *chip)
Vivien Didelot3996a4f2015-10-30 18:56:45 -040037{
Vivien Didelotfad09c72016-06-21 12:28:20 -040038 if (unlikely(!mutex_is_locked(&chip->reg_lock))) {
39 dev_err(chip->dev, "Switch registers lock not held!\n");
Vivien Didelot3996a4f2015-10-30 18:56:45 -040040 dump_stack();
41 }
42}
43
Vivien Didelot914b32f2016-06-20 13:14:11 -040044/* The switch ADDR[4:1] configuration pins define the chip SMI device address
45 * (ADDR[0] is always zero, thus only even SMI addresses can be strapped).
46 *
47 * When ADDR is all zero, the chip uses Single-chip Addressing Mode, assuming it
48 * is the only device connected to the SMI master. In this mode it responds to
49 * all 32 possible SMI addresses, and thus maps directly the internal devices.
50 *
51 * When ADDR is non-zero, the chip uses Multi-chip Addressing Mode, allowing
52 * multiple devices to share the SMI interface. In this mode it responds to only
53 * 2 registers, used to indirectly access the internal SMI devices.
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000054 */
Vivien Didelot914b32f2016-06-20 13:14:11 -040055
Vivien Didelotfad09c72016-06-21 12:28:20 -040056static int mv88e6xxx_smi_read(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -040057 int addr, int reg, u16 *val)
58{
Vivien Didelotfad09c72016-06-21 12:28:20 -040059 if (!chip->smi_ops)
Vivien Didelot914b32f2016-06-20 13:14:11 -040060 return -EOPNOTSUPP;
61
Vivien Didelotfad09c72016-06-21 12:28:20 -040062 return chip->smi_ops->read(chip, addr, reg, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -040063}
64
Vivien Didelotfad09c72016-06-21 12:28:20 -040065static int mv88e6xxx_smi_write(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -040066 int addr, int reg, u16 val)
67{
Vivien Didelotfad09c72016-06-21 12:28:20 -040068 if (!chip->smi_ops)
Vivien Didelot914b32f2016-06-20 13:14:11 -040069 return -EOPNOTSUPP;
70
Vivien Didelotfad09c72016-06-21 12:28:20 -040071 return chip->smi_ops->write(chip, addr, reg, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -040072}
73
Vivien Didelotfad09c72016-06-21 12:28:20 -040074static int mv88e6xxx_smi_single_chip_read(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -040075 int addr, int reg, u16 *val)
76{
77 int ret;
78
Vivien Didelotfad09c72016-06-21 12:28:20 -040079 ret = mdiobus_read_nested(chip->bus, addr, reg);
Vivien Didelot914b32f2016-06-20 13:14:11 -040080 if (ret < 0)
81 return ret;
82
83 *val = ret & 0xffff;
84
85 return 0;
86}
87
Vivien Didelotfad09c72016-06-21 12:28:20 -040088static int mv88e6xxx_smi_single_chip_write(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -040089 int addr, int reg, u16 val)
90{
91 int ret;
92
Vivien Didelotfad09c72016-06-21 12:28:20 -040093 ret = mdiobus_write_nested(chip->bus, addr, reg, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -040094 if (ret < 0)
95 return ret;
96
97 return 0;
98}
99
100static const struct mv88e6xxx_ops mv88e6xxx_smi_single_chip_ops = {
101 .read = mv88e6xxx_smi_single_chip_read,
102 .write = mv88e6xxx_smi_single_chip_write,
103};
104
Vivien Didelotfad09c72016-06-21 12:28:20 -0400105static int mv88e6xxx_smi_multi_chip_wait(struct mv88e6xxx_chip *chip)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000106{
107 int ret;
108 int i;
109
110 for (i = 0; i < 16; i++) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400111 ret = mdiobus_read_nested(chip->bus, chip->sw_addr, SMI_CMD);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000112 if (ret < 0)
113 return ret;
114
Andrew Lunncca8b132015-04-02 04:06:39 +0200115 if ((ret & SMI_CMD_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000116 return 0;
117 }
118
119 return -ETIMEDOUT;
120}
121
Vivien Didelotfad09c72016-06-21 12:28:20 -0400122static int mv88e6xxx_smi_multi_chip_read(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -0400123 int addr, int reg, u16 *val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000124{
125 int ret;
126
Barry Grussling3675c8d2013-01-08 16:05:53 +0000127 /* Wait for the bus to become free. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400128 ret = mv88e6xxx_smi_multi_chip_wait(chip);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000129 if (ret < 0)
130 return ret;
131
Barry Grussling3675c8d2013-01-08 16:05:53 +0000132 /* Transmit the read command. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400133 ret = mdiobus_write_nested(chip->bus, chip->sw_addr, SMI_CMD,
Neil Armstrong6e899e62015-10-22 10:37:53 +0200134 SMI_CMD_OP_22_READ | (addr << 5) | reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000135 if (ret < 0)
136 return ret;
137
Barry Grussling3675c8d2013-01-08 16:05:53 +0000138 /* Wait for the read command to complete. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400139 ret = mv88e6xxx_smi_multi_chip_wait(chip);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000140 if (ret < 0)
141 return ret;
142
Barry Grussling3675c8d2013-01-08 16:05:53 +0000143 /* Read the data. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400144 ret = mdiobus_read_nested(chip->bus, chip->sw_addr, SMI_DATA);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000145 if (ret < 0)
146 return ret;
147
Vivien Didelot914b32f2016-06-20 13:14:11 -0400148 *val = ret & 0xffff;
149
150 return 0;
151}
152
Vivien Didelotfad09c72016-06-21 12:28:20 -0400153static int mv88e6xxx_smi_multi_chip_write(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -0400154 int addr, int reg, u16 val)
155{
156 int ret;
157
158 /* Wait for the bus to become free. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400159 ret = mv88e6xxx_smi_multi_chip_wait(chip);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400160 if (ret < 0)
161 return ret;
162
163 /* Transmit the data to write. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400164 ret = mdiobus_write_nested(chip->bus, chip->sw_addr, SMI_DATA, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400165 if (ret < 0)
166 return ret;
167
168 /* Transmit the write command. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400169 ret = mdiobus_write_nested(chip->bus, chip->sw_addr, SMI_CMD,
Vivien Didelot914b32f2016-06-20 13:14:11 -0400170 SMI_CMD_OP_22_WRITE | (addr << 5) | reg);
171 if (ret < 0)
172 return ret;
173
174 /* Wait for the write command to complete. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400175 ret = mv88e6xxx_smi_multi_chip_wait(chip);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400176 if (ret < 0)
177 return ret;
178
179 return 0;
180}
181
182static const struct mv88e6xxx_ops mv88e6xxx_smi_multi_chip_ops = {
183 .read = mv88e6xxx_smi_multi_chip_read,
184 .write = mv88e6xxx_smi_multi_chip_write,
185};
186
Vivien Didelotec561272016-09-02 14:45:33 -0400187int mv88e6xxx_read(struct mv88e6xxx_chip *chip, int addr, int reg, u16 *val)
Vivien Didelot914b32f2016-06-20 13:14:11 -0400188{
189 int err;
190
Vivien Didelotfad09c72016-06-21 12:28:20 -0400191 assert_reg_lock(chip);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400192
Vivien Didelotfad09c72016-06-21 12:28:20 -0400193 err = mv88e6xxx_smi_read(chip, addr, reg, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400194 if (err)
195 return err;
196
Vivien Didelotfad09c72016-06-21 12:28:20 -0400197 dev_dbg(chip->dev, "<- addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
Vivien Didelot914b32f2016-06-20 13:14:11 -0400198 addr, reg, *val);
199
200 return 0;
201}
202
Vivien Didelotec561272016-09-02 14:45:33 -0400203int mv88e6xxx_write(struct mv88e6xxx_chip *chip, int addr, int reg, u16 val)
Vivien Didelot914b32f2016-06-20 13:14:11 -0400204{
205 int err;
206
Vivien Didelotfad09c72016-06-21 12:28:20 -0400207 assert_reg_lock(chip);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400208
Vivien Didelotfad09c72016-06-21 12:28:20 -0400209 err = mv88e6xxx_smi_write(chip, addr, reg, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400210 if (err)
211 return err;
212
Vivien Didelotfad09c72016-06-21 12:28:20 -0400213 dev_dbg(chip->dev, "-> addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
Vivien Didelot914b32f2016-06-20 13:14:11 -0400214 addr, reg, val);
215
216 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000217}
218
Vivien Didelote57e5e72016-08-15 17:19:00 -0400219static int mv88e6xxx_phy_read(struct mv88e6xxx_chip *chip, int phy,
220 int reg, u16 *val)
221{
222 int addr = phy; /* PHY devices addresses start at 0x0 */
223
224 if (!chip->phy_ops)
225 return -EOPNOTSUPP;
226
227 return chip->phy_ops->read(chip, addr, reg, val);
228}
229
230static int mv88e6xxx_phy_write(struct mv88e6xxx_chip *chip, int phy,
231 int reg, u16 val)
232{
233 int addr = phy; /* PHY devices addresses start at 0x0 */
234
235 if (!chip->phy_ops)
236 return -EOPNOTSUPP;
237
238 return chip->phy_ops->write(chip, addr, reg, val);
239}
240
Vivien Didelot09cb7df2016-08-15 17:19:01 -0400241static int mv88e6xxx_phy_page_get(struct mv88e6xxx_chip *chip, int phy, u8 page)
242{
243 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_PHY_PAGE))
244 return -EOPNOTSUPP;
245
246 return mv88e6xxx_phy_write(chip, phy, PHY_PAGE, page);
247}
248
249static void mv88e6xxx_phy_page_put(struct mv88e6xxx_chip *chip, int phy)
250{
251 int err;
252
253 /* Restore PHY page Copper 0x0 for access via the registered MDIO bus */
254 err = mv88e6xxx_phy_write(chip, phy, PHY_PAGE, PHY_PAGE_COPPER);
255 if (unlikely(err)) {
256 dev_err(chip->dev, "failed to restore PHY %d page Copper (%d)\n",
257 phy, err);
258 }
259}
260
261static int mv88e6xxx_phy_page_read(struct mv88e6xxx_chip *chip, int phy,
262 u8 page, int reg, u16 *val)
263{
264 int err;
265
266 /* There is no paging for registers 22 */
267 if (reg == PHY_PAGE)
268 return -EINVAL;
269
270 err = mv88e6xxx_phy_page_get(chip, phy, page);
271 if (!err) {
272 err = mv88e6xxx_phy_read(chip, phy, reg, val);
273 mv88e6xxx_phy_page_put(chip, phy);
274 }
275
276 return err;
277}
278
279static int mv88e6xxx_phy_page_write(struct mv88e6xxx_chip *chip, int phy,
280 u8 page, int reg, u16 val)
281{
282 int err;
283
284 /* There is no paging for registers 22 */
285 if (reg == PHY_PAGE)
286 return -EINVAL;
287
288 err = mv88e6xxx_phy_page_get(chip, phy, page);
289 if (!err) {
290 err = mv88e6xxx_phy_write(chip, phy, PHY_PAGE, page);
291 mv88e6xxx_phy_page_put(chip, phy);
292 }
293
294 return err;
295}
296
297static int mv88e6xxx_serdes_read(struct mv88e6xxx_chip *chip, int reg, u16 *val)
298{
299 return mv88e6xxx_phy_page_read(chip, ADDR_SERDES, SERDES_PAGE_FIBER,
300 reg, val);
301}
302
303static int mv88e6xxx_serdes_write(struct mv88e6xxx_chip *chip, int reg, u16 val)
304{
305 return mv88e6xxx_phy_page_write(chip, ADDR_SERDES, SERDES_PAGE_FIBER,
306 reg, val);
307}
308
Vivien Didelotec561272016-09-02 14:45:33 -0400309int mv88e6xxx_wait(struct mv88e6xxx_chip *chip, int addr, int reg, u16 mask)
Vivien Didelot2d79af62016-08-15 17:18:57 -0400310{
Andrew Lunn6441e6692016-08-19 00:01:55 +0200311 int i;
Vivien Didelot2d79af62016-08-15 17:18:57 -0400312
Andrew Lunn6441e6692016-08-19 00:01:55 +0200313 for (i = 0; i < 16; i++) {
Vivien Didelot2d79af62016-08-15 17:18:57 -0400314 u16 val;
315 int err;
316
317 err = mv88e6xxx_read(chip, addr, reg, &val);
318 if (err)
319 return err;
320
321 if (!(val & mask))
322 return 0;
323
324 usleep_range(1000, 2000);
325 }
326
Andrew Lunn30853552016-08-19 00:01:57 +0200327 dev_err(chip->dev, "Timeout while waiting for switch\n");
Vivien Didelot2d79af62016-08-15 17:18:57 -0400328 return -ETIMEDOUT;
329}
330
Vivien Didelotf22ab642016-07-18 20:45:31 -0400331/* Indirect write to single pointer-data register with an Update bit */
Vivien Didelotec561272016-09-02 14:45:33 -0400332int mv88e6xxx_update(struct mv88e6xxx_chip *chip, int addr, int reg, u16 update)
Vivien Didelotf22ab642016-07-18 20:45:31 -0400333{
334 u16 val;
Andrew Lunn0f02b4f2016-08-19 00:01:56 +0200335 int err;
Vivien Didelotf22ab642016-07-18 20:45:31 -0400336
337 /* Wait until the previous operation is completed */
Andrew Lunn0f02b4f2016-08-19 00:01:56 +0200338 err = mv88e6xxx_wait(chip, addr, reg, BIT(15));
339 if (err)
340 return err;
Vivien Didelotf22ab642016-07-18 20:45:31 -0400341
342 /* Set the Update bit to trigger a write operation */
343 val = BIT(15) | update;
344
345 return mv88e6xxx_write(chip, addr, reg, val);
346}
347
Vivien Didelotfad09c72016-06-21 12:28:20 -0400348static int _mv88e6xxx_reg_read(struct mv88e6xxx_chip *chip, int addr, int reg)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000349{
Vivien Didelot914b32f2016-06-20 13:14:11 -0400350 u16 val;
351 int err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000352
Vivien Didelotfad09c72016-06-21 12:28:20 -0400353 err = mv88e6xxx_read(chip, addr, reg, &val);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400354 if (err)
355 return err;
Vivien Didelot3996a4f2015-10-30 18:56:45 -0400356
Vivien Didelot914b32f2016-06-20 13:14:11 -0400357 return val;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000358}
359
Vivien Didelotfad09c72016-06-21 12:28:20 -0400360static int _mv88e6xxx_reg_write(struct mv88e6xxx_chip *chip, int addr,
Andrew Lunn158bc062016-04-28 21:24:06 -0400361 int reg, u16 val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000362{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400363 return mv88e6xxx_write(chip, addr, reg, val);
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700364}
365
Vivien Didelotfad09c72016-06-21 12:28:20 -0400366static int mv88e6xxx_ppu_disable(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000367{
368 int ret;
Andrew Lunn6441e6692016-08-19 00:01:55 +0200369 int i;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000370
Vivien Didelotfad09c72016-06-21 12:28:20 -0400371 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_CONTROL);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200372 if (ret < 0)
373 return ret;
374
Vivien Didelotfad09c72016-06-21 12:28:20 -0400375 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL,
Vivien Didelot8c9983a2016-05-09 13:22:39 -0400376 ret & ~GLOBAL_CONTROL_PPU_ENABLE);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200377 if (ret)
378 return ret;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000379
Andrew Lunn6441e6692016-08-19 00:01:55 +0200380 for (i = 0; i < 16; i++) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400381 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATUS);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200382 if (ret < 0)
383 return ret;
384
Barry Grussling19b2f972013-01-08 16:05:54 +0000385 usleep_range(1000, 2000);
Andrew Lunncca8b132015-04-02 04:06:39 +0200386 if ((ret & GLOBAL_STATUS_PPU_MASK) !=
387 GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000388 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000389 }
390
391 return -ETIMEDOUT;
392}
393
Vivien Didelotfad09c72016-06-21 12:28:20 -0400394static int mv88e6xxx_ppu_enable(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000395{
Andrew Lunn6441e6692016-08-19 00:01:55 +0200396 int ret, err, i;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000397
Vivien Didelotfad09c72016-06-21 12:28:20 -0400398 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_CONTROL);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200399 if (ret < 0)
400 return ret;
401
Vivien Didelotfad09c72016-06-21 12:28:20 -0400402 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL,
Vivien Didelot762eb672016-06-04 21:16:54 +0200403 ret | GLOBAL_CONTROL_PPU_ENABLE);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200404 if (err)
405 return err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000406
Andrew Lunn6441e6692016-08-19 00:01:55 +0200407 for (i = 0; i < 16; i++) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400408 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATUS);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200409 if (ret < 0)
410 return ret;
411
Barry Grussling19b2f972013-01-08 16:05:54 +0000412 usleep_range(1000, 2000);
Andrew Lunncca8b132015-04-02 04:06:39 +0200413 if ((ret & GLOBAL_STATUS_PPU_MASK) ==
414 GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000415 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000416 }
417
418 return -ETIMEDOUT;
419}
420
421static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
422{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400423 struct mv88e6xxx_chip *chip;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000424
Vivien Didelotfad09c72016-06-21 12:28:20 -0400425 chip = container_of(ugly, struct mv88e6xxx_chip, ppu_work);
Vivien Didelot762eb672016-06-04 21:16:54 +0200426
Vivien Didelotfad09c72016-06-21 12:28:20 -0400427 mutex_lock(&chip->reg_lock);
Vivien Didelot762eb672016-06-04 21:16:54 +0200428
Vivien Didelotfad09c72016-06-21 12:28:20 -0400429 if (mutex_trylock(&chip->ppu_mutex)) {
430 if (mv88e6xxx_ppu_enable(chip) == 0)
431 chip->ppu_disabled = 0;
432 mutex_unlock(&chip->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000433 }
Vivien Didelot762eb672016-06-04 21:16:54 +0200434
Vivien Didelotfad09c72016-06-21 12:28:20 -0400435 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000436}
437
438static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
439{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400440 struct mv88e6xxx_chip *chip = (void *)_ps;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000441
Vivien Didelotfad09c72016-06-21 12:28:20 -0400442 schedule_work(&chip->ppu_work);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000443}
444
Vivien Didelotfad09c72016-06-21 12:28:20 -0400445static int mv88e6xxx_ppu_access_get(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000446{
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000447 int ret;
448
Vivien Didelotfad09c72016-06-21 12:28:20 -0400449 mutex_lock(&chip->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000450
Barry Grussling3675c8d2013-01-08 16:05:53 +0000451 /* If the PHY polling unit is enabled, disable it so that
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000452 * we can access the PHY registers. If it was already
453 * disabled, cancel the timer that is going to re-enable
454 * it.
455 */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400456 if (!chip->ppu_disabled) {
457 ret = mv88e6xxx_ppu_disable(chip);
Barry Grussling85686582013-01-08 16:05:56 +0000458 if (ret < 0) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400459 mutex_unlock(&chip->ppu_mutex);
Barry Grussling85686582013-01-08 16:05:56 +0000460 return ret;
461 }
Vivien Didelotfad09c72016-06-21 12:28:20 -0400462 chip->ppu_disabled = 1;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000463 } else {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400464 del_timer(&chip->ppu_timer);
Barry Grussling85686582013-01-08 16:05:56 +0000465 ret = 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000466 }
467
468 return ret;
469}
470
Vivien Didelotfad09c72016-06-21 12:28:20 -0400471static void mv88e6xxx_ppu_access_put(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000472{
Barry Grussling3675c8d2013-01-08 16:05:53 +0000473 /* Schedule a timer to re-enable the PHY polling unit. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400474 mod_timer(&chip->ppu_timer, jiffies + msecs_to_jiffies(10));
475 mutex_unlock(&chip->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000476}
477
Vivien Didelotfad09c72016-06-21 12:28:20 -0400478static void mv88e6xxx_ppu_state_init(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000479{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400480 mutex_init(&chip->ppu_mutex);
481 INIT_WORK(&chip->ppu_work, mv88e6xxx_ppu_reenable_work);
482 init_timer(&chip->ppu_timer);
483 chip->ppu_timer.data = (unsigned long)chip;
484 chip->ppu_timer.function = mv88e6xxx_ppu_reenable_timer;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000485}
486
Andrew Lunn930188c2016-08-22 16:01:03 +0200487static void mv88e6xxx_ppu_state_destroy(struct mv88e6xxx_chip *chip)
488{
489 del_timer_sync(&chip->ppu_timer);
490}
491
Vivien Didelote57e5e72016-08-15 17:19:00 -0400492static int mv88e6xxx_phy_ppu_read(struct mv88e6xxx_chip *chip, int addr,
493 int reg, u16 *val)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000494{
Vivien Didelote57e5e72016-08-15 17:19:00 -0400495 int err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000496
Vivien Didelote57e5e72016-08-15 17:19:00 -0400497 err = mv88e6xxx_ppu_access_get(chip);
498 if (!err) {
499 err = mv88e6xxx_read(chip, addr, reg, val);
Vivien Didelotfad09c72016-06-21 12:28:20 -0400500 mv88e6xxx_ppu_access_put(chip);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000501 }
502
Vivien Didelote57e5e72016-08-15 17:19:00 -0400503 return err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000504}
505
Vivien Didelote57e5e72016-08-15 17:19:00 -0400506static int mv88e6xxx_phy_ppu_write(struct mv88e6xxx_chip *chip, int addr,
507 int reg, u16 val)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000508{
Vivien Didelote57e5e72016-08-15 17:19:00 -0400509 int err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000510
Vivien Didelote57e5e72016-08-15 17:19:00 -0400511 err = mv88e6xxx_ppu_access_get(chip);
512 if (!err) {
513 err = mv88e6xxx_write(chip, addr, reg, val);
Vivien Didelotfad09c72016-06-21 12:28:20 -0400514 mv88e6xxx_ppu_access_put(chip);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000515 }
516
Vivien Didelote57e5e72016-08-15 17:19:00 -0400517 return err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000518}
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000519
Vivien Didelote57e5e72016-08-15 17:19:00 -0400520static const struct mv88e6xxx_ops mv88e6xxx_phy_ppu_ops = {
521 .read = mv88e6xxx_phy_ppu_read,
522 .write = mv88e6xxx_phy_ppu_write,
523};
524
Vivien Didelotfad09c72016-06-21 12:28:20 -0400525static bool mv88e6xxx_6065_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200526{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400527 return chip->info->family == MV88E6XXX_FAMILY_6065;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200528}
529
Vivien Didelotfad09c72016-06-21 12:28:20 -0400530static bool mv88e6xxx_6095_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200531{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400532 return chip->info->family == MV88E6XXX_FAMILY_6095;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200533}
534
Vivien Didelotfad09c72016-06-21 12:28:20 -0400535static bool mv88e6xxx_6097_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200536{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400537 return chip->info->family == MV88E6XXX_FAMILY_6097;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200538}
539
Vivien Didelotfad09c72016-06-21 12:28:20 -0400540static bool mv88e6xxx_6165_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200541{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400542 return chip->info->family == MV88E6XXX_FAMILY_6165;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200543}
544
Vivien Didelotfad09c72016-06-21 12:28:20 -0400545static bool mv88e6xxx_6185_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200546{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400547 return chip->info->family == MV88E6XXX_FAMILY_6185;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200548}
549
Vivien Didelotfad09c72016-06-21 12:28:20 -0400550static bool mv88e6xxx_6320_family(struct mv88e6xxx_chip *chip)
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700551{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400552 return chip->info->family == MV88E6XXX_FAMILY_6320;
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700553}
554
Vivien Didelotfad09c72016-06-21 12:28:20 -0400555static bool mv88e6xxx_6351_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200556{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400557 return chip->info->family == MV88E6XXX_FAMILY_6351;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200558}
559
Vivien Didelotfad09c72016-06-21 12:28:20 -0400560static bool mv88e6xxx_6352_family(struct mv88e6xxx_chip *chip)
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200561{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400562 return chip->info->family == MV88E6XXX_FAMILY_6352;
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200563}
564
Vivien Didelotfad09c72016-06-21 12:28:20 -0400565static unsigned int mv88e6xxx_num_databases(struct mv88e6xxx_chip *chip)
Vivien Didelotf74df0b2016-03-31 16:53:43 -0400566{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400567 return chip->info->num_databases;
Vivien Didelotf74df0b2016-03-31 16:53:43 -0400568}
569
Vivien Didelotfad09c72016-06-21 12:28:20 -0400570static bool mv88e6xxx_has_fid_reg(struct mv88e6xxx_chip *chip)
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400571{
572 /* Does the device have dedicated FID registers for ATU and VTU ops? */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400573 if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
574 mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip))
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400575 return true;
576
577 return false;
578}
579
Andrew Lunndea87022015-08-31 15:56:47 +0200580/* We expect the switch to perform auto negotiation if there is a real
581 * phy. However, in the case of a fixed link phy, we force the port
582 * settings from the fixed link settings.
583 */
Vivien Didelotf81ec902016-05-09 13:22:58 -0400584static void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port,
585 struct phy_device *phydev)
Andrew Lunndea87022015-08-31 15:56:47 +0200586{
Vivien Didelot04bed142016-08-31 18:06:13 -0400587 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn49052872015-09-29 01:53:48 +0200588 u32 reg;
589 int ret;
Andrew Lunndea87022015-08-31 15:56:47 +0200590
591 if (!phy_is_pseudo_fixed_link(phydev))
592 return;
593
Vivien Didelotfad09c72016-06-21 12:28:20 -0400594 mutex_lock(&chip->reg_lock);
Andrew Lunndea87022015-08-31 15:56:47 +0200595
Vivien Didelotfad09c72016-06-21 12:28:20 -0400596 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_PCS_CTRL);
Andrew Lunndea87022015-08-31 15:56:47 +0200597 if (ret < 0)
598 goto out;
599
600 reg = ret & ~(PORT_PCS_CTRL_LINK_UP |
601 PORT_PCS_CTRL_FORCE_LINK |
602 PORT_PCS_CTRL_DUPLEX_FULL |
603 PORT_PCS_CTRL_FORCE_DUPLEX |
604 PORT_PCS_CTRL_UNFORCED);
605
606 reg |= PORT_PCS_CTRL_FORCE_LINK;
607 if (phydev->link)
Vivien Didelot57d32312016-06-20 13:13:58 -0400608 reg |= PORT_PCS_CTRL_LINK_UP;
Andrew Lunndea87022015-08-31 15:56:47 +0200609
Vivien Didelotfad09c72016-06-21 12:28:20 -0400610 if (mv88e6xxx_6065_family(chip) && phydev->speed > SPEED_100)
Andrew Lunndea87022015-08-31 15:56:47 +0200611 goto out;
612
613 switch (phydev->speed) {
614 case SPEED_1000:
615 reg |= PORT_PCS_CTRL_1000;
616 break;
617 case SPEED_100:
618 reg |= PORT_PCS_CTRL_100;
619 break;
620 case SPEED_10:
621 reg |= PORT_PCS_CTRL_10;
622 break;
623 default:
624 pr_info("Unknown speed");
625 goto out;
626 }
627
628 reg |= PORT_PCS_CTRL_FORCE_DUPLEX;
629 if (phydev->duplex == DUPLEX_FULL)
630 reg |= PORT_PCS_CTRL_DUPLEX_FULL;
631
Vivien Didelotfad09c72016-06-21 12:28:20 -0400632 if ((mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip)) &&
633 (port >= chip->info->num_ports - 2)) {
Andrew Lunne7e72ac2015-08-31 15:56:51 +0200634 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
635 reg |= PORT_PCS_CTRL_RGMII_DELAY_RXCLK;
636 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
637 reg |= PORT_PCS_CTRL_RGMII_DELAY_TXCLK;
638 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
639 reg |= (PORT_PCS_CTRL_RGMII_DELAY_RXCLK |
640 PORT_PCS_CTRL_RGMII_DELAY_TXCLK);
641 }
Vivien Didelotfad09c72016-06-21 12:28:20 -0400642 _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_PCS_CTRL, reg);
Andrew Lunndea87022015-08-31 15:56:47 +0200643
644out:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400645 mutex_unlock(&chip->reg_lock);
Andrew Lunndea87022015-08-31 15:56:47 +0200646}
647
Vivien Didelotfad09c72016-06-21 12:28:20 -0400648static int _mv88e6xxx_stats_wait(struct mv88e6xxx_chip *chip)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000649{
650 int ret;
651 int i;
652
653 for (i = 0; i < 10; i++) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400654 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_OP);
Andrew Lunncca8b132015-04-02 04:06:39 +0200655 if ((ret & GLOBAL_STATS_OP_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000656 return 0;
657 }
658
659 return -ETIMEDOUT;
660}
661
Vivien Didelotfad09c72016-06-21 12:28:20 -0400662static int _mv88e6xxx_stats_snapshot(struct mv88e6xxx_chip *chip, int port)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000663{
664 int ret;
665
Vivien Didelotfad09c72016-06-21 12:28:20 -0400666 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200667 port = (port + 1) << 5;
668
Barry Grussling3675c8d2013-01-08 16:05:53 +0000669 /* Snapshot the hardware statistics counters for this port. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400670 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
Andrew Lunn31888232015-05-06 01:09:54 +0200671 GLOBAL_STATS_OP_CAPTURE_PORT |
672 GLOBAL_STATS_OP_HIST_RX_TX | port);
673 if (ret < 0)
674 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000675
Barry Grussling3675c8d2013-01-08 16:05:53 +0000676 /* Wait for the snapshotting to complete. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400677 ret = _mv88e6xxx_stats_wait(chip);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000678 if (ret < 0)
679 return ret;
680
681 return 0;
682}
683
Vivien Didelotfad09c72016-06-21 12:28:20 -0400684static void _mv88e6xxx_stats_read(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -0400685 int stat, u32 *val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000686{
687 u32 _val;
688 int ret;
689
690 *val = 0;
691
Vivien Didelotfad09c72016-06-21 12:28:20 -0400692 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
Andrew Lunn31888232015-05-06 01:09:54 +0200693 GLOBAL_STATS_OP_READ_CAPTURED |
694 GLOBAL_STATS_OP_HIST_RX_TX | stat);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000695 if (ret < 0)
696 return;
697
Vivien Didelotfad09c72016-06-21 12:28:20 -0400698 ret = _mv88e6xxx_stats_wait(chip);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000699 if (ret < 0)
700 return;
701
Vivien Didelotfad09c72016-06-21 12:28:20 -0400702 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_COUNTER_32);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000703 if (ret < 0)
704 return;
705
706 _val = ret << 16;
707
Vivien Didelotfad09c72016-06-21 12:28:20 -0400708 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_COUNTER_01);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000709 if (ret < 0)
710 return;
711
712 *val = _val | ret;
713}
714
Andrew Lunne413e7e2015-04-02 04:06:38 +0200715static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100716 { "in_good_octets", 8, 0x00, BANK0, },
717 { "in_bad_octets", 4, 0x02, BANK0, },
718 { "in_unicast", 4, 0x04, BANK0, },
719 { "in_broadcasts", 4, 0x06, BANK0, },
720 { "in_multicasts", 4, 0x07, BANK0, },
721 { "in_pause", 4, 0x16, BANK0, },
722 { "in_undersize", 4, 0x18, BANK0, },
723 { "in_fragments", 4, 0x19, BANK0, },
724 { "in_oversize", 4, 0x1a, BANK0, },
725 { "in_jabber", 4, 0x1b, BANK0, },
726 { "in_rx_error", 4, 0x1c, BANK0, },
727 { "in_fcs_error", 4, 0x1d, BANK0, },
728 { "out_octets", 8, 0x0e, BANK0, },
729 { "out_unicast", 4, 0x10, BANK0, },
730 { "out_broadcasts", 4, 0x13, BANK0, },
731 { "out_multicasts", 4, 0x12, BANK0, },
732 { "out_pause", 4, 0x15, BANK0, },
733 { "excessive", 4, 0x11, BANK0, },
734 { "collisions", 4, 0x1e, BANK0, },
735 { "deferred", 4, 0x05, BANK0, },
736 { "single", 4, 0x14, BANK0, },
737 { "multiple", 4, 0x17, BANK0, },
738 { "out_fcs_error", 4, 0x03, BANK0, },
739 { "late", 4, 0x1f, BANK0, },
740 { "hist_64bytes", 4, 0x08, BANK0, },
741 { "hist_65_127bytes", 4, 0x09, BANK0, },
742 { "hist_128_255bytes", 4, 0x0a, BANK0, },
743 { "hist_256_511bytes", 4, 0x0b, BANK0, },
744 { "hist_512_1023bytes", 4, 0x0c, BANK0, },
745 { "hist_1024_max_bytes", 4, 0x0d, BANK0, },
746 { "sw_in_discards", 4, 0x10, PORT, },
747 { "sw_in_filtered", 2, 0x12, PORT, },
748 { "sw_out_filtered", 2, 0x13, PORT, },
749 { "in_discards", 4, 0x00 | GLOBAL_STATS_OP_BANK_1, BANK1, },
750 { "in_filtered", 4, 0x01 | GLOBAL_STATS_OP_BANK_1, BANK1, },
751 { "in_accepted", 4, 0x02 | GLOBAL_STATS_OP_BANK_1, BANK1, },
752 { "in_bad_accepted", 4, 0x03 | GLOBAL_STATS_OP_BANK_1, BANK1, },
753 { "in_good_avb_class_a", 4, 0x04 | GLOBAL_STATS_OP_BANK_1, BANK1, },
754 { "in_good_avb_class_b", 4, 0x05 | GLOBAL_STATS_OP_BANK_1, BANK1, },
755 { "in_bad_avb_class_a", 4, 0x06 | GLOBAL_STATS_OP_BANK_1, BANK1, },
756 { "in_bad_avb_class_b", 4, 0x07 | GLOBAL_STATS_OP_BANK_1, BANK1, },
757 { "tcam_counter_0", 4, 0x08 | GLOBAL_STATS_OP_BANK_1, BANK1, },
758 { "tcam_counter_1", 4, 0x09 | GLOBAL_STATS_OP_BANK_1, BANK1, },
759 { "tcam_counter_2", 4, 0x0a | GLOBAL_STATS_OP_BANK_1, BANK1, },
760 { "tcam_counter_3", 4, 0x0b | GLOBAL_STATS_OP_BANK_1, BANK1, },
761 { "in_da_unknown", 4, 0x0e | GLOBAL_STATS_OP_BANK_1, BANK1, },
762 { "in_management", 4, 0x0f | GLOBAL_STATS_OP_BANK_1, BANK1, },
763 { "out_queue_0", 4, 0x10 | GLOBAL_STATS_OP_BANK_1, BANK1, },
764 { "out_queue_1", 4, 0x11 | GLOBAL_STATS_OP_BANK_1, BANK1, },
765 { "out_queue_2", 4, 0x12 | GLOBAL_STATS_OP_BANK_1, BANK1, },
766 { "out_queue_3", 4, 0x13 | GLOBAL_STATS_OP_BANK_1, BANK1, },
767 { "out_queue_4", 4, 0x14 | GLOBAL_STATS_OP_BANK_1, BANK1, },
768 { "out_queue_5", 4, 0x15 | GLOBAL_STATS_OP_BANK_1, BANK1, },
769 { "out_queue_6", 4, 0x16 | GLOBAL_STATS_OP_BANK_1, BANK1, },
770 { "out_queue_7", 4, 0x17 | GLOBAL_STATS_OP_BANK_1, BANK1, },
771 { "out_cut_through", 4, 0x18 | GLOBAL_STATS_OP_BANK_1, BANK1, },
772 { "out_octets_a", 4, 0x1a | GLOBAL_STATS_OP_BANK_1, BANK1, },
773 { "out_octets_b", 4, 0x1b | GLOBAL_STATS_OP_BANK_1, BANK1, },
774 { "out_management", 4, 0x1f | GLOBAL_STATS_OP_BANK_1, BANK1, },
Andrew Lunne413e7e2015-04-02 04:06:38 +0200775};
776
Vivien Didelotfad09c72016-06-21 12:28:20 -0400777static bool mv88e6xxx_has_stat(struct mv88e6xxx_chip *chip,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100778 struct mv88e6xxx_hw_stat *stat)
Andrew Lunne413e7e2015-04-02 04:06:38 +0200779{
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100780 switch (stat->type) {
781 case BANK0:
Andrew Lunne413e7e2015-04-02 04:06:38 +0200782 return true;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100783 case BANK1:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400784 return mv88e6xxx_6320_family(chip);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100785 case PORT:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400786 return mv88e6xxx_6095_family(chip) ||
787 mv88e6xxx_6185_family(chip) ||
788 mv88e6xxx_6097_family(chip) ||
789 mv88e6xxx_6165_family(chip) ||
790 mv88e6xxx_6351_family(chip) ||
791 mv88e6xxx_6352_family(chip);
Andrew Lunne413e7e2015-04-02 04:06:38 +0200792 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100793 return false;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000794}
795
Vivien Didelotfad09c72016-06-21 12:28:20 -0400796static uint64_t _mv88e6xxx_get_ethtool_stat(struct mv88e6xxx_chip *chip,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100797 struct mv88e6xxx_hw_stat *s,
Andrew Lunn80c46272015-06-20 18:42:30 +0200798 int port)
799{
Andrew Lunn80c46272015-06-20 18:42:30 +0200800 u32 low;
801 u32 high = 0;
802 int ret;
803 u64 value;
804
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100805 switch (s->type) {
806 case PORT:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400807 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), s->reg);
Andrew Lunn80c46272015-06-20 18:42:30 +0200808 if (ret < 0)
809 return UINT64_MAX;
810
811 low = ret;
812 if (s->sizeof_stat == 4) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400813 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port),
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100814 s->reg + 1);
Andrew Lunn80c46272015-06-20 18:42:30 +0200815 if (ret < 0)
816 return UINT64_MAX;
817 high = ret;
818 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100819 break;
820 case BANK0:
821 case BANK1:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400822 _mv88e6xxx_stats_read(chip, s->reg, &low);
Andrew Lunn80c46272015-06-20 18:42:30 +0200823 if (s->sizeof_stat == 8)
Vivien Didelotfad09c72016-06-21 12:28:20 -0400824 _mv88e6xxx_stats_read(chip, s->reg + 1, &high);
Andrew Lunn80c46272015-06-20 18:42:30 +0200825 }
826 value = (((u64)high) << 16) | low;
827 return value;
828}
829
Vivien Didelotf81ec902016-05-09 13:22:58 -0400830static void mv88e6xxx_get_strings(struct dsa_switch *ds, int port,
831 uint8_t *data)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100832{
Vivien Didelot04bed142016-08-31 18:06:13 -0400833 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100834 struct mv88e6xxx_hw_stat *stat;
835 int i, j;
836
837 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
838 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400839 if (mv88e6xxx_has_stat(chip, stat)) {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100840 memcpy(data + j * ETH_GSTRING_LEN, stat->string,
841 ETH_GSTRING_LEN);
842 j++;
843 }
844 }
845}
846
Vivien Didelotf81ec902016-05-09 13:22:58 -0400847static int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100848{
Vivien Didelot04bed142016-08-31 18:06:13 -0400849 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100850 struct mv88e6xxx_hw_stat *stat;
851 int i, j;
852
853 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
854 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400855 if (mv88e6xxx_has_stat(chip, stat))
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100856 j++;
857 }
858 return j;
859}
860
Vivien Didelotf81ec902016-05-09 13:22:58 -0400861static void mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, int port,
862 uint64_t *data)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000863{
Vivien Didelot04bed142016-08-31 18:06:13 -0400864 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100865 struct mv88e6xxx_hw_stat *stat;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000866 int ret;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100867 int i, j;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000868
Vivien Didelotfad09c72016-06-21 12:28:20 -0400869 mutex_lock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000870
Vivien Didelotfad09c72016-06-21 12:28:20 -0400871 ret = _mv88e6xxx_stats_snapshot(chip, port);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000872 if (ret < 0) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400873 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000874 return;
875 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100876 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
877 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400878 if (mv88e6xxx_has_stat(chip, stat)) {
879 data[j] = _mv88e6xxx_get_ethtool_stat(chip, stat, port);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100880 j++;
881 }
882 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000883
Vivien Didelotfad09c72016-06-21 12:28:20 -0400884 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000885}
Ben Hutchings98e67302011-11-25 14:36:19 +0000886
Vivien Didelotf81ec902016-05-09 13:22:58 -0400887static int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700888{
889 return 32 * sizeof(u16);
890}
891
Vivien Didelotf81ec902016-05-09 13:22:58 -0400892static void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
893 struct ethtool_regs *regs, void *_p)
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700894{
Vivien Didelot04bed142016-08-31 18:06:13 -0400895 struct mv88e6xxx_chip *chip = ds->priv;
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700896 u16 *p = _p;
897 int i;
898
899 regs->version = 0;
900
901 memset(p, 0xff, 32 * sizeof(u16));
902
Vivien Didelotfad09c72016-06-21 12:28:20 -0400903 mutex_lock(&chip->reg_lock);
Vivien Didelot23062512016-05-09 13:22:45 -0400904
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700905 for (i = 0; i < 32; i++) {
906 int ret;
907
Vivien Didelotfad09c72016-06-21 12:28:20 -0400908 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), i);
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700909 if (ret >= 0)
910 p[i] = ret;
911 }
Vivien Didelot23062512016-05-09 13:22:45 -0400912
Vivien Didelotfad09c72016-06-21 12:28:20 -0400913 mutex_unlock(&chip->reg_lock);
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700914}
915
Vivien Didelotfad09c72016-06-21 12:28:20 -0400916static int _mv88e6xxx_atu_wait(struct mv88e6xxx_chip *chip)
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700917{
Vivien Didelot2d79af62016-08-15 17:18:57 -0400918 return mv88e6xxx_wait(chip, REG_GLOBAL, GLOBAL_ATU_OP,
919 GLOBAL_ATU_OP_BUSY);
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700920}
921
Vivien Didelotf81ec902016-05-09 13:22:58 -0400922static int mv88e6xxx_get_eee(struct dsa_switch *ds, int port,
923 struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -0800924{
Vivien Didelot04bed142016-08-31 18:06:13 -0400925 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot9c938292016-08-15 17:19:02 -0400926 u16 reg;
927 int err;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800928
Vivien Didelotfad09c72016-06-21 12:28:20 -0400929 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
Vivien Didelotaadbdb82016-05-09 13:22:44 -0400930 return -EOPNOTSUPP;
931
Vivien Didelotfad09c72016-06-21 12:28:20 -0400932 mutex_lock(&chip->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200933
Vivien Didelot9c938292016-08-15 17:19:02 -0400934 err = mv88e6xxx_phy_read(chip, port, 16, &reg);
935 if (err)
Andrew Lunn2f40c692015-04-02 04:06:37 +0200936 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800937
938 e->eee_enabled = !!(reg & 0x0200);
939 e->tx_lpi_enabled = !!(reg & 0x0100);
940
Vivien Didelot9c938292016-08-15 17:19:02 -0400941 err = mv88e6xxx_read(chip, REG_PORT(port), PORT_STATUS, &reg);
942 if (err)
Andrew Lunn2f40c692015-04-02 04:06:37 +0200943 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800944
Andrew Lunncca8b132015-04-02 04:06:39 +0200945 e->eee_active = !!(reg & PORT_STATUS_EEE);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200946out:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400947 mutex_unlock(&chip->reg_lock);
Vivien Didelot9c938292016-08-15 17:19:02 -0400948
949 return err;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800950}
951
Vivien Didelotf81ec902016-05-09 13:22:58 -0400952static int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
953 struct phy_device *phydev, struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -0800954{
Vivien Didelot04bed142016-08-31 18:06:13 -0400955 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot9c938292016-08-15 17:19:02 -0400956 u16 reg;
957 int err;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800958
Vivien Didelotfad09c72016-06-21 12:28:20 -0400959 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
Vivien Didelotaadbdb82016-05-09 13:22:44 -0400960 return -EOPNOTSUPP;
961
Vivien Didelotfad09c72016-06-21 12:28:20 -0400962 mutex_lock(&chip->reg_lock);
Guenter Roeck11b3b452015-03-06 22:23:51 -0800963
Vivien Didelot9c938292016-08-15 17:19:02 -0400964 err = mv88e6xxx_phy_read(chip, port, 16, &reg);
965 if (err)
Andrew Lunn2f40c692015-04-02 04:06:37 +0200966 goto out;
967
Vivien Didelot9c938292016-08-15 17:19:02 -0400968 reg &= ~0x0300;
Andrew Lunn2f40c692015-04-02 04:06:37 +0200969 if (e->eee_enabled)
970 reg |= 0x0200;
971 if (e->tx_lpi_enabled)
972 reg |= 0x0100;
973
Vivien Didelot9c938292016-08-15 17:19:02 -0400974 err = mv88e6xxx_phy_write(chip, port, 16, reg);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200975out:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400976 mutex_unlock(&chip->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200977
Vivien Didelot9c938292016-08-15 17:19:02 -0400978 return err;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800979}
980
Vivien Didelotfad09c72016-06-21 12:28:20 -0400981static int _mv88e6xxx_atu_cmd(struct mv88e6xxx_chip *chip, u16 fid, u16 cmd)
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700982{
983 int ret;
984
Vivien Didelotfad09c72016-06-21 12:28:20 -0400985 if (mv88e6xxx_has_fid_reg(chip)) {
986 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_FID,
987 fid);
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400988 if (ret < 0)
989 return ret;
Vivien Didelotfad09c72016-06-21 12:28:20 -0400990 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -0400991 /* ATU DBNum[7:4] are located in ATU Control 15:12 */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400992 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL);
Vivien Didelot11ea8092016-03-31 16:53:44 -0400993 if (ret < 0)
994 return ret;
995
Vivien Didelotfad09c72016-06-21 12:28:20 -0400996 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL,
Vivien Didelot11ea8092016-03-31 16:53:44 -0400997 (ret & 0xfff) |
998 ((fid << 8) & 0xf000));
999 if (ret < 0)
1000 return ret;
1001
1002 /* ATU DBNum[3:0] are located in ATU Operation 3:0 */
1003 cmd |= fid & 0xf;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001004 }
1005
Vivien Didelotfad09c72016-06-21 12:28:20 -04001006 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_OP, cmd);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001007 if (ret < 0)
1008 return ret;
1009
Vivien Didelotfad09c72016-06-21 12:28:20 -04001010 return _mv88e6xxx_atu_wait(chip);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001011}
1012
Vivien Didelotfad09c72016-06-21 12:28:20 -04001013static int _mv88e6xxx_atu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot37705b72015-09-04 14:34:11 -04001014 struct mv88e6xxx_atu_entry *entry)
1015{
1016 u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK;
1017
1018 if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) {
1019 unsigned int mask, shift;
1020
1021 if (entry->trunk) {
1022 data |= GLOBAL_ATU_DATA_TRUNK;
1023 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
1024 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
1025 } else {
1026 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
1027 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
1028 }
1029
1030 data |= (entry->portv_trunkid << shift) & mask;
1031 }
1032
Vivien Didelotfad09c72016-06-21 12:28:20 -04001033 return _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_DATA, data);
Vivien Didelot37705b72015-09-04 14:34:11 -04001034}
1035
Vivien Didelotfad09c72016-06-21 12:28:20 -04001036static int _mv88e6xxx_atu_flush_move(struct mv88e6xxx_chip *chip,
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001037 struct mv88e6xxx_atu_entry *entry,
1038 bool static_too)
1039{
1040 int op;
1041 int err;
1042
Vivien Didelotfad09c72016-06-21 12:28:20 -04001043 err = _mv88e6xxx_atu_wait(chip);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001044 if (err)
1045 return err;
1046
Vivien Didelotfad09c72016-06-21 12:28:20 -04001047 err = _mv88e6xxx_atu_data_write(chip, entry);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001048 if (err)
1049 return err;
1050
1051 if (entry->fid) {
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001052 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB :
1053 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB;
1054 } else {
1055 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL :
1056 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC;
1057 }
1058
Vivien Didelotfad09c72016-06-21 12:28:20 -04001059 return _mv88e6xxx_atu_cmd(chip, entry->fid, op);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001060}
1061
Vivien Didelotfad09c72016-06-21 12:28:20 -04001062static int _mv88e6xxx_atu_flush(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001063 u16 fid, bool static_too)
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001064{
1065 struct mv88e6xxx_atu_entry entry = {
1066 .fid = fid,
1067 .state = 0, /* EntryState bits must be 0 */
1068 };
1069
Vivien Didelotfad09c72016-06-21 12:28:20 -04001070 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001071}
1072
Vivien Didelotfad09c72016-06-21 12:28:20 -04001073static int _mv88e6xxx_atu_move(struct mv88e6xxx_chip *chip, u16 fid,
Andrew Lunn158bc062016-04-28 21:24:06 -04001074 int from_port, int to_port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001075{
1076 struct mv88e6xxx_atu_entry entry = {
1077 .trunk = false,
1078 .fid = fid,
1079 };
1080
1081 /* EntryState bits must be 0xF */
1082 entry.state = GLOBAL_ATU_DATA_STATE_MASK;
1083
1084 /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */
1085 entry.portv_trunkid = (to_port & 0x0f) << 4;
1086 entry.portv_trunkid |= from_port & 0x0f;
1087
Vivien Didelotfad09c72016-06-21 12:28:20 -04001088 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001089}
1090
Vivien Didelotfad09c72016-06-21 12:28:20 -04001091static int _mv88e6xxx_atu_remove(struct mv88e6xxx_chip *chip, u16 fid,
Andrew Lunn158bc062016-04-28 21:24:06 -04001092 int port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001093{
1094 /* Destination port 0xF means remove the entries */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001095 return _mv88e6xxx_atu_move(chip, fid, port, 0x0f, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001096}
1097
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001098static const char * const mv88e6xxx_port_state_names[] = {
1099 [PORT_CONTROL_STATE_DISABLED] = "Disabled",
1100 [PORT_CONTROL_STATE_BLOCKING] = "Blocking/Listening",
1101 [PORT_CONTROL_STATE_LEARNING] = "Learning",
1102 [PORT_CONTROL_STATE_FORWARDING] = "Forwarding",
1103};
1104
Vivien Didelotfad09c72016-06-21 12:28:20 -04001105static int _mv88e6xxx_port_state(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001106 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001107{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001108 struct dsa_switch *ds = chip->ds;
Geert Uytterhoevenc3ffe6d2015-04-16 20:49:14 +02001109 int reg, ret = 0;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001110 u8 oldstate;
1111
Vivien Didelotfad09c72016-06-21 12:28:20 -04001112 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL);
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001113 if (reg < 0)
1114 return reg;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001115
Andrew Lunncca8b132015-04-02 04:06:39 +02001116 oldstate = reg & PORT_CONTROL_STATE_MASK;
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001117
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001118 if (oldstate != state) {
1119 /* Flush forwarding database if we're moving a port
1120 * from Learning or Forwarding state to Disabled or
1121 * Blocking or Listening state.
1122 */
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001123 if ((oldstate == PORT_CONTROL_STATE_LEARNING ||
Vivien Didelot57d32312016-06-20 13:13:58 -04001124 oldstate == PORT_CONTROL_STATE_FORWARDING) &&
1125 (state == PORT_CONTROL_STATE_DISABLED ||
1126 state == PORT_CONTROL_STATE_BLOCKING)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001127 ret = _mv88e6xxx_atu_remove(chip, 0, port, false);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001128 if (ret)
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001129 return ret;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001130 }
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001131
Andrew Lunncca8b132015-04-02 04:06:39 +02001132 reg = (reg & ~PORT_CONTROL_STATE_MASK) | state;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001133 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL,
Andrew Lunncca8b132015-04-02 04:06:39 +02001134 reg);
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001135 if (ret)
1136 return ret;
1137
Andrew Lunnc8b09802016-06-04 21:16:57 +02001138 netdev_dbg(ds->ports[port].netdev, "PortState %s (was %s)\n",
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001139 mv88e6xxx_port_state_names[state],
1140 mv88e6xxx_port_state_names[oldstate]);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001141 }
1142
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001143 return ret;
1144}
1145
Vivien Didelotfad09c72016-06-21 12:28:20 -04001146static int _mv88e6xxx_port_based_vlan_map(struct mv88e6xxx_chip *chip, int port)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001147{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001148 struct net_device *bridge = chip->ports[port].bridge_dev;
1149 const u16 mask = (1 << chip->info->num_ports) - 1;
1150 struct dsa_switch *ds = chip->ds;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001151 u16 output_ports = 0;
Vivien Didelotede80982015-10-11 18:08:35 -04001152 int reg;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001153 int i;
1154
1155 /* allow CPU port or DSA link(s) to send frames to every port */
1156 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
1157 output_ports = mask;
1158 } else {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001159 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001160 /* allow sending frames to every group member */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001161 if (bridge && chip->ports[i].bridge_dev == bridge)
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001162 output_ports |= BIT(i);
1163
1164 /* allow sending frames to CPU port and DSA link(s) */
1165 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
1166 output_ports |= BIT(i);
1167 }
1168 }
1169
1170 /* prevent frames from going back out of the port they came in on */
1171 output_ports &= ~BIT(port);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001172
Vivien Didelotfad09c72016-06-21 12:28:20 -04001173 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_BASE_VLAN);
Vivien Didelotede80982015-10-11 18:08:35 -04001174 if (reg < 0)
1175 return reg;
1176
1177 reg &= ~mask;
1178 reg |= output_ports & mask;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001179
Vivien Didelotfad09c72016-06-21 12:28:20 -04001180 return _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_BASE_VLAN, reg);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001181}
1182
Vivien Didelotf81ec902016-05-09 13:22:58 -04001183static void mv88e6xxx_port_stp_state_set(struct dsa_switch *ds, int port,
1184 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001185{
Vivien Didelot04bed142016-08-31 18:06:13 -04001186 struct mv88e6xxx_chip *chip = ds->priv;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001187 int stp_state;
Vivien Didelot553eb542016-05-13 20:38:23 -04001188 int err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001189
1190 switch (state) {
1191 case BR_STATE_DISABLED:
Andrew Lunncca8b132015-04-02 04:06:39 +02001192 stp_state = PORT_CONTROL_STATE_DISABLED;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001193 break;
1194 case BR_STATE_BLOCKING:
1195 case BR_STATE_LISTENING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001196 stp_state = PORT_CONTROL_STATE_BLOCKING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001197 break;
1198 case BR_STATE_LEARNING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001199 stp_state = PORT_CONTROL_STATE_LEARNING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001200 break;
1201 case BR_STATE_FORWARDING:
1202 default:
Andrew Lunncca8b132015-04-02 04:06:39 +02001203 stp_state = PORT_CONTROL_STATE_FORWARDING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001204 break;
1205 }
1206
Vivien Didelotfad09c72016-06-21 12:28:20 -04001207 mutex_lock(&chip->reg_lock);
1208 err = _mv88e6xxx_port_state(chip, port, stp_state);
1209 mutex_unlock(&chip->reg_lock);
Vivien Didelot553eb542016-05-13 20:38:23 -04001210
1211 if (err)
Andrew Lunnc8b09802016-06-04 21:16:57 +02001212 netdev_err(ds->ports[port].netdev,
1213 "failed to update state to %s\n",
Vivien Didelot553eb542016-05-13 20:38:23 -04001214 mv88e6xxx_port_state_names[stp_state]);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001215}
1216
Vivien Didelotfad09c72016-06-21 12:28:20 -04001217static int _mv88e6xxx_port_pvid(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001218 u16 *new, u16 *old)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001219{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001220 struct dsa_switch *ds = chip->ds;
Vivien Didelot5da96032016-03-07 18:24:39 -05001221 u16 pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001222 int ret;
1223
Vivien Didelotfad09c72016-06-21 12:28:20 -04001224 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_DEFAULT_VLAN);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001225 if (ret < 0)
1226 return ret;
1227
Vivien Didelot5da96032016-03-07 18:24:39 -05001228 pvid = ret & PORT_DEFAULT_VLAN_MASK;
1229
1230 if (new) {
1231 ret &= ~PORT_DEFAULT_VLAN_MASK;
1232 ret |= *new & PORT_DEFAULT_VLAN_MASK;
1233
Vivien Didelotfad09c72016-06-21 12:28:20 -04001234 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Vivien Didelot5da96032016-03-07 18:24:39 -05001235 PORT_DEFAULT_VLAN, ret);
1236 if (ret < 0)
1237 return ret;
1238
Andrew Lunnc8b09802016-06-04 21:16:57 +02001239 netdev_dbg(ds->ports[port].netdev,
1240 "DefaultVID %d (was %d)\n", *new, pvid);
Vivien Didelot5da96032016-03-07 18:24:39 -05001241 }
1242
1243 if (old)
1244 *old = pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001245
1246 return 0;
1247}
1248
Vivien Didelotfad09c72016-06-21 12:28:20 -04001249static int _mv88e6xxx_port_pvid_get(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001250 int port, u16 *pvid)
Vivien Didelot5da96032016-03-07 18:24:39 -05001251{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001252 return _mv88e6xxx_port_pvid(chip, port, NULL, pvid);
Vivien Didelot5da96032016-03-07 18:24:39 -05001253}
1254
Vivien Didelotfad09c72016-06-21 12:28:20 -04001255static int _mv88e6xxx_port_pvid_set(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001256 int port, u16 pvid)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001257{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001258 return _mv88e6xxx_port_pvid(chip, port, &pvid, NULL);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001259}
1260
Vivien Didelotfad09c72016-06-21 12:28:20 -04001261static int _mv88e6xxx_vtu_wait(struct mv88e6xxx_chip *chip)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001262{
Vivien Didelot2d79af62016-08-15 17:18:57 -04001263 return mv88e6xxx_wait(chip, REG_GLOBAL, GLOBAL_VTU_OP,
1264 GLOBAL_VTU_OP_BUSY);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001265}
1266
Vivien Didelotfad09c72016-06-21 12:28:20 -04001267static int _mv88e6xxx_vtu_cmd(struct mv88e6xxx_chip *chip, u16 op)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001268{
1269 int ret;
1270
Vivien Didelotfad09c72016-06-21 12:28:20 -04001271 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_OP, op);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001272 if (ret < 0)
1273 return ret;
1274
Vivien Didelotfad09c72016-06-21 12:28:20 -04001275 return _mv88e6xxx_vtu_wait(chip);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001276}
1277
Vivien Didelotfad09c72016-06-21 12:28:20 -04001278static int _mv88e6xxx_vtu_stu_flush(struct mv88e6xxx_chip *chip)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001279{
1280 int ret;
1281
Vivien Didelotfad09c72016-06-21 12:28:20 -04001282 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001283 if (ret < 0)
1284 return ret;
1285
Vivien Didelotfad09c72016-06-21 12:28:20 -04001286 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_FLUSH_ALL);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001287}
1288
Vivien Didelotfad09c72016-06-21 12:28:20 -04001289static int _mv88e6xxx_vtu_stu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001290 struct mv88e6xxx_vtu_stu_entry *entry,
1291 unsigned int nibble_offset)
1292{
Vivien Didelotb8fee952015-08-13 12:52:19 -04001293 u16 regs[3];
1294 int i;
1295 int ret;
1296
1297 for (i = 0; i < 3; ++i) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001298 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001299 GLOBAL_VTU_DATA_0_3 + i);
1300 if (ret < 0)
1301 return ret;
1302
1303 regs[i] = ret;
1304 }
1305
Vivien Didelotfad09c72016-06-21 12:28:20 -04001306 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelotb8fee952015-08-13 12:52:19 -04001307 unsigned int shift = (i % 4) * 4 + nibble_offset;
1308 u16 reg = regs[i / 4];
1309
1310 entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
1311 }
1312
1313 return 0;
1314}
1315
Vivien Didelotfad09c72016-06-21 12:28:20 -04001316static int mv88e6xxx_vtu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001317 struct mv88e6xxx_vtu_stu_entry *entry)
1318{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001319 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 0);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001320}
1321
Vivien Didelotfad09c72016-06-21 12:28:20 -04001322static int mv88e6xxx_stu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001323 struct mv88e6xxx_vtu_stu_entry *entry)
1324{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001325 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 2);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001326}
1327
Vivien Didelotfad09c72016-06-21 12:28:20 -04001328static int _mv88e6xxx_vtu_stu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001329 struct mv88e6xxx_vtu_stu_entry *entry,
1330 unsigned int nibble_offset)
1331{
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001332 u16 regs[3] = { 0 };
1333 int i;
1334 int ret;
1335
Vivien Didelotfad09c72016-06-21 12:28:20 -04001336 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001337 unsigned int shift = (i % 4) * 4 + nibble_offset;
1338 u8 data = entry->data[i];
1339
1340 regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
1341 }
1342
1343 for (i = 0; i < 3; ++i) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001344 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001345 GLOBAL_VTU_DATA_0_3 + i, regs[i]);
1346 if (ret < 0)
1347 return ret;
1348 }
1349
1350 return 0;
1351}
1352
Vivien Didelotfad09c72016-06-21 12:28:20 -04001353static int mv88e6xxx_vtu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001354 struct mv88e6xxx_vtu_stu_entry *entry)
1355{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001356 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 0);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001357}
1358
Vivien Didelotfad09c72016-06-21 12:28:20 -04001359static int mv88e6xxx_stu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001360 struct mv88e6xxx_vtu_stu_entry *entry)
1361{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001362 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 2);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001363}
1364
Vivien Didelotfad09c72016-06-21 12:28:20 -04001365static int _mv88e6xxx_vtu_vid_write(struct mv88e6xxx_chip *chip, u16 vid)
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001366{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001367 return _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID,
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001368 vid & GLOBAL_VTU_VID_MASK);
1369}
1370
Vivien Didelotfad09c72016-06-21 12:28:20 -04001371static int _mv88e6xxx_vtu_getnext(struct mv88e6xxx_chip *chip,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001372 struct mv88e6xxx_vtu_stu_entry *entry)
1373{
1374 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1375 int ret;
1376
Vivien Didelotfad09c72016-06-21 12:28:20 -04001377 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001378 if (ret < 0)
1379 return ret;
1380
Vivien Didelotfad09c72016-06-21 12:28:20 -04001381 ret = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_VTU_GET_NEXT);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001382 if (ret < 0)
1383 return ret;
1384
Vivien Didelotfad09c72016-06-21 12:28:20 -04001385 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_VID);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001386 if (ret < 0)
1387 return ret;
1388
1389 next.vid = ret & GLOBAL_VTU_VID_MASK;
1390 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1391
1392 if (next.valid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001393 ret = mv88e6xxx_vtu_data_read(chip, &next);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001394 if (ret < 0)
1395 return ret;
1396
Vivien Didelotfad09c72016-06-21 12:28:20 -04001397 if (mv88e6xxx_has_fid_reg(chip)) {
1398 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001399 GLOBAL_VTU_FID);
1400 if (ret < 0)
1401 return ret;
1402
1403 next.fid = ret & GLOBAL_VTU_FID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001404 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001405 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1406 * VTU DBNum[3:0] are located in VTU Operation 3:0
1407 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001408 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
Vivien Didelot11ea8092016-03-31 16:53:44 -04001409 GLOBAL_VTU_OP);
1410 if (ret < 0)
1411 return ret;
1412
1413 next.fid = (ret & 0xf00) >> 4;
1414 next.fid |= ret & 0xf;
Vivien Didelot2e7bd5e2016-03-31 16:53:41 -04001415 }
Vivien Didelotb8fee952015-08-13 12:52:19 -04001416
Vivien Didelotfad09c72016-06-21 12:28:20 -04001417 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
1418 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001419 GLOBAL_VTU_SID);
1420 if (ret < 0)
1421 return ret;
1422
1423 next.sid = ret & GLOBAL_VTU_SID_MASK;
1424 }
1425 }
1426
1427 *entry = next;
1428 return 0;
1429}
1430
Vivien Didelotf81ec902016-05-09 13:22:58 -04001431static int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
1432 struct switchdev_obj_port_vlan *vlan,
1433 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001434{
Vivien Didelot04bed142016-08-31 18:06:13 -04001435 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001436 struct mv88e6xxx_vtu_stu_entry next;
1437 u16 pvid;
1438 int err;
1439
Vivien Didelotfad09c72016-06-21 12:28:20 -04001440 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001441 return -EOPNOTSUPP;
1442
Vivien Didelotfad09c72016-06-21 12:28:20 -04001443 mutex_lock(&chip->reg_lock);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001444
Vivien Didelotfad09c72016-06-21 12:28:20 -04001445 err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001446 if (err)
1447 goto unlock;
1448
Vivien Didelotfad09c72016-06-21 12:28:20 -04001449 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001450 if (err)
1451 goto unlock;
1452
1453 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001454 err = _mv88e6xxx_vtu_getnext(chip, &next);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001455 if (err)
1456 break;
1457
1458 if (!next.valid)
1459 break;
1460
1461 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1462 continue;
1463
1464 /* reinit and dump this VLAN obj */
Vivien Didelot57d32312016-06-20 13:13:58 -04001465 vlan->vid_begin = next.vid;
1466 vlan->vid_end = next.vid;
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001467 vlan->flags = 0;
1468
1469 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1470 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1471
1472 if (next.vid == pvid)
1473 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1474
1475 err = cb(&vlan->obj);
1476 if (err)
1477 break;
1478 } while (next.vid < GLOBAL_VTU_VID_MASK);
1479
1480unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001481 mutex_unlock(&chip->reg_lock);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001482
1483 return err;
1484}
1485
Vivien Didelotfad09c72016-06-21 12:28:20 -04001486static int _mv88e6xxx_vtu_loadpurge(struct mv88e6xxx_chip *chip,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001487 struct mv88e6xxx_vtu_stu_entry *entry)
1488{
Vivien Didelot11ea8092016-03-31 16:53:44 -04001489 u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001490 u16 reg = 0;
1491 int ret;
1492
Vivien Didelotfad09c72016-06-21 12:28:20 -04001493 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001494 if (ret < 0)
1495 return ret;
1496
1497 if (!entry->valid)
1498 goto loadpurge;
1499
1500 /* Write port member tags */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001501 ret = mv88e6xxx_vtu_data_write(chip, entry);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001502 if (ret < 0)
1503 return ret;
1504
Vivien Didelotfad09c72016-06-21 12:28:20 -04001505 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001506 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001507 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID,
1508 reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001509 if (ret < 0)
1510 return ret;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001511 }
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001512
Vivien Didelotfad09c72016-06-21 12:28:20 -04001513 if (mv88e6xxx_has_fid_reg(chip)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001514 reg = entry->fid & GLOBAL_VTU_FID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001515 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_FID,
1516 reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001517 if (ret < 0)
1518 return ret;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001519 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001520 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1521 * VTU DBNum[3:0] are located in VTU Operation 3:0
1522 */
1523 op |= (entry->fid & 0xf0) << 8;
1524 op |= entry->fid & 0xf;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001525 }
1526
1527 reg = GLOBAL_VTU_VID_VALID;
1528loadpurge:
1529 reg |= entry->vid & GLOBAL_VTU_VID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001530 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID, reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001531 if (ret < 0)
1532 return ret;
1533
Vivien Didelotfad09c72016-06-21 12:28:20 -04001534 return _mv88e6xxx_vtu_cmd(chip, op);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001535}
1536
Vivien Didelotfad09c72016-06-21 12:28:20 -04001537static int _mv88e6xxx_stu_getnext(struct mv88e6xxx_chip *chip, u8 sid,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001538 struct mv88e6xxx_vtu_stu_entry *entry)
1539{
1540 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1541 int ret;
1542
Vivien Didelotfad09c72016-06-21 12:28:20 -04001543 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001544 if (ret < 0)
1545 return ret;
1546
Vivien Didelotfad09c72016-06-21 12:28:20 -04001547 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001548 sid & GLOBAL_VTU_SID_MASK);
1549 if (ret < 0)
1550 return ret;
1551
Vivien Didelotfad09c72016-06-21 12:28:20 -04001552 ret = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_GET_NEXT);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001553 if (ret < 0)
1554 return ret;
1555
Vivien Didelotfad09c72016-06-21 12:28:20 -04001556 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_SID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001557 if (ret < 0)
1558 return ret;
1559
1560 next.sid = ret & GLOBAL_VTU_SID_MASK;
1561
Vivien Didelotfad09c72016-06-21 12:28:20 -04001562 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_VID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001563 if (ret < 0)
1564 return ret;
1565
1566 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1567
1568 if (next.valid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001569 ret = mv88e6xxx_stu_data_read(chip, &next);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001570 if (ret < 0)
1571 return ret;
1572 }
1573
1574 *entry = next;
1575 return 0;
1576}
1577
Vivien Didelotfad09c72016-06-21 12:28:20 -04001578static int _mv88e6xxx_stu_loadpurge(struct mv88e6xxx_chip *chip,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001579 struct mv88e6xxx_vtu_stu_entry *entry)
1580{
1581 u16 reg = 0;
1582 int ret;
1583
Vivien Didelotfad09c72016-06-21 12:28:20 -04001584 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001585 if (ret < 0)
1586 return ret;
1587
1588 if (!entry->valid)
1589 goto loadpurge;
1590
1591 /* Write port states */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001592 ret = mv88e6xxx_stu_data_write(chip, entry);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001593 if (ret < 0)
1594 return ret;
1595
1596 reg = GLOBAL_VTU_VID_VALID;
1597loadpurge:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001598 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID, reg);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001599 if (ret < 0)
1600 return ret;
1601
1602 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001603 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID, reg);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001604 if (ret < 0)
1605 return ret;
1606
Vivien Didelotfad09c72016-06-21 12:28:20 -04001607 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_LOAD_PURGE);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001608}
1609
Vivien Didelotfad09c72016-06-21 12:28:20 -04001610static int _mv88e6xxx_port_fid(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001611 u16 *new, u16 *old)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001612{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001613 struct dsa_switch *ds = chip->ds;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001614 u16 upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001615 u16 fid;
1616 int ret;
1617
Vivien Didelotfad09c72016-06-21 12:28:20 -04001618 if (mv88e6xxx_num_databases(chip) == 4096)
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001619 upper_mask = 0xff;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001620 else if (mv88e6xxx_num_databases(chip) == 256)
Vivien Didelot11ea8092016-03-31 16:53:44 -04001621 upper_mask = 0xf;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001622 else
1623 return -EOPNOTSUPP;
1624
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001625 /* Port's default FID bits 3:0 are located in reg 0x06, offset 12 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001626 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_BASE_VLAN);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001627 if (ret < 0)
1628 return ret;
1629
1630 fid = (ret & PORT_BASE_VLAN_FID_3_0_MASK) >> 12;
1631
1632 if (new) {
1633 ret &= ~PORT_BASE_VLAN_FID_3_0_MASK;
1634 ret |= (*new << 12) & PORT_BASE_VLAN_FID_3_0_MASK;
1635
Vivien Didelotfad09c72016-06-21 12:28:20 -04001636 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_BASE_VLAN,
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001637 ret);
1638 if (ret < 0)
1639 return ret;
1640 }
1641
1642 /* Port's default FID bits 11:4 are located in reg 0x05, offset 0 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001643 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL_1);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001644 if (ret < 0)
1645 return ret;
1646
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001647 fid |= (ret & upper_mask) << 4;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001648
1649 if (new) {
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001650 ret &= ~upper_mask;
1651 ret |= (*new >> 4) & upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001652
Vivien Didelotfad09c72016-06-21 12:28:20 -04001653 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_1,
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001654 ret);
1655 if (ret < 0)
1656 return ret;
1657
Andrew Lunnc8b09802016-06-04 21:16:57 +02001658 netdev_dbg(ds->ports[port].netdev,
1659 "FID %d (was %d)\n", *new, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001660 }
1661
1662 if (old)
1663 *old = fid;
1664
1665 return 0;
1666}
1667
Vivien Didelotfad09c72016-06-21 12:28:20 -04001668static int _mv88e6xxx_port_fid_get(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001669 int port, u16 *fid)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001670{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001671 return _mv88e6xxx_port_fid(chip, port, NULL, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001672}
1673
Vivien Didelotfad09c72016-06-21 12:28:20 -04001674static int _mv88e6xxx_port_fid_set(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001675 int port, u16 fid)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001676{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001677 return _mv88e6xxx_port_fid(chip, port, &fid, NULL);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001678}
1679
Vivien Didelotfad09c72016-06-21 12:28:20 -04001680static int _mv88e6xxx_fid_new(struct mv88e6xxx_chip *chip, u16 *fid)
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001681{
1682 DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
1683 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001684 int i, err;
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001685
1686 bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);
1687
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001688 /* Set every FID bit used by the (un)bridged ports */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001689 for (i = 0; i < chip->info->num_ports; ++i) {
1690 err = _mv88e6xxx_port_fid_get(chip, i, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001691 if (err)
1692 return err;
1693
1694 set_bit(*fid, fid_bitmap);
1695 }
1696
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001697 /* Set every FID bit used by the VLAN entries */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001698 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001699 if (err)
1700 return err;
1701
1702 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001703 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001704 if (err)
1705 return err;
1706
1707 if (!vlan.valid)
1708 break;
1709
1710 set_bit(vlan.fid, fid_bitmap);
1711 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
1712
1713 /* The reset value 0x000 is used to indicate that multiple address
1714 * databases are not needed. Return the next positive available.
1715 */
1716 *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1);
Vivien Didelotfad09c72016-06-21 12:28:20 -04001717 if (unlikely(*fid >= mv88e6xxx_num_databases(chip)))
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001718 return -ENOSPC;
1719
1720 /* Clear the database */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001721 return _mv88e6xxx_atu_flush(chip, *fid, true);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001722}
1723
Vivien Didelotfad09c72016-06-21 12:28:20 -04001724static int _mv88e6xxx_vtu_new(struct mv88e6xxx_chip *chip, u16 vid,
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001725 struct mv88e6xxx_vtu_stu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001726{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001727 struct dsa_switch *ds = chip->ds;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001728 struct mv88e6xxx_vtu_stu_entry vlan = {
1729 .valid = true,
1730 .vid = vid,
1731 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001732 int i, err;
1733
Vivien Didelotfad09c72016-06-21 12:28:20 -04001734 err = _mv88e6xxx_fid_new(chip, &vlan.fid);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001735 if (err)
1736 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001737
Vivien Didelot3d131f02015-11-03 10:52:52 -05001738 /* exclude all ports except the CPU and DSA ports */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001739 for (i = 0; i < chip->info->num_ports; ++i)
Vivien Didelot3d131f02015-11-03 10:52:52 -05001740 vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)
1741 ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED
1742 : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001743
Vivien Didelotfad09c72016-06-21 12:28:20 -04001744 if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
1745 mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip)) {
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001746 struct mv88e6xxx_vtu_stu_entry vstp;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001747
1748 /* Adding a VTU entry requires a valid STU entry. As VSTP is not
1749 * implemented, only one STU entry is needed to cover all VTU
1750 * entries. Thus, validate the SID 0.
1751 */
1752 vlan.sid = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001753 err = _mv88e6xxx_stu_getnext(chip, GLOBAL_VTU_SID_MASK, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001754 if (err)
1755 return err;
1756
1757 if (vstp.sid != vlan.sid || !vstp.valid) {
1758 memset(&vstp, 0, sizeof(vstp));
1759 vstp.valid = true;
1760 vstp.sid = vlan.sid;
1761
Vivien Didelotfad09c72016-06-21 12:28:20 -04001762 err = _mv88e6xxx_stu_loadpurge(chip, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001763 if (err)
1764 return err;
1765 }
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001766 }
1767
1768 *entry = vlan;
1769 return 0;
1770}
1771
Vivien Didelotfad09c72016-06-21 12:28:20 -04001772static int _mv88e6xxx_vtu_get(struct mv88e6xxx_chip *chip, u16 vid,
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001773 struct mv88e6xxx_vtu_stu_entry *entry, bool creat)
1774{
1775 int err;
1776
1777 if (!vid)
1778 return -EINVAL;
1779
Vivien Didelotfad09c72016-06-21 12:28:20 -04001780 err = _mv88e6xxx_vtu_vid_write(chip, vid - 1);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001781 if (err)
1782 return err;
1783
Vivien Didelotfad09c72016-06-21 12:28:20 -04001784 err = _mv88e6xxx_vtu_getnext(chip, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001785 if (err)
1786 return err;
1787
1788 if (entry->vid != vid || !entry->valid) {
1789 if (!creat)
1790 return -EOPNOTSUPP;
1791 /* -ENOENT would've been more appropriate, but switchdev expects
1792 * -EOPNOTSUPP to inform bridge about an eventual software VLAN.
1793 */
1794
Vivien Didelotfad09c72016-06-21 12:28:20 -04001795 err = _mv88e6xxx_vtu_new(chip, vid, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001796 }
1797
1798 return err;
1799}
1800
Vivien Didelotda9c3592016-02-12 12:09:40 -05001801static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
1802 u16 vid_begin, u16 vid_end)
1803{
Vivien Didelot04bed142016-08-31 18:06:13 -04001804 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotda9c3592016-02-12 12:09:40 -05001805 struct mv88e6xxx_vtu_stu_entry vlan;
1806 int i, err;
1807
1808 if (!vid_begin)
1809 return -EOPNOTSUPP;
1810
Vivien Didelotfad09c72016-06-21 12:28:20 -04001811 mutex_lock(&chip->reg_lock);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001812
Vivien Didelotfad09c72016-06-21 12:28:20 -04001813 err = _mv88e6xxx_vtu_vid_write(chip, vid_begin - 1);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001814 if (err)
1815 goto unlock;
1816
1817 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001818 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001819 if (err)
1820 goto unlock;
1821
1822 if (!vlan.valid)
1823 break;
1824
1825 if (vlan.vid > vid_end)
1826 break;
1827
Vivien Didelotfad09c72016-06-21 12:28:20 -04001828 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelotda9c3592016-02-12 12:09:40 -05001829 if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i))
1830 continue;
1831
1832 if (vlan.data[i] ==
1833 GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1834 continue;
1835
Vivien Didelotfad09c72016-06-21 12:28:20 -04001836 if (chip->ports[i].bridge_dev ==
1837 chip->ports[port].bridge_dev)
Vivien Didelotda9c3592016-02-12 12:09:40 -05001838 break; /* same bridge, check next VLAN */
1839
Andrew Lunnc8b09802016-06-04 21:16:57 +02001840 netdev_warn(ds->ports[port].netdev,
Vivien Didelotda9c3592016-02-12 12:09:40 -05001841 "hardware VLAN %d already used by %s\n",
1842 vlan.vid,
Vivien Didelotfad09c72016-06-21 12:28:20 -04001843 netdev_name(chip->ports[i].bridge_dev));
Vivien Didelotda9c3592016-02-12 12:09:40 -05001844 err = -EOPNOTSUPP;
1845 goto unlock;
1846 }
1847 } while (vlan.vid < vid_end);
1848
1849unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001850 mutex_unlock(&chip->reg_lock);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001851
1852 return err;
1853}
1854
Vivien Didelot214cdb92016-02-26 13:16:08 -05001855static const char * const mv88e6xxx_port_8021q_mode_names[] = {
1856 [PORT_CONTROL_2_8021Q_DISABLED] = "Disabled",
1857 [PORT_CONTROL_2_8021Q_FALLBACK] = "Fallback",
1858 [PORT_CONTROL_2_8021Q_CHECK] = "Check",
1859 [PORT_CONTROL_2_8021Q_SECURE] = "Secure",
1860};
1861
Vivien Didelotf81ec902016-05-09 13:22:58 -04001862static int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port,
1863 bool vlan_filtering)
Vivien Didelot214cdb92016-02-26 13:16:08 -05001864{
Vivien Didelot04bed142016-08-31 18:06:13 -04001865 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001866 u16 old, new = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE :
1867 PORT_CONTROL_2_8021Q_DISABLED;
1868 int ret;
1869
Vivien Didelotfad09c72016-06-21 12:28:20 -04001870 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001871 return -EOPNOTSUPP;
1872
Vivien Didelotfad09c72016-06-21 12:28:20 -04001873 mutex_lock(&chip->reg_lock);
Vivien Didelot214cdb92016-02-26 13:16:08 -05001874
Vivien Didelotfad09c72016-06-21 12:28:20 -04001875 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL_2);
Vivien Didelot214cdb92016-02-26 13:16:08 -05001876 if (ret < 0)
1877 goto unlock;
1878
1879 old = ret & PORT_CONTROL_2_8021Q_MASK;
1880
Vivien Didelot5220ef12016-03-07 18:24:52 -05001881 if (new != old) {
1882 ret &= ~PORT_CONTROL_2_8021Q_MASK;
1883 ret |= new & PORT_CONTROL_2_8021Q_MASK;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001884
Vivien Didelotfad09c72016-06-21 12:28:20 -04001885 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_2,
Vivien Didelot5220ef12016-03-07 18:24:52 -05001886 ret);
1887 if (ret < 0)
1888 goto unlock;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001889
Andrew Lunnc8b09802016-06-04 21:16:57 +02001890 netdev_dbg(ds->ports[port].netdev, "802.1Q Mode %s (was %s)\n",
Vivien Didelot5220ef12016-03-07 18:24:52 -05001891 mv88e6xxx_port_8021q_mode_names[new],
1892 mv88e6xxx_port_8021q_mode_names[old]);
1893 }
1894
1895 ret = 0;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001896unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001897 mutex_unlock(&chip->reg_lock);
Vivien Didelot214cdb92016-02-26 13:16:08 -05001898
1899 return ret;
1900}
1901
Vivien Didelot57d32312016-06-20 13:13:58 -04001902static int
1903mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
1904 const struct switchdev_obj_port_vlan *vlan,
1905 struct switchdev_trans *trans)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001906{
Vivien Didelot04bed142016-08-31 18:06:13 -04001907 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotda9c3592016-02-12 12:09:40 -05001908 int err;
1909
Vivien Didelotfad09c72016-06-21 12:28:20 -04001910 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001911 return -EOPNOTSUPP;
1912
Vivien Didelotda9c3592016-02-12 12:09:40 -05001913 /* If the requested port doesn't belong to the same bridge as the VLAN
1914 * members, do not support it (yet) and fallback to software VLAN.
1915 */
1916 err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin,
1917 vlan->vid_end);
1918 if (err)
1919 return err;
1920
Vivien Didelot76e398a2015-11-01 12:33:55 -05001921 /* We don't need any dynamic resource from the kernel (yet),
1922 * so skip the prepare phase.
1923 */
1924 return 0;
1925}
1926
Vivien Didelotfad09c72016-06-21 12:28:20 -04001927static int _mv88e6xxx_port_vlan_add(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001928 u16 vid, bool untagged)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001929{
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001930 struct mv88e6xxx_vtu_stu_entry vlan;
1931 int err;
1932
Vivien Didelotfad09c72016-06-21 12:28:20 -04001933 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, true);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001934 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001935 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001936
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001937 vlan.data[port] = untagged ?
1938 GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
1939 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
1940
Vivien Didelotfad09c72016-06-21 12:28:20 -04001941 return _mv88e6xxx_vtu_loadpurge(chip, &vlan);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001942}
1943
Vivien Didelotf81ec902016-05-09 13:22:58 -04001944static void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
1945 const struct switchdev_obj_port_vlan *vlan,
1946 struct switchdev_trans *trans)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001947{
Vivien Didelot04bed142016-08-31 18:06:13 -04001948 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001949 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1950 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1951 u16 vid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001952
Vivien Didelotfad09c72016-06-21 12:28:20 -04001953 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001954 return;
1955
Vivien Didelotfad09c72016-06-21 12:28:20 -04001956 mutex_lock(&chip->reg_lock);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001957
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001958 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
Vivien Didelotfad09c72016-06-21 12:28:20 -04001959 if (_mv88e6xxx_port_vlan_add(chip, port, vid, untagged))
Andrew Lunnc8b09802016-06-04 21:16:57 +02001960 netdev_err(ds->ports[port].netdev,
1961 "failed to add VLAN %d%c\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001962 vid, untagged ? 'u' : 't');
Vivien Didelot76e398a2015-11-01 12:33:55 -05001963
Vivien Didelotfad09c72016-06-21 12:28:20 -04001964 if (pvid && _mv88e6xxx_port_pvid_set(chip, port, vlan->vid_end))
Andrew Lunnc8b09802016-06-04 21:16:57 +02001965 netdev_err(ds->ports[port].netdev, "failed to set PVID %d\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001966 vlan->vid_end);
1967
Vivien Didelotfad09c72016-06-21 12:28:20 -04001968 mutex_unlock(&chip->reg_lock);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001969}
1970
Vivien Didelotfad09c72016-06-21 12:28:20 -04001971static int _mv88e6xxx_port_vlan_del(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001972 int port, u16 vid)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001973{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001974 struct dsa_switch *ds = chip->ds;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001975 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001976 int i, err;
1977
Vivien Didelotfad09c72016-06-21 12:28:20 -04001978 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001979 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001980 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001981
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001982 /* Tell switchdev if this VLAN is handled in software */
1983 if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
Vivien Didelot3c06f082016-02-05 14:04:39 -05001984 return -EOPNOTSUPP;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001985
1986 vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
1987
1988 /* keep the VLAN unless all ports are excluded */
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001989 vlan.valid = false;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001990 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelot3d131f02015-11-03 10:52:52 -05001991 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001992 continue;
1993
1994 if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001995 vlan.valid = true;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001996 break;
1997 }
1998 }
1999
Vivien Didelotfad09c72016-06-21 12:28:20 -04002000 err = _mv88e6xxx_vtu_loadpurge(chip, &vlan);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002001 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002002 return err;
2003
Vivien Didelotfad09c72016-06-21 12:28:20 -04002004 return _mv88e6xxx_atu_remove(chip, vlan.fid, port, false);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002005}
2006
Vivien Didelotf81ec902016-05-09 13:22:58 -04002007static int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
2008 const struct switchdev_obj_port_vlan *vlan)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002009{
Vivien Didelot04bed142016-08-31 18:06:13 -04002010 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot76e398a2015-11-01 12:33:55 -05002011 u16 pvid, vid;
2012 int err = 0;
2013
Vivien Didelotfad09c72016-06-21 12:28:20 -04002014 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04002015 return -EOPNOTSUPP;
2016
Vivien Didelotfad09c72016-06-21 12:28:20 -04002017 mutex_lock(&chip->reg_lock);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002018
Vivien Didelotfad09c72016-06-21 12:28:20 -04002019 err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002020 if (err)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002021 goto unlock;
2022
Vivien Didelot76e398a2015-11-01 12:33:55 -05002023 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002024 err = _mv88e6xxx_port_vlan_del(chip, port, vid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002025 if (err)
2026 goto unlock;
2027
2028 if (vid == pvid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002029 err = _mv88e6xxx_port_pvid_set(chip, port, 0);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002030 if (err)
2031 goto unlock;
2032 }
2033 }
2034
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002035unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002036 mutex_unlock(&chip->reg_lock);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002037
2038 return err;
2039}
2040
Vivien Didelotfad09c72016-06-21 12:28:20 -04002041static int _mv88e6xxx_atu_mac_write(struct mv88e6xxx_chip *chip,
Vivien Didelotc5723ac2015-08-10 09:09:48 -04002042 const unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002043{
2044 int i, ret;
2045
2046 for (i = 0; i < 3; i++) {
Andrew Lunncca8b132015-04-02 04:06:39 +02002047 ret = _mv88e6xxx_reg_write(
Vivien Didelotfad09c72016-06-21 12:28:20 -04002048 chip, REG_GLOBAL, GLOBAL_ATU_MAC_01 + i,
Andrew Lunncca8b132015-04-02 04:06:39 +02002049 (addr[i * 2] << 8) | addr[i * 2 + 1]);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002050 if (ret < 0)
2051 return ret;
2052 }
2053
2054 return 0;
2055}
2056
Vivien Didelotfad09c72016-06-21 12:28:20 -04002057static int _mv88e6xxx_atu_mac_read(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04002058 unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002059{
2060 int i, ret;
2061
2062 for (i = 0; i < 3; i++) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002063 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
Andrew Lunncca8b132015-04-02 04:06:39 +02002064 GLOBAL_ATU_MAC_01 + i);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002065 if (ret < 0)
2066 return ret;
2067 addr[i * 2] = ret >> 8;
2068 addr[i * 2 + 1] = ret & 0xff;
2069 }
2070
2071 return 0;
2072}
2073
Vivien Didelotfad09c72016-06-21 12:28:20 -04002074static int _mv88e6xxx_atu_load(struct mv88e6xxx_chip *chip,
Vivien Didelotfd231c82015-08-10 09:09:50 -04002075 struct mv88e6xxx_atu_entry *entry)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002076{
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002077 int ret;
2078
Vivien Didelotfad09c72016-06-21 12:28:20 -04002079 ret = _mv88e6xxx_atu_wait(chip);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002080 if (ret < 0)
2081 return ret;
2082
Vivien Didelotfad09c72016-06-21 12:28:20 -04002083 ret = _mv88e6xxx_atu_mac_write(chip, entry->mac);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002084 if (ret < 0)
2085 return ret;
2086
Vivien Didelotfad09c72016-06-21 12:28:20 -04002087 ret = _mv88e6xxx_atu_data_write(chip, entry);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002088 if (ret < 0)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002089 return ret;
2090
Vivien Didelotfad09c72016-06-21 12:28:20 -04002091 return _mv88e6xxx_atu_cmd(chip, entry->fid, GLOBAL_ATU_OP_LOAD_DB);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002092}
David S. Millercdf09692015-08-11 12:00:37 -07002093
Vivien Didelot83dabd12016-08-31 11:50:04 -04002094static int mv88e6xxx_port_db_load_purge(struct mv88e6xxx_chip *chip, int port,
2095 const unsigned char *addr, u16 vid,
2096 u8 state)
Vivien Didelotfd231c82015-08-10 09:09:50 -04002097{
2098 struct mv88e6xxx_atu_entry entry = { 0 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002099 struct mv88e6xxx_vtu_stu_entry vlan;
2100 int err;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002101
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002102 /* Null VLAN ID corresponds to the port private database */
2103 if (vid == 0)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002104 err = _mv88e6xxx_port_fid_get(chip, port, &vlan.fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002105 else
Vivien Didelotfad09c72016-06-21 12:28:20 -04002106 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002107 if (err)
2108 return err;
2109
2110 entry.fid = vlan.fid;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002111 entry.state = state;
2112 ether_addr_copy(entry.mac, addr);
2113 if (state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2114 entry.trunk = false;
2115 entry.portv_trunkid = BIT(port);
2116 }
2117
Vivien Didelotfad09c72016-06-21 12:28:20 -04002118 return _mv88e6xxx_atu_load(chip, &entry);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002119}
2120
Vivien Didelotf81ec902016-05-09 13:22:58 -04002121static int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
2122 const struct switchdev_obj_port_fdb *fdb,
2123 struct switchdev_trans *trans)
Vivien Didelot146a3202015-10-08 11:35:12 -04002124{
2125 /* We don't need any dynamic resource from the kernel (yet),
2126 * so skip the prepare phase.
2127 */
2128 return 0;
2129}
2130
Vivien Didelotf81ec902016-05-09 13:22:58 -04002131static void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
2132 const struct switchdev_obj_port_fdb *fdb,
2133 struct switchdev_trans *trans)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002134{
Vivien Didelot04bed142016-08-31 18:06:13 -04002135 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot6630e232015-08-06 01:44:07 -04002136
Vivien Didelotfad09c72016-06-21 12:28:20 -04002137 mutex_lock(&chip->reg_lock);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002138 if (mv88e6xxx_port_db_load_purge(chip, port, fdb->addr, fdb->vid,
2139 GLOBAL_ATU_DATA_STATE_UC_STATIC))
2140 netdev_err(ds->ports[port].netdev, "failed to load unicast MAC address\n");
Vivien Didelotfad09c72016-06-21 12:28:20 -04002141 mutex_unlock(&chip->reg_lock);
David S. Millercdf09692015-08-11 12:00:37 -07002142}
2143
Vivien Didelotf81ec902016-05-09 13:22:58 -04002144static int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
2145 const struct switchdev_obj_port_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -07002146{
Vivien Didelot04bed142016-08-31 18:06:13 -04002147 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot83dabd12016-08-31 11:50:04 -04002148 int err;
David S. Millercdf09692015-08-11 12:00:37 -07002149
Vivien Didelotfad09c72016-06-21 12:28:20 -04002150 mutex_lock(&chip->reg_lock);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002151 err = mv88e6xxx_port_db_load_purge(chip, port, fdb->addr, fdb->vid,
2152 GLOBAL_ATU_DATA_STATE_UNUSED);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002153 mutex_unlock(&chip->reg_lock);
David S. Millercdf09692015-08-11 12:00:37 -07002154
Vivien Didelot83dabd12016-08-31 11:50:04 -04002155 return err;
David S. Millercdf09692015-08-11 12:00:37 -07002156}
2157
Vivien Didelotfad09c72016-06-21 12:28:20 -04002158static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
Vivien Didelot1d194042015-08-10 09:09:51 -04002159 struct mv88e6xxx_atu_entry *entry)
David S. Millercdf09692015-08-11 12:00:37 -07002160{
Vivien Didelot1d194042015-08-10 09:09:51 -04002161 struct mv88e6xxx_atu_entry next = { 0 };
2162 int ret;
2163
2164 next.fid = fid;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002165
Vivien Didelotfad09c72016-06-21 12:28:20 -04002166 ret = _mv88e6xxx_atu_wait(chip);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002167 if (ret < 0)
2168 return ret;
2169
Vivien Didelotfad09c72016-06-21 12:28:20 -04002170 ret = _mv88e6xxx_atu_cmd(chip, fid, GLOBAL_ATU_OP_GET_NEXT_DB);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002171 if (ret < 0)
2172 return ret;
2173
Vivien Didelotfad09c72016-06-21 12:28:20 -04002174 ret = _mv88e6xxx_atu_mac_read(chip, next.mac);
Vivien Didelot1d194042015-08-10 09:09:51 -04002175 if (ret < 0)
2176 return ret;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002177
Vivien Didelotfad09c72016-06-21 12:28:20 -04002178 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_ATU_DATA);
Vivien Didelot1d194042015-08-10 09:09:51 -04002179 if (ret < 0)
2180 return ret;
2181
2182 next.state = ret & GLOBAL_ATU_DATA_STATE_MASK;
2183 if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2184 unsigned int mask, shift;
2185
2186 if (ret & GLOBAL_ATU_DATA_TRUNK) {
2187 next.trunk = true;
2188 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
2189 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
2190 } else {
2191 next.trunk = false;
2192 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
2193 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
2194 }
2195
2196 next.portv_trunkid = (ret & mask) >> shift;
2197 }
2198
2199 *entry = next;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002200 return 0;
2201}
2202
Vivien Didelot83dabd12016-08-31 11:50:04 -04002203static int mv88e6xxx_port_db_dump_fid(struct mv88e6xxx_chip *chip,
2204 u16 fid, u16 vid, int port,
2205 struct switchdev_obj *obj,
2206 int (*cb)(struct switchdev_obj *obj))
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002207{
2208 struct mv88e6xxx_atu_entry addr = {
2209 .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2210 };
2211 int err;
2212
Vivien Didelotfad09c72016-06-21 12:28:20 -04002213 err = _mv88e6xxx_atu_mac_write(chip, addr.mac);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002214 if (err)
2215 return err;
2216
2217 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002218 err = _mv88e6xxx_atu_getnext(chip, fid, &addr);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002219 if (err)
Vivien Didelot83dabd12016-08-31 11:50:04 -04002220 return err;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002221
2222 if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2223 break;
2224
Vivien Didelot83dabd12016-08-31 11:50:04 -04002225 if (addr.trunk || (addr.portv_trunkid & BIT(port)) == 0)
2226 continue;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002227
Vivien Didelot83dabd12016-08-31 11:50:04 -04002228 if (obj->id == SWITCHDEV_OBJ_ID_PORT_FDB) {
2229 struct switchdev_obj_port_fdb *fdb;
2230
2231 if (!is_unicast_ether_addr(addr.mac))
2232 continue;
2233
2234 fdb = SWITCHDEV_OBJ_PORT_FDB(obj);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002235 fdb->vid = vid;
2236 ether_addr_copy(fdb->addr, addr.mac);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002237 if (addr.state == GLOBAL_ATU_DATA_STATE_UC_STATIC)
2238 fdb->ndm_state = NUD_NOARP;
2239 else
2240 fdb->ndm_state = NUD_REACHABLE;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04002241 } else if (obj->id == SWITCHDEV_OBJ_ID_PORT_MDB) {
2242 struct switchdev_obj_port_mdb *mdb;
2243
2244 if (!is_multicast_ether_addr(addr.mac))
2245 continue;
2246
2247 mdb = SWITCHDEV_OBJ_PORT_MDB(obj);
2248 mdb->vid = vid;
2249 ether_addr_copy(mdb->addr, addr.mac);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002250 } else {
2251 return -EOPNOTSUPP;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002252 }
Vivien Didelot83dabd12016-08-31 11:50:04 -04002253
2254 err = cb(obj);
2255 if (err)
2256 return err;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002257 } while (!is_broadcast_ether_addr(addr.mac));
2258
2259 return err;
2260}
2261
Vivien Didelot83dabd12016-08-31 11:50:04 -04002262static int mv88e6xxx_port_db_dump(struct mv88e6xxx_chip *chip, int port,
2263 struct switchdev_obj *obj,
2264 int (*cb)(struct switchdev_obj *obj))
2265{
2266 struct mv88e6xxx_vtu_stu_entry vlan = {
2267 .vid = GLOBAL_VTU_VID_MASK, /* all ones */
2268 };
2269 u16 fid;
2270 int err;
2271
2272 /* Dump port's default Filtering Information Database (VLAN ID 0) */
2273 err = _mv88e6xxx_port_fid_get(chip, port, &fid);
2274 if (err)
2275 return err;
2276
2277 err = mv88e6xxx_port_db_dump_fid(chip, fid, 0, port, obj, cb);
2278 if (err)
2279 return err;
2280
2281 /* Dump VLANs' Filtering Information Databases */
2282 err = _mv88e6xxx_vtu_vid_write(chip, vlan.vid);
2283 if (err)
2284 return err;
2285
2286 do {
2287 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
2288 if (err)
2289 return err;
2290
2291 if (!vlan.valid)
2292 break;
2293
2294 err = mv88e6xxx_port_db_dump_fid(chip, vlan.fid, vlan.vid, port,
2295 obj, cb);
2296 if (err)
2297 return err;
2298 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
2299
2300 return err;
2301}
2302
Vivien Didelotf81ec902016-05-09 13:22:58 -04002303static int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
2304 struct switchdev_obj_port_fdb *fdb,
2305 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotf33475b2015-10-22 09:34:41 -04002306{
Vivien Didelot04bed142016-08-31 18:06:13 -04002307 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002308 int err;
2309
Vivien Didelotfad09c72016-06-21 12:28:20 -04002310 mutex_lock(&chip->reg_lock);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002311 err = mv88e6xxx_port_db_dump(chip, port, &fdb->obj, cb);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002312 mutex_unlock(&chip->reg_lock);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002313
2314 return err;
2315}
2316
Vivien Didelotf81ec902016-05-09 13:22:58 -04002317static int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
2318 struct net_device *bridge)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002319{
Vivien Didelot04bed142016-08-31 18:06:13 -04002320 struct mv88e6xxx_chip *chip = ds->priv;
Colin Ian King1d9619d2016-04-25 23:11:22 +01002321 int i, err = 0;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002322
Vivien Didelotfad09c72016-06-21 12:28:20 -04002323 mutex_lock(&chip->reg_lock);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002324
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002325 /* Assign the bridge and remap each port's VLANTable */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002326 chip->ports[port].bridge_dev = bridge;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002327
Vivien Didelotfad09c72016-06-21 12:28:20 -04002328 for (i = 0; i < chip->info->num_ports; ++i) {
2329 if (chip->ports[i].bridge_dev == bridge) {
2330 err = _mv88e6xxx_port_based_vlan_map(chip, i);
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002331 if (err)
2332 break;
2333 }
2334 }
2335
Vivien Didelotfad09c72016-06-21 12:28:20 -04002336 mutex_unlock(&chip->reg_lock);
Vivien Didelota6692752016-02-12 12:09:39 -05002337
Vivien Didelot466dfa02016-02-26 13:16:05 -05002338 return err;
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002339}
2340
Vivien Didelotf81ec902016-05-09 13:22:58 -04002341static void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002342{
Vivien Didelot04bed142016-08-31 18:06:13 -04002343 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002344 struct net_device *bridge = chip->ports[port].bridge_dev;
Vivien Didelot16bfa702016-03-13 16:21:33 -04002345 int i;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002346
Vivien Didelotfad09c72016-06-21 12:28:20 -04002347 mutex_lock(&chip->reg_lock);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002348
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002349 /* Unassign the bridge and remap each port's VLANTable */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002350 chip->ports[port].bridge_dev = NULL;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002351
Vivien Didelotfad09c72016-06-21 12:28:20 -04002352 for (i = 0; i < chip->info->num_ports; ++i)
2353 if (i == port || chip->ports[i].bridge_dev == bridge)
2354 if (_mv88e6xxx_port_based_vlan_map(chip, i))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002355 netdev_warn(ds->ports[i].netdev,
2356 "failed to remap\n");
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002357
Vivien Didelotfad09c72016-06-21 12:28:20 -04002358 mutex_unlock(&chip->reg_lock);
Vivien Didelot66d9cd02016-02-05 14:07:14 -05002359}
2360
Vivien Didelotfad09c72016-06-21 12:28:20 -04002361static int mv88e6xxx_switch_reset(struct mv88e6xxx_chip *chip)
Vivien Didelot552238b2016-05-09 13:22:49 -04002362{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002363 bool ppu_active = mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE);
Vivien Didelot552238b2016-05-09 13:22:49 -04002364 u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002365 struct gpio_desc *gpiod = chip->reset;
Vivien Didelot552238b2016-05-09 13:22:49 -04002366 unsigned long timeout;
2367 int ret;
2368 int i;
2369
2370 /* Set all ports to the disabled state. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002371 for (i = 0; i < chip->info->num_ports; i++) {
2372 ret = _mv88e6xxx_reg_read(chip, REG_PORT(i), PORT_CONTROL);
Vivien Didelot552238b2016-05-09 13:22:49 -04002373 if (ret < 0)
2374 return ret;
2375
Vivien Didelotfad09c72016-06-21 12:28:20 -04002376 ret = _mv88e6xxx_reg_write(chip, REG_PORT(i), PORT_CONTROL,
Vivien Didelot552238b2016-05-09 13:22:49 -04002377 ret & 0xfffc);
2378 if (ret)
2379 return ret;
2380 }
2381
2382 /* Wait for transmit queues to drain. */
2383 usleep_range(2000, 4000);
2384
2385 /* If there is a gpio connected to the reset pin, toggle it */
2386 if (gpiod) {
2387 gpiod_set_value_cansleep(gpiod, 1);
2388 usleep_range(10000, 20000);
2389 gpiod_set_value_cansleep(gpiod, 0);
2390 usleep_range(10000, 20000);
2391 }
2392
2393 /* Reset the switch. Keep the PPU active if requested. The PPU
2394 * needs to be active to support indirect phy register access
2395 * through global registers 0x18 and 0x19.
2396 */
2397 if (ppu_active)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002398 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, 0x04, 0xc000);
Vivien Didelot552238b2016-05-09 13:22:49 -04002399 else
Vivien Didelotfad09c72016-06-21 12:28:20 -04002400 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, 0x04, 0xc400);
Vivien Didelot552238b2016-05-09 13:22:49 -04002401 if (ret)
2402 return ret;
2403
2404 /* Wait up to one second for reset to complete. */
2405 timeout = jiffies + 1 * HZ;
2406 while (time_before(jiffies, timeout)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002407 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, 0x00);
Vivien Didelot552238b2016-05-09 13:22:49 -04002408 if (ret < 0)
2409 return ret;
2410
2411 if ((ret & is_reset) == is_reset)
2412 break;
2413 usleep_range(1000, 2000);
2414 }
2415 if (time_after(jiffies, timeout))
2416 ret = -ETIMEDOUT;
2417 else
2418 ret = 0;
2419
2420 return ret;
2421}
2422
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002423static int mv88e6xxx_serdes_power_on(struct mv88e6xxx_chip *chip)
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002424{
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002425 u16 val;
2426 int err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002427
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002428 /* Clear Power Down bit */
2429 err = mv88e6xxx_serdes_read(chip, MII_BMCR, &val);
2430 if (err)
2431 return err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002432
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002433 if (val & BMCR_PDOWN) {
2434 val &= ~BMCR_PDOWN;
2435 err = mv88e6xxx_serdes_write(chip, MII_BMCR, val);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002436 }
2437
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002438 return err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002439}
2440
Vivien Didelot8f6345b2016-07-20 18:18:36 -04002441static int mv88e6xxx_port_read(struct mv88e6xxx_chip *chip, int port,
2442 int reg, u16 *val)
2443{
2444 int addr = chip->info->port_base_addr + port;
2445
2446 if (port >= chip->info->num_ports)
2447 return -EINVAL;
2448
2449 return mv88e6xxx_read(chip, addr, reg, val);
2450}
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;
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002455 int ret;
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 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002468 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_PCS_CTRL);
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
Vivien Didelotfad09c72016-06-21 12:28:20 -04002483 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002484 PORT_PCS_CTRL, reg);
2485 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002486 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002487 }
2488
2489 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
2490 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
2491 * tunneling, determine priority by looking at 802.1p and IP
2492 * priority fields (IP prio has precedence), and set STP state
2493 * to Forwarding.
2494 *
2495 * If this is the CPU link, use DSA or EDSA tagging depending
2496 * on which tagging mode was configured.
2497 *
2498 * If this is a link to another switch, use DSA tagging mode.
2499 *
2500 * If this is the upstream port for this switch, enable
2501 * forwarding of unknown unicasts and multicasts.
2502 */
2503 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002504 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2505 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2506 mv88e6xxx_6095_family(chip) || mv88e6xxx_6065_family(chip) ||
2507 mv88e6xxx_6185_family(chip) || mv88e6xxx_6320_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002508 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
2509 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
2510 PORT_CONTROL_STATE_FORWARDING;
2511 if (dsa_is_cpu_port(ds, port)) {
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002512 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA))
Andrew Lunn5377b802016-06-04 21:17:02 +02002513 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA |
Andrew Lunnc047a1f2015-09-29 01:50:56 +02002514 PORT_CONTROL_FORWARD_UNKNOWN_MC;
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002515 else
2516 reg |= PORT_CONTROL_DSA_TAG;
Jamie Lentinf027e0c2016-08-22 16:01:04 +02002517 reg |= PORT_CONTROL_EGRESS_ADD_TAG |
2518 PORT_CONTROL_FORWARD_UNKNOWN;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002519 }
Andrew Lunn6083ce72015-08-17 23:52:52 +02002520 if (dsa_is_dsa_port(ds, port)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002521 if (mv88e6xxx_6095_family(chip) ||
2522 mv88e6xxx_6185_family(chip))
Andrew Lunn6083ce72015-08-17 23:52:52 +02002523 reg |= PORT_CONTROL_DSA_TAG;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002524 if (mv88e6xxx_6352_family(chip) ||
2525 mv88e6xxx_6351_family(chip) ||
2526 mv88e6xxx_6165_family(chip) ||
2527 mv88e6xxx_6097_family(chip) ||
2528 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002529 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunn6083ce72015-08-17 23:52:52 +02002530 }
2531
Andrew Lunn54d792f2015-05-06 01:09:47 +02002532 if (port == dsa_upstream_port(ds))
2533 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2534 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2535 }
2536 if (reg) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002537 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002538 PORT_CONTROL, reg);
2539 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002540 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002541 }
2542
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002543 /* If this port is connected to a SerDes, make sure the SerDes is not
2544 * powered down.
2545 */
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002546 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_SERDES)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002547 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_STATUS);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002548 if (ret < 0)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002549 return ret;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002550 ret &= PORT_STATUS_CMODE_MASK;
2551 if ((ret == PORT_STATUS_CMODE_100BASE_X) ||
2552 (ret == PORT_STATUS_CMODE_1000BASE_X) ||
2553 (ret == PORT_STATUS_CMODE_SGMII)) {
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002554 ret = mv88e6xxx_serdes_power_on(chip);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002555 if (ret < 0)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002556 return ret;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002557 }
2558 }
2559
Vivien Didelot8efdda42015-08-13 12:52:23 -04002560 /* Port Control 2: don't force a good FCS, set the maximum frame size to
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002561 * 10240 bytes, disable 802.1q tags checking, don't discard tagged or
Vivien Didelot8efdda42015-08-13 12:52:23 -04002562 * untagged frames on this port, do a destination address lookup on all
2563 * received packets as usual, disable ARP mirroring and don't send a
2564 * copy of all transmitted/received frames on this port to the CPU.
Andrew Lunn54d792f2015-05-06 01:09:47 +02002565 */
2566 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002567 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2568 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2569 mv88e6xxx_6095_family(chip) || mv88e6xxx_6320_family(chip) ||
2570 mv88e6xxx_6185_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002571 reg = PORT_CONTROL_2_MAP_DA;
2572
Vivien Didelotfad09c72016-06-21 12:28:20 -04002573 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2574 mv88e6xxx_6165_family(chip) || mv88e6xxx_6320_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002575 reg |= PORT_CONTROL_2_JUMBO_10240;
2576
Vivien Didelotfad09c72016-06-21 12:28:20 -04002577 if (mv88e6xxx_6095_family(chip) || mv88e6xxx_6185_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002578 /* Set the upstream port this port should use */
2579 reg |= dsa_upstream_port(ds);
2580 /* enable forwarding of unknown multicast addresses to
2581 * the upstream port
2582 */
2583 if (port == dsa_upstream_port(ds))
2584 reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
2585 }
2586
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002587 reg |= PORT_CONTROL_2_8021Q_DISABLED;
Vivien Didelot8efdda42015-08-13 12:52:23 -04002588
Andrew Lunn54d792f2015-05-06 01:09:47 +02002589 if (reg) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002590 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002591 PORT_CONTROL_2, reg);
2592 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002593 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002594 }
2595
2596 /* Port Association Vector: when learning source addresses
2597 * of packets, add the address to the address database using
2598 * a port bitmap that has only the bit for this port set and
2599 * the other bits clear.
2600 */
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002601 reg = 1 << port;
Vivien Didelot996ecb82016-04-14 14:42:08 -04002602 /* Disable learning for CPU port */
2603 if (dsa_is_cpu_port(ds, port))
Vivien Didelot65fa4022016-04-14 14:42:07 -04002604 reg = 0;
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002605
Vivien Didelotfad09c72016-06-21 12:28:20 -04002606 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_ASSOC_VECTOR,
2607 reg);
Andrew Lunn54d792f2015-05-06 01:09:47 +02002608 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002609 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002610
2611 /* Egress rate control 2: disable egress rate control. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002612 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_RATE_CONTROL_2,
Andrew Lunn54d792f2015-05-06 01:09:47 +02002613 0x0000);
2614 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002615 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002616
Vivien Didelotfad09c72016-06-21 12:28:20 -04002617 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2618 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2619 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002620 /* Do not limit the period of time that this port can
2621 * be paused for by the remote end or the period of
2622 * time that this port can pause the remote end.
2623 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002624 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002625 PORT_PAUSE_CTRL, 0x0000);
2626 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002627 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002628
2629 /* Port ATU control: disable limiting the number of
2630 * address database entries that this port is allowed
2631 * to use.
2632 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002633 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002634 PORT_ATU_CONTROL, 0x0000);
2635 /* Priority Override: disable DA, SA and VTU priority
2636 * override.
2637 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002638 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002639 PORT_PRI_OVERRIDE, 0x0000);
2640 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002641 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002642
2643 /* Port Ethertype: use the Ethertype DSA Ethertype
2644 * value.
2645 */
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002646 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA)) {
2647 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2648 PORT_ETH_TYPE, ETH_P_EDSA);
2649 if (ret)
2650 return ret;
2651 }
2652
Andrew Lunn54d792f2015-05-06 01:09:47 +02002653 /* Tag Remap: use an identity 802.1p prio -> switch
2654 * prio mapping.
2655 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002656 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002657 PORT_TAG_REGMAP_0123, 0x3210);
2658 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002659 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002660
2661 /* Tag Remap 2: use an identity 802.1p prio -> switch
2662 * prio mapping.
2663 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002664 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002665 PORT_TAG_REGMAP_4567, 0x7654);
2666 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002667 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002668 }
2669
Jamie Lentin1bc261f2016-08-22 22:47:08 +01002670 /* Rate Control: disable ingress rate limiting. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002671 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2672 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
Vivien Didelotfad09c72016-06-21 12:28:20 -04002673 mv88e6xxx_6320_family(chip)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002674 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002675 PORT_RATE_CONTROL, 0x0001);
2676 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002677 return ret;
Jamie Lentin1bc261f2016-08-22 22:47:08 +01002678 } else if (mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip)) {
2679 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2680 PORT_RATE_CONTROL, 0x0000);
2681 if (ret)
2682 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002683 }
2684
Guenter Roeck366f0a02015-03-26 18:36:30 -07002685 /* Port Control 1: disable trunking, disable sending
2686 * learning messages to this port.
Guenter Roeckd827e882015-03-26 18:36:29 -07002687 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002688 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_1,
2689 0x0000);
Guenter Roeckd827e882015-03-26 18:36:29 -07002690 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002691 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07002692
Vivien Didelot207afda2016-04-14 14:42:09 -04002693 /* Port based VLAN map: give each port the same default address
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002694 * database, and allow bidirectional communication between the
2695 * CPU and DSA port(s), and the other ports.
Guenter Roeckd827e882015-03-26 18:36:29 -07002696 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002697 ret = _mv88e6xxx_port_fid_set(chip, port, 0);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002698 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002699 return ret;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002700
Vivien Didelotfad09c72016-06-21 12:28:20 -04002701 ret = _mv88e6xxx_port_based_vlan_map(chip, port);
Guenter Roeckd827e882015-03-26 18:36:29 -07002702 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002703 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07002704
2705 /* Default VLAN ID and priority: don't set a default VLAN
2706 * ID, and set the default packet priority to zero.
2707 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002708 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_DEFAULT_VLAN,
Vivien Didelot47cf1e652015-04-20 17:43:26 -04002709 0x0000);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002710 if (ret)
2711 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07002712
Andrew Lunndbde9e62015-05-06 01:09:48 +02002713 return 0;
2714}
2715
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002716static int mv88e6xxx_g1_set_switch_mac(struct mv88e6xxx_chip *chip, u8 *addr)
2717{
2718 int err;
2719
2720 err = mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_MAC_01,
2721 (addr[0] << 8) | addr[1]);
2722 if (err)
2723 return err;
2724
2725 err = mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_MAC_23,
2726 (addr[2] << 8) | addr[3]);
2727 if (err)
2728 return err;
2729
2730 return mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_MAC_45,
2731 (addr[4] << 8) | addr[5]);
2732}
2733
Vivien Didelotacddbd22016-07-18 20:45:39 -04002734static int mv88e6xxx_g1_set_age_time(struct mv88e6xxx_chip *chip,
2735 unsigned int msecs)
2736{
2737 const unsigned int coeff = chip->info->age_time_coeff;
2738 const unsigned int min = 0x01 * coeff;
2739 const unsigned int max = 0xff * coeff;
2740 u8 age_time;
2741 u16 val;
2742 int err;
2743
2744 if (msecs < min || msecs > max)
2745 return -ERANGE;
2746
2747 /* Round to nearest multiple of coeff */
2748 age_time = (msecs + coeff / 2) / coeff;
2749
2750 err = mv88e6xxx_read(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL, &val);
2751 if (err)
2752 return err;
2753
2754 /* AgeTime is 11:4 bits */
2755 val &= ~0xff0;
2756 val |= age_time << 4;
2757
2758 return mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL, val);
2759}
2760
Vivien Didelot2cfcd962016-07-18 20:45:40 -04002761static int mv88e6xxx_set_ageing_time(struct dsa_switch *ds,
2762 unsigned int ageing_time)
2763{
Vivien Didelot04bed142016-08-31 18:06:13 -04002764 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot2cfcd962016-07-18 20:45:40 -04002765 int err;
2766
2767 mutex_lock(&chip->reg_lock);
2768 err = mv88e6xxx_g1_set_age_time(chip, ageing_time);
2769 mutex_unlock(&chip->reg_lock);
2770
2771 return err;
2772}
2773
Vivien Didelot97299342016-07-18 20:45:30 -04002774static int mv88e6xxx_g1_setup(struct mv88e6xxx_chip *chip)
Vivien Didelot08a01262016-05-09 13:22:50 -04002775{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002776 struct dsa_switch *ds = chip->ds;
Vivien Didelotb0745e872016-05-09 13:22:53 -04002777 u32 upstream_port = dsa_upstream_port(ds);
Vivien Didelot119477b2016-05-09 13:22:51 -04002778 u16 reg;
Vivien Didelot08a01262016-05-09 13:22:50 -04002779 int err;
Vivien Didelot08a01262016-05-09 13:22:50 -04002780
Vivien Didelot119477b2016-05-09 13:22:51 -04002781 /* Enable the PHY Polling Unit if present, don't discard any packets,
2782 * and mask all interrupt sources.
2783 */
2784 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002785 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU) ||
2786 mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE))
Vivien Didelot119477b2016-05-09 13:22:51 -04002787 reg |= GLOBAL_CONTROL_PPU_ENABLE;
2788
Vivien Didelotfad09c72016-06-21 12:28:20 -04002789 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL, reg);
Vivien Didelot119477b2016-05-09 13:22:51 -04002790 if (err)
2791 return err;
2792
Vivien Didelotb0745e872016-05-09 13:22:53 -04002793 /* Configure the upstream port, and configure it as the port to which
2794 * ingress and egress and ARP monitor frames are to be sent.
2795 */
2796 reg = upstream_port << GLOBAL_MONITOR_CONTROL_INGRESS_SHIFT |
2797 upstream_port << GLOBAL_MONITOR_CONTROL_EGRESS_SHIFT |
2798 upstream_port << GLOBAL_MONITOR_CONTROL_ARP_SHIFT;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002799 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_MONITOR_CONTROL,
2800 reg);
Vivien Didelotb0745e872016-05-09 13:22:53 -04002801 if (err)
2802 return err;
2803
Vivien Didelot50484ff2016-05-09 13:22:54 -04002804 /* Disable remote management, and set the switch's DSA device number. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002805 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL_2,
Vivien Didelot50484ff2016-05-09 13:22:54 -04002806 GLOBAL_CONTROL_2_MULTIPLE_CASCADE |
2807 (ds->index & 0x1f));
2808 if (err)
2809 return err;
2810
Vivien Didelotacddbd22016-07-18 20:45:39 -04002811 /* Clear all the VTU and STU entries */
2812 err = _mv88e6xxx_vtu_stu_flush(chip);
2813 if (err < 0)
2814 return err;
2815
Vivien Didelot08a01262016-05-09 13:22:50 -04002816 /* Set the default address aging time to 5 minutes, and
2817 * enable address learn messages to be sent to all message
2818 * ports.
2819 */
Vivien Didelotacddbd22016-07-18 20:45:39 -04002820 err = mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL,
2821 GLOBAL_ATU_CONTROL_LEARN2ALL);
Vivien Didelot08a01262016-05-09 13:22:50 -04002822 if (err)
2823 return err;
2824
Vivien Didelotacddbd22016-07-18 20:45:39 -04002825 err = mv88e6xxx_g1_set_age_time(chip, 300000);
2826 if (err)
Vivien Didelot97299342016-07-18 20:45:30 -04002827 return err;
2828
2829 /* Clear all ATU entries */
2830 err = _mv88e6xxx_atu_flush(chip, 0, true);
2831 if (err)
2832 return err;
2833
Vivien Didelot08a01262016-05-09 13:22:50 -04002834 /* Configure the IP ToS mapping registers. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002835 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_0, 0x0000);
Vivien Didelot08a01262016-05-09 13:22:50 -04002836 if (err)
2837 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002838 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_1, 0x0000);
Vivien Didelot08a01262016-05-09 13:22:50 -04002839 if (err)
2840 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002841 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_2, 0x5555);
Vivien Didelot08a01262016-05-09 13:22:50 -04002842 if (err)
2843 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002844 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_3, 0x5555);
Vivien Didelot08a01262016-05-09 13:22:50 -04002845 if (err)
2846 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002847 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_4, 0xaaaa);
Vivien Didelot08a01262016-05-09 13:22:50 -04002848 if (err)
2849 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002850 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_5, 0xaaaa);
Vivien Didelot08a01262016-05-09 13:22:50 -04002851 if (err)
2852 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002853 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_6, 0xffff);
Vivien Didelot08a01262016-05-09 13:22:50 -04002854 if (err)
2855 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002856 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_7, 0xffff);
Vivien Didelot08a01262016-05-09 13:22:50 -04002857 if (err)
2858 return err;
2859
2860 /* Configure the IEEE 802.1p priority mapping register. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002861 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IEEE_PRI, 0xfa41);
Vivien Didelot08a01262016-05-09 13:22:50 -04002862 if (err)
2863 return err;
2864
Vivien Didelot97299342016-07-18 20:45:30 -04002865 /* Clear the statistics counters for all ports */
2866 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
2867 GLOBAL_STATS_OP_FLUSH_ALL);
2868 if (err)
2869 return err;
2870
2871 /* Wait for the flush to complete. */
2872 err = _mv88e6xxx_stats_wait(chip);
2873 if (err)
2874 return err;
2875
2876 return 0;
2877}
2878
Vivien Didelotf81ec902016-05-09 13:22:58 -04002879static int mv88e6xxx_setup(struct dsa_switch *ds)
Guenter Roeckacdaffc2015-03-26 18:36:28 -07002880{
Vivien Didelot04bed142016-08-31 18:06:13 -04002881 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot552238b2016-05-09 13:22:49 -04002882 int err;
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002883 int i;
2884
Vivien Didelotfad09c72016-06-21 12:28:20 -04002885 chip->ds = ds;
2886 ds->slave_mii_bus = chip->mdio_bus;
Vivien Didelot552238b2016-05-09 13:22:49 -04002887
Vivien Didelotfad09c72016-06-21 12:28:20 -04002888 mutex_lock(&chip->reg_lock);
Vivien Didelot552238b2016-05-09 13:22:49 -04002889
Vivien Didelotfad09c72016-06-21 12:28:20 -04002890 err = mv88e6xxx_switch_reset(chip);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002891 if (err)
2892 goto unlock;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002893
Vivien Didelot97299342016-07-18 20:45:30 -04002894 /* Setup Switch Port Registers */
2895 for (i = 0; i < chip->info->num_ports; i++) {
2896 err = mv88e6xxx_setup_port(chip, i);
2897 if (err)
2898 goto unlock;
2899 }
2900
2901 /* Setup Switch Global 1 Registers */
2902 err = mv88e6xxx_g1_setup(chip);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002903 if (err)
2904 goto unlock;
2905
Vivien Didelot97299342016-07-18 20:45:30 -04002906 /* Setup Switch Global 2 Registers */
2907 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_GLOBAL2)) {
2908 err = mv88e6xxx_g2_setup(chip);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002909 if (err)
2910 goto unlock;
2911 }
Andrew Lunn54d792f2015-05-06 01:09:47 +02002912
Vivien Didelot6b17e862015-08-13 12:52:18 -04002913unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002914 mutex_unlock(&chip->reg_lock);
Andrew Lunndb687a52015-06-20 21:31:29 +02002915
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002916 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002917}
2918
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002919static int mv88e6xxx_set_addr(struct dsa_switch *ds, u8 *addr)
2920{
Vivien Didelot04bed142016-08-31 18:06:13 -04002921 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002922 int err;
2923
2924 mutex_lock(&chip->reg_lock);
2925
2926 /* Has an indirect Switch MAC/WoL/WoF register in Global 2? */
2927 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_SWITCH_MAC))
2928 err = mv88e6xxx_g2_set_switch_mac(chip, addr);
2929 else
2930 err = mv88e6xxx_g1_set_switch_mac(chip, addr);
2931
2932 mutex_unlock(&chip->reg_lock);
2933
2934 return err;
2935}
2936
Vivien Didelote57e5e72016-08-15 17:19:00 -04002937static int mv88e6xxx_mdio_read(struct mii_bus *bus, int phy, int reg)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002938{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002939 struct mv88e6xxx_chip *chip = bus->priv;
Vivien Didelote57e5e72016-08-15 17:19:00 -04002940 u16 val;
2941 int err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002942
Vivien Didelote57e5e72016-08-15 17:19:00 -04002943 if (phy >= chip->info->num_ports)
Andrew Lunn158bc062016-04-28 21:24:06 -04002944 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002945
Vivien Didelotfad09c72016-06-21 12:28:20 -04002946 mutex_lock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04002947 err = mv88e6xxx_phy_read(chip, phy, reg, &val);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002948 mutex_unlock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04002949
2950 return err ? err : val;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002951}
2952
Vivien Didelote57e5e72016-08-15 17:19:00 -04002953static int mv88e6xxx_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002954{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002955 struct mv88e6xxx_chip *chip = bus->priv;
Vivien Didelote57e5e72016-08-15 17:19:00 -04002956 int err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002957
Vivien Didelote57e5e72016-08-15 17:19:00 -04002958 if (phy >= chip->info->num_ports)
Andrew Lunn158bc062016-04-28 21:24:06 -04002959 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002960
Vivien Didelotfad09c72016-06-21 12:28:20 -04002961 mutex_lock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04002962 err = mv88e6xxx_phy_write(chip, phy, reg, val);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002963 mutex_unlock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04002964
2965 return err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002966}
2967
Vivien Didelotfad09c72016-06-21 12:28:20 -04002968static int mv88e6xxx_mdio_register(struct mv88e6xxx_chip *chip,
Andrew Lunnb516d452016-06-04 21:17:06 +02002969 struct device_node *np)
2970{
2971 static int index;
2972 struct mii_bus *bus;
2973 int err;
2974
Andrew Lunnb516d452016-06-04 21:17:06 +02002975 if (np)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002976 chip->mdio_np = of_get_child_by_name(np, "mdio");
Andrew Lunnb516d452016-06-04 21:17:06 +02002977
Vivien Didelotfad09c72016-06-21 12:28:20 -04002978 bus = devm_mdiobus_alloc(chip->dev);
Andrew Lunnb516d452016-06-04 21:17:06 +02002979 if (!bus)
2980 return -ENOMEM;
2981
Vivien Didelotfad09c72016-06-21 12:28:20 -04002982 bus->priv = (void *)chip;
Andrew Lunnb516d452016-06-04 21:17:06 +02002983 if (np) {
2984 bus->name = np->full_name;
2985 snprintf(bus->id, MII_BUS_ID_SIZE, "%s", np->full_name);
2986 } else {
2987 bus->name = "mv88e6xxx SMI";
2988 snprintf(bus->id, MII_BUS_ID_SIZE, "mv88e6xxx-%d", index++);
2989 }
2990
2991 bus->read = mv88e6xxx_mdio_read;
2992 bus->write = mv88e6xxx_mdio_write;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002993 bus->parent = chip->dev;
Andrew Lunnb516d452016-06-04 21:17:06 +02002994
Vivien Didelotfad09c72016-06-21 12:28:20 -04002995 if (chip->mdio_np)
2996 err = of_mdiobus_register(bus, chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02002997 else
2998 err = mdiobus_register(bus);
2999 if (err) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04003000 dev_err(chip->dev, "Cannot register MDIO bus (%d)\n", err);
Andrew Lunnb516d452016-06-04 21:17:06 +02003001 goto out;
3002 }
Vivien Didelotfad09c72016-06-21 12:28:20 -04003003 chip->mdio_bus = bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003004
3005 return 0;
3006
3007out:
Vivien Didelotfad09c72016-06-21 12:28:20 -04003008 if (chip->mdio_np)
3009 of_node_put(chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003010
3011 return err;
3012}
3013
Vivien Didelotfad09c72016-06-21 12:28:20 -04003014static void mv88e6xxx_mdio_unregister(struct mv88e6xxx_chip *chip)
Andrew Lunnb516d452016-06-04 21:17:06 +02003015
3016{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003017 struct mii_bus *bus = chip->mdio_bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003018
3019 mdiobus_unregister(bus);
3020
Vivien Didelotfad09c72016-06-21 12:28:20 -04003021 if (chip->mdio_np)
3022 of_node_put(chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003023}
3024
Guenter Roeckc22995c2015-07-25 09:42:28 -07003025#ifdef CONFIG_NET_DSA_HWMON
3026
3027static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
3028{
Vivien Didelot04bed142016-08-31 18:06:13 -04003029 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot9c938292016-08-15 17:19:02 -04003030 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003031 int ret;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003032
3033 *temp = 0;
3034
Vivien Didelotfad09c72016-06-21 12:28:20 -04003035 mutex_lock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003036
Vivien Didelot9c938292016-08-15 17:19:02 -04003037 ret = mv88e6xxx_phy_write(chip, 0x0, 0x16, 0x6);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003038 if (ret < 0)
3039 goto error;
3040
3041 /* Enable temperature sensor */
Vivien Didelot9c938292016-08-15 17:19:02 -04003042 ret = mv88e6xxx_phy_read(chip, 0x0, 0x1a, &val);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003043 if (ret < 0)
3044 goto error;
3045
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 /* Wait for temperature to stabilize */
3051 usleep_range(10000, 12000);
3052
Vivien Didelot9c938292016-08-15 17:19:02 -04003053 ret = mv88e6xxx_phy_read(chip, 0x0, 0x1a, &val);
3054 if (ret < 0)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003055 goto error;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003056
3057 /* Disable temperature sensor */
Vivien Didelot9c938292016-08-15 17:19:02 -04003058 ret = mv88e6xxx_phy_write(chip, 0x0, 0x1a, val & ~(1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003059 if (ret < 0)
3060 goto error;
3061
3062 *temp = ((val & 0x1f) - 5) * 5;
3063
3064error:
Vivien Didelot9c938292016-08-15 17:19:02 -04003065 mv88e6xxx_phy_write(chip, 0x0, 0x16, 0x0);
Vivien Didelotfad09c72016-06-21 12:28:20 -04003066 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003067 return ret;
3068}
3069
3070static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
3071{
Vivien Didelot04bed142016-08-31 18:06:13 -04003072 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003073 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003074 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003075 int ret;
3076
3077 *temp = 0;
3078
Vivien Didelot9c938292016-08-15 17:19:02 -04003079 mutex_lock(&chip->reg_lock);
3080 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 27, &val);
3081 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003082 if (ret < 0)
3083 return ret;
3084
Vivien Didelot9c938292016-08-15 17:19:02 -04003085 *temp = (val & 0xff) - 25;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003086
3087 return 0;
3088}
3089
Vivien Didelotf81ec902016-05-09 13:22:58 -04003090static int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003091{
Vivien Didelot04bed142016-08-31 18:06:13 -04003092 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn158bc062016-04-28 21:24:06 -04003093
Vivien Didelotfad09c72016-06-21 12:28:20 -04003094 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP))
Vivien Didelot6594f612016-05-09 13:22:42 -04003095 return -EOPNOTSUPP;
3096
Vivien Didelotfad09c72016-06-21 12:28:20 -04003097 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003098 return mv88e63xx_get_temp(ds, temp);
3099
3100 return mv88e61xx_get_temp(ds, temp);
3101}
3102
Vivien Didelotf81ec902016-05-09 13:22:58 -04003103static int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003104{
Vivien Didelot04bed142016-08-31 18:06:13 -04003105 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003106 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003107 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003108 int ret;
3109
Vivien Didelotfad09c72016-06-21 12:28:20 -04003110 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003111 return -EOPNOTSUPP;
3112
3113 *temp = 0;
3114
Vivien Didelot9c938292016-08-15 17:19:02 -04003115 mutex_lock(&chip->reg_lock);
3116 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3117 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003118 if (ret < 0)
3119 return ret;
3120
Vivien Didelot9c938292016-08-15 17:19:02 -04003121 *temp = (((val >> 8) & 0x1f) * 5) - 25;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003122
3123 return 0;
3124}
3125
Vivien Didelotf81ec902016-05-09 13:22:58 -04003126static int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003127{
Vivien Didelot04bed142016-08-31 18:06:13 -04003128 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003129 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003130 u16 val;
3131 int err;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003132
Vivien Didelotfad09c72016-06-21 12:28:20 -04003133 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003134 return -EOPNOTSUPP;
3135
Vivien Didelot9c938292016-08-15 17:19:02 -04003136 mutex_lock(&chip->reg_lock);
3137 err = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3138 if (err)
3139 goto unlock;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003140 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
Vivien Didelot9c938292016-08-15 17:19:02 -04003141 err = mv88e6xxx_phy_page_write(chip, phy, 6, 26,
3142 (val & 0xe0ff) | (temp << 8));
3143unlock:
3144 mutex_unlock(&chip->reg_lock);
3145
3146 return err;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003147}
3148
Vivien Didelotf81ec902016-05-09 13:22:58 -04003149static int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003150{
Vivien Didelot04bed142016-08-31 18:06:13 -04003151 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003152 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003153 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003154 int ret;
3155
Vivien Didelotfad09c72016-06-21 12:28:20 -04003156 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003157 return -EOPNOTSUPP;
3158
3159 *alarm = false;
3160
Vivien Didelot9c938292016-08-15 17:19:02 -04003161 mutex_lock(&chip->reg_lock);
3162 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3163 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003164 if (ret < 0)
3165 return ret;
3166
Vivien Didelot9c938292016-08-15 17:19:02 -04003167 *alarm = !!(val & 0x40);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003168
3169 return 0;
3170}
3171#endif /* CONFIG_NET_DSA_HWMON */
3172
Vivien Didelot855b1932016-07-20 18:18:35 -04003173static int mv88e6xxx_get_eeprom_len(struct dsa_switch *ds)
3174{
Vivien Didelot04bed142016-08-31 18:06:13 -04003175 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot855b1932016-07-20 18:18:35 -04003176
3177 return chip->eeprom_len;
3178}
3179
Vivien Didelot855b1932016-07-20 18:18:35 -04003180static int mv88e6xxx_get_eeprom(struct dsa_switch *ds,
3181 struct ethtool_eeprom *eeprom, u8 *data)
3182{
Vivien Didelot04bed142016-08-31 18:06:13 -04003183 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot855b1932016-07-20 18:18:35 -04003184 int err;
3185
3186 mutex_lock(&chip->reg_lock);
3187
3188 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16))
Vivien Didelotec561272016-09-02 14:45:33 -04003189 err = mv88e6xxx_g2_get_eeprom16(chip, eeprom, data);
Vivien Didelot855b1932016-07-20 18:18:35 -04003190 else
3191 err = -EOPNOTSUPP;
3192
3193 mutex_unlock(&chip->reg_lock);
3194
3195 if (err)
3196 return err;
3197
3198 eeprom->magic = 0xc3ec4951;
3199
3200 return 0;
3201}
3202
Vivien Didelot855b1932016-07-20 18:18:35 -04003203static int mv88e6xxx_set_eeprom(struct dsa_switch *ds,
3204 struct ethtool_eeprom *eeprom, u8 *data)
3205{
Vivien Didelot04bed142016-08-31 18:06:13 -04003206 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot855b1932016-07-20 18:18:35 -04003207 int err;
3208
3209 if (eeprom->magic != 0xc3ec4951)
3210 return -EINVAL;
3211
3212 mutex_lock(&chip->reg_lock);
3213
3214 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16))
Vivien Didelotec561272016-09-02 14:45:33 -04003215 err = mv88e6xxx_g2_set_eeprom16(chip, eeprom, data);
Vivien Didelot855b1932016-07-20 18:18:35 -04003216 else
3217 err = -EOPNOTSUPP;
3218
3219 mutex_unlock(&chip->reg_lock);
3220
3221 return err;
3222}
3223
Vivien Didelotf81ec902016-05-09 13:22:58 -04003224static const struct mv88e6xxx_info mv88e6xxx_table[] = {
3225 [MV88E6085] = {
3226 .prod_num = PORT_SWITCH_ID_PROD_NUM_6085,
3227 .family = MV88E6XXX_FAMILY_6097,
3228 .name = "Marvell 88E6085",
3229 .num_databases = 4096,
3230 .num_ports = 10,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003231 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003232 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003233 .flags = MV88E6XXX_FLAGS_FAMILY_6097,
3234 },
3235
3236 [MV88E6095] = {
3237 .prod_num = PORT_SWITCH_ID_PROD_NUM_6095,
3238 .family = MV88E6XXX_FAMILY_6095,
3239 .name = "Marvell 88E6095/88E6095F",
3240 .num_databases = 256,
3241 .num_ports = 11,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003242 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003243 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003244 .flags = MV88E6XXX_FLAGS_FAMILY_6095,
3245 },
3246
3247 [MV88E6123] = {
3248 .prod_num = PORT_SWITCH_ID_PROD_NUM_6123,
3249 .family = MV88E6XXX_FAMILY_6165,
3250 .name = "Marvell 88E6123",
3251 .num_databases = 4096,
3252 .num_ports = 3,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003253 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003254 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003255 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3256 },
3257
3258 [MV88E6131] = {
3259 .prod_num = PORT_SWITCH_ID_PROD_NUM_6131,
3260 .family = MV88E6XXX_FAMILY_6185,
3261 .name = "Marvell 88E6131",
3262 .num_databases = 256,
3263 .num_ports = 8,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003264 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003265 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003266 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3267 },
3268
3269 [MV88E6161] = {
3270 .prod_num = PORT_SWITCH_ID_PROD_NUM_6161,
3271 .family = MV88E6XXX_FAMILY_6165,
3272 .name = "Marvell 88E6161",
3273 .num_databases = 4096,
3274 .num_ports = 6,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003275 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003276 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003277 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3278 },
3279
3280 [MV88E6165] = {
3281 .prod_num = PORT_SWITCH_ID_PROD_NUM_6165,
3282 .family = MV88E6XXX_FAMILY_6165,
3283 .name = "Marvell 88E6165",
3284 .num_databases = 4096,
3285 .num_ports = 6,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003286 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003287 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003288 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3289 },
3290
3291 [MV88E6171] = {
3292 .prod_num = PORT_SWITCH_ID_PROD_NUM_6171,
3293 .family = MV88E6XXX_FAMILY_6351,
3294 .name = "Marvell 88E6171",
3295 .num_databases = 4096,
3296 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003297 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003298 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003299 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3300 },
3301
3302 [MV88E6172] = {
3303 .prod_num = PORT_SWITCH_ID_PROD_NUM_6172,
3304 .family = MV88E6XXX_FAMILY_6352,
3305 .name = "Marvell 88E6172",
3306 .num_databases = 4096,
3307 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003308 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003309 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003310 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3311 },
3312
3313 [MV88E6175] = {
3314 .prod_num = PORT_SWITCH_ID_PROD_NUM_6175,
3315 .family = MV88E6XXX_FAMILY_6351,
3316 .name = "Marvell 88E6175",
3317 .num_databases = 4096,
3318 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003319 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003320 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003321 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3322 },
3323
3324 [MV88E6176] = {
3325 .prod_num = PORT_SWITCH_ID_PROD_NUM_6176,
3326 .family = MV88E6XXX_FAMILY_6352,
3327 .name = "Marvell 88E6176",
3328 .num_databases = 4096,
3329 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003330 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003331 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003332 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3333 },
3334
3335 [MV88E6185] = {
3336 .prod_num = PORT_SWITCH_ID_PROD_NUM_6185,
3337 .family = MV88E6XXX_FAMILY_6185,
3338 .name = "Marvell 88E6185",
3339 .num_databases = 256,
3340 .num_ports = 10,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003341 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003342 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003343 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3344 },
3345
3346 [MV88E6240] = {
3347 .prod_num = PORT_SWITCH_ID_PROD_NUM_6240,
3348 .family = MV88E6XXX_FAMILY_6352,
3349 .name = "Marvell 88E6240",
3350 .num_databases = 4096,
3351 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003352 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003353 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003354 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3355 },
3356
3357 [MV88E6320] = {
3358 .prod_num = PORT_SWITCH_ID_PROD_NUM_6320,
3359 .family = MV88E6XXX_FAMILY_6320,
3360 .name = "Marvell 88E6320",
3361 .num_databases = 4096,
3362 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003363 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003364 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003365 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3366 },
3367
3368 [MV88E6321] = {
3369 .prod_num = PORT_SWITCH_ID_PROD_NUM_6321,
3370 .family = MV88E6XXX_FAMILY_6320,
3371 .name = "Marvell 88E6321",
3372 .num_databases = 4096,
3373 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003374 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003375 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003376 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3377 },
3378
3379 [MV88E6350] = {
3380 .prod_num = PORT_SWITCH_ID_PROD_NUM_6350,
3381 .family = MV88E6XXX_FAMILY_6351,
3382 .name = "Marvell 88E6350",
3383 .num_databases = 4096,
3384 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003385 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003386 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003387 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3388 },
3389
3390 [MV88E6351] = {
3391 .prod_num = PORT_SWITCH_ID_PROD_NUM_6351,
3392 .family = MV88E6XXX_FAMILY_6351,
3393 .name = "Marvell 88E6351",
3394 .num_databases = 4096,
3395 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003396 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003397 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003398 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3399 },
3400
3401 [MV88E6352] = {
3402 .prod_num = PORT_SWITCH_ID_PROD_NUM_6352,
3403 .family = MV88E6XXX_FAMILY_6352,
3404 .name = "Marvell 88E6352",
3405 .num_databases = 4096,
3406 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003407 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003408 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003409 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3410 },
3411};
3412
Vivien Didelot5f7c0362016-06-20 13:14:04 -04003413static const struct mv88e6xxx_info *mv88e6xxx_lookup_info(unsigned int prod_num)
Vivien Didelotb9b37712015-10-30 19:39:48 -04003414{
Vivien Didelota439c062016-04-17 13:23:58 -04003415 int i;
Vivien Didelotb9b37712015-10-30 19:39:48 -04003416
Vivien Didelot5f7c0362016-06-20 13:14:04 -04003417 for (i = 0; i < ARRAY_SIZE(mv88e6xxx_table); ++i)
3418 if (mv88e6xxx_table[i].prod_num == prod_num)
3419 return &mv88e6xxx_table[i];
Vivien Didelotb9b37712015-10-30 19:39:48 -04003420
Vivien Didelotb9b37712015-10-30 19:39:48 -04003421 return NULL;
3422}
3423
Vivien Didelotfad09c72016-06-21 12:28:20 -04003424static int mv88e6xxx_detect(struct mv88e6xxx_chip *chip)
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003425{
3426 const struct mv88e6xxx_info *info;
Vivien Didelot8f6345b2016-07-20 18:18:36 -04003427 unsigned int prod_num, rev;
3428 u16 id;
3429 int err;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003430
Vivien Didelot8f6345b2016-07-20 18:18:36 -04003431 mutex_lock(&chip->reg_lock);
3432 err = mv88e6xxx_port_read(chip, 0, PORT_SWITCH_ID, &id);
3433 mutex_unlock(&chip->reg_lock);
3434 if (err)
3435 return err;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003436
3437 prod_num = (id & 0xfff0) >> 4;
3438 rev = id & 0x000f;
3439
3440 info = mv88e6xxx_lookup_info(prod_num);
3441 if (!info)
3442 return -ENODEV;
3443
Vivien Didelotcaac8542016-06-20 13:14:09 -04003444 /* Update the compatible info with the probed one */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003445 chip->info = info;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003446
Vivien Didelotfad09c72016-06-21 12:28:20 -04003447 dev_info(chip->dev, "switch 0x%x detected: %s, revision %u\n",
3448 chip->info->prod_num, chip->info->name, rev);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003449
3450 return 0;
3451}
3452
Vivien Didelotfad09c72016-06-21 12:28:20 -04003453static struct mv88e6xxx_chip *mv88e6xxx_alloc_chip(struct device *dev)
Vivien Didelot469d7292016-06-20 13:14:06 -04003454{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003455 struct mv88e6xxx_chip *chip;
Vivien Didelot469d7292016-06-20 13:14:06 -04003456
Vivien Didelotfad09c72016-06-21 12:28:20 -04003457 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
3458 if (!chip)
Vivien Didelot469d7292016-06-20 13:14:06 -04003459 return NULL;
3460
Vivien Didelotfad09c72016-06-21 12:28:20 -04003461 chip->dev = dev;
Vivien Didelot469d7292016-06-20 13:14:06 -04003462
Vivien Didelotfad09c72016-06-21 12:28:20 -04003463 mutex_init(&chip->reg_lock);
Vivien Didelot469d7292016-06-20 13:14:06 -04003464
Vivien Didelotfad09c72016-06-21 12:28:20 -04003465 return chip;
Vivien Didelot469d7292016-06-20 13:14:06 -04003466}
3467
Vivien Didelotec561272016-09-02 14:45:33 -04003468static const struct mv88e6xxx_ops mv88e6xxx_g2_smi_phy_ops = {
3469 .read = mv88e6xxx_g2_smi_phy_read,
3470 .write = mv88e6xxx_g2_smi_phy_write,
3471};
3472
Vivien Didelote57e5e72016-08-15 17:19:00 -04003473static const struct mv88e6xxx_ops mv88e6xxx_phy_ops = {
3474 .read = mv88e6xxx_read,
3475 .write = mv88e6xxx_write,
3476};
3477
3478static void mv88e6xxx_phy_init(struct mv88e6xxx_chip *chip)
3479{
3480 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_SMI_PHY)) {
3481 chip->phy_ops = &mv88e6xxx_g2_smi_phy_ops;
3482 } else if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU)) {
3483 chip->phy_ops = &mv88e6xxx_phy_ppu_ops;
3484 mv88e6xxx_ppu_state_init(chip);
3485 } else {
3486 chip->phy_ops = &mv88e6xxx_phy_ops;
3487 }
3488}
3489
Andrew Lunn930188c2016-08-22 16:01:03 +02003490static void mv88e6xxx_phy_destroy(struct mv88e6xxx_chip *chip)
3491{
3492 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU)) {
3493 mv88e6xxx_ppu_state_destroy(chip);
3494 }
3495}
3496
Vivien Didelotfad09c72016-06-21 12:28:20 -04003497static int mv88e6xxx_smi_init(struct mv88e6xxx_chip *chip,
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003498 struct mii_bus *bus, int sw_addr)
3499{
3500 /* ADDR[0] pin is unavailable externally and considered zero */
3501 if (sw_addr & 0x1)
3502 return -EINVAL;
3503
Vivien Didelot914b32f2016-06-20 13:14:11 -04003504 if (sw_addr == 0)
Vivien Didelotfad09c72016-06-21 12:28:20 -04003505 chip->smi_ops = &mv88e6xxx_smi_single_chip_ops;
Vivien Didelota0ffff22016-08-15 17:18:58 -04003506 else if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_MULTI_CHIP))
Vivien Didelotfad09c72016-06-21 12:28:20 -04003507 chip->smi_ops = &mv88e6xxx_smi_multi_chip_ops;
Vivien Didelot914b32f2016-06-20 13:14:11 -04003508 else
3509 return -EINVAL;
3510
Vivien Didelotfad09c72016-06-21 12:28:20 -04003511 chip->bus = bus;
3512 chip->sw_addr = sw_addr;
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003513
3514 return 0;
3515}
3516
Andrew Lunn7b314362016-08-22 16:01:01 +02003517static enum dsa_tag_protocol mv88e6xxx_get_tag_protocol(struct dsa_switch *ds)
3518{
Vivien Didelot04bed142016-08-31 18:06:13 -04003519 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02003520
3521 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA))
3522 return DSA_TAG_PROTO_EDSA;
3523
3524 return DSA_TAG_PROTO_DSA;
Andrew Lunn7b314362016-08-22 16:01:01 +02003525}
3526
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02003527static const char *mv88e6xxx_drv_probe(struct device *dsa_dev,
3528 struct device *host_dev, int sw_addr,
3529 void **priv)
Andrew Lunna77d43f2016-04-13 02:40:42 +02003530{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003531 struct mv88e6xxx_chip *chip;
Vivien Didelota439c062016-04-17 13:23:58 -04003532 struct mii_bus *bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003533 int err;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003534
Vivien Didelota439c062016-04-17 13:23:58 -04003535 bus = dsa_host_dev_to_mii_bus(host_dev);
Andrew Lunnc1569132016-04-13 02:40:45 +02003536 if (!bus)
3537 return NULL;
3538
Vivien Didelotfad09c72016-06-21 12:28:20 -04003539 chip = mv88e6xxx_alloc_chip(dsa_dev);
3540 if (!chip)
Vivien Didelot469d7292016-06-20 13:14:06 -04003541 return NULL;
3542
Vivien Didelotcaac8542016-06-20 13:14:09 -04003543 /* Legacy SMI probing will only support chips similar to 88E6085 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003544 chip->info = &mv88e6xxx_table[MV88E6085];
Vivien Didelotcaac8542016-06-20 13:14:09 -04003545
Vivien Didelotfad09c72016-06-21 12:28:20 -04003546 err = mv88e6xxx_smi_init(chip, bus, sw_addr);
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003547 if (err)
3548 goto free;
3549
Vivien Didelotfad09c72016-06-21 12:28:20 -04003550 err = mv88e6xxx_detect(chip);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003551 if (err)
Vivien Didelot469d7292016-06-20 13:14:06 -04003552 goto free;
Vivien Didelota439c062016-04-17 13:23:58 -04003553
Vivien Didelote57e5e72016-08-15 17:19:00 -04003554 mv88e6xxx_phy_init(chip);
3555
Vivien Didelotfad09c72016-06-21 12:28:20 -04003556 err = mv88e6xxx_mdio_register(chip, NULL);
Andrew Lunnb516d452016-06-04 21:17:06 +02003557 if (err)
Vivien Didelot469d7292016-06-20 13:14:06 -04003558 goto free;
Andrew Lunnb516d452016-06-04 21:17:06 +02003559
Vivien Didelotfad09c72016-06-21 12:28:20 -04003560 *priv = chip;
Vivien Didelota439c062016-04-17 13:23:58 -04003561
Vivien Didelotfad09c72016-06-21 12:28:20 -04003562 return chip->info->name;
Vivien Didelot469d7292016-06-20 13:14:06 -04003563free:
Vivien Didelotfad09c72016-06-21 12:28:20 -04003564 devm_kfree(dsa_dev, chip);
Vivien Didelot469d7292016-06-20 13:14:06 -04003565
3566 return NULL;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003567}
3568
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003569static int mv88e6xxx_port_mdb_prepare(struct dsa_switch *ds, int port,
3570 const struct switchdev_obj_port_mdb *mdb,
3571 struct switchdev_trans *trans)
3572{
3573 /* We don't need any dynamic resource from the kernel (yet),
3574 * so skip the prepare phase.
3575 */
3576
3577 return 0;
3578}
3579
3580static void mv88e6xxx_port_mdb_add(struct dsa_switch *ds, int port,
3581 const struct switchdev_obj_port_mdb *mdb,
3582 struct switchdev_trans *trans)
3583{
Vivien Didelot04bed142016-08-31 18:06:13 -04003584 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003585
3586 mutex_lock(&chip->reg_lock);
3587 if (mv88e6xxx_port_db_load_purge(chip, port, mdb->addr, mdb->vid,
3588 GLOBAL_ATU_DATA_STATE_MC_STATIC))
3589 netdev_err(ds->ports[port].netdev, "failed to load multicast MAC address\n");
3590 mutex_unlock(&chip->reg_lock);
3591}
3592
3593static int mv88e6xxx_port_mdb_del(struct dsa_switch *ds, int port,
3594 const struct switchdev_obj_port_mdb *mdb)
3595{
Vivien Didelot04bed142016-08-31 18:06:13 -04003596 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003597 int err;
3598
3599 mutex_lock(&chip->reg_lock);
3600 err = mv88e6xxx_port_db_load_purge(chip, port, mdb->addr, mdb->vid,
3601 GLOBAL_ATU_DATA_STATE_UNUSED);
3602 mutex_unlock(&chip->reg_lock);
3603
3604 return err;
3605}
3606
3607static int mv88e6xxx_port_mdb_dump(struct dsa_switch *ds, int port,
3608 struct switchdev_obj_port_mdb *mdb,
3609 int (*cb)(struct switchdev_obj *obj))
3610{
Vivien Didelot04bed142016-08-31 18:06:13 -04003611 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003612 int err;
3613
3614 mutex_lock(&chip->reg_lock);
3615 err = mv88e6xxx_port_db_dump(chip, port, &mdb->obj, cb);
3616 mutex_unlock(&chip->reg_lock);
3617
3618 return err;
3619}
3620
Vivien Didelot9d490b42016-08-23 12:38:56 -04003621static struct dsa_switch_ops mv88e6xxx_switch_ops = {
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02003622 .probe = mv88e6xxx_drv_probe,
Andrew Lunn7b314362016-08-22 16:01:01 +02003623 .get_tag_protocol = mv88e6xxx_get_tag_protocol,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003624 .setup = mv88e6xxx_setup,
3625 .set_addr = mv88e6xxx_set_addr,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003626 .adjust_link = mv88e6xxx_adjust_link,
3627 .get_strings = mv88e6xxx_get_strings,
3628 .get_ethtool_stats = mv88e6xxx_get_ethtool_stats,
3629 .get_sset_count = mv88e6xxx_get_sset_count,
3630 .set_eee = mv88e6xxx_set_eee,
3631 .get_eee = mv88e6xxx_get_eee,
3632#ifdef CONFIG_NET_DSA_HWMON
3633 .get_temp = mv88e6xxx_get_temp,
3634 .get_temp_limit = mv88e6xxx_get_temp_limit,
3635 .set_temp_limit = mv88e6xxx_set_temp_limit,
3636 .get_temp_alarm = mv88e6xxx_get_temp_alarm,
3637#endif
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003638 .get_eeprom_len = mv88e6xxx_get_eeprom_len,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003639 .get_eeprom = mv88e6xxx_get_eeprom,
3640 .set_eeprom = mv88e6xxx_set_eeprom,
3641 .get_regs_len = mv88e6xxx_get_regs_len,
3642 .get_regs = mv88e6xxx_get_regs,
Vivien Didelot2cfcd962016-07-18 20:45:40 -04003643 .set_ageing_time = mv88e6xxx_set_ageing_time,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003644 .port_bridge_join = mv88e6xxx_port_bridge_join,
3645 .port_bridge_leave = mv88e6xxx_port_bridge_leave,
3646 .port_stp_state_set = mv88e6xxx_port_stp_state_set,
3647 .port_vlan_filtering = mv88e6xxx_port_vlan_filtering,
3648 .port_vlan_prepare = mv88e6xxx_port_vlan_prepare,
3649 .port_vlan_add = mv88e6xxx_port_vlan_add,
3650 .port_vlan_del = mv88e6xxx_port_vlan_del,
3651 .port_vlan_dump = mv88e6xxx_port_vlan_dump,
3652 .port_fdb_prepare = mv88e6xxx_port_fdb_prepare,
3653 .port_fdb_add = mv88e6xxx_port_fdb_add,
3654 .port_fdb_del = mv88e6xxx_port_fdb_del,
3655 .port_fdb_dump = mv88e6xxx_port_fdb_dump,
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04003656 .port_mdb_prepare = mv88e6xxx_port_mdb_prepare,
3657 .port_mdb_add = mv88e6xxx_port_mdb_add,
3658 .port_mdb_del = mv88e6xxx_port_mdb_del,
3659 .port_mdb_dump = mv88e6xxx_port_mdb_dump,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003660};
3661
Vivien Didelotfad09c72016-06-21 12:28:20 -04003662static int mv88e6xxx_register_switch(struct mv88e6xxx_chip *chip,
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003663 struct device_node *np)
3664{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003665 struct device *dev = chip->dev;
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003666 struct dsa_switch *ds;
3667
3668 ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
3669 if (!ds)
3670 return -ENOMEM;
3671
3672 ds->dev = dev;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003673 ds->priv = chip;
Vivien Didelot9d490b42016-08-23 12:38:56 -04003674 ds->ops = &mv88e6xxx_switch_ops;
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003675
3676 dev_set_drvdata(dev, ds);
3677
3678 return dsa_register_switch(ds, np);
3679}
3680
Vivien Didelotfad09c72016-06-21 12:28:20 -04003681static void mv88e6xxx_unregister_switch(struct mv88e6xxx_chip *chip)
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003682{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003683 dsa_unregister_switch(chip->ds);
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003684}
3685
Vivien Didelot57d32312016-06-20 13:13:58 -04003686static int mv88e6xxx_probe(struct mdio_device *mdiodev)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003687{
3688 struct device *dev = &mdiodev->dev;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003689 struct device_node *np = dev->of_node;
Vivien Didelotcaac8542016-06-20 13:14:09 -04003690 const struct mv88e6xxx_info *compat_info;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003691 struct mv88e6xxx_chip *chip;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003692 u32 eeprom_len;
Andrew Lunn52638f72016-05-10 23:27:22 +02003693 int err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003694
Vivien Didelotcaac8542016-06-20 13:14:09 -04003695 compat_info = of_device_get_match_data(dev);
3696 if (!compat_info)
3697 return -EINVAL;
3698
Vivien Didelotfad09c72016-06-21 12:28:20 -04003699 chip = mv88e6xxx_alloc_chip(dev);
3700 if (!chip)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003701 return -ENOMEM;
3702
Vivien Didelotfad09c72016-06-21 12:28:20 -04003703 chip->info = compat_info;
Vivien Didelotcaac8542016-06-20 13:14:09 -04003704
Vivien Didelotfad09c72016-06-21 12:28:20 -04003705 err = mv88e6xxx_smi_init(chip, mdiodev->bus, mdiodev->addr);
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003706 if (err)
3707 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003708
Vivien Didelotfad09c72016-06-21 12:28:20 -04003709 err = mv88e6xxx_detect(chip);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003710 if (err)
3711 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003712
Vivien Didelote57e5e72016-08-15 17:19:00 -04003713 mv88e6xxx_phy_init(chip);
3714
Vivien Didelotfad09c72016-06-21 12:28:20 -04003715 chip->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_ASIS);
3716 if (IS_ERR(chip->reset))
3717 return PTR_ERR(chip->reset);
Andrew Lunn52638f72016-05-10 23:27:22 +02003718
Vivien Didelot855b1932016-07-20 18:18:35 -04003719 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16) &&
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003720 !of_property_read_u32(np, "eeprom-length", &eeprom_len))
Vivien Didelotfad09c72016-06-21 12:28:20 -04003721 chip->eeprom_len = eeprom_len;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003722
Vivien Didelotfad09c72016-06-21 12:28:20 -04003723 err = mv88e6xxx_mdio_register(chip, np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003724 if (err)
3725 return err;
3726
Vivien Didelotfad09c72016-06-21 12:28:20 -04003727 err = mv88e6xxx_register_switch(chip, np);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02003728 if (err) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04003729 mv88e6xxx_mdio_unregister(chip);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02003730 return err;
3731 }
3732
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003733 return 0;
3734}
3735
3736static void mv88e6xxx_remove(struct mdio_device *mdiodev)
3737{
3738 struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev);
Vivien Didelot04bed142016-08-31 18:06:13 -04003739 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003740
Andrew Lunn930188c2016-08-22 16:01:03 +02003741 mv88e6xxx_phy_destroy(chip);
Vivien Didelotfad09c72016-06-21 12:28:20 -04003742 mv88e6xxx_unregister_switch(chip);
3743 mv88e6xxx_mdio_unregister(chip);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003744}
3745
3746static const struct of_device_id mv88e6xxx_of_match[] = {
Vivien Didelotcaac8542016-06-20 13:14:09 -04003747 {
3748 .compatible = "marvell,mv88e6085",
3749 .data = &mv88e6xxx_table[MV88E6085],
3750 },
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003751 { /* sentinel */ },
3752};
3753
3754MODULE_DEVICE_TABLE(of, mv88e6xxx_of_match);
3755
3756static struct mdio_driver mv88e6xxx_driver = {
3757 .probe = mv88e6xxx_probe,
3758 .remove = mv88e6xxx_remove,
3759 .mdiodrv.driver = {
3760 .name = "mv88e6085",
3761 .of_match_table = mv88e6xxx_of_match,
3762 },
3763};
3764
Ben Hutchings98e67302011-11-25 14:36:19 +00003765static int __init mv88e6xxx_init(void)
3766{
Vivien Didelot9d490b42016-08-23 12:38:56 -04003767 register_switch_driver(&mv88e6xxx_switch_ops);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003768 return mdio_driver_register(&mv88e6xxx_driver);
Ben Hutchings98e67302011-11-25 14:36:19 +00003769}
3770module_init(mv88e6xxx_init);
3771
3772static void __exit mv88e6xxx_cleanup(void)
3773{
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003774 mdio_driver_unregister(&mv88e6xxx_driver);
Vivien Didelot9d490b42016-08-23 12:38:56 -04003775 unregister_switch_driver(&mv88e6xxx_switch_ops);
Ben Hutchings98e67302011-11-25 14:36:19 +00003776}
3777module_exit(mv88e6xxx_cleanup);
Ben Hutchings3d825ed2011-11-25 14:37:16 +00003778
3779MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
3780MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
3781MODULE_LICENSE("GPL");