blob: 3feb842ec1d6765dd1a4eaf043c539a8e24c3125 [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 Didelotfad09c72016-06-21 12:28:20 -0400219static int _mv88e6xxx_reg_read(struct mv88e6xxx_chip *chip, int addr, int reg)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000220{
Vivien Didelot914b32f2016-06-20 13:14:11 -0400221 u16 val;
222 int err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000223
Vivien Didelotfad09c72016-06-21 12:28:20 -0400224 err = mv88e6xxx_read(chip, addr, reg, &val);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400225 if (err)
226 return err;
Vivien Didelot3996a4f2015-10-30 18:56:45 -0400227
Vivien Didelot914b32f2016-06-20 13:14:11 -0400228 return val;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000229}
230
Vivien Didelotfad09c72016-06-21 12:28:20 -0400231static int mv88e6xxx_reg_read(struct mv88e6xxx_chip *chip, int addr, int reg)
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700232{
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700233 int ret;
234
Vivien Didelotfad09c72016-06-21 12:28:20 -0400235 mutex_lock(&chip->reg_lock);
236 ret = _mv88e6xxx_reg_read(chip, addr, reg);
237 mutex_unlock(&chip->reg_lock);
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700238
239 return ret;
240}
241
Vivien Didelotfad09c72016-06-21 12:28:20 -0400242static int _mv88e6xxx_reg_write(struct mv88e6xxx_chip *chip, int addr,
Andrew Lunn158bc062016-04-28 21:24:06 -0400243 int reg, u16 val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000244{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400245 return mv88e6xxx_write(chip, addr, reg, val);
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700246}
247
Vivien Didelotfad09c72016-06-21 12:28:20 -0400248static int mv88e6xxx_reg_write(struct mv88e6xxx_chip *chip, int addr,
Vivien Didelot57d32312016-06-20 13:13:58 -0400249 int reg, u16 val)
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700250{
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700251 int ret;
252
Vivien Didelotfad09c72016-06-21 12:28:20 -0400253 mutex_lock(&chip->reg_lock);
254 ret = _mv88e6xxx_reg_write(chip, addr, reg, val);
255 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000256
257 return ret;
258}
259
Vivien Didelot1d13a062016-05-09 13:22:43 -0400260static int mv88e6xxx_set_addr_direct(struct dsa_switch *ds, u8 *addr)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000261{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400262 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200263 int err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000264
Vivien Didelotfad09c72016-06-21 12:28:20 -0400265 err = mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_MAC_01,
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200266 (addr[0] << 8) | addr[1]);
267 if (err)
268 return err;
269
Vivien Didelotfad09c72016-06-21 12:28:20 -0400270 err = mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_MAC_23,
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200271 (addr[2] << 8) | addr[3]);
272 if (err)
273 return err;
274
Vivien Didelotfad09c72016-06-21 12:28:20 -0400275 return mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_MAC_45,
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200276 (addr[4] << 8) | addr[5]);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000277}
278
Vivien Didelot1d13a062016-05-09 13:22:43 -0400279static int mv88e6xxx_set_addr_indirect(struct dsa_switch *ds, u8 *addr)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000280{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400281 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000282 int ret;
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200283 int i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000284
285 for (i = 0; i < 6; i++) {
286 int j;
287
Barry Grussling3675c8d2013-01-08 16:05:53 +0000288 /* Write the MAC address byte. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400289 ret = mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_SWITCH_MAC,
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200290 GLOBAL2_SWITCH_MAC_BUSY |
291 (i << 8) | addr[i]);
292 if (ret)
293 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000294
Barry Grussling3675c8d2013-01-08 16:05:53 +0000295 /* Wait for the write to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000296 for (j = 0; j < 16; j++) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400297 ret = mv88e6xxx_reg_read(chip, REG_GLOBAL2,
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200298 GLOBAL2_SWITCH_MAC);
299 if (ret < 0)
300 return ret;
301
Andrew Lunncca8b132015-04-02 04:06:39 +0200302 if ((ret & GLOBAL2_SWITCH_MAC_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000303 break;
304 }
305 if (j == 16)
306 return -ETIMEDOUT;
307 }
308
309 return 0;
310}
311
Vivien Didelot57d32312016-06-20 13:13:58 -0400312static int mv88e6xxx_set_addr(struct dsa_switch *ds, u8 *addr)
Vivien Didelot1d13a062016-05-09 13:22:43 -0400313{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400314 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelot1d13a062016-05-09 13:22:43 -0400315
Vivien Didelotfad09c72016-06-21 12:28:20 -0400316 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_SWITCH_MAC))
Vivien Didelot1d13a062016-05-09 13:22:43 -0400317 return mv88e6xxx_set_addr_indirect(ds, addr);
318 else
319 return mv88e6xxx_set_addr_direct(ds, addr);
320}
321
Vivien Didelotfad09c72016-06-21 12:28:20 -0400322static int mv88e6xxx_mdio_read_direct(struct mv88e6xxx_chip *chip,
Andrew Lunn03a4a542016-06-04 21:17:05 +0200323 int addr, int regnum)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000324{
325 if (addr >= 0)
Vivien Didelotfad09c72016-06-21 12:28:20 -0400326 return _mv88e6xxx_reg_read(chip, addr, regnum);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000327 return 0xffff;
328}
329
Vivien Didelotfad09c72016-06-21 12:28:20 -0400330static int mv88e6xxx_mdio_write_direct(struct mv88e6xxx_chip *chip,
Andrew Lunn03a4a542016-06-04 21:17:05 +0200331 int addr, int regnum, u16 val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000332{
333 if (addr >= 0)
Vivien Didelotfad09c72016-06-21 12:28:20 -0400334 return _mv88e6xxx_reg_write(chip, addr, regnum, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000335 return 0;
336}
337
Vivien Didelotfad09c72016-06-21 12:28:20 -0400338static int mv88e6xxx_ppu_disable(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000339{
340 int ret;
Barry Grussling19b2f972013-01-08 16:05:54 +0000341 unsigned long timeout;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000342
Vivien Didelotfad09c72016-06-21 12:28:20 -0400343 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_CONTROL);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200344 if (ret < 0)
345 return ret;
346
Vivien Didelotfad09c72016-06-21 12:28:20 -0400347 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL,
Vivien Didelot8c9983a2016-05-09 13:22:39 -0400348 ret & ~GLOBAL_CONTROL_PPU_ENABLE);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200349 if (ret)
350 return ret;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000351
Barry Grussling19b2f972013-01-08 16:05:54 +0000352 timeout = jiffies + 1 * HZ;
353 while (time_before(jiffies, timeout)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400354 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATUS);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200355 if (ret < 0)
356 return ret;
357
Barry Grussling19b2f972013-01-08 16:05:54 +0000358 usleep_range(1000, 2000);
Andrew Lunncca8b132015-04-02 04:06:39 +0200359 if ((ret & GLOBAL_STATUS_PPU_MASK) !=
360 GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000361 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000362 }
363
364 return -ETIMEDOUT;
365}
366
Vivien Didelotfad09c72016-06-21 12:28:20 -0400367static int mv88e6xxx_ppu_enable(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000368{
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200369 int ret, err;
Barry Grussling19b2f972013-01-08 16:05:54 +0000370 unsigned long timeout;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000371
Vivien Didelotfad09c72016-06-21 12:28:20 -0400372 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_CONTROL);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200373 if (ret < 0)
374 return ret;
375
Vivien Didelotfad09c72016-06-21 12:28:20 -0400376 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL,
Vivien Didelot762eb672016-06-04 21:16:54 +0200377 ret | GLOBAL_CONTROL_PPU_ENABLE);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200378 if (err)
379 return err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000380
Barry Grussling19b2f972013-01-08 16:05:54 +0000381 timeout = jiffies + 1 * HZ;
382 while (time_before(jiffies, timeout)) {
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
396static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
397{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400398 struct mv88e6xxx_chip *chip;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000399
Vivien Didelotfad09c72016-06-21 12:28:20 -0400400 chip = container_of(ugly, struct mv88e6xxx_chip, ppu_work);
Vivien Didelot762eb672016-06-04 21:16:54 +0200401
Vivien Didelotfad09c72016-06-21 12:28:20 -0400402 mutex_lock(&chip->reg_lock);
Vivien Didelot762eb672016-06-04 21:16:54 +0200403
Vivien Didelotfad09c72016-06-21 12:28:20 -0400404 if (mutex_trylock(&chip->ppu_mutex)) {
405 if (mv88e6xxx_ppu_enable(chip) == 0)
406 chip->ppu_disabled = 0;
407 mutex_unlock(&chip->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000408 }
Vivien Didelot762eb672016-06-04 21:16:54 +0200409
Vivien Didelotfad09c72016-06-21 12:28:20 -0400410 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000411}
412
413static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
414{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400415 struct mv88e6xxx_chip *chip = (void *)_ps;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000416
Vivien Didelotfad09c72016-06-21 12:28:20 -0400417 schedule_work(&chip->ppu_work);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000418}
419
Vivien Didelotfad09c72016-06-21 12:28:20 -0400420static int mv88e6xxx_ppu_access_get(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000421{
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000422 int ret;
423
Vivien Didelotfad09c72016-06-21 12:28:20 -0400424 mutex_lock(&chip->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000425
Barry Grussling3675c8d2013-01-08 16:05:53 +0000426 /* If the PHY polling unit is enabled, disable it so that
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000427 * we can access the PHY registers. If it was already
428 * disabled, cancel the timer that is going to re-enable
429 * it.
430 */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400431 if (!chip->ppu_disabled) {
432 ret = mv88e6xxx_ppu_disable(chip);
Barry Grussling85686582013-01-08 16:05:56 +0000433 if (ret < 0) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400434 mutex_unlock(&chip->ppu_mutex);
Barry Grussling85686582013-01-08 16:05:56 +0000435 return ret;
436 }
Vivien Didelotfad09c72016-06-21 12:28:20 -0400437 chip->ppu_disabled = 1;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000438 } else {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400439 del_timer(&chip->ppu_timer);
Barry Grussling85686582013-01-08 16:05:56 +0000440 ret = 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000441 }
442
443 return ret;
444}
445
Vivien Didelotfad09c72016-06-21 12:28:20 -0400446static void mv88e6xxx_ppu_access_put(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000447{
Barry Grussling3675c8d2013-01-08 16:05:53 +0000448 /* Schedule a timer to re-enable the PHY polling unit. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400449 mod_timer(&chip->ppu_timer, jiffies + msecs_to_jiffies(10));
450 mutex_unlock(&chip->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000451}
452
Vivien Didelotfad09c72016-06-21 12:28:20 -0400453static void mv88e6xxx_ppu_state_init(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000454{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400455 mutex_init(&chip->ppu_mutex);
456 INIT_WORK(&chip->ppu_work, mv88e6xxx_ppu_reenable_work);
457 init_timer(&chip->ppu_timer);
458 chip->ppu_timer.data = (unsigned long)chip;
459 chip->ppu_timer.function = mv88e6xxx_ppu_reenable_timer;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000460}
461
Vivien Didelotfad09c72016-06-21 12:28:20 -0400462static int mv88e6xxx_mdio_read_ppu(struct mv88e6xxx_chip *chip, int addr,
Andrew Lunn03a4a542016-06-04 21:17:05 +0200463 int regnum)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000464{
465 int ret;
466
Vivien Didelotfad09c72016-06-21 12:28:20 -0400467 ret = mv88e6xxx_ppu_access_get(chip);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000468 if (ret >= 0) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400469 ret = _mv88e6xxx_reg_read(chip, addr, regnum);
470 mv88e6xxx_ppu_access_put(chip);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000471 }
472
473 return ret;
474}
475
Vivien Didelotfad09c72016-06-21 12:28:20 -0400476static int mv88e6xxx_mdio_write_ppu(struct mv88e6xxx_chip *chip, int addr,
Andrew Lunn03a4a542016-06-04 21:17:05 +0200477 int regnum, u16 val)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000478{
479 int ret;
480
Vivien Didelotfad09c72016-06-21 12:28:20 -0400481 ret = mv88e6xxx_ppu_access_get(chip);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000482 if (ret >= 0) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400483 ret = _mv88e6xxx_reg_write(chip, addr, regnum, val);
484 mv88e6xxx_ppu_access_put(chip);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000485 }
486
487 return ret;
488}
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000489
Vivien Didelotfad09c72016-06-21 12:28:20 -0400490static bool mv88e6xxx_6065_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200491{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400492 return chip->info->family == MV88E6XXX_FAMILY_6065;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200493}
494
Vivien Didelotfad09c72016-06-21 12:28:20 -0400495static bool mv88e6xxx_6095_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200496{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400497 return chip->info->family == MV88E6XXX_FAMILY_6095;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200498}
499
Vivien Didelotfad09c72016-06-21 12:28:20 -0400500static bool mv88e6xxx_6097_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200501{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400502 return chip->info->family == MV88E6XXX_FAMILY_6097;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200503}
504
Vivien Didelotfad09c72016-06-21 12:28:20 -0400505static bool mv88e6xxx_6165_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200506{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400507 return chip->info->family == MV88E6XXX_FAMILY_6165;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200508}
509
Vivien Didelotfad09c72016-06-21 12:28:20 -0400510static bool mv88e6xxx_6185_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200511{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400512 return chip->info->family == MV88E6XXX_FAMILY_6185;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200513}
514
Vivien Didelotfad09c72016-06-21 12:28:20 -0400515static bool mv88e6xxx_6320_family(struct mv88e6xxx_chip *chip)
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700516{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400517 return chip->info->family == MV88E6XXX_FAMILY_6320;
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700518}
519
Vivien Didelotfad09c72016-06-21 12:28:20 -0400520static bool mv88e6xxx_6351_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200521{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400522 return chip->info->family == MV88E6XXX_FAMILY_6351;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200523}
524
Vivien Didelotfad09c72016-06-21 12:28:20 -0400525static bool mv88e6xxx_6352_family(struct mv88e6xxx_chip *chip)
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200526{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400527 return chip->info->family == MV88E6XXX_FAMILY_6352;
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200528}
529
Vivien Didelotfad09c72016-06-21 12:28:20 -0400530static unsigned int mv88e6xxx_num_databases(struct mv88e6xxx_chip *chip)
Vivien Didelotf74df0b2016-03-31 16:53:43 -0400531{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400532 return chip->info->num_databases;
Vivien Didelotf74df0b2016-03-31 16:53:43 -0400533}
534
Vivien Didelotfad09c72016-06-21 12:28:20 -0400535static bool mv88e6xxx_has_fid_reg(struct mv88e6xxx_chip *chip)
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400536{
537 /* Does the device have dedicated FID registers for ATU and VTU ops? */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400538 if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
539 mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip))
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400540 return true;
541
542 return false;
543}
544
Andrew Lunndea87022015-08-31 15:56:47 +0200545/* We expect the switch to perform auto negotiation if there is a real
546 * phy. However, in the case of a fixed link phy, we force the port
547 * settings from the fixed link settings.
548 */
Vivien Didelotf81ec902016-05-09 13:22:58 -0400549static void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port,
550 struct phy_device *phydev)
Andrew Lunndea87022015-08-31 15:56:47 +0200551{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400552 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunn49052872015-09-29 01:53:48 +0200553 u32 reg;
554 int ret;
Andrew Lunndea87022015-08-31 15:56:47 +0200555
556 if (!phy_is_pseudo_fixed_link(phydev))
557 return;
558
Vivien Didelotfad09c72016-06-21 12:28:20 -0400559 mutex_lock(&chip->reg_lock);
Andrew Lunndea87022015-08-31 15:56:47 +0200560
Vivien Didelotfad09c72016-06-21 12:28:20 -0400561 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_PCS_CTRL);
Andrew Lunndea87022015-08-31 15:56:47 +0200562 if (ret < 0)
563 goto out;
564
565 reg = ret & ~(PORT_PCS_CTRL_LINK_UP |
566 PORT_PCS_CTRL_FORCE_LINK |
567 PORT_PCS_CTRL_DUPLEX_FULL |
568 PORT_PCS_CTRL_FORCE_DUPLEX |
569 PORT_PCS_CTRL_UNFORCED);
570
571 reg |= PORT_PCS_CTRL_FORCE_LINK;
572 if (phydev->link)
Vivien Didelot57d32312016-06-20 13:13:58 -0400573 reg |= PORT_PCS_CTRL_LINK_UP;
Andrew Lunndea87022015-08-31 15:56:47 +0200574
Vivien Didelotfad09c72016-06-21 12:28:20 -0400575 if (mv88e6xxx_6065_family(chip) && phydev->speed > SPEED_100)
Andrew Lunndea87022015-08-31 15:56:47 +0200576 goto out;
577
578 switch (phydev->speed) {
579 case SPEED_1000:
580 reg |= PORT_PCS_CTRL_1000;
581 break;
582 case SPEED_100:
583 reg |= PORT_PCS_CTRL_100;
584 break;
585 case SPEED_10:
586 reg |= PORT_PCS_CTRL_10;
587 break;
588 default:
589 pr_info("Unknown speed");
590 goto out;
591 }
592
593 reg |= PORT_PCS_CTRL_FORCE_DUPLEX;
594 if (phydev->duplex == DUPLEX_FULL)
595 reg |= PORT_PCS_CTRL_DUPLEX_FULL;
596
Vivien Didelotfad09c72016-06-21 12:28:20 -0400597 if ((mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip)) &&
598 (port >= chip->info->num_ports - 2)) {
Andrew Lunne7e72ac2015-08-31 15:56:51 +0200599 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
600 reg |= PORT_PCS_CTRL_RGMII_DELAY_RXCLK;
601 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
602 reg |= PORT_PCS_CTRL_RGMII_DELAY_TXCLK;
603 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
604 reg |= (PORT_PCS_CTRL_RGMII_DELAY_RXCLK |
605 PORT_PCS_CTRL_RGMII_DELAY_TXCLK);
606 }
Vivien Didelotfad09c72016-06-21 12:28:20 -0400607 _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_PCS_CTRL, reg);
Andrew Lunndea87022015-08-31 15:56:47 +0200608
609out:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400610 mutex_unlock(&chip->reg_lock);
Andrew Lunndea87022015-08-31 15:56:47 +0200611}
612
Vivien Didelotfad09c72016-06-21 12:28:20 -0400613static int _mv88e6xxx_stats_wait(struct mv88e6xxx_chip *chip)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000614{
615 int ret;
616 int i;
617
618 for (i = 0; i < 10; i++) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400619 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_OP);
Andrew Lunncca8b132015-04-02 04:06:39 +0200620 if ((ret & GLOBAL_STATS_OP_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000621 return 0;
622 }
623
624 return -ETIMEDOUT;
625}
626
Vivien Didelotfad09c72016-06-21 12:28:20 -0400627static int _mv88e6xxx_stats_snapshot(struct mv88e6xxx_chip *chip, int port)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000628{
629 int ret;
630
Vivien Didelotfad09c72016-06-21 12:28:20 -0400631 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200632 port = (port + 1) << 5;
633
Barry Grussling3675c8d2013-01-08 16:05:53 +0000634 /* Snapshot the hardware statistics counters for this port. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400635 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
Andrew Lunn31888232015-05-06 01:09:54 +0200636 GLOBAL_STATS_OP_CAPTURE_PORT |
637 GLOBAL_STATS_OP_HIST_RX_TX | port);
638 if (ret < 0)
639 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000640
Barry Grussling3675c8d2013-01-08 16:05:53 +0000641 /* Wait for the snapshotting to complete. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400642 ret = _mv88e6xxx_stats_wait(chip);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000643 if (ret < 0)
644 return ret;
645
646 return 0;
647}
648
Vivien Didelotfad09c72016-06-21 12:28:20 -0400649static void _mv88e6xxx_stats_read(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -0400650 int stat, u32 *val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000651{
652 u32 _val;
653 int ret;
654
655 *val = 0;
656
Vivien Didelotfad09c72016-06-21 12:28:20 -0400657 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
Andrew Lunn31888232015-05-06 01:09:54 +0200658 GLOBAL_STATS_OP_READ_CAPTURED |
659 GLOBAL_STATS_OP_HIST_RX_TX | stat);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000660 if (ret < 0)
661 return;
662
Vivien Didelotfad09c72016-06-21 12:28:20 -0400663 ret = _mv88e6xxx_stats_wait(chip);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000664 if (ret < 0)
665 return;
666
Vivien Didelotfad09c72016-06-21 12:28:20 -0400667 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_COUNTER_32);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000668 if (ret < 0)
669 return;
670
671 _val = ret << 16;
672
Vivien Didelotfad09c72016-06-21 12:28:20 -0400673 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_COUNTER_01);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000674 if (ret < 0)
675 return;
676
677 *val = _val | ret;
678}
679
Andrew Lunne413e7e2015-04-02 04:06:38 +0200680static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100681 { "in_good_octets", 8, 0x00, BANK0, },
682 { "in_bad_octets", 4, 0x02, BANK0, },
683 { "in_unicast", 4, 0x04, BANK0, },
684 { "in_broadcasts", 4, 0x06, BANK0, },
685 { "in_multicasts", 4, 0x07, BANK0, },
686 { "in_pause", 4, 0x16, BANK0, },
687 { "in_undersize", 4, 0x18, BANK0, },
688 { "in_fragments", 4, 0x19, BANK0, },
689 { "in_oversize", 4, 0x1a, BANK0, },
690 { "in_jabber", 4, 0x1b, BANK0, },
691 { "in_rx_error", 4, 0x1c, BANK0, },
692 { "in_fcs_error", 4, 0x1d, BANK0, },
693 { "out_octets", 8, 0x0e, BANK0, },
694 { "out_unicast", 4, 0x10, BANK0, },
695 { "out_broadcasts", 4, 0x13, BANK0, },
696 { "out_multicasts", 4, 0x12, BANK0, },
697 { "out_pause", 4, 0x15, BANK0, },
698 { "excessive", 4, 0x11, BANK0, },
699 { "collisions", 4, 0x1e, BANK0, },
700 { "deferred", 4, 0x05, BANK0, },
701 { "single", 4, 0x14, BANK0, },
702 { "multiple", 4, 0x17, BANK0, },
703 { "out_fcs_error", 4, 0x03, BANK0, },
704 { "late", 4, 0x1f, BANK0, },
705 { "hist_64bytes", 4, 0x08, BANK0, },
706 { "hist_65_127bytes", 4, 0x09, BANK0, },
707 { "hist_128_255bytes", 4, 0x0a, BANK0, },
708 { "hist_256_511bytes", 4, 0x0b, BANK0, },
709 { "hist_512_1023bytes", 4, 0x0c, BANK0, },
710 { "hist_1024_max_bytes", 4, 0x0d, BANK0, },
711 { "sw_in_discards", 4, 0x10, PORT, },
712 { "sw_in_filtered", 2, 0x12, PORT, },
713 { "sw_out_filtered", 2, 0x13, PORT, },
714 { "in_discards", 4, 0x00 | GLOBAL_STATS_OP_BANK_1, BANK1, },
715 { "in_filtered", 4, 0x01 | GLOBAL_STATS_OP_BANK_1, BANK1, },
716 { "in_accepted", 4, 0x02 | GLOBAL_STATS_OP_BANK_1, BANK1, },
717 { "in_bad_accepted", 4, 0x03 | GLOBAL_STATS_OP_BANK_1, BANK1, },
718 { "in_good_avb_class_a", 4, 0x04 | GLOBAL_STATS_OP_BANK_1, BANK1, },
719 { "in_good_avb_class_b", 4, 0x05 | GLOBAL_STATS_OP_BANK_1, BANK1, },
720 { "in_bad_avb_class_a", 4, 0x06 | GLOBAL_STATS_OP_BANK_1, BANK1, },
721 { "in_bad_avb_class_b", 4, 0x07 | GLOBAL_STATS_OP_BANK_1, BANK1, },
722 { "tcam_counter_0", 4, 0x08 | GLOBAL_STATS_OP_BANK_1, BANK1, },
723 { "tcam_counter_1", 4, 0x09 | GLOBAL_STATS_OP_BANK_1, BANK1, },
724 { "tcam_counter_2", 4, 0x0a | GLOBAL_STATS_OP_BANK_1, BANK1, },
725 { "tcam_counter_3", 4, 0x0b | GLOBAL_STATS_OP_BANK_1, BANK1, },
726 { "in_da_unknown", 4, 0x0e | GLOBAL_STATS_OP_BANK_1, BANK1, },
727 { "in_management", 4, 0x0f | GLOBAL_STATS_OP_BANK_1, BANK1, },
728 { "out_queue_0", 4, 0x10 | GLOBAL_STATS_OP_BANK_1, BANK1, },
729 { "out_queue_1", 4, 0x11 | GLOBAL_STATS_OP_BANK_1, BANK1, },
730 { "out_queue_2", 4, 0x12 | GLOBAL_STATS_OP_BANK_1, BANK1, },
731 { "out_queue_3", 4, 0x13 | GLOBAL_STATS_OP_BANK_1, BANK1, },
732 { "out_queue_4", 4, 0x14 | GLOBAL_STATS_OP_BANK_1, BANK1, },
733 { "out_queue_5", 4, 0x15 | GLOBAL_STATS_OP_BANK_1, BANK1, },
734 { "out_queue_6", 4, 0x16 | GLOBAL_STATS_OP_BANK_1, BANK1, },
735 { "out_queue_7", 4, 0x17 | GLOBAL_STATS_OP_BANK_1, BANK1, },
736 { "out_cut_through", 4, 0x18 | GLOBAL_STATS_OP_BANK_1, BANK1, },
737 { "out_octets_a", 4, 0x1a | GLOBAL_STATS_OP_BANK_1, BANK1, },
738 { "out_octets_b", 4, 0x1b | GLOBAL_STATS_OP_BANK_1, BANK1, },
739 { "out_management", 4, 0x1f | GLOBAL_STATS_OP_BANK_1, BANK1, },
Andrew Lunne413e7e2015-04-02 04:06:38 +0200740};
741
Vivien Didelotfad09c72016-06-21 12:28:20 -0400742static bool mv88e6xxx_has_stat(struct mv88e6xxx_chip *chip,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100743 struct mv88e6xxx_hw_stat *stat)
Andrew Lunne413e7e2015-04-02 04:06:38 +0200744{
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100745 switch (stat->type) {
746 case BANK0:
Andrew Lunne413e7e2015-04-02 04:06:38 +0200747 return true;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100748 case BANK1:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400749 return mv88e6xxx_6320_family(chip);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100750 case PORT:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400751 return mv88e6xxx_6095_family(chip) ||
752 mv88e6xxx_6185_family(chip) ||
753 mv88e6xxx_6097_family(chip) ||
754 mv88e6xxx_6165_family(chip) ||
755 mv88e6xxx_6351_family(chip) ||
756 mv88e6xxx_6352_family(chip);
Andrew Lunne413e7e2015-04-02 04:06:38 +0200757 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100758 return false;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000759}
760
Vivien Didelotfad09c72016-06-21 12:28:20 -0400761static uint64_t _mv88e6xxx_get_ethtool_stat(struct mv88e6xxx_chip *chip,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100762 struct mv88e6xxx_hw_stat *s,
Andrew Lunn80c46272015-06-20 18:42:30 +0200763 int port)
764{
Andrew Lunn80c46272015-06-20 18:42:30 +0200765 u32 low;
766 u32 high = 0;
767 int ret;
768 u64 value;
769
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100770 switch (s->type) {
771 case PORT:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400772 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), s->reg);
Andrew Lunn80c46272015-06-20 18:42:30 +0200773 if (ret < 0)
774 return UINT64_MAX;
775
776 low = ret;
777 if (s->sizeof_stat == 4) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400778 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port),
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100779 s->reg + 1);
Andrew Lunn80c46272015-06-20 18:42:30 +0200780 if (ret < 0)
781 return UINT64_MAX;
782 high = ret;
783 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100784 break;
785 case BANK0:
786 case BANK1:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400787 _mv88e6xxx_stats_read(chip, s->reg, &low);
Andrew Lunn80c46272015-06-20 18:42:30 +0200788 if (s->sizeof_stat == 8)
Vivien Didelotfad09c72016-06-21 12:28:20 -0400789 _mv88e6xxx_stats_read(chip, s->reg + 1, &high);
Andrew Lunn80c46272015-06-20 18:42:30 +0200790 }
791 value = (((u64)high) << 16) | low;
792 return value;
793}
794
Vivien Didelotf81ec902016-05-09 13:22:58 -0400795static void mv88e6xxx_get_strings(struct dsa_switch *ds, int port,
796 uint8_t *data)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100797{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400798 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100799 struct mv88e6xxx_hw_stat *stat;
800 int i, j;
801
802 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
803 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400804 if (mv88e6xxx_has_stat(chip, stat)) {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100805 memcpy(data + j * ETH_GSTRING_LEN, stat->string,
806 ETH_GSTRING_LEN);
807 j++;
808 }
809 }
810}
811
Vivien Didelotf81ec902016-05-09 13:22:58 -0400812static int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100813{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400814 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100815 struct mv88e6xxx_hw_stat *stat;
816 int i, j;
817
818 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
819 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400820 if (mv88e6xxx_has_stat(chip, stat))
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100821 j++;
822 }
823 return j;
824}
825
Vivien Didelotf81ec902016-05-09 13:22:58 -0400826static void mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, int port,
827 uint64_t *data)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000828{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400829 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100830 struct mv88e6xxx_hw_stat *stat;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000831 int ret;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100832 int i, j;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000833
Vivien Didelotfad09c72016-06-21 12:28:20 -0400834 mutex_lock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000835
Vivien Didelotfad09c72016-06-21 12:28:20 -0400836 ret = _mv88e6xxx_stats_snapshot(chip, port);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000837 if (ret < 0) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400838 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000839 return;
840 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100841 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
842 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400843 if (mv88e6xxx_has_stat(chip, stat)) {
844 data[j] = _mv88e6xxx_get_ethtool_stat(chip, stat, port);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100845 j++;
846 }
847 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000848
Vivien Didelotfad09c72016-06-21 12:28:20 -0400849 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000850}
Ben Hutchings98e67302011-11-25 14:36:19 +0000851
Vivien Didelotf81ec902016-05-09 13:22:58 -0400852static int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700853{
854 return 32 * sizeof(u16);
855}
856
Vivien Didelotf81ec902016-05-09 13:22:58 -0400857static void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
858 struct ethtool_regs *regs, void *_p)
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700859{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400860 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700861 u16 *p = _p;
862 int i;
863
864 regs->version = 0;
865
866 memset(p, 0xff, 32 * sizeof(u16));
867
Vivien Didelotfad09c72016-06-21 12:28:20 -0400868 mutex_lock(&chip->reg_lock);
Vivien Didelot23062512016-05-09 13:22:45 -0400869
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700870 for (i = 0; i < 32; i++) {
871 int ret;
872
Vivien Didelotfad09c72016-06-21 12:28:20 -0400873 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), i);
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700874 if (ret >= 0)
875 p[i] = ret;
876 }
Vivien Didelot23062512016-05-09 13:22:45 -0400877
Vivien Didelotfad09c72016-06-21 12:28:20 -0400878 mutex_unlock(&chip->reg_lock);
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700879}
880
Vivien Didelotfad09c72016-06-21 12:28:20 -0400881static int _mv88e6xxx_wait(struct mv88e6xxx_chip *chip, int reg, int offset,
Andrew Lunn3898c142015-05-06 01:09:53 +0200882 u16 mask)
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700883{
884 unsigned long timeout = jiffies + HZ / 10;
885
886 while (time_before(jiffies, timeout)) {
887 int ret;
888
Vivien Didelotfad09c72016-06-21 12:28:20 -0400889 ret = _mv88e6xxx_reg_read(chip, reg, offset);
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700890 if (ret < 0)
891 return ret;
892 if (!(ret & mask))
893 return 0;
894
895 usleep_range(1000, 2000);
896 }
897 return -ETIMEDOUT;
898}
899
Vivien Didelotfad09c72016-06-21 12:28:20 -0400900static int mv88e6xxx_wait(struct mv88e6xxx_chip *chip, int reg,
Andrew Lunn158bc062016-04-28 21:24:06 -0400901 int offset, u16 mask)
Andrew Lunn3898c142015-05-06 01:09:53 +0200902{
Andrew Lunn3898c142015-05-06 01:09:53 +0200903 int ret;
904
Vivien Didelotfad09c72016-06-21 12:28:20 -0400905 mutex_lock(&chip->reg_lock);
906 ret = _mv88e6xxx_wait(chip, reg, offset, mask);
907 mutex_unlock(&chip->reg_lock);
Andrew Lunn3898c142015-05-06 01:09:53 +0200908
909 return ret;
910}
911
Vivien Didelotfad09c72016-06-21 12:28:20 -0400912static int mv88e6xxx_mdio_wait(struct mv88e6xxx_chip *chip)
Andrew Lunn3898c142015-05-06 01:09:53 +0200913{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400914 return _mv88e6xxx_wait(chip, REG_GLOBAL2, GLOBAL2_SMI_OP,
Andrew Lunn3898c142015-05-06 01:09:53 +0200915 GLOBAL2_SMI_OP_BUSY);
916}
917
Vivien Didelotd24645b2016-05-09 13:22:41 -0400918static int mv88e6xxx_eeprom_load_wait(struct dsa_switch *ds)
Andrew Lunn3898c142015-05-06 01:09:53 +0200919{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400920 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunn158bc062016-04-28 21:24:06 -0400921
Vivien Didelotfad09c72016-06-21 12:28:20 -0400922 return mv88e6xxx_wait(chip, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
Andrew Lunn3898c142015-05-06 01:09:53 +0200923 GLOBAL2_EEPROM_OP_LOAD);
924}
925
Vivien Didelotd24645b2016-05-09 13:22:41 -0400926static int mv88e6xxx_eeprom_busy_wait(struct dsa_switch *ds)
Andrew Lunn3898c142015-05-06 01:09:53 +0200927{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400928 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunn158bc062016-04-28 21:24:06 -0400929
Vivien Didelotfad09c72016-06-21 12:28:20 -0400930 return mv88e6xxx_wait(chip, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
Andrew Lunn3898c142015-05-06 01:09:53 +0200931 GLOBAL2_EEPROM_OP_BUSY);
932}
933
Vivien Didelotd24645b2016-05-09 13:22:41 -0400934static int mv88e6xxx_read_eeprom_word(struct dsa_switch *ds, int addr)
935{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400936 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelotd24645b2016-05-09 13:22:41 -0400937 int ret;
938
Vivien Didelotfad09c72016-06-21 12:28:20 -0400939 mutex_lock(&chip->eeprom_mutex);
Vivien Didelotd24645b2016-05-09 13:22:41 -0400940
Vivien Didelotfad09c72016-06-21 12:28:20 -0400941 ret = mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
Vivien Didelotd24645b2016-05-09 13:22:41 -0400942 GLOBAL2_EEPROM_OP_READ |
943 (addr & GLOBAL2_EEPROM_OP_ADDR_MASK));
944 if (ret < 0)
945 goto error;
946
947 ret = mv88e6xxx_eeprom_busy_wait(ds);
948 if (ret < 0)
949 goto error;
950
Vivien Didelotfad09c72016-06-21 12:28:20 -0400951 ret = mv88e6xxx_reg_read(chip, REG_GLOBAL2, GLOBAL2_EEPROM_DATA);
Vivien Didelotd24645b2016-05-09 13:22:41 -0400952error:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400953 mutex_unlock(&chip->eeprom_mutex);
Vivien Didelotd24645b2016-05-09 13:22:41 -0400954 return ret;
955}
956
Andrew Lunnf8cd8752016-05-10 23:27:25 +0200957static int mv88e6xxx_get_eeprom_len(struct dsa_switch *ds)
958{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400959 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunnf8cd8752016-05-10 23:27:25 +0200960
Vivien Didelotfad09c72016-06-21 12:28:20 -0400961 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEPROM))
962 return chip->eeprom_len;
Andrew Lunnf8cd8752016-05-10 23:27:25 +0200963
964 return 0;
965}
966
Vivien Didelotf81ec902016-05-09 13:22:58 -0400967static int mv88e6xxx_get_eeprom(struct dsa_switch *ds,
968 struct ethtool_eeprom *eeprom, u8 *data)
Vivien Didelotd24645b2016-05-09 13:22:41 -0400969{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400970 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelotd24645b2016-05-09 13:22:41 -0400971 int offset;
972 int len;
973 int ret;
974
Vivien Didelotfad09c72016-06-21 12:28:20 -0400975 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEPROM))
Vivien Didelotd24645b2016-05-09 13:22:41 -0400976 return -EOPNOTSUPP;
977
978 offset = eeprom->offset;
979 len = eeprom->len;
980 eeprom->len = 0;
981
982 eeprom->magic = 0xc3ec4951;
983
984 ret = mv88e6xxx_eeprom_load_wait(ds);
985 if (ret < 0)
986 return ret;
987
988 if (offset & 1) {
989 int word;
990
991 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
992 if (word < 0)
993 return word;
994
995 *data++ = (word >> 8) & 0xff;
996
997 offset++;
998 len--;
999 eeprom->len++;
1000 }
1001
1002 while (len >= 2) {
1003 int word;
1004
1005 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
1006 if (word < 0)
1007 return word;
1008
1009 *data++ = word & 0xff;
1010 *data++ = (word >> 8) & 0xff;
1011
1012 offset += 2;
1013 len -= 2;
1014 eeprom->len += 2;
1015 }
1016
1017 if (len) {
1018 int word;
1019
1020 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
1021 if (word < 0)
1022 return word;
1023
1024 *data++ = word & 0xff;
1025
1026 offset++;
1027 len--;
1028 eeprom->len++;
1029 }
1030
1031 return 0;
1032}
1033
1034static int mv88e6xxx_eeprom_is_readonly(struct dsa_switch *ds)
1035{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001036 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelotd24645b2016-05-09 13:22:41 -04001037 int ret;
1038
Vivien Didelotfad09c72016-06-21 12:28:20 -04001039 ret = mv88e6xxx_reg_read(chip, REG_GLOBAL2, GLOBAL2_EEPROM_OP);
Vivien Didelotd24645b2016-05-09 13:22:41 -04001040 if (ret < 0)
1041 return ret;
1042
1043 if (!(ret & GLOBAL2_EEPROM_OP_WRITE_EN))
1044 return -EROFS;
1045
1046 return 0;
1047}
1048
1049static int mv88e6xxx_write_eeprom_word(struct dsa_switch *ds, int addr,
1050 u16 data)
1051{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001052 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelotd24645b2016-05-09 13:22:41 -04001053 int ret;
1054
Vivien Didelotfad09c72016-06-21 12:28:20 -04001055 mutex_lock(&chip->eeprom_mutex);
Vivien Didelotd24645b2016-05-09 13:22:41 -04001056
Vivien Didelotfad09c72016-06-21 12:28:20 -04001057 ret = mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_EEPROM_DATA, data);
Vivien Didelotd24645b2016-05-09 13:22:41 -04001058 if (ret < 0)
1059 goto error;
1060
Vivien Didelotfad09c72016-06-21 12:28:20 -04001061 ret = mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
Vivien Didelotd24645b2016-05-09 13:22:41 -04001062 GLOBAL2_EEPROM_OP_WRITE |
1063 (addr & GLOBAL2_EEPROM_OP_ADDR_MASK));
1064 if (ret < 0)
1065 goto error;
1066
1067 ret = mv88e6xxx_eeprom_busy_wait(ds);
1068error:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001069 mutex_unlock(&chip->eeprom_mutex);
Vivien Didelotd24645b2016-05-09 13:22:41 -04001070 return ret;
1071}
1072
Vivien Didelotf81ec902016-05-09 13:22:58 -04001073static int mv88e6xxx_set_eeprom(struct dsa_switch *ds,
1074 struct ethtool_eeprom *eeprom, u8 *data)
Vivien Didelotd24645b2016-05-09 13:22:41 -04001075{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001076 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelotd24645b2016-05-09 13:22:41 -04001077 int offset;
1078 int ret;
1079 int len;
1080
Vivien Didelotfad09c72016-06-21 12:28:20 -04001081 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEPROM))
Vivien Didelotd24645b2016-05-09 13:22:41 -04001082 return -EOPNOTSUPP;
1083
1084 if (eeprom->magic != 0xc3ec4951)
1085 return -EINVAL;
1086
1087 ret = mv88e6xxx_eeprom_is_readonly(ds);
1088 if (ret)
1089 return ret;
1090
1091 offset = eeprom->offset;
1092 len = eeprom->len;
1093 eeprom->len = 0;
1094
1095 ret = mv88e6xxx_eeprom_load_wait(ds);
1096 if (ret < 0)
1097 return ret;
1098
1099 if (offset & 1) {
1100 int word;
1101
1102 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
1103 if (word < 0)
1104 return word;
1105
1106 word = (*data++ << 8) | (word & 0xff);
1107
1108 ret = mv88e6xxx_write_eeprom_word(ds, offset >> 1, word);
1109 if (ret < 0)
1110 return ret;
1111
1112 offset++;
1113 len--;
1114 eeprom->len++;
1115 }
1116
1117 while (len >= 2) {
1118 int word;
1119
1120 word = *data++;
1121 word |= *data++ << 8;
1122
1123 ret = mv88e6xxx_write_eeprom_word(ds, offset >> 1, word);
1124 if (ret < 0)
1125 return ret;
1126
1127 offset += 2;
1128 len -= 2;
1129 eeprom->len += 2;
1130 }
1131
1132 if (len) {
1133 int word;
1134
1135 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
1136 if (word < 0)
1137 return word;
1138
1139 word = (word & 0xff00) | *data++;
1140
1141 ret = mv88e6xxx_write_eeprom_word(ds, offset >> 1, word);
1142 if (ret < 0)
1143 return ret;
1144
1145 offset++;
1146 len--;
1147 eeprom->len++;
1148 }
1149
1150 return 0;
1151}
1152
Vivien Didelotfad09c72016-06-21 12:28:20 -04001153static int _mv88e6xxx_atu_wait(struct mv88e6xxx_chip *chip)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001154{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001155 return _mv88e6xxx_wait(chip, REG_GLOBAL, GLOBAL_ATU_OP,
Andrew Lunncca8b132015-04-02 04:06:39 +02001156 GLOBAL_ATU_OP_BUSY);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001157}
1158
Vivien Didelotfad09c72016-06-21 12:28:20 -04001159static int mv88e6xxx_mdio_read_indirect(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001160 int addr, int regnum)
Andrew Lunnf3044682015-02-14 19:17:50 +01001161{
1162 int ret;
1163
Vivien Didelotfad09c72016-06-21 12:28:20 -04001164 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_SMI_OP,
Andrew Lunn3898c142015-05-06 01:09:53 +02001165 GLOBAL2_SMI_OP_22_READ | (addr << 5) |
1166 regnum);
Andrew Lunnf3044682015-02-14 19:17:50 +01001167 if (ret < 0)
1168 return ret;
1169
Vivien Didelotfad09c72016-06-21 12:28:20 -04001170 ret = mv88e6xxx_mdio_wait(chip);
Andrew Lunn3898c142015-05-06 01:09:53 +02001171 if (ret < 0)
1172 return ret;
1173
Vivien Didelotfad09c72016-06-21 12:28:20 -04001174 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL2, GLOBAL2_SMI_DATA);
Andrew Lunn158bc062016-04-28 21:24:06 -04001175
1176 return ret;
Andrew Lunnf3044682015-02-14 19:17:50 +01001177}
1178
Vivien Didelotfad09c72016-06-21 12:28:20 -04001179static int mv88e6xxx_mdio_write_indirect(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001180 int addr, int regnum, u16 val)
Andrew Lunnf3044682015-02-14 19:17:50 +01001181{
Andrew Lunn3898c142015-05-06 01:09:53 +02001182 int ret;
Andrew Lunnf3044682015-02-14 19:17:50 +01001183
Vivien Didelotfad09c72016-06-21 12:28:20 -04001184 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_SMI_DATA, val);
Andrew Lunn3898c142015-05-06 01:09:53 +02001185 if (ret < 0)
1186 return ret;
1187
Vivien Didelotfad09c72016-06-21 12:28:20 -04001188 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_SMI_OP,
Andrew Lunn3898c142015-05-06 01:09:53 +02001189 GLOBAL2_SMI_OP_22_WRITE | (addr << 5) |
1190 regnum);
1191
Vivien Didelotfad09c72016-06-21 12:28:20 -04001192 return mv88e6xxx_mdio_wait(chip);
Andrew Lunnf3044682015-02-14 19:17:50 +01001193}
1194
Vivien Didelotf81ec902016-05-09 13:22:58 -04001195static int mv88e6xxx_get_eee(struct dsa_switch *ds, int port,
1196 struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -08001197{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001198 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Guenter Roeck11b3b452015-03-06 22:23:51 -08001199 int reg;
1200
Vivien Didelotfad09c72016-06-21 12:28:20 -04001201 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
Vivien Didelotaadbdb82016-05-09 13:22:44 -04001202 return -EOPNOTSUPP;
1203
Vivien Didelotfad09c72016-06-21 12:28:20 -04001204 mutex_lock(&chip->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001205
Vivien Didelotfad09c72016-06-21 12:28:20 -04001206 reg = mv88e6xxx_mdio_read_indirect(chip, port, 16);
Guenter Roeck11b3b452015-03-06 22:23:51 -08001207 if (reg < 0)
Andrew Lunn2f40c692015-04-02 04:06:37 +02001208 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001209
1210 e->eee_enabled = !!(reg & 0x0200);
1211 e->tx_lpi_enabled = !!(reg & 0x0100);
1212
Vivien Didelotfad09c72016-06-21 12:28:20 -04001213 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_STATUS);
Guenter Roeck11b3b452015-03-06 22:23:51 -08001214 if (reg < 0)
Andrew Lunn2f40c692015-04-02 04:06:37 +02001215 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001216
Andrew Lunncca8b132015-04-02 04:06:39 +02001217 e->eee_active = !!(reg & PORT_STATUS_EEE);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001218 reg = 0;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001219
Andrew Lunn2f40c692015-04-02 04:06:37 +02001220out:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001221 mutex_unlock(&chip->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001222 return reg;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001223}
1224
Vivien Didelotf81ec902016-05-09 13:22:58 -04001225static int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
1226 struct phy_device *phydev, struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -08001227{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001228 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001229 int reg;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001230 int ret;
1231
Vivien Didelotfad09c72016-06-21 12:28:20 -04001232 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
Vivien Didelotaadbdb82016-05-09 13:22:44 -04001233 return -EOPNOTSUPP;
1234
Vivien Didelotfad09c72016-06-21 12:28:20 -04001235 mutex_lock(&chip->reg_lock);
Guenter Roeck11b3b452015-03-06 22:23:51 -08001236
Vivien Didelotfad09c72016-06-21 12:28:20 -04001237 ret = mv88e6xxx_mdio_read_indirect(chip, port, 16);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001238 if (ret < 0)
1239 goto out;
1240
1241 reg = ret & ~0x0300;
1242 if (e->eee_enabled)
1243 reg |= 0x0200;
1244 if (e->tx_lpi_enabled)
1245 reg |= 0x0100;
1246
Vivien Didelotfad09c72016-06-21 12:28:20 -04001247 ret = mv88e6xxx_mdio_write_indirect(chip, port, 16, reg);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001248out:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001249 mutex_unlock(&chip->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001250
1251 return ret;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001252}
1253
Vivien Didelotfad09c72016-06-21 12:28:20 -04001254static int _mv88e6xxx_atu_cmd(struct mv88e6xxx_chip *chip, u16 fid, u16 cmd)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001255{
1256 int ret;
1257
Vivien Didelotfad09c72016-06-21 12:28:20 -04001258 if (mv88e6xxx_has_fid_reg(chip)) {
1259 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_FID,
1260 fid);
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001261 if (ret < 0)
1262 return ret;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001263 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001264 /* ATU DBNum[7:4] are located in ATU Control 15:12 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001265 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL);
Vivien Didelot11ea8092016-03-31 16:53:44 -04001266 if (ret < 0)
1267 return ret;
1268
Vivien Didelotfad09c72016-06-21 12:28:20 -04001269 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL,
Vivien Didelot11ea8092016-03-31 16:53:44 -04001270 (ret & 0xfff) |
1271 ((fid << 8) & 0xf000));
1272 if (ret < 0)
1273 return ret;
1274
1275 /* ATU DBNum[3:0] are located in ATU Operation 3:0 */
1276 cmd |= fid & 0xf;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001277 }
1278
Vivien Didelotfad09c72016-06-21 12:28:20 -04001279 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_OP, cmd);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001280 if (ret < 0)
1281 return ret;
1282
Vivien Didelotfad09c72016-06-21 12:28:20 -04001283 return _mv88e6xxx_atu_wait(chip);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001284}
1285
Vivien Didelotfad09c72016-06-21 12:28:20 -04001286static int _mv88e6xxx_atu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot37705b72015-09-04 14:34:11 -04001287 struct mv88e6xxx_atu_entry *entry)
1288{
1289 u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK;
1290
1291 if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) {
1292 unsigned int mask, shift;
1293
1294 if (entry->trunk) {
1295 data |= GLOBAL_ATU_DATA_TRUNK;
1296 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
1297 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
1298 } else {
1299 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
1300 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
1301 }
1302
1303 data |= (entry->portv_trunkid << shift) & mask;
1304 }
1305
Vivien Didelotfad09c72016-06-21 12:28:20 -04001306 return _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_DATA, data);
Vivien Didelot37705b72015-09-04 14:34:11 -04001307}
1308
Vivien Didelotfad09c72016-06-21 12:28:20 -04001309static int _mv88e6xxx_atu_flush_move(struct mv88e6xxx_chip *chip,
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001310 struct mv88e6xxx_atu_entry *entry,
1311 bool static_too)
1312{
1313 int op;
1314 int err;
1315
Vivien Didelotfad09c72016-06-21 12:28:20 -04001316 err = _mv88e6xxx_atu_wait(chip);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001317 if (err)
1318 return err;
1319
Vivien Didelotfad09c72016-06-21 12:28:20 -04001320 err = _mv88e6xxx_atu_data_write(chip, entry);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001321 if (err)
1322 return err;
1323
1324 if (entry->fid) {
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001325 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB :
1326 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB;
1327 } else {
1328 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL :
1329 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC;
1330 }
1331
Vivien Didelotfad09c72016-06-21 12:28:20 -04001332 return _mv88e6xxx_atu_cmd(chip, entry->fid, op);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001333}
1334
Vivien Didelotfad09c72016-06-21 12:28:20 -04001335static int _mv88e6xxx_atu_flush(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001336 u16 fid, bool static_too)
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001337{
1338 struct mv88e6xxx_atu_entry entry = {
1339 .fid = fid,
1340 .state = 0, /* EntryState bits must be 0 */
1341 };
1342
Vivien Didelotfad09c72016-06-21 12:28:20 -04001343 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001344}
1345
Vivien Didelotfad09c72016-06-21 12:28:20 -04001346static int _mv88e6xxx_atu_move(struct mv88e6xxx_chip *chip, u16 fid,
Andrew Lunn158bc062016-04-28 21:24:06 -04001347 int from_port, int to_port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001348{
1349 struct mv88e6xxx_atu_entry entry = {
1350 .trunk = false,
1351 .fid = fid,
1352 };
1353
1354 /* EntryState bits must be 0xF */
1355 entry.state = GLOBAL_ATU_DATA_STATE_MASK;
1356
1357 /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */
1358 entry.portv_trunkid = (to_port & 0x0f) << 4;
1359 entry.portv_trunkid |= from_port & 0x0f;
1360
Vivien Didelotfad09c72016-06-21 12:28:20 -04001361 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001362}
1363
Vivien Didelotfad09c72016-06-21 12:28:20 -04001364static int _mv88e6xxx_atu_remove(struct mv88e6xxx_chip *chip, u16 fid,
Andrew Lunn158bc062016-04-28 21:24:06 -04001365 int port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001366{
1367 /* Destination port 0xF means remove the entries */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001368 return _mv88e6xxx_atu_move(chip, fid, port, 0x0f, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001369}
1370
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001371static const char * const mv88e6xxx_port_state_names[] = {
1372 [PORT_CONTROL_STATE_DISABLED] = "Disabled",
1373 [PORT_CONTROL_STATE_BLOCKING] = "Blocking/Listening",
1374 [PORT_CONTROL_STATE_LEARNING] = "Learning",
1375 [PORT_CONTROL_STATE_FORWARDING] = "Forwarding",
1376};
1377
Vivien Didelotfad09c72016-06-21 12:28:20 -04001378static int _mv88e6xxx_port_state(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001379 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001380{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001381 struct dsa_switch *ds = chip->ds;
Geert Uytterhoevenc3ffe6d2015-04-16 20:49:14 +02001382 int reg, ret = 0;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001383 u8 oldstate;
1384
Vivien Didelotfad09c72016-06-21 12:28:20 -04001385 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL);
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001386 if (reg < 0)
1387 return reg;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001388
Andrew Lunncca8b132015-04-02 04:06:39 +02001389 oldstate = reg & PORT_CONTROL_STATE_MASK;
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001390
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001391 if (oldstate != state) {
1392 /* Flush forwarding database if we're moving a port
1393 * from Learning or Forwarding state to Disabled or
1394 * Blocking or Listening state.
1395 */
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001396 if ((oldstate == PORT_CONTROL_STATE_LEARNING ||
Vivien Didelot57d32312016-06-20 13:13:58 -04001397 oldstate == PORT_CONTROL_STATE_FORWARDING) &&
1398 (state == PORT_CONTROL_STATE_DISABLED ||
1399 state == PORT_CONTROL_STATE_BLOCKING)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001400 ret = _mv88e6xxx_atu_remove(chip, 0, port, false);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001401 if (ret)
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001402 return ret;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001403 }
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001404
Andrew Lunncca8b132015-04-02 04:06:39 +02001405 reg = (reg & ~PORT_CONTROL_STATE_MASK) | state;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001406 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL,
Andrew Lunncca8b132015-04-02 04:06:39 +02001407 reg);
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001408 if (ret)
1409 return ret;
1410
Andrew Lunnc8b09802016-06-04 21:16:57 +02001411 netdev_dbg(ds->ports[port].netdev, "PortState %s (was %s)\n",
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001412 mv88e6xxx_port_state_names[state],
1413 mv88e6xxx_port_state_names[oldstate]);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001414 }
1415
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001416 return ret;
1417}
1418
Vivien Didelotfad09c72016-06-21 12:28:20 -04001419static int _mv88e6xxx_port_based_vlan_map(struct mv88e6xxx_chip *chip, int port)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001420{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001421 struct net_device *bridge = chip->ports[port].bridge_dev;
1422 const u16 mask = (1 << chip->info->num_ports) - 1;
1423 struct dsa_switch *ds = chip->ds;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001424 u16 output_ports = 0;
Vivien Didelotede80982015-10-11 18:08:35 -04001425 int reg;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001426 int i;
1427
1428 /* allow CPU port or DSA link(s) to send frames to every port */
1429 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
1430 output_ports = mask;
1431 } else {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001432 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001433 /* allow sending frames to every group member */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001434 if (bridge && chip->ports[i].bridge_dev == bridge)
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001435 output_ports |= BIT(i);
1436
1437 /* allow sending frames to CPU port and DSA link(s) */
1438 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
1439 output_ports |= BIT(i);
1440 }
1441 }
1442
1443 /* prevent frames from going back out of the port they came in on */
1444 output_ports &= ~BIT(port);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001445
Vivien Didelotfad09c72016-06-21 12:28:20 -04001446 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_BASE_VLAN);
Vivien Didelotede80982015-10-11 18:08:35 -04001447 if (reg < 0)
1448 return reg;
1449
1450 reg &= ~mask;
1451 reg |= output_ports & mask;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001452
Vivien Didelotfad09c72016-06-21 12:28:20 -04001453 return _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_BASE_VLAN, reg);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001454}
1455
Vivien Didelotf81ec902016-05-09 13:22:58 -04001456static void mv88e6xxx_port_stp_state_set(struct dsa_switch *ds, int port,
1457 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001458{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001459 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001460 int stp_state;
Vivien Didelot553eb542016-05-13 20:38:23 -04001461 int err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001462
1463 switch (state) {
1464 case BR_STATE_DISABLED:
Andrew Lunncca8b132015-04-02 04:06:39 +02001465 stp_state = PORT_CONTROL_STATE_DISABLED;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001466 break;
1467 case BR_STATE_BLOCKING:
1468 case BR_STATE_LISTENING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001469 stp_state = PORT_CONTROL_STATE_BLOCKING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001470 break;
1471 case BR_STATE_LEARNING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001472 stp_state = PORT_CONTROL_STATE_LEARNING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001473 break;
1474 case BR_STATE_FORWARDING:
1475 default:
Andrew Lunncca8b132015-04-02 04:06:39 +02001476 stp_state = PORT_CONTROL_STATE_FORWARDING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001477 break;
1478 }
1479
Vivien Didelotfad09c72016-06-21 12:28:20 -04001480 mutex_lock(&chip->reg_lock);
1481 err = _mv88e6xxx_port_state(chip, port, stp_state);
1482 mutex_unlock(&chip->reg_lock);
Vivien Didelot553eb542016-05-13 20:38:23 -04001483
1484 if (err)
Andrew Lunnc8b09802016-06-04 21:16:57 +02001485 netdev_err(ds->ports[port].netdev,
1486 "failed to update state to %s\n",
Vivien Didelot553eb542016-05-13 20:38:23 -04001487 mv88e6xxx_port_state_names[stp_state]);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001488}
1489
Vivien Didelotfad09c72016-06-21 12:28:20 -04001490static int _mv88e6xxx_port_pvid(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001491 u16 *new, u16 *old)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001492{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001493 struct dsa_switch *ds = chip->ds;
Vivien Didelot5da96032016-03-07 18:24:39 -05001494 u16 pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001495 int ret;
1496
Vivien Didelotfad09c72016-06-21 12:28:20 -04001497 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_DEFAULT_VLAN);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001498 if (ret < 0)
1499 return ret;
1500
Vivien Didelot5da96032016-03-07 18:24:39 -05001501 pvid = ret & PORT_DEFAULT_VLAN_MASK;
1502
1503 if (new) {
1504 ret &= ~PORT_DEFAULT_VLAN_MASK;
1505 ret |= *new & PORT_DEFAULT_VLAN_MASK;
1506
Vivien Didelotfad09c72016-06-21 12:28:20 -04001507 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Vivien Didelot5da96032016-03-07 18:24:39 -05001508 PORT_DEFAULT_VLAN, ret);
1509 if (ret < 0)
1510 return ret;
1511
Andrew Lunnc8b09802016-06-04 21:16:57 +02001512 netdev_dbg(ds->ports[port].netdev,
1513 "DefaultVID %d (was %d)\n", *new, pvid);
Vivien Didelot5da96032016-03-07 18:24:39 -05001514 }
1515
1516 if (old)
1517 *old = pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001518
1519 return 0;
1520}
1521
Vivien Didelotfad09c72016-06-21 12:28:20 -04001522static int _mv88e6xxx_port_pvid_get(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001523 int port, u16 *pvid)
Vivien Didelot5da96032016-03-07 18:24:39 -05001524{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001525 return _mv88e6xxx_port_pvid(chip, port, NULL, pvid);
Vivien Didelot5da96032016-03-07 18:24:39 -05001526}
1527
Vivien Didelotfad09c72016-06-21 12:28:20 -04001528static int _mv88e6xxx_port_pvid_set(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001529 int port, u16 pvid)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001530{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001531 return _mv88e6xxx_port_pvid(chip, port, &pvid, NULL);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001532}
1533
Vivien Didelotfad09c72016-06-21 12:28:20 -04001534static int _mv88e6xxx_vtu_wait(struct mv88e6xxx_chip *chip)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001535{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001536 return _mv88e6xxx_wait(chip, REG_GLOBAL, GLOBAL_VTU_OP,
Vivien Didelot6b17e862015-08-13 12:52:18 -04001537 GLOBAL_VTU_OP_BUSY);
1538}
1539
Vivien Didelotfad09c72016-06-21 12:28:20 -04001540static int _mv88e6xxx_vtu_cmd(struct mv88e6xxx_chip *chip, u16 op)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001541{
1542 int ret;
1543
Vivien Didelotfad09c72016-06-21 12:28:20 -04001544 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_OP, op);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001545 if (ret < 0)
1546 return ret;
1547
Vivien Didelotfad09c72016-06-21 12:28:20 -04001548 return _mv88e6xxx_vtu_wait(chip);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001549}
1550
Vivien Didelotfad09c72016-06-21 12:28:20 -04001551static int _mv88e6xxx_vtu_stu_flush(struct mv88e6xxx_chip *chip)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001552{
1553 int ret;
1554
Vivien Didelotfad09c72016-06-21 12:28:20 -04001555 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001556 if (ret < 0)
1557 return ret;
1558
Vivien Didelotfad09c72016-06-21 12:28:20 -04001559 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_FLUSH_ALL);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001560}
1561
Vivien Didelotfad09c72016-06-21 12:28:20 -04001562static int _mv88e6xxx_vtu_stu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001563 struct mv88e6xxx_vtu_stu_entry *entry,
1564 unsigned int nibble_offset)
1565{
Vivien Didelotb8fee952015-08-13 12:52:19 -04001566 u16 regs[3];
1567 int i;
1568 int ret;
1569
1570 for (i = 0; i < 3; ++i) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001571 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001572 GLOBAL_VTU_DATA_0_3 + i);
1573 if (ret < 0)
1574 return ret;
1575
1576 regs[i] = ret;
1577 }
1578
Vivien Didelotfad09c72016-06-21 12:28:20 -04001579 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelotb8fee952015-08-13 12:52:19 -04001580 unsigned int shift = (i % 4) * 4 + nibble_offset;
1581 u16 reg = regs[i / 4];
1582
1583 entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
1584 }
1585
1586 return 0;
1587}
1588
Vivien Didelotfad09c72016-06-21 12:28:20 -04001589static int mv88e6xxx_vtu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001590 struct mv88e6xxx_vtu_stu_entry *entry)
1591{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001592 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 0);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001593}
1594
Vivien Didelotfad09c72016-06-21 12:28:20 -04001595static int mv88e6xxx_stu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001596 struct mv88e6xxx_vtu_stu_entry *entry)
1597{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001598 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 2);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001599}
1600
Vivien Didelotfad09c72016-06-21 12:28:20 -04001601static int _mv88e6xxx_vtu_stu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001602 struct mv88e6xxx_vtu_stu_entry *entry,
1603 unsigned int nibble_offset)
1604{
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001605 u16 regs[3] = { 0 };
1606 int i;
1607 int ret;
1608
Vivien Didelotfad09c72016-06-21 12:28:20 -04001609 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001610 unsigned int shift = (i % 4) * 4 + nibble_offset;
1611 u8 data = entry->data[i];
1612
1613 regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
1614 }
1615
1616 for (i = 0; i < 3; ++i) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001617 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001618 GLOBAL_VTU_DATA_0_3 + i, regs[i]);
1619 if (ret < 0)
1620 return ret;
1621 }
1622
1623 return 0;
1624}
1625
Vivien Didelotfad09c72016-06-21 12:28:20 -04001626static int mv88e6xxx_vtu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001627 struct mv88e6xxx_vtu_stu_entry *entry)
1628{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001629 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 0);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001630}
1631
Vivien Didelotfad09c72016-06-21 12:28:20 -04001632static int mv88e6xxx_stu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001633 struct mv88e6xxx_vtu_stu_entry *entry)
1634{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001635 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 2);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001636}
1637
Vivien Didelotfad09c72016-06-21 12:28:20 -04001638static int _mv88e6xxx_vtu_vid_write(struct mv88e6xxx_chip *chip, u16 vid)
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001639{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001640 return _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID,
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001641 vid & GLOBAL_VTU_VID_MASK);
1642}
1643
Vivien Didelotfad09c72016-06-21 12:28:20 -04001644static int _mv88e6xxx_vtu_getnext(struct mv88e6xxx_chip *chip,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001645 struct mv88e6xxx_vtu_stu_entry *entry)
1646{
1647 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1648 int ret;
1649
Vivien Didelotfad09c72016-06-21 12:28:20 -04001650 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001651 if (ret < 0)
1652 return ret;
1653
Vivien Didelotfad09c72016-06-21 12:28:20 -04001654 ret = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_VTU_GET_NEXT);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001655 if (ret < 0)
1656 return ret;
1657
Vivien Didelotfad09c72016-06-21 12:28:20 -04001658 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_VID);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001659 if (ret < 0)
1660 return ret;
1661
1662 next.vid = ret & GLOBAL_VTU_VID_MASK;
1663 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1664
1665 if (next.valid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001666 ret = mv88e6xxx_vtu_data_read(chip, &next);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001667 if (ret < 0)
1668 return ret;
1669
Vivien Didelotfad09c72016-06-21 12:28:20 -04001670 if (mv88e6xxx_has_fid_reg(chip)) {
1671 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001672 GLOBAL_VTU_FID);
1673 if (ret < 0)
1674 return ret;
1675
1676 next.fid = ret & GLOBAL_VTU_FID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001677 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001678 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1679 * VTU DBNum[3:0] are located in VTU Operation 3:0
1680 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001681 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
Vivien Didelot11ea8092016-03-31 16:53:44 -04001682 GLOBAL_VTU_OP);
1683 if (ret < 0)
1684 return ret;
1685
1686 next.fid = (ret & 0xf00) >> 4;
1687 next.fid |= ret & 0xf;
Vivien Didelot2e7bd5e2016-03-31 16:53:41 -04001688 }
Vivien Didelotb8fee952015-08-13 12:52:19 -04001689
Vivien Didelotfad09c72016-06-21 12:28:20 -04001690 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
1691 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001692 GLOBAL_VTU_SID);
1693 if (ret < 0)
1694 return ret;
1695
1696 next.sid = ret & GLOBAL_VTU_SID_MASK;
1697 }
1698 }
1699
1700 *entry = next;
1701 return 0;
1702}
1703
Vivien Didelotf81ec902016-05-09 13:22:58 -04001704static int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
1705 struct switchdev_obj_port_vlan *vlan,
1706 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001707{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001708 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001709 struct mv88e6xxx_vtu_stu_entry next;
1710 u16 pvid;
1711 int err;
1712
Vivien Didelotfad09c72016-06-21 12:28:20 -04001713 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001714 return -EOPNOTSUPP;
1715
Vivien Didelotfad09c72016-06-21 12:28:20 -04001716 mutex_lock(&chip->reg_lock);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001717
Vivien Didelotfad09c72016-06-21 12:28:20 -04001718 err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001719 if (err)
1720 goto unlock;
1721
Vivien Didelotfad09c72016-06-21 12:28:20 -04001722 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001723 if (err)
1724 goto unlock;
1725
1726 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001727 err = _mv88e6xxx_vtu_getnext(chip, &next);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001728 if (err)
1729 break;
1730
1731 if (!next.valid)
1732 break;
1733
1734 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1735 continue;
1736
1737 /* reinit and dump this VLAN obj */
Vivien Didelot57d32312016-06-20 13:13:58 -04001738 vlan->vid_begin = next.vid;
1739 vlan->vid_end = next.vid;
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001740 vlan->flags = 0;
1741
1742 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1743 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1744
1745 if (next.vid == pvid)
1746 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1747
1748 err = cb(&vlan->obj);
1749 if (err)
1750 break;
1751 } while (next.vid < GLOBAL_VTU_VID_MASK);
1752
1753unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001754 mutex_unlock(&chip->reg_lock);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001755
1756 return err;
1757}
1758
Vivien Didelotfad09c72016-06-21 12:28:20 -04001759static int _mv88e6xxx_vtu_loadpurge(struct mv88e6xxx_chip *chip,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001760 struct mv88e6xxx_vtu_stu_entry *entry)
1761{
Vivien Didelot11ea8092016-03-31 16:53:44 -04001762 u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001763 u16 reg = 0;
1764 int ret;
1765
Vivien Didelotfad09c72016-06-21 12:28:20 -04001766 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001767 if (ret < 0)
1768 return ret;
1769
1770 if (!entry->valid)
1771 goto loadpurge;
1772
1773 /* Write port member tags */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001774 ret = mv88e6xxx_vtu_data_write(chip, entry);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001775 if (ret < 0)
1776 return ret;
1777
Vivien Didelotfad09c72016-06-21 12:28:20 -04001778 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001779 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001780 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID,
1781 reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001782 if (ret < 0)
1783 return ret;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001784 }
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001785
Vivien Didelotfad09c72016-06-21 12:28:20 -04001786 if (mv88e6xxx_has_fid_reg(chip)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001787 reg = entry->fid & GLOBAL_VTU_FID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001788 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_FID,
1789 reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001790 if (ret < 0)
1791 return ret;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001792 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001793 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1794 * VTU DBNum[3:0] are located in VTU Operation 3:0
1795 */
1796 op |= (entry->fid & 0xf0) << 8;
1797 op |= entry->fid & 0xf;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001798 }
1799
1800 reg = GLOBAL_VTU_VID_VALID;
1801loadpurge:
1802 reg |= entry->vid & GLOBAL_VTU_VID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001803 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID, reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001804 if (ret < 0)
1805 return ret;
1806
Vivien Didelotfad09c72016-06-21 12:28:20 -04001807 return _mv88e6xxx_vtu_cmd(chip, op);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001808}
1809
Vivien Didelotfad09c72016-06-21 12:28:20 -04001810static int _mv88e6xxx_stu_getnext(struct mv88e6xxx_chip *chip, u8 sid,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001811 struct mv88e6xxx_vtu_stu_entry *entry)
1812{
1813 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1814 int ret;
1815
Vivien Didelotfad09c72016-06-21 12:28:20 -04001816 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001817 if (ret < 0)
1818 return ret;
1819
Vivien Didelotfad09c72016-06-21 12:28:20 -04001820 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001821 sid & GLOBAL_VTU_SID_MASK);
1822 if (ret < 0)
1823 return ret;
1824
Vivien Didelotfad09c72016-06-21 12:28:20 -04001825 ret = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_GET_NEXT);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001826 if (ret < 0)
1827 return ret;
1828
Vivien Didelotfad09c72016-06-21 12:28:20 -04001829 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_SID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001830 if (ret < 0)
1831 return ret;
1832
1833 next.sid = ret & GLOBAL_VTU_SID_MASK;
1834
Vivien Didelotfad09c72016-06-21 12:28:20 -04001835 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_VID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001836 if (ret < 0)
1837 return ret;
1838
1839 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1840
1841 if (next.valid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001842 ret = mv88e6xxx_stu_data_read(chip, &next);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001843 if (ret < 0)
1844 return ret;
1845 }
1846
1847 *entry = next;
1848 return 0;
1849}
1850
Vivien Didelotfad09c72016-06-21 12:28:20 -04001851static int _mv88e6xxx_stu_loadpurge(struct mv88e6xxx_chip *chip,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001852 struct mv88e6xxx_vtu_stu_entry *entry)
1853{
1854 u16 reg = 0;
1855 int ret;
1856
Vivien Didelotfad09c72016-06-21 12:28:20 -04001857 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001858 if (ret < 0)
1859 return ret;
1860
1861 if (!entry->valid)
1862 goto loadpurge;
1863
1864 /* Write port states */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001865 ret = mv88e6xxx_stu_data_write(chip, entry);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001866 if (ret < 0)
1867 return ret;
1868
1869 reg = GLOBAL_VTU_VID_VALID;
1870loadpurge:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001871 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID, reg);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001872 if (ret < 0)
1873 return ret;
1874
1875 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001876 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID, reg);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001877 if (ret < 0)
1878 return ret;
1879
Vivien Didelotfad09c72016-06-21 12:28:20 -04001880 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_LOAD_PURGE);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001881}
1882
Vivien Didelotfad09c72016-06-21 12:28:20 -04001883static int _mv88e6xxx_port_fid(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001884 u16 *new, u16 *old)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001885{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001886 struct dsa_switch *ds = chip->ds;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001887 u16 upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001888 u16 fid;
1889 int ret;
1890
Vivien Didelotfad09c72016-06-21 12:28:20 -04001891 if (mv88e6xxx_num_databases(chip) == 4096)
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001892 upper_mask = 0xff;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001893 else if (mv88e6xxx_num_databases(chip) == 256)
Vivien Didelot11ea8092016-03-31 16:53:44 -04001894 upper_mask = 0xf;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001895 else
1896 return -EOPNOTSUPP;
1897
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001898 /* Port's default FID bits 3:0 are located in reg 0x06, offset 12 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001899 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_BASE_VLAN);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001900 if (ret < 0)
1901 return ret;
1902
1903 fid = (ret & PORT_BASE_VLAN_FID_3_0_MASK) >> 12;
1904
1905 if (new) {
1906 ret &= ~PORT_BASE_VLAN_FID_3_0_MASK;
1907 ret |= (*new << 12) & PORT_BASE_VLAN_FID_3_0_MASK;
1908
Vivien Didelotfad09c72016-06-21 12:28:20 -04001909 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_BASE_VLAN,
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001910 ret);
1911 if (ret < 0)
1912 return ret;
1913 }
1914
1915 /* Port's default FID bits 11:4 are located in reg 0x05, offset 0 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001916 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL_1);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001917 if (ret < 0)
1918 return ret;
1919
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001920 fid |= (ret & upper_mask) << 4;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001921
1922 if (new) {
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001923 ret &= ~upper_mask;
1924 ret |= (*new >> 4) & upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001925
Vivien Didelotfad09c72016-06-21 12:28:20 -04001926 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_1,
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001927 ret);
1928 if (ret < 0)
1929 return ret;
1930
Andrew Lunnc8b09802016-06-04 21:16:57 +02001931 netdev_dbg(ds->ports[port].netdev,
1932 "FID %d (was %d)\n", *new, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001933 }
1934
1935 if (old)
1936 *old = fid;
1937
1938 return 0;
1939}
1940
Vivien Didelotfad09c72016-06-21 12:28:20 -04001941static int _mv88e6xxx_port_fid_get(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001942 int port, u16 *fid)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001943{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001944 return _mv88e6xxx_port_fid(chip, port, NULL, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001945}
1946
Vivien Didelotfad09c72016-06-21 12:28:20 -04001947static int _mv88e6xxx_port_fid_set(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001948 int port, u16 fid)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001949{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001950 return _mv88e6xxx_port_fid(chip, port, &fid, NULL);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001951}
1952
Vivien Didelotfad09c72016-06-21 12:28:20 -04001953static int _mv88e6xxx_fid_new(struct mv88e6xxx_chip *chip, u16 *fid)
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001954{
1955 DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
1956 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001957 int i, err;
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001958
1959 bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);
1960
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001961 /* Set every FID bit used by the (un)bridged ports */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001962 for (i = 0; i < chip->info->num_ports; ++i) {
1963 err = _mv88e6xxx_port_fid_get(chip, i, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001964 if (err)
1965 return err;
1966
1967 set_bit(*fid, fid_bitmap);
1968 }
1969
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001970 /* Set every FID bit used by the VLAN entries */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001971 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001972 if (err)
1973 return err;
1974
1975 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001976 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001977 if (err)
1978 return err;
1979
1980 if (!vlan.valid)
1981 break;
1982
1983 set_bit(vlan.fid, fid_bitmap);
1984 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
1985
1986 /* The reset value 0x000 is used to indicate that multiple address
1987 * databases are not needed. Return the next positive available.
1988 */
1989 *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1);
Vivien Didelotfad09c72016-06-21 12:28:20 -04001990 if (unlikely(*fid >= mv88e6xxx_num_databases(chip)))
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001991 return -ENOSPC;
1992
1993 /* Clear the database */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001994 return _mv88e6xxx_atu_flush(chip, *fid, true);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001995}
1996
Vivien Didelotfad09c72016-06-21 12:28:20 -04001997static int _mv88e6xxx_vtu_new(struct mv88e6xxx_chip *chip, u16 vid,
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001998 struct mv88e6xxx_vtu_stu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001999{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002000 struct dsa_switch *ds = chip->ds;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002001 struct mv88e6xxx_vtu_stu_entry vlan = {
2002 .valid = true,
2003 .vid = vid,
2004 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002005 int i, err;
2006
Vivien Didelotfad09c72016-06-21 12:28:20 -04002007 err = _mv88e6xxx_fid_new(chip, &vlan.fid);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002008 if (err)
2009 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002010
Vivien Didelot3d131f02015-11-03 10:52:52 -05002011 /* exclude all ports except the CPU and DSA ports */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002012 for (i = 0; i < chip->info->num_ports; ++i)
Vivien Didelot3d131f02015-11-03 10:52:52 -05002013 vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)
2014 ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED
2015 : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002016
Vivien Didelotfad09c72016-06-21 12:28:20 -04002017 if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
2018 mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip)) {
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002019 struct mv88e6xxx_vtu_stu_entry vstp;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002020
2021 /* Adding a VTU entry requires a valid STU entry. As VSTP is not
2022 * implemented, only one STU entry is needed to cover all VTU
2023 * entries. Thus, validate the SID 0.
2024 */
2025 vlan.sid = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002026 err = _mv88e6xxx_stu_getnext(chip, GLOBAL_VTU_SID_MASK, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002027 if (err)
2028 return err;
2029
2030 if (vstp.sid != vlan.sid || !vstp.valid) {
2031 memset(&vstp, 0, sizeof(vstp));
2032 vstp.valid = true;
2033 vstp.sid = vlan.sid;
2034
Vivien Didelotfad09c72016-06-21 12:28:20 -04002035 err = _mv88e6xxx_stu_loadpurge(chip, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002036 if (err)
2037 return err;
2038 }
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002039 }
2040
2041 *entry = vlan;
2042 return 0;
2043}
2044
Vivien Didelotfad09c72016-06-21 12:28:20 -04002045static int _mv88e6xxx_vtu_get(struct mv88e6xxx_chip *chip, u16 vid,
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05002046 struct mv88e6xxx_vtu_stu_entry *entry, bool creat)
2047{
2048 int err;
2049
2050 if (!vid)
2051 return -EINVAL;
2052
Vivien Didelotfad09c72016-06-21 12:28:20 -04002053 err = _mv88e6xxx_vtu_vid_write(chip, vid - 1);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05002054 if (err)
2055 return err;
2056
Vivien Didelotfad09c72016-06-21 12:28:20 -04002057 err = _mv88e6xxx_vtu_getnext(chip, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05002058 if (err)
2059 return err;
2060
2061 if (entry->vid != vid || !entry->valid) {
2062 if (!creat)
2063 return -EOPNOTSUPP;
2064 /* -ENOENT would've been more appropriate, but switchdev expects
2065 * -EOPNOTSUPP to inform bridge about an eventual software VLAN.
2066 */
2067
Vivien Didelotfad09c72016-06-21 12:28:20 -04002068 err = _mv88e6xxx_vtu_new(chip, vid, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05002069 }
2070
2071 return err;
2072}
2073
Vivien Didelotda9c3592016-02-12 12:09:40 -05002074static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
2075 u16 vid_begin, u16 vid_end)
2076{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002077 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelotda9c3592016-02-12 12:09:40 -05002078 struct mv88e6xxx_vtu_stu_entry vlan;
2079 int i, err;
2080
2081 if (!vid_begin)
2082 return -EOPNOTSUPP;
2083
Vivien Didelotfad09c72016-06-21 12:28:20 -04002084 mutex_lock(&chip->reg_lock);
Vivien Didelotda9c3592016-02-12 12:09:40 -05002085
Vivien Didelotfad09c72016-06-21 12:28:20 -04002086 err = _mv88e6xxx_vtu_vid_write(chip, vid_begin - 1);
Vivien Didelotda9c3592016-02-12 12:09:40 -05002087 if (err)
2088 goto unlock;
2089
2090 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002091 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelotda9c3592016-02-12 12:09:40 -05002092 if (err)
2093 goto unlock;
2094
2095 if (!vlan.valid)
2096 break;
2097
2098 if (vlan.vid > vid_end)
2099 break;
2100
Vivien Didelotfad09c72016-06-21 12:28:20 -04002101 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelotda9c3592016-02-12 12:09:40 -05002102 if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i))
2103 continue;
2104
2105 if (vlan.data[i] ==
2106 GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
2107 continue;
2108
Vivien Didelotfad09c72016-06-21 12:28:20 -04002109 if (chip->ports[i].bridge_dev ==
2110 chip->ports[port].bridge_dev)
Vivien Didelotda9c3592016-02-12 12:09:40 -05002111 break; /* same bridge, check next VLAN */
2112
Andrew Lunnc8b09802016-06-04 21:16:57 +02002113 netdev_warn(ds->ports[port].netdev,
Vivien Didelotda9c3592016-02-12 12:09:40 -05002114 "hardware VLAN %d already used by %s\n",
2115 vlan.vid,
Vivien Didelotfad09c72016-06-21 12:28:20 -04002116 netdev_name(chip->ports[i].bridge_dev));
Vivien Didelotda9c3592016-02-12 12:09:40 -05002117 err = -EOPNOTSUPP;
2118 goto unlock;
2119 }
2120 } while (vlan.vid < vid_end);
2121
2122unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002123 mutex_unlock(&chip->reg_lock);
Vivien Didelotda9c3592016-02-12 12:09:40 -05002124
2125 return err;
2126}
2127
Vivien Didelot214cdb92016-02-26 13:16:08 -05002128static const char * const mv88e6xxx_port_8021q_mode_names[] = {
2129 [PORT_CONTROL_2_8021Q_DISABLED] = "Disabled",
2130 [PORT_CONTROL_2_8021Q_FALLBACK] = "Fallback",
2131 [PORT_CONTROL_2_8021Q_CHECK] = "Check",
2132 [PORT_CONTROL_2_8021Q_SECURE] = "Secure",
2133};
2134
Vivien Didelotf81ec902016-05-09 13:22:58 -04002135static int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port,
2136 bool vlan_filtering)
Vivien Didelot214cdb92016-02-26 13:16:08 -05002137{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002138 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelot214cdb92016-02-26 13:16:08 -05002139 u16 old, new = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE :
2140 PORT_CONTROL_2_8021Q_DISABLED;
2141 int ret;
2142
Vivien Didelotfad09c72016-06-21 12:28:20 -04002143 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04002144 return -EOPNOTSUPP;
2145
Vivien Didelotfad09c72016-06-21 12:28:20 -04002146 mutex_lock(&chip->reg_lock);
Vivien Didelot214cdb92016-02-26 13:16:08 -05002147
Vivien Didelotfad09c72016-06-21 12:28:20 -04002148 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL_2);
Vivien Didelot214cdb92016-02-26 13:16:08 -05002149 if (ret < 0)
2150 goto unlock;
2151
2152 old = ret & PORT_CONTROL_2_8021Q_MASK;
2153
Vivien Didelot5220ef12016-03-07 18:24:52 -05002154 if (new != old) {
2155 ret &= ~PORT_CONTROL_2_8021Q_MASK;
2156 ret |= new & PORT_CONTROL_2_8021Q_MASK;
Vivien Didelot214cdb92016-02-26 13:16:08 -05002157
Vivien Didelotfad09c72016-06-21 12:28:20 -04002158 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_2,
Vivien Didelot5220ef12016-03-07 18:24:52 -05002159 ret);
2160 if (ret < 0)
2161 goto unlock;
Vivien Didelot214cdb92016-02-26 13:16:08 -05002162
Andrew Lunnc8b09802016-06-04 21:16:57 +02002163 netdev_dbg(ds->ports[port].netdev, "802.1Q Mode %s (was %s)\n",
Vivien Didelot5220ef12016-03-07 18:24:52 -05002164 mv88e6xxx_port_8021q_mode_names[new],
2165 mv88e6xxx_port_8021q_mode_names[old]);
2166 }
2167
2168 ret = 0;
Vivien Didelot214cdb92016-02-26 13:16:08 -05002169unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002170 mutex_unlock(&chip->reg_lock);
Vivien Didelot214cdb92016-02-26 13:16:08 -05002171
2172 return ret;
2173}
2174
Vivien Didelot57d32312016-06-20 13:13:58 -04002175static int
2176mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
2177 const struct switchdev_obj_port_vlan *vlan,
2178 struct switchdev_trans *trans)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002179{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002180 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelotda9c3592016-02-12 12:09:40 -05002181 int err;
2182
Vivien Didelotfad09c72016-06-21 12:28:20 -04002183 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04002184 return -EOPNOTSUPP;
2185
Vivien Didelotda9c3592016-02-12 12:09:40 -05002186 /* If the requested port doesn't belong to the same bridge as the VLAN
2187 * members, do not support it (yet) and fallback to software VLAN.
2188 */
2189 err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin,
2190 vlan->vid_end);
2191 if (err)
2192 return err;
2193
Vivien Didelot76e398a2015-11-01 12:33:55 -05002194 /* We don't need any dynamic resource from the kernel (yet),
2195 * so skip the prepare phase.
2196 */
2197 return 0;
2198}
2199
Vivien Didelotfad09c72016-06-21 12:28:20 -04002200static int _mv88e6xxx_port_vlan_add(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04002201 u16 vid, bool untagged)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002202{
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002203 struct mv88e6xxx_vtu_stu_entry vlan;
2204 int err;
2205
Vivien Didelotfad09c72016-06-21 12:28:20 -04002206 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, true);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002207 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002208 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002209
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002210 vlan.data[port] = untagged ?
2211 GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
2212 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
2213
Vivien Didelotfad09c72016-06-21 12:28:20 -04002214 return _mv88e6xxx_vtu_loadpurge(chip, &vlan);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002215}
2216
Vivien Didelotf81ec902016-05-09 13:22:58 -04002217static void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
2218 const struct switchdev_obj_port_vlan *vlan,
2219 struct switchdev_trans *trans)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002220{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002221 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002222 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
2223 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
2224 u16 vid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05002225
Vivien Didelotfad09c72016-06-21 12:28:20 -04002226 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04002227 return;
2228
Vivien Didelotfad09c72016-06-21 12:28:20 -04002229 mutex_lock(&chip->reg_lock);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002230
Vivien Didelot4d5770b2016-04-06 11:55:05 -04002231 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002232 if (_mv88e6xxx_port_vlan_add(chip, port, vid, untagged))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002233 netdev_err(ds->ports[port].netdev,
2234 "failed to add VLAN %d%c\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04002235 vid, untagged ? 'u' : 't');
Vivien Didelot76e398a2015-11-01 12:33:55 -05002236
Vivien Didelotfad09c72016-06-21 12:28:20 -04002237 if (pvid && _mv88e6xxx_port_pvid_set(chip, port, vlan->vid_end))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002238 netdev_err(ds->ports[port].netdev, "failed to set PVID %d\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04002239 vlan->vid_end);
2240
Vivien Didelotfad09c72016-06-21 12:28:20 -04002241 mutex_unlock(&chip->reg_lock);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002242}
2243
Vivien Didelotfad09c72016-06-21 12:28:20 -04002244static int _mv88e6xxx_port_vlan_del(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04002245 int port, u16 vid)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002246{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002247 struct dsa_switch *ds = chip->ds;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002248 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002249 int i, err;
2250
Vivien Didelotfad09c72016-06-21 12:28:20 -04002251 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002252 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002253 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002254
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05002255 /* Tell switchdev if this VLAN is handled in software */
2256 if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
Vivien Didelot3c06f082016-02-05 14:04:39 -05002257 return -EOPNOTSUPP;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002258
2259 vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
2260
2261 /* keep the VLAN unless all ports are excluded */
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002262 vlan.valid = false;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002263 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelot3d131f02015-11-03 10:52:52 -05002264 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002265 continue;
2266
2267 if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002268 vlan.valid = true;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002269 break;
2270 }
2271 }
2272
Vivien Didelotfad09c72016-06-21 12:28:20 -04002273 err = _mv88e6xxx_vtu_loadpurge(chip, &vlan);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002274 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002275 return err;
2276
Vivien Didelotfad09c72016-06-21 12:28:20 -04002277 return _mv88e6xxx_atu_remove(chip, vlan.fid, port, false);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002278}
2279
Vivien Didelotf81ec902016-05-09 13:22:58 -04002280static int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
2281 const struct switchdev_obj_port_vlan *vlan)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002282{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002283 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002284 u16 pvid, vid;
2285 int err = 0;
2286
Vivien Didelotfad09c72016-06-21 12:28:20 -04002287 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04002288 return -EOPNOTSUPP;
2289
Vivien Didelotfad09c72016-06-21 12:28:20 -04002290 mutex_lock(&chip->reg_lock);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002291
Vivien Didelotfad09c72016-06-21 12:28:20 -04002292 err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002293 if (err)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002294 goto unlock;
2295
Vivien Didelot76e398a2015-11-01 12:33:55 -05002296 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002297 err = _mv88e6xxx_port_vlan_del(chip, port, vid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002298 if (err)
2299 goto unlock;
2300
2301 if (vid == pvid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002302 err = _mv88e6xxx_port_pvid_set(chip, port, 0);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002303 if (err)
2304 goto unlock;
2305 }
2306 }
2307
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002308unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002309 mutex_unlock(&chip->reg_lock);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002310
2311 return err;
2312}
2313
Vivien Didelotfad09c72016-06-21 12:28:20 -04002314static int _mv88e6xxx_atu_mac_write(struct mv88e6xxx_chip *chip,
Vivien Didelotc5723ac2015-08-10 09:09:48 -04002315 const unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002316{
2317 int i, ret;
2318
2319 for (i = 0; i < 3; i++) {
Andrew Lunncca8b132015-04-02 04:06:39 +02002320 ret = _mv88e6xxx_reg_write(
Vivien Didelotfad09c72016-06-21 12:28:20 -04002321 chip, REG_GLOBAL, GLOBAL_ATU_MAC_01 + i,
Andrew Lunncca8b132015-04-02 04:06:39 +02002322 (addr[i * 2] << 8) | addr[i * 2 + 1]);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002323 if (ret < 0)
2324 return ret;
2325 }
2326
2327 return 0;
2328}
2329
Vivien Didelotfad09c72016-06-21 12:28:20 -04002330static int _mv88e6xxx_atu_mac_read(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04002331 unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002332{
2333 int i, ret;
2334
2335 for (i = 0; i < 3; i++) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002336 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
Andrew Lunncca8b132015-04-02 04:06:39 +02002337 GLOBAL_ATU_MAC_01 + i);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002338 if (ret < 0)
2339 return ret;
2340 addr[i * 2] = ret >> 8;
2341 addr[i * 2 + 1] = ret & 0xff;
2342 }
2343
2344 return 0;
2345}
2346
Vivien Didelotfad09c72016-06-21 12:28:20 -04002347static int _mv88e6xxx_atu_load(struct mv88e6xxx_chip *chip,
Vivien Didelotfd231c82015-08-10 09:09:50 -04002348 struct mv88e6xxx_atu_entry *entry)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002349{
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002350 int ret;
2351
Vivien Didelotfad09c72016-06-21 12:28:20 -04002352 ret = _mv88e6xxx_atu_wait(chip);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002353 if (ret < 0)
2354 return ret;
2355
Vivien Didelotfad09c72016-06-21 12:28:20 -04002356 ret = _mv88e6xxx_atu_mac_write(chip, entry->mac);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002357 if (ret < 0)
2358 return ret;
2359
Vivien Didelotfad09c72016-06-21 12:28:20 -04002360 ret = _mv88e6xxx_atu_data_write(chip, entry);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002361 if (ret < 0)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002362 return ret;
2363
Vivien Didelotfad09c72016-06-21 12:28:20 -04002364 return _mv88e6xxx_atu_cmd(chip, entry->fid, GLOBAL_ATU_OP_LOAD_DB);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002365}
David S. Millercdf09692015-08-11 12:00:37 -07002366
Vivien Didelotfad09c72016-06-21 12:28:20 -04002367static int _mv88e6xxx_port_fdb_load(struct mv88e6xxx_chip *chip, int port,
Vivien Didelotfd231c82015-08-10 09:09:50 -04002368 const unsigned char *addr, u16 vid,
2369 u8 state)
2370{
2371 struct mv88e6xxx_atu_entry entry = { 0 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002372 struct mv88e6xxx_vtu_stu_entry vlan;
2373 int err;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002374
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002375 /* Null VLAN ID corresponds to the port private database */
2376 if (vid == 0)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002377 err = _mv88e6xxx_port_fid_get(chip, port, &vlan.fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002378 else
Vivien Didelotfad09c72016-06-21 12:28:20 -04002379 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002380 if (err)
2381 return err;
2382
2383 entry.fid = vlan.fid;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002384 entry.state = state;
2385 ether_addr_copy(entry.mac, addr);
2386 if (state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2387 entry.trunk = false;
2388 entry.portv_trunkid = BIT(port);
2389 }
2390
Vivien Didelotfad09c72016-06-21 12:28:20 -04002391 return _mv88e6xxx_atu_load(chip, &entry);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002392}
2393
Vivien Didelotf81ec902016-05-09 13:22:58 -04002394static int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
2395 const struct switchdev_obj_port_fdb *fdb,
2396 struct switchdev_trans *trans)
Vivien Didelot146a3202015-10-08 11:35:12 -04002397{
2398 /* We don't need any dynamic resource from the kernel (yet),
2399 * so skip the prepare phase.
2400 */
2401 return 0;
2402}
2403
Vivien Didelotf81ec902016-05-09 13:22:58 -04002404static void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
2405 const struct switchdev_obj_port_fdb *fdb,
2406 struct switchdev_trans *trans)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002407{
Vivien Didelot1f36faf2015-10-08 11:35:13 -04002408 int state = is_multicast_ether_addr(fdb->addr) ?
David S. Millercdf09692015-08-11 12:00:37 -07002409 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2410 GLOBAL_ATU_DATA_STATE_UC_STATIC;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002411 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelot6630e232015-08-06 01:44:07 -04002412
Vivien Didelotfad09c72016-06-21 12:28:20 -04002413 mutex_lock(&chip->reg_lock);
2414 if (_mv88e6xxx_port_fdb_load(chip, port, fdb->addr, fdb->vid, state))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002415 netdev_err(ds->ports[port].netdev,
2416 "failed to load MAC address\n");
Vivien Didelotfad09c72016-06-21 12:28:20 -04002417 mutex_unlock(&chip->reg_lock);
David S. Millercdf09692015-08-11 12:00:37 -07002418}
2419
Vivien Didelotf81ec902016-05-09 13:22:58 -04002420static int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
2421 const struct switchdev_obj_port_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -07002422{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002423 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
David S. Millercdf09692015-08-11 12:00:37 -07002424 int ret;
2425
Vivien Didelotfad09c72016-06-21 12:28:20 -04002426 mutex_lock(&chip->reg_lock);
2427 ret = _mv88e6xxx_port_fdb_load(chip, port, fdb->addr, fdb->vid,
David S. Millercdf09692015-08-11 12:00:37 -07002428 GLOBAL_ATU_DATA_STATE_UNUSED);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002429 mutex_unlock(&chip->reg_lock);
David S. Millercdf09692015-08-11 12:00:37 -07002430
2431 return ret;
2432}
2433
Vivien Didelotfad09c72016-06-21 12:28:20 -04002434static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
Vivien Didelot1d194042015-08-10 09:09:51 -04002435 struct mv88e6xxx_atu_entry *entry)
David S. Millercdf09692015-08-11 12:00:37 -07002436{
Vivien Didelot1d194042015-08-10 09:09:51 -04002437 struct mv88e6xxx_atu_entry next = { 0 };
2438 int ret;
2439
2440 next.fid = fid;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002441
Vivien Didelotfad09c72016-06-21 12:28:20 -04002442 ret = _mv88e6xxx_atu_wait(chip);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002443 if (ret < 0)
2444 return ret;
2445
Vivien Didelotfad09c72016-06-21 12:28:20 -04002446 ret = _mv88e6xxx_atu_cmd(chip, fid, GLOBAL_ATU_OP_GET_NEXT_DB);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002447 if (ret < 0)
2448 return ret;
2449
Vivien Didelotfad09c72016-06-21 12:28:20 -04002450 ret = _mv88e6xxx_atu_mac_read(chip, next.mac);
Vivien Didelot1d194042015-08-10 09:09:51 -04002451 if (ret < 0)
2452 return ret;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002453
Vivien Didelotfad09c72016-06-21 12:28:20 -04002454 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_ATU_DATA);
Vivien Didelot1d194042015-08-10 09:09:51 -04002455 if (ret < 0)
2456 return ret;
2457
2458 next.state = ret & GLOBAL_ATU_DATA_STATE_MASK;
2459 if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2460 unsigned int mask, shift;
2461
2462 if (ret & GLOBAL_ATU_DATA_TRUNK) {
2463 next.trunk = true;
2464 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
2465 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
2466 } else {
2467 next.trunk = false;
2468 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
2469 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
2470 }
2471
2472 next.portv_trunkid = (ret & mask) >> shift;
2473 }
2474
2475 *entry = next;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002476 return 0;
2477}
2478
Vivien Didelotfad09c72016-06-21 12:28:20 -04002479static int _mv88e6xxx_port_fdb_dump_one(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04002480 u16 fid, u16 vid, int port,
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002481 struct switchdev_obj_port_fdb *fdb,
2482 int (*cb)(struct switchdev_obj *obj))
2483{
2484 struct mv88e6xxx_atu_entry addr = {
2485 .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2486 };
2487 int err;
2488
Vivien Didelotfad09c72016-06-21 12:28:20 -04002489 err = _mv88e6xxx_atu_mac_write(chip, addr.mac);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002490 if (err)
2491 return err;
2492
2493 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002494 err = _mv88e6xxx_atu_getnext(chip, fid, &addr);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002495 if (err)
2496 break;
2497
2498 if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2499 break;
2500
2501 if (!addr.trunk && addr.portv_trunkid & BIT(port)) {
2502 bool is_static = addr.state ==
2503 (is_multicast_ether_addr(addr.mac) ?
2504 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2505 GLOBAL_ATU_DATA_STATE_UC_STATIC);
2506
2507 fdb->vid = vid;
2508 ether_addr_copy(fdb->addr, addr.mac);
2509 fdb->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
2510
2511 err = cb(&fdb->obj);
2512 if (err)
2513 break;
2514 }
2515 } while (!is_broadcast_ether_addr(addr.mac));
2516
2517 return err;
2518}
2519
Vivien Didelotf81ec902016-05-09 13:22:58 -04002520static int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
2521 struct switchdev_obj_port_fdb *fdb,
2522 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotf33475b2015-10-22 09:34:41 -04002523{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002524 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002525 struct mv88e6xxx_vtu_stu_entry vlan = {
2526 .vid = GLOBAL_VTU_VID_MASK, /* all ones */
2527 };
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002528 u16 fid;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002529 int err;
2530
Vivien Didelotfad09c72016-06-21 12:28:20 -04002531 mutex_lock(&chip->reg_lock);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002532
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002533 /* Dump port's default Filtering Information Database (VLAN ID 0) */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002534 err = _mv88e6xxx_port_fid_get(chip, port, &fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002535 if (err)
2536 goto unlock;
2537
Vivien Didelotfad09c72016-06-21 12:28:20 -04002538 err = _mv88e6xxx_port_fdb_dump_one(chip, fid, 0, port, fdb, cb);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002539 if (err)
2540 goto unlock;
2541
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002542 /* Dump VLANs' Filtering Information Databases */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002543 err = _mv88e6xxx_vtu_vid_write(chip, vlan.vid);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002544 if (err)
2545 goto unlock;
2546
2547 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002548 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002549 if (err)
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002550 break;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002551
2552 if (!vlan.valid)
2553 break;
2554
Vivien Didelotfad09c72016-06-21 12:28:20 -04002555 err = _mv88e6xxx_port_fdb_dump_one(chip, vlan.fid, vlan.vid,
2556 port, fdb, cb);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002557 if (err)
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002558 break;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002559 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
2560
2561unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002562 mutex_unlock(&chip->reg_lock);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002563
2564 return err;
2565}
2566
Vivien Didelotf81ec902016-05-09 13:22:58 -04002567static int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
2568 struct net_device *bridge)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002569{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002570 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Colin Ian King1d9619d2016-04-25 23:11:22 +01002571 int i, err = 0;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002572
Vivien Didelotfad09c72016-06-21 12:28:20 -04002573 mutex_lock(&chip->reg_lock);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002574
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002575 /* Assign the bridge and remap each port's VLANTable */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002576 chip->ports[port].bridge_dev = bridge;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002577
Vivien Didelotfad09c72016-06-21 12:28:20 -04002578 for (i = 0; i < chip->info->num_ports; ++i) {
2579 if (chip->ports[i].bridge_dev == bridge) {
2580 err = _mv88e6xxx_port_based_vlan_map(chip, i);
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002581 if (err)
2582 break;
2583 }
2584 }
2585
Vivien Didelotfad09c72016-06-21 12:28:20 -04002586 mutex_unlock(&chip->reg_lock);
Vivien Didelota6692752016-02-12 12:09:39 -05002587
Vivien Didelot466dfa02016-02-26 13:16:05 -05002588 return err;
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002589}
2590
Vivien Didelotf81ec902016-05-09 13:22:58 -04002591static void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002592{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002593 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2594 struct net_device *bridge = chip->ports[port].bridge_dev;
Vivien Didelot16bfa702016-03-13 16:21:33 -04002595 int i;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002596
Vivien Didelotfad09c72016-06-21 12:28:20 -04002597 mutex_lock(&chip->reg_lock);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002598
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002599 /* Unassign the bridge and remap each port's VLANTable */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002600 chip->ports[port].bridge_dev = NULL;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002601
Vivien Didelotfad09c72016-06-21 12:28:20 -04002602 for (i = 0; i < chip->info->num_ports; ++i)
2603 if (i == port || chip->ports[i].bridge_dev == bridge)
2604 if (_mv88e6xxx_port_based_vlan_map(chip, i))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002605 netdev_warn(ds->ports[i].netdev,
2606 "failed to remap\n");
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002607
Vivien Didelotfad09c72016-06-21 12:28:20 -04002608 mutex_unlock(&chip->reg_lock);
Vivien Didelot66d9cd02016-02-05 14:07:14 -05002609}
2610
Vivien Didelotfad09c72016-06-21 12:28:20 -04002611static int _mv88e6xxx_mdio_page_write(struct mv88e6xxx_chip *chip,
Andrew Lunn03a4a542016-06-04 21:17:05 +02002612 int port, int page, int reg, int val)
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002613{
2614 int ret;
2615
Vivien Didelotfad09c72016-06-21 12:28:20 -04002616 ret = mv88e6xxx_mdio_write_indirect(chip, port, 0x16, page);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002617 if (ret < 0)
2618 goto restore_page_0;
2619
Vivien Didelotfad09c72016-06-21 12:28:20 -04002620 ret = mv88e6xxx_mdio_write_indirect(chip, port, reg, val);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002621restore_page_0:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002622 mv88e6xxx_mdio_write_indirect(chip, port, 0x16, 0x0);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002623
2624 return ret;
2625}
2626
Vivien Didelotfad09c72016-06-21 12:28:20 -04002627static int _mv88e6xxx_mdio_page_read(struct mv88e6xxx_chip *chip,
Andrew Lunn03a4a542016-06-04 21:17:05 +02002628 int port, int page, int reg)
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002629{
2630 int ret;
2631
Vivien Didelotfad09c72016-06-21 12:28:20 -04002632 ret = mv88e6xxx_mdio_write_indirect(chip, port, 0x16, page);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002633 if (ret < 0)
2634 goto restore_page_0;
2635
Vivien Didelotfad09c72016-06-21 12:28:20 -04002636 ret = mv88e6xxx_mdio_read_indirect(chip, port, reg);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002637restore_page_0:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002638 mv88e6xxx_mdio_write_indirect(chip, port, 0x16, 0x0);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002639
2640 return ret;
2641}
2642
Vivien Didelotfad09c72016-06-21 12:28:20 -04002643static int mv88e6xxx_switch_reset(struct mv88e6xxx_chip *chip)
Vivien Didelot552238b2016-05-09 13:22:49 -04002644{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002645 bool ppu_active = mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE);
Vivien Didelot552238b2016-05-09 13:22:49 -04002646 u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002647 struct gpio_desc *gpiod = chip->reset;
Vivien Didelot552238b2016-05-09 13:22:49 -04002648 unsigned long timeout;
2649 int ret;
2650 int i;
2651
2652 /* Set all ports to the disabled state. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002653 for (i = 0; i < chip->info->num_ports; i++) {
2654 ret = _mv88e6xxx_reg_read(chip, REG_PORT(i), PORT_CONTROL);
Vivien Didelot552238b2016-05-09 13:22:49 -04002655 if (ret < 0)
2656 return ret;
2657
Vivien Didelotfad09c72016-06-21 12:28:20 -04002658 ret = _mv88e6xxx_reg_write(chip, REG_PORT(i), PORT_CONTROL,
Vivien Didelot552238b2016-05-09 13:22:49 -04002659 ret & 0xfffc);
2660 if (ret)
2661 return ret;
2662 }
2663
2664 /* Wait for transmit queues to drain. */
2665 usleep_range(2000, 4000);
2666
2667 /* If there is a gpio connected to the reset pin, toggle it */
2668 if (gpiod) {
2669 gpiod_set_value_cansleep(gpiod, 1);
2670 usleep_range(10000, 20000);
2671 gpiod_set_value_cansleep(gpiod, 0);
2672 usleep_range(10000, 20000);
2673 }
2674
2675 /* Reset the switch. Keep the PPU active if requested. The PPU
2676 * needs to be active to support indirect phy register access
2677 * through global registers 0x18 and 0x19.
2678 */
2679 if (ppu_active)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002680 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, 0x04, 0xc000);
Vivien Didelot552238b2016-05-09 13:22:49 -04002681 else
Vivien Didelotfad09c72016-06-21 12:28:20 -04002682 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, 0x04, 0xc400);
Vivien Didelot552238b2016-05-09 13:22:49 -04002683 if (ret)
2684 return ret;
2685
2686 /* Wait up to one second for reset to complete. */
2687 timeout = jiffies + 1 * HZ;
2688 while (time_before(jiffies, timeout)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002689 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, 0x00);
Vivien Didelot552238b2016-05-09 13:22:49 -04002690 if (ret < 0)
2691 return ret;
2692
2693 if ((ret & is_reset) == is_reset)
2694 break;
2695 usleep_range(1000, 2000);
2696 }
2697 if (time_after(jiffies, timeout))
2698 ret = -ETIMEDOUT;
2699 else
2700 ret = 0;
2701
2702 return ret;
2703}
2704
Vivien Didelotfad09c72016-06-21 12:28:20 -04002705static int mv88e6xxx_power_on_serdes(struct mv88e6xxx_chip *chip)
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002706{
2707 int ret;
2708
Vivien Didelotfad09c72016-06-21 12:28:20 -04002709 ret = _mv88e6xxx_mdio_page_read(chip, REG_FIBER_SERDES,
Andrew Lunn03a4a542016-06-04 21:17:05 +02002710 PAGE_FIBER_SERDES, MII_BMCR);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002711 if (ret < 0)
2712 return ret;
2713
2714 if (ret & BMCR_PDOWN) {
2715 ret &= ~BMCR_PDOWN;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002716 ret = _mv88e6xxx_mdio_page_write(chip, REG_FIBER_SERDES,
Andrew Lunn03a4a542016-06-04 21:17:05 +02002717 PAGE_FIBER_SERDES, MII_BMCR,
2718 ret);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002719 }
2720
2721 return ret;
2722}
2723
Vivien Didelotfad09c72016-06-21 12:28:20 -04002724static int mv88e6xxx_setup_port(struct mv88e6xxx_chip *chip, int port)
Guenter Roeckd827e882015-03-26 18:36:29 -07002725{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002726 struct dsa_switch *ds = chip->ds;
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002727 int ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002728 u16 reg;
Guenter Roeckd827e882015-03-26 18:36:29 -07002729
Vivien Didelotfad09c72016-06-21 12:28:20 -04002730 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2731 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2732 mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip) ||
2733 mv88e6xxx_6065_family(chip) || mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002734 /* MAC Forcing register: don't force link, speed,
2735 * duplex or flow control state to any particular
2736 * values on physical ports, but force the CPU port
2737 * and all DSA ports to their maximum bandwidth and
2738 * full duplex.
2739 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002740 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_PCS_CTRL);
Andrew Lunn60045cb2015-08-17 23:52:51 +02002741 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
Russell King53adc9e2015-09-21 21:42:59 +01002742 reg &= ~PORT_PCS_CTRL_UNFORCED;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002743 reg |= PORT_PCS_CTRL_FORCE_LINK |
2744 PORT_PCS_CTRL_LINK_UP |
2745 PORT_PCS_CTRL_DUPLEX_FULL |
2746 PORT_PCS_CTRL_FORCE_DUPLEX;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002747 if (mv88e6xxx_6065_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002748 reg |= PORT_PCS_CTRL_100;
2749 else
2750 reg |= PORT_PCS_CTRL_1000;
2751 } else {
2752 reg |= PORT_PCS_CTRL_UNFORCED;
2753 }
2754
Vivien Didelotfad09c72016-06-21 12:28:20 -04002755 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002756 PORT_PCS_CTRL, reg);
2757 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002758 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002759 }
2760
2761 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
2762 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
2763 * tunneling, determine priority by looking at 802.1p and IP
2764 * priority fields (IP prio has precedence), and set STP state
2765 * to Forwarding.
2766 *
2767 * If this is the CPU link, use DSA or EDSA tagging depending
2768 * on which tagging mode was configured.
2769 *
2770 * If this is a link to another switch, use DSA tagging mode.
2771 *
2772 * If this is the upstream port for this switch, enable
2773 * forwarding of unknown unicasts and multicasts.
2774 */
2775 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002776 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2777 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2778 mv88e6xxx_6095_family(chip) || mv88e6xxx_6065_family(chip) ||
2779 mv88e6xxx_6185_family(chip) || mv88e6xxx_6320_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002780 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
2781 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
2782 PORT_CONTROL_STATE_FORWARDING;
2783 if (dsa_is_cpu_port(ds, port)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002784 if (mv88e6xxx_6095_family(chip) || mv88e6xxx_6185_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002785 reg |= PORT_CONTROL_DSA_TAG;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002786 if (mv88e6xxx_6352_family(chip) ||
2787 mv88e6xxx_6351_family(chip) ||
2788 mv88e6xxx_6165_family(chip) ||
2789 mv88e6xxx_6097_family(chip) ||
2790 mv88e6xxx_6320_family(chip)) {
Andrew Lunn5377b802016-06-04 21:17:02 +02002791 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA |
2792 PORT_CONTROL_FORWARD_UNKNOWN |
Andrew Lunnc047a1f2015-09-29 01:50:56 +02002793 PORT_CONTROL_FORWARD_UNKNOWN_MC;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002794 }
2795
Vivien Didelotfad09c72016-06-21 12:28:20 -04002796 if (mv88e6xxx_6352_family(chip) ||
2797 mv88e6xxx_6351_family(chip) ||
2798 mv88e6xxx_6165_family(chip) ||
2799 mv88e6xxx_6097_family(chip) ||
2800 mv88e6xxx_6095_family(chip) ||
2801 mv88e6xxx_6065_family(chip) ||
2802 mv88e6xxx_6185_family(chip) ||
2803 mv88e6xxx_6320_family(chip)) {
Vivien Didelot57d32312016-06-20 13:13:58 -04002804 reg |= PORT_CONTROL_EGRESS_ADD_TAG;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002805 }
2806 }
Andrew Lunn6083ce72015-08-17 23:52:52 +02002807 if (dsa_is_dsa_port(ds, port)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002808 if (mv88e6xxx_6095_family(chip) ||
2809 mv88e6xxx_6185_family(chip))
Andrew Lunn6083ce72015-08-17 23:52:52 +02002810 reg |= PORT_CONTROL_DSA_TAG;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002811 if (mv88e6xxx_6352_family(chip) ||
2812 mv88e6xxx_6351_family(chip) ||
2813 mv88e6xxx_6165_family(chip) ||
2814 mv88e6xxx_6097_family(chip) ||
2815 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002816 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunn6083ce72015-08-17 23:52:52 +02002817 }
2818
Andrew Lunn54d792f2015-05-06 01:09:47 +02002819 if (port == dsa_upstream_port(ds))
2820 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2821 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2822 }
2823 if (reg) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002824 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002825 PORT_CONTROL, reg);
2826 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002827 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002828 }
2829
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002830 /* If this port is connected to a SerDes, make sure the SerDes is not
2831 * powered down.
2832 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002833 if (mv88e6xxx_6352_family(chip)) {
2834 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_STATUS);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002835 if (ret < 0)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002836 return ret;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002837 ret &= PORT_STATUS_CMODE_MASK;
2838 if ((ret == PORT_STATUS_CMODE_100BASE_X) ||
2839 (ret == PORT_STATUS_CMODE_1000BASE_X) ||
2840 (ret == PORT_STATUS_CMODE_SGMII)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002841 ret = mv88e6xxx_power_on_serdes(chip);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002842 if (ret < 0)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002843 return ret;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002844 }
2845 }
2846
Vivien Didelot8efdda42015-08-13 12:52:23 -04002847 /* Port Control 2: don't force a good FCS, set the maximum frame size to
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002848 * 10240 bytes, disable 802.1q tags checking, don't discard tagged or
Vivien Didelot8efdda42015-08-13 12:52:23 -04002849 * untagged frames on this port, do a destination address lookup on all
2850 * received packets as usual, disable ARP mirroring and don't send a
2851 * copy of all transmitted/received frames on this port to the CPU.
Andrew Lunn54d792f2015-05-06 01:09:47 +02002852 */
2853 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002854 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2855 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2856 mv88e6xxx_6095_family(chip) || mv88e6xxx_6320_family(chip) ||
2857 mv88e6xxx_6185_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002858 reg = PORT_CONTROL_2_MAP_DA;
2859
Vivien Didelotfad09c72016-06-21 12:28:20 -04002860 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2861 mv88e6xxx_6165_family(chip) || mv88e6xxx_6320_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002862 reg |= PORT_CONTROL_2_JUMBO_10240;
2863
Vivien Didelotfad09c72016-06-21 12:28:20 -04002864 if (mv88e6xxx_6095_family(chip) || mv88e6xxx_6185_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002865 /* Set the upstream port this port should use */
2866 reg |= dsa_upstream_port(ds);
2867 /* enable forwarding of unknown multicast addresses to
2868 * the upstream port
2869 */
2870 if (port == dsa_upstream_port(ds))
2871 reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
2872 }
2873
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002874 reg |= PORT_CONTROL_2_8021Q_DISABLED;
Vivien Didelot8efdda42015-08-13 12:52:23 -04002875
Andrew Lunn54d792f2015-05-06 01:09:47 +02002876 if (reg) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002877 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002878 PORT_CONTROL_2, reg);
2879 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002880 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002881 }
2882
2883 /* Port Association Vector: when learning source addresses
2884 * of packets, add the address to the address database using
2885 * a port bitmap that has only the bit for this port set and
2886 * the other bits clear.
2887 */
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002888 reg = 1 << port;
Vivien Didelot996ecb82016-04-14 14:42:08 -04002889 /* Disable learning for CPU port */
2890 if (dsa_is_cpu_port(ds, port))
Vivien Didelot65fa4022016-04-14 14:42:07 -04002891 reg = 0;
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002892
Vivien Didelotfad09c72016-06-21 12:28:20 -04002893 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_ASSOC_VECTOR,
2894 reg);
Andrew Lunn54d792f2015-05-06 01:09:47 +02002895 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002896 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002897
2898 /* Egress rate control 2: disable egress rate control. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002899 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_RATE_CONTROL_2,
Andrew Lunn54d792f2015-05-06 01:09:47 +02002900 0x0000);
2901 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002902 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002903
Vivien Didelotfad09c72016-06-21 12:28:20 -04002904 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2905 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2906 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002907 /* Do not limit the period of time that this port can
2908 * be paused for by the remote end or the period of
2909 * time that this port can pause the remote end.
2910 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002911 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002912 PORT_PAUSE_CTRL, 0x0000);
2913 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002914 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002915
2916 /* Port ATU control: disable limiting the number of
2917 * address database entries that this port is allowed
2918 * to use.
2919 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002920 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002921 PORT_ATU_CONTROL, 0x0000);
2922 /* Priority Override: disable DA, SA and VTU priority
2923 * override.
2924 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002925 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002926 PORT_PRI_OVERRIDE, 0x0000);
2927 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002928 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002929
2930 /* Port Ethertype: use the Ethertype DSA Ethertype
2931 * value.
2932 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002933 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002934 PORT_ETH_TYPE, ETH_P_EDSA);
2935 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002936 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002937 /* Tag Remap: use an identity 802.1p prio -> switch
2938 * prio mapping.
2939 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002940 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002941 PORT_TAG_REGMAP_0123, 0x3210);
2942 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002943 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002944
2945 /* Tag Remap 2: use an identity 802.1p prio -> switch
2946 * prio mapping.
2947 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002948 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002949 PORT_TAG_REGMAP_4567, 0x7654);
2950 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002951 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002952 }
2953
Vivien Didelotfad09c72016-06-21 12:28:20 -04002954 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2955 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2956 mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip) ||
2957 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002958 /* Rate Control: disable ingress rate limiting. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002959 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002960 PORT_RATE_CONTROL, 0x0001);
2961 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002962 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002963 }
2964
Guenter Roeck366f0a02015-03-26 18:36:30 -07002965 /* Port Control 1: disable trunking, disable sending
2966 * learning messages to this port.
Guenter Roeckd827e882015-03-26 18:36:29 -07002967 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002968 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_1,
2969 0x0000);
Guenter Roeckd827e882015-03-26 18:36:29 -07002970 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002971 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07002972
Vivien Didelot207afda2016-04-14 14:42:09 -04002973 /* Port based VLAN map: give each port the same default address
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002974 * database, and allow bidirectional communication between the
2975 * CPU and DSA port(s), and the other ports.
Guenter Roeckd827e882015-03-26 18:36:29 -07002976 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002977 ret = _mv88e6xxx_port_fid_set(chip, port, 0);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002978 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002979 return ret;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002980
Vivien Didelotfad09c72016-06-21 12:28:20 -04002981 ret = _mv88e6xxx_port_based_vlan_map(chip, port);
Guenter Roeckd827e882015-03-26 18:36:29 -07002982 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002983 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07002984
2985 /* Default VLAN ID and priority: don't set a default VLAN
2986 * ID, and set the default packet priority to zero.
2987 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002988 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_DEFAULT_VLAN,
Vivien Didelot47cf1e652015-04-20 17:43:26 -04002989 0x0000);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002990 if (ret)
2991 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07002992
Andrew Lunndbde9e62015-05-06 01:09:48 +02002993 return 0;
2994}
2995
Vivien Didelotfad09c72016-06-21 12:28:20 -04002996static int mv88e6xxx_setup_global(struct mv88e6xxx_chip *chip)
Vivien Didelot08a01262016-05-09 13:22:50 -04002997{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002998 struct dsa_switch *ds = chip->ds;
Vivien Didelotb0745e872016-05-09 13:22:53 -04002999 u32 upstream_port = dsa_upstream_port(ds);
Vivien Didelot119477b2016-05-09 13:22:51 -04003000 u16 reg;
Vivien Didelot08a01262016-05-09 13:22:50 -04003001 int err;
3002 int i;
3003
Vivien Didelot119477b2016-05-09 13:22:51 -04003004 /* Enable the PHY Polling Unit if present, don't discard any packets,
3005 * and mask all interrupt sources.
3006 */
3007 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003008 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU) ||
3009 mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE))
Vivien Didelot119477b2016-05-09 13:22:51 -04003010 reg |= GLOBAL_CONTROL_PPU_ENABLE;
3011
Vivien Didelotfad09c72016-06-21 12:28:20 -04003012 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL, reg);
Vivien Didelot119477b2016-05-09 13:22:51 -04003013 if (err)
3014 return err;
3015
Vivien Didelotb0745e872016-05-09 13:22:53 -04003016 /* Configure the upstream port, and configure it as the port to which
3017 * ingress and egress and ARP monitor frames are to be sent.
3018 */
3019 reg = upstream_port << GLOBAL_MONITOR_CONTROL_INGRESS_SHIFT |
3020 upstream_port << GLOBAL_MONITOR_CONTROL_EGRESS_SHIFT |
3021 upstream_port << GLOBAL_MONITOR_CONTROL_ARP_SHIFT;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003022 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_MONITOR_CONTROL,
3023 reg);
Vivien Didelotb0745e872016-05-09 13:22:53 -04003024 if (err)
3025 return err;
3026
Vivien Didelot50484ff2016-05-09 13:22:54 -04003027 /* Disable remote management, and set the switch's DSA device number. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003028 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL_2,
Vivien Didelot50484ff2016-05-09 13:22:54 -04003029 GLOBAL_CONTROL_2_MULTIPLE_CASCADE |
3030 (ds->index & 0x1f));
3031 if (err)
3032 return err;
3033
Vivien Didelot08a01262016-05-09 13:22:50 -04003034 /* Set the default address aging time to 5 minutes, and
3035 * enable address learn messages to be sent to all message
3036 * ports.
3037 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003038 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL,
Vivien Didelot08a01262016-05-09 13:22:50 -04003039 0x0140 | GLOBAL_ATU_CONTROL_LEARN2ALL);
3040 if (err)
3041 return err;
3042
3043 /* Configure the IP ToS mapping registers. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003044 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_0, 0x0000);
Vivien Didelot08a01262016-05-09 13:22:50 -04003045 if (err)
3046 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003047 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_1, 0x0000);
Vivien Didelot08a01262016-05-09 13:22:50 -04003048 if (err)
3049 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003050 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_2, 0x5555);
Vivien Didelot08a01262016-05-09 13:22:50 -04003051 if (err)
3052 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003053 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_3, 0x5555);
Vivien Didelot08a01262016-05-09 13:22:50 -04003054 if (err)
3055 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003056 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_4, 0xaaaa);
Vivien Didelot08a01262016-05-09 13:22:50 -04003057 if (err)
3058 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003059 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_5, 0xaaaa);
Vivien Didelot08a01262016-05-09 13:22:50 -04003060 if (err)
3061 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003062 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_6, 0xffff);
Vivien Didelot08a01262016-05-09 13:22:50 -04003063 if (err)
3064 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003065 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_7, 0xffff);
Vivien Didelot08a01262016-05-09 13:22:50 -04003066 if (err)
3067 return err;
3068
3069 /* Configure the IEEE 802.1p priority mapping register. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003070 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IEEE_PRI, 0xfa41);
Vivien Didelot08a01262016-05-09 13:22:50 -04003071 if (err)
3072 return err;
3073
3074 /* Send all frames with destination addresses matching
3075 * 01:80:c2:00:00:0x to the CPU port.
3076 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003077 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_MGMT_EN_0X,
3078 0xffff);
Vivien Didelot08a01262016-05-09 13:22:50 -04003079 if (err)
3080 return err;
3081
3082 /* Ignore removed tag data on doubly tagged packets, disable
3083 * flow control messages, force flow control priority to the
3084 * highest, and send all special multicast frames to the CPU
3085 * port at the highest priority.
3086 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003087 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_SWITCH_MGMT,
Vivien Didelot08a01262016-05-09 13:22:50 -04003088 0x7 | GLOBAL2_SWITCH_MGMT_RSVD2CPU | 0x70 |
3089 GLOBAL2_SWITCH_MGMT_FORCE_FLOW_CTRL_PRI);
3090 if (err)
3091 return err;
3092
3093 /* Program the DSA routing table. */
3094 for (i = 0; i < 32; i++) {
3095 int nexthop = 0x1f;
3096
Andrew Lunn66472fc2016-06-04 21:17:00 +02003097 if (i != ds->index && i < DSA_MAX_SWITCHES)
3098 nexthop = ds->rtable[i] & 0x1f;
Vivien Didelot08a01262016-05-09 13:22:50 -04003099
3100 err = _mv88e6xxx_reg_write(
Vivien Didelotfad09c72016-06-21 12:28:20 -04003101 chip, REG_GLOBAL2,
Vivien Didelot08a01262016-05-09 13:22:50 -04003102 GLOBAL2_DEVICE_MAPPING,
3103 GLOBAL2_DEVICE_MAPPING_UPDATE |
3104 (i << GLOBAL2_DEVICE_MAPPING_TARGET_SHIFT) | nexthop);
3105 if (err)
3106 return err;
3107 }
3108
3109 /* Clear all trunk masks. */
3110 for (i = 0; i < 8; i++) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04003111 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL2,
3112 GLOBAL2_TRUNK_MASK,
Vivien Didelot08a01262016-05-09 13:22:50 -04003113 0x8000 |
3114 (i << GLOBAL2_TRUNK_MASK_NUM_SHIFT) |
Vivien Didelotfad09c72016-06-21 12:28:20 -04003115 ((1 << chip->info->num_ports) - 1));
Vivien Didelot08a01262016-05-09 13:22:50 -04003116 if (err)
3117 return err;
3118 }
3119
3120 /* Clear all trunk mappings. */
3121 for (i = 0; i < 16; i++) {
3122 err = _mv88e6xxx_reg_write(
Vivien Didelotfad09c72016-06-21 12:28:20 -04003123 chip, REG_GLOBAL2,
Vivien Didelot08a01262016-05-09 13:22:50 -04003124 GLOBAL2_TRUNK_MAPPING,
3125 GLOBAL2_TRUNK_MAPPING_UPDATE |
3126 (i << GLOBAL2_TRUNK_MAPPING_ID_SHIFT));
3127 if (err)
3128 return err;
3129 }
3130
Vivien Didelotfad09c72016-06-21 12:28:20 -04003131 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
3132 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
3133 mv88e6xxx_6320_family(chip)) {
Vivien Didelot08a01262016-05-09 13:22:50 -04003134 /* Send all frames with destination addresses matching
3135 * 01:80:c2:00:00:2x to the CPU port.
3136 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003137 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL2,
Vivien Didelot08a01262016-05-09 13:22:50 -04003138 GLOBAL2_MGMT_EN_2X, 0xffff);
3139 if (err)
3140 return err;
3141
3142 /* Initialise cross-chip port VLAN table to reset
3143 * defaults.
3144 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003145 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL2,
Vivien Didelot08a01262016-05-09 13:22:50 -04003146 GLOBAL2_PVT_ADDR, 0x9000);
3147 if (err)
3148 return err;
3149
3150 /* Clear the priority override table. */
3151 for (i = 0; i < 16; i++) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04003152 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL2,
Vivien Didelot08a01262016-05-09 13:22:50 -04003153 GLOBAL2_PRIO_OVERRIDE,
3154 0x8000 | (i << 8));
3155 if (err)
3156 return err;
3157 }
3158 }
3159
Vivien Didelotfad09c72016-06-21 12:28:20 -04003160 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
3161 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
3162 mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip) ||
3163 mv88e6xxx_6320_family(chip)) {
Vivien Didelot08a01262016-05-09 13:22:50 -04003164 /* Disable ingress rate limiting by resetting all
3165 * ingress rate limit registers to their initial
3166 * state.
3167 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003168 for (i = 0; i < chip->info->num_ports; i++) {
3169 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL2,
Vivien Didelot08a01262016-05-09 13:22:50 -04003170 GLOBAL2_INGRESS_OP,
3171 0x9000 | (i << 8));
3172 if (err)
3173 return err;
3174 }
3175 }
3176
3177 /* Clear the statistics counters for all ports */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003178 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
Vivien Didelot08a01262016-05-09 13:22:50 -04003179 GLOBAL_STATS_OP_FLUSH_ALL);
3180 if (err)
3181 return err;
3182
3183 /* Wait for the flush to complete. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003184 err = _mv88e6xxx_stats_wait(chip);
Vivien Didelot08a01262016-05-09 13:22:50 -04003185 if (err)
3186 return err;
3187
3188 /* Clear all ATU entries */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003189 err = _mv88e6xxx_atu_flush(chip, 0, true);
Vivien Didelot08a01262016-05-09 13:22:50 -04003190 if (err)
3191 return err;
3192
3193 /* Clear all the VTU and STU entries */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003194 err = _mv88e6xxx_vtu_stu_flush(chip);
Vivien Didelot08a01262016-05-09 13:22:50 -04003195 if (err < 0)
3196 return err;
3197
3198 return err;
3199}
3200
Vivien Didelotf81ec902016-05-09 13:22:58 -04003201static int mv88e6xxx_setup(struct dsa_switch *ds)
Guenter Roeckacdaffc2015-03-26 18:36:28 -07003202{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003203 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelot552238b2016-05-09 13:22:49 -04003204 int err;
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003205 int i;
3206
Vivien Didelotfad09c72016-06-21 12:28:20 -04003207 chip->ds = ds;
3208 ds->slave_mii_bus = chip->mdio_bus;
Vivien Didelot552238b2016-05-09 13:22:49 -04003209
Vivien Didelotfad09c72016-06-21 12:28:20 -04003210 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEPROM))
3211 mutex_init(&chip->eeprom_mutex);
Vivien Didelotd24645b2016-05-09 13:22:41 -04003212
Vivien Didelotfad09c72016-06-21 12:28:20 -04003213 mutex_lock(&chip->reg_lock);
Vivien Didelot552238b2016-05-09 13:22:49 -04003214
Vivien Didelotfad09c72016-06-21 12:28:20 -04003215 err = mv88e6xxx_switch_reset(chip);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003216 if (err)
3217 goto unlock;
Andrew Lunn54d792f2015-05-06 01:09:47 +02003218
Vivien Didelotfad09c72016-06-21 12:28:20 -04003219 err = mv88e6xxx_setup_global(chip);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003220 if (err)
3221 goto unlock;
3222
Vivien Didelotfad09c72016-06-21 12:28:20 -04003223 for (i = 0; i < chip->info->num_ports; i++) {
3224 err = mv88e6xxx_setup_port(chip, i);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003225 if (err)
3226 goto unlock;
3227 }
Andrew Lunn54d792f2015-05-06 01:09:47 +02003228
Vivien Didelot6b17e862015-08-13 12:52:18 -04003229unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04003230 mutex_unlock(&chip->reg_lock);
Andrew Lunndb687a52015-06-20 21:31:29 +02003231
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003232 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02003233}
3234
Vivien Didelot57d32312016-06-20 13:13:58 -04003235static int mv88e6xxx_mdio_page_read(struct dsa_switch *ds, int port, int page,
3236 int reg)
Andrew Lunn491435852015-04-02 04:06:35 +02003237{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003238 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunn491435852015-04-02 04:06:35 +02003239 int ret;
3240
Vivien Didelotfad09c72016-06-21 12:28:20 -04003241 mutex_lock(&chip->reg_lock);
3242 ret = _mv88e6xxx_mdio_page_read(chip, port, page, reg);
3243 mutex_unlock(&chip->reg_lock);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00003244
Andrew Lunn491435852015-04-02 04:06:35 +02003245 return ret;
3246}
3247
Vivien Didelot57d32312016-06-20 13:13:58 -04003248static int mv88e6xxx_mdio_page_write(struct dsa_switch *ds, int port, int page,
3249 int reg, int val)
Andrew Lunn491435852015-04-02 04:06:35 +02003250{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003251 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunn491435852015-04-02 04:06:35 +02003252 int ret;
3253
Vivien Didelotfad09c72016-06-21 12:28:20 -04003254 mutex_lock(&chip->reg_lock);
3255 ret = _mv88e6xxx_mdio_page_write(chip, port, page, reg, val);
3256 mutex_unlock(&chip->reg_lock);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00003257
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003258 return ret;
3259}
3260
Vivien Didelotfad09c72016-06-21 12:28:20 -04003261static int mv88e6xxx_port_to_mdio_addr(struct mv88e6xxx_chip *chip, int port)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003262{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003263 if (port >= 0 && port < chip->info->num_ports)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003264 return port;
3265 return -EINVAL;
3266}
3267
Andrew Lunnb516d452016-06-04 21:17:06 +02003268static int mv88e6xxx_mdio_read(struct mii_bus *bus, int port, int regnum)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003269{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003270 struct mv88e6xxx_chip *chip = bus->priv;
3271 int addr = mv88e6xxx_port_to_mdio_addr(chip, port);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003272 int ret;
3273
3274 if (addr < 0)
Andrew Lunn158bc062016-04-28 21:24:06 -04003275 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003276
Vivien Didelotfad09c72016-06-21 12:28:20 -04003277 mutex_lock(&chip->reg_lock);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003278
Vivien Didelotfad09c72016-06-21 12:28:20 -04003279 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU))
3280 ret = mv88e6xxx_mdio_read_ppu(chip, addr, regnum);
3281 else if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_SMI_PHY))
3282 ret = mv88e6xxx_mdio_read_indirect(chip, addr, regnum);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003283 else
Vivien Didelotfad09c72016-06-21 12:28:20 -04003284 ret = mv88e6xxx_mdio_read_direct(chip, addr, regnum);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003285
Vivien Didelotfad09c72016-06-21 12:28:20 -04003286 mutex_unlock(&chip->reg_lock);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003287 return ret;
3288}
3289
Andrew Lunnb516d452016-06-04 21:17:06 +02003290static int mv88e6xxx_mdio_write(struct mii_bus *bus, int port, int regnum,
Andrew Lunn03a4a542016-06-04 21:17:05 +02003291 u16 val)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003292{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003293 struct mv88e6xxx_chip *chip = bus->priv;
3294 int addr = mv88e6xxx_port_to_mdio_addr(chip, port);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003295 int ret;
3296
3297 if (addr < 0)
Andrew Lunn158bc062016-04-28 21:24:06 -04003298 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003299
Vivien Didelotfad09c72016-06-21 12:28:20 -04003300 mutex_lock(&chip->reg_lock);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003301
Vivien Didelotfad09c72016-06-21 12:28:20 -04003302 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU))
3303 ret = mv88e6xxx_mdio_write_ppu(chip, addr, regnum, val);
3304 else if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_SMI_PHY))
3305 ret = mv88e6xxx_mdio_write_indirect(chip, addr, regnum, val);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003306 else
Vivien Didelotfad09c72016-06-21 12:28:20 -04003307 ret = mv88e6xxx_mdio_write_direct(chip, addr, regnum, val);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003308
Vivien Didelotfad09c72016-06-21 12:28:20 -04003309 mutex_unlock(&chip->reg_lock);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003310 return ret;
3311}
3312
Vivien Didelotfad09c72016-06-21 12:28:20 -04003313static int mv88e6xxx_mdio_register(struct mv88e6xxx_chip *chip,
Andrew Lunnb516d452016-06-04 21:17:06 +02003314 struct device_node *np)
3315{
3316 static int index;
3317 struct mii_bus *bus;
3318 int err;
3319
Vivien Didelotfad09c72016-06-21 12:28:20 -04003320 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU))
3321 mv88e6xxx_ppu_state_init(chip);
Andrew Lunnb516d452016-06-04 21:17:06 +02003322
3323 if (np)
Vivien Didelotfad09c72016-06-21 12:28:20 -04003324 chip->mdio_np = of_get_child_by_name(np, "mdio");
Andrew Lunnb516d452016-06-04 21:17:06 +02003325
Vivien Didelotfad09c72016-06-21 12:28:20 -04003326 bus = devm_mdiobus_alloc(chip->dev);
Andrew Lunnb516d452016-06-04 21:17:06 +02003327 if (!bus)
3328 return -ENOMEM;
3329
Vivien Didelotfad09c72016-06-21 12:28:20 -04003330 bus->priv = (void *)chip;
Andrew Lunnb516d452016-06-04 21:17:06 +02003331 if (np) {
3332 bus->name = np->full_name;
3333 snprintf(bus->id, MII_BUS_ID_SIZE, "%s", np->full_name);
3334 } else {
3335 bus->name = "mv88e6xxx SMI";
3336 snprintf(bus->id, MII_BUS_ID_SIZE, "mv88e6xxx-%d", index++);
3337 }
3338
3339 bus->read = mv88e6xxx_mdio_read;
3340 bus->write = mv88e6xxx_mdio_write;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003341 bus->parent = chip->dev;
Andrew Lunnb516d452016-06-04 21:17:06 +02003342
Vivien Didelotfad09c72016-06-21 12:28:20 -04003343 if (chip->mdio_np)
3344 err = of_mdiobus_register(bus, chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003345 else
3346 err = mdiobus_register(bus);
3347 if (err) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04003348 dev_err(chip->dev, "Cannot register MDIO bus (%d)\n", err);
Andrew Lunnb516d452016-06-04 21:17:06 +02003349 goto out;
3350 }
Vivien Didelotfad09c72016-06-21 12:28:20 -04003351 chip->mdio_bus = bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003352
3353 return 0;
3354
3355out:
Vivien Didelotfad09c72016-06-21 12:28:20 -04003356 if (chip->mdio_np)
3357 of_node_put(chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003358
3359 return err;
3360}
3361
Vivien Didelotfad09c72016-06-21 12:28:20 -04003362static void mv88e6xxx_mdio_unregister(struct mv88e6xxx_chip *chip)
Andrew Lunnb516d452016-06-04 21:17:06 +02003363
3364{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003365 struct mii_bus *bus = chip->mdio_bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003366
3367 mdiobus_unregister(bus);
3368
Vivien Didelotfad09c72016-06-21 12:28:20 -04003369 if (chip->mdio_np)
3370 of_node_put(chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003371}
3372
Guenter Roeckc22995c2015-07-25 09:42:28 -07003373#ifdef CONFIG_NET_DSA_HWMON
3374
3375static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
3376{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003377 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003378 int ret;
3379 int val;
3380
3381 *temp = 0;
3382
Vivien Didelotfad09c72016-06-21 12:28:20 -04003383 mutex_lock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003384
Vivien Didelotfad09c72016-06-21 12:28:20 -04003385 ret = mv88e6xxx_mdio_write_direct(chip, 0x0, 0x16, 0x6);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003386 if (ret < 0)
3387 goto error;
3388
3389 /* Enable temperature sensor */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003390 ret = mv88e6xxx_mdio_read_direct(chip, 0x0, 0x1a);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003391 if (ret < 0)
3392 goto error;
3393
Vivien Didelotfad09c72016-06-21 12:28:20 -04003394 ret = mv88e6xxx_mdio_write_direct(chip, 0x0, 0x1a, ret | (1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003395 if (ret < 0)
3396 goto error;
3397
3398 /* Wait for temperature to stabilize */
3399 usleep_range(10000, 12000);
3400
Vivien Didelotfad09c72016-06-21 12:28:20 -04003401 val = mv88e6xxx_mdio_read_direct(chip, 0x0, 0x1a);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003402 if (val < 0) {
3403 ret = val;
3404 goto error;
3405 }
3406
3407 /* Disable temperature sensor */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003408 ret = mv88e6xxx_mdio_write_direct(chip, 0x0, 0x1a, ret & ~(1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003409 if (ret < 0)
3410 goto error;
3411
3412 *temp = ((val & 0x1f) - 5) * 5;
3413
3414error:
Vivien Didelotfad09c72016-06-21 12:28:20 -04003415 mv88e6xxx_mdio_write_direct(chip, 0x0, 0x16, 0x0);
3416 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003417 return ret;
3418}
3419
3420static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
3421{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003422 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3423 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003424 int ret;
3425
3426 *temp = 0;
3427
Andrew Lunn03a4a542016-06-04 21:17:05 +02003428 ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 27);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003429 if (ret < 0)
3430 return ret;
3431
3432 *temp = (ret & 0xff) - 25;
3433
3434 return 0;
3435}
3436
Vivien Didelotf81ec902016-05-09 13:22:58 -04003437static int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003438{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003439 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunn158bc062016-04-28 21:24:06 -04003440
Vivien Didelotfad09c72016-06-21 12:28:20 -04003441 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP))
Vivien Didelot6594f612016-05-09 13:22:42 -04003442 return -EOPNOTSUPP;
3443
Vivien Didelotfad09c72016-06-21 12:28:20 -04003444 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003445 return mv88e63xx_get_temp(ds, temp);
3446
3447 return mv88e61xx_get_temp(ds, temp);
3448}
3449
Vivien Didelotf81ec902016-05-09 13:22:58 -04003450static int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003451{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003452 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3453 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003454 int ret;
3455
Vivien Didelotfad09c72016-06-21 12:28:20 -04003456 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003457 return -EOPNOTSUPP;
3458
3459 *temp = 0;
3460
Andrew Lunn03a4a542016-06-04 21:17:05 +02003461 ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 26);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003462 if (ret < 0)
3463 return ret;
3464
3465 *temp = (((ret >> 8) & 0x1f) * 5) - 25;
3466
3467 return 0;
3468}
3469
Vivien Didelotf81ec902016-05-09 13:22:58 -04003470static int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003471{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003472 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3473 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003474 int ret;
3475
Vivien Didelotfad09c72016-06-21 12:28:20 -04003476 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003477 return -EOPNOTSUPP;
3478
Andrew Lunn03a4a542016-06-04 21:17:05 +02003479 ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 26);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003480 if (ret < 0)
3481 return ret;
3482 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
Andrew Lunn03a4a542016-06-04 21:17:05 +02003483 return mv88e6xxx_mdio_page_write(ds, phy, 6, 26,
3484 (ret & 0xe0ff) | (temp << 8));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003485}
3486
Vivien Didelotf81ec902016-05-09 13:22:58 -04003487static int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003488{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003489 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3490 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003491 int ret;
3492
Vivien Didelotfad09c72016-06-21 12:28:20 -04003493 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003494 return -EOPNOTSUPP;
3495
3496 *alarm = false;
3497
Andrew Lunn03a4a542016-06-04 21:17:05 +02003498 ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 26);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003499 if (ret < 0)
3500 return ret;
3501
3502 *alarm = !!(ret & 0x40);
3503
3504 return 0;
3505}
3506#endif /* CONFIG_NET_DSA_HWMON */
3507
Vivien Didelotf81ec902016-05-09 13:22:58 -04003508static const struct mv88e6xxx_info mv88e6xxx_table[] = {
3509 [MV88E6085] = {
3510 .prod_num = PORT_SWITCH_ID_PROD_NUM_6085,
3511 .family = MV88E6XXX_FAMILY_6097,
3512 .name = "Marvell 88E6085",
3513 .num_databases = 4096,
3514 .num_ports = 10,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003515 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003516 .flags = MV88E6XXX_FLAGS_FAMILY_6097,
3517 },
3518
3519 [MV88E6095] = {
3520 .prod_num = PORT_SWITCH_ID_PROD_NUM_6095,
3521 .family = MV88E6XXX_FAMILY_6095,
3522 .name = "Marvell 88E6095/88E6095F",
3523 .num_databases = 256,
3524 .num_ports = 11,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003525 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003526 .flags = MV88E6XXX_FLAGS_FAMILY_6095,
3527 },
3528
3529 [MV88E6123] = {
3530 .prod_num = PORT_SWITCH_ID_PROD_NUM_6123,
3531 .family = MV88E6XXX_FAMILY_6165,
3532 .name = "Marvell 88E6123",
3533 .num_databases = 4096,
3534 .num_ports = 3,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003535 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003536 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3537 },
3538
3539 [MV88E6131] = {
3540 .prod_num = PORT_SWITCH_ID_PROD_NUM_6131,
3541 .family = MV88E6XXX_FAMILY_6185,
3542 .name = "Marvell 88E6131",
3543 .num_databases = 256,
3544 .num_ports = 8,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003545 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003546 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3547 },
3548
3549 [MV88E6161] = {
3550 .prod_num = PORT_SWITCH_ID_PROD_NUM_6161,
3551 .family = MV88E6XXX_FAMILY_6165,
3552 .name = "Marvell 88E6161",
3553 .num_databases = 4096,
3554 .num_ports = 6,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003555 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003556 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3557 },
3558
3559 [MV88E6165] = {
3560 .prod_num = PORT_SWITCH_ID_PROD_NUM_6165,
3561 .family = MV88E6XXX_FAMILY_6165,
3562 .name = "Marvell 88E6165",
3563 .num_databases = 4096,
3564 .num_ports = 6,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003565 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003566 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3567 },
3568
3569 [MV88E6171] = {
3570 .prod_num = PORT_SWITCH_ID_PROD_NUM_6171,
3571 .family = MV88E6XXX_FAMILY_6351,
3572 .name = "Marvell 88E6171",
3573 .num_databases = 4096,
3574 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003575 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003576 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3577 },
3578
3579 [MV88E6172] = {
3580 .prod_num = PORT_SWITCH_ID_PROD_NUM_6172,
3581 .family = MV88E6XXX_FAMILY_6352,
3582 .name = "Marvell 88E6172",
3583 .num_databases = 4096,
3584 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003585 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003586 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3587 },
3588
3589 [MV88E6175] = {
3590 .prod_num = PORT_SWITCH_ID_PROD_NUM_6175,
3591 .family = MV88E6XXX_FAMILY_6351,
3592 .name = "Marvell 88E6175",
3593 .num_databases = 4096,
3594 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003595 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003596 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3597 },
3598
3599 [MV88E6176] = {
3600 .prod_num = PORT_SWITCH_ID_PROD_NUM_6176,
3601 .family = MV88E6XXX_FAMILY_6352,
3602 .name = "Marvell 88E6176",
3603 .num_databases = 4096,
3604 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003605 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003606 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3607 },
3608
3609 [MV88E6185] = {
3610 .prod_num = PORT_SWITCH_ID_PROD_NUM_6185,
3611 .family = MV88E6XXX_FAMILY_6185,
3612 .name = "Marvell 88E6185",
3613 .num_databases = 256,
3614 .num_ports = 10,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003615 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003616 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3617 },
3618
3619 [MV88E6240] = {
3620 .prod_num = PORT_SWITCH_ID_PROD_NUM_6240,
3621 .family = MV88E6XXX_FAMILY_6352,
3622 .name = "Marvell 88E6240",
3623 .num_databases = 4096,
3624 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003625 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003626 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3627 },
3628
3629 [MV88E6320] = {
3630 .prod_num = PORT_SWITCH_ID_PROD_NUM_6320,
3631 .family = MV88E6XXX_FAMILY_6320,
3632 .name = "Marvell 88E6320",
3633 .num_databases = 4096,
3634 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003635 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003636 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3637 },
3638
3639 [MV88E6321] = {
3640 .prod_num = PORT_SWITCH_ID_PROD_NUM_6321,
3641 .family = MV88E6XXX_FAMILY_6320,
3642 .name = "Marvell 88E6321",
3643 .num_databases = 4096,
3644 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003645 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003646 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3647 },
3648
3649 [MV88E6350] = {
3650 .prod_num = PORT_SWITCH_ID_PROD_NUM_6350,
3651 .family = MV88E6XXX_FAMILY_6351,
3652 .name = "Marvell 88E6350",
3653 .num_databases = 4096,
3654 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003655 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003656 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3657 },
3658
3659 [MV88E6351] = {
3660 .prod_num = PORT_SWITCH_ID_PROD_NUM_6351,
3661 .family = MV88E6XXX_FAMILY_6351,
3662 .name = "Marvell 88E6351",
3663 .num_databases = 4096,
3664 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003665 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003666 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3667 },
3668
3669 [MV88E6352] = {
3670 .prod_num = PORT_SWITCH_ID_PROD_NUM_6352,
3671 .family = MV88E6XXX_FAMILY_6352,
3672 .name = "Marvell 88E6352",
3673 .num_databases = 4096,
3674 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003675 .port_base_addr = 0x10,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003676 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3677 },
3678};
3679
Vivien Didelot5f7c0362016-06-20 13:14:04 -04003680static const struct mv88e6xxx_info *mv88e6xxx_lookup_info(unsigned int prod_num)
Vivien Didelotb9b37712015-10-30 19:39:48 -04003681{
Vivien Didelota439c062016-04-17 13:23:58 -04003682 int i;
Vivien Didelotb9b37712015-10-30 19:39:48 -04003683
Vivien Didelot5f7c0362016-06-20 13:14:04 -04003684 for (i = 0; i < ARRAY_SIZE(mv88e6xxx_table); ++i)
3685 if (mv88e6xxx_table[i].prod_num == prod_num)
3686 return &mv88e6xxx_table[i];
Vivien Didelotb9b37712015-10-30 19:39:48 -04003687
Vivien Didelotb9b37712015-10-30 19:39:48 -04003688 return NULL;
3689}
3690
Vivien Didelotfad09c72016-06-21 12:28:20 -04003691static int mv88e6xxx_detect(struct mv88e6xxx_chip *chip)
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003692{
3693 const struct mv88e6xxx_info *info;
3694 int id, prod_num, rev;
3695
Vivien Didelotfad09c72016-06-21 12:28:20 -04003696 id = mv88e6xxx_reg_read(chip, chip->info->port_base_addr,
3697 PORT_SWITCH_ID);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003698 if (id < 0)
3699 return id;
3700
3701 prod_num = (id & 0xfff0) >> 4;
3702 rev = id & 0x000f;
3703
3704 info = mv88e6xxx_lookup_info(prod_num);
3705 if (!info)
3706 return -ENODEV;
3707
Vivien Didelotcaac8542016-06-20 13:14:09 -04003708 /* Update the compatible info with the probed one */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003709 chip->info = info;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003710
Vivien Didelotfad09c72016-06-21 12:28:20 -04003711 dev_info(chip->dev, "switch 0x%x detected: %s, revision %u\n",
3712 chip->info->prod_num, chip->info->name, rev);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003713
3714 return 0;
3715}
3716
Vivien Didelotfad09c72016-06-21 12:28:20 -04003717static struct mv88e6xxx_chip *mv88e6xxx_alloc_chip(struct device *dev)
Vivien Didelot469d7292016-06-20 13:14:06 -04003718{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003719 struct mv88e6xxx_chip *chip;
Vivien Didelot469d7292016-06-20 13:14:06 -04003720
Vivien Didelotfad09c72016-06-21 12:28:20 -04003721 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
3722 if (!chip)
Vivien Didelot469d7292016-06-20 13:14:06 -04003723 return NULL;
3724
Vivien Didelotfad09c72016-06-21 12:28:20 -04003725 chip->dev = dev;
Vivien Didelot469d7292016-06-20 13:14:06 -04003726
Vivien Didelotfad09c72016-06-21 12:28:20 -04003727 mutex_init(&chip->reg_lock);
Vivien Didelot469d7292016-06-20 13:14:06 -04003728
Vivien Didelotfad09c72016-06-21 12:28:20 -04003729 return chip;
Vivien Didelot469d7292016-06-20 13:14:06 -04003730}
3731
Vivien Didelotfad09c72016-06-21 12:28:20 -04003732static int mv88e6xxx_smi_init(struct mv88e6xxx_chip *chip,
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003733 struct mii_bus *bus, int sw_addr)
3734{
3735 /* ADDR[0] pin is unavailable externally and considered zero */
3736 if (sw_addr & 0x1)
3737 return -EINVAL;
3738
Vivien Didelot914b32f2016-06-20 13:14:11 -04003739 if (sw_addr == 0)
Vivien Didelotfad09c72016-06-21 12:28:20 -04003740 chip->smi_ops = &mv88e6xxx_smi_single_chip_ops;
3741 else if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_MULTI_CHIP))
3742 chip->smi_ops = &mv88e6xxx_smi_multi_chip_ops;
Vivien Didelot914b32f2016-06-20 13:14:11 -04003743 else
3744 return -EINVAL;
3745
Vivien Didelotfad09c72016-06-21 12:28:20 -04003746 chip->bus = bus;
3747 chip->sw_addr = sw_addr;
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003748
3749 return 0;
3750}
3751
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02003752static const char *mv88e6xxx_drv_probe(struct device *dsa_dev,
3753 struct device *host_dev, int sw_addr,
3754 void **priv)
Andrew Lunna77d43f2016-04-13 02:40:42 +02003755{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003756 struct mv88e6xxx_chip *chip;
Vivien Didelota439c062016-04-17 13:23:58 -04003757 struct mii_bus *bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003758 int err;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003759
Vivien Didelota439c062016-04-17 13:23:58 -04003760 bus = dsa_host_dev_to_mii_bus(host_dev);
Andrew Lunnc1569132016-04-13 02:40:45 +02003761 if (!bus)
3762 return NULL;
3763
Vivien Didelotfad09c72016-06-21 12:28:20 -04003764 chip = mv88e6xxx_alloc_chip(dsa_dev);
3765 if (!chip)
Vivien Didelot469d7292016-06-20 13:14:06 -04003766 return NULL;
3767
Vivien Didelotcaac8542016-06-20 13:14:09 -04003768 /* Legacy SMI probing will only support chips similar to 88E6085 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003769 chip->info = &mv88e6xxx_table[MV88E6085];
Vivien Didelotcaac8542016-06-20 13:14:09 -04003770
Vivien Didelotfad09c72016-06-21 12:28:20 -04003771 err = mv88e6xxx_smi_init(chip, bus, sw_addr);
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003772 if (err)
3773 goto free;
3774
Vivien Didelotfad09c72016-06-21 12:28:20 -04003775 err = mv88e6xxx_detect(chip);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003776 if (err)
Vivien Didelot469d7292016-06-20 13:14:06 -04003777 goto free;
Vivien Didelota439c062016-04-17 13:23:58 -04003778
Vivien Didelotfad09c72016-06-21 12:28:20 -04003779 err = mv88e6xxx_mdio_register(chip, NULL);
Andrew Lunnb516d452016-06-04 21:17:06 +02003780 if (err)
Vivien Didelot469d7292016-06-20 13:14:06 -04003781 goto free;
Andrew Lunnb516d452016-06-04 21:17:06 +02003782
Vivien Didelotfad09c72016-06-21 12:28:20 -04003783 *priv = chip;
Vivien Didelota439c062016-04-17 13:23:58 -04003784
Vivien Didelotfad09c72016-06-21 12:28:20 -04003785 return chip->info->name;
Vivien Didelot469d7292016-06-20 13:14:06 -04003786free:
Vivien Didelotfad09c72016-06-21 12:28:20 -04003787 devm_kfree(dsa_dev, chip);
Vivien Didelot469d7292016-06-20 13:14:06 -04003788
3789 return NULL;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003790}
3791
Vivien Didelot57d32312016-06-20 13:13:58 -04003792static struct dsa_switch_driver mv88e6xxx_switch_driver = {
Vivien Didelotf81ec902016-05-09 13:22:58 -04003793 .tag_protocol = DSA_TAG_PROTO_EDSA,
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02003794 .probe = mv88e6xxx_drv_probe,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003795 .setup = mv88e6xxx_setup,
3796 .set_addr = mv88e6xxx_set_addr,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003797 .adjust_link = mv88e6xxx_adjust_link,
3798 .get_strings = mv88e6xxx_get_strings,
3799 .get_ethtool_stats = mv88e6xxx_get_ethtool_stats,
3800 .get_sset_count = mv88e6xxx_get_sset_count,
3801 .set_eee = mv88e6xxx_set_eee,
3802 .get_eee = mv88e6xxx_get_eee,
3803#ifdef CONFIG_NET_DSA_HWMON
3804 .get_temp = mv88e6xxx_get_temp,
3805 .get_temp_limit = mv88e6xxx_get_temp_limit,
3806 .set_temp_limit = mv88e6xxx_set_temp_limit,
3807 .get_temp_alarm = mv88e6xxx_get_temp_alarm,
3808#endif
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003809 .get_eeprom_len = mv88e6xxx_get_eeprom_len,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003810 .get_eeprom = mv88e6xxx_get_eeprom,
3811 .set_eeprom = mv88e6xxx_set_eeprom,
3812 .get_regs_len = mv88e6xxx_get_regs_len,
3813 .get_regs = mv88e6xxx_get_regs,
3814 .port_bridge_join = mv88e6xxx_port_bridge_join,
3815 .port_bridge_leave = mv88e6xxx_port_bridge_leave,
3816 .port_stp_state_set = mv88e6xxx_port_stp_state_set,
3817 .port_vlan_filtering = mv88e6xxx_port_vlan_filtering,
3818 .port_vlan_prepare = mv88e6xxx_port_vlan_prepare,
3819 .port_vlan_add = mv88e6xxx_port_vlan_add,
3820 .port_vlan_del = mv88e6xxx_port_vlan_del,
3821 .port_vlan_dump = mv88e6xxx_port_vlan_dump,
3822 .port_fdb_prepare = mv88e6xxx_port_fdb_prepare,
3823 .port_fdb_add = mv88e6xxx_port_fdb_add,
3824 .port_fdb_del = mv88e6xxx_port_fdb_del,
3825 .port_fdb_dump = mv88e6xxx_port_fdb_dump,
3826};
3827
Vivien Didelotfad09c72016-06-21 12:28:20 -04003828static int mv88e6xxx_register_switch(struct mv88e6xxx_chip *chip,
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003829 struct device_node *np)
3830{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003831 struct device *dev = chip->dev;
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003832 struct dsa_switch *ds;
3833
3834 ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
3835 if (!ds)
3836 return -ENOMEM;
3837
3838 ds->dev = dev;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003839 ds->priv = chip;
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003840 ds->drv = &mv88e6xxx_switch_driver;
3841
3842 dev_set_drvdata(dev, ds);
3843
3844 return dsa_register_switch(ds, np);
3845}
3846
Vivien Didelotfad09c72016-06-21 12:28:20 -04003847static void mv88e6xxx_unregister_switch(struct mv88e6xxx_chip *chip)
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003848{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003849 dsa_unregister_switch(chip->ds);
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003850}
3851
Vivien Didelot57d32312016-06-20 13:13:58 -04003852static int mv88e6xxx_probe(struct mdio_device *mdiodev)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003853{
3854 struct device *dev = &mdiodev->dev;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003855 struct device_node *np = dev->of_node;
Vivien Didelotcaac8542016-06-20 13:14:09 -04003856 const struct mv88e6xxx_info *compat_info;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003857 struct mv88e6xxx_chip *chip;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003858 u32 eeprom_len;
Andrew Lunn52638f72016-05-10 23:27:22 +02003859 int err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003860
Vivien Didelotcaac8542016-06-20 13:14:09 -04003861 compat_info = of_device_get_match_data(dev);
3862 if (!compat_info)
3863 return -EINVAL;
3864
Vivien Didelotfad09c72016-06-21 12:28:20 -04003865 chip = mv88e6xxx_alloc_chip(dev);
3866 if (!chip)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003867 return -ENOMEM;
3868
Vivien Didelotfad09c72016-06-21 12:28:20 -04003869 chip->info = compat_info;
Vivien Didelotcaac8542016-06-20 13:14:09 -04003870
Vivien Didelotfad09c72016-06-21 12:28:20 -04003871 err = mv88e6xxx_smi_init(chip, mdiodev->bus, mdiodev->addr);
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003872 if (err)
3873 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003874
Vivien Didelotfad09c72016-06-21 12:28:20 -04003875 err = mv88e6xxx_detect(chip);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003876 if (err)
3877 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003878
Vivien Didelotfad09c72016-06-21 12:28:20 -04003879 chip->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_ASIS);
3880 if (IS_ERR(chip->reset))
3881 return PTR_ERR(chip->reset);
Andrew Lunn52638f72016-05-10 23:27:22 +02003882
Vivien Didelotfad09c72016-06-21 12:28:20 -04003883 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEPROM) &&
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003884 !of_property_read_u32(np, "eeprom-length", &eeprom_len))
Vivien Didelotfad09c72016-06-21 12:28:20 -04003885 chip->eeprom_len = eeprom_len;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003886
Vivien Didelotfad09c72016-06-21 12:28:20 -04003887 err = mv88e6xxx_mdio_register(chip, np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003888 if (err)
3889 return err;
3890
Vivien Didelotfad09c72016-06-21 12:28:20 -04003891 err = mv88e6xxx_register_switch(chip, np);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02003892 if (err) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04003893 mv88e6xxx_mdio_unregister(chip);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02003894 return err;
3895 }
3896
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003897 return 0;
3898}
3899
3900static void mv88e6xxx_remove(struct mdio_device *mdiodev)
3901{
3902 struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev);
Vivien Didelotfad09c72016-06-21 12:28:20 -04003903 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003904
Vivien Didelotfad09c72016-06-21 12:28:20 -04003905 mv88e6xxx_unregister_switch(chip);
3906 mv88e6xxx_mdio_unregister(chip);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003907}
3908
3909static const struct of_device_id mv88e6xxx_of_match[] = {
Vivien Didelotcaac8542016-06-20 13:14:09 -04003910 {
3911 .compatible = "marvell,mv88e6085",
3912 .data = &mv88e6xxx_table[MV88E6085],
3913 },
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003914 { /* sentinel */ },
3915};
3916
3917MODULE_DEVICE_TABLE(of, mv88e6xxx_of_match);
3918
3919static struct mdio_driver mv88e6xxx_driver = {
3920 .probe = mv88e6xxx_probe,
3921 .remove = mv88e6xxx_remove,
3922 .mdiodrv.driver = {
3923 .name = "mv88e6085",
3924 .of_match_table = mv88e6xxx_of_match,
3925 },
3926};
3927
Ben Hutchings98e67302011-11-25 14:36:19 +00003928static int __init mv88e6xxx_init(void)
3929{
Vivien Didelotf81ec902016-05-09 13:22:58 -04003930 register_switch_driver(&mv88e6xxx_switch_driver);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003931 return mdio_driver_register(&mv88e6xxx_driver);
Ben Hutchings98e67302011-11-25 14:36:19 +00003932}
3933module_init(mv88e6xxx_init);
3934
3935static void __exit mv88e6xxx_cleanup(void)
3936{
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003937 mdio_driver_unregister(&mv88e6xxx_driver);
Vivien Didelotf81ec902016-05-09 13:22:58 -04003938 unregister_switch_driver(&mv88e6xxx_switch_driver);
Ben Hutchings98e67302011-11-25 14:36:19 +00003939}
3940module_exit(mv88e6xxx_cleanup);
Ben Hutchings3d825ed2011-11-25 14:37:16 +00003941
3942MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
3943MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
3944MODULE_LICENSE("GPL");