blob: 44debf22419b88397a1405932eb9347bd5fac973 [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 Didelot2d79af62016-08-15 17:18:57 -0400219static int mv88e6xxx_wait(struct mv88e6xxx_chip *chip, int addr, int reg,
220 u16 mask)
221{
222 unsigned long timeout = jiffies + HZ / 10;
223
224 while (time_before(jiffies, timeout)) {
225 u16 val;
226 int err;
227
228 err = mv88e6xxx_read(chip, addr, reg, &val);
229 if (err)
230 return err;
231
232 if (!(val & mask))
233 return 0;
234
235 usleep_range(1000, 2000);
236 }
237
238 return -ETIMEDOUT;
239}
240
Vivien Didelotf22ab642016-07-18 20:45:31 -0400241/* Indirect write to single pointer-data register with an Update bit */
242static int mv88e6xxx_update(struct mv88e6xxx_chip *chip, int addr, int reg,
243 u16 update)
244{
245 u16 val;
246 int i, err;
247
248 /* Wait until the previous operation is completed */
249 for (i = 0; i < 16; ++i) {
250 err = mv88e6xxx_read(chip, addr, reg, &val);
251 if (err)
252 return err;
253
254 if (!(val & BIT(15)))
255 break;
256 }
257
258 if (i == 16)
259 return -ETIMEDOUT;
260
261 /* Set the Update bit to trigger a write operation */
262 val = BIT(15) | update;
263
264 return mv88e6xxx_write(chip, addr, reg, val);
265}
266
Vivien Didelotfad09c72016-06-21 12:28:20 -0400267static int _mv88e6xxx_reg_read(struct mv88e6xxx_chip *chip, int addr, int reg)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000268{
Vivien Didelot914b32f2016-06-20 13:14:11 -0400269 u16 val;
270 int err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000271
Vivien Didelotfad09c72016-06-21 12:28:20 -0400272 err = mv88e6xxx_read(chip, addr, reg, &val);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400273 if (err)
274 return err;
Vivien Didelot3996a4f2015-10-30 18:56:45 -0400275
Vivien Didelot914b32f2016-06-20 13:14:11 -0400276 return val;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000277}
278
Vivien Didelotfad09c72016-06-21 12:28:20 -0400279static int _mv88e6xxx_reg_write(struct mv88e6xxx_chip *chip, int addr,
Andrew Lunn158bc062016-04-28 21:24:06 -0400280 int reg, u16 val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000281{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400282 return mv88e6xxx_write(chip, addr, reg, val);
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700283}
284
Vivien Didelotfad09c72016-06-21 12:28:20 -0400285static int mv88e6xxx_mdio_read_direct(struct mv88e6xxx_chip *chip,
Andrew Lunn03a4a542016-06-04 21:17:05 +0200286 int addr, int regnum)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000287{
288 if (addr >= 0)
Vivien Didelotfad09c72016-06-21 12:28:20 -0400289 return _mv88e6xxx_reg_read(chip, addr, regnum);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000290 return 0xffff;
291}
292
Vivien Didelotfad09c72016-06-21 12:28:20 -0400293static int mv88e6xxx_mdio_write_direct(struct mv88e6xxx_chip *chip,
Andrew Lunn03a4a542016-06-04 21:17:05 +0200294 int addr, int regnum, u16 val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000295{
296 if (addr >= 0)
Vivien Didelotfad09c72016-06-21 12:28:20 -0400297 return _mv88e6xxx_reg_write(chip, addr, regnum, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000298 return 0;
299}
300
Vivien Didelotfad09c72016-06-21 12:28:20 -0400301static int mv88e6xxx_ppu_disable(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000302{
303 int ret;
Barry Grussling19b2f972013-01-08 16:05:54 +0000304 unsigned long timeout;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000305
Vivien Didelotfad09c72016-06-21 12:28:20 -0400306 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_CONTROL);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200307 if (ret < 0)
308 return ret;
309
Vivien Didelotfad09c72016-06-21 12:28:20 -0400310 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL,
Vivien Didelot8c9983a2016-05-09 13:22:39 -0400311 ret & ~GLOBAL_CONTROL_PPU_ENABLE);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200312 if (ret)
313 return ret;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000314
Barry Grussling19b2f972013-01-08 16:05:54 +0000315 timeout = jiffies + 1 * HZ;
316 while (time_before(jiffies, timeout)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400317 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATUS);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200318 if (ret < 0)
319 return ret;
320
Barry Grussling19b2f972013-01-08 16:05:54 +0000321 usleep_range(1000, 2000);
Andrew Lunncca8b132015-04-02 04:06:39 +0200322 if ((ret & GLOBAL_STATUS_PPU_MASK) !=
323 GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000324 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000325 }
326
327 return -ETIMEDOUT;
328}
329
Vivien Didelotfad09c72016-06-21 12:28:20 -0400330static int mv88e6xxx_ppu_enable(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000331{
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200332 int ret, err;
Barry Grussling19b2f972013-01-08 16:05:54 +0000333 unsigned long timeout;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000334
Vivien Didelotfad09c72016-06-21 12:28:20 -0400335 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_CONTROL);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200336 if (ret < 0)
337 return ret;
338
Vivien Didelotfad09c72016-06-21 12:28:20 -0400339 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL,
Vivien Didelot762eb672016-06-04 21:16:54 +0200340 ret | GLOBAL_CONTROL_PPU_ENABLE);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200341 if (err)
342 return err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000343
Barry Grussling19b2f972013-01-08 16:05:54 +0000344 timeout = jiffies + 1 * HZ;
345 while (time_before(jiffies, timeout)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400346 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATUS);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200347 if (ret < 0)
348 return ret;
349
Barry Grussling19b2f972013-01-08 16:05:54 +0000350 usleep_range(1000, 2000);
Andrew Lunncca8b132015-04-02 04:06:39 +0200351 if ((ret & GLOBAL_STATUS_PPU_MASK) ==
352 GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000353 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000354 }
355
356 return -ETIMEDOUT;
357}
358
359static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
360{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400361 struct mv88e6xxx_chip *chip;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000362
Vivien Didelotfad09c72016-06-21 12:28:20 -0400363 chip = container_of(ugly, struct mv88e6xxx_chip, ppu_work);
Vivien Didelot762eb672016-06-04 21:16:54 +0200364
Vivien Didelotfad09c72016-06-21 12:28:20 -0400365 mutex_lock(&chip->reg_lock);
Vivien Didelot762eb672016-06-04 21:16:54 +0200366
Vivien Didelotfad09c72016-06-21 12:28:20 -0400367 if (mutex_trylock(&chip->ppu_mutex)) {
368 if (mv88e6xxx_ppu_enable(chip) == 0)
369 chip->ppu_disabled = 0;
370 mutex_unlock(&chip->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000371 }
Vivien Didelot762eb672016-06-04 21:16:54 +0200372
Vivien Didelotfad09c72016-06-21 12:28:20 -0400373 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000374}
375
376static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
377{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400378 struct mv88e6xxx_chip *chip = (void *)_ps;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000379
Vivien Didelotfad09c72016-06-21 12:28:20 -0400380 schedule_work(&chip->ppu_work);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000381}
382
Vivien Didelotfad09c72016-06-21 12:28:20 -0400383static int mv88e6xxx_ppu_access_get(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000384{
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000385 int ret;
386
Vivien Didelotfad09c72016-06-21 12:28:20 -0400387 mutex_lock(&chip->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000388
Barry Grussling3675c8d2013-01-08 16:05:53 +0000389 /* If the PHY polling unit is enabled, disable it so that
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000390 * we can access the PHY registers. If it was already
391 * disabled, cancel the timer that is going to re-enable
392 * it.
393 */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400394 if (!chip->ppu_disabled) {
395 ret = mv88e6xxx_ppu_disable(chip);
Barry Grussling85686582013-01-08 16:05:56 +0000396 if (ret < 0) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400397 mutex_unlock(&chip->ppu_mutex);
Barry Grussling85686582013-01-08 16:05:56 +0000398 return ret;
399 }
Vivien Didelotfad09c72016-06-21 12:28:20 -0400400 chip->ppu_disabled = 1;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000401 } else {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400402 del_timer(&chip->ppu_timer);
Barry Grussling85686582013-01-08 16:05:56 +0000403 ret = 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000404 }
405
406 return ret;
407}
408
Vivien Didelotfad09c72016-06-21 12:28:20 -0400409static void mv88e6xxx_ppu_access_put(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000410{
Barry Grussling3675c8d2013-01-08 16:05:53 +0000411 /* Schedule a timer to re-enable the PHY polling unit. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400412 mod_timer(&chip->ppu_timer, jiffies + msecs_to_jiffies(10));
413 mutex_unlock(&chip->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000414}
415
Vivien Didelotfad09c72016-06-21 12:28:20 -0400416static void mv88e6xxx_ppu_state_init(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000417{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400418 mutex_init(&chip->ppu_mutex);
419 INIT_WORK(&chip->ppu_work, mv88e6xxx_ppu_reenable_work);
420 init_timer(&chip->ppu_timer);
421 chip->ppu_timer.data = (unsigned long)chip;
422 chip->ppu_timer.function = mv88e6xxx_ppu_reenable_timer;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000423}
424
Vivien Didelotfad09c72016-06-21 12:28:20 -0400425static int mv88e6xxx_mdio_read_ppu(struct mv88e6xxx_chip *chip, int addr,
Andrew Lunn03a4a542016-06-04 21:17:05 +0200426 int regnum)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000427{
428 int ret;
429
Vivien Didelotfad09c72016-06-21 12:28:20 -0400430 ret = mv88e6xxx_ppu_access_get(chip);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000431 if (ret >= 0) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400432 ret = _mv88e6xxx_reg_read(chip, addr, regnum);
433 mv88e6xxx_ppu_access_put(chip);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000434 }
435
436 return ret;
437}
438
Vivien Didelotfad09c72016-06-21 12:28:20 -0400439static int mv88e6xxx_mdio_write_ppu(struct mv88e6xxx_chip *chip, int addr,
Andrew Lunn03a4a542016-06-04 21:17:05 +0200440 int regnum, u16 val)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000441{
442 int ret;
443
Vivien Didelotfad09c72016-06-21 12:28:20 -0400444 ret = mv88e6xxx_ppu_access_get(chip);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000445 if (ret >= 0) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400446 ret = _mv88e6xxx_reg_write(chip, addr, regnum, val);
447 mv88e6xxx_ppu_access_put(chip);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000448 }
449
450 return ret;
451}
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000452
Vivien Didelotfad09c72016-06-21 12:28:20 -0400453static bool mv88e6xxx_6065_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200454{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400455 return chip->info->family == MV88E6XXX_FAMILY_6065;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200456}
457
Vivien Didelotfad09c72016-06-21 12:28:20 -0400458static bool mv88e6xxx_6095_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200459{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400460 return chip->info->family == MV88E6XXX_FAMILY_6095;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200461}
462
Vivien Didelotfad09c72016-06-21 12:28:20 -0400463static bool mv88e6xxx_6097_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200464{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400465 return chip->info->family == MV88E6XXX_FAMILY_6097;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200466}
467
Vivien Didelotfad09c72016-06-21 12:28:20 -0400468static bool mv88e6xxx_6165_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200469{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400470 return chip->info->family == MV88E6XXX_FAMILY_6165;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200471}
472
Vivien Didelotfad09c72016-06-21 12:28:20 -0400473static bool mv88e6xxx_6185_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200474{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400475 return chip->info->family == MV88E6XXX_FAMILY_6185;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200476}
477
Vivien Didelotfad09c72016-06-21 12:28:20 -0400478static bool mv88e6xxx_6320_family(struct mv88e6xxx_chip *chip)
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700479{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400480 return chip->info->family == MV88E6XXX_FAMILY_6320;
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700481}
482
Vivien Didelotfad09c72016-06-21 12:28:20 -0400483static bool mv88e6xxx_6351_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200484{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400485 return chip->info->family == MV88E6XXX_FAMILY_6351;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200486}
487
Vivien Didelotfad09c72016-06-21 12:28:20 -0400488static bool mv88e6xxx_6352_family(struct mv88e6xxx_chip *chip)
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200489{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400490 return chip->info->family == MV88E6XXX_FAMILY_6352;
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200491}
492
Vivien Didelotfad09c72016-06-21 12:28:20 -0400493static unsigned int mv88e6xxx_num_databases(struct mv88e6xxx_chip *chip)
Vivien Didelotf74df0b2016-03-31 16:53:43 -0400494{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400495 return chip->info->num_databases;
Vivien Didelotf74df0b2016-03-31 16:53:43 -0400496}
497
Vivien Didelotfad09c72016-06-21 12:28:20 -0400498static bool mv88e6xxx_has_fid_reg(struct mv88e6xxx_chip *chip)
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400499{
500 /* Does the device have dedicated FID registers for ATU and VTU ops? */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400501 if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
502 mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip))
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400503 return true;
504
505 return false;
506}
507
Andrew Lunndea87022015-08-31 15:56:47 +0200508/* We expect the switch to perform auto negotiation if there is a real
509 * phy. However, in the case of a fixed link phy, we force the port
510 * settings from the fixed link settings.
511 */
Vivien Didelotf81ec902016-05-09 13:22:58 -0400512static void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port,
513 struct phy_device *phydev)
Andrew Lunndea87022015-08-31 15:56:47 +0200514{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400515 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunn49052872015-09-29 01:53:48 +0200516 u32 reg;
517 int ret;
Andrew Lunndea87022015-08-31 15:56:47 +0200518
519 if (!phy_is_pseudo_fixed_link(phydev))
520 return;
521
Vivien Didelotfad09c72016-06-21 12:28:20 -0400522 mutex_lock(&chip->reg_lock);
Andrew Lunndea87022015-08-31 15:56:47 +0200523
Vivien Didelotfad09c72016-06-21 12:28:20 -0400524 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_PCS_CTRL);
Andrew Lunndea87022015-08-31 15:56:47 +0200525 if (ret < 0)
526 goto out;
527
528 reg = ret & ~(PORT_PCS_CTRL_LINK_UP |
529 PORT_PCS_CTRL_FORCE_LINK |
530 PORT_PCS_CTRL_DUPLEX_FULL |
531 PORT_PCS_CTRL_FORCE_DUPLEX |
532 PORT_PCS_CTRL_UNFORCED);
533
534 reg |= PORT_PCS_CTRL_FORCE_LINK;
535 if (phydev->link)
Vivien Didelot57d32312016-06-20 13:13:58 -0400536 reg |= PORT_PCS_CTRL_LINK_UP;
Andrew Lunndea87022015-08-31 15:56:47 +0200537
Vivien Didelotfad09c72016-06-21 12:28:20 -0400538 if (mv88e6xxx_6065_family(chip) && phydev->speed > SPEED_100)
Andrew Lunndea87022015-08-31 15:56:47 +0200539 goto out;
540
541 switch (phydev->speed) {
542 case SPEED_1000:
543 reg |= PORT_PCS_CTRL_1000;
544 break;
545 case SPEED_100:
546 reg |= PORT_PCS_CTRL_100;
547 break;
548 case SPEED_10:
549 reg |= PORT_PCS_CTRL_10;
550 break;
551 default:
552 pr_info("Unknown speed");
553 goto out;
554 }
555
556 reg |= PORT_PCS_CTRL_FORCE_DUPLEX;
557 if (phydev->duplex == DUPLEX_FULL)
558 reg |= PORT_PCS_CTRL_DUPLEX_FULL;
559
Vivien Didelotfad09c72016-06-21 12:28:20 -0400560 if ((mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip)) &&
561 (port >= chip->info->num_ports - 2)) {
Andrew Lunne7e72ac2015-08-31 15:56:51 +0200562 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
563 reg |= PORT_PCS_CTRL_RGMII_DELAY_RXCLK;
564 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
565 reg |= PORT_PCS_CTRL_RGMII_DELAY_TXCLK;
566 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
567 reg |= (PORT_PCS_CTRL_RGMII_DELAY_RXCLK |
568 PORT_PCS_CTRL_RGMII_DELAY_TXCLK);
569 }
Vivien Didelotfad09c72016-06-21 12:28:20 -0400570 _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_PCS_CTRL, reg);
Andrew Lunndea87022015-08-31 15:56:47 +0200571
572out:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400573 mutex_unlock(&chip->reg_lock);
Andrew Lunndea87022015-08-31 15:56:47 +0200574}
575
Vivien Didelotfad09c72016-06-21 12:28:20 -0400576static int _mv88e6xxx_stats_wait(struct mv88e6xxx_chip *chip)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000577{
578 int ret;
579 int i;
580
581 for (i = 0; i < 10; i++) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400582 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_OP);
Andrew Lunncca8b132015-04-02 04:06:39 +0200583 if ((ret & GLOBAL_STATS_OP_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000584 return 0;
585 }
586
587 return -ETIMEDOUT;
588}
589
Vivien Didelotfad09c72016-06-21 12:28:20 -0400590static int _mv88e6xxx_stats_snapshot(struct mv88e6xxx_chip *chip, int port)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000591{
592 int ret;
593
Vivien Didelotfad09c72016-06-21 12:28:20 -0400594 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200595 port = (port + 1) << 5;
596
Barry Grussling3675c8d2013-01-08 16:05:53 +0000597 /* Snapshot the hardware statistics counters for this port. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400598 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
Andrew Lunn31888232015-05-06 01:09:54 +0200599 GLOBAL_STATS_OP_CAPTURE_PORT |
600 GLOBAL_STATS_OP_HIST_RX_TX | port);
601 if (ret < 0)
602 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000603
Barry Grussling3675c8d2013-01-08 16:05:53 +0000604 /* Wait for the snapshotting to complete. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400605 ret = _mv88e6xxx_stats_wait(chip);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000606 if (ret < 0)
607 return ret;
608
609 return 0;
610}
611
Vivien Didelotfad09c72016-06-21 12:28:20 -0400612static void _mv88e6xxx_stats_read(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -0400613 int stat, u32 *val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000614{
615 u32 _val;
616 int ret;
617
618 *val = 0;
619
Vivien Didelotfad09c72016-06-21 12:28:20 -0400620 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
Andrew Lunn31888232015-05-06 01:09:54 +0200621 GLOBAL_STATS_OP_READ_CAPTURED |
622 GLOBAL_STATS_OP_HIST_RX_TX | stat);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000623 if (ret < 0)
624 return;
625
Vivien Didelotfad09c72016-06-21 12:28:20 -0400626 ret = _mv88e6xxx_stats_wait(chip);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000627 if (ret < 0)
628 return;
629
Vivien Didelotfad09c72016-06-21 12:28:20 -0400630 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_COUNTER_32);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000631 if (ret < 0)
632 return;
633
634 _val = ret << 16;
635
Vivien Didelotfad09c72016-06-21 12:28:20 -0400636 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_COUNTER_01);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000637 if (ret < 0)
638 return;
639
640 *val = _val | ret;
641}
642
Andrew Lunne413e7e2015-04-02 04:06:38 +0200643static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100644 { "in_good_octets", 8, 0x00, BANK0, },
645 { "in_bad_octets", 4, 0x02, BANK0, },
646 { "in_unicast", 4, 0x04, BANK0, },
647 { "in_broadcasts", 4, 0x06, BANK0, },
648 { "in_multicasts", 4, 0x07, BANK0, },
649 { "in_pause", 4, 0x16, BANK0, },
650 { "in_undersize", 4, 0x18, BANK0, },
651 { "in_fragments", 4, 0x19, BANK0, },
652 { "in_oversize", 4, 0x1a, BANK0, },
653 { "in_jabber", 4, 0x1b, BANK0, },
654 { "in_rx_error", 4, 0x1c, BANK0, },
655 { "in_fcs_error", 4, 0x1d, BANK0, },
656 { "out_octets", 8, 0x0e, BANK0, },
657 { "out_unicast", 4, 0x10, BANK0, },
658 { "out_broadcasts", 4, 0x13, BANK0, },
659 { "out_multicasts", 4, 0x12, BANK0, },
660 { "out_pause", 4, 0x15, BANK0, },
661 { "excessive", 4, 0x11, BANK0, },
662 { "collisions", 4, 0x1e, BANK0, },
663 { "deferred", 4, 0x05, BANK0, },
664 { "single", 4, 0x14, BANK0, },
665 { "multiple", 4, 0x17, BANK0, },
666 { "out_fcs_error", 4, 0x03, BANK0, },
667 { "late", 4, 0x1f, BANK0, },
668 { "hist_64bytes", 4, 0x08, BANK0, },
669 { "hist_65_127bytes", 4, 0x09, BANK0, },
670 { "hist_128_255bytes", 4, 0x0a, BANK0, },
671 { "hist_256_511bytes", 4, 0x0b, BANK0, },
672 { "hist_512_1023bytes", 4, 0x0c, BANK0, },
673 { "hist_1024_max_bytes", 4, 0x0d, BANK0, },
674 { "sw_in_discards", 4, 0x10, PORT, },
675 { "sw_in_filtered", 2, 0x12, PORT, },
676 { "sw_out_filtered", 2, 0x13, PORT, },
677 { "in_discards", 4, 0x00 | GLOBAL_STATS_OP_BANK_1, BANK1, },
678 { "in_filtered", 4, 0x01 | GLOBAL_STATS_OP_BANK_1, BANK1, },
679 { "in_accepted", 4, 0x02 | GLOBAL_STATS_OP_BANK_1, BANK1, },
680 { "in_bad_accepted", 4, 0x03 | GLOBAL_STATS_OP_BANK_1, BANK1, },
681 { "in_good_avb_class_a", 4, 0x04 | GLOBAL_STATS_OP_BANK_1, BANK1, },
682 { "in_good_avb_class_b", 4, 0x05 | GLOBAL_STATS_OP_BANK_1, BANK1, },
683 { "in_bad_avb_class_a", 4, 0x06 | GLOBAL_STATS_OP_BANK_1, BANK1, },
684 { "in_bad_avb_class_b", 4, 0x07 | GLOBAL_STATS_OP_BANK_1, BANK1, },
685 { "tcam_counter_0", 4, 0x08 | GLOBAL_STATS_OP_BANK_1, BANK1, },
686 { "tcam_counter_1", 4, 0x09 | GLOBAL_STATS_OP_BANK_1, BANK1, },
687 { "tcam_counter_2", 4, 0x0a | GLOBAL_STATS_OP_BANK_1, BANK1, },
688 { "tcam_counter_3", 4, 0x0b | GLOBAL_STATS_OP_BANK_1, BANK1, },
689 { "in_da_unknown", 4, 0x0e | GLOBAL_STATS_OP_BANK_1, BANK1, },
690 { "in_management", 4, 0x0f | GLOBAL_STATS_OP_BANK_1, BANK1, },
691 { "out_queue_0", 4, 0x10 | GLOBAL_STATS_OP_BANK_1, BANK1, },
692 { "out_queue_1", 4, 0x11 | GLOBAL_STATS_OP_BANK_1, BANK1, },
693 { "out_queue_2", 4, 0x12 | GLOBAL_STATS_OP_BANK_1, BANK1, },
694 { "out_queue_3", 4, 0x13 | GLOBAL_STATS_OP_BANK_1, BANK1, },
695 { "out_queue_4", 4, 0x14 | GLOBAL_STATS_OP_BANK_1, BANK1, },
696 { "out_queue_5", 4, 0x15 | GLOBAL_STATS_OP_BANK_1, BANK1, },
697 { "out_queue_6", 4, 0x16 | GLOBAL_STATS_OP_BANK_1, BANK1, },
698 { "out_queue_7", 4, 0x17 | GLOBAL_STATS_OP_BANK_1, BANK1, },
699 { "out_cut_through", 4, 0x18 | GLOBAL_STATS_OP_BANK_1, BANK1, },
700 { "out_octets_a", 4, 0x1a | GLOBAL_STATS_OP_BANK_1, BANK1, },
701 { "out_octets_b", 4, 0x1b | GLOBAL_STATS_OP_BANK_1, BANK1, },
702 { "out_management", 4, 0x1f | GLOBAL_STATS_OP_BANK_1, BANK1, },
Andrew Lunne413e7e2015-04-02 04:06:38 +0200703};
704
Vivien Didelotfad09c72016-06-21 12:28:20 -0400705static bool mv88e6xxx_has_stat(struct mv88e6xxx_chip *chip,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100706 struct mv88e6xxx_hw_stat *stat)
Andrew Lunne413e7e2015-04-02 04:06:38 +0200707{
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100708 switch (stat->type) {
709 case BANK0:
Andrew Lunne413e7e2015-04-02 04:06:38 +0200710 return true;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100711 case BANK1:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400712 return mv88e6xxx_6320_family(chip);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100713 case PORT:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400714 return mv88e6xxx_6095_family(chip) ||
715 mv88e6xxx_6185_family(chip) ||
716 mv88e6xxx_6097_family(chip) ||
717 mv88e6xxx_6165_family(chip) ||
718 mv88e6xxx_6351_family(chip) ||
719 mv88e6xxx_6352_family(chip);
Andrew Lunne413e7e2015-04-02 04:06:38 +0200720 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100721 return false;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000722}
723
Vivien Didelotfad09c72016-06-21 12:28:20 -0400724static uint64_t _mv88e6xxx_get_ethtool_stat(struct mv88e6xxx_chip *chip,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100725 struct mv88e6xxx_hw_stat *s,
Andrew Lunn80c46272015-06-20 18:42:30 +0200726 int port)
727{
Andrew Lunn80c46272015-06-20 18:42:30 +0200728 u32 low;
729 u32 high = 0;
730 int ret;
731 u64 value;
732
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100733 switch (s->type) {
734 case PORT:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400735 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), s->reg);
Andrew Lunn80c46272015-06-20 18:42:30 +0200736 if (ret < 0)
737 return UINT64_MAX;
738
739 low = ret;
740 if (s->sizeof_stat == 4) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400741 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port),
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100742 s->reg + 1);
Andrew Lunn80c46272015-06-20 18:42:30 +0200743 if (ret < 0)
744 return UINT64_MAX;
745 high = ret;
746 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100747 break;
748 case BANK0:
749 case BANK1:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400750 _mv88e6xxx_stats_read(chip, s->reg, &low);
Andrew Lunn80c46272015-06-20 18:42:30 +0200751 if (s->sizeof_stat == 8)
Vivien Didelotfad09c72016-06-21 12:28:20 -0400752 _mv88e6xxx_stats_read(chip, s->reg + 1, &high);
Andrew Lunn80c46272015-06-20 18:42:30 +0200753 }
754 value = (((u64)high) << 16) | low;
755 return value;
756}
757
Vivien Didelotf81ec902016-05-09 13:22:58 -0400758static void mv88e6xxx_get_strings(struct dsa_switch *ds, int port,
759 uint8_t *data)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100760{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400761 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100762 struct mv88e6xxx_hw_stat *stat;
763 int i, j;
764
765 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
766 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400767 if (mv88e6xxx_has_stat(chip, stat)) {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100768 memcpy(data + j * ETH_GSTRING_LEN, stat->string,
769 ETH_GSTRING_LEN);
770 j++;
771 }
772 }
773}
774
Vivien Didelotf81ec902016-05-09 13:22:58 -0400775static int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100776{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400777 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100778 struct mv88e6xxx_hw_stat *stat;
779 int i, j;
780
781 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
782 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400783 if (mv88e6xxx_has_stat(chip, stat))
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100784 j++;
785 }
786 return j;
787}
788
Vivien Didelotf81ec902016-05-09 13:22:58 -0400789static void mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, int port,
790 uint64_t *data)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000791{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400792 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100793 struct mv88e6xxx_hw_stat *stat;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000794 int ret;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100795 int i, j;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000796
Vivien Didelotfad09c72016-06-21 12:28:20 -0400797 mutex_lock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000798
Vivien Didelotfad09c72016-06-21 12:28:20 -0400799 ret = _mv88e6xxx_stats_snapshot(chip, port);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000800 if (ret < 0) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400801 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000802 return;
803 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100804 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
805 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400806 if (mv88e6xxx_has_stat(chip, stat)) {
807 data[j] = _mv88e6xxx_get_ethtool_stat(chip, stat, port);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100808 j++;
809 }
810 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000811
Vivien Didelotfad09c72016-06-21 12:28:20 -0400812 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000813}
Ben Hutchings98e67302011-11-25 14:36:19 +0000814
Vivien Didelotf81ec902016-05-09 13:22:58 -0400815static int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700816{
817 return 32 * sizeof(u16);
818}
819
Vivien Didelotf81ec902016-05-09 13:22:58 -0400820static void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
821 struct ethtool_regs *regs, void *_p)
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700822{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400823 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700824 u16 *p = _p;
825 int i;
826
827 regs->version = 0;
828
829 memset(p, 0xff, 32 * sizeof(u16));
830
Vivien Didelotfad09c72016-06-21 12:28:20 -0400831 mutex_lock(&chip->reg_lock);
Vivien Didelot23062512016-05-09 13:22:45 -0400832
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700833 for (i = 0; i < 32; i++) {
834 int ret;
835
Vivien Didelotfad09c72016-06-21 12:28:20 -0400836 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), i);
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700837 if (ret >= 0)
838 p[i] = ret;
839 }
Vivien Didelot23062512016-05-09 13:22:45 -0400840
Vivien Didelotfad09c72016-06-21 12:28:20 -0400841 mutex_unlock(&chip->reg_lock);
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700842}
843
Vivien Didelotfad09c72016-06-21 12:28:20 -0400844static int mv88e6xxx_mdio_wait(struct mv88e6xxx_chip *chip)
Andrew Lunn3898c142015-05-06 01:09:53 +0200845{
Vivien Didelot2d79af62016-08-15 17:18:57 -0400846 return mv88e6xxx_wait(chip, REG_GLOBAL2, GLOBAL2_SMI_OP,
847 GLOBAL2_SMI_OP_BUSY);
Andrew Lunn3898c142015-05-06 01:09:53 +0200848}
849
Vivien Didelotfad09c72016-06-21 12:28:20 -0400850static int _mv88e6xxx_atu_wait(struct mv88e6xxx_chip *chip)
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700851{
Vivien Didelot2d79af62016-08-15 17:18:57 -0400852 return mv88e6xxx_wait(chip, REG_GLOBAL, GLOBAL_ATU_OP,
853 GLOBAL_ATU_OP_BUSY);
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700854}
855
Vivien Didelotfad09c72016-06-21 12:28:20 -0400856static int mv88e6xxx_mdio_read_indirect(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -0400857 int addr, int regnum)
Andrew Lunnf3044682015-02-14 19:17:50 +0100858{
859 int ret;
860
Vivien Didelotfad09c72016-06-21 12:28:20 -0400861 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_SMI_OP,
Andrew Lunn3898c142015-05-06 01:09:53 +0200862 GLOBAL2_SMI_OP_22_READ | (addr << 5) |
863 regnum);
Andrew Lunnf3044682015-02-14 19:17:50 +0100864 if (ret < 0)
865 return ret;
866
Vivien Didelotfad09c72016-06-21 12:28:20 -0400867 ret = mv88e6xxx_mdio_wait(chip);
Andrew Lunn3898c142015-05-06 01:09:53 +0200868 if (ret < 0)
869 return ret;
870
Vivien Didelotfad09c72016-06-21 12:28:20 -0400871 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL2, GLOBAL2_SMI_DATA);
Andrew Lunn158bc062016-04-28 21:24:06 -0400872
873 return ret;
Andrew Lunnf3044682015-02-14 19:17:50 +0100874}
875
Vivien Didelotfad09c72016-06-21 12:28:20 -0400876static int mv88e6xxx_mdio_write_indirect(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -0400877 int addr, int regnum, u16 val)
Andrew Lunnf3044682015-02-14 19:17:50 +0100878{
Andrew Lunn3898c142015-05-06 01:09:53 +0200879 int ret;
Andrew Lunnf3044682015-02-14 19:17:50 +0100880
Vivien Didelotfad09c72016-06-21 12:28:20 -0400881 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_SMI_DATA, val);
Andrew Lunn3898c142015-05-06 01:09:53 +0200882 if (ret < 0)
883 return ret;
884
Vivien Didelotfad09c72016-06-21 12:28:20 -0400885 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_SMI_OP,
Andrew Lunn3898c142015-05-06 01:09:53 +0200886 GLOBAL2_SMI_OP_22_WRITE | (addr << 5) |
887 regnum);
888
Vivien Didelotfad09c72016-06-21 12:28:20 -0400889 return mv88e6xxx_mdio_wait(chip);
Andrew Lunnf3044682015-02-14 19:17:50 +0100890}
891
Vivien Didelotf81ec902016-05-09 13:22:58 -0400892static int mv88e6xxx_get_eee(struct dsa_switch *ds, int port,
893 struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -0800894{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400895 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Guenter Roeck11b3b452015-03-06 22:23:51 -0800896 int reg;
897
Vivien Didelotfad09c72016-06-21 12:28:20 -0400898 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
Vivien Didelotaadbdb82016-05-09 13:22:44 -0400899 return -EOPNOTSUPP;
900
Vivien Didelotfad09c72016-06-21 12:28:20 -0400901 mutex_lock(&chip->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200902
Vivien Didelotfad09c72016-06-21 12:28:20 -0400903 reg = mv88e6xxx_mdio_read_indirect(chip, port, 16);
Guenter Roeck11b3b452015-03-06 22:23:51 -0800904 if (reg < 0)
Andrew Lunn2f40c692015-04-02 04:06:37 +0200905 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800906
907 e->eee_enabled = !!(reg & 0x0200);
908 e->tx_lpi_enabled = !!(reg & 0x0100);
909
Vivien Didelotfad09c72016-06-21 12:28:20 -0400910 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_STATUS);
Guenter Roeck11b3b452015-03-06 22:23:51 -0800911 if (reg < 0)
Andrew Lunn2f40c692015-04-02 04:06:37 +0200912 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800913
Andrew Lunncca8b132015-04-02 04:06:39 +0200914 e->eee_active = !!(reg & PORT_STATUS_EEE);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200915 reg = 0;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800916
Andrew Lunn2f40c692015-04-02 04:06:37 +0200917out:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400918 mutex_unlock(&chip->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200919 return reg;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800920}
921
Vivien Didelotf81ec902016-05-09 13:22:58 -0400922static int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
923 struct phy_device *phydev, struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -0800924{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400925 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200926 int reg;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800927 int ret;
928
Vivien Didelotfad09c72016-06-21 12:28:20 -0400929 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
Vivien Didelotaadbdb82016-05-09 13:22:44 -0400930 return -EOPNOTSUPP;
931
Vivien Didelotfad09c72016-06-21 12:28:20 -0400932 mutex_lock(&chip->reg_lock);
Guenter Roeck11b3b452015-03-06 22:23:51 -0800933
Vivien Didelotfad09c72016-06-21 12:28:20 -0400934 ret = mv88e6xxx_mdio_read_indirect(chip, port, 16);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200935 if (ret < 0)
936 goto out;
937
938 reg = ret & ~0x0300;
939 if (e->eee_enabled)
940 reg |= 0x0200;
941 if (e->tx_lpi_enabled)
942 reg |= 0x0100;
943
Vivien Didelotfad09c72016-06-21 12:28:20 -0400944 ret = mv88e6xxx_mdio_write_indirect(chip, port, 16, reg);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200945out:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400946 mutex_unlock(&chip->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200947
948 return ret;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800949}
950
Vivien Didelotfad09c72016-06-21 12:28:20 -0400951static int _mv88e6xxx_atu_cmd(struct mv88e6xxx_chip *chip, u16 fid, u16 cmd)
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700952{
953 int ret;
954
Vivien Didelotfad09c72016-06-21 12:28:20 -0400955 if (mv88e6xxx_has_fid_reg(chip)) {
956 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_FID,
957 fid);
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400958 if (ret < 0)
959 return ret;
Vivien Didelotfad09c72016-06-21 12:28:20 -0400960 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -0400961 /* ATU DBNum[7:4] are located in ATU Control 15:12 */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400962 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL);
Vivien Didelot11ea8092016-03-31 16:53:44 -0400963 if (ret < 0)
964 return ret;
965
Vivien Didelotfad09c72016-06-21 12:28:20 -0400966 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL,
Vivien Didelot11ea8092016-03-31 16:53:44 -0400967 (ret & 0xfff) |
968 ((fid << 8) & 0xf000));
969 if (ret < 0)
970 return ret;
971
972 /* ATU DBNum[3:0] are located in ATU Operation 3:0 */
973 cmd |= fid & 0xf;
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400974 }
975
Vivien Didelotfad09c72016-06-21 12:28:20 -0400976 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_OP, cmd);
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700977 if (ret < 0)
978 return ret;
979
Vivien Didelotfad09c72016-06-21 12:28:20 -0400980 return _mv88e6xxx_atu_wait(chip);
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700981}
982
Vivien Didelotfad09c72016-06-21 12:28:20 -0400983static int _mv88e6xxx_atu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot37705b72015-09-04 14:34:11 -0400984 struct mv88e6xxx_atu_entry *entry)
985{
986 u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK;
987
988 if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) {
989 unsigned int mask, shift;
990
991 if (entry->trunk) {
992 data |= GLOBAL_ATU_DATA_TRUNK;
993 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
994 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
995 } else {
996 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
997 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
998 }
999
1000 data |= (entry->portv_trunkid << shift) & mask;
1001 }
1002
Vivien Didelotfad09c72016-06-21 12:28:20 -04001003 return _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_DATA, data);
Vivien Didelot37705b72015-09-04 14:34:11 -04001004}
1005
Vivien Didelotfad09c72016-06-21 12:28:20 -04001006static int _mv88e6xxx_atu_flush_move(struct mv88e6xxx_chip *chip,
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001007 struct mv88e6xxx_atu_entry *entry,
1008 bool static_too)
1009{
1010 int op;
1011 int err;
1012
Vivien Didelotfad09c72016-06-21 12:28:20 -04001013 err = _mv88e6xxx_atu_wait(chip);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001014 if (err)
1015 return err;
1016
Vivien Didelotfad09c72016-06-21 12:28:20 -04001017 err = _mv88e6xxx_atu_data_write(chip, entry);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001018 if (err)
1019 return err;
1020
1021 if (entry->fid) {
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001022 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB :
1023 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB;
1024 } else {
1025 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL :
1026 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC;
1027 }
1028
Vivien Didelotfad09c72016-06-21 12:28:20 -04001029 return _mv88e6xxx_atu_cmd(chip, entry->fid, op);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001030}
1031
Vivien Didelotfad09c72016-06-21 12:28:20 -04001032static int _mv88e6xxx_atu_flush(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001033 u16 fid, bool static_too)
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001034{
1035 struct mv88e6xxx_atu_entry entry = {
1036 .fid = fid,
1037 .state = 0, /* EntryState bits must be 0 */
1038 };
1039
Vivien Didelotfad09c72016-06-21 12:28:20 -04001040 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001041}
1042
Vivien Didelotfad09c72016-06-21 12:28:20 -04001043static int _mv88e6xxx_atu_move(struct mv88e6xxx_chip *chip, u16 fid,
Andrew Lunn158bc062016-04-28 21:24:06 -04001044 int from_port, int to_port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001045{
1046 struct mv88e6xxx_atu_entry entry = {
1047 .trunk = false,
1048 .fid = fid,
1049 };
1050
1051 /* EntryState bits must be 0xF */
1052 entry.state = GLOBAL_ATU_DATA_STATE_MASK;
1053
1054 /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */
1055 entry.portv_trunkid = (to_port & 0x0f) << 4;
1056 entry.portv_trunkid |= from_port & 0x0f;
1057
Vivien Didelotfad09c72016-06-21 12:28:20 -04001058 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001059}
1060
Vivien Didelotfad09c72016-06-21 12:28:20 -04001061static int _mv88e6xxx_atu_remove(struct mv88e6xxx_chip *chip, u16 fid,
Andrew Lunn158bc062016-04-28 21:24:06 -04001062 int port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001063{
1064 /* Destination port 0xF means remove the entries */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001065 return _mv88e6xxx_atu_move(chip, fid, port, 0x0f, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001066}
1067
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001068static const char * const mv88e6xxx_port_state_names[] = {
1069 [PORT_CONTROL_STATE_DISABLED] = "Disabled",
1070 [PORT_CONTROL_STATE_BLOCKING] = "Blocking/Listening",
1071 [PORT_CONTROL_STATE_LEARNING] = "Learning",
1072 [PORT_CONTROL_STATE_FORWARDING] = "Forwarding",
1073};
1074
Vivien Didelotfad09c72016-06-21 12:28:20 -04001075static int _mv88e6xxx_port_state(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001076 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001077{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001078 struct dsa_switch *ds = chip->ds;
Geert Uytterhoevenc3ffe6d2015-04-16 20:49:14 +02001079 int reg, ret = 0;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001080 u8 oldstate;
1081
Vivien Didelotfad09c72016-06-21 12:28:20 -04001082 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL);
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001083 if (reg < 0)
1084 return reg;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001085
Andrew Lunncca8b132015-04-02 04:06:39 +02001086 oldstate = reg & PORT_CONTROL_STATE_MASK;
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001087
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001088 if (oldstate != state) {
1089 /* Flush forwarding database if we're moving a port
1090 * from Learning or Forwarding state to Disabled or
1091 * Blocking or Listening state.
1092 */
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001093 if ((oldstate == PORT_CONTROL_STATE_LEARNING ||
Vivien Didelot57d32312016-06-20 13:13:58 -04001094 oldstate == PORT_CONTROL_STATE_FORWARDING) &&
1095 (state == PORT_CONTROL_STATE_DISABLED ||
1096 state == PORT_CONTROL_STATE_BLOCKING)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001097 ret = _mv88e6xxx_atu_remove(chip, 0, port, false);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001098 if (ret)
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001099 return ret;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001100 }
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001101
Andrew Lunncca8b132015-04-02 04:06:39 +02001102 reg = (reg & ~PORT_CONTROL_STATE_MASK) | state;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001103 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL,
Andrew Lunncca8b132015-04-02 04:06:39 +02001104 reg);
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001105 if (ret)
1106 return ret;
1107
Andrew Lunnc8b09802016-06-04 21:16:57 +02001108 netdev_dbg(ds->ports[port].netdev, "PortState %s (was %s)\n",
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001109 mv88e6xxx_port_state_names[state],
1110 mv88e6xxx_port_state_names[oldstate]);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001111 }
1112
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001113 return ret;
1114}
1115
Vivien Didelotfad09c72016-06-21 12:28:20 -04001116static int _mv88e6xxx_port_based_vlan_map(struct mv88e6xxx_chip *chip, int port)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001117{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001118 struct net_device *bridge = chip->ports[port].bridge_dev;
1119 const u16 mask = (1 << chip->info->num_ports) - 1;
1120 struct dsa_switch *ds = chip->ds;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001121 u16 output_ports = 0;
Vivien Didelotede80982015-10-11 18:08:35 -04001122 int reg;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001123 int i;
1124
1125 /* allow CPU port or DSA link(s) to send frames to every port */
1126 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
1127 output_ports = mask;
1128 } else {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001129 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001130 /* allow sending frames to every group member */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001131 if (bridge && chip->ports[i].bridge_dev == bridge)
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001132 output_ports |= BIT(i);
1133
1134 /* allow sending frames to CPU port and DSA link(s) */
1135 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
1136 output_ports |= BIT(i);
1137 }
1138 }
1139
1140 /* prevent frames from going back out of the port they came in on */
1141 output_ports &= ~BIT(port);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001142
Vivien Didelotfad09c72016-06-21 12:28:20 -04001143 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_BASE_VLAN);
Vivien Didelotede80982015-10-11 18:08:35 -04001144 if (reg < 0)
1145 return reg;
1146
1147 reg &= ~mask;
1148 reg |= output_ports & mask;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001149
Vivien Didelotfad09c72016-06-21 12:28:20 -04001150 return _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_BASE_VLAN, reg);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001151}
1152
Vivien Didelotf81ec902016-05-09 13:22:58 -04001153static void mv88e6xxx_port_stp_state_set(struct dsa_switch *ds, int port,
1154 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001155{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001156 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001157 int stp_state;
Vivien Didelot553eb542016-05-13 20:38:23 -04001158 int err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001159
1160 switch (state) {
1161 case BR_STATE_DISABLED:
Andrew Lunncca8b132015-04-02 04:06:39 +02001162 stp_state = PORT_CONTROL_STATE_DISABLED;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001163 break;
1164 case BR_STATE_BLOCKING:
1165 case BR_STATE_LISTENING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001166 stp_state = PORT_CONTROL_STATE_BLOCKING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001167 break;
1168 case BR_STATE_LEARNING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001169 stp_state = PORT_CONTROL_STATE_LEARNING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001170 break;
1171 case BR_STATE_FORWARDING:
1172 default:
Andrew Lunncca8b132015-04-02 04:06:39 +02001173 stp_state = PORT_CONTROL_STATE_FORWARDING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001174 break;
1175 }
1176
Vivien Didelotfad09c72016-06-21 12:28:20 -04001177 mutex_lock(&chip->reg_lock);
1178 err = _mv88e6xxx_port_state(chip, port, stp_state);
1179 mutex_unlock(&chip->reg_lock);
Vivien Didelot553eb542016-05-13 20:38:23 -04001180
1181 if (err)
Andrew Lunnc8b09802016-06-04 21:16:57 +02001182 netdev_err(ds->ports[port].netdev,
1183 "failed to update state to %s\n",
Vivien Didelot553eb542016-05-13 20:38:23 -04001184 mv88e6xxx_port_state_names[stp_state]);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001185}
1186
Vivien Didelotfad09c72016-06-21 12:28:20 -04001187static int _mv88e6xxx_port_pvid(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001188 u16 *new, u16 *old)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001189{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001190 struct dsa_switch *ds = chip->ds;
Vivien Didelot5da96032016-03-07 18:24:39 -05001191 u16 pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001192 int ret;
1193
Vivien Didelotfad09c72016-06-21 12:28:20 -04001194 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_DEFAULT_VLAN);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001195 if (ret < 0)
1196 return ret;
1197
Vivien Didelot5da96032016-03-07 18:24:39 -05001198 pvid = ret & PORT_DEFAULT_VLAN_MASK;
1199
1200 if (new) {
1201 ret &= ~PORT_DEFAULT_VLAN_MASK;
1202 ret |= *new & PORT_DEFAULT_VLAN_MASK;
1203
Vivien Didelotfad09c72016-06-21 12:28:20 -04001204 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Vivien Didelot5da96032016-03-07 18:24:39 -05001205 PORT_DEFAULT_VLAN, ret);
1206 if (ret < 0)
1207 return ret;
1208
Andrew Lunnc8b09802016-06-04 21:16:57 +02001209 netdev_dbg(ds->ports[port].netdev,
1210 "DefaultVID %d (was %d)\n", *new, pvid);
Vivien Didelot5da96032016-03-07 18:24:39 -05001211 }
1212
1213 if (old)
1214 *old = pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001215
1216 return 0;
1217}
1218
Vivien Didelotfad09c72016-06-21 12:28:20 -04001219static int _mv88e6xxx_port_pvid_get(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001220 int port, u16 *pvid)
Vivien Didelot5da96032016-03-07 18:24:39 -05001221{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001222 return _mv88e6xxx_port_pvid(chip, port, NULL, pvid);
Vivien Didelot5da96032016-03-07 18:24:39 -05001223}
1224
Vivien Didelotfad09c72016-06-21 12:28:20 -04001225static int _mv88e6xxx_port_pvid_set(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001226 int port, u16 pvid)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001227{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001228 return _mv88e6xxx_port_pvid(chip, port, &pvid, NULL);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001229}
1230
Vivien Didelotfad09c72016-06-21 12:28:20 -04001231static int _mv88e6xxx_vtu_wait(struct mv88e6xxx_chip *chip)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001232{
Vivien Didelot2d79af62016-08-15 17:18:57 -04001233 return mv88e6xxx_wait(chip, REG_GLOBAL, GLOBAL_VTU_OP,
1234 GLOBAL_VTU_OP_BUSY);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001235}
1236
Vivien Didelotfad09c72016-06-21 12:28:20 -04001237static int _mv88e6xxx_vtu_cmd(struct mv88e6xxx_chip *chip, u16 op)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001238{
1239 int ret;
1240
Vivien Didelotfad09c72016-06-21 12:28:20 -04001241 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_OP, op);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001242 if (ret < 0)
1243 return ret;
1244
Vivien Didelotfad09c72016-06-21 12:28:20 -04001245 return _mv88e6xxx_vtu_wait(chip);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001246}
1247
Vivien Didelotfad09c72016-06-21 12:28:20 -04001248static int _mv88e6xxx_vtu_stu_flush(struct mv88e6xxx_chip *chip)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001249{
1250 int ret;
1251
Vivien Didelotfad09c72016-06-21 12:28:20 -04001252 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001253 if (ret < 0)
1254 return ret;
1255
Vivien Didelotfad09c72016-06-21 12:28:20 -04001256 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_FLUSH_ALL);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001257}
1258
Vivien Didelotfad09c72016-06-21 12:28:20 -04001259static int _mv88e6xxx_vtu_stu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001260 struct mv88e6xxx_vtu_stu_entry *entry,
1261 unsigned int nibble_offset)
1262{
Vivien Didelotb8fee952015-08-13 12:52:19 -04001263 u16 regs[3];
1264 int i;
1265 int ret;
1266
1267 for (i = 0; i < 3; ++i) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001268 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001269 GLOBAL_VTU_DATA_0_3 + i);
1270 if (ret < 0)
1271 return ret;
1272
1273 regs[i] = ret;
1274 }
1275
Vivien Didelotfad09c72016-06-21 12:28:20 -04001276 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelotb8fee952015-08-13 12:52:19 -04001277 unsigned int shift = (i % 4) * 4 + nibble_offset;
1278 u16 reg = regs[i / 4];
1279
1280 entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
1281 }
1282
1283 return 0;
1284}
1285
Vivien Didelotfad09c72016-06-21 12:28:20 -04001286static int mv88e6xxx_vtu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001287 struct mv88e6xxx_vtu_stu_entry *entry)
1288{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001289 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 0);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001290}
1291
Vivien Didelotfad09c72016-06-21 12:28:20 -04001292static int mv88e6xxx_stu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001293 struct mv88e6xxx_vtu_stu_entry *entry)
1294{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001295 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 2);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001296}
1297
Vivien Didelotfad09c72016-06-21 12:28:20 -04001298static int _mv88e6xxx_vtu_stu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001299 struct mv88e6xxx_vtu_stu_entry *entry,
1300 unsigned int nibble_offset)
1301{
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001302 u16 regs[3] = { 0 };
1303 int i;
1304 int ret;
1305
Vivien Didelotfad09c72016-06-21 12:28:20 -04001306 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001307 unsigned int shift = (i % 4) * 4 + nibble_offset;
1308 u8 data = entry->data[i];
1309
1310 regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
1311 }
1312
1313 for (i = 0; i < 3; ++i) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001314 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001315 GLOBAL_VTU_DATA_0_3 + i, regs[i]);
1316 if (ret < 0)
1317 return ret;
1318 }
1319
1320 return 0;
1321}
1322
Vivien Didelotfad09c72016-06-21 12:28:20 -04001323static int mv88e6xxx_vtu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001324 struct mv88e6xxx_vtu_stu_entry *entry)
1325{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001326 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 0);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001327}
1328
Vivien Didelotfad09c72016-06-21 12:28:20 -04001329static int mv88e6xxx_stu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001330 struct mv88e6xxx_vtu_stu_entry *entry)
1331{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001332 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 2);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001333}
1334
Vivien Didelotfad09c72016-06-21 12:28:20 -04001335static int _mv88e6xxx_vtu_vid_write(struct mv88e6xxx_chip *chip, u16 vid)
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001336{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001337 return _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID,
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001338 vid & GLOBAL_VTU_VID_MASK);
1339}
1340
Vivien Didelotfad09c72016-06-21 12:28:20 -04001341static int _mv88e6xxx_vtu_getnext(struct mv88e6xxx_chip *chip,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001342 struct mv88e6xxx_vtu_stu_entry *entry)
1343{
1344 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1345 int ret;
1346
Vivien Didelotfad09c72016-06-21 12:28:20 -04001347 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001348 if (ret < 0)
1349 return ret;
1350
Vivien Didelotfad09c72016-06-21 12:28:20 -04001351 ret = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_VTU_GET_NEXT);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001352 if (ret < 0)
1353 return ret;
1354
Vivien Didelotfad09c72016-06-21 12:28:20 -04001355 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_VID);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001356 if (ret < 0)
1357 return ret;
1358
1359 next.vid = ret & GLOBAL_VTU_VID_MASK;
1360 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1361
1362 if (next.valid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001363 ret = mv88e6xxx_vtu_data_read(chip, &next);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001364 if (ret < 0)
1365 return ret;
1366
Vivien Didelotfad09c72016-06-21 12:28:20 -04001367 if (mv88e6xxx_has_fid_reg(chip)) {
1368 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001369 GLOBAL_VTU_FID);
1370 if (ret < 0)
1371 return ret;
1372
1373 next.fid = ret & GLOBAL_VTU_FID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001374 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001375 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1376 * VTU DBNum[3:0] are located in VTU Operation 3:0
1377 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001378 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
Vivien Didelot11ea8092016-03-31 16:53:44 -04001379 GLOBAL_VTU_OP);
1380 if (ret < 0)
1381 return ret;
1382
1383 next.fid = (ret & 0xf00) >> 4;
1384 next.fid |= ret & 0xf;
Vivien Didelot2e7bd5e2016-03-31 16:53:41 -04001385 }
Vivien Didelotb8fee952015-08-13 12:52:19 -04001386
Vivien Didelotfad09c72016-06-21 12:28:20 -04001387 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
1388 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001389 GLOBAL_VTU_SID);
1390 if (ret < 0)
1391 return ret;
1392
1393 next.sid = ret & GLOBAL_VTU_SID_MASK;
1394 }
1395 }
1396
1397 *entry = next;
1398 return 0;
1399}
1400
Vivien Didelotf81ec902016-05-09 13:22:58 -04001401static int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
1402 struct switchdev_obj_port_vlan *vlan,
1403 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001404{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001405 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001406 struct mv88e6xxx_vtu_stu_entry next;
1407 u16 pvid;
1408 int err;
1409
Vivien Didelotfad09c72016-06-21 12:28:20 -04001410 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001411 return -EOPNOTSUPP;
1412
Vivien Didelotfad09c72016-06-21 12:28:20 -04001413 mutex_lock(&chip->reg_lock);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001414
Vivien Didelotfad09c72016-06-21 12:28:20 -04001415 err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001416 if (err)
1417 goto unlock;
1418
Vivien Didelotfad09c72016-06-21 12:28:20 -04001419 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001420 if (err)
1421 goto unlock;
1422
1423 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001424 err = _mv88e6xxx_vtu_getnext(chip, &next);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001425 if (err)
1426 break;
1427
1428 if (!next.valid)
1429 break;
1430
1431 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1432 continue;
1433
1434 /* reinit and dump this VLAN obj */
Vivien Didelot57d32312016-06-20 13:13:58 -04001435 vlan->vid_begin = next.vid;
1436 vlan->vid_end = next.vid;
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001437 vlan->flags = 0;
1438
1439 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1440 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1441
1442 if (next.vid == pvid)
1443 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1444
1445 err = cb(&vlan->obj);
1446 if (err)
1447 break;
1448 } while (next.vid < GLOBAL_VTU_VID_MASK);
1449
1450unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001451 mutex_unlock(&chip->reg_lock);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001452
1453 return err;
1454}
1455
Vivien Didelotfad09c72016-06-21 12:28:20 -04001456static int _mv88e6xxx_vtu_loadpurge(struct mv88e6xxx_chip *chip,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001457 struct mv88e6xxx_vtu_stu_entry *entry)
1458{
Vivien Didelot11ea8092016-03-31 16:53:44 -04001459 u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001460 u16 reg = 0;
1461 int ret;
1462
Vivien Didelotfad09c72016-06-21 12:28:20 -04001463 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001464 if (ret < 0)
1465 return ret;
1466
1467 if (!entry->valid)
1468 goto loadpurge;
1469
1470 /* Write port member tags */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001471 ret = mv88e6xxx_vtu_data_write(chip, entry);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001472 if (ret < 0)
1473 return ret;
1474
Vivien Didelotfad09c72016-06-21 12:28:20 -04001475 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001476 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001477 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID,
1478 reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001479 if (ret < 0)
1480 return ret;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001481 }
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001482
Vivien Didelotfad09c72016-06-21 12:28:20 -04001483 if (mv88e6xxx_has_fid_reg(chip)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001484 reg = entry->fid & GLOBAL_VTU_FID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001485 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_FID,
1486 reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001487 if (ret < 0)
1488 return ret;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001489 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001490 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1491 * VTU DBNum[3:0] are located in VTU Operation 3:0
1492 */
1493 op |= (entry->fid & 0xf0) << 8;
1494 op |= entry->fid & 0xf;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001495 }
1496
1497 reg = GLOBAL_VTU_VID_VALID;
1498loadpurge:
1499 reg |= entry->vid & GLOBAL_VTU_VID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001500 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID, reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001501 if (ret < 0)
1502 return ret;
1503
Vivien Didelotfad09c72016-06-21 12:28:20 -04001504 return _mv88e6xxx_vtu_cmd(chip, op);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001505}
1506
Vivien Didelotfad09c72016-06-21 12:28:20 -04001507static int _mv88e6xxx_stu_getnext(struct mv88e6xxx_chip *chip, u8 sid,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001508 struct mv88e6xxx_vtu_stu_entry *entry)
1509{
1510 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1511 int ret;
1512
Vivien Didelotfad09c72016-06-21 12:28:20 -04001513 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001514 if (ret < 0)
1515 return ret;
1516
Vivien Didelotfad09c72016-06-21 12:28:20 -04001517 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001518 sid & GLOBAL_VTU_SID_MASK);
1519 if (ret < 0)
1520 return ret;
1521
Vivien Didelotfad09c72016-06-21 12:28:20 -04001522 ret = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_GET_NEXT);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001523 if (ret < 0)
1524 return ret;
1525
Vivien Didelotfad09c72016-06-21 12:28:20 -04001526 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_SID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001527 if (ret < 0)
1528 return ret;
1529
1530 next.sid = ret & GLOBAL_VTU_SID_MASK;
1531
Vivien Didelotfad09c72016-06-21 12:28:20 -04001532 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_VID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001533 if (ret < 0)
1534 return ret;
1535
1536 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1537
1538 if (next.valid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001539 ret = mv88e6xxx_stu_data_read(chip, &next);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001540 if (ret < 0)
1541 return ret;
1542 }
1543
1544 *entry = next;
1545 return 0;
1546}
1547
Vivien Didelotfad09c72016-06-21 12:28:20 -04001548static int _mv88e6xxx_stu_loadpurge(struct mv88e6xxx_chip *chip,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001549 struct mv88e6xxx_vtu_stu_entry *entry)
1550{
1551 u16 reg = 0;
1552 int ret;
1553
Vivien Didelotfad09c72016-06-21 12:28:20 -04001554 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001555 if (ret < 0)
1556 return ret;
1557
1558 if (!entry->valid)
1559 goto loadpurge;
1560
1561 /* Write port states */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001562 ret = mv88e6xxx_stu_data_write(chip, entry);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001563 if (ret < 0)
1564 return ret;
1565
1566 reg = GLOBAL_VTU_VID_VALID;
1567loadpurge:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001568 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID, reg);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001569 if (ret < 0)
1570 return ret;
1571
1572 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001573 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID, reg);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001574 if (ret < 0)
1575 return ret;
1576
Vivien Didelotfad09c72016-06-21 12:28:20 -04001577 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_LOAD_PURGE);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001578}
1579
Vivien Didelotfad09c72016-06-21 12:28:20 -04001580static int _mv88e6xxx_port_fid(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001581 u16 *new, u16 *old)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001582{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001583 struct dsa_switch *ds = chip->ds;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001584 u16 upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001585 u16 fid;
1586 int ret;
1587
Vivien Didelotfad09c72016-06-21 12:28:20 -04001588 if (mv88e6xxx_num_databases(chip) == 4096)
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001589 upper_mask = 0xff;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001590 else if (mv88e6xxx_num_databases(chip) == 256)
Vivien Didelot11ea8092016-03-31 16:53:44 -04001591 upper_mask = 0xf;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001592 else
1593 return -EOPNOTSUPP;
1594
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001595 /* Port's default FID bits 3:0 are located in reg 0x06, offset 12 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001596 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_BASE_VLAN);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001597 if (ret < 0)
1598 return ret;
1599
1600 fid = (ret & PORT_BASE_VLAN_FID_3_0_MASK) >> 12;
1601
1602 if (new) {
1603 ret &= ~PORT_BASE_VLAN_FID_3_0_MASK;
1604 ret |= (*new << 12) & PORT_BASE_VLAN_FID_3_0_MASK;
1605
Vivien Didelotfad09c72016-06-21 12:28:20 -04001606 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_BASE_VLAN,
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001607 ret);
1608 if (ret < 0)
1609 return ret;
1610 }
1611
1612 /* Port's default FID bits 11:4 are located in reg 0x05, offset 0 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001613 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL_1);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001614 if (ret < 0)
1615 return ret;
1616
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001617 fid |= (ret & upper_mask) << 4;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001618
1619 if (new) {
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001620 ret &= ~upper_mask;
1621 ret |= (*new >> 4) & upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001622
Vivien Didelotfad09c72016-06-21 12:28:20 -04001623 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_1,
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001624 ret);
1625 if (ret < 0)
1626 return ret;
1627
Andrew Lunnc8b09802016-06-04 21:16:57 +02001628 netdev_dbg(ds->ports[port].netdev,
1629 "FID %d (was %d)\n", *new, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001630 }
1631
1632 if (old)
1633 *old = fid;
1634
1635 return 0;
1636}
1637
Vivien Didelotfad09c72016-06-21 12:28:20 -04001638static int _mv88e6xxx_port_fid_get(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001639 int port, u16 *fid)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001640{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001641 return _mv88e6xxx_port_fid(chip, port, NULL, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001642}
1643
Vivien Didelotfad09c72016-06-21 12:28:20 -04001644static int _mv88e6xxx_port_fid_set(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001645 int port, u16 fid)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001646{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001647 return _mv88e6xxx_port_fid(chip, port, &fid, NULL);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001648}
1649
Vivien Didelotfad09c72016-06-21 12:28:20 -04001650static int _mv88e6xxx_fid_new(struct mv88e6xxx_chip *chip, u16 *fid)
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001651{
1652 DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
1653 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001654 int i, err;
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001655
1656 bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);
1657
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001658 /* Set every FID bit used by the (un)bridged ports */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001659 for (i = 0; i < chip->info->num_ports; ++i) {
1660 err = _mv88e6xxx_port_fid_get(chip, i, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001661 if (err)
1662 return err;
1663
1664 set_bit(*fid, fid_bitmap);
1665 }
1666
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001667 /* Set every FID bit used by the VLAN entries */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001668 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001669 if (err)
1670 return err;
1671
1672 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001673 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001674 if (err)
1675 return err;
1676
1677 if (!vlan.valid)
1678 break;
1679
1680 set_bit(vlan.fid, fid_bitmap);
1681 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
1682
1683 /* The reset value 0x000 is used to indicate that multiple address
1684 * databases are not needed. Return the next positive available.
1685 */
1686 *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1);
Vivien Didelotfad09c72016-06-21 12:28:20 -04001687 if (unlikely(*fid >= mv88e6xxx_num_databases(chip)))
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001688 return -ENOSPC;
1689
1690 /* Clear the database */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001691 return _mv88e6xxx_atu_flush(chip, *fid, true);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001692}
1693
Vivien Didelotfad09c72016-06-21 12:28:20 -04001694static int _mv88e6xxx_vtu_new(struct mv88e6xxx_chip *chip, u16 vid,
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001695 struct mv88e6xxx_vtu_stu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001696{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001697 struct dsa_switch *ds = chip->ds;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001698 struct mv88e6xxx_vtu_stu_entry vlan = {
1699 .valid = true,
1700 .vid = vid,
1701 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001702 int i, err;
1703
Vivien Didelotfad09c72016-06-21 12:28:20 -04001704 err = _mv88e6xxx_fid_new(chip, &vlan.fid);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001705 if (err)
1706 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001707
Vivien Didelot3d131f02015-11-03 10:52:52 -05001708 /* exclude all ports except the CPU and DSA ports */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001709 for (i = 0; i < chip->info->num_ports; ++i)
Vivien Didelot3d131f02015-11-03 10:52:52 -05001710 vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)
1711 ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED
1712 : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001713
Vivien Didelotfad09c72016-06-21 12:28:20 -04001714 if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
1715 mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip)) {
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001716 struct mv88e6xxx_vtu_stu_entry vstp;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001717
1718 /* Adding a VTU entry requires a valid STU entry. As VSTP is not
1719 * implemented, only one STU entry is needed to cover all VTU
1720 * entries. Thus, validate the SID 0.
1721 */
1722 vlan.sid = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001723 err = _mv88e6xxx_stu_getnext(chip, GLOBAL_VTU_SID_MASK, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001724 if (err)
1725 return err;
1726
1727 if (vstp.sid != vlan.sid || !vstp.valid) {
1728 memset(&vstp, 0, sizeof(vstp));
1729 vstp.valid = true;
1730 vstp.sid = vlan.sid;
1731
Vivien Didelotfad09c72016-06-21 12:28:20 -04001732 err = _mv88e6xxx_stu_loadpurge(chip, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001733 if (err)
1734 return err;
1735 }
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001736 }
1737
1738 *entry = vlan;
1739 return 0;
1740}
1741
Vivien Didelotfad09c72016-06-21 12:28:20 -04001742static int _mv88e6xxx_vtu_get(struct mv88e6xxx_chip *chip, u16 vid,
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001743 struct mv88e6xxx_vtu_stu_entry *entry, bool creat)
1744{
1745 int err;
1746
1747 if (!vid)
1748 return -EINVAL;
1749
Vivien Didelotfad09c72016-06-21 12:28:20 -04001750 err = _mv88e6xxx_vtu_vid_write(chip, vid - 1);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001751 if (err)
1752 return err;
1753
Vivien Didelotfad09c72016-06-21 12:28:20 -04001754 err = _mv88e6xxx_vtu_getnext(chip, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001755 if (err)
1756 return err;
1757
1758 if (entry->vid != vid || !entry->valid) {
1759 if (!creat)
1760 return -EOPNOTSUPP;
1761 /* -ENOENT would've been more appropriate, but switchdev expects
1762 * -EOPNOTSUPP to inform bridge about an eventual software VLAN.
1763 */
1764
Vivien Didelotfad09c72016-06-21 12:28:20 -04001765 err = _mv88e6xxx_vtu_new(chip, vid, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001766 }
1767
1768 return err;
1769}
1770
Vivien Didelotda9c3592016-02-12 12:09:40 -05001771static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
1772 u16 vid_begin, u16 vid_end)
1773{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001774 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001775 struct mv88e6xxx_vtu_stu_entry vlan;
1776 int i, err;
1777
1778 if (!vid_begin)
1779 return -EOPNOTSUPP;
1780
Vivien Didelotfad09c72016-06-21 12:28:20 -04001781 mutex_lock(&chip->reg_lock);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001782
Vivien Didelotfad09c72016-06-21 12:28:20 -04001783 err = _mv88e6xxx_vtu_vid_write(chip, vid_begin - 1);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001784 if (err)
1785 goto unlock;
1786
1787 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001788 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001789 if (err)
1790 goto unlock;
1791
1792 if (!vlan.valid)
1793 break;
1794
1795 if (vlan.vid > vid_end)
1796 break;
1797
Vivien Didelotfad09c72016-06-21 12:28:20 -04001798 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelotda9c3592016-02-12 12:09:40 -05001799 if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i))
1800 continue;
1801
1802 if (vlan.data[i] ==
1803 GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1804 continue;
1805
Vivien Didelotfad09c72016-06-21 12:28:20 -04001806 if (chip->ports[i].bridge_dev ==
1807 chip->ports[port].bridge_dev)
Vivien Didelotda9c3592016-02-12 12:09:40 -05001808 break; /* same bridge, check next VLAN */
1809
Andrew Lunnc8b09802016-06-04 21:16:57 +02001810 netdev_warn(ds->ports[port].netdev,
Vivien Didelotda9c3592016-02-12 12:09:40 -05001811 "hardware VLAN %d already used by %s\n",
1812 vlan.vid,
Vivien Didelotfad09c72016-06-21 12:28:20 -04001813 netdev_name(chip->ports[i].bridge_dev));
Vivien Didelotda9c3592016-02-12 12:09:40 -05001814 err = -EOPNOTSUPP;
1815 goto unlock;
1816 }
1817 } while (vlan.vid < vid_end);
1818
1819unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001820 mutex_unlock(&chip->reg_lock);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001821
1822 return err;
1823}
1824
Vivien Didelot214cdb92016-02-26 13:16:08 -05001825static const char * const mv88e6xxx_port_8021q_mode_names[] = {
1826 [PORT_CONTROL_2_8021Q_DISABLED] = "Disabled",
1827 [PORT_CONTROL_2_8021Q_FALLBACK] = "Fallback",
1828 [PORT_CONTROL_2_8021Q_CHECK] = "Check",
1829 [PORT_CONTROL_2_8021Q_SECURE] = "Secure",
1830};
1831
Vivien Didelotf81ec902016-05-09 13:22:58 -04001832static int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port,
1833 bool vlan_filtering)
Vivien Didelot214cdb92016-02-26 13:16:08 -05001834{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001835 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelot214cdb92016-02-26 13:16:08 -05001836 u16 old, new = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE :
1837 PORT_CONTROL_2_8021Q_DISABLED;
1838 int ret;
1839
Vivien Didelotfad09c72016-06-21 12:28:20 -04001840 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001841 return -EOPNOTSUPP;
1842
Vivien Didelotfad09c72016-06-21 12:28:20 -04001843 mutex_lock(&chip->reg_lock);
Vivien Didelot214cdb92016-02-26 13:16:08 -05001844
Vivien Didelotfad09c72016-06-21 12:28:20 -04001845 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL_2);
Vivien Didelot214cdb92016-02-26 13:16:08 -05001846 if (ret < 0)
1847 goto unlock;
1848
1849 old = ret & PORT_CONTROL_2_8021Q_MASK;
1850
Vivien Didelot5220ef12016-03-07 18:24:52 -05001851 if (new != old) {
1852 ret &= ~PORT_CONTROL_2_8021Q_MASK;
1853 ret |= new & PORT_CONTROL_2_8021Q_MASK;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001854
Vivien Didelotfad09c72016-06-21 12:28:20 -04001855 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_2,
Vivien Didelot5220ef12016-03-07 18:24:52 -05001856 ret);
1857 if (ret < 0)
1858 goto unlock;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001859
Andrew Lunnc8b09802016-06-04 21:16:57 +02001860 netdev_dbg(ds->ports[port].netdev, "802.1Q Mode %s (was %s)\n",
Vivien Didelot5220ef12016-03-07 18:24:52 -05001861 mv88e6xxx_port_8021q_mode_names[new],
1862 mv88e6xxx_port_8021q_mode_names[old]);
1863 }
1864
1865 ret = 0;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001866unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001867 mutex_unlock(&chip->reg_lock);
Vivien Didelot214cdb92016-02-26 13:16:08 -05001868
1869 return ret;
1870}
1871
Vivien Didelot57d32312016-06-20 13:13:58 -04001872static int
1873mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
1874 const struct switchdev_obj_port_vlan *vlan,
1875 struct switchdev_trans *trans)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001876{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001877 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001878 int err;
1879
Vivien Didelotfad09c72016-06-21 12:28:20 -04001880 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001881 return -EOPNOTSUPP;
1882
Vivien Didelotda9c3592016-02-12 12:09:40 -05001883 /* If the requested port doesn't belong to the same bridge as the VLAN
1884 * members, do not support it (yet) and fallback to software VLAN.
1885 */
1886 err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin,
1887 vlan->vid_end);
1888 if (err)
1889 return err;
1890
Vivien Didelot76e398a2015-11-01 12:33:55 -05001891 /* We don't need any dynamic resource from the kernel (yet),
1892 * so skip the prepare phase.
1893 */
1894 return 0;
1895}
1896
Vivien Didelotfad09c72016-06-21 12:28:20 -04001897static int _mv88e6xxx_port_vlan_add(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001898 u16 vid, bool untagged)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001899{
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001900 struct mv88e6xxx_vtu_stu_entry vlan;
1901 int err;
1902
Vivien Didelotfad09c72016-06-21 12:28:20 -04001903 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, true);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001904 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001905 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001906
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001907 vlan.data[port] = untagged ?
1908 GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
1909 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
1910
Vivien Didelotfad09c72016-06-21 12:28:20 -04001911 return _mv88e6xxx_vtu_loadpurge(chip, &vlan);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001912}
1913
Vivien Didelotf81ec902016-05-09 13:22:58 -04001914static void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
1915 const struct switchdev_obj_port_vlan *vlan,
1916 struct switchdev_trans *trans)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001917{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001918 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001919 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1920 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1921 u16 vid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001922
Vivien Didelotfad09c72016-06-21 12:28:20 -04001923 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001924 return;
1925
Vivien Didelotfad09c72016-06-21 12:28:20 -04001926 mutex_lock(&chip->reg_lock);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001927
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001928 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
Vivien Didelotfad09c72016-06-21 12:28:20 -04001929 if (_mv88e6xxx_port_vlan_add(chip, port, vid, untagged))
Andrew Lunnc8b09802016-06-04 21:16:57 +02001930 netdev_err(ds->ports[port].netdev,
1931 "failed to add VLAN %d%c\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001932 vid, untagged ? 'u' : 't');
Vivien Didelot76e398a2015-11-01 12:33:55 -05001933
Vivien Didelotfad09c72016-06-21 12:28:20 -04001934 if (pvid && _mv88e6xxx_port_pvid_set(chip, port, vlan->vid_end))
Andrew Lunnc8b09802016-06-04 21:16:57 +02001935 netdev_err(ds->ports[port].netdev, "failed to set PVID %d\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001936 vlan->vid_end);
1937
Vivien Didelotfad09c72016-06-21 12:28:20 -04001938 mutex_unlock(&chip->reg_lock);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001939}
1940
Vivien Didelotfad09c72016-06-21 12:28:20 -04001941static int _mv88e6xxx_port_vlan_del(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001942 int port, u16 vid)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001943{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001944 struct dsa_switch *ds = chip->ds;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001945 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001946 int i, err;
1947
Vivien Didelotfad09c72016-06-21 12:28:20 -04001948 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001949 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001950 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001951
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001952 /* Tell switchdev if this VLAN is handled in software */
1953 if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
Vivien Didelot3c06f082016-02-05 14:04:39 -05001954 return -EOPNOTSUPP;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001955
1956 vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
1957
1958 /* keep the VLAN unless all ports are excluded */
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001959 vlan.valid = false;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001960 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelot3d131f02015-11-03 10:52:52 -05001961 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001962 continue;
1963
1964 if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001965 vlan.valid = true;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001966 break;
1967 }
1968 }
1969
Vivien Didelotfad09c72016-06-21 12:28:20 -04001970 err = _mv88e6xxx_vtu_loadpurge(chip, &vlan);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001971 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001972 return err;
1973
Vivien Didelotfad09c72016-06-21 12:28:20 -04001974 return _mv88e6xxx_atu_remove(chip, vlan.fid, port, false);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001975}
1976
Vivien Didelotf81ec902016-05-09 13:22:58 -04001977static int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
1978 const struct switchdev_obj_port_vlan *vlan)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001979{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001980 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001981 u16 pvid, vid;
1982 int err = 0;
1983
Vivien Didelotfad09c72016-06-21 12:28:20 -04001984 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001985 return -EOPNOTSUPP;
1986
Vivien Didelotfad09c72016-06-21 12:28:20 -04001987 mutex_lock(&chip->reg_lock);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001988
Vivien Didelotfad09c72016-06-21 12:28:20 -04001989 err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001990 if (err)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001991 goto unlock;
1992
Vivien Didelot76e398a2015-11-01 12:33:55 -05001993 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001994 err = _mv88e6xxx_port_vlan_del(chip, port, vid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001995 if (err)
1996 goto unlock;
1997
1998 if (vid == pvid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001999 err = _mv88e6xxx_port_pvid_set(chip, port, 0);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002000 if (err)
2001 goto unlock;
2002 }
2003 }
2004
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002005unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002006 mutex_unlock(&chip->reg_lock);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002007
2008 return err;
2009}
2010
Vivien Didelotfad09c72016-06-21 12:28:20 -04002011static int _mv88e6xxx_atu_mac_write(struct mv88e6xxx_chip *chip,
Vivien Didelotc5723ac2015-08-10 09:09:48 -04002012 const unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002013{
2014 int i, ret;
2015
2016 for (i = 0; i < 3; i++) {
Andrew Lunncca8b132015-04-02 04:06:39 +02002017 ret = _mv88e6xxx_reg_write(
Vivien Didelotfad09c72016-06-21 12:28:20 -04002018 chip, REG_GLOBAL, GLOBAL_ATU_MAC_01 + i,
Andrew Lunncca8b132015-04-02 04:06:39 +02002019 (addr[i * 2] << 8) | addr[i * 2 + 1]);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002020 if (ret < 0)
2021 return ret;
2022 }
2023
2024 return 0;
2025}
2026
Vivien Didelotfad09c72016-06-21 12:28:20 -04002027static int _mv88e6xxx_atu_mac_read(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04002028 unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002029{
2030 int i, ret;
2031
2032 for (i = 0; i < 3; i++) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002033 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
Andrew Lunncca8b132015-04-02 04:06:39 +02002034 GLOBAL_ATU_MAC_01 + i);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002035 if (ret < 0)
2036 return ret;
2037 addr[i * 2] = ret >> 8;
2038 addr[i * 2 + 1] = ret & 0xff;
2039 }
2040
2041 return 0;
2042}
2043
Vivien Didelotfad09c72016-06-21 12:28:20 -04002044static int _mv88e6xxx_atu_load(struct mv88e6xxx_chip *chip,
Vivien Didelotfd231c82015-08-10 09:09:50 -04002045 struct mv88e6xxx_atu_entry *entry)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002046{
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002047 int ret;
2048
Vivien Didelotfad09c72016-06-21 12:28:20 -04002049 ret = _mv88e6xxx_atu_wait(chip);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002050 if (ret < 0)
2051 return ret;
2052
Vivien Didelotfad09c72016-06-21 12:28:20 -04002053 ret = _mv88e6xxx_atu_mac_write(chip, entry->mac);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002054 if (ret < 0)
2055 return ret;
2056
Vivien Didelotfad09c72016-06-21 12:28:20 -04002057 ret = _mv88e6xxx_atu_data_write(chip, entry);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002058 if (ret < 0)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002059 return ret;
2060
Vivien Didelotfad09c72016-06-21 12:28:20 -04002061 return _mv88e6xxx_atu_cmd(chip, entry->fid, GLOBAL_ATU_OP_LOAD_DB);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002062}
David S. Millercdf09692015-08-11 12:00:37 -07002063
Vivien Didelotfad09c72016-06-21 12:28:20 -04002064static int _mv88e6xxx_port_fdb_load(struct mv88e6xxx_chip *chip, int port,
Vivien Didelotfd231c82015-08-10 09:09:50 -04002065 const unsigned char *addr, u16 vid,
2066 u8 state)
2067{
2068 struct mv88e6xxx_atu_entry entry = { 0 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002069 struct mv88e6xxx_vtu_stu_entry vlan;
2070 int err;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002071
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002072 /* Null VLAN ID corresponds to the port private database */
2073 if (vid == 0)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002074 err = _mv88e6xxx_port_fid_get(chip, port, &vlan.fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002075 else
Vivien Didelotfad09c72016-06-21 12:28:20 -04002076 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002077 if (err)
2078 return err;
2079
2080 entry.fid = vlan.fid;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002081 entry.state = state;
2082 ether_addr_copy(entry.mac, addr);
2083 if (state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2084 entry.trunk = false;
2085 entry.portv_trunkid = BIT(port);
2086 }
2087
Vivien Didelotfad09c72016-06-21 12:28:20 -04002088 return _mv88e6xxx_atu_load(chip, &entry);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002089}
2090
Vivien Didelotf81ec902016-05-09 13:22:58 -04002091static int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
2092 const struct switchdev_obj_port_fdb *fdb,
2093 struct switchdev_trans *trans)
Vivien Didelot146a3202015-10-08 11:35:12 -04002094{
2095 /* We don't need any dynamic resource from the kernel (yet),
2096 * so skip the prepare phase.
2097 */
2098 return 0;
2099}
2100
Vivien Didelotf81ec902016-05-09 13:22:58 -04002101static void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
2102 const struct switchdev_obj_port_fdb *fdb,
2103 struct switchdev_trans *trans)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002104{
Vivien Didelot1f36faf2015-10-08 11:35:13 -04002105 int state = is_multicast_ether_addr(fdb->addr) ?
David S. Millercdf09692015-08-11 12:00:37 -07002106 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2107 GLOBAL_ATU_DATA_STATE_UC_STATIC;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002108 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelot6630e232015-08-06 01:44:07 -04002109
Vivien Didelotfad09c72016-06-21 12:28:20 -04002110 mutex_lock(&chip->reg_lock);
2111 if (_mv88e6xxx_port_fdb_load(chip, port, fdb->addr, fdb->vid, state))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002112 netdev_err(ds->ports[port].netdev,
2113 "failed to load MAC address\n");
Vivien Didelotfad09c72016-06-21 12:28:20 -04002114 mutex_unlock(&chip->reg_lock);
David S. Millercdf09692015-08-11 12:00:37 -07002115}
2116
Vivien Didelotf81ec902016-05-09 13:22:58 -04002117static int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
2118 const struct switchdev_obj_port_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -07002119{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002120 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
David S. Millercdf09692015-08-11 12:00:37 -07002121 int ret;
2122
Vivien Didelotfad09c72016-06-21 12:28:20 -04002123 mutex_lock(&chip->reg_lock);
2124 ret = _mv88e6xxx_port_fdb_load(chip, port, fdb->addr, fdb->vid,
David S. Millercdf09692015-08-11 12:00:37 -07002125 GLOBAL_ATU_DATA_STATE_UNUSED);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002126 mutex_unlock(&chip->reg_lock);
David S. Millercdf09692015-08-11 12:00:37 -07002127
2128 return ret;
2129}
2130
Vivien Didelotfad09c72016-06-21 12:28:20 -04002131static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
Vivien Didelot1d194042015-08-10 09:09:51 -04002132 struct mv88e6xxx_atu_entry *entry)
David S. Millercdf09692015-08-11 12:00:37 -07002133{
Vivien Didelot1d194042015-08-10 09:09:51 -04002134 struct mv88e6xxx_atu_entry next = { 0 };
2135 int ret;
2136
2137 next.fid = fid;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002138
Vivien Didelotfad09c72016-06-21 12:28:20 -04002139 ret = _mv88e6xxx_atu_wait(chip);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002140 if (ret < 0)
2141 return ret;
2142
Vivien Didelotfad09c72016-06-21 12:28:20 -04002143 ret = _mv88e6xxx_atu_cmd(chip, fid, GLOBAL_ATU_OP_GET_NEXT_DB);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002144 if (ret < 0)
2145 return ret;
2146
Vivien Didelotfad09c72016-06-21 12:28:20 -04002147 ret = _mv88e6xxx_atu_mac_read(chip, next.mac);
Vivien Didelot1d194042015-08-10 09:09:51 -04002148 if (ret < 0)
2149 return ret;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002150
Vivien Didelotfad09c72016-06-21 12:28:20 -04002151 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_ATU_DATA);
Vivien Didelot1d194042015-08-10 09:09:51 -04002152 if (ret < 0)
2153 return ret;
2154
2155 next.state = ret & GLOBAL_ATU_DATA_STATE_MASK;
2156 if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2157 unsigned int mask, shift;
2158
2159 if (ret & GLOBAL_ATU_DATA_TRUNK) {
2160 next.trunk = true;
2161 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
2162 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
2163 } else {
2164 next.trunk = false;
2165 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
2166 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
2167 }
2168
2169 next.portv_trunkid = (ret & mask) >> shift;
2170 }
2171
2172 *entry = next;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002173 return 0;
2174}
2175
Vivien Didelotfad09c72016-06-21 12:28:20 -04002176static int _mv88e6xxx_port_fdb_dump_one(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04002177 u16 fid, u16 vid, int port,
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002178 struct switchdev_obj_port_fdb *fdb,
2179 int (*cb)(struct switchdev_obj *obj))
2180{
2181 struct mv88e6xxx_atu_entry addr = {
2182 .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2183 };
2184 int err;
2185
Vivien Didelotfad09c72016-06-21 12:28:20 -04002186 err = _mv88e6xxx_atu_mac_write(chip, addr.mac);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002187 if (err)
2188 return err;
2189
2190 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002191 err = _mv88e6xxx_atu_getnext(chip, fid, &addr);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002192 if (err)
2193 break;
2194
2195 if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2196 break;
2197
2198 if (!addr.trunk && addr.portv_trunkid & BIT(port)) {
2199 bool is_static = addr.state ==
2200 (is_multicast_ether_addr(addr.mac) ?
2201 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2202 GLOBAL_ATU_DATA_STATE_UC_STATIC);
2203
2204 fdb->vid = vid;
2205 ether_addr_copy(fdb->addr, addr.mac);
2206 fdb->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
2207
2208 err = cb(&fdb->obj);
2209 if (err)
2210 break;
2211 }
2212 } while (!is_broadcast_ether_addr(addr.mac));
2213
2214 return err;
2215}
2216
Vivien Didelotf81ec902016-05-09 13:22:58 -04002217static int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
2218 struct switchdev_obj_port_fdb *fdb,
2219 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotf33475b2015-10-22 09:34:41 -04002220{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002221 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002222 struct mv88e6xxx_vtu_stu_entry vlan = {
2223 .vid = GLOBAL_VTU_VID_MASK, /* all ones */
2224 };
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002225 u16 fid;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002226 int err;
2227
Vivien Didelotfad09c72016-06-21 12:28:20 -04002228 mutex_lock(&chip->reg_lock);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002229
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002230 /* Dump port's default Filtering Information Database (VLAN ID 0) */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002231 err = _mv88e6xxx_port_fid_get(chip, port, &fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002232 if (err)
2233 goto unlock;
2234
Vivien Didelotfad09c72016-06-21 12:28:20 -04002235 err = _mv88e6xxx_port_fdb_dump_one(chip, fid, 0, port, fdb, cb);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002236 if (err)
2237 goto unlock;
2238
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002239 /* Dump VLANs' Filtering Information Databases */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002240 err = _mv88e6xxx_vtu_vid_write(chip, vlan.vid);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002241 if (err)
2242 goto unlock;
2243
2244 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002245 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002246 if (err)
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002247 break;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002248
2249 if (!vlan.valid)
2250 break;
2251
Vivien Didelotfad09c72016-06-21 12:28:20 -04002252 err = _mv88e6xxx_port_fdb_dump_one(chip, vlan.fid, vlan.vid,
2253 port, fdb, cb);
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 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
2257
2258unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002259 mutex_unlock(&chip->reg_lock);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002260
2261 return err;
2262}
2263
Vivien Didelotf81ec902016-05-09 13:22:58 -04002264static int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
2265 struct net_device *bridge)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002266{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002267 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Colin Ian King1d9619d2016-04-25 23:11:22 +01002268 int i, err = 0;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002269
Vivien Didelotfad09c72016-06-21 12:28:20 -04002270 mutex_lock(&chip->reg_lock);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002271
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002272 /* Assign the bridge and remap each port's VLANTable */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002273 chip->ports[port].bridge_dev = bridge;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002274
Vivien Didelotfad09c72016-06-21 12:28:20 -04002275 for (i = 0; i < chip->info->num_ports; ++i) {
2276 if (chip->ports[i].bridge_dev == bridge) {
2277 err = _mv88e6xxx_port_based_vlan_map(chip, i);
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002278 if (err)
2279 break;
2280 }
2281 }
2282
Vivien Didelotfad09c72016-06-21 12:28:20 -04002283 mutex_unlock(&chip->reg_lock);
Vivien Didelota6692752016-02-12 12:09:39 -05002284
Vivien Didelot466dfa02016-02-26 13:16:05 -05002285 return err;
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002286}
2287
Vivien Didelotf81ec902016-05-09 13:22:58 -04002288static void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002289{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002290 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2291 struct net_device *bridge = chip->ports[port].bridge_dev;
Vivien Didelot16bfa702016-03-13 16:21:33 -04002292 int i;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002293
Vivien Didelotfad09c72016-06-21 12:28:20 -04002294 mutex_lock(&chip->reg_lock);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002295
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002296 /* Unassign the bridge and remap each port's VLANTable */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002297 chip->ports[port].bridge_dev = NULL;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002298
Vivien Didelotfad09c72016-06-21 12:28:20 -04002299 for (i = 0; i < chip->info->num_ports; ++i)
2300 if (i == port || chip->ports[i].bridge_dev == bridge)
2301 if (_mv88e6xxx_port_based_vlan_map(chip, i))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002302 netdev_warn(ds->ports[i].netdev,
2303 "failed to remap\n");
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002304
Vivien Didelotfad09c72016-06-21 12:28:20 -04002305 mutex_unlock(&chip->reg_lock);
Vivien Didelot66d9cd02016-02-05 14:07:14 -05002306}
2307
Vivien Didelotfad09c72016-06-21 12:28:20 -04002308static int _mv88e6xxx_mdio_page_write(struct mv88e6xxx_chip *chip,
Andrew Lunn03a4a542016-06-04 21:17:05 +02002309 int port, int page, int reg, int val)
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002310{
2311 int ret;
2312
Vivien Didelotfad09c72016-06-21 12:28:20 -04002313 ret = mv88e6xxx_mdio_write_indirect(chip, port, 0x16, page);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002314 if (ret < 0)
2315 goto restore_page_0;
2316
Vivien Didelotfad09c72016-06-21 12:28:20 -04002317 ret = mv88e6xxx_mdio_write_indirect(chip, port, reg, val);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002318restore_page_0:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002319 mv88e6xxx_mdio_write_indirect(chip, port, 0x16, 0x0);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002320
2321 return ret;
2322}
2323
Vivien Didelotfad09c72016-06-21 12:28:20 -04002324static int _mv88e6xxx_mdio_page_read(struct mv88e6xxx_chip *chip,
Andrew Lunn03a4a542016-06-04 21:17:05 +02002325 int port, int page, int reg)
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002326{
2327 int ret;
2328
Vivien Didelotfad09c72016-06-21 12:28:20 -04002329 ret = mv88e6xxx_mdio_write_indirect(chip, port, 0x16, page);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002330 if (ret < 0)
2331 goto restore_page_0;
2332
Vivien Didelotfad09c72016-06-21 12:28:20 -04002333 ret = mv88e6xxx_mdio_read_indirect(chip, port, reg);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002334restore_page_0:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002335 mv88e6xxx_mdio_write_indirect(chip, port, 0x16, 0x0);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002336
2337 return ret;
2338}
2339
Vivien Didelotfad09c72016-06-21 12:28:20 -04002340static int mv88e6xxx_switch_reset(struct mv88e6xxx_chip *chip)
Vivien Didelot552238b2016-05-09 13:22:49 -04002341{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002342 bool ppu_active = mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE);
Vivien Didelot552238b2016-05-09 13:22:49 -04002343 u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002344 struct gpio_desc *gpiod = chip->reset;
Vivien Didelot552238b2016-05-09 13:22:49 -04002345 unsigned long timeout;
2346 int ret;
2347 int i;
2348
2349 /* Set all ports to the disabled state. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002350 for (i = 0; i < chip->info->num_ports; i++) {
2351 ret = _mv88e6xxx_reg_read(chip, REG_PORT(i), PORT_CONTROL);
Vivien Didelot552238b2016-05-09 13:22:49 -04002352 if (ret < 0)
2353 return ret;
2354
Vivien Didelotfad09c72016-06-21 12:28:20 -04002355 ret = _mv88e6xxx_reg_write(chip, REG_PORT(i), PORT_CONTROL,
Vivien Didelot552238b2016-05-09 13:22:49 -04002356 ret & 0xfffc);
2357 if (ret)
2358 return ret;
2359 }
2360
2361 /* Wait for transmit queues to drain. */
2362 usleep_range(2000, 4000);
2363
2364 /* If there is a gpio connected to the reset pin, toggle it */
2365 if (gpiod) {
2366 gpiod_set_value_cansleep(gpiod, 1);
2367 usleep_range(10000, 20000);
2368 gpiod_set_value_cansleep(gpiod, 0);
2369 usleep_range(10000, 20000);
2370 }
2371
2372 /* Reset the switch. Keep the PPU active if requested. The PPU
2373 * needs to be active to support indirect phy register access
2374 * through global registers 0x18 and 0x19.
2375 */
2376 if (ppu_active)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002377 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, 0x04, 0xc000);
Vivien Didelot552238b2016-05-09 13:22:49 -04002378 else
Vivien Didelotfad09c72016-06-21 12:28:20 -04002379 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, 0x04, 0xc400);
Vivien Didelot552238b2016-05-09 13:22:49 -04002380 if (ret)
2381 return ret;
2382
2383 /* Wait up to one second for reset to complete. */
2384 timeout = jiffies + 1 * HZ;
2385 while (time_before(jiffies, timeout)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002386 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, 0x00);
Vivien Didelot552238b2016-05-09 13:22:49 -04002387 if (ret < 0)
2388 return ret;
2389
2390 if ((ret & is_reset) == is_reset)
2391 break;
2392 usleep_range(1000, 2000);
2393 }
2394 if (time_after(jiffies, timeout))
2395 ret = -ETIMEDOUT;
2396 else
2397 ret = 0;
2398
2399 return ret;
2400}
2401
Vivien Didelotfad09c72016-06-21 12:28:20 -04002402static int mv88e6xxx_power_on_serdes(struct mv88e6xxx_chip *chip)
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002403{
2404 int ret;
2405
Vivien Didelotfad09c72016-06-21 12:28:20 -04002406 ret = _mv88e6xxx_mdio_page_read(chip, REG_FIBER_SERDES,
Andrew Lunn03a4a542016-06-04 21:17:05 +02002407 PAGE_FIBER_SERDES, MII_BMCR);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002408 if (ret < 0)
2409 return ret;
2410
2411 if (ret & BMCR_PDOWN) {
2412 ret &= ~BMCR_PDOWN;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002413 ret = _mv88e6xxx_mdio_page_write(chip, REG_FIBER_SERDES,
Andrew Lunn03a4a542016-06-04 21:17:05 +02002414 PAGE_FIBER_SERDES, MII_BMCR,
2415 ret);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002416 }
2417
2418 return ret;
2419}
2420
Vivien Didelot8f6345b2016-07-20 18:18:36 -04002421static int mv88e6xxx_port_read(struct mv88e6xxx_chip *chip, int port,
2422 int reg, u16 *val)
2423{
2424 int addr = chip->info->port_base_addr + port;
2425
2426 if (port >= chip->info->num_ports)
2427 return -EINVAL;
2428
2429 return mv88e6xxx_read(chip, addr, reg, val);
2430}
2431
Vivien Didelotfad09c72016-06-21 12:28:20 -04002432static int mv88e6xxx_setup_port(struct mv88e6xxx_chip *chip, int port)
Guenter Roeckd827e882015-03-26 18:36:29 -07002433{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002434 struct dsa_switch *ds = chip->ds;
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002435 int ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002436 u16 reg;
Guenter Roeckd827e882015-03-26 18:36:29 -07002437
Vivien Didelotfad09c72016-06-21 12:28:20 -04002438 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2439 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2440 mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip) ||
2441 mv88e6xxx_6065_family(chip) || mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002442 /* MAC Forcing register: don't force link, speed,
2443 * duplex or flow control state to any particular
2444 * values on physical ports, but force the CPU port
2445 * and all DSA ports to their maximum bandwidth and
2446 * full duplex.
2447 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002448 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_PCS_CTRL);
Andrew Lunn60045cb2015-08-17 23:52:51 +02002449 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
Russell King53adc9e2015-09-21 21:42:59 +01002450 reg &= ~PORT_PCS_CTRL_UNFORCED;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002451 reg |= PORT_PCS_CTRL_FORCE_LINK |
2452 PORT_PCS_CTRL_LINK_UP |
2453 PORT_PCS_CTRL_DUPLEX_FULL |
2454 PORT_PCS_CTRL_FORCE_DUPLEX;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002455 if (mv88e6xxx_6065_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002456 reg |= PORT_PCS_CTRL_100;
2457 else
2458 reg |= PORT_PCS_CTRL_1000;
2459 } else {
2460 reg |= PORT_PCS_CTRL_UNFORCED;
2461 }
2462
Vivien Didelotfad09c72016-06-21 12:28:20 -04002463 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002464 PORT_PCS_CTRL, reg);
2465 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002466 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002467 }
2468
2469 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
2470 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
2471 * tunneling, determine priority by looking at 802.1p and IP
2472 * priority fields (IP prio has precedence), and set STP state
2473 * to Forwarding.
2474 *
2475 * If this is the CPU link, use DSA or EDSA tagging depending
2476 * on which tagging mode was configured.
2477 *
2478 * If this is a link to another switch, use DSA tagging mode.
2479 *
2480 * If this is the upstream port for this switch, enable
2481 * forwarding of unknown unicasts and multicasts.
2482 */
2483 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002484 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2485 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2486 mv88e6xxx_6095_family(chip) || mv88e6xxx_6065_family(chip) ||
2487 mv88e6xxx_6185_family(chip) || mv88e6xxx_6320_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002488 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
2489 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
2490 PORT_CONTROL_STATE_FORWARDING;
2491 if (dsa_is_cpu_port(ds, port)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002492 if (mv88e6xxx_6095_family(chip) || mv88e6xxx_6185_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002493 reg |= PORT_CONTROL_DSA_TAG;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002494 if (mv88e6xxx_6352_family(chip) ||
2495 mv88e6xxx_6351_family(chip) ||
2496 mv88e6xxx_6165_family(chip) ||
2497 mv88e6xxx_6097_family(chip) ||
2498 mv88e6xxx_6320_family(chip)) {
Andrew Lunn5377b802016-06-04 21:17:02 +02002499 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA |
2500 PORT_CONTROL_FORWARD_UNKNOWN |
Andrew Lunnc047a1f2015-09-29 01:50:56 +02002501 PORT_CONTROL_FORWARD_UNKNOWN_MC;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002502 }
2503
Vivien Didelotfad09c72016-06-21 12:28:20 -04002504 if (mv88e6xxx_6352_family(chip) ||
2505 mv88e6xxx_6351_family(chip) ||
2506 mv88e6xxx_6165_family(chip) ||
2507 mv88e6xxx_6097_family(chip) ||
2508 mv88e6xxx_6095_family(chip) ||
2509 mv88e6xxx_6065_family(chip) ||
2510 mv88e6xxx_6185_family(chip) ||
2511 mv88e6xxx_6320_family(chip)) {
Vivien Didelot57d32312016-06-20 13:13:58 -04002512 reg |= PORT_CONTROL_EGRESS_ADD_TAG;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002513 }
2514 }
Andrew Lunn6083ce72015-08-17 23:52:52 +02002515 if (dsa_is_dsa_port(ds, port)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002516 if (mv88e6xxx_6095_family(chip) ||
2517 mv88e6xxx_6185_family(chip))
Andrew Lunn6083ce72015-08-17 23:52:52 +02002518 reg |= PORT_CONTROL_DSA_TAG;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002519 if (mv88e6xxx_6352_family(chip) ||
2520 mv88e6xxx_6351_family(chip) ||
2521 mv88e6xxx_6165_family(chip) ||
2522 mv88e6xxx_6097_family(chip) ||
2523 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002524 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunn6083ce72015-08-17 23:52:52 +02002525 }
2526
Andrew Lunn54d792f2015-05-06 01:09:47 +02002527 if (port == dsa_upstream_port(ds))
2528 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2529 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2530 }
2531 if (reg) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002532 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002533 PORT_CONTROL, reg);
2534 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002535 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002536 }
2537
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002538 /* If this port is connected to a SerDes, make sure the SerDes is not
2539 * powered down.
2540 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002541 if (mv88e6xxx_6352_family(chip)) {
2542 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_STATUS);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002543 if (ret < 0)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002544 return ret;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002545 ret &= PORT_STATUS_CMODE_MASK;
2546 if ((ret == PORT_STATUS_CMODE_100BASE_X) ||
2547 (ret == PORT_STATUS_CMODE_1000BASE_X) ||
2548 (ret == PORT_STATUS_CMODE_SGMII)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002549 ret = mv88e6xxx_power_on_serdes(chip);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002550 if (ret < 0)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002551 return ret;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002552 }
2553 }
2554
Vivien Didelot8efdda42015-08-13 12:52:23 -04002555 /* Port Control 2: don't force a good FCS, set the maximum frame size to
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002556 * 10240 bytes, disable 802.1q tags checking, don't discard tagged or
Vivien Didelot8efdda42015-08-13 12:52:23 -04002557 * untagged frames on this port, do a destination address lookup on all
2558 * received packets as usual, disable ARP mirroring and don't send a
2559 * copy of all transmitted/received frames on this port to the CPU.
Andrew Lunn54d792f2015-05-06 01:09:47 +02002560 */
2561 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002562 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2563 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2564 mv88e6xxx_6095_family(chip) || mv88e6xxx_6320_family(chip) ||
2565 mv88e6xxx_6185_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002566 reg = PORT_CONTROL_2_MAP_DA;
2567
Vivien Didelotfad09c72016-06-21 12:28:20 -04002568 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2569 mv88e6xxx_6165_family(chip) || mv88e6xxx_6320_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002570 reg |= PORT_CONTROL_2_JUMBO_10240;
2571
Vivien Didelotfad09c72016-06-21 12:28:20 -04002572 if (mv88e6xxx_6095_family(chip) || mv88e6xxx_6185_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002573 /* Set the upstream port this port should use */
2574 reg |= dsa_upstream_port(ds);
2575 /* enable forwarding of unknown multicast addresses to
2576 * the upstream port
2577 */
2578 if (port == dsa_upstream_port(ds))
2579 reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
2580 }
2581
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002582 reg |= PORT_CONTROL_2_8021Q_DISABLED;
Vivien Didelot8efdda42015-08-13 12:52:23 -04002583
Andrew Lunn54d792f2015-05-06 01:09:47 +02002584 if (reg) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002585 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002586 PORT_CONTROL_2, reg);
2587 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002588 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002589 }
2590
2591 /* Port Association Vector: when learning source addresses
2592 * of packets, add the address to the address database using
2593 * a port bitmap that has only the bit for this port set and
2594 * the other bits clear.
2595 */
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002596 reg = 1 << port;
Vivien Didelot996ecb82016-04-14 14:42:08 -04002597 /* Disable learning for CPU port */
2598 if (dsa_is_cpu_port(ds, port))
Vivien Didelot65fa4022016-04-14 14:42:07 -04002599 reg = 0;
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002600
Vivien Didelotfad09c72016-06-21 12:28:20 -04002601 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_ASSOC_VECTOR,
2602 reg);
Andrew Lunn54d792f2015-05-06 01:09:47 +02002603 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002604 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002605
2606 /* Egress rate control 2: disable egress rate control. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002607 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_RATE_CONTROL_2,
Andrew Lunn54d792f2015-05-06 01:09:47 +02002608 0x0000);
2609 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002610 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002611
Vivien Didelotfad09c72016-06-21 12:28:20 -04002612 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2613 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2614 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002615 /* Do not limit the period of time that this port can
2616 * be paused for by the remote end or the period of
2617 * time that this port can pause the remote end.
2618 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002619 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002620 PORT_PAUSE_CTRL, 0x0000);
2621 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002622 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002623
2624 /* Port ATU control: disable limiting the number of
2625 * address database entries that this port is allowed
2626 * to use.
2627 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002628 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002629 PORT_ATU_CONTROL, 0x0000);
2630 /* Priority Override: disable DA, SA and VTU priority
2631 * override.
2632 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002633 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002634 PORT_PRI_OVERRIDE, 0x0000);
2635 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002636 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002637
2638 /* Port Ethertype: use the Ethertype DSA Ethertype
2639 * value.
2640 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002641 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002642 PORT_ETH_TYPE, ETH_P_EDSA);
2643 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002644 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002645 /* Tag Remap: use an identity 802.1p prio -> switch
2646 * prio mapping.
2647 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002648 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002649 PORT_TAG_REGMAP_0123, 0x3210);
2650 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002651 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002652
2653 /* Tag Remap 2: use an identity 802.1p prio -> switch
2654 * prio mapping.
2655 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002656 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002657 PORT_TAG_REGMAP_4567, 0x7654);
2658 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002659 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002660 }
2661
Vivien Didelotfad09c72016-06-21 12:28:20 -04002662 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2663 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2664 mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip) ||
2665 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002666 /* Rate Control: disable ingress rate limiting. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002667 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002668 PORT_RATE_CONTROL, 0x0001);
2669 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002670 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002671 }
2672
Guenter Roeck366f0a02015-03-26 18:36:30 -07002673 /* Port Control 1: disable trunking, disable sending
2674 * learning messages to this port.
Guenter Roeckd827e882015-03-26 18:36:29 -07002675 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002676 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_1,
2677 0x0000);
Guenter Roeckd827e882015-03-26 18:36:29 -07002678 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002679 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07002680
Vivien Didelot207afda2016-04-14 14:42:09 -04002681 /* Port based VLAN map: give each port the same default address
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002682 * database, and allow bidirectional communication between the
2683 * CPU and DSA port(s), and the other ports.
Guenter Roeckd827e882015-03-26 18:36:29 -07002684 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002685 ret = _mv88e6xxx_port_fid_set(chip, port, 0);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002686 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002687 return ret;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002688
Vivien Didelotfad09c72016-06-21 12:28:20 -04002689 ret = _mv88e6xxx_port_based_vlan_map(chip, port);
Guenter Roeckd827e882015-03-26 18:36:29 -07002690 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002691 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07002692
2693 /* Default VLAN ID and priority: don't set a default VLAN
2694 * ID, and set the default packet priority to zero.
2695 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002696 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_DEFAULT_VLAN,
Vivien Didelot47cf1e652015-04-20 17:43:26 -04002697 0x0000);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002698 if (ret)
2699 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07002700
Andrew Lunndbde9e62015-05-06 01:09:48 +02002701 return 0;
2702}
2703
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002704static int mv88e6xxx_g1_set_switch_mac(struct mv88e6xxx_chip *chip, u8 *addr)
2705{
2706 int err;
2707
2708 err = mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_MAC_01,
2709 (addr[0] << 8) | addr[1]);
2710 if (err)
2711 return err;
2712
2713 err = mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_MAC_23,
2714 (addr[2] << 8) | addr[3]);
2715 if (err)
2716 return err;
2717
2718 return mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_MAC_45,
2719 (addr[4] << 8) | addr[5]);
2720}
2721
Vivien Didelotacddbd22016-07-18 20:45:39 -04002722static int mv88e6xxx_g1_set_age_time(struct mv88e6xxx_chip *chip,
2723 unsigned int msecs)
2724{
2725 const unsigned int coeff = chip->info->age_time_coeff;
2726 const unsigned int min = 0x01 * coeff;
2727 const unsigned int max = 0xff * coeff;
2728 u8 age_time;
2729 u16 val;
2730 int err;
2731
2732 if (msecs < min || msecs > max)
2733 return -ERANGE;
2734
2735 /* Round to nearest multiple of coeff */
2736 age_time = (msecs + coeff / 2) / coeff;
2737
2738 err = mv88e6xxx_read(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL, &val);
2739 if (err)
2740 return err;
2741
2742 /* AgeTime is 11:4 bits */
2743 val &= ~0xff0;
2744 val |= age_time << 4;
2745
2746 return mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL, val);
2747}
2748
Vivien Didelot2cfcd962016-07-18 20:45:40 -04002749static int mv88e6xxx_set_ageing_time(struct dsa_switch *ds,
2750 unsigned int ageing_time)
2751{
2752 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2753 int err;
2754
2755 mutex_lock(&chip->reg_lock);
2756 err = mv88e6xxx_g1_set_age_time(chip, ageing_time);
2757 mutex_unlock(&chip->reg_lock);
2758
2759 return err;
2760}
2761
Vivien Didelot97299342016-07-18 20:45:30 -04002762static int mv88e6xxx_g1_setup(struct mv88e6xxx_chip *chip)
Vivien Didelot08a01262016-05-09 13:22:50 -04002763{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002764 struct dsa_switch *ds = chip->ds;
Vivien Didelotb0745e872016-05-09 13:22:53 -04002765 u32 upstream_port = dsa_upstream_port(ds);
Vivien Didelot119477b2016-05-09 13:22:51 -04002766 u16 reg;
Vivien Didelot08a01262016-05-09 13:22:50 -04002767 int err;
Vivien Didelot08a01262016-05-09 13:22:50 -04002768
Vivien Didelot119477b2016-05-09 13:22:51 -04002769 /* Enable the PHY Polling Unit if present, don't discard any packets,
2770 * and mask all interrupt sources.
2771 */
2772 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002773 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU) ||
2774 mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE))
Vivien Didelot119477b2016-05-09 13:22:51 -04002775 reg |= GLOBAL_CONTROL_PPU_ENABLE;
2776
Vivien Didelotfad09c72016-06-21 12:28:20 -04002777 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL, reg);
Vivien Didelot119477b2016-05-09 13:22:51 -04002778 if (err)
2779 return err;
2780
Vivien Didelotb0745e872016-05-09 13:22:53 -04002781 /* Configure the upstream port, and configure it as the port to which
2782 * ingress and egress and ARP monitor frames are to be sent.
2783 */
2784 reg = upstream_port << GLOBAL_MONITOR_CONTROL_INGRESS_SHIFT |
2785 upstream_port << GLOBAL_MONITOR_CONTROL_EGRESS_SHIFT |
2786 upstream_port << GLOBAL_MONITOR_CONTROL_ARP_SHIFT;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002787 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_MONITOR_CONTROL,
2788 reg);
Vivien Didelotb0745e872016-05-09 13:22:53 -04002789 if (err)
2790 return err;
2791
Vivien Didelot50484ff2016-05-09 13:22:54 -04002792 /* Disable remote management, and set the switch's DSA device number. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002793 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL_2,
Vivien Didelot50484ff2016-05-09 13:22:54 -04002794 GLOBAL_CONTROL_2_MULTIPLE_CASCADE |
2795 (ds->index & 0x1f));
2796 if (err)
2797 return err;
2798
Vivien Didelotacddbd22016-07-18 20:45:39 -04002799 /* Clear all the VTU and STU entries */
2800 err = _mv88e6xxx_vtu_stu_flush(chip);
2801 if (err < 0)
2802 return err;
2803
Vivien Didelot08a01262016-05-09 13:22:50 -04002804 /* Set the default address aging time to 5 minutes, and
2805 * enable address learn messages to be sent to all message
2806 * ports.
2807 */
Vivien Didelotacddbd22016-07-18 20:45:39 -04002808 err = mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL,
2809 GLOBAL_ATU_CONTROL_LEARN2ALL);
Vivien Didelot08a01262016-05-09 13:22:50 -04002810 if (err)
2811 return err;
2812
Vivien Didelotacddbd22016-07-18 20:45:39 -04002813 err = mv88e6xxx_g1_set_age_time(chip, 300000);
2814 if (err)
Vivien Didelot97299342016-07-18 20:45:30 -04002815 return err;
2816
2817 /* Clear all ATU entries */
2818 err = _mv88e6xxx_atu_flush(chip, 0, true);
2819 if (err)
2820 return err;
2821
Vivien Didelot08a01262016-05-09 13:22:50 -04002822 /* Configure the IP ToS mapping registers. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002823 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_0, 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_1, 0x0000);
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_2, 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_3, 0x5555);
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_4, 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_5, 0xaaaa);
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_6, 0xffff);
Vivien Didelot08a01262016-05-09 13:22:50 -04002842 if (err)
2843 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002844 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_7, 0xffff);
Vivien Didelot08a01262016-05-09 13:22:50 -04002845 if (err)
2846 return err;
2847
2848 /* Configure the IEEE 802.1p priority mapping register. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002849 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IEEE_PRI, 0xfa41);
Vivien Didelot08a01262016-05-09 13:22:50 -04002850 if (err)
2851 return err;
2852
Vivien Didelot97299342016-07-18 20:45:30 -04002853 /* Clear the statistics counters for all ports */
2854 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
2855 GLOBAL_STATS_OP_FLUSH_ALL);
2856 if (err)
2857 return err;
2858
2859 /* Wait for the flush to complete. */
2860 err = _mv88e6xxx_stats_wait(chip);
2861 if (err)
2862 return err;
2863
2864 return 0;
2865}
2866
Vivien Didelotf22ab642016-07-18 20:45:31 -04002867static int mv88e6xxx_g2_device_mapping_write(struct mv88e6xxx_chip *chip,
2868 int target, int port)
2869{
2870 u16 val = (target << 8) | (port & 0xf);
2871
2872 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_DEVICE_MAPPING, val);
2873}
2874
2875static int mv88e6xxx_g2_set_device_mapping(struct mv88e6xxx_chip *chip)
2876{
2877 int target, port;
2878 int err;
2879
2880 /* Initialize the routing port to the 32 possible target devices */
2881 for (target = 0; target < 32; ++target) {
2882 port = 0xf;
2883
2884 if (target < DSA_MAX_SWITCHES) {
2885 port = chip->ds->rtable[target];
2886 if (port == DSA_RTABLE_NONE)
2887 port = 0xf;
2888 }
2889
2890 err = mv88e6xxx_g2_device_mapping_write(chip, target, port);
2891 if (err)
2892 break;
2893 }
2894
2895 return err;
2896}
2897
Vivien Didelot51540412016-07-18 20:45:32 -04002898static int mv88e6xxx_g2_trunk_mask_write(struct mv88e6xxx_chip *chip, int num,
2899 bool hask, u16 mask)
2900{
2901 const u16 port_mask = BIT(chip->info->num_ports) - 1;
2902 u16 val = (num << 12) | (mask & port_mask);
2903
2904 if (hask)
2905 val |= GLOBAL2_TRUNK_MASK_HASK;
2906
2907 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_TRUNK_MASK, val);
2908}
2909
2910static int mv88e6xxx_g2_trunk_mapping_write(struct mv88e6xxx_chip *chip, int id,
2911 u16 map)
2912{
2913 const u16 port_mask = BIT(chip->info->num_ports) - 1;
2914 u16 val = (id << 11) | (map & port_mask);
2915
2916 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_TRUNK_MAPPING, val);
2917}
2918
2919static int mv88e6xxx_g2_clear_trunk(struct mv88e6xxx_chip *chip)
2920{
2921 const u16 port_mask = BIT(chip->info->num_ports) - 1;
2922 int i, err;
2923
2924 /* Clear all eight possible Trunk Mask vectors */
2925 for (i = 0; i < 8; ++i) {
2926 err = mv88e6xxx_g2_trunk_mask_write(chip, i, false, port_mask);
2927 if (err)
2928 return err;
2929 }
2930
2931 /* Clear all sixteen possible Trunk ID routing vectors */
2932 for (i = 0; i < 16; ++i) {
2933 err = mv88e6xxx_g2_trunk_mapping_write(chip, i, 0);
2934 if (err)
2935 return err;
2936 }
2937
2938 return 0;
2939}
2940
Vivien Didelot8ec61c72016-07-18 20:45:37 -04002941static int mv88e6xxx_g2_clear_irl(struct mv88e6xxx_chip *chip)
2942{
2943 int port, err;
2944
2945 /* Init all Ingress Rate Limit resources of all ports */
2946 for (port = 0; port < chip->info->num_ports; ++port) {
2947 /* XXX newer chips (like 88E6390) have different 2-bit ops */
2948 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_IRL_CMD,
2949 GLOBAL2_IRL_CMD_OP_INIT_ALL |
2950 (port << 8));
2951 if (err)
2952 break;
2953
2954 /* Wait for the operation to complete */
Vivien Didelot2d79af62016-08-15 17:18:57 -04002955 err = mv88e6xxx_wait(chip, REG_GLOBAL2, GLOBAL2_IRL_CMD,
2956 GLOBAL2_IRL_CMD_BUSY);
Vivien Didelot8ec61c72016-07-18 20:45:37 -04002957 if (err)
2958 break;
2959 }
2960
2961 return err;
2962}
2963
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002964/* Indirect write to the Switch MAC/WoL/WoF register */
2965static int mv88e6xxx_g2_switch_mac_write(struct mv88e6xxx_chip *chip,
2966 unsigned int pointer, u8 data)
2967{
2968 u16 val = (pointer << 8) | data;
2969
2970 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_SWITCH_MAC, val);
2971}
2972
2973static int mv88e6xxx_g2_set_switch_mac(struct mv88e6xxx_chip *chip, u8 *addr)
2974{
2975 int i, err;
2976
2977 for (i = 0; i < 6; i++) {
2978 err = mv88e6xxx_g2_switch_mac_write(chip, i, addr[i]);
2979 if (err)
2980 break;
2981 }
2982
2983 return err;
2984}
2985
Vivien Didelot9bda8892016-07-18 20:45:36 -04002986static int mv88e6xxx_g2_pot_write(struct mv88e6xxx_chip *chip, int pointer,
2987 u8 data)
2988{
2989 u16 val = (pointer << 8) | (data & 0x7);
2990
2991 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_PRIO_OVERRIDE, val);
2992}
2993
2994static int mv88e6xxx_g2_clear_pot(struct mv88e6xxx_chip *chip)
2995{
2996 int i, err;
2997
2998 /* Clear all sixteen possible Priority Override entries */
2999 for (i = 0; i < 16; i++) {
3000 err = mv88e6xxx_g2_pot_write(chip, i, 0);
3001 if (err)
3002 break;
3003 }
3004
3005 return err;
3006}
3007
Vivien Didelot855b1932016-07-20 18:18:35 -04003008static int mv88e6xxx_g2_eeprom_wait(struct mv88e6xxx_chip *chip)
3009{
Vivien Didelot2d79af62016-08-15 17:18:57 -04003010 return mv88e6xxx_wait(chip, REG_GLOBAL2, GLOBAL2_EEPROM_CMD,
3011 GLOBAL2_EEPROM_CMD_BUSY |
3012 GLOBAL2_EEPROM_CMD_RUNNING);
Vivien Didelot855b1932016-07-20 18:18:35 -04003013}
3014
3015static int mv88e6xxx_g2_eeprom_cmd(struct mv88e6xxx_chip *chip, u16 cmd)
3016{
3017 int err;
3018
3019 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_EEPROM_CMD, cmd);
3020 if (err)
3021 return err;
3022
3023 return mv88e6xxx_g2_eeprom_wait(chip);
3024}
3025
3026static int mv88e6xxx_g2_eeprom_read16(struct mv88e6xxx_chip *chip,
3027 u8 addr, u16 *data)
3028{
3029 u16 cmd = GLOBAL2_EEPROM_CMD_OP_READ | addr;
3030 int err;
3031
3032 err = mv88e6xxx_g2_eeprom_wait(chip);
3033 if (err)
3034 return err;
3035
3036 err = mv88e6xxx_g2_eeprom_cmd(chip, cmd);
3037 if (err)
3038 return err;
3039
3040 return mv88e6xxx_read(chip, REG_GLOBAL2, GLOBAL2_EEPROM_DATA, data);
3041}
3042
3043static int mv88e6xxx_g2_eeprom_write16(struct mv88e6xxx_chip *chip,
3044 u8 addr, u16 data)
3045{
3046 u16 cmd = GLOBAL2_EEPROM_CMD_OP_WRITE | addr;
3047 int err;
3048
3049 err = mv88e6xxx_g2_eeprom_wait(chip);
3050 if (err)
3051 return err;
3052
3053 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_EEPROM_DATA, data);
3054 if (err)
3055 return err;
3056
3057 return mv88e6xxx_g2_eeprom_cmd(chip, cmd);
3058}
3059
Vivien Didelot97299342016-07-18 20:45:30 -04003060static int mv88e6xxx_g2_setup(struct mv88e6xxx_chip *chip)
3061{
Vivien Didelot47395ed2016-07-18 20:45:33 -04003062 u16 reg;
Vivien Didelot97299342016-07-18 20:45:30 -04003063 int err;
Vivien Didelot97299342016-07-18 20:45:30 -04003064
Vivien Didelot47395ed2016-07-18 20:45:33 -04003065 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_2X)) {
3066 /* Consider the frames with reserved multicast destination
3067 * addresses matching 01:80:c2:00:00:2x as MGMT.
3068 */
3069 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_MGMT_EN_2X,
3070 0xffff);
3071 if (err)
3072 return err;
3073 }
3074
3075 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_0X)) {
3076 /* Consider the frames with reserved multicast destination
3077 * addresses matching 01:80:c2:00:00:0x as MGMT.
3078 */
3079 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_MGMT_EN_0X,
3080 0xffff);
3081 if (err)
3082 return err;
3083 }
Vivien Didelot08a01262016-05-09 13:22:50 -04003084
3085 /* Ignore removed tag data on doubly tagged packets, disable
3086 * flow control messages, force flow control priority to the
3087 * highest, and send all special multicast frames to the CPU
3088 * port at the highest priority.
3089 */
Vivien Didelot47395ed2016-07-18 20:45:33 -04003090 reg = GLOBAL2_SWITCH_MGMT_FORCE_FLOW_CTRL_PRI | (0x7 << 4);
3091 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_0X) ||
3092 mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_2X))
3093 reg |= GLOBAL2_SWITCH_MGMT_RSVD2CPU | 0x7;
3094 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_SWITCH_MGMT, reg);
Vivien Didelot08a01262016-05-09 13:22:50 -04003095 if (err)
3096 return err;
3097
3098 /* Program the DSA routing table. */
Vivien Didelotf22ab642016-07-18 20:45:31 -04003099 err = mv88e6xxx_g2_set_device_mapping(chip);
3100 if (err)
3101 return err;
Vivien Didelot08a01262016-05-09 13:22:50 -04003102
Vivien Didelot51540412016-07-18 20:45:32 -04003103 /* Clear all trunk masks and mapping. */
3104 err = mv88e6xxx_g2_clear_trunk(chip);
3105 if (err)
3106 return err;
Vivien Didelot08a01262016-05-09 13:22:50 -04003107
Vivien Didelot8ec61c72016-07-18 20:45:37 -04003108 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_IRL)) {
3109 /* Disable ingress rate limiting by resetting all per port
3110 * ingress rate limit resources to their initial state.
3111 */
3112 err = mv88e6xxx_g2_clear_irl(chip);
3113 if (err)
3114 return err;
3115 }
3116
Vivien Didelot63ed8802016-07-18 20:45:35 -04003117 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_PVT)) {
3118 /* Initialize Cross-chip Port VLAN Table to reset defaults */
3119 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_PVT_ADDR,
3120 GLOBAL2_PVT_ADDR_OP_INIT_ONES);
3121 if (err)
3122 return err;
3123 }
3124
Vivien Didelot9bda8892016-07-18 20:45:36 -04003125 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_POT)) {
Vivien Didelot08a01262016-05-09 13:22:50 -04003126 /* Clear the priority override table. */
Vivien Didelot9bda8892016-07-18 20:45:36 -04003127 err = mv88e6xxx_g2_clear_pot(chip);
3128 if (err)
3129 return err;
Vivien Didelot08a01262016-05-09 13:22:50 -04003130 }
3131
Vivien Didelot97299342016-07-18 20:45:30 -04003132 return 0;
Vivien Didelot08a01262016-05-09 13:22:50 -04003133}
3134
Vivien Didelotf81ec902016-05-09 13:22:58 -04003135static int mv88e6xxx_setup(struct dsa_switch *ds)
Guenter Roeckacdaffc2015-03-26 18:36:28 -07003136{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003137 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Vivien Didelot552238b2016-05-09 13:22:49 -04003138 int err;
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003139 int i;
3140
Vivien Didelotfad09c72016-06-21 12:28:20 -04003141 chip->ds = ds;
3142 ds->slave_mii_bus = chip->mdio_bus;
Vivien Didelot552238b2016-05-09 13:22:49 -04003143
Vivien Didelotfad09c72016-06-21 12:28:20 -04003144 mutex_lock(&chip->reg_lock);
Vivien Didelot552238b2016-05-09 13:22:49 -04003145
Vivien Didelotfad09c72016-06-21 12:28:20 -04003146 err = mv88e6xxx_switch_reset(chip);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003147 if (err)
3148 goto unlock;
Andrew Lunn54d792f2015-05-06 01:09:47 +02003149
Vivien Didelot97299342016-07-18 20:45:30 -04003150 /* Setup Switch Port Registers */
3151 for (i = 0; i < chip->info->num_ports; i++) {
3152 err = mv88e6xxx_setup_port(chip, i);
3153 if (err)
3154 goto unlock;
3155 }
3156
3157 /* Setup Switch Global 1 Registers */
3158 err = mv88e6xxx_g1_setup(chip);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003159 if (err)
3160 goto unlock;
3161
Vivien Didelot97299342016-07-18 20:45:30 -04003162 /* Setup Switch Global 2 Registers */
3163 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_GLOBAL2)) {
3164 err = mv88e6xxx_g2_setup(chip);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003165 if (err)
3166 goto unlock;
3167 }
Andrew Lunn54d792f2015-05-06 01:09:47 +02003168
Vivien Didelot6b17e862015-08-13 12:52:18 -04003169unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04003170 mutex_unlock(&chip->reg_lock);
Andrew Lunndb687a52015-06-20 21:31:29 +02003171
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003172 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02003173}
3174
Vivien Didelot3b4caa12016-07-18 20:45:34 -04003175static int mv88e6xxx_set_addr(struct dsa_switch *ds, u8 *addr)
3176{
3177 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3178 int err;
3179
3180 mutex_lock(&chip->reg_lock);
3181
3182 /* Has an indirect Switch MAC/WoL/WoF register in Global 2? */
3183 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_SWITCH_MAC))
3184 err = mv88e6xxx_g2_set_switch_mac(chip, addr);
3185 else
3186 err = mv88e6xxx_g1_set_switch_mac(chip, addr);
3187
3188 mutex_unlock(&chip->reg_lock);
3189
3190 return err;
3191}
3192
Vivien Didelot57d32312016-06-20 13:13:58 -04003193static int mv88e6xxx_mdio_page_read(struct dsa_switch *ds, int port, int page,
3194 int reg)
Andrew Lunn491435852015-04-02 04:06:35 +02003195{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003196 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunn491435852015-04-02 04:06:35 +02003197 int ret;
3198
Vivien Didelotfad09c72016-06-21 12:28:20 -04003199 mutex_lock(&chip->reg_lock);
3200 ret = _mv88e6xxx_mdio_page_read(chip, port, page, reg);
3201 mutex_unlock(&chip->reg_lock);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00003202
Andrew Lunn491435852015-04-02 04:06:35 +02003203 return ret;
3204}
3205
Vivien Didelot57d32312016-06-20 13:13:58 -04003206static int mv88e6xxx_mdio_page_write(struct dsa_switch *ds, int port, int page,
3207 int reg, int val)
Andrew Lunn491435852015-04-02 04:06:35 +02003208{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003209 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunn491435852015-04-02 04:06:35 +02003210 int ret;
3211
Vivien Didelotfad09c72016-06-21 12:28:20 -04003212 mutex_lock(&chip->reg_lock);
3213 ret = _mv88e6xxx_mdio_page_write(chip, port, page, reg, val);
3214 mutex_unlock(&chip->reg_lock);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00003215
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003216 return ret;
3217}
3218
Vivien Didelotfad09c72016-06-21 12:28:20 -04003219static int mv88e6xxx_port_to_mdio_addr(struct mv88e6xxx_chip *chip, int port)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003220{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003221 if (port >= 0 && port < chip->info->num_ports)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003222 return port;
3223 return -EINVAL;
3224}
3225
Andrew Lunnb516d452016-06-04 21:17:06 +02003226static int mv88e6xxx_mdio_read(struct mii_bus *bus, int port, int regnum)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003227{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003228 struct mv88e6xxx_chip *chip = bus->priv;
3229 int addr = mv88e6xxx_port_to_mdio_addr(chip, port);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003230 int ret;
3231
3232 if (addr < 0)
Andrew Lunn158bc062016-04-28 21:24:06 -04003233 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003234
Vivien Didelotfad09c72016-06-21 12:28:20 -04003235 mutex_lock(&chip->reg_lock);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003236
Vivien Didelotfad09c72016-06-21 12:28:20 -04003237 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU))
3238 ret = mv88e6xxx_mdio_read_ppu(chip, addr, regnum);
3239 else if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_SMI_PHY))
3240 ret = mv88e6xxx_mdio_read_indirect(chip, addr, regnum);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003241 else
Vivien Didelotfad09c72016-06-21 12:28:20 -04003242 ret = mv88e6xxx_mdio_read_direct(chip, addr, regnum);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003243
Vivien Didelotfad09c72016-06-21 12:28:20 -04003244 mutex_unlock(&chip->reg_lock);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003245 return ret;
3246}
3247
Andrew Lunnb516d452016-06-04 21:17:06 +02003248static int mv88e6xxx_mdio_write(struct mii_bus *bus, int port, int regnum,
Andrew Lunn03a4a542016-06-04 21:17:05 +02003249 u16 val)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003250{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003251 struct mv88e6xxx_chip *chip = bus->priv;
3252 int addr = mv88e6xxx_port_to_mdio_addr(chip, port);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003253 int ret;
3254
3255 if (addr < 0)
Andrew Lunn158bc062016-04-28 21:24:06 -04003256 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003257
Vivien Didelotfad09c72016-06-21 12:28:20 -04003258 mutex_lock(&chip->reg_lock);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003259
Vivien Didelotfad09c72016-06-21 12:28:20 -04003260 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU))
3261 ret = mv88e6xxx_mdio_write_ppu(chip, addr, regnum, val);
3262 else if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_SMI_PHY))
3263 ret = mv88e6xxx_mdio_write_indirect(chip, addr, regnum, val);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003264 else
Vivien Didelotfad09c72016-06-21 12:28:20 -04003265 ret = mv88e6xxx_mdio_write_direct(chip, addr, regnum, val);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003266
Vivien Didelotfad09c72016-06-21 12:28:20 -04003267 mutex_unlock(&chip->reg_lock);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003268 return ret;
3269}
3270
Vivien Didelotfad09c72016-06-21 12:28:20 -04003271static int mv88e6xxx_mdio_register(struct mv88e6xxx_chip *chip,
Andrew Lunnb516d452016-06-04 21:17:06 +02003272 struct device_node *np)
3273{
3274 static int index;
3275 struct mii_bus *bus;
3276 int err;
3277
Vivien Didelotfad09c72016-06-21 12:28:20 -04003278 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU))
3279 mv88e6xxx_ppu_state_init(chip);
Andrew Lunnb516d452016-06-04 21:17:06 +02003280
3281 if (np)
Vivien Didelotfad09c72016-06-21 12:28:20 -04003282 chip->mdio_np = of_get_child_by_name(np, "mdio");
Andrew Lunnb516d452016-06-04 21:17:06 +02003283
Vivien Didelotfad09c72016-06-21 12:28:20 -04003284 bus = devm_mdiobus_alloc(chip->dev);
Andrew Lunnb516d452016-06-04 21:17:06 +02003285 if (!bus)
3286 return -ENOMEM;
3287
Vivien Didelotfad09c72016-06-21 12:28:20 -04003288 bus->priv = (void *)chip;
Andrew Lunnb516d452016-06-04 21:17:06 +02003289 if (np) {
3290 bus->name = np->full_name;
3291 snprintf(bus->id, MII_BUS_ID_SIZE, "%s", np->full_name);
3292 } else {
3293 bus->name = "mv88e6xxx SMI";
3294 snprintf(bus->id, MII_BUS_ID_SIZE, "mv88e6xxx-%d", index++);
3295 }
3296
3297 bus->read = mv88e6xxx_mdio_read;
3298 bus->write = mv88e6xxx_mdio_write;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003299 bus->parent = chip->dev;
Andrew Lunnb516d452016-06-04 21:17:06 +02003300
Vivien Didelotfad09c72016-06-21 12:28:20 -04003301 if (chip->mdio_np)
3302 err = of_mdiobus_register(bus, chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003303 else
3304 err = mdiobus_register(bus);
3305 if (err) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04003306 dev_err(chip->dev, "Cannot register MDIO bus (%d)\n", err);
Andrew Lunnb516d452016-06-04 21:17:06 +02003307 goto out;
3308 }
Vivien Didelotfad09c72016-06-21 12:28:20 -04003309 chip->mdio_bus = bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003310
3311 return 0;
3312
3313out:
Vivien Didelotfad09c72016-06-21 12:28:20 -04003314 if (chip->mdio_np)
3315 of_node_put(chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003316
3317 return err;
3318}
3319
Vivien Didelotfad09c72016-06-21 12:28:20 -04003320static void mv88e6xxx_mdio_unregister(struct mv88e6xxx_chip *chip)
Andrew Lunnb516d452016-06-04 21:17:06 +02003321
3322{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003323 struct mii_bus *bus = chip->mdio_bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003324
3325 mdiobus_unregister(bus);
3326
Vivien Didelotfad09c72016-06-21 12:28:20 -04003327 if (chip->mdio_np)
3328 of_node_put(chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003329}
3330
Guenter Roeckc22995c2015-07-25 09:42:28 -07003331#ifdef CONFIG_NET_DSA_HWMON
3332
3333static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
3334{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003335 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003336 int ret;
3337 int val;
3338
3339 *temp = 0;
3340
Vivien Didelotfad09c72016-06-21 12:28:20 -04003341 mutex_lock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003342
Vivien Didelotfad09c72016-06-21 12:28:20 -04003343 ret = mv88e6xxx_mdio_write_direct(chip, 0x0, 0x16, 0x6);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003344 if (ret < 0)
3345 goto error;
3346
3347 /* Enable temperature sensor */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003348 ret = mv88e6xxx_mdio_read_direct(chip, 0x0, 0x1a);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003349 if (ret < 0)
3350 goto error;
3351
Vivien Didelotfad09c72016-06-21 12:28:20 -04003352 ret = mv88e6xxx_mdio_write_direct(chip, 0x0, 0x1a, ret | (1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003353 if (ret < 0)
3354 goto error;
3355
3356 /* Wait for temperature to stabilize */
3357 usleep_range(10000, 12000);
3358
Vivien Didelotfad09c72016-06-21 12:28:20 -04003359 val = mv88e6xxx_mdio_read_direct(chip, 0x0, 0x1a);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003360 if (val < 0) {
3361 ret = val;
3362 goto error;
3363 }
3364
3365 /* Disable temperature sensor */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003366 ret = mv88e6xxx_mdio_write_direct(chip, 0x0, 0x1a, ret & ~(1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003367 if (ret < 0)
3368 goto error;
3369
3370 *temp = ((val & 0x1f) - 5) * 5;
3371
3372error:
Vivien Didelotfad09c72016-06-21 12:28:20 -04003373 mv88e6xxx_mdio_write_direct(chip, 0x0, 0x16, 0x0);
3374 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003375 return ret;
3376}
3377
3378static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
3379{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003380 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3381 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003382 int ret;
3383
3384 *temp = 0;
3385
Andrew Lunn03a4a542016-06-04 21:17:05 +02003386 ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 27);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003387 if (ret < 0)
3388 return ret;
3389
3390 *temp = (ret & 0xff) - 25;
3391
3392 return 0;
3393}
3394
Vivien Didelotf81ec902016-05-09 13:22:58 -04003395static int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003396{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003397 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunn158bc062016-04-28 21:24:06 -04003398
Vivien Didelotfad09c72016-06-21 12:28:20 -04003399 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP))
Vivien Didelot6594f612016-05-09 13:22:42 -04003400 return -EOPNOTSUPP;
3401
Vivien Didelotfad09c72016-06-21 12:28:20 -04003402 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003403 return mv88e63xx_get_temp(ds, temp);
3404
3405 return mv88e61xx_get_temp(ds, temp);
3406}
3407
Vivien Didelotf81ec902016-05-09 13:22:58 -04003408static int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003409{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003410 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3411 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003412 int ret;
3413
Vivien Didelotfad09c72016-06-21 12:28:20 -04003414 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003415 return -EOPNOTSUPP;
3416
3417 *temp = 0;
3418
Andrew Lunn03a4a542016-06-04 21:17:05 +02003419 ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 26);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003420 if (ret < 0)
3421 return ret;
3422
3423 *temp = (((ret >> 8) & 0x1f) * 5) - 25;
3424
3425 return 0;
3426}
3427
Vivien Didelotf81ec902016-05-09 13:22:58 -04003428static int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003429{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003430 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3431 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003432 int ret;
3433
Vivien Didelotfad09c72016-06-21 12:28:20 -04003434 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003435 return -EOPNOTSUPP;
3436
Andrew Lunn03a4a542016-06-04 21:17:05 +02003437 ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 26);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003438 if (ret < 0)
3439 return ret;
3440 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
Andrew Lunn03a4a542016-06-04 21:17:05 +02003441 return mv88e6xxx_mdio_page_write(ds, phy, 6, 26,
3442 (ret & 0xe0ff) | (temp << 8));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003443}
3444
Vivien Didelotf81ec902016-05-09 13:22:58 -04003445static int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003446{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003447 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3448 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003449 int ret;
3450
Vivien Didelotfad09c72016-06-21 12:28:20 -04003451 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003452 return -EOPNOTSUPP;
3453
3454 *alarm = false;
3455
Andrew Lunn03a4a542016-06-04 21:17:05 +02003456 ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 26);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003457 if (ret < 0)
3458 return ret;
3459
3460 *alarm = !!(ret & 0x40);
3461
3462 return 0;
3463}
3464#endif /* CONFIG_NET_DSA_HWMON */
3465
Vivien Didelot855b1932016-07-20 18:18:35 -04003466static int mv88e6xxx_get_eeprom_len(struct dsa_switch *ds)
3467{
3468 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3469
3470 return chip->eeprom_len;
3471}
3472
3473static int mv88e6xxx_get_eeprom16(struct mv88e6xxx_chip *chip,
3474 struct ethtool_eeprom *eeprom, u8 *data)
3475{
3476 unsigned int offset = eeprom->offset;
3477 unsigned int len = eeprom->len;
3478 u16 val;
3479 int err;
3480
3481 eeprom->len = 0;
3482
3483 if (offset & 1) {
3484 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
3485 if (err)
3486 return err;
3487
3488 *data++ = (val >> 8) & 0xff;
3489
3490 offset++;
3491 len--;
3492 eeprom->len++;
3493 }
3494
3495 while (len >= 2) {
3496 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
3497 if (err)
3498 return err;
3499
3500 *data++ = val & 0xff;
3501 *data++ = (val >> 8) & 0xff;
3502
3503 offset += 2;
3504 len -= 2;
3505 eeprom->len += 2;
3506 }
3507
3508 if (len) {
3509 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
3510 if (err)
3511 return err;
3512
3513 *data++ = val & 0xff;
3514
3515 offset++;
3516 len--;
3517 eeprom->len++;
3518 }
3519
3520 return 0;
3521}
3522
3523static int mv88e6xxx_get_eeprom(struct dsa_switch *ds,
3524 struct ethtool_eeprom *eeprom, u8 *data)
3525{
3526 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3527 int err;
3528
3529 mutex_lock(&chip->reg_lock);
3530
3531 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16))
3532 err = mv88e6xxx_get_eeprom16(chip, eeprom, data);
3533 else
3534 err = -EOPNOTSUPP;
3535
3536 mutex_unlock(&chip->reg_lock);
3537
3538 if (err)
3539 return err;
3540
3541 eeprom->magic = 0xc3ec4951;
3542
3543 return 0;
3544}
3545
3546static int mv88e6xxx_set_eeprom16(struct mv88e6xxx_chip *chip,
3547 struct ethtool_eeprom *eeprom, u8 *data)
3548{
3549 unsigned int offset = eeprom->offset;
3550 unsigned int len = eeprom->len;
3551 u16 val;
3552 int err;
3553
3554 /* Ensure the RO WriteEn bit is set */
3555 err = mv88e6xxx_read(chip, REG_GLOBAL2, GLOBAL2_EEPROM_CMD, &val);
3556 if (err)
3557 return err;
3558
3559 if (!(val & GLOBAL2_EEPROM_CMD_WRITE_EN))
3560 return -EROFS;
3561
3562 eeprom->len = 0;
3563
3564 if (offset & 1) {
3565 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
3566 if (err)
3567 return err;
3568
3569 val = (*data++ << 8) | (val & 0xff);
3570
3571 err = mv88e6xxx_g2_eeprom_write16(chip, offset >> 1, val);
3572 if (err)
3573 return err;
3574
3575 offset++;
3576 len--;
3577 eeprom->len++;
3578 }
3579
3580 while (len >= 2) {
3581 val = *data++;
3582 val |= *data++ << 8;
3583
3584 err = mv88e6xxx_g2_eeprom_write16(chip, offset >> 1, val);
3585 if (err)
3586 return err;
3587
3588 offset += 2;
3589 len -= 2;
3590 eeprom->len += 2;
3591 }
3592
3593 if (len) {
3594 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
3595 if (err)
3596 return err;
3597
3598 val = (val & 0xff00) | *data++;
3599
3600 err = mv88e6xxx_g2_eeprom_write16(chip, offset >> 1, val);
3601 if (err)
3602 return err;
3603
3604 offset++;
3605 len--;
3606 eeprom->len++;
3607 }
3608
3609 return 0;
3610}
3611
3612static int mv88e6xxx_set_eeprom(struct dsa_switch *ds,
3613 struct ethtool_eeprom *eeprom, u8 *data)
3614{
3615 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3616 int err;
3617
3618 if (eeprom->magic != 0xc3ec4951)
3619 return -EINVAL;
3620
3621 mutex_lock(&chip->reg_lock);
3622
3623 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16))
3624 err = mv88e6xxx_set_eeprom16(chip, eeprom, data);
3625 else
3626 err = -EOPNOTSUPP;
3627
3628 mutex_unlock(&chip->reg_lock);
3629
3630 return err;
3631}
3632
Vivien Didelotf81ec902016-05-09 13:22:58 -04003633static const struct mv88e6xxx_info mv88e6xxx_table[] = {
3634 [MV88E6085] = {
3635 .prod_num = PORT_SWITCH_ID_PROD_NUM_6085,
3636 .family = MV88E6XXX_FAMILY_6097,
3637 .name = "Marvell 88E6085",
3638 .num_databases = 4096,
3639 .num_ports = 10,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003640 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003641 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003642 .flags = MV88E6XXX_FLAGS_FAMILY_6097,
3643 },
3644
3645 [MV88E6095] = {
3646 .prod_num = PORT_SWITCH_ID_PROD_NUM_6095,
3647 .family = MV88E6XXX_FAMILY_6095,
3648 .name = "Marvell 88E6095/88E6095F",
3649 .num_databases = 256,
3650 .num_ports = 11,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003651 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003652 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003653 .flags = MV88E6XXX_FLAGS_FAMILY_6095,
3654 },
3655
3656 [MV88E6123] = {
3657 .prod_num = PORT_SWITCH_ID_PROD_NUM_6123,
3658 .family = MV88E6XXX_FAMILY_6165,
3659 .name = "Marvell 88E6123",
3660 .num_databases = 4096,
3661 .num_ports = 3,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003662 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003663 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003664 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3665 },
3666
3667 [MV88E6131] = {
3668 .prod_num = PORT_SWITCH_ID_PROD_NUM_6131,
3669 .family = MV88E6XXX_FAMILY_6185,
3670 .name = "Marvell 88E6131",
3671 .num_databases = 256,
3672 .num_ports = 8,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003673 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003674 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003675 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3676 },
3677
3678 [MV88E6161] = {
3679 .prod_num = PORT_SWITCH_ID_PROD_NUM_6161,
3680 .family = MV88E6XXX_FAMILY_6165,
3681 .name = "Marvell 88E6161",
3682 .num_databases = 4096,
3683 .num_ports = 6,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003684 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003685 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003686 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3687 },
3688
3689 [MV88E6165] = {
3690 .prod_num = PORT_SWITCH_ID_PROD_NUM_6165,
3691 .family = MV88E6XXX_FAMILY_6165,
3692 .name = "Marvell 88E6165",
3693 .num_databases = 4096,
3694 .num_ports = 6,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003695 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003696 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003697 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3698 },
3699
3700 [MV88E6171] = {
3701 .prod_num = PORT_SWITCH_ID_PROD_NUM_6171,
3702 .family = MV88E6XXX_FAMILY_6351,
3703 .name = "Marvell 88E6171",
3704 .num_databases = 4096,
3705 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003706 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003707 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003708 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3709 },
3710
3711 [MV88E6172] = {
3712 .prod_num = PORT_SWITCH_ID_PROD_NUM_6172,
3713 .family = MV88E6XXX_FAMILY_6352,
3714 .name = "Marvell 88E6172",
3715 .num_databases = 4096,
3716 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003717 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003718 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003719 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3720 },
3721
3722 [MV88E6175] = {
3723 .prod_num = PORT_SWITCH_ID_PROD_NUM_6175,
3724 .family = MV88E6XXX_FAMILY_6351,
3725 .name = "Marvell 88E6175",
3726 .num_databases = 4096,
3727 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003728 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003729 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003730 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3731 },
3732
3733 [MV88E6176] = {
3734 .prod_num = PORT_SWITCH_ID_PROD_NUM_6176,
3735 .family = MV88E6XXX_FAMILY_6352,
3736 .name = "Marvell 88E6176",
3737 .num_databases = 4096,
3738 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003739 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003740 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003741 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3742 },
3743
3744 [MV88E6185] = {
3745 .prod_num = PORT_SWITCH_ID_PROD_NUM_6185,
3746 .family = MV88E6XXX_FAMILY_6185,
3747 .name = "Marvell 88E6185",
3748 .num_databases = 256,
3749 .num_ports = 10,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003750 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003751 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003752 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3753 },
3754
3755 [MV88E6240] = {
3756 .prod_num = PORT_SWITCH_ID_PROD_NUM_6240,
3757 .family = MV88E6XXX_FAMILY_6352,
3758 .name = "Marvell 88E6240",
3759 .num_databases = 4096,
3760 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003761 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003762 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003763 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3764 },
3765
3766 [MV88E6320] = {
3767 .prod_num = PORT_SWITCH_ID_PROD_NUM_6320,
3768 .family = MV88E6XXX_FAMILY_6320,
3769 .name = "Marvell 88E6320",
3770 .num_databases = 4096,
3771 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003772 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003773 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003774 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3775 },
3776
3777 [MV88E6321] = {
3778 .prod_num = PORT_SWITCH_ID_PROD_NUM_6321,
3779 .family = MV88E6XXX_FAMILY_6320,
3780 .name = "Marvell 88E6321",
3781 .num_databases = 4096,
3782 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003783 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003784 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003785 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3786 },
3787
3788 [MV88E6350] = {
3789 .prod_num = PORT_SWITCH_ID_PROD_NUM_6350,
3790 .family = MV88E6XXX_FAMILY_6351,
3791 .name = "Marvell 88E6350",
3792 .num_databases = 4096,
3793 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003794 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003795 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003796 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3797 },
3798
3799 [MV88E6351] = {
3800 .prod_num = PORT_SWITCH_ID_PROD_NUM_6351,
3801 .family = MV88E6XXX_FAMILY_6351,
3802 .name = "Marvell 88E6351",
3803 .num_databases = 4096,
3804 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003805 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003806 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003807 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3808 },
3809
3810 [MV88E6352] = {
3811 .prod_num = PORT_SWITCH_ID_PROD_NUM_6352,
3812 .family = MV88E6XXX_FAMILY_6352,
3813 .name = "Marvell 88E6352",
3814 .num_databases = 4096,
3815 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003816 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003817 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003818 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3819 },
3820};
3821
Vivien Didelot5f7c0362016-06-20 13:14:04 -04003822static const struct mv88e6xxx_info *mv88e6xxx_lookup_info(unsigned int prod_num)
Vivien Didelotb9b37712015-10-30 19:39:48 -04003823{
Vivien Didelota439c062016-04-17 13:23:58 -04003824 int i;
Vivien Didelotb9b37712015-10-30 19:39:48 -04003825
Vivien Didelot5f7c0362016-06-20 13:14:04 -04003826 for (i = 0; i < ARRAY_SIZE(mv88e6xxx_table); ++i)
3827 if (mv88e6xxx_table[i].prod_num == prod_num)
3828 return &mv88e6xxx_table[i];
Vivien Didelotb9b37712015-10-30 19:39:48 -04003829
Vivien Didelotb9b37712015-10-30 19:39:48 -04003830 return NULL;
3831}
3832
Vivien Didelotfad09c72016-06-21 12:28:20 -04003833static int mv88e6xxx_detect(struct mv88e6xxx_chip *chip)
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003834{
3835 const struct mv88e6xxx_info *info;
Vivien Didelot8f6345b2016-07-20 18:18:36 -04003836 unsigned int prod_num, rev;
3837 u16 id;
3838 int err;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003839
Vivien Didelot8f6345b2016-07-20 18:18:36 -04003840 mutex_lock(&chip->reg_lock);
3841 err = mv88e6xxx_port_read(chip, 0, PORT_SWITCH_ID, &id);
3842 mutex_unlock(&chip->reg_lock);
3843 if (err)
3844 return err;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003845
3846 prod_num = (id & 0xfff0) >> 4;
3847 rev = id & 0x000f;
3848
3849 info = mv88e6xxx_lookup_info(prod_num);
3850 if (!info)
3851 return -ENODEV;
3852
Vivien Didelotcaac8542016-06-20 13:14:09 -04003853 /* Update the compatible info with the probed one */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003854 chip->info = info;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003855
Vivien Didelotfad09c72016-06-21 12:28:20 -04003856 dev_info(chip->dev, "switch 0x%x detected: %s, revision %u\n",
3857 chip->info->prod_num, chip->info->name, rev);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003858
3859 return 0;
3860}
3861
Vivien Didelotfad09c72016-06-21 12:28:20 -04003862static struct mv88e6xxx_chip *mv88e6xxx_alloc_chip(struct device *dev)
Vivien Didelot469d7292016-06-20 13:14:06 -04003863{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003864 struct mv88e6xxx_chip *chip;
Vivien Didelot469d7292016-06-20 13:14:06 -04003865
Vivien Didelotfad09c72016-06-21 12:28:20 -04003866 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
3867 if (!chip)
Vivien Didelot469d7292016-06-20 13:14:06 -04003868 return NULL;
3869
Vivien Didelotfad09c72016-06-21 12:28:20 -04003870 chip->dev = dev;
Vivien Didelot469d7292016-06-20 13:14:06 -04003871
Vivien Didelotfad09c72016-06-21 12:28:20 -04003872 mutex_init(&chip->reg_lock);
Vivien Didelot469d7292016-06-20 13:14:06 -04003873
Vivien Didelotfad09c72016-06-21 12:28:20 -04003874 return chip;
Vivien Didelot469d7292016-06-20 13:14:06 -04003875}
3876
Vivien Didelotfad09c72016-06-21 12:28:20 -04003877static int mv88e6xxx_smi_init(struct mv88e6xxx_chip *chip,
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003878 struct mii_bus *bus, int sw_addr)
3879{
3880 /* ADDR[0] pin is unavailable externally and considered zero */
3881 if (sw_addr & 0x1)
3882 return -EINVAL;
3883
Vivien Didelot914b32f2016-06-20 13:14:11 -04003884 if (sw_addr == 0)
Vivien Didelotfad09c72016-06-21 12:28:20 -04003885 chip->smi_ops = &mv88e6xxx_smi_single_chip_ops;
Vivien Didelota0ffff22016-08-15 17:18:58 -04003886 else if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_MULTI_CHIP))
Vivien Didelotfad09c72016-06-21 12:28:20 -04003887 chip->smi_ops = &mv88e6xxx_smi_multi_chip_ops;
Vivien Didelot914b32f2016-06-20 13:14:11 -04003888 else
3889 return -EINVAL;
3890
Vivien Didelotfad09c72016-06-21 12:28:20 -04003891 chip->bus = bus;
3892 chip->sw_addr = sw_addr;
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003893
3894 return 0;
3895}
3896
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02003897static const char *mv88e6xxx_drv_probe(struct device *dsa_dev,
3898 struct device *host_dev, int sw_addr,
3899 void **priv)
Andrew Lunna77d43f2016-04-13 02:40:42 +02003900{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003901 struct mv88e6xxx_chip *chip;
Vivien Didelota439c062016-04-17 13:23:58 -04003902 struct mii_bus *bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003903 int err;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003904
Vivien Didelota439c062016-04-17 13:23:58 -04003905 bus = dsa_host_dev_to_mii_bus(host_dev);
Andrew Lunnc1569132016-04-13 02:40:45 +02003906 if (!bus)
3907 return NULL;
3908
Vivien Didelotfad09c72016-06-21 12:28:20 -04003909 chip = mv88e6xxx_alloc_chip(dsa_dev);
3910 if (!chip)
Vivien Didelot469d7292016-06-20 13:14:06 -04003911 return NULL;
3912
Vivien Didelotcaac8542016-06-20 13:14:09 -04003913 /* Legacy SMI probing will only support chips similar to 88E6085 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003914 chip->info = &mv88e6xxx_table[MV88E6085];
Vivien Didelotcaac8542016-06-20 13:14:09 -04003915
Vivien Didelotfad09c72016-06-21 12:28:20 -04003916 err = mv88e6xxx_smi_init(chip, bus, sw_addr);
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003917 if (err)
3918 goto free;
3919
Vivien Didelotfad09c72016-06-21 12:28:20 -04003920 err = mv88e6xxx_detect(chip);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003921 if (err)
Vivien Didelot469d7292016-06-20 13:14:06 -04003922 goto free;
Vivien Didelota439c062016-04-17 13:23:58 -04003923
Vivien Didelotfad09c72016-06-21 12:28:20 -04003924 err = mv88e6xxx_mdio_register(chip, NULL);
Andrew Lunnb516d452016-06-04 21:17:06 +02003925 if (err)
Vivien Didelot469d7292016-06-20 13:14:06 -04003926 goto free;
Andrew Lunnb516d452016-06-04 21:17:06 +02003927
Vivien Didelotfad09c72016-06-21 12:28:20 -04003928 *priv = chip;
Vivien Didelota439c062016-04-17 13:23:58 -04003929
Vivien Didelotfad09c72016-06-21 12:28:20 -04003930 return chip->info->name;
Vivien Didelot469d7292016-06-20 13:14:06 -04003931free:
Vivien Didelotfad09c72016-06-21 12:28:20 -04003932 devm_kfree(dsa_dev, chip);
Vivien Didelot469d7292016-06-20 13:14:06 -04003933
3934 return NULL;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003935}
3936
Vivien Didelot57d32312016-06-20 13:13:58 -04003937static struct dsa_switch_driver mv88e6xxx_switch_driver = {
Vivien Didelotf81ec902016-05-09 13:22:58 -04003938 .tag_protocol = DSA_TAG_PROTO_EDSA,
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02003939 .probe = mv88e6xxx_drv_probe,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003940 .setup = mv88e6xxx_setup,
3941 .set_addr = mv88e6xxx_set_addr,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003942 .adjust_link = mv88e6xxx_adjust_link,
3943 .get_strings = mv88e6xxx_get_strings,
3944 .get_ethtool_stats = mv88e6xxx_get_ethtool_stats,
3945 .get_sset_count = mv88e6xxx_get_sset_count,
3946 .set_eee = mv88e6xxx_set_eee,
3947 .get_eee = mv88e6xxx_get_eee,
3948#ifdef CONFIG_NET_DSA_HWMON
3949 .get_temp = mv88e6xxx_get_temp,
3950 .get_temp_limit = mv88e6xxx_get_temp_limit,
3951 .set_temp_limit = mv88e6xxx_set_temp_limit,
3952 .get_temp_alarm = mv88e6xxx_get_temp_alarm,
3953#endif
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003954 .get_eeprom_len = mv88e6xxx_get_eeprom_len,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003955 .get_eeprom = mv88e6xxx_get_eeprom,
3956 .set_eeprom = mv88e6xxx_set_eeprom,
3957 .get_regs_len = mv88e6xxx_get_regs_len,
3958 .get_regs = mv88e6xxx_get_regs,
Vivien Didelot2cfcd962016-07-18 20:45:40 -04003959 .set_ageing_time = mv88e6xxx_set_ageing_time,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003960 .port_bridge_join = mv88e6xxx_port_bridge_join,
3961 .port_bridge_leave = mv88e6xxx_port_bridge_leave,
3962 .port_stp_state_set = mv88e6xxx_port_stp_state_set,
3963 .port_vlan_filtering = mv88e6xxx_port_vlan_filtering,
3964 .port_vlan_prepare = mv88e6xxx_port_vlan_prepare,
3965 .port_vlan_add = mv88e6xxx_port_vlan_add,
3966 .port_vlan_del = mv88e6xxx_port_vlan_del,
3967 .port_vlan_dump = mv88e6xxx_port_vlan_dump,
3968 .port_fdb_prepare = mv88e6xxx_port_fdb_prepare,
3969 .port_fdb_add = mv88e6xxx_port_fdb_add,
3970 .port_fdb_del = mv88e6xxx_port_fdb_del,
3971 .port_fdb_dump = mv88e6xxx_port_fdb_dump,
3972};
3973
Vivien Didelotfad09c72016-06-21 12:28:20 -04003974static int mv88e6xxx_register_switch(struct mv88e6xxx_chip *chip,
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003975 struct device_node *np)
3976{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003977 struct device *dev = chip->dev;
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003978 struct dsa_switch *ds;
3979
3980 ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
3981 if (!ds)
3982 return -ENOMEM;
3983
3984 ds->dev = dev;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003985 ds->priv = chip;
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003986 ds->drv = &mv88e6xxx_switch_driver;
3987
3988 dev_set_drvdata(dev, ds);
3989
3990 return dsa_register_switch(ds, np);
3991}
3992
Vivien Didelotfad09c72016-06-21 12:28:20 -04003993static void mv88e6xxx_unregister_switch(struct mv88e6xxx_chip *chip)
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003994{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003995 dsa_unregister_switch(chip->ds);
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003996}
3997
Vivien Didelot57d32312016-06-20 13:13:58 -04003998static int mv88e6xxx_probe(struct mdio_device *mdiodev)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003999{
4000 struct device *dev = &mdiodev->dev;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02004001 struct device_node *np = dev->of_node;
Vivien Didelotcaac8542016-06-20 13:14:09 -04004002 const struct mv88e6xxx_info *compat_info;
Vivien Didelotfad09c72016-06-21 12:28:20 -04004003 struct mv88e6xxx_chip *chip;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02004004 u32 eeprom_len;
Andrew Lunn52638f72016-05-10 23:27:22 +02004005 int err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004006
Vivien Didelotcaac8542016-06-20 13:14:09 -04004007 compat_info = of_device_get_match_data(dev);
4008 if (!compat_info)
4009 return -EINVAL;
4010
Vivien Didelotfad09c72016-06-21 12:28:20 -04004011 chip = mv88e6xxx_alloc_chip(dev);
4012 if (!chip)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004013 return -ENOMEM;
4014
Vivien Didelotfad09c72016-06-21 12:28:20 -04004015 chip->info = compat_info;
Vivien Didelotcaac8542016-06-20 13:14:09 -04004016
Vivien Didelotfad09c72016-06-21 12:28:20 -04004017 err = mv88e6xxx_smi_init(chip, mdiodev->bus, mdiodev->addr);
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04004018 if (err)
4019 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004020
Vivien Didelotfad09c72016-06-21 12:28:20 -04004021 err = mv88e6xxx_detect(chip);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04004022 if (err)
4023 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004024
Vivien Didelotfad09c72016-06-21 12:28:20 -04004025 chip->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_ASIS);
4026 if (IS_ERR(chip->reset))
4027 return PTR_ERR(chip->reset);
Andrew Lunn52638f72016-05-10 23:27:22 +02004028
Vivien Didelot855b1932016-07-20 18:18:35 -04004029 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16) &&
Andrew Lunnf8cd8752016-05-10 23:27:25 +02004030 !of_property_read_u32(np, "eeprom-length", &eeprom_len))
Vivien Didelotfad09c72016-06-21 12:28:20 -04004031 chip->eeprom_len = eeprom_len;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02004032
Vivien Didelotfad09c72016-06-21 12:28:20 -04004033 err = mv88e6xxx_mdio_register(chip, np);
Andrew Lunnb516d452016-06-04 21:17:06 +02004034 if (err)
4035 return err;
4036
Vivien Didelotfad09c72016-06-21 12:28:20 -04004037 err = mv88e6xxx_register_switch(chip, np);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02004038 if (err) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04004039 mv88e6xxx_mdio_unregister(chip);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02004040 return err;
4041 }
4042
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004043 return 0;
4044}
4045
4046static void mv88e6xxx_remove(struct mdio_device *mdiodev)
4047{
4048 struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev);
Vivien Didelotfad09c72016-06-21 12:28:20 -04004049 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004050
Vivien Didelotfad09c72016-06-21 12:28:20 -04004051 mv88e6xxx_unregister_switch(chip);
4052 mv88e6xxx_mdio_unregister(chip);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004053}
4054
4055static const struct of_device_id mv88e6xxx_of_match[] = {
Vivien Didelotcaac8542016-06-20 13:14:09 -04004056 {
4057 .compatible = "marvell,mv88e6085",
4058 .data = &mv88e6xxx_table[MV88E6085],
4059 },
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004060 { /* sentinel */ },
4061};
4062
4063MODULE_DEVICE_TABLE(of, mv88e6xxx_of_match);
4064
4065static struct mdio_driver mv88e6xxx_driver = {
4066 .probe = mv88e6xxx_probe,
4067 .remove = mv88e6xxx_remove,
4068 .mdiodrv.driver = {
4069 .name = "mv88e6085",
4070 .of_match_table = mv88e6xxx_of_match,
4071 },
4072};
4073
Ben Hutchings98e67302011-11-25 14:36:19 +00004074static int __init mv88e6xxx_init(void)
4075{
Vivien Didelotf81ec902016-05-09 13:22:58 -04004076 register_switch_driver(&mv88e6xxx_switch_driver);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004077 return mdio_driver_register(&mv88e6xxx_driver);
Ben Hutchings98e67302011-11-25 14:36:19 +00004078}
4079module_init(mv88e6xxx_init);
4080
4081static void __exit mv88e6xxx_cleanup(void)
4082{
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004083 mdio_driver_unregister(&mv88e6xxx_driver);
Vivien Didelotf81ec902016-05-09 13:22:58 -04004084 unregister_switch_driver(&mv88e6xxx_switch_driver);
Ben Hutchings98e67302011-11-25 14:36:19 +00004085}
4086module_exit(mv88e6xxx_cleanup);
Ben Hutchings3d825ed2011-11-25 14:37:16 +00004087
4088MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
4089MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
4090MODULE_LICENSE("GPL");