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_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 void hellcreek_select_tgd(struct hellcreek *hellcreek, int port)
158 u16 val = port << TR_TGDSEL_TDGSEL_SHIFT;
160 hellcreek_write(hellcreek, val, TR_TGDSEL);
163 static int hellcreek_wait_until_ready(struct hellcreek *hellcreek)
167 /* Wait up to 1ms, although 3 us should be enough */
168 return readx_poll_timeout(hellcreek_read_ctrl, hellcreek,
169 val, val & HR_CTRL_C_READY,
173 static int hellcreek_wait_until_transitioned(struct hellcreek *hellcreek)
177 return readx_poll_timeout_atomic(hellcreek_read_ctrl, hellcreek,
178 val, !(val & HR_CTRL_C_TRANSITION),
182 static int hellcreek_wait_fdb_ready(struct hellcreek *hellcreek)
186 return readx_poll_timeout_atomic(hellcreek_read_stat, hellcreek,
187 val, !(val & HR_SWSTAT_BUSY),
191 static int hellcreek_detect(struct hellcreek *hellcreek)
193 u16 id, rel_low, rel_high, date_low, date_high, tgd_ver;
197 id = hellcreek_read(hellcreek, HR_MODID_C);
198 rel_low = hellcreek_read(hellcreek, HR_REL_L_C);
199 rel_high = hellcreek_read(hellcreek, HR_REL_H_C);
200 date_low = hellcreek_read(hellcreek, HR_BLD_L_C);
201 date_high = hellcreek_read(hellcreek, HR_BLD_H_C);
202 tgd_ver = hellcreek_read(hellcreek, TR_TGDVER);
204 if (id != hellcreek->pdata->module_id)
207 rel = rel_low | (rel_high << 16);
208 date = date_low | (date_high << 16);
209 tgd_maj = (tgd_ver & TR_TGDVER_REV_MAJ_MASK) >> TR_TGDVER_REV_MAJ_SHIFT;
210 tgd_min = (tgd_ver & TR_TGDVER_REV_MIN_MASK) >> TR_TGDVER_REV_MIN_SHIFT;
212 dev_info(hellcreek->dev, "Module ID=%02x Release=%04x Date=%04x TGD Version=%02x.%02x\n",
213 id, rel, date, tgd_maj, tgd_min);
218 static void hellcreek_feature_detect(struct hellcreek *hellcreek)
222 features = hellcreek_read(hellcreek, HR_FEABITS0);
224 /* Only detect the size of the FDB table. The size and current
225 * utilization can be queried via devlink.
227 hellcreek->fdb_entries = ((features & HR_FEABITS0_FDBBINS_MASK) >>
228 HR_FEABITS0_FDBBINS_SHIFT) * 32;
231 static enum dsa_tag_protocol hellcreek_get_tag_protocol(struct dsa_switch *ds,
233 enum dsa_tag_protocol mp)
235 return DSA_TAG_PROTO_HELLCREEK;
238 static int hellcreek_port_enable(struct dsa_switch *ds, int port,
239 struct phy_device *phy)
241 struct hellcreek *hellcreek = ds->priv;
242 struct hellcreek_port *hellcreek_port;
245 hellcreek_port = &hellcreek->ports[port];
247 dev_dbg(hellcreek->dev, "Enable port %d\n", port);
249 mutex_lock(&hellcreek->reg_lock);
251 hellcreek_select_port(hellcreek, port);
252 val = hellcreek_port->ptcfg;
253 val |= HR_PTCFG_ADMIN_EN;
254 hellcreek_write(hellcreek, val, HR_PTCFG);
255 hellcreek_port->ptcfg = val;
257 mutex_unlock(&hellcreek->reg_lock);
262 static void hellcreek_port_disable(struct dsa_switch *ds, int port)
264 struct hellcreek *hellcreek = ds->priv;
265 struct hellcreek_port *hellcreek_port;
268 hellcreek_port = &hellcreek->ports[port];
270 dev_dbg(hellcreek->dev, "Disable port %d\n", port);
272 mutex_lock(&hellcreek->reg_lock);
274 hellcreek_select_port(hellcreek, port);
275 val = hellcreek_port->ptcfg;
276 val &= ~HR_PTCFG_ADMIN_EN;
277 hellcreek_write(hellcreek, val, HR_PTCFG);
278 hellcreek_port->ptcfg = val;
280 mutex_unlock(&hellcreek->reg_lock);
283 static void hellcreek_get_strings(struct dsa_switch *ds, int port,
284 u32 stringset, uint8_t *data)
288 for (i = 0; i < ARRAY_SIZE(hellcreek_counter); ++i) {
289 const struct hellcreek_counter *counter = &hellcreek_counter[i];
291 strlcpy(data + i * ETH_GSTRING_LEN,
292 counter->name, ETH_GSTRING_LEN);
296 static int hellcreek_get_sset_count(struct dsa_switch *ds, int port, int sset)
298 if (sset != ETH_SS_STATS)
301 return ARRAY_SIZE(hellcreek_counter);
304 static void hellcreek_get_ethtool_stats(struct dsa_switch *ds, int port,
307 struct hellcreek *hellcreek = ds->priv;
308 struct hellcreek_port *hellcreek_port;
311 hellcreek_port = &hellcreek->ports[port];
313 for (i = 0; i < ARRAY_SIZE(hellcreek_counter); ++i) {
314 const struct hellcreek_counter *counter = &hellcreek_counter[i];
315 u8 offset = counter->offset + port * 64;
319 mutex_lock(&hellcreek->reg_lock);
321 hellcreek_select_counter(hellcreek, offset);
323 /* The registers are locked internally by selecting the
324 * counter. So low and high can be read without reading high
327 high = hellcreek_read(hellcreek, HR_CRDH);
328 low = hellcreek_read(hellcreek, HR_CRDL);
329 value = ((u64)high << 16) | low;
331 hellcreek_port->counter_values[i] += value;
332 data[i] = hellcreek_port->counter_values[i];
334 mutex_unlock(&hellcreek->reg_lock);
338 static u16 hellcreek_private_vid(int port)
340 return VLAN_N_VID - port + 1;
343 static int hellcreek_vlan_prepare(struct dsa_switch *ds, int port,
344 const struct switchdev_obj_port_vlan *vlan,
345 struct netlink_ext_ack *extack)
347 struct hellcreek *hellcreek = ds->priv;
350 dev_dbg(hellcreek->dev, "VLAN prepare for port %d\n", port);
352 /* Restriction: Make sure that nobody uses the "private" VLANs. These
353 * VLANs are internally used by the driver to ensure port
354 * separation. Thus, they cannot be used by someone else.
356 for (i = 0; i < hellcreek->pdata->num_ports; ++i) {
357 const u16 restricted_vid = hellcreek_private_vid(i);
359 if (!dsa_is_user_port(ds, i))
362 if (vlan->vid == restricted_vid) {
363 NL_SET_ERR_MSG_MOD(extack, "VID restricted by driver");
371 static void hellcreek_select_vlan_params(struct hellcreek *hellcreek, int port,
372 int *shift, int *mask)
376 *shift = HR_VIDMBRCFG_P0MBR_SHIFT;
377 *mask = HR_VIDMBRCFG_P0MBR_MASK;
380 *shift = HR_VIDMBRCFG_P1MBR_SHIFT;
381 *mask = HR_VIDMBRCFG_P1MBR_MASK;
384 *shift = HR_VIDMBRCFG_P2MBR_SHIFT;
385 *mask = HR_VIDMBRCFG_P2MBR_MASK;
388 *shift = HR_VIDMBRCFG_P3MBR_SHIFT;
389 *mask = HR_VIDMBRCFG_P3MBR_MASK;
393 dev_err(hellcreek->dev, "Unknown port %d selected!\n", port);
397 static void hellcreek_apply_vlan(struct hellcreek *hellcreek, int port, u16 vid,
398 bool pvid, bool untagged)
403 dev_dbg(hellcreek->dev, "Apply VLAN: port=%d vid=%u pvid=%d untagged=%d",
404 port, vid, pvid, untagged);
406 mutex_lock(&hellcreek->reg_lock);
408 hellcreek_select_port(hellcreek, port);
409 hellcreek_select_vlan(hellcreek, vid, pvid);
411 /* Setup port vlan membership */
412 hellcreek_select_vlan_params(hellcreek, port, &shift, &mask);
413 val = hellcreek->vidmbrcfg[vid];
416 val |= HELLCREEK_VLAN_UNTAGGED_MEMBER << shift;
418 val |= HELLCREEK_VLAN_TAGGED_MEMBER << shift;
420 hellcreek_write(hellcreek, val, HR_VIDMBRCFG);
421 hellcreek->vidmbrcfg[vid] = val;
423 mutex_unlock(&hellcreek->reg_lock);
426 static void hellcreek_unapply_vlan(struct hellcreek *hellcreek, int port,
432 dev_dbg(hellcreek->dev, "Unapply VLAN: port=%d vid=%u\n", port, vid);
434 mutex_lock(&hellcreek->reg_lock);
436 hellcreek_select_vlan(hellcreek, vid, false);
438 /* Setup port vlan membership */
439 hellcreek_select_vlan_params(hellcreek, port, &shift, &mask);
440 val = hellcreek->vidmbrcfg[vid];
442 val |= HELLCREEK_VLAN_NO_MEMBER << shift;
444 hellcreek_write(hellcreek, val, HR_VIDMBRCFG);
445 hellcreek->vidmbrcfg[vid] = val;
447 mutex_unlock(&hellcreek->reg_lock);
450 static int hellcreek_vlan_add(struct dsa_switch *ds, int port,
451 const struct switchdev_obj_port_vlan *vlan,
452 struct netlink_ext_ack *extack)
454 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
455 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
456 struct hellcreek *hellcreek = ds->priv;
459 err = hellcreek_vlan_prepare(ds, port, vlan, extack);
463 dev_dbg(hellcreek->dev, "Add VLAN %d on port %d, %s, %s\n",
464 vlan->vid, port, untagged ? "untagged" : "tagged",
465 pvid ? "PVID" : "no PVID");
467 hellcreek_apply_vlan(hellcreek, port, vlan->vid, pvid, untagged);
472 static int hellcreek_vlan_del(struct dsa_switch *ds, int port,
473 const struct switchdev_obj_port_vlan *vlan)
475 struct hellcreek *hellcreek = ds->priv;
477 dev_dbg(hellcreek->dev, "Remove VLAN %d on port %d\n", vlan->vid, port);
479 hellcreek_unapply_vlan(hellcreek, port, vlan->vid);
484 static void hellcreek_port_stp_state_set(struct dsa_switch *ds, int port,
487 struct hellcreek *hellcreek = ds->priv;
488 struct hellcreek_port *hellcreek_port;
489 const char *new_state;
492 mutex_lock(&hellcreek->reg_lock);
494 hellcreek_port = &hellcreek->ports[port];
495 val = hellcreek_port->ptcfg;
498 case BR_STATE_DISABLED:
499 new_state = "DISABLED";
500 val |= HR_PTCFG_BLOCKED;
501 val &= ~HR_PTCFG_LEARNING_EN;
503 case BR_STATE_BLOCKING:
504 new_state = "BLOCKING";
505 val |= HR_PTCFG_BLOCKED;
506 val &= ~HR_PTCFG_LEARNING_EN;
508 case BR_STATE_LISTENING:
509 new_state = "LISTENING";
510 val |= HR_PTCFG_BLOCKED;
511 val &= ~HR_PTCFG_LEARNING_EN;
513 case BR_STATE_LEARNING:
514 new_state = "LEARNING";
515 val |= HR_PTCFG_BLOCKED;
516 val |= HR_PTCFG_LEARNING_EN;
518 case BR_STATE_FORWARDING:
519 new_state = "FORWARDING";
520 val &= ~HR_PTCFG_BLOCKED;
521 val |= HR_PTCFG_LEARNING_EN;
524 new_state = "UNKNOWN";
527 hellcreek_select_port(hellcreek, port);
528 hellcreek_write(hellcreek, val, HR_PTCFG);
529 hellcreek_port->ptcfg = val;
531 mutex_unlock(&hellcreek->reg_lock);
533 dev_dbg(hellcreek->dev, "Configured STP state for port %d: %s\n",
537 static void hellcreek_setup_ingressflt(struct hellcreek *hellcreek, int port,
540 struct hellcreek_port *hellcreek_port = &hellcreek->ports[port];
543 mutex_lock(&hellcreek->reg_lock);
545 ptcfg = hellcreek_port->ptcfg;
548 ptcfg |= HR_PTCFG_INGRESSFLT;
550 ptcfg &= ~HR_PTCFG_INGRESSFLT;
552 hellcreek_select_port(hellcreek, port);
553 hellcreek_write(hellcreek, ptcfg, HR_PTCFG);
554 hellcreek_port->ptcfg = ptcfg;
556 mutex_unlock(&hellcreek->reg_lock);
559 static void hellcreek_setup_vlan_awareness(struct hellcreek *hellcreek,
564 mutex_lock(&hellcreek->reg_lock);
566 swcfg = hellcreek->swcfg;
569 swcfg |= HR_SWCFG_VLAN_UNAWARE;
571 swcfg &= ~HR_SWCFG_VLAN_UNAWARE;
573 hellcreek_write(hellcreek, swcfg, HR_SWCFG);
575 mutex_unlock(&hellcreek->reg_lock);
578 /* Default setup for DSA: VLAN <X>: CPU and Port <X> egress untagged. */
579 static void hellcreek_setup_vlan_membership(struct dsa_switch *ds, int port,
582 const u16 vid = hellcreek_private_vid(port);
583 int upstream = dsa_upstream_port(ds, port);
584 struct hellcreek *hellcreek = ds->priv;
586 /* Apply vid to port as egress untagged and port vlan id */
588 hellcreek_apply_vlan(hellcreek, port, vid, true, true);
590 hellcreek_unapply_vlan(hellcreek, port, vid);
592 /* Apply vid to cpu port as well */
594 hellcreek_apply_vlan(hellcreek, upstream, vid, false, true);
596 hellcreek_unapply_vlan(hellcreek, upstream, vid);
599 static void hellcreek_port_set_ucast_flood(struct hellcreek *hellcreek,
600 int port, bool enable)
602 struct hellcreek_port *hellcreek_port;
605 hellcreek_port = &hellcreek->ports[port];
607 dev_dbg(hellcreek->dev, "%s unicast flooding on port %d\n",
608 enable ? "Enable" : "Disable", port);
610 mutex_lock(&hellcreek->reg_lock);
612 hellcreek_select_port(hellcreek, port);
613 val = hellcreek_port->ptcfg;
615 val &= ~HR_PTCFG_UUC_FLT;
617 val |= HR_PTCFG_UUC_FLT;
618 hellcreek_write(hellcreek, val, HR_PTCFG);
619 hellcreek_port->ptcfg = val;
621 mutex_unlock(&hellcreek->reg_lock);
624 static void hellcreek_port_set_mcast_flood(struct hellcreek *hellcreek,
625 int port, bool enable)
627 struct hellcreek_port *hellcreek_port;
630 hellcreek_port = &hellcreek->ports[port];
632 dev_dbg(hellcreek->dev, "%s multicast flooding on port %d\n",
633 enable ? "Enable" : "Disable", port);
635 mutex_lock(&hellcreek->reg_lock);
637 hellcreek_select_port(hellcreek, port);
638 val = hellcreek_port->ptcfg;
640 val &= ~HR_PTCFG_UMC_FLT;
642 val |= HR_PTCFG_UMC_FLT;
643 hellcreek_write(hellcreek, val, HR_PTCFG);
644 hellcreek_port->ptcfg = val;
646 mutex_unlock(&hellcreek->reg_lock);
649 static int hellcreek_pre_bridge_flags(struct dsa_switch *ds, int port,
650 struct switchdev_brport_flags flags,
651 struct netlink_ext_ack *extack)
653 if (flags.mask & ~(BR_FLOOD | BR_MCAST_FLOOD))
659 static int hellcreek_bridge_flags(struct dsa_switch *ds, int port,
660 struct switchdev_brport_flags flags,
661 struct netlink_ext_ack *extack)
663 struct hellcreek *hellcreek = ds->priv;
665 if (flags.mask & BR_FLOOD)
666 hellcreek_port_set_ucast_flood(hellcreek, port,
667 !!(flags.val & BR_FLOOD));
669 if (flags.mask & BR_MCAST_FLOOD)
670 hellcreek_port_set_mcast_flood(hellcreek, port,
671 !!(flags.val & BR_MCAST_FLOOD));
676 static int hellcreek_port_bridge_join(struct dsa_switch *ds, int port,
677 struct dsa_bridge bridge,
678 bool *tx_fwd_offload)
680 struct hellcreek *hellcreek = ds->priv;
682 dev_dbg(hellcreek->dev, "Port %d joins a bridge\n", port);
684 /* When joining a vlan_filtering bridge, keep the switch VLAN aware */
685 if (!ds->vlan_filtering)
686 hellcreek_setup_vlan_awareness(hellcreek, false);
688 /* Drop private vlans */
689 hellcreek_setup_vlan_membership(ds, port, false);
694 static void hellcreek_port_bridge_leave(struct dsa_switch *ds, int port,
695 struct dsa_bridge bridge)
697 struct hellcreek *hellcreek = ds->priv;
699 dev_dbg(hellcreek->dev, "Port %d leaves a bridge\n", port);
701 /* Enable VLAN awareness */
702 hellcreek_setup_vlan_awareness(hellcreek, true);
704 /* Enable private vlans */
705 hellcreek_setup_vlan_membership(ds, port, true);
708 static int __hellcreek_fdb_add(struct hellcreek *hellcreek,
709 const struct hellcreek_fdb_entry *entry)
713 dev_dbg(hellcreek->dev, "Add static FDB entry: MAC=%pM, MASK=0x%02x, "
714 "OBT=%d, PASS_BLOCKED=%d, REPRIO_EN=%d, PRIO=%d\n", entry->mac,
715 entry->portmask, entry->is_obt, entry->pass_blocked,
716 entry->reprio_en, entry->reprio_tc);
718 /* Add mac address */
719 hellcreek_write(hellcreek, entry->mac[1] | (entry->mac[0] << 8), HR_FDBWDH);
720 hellcreek_write(hellcreek, entry->mac[3] | (entry->mac[2] << 8), HR_FDBWDM);
721 hellcreek_write(hellcreek, entry->mac[5] | (entry->mac[4] << 8), HR_FDBWDL);
724 meta |= entry->portmask << HR_FDBWRM0_PORTMASK_SHIFT;
726 meta |= HR_FDBWRM0_OBT;
727 if (entry->pass_blocked)
728 meta |= HR_FDBWRM0_PASS_BLOCKED;
729 if (entry->reprio_en) {
730 meta |= HR_FDBWRM0_REPRIO_EN;
731 meta |= entry->reprio_tc << HR_FDBWRM0_REPRIO_TC_SHIFT;
733 hellcreek_write(hellcreek, meta, HR_FDBWRM0);
736 hellcreek_write(hellcreek, 0x00, HR_FDBWRCMD);
738 /* Wait until done */
739 return hellcreek_wait_fdb_ready(hellcreek);
742 static int __hellcreek_fdb_del(struct hellcreek *hellcreek,
743 const struct hellcreek_fdb_entry *entry)
745 dev_dbg(hellcreek->dev, "Delete FDB entry: MAC=%pM!\n", entry->mac);
747 /* Delete by matching idx */
748 hellcreek_write(hellcreek, entry->idx | HR_FDBWRCMD_FDBDEL, HR_FDBWRCMD);
750 /* Wait until done */
751 return hellcreek_wait_fdb_ready(hellcreek);
754 static void hellcreek_populate_fdb_entry(struct hellcreek *hellcreek,
755 struct hellcreek_fdb_entry *entry,
758 unsigned char addr[ETH_ALEN];
762 meta = hellcreek_read(hellcreek, HR_FDBMDRD);
763 mac = hellcreek_read(hellcreek, HR_FDBRDL);
764 addr[5] = mac & 0xff;
765 addr[4] = (mac & 0xff00) >> 8;
766 mac = hellcreek_read(hellcreek, HR_FDBRDM);
767 addr[3] = mac & 0xff;
768 addr[2] = (mac & 0xff00) >> 8;
769 mac = hellcreek_read(hellcreek, HR_FDBRDH);
770 addr[1] = mac & 0xff;
771 addr[0] = (mac & 0xff00) >> 8;
773 /* Populate @entry */
774 memcpy(entry->mac, addr, sizeof(addr));
776 entry->portmask = (meta & HR_FDBMDRD_PORTMASK_MASK) >>
777 HR_FDBMDRD_PORTMASK_SHIFT;
778 entry->age = (meta & HR_FDBMDRD_AGE_MASK) >>
779 HR_FDBMDRD_AGE_SHIFT;
780 entry->is_obt = !!(meta & HR_FDBMDRD_OBT);
781 entry->pass_blocked = !!(meta & HR_FDBMDRD_PASS_BLOCKED);
782 entry->is_static = !!(meta & HR_FDBMDRD_STATIC);
783 entry->reprio_tc = (meta & HR_FDBMDRD_REPRIO_TC_MASK) >>
784 HR_FDBMDRD_REPRIO_TC_SHIFT;
785 entry->reprio_en = !!(meta & HR_FDBMDRD_REPRIO_EN);
788 /* Retrieve the index of a FDB entry by mac address. Currently we search through
789 * the complete table in hardware. If that's too slow, we might have to cache
790 * the complete FDB table in software.
792 static int hellcreek_fdb_get(struct hellcreek *hellcreek,
793 const unsigned char *dest,
794 struct hellcreek_fdb_entry *entry)
798 /* Set read pointer to zero: The read of HR_FDBMAX (read-only register)
799 * should reset the internal pointer. But, that doesn't work. The vendor
800 * suggested a subsequent write as workaround. Same for HR_FDBRDH below.
802 hellcreek_read(hellcreek, HR_FDBMAX);
803 hellcreek_write(hellcreek, 0x00, HR_FDBMAX);
805 /* We have to read the complete table, because the switch/driver might
806 * enter new entries anywhere.
808 for (i = 0; i < hellcreek->fdb_entries; ++i) {
809 struct hellcreek_fdb_entry tmp = { 0 };
812 hellcreek_populate_fdb_entry(hellcreek, &tmp, i);
814 /* Force next entry */
815 hellcreek_write(hellcreek, 0x00, HR_FDBRDH);
817 if (memcmp(tmp.mac, dest, ETH_ALEN))
821 memcpy(entry, &tmp, sizeof(*entry));
829 static int hellcreek_fdb_add(struct dsa_switch *ds, int port,
830 const unsigned char *addr, u16 vid,
833 struct hellcreek_fdb_entry entry = { 0 };
834 struct hellcreek *hellcreek = ds->priv;
837 dev_dbg(hellcreek->dev, "Add FDB entry for MAC=%pM\n", addr);
839 mutex_lock(&hellcreek->reg_lock);
841 ret = hellcreek_fdb_get(hellcreek, addr, &entry);
844 memcpy(entry.mac, addr, sizeof(entry.mac));
845 entry.portmask = BIT(port);
847 ret = __hellcreek_fdb_add(hellcreek, &entry);
849 dev_err(hellcreek->dev, "Failed to add FDB entry!\n");
854 ret = __hellcreek_fdb_del(hellcreek, &entry);
856 dev_err(hellcreek->dev, "Failed to delete FDB entry!\n");
860 entry.portmask |= BIT(port);
862 ret = __hellcreek_fdb_add(hellcreek, &entry);
864 dev_err(hellcreek->dev, "Failed to add FDB entry!\n");
870 mutex_unlock(&hellcreek->reg_lock);
875 static int hellcreek_fdb_del(struct dsa_switch *ds, int port,
876 const unsigned char *addr, u16 vid,
879 struct hellcreek_fdb_entry entry = { 0 };
880 struct hellcreek *hellcreek = ds->priv;
883 dev_dbg(hellcreek->dev, "Delete FDB entry for MAC=%pM\n", addr);
885 mutex_lock(&hellcreek->reg_lock);
887 ret = hellcreek_fdb_get(hellcreek, addr, &entry);
890 dev_err(hellcreek->dev, "FDB entry for deletion not found!\n");
893 ret = __hellcreek_fdb_del(hellcreek, &entry);
895 dev_err(hellcreek->dev, "Failed to delete FDB entry!\n");
899 entry.portmask &= ~BIT(port);
901 if (entry.portmask != 0x00) {
902 ret = __hellcreek_fdb_add(hellcreek, &entry);
904 dev_err(hellcreek->dev, "Failed to add FDB entry!\n");
911 mutex_unlock(&hellcreek->reg_lock);
916 static int hellcreek_fdb_dump(struct dsa_switch *ds, int port,
917 dsa_fdb_dump_cb_t *cb, void *data)
919 struct hellcreek *hellcreek = ds->priv;
924 mutex_lock(&hellcreek->reg_lock);
926 /* Set read pointer to zero: The read of HR_FDBMAX (read-only register)
927 * should reset the internal pointer. But, that doesn't work. The vendor
928 * suggested a subsequent write as workaround. Same for HR_FDBRDH below.
930 entries = hellcreek_read(hellcreek, HR_FDBMAX);
931 hellcreek_write(hellcreek, 0x00, HR_FDBMAX);
933 dev_dbg(hellcreek->dev, "FDB dump for port %d, entries=%d!\n", port, entries);
936 for (i = 0; i < hellcreek->fdb_entries; ++i) {
937 struct hellcreek_fdb_entry entry = { 0 };
940 hellcreek_populate_fdb_entry(hellcreek, &entry, i);
942 /* Force next entry */
943 hellcreek_write(hellcreek, 0x00, HR_FDBRDH);
946 if (is_zero_ether_addr(entry.mac))
949 /* Check port mask */
950 if (!(entry.portmask & BIT(port)))
953 ret = cb(entry.mac, 0, entry.is_static, data);
958 mutex_unlock(&hellcreek->reg_lock);
963 static int hellcreek_vlan_filtering(struct dsa_switch *ds, int port,
965 struct netlink_ext_ack *extack)
967 struct hellcreek *hellcreek = ds->priv;
969 dev_dbg(hellcreek->dev, "%s VLAN filtering on port %d\n",
970 vlan_filtering ? "Enable" : "Disable", port);
972 /* Configure port to drop packages with not known vids */
973 hellcreek_setup_ingressflt(hellcreek, port, vlan_filtering);
975 /* Enable VLAN awareness on the switch. This save due to
976 * ds->vlan_filtering_is_global.
978 hellcreek_setup_vlan_awareness(hellcreek, vlan_filtering);
983 static int hellcreek_enable_ip_core(struct hellcreek *hellcreek)
988 mutex_lock(&hellcreek->reg_lock);
990 val = hellcreek_read(hellcreek, HR_CTRL_C);
991 val |= HR_CTRL_C_ENABLE;
992 hellcreek_write(hellcreek, val, HR_CTRL_C);
993 ret = hellcreek_wait_until_transitioned(hellcreek);
995 mutex_unlock(&hellcreek->reg_lock);
1000 static void hellcreek_setup_cpu_and_tunnel_port(struct hellcreek *hellcreek)
1002 struct hellcreek_port *tunnel_port = &hellcreek->ports[TUNNEL_PORT];
1003 struct hellcreek_port *cpu_port = &hellcreek->ports[CPU_PORT];
1006 ptcfg |= HR_PTCFG_LEARNING_EN | HR_PTCFG_ADMIN_EN;
1008 mutex_lock(&hellcreek->reg_lock);
1010 hellcreek_select_port(hellcreek, CPU_PORT);
1011 hellcreek_write(hellcreek, ptcfg, HR_PTCFG);
1013 hellcreek_select_port(hellcreek, TUNNEL_PORT);
1014 hellcreek_write(hellcreek, ptcfg, HR_PTCFG);
1016 cpu_port->ptcfg = ptcfg;
1017 tunnel_port->ptcfg = ptcfg;
1019 mutex_unlock(&hellcreek->reg_lock);
1022 static void hellcreek_setup_tc_identity_mapping(struct hellcreek *hellcreek)
1026 /* The switch has multiple egress queues per port. The queue is selected
1027 * via the PCP field in the VLAN header. The switch internally deals
1028 * with traffic classes instead of PCP values and this mapping is
1031 * The default mapping is (PCP - TC):
1041 * The default should be an identity mapping.
1044 for (i = 0; i < 8; ++i) {
1045 mutex_lock(&hellcreek->reg_lock);
1047 hellcreek_select_prio(hellcreek, i);
1048 hellcreek_write(hellcreek,
1049 i << HR_PRTCCFG_PCP_TC_MAP_SHIFT,
1052 mutex_unlock(&hellcreek->reg_lock);
1056 static int hellcreek_setup_fdb(struct hellcreek *hellcreek)
1058 static struct hellcreek_fdb_entry l2_ptp = {
1059 /* MAC: 01-1B-19-00-00-00 */
1060 .mac = { 0x01, 0x1b, 0x19, 0x00, 0x00, 0x00 },
1061 .portmask = 0x03, /* Management ports */
1066 .reprio_tc = 6, /* TC: 6 as per IEEE 802.1AS */
1069 static struct hellcreek_fdb_entry udp4_ptp = {
1070 /* MAC: 01-00-5E-00-01-81 */
1071 .mac = { 0x01, 0x00, 0x5e, 0x00, 0x01, 0x81 },
1072 .portmask = 0x03, /* Management ports */
1080 static struct hellcreek_fdb_entry udp6_ptp = {
1081 /* MAC: 33-33-00-00-01-81 */
1082 .mac = { 0x33, 0x33, 0x00, 0x00, 0x01, 0x81 },
1083 .portmask = 0x03, /* Management ports */
1091 static struct hellcreek_fdb_entry l2_p2p = {
1092 /* MAC: 01-80-C2-00-00-0E */
1093 .mac = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x0e },
1094 .portmask = 0x03, /* Management ports */
1099 .reprio_tc = 6, /* TC: 6 as per IEEE 802.1AS */
1102 static struct hellcreek_fdb_entry udp4_p2p = {
1103 /* MAC: 01-00-5E-00-00-6B */
1104 .mac = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x6b },
1105 .portmask = 0x03, /* Management ports */
1113 static struct hellcreek_fdb_entry udp6_p2p = {
1114 /* MAC: 33-33-00-00-00-6B */
1115 .mac = { 0x33, 0x33, 0x00, 0x00, 0x00, 0x6b },
1116 .portmask = 0x03, /* Management ports */
1124 static struct hellcreek_fdb_entry stp = {
1125 /* MAC: 01-80-C2-00-00-00 */
1126 .mac = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 },
1127 .portmask = 0x03, /* Management ports */
1137 mutex_lock(&hellcreek->reg_lock);
1138 ret = __hellcreek_fdb_add(hellcreek, &l2_ptp);
1141 ret = __hellcreek_fdb_add(hellcreek, &udp4_ptp);
1144 ret = __hellcreek_fdb_add(hellcreek, &udp6_ptp);
1147 ret = __hellcreek_fdb_add(hellcreek, &l2_p2p);
1150 ret = __hellcreek_fdb_add(hellcreek, &udp4_p2p);
1153 ret = __hellcreek_fdb_add(hellcreek, &udp6_p2p);
1156 ret = __hellcreek_fdb_add(hellcreek, &stp);
1158 mutex_unlock(&hellcreek->reg_lock);
1163 static int hellcreek_devlink_info_get(struct dsa_switch *ds,
1164 struct devlink_info_req *req,
1165 struct netlink_ext_ack *extack)
1167 struct hellcreek *hellcreek = ds->priv;
1170 ret = devlink_info_driver_name_put(req, "hellcreek");
1174 return devlink_info_version_fixed_put(req,
1175 DEVLINK_INFO_VERSION_GENERIC_ASIC_ID,
1176 hellcreek->pdata->name);
1179 static u64 hellcreek_devlink_vlan_table_get(void *priv)
1181 struct hellcreek *hellcreek = priv;
1185 mutex_lock(&hellcreek->reg_lock);
1186 for (i = 0; i < VLAN_N_VID; ++i)
1187 if (hellcreek->vidmbrcfg[i])
1189 mutex_unlock(&hellcreek->reg_lock);
1194 static u64 hellcreek_devlink_fdb_table_get(void *priv)
1196 struct hellcreek *hellcreek = priv;
1199 /* Reading this register has side effects. Synchronize against the other
1202 mutex_lock(&hellcreek->reg_lock);
1203 count = hellcreek_read(hellcreek, HR_FDBMAX);
1204 mutex_unlock(&hellcreek->reg_lock);
1209 static int hellcreek_setup_devlink_resources(struct dsa_switch *ds)
1211 struct devlink_resource_size_params size_vlan_params;
1212 struct devlink_resource_size_params size_fdb_params;
1213 struct hellcreek *hellcreek = ds->priv;
1216 devlink_resource_size_params_init(&size_vlan_params, VLAN_N_VID,
1218 1, DEVLINK_RESOURCE_UNIT_ENTRY);
1220 devlink_resource_size_params_init(&size_fdb_params,
1221 hellcreek->fdb_entries,
1222 hellcreek->fdb_entries,
1223 1, DEVLINK_RESOURCE_UNIT_ENTRY);
1225 err = dsa_devlink_resource_register(ds, "VLAN", VLAN_N_VID,
1226 HELLCREEK_DEVLINK_PARAM_ID_VLAN_TABLE,
1227 DEVLINK_RESOURCE_ID_PARENT_TOP,
1232 err = dsa_devlink_resource_register(ds, "FDB", hellcreek->fdb_entries,
1233 HELLCREEK_DEVLINK_PARAM_ID_FDB_TABLE,
1234 DEVLINK_RESOURCE_ID_PARENT_TOP,
1239 dsa_devlink_resource_occ_get_register(ds,
1240 HELLCREEK_DEVLINK_PARAM_ID_VLAN_TABLE,
1241 hellcreek_devlink_vlan_table_get,
1244 dsa_devlink_resource_occ_get_register(ds,
1245 HELLCREEK_DEVLINK_PARAM_ID_FDB_TABLE,
1246 hellcreek_devlink_fdb_table_get,
1252 dsa_devlink_resources_unregister(ds);
1257 static int hellcreek_devlink_region_vlan_snapshot(struct devlink *dl,
1258 const struct devlink_region_ops *ops,
1259 struct netlink_ext_ack *extack,
1262 struct hellcreek_devlink_vlan_entry *table, *entry;
1263 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
1264 struct hellcreek *hellcreek = ds->priv;
1267 table = kcalloc(VLAN_N_VID, sizeof(*entry), GFP_KERNEL);
1273 mutex_lock(&hellcreek->reg_lock);
1274 for (i = 0; i < VLAN_N_VID; ++i, ++entry) {
1275 entry->member = hellcreek->vidmbrcfg[i];
1278 mutex_unlock(&hellcreek->reg_lock);
1280 *data = (u8 *)table;
1285 static int hellcreek_devlink_region_fdb_snapshot(struct devlink *dl,
1286 const struct devlink_region_ops *ops,
1287 struct netlink_ext_ack *extack,
1290 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
1291 struct hellcreek_fdb_entry *table, *entry;
1292 struct hellcreek *hellcreek = ds->priv;
1295 table = kcalloc(hellcreek->fdb_entries, sizeof(*entry), GFP_KERNEL);
1301 mutex_lock(&hellcreek->reg_lock);
1303 /* Start table read */
1304 hellcreek_read(hellcreek, HR_FDBMAX);
1305 hellcreek_write(hellcreek, 0x00, HR_FDBMAX);
1307 for (i = 0; i < hellcreek->fdb_entries; ++i, ++entry) {
1308 /* Read current entry */
1309 hellcreek_populate_fdb_entry(hellcreek, entry, i);
1311 /* Advance read pointer */
1312 hellcreek_write(hellcreek, 0x00, HR_FDBRDH);
1315 mutex_unlock(&hellcreek->reg_lock);
1317 *data = (u8 *)table;
1322 static struct devlink_region_ops hellcreek_region_vlan_ops = {
1324 .snapshot = hellcreek_devlink_region_vlan_snapshot,
1325 .destructor = kfree,
1328 static struct devlink_region_ops hellcreek_region_fdb_ops = {
1330 .snapshot = hellcreek_devlink_region_fdb_snapshot,
1331 .destructor = kfree,
1334 static int hellcreek_setup_devlink_regions(struct dsa_switch *ds)
1336 struct hellcreek *hellcreek = ds->priv;
1337 struct devlink_region_ops *ops;
1338 struct devlink_region *region;
1343 size = VLAN_N_VID * sizeof(struct hellcreek_devlink_vlan_entry);
1344 ops = &hellcreek_region_vlan_ops;
1346 region = dsa_devlink_region_create(ds, ops, 1, size);
1348 return PTR_ERR(region);
1350 hellcreek->vlan_region = region;
1353 size = hellcreek->fdb_entries * sizeof(struct hellcreek_fdb_entry);
1354 ops = &hellcreek_region_fdb_ops;
1356 region = dsa_devlink_region_create(ds, ops, 1, size);
1357 if (IS_ERR(region)) {
1358 ret = PTR_ERR(region);
1362 hellcreek->fdb_region = region;
1367 dsa_devlink_region_destroy(hellcreek->vlan_region);
1372 static void hellcreek_teardown_devlink_regions(struct dsa_switch *ds)
1374 struct hellcreek *hellcreek = ds->priv;
1376 dsa_devlink_region_destroy(hellcreek->fdb_region);
1377 dsa_devlink_region_destroy(hellcreek->vlan_region);
1380 static int hellcreek_setup(struct dsa_switch *ds)
1382 struct hellcreek *hellcreek = ds->priv;
1386 dev_dbg(hellcreek->dev, "Set up the switch\n");
1389 ret = hellcreek_enable_ip_core(hellcreek);
1391 dev_err(hellcreek->dev, "Failed to enable IP core!\n");
1395 /* Enable CPU/Tunnel ports */
1396 hellcreek_setup_cpu_and_tunnel_port(hellcreek);
1398 /* Switch config: Keep defaults, enable FDB aging and learning and tag
1399 * each frame from/to cpu port for DSA tagging. Also enable the length
1400 * aware shaping mode. This eliminates the need for Qbv guard bands.
1402 swcfg |= HR_SWCFG_FDBAGE_EN |
1403 HR_SWCFG_FDBLRN_EN |
1404 HR_SWCFG_ALWAYS_OBT |
1405 (HR_SWCFG_LAS_ON << HR_SWCFG_LAS_MODE_SHIFT);
1406 hellcreek->swcfg = swcfg;
1407 hellcreek_write(hellcreek, swcfg, HR_SWCFG);
1409 /* Initial vlan membership to reflect port separation */
1410 for (i = 0; i < ds->num_ports; ++i) {
1411 if (!dsa_is_user_port(ds, i))
1414 hellcreek_setup_vlan_membership(ds, i, true);
1417 /* Configure PCP <-> TC mapping */
1418 hellcreek_setup_tc_identity_mapping(hellcreek);
1420 /* The VLAN awareness is a global switch setting. Therefore, mixed vlan
1421 * filtering setups are not supported.
1423 ds->vlan_filtering_is_global = true;
1424 ds->needs_standalone_vlan_filtering = true;
1426 /* Intercept _all_ PTP multicast traffic */
1427 ret = hellcreek_setup_fdb(hellcreek);
1429 dev_err(hellcreek->dev,
1430 "Failed to insert static PTP FDB entries\n");
1434 /* Register devlink resources with DSA */
1435 ret = hellcreek_setup_devlink_resources(ds);
1437 dev_err(hellcreek->dev,
1438 "Failed to setup devlink resources!\n");
1442 ret = hellcreek_setup_devlink_regions(ds);
1444 dev_err(hellcreek->dev,
1445 "Failed to setup devlink regions!\n");
1452 dsa_devlink_resources_unregister(ds);
1457 static void hellcreek_teardown(struct dsa_switch *ds)
1459 hellcreek_teardown_devlink_regions(ds);
1460 dsa_devlink_resources_unregister(ds);
1463 static void hellcreek_phylink_get_caps(struct dsa_switch *ds, int port,
1464 struct phylink_config *config)
1466 struct hellcreek *hellcreek = ds->priv;
1468 __set_bit(PHY_INTERFACE_MODE_MII, config->supported_interfaces);
1469 __set_bit(PHY_INTERFACE_MODE_RGMII, config->supported_interfaces);
1471 /* Include GMII - the hardware does not support this interface
1472 * mode, but it's the default interface mode for phylib, so we
1473 * need it for compatibility with existing DT.
1475 __set_bit(PHY_INTERFACE_MODE_GMII, config->supported_interfaces);
1477 /* The MAC settings are a hardware configuration option and cannot be
1478 * changed at run time or by strapping. Therefore the attached PHYs
1479 * should be programmed to only advertise settings which are supported
1482 if (hellcreek->pdata->is_100_mbits)
1483 config->mac_capabilities = MAC_100FD;
1485 config->mac_capabilities = MAC_1000FD;
1489 hellcreek_port_prechangeupper(struct dsa_switch *ds, int port,
1490 struct netdev_notifier_changeupper_info *info)
1492 struct hellcreek *hellcreek = ds->priv;
1498 dev_dbg(hellcreek->dev, "Pre change upper for port %d\n", port);
1501 * Deny VLAN devices on top of lan ports with the same VLAN ids, because
1502 * it breaks the port separation due to the private VLANs. Example:
1504 * lan0.100 *and* lan1.100 cannot be used in parallel. However, lan0.99
1505 * and lan1.100 works.
1508 if (!is_vlan_dev(info->upper_dev))
1511 vid = vlan_dev_vlan_id(info->upper_dev);
1513 /* For all ports, check bitmaps */
1514 mutex_lock(&hellcreek->vlan_lock);
1515 for (i = 0; i < hellcreek->pdata->num_ports; ++i) {
1516 if (!dsa_is_user_port(ds, i))
1522 used = used && test_bit(vid, hellcreek->ports[i].vlan_dev_bitmap);
1529 set_bit(vid, hellcreek->ports[port].vlan_dev_bitmap);
1534 mutex_unlock(&hellcreek->vlan_lock);
1539 static void hellcreek_setup_gcl(struct hellcreek *hellcreek, int port,
1540 const struct tc_taprio_qopt_offload *schedule)
1542 const struct tc_taprio_sched_entry *cur, *initial, *next;
1545 cur = initial = &schedule->entries[0];
1548 for (i = 1; i <= schedule->num_entries; ++i) {
1552 if (i == schedule->num_entries)
1553 gates = initial->gate_mask ^
1556 gates = next->gate_mask ^
1561 if (i == schedule->num_entries)
1562 data |= TR_GCLDAT_GCLWRLAST;
1565 hellcreek_write(hellcreek, data, TR_GCLDAT);
1568 hellcreek_write(hellcreek,
1569 cur->interval & 0x0000ffff,
1571 hellcreek_write(hellcreek,
1572 (cur->interval & 0xffff0000) >> 16,
1576 data = ((i - 1) << TR_GCLCMD_GCLWRADR_SHIFT) |
1577 (initial->gate_mask <<
1578 TR_GCLCMD_INIT_GATE_STATES_SHIFT);
1579 hellcreek_write(hellcreek, data, TR_GCLCMD);
1586 static void hellcreek_set_cycle_time(struct hellcreek *hellcreek,
1587 const struct tc_taprio_qopt_offload *schedule)
1589 u32 cycle_time = schedule->cycle_time;
1591 hellcreek_write(hellcreek, cycle_time & 0x0000ffff, TR_CTWRL);
1592 hellcreek_write(hellcreek, (cycle_time & 0xffff0000) >> 16, TR_CTWRH);
1595 static void hellcreek_switch_schedule(struct hellcreek *hellcreek,
1598 struct timespec64 ts = ktime_to_timespec64(start_time);
1600 /* Start schedule at this point of time */
1601 hellcreek_write(hellcreek, ts.tv_nsec & 0x0000ffff, TR_ESTWRL);
1602 hellcreek_write(hellcreek, (ts.tv_nsec & 0xffff0000) >> 16, TR_ESTWRH);
1604 /* Arm timer, set seconds and switch schedule */
1605 hellcreek_write(hellcreek, TR_ESTCMD_ESTARM | TR_ESTCMD_ESTSWCFG |
1606 ((ts.tv_sec & TR_ESTCMD_ESTSEC_MASK) <<
1607 TR_ESTCMD_ESTSEC_SHIFT), TR_ESTCMD);
1610 static bool hellcreek_schedule_startable(struct hellcreek *hellcreek, int port)
1612 struct hellcreek_port *hellcreek_port = &hellcreek->ports[port];
1613 s64 base_time_ns, current_ns;
1615 /* The switch allows a schedule to be started only eight seconds within
1616 * the future. Therefore, check the current PTP time if the schedule is
1620 /* Use the "cached" time. That should be alright, as it's updated quite
1621 * frequently in the PTP code.
1623 mutex_lock(&hellcreek->ptp_lock);
1624 current_ns = hellcreek->seconds * NSEC_PER_SEC + hellcreek->last_ts;
1625 mutex_unlock(&hellcreek->ptp_lock);
1627 /* Calculate difference to admin base time */
1628 base_time_ns = ktime_to_ns(hellcreek_port->current_schedule->base_time);
1630 return base_time_ns - current_ns < (s64)4 * NSEC_PER_SEC;
1633 static void hellcreek_start_schedule(struct hellcreek *hellcreek, int port)
1635 struct hellcreek_port *hellcreek_port = &hellcreek->ports[port];
1636 ktime_t base_time, current_time;
1640 /* First select port */
1641 hellcreek_select_tgd(hellcreek, port);
1643 /* Forward base time into the future if needed */
1644 mutex_lock(&hellcreek->ptp_lock);
1645 current_ns = hellcreek->seconds * NSEC_PER_SEC + hellcreek->last_ts;
1646 mutex_unlock(&hellcreek->ptp_lock);
1648 current_time = ns_to_ktime(current_ns);
1649 base_time = hellcreek_port->current_schedule->base_time;
1650 cycle_time = hellcreek_port->current_schedule->cycle_time;
1652 if (ktime_compare(current_time, base_time) > 0) {
1655 n = div64_s64(ktime_sub_ns(current_time, base_time),
1657 base_time = ktime_add_ns(base_time, (n + 1) * cycle_time);
1660 /* Set admin base time and switch schedule */
1661 hellcreek_switch_schedule(hellcreek, base_time);
1663 taprio_offload_free(hellcreek_port->current_schedule);
1664 hellcreek_port->current_schedule = NULL;
1666 dev_dbg(hellcreek->dev, "Armed EST timer for port %d\n",
1667 hellcreek_port->port);
1670 static void hellcreek_check_schedule(struct work_struct *work)
1672 struct delayed_work *dw = to_delayed_work(work);
1673 struct hellcreek_port *hellcreek_port;
1674 struct hellcreek *hellcreek;
1677 hellcreek_port = dw_to_hellcreek_port(dw);
1678 hellcreek = hellcreek_port->hellcreek;
1680 mutex_lock(&hellcreek->reg_lock);
1682 /* Check starting time */
1683 startable = hellcreek_schedule_startable(hellcreek,
1684 hellcreek_port->port);
1686 hellcreek_start_schedule(hellcreek, hellcreek_port->port);
1687 mutex_unlock(&hellcreek->reg_lock);
1691 mutex_unlock(&hellcreek->reg_lock);
1694 schedule_delayed_work(&hellcreek_port->schedule_work,
1695 HELLCREEK_SCHEDULE_PERIOD);
1698 static int hellcreek_port_set_schedule(struct dsa_switch *ds, int port,
1699 struct tc_taprio_qopt_offload *taprio)
1701 struct hellcreek *hellcreek = ds->priv;
1702 struct hellcreek_port *hellcreek_port;
1706 hellcreek_port = &hellcreek->ports[port];
1708 dev_dbg(hellcreek->dev, "Configure traffic schedule on port %d\n",
1711 /* First cancel delayed work */
1712 cancel_delayed_work_sync(&hellcreek_port->schedule_work);
1714 mutex_lock(&hellcreek->reg_lock);
1716 if (hellcreek_port->current_schedule) {
1717 taprio_offload_free(hellcreek_port->current_schedule);
1718 hellcreek_port->current_schedule = NULL;
1720 hellcreek_port->current_schedule = taprio_offload_get(taprio);
1722 /* Then select port */
1723 hellcreek_select_tgd(hellcreek, port);
1725 /* Enable gating and keep defaults */
1726 ctrl = (0xff << TR_TGDCTRL_ADMINGATESTATES_SHIFT) | TR_TGDCTRL_GATE_EN;
1727 hellcreek_write(hellcreek, ctrl, TR_TGDCTRL);
1729 /* Cancel pending schedule */
1730 hellcreek_write(hellcreek, 0x00, TR_ESTCMD);
1732 /* Setup a new schedule */
1733 hellcreek_setup_gcl(hellcreek, port, hellcreek_port->current_schedule);
1735 /* Configure cycle time */
1736 hellcreek_set_cycle_time(hellcreek, hellcreek_port->current_schedule);
1738 /* Check starting time */
1739 startable = hellcreek_schedule_startable(hellcreek, port);
1741 hellcreek_start_schedule(hellcreek, port);
1742 mutex_unlock(&hellcreek->reg_lock);
1746 mutex_unlock(&hellcreek->reg_lock);
1748 /* Schedule periodic schedule check */
1749 schedule_delayed_work(&hellcreek_port->schedule_work,
1750 HELLCREEK_SCHEDULE_PERIOD);
1755 static int hellcreek_port_del_schedule(struct dsa_switch *ds, int port)
1757 struct hellcreek *hellcreek = ds->priv;
1758 struct hellcreek_port *hellcreek_port;
1760 hellcreek_port = &hellcreek->ports[port];
1762 dev_dbg(hellcreek->dev, "Remove traffic schedule on port %d\n", port);
1764 /* First cancel delayed work */
1765 cancel_delayed_work_sync(&hellcreek_port->schedule_work);
1767 mutex_lock(&hellcreek->reg_lock);
1769 if (hellcreek_port->current_schedule) {
1770 taprio_offload_free(hellcreek_port->current_schedule);
1771 hellcreek_port->current_schedule = NULL;
1774 /* Then select port */
1775 hellcreek_select_tgd(hellcreek, port);
1777 /* Disable gating and return to regular switching flow */
1778 hellcreek_write(hellcreek, 0xff << TR_TGDCTRL_ADMINGATESTATES_SHIFT,
1781 mutex_unlock(&hellcreek->reg_lock);
1786 static bool hellcreek_validate_schedule(struct hellcreek *hellcreek,
1787 struct tc_taprio_qopt_offload *schedule)
1791 /* Does this hellcreek version support Qbv in hardware? */
1792 if (!hellcreek->pdata->qbv_support)
1795 /* cycle time can only be 32bit */
1796 if (schedule->cycle_time > (u32)-1)
1799 /* cycle time extension is not supported */
1800 if (schedule->cycle_time_extension)
1803 /* Only set command is supported */
1804 for (i = 0; i < schedule->num_entries; ++i)
1805 if (schedule->entries[i].command != TC_TAPRIO_CMD_SET_GATES)
1811 static int hellcreek_port_setup_tc(struct dsa_switch *ds, int port,
1812 enum tc_setup_type type, void *type_data)
1814 struct tc_taprio_qopt_offload *taprio = type_data;
1815 struct hellcreek *hellcreek = ds->priv;
1817 if (type != TC_SETUP_QDISC_TAPRIO)
1820 if (!hellcreek_validate_schedule(hellcreek, taprio))
1824 return hellcreek_port_set_schedule(ds, port, taprio);
1826 return hellcreek_port_del_schedule(ds, port);
1829 static const struct dsa_switch_ops hellcreek_ds_ops = {
1830 .devlink_info_get = hellcreek_devlink_info_get,
1831 .get_ethtool_stats = hellcreek_get_ethtool_stats,
1832 .get_sset_count = hellcreek_get_sset_count,
1833 .get_strings = hellcreek_get_strings,
1834 .get_tag_protocol = hellcreek_get_tag_protocol,
1835 .get_ts_info = hellcreek_get_ts_info,
1836 .phylink_get_caps = hellcreek_phylink_get_caps,
1837 .port_bridge_flags = hellcreek_bridge_flags,
1838 .port_bridge_join = hellcreek_port_bridge_join,
1839 .port_bridge_leave = hellcreek_port_bridge_leave,
1840 .port_disable = hellcreek_port_disable,
1841 .port_enable = hellcreek_port_enable,
1842 .port_fdb_add = hellcreek_fdb_add,
1843 .port_fdb_del = hellcreek_fdb_del,
1844 .port_fdb_dump = hellcreek_fdb_dump,
1845 .port_hwtstamp_set = hellcreek_port_hwtstamp_set,
1846 .port_hwtstamp_get = hellcreek_port_hwtstamp_get,
1847 .port_pre_bridge_flags = hellcreek_pre_bridge_flags,
1848 .port_prechangeupper = hellcreek_port_prechangeupper,
1849 .port_rxtstamp = hellcreek_port_rxtstamp,
1850 .port_setup_tc = hellcreek_port_setup_tc,
1851 .port_stp_state_set = hellcreek_port_stp_state_set,
1852 .port_txtstamp = hellcreek_port_txtstamp,
1853 .port_vlan_add = hellcreek_vlan_add,
1854 .port_vlan_del = hellcreek_vlan_del,
1855 .port_vlan_filtering = hellcreek_vlan_filtering,
1856 .setup = hellcreek_setup,
1857 .teardown = hellcreek_teardown,
1860 static int hellcreek_probe(struct platform_device *pdev)
1862 struct device *dev = &pdev->dev;
1863 struct hellcreek *hellcreek;
1864 struct resource *res;
1867 hellcreek = devm_kzalloc(dev, sizeof(*hellcreek), GFP_KERNEL);
1871 hellcreek->vidmbrcfg = devm_kcalloc(dev, VLAN_N_VID,
1872 sizeof(*hellcreek->vidmbrcfg),
1874 if (!hellcreek->vidmbrcfg)
1877 hellcreek->pdata = of_device_get_match_data(dev);
1879 hellcreek->ports = devm_kcalloc(dev, hellcreek->pdata->num_ports,
1880 sizeof(*hellcreek->ports),
1882 if (!hellcreek->ports)
1885 for (i = 0; i < hellcreek->pdata->num_ports; ++i) {
1886 struct hellcreek_port *port = &hellcreek->ports[i];
1888 port->counter_values =
1890 ARRAY_SIZE(hellcreek_counter),
1891 sizeof(*port->counter_values),
1893 if (!port->counter_values)
1896 port->vlan_dev_bitmap =
1898 BITS_TO_LONGS(VLAN_N_VID),
1899 sizeof(unsigned long),
1901 if (!port->vlan_dev_bitmap)
1904 port->hellcreek = hellcreek;
1907 INIT_DELAYED_WORK(&port->schedule_work,
1908 hellcreek_check_schedule);
1911 mutex_init(&hellcreek->reg_lock);
1912 mutex_init(&hellcreek->vlan_lock);
1913 mutex_init(&hellcreek->ptp_lock);
1915 hellcreek->dev = dev;
1917 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "tsn");
1919 dev_err(dev, "No memory region provided!\n");
1923 hellcreek->base = devm_ioremap_resource(dev, res);
1924 if (IS_ERR(hellcreek->base))
1925 return PTR_ERR(hellcreek->base);
1927 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ptp");
1929 dev_err(dev, "No PTP memory region provided!\n");
1933 hellcreek->ptp_base = devm_ioremap_resource(dev, res);
1934 if (IS_ERR(hellcreek->ptp_base))
1935 return PTR_ERR(hellcreek->ptp_base);
1937 ret = hellcreek_detect(hellcreek);
1939 dev_err(dev, "No (known) chip found!\n");
1943 ret = hellcreek_wait_until_ready(hellcreek);
1945 dev_err(dev, "Switch didn't become ready!\n");
1949 hellcreek_feature_detect(hellcreek);
1951 hellcreek->ds = devm_kzalloc(dev, sizeof(*hellcreek->ds), GFP_KERNEL);
1955 hellcreek->ds->dev = dev;
1956 hellcreek->ds->priv = hellcreek;
1957 hellcreek->ds->ops = &hellcreek_ds_ops;
1958 hellcreek->ds->num_ports = hellcreek->pdata->num_ports;
1959 hellcreek->ds->num_tx_queues = HELLCREEK_NUM_EGRESS_QUEUES;
1961 ret = dsa_register_switch(hellcreek->ds);
1963 dev_err_probe(dev, ret, "Unable to register switch\n");
1967 ret = hellcreek_ptp_setup(hellcreek);
1969 dev_err(dev, "Failed to setup PTP!\n");
1973 ret = hellcreek_hwtstamp_setup(hellcreek);
1975 dev_err(dev, "Failed to setup hardware timestamping!\n");
1976 goto err_tstamp_setup;
1979 platform_set_drvdata(pdev, hellcreek);
1984 hellcreek_ptp_free(hellcreek);
1986 dsa_unregister_switch(hellcreek->ds);
1991 static int hellcreek_remove(struct platform_device *pdev)
1993 struct hellcreek *hellcreek = platform_get_drvdata(pdev);
1998 hellcreek_hwtstamp_free(hellcreek);
1999 hellcreek_ptp_free(hellcreek);
2000 dsa_unregister_switch(hellcreek->ds);
2001 platform_set_drvdata(pdev, NULL);
2006 static void hellcreek_shutdown(struct platform_device *pdev)
2008 struct hellcreek *hellcreek = platform_get_drvdata(pdev);
2013 dsa_switch_shutdown(hellcreek->ds);
2015 platform_set_drvdata(pdev, NULL);
2018 static const struct hellcreek_platform_data de1soc_r1_pdata = {
2023 .qbv_on_cpu_port = 1,
2025 .module_id = 0x4c30,
2028 static const struct of_device_id hellcreek_of_match[] = {
2030 .compatible = "hirschmann,hellcreek-de1soc-r1",
2031 .data = &de1soc_r1_pdata,
2035 MODULE_DEVICE_TABLE(of, hellcreek_of_match);
2037 static struct platform_driver hellcreek_driver = {
2038 .probe = hellcreek_probe,
2039 .remove = hellcreek_remove,
2040 .shutdown = hellcreek_shutdown,
2042 .name = "hellcreek",
2043 .of_match_table = hellcreek_of_match,
2046 module_platform_driver(hellcreek_driver);
2048 MODULE_AUTHOR("Kurt Kanzenbach <kurt@linutronix.de>");
2049 MODULE_DESCRIPTION("Hirschmann Hellcreek driver");
2050 MODULE_LICENSE("Dual MIT/GPL");