blob: d436668cb51528027fd59675803265594cfb6c7c [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
11#include <linux/list.h>
Paul Gortmaker2bbba272012-01-24 10:41:40 +000012#include <linux/module.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000013#include <linux/netdevice.h>
14#include <linux/phy.h>
Ben Hutchingsc8f0b862011-11-27 17:06:08 +000015#include <net/dsa.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000016#include "mv88e6xxx.h"
17
Barry Grussling3675c8d2013-01-08 16:05:53 +000018/* If the switch's ADDR[4:0] strap pins are strapped to zero, it will
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000019 * use all 32 SMI bus addresses on its SMI bus, and all switch registers
20 * will be directly accessible on some {device address,register address}
21 * pair. If the ADDR[4:0] pins are not strapped to zero, the switch
22 * will only respond to SMI transactions to that specific address, and
23 * an indirect addressing mechanism needs to be used to access its
24 * registers.
25 */
26static int mv88e6xxx_reg_wait_ready(struct mii_bus *bus, int sw_addr)
27{
28 int ret;
29 int i;
30
31 for (i = 0; i < 16; i++) {
32 ret = mdiobus_read(bus, sw_addr, 0);
33 if (ret < 0)
34 return ret;
35
36 if ((ret & 0x8000) == 0)
37 return 0;
38 }
39
40 return -ETIMEDOUT;
41}
42
43int __mv88e6xxx_reg_read(struct mii_bus *bus, int sw_addr, int addr, int reg)
44{
45 int ret;
46
47 if (sw_addr == 0)
48 return mdiobus_read(bus, addr, reg);
49
Barry Grussling3675c8d2013-01-08 16:05:53 +000050 /* Wait for the bus to become free. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000051 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
52 if (ret < 0)
53 return ret;
54
Barry Grussling3675c8d2013-01-08 16:05:53 +000055 /* Transmit the read command. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000056 ret = mdiobus_write(bus, sw_addr, 0, 0x9800 | (addr << 5) | reg);
57 if (ret < 0)
58 return ret;
59
Barry Grussling3675c8d2013-01-08 16:05:53 +000060 /* Wait for the read command to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000061 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
62 if (ret < 0)
63 return ret;
64
Barry Grussling3675c8d2013-01-08 16:05:53 +000065 /* Read the data. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000066 ret = mdiobus_read(bus, sw_addr, 1);
67 if (ret < 0)
68 return ret;
69
70 return ret & 0xffff;
71}
72
73int mv88e6xxx_reg_read(struct dsa_switch *ds, int addr, int reg)
74{
75 struct mv88e6xxx_priv_state *ps = (void *)(ds + 1);
76 int ret;
77
78 mutex_lock(&ps->smi_mutex);
79 ret = __mv88e6xxx_reg_read(ds->master_mii_bus,
80 ds->pd->sw_addr, addr, reg);
81 mutex_unlock(&ps->smi_mutex);
82
83 return ret;
84}
85
86int __mv88e6xxx_reg_write(struct mii_bus *bus, int sw_addr, int addr,
87 int reg, u16 val)
88{
89 int ret;
90
91 if (sw_addr == 0)
92 return mdiobus_write(bus, addr, reg, val);
93
Barry Grussling3675c8d2013-01-08 16:05:53 +000094 /* Wait for the bus to become free. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000095 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
96 if (ret < 0)
97 return ret;
98
Barry Grussling3675c8d2013-01-08 16:05:53 +000099 /* Transmit the data to write. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000100 ret = mdiobus_write(bus, sw_addr, 1, val);
101 if (ret < 0)
102 return ret;
103
Barry Grussling3675c8d2013-01-08 16:05:53 +0000104 /* Transmit the write command. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000105 ret = mdiobus_write(bus, sw_addr, 0, 0x9400 | (addr << 5) | reg);
106 if (ret < 0)
107 return ret;
108
Barry Grussling3675c8d2013-01-08 16:05:53 +0000109 /* Wait for the write command to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000110 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
111 if (ret < 0)
112 return ret;
113
114 return 0;
115}
116
117int mv88e6xxx_reg_write(struct dsa_switch *ds, int addr, int reg, u16 val)
118{
119 struct mv88e6xxx_priv_state *ps = (void *)(ds + 1);
120 int ret;
121
122 mutex_lock(&ps->smi_mutex);
123 ret = __mv88e6xxx_reg_write(ds->master_mii_bus,
124 ds->pd->sw_addr, addr, reg, val);
125 mutex_unlock(&ps->smi_mutex);
126
127 return ret;
128}
129
130int mv88e6xxx_config_prio(struct dsa_switch *ds)
131{
Barry Grussling3675c8d2013-01-08 16:05:53 +0000132 /* Configure the IP ToS mapping registers. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000133 REG_WRITE(REG_GLOBAL, 0x10, 0x0000);
134 REG_WRITE(REG_GLOBAL, 0x11, 0x0000);
135 REG_WRITE(REG_GLOBAL, 0x12, 0x5555);
136 REG_WRITE(REG_GLOBAL, 0x13, 0x5555);
137 REG_WRITE(REG_GLOBAL, 0x14, 0xaaaa);
138 REG_WRITE(REG_GLOBAL, 0x15, 0xaaaa);
139 REG_WRITE(REG_GLOBAL, 0x16, 0xffff);
140 REG_WRITE(REG_GLOBAL, 0x17, 0xffff);
141
Barry Grussling3675c8d2013-01-08 16:05:53 +0000142 /* Configure the IEEE 802.1p priority mapping register. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000143 REG_WRITE(REG_GLOBAL, 0x18, 0xfa41);
144
145 return 0;
146}
147
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000148int mv88e6xxx_set_addr_direct(struct dsa_switch *ds, u8 *addr)
149{
150 REG_WRITE(REG_GLOBAL, 0x01, (addr[0] << 8) | addr[1]);
151 REG_WRITE(REG_GLOBAL, 0x02, (addr[2] << 8) | addr[3]);
152 REG_WRITE(REG_GLOBAL, 0x03, (addr[4] << 8) | addr[5]);
153
154 return 0;
155}
156
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000157int mv88e6xxx_set_addr_indirect(struct dsa_switch *ds, u8 *addr)
158{
159 int i;
160 int ret;
161
162 for (i = 0; i < 6; i++) {
163 int j;
164
Barry Grussling3675c8d2013-01-08 16:05:53 +0000165 /* Write the MAC address byte. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000166 REG_WRITE(REG_GLOBAL2, 0x0d, 0x8000 | (i << 8) | addr[i]);
167
Barry Grussling3675c8d2013-01-08 16:05:53 +0000168 /* Wait for the write to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000169 for (j = 0; j < 16; j++) {
170 ret = REG_READ(REG_GLOBAL2, 0x0d);
171 if ((ret & 0x8000) == 0)
172 break;
173 }
174 if (j == 16)
175 return -ETIMEDOUT;
176 }
177
178 return 0;
179}
180
181int mv88e6xxx_phy_read(struct dsa_switch *ds, int addr, int regnum)
182{
183 if (addr >= 0)
184 return mv88e6xxx_reg_read(ds, addr, regnum);
185 return 0xffff;
186}
187
188int mv88e6xxx_phy_write(struct dsa_switch *ds, int addr, int regnum, u16 val)
189{
190 if (addr >= 0)
191 return mv88e6xxx_reg_write(ds, addr, regnum, val);
192 return 0;
193}
194
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000195#ifdef CONFIG_NET_DSA_MV88E6XXX_NEED_PPU
196static int mv88e6xxx_ppu_disable(struct dsa_switch *ds)
197{
198 int ret;
199 int i;
200
201 ret = REG_READ(REG_GLOBAL, 0x04);
202 REG_WRITE(REG_GLOBAL, 0x04, ret & ~0x4000);
203
204 for (i = 0; i < 1000; i++) {
205 ret = REG_READ(REG_GLOBAL, 0x00);
206 msleep(1);
207 if ((ret & 0xc000) != 0xc000)
208 return 0;
209 }
210
211 return -ETIMEDOUT;
212}
213
214static int mv88e6xxx_ppu_enable(struct dsa_switch *ds)
215{
216 int ret;
217 int i;
218
219 ret = REG_READ(REG_GLOBAL, 0x04);
220 REG_WRITE(REG_GLOBAL, 0x04, ret | 0x4000);
221
222 for (i = 0; i < 1000; i++) {
223 ret = REG_READ(REG_GLOBAL, 0x00);
224 msleep(1);
225 if ((ret & 0xc000) == 0xc000)
226 return 0;
227 }
228
229 return -ETIMEDOUT;
230}
231
232static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
233{
234 struct mv88e6xxx_priv_state *ps;
235
236 ps = container_of(ugly, struct mv88e6xxx_priv_state, ppu_work);
237 if (mutex_trylock(&ps->ppu_mutex)) {
238 struct dsa_switch *ds = ((struct dsa_switch *)ps) - 1;
239
240 if (mv88e6xxx_ppu_enable(ds) == 0)
241 ps->ppu_disabled = 0;
242 mutex_unlock(&ps->ppu_mutex);
243 }
244}
245
246static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
247{
248 struct mv88e6xxx_priv_state *ps = (void *)_ps;
249
250 schedule_work(&ps->ppu_work);
251}
252
253static int mv88e6xxx_ppu_access_get(struct dsa_switch *ds)
254{
255 struct mv88e6xxx_priv_state *ps = (void *)(ds + 1);
256 int ret;
257
258 mutex_lock(&ps->ppu_mutex);
259
Barry Grussling3675c8d2013-01-08 16:05:53 +0000260 /* If the PHY polling unit is enabled, disable it so that
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000261 * we can access the PHY registers. If it was already
262 * disabled, cancel the timer that is going to re-enable
263 * it.
264 */
265 if (!ps->ppu_disabled) {
266 ret = mv88e6xxx_ppu_disable(ds);
267 if (ret < 0) {
268 mutex_unlock(&ps->ppu_mutex);
269 return ret;
270 }
271 ps->ppu_disabled = 1;
272 } else {
273 del_timer(&ps->ppu_timer);
274 ret = 0;
275 }
276
277 return ret;
278}
279
280static void mv88e6xxx_ppu_access_put(struct dsa_switch *ds)
281{
282 struct mv88e6xxx_priv_state *ps = (void *)(ds + 1);
283
Barry Grussling3675c8d2013-01-08 16:05:53 +0000284 /* Schedule a timer to re-enable the PHY polling unit. */
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000285 mod_timer(&ps->ppu_timer, jiffies + msecs_to_jiffies(10));
286 mutex_unlock(&ps->ppu_mutex);
287}
288
289void mv88e6xxx_ppu_state_init(struct dsa_switch *ds)
290{
291 struct mv88e6xxx_priv_state *ps = (void *)(ds + 1);
292
293 mutex_init(&ps->ppu_mutex);
294 INIT_WORK(&ps->ppu_work, mv88e6xxx_ppu_reenable_work);
295 init_timer(&ps->ppu_timer);
296 ps->ppu_timer.data = (unsigned long)ps;
297 ps->ppu_timer.function = mv88e6xxx_ppu_reenable_timer;
298}
299
300int mv88e6xxx_phy_read_ppu(struct dsa_switch *ds, int addr, int regnum)
301{
302 int ret;
303
304 ret = mv88e6xxx_ppu_access_get(ds);
305 if (ret >= 0) {
306 ret = mv88e6xxx_reg_read(ds, addr, regnum);
307 mv88e6xxx_ppu_access_put(ds);
308 }
309
310 return ret;
311}
312
313int mv88e6xxx_phy_write_ppu(struct dsa_switch *ds, int addr,
314 int regnum, u16 val)
315{
316 int ret;
317
318 ret = mv88e6xxx_ppu_access_get(ds);
319 if (ret >= 0) {
320 ret = mv88e6xxx_reg_write(ds, addr, regnum, val);
321 mv88e6xxx_ppu_access_put(ds);
322 }
323
324 return ret;
325}
326#endif
327
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000328void mv88e6xxx_poll_link(struct dsa_switch *ds)
329{
330 int i;
331
332 for (i = 0; i < DSA_MAX_PORTS; i++) {
333 struct net_device *dev;
Ingo Molnar2a9e7972008-11-25 16:50:49 -0800334 int uninitialized_var(port_status);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000335 int link;
336 int speed;
337 int duplex;
338 int fc;
339
340 dev = ds->ports[i];
341 if (dev == NULL)
342 continue;
343
344 link = 0;
345 if (dev->flags & IFF_UP) {
346 port_status = mv88e6xxx_reg_read(ds, REG_PORT(i), 0x00);
347 if (port_status < 0)
348 continue;
349
350 link = !!(port_status & 0x0800);
351 }
352
353 if (!link) {
354 if (netif_carrier_ok(dev)) {
355 printk(KERN_INFO "%s: link down\n", dev->name);
356 netif_carrier_off(dev);
357 }
358 continue;
359 }
360
361 switch (port_status & 0x0300) {
362 case 0x0000:
363 speed = 10;
364 break;
365 case 0x0100:
366 speed = 100;
367 break;
368 case 0x0200:
369 speed = 1000;
370 break;
371 default:
372 speed = -1;
373 break;
374 }
375 duplex = (port_status & 0x0400) ? 1 : 0;
376 fc = (port_status & 0x8000) ? 1 : 0;
377
378 if (!netif_carrier_ok(dev)) {
379 printk(KERN_INFO "%s: link up, %d Mb/s, %s duplex, "
380 "flow control %sabled\n", dev->name,
381 speed, duplex ? "full" : "half",
382 fc ? "en" : "dis");
383 netif_carrier_on(dev);
384 }
385 }
386}
387
388static int mv88e6xxx_stats_wait(struct dsa_switch *ds)
389{
390 int ret;
391 int i;
392
393 for (i = 0; i < 10; i++) {
Stephane Contri1ded3f52009-07-02 23:26:48 +0000394 ret = REG_READ(REG_GLOBAL, 0x1d);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000395 if ((ret & 0x8000) == 0)
396 return 0;
397 }
398
399 return -ETIMEDOUT;
400}
401
402static int mv88e6xxx_stats_snapshot(struct dsa_switch *ds, int port)
403{
404 int ret;
405
Barry Grussling3675c8d2013-01-08 16:05:53 +0000406 /* Snapshot the hardware statistics counters for this port. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000407 REG_WRITE(REG_GLOBAL, 0x1d, 0xdc00 | port);
408
Barry Grussling3675c8d2013-01-08 16:05:53 +0000409 /* Wait for the snapshotting to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000410 ret = mv88e6xxx_stats_wait(ds);
411 if (ret < 0)
412 return ret;
413
414 return 0;
415}
416
417static void mv88e6xxx_stats_read(struct dsa_switch *ds, int stat, u32 *val)
418{
419 u32 _val;
420 int ret;
421
422 *val = 0;
423
424 ret = mv88e6xxx_reg_write(ds, REG_GLOBAL, 0x1d, 0xcc00 | stat);
425 if (ret < 0)
426 return;
427
428 ret = mv88e6xxx_stats_wait(ds);
429 if (ret < 0)
430 return;
431
432 ret = mv88e6xxx_reg_read(ds, REG_GLOBAL, 0x1e);
433 if (ret < 0)
434 return;
435
436 _val = ret << 16;
437
438 ret = mv88e6xxx_reg_read(ds, REG_GLOBAL, 0x1f);
439 if (ret < 0)
440 return;
441
442 *val = _val | ret;
443}
444
445void mv88e6xxx_get_strings(struct dsa_switch *ds,
446 int nr_stats, struct mv88e6xxx_hw_stat *stats,
447 int port, uint8_t *data)
448{
449 int i;
450
451 for (i = 0; i < nr_stats; i++) {
452 memcpy(data + i * ETH_GSTRING_LEN,
453 stats[i].string, ETH_GSTRING_LEN);
454 }
455}
456
457void mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds,
458 int nr_stats, struct mv88e6xxx_hw_stat *stats,
459 int port, uint64_t *data)
460{
461 struct mv88e6xxx_priv_state *ps = (void *)(ds + 1);
462 int ret;
463 int i;
464
465 mutex_lock(&ps->stats_mutex);
466
467 ret = mv88e6xxx_stats_snapshot(ds, port);
468 if (ret < 0) {
469 mutex_unlock(&ps->stats_mutex);
470 return;
471 }
472
Barry Grussling3675c8d2013-01-08 16:05:53 +0000473 /* Read each of the counters. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000474 for (i = 0; i < nr_stats; i++) {
475 struct mv88e6xxx_hw_stat *s = stats + i;
476 u32 low;
477 u32 high;
478
479 mv88e6xxx_stats_read(ds, s->reg, &low);
480 if (s->sizeof_stat == 8)
481 mv88e6xxx_stats_read(ds, s->reg + 1, &high);
482 else
483 high = 0;
484
485 data[i] = (((u64)high) << 32) | low;
486 }
487
488 mutex_unlock(&ps->stats_mutex);
489}
Ben Hutchings98e67302011-11-25 14:36:19 +0000490
491static int __init mv88e6xxx_init(void)
492{
493#if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
494 register_switch_driver(&mv88e6131_switch_driver);
495#endif
496#if IS_ENABLED(CONFIG_NET_DSA_MV88E6123_61_65)
497 register_switch_driver(&mv88e6123_61_65_switch_driver);
498#endif
499 return 0;
500}
501module_init(mv88e6xxx_init);
502
503static void __exit mv88e6xxx_cleanup(void)
504{
505#if IS_ENABLED(CONFIG_NET_DSA_MV88E6123_61_65)
506 unregister_switch_driver(&mv88e6123_61_65_switch_driver);
507#endif
508#if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
509 unregister_switch_driver(&mv88e6131_switch_driver);
510#endif
511}
512module_exit(mv88e6xxx_cleanup);
Ben Hutchings3d825ed2011-11-25 14:37:16 +0000513
514MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
515MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
516MODULE_LICENSE("GPL");