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