1 // SPDX-License-Identifier: (GPL-2.0 or MIT)
4 * Hirschmann Hellcreek TSN switch.
6 * Copyright (C) 2019,2020 Linutronix GmbH
7 * Author Kurt Kanzenbach <kurt@linutronix.de>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/device.h>
14 #include <linux/of_device.h>
15 #include <linux/of_mdio.h>
16 #include <linux/platform_device.h>
17 #include <linux/bitops.h>
18 #include <linux/if_bridge.h>
19 #include <linux/if_vlan.h>
20 #include <linux/etherdevice.h>
21 #include <linux/random.h>
22 #include <linux/iopoll.h>
23 #include <linux/mutex.h>
24 #include <linux/delay.h>
27 #include "hellcreek.h"
28 #include "hellcreek_ptp.h"
29 #include "hellcreek_hwtstamp.h"
31 static const struct hellcreek_counter hellcreek_counter[] = {
32 { 0x00, "RxFiltered", },
33 { 0x01, "RxOctets1k", },
36 { 0x04, "RxOverloadDrop", },
42 { 0x0a, "RxRS65_127", },
43 { 0x0b, "RxRS128_255", },
44 { 0x0c, "RxRS256_511", },
45 { 0x0d, "RxRS512_1023", },
46 { 0x0e, "RxRS1024_1518", },
47 { 0x0f, "RxRS>1518", },
48 { 0x10, "TxTailDropQueue0", },
49 { 0x11, "TxTailDropQueue1", },
50 { 0x12, "TxTailDropQueue2", },
51 { 0x13, "TxTailDropQueue3", },
52 { 0x14, "TxTailDropQueue4", },
53 { 0x15, "TxTailDropQueue5", },
54 { 0x16, "TxTailDropQueue6", },
55 { 0x17, "TxTailDropQueue7", },
56 { 0x18, "RxTrafficClass0", },
57 { 0x19, "RxTrafficClass1", },
58 { 0x1a, "RxTrafficClass2", },
59 { 0x1b, "RxTrafficClass3", },
60 { 0x1c, "RxTrafficClass4", },
61 { 0x1d, "RxTrafficClass5", },
62 { 0x1e, "RxTrafficClass6", },
63 { 0x1f, "RxTrafficClass7", },
64 { 0x21, "TxOctets1k", },
72 { 0x2a, "TxTS65_127", },
73 { 0x2b, "TxTS128_255", },
74 { 0x2c, "TxTS256_511", },
75 { 0x2d, "TxTS512_1023", },
76 { 0x2e, "TxTS1024_1518", },
77 { 0x2f, "TxTS>1518", },
78 { 0x30, "TxTrafficClassOverrun0", },
79 { 0x31, "TxTrafficClassOverrun1", },
80 { 0x32, "TxTrafficClassOverrun2", },
81 { 0x33, "TxTrafficClassOverrun3", },
82 { 0x34, "TxTrafficClassOverrun4", },
83 { 0x35, "TxTrafficClassOverrun5", },
84 { 0x36, "TxTrafficClassOverrun6", },
85 { 0x37, "TxTrafficClassOverrun7", },
86 { 0x38, "TxTrafficClass0", },
87 { 0x39, "TxTrafficClass1", },
88 { 0x3a, "TxTrafficClass2", },
89 { 0x3b, "TxTrafficClass3", },
90 { 0x3c, "TxTrafficClass4", },
91 { 0x3d, "TxTrafficClass5", },
92 { 0x3e, "TxTrafficClass6", },
93 { 0x3f, "TxTrafficClass7", },
96 static u16 hellcreek_read(struct hellcreek *hellcreek, unsigned int offset)
98 return readw(hellcreek->base + offset);
101 static u16 hellcreek_read_ctrl(struct hellcreek *hellcreek)
103 return readw(hellcreek->base + HR_CTRL_C);
106 static u16 hellcreek_read_stat(struct hellcreek *hellcreek)
108 return readw(hellcreek->base + HR_SWSTAT);
111 static void hellcreek_write(struct hellcreek *hellcreek, u16 data,
114 writew(data, hellcreek->base + offset);
117 static void hellcreek_select_port(struct hellcreek *hellcreek, int port)
119 u16 val = port << HR_PSEL_PTWSEL_SHIFT;
121 hellcreek_write(hellcreek, val, HR_PSEL);
124 static void hellcreek_select_prio(struct hellcreek *hellcreek, int prio)
126 u16 val = prio << HR_PSEL_PRTCWSEL_SHIFT;
128 hellcreek_write(hellcreek, val, HR_PSEL);
131 static void hellcreek_select_counter(struct hellcreek *hellcreek, int counter)
133 u16 val = counter << HR_CSEL_SHIFT;
135 hellcreek_write(hellcreek, val, HR_CSEL);
137 /* Data sheet states to wait at least 20 internal clock cycles */
141 static void hellcreek_select_vlan(struct hellcreek *hellcreek, int vid,
146 /* Set pvid bit first */
148 val |= HR_VIDCFG_PVID;
149 hellcreek_write(hellcreek, val, HR_VIDCFG);
152 val |= vid << HR_VIDCFG_VID_SHIFT;
153 hellcreek_write(hellcreek, val, HR_VIDCFG);
156 static int hellcreek_wait_until_ready(struct hellcreek *hellcreek)
160 /* Wait up to 1ms, although 3 us should be enough */
161 return readx_poll_timeout(hellcreek_read_ctrl, hellcreek,
162 val, val & HR_CTRL_C_READY,
166 static int hellcreek_wait_until_transitioned(struct hellcreek *hellcreek)
170 return readx_poll_timeout_atomic(hellcreek_read_ctrl, hellcreek,
171 val, !(val & HR_CTRL_C_TRANSITION),
175 static int hellcreek_wait_fdb_ready(struct hellcreek *hellcreek)
179 return readx_poll_timeout_atomic(hellcreek_read_stat, hellcreek,
180 val, !(val & HR_SWSTAT_BUSY),
184 static int hellcreek_detect(struct hellcreek *hellcreek)
186 u16 id, rel_low, rel_high, date_low, date_high, tgd_ver;
190 id = hellcreek_read(hellcreek, HR_MODID_C);
191 rel_low = hellcreek_read(hellcreek, HR_REL_L_C);
192 rel_high = hellcreek_read(hellcreek, HR_REL_H_C);
193 date_low = hellcreek_read(hellcreek, HR_BLD_L_C);
194 date_high = hellcreek_read(hellcreek, HR_BLD_H_C);
195 tgd_ver = hellcreek_read(hellcreek, TR_TGDVER);
197 if (id != hellcreek->pdata->module_id)
200 rel = rel_low | (rel_high << 16);
201 date = date_low | (date_high << 16);
202 tgd_maj = (tgd_ver & TR_TGDVER_REV_MAJ_MASK) >> TR_TGDVER_REV_MAJ_SHIFT;
203 tgd_min = (tgd_ver & TR_TGDVER_REV_MIN_MASK) >> TR_TGDVER_REV_MIN_SHIFT;
205 dev_info(hellcreek->dev, "Module ID=%02x Release=%04x Date=%04x TGD Version=%02x.%02x\n",
206 id, rel, date, tgd_maj, tgd_min);
211 static void hellcreek_feature_detect(struct hellcreek *hellcreek)
215 features = hellcreek_read(hellcreek, HR_FEABITS0);
217 /* Currently we only detect the size of the FDB table */
218 hellcreek->fdb_entries = ((features & HR_FEABITS0_FDBBINS_MASK) >>
219 HR_FEABITS0_FDBBINS_SHIFT) * 32;
221 dev_info(hellcreek->dev, "Feature detect: FDB entries=%zu\n",
222 hellcreek->fdb_entries);
225 static enum dsa_tag_protocol hellcreek_get_tag_protocol(struct dsa_switch *ds,
227 enum dsa_tag_protocol mp)
229 return DSA_TAG_PROTO_HELLCREEK;
232 static int hellcreek_port_enable(struct dsa_switch *ds, int port,
233 struct phy_device *phy)
235 struct hellcreek *hellcreek = ds->priv;
236 struct hellcreek_port *hellcreek_port;
239 hellcreek_port = &hellcreek->ports[port];
241 dev_dbg(hellcreek->dev, "Enable port %d\n", port);
243 mutex_lock(&hellcreek->reg_lock);
245 hellcreek_select_port(hellcreek, port);
246 val = hellcreek_port->ptcfg;
247 val |= HR_PTCFG_ADMIN_EN;
248 hellcreek_write(hellcreek, val, HR_PTCFG);
249 hellcreek_port->ptcfg = val;
251 mutex_unlock(&hellcreek->reg_lock);
256 static void hellcreek_port_disable(struct dsa_switch *ds, int port)
258 struct hellcreek *hellcreek = ds->priv;
259 struct hellcreek_port *hellcreek_port;
262 hellcreek_port = &hellcreek->ports[port];
264 dev_dbg(hellcreek->dev, "Disable port %d\n", port);
266 mutex_lock(&hellcreek->reg_lock);
268 hellcreek_select_port(hellcreek, port);
269 val = hellcreek_port->ptcfg;
270 val &= ~HR_PTCFG_ADMIN_EN;
271 hellcreek_write(hellcreek, val, HR_PTCFG);
272 hellcreek_port->ptcfg = val;
274 mutex_unlock(&hellcreek->reg_lock);
277 static void hellcreek_get_strings(struct dsa_switch *ds, int port,
278 u32 stringset, uint8_t *data)
282 for (i = 0; i < ARRAY_SIZE(hellcreek_counter); ++i) {
283 const struct hellcreek_counter *counter = &hellcreek_counter[i];
285 strlcpy(data + i * ETH_GSTRING_LEN,
286 counter->name, ETH_GSTRING_LEN);
290 static int hellcreek_get_sset_count(struct dsa_switch *ds, int port, int sset)
292 if (sset != ETH_SS_STATS)
295 return ARRAY_SIZE(hellcreek_counter);
298 static void hellcreek_get_ethtool_stats(struct dsa_switch *ds, int port,
301 struct hellcreek *hellcreek = ds->priv;
302 struct hellcreek_port *hellcreek_port;
305 hellcreek_port = &hellcreek->ports[port];
307 for (i = 0; i < ARRAY_SIZE(hellcreek_counter); ++i) {
308 const struct hellcreek_counter *counter = &hellcreek_counter[i];
309 u8 offset = counter->offset + port * 64;
313 mutex_lock(&hellcreek->reg_lock);
315 hellcreek_select_counter(hellcreek, offset);
317 /* The registers are locked internally by selecting the
318 * counter. So low and high can be read without reading high
321 high = hellcreek_read(hellcreek, HR_CRDH);
322 low = hellcreek_read(hellcreek, HR_CRDL);
323 value = ((u64)high << 16) | low;
325 hellcreek_port->counter_values[i] += value;
326 data[i] = hellcreek_port->counter_values[i];
328 mutex_unlock(&hellcreek->reg_lock);
332 static u16 hellcreek_private_vid(int port)
334 return VLAN_N_VID - port + 1;
337 static int hellcreek_vlan_prepare(struct dsa_switch *ds, int port,
338 const struct switchdev_obj_port_vlan *vlan)
340 struct hellcreek *hellcreek = ds->priv;
343 dev_dbg(hellcreek->dev, "VLAN prepare for port %d\n", port);
345 /* Restriction: Make sure that nobody uses the "private" VLANs. These
346 * VLANs are internally used by the driver to ensure port
347 * separation. Thus, they cannot be used by someone else.
349 for (i = 0; i < hellcreek->pdata->num_ports; ++i) {
350 const u16 restricted_vid = hellcreek_private_vid(i);
352 if (!dsa_is_user_port(ds, i))
355 if (vlan->vid == restricted_vid)
362 static void hellcreek_select_vlan_params(struct hellcreek *hellcreek, int port,
363 int *shift, int *mask)
367 *shift = HR_VIDMBRCFG_P0MBR_SHIFT;
368 *mask = HR_VIDMBRCFG_P0MBR_MASK;
371 *shift = HR_VIDMBRCFG_P1MBR_SHIFT;
372 *mask = HR_VIDMBRCFG_P1MBR_MASK;
375 *shift = HR_VIDMBRCFG_P2MBR_SHIFT;
376 *mask = HR_VIDMBRCFG_P2MBR_MASK;
379 *shift = HR_VIDMBRCFG_P3MBR_SHIFT;
380 *mask = HR_VIDMBRCFG_P3MBR_MASK;
384 dev_err(hellcreek->dev, "Unknown port %d selected!\n", port);
388 static void hellcreek_apply_vlan(struct hellcreek *hellcreek, int port, u16 vid,
389 bool pvid, bool untagged)
394 dev_dbg(hellcreek->dev, "Apply VLAN: port=%d vid=%u pvid=%d untagged=%d",
395 port, vid, pvid, untagged);
397 mutex_lock(&hellcreek->reg_lock);
399 hellcreek_select_port(hellcreek, port);
400 hellcreek_select_vlan(hellcreek, vid, pvid);
402 /* Setup port vlan membership */
403 hellcreek_select_vlan_params(hellcreek, port, &shift, &mask);
404 val = hellcreek->vidmbrcfg[vid];
407 val |= HELLCREEK_VLAN_UNTAGGED_MEMBER << shift;
409 val |= HELLCREEK_VLAN_TAGGED_MEMBER << shift;
411 hellcreek_write(hellcreek, val, HR_VIDMBRCFG);
412 hellcreek->vidmbrcfg[vid] = val;
414 mutex_unlock(&hellcreek->reg_lock);
417 static void hellcreek_unapply_vlan(struct hellcreek *hellcreek, int port,
423 dev_dbg(hellcreek->dev, "Unapply VLAN: port=%d vid=%u\n", port, vid);
425 mutex_lock(&hellcreek->reg_lock);
427 hellcreek_select_vlan(hellcreek, vid, 0);
429 /* Setup port vlan membership */
430 hellcreek_select_vlan_params(hellcreek, port, &shift, &mask);
431 val = hellcreek->vidmbrcfg[vid];
433 val |= HELLCREEK_VLAN_NO_MEMBER << shift;
435 hellcreek_write(hellcreek, val, HR_VIDMBRCFG);
436 hellcreek->vidmbrcfg[vid] = val;
438 mutex_unlock(&hellcreek->reg_lock);
441 static int hellcreek_vlan_add(struct dsa_switch *ds, int port,
442 const struct switchdev_obj_port_vlan *vlan)
444 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
445 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
446 struct hellcreek *hellcreek = ds->priv;
449 err = hellcreek_vlan_prepare(ds, port, vlan);
453 dev_dbg(hellcreek->dev, "Add VLAN %d on port %d, %s, %s\n",
454 vlan->vid, port, untagged ? "untagged" : "tagged",
455 pvid ? "PVID" : "no PVID");
457 hellcreek_apply_vlan(hellcreek, port, vlan->vid, pvid, untagged);
462 static int hellcreek_vlan_del(struct dsa_switch *ds, int port,
463 const struct switchdev_obj_port_vlan *vlan)
465 struct hellcreek *hellcreek = ds->priv;
467 dev_dbg(hellcreek->dev, "Remove VLAN %d on port %d\n", vlan->vid, port);
469 hellcreek_unapply_vlan(hellcreek, port, vlan->vid);
474 static void hellcreek_port_stp_state_set(struct dsa_switch *ds, int port,
477 struct hellcreek *hellcreek = ds->priv;
478 struct hellcreek_port *hellcreek_port;
479 const char *new_state;
482 mutex_lock(&hellcreek->reg_lock);
484 hellcreek_port = &hellcreek->ports[port];
485 val = hellcreek_port->ptcfg;
488 case BR_STATE_DISABLED:
489 new_state = "DISABLED";
490 val |= HR_PTCFG_BLOCKED;
491 val &= ~HR_PTCFG_LEARNING_EN;
493 case BR_STATE_BLOCKING:
494 new_state = "BLOCKING";
495 val |= HR_PTCFG_BLOCKED;
496 val &= ~HR_PTCFG_LEARNING_EN;
498 case BR_STATE_LISTENING:
499 new_state = "LISTENING";
500 val |= HR_PTCFG_BLOCKED;
501 val &= ~HR_PTCFG_LEARNING_EN;
503 case BR_STATE_LEARNING:
504 new_state = "LEARNING";
505 val |= HR_PTCFG_BLOCKED;
506 val |= HR_PTCFG_LEARNING_EN;
508 case BR_STATE_FORWARDING:
509 new_state = "FORWARDING";
510 val &= ~HR_PTCFG_BLOCKED;
511 val |= HR_PTCFG_LEARNING_EN;
514 new_state = "UNKNOWN";
517 hellcreek_select_port(hellcreek, port);
518 hellcreek_write(hellcreek, val, HR_PTCFG);
519 hellcreek_port->ptcfg = val;
521 mutex_unlock(&hellcreek->reg_lock);
523 dev_dbg(hellcreek->dev, "Configured STP state for port %d: %s\n",
527 static void hellcreek_setup_ingressflt(struct hellcreek *hellcreek, int port,
530 struct hellcreek_port *hellcreek_port = &hellcreek->ports[port];
533 mutex_lock(&hellcreek->reg_lock);
535 ptcfg = hellcreek_port->ptcfg;
538 ptcfg |= HR_PTCFG_INGRESSFLT;
540 ptcfg &= ~HR_PTCFG_INGRESSFLT;
542 hellcreek_select_port(hellcreek, port);
543 hellcreek_write(hellcreek, ptcfg, HR_PTCFG);
544 hellcreek_port->ptcfg = ptcfg;
546 mutex_unlock(&hellcreek->reg_lock);
549 static void hellcreek_setup_vlan_awareness(struct hellcreek *hellcreek,
554 mutex_lock(&hellcreek->reg_lock);
556 swcfg = hellcreek->swcfg;
559 swcfg |= HR_SWCFG_VLAN_UNAWARE;
561 swcfg &= ~HR_SWCFG_VLAN_UNAWARE;
563 hellcreek_write(hellcreek, swcfg, HR_SWCFG);
565 mutex_unlock(&hellcreek->reg_lock);
568 /* Default setup for DSA: VLAN <X>: CPU and Port <X> egress untagged. */
569 static void hellcreek_setup_vlan_membership(struct dsa_switch *ds, int port,
572 const u16 vid = hellcreek_private_vid(port);
573 int upstream = dsa_upstream_port(ds, port);
574 struct hellcreek *hellcreek = ds->priv;
576 /* Apply vid to port as egress untagged and port vlan id */
578 hellcreek_apply_vlan(hellcreek, port, vid, true, true);
580 hellcreek_unapply_vlan(hellcreek, port, vid);
582 /* Apply vid to cpu port as well */
584 hellcreek_apply_vlan(hellcreek, upstream, vid, false, true);
586 hellcreek_unapply_vlan(hellcreek, upstream, vid);
589 static int hellcreek_port_bridge_join(struct dsa_switch *ds, int port,
590 struct net_device *br)
592 struct hellcreek *hellcreek = ds->priv;
594 dev_dbg(hellcreek->dev, "Port %d joins a bridge\n", port);
596 /* When joining a vlan_filtering bridge, keep the switch VLAN aware */
597 if (!ds->vlan_filtering)
598 hellcreek_setup_vlan_awareness(hellcreek, false);
600 /* Drop private vlans */
601 hellcreek_setup_vlan_membership(ds, port, false);
606 static void hellcreek_port_bridge_leave(struct dsa_switch *ds, int port,
607 struct net_device *br)
609 struct hellcreek *hellcreek = ds->priv;
611 dev_dbg(hellcreek->dev, "Port %d leaves a bridge\n", port);
613 /* Enable VLAN awareness */
614 hellcreek_setup_vlan_awareness(hellcreek, true);
616 /* Enable private vlans */
617 hellcreek_setup_vlan_membership(ds, port, true);
620 static int __hellcreek_fdb_add(struct hellcreek *hellcreek,
621 const struct hellcreek_fdb_entry *entry)
625 dev_dbg(hellcreek->dev, "Add static FDB entry: MAC=%pM, MASK=0x%02x, "
626 "OBT=%d, REPRIO_EN=%d, PRIO=%d\n", entry->mac, entry->portmask,
627 entry->is_obt, entry->reprio_en, entry->reprio_tc);
629 /* Add mac address */
630 hellcreek_write(hellcreek, entry->mac[1] | (entry->mac[0] << 8), HR_FDBWDH);
631 hellcreek_write(hellcreek, entry->mac[3] | (entry->mac[2] << 8), HR_FDBWDM);
632 hellcreek_write(hellcreek, entry->mac[5] | (entry->mac[4] << 8), HR_FDBWDL);
635 meta |= entry->portmask << HR_FDBWRM0_PORTMASK_SHIFT;
637 meta |= HR_FDBWRM0_OBT;
638 if (entry->reprio_en) {
639 meta |= HR_FDBWRM0_REPRIO_EN;
640 meta |= entry->reprio_tc << HR_FDBWRM0_REPRIO_TC_SHIFT;
642 hellcreek_write(hellcreek, meta, HR_FDBWRM0);
645 hellcreek_write(hellcreek, 0x00, HR_FDBWRCMD);
647 /* Wait until done */
648 return hellcreek_wait_fdb_ready(hellcreek);
651 static int __hellcreek_fdb_del(struct hellcreek *hellcreek,
652 const struct hellcreek_fdb_entry *entry)
654 dev_dbg(hellcreek->dev, "Delete FDB entry: MAC=%pM!\n", entry->mac);
656 /* Delete by matching idx */
657 hellcreek_write(hellcreek, entry->idx | HR_FDBWRCMD_FDBDEL, HR_FDBWRCMD);
659 /* Wait until done */
660 return hellcreek_wait_fdb_ready(hellcreek);
663 /* Retrieve the index of a FDB entry by mac address. Currently we search through
664 * the complete table in hardware. If that's too slow, we might have to cache
665 * the complete FDB table in software.
667 static int hellcreek_fdb_get(struct hellcreek *hellcreek,
668 const unsigned char *dest,
669 struct hellcreek_fdb_entry *entry)
673 /* Set read pointer to zero: The read of HR_FDBMAX (read-only register)
674 * should reset the internal pointer. But, that doesn't work. The vendor
675 * suggested a subsequent write as workaround. Same for HR_FDBRDH below.
677 hellcreek_read(hellcreek, HR_FDBMAX);
678 hellcreek_write(hellcreek, 0x00, HR_FDBMAX);
680 /* We have to read the complete table, because the switch/driver might
681 * enter new entries anywhere.
683 for (i = 0; i < hellcreek->fdb_entries; ++i) {
684 unsigned char addr[ETH_ALEN];
687 meta = hellcreek_read(hellcreek, HR_FDBMDRD);
688 mac = hellcreek_read(hellcreek, HR_FDBRDL);
689 addr[5] = mac & 0xff;
690 addr[4] = (mac & 0xff00) >> 8;
691 mac = hellcreek_read(hellcreek, HR_FDBRDM);
692 addr[3] = mac & 0xff;
693 addr[2] = (mac & 0xff00) >> 8;
694 mac = hellcreek_read(hellcreek, HR_FDBRDH);
695 addr[1] = mac & 0xff;
696 addr[0] = (mac & 0xff00) >> 8;
698 /* Force next entry */
699 hellcreek_write(hellcreek, 0x00, HR_FDBRDH);
701 if (memcmp(addr, dest, ETH_ALEN))
706 entry->portmask = (meta & HR_FDBMDRD_PORTMASK_MASK) >>
707 HR_FDBMDRD_PORTMASK_SHIFT;
708 entry->age = (meta & HR_FDBMDRD_AGE_MASK) >>
709 HR_FDBMDRD_AGE_SHIFT;
710 entry->is_obt = !!(meta & HR_FDBMDRD_OBT);
711 entry->pass_blocked = !!(meta & HR_FDBMDRD_PASS_BLOCKED);
712 entry->is_static = !!(meta & HR_FDBMDRD_STATIC);
713 entry->reprio_tc = (meta & HR_FDBMDRD_REPRIO_TC_MASK) >>
714 HR_FDBMDRD_REPRIO_TC_SHIFT;
715 entry->reprio_en = !!(meta & HR_FDBMDRD_REPRIO_EN);
716 memcpy(entry->mac, addr, sizeof(addr));
724 static int hellcreek_fdb_add(struct dsa_switch *ds, int port,
725 const unsigned char *addr, u16 vid)
727 struct hellcreek_fdb_entry entry = { 0 };
728 struct hellcreek *hellcreek = ds->priv;
731 dev_dbg(hellcreek->dev, "Add FDB entry for MAC=%pM\n", addr);
733 mutex_lock(&hellcreek->reg_lock);
735 ret = hellcreek_fdb_get(hellcreek, addr, &entry);
738 memcpy(entry.mac, addr, sizeof(entry.mac));
739 entry.portmask = BIT(port);
741 ret = __hellcreek_fdb_add(hellcreek, &entry);
743 dev_err(hellcreek->dev, "Failed to add FDB entry!\n");
748 ret = __hellcreek_fdb_del(hellcreek, &entry);
750 dev_err(hellcreek->dev, "Failed to delete FDB entry!\n");
754 entry.portmask |= BIT(port);
756 ret = __hellcreek_fdb_add(hellcreek, &entry);
758 dev_err(hellcreek->dev, "Failed to add FDB entry!\n");
764 mutex_unlock(&hellcreek->reg_lock);
769 static int hellcreek_fdb_del(struct dsa_switch *ds, int port,
770 const unsigned char *addr, u16 vid)
772 struct hellcreek_fdb_entry entry = { 0 };
773 struct hellcreek *hellcreek = ds->priv;
776 dev_dbg(hellcreek->dev, "Delete FDB entry for MAC=%pM\n", addr);
778 mutex_lock(&hellcreek->reg_lock);
780 ret = hellcreek_fdb_get(hellcreek, addr, &entry);
783 dev_err(hellcreek->dev, "FDB entry for deletion not found!\n");
786 ret = __hellcreek_fdb_del(hellcreek, &entry);
788 dev_err(hellcreek->dev, "Failed to delete FDB entry!\n");
792 entry.portmask &= ~BIT(port);
794 if (entry.portmask != 0x00) {
795 ret = __hellcreek_fdb_add(hellcreek, &entry);
797 dev_err(hellcreek->dev, "Failed to add FDB entry!\n");
804 mutex_unlock(&hellcreek->reg_lock);
809 static int hellcreek_fdb_dump(struct dsa_switch *ds, int port,
810 dsa_fdb_dump_cb_t *cb, void *data)
812 struct hellcreek *hellcreek = ds->priv;
816 mutex_lock(&hellcreek->reg_lock);
818 /* Set read pointer to zero: The read of HR_FDBMAX (read-only register)
819 * should reset the internal pointer. But, that doesn't work. The vendor
820 * suggested a subsequent write as workaround. Same for HR_FDBRDH below.
822 entries = hellcreek_read(hellcreek, HR_FDBMAX);
823 hellcreek_write(hellcreek, 0x00, HR_FDBMAX);
825 dev_dbg(hellcreek->dev, "FDB dump for port %d, entries=%d!\n", port, entries);
828 for (i = 0; i < hellcreek->fdb_entries; ++i) {
829 unsigned char null_addr[ETH_ALEN] = { 0 };
830 struct hellcreek_fdb_entry entry = { 0 };
833 meta = hellcreek_read(hellcreek, HR_FDBMDRD);
834 mac = hellcreek_read(hellcreek, HR_FDBRDL);
835 entry.mac[5] = mac & 0xff;
836 entry.mac[4] = (mac & 0xff00) >> 8;
837 mac = hellcreek_read(hellcreek, HR_FDBRDM);
838 entry.mac[3] = mac & 0xff;
839 entry.mac[2] = (mac & 0xff00) >> 8;
840 mac = hellcreek_read(hellcreek, HR_FDBRDH);
841 entry.mac[1] = mac & 0xff;
842 entry.mac[0] = (mac & 0xff00) >> 8;
844 /* Force next entry */
845 hellcreek_write(hellcreek, 0x00, HR_FDBRDH);
848 if (!memcmp(entry.mac, null_addr, ETH_ALEN))
851 entry.portmask = (meta & HR_FDBMDRD_PORTMASK_MASK) >>
852 HR_FDBMDRD_PORTMASK_SHIFT;
853 entry.is_static = !!(meta & HR_FDBMDRD_STATIC);
855 /* Check port mask */
856 if (!(entry.portmask & BIT(port)))
859 cb(entry.mac, 0, entry.is_static, data);
862 mutex_unlock(&hellcreek->reg_lock);
867 static int hellcreek_vlan_filtering(struct dsa_switch *ds, int port,
870 struct hellcreek *hellcreek = ds->priv;
872 dev_dbg(hellcreek->dev, "%s VLAN filtering on port %d\n",
873 vlan_filtering ? "Enable" : "Disable", port);
875 /* Configure port to drop packages with not known vids */
876 hellcreek_setup_ingressflt(hellcreek, port, vlan_filtering);
878 /* Enable VLAN awareness on the switch. This save due to
879 * ds->vlan_filtering_is_global.
881 hellcreek_setup_vlan_awareness(hellcreek, vlan_filtering);
886 static int hellcreek_enable_ip_core(struct hellcreek *hellcreek)
891 mutex_lock(&hellcreek->reg_lock);
893 val = hellcreek_read(hellcreek, HR_CTRL_C);
894 val |= HR_CTRL_C_ENABLE;
895 hellcreek_write(hellcreek, val, HR_CTRL_C);
896 ret = hellcreek_wait_until_transitioned(hellcreek);
898 mutex_unlock(&hellcreek->reg_lock);
903 static void hellcreek_setup_cpu_and_tunnel_port(struct hellcreek *hellcreek)
905 struct hellcreek_port *tunnel_port = &hellcreek->ports[TUNNEL_PORT];
906 struct hellcreek_port *cpu_port = &hellcreek->ports[CPU_PORT];
909 ptcfg |= HR_PTCFG_LEARNING_EN | HR_PTCFG_ADMIN_EN;
911 mutex_lock(&hellcreek->reg_lock);
913 hellcreek_select_port(hellcreek, CPU_PORT);
914 hellcreek_write(hellcreek, ptcfg, HR_PTCFG);
916 hellcreek_select_port(hellcreek, TUNNEL_PORT);
917 hellcreek_write(hellcreek, ptcfg, HR_PTCFG);
919 cpu_port->ptcfg = ptcfg;
920 tunnel_port->ptcfg = ptcfg;
922 mutex_unlock(&hellcreek->reg_lock);
925 static void hellcreek_setup_tc_identity_mapping(struct hellcreek *hellcreek)
929 /* The switch has multiple egress queues per port. The queue is selected
930 * via the PCP field in the VLAN header. The switch internally deals
931 * with traffic classes instead of PCP values and this mapping is
934 * The default mapping is (PCP - TC):
944 * The default should be an identity mapping.
947 for (i = 0; i < 8; ++i) {
948 mutex_lock(&hellcreek->reg_lock);
950 hellcreek_select_prio(hellcreek, i);
951 hellcreek_write(hellcreek,
952 i << HR_PRTCCFG_PCP_TC_MAP_SHIFT,
955 mutex_unlock(&hellcreek->reg_lock);
959 static int hellcreek_setup_fdb(struct hellcreek *hellcreek)
961 static struct hellcreek_fdb_entry ptp = {
962 /* MAC: 01-1B-19-00-00-00 */
963 .mac = { 0x01, 0x1b, 0x19, 0x00, 0x00, 0x00 },
964 .portmask = 0x03, /* Management ports */
969 .reprio_tc = 6, /* TC: 6 as per IEEE 802.1AS */
972 static struct hellcreek_fdb_entry p2p = {
973 /* MAC: 01-80-C2-00-00-0E */
974 .mac = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x0e },
975 .portmask = 0x03, /* Management ports */
980 .reprio_tc = 6, /* TC: 6 as per IEEE 802.1AS */
985 mutex_lock(&hellcreek->reg_lock);
986 ret = __hellcreek_fdb_add(hellcreek, &ptp);
989 ret = __hellcreek_fdb_add(hellcreek, &p2p);
991 mutex_unlock(&hellcreek->reg_lock);
996 static int hellcreek_setup(struct dsa_switch *ds)
998 struct hellcreek *hellcreek = ds->priv;
1002 dev_dbg(hellcreek->dev, "Set up the switch\n");
1005 ret = hellcreek_enable_ip_core(hellcreek);
1007 dev_err(hellcreek->dev, "Failed to enable IP core!\n");
1011 /* Enable CPU/Tunnel ports */
1012 hellcreek_setup_cpu_and_tunnel_port(hellcreek);
1014 /* Switch config: Keep defaults, enable FDB aging and learning and tag
1015 * each frame from/to cpu port for DSA tagging. Also enable the length
1016 * aware shaping mode. This eliminates the need for Qbv guard bands.
1018 swcfg |= HR_SWCFG_FDBAGE_EN |
1019 HR_SWCFG_FDBLRN_EN |
1020 HR_SWCFG_ALWAYS_OBT |
1021 (HR_SWCFG_LAS_ON << HR_SWCFG_LAS_MODE_SHIFT);
1022 hellcreek->swcfg = swcfg;
1023 hellcreek_write(hellcreek, swcfg, HR_SWCFG);
1025 /* Initial vlan membership to reflect port separation */
1026 for (i = 0; i < ds->num_ports; ++i) {
1027 if (!dsa_is_user_port(ds, i))
1030 hellcreek_setup_vlan_membership(ds, i, true);
1033 /* Configure PCP <-> TC mapping */
1034 hellcreek_setup_tc_identity_mapping(hellcreek);
1036 /* Allow VLAN configurations while not filtering which is the default
1037 * for new DSA drivers.
1039 ds->configure_vlan_while_not_filtering = true;
1041 /* The VLAN awareness is a global switch setting. Therefore, mixed vlan
1042 * filtering setups are not supported.
1044 ds->vlan_filtering_is_global = true;
1046 /* Intercept _all_ PTP multicast traffic */
1047 ret = hellcreek_setup_fdb(hellcreek);
1049 dev_err(hellcreek->dev,
1050 "Failed to insert static PTP FDB entries\n");
1057 static void hellcreek_phylink_validate(struct dsa_switch *ds, int port,
1058 unsigned long *supported,
1059 struct phylink_link_state *state)
1061 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
1062 struct hellcreek *hellcreek = ds->priv;
1064 dev_dbg(hellcreek->dev, "Phylink validate for port %d\n", port);
1066 /* The MAC settings are a hardware configuration option and cannot be
1067 * changed at run time or by strapping. Therefore the attached PHYs
1068 * should be programmed to only advertise settings which are supported
1071 if (hellcreek->pdata->is_100_mbits)
1072 phylink_set(mask, 100baseT_Full);
1074 phylink_set(mask, 1000baseT_Full);
1076 bitmap_and(supported, supported, mask,
1077 __ETHTOOL_LINK_MODE_MASK_NBITS);
1078 bitmap_and(state->advertising, state->advertising, mask,
1079 __ETHTOOL_LINK_MODE_MASK_NBITS);
1083 hellcreek_port_prechangeupper(struct dsa_switch *ds, int port,
1084 struct netdev_notifier_changeupper_info *info)
1086 struct hellcreek *hellcreek = ds->priv;
1092 dev_dbg(hellcreek->dev, "Pre change upper for port %d\n", port);
1095 * Deny VLAN devices on top of lan ports with the same VLAN ids, because
1096 * it breaks the port separation due to the private VLANs. Example:
1098 * lan0.100 *and* lan1.100 cannot be used in parallel. However, lan0.99
1099 * and lan1.100 works.
1102 if (!is_vlan_dev(info->upper_dev))
1105 vid = vlan_dev_vlan_id(info->upper_dev);
1107 /* For all ports, check bitmaps */
1108 mutex_lock(&hellcreek->vlan_lock);
1109 for (i = 0; i < hellcreek->pdata->num_ports; ++i) {
1110 if (!dsa_is_user_port(ds, i))
1116 used = used && test_bit(vid, hellcreek->ports[i].vlan_dev_bitmap);
1123 set_bit(vid, hellcreek->ports[port].vlan_dev_bitmap);
1128 mutex_unlock(&hellcreek->vlan_lock);
1133 static const struct dsa_switch_ops hellcreek_ds_ops = {
1134 .get_ethtool_stats = hellcreek_get_ethtool_stats,
1135 .get_sset_count = hellcreek_get_sset_count,
1136 .get_strings = hellcreek_get_strings,
1137 .get_tag_protocol = hellcreek_get_tag_protocol,
1138 .get_ts_info = hellcreek_get_ts_info,
1139 .phylink_validate = hellcreek_phylink_validate,
1140 .port_bridge_join = hellcreek_port_bridge_join,
1141 .port_bridge_leave = hellcreek_port_bridge_leave,
1142 .port_disable = hellcreek_port_disable,
1143 .port_enable = hellcreek_port_enable,
1144 .port_fdb_add = hellcreek_fdb_add,
1145 .port_fdb_del = hellcreek_fdb_del,
1146 .port_fdb_dump = hellcreek_fdb_dump,
1147 .port_hwtstamp_set = hellcreek_port_hwtstamp_set,
1148 .port_hwtstamp_get = hellcreek_port_hwtstamp_get,
1149 .port_prechangeupper = hellcreek_port_prechangeupper,
1150 .port_rxtstamp = hellcreek_port_rxtstamp,
1151 .port_stp_state_set = hellcreek_port_stp_state_set,
1152 .port_txtstamp = hellcreek_port_txtstamp,
1153 .port_vlan_add = hellcreek_vlan_add,
1154 .port_vlan_del = hellcreek_vlan_del,
1155 .port_vlan_filtering = hellcreek_vlan_filtering,
1156 .setup = hellcreek_setup,
1159 static int hellcreek_probe(struct platform_device *pdev)
1161 struct device *dev = &pdev->dev;
1162 struct hellcreek *hellcreek;
1163 struct resource *res;
1166 hellcreek = devm_kzalloc(dev, sizeof(*hellcreek), GFP_KERNEL);
1170 hellcreek->vidmbrcfg = devm_kcalloc(dev, VLAN_N_VID,
1171 sizeof(*hellcreek->vidmbrcfg),
1173 if (!hellcreek->vidmbrcfg)
1176 hellcreek->pdata = of_device_get_match_data(dev);
1178 hellcreek->ports = devm_kcalloc(dev, hellcreek->pdata->num_ports,
1179 sizeof(*hellcreek->ports),
1181 if (!hellcreek->ports)
1184 for (i = 0; i < hellcreek->pdata->num_ports; ++i) {
1185 struct hellcreek_port *port = &hellcreek->ports[i];
1187 port->counter_values =
1189 ARRAY_SIZE(hellcreek_counter),
1190 sizeof(*port->counter_values),
1192 if (!port->counter_values)
1195 port->vlan_dev_bitmap =
1197 BITS_TO_LONGS(VLAN_N_VID),
1198 sizeof(unsigned long),
1200 if (!port->vlan_dev_bitmap)
1203 port->hellcreek = hellcreek;
1207 mutex_init(&hellcreek->reg_lock);
1208 mutex_init(&hellcreek->vlan_lock);
1209 mutex_init(&hellcreek->ptp_lock);
1211 hellcreek->dev = dev;
1213 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "tsn");
1215 dev_err(dev, "No memory region provided!\n");
1219 hellcreek->base = devm_ioremap_resource(dev, res);
1220 if (IS_ERR(hellcreek->base)) {
1221 dev_err(dev, "No memory available!\n");
1222 return PTR_ERR(hellcreek->base);
1225 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ptp");
1227 dev_err(dev, "No PTP memory region provided!\n");
1231 hellcreek->ptp_base = devm_ioremap_resource(dev, res);
1232 if (IS_ERR(hellcreek->ptp_base)) {
1233 dev_err(dev, "No memory available!\n");
1234 return PTR_ERR(hellcreek->ptp_base);
1237 ret = hellcreek_detect(hellcreek);
1239 dev_err(dev, "No (known) chip found!\n");
1243 ret = hellcreek_wait_until_ready(hellcreek);
1245 dev_err(dev, "Switch didn't become ready!\n");
1249 hellcreek_feature_detect(hellcreek);
1251 hellcreek->ds = devm_kzalloc(dev, sizeof(*hellcreek->ds), GFP_KERNEL);
1255 hellcreek->ds->dev = dev;
1256 hellcreek->ds->priv = hellcreek;
1257 hellcreek->ds->ops = &hellcreek_ds_ops;
1258 hellcreek->ds->num_ports = hellcreek->pdata->num_ports;
1259 hellcreek->ds->num_tx_queues = HELLCREEK_NUM_EGRESS_QUEUES;
1261 ret = dsa_register_switch(hellcreek->ds);
1263 dev_err_probe(dev, ret, "Unable to register switch\n");
1267 ret = hellcreek_ptp_setup(hellcreek);
1269 dev_err(dev, "Failed to setup PTP!\n");
1273 ret = hellcreek_hwtstamp_setup(hellcreek);
1275 dev_err(dev, "Failed to setup hardware timestamping!\n");
1276 goto err_tstamp_setup;
1279 platform_set_drvdata(pdev, hellcreek);
1284 hellcreek_ptp_free(hellcreek);
1286 dsa_unregister_switch(hellcreek->ds);
1291 static int hellcreek_remove(struct platform_device *pdev)
1293 struct hellcreek *hellcreek = platform_get_drvdata(pdev);
1295 hellcreek_hwtstamp_free(hellcreek);
1296 hellcreek_ptp_free(hellcreek);
1297 dsa_unregister_switch(hellcreek->ds);
1298 platform_set_drvdata(pdev, NULL);
1303 static const struct hellcreek_platform_data de1soc_r1_pdata = {
1307 .qbv_on_cpu_port = 1,
1309 .module_id = 0x4c30,
1312 static const struct of_device_id hellcreek_of_match[] = {
1314 .compatible = "hirschmann,hellcreek-de1soc-r1",
1315 .data = &de1soc_r1_pdata,
1319 MODULE_DEVICE_TABLE(of, hellcreek_of_match);
1321 static struct platform_driver hellcreek_driver = {
1322 .probe = hellcreek_probe,
1323 .remove = hellcreek_remove,
1325 .name = "hellcreek",
1326 .of_match_table = hellcreek_of_match,
1329 module_platform_driver(hellcreek_driver);
1331 MODULE_AUTHOR("Kurt Kanzenbach <kurt@linutronix.de>");
1332 MODULE_DESCRIPTION("Hirschmann Hellcreek driver");
1333 MODULE_LICENSE("Dual MIT/GPL");