1 // SPDX-License-Identifier: (GPL-2.0 or MIT)
4 * Hirschmann Hellcreek TSN switch.
6 * Copyright (C) 2019-2021 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_port_prio(struct hellcreek *hellcreek, int port,
134 u16 val = port << HR_PSEL_PTWSEL_SHIFT;
136 val |= prio << HR_PSEL_PRTCWSEL_SHIFT;
138 hellcreek_write(hellcreek, val, HR_PSEL);
141 static void hellcreek_select_counter(struct hellcreek *hellcreek, int counter)
143 u16 val = counter << HR_CSEL_SHIFT;
145 hellcreek_write(hellcreek, val, HR_CSEL);
147 /* Data sheet states to wait at least 20 internal clock cycles */
151 static void hellcreek_select_vlan(struct hellcreek *hellcreek, int vid,
156 /* Set pvid bit first */
158 val |= HR_VIDCFG_PVID;
159 hellcreek_write(hellcreek, val, HR_VIDCFG);
162 val |= vid << HR_VIDCFG_VID_SHIFT;
163 hellcreek_write(hellcreek, val, HR_VIDCFG);
166 static void hellcreek_select_tgd(struct hellcreek *hellcreek, int port)
168 u16 val = port << TR_TGDSEL_TDGSEL_SHIFT;
170 hellcreek_write(hellcreek, val, TR_TGDSEL);
173 static int hellcreek_wait_until_ready(struct hellcreek *hellcreek)
177 /* Wait up to 1ms, although 3 us should be enough */
178 return readx_poll_timeout(hellcreek_read_ctrl, hellcreek,
179 val, val & HR_CTRL_C_READY,
183 static int hellcreek_wait_until_transitioned(struct hellcreek *hellcreek)
187 return readx_poll_timeout_atomic(hellcreek_read_ctrl, hellcreek,
188 val, !(val & HR_CTRL_C_TRANSITION),
192 static int hellcreek_wait_fdb_ready(struct hellcreek *hellcreek)
196 return readx_poll_timeout_atomic(hellcreek_read_stat, hellcreek,
197 val, !(val & HR_SWSTAT_BUSY),
201 static int hellcreek_detect(struct hellcreek *hellcreek)
203 u16 id, rel_low, rel_high, date_low, date_high, tgd_ver;
207 id = hellcreek_read(hellcreek, HR_MODID_C);
208 rel_low = hellcreek_read(hellcreek, HR_REL_L_C);
209 rel_high = hellcreek_read(hellcreek, HR_REL_H_C);
210 date_low = hellcreek_read(hellcreek, HR_BLD_L_C);
211 date_high = hellcreek_read(hellcreek, HR_BLD_H_C);
212 tgd_ver = hellcreek_read(hellcreek, TR_TGDVER);
214 if (id != hellcreek->pdata->module_id)
217 rel = rel_low | (rel_high << 16);
218 date = date_low | (date_high << 16);
219 tgd_maj = (tgd_ver & TR_TGDVER_REV_MAJ_MASK) >> TR_TGDVER_REV_MAJ_SHIFT;
220 tgd_min = (tgd_ver & TR_TGDVER_REV_MIN_MASK) >> TR_TGDVER_REV_MIN_SHIFT;
222 dev_info(hellcreek->dev, "Module ID=%02x Release=%04x Date=%04x TGD Version=%02x.%02x\n",
223 id, rel, date, tgd_maj, tgd_min);
228 static void hellcreek_feature_detect(struct hellcreek *hellcreek)
232 features = hellcreek_read(hellcreek, HR_FEABITS0);
234 /* Only detect the size of the FDB table. The size and current
235 * utilization can be queried via devlink.
237 hellcreek->fdb_entries = ((features & HR_FEABITS0_FDBBINS_MASK) >>
238 HR_FEABITS0_FDBBINS_SHIFT) * 32;
241 static enum dsa_tag_protocol hellcreek_get_tag_protocol(struct dsa_switch *ds,
243 enum dsa_tag_protocol mp)
245 return DSA_TAG_PROTO_HELLCREEK;
248 static int hellcreek_port_enable(struct dsa_switch *ds, int port,
249 struct phy_device *phy)
251 struct hellcreek *hellcreek = ds->priv;
252 struct hellcreek_port *hellcreek_port;
255 hellcreek_port = &hellcreek->ports[port];
257 dev_dbg(hellcreek->dev, "Enable port %d\n", port);
259 mutex_lock(&hellcreek->reg_lock);
261 hellcreek_select_port(hellcreek, port);
262 val = hellcreek_port->ptcfg;
263 val |= HR_PTCFG_ADMIN_EN;
264 hellcreek_write(hellcreek, val, HR_PTCFG);
265 hellcreek_port->ptcfg = val;
267 mutex_unlock(&hellcreek->reg_lock);
272 static void hellcreek_port_disable(struct dsa_switch *ds, int port)
274 struct hellcreek *hellcreek = ds->priv;
275 struct hellcreek_port *hellcreek_port;
278 hellcreek_port = &hellcreek->ports[port];
280 dev_dbg(hellcreek->dev, "Disable port %d\n", port);
282 mutex_lock(&hellcreek->reg_lock);
284 hellcreek_select_port(hellcreek, port);
285 val = hellcreek_port->ptcfg;
286 val &= ~HR_PTCFG_ADMIN_EN;
287 hellcreek_write(hellcreek, val, HR_PTCFG);
288 hellcreek_port->ptcfg = val;
290 mutex_unlock(&hellcreek->reg_lock);
293 static void hellcreek_get_strings(struct dsa_switch *ds, int port,
294 u32 stringset, uint8_t *data)
298 for (i = 0; i < ARRAY_SIZE(hellcreek_counter); ++i) {
299 const struct hellcreek_counter *counter = &hellcreek_counter[i];
301 strscpy(data + i * ETH_GSTRING_LEN,
302 counter->name, ETH_GSTRING_LEN);
306 static int hellcreek_get_sset_count(struct dsa_switch *ds, int port, int sset)
308 if (sset != ETH_SS_STATS)
311 return ARRAY_SIZE(hellcreek_counter);
314 static void hellcreek_get_ethtool_stats(struct dsa_switch *ds, int port,
317 struct hellcreek *hellcreek = ds->priv;
318 struct hellcreek_port *hellcreek_port;
321 hellcreek_port = &hellcreek->ports[port];
323 for (i = 0; i < ARRAY_SIZE(hellcreek_counter); ++i) {
324 const struct hellcreek_counter *counter = &hellcreek_counter[i];
325 u8 offset = counter->offset + port * 64;
329 mutex_lock(&hellcreek->reg_lock);
331 hellcreek_select_counter(hellcreek, offset);
333 /* The registers are locked internally by selecting the
334 * counter. So low and high can be read without reading high
337 high = hellcreek_read(hellcreek, HR_CRDH);
338 low = hellcreek_read(hellcreek, HR_CRDL);
339 value = ((u64)high << 16) | low;
341 hellcreek_port->counter_values[i] += value;
342 data[i] = hellcreek_port->counter_values[i];
344 mutex_unlock(&hellcreek->reg_lock);
348 static u16 hellcreek_private_vid(int port)
350 return VLAN_N_VID - port + 1;
353 static int hellcreek_vlan_prepare(struct dsa_switch *ds, int port,
354 const struct switchdev_obj_port_vlan *vlan,
355 struct netlink_ext_ack *extack)
357 struct hellcreek *hellcreek = ds->priv;
360 dev_dbg(hellcreek->dev, "VLAN prepare for port %d\n", port);
362 /* Restriction: Make sure that nobody uses the "private" VLANs. These
363 * VLANs are internally used by the driver to ensure port
364 * separation. Thus, they cannot be used by someone else.
366 for (i = 0; i < hellcreek->pdata->num_ports; ++i) {
367 const u16 restricted_vid = hellcreek_private_vid(i);
369 if (!dsa_is_user_port(ds, i))
372 if (vlan->vid == restricted_vid) {
373 NL_SET_ERR_MSG_MOD(extack, "VID restricted by driver");
381 static void hellcreek_select_vlan_params(struct hellcreek *hellcreek, int port,
382 int *shift, int *mask)
386 *shift = HR_VIDMBRCFG_P0MBR_SHIFT;
387 *mask = HR_VIDMBRCFG_P0MBR_MASK;
390 *shift = HR_VIDMBRCFG_P1MBR_SHIFT;
391 *mask = HR_VIDMBRCFG_P1MBR_MASK;
394 *shift = HR_VIDMBRCFG_P2MBR_SHIFT;
395 *mask = HR_VIDMBRCFG_P2MBR_MASK;
398 *shift = HR_VIDMBRCFG_P3MBR_SHIFT;
399 *mask = HR_VIDMBRCFG_P3MBR_MASK;
403 dev_err(hellcreek->dev, "Unknown port %d selected!\n", port);
407 static void hellcreek_apply_vlan(struct hellcreek *hellcreek, int port, u16 vid,
408 bool pvid, bool untagged)
413 dev_dbg(hellcreek->dev, "Apply VLAN: port=%d vid=%u pvid=%d untagged=%d",
414 port, vid, pvid, untagged);
416 mutex_lock(&hellcreek->reg_lock);
418 hellcreek_select_port(hellcreek, port);
419 hellcreek_select_vlan(hellcreek, vid, pvid);
421 /* Setup port vlan membership */
422 hellcreek_select_vlan_params(hellcreek, port, &shift, &mask);
423 val = hellcreek->vidmbrcfg[vid];
426 val |= HELLCREEK_VLAN_UNTAGGED_MEMBER << shift;
428 val |= HELLCREEK_VLAN_TAGGED_MEMBER << shift;
430 hellcreek_write(hellcreek, val, HR_VIDMBRCFG);
431 hellcreek->vidmbrcfg[vid] = val;
433 mutex_unlock(&hellcreek->reg_lock);
436 static void hellcreek_unapply_vlan(struct hellcreek *hellcreek, int port,
442 dev_dbg(hellcreek->dev, "Unapply VLAN: port=%d vid=%u\n", port, vid);
444 mutex_lock(&hellcreek->reg_lock);
446 hellcreek_select_vlan(hellcreek, vid, false);
448 /* Setup port vlan membership */
449 hellcreek_select_vlan_params(hellcreek, port, &shift, &mask);
450 val = hellcreek->vidmbrcfg[vid];
452 val |= HELLCREEK_VLAN_NO_MEMBER << shift;
454 hellcreek_write(hellcreek, val, HR_VIDMBRCFG);
455 hellcreek->vidmbrcfg[vid] = val;
457 mutex_unlock(&hellcreek->reg_lock);
460 static int hellcreek_vlan_add(struct dsa_switch *ds, int port,
461 const struct switchdev_obj_port_vlan *vlan,
462 struct netlink_ext_ack *extack)
464 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
465 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
466 struct hellcreek *hellcreek = ds->priv;
469 err = hellcreek_vlan_prepare(ds, port, vlan, extack);
473 dev_dbg(hellcreek->dev, "Add VLAN %d on port %d, %s, %s\n",
474 vlan->vid, port, untagged ? "untagged" : "tagged",
475 pvid ? "PVID" : "no PVID");
477 hellcreek_apply_vlan(hellcreek, port, vlan->vid, pvid, untagged);
482 static int hellcreek_vlan_del(struct dsa_switch *ds, int port,
483 const struct switchdev_obj_port_vlan *vlan)
485 struct hellcreek *hellcreek = ds->priv;
487 dev_dbg(hellcreek->dev, "Remove VLAN %d on port %d\n", vlan->vid, port);
489 hellcreek_unapply_vlan(hellcreek, port, vlan->vid);
494 static void hellcreek_port_stp_state_set(struct dsa_switch *ds, int port,
497 struct hellcreek *hellcreek = ds->priv;
498 struct hellcreek_port *hellcreek_port;
499 const char *new_state;
502 mutex_lock(&hellcreek->reg_lock);
504 hellcreek_port = &hellcreek->ports[port];
505 val = hellcreek_port->ptcfg;
508 case BR_STATE_DISABLED:
509 new_state = "DISABLED";
510 val |= HR_PTCFG_BLOCKED;
511 val &= ~HR_PTCFG_LEARNING_EN;
513 case BR_STATE_BLOCKING:
514 new_state = "BLOCKING";
515 val |= HR_PTCFG_BLOCKED;
516 val &= ~HR_PTCFG_LEARNING_EN;
518 case BR_STATE_LISTENING:
519 new_state = "LISTENING";
520 val |= HR_PTCFG_BLOCKED;
521 val &= ~HR_PTCFG_LEARNING_EN;
523 case BR_STATE_LEARNING:
524 new_state = "LEARNING";
525 val |= HR_PTCFG_BLOCKED;
526 val |= HR_PTCFG_LEARNING_EN;
528 case BR_STATE_FORWARDING:
529 new_state = "FORWARDING";
530 val &= ~HR_PTCFG_BLOCKED;
531 val |= HR_PTCFG_LEARNING_EN;
534 new_state = "UNKNOWN";
537 hellcreek_select_port(hellcreek, port);
538 hellcreek_write(hellcreek, val, HR_PTCFG);
539 hellcreek_port->ptcfg = val;
541 mutex_unlock(&hellcreek->reg_lock);
543 dev_dbg(hellcreek->dev, "Configured STP state for port %d: %s\n",
547 static void hellcreek_setup_ingressflt(struct hellcreek *hellcreek, int port,
550 struct hellcreek_port *hellcreek_port = &hellcreek->ports[port];
553 mutex_lock(&hellcreek->reg_lock);
555 ptcfg = hellcreek_port->ptcfg;
558 ptcfg |= HR_PTCFG_INGRESSFLT;
560 ptcfg &= ~HR_PTCFG_INGRESSFLT;
562 hellcreek_select_port(hellcreek, port);
563 hellcreek_write(hellcreek, ptcfg, HR_PTCFG);
564 hellcreek_port->ptcfg = ptcfg;
566 mutex_unlock(&hellcreek->reg_lock);
569 static void hellcreek_setup_vlan_awareness(struct hellcreek *hellcreek,
574 mutex_lock(&hellcreek->reg_lock);
576 swcfg = hellcreek->swcfg;
579 swcfg |= HR_SWCFG_VLAN_UNAWARE;
581 swcfg &= ~HR_SWCFG_VLAN_UNAWARE;
583 hellcreek_write(hellcreek, swcfg, HR_SWCFG);
585 mutex_unlock(&hellcreek->reg_lock);
588 /* Default setup for DSA: VLAN <X>: CPU and Port <X> egress untagged. */
589 static void hellcreek_setup_vlan_membership(struct dsa_switch *ds, int port,
592 const u16 vid = hellcreek_private_vid(port);
593 int upstream = dsa_upstream_port(ds, port);
594 struct hellcreek *hellcreek = ds->priv;
596 /* Apply vid to port as egress untagged and port vlan id */
598 hellcreek_apply_vlan(hellcreek, port, vid, true, true);
600 hellcreek_unapply_vlan(hellcreek, port, vid);
602 /* Apply vid to cpu port as well */
604 hellcreek_apply_vlan(hellcreek, upstream, vid, false, true);
606 hellcreek_unapply_vlan(hellcreek, upstream, vid);
609 static void hellcreek_port_set_ucast_flood(struct hellcreek *hellcreek,
610 int port, bool enable)
612 struct hellcreek_port *hellcreek_port;
615 hellcreek_port = &hellcreek->ports[port];
617 dev_dbg(hellcreek->dev, "%s unicast flooding on port %d\n",
618 enable ? "Enable" : "Disable", port);
620 mutex_lock(&hellcreek->reg_lock);
622 hellcreek_select_port(hellcreek, port);
623 val = hellcreek_port->ptcfg;
625 val &= ~HR_PTCFG_UUC_FLT;
627 val |= HR_PTCFG_UUC_FLT;
628 hellcreek_write(hellcreek, val, HR_PTCFG);
629 hellcreek_port->ptcfg = val;
631 mutex_unlock(&hellcreek->reg_lock);
634 static void hellcreek_port_set_mcast_flood(struct hellcreek *hellcreek,
635 int port, bool enable)
637 struct hellcreek_port *hellcreek_port;
640 hellcreek_port = &hellcreek->ports[port];
642 dev_dbg(hellcreek->dev, "%s multicast flooding on port %d\n",
643 enable ? "Enable" : "Disable", port);
645 mutex_lock(&hellcreek->reg_lock);
647 hellcreek_select_port(hellcreek, port);
648 val = hellcreek_port->ptcfg;
650 val &= ~HR_PTCFG_UMC_FLT;
652 val |= HR_PTCFG_UMC_FLT;
653 hellcreek_write(hellcreek, val, HR_PTCFG);
654 hellcreek_port->ptcfg = val;
656 mutex_unlock(&hellcreek->reg_lock);
659 static int hellcreek_pre_bridge_flags(struct dsa_switch *ds, int port,
660 struct switchdev_brport_flags flags,
661 struct netlink_ext_ack *extack)
663 if (flags.mask & ~(BR_FLOOD | BR_MCAST_FLOOD))
669 static int hellcreek_bridge_flags(struct dsa_switch *ds, int port,
670 struct switchdev_brport_flags flags,
671 struct netlink_ext_ack *extack)
673 struct hellcreek *hellcreek = ds->priv;
675 if (flags.mask & BR_FLOOD)
676 hellcreek_port_set_ucast_flood(hellcreek, port,
677 !!(flags.val & BR_FLOOD));
679 if (flags.mask & BR_MCAST_FLOOD)
680 hellcreek_port_set_mcast_flood(hellcreek, port,
681 !!(flags.val & BR_MCAST_FLOOD));
686 static int hellcreek_port_bridge_join(struct dsa_switch *ds, int port,
687 struct dsa_bridge bridge,
688 bool *tx_fwd_offload,
689 struct netlink_ext_ack *extack)
691 struct hellcreek *hellcreek = ds->priv;
693 dev_dbg(hellcreek->dev, "Port %d joins a bridge\n", port);
695 /* When joining a vlan_filtering bridge, keep the switch VLAN aware */
696 if (!ds->vlan_filtering)
697 hellcreek_setup_vlan_awareness(hellcreek, false);
699 /* Drop private vlans */
700 hellcreek_setup_vlan_membership(ds, port, false);
705 static void hellcreek_port_bridge_leave(struct dsa_switch *ds, int port,
706 struct dsa_bridge bridge)
708 struct hellcreek *hellcreek = ds->priv;
710 dev_dbg(hellcreek->dev, "Port %d leaves a bridge\n", port);
712 /* Enable VLAN awareness */
713 hellcreek_setup_vlan_awareness(hellcreek, true);
715 /* Enable private vlans */
716 hellcreek_setup_vlan_membership(ds, port, true);
719 static int __hellcreek_fdb_add(struct hellcreek *hellcreek,
720 const struct hellcreek_fdb_entry *entry)
724 dev_dbg(hellcreek->dev, "Add static FDB entry: MAC=%pM, MASK=0x%02x, "
725 "OBT=%d, PASS_BLOCKED=%d, REPRIO_EN=%d, PRIO=%d\n", entry->mac,
726 entry->portmask, entry->is_obt, entry->pass_blocked,
727 entry->reprio_en, entry->reprio_tc);
729 /* Add mac address */
730 hellcreek_write(hellcreek, entry->mac[1] | (entry->mac[0] << 8), HR_FDBWDH);
731 hellcreek_write(hellcreek, entry->mac[3] | (entry->mac[2] << 8), HR_FDBWDM);
732 hellcreek_write(hellcreek, entry->mac[5] | (entry->mac[4] << 8), HR_FDBWDL);
735 meta |= entry->portmask << HR_FDBWRM0_PORTMASK_SHIFT;
737 meta |= HR_FDBWRM0_OBT;
738 if (entry->pass_blocked)
739 meta |= HR_FDBWRM0_PASS_BLOCKED;
740 if (entry->reprio_en) {
741 meta |= HR_FDBWRM0_REPRIO_EN;
742 meta |= entry->reprio_tc << HR_FDBWRM0_REPRIO_TC_SHIFT;
744 hellcreek_write(hellcreek, meta, HR_FDBWRM0);
747 hellcreek_write(hellcreek, 0x00, HR_FDBWRCMD);
749 /* Wait until done */
750 return hellcreek_wait_fdb_ready(hellcreek);
753 static int __hellcreek_fdb_del(struct hellcreek *hellcreek,
754 const struct hellcreek_fdb_entry *entry)
756 dev_dbg(hellcreek->dev, "Delete FDB entry: MAC=%pM!\n", entry->mac);
758 /* Delete by matching idx */
759 hellcreek_write(hellcreek, entry->idx | HR_FDBWRCMD_FDBDEL, HR_FDBWRCMD);
761 /* Wait until done */
762 return hellcreek_wait_fdb_ready(hellcreek);
765 static void hellcreek_populate_fdb_entry(struct hellcreek *hellcreek,
766 struct hellcreek_fdb_entry *entry,
769 unsigned char addr[ETH_ALEN];
773 meta = hellcreek_read(hellcreek, HR_FDBMDRD);
774 mac = hellcreek_read(hellcreek, HR_FDBRDL);
775 addr[5] = mac & 0xff;
776 addr[4] = (mac & 0xff00) >> 8;
777 mac = hellcreek_read(hellcreek, HR_FDBRDM);
778 addr[3] = mac & 0xff;
779 addr[2] = (mac & 0xff00) >> 8;
780 mac = hellcreek_read(hellcreek, HR_FDBRDH);
781 addr[1] = mac & 0xff;
782 addr[0] = (mac & 0xff00) >> 8;
784 /* Populate @entry */
785 memcpy(entry->mac, addr, sizeof(addr));
787 entry->portmask = (meta & HR_FDBMDRD_PORTMASK_MASK) >>
788 HR_FDBMDRD_PORTMASK_SHIFT;
789 entry->age = (meta & HR_FDBMDRD_AGE_MASK) >>
790 HR_FDBMDRD_AGE_SHIFT;
791 entry->is_obt = !!(meta & HR_FDBMDRD_OBT);
792 entry->pass_blocked = !!(meta & HR_FDBMDRD_PASS_BLOCKED);
793 entry->is_static = !!(meta & HR_FDBMDRD_STATIC);
794 entry->reprio_tc = (meta & HR_FDBMDRD_REPRIO_TC_MASK) >>
795 HR_FDBMDRD_REPRIO_TC_SHIFT;
796 entry->reprio_en = !!(meta & HR_FDBMDRD_REPRIO_EN);
799 /* Retrieve the index of a FDB entry by mac address. Currently we search through
800 * the complete table in hardware. If that's too slow, we might have to cache
801 * the complete FDB table in software.
803 static int hellcreek_fdb_get(struct hellcreek *hellcreek,
804 const unsigned char *dest,
805 struct hellcreek_fdb_entry *entry)
809 /* Set read pointer to zero: The read of HR_FDBMAX (read-only register)
810 * should reset the internal pointer. But, that doesn't work. The vendor
811 * suggested a subsequent write as workaround. Same for HR_FDBRDH below.
813 hellcreek_read(hellcreek, HR_FDBMAX);
814 hellcreek_write(hellcreek, 0x00, HR_FDBMAX);
816 /* We have to read the complete table, because the switch/driver might
817 * enter new entries anywhere.
819 for (i = 0; i < hellcreek->fdb_entries; ++i) {
820 struct hellcreek_fdb_entry tmp = { 0 };
823 hellcreek_populate_fdb_entry(hellcreek, &tmp, i);
825 /* Force next entry */
826 hellcreek_write(hellcreek, 0x00, HR_FDBRDH);
828 if (memcmp(tmp.mac, dest, ETH_ALEN))
832 memcpy(entry, &tmp, sizeof(*entry));
840 static int hellcreek_fdb_add(struct dsa_switch *ds, int port,
841 const unsigned char *addr, u16 vid,
844 struct hellcreek_fdb_entry entry = { 0 };
845 struct hellcreek *hellcreek = ds->priv;
848 dev_dbg(hellcreek->dev, "Add FDB entry for MAC=%pM\n", addr);
850 mutex_lock(&hellcreek->reg_lock);
852 ret = hellcreek_fdb_get(hellcreek, addr, &entry);
855 memcpy(entry.mac, addr, sizeof(entry.mac));
856 entry.portmask = BIT(port);
858 ret = __hellcreek_fdb_add(hellcreek, &entry);
860 dev_err(hellcreek->dev, "Failed to add FDB entry!\n");
865 ret = __hellcreek_fdb_del(hellcreek, &entry);
867 dev_err(hellcreek->dev, "Failed to delete FDB entry!\n");
871 entry.portmask |= BIT(port);
873 ret = __hellcreek_fdb_add(hellcreek, &entry);
875 dev_err(hellcreek->dev, "Failed to add FDB entry!\n");
881 mutex_unlock(&hellcreek->reg_lock);
886 static int hellcreek_fdb_del(struct dsa_switch *ds, int port,
887 const unsigned char *addr, u16 vid,
890 struct hellcreek_fdb_entry entry = { 0 };
891 struct hellcreek *hellcreek = ds->priv;
894 dev_dbg(hellcreek->dev, "Delete FDB entry for MAC=%pM\n", addr);
896 mutex_lock(&hellcreek->reg_lock);
898 ret = hellcreek_fdb_get(hellcreek, addr, &entry);
901 dev_err(hellcreek->dev, "FDB entry for deletion not found!\n");
904 ret = __hellcreek_fdb_del(hellcreek, &entry);
906 dev_err(hellcreek->dev, "Failed to delete FDB entry!\n");
910 entry.portmask &= ~BIT(port);
912 if (entry.portmask != 0x00) {
913 ret = __hellcreek_fdb_add(hellcreek, &entry);
915 dev_err(hellcreek->dev, "Failed to add FDB entry!\n");
922 mutex_unlock(&hellcreek->reg_lock);
927 static int hellcreek_fdb_dump(struct dsa_switch *ds, int port,
928 dsa_fdb_dump_cb_t *cb, void *data)
930 struct hellcreek *hellcreek = ds->priv;
935 mutex_lock(&hellcreek->reg_lock);
937 /* Set read pointer to zero: The read of HR_FDBMAX (read-only register)
938 * should reset the internal pointer. But, that doesn't work. The vendor
939 * suggested a subsequent write as workaround. Same for HR_FDBRDH below.
941 entries = hellcreek_read(hellcreek, HR_FDBMAX);
942 hellcreek_write(hellcreek, 0x00, HR_FDBMAX);
944 dev_dbg(hellcreek->dev, "FDB dump for port %d, entries=%d!\n", port, entries);
947 for (i = 0; i < hellcreek->fdb_entries; ++i) {
948 struct hellcreek_fdb_entry entry = { 0 };
951 hellcreek_populate_fdb_entry(hellcreek, &entry, i);
953 /* Force next entry */
954 hellcreek_write(hellcreek, 0x00, HR_FDBRDH);
957 if (is_zero_ether_addr(entry.mac))
960 /* Check port mask */
961 if (!(entry.portmask & BIT(port)))
964 ret = cb(entry.mac, 0, entry.is_static, data);
969 mutex_unlock(&hellcreek->reg_lock);
974 static int hellcreek_vlan_filtering(struct dsa_switch *ds, int port,
976 struct netlink_ext_ack *extack)
978 struct hellcreek *hellcreek = ds->priv;
980 dev_dbg(hellcreek->dev, "%s VLAN filtering on port %d\n",
981 vlan_filtering ? "Enable" : "Disable", port);
983 /* Configure port to drop packages with not known vids */
984 hellcreek_setup_ingressflt(hellcreek, port, vlan_filtering);
986 /* Enable VLAN awareness on the switch. This save due to
987 * ds->vlan_filtering_is_global.
989 hellcreek_setup_vlan_awareness(hellcreek, vlan_filtering);
994 static int hellcreek_enable_ip_core(struct hellcreek *hellcreek)
999 mutex_lock(&hellcreek->reg_lock);
1001 val = hellcreek_read(hellcreek, HR_CTRL_C);
1002 val |= HR_CTRL_C_ENABLE;
1003 hellcreek_write(hellcreek, val, HR_CTRL_C);
1004 ret = hellcreek_wait_until_transitioned(hellcreek);
1006 mutex_unlock(&hellcreek->reg_lock);
1011 static void hellcreek_setup_cpu_and_tunnel_port(struct hellcreek *hellcreek)
1013 struct hellcreek_port *tunnel_port = &hellcreek->ports[TUNNEL_PORT];
1014 struct hellcreek_port *cpu_port = &hellcreek->ports[CPU_PORT];
1017 ptcfg |= HR_PTCFG_LEARNING_EN | HR_PTCFG_ADMIN_EN;
1019 mutex_lock(&hellcreek->reg_lock);
1021 hellcreek_select_port(hellcreek, CPU_PORT);
1022 hellcreek_write(hellcreek, ptcfg, HR_PTCFG);
1024 hellcreek_select_port(hellcreek, TUNNEL_PORT);
1025 hellcreek_write(hellcreek, ptcfg, HR_PTCFG);
1027 cpu_port->ptcfg = ptcfg;
1028 tunnel_port->ptcfg = ptcfg;
1030 mutex_unlock(&hellcreek->reg_lock);
1033 static void hellcreek_setup_tc_identity_mapping(struct hellcreek *hellcreek)
1037 /* The switch has multiple egress queues per port. The queue is selected
1038 * via the PCP field in the VLAN header. The switch internally deals
1039 * with traffic classes instead of PCP values and this mapping is
1042 * The default mapping is (PCP - TC):
1052 * The default should be an identity mapping.
1055 for (i = 0; i < 8; ++i) {
1056 mutex_lock(&hellcreek->reg_lock);
1058 hellcreek_select_prio(hellcreek, i);
1059 hellcreek_write(hellcreek,
1060 i << HR_PRTCCFG_PCP_TC_MAP_SHIFT,
1063 mutex_unlock(&hellcreek->reg_lock);
1067 static int hellcreek_setup_fdb(struct hellcreek *hellcreek)
1069 static struct hellcreek_fdb_entry l2_ptp = {
1070 /* MAC: 01-1B-19-00-00-00 */
1071 .mac = { 0x01, 0x1b, 0x19, 0x00, 0x00, 0x00 },
1072 .portmask = 0x03, /* Management ports */
1077 .reprio_tc = 6, /* TC: 6 as per IEEE 802.1AS */
1080 static struct hellcreek_fdb_entry udp4_ptp = {
1081 /* MAC: 01-00-5E-00-01-81 */
1082 .mac = { 0x01, 0x00, 0x5e, 0x00, 0x01, 0x81 },
1083 .portmask = 0x03, /* Management ports */
1091 static struct hellcreek_fdb_entry udp6_ptp = {
1092 /* MAC: 33-33-00-00-01-81 */
1093 .mac = { 0x33, 0x33, 0x00, 0x00, 0x01, 0x81 },
1094 .portmask = 0x03, /* Management ports */
1102 static struct hellcreek_fdb_entry l2_p2p = {
1103 /* MAC: 01-80-C2-00-00-0E */
1104 .mac = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x0e },
1105 .portmask = 0x03, /* Management ports */
1110 .reprio_tc = 6, /* TC: 6 as per IEEE 802.1AS */
1113 static struct hellcreek_fdb_entry udp4_p2p = {
1114 /* MAC: 01-00-5E-00-00-6B */
1115 .mac = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x6b },
1116 .portmask = 0x03, /* Management ports */
1124 static struct hellcreek_fdb_entry udp6_p2p = {
1125 /* MAC: 33-33-00-00-00-6B */
1126 .mac = { 0x33, 0x33, 0x00, 0x00, 0x00, 0x6b },
1127 .portmask = 0x03, /* Management ports */
1135 static struct hellcreek_fdb_entry stp = {
1136 /* MAC: 01-80-C2-00-00-00 */
1137 .mac = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 },
1138 .portmask = 0x03, /* Management ports */
1148 mutex_lock(&hellcreek->reg_lock);
1149 ret = __hellcreek_fdb_add(hellcreek, &l2_ptp);
1152 ret = __hellcreek_fdb_add(hellcreek, &udp4_ptp);
1155 ret = __hellcreek_fdb_add(hellcreek, &udp6_ptp);
1158 ret = __hellcreek_fdb_add(hellcreek, &l2_p2p);
1161 ret = __hellcreek_fdb_add(hellcreek, &udp4_p2p);
1164 ret = __hellcreek_fdb_add(hellcreek, &udp6_p2p);
1167 ret = __hellcreek_fdb_add(hellcreek, &stp);
1169 mutex_unlock(&hellcreek->reg_lock);
1174 static int hellcreek_devlink_info_get(struct dsa_switch *ds,
1175 struct devlink_info_req *req,
1176 struct netlink_ext_ack *extack)
1178 struct hellcreek *hellcreek = ds->priv;
1181 ret = devlink_info_driver_name_put(req, "hellcreek");
1185 return devlink_info_version_fixed_put(req,
1186 DEVLINK_INFO_VERSION_GENERIC_ASIC_ID,
1187 hellcreek->pdata->name);
1190 static u64 hellcreek_devlink_vlan_table_get(void *priv)
1192 struct hellcreek *hellcreek = priv;
1196 mutex_lock(&hellcreek->reg_lock);
1197 for (i = 0; i < VLAN_N_VID; ++i)
1198 if (hellcreek->vidmbrcfg[i])
1200 mutex_unlock(&hellcreek->reg_lock);
1205 static u64 hellcreek_devlink_fdb_table_get(void *priv)
1207 struct hellcreek *hellcreek = priv;
1210 /* Reading this register has side effects. Synchronize against the other
1213 mutex_lock(&hellcreek->reg_lock);
1214 count = hellcreek_read(hellcreek, HR_FDBMAX);
1215 mutex_unlock(&hellcreek->reg_lock);
1220 static int hellcreek_setup_devlink_resources(struct dsa_switch *ds)
1222 struct devlink_resource_size_params size_vlan_params;
1223 struct devlink_resource_size_params size_fdb_params;
1224 struct hellcreek *hellcreek = ds->priv;
1227 devlink_resource_size_params_init(&size_vlan_params, VLAN_N_VID,
1229 1, DEVLINK_RESOURCE_UNIT_ENTRY);
1231 devlink_resource_size_params_init(&size_fdb_params,
1232 hellcreek->fdb_entries,
1233 hellcreek->fdb_entries,
1234 1, DEVLINK_RESOURCE_UNIT_ENTRY);
1236 err = dsa_devlink_resource_register(ds, "VLAN", VLAN_N_VID,
1237 HELLCREEK_DEVLINK_PARAM_ID_VLAN_TABLE,
1238 DEVLINK_RESOURCE_ID_PARENT_TOP,
1243 err = dsa_devlink_resource_register(ds, "FDB", hellcreek->fdb_entries,
1244 HELLCREEK_DEVLINK_PARAM_ID_FDB_TABLE,
1245 DEVLINK_RESOURCE_ID_PARENT_TOP,
1250 dsa_devlink_resource_occ_get_register(ds,
1251 HELLCREEK_DEVLINK_PARAM_ID_VLAN_TABLE,
1252 hellcreek_devlink_vlan_table_get,
1255 dsa_devlink_resource_occ_get_register(ds,
1256 HELLCREEK_DEVLINK_PARAM_ID_FDB_TABLE,
1257 hellcreek_devlink_fdb_table_get,
1263 dsa_devlink_resources_unregister(ds);
1268 static int hellcreek_devlink_region_vlan_snapshot(struct devlink *dl,
1269 const struct devlink_region_ops *ops,
1270 struct netlink_ext_ack *extack,
1273 struct hellcreek_devlink_vlan_entry *table, *entry;
1274 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
1275 struct hellcreek *hellcreek = ds->priv;
1278 table = kcalloc(VLAN_N_VID, sizeof(*entry), GFP_KERNEL);
1284 mutex_lock(&hellcreek->reg_lock);
1285 for (i = 0; i < VLAN_N_VID; ++i, ++entry) {
1286 entry->member = hellcreek->vidmbrcfg[i];
1289 mutex_unlock(&hellcreek->reg_lock);
1291 *data = (u8 *)table;
1296 static int hellcreek_devlink_region_fdb_snapshot(struct devlink *dl,
1297 const struct devlink_region_ops *ops,
1298 struct netlink_ext_ack *extack,
1301 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
1302 struct hellcreek_fdb_entry *table, *entry;
1303 struct hellcreek *hellcreek = ds->priv;
1306 table = kcalloc(hellcreek->fdb_entries, sizeof(*entry), GFP_KERNEL);
1312 mutex_lock(&hellcreek->reg_lock);
1314 /* Start table read */
1315 hellcreek_read(hellcreek, HR_FDBMAX);
1316 hellcreek_write(hellcreek, 0x00, HR_FDBMAX);
1318 for (i = 0; i < hellcreek->fdb_entries; ++i, ++entry) {
1319 /* Read current entry */
1320 hellcreek_populate_fdb_entry(hellcreek, entry, i);
1322 /* Advance read pointer */
1323 hellcreek_write(hellcreek, 0x00, HR_FDBRDH);
1326 mutex_unlock(&hellcreek->reg_lock);
1328 *data = (u8 *)table;
1333 static struct devlink_region_ops hellcreek_region_vlan_ops = {
1335 .snapshot = hellcreek_devlink_region_vlan_snapshot,
1336 .destructor = kfree,
1339 static struct devlink_region_ops hellcreek_region_fdb_ops = {
1341 .snapshot = hellcreek_devlink_region_fdb_snapshot,
1342 .destructor = kfree,
1345 static int hellcreek_setup_devlink_regions(struct dsa_switch *ds)
1347 struct hellcreek *hellcreek = ds->priv;
1348 struct devlink_region_ops *ops;
1349 struct devlink_region *region;
1354 size = VLAN_N_VID * sizeof(struct hellcreek_devlink_vlan_entry);
1355 ops = &hellcreek_region_vlan_ops;
1357 region = dsa_devlink_region_create(ds, ops, 1, size);
1359 return PTR_ERR(region);
1361 hellcreek->vlan_region = region;
1364 size = hellcreek->fdb_entries * sizeof(struct hellcreek_fdb_entry);
1365 ops = &hellcreek_region_fdb_ops;
1367 region = dsa_devlink_region_create(ds, ops, 1, size);
1368 if (IS_ERR(region)) {
1369 ret = PTR_ERR(region);
1373 hellcreek->fdb_region = region;
1378 dsa_devlink_region_destroy(hellcreek->vlan_region);
1383 static void hellcreek_teardown_devlink_regions(struct dsa_switch *ds)
1385 struct hellcreek *hellcreek = ds->priv;
1387 dsa_devlink_region_destroy(hellcreek->fdb_region);
1388 dsa_devlink_region_destroy(hellcreek->vlan_region);
1391 static int hellcreek_setup(struct dsa_switch *ds)
1393 struct hellcreek *hellcreek = ds->priv;
1397 dev_dbg(hellcreek->dev, "Set up the switch\n");
1400 ret = hellcreek_enable_ip_core(hellcreek);
1402 dev_err(hellcreek->dev, "Failed to enable IP core!\n");
1406 /* Enable CPU/Tunnel ports */
1407 hellcreek_setup_cpu_and_tunnel_port(hellcreek);
1409 /* Switch config: Keep defaults, enable FDB aging and learning and tag
1410 * each frame from/to cpu port for DSA tagging. Also enable the length
1411 * aware shaping mode. This eliminates the need for Qbv guard bands.
1413 swcfg |= HR_SWCFG_FDBAGE_EN |
1414 HR_SWCFG_FDBLRN_EN |
1415 HR_SWCFG_ALWAYS_OBT |
1416 (HR_SWCFG_LAS_ON << HR_SWCFG_LAS_MODE_SHIFT);
1417 hellcreek->swcfg = swcfg;
1418 hellcreek_write(hellcreek, swcfg, HR_SWCFG);
1420 /* Initial vlan membership to reflect port separation */
1421 for (i = 0; i < ds->num_ports; ++i) {
1422 if (!dsa_is_user_port(ds, i))
1425 hellcreek_setup_vlan_membership(ds, i, true);
1428 /* Configure PCP <-> TC mapping */
1429 hellcreek_setup_tc_identity_mapping(hellcreek);
1431 /* The VLAN awareness is a global switch setting. Therefore, mixed vlan
1432 * filtering setups are not supported.
1434 ds->vlan_filtering_is_global = true;
1435 ds->needs_standalone_vlan_filtering = true;
1437 /* Intercept _all_ PTP multicast traffic */
1438 ret = hellcreek_setup_fdb(hellcreek);
1440 dev_err(hellcreek->dev,
1441 "Failed to insert static PTP FDB entries\n");
1445 /* Register devlink resources with DSA */
1446 ret = hellcreek_setup_devlink_resources(ds);
1448 dev_err(hellcreek->dev,
1449 "Failed to setup devlink resources!\n");
1453 ret = hellcreek_setup_devlink_regions(ds);
1455 dev_err(hellcreek->dev,
1456 "Failed to setup devlink regions!\n");
1463 dsa_devlink_resources_unregister(ds);
1468 static void hellcreek_teardown(struct dsa_switch *ds)
1470 hellcreek_teardown_devlink_regions(ds);
1471 dsa_devlink_resources_unregister(ds);
1474 static void hellcreek_phylink_get_caps(struct dsa_switch *ds, int port,
1475 struct phylink_config *config)
1477 struct hellcreek *hellcreek = ds->priv;
1479 __set_bit(PHY_INTERFACE_MODE_MII, config->supported_interfaces);
1480 __set_bit(PHY_INTERFACE_MODE_RGMII, config->supported_interfaces);
1482 /* Include GMII - the hardware does not support this interface
1483 * mode, but it's the default interface mode for phylib, so we
1484 * need it for compatibility with existing DT.
1486 __set_bit(PHY_INTERFACE_MODE_GMII, config->supported_interfaces);
1488 /* The MAC settings are a hardware configuration option and cannot be
1489 * changed at run time or by strapping. Therefore the attached PHYs
1490 * should be programmed to only advertise settings which are supported
1493 if (hellcreek->pdata->is_100_mbits)
1494 config->mac_capabilities = MAC_100FD;
1496 config->mac_capabilities = MAC_1000FD;
1500 hellcreek_port_prechangeupper(struct dsa_switch *ds, int port,
1501 struct netdev_notifier_changeupper_info *info)
1503 struct hellcreek *hellcreek = ds->priv;
1509 dev_dbg(hellcreek->dev, "Pre change upper for port %d\n", port);
1512 * Deny VLAN devices on top of lan ports with the same VLAN ids, because
1513 * it breaks the port separation due to the private VLANs. Example:
1515 * lan0.100 *and* lan1.100 cannot be used in parallel. However, lan0.99
1516 * and lan1.100 works.
1519 if (!is_vlan_dev(info->upper_dev))
1522 vid = vlan_dev_vlan_id(info->upper_dev);
1524 /* For all ports, check bitmaps */
1525 mutex_lock(&hellcreek->vlan_lock);
1526 for (i = 0; i < hellcreek->pdata->num_ports; ++i) {
1527 if (!dsa_is_user_port(ds, i))
1533 used = used && test_bit(vid, hellcreek->ports[i].vlan_dev_bitmap);
1540 set_bit(vid, hellcreek->ports[port].vlan_dev_bitmap);
1545 mutex_unlock(&hellcreek->vlan_lock);
1550 static void hellcreek_setup_maxsdu(struct hellcreek *hellcreek, int port,
1551 const struct tc_taprio_qopt_offload *schedule)
1555 for (tc = 0; tc < 8; ++tc) {
1556 u32 max_sdu = schedule->max_sdu[tc] + VLAN_ETH_HLEN - ETH_FCS_LEN;
1559 if (!schedule->max_sdu[tc])
1562 dev_dbg(hellcreek->dev, "Configure max-sdu %u for tc %d on port %d\n",
1565 hellcreek_select_port_prio(hellcreek, port, tc);
1567 val = (max_sdu & HR_PTPRTCCFG_MAXSDU_MASK) << HR_PTPRTCCFG_MAXSDU_SHIFT;
1569 hellcreek_write(hellcreek, val, HR_PTPRTCCFG);
1573 static void hellcreek_reset_maxsdu(struct hellcreek *hellcreek, int port)
1577 for (tc = 0; tc < 8; ++tc) {
1580 hellcreek_select_port_prio(hellcreek, port, tc);
1582 val = (HELLCREEK_DEFAULT_MAX_SDU & HR_PTPRTCCFG_MAXSDU_MASK)
1583 << HR_PTPRTCCFG_MAXSDU_SHIFT;
1585 hellcreek_write(hellcreek, val, HR_PTPRTCCFG);
1589 static void hellcreek_setup_gcl(struct hellcreek *hellcreek, int port,
1590 const struct tc_taprio_qopt_offload *schedule)
1592 const struct tc_taprio_sched_entry *cur, *initial, *next;
1595 cur = initial = &schedule->entries[0];
1598 for (i = 1; i <= schedule->num_entries; ++i) {
1602 if (i == schedule->num_entries)
1603 gates = initial->gate_mask ^
1606 gates = next->gate_mask ^
1611 if (i == schedule->num_entries)
1612 data |= TR_GCLDAT_GCLWRLAST;
1615 hellcreek_write(hellcreek, data, TR_GCLDAT);
1618 hellcreek_write(hellcreek,
1619 cur->interval & 0x0000ffff,
1621 hellcreek_write(hellcreek,
1622 (cur->interval & 0xffff0000) >> 16,
1626 data = ((i - 1) << TR_GCLCMD_GCLWRADR_SHIFT) |
1627 (initial->gate_mask <<
1628 TR_GCLCMD_INIT_GATE_STATES_SHIFT);
1629 hellcreek_write(hellcreek, data, TR_GCLCMD);
1636 static void hellcreek_set_cycle_time(struct hellcreek *hellcreek,
1637 const struct tc_taprio_qopt_offload *schedule)
1639 u32 cycle_time = schedule->cycle_time;
1641 hellcreek_write(hellcreek, cycle_time & 0x0000ffff, TR_CTWRL);
1642 hellcreek_write(hellcreek, (cycle_time & 0xffff0000) >> 16, TR_CTWRH);
1645 static void hellcreek_switch_schedule(struct hellcreek *hellcreek,
1648 struct timespec64 ts = ktime_to_timespec64(start_time);
1650 /* Start schedule at this point of time */
1651 hellcreek_write(hellcreek, ts.tv_nsec & 0x0000ffff, TR_ESTWRL);
1652 hellcreek_write(hellcreek, (ts.tv_nsec & 0xffff0000) >> 16, TR_ESTWRH);
1654 /* Arm timer, set seconds and switch schedule */
1655 hellcreek_write(hellcreek, TR_ESTCMD_ESTARM | TR_ESTCMD_ESTSWCFG |
1656 ((ts.tv_sec & TR_ESTCMD_ESTSEC_MASK) <<
1657 TR_ESTCMD_ESTSEC_SHIFT), TR_ESTCMD);
1660 static bool hellcreek_schedule_startable(struct hellcreek *hellcreek, int port)
1662 struct hellcreek_port *hellcreek_port = &hellcreek->ports[port];
1663 s64 base_time_ns, current_ns;
1665 /* The switch allows a schedule to be started only eight seconds within
1666 * the future. Therefore, check the current PTP time if the schedule is
1670 /* Use the "cached" time. That should be alright, as it's updated quite
1671 * frequently in the PTP code.
1673 mutex_lock(&hellcreek->ptp_lock);
1674 current_ns = hellcreek->seconds * NSEC_PER_SEC + hellcreek->last_ts;
1675 mutex_unlock(&hellcreek->ptp_lock);
1677 /* Calculate difference to admin base time */
1678 base_time_ns = ktime_to_ns(hellcreek_port->current_schedule->base_time);
1680 return base_time_ns - current_ns < (s64)4 * NSEC_PER_SEC;
1683 static void hellcreek_start_schedule(struct hellcreek *hellcreek, int port)
1685 struct hellcreek_port *hellcreek_port = &hellcreek->ports[port];
1686 ktime_t base_time, current_time;
1690 /* First select port */
1691 hellcreek_select_tgd(hellcreek, port);
1693 /* Forward base time into the future if needed */
1694 mutex_lock(&hellcreek->ptp_lock);
1695 current_ns = hellcreek->seconds * NSEC_PER_SEC + hellcreek->last_ts;
1696 mutex_unlock(&hellcreek->ptp_lock);
1698 current_time = ns_to_ktime(current_ns);
1699 base_time = hellcreek_port->current_schedule->base_time;
1700 cycle_time = hellcreek_port->current_schedule->cycle_time;
1702 if (ktime_compare(current_time, base_time) > 0) {
1705 n = div64_s64(ktime_sub_ns(current_time, base_time),
1707 base_time = ktime_add_ns(base_time, (n + 1) * cycle_time);
1710 /* Set admin base time and switch schedule */
1711 hellcreek_switch_schedule(hellcreek, base_time);
1713 taprio_offload_free(hellcreek_port->current_schedule);
1714 hellcreek_port->current_schedule = NULL;
1716 dev_dbg(hellcreek->dev, "Armed EST timer for port %d\n",
1717 hellcreek_port->port);
1720 static void hellcreek_check_schedule(struct work_struct *work)
1722 struct delayed_work *dw = to_delayed_work(work);
1723 struct hellcreek_port *hellcreek_port;
1724 struct hellcreek *hellcreek;
1727 hellcreek_port = dw_to_hellcreek_port(dw);
1728 hellcreek = hellcreek_port->hellcreek;
1730 mutex_lock(&hellcreek->reg_lock);
1732 /* Check starting time */
1733 startable = hellcreek_schedule_startable(hellcreek,
1734 hellcreek_port->port);
1736 hellcreek_start_schedule(hellcreek, hellcreek_port->port);
1737 mutex_unlock(&hellcreek->reg_lock);
1741 mutex_unlock(&hellcreek->reg_lock);
1744 schedule_delayed_work(&hellcreek_port->schedule_work,
1745 HELLCREEK_SCHEDULE_PERIOD);
1748 static int hellcreek_port_set_schedule(struct dsa_switch *ds, int port,
1749 struct tc_taprio_qopt_offload *taprio)
1751 struct hellcreek *hellcreek = ds->priv;
1752 struct hellcreek_port *hellcreek_port;
1756 hellcreek_port = &hellcreek->ports[port];
1758 dev_dbg(hellcreek->dev, "Configure traffic schedule on port %d\n",
1761 /* First cancel delayed work */
1762 cancel_delayed_work_sync(&hellcreek_port->schedule_work);
1764 mutex_lock(&hellcreek->reg_lock);
1766 if (hellcreek_port->current_schedule) {
1767 taprio_offload_free(hellcreek_port->current_schedule);
1768 hellcreek_port->current_schedule = NULL;
1770 hellcreek_port->current_schedule = taprio_offload_get(taprio);
1772 /* Configure max sdu */
1773 hellcreek_setup_maxsdu(hellcreek, port, hellcreek_port->current_schedule);
1776 hellcreek_select_tgd(hellcreek, port);
1778 /* Enable gating and keep defaults */
1779 ctrl = (0xff << TR_TGDCTRL_ADMINGATESTATES_SHIFT) | TR_TGDCTRL_GATE_EN;
1780 hellcreek_write(hellcreek, ctrl, TR_TGDCTRL);
1782 /* Cancel pending schedule */
1783 hellcreek_write(hellcreek, 0x00, TR_ESTCMD);
1785 /* Setup a new schedule */
1786 hellcreek_setup_gcl(hellcreek, port, hellcreek_port->current_schedule);
1788 /* Configure cycle time */
1789 hellcreek_set_cycle_time(hellcreek, hellcreek_port->current_schedule);
1791 /* Check starting time */
1792 startable = hellcreek_schedule_startable(hellcreek, port);
1794 hellcreek_start_schedule(hellcreek, port);
1795 mutex_unlock(&hellcreek->reg_lock);
1799 mutex_unlock(&hellcreek->reg_lock);
1801 /* Schedule periodic schedule check */
1802 schedule_delayed_work(&hellcreek_port->schedule_work,
1803 HELLCREEK_SCHEDULE_PERIOD);
1808 static int hellcreek_port_del_schedule(struct dsa_switch *ds, int port)
1810 struct hellcreek *hellcreek = ds->priv;
1811 struct hellcreek_port *hellcreek_port;
1813 hellcreek_port = &hellcreek->ports[port];
1815 dev_dbg(hellcreek->dev, "Remove traffic schedule on port %d\n", port);
1817 /* First cancel delayed work */
1818 cancel_delayed_work_sync(&hellcreek_port->schedule_work);
1820 mutex_lock(&hellcreek->reg_lock);
1822 if (hellcreek_port->current_schedule) {
1823 taprio_offload_free(hellcreek_port->current_schedule);
1824 hellcreek_port->current_schedule = NULL;
1828 hellcreek_reset_maxsdu(hellcreek, port);
1831 hellcreek_select_tgd(hellcreek, port);
1833 /* Disable gating and return to regular switching flow */
1834 hellcreek_write(hellcreek, 0xff << TR_TGDCTRL_ADMINGATESTATES_SHIFT,
1837 mutex_unlock(&hellcreek->reg_lock);
1842 static bool hellcreek_validate_schedule(struct hellcreek *hellcreek,
1843 struct tc_taprio_qopt_offload *schedule)
1847 /* Does this hellcreek version support Qbv in hardware? */
1848 if (!hellcreek->pdata->qbv_support)
1851 /* cycle time can only be 32bit */
1852 if (schedule->cycle_time > (u32)-1)
1855 /* cycle time extension is not supported */
1856 if (schedule->cycle_time_extension)
1859 /* Only set command is supported */
1860 for (i = 0; i < schedule->num_entries; ++i)
1861 if (schedule->entries[i].command != TC_TAPRIO_CMD_SET_GATES)
1867 static int hellcreek_tc_query_caps(struct tc_query_caps_base *base)
1869 switch (base->type) {
1870 case TC_SETUP_QDISC_TAPRIO: {
1871 struct tc_taprio_caps *caps = base->caps;
1873 caps->supports_queue_max_sdu = true;
1882 static int hellcreek_port_setup_tc(struct dsa_switch *ds, int port,
1883 enum tc_setup_type type, void *type_data)
1885 struct hellcreek *hellcreek = ds->priv;
1889 return hellcreek_tc_query_caps(type_data);
1890 case TC_SETUP_QDISC_TAPRIO: {
1891 struct tc_taprio_qopt_offload *taprio = type_data;
1893 if (!hellcreek_validate_schedule(hellcreek, taprio))
1897 return hellcreek_port_set_schedule(ds, port, taprio);
1899 return hellcreek_port_del_schedule(ds, port);
1906 static const struct dsa_switch_ops hellcreek_ds_ops = {
1907 .devlink_info_get = hellcreek_devlink_info_get,
1908 .get_ethtool_stats = hellcreek_get_ethtool_stats,
1909 .get_sset_count = hellcreek_get_sset_count,
1910 .get_strings = hellcreek_get_strings,
1911 .get_tag_protocol = hellcreek_get_tag_protocol,
1912 .get_ts_info = hellcreek_get_ts_info,
1913 .phylink_get_caps = hellcreek_phylink_get_caps,
1914 .port_bridge_flags = hellcreek_bridge_flags,
1915 .port_bridge_join = hellcreek_port_bridge_join,
1916 .port_bridge_leave = hellcreek_port_bridge_leave,
1917 .port_disable = hellcreek_port_disable,
1918 .port_enable = hellcreek_port_enable,
1919 .port_fdb_add = hellcreek_fdb_add,
1920 .port_fdb_del = hellcreek_fdb_del,
1921 .port_fdb_dump = hellcreek_fdb_dump,
1922 .port_hwtstamp_set = hellcreek_port_hwtstamp_set,
1923 .port_hwtstamp_get = hellcreek_port_hwtstamp_get,
1924 .port_pre_bridge_flags = hellcreek_pre_bridge_flags,
1925 .port_prechangeupper = hellcreek_port_prechangeupper,
1926 .port_rxtstamp = hellcreek_port_rxtstamp,
1927 .port_setup_tc = hellcreek_port_setup_tc,
1928 .port_stp_state_set = hellcreek_port_stp_state_set,
1929 .port_txtstamp = hellcreek_port_txtstamp,
1930 .port_vlan_add = hellcreek_vlan_add,
1931 .port_vlan_del = hellcreek_vlan_del,
1932 .port_vlan_filtering = hellcreek_vlan_filtering,
1933 .setup = hellcreek_setup,
1934 .teardown = hellcreek_teardown,
1937 static int hellcreek_probe(struct platform_device *pdev)
1939 struct device *dev = &pdev->dev;
1940 struct hellcreek *hellcreek;
1941 struct resource *res;
1944 hellcreek = devm_kzalloc(dev, sizeof(*hellcreek), GFP_KERNEL);
1948 hellcreek->vidmbrcfg = devm_kcalloc(dev, VLAN_N_VID,
1949 sizeof(*hellcreek->vidmbrcfg),
1951 if (!hellcreek->vidmbrcfg)
1954 hellcreek->pdata = of_device_get_match_data(dev);
1956 hellcreek->ports = devm_kcalloc(dev, hellcreek->pdata->num_ports,
1957 sizeof(*hellcreek->ports),
1959 if (!hellcreek->ports)
1962 for (i = 0; i < hellcreek->pdata->num_ports; ++i) {
1963 struct hellcreek_port *port = &hellcreek->ports[i];
1965 port->counter_values =
1967 ARRAY_SIZE(hellcreek_counter),
1968 sizeof(*port->counter_values),
1970 if (!port->counter_values)
1973 port->vlan_dev_bitmap = devm_bitmap_zalloc(dev, VLAN_N_VID,
1975 if (!port->vlan_dev_bitmap)
1978 port->hellcreek = hellcreek;
1981 INIT_DELAYED_WORK(&port->schedule_work,
1982 hellcreek_check_schedule);
1985 mutex_init(&hellcreek->reg_lock);
1986 mutex_init(&hellcreek->vlan_lock);
1987 mutex_init(&hellcreek->ptp_lock);
1989 hellcreek->dev = dev;
1991 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "tsn");
1993 dev_err(dev, "No memory region provided!\n");
1997 hellcreek->base = devm_ioremap_resource(dev, res);
1998 if (IS_ERR(hellcreek->base))
1999 return PTR_ERR(hellcreek->base);
2001 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ptp");
2003 dev_err(dev, "No PTP memory region provided!\n");
2007 hellcreek->ptp_base = devm_ioremap_resource(dev, res);
2008 if (IS_ERR(hellcreek->ptp_base))
2009 return PTR_ERR(hellcreek->ptp_base);
2011 ret = hellcreek_detect(hellcreek);
2013 dev_err(dev, "No (known) chip found!\n");
2017 ret = hellcreek_wait_until_ready(hellcreek);
2019 dev_err(dev, "Switch didn't become ready!\n");
2023 hellcreek_feature_detect(hellcreek);
2025 hellcreek->ds = devm_kzalloc(dev, sizeof(*hellcreek->ds), GFP_KERNEL);
2029 hellcreek->ds->dev = dev;
2030 hellcreek->ds->priv = hellcreek;
2031 hellcreek->ds->ops = &hellcreek_ds_ops;
2032 hellcreek->ds->num_ports = hellcreek->pdata->num_ports;
2033 hellcreek->ds->num_tx_queues = HELLCREEK_NUM_EGRESS_QUEUES;
2035 ret = dsa_register_switch(hellcreek->ds);
2037 dev_err_probe(dev, ret, "Unable to register switch\n");
2041 ret = hellcreek_ptp_setup(hellcreek);
2043 dev_err(dev, "Failed to setup PTP!\n");
2047 ret = hellcreek_hwtstamp_setup(hellcreek);
2049 dev_err(dev, "Failed to setup hardware timestamping!\n");
2050 goto err_tstamp_setup;
2053 platform_set_drvdata(pdev, hellcreek);
2058 hellcreek_ptp_free(hellcreek);
2060 dsa_unregister_switch(hellcreek->ds);
2065 static int hellcreek_remove(struct platform_device *pdev)
2067 struct hellcreek *hellcreek = platform_get_drvdata(pdev);
2072 hellcreek_hwtstamp_free(hellcreek);
2073 hellcreek_ptp_free(hellcreek);
2074 dsa_unregister_switch(hellcreek->ds);
2079 static void hellcreek_shutdown(struct platform_device *pdev)
2081 struct hellcreek *hellcreek = platform_get_drvdata(pdev);
2086 dsa_switch_shutdown(hellcreek->ds);
2088 platform_set_drvdata(pdev, NULL);
2091 static const struct hellcreek_platform_data de1soc_r1_pdata = {
2096 .qbv_on_cpu_port = 1,
2098 .module_id = 0x4c30,
2101 static const struct of_device_id hellcreek_of_match[] = {
2103 .compatible = "hirschmann,hellcreek-de1soc-r1",
2104 .data = &de1soc_r1_pdata,
2108 MODULE_DEVICE_TABLE(of, hellcreek_of_match);
2110 static struct platform_driver hellcreek_driver = {
2111 .probe = hellcreek_probe,
2112 .remove = hellcreek_remove,
2113 .shutdown = hellcreek_shutdown,
2115 .name = "hellcreek",
2116 .of_match_table = hellcreek_of_match,
2119 module_platform_driver(hellcreek_driver);
2121 MODULE_AUTHOR("Kurt Kanzenbach <kurt@linutronix.de>");
2122 MODULE_DESCRIPTION("Hirschmann Hellcreek driver");
2123 MODULE_LICENSE("Dual MIT/GPL");