blob: 750d01d775e0f304cbef0bf0b590128b875a8af1 [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>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000032#include "mv88e6xxx.h"
33
Vivien Didelotfad09c72016-06-21 12:28:20 -040034static void assert_reg_lock(struct mv88e6xxx_chip *chip)
Vivien Didelot3996a4f2015-10-30 18:56:45 -040035{
Vivien Didelotfad09c72016-06-21 12:28:20 -040036 if (unlikely(!mutex_is_locked(&chip->reg_lock))) {
37 dev_err(chip->dev, "Switch registers lock not held!\n");
Vivien Didelot3996a4f2015-10-30 18:56:45 -040038 dump_stack();
39 }
40}
41
Vivien Didelot914b32f2016-06-20 13:14:11 -040042/* The switch ADDR[4:1] configuration pins define the chip SMI device address
43 * (ADDR[0] is always zero, thus only even SMI addresses can be strapped).
44 *
45 * When ADDR is all zero, the chip uses Single-chip Addressing Mode, assuming it
46 * is the only device connected to the SMI master. In this mode it responds to
47 * all 32 possible SMI addresses, and thus maps directly the internal devices.
48 *
49 * When ADDR is non-zero, the chip uses Multi-chip Addressing Mode, allowing
50 * multiple devices to share the SMI interface. In this mode it responds to only
51 * 2 registers, used to indirectly access the internal SMI devices.
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000052 */
Vivien Didelot914b32f2016-06-20 13:14:11 -040053
Vivien Didelotfad09c72016-06-21 12:28:20 -040054static int mv88e6xxx_smi_read(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -040055 int addr, int reg, u16 *val)
56{
Vivien Didelotfad09c72016-06-21 12:28:20 -040057 if (!chip->smi_ops)
Vivien Didelot914b32f2016-06-20 13:14:11 -040058 return -EOPNOTSUPP;
59
Vivien Didelotfad09c72016-06-21 12:28:20 -040060 return chip->smi_ops->read(chip, addr, reg, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -040061}
62
Vivien Didelotfad09c72016-06-21 12:28:20 -040063static int mv88e6xxx_smi_write(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -040064 int addr, int reg, u16 val)
65{
Vivien Didelotfad09c72016-06-21 12:28:20 -040066 if (!chip->smi_ops)
Vivien Didelot914b32f2016-06-20 13:14:11 -040067 return -EOPNOTSUPP;
68
Vivien Didelotfad09c72016-06-21 12:28:20 -040069 return chip->smi_ops->write(chip, addr, reg, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -040070}
71
Vivien Didelotfad09c72016-06-21 12:28:20 -040072static int mv88e6xxx_smi_single_chip_read(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -040073 int addr, int reg, u16 *val)
74{
75 int ret;
76
Vivien Didelotfad09c72016-06-21 12:28:20 -040077 ret = mdiobus_read_nested(chip->bus, addr, reg);
Vivien Didelot914b32f2016-06-20 13:14:11 -040078 if (ret < 0)
79 return ret;
80
81 *val = ret & 0xffff;
82
83 return 0;
84}
85
Vivien Didelotfad09c72016-06-21 12:28:20 -040086static int mv88e6xxx_smi_single_chip_write(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -040087 int addr, int reg, u16 val)
88{
89 int ret;
90
Vivien Didelotfad09c72016-06-21 12:28:20 -040091 ret = mdiobus_write_nested(chip->bus, addr, reg, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -040092 if (ret < 0)
93 return ret;
94
95 return 0;
96}
97
98static const struct mv88e6xxx_ops mv88e6xxx_smi_single_chip_ops = {
99 .read = mv88e6xxx_smi_single_chip_read,
100 .write = mv88e6xxx_smi_single_chip_write,
101};
102
Vivien Didelotfad09c72016-06-21 12:28:20 -0400103static int mv88e6xxx_smi_multi_chip_wait(struct mv88e6xxx_chip *chip)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000104{
105 int ret;
106 int i;
107
108 for (i = 0; i < 16; i++) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400109 ret = mdiobus_read_nested(chip->bus, chip->sw_addr, SMI_CMD);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000110 if (ret < 0)
111 return ret;
112
Andrew Lunncca8b132015-04-02 04:06:39 +0200113 if ((ret & SMI_CMD_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000114 return 0;
115 }
116
117 return -ETIMEDOUT;
118}
119
Vivien Didelotfad09c72016-06-21 12:28:20 -0400120static int mv88e6xxx_smi_multi_chip_read(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -0400121 int addr, int reg, u16 *val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000122{
123 int ret;
124
Barry Grussling3675c8d2013-01-08 16:05:53 +0000125 /* Wait for the bus to become free. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400126 ret = mv88e6xxx_smi_multi_chip_wait(chip);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000127 if (ret < 0)
128 return ret;
129
Barry Grussling3675c8d2013-01-08 16:05:53 +0000130 /* Transmit the read command. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400131 ret = mdiobus_write_nested(chip->bus, chip->sw_addr, SMI_CMD,
Neil Armstrong6e899e62015-10-22 10:37:53 +0200132 SMI_CMD_OP_22_READ | (addr << 5) | reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000133 if (ret < 0)
134 return ret;
135
Barry Grussling3675c8d2013-01-08 16:05:53 +0000136 /* Wait for the read command to complete. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400137 ret = mv88e6xxx_smi_multi_chip_wait(chip);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000138 if (ret < 0)
139 return ret;
140
Barry Grussling3675c8d2013-01-08 16:05:53 +0000141 /* Read the data. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400142 ret = mdiobus_read_nested(chip->bus, chip->sw_addr, SMI_DATA);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000143 if (ret < 0)
144 return ret;
145
Vivien Didelot914b32f2016-06-20 13:14:11 -0400146 *val = ret & 0xffff;
147
148 return 0;
149}
150
Vivien Didelotfad09c72016-06-21 12:28:20 -0400151static int mv88e6xxx_smi_multi_chip_write(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -0400152 int addr, int reg, u16 val)
153{
154 int ret;
155
156 /* Wait for the bus to become free. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400157 ret = mv88e6xxx_smi_multi_chip_wait(chip);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400158 if (ret < 0)
159 return ret;
160
161 /* Transmit the data to write. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400162 ret = mdiobus_write_nested(chip->bus, chip->sw_addr, SMI_DATA, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400163 if (ret < 0)
164 return ret;
165
166 /* Transmit the write command. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400167 ret = mdiobus_write_nested(chip->bus, chip->sw_addr, SMI_CMD,
Vivien Didelot914b32f2016-06-20 13:14:11 -0400168 SMI_CMD_OP_22_WRITE | (addr << 5) | reg);
169 if (ret < 0)
170 return ret;
171
172 /* Wait for the write command to complete. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400173 ret = mv88e6xxx_smi_multi_chip_wait(chip);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400174 if (ret < 0)
175 return ret;
176
177 return 0;
178}
179
180static const struct mv88e6xxx_ops mv88e6xxx_smi_multi_chip_ops = {
181 .read = mv88e6xxx_smi_multi_chip_read,
182 .write = mv88e6xxx_smi_multi_chip_write,
183};
184
Vivien Didelotfad09c72016-06-21 12:28:20 -0400185static int mv88e6xxx_read(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -0400186 int addr, int reg, u16 *val)
187{
188 int err;
189
Vivien Didelotfad09c72016-06-21 12:28:20 -0400190 assert_reg_lock(chip);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400191
Vivien Didelotfad09c72016-06-21 12:28:20 -0400192 err = mv88e6xxx_smi_read(chip, addr, reg, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400193 if (err)
194 return err;
195
Vivien Didelotfad09c72016-06-21 12:28:20 -0400196 dev_dbg(chip->dev, "<- addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
Vivien Didelot914b32f2016-06-20 13:14:11 -0400197 addr, reg, *val);
198
199 return 0;
200}
201
Vivien Didelotfad09c72016-06-21 12:28:20 -0400202static int mv88e6xxx_write(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -0400203 int addr, int reg, u16 val)
204{
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 Didelot2d79af62016-08-15 17:18:57 -0400309static int mv88e6xxx_wait(struct mv88e6xxx_chip *chip, int addr, int reg,
310 u16 mask)
311{
Andrew Lunn6441e6692016-08-19 00:01:55 +0200312 int i;
Vivien Didelot2d79af62016-08-15 17:18:57 -0400313
Andrew Lunn6441e6692016-08-19 00:01:55 +0200314 for (i = 0; i < 16; i++) {
Vivien Didelot2d79af62016-08-15 17:18:57 -0400315 u16 val;
316 int err;
317
318 err = mv88e6xxx_read(chip, addr, reg, &val);
319 if (err)
320 return err;
321
322 if (!(val & mask))
323 return 0;
324
325 usleep_range(1000, 2000);
326 }
327
Andrew Lunn30853552016-08-19 00:01:57 +0200328 dev_err(chip->dev, "Timeout while waiting for switch\n");
Vivien Didelot2d79af62016-08-15 17:18:57 -0400329 return -ETIMEDOUT;
330}
331
Vivien Didelotf22ab642016-07-18 20:45:31 -0400332/* Indirect write to single pointer-data register with an Update bit */
333static int mv88e6xxx_update(struct mv88e6xxx_chip *chip, int addr, int reg,
334 u16 update)
335{
336 u16 val;
Andrew Lunn0f02b4f2016-08-19 00:01:56 +0200337 int err;
Vivien Didelotf22ab642016-07-18 20:45:31 -0400338
339 /* Wait until the previous operation is completed */
Andrew Lunn0f02b4f2016-08-19 00:01:56 +0200340 err = mv88e6xxx_wait(chip, addr, reg, BIT(15));
341 if (err)
342 return err;
Vivien Didelotf22ab642016-07-18 20:45:31 -0400343
344 /* Set the Update bit to trigger a write operation */
345 val = BIT(15) | update;
346
347 return mv88e6xxx_write(chip, addr, reg, val);
348}
349
Vivien Didelotfad09c72016-06-21 12:28:20 -0400350static int _mv88e6xxx_reg_read(struct mv88e6xxx_chip *chip, int addr, int reg)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000351{
Vivien Didelot914b32f2016-06-20 13:14:11 -0400352 u16 val;
353 int err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000354
Vivien Didelotfad09c72016-06-21 12:28:20 -0400355 err = mv88e6xxx_read(chip, addr, reg, &val);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400356 if (err)
357 return err;
Vivien Didelot3996a4f2015-10-30 18:56:45 -0400358
Vivien Didelot914b32f2016-06-20 13:14:11 -0400359 return val;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000360}
361
Vivien Didelotfad09c72016-06-21 12:28:20 -0400362static int _mv88e6xxx_reg_write(struct mv88e6xxx_chip *chip, int addr,
Andrew Lunn158bc062016-04-28 21:24:06 -0400363 int reg, u16 val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000364{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400365 return mv88e6xxx_write(chip, addr, reg, val);
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700366}
367
Vivien Didelotfad09c72016-06-21 12:28:20 -0400368static int mv88e6xxx_ppu_disable(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000369{
370 int ret;
Andrew Lunn6441e6692016-08-19 00:01:55 +0200371 int i;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000372
Vivien Didelotfad09c72016-06-21 12:28:20 -0400373 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_CONTROL);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200374 if (ret < 0)
375 return ret;
376
Vivien Didelotfad09c72016-06-21 12:28:20 -0400377 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL,
Vivien Didelot8c9983a2016-05-09 13:22:39 -0400378 ret & ~GLOBAL_CONTROL_PPU_ENABLE);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200379 if (ret)
380 return ret;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000381
Andrew Lunn6441e6692016-08-19 00:01:55 +0200382 for (i = 0; i < 16; i++) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400383 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATUS);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200384 if (ret < 0)
385 return ret;
386
Barry Grussling19b2f972013-01-08 16:05:54 +0000387 usleep_range(1000, 2000);
Andrew Lunncca8b132015-04-02 04:06:39 +0200388 if ((ret & GLOBAL_STATUS_PPU_MASK) !=
389 GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000390 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000391 }
392
393 return -ETIMEDOUT;
394}
395
Vivien Didelotfad09c72016-06-21 12:28:20 -0400396static int mv88e6xxx_ppu_enable(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000397{
Andrew Lunn6441e6692016-08-19 00:01:55 +0200398 int ret, err, i;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000399
Vivien Didelotfad09c72016-06-21 12:28:20 -0400400 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_CONTROL);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200401 if (ret < 0)
402 return ret;
403
Vivien Didelotfad09c72016-06-21 12:28:20 -0400404 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL,
Vivien Didelot762eb672016-06-04 21:16:54 +0200405 ret | GLOBAL_CONTROL_PPU_ENABLE);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200406 if (err)
407 return err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000408
Andrew Lunn6441e6692016-08-19 00:01:55 +0200409 for (i = 0; i < 16; i++) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400410 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATUS);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200411 if (ret < 0)
412 return ret;
413
Barry Grussling19b2f972013-01-08 16:05:54 +0000414 usleep_range(1000, 2000);
Andrew Lunncca8b132015-04-02 04:06:39 +0200415 if ((ret & GLOBAL_STATUS_PPU_MASK) ==
416 GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000417 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000418 }
419
420 return -ETIMEDOUT;
421}
422
423static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
424{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400425 struct mv88e6xxx_chip *chip;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000426
Vivien Didelotfad09c72016-06-21 12:28:20 -0400427 chip = container_of(ugly, struct mv88e6xxx_chip, ppu_work);
Vivien Didelot762eb672016-06-04 21:16:54 +0200428
Vivien Didelotfad09c72016-06-21 12:28:20 -0400429 mutex_lock(&chip->reg_lock);
Vivien Didelot762eb672016-06-04 21:16:54 +0200430
Vivien Didelotfad09c72016-06-21 12:28:20 -0400431 if (mutex_trylock(&chip->ppu_mutex)) {
432 if (mv88e6xxx_ppu_enable(chip) == 0)
433 chip->ppu_disabled = 0;
434 mutex_unlock(&chip->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000435 }
Vivien Didelot762eb672016-06-04 21:16:54 +0200436
Vivien Didelotfad09c72016-06-21 12:28:20 -0400437 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000438}
439
440static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
441{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400442 struct mv88e6xxx_chip *chip = (void *)_ps;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000443
Vivien Didelotfad09c72016-06-21 12:28:20 -0400444 schedule_work(&chip->ppu_work);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000445}
446
Vivien Didelotfad09c72016-06-21 12:28:20 -0400447static int mv88e6xxx_ppu_access_get(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000448{
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000449 int ret;
450
Vivien Didelotfad09c72016-06-21 12:28:20 -0400451 mutex_lock(&chip->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000452
Barry Grussling3675c8d2013-01-08 16:05:53 +0000453 /* If the PHY polling unit is enabled, disable it so that
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000454 * we can access the PHY registers. If it was already
455 * disabled, cancel the timer that is going to re-enable
456 * it.
457 */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400458 if (!chip->ppu_disabled) {
459 ret = mv88e6xxx_ppu_disable(chip);
Barry Grussling85686582013-01-08 16:05:56 +0000460 if (ret < 0) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400461 mutex_unlock(&chip->ppu_mutex);
Barry Grussling85686582013-01-08 16:05:56 +0000462 return ret;
463 }
Vivien Didelotfad09c72016-06-21 12:28:20 -0400464 chip->ppu_disabled = 1;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000465 } else {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400466 del_timer(&chip->ppu_timer);
Barry Grussling85686582013-01-08 16:05:56 +0000467 ret = 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000468 }
469
470 return ret;
471}
472
Vivien Didelotfad09c72016-06-21 12:28:20 -0400473static void mv88e6xxx_ppu_access_put(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000474{
Barry Grussling3675c8d2013-01-08 16:05:53 +0000475 /* Schedule a timer to re-enable the PHY polling unit. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400476 mod_timer(&chip->ppu_timer, jiffies + msecs_to_jiffies(10));
477 mutex_unlock(&chip->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000478}
479
Vivien Didelotfad09c72016-06-21 12:28:20 -0400480static void mv88e6xxx_ppu_state_init(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000481{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400482 mutex_init(&chip->ppu_mutex);
483 INIT_WORK(&chip->ppu_work, mv88e6xxx_ppu_reenable_work);
484 init_timer(&chip->ppu_timer);
485 chip->ppu_timer.data = (unsigned long)chip;
486 chip->ppu_timer.function = mv88e6xxx_ppu_reenable_timer;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000487}
488
Andrew Lunn930188c2016-08-22 16:01:03 +0200489static void mv88e6xxx_ppu_state_destroy(struct mv88e6xxx_chip *chip)
490{
491 del_timer_sync(&chip->ppu_timer);
492}
493
Vivien Didelote57e5e72016-08-15 17:19:00 -0400494static int mv88e6xxx_phy_ppu_read(struct mv88e6xxx_chip *chip, int addr,
495 int reg, u16 *val)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000496{
Vivien Didelote57e5e72016-08-15 17:19:00 -0400497 int err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000498
Vivien Didelote57e5e72016-08-15 17:19:00 -0400499 err = mv88e6xxx_ppu_access_get(chip);
500 if (!err) {
501 err = mv88e6xxx_read(chip, addr, reg, val);
Vivien Didelotfad09c72016-06-21 12:28:20 -0400502 mv88e6xxx_ppu_access_put(chip);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000503 }
504
Vivien Didelote57e5e72016-08-15 17:19:00 -0400505 return err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000506}
507
Vivien Didelote57e5e72016-08-15 17:19:00 -0400508static int mv88e6xxx_phy_ppu_write(struct mv88e6xxx_chip *chip, int addr,
509 int reg, u16 val)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000510{
Vivien Didelote57e5e72016-08-15 17:19:00 -0400511 int err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000512
Vivien Didelote57e5e72016-08-15 17:19:00 -0400513 err = mv88e6xxx_ppu_access_get(chip);
514 if (!err) {
515 err = mv88e6xxx_write(chip, addr, reg, val);
Vivien Didelotfad09c72016-06-21 12:28:20 -0400516 mv88e6xxx_ppu_access_put(chip);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000517 }
518
Vivien Didelote57e5e72016-08-15 17:19:00 -0400519 return err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000520}
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000521
Vivien Didelote57e5e72016-08-15 17:19:00 -0400522static const struct mv88e6xxx_ops mv88e6xxx_phy_ppu_ops = {
523 .read = mv88e6xxx_phy_ppu_read,
524 .write = mv88e6xxx_phy_ppu_write,
525};
526
Vivien Didelotfad09c72016-06-21 12:28:20 -0400527static bool mv88e6xxx_6065_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200528{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400529 return chip->info->family == MV88E6XXX_FAMILY_6065;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200530}
531
Vivien Didelotfad09c72016-06-21 12:28:20 -0400532static bool mv88e6xxx_6095_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200533{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400534 return chip->info->family == MV88E6XXX_FAMILY_6095;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200535}
536
Vivien Didelotfad09c72016-06-21 12:28:20 -0400537static bool mv88e6xxx_6097_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200538{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400539 return chip->info->family == MV88E6XXX_FAMILY_6097;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200540}
541
Vivien Didelotfad09c72016-06-21 12:28:20 -0400542static bool mv88e6xxx_6165_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200543{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400544 return chip->info->family == MV88E6XXX_FAMILY_6165;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200545}
546
Vivien Didelotfad09c72016-06-21 12:28:20 -0400547static bool mv88e6xxx_6185_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200548{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400549 return chip->info->family == MV88E6XXX_FAMILY_6185;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200550}
551
Vivien Didelotfad09c72016-06-21 12:28:20 -0400552static bool mv88e6xxx_6320_family(struct mv88e6xxx_chip *chip)
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700553{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400554 return chip->info->family == MV88E6XXX_FAMILY_6320;
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700555}
556
Vivien Didelotfad09c72016-06-21 12:28:20 -0400557static bool mv88e6xxx_6351_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200558{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400559 return chip->info->family == MV88E6XXX_FAMILY_6351;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200560}
561
Vivien Didelotfad09c72016-06-21 12:28:20 -0400562static bool mv88e6xxx_6352_family(struct mv88e6xxx_chip *chip)
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200563{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400564 return chip->info->family == MV88E6XXX_FAMILY_6352;
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200565}
566
Vivien Didelotfad09c72016-06-21 12:28:20 -0400567static unsigned int mv88e6xxx_num_databases(struct mv88e6xxx_chip *chip)
Vivien Didelotf74df0b2016-03-31 16:53:43 -0400568{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400569 return chip->info->num_databases;
Vivien Didelotf74df0b2016-03-31 16:53:43 -0400570}
571
Vivien Didelotfad09c72016-06-21 12:28:20 -0400572static bool mv88e6xxx_has_fid_reg(struct mv88e6xxx_chip *chip)
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400573{
574 /* Does the device have dedicated FID registers for ATU and VTU ops? */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400575 if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
576 mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip))
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400577 return true;
578
579 return false;
580}
581
Andrew Lunndea87022015-08-31 15:56:47 +0200582/* We expect the switch to perform auto negotiation if there is a real
583 * phy. However, in the case of a fixed link phy, we force the port
584 * settings from the fixed link settings.
585 */
Vivien Didelotf81ec902016-05-09 13:22:58 -0400586static void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port,
587 struct phy_device *phydev)
Andrew Lunndea87022015-08-31 15:56:47 +0200588{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400589 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunn49052872015-09-29 01:53:48 +0200590 u32 reg;
591 int ret;
Andrew Lunndea87022015-08-31 15:56:47 +0200592
593 if (!phy_is_pseudo_fixed_link(phydev))
594 return;
595
Vivien Didelotfad09c72016-06-21 12:28:20 -0400596 mutex_lock(&chip->reg_lock);
Andrew Lunndea87022015-08-31 15:56:47 +0200597
Vivien Didelotfad09c72016-06-21 12:28:20 -0400598 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_PCS_CTRL);
Andrew Lunndea87022015-08-31 15:56:47 +0200599 if (ret < 0)
600 goto out;
601
602 reg = ret & ~(PORT_PCS_CTRL_LINK_UP |
603 PORT_PCS_CTRL_FORCE_LINK |
604 PORT_PCS_CTRL_DUPLEX_FULL |
605 PORT_PCS_CTRL_FORCE_DUPLEX |
606 PORT_PCS_CTRL_UNFORCED);
607
608 reg |= PORT_PCS_CTRL_FORCE_LINK;
609 if (phydev->link)
Vivien Didelot57d32312016-06-20 13:13:58 -0400610 reg |= PORT_PCS_CTRL_LINK_UP;
Andrew Lunndea87022015-08-31 15:56:47 +0200611
Vivien Didelotfad09c72016-06-21 12:28:20 -0400612 if (mv88e6xxx_6065_family(chip) && phydev->speed > SPEED_100)
Andrew Lunndea87022015-08-31 15:56:47 +0200613 goto out;
614
615 switch (phydev->speed) {
616 case SPEED_1000:
617 reg |= PORT_PCS_CTRL_1000;
618 break;
619 case SPEED_100:
620 reg |= PORT_PCS_CTRL_100;
621 break;
622 case SPEED_10:
623 reg |= PORT_PCS_CTRL_10;
624 break;
625 default:
626 pr_info("Unknown speed");
627 goto out;
628 }
629
630 reg |= PORT_PCS_CTRL_FORCE_DUPLEX;
631 if (phydev->duplex == DUPLEX_FULL)
632 reg |= PORT_PCS_CTRL_DUPLEX_FULL;
633
Vivien Didelotfad09c72016-06-21 12:28:20 -0400634 if ((mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip)) &&
635 (port >= chip->info->num_ports - 2)) {
Andrew Lunne7e72ac2015-08-31 15:56:51 +0200636 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
637 reg |= PORT_PCS_CTRL_RGMII_DELAY_RXCLK;
638 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
639 reg |= PORT_PCS_CTRL_RGMII_DELAY_TXCLK;
640 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
641 reg |= (PORT_PCS_CTRL_RGMII_DELAY_RXCLK |
642 PORT_PCS_CTRL_RGMII_DELAY_TXCLK);
643 }
Vivien Didelotfad09c72016-06-21 12:28:20 -0400644 _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_PCS_CTRL, reg);
Andrew Lunndea87022015-08-31 15:56:47 +0200645
646out:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400647 mutex_unlock(&chip->reg_lock);
Andrew Lunndea87022015-08-31 15:56:47 +0200648}
649
Vivien Didelotfad09c72016-06-21 12:28:20 -0400650static int _mv88e6xxx_stats_wait(struct mv88e6xxx_chip *chip)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000651{
652 int ret;
653 int i;
654
655 for (i = 0; i < 10; i++) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400656 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_OP);
Andrew Lunncca8b132015-04-02 04:06:39 +0200657 if ((ret & GLOBAL_STATS_OP_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000658 return 0;
659 }
660
661 return -ETIMEDOUT;
662}
663
Vivien Didelotfad09c72016-06-21 12:28:20 -0400664static int _mv88e6xxx_stats_snapshot(struct mv88e6xxx_chip *chip, int port)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000665{
666 int ret;
667
Vivien Didelotfad09c72016-06-21 12:28:20 -0400668 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200669 port = (port + 1) << 5;
670
Barry Grussling3675c8d2013-01-08 16:05:53 +0000671 /* Snapshot the hardware statistics counters for this port. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400672 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
Andrew Lunn31888232015-05-06 01:09:54 +0200673 GLOBAL_STATS_OP_CAPTURE_PORT |
674 GLOBAL_STATS_OP_HIST_RX_TX | port);
675 if (ret < 0)
676 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000677
Barry Grussling3675c8d2013-01-08 16:05:53 +0000678 /* Wait for the snapshotting to complete. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400679 ret = _mv88e6xxx_stats_wait(chip);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000680 if (ret < 0)
681 return ret;
682
683 return 0;
684}
685
Vivien Didelotfad09c72016-06-21 12:28:20 -0400686static void _mv88e6xxx_stats_read(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -0400687 int stat, u32 *val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000688{
689 u32 _val;
690 int ret;
691
692 *val = 0;
693
Vivien Didelotfad09c72016-06-21 12:28:20 -0400694 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
Andrew Lunn31888232015-05-06 01:09:54 +0200695 GLOBAL_STATS_OP_READ_CAPTURED |
696 GLOBAL_STATS_OP_HIST_RX_TX | stat);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000697 if (ret < 0)
698 return;
699
Vivien Didelotfad09c72016-06-21 12:28:20 -0400700 ret = _mv88e6xxx_stats_wait(chip);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000701 if (ret < 0)
702 return;
703
Vivien Didelotfad09c72016-06-21 12:28:20 -0400704 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_COUNTER_32);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000705 if (ret < 0)
706 return;
707
708 _val = ret << 16;
709
Vivien Didelotfad09c72016-06-21 12:28:20 -0400710 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_COUNTER_01);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000711 if (ret < 0)
712 return;
713
714 *val = _val | ret;
715}
716
Andrew Lunne413e7e2015-04-02 04:06:38 +0200717static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100718 { "in_good_octets", 8, 0x00, BANK0, },
719 { "in_bad_octets", 4, 0x02, BANK0, },
720 { "in_unicast", 4, 0x04, BANK0, },
721 { "in_broadcasts", 4, 0x06, BANK0, },
722 { "in_multicasts", 4, 0x07, BANK0, },
723 { "in_pause", 4, 0x16, BANK0, },
724 { "in_undersize", 4, 0x18, BANK0, },
725 { "in_fragments", 4, 0x19, BANK0, },
726 { "in_oversize", 4, 0x1a, BANK0, },
727 { "in_jabber", 4, 0x1b, BANK0, },
728 { "in_rx_error", 4, 0x1c, BANK0, },
729 { "in_fcs_error", 4, 0x1d, BANK0, },
730 { "out_octets", 8, 0x0e, BANK0, },
731 { "out_unicast", 4, 0x10, BANK0, },
732 { "out_broadcasts", 4, 0x13, BANK0, },
733 { "out_multicasts", 4, 0x12, BANK0, },
734 { "out_pause", 4, 0x15, BANK0, },
735 { "excessive", 4, 0x11, BANK0, },
736 { "collisions", 4, 0x1e, BANK0, },
737 { "deferred", 4, 0x05, BANK0, },
738 { "single", 4, 0x14, BANK0, },
739 { "multiple", 4, 0x17, BANK0, },
740 { "out_fcs_error", 4, 0x03, BANK0, },
741 { "late", 4, 0x1f, BANK0, },
742 { "hist_64bytes", 4, 0x08, BANK0, },
743 { "hist_65_127bytes", 4, 0x09, BANK0, },
744 { "hist_128_255bytes", 4, 0x0a, BANK0, },
745 { "hist_256_511bytes", 4, 0x0b, BANK0, },
746 { "hist_512_1023bytes", 4, 0x0c, BANK0, },
747 { "hist_1024_max_bytes", 4, 0x0d, BANK0, },
748 { "sw_in_discards", 4, 0x10, PORT, },
749 { "sw_in_filtered", 2, 0x12, PORT, },
750 { "sw_out_filtered", 2, 0x13, PORT, },
751 { "in_discards", 4, 0x00 | GLOBAL_STATS_OP_BANK_1, BANK1, },
752 { "in_filtered", 4, 0x01 | GLOBAL_STATS_OP_BANK_1, BANK1, },
753 { "in_accepted", 4, 0x02 | GLOBAL_STATS_OP_BANK_1, BANK1, },
754 { "in_bad_accepted", 4, 0x03 | GLOBAL_STATS_OP_BANK_1, BANK1, },
755 { "in_good_avb_class_a", 4, 0x04 | GLOBAL_STATS_OP_BANK_1, BANK1, },
756 { "in_good_avb_class_b", 4, 0x05 | GLOBAL_STATS_OP_BANK_1, BANK1, },
757 { "in_bad_avb_class_a", 4, 0x06 | GLOBAL_STATS_OP_BANK_1, BANK1, },
758 { "in_bad_avb_class_b", 4, 0x07 | GLOBAL_STATS_OP_BANK_1, BANK1, },
759 { "tcam_counter_0", 4, 0x08 | GLOBAL_STATS_OP_BANK_1, BANK1, },
760 { "tcam_counter_1", 4, 0x09 | GLOBAL_STATS_OP_BANK_1, BANK1, },
761 { "tcam_counter_2", 4, 0x0a | GLOBAL_STATS_OP_BANK_1, BANK1, },
762 { "tcam_counter_3", 4, 0x0b | GLOBAL_STATS_OP_BANK_1, BANK1, },
763 { "in_da_unknown", 4, 0x0e | GLOBAL_STATS_OP_BANK_1, BANK1, },
764 { "in_management", 4, 0x0f | GLOBAL_STATS_OP_BANK_1, BANK1, },
765 { "out_queue_0", 4, 0x10 | GLOBAL_STATS_OP_BANK_1, BANK1, },
766 { "out_queue_1", 4, 0x11 | GLOBAL_STATS_OP_BANK_1, BANK1, },
767 { "out_queue_2", 4, 0x12 | GLOBAL_STATS_OP_BANK_1, BANK1, },
768 { "out_queue_3", 4, 0x13 | GLOBAL_STATS_OP_BANK_1, BANK1, },
769 { "out_queue_4", 4, 0x14 | GLOBAL_STATS_OP_BANK_1, BANK1, },
770 { "out_queue_5", 4, 0x15 | GLOBAL_STATS_OP_BANK_1, BANK1, },
771 { "out_queue_6", 4, 0x16 | GLOBAL_STATS_OP_BANK_1, BANK1, },
772 { "out_queue_7", 4, 0x17 | GLOBAL_STATS_OP_BANK_1, BANK1, },
773 { "out_cut_through", 4, 0x18 | GLOBAL_STATS_OP_BANK_1, BANK1, },
774 { "out_octets_a", 4, 0x1a | GLOBAL_STATS_OP_BANK_1, BANK1, },
775 { "out_octets_b", 4, 0x1b | GLOBAL_STATS_OP_BANK_1, BANK1, },
776 { "out_management", 4, 0x1f | GLOBAL_STATS_OP_BANK_1, BANK1, },
Andrew Lunne413e7e2015-04-02 04:06:38 +0200777};
778
Vivien Didelotfad09c72016-06-21 12:28:20 -0400779static bool mv88e6xxx_has_stat(struct mv88e6xxx_chip *chip,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100780 struct mv88e6xxx_hw_stat *stat)
Andrew Lunne413e7e2015-04-02 04:06:38 +0200781{
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100782 switch (stat->type) {
783 case BANK0:
Andrew Lunne413e7e2015-04-02 04:06:38 +0200784 return true;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100785 case BANK1:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400786 return mv88e6xxx_6320_family(chip);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100787 case PORT:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400788 return mv88e6xxx_6095_family(chip) ||
789 mv88e6xxx_6185_family(chip) ||
790 mv88e6xxx_6097_family(chip) ||
791 mv88e6xxx_6165_family(chip) ||
792 mv88e6xxx_6351_family(chip) ||
793 mv88e6xxx_6352_family(chip);
Andrew Lunne413e7e2015-04-02 04:06:38 +0200794 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100795 return false;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000796}
797
Vivien Didelotfad09c72016-06-21 12:28:20 -0400798static uint64_t _mv88e6xxx_get_ethtool_stat(struct mv88e6xxx_chip *chip,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100799 struct mv88e6xxx_hw_stat *s,
Andrew Lunn80c46272015-06-20 18:42:30 +0200800 int port)
801{
Andrew Lunn80c46272015-06-20 18:42:30 +0200802 u32 low;
803 u32 high = 0;
804 int ret;
805 u64 value;
806
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100807 switch (s->type) {
808 case PORT:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400809 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), s->reg);
Andrew Lunn80c46272015-06-20 18:42:30 +0200810 if (ret < 0)
811 return UINT64_MAX;
812
813 low = ret;
814 if (s->sizeof_stat == 4) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400815 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port),
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100816 s->reg + 1);
Andrew Lunn80c46272015-06-20 18:42:30 +0200817 if (ret < 0)
818 return UINT64_MAX;
819 high = ret;
820 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100821 break;
822 case BANK0:
823 case BANK1:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400824 _mv88e6xxx_stats_read(chip, s->reg, &low);
Andrew Lunn80c46272015-06-20 18:42:30 +0200825 if (s->sizeof_stat == 8)
Vivien Didelotfad09c72016-06-21 12:28:20 -0400826 _mv88e6xxx_stats_read(chip, s->reg + 1, &high);
Andrew Lunn80c46272015-06-20 18:42:30 +0200827 }
828 value = (((u64)high) << 16) | low;
829 return value;
830}
831
Vivien Didelotf81ec902016-05-09 13:22:58 -0400832static void mv88e6xxx_get_strings(struct dsa_switch *ds, int port,
833 uint8_t *data)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100834{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400835 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100836 struct mv88e6xxx_hw_stat *stat;
837 int i, j;
838
839 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
840 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400841 if (mv88e6xxx_has_stat(chip, stat)) {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100842 memcpy(data + j * ETH_GSTRING_LEN, stat->string,
843 ETH_GSTRING_LEN);
844 j++;
845 }
846 }
847}
848
Vivien Didelotf81ec902016-05-09 13:22:58 -0400849static int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100850{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400851 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100852 struct mv88e6xxx_hw_stat *stat;
853 int i, j;
854
855 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
856 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400857 if (mv88e6xxx_has_stat(chip, stat))
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100858 j++;
859 }
860 return j;
861}
862
Vivien Didelotf81ec902016-05-09 13:22:58 -0400863static void mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, int port,
864 uint64_t *data)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000865{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400866 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100867 struct mv88e6xxx_hw_stat *stat;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000868 int ret;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100869 int i, j;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000870
Vivien Didelotfad09c72016-06-21 12:28:20 -0400871 mutex_lock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000872
Vivien Didelotfad09c72016-06-21 12:28:20 -0400873 ret = _mv88e6xxx_stats_snapshot(chip, port);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000874 if (ret < 0) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400875 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000876 return;
877 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100878 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
879 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400880 if (mv88e6xxx_has_stat(chip, stat)) {
881 data[j] = _mv88e6xxx_get_ethtool_stat(chip, stat, port);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100882 j++;
883 }
884 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000885
Vivien Didelotfad09c72016-06-21 12:28:20 -0400886 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000887}
Ben Hutchings98e67302011-11-25 14:36:19 +0000888
Vivien Didelotf81ec902016-05-09 13:22:58 -0400889static int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700890{
891 return 32 * sizeof(u16);
892}
893
Vivien Didelotf81ec902016-05-09 13:22:58 -0400894static void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
895 struct ethtool_regs *regs, void *_p)
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700896{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400897 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700898 u16 *p = _p;
899 int i;
900
901 regs->version = 0;
902
903 memset(p, 0xff, 32 * sizeof(u16));
904
Vivien Didelotfad09c72016-06-21 12:28:20 -0400905 mutex_lock(&chip->reg_lock);
Vivien Didelot23062512016-05-09 13:22:45 -0400906
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700907 for (i = 0; i < 32; i++) {
908 int ret;
909
Vivien Didelotfad09c72016-06-21 12:28:20 -0400910 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), i);
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700911 if (ret >= 0)
912 p[i] = ret;
913 }
Vivien Didelot23062512016-05-09 13:22:45 -0400914
Vivien Didelotfad09c72016-06-21 12:28:20 -0400915 mutex_unlock(&chip->reg_lock);
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700916}
917
Vivien Didelotfad09c72016-06-21 12:28:20 -0400918static int _mv88e6xxx_atu_wait(struct mv88e6xxx_chip *chip)
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700919{
Vivien Didelot2d79af62016-08-15 17:18:57 -0400920 return mv88e6xxx_wait(chip, REG_GLOBAL, GLOBAL_ATU_OP,
921 GLOBAL_ATU_OP_BUSY);
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700922}
923
Vivien Didelotf81ec902016-05-09 13:22:58 -0400924static int mv88e6xxx_get_eee(struct dsa_switch *ds, int port,
925 struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -0800926{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400927 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelot9c938292016-08-15 17:19:02 -0400928 u16 reg;
929 int err;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800930
Vivien Didelotfad09c72016-06-21 12:28:20 -0400931 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
Vivien Didelotaadbdb82016-05-09 13:22:44 -0400932 return -EOPNOTSUPP;
933
Vivien Didelotfad09c72016-06-21 12:28:20 -0400934 mutex_lock(&chip->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200935
Vivien Didelot9c938292016-08-15 17:19:02 -0400936 err = mv88e6xxx_phy_read(chip, port, 16, &reg);
937 if (err)
Andrew Lunn2f40c692015-04-02 04:06:37 +0200938 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800939
940 e->eee_enabled = !!(reg & 0x0200);
941 e->tx_lpi_enabled = !!(reg & 0x0100);
942
Vivien Didelot9c938292016-08-15 17:19:02 -0400943 err = mv88e6xxx_read(chip, REG_PORT(port), PORT_STATUS, &reg);
944 if (err)
Andrew Lunn2f40c692015-04-02 04:06:37 +0200945 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800946
Andrew Lunncca8b132015-04-02 04:06:39 +0200947 e->eee_active = !!(reg & PORT_STATUS_EEE);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200948out:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400949 mutex_unlock(&chip->reg_lock);
Vivien Didelot9c938292016-08-15 17:19:02 -0400950
951 return err;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800952}
953
Vivien Didelotf81ec902016-05-09 13:22:58 -0400954static int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
955 struct phy_device *phydev, struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -0800956{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400957 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelot9c938292016-08-15 17:19:02 -0400958 u16 reg;
959 int err;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800960
Vivien Didelotfad09c72016-06-21 12:28:20 -0400961 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
Vivien Didelotaadbdb82016-05-09 13:22:44 -0400962 return -EOPNOTSUPP;
963
Vivien Didelotfad09c72016-06-21 12:28:20 -0400964 mutex_lock(&chip->reg_lock);
Guenter Roeck11b3b452015-03-06 22:23:51 -0800965
Vivien Didelot9c938292016-08-15 17:19:02 -0400966 err = mv88e6xxx_phy_read(chip, port, 16, &reg);
967 if (err)
Andrew Lunn2f40c692015-04-02 04:06:37 +0200968 goto out;
969
Vivien Didelot9c938292016-08-15 17:19:02 -0400970 reg &= ~0x0300;
Andrew Lunn2f40c692015-04-02 04:06:37 +0200971 if (e->eee_enabled)
972 reg |= 0x0200;
973 if (e->tx_lpi_enabled)
974 reg |= 0x0100;
975
Vivien Didelot9c938292016-08-15 17:19:02 -0400976 err = mv88e6xxx_phy_write(chip, port, 16, reg);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200977out:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400978 mutex_unlock(&chip->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200979
Vivien Didelot9c938292016-08-15 17:19:02 -0400980 return err;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800981}
982
Vivien Didelotfad09c72016-06-21 12:28:20 -0400983static int _mv88e6xxx_atu_cmd(struct mv88e6xxx_chip *chip, u16 fid, u16 cmd)
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700984{
985 int ret;
986
Vivien Didelotfad09c72016-06-21 12:28:20 -0400987 if (mv88e6xxx_has_fid_reg(chip)) {
988 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_FID,
989 fid);
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400990 if (ret < 0)
991 return ret;
Vivien Didelotfad09c72016-06-21 12:28:20 -0400992 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -0400993 /* ATU DBNum[7:4] are located in ATU Control 15:12 */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400994 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL);
Vivien Didelot11ea8092016-03-31 16:53:44 -0400995 if (ret < 0)
996 return ret;
997
Vivien Didelotfad09c72016-06-21 12:28:20 -0400998 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL,
Vivien Didelot11ea8092016-03-31 16:53:44 -0400999 (ret & 0xfff) |
1000 ((fid << 8) & 0xf000));
1001 if (ret < 0)
1002 return ret;
1003
1004 /* ATU DBNum[3:0] are located in ATU Operation 3:0 */
1005 cmd |= fid & 0xf;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001006 }
1007
Vivien Didelotfad09c72016-06-21 12:28:20 -04001008 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_OP, cmd);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001009 if (ret < 0)
1010 return ret;
1011
Vivien Didelotfad09c72016-06-21 12:28:20 -04001012 return _mv88e6xxx_atu_wait(chip);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001013}
1014
Vivien Didelotfad09c72016-06-21 12:28:20 -04001015static int _mv88e6xxx_atu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot37705b72015-09-04 14:34:11 -04001016 struct mv88e6xxx_atu_entry *entry)
1017{
1018 u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK;
1019
1020 if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) {
1021 unsigned int mask, shift;
1022
1023 if (entry->trunk) {
1024 data |= GLOBAL_ATU_DATA_TRUNK;
1025 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
1026 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
1027 } else {
1028 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
1029 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
1030 }
1031
1032 data |= (entry->portv_trunkid << shift) & mask;
1033 }
1034
Vivien Didelotfad09c72016-06-21 12:28:20 -04001035 return _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_DATA, data);
Vivien Didelot37705b72015-09-04 14:34:11 -04001036}
1037
Vivien Didelotfad09c72016-06-21 12:28:20 -04001038static int _mv88e6xxx_atu_flush_move(struct mv88e6xxx_chip *chip,
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001039 struct mv88e6xxx_atu_entry *entry,
1040 bool static_too)
1041{
1042 int op;
1043 int err;
1044
Vivien Didelotfad09c72016-06-21 12:28:20 -04001045 err = _mv88e6xxx_atu_wait(chip);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001046 if (err)
1047 return err;
1048
Vivien Didelotfad09c72016-06-21 12:28:20 -04001049 err = _mv88e6xxx_atu_data_write(chip, entry);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001050 if (err)
1051 return err;
1052
1053 if (entry->fid) {
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001054 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB :
1055 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB;
1056 } else {
1057 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL :
1058 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC;
1059 }
1060
Vivien Didelotfad09c72016-06-21 12:28:20 -04001061 return _mv88e6xxx_atu_cmd(chip, entry->fid, op);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001062}
1063
Vivien Didelotfad09c72016-06-21 12:28:20 -04001064static int _mv88e6xxx_atu_flush(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001065 u16 fid, bool static_too)
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001066{
1067 struct mv88e6xxx_atu_entry entry = {
1068 .fid = fid,
1069 .state = 0, /* EntryState bits must be 0 */
1070 };
1071
Vivien Didelotfad09c72016-06-21 12:28:20 -04001072 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001073}
1074
Vivien Didelotfad09c72016-06-21 12:28:20 -04001075static int _mv88e6xxx_atu_move(struct mv88e6xxx_chip *chip, u16 fid,
Andrew Lunn158bc062016-04-28 21:24:06 -04001076 int from_port, int to_port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001077{
1078 struct mv88e6xxx_atu_entry entry = {
1079 .trunk = false,
1080 .fid = fid,
1081 };
1082
1083 /* EntryState bits must be 0xF */
1084 entry.state = GLOBAL_ATU_DATA_STATE_MASK;
1085
1086 /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */
1087 entry.portv_trunkid = (to_port & 0x0f) << 4;
1088 entry.portv_trunkid |= from_port & 0x0f;
1089
Vivien Didelotfad09c72016-06-21 12:28:20 -04001090 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001091}
1092
Vivien Didelotfad09c72016-06-21 12:28:20 -04001093static int _mv88e6xxx_atu_remove(struct mv88e6xxx_chip *chip, u16 fid,
Andrew Lunn158bc062016-04-28 21:24:06 -04001094 int port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001095{
1096 /* Destination port 0xF means remove the entries */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001097 return _mv88e6xxx_atu_move(chip, fid, port, 0x0f, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001098}
1099
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001100static const char * const mv88e6xxx_port_state_names[] = {
1101 [PORT_CONTROL_STATE_DISABLED] = "Disabled",
1102 [PORT_CONTROL_STATE_BLOCKING] = "Blocking/Listening",
1103 [PORT_CONTROL_STATE_LEARNING] = "Learning",
1104 [PORT_CONTROL_STATE_FORWARDING] = "Forwarding",
1105};
1106
Vivien Didelotfad09c72016-06-21 12:28:20 -04001107static int _mv88e6xxx_port_state(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001108 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001109{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001110 struct dsa_switch *ds = chip->ds;
Geert Uytterhoevenc3ffe6d2015-04-16 20:49:14 +02001111 int reg, ret = 0;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001112 u8 oldstate;
1113
Vivien Didelotfad09c72016-06-21 12:28:20 -04001114 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL);
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001115 if (reg < 0)
1116 return reg;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001117
Andrew Lunncca8b132015-04-02 04:06:39 +02001118 oldstate = reg & PORT_CONTROL_STATE_MASK;
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001119
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001120 if (oldstate != state) {
1121 /* Flush forwarding database if we're moving a port
1122 * from Learning or Forwarding state to Disabled or
1123 * Blocking or Listening state.
1124 */
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001125 if ((oldstate == PORT_CONTROL_STATE_LEARNING ||
Vivien Didelot57d32312016-06-20 13:13:58 -04001126 oldstate == PORT_CONTROL_STATE_FORWARDING) &&
1127 (state == PORT_CONTROL_STATE_DISABLED ||
1128 state == PORT_CONTROL_STATE_BLOCKING)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001129 ret = _mv88e6xxx_atu_remove(chip, 0, port, false);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001130 if (ret)
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001131 return ret;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001132 }
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001133
Andrew Lunncca8b132015-04-02 04:06:39 +02001134 reg = (reg & ~PORT_CONTROL_STATE_MASK) | state;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001135 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL,
Andrew Lunncca8b132015-04-02 04:06:39 +02001136 reg);
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001137 if (ret)
1138 return ret;
1139
Andrew Lunnc8b09802016-06-04 21:16:57 +02001140 netdev_dbg(ds->ports[port].netdev, "PortState %s (was %s)\n",
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001141 mv88e6xxx_port_state_names[state],
1142 mv88e6xxx_port_state_names[oldstate]);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001143 }
1144
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001145 return ret;
1146}
1147
Vivien Didelotfad09c72016-06-21 12:28:20 -04001148static int _mv88e6xxx_port_based_vlan_map(struct mv88e6xxx_chip *chip, int port)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001149{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001150 struct net_device *bridge = chip->ports[port].bridge_dev;
1151 const u16 mask = (1 << chip->info->num_ports) - 1;
1152 struct dsa_switch *ds = chip->ds;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001153 u16 output_ports = 0;
Vivien Didelotede80982015-10-11 18:08:35 -04001154 int reg;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001155 int i;
1156
1157 /* allow CPU port or DSA link(s) to send frames to every port */
1158 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
1159 output_ports = mask;
1160 } else {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001161 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001162 /* allow sending frames to every group member */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001163 if (bridge && chip->ports[i].bridge_dev == bridge)
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001164 output_ports |= BIT(i);
1165
1166 /* allow sending frames to CPU port and DSA link(s) */
1167 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
1168 output_ports |= BIT(i);
1169 }
1170 }
1171
1172 /* prevent frames from going back out of the port they came in on */
1173 output_ports &= ~BIT(port);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001174
Vivien Didelotfad09c72016-06-21 12:28:20 -04001175 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_BASE_VLAN);
Vivien Didelotede80982015-10-11 18:08:35 -04001176 if (reg < 0)
1177 return reg;
1178
1179 reg &= ~mask;
1180 reg |= output_ports & mask;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001181
Vivien Didelotfad09c72016-06-21 12:28:20 -04001182 return _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_BASE_VLAN, reg);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001183}
1184
Vivien Didelotf81ec902016-05-09 13:22:58 -04001185static void mv88e6xxx_port_stp_state_set(struct dsa_switch *ds, int port,
1186 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001187{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001188 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001189 int stp_state;
Vivien Didelot553eb542016-05-13 20:38:23 -04001190 int err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001191
1192 switch (state) {
1193 case BR_STATE_DISABLED:
Andrew Lunncca8b132015-04-02 04:06:39 +02001194 stp_state = PORT_CONTROL_STATE_DISABLED;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001195 break;
1196 case BR_STATE_BLOCKING:
1197 case BR_STATE_LISTENING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001198 stp_state = PORT_CONTROL_STATE_BLOCKING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001199 break;
1200 case BR_STATE_LEARNING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001201 stp_state = PORT_CONTROL_STATE_LEARNING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001202 break;
1203 case BR_STATE_FORWARDING:
1204 default:
Andrew Lunncca8b132015-04-02 04:06:39 +02001205 stp_state = PORT_CONTROL_STATE_FORWARDING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001206 break;
1207 }
1208
Vivien Didelotfad09c72016-06-21 12:28:20 -04001209 mutex_lock(&chip->reg_lock);
1210 err = _mv88e6xxx_port_state(chip, port, stp_state);
1211 mutex_unlock(&chip->reg_lock);
Vivien Didelot553eb542016-05-13 20:38:23 -04001212
1213 if (err)
Andrew Lunnc8b09802016-06-04 21:16:57 +02001214 netdev_err(ds->ports[port].netdev,
1215 "failed to update state to %s\n",
Vivien Didelot553eb542016-05-13 20:38:23 -04001216 mv88e6xxx_port_state_names[stp_state]);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001217}
1218
Vivien Didelotfad09c72016-06-21 12:28:20 -04001219static int _mv88e6xxx_port_pvid(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001220 u16 *new, u16 *old)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001221{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001222 struct dsa_switch *ds = chip->ds;
Vivien Didelot5da96032016-03-07 18:24:39 -05001223 u16 pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001224 int ret;
1225
Vivien Didelotfad09c72016-06-21 12:28:20 -04001226 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_DEFAULT_VLAN);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001227 if (ret < 0)
1228 return ret;
1229
Vivien Didelot5da96032016-03-07 18:24:39 -05001230 pvid = ret & PORT_DEFAULT_VLAN_MASK;
1231
1232 if (new) {
1233 ret &= ~PORT_DEFAULT_VLAN_MASK;
1234 ret |= *new & PORT_DEFAULT_VLAN_MASK;
1235
Vivien Didelotfad09c72016-06-21 12:28:20 -04001236 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Vivien Didelot5da96032016-03-07 18:24:39 -05001237 PORT_DEFAULT_VLAN, ret);
1238 if (ret < 0)
1239 return ret;
1240
Andrew Lunnc8b09802016-06-04 21:16:57 +02001241 netdev_dbg(ds->ports[port].netdev,
1242 "DefaultVID %d (was %d)\n", *new, pvid);
Vivien Didelot5da96032016-03-07 18:24:39 -05001243 }
1244
1245 if (old)
1246 *old = pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001247
1248 return 0;
1249}
1250
Vivien Didelotfad09c72016-06-21 12:28:20 -04001251static int _mv88e6xxx_port_pvid_get(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001252 int port, u16 *pvid)
Vivien Didelot5da96032016-03-07 18:24:39 -05001253{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001254 return _mv88e6xxx_port_pvid(chip, port, NULL, pvid);
Vivien Didelot5da96032016-03-07 18:24:39 -05001255}
1256
Vivien Didelotfad09c72016-06-21 12:28:20 -04001257static int _mv88e6xxx_port_pvid_set(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001258 int port, u16 pvid)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001259{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001260 return _mv88e6xxx_port_pvid(chip, port, &pvid, NULL);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001261}
1262
Vivien Didelotfad09c72016-06-21 12:28:20 -04001263static int _mv88e6xxx_vtu_wait(struct mv88e6xxx_chip *chip)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001264{
Vivien Didelot2d79af62016-08-15 17:18:57 -04001265 return mv88e6xxx_wait(chip, REG_GLOBAL, GLOBAL_VTU_OP,
1266 GLOBAL_VTU_OP_BUSY);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001267}
1268
Vivien Didelotfad09c72016-06-21 12:28:20 -04001269static int _mv88e6xxx_vtu_cmd(struct mv88e6xxx_chip *chip, u16 op)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001270{
1271 int ret;
1272
Vivien Didelotfad09c72016-06-21 12:28:20 -04001273 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_OP, op);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001274 if (ret < 0)
1275 return ret;
1276
Vivien Didelotfad09c72016-06-21 12:28:20 -04001277 return _mv88e6xxx_vtu_wait(chip);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001278}
1279
Vivien Didelotfad09c72016-06-21 12:28:20 -04001280static int _mv88e6xxx_vtu_stu_flush(struct mv88e6xxx_chip *chip)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001281{
1282 int ret;
1283
Vivien Didelotfad09c72016-06-21 12:28:20 -04001284 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001285 if (ret < 0)
1286 return ret;
1287
Vivien Didelotfad09c72016-06-21 12:28:20 -04001288 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_FLUSH_ALL);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001289}
1290
Vivien Didelotfad09c72016-06-21 12:28:20 -04001291static int _mv88e6xxx_vtu_stu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001292 struct mv88e6xxx_vtu_stu_entry *entry,
1293 unsigned int nibble_offset)
1294{
Vivien Didelotb8fee952015-08-13 12:52:19 -04001295 u16 regs[3];
1296 int i;
1297 int ret;
1298
1299 for (i = 0; i < 3; ++i) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001300 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001301 GLOBAL_VTU_DATA_0_3 + i);
1302 if (ret < 0)
1303 return ret;
1304
1305 regs[i] = ret;
1306 }
1307
Vivien Didelotfad09c72016-06-21 12:28:20 -04001308 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelotb8fee952015-08-13 12:52:19 -04001309 unsigned int shift = (i % 4) * 4 + nibble_offset;
1310 u16 reg = regs[i / 4];
1311
1312 entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
1313 }
1314
1315 return 0;
1316}
1317
Vivien Didelotfad09c72016-06-21 12:28:20 -04001318static int mv88e6xxx_vtu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001319 struct mv88e6xxx_vtu_stu_entry *entry)
1320{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001321 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 0);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001322}
1323
Vivien Didelotfad09c72016-06-21 12:28:20 -04001324static int mv88e6xxx_stu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001325 struct mv88e6xxx_vtu_stu_entry *entry)
1326{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001327 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 2);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001328}
1329
Vivien Didelotfad09c72016-06-21 12:28:20 -04001330static int _mv88e6xxx_vtu_stu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001331 struct mv88e6xxx_vtu_stu_entry *entry,
1332 unsigned int nibble_offset)
1333{
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001334 u16 regs[3] = { 0 };
1335 int i;
1336 int ret;
1337
Vivien Didelotfad09c72016-06-21 12:28:20 -04001338 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001339 unsigned int shift = (i % 4) * 4 + nibble_offset;
1340 u8 data = entry->data[i];
1341
1342 regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
1343 }
1344
1345 for (i = 0; i < 3; ++i) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001346 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001347 GLOBAL_VTU_DATA_0_3 + i, regs[i]);
1348 if (ret < 0)
1349 return ret;
1350 }
1351
1352 return 0;
1353}
1354
Vivien Didelotfad09c72016-06-21 12:28:20 -04001355static int mv88e6xxx_vtu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001356 struct mv88e6xxx_vtu_stu_entry *entry)
1357{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001358 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 0);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001359}
1360
Vivien Didelotfad09c72016-06-21 12:28:20 -04001361static int mv88e6xxx_stu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001362 struct mv88e6xxx_vtu_stu_entry *entry)
1363{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001364 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 2);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001365}
1366
Vivien Didelotfad09c72016-06-21 12:28:20 -04001367static int _mv88e6xxx_vtu_vid_write(struct mv88e6xxx_chip *chip, u16 vid)
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001368{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001369 return _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID,
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001370 vid & GLOBAL_VTU_VID_MASK);
1371}
1372
Vivien Didelotfad09c72016-06-21 12:28:20 -04001373static int _mv88e6xxx_vtu_getnext(struct mv88e6xxx_chip *chip,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001374 struct mv88e6xxx_vtu_stu_entry *entry)
1375{
1376 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1377 int ret;
1378
Vivien Didelotfad09c72016-06-21 12:28:20 -04001379 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001380 if (ret < 0)
1381 return ret;
1382
Vivien Didelotfad09c72016-06-21 12:28:20 -04001383 ret = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_VTU_GET_NEXT);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001384 if (ret < 0)
1385 return ret;
1386
Vivien Didelotfad09c72016-06-21 12:28:20 -04001387 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_VID);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001388 if (ret < 0)
1389 return ret;
1390
1391 next.vid = ret & GLOBAL_VTU_VID_MASK;
1392 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1393
1394 if (next.valid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001395 ret = mv88e6xxx_vtu_data_read(chip, &next);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001396 if (ret < 0)
1397 return ret;
1398
Vivien Didelotfad09c72016-06-21 12:28:20 -04001399 if (mv88e6xxx_has_fid_reg(chip)) {
1400 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001401 GLOBAL_VTU_FID);
1402 if (ret < 0)
1403 return ret;
1404
1405 next.fid = ret & GLOBAL_VTU_FID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001406 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001407 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1408 * VTU DBNum[3:0] are located in VTU Operation 3:0
1409 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001410 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
Vivien Didelot11ea8092016-03-31 16:53:44 -04001411 GLOBAL_VTU_OP);
1412 if (ret < 0)
1413 return ret;
1414
1415 next.fid = (ret & 0xf00) >> 4;
1416 next.fid |= ret & 0xf;
Vivien Didelot2e7bd5e2016-03-31 16:53:41 -04001417 }
Vivien Didelotb8fee952015-08-13 12:52:19 -04001418
Vivien Didelotfad09c72016-06-21 12:28:20 -04001419 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
1420 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001421 GLOBAL_VTU_SID);
1422 if (ret < 0)
1423 return ret;
1424
1425 next.sid = ret & GLOBAL_VTU_SID_MASK;
1426 }
1427 }
1428
1429 *entry = next;
1430 return 0;
1431}
1432
Vivien Didelotf81ec902016-05-09 13:22:58 -04001433static int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
1434 struct switchdev_obj_port_vlan *vlan,
1435 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001436{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001437 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001438 struct mv88e6xxx_vtu_stu_entry next;
1439 u16 pvid;
1440 int err;
1441
Vivien Didelotfad09c72016-06-21 12:28:20 -04001442 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001443 return -EOPNOTSUPP;
1444
Vivien Didelotfad09c72016-06-21 12:28:20 -04001445 mutex_lock(&chip->reg_lock);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001446
Vivien Didelotfad09c72016-06-21 12:28:20 -04001447 err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001448 if (err)
1449 goto unlock;
1450
Vivien Didelotfad09c72016-06-21 12:28:20 -04001451 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001452 if (err)
1453 goto unlock;
1454
1455 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001456 err = _mv88e6xxx_vtu_getnext(chip, &next);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001457 if (err)
1458 break;
1459
1460 if (!next.valid)
1461 break;
1462
1463 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1464 continue;
1465
1466 /* reinit and dump this VLAN obj */
Vivien Didelot57d32312016-06-20 13:13:58 -04001467 vlan->vid_begin = next.vid;
1468 vlan->vid_end = next.vid;
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001469 vlan->flags = 0;
1470
1471 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1472 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1473
1474 if (next.vid == pvid)
1475 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1476
1477 err = cb(&vlan->obj);
1478 if (err)
1479 break;
1480 } while (next.vid < GLOBAL_VTU_VID_MASK);
1481
1482unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001483 mutex_unlock(&chip->reg_lock);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001484
1485 return err;
1486}
1487
Vivien Didelotfad09c72016-06-21 12:28:20 -04001488static int _mv88e6xxx_vtu_loadpurge(struct mv88e6xxx_chip *chip,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001489 struct mv88e6xxx_vtu_stu_entry *entry)
1490{
Vivien Didelot11ea8092016-03-31 16:53:44 -04001491 u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001492 u16 reg = 0;
1493 int ret;
1494
Vivien Didelotfad09c72016-06-21 12:28:20 -04001495 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001496 if (ret < 0)
1497 return ret;
1498
1499 if (!entry->valid)
1500 goto loadpurge;
1501
1502 /* Write port member tags */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001503 ret = mv88e6xxx_vtu_data_write(chip, entry);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001504 if (ret < 0)
1505 return ret;
1506
Vivien Didelotfad09c72016-06-21 12:28:20 -04001507 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001508 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001509 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID,
1510 reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001511 if (ret < 0)
1512 return ret;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001513 }
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001514
Vivien Didelotfad09c72016-06-21 12:28:20 -04001515 if (mv88e6xxx_has_fid_reg(chip)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001516 reg = entry->fid & GLOBAL_VTU_FID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001517 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_FID,
1518 reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001519 if (ret < 0)
1520 return ret;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001521 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001522 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1523 * VTU DBNum[3:0] are located in VTU Operation 3:0
1524 */
1525 op |= (entry->fid & 0xf0) << 8;
1526 op |= entry->fid & 0xf;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001527 }
1528
1529 reg = GLOBAL_VTU_VID_VALID;
1530loadpurge:
1531 reg |= entry->vid & GLOBAL_VTU_VID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001532 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID, reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001533 if (ret < 0)
1534 return ret;
1535
Vivien Didelotfad09c72016-06-21 12:28:20 -04001536 return _mv88e6xxx_vtu_cmd(chip, op);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001537}
1538
Vivien Didelotfad09c72016-06-21 12:28:20 -04001539static int _mv88e6xxx_stu_getnext(struct mv88e6xxx_chip *chip, u8 sid,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001540 struct mv88e6xxx_vtu_stu_entry *entry)
1541{
1542 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1543 int ret;
1544
Vivien Didelotfad09c72016-06-21 12:28:20 -04001545 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001546 if (ret < 0)
1547 return ret;
1548
Vivien Didelotfad09c72016-06-21 12:28:20 -04001549 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001550 sid & GLOBAL_VTU_SID_MASK);
1551 if (ret < 0)
1552 return ret;
1553
Vivien Didelotfad09c72016-06-21 12:28:20 -04001554 ret = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_GET_NEXT);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001555 if (ret < 0)
1556 return ret;
1557
Vivien Didelotfad09c72016-06-21 12:28:20 -04001558 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_SID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001559 if (ret < 0)
1560 return ret;
1561
1562 next.sid = ret & GLOBAL_VTU_SID_MASK;
1563
Vivien Didelotfad09c72016-06-21 12:28:20 -04001564 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_VID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001565 if (ret < 0)
1566 return ret;
1567
1568 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1569
1570 if (next.valid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001571 ret = mv88e6xxx_stu_data_read(chip, &next);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001572 if (ret < 0)
1573 return ret;
1574 }
1575
1576 *entry = next;
1577 return 0;
1578}
1579
Vivien Didelotfad09c72016-06-21 12:28:20 -04001580static int _mv88e6xxx_stu_loadpurge(struct mv88e6xxx_chip *chip,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001581 struct mv88e6xxx_vtu_stu_entry *entry)
1582{
1583 u16 reg = 0;
1584 int ret;
1585
Vivien Didelotfad09c72016-06-21 12:28:20 -04001586 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001587 if (ret < 0)
1588 return ret;
1589
1590 if (!entry->valid)
1591 goto loadpurge;
1592
1593 /* Write port states */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001594 ret = mv88e6xxx_stu_data_write(chip, entry);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001595 if (ret < 0)
1596 return ret;
1597
1598 reg = GLOBAL_VTU_VID_VALID;
1599loadpurge:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001600 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID, reg);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001601 if (ret < 0)
1602 return ret;
1603
1604 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001605 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID, reg);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001606 if (ret < 0)
1607 return ret;
1608
Vivien Didelotfad09c72016-06-21 12:28:20 -04001609 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_LOAD_PURGE);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001610}
1611
Vivien Didelotfad09c72016-06-21 12:28:20 -04001612static int _mv88e6xxx_port_fid(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001613 u16 *new, u16 *old)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001614{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001615 struct dsa_switch *ds = chip->ds;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001616 u16 upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001617 u16 fid;
1618 int ret;
1619
Vivien Didelotfad09c72016-06-21 12:28:20 -04001620 if (mv88e6xxx_num_databases(chip) == 4096)
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001621 upper_mask = 0xff;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001622 else if (mv88e6xxx_num_databases(chip) == 256)
Vivien Didelot11ea8092016-03-31 16:53:44 -04001623 upper_mask = 0xf;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001624 else
1625 return -EOPNOTSUPP;
1626
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001627 /* Port's default FID bits 3:0 are located in reg 0x06, offset 12 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001628 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_BASE_VLAN);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001629 if (ret < 0)
1630 return ret;
1631
1632 fid = (ret & PORT_BASE_VLAN_FID_3_0_MASK) >> 12;
1633
1634 if (new) {
1635 ret &= ~PORT_BASE_VLAN_FID_3_0_MASK;
1636 ret |= (*new << 12) & PORT_BASE_VLAN_FID_3_0_MASK;
1637
Vivien Didelotfad09c72016-06-21 12:28:20 -04001638 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_BASE_VLAN,
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001639 ret);
1640 if (ret < 0)
1641 return ret;
1642 }
1643
1644 /* Port's default FID bits 11:4 are located in reg 0x05, offset 0 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001645 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL_1);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001646 if (ret < 0)
1647 return ret;
1648
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001649 fid |= (ret & upper_mask) << 4;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001650
1651 if (new) {
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001652 ret &= ~upper_mask;
1653 ret |= (*new >> 4) & upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001654
Vivien Didelotfad09c72016-06-21 12:28:20 -04001655 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_1,
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001656 ret);
1657 if (ret < 0)
1658 return ret;
1659
Andrew Lunnc8b09802016-06-04 21:16:57 +02001660 netdev_dbg(ds->ports[port].netdev,
1661 "FID %d (was %d)\n", *new, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001662 }
1663
1664 if (old)
1665 *old = fid;
1666
1667 return 0;
1668}
1669
Vivien Didelotfad09c72016-06-21 12:28:20 -04001670static int _mv88e6xxx_port_fid_get(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001671 int port, u16 *fid)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001672{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001673 return _mv88e6xxx_port_fid(chip, port, NULL, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001674}
1675
Vivien Didelotfad09c72016-06-21 12:28:20 -04001676static int _mv88e6xxx_port_fid_set(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001677 int port, u16 fid)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001678{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001679 return _mv88e6xxx_port_fid(chip, port, &fid, NULL);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001680}
1681
Vivien Didelotfad09c72016-06-21 12:28:20 -04001682static int _mv88e6xxx_fid_new(struct mv88e6xxx_chip *chip, u16 *fid)
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001683{
1684 DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
1685 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001686 int i, err;
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001687
1688 bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);
1689
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001690 /* Set every FID bit used by the (un)bridged ports */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001691 for (i = 0; i < chip->info->num_ports; ++i) {
1692 err = _mv88e6xxx_port_fid_get(chip, i, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001693 if (err)
1694 return err;
1695
1696 set_bit(*fid, fid_bitmap);
1697 }
1698
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001699 /* Set every FID bit used by the VLAN entries */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001700 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001701 if (err)
1702 return err;
1703
1704 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001705 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001706 if (err)
1707 return err;
1708
1709 if (!vlan.valid)
1710 break;
1711
1712 set_bit(vlan.fid, fid_bitmap);
1713 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
1714
1715 /* The reset value 0x000 is used to indicate that multiple address
1716 * databases are not needed. Return the next positive available.
1717 */
1718 *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1);
Vivien Didelotfad09c72016-06-21 12:28:20 -04001719 if (unlikely(*fid >= mv88e6xxx_num_databases(chip)))
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001720 return -ENOSPC;
1721
1722 /* Clear the database */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001723 return _mv88e6xxx_atu_flush(chip, *fid, true);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001724}
1725
Vivien Didelotfad09c72016-06-21 12:28:20 -04001726static int _mv88e6xxx_vtu_new(struct mv88e6xxx_chip *chip, u16 vid,
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001727 struct mv88e6xxx_vtu_stu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001728{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001729 struct dsa_switch *ds = chip->ds;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001730 struct mv88e6xxx_vtu_stu_entry vlan = {
1731 .valid = true,
1732 .vid = vid,
1733 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001734 int i, err;
1735
Vivien Didelotfad09c72016-06-21 12:28:20 -04001736 err = _mv88e6xxx_fid_new(chip, &vlan.fid);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001737 if (err)
1738 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001739
Vivien Didelot3d131f02015-11-03 10:52:52 -05001740 /* exclude all ports except the CPU and DSA ports */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001741 for (i = 0; i < chip->info->num_ports; ++i)
Vivien Didelot3d131f02015-11-03 10:52:52 -05001742 vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)
1743 ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED
1744 : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001745
Vivien Didelotfad09c72016-06-21 12:28:20 -04001746 if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
1747 mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip)) {
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001748 struct mv88e6xxx_vtu_stu_entry vstp;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001749
1750 /* Adding a VTU entry requires a valid STU entry. As VSTP is not
1751 * implemented, only one STU entry is needed to cover all VTU
1752 * entries. Thus, validate the SID 0.
1753 */
1754 vlan.sid = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001755 err = _mv88e6xxx_stu_getnext(chip, GLOBAL_VTU_SID_MASK, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001756 if (err)
1757 return err;
1758
1759 if (vstp.sid != vlan.sid || !vstp.valid) {
1760 memset(&vstp, 0, sizeof(vstp));
1761 vstp.valid = true;
1762 vstp.sid = vlan.sid;
1763
Vivien Didelotfad09c72016-06-21 12:28:20 -04001764 err = _mv88e6xxx_stu_loadpurge(chip, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001765 if (err)
1766 return err;
1767 }
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001768 }
1769
1770 *entry = vlan;
1771 return 0;
1772}
1773
Vivien Didelotfad09c72016-06-21 12:28:20 -04001774static int _mv88e6xxx_vtu_get(struct mv88e6xxx_chip *chip, u16 vid,
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001775 struct mv88e6xxx_vtu_stu_entry *entry, bool creat)
1776{
1777 int err;
1778
1779 if (!vid)
1780 return -EINVAL;
1781
Vivien Didelotfad09c72016-06-21 12:28:20 -04001782 err = _mv88e6xxx_vtu_vid_write(chip, vid - 1);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001783 if (err)
1784 return err;
1785
Vivien Didelotfad09c72016-06-21 12:28:20 -04001786 err = _mv88e6xxx_vtu_getnext(chip, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001787 if (err)
1788 return err;
1789
1790 if (entry->vid != vid || !entry->valid) {
1791 if (!creat)
1792 return -EOPNOTSUPP;
1793 /* -ENOENT would've been more appropriate, but switchdev expects
1794 * -EOPNOTSUPP to inform bridge about an eventual software VLAN.
1795 */
1796
Vivien Didelotfad09c72016-06-21 12:28:20 -04001797 err = _mv88e6xxx_vtu_new(chip, vid, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001798 }
1799
1800 return err;
1801}
1802
Vivien Didelotda9c3592016-02-12 12:09:40 -05001803static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
1804 u16 vid_begin, u16 vid_end)
1805{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001806 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001807 struct mv88e6xxx_vtu_stu_entry vlan;
1808 int i, err;
1809
1810 if (!vid_begin)
1811 return -EOPNOTSUPP;
1812
Vivien Didelotfad09c72016-06-21 12:28:20 -04001813 mutex_lock(&chip->reg_lock);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001814
Vivien Didelotfad09c72016-06-21 12:28:20 -04001815 err = _mv88e6xxx_vtu_vid_write(chip, vid_begin - 1);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001816 if (err)
1817 goto unlock;
1818
1819 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001820 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001821 if (err)
1822 goto unlock;
1823
1824 if (!vlan.valid)
1825 break;
1826
1827 if (vlan.vid > vid_end)
1828 break;
1829
Vivien Didelotfad09c72016-06-21 12:28:20 -04001830 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelotda9c3592016-02-12 12:09:40 -05001831 if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i))
1832 continue;
1833
1834 if (vlan.data[i] ==
1835 GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1836 continue;
1837
Vivien Didelotfad09c72016-06-21 12:28:20 -04001838 if (chip->ports[i].bridge_dev ==
1839 chip->ports[port].bridge_dev)
Vivien Didelotda9c3592016-02-12 12:09:40 -05001840 break; /* same bridge, check next VLAN */
1841
Andrew Lunnc8b09802016-06-04 21:16:57 +02001842 netdev_warn(ds->ports[port].netdev,
Vivien Didelotda9c3592016-02-12 12:09:40 -05001843 "hardware VLAN %d already used by %s\n",
1844 vlan.vid,
Vivien Didelotfad09c72016-06-21 12:28:20 -04001845 netdev_name(chip->ports[i].bridge_dev));
Vivien Didelotda9c3592016-02-12 12:09:40 -05001846 err = -EOPNOTSUPP;
1847 goto unlock;
1848 }
1849 } while (vlan.vid < vid_end);
1850
1851unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001852 mutex_unlock(&chip->reg_lock);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001853
1854 return err;
1855}
1856
Vivien Didelot214cdb92016-02-26 13:16:08 -05001857static const char * const mv88e6xxx_port_8021q_mode_names[] = {
1858 [PORT_CONTROL_2_8021Q_DISABLED] = "Disabled",
1859 [PORT_CONTROL_2_8021Q_FALLBACK] = "Fallback",
1860 [PORT_CONTROL_2_8021Q_CHECK] = "Check",
1861 [PORT_CONTROL_2_8021Q_SECURE] = "Secure",
1862};
1863
Vivien Didelotf81ec902016-05-09 13:22:58 -04001864static int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port,
1865 bool vlan_filtering)
Vivien Didelot214cdb92016-02-26 13:16:08 -05001866{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001867 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelot214cdb92016-02-26 13:16:08 -05001868 u16 old, new = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE :
1869 PORT_CONTROL_2_8021Q_DISABLED;
1870 int ret;
1871
Vivien Didelotfad09c72016-06-21 12:28:20 -04001872 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001873 return -EOPNOTSUPP;
1874
Vivien Didelotfad09c72016-06-21 12:28:20 -04001875 mutex_lock(&chip->reg_lock);
Vivien Didelot214cdb92016-02-26 13:16:08 -05001876
Vivien Didelotfad09c72016-06-21 12:28:20 -04001877 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL_2);
Vivien Didelot214cdb92016-02-26 13:16:08 -05001878 if (ret < 0)
1879 goto unlock;
1880
1881 old = ret & PORT_CONTROL_2_8021Q_MASK;
1882
Vivien Didelot5220ef12016-03-07 18:24:52 -05001883 if (new != old) {
1884 ret &= ~PORT_CONTROL_2_8021Q_MASK;
1885 ret |= new & PORT_CONTROL_2_8021Q_MASK;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001886
Vivien Didelotfad09c72016-06-21 12:28:20 -04001887 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_2,
Vivien Didelot5220ef12016-03-07 18:24:52 -05001888 ret);
1889 if (ret < 0)
1890 goto unlock;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001891
Andrew Lunnc8b09802016-06-04 21:16:57 +02001892 netdev_dbg(ds->ports[port].netdev, "802.1Q Mode %s (was %s)\n",
Vivien Didelot5220ef12016-03-07 18:24:52 -05001893 mv88e6xxx_port_8021q_mode_names[new],
1894 mv88e6xxx_port_8021q_mode_names[old]);
1895 }
1896
1897 ret = 0;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001898unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001899 mutex_unlock(&chip->reg_lock);
Vivien Didelot214cdb92016-02-26 13:16:08 -05001900
1901 return ret;
1902}
1903
Vivien Didelot57d32312016-06-20 13:13:58 -04001904static int
1905mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
1906 const struct switchdev_obj_port_vlan *vlan,
1907 struct switchdev_trans *trans)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001908{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001909 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001910 int err;
1911
Vivien Didelotfad09c72016-06-21 12:28:20 -04001912 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001913 return -EOPNOTSUPP;
1914
Vivien Didelotda9c3592016-02-12 12:09:40 -05001915 /* If the requested port doesn't belong to the same bridge as the VLAN
1916 * members, do not support it (yet) and fallback to software VLAN.
1917 */
1918 err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin,
1919 vlan->vid_end);
1920 if (err)
1921 return err;
1922
Vivien Didelot76e398a2015-11-01 12:33:55 -05001923 /* We don't need any dynamic resource from the kernel (yet),
1924 * so skip the prepare phase.
1925 */
1926 return 0;
1927}
1928
Vivien Didelotfad09c72016-06-21 12:28:20 -04001929static int _mv88e6xxx_port_vlan_add(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001930 u16 vid, bool untagged)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001931{
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001932 struct mv88e6xxx_vtu_stu_entry vlan;
1933 int err;
1934
Vivien Didelotfad09c72016-06-21 12:28:20 -04001935 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, true);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001936 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001937 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001938
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001939 vlan.data[port] = untagged ?
1940 GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
1941 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
1942
Vivien Didelotfad09c72016-06-21 12:28:20 -04001943 return _mv88e6xxx_vtu_loadpurge(chip, &vlan);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001944}
1945
Vivien Didelotf81ec902016-05-09 13:22:58 -04001946static void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
1947 const struct switchdev_obj_port_vlan *vlan,
1948 struct switchdev_trans *trans)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001949{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001950 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001951 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1952 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1953 u16 vid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001954
Vivien Didelotfad09c72016-06-21 12:28:20 -04001955 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001956 return;
1957
Vivien Didelotfad09c72016-06-21 12:28:20 -04001958 mutex_lock(&chip->reg_lock);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001959
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001960 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
Vivien Didelotfad09c72016-06-21 12:28:20 -04001961 if (_mv88e6xxx_port_vlan_add(chip, port, vid, untagged))
Andrew Lunnc8b09802016-06-04 21:16:57 +02001962 netdev_err(ds->ports[port].netdev,
1963 "failed to add VLAN %d%c\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001964 vid, untagged ? 'u' : 't');
Vivien Didelot76e398a2015-11-01 12:33:55 -05001965
Vivien Didelotfad09c72016-06-21 12:28:20 -04001966 if (pvid && _mv88e6xxx_port_pvid_set(chip, port, vlan->vid_end))
Andrew Lunnc8b09802016-06-04 21:16:57 +02001967 netdev_err(ds->ports[port].netdev, "failed to set PVID %d\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001968 vlan->vid_end);
1969
Vivien Didelotfad09c72016-06-21 12:28:20 -04001970 mutex_unlock(&chip->reg_lock);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001971}
1972
Vivien Didelotfad09c72016-06-21 12:28:20 -04001973static int _mv88e6xxx_port_vlan_del(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001974 int port, u16 vid)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001975{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001976 struct dsa_switch *ds = chip->ds;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001977 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001978 int i, err;
1979
Vivien Didelotfad09c72016-06-21 12:28:20 -04001980 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001981 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001982 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001983
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001984 /* Tell switchdev if this VLAN is handled in software */
1985 if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
Vivien Didelot3c06f082016-02-05 14:04:39 -05001986 return -EOPNOTSUPP;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001987
1988 vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
1989
1990 /* keep the VLAN unless all ports are excluded */
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001991 vlan.valid = false;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001992 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelot3d131f02015-11-03 10:52:52 -05001993 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001994 continue;
1995
1996 if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001997 vlan.valid = true;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001998 break;
1999 }
2000 }
2001
Vivien Didelotfad09c72016-06-21 12:28:20 -04002002 err = _mv88e6xxx_vtu_loadpurge(chip, &vlan);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002003 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002004 return err;
2005
Vivien Didelotfad09c72016-06-21 12:28:20 -04002006 return _mv88e6xxx_atu_remove(chip, vlan.fid, port, false);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002007}
2008
Vivien Didelotf81ec902016-05-09 13:22:58 -04002009static int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
2010 const struct switchdev_obj_port_vlan *vlan)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002011{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002012 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002013 u16 pvid, vid;
2014 int err = 0;
2015
Vivien Didelotfad09c72016-06-21 12:28:20 -04002016 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04002017 return -EOPNOTSUPP;
2018
Vivien Didelotfad09c72016-06-21 12:28:20 -04002019 mutex_lock(&chip->reg_lock);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002020
Vivien Didelotfad09c72016-06-21 12:28:20 -04002021 err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002022 if (err)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002023 goto unlock;
2024
Vivien Didelot76e398a2015-11-01 12:33:55 -05002025 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002026 err = _mv88e6xxx_port_vlan_del(chip, port, vid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002027 if (err)
2028 goto unlock;
2029
2030 if (vid == pvid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002031 err = _mv88e6xxx_port_pvid_set(chip, port, 0);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002032 if (err)
2033 goto unlock;
2034 }
2035 }
2036
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002037unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002038 mutex_unlock(&chip->reg_lock);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002039
2040 return err;
2041}
2042
Vivien Didelotfad09c72016-06-21 12:28:20 -04002043static int _mv88e6xxx_atu_mac_write(struct mv88e6xxx_chip *chip,
Vivien Didelotc5723ac2015-08-10 09:09:48 -04002044 const unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002045{
2046 int i, ret;
2047
2048 for (i = 0; i < 3; i++) {
Andrew Lunncca8b132015-04-02 04:06:39 +02002049 ret = _mv88e6xxx_reg_write(
Vivien Didelotfad09c72016-06-21 12:28:20 -04002050 chip, REG_GLOBAL, GLOBAL_ATU_MAC_01 + i,
Andrew Lunncca8b132015-04-02 04:06:39 +02002051 (addr[i * 2] << 8) | addr[i * 2 + 1]);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002052 if (ret < 0)
2053 return ret;
2054 }
2055
2056 return 0;
2057}
2058
Vivien Didelotfad09c72016-06-21 12:28:20 -04002059static int _mv88e6xxx_atu_mac_read(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04002060 unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002061{
2062 int i, ret;
2063
2064 for (i = 0; i < 3; i++) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002065 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
Andrew Lunncca8b132015-04-02 04:06:39 +02002066 GLOBAL_ATU_MAC_01 + i);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002067 if (ret < 0)
2068 return ret;
2069 addr[i * 2] = ret >> 8;
2070 addr[i * 2 + 1] = ret & 0xff;
2071 }
2072
2073 return 0;
2074}
2075
Vivien Didelotfad09c72016-06-21 12:28:20 -04002076static int _mv88e6xxx_atu_load(struct mv88e6xxx_chip *chip,
Vivien Didelotfd231c82015-08-10 09:09:50 -04002077 struct mv88e6xxx_atu_entry *entry)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002078{
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002079 int ret;
2080
Vivien Didelotfad09c72016-06-21 12:28:20 -04002081 ret = _mv88e6xxx_atu_wait(chip);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002082 if (ret < 0)
2083 return ret;
2084
Vivien Didelotfad09c72016-06-21 12:28:20 -04002085 ret = _mv88e6xxx_atu_mac_write(chip, entry->mac);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002086 if (ret < 0)
2087 return ret;
2088
Vivien Didelotfad09c72016-06-21 12:28:20 -04002089 ret = _mv88e6xxx_atu_data_write(chip, entry);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002090 if (ret < 0)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002091 return ret;
2092
Vivien Didelotfad09c72016-06-21 12:28:20 -04002093 return _mv88e6xxx_atu_cmd(chip, entry->fid, GLOBAL_ATU_OP_LOAD_DB);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002094}
David S. Millercdf09692015-08-11 12:00:37 -07002095
Vivien Didelotfad09c72016-06-21 12:28:20 -04002096static int _mv88e6xxx_port_fdb_load(struct mv88e6xxx_chip *chip, int port,
Vivien Didelotfd231c82015-08-10 09:09:50 -04002097 const unsigned char *addr, u16 vid,
2098 u8 state)
2099{
2100 struct mv88e6xxx_atu_entry entry = { 0 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002101 struct mv88e6xxx_vtu_stu_entry vlan;
2102 int err;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002103
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002104 /* Null VLAN ID corresponds to the port private database */
2105 if (vid == 0)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002106 err = _mv88e6xxx_port_fid_get(chip, port, &vlan.fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002107 else
Vivien Didelotfad09c72016-06-21 12:28:20 -04002108 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002109 if (err)
2110 return err;
2111
2112 entry.fid = vlan.fid;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002113 entry.state = state;
2114 ether_addr_copy(entry.mac, addr);
2115 if (state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2116 entry.trunk = false;
2117 entry.portv_trunkid = BIT(port);
2118 }
2119
Vivien Didelotfad09c72016-06-21 12:28:20 -04002120 return _mv88e6xxx_atu_load(chip, &entry);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002121}
2122
Vivien Didelotf81ec902016-05-09 13:22:58 -04002123static int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
2124 const struct switchdev_obj_port_fdb *fdb,
2125 struct switchdev_trans *trans)
Vivien Didelot146a3202015-10-08 11:35:12 -04002126{
2127 /* We don't need any dynamic resource from the kernel (yet),
2128 * so skip the prepare phase.
2129 */
2130 return 0;
2131}
2132
Vivien Didelotf81ec902016-05-09 13:22:58 -04002133static void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
2134 const struct switchdev_obj_port_fdb *fdb,
2135 struct switchdev_trans *trans)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002136{
Vivien Didelot1f36faf2015-10-08 11:35:13 -04002137 int state = is_multicast_ether_addr(fdb->addr) ?
David S. Millercdf09692015-08-11 12:00:37 -07002138 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2139 GLOBAL_ATU_DATA_STATE_UC_STATIC;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002140 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelot6630e232015-08-06 01:44:07 -04002141
Vivien Didelotfad09c72016-06-21 12:28:20 -04002142 mutex_lock(&chip->reg_lock);
2143 if (_mv88e6xxx_port_fdb_load(chip, port, fdb->addr, fdb->vid, state))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002144 netdev_err(ds->ports[port].netdev,
2145 "failed to load MAC address\n");
Vivien Didelotfad09c72016-06-21 12:28:20 -04002146 mutex_unlock(&chip->reg_lock);
David S. Millercdf09692015-08-11 12:00:37 -07002147}
2148
Vivien Didelotf81ec902016-05-09 13:22:58 -04002149static int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
2150 const struct switchdev_obj_port_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -07002151{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002152 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
David S. Millercdf09692015-08-11 12:00:37 -07002153 int ret;
2154
Vivien Didelotfad09c72016-06-21 12:28:20 -04002155 mutex_lock(&chip->reg_lock);
2156 ret = _mv88e6xxx_port_fdb_load(chip, port, fdb->addr, fdb->vid,
David S. Millercdf09692015-08-11 12:00:37 -07002157 GLOBAL_ATU_DATA_STATE_UNUSED);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002158 mutex_unlock(&chip->reg_lock);
David S. Millercdf09692015-08-11 12:00:37 -07002159
2160 return ret;
2161}
2162
Vivien Didelotfad09c72016-06-21 12:28:20 -04002163static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
Vivien Didelot1d194042015-08-10 09:09:51 -04002164 struct mv88e6xxx_atu_entry *entry)
David S. Millercdf09692015-08-11 12:00:37 -07002165{
Vivien Didelot1d194042015-08-10 09:09:51 -04002166 struct mv88e6xxx_atu_entry next = { 0 };
2167 int ret;
2168
2169 next.fid = fid;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002170
Vivien Didelotfad09c72016-06-21 12:28:20 -04002171 ret = _mv88e6xxx_atu_wait(chip);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002172 if (ret < 0)
2173 return ret;
2174
Vivien Didelotfad09c72016-06-21 12:28:20 -04002175 ret = _mv88e6xxx_atu_cmd(chip, fid, GLOBAL_ATU_OP_GET_NEXT_DB);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002176 if (ret < 0)
2177 return ret;
2178
Vivien Didelotfad09c72016-06-21 12:28:20 -04002179 ret = _mv88e6xxx_atu_mac_read(chip, next.mac);
Vivien Didelot1d194042015-08-10 09:09:51 -04002180 if (ret < 0)
2181 return ret;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002182
Vivien Didelotfad09c72016-06-21 12:28:20 -04002183 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_ATU_DATA);
Vivien Didelot1d194042015-08-10 09:09:51 -04002184 if (ret < 0)
2185 return ret;
2186
2187 next.state = ret & GLOBAL_ATU_DATA_STATE_MASK;
2188 if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2189 unsigned int mask, shift;
2190
2191 if (ret & GLOBAL_ATU_DATA_TRUNK) {
2192 next.trunk = true;
2193 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
2194 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
2195 } else {
2196 next.trunk = false;
2197 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
2198 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
2199 }
2200
2201 next.portv_trunkid = (ret & mask) >> shift;
2202 }
2203
2204 *entry = next;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002205 return 0;
2206}
2207
Vivien Didelotfad09c72016-06-21 12:28:20 -04002208static int _mv88e6xxx_port_fdb_dump_one(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04002209 u16 fid, u16 vid, int port,
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002210 struct switchdev_obj_port_fdb *fdb,
2211 int (*cb)(struct switchdev_obj *obj))
2212{
2213 struct mv88e6xxx_atu_entry addr = {
2214 .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2215 };
2216 int err;
2217
Vivien Didelotfad09c72016-06-21 12:28:20 -04002218 err = _mv88e6xxx_atu_mac_write(chip, addr.mac);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002219 if (err)
2220 return err;
2221
2222 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002223 err = _mv88e6xxx_atu_getnext(chip, fid, &addr);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002224 if (err)
2225 break;
2226
2227 if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2228 break;
2229
2230 if (!addr.trunk && addr.portv_trunkid & BIT(port)) {
2231 bool is_static = addr.state ==
2232 (is_multicast_ether_addr(addr.mac) ?
2233 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2234 GLOBAL_ATU_DATA_STATE_UC_STATIC);
2235
2236 fdb->vid = vid;
2237 ether_addr_copy(fdb->addr, addr.mac);
2238 fdb->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
2239
2240 err = cb(&fdb->obj);
2241 if (err)
2242 break;
2243 }
2244 } while (!is_broadcast_ether_addr(addr.mac));
2245
2246 return err;
2247}
2248
Vivien Didelotf81ec902016-05-09 13:22:58 -04002249static int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
2250 struct switchdev_obj_port_fdb *fdb,
2251 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotf33475b2015-10-22 09:34:41 -04002252{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002253 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002254 struct mv88e6xxx_vtu_stu_entry vlan = {
2255 .vid = GLOBAL_VTU_VID_MASK, /* all ones */
2256 };
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002257 u16 fid;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002258 int err;
2259
Vivien Didelotfad09c72016-06-21 12:28:20 -04002260 mutex_lock(&chip->reg_lock);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002261
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002262 /* Dump port's default Filtering Information Database (VLAN ID 0) */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002263 err = _mv88e6xxx_port_fid_get(chip, port, &fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002264 if (err)
2265 goto unlock;
2266
Vivien Didelotfad09c72016-06-21 12:28:20 -04002267 err = _mv88e6xxx_port_fdb_dump_one(chip, fid, 0, port, fdb, cb);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002268 if (err)
2269 goto unlock;
2270
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002271 /* Dump VLANs' Filtering Information Databases */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002272 err = _mv88e6xxx_vtu_vid_write(chip, vlan.vid);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002273 if (err)
2274 goto unlock;
2275
2276 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002277 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002278 if (err)
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002279 break;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002280
2281 if (!vlan.valid)
2282 break;
2283
Vivien Didelotfad09c72016-06-21 12:28:20 -04002284 err = _mv88e6xxx_port_fdb_dump_one(chip, vlan.fid, vlan.vid,
2285 port, fdb, cb);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002286 if (err)
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002287 break;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002288 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
2289
2290unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002291 mutex_unlock(&chip->reg_lock);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002292
2293 return err;
2294}
2295
Vivien Didelotf81ec902016-05-09 13:22:58 -04002296static int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
2297 struct net_device *bridge)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002298{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002299 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Colin Ian King1d9619d2016-04-25 23:11:22 +01002300 int i, err = 0;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002301
Vivien Didelotfad09c72016-06-21 12:28:20 -04002302 mutex_lock(&chip->reg_lock);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002303
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002304 /* Assign the bridge and remap each port's VLANTable */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002305 chip->ports[port].bridge_dev = bridge;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002306
Vivien Didelotfad09c72016-06-21 12:28:20 -04002307 for (i = 0; i < chip->info->num_ports; ++i) {
2308 if (chip->ports[i].bridge_dev == bridge) {
2309 err = _mv88e6xxx_port_based_vlan_map(chip, i);
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002310 if (err)
2311 break;
2312 }
2313 }
2314
Vivien Didelotfad09c72016-06-21 12:28:20 -04002315 mutex_unlock(&chip->reg_lock);
Vivien Didelota6692752016-02-12 12:09:39 -05002316
Vivien Didelot466dfa02016-02-26 13:16:05 -05002317 return err;
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002318}
2319
Vivien Didelotf81ec902016-05-09 13:22:58 -04002320static void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002321{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002322 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2323 struct net_device *bridge = chip->ports[port].bridge_dev;
Vivien Didelot16bfa702016-03-13 16:21:33 -04002324 int i;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002325
Vivien Didelotfad09c72016-06-21 12:28:20 -04002326 mutex_lock(&chip->reg_lock);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002327
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002328 /* Unassign the bridge and remap each port's VLANTable */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002329 chip->ports[port].bridge_dev = NULL;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002330
Vivien Didelotfad09c72016-06-21 12:28:20 -04002331 for (i = 0; i < chip->info->num_ports; ++i)
2332 if (i == port || chip->ports[i].bridge_dev == bridge)
2333 if (_mv88e6xxx_port_based_vlan_map(chip, i))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002334 netdev_warn(ds->ports[i].netdev,
2335 "failed to remap\n");
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002336
Vivien Didelotfad09c72016-06-21 12:28:20 -04002337 mutex_unlock(&chip->reg_lock);
Vivien Didelot66d9cd02016-02-05 14:07:14 -05002338}
2339
Vivien Didelotfad09c72016-06-21 12:28:20 -04002340static int mv88e6xxx_switch_reset(struct mv88e6xxx_chip *chip)
Vivien Didelot552238b2016-05-09 13:22:49 -04002341{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002342 bool ppu_active = mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE);
Vivien Didelot552238b2016-05-09 13:22:49 -04002343 u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002344 struct gpio_desc *gpiod = chip->reset;
Vivien Didelot552238b2016-05-09 13:22:49 -04002345 unsigned long timeout;
2346 int ret;
2347 int i;
2348
2349 /* Set all ports to the disabled state. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002350 for (i = 0; i < chip->info->num_ports; i++) {
2351 ret = _mv88e6xxx_reg_read(chip, REG_PORT(i), PORT_CONTROL);
Vivien Didelot552238b2016-05-09 13:22:49 -04002352 if (ret < 0)
2353 return ret;
2354
Vivien Didelotfad09c72016-06-21 12:28:20 -04002355 ret = _mv88e6xxx_reg_write(chip, REG_PORT(i), PORT_CONTROL,
Vivien Didelot552238b2016-05-09 13:22:49 -04002356 ret & 0xfffc);
2357 if (ret)
2358 return ret;
2359 }
2360
2361 /* Wait for transmit queues to drain. */
2362 usleep_range(2000, 4000);
2363
2364 /* If there is a gpio connected to the reset pin, toggle it */
2365 if (gpiod) {
2366 gpiod_set_value_cansleep(gpiod, 1);
2367 usleep_range(10000, 20000);
2368 gpiod_set_value_cansleep(gpiod, 0);
2369 usleep_range(10000, 20000);
2370 }
2371
2372 /* Reset the switch. Keep the PPU active if requested. The PPU
2373 * needs to be active to support indirect phy register access
2374 * through global registers 0x18 and 0x19.
2375 */
2376 if (ppu_active)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002377 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, 0x04, 0xc000);
Vivien Didelot552238b2016-05-09 13:22:49 -04002378 else
Vivien Didelotfad09c72016-06-21 12:28:20 -04002379 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, 0x04, 0xc400);
Vivien Didelot552238b2016-05-09 13:22:49 -04002380 if (ret)
2381 return ret;
2382
2383 /* Wait up to one second for reset to complete. */
2384 timeout = jiffies + 1 * HZ;
2385 while (time_before(jiffies, timeout)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002386 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, 0x00);
Vivien Didelot552238b2016-05-09 13:22:49 -04002387 if (ret < 0)
2388 return ret;
2389
2390 if ((ret & is_reset) == is_reset)
2391 break;
2392 usleep_range(1000, 2000);
2393 }
2394 if (time_after(jiffies, timeout))
2395 ret = -ETIMEDOUT;
2396 else
2397 ret = 0;
2398
2399 return ret;
2400}
2401
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002402static int mv88e6xxx_serdes_power_on(struct mv88e6xxx_chip *chip)
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002403{
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002404 u16 val;
2405 int err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002406
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002407 /* Clear Power Down bit */
2408 err = mv88e6xxx_serdes_read(chip, MII_BMCR, &val);
2409 if (err)
2410 return err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002411
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002412 if (val & BMCR_PDOWN) {
2413 val &= ~BMCR_PDOWN;
2414 err = mv88e6xxx_serdes_write(chip, MII_BMCR, val);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002415 }
2416
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002417 return err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002418}
2419
Vivien Didelot8f6345b2016-07-20 18:18:36 -04002420static int mv88e6xxx_port_read(struct mv88e6xxx_chip *chip, int port,
2421 int reg, u16 *val)
2422{
2423 int addr = chip->info->port_base_addr + port;
2424
2425 if (port >= chip->info->num_ports)
2426 return -EINVAL;
2427
2428 return mv88e6xxx_read(chip, addr, reg, val);
2429}
2430
Vivien Didelotfad09c72016-06-21 12:28:20 -04002431static int mv88e6xxx_setup_port(struct mv88e6xxx_chip *chip, int port)
Guenter Roeckd827e882015-03-26 18:36:29 -07002432{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002433 struct dsa_switch *ds = chip->ds;
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002434 int ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002435 u16 reg;
Guenter Roeckd827e882015-03-26 18:36:29 -07002436
Vivien Didelotfad09c72016-06-21 12:28:20 -04002437 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2438 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2439 mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip) ||
2440 mv88e6xxx_6065_family(chip) || mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002441 /* MAC Forcing register: don't force link, speed,
2442 * duplex or flow control state to any particular
2443 * values on physical ports, but force the CPU port
2444 * and all DSA ports to their maximum bandwidth and
2445 * full duplex.
2446 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002447 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_PCS_CTRL);
Andrew Lunn60045cb2015-08-17 23:52:51 +02002448 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
Russell King53adc9e2015-09-21 21:42:59 +01002449 reg &= ~PORT_PCS_CTRL_UNFORCED;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002450 reg |= PORT_PCS_CTRL_FORCE_LINK |
2451 PORT_PCS_CTRL_LINK_UP |
2452 PORT_PCS_CTRL_DUPLEX_FULL |
2453 PORT_PCS_CTRL_FORCE_DUPLEX;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002454 if (mv88e6xxx_6065_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002455 reg |= PORT_PCS_CTRL_100;
2456 else
2457 reg |= PORT_PCS_CTRL_1000;
2458 } else {
2459 reg |= PORT_PCS_CTRL_UNFORCED;
2460 }
2461
Vivien Didelotfad09c72016-06-21 12:28:20 -04002462 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002463 PORT_PCS_CTRL, reg);
2464 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002465 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002466 }
2467
2468 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
2469 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
2470 * tunneling, determine priority by looking at 802.1p and IP
2471 * priority fields (IP prio has precedence), and set STP state
2472 * to Forwarding.
2473 *
2474 * If this is the CPU link, use DSA or EDSA tagging depending
2475 * on which tagging mode was configured.
2476 *
2477 * If this is a link to another switch, use DSA tagging mode.
2478 *
2479 * If this is the upstream port for this switch, enable
2480 * forwarding of unknown unicasts and multicasts.
2481 */
2482 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002483 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2484 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2485 mv88e6xxx_6095_family(chip) || mv88e6xxx_6065_family(chip) ||
2486 mv88e6xxx_6185_family(chip) || mv88e6xxx_6320_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002487 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
2488 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
2489 PORT_CONTROL_STATE_FORWARDING;
2490 if (dsa_is_cpu_port(ds, port)) {
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002491 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA))
Andrew Lunn5377b802016-06-04 21:17:02 +02002492 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA |
Andrew Lunnc047a1f2015-09-29 01:50:56 +02002493 PORT_CONTROL_FORWARD_UNKNOWN_MC;
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002494 else
2495 reg |= PORT_CONTROL_DSA_TAG;
Jamie Lentinf027e0c2016-08-22 16:01:04 +02002496 reg |= PORT_CONTROL_EGRESS_ADD_TAG |
2497 PORT_CONTROL_FORWARD_UNKNOWN;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002498 }
Andrew Lunn6083ce72015-08-17 23:52:52 +02002499 if (dsa_is_dsa_port(ds, port)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002500 if (mv88e6xxx_6095_family(chip) ||
2501 mv88e6xxx_6185_family(chip))
Andrew Lunn6083ce72015-08-17 23:52:52 +02002502 reg |= PORT_CONTROL_DSA_TAG;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002503 if (mv88e6xxx_6352_family(chip) ||
2504 mv88e6xxx_6351_family(chip) ||
2505 mv88e6xxx_6165_family(chip) ||
2506 mv88e6xxx_6097_family(chip) ||
2507 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002508 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunn6083ce72015-08-17 23:52:52 +02002509 }
2510
Andrew Lunn54d792f2015-05-06 01:09:47 +02002511 if (port == dsa_upstream_port(ds))
2512 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2513 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2514 }
2515 if (reg) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002516 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002517 PORT_CONTROL, reg);
2518 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002519 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002520 }
2521
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002522 /* If this port is connected to a SerDes, make sure the SerDes is not
2523 * powered down.
2524 */
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002525 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_SERDES)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002526 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_STATUS);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002527 if (ret < 0)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002528 return ret;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002529 ret &= PORT_STATUS_CMODE_MASK;
2530 if ((ret == PORT_STATUS_CMODE_100BASE_X) ||
2531 (ret == PORT_STATUS_CMODE_1000BASE_X) ||
2532 (ret == PORT_STATUS_CMODE_SGMII)) {
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002533 ret = mv88e6xxx_serdes_power_on(chip);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002534 if (ret < 0)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002535 return ret;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002536 }
2537 }
2538
Vivien Didelot8efdda42015-08-13 12:52:23 -04002539 /* Port Control 2: don't force a good FCS, set the maximum frame size to
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002540 * 10240 bytes, disable 802.1q tags checking, don't discard tagged or
Vivien Didelot8efdda42015-08-13 12:52:23 -04002541 * untagged frames on this port, do a destination address lookup on all
2542 * received packets as usual, disable ARP mirroring and don't send a
2543 * copy of all transmitted/received frames on this port to the CPU.
Andrew Lunn54d792f2015-05-06 01:09:47 +02002544 */
2545 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002546 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2547 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2548 mv88e6xxx_6095_family(chip) || mv88e6xxx_6320_family(chip) ||
2549 mv88e6xxx_6185_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002550 reg = PORT_CONTROL_2_MAP_DA;
2551
Vivien Didelotfad09c72016-06-21 12:28:20 -04002552 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2553 mv88e6xxx_6165_family(chip) || mv88e6xxx_6320_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002554 reg |= PORT_CONTROL_2_JUMBO_10240;
2555
Vivien Didelotfad09c72016-06-21 12:28:20 -04002556 if (mv88e6xxx_6095_family(chip) || mv88e6xxx_6185_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002557 /* Set the upstream port this port should use */
2558 reg |= dsa_upstream_port(ds);
2559 /* enable forwarding of unknown multicast addresses to
2560 * the upstream port
2561 */
2562 if (port == dsa_upstream_port(ds))
2563 reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
2564 }
2565
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002566 reg |= PORT_CONTROL_2_8021Q_DISABLED;
Vivien Didelot8efdda42015-08-13 12:52:23 -04002567
Andrew Lunn54d792f2015-05-06 01:09:47 +02002568 if (reg) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002569 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002570 PORT_CONTROL_2, reg);
2571 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002572 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002573 }
2574
2575 /* Port Association Vector: when learning source addresses
2576 * of packets, add the address to the address database using
2577 * a port bitmap that has only the bit for this port set and
2578 * the other bits clear.
2579 */
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002580 reg = 1 << port;
Vivien Didelot996ecb82016-04-14 14:42:08 -04002581 /* Disable learning for CPU port */
2582 if (dsa_is_cpu_port(ds, port))
Vivien Didelot65fa4022016-04-14 14:42:07 -04002583 reg = 0;
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002584
Vivien Didelotfad09c72016-06-21 12:28:20 -04002585 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_ASSOC_VECTOR,
2586 reg);
Andrew Lunn54d792f2015-05-06 01:09:47 +02002587 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002588 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002589
2590 /* Egress rate control 2: disable egress rate control. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002591 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_RATE_CONTROL_2,
Andrew Lunn54d792f2015-05-06 01:09:47 +02002592 0x0000);
2593 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002594 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002595
Vivien Didelotfad09c72016-06-21 12:28:20 -04002596 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2597 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2598 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002599 /* Do not limit the period of time that this port can
2600 * be paused for by the remote end or the period of
2601 * time that this port can pause the remote end.
2602 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002603 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002604 PORT_PAUSE_CTRL, 0x0000);
2605 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002606 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002607
2608 /* Port ATU control: disable limiting the number of
2609 * address database entries that this port is allowed
2610 * to use.
2611 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002612 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002613 PORT_ATU_CONTROL, 0x0000);
2614 /* Priority Override: disable DA, SA and VTU priority
2615 * override.
2616 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002617 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002618 PORT_PRI_OVERRIDE, 0x0000);
2619 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002620 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002621
2622 /* Port Ethertype: use the Ethertype DSA Ethertype
2623 * value.
2624 */
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002625 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA)) {
2626 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2627 PORT_ETH_TYPE, ETH_P_EDSA);
2628 if (ret)
2629 return ret;
2630 }
2631
Andrew Lunn54d792f2015-05-06 01:09:47 +02002632 /* Tag Remap: use an identity 802.1p prio -> switch
2633 * prio mapping.
2634 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002635 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002636 PORT_TAG_REGMAP_0123, 0x3210);
2637 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002638 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002639
2640 /* Tag Remap 2: use an identity 802.1p prio -> switch
2641 * prio mapping.
2642 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002643 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002644 PORT_TAG_REGMAP_4567, 0x7654);
2645 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002646 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002647 }
2648
Vivien Didelotfad09c72016-06-21 12:28:20 -04002649 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2650 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2651 mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip) ||
2652 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002653 /* Rate Control: disable ingress rate limiting. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002654 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002655 PORT_RATE_CONTROL, 0x0001);
2656 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002657 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002658 }
2659
Guenter Roeck366f0a02015-03-26 18:36:30 -07002660 /* Port Control 1: disable trunking, disable sending
2661 * learning messages to this port.
Guenter Roeckd827e882015-03-26 18:36:29 -07002662 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002663 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_1,
2664 0x0000);
Guenter Roeckd827e882015-03-26 18:36:29 -07002665 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002666 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07002667
Vivien Didelot207afda2016-04-14 14:42:09 -04002668 /* Port based VLAN map: give each port the same default address
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002669 * database, and allow bidirectional communication between the
2670 * CPU and DSA port(s), and the other ports.
Guenter Roeckd827e882015-03-26 18:36:29 -07002671 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002672 ret = _mv88e6xxx_port_fid_set(chip, port, 0);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002673 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002674 return ret;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002675
Vivien Didelotfad09c72016-06-21 12:28:20 -04002676 ret = _mv88e6xxx_port_based_vlan_map(chip, port);
Guenter Roeckd827e882015-03-26 18:36:29 -07002677 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002678 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07002679
2680 /* Default VLAN ID and priority: don't set a default VLAN
2681 * ID, and set the default packet priority to zero.
2682 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002683 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_DEFAULT_VLAN,
Vivien Didelot47cf1e652015-04-20 17:43:26 -04002684 0x0000);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002685 if (ret)
2686 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07002687
Andrew Lunndbde9e62015-05-06 01:09:48 +02002688 return 0;
2689}
2690
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002691static int mv88e6xxx_g1_set_switch_mac(struct mv88e6xxx_chip *chip, u8 *addr)
2692{
2693 int err;
2694
2695 err = mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_MAC_01,
2696 (addr[0] << 8) | addr[1]);
2697 if (err)
2698 return err;
2699
2700 err = mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_MAC_23,
2701 (addr[2] << 8) | addr[3]);
2702 if (err)
2703 return err;
2704
2705 return mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_MAC_45,
2706 (addr[4] << 8) | addr[5]);
2707}
2708
Vivien Didelotacddbd22016-07-18 20:45:39 -04002709static int mv88e6xxx_g1_set_age_time(struct mv88e6xxx_chip *chip,
2710 unsigned int msecs)
2711{
2712 const unsigned int coeff = chip->info->age_time_coeff;
2713 const unsigned int min = 0x01 * coeff;
2714 const unsigned int max = 0xff * coeff;
2715 u8 age_time;
2716 u16 val;
2717 int err;
2718
2719 if (msecs < min || msecs > max)
2720 return -ERANGE;
2721
2722 /* Round to nearest multiple of coeff */
2723 age_time = (msecs + coeff / 2) / coeff;
2724
2725 err = mv88e6xxx_read(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL, &val);
2726 if (err)
2727 return err;
2728
2729 /* AgeTime is 11:4 bits */
2730 val &= ~0xff0;
2731 val |= age_time << 4;
2732
2733 return mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL, val);
2734}
2735
Vivien Didelot2cfcd962016-07-18 20:45:40 -04002736static int mv88e6xxx_set_ageing_time(struct dsa_switch *ds,
2737 unsigned int ageing_time)
2738{
2739 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2740 int err;
2741
2742 mutex_lock(&chip->reg_lock);
2743 err = mv88e6xxx_g1_set_age_time(chip, ageing_time);
2744 mutex_unlock(&chip->reg_lock);
2745
2746 return err;
2747}
2748
Vivien Didelot97299342016-07-18 20:45:30 -04002749static int mv88e6xxx_g1_setup(struct mv88e6xxx_chip *chip)
Vivien Didelot08a01262016-05-09 13:22:50 -04002750{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002751 struct dsa_switch *ds = chip->ds;
Vivien Didelotb0745e872016-05-09 13:22:53 -04002752 u32 upstream_port = dsa_upstream_port(ds);
Vivien Didelot119477b2016-05-09 13:22:51 -04002753 u16 reg;
Vivien Didelot08a01262016-05-09 13:22:50 -04002754 int err;
Vivien Didelot08a01262016-05-09 13:22:50 -04002755
Vivien Didelot119477b2016-05-09 13:22:51 -04002756 /* Enable the PHY Polling Unit if present, don't discard any packets,
2757 * and mask all interrupt sources.
2758 */
2759 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002760 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU) ||
2761 mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE))
Vivien Didelot119477b2016-05-09 13:22:51 -04002762 reg |= GLOBAL_CONTROL_PPU_ENABLE;
2763
Vivien Didelotfad09c72016-06-21 12:28:20 -04002764 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL, reg);
Vivien Didelot119477b2016-05-09 13:22:51 -04002765 if (err)
2766 return err;
2767
Vivien Didelotb0745e872016-05-09 13:22:53 -04002768 /* Configure the upstream port, and configure it as the port to which
2769 * ingress and egress and ARP monitor frames are to be sent.
2770 */
2771 reg = upstream_port << GLOBAL_MONITOR_CONTROL_INGRESS_SHIFT |
2772 upstream_port << GLOBAL_MONITOR_CONTROL_EGRESS_SHIFT |
2773 upstream_port << GLOBAL_MONITOR_CONTROL_ARP_SHIFT;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002774 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_MONITOR_CONTROL,
2775 reg);
Vivien Didelotb0745e872016-05-09 13:22:53 -04002776 if (err)
2777 return err;
2778
Vivien Didelot50484ff2016-05-09 13:22:54 -04002779 /* Disable remote management, and set the switch's DSA device number. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002780 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL_2,
Vivien Didelot50484ff2016-05-09 13:22:54 -04002781 GLOBAL_CONTROL_2_MULTIPLE_CASCADE |
2782 (ds->index & 0x1f));
2783 if (err)
2784 return err;
2785
Vivien Didelotacddbd22016-07-18 20:45:39 -04002786 /* Clear all the VTU and STU entries */
2787 err = _mv88e6xxx_vtu_stu_flush(chip);
2788 if (err < 0)
2789 return err;
2790
Vivien Didelot08a01262016-05-09 13:22:50 -04002791 /* Set the default address aging time to 5 minutes, and
2792 * enable address learn messages to be sent to all message
2793 * ports.
2794 */
Vivien Didelotacddbd22016-07-18 20:45:39 -04002795 err = mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL,
2796 GLOBAL_ATU_CONTROL_LEARN2ALL);
Vivien Didelot08a01262016-05-09 13:22:50 -04002797 if (err)
2798 return err;
2799
Vivien Didelotacddbd22016-07-18 20:45:39 -04002800 err = mv88e6xxx_g1_set_age_time(chip, 300000);
2801 if (err)
Vivien Didelot97299342016-07-18 20:45:30 -04002802 return err;
2803
2804 /* Clear all ATU entries */
2805 err = _mv88e6xxx_atu_flush(chip, 0, true);
2806 if (err)
2807 return err;
2808
Vivien Didelot08a01262016-05-09 13:22:50 -04002809 /* Configure the IP ToS mapping registers. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002810 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_0, 0x0000);
Vivien Didelot08a01262016-05-09 13:22:50 -04002811 if (err)
2812 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002813 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_1, 0x0000);
Vivien Didelot08a01262016-05-09 13:22:50 -04002814 if (err)
2815 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002816 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_2, 0x5555);
Vivien Didelot08a01262016-05-09 13:22:50 -04002817 if (err)
2818 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002819 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_3, 0x5555);
Vivien Didelot08a01262016-05-09 13:22:50 -04002820 if (err)
2821 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002822 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_4, 0xaaaa);
Vivien Didelot08a01262016-05-09 13:22:50 -04002823 if (err)
2824 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002825 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_5, 0xaaaa);
Vivien Didelot08a01262016-05-09 13:22:50 -04002826 if (err)
2827 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002828 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_6, 0xffff);
Vivien Didelot08a01262016-05-09 13:22:50 -04002829 if (err)
2830 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002831 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_7, 0xffff);
Vivien Didelot08a01262016-05-09 13:22:50 -04002832 if (err)
2833 return err;
2834
2835 /* Configure the IEEE 802.1p priority mapping register. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002836 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IEEE_PRI, 0xfa41);
Vivien Didelot08a01262016-05-09 13:22:50 -04002837 if (err)
2838 return err;
2839
Vivien Didelot97299342016-07-18 20:45:30 -04002840 /* Clear the statistics counters for all ports */
2841 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
2842 GLOBAL_STATS_OP_FLUSH_ALL);
2843 if (err)
2844 return err;
2845
2846 /* Wait for the flush to complete. */
2847 err = _mv88e6xxx_stats_wait(chip);
2848 if (err)
2849 return err;
2850
2851 return 0;
2852}
2853
Vivien Didelotf22ab642016-07-18 20:45:31 -04002854static int mv88e6xxx_g2_device_mapping_write(struct mv88e6xxx_chip *chip,
2855 int target, int port)
2856{
2857 u16 val = (target << 8) | (port & 0xf);
2858
2859 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_DEVICE_MAPPING, val);
2860}
2861
2862static int mv88e6xxx_g2_set_device_mapping(struct mv88e6xxx_chip *chip)
2863{
2864 int target, port;
2865 int err;
2866
2867 /* Initialize the routing port to the 32 possible target devices */
2868 for (target = 0; target < 32; ++target) {
2869 port = 0xf;
2870
2871 if (target < DSA_MAX_SWITCHES) {
2872 port = chip->ds->rtable[target];
2873 if (port == DSA_RTABLE_NONE)
2874 port = 0xf;
2875 }
2876
2877 err = mv88e6xxx_g2_device_mapping_write(chip, target, port);
2878 if (err)
2879 break;
2880 }
2881
2882 return err;
2883}
2884
Vivien Didelot51540412016-07-18 20:45:32 -04002885static int mv88e6xxx_g2_trunk_mask_write(struct mv88e6xxx_chip *chip, int num,
2886 bool hask, u16 mask)
2887{
2888 const u16 port_mask = BIT(chip->info->num_ports) - 1;
2889 u16 val = (num << 12) | (mask & port_mask);
2890
2891 if (hask)
2892 val |= GLOBAL2_TRUNK_MASK_HASK;
2893
2894 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_TRUNK_MASK, val);
2895}
2896
2897static int mv88e6xxx_g2_trunk_mapping_write(struct mv88e6xxx_chip *chip, int id,
2898 u16 map)
2899{
2900 const u16 port_mask = BIT(chip->info->num_ports) - 1;
2901 u16 val = (id << 11) | (map & port_mask);
2902
2903 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_TRUNK_MAPPING, val);
2904}
2905
2906static int mv88e6xxx_g2_clear_trunk(struct mv88e6xxx_chip *chip)
2907{
2908 const u16 port_mask = BIT(chip->info->num_ports) - 1;
2909 int i, err;
2910
2911 /* Clear all eight possible Trunk Mask vectors */
2912 for (i = 0; i < 8; ++i) {
2913 err = mv88e6xxx_g2_trunk_mask_write(chip, i, false, port_mask);
2914 if (err)
2915 return err;
2916 }
2917
2918 /* Clear all sixteen possible Trunk ID routing vectors */
2919 for (i = 0; i < 16; ++i) {
2920 err = mv88e6xxx_g2_trunk_mapping_write(chip, i, 0);
2921 if (err)
2922 return err;
2923 }
2924
2925 return 0;
2926}
2927
Vivien Didelot8ec61c72016-07-18 20:45:37 -04002928static int mv88e6xxx_g2_clear_irl(struct mv88e6xxx_chip *chip)
2929{
2930 int port, err;
2931
2932 /* Init all Ingress Rate Limit resources of all ports */
2933 for (port = 0; port < chip->info->num_ports; ++port) {
2934 /* XXX newer chips (like 88E6390) have different 2-bit ops */
2935 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_IRL_CMD,
2936 GLOBAL2_IRL_CMD_OP_INIT_ALL |
2937 (port << 8));
2938 if (err)
2939 break;
2940
2941 /* Wait for the operation to complete */
Vivien Didelot2d79af62016-08-15 17:18:57 -04002942 err = mv88e6xxx_wait(chip, REG_GLOBAL2, GLOBAL2_IRL_CMD,
2943 GLOBAL2_IRL_CMD_BUSY);
Vivien Didelot8ec61c72016-07-18 20:45:37 -04002944 if (err)
2945 break;
2946 }
2947
2948 return err;
2949}
2950
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002951/* Indirect write to the Switch MAC/WoL/WoF register */
2952static int mv88e6xxx_g2_switch_mac_write(struct mv88e6xxx_chip *chip,
2953 unsigned int pointer, u8 data)
2954{
2955 u16 val = (pointer << 8) | data;
2956
2957 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_SWITCH_MAC, val);
2958}
2959
2960static int mv88e6xxx_g2_set_switch_mac(struct mv88e6xxx_chip *chip, u8 *addr)
2961{
2962 int i, err;
2963
2964 for (i = 0; i < 6; i++) {
2965 err = mv88e6xxx_g2_switch_mac_write(chip, i, addr[i]);
2966 if (err)
2967 break;
2968 }
2969
2970 return err;
2971}
2972
Vivien Didelot9bda8892016-07-18 20:45:36 -04002973static int mv88e6xxx_g2_pot_write(struct mv88e6xxx_chip *chip, int pointer,
2974 u8 data)
2975{
2976 u16 val = (pointer << 8) | (data & 0x7);
2977
2978 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_PRIO_OVERRIDE, val);
2979}
2980
2981static int mv88e6xxx_g2_clear_pot(struct mv88e6xxx_chip *chip)
2982{
2983 int i, err;
2984
2985 /* Clear all sixteen possible Priority Override entries */
2986 for (i = 0; i < 16; i++) {
2987 err = mv88e6xxx_g2_pot_write(chip, i, 0);
2988 if (err)
2989 break;
2990 }
2991
2992 return err;
2993}
2994
Vivien Didelot855b1932016-07-20 18:18:35 -04002995static int mv88e6xxx_g2_eeprom_wait(struct mv88e6xxx_chip *chip)
2996{
Vivien Didelot2d79af62016-08-15 17:18:57 -04002997 return mv88e6xxx_wait(chip, REG_GLOBAL2, GLOBAL2_EEPROM_CMD,
2998 GLOBAL2_EEPROM_CMD_BUSY |
2999 GLOBAL2_EEPROM_CMD_RUNNING);
Vivien Didelot855b1932016-07-20 18:18:35 -04003000}
3001
3002static int mv88e6xxx_g2_eeprom_cmd(struct mv88e6xxx_chip *chip, u16 cmd)
3003{
3004 int err;
3005
3006 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_EEPROM_CMD, cmd);
3007 if (err)
3008 return err;
3009
3010 return mv88e6xxx_g2_eeprom_wait(chip);
3011}
3012
3013static int mv88e6xxx_g2_eeprom_read16(struct mv88e6xxx_chip *chip,
3014 u8 addr, u16 *data)
3015{
3016 u16 cmd = GLOBAL2_EEPROM_CMD_OP_READ | addr;
3017 int err;
3018
3019 err = mv88e6xxx_g2_eeprom_wait(chip);
3020 if (err)
3021 return err;
3022
3023 err = mv88e6xxx_g2_eeprom_cmd(chip, cmd);
3024 if (err)
3025 return err;
3026
3027 return mv88e6xxx_read(chip, REG_GLOBAL2, GLOBAL2_EEPROM_DATA, data);
3028}
3029
3030static int mv88e6xxx_g2_eeprom_write16(struct mv88e6xxx_chip *chip,
3031 u8 addr, u16 data)
3032{
3033 u16 cmd = GLOBAL2_EEPROM_CMD_OP_WRITE | addr;
3034 int err;
3035
3036 err = mv88e6xxx_g2_eeprom_wait(chip);
3037 if (err)
3038 return err;
3039
3040 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_EEPROM_DATA, data);
3041 if (err)
3042 return err;
3043
3044 return mv88e6xxx_g2_eeprom_cmd(chip, cmd);
3045}
3046
Vivien Didelot57c67cf2016-08-15 17:18:59 -04003047static int mv88e6xxx_g2_smi_phy_wait(struct mv88e6xxx_chip *chip)
3048{
3049 return mv88e6xxx_wait(chip, REG_GLOBAL2, GLOBAL2_SMI_PHY_CMD,
3050 GLOBAL2_SMI_PHY_CMD_BUSY);
3051}
3052
3053static int mv88e6xxx_g2_smi_phy_cmd(struct mv88e6xxx_chip *chip, u16 cmd)
3054{
3055 int err;
3056
3057 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_SMI_PHY_CMD, cmd);
3058 if (err)
3059 return err;
3060
3061 return mv88e6xxx_g2_smi_phy_wait(chip);
3062}
3063
3064static int mv88e6xxx_g2_smi_phy_read(struct mv88e6xxx_chip *chip, int addr,
3065 int reg, u16 *val)
3066{
3067 u16 cmd = GLOBAL2_SMI_PHY_CMD_OP_22_READ_DATA | (addr << 5) | reg;
3068 int err;
3069
3070 err = mv88e6xxx_g2_smi_phy_wait(chip);
3071 if (err)
3072 return err;
3073
3074 err = mv88e6xxx_g2_smi_phy_cmd(chip, cmd);
3075 if (err)
3076 return err;
3077
3078 return mv88e6xxx_read(chip, REG_GLOBAL2, GLOBAL2_SMI_PHY_DATA, val);
3079}
3080
3081static int mv88e6xxx_g2_smi_phy_write(struct mv88e6xxx_chip *chip, int addr,
3082 int reg, u16 val)
3083{
3084 u16 cmd = GLOBAL2_SMI_PHY_CMD_OP_22_WRITE_DATA | (addr << 5) | reg;
3085 int err;
3086
3087 err = mv88e6xxx_g2_smi_phy_wait(chip);
3088 if (err)
3089 return err;
3090
3091 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_SMI_PHY_DATA, val);
3092 if (err)
3093 return err;
3094
3095 return mv88e6xxx_g2_smi_phy_cmd(chip, cmd);
3096}
3097
Vivien Didelote57e5e72016-08-15 17:19:00 -04003098static const struct mv88e6xxx_ops mv88e6xxx_g2_smi_phy_ops = {
3099 .read = mv88e6xxx_g2_smi_phy_read,
3100 .write = mv88e6xxx_g2_smi_phy_write,
3101};
3102
Vivien Didelot97299342016-07-18 20:45:30 -04003103static int mv88e6xxx_g2_setup(struct mv88e6xxx_chip *chip)
3104{
Vivien Didelot47395ed2016-07-18 20:45:33 -04003105 u16 reg;
Vivien Didelot97299342016-07-18 20:45:30 -04003106 int err;
Vivien Didelot97299342016-07-18 20:45:30 -04003107
Vivien Didelot47395ed2016-07-18 20:45:33 -04003108 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_2X)) {
3109 /* Consider the frames with reserved multicast destination
3110 * addresses matching 01:80:c2:00:00:2x as MGMT.
3111 */
3112 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_MGMT_EN_2X,
3113 0xffff);
3114 if (err)
3115 return err;
3116 }
3117
3118 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_0X)) {
3119 /* Consider the frames with reserved multicast destination
3120 * addresses matching 01:80:c2:00:00:0x as MGMT.
3121 */
3122 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_MGMT_EN_0X,
3123 0xffff);
3124 if (err)
3125 return err;
3126 }
Vivien Didelot08a01262016-05-09 13:22:50 -04003127
3128 /* Ignore removed tag data on doubly tagged packets, disable
3129 * flow control messages, force flow control priority to the
3130 * highest, and send all special multicast frames to the CPU
3131 * port at the highest priority.
3132 */
Vivien Didelot47395ed2016-07-18 20:45:33 -04003133 reg = GLOBAL2_SWITCH_MGMT_FORCE_FLOW_CTRL_PRI | (0x7 << 4);
3134 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_0X) ||
3135 mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_2X))
3136 reg |= GLOBAL2_SWITCH_MGMT_RSVD2CPU | 0x7;
3137 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_SWITCH_MGMT, reg);
Vivien Didelot08a01262016-05-09 13:22:50 -04003138 if (err)
3139 return err;
3140
3141 /* Program the DSA routing table. */
Vivien Didelotf22ab642016-07-18 20:45:31 -04003142 err = mv88e6xxx_g2_set_device_mapping(chip);
3143 if (err)
3144 return err;
Vivien Didelot08a01262016-05-09 13:22:50 -04003145
Vivien Didelot51540412016-07-18 20:45:32 -04003146 /* Clear all trunk masks and mapping. */
3147 err = mv88e6xxx_g2_clear_trunk(chip);
3148 if (err)
3149 return err;
Vivien Didelot08a01262016-05-09 13:22:50 -04003150
Vivien Didelot8ec61c72016-07-18 20:45:37 -04003151 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_IRL)) {
3152 /* Disable ingress rate limiting by resetting all per port
3153 * ingress rate limit resources to their initial state.
3154 */
3155 err = mv88e6xxx_g2_clear_irl(chip);
3156 if (err)
3157 return err;
3158 }
3159
Vivien Didelot63ed8802016-07-18 20:45:35 -04003160 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_PVT)) {
3161 /* Initialize Cross-chip Port VLAN Table to reset defaults */
3162 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_PVT_ADDR,
3163 GLOBAL2_PVT_ADDR_OP_INIT_ONES);
3164 if (err)
3165 return err;
3166 }
3167
Vivien Didelot9bda8892016-07-18 20:45:36 -04003168 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_POT)) {
Vivien Didelot08a01262016-05-09 13:22:50 -04003169 /* Clear the priority override table. */
Vivien Didelot9bda8892016-07-18 20:45:36 -04003170 err = mv88e6xxx_g2_clear_pot(chip);
3171 if (err)
3172 return err;
Vivien Didelot08a01262016-05-09 13:22:50 -04003173 }
3174
Vivien Didelot97299342016-07-18 20:45:30 -04003175 return 0;
Vivien Didelot08a01262016-05-09 13:22:50 -04003176}
3177
Vivien Didelotf81ec902016-05-09 13:22:58 -04003178static int mv88e6xxx_setup(struct dsa_switch *ds)
Guenter Roeckacdaffc2015-03-26 18:36:28 -07003179{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003180 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelot552238b2016-05-09 13:22:49 -04003181 int err;
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003182 int i;
3183
Vivien Didelotfad09c72016-06-21 12:28:20 -04003184 chip->ds = ds;
3185 ds->slave_mii_bus = chip->mdio_bus;
Vivien Didelot552238b2016-05-09 13:22:49 -04003186
Vivien Didelotfad09c72016-06-21 12:28:20 -04003187 mutex_lock(&chip->reg_lock);
Vivien Didelot552238b2016-05-09 13:22:49 -04003188
Vivien Didelotfad09c72016-06-21 12:28:20 -04003189 err = mv88e6xxx_switch_reset(chip);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003190 if (err)
3191 goto unlock;
Andrew Lunn54d792f2015-05-06 01:09:47 +02003192
Vivien Didelot97299342016-07-18 20:45:30 -04003193 /* Setup Switch Port Registers */
3194 for (i = 0; i < chip->info->num_ports; i++) {
3195 err = mv88e6xxx_setup_port(chip, i);
3196 if (err)
3197 goto unlock;
3198 }
3199
3200 /* Setup Switch Global 1 Registers */
3201 err = mv88e6xxx_g1_setup(chip);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003202 if (err)
3203 goto unlock;
3204
Vivien Didelot97299342016-07-18 20:45:30 -04003205 /* Setup Switch Global 2 Registers */
3206 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_GLOBAL2)) {
3207 err = mv88e6xxx_g2_setup(chip);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003208 if (err)
3209 goto unlock;
3210 }
Andrew Lunn54d792f2015-05-06 01:09:47 +02003211
Vivien Didelot6b17e862015-08-13 12:52:18 -04003212unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04003213 mutex_unlock(&chip->reg_lock);
Andrew Lunndb687a52015-06-20 21:31:29 +02003214
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003215 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02003216}
3217
Vivien Didelot3b4caa12016-07-18 20:45:34 -04003218static int mv88e6xxx_set_addr(struct dsa_switch *ds, u8 *addr)
3219{
3220 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3221 int err;
3222
3223 mutex_lock(&chip->reg_lock);
3224
3225 /* Has an indirect Switch MAC/WoL/WoF register in Global 2? */
3226 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_SWITCH_MAC))
3227 err = mv88e6xxx_g2_set_switch_mac(chip, addr);
3228 else
3229 err = mv88e6xxx_g1_set_switch_mac(chip, addr);
3230
3231 mutex_unlock(&chip->reg_lock);
3232
3233 return err;
3234}
3235
Vivien Didelote57e5e72016-08-15 17:19:00 -04003236static int mv88e6xxx_mdio_read(struct mii_bus *bus, int phy, int reg)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003237{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003238 struct mv88e6xxx_chip *chip = bus->priv;
Vivien Didelote57e5e72016-08-15 17:19:00 -04003239 u16 val;
3240 int err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003241
Vivien Didelote57e5e72016-08-15 17:19:00 -04003242 if (phy >= chip->info->num_ports)
Andrew Lunn158bc062016-04-28 21:24:06 -04003243 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003244
Vivien Didelotfad09c72016-06-21 12:28:20 -04003245 mutex_lock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04003246 err = mv88e6xxx_phy_read(chip, phy, reg, &val);
Vivien Didelotfad09c72016-06-21 12:28:20 -04003247 mutex_unlock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04003248
3249 return err ? err : val;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003250}
3251
Vivien Didelote57e5e72016-08-15 17:19:00 -04003252static int mv88e6xxx_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003253{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003254 struct mv88e6xxx_chip *chip = bus->priv;
Vivien Didelote57e5e72016-08-15 17:19:00 -04003255 int err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003256
Vivien Didelote57e5e72016-08-15 17:19:00 -04003257 if (phy >= chip->info->num_ports)
Andrew Lunn158bc062016-04-28 21:24:06 -04003258 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003259
Vivien Didelotfad09c72016-06-21 12:28:20 -04003260 mutex_lock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04003261 err = mv88e6xxx_phy_write(chip, phy, reg, val);
Vivien Didelotfad09c72016-06-21 12:28:20 -04003262 mutex_unlock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04003263
3264 return err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003265}
3266
Vivien Didelotfad09c72016-06-21 12:28:20 -04003267static int mv88e6xxx_mdio_register(struct mv88e6xxx_chip *chip,
Andrew Lunnb516d452016-06-04 21:17:06 +02003268 struct device_node *np)
3269{
3270 static int index;
3271 struct mii_bus *bus;
3272 int err;
3273
Andrew Lunnb516d452016-06-04 21:17:06 +02003274 if (np)
Vivien Didelotfad09c72016-06-21 12:28:20 -04003275 chip->mdio_np = of_get_child_by_name(np, "mdio");
Andrew Lunnb516d452016-06-04 21:17:06 +02003276
Vivien Didelotfad09c72016-06-21 12:28:20 -04003277 bus = devm_mdiobus_alloc(chip->dev);
Andrew Lunnb516d452016-06-04 21:17:06 +02003278 if (!bus)
3279 return -ENOMEM;
3280
Vivien Didelotfad09c72016-06-21 12:28:20 -04003281 bus->priv = (void *)chip;
Andrew Lunnb516d452016-06-04 21:17:06 +02003282 if (np) {
3283 bus->name = np->full_name;
3284 snprintf(bus->id, MII_BUS_ID_SIZE, "%s", np->full_name);
3285 } else {
3286 bus->name = "mv88e6xxx SMI";
3287 snprintf(bus->id, MII_BUS_ID_SIZE, "mv88e6xxx-%d", index++);
3288 }
3289
3290 bus->read = mv88e6xxx_mdio_read;
3291 bus->write = mv88e6xxx_mdio_write;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003292 bus->parent = chip->dev;
Andrew Lunnb516d452016-06-04 21:17:06 +02003293
Vivien Didelotfad09c72016-06-21 12:28:20 -04003294 if (chip->mdio_np)
3295 err = of_mdiobus_register(bus, chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003296 else
3297 err = mdiobus_register(bus);
3298 if (err) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04003299 dev_err(chip->dev, "Cannot register MDIO bus (%d)\n", err);
Andrew Lunnb516d452016-06-04 21:17:06 +02003300 goto out;
3301 }
Vivien Didelotfad09c72016-06-21 12:28:20 -04003302 chip->mdio_bus = bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003303
3304 return 0;
3305
3306out:
Vivien Didelotfad09c72016-06-21 12:28:20 -04003307 if (chip->mdio_np)
3308 of_node_put(chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003309
3310 return err;
3311}
3312
Vivien Didelotfad09c72016-06-21 12:28:20 -04003313static void mv88e6xxx_mdio_unregister(struct mv88e6xxx_chip *chip)
Andrew Lunnb516d452016-06-04 21:17:06 +02003314
3315{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003316 struct mii_bus *bus = chip->mdio_bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003317
3318 mdiobus_unregister(bus);
3319
Vivien Didelotfad09c72016-06-21 12:28:20 -04003320 if (chip->mdio_np)
3321 of_node_put(chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003322}
3323
Guenter Roeckc22995c2015-07-25 09:42:28 -07003324#ifdef CONFIG_NET_DSA_HWMON
3325
3326static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
3327{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003328 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelot9c938292016-08-15 17:19:02 -04003329 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003330 int ret;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003331
3332 *temp = 0;
3333
Vivien Didelotfad09c72016-06-21 12:28:20 -04003334 mutex_lock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003335
Vivien Didelot9c938292016-08-15 17:19:02 -04003336 ret = mv88e6xxx_phy_write(chip, 0x0, 0x16, 0x6);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003337 if (ret < 0)
3338 goto error;
3339
3340 /* Enable temperature sensor */
Vivien Didelot9c938292016-08-15 17:19:02 -04003341 ret = mv88e6xxx_phy_read(chip, 0x0, 0x1a, &val);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003342 if (ret < 0)
3343 goto error;
3344
Vivien Didelot9c938292016-08-15 17:19:02 -04003345 ret = mv88e6xxx_phy_write(chip, 0x0, 0x1a, val | (1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003346 if (ret < 0)
3347 goto error;
3348
3349 /* Wait for temperature to stabilize */
3350 usleep_range(10000, 12000);
3351
Vivien Didelot9c938292016-08-15 17:19:02 -04003352 ret = mv88e6xxx_phy_read(chip, 0x0, 0x1a, &val);
3353 if (ret < 0)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003354 goto error;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003355
3356 /* Disable temperature sensor */
Vivien Didelot9c938292016-08-15 17:19:02 -04003357 ret = mv88e6xxx_phy_write(chip, 0x0, 0x1a, val & ~(1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003358 if (ret < 0)
3359 goto error;
3360
3361 *temp = ((val & 0x1f) - 5) * 5;
3362
3363error:
Vivien Didelot9c938292016-08-15 17:19:02 -04003364 mv88e6xxx_phy_write(chip, 0x0, 0x16, 0x0);
Vivien Didelotfad09c72016-06-21 12:28:20 -04003365 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003366 return ret;
3367}
3368
3369static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
3370{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003371 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3372 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003373 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003374 int ret;
3375
3376 *temp = 0;
3377
Vivien Didelot9c938292016-08-15 17:19:02 -04003378 mutex_lock(&chip->reg_lock);
3379 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 27, &val);
3380 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003381 if (ret < 0)
3382 return ret;
3383
Vivien Didelot9c938292016-08-15 17:19:02 -04003384 *temp = (val & 0xff) - 25;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003385
3386 return 0;
3387}
3388
Vivien Didelotf81ec902016-05-09 13:22:58 -04003389static int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003390{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003391 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunn158bc062016-04-28 21:24:06 -04003392
Vivien Didelotfad09c72016-06-21 12:28:20 -04003393 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP))
Vivien Didelot6594f612016-05-09 13:22:42 -04003394 return -EOPNOTSUPP;
3395
Vivien Didelotfad09c72016-06-21 12:28:20 -04003396 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003397 return mv88e63xx_get_temp(ds, temp);
3398
3399 return mv88e61xx_get_temp(ds, temp);
3400}
3401
Vivien Didelotf81ec902016-05-09 13:22:58 -04003402static int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003403{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003404 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3405 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003406 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003407 int ret;
3408
Vivien Didelotfad09c72016-06-21 12:28:20 -04003409 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003410 return -EOPNOTSUPP;
3411
3412 *temp = 0;
3413
Vivien Didelot9c938292016-08-15 17:19:02 -04003414 mutex_lock(&chip->reg_lock);
3415 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3416 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003417 if (ret < 0)
3418 return ret;
3419
Vivien Didelot9c938292016-08-15 17:19:02 -04003420 *temp = (((val >> 8) & 0x1f) * 5) - 25;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003421
3422 return 0;
3423}
3424
Vivien Didelotf81ec902016-05-09 13:22:58 -04003425static int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003426{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003427 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3428 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003429 u16 val;
3430 int err;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003431
Vivien Didelotfad09c72016-06-21 12:28:20 -04003432 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003433 return -EOPNOTSUPP;
3434
Vivien Didelot9c938292016-08-15 17:19:02 -04003435 mutex_lock(&chip->reg_lock);
3436 err = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3437 if (err)
3438 goto unlock;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003439 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
Vivien Didelot9c938292016-08-15 17:19:02 -04003440 err = mv88e6xxx_phy_page_write(chip, phy, 6, 26,
3441 (val & 0xe0ff) | (temp << 8));
3442unlock:
3443 mutex_unlock(&chip->reg_lock);
3444
3445 return err;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003446}
3447
Vivien Didelotf81ec902016-05-09 13:22:58 -04003448static int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003449{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003450 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3451 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003452 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003453 int ret;
3454
Vivien Didelotfad09c72016-06-21 12:28:20 -04003455 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003456 return -EOPNOTSUPP;
3457
3458 *alarm = false;
3459
Vivien Didelot9c938292016-08-15 17:19:02 -04003460 mutex_lock(&chip->reg_lock);
3461 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3462 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003463 if (ret < 0)
3464 return ret;
3465
Vivien Didelot9c938292016-08-15 17:19:02 -04003466 *alarm = !!(val & 0x40);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003467
3468 return 0;
3469}
3470#endif /* CONFIG_NET_DSA_HWMON */
3471
Vivien Didelot855b1932016-07-20 18:18:35 -04003472static int mv88e6xxx_get_eeprom_len(struct dsa_switch *ds)
3473{
3474 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3475
3476 return chip->eeprom_len;
3477}
3478
3479static int mv88e6xxx_get_eeprom16(struct mv88e6xxx_chip *chip,
3480 struct ethtool_eeprom *eeprom, u8 *data)
3481{
3482 unsigned int offset = eeprom->offset;
3483 unsigned int len = eeprom->len;
3484 u16 val;
3485 int err;
3486
3487 eeprom->len = 0;
3488
3489 if (offset & 1) {
3490 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
3491 if (err)
3492 return err;
3493
3494 *data++ = (val >> 8) & 0xff;
3495
3496 offset++;
3497 len--;
3498 eeprom->len++;
3499 }
3500
3501 while (len >= 2) {
3502 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
3503 if (err)
3504 return err;
3505
3506 *data++ = val & 0xff;
3507 *data++ = (val >> 8) & 0xff;
3508
3509 offset += 2;
3510 len -= 2;
3511 eeprom->len += 2;
3512 }
3513
3514 if (len) {
3515 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
3516 if (err)
3517 return err;
3518
3519 *data++ = val & 0xff;
3520
3521 offset++;
3522 len--;
3523 eeprom->len++;
3524 }
3525
3526 return 0;
3527}
3528
3529static int mv88e6xxx_get_eeprom(struct dsa_switch *ds,
3530 struct ethtool_eeprom *eeprom, u8 *data)
3531{
3532 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3533 int err;
3534
3535 mutex_lock(&chip->reg_lock);
3536
3537 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16))
3538 err = mv88e6xxx_get_eeprom16(chip, eeprom, data);
3539 else
3540 err = -EOPNOTSUPP;
3541
3542 mutex_unlock(&chip->reg_lock);
3543
3544 if (err)
3545 return err;
3546
3547 eeprom->magic = 0xc3ec4951;
3548
3549 return 0;
3550}
3551
3552static int mv88e6xxx_set_eeprom16(struct mv88e6xxx_chip *chip,
3553 struct ethtool_eeprom *eeprom, u8 *data)
3554{
3555 unsigned int offset = eeprom->offset;
3556 unsigned int len = eeprom->len;
3557 u16 val;
3558 int err;
3559
3560 /* Ensure the RO WriteEn bit is set */
3561 err = mv88e6xxx_read(chip, REG_GLOBAL2, GLOBAL2_EEPROM_CMD, &val);
3562 if (err)
3563 return err;
3564
3565 if (!(val & GLOBAL2_EEPROM_CMD_WRITE_EN))
3566 return -EROFS;
3567
3568 eeprom->len = 0;
3569
3570 if (offset & 1) {
3571 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
3572 if (err)
3573 return err;
3574
3575 val = (*data++ << 8) | (val & 0xff);
3576
3577 err = mv88e6xxx_g2_eeprom_write16(chip, offset >> 1, val);
3578 if (err)
3579 return err;
3580
3581 offset++;
3582 len--;
3583 eeprom->len++;
3584 }
3585
3586 while (len >= 2) {
3587 val = *data++;
3588 val |= *data++ << 8;
3589
3590 err = mv88e6xxx_g2_eeprom_write16(chip, offset >> 1, val);
3591 if (err)
3592 return err;
3593
3594 offset += 2;
3595 len -= 2;
3596 eeprom->len += 2;
3597 }
3598
3599 if (len) {
3600 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
3601 if (err)
3602 return err;
3603
3604 val = (val & 0xff00) | *data++;
3605
3606 err = mv88e6xxx_g2_eeprom_write16(chip, offset >> 1, val);
3607 if (err)
3608 return err;
3609
3610 offset++;
3611 len--;
3612 eeprom->len++;
3613 }
3614
3615 return 0;
3616}
3617
3618static int mv88e6xxx_set_eeprom(struct dsa_switch *ds,
3619 struct ethtool_eeprom *eeprom, u8 *data)
3620{
3621 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3622 int err;
3623
3624 if (eeprom->magic != 0xc3ec4951)
3625 return -EINVAL;
3626
3627 mutex_lock(&chip->reg_lock);
3628
3629 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16))
3630 err = mv88e6xxx_set_eeprom16(chip, eeprom, data);
3631 else
3632 err = -EOPNOTSUPP;
3633
3634 mutex_unlock(&chip->reg_lock);
3635
3636 return err;
3637}
3638
Vivien Didelotf81ec902016-05-09 13:22:58 -04003639static const struct mv88e6xxx_info mv88e6xxx_table[] = {
3640 [MV88E6085] = {
3641 .prod_num = PORT_SWITCH_ID_PROD_NUM_6085,
3642 .family = MV88E6XXX_FAMILY_6097,
3643 .name = "Marvell 88E6085",
3644 .num_databases = 4096,
3645 .num_ports = 10,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003646 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003647 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003648 .flags = MV88E6XXX_FLAGS_FAMILY_6097,
3649 },
3650
3651 [MV88E6095] = {
3652 .prod_num = PORT_SWITCH_ID_PROD_NUM_6095,
3653 .family = MV88E6XXX_FAMILY_6095,
3654 .name = "Marvell 88E6095/88E6095F",
3655 .num_databases = 256,
3656 .num_ports = 11,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003657 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003658 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003659 .flags = MV88E6XXX_FLAGS_FAMILY_6095,
3660 },
3661
3662 [MV88E6123] = {
3663 .prod_num = PORT_SWITCH_ID_PROD_NUM_6123,
3664 .family = MV88E6XXX_FAMILY_6165,
3665 .name = "Marvell 88E6123",
3666 .num_databases = 4096,
3667 .num_ports = 3,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003668 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003669 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003670 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3671 },
3672
3673 [MV88E6131] = {
3674 .prod_num = PORT_SWITCH_ID_PROD_NUM_6131,
3675 .family = MV88E6XXX_FAMILY_6185,
3676 .name = "Marvell 88E6131",
3677 .num_databases = 256,
3678 .num_ports = 8,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003679 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003680 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003681 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3682 },
3683
3684 [MV88E6161] = {
3685 .prod_num = PORT_SWITCH_ID_PROD_NUM_6161,
3686 .family = MV88E6XXX_FAMILY_6165,
3687 .name = "Marvell 88E6161",
3688 .num_databases = 4096,
3689 .num_ports = 6,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003690 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003691 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003692 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3693 },
3694
3695 [MV88E6165] = {
3696 .prod_num = PORT_SWITCH_ID_PROD_NUM_6165,
3697 .family = MV88E6XXX_FAMILY_6165,
3698 .name = "Marvell 88E6165",
3699 .num_databases = 4096,
3700 .num_ports = 6,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003701 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003702 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003703 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3704 },
3705
3706 [MV88E6171] = {
3707 .prod_num = PORT_SWITCH_ID_PROD_NUM_6171,
3708 .family = MV88E6XXX_FAMILY_6351,
3709 .name = "Marvell 88E6171",
3710 .num_databases = 4096,
3711 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003712 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003713 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003714 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3715 },
3716
3717 [MV88E6172] = {
3718 .prod_num = PORT_SWITCH_ID_PROD_NUM_6172,
3719 .family = MV88E6XXX_FAMILY_6352,
3720 .name = "Marvell 88E6172",
3721 .num_databases = 4096,
3722 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003723 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003724 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003725 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3726 },
3727
3728 [MV88E6175] = {
3729 .prod_num = PORT_SWITCH_ID_PROD_NUM_6175,
3730 .family = MV88E6XXX_FAMILY_6351,
3731 .name = "Marvell 88E6175",
3732 .num_databases = 4096,
3733 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003734 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003735 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003736 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3737 },
3738
3739 [MV88E6176] = {
3740 .prod_num = PORT_SWITCH_ID_PROD_NUM_6176,
3741 .family = MV88E6XXX_FAMILY_6352,
3742 .name = "Marvell 88E6176",
3743 .num_databases = 4096,
3744 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003745 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003746 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003747 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3748 },
3749
3750 [MV88E6185] = {
3751 .prod_num = PORT_SWITCH_ID_PROD_NUM_6185,
3752 .family = MV88E6XXX_FAMILY_6185,
3753 .name = "Marvell 88E6185",
3754 .num_databases = 256,
3755 .num_ports = 10,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003756 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003757 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003758 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3759 },
3760
3761 [MV88E6240] = {
3762 .prod_num = PORT_SWITCH_ID_PROD_NUM_6240,
3763 .family = MV88E6XXX_FAMILY_6352,
3764 .name = "Marvell 88E6240",
3765 .num_databases = 4096,
3766 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003767 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003768 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003769 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3770 },
3771
3772 [MV88E6320] = {
3773 .prod_num = PORT_SWITCH_ID_PROD_NUM_6320,
3774 .family = MV88E6XXX_FAMILY_6320,
3775 .name = "Marvell 88E6320",
3776 .num_databases = 4096,
3777 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003778 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003779 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003780 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3781 },
3782
3783 [MV88E6321] = {
3784 .prod_num = PORT_SWITCH_ID_PROD_NUM_6321,
3785 .family = MV88E6XXX_FAMILY_6320,
3786 .name = "Marvell 88E6321",
3787 .num_databases = 4096,
3788 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003789 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003790 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003791 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3792 },
3793
3794 [MV88E6350] = {
3795 .prod_num = PORT_SWITCH_ID_PROD_NUM_6350,
3796 .family = MV88E6XXX_FAMILY_6351,
3797 .name = "Marvell 88E6350",
3798 .num_databases = 4096,
3799 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003800 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003801 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003802 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3803 },
3804
3805 [MV88E6351] = {
3806 .prod_num = PORT_SWITCH_ID_PROD_NUM_6351,
3807 .family = MV88E6XXX_FAMILY_6351,
3808 .name = "Marvell 88E6351",
3809 .num_databases = 4096,
3810 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003811 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003812 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003813 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3814 },
3815
3816 [MV88E6352] = {
3817 .prod_num = PORT_SWITCH_ID_PROD_NUM_6352,
3818 .family = MV88E6XXX_FAMILY_6352,
3819 .name = "Marvell 88E6352",
3820 .num_databases = 4096,
3821 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003822 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003823 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003824 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3825 },
3826};
3827
Vivien Didelot5f7c0362016-06-20 13:14:04 -04003828static const struct mv88e6xxx_info *mv88e6xxx_lookup_info(unsigned int prod_num)
Vivien Didelotb9b37712015-10-30 19:39:48 -04003829{
Vivien Didelota439c062016-04-17 13:23:58 -04003830 int i;
Vivien Didelotb9b37712015-10-30 19:39:48 -04003831
Vivien Didelot5f7c0362016-06-20 13:14:04 -04003832 for (i = 0; i < ARRAY_SIZE(mv88e6xxx_table); ++i)
3833 if (mv88e6xxx_table[i].prod_num == prod_num)
3834 return &mv88e6xxx_table[i];
Vivien Didelotb9b37712015-10-30 19:39:48 -04003835
Vivien Didelotb9b37712015-10-30 19:39:48 -04003836 return NULL;
3837}
3838
Vivien Didelotfad09c72016-06-21 12:28:20 -04003839static int mv88e6xxx_detect(struct mv88e6xxx_chip *chip)
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003840{
3841 const struct mv88e6xxx_info *info;
Vivien Didelot8f6345b2016-07-20 18:18:36 -04003842 unsigned int prod_num, rev;
3843 u16 id;
3844 int err;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003845
Vivien Didelot8f6345b2016-07-20 18:18:36 -04003846 mutex_lock(&chip->reg_lock);
3847 err = mv88e6xxx_port_read(chip, 0, PORT_SWITCH_ID, &id);
3848 mutex_unlock(&chip->reg_lock);
3849 if (err)
3850 return err;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003851
3852 prod_num = (id & 0xfff0) >> 4;
3853 rev = id & 0x000f;
3854
3855 info = mv88e6xxx_lookup_info(prod_num);
3856 if (!info)
3857 return -ENODEV;
3858
Vivien Didelotcaac8542016-06-20 13:14:09 -04003859 /* Update the compatible info with the probed one */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003860 chip->info = info;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003861
Vivien Didelotfad09c72016-06-21 12:28:20 -04003862 dev_info(chip->dev, "switch 0x%x detected: %s, revision %u\n",
3863 chip->info->prod_num, chip->info->name, rev);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003864
3865 return 0;
3866}
3867
Vivien Didelotfad09c72016-06-21 12:28:20 -04003868static struct mv88e6xxx_chip *mv88e6xxx_alloc_chip(struct device *dev)
Vivien Didelot469d7292016-06-20 13:14:06 -04003869{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003870 struct mv88e6xxx_chip *chip;
Vivien Didelot469d7292016-06-20 13:14:06 -04003871
Vivien Didelotfad09c72016-06-21 12:28:20 -04003872 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
3873 if (!chip)
Vivien Didelot469d7292016-06-20 13:14:06 -04003874 return NULL;
3875
Vivien Didelotfad09c72016-06-21 12:28:20 -04003876 chip->dev = dev;
Vivien Didelot469d7292016-06-20 13:14:06 -04003877
Vivien Didelotfad09c72016-06-21 12:28:20 -04003878 mutex_init(&chip->reg_lock);
Vivien Didelot469d7292016-06-20 13:14:06 -04003879
Vivien Didelotfad09c72016-06-21 12:28:20 -04003880 return chip;
Vivien Didelot469d7292016-06-20 13:14:06 -04003881}
3882
Vivien Didelote57e5e72016-08-15 17:19:00 -04003883static const struct mv88e6xxx_ops mv88e6xxx_phy_ops = {
3884 .read = mv88e6xxx_read,
3885 .write = mv88e6xxx_write,
3886};
3887
3888static void mv88e6xxx_phy_init(struct mv88e6xxx_chip *chip)
3889{
3890 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_SMI_PHY)) {
3891 chip->phy_ops = &mv88e6xxx_g2_smi_phy_ops;
3892 } else if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU)) {
3893 chip->phy_ops = &mv88e6xxx_phy_ppu_ops;
3894 mv88e6xxx_ppu_state_init(chip);
3895 } else {
3896 chip->phy_ops = &mv88e6xxx_phy_ops;
3897 }
3898}
3899
Andrew Lunn930188c2016-08-22 16:01:03 +02003900static void mv88e6xxx_phy_destroy(struct mv88e6xxx_chip *chip)
3901{
3902 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU)) {
3903 mv88e6xxx_ppu_state_destroy(chip);
3904 }
3905}
3906
Vivien Didelotfad09c72016-06-21 12:28:20 -04003907static int mv88e6xxx_smi_init(struct mv88e6xxx_chip *chip,
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003908 struct mii_bus *bus, int sw_addr)
3909{
3910 /* ADDR[0] pin is unavailable externally and considered zero */
3911 if (sw_addr & 0x1)
3912 return -EINVAL;
3913
Vivien Didelot914b32f2016-06-20 13:14:11 -04003914 if (sw_addr == 0)
Vivien Didelotfad09c72016-06-21 12:28:20 -04003915 chip->smi_ops = &mv88e6xxx_smi_single_chip_ops;
Vivien Didelota0ffff22016-08-15 17:18:58 -04003916 else if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_MULTI_CHIP))
Vivien Didelotfad09c72016-06-21 12:28:20 -04003917 chip->smi_ops = &mv88e6xxx_smi_multi_chip_ops;
Vivien Didelot914b32f2016-06-20 13:14:11 -04003918 else
3919 return -EINVAL;
3920
Vivien Didelotfad09c72016-06-21 12:28:20 -04003921 chip->bus = bus;
3922 chip->sw_addr = sw_addr;
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003923
3924 return 0;
3925}
3926
Andrew Lunn7b314362016-08-22 16:01:01 +02003927static enum dsa_tag_protocol mv88e6xxx_get_tag_protocol(struct dsa_switch *ds)
3928{
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02003929 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3930
3931 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA))
3932 return DSA_TAG_PROTO_EDSA;
3933
3934 return DSA_TAG_PROTO_DSA;
Andrew Lunn7b314362016-08-22 16:01:01 +02003935}
3936
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02003937static const char *mv88e6xxx_drv_probe(struct device *dsa_dev,
3938 struct device *host_dev, int sw_addr,
3939 void **priv)
Andrew Lunna77d43f2016-04-13 02:40:42 +02003940{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003941 struct mv88e6xxx_chip *chip;
Vivien Didelota439c062016-04-17 13:23:58 -04003942 struct mii_bus *bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003943 int err;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003944
Vivien Didelota439c062016-04-17 13:23:58 -04003945 bus = dsa_host_dev_to_mii_bus(host_dev);
Andrew Lunnc1569132016-04-13 02:40:45 +02003946 if (!bus)
3947 return NULL;
3948
Vivien Didelotfad09c72016-06-21 12:28:20 -04003949 chip = mv88e6xxx_alloc_chip(dsa_dev);
3950 if (!chip)
Vivien Didelot469d7292016-06-20 13:14:06 -04003951 return NULL;
3952
Vivien Didelotcaac8542016-06-20 13:14:09 -04003953 /* Legacy SMI probing will only support chips similar to 88E6085 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003954 chip->info = &mv88e6xxx_table[MV88E6085];
Vivien Didelotcaac8542016-06-20 13:14:09 -04003955
Vivien Didelotfad09c72016-06-21 12:28:20 -04003956 err = mv88e6xxx_smi_init(chip, bus, sw_addr);
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003957 if (err)
3958 goto free;
3959
Vivien Didelotfad09c72016-06-21 12:28:20 -04003960 err = mv88e6xxx_detect(chip);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003961 if (err)
Vivien Didelot469d7292016-06-20 13:14:06 -04003962 goto free;
Vivien Didelota439c062016-04-17 13:23:58 -04003963
Vivien Didelote57e5e72016-08-15 17:19:00 -04003964 mv88e6xxx_phy_init(chip);
3965
Vivien Didelotfad09c72016-06-21 12:28:20 -04003966 err = mv88e6xxx_mdio_register(chip, NULL);
Andrew Lunnb516d452016-06-04 21:17:06 +02003967 if (err)
Vivien Didelot469d7292016-06-20 13:14:06 -04003968 goto free;
Andrew Lunnb516d452016-06-04 21:17:06 +02003969
Vivien Didelotfad09c72016-06-21 12:28:20 -04003970 *priv = chip;
Vivien Didelota439c062016-04-17 13:23:58 -04003971
Vivien Didelotfad09c72016-06-21 12:28:20 -04003972 return chip->info->name;
Vivien Didelot469d7292016-06-20 13:14:06 -04003973free:
Vivien Didelotfad09c72016-06-21 12:28:20 -04003974 devm_kfree(dsa_dev, chip);
Vivien Didelot469d7292016-06-20 13:14:06 -04003975
3976 return NULL;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003977}
3978
Vivien Didelot9d490b42016-08-23 12:38:56 -04003979static struct dsa_switch_ops mv88e6xxx_switch_ops = {
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02003980 .probe = mv88e6xxx_drv_probe,
Andrew Lunn7b314362016-08-22 16:01:01 +02003981 .get_tag_protocol = mv88e6xxx_get_tag_protocol,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003982 .setup = mv88e6xxx_setup,
3983 .set_addr = mv88e6xxx_set_addr,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003984 .adjust_link = mv88e6xxx_adjust_link,
3985 .get_strings = mv88e6xxx_get_strings,
3986 .get_ethtool_stats = mv88e6xxx_get_ethtool_stats,
3987 .get_sset_count = mv88e6xxx_get_sset_count,
3988 .set_eee = mv88e6xxx_set_eee,
3989 .get_eee = mv88e6xxx_get_eee,
3990#ifdef CONFIG_NET_DSA_HWMON
3991 .get_temp = mv88e6xxx_get_temp,
3992 .get_temp_limit = mv88e6xxx_get_temp_limit,
3993 .set_temp_limit = mv88e6xxx_set_temp_limit,
3994 .get_temp_alarm = mv88e6xxx_get_temp_alarm,
3995#endif
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003996 .get_eeprom_len = mv88e6xxx_get_eeprom_len,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003997 .get_eeprom = mv88e6xxx_get_eeprom,
3998 .set_eeprom = mv88e6xxx_set_eeprom,
3999 .get_regs_len = mv88e6xxx_get_regs_len,
4000 .get_regs = mv88e6xxx_get_regs,
Vivien Didelot2cfcd962016-07-18 20:45:40 -04004001 .set_ageing_time = mv88e6xxx_set_ageing_time,
Vivien Didelotf81ec902016-05-09 13:22:58 -04004002 .port_bridge_join = mv88e6xxx_port_bridge_join,
4003 .port_bridge_leave = mv88e6xxx_port_bridge_leave,
4004 .port_stp_state_set = mv88e6xxx_port_stp_state_set,
4005 .port_vlan_filtering = mv88e6xxx_port_vlan_filtering,
4006 .port_vlan_prepare = mv88e6xxx_port_vlan_prepare,
4007 .port_vlan_add = mv88e6xxx_port_vlan_add,
4008 .port_vlan_del = mv88e6xxx_port_vlan_del,
4009 .port_vlan_dump = mv88e6xxx_port_vlan_dump,
4010 .port_fdb_prepare = mv88e6xxx_port_fdb_prepare,
4011 .port_fdb_add = mv88e6xxx_port_fdb_add,
4012 .port_fdb_del = mv88e6xxx_port_fdb_del,
4013 .port_fdb_dump = mv88e6xxx_port_fdb_dump,
4014};
4015
Vivien Didelotfad09c72016-06-21 12:28:20 -04004016static int mv88e6xxx_register_switch(struct mv88e6xxx_chip *chip,
Vivien Didelotb7e66a52016-06-20 13:14:02 -04004017 struct device_node *np)
4018{
Vivien Didelotfad09c72016-06-21 12:28:20 -04004019 struct device *dev = chip->dev;
Vivien Didelotb7e66a52016-06-20 13:14:02 -04004020 struct dsa_switch *ds;
4021
4022 ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
4023 if (!ds)
4024 return -ENOMEM;
4025
4026 ds->dev = dev;
Vivien Didelotfad09c72016-06-21 12:28:20 -04004027 ds->priv = chip;
Vivien Didelot9d490b42016-08-23 12:38:56 -04004028 ds->ops = &mv88e6xxx_switch_ops;
Vivien Didelotb7e66a52016-06-20 13:14:02 -04004029
4030 dev_set_drvdata(dev, ds);
4031
4032 return dsa_register_switch(ds, np);
4033}
4034
Vivien Didelotfad09c72016-06-21 12:28:20 -04004035static void mv88e6xxx_unregister_switch(struct mv88e6xxx_chip *chip)
Vivien Didelotb7e66a52016-06-20 13:14:02 -04004036{
Vivien Didelotfad09c72016-06-21 12:28:20 -04004037 dsa_unregister_switch(chip->ds);
Vivien Didelotb7e66a52016-06-20 13:14:02 -04004038}
4039
Vivien Didelot57d32312016-06-20 13:13:58 -04004040static int mv88e6xxx_probe(struct mdio_device *mdiodev)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004041{
4042 struct device *dev = &mdiodev->dev;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02004043 struct device_node *np = dev->of_node;
Vivien Didelotcaac8542016-06-20 13:14:09 -04004044 const struct mv88e6xxx_info *compat_info;
Vivien Didelotfad09c72016-06-21 12:28:20 -04004045 struct mv88e6xxx_chip *chip;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02004046 u32 eeprom_len;
Andrew Lunn52638f72016-05-10 23:27:22 +02004047 int err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004048
Vivien Didelotcaac8542016-06-20 13:14:09 -04004049 compat_info = of_device_get_match_data(dev);
4050 if (!compat_info)
4051 return -EINVAL;
4052
Vivien Didelotfad09c72016-06-21 12:28:20 -04004053 chip = mv88e6xxx_alloc_chip(dev);
4054 if (!chip)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004055 return -ENOMEM;
4056
Vivien Didelotfad09c72016-06-21 12:28:20 -04004057 chip->info = compat_info;
Vivien Didelotcaac8542016-06-20 13:14:09 -04004058
Vivien Didelotfad09c72016-06-21 12:28:20 -04004059 err = mv88e6xxx_smi_init(chip, mdiodev->bus, mdiodev->addr);
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04004060 if (err)
4061 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004062
Vivien Didelotfad09c72016-06-21 12:28:20 -04004063 err = mv88e6xxx_detect(chip);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04004064 if (err)
4065 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004066
Vivien Didelote57e5e72016-08-15 17:19:00 -04004067 mv88e6xxx_phy_init(chip);
4068
Vivien Didelotfad09c72016-06-21 12:28:20 -04004069 chip->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_ASIS);
4070 if (IS_ERR(chip->reset))
4071 return PTR_ERR(chip->reset);
Andrew Lunn52638f72016-05-10 23:27:22 +02004072
Vivien Didelot855b1932016-07-20 18:18:35 -04004073 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16) &&
Andrew Lunnf8cd8752016-05-10 23:27:25 +02004074 !of_property_read_u32(np, "eeprom-length", &eeprom_len))
Vivien Didelotfad09c72016-06-21 12:28:20 -04004075 chip->eeprom_len = eeprom_len;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02004076
Vivien Didelotfad09c72016-06-21 12:28:20 -04004077 err = mv88e6xxx_mdio_register(chip, np);
Andrew Lunnb516d452016-06-04 21:17:06 +02004078 if (err)
4079 return err;
4080
Vivien Didelotfad09c72016-06-21 12:28:20 -04004081 err = mv88e6xxx_register_switch(chip, np);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02004082 if (err) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04004083 mv88e6xxx_mdio_unregister(chip);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02004084 return err;
4085 }
4086
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004087 return 0;
4088}
4089
4090static void mv88e6xxx_remove(struct mdio_device *mdiodev)
4091{
4092 struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev);
Vivien Didelotfad09c72016-06-21 12:28:20 -04004093 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004094
Andrew Lunn930188c2016-08-22 16:01:03 +02004095 mv88e6xxx_phy_destroy(chip);
Vivien Didelotfad09c72016-06-21 12:28:20 -04004096 mv88e6xxx_unregister_switch(chip);
4097 mv88e6xxx_mdio_unregister(chip);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004098}
4099
4100static const struct of_device_id mv88e6xxx_of_match[] = {
Vivien Didelotcaac8542016-06-20 13:14:09 -04004101 {
4102 .compatible = "marvell,mv88e6085",
4103 .data = &mv88e6xxx_table[MV88E6085],
4104 },
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004105 { /* sentinel */ },
4106};
4107
4108MODULE_DEVICE_TABLE(of, mv88e6xxx_of_match);
4109
4110static struct mdio_driver mv88e6xxx_driver = {
4111 .probe = mv88e6xxx_probe,
4112 .remove = mv88e6xxx_remove,
4113 .mdiodrv.driver = {
4114 .name = "mv88e6085",
4115 .of_match_table = mv88e6xxx_of_match,
4116 },
4117};
4118
Ben Hutchings98e67302011-11-25 14:36:19 +00004119static int __init mv88e6xxx_init(void)
4120{
Vivien Didelot9d490b42016-08-23 12:38:56 -04004121 register_switch_driver(&mv88e6xxx_switch_ops);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004122 return mdio_driver_register(&mv88e6xxx_driver);
Ben Hutchings98e67302011-11-25 14:36:19 +00004123}
4124module_init(mv88e6xxx_init);
4125
4126static void __exit mv88e6xxx_cleanup(void)
4127{
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004128 mdio_driver_unregister(&mv88e6xxx_driver);
Vivien Didelot9d490b42016-08-23 12:38:56 -04004129 unregister_switch_driver(&mv88e6xxx_switch_ops);
Ben Hutchings98e67302011-11-25 14:36:19 +00004130}
4131module_exit(mv88e6xxx_cleanup);
Ben Hutchings3d825ed2011-11-25 14:37:16 +00004132
4133MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
4134MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
4135MODULE_LICENSE("GPL");