blob: 9808c860a797f713bbe0bc9b5d3a92e598848922 [file] [log] [blame]
Andrew Lunnf03ae5f2014-11-05 20:01:59 +01001/* net/dsa/mv88e6171.c - Marvell 88e6171/8826172 switch chip support
Andrew Lunn42f27252014-09-12 23:58:44 +02002 * 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
Alexander Duyckb4d23942014-09-15 13:00:27 -040020static char *mv88e6171_probe(struct device *host_dev, int sw_addr)
Andrew Lunn42f27252014-09-12 23:58:44 +020021{
Alexander Duyckb4d23942014-09-15 13:00:27 -040022 struct mii_bus *bus = dsa_host_dev_to_mii_bus(host_dev);
Andrew Lunn42f27252014-09-12 23:58:44 +020023 int ret;
24
Alexander Duyckb4d23942014-09-15 13:00:27 -040025 if (bus == NULL)
26 return NULL;
27
Andrew Lunn42f27252014-09-12 23:58:44 +020028 ret = __mv88e6xxx_reg_read(bus, sw_addr, REG_PORT(0), 0x03);
29 if (ret >= 0) {
30 if ((ret & 0xfff0) == 0x1710)
31 return "Marvell 88E6171";
Andrew Lunnf03ae5f2014-11-05 20:01:59 +010032 if ((ret & 0xfff0) == 0x1720)
33 return "Marvell 88E6172";
Andrew Lunn42f27252014-09-12 23:58:44 +020034 }
35
36 return NULL;
37}
38
39static 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
Andrew Lunn4c732662015-02-14 19:17:51 +010054 /* 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);
Andrew Lunn42f27252014-09-12 23:58:44 +020059
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
84static int mv88e6171_setup_global(struct dsa_switch *ds)
85{
86 int ret;
87 int i;
88
Andrew Lunn4c732662015-02-14 19:17:51 +010089 /* Discard packets with excessive collisions, mask all
90 * interrupt sources, enable PPU.
Andrew Lunn42f27252014-09-12 23:58:44 +020091 */
Andrew Lunn4c732662015-02-14 19:17:51 +010092 REG_WRITE(REG_GLOBAL, 0x04, 0x6000);
Andrew Lunn42f27252014-09-12 23:58:44 +020093
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
175static 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)) {
Guenter Roeck77b3a4d2014-10-14 11:21:04 -0700213 if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA)
Andrew Lunn42f27252014-09-12 23:58:44 +0200214 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
293static int mv88e6171_setup(struct dsa_switch *ds)
294{
295 struct mv88e6xxx_priv_state *ps = (void *)(ds + 1);
296 int i;
297 int ret;
298
299 mutex_init(&ps->smi_mutex);
300 mutex_init(&ps->stats_mutex);
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
Andrew Lunn4dd38cd2014-11-15 22:24:52 +0100321 mutex_init(&ps->phy_mutex);
322
Andrew Lunn42f27252014-09-12 23:58:44 +0200323 return 0;
324}
325
326static int mv88e6171_port_to_phy_addr(int port)
327{
328 if (port >= 0 && port <= 4)
329 return port;
330 return -1;
331}
332
333static int
334mv88e6171_phy_read(struct dsa_switch *ds, int port, int regnum)
335{
Andrew Lunn4dd38cd2014-11-15 22:24:52 +0100336 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunn42f27252014-09-12 23:58:44 +0200337 int addr = mv88e6171_port_to_phy_addr(port);
Andrew Lunn4dd38cd2014-11-15 22:24:52 +0100338 int ret;
Andrew Lunn42f27252014-09-12 23:58:44 +0200339
Andrew Lunn4dd38cd2014-11-15 22:24:52 +0100340 mutex_lock(&ps->phy_mutex);
Andrew Lunn4c732662015-02-14 19:17:51 +0100341 ret = mv88e6xxx_phy_read_indirect(ds, addr, regnum);
Andrew Lunn4dd38cd2014-11-15 22:24:52 +0100342 mutex_unlock(&ps->phy_mutex);
343 return ret;
Andrew Lunn42f27252014-09-12 23:58:44 +0200344}
345
346static int
347mv88e6171_phy_write(struct dsa_switch *ds,
348 int port, int regnum, u16 val)
349{
Andrew Lunn4dd38cd2014-11-15 22:24:52 +0100350 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunn42f27252014-09-12 23:58:44 +0200351 int addr = mv88e6171_port_to_phy_addr(port);
Andrew Lunn4dd38cd2014-11-15 22:24:52 +0100352 int ret;
Andrew Lunn42f27252014-09-12 23:58:44 +0200353
Andrew Lunn4dd38cd2014-11-15 22:24:52 +0100354 mutex_lock(&ps->phy_mutex);
Andrew Lunn4c732662015-02-14 19:17:51 +0100355 ret = mv88e6xxx_phy_write_indirect(ds, addr, regnum, val);
Andrew Lunn4dd38cd2014-11-15 22:24:52 +0100356 mutex_unlock(&ps->phy_mutex);
357 return ret;
Andrew Lunn42f27252014-09-12 23:58:44 +0200358}
359
360static struct mv88e6xxx_hw_stat mv88e6171_hw_stats[] = {
361 { "in_good_octets", 8, 0x00, },
362 { "in_bad_octets", 4, 0x02, },
363 { "in_unicast", 4, 0x04, },
364 { "in_broadcasts", 4, 0x06, },
365 { "in_multicasts", 4, 0x07, },
366 { "in_pause", 4, 0x16, },
367 { "in_undersize", 4, 0x18, },
368 { "in_fragments", 4, 0x19, },
369 { "in_oversize", 4, 0x1a, },
370 { "in_jabber", 4, 0x1b, },
371 { "in_rx_error", 4, 0x1c, },
372 { "in_fcs_error", 4, 0x1d, },
373 { "out_octets", 8, 0x0e, },
374 { "out_unicast", 4, 0x10, },
375 { "out_broadcasts", 4, 0x13, },
376 { "out_multicasts", 4, 0x12, },
377 { "out_pause", 4, 0x15, },
378 { "excessive", 4, 0x11, },
379 { "collisions", 4, 0x1e, },
380 { "deferred", 4, 0x05, },
381 { "single", 4, 0x14, },
382 { "multiple", 4, 0x17, },
383 { "out_fcs_error", 4, 0x03, },
384 { "late", 4, 0x1f, },
385 { "hist_64bytes", 4, 0x08, },
386 { "hist_65_127bytes", 4, 0x09, },
387 { "hist_128_255bytes", 4, 0x0a, },
388 { "hist_256_511bytes", 4, 0x0b, },
389 { "hist_512_1023bytes", 4, 0x0c, },
390 { "hist_1024_max_bytes", 4, 0x0d, },
391};
392
393static void
394mv88e6171_get_strings(struct dsa_switch *ds, int port, uint8_t *data)
395{
396 mv88e6xxx_get_strings(ds, ARRAY_SIZE(mv88e6171_hw_stats),
397 mv88e6171_hw_stats, port, data);
398}
399
400static void
401mv88e6171_get_ethtool_stats(struct dsa_switch *ds,
402 int port, uint64_t *data)
403{
404 mv88e6xxx_get_ethtool_stats(ds, ARRAY_SIZE(mv88e6171_hw_stats),
405 mv88e6171_hw_stats, port, data);
406}
407
408static int mv88e6171_get_sset_count(struct dsa_switch *ds)
409{
410 return ARRAY_SIZE(mv88e6171_hw_stats);
411}
412
413struct dsa_switch_driver mv88e6171_switch_driver = {
Andrew Lunnc146b772014-10-24 23:44:05 +0200414 .tag_protocol = DSA_TAG_PROTO_EDSA,
Andrew Lunn42f27252014-09-12 23:58:44 +0200415 .priv_size = sizeof(struct mv88e6xxx_priv_state),
416 .probe = mv88e6171_probe,
417 .setup = mv88e6171_setup,
418 .set_addr = mv88e6xxx_set_addr_indirect,
419 .phy_read = mv88e6171_phy_read,
420 .phy_write = mv88e6171_phy_write,
421 .poll_link = mv88e6xxx_poll_link,
422 .get_strings = mv88e6171_get_strings,
423 .get_ethtool_stats = mv88e6171_get_ethtool_stats,
424 .get_sset_count = mv88e6171_get_sset_count,
Andrew Lunn4dd38cd2014-11-15 22:24:52 +0100425#ifdef CONFIG_NET_DSA_HWMON
426 .get_temp = mv88e6xxx_get_temp,
427#endif
Andrew Lunn03d6faa2014-11-15 22:24:53 +0100428 .get_regs_len = mv88e6xxx_get_regs_len,
429 .get_regs = mv88e6xxx_get_regs,
Andrew Lunn42f27252014-09-12 23:58:44 +0200430};
431
432MODULE_ALIAS("platform:mv88e6171");
Andrew Lunnf03ae5f2014-11-05 20:01:59 +0100433MODULE_ALIAS("platform:mv88e6172");