blob: 63cad6c00bc775693a206a2f1a475fd589901ec2 [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
Vivien Didelote57e5e72016-08-15 17:19:00 -0400489static int mv88e6xxx_phy_ppu_read(struct mv88e6xxx_chip *chip, int addr,
490 int reg, u16 *val)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000491{
Vivien Didelote57e5e72016-08-15 17:19:00 -0400492 int err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000493
Vivien Didelote57e5e72016-08-15 17:19:00 -0400494 err = mv88e6xxx_ppu_access_get(chip);
495 if (!err) {
496 err = mv88e6xxx_read(chip, addr, reg, val);
Vivien Didelotfad09c72016-06-21 12:28:20 -0400497 mv88e6xxx_ppu_access_put(chip);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000498 }
499
Vivien Didelote57e5e72016-08-15 17:19:00 -0400500 return err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000501}
502
Vivien Didelote57e5e72016-08-15 17:19:00 -0400503static int mv88e6xxx_phy_ppu_write(struct mv88e6xxx_chip *chip, int addr,
504 int reg, u16 val)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000505{
Vivien Didelote57e5e72016-08-15 17:19:00 -0400506 int err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000507
Vivien Didelote57e5e72016-08-15 17:19:00 -0400508 err = mv88e6xxx_ppu_access_get(chip);
509 if (!err) {
510 err = mv88e6xxx_write(chip, addr, reg, val);
Vivien Didelotfad09c72016-06-21 12:28:20 -0400511 mv88e6xxx_ppu_access_put(chip);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000512 }
513
Vivien Didelote57e5e72016-08-15 17:19:00 -0400514 return err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000515}
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000516
Vivien Didelote57e5e72016-08-15 17:19:00 -0400517static const struct mv88e6xxx_ops mv88e6xxx_phy_ppu_ops = {
518 .read = mv88e6xxx_phy_ppu_read,
519 .write = mv88e6xxx_phy_ppu_write,
520};
521
Vivien Didelotfad09c72016-06-21 12:28:20 -0400522static bool mv88e6xxx_6065_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200523{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400524 return chip->info->family == MV88E6XXX_FAMILY_6065;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200525}
526
Vivien Didelotfad09c72016-06-21 12:28:20 -0400527static bool mv88e6xxx_6095_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_6095;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200530}
531
Vivien Didelotfad09c72016-06-21 12:28:20 -0400532static bool mv88e6xxx_6097_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_6097;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200535}
536
Vivien Didelotfad09c72016-06-21 12:28:20 -0400537static bool mv88e6xxx_6165_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_6165;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200540}
541
Vivien Didelotfad09c72016-06-21 12:28:20 -0400542static bool mv88e6xxx_6185_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_6185;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200545}
546
Vivien Didelotfad09c72016-06-21 12:28:20 -0400547static bool mv88e6xxx_6320_family(struct mv88e6xxx_chip *chip)
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700548{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400549 return chip->info->family == MV88E6XXX_FAMILY_6320;
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700550}
551
Vivien Didelotfad09c72016-06-21 12:28:20 -0400552static bool mv88e6xxx_6351_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200553{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400554 return chip->info->family == MV88E6XXX_FAMILY_6351;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200555}
556
Vivien Didelotfad09c72016-06-21 12:28:20 -0400557static bool mv88e6xxx_6352_family(struct mv88e6xxx_chip *chip)
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200558{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400559 return chip->info->family == MV88E6XXX_FAMILY_6352;
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200560}
561
Vivien Didelotfad09c72016-06-21 12:28:20 -0400562static unsigned int mv88e6xxx_num_databases(struct mv88e6xxx_chip *chip)
Vivien Didelotf74df0b2016-03-31 16:53:43 -0400563{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400564 return chip->info->num_databases;
Vivien Didelotf74df0b2016-03-31 16:53:43 -0400565}
566
Vivien Didelotfad09c72016-06-21 12:28:20 -0400567static bool mv88e6xxx_has_fid_reg(struct mv88e6xxx_chip *chip)
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400568{
569 /* Does the device have dedicated FID registers for ATU and VTU ops? */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400570 if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
571 mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip))
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400572 return true;
573
574 return false;
575}
576
Andrew Lunndea87022015-08-31 15:56:47 +0200577/* We expect the switch to perform auto negotiation if there is a real
578 * phy. However, in the case of a fixed link phy, we force the port
579 * settings from the fixed link settings.
580 */
Vivien Didelotf81ec902016-05-09 13:22:58 -0400581static void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port,
582 struct phy_device *phydev)
Andrew Lunndea87022015-08-31 15:56:47 +0200583{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400584 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunn49052872015-09-29 01:53:48 +0200585 u32 reg;
586 int ret;
Andrew Lunndea87022015-08-31 15:56:47 +0200587
588 if (!phy_is_pseudo_fixed_link(phydev))
589 return;
590
Vivien Didelotfad09c72016-06-21 12:28:20 -0400591 mutex_lock(&chip->reg_lock);
Andrew Lunndea87022015-08-31 15:56:47 +0200592
Vivien Didelotfad09c72016-06-21 12:28:20 -0400593 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_PCS_CTRL);
Andrew Lunndea87022015-08-31 15:56:47 +0200594 if (ret < 0)
595 goto out;
596
597 reg = ret & ~(PORT_PCS_CTRL_LINK_UP |
598 PORT_PCS_CTRL_FORCE_LINK |
599 PORT_PCS_CTRL_DUPLEX_FULL |
600 PORT_PCS_CTRL_FORCE_DUPLEX |
601 PORT_PCS_CTRL_UNFORCED);
602
603 reg |= PORT_PCS_CTRL_FORCE_LINK;
604 if (phydev->link)
Vivien Didelot57d32312016-06-20 13:13:58 -0400605 reg |= PORT_PCS_CTRL_LINK_UP;
Andrew Lunndea87022015-08-31 15:56:47 +0200606
Vivien Didelotfad09c72016-06-21 12:28:20 -0400607 if (mv88e6xxx_6065_family(chip) && phydev->speed > SPEED_100)
Andrew Lunndea87022015-08-31 15:56:47 +0200608 goto out;
609
610 switch (phydev->speed) {
611 case SPEED_1000:
612 reg |= PORT_PCS_CTRL_1000;
613 break;
614 case SPEED_100:
615 reg |= PORT_PCS_CTRL_100;
616 break;
617 case SPEED_10:
618 reg |= PORT_PCS_CTRL_10;
619 break;
620 default:
621 pr_info("Unknown speed");
622 goto out;
623 }
624
625 reg |= PORT_PCS_CTRL_FORCE_DUPLEX;
626 if (phydev->duplex == DUPLEX_FULL)
627 reg |= PORT_PCS_CTRL_DUPLEX_FULL;
628
Vivien Didelotfad09c72016-06-21 12:28:20 -0400629 if ((mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip)) &&
630 (port >= chip->info->num_ports - 2)) {
Andrew Lunne7e72ac2015-08-31 15:56:51 +0200631 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
632 reg |= PORT_PCS_CTRL_RGMII_DELAY_RXCLK;
633 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
634 reg |= PORT_PCS_CTRL_RGMII_DELAY_TXCLK;
635 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
636 reg |= (PORT_PCS_CTRL_RGMII_DELAY_RXCLK |
637 PORT_PCS_CTRL_RGMII_DELAY_TXCLK);
638 }
Vivien Didelotfad09c72016-06-21 12:28:20 -0400639 _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_PCS_CTRL, reg);
Andrew Lunndea87022015-08-31 15:56:47 +0200640
641out:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400642 mutex_unlock(&chip->reg_lock);
Andrew Lunndea87022015-08-31 15:56:47 +0200643}
644
Vivien Didelotfad09c72016-06-21 12:28:20 -0400645static int _mv88e6xxx_stats_wait(struct mv88e6xxx_chip *chip)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000646{
647 int ret;
648 int i;
649
650 for (i = 0; i < 10; i++) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400651 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_OP);
Andrew Lunncca8b132015-04-02 04:06:39 +0200652 if ((ret & GLOBAL_STATS_OP_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000653 return 0;
654 }
655
656 return -ETIMEDOUT;
657}
658
Vivien Didelotfad09c72016-06-21 12:28:20 -0400659static int _mv88e6xxx_stats_snapshot(struct mv88e6xxx_chip *chip, int port)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000660{
661 int ret;
662
Vivien Didelotfad09c72016-06-21 12:28:20 -0400663 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200664 port = (port + 1) << 5;
665
Barry Grussling3675c8d2013-01-08 16:05:53 +0000666 /* Snapshot the hardware statistics counters for this port. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400667 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
Andrew Lunn31888232015-05-06 01:09:54 +0200668 GLOBAL_STATS_OP_CAPTURE_PORT |
669 GLOBAL_STATS_OP_HIST_RX_TX | port);
670 if (ret < 0)
671 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000672
Barry Grussling3675c8d2013-01-08 16:05:53 +0000673 /* Wait for the snapshotting to complete. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400674 ret = _mv88e6xxx_stats_wait(chip);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000675 if (ret < 0)
676 return ret;
677
678 return 0;
679}
680
Vivien Didelotfad09c72016-06-21 12:28:20 -0400681static void _mv88e6xxx_stats_read(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -0400682 int stat, u32 *val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000683{
684 u32 _val;
685 int ret;
686
687 *val = 0;
688
Vivien Didelotfad09c72016-06-21 12:28:20 -0400689 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
Andrew Lunn31888232015-05-06 01:09:54 +0200690 GLOBAL_STATS_OP_READ_CAPTURED |
691 GLOBAL_STATS_OP_HIST_RX_TX | stat);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000692 if (ret < 0)
693 return;
694
Vivien Didelotfad09c72016-06-21 12:28:20 -0400695 ret = _mv88e6xxx_stats_wait(chip);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000696 if (ret < 0)
697 return;
698
Vivien Didelotfad09c72016-06-21 12:28:20 -0400699 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_COUNTER_32);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000700 if (ret < 0)
701 return;
702
703 _val = ret << 16;
704
Vivien Didelotfad09c72016-06-21 12:28:20 -0400705 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_COUNTER_01);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000706 if (ret < 0)
707 return;
708
709 *val = _val | ret;
710}
711
Andrew Lunne413e7e2015-04-02 04:06:38 +0200712static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100713 { "in_good_octets", 8, 0x00, BANK0, },
714 { "in_bad_octets", 4, 0x02, BANK0, },
715 { "in_unicast", 4, 0x04, BANK0, },
716 { "in_broadcasts", 4, 0x06, BANK0, },
717 { "in_multicasts", 4, 0x07, BANK0, },
718 { "in_pause", 4, 0x16, BANK0, },
719 { "in_undersize", 4, 0x18, BANK0, },
720 { "in_fragments", 4, 0x19, BANK0, },
721 { "in_oversize", 4, 0x1a, BANK0, },
722 { "in_jabber", 4, 0x1b, BANK0, },
723 { "in_rx_error", 4, 0x1c, BANK0, },
724 { "in_fcs_error", 4, 0x1d, BANK0, },
725 { "out_octets", 8, 0x0e, BANK0, },
726 { "out_unicast", 4, 0x10, BANK0, },
727 { "out_broadcasts", 4, 0x13, BANK0, },
728 { "out_multicasts", 4, 0x12, BANK0, },
729 { "out_pause", 4, 0x15, BANK0, },
730 { "excessive", 4, 0x11, BANK0, },
731 { "collisions", 4, 0x1e, BANK0, },
732 { "deferred", 4, 0x05, BANK0, },
733 { "single", 4, 0x14, BANK0, },
734 { "multiple", 4, 0x17, BANK0, },
735 { "out_fcs_error", 4, 0x03, BANK0, },
736 { "late", 4, 0x1f, BANK0, },
737 { "hist_64bytes", 4, 0x08, BANK0, },
738 { "hist_65_127bytes", 4, 0x09, BANK0, },
739 { "hist_128_255bytes", 4, 0x0a, BANK0, },
740 { "hist_256_511bytes", 4, 0x0b, BANK0, },
741 { "hist_512_1023bytes", 4, 0x0c, BANK0, },
742 { "hist_1024_max_bytes", 4, 0x0d, BANK0, },
743 { "sw_in_discards", 4, 0x10, PORT, },
744 { "sw_in_filtered", 2, 0x12, PORT, },
745 { "sw_out_filtered", 2, 0x13, PORT, },
746 { "in_discards", 4, 0x00 | GLOBAL_STATS_OP_BANK_1, BANK1, },
747 { "in_filtered", 4, 0x01 | GLOBAL_STATS_OP_BANK_1, BANK1, },
748 { "in_accepted", 4, 0x02 | GLOBAL_STATS_OP_BANK_1, BANK1, },
749 { "in_bad_accepted", 4, 0x03 | GLOBAL_STATS_OP_BANK_1, BANK1, },
750 { "in_good_avb_class_a", 4, 0x04 | GLOBAL_STATS_OP_BANK_1, BANK1, },
751 { "in_good_avb_class_b", 4, 0x05 | GLOBAL_STATS_OP_BANK_1, BANK1, },
752 { "in_bad_avb_class_a", 4, 0x06 | GLOBAL_STATS_OP_BANK_1, BANK1, },
753 { "in_bad_avb_class_b", 4, 0x07 | GLOBAL_STATS_OP_BANK_1, BANK1, },
754 { "tcam_counter_0", 4, 0x08 | GLOBAL_STATS_OP_BANK_1, BANK1, },
755 { "tcam_counter_1", 4, 0x09 | GLOBAL_STATS_OP_BANK_1, BANK1, },
756 { "tcam_counter_2", 4, 0x0a | GLOBAL_STATS_OP_BANK_1, BANK1, },
757 { "tcam_counter_3", 4, 0x0b | GLOBAL_STATS_OP_BANK_1, BANK1, },
758 { "in_da_unknown", 4, 0x0e | GLOBAL_STATS_OP_BANK_1, BANK1, },
759 { "in_management", 4, 0x0f | GLOBAL_STATS_OP_BANK_1, BANK1, },
760 { "out_queue_0", 4, 0x10 | GLOBAL_STATS_OP_BANK_1, BANK1, },
761 { "out_queue_1", 4, 0x11 | GLOBAL_STATS_OP_BANK_1, BANK1, },
762 { "out_queue_2", 4, 0x12 | GLOBAL_STATS_OP_BANK_1, BANK1, },
763 { "out_queue_3", 4, 0x13 | GLOBAL_STATS_OP_BANK_1, BANK1, },
764 { "out_queue_4", 4, 0x14 | GLOBAL_STATS_OP_BANK_1, BANK1, },
765 { "out_queue_5", 4, 0x15 | GLOBAL_STATS_OP_BANK_1, BANK1, },
766 { "out_queue_6", 4, 0x16 | GLOBAL_STATS_OP_BANK_1, BANK1, },
767 { "out_queue_7", 4, 0x17 | GLOBAL_STATS_OP_BANK_1, BANK1, },
768 { "out_cut_through", 4, 0x18 | GLOBAL_STATS_OP_BANK_1, BANK1, },
769 { "out_octets_a", 4, 0x1a | GLOBAL_STATS_OP_BANK_1, BANK1, },
770 { "out_octets_b", 4, 0x1b | GLOBAL_STATS_OP_BANK_1, BANK1, },
771 { "out_management", 4, 0x1f | GLOBAL_STATS_OP_BANK_1, BANK1, },
Andrew Lunne413e7e2015-04-02 04:06:38 +0200772};
773
Vivien Didelotfad09c72016-06-21 12:28:20 -0400774static bool mv88e6xxx_has_stat(struct mv88e6xxx_chip *chip,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100775 struct mv88e6xxx_hw_stat *stat)
Andrew Lunne413e7e2015-04-02 04:06:38 +0200776{
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100777 switch (stat->type) {
778 case BANK0:
Andrew Lunne413e7e2015-04-02 04:06:38 +0200779 return true;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100780 case BANK1:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400781 return mv88e6xxx_6320_family(chip);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100782 case PORT:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400783 return mv88e6xxx_6095_family(chip) ||
784 mv88e6xxx_6185_family(chip) ||
785 mv88e6xxx_6097_family(chip) ||
786 mv88e6xxx_6165_family(chip) ||
787 mv88e6xxx_6351_family(chip) ||
788 mv88e6xxx_6352_family(chip);
Andrew Lunne413e7e2015-04-02 04:06:38 +0200789 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100790 return false;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000791}
792
Vivien Didelotfad09c72016-06-21 12:28:20 -0400793static uint64_t _mv88e6xxx_get_ethtool_stat(struct mv88e6xxx_chip *chip,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100794 struct mv88e6xxx_hw_stat *s,
Andrew Lunn80c46272015-06-20 18:42:30 +0200795 int port)
796{
Andrew Lunn80c46272015-06-20 18:42:30 +0200797 u32 low;
798 u32 high = 0;
799 int ret;
800 u64 value;
801
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100802 switch (s->type) {
803 case PORT:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400804 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), s->reg);
Andrew Lunn80c46272015-06-20 18:42:30 +0200805 if (ret < 0)
806 return UINT64_MAX;
807
808 low = ret;
809 if (s->sizeof_stat == 4) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400810 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port),
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100811 s->reg + 1);
Andrew Lunn80c46272015-06-20 18:42:30 +0200812 if (ret < 0)
813 return UINT64_MAX;
814 high = ret;
815 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100816 break;
817 case BANK0:
818 case BANK1:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400819 _mv88e6xxx_stats_read(chip, s->reg, &low);
Andrew Lunn80c46272015-06-20 18:42:30 +0200820 if (s->sizeof_stat == 8)
Vivien Didelotfad09c72016-06-21 12:28:20 -0400821 _mv88e6xxx_stats_read(chip, s->reg + 1, &high);
Andrew Lunn80c46272015-06-20 18:42:30 +0200822 }
823 value = (((u64)high) << 16) | low;
824 return value;
825}
826
Vivien Didelotf81ec902016-05-09 13:22:58 -0400827static void mv88e6xxx_get_strings(struct dsa_switch *ds, int port,
828 uint8_t *data)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100829{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400830 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100831 struct mv88e6xxx_hw_stat *stat;
832 int i, j;
833
834 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
835 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400836 if (mv88e6xxx_has_stat(chip, stat)) {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100837 memcpy(data + j * ETH_GSTRING_LEN, stat->string,
838 ETH_GSTRING_LEN);
839 j++;
840 }
841 }
842}
843
Vivien Didelotf81ec902016-05-09 13:22:58 -0400844static int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100845{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400846 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100847 struct mv88e6xxx_hw_stat *stat;
848 int i, j;
849
850 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
851 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400852 if (mv88e6xxx_has_stat(chip, stat))
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100853 j++;
854 }
855 return j;
856}
857
Vivien Didelotf81ec902016-05-09 13:22:58 -0400858static void mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, int port,
859 uint64_t *data)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000860{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400861 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100862 struct mv88e6xxx_hw_stat *stat;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000863 int ret;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100864 int i, j;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000865
Vivien Didelotfad09c72016-06-21 12:28:20 -0400866 mutex_lock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000867
Vivien Didelotfad09c72016-06-21 12:28:20 -0400868 ret = _mv88e6xxx_stats_snapshot(chip, port);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000869 if (ret < 0) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400870 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000871 return;
872 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100873 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
874 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400875 if (mv88e6xxx_has_stat(chip, stat)) {
876 data[j] = _mv88e6xxx_get_ethtool_stat(chip, stat, port);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100877 j++;
878 }
879 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000880
Vivien Didelotfad09c72016-06-21 12:28:20 -0400881 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000882}
Ben Hutchings98e67302011-11-25 14:36:19 +0000883
Vivien Didelotf81ec902016-05-09 13:22:58 -0400884static int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700885{
886 return 32 * sizeof(u16);
887}
888
Vivien Didelotf81ec902016-05-09 13:22:58 -0400889static void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
890 struct ethtool_regs *regs, void *_p)
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700891{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400892 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700893 u16 *p = _p;
894 int i;
895
896 regs->version = 0;
897
898 memset(p, 0xff, 32 * sizeof(u16));
899
Vivien Didelotfad09c72016-06-21 12:28:20 -0400900 mutex_lock(&chip->reg_lock);
Vivien Didelot23062512016-05-09 13:22:45 -0400901
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700902 for (i = 0; i < 32; i++) {
903 int ret;
904
Vivien Didelotfad09c72016-06-21 12:28:20 -0400905 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), i);
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700906 if (ret >= 0)
907 p[i] = ret;
908 }
Vivien Didelot23062512016-05-09 13:22:45 -0400909
Vivien Didelotfad09c72016-06-21 12:28:20 -0400910 mutex_unlock(&chip->reg_lock);
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700911}
912
Vivien Didelotfad09c72016-06-21 12:28:20 -0400913static int _mv88e6xxx_atu_wait(struct mv88e6xxx_chip *chip)
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700914{
Vivien Didelot2d79af62016-08-15 17:18:57 -0400915 return mv88e6xxx_wait(chip, REG_GLOBAL, GLOBAL_ATU_OP,
916 GLOBAL_ATU_OP_BUSY);
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700917}
918
Vivien Didelotf81ec902016-05-09 13:22:58 -0400919static int mv88e6xxx_get_eee(struct dsa_switch *ds, int port,
920 struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -0800921{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400922 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelot9c938292016-08-15 17:19:02 -0400923 u16 reg;
924 int err;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800925
Vivien Didelotfad09c72016-06-21 12:28:20 -0400926 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
Vivien Didelotaadbdb82016-05-09 13:22:44 -0400927 return -EOPNOTSUPP;
928
Vivien Didelotfad09c72016-06-21 12:28:20 -0400929 mutex_lock(&chip->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200930
Vivien Didelot9c938292016-08-15 17:19:02 -0400931 err = mv88e6xxx_phy_read(chip, port, 16, &reg);
932 if (err)
Andrew Lunn2f40c692015-04-02 04:06:37 +0200933 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800934
935 e->eee_enabled = !!(reg & 0x0200);
936 e->tx_lpi_enabled = !!(reg & 0x0100);
937
Vivien Didelot9c938292016-08-15 17:19:02 -0400938 err = mv88e6xxx_read(chip, REG_PORT(port), PORT_STATUS, &reg);
939 if (err)
Andrew Lunn2f40c692015-04-02 04:06:37 +0200940 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800941
Andrew Lunncca8b132015-04-02 04:06:39 +0200942 e->eee_active = !!(reg & PORT_STATUS_EEE);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200943out:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400944 mutex_unlock(&chip->reg_lock);
Vivien Didelot9c938292016-08-15 17:19:02 -0400945
946 return err;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800947}
948
Vivien Didelotf81ec902016-05-09 13:22:58 -0400949static int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
950 struct phy_device *phydev, struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -0800951{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400952 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelot9c938292016-08-15 17:19:02 -0400953 u16 reg;
954 int err;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800955
Vivien Didelotfad09c72016-06-21 12:28:20 -0400956 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
Vivien Didelotaadbdb82016-05-09 13:22:44 -0400957 return -EOPNOTSUPP;
958
Vivien Didelotfad09c72016-06-21 12:28:20 -0400959 mutex_lock(&chip->reg_lock);
Guenter Roeck11b3b452015-03-06 22:23:51 -0800960
Vivien Didelot9c938292016-08-15 17:19:02 -0400961 err = mv88e6xxx_phy_read(chip, port, 16, &reg);
962 if (err)
Andrew Lunn2f40c692015-04-02 04:06:37 +0200963 goto out;
964
Vivien Didelot9c938292016-08-15 17:19:02 -0400965 reg &= ~0x0300;
Andrew Lunn2f40c692015-04-02 04:06:37 +0200966 if (e->eee_enabled)
967 reg |= 0x0200;
968 if (e->tx_lpi_enabled)
969 reg |= 0x0100;
970
Vivien Didelot9c938292016-08-15 17:19:02 -0400971 err = mv88e6xxx_phy_write(chip, port, 16, reg);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200972out:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400973 mutex_unlock(&chip->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200974
Vivien Didelot9c938292016-08-15 17:19:02 -0400975 return err;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800976}
977
Vivien Didelotfad09c72016-06-21 12:28:20 -0400978static int _mv88e6xxx_atu_cmd(struct mv88e6xxx_chip *chip, u16 fid, u16 cmd)
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700979{
980 int ret;
981
Vivien Didelotfad09c72016-06-21 12:28:20 -0400982 if (mv88e6xxx_has_fid_reg(chip)) {
983 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_FID,
984 fid);
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400985 if (ret < 0)
986 return ret;
Vivien Didelotfad09c72016-06-21 12:28:20 -0400987 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -0400988 /* ATU DBNum[7:4] are located in ATU Control 15:12 */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400989 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL);
Vivien Didelot11ea8092016-03-31 16:53:44 -0400990 if (ret < 0)
991 return ret;
992
Vivien Didelotfad09c72016-06-21 12:28:20 -0400993 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL,
Vivien Didelot11ea8092016-03-31 16:53:44 -0400994 (ret & 0xfff) |
995 ((fid << 8) & 0xf000));
996 if (ret < 0)
997 return ret;
998
999 /* ATU DBNum[3:0] are located in ATU Operation 3:0 */
1000 cmd |= fid & 0xf;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001001 }
1002
Vivien Didelotfad09c72016-06-21 12:28:20 -04001003 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_OP, cmd);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001004 if (ret < 0)
1005 return ret;
1006
Vivien Didelotfad09c72016-06-21 12:28:20 -04001007 return _mv88e6xxx_atu_wait(chip);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001008}
1009
Vivien Didelotfad09c72016-06-21 12:28:20 -04001010static int _mv88e6xxx_atu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot37705b72015-09-04 14:34:11 -04001011 struct mv88e6xxx_atu_entry *entry)
1012{
1013 u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK;
1014
1015 if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) {
1016 unsigned int mask, shift;
1017
1018 if (entry->trunk) {
1019 data |= GLOBAL_ATU_DATA_TRUNK;
1020 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
1021 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
1022 } else {
1023 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
1024 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
1025 }
1026
1027 data |= (entry->portv_trunkid << shift) & mask;
1028 }
1029
Vivien Didelotfad09c72016-06-21 12:28:20 -04001030 return _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_DATA, data);
Vivien Didelot37705b72015-09-04 14:34:11 -04001031}
1032
Vivien Didelotfad09c72016-06-21 12:28:20 -04001033static int _mv88e6xxx_atu_flush_move(struct mv88e6xxx_chip *chip,
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001034 struct mv88e6xxx_atu_entry *entry,
1035 bool static_too)
1036{
1037 int op;
1038 int err;
1039
Vivien Didelotfad09c72016-06-21 12:28:20 -04001040 err = _mv88e6xxx_atu_wait(chip);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001041 if (err)
1042 return err;
1043
Vivien Didelotfad09c72016-06-21 12:28:20 -04001044 err = _mv88e6xxx_atu_data_write(chip, entry);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001045 if (err)
1046 return err;
1047
1048 if (entry->fid) {
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001049 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB :
1050 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB;
1051 } else {
1052 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL :
1053 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC;
1054 }
1055
Vivien Didelotfad09c72016-06-21 12:28:20 -04001056 return _mv88e6xxx_atu_cmd(chip, entry->fid, op);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001057}
1058
Vivien Didelotfad09c72016-06-21 12:28:20 -04001059static int _mv88e6xxx_atu_flush(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001060 u16 fid, bool static_too)
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001061{
1062 struct mv88e6xxx_atu_entry entry = {
1063 .fid = fid,
1064 .state = 0, /* EntryState bits must be 0 */
1065 };
1066
Vivien Didelotfad09c72016-06-21 12:28:20 -04001067 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001068}
1069
Vivien Didelotfad09c72016-06-21 12:28:20 -04001070static int _mv88e6xxx_atu_move(struct mv88e6xxx_chip *chip, u16 fid,
Andrew Lunn158bc062016-04-28 21:24:06 -04001071 int from_port, int to_port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001072{
1073 struct mv88e6xxx_atu_entry entry = {
1074 .trunk = false,
1075 .fid = fid,
1076 };
1077
1078 /* EntryState bits must be 0xF */
1079 entry.state = GLOBAL_ATU_DATA_STATE_MASK;
1080
1081 /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */
1082 entry.portv_trunkid = (to_port & 0x0f) << 4;
1083 entry.portv_trunkid |= from_port & 0x0f;
1084
Vivien Didelotfad09c72016-06-21 12:28:20 -04001085 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001086}
1087
Vivien Didelotfad09c72016-06-21 12:28:20 -04001088static int _mv88e6xxx_atu_remove(struct mv88e6xxx_chip *chip, u16 fid,
Andrew Lunn158bc062016-04-28 21:24:06 -04001089 int port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001090{
1091 /* Destination port 0xF means remove the entries */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001092 return _mv88e6xxx_atu_move(chip, fid, port, 0x0f, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001093}
1094
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001095static const char * const mv88e6xxx_port_state_names[] = {
1096 [PORT_CONTROL_STATE_DISABLED] = "Disabled",
1097 [PORT_CONTROL_STATE_BLOCKING] = "Blocking/Listening",
1098 [PORT_CONTROL_STATE_LEARNING] = "Learning",
1099 [PORT_CONTROL_STATE_FORWARDING] = "Forwarding",
1100};
1101
Vivien Didelotfad09c72016-06-21 12:28:20 -04001102static int _mv88e6xxx_port_state(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001103 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001104{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001105 struct dsa_switch *ds = chip->ds;
Geert Uytterhoevenc3ffe6d2015-04-16 20:49:14 +02001106 int reg, ret = 0;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001107 u8 oldstate;
1108
Vivien Didelotfad09c72016-06-21 12:28:20 -04001109 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL);
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001110 if (reg < 0)
1111 return reg;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001112
Andrew Lunncca8b132015-04-02 04:06:39 +02001113 oldstate = reg & PORT_CONTROL_STATE_MASK;
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001114
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001115 if (oldstate != state) {
1116 /* Flush forwarding database if we're moving a port
1117 * from Learning or Forwarding state to Disabled or
1118 * Blocking or Listening state.
1119 */
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001120 if ((oldstate == PORT_CONTROL_STATE_LEARNING ||
Vivien Didelot57d32312016-06-20 13:13:58 -04001121 oldstate == PORT_CONTROL_STATE_FORWARDING) &&
1122 (state == PORT_CONTROL_STATE_DISABLED ||
1123 state == PORT_CONTROL_STATE_BLOCKING)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001124 ret = _mv88e6xxx_atu_remove(chip, 0, port, false);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001125 if (ret)
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001126 return ret;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001127 }
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001128
Andrew Lunncca8b132015-04-02 04:06:39 +02001129 reg = (reg & ~PORT_CONTROL_STATE_MASK) | state;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001130 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL,
Andrew Lunncca8b132015-04-02 04:06:39 +02001131 reg);
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001132 if (ret)
1133 return ret;
1134
Andrew Lunnc8b09802016-06-04 21:16:57 +02001135 netdev_dbg(ds->ports[port].netdev, "PortState %s (was %s)\n",
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001136 mv88e6xxx_port_state_names[state],
1137 mv88e6xxx_port_state_names[oldstate]);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001138 }
1139
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001140 return ret;
1141}
1142
Vivien Didelotfad09c72016-06-21 12:28:20 -04001143static int _mv88e6xxx_port_based_vlan_map(struct mv88e6xxx_chip *chip, int port)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001144{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001145 struct net_device *bridge = chip->ports[port].bridge_dev;
1146 const u16 mask = (1 << chip->info->num_ports) - 1;
1147 struct dsa_switch *ds = chip->ds;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001148 u16 output_ports = 0;
Vivien Didelotede80982015-10-11 18:08:35 -04001149 int reg;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001150 int i;
1151
1152 /* allow CPU port or DSA link(s) to send frames to every port */
1153 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
1154 output_ports = mask;
1155 } else {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001156 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001157 /* allow sending frames to every group member */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001158 if (bridge && chip->ports[i].bridge_dev == bridge)
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001159 output_ports |= BIT(i);
1160
1161 /* allow sending frames to CPU port and DSA link(s) */
1162 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
1163 output_ports |= BIT(i);
1164 }
1165 }
1166
1167 /* prevent frames from going back out of the port they came in on */
1168 output_ports &= ~BIT(port);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001169
Vivien Didelotfad09c72016-06-21 12:28:20 -04001170 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_BASE_VLAN);
Vivien Didelotede80982015-10-11 18:08:35 -04001171 if (reg < 0)
1172 return reg;
1173
1174 reg &= ~mask;
1175 reg |= output_ports & mask;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001176
Vivien Didelotfad09c72016-06-21 12:28:20 -04001177 return _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_BASE_VLAN, reg);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001178}
1179
Vivien Didelotf81ec902016-05-09 13:22:58 -04001180static void mv88e6xxx_port_stp_state_set(struct dsa_switch *ds, int port,
1181 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001182{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001183 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001184 int stp_state;
Vivien Didelot553eb542016-05-13 20:38:23 -04001185 int err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001186
1187 switch (state) {
1188 case BR_STATE_DISABLED:
Andrew Lunncca8b132015-04-02 04:06:39 +02001189 stp_state = PORT_CONTROL_STATE_DISABLED;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001190 break;
1191 case BR_STATE_BLOCKING:
1192 case BR_STATE_LISTENING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001193 stp_state = PORT_CONTROL_STATE_BLOCKING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001194 break;
1195 case BR_STATE_LEARNING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001196 stp_state = PORT_CONTROL_STATE_LEARNING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001197 break;
1198 case BR_STATE_FORWARDING:
1199 default:
Andrew Lunncca8b132015-04-02 04:06:39 +02001200 stp_state = PORT_CONTROL_STATE_FORWARDING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001201 break;
1202 }
1203
Vivien Didelotfad09c72016-06-21 12:28:20 -04001204 mutex_lock(&chip->reg_lock);
1205 err = _mv88e6xxx_port_state(chip, port, stp_state);
1206 mutex_unlock(&chip->reg_lock);
Vivien Didelot553eb542016-05-13 20:38:23 -04001207
1208 if (err)
Andrew Lunnc8b09802016-06-04 21:16:57 +02001209 netdev_err(ds->ports[port].netdev,
1210 "failed to update state to %s\n",
Vivien Didelot553eb542016-05-13 20:38:23 -04001211 mv88e6xxx_port_state_names[stp_state]);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001212}
1213
Vivien Didelotfad09c72016-06-21 12:28:20 -04001214static int _mv88e6xxx_port_pvid(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001215 u16 *new, u16 *old)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001216{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001217 struct dsa_switch *ds = chip->ds;
Vivien Didelot5da96032016-03-07 18:24:39 -05001218 u16 pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001219 int ret;
1220
Vivien Didelotfad09c72016-06-21 12:28:20 -04001221 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_DEFAULT_VLAN);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001222 if (ret < 0)
1223 return ret;
1224
Vivien Didelot5da96032016-03-07 18:24:39 -05001225 pvid = ret & PORT_DEFAULT_VLAN_MASK;
1226
1227 if (new) {
1228 ret &= ~PORT_DEFAULT_VLAN_MASK;
1229 ret |= *new & PORT_DEFAULT_VLAN_MASK;
1230
Vivien Didelotfad09c72016-06-21 12:28:20 -04001231 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Vivien Didelot5da96032016-03-07 18:24:39 -05001232 PORT_DEFAULT_VLAN, ret);
1233 if (ret < 0)
1234 return ret;
1235
Andrew Lunnc8b09802016-06-04 21:16:57 +02001236 netdev_dbg(ds->ports[port].netdev,
1237 "DefaultVID %d (was %d)\n", *new, pvid);
Vivien Didelot5da96032016-03-07 18:24:39 -05001238 }
1239
1240 if (old)
1241 *old = pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001242
1243 return 0;
1244}
1245
Vivien Didelotfad09c72016-06-21 12:28:20 -04001246static int _mv88e6xxx_port_pvid_get(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001247 int port, u16 *pvid)
Vivien Didelot5da96032016-03-07 18:24:39 -05001248{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001249 return _mv88e6xxx_port_pvid(chip, port, NULL, pvid);
Vivien Didelot5da96032016-03-07 18:24:39 -05001250}
1251
Vivien Didelotfad09c72016-06-21 12:28:20 -04001252static int _mv88e6xxx_port_pvid_set(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001253 int port, u16 pvid)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001254{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001255 return _mv88e6xxx_port_pvid(chip, port, &pvid, NULL);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001256}
1257
Vivien Didelotfad09c72016-06-21 12:28:20 -04001258static int _mv88e6xxx_vtu_wait(struct mv88e6xxx_chip *chip)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001259{
Vivien Didelot2d79af62016-08-15 17:18:57 -04001260 return mv88e6xxx_wait(chip, REG_GLOBAL, GLOBAL_VTU_OP,
1261 GLOBAL_VTU_OP_BUSY);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001262}
1263
Vivien Didelotfad09c72016-06-21 12:28:20 -04001264static int _mv88e6xxx_vtu_cmd(struct mv88e6xxx_chip *chip, u16 op)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001265{
1266 int ret;
1267
Vivien Didelotfad09c72016-06-21 12:28:20 -04001268 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_OP, op);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001269 if (ret < 0)
1270 return ret;
1271
Vivien Didelotfad09c72016-06-21 12:28:20 -04001272 return _mv88e6xxx_vtu_wait(chip);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001273}
1274
Vivien Didelotfad09c72016-06-21 12:28:20 -04001275static int _mv88e6xxx_vtu_stu_flush(struct mv88e6xxx_chip *chip)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001276{
1277 int ret;
1278
Vivien Didelotfad09c72016-06-21 12:28:20 -04001279 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001280 if (ret < 0)
1281 return ret;
1282
Vivien Didelotfad09c72016-06-21 12:28:20 -04001283 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_FLUSH_ALL);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001284}
1285
Vivien Didelotfad09c72016-06-21 12:28:20 -04001286static int _mv88e6xxx_vtu_stu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001287 struct mv88e6xxx_vtu_stu_entry *entry,
1288 unsigned int nibble_offset)
1289{
Vivien Didelotb8fee952015-08-13 12:52:19 -04001290 u16 regs[3];
1291 int i;
1292 int ret;
1293
1294 for (i = 0; i < 3; ++i) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001295 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001296 GLOBAL_VTU_DATA_0_3 + i);
1297 if (ret < 0)
1298 return ret;
1299
1300 regs[i] = ret;
1301 }
1302
Vivien Didelotfad09c72016-06-21 12:28:20 -04001303 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelotb8fee952015-08-13 12:52:19 -04001304 unsigned int shift = (i % 4) * 4 + nibble_offset;
1305 u16 reg = regs[i / 4];
1306
1307 entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
1308 }
1309
1310 return 0;
1311}
1312
Vivien Didelotfad09c72016-06-21 12:28:20 -04001313static int mv88e6xxx_vtu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001314 struct mv88e6xxx_vtu_stu_entry *entry)
1315{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001316 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 0);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001317}
1318
Vivien Didelotfad09c72016-06-21 12:28:20 -04001319static int mv88e6xxx_stu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001320 struct mv88e6xxx_vtu_stu_entry *entry)
1321{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001322 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 2);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001323}
1324
Vivien Didelotfad09c72016-06-21 12:28:20 -04001325static int _mv88e6xxx_vtu_stu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001326 struct mv88e6xxx_vtu_stu_entry *entry,
1327 unsigned int nibble_offset)
1328{
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001329 u16 regs[3] = { 0 };
1330 int i;
1331 int ret;
1332
Vivien Didelotfad09c72016-06-21 12:28:20 -04001333 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001334 unsigned int shift = (i % 4) * 4 + nibble_offset;
1335 u8 data = entry->data[i];
1336
1337 regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
1338 }
1339
1340 for (i = 0; i < 3; ++i) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001341 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001342 GLOBAL_VTU_DATA_0_3 + i, regs[i]);
1343 if (ret < 0)
1344 return ret;
1345 }
1346
1347 return 0;
1348}
1349
Vivien Didelotfad09c72016-06-21 12:28:20 -04001350static int mv88e6xxx_vtu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001351 struct mv88e6xxx_vtu_stu_entry *entry)
1352{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001353 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 0);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001354}
1355
Vivien Didelotfad09c72016-06-21 12:28:20 -04001356static int mv88e6xxx_stu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001357 struct mv88e6xxx_vtu_stu_entry *entry)
1358{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001359 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 2);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001360}
1361
Vivien Didelotfad09c72016-06-21 12:28:20 -04001362static int _mv88e6xxx_vtu_vid_write(struct mv88e6xxx_chip *chip, u16 vid)
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001363{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001364 return _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID,
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001365 vid & GLOBAL_VTU_VID_MASK);
1366}
1367
Vivien Didelotfad09c72016-06-21 12:28:20 -04001368static int _mv88e6xxx_vtu_getnext(struct mv88e6xxx_chip *chip,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001369 struct mv88e6xxx_vtu_stu_entry *entry)
1370{
1371 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1372 int ret;
1373
Vivien Didelotfad09c72016-06-21 12:28:20 -04001374 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001375 if (ret < 0)
1376 return ret;
1377
Vivien Didelotfad09c72016-06-21 12:28:20 -04001378 ret = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_VTU_GET_NEXT);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001379 if (ret < 0)
1380 return ret;
1381
Vivien Didelotfad09c72016-06-21 12:28:20 -04001382 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_VID);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001383 if (ret < 0)
1384 return ret;
1385
1386 next.vid = ret & GLOBAL_VTU_VID_MASK;
1387 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1388
1389 if (next.valid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001390 ret = mv88e6xxx_vtu_data_read(chip, &next);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001391 if (ret < 0)
1392 return ret;
1393
Vivien Didelotfad09c72016-06-21 12:28:20 -04001394 if (mv88e6xxx_has_fid_reg(chip)) {
1395 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001396 GLOBAL_VTU_FID);
1397 if (ret < 0)
1398 return ret;
1399
1400 next.fid = ret & GLOBAL_VTU_FID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001401 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001402 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1403 * VTU DBNum[3:0] are located in VTU Operation 3:0
1404 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001405 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
Vivien Didelot11ea8092016-03-31 16:53:44 -04001406 GLOBAL_VTU_OP);
1407 if (ret < 0)
1408 return ret;
1409
1410 next.fid = (ret & 0xf00) >> 4;
1411 next.fid |= ret & 0xf;
Vivien Didelot2e7bd5e2016-03-31 16:53:41 -04001412 }
Vivien Didelotb8fee952015-08-13 12:52:19 -04001413
Vivien Didelotfad09c72016-06-21 12:28:20 -04001414 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
1415 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001416 GLOBAL_VTU_SID);
1417 if (ret < 0)
1418 return ret;
1419
1420 next.sid = ret & GLOBAL_VTU_SID_MASK;
1421 }
1422 }
1423
1424 *entry = next;
1425 return 0;
1426}
1427
Vivien Didelotf81ec902016-05-09 13:22:58 -04001428static int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
1429 struct switchdev_obj_port_vlan *vlan,
1430 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001431{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001432 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001433 struct mv88e6xxx_vtu_stu_entry next;
1434 u16 pvid;
1435 int err;
1436
Vivien Didelotfad09c72016-06-21 12:28:20 -04001437 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001438 return -EOPNOTSUPP;
1439
Vivien Didelotfad09c72016-06-21 12:28:20 -04001440 mutex_lock(&chip->reg_lock);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001441
Vivien Didelotfad09c72016-06-21 12:28:20 -04001442 err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001443 if (err)
1444 goto unlock;
1445
Vivien Didelotfad09c72016-06-21 12:28:20 -04001446 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001447 if (err)
1448 goto unlock;
1449
1450 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001451 err = _mv88e6xxx_vtu_getnext(chip, &next);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001452 if (err)
1453 break;
1454
1455 if (!next.valid)
1456 break;
1457
1458 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1459 continue;
1460
1461 /* reinit and dump this VLAN obj */
Vivien Didelot57d32312016-06-20 13:13:58 -04001462 vlan->vid_begin = next.vid;
1463 vlan->vid_end = next.vid;
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001464 vlan->flags = 0;
1465
1466 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1467 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1468
1469 if (next.vid == pvid)
1470 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1471
1472 err = cb(&vlan->obj);
1473 if (err)
1474 break;
1475 } while (next.vid < GLOBAL_VTU_VID_MASK);
1476
1477unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001478 mutex_unlock(&chip->reg_lock);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001479
1480 return err;
1481}
1482
Vivien Didelotfad09c72016-06-21 12:28:20 -04001483static int _mv88e6xxx_vtu_loadpurge(struct mv88e6xxx_chip *chip,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001484 struct mv88e6xxx_vtu_stu_entry *entry)
1485{
Vivien Didelot11ea8092016-03-31 16:53:44 -04001486 u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001487 u16 reg = 0;
1488 int ret;
1489
Vivien Didelotfad09c72016-06-21 12:28:20 -04001490 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001491 if (ret < 0)
1492 return ret;
1493
1494 if (!entry->valid)
1495 goto loadpurge;
1496
1497 /* Write port member tags */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001498 ret = mv88e6xxx_vtu_data_write(chip, entry);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001499 if (ret < 0)
1500 return ret;
1501
Vivien Didelotfad09c72016-06-21 12:28:20 -04001502 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001503 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001504 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID,
1505 reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001506 if (ret < 0)
1507 return ret;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001508 }
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001509
Vivien Didelotfad09c72016-06-21 12:28:20 -04001510 if (mv88e6xxx_has_fid_reg(chip)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001511 reg = entry->fid & GLOBAL_VTU_FID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001512 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_FID,
1513 reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001514 if (ret < 0)
1515 return ret;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001516 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001517 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1518 * VTU DBNum[3:0] are located in VTU Operation 3:0
1519 */
1520 op |= (entry->fid & 0xf0) << 8;
1521 op |= entry->fid & 0xf;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001522 }
1523
1524 reg = GLOBAL_VTU_VID_VALID;
1525loadpurge:
1526 reg |= entry->vid & GLOBAL_VTU_VID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001527 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID, reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001528 if (ret < 0)
1529 return ret;
1530
Vivien Didelotfad09c72016-06-21 12:28:20 -04001531 return _mv88e6xxx_vtu_cmd(chip, op);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001532}
1533
Vivien Didelotfad09c72016-06-21 12:28:20 -04001534static int _mv88e6xxx_stu_getnext(struct mv88e6xxx_chip *chip, u8 sid,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001535 struct mv88e6xxx_vtu_stu_entry *entry)
1536{
1537 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1538 int ret;
1539
Vivien Didelotfad09c72016-06-21 12:28:20 -04001540 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001541 if (ret < 0)
1542 return ret;
1543
Vivien Didelotfad09c72016-06-21 12:28:20 -04001544 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001545 sid & GLOBAL_VTU_SID_MASK);
1546 if (ret < 0)
1547 return ret;
1548
Vivien Didelotfad09c72016-06-21 12:28:20 -04001549 ret = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_GET_NEXT);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001550 if (ret < 0)
1551 return ret;
1552
Vivien Didelotfad09c72016-06-21 12:28:20 -04001553 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_SID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001554 if (ret < 0)
1555 return ret;
1556
1557 next.sid = ret & GLOBAL_VTU_SID_MASK;
1558
Vivien Didelotfad09c72016-06-21 12:28:20 -04001559 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_VID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001560 if (ret < 0)
1561 return ret;
1562
1563 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1564
1565 if (next.valid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001566 ret = mv88e6xxx_stu_data_read(chip, &next);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001567 if (ret < 0)
1568 return ret;
1569 }
1570
1571 *entry = next;
1572 return 0;
1573}
1574
Vivien Didelotfad09c72016-06-21 12:28:20 -04001575static int _mv88e6xxx_stu_loadpurge(struct mv88e6xxx_chip *chip,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001576 struct mv88e6xxx_vtu_stu_entry *entry)
1577{
1578 u16 reg = 0;
1579 int ret;
1580
Vivien Didelotfad09c72016-06-21 12:28:20 -04001581 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001582 if (ret < 0)
1583 return ret;
1584
1585 if (!entry->valid)
1586 goto loadpurge;
1587
1588 /* Write port states */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001589 ret = mv88e6xxx_stu_data_write(chip, entry);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001590 if (ret < 0)
1591 return ret;
1592
1593 reg = GLOBAL_VTU_VID_VALID;
1594loadpurge:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001595 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID, reg);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001596 if (ret < 0)
1597 return ret;
1598
1599 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001600 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID, reg);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001601 if (ret < 0)
1602 return ret;
1603
Vivien Didelotfad09c72016-06-21 12:28:20 -04001604 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_LOAD_PURGE);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001605}
1606
Vivien Didelotfad09c72016-06-21 12:28:20 -04001607static int _mv88e6xxx_port_fid(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001608 u16 *new, u16 *old)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001609{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001610 struct dsa_switch *ds = chip->ds;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001611 u16 upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001612 u16 fid;
1613 int ret;
1614
Vivien Didelotfad09c72016-06-21 12:28:20 -04001615 if (mv88e6xxx_num_databases(chip) == 4096)
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001616 upper_mask = 0xff;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001617 else if (mv88e6xxx_num_databases(chip) == 256)
Vivien Didelot11ea8092016-03-31 16:53:44 -04001618 upper_mask = 0xf;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001619 else
1620 return -EOPNOTSUPP;
1621
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001622 /* Port's default FID bits 3:0 are located in reg 0x06, offset 12 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001623 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_BASE_VLAN);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001624 if (ret < 0)
1625 return ret;
1626
1627 fid = (ret & PORT_BASE_VLAN_FID_3_0_MASK) >> 12;
1628
1629 if (new) {
1630 ret &= ~PORT_BASE_VLAN_FID_3_0_MASK;
1631 ret |= (*new << 12) & PORT_BASE_VLAN_FID_3_0_MASK;
1632
Vivien Didelotfad09c72016-06-21 12:28:20 -04001633 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_BASE_VLAN,
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001634 ret);
1635 if (ret < 0)
1636 return ret;
1637 }
1638
1639 /* Port's default FID bits 11:4 are located in reg 0x05, offset 0 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001640 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL_1);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001641 if (ret < 0)
1642 return ret;
1643
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001644 fid |= (ret & upper_mask) << 4;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001645
1646 if (new) {
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001647 ret &= ~upper_mask;
1648 ret |= (*new >> 4) & upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001649
Vivien Didelotfad09c72016-06-21 12:28:20 -04001650 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_1,
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001651 ret);
1652 if (ret < 0)
1653 return ret;
1654
Andrew Lunnc8b09802016-06-04 21:16:57 +02001655 netdev_dbg(ds->ports[port].netdev,
1656 "FID %d (was %d)\n", *new, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001657 }
1658
1659 if (old)
1660 *old = fid;
1661
1662 return 0;
1663}
1664
Vivien Didelotfad09c72016-06-21 12:28:20 -04001665static int _mv88e6xxx_port_fid_get(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001666 int port, u16 *fid)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001667{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001668 return _mv88e6xxx_port_fid(chip, port, NULL, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001669}
1670
Vivien Didelotfad09c72016-06-21 12:28:20 -04001671static int _mv88e6xxx_port_fid_set(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001672 int port, u16 fid)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001673{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001674 return _mv88e6xxx_port_fid(chip, port, &fid, NULL);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001675}
1676
Vivien Didelotfad09c72016-06-21 12:28:20 -04001677static int _mv88e6xxx_fid_new(struct mv88e6xxx_chip *chip, u16 *fid)
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001678{
1679 DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
1680 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001681 int i, err;
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001682
1683 bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);
1684
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001685 /* Set every FID bit used by the (un)bridged ports */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001686 for (i = 0; i < chip->info->num_ports; ++i) {
1687 err = _mv88e6xxx_port_fid_get(chip, i, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001688 if (err)
1689 return err;
1690
1691 set_bit(*fid, fid_bitmap);
1692 }
1693
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001694 /* Set every FID bit used by the VLAN entries */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001695 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001696 if (err)
1697 return err;
1698
1699 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001700 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001701 if (err)
1702 return err;
1703
1704 if (!vlan.valid)
1705 break;
1706
1707 set_bit(vlan.fid, fid_bitmap);
1708 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
1709
1710 /* The reset value 0x000 is used to indicate that multiple address
1711 * databases are not needed. Return the next positive available.
1712 */
1713 *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1);
Vivien Didelotfad09c72016-06-21 12:28:20 -04001714 if (unlikely(*fid >= mv88e6xxx_num_databases(chip)))
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001715 return -ENOSPC;
1716
1717 /* Clear the database */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001718 return _mv88e6xxx_atu_flush(chip, *fid, true);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001719}
1720
Vivien Didelotfad09c72016-06-21 12:28:20 -04001721static int _mv88e6xxx_vtu_new(struct mv88e6xxx_chip *chip, u16 vid,
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001722 struct mv88e6xxx_vtu_stu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001723{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001724 struct dsa_switch *ds = chip->ds;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001725 struct mv88e6xxx_vtu_stu_entry vlan = {
1726 .valid = true,
1727 .vid = vid,
1728 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001729 int i, err;
1730
Vivien Didelotfad09c72016-06-21 12:28:20 -04001731 err = _mv88e6xxx_fid_new(chip, &vlan.fid);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001732 if (err)
1733 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001734
Vivien Didelot3d131f02015-11-03 10:52:52 -05001735 /* exclude all ports except the CPU and DSA ports */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001736 for (i = 0; i < chip->info->num_ports; ++i)
Vivien Didelot3d131f02015-11-03 10:52:52 -05001737 vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)
1738 ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED
1739 : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001740
Vivien Didelotfad09c72016-06-21 12:28:20 -04001741 if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
1742 mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip)) {
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001743 struct mv88e6xxx_vtu_stu_entry vstp;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001744
1745 /* Adding a VTU entry requires a valid STU entry. As VSTP is not
1746 * implemented, only one STU entry is needed to cover all VTU
1747 * entries. Thus, validate the SID 0.
1748 */
1749 vlan.sid = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001750 err = _mv88e6xxx_stu_getnext(chip, GLOBAL_VTU_SID_MASK, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001751 if (err)
1752 return err;
1753
1754 if (vstp.sid != vlan.sid || !vstp.valid) {
1755 memset(&vstp, 0, sizeof(vstp));
1756 vstp.valid = true;
1757 vstp.sid = vlan.sid;
1758
Vivien Didelotfad09c72016-06-21 12:28:20 -04001759 err = _mv88e6xxx_stu_loadpurge(chip, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001760 if (err)
1761 return err;
1762 }
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001763 }
1764
1765 *entry = vlan;
1766 return 0;
1767}
1768
Vivien Didelotfad09c72016-06-21 12:28:20 -04001769static int _mv88e6xxx_vtu_get(struct mv88e6xxx_chip *chip, u16 vid,
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001770 struct mv88e6xxx_vtu_stu_entry *entry, bool creat)
1771{
1772 int err;
1773
1774 if (!vid)
1775 return -EINVAL;
1776
Vivien Didelotfad09c72016-06-21 12:28:20 -04001777 err = _mv88e6xxx_vtu_vid_write(chip, vid - 1);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001778 if (err)
1779 return err;
1780
Vivien Didelotfad09c72016-06-21 12:28:20 -04001781 err = _mv88e6xxx_vtu_getnext(chip, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001782 if (err)
1783 return err;
1784
1785 if (entry->vid != vid || !entry->valid) {
1786 if (!creat)
1787 return -EOPNOTSUPP;
1788 /* -ENOENT would've been more appropriate, but switchdev expects
1789 * -EOPNOTSUPP to inform bridge about an eventual software VLAN.
1790 */
1791
Vivien Didelotfad09c72016-06-21 12:28:20 -04001792 err = _mv88e6xxx_vtu_new(chip, vid, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001793 }
1794
1795 return err;
1796}
1797
Vivien Didelotda9c3592016-02-12 12:09:40 -05001798static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
1799 u16 vid_begin, u16 vid_end)
1800{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001801 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001802 struct mv88e6xxx_vtu_stu_entry vlan;
1803 int i, err;
1804
1805 if (!vid_begin)
1806 return -EOPNOTSUPP;
1807
Vivien Didelotfad09c72016-06-21 12:28:20 -04001808 mutex_lock(&chip->reg_lock);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001809
Vivien Didelotfad09c72016-06-21 12:28:20 -04001810 err = _mv88e6xxx_vtu_vid_write(chip, vid_begin - 1);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001811 if (err)
1812 goto unlock;
1813
1814 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001815 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001816 if (err)
1817 goto unlock;
1818
1819 if (!vlan.valid)
1820 break;
1821
1822 if (vlan.vid > vid_end)
1823 break;
1824
Vivien Didelotfad09c72016-06-21 12:28:20 -04001825 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelotda9c3592016-02-12 12:09:40 -05001826 if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i))
1827 continue;
1828
1829 if (vlan.data[i] ==
1830 GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1831 continue;
1832
Vivien Didelotfad09c72016-06-21 12:28:20 -04001833 if (chip->ports[i].bridge_dev ==
1834 chip->ports[port].bridge_dev)
Vivien Didelotda9c3592016-02-12 12:09:40 -05001835 break; /* same bridge, check next VLAN */
1836
Andrew Lunnc8b09802016-06-04 21:16:57 +02001837 netdev_warn(ds->ports[port].netdev,
Vivien Didelotda9c3592016-02-12 12:09:40 -05001838 "hardware VLAN %d already used by %s\n",
1839 vlan.vid,
Vivien Didelotfad09c72016-06-21 12:28:20 -04001840 netdev_name(chip->ports[i].bridge_dev));
Vivien Didelotda9c3592016-02-12 12:09:40 -05001841 err = -EOPNOTSUPP;
1842 goto unlock;
1843 }
1844 } while (vlan.vid < vid_end);
1845
1846unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001847 mutex_unlock(&chip->reg_lock);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001848
1849 return err;
1850}
1851
Vivien Didelot214cdb92016-02-26 13:16:08 -05001852static const char * const mv88e6xxx_port_8021q_mode_names[] = {
1853 [PORT_CONTROL_2_8021Q_DISABLED] = "Disabled",
1854 [PORT_CONTROL_2_8021Q_FALLBACK] = "Fallback",
1855 [PORT_CONTROL_2_8021Q_CHECK] = "Check",
1856 [PORT_CONTROL_2_8021Q_SECURE] = "Secure",
1857};
1858
Vivien Didelotf81ec902016-05-09 13:22:58 -04001859static int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port,
1860 bool vlan_filtering)
Vivien Didelot214cdb92016-02-26 13:16:08 -05001861{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001862 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelot214cdb92016-02-26 13:16:08 -05001863 u16 old, new = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE :
1864 PORT_CONTROL_2_8021Q_DISABLED;
1865 int ret;
1866
Vivien Didelotfad09c72016-06-21 12:28:20 -04001867 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001868 return -EOPNOTSUPP;
1869
Vivien Didelotfad09c72016-06-21 12:28:20 -04001870 mutex_lock(&chip->reg_lock);
Vivien Didelot214cdb92016-02-26 13:16:08 -05001871
Vivien Didelotfad09c72016-06-21 12:28:20 -04001872 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL_2);
Vivien Didelot214cdb92016-02-26 13:16:08 -05001873 if (ret < 0)
1874 goto unlock;
1875
1876 old = ret & PORT_CONTROL_2_8021Q_MASK;
1877
Vivien Didelot5220ef12016-03-07 18:24:52 -05001878 if (new != old) {
1879 ret &= ~PORT_CONTROL_2_8021Q_MASK;
1880 ret |= new & PORT_CONTROL_2_8021Q_MASK;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001881
Vivien Didelotfad09c72016-06-21 12:28:20 -04001882 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_2,
Vivien Didelot5220ef12016-03-07 18:24:52 -05001883 ret);
1884 if (ret < 0)
1885 goto unlock;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001886
Andrew Lunnc8b09802016-06-04 21:16:57 +02001887 netdev_dbg(ds->ports[port].netdev, "802.1Q Mode %s (was %s)\n",
Vivien Didelot5220ef12016-03-07 18:24:52 -05001888 mv88e6xxx_port_8021q_mode_names[new],
1889 mv88e6xxx_port_8021q_mode_names[old]);
1890 }
1891
1892 ret = 0;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001893unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001894 mutex_unlock(&chip->reg_lock);
Vivien Didelot214cdb92016-02-26 13:16:08 -05001895
1896 return ret;
1897}
1898
Vivien Didelot57d32312016-06-20 13:13:58 -04001899static int
1900mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
1901 const struct switchdev_obj_port_vlan *vlan,
1902 struct switchdev_trans *trans)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001903{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001904 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001905 int err;
1906
Vivien Didelotfad09c72016-06-21 12:28:20 -04001907 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001908 return -EOPNOTSUPP;
1909
Vivien Didelotda9c3592016-02-12 12:09:40 -05001910 /* If the requested port doesn't belong to the same bridge as the VLAN
1911 * members, do not support it (yet) and fallback to software VLAN.
1912 */
1913 err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin,
1914 vlan->vid_end);
1915 if (err)
1916 return err;
1917
Vivien Didelot76e398a2015-11-01 12:33:55 -05001918 /* We don't need any dynamic resource from the kernel (yet),
1919 * so skip the prepare phase.
1920 */
1921 return 0;
1922}
1923
Vivien Didelotfad09c72016-06-21 12:28:20 -04001924static int _mv88e6xxx_port_vlan_add(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001925 u16 vid, bool untagged)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001926{
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001927 struct mv88e6xxx_vtu_stu_entry vlan;
1928 int err;
1929
Vivien Didelotfad09c72016-06-21 12:28:20 -04001930 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, true);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001931 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001932 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001933
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001934 vlan.data[port] = untagged ?
1935 GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
1936 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
1937
Vivien Didelotfad09c72016-06-21 12:28:20 -04001938 return _mv88e6xxx_vtu_loadpurge(chip, &vlan);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001939}
1940
Vivien Didelotf81ec902016-05-09 13:22:58 -04001941static void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
1942 const struct switchdev_obj_port_vlan *vlan,
1943 struct switchdev_trans *trans)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001944{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001945 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001946 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1947 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1948 u16 vid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001949
Vivien Didelotfad09c72016-06-21 12:28:20 -04001950 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001951 return;
1952
Vivien Didelotfad09c72016-06-21 12:28:20 -04001953 mutex_lock(&chip->reg_lock);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001954
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001955 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
Vivien Didelotfad09c72016-06-21 12:28:20 -04001956 if (_mv88e6xxx_port_vlan_add(chip, port, vid, untagged))
Andrew Lunnc8b09802016-06-04 21:16:57 +02001957 netdev_err(ds->ports[port].netdev,
1958 "failed to add VLAN %d%c\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001959 vid, untagged ? 'u' : 't');
Vivien Didelot76e398a2015-11-01 12:33:55 -05001960
Vivien Didelotfad09c72016-06-21 12:28:20 -04001961 if (pvid && _mv88e6xxx_port_pvid_set(chip, port, vlan->vid_end))
Andrew Lunnc8b09802016-06-04 21:16:57 +02001962 netdev_err(ds->ports[port].netdev, "failed to set PVID %d\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001963 vlan->vid_end);
1964
Vivien Didelotfad09c72016-06-21 12:28:20 -04001965 mutex_unlock(&chip->reg_lock);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001966}
1967
Vivien Didelotfad09c72016-06-21 12:28:20 -04001968static int _mv88e6xxx_port_vlan_del(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001969 int port, u16 vid)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001970{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001971 struct dsa_switch *ds = chip->ds;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001972 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001973 int i, err;
1974
Vivien Didelotfad09c72016-06-21 12:28:20 -04001975 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001976 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001977 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001978
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001979 /* Tell switchdev if this VLAN is handled in software */
1980 if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
Vivien Didelot3c06f082016-02-05 14:04:39 -05001981 return -EOPNOTSUPP;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001982
1983 vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
1984
1985 /* keep the VLAN unless all ports are excluded */
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001986 vlan.valid = false;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001987 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelot3d131f02015-11-03 10:52:52 -05001988 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001989 continue;
1990
1991 if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001992 vlan.valid = true;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001993 break;
1994 }
1995 }
1996
Vivien Didelotfad09c72016-06-21 12:28:20 -04001997 err = _mv88e6xxx_vtu_loadpurge(chip, &vlan);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001998 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001999 return err;
2000
Vivien Didelotfad09c72016-06-21 12:28:20 -04002001 return _mv88e6xxx_atu_remove(chip, vlan.fid, port, false);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002002}
2003
Vivien Didelotf81ec902016-05-09 13:22:58 -04002004static int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
2005 const struct switchdev_obj_port_vlan *vlan)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002006{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002007 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002008 u16 pvid, vid;
2009 int err = 0;
2010
Vivien Didelotfad09c72016-06-21 12:28:20 -04002011 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04002012 return -EOPNOTSUPP;
2013
Vivien Didelotfad09c72016-06-21 12:28:20 -04002014 mutex_lock(&chip->reg_lock);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002015
Vivien Didelotfad09c72016-06-21 12:28:20 -04002016 err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002017 if (err)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002018 goto unlock;
2019
Vivien Didelot76e398a2015-11-01 12:33:55 -05002020 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002021 err = _mv88e6xxx_port_vlan_del(chip, port, vid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002022 if (err)
2023 goto unlock;
2024
2025 if (vid == pvid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002026 err = _mv88e6xxx_port_pvid_set(chip, port, 0);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002027 if (err)
2028 goto unlock;
2029 }
2030 }
2031
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002032unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002033 mutex_unlock(&chip->reg_lock);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002034
2035 return err;
2036}
2037
Vivien Didelotfad09c72016-06-21 12:28:20 -04002038static int _mv88e6xxx_atu_mac_write(struct mv88e6xxx_chip *chip,
Vivien Didelotc5723ac2015-08-10 09:09:48 -04002039 const unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002040{
2041 int i, ret;
2042
2043 for (i = 0; i < 3; i++) {
Andrew Lunncca8b132015-04-02 04:06:39 +02002044 ret = _mv88e6xxx_reg_write(
Vivien Didelotfad09c72016-06-21 12:28:20 -04002045 chip, REG_GLOBAL, GLOBAL_ATU_MAC_01 + i,
Andrew Lunncca8b132015-04-02 04:06:39 +02002046 (addr[i * 2] << 8) | addr[i * 2 + 1]);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002047 if (ret < 0)
2048 return ret;
2049 }
2050
2051 return 0;
2052}
2053
Vivien Didelotfad09c72016-06-21 12:28:20 -04002054static int _mv88e6xxx_atu_mac_read(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04002055 unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002056{
2057 int i, ret;
2058
2059 for (i = 0; i < 3; i++) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002060 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
Andrew Lunncca8b132015-04-02 04:06:39 +02002061 GLOBAL_ATU_MAC_01 + i);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002062 if (ret < 0)
2063 return ret;
2064 addr[i * 2] = ret >> 8;
2065 addr[i * 2 + 1] = ret & 0xff;
2066 }
2067
2068 return 0;
2069}
2070
Vivien Didelotfad09c72016-06-21 12:28:20 -04002071static int _mv88e6xxx_atu_load(struct mv88e6xxx_chip *chip,
Vivien Didelotfd231c82015-08-10 09:09:50 -04002072 struct mv88e6xxx_atu_entry *entry)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002073{
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002074 int ret;
2075
Vivien Didelotfad09c72016-06-21 12:28:20 -04002076 ret = _mv88e6xxx_atu_wait(chip);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002077 if (ret < 0)
2078 return ret;
2079
Vivien Didelotfad09c72016-06-21 12:28:20 -04002080 ret = _mv88e6xxx_atu_mac_write(chip, entry->mac);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002081 if (ret < 0)
2082 return ret;
2083
Vivien Didelotfad09c72016-06-21 12:28:20 -04002084 ret = _mv88e6xxx_atu_data_write(chip, entry);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002085 if (ret < 0)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002086 return ret;
2087
Vivien Didelotfad09c72016-06-21 12:28:20 -04002088 return _mv88e6xxx_atu_cmd(chip, entry->fid, GLOBAL_ATU_OP_LOAD_DB);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002089}
David S. Millercdf09692015-08-11 12:00:37 -07002090
Vivien Didelotfad09c72016-06-21 12:28:20 -04002091static int _mv88e6xxx_port_fdb_load(struct mv88e6xxx_chip *chip, int port,
Vivien Didelotfd231c82015-08-10 09:09:50 -04002092 const unsigned char *addr, u16 vid,
2093 u8 state)
2094{
2095 struct mv88e6xxx_atu_entry entry = { 0 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002096 struct mv88e6xxx_vtu_stu_entry vlan;
2097 int err;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002098
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002099 /* Null VLAN ID corresponds to the port private database */
2100 if (vid == 0)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002101 err = _mv88e6xxx_port_fid_get(chip, port, &vlan.fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002102 else
Vivien Didelotfad09c72016-06-21 12:28:20 -04002103 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002104 if (err)
2105 return err;
2106
2107 entry.fid = vlan.fid;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002108 entry.state = state;
2109 ether_addr_copy(entry.mac, addr);
2110 if (state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2111 entry.trunk = false;
2112 entry.portv_trunkid = BIT(port);
2113 }
2114
Vivien Didelotfad09c72016-06-21 12:28:20 -04002115 return _mv88e6xxx_atu_load(chip, &entry);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002116}
2117
Vivien Didelotf81ec902016-05-09 13:22:58 -04002118static int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
2119 const struct switchdev_obj_port_fdb *fdb,
2120 struct switchdev_trans *trans)
Vivien Didelot146a3202015-10-08 11:35:12 -04002121{
2122 /* We don't need any dynamic resource from the kernel (yet),
2123 * so skip the prepare phase.
2124 */
2125 return 0;
2126}
2127
Vivien Didelotf81ec902016-05-09 13:22:58 -04002128static void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
2129 const struct switchdev_obj_port_fdb *fdb,
2130 struct switchdev_trans *trans)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002131{
Vivien Didelot1f36faf2015-10-08 11:35:13 -04002132 int state = is_multicast_ether_addr(fdb->addr) ?
David S. Millercdf09692015-08-11 12:00:37 -07002133 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2134 GLOBAL_ATU_DATA_STATE_UC_STATIC;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002135 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelot6630e232015-08-06 01:44:07 -04002136
Vivien Didelotfad09c72016-06-21 12:28:20 -04002137 mutex_lock(&chip->reg_lock);
2138 if (_mv88e6xxx_port_fdb_load(chip, port, fdb->addr, fdb->vid, state))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002139 netdev_err(ds->ports[port].netdev,
2140 "failed to load MAC address\n");
Vivien Didelotfad09c72016-06-21 12:28:20 -04002141 mutex_unlock(&chip->reg_lock);
David S. Millercdf09692015-08-11 12:00:37 -07002142}
2143
Vivien Didelotf81ec902016-05-09 13:22:58 -04002144static int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
2145 const struct switchdev_obj_port_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -07002146{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002147 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
David S. Millercdf09692015-08-11 12:00:37 -07002148 int ret;
2149
Vivien Didelotfad09c72016-06-21 12:28:20 -04002150 mutex_lock(&chip->reg_lock);
2151 ret = _mv88e6xxx_port_fdb_load(chip, port, fdb->addr, fdb->vid,
David S. Millercdf09692015-08-11 12:00:37 -07002152 GLOBAL_ATU_DATA_STATE_UNUSED);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002153 mutex_unlock(&chip->reg_lock);
David S. Millercdf09692015-08-11 12:00:37 -07002154
2155 return ret;
2156}
2157
Vivien Didelotfad09c72016-06-21 12:28:20 -04002158static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
Vivien Didelot1d194042015-08-10 09:09:51 -04002159 struct mv88e6xxx_atu_entry *entry)
David S. Millercdf09692015-08-11 12:00:37 -07002160{
Vivien Didelot1d194042015-08-10 09:09:51 -04002161 struct mv88e6xxx_atu_entry next = { 0 };
2162 int ret;
2163
2164 next.fid = fid;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002165
Vivien Didelotfad09c72016-06-21 12:28:20 -04002166 ret = _mv88e6xxx_atu_wait(chip);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002167 if (ret < 0)
2168 return ret;
2169
Vivien Didelotfad09c72016-06-21 12:28:20 -04002170 ret = _mv88e6xxx_atu_cmd(chip, fid, GLOBAL_ATU_OP_GET_NEXT_DB);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002171 if (ret < 0)
2172 return ret;
2173
Vivien Didelotfad09c72016-06-21 12:28:20 -04002174 ret = _mv88e6xxx_atu_mac_read(chip, next.mac);
Vivien Didelot1d194042015-08-10 09:09:51 -04002175 if (ret < 0)
2176 return ret;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002177
Vivien Didelotfad09c72016-06-21 12:28:20 -04002178 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_ATU_DATA);
Vivien Didelot1d194042015-08-10 09:09:51 -04002179 if (ret < 0)
2180 return ret;
2181
2182 next.state = ret & GLOBAL_ATU_DATA_STATE_MASK;
2183 if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2184 unsigned int mask, shift;
2185
2186 if (ret & GLOBAL_ATU_DATA_TRUNK) {
2187 next.trunk = true;
2188 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
2189 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
2190 } else {
2191 next.trunk = false;
2192 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
2193 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
2194 }
2195
2196 next.portv_trunkid = (ret & mask) >> shift;
2197 }
2198
2199 *entry = next;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002200 return 0;
2201}
2202
Vivien Didelotfad09c72016-06-21 12:28:20 -04002203static int _mv88e6xxx_port_fdb_dump_one(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04002204 u16 fid, u16 vid, int port,
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002205 struct switchdev_obj_port_fdb *fdb,
2206 int (*cb)(struct switchdev_obj *obj))
2207{
2208 struct mv88e6xxx_atu_entry addr = {
2209 .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2210 };
2211 int err;
2212
Vivien Didelotfad09c72016-06-21 12:28:20 -04002213 err = _mv88e6xxx_atu_mac_write(chip, addr.mac);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002214 if (err)
2215 return err;
2216
2217 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002218 err = _mv88e6xxx_atu_getnext(chip, fid, &addr);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002219 if (err)
2220 break;
2221
2222 if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2223 break;
2224
2225 if (!addr.trunk && addr.portv_trunkid & BIT(port)) {
2226 bool is_static = addr.state ==
2227 (is_multicast_ether_addr(addr.mac) ?
2228 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2229 GLOBAL_ATU_DATA_STATE_UC_STATIC);
2230
2231 fdb->vid = vid;
2232 ether_addr_copy(fdb->addr, addr.mac);
2233 fdb->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
2234
2235 err = cb(&fdb->obj);
2236 if (err)
2237 break;
2238 }
2239 } while (!is_broadcast_ether_addr(addr.mac));
2240
2241 return err;
2242}
2243
Vivien Didelotf81ec902016-05-09 13:22:58 -04002244static int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
2245 struct switchdev_obj_port_fdb *fdb,
2246 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotf33475b2015-10-22 09:34:41 -04002247{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002248 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002249 struct mv88e6xxx_vtu_stu_entry vlan = {
2250 .vid = GLOBAL_VTU_VID_MASK, /* all ones */
2251 };
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002252 u16 fid;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002253 int err;
2254
Vivien Didelotfad09c72016-06-21 12:28:20 -04002255 mutex_lock(&chip->reg_lock);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002256
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002257 /* Dump port's default Filtering Information Database (VLAN ID 0) */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002258 err = _mv88e6xxx_port_fid_get(chip, port, &fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002259 if (err)
2260 goto unlock;
2261
Vivien Didelotfad09c72016-06-21 12:28:20 -04002262 err = _mv88e6xxx_port_fdb_dump_one(chip, fid, 0, port, fdb, cb);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002263 if (err)
2264 goto unlock;
2265
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002266 /* Dump VLANs' Filtering Information Databases */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002267 err = _mv88e6xxx_vtu_vid_write(chip, vlan.vid);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002268 if (err)
2269 goto unlock;
2270
2271 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002272 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002273 if (err)
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002274 break;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002275
2276 if (!vlan.valid)
2277 break;
2278
Vivien Didelotfad09c72016-06-21 12:28:20 -04002279 err = _mv88e6xxx_port_fdb_dump_one(chip, vlan.fid, vlan.vid,
2280 port, fdb, cb);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002281 if (err)
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002282 break;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002283 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
2284
2285unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002286 mutex_unlock(&chip->reg_lock);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002287
2288 return err;
2289}
2290
Vivien Didelotf81ec902016-05-09 13:22:58 -04002291static int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
2292 struct net_device *bridge)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002293{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002294 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Colin Ian King1d9619d2016-04-25 23:11:22 +01002295 int i, err = 0;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002296
Vivien Didelotfad09c72016-06-21 12:28:20 -04002297 mutex_lock(&chip->reg_lock);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002298
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002299 /* Assign the bridge and remap each port's VLANTable */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002300 chip->ports[port].bridge_dev = bridge;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002301
Vivien Didelotfad09c72016-06-21 12:28:20 -04002302 for (i = 0; i < chip->info->num_ports; ++i) {
2303 if (chip->ports[i].bridge_dev == bridge) {
2304 err = _mv88e6xxx_port_based_vlan_map(chip, i);
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002305 if (err)
2306 break;
2307 }
2308 }
2309
Vivien Didelotfad09c72016-06-21 12:28:20 -04002310 mutex_unlock(&chip->reg_lock);
Vivien Didelota6692752016-02-12 12:09:39 -05002311
Vivien Didelot466dfa02016-02-26 13:16:05 -05002312 return err;
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002313}
2314
Vivien Didelotf81ec902016-05-09 13:22:58 -04002315static void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002316{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002317 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2318 struct net_device *bridge = chip->ports[port].bridge_dev;
Vivien Didelot16bfa702016-03-13 16:21:33 -04002319 int i;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002320
Vivien Didelotfad09c72016-06-21 12:28:20 -04002321 mutex_lock(&chip->reg_lock);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002322
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002323 /* Unassign the bridge and remap each port's VLANTable */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002324 chip->ports[port].bridge_dev = NULL;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002325
Vivien Didelotfad09c72016-06-21 12:28:20 -04002326 for (i = 0; i < chip->info->num_ports; ++i)
2327 if (i == port || chip->ports[i].bridge_dev == bridge)
2328 if (_mv88e6xxx_port_based_vlan_map(chip, i))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002329 netdev_warn(ds->ports[i].netdev,
2330 "failed to remap\n");
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002331
Vivien Didelotfad09c72016-06-21 12:28:20 -04002332 mutex_unlock(&chip->reg_lock);
Vivien Didelot66d9cd02016-02-05 14:07:14 -05002333}
2334
Vivien Didelotfad09c72016-06-21 12:28:20 -04002335static int mv88e6xxx_switch_reset(struct mv88e6xxx_chip *chip)
Vivien Didelot552238b2016-05-09 13:22:49 -04002336{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002337 bool ppu_active = mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE);
Vivien Didelot552238b2016-05-09 13:22:49 -04002338 u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002339 struct gpio_desc *gpiod = chip->reset;
Vivien Didelot552238b2016-05-09 13:22:49 -04002340 unsigned long timeout;
2341 int ret;
2342 int i;
2343
2344 /* Set all ports to the disabled state. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002345 for (i = 0; i < chip->info->num_ports; i++) {
2346 ret = _mv88e6xxx_reg_read(chip, REG_PORT(i), PORT_CONTROL);
Vivien Didelot552238b2016-05-09 13:22:49 -04002347 if (ret < 0)
2348 return ret;
2349
Vivien Didelotfad09c72016-06-21 12:28:20 -04002350 ret = _mv88e6xxx_reg_write(chip, REG_PORT(i), PORT_CONTROL,
Vivien Didelot552238b2016-05-09 13:22:49 -04002351 ret & 0xfffc);
2352 if (ret)
2353 return ret;
2354 }
2355
2356 /* Wait for transmit queues to drain. */
2357 usleep_range(2000, 4000);
2358
2359 /* If there is a gpio connected to the reset pin, toggle it */
2360 if (gpiod) {
2361 gpiod_set_value_cansleep(gpiod, 1);
2362 usleep_range(10000, 20000);
2363 gpiod_set_value_cansleep(gpiod, 0);
2364 usleep_range(10000, 20000);
2365 }
2366
2367 /* Reset the switch. Keep the PPU active if requested. The PPU
2368 * needs to be active to support indirect phy register access
2369 * through global registers 0x18 and 0x19.
2370 */
2371 if (ppu_active)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002372 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, 0x04, 0xc000);
Vivien Didelot552238b2016-05-09 13:22:49 -04002373 else
Vivien Didelotfad09c72016-06-21 12:28:20 -04002374 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, 0x04, 0xc400);
Vivien Didelot552238b2016-05-09 13:22:49 -04002375 if (ret)
2376 return ret;
2377
2378 /* Wait up to one second for reset to complete. */
2379 timeout = jiffies + 1 * HZ;
2380 while (time_before(jiffies, timeout)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002381 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, 0x00);
Vivien Didelot552238b2016-05-09 13:22:49 -04002382 if (ret < 0)
2383 return ret;
2384
2385 if ((ret & is_reset) == is_reset)
2386 break;
2387 usleep_range(1000, 2000);
2388 }
2389 if (time_after(jiffies, timeout))
2390 ret = -ETIMEDOUT;
2391 else
2392 ret = 0;
2393
2394 return ret;
2395}
2396
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002397static int mv88e6xxx_serdes_power_on(struct mv88e6xxx_chip *chip)
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002398{
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002399 u16 val;
2400 int err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002401
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002402 /* Clear Power Down bit */
2403 err = mv88e6xxx_serdes_read(chip, MII_BMCR, &val);
2404 if (err)
2405 return err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002406
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002407 if (val & BMCR_PDOWN) {
2408 val &= ~BMCR_PDOWN;
2409 err = mv88e6xxx_serdes_write(chip, MII_BMCR, val);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002410 }
2411
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002412 return err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002413}
2414
Vivien Didelot8f6345b2016-07-20 18:18:36 -04002415static int mv88e6xxx_port_read(struct mv88e6xxx_chip *chip, int port,
2416 int reg, u16 *val)
2417{
2418 int addr = chip->info->port_base_addr + port;
2419
2420 if (port >= chip->info->num_ports)
2421 return -EINVAL;
2422
2423 return mv88e6xxx_read(chip, addr, reg, val);
2424}
2425
Vivien Didelotfad09c72016-06-21 12:28:20 -04002426static int mv88e6xxx_setup_port(struct mv88e6xxx_chip *chip, int port)
Guenter Roeckd827e882015-03-26 18:36:29 -07002427{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002428 struct dsa_switch *ds = chip->ds;
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002429 int ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002430 u16 reg;
Guenter Roeckd827e882015-03-26 18:36:29 -07002431
Vivien Didelotfad09c72016-06-21 12:28:20 -04002432 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2433 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2434 mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip) ||
2435 mv88e6xxx_6065_family(chip) || mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002436 /* MAC Forcing register: don't force link, speed,
2437 * duplex or flow control state to any particular
2438 * values on physical ports, but force the CPU port
2439 * and all DSA ports to their maximum bandwidth and
2440 * full duplex.
2441 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002442 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_PCS_CTRL);
Andrew Lunn60045cb2015-08-17 23:52:51 +02002443 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
Russell King53adc9e2015-09-21 21:42:59 +01002444 reg &= ~PORT_PCS_CTRL_UNFORCED;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002445 reg |= PORT_PCS_CTRL_FORCE_LINK |
2446 PORT_PCS_CTRL_LINK_UP |
2447 PORT_PCS_CTRL_DUPLEX_FULL |
2448 PORT_PCS_CTRL_FORCE_DUPLEX;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002449 if (mv88e6xxx_6065_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002450 reg |= PORT_PCS_CTRL_100;
2451 else
2452 reg |= PORT_PCS_CTRL_1000;
2453 } else {
2454 reg |= PORT_PCS_CTRL_UNFORCED;
2455 }
2456
Vivien Didelotfad09c72016-06-21 12:28:20 -04002457 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002458 PORT_PCS_CTRL, reg);
2459 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002460 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002461 }
2462
2463 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
2464 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
2465 * tunneling, determine priority by looking at 802.1p and IP
2466 * priority fields (IP prio has precedence), and set STP state
2467 * to Forwarding.
2468 *
2469 * If this is the CPU link, use DSA or EDSA tagging depending
2470 * on which tagging mode was configured.
2471 *
2472 * If this is a link to another switch, use DSA tagging mode.
2473 *
2474 * If this is the upstream port for this switch, enable
2475 * forwarding of unknown unicasts and multicasts.
2476 */
2477 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002478 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2479 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2480 mv88e6xxx_6095_family(chip) || mv88e6xxx_6065_family(chip) ||
2481 mv88e6xxx_6185_family(chip) || mv88e6xxx_6320_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002482 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
2483 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
2484 PORT_CONTROL_STATE_FORWARDING;
2485 if (dsa_is_cpu_port(ds, port)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002486 if (mv88e6xxx_6095_family(chip) || mv88e6xxx_6185_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002487 reg |= PORT_CONTROL_DSA_TAG;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002488 if (mv88e6xxx_6352_family(chip) ||
2489 mv88e6xxx_6351_family(chip) ||
2490 mv88e6xxx_6165_family(chip) ||
2491 mv88e6xxx_6097_family(chip) ||
2492 mv88e6xxx_6320_family(chip)) {
Andrew Lunn5377b802016-06-04 21:17:02 +02002493 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA |
2494 PORT_CONTROL_FORWARD_UNKNOWN |
Andrew Lunnc047a1f2015-09-29 01:50:56 +02002495 PORT_CONTROL_FORWARD_UNKNOWN_MC;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002496 }
2497
Vivien Didelotfad09c72016-06-21 12:28:20 -04002498 if (mv88e6xxx_6352_family(chip) ||
2499 mv88e6xxx_6351_family(chip) ||
2500 mv88e6xxx_6165_family(chip) ||
2501 mv88e6xxx_6097_family(chip) ||
2502 mv88e6xxx_6095_family(chip) ||
2503 mv88e6xxx_6065_family(chip) ||
2504 mv88e6xxx_6185_family(chip) ||
2505 mv88e6xxx_6320_family(chip)) {
Vivien Didelot57d32312016-06-20 13:13:58 -04002506 reg |= PORT_CONTROL_EGRESS_ADD_TAG;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002507 }
2508 }
Andrew Lunn6083ce72015-08-17 23:52:52 +02002509 if (dsa_is_dsa_port(ds, port)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002510 if (mv88e6xxx_6095_family(chip) ||
2511 mv88e6xxx_6185_family(chip))
Andrew Lunn6083ce72015-08-17 23:52:52 +02002512 reg |= PORT_CONTROL_DSA_TAG;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002513 if (mv88e6xxx_6352_family(chip) ||
2514 mv88e6xxx_6351_family(chip) ||
2515 mv88e6xxx_6165_family(chip) ||
2516 mv88e6xxx_6097_family(chip) ||
2517 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002518 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunn6083ce72015-08-17 23:52:52 +02002519 }
2520
Andrew Lunn54d792f2015-05-06 01:09:47 +02002521 if (port == dsa_upstream_port(ds))
2522 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2523 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2524 }
2525 if (reg) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002526 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002527 PORT_CONTROL, reg);
2528 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002529 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002530 }
2531
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002532 /* If this port is connected to a SerDes, make sure the SerDes is not
2533 * powered down.
2534 */
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002535 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_SERDES)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002536 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_STATUS);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002537 if (ret < 0)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002538 return ret;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002539 ret &= PORT_STATUS_CMODE_MASK;
2540 if ((ret == PORT_STATUS_CMODE_100BASE_X) ||
2541 (ret == PORT_STATUS_CMODE_1000BASE_X) ||
2542 (ret == PORT_STATUS_CMODE_SGMII)) {
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002543 ret = mv88e6xxx_serdes_power_on(chip);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002544 if (ret < 0)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002545 return ret;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002546 }
2547 }
2548
Vivien Didelot8efdda42015-08-13 12:52:23 -04002549 /* Port Control 2: don't force a good FCS, set the maximum frame size to
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002550 * 10240 bytes, disable 802.1q tags checking, don't discard tagged or
Vivien Didelot8efdda42015-08-13 12:52:23 -04002551 * untagged frames on this port, do a destination address lookup on all
2552 * received packets as usual, disable ARP mirroring and don't send a
2553 * copy of all transmitted/received frames on this port to the CPU.
Andrew Lunn54d792f2015-05-06 01:09:47 +02002554 */
2555 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002556 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2557 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2558 mv88e6xxx_6095_family(chip) || mv88e6xxx_6320_family(chip) ||
2559 mv88e6xxx_6185_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002560 reg = PORT_CONTROL_2_MAP_DA;
2561
Vivien Didelotfad09c72016-06-21 12:28:20 -04002562 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2563 mv88e6xxx_6165_family(chip) || mv88e6xxx_6320_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002564 reg |= PORT_CONTROL_2_JUMBO_10240;
2565
Vivien Didelotfad09c72016-06-21 12:28:20 -04002566 if (mv88e6xxx_6095_family(chip) || mv88e6xxx_6185_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002567 /* Set the upstream port this port should use */
2568 reg |= dsa_upstream_port(ds);
2569 /* enable forwarding of unknown multicast addresses to
2570 * the upstream port
2571 */
2572 if (port == dsa_upstream_port(ds))
2573 reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
2574 }
2575
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002576 reg |= PORT_CONTROL_2_8021Q_DISABLED;
Vivien Didelot8efdda42015-08-13 12:52:23 -04002577
Andrew Lunn54d792f2015-05-06 01:09:47 +02002578 if (reg) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002579 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002580 PORT_CONTROL_2, reg);
2581 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002582 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002583 }
2584
2585 /* Port Association Vector: when learning source addresses
2586 * of packets, add the address to the address database using
2587 * a port bitmap that has only the bit for this port set and
2588 * the other bits clear.
2589 */
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002590 reg = 1 << port;
Vivien Didelot996ecb82016-04-14 14:42:08 -04002591 /* Disable learning for CPU port */
2592 if (dsa_is_cpu_port(ds, port))
Vivien Didelot65fa4022016-04-14 14:42:07 -04002593 reg = 0;
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002594
Vivien Didelotfad09c72016-06-21 12:28:20 -04002595 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_ASSOC_VECTOR,
2596 reg);
Andrew Lunn54d792f2015-05-06 01:09:47 +02002597 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002598 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002599
2600 /* Egress rate control 2: disable egress rate control. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002601 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_RATE_CONTROL_2,
Andrew Lunn54d792f2015-05-06 01:09:47 +02002602 0x0000);
2603 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002604 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002605
Vivien Didelotfad09c72016-06-21 12:28:20 -04002606 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2607 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2608 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002609 /* Do not limit the period of time that this port can
2610 * be paused for by the remote end or the period of
2611 * time that this port can pause the remote end.
2612 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002613 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002614 PORT_PAUSE_CTRL, 0x0000);
2615 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002616 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002617
2618 /* Port ATU control: disable limiting the number of
2619 * address database entries that this port is allowed
2620 * to use.
2621 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002622 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002623 PORT_ATU_CONTROL, 0x0000);
2624 /* Priority Override: disable DA, SA and VTU priority
2625 * override.
2626 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002627 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002628 PORT_PRI_OVERRIDE, 0x0000);
2629 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002630 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002631
2632 /* Port Ethertype: use the Ethertype DSA Ethertype
2633 * value.
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_ETH_TYPE, ETH_P_EDSA);
2637 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002638 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002639 /* Tag Remap: use an identity 802.1p prio -> switch
2640 * prio mapping.
2641 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002642 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002643 PORT_TAG_REGMAP_0123, 0x3210);
2644 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002645 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002646
2647 /* Tag Remap 2: use an identity 802.1p prio -> switch
2648 * prio mapping.
2649 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002650 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002651 PORT_TAG_REGMAP_4567, 0x7654);
2652 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002653 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002654 }
2655
Vivien Didelotfad09c72016-06-21 12:28:20 -04002656 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2657 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2658 mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip) ||
2659 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002660 /* Rate Control: disable ingress rate limiting. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002661 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002662 PORT_RATE_CONTROL, 0x0001);
2663 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002664 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002665 }
2666
Guenter Roeck366f0a02015-03-26 18:36:30 -07002667 /* Port Control 1: disable trunking, disable sending
2668 * learning messages to this port.
Guenter Roeckd827e882015-03-26 18:36:29 -07002669 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002670 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_1,
2671 0x0000);
Guenter Roeckd827e882015-03-26 18:36:29 -07002672 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002673 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07002674
Vivien Didelot207afda2016-04-14 14:42:09 -04002675 /* Port based VLAN map: give each port the same default address
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002676 * database, and allow bidirectional communication between the
2677 * CPU and DSA port(s), and the other ports.
Guenter Roeckd827e882015-03-26 18:36:29 -07002678 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002679 ret = _mv88e6xxx_port_fid_set(chip, port, 0);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002680 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002681 return ret;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002682
Vivien Didelotfad09c72016-06-21 12:28:20 -04002683 ret = _mv88e6xxx_port_based_vlan_map(chip, port);
Guenter Roeckd827e882015-03-26 18:36:29 -07002684 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002685 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07002686
2687 /* Default VLAN ID and priority: don't set a default VLAN
2688 * ID, and set the default packet priority to zero.
2689 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002690 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_DEFAULT_VLAN,
Vivien Didelot47cf1e652015-04-20 17:43:26 -04002691 0x0000);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002692 if (ret)
2693 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07002694
Andrew Lunndbde9e62015-05-06 01:09:48 +02002695 return 0;
2696}
2697
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002698static int mv88e6xxx_g1_set_switch_mac(struct mv88e6xxx_chip *chip, u8 *addr)
2699{
2700 int err;
2701
2702 err = mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_MAC_01,
2703 (addr[0] << 8) | addr[1]);
2704 if (err)
2705 return err;
2706
2707 err = mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_MAC_23,
2708 (addr[2] << 8) | addr[3]);
2709 if (err)
2710 return err;
2711
2712 return mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_MAC_45,
2713 (addr[4] << 8) | addr[5]);
2714}
2715
Vivien Didelotacddbd22016-07-18 20:45:39 -04002716static int mv88e6xxx_g1_set_age_time(struct mv88e6xxx_chip *chip,
2717 unsigned int msecs)
2718{
2719 const unsigned int coeff = chip->info->age_time_coeff;
2720 const unsigned int min = 0x01 * coeff;
2721 const unsigned int max = 0xff * coeff;
2722 u8 age_time;
2723 u16 val;
2724 int err;
2725
2726 if (msecs < min || msecs > max)
2727 return -ERANGE;
2728
2729 /* Round to nearest multiple of coeff */
2730 age_time = (msecs + coeff / 2) / coeff;
2731
2732 err = mv88e6xxx_read(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL, &val);
2733 if (err)
2734 return err;
2735
2736 /* AgeTime is 11:4 bits */
2737 val &= ~0xff0;
2738 val |= age_time << 4;
2739
2740 return mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL, val);
2741}
2742
Vivien Didelot2cfcd962016-07-18 20:45:40 -04002743static int mv88e6xxx_set_ageing_time(struct dsa_switch *ds,
2744 unsigned int ageing_time)
2745{
2746 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2747 int err;
2748
2749 mutex_lock(&chip->reg_lock);
2750 err = mv88e6xxx_g1_set_age_time(chip, ageing_time);
2751 mutex_unlock(&chip->reg_lock);
2752
2753 return err;
2754}
2755
Vivien Didelot97299342016-07-18 20:45:30 -04002756static int mv88e6xxx_g1_setup(struct mv88e6xxx_chip *chip)
Vivien Didelot08a01262016-05-09 13:22:50 -04002757{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002758 struct dsa_switch *ds = chip->ds;
Vivien Didelotb0745e872016-05-09 13:22:53 -04002759 u32 upstream_port = dsa_upstream_port(ds);
Vivien Didelot119477b2016-05-09 13:22:51 -04002760 u16 reg;
Vivien Didelot08a01262016-05-09 13:22:50 -04002761 int err;
Vivien Didelot08a01262016-05-09 13:22:50 -04002762
Vivien Didelot119477b2016-05-09 13:22:51 -04002763 /* Enable the PHY Polling Unit if present, don't discard any packets,
2764 * and mask all interrupt sources.
2765 */
2766 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002767 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU) ||
2768 mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE))
Vivien Didelot119477b2016-05-09 13:22:51 -04002769 reg |= GLOBAL_CONTROL_PPU_ENABLE;
2770
Vivien Didelotfad09c72016-06-21 12:28:20 -04002771 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL, reg);
Vivien Didelot119477b2016-05-09 13:22:51 -04002772 if (err)
2773 return err;
2774
Vivien Didelotb0745e872016-05-09 13:22:53 -04002775 /* Configure the upstream port, and configure it as the port to which
2776 * ingress and egress and ARP monitor frames are to be sent.
2777 */
2778 reg = upstream_port << GLOBAL_MONITOR_CONTROL_INGRESS_SHIFT |
2779 upstream_port << GLOBAL_MONITOR_CONTROL_EGRESS_SHIFT |
2780 upstream_port << GLOBAL_MONITOR_CONTROL_ARP_SHIFT;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002781 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_MONITOR_CONTROL,
2782 reg);
Vivien Didelotb0745e872016-05-09 13:22:53 -04002783 if (err)
2784 return err;
2785
Vivien Didelot50484ff2016-05-09 13:22:54 -04002786 /* Disable remote management, and set the switch's DSA device number. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002787 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL_2,
Vivien Didelot50484ff2016-05-09 13:22:54 -04002788 GLOBAL_CONTROL_2_MULTIPLE_CASCADE |
2789 (ds->index & 0x1f));
2790 if (err)
2791 return err;
2792
Vivien Didelotacddbd22016-07-18 20:45:39 -04002793 /* Clear all the VTU and STU entries */
2794 err = _mv88e6xxx_vtu_stu_flush(chip);
2795 if (err < 0)
2796 return err;
2797
Vivien Didelot08a01262016-05-09 13:22:50 -04002798 /* Set the default address aging time to 5 minutes, and
2799 * enable address learn messages to be sent to all message
2800 * ports.
2801 */
Vivien Didelotacddbd22016-07-18 20:45:39 -04002802 err = mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL,
2803 GLOBAL_ATU_CONTROL_LEARN2ALL);
Vivien Didelot08a01262016-05-09 13:22:50 -04002804 if (err)
2805 return err;
2806
Vivien Didelotacddbd22016-07-18 20:45:39 -04002807 err = mv88e6xxx_g1_set_age_time(chip, 300000);
2808 if (err)
Vivien Didelot97299342016-07-18 20:45:30 -04002809 return err;
2810
2811 /* Clear all ATU entries */
2812 err = _mv88e6xxx_atu_flush(chip, 0, true);
2813 if (err)
2814 return err;
2815
Vivien Didelot08a01262016-05-09 13:22:50 -04002816 /* Configure the IP ToS mapping registers. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002817 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_0, 0x0000);
Vivien Didelot08a01262016-05-09 13:22:50 -04002818 if (err)
2819 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002820 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_1, 0x0000);
Vivien Didelot08a01262016-05-09 13:22:50 -04002821 if (err)
2822 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002823 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_2, 0x5555);
Vivien Didelot08a01262016-05-09 13:22:50 -04002824 if (err)
2825 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002826 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_3, 0x5555);
Vivien Didelot08a01262016-05-09 13:22:50 -04002827 if (err)
2828 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002829 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_4, 0xaaaa);
Vivien Didelot08a01262016-05-09 13:22:50 -04002830 if (err)
2831 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002832 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_5, 0xaaaa);
Vivien Didelot08a01262016-05-09 13:22:50 -04002833 if (err)
2834 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002835 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_6, 0xffff);
Vivien Didelot08a01262016-05-09 13:22:50 -04002836 if (err)
2837 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002838 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_7, 0xffff);
Vivien Didelot08a01262016-05-09 13:22:50 -04002839 if (err)
2840 return err;
2841
2842 /* Configure the IEEE 802.1p priority mapping register. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002843 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IEEE_PRI, 0xfa41);
Vivien Didelot08a01262016-05-09 13:22:50 -04002844 if (err)
2845 return err;
2846
Vivien Didelot97299342016-07-18 20:45:30 -04002847 /* Clear the statistics counters for all ports */
2848 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
2849 GLOBAL_STATS_OP_FLUSH_ALL);
2850 if (err)
2851 return err;
2852
2853 /* Wait for the flush to complete. */
2854 err = _mv88e6xxx_stats_wait(chip);
2855 if (err)
2856 return err;
2857
2858 return 0;
2859}
2860
Vivien Didelotf22ab642016-07-18 20:45:31 -04002861static int mv88e6xxx_g2_device_mapping_write(struct mv88e6xxx_chip *chip,
2862 int target, int port)
2863{
2864 u16 val = (target << 8) | (port & 0xf);
2865
2866 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_DEVICE_MAPPING, val);
2867}
2868
2869static int mv88e6xxx_g2_set_device_mapping(struct mv88e6xxx_chip *chip)
2870{
2871 int target, port;
2872 int err;
2873
2874 /* Initialize the routing port to the 32 possible target devices */
2875 for (target = 0; target < 32; ++target) {
2876 port = 0xf;
2877
2878 if (target < DSA_MAX_SWITCHES) {
2879 port = chip->ds->rtable[target];
2880 if (port == DSA_RTABLE_NONE)
2881 port = 0xf;
2882 }
2883
2884 err = mv88e6xxx_g2_device_mapping_write(chip, target, port);
2885 if (err)
2886 break;
2887 }
2888
2889 return err;
2890}
2891
Vivien Didelot51540412016-07-18 20:45:32 -04002892static int mv88e6xxx_g2_trunk_mask_write(struct mv88e6xxx_chip *chip, int num,
2893 bool hask, u16 mask)
2894{
2895 const u16 port_mask = BIT(chip->info->num_ports) - 1;
2896 u16 val = (num << 12) | (mask & port_mask);
2897
2898 if (hask)
2899 val |= GLOBAL2_TRUNK_MASK_HASK;
2900
2901 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_TRUNK_MASK, val);
2902}
2903
2904static int mv88e6xxx_g2_trunk_mapping_write(struct mv88e6xxx_chip *chip, int id,
2905 u16 map)
2906{
2907 const u16 port_mask = BIT(chip->info->num_ports) - 1;
2908 u16 val = (id << 11) | (map & port_mask);
2909
2910 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_TRUNK_MAPPING, val);
2911}
2912
2913static int mv88e6xxx_g2_clear_trunk(struct mv88e6xxx_chip *chip)
2914{
2915 const u16 port_mask = BIT(chip->info->num_ports) - 1;
2916 int i, err;
2917
2918 /* Clear all eight possible Trunk Mask vectors */
2919 for (i = 0; i < 8; ++i) {
2920 err = mv88e6xxx_g2_trunk_mask_write(chip, i, false, port_mask);
2921 if (err)
2922 return err;
2923 }
2924
2925 /* Clear all sixteen possible Trunk ID routing vectors */
2926 for (i = 0; i < 16; ++i) {
2927 err = mv88e6xxx_g2_trunk_mapping_write(chip, i, 0);
2928 if (err)
2929 return err;
2930 }
2931
2932 return 0;
2933}
2934
Vivien Didelot8ec61c72016-07-18 20:45:37 -04002935static int mv88e6xxx_g2_clear_irl(struct mv88e6xxx_chip *chip)
2936{
2937 int port, err;
2938
2939 /* Init all Ingress Rate Limit resources of all ports */
2940 for (port = 0; port < chip->info->num_ports; ++port) {
2941 /* XXX newer chips (like 88E6390) have different 2-bit ops */
2942 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_IRL_CMD,
2943 GLOBAL2_IRL_CMD_OP_INIT_ALL |
2944 (port << 8));
2945 if (err)
2946 break;
2947
2948 /* Wait for the operation to complete */
Vivien Didelot2d79af62016-08-15 17:18:57 -04002949 err = mv88e6xxx_wait(chip, REG_GLOBAL2, GLOBAL2_IRL_CMD,
2950 GLOBAL2_IRL_CMD_BUSY);
Vivien Didelot8ec61c72016-07-18 20:45:37 -04002951 if (err)
2952 break;
2953 }
2954
2955 return err;
2956}
2957
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002958/* Indirect write to the Switch MAC/WoL/WoF register */
2959static int mv88e6xxx_g2_switch_mac_write(struct mv88e6xxx_chip *chip,
2960 unsigned int pointer, u8 data)
2961{
2962 u16 val = (pointer << 8) | data;
2963
2964 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_SWITCH_MAC, val);
2965}
2966
2967static int mv88e6xxx_g2_set_switch_mac(struct mv88e6xxx_chip *chip, u8 *addr)
2968{
2969 int i, err;
2970
2971 for (i = 0; i < 6; i++) {
2972 err = mv88e6xxx_g2_switch_mac_write(chip, i, addr[i]);
2973 if (err)
2974 break;
2975 }
2976
2977 return err;
2978}
2979
Vivien Didelot9bda8892016-07-18 20:45:36 -04002980static int mv88e6xxx_g2_pot_write(struct mv88e6xxx_chip *chip, int pointer,
2981 u8 data)
2982{
2983 u16 val = (pointer << 8) | (data & 0x7);
2984
2985 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_PRIO_OVERRIDE, val);
2986}
2987
2988static int mv88e6xxx_g2_clear_pot(struct mv88e6xxx_chip *chip)
2989{
2990 int i, err;
2991
2992 /* Clear all sixteen possible Priority Override entries */
2993 for (i = 0; i < 16; i++) {
2994 err = mv88e6xxx_g2_pot_write(chip, i, 0);
2995 if (err)
2996 break;
2997 }
2998
2999 return err;
3000}
3001
Vivien Didelot855b1932016-07-20 18:18:35 -04003002static int mv88e6xxx_g2_eeprom_wait(struct mv88e6xxx_chip *chip)
3003{
Vivien Didelot2d79af62016-08-15 17:18:57 -04003004 return mv88e6xxx_wait(chip, REG_GLOBAL2, GLOBAL2_EEPROM_CMD,
3005 GLOBAL2_EEPROM_CMD_BUSY |
3006 GLOBAL2_EEPROM_CMD_RUNNING);
Vivien Didelot855b1932016-07-20 18:18:35 -04003007}
3008
3009static int mv88e6xxx_g2_eeprom_cmd(struct mv88e6xxx_chip *chip, u16 cmd)
3010{
3011 int err;
3012
3013 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_EEPROM_CMD, cmd);
3014 if (err)
3015 return err;
3016
3017 return mv88e6xxx_g2_eeprom_wait(chip);
3018}
3019
3020static int mv88e6xxx_g2_eeprom_read16(struct mv88e6xxx_chip *chip,
3021 u8 addr, u16 *data)
3022{
3023 u16 cmd = GLOBAL2_EEPROM_CMD_OP_READ | addr;
3024 int err;
3025
3026 err = mv88e6xxx_g2_eeprom_wait(chip);
3027 if (err)
3028 return err;
3029
3030 err = mv88e6xxx_g2_eeprom_cmd(chip, cmd);
3031 if (err)
3032 return err;
3033
3034 return mv88e6xxx_read(chip, REG_GLOBAL2, GLOBAL2_EEPROM_DATA, data);
3035}
3036
3037static int mv88e6xxx_g2_eeprom_write16(struct mv88e6xxx_chip *chip,
3038 u8 addr, u16 data)
3039{
3040 u16 cmd = GLOBAL2_EEPROM_CMD_OP_WRITE | addr;
3041 int err;
3042
3043 err = mv88e6xxx_g2_eeprom_wait(chip);
3044 if (err)
3045 return err;
3046
3047 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_EEPROM_DATA, data);
3048 if (err)
3049 return err;
3050
3051 return mv88e6xxx_g2_eeprom_cmd(chip, cmd);
3052}
3053
Vivien Didelot57c67cf2016-08-15 17:18:59 -04003054static int mv88e6xxx_g2_smi_phy_wait(struct mv88e6xxx_chip *chip)
3055{
3056 return mv88e6xxx_wait(chip, REG_GLOBAL2, GLOBAL2_SMI_PHY_CMD,
3057 GLOBAL2_SMI_PHY_CMD_BUSY);
3058}
3059
3060static int mv88e6xxx_g2_smi_phy_cmd(struct mv88e6xxx_chip *chip, u16 cmd)
3061{
3062 int err;
3063
3064 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_SMI_PHY_CMD, cmd);
3065 if (err)
3066 return err;
3067
3068 return mv88e6xxx_g2_smi_phy_wait(chip);
3069}
3070
3071static int mv88e6xxx_g2_smi_phy_read(struct mv88e6xxx_chip *chip, int addr,
3072 int reg, u16 *val)
3073{
3074 u16 cmd = GLOBAL2_SMI_PHY_CMD_OP_22_READ_DATA | (addr << 5) | reg;
3075 int err;
3076
3077 err = mv88e6xxx_g2_smi_phy_wait(chip);
3078 if (err)
3079 return err;
3080
3081 err = mv88e6xxx_g2_smi_phy_cmd(chip, cmd);
3082 if (err)
3083 return err;
3084
3085 return mv88e6xxx_read(chip, REG_GLOBAL2, GLOBAL2_SMI_PHY_DATA, val);
3086}
3087
3088static int mv88e6xxx_g2_smi_phy_write(struct mv88e6xxx_chip *chip, int addr,
3089 int reg, u16 val)
3090{
3091 u16 cmd = GLOBAL2_SMI_PHY_CMD_OP_22_WRITE_DATA | (addr << 5) | reg;
3092 int err;
3093
3094 err = mv88e6xxx_g2_smi_phy_wait(chip);
3095 if (err)
3096 return err;
3097
3098 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_SMI_PHY_DATA, val);
3099 if (err)
3100 return err;
3101
3102 return mv88e6xxx_g2_smi_phy_cmd(chip, cmd);
3103}
3104
Vivien Didelote57e5e72016-08-15 17:19:00 -04003105static const struct mv88e6xxx_ops mv88e6xxx_g2_smi_phy_ops = {
3106 .read = mv88e6xxx_g2_smi_phy_read,
3107 .write = mv88e6xxx_g2_smi_phy_write,
3108};
3109
Vivien Didelot97299342016-07-18 20:45:30 -04003110static int mv88e6xxx_g2_setup(struct mv88e6xxx_chip *chip)
3111{
Vivien Didelot47395ed2016-07-18 20:45:33 -04003112 u16 reg;
Vivien Didelot97299342016-07-18 20:45:30 -04003113 int err;
Vivien Didelot97299342016-07-18 20:45:30 -04003114
Vivien Didelot47395ed2016-07-18 20:45:33 -04003115 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_2X)) {
3116 /* Consider the frames with reserved multicast destination
3117 * addresses matching 01:80:c2:00:00:2x as MGMT.
3118 */
3119 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_MGMT_EN_2X,
3120 0xffff);
3121 if (err)
3122 return err;
3123 }
3124
3125 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_0X)) {
3126 /* Consider the frames with reserved multicast destination
3127 * addresses matching 01:80:c2:00:00:0x as MGMT.
3128 */
3129 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_MGMT_EN_0X,
3130 0xffff);
3131 if (err)
3132 return err;
3133 }
Vivien Didelot08a01262016-05-09 13:22:50 -04003134
3135 /* Ignore removed tag data on doubly tagged packets, disable
3136 * flow control messages, force flow control priority to the
3137 * highest, and send all special multicast frames to the CPU
3138 * port at the highest priority.
3139 */
Vivien Didelot47395ed2016-07-18 20:45:33 -04003140 reg = GLOBAL2_SWITCH_MGMT_FORCE_FLOW_CTRL_PRI | (0x7 << 4);
3141 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_0X) ||
3142 mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_2X))
3143 reg |= GLOBAL2_SWITCH_MGMT_RSVD2CPU | 0x7;
3144 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_SWITCH_MGMT, reg);
Vivien Didelot08a01262016-05-09 13:22:50 -04003145 if (err)
3146 return err;
3147
3148 /* Program the DSA routing table. */
Vivien Didelotf22ab642016-07-18 20:45:31 -04003149 err = mv88e6xxx_g2_set_device_mapping(chip);
3150 if (err)
3151 return err;
Vivien Didelot08a01262016-05-09 13:22:50 -04003152
Vivien Didelot51540412016-07-18 20:45:32 -04003153 /* Clear all trunk masks and mapping. */
3154 err = mv88e6xxx_g2_clear_trunk(chip);
3155 if (err)
3156 return err;
Vivien Didelot08a01262016-05-09 13:22:50 -04003157
Vivien Didelot8ec61c72016-07-18 20:45:37 -04003158 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_IRL)) {
3159 /* Disable ingress rate limiting by resetting all per port
3160 * ingress rate limit resources to their initial state.
3161 */
3162 err = mv88e6xxx_g2_clear_irl(chip);
3163 if (err)
3164 return err;
3165 }
3166
Vivien Didelot63ed8802016-07-18 20:45:35 -04003167 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_PVT)) {
3168 /* Initialize Cross-chip Port VLAN Table to reset defaults */
3169 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_PVT_ADDR,
3170 GLOBAL2_PVT_ADDR_OP_INIT_ONES);
3171 if (err)
3172 return err;
3173 }
3174
Vivien Didelot9bda8892016-07-18 20:45:36 -04003175 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_POT)) {
Vivien Didelot08a01262016-05-09 13:22:50 -04003176 /* Clear the priority override table. */
Vivien Didelot9bda8892016-07-18 20:45:36 -04003177 err = mv88e6xxx_g2_clear_pot(chip);
3178 if (err)
3179 return err;
Vivien Didelot08a01262016-05-09 13:22:50 -04003180 }
3181
Vivien Didelot97299342016-07-18 20:45:30 -04003182 return 0;
Vivien Didelot08a01262016-05-09 13:22:50 -04003183}
3184
Vivien Didelotf81ec902016-05-09 13:22:58 -04003185static int mv88e6xxx_setup(struct dsa_switch *ds)
Guenter Roeckacdaffc2015-03-26 18:36:28 -07003186{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003187 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelot552238b2016-05-09 13:22:49 -04003188 int err;
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003189 int i;
3190
Vivien Didelotfad09c72016-06-21 12:28:20 -04003191 chip->ds = ds;
3192 ds->slave_mii_bus = chip->mdio_bus;
Vivien Didelot552238b2016-05-09 13:22:49 -04003193
Vivien Didelotfad09c72016-06-21 12:28:20 -04003194 mutex_lock(&chip->reg_lock);
Vivien Didelot552238b2016-05-09 13:22:49 -04003195
Vivien Didelotfad09c72016-06-21 12:28:20 -04003196 err = mv88e6xxx_switch_reset(chip);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003197 if (err)
3198 goto unlock;
Andrew Lunn54d792f2015-05-06 01:09:47 +02003199
Vivien Didelot97299342016-07-18 20:45:30 -04003200 /* Setup Switch Port Registers */
3201 for (i = 0; i < chip->info->num_ports; i++) {
3202 err = mv88e6xxx_setup_port(chip, i);
3203 if (err)
3204 goto unlock;
3205 }
3206
3207 /* Setup Switch Global 1 Registers */
3208 err = mv88e6xxx_g1_setup(chip);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003209 if (err)
3210 goto unlock;
3211
Vivien Didelot97299342016-07-18 20:45:30 -04003212 /* Setup Switch Global 2 Registers */
3213 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_GLOBAL2)) {
3214 err = mv88e6xxx_g2_setup(chip);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003215 if (err)
3216 goto unlock;
3217 }
Andrew Lunn54d792f2015-05-06 01:09:47 +02003218
Vivien Didelot6b17e862015-08-13 12:52:18 -04003219unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04003220 mutex_unlock(&chip->reg_lock);
Andrew Lunndb687a52015-06-20 21:31:29 +02003221
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003222 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02003223}
3224
Vivien Didelot3b4caa12016-07-18 20:45:34 -04003225static int mv88e6xxx_set_addr(struct dsa_switch *ds, u8 *addr)
3226{
3227 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3228 int err;
3229
3230 mutex_lock(&chip->reg_lock);
3231
3232 /* Has an indirect Switch MAC/WoL/WoF register in Global 2? */
3233 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_SWITCH_MAC))
3234 err = mv88e6xxx_g2_set_switch_mac(chip, addr);
3235 else
3236 err = mv88e6xxx_g1_set_switch_mac(chip, addr);
3237
3238 mutex_unlock(&chip->reg_lock);
3239
3240 return err;
3241}
3242
Vivien Didelote57e5e72016-08-15 17:19:00 -04003243static int mv88e6xxx_mdio_read(struct mii_bus *bus, int phy, int reg)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003244{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003245 struct mv88e6xxx_chip *chip = bus->priv;
Vivien Didelote57e5e72016-08-15 17:19:00 -04003246 u16 val;
3247 int err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003248
Vivien Didelote57e5e72016-08-15 17:19:00 -04003249 if (phy >= chip->info->num_ports)
Andrew Lunn158bc062016-04-28 21:24:06 -04003250 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003251
Vivien Didelotfad09c72016-06-21 12:28:20 -04003252 mutex_lock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04003253 err = mv88e6xxx_phy_read(chip, phy, reg, &val);
Vivien Didelotfad09c72016-06-21 12:28:20 -04003254 mutex_unlock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04003255
3256 return err ? err : val;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003257}
3258
Vivien Didelote57e5e72016-08-15 17:19:00 -04003259static int mv88e6xxx_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003260{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003261 struct mv88e6xxx_chip *chip = bus->priv;
Vivien Didelote57e5e72016-08-15 17:19:00 -04003262 int err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003263
Vivien Didelote57e5e72016-08-15 17:19:00 -04003264 if (phy >= chip->info->num_ports)
Andrew Lunn158bc062016-04-28 21:24:06 -04003265 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003266
Vivien Didelotfad09c72016-06-21 12:28:20 -04003267 mutex_lock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04003268 err = mv88e6xxx_phy_write(chip, phy, reg, val);
Vivien Didelotfad09c72016-06-21 12:28:20 -04003269 mutex_unlock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04003270
3271 return err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003272}
3273
Vivien Didelotfad09c72016-06-21 12:28:20 -04003274static int mv88e6xxx_mdio_register(struct mv88e6xxx_chip *chip,
Andrew Lunnb516d452016-06-04 21:17:06 +02003275 struct device_node *np)
3276{
3277 static int index;
3278 struct mii_bus *bus;
3279 int err;
3280
Andrew Lunnb516d452016-06-04 21:17:06 +02003281 if (np)
Vivien Didelotfad09c72016-06-21 12:28:20 -04003282 chip->mdio_np = of_get_child_by_name(np, "mdio");
Andrew Lunnb516d452016-06-04 21:17:06 +02003283
Vivien Didelotfad09c72016-06-21 12:28:20 -04003284 bus = devm_mdiobus_alloc(chip->dev);
Andrew Lunnb516d452016-06-04 21:17:06 +02003285 if (!bus)
3286 return -ENOMEM;
3287
Vivien Didelotfad09c72016-06-21 12:28:20 -04003288 bus->priv = (void *)chip;
Andrew Lunnb516d452016-06-04 21:17:06 +02003289 if (np) {
3290 bus->name = np->full_name;
3291 snprintf(bus->id, MII_BUS_ID_SIZE, "%s", np->full_name);
3292 } else {
3293 bus->name = "mv88e6xxx SMI";
3294 snprintf(bus->id, MII_BUS_ID_SIZE, "mv88e6xxx-%d", index++);
3295 }
3296
3297 bus->read = mv88e6xxx_mdio_read;
3298 bus->write = mv88e6xxx_mdio_write;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003299 bus->parent = chip->dev;
Andrew Lunnb516d452016-06-04 21:17:06 +02003300
Vivien Didelotfad09c72016-06-21 12:28:20 -04003301 if (chip->mdio_np)
3302 err = of_mdiobus_register(bus, chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003303 else
3304 err = mdiobus_register(bus);
3305 if (err) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04003306 dev_err(chip->dev, "Cannot register MDIO bus (%d)\n", err);
Andrew Lunnb516d452016-06-04 21:17:06 +02003307 goto out;
3308 }
Vivien Didelotfad09c72016-06-21 12:28:20 -04003309 chip->mdio_bus = bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003310
3311 return 0;
3312
3313out:
Vivien Didelotfad09c72016-06-21 12:28:20 -04003314 if (chip->mdio_np)
3315 of_node_put(chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003316
3317 return err;
3318}
3319
Vivien Didelotfad09c72016-06-21 12:28:20 -04003320static void mv88e6xxx_mdio_unregister(struct mv88e6xxx_chip *chip)
Andrew Lunnb516d452016-06-04 21:17:06 +02003321
3322{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003323 struct mii_bus *bus = chip->mdio_bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003324
3325 mdiobus_unregister(bus);
3326
Vivien Didelotfad09c72016-06-21 12:28:20 -04003327 if (chip->mdio_np)
3328 of_node_put(chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003329}
3330
Guenter Roeckc22995c2015-07-25 09:42:28 -07003331#ifdef CONFIG_NET_DSA_HWMON
3332
3333static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
3334{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003335 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelot9c938292016-08-15 17:19:02 -04003336 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003337 int ret;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003338
3339 *temp = 0;
3340
Vivien Didelotfad09c72016-06-21 12:28:20 -04003341 mutex_lock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003342
Vivien Didelot9c938292016-08-15 17:19:02 -04003343 ret = mv88e6xxx_phy_write(chip, 0x0, 0x16, 0x6);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003344 if (ret < 0)
3345 goto error;
3346
3347 /* Enable temperature sensor */
Vivien Didelot9c938292016-08-15 17:19:02 -04003348 ret = mv88e6xxx_phy_read(chip, 0x0, 0x1a, &val);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003349 if (ret < 0)
3350 goto error;
3351
Vivien Didelot9c938292016-08-15 17:19:02 -04003352 ret = mv88e6xxx_phy_write(chip, 0x0, 0x1a, val | (1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003353 if (ret < 0)
3354 goto error;
3355
3356 /* Wait for temperature to stabilize */
3357 usleep_range(10000, 12000);
3358
Vivien Didelot9c938292016-08-15 17:19:02 -04003359 ret = mv88e6xxx_phy_read(chip, 0x0, 0x1a, &val);
3360 if (ret < 0)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003361 goto error;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003362
3363 /* Disable temperature sensor */
Vivien Didelot9c938292016-08-15 17:19:02 -04003364 ret = mv88e6xxx_phy_write(chip, 0x0, 0x1a, val & ~(1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003365 if (ret < 0)
3366 goto error;
3367
3368 *temp = ((val & 0x1f) - 5) * 5;
3369
3370error:
Vivien Didelot9c938292016-08-15 17:19:02 -04003371 mv88e6xxx_phy_write(chip, 0x0, 0x16, 0x0);
Vivien Didelotfad09c72016-06-21 12:28:20 -04003372 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003373 return ret;
3374}
3375
3376static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
3377{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003378 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3379 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003380 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003381 int ret;
3382
3383 *temp = 0;
3384
Vivien Didelot9c938292016-08-15 17:19:02 -04003385 mutex_lock(&chip->reg_lock);
3386 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 27, &val);
3387 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003388 if (ret < 0)
3389 return ret;
3390
Vivien Didelot9c938292016-08-15 17:19:02 -04003391 *temp = (val & 0xff) - 25;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003392
3393 return 0;
3394}
3395
Vivien Didelotf81ec902016-05-09 13:22:58 -04003396static int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003397{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003398 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunn158bc062016-04-28 21:24:06 -04003399
Vivien Didelotfad09c72016-06-21 12:28:20 -04003400 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP))
Vivien Didelot6594f612016-05-09 13:22:42 -04003401 return -EOPNOTSUPP;
3402
Vivien Didelotfad09c72016-06-21 12:28:20 -04003403 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003404 return mv88e63xx_get_temp(ds, temp);
3405
3406 return mv88e61xx_get_temp(ds, temp);
3407}
3408
Vivien Didelotf81ec902016-05-09 13:22:58 -04003409static int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003410{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003411 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3412 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003413 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003414 int ret;
3415
Vivien Didelotfad09c72016-06-21 12:28:20 -04003416 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003417 return -EOPNOTSUPP;
3418
3419 *temp = 0;
3420
Vivien Didelot9c938292016-08-15 17:19:02 -04003421 mutex_lock(&chip->reg_lock);
3422 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3423 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003424 if (ret < 0)
3425 return ret;
3426
Vivien Didelot9c938292016-08-15 17:19:02 -04003427 *temp = (((val >> 8) & 0x1f) * 5) - 25;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003428
3429 return 0;
3430}
3431
Vivien Didelotf81ec902016-05-09 13:22:58 -04003432static int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003433{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003434 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3435 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003436 u16 val;
3437 int err;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003438
Vivien Didelotfad09c72016-06-21 12:28:20 -04003439 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003440 return -EOPNOTSUPP;
3441
Vivien Didelot9c938292016-08-15 17:19:02 -04003442 mutex_lock(&chip->reg_lock);
3443 err = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3444 if (err)
3445 goto unlock;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003446 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
Vivien Didelot9c938292016-08-15 17:19:02 -04003447 err = mv88e6xxx_phy_page_write(chip, phy, 6, 26,
3448 (val & 0xe0ff) | (temp << 8));
3449unlock:
3450 mutex_unlock(&chip->reg_lock);
3451
3452 return err;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003453}
3454
Vivien Didelotf81ec902016-05-09 13:22:58 -04003455static int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003456{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003457 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3458 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003459 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003460 int ret;
3461
Vivien Didelotfad09c72016-06-21 12:28:20 -04003462 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003463 return -EOPNOTSUPP;
3464
3465 *alarm = false;
3466
Vivien Didelot9c938292016-08-15 17:19:02 -04003467 mutex_lock(&chip->reg_lock);
3468 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3469 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003470 if (ret < 0)
3471 return ret;
3472
Vivien Didelot9c938292016-08-15 17:19:02 -04003473 *alarm = !!(val & 0x40);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003474
3475 return 0;
3476}
3477#endif /* CONFIG_NET_DSA_HWMON */
3478
Vivien Didelot855b1932016-07-20 18:18:35 -04003479static int mv88e6xxx_get_eeprom_len(struct dsa_switch *ds)
3480{
3481 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3482
3483 return chip->eeprom_len;
3484}
3485
3486static int mv88e6xxx_get_eeprom16(struct mv88e6xxx_chip *chip,
3487 struct ethtool_eeprom *eeprom, u8 *data)
3488{
3489 unsigned int offset = eeprom->offset;
3490 unsigned int len = eeprom->len;
3491 u16 val;
3492 int err;
3493
3494 eeprom->len = 0;
3495
3496 if (offset & 1) {
3497 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
3498 if (err)
3499 return err;
3500
3501 *data++ = (val >> 8) & 0xff;
3502
3503 offset++;
3504 len--;
3505 eeprom->len++;
3506 }
3507
3508 while (len >= 2) {
3509 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
3510 if (err)
3511 return err;
3512
3513 *data++ = val & 0xff;
3514 *data++ = (val >> 8) & 0xff;
3515
3516 offset += 2;
3517 len -= 2;
3518 eeprom->len += 2;
3519 }
3520
3521 if (len) {
3522 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
3523 if (err)
3524 return err;
3525
3526 *data++ = val & 0xff;
3527
3528 offset++;
3529 len--;
3530 eeprom->len++;
3531 }
3532
3533 return 0;
3534}
3535
3536static int mv88e6xxx_get_eeprom(struct dsa_switch *ds,
3537 struct ethtool_eeprom *eeprom, u8 *data)
3538{
3539 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3540 int err;
3541
3542 mutex_lock(&chip->reg_lock);
3543
3544 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16))
3545 err = mv88e6xxx_get_eeprom16(chip, eeprom, data);
3546 else
3547 err = -EOPNOTSUPP;
3548
3549 mutex_unlock(&chip->reg_lock);
3550
3551 if (err)
3552 return err;
3553
3554 eeprom->magic = 0xc3ec4951;
3555
3556 return 0;
3557}
3558
3559static int mv88e6xxx_set_eeprom16(struct mv88e6xxx_chip *chip,
3560 struct ethtool_eeprom *eeprom, u8 *data)
3561{
3562 unsigned int offset = eeprom->offset;
3563 unsigned int len = eeprom->len;
3564 u16 val;
3565 int err;
3566
3567 /* Ensure the RO WriteEn bit is set */
3568 err = mv88e6xxx_read(chip, REG_GLOBAL2, GLOBAL2_EEPROM_CMD, &val);
3569 if (err)
3570 return err;
3571
3572 if (!(val & GLOBAL2_EEPROM_CMD_WRITE_EN))
3573 return -EROFS;
3574
3575 eeprom->len = 0;
3576
3577 if (offset & 1) {
3578 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
3579 if (err)
3580 return err;
3581
3582 val = (*data++ << 8) | (val & 0xff);
3583
3584 err = mv88e6xxx_g2_eeprom_write16(chip, offset >> 1, val);
3585 if (err)
3586 return err;
3587
3588 offset++;
3589 len--;
3590 eeprom->len++;
3591 }
3592
3593 while (len >= 2) {
3594 val = *data++;
3595 val |= *data++ << 8;
3596
3597 err = mv88e6xxx_g2_eeprom_write16(chip, offset >> 1, val);
3598 if (err)
3599 return err;
3600
3601 offset += 2;
3602 len -= 2;
3603 eeprom->len += 2;
3604 }
3605
3606 if (len) {
3607 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
3608 if (err)
3609 return err;
3610
3611 val = (val & 0xff00) | *data++;
3612
3613 err = mv88e6xxx_g2_eeprom_write16(chip, offset >> 1, val);
3614 if (err)
3615 return err;
3616
3617 offset++;
3618 len--;
3619 eeprom->len++;
3620 }
3621
3622 return 0;
3623}
3624
3625static int mv88e6xxx_set_eeprom(struct dsa_switch *ds,
3626 struct ethtool_eeprom *eeprom, u8 *data)
3627{
3628 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3629 int err;
3630
3631 if (eeprom->magic != 0xc3ec4951)
3632 return -EINVAL;
3633
3634 mutex_lock(&chip->reg_lock);
3635
3636 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16))
3637 err = mv88e6xxx_set_eeprom16(chip, eeprom, data);
3638 else
3639 err = -EOPNOTSUPP;
3640
3641 mutex_unlock(&chip->reg_lock);
3642
3643 return err;
3644}
3645
Vivien Didelotf81ec902016-05-09 13:22:58 -04003646static const struct mv88e6xxx_info mv88e6xxx_table[] = {
3647 [MV88E6085] = {
3648 .prod_num = PORT_SWITCH_ID_PROD_NUM_6085,
3649 .family = MV88E6XXX_FAMILY_6097,
3650 .name = "Marvell 88E6085",
3651 .num_databases = 4096,
3652 .num_ports = 10,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003653 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003654 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003655 .flags = MV88E6XXX_FLAGS_FAMILY_6097,
3656 },
3657
3658 [MV88E6095] = {
3659 .prod_num = PORT_SWITCH_ID_PROD_NUM_6095,
3660 .family = MV88E6XXX_FAMILY_6095,
3661 .name = "Marvell 88E6095/88E6095F",
3662 .num_databases = 256,
3663 .num_ports = 11,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003664 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003665 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003666 .flags = MV88E6XXX_FLAGS_FAMILY_6095,
3667 },
3668
3669 [MV88E6123] = {
3670 .prod_num = PORT_SWITCH_ID_PROD_NUM_6123,
3671 .family = MV88E6XXX_FAMILY_6165,
3672 .name = "Marvell 88E6123",
3673 .num_databases = 4096,
3674 .num_ports = 3,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003675 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003676 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003677 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3678 },
3679
3680 [MV88E6131] = {
3681 .prod_num = PORT_SWITCH_ID_PROD_NUM_6131,
3682 .family = MV88E6XXX_FAMILY_6185,
3683 .name = "Marvell 88E6131",
3684 .num_databases = 256,
3685 .num_ports = 8,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003686 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003687 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003688 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3689 },
3690
3691 [MV88E6161] = {
3692 .prod_num = PORT_SWITCH_ID_PROD_NUM_6161,
3693 .family = MV88E6XXX_FAMILY_6165,
3694 .name = "Marvell 88E6161",
3695 .num_databases = 4096,
3696 .num_ports = 6,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003697 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003698 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003699 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3700 },
3701
3702 [MV88E6165] = {
3703 .prod_num = PORT_SWITCH_ID_PROD_NUM_6165,
3704 .family = MV88E6XXX_FAMILY_6165,
3705 .name = "Marvell 88E6165",
3706 .num_databases = 4096,
3707 .num_ports = 6,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003708 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003709 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003710 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3711 },
3712
3713 [MV88E6171] = {
3714 .prod_num = PORT_SWITCH_ID_PROD_NUM_6171,
3715 .family = MV88E6XXX_FAMILY_6351,
3716 .name = "Marvell 88E6171",
3717 .num_databases = 4096,
3718 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003719 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003720 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003721 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3722 },
3723
3724 [MV88E6172] = {
3725 .prod_num = PORT_SWITCH_ID_PROD_NUM_6172,
3726 .family = MV88E6XXX_FAMILY_6352,
3727 .name = "Marvell 88E6172",
3728 .num_databases = 4096,
3729 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003730 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003731 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003732 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3733 },
3734
3735 [MV88E6175] = {
3736 .prod_num = PORT_SWITCH_ID_PROD_NUM_6175,
3737 .family = MV88E6XXX_FAMILY_6351,
3738 .name = "Marvell 88E6175",
3739 .num_databases = 4096,
3740 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003741 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003742 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003743 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3744 },
3745
3746 [MV88E6176] = {
3747 .prod_num = PORT_SWITCH_ID_PROD_NUM_6176,
3748 .family = MV88E6XXX_FAMILY_6352,
3749 .name = "Marvell 88E6176",
3750 .num_databases = 4096,
3751 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003752 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003753 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003754 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3755 },
3756
3757 [MV88E6185] = {
3758 .prod_num = PORT_SWITCH_ID_PROD_NUM_6185,
3759 .family = MV88E6XXX_FAMILY_6185,
3760 .name = "Marvell 88E6185",
3761 .num_databases = 256,
3762 .num_ports = 10,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003763 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003764 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003765 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3766 },
3767
3768 [MV88E6240] = {
3769 .prod_num = PORT_SWITCH_ID_PROD_NUM_6240,
3770 .family = MV88E6XXX_FAMILY_6352,
3771 .name = "Marvell 88E6240",
3772 .num_databases = 4096,
3773 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003774 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003775 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003776 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3777 },
3778
3779 [MV88E6320] = {
3780 .prod_num = PORT_SWITCH_ID_PROD_NUM_6320,
3781 .family = MV88E6XXX_FAMILY_6320,
3782 .name = "Marvell 88E6320",
3783 .num_databases = 4096,
3784 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003785 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003786 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003787 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3788 },
3789
3790 [MV88E6321] = {
3791 .prod_num = PORT_SWITCH_ID_PROD_NUM_6321,
3792 .family = MV88E6XXX_FAMILY_6320,
3793 .name = "Marvell 88E6321",
3794 .num_databases = 4096,
3795 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003796 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003797 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003798 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3799 },
3800
3801 [MV88E6350] = {
3802 .prod_num = PORT_SWITCH_ID_PROD_NUM_6350,
3803 .family = MV88E6XXX_FAMILY_6351,
3804 .name = "Marvell 88E6350",
3805 .num_databases = 4096,
3806 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003807 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003808 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003809 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3810 },
3811
3812 [MV88E6351] = {
3813 .prod_num = PORT_SWITCH_ID_PROD_NUM_6351,
3814 .family = MV88E6XXX_FAMILY_6351,
3815 .name = "Marvell 88E6351",
3816 .num_databases = 4096,
3817 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003818 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003819 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003820 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3821 },
3822
3823 [MV88E6352] = {
3824 .prod_num = PORT_SWITCH_ID_PROD_NUM_6352,
3825 .family = MV88E6XXX_FAMILY_6352,
3826 .name = "Marvell 88E6352",
3827 .num_databases = 4096,
3828 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003829 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003830 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003831 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3832 },
3833};
3834
Vivien Didelot5f7c0362016-06-20 13:14:04 -04003835static const struct mv88e6xxx_info *mv88e6xxx_lookup_info(unsigned int prod_num)
Vivien Didelotb9b37712015-10-30 19:39:48 -04003836{
Vivien Didelota439c062016-04-17 13:23:58 -04003837 int i;
Vivien Didelotb9b37712015-10-30 19:39:48 -04003838
Vivien Didelot5f7c0362016-06-20 13:14:04 -04003839 for (i = 0; i < ARRAY_SIZE(mv88e6xxx_table); ++i)
3840 if (mv88e6xxx_table[i].prod_num == prod_num)
3841 return &mv88e6xxx_table[i];
Vivien Didelotb9b37712015-10-30 19:39:48 -04003842
Vivien Didelotb9b37712015-10-30 19:39:48 -04003843 return NULL;
3844}
3845
Vivien Didelotfad09c72016-06-21 12:28:20 -04003846static int mv88e6xxx_detect(struct mv88e6xxx_chip *chip)
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003847{
3848 const struct mv88e6xxx_info *info;
Vivien Didelot8f6345b2016-07-20 18:18:36 -04003849 unsigned int prod_num, rev;
3850 u16 id;
3851 int err;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003852
Vivien Didelot8f6345b2016-07-20 18:18:36 -04003853 mutex_lock(&chip->reg_lock);
3854 err = mv88e6xxx_port_read(chip, 0, PORT_SWITCH_ID, &id);
3855 mutex_unlock(&chip->reg_lock);
3856 if (err)
3857 return err;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003858
3859 prod_num = (id & 0xfff0) >> 4;
3860 rev = id & 0x000f;
3861
3862 info = mv88e6xxx_lookup_info(prod_num);
3863 if (!info)
3864 return -ENODEV;
3865
Vivien Didelotcaac8542016-06-20 13:14:09 -04003866 /* Update the compatible info with the probed one */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003867 chip->info = info;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003868
Vivien Didelotfad09c72016-06-21 12:28:20 -04003869 dev_info(chip->dev, "switch 0x%x detected: %s, revision %u\n",
3870 chip->info->prod_num, chip->info->name, rev);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003871
3872 return 0;
3873}
3874
Vivien Didelotfad09c72016-06-21 12:28:20 -04003875static struct mv88e6xxx_chip *mv88e6xxx_alloc_chip(struct device *dev)
Vivien Didelot469d7292016-06-20 13:14:06 -04003876{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003877 struct mv88e6xxx_chip *chip;
Vivien Didelot469d7292016-06-20 13:14:06 -04003878
Vivien Didelotfad09c72016-06-21 12:28:20 -04003879 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
3880 if (!chip)
Vivien Didelot469d7292016-06-20 13:14:06 -04003881 return NULL;
3882
Vivien Didelotfad09c72016-06-21 12:28:20 -04003883 chip->dev = dev;
Vivien Didelot469d7292016-06-20 13:14:06 -04003884
Vivien Didelotfad09c72016-06-21 12:28:20 -04003885 mutex_init(&chip->reg_lock);
Vivien Didelot469d7292016-06-20 13:14:06 -04003886
Vivien Didelotfad09c72016-06-21 12:28:20 -04003887 return chip;
Vivien Didelot469d7292016-06-20 13:14:06 -04003888}
3889
Vivien Didelote57e5e72016-08-15 17:19:00 -04003890static const struct mv88e6xxx_ops mv88e6xxx_phy_ops = {
3891 .read = mv88e6xxx_read,
3892 .write = mv88e6xxx_write,
3893};
3894
3895static void mv88e6xxx_phy_init(struct mv88e6xxx_chip *chip)
3896{
3897 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_SMI_PHY)) {
3898 chip->phy_ops = &mv88e6xxx_g2_smi_phy_ops;
3899 } else if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU)) {
3900 chip->phy_ops = &mv88e6xxx_phy_ppu_ops;
3901 mv88e6xxx_ppu_state_init(chip);
3902 } else {
3903 chip->phy_ops = &mv88e6xxx_phy_ops;
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{
3929 return DSA_TAG_PROTO_EDSA;
3930}
3931
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02003932static const char *mv88e6xxx_drv_probe(struct device *dsa_dev,
3933 struct device *host_dev, int sw_addr,
3934 void **priv)
Andrew Lunna77d43f2016-04-13 02:40:42 +02003935{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003936 struct mv88e6xxx_chip *chip;
Vivien Didelota439c062016-04-17 13:23:58 -04003937 struct mii_bus *bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003938 int err;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003939
Vivien Didelota439c062016-04-17 13:23:58 -04003940 bus = dsa_host_dev_to_mii_bus(host_dev);
Andrew Lunnc1569132016-04-13 02:40:45 +02003941 if (!bus)
3942 return NULL;
3943
Vivien Didelotfad09c72016-06-21 12:28:20 -04003944 chip = mv88e6xxx_alloc_chip(dsa_dev);
3945 if (!chip)
Vivien Didelot469d7292016-06-20 13:14:06 -04003946 return NULL;
3947
Vivien Didelotcaac8542016-06-20 13:14:09 -04003948 /* Legacy SMI probing will only support chips similar to 88E6085 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003949 chip->info = &mv88e6xxx_table[MV88E6085];
Vivien Didelotcaac8542016-06-20 13:14:09 -04003950
Vivien Didelotfad09c72016-06-21 12:28:20 -04003951 err = mv88e6xxx_smi_init(chip, bus, sw_addr);
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003952 if (err)
3953 goto free;
3954
Vivien Didelotfad09c72016-06-21 12:28:20 -04003955 err = mv88e6xxx_detect(chip);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003956 if (err)
Vivien Didelot469d7292016-06-20 13:14:06 -04003957 goto free;
Vivien Didelota439c062016-04-17 13:23:58 -04003958
Vivien Didelote57e5e72016-08-15 17:19:00 -04003959 mv88e6xxx_phy_init(chip);
3960
Vivien Didelotfad09c72016-06-21 12:28:20 -04003961 err = mv88e6xxx_mdio_register(chip, NULL);
Andrew Lunnb516d452016-06-04 21:17:06 +02003962 if (err)
Vivien Didelot469d7292016-06-20 13:14:06 -04003963 goto free;
Andrew Lunnb516d452016-06-04 21:17:06 +02003964
Vivien Didelotfad09c72016-06-21 12:28:20 -04003965 *priv = chip;
Vivien Didelota439c062016-04-17 13:23:58 -04003966
Vivien Didelotfad09c72016-06-21 12:28:20 -04003967 return chip->info->name;
Vivien Didelot469d7292016-06-20 13:14:06 -04003968free:
Vivien Didelotfad09c72016-06-21 12:28:20 -04003969 devm_kfree(dsa_dev, chip);
Vivien Didelot469d7292016-06-20 13:14:06 -04003970
3971 return NULL;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003972}
3973
Vivien Didelot57d32312016-06-20 13:13:58 -04003974static struct dsa_switch_driver mv88e6xxx_switch_driver = {
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02003975 .probe = mv88e6xxx_drv_probe,
Andrew Lunn7b314362016-08-22 16:01:01 +02003976 .get_tag_protocol = mv88e6xxx_get_tag_protocol,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003977 .setup = mv88e6xxx_setup,
3978 .set_addr = mv88e6xxx_set_addr,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003979 .adjust_link = mv88e6xxx_adjust_link,
3980 .get_strings = mv88e6xxx_get_strings,
3981 .get_ethtool_stats = mv88e6xxx_get_ethtool_stats,
3982 .get_sset_count = mv88e6xxx_get_sset_count,
3983 .set_eee = mv88e6xxx_set_eee,
3984 .get_eee = mv88e6xxx_get_eee,
3985#ifdef CONFIG_NET_DSA_HWMON
3986 .get_temp = mv88e6xxx_get_temp,
3987 .get_temp_limit = mv88e6xxx_get_temp_limit,
3988 .set_temp_limit = mv88e6xxx_set_temp_limit,
3989 .get_temp_alarm = mv88e6xxx_get_temp_alarm,
3990#endif
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003991 .get_eeprom_len = mv88e6xxx_get_eeprom_len,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003992 .get_eeprom = mv88e6xxx_get_eeprom,
3993 .set_eeprom = mv88e6xxx_set_eeprom,
3994 .get_regs_len = mv88e6xxx_get_regs_len,
3995 .get_regs = mv88e6xxx_get_regs,
Vivien Didelot2cfcd962016-07-18 20:45:40 -04003996 .set_ageing_time = mv88e6xxx_set_ageing_time,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003997 .port_bridge_join = mv88e6xxx_port_bridge_join,
3998 .port_bridge_leave = mv88e6xxx_port_bridge_leave,
3999 .port_stp_state_set = mv88e6xxx_port_stp_state_set,
4000 .port_vlan_filtering = mv88e6xxx_port_vlan_filtering,
4001 .port_vlan_prepare = mv88e6xxx_port_vlan_prepare,
4002 .port_vlan_add = mv88e6xxx_port_vlan_add,
4003 .port_vlan_del = mv88e6xxx_port_vlan_del,
4004 .port_vlan_dump = mv88e6xxx_port_vlan_dump,
4005 .port_fdb_prepare = mv88e6xxx_port_fdb_prepare,
4006 .port_fdb_add = mv88e6xxx_port_fdb_add,
4007 .port_fdb_del = mv88e6xxx_port_fdb_del,
4008 .port_fdb_dump = mv88e6xxx_port_fdb_dump,
4009};
4010
Vivien Didelotfad09c72016-06-21 12:28:20 -04004011static int mv88e6xxx_register_switch(struct mv88e6xxx_chip *chip,
Vivien Didelotb7e66a52016-06-20 13:14:02 -04004012 struct device_node *np)
4013{
Vivien Didelotfad09c72016-06-21 12:28:20 -04004014 struct device *dev = chip->dev;
Vivien Didelotb7e66a52016-06-20 13:14:02 -04004015 struct dsa_switch *ds;
4016
4017 ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
4018 if (!ds)
4019 return -ENOMEM;
4020
4021 ds->dev = dev;
Vivien Didelotfad09c72016-06-21 12:28:20 -04004022 ds->priv = chip;
Vivien Didelotb7e66a52016-06-20 13:14:02 -04004023 ds->drv = &mv88e6xxx_switch_driver;
4024
4025 dev_set_drvdata(dev, ds);
4026
4027 return dsa_register_switch(ds, np);
4028}
4029
Vivien Didelotfad09c72016-06-21 12:28:20 -04004030static void mv88e6xxx_unregister_switch(struct mv88e6xxx_chip *chip)
Vivien Didelotb7e66a52016-06-20 13:14:02 -04004031{
Vivien Didelotfad09c72016-06-21 12:28:20 -04004032 dsa_unregister_switch(chip->ds);
Vivien Didelotb7e66a52016-06-20 13:14:02 -04004033}
4034
Vivien Didelot57d32312016-06-20 13:13:58 -04004035static int mv88e6xxx_probe(struct mdio_device *mdiodev)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004036{
4037 struct device *dev = &mdiodev->dev;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02004038 struct device_node *np = dev->of_node;
Vivien Didelotcaac8542016-06-20 13:14:09 -04004039 const struct mv88e6xxx_info *compat_info;
Vivien Didelotfad09c72016-06-21 12:28:20 -04004040 struct mv88e6xxx_chip *chip;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02004041 u32 eeprom_len;
Andrew Lunn52638f72016-05-10 23:27:22 +02004042 int err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004043
Vivien Didelotcaac8542016-06-20 13:14:09 -04004044 compat_info = of_device_get_match_data(dev);
4045 if (!compat_info)
4046 return -EINVAL;
4047
Vivien Didelotfad09c72016-06-21 12:28:20 -04004048 chip = mv88e6xxx_alloc_chip(dev);
4049 if (!chip)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004050 return -ENOMEM;
4051
Vivien Didelotfad09c72016-06-21 12:28:20 -04004052 chip->info = compat_info;
Vivien Didelotcaac8542016-06-20 13:14:09 -04004053
Vivien Didelotfad09c72016-06-21 12:28:20 -04004054 err = mv88e6xxx_smi_init(chip, mdiodev->bus, mdiodev->addr);
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04004055 if (err)
4056 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004057
Vivien Didelotfad09c72016-06-21 12:28:20 -04004058 err = mv88e6xxx_detect(chip);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04004059 if (err)
4060 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004061
Vivien Didelote57e5e72016-08-15 17:19:00 -04004062 mv88e6xxx_phy_init(chip);
4063
Vivien Didelotfad09c72016-06-21 12:28:20 -04004064 chip->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_ASIS);
4065 if (IS_ERR(chip->reset))
4066 return PTR_ERR(chip->reset);
Andrew Lunn52638f72016-05-10 23:27:22 +02004067
Vivien Didelot855b1932016-07-20 18:18:35 -04004068 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16) &&
Andrew Lunnf8cd8752016-05-10 23:27:25 +02004069 !of_property_read_u32(np, "eeprom-length", &eeprom_len))
Vivien Didelotfad09c72016-06-21 12:28:20 -04004070 chip->eeprom_len = eeprom_len;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02004071
Vivien Didelotfad09c72016-06-21 12:28:20 -04004072 err = mv88e6xxx_mdio_register(chip, np);
Andrew Lunnb516d452016-06-04 21:17:06 +02004073 if (err)
4074 return err;
4075
Vivien Didelotfad09c72016-06-21 12:28:20 -04004076 err = mv88e6xxx_register_switch(chip, np);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02004077 if (err) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04004078 mv88e6xxx_mdio_unregister(chip);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02004079 return err;
4080 }
4081
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004082 return 0;
4083}
4084
4085static void mv88e6xxx_remove(struct mdio_device *mdiodev)
4086{
4087 struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev);
Vivien Didelotfad09c72016-06-21 12:28:20 -04004088 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004089
Vivien Didelotfad09c72016-06-21 12:28:20 -04004090 mv88e6xxx_unregister_switch(chip);
4091 mv88e6xxx_mdio_unregister(chip);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004092}
4093
4094static const struct of_device_id mv88e6xxx_of_match[] = {
Vivien Didelotcaac8542016-06-20 13:14:09 -04004095 {
4096 .compatible = "marvell,mv88e6085",
4097 .data = &mv88e6xxx_table[MV88E6085],
4098 },
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004099 { /* sentinel */ },
4100};
4101
4102MODULE_DEVICE_TABLE(of, mv88e6xxx_of_match);
4103
4104static struct mdio_driver mv88e6xxx_driver = {
4105 .probe = mv88e6xxx_probe,
4106 .remove = mv88e6xxx_remove,
4107 .mdiodrv.driver = {
4108 .name = "mv88e6085",
4109 .of_match_table = mv88e6xxx_of_match,
4110 },
4111};
4112
Ben Hutchings98e67302011-11-25 14:36:19 +00004113static int __init mv88e6xxx_init(void)
4114{
Vivien Didelotf81ec902016-05-09 13:22:58 -04004115 register_switch_driver(&mv88e6xxx_switch_driver);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004116 return mdio_driver_register(&mv88e6xxx_driver);
Ben Hutchings98e67302011-11-25 14:36:19 +00004117}
4118module_init(mv88e6xxx_init);
4119
4120static void __exit mv88e6xxx_cleanup(void)
4121{
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004122 mdio_driver_unregister(&mv88e6xxx_driver);
Vivien Didelotf81ec902016-05-09 13:22:58 -04004123 unregister_switch_driver(&mv88e6xxx_switch_driver);
Ben Hutchings98e67302011-11-25 14:36:19 +00004124}
4125module_exit(mv88e6xxx_cleanup);
Ben Hutchings3d825ed2011-11-25 14:37:16 +00004126
4127MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
4128MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
4129MODULE_LICENSE("GPL");