blob: 17aa74f64233ccdfff5e8132c5a54ec1b3743e5e [file] [log] [blame]
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001/*
2 * net/dsa/mv88e6xxx.c - Marvell 88e6xxx switch chip support
3 * Copyright (c) 2008 Marvell Semiconductor
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 */
10
Barry Grussling19b2f972013-01-08 16:05:54 +000011#include <linux/delay.h>
Guenter Roeckfacd95b2015-03-26 18:36:35 -070012#include <linux/if_bridge.h>
Barry Grussling19b2f972013-01-08 16:05:54 +000013#include <linux/jiffies.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000014#include <linux/list.h>
Paul Gortmaker2bbba272012-01-24 10:41:40 +000015#include <linux/module.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000016#include <linux/netdevice.h>
17#include <linux/phy.h>
Ben Hutchingsc8f0b862011-11-27 17:06:08 +000018#include <net/dsa.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000019#include "mv88e6xxx.h"
20
Barry Grussling3675c8d2013-01-08 16:05:53 +000021/* If the switch's ADDR[4:0] strap pins are strapped to zero, it will
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000022 * use all 32 SMI bus addresses on its SMI bus, and all switch registers
23 * will be directly accessible on some {device address,register address}
24 * pair. If the ADDR[4:0] pins are not strapped to zero, the switch
25 * will only respond to SMI transactions to that specific address, and
26 * an indirect addressing mechanism needs to be used to access its
27 * registers.
28 */
29static int mv88e6xxx_reg_wait_ready(struct mii_bus *bus, int sw_addr)
30{
31 int ret;
32 int i;
33
34 for (i = 0; i < 16; i++) {
35 ret = mdiobus_read(bus, sw_addr, 0);
36 if (ret < 0)
37 return ret;
38
39 if ((ret & 0x8000) == 0)
40 return 0;
41 }
42
43 return -ETIMEDOUT;
44}
45
46int __mv88e6xxx_reg_read(struct mii_bus *bus, int sw_addr, int addr, int reg)
47{
48 int ret;
49
50 if (sw_addr == 0)
51 return mdiobus_read(bus, addr, reg);
52
Barry Grussling3675c8d2013-01-08 16:05:53 +000053 /* Wait for the bus to become free. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000054 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
55 if (ret < 0)
56 return ret;
57
Barry Grussling3675c8d2013-01-08 16:05:53 +000058 /* Transmit the read command. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000059 ret = mdiobus_write(bus, sw_addr, 0, 0x9800 | (addr << 5) | reg);
60 if (ret < 0)
61 return ret;
62
Barry Grussling3675c8d2013-01-08 16:05:53 +000063 /* Wait for the read command to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000064 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
65 if (ret < 0)
66 return ret;
67
Barry Grussling3675c8d2013-01-08 16:05:53 +000068 /* Read the data. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000069 ret = mdiobus_read(bus, sw_addr, 1);
70 if (ret < 0)
71 return ret;
72
73 return ret & 0xffff;
74}
75
Guenter Roeck8d6d09e2015-03-26 18:36:31 -070076/* Must be called with SMI mutex held */
77static int _mv88e6xxx_reg_read(struct dsa_switch *ds, int addr, int reg)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000078{
Guenter Roeckb184e492014-10-17 12:30:58 -070079 struct mii_bus *bus = dsa_host_dev_to_mii_bus(ds->master_dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000080 int ret;
81
Guenter Roeckb184e492014-10-17 12:30:58 -070082 if (bus == NULL)
83 return -EINVAL;
84
Guenter Roeckb184e492014-10-17 12:30:58 -070085 ret = __mv88e6xxx_reg_read(bus, ds->pd->sw_addr, addr, reg);
Vivien Didelotbb92ea52015-01-23 16:10:36 -050086 if (ret < 0)
87 return ret;
88
89 dev_dbg(ds->master_dev, "<- addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
90 addr, reg, ret);
91
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000092 return ret;
93}
94
Guenter Roeck8d6d09e2015-03-26 18:36:31 -070095int mv88e6xxx_reg_read(struct dsa_switch *ds, int addr, int reg)
96{
97 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
98 int ret;
99
100 mutex_lock(&ps->smi_mutex);
101 ret = _mv88e6xxx_reg_read(ds, addr, reg);
102 mutex_unlock(&ps->smi_mutex);
103
104 return ret;
105}
106
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000107int __mv88e6xxx_reg_write(struct mii_bus *bus, int sw_addr, int addr,
108 int reg, u16 val)
109{
110 int ret;
111
112 if (sw_addr == 0)
113 return mdiobus_write(bus, addr, reg, val);
114
Barry Grussling3675c8d2013-01-08 16:05:53 +0000115 /* Wait for the bus to become free. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000116 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
117 if (ret < 0)
118 return ret;
119
Barry Grussling3675c8d2013-01-08 16:05:53 +0000120 /* Transmit the data to write. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000121 ret = mdiobus_write(bus, sw_addr, 1, val);
122 if (ret < 0)
123 return ret;
124
Barry Grussling3675c8d2013-01-08 16:05:53 +0000125 /* Transmit the write command. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000126 ret = mdiobus_write(bus, sw_addr, 0, 0x9400 | (addr << 5) | reg);
127 if (ret < 0)
128 return ret;
129
Barry Grussling3675c8d2013-01-08 16:05:53 +0000130 /* Wait for the write command to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000131 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
132 if (ret < 0)
133 return ret;
134
135 return 0;
136}
137
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700138/* Must be called with SMI mutex held */
139static int _mv88e6xxx_reg_write(struct dsa_switch *ds, int addr, int reg,
140 u16 val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000141{
Guenter Roeckb184e492014-10-17 12:30:58 -0700142 struct mii_bus *bus = dsa_host_dev_to_mii_bus(ds->master_dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000143
Guenter Roeckb184e492014-10-17 12:30:58 -0700144 if (bus == NULL)
145 return -EINVAL;
146
Vivien Didelotbb92ea52015-01-23 16:10:36 -0500147 dev_dbg(ds->master_dev, "-> addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
148 addr, reg, val);
149
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700150 return __mv88e6xxx_reg_write(bus, ds->pd->sw_addr, addr, reg, val);
151}
152
153int mv88e6xxx_reg_write(struct dsa_switch *ds, int addr, int reg, u16 val)
154{
155 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
156 int ret;
157
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000158 mutex_lock(&ps->smi_mutex);
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700159 ret = _mv88e6xxx_reg_write(ds, addr, reg, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000160 mutex_unlock(&ps->smi_mutex);
161
162 return ret;
163}
164
165int mv88e6xxx_config_prio(struct dsa_switch *ds)
166{
Barry Grussling3675c8d2013-01-08 16:05:53 +0000167 /* Configure the IP ToS mapping registers. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000168 REG_WRITE(REG_GLOBAL, 0x10, 0x0000);
169 REG_WRITE(REG_GLOBAL, 0x11, 0x0000);
170 REG_WRITE(REG_GLOBAL, 0x12, 0x5555);
171 REG_WRITE(REG_GLOBAL, 0x13, 0x5555);
172 REG_WRITE(REG_GLOBAL, 0x14, 0xaaaa);
173 REG_WRITE(REG_GLOBAL, 0x15, 0xaaaa);
174 REG_WRITE(REG_GLOBAL, 0x16, 0xffff);
175 REG_WRITE(REG_GLOBAL, 0x17, 0xffff);
176
Barry Grussling3675c8d2013-01-08 16:05:53 +0000177 /* Configure the IEEE 802.1p priority mapping register. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000178 REG_WRITE(REG_GLOBAL, 0x18, 0xfa41);
179
180 return 0;
181}
182
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000183int mv88e6xxx_set_addr_direct(struct dsa_switch *ds, u8 *addr)
184{
185 REG_WRITE(REG_GLOBAL, 0x01, (addr[0] << 8) | addr[1]);
186 REG_WRITE(REG_GLOBAL, 0x02, (addr[2] << 8) | addr[3]);
187 REG_WRITE(REG_GLOBAL, 0x03, (addr[4] << 8) | addr[5]);
188
189 return 0;
190}
191
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000192int mv88e6xxx_set_addr_indirect(struct dsa_switch *ds, u8 *addr)
193{
194 int i;
195 int ret;
196
197 for (i = 0; i < 6; i++) {
198 int j;
199
Barry Grussling3675c8d2013-01-08 16:05:53 +0000200 /* Write the MAC address byte. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000201 REG_WRITE(REG_GLOBAL2, 0x0d, 0x8000 | (i << 8) | addr[i]);
202
Barry Grussling3675c8d2013-01-08 16:05:53 +0000203 /* Wait for the write to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000204 for (j = 0; j < 16; j++) {
205 ret = REG_READ(REG_GLOBAL2, 0x0d);
206 if ((ret & 0x8000) == 0)
207 break;
208 }
209 if (j == 16)
210 return -ETIMEDOUT;
211 }
212
213 return 0;
214}
215
216int mv88e6xxx_phy_read(struct dsa_switch *ds, int addr, int regnum)
217{
218 if (addr >= 0)
219 return mv88e6xxx_reg_read(ds, addr, regnum);
220 return 0xffff;
221}
222
223int mv88e6xxx_phy_write(struct dsa_switch *ds, int addr, int regnum, u16 val)
224{
225 if (addr >= 0)
226 return mv88e6xxx_reg_write(ds, addr, regnum, val);
227 return 0;
228}
229
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000230#ifdef CONFIG_NET_DSA_MV88E6XXX_NEED_PPU
231static int mv88e6xxx_ppu_disable(struct dsa_switch *ds)
232{
233 int ret;
Barry Grussling19b2f972013-01-08 16:05:54 +0000234 unsigned long timeout;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000235
236 ret = REG_READ(REG_GLOBAL, 0x04);
237 REG_WRITE(REG_GLOBAL, 0x04, ret & ~0x4000);
238
Barry Grussling19b2f972013-01-08 16:05:54 +0000239 timeout = jiffies + 1 * HZ;
240 while (time_before(jiffies, timeout)) {
Barry Grussling85686582013-01-08 16:05:56 +0000241 ret = REG_READ(REG_GLOBAL, 0x00);
Barry Grussling19b2f972013-01-08 16:05:54 +0000242 usleep_range(1000, 2000);
Barry Grussling85686582013-01-08 16:05:56 +0000243 if ((ret & 0xc000) != 0xc000)
244 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000245 }
246
247 return -ETIMEDOUT;
248}
249
250static int mv88e6xxx_ppu_enable(struct dsa_switch *ds)
251{
252 int ret;
Barry Grussling19b2f972013-01-08 16:05:54 +0000253 unsigned long timeout;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000254
255 ret = REG_READ(REG_GLOBAL, 0x04);
256 REG_WRITE(REG_GLOBAL, 0x04, ret | 0x4000);
257
Barry Grussling19b2f972013-01-08 16:05:54 +0000258 timeout = jiffies + 1 * HZ;
259 while (time_before(jiffies, timeout)) {
Barry Grussling85686582013-01-08 16:05:56 +0000260 ret = REG_READ(REG_GLOBAL, 0x00);
Barry Grussling19b2f972013-01-08 16:05:54 +0000261 usleep_range(1000, 2000);
Barry Grussling85686582013-01-08 16:05:56 +0000262 if ((ret & 0xc000) == 0xc000)
263 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000264 }
265
266 return -ETIMEDOUT;
267}
268
269static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
270{
271 struct mv88e6xxx_priv_state *ps;
272
273 ps = container_of(ugly, struct mv88e6xxx_priv_state, ppu_work);
274 if (mutex_trylock(&ps->ppu_mutex)) {
Barry Grussling85686582013-01-08 16:05:56 +0000275 struct dsa_switch *ds = ((struct dsa_switch *)ps) - 1;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000276
Barry Grussling85686582013-01-08 16:05:56 +0000277 if (mv88e6xxx_ppu_enable(ds) == 0)
278 ps->ppu_disabled = 0;
279 mutex_unlock(&ps->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000280 }
281}
282
283static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
284{
285 struct mv88e6xxx_priv_state *ps = (void *)_ps;
286
287 schedule_work(&ps->ppu_work);
288}
289
290static int mv88e6xxx_ppu_access_get(struct dsa_switch *ds)
291{
Florian Fainellia22adce2014-04-28 11:14:28 -0700292 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000293 int ret;
294
295 mutex_lock(&ps->ppu_mutex);
296
Barry Grussling3675c8d2013-01-08 16:05:53 +0000297 /* If the PHY polling unit is enabled, disable it so that
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000298 * we can access the PHY registers. If it was already
299 * disabled, cancel the timer that is going to re-enable
300 * it.
301 */
302 if (!ps->ppu_disabled) {
Barry Grussling85686582013-01-08 16:05:56 +0000303 ret = mv88e6xxx_ppu_disable(ds);
304 if (ret < 0) {
305 mutex_unlock(&ps->ppu_mutex);
306 return ret;
307 }
308 ps->ppu_disabled = 1;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000309 } else {
Barry Grussling85686582013-01-08 16:05:56 +0000310 del_timer(&ps->ppu_timer);
311 ret = 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000312 }
313
314 return ret;
315}
316
317static void mv88e6xxx_ppu_access_put(struct dsa_switch *ds)
318{
Florian Fainellia22adce2014-04-28 11:14:28 -0700319 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000320
Barry Grussling3675c8d2013-01-08 16:05:53 +0000321 /* Schedule a timer to re-enable the PHY polling unit. */
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000322 mod_timer(&ps->ppu_timer, jiffies + msecs_to_jiffies(10));
323 mutex_unlock(&ps->ppu_mutex);
324}
325
326void mv88e6xxx_ppu_state_init(struct dsa_switch *ds)
327{
Florian Fainellia22adce2014-04-28 11:14:28 -0700328 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000329
330 mutex_init(&ps->ppu_mutex);
331 INIT_WORK(&ps->ppu_work, mv88e6xxx_ppu_reenable_work);
332 init_timer(&ps->ppu_timer);
333 ps->ppu_timer.data = (unsigned long)ps;
334 ps->ppu_timer.function = mv88e6xxx_ppu_reenable_timer;
335}
336
337int mv88e6xxx_phy_read_ppu(struct dsa_switch *ds, int addr, int regnum)
338{
339 int ret;
340
341 ret = mv88e6xxx_ppu_access_get(ds);
342 if (ret >= 0) {
Barry Grussling85686582013-01-08 16:05:56 +0000343 ret = mv88e6xxx_reg_read(ds, addr, regnum);
344 mv88e6xxx_ppu_access_put(ds);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000345 }
346
347 return ret;
348}
349
350int mv88e6xxx_phy_write_ppu(struct dsa_switch *ds, int addr,
351 int regnum, u16 val)
352{
353 int ret;
354
355 ret = mv88e6xxx_ppu_access_get(ds);
356 if (ret >= 0) {
Barry Grussling85686582013-01-08 16:05:56 +0000357 ret = mv88e6xxx_reg_write(ds, addr, regnum, val);
358 mv88e6xxx_ppu_access_put(ds);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000359 }
360
361 return ret;
362}
363#endif
364
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000365void mv88e6xxx_poll_link(struct dsa_switch *ds)
366{
367 int i;
368
369 for (i = 0; i < DSA_MAX_PORTS; i++) {
370 struct net_device *dev;
Ingo Molnar2a9e7972008-11-25 16:50:49 -0800371 int uninitialized_var(port_status);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000372 int link;
373 int speed;
374 int duplex;
375 int fc;
376
377 dev = ds->ports[i];
378 if (dev == NULL)
379 continue;
380
381 link = 0;
382 if (dev->flags & IFF_UP) {
383 port_status = mv88e6xxx_reg_read(ds, REG_PORT(i), 0x00);
384 if (port_status < 0)
385 continue;
386
387 link = !!(port_status & 0x0800);
388 }
389
390 if (!link) {
391 if (netif_carrier_ok(dev)) {
Barry Grusslingab381a92013-01-08 16:05:55 +0000392 netdev_info(dev, "link down\n");
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000393 netif_carrier_off(dev);
394 }
395 continue;
396 }
397
398 switch (port_status & 0x0300) {
399 case 0x0000:
400 speed = 10;
401 break;
402 case 0x0100:
403 speed = 100;
404 break;
405 case 0x0200:
406 speed = 1000;
407 break;
408 default:
409 speed = -1;
410 break;
411 }
412 duplex = (port_status & 0x0400) ? 1 : 0;
413 fc = (port_status & 0x8000) ? 1 : 0;
414
415 if (!netif_carrier_ok(dev)) {
Barry Grusslingab381a92013-01-08 16:05:55 +0000416 netdev_info(dev,
417 "link up, %d Mb/s, %s duplex, flow control %sabled\n",
418 speed,
419 duplex ? "full" : "half",
420 fc ? "en" : "dis");
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000421 netif_carrier_on(dev);
422 }
423 }
424}
425
426static int mv88e6xxx_stats_wait(struct dsa_switch *ds)
427{
428 int ret;
429 int i;
430
431 for (i = 0; i < 10; i++) {
Stephane Contri1ded3f52009-07-02 23:26:48 +0000432 ret = REG_READ(REG_GLOBAL, 0x1d);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000433 if ((ret & 0x8000) == 0)
434 return 0;
435 }
436
437 return -ETIMEDOUT;
438}
439
440static int mv88e6xxx_stats_snapshot(struct dsa_switch *ds, int port)
441{
442 int ret;
443
Barry Grussling3675c8d2013-01-08 16:05:53 +0000444 /* Snapshot the hardware statistics counters for this port. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000445 REG_WRITE(REG_GLOBAL, 0x1d, 0xdc00 | port);
446
Barry Grussling3675c8d2013-01-08 16:05:53 +0000447 /* Wait for the snapshotting to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000448 ret = mv88e6xxx_stats_wait(ds);
449 if (ret < 0)
450 return ret;
451
452 return 0;
453}
454
455static void mv88e6xxx_stats_read(struct dsa_switch *ds, int stat, u32 *val)
456{
457 u32 _val;
458 int ret;
459
460 *val = 0;
461
462 ret = mv88e6xxx_reg_write(ds, REG_GLOBAL, 0x1d, 0xcc00 | stat);
463 if (ret < 0)
464 return;
465
466 ret = mv88e6xxx_stats_wait(ds);
467 if (ret < 0)
468 return;
469
470 ret = mv88e6xxx_reg_read(ds, REG_GLOBAL, 0x1e);
471 if (ret < 0)
472 return;
473
474 _val = ret << 16;
475
476 ret = mv88e6xxx_reg_read(ds, REG_GLOBAL, 0x1f);
477 if (ret < 0)
478 return;
479
480 *val = _val | ret;
481}
482
483void mv88e6xxx_get_strings(struct dsa_switch *ds,
484 int nr_stats, struct mv88e6xxx_hw_stat *stats,
485 int port, uint8_t *data)
486{
487 int i;
488
489 for (i = 0; i < nr_stats; i++) {
490 memcpy(data + i * ETH_GSTRING_LEN,
491 stats[i].string, ETH_GSTRING_LEN);
492 }
493}
494
495void mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds,
496 int nr_stats, struct mv88e6xxx_hw_stat *stats,
497 int port, uint64_t *data)
498{
Florian Fainellia22adce2014-04-28 11:14:28 -0700499 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000500 int ret;
501 int i;
502
503 mutex_lock(&ps->stats_mutex);
504
505 ret = mv88e6xxx_stats_snapshot(ds, port);
506 if (ret < 0) {
507 mutex_unlock(&ps->stats_mutex);
508 return;
509 }
510
Barry Grussling3675c8d2013-01-08 16:05:53 +0000511 /* Read each of the counters. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000512 for (i = 0; i < nr_stats; i++) {
513 struct mv88e6xxx_hw_stat *s = stats + i;
514 u32 low;
Guenter Roeck17ee3e02014-10-29 10:45:07 -0700515 u32 high = 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000516
Guenter Roeck17ee3e02014-10-29 10:45:07 -0700517 if (s->reg >= 0x100) {
518 int ret;
519
520 ret = mv88e6xxx_reg_read(ds, REG_PORT(port),
521 s->reg - 0x100);
522 if (ret < 0)
523 goto error;
524 low = ret;
525 if (s->sizeof_stat == 4) {
526 ret = mv88e6xxx_reg_read(ds, REG_PORT(port),
527 s->reg - 0x100 + 1);
528 if (ret < 0)
529 goto error;
530 high = ret;
531 }
532 data[i] = (((u64)high) << 16) | low;
533 continue;
534 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000535 mv88e6xxx_stats_read(ds, s->reg, &low);
536 if (s->sizeof_stat == 8)
537 mv88e6xxx_stats_read(ds, s->reg + 1, &high);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000538
539 data[i] = (((u64)high) << 32) | low;
540 }
Guenter Roeck17ee3e02014-10-29 10:45:07 -0700541error:
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000542 mutex_unlock(&ps->stats_mutex);
543}
Ben Hutchings98e67302011-11-25 14:36:19 +0000544
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700545int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
546{
547 return 32 * sizeof(u16);
548}
549
550void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
551 struct ethtool_regs *regs, void *_p)
552{
553 u16 *p = _p;
554 int i;
555
556 regs->version = 0;
557
558 memset(p, 0xff, 32 * sizeof(u16));
559
560 for (i = 0; i < 32; i++) {
561 int ret;
562
563 ret = mv88e6xxx_reg_read(ds, REG_PORT(port), i);
564 if (ret >= 0)
565 p[i] = ret;
566 }
567}
568
Andrew Lunneaa23762014-11-15 22:24:51 +0100569#ifdef CONFIG_NET_DSA_HWMON
570
571int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
572{
573 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
574 int ret;
575 int val;
576
577 *temp = 0;
578
579 mutex_lock(&ps->phy_mutex);
580
581 ret = mv88e6xxx_phy_write(ds, 0x0, 0x16, 0x6);
582 if (ret < 0)
583 goto error;
584
585 /* Enable temperature sensor */
586 ret = mv88e6xxx_phy_read(ds, 0x0, 0x1a);
587 if (ret < 0)
588 goto error;
589
590 ret = mv88e6xxx_phy_write(ds, 0x0, 0x1a, ret | (1 << 5));
591 if (ret < 0)
592 goto error;
593
594 /* Wait for temperature to stabilize */
595 usleep_range(10000, 12000);
596
597 val = mv88e6xxx_phy_read(ds, 0x0, 0x1a);
598 if (val < 0) {
599 ret = val;
600 goto error;
601 }
602
603 /* Disable temperature sensor */
604 ret = mv88e6xxx_phy_write(ds, 0x0, 0x1a, ret & ~(1 << 5));
605 if (ret < 0)
606 goto error;
607
608 *temp = ((val & 0x1f) - 5) * 5;
609
610error:
611 mv88e6xxx_phy_write(ds, 0x0, 0x16, 0x0);
612 mutex_unlock(&ps->phy_mutex);
613 return ret;
614}
615#endif /* CONFIG_NET_DSA_HWMON */
616
Andrew Lunnf3044682015-02-14 19:17:50 +0100617static int mv88e6xxx_wait(struct dsa_switch *ds, int reg, int offset, u16 mask)
618{
619 unsigned long timeout = jiffies + HZ / 10;
620
621 while (time_before(jiffies, timeout)) {
622 int ret;
623
624 ret = REG_READ(reg, offset);
625 if (!(ret & mask))
626 return 0;
627
628 usleep_range(1000, 2000);
629 }
630 return -ETIMEDOUT;
631}
632
633int mv88e6xxx_phy_wait(struct dsa_switch *ds)
634{
635 return mv88e6xxx_wait(ds, REG_GLOBAL2, 0x18, 0x8000);
636}
637
638int mv88e6xxx_eeprom_load_wait(struct dsa_switch *ds)
639{
640 return mv88e6xxx_wait(ds, REG_GLOBAL2, 0x14, 0x0800);
641}
642
643int mv88e6xxx_eeprom_busy_wait(struct dsa_switch *ds)
644{
645 return mv88e6xxx_wait(ds, REG_GLOBAL2, 0x14, 0x8000);
646}
647
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700648/* Must be called with SMI lock held */
649static int _mv88e6xxx_wait(struct dsa_switch *ds, int reg, int offset, u16 mask)
650{
651 unsigned long timeout = jiffies + HZ / 10;
652
653 while (time_before(jiffies, timeout)) {
654 int ret;
655
656 ret = _mv88e6xxx_reg_read(ds, reg, offset);
657 if (ret < 0)
658 return ret;
659 if (!(ret & mask))
660 return 0;
661
662 usleep_range(1000, 2000);
663 }
664 return -ETIMEDOUT;
665}
666
667/* Must be called with SMI lock held */
668static int _mv88e6xxx_atu_wait(struct dsa_switch *ds)
669{
670 return _mv88e6xxx_wait(ds, REG_GLOBAL, 0x0b, ATU_BUSY);
671}
672
Andrew Lunnf3044682015-02-14 19:17:50 +0100673int mv88e6xxx_phy_read_indirect(struct dsa_switch *ds, int addr, int regnum)
674{
675 int ret;
676
677 REG_WRITE(REG_GLOBAL2, 0x18, 0x9800 | (addr << 5) | regnum);
678
679 ret = mv88e6xxx_phy_wait(ds);
680 if (ret < 0)
681 return ret;
682
683 return REG_READ(REG_GLOBAL2, 0x19);
684}
685
686int mv88e6xxx_phy_write_indirect(struct dsa_switch *ds, int addr, int regnum,
687 u16 val)
688{
689 REG_WRITE(REG_GLOBAL2, 0x19, val);
690 REG_WRITE(REG_GLOBAL2, 0x18, 0x9400 | (addr << 5) | regnum);
691
692 return mv88e6xxx_phy_wait(ds);
693}
694
Guenter Roeck11b3b452015-03-06 22:23:51 -0800695int mv88e6xxx_get_eee(struct dsa_switch *ds, int port, struct ethtool_eee *e)
696{
697 int reg;
698
699 reg = mv88e6xxx_phy_read_indirect(ds, port, 16);
700 if (reg < 0)
701 return -EOPNOTSUPP;
702
703 e->eee_enabled = !!(reg & 0x0200);
704 e->tx_lpi_enabled = !!(reg & 0x0100);
705
706 reg = REG_READ(REG_PORT(port), 0);
707 e->eee_active = !!(reg & 0x0040);
708
709 return 0;
710}
711
712static int mv88e6xxx_eee_enable_set(struct dsa_switch *ds, int port,
713 bool eee_enabled, bool tx_lpi_enabled)
714{
715 int reg, nreg;
716
717 reg = mv88e6xxx_phy_read_indirect(ds, port, 16);
718 if (reg < 0)
719 return reg;
720
721 nreg = reg & ~0x0300;
722 if (eee_enabled)
723 nreg |= 0x0200;
724 if (tx_lpi_enabled)
725 nreg |= 0x0100;
726
727 if (nreg != reg)
728 return mv88e6xxx_phy_write_indirect(ds, port, 16, nreg);
729
730 return 0;
731}
732
733int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
734 struct phy_device *phydev, struct ethtool_eee *e)
735{
736 int ret;
737
738 ret = mv88e6xxx_eee_enable_set(ds, port, e->eee_enabled,
739 e->tx_lpi_enabled);
740 if (ret)
741 return -EOPNOTSUPP;
742
743 return 0;
744}
745
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700746static int _mv88e6xxx_atu_cmd(struct dsa_switch *ds, int fid, u16 cmd)
747{
748 int ret;
749
750 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, 0x01, fid);
751 if (ret < 0)
752 return ret;
753
754 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, 0x0b, cmd);
755 if (ret < 0)
756 return ret;
757
758 return _mv88e6xxx_atu_wait(ds);
759}
760
761static int _mv88e6xxx_flush_fid(struct dsa_switch *ds, int fid)
762{
763 int ret;
764
765 ret = _mv88e6xxx_atu_wait(ds);
766 if (ret < 0)
767 return ret;
768
769 return _mv88e6xxx_atu_cmd(ds, fid, ATU_CMD_FLUSH_NONSTATIC_FID);
770}
771
772static int mv88e6xxx_set_port_state(struct dsa_switch *ds, int port, u8 state)
773{
774 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
775 int reg, ret;
776 u8 oldstate;
777
778 mutex_lock(&ps->smi_mutex);
779
780 reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), 0x04);
781 if (reg < 0)
782 goto abort;
783
784 oldstate = reg & PSTATE_MASK;
785 if (oldstate != state) {
786 /* Flush forwarding database if we're moving a port
787 * from Learning or Forwarding state to Disabled or
788 * Blocking or Listening state.
789 */
790 if (oldstate >= PSTATE_LEARNING && state <= PSTATE_BLOCKING) {
791 ret = _mv88e6xxx_flush_fid(ds, ps->fid[port]);
792 if (ret)
793 goto abort;
794 }
795 reg = (reg & ~PSTATE_MASK) | state;
796 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), 0x04, reg);
797 }
798
799abort:
800 mutex_unlock(&ps->smi_mutex);
801 return ret;
802}
803
804/* Must be called with smi lock held */
805static int _mv88e6xxx_update_port_config(struct dsa_switch *ds, int port)
806{
807 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
808 u8 fid = ps->fid[port];
809 u16 reg = fid << 12;
810
811 if (dsa_is_cpu_port(ds, port))
812 reg |= ds->phys_port_mask;
813 else
814 reg |= (ps->bridge_mask[fid] |
815 (1 << dsa_upstream_port(ds))) & ~(1 << port);
816
817 return _mv88e6xxx_reg_write(ds, REG_PORT(port), 0x06, reg);
818}
819
820/* Must be called with smi lock held */
821static int _mv88e6xxx_update_bridge_config(struct dsa_switch *ds, int fid)
822{
823 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
824 int port;
825 u32 mask;
826 int ret;
827
828 mask = ds->phys_port_mask;
829 while (mask) {
830 port = __ffs(mask);
831 mask &= ~(1 << port);
832 if (ps->fid[port] != fid)
833 continue;
834
835 ret = _mv88e6xxx_update_port_config(ds, port);
836 if (ret)
837 return ret;
838 }
839
840 return _mv88e6xxx_flush_fid(ds, fid);
841}
842
843/* Bridge handling functions */
844
845int mv88e6xxx_join_bridge(struct dsa_switch *ds, int port, u32 br_port_mask)
846{
847 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
848 int ret = 0;
849 u32 nmask;
850 int fid;
851
852 /* If the bridge group is not empty, join that group.
853 * Otherwise create a new group.
854 */
855 fid = ps->fid[port];
856 nmask = br_port_mask & ~(1 << port);
857 if (nmask)
858 fid = ps->fid[__ffs(nmask)];
859
860 nmask = ps->bridge_mask[fid] | (1 << port);
861 if (nmask != br_port_mask) {
862 netdev_err(ds->ports[port],
863 "join: Bridge port mask mismatch fid=%d mask=0x%x expected 0x%x\n",
864 fid, br_port_mask, nmask);
865 return -EINVAL;
866 }
867
868 mutex_lock(&ps->smi_mutex);
869
870 ps->bridge_mask[fid] = br_port_mask;
871
872 if (fid != ps->fid[port]) {
873 ps->fid_mask |= 1 << ps->fid[port];
874 ps->fid[port] = fid;
875 ret = _mv88e6xxx_update_bridge_config(ds, fid);
876 }
877
878 mutex_unlock(&ps->smi_mutex);
879
880 return ret;
881}
882
883int mv88e6xxx_leave_bridge(struct dsa_switch *ds, int port, u32 br_port_mask)
884{
885 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
886 u8 fid, newfid;
887 int ret;
888
889 fid = ps->fid[port];
890
891 if (ps->bridge_mask[fid] != br_port_mask) {
892 netdev_err(ds->ports[port],
893 "leave: Bridge port mask mismatch fid=%d mask=0x%x expected 0x%x\n",
894 fid, br_port_mask, ps->bridge_mask[fid]);
895 return -EINVAL;
896 }
897
898 /* If the port was the last port of a bridge, we are done.
899 * Otherwise assign a new fid to the port, and fix up
900 * the bridge configuration.
901 */
902 if (br_port_mask == (1 << port))
903 return 0;
904
905 mutex_lock(&ps->smi_mutex);
906
907 newfid = __ffs(ps->fid_mask);
908 ps->fid[port] = newfid;
909 ps->fid_mask &= (1 << newfid);
910 ps->bridge_mask[fid] &= ~(1 << port);
911 ps->bridge_mask[newfid] = 1 << port;
912
913 ret = _mv88e6xxx_update_bridge_config(ds, fid);
914 if (!ret)
915 ret = _mv88e6xxx_update_bridge_config(ds, newfid);
916
917 mutex_unlock(&ps->smi_mutex);
918
919 return ret;
920}
921
922int mv88e6xxx_port_stp_update(struct dsa_switch *ds, int port, u8 state)
923{
924 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
925 int stp_state;
926
927 switch (state) {
928 case BR_STATE_DISABLED:
929 stp_state = PSTATE_DISABLED;
930 break;
931 case BR_STATE_BLOCKING:
932 case BR_STATE_LISTENING:
933 stp_state = PSTATE_BLOCKING;
934 break;
935 case BR_STATE_LEARNING:
936 stp_state = PSTATE_LEARNING;
937 break;
938 case BR_STATE_FORWARDING:
939 default:
940 stp_state = PSTATE_FORWARDING;
941 break;
942 }
943
944 netdev_dbg(ds->ports[port], "port state %d [%d]\n", state, stp_state);
945
946 /* mv88e6xxx_port_stp_update may be called with softirqs disabled,
947 * so we can not update the port state directly but need to schedule it.
948 */
949 ps->port_state[port] = stp_state;
950 set_bit(port, &ps->port_state_update_mask);
951 schedule_work(&ps->bridge_work);
952
953 return 0;
954}
955
956static void mv88e6xxx_bridge_work(struct work_struct *work)
957{
958 struct mv88e6xxx_priv_state *ps;
959 struct dsa_switch *ds;
960 int port;
961
962 ps = container_of(work, struct mv88e6xxx_priv_state, bridge_work);
963 ds = ((struct dsa_switch *)ps) - 1;
964
965 while (ps->port_state_update_mask) {
966 port = __ffs(ps->port_state_update_mask);
967 clear_bit(port, &ps->port_state_update_mask);
968 mv88e6xxx_set_port_state(ds, port, ps->port_state[port]);
969 }
970}
971
Guenter Roeckd827e882015-03-26 18:36:29 -0700972int mv88e6xxx_setup_port_common(struct dsa_switch *ds, int port)
973{
974 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700975 int ret, fid;
Guenter Roeckd827e882015-03-26 18:36:29 -0700976
977 mutex_lock(&ps->smi_mutex);
978
Guenter Roeck366f0a02015-03-26 18:36:30 -0700979 /* Port Control 1: disable trunking, disable sending
980 * learning messages to this port.
Guenter Roeckd827e882015-03-26 18:36:29 -0700981 */
Guenter Roeck366f0a02015-03-26 18:36:30 -0700982 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), 0x05, 0x0000);
Guenter Roeckd827e882015-03-26 18:36:29 -0700983 if (ret)
984 goto abort;
985
986 /* Port based VLAN map: give each port its own address
987 * database, allow the CPU port to talk to each of the 'real'
988 * ports, and allow each of the 'real' ports to only talk to
989 * the upstream port.
990 */
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700991 fid = __ffs(ps->fid_mask);
992 ps->fid[port] = fid;
993 ps->fid_mask &= ~(1 << fid);
Guenter Roeckd827e882015-03-26 18:36:29 -0700994
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700995 if (!dsa_is_cpu_port(ds, port))
996 ps->bridge_mask[fid] = 1 << port;
997
998 ret = _mv88e6xxx_update_port_config(ds, port);
Guenter Roeckd827e882015-03-26 18:36:29 -0700999 if (ret)
1000 goto abort;
1001
1002 /* Default VLAN ID and priority: don't set a default VLAN
1003 * ID, and set the default packet priority to zero.
1004 */
1005 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), 0x07, 0x0000);
1006abort:
1007 mutex_unlock(&ps->smi_mutex);
1008 return ret;
1009}
1010
Guenter Roeckacdaffc2015-03-26 18:36:28 -07001011int mv88e6xxx_setup_common(struct dsa_switch *ds)
1012{
1013 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1014
1015 mutex_init(&ps->smi_mutex);
1016 mutex_init(&ps->stats_mutex);
1017 mutex_init(&ps->phy_mutex);
1018
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001019 ps->fid_mask = (1 << DSA_MAX_PORTS) - 1;
1020
1021 INIT_WORK(&ps->bridge_work, mv88e6xxx_bridge_work);
1022
Guenter Roeckacdaffc2015-03-26 18:36:28 -07001023 return 0;
1024}
1025
Ben Hutchings98e67302011-11-25 14:36:19 +00001026static int __init mv88e6xxx_init(void)
1027{
1028#if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
1029 register_switch_driver(&mv88e6131_switch_driver);
1030#endif
1031#if IS_ENABLED(CONFIG_NET_DSA_MV88E6123_61_65)
1032 register_switch_driver(&mv88e6123_61_65_switch_driver);
1033#endif
Guenter Roeck3ad50cc2014-10-29 10:44:56 -07001034#if IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
1035 register_switch_driver(&mv88e6352_switch_driver);
1036#endif
Andrew Lunn42f27252014-09-12 23:58:44 +02001037#if IS_ENABLED(CONFIG_NET_DSA_MV88E6171)
1038 register_switch_driver(&mv88e6171_switch_driver);
1039#endif
Ben Hutchings98e67302011-11-25 14:36:19 +00001040 return 0;
1041}
1042module_init(mv88e6xxx_init);
1043
1044static void __exit mv88e6xxx_cleanup(void)
1045{
Andrew Lunn42f27252014-09-12 23:58:44 +02001046#if IS_ENABLED(CONFIG_NET_DSA_MV88E6171)
1047 unregister_switch_driver(&mv88e6171_switch_driver);
1048#endif
Ben Hutchings98e67302011-11-25 14:36:19 +00001049#if IS_ENABLED(CONFIG_NET_DSA_MV88E6123_61_65)
1050 unregister_switch_driver(&mv88e6123_61_65_switch_driver);
1051#endif
1052#if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
1053 unregister_switch_driver(&mv88e6131_switch_driver);
1054#endif
1055}
1056module_exit(mv88e6xxx_cleanup);
Ben Hutchings3d825ed2011-11-25 14:37:16 +00001057
1058MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
1059MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
1060MODULE_LICENSE("GPL");