blob: afe7d80001e2b974a4b678bd4e3e32de99bce2d3 [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>
12#include <linux/jiffies.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000013#include <linux/list.h>
Paul Gortmaker2bbba272012-01-24 10:41:40 +000014#include <linux/module.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000015#include <linux/netdevice.h>
16#include <linux/phy.h>
Ben Hutchingsc8f0b862011-11-27 17:06:08 +000017#include <net/dsa.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000018#include "mv88e6xxx.h"
19
Barry Grussling3675c8d2013-01-08 16:05:53 +000020/* If the switch's ADDR[4:0] strap pins are strapped to zero, it will
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000021 * use all 32 SMI bus addresses on its SMI bus, and all switch registers
22 * will be directly accessible on some {device address,register address}
23 * pair. If the ADDR[4:0] pins are not strapped to zero, the switch
24 * will only respond to SMI transactions to that specific address, and
25 * an indirect addressing mechanism needs to be used to access its
26 * registers.
27 */
28static int mv88e6xxx_reg_wait_ready(struct mii_bus *bus, int sw_addr)
29{
30 int ret;
31 int i;
32
33 for (i = 0; i < 16; i++) {
34 ret = mdiobus_read(bus, sw_addr, 0);
35 if (ret < 0)
36 return ret;
37
38 if ((ret & 0x8000) == 0)
39 return 0;
40 }
41
42 return -ETIMEDOUT;
43}
44
45int __mv88e6xxx_reg_read(struct mii_bus *bus, int sw_addr, int addr, int reg)
46{
47 int ret;
48
49 if (sw_addr == 0)
50 return mdiobus_read(bus, addr, reg);
51
Barry Grussling3675c8d2013-01-08 16:05:53 +000052 /* Wait for the bus to become free. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000053 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
54 if (ret < 0)
55 return ret;
56
Barry Grussling3675c8d2013-01-08 16:05:53 +000057 /* Transmit the read command. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000058 ret = mdiobus_write(bus, sw_addr, 0, 0x9800 | (addr << 5) | reg);
59 if (ret < 0)
60 return ret;
61
Barry Grussling3675c8d2013-01-08 16:05:53 +000062 /* Wait for the read command to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000063 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
64 if (ret < 0)
65 return ret;
66
Barry Grussling3675c8d2013-01-08 16:05:53 +000067 /* Read the data. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000068 ret = mdiobus_read(bus, sw_addr, 1);
69 if (ret < 0)
70 return ret;
71
72 return ret & 0xffff;
73}
74
75int mv88e6xxx_reg_read(struct dsa_switch *ds, int addr, int reg)
76{
77 struct mv88e6xxx_priv_state *ps = (void *)(ds + 1);
78 int ret;
79
80 mutex_lock(&ps->smi_mutex);
81 ret = __mv88e6xxx_reg_read(ds->master_mii_bus,
82 ds->pd->sw_addr, addr, reg);
83 mutex_unlock(&ps->smi_mutex);
84
85 return ret;
86}
87
88int __mv88e6xxx_reg_write(struct mii_bus *bus, int sw_addr, int addr,
89 int reg, u16 val)
90{
91 int ret;
92
93 if (sw_addr == 0)
94 return mdiobus_write(bus, addr, reg, val);
95
Barry Grussling3675c8d2013-01-08 16:05:53 +000096 /* Wait for the bus to become free. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000097 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
98 if (ret < 0)
99 return ret;
100
Barry Grussling3675c8d2013-01-08 16:05:53 +0000101 /* Transmit the data to write. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000102 ret = mdiobus_write(bus, sw_addr, 1, val);
103 if (ret < 0)
104 return ret;
105
Barry Grussling3675c8d2013-01-08 16:05:53 +0000106 /* Transmit the write command. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000107 ret = mdiobus_write(bus, sw_addr, 0, 0x9400 | (addr << 5) | reg);
108 if (ret < 0)
109 return ret;
110
Barry Grussling3675c8d2013-01-08 16:05:53 +0000111 /* Wait for the write command to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000112 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
113 if (ret < 0)
114 return ret;
115
116 return 0;
117}
118
119int mv88e6xxx_reg_write(struct dsa_switch *ds, int addr, int reg, u16 val)
120{
121 struct mv88e6xxx_priv_state *ps = (void *)(ds + 1);
122 int ret;
123
124 mutex_lock(&ps->smi_mutex);
125 ret = __mv88e6xxx_reg_write(ds->master_mii_bus,
126 ds->pd->sw_addr, addr, reg, val);
127 mutex_unlock(&ps->smi_mutex);
128
129 return ret;
130}
131
132int mv88e6xxx_config_prio(struct dsa_switch *ds)
133{
Barry Grussling3675c8d2013-01-08 16:05:53 +0000134 /* Configure the IP ToS mapping registers. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000135 REG_WRITE(REG_GLOBAL, 0x10, 0x0000);
136 REG_WRITE(REG_GLOBAL, 0x11, 0x0000);
137 REG_WRITE(REG_GLOBAL, 0x12, 0x5555);
138 REG_WRITE(REG_GLOBAL, 0x13, 0x5555);
139 REG_WRITE(REG_GLOBAL, 0x14, 0xaaaa);
140 REG_WRITE(REG_GLOBAL, 0x15, 0xaaaa);
141 REG_WRITE(REG_GLOBAL, 0x16, 0xffff);
142 REG_WRITE(REG_GLOBAL, 0x17, 0xffff);
143
Barry Grussling3675c8d2013-01-08 16:05:53 +0000144 /* Configure the IEEE 802.1p priority mapping register. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000145 REG_WRITE(REG_GLOBAL, 0x18, 0xfa41);
146
147 return 0;
148}
149
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000150int mv88e6xxx_set_addr_direct(struct dsa_switch *ds, u8 *addr)
151{
152 REG_WRITE(REG_GLOBAL, 0x01, (addr[0] << 8) | addr[1]);
153 REG_WRITE(REG_GLOBAL, 0x02, (addr[2] << 8) | addr[3]);
154 REG_WRITE(REG_GLOBAL, 0x03, (addr[4] << 8) | addr[5]);
155
156 return 0;
157}
158
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000159int mv88e6xxx_set_addr_indirect(struct dsa_switch *ds, u8 *addr)
160{
161 int i;
162 int ret;
163
164 for (i = 0; i < 6; i++) {
165 int j;
166
Barry Grussling3675c8d2013-01-08 16:05:53 +0000167 /* Write the MAC address byte. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000168 REG_WRITE(REG_GLOBAL2, 0x0d, 0x8000 | (i << 8) | addr[i]);
169
Barry Grussling3675c8d2013-01-08 16:05:53 +0000170 /* Wait for the write to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000171 for (j = 0; j < 16; j++) {
172 ret = REG_READ(REG_GLOBAL2, 0x0d);
173 if ((ret & 0x8000) == 0)
174 break;
175 }
176 if (j == 16)
177 return -ETIMEDOUT;
178 }
179
180 return 0;
181}
182
183int mv88e6xxx_phy_read(struct dsa_switch *ds, int addr, int regnum)
184{
185 if (addr >= 0)
186 return mv88e6xxx_reg_read(ds, addr, regnum);
187 return 0xffff;
188}
189
190int mv88e6xxx_phy_write(struct dsa_switch *ds, int addr, int regnum, u16 val)
191{
192 if (addr >= 0)
193 return mv88e6xxx_reg_write(ds, addr, regnum, val);
194 return 0;
195}
196
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000197#ifdef CONFIG_NET_DSA_MV88E6XXX_NEED_PPU
198static int mv88e6xxx_ppu_disable(struct dsa_switch *ds)
199{
200 int ret;
Barry Grussling19b2f972013-01-08 16:05:54 +0000201 unsigned long timeout;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000202
203 ret = REG_READ(REG_GLOBAL, 0x04);
204 REG_WRITE(REG_GLOBAL, 0x04, ret & ~0x4000);
205
Barry Grussling19b2f972013-01-08 16:05:54 +0000206 timeout = jiffies + 1 * HZ;
207 while (time_before(jiffies, timeout)) {
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000208 ret = REG_READ(REG_GLOBAL, 0x00);
Barry Grussling19b2f972013-01-08 16:05:54 +0000209 usleep_range(1000, 2000);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000210 if ((ret & 0xc000) != 0xc000)
211 return 0;
212 }
213
214 return -ETIMEDOUT;
215}
216
217static int mv88e6xxx_ppu_enable(struct dsa_switch *ds)
218{
219 int ret;
Barry Grussling19b2f972013-01-08 16:05:54 +0000220 unsigned long timeout;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000221
222 ret = REG_READ(REG_GLOBAL, 0x04);
223 REG_WRITE(REG_GLOBAL, 0x04, ret | 0x4000);
224
Barry Grussling19b2f972013-01-08 16:05:54 +0000225 timeout = jiffies + 1 * HZ;
226 while (time_before(jiffies, timeout)) {
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000227 ret = REG_READ(REG_GLOBAL, 0x00);
Barry Grussling19b2f972013-01-08 16:05:54 +0000228 usleep_range(1000, 2000);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000229 if ((ret & 0xc000) == 0xc000)
230 return 0;
231 }
232
233 return -ETIMEDOUT;
234}
235
236static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
237{
238 struct mv88e6xxx_priv_state *ps;
239
240 ps = container_of(ugly, struct mv88e6xxx_priv_state, ppu_work);
241 if (mutex_trylock(&ps->ppu_mutex)) {
242 struct dsa_switch *ds = ((struct dsa_switch *)ps) - 1;
243
244 if (mv88e6xxx_ppu_enable(ds) == 0)
245 ps->ppu_disabled = 0;
246 mutex_unlock(&ps->ppu_mutex);
247 }
248}
249
250static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
251{
252 struct mv88e6xxx_priv_state *ps = (void *)_ps;
253
254 schedule_work(&ps->ppu_work);
255}
256
257static int mv88e6xxx_ppu_access_get(struct dsa_switch *ds)
258{
259 struct mv88e6xxx_priv_state *ps = (void *)(ds + 1);
260 int ret;
261
262 mutex_lock(&ps->ppu_mutex);
263
Barry Grussling3675c8d2013-01-08 16:05:53 +0000264 /* If the PHY polling unit is enabled, disable it so that
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000265 * we can access the PHY registers. If it was already
266 * disabled, cancel the timer that is going to re-enable
267 * it.
268 */
269 if (!ps->ppu_disabled) {
270 ret = mv88e6xxx_ppu_disable(ds);
271 if (ret < 0) {
272 mutex_unlock(&ps->ppu_mutex);
273 return ret;
274 }
275 ps->ppu_disabled = 1;
276 } else {
277 del_timer(&ps->ppu_timer);
278 ret = 0;
279 }
280
281 return ret;
282}
283
284static void mv88e6xxx_ppu_access_put(struct dsa_switch *ds)
285{
286 struct mv88e6xxx_priv_state *ps = (void *)(ds + 1);
287
Barry Grussling3675c8d2013-01-08 16:05:53 +0000288 /* Schedule a timer to re-enable the PHY polling unit. */
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000289 mod_timer(&ps->ppu_timer, jiffies + msecs_to_jiffies(10));
290 mutex_unlock(&ps->ppu_mutex);
291}
292
293void mv88e6xxx_ppu_state_init(struct dsa_switch *ds)
294{
295 struct mv88e6xxx_priv_state *ps = (void *)(ds + 1);
296
297 mutex_init(&ps->ppu_mutex);
298 INIT_WORK(&ps->ppu_work, mv88e6xxx_ppu_reenable_work);
299 init_timer(&ps->ppu_timer);
300 ps->ppu_timer.data = (unsigned long)ps;
301 ps->ppu_timer.function = mv88e6xxx_ppu_reenable_timer;
302}
303
304int mv88e6xxx_phy_read_ppu(struct dsa_switch *ds, int addr, int regnum)
305{
306 int ret;
307
308 ret = mv88e6xxx_ppu_access_get(ds);
309 if (ret >= 0) {
310 ret = mv88e6xxx_reg_read(ds, addr, regnum);
311 mv88e6xxx_ppu_access_put(ds);
312 }
313
314 return ret;
315}
316
317int mv88e6xxx_phy_write_ppu(struct dsa_switch *ds, int addr,
318 int regnum, u16 val)
319{
320 int ret;
321
322 ret = mv88e6xxx_ppu_access_get(ds);
323 if (ret >= 0) {
324 ret = mv88e6xxx_reg_write(ds, addr, regnum, val);
325 mv88e6xxx_ppu_access_put(ds);
326 }
327
328 return ret;
329}
330#endif
331
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000332void mv88e6xxx_poll_link(struct dsa_switch *ds)
333{
334 int i;
335
336 for (i = 0; i < DSA_MAX_PORTS; i++) {
337 struct net_device *dev;
Ingo Molnar2a9e7972008-11-25 16:50:49 -0800338 int uninitialized_var(port_status);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000339 int link;
340 int speed;
341 int duplex;
342 int fc;
343
344 dev = ds->ports[i];
345 if (dev == NULL)
346 continue;
347
348 link = 0;
349 if (dev->flags & IFF_UP) {
350 port_status = mv88e6xxx_reg_read(ds, REG_PORT(i), 0x00);
351 if (port_status < 0)
352 continue;
353
354 link = !!(port_status & 0x0800);
355 }
356
357 if (!link) {
358 if (netif_carrier_ok(dev)) {
359 printk(KERN_INFO "%s: link down\n", dev->name);
360 netif_carrier_off(dev);
361 }
362 continue;
363 }
364
365 switch (port_status & 0x0300) {
366 case 0x0000:
367 speed = 10;
368 break;
369 case 0x0100:
370 speed = 100;
371 break;
372 case 0x0200:
373 speed = 1000;
374 break;
375 default:
376 speed = -1;
377 break;
378 }
379 duplex = (port_status & 0x0400) ? 1 : 0;
380 fc = (port_status & 0x8000) ? 1 : 0;
381
382 if (!netif_carrier_ok(dev)) {
383 printk(KERN_INFO "%s: link up, %d Mb/s, %s duplex, "
384 "flow control %sabled\n", dev->name,
385 speed, duplex ? "full" : "half",
386 fc ? "en" : "dis");
387 netif_carrier_on(dev);
388 }
389 }
390}
391
392static int mv88e6xxx_stats_wait(struct dsa_switch *ds)
393{
394 int ret;
395 int i;
396
397 for (i = 0; i < 10; i++) {
Stephane Contri1ded3f52009-07-02 23:26:48 +0000398 ret = REG_READ(REG_GLOBAL, 0x1d);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000399 if ((ret & 0x8000) == 0)
400 return 0;
401 }
402
403 return -ETIMEDOUT;
404}
405
406static int mv88e6xxx_stats_snapshot(struct dsa_switch *ds, int port)
407{
408 int ret;
409
Barry Grussling3675c8d2013-01-08 16:05:53 +0000410 /* Snapshot the hardware statistics counters for this port. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000411 REG_WRITE(REG_GLOBAL, 0x1d, 0xdc00 | port);
412
Barry Grussling3675c8d2013-01-08 16:05:53 +0000413 /* Wait for the snapshotting to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000414 ret = mv88e6xxx_stats_wait(ds);
415 if (ret < 0)
416 return ret;
417
418 return 0;
419}
420
421static void mv88e6xxx_stats_read(struct dsa_switch *ds, int stat, u32 *val)
422{
423 u32 _val;
424 int ret;
425
426 *val = 0;
427
428 ret = mv88e6xxx_reg_write(ds, REG_GLOBAL, 0x1d, 0xcc00 | stat);
429 if (ret < 0)
430 return;
431
432 ret = mv88e6xxx_stats_wait(ds);
433 if (ret < 0)
434 return;
435
436 ret = mv88e6xxx_reg_read(ds, REG_GLOBAL, 0x1e);
437 if (ret < 0)
438 return;
439
440 _val = ret << 16;
441
442 ret = mv88e6xxx_reg_read(ds, REG_GLOBAL, 0x1f);
443 if (ret < 0)
444 return;
445
446 *val = _val | ret;
447}
448
449void mv88e6xxx_get_strings(struct dsa_switch *ds,
450 int nr_stats, struct mv88e6xxx_hw_stat *stats,
451 int port, uint8_t *data)
452{
453 int i;
454
455 for (i = 0; i < nr_stats; i++) {
456 memcpy(data + i * ETH_GSTRING_LEN,
457 stats[i].string, ETH_GSTRING_LEN);
458 }
459}
460
461void mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds,
462 int nr_stats, struct mv88e6xxx_hw_stat *stats,
463 int port, uint64_t *data)
464{
465 struct mv88e6xxx_priv_state *ps = (void *)(ds + 1);
466 int ret;
467 int i;
468
469 mutex_lock(&ps->stats_mutex);
470
471 ret = mv88e6xxx_stats_snapshot(ds, port);
472 if (ret < 0) {
473 mutex_unlock(&ps->stats_mutex);
474 return;
475 }
476
Barry Grussling3675c8d2013-01-08 16:05:53 +0000477 /* Read each of the counters. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000478 for (i = 0; i < nr_stats; i++) {
479 struct mv88e6xxx_hw_stat *s = stats + i;
480 u32 low;
481 u32 high;
482
483 mv88e6xxx_stats_read(ds, s->reg, &low);
484 if (s->sizeof_stat == 8)
485 mv88e6xxx_stats_read(ds, s->reg + 1, &high);
486 else
487 high = 0;
488
489 data[i] = (((u64)high) << 32) | low;
490 }
491
492 mutex_unlock(&ps->stats_mutex);
493}
Ben Hutchings98e67302011-11-25 14:36:19 +0000494
495static int __init mv88e6xxx_init(void)
496{
497#if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
498 register_switch_driver(&mv88e6131_switch_driver);
499#endif
500#if IS_ENABLED(CONFIG_NET_DSA_MV88E6123_61_65)
501 register_switch_driver(&mv88e6123_61_65_switch_driver);
502#endif
503 return 0;
504}
505module_init(mv88e6xxx_init);
506
507static void __exit mv88e6xxx_cleanup(void)
508{
509#if IS_ENABLED(CONFIG_NET_DSA_MV88E6123_61_65)
510 unregister_switch_driver(&mv88e6123_61_65_switch_driver);
511#endif
512#if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
513 unregister_switch_driver(&mv88e6131_switch_driver);
514#endif
515}
516module_exit(mv88e6xxx_cleanup);
Ben Hutchings3d825ed2011-11-25 14:37:16 +0000517
518MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
519MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
520MODULE_LICENSE("GPL");