1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
4 * Hirschmann Hellcreek TSN switch.
6 * Copyright (C) 2019-2021 Linutronix GmbH
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/device.h>
14 #include <linux/of_mdio.h>
15 #include <linux/platform_device.h>
16 #include <linux/bitops.h>
17 #include <linux/if_bridge.h>
18 #include <linux/if_vlan.h>
19 #include <linux/etherdevice.h>
20 #include <linux/random.h>
21 #include <linux/iopoll.h>
22 #include <linux/mutex.h>
23 #include <linux/delay.h>
26 #include "hellcreek.h"
27 #include "hellcreek_ptp.h"
28 #include "hellcreek_hwtstamp.h"
30 static const struct hellcreek_counter hellcreek_counter[] = {
31 { 0x00, "RxFiltered", },
32 { 0x01, "RxOctets1k", },
35 { 0x04, "RxOverloadDrop", },
41 { 0x0a, "RxRS65_127", },
42 { 0x0b, "RxRS128_255", },
43 { 0x0c, "RxRS256_511", },
44 { 0x0d, "RxRS512_1023", },
45 { 0x0e, "RxRS1024_1518", },
46 { 0x0f, "RxRS>1518", },
47 { 0x10, "TxTailDropQueue0", },
48 { 0x11, "TxTailDropQueue1", },
49 { 0x12, "TxTailDropQueue2", },
50 { 0x13, "TxTailDropQueue3", },
51 { 0x14, "TxTailDropQueue4", },
52 { 0x15, "TxTailDropQueue5", },
53 { 0x16, "TxTailDropQueue6", },
54 { 0x17, "TxTailDropQueue7", },
55 { 0x18, "RxTrafficClass0", },
56 { 0x19, "RxTrafficClass1", },
57 { 0x1a, "RxTrafficClass2", },
58 { 0x1b, "RxTrafficClass3", },
59 { 0x1c, "RxTrafficClass4", },
60 { 0x1d, "RxTrafficClass5", },
61 { 0x1e, "RxTrafficClass6", },
62 { 0x1f, "RxTrafficClass7", },
63 { 0x21, "TxOctets1k", },
71 { 0x2a, "TxTS65_127", },
72 { 0x2b, "TxTS128_255", },
73 { 0x2c, "TxTS256_511", },
74 { 0x2d, "TxTS512_1023", },
75 { 0x2e, "TxTS1024_1518", },
76 { 0x2f, "TxTS>1518", },
77 { 0x30, "TxTrafficClassOverrun0", },
78 { 0x31, "TxTrafficClassOverrun1", },
79 { 0x32, "TxTrafficClassOverrun2", },
80 { 0x33, "TxTrafficClassOverrun3", },
81 { 0x34, "TxTrafficClassOverrun4", },
82 { 0x35, "TxTrafficClassOverrun5", },
83 { 0x36, "TxTrafficClassOverrun6", },
84 { 0x37, "TxTrafficClassOverrun7", },
85 { 0x38, "TxTrafficClass0", },
86 { 0x39, "TxTrafficClass1", },
87 { 0x3a, "TxTrafficClass2", },
88 { 0x3b, "TxTrafficClass3", },
89 { 0x3c, "TxTrafficClass4", },
90 { 0x3d, "TxTrafficClass5", },
91 { 0x3e, "TxTrafficClass6", },
92 { 0x3f, "TxTrafficClass7", },
95 static u16 hellcreek_read(struct hellcreek *hellcreek, unsigned int offset)
97 return readw(hellcreek->base + offset);
100 static u16 hellcreek_read_ctrl(struct hellcreek *hellcreek)
102 return readw(hellcreek->base + HR_CTRL_C);
105 static u16 hellcreek_read_stat(struct hellcreek *hellcreek)
107 return readw(hellcreek->base + HR_SWSTAT);
110 static void hellcreek_write(struct hellcreek *hellcreek, u16 data,
113 writew(data, hellcreek->base + offset);
116 static void hellcreek_select_port(struct hellcreek *hellcreek, int port)
118 u16 val = port << HR_PSEL_PTWSEL_SHIFT;
120 hellcreek_write(hellcreek, val, HR_PSEL);
123 static void hellcreek_select_prio(struct hellcreek *hellcreek, int prio)
125 u16 val = prio << HR_PSEL_PRTCWSEL_SHIFT;
127 hellcreek_write(hellcreek, val, HR_PSEL);
130 static void hellcreek_select_port_prio(struct hellcreek *hellcreek, int port,
133 u16 val = port << HR_PSEL_PTWSEL_SHIFT;
135 val |= prio << HR_PSEL_PRTCWSEL_SHIFT;
137 hellcreek_write(hellcreek, val, HR_PSEL);
140 static void hellcreek_select_counter(struct hellcreek *hellcreek, int counter)
142 u16 val = counter << HR_CSEL_SHIFT;
144 hellcreek_write(hellcreek, val, HR_CSEL);
146 /* Data sheet states to wait at least 20 internal clock cycles */
150 static void hellcreek_select_vlan(struct hellcreek *hellcreek, int vid,
155 /* Set pvid bit first */
157 val |= HR_VIDCFG_PVID;
158 hellcreek_write(hellcreek, val, HR_VIDCFG);
161 val |= vid << HR_VIDCFG_VID_SHIFT;
162 hellcreek_write(hellcreek, val, HR_VIDCFG);
165 static void hellcreek_select_tgd(struct hellcreek *hellcreek, int port)
167 u16 val = port << TR_TGDSEL_TDGSEL_SHIFT;
169 hellcreek_write(hellcreek, val, TR_TGDSEL);
172 static int hellcreek_wait_until_ready(struct hellcreek *hellcreek)
176 /* Wait up to 1ms, although 3 us should be enough */
177 return readx_poll_timeout(hellcreek_read_ctrl, hellcreek,
178 val, val & HR_CTRL_C_READY,
182 static int hellcreek_wait_until_transitioned(struct hellcreek *hellcreek)
186 return readx_poll_timeout_atomic(hellcreek_read_ctrl, hellcreek,
187 val, !(val & HR_CTRL_C_TRANSITION),
191 static int hellcreek_wait_fdb_ready(struct hellcreek *hellcreek)
195 return readx_poll_timeout_atomic(hellcreek_read_stat, hellcreek,
196 val, !(val & HR_SWSTAT_BUSY),
200 static int hellcreek_detect(struct hellcreek *hellcreek)
202 u16 id, rel_low, rel_high, date_low, date_high, tgd_ver;
206 id = hellcreek_read(hellcreek, HR_MODID_C);
207 rel_low = hellcreek_read(hellcreek, HR_REL_L_C);
208 rel_high = hellcreek_read(hellcreek, HR_REL_H_C);
209 date_low = hellcreek_read(hellcreek, HR_BLD_L_C);
210 date_high = hellcreek_read(hellcreek, HR_BLD_H_C);
211 tgd_ver = hellcreek_read(hellcreek, TR_TGDVER);
213 if (id != hellcreek->pdata->module_id)
216 rel = rel_low | (rel_high << 16);
217 date = date_low | (date_high << 16);
218 tgd_maj = (tgd_ver & TR_TGDVER_REV_MAJ_MASK) >> TR_TGDVER_REV_MAJ_SHIFT;
219 tgd_min = (tgd_ver & TR_TGDVER_REV_MIN_MASK) >> TR_TGDVER_REV_MIN_SHIFT;
221 dev_info(hellcreek->dev, "Module ID=%02x Release=%04x Date=%04x TGD Version=%02x.%02x\n",
222 id, rel, date, tgd_maj, tgd_min);
227 static void hellcreek_feature_detect(struct hellcreek *hellcreek)
231 features = hellcreek_read(hellcreek, HR_FEABITS0);
233 /* Only detect the size of the FDB table. The size and current
234 * utilization can be queried via devlink.
236 hellcreek->fdb_entries = ((features & HR_FEABITS0_FDBBINS_MASK) >>
237 HR_FEABITS0_FDBBINS_SHIFT) * 32;
240 static enum dsa_tag_protocol hellcreek_get_tag_protocol(struct dsa_switch *ds,
242 enum dsa_tag_protocol mp)
244 return DSA_TAG_PROTO_HELLCREEK;
247 static int hellcreek_port_enable(struct dsa_switch *ds, int port,
248 struct phy_device *phy)
250 struct hellcreek *hellcreek = ds->priv;
251 struct hellcreek_port *hellcreek_port;
254 hellcreek_port = &hellcreek->ports[port];
256 dev_dbg(hellcreek->dev, "Enable port %d\n", port);
258 mutex_lock(&hellcreek->reg_lock);
260 hellcreek_select_port(hellcreek, port);
261 val = hellcreek_port->ptcfg;
262 val |= HR_PTCFG_ADMIN_EN;
263 hellcreek_write(hellcreek, val, HR_PTCFG);
264 hellcreek_port->ptcfg = val;
266 mutex_unlock(&hellcreek->reg_lock);
271 static void hellcreek_port_disable(struct dsa_switch *ds, int port)
273 struct hellcreek *hellcreek = ds->priv;
274 struct hellcreek_port *hellcreek_port;
277 hellcreek_port = &hellcreek->ports[port];
279 dev_dbg(hellcreek->dev, "Disable port %d\n", port);
281 mutex_lock(&hellcreek->reg_lock);
283 hellcreek_select_port(hellcreek, port);
284 val = hellcreek_port->ptcfg;
285 val &= ~HR_PTCFG_ADMIN_EN;
286 hellcreek_write(hellcreek, val, HR_PTCFG);
287 hellcreek_port->ptcfg = val;
289 mutex_unlock(&hellcreek->reg_lock);
292 static void hellcreek_get_strings(struct dsa_switch *ds, int port,
293 u32 stringset, uint8_t *data)
297 for (i = 0; i < ARRAY_SIZE(hellcreek_counter); ++i)
298 ethtool_puts(&data, hellcreek_counter[i].name);
301 static int hellcreek_get_sset_count(struct dsa_switch *ds, int port, int sset)
303 if (sset != ETH_SS_STATS)
306 return ARRAY_SIZE(hellcreek_counter);
309 static void hellcreek_get_ethtool_stats(struct dsa_switch *ds, int port,
312 struct hellcreek *hellcreek = ds->priv;
313 struct hellcreek_port *hellcreek_port;
316 hellcreek_port = &hellcreek->ports[port];
318 for (i = 0; i < ARRAY_SIZE(hellcreek_counter); ++i) {
319 const struct hellcreek_counter *counter = &hellcreek_counter[i];
320 u8 offset = counter->offset + port * 64;
324 mutex_lock(&hellcreek->reg_lock);
326 hellcreek_select_counter(hellcreek, offset);
328 /* The registers are locked internally by selecting the
329 * counter. So low and high can be read without reading high
332 high = hellcreek_read(hellcreek, HR_CRDH);
333 low = hellcreek_read(hellcreek, HR_CRDL);
334 value = ((u64)high << 16) | low;
336 hellcreek_port->counter_values[i] += value;
337 data[i] = hellcreek_port->counter_values[i];
339 mutex_unlock(&hellcreek->reg_lock);
343 static u16 hellcreek_private_vid(int port)
345 return VLAN_N_VID - port + 1;
348 static int hellcreek_vlan_prepare(struct dsa_switch *ds, int port,
349 const struct switchdev_obj_port_vlan *vlan,
350 struct netlink_ext_ack *extack)
352 struct hellcreek *hellcreek = ds->priv;
355 dev_dbg(hellcreek->dev, "VLAN prepare for port %d\n", port);
357 /* Restriction: Make sure that nobody uses the "private" VLANs. These
358 * VLANs are internally used by the driver to ensure port
359 * separation. Thus, they cannot be used by someone else.
361 for (i = 0; i < hellcreek->pdata->num_ports; ++i) {
362 const u16 restricted_vid = hellcreek_private_vid(i);
364 if (!dsa_is_user_port(ds, i))
367 if (vlan->vid == restricted_vid) {
368 NL_SET_ERR_MSG_MOD(extack, "VID restricted by driver");
376 static void hellcreek_select_vlan_params(struct hellcreek *hellcreek, int port,
377 int *shift, int *mask)
381 *shift = HR_VIDMBRCFG_P0MBR_SHIFT;
382 *mask = HR_VIDMBRCFG_P0MBR_MASK;
385 *shift = HR_VIDMBRCFG_P1MBR_SHIFT;
386 *mask = HR_VIDMBRCFG_P1MBR_MASK;
389 *shift = HR_VIDMBRCFG_P2MBR_SHIFT;
390 *mask = HR_VIDMBRCFG_P2MBR_MASK;
393 *shift = HR_VIDMBRCFG_P3MBR_SHIFT;
394 *mask = HR_VIDMBRCFG_P3MBR_MASK;
398 dev_err(hellcreek->dev, "Unknown port %d selected!\n", port);
402 static void hellcreek_apply_vlan(struct hellcreek *hellcreek, int port, u16 vid,
403 bool pvid, bool untagged)
408 dev_dbg(hellcreek->dev, "Apply VLAN: port=%d vid=%u pvid=%d untagged=%d",
409 port, vid, pvid, untagged);
411 mutex_lock(&hellcreek->reg_lock);
413 hellcreek_select_port(hellcreek, port);
414 hellcreek_select_vlan(hellcreek, vid, pvid);
416 /* Setup port vlan membership */
417 hellcreek_select_vlan_params(hellcreek, port, &shift, &mask);
418 val = hellcreek->vidmbrcfg[vid];
421 val |= HELLCREEK_VLAN_UNTAGGED_MEMBER << shift;
423 val |= HELLCREEK_VLAN_TAGGED_MEMBER << shift;
425 hellcreek_write(hellcreek, val, HR_VIDMBRCFG);
426 hellcreek->vidmbrcfg[vid] = val;
428 mutex_unlock(&hellcreek->reg_lock);
431 static void hellcreek_unapply_vlan(struct hellcreek *hellcreek, int port,
437 dev_dbg(hellcreek->dev, "Unapply VLAN: port=%d vid=%u\n", port, vid);
439 mutex_lock(&hellcreek->reg_lock);
441 hellcreek_select_vlan(hellcreek, vid, false);
443 /* Setup port vlan membership */
444 hellcreek_select_vlan_params(hellcreek, port, &shift, &mask);
445 val = hellcreek->vidmbrcfg[vid];
447 val |= HELLCREEK_VLAN_NO_MEMBER << shift;
449 hellcreek_write(hellcreek, val, HR_VIDMBRCFG);
450 hellcreek->vidmbrcfg[vid] = val;
452 mutex_unlock(&hellcreek->reg_lock);
455 static int hellcreek_vlan_add(struct dsa_switch *ds, int port,
456 const struct switchdev_obj_port_vlan *vlan,
457 struct netlink_ext_ack *extack)
459 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
460 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
461 struct hellcreek *hellcreek = ds->priv;
464 err = hellcreek_vlan_prepare(ds, port, vlan, extack);
468 dev_dbg(hellcreek->dev, "Add VLAN %d on port %d, %s, %s\n",
469 vlan->vid, port, untagged ? "untagged" : "tagged",
470 pvid ? "PVID" : "no PVID");
472 hellcreek_apply_vlan(hellcreek, port, vlan->vid, pvid, untagged);
477 static int hellcreek_vlan_del(struct dsa_switch *ds, int port,
478 const struct switchdev_obj_port_vlan *vlan)
480 struct hellcreek *hellcreek = ds->priv;
482 dev_dbg(hellcreek->dev, "Remove VLAN %d on port %d\n", vlan->vid, port);
484 hellcreek_unapply_vlan(hellcreek, port, vlan->vid);
489 static void hellcreek_port_stp_state_set(struct dsa_switch *ds, int port,
492 struct hellcreek *hellcreek = ds->priv;
493 struct hellcreek_port *hellcreek_port;
494 const char *new_state;
497 mutex_lock(&hellcreek->reg_lock);
499 hellcreek_port = &hellcreek->ports[port];
500 val = hellcreek_port->ptcfg;
503 case BR_STATE_DISABLED:
504 new_state = "DISABLED";
505 val |= HR_PTCFG_BLOCKED;
506 val &= ~HR_PTCFG_LEARNING_EN;
508 case BR_STATE_BLOCKING:
509 new_state = "BLOCKING";
510 val |= HR_PTCFG_BLOCKED;
511 val &= ~HR_PTCFG_LEARNING_EN;
513 case BR_STATE_LISTENING:
514 new_state = "LISTENING";
515 val |= HR_PTCFG_BLOCKED;
516 val &= ~HR_PTCFG_LEARNING_EN;
518 case BR_STATE_LEARNING:
519 new_state = "LEARNING";
520 val |= HR_PTCFG_BLOCKED;
521 val |= HR_PTCFG_LEARNING_EN;
523 case BR_STATE_FORWARDING:
524 new_state = "FORWARDING";
525 val &= ~HR_PTCFG_BLOCKED;
526 val |= HR_PTCFG_LEARNING_EN;
529 new_state = "UNKNOWN";
532 hellcreek_select_port(hellcreek, port);
533 hellcreek_write(hellcreek, val, HR_PTCFG);
534 hellcreek_port->ptcfg = val;
536 mutex_unlock(&hellcreek->reg_lock);
538 dev_dbg(hellcreek->dev, "Configured STP state for port %d: %s\n",
542 static void hellcreek_setup_ingressflt(struct hellcreek *hellcreek, int port,
545 struct hellcreek_port *hellcreek_port = &hellcreek->ports[port];
548 mutex_lock(&hellcreek->reg_lock);
550 ptcfg = hellcreek_port->ptcfg;
553 ptcfg |= HR_PTCFG_INGRESSFLT;
555 ptcfg &= ~HR_PTCFG_INGRESSFLT;
557 hellcreek_select_port(hellcreek, port);
558 hellcreek_write(hellcreek, ptcfg, HR_PTCFG);
559 hellcreek_port->ptcfg = ptcfg;
561 mutex_unlock(&hellcreek->reg_lock);
564 static void hellcreek_setup_vlan_awareness(struct hellcreek *hellcreek,
569 mutex_lock(&hellcreek->reg_lock);
571 swcfg = hellcreek->swcfg;
574 swcfg |= HR_SWCFG_VLAN_UNAWARE;
576 swcfg &= ~HR_SWCFG_VLAN_UNAWARE;
578 hellcreek_write(hellcreek, swcfg, HR_SWCFG);
580 mutex_unlock(&hellcreek->reg_lock);
583 /* Default setup for DSA: VLAN <X>: CPU and Port <X> egress untagged. */
584 static void hellcreek_setup_vlan_membership(struct dsa_switch *ds, int port,
587 const u16 vid = hellcreek_private_vid(port);
588 int upstream = dsa_upstream_port(ds, port);
589 struct hellcreek *hellcreek = ds->priv;
591 /* Apply vid to port as egress untagged and port vlan id */
593 hellcreek_apply_vlan(hellcreek, port, vid, true, true);
595 hellcreek_unapply_vlan(hellcreek, port, vid);
597 /* Apply vid to cpu port as well */
599 hellcreek_apply_vlan(hellcreek, upstream, vid, false, true);
601 hellcreek_unapply_vlan(hellcreek, upstream, vid);
604 static void hellcreek_port_set_ucast_flood(struct hellcreek *hellcreek,
605 int port, bool enable)
607 struct hellcreek_port *hellcreek_port;
610 hellcreek_port = &hellcreek->ports[port];
612 dev_dbg(hellcreek->dev, "%s unicast flooding on port %d\n",
613 enable ? "Enable" : "Disable", port);
615 mutex_lock(&hellcreek->reg_lock);
617 hellcreek_select_port(hellcreek, port);
618 val = hellcreek_port->ptcfg;
620 val &= ~HR_PTCFG_UUC_FLT;
622 val |= HR_PTCFG_UUC_FLT;
623 hellcreek_write(hellcreek, val, HR_PTCFG);
624 hellcreek_port->ptcfg = val;
626 mutex_unlock(&hellcreek->reg_lock);
629 static void hellcreek_port_set_mcast_flood(struct hellcreek *hellcreek,
630 int port, bool enable)
632 struct hellcreek_port *hellcreek_port;
635 hellcreek_port = &hellcreek->ports[port];
637 dev_dbg(hellcreek->dev, "%s multicast flooding on port %d\n",
638 enable ? "Enable" : "Disable", port);
640 mutex_lock(&hellcreek->reg_lock);
642 hellcreek_select_port(hellcreek, port);
643 val = hellcreek_port->ptcfg;
645 val &= ~HR_PTCFG_UMC_FLT;
647 val |= HR_PTCFG_UMC_FLT;
648 hellcreek_write(hellcreek, val, HR_PTCFG);
649 hellcreek_port->ptcfg = val;
651 mutex_unlock(&hellcreek->reg_lock);
654 static int hellcreek_pre_bridge_flags(struct dsa_switch *ds, int port,
655 struct switchdev_brport_flags flags,
656 struct netlink_ext_ack *extack)
658 if (flags.mask & ~(BR_FLOOD | BR_MCAST_FLOOD))
664 static int hellcreek_bridge_flags(struct dsa_switch *ds, int port,
665 struct switchdev_brport_flags flags,
666 struct netlink_ext_ack *extack)
668 struct hellcreek *hellcreek = ds->priv;
670 if (flags.mask & BR_FLOOD)
671 hellcreek_port_set_ucast_flood(hellcreek, port,
672 !!(flags.val & BR_FLOOD));
674 if (flags.mask & BR_MCAST_FLOOD)
675 hellcreek_port_set_mcast_flood(hellcreek, port,
676 !!(flags.val & BR_MCAST_FLOOD));
681 static int hellcreek_port_bridge_join(struct dsa_switch *ds, int port,
682 struct dsa_bridge bridge,
683 bool *tx_fwd_offload,
684 struct netlink_ext_ack *extack)
686 struct hellcreek *hellcreek = ds->priv;
688 dev_dbg(hellcreek->dev, "Port %d joins a bridge\n", port);
690 /* When joining a vlan_filtering bridge, keep the switch VLAN aware */
691 if (!ds->vlan_filtering)
692 hellcreek_setup_vlan_awareness(hellcreek, false);
694 /* Drop private vlans */
695 hellcreek_setup_vlan_membership(ds, port, false);
700 static void hellcreek_port_bridge_leave(struct dsa_switch *ds, int port,
701 struct dsa_bridge bridge)
703 struct hellcreek *hellcreek = ds->priv;
705 dev_dbg(hellcreek->dev, "Port %d leaves a bridge\n", port);
707 /* Enable VLAN awareness */
708 hellcreek_setup_vlan_awareness(hellcreek, true);
710 /* Enable private vlans */
711 hellcreek_setup_vlan_membership(ds, port, true);
714 static int __hellcreek_fdb_add(struct hellcreek *hellcreek,
715 const struct hellcreek_fdb_entry *entry)
719 dev_dbg(hellcreek->dev, "Add static FDB entry: MAC=%pM, MASK=0x%02x, "
720 "OBT=%d, PASS_BLOCKED=%d, REPRIO_EN=%d, PRIO=%d\n", entry->mac,
721 entry->portmask, entry->is_obt, entry->pass_blocked,
722 entry->reprio_en, entry->reprio_tc);
724 /* Add mac address */
725 hellcreek_write(hellcreek, entry->mac[1] | (entry->mac[0] << 8), HR_FDBWDH);
726 hellcreek_write(hellcreek, entry->mac[3] | (entry->mac[2] << 8), HR_FDBWDM);
727 hellcreek_write(hellcreek, entry->mac[5] | (entry->mac[4] << 8), HR_FDBWDL);
730 meta |= entry->portmask << HR_FDBWRM0_PORTMASK_SHIFT;
732 meta |= HR_FDBWRM0_OBT;
733 if (entry->pass_blocked)
734 meta |= HR_FDBWRM0_PASS_BLOCKED;
735 if (entry->reprio_en) {
736 meta |= HR_FDBWRM0_REPRIO_EN;
737 meta |= entry->reprio_tc << HR_FDBWRM0_REPRIO_TC_SHIFT;
739 hellcreek_write(hellcreek, meta, HR_FDBWRM0);
742 hellcreek_write(hellcreek, 0x00, HR_FDBWRCMD);
744 /* Wait until done */
745 return hellcreek_wait_fdb_ready(hellcreek);
748 static int __hellcreek_fdb_del(struct hellcreek *hellcreek,
749 const struct hellcreek_fdb_entry *entry)
751 dev_dbg(hellcreek->dev, "Delete FDB entry: MAC=%pM!\n", entry->mac);
753 /* Delete by matching idx */
754 hellcreek_write(hellcreek, entry->idx | HR_FDBWRCMD_FDBDEL, HR_FDBWRCMD);
756 /* Wait until done */
757 return hellcreek_wait_fdb_ready(hellcreek);
760 static void hellcreek_populate_fdb_entry(struct hellcreek *hellcreek,
761 struct hellcreek_fdb_entry *entry,
764 unsigned char addr[ETH_ALEN];
768 meta = hellcreek_read(hellcreek, HR_FDBMDRD);
769 mac = hellcreek_read(hellcreek, HR_FDBRDL);
770 addr[5] = mac & 0xff;
771 addr[4] = (mac & 0xff00) >> 8;
772 mac = hellcreek_read(hellcreek, HR_FDBRDM);
773 addr[3] = mac & 0xff;
774 addr[2] = (mac & 0xff00) >> 8;
775 mac = hellcreek_read(hellcreek, HR_FDBRDH);
776 addr[1] = mac & 0xff;
777 addr[0] = (mac & 0xff00) >> 8;
779 /* Populate @entry */
780 memcpy(entry->mac, addr, sizeof(addr));
782 entry->portmask = (meta & HR_FDBMDRD_PORTMASK_MASK) >>
783 HR_FDBMDRD_PORTMASK_SHIFT;
784 entry->age = (meta & HR_FDBMDRD_AGE_MASK) >>
785 HR_FDBMDRD_AGE_SHIFT;
786 entry->is_obt = !!(meta & HR_FDBMDRD_OBT);
787 entry->pass_blocked = !!(meta & HR_FDBMDRD_PASS_BLOCKED);
788 entry->is_static = !!(meta & HR_FDBMDRD_STATIC);
789 entry->reprio_tc = (meta & HR_FDBMDRD_REPRIO_TC_MASK) >>
790 HR_FDBMDRD_REPRIO_TC_SHIFT;
791 entry->reprio_en = !!(meta & HR_FDBMDRD_REPRIO_EN);
794 /* Retrieve the index of a FDB entry by mac address. Currently we search through
795 * the complete table in hardware. If that's too slow, we might have to cache
796 * the complete FDB table in software.
798 static int hellcreek_fdb_get(struct hellcreek *hellcreek,
799 const unsigned char *dest,
800 struct hellcreek_fdb_entry *entry)
804 /* Set read pointer to zero: The read of HR_FDBMAX (read-only register)
805 * should reset the internal pointer. But, that doesn't work. The vendor
806 * suggested a subsequent write as workaround. Same for HR_FDBRDH below.
808 hellcreek_read(hellcreek, HR_FDBMAX);
809 hellcreek_write(hellcreek, 0x00, HR_FDBMAX);
811 /* We have to read the complete table, because the switch/driver might
812 * enter new entries anywhere.
814 for (i = 0; i < hellcreek->fdb_entries; ++i) {
815 struct hellcreek_fdb_entry tmp = { 0 };
818 hellcreek_populate_fdb_entry(hellcreek, &tmp, i);
820 /* Force next entry */
821 hellcreek_write(hellcreek, 0x00, HR_FDBRDH);
823 if (memcmp(tmp.mac, dest, ETH_ALEN))
827 memcpy(entry, &tmp, sizeof(*entry));
835 static int hellcreek_fdb_add(struct dsa_switch *ds, int port,
836 const unsigned char *addr, u16 vid,
839 struct hellcreek_fdb_entry entry = { 0 };
840 struct hellcreek *hellcreek = ds->priv;
843 dev_dbg(hellcreek->dev, "Add FDB entry for MAC=%pM\n", addr);
845 mutex_lock(&hellcreek->reg_lock);
847 ret = hellcreek_fdb_get(hellcreek, addr, &entry);
850 memcpy(entry.mac, addr, sizeof(entry.mac));
851 entry.portmask = BIT(port);
853 ret = __hellcreek_fdb_add(hellcreek, &entry);
855 dev_err(hellcreek->dev, "Failed to add FDB entry!\n");
860 ret = __hellcreek_fdb_del(hellcreek, &entry);
862 dev_err(hellcreek->dev, "Failed to delete FDB entry!\n");
866 entry.portmask |= BIT(port);
868 ret = __hellcreek_fdb_add(hellcreek, &entry);
870 dev_err(hellcreek->dev, "Failed to add FDB entry!\n");
876 mutex_unlock(&hellcreek->reg_lock);
881 static int hellcreek_fdb_del(struct dsa_switch *ds, int port,
882 const unsigned char *addr, u16 vid,
885 struct hellcreek_fdb_entry entry = { 0 };
886 struct hellcreek *hellcreek = ds->priv;
889 dev_dbg(hellcreek->dev, "Delete FDB entry for MAC=%pM\n", addr);
891 mutex_lock(&hellcreek->reg_lock);
893 ret = hellcreek_fdb_get(hellcreek, addr, &entry);
896 dev_err(hellcreek->dev, "FDB entry for deletion not found!\n");
899 ret = __hellcreek_fdb_del(hellcreek, &entry);
901 dev_err(hellcreek->dev, "Failed to delete FDB entry!\n");
905 entry.portmask &= ~BIT(port);
907 if (entry.portmask != 0x00) {
908 ret = __hellcreek_fdb_add(hellcreek, &entry);
910 dev_err(hellcreek->dev, "Failed to add FDB entry!\n");
917 mutex_unlock(&hellcreek->reg_lock);
922 static int hellcreek_fdb_dump(struct dsa_switch *ds, int port,
923 dsa_fdb_dump_cb_t *cb, void *data)
925 struct hellcreek *hellcreek = ds->priv;
930 mutex_lock(&hellcreek->reg_lock);
932 /* Set read pointer to zero: The read of HR_FDBMAX (read-only register)
933 * should reset the internal pointer. But, that doesn't work. The vendor
934 * suggested a subsequent write as workaround. Same for HR_FDBRDH below.
936 entries = hellcreek_read(hellcreek, HR_FDBMAX);
937 hellcreek_write(hellcreek, 0x00, HR_FDBMAX);
939 dev_dbg(hellcreek->dev, "FDB dump for port %d, entries=%d!\n", port, entries);
942 for (i = 0; i < hellcreek->fdb_entries; ++i) {
943 struct hellcreek_fdb_entry entry = { 0 };
946 hellcreek_populate_fdb_entry(hellcreek, &entry, i);
948 /* Force next entry */
949 hellcreek_write(hellcreek, 0x00, HR_FDBRDH);
952 if (is_zero_ether_addr(entry.mac))
955 /* Check port mask */
956 if (!(entry.portmask & BIT(port)))
959 ret = cb(entry.mac, 0, entry.is_static, data);
964 mutex_unlock(&hellcreek->reg_lock);
969 static int hellcreek_vlan_filtering(struct dsa_switch *ds, int port,
971 struct netlink_ext_ack *extack)
973 struct hellcreek *hellcreek = ds->priv;
975 dev_dbg(hellcreek->dev, "%s VLAN filtering on port %d\n",
976 vlan_filtering ? "Enable" : "Disable", port);
978 /* Configure port to drop packages with not known vids */
979 hellcreek_setup_ingressflt(hellcreek, port, vlan_filtering);
981 /* Enable VLAN awareness on the switch. This save due to
982 * ds->vlan_filtering_is_global.
984 hellcreek_setup_vlan_awareness(hellcreek, vlan_filtering);
989 static int hellcreek_enable_ip_core(struct hellcreek *hellcreek)
994 mutex_lock(&hellcreek->reg_lock);
996 val = hellcreek_read(hellcreek, HR_CTRL_C);
997 val |= HR_CTRL_C_ENABLE;
998 hellcreek_write(hellcreek, val, HR_CTRL_C);
999 ret = hellcreek_wait_until_transitioned(hellcreek);
1001 mutex_unlock(&hellcreek->reg_lock);
1006 static void hellcreek_setup_cpu_and_tunnel_port(struct hellcreek *hellcreek)
1008 struct hellcreek_port *tunnel_port = &hellcreek->ports[TUNNEL_PORT];
1009 struct hellcreek_port *cpu_port = &hellcreek->ports[CPU_PORT];
1012 ptcfg |= HR_PTCFG_LEARNING_EN | HR_PTCFG_ADMIN_EN;
1014 mutex_lock(&hellcreek->reg_lock);
1016 hellcreek_select_port(hellcreek, CPU_PORT);
1017 hellcreek_write(hellcreek, ptcfg, HR_PTCFG);
1019 hellcreek_select_port(hellcreek, TUNNEL_PORT);
1020 hellcreek_write(hellcreek, ptcfg, HR_PTCFG);
1022 cpu_port->ptcfg = ptcfg;
1023 tunnel_port->ptcfg = ptcfg;
1025 mutex_unlock(&hellcreek->reg_lock);
1028 static void hellcreek_setup_tc_identity_mapping(struct hellcreek *hellcreek)
1032 /* The switch has multiple egress queues per port. The queue is selected
1033 * via the PCP field in the VLAN header. The switch internally deals
1034 * with traffic classes instead of PCP values and this mapping is
1037 * The default mapping is (PCP - TC):
1047 * The default should be an identity mapping.
1050 for (i = 0; i < 8; ++i) {
1051 mutex_lock(&hellcreek->reg_lock);
1053 hellcreek_select_prio(hellcreek, i);
1054 hellcreek_write(hellcreek,
1055 i << HR_PRTCCFG_PCP_TC_MAP_SHIFT,
1058 mutex_unlock(&hellcreek->reg_lock);
1062 static int hellcreek_setup_fdb(struct hellcreek *hellcreek)
1064 static struct hellcreek_fdb_entry l2_ptp = {
1065 /* MAC: 01-1B-19-00-00-00 */
1066 .mac = { 0x01, 0x1b, 0x19, 0x00, 0x00, 0x00 },
1067 .portmask = 0x03, /* Management ports */
1072 .reprio_tc = 6, /* TC: 6 as per IEEE 802.1AS */
1075 static struct hellcreek_fdb_entry udp4_ptp = {
1076 /* MAC: 01-00-5E-00-01-81 */
1077 .mac = { 0x01, 0x00, 0x5e, 0x00, 0x01, 0x81 },
1078 .portmask = 0x03, /* Management ports */
1086 static struct hellcreek_fdb_entry udp6_ptp = {
1087 /* MAC: 33-33-00-00-01-81 */
1088 .mac = { 0x33, 0x33, 0x00, 0x00, 0x01, 0x81 },
1089 .portmask = 0x03, /* Management ports */
1097 static struct hellcreek_fdb_entry l2_p2p = {
1098 /* MAC: 01-80-C2-00-00-0E */
1099 .mac = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x0e },
1100 .portmask = 0x03, /* Management ports */
1105 .reprio_tc = 6, /* TC: 6 as per IEEE 802.1AS */
1108 static struct hellcreek_fdb_entry udp4_p2p = {
1109 /* MAC: 01-00-5E-00-00-6B */
1110 .mac = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x6b },
1111 .portmask = 0x03, /* Management ports */
1119 static struct hellcreek_fdb_entry udp6_p2p = {
1120 /* MAC: 33-33-00-00-00-6B */
1121 .mac = { 0x33, 0x33, 0x00, 0x00, 0x00, 0x6b },
1122 .portmask = 0x03, /* Management ports */
1130 static struct hellcreek_fdb_entry stp = {
1131 /* MAC: 01-80-C2-00-00-00 */
1132 .mac = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 },
1133 .portmask = 0x03, /* Management ports */
1143 mutex_lock(&hellcreek->reg_lock);
1144 ret = __hellcreek_fdb_add(hellcreek, &l2_ptp);
1147 ret = __hellcreek_fdb_add(hellcreek, &udp4_ptp);
1150 ret = __hellcreek_fdb_add(hellcreek, &udp6_ptp);
1153 ret = __hellcreek_fdb_add(hellcreek, &l2_p2p);
1156 ret = __hellcreek_fdb_add(hellcreek, &udp4_p2p);
1159 ret = __hellcreek_fdb_add(hellcreek, &udp6_p2p);
1162 ret = __hellcreek_fdb_add(hellcreek, &stp);
1164 mutex_unlock(&hellcreek->reg_lock);
1169 static int hellcreek_devlink_info_get(struct dsa_switch *ds,
1170 struct devlink_info_req *req,
1171 struct netlink_ext_ack *extack)
1173 struct hellcreek *hellcreek = ds->priv;
1175 return devlink_info_version_fixed_put(req,
1176 DEVLINK_INFO_VERSION_GENERIC_ASIC_ID,
1177 hellcreek->pdata->name);
1180 static u64 hellcreek_devlink_vlan_table_get(void *priv)
1182 struct hellcreek *hellcreek = priv;
1186 mutex_lock(&hellcreek->reg_lock);
1187 for (i = 0; i < VLAN_N_VID; ++i)
1188 if (hellcreek->vidmbrcfg[i])
1190 mutex_unlock(&hellcreek->reg_lock);
1195 static u64 hellcreek_devlink_fdb_table_get(void *priv)
1197 struct hellcreek *hellcreek = priv;
1200 /* Reading this register has side effects. Synchronize against the other
1203 mutex_lock(&hellcreek->reg_lock);
1204 count = hellcreek_read(hellcreek, HR_FDBMAX);
1205 mutex_unlock(&hellcreek->reg_lock);
1210 static int hellcreek_setup_devlink_resources(struct dsa_switch *ds)
1212 struct devlink_resource_size_params size_vlan_params;
1213 struct devlink_resource_size_params size_fdb_params;
1214 struct hellcreek *hellcreek = ds->priv;
1217 devlink_resource_size_params_init(&size_vlan_params, VLAN_N_VID,
1219 1, DEVLINK_RESOURCE_UNIT_ENTRY);
1221 devlink_resource_size_params_init(&size_fdb_params,
1222 hellcreek->fdb_entries,
1223 hellcreek->fdb_entries,
1224 1, DEVLINK_RESOURCE_UNIT_ENTRY);
1226 err = dsa_devlink_resource_register(ds, "VLAN", VLAN_N_VID,
1227 HELLCREEK_DEVLINK_PARAM_ID_VLAN_TABLE,
1228 DEVLINK_RESOURCE_ID_PARENT_TOP,
1233 err = dsa_devlink_resource_register(ds, "FDB", hellcreek->fdb_entries,
1234 HELLCREEK_DEVLINK_PARAM_ID_FDB_TABLE,
1235 DEVLINK_RESOURCE_ID_PARENT_TOP,
1240 dsa_devlink_resource_occ_get_register(ds,
1241 HELLCREEK_DEVLINK_PARAM_ID_VLAN_TABLE,
1242 hellcreek_devlink_vlan_table_get,
1245 dsa_devlink_resource_occ_get_register(ds,
1246 HELLCREEK_DEVLINK_PARAM_ID_FDB_TABLE,
1247 hellcreek_devlink_fdb_table_get,
1253 dsa_devlink_resources_unregister(ds);
1258 static int hellcreek_devlink_region_vlan_snapshot(struct devlink *dl,
1259 const struct devlink_region_ops *ops,
1260 struct netlink_ext_ack *extack,
1263 struct hellcreek_devlink_vlan_entry *table, *entry;
1264 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
1265 struct hellcreek *hellcreek = ds->priv;
1268 table = kcalloc(VLAN_N_VID, sizeof(*entry), GFP_KERNEL);
1274 mutex_lock(&hellcreek->reg_lock);
1275 for (i = 0; i < VLAN_N_VID; ++i, ++entry) {
1276 entry->member = hellcreek->vidmbrcfg[i];
1279 mutex_unlock(&hellcreek->reg_lock);
1281 *data = (u8 *)table;
1286 static int hellcreek_devlink_region_fdb_snapshot(struct devlink *dl,
1287 const struct devlink_region_ops *ops,
1288 struct netlink_ext_ack *extack,
1291 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
1292 struct hellcreek_fdb_entry *table, *entry;
1293 struct hellcreek *hellcreek = ds->priv;
1296 table = kcalloc(hellcreek->fdb_entries, sizeof(*entry), GFP_KERNEL);
1302 mutex_lock(&hellcreek->reg_lock);
1304 /* Start table read */
1305 hellcreek_read(hellcreek, HR_FDBMAX);
1306 hellcreek_write(hellcreek, 0x00, HR_FDBMAX);
1308 for (i = 0; i < hellcreek->fdb_entries; ++i, ++entry) {
1309 /* Read current entry */
1310 hellcreek_populate_fdb_entry(hellcreek, entry, i);
1312 /* Advance read pointer */
1313 hellcreek_write(hellcreek, 0x00, HR_FDBRDH);
1316 mutex_unlock(&hellcreek->reg_lock);
1318 *data = (u8 *)table;
1323 static struct devlink_region_ops hellcreek_region_vlan_ops = {
1325 .snapshot = hellcreek_devlink_region_vlan_snapshot,
1326 .destructor = kfree,
1329 static struct devlink_region_ops hellcreek_region_fdb_ops = {
1331 .snapshot = hellcreek_devlink_region_fdb_snapshot,
1332 .destructor = kfree,
1335 static int hellcreek_setup_devlink_regions(struct dsa_switch *ds)
1337 struct hellcreek *hellcreek = ds->priv;
1338 struct devlink_region_ops *ops;
1339 struct devlink_region *region;
1344 size = VLAN_N_VID * sizeof(struct hellcreek_devlink_vlan_entry);
1345 ops = &hellcreek_region_vlan_ops;
1347 region = dsa_devlink_region_create(ds, ops, 1, size);
1349 return PTR_ERR(region);
1351 hellcreek->vlan_region = region;
1354 size = hellcreek->fdb_entries * sizeof(struct hellcreek_fdb_entry);
1355 ops = &hellcreek_region_fdb_ops;
1357 region = dsa_devlink_region_create(ds, ops, 1, size);
1358 if (IS_ERR(region)) {
1359 ret = PTR_ERR(region);
1363 hellcreek->fdb_region = region;
1368 dsa_devlink_region_destroy(hellcreek->vlan_region);
1373 static void hellcreek_teardown_devlink_regions(struct dsa_switch *ds)
1375 struct hellcreek *hellcreek = ds->priv;
1377 dsa_devlink_region_destroy(hellcreek->fdb_region);
1378 dsa_devlink_region_destroy(hellcreek->vlan_region);
1381 static int hellcreek_setup(struct dsa_switch *ds)
1383 struct hellcreek *hellcreek = ds->priv;
1387 dev_dbg(hellcreek->dev, "Set up the switch\n");
1390 ret = hellcreek_enable_ip_core(hellcreek);
1392 dev_err(hellcreek->dev, "Failed to enable IP core!\n");
1396 /* Enable CPU/Tunnel ports */
1397 hellcreek_setup_cpu_and_tunnel_port(hellcreek);
1399 /* Switch config: Keep defaults, enable FDB aging and learning and tag
1400 * each frame from/to cpu port for DSA tagging. Also enable the length
1401 * aware shaping mode. This eliminates the need for Qbv guard bands.
1403 swcfg |= HR_SWCFG_FDBAGE_EN |
1404 HR_SWCFG_FDBLRN_EN |
1405 HR_SWCFG_ALWAYS_OBT |
1406 (HR_SWCFG_LAS_ON << HR_SWCFG_LAS_MODE_SHIFT);
1407 hellcreek->swcfg = swcfg;
1408 hellcreek_write(hellcreek, swcfg, HR_SWCFG);
1410 /* Initial vlan membership to reflect port separation */
1411 for (i = 0; i < ds->num_ports; ++i) {
1412 if (!dsa_is_user_port(ds, i))
1415 hellcreek_setup_vlan_membership(ds, i, true);
1418 /* Configure PCP <-> TC mapping */
1419 hellcreek_setup_tc_identity_mapping(hellcreek);
1421 /* The VLAN awareness is a global switch setting. Therefore, mixed vlan
1422 * filtering setups are not supported.
1424 ds->vlan_filtering_is_global = true;
1425 ds->needs_standalone_vlan_filtering = true;
1427 /* Intercept _all_ PTP multicast traffic */
1428 ret = hellcreek_setup_fdb(hellcreek);
1430 dev_err(hellcreek->dev,
1431 "Failed to insert static PTP FDB entries\n");
1435 /* Register devlink resources with DSA */
1436 ret = hellcreek_setup_devlink_resources(ds);
1438 dev_err(hellcreek->dev,
1439 "Failed to setup devlink resources!\n");
1443 ret = hellcreek_setup_devlink_regions(ds);
1445 dev_err(hellcreek->dev,
1446 "Failed to setup devlink regions!\n");
1453 dsa_devlink_resources_unregister(ds);
1458 static void hellcreek_teardown(struct dsa_switch *ds)
1460 hellcreek_teardown_devlink_regions(ds);
1461 dsa_devlink_resources_unregister(ds);
1464 static void hellcreek_phylink_get_caps(struct dsa_switch *ds, int port,
1465 struct phylink_config *config)
1467 struct hellcreek *hellcreek = ds->priv;
1469 __set_bit(PHY_INTERFACE_MODE_MII, config->supported_interfaces);
1470 __set_bit(PHY_INTERFACE_MODE_RGMII, config->supported_interfaces);
1472 /* Include GMII - the hardware does not support this interface
1473 * mode, but it's the default interface mode for phylib, so we
1474 * need it for compatibility with existing DT.
1476 __set_bit(PHY_INTERFACE_MODE_GMII, config->supported_interfaces);
1478 /* The MAC settings are a hardware configuration option and cannot be
1479 * changed at run time or by strapping. Therefore the attached PHYs
1480 * should be programmed to only advertise settings which are supported
1483 if (hellcreek->pdata->is_100_mbits)
1484 config->mac_capabilities = MAC_100FD;
1486 config->mac_capabilities = MAC_1000FD;
1490 hellcreek_port_prechangeupper(struct dsa_switch *ds, int port,
1491 struct netdev_notifier_changeupper_info *info)
1493 struct hellcreek *hellcreek = ds->priv;
1499 dev_dbg(hellcreek->dev, "Pre change upper for port %d\n", port);
1502 * Deny VLAN devices on top of lan ports with the same VLAN ids, because
1503 * it breaks the port separation due to the private VLANs. Example:
1505 * lan0.100 *and* lan1.100 cannot be used in parallel. However, lan0.99
1506 * and lan1.100 works.
1509 if (!is_vlan_dev(info->upper_dev))
1512 vid = vlan_dev_vlan_id(info->upper_dev);
1514 /* For all ports, check bitmaps */
1515 mutex_lock(&hellcreek->vlan_lock);
1516 for (i = 0; i < hellcreek->pdata->num_ports; ++i) {
1517 if (!dsa_is_user_port(ds, i))
1523 used = used && test_bit(vid, hellcreek->ports[i].vlan_dev_bitmap);
1530 set_bit(vid, hellcreek->ports[port].vlan_dev_bitmap);
1535 mutex_unlock(&hellcreek->vlan_lock);
1540 static void hellcreek_setup_maxsdu(struct hellcreek *hellcreek, int port,
1541 const struct tc_taprio_qopt_offload *schedule)
1545 for (tc = 0; tc < 8; ++tc) {
1546 u32 max_sdu = schedule->max_sdu[tc] + VLAN_ETH_HLEN - ETH_FCS_LEN;
1549 if (!schedule->max_sdu[tc])
1552 dev_dbg(hellcreek->dev, "Configure max-sdu %u for tc %d on port %d\n",
1555 hellcreek_select_port_prio(hellcreek, port, tc);
1557 val = (max_sdu & HR_PTPRTCCFG_MAXSDU_MASK) << HR_PTPRTCCFG_MAXSDU_SHIFT;
1559 hellcreek_write(hellcreek, val, HR_PTPRTCCFG);
1563 static void hellcreek_reset_maxsdu(struct hellcreek *hellcreek, int port)
1567 for (tc = 0; tc < 8; ++tc) {
1570 hellcreek_select_port_prio(hellcreek, port, tc);
1572 val = (HELLCREEK_DEFAULT_MAX_SDU & HR_PTPRTCCFG_MAXSDU_MASK)
1573 << HR_PTPRTCCFG_MAXSDU_SHIFT;
1575 hellcreek_write(hellcreek, val, HR_PTPRTCCFG);
1579 static void hellcreek_setup_gcl(struct hellcreek *hellcreek, int port,
1580 const struct tc_taprio_qopt_offload *schedule)
1582 const struct tc_taprio_sched_entry *cur, *initial, *next;
1585 cur = initial = &schedule->entries[0];
1588 for (i = 1; i <= schedule->num_entries; ++i) {
1592 if (i == schedule->num_entries)
1593 gates = initial->gate_mask ^
1596 gates = next->gate_mask ^
1601 if (i == schedule->num_entries)
1602 data |= TR_GCLDAT_GCLWRLAST;
1605 hellcreek_write(hellcreek, data, TR_GCLDAT);
1608 hellcreek_write(hellcreek,
1609 cur->interval & 0x0000ffff,
1611 hellcreek_write(hellcreek,
1612 (cur->interval & 0xffff0000) >> 16,
1616 data = ((i - 1) << TR_GCLCMD_GCLWRADR_SHIFT) |
1617 (initial->gate_mask <<
1618 TR_GCLCMD_INIT_GATE_STATES_SHIFT);
1619 hellcreek_write(hellcreek, data, TR_GCLCMD);
1626 static void hellcreek_set_cycle_time(struct hellcreek *hellcreek,
1627 const struct tc_taprio_qopt_offload *schedule)
1629 u32 cycle_time = schedule->cycle_time;
1631 hellcreek_write(hellcreek, cycle_time & 0x0000ffff, TR_CTWRL);
1632 hellcreek_write(hellcreek, (cycle_time & 0xffff0000) >> 16, TR_CTWRH);
1635 static void hellcreek_switch_schedule(struct hellcreek *hellcreek,
1638 struct timespec64 ts = ktime_to_timespec64(start_time);
1640 /* Start schedule at this point of time */
1641 hellcreek_write(hellcreek, ts.tv_nsec & 0x0000ffff, TR_ESTWRL);
1642 hellcreek_write(hellcreek, (ts.tv_nsec & 0xffff0000) >> 16, TR_ESTWRH);
1644 /* Arm timer, set seconds and switch schedule */
1645 hellcreek_write(hellcreek, TR_ESTCMD_ESTARM | TR_ESTCMD_ESTSWCFG |
1646 ((ts.tv_sec & TR_ESTCMD_ESTSEC_MASK) <<
1647 TR_ESTCMD_ESTSEC_SHIFT), TR_ESTCMD);
1650 static bool hellcreek_schedule_startable(struct hellcreek *hellcreek, int port)
1652 struct hellcreek_port *hellcreek_port = &hellcreek->ports[port];
1653 s64 base_time_ns, current_ns;
1655 /* The switch allows a schedule to be started only eight seconds within
1656 * the future. Therefore, check the current PTP time if the schedule is
1660 /* Use the "cached" time. That should be alright, as it's updated quite
1661 * frequently in the PTP code.
1663 mutex_lock(&hellcreek->ptp_lock);
1664 current_ns = hellcreek->seconds * NSEC_PER_SEC + hellcreek->last_ts;
1665 mutex_unlock(&hellcreek->ptp_lock);
1667 /* Calculate difference to admin base time */
1668 base_time_ns = ktime_to_ns(hellcreek_port->current_schedule->base_time);
1670 return base_time_ns - current_ns < (s64)4 * NSEC_PER_SEC;
1673 static void hellcreek_start_schedule(struct hellcreek *hellcreek, int port)
1675 struct hellcreek_port *hellcreek_port = &hellcreek->ports[port];
1676 ktime_t base_time, current_time;
1680 /* First select port */
1681 hellcreek_select_tgd(hellcreek, port);
1683 /* Forward base time into the future if needed */
1684 mutex_lock(&hellcreek->ptp_lock);
1685 current_ns = hellcreek->seconds * NSEC_PER_SEC + hellcreek->last_ts;
1686 mutex_unlock(&hellcreek->ptp_lock);
1688 current_time = ns_to_ktime(current_ns);
1689 base_time = hellcreek_port->current_schedule->base_time;
1690 cycle_time = hellcreek_port->current_schedule->cycle_time;
1692 if (ktime_compare(current_time, base_time) > 0) {
1695 n = div64_s64(ktime_sub_ns(current_time, base_time),
1697 base_time = ktime_add_ns(base_time, (n + 1) * cycle_time);
1700 /* Set admin base time and switch schedule */
1701 hellcreek_switch_schedule(hellcreek, base_time);
1703 taprio_offload_free(hellcreek_port->current_schedule);
1704 hellcreek_port->current_schedule = NULL;
1706 dev_dbg(hellcreek->dev, "Armed EST timer for port %d\n",
1707 hellcreek_port->port);
1710 static void hellcreek_check_schedule(struct work_struct *work)
1712 struct delayed_work *dw = to_delayed_work(work);
1713 struct hellcreek_port *hellcreek_port;
1714 struct hellcreek *hellcreek;
1717 hellcreek_port = dw_to_hellcreek_port(dw);
1718 hellcreek = hellcreek_port->hellcreek;
1720 mutex_lock(&hellcreek->reg_lock);
1722 /* Check starting time */
1723 startable = hellcreek_schedule_startable(hellcreek,
1724 hellcreek_port->port);
1726 hellcreek_start_schedule(hellcreek, hellcreek_port->port);
1727 mutex_unlock(&hellcreek->reg_lock);
1731 mutex_unlock(&hellcreek->reg_lock);
1734 schedule_delayed_work(&hellcreek_port->schedule_work,
1735 HELLCREEK_SCHEDULE_PERIOD);
1738 static int hellcreek_port_set_schedule(struct dsa_switch *ds, int port,
1739 struct tc_taprio_qopt_offload *taprio)
1741 struct hellcreek *hellcreek = ds->priv;
1742 struct hellcreek_port *hellcreek_port;
1746 hellcreek_port = &hellcreek->ports[port];
1748 dev_dbg(hellcreek->dev, "Configure traffic schedule on port %d\n",
1751 /* First cancel delayed work */
1752 cancel_delayed_work_sync(&hellcreek_port->schedule_work);
1754 mutex_lock(&hellcreek->reg_lock);
1756 if (hellcreek_port->current_schedule) {
1757 taprio_offload_free(hellcreek_port->current_schedule);
1758 hellcreek_port->current_schedule = NULL;
1760 hellcreek_port->current_schedule = taprio_offload_get(taprio);
1762 /* Configure max sdu */
1763 hellcreek_setup_maxsdu(hellcreek, port, hellcreek_port->current_schedule);
1766 hellcreek_select_tgd(hellcreek, port);
1768 /* Enable gating and keep defaults */
1769 ctrl = (0xff << TR_TGDCTRL_ADMINGATESTATES_SHIFT) | TR_TGDCTRL_GATE_EN;
1770 hellcreek_write(hellcreek, ctrl, TR_TGDCTRL);
1772 /* Cancel pending schedule */
1773 hellcreek_write(hellcreek, 0x00, TR_ESTCMD);
1775 /* Setup a new schedule */
1776 hellcreek_setup_gcl(hellcreek, port, hellcreek_port->current_schedule);
1778 /* Configure cycle time */
1779 hellcreek_set_cycle_time(hellcreek, hellcreek_port->current_schedule);
1781 /* Check starting time */
1782 startable = hellcreek_schedule_startable(hellcreek, port);
1784 hellcreek_start_schedule(hellcreek, port);
1785 mutex_unlock(&hellcreek->reg_lock);
1789 mutex_unlock(&hellcreek->reg_lock);
1791 /* Schedule periodic schedule check */
1792 schedule_delayed_work(&hellcreek_port->schedule_work,
1793 HELLCREEK_SCHEDULE_PERIOD);
1798 static int hellcreek_port_del_schedule(struct dsa_switch *ds, int port)
1800 struct hellcreek *hellcreek = ds->priv;
1801 struct hellcreek_port *hellcreek_port;
1803 hellcreek_port = &hellcreek->ports[port];
1805 dev_dbg(hellcreek->dev, "Remove traffic schedule on port %d\n", port);
1807 /* First cancel delayed work */
1808 cancel_delayed_work_sync(&hellcreek_port->schedule_work);
1810 mutex_lock(&hellcreek->reg_lock);
1812 if (hellcreek_port->current_schedule) {
1813 taprio_offload_free(hellcreek_port->current_schedule);
1814 hellcreek_port->current_schedule = NULL;
1818 hellcreek_reset_maxsdu(hellcreek, port);
1821 hellcreek_select_tgd(hellcreek, port);
1823 /* Disable gating and return to regular switching flow */
1824 hellcreek_write(hellcreek, 0xff << TR_TGDCTRL_ADMINGATESTATES_SHIFT,
1827 mutex_unlock(&hellcreek->reg_lock);
1832 static bool hellcreek_validate_schedule(struct hellcreek *hellcreek,
1833 struct tc_taprio_qopt_offload *schedule)
1837 /* Does this hellcreek version support Qbv in hardware? */
1838 if (!hellcreek->pdata->qbv_support)
1841 /* cycle time can only be 32bit */
1842 if (schedule->cycle_time > (u32)-1)
1845 /* cycle time extension is not supported */
1846 if (schedule->cycle_time_extension)
1849 /* Only set command is supported */
1850 for (i = 0; i < schedule->num_entries; ++i)
1851 if (schedule->entries[i].command != TC_TAPRIO_CMD_SET_GATES)
1857 static int hellcreek_tc_query_caps(struct tc_query_caps_base *base)
1859 switch (base->type) {
1860 case TC_SETUP_QDISC_TAPRIO: {
1861 struct tc_taprio_caps *caps = base->caps;
1863 caps->supports_queue_max_sdu = true;
1872 static int hellcreek_port_setup_tc(struct dsa_switch *ds, int port,
1873 enum tc_setup_type type, void *type_data)
1875 struct hellcreek *hellcreek = ds->priv;
1879 return hellcreek_tc_query_caps(type_data);
1880 case TC_SETUP_QDISC_TAPRIO: {
1881 struct tc_taprio_qopt_offload *taprio = type_data;
1883 switch (taprio->cmd) {
1884 case TAPRIO_CMD_REPLACE:
1885 if (!hellcreek_validate_schedule(hellcreek, taprio))
1888 return hellcreek_port_set_schedule(ds, port, taprio);
1889 case TAPRIO_CMD_DESTROY:
1890 return hellcreek_port_del_schedule(ds, port);
1900 static const struct dsa_switch_ops hellcreek_ds_ops = {
1901 .devlink_info_get = hellcreek_devlink_info_get,
1902 .get_ethtool_stats = hellcreek_get_ethtool_stats,
1903 .get_sset_count = hellcreek_get_sset_count,
1904 .get_strings = hellcreek_get_strings,
1905 .get_tag_protocol = hellcreek_get_tag_protocol,
1906 .get_ts_info = hellcreek_get_ts_info,
1907 .phylink_get_caps = hellcreek_phylink_get_caps,
1908 .port_bridge_flags = hellcreek_bridge_flags,
1909 .port_bridge_join = hellcreek_port_bridge_join,
1910 .port_bridge_leave = hellcreek_port_bridge_leave,
1911 .port_disable = hellcreek_port_disable,
1912 .port_enable = hellcreek_port_enable,
1913 .port_fdb_add = hellcreek_fdb_add,
1914 .port_fdb_del = hellcreek_fdb_del,
1915 .port_fdb_dump = hellcreek_fdb_dump,
1916 .port_hwtstamp_set = hellcreek_port_hwtstamp_set,
1917 .port_hwtstamp_get = hellcreek_port_hwtstamp_get,
1918 .port_pre_bridge_flags = hellcreek_pre_bridge_flags,
1919 .port_prechangeupper = hellcreek_port_prechangeupper,
1920 .port_rxtstamp = hellcreek_port_rxtstamp,
1921 .port_setup_tc = hellcreek_port_setup_tc,
1922 .port_stp_state_set = hellcreek_port_stp_state_set,
1923 .port_txtstamp = hellcreek_port_txtstamp,
1924 .port_vlan_add = hellcreek_vlan_add,
1925 .port_vlan_del = hellcreek_vlan_del,
1926 .port_vlan_filtering = hellcreek_vlan_filtering,
1927 .setup = hellcreek_setup,
1928 .teardown = hellcreek_teardown,
1931 static int hellcreek_probe(struct platform_device *pdev)
1933 struct device *dev = &pdev->dev;
1934 struct hellcreek *hellcreek;
1935 struct resource *res;
1938 hellcreek = devm_kzalloc(dev, sizeof(*hellcreek), GFP_KERNEL);
1942 hellcreek->vidmbrcfg = devm_kcalloc(dev, VLAN_N_VID,
1943 sizeof(*hellcreek->vidmbrcfg),
1945 if (!hellcreek->vidmbrcfg)
1948 hellcreek->pdata = of_device_get_match_data(dev);
1950 hellcreek->ports = devm_kcalloc(dev, hellcreek->pdata->num_ports,
1951 sizeof(*hellcreek->ports),
1953 if (!hellcreek->ports)
1956 for (i = 0; i < hellcreek->pdata->num_ports; ++i) {
1957 struct hellcreek_port *port = &hellcreek->ports[i];
1959 port->counter_values =
1961 ARRAY_SIZE(hellcreek_counter),
1962 sizeof(*port->counter_values),
1964 if (!port->counter_values)
1967 port->vlan_dev_bitmap = devm_bitmap_zalloc(dev, VLAN_N_VID,
1969 if (!port->vlan_dev_bitmap)
1972 port->hellcreek = hellcreek;
1975 INIT_DELAYED_WORK(&port->schedule_work,
1976 hellcreek_check_schedule);
1979 mutex_init(&hellcreek->reg_lock);
1980 mutex_init(&hellcreek->vlan_lock);
1981 mutex_init(&hellcreek->ptp_lock);
1983 hellcreek->dev = dev;
1985 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "tsn");
1987 dev_err(dev, "No memory region provided!\n");
1991 hellcreek->base = devm_ioremap_resource(dev, res);
1992 if (IS_ERR(hellcreek->base))
1993 return PTR_ERR(hellcreek->base);
1995 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ptp");
1997 dev_err(dev, "No PTP memory region provided!\n");
2001 hellcreek->ptp_base = devm_ioremap_resource(dev, res);
2002 if (IS_ERR(hellcreek->ptp_base))
2003 return PTR_ERR(hellcreek->ptp_base);
2005 ret = hellcreek_detect(hellcreek);
2007 dev_err(dev, "No (known) chip found!\n");
2011 ret = hellcreek_wait_until_ready(hellcreek);
2013 dev_err(dev, "Switch didn't become ready!\n");
2017 hellcreek_feature_detect(hellcreek);
2019 hellcreek->ds = devm_kzalloc(dev, sizeof(*hellcreek->ds), GFP_KERNEL);
2023 hellcreek->ds->dev = dev;
2024 hellcreek->ds->priv = hellcreek;
2025 hellcreek->ds->ops = &hellcreek_ds_ops;
2026 hellcreek->ds->num_ports = hellcreek->pdata->num_ports;
2027 hellcreek->ds->num_tx_queues = HELLCREEK_NUM_EGRESS_QUEUES;
2029 ret = dsa_register_switch(hellcreek->ds);
2031 dev_err_probe(dev, ret, "Unable to register switch\n");
2035 ret = hellcreek_ptp_setup(hellcreek);
2037 dev_err(dev, "Failed to setup PTP!\n");
2041 ret = hellcreek_hwtstamp_setup(hellcreek);
2043 dev_err(dev, "Failed to setup hardware timestamping!\n");
2044 goto err_tstamp_setup;
2047 platform_set_drvdata(pdev, hellcreek);
2052 hellcreek_ptp_free(hellcreek);
2054 dsa_unregister_switch(hellcreek->ds);
2059 static void hellcreek_remove(struct platform_device *pdev)
2061 struct hellcreek *hellcreek = platform_get_drvdata(pdev);
2066 hellcreek_hwtstamp_free(hellcreek);
2067 hellcreek_ptp_free(hellcreek);
2068 dsa_unregister_switch(hellcreek->ds);
2071 static void hellcreek_shutdown(struct platform_device *pdev)
2073 struct hellcreek *hellcreek = platform_get_drvdata(pdev);
2078 dsa_switch_shutdown(hellcreek->ds);
2080 platform_set_drvdata(pdev, NULL);
2083 static const struct hellcreek_platform_data de1soc_r1_pdata = {
2088 .qbv_on_cpu_port = 1,
2090 .module_id = 0x4c30,
2093 static const struct of_device_id hellcreek_of_match[] = {
2095 .compatible = "hirschmann,hellcreek-de1soc-r1",
2096 .data = &de1soc_r1_pdata,
2100 MODULE_DEVICE_TABLE(of, hellcreek_of_match);
2102 static struct platform_driver hellcreek_driver = {
2103 .probe = hellcreek_probe,
2104 .remove = hellcreek_remove,
2105 .shutdown = hellcreek_shutdown,
2107 .name = "hellcreek",
2108 .of_match_table = hellcreek_of_match,
2111 module_platform_driver(hellcreek_driver);
2114 MODULE_DESCRIPTION("Hirschmann Hellcreek driver");
2115 MODULE_LICENSE("Dual MIT/GPL");