blob: 3806ff1aaa9c7683b50b7f53fc1f107d36f78ebb [file] [log] [blame]
Guenter Roeck3ad50cc2014-10-29 10:44:56 -07001/*
2 * net/dsa/mv88e6352.c - Marvell 88e6352 switch chip support
3 *
4 * Copyright (c) 2014 Guenter Roeck
5 *
6 * Derived from mv88e6123_61_65.c
7 * Copyright (c) 2008-2009 Marvell Semiconductor
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 */
14
15#include <linux/delay.h>
16#include <linux/jiffies.h>
17#include <linux/list.h>
18#include <linux/module.h>
19#include <linux/netdevice.h>
20#include <linux/platform_device.h>
21#include <linux/phy.h>
22#include <net/dsa.h>
23#include "mv88e6xxx.h"
24
Guenter Roeck3ad50cc2014-10-29 10:44:56 -070025static char *mv88e6352_probe(struct device *host_dev, int sw_addr)
26{
27 struct mii_bus *bus = dsa_host_dev_to_mii_bus(host_dev);
28 int ret;
29
30 if (bus == NULL)
31 return NULL;
32
33 ret = __mv88e6xxx_reg_read(bus, sw_addr, REG_PORT(0), 0x03);
34 if (ret >= 0) {
Guenter Roeck27167772014-10-29 10:44:57 -070035 if ((ret & 0xfff0) == 0x1760)
36 return "Marvell 88E6176";
Guenter Roeck3ad50cc2014-10-29 10:44:56 -070037 if (ret == 0x3521)
38 return "Marvell 88E6352 (A0)";
39 if (ret == 0x3522)
40 return "Marvell 88E6352 (A1)";
41 if ((ret & 0xfff0) == 0x3520)
42 return "Marvell 88E6352";
43 }
44
45 return NULL;
46}
47
Guenter Roeck3ad50cc2014-10-29 10:44:56 -070048static int mv88e6352_setup_global(struct dsa_switch *ds)
49{
Andrew Lunn44e50dd2015-04-02 04:06:33 +020050 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Guenter Roeck3ad50cc2014-10-29 10:44:56 -070051 int ret;
52 int i;
53
54 /* Discard packets with excessive collisions,
55 * mask all interrupt sources, enable PPU (bit 14, undocumented).
56 */
57 REG_WRITE(REG_GLOBAL, 0x04, 0x6000);
58
59 /* Set the default address aging time to 5 minutes, and
60 * enable address learn messages to be sent to all message
61 * ports.
62 */
63 REG_WRITE(REG_GLOBAL, 0x0a, 0x0148);
64
65 /* Configure the priority mapping registers. */
66 ret = mv88e6xxx_config_prio(ds);
67 if (ret < 0)
68 return ret;
69
70 /* Configure the upstream port, and configure the upstream
71 * port as the port to which ingress and egress monitor frames
72 * are to be sent.
73 */
74 REG_WRITE(REG_GLOBAL, 0x1a, (dsa_upstream_port(ds) * 0x1110));
75
76 /* Disable remote management for now, and set the switch's
77 * DSA device number.
78 */
79 REG_WRITE(REG_GLOBAL, 0x1c, ds->index & 0x1f);
80
81 /* Send all frames with destination addresses matching
82 * 01:80:c2:00:00:2x to the CPU port.
83 */
84 REG_WRITE(REG_GLOBAL2, 0x02, 0xffff);
85
86 /* Send all frames with destination addresses matching
87 * 01:80:c2:00:00:0x to the CPU port.
88 */
89 REG_WRITE(REG_GLOBAL2, 0x03, 0xffff);
90
91 /* Disable the loopback filter, disable flow control
92 * messages, disable flood broadcast override, disable
93 * removing of provider tags, disable ATU age violation
94 * interrupts, disable tag flow control, force flow
95 * control priority to the highest, and send all special
96 * multicast frames to the CPU at the highest priority.
97 */
98 REG_WRITE(REG_GLOBAL2, 0x05, 0x00ff);
99
100 /* Program the DSA routing table. */
101 for (i = 0; i < 32; i++) {
102 int nexthop = 0x1f;
103
104 if (i != ds->index && i < ds->dst->pd->nr_chips)
105 nexthop = ds->pd->rtable[i] & 0x1f;
106
107 REG_WRITE(REG_GLOBAL2, 0x06, 0x8000 | (i << 8) | nexthop);
108 }
109
110 /* Clear all trunk masks. */
111 for (i = 0; i < 8; i++)
112 REG_WRITE(REG_GLOBAL2, 0x07, 0x8000 | (i << 12) | 0x7f);
113
114 /* Clear all trunk mappings. */
115 for (i = 0; i < 16; i++)
116 REG_WRITE(REG_GLOBAL2, 0x08, 0x8000 | (i << 11));
117
118 /* Disable ingress rate limiting by resetting all ingress
119 * rate limit registers to their initial state.
120 */
Andrew Lunn44e50dd2015-04-02 04:06:33 +0200121 for (i = 0; i < ps->num_ports; i++)
Guenter Roeck3ad50cc2014-10-29 10:44:56 -0700122 REG_WRITE(REG_GLOBAL2, 0x09, 0x9000 | (i << 8));
123
124 /* Initialise cross-chip port VLAN table to reset defaults. */
125 REG_WRITE(REG_GLOBAL2, 0x0b, 0x9000);
126
127 /* Clear the priority override table. */
128 for (i = 0; i < 16; i++)
129 REG_WRITE(REG_GLOBAL2, 0x0f, 0x8000 | (i << 8));
130
131 /* @@@ initialise AVB (22/23) watchdog (27) sdet (29) registers */
132
133 return 0;
134}
135
136static int mv88e6352_setup_port(struct dsa_switch *ds, int p)
137{
138 int addr = REG_PORT(p);
139 u16 val;
140
141 /* MAC Forcing register: don't force link, speed, duplex
142 * or flow control state to any particular values on physical
143 * ports, but force the CPU port and all DSA ports to 1000 Mb/s
144 * full duplex.
145 */
146 if (dsa_is_cpu_port(ds, p) || ds->dsa_port_mask & (1 << p))
147 REG_WRITE(addr, 0x01, 0x003e);
148 else
149 REG_WRITE(addr, 0x01, 0x0003);
150
151 /* Do not limit the period of time that this port can be
152 * paused for by the remote end or the period of time that
153 * this port can pause the remote end.
154 */
155 REG_WRITE(addr, 0x02, 0x0000);
156
157 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
158 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
159 * tunneling, determine priority by looking at 802.1p and IP
160 * priority fields (IP prio has precedence), and set STP state
161 * to Forwarding.
162 *
163 * If this is the CPU link, use DSA or EDSA tagging depending
164 * on which tagging mode was configured.
165 *
166 * If this is a link to another switch, use DSA tagging mode.
167 *
168 * If this is the upstream port for this switch, enable
169 * forwarding of unknown unicasts and multicasts.
170 */
171 val = 0x0433;
172 if (dsa_is_cpu_port(ds, p)) {
173 if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA)
174 val |= 0x3300;
175 else
176 val |= 0x0100;
177 }
178 if (ds->dsa_port_mask & (1 << p))
179 val |= 0x0100;
180 if (p == dsa_upstream_port(ds))
181 val |= 0x000c;
182 REG_WRITE(addr, 0x04, val);
183
Guenter Roeck3ad50cc2014-10-29 10:44:56 -0700184 /* Port Control 2: don't force a good FCS, set the maximum
185 * frame size to 10240 bytes, don't let the switch add or
186 * strip 802.1q tags, don't discard tagged or untagged frames
187 * on this port, do a destination address lookup on all
188 * received packets as usual, disable ARP mirroring and don't
189 * send a copy of all transmitted/received frames on this port
190 * to the CPU.
191 */
192 REG_WRITE(addr, 0x08, 0x2080);
193
194 /* Egress rate control: disable egress rate control. */
195 REG_WRITE(addr, 0x09, 0x0001);
196
197 /* Egress rate control 2: disable egress rate control. */
198 REG_WRITE(addr, 0x0a, 0x0000);
199
200 /* Port Association Vector: when learning source addresses
201 * of packets, add the address to the address database using
202 * a port bitmap that has only the bit for this port set and
203 * the other bits clear.
204 */
205 REG_WRITE(addr, 0x0b, 1 << p);
206
207 /* Port ATU control: disable limiting the number of address
208 * database entries that this port is allowed to use.
209 */
210 REG_WRITE(addr, 0x0c, 0x0000);
211
212 /* Priority Override: disable DA, SA and VTU priority override. */
213 REG_WRITE(addr, 0x0d, 0x0000);
214
215 /* Port Ethertype: use the Ethertype DSA Ethertype value. */
216 REG_WRITE(addr, 0x0f, ETH_P_EDSA);
217
218 /* Tag Remap: use an identity 802.1p prio -> switch prio
219 * mapping.
220 */
221 REG_WRITE(addr, 0x18, 0x3210);
222
223 /* Tag Remap 2: use an identity 802.1p prio -> switch prio
224 * mapping.
225 */
226 REG_WRITE(addr, 0x19, 0x7654);
227
Guenter Roeck20890522015-03-26 18:36:32 -0700228 return mv88e6xxx_setup_port_common(ds, p);
Guenter Roeck3ad50cc2014-10-29 10:44:56 -0700229}
230
Guenter Roeck276db3b2014-10-29 10:44:59 -0700231#ifdef CONFIG_NET_DSA_HWMON
232
Guenter Roeck276db3b2014-10-29 10:44:59 -0700233static int mv88e6352_get_temp(struct dsa_switch *ds, int *temp)
234{
235 int ret;
236
237 *temp = 0;
238
Andrew Lunn491435852015-04-02 04:06:35 +0200239 ret = mv88e6xxx_phy_page_read(ds, 0, 6, 27);
Guenter Roeck276db3b2014-10-29 10:44:59 -0700240 if (ret < 0)
241 return ret;
242
243 *temp = (ret & 0xff) - 25;
244
245 return 0;
246}
247
248static int mv88e6352_get_temp_limit(struct dsa_switch *ds, int *temp)
249{
250 int ret;
251
252 *temp = 0;
253
Andrew Lunn491435852015-04-02 04:06:35 +0200254 ret = mv88e6xxx_phy_page_read(ds, 0, 6, 26);
Guenter Roeck276db3b2014-10-29 10:44:59 -0700255 if (ret < 0)
256 return ret;
257
258 *temp = (((ret >> 8) & 0x1f) * 5) - 25;
259
260 return 0;
261}
262
263static int mv88e6352_set_temp_limit(struct dsa_switch *ds, int temp)
264{
265 int ret;
266
Andrew Lunn491435852015-04-02 04:06:35 +0200267 ret = mv88e6xxx_phy_page_read(ds, 0, 6, 26);
Guenter Roeck276db3b2014-10-29 10:44:59 -0700268 if (ret < 0)
269 return ret;
270 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
Andrew Lunn491435852015-04-02 04:06:35 +0200271 return mv88e6xxx_phy_page_write(ds, 0, 6, 26,
Guenter Roeck276db3b2014-10-29 10:44:59 -0700272 (ret & 0xe0ff) | (temp << 8));
273}
274
275static int mv88e6352_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
276{
277 int ret;
278
279 *alarm = false;
280
Andrew Lunn491435852015-04-02 04:06:35 +0200281 ret = mv88e6xxx_phy_page_read(ds, 0, 6, 26);
Guenter Roeck276db3b2014-10-29 10:44:59 -0700282 if (ret < 0)
283 return ret;
284
285 *alarm = !!(ret & 0x40);
286
287 return 0;
288}
289#endif /* CONFIG_NET_DSA_HWMON */
290
Guenter Roeck3ad50cc2014-10-29 10:44:56 -0700291static int mv88e6352_setup(struct dsa_switch *ds)
292{
293 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
294 int ret;
295 int i;
296
Guenter Roeckacdaffc2015-03-26 18:36:28 -0700297 ret = mv88e6xxx_setup_common(ds);
298 if (ret < 0)
299 return ret;
300
Andrew Lunn44e50dd2015-04-02 04:06:33 +0200301 ps->num_ports = 7;
302
Guenter Roeck33b43df2014-10-29 10:45:03 -0700303 mutex_init(&ps->eeprom_mutex);
Guenter Roeck3ad50cc2014-10-29 10:44:56 -0700304
Andrew Lunn143a8302015-04-02 04:06:34 +0200305 ret = mv88e6xxx_switch_reset(ds, true);
Guenter Roeck3ad50cc2014-10-29 10:44:56 -0700306 if (ret < 0)
307 return ret;
308
309 /* @@@ initialise vtu and atu */
310
311 ret = mv88e6352_setup_global(ds);
312 if (ret < 0)
313 return ret;
314
Andrew Lunn44e50dd2015-04-02 04:06:33 +0200315 for (i = 0; i < ps->num_ports; i++) {
Guenter Roeck3ad50cc2014-10-29 10:44:56 -0700316 ret = mv88e6352_setup_port(ds, i);
317 if (ret < 0)
318 return ret;
319 }
320
321 return 0;
322}
323
Guenter Roeck3ad50cc2014-10-29 10:44:56 -0700324static struct mv88e6xxx_hw_stat mv88e6352_hw_stats[] = {
325 { "in_good_octets", 8, 0x00, },
326 { "in_bad_octets", 4, 0x02, },
327 { "in_unicast", 4, 0x04, },
328 { "in_broadcasts", 4, 0x06, },
329 { "in_multicasts", 4, 0x07, },
330 { "in_pause", 4, 0x16, },
331 { "in_undersize", 4, 0x18, },
332 { "in_fragments", 4, 0x19, },
333 { "in_oversize", 4, 0x1a, },
334 { "in_jabber", 4, 0x1b, },
335 { "in_rx_error", 4, 0x1c, },
336 { "in_fcs_error", 4, 0x1d, },
337 { "out_octets", 8, 0x0e, },
338 { "out_unicast", 4, 0x10, },
339 { "out_broadcasts", 4, 0x13, },
340 { "out_multicasts", 4, 0x12, },
341 { "out_pause", 4, 0x15, },
342 { "excessive", 4, 0x11, },
343 { "collisions", 4, 0x1e, },
344 { "deferred", 4, 0x05, },
345 { "single", 4, 0x14, },
346 { "multiple", 4, 0x17, },
347 { "out_fcs_error", 4, 0x03, },
348 { "late", 4, 0x1f, },
349 { "hist_64bytes", 4, 0x08, },
350 { "hist_65_127bytes", 4, 0x09, },
351 { "hist_128_255bytes", 4, 0x0a, },
352 { "hist_256_511bytes", 4, 0x0b, },
353 { "hist_512_1023bytes", 4, 0x0c, },
354 { "hist_1024_max_bytes", 4, 0x0d, },
Guenter Roeck17ee3e02014-10-29 10:45:07 -0700355 { "sw_in_discards", 4, 0x110, },
356 { "sw_in_filtered", 2, 0x112, },
357 { "sw_out_filtered", 2, 0x113, },
Guenter Roeck3ad50cc2014-10-29 10:44:56 -0700358};
359
Guenter Roeck33b43df2014-10-29 10:45:03 -0700360static int mv88e6352_read_eeprom_word(struct dsa_switch *ds, int addr)
361{
362 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
363 int ret;
364
365 mutex_lock(&ps->eeprom_mutex);
366
367 ret = mv88e6xxx_reg_write(ds, REG_GLOBAL2, 0x14,
368 0xc000 | (addr & 0xff));
369 if (ret < 0)
370 goto error;
371
Andrew Lunnf3044682015-02-14 19:17:50 +0100372 ret = mv88e6xxx_eeprom_busy_wait(ds);
Guenter Roeck33b43df2014-10-29 10:45:03 -0700373 if (ret < 0)
374 goto error;
375
376 ret = mv88e6xxx_reg_read(ds, REG_GLOBAL2, 0x15);
377error:
378 mutex_unlock(&ps->eeprom_mutex);
379 return ret;
380}
381
382static int mv88e6352_get_eeprom(struct dsa_switch *ds,
383 struct ethtool_eeprom *eeprom, u8 *data)
384{
385 int offset;
386 int len;
387 int ret;
388
389 offset = eeprom->offset;
390 len = eeprom->len;
391 eeprom->len = 0;
392
393 eeprom->magic = 0xc3ec4951;
394
Andrew Lunnf3044682015-02-14 19:17:50 +0100395 ret = mv88e6xxx_eeprom_load_wait(ds);
Guenter Roeck33b43df2014-10-29 10:45:03 -0700396 if (ret < 0)
397 return ret;
398
399 if (offset & 1) {
400 int word;
401
402 word = mv88e6352_read_eeprom_word(ds, offset >> 1);
403 if (word < 0)
404 return word;
405
406 *data++ = (word >> 8) & 0xff;
407
408 offset++;
409 len--;
410 eeprom->len++;
411 }
412
413 while (len >= 2) {
414 int word;
415
416 word = mv88e6352_read_eeprom_word(ds, offset >> 1);
417 if (word < 0)
418 return word;
419
420 *data++ = word & 0xff;
421 *data++ = (word >> 8) & 0xff;
422
423 offset += 2;
424 len -= 2;
425 eeprom->len += 2;
426 }
427
428 if (len) {
429 int word;
430
431 word = mv88e6352_read_eeprom_word(ds, offset >> 1);
432 if (word < 0)
433 return word;
434
435 *data++ = word & 0xff;
436
437 offset++;
438 len--;
439 eeprom->len++;
440 }
441
442 return 0;
443}
444
445static int mv88e6352_eeprom_is_readonly(struct dsa_switch *ds)
446{
447 int ret;
448
449 ret = mv88e6xxx_reg_read(ds, REG_GLOBAL2, 0x14);
450 if (ret < 0)
451 return ret;
452
453 if (!(ret & 0x0400))
454 return -EROFS;
455
456 return 0;
457}
458
459static int mv88e6352_write_eeprom_word(struct dsa_switch *ds, int addr,
460 u16 data)
461{
462 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
463 int ret;
464
465 mutex_lock(&ps->eeprom_mutex);
466
467 ret = mv88e6xxx_reg_write(ds, REG_GLOBAL2, 0x15, data);
468 if (ret < 0)
469 goto error;
470
471 ret = mv88e6xxx_reg_write(ds, REG_GLOBAL2, 0x14,
472 0xb000 | (addr & 0xff));
473 if (ret < 0)
474 goto error;
475
Andrew Lunnf3044682015-02-14 19:17:50 +0100476 ret = mv88e6xxx_eeprom_busy_wait(ds);
Guenter Roeck33b43df2014-10-29 10:45:03 -0700477error:
478 mutex_unlock(&ps->eeprom_mutex);
479 return ret;
480}
481
482static int mv88e6352_set_eeprom(struct dsa_switch *ds,
483 struct ethtool_eeprom *eeprom, u8 *data)
484{
485 int offset;
486 int ret;
487 int len;
488
489 if (eeprom->magic != 0xc3ec4951)
490 return -EINVAL;
491
492 ret = mv88e6352_eeprom_is_readonly(ds);
493 if (ret)
494 return ret;
495
496 offset = eeprom->offset;
497 len = eeprom->len;
498 eeprom->len = 0;
499
Andrew Lunnf3044682015-02-14 19:17:50 +0100500 ret = mv88e6xxx_eeprom_load_wait(ds);
Guenter Roeck33b43df2014-10-29 10:45:03 -0700501 if (ret < 0)
502 return ret;
503
504 if (offset & 1) {
505 int word;
506
507 word = mv88e6352_read_eeprom_word(ds, offset >> 1);
508 if (word < 0)
509 return word;
510
511 word = (*data++ << 8) | (word & 0xff);
512
513 ret = mv88e6352_write_eeprom_word(ds, offset >> 1, word);
514 if (ret < 0)
515 return ret;
516
517 offset++;
518 len--;
519 eeprom->len++;
520 }
521
522 while (len >= 2) {
523 int word;
524
525 word = *data++;
526 word |= *data++ << 8;
527
528 ret = mv88e6352_write_eeprom_word(ds, offset >> 1, word);
529 if (ret < 0)
530 return ret;
531
532 offset += 2;
533 len -= 2;
534 eeprom->len += 2;
535 }
536
537 if (len) {
538 int word;
539
540 word = mv88e6352_read_eeprom_word(ds, offset >> 1);
541 if (word < 0)
542 return word;
543
544 word = (word & 0xff00) | *data++;
545
546 ret = mv88e6352_write_eeprom_word(ds, offset >> 1, word);
547 if (ret < 0)
548 return ret;
549
550 offset++;
551 len--;
552 eeprom->len++;
553 }
554
555 return 0;
556}
557
Guenter Roeck3ad50cc2014-10-29 10:44:56 -0700558static void
559mv88e6352_get_strings(struct dsa_switch *ds, int port, uint8_t *data)
560{
561 mv88e6xxx_get_strings(ds, ARRAY_SIZE(mv88e6352_hw_stats),
562 mv88e6352_hw_stats, port, data);
563}
564
565static void
566mv88e6352_get_ethtool_stats(struct dsa_switch *ds, int port, uint64_t *data)
567{
568 mv88e6xxx_get_ethtool_stats(ds, ARRAY_SIZE(mv88e6352_hw_stats),
569 mv88e6352_hw_stats, port, data);
570}
571
572static int mv88e6352_get_sset_count(struct dsa_switch *ds)
573{
574 return ARRAY_SIZE(mv88e6352_hw_stats);
575}
576
577struct dsa_switch_driver mv88e6352_switch_driver = {
578 .tag_protocol = DSA_TAG_PROTO_EDSA,
579 .priv_size = sizeof(struct mv88e6xxx_priv_state),
580 .probe = mv88e6352_probe,
581 .setup = mv88e6352_setup,
582 .set_addr = mv88e6xxx_set_addr_indirect,
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +0200583 .phy_read = mv88e6xxx_phy_read_indirect,
584 .phy_write = mv88e6xxx_phy_write_indirect,
Guenter Roeck3ad50cc2014-10-29 10:44:56 -0700585 .poll_link = mv88e6xxx_poll_link,
586 .get_strings = mv88e6352_get_strings,
587 .get_ethtool_stats = mv88e6352_get_ethtool_stats,
588 .get_sset_count = mv88e6352_get_sset_count,
Guenter Roeck04b0a802015-03-06 22:23:52 -0800589 .set_eee = mv88e6xxx_set_eee,
590 .get_eee = mv88e6xxx_get_eee,
Guenter Roeck276db3b2014-10-29 10:44:59 -0700591#ifdef CONFIG_NET_DSA_HWMON
592 .get_temp = mv88e6352_get_temp,
593 .get_temp_limit = mv88e6352_get_temp_limit,
594 .set_temp_limit = mv88e6352_set_temp_limit,
595 .get_temp_alarm = mv88e6352_get_temp_alarm,
596#endif
Guenter Roeck33b43df2014-10-29 10:45:03 -0700597 .get_eeprom = mv88e6352_get_eeprom,
598 .set_eeprom = mv88e6352_set_eeprom,
Guenter Roeck95d08b52014-10-29 10:45:06 -0700599 .get_regs_len = mv88e6xxx_get_regs_len,
600 .get_regs = mv88e6xxx_get_regs,
Guenter Roeck3f244ab2015-03-26 18:36:36 -0700601 .port_join_bridge = mv88e6xxx_join_bridge,
602 .port_leave_bridge = mv88e6xxx_leave_bridge,
603 .port_stp_update = mv88e6xxx_port_stp_update,
Guenter Roeck4f431e52015-03-26 18:36:39 -0700604 .fdb_add = mv88e6xxx_port_fdb_add,
605 .fdb_del = mv88e6xxx_port_fdb_del,
606 .fdb_getnext = mv88e6xxx_port_fdb_getnext,
Guenter Roeck3ad50cc2014-10-29 10:44:56 -0700607};
608
609MODULE_ALIAS("platform:mv88e6352");