blob: f95f1d49fdba4be951e7540ef7c9ea7565003de9 [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 Lunnc8c1b392015-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 Didelotf22ab642016-07-18 20:45:31 -0400219/* Indirect write to single pointer-data register with an Update bit */
220static int mv88e6xxx_update(struct mv88e6xxx_chip *chip, int addr, int reg,
221 u16 update)
222{
223 u16 val;
224 int i, err;
225
226 /* Wait until the previous operation is completed */
227 for (i = 0; i < 16; ++i) {
228 err = mv88e6xxx_read(chip, addr, reg, &val);
229 if (err)
230 return err;
231
232 if (!(val & BIT(15)))
233 break;
234 }
235
236 if (i == 16)
237 return -ETIMEDOUT;
238
239 /* Set the Update bit to trigger a write operation */
240 val = BIT(15) | update;
241
242 return mv88e6xxx_write(chip, addr, reg, val);
243}
244
Vivien Didelotfad09c72016-06-21 12:28:20 -0400245static int _mv88e6xxx_reg_read(struct mv88e6xxx_chip *chip, int addr, int reg)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000246{
Vivien Didelot914b32f2016-06-20 13:14:11 -0400247 u16 val;
248 int err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000249
Vivien Didelotfad09c72016-06-21 12:28:20 -0400250 err = mv88e6xxx_read(chip, addr, reg, &val);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400251 if (err)
252 return err;
Vivien Didelot3996a4f2015-10-30 18:56:45 -0400253
Vivien Didelot914b32f2016-06-20 13:14:11 -0400254 return val;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000255}
256
Vivien Didelotfad09c72016-06-21 12:28:20 -0400257static int mv88e6xxx_reg_read(struct mv88e6xxx_chip *chip, int addr, int reg)
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700258{
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700259 int ret;
260
Vivien Didelotfad09c72016-06-21 12:28:20 -0400261 mutex_lock(&chip->reg_lock);
262 ret = _mv88e6xxx_reg_read(chip, addr, reg);
263 mutex_unlock(&chip->reg_lock);
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700264
265 return ret;
266}
267
Vivien Didelotfad09c72016-06-21 12:28:20 -0400268static int _mv88e6xxx_reg_write(struct mv88e6xxx_chip *chip, int addr,
Andrew Lunn158bc062016-04-28 21:24:06 -0400269 int reg, u16 val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000270{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400271 return mv88e6xxx_write(chip, addr, reg, val);
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700272}
273
Vivien Didelotfad09c72016-06-21 12:28:20 -0400274static int mv88e6xxx_mdio_read_direct(struct mv88e6xxx_chip *chip,
Andrew Lunn03a4a542016-06-04 21:17:05 +0200275 int addr, int regnum)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000276{
277 if (addr >= 0)
Vivien Didelotfad09c72016-06-21 12:28:20 -0400278 return _mv88e6xxx_reg_read(chip, addr, regnum);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000279 return 0xffff;
280}
281
Vivien Didelotfad09c72016-06-21 12:28:20 -0400282static int mv88e6xxx_mdio_write_direct(struct mv88e6xxx_chip *chip,
Andrew Lunn03a4a542016-06-04 21:17:05 +0200283 int addr, int regnum, u16 val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000284{
285 if (addr >= 0)
Vivien Didelotfad09c72016-06-21 12:28:20 -0400286 return _mv88e6xxx_reg_write(chip, addr, regnum, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000287 return 0;
288}
289
Vivien Didelotfad09c72016-06-21 12:28:20 -0400290static int mv88e6xxx_ppu_disable(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000291{
292 int ret;
Barry Grussling19b2f972013-01-08 16:05:54 +0000293 unsigned long timeout;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000294
Vivien Didelotfad09c72016-06-21 12:28:20 -0400295 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_CONTROL);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200296 if (ret < 0)
297 return ret;
298
Vivien Didelotfad09c72016-06-21 12:28:20 -0400299 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL,
Vivien Didelot8c9983a2016-05-09 13:22:39 -0400300 ret & ~GLOBAL_CONTROL_PPU_ENABLE);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200301 if (ret)
302 return ret;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000303
Barry Grussling19b2f972013-01-08 16:05:54 +0000304 timeout = jiffies + 1 * HZ;
305 while (time_before(jiffies, timeout)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400306 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATUS);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200307 if (ret < 0)
308 return ret;
309
Barry Grussling19b2f972013-01-08 16:05:54 +0000310 usleep_range(1000, 2000);
Andrew Lunncca8b132015-04-02 04:06:39 +0200311 if ((ret & GLOBAL_STATUS_PPU_MASK) !=
312 GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000313 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000314 }
315
316 return -ETIMEDOUT;
317}
318
Vivien Didelotfad09c72016-06-21 12:28:20 -0400319static int mv88e6xxx_ppu_enable(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000320{
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200321 int ret, err;
Barry Grussling19b2f972013-01-08 16:05:54 +0000322 unsigned long timeout;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000323
Vivien Didelotfad09c72016-06-21 12:28:20 -0400324 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_CONTROL);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200325 if (ret < 0)
326 return ret;
327
Vivien Didelotfad09c72016-06-21 12:28:20 -0400328 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL,
Vivien Didelot762eb672016-06-04 21:16:54 +0200329 ret | GLOBAL_CONTROL_PPU_ENABLE);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200330 if (err)
331 return err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000332
Barry Grussling19b2f972013-01-08 16:05:54 +0000333 timeout = jiffies + 1 * HZ;
334 while (time_before(jiffies, timeout)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400335 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATUS);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200336 if (ret < 0)
337 return ret;
338
Barry Grussling19b2f972013-01-08 16:05:54 +0000339 usleep_range(1000, 2000);
Andrew Lunncca8b132015-04-02 04:06:39 +0200340 if ((ret & GLOBAL_STATUS_PPU_MASK) ==
341 GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000342 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000343 }
344
345 return -ETIMEDOUT;
346}
347
348static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
349{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400350 struct mv88e6xxx_chip *chip;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000351
Vivien Didelotfad09c72016-06-21 12:28:20 -0400352 chip = container_of(ugly, struct mv88e6xxx_chip, ppu_work);
Vivien Didelot762eb672016-06-04 21:16:54 +0200353
Vivien Didelotfad09c72016-06-21 12:28:20 -0400354 mutex_lock(&chip->reg_lock);
Vivien Didelot762eb672016-06-04 21:16:54 +0200355
Vivien Didelotfad09c72016-06-21 12:28:20 -0400356 if (mutex_trylock(&chip->ppu_mutex)) {
357 if (mv88e6xxx_ppu_enable(chip) == 0)
358 chip->ppu_disabled = 0;
359 mutex_unlock(&chip->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000360 }
Vivien Didelot762eb672016-06-04 21:16:54 +0200361
Vivien Didelotfad09c72016-06-21 12:28:20 -0400362 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000363}
364
365static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
366{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400367 struct mv88e6xxx_chip *chip = (void *)_ps;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000368
Vivien Didelotfad09c72016-06-21 12:28:20 -0400369 schedule_work(&chip->ppu_work);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000370}
371
Vivien Didelotfad09c72016-06-21 12:28:20 -0400372static int mv88e6xxx_ppu_access_get(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000373{
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000374 int ret;
375
Vivien Didelotfad09c72016-06-21 12:28:20 -0400376 mutex_lock(&chip->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000377
Barry Grussling3675c8d2013-01-08 16:05:53 +0000378 /* If the PHY polling unit is enabled, disable it so that
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000379 * we can access the PHY registers. If it was already
380 * disabled, cancel the timer that is going to re-enable
381 * it.
382 */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400383 if (!chip->ppu_disabled) {
384 ret = mv88e6xxx_ppu_disable(chip);
Barry Grussling85686582013-01-08 16:05:56 +0000385 if (ret < 0) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400386 mutex_unlock(&chip->ppu_mutex);
Barry Grussling85686582013-01-08 16:05:56 +0000387 return ret;
388 }
Vivien Didelotfad09c72016-06-21 12:28:20 -0400389 chip->ppu_disabled = 1;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000390 } else {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400391 del_timer(&chip->ppu_timer);
Barry Grussling85686582013-01-08 16:05:56 +0000392 ret = 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000393 }
394
395 return ret;
396}
397
Vivien Didelotfad09c72016-06-21 12:28:20 -0400398static void mv88e6xxx_ppu_access_put(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000399{
Barry Grussling3675c8d2013-01-08 16:05:53 +0000400 /* Schedule a timer to re-enable the PHY polling unit. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400401 mod_timer(&chip->ppu_timer, jiffies + msecs_to_jiffies(10));
402 mutex_unlock(&chip->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000403}
404
Vivien Didelotfad09c72016-06-21 12:28:20 -0400405static void mv88e6xxx_ppu_state_init(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000406{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400407 mutex_init(&chip->ppu_mutex);
408 INIT_WORK(&chip->ppu_work, mv88e6xxx_ppu_reenable_work);
409 init_timer(&chip->ppu_timer);
410 chip->ppu_timer.data = (unsigned long)chip;
411 chip->ppu_timer.function = mv88e6xxx_ppu_reenable_timer;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000412}
413
Vivien Didelotfad09c72016-06-21 12:28:20 -0400414static int mv88e6xxx_mdio_read_ppu(struct mv88e6xxx_chip *chip, int addr,
Andrew Lunn03a4a542016-06-04 21:17:05 +0200415 int regnum)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000416{
417 int ret;
418
Vivien Didelotfad09c72016-06-21 12:28:20 -0400419 ret = mv88e6xxx_ppu_access_get(chip);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000420 if (ret >= 0) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400421 ret = _mv88e6xxx_reg_read(chip, addr, regnum);
422 mv88e6xxx_ppu_access_put(chip);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000423 }
424
425 return ret;
426}
427
Vivien Didelotfad09c72016-06-21 12:28:20 -0400428static int mv88e6xxx_mdio_write_ppu(struct mv88e6xxx_chip *chip, int addr,
Andrew Lunn03a4a542016-06-04 21:17:05 +0200429 int regnum, u16 val)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000430{
431 int ret;
432
Vivien Didelotfad09c72016-06-21 12:28:20 -0400433 ret = mv88e6xxx_ppu_access_get(chip);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000434 if (ret >= 0) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400435 ret = _mv88e6xxx_reg_write(chip, addr, regnum, val);
436 mv88e6xxx_ppu_access_put(chip);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000437 }
438
439 return ret;
440}
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000441
Vivien Didelotfad09c72016-06-21 12:28:20 -0400442static bool mv88e6xxx_6065_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200443{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400444 return chip->info->family == MV88E6XXX_FAMILY_6065;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200445}
446
Vivien Didelotfad09c72016-06-21 12:28:20 -0400447static bool mv88e6xxx_6095_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200448{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400449 return chip->info->family == MV88E6XXX_FAMILY_6095;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200450}
451
Vivien Didelotfad09c72016-06-21 12:28:20 -0400452static bool mv88e6xxx_6097_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200453{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400454 return chip->info->family == MV88E6XXX_FAMILY_6097;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200455}
456
Vivien Didelotfad09c72016-06-21 12:28:20 -0400457static bool mv88e6xxx_6165_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200458{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400459 return chip->info->family == MV88E6XXX_FAMILY_6165;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200460}
461
Vivien Didelotfad09c72016-06-21 12:28:20 -0400462static bool mv88e6xxx_6185_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200463{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400464 return chip->info->family == MV88E6XXX_FAMILY_6185;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200465}
466
Vivien Didelotfad09c72016-06-21 12:28:20 -0400467static bool mv88e6xxx_6320_family(struct mv88e6xxx_chip *chip)
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700468{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400469 return chip->info->family == MV88E6XXX_FAMILY_6320;
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700470}
471
Vivien Didelotfad09c72016-06-21 12:28:20 -0400472static bool mv88e6xxx_6351_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200473{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400474 return chip->info->family == MV88E6XXX_FAMILY_6351;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200475}
476
Vivien Didelotfad09c72016-06-21 12:28:20 -0400477static bool mv88e6xxx_6352_family(struct mv88e6xxx_chip *chip)
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200478{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400479 return chip->info->family == MV88E6XXX_FAMILY_6352;
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200480}
481
Vivien Didelotfad09c72016-06-21 12:28:20 -0400482static unsigned int mv88e6xxx_num_databases(struct mv88e6xxx_chip *chip)
Vivien Didelotf74df0b2016-03-31 16:53:43 -0400483{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400484 return chip->info->num_databases;
Vivien Didelotf74df0b2016-03-31 16:53:43 -0400485}
486
Vivien Didelotfad09c72016-06-21 12:28:20 -0400487static bool mv88e6xxx_has_fid_reg(struct mv88e6xxx_chip *chip)
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400488{
489 /* Does the device have dedicated FID registers for ATU and VTU ops? */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400490 if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
491 mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip))
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400492 return true;
493
494 return false;
495}
496
Andrew Lunndea87022015-08-31 15:56:47 +0200497/* We expect the switch to perform auto negotiation if there is a real
498 * phy. However, in the case of a fixed link phy, we force the port
499 * settings from the fixed link settings.
500 */
Vivien Didelotf81ec902016-05-09 13:22:58 -0400501static void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port,
502 struct phy_device *phydev)
Andrew Lunndea87022015-08-31 15:56:47 +0200503{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400504 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunn49052872015-09-29 01:53:48 +0200505 u32 reg;
506 int ret;
Andrew Lunndea87022015-08-31 15:56:47 +0200507
508 if (!phy_is_pseudo_fixed_link(phydev))
509 return;
510
Vivien Didelotfad09c72016-06-21 12:28:20 -0400511 mutex_lock(&chip->reg_lock);
Andrew Lunndea87022015-08-31 15:56:47 +0200512
Vivien Didelotfad09c72016-06-21 12:28:20 -0400513 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_PCS_CTRL);
Andrew Lunndea87022015-08-31 15:56:47 +0200514 if (ret < 0)
515 goto out;
516
517 reg = ret & ~(PORT_PCS_CTRL_LINK_UP |
518 PORT_PCS_CTRL_FORCE_LINK |
519 PORT_PCS_CTRL_DUPLEX_FULL |
520 PORT_PCS_CTRL_FORCE_DUPLEX |
521 PORT_PCS_CTRL_UNFORCED);
522
523 reg |= PORT_PCS_CTRL_FORCE_LINK;
524 if (phydev->link)
Vivien Didelot57d32312016-06-20 13:13:58 -0400525 reg |= PORT_PCS_CTRL_LINK_UP;
Andrew Lunndea87022015-08-31 15:56:47 +0200526
Vivien Didelotfad09c72016-06-21 12:28:20 -0400527 if (mv88e6xxx_6065_family(chip) && phydev->speed > SPEED_100)
Andrew Lunndea87022015-08-31 15:56:47 +0200528 goto out;
529
530 switch (phydev->speed) {
531 case SPEED_1000:
532 reg |= PORT_PCS_CTRL_1000;
533 break;
534 case SPEED_100:
535 reg |= PORT_PCS_CTRL_100;
536 break;
537 case SPEED_10:
538 reg |= PORT_PCS_CTRL_10;
539 break;
540 default:
541 pr_info("Unknown speed");
542 goto out;
543 }
544
545 reg |= PORT_PCS_CTRL_FORCE_DUPLEX;
546 if (phydev->duplex == DUPLEX_FULL)
547 reg |= PORT_PCS_CTRL_DUPLEX_FULL;
548
Vivien Didelotfad09c72016-06-21 12:28:20 -0400549 if ((mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip)) &&
550 (port >= chip->info->num_ports - 2)) {
Andrew Lunne7e72ac2015-08-31 15:56:51 +0200551 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
552 reg |= PORT_PCS_CTRL_RGMII_DELAY_RXCLK;
553 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
554 reg |= PORT_PCS_CTRL_RGMII_DELAY_TXCLK;
555 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
556 reg |= (PORT_PCS_CTRL_RGMII_DELAY_RXCLK |
557 PORT_PCS_CTRL_RGMII_DELAY_TXCLK);
558 }
Vivien Didelotfad09c72016-06-21 12:28:20 -0400559 _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_PCS_CTRL, reg);
Andrew Lunndea87022015-08-31 15:56:47 +0200560
561out:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400562 mutex_unlock(&chip->reg_lock);
Andrew Lunndea87022015-08-31 15:56:47 +0200563}
564
Vivien Didelotfad09c72016-06-21 12:28:20 -0400565static int _mv88e6xxx_stats_wait(struct mv88e6xxx_chip *chip)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000566{
567 int ret;
568 int i;
569
570 for (i = 0; i < 10; i++) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400571 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_OP);
Andrew Lunncca8b132015-04-02 04:06:39 +0200572 if ((ret & GLOBAL_STATS_OP_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000573 return 0;
574 }
575
576 return -ETIMEDOUT;
577}
578
Vivien Didelotfad09c72016-06-21 12:28:20 -0400579static int _mv88e6xxx_stats_snapshot(struct mv88e6xxx_chip *chip, int port)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000580{
581 int ret;
582
Vivien Didelotfad09c72016-06-21 12:28:20 -0400583 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200584 port = (port + 1) << 5;
585
Barry Grussling3675c8d2013-01-08 16:05:53 +0000586 /* Snapshot the hardware statistics counters for this port. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400587 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
Andrew Lunn31888232015-05-06 01:09:54 +0200588 GLOBAL_STATS_OP_CAPTURE_PORT |
589 GLOBAL_STATS_OP_HIST_RX_TX | port);
590 if (ret < 0)
591 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000592
Barry Grussling3675c8d2013-01-08 16:05:53 +0000593 /* Wait for the snapshotting to complete. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400594 ret = _mv88e6xxx_stats_wait(chip);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000595 if (ret < 0)
596 return ret;
597
598 return 0;
599}
600
Vivien Didelotfad09c72016-06-21 12:28:20 -0400601static void _mv88e6xxx_stats_read(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -0400602 int stat, u32 *val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000603{
604 u32 _val;
605 int ret;
606
607 *val = 0;
608
Vivien Didelotfad09c72016-06-21 12:28:20 -0400609 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
Andrew Lunn31888232015-05-06 01:09:54 +0200610 GLOBAL_STATS_OP_READ_CAPTURED |
611 GLOBAL_STATS_OP_HIST_RX_TX | stat);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000612 if (ret < 0)
613 return;
614
Vivien Didelotfad09c72016-06-21 12:28:20 -0400615 ret = _mv88e6xxx_stats_wait(chip);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000616 if (ret < 0)
617 return;
618
Vivien Didelotfad09c72016-06-21 12:28:20 -0400619 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_COUNTER_32);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000620 if (ret < 0)
621 return;
622
623 _val = ret << 16;
624
Vivien Didelotfad09c72016-06-21 12:28:20 -0400625 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_COUNTER_01);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000626 if (ret < 0)
627 return;
628
629 *val = _val | ret;
630}
631
Andrew Lunne413e7e2015-04-02 04:06:38 +0200632static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100633 { "in_good_octets", 8, 0x00, BANK0, },
634 { "in_bad_octets", 4, 0x02, BANK0, },
635 { "in_unicast", 4, 0x04, BANK0, },
636 { "in_broadcasts", 4, 0x06, BANK0, },
637 { "in_multicasts", 4, 0x07, BANK0, },
638 { "in_pause", 4, 0x16, BANK0, },
639 { "in_undersize", 4, 0x18, BANK0, },
640 { "in_fragments", 4, 0x19, BANK0, },
641 { "in_oversize", 4, 0x1a, BANK0, },
642 { "in_jabber", 4, 0x1b, BANK0, },
643 { "in_rx_error", 4, 0x1c, BANK0, },
644 { "in_fcs_error", 4, 0x1d, BANK0, },
645 { "out_octets", 8, 0x0e, BANK0, },
646 { "out_unicast", 4, 0x10, BANK0, },
647 { "out_broadcasts", 4, 0x13, BANK0, },
648 { "out_multicasts", 4, 0x12, BANK0, },
649 { "out_pause", 4, 0x15, BANK0, },
650 { "excessive", 4, 0x11, BANK0, },
651 { "collisions", 4, 0x1e, BANK0, },
652 { "deferred", 4, 0x05, BANK0, },
653 { "single", 4, 0x14, BANK0, },
654 { "multiple", 4, 0x17, BANK0, },
655 { "out_fcs_error", 4, 0x03, BANK0, },
656 { "late", 4, 0x1f, BANK0, },
657 { "hist_64bytes", 4, 0x08, BANK0, },
658 { "hist_65_127bytes", 4, 0x09, BANK0, },
659 { "hist_128_255bytes", 4, 0x0a, BANK0, },
660 { "hist_256_511bytes", 4, 0x0b, BANK0, },
661 { "hist_512_1023bytes", 4, 0x0c, BANK0, },
662 { "hist_1024_max_bytes", 4, 0x0d, BANK0, },
663 { "sw_in_discards", 4, 0x10, PORT, },
664 { "sw_in_filtered", 2, 0x12, PORT, },
665 { "sw_out_filtered", 2, 0x13, PORT, },
666 { "in_discards", 4, 0x00 | GLOBAL_STATS_OP_BANK_1, BANK1, },
667 { "in_filtered", 4, 0x01 | GLOBAL_STATS_OP_BANK_1, BANK1, },
668 { "in_accepted", 4, 0x02 | GLOBAL_STATS_OP_BANK_1, BANK1, },
669 { "in_bad_accepted", 4, 0x03 | GLOBAL_STATS_OP_BANK_1, BANK1, },
670 { "in_good_avb_class_a", 4, 0x04 | GLOBAL_STATS_OP_BANK_1, BANK1, },
671 { "in_good_avb_class_b", 4, 0x05 | GLOBAL_STATS_OP_BANK_1, BANK1, },
672 { "in_bad_avb_class_a", 4, 0x06 | GLOBAL_STATS_OP_BANK_1, BANK1, },
673 { "in_bad_avb_class_b", 4, 0x07 | GLOBAL_STATS_OP_BANK_1, BANK1, },
674 { "tcam_counter_0", 4, 0x08 | GLOBAL_STATS_OP_BANK_1, BANK1, },
675 { "tcam_counter_1", 4, 0x09 | GLOBAL_STATS_OP_BANK_1, BANK1, },
676 { "tcam_counter_2", 4, 0x0a | GLOBAL_STATS_OP_BANK_1, BANK1, },
677 { "tcam_counter_3", 4, 0x0b | GLOBAL_STATS_OP_BANK_1, BANK1, },
678 { "in_da_unknown", 4, 0x0e | GLOBAL_STATS_OP_BANK_1, BANK1, },
679 { "in_management", 4, 0x0f | GLOBAL_STATS_OP_BANK_1, BANK1, },
680 { "out_queue_0", 4, 0x10 | GLOBAL_STATS_OP_BANK_1, BANK1, },
681 { "out_queue_1", 4, 0x11 | GLOBAL_STATS_OP_BANK_1, BANK1, },
682 { "out_queue_2", 4, 0x12 | GLOBAL_STATS_OP_BANK_1, BANK1, },
683 { "out_queue_3", 4, 0x13 | GLOBAL_STATS_OP_BANK_1, BANK1, },
684 { "out_queue_4", 4, 0x14 | GLOBAL_STATS_OP_BANK_1, BANK1, },
685 { "out_queue_5", 4, 0x15 | GLOBAL_STATS_OP_BANK_1, BANK1, },
686 { "out_queue_6", 4, 0x16 | GLOBAL_STATS_OP_BANK_1, BANK1, },
687 { "out_queue_7", 4, 0x17 | GLOBAL_STATS_OP_BANK_1, BANK1, },
688 { "out_cut_through", 4, 0x18 | GLOBAL_STATS_OP_BANK_1, BANK1, },
689 { "out_octets_a", 4, 0x1a | GLOBAL_STATS_OP_BANK_1, BANK1, },
690 { "out_octets_b", 4, 0x1b | GLOBAL_STATS_OP_BANK_1, BANK1, },
691 { "out_management", 4, 0x1f | GLOBAL_STATS_OP_BANK_1, BANK1, },
Andrew Lunne413e7e2015-04-02 04:06:38 +0200692};
693
Vivien Didelotfad09c72016-06-21 12:28:20 -0400694static bool mv88e6xxx_has_stat(struct mv88e6xxx_chip *chip,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100695 struct mv88e6xxx_hw_stat *stat)
Andrew Lunne413e7e2015-04-02 04:06:38 +0200696{
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100697 switch (stat->type) {
698 case BANK0:
Andrew Lunne413e7e2015-04-02 04:06:38 +0200699 return true;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100700 case BANK1:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400701 return mv88e6xxx_6320_family(chip);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100702 case PORT:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400703 return mv88e6xxx_6095_family(chip) ||
704 mv88e6xxx_6185_family(chip) ||
705 mv88e6xxx_6097_family(chip) ||
706 mv88e6xxx_6165_family(chip) ||
707 mv88e6xxx_6351_family(chip) ||
708 mv88e6xxx_6352_family(chip);
Andrew Lunne413e7e2015-04-02 04:06:38 +0200709 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100710 return false;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000711}
712
Vivien Didelotfad09c72016-06-21 12:28:20 -0400713static uint64_t _mv88e6xxx_get_ethtool_stat(struct mv88e6xxx_chip *chip,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100714 struct mv88e6xxx_hw_stat *s,
Andrew Lunn80c46272015-06-20 18:42:30 +0200715 int port)
716{
Andrew Lunn80c46272015-06-20 18:42:30 +0200717 u32 low;
718 u32 high = 0;
719 int ret;
720 u64 value;
721
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100722 switch (s->type) {
723 case PORT:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400724 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), s->reg);
Andrew Lunn80c46272015-06-20 18:42:30 +0200725 if (ret < 0)
726 return UINT64_MAX;
727
728 low = ret;
729 if (s->sizeof_stat == 4) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400730 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port),
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100731 s->reg + 1);
Andrew Lunn80c46272015-06-20 18:42:30 +0200732 if (ret < 0)
733 return UINT64_MAX;
734 high = ret;
735 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100736 break;
737 case BANK0:
738 case BANK1:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400739 _mv88e6xxx_stats_read(chip, s->reg, &low);
Andrew Lunn80c46272015-06-20 18:42:30 +0200740 if (s->sizeof_stat == 8)
Vivien Didelotfad09c72016-06-21 12:28:20 -0400741 _mv88e6xxx_stats_read(chip, s->reg + 1, &high);
Andrew Lunn80c46272015-06-20 18:42:30 +0200742 }
743 value = (((u64)high) << 16) | low;
744 return value;
745}
746
Vivien Didelotf81ec902016-05-09 13:22:58 -0400747static void mv88e6xxx_get_strings(struct dsa_switch *ds, int port,
748 uint8_t *data)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100749{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400750 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100751 struct mv88e6xxx_hw_stat *stat;
752 int i, j;
753
754 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
755 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400756 if (mv88e6xxx_has_stat(chip, stat)) {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100757 memcpy(data + j * ETH_GSTRING_LEN, stat->string,
758 ETH_GSTRING_LEN);
759 j++;
760 }
761 }
762}
763
Vivien Didelotf81ec902016-05-09 13:22:58 -0400764static int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100765{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400766 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100767 struct mv88e6xxx_hw_stat *stat;
768 int i, j;
769
770 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
771 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400772 if (mv88e6xxx_has_stat(chip, stat))
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100773 j++;
774 }
775 return j;
776}
777
Vivien Didelotf81ec902016-05-09 13:22:58 -0400778static void mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, int port,
779 uint64_t *data)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000780{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400781 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100782 struct mv88e6xxx_hw_stat *stat;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000783 int ret;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100784 int i, j;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000785
Vivien Didelotfad09c72016-06-21 12:28:20 -0400786 mutex_lock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000787
Vivien Didelotfad09c72016-06-21 12:28:20 -0400788 ret = _mv88e6xxx_stats_snapshot(chip, port);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000789 if (ret < 0) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400790 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000791 return;
792 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100793 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
794 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400795 if (mv88e6xxx_has_stat(chip, stat)) {
796 data[j] = _mv88e6xxx_get_ethtool_stat(chip, stat, port);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100797 j++;
798 }
799 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000800
Vivien Didelotfad09c72016-06-21 12:28:20 -0400801 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000802}
Ben Hutchings98e67302011-11-25 14:36:19 +0000803
Vivien Didelotf81ec902016-05-09 13:22:58 -0400804static int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700805{
806 return 32 * sizeof(u16);
807}
808
Vivien Didelotf81ec902016-05-09 13:22:58 -0400809static void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
810 struct ethtool_regs *regs, void *_p)
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700811{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400812 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700813 u16 *p = _p;
814 int i;
815
816 regs->version = 0;
817
818 memset(p, 0xff, 32 * sizeof(u16));
819
Vivien Didelotfad09c72016-06-21 12:28:20 -0400820 mutex_lock(&chip->reg_lock);
Vivien Didelot23062512016-05-09 13:22:45 -0400821
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700822 for (i = 0; i < 32; i++) {
823 int ret;
824
Vivien Didelotfad09c72016-06-21 12:28:20 -0400825 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), i);
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700826 if (ret >= 0)
827 p[i] = ret;
828 }
Vivien Didelot23062512016-05-09 13:22:45 -0400829
Vivien Didelotfad09c72016-06-21 12:28:20 -0400830 mutex_unlock(&chip->reg_lock);
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700831}
832
Vivien Didelotfad09c72016-06-21 12:28:20 -0400833static int _mv88e6xxx_wait(struct mv88e6xxx_chip *chip, int reg, int offset,
Andrew Lunn3898c142015-05-06 01:09:53 +0200834 u16 mask)
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700835{
836 unsigned long timeout = jiffies + HZ / 10;
837
838 while (time_before(jiffies, timeout)) {
839 int ret;
840
Vivien Didelotfad09c72016-06-21 12:28:20 -0400841 ret = _mv88e6xxx_reg_read(chip, reg, offset);
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700842 if (ret < 0)
843 return ret;
844 if (!(ret & mask))
845 return 0;
846
847 usleep_range(1000, 2000);
848 }
849 return -ETIMEDOUT;
850}
851
Vivien Didelotfad09c72016-06-21 12:28:20 -0400852static int mv88e6xxx_mdio_wait(struct mv88e6xxx_chip *chip)
Andrew Lunn3898c142015-05-06 01:09:53 +0200853{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400854 return _mv88e6xxx_wait(chip, REG_GLOBAL2, GLOBAL2_SMI_OP,
Andrew Lunn3898c142015-05-06 01:09:53 +0200855 GLOBAL2_SMI_OP_BUSY);
856}
857
Vivien Didelotfad09c72016-06-21 12:28:20 -0400858static int _mv88e6xxx_atu_wait(struct mv88e6xxx_chip *chip)
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700859{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400860 return _mv88e6xxx_wait(chip, REG_GLOBAL, GLOBAL_ATU_OP,
Andrew Lunncca8b132015-04-02 04:06:39 +0200861 GLOBAL_ATU_OP_BUSY);
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700862}
863
Vivien Didelotfad09c72016-06-21 12:28:20 -0400864static int mv88e6xxx_mdio_read_indirect(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -0400865 int addr, int regnum)
Andrew Lunnf3044682015-02-14 19:17:50 +0100866{
867 int ret;
868
Vivien Didelotfad09c72016-06-21 12:28:20 -0400869 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_SMI_OP,
Andrew Lunn3898c142015-05-06 01:09:53 +0200870 GLOBAL2_SMI_OP_22_READ | (addr << 5) |
871 regnum);
Andrew Lunnf3044682015-02-14 19:17:50 +0100872 if (ret < 0)
873 return ret;
874
Vivien Didelotfad09c72016-06-21 12:28:20 -0400875 ret = mv88e6xxx_mdio_wait(chip);
Andrew Lunn3898c142015-05-06 01:09:53 +0200876 if (ret < 0)
877 return ret;
878
Vivien Didelotfad09c72016-06-21 12:28:20 -0400879 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL2, GLOBAL2_SMI_DATA);
Andrew Lunn158bc062016-04-28 21:24:06 -0400880
881 return ret;
Andrew Lunnf3044682015-02-14 19:17:50 +0100882}
883
Vivien Didelotfad09c72016-06-21 12:28:20 -0400884static int mv88e6xxx_mdio_write_indirect(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -0400885 int addr, int regnum, u16 val)
Andrew Lunnf3044682015-02-14 19:17:50 +0100886{
Andrew Lunn3898c142015-05-06 01:09:53 +0200887 int ret;
Andrew Lunnf3044682015-02-14 19:17:50 +0100888
Vivien Didelotfad09c72016-06-21 12:28:20 -0400889 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_SMI_DATA, val);
Andrew Lunn3898c142015-05-06 01:09:53 +0200890 if (ret < 0)
891 return ret;
892
Vivien Didelotfad09c72016-06-21 12:28:20 -0400893 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_SMI_OP,
Andrew Lunn3898c142015-05-06 01:09:53 +0200894 GLOBAL2_SMI_OP_22_WRITE | (addr << 5) |
895 regnum);
896
Vivien Didelotfad09c72016-06-21 12:28:20 -0400897 return mv88e6xxx_mdio_wait(chip);
Andrew Lunnf3044682015-02-14 19:17:50 +0100898}
899
Vivien Didelotf81ec902016-05-09 13:22:58 -0400900static int mv88e6xxx_get_eee(struct dsa_switch *ds, int port,
901 struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -0800902{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400903 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Guenter Roeck11b3b452015-03-06 22:23:51 -0800904 int reg;
905
Vivien Didelotfad09c72016-06-21 12:28:20 -0400906 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
Vivien Didelotaadbdb82016-05-09 13:22:44 -0400907 return -EOPNOTSUPP;
908
Vivien Didelotfad09c72016-06-21 12:28:20 -0400909 mutex_lock(&chip->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200910
Vivien Didelotfad09c72016-06-21 12:28:20 -0400911 reg = mv88e6xxx_mdio_read_indirect(chip, port, 16);
Guenter Roeck11b3b452015-03-06 22:23:51 -0800912 if (reg < 0)
Andrew Lunn2f40c692015-04-02 04:06:37 +0200913 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800914
915 e->eee_enabled = !!(reg & 0x0200);
916 e->tx_lpi_enabled = !!(reg & 0x0100);
917
Vivien Didelotfad09c72016-06-21 12:28:20 -0400918 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_STATUS);
Guenter Roeck11b3b452015-03-06 22:23:51 -0800919 if (reg < 0)
Andrew Lunn2f40c692015-04-02 04:06:37 +0200920 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800921
Andrew Lunncca8b132015-04-02 04:06:39 +0200922 e->eee_active = !!(reg & PORT_STATUS_EEE);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200923 reg = 0;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800924
Andrew Lunn2f40c692015-04-02 04:06:37 +0200925out:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400926 mutex_unlock(&chip->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200927 return reg;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800928}
929
Vivien Didelotf81ec902016-05-09 13:22:58 -0400930static int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
931 struct phy_device *phydev, struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -0800932{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400933 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200934 int reg;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800935 int ret;
936
Vivien Didelotfad09c72016-06-21 12:28:20 -0400937 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
Vivien Didelotaadbdb82016-05-09 13:22:44 -0400938 return -EOPNOTSUPP;
939
Vivien Didelotfad09c72016-06-21 12:28:20 -0400940 mutex_lock(&chip->reg_lock);
Guenter Roeck11b3b452015-03-06 22:23:51 -0800941
Vivien Didelotfad09c72016-06-21 12:28:20 -0400942 ret = mv88e6xxx_mdio_read_indirect(chip, port, 16);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200943 if (ret < 0)
944 goto out;
945
946 reg = ret & ~0x0300;
947 if (e->eee_enabled)
948 reg |= 0x0200;
949 if (e->tx_lpi_enabled)
950 reg |= 0x0100;
951
Vivien Didelotfad09c72016-06-21 12:28:20 -0400952 ret = mv88e6xxx_mdio_write_indirect(chip, port, 16, reg);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200953out:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400954 mutex_unlock(&chip->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200955
956 return ret;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800957}
958
Vivien Didelotfad09c72016-06-21 12:28:20 -0400959static int _mv88e6xxx_atu_cmd(struct mv88e6xxx_chip *chip, u16 fid, u16 cmd)
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700960{
961 int ret;
962
Vivien Didelotfad09c72016-06-21 12:28:20 -0400963 if (mv88e6xxx_has_fid_reg(chip)) {
964 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_FID,
965 fid);
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400966 if (ret < 0)
967 return ret;
Vivien Didelotfad09c72016-06-21 12:28:20 -0400968 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -0400969 /* ATU DBNum[7:4] are located in ATU Control 15:12 */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400970 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL);
Vivien Didelot11ea8092016-03-31 16:53:44 -0400971 if (ret < 0)
972 return ret;
973
Vivien Didelotfad09c72016-06-21 12:28:20 -0400974 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL,
Vivien Didelot11ea8092016-03-31 16:53:44 -0400975 (ret & 0xfff) |
976 ((fid << 8) & 0xf000));
977 if (ret < 0)
978 return ret;
979
980 /* ATU DBNum[3:0] are located in ATU Operation 3:0 */
981 cmd |= fid & 0xf;
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400982 }
983
Vivien Didelotfad09c72016-06-21 12:28:20 -0400984 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_OP, cmd);
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700985 if (ret < 0)
986 return ret;
987
Vivien Didelotfad09c72016-06-21 12:28:20 -0400988 return _mv88e6xxx_atu_wait(chip);
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700989}
990
Vivien Didelotfad09c72016-06-21 12:28:20 -0400991static int _mv88e6xxx_atu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot37705b72015-09-04 14:34:11 -0400992 struct mv88e6xxx_atu_entry *entry)
993{
994 u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK;
995
996 if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) {
997 unsigned int mask, shift;
998
999 if (entry->trunk) {
1000 data |= GLOBAL_ATU_DATA_TRUNK;
1001 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
1002 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
1003 } else {
1004 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
1005 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
1006 }
1007
1008 data |= (entry->portv_trunkid << shift) & mask;
1009 }
1010
Vivien Didelotfad09c72016-06-21 12:28:20 -04001011 return _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_DATA, data);
Vivien Didelot37705b72015-09-04 14:34:11 -04001012}
1013
Vivien Didelotfad09c72016-06-21 12:28:20 -04001014static int _mv88e6xxx_atu_flush_move(struct mv88e6xxx_chip *chip,
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001015 struct mv88e6xxx_atu_entry *entry,
1016 bool static_too)
1017{
1018 int op;
1019 int err;
1020
Vivien Didelotfad09c72016-06-21 12:28:20 -04001021 err = _mv88e6xxx_atu_wait(chip);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001022 if (err)
1023 return err;
1024
Vivien Didelotfad09c72016-06-21 12:28:20 -04001025 err = _mv88e6xxx_atu_data_write(chip, entry);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001026 if (err)
1027 return err;
1028
1029 if (entry->fid) {
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001030 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB :
1031 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB;
1032 } else {
1033 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL :
1034 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC;
1035 }
1036
Vivien Didelotfad09c72016-06-21 12:28:20 -04001037 return _mv88e6xxx_atu_cmd(chip, entry->fid, op);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001038}
1039
Vivien Didelotfad09c72016-06-21 12:28:20 -04001040static int _mv88e6xxx_atu_flush(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001041 u16 fid, bool static_too)
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001042{
1043 struct mv88e6xxx_atu_entry entry = {
1044 .fid = fid,
1045 .state = 0, /* EntryState bits must be 0 */
1046 };
1047
Vivien Didelotfad09c72016-06-21 12:28:20 -04001048 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001049}
1050
Vivien Didelotfad09c72016-06-21 12:28:20 -04001051static int _mv88e6xxx_atu_move(struct mv88e6xxx_chip *chip, u16 fid,
Andrew Lunn158bc062016-04-28 21:24:06 -04001052 int from_port, int to_port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001053{
1054 struct mv88e6xxx_atu_entry entry = {
1055 .trunk = false,
1056 .fid = fid,
1057 };
1058
1059 /* EntryState bits must be 0xF */
1060 entry.state = GLOBAL_ATU_DATA_STATE_MASK;
1061
1062 /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */
1063 entry.portv_trunkid = (to_port & 0x0f) << 4;
1064 entry.portv_trunkid |= from_port & 0x0f;
1065
Vivien Didelotfad09c72016-06-21 12:28:20 -04001066 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001067}
1068
Vivien Didelotfad09c72016-06-21 12:28:20 -04001069static int _mv88e6xxx_atu_remove(struct mv88e6xxx_chip *chip, u16 fid,
Andrew Lunn158bc062016-04-28 21:24:06 -04001070 int port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001071{
1072 /* Destination port 0xF means remove the entries */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001073 return _mv88e6xxx_atu_move(chip, fid, port, 0x0f, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001074}
1075
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001076static const char * const mv88e6xxx_port_state_names[] = {
1077 [PORT_CONTROL_STATE_DISABLED] = "Disabled",
1078 [PORT_CONTROL_STATE_BLOCKING] = "Blocking/Listening",
1079 [PORT_CONTROL_STATE_LEARNING] = "Learning",
1080 [PORT_CONTROL_STATE_FORWARDING] = "Forwarding",
1081};
1082
Vivien Didelotfad09c72016-06-21 12:28:20 -04001083static int _mv88e6xxx_port_state(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001084 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001085{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001086 struct dsa_switch *ds = chip->ds;
Geert Uytterhoevenc3ffe6d2015-04-16 20:49:14 +02001087 int reg, ret = 0;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001088 u8 oldstate;
1089
Vivien Didelotfad09c72016-06-21 12:28:20 -04001090 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL);
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001091 if (reg < 0)
1092 return reg;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001093
Andrew Lunncca8b132015-04-02 04:06:39 +02001094 oldstate = reg & PORT_CONTROL_STATE_MASK;
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001095
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001096 if (oldstate != state) {
1097 /* Flush forwarding database if we're moving a port
1098 * from Learning or Forwarding state to Disabled or
1099 * Blocking or Listening state.
1100 */
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001101 if ((oldstate == PORT_CONTROL_STATE_LEARNING ||
Vivien Didelot57d32312016-06-20 13:13:58 -04001102 oldstate == PORT_CONTROL_STATE_FORWARDING) &&
1103 (state == PORT_CONTROL_STATE_DISABLED ||
1104 state == PORT_CONTROL_STATE_BLOCKING)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001105 ret = _mv88e6xxx_atu_remove(chip, 0, port, false);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001106 if (ret)
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001107 return ret;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001108 }
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001109
Andrew Lunncca8b132015-04-02 04:06:39 +02001110 reg = (reg & ~PORT_CONTROL_STATE_MASK) | state;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001111 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL,
Andrew Lunncca8b132015-04-02 04:06:39 +02001112 reg);
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001113 if (ret)
1114 return ret;
1115
Andrew Lunnc8b09802016-06-04 21:16:57 +02001116 netdev_dbg(ds->ports[port].netdev, "PortState %s (was %s)\n",
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001117 mv88e6xxx_port_state_names[state],
1118 mv88e6xxx_port_state_names[oldstate]);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001119 }
1120
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001121 return ret;
1122}
1123
Vivien Didelotfad09c72016-06-21 12:28:20 -04001124static int _mv88e6xxx_port_based_vlan_map(struct mv88e6xxx_chip *chip, int port)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001125{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001126 struct net_device *bridge = chip->ports[port].bridge_dev;
1127 const u16 mask = (1 << chip->info->num_ports) - 1;
1128 struct dsa_switch *ds = chip->ds;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001129 u16 output_ports = 0;
Vivien Didelotede80982015-10-11 18:08:35 -04001130 int reg;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001131 int i;
1132
1133 /* allow CPU port or DSA link(s) to send frames to every port */
1134 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
1135 output_ports = mask;
1136 } else {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001137 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001138 /* allow sending frames to every group member */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001139 if (bridge && chip->ports[i].bridge_dev == bridge)
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001140 output_ports |= BIT(i);
1141
1142 /* allow sending frames to CPU port and DSA link(s) */
1143 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
1144 output_ports |= BIT(i);
1145 }
1146 }
1147
1148 /* prevent frames from going back out of the port they came in on */
1149 output_ports &= ~BIT(port);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001150
Vivien Didelotfad09c72016-06-21 12:28:20 -04001151 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_BASE_VLAN);
Vivien Didelotede80982015-10-11 18:08:35 -04001152 if (reg < 0)
1153 return reg;
1154
1155 reg &= ~mask;
1156 reg |= output_ports & mask;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001157
Vivien Didelotfad09c72016-06-21 12:28:20 -04001158 return _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_BASE_VLAN, reg);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001159}
1160
Vivien Didelotf81ec902016-05-09 13:22:58 -04001161static void mv88e6xxx_port_stp_state_set(struct dsa_switch *ds, int port,
1162 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001163{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001164 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001165 int stp_state;
Vivien Didelot553eb542016-05-13 20:38:23 -04001166 int err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001167
1168 switch (state) {
1169 case BR_STATE_DISABLED:
Andrew Lunncca8b132015-04-02 04:06:39 +02001170 stp_state = PORT_CONTROL_STATE_DISABLED;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001171 break;
1172 case BR_STATE_BLOCKING:
1173 case BR_STATE_LISTENING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001174 stp_state = PORT_CONTROL_STATE_BLOCKING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001175 break;
1176 case BR_STATE_LEARNING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001177 stp_state = PORT_CONTROL_STATE_LEARNING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001178 break;
1179 case BR_STATE_FORWARDING:
1180 default:
Andrew Lunncca8b132015-04-02 04:06:39 +02001181 stp_state = PORT_CONTROL_STATE_FORWARDING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001182 break;
1183 }
1184
Vivien Didelotfad09c72016-06-21 12:28:20 -04001185 mutex_lock(&chip->reg_lock);
1186 err = _mv88e6xxx_port_state(chip, port, stp_state);
1187 mutex_unlock(&chip->reg_lock);
Vivien Didelot553eb542016-05-13 20:38:23 -04001188
1189 if (err)
Andrew Lunnc8b09802016-06-04 21:16:57 +02001190 netdev_err(ds->ports[port].netdev,
1191 "failed to update state to %s\n",
Vivien Didelot553eb542016-05-13 20:38:23 -04001192 mv88e6xxx_port_state_names[stp_state]);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001193}
1194
Vivien Didelotfad09c72016-06-21 12:28:20 -04001195static int _mv88e6xxx_port_pvid(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001196 u16 *new, u16 *old)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001197{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001198 struct dsa_switch *ds = chip->ds;
Vivien Didelot5da96032016-03-07 18:24:39 -05001199 u16 pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001200 int ret;
1201
Vivien Didelotfad09c72016-06-21 12:28:20 -04001202 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_DEFAULT_VLAN);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001203 if (ret < 0)
1204 return ret;
1205
Vivien Didelot5da96032016-03-07 18:24:39 -05001206 pvid = ret & PORT_DEFAULT_VLAN_MASK;
1207
1208 if (new) {
1209 ret &= ~PORT_DEFAULT_VLAN_MASK;
1210 ret |= *new & PORT_DEFAULT_VLAN_MASK;
1211
Vivien Didelotfad09c72016-06-21 12:28:20 -04001212 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Vivien Didelot5da96032016-03-07 18:24:39 -05001213 PORT_DEFAULT_VLAN, ret);
1214 if (ret < 0)
1215 return ret;
1216
Andrew Lunnc8b09802016-06-04 21:16:57 +02001217 netdev_dbg(ds->ports[port].netdev,
1218 "DefaultVID %d (was %d)\n", *new, pvid);
Vivien Didelot5da96032016-03-07 18:24:39 -05001219 }
1220
1221 if (old)
1222 *old = pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001223
1224 return 0;
1225}
1226
Vivien Didelotfad09c72016-06-21 12:28:20 -04001227static int _mv88e6xxx_port_pvid_get(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001228 int port, u16 *pvid)
Vivien Didelot5da96032016-03-07 18:24:39 -05001229{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001230 return _mv88e6xxx_port_pvid(chip, port, NULL, pvid);
Vivien Didelot5da96032016-03-07 18:24:39 -05001231}
1232
Vivien Didelotfad09c72016-06-21 12:28:20 -04001233static int _mv88e6xxx_port_pvid_set(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001234 int port, u16 pvid)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001235{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001236 return _mv88e6xxx_port_pvid(chip, port, &pvid, NULL);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001237}
1238
Vivien Didelotfad09c72016-06-21 12:28:20 -04001239static int _mv88e6xxx_vtu_wait(struct mv88e6xxx_chip *chip)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001240{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001241 return _mv88e6xxx_wait(chip, REG_GLOBAL, GLOBAL_VTU_OP,
Vivien Didelot6b17e862015-08-13 12:52:18 -04001242 GLOBAL_VTU_OP_BUSY);
1243}
1244
Vivien Didelotfad09c72016-06-21 12:28:20 -04001245static int _mv88e6xxx_vtu_cmd(struct mv88e6xxx_chip *chip, u16 op)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001246{
1247 int ret;
1248
Vivien Didelotfad09c72016-06-21 12:28:20 -04001249 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_OP, op);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001250 if (ret < 0)
1251 return ret;
1252
Vivien Didelotfad09c72016-06-21 12:28:20 -04001253 return _mv88e6xxx_vtu_wait(chip);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001254}
1255
Vivien Didelotfad09c72016-06-21 12:28:20 -04001256static int _mv88e6xxx_vtu_stu_flush(struct mv88e6xxx_chip *chip)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001257{
1258 int ret;
1259
Vivien Didelotfad09c72016-06-21 12:28:20 -04001260 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001261 if (ret < 0)
1262 return ret;
1263
Vivien Didelotfad09c72016-06-21 12:28:20 -04001264 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_FLUSH_ALL);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001265}
1266
Vivien Didelotfad09c72016-06-21 12:28:20 -04001267static int _mv88e6xxx_vtu_stu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001268 struct mv88e6xxx_vtu_stu_entry *entry,
1269 unsigned int nibble_offset)
1270{
Vivien Didelotb8fee952015-08-13 12:52:19 -04001271 u16 regs[3];
1272 int i;
1273 int ret;
1274
1275 for (i = 0; i < 3; ++i) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001276 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001277 GLOBAL_VTU_DATA_0_3 + i);
1278 if (ret < 0)
1279 return ret;
1280
1281 regs[i] = ret;
1282 }
1283
Vivien Didelotfad09c72016-06-21 12:28:20 -04001284 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelotb8fee952015-08-13 12:52:19 -04001285 unsigned int shift = (i % 4) * 4 + nibble_offset;
1286 u16 reg = regs[i / 4];
1287
1288 entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
1289 }
1290
1291 return 0;
1292}
1293
Vivien Didelotfad09c72016-06-21 12:28:20 -04001294static int mv88e6xxx_vtu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001295 struct mv88e6xxx_vtu_stu_entry *entry)
1296{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001297 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 0);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001298}
1299
Vivien Didelotfad09c72016-06-21 12:28:20 -04001300static int mv88e6xxx_stu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001301 struct mv88e6xxx_vtu_stu_entry *entry)
1302{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001303 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 2);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001304}
1305
Vivien Didelotfad09c72016-06-21 12:28:20 -04001306static int _mv88e6xxx_vtu_stu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001307 struct mv88e6xxx_vtu_stu_entry *entry,
1308 unsigned int nibble_offset)
1309{
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001310 u16 regs[3] = { 0 };
1311 int i;
1312 int ret;
1313
Vivien Didelotfad09c72016-06-21 12:28:20 -04001314 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001315 unsigned int shift = (i % 4) * 4 + nibble_offset;
1316 u8 data = entry->data[i];
1317
1318 regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
1319 }
1320
1321 for (i = 0; i < 3; ++i) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001322 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001323 GLOBAL_VTU_DATA_0_3 + i, regs[i]);
1324 if (ret < 0)
1325 return ret;
1326 }
1327
1328 return 0;
1329}
1330
Vivien Didelotfad09c72016-06-21 12:28:20 -04001331static int mv88e6xxx_vtu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001332 struct mv88e6xxx_vtu_stu_entry *entry)
1333{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001334 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 0);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001335}
1336
Vivien Didelotfad09c72016-06-21 12:28:20 -04001337static int mv88e6xxx_stu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001338 struct mv88e6xxx_vtu_stu_entry *entry)
1339{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001340 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 2);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001341}
1342
Vivien Didelotfad09c72016-06-21 12:28:20 -04001343static int _mv88e6xxx_vtu_vid_write(struct mv88e6xxx_chip *chip, u16 vid)
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001344{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001345 return _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID,
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001346 vid & GLOBAL_VTU_VID_MASK);
1347}
1348
Vivien Didelotfad09c72016-06-21 12:28:20 -04001349static int _mv88e6xxx_vtu_getnext(struct mv88e6xxx_chip *chip,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001350 struct mv88e6xxx_vtu_stu_entry *entry)
1351{
1352 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1353 int ret;
1354
Vivien Didelotfad09c72016-06-21 12:28:20 -04001355 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001356 if (ret < 0)
1357 return ret;
1358
Vivien Didelotfad09c72016-06-21 12:28:20 -04001359 ret = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_VTU_GET_NEXT);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001360 if (ret < 0)
1361 return ret;
1362
Vivien Didelotfad09c72016-06-21 12:28:20 -04001363 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_VID);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001364 if (ret < 0)
1365 return ret;
1366
1367 next.vid = ret & GLOBAL_VTU_VID_MASK;
1368 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1369
1370 if (next.valid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001371 ret = mv88e6xxx_vtu_data_read(chip, &next);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001372 if (ret < 0)
1373 return ret;
1374
Vivien Didelotfad09c72016-06-21 12:28:20 -04001375 if (mv88e6xxx_has_fid_reg(chip)) {
1376 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001377 GLOBAL_VTU_FID);
1378 if (ret < 0)
1379 return ret;
1380
1381 next.fid = ret & GLOBAL_VTU_FID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001382 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001383 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1384 * VTU DBNum[3:0] are located in VTU Operation 3:0
1385 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001386 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
Vivien Didelot11ea8092016-03-31 16:53:44 -04001387 GLOBAL_VTU_OP);
1388 if (ret < 0)
1389 return ret;
1390
1391 next.fid = (ret & 0xf00) >> 4;
1392 next.fid |= ret & 0xf;
Vivien Didelot2e7bd5e2016-03-31 16:53:41 -04001393 }
Vivien Didelotb8fee952015-08-13 12:52:19 -04001394
Vivien Didelotfad09c72016-06-21 12:28:20 -04001395 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
1396 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001397 GLOBAL_VTU_SID);
1398 if (ret < 0)
1399 return ret;
1400
1401 next.sid = ret & GLOBAL_VTU_SID_MASK;
1402 }
1403 }
1404
1405 *entry = next;
1406 return 0;
1407}
1408
Vivien Didelotf81ec902016-05-09 13:22:58 -04001409static int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
1410 struct switchdev_obj_port_vlan *vlan,
1411 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001412{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001413 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001414 struct mv88e6xxx_vtu_stu_entry next;
1415 u16 pvid;
1416 int err;
1417
Vivien Didelotfad09c72016-06-21 12:28:20 -04001418 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001419 return -EOPNOTSUPP;
1420
Vivien Didelotfad09c72016-06-21 12:28:20 -04001421 mutex_lock(&chip->reg_lock);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001422
Vivien Didelotfad09c72016-06-21 12:28:20 -04001423 err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001424 if (err)
1425 goto unlock;
1426
Vivien Didelotfad09c72016-06-21 12:28:20 -04001427 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001428 if (err)
1429 goto unlock;
1430
1431 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001432 err = _mv88e6xxx_vtu_getnext(chip, &next);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001433 if (err)
1434 break;
1435
1436 if (!next.valid)
1437 break;
1438
1439 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1440 continue;
1441
1442 /* reinit and dump this VLAN obj */
Vivien Didelot57d32312016-06-20 13:13:58 -04001443 vlan->vid_begin = next.vid;
1444 vlan->vid_end = next.vid;
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001445 vlan->flags = 0;
1446
1447 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1448 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1449
1450 if (next.vid == pvid)
1451 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1452
1453 err = cb(&vlan->obj);
1454 if (err)
1455 break;
1456 } while (next.vid < GLOBAL_VTU_VID_MASK);
1457
1458unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001459 mutex_unlock(&chip->reg_lock);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001460
1461 return err;
1462}
1463
Vivien Didelotfad09c72016-06-21 12:28:20 -04001464static int _mv88e6xxx_vtu_loadpurge(struct mv88e6xxx_chip *chip,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001465 struct mv88e6xxx_vtu_stu_entry *entry)
1466{
Vivien Didelot11ea8092016-03-31 16:53:44 -04001467 u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001468 u16 reg = 0;
1469 int ret;
1470
Vivien Didelotfad09c72016-06-21 12:28:20 -04001471 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001472 if (ret < 0)
1473 return ret;
1474
1475 if (!entry->valid)
1476 goto loadpurge;
1477
1478 /* Write port member tags */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001479 ret = mv88e6xxx_vtu_data_write(chip, entry);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001480 if (ret < 0)
1481 return ret;
1482
Vivien Didelotfad09c72016-06-21 12:28:20 -04001483 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001484 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001485 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID,
1486 reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001487 if (ret < 0)
1488 return ret;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001489 }
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001490
Vivien Didelotfad09c72016-06-21 12:28:20 -04001491 if (mv88e6xxx_has_fid_reg(chip)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001492 reg = entry->fid & GLOBAL_VTU_FID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001493 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_FID,
1494 reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001495 if (ret < 0)
1496 return ret;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001497 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001498 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1499 * VTU DBNum[3:0] are located in VTU Operation 3:0
1500 */
1501 op |= (entry->fid & 0xf0) << 8;
1502 op |= entry->fid & 0xf;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001503 }
1504
1505 reg = GLOBAL_VTU_VID_VALID;
1506loadpurge:
1507 reg |= entry->vid & GLOBAL_VTU_VID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001508 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID, reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001509 if (ret < 0)
1510 return ret;
1511
Vivien Didelotfad09c72016-06-21 12:28:20 -04001512 return _mv88e6xxx_vtu_cmd(chip, op);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001513}
1514
Vivien Didelotfad09c72016-06-21 12:28:20 -04001515static int _mv88e6xxx_stu_getnext(struct mv88e6xxx_chip *chip, u8 sid,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001516 struct mv88e6xxx_vtu_stu_entry *entry)
1517{
1518 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1519 int ret;
1520
Vivien Didelotfad09c72016-06-21 12:28:20 -04001521 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001522 if (ret < 0)
1523 return ret;
1524
Vivien Didelotfad09c72016-06-21 12:28:20 -04001525 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001526 sid & GLOBAL_VTU_SID_MASK);
1527 if (ret < 0)
1528 return ret;
1529
Vivien Didelotfad09c72016-06-21 12:28:20 -04001530 ret = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_GET_NEXT);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001531 if (ret < 0)
1532 return ret;
1533
Vivien Didelotfad09c72016-06-21 12:28:20 -04001534 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_SID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001535 if (ret < 0)
1536 return ret;
1537
1538 next.sid = ret & GLOBAL_VTU_SID_MASK;
1539
Vivien Didelotfad09c72016-06-21 12:28:20 -04001540 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_VID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001541 if (ret < 0)
1542 return ret;
1543
1544 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1545
1546 if (next.valid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001547 ret = mv88e6xxx_stu_data_read(chip, &next);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001548 if (ret < 0)
1549 return ret;
1550 }
1551
1552 *entry = next;
1553 return 0;
1554}
1555
Vivien Didelotfad09c72016-06-21 12:28:20 -04001556static int _mv88e6xxx_stu_loadpurge(struct mv88e6xxx_chip *chip,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001557 struct mv88e6xxx_vtu_stu_entry *entry)
1558{
1559 u16 reg = 0;
1560 int ret;
1561
Vivien Didelotfad09c72016-06-21 12:28:20 -04001562 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001563 if (ret < 0)
1564 return ret;
1565
1566 if (!entry->valid)
1567 goto loadpurge;
1568
1569 /* Write port states */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001570 ret = mv88e6xxx_stu_data_write(chip, entry);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001571 if (ret < 0)
1572 return ret;
1573
1574 reg = GLOBAL_VTU_VID_VALID;
1575loadpurge:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001576 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID, reg);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001577 if (ret < 0)
1578 return ret;
1579
1580 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001581 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID, reg);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001582 if (ret < 0)
1583 return ret;
1584
Vivien Didelotfad09c72016-06-21 12:28:20 -04001585 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_LOAD_PURGE);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001586}
1587
Vivien Didelotfad09c72016-06-21 12:28:20 -04001588static int _mv88e6xxx_port_fid(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001589 u16 *new, u16 *old)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001590{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001591 struct dsa_switch *ds = chip->ds;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001592 u16 upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001593 u16 fid;
1594 int ret;
1595
Vivien Didelotfad09c72016-06-21 12:28:20 -04001596 if (mv88e6xxx_num_databases(chip) == 4096)
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001597 upper_mask = 0xff;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001598 else if (mv88e6xxx_num_databases(chip) == 256)
Vivien Didelot11ea8092016-03-31 16:53:44 -04001599 upper_mask = 0xf;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001600 else
1601 return -EOPNOTSUPP;
1602
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001603 /* Port's default FID bits 3:0 are located in reg 0x06, offset 12 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001604 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_BASE_VLAN);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001605 if (ret < 0)
1606 return ret;
1607
1608 fid = (ret & PORT_BASE_VLAN_FID_3_0_MASK) >> 12;
1609
1610 if (new) {
1611 ret &= ~PORT_BASE_VLAN_FID_3_0_MASK;
1612 ret |= (*new << 12) & PORT_BASE_VLAN_FID_3_0_MASK;
1613
Vivien Didelotfad09c72016-06-21 12:28:20 -04001614 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_BASE_VLAN,
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001615 ret);
1616 if (ret < 0)
1617 return ret;
1618 }
1619
1620 /* Port's default FID bits 11:4 are located in reg 0x05, offset 0 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001621 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL_1);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001622 if (ret < 0)
1623 return ret;
1624
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001625 fid |= (ret & upper_mask) << 4;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001626
1627 if (new) {
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001628 ret &= ~upper_mask;
1629 ret |= (*new >> 4) & upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001630
Vivien Didelotfad09c72016-06-21 12:28:20 -04001631 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_1,
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001632 ret);
1633 if (ret < 0)
1634 return ret;
1635
Andrew Lunnc8b09802016-06-04 21:16:57 +02001636 netdev_dbg(ds->ports[port].netdev,
1637 "FID %d (was %d)\n", *new, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001638 }
1639
1640 if (old)
1641 *old = fid;
1642
1643 return 0;
1644}
1645
Vivien Didelotfad09c72016-06-21 12:28:20 -04001646static int _mv88e6xxx_port_fid_get(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001647 int port, u16 *fid)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001648{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001649 return _mv88e6xxx_port_fid(chip, port, NULL, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001650}
1651
Vivien Didelotfad09c72016-06-21 12:28:20 -04001652static int _mv88e6xxx_port_fid_set(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001653 int port, u16 fid)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001654{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001655 return _mv88e6xxx_port_fid(chip, port, &fid, NULL);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001656}
1657
Vivien Didelotfad09c72016-06-21 12:28:20 -04001658static int _mv88e6xxx_fid_new(struct mv88e6xxx_chip *chip, u16 *fid)
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001659{
1660 DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
1661 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001662 int i, err;
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001663
1664 bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);
1665
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001666 /* Set every FID bit used by the (un)bridged ports */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001667 for (i = 0; i < chip->info->num_ports; ++i) {
1668 err = _mv88e6xxx_port_fid_get(chip, i, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001669 if (err)
1670 return err;
1671
1672 set_bit(*fid, fid_bitmap);
1673 }
1674
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001675 /* Set every FID bit used by the VLAN entries */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001676 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001677 if (err)
1678 return err;
1679
1680 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001681 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001682 if (err)
1683 return err;
1684
1685 if (!vlan.valid)
1686 break;
1687
1688 set_bit(vlan.fid, fid_bitmap);
1689 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
1690
1691 /* The reset value 0x000 is used to indicate that multiple address
1692 * databases are not needed. Return the next positive available.
1693 */
1694 *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1);
Vivien Didelotfad09c72016-06-21 12:28:20 -04001695 if (unlikely(*fid >= mv88e6xxx_num_databases(chip)))
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001696 return -ENOSPC;
1697
1698 /* Clear the database */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001699 return _mv88e6xxx_atu_flush(chip, *fid, true);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001700}
1701
Vivien Didelotfad09c72016-06-21 12:28:20 -04001702static int _mv88e6xxx_vtu_new(struct mv88e6xxx_chip *chip, u16 vid,
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001703 struct mv88e6xxx_vtu_stu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001704{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001705 struct dsa_switch *ds = chip->ds;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001706 struct mv88e6xxx_vtu_stu_entry vlan = {
1707 .valid = true,
1708 .vid = vid,
1709 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001710 int i, err;
1711
Vivien Didelotfad09c72016-06-21 12:28:20 -04001712 err = _mv88e6xxx_fid_new(chip, &vlan.fid);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001713 if (err)
1714 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001715
Vivien Didelot3d131f02015-11-03 10:52:52 -05001716 /* exclude all ports except the CPU and DSA ports */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001717 for (i = 0; i < chip->info->num_ports; ++i)
Vivien Didelot3d131f02015-11-03 10:52:52 -05001718 vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)
1719 ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED
1720 : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001721
Vivien Didelotfad09c72016-06-21 12:28:20 -04001722 if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
1723 mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip)) {
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001724 struct mv88e6xxx_vtu_stu_entry vstp;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001725
1726 /* Adding a VTU entry requires a valid STU entry. As VSTP is not
1727 * implemented, only one STU entry is needed to cover all VTU
1728 * entries. Thus, validate the SID 0.
1729 */
1730 vlan.sid = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001731 err = _mv88e6xxx_stu_getnext(chip, GLOBAL_VTU_SID_MASK, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001732 if (err)
1733 return err;
1734
1735 if (vstp.sid != vlan.sid || !vstp.valid) {
1736 memset(&vstp, 0, sizeof(vstp));
1737 vstp.valid = true;
1738 vstp.sid = vlan.sid;
1739
Vivien Didelotfad09c72016-06-21 12:28:20 -04001740 err = _mv88e6xxx_stu_loadpurge(chip, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001741 if (err)
1742 return err;
1743 }
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001744 }
1745
1746 *entry = vlan;
1747 return 0;
1748}
1749
Vivien Didelotfad09c72016-06-21 12:28:20 -04001750static int _mv88e6xxx_vtu_get(struct mv88e6xxx_chip *chip, u16 vid,
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001751 struct mv88e6xxx_vtu_stu_entry *entry, bool creat)
1752{
1753 int err;
1754
1755 if (!vid)
1756 return -EINVAL;
1757
Vivien Didelotfad09c72016-06-21 12:28:20 -04001758 err = _mv88e6xxx_vtu_vid_write(chip, vid - 1);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001759 if (err)
1760 return err;
1761
Vivien Didelotfad09c72016-06-21 12:28:20 -04001762 err = _mv88e6xxx_vtu_getnext(chip, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001763 if (err)
1764 return err;
1765
1766 if (entry->vid != vid || !entry->valid) {
1767 if (!creat)
1768 return -EOPNOTSUPP;
1769 /* -ENOENT would've been more appropriate, but switchdev expects
1770 * -EOPNOTSUPP to inform bridge about an eventual software VLAN.
1771 */
1772
Vivien Didelotfad09c72016-06-21 12:28:20 -04001773 err = _mv88e6xxx_vtu_new(chip, vid, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001774 }
1775
1776 return err;
1777}
1778
Vivien Didelotda9c3592016-02-12 12:09:40 -05001779static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
1780 u16 vid_begin, u16 vid_end)
1781{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001782 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001783 struct mv88e6xxx_vtu_stu_entry vlan;
1784 int i, err;
1785
1786 if (!vid_begin)
1787 return -EOPNOTSUPP;
1788
Vivien Didelotfad09c72016-06-21 12:28:20 -04001789 mutex_lock(&chip->reg_lock);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001790
Vivien Didelotfad09c72016-06-21 12:28:20 -04001791 err = _mv88e6xxx_vtu_vid_write(chip, vid_begin - 1);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001792 if (err)
1793 goto unlock;
1794
1795 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001796 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001797 if (err)
1798 goto unlock;
1799
1800 if (!vlan.valid)
1801 break;
1802
1803 if (vlan.vid > vid_end)
1804 break;
1805
Vivien Didelotfad09c72016-06-21 12:28:20 -04001806 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelotda9c3592016-02-12 12:09:40 -05001807 if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i))
1808 continue;
1809
1810 if (vlan.data[i] ==
1811 GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1812 continue;
1813
Vivien Didelotfad09c72016-06-21 12:28:20 -04001814 if (chip->ports[i].bridge_dev ==
1815 chip->ports[port].bridge_dev)
Vivien Didelotda9c3592016-02-12 12:09:40 -05001816 break; /* same bridge, check next VLAN */
1817
Andrew Lunnc8b09802016-06-04 21:16:57 +02001818 netdev_warn(ds->ports[port].netdev,
Vivien Didelotda9c3592016-02-12 12:09:40 -05001819 "hardware VLAN %d already used by %s\n",
1820 vlan.vid,
Vivien Didelotfad09c72016-06-21 12:28:20 -04001821 netdev_name(chip->ports[i].bridge_dev));
Vivien Didelotda9c3592016-02-12 12:09:40 -05001822 err = -EOPNOTSUPP;
1823 goto unlock;
1824 }
1825 } while (vlan.vid < vid_end);
1826
1827unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001828 mutex_unlock(&chip->reg_lock);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001829
1830 return err;
1831}
1832
Vivien Didelot214cdb92016-02-26 13:16:08 -05001833static const char * const mv88e6xxx_port_8021q_mode_names[] = {
1834 [PORT_CONTROL_2_8021Q_DISABLED] = "Disabled",
1835 [PORT_CONTROL_2_8021Q_FALLBACK] = "Fallback",
1836 [PORT_CONTROL_2_8021Q_CHECK] = "Check",
1837 [PORT_CONTROL_2_8021Q_SECURE] = "Secure",
1838};
1839
Vivien Didelotf81ec902016-05-09 13:22:58 -04001840static int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port,
1841 bool vlan_filtering)
Vivien Didelot214cdb92016-02-26 13:16:08 -05001842{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001843 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelot214cdb92016-02-26 13:16:08 -05001844 u16 old, new = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE :
1845 PORT_CONTROL_2_8021Q_DISABLED;
1846 int ret;
1847
Vivien Didelotfad09c72016-06-21 12:28:20 -04001848 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001849 return -EOPNOTSUPP;
1850
Vivien Didelotfad09c72016-06-21 12:28:20 -04001851 mutex_lock(&chip->reg_lock);
Vivien Didelot214cdb92016-02-26 13:16:08 -05001852
Vivien Didelotfad09c72016-06-21 12:28:20 -04001853 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL_2);
Vivien Didelot214cdb92016-02-26 13:16:08 -05001854 if (ret < 0)
1855 goto unlock;
1856
1857 old = ret & PORT_CONTROL_2_8021Q_MASK;
1858
Vivien Didelot5220ef12016-03-07 18:24:52 -05001859 if (new != old) {
1860 ret &= ~PORT_CONTROL_2_8021Q_MASK;
1861 ret |= new & PORT_CONTROL_2_8021Q_MASK;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001862
Vivien Didelotfad09c72016-06-21 12:28:20 -04001863 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_2,
Vivien Didelot5220ef12016-03-07 18:24:52 -05001864 ret);
1865 if (ret < 0)
1866 goto unlock;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001867
Andrew Lunnc8b09802016-06-04 21:16:57 +02001868 netdev_dbg(ds->ports[port].netdev, "802.1Q Mode %s (was %s)\n",
Vivien Didelot5220ef12016-03-07 18:24:52 -05001869 mv88e6xxx_port_8021q_mode_names[new],
1870 mv88e6xxx_port_8021q_mode_names[old]);
1871 }
1872
1873 ret = 0;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001874unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001875 mutex_unlock(&chip->reg_lock);
Vivien Didelot214cdb92016-02-26 13:16:08 -05001876
1877 return ret;
1878}
1879
Vivien Didelot57d32312016-06-20 13:13:58 -04001880static int
1881mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
1882 const struct switchdev_obj_port_vlan *vlan,
1883 struct switchdev_trans *trans)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001884{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001885 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001886 int err;
1887
Vivien Didelotfad09c72016-06-21 12:28:20 -04001888 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001889 return -EOPNOTSUPP;
1890
Vivien Didelotda9c3592016-02-12 12:09:40 -05001891 /* If the requested port doesn't belong to the same bridge as the VLAN
1892 * members, do not support it (yet) and fallback to software VLAN.
1893 */
1894 err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin,
1895 vlan->vid_end);
1896 if (err)
1897 return err;
1898
Vivien Didelot76e398a2015-11-01 12:33:55 -05001899 /* We don't need any dynamic resource from the kernel (yet),
1900 * so skip the prepare phase.
1901 */
1902 return 0;
1903}
1904
Vivien Didelotfad09c72016-06-21 12:28:20 -04001905static int _mv88e6xxx_port_vlan_add(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001906 u16 vid, bool untagged)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001907{
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001908 struct mv88e6xxx_vtu_stu_entry vlan;
1909 int err;
1910
Vivien Didelotfad09c72016-06-21 12:28:20 -04001911 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, true);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001912 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001913 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001914
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001915 vlan.data[port] = untagged ?
1916 GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
1917 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
1918
Vivien Didelotfad09c72016-06-21 12:28:20 -04001919 return _mv88e6xxx_vtu_loadpurge(chip, &vlan);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001920}
1921
Vivien Didelotf81ec902016-05-09 13:22:58 -04001922static void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
1923 const struct switchdev_obj_port_vlan *vlan,
1924 struct switchdev_trans *trans)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001925{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001926 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001927 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1928 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1929 u16 vid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001930
Vivien Didelotfad09c72016-06-21 12:28:20 -04001931 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001932 return;
1933
Vivien Didelotfad09c72016-06-21 12:28:20 -04001934 mutex_lock(&chip->reg_lock);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001935
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001936 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
Vivien Didelotfad09c72016-06-21 12:28:20 -04001937 if (_mv88e6xxx_port_vlan_add(chip, port, vid, untagged))
Andrew Lunnc8b09802016-06-04 21:16:57 +02001938 netdev_err(ds->ports[port].netdev,
1939 "failed to add VLAN %d%c\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001940 vid, untagged ? 'u' : 't');
Vivien Didelot76e398a2015-11-01 12:33:55 -05001941
Vivien Didelotfad09c72016-06-21 12:28:20 -04001942 if (pvid && _mv88e6xxx_port_pvid_set(chip, port, vlan->vid_end))
Andrew Lunnc8b09802016-06-04 21:16:57 +02001943 netdev_err(ds->ports[port].netdev, "failed to set PVID %d\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001944 vlan->vid_end);
1945
Vivien Didelotfad09c72016-06-21 12:28:20 -04001946 mutex_unlock(&chip->reg_lock);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001947}
1948
Vivien Didelotfad09c72016-06-21 12:28:20 -04001949static int _mv88e6xxx_port_vlan_del(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001950 int port, u16 vid)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001951{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001952 struct dsa_switch *ds = chip->ds;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001953 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001954 int i, err;
1955
Vivien Didelotfad09c72016-06-21 12:28:20 -04001956 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001957 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001958 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001959
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001960 /* Tell switchdev if this VLAN is handled in software */
1961 if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
Vivien Didelot3c06f082016-02-05 14:04:39 -05001962 return -EOPNOTSUPP;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001963
1964 vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
1965
1966 /* keep the VLAN unless all ports are excluded */
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001967 vlan.valid = false;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001968 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelot3d131f02015-11-03 10:52:52 -05001969 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001970 continue;
1971
1972 if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001973 vlan.valid = true;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001974 break;
1975 }
1976 }
1977
Vivien Didelotfad09c72016-06-21 12:28:20 -04001978 err = _mv88e6xxx_vtu_loadpurge(chip, &vlan);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001979 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001980 return err;
1981
Vivien Didelotfad09c72016-06-21 12:28:20 -04001982 return _mv88e6xxx_atu_remove(chip, vlan.fid, port, false);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001983}
1984
Vivien Didelotf81ec902016-05-09 13:22:58 -04001985static int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
1986 const struct switchdev_obj_port_vlan *vlan)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001987{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001988 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001989 u16 pvid, vid;
1990 int err = 0;
1991
Vivien Didelotfad09c72016-06-21 12:28:20 -04001992 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001993 return -EOPNOTSUPP;
1994
Vivien Didelotfad09c72016-06-21 12:28:20 -04001995 mutex_lock(&chip->reg_lock);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001996
Vivien Didelotfad09c72016-06-21 12:28:20 -04001997 err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001998 if (err)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001999 goto unlock;
2000
Vivien Didelot76e398a2015-11-01 12:33:55 -05002001 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002002 err = _mv88e6xxx_port_vlan_del(chip, port, vid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002003 if (err)
2004 goto unlock;
2005
2006 if (vid == pvid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002007 err = _mv88e6xxx_port_pvid_set(chip, port, 0);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002008 if (err)
2009 goto unlock;
2010 }
2011 }
2012
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002013unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002014 mutex_unlock(&chip->reg_lock);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002015
2016 return err;
2017}
2018
Vivien Didelotfad09c72016-06-21 12:28:20 -04002019static int _mv88e6xxx_atu_mac_write(struct mv88e6xxx_chip *chip,
Vivien Didelotc5723ac2015-08-10 09:09:48 -04002020 const unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002021{
2022 int i, ret;
2023
2024 for (i = 0; i < 3; i++) {
Andrew Lunncca8b132015-04-02 04:06:39 +02002025 ret = _mv88e6xxx_reg_write(
Vivien Didelotfad09c72016-06-21 12:28:20 -04002026 chip, REG_GLOBAL, GLOBAL_ATU_MAC_01 + i,
Andrew Lunncca8b132015-04-02 04:06:39 +02002027 (addr[i * 2] << 8) | addr[i * 2 + 1]);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002028 if (ret < 0)
2029 return ret;
2030 }
2031
2032 return 0;
2033}
2034
Vivien Didelotfad09c72016-06-21 12:28:20 -04002035static int _mv88e6xxx_atu_mac_read(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04002036 unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002037{
2038 int i, ret;
2039
2040 for (i = 0; i < 3; i++) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002041 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
Andrew Lunncca8b132015-04-02 04:06:39 +02002042 GLOBAL_ATU_MAC_01 + i);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002043 if (ret < 0)
2044 return ret;
2045 addr[i * 2] = ret >> 8;
2046 addr[i * 2 + 1] = ret & 0xff;
2047 }
2048
2049 return 0;
2050}
2051
Vivien Didelotfad09c72016-06-21 12:28:20 -04002052static int _mv88e6xxx_atu_load(struct mv88e6xxx_chip *chip,
Vivien Didelotfd231c82015-08-10 09:09:50 -04002053 struct mv88e6xxx_atu_entry *entry)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002054{
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002055 int ret;
2056
Vivien Didelotfad09c72016-06-21 12:28:20 -04002057 ret = _mv88e6xxx_atu_wait(chip);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002058 if (ret < 0)
2059 return ret;
2060
Vivien Didelotfad09c72016-06-21 12:28:20 -04002061 ret = _mv88e6xxx_atu_mac_write(chip, entry->mac);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002062 if (ret < 0)
2063 return ret;
2064
Vivien Didelotfad09c72016-06-21 12:28:20 -04002065 ret = _mv88e6xxx_atu_data_write(chip, entry);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002066 if (ret < 0)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002067 return ret;
2068
Vivien Didelotfad09c72016-06-21 12:28:20 -04002069 return _mv88e6xxx_atu_cmd(chip, entry->fid, GLOBAL_ATU_OP_LOAD_DB);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002070}
David S. Millercdf09692015-08-11 12:00:37 -07002071
Vivien Didelotfad09c72016-06-21 12:28:20 -04002072static int _mv88e6xxx_port_fdb_load(struct mv88e6xxx_chip *chip, int port,
Vivien Didelotfd231c82015-08-10 09:09:50 -04002073 const unsigned char *addr, u16 vid,
2074 u8 state)
2075{
2076 struct mv88e6xxx_atu_entry entry = { 0 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002077 struct mv88e6xxx_vtu_stu_entry vlan;
2078 int err;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002079
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002080 /* Null VLAN ID corresponds to the port private database */
2081 if (vid == 0)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002082 err = _mv88e6xxx_port_fid_get(chip, port, &vlan.fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002083 else
Vivien Didelotfad09c72016-06-21 12:28:20 -04002084 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002085 if (err)
2086 return err;
2087
2088 entry.fid = vlan.fid;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002089 entry.state = state;
2090 ether_addr_copy(entry.mac, addr);
2091 if (state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2092 entry.trunk = false;
2093 entry.portv_trunkid = BIT(port);
2094 }
2095
Vivien Didelotfad09c72016-06-21 12:28:20 -04002096 return _mv88e6xxx_atu_load(chip, &entry);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002097}
2098
Vivien Didelotf81ec902016-05-09 13:22:58 -04002099static int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
2100 const struct switchdev_obj_port_fdb *fdb,
2101 struct switchdev_trans *trans)
Vivien Didelot146a3202015-10-08 11:35:12 -04002102{
2103 /* We don't need any dynamic resource from the kernel (yet),
2104 * so skip the prepare phase.
2105 */
2106 return 0;
2107}
2108
Vivien Didelotf81ec902016-05-09 13:22:58 -04002109static void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
2110 const struct switchdev_obj_port_fdb *fdb,
2111 struct switchdev_trans *trans)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002112{
Vivien Didelot1f36faf2015-10-08 11:35:13 -04002113 int state = is_multicast_ether_addr(fdb->addr) ?
David S. Millercdf09692015-08-11 12:00:37 -07002114 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2115 GLOBAL_ATU_DATA_STATE_UC_STATIC;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002116 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelot6630e232015-08-06 01:44:07 -04002117
Vivien Didelotfad09c72016-06-21 12:28:20 -04002118 mutex_lock(&chip->reg_lock);
2119 if (_mv88e6xxx_port_fdb_load(chip, port, fdb->addr, fdb->vid, state))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002120 netdev_err(ds->ports[port].netdev,
2121 "failed to load MAC address\n");
Vivien Didelotfad09c72016-06-21 12:28:20 -04002122 mutex_unlock(&chip->reg_lock);
David S. Millercdf09692015-08-11 12:00:37 -07002123}
2124
Vivien Didelotf81ec902016-05-09 13:22:58 -04002125static int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
2126 const struct switchdev_obj_port_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -07002127{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002128 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
David S. Millercdf09692015-08-11 12:00:37 -07002129 int ret;
2130
Vivien Didelotfad09c72016-06-21 12:28:20 -04002131 mutex_lock(&chip->reg_lock);
2132 ret = _mv88e6xxx_port_fdb_load(chip, port, fdb->addr, fdb->vid,
David S. Millercdf09692015-08-11 12:00:37 -07002133 GLOBAL_ATU_DATA_STATE_UNUSED);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002134 mutex_unlock(&chip->reg_lock);
David S. Millercdf09692015-08-11 12:00:37 -07002135
2136 return ret;
2137}
2138
Vivien Didelotfad09c72016-06-21 12:28:20 -04002139static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
Vivien Didelot1d194042015-08-10 09:09:51 -04002140 struct mv88e6xxx_atu_entry *entry)
David S. Millercdf09692015-08-11 12:00:37 -07002141{
Vivien Didelot1d194042015-08-10 09:09:51 -04002142 struct mv88e6xxx_atu_entry next = { 0 };
2143 int ret;
2144
2145 next.fid = fid;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002146
Vivien Didelotfad09c72016-06-21 12:28:20 -04002147 ret = _mv88e6xxx_atu_wait(chip);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002148 if (ret < 0)
2149 return ret;
2150
Vivien Didelotfad09c72016-06-21 12:28:20 -04002151 ret = _mv88e6xxx_atu_cmd(chip, fid, GLOBAL_ATU_OP_GET_NEXT_DB);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002152 if (ret < 0)
2153 return ret;
2154
Vivien Didelotfad09c72016-06-21 12:28:20 -04002155 ret = _mv88e6xxx_atu_mac_read(chip, next.mac);
Vivien Didelot1d194042015-08-10 09:09:51 -04002156 if (ret < 0)
2157 return ret;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002158
Vivien Didelotfad09c72016-06-21 12:28:20 -04002159 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_ATU_DATA);
Vivien Didelot1d194042015-08-10 09:09:51 -04002160 if (ret < 0)
2161 return ret;
2162
2163 next.state = ret & GLOBAL_ATU_DATA_STATE_MASK;
2164 if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2165 unsigned int mask, shift;
2166
2167 if (ret & GLOBAL_ATU_DATA_TRUNK) {
2168 next.trunk = true;
2169 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
2170 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
2171 } else {
2172 next.trunk = false;
2173 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
2174 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
2175 }
2176
2177 next.portv_trunkid = (ret & mask) >> shift;
2178 }
2179
2180 *entry = next;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002181 return 0;
2182}
2183
Vivien Didelotfad09c72016-06-21 12:28:20 -04002184static int _mv88e6xxx_port_fdb_dump_one(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04002185 u16 fid, u16 vid, int port,
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002186 struct switchdev_obj_port_fdb *fdb,
2187 int (*cb)(struct switchdev_obj *obj))
2188{
2189 struct mv88e6xxx_atu_entry addr = {
2190 .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2191 };
2192 int err;
2193
Vivien Didelotfad09c72016-06-21 12:28:20 -04002194 err = _mv88e6xxx_atu_mac_write(chip, addr.mac);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002195 if (err)
2196 return err;
2197
2198 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002199 err = _mv88e6xxx_atu_getnext(chip, fid, &addr);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002200 if (err)
2201 break;
2202
2203 if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2204 break;
2205
2206 if (!addr.trunk && addr.portv_trunkid & BIT(port)) {
2207 bool is_static = addr.state ==
2208 (is_multicast_ether_addr(addr.mac) ?
2209 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2210 GLOBAL_ATU_DATA_STATE_UC_STATIC);
2211
2212 fdb->vid = vid;
2213 ether_addr_copy(fdb->addr, addr.mac);
2214 fdb->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
2215
2216 err = cb(&fdb->obj);
2217 if (err)
2218 break;
2219 }
2220 } while (!is_broadcast_ether_addr(addr.mac));
2221
2222 return err;
2223}
2224
Vivien Didelotf81ec902016-05-09 13:22:58 -04002225static int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
2226 struct switchdev_obj_port_fdb *fdb,
2227 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotf33475b2015-10-22 09:34:41 -04002228{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002229 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002230 struct mv88e6xxx_vtu_stu_entry vlan = {
2231 .vid = GLOBAL_VTU_VID_MASK, /* all ones */
2232 };
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002233 u16 fid;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002234 int err;
2235
Vivien Didelotfad09c72016-06-21 12:28:20 -04002236 mutex_lock(&chip->reg_lock);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002237
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002238 /* Dump port's default Filtering Information Database (VLAN ID 0) */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002239 err = _mv88e6xxx_port_fid_get(chip, port, &fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002240 if (err)
2241 goto unlock;
2242
Vivien Didelotfad09c72016-06-21 12:28:20 -04002243 err = _mv88e6xxx_port_fdb_dump_one(chip, fid, 0, port, fdb, cb);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002244 if (err)
2245 goto unlock;
2246
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002247 /* Dump VLANs' Filtering Information Databases */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002248 err = _mv88e6xxx_vtu_vid_write(chip, vlan.vid);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002249 if (err)
2250 goto unlock;
2251
2252 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002253 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002254 if (err)
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002255 break;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002256
2257 if (!vlan.valid)
2258 break;
2259
Vivien Didelotfad09c72016-06-21 12:28:20 -04002260 err = _mv88e6xxx_port_fdb_dump_one(chip, vlan.fid, vlan.vid,
2261 port, fdb, cb);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002262 if (err)
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002263 break;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002264 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
2265
2266unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002267 mutex_unlock(&chip->reg_lock);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002268
2269 return err;
2270}
2271
Vivien Didelotf81ec902016-05-09 13:22:58 -04002272static int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
2273 struct net_device *bridge)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002274{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002275 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Colin Ian King1d9619d2016-04-25 23:11:22 +01002276 int i, err = 0;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002277
Vivien Didelotfad09c72016-06-21 12:28:20 -04002278 mutex_lock(&chip->reg_lock);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002279
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002280 /* Assign the bridge and remap each port's VLANTable */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002281 chip->ports[port].bridge_dev = bridge;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002282
Vivien Didelotfad09c72016-06-21 12:28:20 -04002283 for (i = 0; i < chip->info->num_ports; ++i) {
2284 if (chip->ports[i].bridge_dev == bridge) {
2285 err = _mv88e6xxx_port_based_vlan_map(chip, i);
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002286 if (err)
2287 break;
2288 }
2289 }
2290
Vivien Didelotfad09c72016-06-21 12:28:20 -04002291 mutex_unlock(&chip->reg_lock);
Vivien Didelota6692752016-02-12 12:09:39 -05002292
Vivien Didelot466dfa02016-02-26 13:16:05 -05002293 return err;
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002294}
2295
Vivien Didelotf81ec902016-05-09 13:22:58 -04002296static void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002297{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002298 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2299 struct net_device *bridge = chip->ports[port].bridge_dev;
Vivien Didelot16bfa702016-03-13 16:21:33 -04002300 int i;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002301
Vivien Didelotfad09c72016-06-21 12:28:20 -04002302 mutex_lock(&chip->reg_lock);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002303
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002304 /* Unassign the bridge and remap each port's VLANTable */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002305 chip->ports[port].bridge_dev = NULL;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002306
Vivien Didelotfad09c72016-06-21 12:28:20 -04002307 for (i = 0; i < chip->info->num_ports; ++i)
2308 if (i == port || chip->ports[i].bridge_dev == bridge)
2309 if (_mv88e6xxx_port_based_vlan_map(chip, i))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002310 netdev_warn(ds->ports[i].netdev,
2311 "failed to remap\n");
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002312
Vivien Didelotfad09c72016-06-21 12:28:20 -04002313 mutex_unlock(&chip->reg_lock);
Vivien Didelot66d9cd02016-02-05 14:07:14 -05002314}
2315
Vivien Didelotfad09c72016-06-21 12:28:20 -04002316static int _mv88e6xxx_mdio_page_write(struct mv88e6xxx_chip *chip,
Andrew Lunn03a4a542016-06-04 21:17:05 +02002317 int port, int page, int reg, int val)
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002318{
2319 int ret;
2320
Vivien Didelotfad09c72016-06-21 12:28:20 -04002321 ret = mv88e6xxx_mdio_write_indirect(chip, port, 0x16, page);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002322 if (ret < 0)
2323 goto restore_page_0;
2324
Vivien Didelotfad09c72016-06-21 12:28:20 -04002325 ret = mv88e6xxx_mdio_write_indirect(chip, port, reg, val);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002326restore_page_0:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002327 mv88e6xxx_mdio_write_indirect(chip, port, 0x16, 0x0);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002328
2329 return ret;
2330}
2331
Vivien Didelotfad09c72016-06-21 12:28:20 -04002332static int _mv88e6xxx_mdio_page_read(struct mv88e6xxx_chip *chip,
Andrew Lunn03a4a542016-06-04 21:17:05 +02002333 int port, int page, int reg)
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002334{
2335 int ret;
2336
Vivien Didelotfad09c72016-06-21 12:28:20 -04002337 ret = mv88e6xxx_mdio_write_indirect(chip, port, 0x16, page);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002338 if (ret < 0)
2339 goto restore_page_0;
2340
Vivien Didelotfad09c72016-06-21 12:28:20 -04002341 ret = mv88e6xxx_mdio_read_indirect(chip, port, reg);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002342restore_page_0:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002343 mv88e6xxx_mdio_write_indirect(chip, port, 0x16, 0x0);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002344
2345 return ret;
2346}
2347
Vivien Didelotfad09c72016-06-21 12:28:20 -04002348static int mv88e6xxx_switch_reset(struct mv88e6xxx_chip *chip)
Vivien Didelot552238b2016-05-09 13:22:49 -04002349{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002350 bool ppu_active = mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE);
Vivien Didelot552238b2016-05-09 13:22:49 -04002351 u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002352 struct gpio_desc *gpiod = chip->reset;
Vivien Didelot552238b2016-05-09 13:22:49 -04002353 unsigned long timeout;
2354 int ret;
2355 int i;
2356
2357 /* Set all ports to the disabled state. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002358 for (i = 0; i < chip->info->num_ports; i++) {
2359 ret = _mv88e6xxx_reg_read(chip, REG_PORT(i), PORT_CONTROL);
Vivien Didelot552238b2016-05-09 13:22:49 -04002360 if (ret < 0)
2361 return ret;
2362
Vivien Didelotfad09c72016-06-21 12:28:20 -04002363 ret = _mv88e6xxx_reg_write(chip, REG_PORT(i), PORT_CONTROL,
Vivien Didelot552238b2016-05-09 13:22:49 -04002364 ret & 0xfffc);
2365 if (ret)
2366 return ret;
2367 }
2368
2369 /* Wait for transmit queues to drain. */
2370 usleep_range(2000, 4000);
2371
2372 /* If there is a gpio connected to the reset pin, toggle it */
2373 if (gpiod) {
2374 gpiod_set_value_cansleep(gpiod, 1);
2375 usleep_range(10000, 20000);
2376 gpiod_set_value_cansleep(gpiod, 0);
2377 usleep_range(10000, 20000);
2378 }
2379
2380 /* Reset the switch. Keep the PPU active if requested. The PPU
2381 * needs to be active to support indirect phy register access
2382 * through global registers 0x18 and 0x19.
2383 */
2384 if (ppu_active)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002385 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, 0x04, 0xc000);
Vivien Didelot552238b2016-05-09 13:22:49 -04002386 else
Vivien Didelotfad09c72016-06-21 12:28:20 -04002387 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, 0x04, 0xc400);
Vivien Didelot552238b2016-05-09 13:22:49 -04002388 if (ret)
2389 return ret;
2390
2391 /* Wait up to one second for reset to complete. */
2392 timeout = jiffies + 1 * HZ;
2393 while (time_before(jiffies, timeout)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002394 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, 0x00);
Vivien Didelot552238b2016-05-09 13:22:49 -04002395 if (ret < 0)
2396 return ret;
2397
2398 if ((ret & is_reset) == is_reset)
2399 break;
2400 usleep_range(1000, 2000);
2401 }
2402 if (time_after(jiffies, timeout))
2403 ret = -ETIMEDOUT;
2404 else
2405 ret = 0;
2406
2407 return ret;
2408}
2409
Vivien Didelotfad09c72016-06-21 12:28:20 -04002410static int mv88e6xxx_power_on_serdes(struct mv88e6xxx_chip *chip)
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002411{
2412 int ret;
2413
Vivien Didelotfad09c72016-06-21 12:28:20 -04002414 ret = _mv88e6xxx_mdio_page_read(chip, REG_FIBER_SERDES,
Andrew Lunn03a4a542016-06-04 21:17:05 +02002415 PAGE_FIBER_SERDES, MII_BMCR);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002416 if (ret < 0)
2417 return ret;
2418
2419 if (ret & BMCR_PDOWN) {
2420 ret &= ~BMCR_PDOWN;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002421 ret = _mv88e6xxx_mdio_page_write(chip, REG_FIBER_SERDES,
Andrew Lunn03a4a542016-06-04 21:17:05 +02002422 PAGE_FIBER_SERDES, MII_BMCR,
2423 ret);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002424 }
2425
2426 return ret;
2427}
2428
Vivien Didelotfad09c72016-06-21 12:28:20 -04002429static int mv88e6xxx_setup_port(struct mv88e6xxx_chip *chip, int port)
Guenter Roeckd827e882015-03-26 18:36:29 -07002430{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002431 struct dsa_switch *ds = chip->ds;
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002432 int ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002433 u16 reg;
Guenter Roeckd827e882015-03-26 18:36:29 -07002434
Vivien Didelotfad09c72016-06-21 12:28:20 -04002435 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2436 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2437 mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip) ||
2438 mv88e6xxx_6065_family(chip) || mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002439 /* MAC Forcing register: don't force link, speed,
2440 * duplex or flow control state to any particular
2441 * values on physical ports, but force the CPU port
2442 * and all DSA ports to their maximum bandwidth and
2443 * full duplex.
2444 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002445 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_PCS_CTRL);
Andrew Lunn60045cb2015-08-17 23:52:51 +02002446 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
Russell King53adc9e2015-09-21 21:42:59 +01002447 reg &= ~PORT_PCS_CTRL_UNFORCED;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002448 reg |= PORT_PCS_CTRL_FORCE_LINK |
2449 PORT_PCS_CTRL_LINK_UP |
2450 PORT_PCS_CTRL_DUPLEX_FULL |
2451 PORT_PCS_CTRL_FORCE_DUPLEX;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002452 if (mv88e6xxx_6065_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002453 reg |= PORT_PCS_CTRL_100;
2454 else
2455 reg |= PORT_PCS_CTRL_1000;
2456 } else {
2457 reg |= PORT_PCS_CTRL_UNFORCED;
2458 }
2459
Vivien Didelotfad09c72016-06-21 12:28:20 -04002460 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002461 PORT_PCS_CTRL, reg);
2462 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002463 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002464 }
2465
2466 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
2467 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
2468 * tunneling, determine priority by looking at 802.1p and IP
2469 * priority fields (IP prio has precedence), and set STP state
2470 * to Forwarding.
2471 *
2472 * If this is the CPU link, use DSA or EDSA tagging depending
2473 * on which tagging mode was configured.
2474 *
2475 * If this is a link to another switch, use DSA tagging mode.
2476 *
2477 * If this is the upstream port for this switch, enable
2478 * forwarding of unknown unicasts and multicasts.
2479 */
2480 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002481 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2482 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2483 mv88e6xxx_6095_family(chip) || mv88e6xxx_6065_family(chip) ||
2484 mv88e6xxx_6185_family(chip) || mv88e6xxx_6320_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002485 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
2486 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
2487 PORT_CONTROL_STATE_FORWARDING;
2488 if (dsa_is_cpu_port(ds, port)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002489 if (mv88e6xxx_6095_family(chip) || mv88e6xxx_6185_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002490 reg |= PORT_CONTROL_DSA_TAG;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002491 if (mv88e6xxx_6352_family(chip) ||
2492 mv88e6xxx_6351_family(chip) ||
2493 mv88e6xxx_6165_family(chip) ||
2494 mv88e6xxx_6097_family(chip) ||
2495 mv88e6xxx_6320_family(chip)) {
Andrew Lunn5377b802016-06-04 21:17:02 +02002496 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA |
2497 PORT_CONTROL_FORWARD_UNKNOWN |
Andrew Lunnc047a1f2015-09-29 01:50:56 +02002498 PORT_CONTROL_FORWARD_UNKNOWN_MC;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002499 }
2500
Vivien Didelotfad09c72016-06-21 12:28:20 -04002501 if (mv88e6xxx_6352_family(chip) ||
2502 mv88e6xxx_6351_family(chip) ||
2503 mv88e6xxx_6165_family(chip) ||
2504 mv88e6xxx_6097_family(chip) ||
2505 mv88e6xxx_6095_family(chip) ||
2506 mv88e6xxx_6065_family(chip) ||
2507 mv88e6xxx_6185_family(chip) ||
2508 mv88e6xxx_6320_family(chip)) {
Vivien Didelot57d32312016-06-20 13:13:58 -04002509 reg |= PORT_CONTROL_EGRESS_ADD_TAG;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002510 }
2511 }
Andrew Lunn6083ce72015-08-17 23:52:52 +02002512 if (dsa_is_dsa_port(ds, port)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002513 if (mv88e6xxx_6095_family(chip) ||
2514 mv88e6xxx_6185_family(chip))
Andrew Lunn6083ce72015-08-17 23:52:52 +02002515 reg |= PORT_CONTROL_DSA_TAG;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002516 if (mv88e6xxx_6352_family(chip) ||
2517 mv88e6xxx_6351_family(chip) ||
2518 mv88e6xxx_6165_family(chip) ||
2519 mv88e6xxx_6097_family(chip) ||
2520 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002521 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunn6083ce72015-08-17 23:52:52 +02002522 }
2523
Andrew Lunn54d792f2015-05-06 01:09:47 +02002524 if (port == dsa_upstream_port(ds))
2525 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2526 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2527 }
2528 if (reg) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002529 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002530 PORT_CONTROL, reg);
2531 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002532 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002533 }
2534
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002535 /* If this port is connected to a SerDes, make sure the SerDes is not
2536 * powered down.
2537 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002538 if (mv88e6xxx_6352_family(chip)) {
2539 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_STATUS);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002540 if (ret < 0)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002541 return ret;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002542 ret &= PORT_STATUS_CMODE_MASK;
2543 if ((ret == PORT_STATUS_CMODE_100BASE_X) ||
2544 (ret == PORT_STATUS_CMODE_1000BASE_X) ||
2545 (ret == PORT_STATUS_CMODE_SGMII)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002546 ret = mv88e6xxx_power_on_serdes(chip);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002547 if (ret < 0)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002548 return ret;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002549 }
2550 }
2551
Vivien Didelot8efdda42015-08-13 12:52:23 -04002552 /* Port Control 2: don't force a good FCS, set the maximum frame size to
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002553 * 10240 bytes, disable 802.1q tags checking, don't discard tagged or
Vivien Didelot8efdda42015-08-13 12:52:23 -04002554 * untagged frames on this port, do a destination address lookup on all
2555 * received packets as usual, disable ARP mirroring and don't send a
2556 * copy of all transmitted/received frames on this port to the CPU.
Andrew Lunn54d792f2015-05-06 01:09:47 +02002557 */
2558 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002559 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2560 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2561 mv88e6xxx_6095_family(chip) || mv88e6xxx_6320_family(chip) ||
2562 mv88e6xxx_6185_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002563 reg = PORT_CONTROL_2_MAP_DA;
2564
Vivien Didelotfad09c72016-06-21 12:28:20 -04002565 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2566 mv88e6xxx_6165_family(chip) || mv88e6xxx_6320_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002567 reg |= PORT_CONTROL_2_JUMBO_10240;
2568
Vivien Didelotfad09c72016-06-21 12:28:20 -04002569 if (mv88e6xxx_6095_family(chip) || mv88e6xxx_6185_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002570 /* Set the upstream port this port should use */
2571 reg |= dsa_upstream_port(ds);
2572 /* enable forwarding of unknown multicast addresses to
2573 * the upstream port
2574 */
2575 if (port == dsa_upstream_port(ds))
2576 reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
2577 }
2578
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002579 reg |= PORT_CONTROL_2_8021Q_DISABLED;
Vivien Didelot8efdda42015-08-13 12:52:23 -04002580
Andrew Lunn54d792f2015-05-06 01:09:47 +02002581 if (reg) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002582 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002583 PORT_CONTROL_2, reg);
2584 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002585 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002586 }
2587
2588 /* Port Association Vector: when learning source addresses
2589 * of packets, add the address to the address database using
2590 * a port bitmap that has only the bit for this port set and
2591 * the other bits clear.
2592 */
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002593 reg = 1 << port;
Vivien Didelot996ecb82016-04-14 14:42:08 -04002594 /* Disable learning for CPU port */
2595 if (dsa_is_cpu_port(ds, port))
Vivien Didelot65fa4022016-04-14 14:42:07 -04002596 reg = 0;
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002597
Vivien Didelotfad09c72016-06-21 12:28:20 -04002598 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_ASSOC_VECTOR,
2599 reg);
Andrew Lunn54d792f2015-05-06 01:09:47 +02002600 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002601 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002602
2603 /* Egress rate control 2: disable egress rate control. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002604 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_RATE_CONTROL_2,
Andrew Lunn54d792f2015-05-06 01:09:47 +02002605 0x0000);
2606 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002607 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002608
Vivien Didelotfad09c72016-06-21 12:28:20 -04002609 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2610 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2611 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002612 /* Do not limit the period of time that this port can
2613 * be paused for by the remote end or the period of
2614 * time that this port can pause the remote end.
2615 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002616 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002617 PORT_PAUSE_CTRL, 0x0000);
2618 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002619 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002620
2621 /* Port ATU control: disable limiting the number of
2622 * address database entries that this port is allowed
2623 * to use.
2624 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002625 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002626 PORT_ATU_CONTROL, 0x0000);
2627 /* Priority Override: disable DA, SA and VTU priority
2628 * override.
2629 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002630 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002631 PORT_PRI_OVERRIDE, 0x0000);
2632 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002633 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002634
2635 /* Port Ethertype: use the Ethertype DSA Ethertype
2636 * value.
2637 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002638 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002639 PORT_ETH_TYPE, ETH_P_EDSA);
2640 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002641 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002642 /* Tag Remap: use an identity 802.1p prio -> switch
2643 * prio mapping.
2644 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002645 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002646 PORT_TAG_REGMAP_0123, 0x3210);
2647 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002648 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002649
2650 /* Tag Remap 2: use an identity 802.1p prio -> switch
2651 * prio mapping.
2652 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002653 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002654 PORT_TAG_REGMAP_4567, 0x7654);
2655 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002656 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002657 }
2658
Vivien Didelotfad09c72016-06-21 12:28:20 -04002659 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2660 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2661 mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip) ||
2662 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002663 /* Rate Control: disable ingress rate limiting. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002664 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002665 PORT_RATE_CONTROL, 0x0001);
2666 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002667 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002668 }
2669
Guenter Roeck366f0a02015-03-26 18:36:30 -07002670 /* Port Control 1: disable trunking, disable sending
2671 * learning messages to this port.
Guenter Roeckd827e882015-03-26 18:36:29 -07002672 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002673 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_1,
2674 0x0000);
Guenter Roeckd827e882015-03-26 18:36:29 -07002675 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002676 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07002677
Vivien Didelot207afda2016-04-14 14:42:09 -04002678 /* Port based VLAN map: give each port the same default address
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002679 * database, and allow bidirectional communication between the
2680 * CPU and DSA port(s), and the other ports.
Guenter Roeckd827e882015-03-26 18:36:29 -07002681 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002682 ret = _mv88e6xxx_port_fid_set(chip, port, 0);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002683 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002684 return ret;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002685
Vivien Didelotfad09c72016-06-21 12:28:20 -04002686 ret = _mv88e6xxx_port_based_vlan_map(chip, port);
Guenter Roeckd827e882015-03-26 18:36:29 -07002687 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002688 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07002689
2690 /* Default VLAN ID and priority: don't set a default VLAN
2691 * ID, and set the default packet priority to zero.
2692 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002693 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_DEFAULT_VLAN,
Vivien Didelot47cf1e62015-04-20 17:43:26 -04002694 0x0000);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002695 if (ret)
2696 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07002697
Andrew Lunndbde9e62015-05-06 01:09:48 +02002698 return 0;
2699}
2700
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002701static int mv88e6xxx_g1_set_switch_mac(struct mv88e6xxx_chip *chip, u8 *addr)
2702{
2703 int err;
2704
2705 err = mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_MAC_01,
2706 (addr[0] << 8) | addr[1]);
2707 if (err)
2708 return err;
2709
2710 err = mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_MAC_23,
2711 (addr[2] << 8) | addr[3]);
2712 if (err)
2713 return err;
2714
2715 return mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_MAC_45,
2716 (addr[4] << 8) | addr[5]);
2717}
2718
Vivien Didelotacddbd22016-07-18 20:45:39 -04002719static int mv88e6xxx_g1_set_age_time(struct mv88e6xxx_chip *chip,
2720 unsigned int msecs)
2721{
2722 const unsigned int coeff = chip->info->age_time_coeff;
2723 const unsigned int min = 0x01 * coeff;
2724 const unsigned int max = 0xff * coeff;
2725 u8 age_time;
2726 u16 val;
2727 int err;
2728
2729 if (msecs < min || msecs > max)
2730 return -ERANGE;
2731
2732 /* Round to nearest multiple of coeff */
2733 age_time = (msecs + coeff / 2) / coeff;
2734
2735 err = mv88e6xxx_read(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL, &val);
2736 if (err)
2737 return err;
2738
2739 /* AgeTime is 11:4 bits */
2740 val &= ~0xff0;
2741 val |= age_time << 4;
2742
2743 return mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL, val);
2744}
2745
Vivien Didelot2cfcd962016-07-18 20:45:40 -04002746static int mv88e6xxx_set_ageing_time(struct dsa_switch *ds,
2747 unsigned int ageing_time)
2748{
2749 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2750 int err;
2751
2752 mutex_lock(&chip->reg_lock);
2753 err = mv88e6xxx_g1_set_age_time(chip, ageing_time);
2754 mutex_unlock(&chip->reg_lock);
2755
2756 return err;
2757}
2758
Vivien Didelot97299342016-07-18 20:45:30 -04002759static int mv88e6xxx_g1_setup(struct mv88e6xxx_chip *chip)
Vivien Didelot08a01262016-05-09 13:22:50 -04002760{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002761 struct dsa_switch *ds = chip->ds;
Vivien Didelotb0745e872016-05-09 13:22:53 -04002762 u32 upstream_port = dsa_upstream_port(ds);
Vivien Didelot119477b2016-05-09 13:22:51 -04002763 u16 reg;
Vivien Didelot08a01262016-05-09 13:22:50 -04002764 int err;
Vivien Didelot08a01262016-05-09 13:22:50 -04002765
Vivien Didelot119477b2016-05-09 13:22:51 -04002766 /* Enable the PHY Polling Unit if present, don't discard any packets,
2767 * and mask all interrupt sources.
2768 */
2769 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002770 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU) ||
2771 mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE))
Vivien Didelot119477b2016-05-09 13:22:51 -04002772 reg |= GLOBAL_CONTROL_PPU_ENABLE;
2773
Vivien Didelotfad09c72016-06-21 12:28:20 -04002774 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL, reg);
Vivien Didelot119477b2016-05-09 13:22:51 -04002775 if (err)
2776 return err;
2777
Vivien Didelotb0745e872016-05-09 13:22:53 -04002778 /* Configure the upstream port, and configure it as the port to which
2779 * ingress and egress and ARP monitor frames are to be sent.
2780 */
2781 reg = upstream_port << GLOBAL_MONITOR_CONTROL_INGRESS_SHIFT |
2782 upstream_port << GLOBAL_MONITOR_CONTROL_EGRESS_SHIFT |
2783 upstream_port << GLOBAL_MONITOR_CONTROL_ARP_SHIFT;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002784 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_MONITOR_CONTROL,
2785 reg);
Vivien Didelotb0745e872016-05-09 13:22:53 -04002786 if (err)
2787 return err;
2788
Vivien Didelot50484ff2016-05-09 13:22:54 -04002789 /* Disable remote management, and set the switch's DSA device number. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002790 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL_2,
Vivien Didelot50484ff2016-05-09 13:22:54 -04002791 GLOBAL_CONTROL_2_MULTIPLE_CASCADE |
2792 (ds->index & 0x1f));
2793 if (err)
2794 return err;
2795
Vivien Didelotacddbd22016-07-18 20:45:39 -04002796 /* Clear all the VTU and STU entries */
2797 err = _mv88e6xxx_vtu_stu_flush(chip);
2798 if (err < 0)
2799 return err;
2800
Vivien Didelot08a01262016-05-09 13:22:50 -04002801 /* Set the default address aging time to 5 minutes, and
2802 * enable address learn messages to be sent to all message
2803 * ports.
2804 */
Vivien Didelotacddbd22016-07-18 20:45:39 -04002805 err = mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL,
2806 GLOBAL_ATU_CONTROL_LEARN2ALL);
Vivien Didelot08a01262016-05-09 13:22:50 -04002807 if (err)
2808 return err;
2809
Vivien Didelotacddbd22016-07-18 20:45:39 -04002810 err = mv88e6xxx_g1_set_age_time(chip, 300000);
2811 if (err)
Vivien Didelot97299342016-07-18 20:45:30 -04002812 return err;
2813
2814 /* Clear all ATU entries */
2815 err = _mv88e6xxx_atu_flush(chip, 0, true);
2816 if (err)
2817 return err;
2818
Vivien Didelot08a01262016-05-09 13:22:50 -04002819 /* Configure the IP ToS mapping registers. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002820 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_0, 0x0000);
Vivien Didelot08a01262016-05-09 13:22:50 -04002821 if (err)
2822 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002823 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_1, 0x0000);
Vivien Didelot08a01262016-05-09 13:22:50 -04002824 if (err)
2825 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002826 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_2, 0x5555);
Vivien Didelot08a01262016-05-09 13:22:50 -04002827 if (err)
2828 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002829 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_3, 0x5555);
Vivien Didelot08a01262016-05-09 13:22:50 -04002830 if (err)
2831 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002832 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_4, 0xaaaa);
Vivien Didelot08a01262016-05-09 13:22:50 -04002833 if (err)
2834 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002835 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_5, 0xaaaa);
Vivien Didelot08a01262016-05-09 13:22:50 -04002836 if (err)
2837 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002838 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_6, 0xffff);
Vivien Didelot08a01262016-05-09 13:22:50 -04002839 if (err)
2840 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002841 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_7, 0xffff);
Vivien Didelot08a01262016-05-09 13:22:50 -04002842 if (err)
2843 return err;
2844
2845 /* Configure the IEEE 802.1p priority mapping register. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002846 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IEEE_PRI, 0xfa41);
Vivien Didelot08a01262016-05-09 13:22:50 -04002847 if (err)
2848 return err;
2849
Vivien Didelot97299342016-07-18 20:45:30 -04002850 /* Clear the statistics counters for all ports */
2851 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
2852 GLOBAL_STATS_OP_FLUSH_ALL);
2853 if (err)
2854 return err;
2855
2856 /* Wait for the flush to complete. */
2857 err = _mv88e6xxx_stats_wait(chip);
2858 if (err)
2859 return err;
2860
2861 return 0;
2862}
2863
Vivien Didelotf22ab642016-07-18 20:45:31 -04002864static int mv88e6xxx_g2_device_mapping_write(struct mv88e6xxx_chip *chip,
2865 int target, int port)
2866{
2867 u16 val = (target << 8) | (port & 0xf);
2868
2869 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_DEVICE_MAPPING, val);
2870}
2871
2872static int mv88e6xxx_g2_set_device_mapping(struct mv88e6xxx_chip *chip)
2873{
2874 int target, port;
2875 int err;
2876
2877 /* Initialize the routing port to the 32 possible target devices */
2878 for (target = 0; target < 32; ++target) {
2879 port = 0xf;
2880
2881 if (target < DSA_MAX_SWITCHES) {
2882 port = chip->ds->rtable[target];
2883 if (port == DSA_RTABLE_NONE)
2884 port = 0xf;
2885 }
2886
2887 err = mv88e6xxx_g2_device_mapping_write(chip, target, port);
2888 if (err)
2889 break;
2890 }
2891
2892 return err;
2893}
2894
Vivien Didelot51540412016-07-18 20:45:32 -04002895static int mv88e6xxx_g2_trunk_mask_write(struct mv88e6xxx_chip *chip, int num,
2896 bool hask, u16 mask)
2897{
2898 const u16 port_mask = BIT(chip->info->num_ports) - 1;
2899 u16 val = (num << 12) | (mask & port_mask);
2900
2901 if (hask)
2902 val |= GLOBAL2_TRUNK_MASK_HASK;
2903
2904 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_TRUNK_MASK, val);
2905}
2906
2907static int mv88e6xxx_g2_trunk_mapping_write(struct mv88e6xxx_chip *chip, int id,
2908 u16 map)
2909{
2910 const u16 port_mask = BIT(chip->info->num_ports) - 1;
2911 u16 val = (id << 11) | (map & port_mask);
2912
2913 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_TRUNK_MAPPING, val);
2914}
2915
2916static int mv88e6xxx_g2_clear_trunk(struct mv88e6xxx_chip *chip)
2917{
2918 const u16 port_mask = BIT(chip->info->num_ports) - 1;
2919 int i, err;
2920
2921 /* Clear all eight possible Trunk Mask vectors */
2922 for (i = 0; i < 8; ++i) {
2923 err = mv88e6xxx_g2_trunk_mask_write(chip, i, false, port_mask);
2924 if (err)
2925 return err;
2926 }
2927
2928 /* Clear all sixteen possible Trunk ID routing vectors */
2929 for (i = 0; i < 16; ++i) {
2930 err = mv88e6xxx_g2_trunk_mapping_write(chip, i, 0);
2931 if (err)
2932 return err;
2933 }
2934
2935 return 0;
2936}
2937
Vivien Didelot8ec61c72016-07-18 20:45:37 -04002938static int mv88e6xxx_g2_clear_irl(struct mv88e6xxx_chip *chip)
2939{
2940 int port, err;
2941
2942 /* Init all Ingress Rate Limit resources of all ports */
2943 for (port = 0; port < chip->info->num_ports; ++port) {
2944 /* XXX newer chips (like 88E6390) have different 2-bit ops */
2945 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_IRL_CMD,
2946 GLOBAL2_IRL_CMD_OP_INIT_ALL |
2947 (port << 8));
2948 if (err)
2949 break;
2950
2951 /* Wait for the operation to complete */
2952 err = _mv88e6xxx_wait(chip, REG_GLOBAL2, GLOBAL2_IRL_CMD,
2953 GLOBAL2_IRL_CMD_BUSY);
2954 if (err)
2955 break;
2956 }
2957
2958 return err;
2959}
2960
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002961/* Indirect write to the Switch MAC/WoL/WoF register */
2962static int mv88e6xxx_g2_switch_mac_write(struct mv88e6xxx_chip *chip,
2963 unsigned int pointer, u8 data)
2964{
2965 u16 val = (pointer << 8) | data;
2966
2967 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_SWITCH_MAC, val);
2968}
2969
2970static int mv88e6xxx_g2_set_switch_mac(struct mv88e6xxx_chip *chip, u8 *addr)
2971{
2972 int i, err;
2973
2974 for (i = 0; i < 6; i++) {
2975 err = mv88e6xxx_g2_switch_mac_write(chip, i, addr[i]);
2976 if (err)
2977 break;
2978 }
2979
2980 return err;
2981}
2982
Vivien Didelot9bda8892016-07-18 20:45:36 -04002983static int mv88e6xxx_g2_pot_write(struct mv88e6xxx_chip *chip, int pointer,
2984 u8 data)
2985{
2986 u16 val = (pointer << 8) | (data & 0x7);
2987
2988 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_PRIO_OVERRIDE, val);
2989}
2990
2991static int mv88e6xxx_g2_clear_pot(struct mv88e6xxx_chip *chip)
2992{
2993 int i, err;
2994
2995 /* Clear all sixteen possible Priority Override entries */
2996 for (i = 0; i < 16; i++) {
2997 err = mv88e6xxx_g2_pot_write(chip, i, 0);
2998 if (err)
2999 break;
3000 }
3001
3002 return err;
3003}
3004
Vivien Didelot855b1932016-07-20 18:18:35 -04003005static int mv88e6xxx_g2_eeprom_wait(struct mv88e6xxx_chip *chip)
3006{
3007 return _mv88e6xxx_wait(chip, REG_GLOBAL2, GLOBAL2_EEPROM_CMD,
3008 GLOBAL2_EEPROM_CMD_BUSY |
3009 GLOBAL2_EEPROM_CMD_RUNNING);
3010}
3011
3012static int mv88e6xxx_g2_eeprom_cmd(struct mv88e6xxx_chip *chip, u16 cmd)
3013{
3014 int err;
3015
3016 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_EEPROM_CMD, cmd);
3017 if (err)
3018 return err;
3019
3020 return mv88e6xxx_g2_eeprom_wait(chip);
3021}
3022
3023static int mv88e6xxx_g2_eeprom_read16(struct mv88e6xxx_chip *chip,
3024 u8 addr, u16 *data)
3025{
3026 u16 cmd = GLOBAL2_EEPROM_CMD_OP_READ | addr;
3027 int err;
3028
3029 err = mv88e6xxx_g2_eeprom_wait(chip);
3030 if (err)
3031 return err;
3032
3033 err = mv88e6xxx_g2_eeprom_cmd(chip, cmd);
3034 if (err)
3035 return err;
3036
3037 return mv88e6xxx_read(chip, REG_GLOBAL2, GLOBAL2_EEPROM_DATA, data);
3038}
3039
3040static int mv88e6xxx_g2_eeprom_write16(struct mv88e6xxx_chip *chip,
3041 u8 addr, u16 data)
3042{
3043 u16 cmd = GLOBAL2_EEPROM_CMD_OP_WRITE | addr;
3044 int err;
3045
3046 err = mv88e6xxx_g2_eeprom_wait(chip);
3047 if (err)
3048 return err;
3049
3050 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_EEPROM_DATA, data);
3051 if (err)
3052 return err;
3053
3054 return mv88e6xxx_g2_eeprom_cmd(chip, cmd);
3055}
3056
Vivien Didelot97299342016-07-18 20:45:30 -04003057static int mv88e6xxx_g2_setup(struct mv88e6xxx_chip *chip)
3058{
Vivien Didelot47395ed2016-07-18 20:45:33 -04003059 u16 reg;
Vivien Didelot97299342016-07-18 20:45:30 -04003060 int err;
Vivien Didelot97299342016-07-18 20:45:30 -04003061
Vivien Didelot47395ed2016-07-18 20:45:33 -04003062 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_2X)) {
3063 /* Consider the frames with reserved multicast destination
3064 * addresses matching 01:80:c2:00:00:2x as MGMT.
3065 */
3066 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_MGMT_EN_2X,
3067 0xffff);
3068 if (err)
3069 return err;
3070 }
3071
3072 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_0X)) {
3073 /* Consider the frames with reserved multicast destination
3074 * addresses matching 01:80:c2:00:00:0x as MGMT.
3075 */
3076 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_MGMT_EN_0X,
3077 0xffff);
3078 if (err)
3079 return err;
3080 }
Vivien Didelot08a01262016-05-09 13:22:50 -04003081
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 Didelot47395ed2016-07-18 20:45:33 -04003087 reg = GLOBAL2_SWITCH_MGMT_FORCE_FLOW_CTRL_PRI | (0x7 << 4);
3088 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_0X) ||
3089 mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_2X))
3090 reg |= GLOBAL2_SWITCH_MGMT_RSVD2CPU | 0x7;
3091 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_SWITCH_MGMT, reg);
Vivien Didelot08a01262016-05-09 13:22:50 -04003092 if (err)
3093 return err;
3094
3095 /* Program the DSA routing table. */
Vivien Didelotf22ab642016-07-18 20:45:31 -04003096 err = mv88e6xxx_g2_set_device_mapping(chip);
3097 if (err)
3098 return err;
Vivien Didelot08a01262016-05-09 13:22:50 -04003099
Vivien Didelot51540412016-07-18 20:45:32 -04003100 /* Clear all trunk masks and mapping. */
3101 err = mv88e6xxx_g2_clear_trunk(chip);
3102 if (err)
3103 return err;
Vivien Didelot08a01262016-05-09 13:22:50 -04003104
Vivien Didelot8ec61c72016-07-18 20:45:37 -04003105 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_IRL)) {
3106 /* Disable ingress rate limiting by resetting all per port
3107 * ingress rate limit resources to their initial state.
3108 */
3109 err = mv88e6xxx_g2_clear_irl(chip);
3110 if (err)
3111 return err;
3112 }
3113
Vivien Didelot63ed8802016-07-18 20:45:35 -04003114 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_PVT)) {
3115 /* Initialize Cross-chip Port VLAN Table to reset defaults */
3116 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_PVT_ADDR,
3117 GLOBAL2_PVT_ADDR_OP_INIT_ONES);
3118 if (err)
3119 return err;
3120 }
3121
Vivien Didelot9bda8892016-07-18 20:45:36 -04003122 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_POT)) {
Vivien Didelot08a01262016-05-09 13:22:50 -04003123 /* Clear the priority override table. */
Vivien Didelot9bda8892016-07-18 20:45:36 -04003124 err = mv88e6xxx_g2_clear_pot(chip);
3125 if (err)
3126 return err;
Vivien Didelot08a01262016-05-09 13:22:50 -04003127 }
3128
Vivien Didelot97299342016-07-18 20:45:30 -04003129 return 0;
Vivien Didelot08a01262016-05-09 13:22:50 -04003130}
3131
Vivien Didelotf81ec902016-05-09 13:22:58 -04003132static int mv88e6xxx_setup(struct dsa_switch *ds)
Guenter Roeckacdaffc2015-03-26 18:36:28 -07003133{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003134 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelot552238b2016-05-09 13:22:49 -04003135 int err;
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003136 int i;
3137
Vivien Didelotfad09c72016-06-21 12:28:20 -04003138 chip->ds = ds;
3139 ds->slave_mii_bus = chip->mdio_bus;
Vivien Didelot552238b2016-05-09 13:22:49 -04003140
Vivien Didelotfad09c72016-06-21 12:28:20 -04003141 mutex_lock(&chip->reg_lock);
Vivien Didelot552238b2016-05-09 13:22:49 -04003142
Vivien Didelotfad09c72016-06-21 12:28:20 -04003143 err = mv88e6xxx_switch_reset(chip);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003144 if (err)
3145 goto unlock;
Andrew Lunn54d792f2015-05-06 01:09:47 +02003146
Vivien Didelot97299342016-07-18 20:45:30 -04003147 /* Setup Switch Port Registers */
3148 for (i = 0; i < chip->info->num_ports; i++) {
3149 err = mv88e6xxx_setup_port(chip, i);
3150 if (err)
3151 goto unlock;
3152 }
3153
3154 /* Setup Switch Global 1 Registers */
3155 err = mv88e6xxx_g1_setup(chip);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003156 if (err)
3157 goto unlock;
3158
Vivien Didelot97299342016-07-18 20:45:30 -04003159 /* Setup Switch Global 2 Registers */
3160 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_GLOBAL2)) {
3161 err = mv88e6xxx_g2_setup(chip);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003162 if (err)
3163 goto unlock;
3164 }
Andrew Lunn54d792f2015-05-06 01:09:47 +02003165
Vivien Didelot6b17e862015-08-13 12:52:18 -04003166unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04003167 mutex_unlock(&chip->reg_lock);
Andrew Lunndb687a52015-06-20 21:31:29 +02003168
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003169 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02003170}
3171
Vivien Didelot3b4caa12016-07-18 20:45:34 -04003172static int mv88e6xxx_set_addr(struct dsa_switch *ds, u8 *addr)
3173{
3174 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3175 int err;
3176
3177 mutex_lock(&chip->reg_lock);
3178
3179 /* Has an indirect Switch MAC/WoL/WoF register in Global 2? */
3180 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_SWITCH_MAC))
3181 err = mv88e6xxx_g2_set_switch_mac(chip, addr);
3182 else
3183 err = mv88e6xxx_g1_set_switch_mac(chip, addr);
3184
3185 mutex_unlock(&chip->reg_lock);
3186
3187 return err;
3188}
3189
Vivien Didelot57d32312016-06-20 13:13:58 -04003190static int mv88e6xxx_mdio_page_read(struct dsa_switch *ds, int port, int page,
3191 int reg)
Andrew Lunn491435852015-04-02 04:06:35 +02003192{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003193 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunn491435852015-04-02 04:06:35 +02003194 int ret;
3195
Vivien Didelotfad09c72016-06-21 12:28:20 -04003196 mutex_lock(&chip->reg_lock);
3197 ret = _mv88e6xxx_mdio_page_read(chip, port, page, reg);
3198 mutex_unlock(&chip->reg_lock);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00003199
Andrew Lunn491435852015-04-02 04:06:35 +02003200 return ret;
3201}
3202
Vivien Didelot57d32312016-06-20 13:13:58 -04003203static int mv88e6xxx_mdio_page_write(struct dsa_switch *ds, int port, int page,
3204 int reg, int val)
Andrew Lunn491435852015-04-02 04:06:35 +02003205{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003206 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunn491435852015-04-02 04:06:35 +02003207 int ret;
3208
Vivien Didelotfad09c72016-06-21 12:28:20 -04003209 mutex_lock(&chip->reg_lock);
3210 ret = _mv88e6xxx_mdio_page_write(chip, port, page, reg, val);
3211 mutex_unlock(&chip->reg_lock);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00003212
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003213 return ret;
3214}
3215
Vivien Didelotfad09c72016-06-21 12:28:20 -04003216static int mv88e6xxx_port_to_mdio_addr(struct mv88e6xxx_chip *chip, int port)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003217{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003218 if (port >= 0 && port < chip->info->num_ports)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003219 return port;
3220 return -EINVAL;
3221}
3222
Andrew Lunnb516d452016-06-04 21:17:06 +02003223static int mv88e6xxx_mdio_read(struct mii_bus *bus, int port, int regnum)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003224{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003225 struct mv88e6xxx_chip *chip = bus->priv;
3226 int addr = mv88e6xxx_port_to_mdio_addr(chip, port);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003227 int ret;
3228
3229 if (addr < 0)
Andrew Lunn158bc062016-04-28 21:24:06 -04003230 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003231
Vivien Didelotfad09c72016-06-21 12:28:20 -04003232 mutex_lock(&chip->reg_lock);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003233
Vivien Didelotfad09c72016-06-21 12:28:20 -04003234 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU))
3235 ret = mv88e6xxx_mdio_read_ppu(chip, addr, regnum);
3236 else if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_SMI_PHY))
3237 ret = mv88e6xxx_mdio_read_indirect(chip, addr, regnum);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003238 else
Vivien Didelotfad09c72016-06-21 12:28:20 -04003239 ret = mv88e6xxx_mdio_read_direct(chip, addr, regnum);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003240
Vivien Didelotfad09c72016-06-21 12:28:20 -04003241 mutex_unlock(&chip->reg_lock);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003242 return ret;
3243}
3244
Andrew Lunnb516d452016-06-04 21:17:06 +02003245static int mv88e6xxx_mdio_write(struct mii_bus *bus, int port, int regnum,
Andrew Lunn03a4a542016-06-04 21:17:05 +02003246 u16 val)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003247{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003248 struct mv88e6xxx_chip *chip = bus->priv;
3249 int addr = mv88e6xxx_port_to_mdio_addr(chip, port);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003250 int ret;
3251
3252 if (addr < 0)
Andrew Lunn158bc062016-04-28 21:24:06 -04003253 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003254
Vivien Didelotfad09c72016-06-21 12:28:20 -04003255 mutex_lock(&chip->reg_lock);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003256
Vivien Didelotfad09c72016-06-21 12:28:20 -04003257 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU))
3258 ret = mv88e6xxx_mdio_write_ppu(chip, addr, regnum, val);
3259 else if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_SMI_PHY))
3260 ret = mv88e6xxx_mdio_write_indirect(chip, addr, regnum, val);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003261 else
Vivien Didelotfad09c72016-06-21 12:28:20 -04003262 ret = mv88e6xxx_mdio_write_direct(chip, addr, regnum, val);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003263
Vivien Didelotfad09c72016-06-21 12:28:20 -04003264 mutex_unlock(&chip->reg_lock);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003265 return ret;
3266}
3267
Vivien Didelotfad09c72016-06-21 12:28:20 -04003268static int mv88e6xxx_mdio_register(struct mv88e6xxx_chip *chip,
Andrew Lunnb516d452016-06-04 21:17:06 +02003269 struct device_node *np)
3270{
3271 static int index;
3272 struct mii_bus *bus;
3273 int err;
3274
Vivien Didelotfad09c72016-06-21 12:28:20 -04003275 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU))
3276 mv88e6xxx_ppu_state_init(chip);
Andrew Lunnb516d452016-06-04 21:17:06 +02003277
3278 if (np)
Vivien Didelotfad09c72016-06-21 12:28:20 -04003279 chip->mdio_np = of_get_child_by_name(np, "mdio");
Andrew Lunnb516d452016-06-04 21:17:06 +02003280
Vivien Didelotfad09c72016-06-21 12:28:20 -04003281 bus = devm_mdiobus_alloc(chip->dev);
Andrew Lunnb516d452016-06-04 21:17:06 +02003282 if (!bus)
3283 return -ENOMEM;
3284
Vivien Didelotfad09c72016-06-21 12:28:20 -04003285 bus->priv = (void *)chip;
Andrew Lunnb516d452016-06-04 21:17:06 +02003286 if (np) {
3287 bus->name = np->full_name;
3288 snprintf(bus->id, MII_BUS_ID_SIZE, "%s", np->full_name);
3289 } else {
3290 bus->name = "mv88e6xxx SMI";
3291 snprintf(bus->id, MII_BUS_ID_SIZE, "mv88e6xxx-%d", index++);
3292 }
3293
3294 bus->read = mv88e6xxx_mdio_read;
3295 bus->write = mv88e6xxx_mdio_write;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003296 bus->parent = chip->dev;
Andrew Lunnb516d452016-06-04 21:17:06 +02003297
Vivien Didelotfad09c72016-06-21 12:28:20 -04003298 if (chip->mdio_np)
3299 err = of_mdiobus_register(bus, chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003300 else
3301 err = mdiobus_register(bus);
3302 if (err) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04003303 dev_err(chip->dev, "Cannot register MDIO bus (%d)\n", err);
Andrew Lunnb516d452016-06-04 21:17:06 +02003304 goto out;
3305 }
Vivien Didelotfad09c72016-06-21 12:28:20 -04003306 chip->mdio_bus = bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003307
3308 return 0;
3309
3310out:
Vivien Didelotfad09c72016-06-21 12:28:20 -04003311 if (chip->mdio_np)
3312 of_node_put(chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003313
3314 return err;
3315}
3316
Vivien Didelotfad09c72016-06-21 12:28:20 -04003317static void mv88e6xxx_mdio_unregister(struct mv88e6xxx_chip *chip)
Andrew Lunnb516d452016-06-04 21:17:06 +02003318
3319{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003320 struct mii_bus *bus = chip->mdio_bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003321
3322 mdiobus_unregister(bus);
3323
Vivien Didelotfad09c72016-06-21 12:28:20 -04003324 if (chip->mdio_np)
3325 of_node_put(chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003326}
3327
Guenter Roeckc22995c2015-07-25 09:42:28 -07003328#ifdef CONFIG_NET_DSA_HWMON
3329
3330static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
3331{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003332 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003333 int ret;
3334 int val;
3335
3336 *temp = 0;
3337
Vivien Didelotfad09c72016-06-21 12:28:20 -04003338 mutex_lock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003339
Vivien Didelotfad09c72016-06-21 12:28:20 -04003340 ret = mv88e6xxx_mdio_write_direct(chip, 0x0, 0x16, 0x6);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003341 if (ret < 0)
3342 goto error;
3343
3344 /* Enable temperature sensor */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003345 ret = mv88e6xxx_mdio_read_direct(chip, 0x0, 0x1a);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003346 if (ret < 0)
3347 goto error;
3348
Vivien Didelotfad09c72016-06-21 12:28:20 -04003349 ret = mv88e6xxx_mdio_write_direct(chip, 0x0, 0x1a, ret | (1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003350 if (ret < 0)
3351 goto error;
3352
3353 /* Wait for temperature to stabilize */
3354 usleep_range(10000, 12000);
3355
Vivien Didelotfad09c72016-06-21 12:28:20 -04003356 val = mv88e6xxx_mdio_read_direct(chip, 0x0, 0x1a);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003357 if (val < 0) {
3358 ret = val;
3359 goto error;
3360 }
3361
3362 /* Disable temperature sensor */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003363 ret = mv88e6xxx_mdio_write_direct(chip, 0x0, 0x1a, ret & ~(1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003364 if (ret < 0)
3365 goto error;
3366
3367 *temp = ((val & 0x1f) - 5) * 5;
3368
3369error:
Vivien Didelotfad09c72016-06-21 12:28:20 -04003370 mv88e6xxx_mdio_write_direct(chip, 0x0, 0x16, 0x0);
3371 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003372 return ret;
3373}
3374
3375static int mv88e63xx_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);
3378 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003379 int ret;
3380
3381 *temp = 0;
3382
Andrew Lunn03a4a542016-06-04 21:17:05 +02003383 ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 27);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003384 if (ret < 0)
3385 return ret;
3386
3387 *temp = (ret & 0xff) - 25;
3388
3389 return 0;
3390}
3391
Vivien Didelotf81ec902016-05-09 13:22:58 -04003392static int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003393{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003394 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunn158bc062016-04-28 21:24:06 -04003395
Vivien Didelotfad09c72016-06-21 12:28:20 -04003396 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP))
Vivien Didelot6594f612016-05-09 13:22:42 -04003397 return -EOPNOTSUPP;
3398
Vivien Didelotfad09c72016-06-21 12:28:20 -04003399 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003400 return mv88e63xx_get_temp(ds, temp);
3401
3402 return mv88e61xx_get_temp(ds, temp);
3403}
3404
Vivien Didelotf81ec902016-05-09 13:22:58 -04003405static int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003406{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003407 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3408 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003409 int ret;
3410
Vivien Didelotfad09c72016-06-21 12:28:20 -04003411 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003412 return -EOPNOTSUPP;
3413
3414 *temp = 0;
3415
Andrew Lunn03a4a542016-06-04 21:17:05 +02003416 ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 26);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003417 if (ret < 0)
3418 return ret;
3419
3420 *temp = (((ret >> 8) & 0x1f) * 5) - 25;
3421
3422 return 0;
3423}
3424
Vivien Didelotf81ec902016-05-09 13:22:58 -04003425static int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003426{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003427 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3428 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003429 int ret;
3430
Vivien Didelotfad09c72016-06-21 12:28:20 -04003431 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003432 return -EOPNOTSUPP;
3433
Andrew Lunn03a4a542016-06-04 21:17:05 +02003434 ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 26);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003435 if (ret < 0)
3436 return ret;
3437 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
Andrew Lunn03a4a542016-06-04 21:17:05 +02003438 return mv88e6xxx_mdio_page_write(ds, phy, 6, 26,
3439 (ret & 0xe0ff) | (temp << 8));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003440}
3441
Vivien Didelotf81ec902016-05-09 13:22:58 -04003442static int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003443{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003444 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3445 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003446 int ret;
3447
Vivien Didelotfad09c72016-06-21 12:28:20 -04003448 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003449 return -EOPNOTSUPP;
3450
3451 *alarm = false;
3452
Andrew Lunn03a4a542016-06-04 21:17:05 +02003453 ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 26);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003454 if (ret < 0)
3455 return ret;
3456
3457 *alarm = !!(ret & 0x40);
3458
3459 return 0;
3460}
3461#endif /* CONFIG_NET_DSA_HWMON */
3462
Vivien Didelot855b1932016-07-20 18:18:35 -04003463static int mv88e6xxx_get_eeprom_len(struct dsa_switch *ds)
3464{
3465 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3466
3467 return chip->eeprom_len;
3468}
3469
3470static int mv88e6xxx_get_eeprom16(struct mv88e6xxx_chip *chip,
3471 struct ethtool_eeprom *eeprom, u8 *data)
3472{
3473 unsigned int offset = eeprom->offset;
3474 unsigned int len = eeprom->len;
3475 u16 val;
3476 int err;
3477
3478 eeprom->len = 0;
3479
3480 if (offset & 1) {
3481 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
3482 if (err)
3483 return err;
3484
3485 *data++ = (val >> 8) & 0xff;
3486
3487 offset++;
3488 len--;
3489 eeprom->len++;
3490 }
3491
3492 while (len >= 2) {
3493 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
3494 if (err)
3495 return err;
3496
3497 *data++ = val & 0xff;
3498 *data++ = (val >> 8) & 0xff;
3499
3500 offset += 2;
3501 len -= 2;
3502 eeprom->len += 2;
3503 }
3504
3505 if (len) {
3506 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
3507 if (err)
3508 return err;
3509
3510 *data++ = val & 0xff;
3511
3512 offset++;
3513 len--;
3514 eeprom->len++;
3515 }
3516
3517 return 0;
3518}
3519
3520static int mv88e6xxx_get_eeprom(struct dsa_switch *ds,
3521 struct ethtool_eeprom *eeprom, u8 *data)
3522{
3523 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3524 int err;
3525
3526 mutex_lock(&chip->reg_lock);
3527
3528 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16))
3529 err = mv88e6xxx_get_eeprom16(chip, eeprom, data);
3530 else
3531 err = -EOPNOTSUPP;
3532
3533 mutex_unlock(&chip->reg_lock);
3534
3535 if (err)
3536 return err;
3537
3538 eeprom->magic = 0xc3ec4951;
3539
3540 return 0;
3541}
3542
3543static int mv88e6xxx_set_eeprom16(struct mv88e6xxx_chip *chip,
3544 struct ethtool_eeprom *eeprom, u8 *data)
3545{
3546 unsigned int offset = eeprom->offset;
3547 unsigned int len = eeprom->len;
3548 u16 val;
3549 int err;
3550
3551 /* Ensure the RO WriteEn bit is set */
3552 err = mv88e6xxx_read(chip, REG_GLOBAL2, GLOBAL2_EEPROM_CMD, &val);
3553 if (err)
3554 return err;
3555
3556 if (!(val & GLOBAL2_EEPROM_CMD_WRITE_EN))
3557 return -EROFS;
3558
3559 eeprom->len = 0;
3560
3561 if (offset & 1) {
3562 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
3563 if (err)
3564 return err;
3565
3566 val = (*data++ << 8) | (val & 0xff);
3567
3568 err = mv88e6xxx_g2_eeprom_write16(chip, offset >> 1, val);
3569 if (err)
3570 return err;
3571
3572 offset++;
3573 len--;
3574 eeprom->len++;
3575 }
3576
3577 while (len >= 2) {
3578 val = *data++;
3579 val |= *data++ << 8;
3580
3581 err = mv88e6xxx_g2_eeprom_write16(chip, offset >> 1, val);
3582 if (err)
3583 return err;
3584
3585 offset += 2;
3586 len -= 2;
3587 eeprom->len += 2;
3588 }
3589
3590 if (len) {
3591 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
3592 if (err)
3593 return err;
3594
3595 val = (val & 0xff00) | *data++;
3596
3597 err = mv88e6xxx_g2_eeprom_write16(chip, offset >> 1, val);
3598 if (err)
3599 return err;
3600
3601 offset++;
3602 len--;
3603 eeprom->len++;
3604 }
3605
3606 return 0;
3607}
3608
3609static int mv88e6xxx_set_eeprom(struct dsa_switch *ds,
3610 struct ethtool_eeprom *eeprom, u8 *data)
3611{
3612 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3613 int err;
3614
3615 if (eeprom->magic != 0xc3ec4951)
3616 return -EINVAL;
3617
3618 mutex_lock(&chip->reg_lock);
3619
3620 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16))
3621 err = mv88e6xxx_set_eeprom16(chip, eeprom, data);
3622 else
3623 err = -EOPNOTSUPP;
3624
3625 mutex_unlock(&chip->reg_lock);
3626
3627 return err;
3628}
3629
Vivien Didelotf81ec902016-05-09 13:22:58 -04003630static const struct mv88e6xxx_info mv88e6xxx_table[] = {
3631 [MV88E6085] = {
3632 .prod_num = PORT_SWITCH_ID_PROD_NUM_6085,
3633 .family = MV88E6XXX_FAMILY_6097,
3634 .name = "Marvell 88E6085",
3635 .num_databases = 4096,
3636 .num_ports = 10,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003637 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003638 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003639 .flags = MV88E6XXX_FLAGS_FAMILY_6097,
3640 },
3641
3642 [MV88E6095] = {
3643 .prod_num = PORT_SWITCH_ID_PROD_NUM_6095,
3644 .family = MV88E6XXX_FAMILY_6095,
3645 .name = "Marvell 88E6095/88E6095F",
3646 .num_databases = 256,
3647 .num_ports = 11,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003648 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003649 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003650 .flags = MV88E6XXX_FLAGS_FAMILY_6095,
3651 },
3652
3653 [MV88E6123] = {
3654 .prod_num = PORT_SWITCH_ID_PROD_NUM_6123,
3655 .family = MV88E6XXX_FAMILY_6165,
3656 .name = "Marvell 88E6123",
3657 .num_databases = 4096,
3658 .num_ports = 3,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003659 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003660 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003661 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3662 },
3663
3664 [MV88E6131] = {
3665 .prod_num = PORT_SWITCH_ID_PROD_NUM_6131,
3666 .family = MV88E6XXX_FAMILY_6185,
3667 .name = "Marvell 88E6131",
3668 .num_databases = 256,
3669 .num_ports = 8,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003670 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003671 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003672 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3673 },
3674
3675 [MV88E6161] = {
3676 .prod_num = PORT_SWITCH_ID_PROD_NUM_6161,
3677 .family = MV88E6XXX_FAMILY_6165,
3678 .name = "Marvell 88E6161",
3679 .num_databases = 4096,
3680 .num_ports = 6,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003681 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003682 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003683 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3684 },
3685
3686 [MV88E6165] = {
3687 .prod_num = PORT_SWITCH_ID_PROD_NUM_6165,
3688 .family = MV88E6XXX_FAMILY_6165,
3689 .name = "Marvell 88E6165",
3690 .num_databases = 4096,
3691 .num_ports = 6,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003692 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003693 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003694 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3695 },
3696
3697 [MV88E6171] = {
3698 .prod_num = PORT_SWITCH_ID_PROD_NUM_6171,
3699 .family = MV88E6XXX_FAMILY_6351,
3700 .name = "Marvell 88E6171",
3701 .num_databases = 4096,
3702 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003703 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003704 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003705 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3706 },
3707
3708 [MV88E6172] = {
3709 .prod_num = PORT_SWITCH_ID_PROD_NUM_6172,
3710 .family = MV88E6XXX_FAMILY_6352,
3711 .name = "Marvell 88E6172",
3712 .num_databases = 4096,
3713 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003714 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003715 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003716 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3717 },
3718
3719 [MV88E6175] = {
3720 .prod_num = PORT_SWITCH_ID_PROD_NUM_6175,
3721 .family = MV88E6XXX_FAMILY_6351,
3722 .name = "Marvell 88E6175",
3723 .num_databases = 4096,
3724 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003725 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003726 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003727 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3728 },
3729
3730 [MV88E6176] = {
3731 .prod_num = PORT_SWITCH_ID_PROD_NUM_6176,
3732 .family = MV88E6XXX_FAMILY_6352,
3733 .name = "Marvell 88E6176",
3734 .num_databases = 4096,
3735 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003736 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003737 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003738 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3739 },
3740
3741 [MV88E6185] = {
3742 .prod_num = PORT_SWITCH_ID_PROD_NUM_6185,
3743 .family = MV88E6XXX_FAMILY_6185,
3744 .name = "Marvell 88E6185",
3745 .num_databases = 256,
3746 .num_ports = 10,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003747 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003748 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003749 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3750 },
3751
3752 [MV88E6240] = {
3753 .prod_num = PORT_SWITCH_ID_PROD_NUM_6240,
3754 .family = MV88E6XXX_FAMILY_6352,
3755 .name = "Marvell 88E6240",
3756 .num_databases = 4096,
3757 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003758 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003759 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003760 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3761 },
3762
3763 [MV88E6320] = {
3764 .prod_num = PORT_SWITCH_ID_PROD_NUM_6320,
3765 .family = MV88E6XXX_FAMILY_6320,
3766 .name = "Marvell 88E6320",
3767 .num_databases = 4096,
3768 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003769 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003770 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003771 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3772 },
3773
3774 [MV88E6321] = {
3775 .prod_num = PORT_SWITCH_ID_PROD_NUM_6321,
3776 .family = MV88E6XXX_FAMILY_6320,
3777 .name = "Marvell 88E6321",
3778 .num_databases = 4096,
3779 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003780 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003781 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003782 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3783 },
3784
3785 [MV88E6350] = {
3786 .prod_num = PORT_SWITCH_ID_PROD_NUM_6350,
3787 .family = MV88E6XXX_FAMILY_6351,
3788 .name = "Marvell 88E6350",
3789 .num_databases = 4096,
3790 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003791 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003792 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003793 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3794 },
3795
3796 [MV88E6351] = {
3797 .prod_num = PORT_SWITCH_ID_PROD_NUM_6351,
3798 .family = MV88E6XXX_FAMILY_6351,
3799 .name = "Marvell 88E6351",
3800 .num_databases = 4096,
3801 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003802 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003803 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003804 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3805 },
3806
3807 [MV88E6352] = {
3808 .prod_num = PORT_SWITCH_ID_PROD_NUM_6352,
3809 .family = MV88E6XXX_FAMILY_6352,
3810 .name = "Marvell 88E6352",
3811 .num_databases = 4096,
3812 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003813 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003814 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003815 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3816 },
3817};
3818
Vivien Didelot5f7c0362016-06-20 13:14:04 -04003819static const struct mv88e6xxx_info *mv88e6xxx_lookup_info(unsigned int prod_num)
Vivien Didelotb9b37712015-10-30 19:39:48 -04003820{
Vivien Didelota439c062016-04-17 13:23:58 -04003821 int i;
Vivien Didelotb9b37712015-10-30 19:39:48 -04003822
Vivien Didelot5f7c0362016-06-20 13:14:04 -04003823 for (i = 0; i < ARRAY_SIZE(mv88e6xxx_table); ++i)
3824 if (mv88e6xxx_table[i].prod_num == prod_num)
3825 return &mv88e6xxx_table[i];
Vivien Didelotb9b37712015-10-30 19:39:48 -04003826
Vivien Didelotb9b37712015-10-30 19:39:48 -04003827 return NULL;
3828}
3829
Vivien Didelotfad09c72016-06-21 12:28:20 -04003830static int mv88e6xxx_detect(struct mv88e6xxx_chip *chip)
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003831{
3832 const struct mv88e6xxx_info *info;
3833 int id, prod_num, rev;
3834
Vivien Didelotfad09c72016-06-21 12:28:20 -04003835 id = mv88e6xxx_reg_read(chip, chip->info->port_base_addr,
3836 PORT_SWITCH_ID);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003837 if (id < 0)
3838 return id;
3839
3840 prod_num = (id & 0xfff0) >> 4;
3841 rev = id & 0x000f;
3842
3843 info = mv88e6xxx_lookup_info(prod_num);
3844 if (!info)
3845 return -ENODEV;
3846
Vivien Didelotcaac8542016-06-20 13:14:09 -04003847 /* Update the compatible info with the probed one */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003848 chip->info = info;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003849
Vivien Didelotfad09c72016-06-21 12:28:20 -04003850 dev_info(chip->dev, "switch 0x%x detected: %s, revision %u\n",
3851 chip->info->prod_num, chip->info->name, rev);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003852
3853 return 0;
3854}
3855
Vivien Didelotfad09c72016-06-21 12:28:20 -04003856static struct mv88e6xxx_chip *mv88e6xxx_alloc_chip(struct device *dev)
Vivien Didelot469d7292016-06-20 13:14:06 -04003857{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003858 struct mv88e6xxx_chip *chip;
Vivien Didelot469d7292016-06-20 13:14:06 -04003859
Vivien Didelotfad09c72016-06-21 12:28:20 -04003860 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
3861 if (!chip)
Vivien Didelot469d7292016-06-20 13:14:06 -04003862 return NULL;
3863
Vivien Didelotfad09c72016-06-21 12:28:20 -04003864 chip->dev = dev;
Vivien Didelot469d7292016-06-20 13:14:06 -04003865
Vivien Didelotfad09c72016-06-21 12:28:20 -04003866 mutex_init(&chip->reg_lock);
Vivien Didelot469d7292016-06-20 13:14:06 -04003867
Vivien Didelotfad09c72016-06-21 12:28:20 -04003868 return chip;
Vivien Didelot469d7292016-06-20 13:14:06 -04003869}
3870
Vivien Didelotfad09c72016-06-21 12:28:20 -04003871static int mv88e6xxx_smi_init(struct mv88e6xxx_chip *chip,
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003872 struct mii_bus *bus, int sw_addr)
3873{
3874 /* ADDR[0] pin is unavailable externally and considered zero */
3875 if (sw_addr & 0x1)
3876 return -EINVAL;
3877
Vivien Didelot914b32f2016-06-20 13:14:11 -04003878 if (sw_addr == 0)
Vivien Didelotfad09c72016-06-21 12:28:20 -04003879 chip->smi_ops = &mv88e6xxx_smi_single_chip_ops;
3880 else if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_MULTI_CHIP))
3881 chip->smi_ops = &mv88e6xxx_smi_multi_chip_ops;
Vivien Didelot914b32f2016-06-20 13:14:11 -04003882 else
3883 return -EINVAL;
3884
Vivien Didelotfad09c72016-06-21 12:28:20 -04003885 chip->bus = bus;
3886 chip->sw_addr = sw_addr;
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003887
3888 return 0;
3889}
3890
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02003891static const char *mv88e6xxx_drv_probe(struct device *dsa_dev,
3892 struct device *host_dev, int sw_addr,
3893 void **priv)
Andrew Lunna77d43f2016-04-13 02:40:42 +02003894{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003895 struct mv88e6xxx_chip *chip;
Vivien Didelota439c062016-04-17 13:23:58 -04003896 struct mii_bus *bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003897 int err;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003898
Vivien Didelota439c062016-04-17 13:23:58 -04003899 bus = dsa_host_dev_to_mii_bus(host_dev);
Andrew Lunnc1569132016-04-13 02:40:45 +02003900 if (!bus)
3901 return NULL;
3902
Vivien Didelotfad09c72016-06-21 12:28:20 -04003903 chip = mv88e6xxx_alloc_chip(dsa_dev);
3904 if (!chip)
Vivien Didelot469d7292016-06-20 13:14:06 -04003905 return NULL;
3906
Vivien Didelotcaac8542016-06-20 13:14:09 -04003907 /* Legacy SMI probing will only support chips similar to 88E6085 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003908 chip->info = &mv88e6xxx_table[MV88E6085];
Vivien Didelotcaac8542016-06-20 13:14:09 -04003909
Vivien Didelotfad09c72016-06-21 12:28:20 -04003910 err = mv88e6xxx_smi_init(chip, bus, sw_addr);
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003911 if (err)
3912 goto free;
3913
Vivien Didelotfad09c72016-06-21 12:28:20 -04003914 err = mv88e6xxx_detect(chip);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003915 if (err)
Vivien Didelot469d7292016-06-20 13:14:06 -04003916 goto free;
Vivien Didelota439c062016-04-17 13:23:58 -04003917
Vivien Didelotfad09c72016-06-21 12:28:20 -04003918 err = mv88e6xxx_mdio_register(chip, NULL);
Andrew Lunnb516d452016-06-04 21:17:06 +02003919 if (err)
Vivien Didelot469d7292016-06-20 13:14:06 -04003920 goto free;
Andrew Lunnb516d452016-06-04 21:17:06 +02003921
Vivien Didelotfad09c72016-06-21 12:28:20 -04003922 *priv = chip;
Vivien Didelota439c062016-04-17 13:23:58 -04003923
Vivien Didelotfad09c72016-06-21 12:28:20 -04003924 return chip->info->name;
Vivien Didelot469d7292016-06-20 13:14:06 -04003925free:
Vivien Didelotfad09c72016-06-21 12:28:20 -04003926 devm_kfree(dsa_dev, chip);
Vivien Didelot469d7292016-06-20 13:14:06 -04003927
3928 return NULL;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003929}
3930
Vivien Didelot57d32312016-06-20 13:13:58 -04003931static struct dsa_switch_driver mv88e6xxx_switch_driver = {
Vivien Didelotf81ec902016-05-09 13:22:58 -04003932 .tag_protocol = DSA_TAG_PROTO_EDSA,
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02003933 .probe = mv88e6xxx_drv_probe,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003934 .setup = mv88e6xxx_setup,
3935 .set_addr = mv88e6xxx_set_addr,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003936 .adjust_link = mv88e6xxx_adjust_link,
3937 .get_strings = mv88e6xxx_get_strings,
3938 .get_ethtool_stats = mv88e6xxx_get_ethtool_stats,
3939 .get_sset_count = mv88e6xxx_get_sset_count,
3940 .set_eee = mv88e6xxx_set_eee,
3941 .get_eee = mv88e6xxx_get_eee,
3942#ifdef CONFIG_NET_DSA_HWMON
3943 .get_temp = mv88e6xxx_get_temp,
3944 .get_temp_limit = mv88e6xxx_get_temp_limit,
3945 .set_temp_limit = mv88e6xxx_set_temp_limit,
3946 .get_temp_alarm = mv88e6xxx_get_temp_alarm,
3947#endif
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003948 .get_eeprom_len = mv88e6xxx_get_eeprom_len,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003949 .get_eeprom = mv88e6xxx_get_eeprom,
3950 .set_eeprom = mv88e6xxx_set_eeprom,
3951 .get_regs_len = mv88e6xxx_get_regs_len,
3952 .get_regs = mv88e6xxx_get_regs,
Vivien Didelot2cfcd962016-07-18 20:45:40 -04003953 .set_ageing_time = mv88e6xxx_set_ageing_time,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003954 .port_bridge_join = mv88e6xxx_port_bridge_join,
3955 .port_bridge_leave = mv88e6xxx_port_bridge_leave,
3956 .port_stp_state_set = mv88e6xxx_port_stp_state_set,
3957 .port_vlan_filtering = mv88e6xxx_port_vlan_filtering,
3958 .port_vlan_prepare = mv88e6xxx_port_vlan_prepare,
3959 .port_vlan_add = mv88e6xxx_port_vlan_add,
3960 .port_vlan_del = mv88e6xxx_port_vlan_del,
3961 .port_vlan_dump = mv88e6xxx_port_vlan_dump,
3962 .port_fdb_prepare = mv88e6xxx_port_fdb_prepare,
3963 .port_fdb_add = mv88e6xxx_port_fdb_add,
3964 .port_fdb_del = mv88e6xxx_port_fdb_del,
3965 .port_fdb_dump = mv88e6xxx_port_fdb_dump,
3966};
3967
Vivien Didelotfad09c72016-06-21 12:28:20 -04003968static int mv88e6xxx_register_switch(struct mv88e6xxx_chip *chip,
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003969 struct device_node *np)
3970{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003971 struct device *dev = chip->dev;
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003972 struct dsa_switch *ds;
3973
3974 ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
3975 if (!ds)
3976 return -ENOMEM;
3977
3978 ds->dev = dev;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003979 ds->priv = chip;
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003980 ds->drv = &mv88e6xxx_switch_driver;
3981
3982 dev_set_drvdata(dev, ds);
3983
3984 return dsa_register_switch(ds, np);
3985}
3986
Vivien Didelotfad09c72016-06-21 12:28:20 -04003987static void mv88e6xxx_unregister_switch(struct mv88e6xxx_chip *chip)
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003988{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003989 dsa_unregister_switch(chip->ds);
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003990}
3991
Vivien Didelot57d32312016-06-20 13:13:58 -04003992static int mv88e6xxx_probe(struct mdio_device *mdiodev)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003993{
3994 struct device *dev = &mdiodev->dev;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003995 struct device_node *np = dev->of_node;
Vivien Didelotcaac8542016-06-20 13:14:09 -04003996 const struct mv88e6xxx_info *compat_info;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003997 struct mv88e6xxx_chip *chip;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003998 u32 eeprom_len;
Andrew Lunn52638f72016-05-10 23:27:22 +02003999 int err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004000
Vivien Didelotcaac8542016-06-20 13:14:09 -04004001 compat_info = of_device_get_match_data(dev);
4002 if (!compat_info)
4003 return -EINVAL;
4004
Vivien Didelotfad09c72016-06-21 12:28:20 -04004005 chip = mv88e6xxx_alloc_chip(dev);
4006 if (!chip)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004007 return -ENOMEM;
4008
Vivien Didelotfad09c72016-06-21 12:28:20 -04004009 chip->info = compat_info;
Vivien Didelotcaac8542016-06-20 13:14:09 -04004010
Vivien Didelotfad09c72016-06-21 12:28:20 -04004011 err = mv88e6xxx_smi_init(chip, mdiodev->bus, mdiodev->addr);
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04004012 if (err)
4013 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004014
Vivien Didelotfad09c72016-06-21 12:28:20 -04004015 err = mv88e6xxx_detect(chip);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04004016 if (err)
4017 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004018
Vivien Didelotfad09c72016-06-21 12:28:20 -04004019 chip->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_ASIS);
4020 if (IS_ERR(chip->reset))
4021 return PTR_ERR(chip->reset);
Andrew Lunn52638f72016-05-10 23:27:22 +02004022
Vivien Didelot855b1932016-07-20 18:18:35 -04004023 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16) &&
Andrew Lunnf8cd8752016-05-10 23:27:25 +02004024 !of_property_read_u32(np, "eeprom-length", &eeprom_len))
Vivien Didelotfad09c72016-06-21 12:28:20 -04004025 chip->eeprom_len = eeprom_len;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02004026
Vivien Didelotfad09c72016-06-21 12:28:20 -04004027 err = mv88e6xxx_mdio_register(chip, np);
Andrew Lunnb516d452016-06-04 21:17:06 +02004028 if (err)
4029 return err;
4030
Vivien Didelotfad09c72016-06-21 12:28:20 -04004031 err = mv88e6xxx_register_switch(chip, np);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02004032 if (err) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04004033 mv88e6xxx_mdio_unregister(chip);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02004034 return err;
4035 }
4036
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004037 return 0;
4038}
4039
4040static void mv88e6xxx_remove(struct mdio_device *mdiodev)
4041{
4042 struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev);
Vivien Didelotfad09c72016-06-21 12:28:20 -04004043 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004044
Vivien Didelotfad09c72016-06-21 12:28:20 -04004045 mv88e6xxx_unregister_switch(chip);
4046 mv88e6xxx_mdio_unregister(chip);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004047}
4048
4049static const struct of_device_id mv88e6xxx_of_match[] = {
Vivien Didelotcaac8542016-06-20 13:14:09 -04004050 {
4051 .compatible = "marvell,mv88e6085",
4052 .data = &mv88e6xxx_table[MV88E6085],
4053 },
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004054 { /* sentinel */ },
4055};
4056
4057MODULE_DEVICE_TABLE(of, mv88e6xxx_of_match);
4058
4059static struct mdio_driver mv88e6xxx_driver = {
4060 .probe = mv88e6xxx_probe,
4061 .remove = mv88e6xxx_remove,
4062 .mdiodrv.driver = {
4063 .name = "mv88e6085",
4064 .of_match_table = mv88e6xxx_of_match,
4065 },
4066};
4067
Ben Hutchings98e67302011-11-25 14:36:19 +00004068static int __init mv88e6xxx_init(void)
4069{
Vivien Didelotf81ec902016-05-09 13:22:58 -04004070 register_switch_driver(&mv88e6xxx_switch_driver);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004071 return mdio_driver_register(&mv88e6xxx_driver);
Ben Hutchings98e67302011-11-25 14:36:19 +00004072}
4073module_init(mv88e6xxx_init);
4074
4075static void __exit mv88e6xxx_cleanup(void)
4076{
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004077 mdio_driver_unregister(&mv88e6xxx_driver);
Vivien Didelotf81ec902016-05-09 13:22:58 -04004078 unregister_switch_driver(&mv88e6xxx_switch_driver);
Ben Hutchings98e67302011-11-25 14:36:19 +00004079}
4080module_exit(mv88e6xxx_cleanup);
Ben Hutchings3d825ed2011-11-25 14:37:16 +00004081
4082MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
4083MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
4084MODULE_LICENSE("GPL");