net: dsa: mv88e6xxx: Factor out common initialization code
[firefly-linux-kernel-4.4.55.git] / drivers / net / dsa / mv88e6171.c
1 /* net/dsa/mv88e6171.c - Marvell 88e6171/8826172 switch chip support
2  * Copyright (c) 2008-2009 Marvell Semiconductor
3  * Copyright (c) 2014 Claudio Leite <leitec@staticky.com>
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/delay.h>
12 #include <linux/jiffies.h>
13 #include <linux/list.h>
14 #include <linux/module.h>
15 #include <linux/netdevice.h>
16 #include <linux/phy.h>
17 #include <net/dsa.h>
18 #include "mv88e6xxx.h"
19
20 static char *mv88e6171_probe(struct device *host_dev, int sw_addr)
21 {
22         struct mii_bus *bus = dsa_host_dev_to_mii_bus(host_dev);
23         int ret;
24
25         if (bus == NULL)
26                 return NULL;
27
28         ret = __mv88e6xxx_reg_read(bus, sw_addr, REG_PORT(0), 0x03);
29         if (ret >= 0) {
30                 if ((ret & 0xfff0) == 0x1710)
31                         return "Marvell 88E6171";
32                 if ((ret & 0xfff0) == 0x1720)
33                         return "Marvell 88E6172";
34         }
35
36         return NULL;
37 }
38
39 static int mv88e6171_switch_reset(struct dsa_switch *ds)
40 {
41         int i;
42         int ret;
43         unsigned long timeout;
44
45         /* Set all ports to the disabled state. */
46         for (i = 0; i < 8; i++) {
47                 ret = REG_READ(REG_PORT(i), 0x04);
48                 REG_WRITE(REG_PORT(i), 0x04, ret & 0xfffc);
49         }
50
51         /* Wait for transmit queues to drain. */
52         usleep_range(2000, 4000);
53
54         /* Reset the switch. Keep PPU active.  The PPU needs to be
55          * active to support indirect phy register accesses through
56          * global registers 0x18 and 0x19.
57          */
58         REG_WRITE(REG_GLOBAL, 0x04, 0xc000);
59
60         /* Wait up to one second for reset to complete. */
61         timeout = jiffies + 1 * HZ;
62         while (time_before(jiffies, timeout)) {
63                 ret = REG_READ(REG_GLOBAL, 0x00);
64                 if ((ret & 0xc800) == 0xc800)
65                         break;
66
67                 usleep_range(1000, 2000);
68         }
69         if (time_after(jiffies, timeout))
70                 return -ETIMEDOUT;
71
72         /* Enable ports not under DSA, e.g. WAN port */
73         for (i = 0; i < 8; i++) {
74                 if (dsa_is_cpu_port(ds, i) || ds->phys_port_mask & (1 << i))
75                         continue;
76
77                 ret = REG_READ(REG_PORT(i), 0x04);
78                 REG_WRITE(REG_PORT(i), 0x04, ret | 0x03);
79         }
80
81         return 0;
82 }
83
84 static int mv88e6171_setup_global(struct dsa_switch *ds)
85 {
86         int ret;
87         int i;
88
89         /* Discard packets with excessive collisions, mask all
90          * interrupt sources, enable PPU.
91          */
92         REG_WRITE(REG_GLOBAL, 0x04, 0x6000);
93
94         /* Set the default address aging time to 5 minutes, and
95          * enable address learn messages to be sent to all message
96          * ports.
97          */
98         REG_WRITE(REG_GLOBAL, 0x0a, 0x0148);
99
100         /* Configure the priority mapping registers. */
101         ret = mv88e6xxx_config_prio(ds);
102         if (ret < 0)
103                 return ret;
104
105         /* Configure the upstream port, and configure the upstream
106          * port as the port to which ingress and egress monitor frames
107          * are to be sent.
108          */
109         if (REG_READ(REG_PORT(0), 0x03) == 0x1710)
110                 REG_WRITE(REG_GLOBAL, 0x1a, (dsa_upstream_port(ds) * 0x1111));
111         else
112                 REG_WRITE(REG_GLOBAL, 0x1a, (dsa_upstream_port(ds) * 0x1110));
113
114         /* Disable remote management for now, and set the switch's
115          * DSA device number.
116          */
117         REG_WRITE(REG_GLOBAL, 0x1c, ds->index & 0x1f);
118
119         /* Send all frames with destination addresses matching
120          * 01:80:c2:00:00:2x to the CPU port.
121          */
122         REG_WRITE(REG_GLOBAL2, 0x02, 0xffff);
123
124         /* Send all frames with destination addresses matching
125          * 01:80:c2:00:00:0x to the CPU port.
126          */
127         REG_WRITE(REG_GLOBAL2, 0x03, 0xffff);
128
129         /* Disable the loopback filter, disable flow control
130          * messages, disable flood broadcast override, disable
131          * removing of provider tags, disable ATU age violation
132          * interrupts, disable tag flow control, force flow
133          * control priority to the highest, and send all special
134          * multicast frames to the CPU at the highest priority.
135          */
136         REG_WRITE(REG_GLOBAL2, 0x05, 0x00ff);
137
138         /* Program the DSA routing table. */
139         for (i = 0; i < 32; i++) {
140                 int nexthop;
141
142                 nexthop = 0x1f;
143                 if (i != ds->index && i < ds->dst->pd->nr_chips)
144                         nexthop = ds->pd->rtable[i] & 0x1f;
145
146                 REG_WRITE(REG_GLOBAL2, 0x06, 0x8000 | (i << 8) | nexthop);
147         }
148
149         /* Clear all trunk masks. */
150         for (i = 0; i < 8; i++)
151                 REG_WRITE(REG_GLOBAL2, 0x07, 0x8000 | (i << 12) | 0xff);
152
153         /* Clear all trunk mappings. */
154         for (i = 0; i < 16; i++)
155                 REG_WRITE(REG_GLOBAL2, 0x08, 0x8000 | (i << 11));
156
157         /* Disable ingress rate limiting by resetting all ingress
158          * rate limit registers to their initial state.
159          */
160         for (i = 0; i < 6; i++)
161                 REG_WRITE(REG_GLOBAL2, 0x09, 0x9000 | (i << 8));
162
163         /* Initialise cross-chip port VLAN table to reset defaults. */
164         REG_WRITE(REG_GLOBAL2, 0x0b, 0x9000);
165
166         /* Clear the priority override table. */
167         for (i = 0; i < 16; i++)
168                 REG_WRITE(REG_GLOBAL2, 0x0f, 0x8000 | (i << 8));
169
170         /* @@@ initialise AVB (22/23) watchdog (27) sdet (29) registers */
171
172         return 0;
173 }
174
175 static int mv88e6171_setup_port(struct dsa_switch *ds, int p)
176 {
177         int addr = REG_PORT(p);
178         u16 val;
179
180         /* MAC Forcing register: don't force link, speed, duplex
181          * or flow control state to any particular values on physical
182          * ports, but force the CPU port and all DSA ports to 1000 Mb/s
183          * full duplex.
184          */
185         val = REG_READ(addr, 0x01);
186         if (dsa_is_cpu_port(ds, p) || ds->dsa_port_mask & (1 << p))
187                 REG_WRITE(addr, 0x01, val | 0x003e);
188         else
189                 REG_WRITE(addr, 0x01, val | 0x0003);
190
191         /* Do not limit the period of time that this port can be
192          * paused for by the remote end or the period of time that
193          * this port can pause the remote end.
194          */
195         REG_WRITE(addr, 0x02, 0x0000);
196
197         /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
198          * disable Header mode, enable IGMP/MLD snooping, disable VLAN
199          * tunneling, determine priority by looking at 802.1p and IP
200          * priority fields (IP prio has precedence), and set STP state
201          * to Forwarding.
202          *
203          * If this is the CPU link, use DSA or EDSA tagging depending
204          * on which tagging mode was configured.
205          *
206          * If this is a link to another switch, use DSA tagging mode.
207          *
208          * If this is the upstream port for this switch, enable
209          * forwarding of unknown unicasts and multicasts.
210          */
211         val = 0x0433;
212         if (dsa_is_cpu_port(ds, p)) {
213                 if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA)
214                         val |= 0x3300;
215                 else
216                         val |= 0x0100;
217         }
218         if (ds->dsa_port_mask & (1 << p))
219                 val |= 0x0100;
220         if (p == dsa_upstream_port(ds))
221                 val |= 0x000c;
222         REG_WRITE(addr, 0x04, val);
223
224         /* Port Control 1: disable trunking.  Also, if this is the
225          * CPU port, enable learn messages to be sent to this port.
226          */
227         REG_WRITE(addr, 0x05, dsa_is_cpu_port(ds, p) ? 0x8000 : 0x0000);
228
229         /* Port based VLAN map: give each port its own address
230          * database, allow the CPU port to talk to each of the 'real'
231          * ports, and allow each of the 'real' ports to only talk to
232          * the upstream port.
233          */
234         val = (p & 0xf) << 12;
235         if (dsa_is_cpu_port(ds, p))
236                 val |= ds->phys_port_mask;
237         else
238                 val |= 1 << dsa_upstream_port(ds);
239         REG_WRITE(addr, 0x06, val);
240
241         /* Default VLAN ID and priority: don't set a default VLAN
242          * ID, and set the default packet priority to zero.
243          */
244         REG_WRITE(addr, 0x07, 0x0000);
245
246         /* Port Control 2: don't force a good FCS, set the maximum
247          * frame size to 10240 bytes, don't let the switch add or
248          * strip 802.1q tags, don't discard tagged or untagged frames
249          * on this port, do a destination address lookup on all
250          * received packets as usual, disable ARP mirroring and don't
251          * send a copy of all transmitted/received frames on this port
252          * to the CPU.
253          */
254         REG_WRITE(addr, 0x08, 0x2080);
255
256         /* Egress rate control: disable egress rate control. */
257         REG_WRITE(addr, 0x09, 0x0001);
258
259         /* Egress rate control 2: disable egress rate control. */
260         REG_WRITE(addr, 0x0a, 0x0000);
261
262         /* Port Association Vector: when learning source addresses
263          * of packets, add the address to the address database using
264          * a port bitmap that has only the bit for this port set and
265          * the other bits clear.
266          */
267         REG_WRITE(addr, 0x0b, 1 << p);
268
269         /* Port ATU control: disable limiting the number of address
270          * database entries that this port is allowed to use.
271          */
272         REG_WRITE(addr, 0x0c, 0x0000);
273
274         /* Priority Override: disable DA, SA and VTU priority override. */
275         REG_WRITE(addr, 0x0d, 0x0000);
276
277         /* Port Ethertype: use the Ethertype DSA Ethertype value. */
278         REG_WRITE(addr, 0x0f, ETH_P_EDSA);
279
280         /* Tag Remap: use an identity 802.1p prio -> switch prio
281          * mapping.
282          */
283         REG_WRITE(addr, 0x18, 0x3210);
284
285         /* Tag Remap 2: use an identity 802.1p prio -> switch prio
286          * mapping.
287          */
288         REG_WRITE(addr, 0x19, 0x7654);
289
290         return 0;
291 }
292
293 static int mv88e6171_setup(struct dsa_switch *ds)
294 {
295         int i;
296         int ret;
297
298         ret = mv88e6xxx_setup_common(ds);
299         if (ret < 0)
300                 return ret;
301
302         ret = mv88e6171_switch_reset(ds);
303         if (ret < 0)
304                 return ret;
305
306         /* @@@ initialise vtu and atu */
307
308         ret = mv88e6171_setup_global(ds);
309         if (ret < 0)
310                 return ret;
311
312         for (i = 0; i < 8; i++) {
313                 if (!(dsa_is_cpu_port(ds, i) || ds->phys_port_mask & (1 << i)))
314                         continue;
315
316                 ret = mv88e6171_setup_port(ds, i);
317                 if (ret < 0)
318                         return ret;
319         }
320
321         return 0;
322 }
323
324 static int mv88e6171_port_to_phy_addr(int port)
325 {
326         if (port >= 0 && port <= 4)
327                 return port;
328         return -1;
329 }
330
331 static int
332 mv88e6171_phy_read(struct dsa_switch *ds, int port, int regnum)
333 {
334         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
335         int addr = mv88e6171_port_to_phy_addr(port);
336         int ret;
337
338         mutex_lock(&ps->phy_mutex);
339         ret = mv88e6xxx_phy_read_indirect(ds, addr, regnum);
340         mutex_unlock(&ps->phy_mutex);
341         return ret;
342 }
343
344 static int
345 mv88e6171_phy_write(struct dsa_switch *ds,
346                     int port, int regnum, u16 val)
347 {
348         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
349         int addr = mv88e6171_port_to_phy_addr(port);
350         int ret;
351
352         mutex_lock(&ps->phy_mutex);
353         ret = mv88e6xxx_phy_write_indirect(ds, addr, regnum, val);
354         mutex_unlock(&ps->phy_mutex);
355         return ret;
356 }
357
358 static struct mv88e6xxx_hw_stat mv88e6171_hw_stats[] = {
359         { "in_good_octets", 8, 0x00, },
360         { "in_bad_octets", 4, 0x02, },
361         { "in_unicast", 4, 0x04, },
362         { "in_broadcasts", 4, 0x06, },
363         { "in_multicasts", 4, 0x07, },
364         { "in_pause", 4, 0x16, },
365         { "in_undersize", 4, 0x18, },
366         { "in_fragments", 4, 0x19, },
367         { "in_oversize", 4, 0x1a, },
368         { "in_jabber", 4, 0x1b, },
369         { "in_rx_error", 4, 0x1c, },
370         { "in_fcs_error", 4, 0x1d, },
371         { "out_octets", 8, 0x0e, },
372         { "out_unicast", 4, 0x10, },
373         { "out_broadcasts", 4, 0x13, },
374         { "out_multicasts", 4, 0x12, },
375         { "out_pause", 4, 0x15, },
376         { "excessive", 4, 0x11, },
377         { "collisions", 4, 0x1e, },
378         { "deferred", 4, 0x05, },
379         { "single", 4, 0x14, },
380         { "multiple", 4, 0x17, },
381         { "out_fcs_error", 4, 0x03, },
382         { "late", 4, 0x1f, },
383         { "hist_64bytes", 4, 0x08, },
384         { "hist_65_127bytes", 4, 0x09, },
385         { "hist_128_255bytes", 4, 0x0a, },
386         { "hist_256_511bytes", 4, 0x0b, },
387         { "hist_512_1023bytes", 4, 0x0c, },
388         { "hist_1024_max_bytes", 4, 0x0d, },
389 };
390
391 static void
392 mv88e6171_get_strings(struct dsa_switch *ds, int port, uint8_t *data)
393 {
394         mv88e6xxx_get_strings(ds, ARRAY_SIZE(mv88e6171_hw_stats),
395                               mv88e6171_hw_stats, port, data);
396 }
397
398 static void
399 mv88e6171_get_ethtool_stats(struct dsa_switch *ds,
400                             int port, uint64_t *data)
401 {
402         mv88e6xxx_get_ethtool_stats(ds, ARRAY_SIZE(mv88e6171_hw_stats),
403                                     mv88e6171_hw_stats, port, data);
404 }
405
406 static int mv88e6171_get_sset_count(struct dsa_switch *ds)
407 {
408         return ARRAY_SIZE(mv88e6171_hw_stats);
409 }
410
411 struct dsa_switch_driver mv88e6171_switch_driver = {
412         .tag_protocol           = DSA_TAG_PROTO_EDSA,
413         .priv_size              = sizeof(struct mv88e6xxx_priv_state),
414         .probe                  = mv88e6171_probe,
415         .setup                  = mv88e6171_setup,
416         .set_addr               = mv88e6xxx_set_addr_indirect,
417         .phy_read               = mv88e6171_phy_read,
418         .phy_write              = mv88e6171_phy_write,
419         .poll_link              = mv88e6xxx_poll_link,
420         .get_strings            = mv88e6171_get_strings,
421         .get_ethtool_stats      = mv88e6171_get_ethtool_stats,
422         .get_sset_count         = mv88e6171_get_sset_count,
423 #ifdef CONFIG_NET_DSA_HWMON
424         .get_temp               = mv88e6xxx_get_temp,
425 #endif
426         .get_regs_len           = mv88e6xxx_get_regs_len,
427         .get_regs               = mv88e6xxx_get_regs,
428 };
429
430 MODULE_ALIAS("platform:mv88e6171");
431 MODULE_ALIAS("platform:mv88e6172");