xref: /linux/drivers/net/dsa/hirschmann/hellcreek.c (revision b079922ba2acf072b23d82fa246a0d8de198f0a2)
1 // SPDX-License-Identifier: (GPL-2.0 or MIT)
2 /*
3  * DSA driver for:
4  * Hirschmann Hellcreek TSN switch.
5  *
6  * Copyright (C) 2019-2021 Linutronix GmbH
7  * Author Kurt Kanzenbach <kurt@linutronix.de>
8  */
9 
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/device.h>
13 #include <linux/of.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>
25 #include <net/dsa.h>
26 
27 #include "hellcreek.h"
28 #include "hellcreek_ptp.h"
29 #include "hellcreek_hwtstamp.h"
30 
31 static const struct hellcreek_counter hellcreek_counter[] = {
32 	{ 0x00, "RxFiltered", },
33 	{ 0x01, "RxOctets1k", },
34 	{ 0x02, "RxVTAG", },
35 	{ 0x03, "RxL2BAD", },
36 	{ 0x04, "RxOverloadDrop", },
37 	{ 0x05, "RxUC", },
38 	{ 0x06, "RxMC", },
39 	{ 0x07, "RxBC", },
40 	{ 0x08, "RxRS<64", },
41 	{ 0x09, "RxRS64", },
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", },
65 	{ 0x22, "TxVTAG", },
66 	{ 0x23, "TxL2BAD", },
67 	{ 0x25, "TxUC", },
68 	{ 0x26, "TxMC", },
69 	{ 0x27, "TxBC", },
70 	{ 0x28, "TxTS<64", },
71 	{ 0x29, "TxTS64", },
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", },
94 };
95 
96 static u16 hellcreek_read(struct hellcreek *hellcreek, unsigned int offset)
97 {
98 	return readw(hellcreek->base + offset);
99 }
100 
101 static u16 hellcreek_read_ctrl(struct hellcreek *hellcreek)
102 {
103 	return readw(hellcreek->base + HR_CTRL_C);
104 }
105 
106 static u16 hellcreek_read_stat(struct hellcreek *hellcreek)
107 {
108 	return readw(hellcreek->base + HR_SWSTAT);
109 }
110 
111 static void hellcreek_write(struct hellcreek *hellcreek, u16 data,
112 			    unsigned int offset)
113 {
114 	writew(data, hellcreek->base + offset);
115 }
116 
117 static void hellcreek_select_port(struct hellcreek *hellcreek, int port)
118 {
119 	u16 val = port << HR_PSEL_PTWSEL_SHIFT;
120 
121 	hellcreek_write(hellcreek, val, HR_PSEL);
122 }
123 
124 static void hellcreek_select_prio(struct hellcreek *hellcreek, int prio)
125 {
126 	u16 val = prio << HR_PSEL_PRTCWSEL_SHIFT;
127 
128 	hellcreek_write(hellcreek, val, HR_PSEL);
129 }
130 
131 static void hellcreek_select_counter(struct hellcreek *hellcreek, int counter)
132 {
133 	u16 val = counter << HR_CSEL_SHIFT;
134 
135 	hellcreek_write(hellcreek, val, HR_CSEL);
136 
137 	/* Data sheet states to wait at least 20 internal clock cycles */
138 	ndelay(200);
139 }
140 
141 static void hellcreek_select_vlan(struct hellcreek *hellcreek, int vid,
142 				  bool pvid)
143 {
144 	u16 val = 0;
145 
146 	/* Set pvid bit first */
147 	if (pvid)
148 		val |= HR_VIDCFG_PVID;
149 	hellcreek_write(hellcreek, val, HR_VIDCFG);
150 
151 	/* Set vlan */
152 	val |= vid << HR_VIDCFG_VID_SHIFT;
153 	hellcreek_write(hellcreek, val, HR_VIDCFG);
154 }
155 
156 static void hellcreek_select_tgd(struct hellcreek *hellcreek, int port)
157 {
158 	u16 val = port << TR_TGDSEL_TDGSEL_SHIFT;
159 
160 	hellcreek_write(hellcreek, val, TR_TGDSEL);
161 }
162 
163 static int hellcreek_wait_until_ready(struct hellcreek *hellcreek)
164 {
165 	u16 val;
166 
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,
170 				  3, 1000);
171 }
172 
173 static int hellcreek_wait_until_transitioned(struct hellcreek *hellcreek)
174 {
175 	u16 val;
176 
177 	return readx_poll_timeout_atomic(hellcreek_read_ctrl, hellcreek,
178 					 val, !(val & HR_CTRL_C_TRANSITION),
179 					 1, 1000);
180 }
181 
182 static int hellcreek_wait_fdb_ready(struct hellcreek *hellcreek)
183 {
184 	u16 val;
185 
186 	return readx_poll_timeout_atomic(hellcreek_read_stat, hellcreek,
187 					 val, !(val & HR_SWSTAT_BUSY),
188 					 1, 1000);
189 }
190 
191 static int hellcreek_detect(struct hellcreek *hellcreek)
192 {
193 	u16 id, rel_low, rel_high, date_low, date_high, tgd_ver;
194 	u8 tgd_maj, tgd_min;
195 	u32 rel, date;
196 
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);
203 
204 	if (id != hellcreek->pdata->module_id)
205 		return -ENODEV;
206 
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;
211 
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);
214 
215 	return 0;
216 }
217 
218 static void hellcreek_feature_detect(struct hellcreek *hellcreek)
219 {
220 	u16 features;
221 
222 	features = hellcreek_read(hellcreek, HR_FEABITS0);
223 
224 	/* Only detect the size of the FDB table. The size and current
225 	 * utilization can be queried via devlink.
226 	 */
227 	hellcreek->fdb_entries = ((features & HR_FEABITS0_FDBBINS_MASK) >>
228 			       HR_FEABITS0_FDBBINS_SHIFT) * 32;
229 }
230 
231 static enum dsa_tag_protocol hellcreek_get_tag_protocol(struct dsa_switch *ds,
232 							int port,
233 							enum dsa_tag_protocol mp)
234 {
235 	return DSA_TAG_PROTO_HELLCREEK;
236 }
237 
238 static int hellcreek_port_enable(struct dsa_switch *ds, int port,
239 				 struct phy_device *phy)
240 {
241 	struct hellcreek *hellcreek = ds->priv;
242 	struct hellcreek_port *hellcreek_port;
243 	u16 val;
244 
245 	hellcreek_port = &hellcreek->ports[port];
246 
247 	dev_dbg(hellcreek->dev, "Enable port %d\n", port);
248 
249 	mutex_lock(&hellcreek->reg_lock);
250 
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;
256 
257 	mutex_unlock(&hellcreek->reg_lock);
258 
259 	return 0;
260 }
261 
262 static void hellcreek_port_disable(struct dsa_switch *ds, int port)
263 {
264 	struct hellcreek *hellcreek = ds->priv;
265 	struct hellcreek_port *hellcreek_port;
266 	u16 val;
267 
268 	hellcreek_port = &hellcreek->ports[port];
269 
270 	dev_dbg(hellcreek->dev, "Disable port %d\n", port);
271 
272 	mutex_lock(&hellcreek->reg_lock);
273 
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;
279 
280 	mutex_unlock(&hellcreek->reg_lock);
281 }
282 
283 static void hellcreek_get_strings(struct dsa_switch *ds, int port,
284 				  u32 stringset, uint8_t *data)
285 {
286 	int i;
287 
288 	for (i = 0; i < ARRAY_SIZE(hellcreek_counter); ++i) {
289 		const struct hellcreek_counter *counter = &hellcreek_counter[i];
290 
291 		strlcpy(data + i * ETH_GSTRING_LEN,
292 			counter->name, ETH_GSTRING_LEN);
293 	}
294 }
295 
296 static int hellcreek_get_sset_count(struct dsa_switch *ds, int port, int sset)
297 {
298 	if (sset != ETH_SS_STATS)
299 		return 0;
300 
301 	return ARRAY_SIZE(hellcreek_counter);
302 }
303 
304 static void hellcreek_get_ethtool_stats(struct dsa_switch *ds, int port,
305 					uint64_t *data)
306 {
307 	struct hellcreek *hellcreek = ds->priv;
308 	struct hellcreek_port *hellcreek_port;
309 	int i;
310 
311 	hellcreek_port = &hellcreek->ports[port];
312 
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;
316 		u16 high, low;
317 		u64 value;
318 
319 		mutex_lock(&hellcreek->reg_lock);
320 
321 		hellcreek_select_counter(hellcreek, offset);
322 
323 		/* The registers are locked internally by selecting the
324 		 * counter. So low and high can be read without reading high
325 		 * again.
326 		 */
327 		high  = hellcreek_read(hellcreek, HR_CRDH);
328 		low   = hellcreek_read(hellcreek, HR_CRDL);
329 		value = ((u64)high << 16) | low;
330 
331 		hellcreek_port->counter_values[i] += value;
332 		data[i] = hellcreek_port->counter_values[i];
333 
334 		mutex_unlock(&hellcreek->reg_lock);
335 	}
336 }
337 
338 static u16 hellcreek_private_vid(int port)
339 {
340 	return VLAN_N_VID - port + 1;
341 }
342 
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)
346 {
347 	struct hellcreek *hellcreek = ds->priv;
348 	int i;
349 
350 	dev_dbg(hellcreek->dev, "VLAN prepare for port %d\n", port);
351 
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.
355 	 */
356 	for (i = 0; i < hellcreek->pdata->num_ports; ++i) {
357 		const u16 restricted_vid = hellcreek_private_vid(i);
358 
359 		if (!dsa_is_user_port(ds, i))
360 			continue;
361 
362 		if (vlan->vid == restricted_vid) {
363 			NL_SET_ERR_MSG_MOD(extack, "VID restricted by driver");
364 			return -EBUSY;
365 		}
366 	}
367 
368 	return 0;
369 }
370 
371 static void hellcreek_select_vlan_params(struct hellcreek *hellcreek, int port,
372 					 int *shift, int *mask)
373 {
374 	switch (port) {
375 	case 0:
376 		*shift = HR_VIDMBRCFG_P0MBR_SHIFT;
377 		*mask  = HR_VIDMBRCFG_P0MBR_MASK;
378 		break;
379 	case 1:
380 		*shift = HR_VIDMBRCFG_P1MBR_SHIFT;
381 		*mask  = HR_VIDMBRCFG_P1MBR_MASK;
382 		break;
383 	case 2:
384 		*shift = HR_VIDMBRCFG_P2MBR_SHIFT;
385 		*mask  = HR_VIDMBRCFG_P2MBR_MASK;
386 		break;
387 	case 3:
388 		*shift = HR_VIDMBRCFG_P3MBR_SHIFT;
389 		*mask  = HR_VIDMBRCFG_P3MBR_MASK;
390 		break;
391 	default:
392 		*shift = *mask = 0;
393 		dev_err(hellcreek->dev, "Unknown port %d selected!\n", port);
394 	}
395 }
396 
397 static void hellcreek_apply_vlan(struct hellcreek *hellcreek, int port, u16 vid,
398 				 bool pvid, bool untagged)
399 {
400 	int shift, mask;
401 	u16 val;
402 
403 	dev_dbg(hellcreek->dev, "Apply VLAN: port=%d vid=%u pvid=%d untagged=%d",
404 		port, vid, pvid, untagged);
405 
406 	mutex_lock(&hellcreek->reg_lock);
407 
408 	hellcreek_select_port(hellcreek, port);
409 	hellcreek_select_vlan(hellcreek, vid, pvid);
410 
411 	/* Setup port vlan membership */
412 	hellcreek_select_vlan_params(hellcreek, port, &shift, &mask);
413 	val = hellcreek->vidmbrcfg[vid];
414 	val &= ~mask;
415 	if (untagged)
416 		val |= HELLCREEK_VLAN_UNTAGGED_MEMBER << shift;
417 	else
418 		val |= HELLCREEK_VLAN_TAGGED_MEMBER << shift;
419 
420 	hellcreek_write(hellcreek, val, HR_VIDMBRCFG);
421 	hellcreek->vidmbrcfg[vid] = val;
422 
423 	mutex_unlock(&hellcreek->reg_lock);
424 }
425 
426 static void hellcreek_unapply_vlan(struct hellcreek *hellcreek, int port,
427 				   u16 vid)
428 {
429 	int shift, mask;
430 	u16 val;
431 
432 	dev_dbg(hellcreek->dev, "Unapply VLAN: port=%d vid=%u\n", port, vid);
433 
434 	mutex_lock(&hellcreek->reg_lock);
435 
436 	hellcreek_select_vlan(hellcreek, vid, false);
437 
438 	/* Setup port vlan membership */
439 	hellcreek_select_vlan_params(hellcreek, port, &shift, &mask);
440 	val = hellcreek->vidmbrcfg[vid];
441 	val &= ~mask;
442 	val |= HELLCREEK_VLAN_NO_MEMBER << shift;
443 
444 	hellcreek_write(hellcreek, val, HR_VIDMBRCFG);
445 	hellcreek->vidmbrcfg[vid] = val;
446 
447 	mutex_unlock(&hellcreek->reg_lock);
448 }
449 
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)
453 {
454 	bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
455 	bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
456 	struct hellcreek *hellcreek = ds->priv;
457 	int err;
458 
459 	err = hellcreek_vlan_prepare(ds, port, vlan, extack);
460 	if (err)
461 		return err;
462 
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");
466 
467 	hellcreek_apply_vlan(hellcreek, port, vlan->vid, pvid, untagged);
468 
469 	return 0;
470 }
471 
472 static int hellcreek_vlan_del(struct dsa_switch *ds, int port,
473 			      const struct switchdev_obj_port_vlan *vlan)
474 {
475 	struct hellcreek *hellcreek = ds->priv;
476 
477 	dev_dbg(hellcreek->dev, "Remove VLAN %d on port %d\n", vlan->vid, port);
478 
479 	hellcreek_unapply_vlan(hellcreek, port, vlan->vid);
480 
481 	return 0;
482 }
483 
484 static void hellcreek_port_stp_state_set(struct dsa_switch *ds, int port,
485 					 u8 state)
486 {
487 	struct hellcreek *hellcreek = ds->priv;
488 	struct hellcreek_port *hellcreek_port;
489 	const char *new_state;
490 	u16 val;
491 
492 	mutex_lock(&hellcreek->reg_lock);
493 
494 	hellcreek_port = &hellcreek->ports[port];
495 	val = hellcreek_port->ptcfg;
496 
497 	switch (state) {
498 	case BR_STATE_DISABLED:
499 		new_state = "DISABLED";
500 		val |= HR_PTCFG_BLOCKED;
501 		val &= ~HR_PTCFG_LEARNING_EN;
502 		break;
503 	case BR_STATE_BLOCKING:
504 		new_state = "BLOCKING";
505 		val |= HR_PTCFG_BLOCKED;
506 		val &= ~HR_PTCFG_LEARNING_EN;
507 		break;
508 	case BR_STATE_LISTENING:
509 		new_state = "LISTENING";
510 		val |= HR_PTCFG_BLOCKED;
511 		val &= ~HR_PTCFG_LEARNING_EN;
512 		break;
513 	case BR_STATE_LEARNING:
514 		new_state = "LEARNING";
515 		val |= HR_PTCFG_BLOCKED;
516 		val |= HR_PTCFG_LEARNING_EN;
517 		break;
518 	case BR_STATE_FORWARDING:
519 		new_state = "FORWARDING";
520 		val &= ~HR_PTCFG_BLOCKED;
521 		val |= HR_PTCFG_LEARNING_EN;
522 		break;
523 	default:
524 		new_state = "UNKNOWN";
525 	}
526 
527 	hellcreek_select_port(hellcreek, port);
528 	hellcreek_write(hellcreek, val, HR_PTCFG);
529 	hellcreek_port->ptcfg = val;
530 
531 	mutex_unlock(&hellcreek->reg_lock);
532 
533 	dev_dbg(hellcreek->dev, "Configured STP state for port %d: %s\n",
534 		port, new_state);
535 }
536 
537 static void hellcreek_setup_ingressflt(struct hellcreek *hellcreek, int port,
538 				       bool enable)
539 {
540 	struct hellcreek_port *hellcreek_port = &hellcreek->ports[port];
541 	u16 ptcfg;
542 
543 	mutex_lock(&hellcreek->reg_lock);
544 
545 	ptcfg = hellcreek_port->ptcfg;
546 
547 	if (enable)
548 		ptcfg |= HR_PTCFG_INGRESSFLT;
549 	else
550 		ptcfg &= ~HR_PTCFG_INGRESSFLT;
551 
552 	hellcreek_select_port(hellcreek, port);
553 	hellcreek_write(hellcreek, ptcfg, HR_PTCFG);
554 	hellcreek_port->ptcfg = ptcfg;
555 
556 	mutex_unlock(&hellcreek->reg_lock);
557 }
558 
559 static void hellcreek_setup_vlan_awareness(struct hellcreek *hellcreek,
560 					   bool enable)
561 {
562 	u16 swcfg;
563 
564 	mutex_lock(&hellcreek->reg_lock);
565 
566 	swcfg = hellcreek->swcfg;
567 
568 	if (enable)
569 		swcfg |= HR_SWCFG_VLAN_UNAWARE;
570 	else
571 		swcfg &= ~HR_SWCFG_VLAN_UNAWARE;
572 
573 	hellcreek_write(hellcreek, swcfg, HR_SWCFG);
574 
575 	mutex_unlock(&hellcreek->reg_lock);
576 }
577 
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,
580 					    bool enabled)
581 {
582 	const u16 vid = hellcreek_private_vid(port);
583 	int upstream = dsa_upstream_port(ds, port);
584 	struct hellcreek *hellcreek = ds->priv;
585 
586 	/* Apply vid to port as egress untagged and port vlan id */
587 	if (enabled)
588 		hellcreek_apply_vlan(hellcreek, port, vid, true, true);
589 	else
590 		hellcreek_unapply_vlan(hellcreek, port, vid);
591 
592 	/* Apply vid to cpu port as well */
593 	if (enabled)
594 		hellcreek_apply_vlan(hellcreek, upstream, vid, false, true);
595 	else
596 		hellcreek_unapply_vlan(hellcreek, upstream, vid);
597 }
598 
599 static void hellcreek_port_set_ucast_flood(struct hellcreek *hellcreek,
600 					   int port, bool enable)
601 {
602 	struct hellcreek_port *hellcreek_port;
603 	u16 val;
604 
605 	hellcreek_port = &hellcreek->ports[port];
606 
607 	dev_dbg(hellcreek->dev, "%s unicast flooding on port %d\n",
608 		enable ? "Enable" : "Disable", port);
609 
610 	mutex_lock(&hellcreek->reg_lock);
611 
612 	hellcreek_select_port(hellcreek, port);
613 	val = hellcreek_port->ptcfg;
614 	if (enable)
615 		val &= ~HR_PTCFG_UUC_FLT;
616 	else
617 		val |= HR_PTCFG_UUC_FLT;
618 	hellcreek_write(hellcreek, val, HR_PTCFG);
619 	hellcreek_port->ptcfg = val;
620 
621 	mutex_unlock(&hellcreek->reg_lock);
622 }
623 
624 static void hellcreek_port_set_mcast_flood(struct hellcreek *hellcreek,
625 					   int port, bool enable)
626 {
627 	struct hellcreek_port *hellcreek_port;
628 	u16 val;
629 
630 	hellcreek_port = &hellcreek->ports[port];
631 
632 	dev_dbg(hellcreek->dev, "%s multicast flooding on port %d\n",
633 		enable ? "Enable" : "Disable", port);
634 
635 	mutex_lock(&hellcreek->reg_lock);
636 
637 	hellcreek_select_port(hellcreek, port);
638 	val = hellcreek_port->ptcfg;
639 	if (enable)
640 		val &= ~HR_PTCFG_UMC_FLT;
641 	else
642 		val |= HR_PTCFG_UMC_FLT;
643 	hellcreek_write(hellcreek, val, HR_PTCFG);
644 	hellcreek_port->ptcfg = val;
645 
646 	mutex_unlock(&hellcreek->reg_lock);
647 }
648 
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)
652 {
653 	if (flags.mask & ~(BR_FLOOD | BR_MCAST_FLOOD))
654 		return -EINVAL;
655 
656 	return 0;
657 }
658 
659 static int hellcreek_bridge_flags(struct dsa_switch *ds, int port,
660 				  struct switchdev_brport_flags flags,
661 				  struct netlink_ext_ack *extack)
662 {
663 	struct hellcreek *hellcreek = ds->priv;
664 
665 	if (flags.mask & BR_FLOOD)
666 		hellcreek_port_set_ucast_flood(hellcreek, port,
667 					       !!(flags.val & BR_FLOOD));
668 
669 	if (flags.mask & BR_MCAST_FLOOD)
670 		hellcreek_port_set_mcast_flood(hellcreek, port,
671 					       !!(flags.val & BR_MCAST_FLOOD));
672 
673 	return 0;
674 }
675 
676 static int hellcreek_port_bridge_join(struct dsa_switch *ds, int port,
677 				      struct dsa_bridge bridge,
678 				      bool *tx_fwd_offload)
679 {
680 	struct hellcreek *hellcreek = ds->priv;
681 
682 	dev_dbg(hellcreek->dev, "Port %d joins a bridge\n", port);
683 
684 	/* When joining a vlan_filtering bridge, keep the switch VLAN aware */
685 	if (!ds->vlan_filtering)
686 		hellcreek_setup_vlan_awareness(hellcreek, false);
687 
688 	/* Drop private vlans */
689 	hellcreek_setup_vlan_membership(ds, port, false);
690 
691 	return 0;
692 }
693 
694 static void hellcreek_port_bridge_leave(struct dsa_switch *ds, int port,
695 					struct dsa_bridge bridge)
696 {
697 	struct hellcreek *hellcreek = ds->priv;
698 
699 	dev_dbg(hellcreek->dev, "Port %d leaves a bridge\n", port);
700 
701 	/* Enable VLAN awareness */
702 	hellcreek_setup_vlan_awareness(hellcreek, true);
703 
704 	/* Enable private vlans */
705 	hellcreek_setup_vlan_membership(ds, port, true);
706 }
707 
708 static int __hellcreek_fdb_add(struct hellcreek *hellcreek,
709 			       const struct hellcreek_fdb_entry *entry)
710 {
711 	u16 meta = 0;
712 
713 	dev_dbg(hellcreek->dev, "Add static FDB entry: MAC=%pM, MASK=0x%02x, "
714 		"OBT=%d, REPRIO_EN=%d, PRIO=%d\n", entry->mac, entry->portmask,
715 		entry->is_obt, entry->reprio_en, entry->reprio_tc);
716 
717 	/* Add mac address */
718 	hellcreek_write(hellcreek, entry->mac[1] | (entry->mac[0] << 8), HR_FDBWDH);
719 	hellcreek_write(hellcreek, entry->mac[3] | (entry->mac[2] << 8), HR_FDBWDM);
720 	hellcreek_write(hellcreek, entry->mac[5] | (entry->mac[4] << 8), HR_FDBWDL);
721 
722 	/* Meta data */
723 	meta |= entry->portmask << HR_FDBWRM0_PORTMASK_SHIFT;
724 	if (entry->is_obt)
725 		meta |= HR_FDBWRM0_OBT;
726 	if (entry->reprio_en) {
727 		meta |= HR_FDBWRM0_REPRIO_EN;
728 		meta |= entry->reprio_tc << HR_FDBWRM0_REPRIO_TC_SHIFT;
729 	}
730 	hellcreek_write(hellcreek, meta, HR_FDBWRM0);
731 
732 	/* Commit */
733 	hellcreek_write(hellcreek, 0x00, HR_FDBWRCMD);
734 
735 	/* Wait until done */
736 	return hellcreek_wait_fdb_ready(hellcreek);
737 }
738 
739 static int __hellcreek_fdb_del(struct hellcreek *hellcreek,
740 			       const struct hellcreek_fdb_entry *entry)
741 {
742 	dev_dbg(hellcreek->dev, "Delete FDB entry: MAC=%pM!\n", entry->mac);
743 
744 	/* Delete by matching idx */
745 	hellcreek_write(hellcreek, entry->idx | HR_FDBWRCMD_FDBDEL, HR_FDBWRCMD);
746 
747 	/* Wait until done */
748 	return hellcreek_wait_fdb_ready(hellcreek);
749 }
750 
751 static void hellcreek_populate_fdb_entry(struct hellcreek *hellcreek,
752 					 struct hellcreek_fdb_entry *entry,
753 					 size_t idx)
754 {
755 	unsigned char addr[ETH_ALEN];
756 	u16 meta, mac;
757 
758 	/* Read values */
759 	meta	= hellcreek_read(hellcreek, HR_FDBMDRD);
760 	mac	= hellcreek_read(hellcreek, HR_FDBRDL);
761 	addr[5] = mac & 0xff;
762 	addr[4] = (mac & 0xff00) >> 8;
763 	mac	= hellcreek_read(hellcreek, HR_FDBRDM);
764 	addr[3] = mac & 0xff;
765 	addr[2] = (mac & 0xff00) >> 8;
766 	mac	= hellcreek_read(hellcreek, HR_FDBRDH);
767 	addr[1] = mac & 0xff;
768 	addr[0] = (mac & 0xff00) >> 8;
769 
770 	/* Populate @entry */
771 	memcpy(entry->mac, addr, sizeof(addr));
772 	entry->idx	    = idx;
773 	entry->portmask	    = (meta & HR_FDBMDRD_PORTMASK_MASK) >>
774 		HR_FDBMDRD_PORTMASK_SHIFT;
775 	entry->age	    = (meta & HR_FDBMDRD_AGE_MASK) >>
776 		HR_FDBMDRD_AGE_SHIFT;
777 	entry->is_obt	    = !!(meta & HR_FDBMDRD_OBT);
778 	entry->pass_blocked = !!(meta & HR_FDBMDRD_PASS_BLOCKED);
779 	entry->is_static    = !!(meta & HR_FDBMDRD_STATIC);
780 	entry->reprio_tc    = (meta & HR_FDBMDRD_REPRIO_TC_MASK) >>
781 		HR_FDBMDRD_REPRIO_TC_SHIFT;
782 	entry->reprio_en    = !!(meta & HR_FDBMDRD_REPRIO_EN);
783 }
784 
785 /* Retrieve the index of a FDB entry by mac address. Currently we search through
786  * the complete table in hardware. If that's too slow, we might have to cache
787  * the complete FDB table in software.
788  */
789 static int hellcreek_fdb_get(struct hellcreek *hellcreek,
790 			     const unsigned char *dest,
791 			     struct hellcreek_fdb_entry *entry)
792 {
793 	size_t i;
794 
795 	/* Set read pointer to zero: The read of HR_FDBMAX (read-only register)
796 	 * should reset the internal pointer. But, that doesn't work. The vendor
797 	 * suggested a subsequent write as workaround. Same for HR_FDBRDH below.
798 	 */
799 	hellcreek_read(hellcreek, HR_FDBMAX);
800 	hellcreek_write(hellcreek, 0x00, HR_FDBMAX);
801 
802 	/* We have to read the complete table, because the switch/driver might
803 	 * enter new entries anywhere.
804 	 */
805 	for (i = 0; i < hellcreek->fdb_entries; ++i) {
806 		struct hellcreek_fdb_entry tmp = { 0 };
807 
808 		/* Read entry */
809 		hellcreek_populate_fdb_entry(hellcreek, &tmp, i);
810 
811 		/* Force next entry */
812 		hellcreek_write(hellcreek, 0x00, HR_FDBRDH);
813 
814 		if (memcmp(tmp.mac, dest, ETH_ALEN))
815 			continue;
816 
817 		/* Match found */
818 		memcpy(entry, &tmp, sizeof(*entry));
819 
820 		return 0;
821 	}
822 
823 	return -ENOENT;
824 }
825 
826 static int hellcreek_fdb_add(struct dsa_switch *ds, int port,
827 			     const unsigned char *addr, u16 vid)
828 {
829 	struct hellcreek_fdb_entry entry = { 0 };
830 	struct hellcreek *hellcreek = ds->priv;
831 	int ret;
832 
833 	dev_dbg(hellcreek->dev, "Add FDB entry for MAC=%pM\n", addr);
834 
835 	mutex_lock(&hellcreek->reg_lock);
836 
837 	ret = hellcreek_fdb_get(hellcreek, addr, &entry);
838 	if (ret) {
839 		/* Not found */
840 		memcpy(entry.mac, addr, sizeof(entry.mac));
841 		entry.portmask = BIT(port);
842 
843 		ret = __hellcreek_fdb_add(hellcreek, &entry);
844 		if (ret) {
845 			dev_err(hellcreek->dev, "Failed to add FDB entry!\n");
846 			goto out;
847 		}
848 	} else {
849 		/* Found */
850 		ret = __hellcreek_fdb_del(hellcreek, &entry);
851 		if (ret) {
852 			dev_err(hellcreek->dev, "Failed to delete FDB entry!\n");
853 			goto out;
854 		}
855 
856 		entry.portmask |= BIT(port);
857 
858 		ret = __hellcreek_fdb_add(hellcreek, &entry);
859 		if (ret) {
860 			dev_err(hellcreek->dev, "Failed to add FDB entry!\n");
861 			goto out;
862 		}
863 	}
864 
865 out:
866 	mutex_unlock(&hellcreek->reg_lock);
867 
868 	return ret;
869 }
870 
871 static int hellcreek_fdb_del(struct dsa_switch *ds, int port,
872 			     const unsigned char *addr, u16 vid)
873 {
874 	struct hellcreek_fdb_entry entry = { 0 };
875 	struct hellcreek *hellcreek = ds->priv;
876 	int ret;
877 
878 	dev_dbg(hellcreek->dev, "Delete FDB entry for MAC=%pM\n", addr);
879 
880 	mutex_lock(&hellcreek->reg_lock);
881 
882 	ret = hellcreek_fdb_get(hellcreek, addr, &entry);
883 	if (ret) {
884 		/* Not found */
885 		dev_err(hellcreek->dev, "FDB entry for deletion not found!\n");
886 	} else {
887 		/* Found */
888 		ret = __hellcreek_fdb_del(hellcreek, &entry);
889 		if (ret) {
890 			dev_err(hellcreek->dev, "Failed to delete FDB entry!\n");
891 			goto out;
892 		}
893 
894 		entry.portmask &= ~BIT(port);
895 
896 		if (entry.portmask != 0x00) {
897 			ret = __hellcreek_fdb_add(hellcreek, &entry);
898 			if (ret) {
899 				dev_err(hellcreek->dev, "Failed to add FDB entry!\n");
900 				goto out;
901 			}
902 		}
903 	}
904 
905 out:
906 	mutex_unlock(&hellcreek->reg_lock);
907 
908 	return ret;
909 }
910 
911 static int hellcreek_fdb_dump(struct dsa_switch *ds, int port,
912 			      dsa_fdb_dump_cb_t *cb, void *data)
913 {
914 	struct hellcreek *hellcreek = ds->priv;
915 	u16 entries;
916 	int ret = 0;
917 	size_t i;
918 
919 	mutex_lock(&hellcreek->reg_lock);
920 
921 	/* Set read pointer to zero: The read of HR_FDBMAX (read-only register)
922 	 * should reset the internal pointer. But, that doesn't work. The vendor
923 	 * suggested a subsequent write as workaround. Same for HR_FDBRDH below.
924 	 */
925 	entries = hellcreek_read(hellcreek, HR_FDBMAX);
926 	hellcreek_write(hellcreek, 0x00, HR_FDBMAX);
927 
928 	dev_dbg(hellcreek->dev, "FDB dump for port %d, entries=%d!\n", port, entries);
929 
930 	/* Read table */
931 	for (i = 0; i < hellcreek->fdb_entries; ++i) {
932 		struct hellcreek_fdb_entry entry = { 0 };
933 
934 		/* Read entry */
935 		hellcreek_populate_fdb_entry(hellcreek, &entry, i);
936 
937 		/* Force next entry */
938 		hellcreek_write(hellcreek, 0x00, HR_FDBRDH);
939 
940 		/* Check valid */
941 		if (is_zero_ether_addr(entry.mac))
942 			continue;
943 
944 		/* Check port mask */
945 		if (!(entry.portmask & BIT(port)))
946 			continue;
947 
948 		ret = cb(entry.mac, 0, entry.is_static, data);
949 		if (ret)
950 			break;
951 	}
952 
953 	mutex_unlock(&hellcreek->reg_lock);
954 
955 	return ret;
956 }
957 
958 static int hellcreek_vlan_filtering(struct dsa_switch *ds, int port,
959 				    bool vlan_filtering,
960 				    struct netlink_ext_ack *extack)
961 {
962 	struct hellcreek *hellcreek = ds->priv;
963 
964 	dev_dbg(hellcreek->dev, "%s VLAN filtering on port %d\n",
965 		vlan_filtering ? "Enable" : "Disable", port);
966 
967 	/* Configure port to drop packages with not known vids */
968 	hellcreek_setup_ingressflt(hellcreek, port, vlan_filtering);
969 
970 	/* Enable VLAN awareness on the switch. This save due to
971 	 * ds->vlan_filtering_is_global.
972 	 */
973 	hellcreek_setup_vlan_awareness(hellcreek, vlan_filtering);
974 
975 	return 0;
976 }
977 
978 static int hellcreek_enable_ip_core(struct hellcreek *hellcreek)
979 {
980 	int ret;
981 	u16 val;
982 
983 	mutex_lock(&hellcreek->reg_lock);
984 
985 	val = hellcreek_read(hellcreek, HR_CTRL_C);
986 	val |= HR_CTRL_C_ENABLE;
987 	hellcreek_write(hellcreek, val, HR_CTRL_C);
988 	ret = hellcreek_wait_until_transitioned(hellcreek);
989 
990 	mutex_unlock(&hellcreek->reg_lock);
991 
992 	return ret;
993 }
994 
995 static void hellcreek_setup_cpu_and_tunnel_port(struct hellcreek *hellcreek)
996 {
997 	struct hellcreek_port *tunnel_port = &hellcreek->ports[TUNNEL_PORT];
998 	struct hellcreek_port *cpu_port = &hellcreek->ports[CPU_PORT];
999 	u16 ptcfg = 0;
1000 
1001 	ptcfg |= HR_PTCFG_LEARNING_EN | HR_PTCFG_ADMIN_EN;
1002 
1003 	mutex_lock(&hellcreek->reg_lock);
1004 
1005 	hellcreek_select_port(hellcreek, CPU_PORT);
1006 	hellcreek_write(hellcreek, ptcfg, HR_PTCFG);
1007 
1008 	hellcreek_select_port(hellcreek, TUNNEL_PORT);
1009 	hellcreek_write(hellcreek, ptcfg, HR_PTCFG);
1010 
1011 	cpu_port->ptcfg	   = ptcfg;
1012 	tunnel_port->ptcfg = ptcfg;
1013 
1014 	mutex_unlock(&hellcreek->reg_lock);
1015 }
1016 
1017 static void hellcreek_setup_tc_identity_mapping(struct hellcreek *hellcreek)
1018 {
1019 	int i;
1020 
1021 	/* The switch has multiple egress queues per port. The queue is selected
1022 	 * via the PCP field in the VLAN header. The switch internally deals
1023 	 * with traffic classes instead of PCP values and this mapping is
1024 	 * configurable.
1025 	 *
1026 	 * The default mapping is (PCP - TC):
1027 	 *  7 - 7
1028 	 *  6 - 6
1029 	 *  5 - 5
1030 	 *  4 - 4
1031 	 *  3 - 3
1032 	 *  2 - 1
1033 	 *  1 - 0
1034 	 *  0 - 2
1035 	 *
1036 	 * The default should be an identity mapping.
1037 	 */
1038 
1039 	for (i = 0; i < 8; ++i) {
1040 		mutex_lock(&hellcreek->reg_lock);
1041 
1042 		hellcreek_select_prio(hellcreek, i);
1043 		hellcreek_write(hellcreek,
1044 				i << HR_PRTCCFG_PCP_TC_MAP_SHIFT,
1045 				HR_PRTCCFG);
1046 
1047 		mutex_unlock(&hellcreek->reg_lock);
1048 	}
1049 }
1050 
1051 static int hellcreek_setup_fdb(struct hellcreek *hellcreek)
1052 {
1053 	static struct hellcreek_fdb_entry ptp = {
1054 		/* MAC: 01-1B-19-00-00-00 */
1055 		.mac	      = { 0x01, 0x1b, 0x19, 0x00, 0x00, 0x00 },
1056 		.portmask     = 0x03,	/* Management ports */
1057 		.age	      = 0,
1058 		.is_obt	      = 0,
1059 		.pass_blocked = 0,
1060 		.is_static    = 1,
1061 		.reprio_tc    = 6,	/* TC: 6 as per IEEE 802.1AS */
1062 		.reprio_en    = 1,
1063 	};
1064 	static struct hellcreek_fdb_entry p2p = {
1065 		/* MAC: 01-80-C2-00-00-0E */
1066 		.mac	      = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x0e },
1067 		.portmask     = 0x03,	/* Management ports */
1068 		.age	      = 0,
1069 		.is_obt	      = 0,
1070 		.pass_blocked = 0,
1071 		.is_static    = 1,
1072 		.reprio_tc    = 6,	/* TC: 6 as per IEEE 802.1AS */
1073 		.reprio_en    = 1,
1074 	};
1075 	int ret;
1076 
1077 	mutex_lock(&hellcreek->reg_lock);
1078 	ret = __hellcreek_fdb_add(hellcreek, &ptp);
1079 	if (ret)
1080 		goto out;
1081 	ret = __hellcreek_fdb_add(hellcreek, &p2p);
1082 out:
1083 	mutex_unlock(&hellcreek->reg_lock);
1084 
1085 	return ret;
1086 }
1087 
1088 static int hellcreek_devlink_info_get(struct dsa_switch *ds,
1089 				      struct devlink_info_req *req,
1090 				      struct netlink_ext_ack *extack)
1091 {
1092 	struct hellcreek *hellcreek = ds->priv;
1093 	int ret;
1094 
1095 	ret = devlink_info_driver_name_put(req, "hellcreek");
1096 	if (ret)
1097 		return ret;
1098 
1099 	return devlink_info_version_fixed_put(req,
1100 					      DEVLINK_INFO_VERSION_GENERIC_ASIC_ID,
1101 					      hellcreek->pdata->name);
1102 }
1103 
1104 static u64 hellcreek_devlink_vlan_table_get(void *priv)
1105 {
1106 	struct hellcreek *hellcreek = priv;
1107 	u64 count = 0;
1108 	int i;
1109 
1110 	mutex_lock(&hellcreek->reg_lock);
1111 	for (i = 0; i < VLAN_N_VID; ++i)
1112 		if (hellcreek->vidmbrcfg[i])
1113 			count++;
1114 	mutex_unlock(&hellcreek->reg_lock);
1115 
1116 	return count;
1117 }
1118 
1119 static u64 hellcreek_devlink_fdb_table_get(void *priv)
1120 {
1121 	struct hellcreek *hellcreek = priv;
1122 	u64 count = 0;
1123 
1124 	/* Reading this register has side effects. Synchronize against the other
1125 	 * FDB operations.
1126 	 */
1127 	mutex_lock(&hellcreek->reg_lock);
1128 	count = hellcreek_read(hellcreek, HR_FDBMAX);
1129 	mutex_unlock(&hellcreek->reg_lock);
1130 
1131 	return count;
1132 }
1133 
1134 static int hellcreek_setup_devlink_resources(struct dsa_switch *ds)
1135 {
1136 	struct devlink_resource_size_params size_vlan_params;
1137 	struct devlink_resource_size_params size_fdb_params;
1138 	struct hellcreek *hellcreek = ds->priv;
1139 	int err;
1140 
1141 	devlink_resource_size_params_init(&size_vlan_params, VLAN_N_VID,
1142 					  VLAN_N_VID,
1143 					  1, DEVLINK_RESOURCE_UNIT_ENTRY);
1144 
1145 	devlink_resource_size_params_init(&size_fdb_params,
1146 					  hellcreek->fdb_entries,
1147 					  hellcreek->fdb_entries,
1148 					  1, DEVLINK_RESOURCE_UNIT_ENTRY);
1149 
1150 	err = dsa_devlink_resource_register(ds, "VLAN", VLAN_N_VID,
1151 					    HELLCREEK_DEVLINK_PARAM_ID_VLAN_TABLE,
1152 					    DEVLINK_RESOURCE_ID_PARENT_TOP,
1153 					    &size_vlan_params);
1154 	if (err)
1155 		goto out;
1156 
1157 	err = dsa_devlink_resource_register(ds, "FDB", hellcreek->fdb_entries,
1158 					    HELLCREEK_DEVLINK_PARAM_ID_FDB_TABLE,
1159 					    DEVLINK_RESOURCE_ID_PARENT_TOP,
1160 					    &size_fdb_params);
1161 	if (err)
1162 		goto out;
1163 
1164 	dsa_devlink_resource_occ_get_register(ds,
1165 					      HELLCREEK_DEVLINK_PARAM_ID_VLAN_TABLE,
1166 					      hellcreek_devlink_vlan_table_get,
1167 					      hellcreek);
1168 
1169 	dsa_devlink_resource_occ_get_register(ds,
1170 					      HELLCREEK_DEVLINK_PARAM_ID_FDB_TABLE,
1171 					      hellcreek_devlink_fdb_table_get,
1172 					      hellcreek);
1173 
1174 	return 0;
1175 
1176 out:
1177 	dsa_devlink_resources_unregister(ds);
1178 
1179 	return err;
1180 }
1181 
1182 static int hellcreek_devlink_region_vlan_snapshot(struct devlink *dl,
1183 						  const struct devlink_region_ops *ops,
1184 						  struct netlink_ext_ack *extack,
1185 						  u8 **data)
1186 {
1187 	struct hellcreek_devlink_vlan_entry *table, *entry;
1188 	struct dsa_switch *ds = dsa_devlink_to_ds(dl);
1189 	struct hellcreek *hellcreek = ds->priv;
1190 	int i;
1191 
1192 	table = kcalloc(VLAN_N_VID, sizeof(*entry), GFP_KERNEL);
1193 	if (!table)
1194 		return -ENOMEM;
1195 
1196 	entry = table;
1197 
1198 	mutex_lock(&hellcreek->reg_lock);
1199 	for (i = 0; i < VLAN_N_VID; ++i, ++entry) {
1200 		entry->member = hellcreek->vidmbrcfg[i];
1201 		entry->vid    = i;
1202 	}
1203 	mutex_unlock(&hellcreek->reg_lock);
1204 
1205 	*data = (u8 *)table;
1206 
1207 	return 0;
1208 }
1209 
1210 static int hellcreek_devlink_region_fdb_snapshot(struct devlink *dl,
1211 						 const struct devlink_region_ops *ops,
1212 						 struct netlink_ext_ack *extack,
1213 						 u8 **data)
1214 {
1215 	struct dsa_switch *ds = dsa_devlink_to_ds(dl);
1216 	struct hellcreek_fdb_entry *table, *entry;
1217 	struct hellcreek *hellcreek = ds->priv;
1218 	size_t i;
1219 
1220 	table = kcalloc(hellcreek->fdb_entries, sizeof(*entry), GFP_KERNEL);
1221 	if (!table)
1222 		return -ENOMEM;
1223 
1224 	entry = table;
1225 
1226 	mutex_lock(&hellcreek->reg_lock);
1227 
1228 	/* Start table read */
1229 	hellcreek_read(hellcreek, HR_FDBMAX);
1230 	hellcreek_write(hellcreek, 0x00, HR_FDBMAX);
1231 
1232 	for (i = 0; i < hellcreek->fdb_entries; ++i, ++entry) {
1233 		/* Read current entry */
1234 		hellcreek_populate_fdb_entry(hellcreek, entry, i);
1235 
1236 		/* Advance read pointer */
1237 		hellcreek_write(hellcreek, 0x00, HR_FDBRDH);
1238 	}
1239 
1240 	mutex_unlock(&hellcreek->reg_lock);
1241 
1242 	*data = (u8 *)table;
1243 
1244 	return 0;
1245 }
1246 
1247 static struct devlink_region_ops hellcreek_region_vlan_ops = {
1248 	.name	    = "vlan",
1249 	.snapshot   = hellcreek_devlink_region_vlan_snapshot,
1250 	.destructor = kfree,
1251 };
1252 
1253 static struct devlink_region_ops hellcreek_region_fdb_ops = {
1254 	.name	    = "fdb",
1255 	.snapshot   = hellcreek_devlink_region_fdb_snapshot,
1256 	.destructor = kfree,
1257 };
1258 
1259 static int hellcreek_setup_devlink_regions(struct dsa_switch *ds)
1260 {
1261 	struct hellcreek *hellcreek = ds->priv;
1262 	struct devlink_region_ops *ops;
1263 	struct devlink_region *region;
1264 	u64 size;
1265 	int ret;
1266 
1267 	/* VLAN table */
1268 	size = VLAN_N_VID * sizeof(struct hellcreek_devlink_vlan_entry);
1269 	ops  = &hellcreek_region_vlan_ops;
1270 
1271 	region = dsa_devlink_region_create(ds, ops, 1, size);
1272 	if (IS_ERR(region))
1273 		return PTR_ERR(region);
1274 
1275 	hellcreek->vlan_region = region;
1276 
1277 	/* FDB table */
1278 	size = hellcreek->fdb_entries * sizeof(struct hellcreek_fdb_entry);
1279 	ops  = &hellcreek_region_fdb_ops;
1280 
1281 	region = dsa_devlink_region_create(ds, ops, 1, size);
1282 	if (IS_ERR(region)) {
1283 		ret = PTR_ERR(region);
1284 		goto err_fdb;
1285 	}
1286 
1287 	hellcreek->fdb_region = region;
1288 
1289 	return 0;
1290 
1291 err_fdb:
1292 	dsa_devlink_region_destroy(hellcreek->vlan_region);
1293 
1294 	return ret;
1295 }
1296 
1297 static void hellcreek_teardown_devlink_regions(struct dsa_switch *ds)
1298 {
1299 	struct hellcreek *hellcreek = ds->priv;
1300 
1301 	dsa_devlink_region_destroy(hellcreek->fdb_region);
1302 	dsa_devlink_region_destroy(hellcreek->vlan_region);
1303 }
1304 
1305 static int hellcreek_setup(struct dsa_switch *ds)
1306 {
1307 	struct hellcreek *hellcreek = ds->priv;
1308 	u16 swcfg = 0;
1309 	int ret, i;
1310 
1311 	dev_dbg(hellcreek->dev, "Set up the switch\n");
1312 
1313 	/* Let's go */
1314 	ret = hellcreek_enable_ip_core(hellcreek);
1315 	if (ret) {
1316 		dev_err(hellcreek->dev, "Failed to enable IP core!\n");
1317 		return ret;
1318 	}
1319 
1320 	/* Enable CPU/Tunnel ports */
1321 	hellcreek_setup_cpu_and_tunnel_port(hellcreek);
1322 
1323 	/* Switch config: Keep defaults, enable FDB aging and learning and tag
1324 	 * each frame from/to cpu port for DSA tagging.  Also enable the length
1325 	 * aware shaping mode. This eliminates the need for Qbv guard bands.
1326 	 */
1327 	swcfg |= HR_SWCFG_FDBAGE_EN |
1328 		HR_SWCFG_FDBLRN_EN  |
1329 		HR_SWCFG_ALWAYS_OBT |
1330 		(HR_SWCFG_LAS_ON << HR_SWCFG_LAS_MODE_SHIFT);
1331 	hellcreek->swcfg = swcfg;
1332 	hellcreek_write(hellcreek, swcfg, HR_SWCFG);
1333 
1334 	/* Initial vlan membership to reflect port separation */
1335 	for (i = 0; i < ds->num_ports; ++i) {
1336 		if (!dsa_is_user_port(ds, i))
1337 			continue;
1338 
1339 		hellcreek_setup_vlan_membership(ds, i, true);
1340 	}
1341 
1342 	/* Configure PCP <-> TC mapping */
1343 	hellcreek_setup_tc_identity_mapping(hellcreek);
1344 
1345 	/* The VLAN awareness is a global switch setting. Therefore, mixed vlan
1346 	 * filtering setups are not supported.
1347 	 */
1348 	ds->vlan_filtering_is_global = true;
1349 	ds->needs_standalone_vlan_filtering = true;
1350 
1351 	/* Intercept _all_ PTP multicast traffic */
1352 	ret = hellcreek_setup_fdb(hellcreek);
1353 	if (ret) {
1354 		dev_err(hellcreek->dev,
1355 			"Failed to insert static PTP FDB entries\n");
1356 		return ret;
1357 	}
1358 
1359 	/* Register devlink resources with DSA */
1360 	ret = hellcreek_setup_devlink_resources(ds);
1361 	if (ret) {
1362 		dev_err(hellcreek->dev,
1363 			"Failed to setup devlink resources!\n");
1364 		return ret;
1365 	}
1366 
1367 	ret = hellcreek_setup_devlink_regions(ds);
1368 	if (ret) {
1369 		dev_err(hellcreek->dev,
1370 			"Failed to setup devlink regions!\n");
1371 		goto err_regions;
1372 	}
1373 
1374 	return 0;
1375 
1376 err_regions:
1377 	dsa_devlink_resources_unregister(ds);
1378 
1379 	return ret;
1380 }
1381 
1382 static void hellcreek_teardown(struct dsa_switch *ds)
1383 {
1384 	hellcreek_teardown_devlink_regions(ds);
1385 	dsa_devlink_resources_unregister(ds);
1386 }
1387 
1388 static void hellcreek_phylink_get_caps(struct dsa_switch *ds, int port,
1389 				       struct phylink_config *config)
1390 {
1391 	struct hellcreek *hellcreek = ds->priv;
1392 
1393 	__set_bit(PHY_INTERFACE_MODE_MII, config->supported_interfaces);
1394 	__set_bit(PHY_INTERFACE_MODE_RGMII, config->supported_interfaces);
1395 
1396 	/* Include GMII - the hardware does not support this interface
1397 	 * mode, but it's the default interface mode for phylib, so we
1398 	 * need it for compatibility with existing DT.
1399 	 */
1400 	__set_bit(PHY_INTERFACE_MODE_GMII, config->supported_interfaces);
1401 
1402 	/* The MAC settings are a hardware configuration option and cannot be
1403 	 * changed at run time or by strapping. Therefore the attached PHYs
1404 	 * should be programmed to only advertise settings which are supported
1405 	 * by the hardware.
1406 	 */
1407 	if (hellcreek->pdata->is_100_mbits)
1408 		config->mac_capabilities = MAC_100FD;
1409 	else
1410 		config->mac_capabilities = MAC_1000FD;
1411 }
1412 
1413 static int
1414 hellcreek_port_prechangeupper(struct dsa_switch *ds, int port,
1415 			      struct netdev_notifier_changeupper_info *info)
1416 {
1417 	struct hellcreek *hellcreek = ds->priv;
1418 	bool used = true;
1419 	int ret = -EBUSY;
1420 	u16 vid;
1421 	int i;
1422 
1423 	dev_dbg(hellcreek->dev, "Pre change upper for port %d\n", port);
1424 
1425 	/*
1426 	 * Deny VLAN devices on top of lan ports with the same VLAN ids, because
1427 	 * it breaks the port separation due to the private VLANs. Example:
1428 	 *
1429 	 * lan0.100 *and* lan1.100 cannot be used in parallel. However, lan0.99
1430 	 * and lan1.100 works.
1431 	 */
1432 
1433 	if (!is_vlan_dev(info->upper_dev))
1434 		return 0;
1435 
1436 	vid = vlan_dev_vlan_id(info->upper_dev);
1437 
1438 	/* For all ports, check bitmaps */
1439 	mutex_lock(&hellcreek->vlan_lock);
1440 	for (i = 0; i < hellcreek->pdata->num_ports; ++i) {
1441 		if (!dsa_is_user_port(ds, i))
1442 			continue;
1443 
1444 		if (port == i)
1445 			continue;
1446 
1447 		used = used && test_bit(vid, hellcreek->ports[i].vlan_dev_bitmap);
1448 	}
1449 
1450 	if (used)
1451 		goto out;
1452 
1453 	/* Update bitmap */
1454 	set_bit(vid, hellcreek->ports[port].vlan_dev_bitmap);
1455 
1456 	ret = 0;
1457 
1458 out:
1459 	mutex_unlock(&hellcreek->vlan_lock);
1460 
1461 	return ret;
1462 }
1463 
1464 static void hellcreek_setup_gcl(struct hellcreek *hellcreek, int port,
1465 				const struct tc_taprio_qopt_offload *schedule)
1466 {
1467 	const struct tc_taprio_sched_entry *cur, *initial, *next;
1468 	size_t i;
1469 
1470 	cur = initial = &schedule->entries[0];
1471 	next = cur + 1;
1472 
1473 	for (i = 1; i <= schedule->num_entries; ++i) {
1474 		u16 data;
1475 		u8 gates;
1476 
1477 		if (i == schedule->num_entries)
1478 			gates = initial->gate_mask ^
1479 				cur->gate_mask;
1480 		else
1481 			gates = next->gate_mask ^
1482 				cur->gate_mask;
1483 
1484 		data = gates;
1485 
1486 		if (i == schedule->num_entries)
1487 			data |= TR_GCLDAT_GCLWRLAST;
1488 
1489 		/* Gates states */
1490 		hellcreek_write(hellcreek, data, TR_GCLDAT);
1491 
1492 		/* Time interval */
1493 		hellcreek_write(hellcreek,
1494 				cur->interval & 0x0000ffff,
1495 				TR_GCLTIL);
1496 		hellcreek_write(hellcreek,
1497 				(cur->interval & 0xffff0000) >> 16,
1498 				TR_GCLTIH);
1499 
1500 		/* Commit entry */
1501 		data = ((i - 1) << TR_GCLCMD_GCLWRADR_SHIFT) |
1502 			(initial->gate_mask <<
1503 			 TR_GCLCMD_INIT_GATE_STATES_SHIFT);
1504 		hellcreek_write(hellcreek, data, TR_GCLCMD);
1505 
1506 		cur++;
1507 		next++;
1508 	}
1509 }
1510 
1511 static void hellcreek_set_cycle_time(struct hellcreek *hellcreek,
1512 				     const struct tc_taprio_qopt_offload *schedule)
1513 {
1514 	u32 cycle_time = schedule->cycle_time;
1515 
1516 	hellcreek_write(hellcreek, cycle_time & 0x0000ffff, TR_CTWRL);
1517 	hellcreek_write(hellcreek, (cycle_time & 0xffff0000) >> 16, TR_CTWRH);
1518 }
1519 
1520 static void hellcreek_switch_schedule(struct hellcreek *hellcreek,
1521 				      ktime_t start_time)
1522 {
1523 	struct timespec64 ts = ktime_to_timespec64(start_time);
1524 
1525 	/* Start schedule at this point of time */
1526 	hellcreek_write(hellcreek, ts.tv_nsec & 0x0000ffff, TR_ESTWRL);
1527 	hellcreek_write(hellcreek, (ts.tv_nsec & 0xffff0000) >> 16, TR_ESTWRH);
1528 
1529 	/* Arm timer, set seconds and switch schedule */
1530 	hellcreek_write(hellcreek, TR_ESTCMD_ESTARM | TR_ESTCMD_ESTSWCFG |
1531 			((ts.tv_sec & TR_ESTCMD_ESTSEC_MASK) <<
1532 			 TR_ESTCMD_ESTSEC_SHIFT), TR_ESTCMD);
1533 }
1534 
1535 static bool hellcreek_schedule_startable(struct hellcreek *hellcreek, int port)
1536 {
1537 	struct hellcreek_port *hellcreek_port = &hellcreek->ports[port];
1538 	s64 base_time_ns, current_ns;
1539 
1540 	/* The switch allows a schedule to be started only eight seconds within
1541 	 * the future. Therefore, check the current PTP time if the schedule is
1542 	 * startable or not.
1543 	 */
1544 
1545 	/* Use the "cached" time. That should be alright, as it's updated quite
1546 	 * frequently in the PTP code.
1547 	 */
1548 	mutex_lock(&hellcreek->ptp_lock);
1549 	current_ns = hellcreek->seconds * NSEC_PER_SEC + hellcreek->last_ts;
1550 	mutex_unlock(&hellcreek->ptp_lock);
1551 
1552 	/* Calculate difference to admin base time */
1553 	base_time_ns = ktime_to_ns(hellcreek_port->current_schedule->base_time);
1554 
1555 	return base_time_ns - current_ns < (s64)4 * NSEC_PER_SEC;
1556 }
1557 
1558 static void hellcreek_start_schedule(struct hellcreek *hellcreek, int port)
1559 {
1560 	struct hellcreek_port *hellcreek_port = &hellcreek->ports[port];
1561 	ktime_t base_time, current_time;
1562 	s64 current_ns;
1563 	u32 cycle_time;
1564 
1565 	/* First select port */
1566 	hellcreek_select_tgd(hellcreek, port);
1567 
1568 	/* Forward base time into the future if needed */
1569 	mutex_lock(&hellcreek->ptp_lock);
1570 	current_ns = hellcreek->seconds * NSEC_PER_SEC + hellcreek->last_ts;
1571 	mutex_unlock(&hellcreek->ptp_lock);
1572 
1573 	current_time = ns_to_ktime(current_ns);
1574 	base_time    = hellcreek_port->current_schedule->base_time;
1575 	cycle_time   = hellcreek_port->current_schedule->cycle_time;
1576 
1577 	if (ktime_compare(current_time, base_time) > 0) {
1578 		s64 n;
1579 
1580 		n = div64_s64(ktime_sub_ns(current_time, base_time),
1581 			      cycle_time);
1582 		base_time = ktime_add_ns(base_time, (n + 1) * cycle_time);
1583 	}
1584 
1585 	/* Set admin base time and switch schedule */
1586 	hellcreek_switch_schedule(hellcreek, base_time);
1587 
1588 	taprio_offload_free(hellcreek_port->current_schedule);
1589 	hellcreek_port->current_schedule = NULL;
1590 
1591 	dev_dbg(hellcreek->dev, "Armed EST timer for port %d\n",
1592 		hellcreek_port->port);
1593 }
1594 
1595 static void hellcreek_check_schedule(struct work_struct *work)
1596 {
1597 	struct delayed_work *dw = to_delayed_work(work);
1598 	struct hellcreek_port *hellcreek_port;
1599 	struct hellcreek *hellcreek;
1600 	bool startable;
1601 
1602 	hellcreek_port = dw_to_hellcreek_port(dw);
1603 	hellcreek = hellcreek_port->hellcreek;
1604 
1605 	mutex_lock(&hellcreek->reg_lock);
1606 
1607 	/* Check starting time */
1608 	startable = hellcreek_schedule_startable(hellcreek,
1609 						 hellcreek_port->port);
1610 	if (startable) {
1611 		hellcreek_start_schedule(hellcreek, hellcreek_port->port);
1612 		mutex_unlock(&hellcreek->reg_lock);
1613 		return;
1614 	}
1615 
1616 	mutex_unlock(&hellcreek->reg_lock);
1617 
1618 	/* Reschedule */
1619 	schedule_delayed_work(&hellcreek_port->schedule_work,
1620 			      HELLCREEK_SCHEDULE_PERIOD);
1621 }
1622 
1623 static int hellcreek_port_set_schedule(struct dsa_switch *ds, int port,
1624 				       struct tc_taprio_qopt_offload *taprio)
1625 {
1626 	struct hellcreek *hellcreek = ds->priv;
1627 	struct hellcreek_port *hellcreek_port;
1628 	bool startable;
1629 	u16 ctrl;
1630 
1631 	hellcreek_port = &hellcreek->ports[port];
1632 
1633 	dev_dbg(hellcreek->dev, "Configure traffic schedule on port %d\n",
1634 		port);
1635 
1636 	/* First cancel delayed work */
1637 	cancel_delayed_work_sync(&hellcreek_port->schedule_work);
1638 
1639 	mutex_lock(&hellcreek->reg_lock);
1640 
1641 	if (hellcreek_port->current_schedule) {
1642 		taprio_offload_free(hellcreek_port->current_schedule);
1643 		hellcreek_port->current_schedule = NULL;
1644 	}
1645 	hellcreek_port->current_schedule = taprio_offload_get(taprio);
1646 
1647 	/* Then select port */
1648 	hellcreek_select_tgd(hellcreek, port);
1649 
1650 	/* Enable gating and keep defaults */
1651 	ctrl = (0xff << TR_TGDCTRL_ADMINGATESTATES_SHIFT) | TR_TGDCTRL_GATE_EN;
1652 	hellcreek_write(hellcreek, ctrl, TR_TGDCTRL);
1653 
1654 	/* Cancel pending schedule */
1655 	hellcreek_write(hellcreek, 0x00, TR_ESTCMD);
1656 
1657 	/* Setup a new schedule */
1658 	hellcreek_setup_gcl(hellcreek, port, hellcreek_port->current_schedule);
1659 
1660 	/* Configure cycle time */
1661 	hellcreek_set_cycle_time(hellcreek, hellcreek_port->current_schedule);
1662 
1663 	/* Check starting time */
1664 	startable = hellcreek_schedule_startable(hellcreek, port);
1665 	if (startable) {
1666 		hellcreek_start_schedule(hellcreek, port);
1667 		mutex_unlock(&hellcreek->reg_lock);
1668 		return 0;
1669 	}
1670 
1671 	mutex_unlock(&hellcreek->reg_lock);
1672 
1673 	/* Schedule periodic schedule check */
1674 	schedule_delayed_work(&hellcreek_port->schedule_work,
1675 			      HELLCREEK_SCHEDULE_PERIOD);
1676 
1677 	return 0;
1678 }
1679 
1680 static int hellcreek_port_del_schedule(struct dsa_switch *ds, int port)
1681 {
1682 	struct hellcreek *hellcreek = ds->priv;
1683 	struct hellcreek_port *hellcreek_port;
1684 
1685 	hellcreek_port = &hellcreek->ports[port];
1686 
1687 	dev_dbg(hellcreek->dev, "Remove traffic schedule on port %d\n", port);
1688 
1689 	/* First cancel delayed work */
1690 	cancel_delayed_work_sync(&hellcreek_port->schedule_work);
1691 
1692 	mutex_lock(&hellcreek->reg_lock);
1693 
1694 	if (hellcreek_port->current_schedule) {
1695 		taprio_offload_free(hellcreek_port->current_schedule);
1696 		hellcreek_port->current_schedule = NULL;
1697 	}
1698 
1699 	/* Then select port */
1700 	hellcreek_select_tgd(hellcreek, port);
1701 
1702 	/* Disable gating and return to regular switching flow */
1703 	hellcreek_write(hellcreek, 0xff << TR_TGDCTRL_ADMINGATESTATES_SHIFT,
1704 			TR_TGDCTRL);
1705 
1706 	mutex_unlock(&hellcreek->reg_lock);
1707 
1708 	return 0;
1709 }
1710 
1711 static bool hellcreek_validate_schedule(struct hellcreek *hellcreek,
1712 					struct tc_taprio_qopt_offload *schedule)
1713 {
1714 	size_t i;
1715 
1716 	/* Does this hellcreek version support Qbv in hardware? */
1717 	if (!hellcreek->pdata->qbv_support)
1718 		return false;
1719 
1720 	/* cycle time can only be 32bit */
1721 	if (schedule->cycle_time > (u32)-1)
1722 		return false;
1723 
1724 	/* cycle time extension is not supported */
1725 	if (schedule->cycle_time_extension)
1726 		return false;
1727 
1728 	/* Only set command is supported */
1729 	for (i = 0; i < schedule->num_entries; ++i)
1730 		if (schedule->entries[i].command != TC_TAPRIO_CMD_SET_GATES)
1731 			return false;
1732 
1733 	return true;
1734 }
1735 
1736 static int hellcreek_port_setup_tc(struct dsa_switch *ds, int port,
1737 				   enum tc_setup_type type, void *type_data)
1738 {
1739 	struct tc_taprio_qopt_offload *taprio = type_data;
1740 	struct hellcreek *hellcreek = ds->priv;
1741 
1742 	if (type != TC_SETUP_QDISC_TAPRIO)
1743 		return -EOPNOTSUPP;
1744 
1745 	if (!hellcreek_validate_schedule(hellcreek, taprio))
1746 		return -EOPNOTSUPP;
1747 
1748 	if (taprio->enable)
1749 		return hellcreek_port_set_schedule(ds, port, taprio);
1750 
1751 	return hellcreek_port_del_schedule(ds, port);
1752 }
1753 
1754 static const struct dsa_switch_ops hellcreek_ds_ops = {
1755 	.devlink_info_get      = hellcreek_devlink_info_get,
1756 	.get_ethtool_stats     = hellcreek_get_ethtool_stats,
1757 	.get_sset_count	       = hellcreek_get_sset_count,
1758 	.get_strings	       = hellcreek_get_strings,
1759 	.get_tag_protocol      = hellcreek_get_tag_protocol,
1760 	.get_ts_info	       = hellcreek_get_ts_info,
1761 	.phylink_get_caps      = hellcreek_phylink_get_caps,
1762 	.port_bridge_flags     = hellcreek_bridge_flags,
1763 	.port_bridge_join      = hellcreek_port_bridge_join,
1764 	.port_bridge_leave     = hellcreek_port_bridge_leave,
1765 	.port_disable	       = hellcreek_port_disable,
1766 	.port_enable	       = hellcreek_port_enable,
1767 	.port_fdb_add	       = hellcreek_fdb_add,
1768 	.port_fdb_del	       = hellcreek_fdb_del,
1769 	.port_fdb_dump	       = hellcreek_fdb_dump,
1770 	.port_hwtstamp_set     = hellcreek_port_hwtstamp_set,
1771 	.port_hwtstamp_get     = hellcreek_port_hwtstamp_get,
1772 	.port_pre_bridge_flags = hellcreek_pre_bridge_flags,
1773 	.port_prechangeupper   = hellcreek_port_prechangeupper,
1774 	.port_rxtstamp	       = hellcreek_port_rxtstamp,
1775 	.port_setup_tc	       = hellcreek_port_setup_tc,
1776 	.port_stp_state_set    = hellcreek_port_stp_state_set,
1777 	.port_txtstamp	       = hellcreek_port_txtstamp,
1778 	.port_vlan_add	       = hellcreek_vlan_add,
1779 	.port_vlan_del	       = hellcreek_vlan_del,
1780 	.port_vlan_filtering   = hellcreek_vlan_filtering,
1781 	.setup		       = hellcreek_setup,
1782 	.teardown	       = hellcreek_teardown,
1783 };
1784 
1785 static int hellcreek_probe(struct platform_device *pdev)
1786 {
1787 	struct device *dev = &pdev->dev;
1788 	struct hellcreek *hellcreek;
1789 	struct resource *res;
1790 	int ret, i;
1791 
1792 	hellcreek = devm_kzalloc(dev, sizeof(*hellcreek), GFP_KERNEL);
1793 	if (!hellcreek)
1794 		return -ENOMEM;
1795 
1796 	hellcreek->vidmbrcfg = devm_kcalloc(dev, VLAN_N_VID,
1797 					    sizeof(*hellcreek->vidmbrcfg),
1798 					    GFP_KERNEL);
1799 	if (!hellcreek->vidmbrcfg)
1800 		return -ENOMEM;
1801 
1802 	hellcreek->pdata = of_device_get_match_data(dev);
1803 
1804 	hellcreek->ports = devm_kcalloc(dev, hellcreek->pdata->num_ports,
1805 					sizeof(*hellcreek->ports),
1806 					GFP_KERNEL);
1807 	if (!hellcreek->ports)
1808 		return -ENOMEM;
1809 
1810 	for (i = 0; i < hellcreek->pdata->num_ports; ++i) {
1811 		struct hellcreek_port *port = &hellcreek->ports[i];
1812 
1813 		port->counter_values =
1814 			devm_kcalloc(dev,
1815 				     ARRAY_SIZE(hellcreek_counter),
1816 				     sizeof(*port->counter_values),
1817 				     GFP_KERNEL);
1818 		if (!port->counter_values)
1819 			return -ENOMEM;
1820 
1821 		port->vlan_dev_bitmap =
1822 			devm_kcalloc(dev,
1823 				     BITS_TO_LONGS(VLAN_N_VID),
1824 				     sizeof(unsigned long),
1825 				     GFP_KERNEL);
1826 		if (!port->vlan_dev_bitmap)
1827 			return -ENOMEM;
1828 
1829 		port->hellcreek	= hellcreek;
1830 		port->port	= i;
1831 
1832 		INIT_DELAYED_WORK(&port->schedule_work,
1833 				  hellcreek_check_schedule);
1834 	}
1835 
1836 	mutex_init(&hellcreek->reg_lock);
1837 	mutex_init(&hellcreek->vlan_lock);
1838 	mutex_init(&hellcreek->ptp_lock);
1839 
1840 	hellcreek->dev = dev;
1841 
1842 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "tsn");
1843 	if (!res) {
1844 		dev_err(dev, "No memory region provided!\n");
1845 		return -ENODEV;
1846 	}
1847 
1848 	hellcreek->base = devm_ioremap_resource(dev, res);
1849 	if (IS_ERR(hellcreek->base))
1850 		return PTR_ERR(hellcreek->base);
1851 
1852 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ptp");
1853 	if (!res) {
1854 		dev_err(dev, "No PTP memory region provided!\n");
1855 		return -ENODEV;
1856 	}
1857 
1858 	hellcreek->ptp_base = devm_ioremap_resource(dev, res);
1859 	if (IS_ERR(hellcreek->ptp_base))
1860 		return PTR_ERR(hellcreek->ptp_base);
1861 
1862 	ret = hellcreek_detect(hellcreek);
1863 	if (ret) {
1864 		dev_err(dev, "No (known) chip found!\n");
1865 		return ret;
1866 	}
1867 
1868 	ret = hellcreek_wait_until_ready(hellcreek);
1869 	if (ret) {
1870 		dev_err(dev, "Switch didn't become ready!\n");
1871 		return ret;
1872 	}
1873 
1874 	hellcreek_feature_detect(hellcreek);
1875 
1876 	hellcreek->ds = devm_kzalloc(dev, sizeof(*hellcreek->ds), GFP_KERNEL);
1877 	if (!hellcreek->ds)
1878 		return -ENOMEM;
1879 
1880 	hellcreek->ds->dev	     = dev;
1881 	hellcreek->ds->priv	     = hellcreek;
1882 	hellcreek->ds->ops	     = &hellcreek_ds_ops;
1883 	hellcreek->ds->num_ports     = hellcreek->pdata->num_ports;
1884 	hellcreek->ds->num_tx_queues = HELLCREEK_NUM_EGRESS_QUEUES;
1885 
1886 	ret = dsa_register_switch(hellcreek->ds);
1887 	if (ret) {
1888 		dev_err_probe(dev, ret, "Unable to register switch\n");
1889 		return ret;
1890 	}
1891 
1892 	ret = hellcreek_ptp_setup(hellcreek);
1893 	if (ret) {
1894 		dev_err(dev, "Failed to setup PTP!\n");
1895 		goto err_ptp_setup;
1896 	}
1897 
1898 	ret = hellcreek_hwtstamp_setup(hellcreek);
1899 	if (ret) {
1900 		dev_err(dev, "Failed to setup hardware timestamping!\n");
1901 		goto err_tstamp_setup;
1902 	}
1903 
1904 	platform_set_drvdata(pdev, hellcreek);
1905 
1906 	return 0;
1907 
1908 err_tstamp_setup:
1909 	hellcreek_ptp_free(hellcreek);
1910 err_ptp_setup:
1911 	dsa_unregister_switch(hellcreek->ds);
1912 
1913 	return ret;
1914 }
1915 
1916 static int hellcreek_remove(struct platform_device *pdev)
1917 {
1918 	struct hellcreek *hellcreek = platform_get_drvdata(pdev);
1919 
1920 	if (!hellcreek)
1921 		return 0;
1922 
1923 	hellcreek_hwtstamp_free(hellcreek);
1924 	hellcreek_ptp_free(hellcreek);
1925 	dsa_unregister_switch(hellcreek->ds);
1926 	platform_set_drvdata(pdev, NULL);
1927 
1928 	return 0;
1929 }
1930 
1931 static void hellcreek_shutdown(struct platform_device *pdev)
1932 {
1933 	struct hellcreek *hellcreek = platform_get_drvdata(pdev);
1934 
1935 	if (!hellcreek)
1936 		return;
1937 
1938 	dsa_switch_shutdown(hellcreek->ds);
1939 
1940 	platform_set_drvdata(pdev, NULL);
1941 }
1942 
1943 static const struct hellcreek_platform_data de1soc_r1_pdata = {
1944 	.name		 = "r4c30",
1945 	.num_ports	 = 4,
1946 	.is_100_mbits	 = 1,
1947 	.qbv_support	 = 1,
1948 	.qbv_on_cpu_port = 1,
1949 	.qbu_support	 = 0,
1950 	.module_id	 = 0x4c30,
1951 };
1952 
1953 static const struct of_device_id hellcreek_of_match[] = {
1954 	{
1955 		.compatible = "hirschmann,hellcreek-de1soc-r1",
1956 		.data	    = &de1soc_r1_pdata,
1957 	},
1958 	{ /* sentinel */ },
1959 };
1960 MODULE_DEVICE_TABLE(of, hellcreek_of_match);
1961 
1962 static struct platform_driver hellcreek_driver = {
1963 	.probe	= hellcreek_probe,
1964 	.remove = hellcreek_remove,
1965 	.shutdown = hellcreek_shutdown,
1966 	.driver = {
1967 		.name = "hellcreek",
1968 		.of_match_table = hellcreek_of_match,
1969 	},
1970 };
1971 module_platform_driver(hellcreek_driver);
1972 
1973 MODULE_AUTHOR("Kurt Kanzenbach <kurt@linutronix.de>");
1974 MODULE_DESCRIPTION("Hirschmann Hellcreek driver");
1975 MODULE_LICENSE("Dual MIT/GPL");
1976