blob: 5a295533ff20c73a6a7c4b6ebfd35f149f228d03 [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) {
Andrew Lunn464caa22015-03-26 18:36:41 -070030 if ((ret & 0xfff0) == ID_6171)
Andrew Lunn42f27252014-09-12 23:58:44 +020031 return "Marvell 88E6171";
Andrew Lunn464caa22015-03-26 18:36:41 -070032 if ((ret & 0xfff0) == ID_6172)
Andrew Lunnf03ae5f2014-11-05 20:01:59 +010033 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{
Andrew Lunn44e50dd2015-04-02 04:06:33 +020041 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunn42f27252014-09-12 23:58:44 +020042 int i;
43 int ret;
44 unsigned long timeout;
45
46 /* Set all ports to the disabled state. */
Andrew Lunn44e50dd2015-04-02 04:06:33 +020047 for (i = 0; i < ps->num_ports; i++) {
Andrew Lunn42f27252014-09-12 23:58:44 +020048 ret = REG_READ(REG_PORT(i), 0x04);
49 REG_WRITE(REG_PORT(i), 0x04, ret & 0xfffc);
50 }
51
52 /* Wait for transmit queues to drain. */
53 usleep_range(2000, 4000);
54
Andrew Lunn4c732662015-02-14 19:17:51 +010055 /* Reset the switch. Keep PPU active. The PPU needs to be
56 * active to support indirect phy register accesses through
57 * global registers 0x18 and 0x19.
58 */
59 REG_WRITE(REG_GLOBAL, 0x04, 0xc000);
Andrew Lunn42f27252014-09-12 23:58:44 +020060
61 /* Wait up to one second for reset to complete. */
62 timeout = jiffies + 1 * HZ;
63 while (time_before(jiffies, timeout)) {
64 ret = REG_READ(REG_GLOBAL, 0x00);
65 if ((ret & 0xc800) == 0xc800)
66 break;
67
68 usleep_range(1000, 2000);
69 }
70 if (time_after(jiffies, timeout))
71 return -ETIMEDOUT;
72
73 /* Enable ports not under DSA, e.g. WAN port */
Andrew Lunn44e50dd2015-04-02 04:06:33 +020074 for (i = 0; i < ps->num_ports; i++) {
Andrew Lunn42f27252014-09-12 23:58:44 +020075 if (dsa_is_cpu_port(ds, i) || ds->phys_port_mask & (1 << i))
76 continue;
77
78 ret = REG_READ(REG_PORT(i), 0x04);
79 REG_WRITE(REG_PORT(i), 0x04, ret | 0x03);
80 }
81
82 return 0;
83}
84
85static int mv88e6171_setup_global(struct dsa_switch *ds)
86{
Andrew Lunn44e50dd2015-04-02 04:06:33 +020087 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunn42f27252014-09-12 23:58:44 +020088 int ret;
89 int i;
90
Andrew Lunn4c732662015-02-14 19:17:51 +010091 /* Discard packets with excessive collisions, mask all
92 * interrupt sources, enable PPU.
Andrew Lunn42f27252014-09-12 23:58:44 +020093 */
Andrew Lunn4c732662015-02-14 19:17:51 +010094 REG_WRITE(REG_GLOBAL, 0x04, 0x6000);
Andrew Lunn42f27252014-09-12 23:58:44 +020095
96 /* Set the default address aging time to 5 minutes, and
97 * enable address learn messages to be sent to all message
98 * ports.
99 */
100 REG_WRITE(REG_GLOBAL, 0x0a, 0x0148);
101
102 /* Configure the priority mapping registers. */
103 ret = mv88e6xxx_config_prio(ds);
104 if (ret < 0)
105 return ret;
106
107 /* Configure the upstream port, and configure the upstream
108 * port as the port to which ingress and egress monitor frames
109 * are to be sent.
110 */
111 if (REG_READ(REG_PORT(0), 0x03) == 0x1710)
112 REG_WRITE(REG_GLOBAL, 0x1a, (dsa_upstream_port(ds) * 0x1111));
113 else
114 REG_WRITE(REG_GLOBAL, 0x1a, (dsa_upstream_port(ds) * 0x1110));
115
116 /* Disable remote management for now, and set the switch's
117 * DSA device number.
118 */
119 REG_WRITE(REG_GLOBAL, 0x1c, ds->index & 0x1f);
120
121 /* Send all frames with destination addresses matching
122 * 01:80:c2:00:00:2x to the CPU port.
123 */
124 REG_WRITE(REG_GLOBAL2, 0x02, 0xffff);
125
126 /* Send all frames with destination addresses matching
127 * 01:80:c2:00:00:0x to the CPU port.
128 */
129 REG_WRITE(REG_GLOBAL2, 0x03, 0xffff);
130
131 /* Disable the loopback filter, disable flow control
132 * messages, disable flood broadcast override, disable
133 * removing of provider tags, disable ATU age violation
134 * interrupts, disable tag flow control, force flow
135 * control priority to the highest, and send all special
136 * multicast frames to the CPU at the highest priority.
137 */
138 REG_WRITE(REG_GLOBAL2, 0x05, 0x00ff);
139
140 /* Program the DSA routing table. */
141 for (i = 0; i < 32; i++) {
142 int nexthop;
143
144 nexthop = 0x1f;
145 if (i != ds->index && i < ds->dst->pd->nr_chips)
146 nexthop = ds->pd->rtable[i] & 0x1f;
147
148 REG_WRITE(REG_GLOBAL2, 0x06, 0x8000 | (i << 8) | nexthop);
149 }
150
151 /* Clear all trunk masks. */
Andrew Lunn44e50dd2015-04-02 04:06:33 +0200152 for (i = 0; i < ps->num_ports; i++)
Andrew Lunn42f27252014-09-12 23:58:44 +0200153 REG_WRITE(REG_GLOBAL2, 0x07, 0x8000 | (i << 12) | 0xff);
154
155 /* Clear all trunk mappings. */
156 for (i = 0; i < 16; i++)
157 REG_WRITE(REG_GLOBAL2, 0x08, 0x8000 | (i << 11));
158
159 /* Disable ingress rate limiting by resetting all ingress
160 * rate limit registers to their initial state.
161 */
162 for (i = 0; i < 6; i++)
163 REG_WRITE(REG_GLOBAL2, 0x09, 0x9000 | (i << 8));
164
165 /* Initialise cross-chip port VLAN table to reset defaults. */
166 REG_WRITE(REG_GLOBAL2, 0x0b, 0x9000);
167
168 /* Clear the priority override table. */
169 for (i = 0; i < 16; i++)
170 REG_WRITE(REG_GLOBAL2, 0x0f, 0x8000 | (i << 8));
171
172 /* @@@ initialise AVB (22/23) watchdog (27) sdet (29) registers */
173
174 return 0;
175}
176
177static int mv88e6171_setup_port(struct dsa_switch *ds, int p)
178{
179 int addr = REG_PORT(p);
180 u16 val;
181
182 /* MAC Forcing register: don't force link, speed, duplex
183 * or flow control state to any particular values on physical
184 * ports, but force the CPU port and all DSA ports to 1000 Mb/s
185 * full duplex.
186 */
187 val = REG_READ(addr, 0x01);
188 if (dsa_is_cpu_port(ds, p) || ds->dsa_port_mask & (1 << p))
189 REG_WRITE(addr, 0x01, val | 0x003e);
190 else
191 REG_WRITE(addr, 0x01, val | 0x0003);
192
193 /* Do not limit the period of time that this port can be
194 * paused for by the remote end or the period of time that
195 * this port can pause the remote end.
196 */
197 REG_WRITE(addr, 0x02, 0x0000);
198
199 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
200 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
201 * tunneling, determine priority by looking at 802.1p and IP
202 * priority fields (IP prio has precedence), and set STP state
203 * to Forwarding.
204 *
205 * If this is the CPU link, use DSA or EDSA tagging depending
206 * on which tagging mode was configured.
207 *
208 * If this is a link to another switch, use DSA tagging mode.
209 *
210 * If this is the upstream port for this switch, enable
211 * forwarding of unknown unicasts and multicasts.
212 */
213 val = 0x0433;
214 if (dsa_is_cpu_port(ds, p)) {
Guenter Roeck77b3a4d2014-10-14 11:21:04 -0700215 if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA)
Andrew Lunn42f27252014-09-12 23:58:44 +0200216 val |= 0x3300;
217 else
218 val |= 0x0100;
219 }
220 if (ds->dsa_port_mask & (1 << p))
221 val |= 0x0100;
222 if (p == dsa_upstream_port(ds))
223 val |= 0x000c;
224 REG_WRITE(addr, 0x04, val);
225
Andrew Lunn42f27252014-09-12 23:58:44 +0200226 /* Port Control 2: don't force a good FCS, set the maximum
227 * frame size to 10240 bytes, don't let the switch add or
228 * strip 802.1q tags, don't discard tagged or untagged frames
229 * on this port, do a destination address lookup on all
230 * received packets as usual, disable ARP mirroring and don't
231 * send a copy of all transmitted/received frames on this port
232 * to the CPU.
233 */
234 REG_WRITE(addr, 0x08, 0x2080);
235
236 /* Egress rate control: disable egress rate control. */
237 REG_WRITE(addr, 0x09, 0x0001);
238
239 /* Egress rate control 2: disable egress rate control. */
240 REG_WRITE(addr, 0x0a, 0x0000);
241
242 /* Port Association Vector: when learning source addresses
243 * of packets, add the address to the address database using
244 * a port bitmap that has only the bit for this port set and
245 * the other bits clear.
246 */
247 REG_WRITE(addr, 0x0b, 1 << p);
248
249 /* Port ATU control: disable limiting the number of address
250 * database entries that this port is allowed to use.
251 */
252 REG_WRITE(addr, 0x0c, 0x0000);
253
254 /* Priority Override: disable DA, SA and VTU priority override. */
255 REG_WRITE(addr, 0x0d, 0x0000);
256
257 /* Port Ethertype: use the Ethertype DSA Ethertype value. */
258 REG_WRITE(addr, 0x0f, ETH_P_EDSA);
259
260 /* Tag Remap: use an identity 802.1p prio -> switch prio
261 * mapping.
262 */
263 REG_WRITE(addr, 0x18, 0x3210);
264
265 /* Tag Remap 2: use an identity 802.1p prio -> switch prio
266 * mapping.
267 */
268 REG_WRITE(addr, 0x19, 0x7654);
269
Guenter Roeckb0019b72015-03-26 18:36:34 -0700270 return mv88e6xxx_setup_port_common(ds, p);
Andrew Lunn42f27252014-09-12 23:58:44 +0200271}
272
273static int mv88e6171_setup(struct dsa_switch *ds)
274{
Andrew Lunn44e50dd2015-04-02 04:06:33 +0200275 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunn42f27252014-09-12 23:58:44 +0200276 int i;
277 int ret;
278
Guenter Roeckacdaffc2015-03-26 18:36:28 -0700279 ret = mv88e6xxx_setup_common(ds);
280 if (ret < 0)
281 return ret;
Andrew Lunn42f27252014-09-12 23:58:44 +0200282
Andrew Lunn44e50dd2015-04-02 04:06:33 +0200283 ps->num_ports = 7;
284
Andrew Lunn42f27252014-09-12 23:58:44 +0200285 ret = mv88e6171_switch_reset(ds);
286 if (ret < 0)
287 return ret;
288
289 /* @@@ initialise vtu and atu */
290
291 ret = mv88e6171_setup_global(ds);
292 if (ret < 0)
293 return ret;
294
Andrew Lunn44e50dd2015-04-02 04:06:33 +0200295 for (i = 0; i < ps->num_ports; i++) {
Andrew Lunn42f27252014-09-12 23:58:44 +0200296 if (!(dsa_is_cpu_port(ds, i) || ds->phys_port_mask & (1 << i)))
297 continue;
298
299 ret = mv88e6171_setup_port(ds, i);
300 if (ret < 0)
301 return ret;
302 }
303
304 return 0;
305}
306
Andrew Lunn44e50dd2015-04-02 04:06:33 +0200307static int mv88e6171_port_to_phy_addr(struct dsa_switch *ds, int port)
Andrew Lunn42f27252014-09-12 23:58:44 +0200308{
Andrew Lunn44e50dd2015-04-02 04:06:33 +0200309 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
310
311 if (port >= 0 && port < ps->num_ports)
Andrew Lunn42f27252014-09-12 23:58:44 +0200312 return port;
313 return -1;
314}
315
316static int
317mv88e6171_phy_read(struct dsa_switch *ds, int port, int regnum)
318{
Andrew Lunn4dd38cd2014-11-15 22:24:52 +0100319 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunn44e50dd2015-04-02 04:06:33 +0200320 int addr = mv88e6171_port_to_phy_addr(ds, port);
Andrew Lunn4dd38cd2014-11-15 22:24:52 +0100321 int ret;
Andrew Lunn42f27252014-09-12 23:58:44 +0200322
Andrew Lunn4dd38cd2014-11-15 22:24:52 +0100323 mutex_lock(&ps->phy_mutex);
Andrew Lunn4c732662015-02-14 19:17:51 +0100324 ret = mv88e6xxx_phy_read_indirect(ds, addr, regnum);
Andrew Lunn4dd38cd2014-11-15 22:24:52 +0100325 mutex_unlock(&ps->phy_mutex);
326 return ret;
Andrew Lunn42f27252014-09-12 23:58:44 +0200327}
328
329static int
330mv88e6171_phy_write(struct dsa_switch *ds,
331 int port, int regnum, u16 val)
332{
Andrew Lunn4dd38cd2014-11-15 22:24:52 +0100333 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunn44e50dd2015-04-02 04:06:33 +0200334 int addr = mv88e6171_port_to_phy_addr(ds, port);
Andrew Lunn4dd38cd2014-11-15 22:24:52 +0100335 int ret;
Andrew Lunn42f27252014-09-12 23:58:44 +0200336
Andrew Lunn4dd38cd2014-11-15 22:24:52 +0100337 mutex_lock(&ps->phy_mutex);
Andrew Lunn4c732662015-02-14 19:17:51 +0100338 ret = mv88e6xxx_phy_write_indirect(ds, addr, regnum, val);
Andrew Lunn4dd38cd2014-11-15 22:24:52 +0100339 mutex_unlock(&ps->phy_mutex);
340 return ret;
Andrew Lunn42f27252014-09-12 23:58:44 +0200341}
342
343static struct mv88e6xxx_hw_stat mv88e6171_hw_stats[] = {
344 { "in_good_octets", 8, 0x00, },
345 { "in_bad_octets", 4, 0x02, },
346 { "in_unicast", 4, 0x04, },
347 { "in_broadcasts", 4, 0x06, },
348 { "in_multicasts", 4, 0x07, },
349 { "in_pause", 4, 0x16, },
350 { "in_undersize", 4, 0x18, },
351 { "in_fragments", 4, 0x19, },
352 { "in_oversize", 4, 0x1a, },
353 { "in_jabber", 4, 0x1b, },
354 { "in_rx_error", 4, 0x1c, },
355 { "in_fcs_error", 4, 0x1d, },
356 { "out_octets", 8, 0x0e, },
357 { "out_unicast", 4, 0x10, },
358 { "out_broadcasts", 4, 0x13, },
359 { "out_multicasts", 4, 0x12, },
360 { "out_pause", 4, 0x15, },
361 { "excessive", 4, 0x11, },
362 { "collisions", 4, 0x1e, },
363 { "deferred", 4, 0x05, },
364 { "single", 4, 0x14, },
365 { "multiple", 4, 0x17, },
366 { "out_fcs_error", 4, 0x03, },
367 { "late", 4, 0x1f, },
368 { "hist_64bytes", 4, 0x08, },
369 { "hist_65_127bytes", 4, 0x09, },
370 { "hist_128_255bytes", 4, 0x0a, },
371 { "hist_256_511bytes", 4, 0x0b, },
372 { "hist_512_1023bytes", 4, 0x0c, },
373 { "hist_1024_max_bytes", 4, 0x0d, },
374};
375
376static void
377mv88e6171_get_strings(struct dsa_switch *ds, int port, uint8_t *data)
378{
379 mv88e6xxx_get_strings(ds, ARRAY_SIZE(mv88e6171_hw_stats),
380 mv88e6171_hw_stats, port, data);
381}
382
383static void
384mv88e6171_get_ethtool_stats(struct dsa_switch *ds,
385 int port, uint64_t *data)
386{
387 mv88e6xxx_get_ethtool_stats(ds, ARRAY_SIZE(mv88e6171_hw_stats),
388 mv88e6171_hw_stats, port, data);
389}
390
391static int mv88e6171_get_sset_count(struct dsa_switch *ds)
392{
393 return ARRAY_SIZE(mv88e6171_hw_stats);
394}
395
Andrew Lunnbaae51d2015-03-26 18:36:42 -0700396static int mv88e6171_get_eee(struct dsa_switch *ds, int port,
397 struct ethtool_eee *e)
398{
399 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
400
401 if (ps->id == ID_6172)
402 return mv88e6xxx_get_eee(ds, port, e);
403
404 return -EOPNOTSUPP;
405}
406
407static int mv88e6171_set_eee(struct dsa_switch *ds, int port,
408 struct phy_device *phydev, struct ethtool_eee *e)
409{
410 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
411
412 if (ps->id == ID_6172)
413 return mv88e6xxx_set_eee(ds, port, phydev, e);
414
415 return -EOPNOTSUPP;
416}
417
Andrew Lunn42f27252014-09-12 23:58:44 +0200418struct dsa_switch_driver mv88e6171_switch_driver = {
Andrew Lunnc146b772014-10-24 23:44:05 +0200419 .tag_protocol = DSA_TAG_PROTO_EDSA,
Andrew Lunn42f27252014-09-12 23:58:44 +0200420 .priv_size = sizeof(struct mv88e6xxx_priv_state),
421 .probe = mv88e6171_probe,
422 .setup = mv88e6171_setup,
423 .set_addr = mv88e6xxx_set_addr_indirect,
424 .phy_read = mv88e6171_phy_read,
425 .phy_write = mv88e6171_phy_write,
426 .poll_link = mv88e6xxx_poll_link,
427 .get_strings = mv88e6171_get_strings,
428 .get_ethtool_stats = mv88e6171_get_ethtool_stats,
429 .get_sset_count = mv88e6171_get_sset_count,
Andrew Lunnbaae51d2015-03-26 18:36:42 -0700430 .set_eee = mv88e6171_set_eee,
431 .get_eee = mv88e6171_get_eee,
Andrew Lunn4dd38cd2014-11-15 22:24:52 +0100432#ifdef CONFIG_NET_DSA_HWMON
433 .get_temp = mv88e6xxx_get_temp,
434#endif
Andrew Lunn03d6faa2014-11-15 22:24:53 +0100435 .get_regs_len = mv88e6xxx_get_regs_len,
436 .get_regs = mv88e6xxx_get_regs,
Andrew Lunnb2a6b932015-03-26 18:36:43 -0700437 .port_join_bridge = mv88e6xxx_join_bridge,
438 .port_leave_bridge = mv88e6xxx_leave_bridge,
439 .port_stp_update = mv88e6xxx_port_stp_update,
440 .fdb_add = mv88e6xxx_port_fdb_add,
441 .fdb_del = mv88e6xxx_port_fdb_del,
442 .fdb_getnext = mv88e6xxx_port_fdb_getnext,
Andrew Lunn42f27252014-09-12 23:58:44 +0200443};
444
445MODULE_ALIAS("platform:mv88e6171");
Andrew Lunnf03ae5f2014-11-05 20:01:59 +0100446MODULE_ALIAS("platform:mv88e6172");