214bdbc8a5b19e96361e93bcd422a381f5ce2d89
[firefly-linux-kernel-4.4.55.git] / drivers / net / dsa / mv88e6352.c
1 /*
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
25 static 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) {
35                 if ((ret & 0xfff0) == 0x1760)
36                         return "Marvell 88E6176";
37                 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
48 static int mv88e6352_setup_global(struct dsa_switch *ds)
49 {
50         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
51         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          */
121         for (i = 0; i < ps->num_ports; i++)
122                 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
136 static 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
184         /* 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
228         return mv88e6xxx_setup_port_common(ds, p);
229 }
230
231 #ifdef CONFIG_NET_DSA_HWMON
232
233 static int mv88e6352_phy_page_read(struct dsa_switch *ds,
234                                    int port, int page, int reg)
235 {
236         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
237         int ret;
238
239         mutex_lock(&ps->phy_mutex);
240         ret = mv88e6xxx_phy_write_indirect(ds, port, 0x16, page);
241         if (ret < 0)
242                 goto error;
243         ret = mv88e6xxx_phy_read_indirect(ds, port, reg);
244 error:
245         mv88e6xxx_phy_write_indirect(ds, port, 0x16, 0x0);
246         mutex_unlock(&ps->phy_mutex);
247         return ret;
248 }
249
250 static int mv88e6352_phy_page_write(struct dsa_switch *ds,
251                                     int port, int page, int reg, int val)
252 {
253         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
254         int ret;
255
256         mutex_lock(&ps->phy_mutex);
257         ret = mv88e6xxx_phy_write_indirect(ds, port, 0x16, page);
258         if (ret < 0)
259                 goto error;
260
261         ret = mv88e6xxx_phy_write_indirect(ds, port, reg, val);
262 error:
263         mv88e6xxx_phy_write_indirect(ds, port, 0x16, 0x0);
264         mutex_unlock(&ps->phy_mutex);
265         return ret;
266 }
267
268 static int mv88e6352_get_temp(struct dsa_switch *ds, int *temp)
269 {
270         int ret;
271
272         *temp = 0;
273
274         ret = mv88e6352_phy_page_read(ds, 0, 6, 27);
275         if (ret < 0)
276                 return ret;
277
278         *temp = (ret & 0xff) - 25;
279
280         return 0;
281 }
282
283 static int mv88e6352_get_temp_limit(struct dsa_switch *ds, int *temp)
284 {
285         int ret;
286
287         *temp = 0;
288
289         ret = mv88e6352_phy_page_read(ds, 0, 6, 26);
290         if (ret < 0)
291                 return ret;
292
293         *temp = (((ret >> 8) & 0x1f) * 5) - 25;
294
295         return 0;
296 }
297
298 static int mv88e6352_set_temp_limit(struct dsa_switch *ds, int temp)
299 {
300         int ret;
301
302         ret = mv88e6352_phy_page_read(ds, 0, 6, 26);
303         if (ret < 0)
304                 return ret;
305         temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
306         return mv88e6352_phy_page_write(ds, 0, 6, 26,
307                                         (ret & 0xe0ff) | (temp << 8));
308 }
309
310 static int mv88e6352_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
311 {
312         int ret;
313
314         *alarm = false;
315
316         ret = mv88e6352_phy_page_read(ds, 0, 6, 26);
317         if (ret < 0)
318                 return ret;
319
320         *alarm = !!(ret & 0x40);
321
322         return 0;
323 }
324 #endif /* CONFIG_NET_DSA_HWMON */
325
326 static int mv88e6352_setup(struct dsa_switch *ds)
327 {
328         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
329         int ret;
330         int i;
331
332         ret = mv88e6xxx_setup_common(ds);
333         if (ret < 0)
334                 return ret;
335
336         ps->num_ports = 7;
337
338         mutex_init(&ps->eeprom_mutex);
339
340         ret = mv88e6xxx_switch_reset(ds, true);
341         if (ret < 0)
342                 return ret;
343
344         /* @@@ initialise vtu and atu */
345
346         ret = mv88e6352_setup_global(ds);
347         if (ret < 0)
348                 return ret;
349
350         for (i = 0; i < ps->num_ports; i++) {
351                 ret = mv88e6352_setup_port(ds, i);
352                 if (ret < 0)
353                         return ret;
354         }
355
356         return 0;
357 }
358
359 static int mv88e6352_port_to_phy_addr(int port)
360 {
361         if (port >= 0 && port <= 4)
362                 return port;
363         return -EINVAL;
364 }
365
366 static int
367 mv88e6352_phy_read(struct dsa_switch *ds, int port, int regnum)
368 {
369         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
370         int addr = mv88e6352_port_to_phy_addr(port);
371         int ret;
372
373         if (addr < 0)
374                 return addr;
375
376         mutex_lock(&ps->phy_mutex);
377         ret = mv88e6xxx_phy_read_indirect(ds, addr, regnum);
378         mutex_unlock(&ps->phy_mutex);
379
380         return ret;
381 }
382
383 static int
384 mv88e6352_phy_write(struct dsa_switch *ds, int port, int regnum, u16 val)
385 {
386         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
387         int addr = mv88e6352_port_to_phy_addr(port);
388         int ret;
389
390         if (addr < 0)
391                 return addr;
392
393         mutex_lock(&ps->phy_mutex);
394         ret = mv88e6xxx_phy_write_indirect(ds, addr, regnum, val);
395         mutex_unlock(&ps->phy_mutex);
396
397         return ret;
398 }
399
400 static struct mv88e6xxx_hw_stat mv88e6352_hw_stats[] = {
401         { "in_good_octets", 8, 0x00, },
402         { "in_bad_octets", 4, 0x02, },
403         { "in_unicast", 4, 0x04, },
404         { "in_broadcasts", 4, 0x06, },
405         { "in_multicasts", 4, 0x07, },
406         { "in_pause", 4, 0x16, },
407         { "in_undersize", 4, 0x18, },
408         { "in_fragments", 4, 0x19, },
409         { "in_oversize", 4, 0x1a, },
410         { "in_jabber", 4, 0x1b, },
411         { "in_rx_error", 4, 0x1c, },
412         { "in_fcs_error", 4, 0x1d, },
413         { "out_octets", 8, 0x0e, },
414         { "out_unicast", 4, 0x10, },
415         { "out_broadcasts", 4, 0x13, },
416         { "out_multicasts", 4, 0x12, },
417         { "out_pause", 4, 0x15, },
418         { "excessive", 4, 0x11, },
419         { "collisions", 4, 0x1e, },
420         { "deferred", 4, 0x05, },
421         { "single", 4, 0x14, },
422         { "multiple", 4, 0x17, },
423         { "out_fcs_error", 4, 0x03, },
424         { "late", 4, 0x1f, },
425         { "hist_64bytes", 4, 0x08, },
426         { "hist_65_127bytes", 4, 0x09, },
427         { "hist_128_255bytes", 4, 0x0a, },
428         { "hist_256_511bytes", 4, 0x0b, },
429         { "hist_512_1023bytes", 4, 0x0c, },
430         { "hist_1024_max_bytes", 4, 0x0d, },
431         { "sw_in_discards", 4, 0x110, },
432         { "sw_in_filtered", 2, 0x112, },
433         { "sw_out_filtered", 2, 0x113, },
434 };
435
436 static int mv88e6352_read_eeprom_word(struct dsa_switch *ds, int addr)
437 {
438         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
439         int ret;
440
441         mutex_lock(&ps->eeprom_mutex);
442
443         ret = mv88e6xxx_reg_write(ds, REG_GLOBAL2, 0x14,
444                                   0xc000 | (addr & 0xff));
445         if (ret < 0)
446                 goto error;
447
448         ret = mv88e6xxx_eeprom_busy_wait(ds);
449         if (ret < 0)
450                 goto error;
451
452         ret = mv88e6xxx_reg_read(ds, REG_GLOBAL2, 0x15);
453 error:
454         mutex_unlock(&ps->eeprom_mutex);
455         return ret;
456 }
457
458 static int mv88e6352_get_eeprom(struct dsa_switch *ds,
459                                 struct ethtool_eeprom *eeprom, u8 *data)
460 {
461         int offset;
462         int len;
463         int ret;
464
465         offset = eeprom->offset;
466         len = eeprom->len;
467         eeprom->len = 0;
468
469         eeprom->magic = 0xc3ec4951;
470
471         ret = mv88e6xxx_eeprom_load_wait(ds);
472         if (ret < 0)
473                 return ret;
474
475         if (offset & 1) {
476                 int word;
477
478                 word = mv88e6352_read_eeprom_word(ds, offset >> 1);
479                 if (word < 0)
480                         return word;
481
482                 *data++ = (word >> 8) & 0xff;
483
484                 offset++;
485                 len--;
486                 eeprom->len++;
487         }
488
489         while (len >= 2) {
490                 int word;
491
492                 word = mv88e6352_read_eeprom_word(ds, offset >> 1);
493                 if (word < 0)
494                         return word;
495
496                 *data++ = word & 0xff;
497                 *data++ = (word >> 8) & 0xff;
498
499                 offset += 2;
500                 len -= 2;
501                 eeprom->len += 2;
502         }
503
504         if (len) {
505                 int word;
506
507                 word = mv88e6352_read_eeprom_word(ds, offset >> 1);
508                 if (word < 0)
509                         return word;
510
511                 *data++ = word & 0xff;
512
513                 offset++;
514                 len--;
515                 eeprom->len++;
516         }
517
518         return 0;
519 }
520
521 static int mv88e6352_eeprom_is_readonly(struct dsa_switch *ds)
522 {
523         int ret;
524
525         ret = mv88e6xxx_reg_read(ds, REG_GLOBAL2, 0x14);
526         if (ret < 0)
527                 return ret;
528
529         if (!(ret & 0x0400))
530                 return -EROFS;
531
532         return 0;
533 }
534
535 static int mv88e6352_write_eeprom_word(struct dsa_switch *ds, int addr,
536                                        u16 data)
537 {
538         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
539         int ret;
540
541         mutex_lock(&ps->eeprom_mutex);
542
543         ret = mv88e6xxx_reg_write(ds, REG_GLOBAL2, 0x15, data);
544         if (ret < 0)
545                 goto error;
546
547         ret = mv88e6xxx_reg_write(ds, REG_GLOBAL2, 0x14,
548                                   0xb000 | (addr & 0xff));
549         if (ret < 0)
550                 goto error;
551
552         ret = mv88e6xxx_eeprom_busy_wait(ds);
553 error:
554         mutex_unlock(&ps->eeprom_mutex);
555         return ret;
556 }
557
558 static int mv88e6352_set_eeprom(struct dsa_switch *ds,
559                                 struct ethtool_eeprom *eeprom, u8 *data)
560 {
561         int offset;
562         int ret;
563         int len;
564
565         if (eeprom->magic != 0xc3ec4951)
566                 return -EINVAL;
567
568         ret = mv88e6352_eeprom_is_readonly(ds);
569         if (ret)
570                 return ret;
571
572         offset = eeprom->offset;
573         len = eeprom->len;
574         eeprom->len = 0;
575
576         ret = mv88e6xxx_eeprom_load_wait(ds);
577         if (ret < 0)
578                 return ret;
579
580         if (offset & 1) {
581                 int word;
582
583                 word = mv88e6352_read_eeprom_word(ds, offset >> 1);
584                 if (word < 0)
585                         return word;
586
587                 word = (*data++ << 8) | (word & 0xff);
588
589                 ret = mv88e6352_write_eeprom_word(ds, offset >> 1, word);
590                 if (ret < 0)
591                         return ret;
592
593                 offset++;
594                 len--;
595                 eeprom->len++;
596         }
597
598         while (len >= 2) {
599                 int word;
600
601                 word = *data++;
602                 word |= *data++ << 8;
603
604                 ret = mv88e6352_write_eeprom_word(ds, offset >> 1, word);
605                 if (ret < 0)
606                         return ret;
607
608                 offset += 2;
609                 len -= 2;
610                 eeprom->len += 2;
611         }
612
613         if (len) {
614                 int word;
615
616                 word = mv88e6352_read_eeprom_word(ds, offset >> 1);
617                 if (word < 0)
618                         return word;
619
620                 word = (word & 0xff00) | *data++;
621
622                 ret = mv88e6352_write_eeprom_word(ds, offset >> 1, word);
623                 if (ret < 0)
624                         return ret;
625
626                 offset++;
627                 len--;
628                 eeprom->len++;
629         }
630
631         return 0;
632 }
633
634 static void
635 mv88e6352_get_strings(struct dsa_switch *ds, int port, uint8_t *data)
636 {
637         mv88e6xxx_get_strings(ds, ARRAY_SIZE(mv88e6352_hw_stats),
638                               mv88e6352_hw_stats, port, data);
639 }
640
641 static void
642 mv88e6352_get_ethtool_stats(struct dsa_switch *ds, int port, uint64_t *data)
643 {
644         mv88e6xxx_get_ethtool_stats(ds, ARRAY_SIZE(mv88e6352_hw_stats),
645                                     mv88e6352_hw_stats, port, data);
646 }
647
648 static int mv88e6352_get_sset_count(struct dsa_switch *ds)
649 {
650         return ARRAY_SIZE(mv88e6352_hw_stats);
651 }
652
653 struct dsa_switch_driver mv88e6352_switch_driver = {
654         .tag_protocol           = DSA_TAG_PROTO_EDSA,
655         .priv_size              = sizeof(struct mv88e6xxx_priv_state),
656         .probe                  = mv88e6352_probe,
657         .setup                  = mv88e6352_setup,
658         .set_addr               = mv88e6xxx_set_addr_indirect,
659         .phy_read               = mv88e6352_phy_read,
660         .phy_write              = mv88e6352_phy_write,
661         .poll_link              = mv88e6xxx_poll_link,
662         .get_strings            = mv88e6352_get_strings,
663         .get_ethtool_stats      = mv88e6352_get_ethtool_stats,
664         .get_sset_count         = mv88e6352_get_sset_count,
665         .set_eee                = mv88e6xxx_set_eee,
666         .get_eee                = mv88e6xxx_get_eee,
667 #ifdef CONFIG_NET_DSA_HWMON
668         .get_temp               = mv88e6352_get_temp,
669         .get_temp_limit         = mv88e6352_get_temp_limit,
670         .set_temp_limit         = mv88e6352_set_temp_limit,
671         .get_temp_alarm         = mv88e6352_get_temp_alarm,
672 #endif
673         .get_eeprom             = mv88e6352_get_eeprom,
674         .set_eeprom             = mv88e6352_set_eeprom,
675         .get_regs_len           = mv88e6xxx_get_regs_len,
676         .get_regs               = mv88e6xxx_get_regs,
677         .port_join_bridge       = mv88e6xxx_join_bridge,
678         .port_leave_bridge      = mv88e6xxx_leave_bridge,
679         .port_stp_update        = mv88e6xxx_port_stp_update,
680         .fdb_add                = mv88e6xxx_port_fdb_add,
681         .fdb_del                = mv88e6xxx_port_fdb_del,
682         .fdb_getnext            = mv88e6xxx_port_fdb_getnext,
683 };
684
685 MODULE_ALIAS("platform:mv88e6352");