1 // SPDX-License-Identifier: (GPL-2.0 or MIT)
2 /*
3  * DSA driver for:
4  * Hirschmann Hellcreek TSN switch.
5  *
6  * Copyright (C) 2019,2020 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 int hellcreek_wait_until_ready(struct hellcreek *hellcreek)
157 {
158 	u16 val;
159 
160 	/* Wait up to 1ms, although 3 us should be enough */
161 	return readx_poll_timeout(hellcreek_read_ctrl, hellcreek,
162 				  val, val & HR_CTRL_C_READY,
163 				  3, 1000);
164 }
165 
166 static int hellcreek_wait_until_transitioned(struct hellcreek *hellcreek)
167 {
168 	u16 val;
169 
170 	return readx_poll_timeout_atomic(hellcreek_read_ctrl, hellcreek,
171 					 val, !(val & HR_CTRL_C_TRANSITION),
172 					 1, 1000);
173 }
174 
175 static int hellcreek_wait_fdb_ready(struct hellcreek *hellcreek)
176 {
177 	u16 val;
178 
179 	return readx_poll_timeout_atomic(hellcreek_read_stat, hellcreek,
180 					 val, !(val & HR_SWSTAT_BUSY),
181 					 1, 1000);
182 }
183 
184 static int hellcreek_detect(struct hellcreek *hellcreek)
185 {
186 	u16 id, rel_low, rel_high, date_low, date_high, tgd_ver;
187 	u8 tgd_maj, tgd_min;
188 	u32 rel, date;
189 
190 	id	  = hellcreek_read(hellcreek, HR_MODID_C);
191 	rel_low	  = hellcreek_read(hellcreek, HR_REL_L_C);
192 	rel_high  = hellcreek_read(hellcreek, HR_REL_H_C);
193 	date_low  = hellcreek_read(hellcreek, HR_BLD_L_C);
194 	date_high = hellcreek_read(hellcreek, HR_BLD_H_C);
195 	tgd_ver   = hellcreek_read(hellcreek, TR_TGDVER);
196 
197 	if (id != hellcreek->pdata->module_id)
198 		return -ENODEV;
199 
200 	rel	= rel_low | (rel_high << 16);
201 	date	= date_low | (date_high << 16);
202 	tgd_maj = (tgd_ver & TR_TGDVER_REV_MAJ_MASK) >> TR_TGDVER_REV_MAJ_SHIFT;
203 	tgd_min = (tgd_ver & TR_TGDVER_REV_MIN_MASK) >> TR_TGDVER_REV_MIN_SHIFT;
204 
205 	dev_info(hellcreek->dev, "Module ID=%02x Release=%04x Date=%04x TGD Version=%02x.%02x\n",
206 		 id, rel, date, tgd_maj, tgd_min);
207 
208 	return 0;
209 }
210 
211 static void hellcreek_feature_detect(struct hellcreek *hellcreek)
212 {
213 	u16 features;
214 
215 	features = hellcreek_read(hellcreek, HR_FEABITS0);
216 
217 	/* Currently we only detect the size of the FDB table */
218 	hellcreek->fdb_entries = ((features & HR_FEABITS0_FDBBINS_MASK) >>
219 			       HR_FEABITS0_FDBBINS_SHIFT) * 32;
220 
221 	dev_info(hellcreek->dev, "Feature detect: FDB entries=%zu\n",
222 		 hellcreek->fdb_entries);
223 }
224 
225 static enum dsa_tag_protocol hellcreek_get_tag_protocol(struct dsa_switch *ds,
226 							int port,
227 							enum dsa_tag_protocol mp)
228 {
229 	return DSA_TAG_PROTO_HELLCREEK;
230 }
231 
232 static int hellcreek_port_enable(struct dsa_switch *ds, int port,
233 				 struct phy_device *phy)
234 {
235 	struct hellcreek *hellcreek = ds->priv;
236 	struct hellcreek_port *hellcreek_port;
237 	u16 val;
238 
239 	hellcreek_port = &hellcreek->ports[port];
240 
241 	dev_dbg(hellcreek->dev, "Enable port %d\n", port);
242 
243 	mutex_lock(&hellcreek->reg_lock);
244 
245 	hellcreek_select_port(hellcreek, port);
246 	val = hellcreek_port->ptcfg;
247 	val |= HR_PTCFG_ADMIN_EN;
248 	hellcreek_write(hellcreek, val, HR_PTCFG);
249 	hellcreek_port->ptcfg = val;
250 
251 	mutex_unlock(&hellcreek->reg_lock);
252 
253 	return 0;
254 }
255 
256 static void hellcreek_port_disable(struct dsa_switch *ds, int port)
257 {
258 	struct hellcreek *hellcreek = ds->priv;
259 	struct hellcreek_port *hellcreek_port;
260 	u16 val;
261 
262 	hellcreek_port = &hellcreek->ports[port];
263 
264 	dev_dbg(hellcreek->dev, "Disable port %d\n", port);
265 
266 	mutex_lock(&hellcreek->reg_lock);
267 
268 	hellcreek_select_port(hellcreek, port);
269 	val = hellcreek_port->ptcfg;
270 	val &= ~HR_PTCFG_ADMIN_EN;
271 	hellcreek_write(hellcreek, val, HR_PTCFG);
272 	hellcreek_port->ptcfg = val;
273 
274 	mutex_unlock(&hellcreek->reg_lock);
275 }
276 
277 static void hellcreek_get_strings(struct dsa_switch *ds, int port,
278 				  u32 stringset, uint8_t *data)
279 {
280 	int i;
281 
282 	for (i = 0; i < ARRAY_SIZE(hellcreek_counter); ++i) {
283 		const struct hellcreek_counter *counter = &hellcreek_counter[i];
284 
285 		strlcpy(data + i * ETH_GSTRING_LEN,
286 			counter->name, ETH_GSTRING_LEN);
287 	}
288 }
289 
290 static int hellcreek_get_sset_count(struct dsa_switch *ds, int port, int sset)
291 {
292 	if (sset != ETH_SS_STATS)
293 		return 0;
294 
295 	return ARRAY_SIZE(hellcreek_counter);
296 }
297 
298 static void hellcreek_get_ethtool_stats(struct dsa_switch *ds, int port,
299 					uint64_t *data)
300 {
301 	struct hellcreek *hellcreek = ds->priv;
302 	struct hellcreek_port *hellcreek_port;
303 	int i;
304 
305 	hellcreek_port = &hellcreek->ports[port];
306 
307 	for (i = 0; i < ARRAY_SIZE(hellcreek_counter); ++i) {
308 		const struct hellcreek_counter *counter = &hellcreek_counter[i];
309 		u8 offset = counter->offset + port * 64;
310 		u16 high, low;
311 		u64 value;
312 
313 		mutex_lock(&hellcreek->reg_lock);
314 
315 		hellcreek_select_counter(hellcreek, offset);
316 
317 		/* The registers are locked internally by selecting the
318 		 * counter. So low and high can be read without reading high
319 		 * again.
320 		 */
321 		high  = hellcreek_read(hellcreek, HR_CRDH);
322 		low   = hellcreek_read(hellcreek, HR_CRDL);
323 		value = ((u64)high << 16) | low;
324 
325 		hellcreek_port->counter_values[i] += value;
326 		data[i] = hellcreek_port->counter_values[i];
327 
328 		mutex_unlock(&hellcreek->reg_lock);
329 	}
330 }
331 
332 static u16 hellcreek_private_vid(int port)
333 {
334 	return VLAN_N_VID - port + 1;
335 }
336 
337 static int hellcreek_vlan_prepare(struct dsa_switch *ds, int port,
338 				  const struct switchdev_obj_port_vlan *vlan)
339 {
340 	struct hellcreek *hellcreek = ds->priv;
341 	int i;
342 
343 	dev_dbg(hellcreek->dev, "VLAN prepare for port %d\n", port);
344 
345 	/* Restriction: Make sure that nobody uses the "private" VLANs. These
346 	 * VLANs are internally used by the driver to ensure port
347 	 * separation. Thus, they cannot be used by someone else.
348 	 */
349 	for (i = 0; i < hellcreek->pdata->num_ports; ++i) {
350 		const u16 restricted_vid = hellcreek_private_vid(i);
351 		u16 vid;
352 
353 		if (!dsa_is_user_port(ds, i))
354 			continue;
355 
356 		for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
357 			if (vid == restricted_vid)
358 				return -EBUSY;
359 	}
360 
361 	return 0;
362 }
363 
364 static void hellcreek_select_vlan_params(struct hellcreek *hellcreek, int port,
365 					 int *shift, int *mask)
366 {
367 	switch (port) {
368 	case 0:
369 		*shift = HR_VIDMBRCFG_P0MBR_SHIFT;
370 		*mask  = HR_VIDMBRCFG_P0MBR_MASK;
371 		break;
372 	case 1:
373 		*shift = HR_VIDMBRCFG_P1MBR_SHIFT;
374 		*mask  = HR_VIDMBRCFG_P1MBR_MASK;
375 		break;
376 	case 2:
377 		*shift = HR_VIDMBRCFG_P2MBR_SHIFT;
378 		*mask  = HR_VIDMBRCFG_P2MBR_MASK;
379 		break;
380 	case 3:
381 		*shift = HR_VIDMBRCFG_P3MBR_SHIFT;
382 		*mask  = HR_VIDMBRCFG_P3MBR_MASK;
383 		break;
384 	default:
385 		*shift = *mask = 0;
386 		dev_err(hellcreek->dev, "Unknown port %d selected!\n", port);
387 	}
388 }
389 
390 static void hellcreek_apply_vlan(struct hellcreek *hellcreek, int port, u16 vid,
391 				 bool pvid, bool untagged)
392 {
393 	int shift, mask;
394 	u16 val;
395 
396 	dev_dbg(hellcreek->dev, "Apply VLAN: port=%d vid=%u pvid=%d untagged=%d",
397 		port, vid, pvid, untagged);
398 
399 	mutex_lock(&hellcreek->reg_lock);
400 
401 	hellcreek_select_port(hellcreek, port);
402 	hellcreek_select_vlan(hellcreek, vid, pvid);
403 
404 	/* Setup port vlan membership */
405 	hellcreek_select_vlan_params(hellcreek, port, &shift, &mask);
406 	val = hellcreek->vidmbrcfg[vid];
407 	val &= ~mask;
408 	if (untagged)
409 		val |= HELLCREEK_VLAN_UNTAGGED_MEMBER << shift;
410 	else
411 		val |= HELLCREEK_VLAN_TAGGED_MEMBER << shift;
412 
413 	hellcreek_write(hellcreek, val, HR_VIDMBRCFG);
414 	hellcreek->vidmbrcfg[vid] = val;
415 
416 	mutex_unlock(&hellcreek->reg_lock);
417 }
418 
419 static void hellcreek_unapply_vlan(struct hellcreek *hellcreek, int port,
420 				   u16 vid)
421 {
422 	int shift, mask;
423 	u16 val;
424 
425 	dev_dbg(hellcreek->dev, "Unapply VLAN: port=%d vid=%u\n", port, vid);
426 
427 	mutex_lock(&hellcreek->reg_lock);
428 
429 	hellcreek_select_vlan(hellcreek, vid, 0);
430 
431 	/* Setup port vlan membership */
432 	hellcreek_select_vlan_params(hellcreek, port, &shift, &mask);
433 	val = hellcreek->vidmbrcfg[vid];
434 	val &= ~mask;
435 	val |= HELLCREEK_VLAN_NO_MEMBER << shift;
436 
437 	hellcreek_write(hellcreek, val, HR_VIDMBRCFG);
438 	hellcreek->vidmbrcfg[vid] = val;
439 
440 	mutex_unlock(&hellcreek->reg_lock);
441 }
442 
443 static void hellcreek_vlan_add(struct dsa_switch *ds, int port,
444 			       const struct switchdev_obj_port_vlan *vlan)
445 {
446 	bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
447 	bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
448 	struct hellcreek *hellcreek = ds->priv;
449 	u16 vid;
450 
451 	dev_dbg(hellcreek->dev, "Add VLANs (%d -- %d) on port %d, %s, %s\n",
452 		vlan->vid_begin, vlan->vid_end, port,
453 		untagged ? "untagged" : "tagged",
454 		pvid ? "PVID" : "no PVID");
455 
456 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
457 		hellcreek_apply_vlan(hellcreek, port, vid, pvid, untagged);
458 }
459 
460 static int hellcreek_vlan_del(struct dsa_switch *ds, int port,
461 			      const struct switchdev_obj_port_vlan *vlan)
462 {
463 	struct hellcreek *hellcreek = ds->priv;
464 	u16 vid;
465 
466 	dev_dbg(hellcreek->dev, "Remove VLANs (%d -- %d) on port %d\n",
467 		vlan->vid_begin, vlan->vid_end, port);
468 
469 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
470 		hellcreek_unapply_vlan(hellcreek, port, vid);
471 
472 	return 0;
473 }
474 
475 static void hellcreek_port_stp_state_set(struct dsa_switch *ds, int port,
476 					 u8 state)
477 {
478 	struct hellcreek *hellcreek = ds->priv;
479 	struct hellcreek_port *hellcreek_port;
480 	const char *new_state;
481 	u16 val;
482 
483 	mutex_lock(&hellcreek->reg_lock);
484 
485 	hellcreek_port = &hellcreek->ports[port];
486 	val = hellcreek_port->ptcfg;
487 
488 	switch (state) {
489 	case BR_STATE_DISABLED:
490 		new_state = "DISABLED";
491 		val |= HR_PTCFG_BLOCKED;
492 		val &= ~HR_PTCFG_LEARNING_EN;
493 		break;
494 	case BR_STATE_BLOCKING:
495 		new_state = "BLOCKING";
496 		val |= HR_PTCFG_BLOCKED;
497 		val &= ~HR_PTCFG_LEARNING_EN;
498 		break;
499 	case BR_STATE_LISTENING:
500 		new_state = "LISTENING";
501 		val |= HR_PTCFG_BLOCKED;
502 		val &= ~HR_PTCFG_LEARNING_EN;
503 		break;
504 	case BR_STATE_LEARNING:
505 		new_state = "LEARNING";
506 		val |= HR_PTCFG_BLOCKED;
507 		val |= HR_PTCFG_LEARNING_EN;
508 		break;
509 	case BR_STATE_FORWARDING:
510 		new_state = "FORWARDING";
511 		val &= ~HR_PTCFG_BLOCKED;
512 		val |= HR_PTCFG_LEARNING_EN;
513 		break;
514 	default:
515 		new_state = "UNKNOWN";
516 	}
517 
518 	hellcreek_select_port(hellcreek, port);
519 	hellcreek_write(hellcreek, val, HR_PTCFG);
520 	hellcreek_port->ptcfg = val;
521 
522 	mutex_unlock(&hellcreek->reg_lock);
523 
524 	dev_dbg(hellcreek->dev, "Configured STP state for port %d: %s\n",
525 		port, new_state);
526 }
527 
528 static void hellcreek_setup_ingressflt(struct hellcreek *hellcreek, int port,
529 				       bool enable)
530 {
531 	struct hellcreek_port *hellcreek_port = &hellcreek->ports[port];
532 	u16 ptcfg;
533 
534 	mutex_lock(&hellcreek->reg_lock);
535 
536 	ptcfg = hellcreek_port->ptcfg;
537 
538 	if (enable)
539 		ptcfg |= HR_PTCFG_INGRESSFLT;
540 	else
541 		ptcfg &= ~HR_PTCFG_INGRESSFLT;
542 
543 	hellcreek_select_port(hellcreek, port);
544 	hellcreek_write(hellcreek, ptcfg, HR_PTCFG);
545 	hellcreek_port->ptcfg = ptcfg;
546 
547 	mutex_unlock(&hellcreek->reg_lock);
548 }
549 
550 static void hellcreek_setup_vlan_awareness(struct hellcreek *hellcreek,
551 					   bool enable)
552 {
553 	u16 swcfg;
554 
555 	mutex_lock(&hellcreek->reg_lock);
556 
557 	swcfg = hellcreek->swcfg;
558 
559 	if (enable)
560 		swcfg |= HR_SWCFG_VLAN_UNAWARE;
561 	else
562 		swcfg &= ~HR_SWCFG_VLAN_UNAWARE;
563 
564 	hellcreek_write(hellcreek, swcfg, HR_SWCFG);
565 
566 	mutex_unlock(&hellcreek->reg_lock);
567 }
568 
569 /* Default setup for DSA: VLAN <X>: CPU and Port <X> egress untagged. */
570 static void hellcreek_setup_vlan_membership(struct dsa_switch *ds, int port,
571 					    bool enabled)
572 {
573 	const u16 vid = hellcreek_private_vid(port);
574 	int upstream = dsa_upstream_port(ds, port);
575 	struct hellcreek *hellcreek = ds->priv;
576 
577 	/* Apply vid to port as egress untagged and port vlan id */
578 	if (enabled)
579 		hellcreek_apply_vlan(hellcreek, port, vid, true, true);
580 	else
581 		hellcreek_unapply_vlan(hellcreek, port, vid);
582 
583 	/* Apply vid to cpu port as well */
584 	if (enabled)
585 		hellcreek_apply_vlan(hellcreek, upstream, vid, false, true);
586 	else
587 		hellcreek_unapply_vlan(hellcreek, upstream, vid);
588 }
589 
590 static int hellcreek_port_bridge_join(struct dsa_switch *ds, int port,
591 				      struct net_device *br)
592 {
593 	struct hellcreek *hellcreek = ds->priv;
594 
595 	dev_dbg(hellcreek->dev, "Port %d joins a bridge\n", port);
596 
597 	/* When joining a vlan_filtering bridge, keep the switch VLAN aware */
598 	if (!ds->vlan_filtering)
599 		hellcreek_setup_vlan_awareness(hellcreek, false);
600 
601 	/* Drop private vlans */
602 	hellcreek_setup_vlan_membership(ds, port, false);
603 
604 	return 0;
605 }
606 
607 static void hellcreek_port_bridge_leave(struct dsa_switch *ds, int port,
608 					struct net_device *br)
609 {
610 	struct hellcreek *hellcreek = ds->priv;
611 
612 	dev_dbg(hellcreek->dev, "Port %d leaves a bridge\n", port);
613 
614 	/* Enable VLAN awareness */
615 	hellcreek_setup_vlan_awareness(hellcreek, true);
616 
617 	/* Enable private vlans */
618 	hellcreek_setup_vlan_membership(ds, port, true);
619 }
620 
621 static int __hellcreek_fdb_add(struct hellcreek *hellcreek,
622 			       const struct hellcreek_fdb_entry *entry)
623 {
624 	u16 meta = 0;
625 
626 	dev_dbg(hellcreek->dev, "Add static FDB entry: MAC=%pM, MASK=0x%02x, "
627 		"OBT=%d, REPRIO_EN=%d, PRIO=%d\n", entry->mac, entry->portmask,
628 		entry->is_obt, entry->reprio_en, entry->reprio_tc);
629 
630 	/* Add mac address */
631 	hellcreek_write(hellcreek, entry->mac[1] | (entry->mac[0] << 8), HR_FDBWDH);
632 	hellcreek_write(hellcreek, entry->mac[3] | (entry->mac[2] << 8), HR_FDBWDM);
633 	hellcreek_write(hellcreek, entry->mac[5] | (entry->mac[4] << 8), HR_FDBWDL);
634 
635 	/* Meta data */
636 	meta |= entry->portmask << HR_FDBWRM0_PORTMASK_SHIFT;
637 	if (entry->is_obt)
638 		meta |= HR_FDBWRM0_OBT;
639 	if (entry->reprio_en) {
640 		meta |= HR_FDBWRM0_REPRIO_EN;
641 		meta |= entry->reprio_tc << HR_FDBWRM0_REPRIO_TC_SHIFT;
642 	}
643 	hellcreek_write(hellcreek, meta, HR_FDBWRM0);
644 
645 	/* Commit */
646 	hellcreek_write(hellcreek, 0x00, HR_FDBWRCMD);
647 
648 	/* Wait until done */
649 	return hellcreek_wait_fdb_ready(hellcreek);
650 }
651 
652 static int __hellcreek_fdb_del(struct hellcreek *hellcreek,
653 			       const struct hellcreek_fdb_entry *entry)
654 {
655 	dev_dbg(hellcreek->dev, "Delete FDB entry: MAC=%pM!\n", entry->mac);
656 
657 	/* Delete by matching idx */
658 	hellcreek_write(hellcreek, entry->idx | HR_FDBWRCMD_FDBDEL, HR_FDBWRCMD);
659 
660 	/* Wait until done */
661 	return hellcreek_wait_fdb_ready(hellcreek);
662 }
663 
664 /* Retrieve the index of a FDB entry by mac address. Currently we search through
665  * the complete table in hardware. If that's too slow, we might have to cache
666  * the complete FDB table in software.
667  */
668 static int hellcreek_fdb_get(struct hellcreek *hellcreek,
669 			     const unsigned char *dest,
670 			     struct hellcreek_fdb_entry *entry)
671 {
672 	size_t i;
673 
674 	/* Set read pointer to zero: The read of HR_FDBMAX (read-only register)
675 	 * should reset the internal pointer. But, that doesn't work. The vendor
676 	 * suggested a subsequent write as workaround. Same for HR_FDBRDH below.
677 	 */
678 	hellcreek_read(hellcreek, HR_FDBMAX);
679 	hellcreek_write(hellcreek, 0x00, HR_FDBMAX);
680 
681 	/* We have to read the complete table, because the switch/driver might
682 	 * enter new entries anywhere.
683 	 */
684 	for (i = 0; i < hellcreek->fdb_entries; ++i) {
685 		unsigned char addr[ETH_ALEN];
686 		u16 meta, mac;
687 
688 		meta	= hellcreek_read(hellcreek, HR_FDBMDRD);
689 		mac	= hellcreek_read(hellcreek, HR_FDBRDL);
690 		addr[5] = mac & 0xff;
691 		addr[4] = (mac & 0xff00) >> 8;
692 		mac	= hellcreek_read(hellcreek, HR_FDBRDM);
693 		addr[3] = mac & 0xff;
694 		addr[2] = (mac & 0xff00) >> 8;
695 		mac	= hellcreek_read(hellcreek, HR_FDBRDH);
696 		addr[1] = mac & 0xff;
697 		addr[0] = (mac & 0xff00) >> 8;
698 
699 		/* Force next entry */
700 		hellcreek_write(hellcreek, 0x00, HR_FDBRDH);
701 
702 		if (memcmp(addr, dest, ETH_ALEN))
703 			continue;
704 
705 		/* Match found */
706 		entry->idx	    = i;
707 		entry->portmask	    = (meta & HR_FDBMDRD_PORTMASK_MASK) >>
708 			HR_FDBMDRD_PORTMASK_SHIFT;
709 		entry->age	    = (meta & HR_FDBMDRD_AGE_MASK) >>
710 			HR_FDBMDRD_AGE_SHIFT;
711 		entry->is_obt	    = !!(meta & HR_FDBMDRD_OBT);
712 		entry->pass_blocked = !!(meta & HR_FDBMDRD_PASS_BLOCKED);
713 		entry->is_static    = !!(meta & HR_FDBMDRD_STATIC);
714 		entry->reprio_tc    = (meta & HR_FDBMDRD_REPRIO_TC_MASK) >>
715 			HR_FDBMDRD_REPRIO_TC_SHIFT;
716 		entry->reprio_en    = !!(meta & HR_FDBMDRD_REPRIO_EN);
717 		memcpy(entry->mac, addr, sizeof(addr));
718 
719 		return 0;
720 	}
721 
722 	return -ENOENT;
723 }
724 
725 static int hellcreek_fdb_add(struct dsa_switch *ds, int port,
726 			     const unsigned char *addr, u16 vid)
727 {
728 	struct hellcreek_fdb_entry entry = { 0 };
729 	struct hellcreek *hellcreek = ds->priv;
730 	int ret;
731 
732 	dev_dbg(hellcreek->dev, "Add FDB entry for MAC=%pM\n", addr);
733 
734 	mutex_lock(&hellcreek->reg_lock);
735 
736 	ret = hellcreek_fdb_get(hellcreek, addr, &entry);
737 	if (ret) {
738 		/* Not found */
739 		memcpy(entry.mac, addr, sizeof(entry.mac));
740 		entry.portmask = BIT(port);
741 
742 		ret = __hellcreek_fdb_add(hellcreek, &entry);
743 		if (ret) {
744 			dev_err(hellcreek->dev, "Failed to add FDB entry!\n");
745 			goto out;
746 		}
747 	} else {
748 		/* Found */
749 		ret = __hellcreek_fdb_del(hellcreek, &entry);
750 		if (ret) {
751 			dev_err(hellcreek->dev, "Failed to delete FDB entry!\n");
752 			goto out;
753 		}
754 
755 		entry.portmask |= BIT(port);
756 
757 		ret = __hellcreek_fdb_add(hellcreek, &entry);
758 		if (ret) {
759 			dev_err(hellcreek->dev, "Failed to add FDB entry!\n");
760 			goto out;
761 		}
762 	}
763 
764 out:
765 	mutex_unlock(&hellcreek->reg_lock);
766 
767 	return ret;
768 }
769 
770 static int hellcreek_fdb_del(struct dsa_switch *ds, int port,
771 			     const unsigned char *addr, u16 vid)
772 {
773 	struct hellcreek_fdb_entry entry = { 0 };
774 	struct hellcreek *hellcreek = ds->priv;
775 	int ret;
776 
777 	dev_dbg(hellcreek->dev, "Delete FDB entry for MAC=%pM\n", addr);
778 
779 	mutex_lock(&hellcreek->reg_lock);
780 
781 	ret = hellcreek_fdb_get(hellcreek, addr, &entry);
782 	if (ret) {
783 		/* Not found */
784 		dev_err(hellcreek->dev, "FDB entry for deletion not found!\n");
785 	} else {
786 		/* Found */
787 		ret = __hellcreek_fdb_del(hellcreek, &entry);
788 		if (ret) {
789 			dev_err(hellcreek->dev, "Failed to delete FDB entry!\n");
790 			goto out;
791 		}
792 
793 		entry.portmask &= ~BIT(port);
794 
795 		if (entry.portmask != 0x00) {
796 			ret = __hellcreek_fdb_add(hellcreek, &entry);
797 			if (ret) {
798 				dev_err(hellcreek->dev, "Failed to add FDB entry!\n");
799 				goto out;
800 			}
801 		}
802 	}
803 
804 out:
805 	mutex_unlock(&hellcreek->reg_lock);
806 
807 	return ret;
808 }
809 
810 static int hellcreek_fdb_dump(struct dsa_switch *ds, int port,
811 			      dsa_fdb_dump_cb_t *cb, void *data)
812 {
813 	struct hellcreek *hellcreek = ds->priv;
814 	u16 entries;
815 	size_t i;
816 
817 	mutex_lock(&hellcreek->reg_lock);
818 
819 	/* Set read pointer to zero: The read of HR_FDBMAX (read-only register)
820 	 * should reset the internal pointer. But, that doesn't work. The vendor
821 	 * suggested a subsequent write as workaround. Same for HR_FDBRDH below.
822 	 */
823 	entries = hellcreek_read(hellcreek, HR_FDBMAX);
824 	hellcreek_write(hellcreek, 0x00, HR_FDBMAX);
825 
826 	dev_dbg(hellcreek->dev, "FDB dump for port %d, entries=%d!\n", port, entries);
827 
828 	/* Read table */
829 	for (i = 0; i < hellcreek->fdb_entries; ++i) {
830 		unsigned char null_addr[ETH_ALEN] = { 0 };
831 		struct hellcreek_fdb_entry entry = { 0 };
832 		u16 meta, mac;
833 
834 		meta	= hellcreek_read(hellcreek, HR_FDBMDRD);
835 		mac	= hellcreek_read(hellcreek, HR_FDBRDL);
836 		entry.mac[5] = mac & 0xff;
837 		entry.mac[4] = (mac & 0xff00) >> 8;
838 		mac	= hellcreek_read(hellcreek, HR_FDBRDM);
839 		entry.mac[3] = mac & 0xff;
840 		entry.mac[2] = (mac & 0xff00) >> 8;
841 		mac	= hellcreek_read(hellcreek, HR_FDBRDH);
842 		entry.mac[1] = mac & 0xff;
843 		entry.mac[0] = (mac & 0xff00) >> 8;
844 
845 		/* Force next entry */
846 		hellcreek_write(hellcreek, 0x00, HR_FDBRDH);
847 
848 		/* Check valid */
849 		if (!memcmp(entry.mac, null_addr, ETH_ALEN))
850 			continue;
851 
852 		entry.portmask	= (meta & HR_FDBMDRD_PORTMASK_MASK) >>
853 			HR_FDBMDRD_PORTMASK_SHIFT;
854 		entry.is_static	= !!(meta & HR_FDBMDRD_STATIC);
855 
856 		/* Check port mask */
857 		if (!(entry.portmask & BIT(port)))
858 			continue;
859 
860 		cb(entry.mac, 0, entry.is_static, data);
861 	}
862 
863 	mutex_unlock(&hellcreek->reg_lock);
864 
865 	return 0;
866 }
867 
868 static int hellcreek_vlan_filtering(struct dsa_switch *ds, int port,
869 				    bool vlan_filtering,
870 				    struct switchdev_trans *trans)
871 {
872 	struct hellcreek *hellcreek = ds->priv;
873 
874 	if (switchdev_trans_ph_prepare(trans))
875 		return 0;
876 
877 	dev_dbg(hellcreek->dev, "%s VLAN filtering on port %d\n",
878 		vlan_filtering ? "Enable" : "Disable", port);
879 
880 	/* Configure port to drop packages with not known vids */
881 	hellcreek_setup_ingressflt(hellcreek, port, vlan_filtering);
882 
883 	/* Enable VLAN awareness on the switch. This save due to
884 	 * ds->vlan_filtering_is_global.
885 	 */
886 	hellcreek_setup_vlan_awareness(hellcreek, vlan_filtering);
887 
888 	return 0;
889 }
890 
891 static int hellcreek_enable_ip_core(struct hellcreek *hellcreek)
892 {
893 	int ret;
894 	u16 val;
895 
896 	mutex_lock(&hellcreek->reg_lock);
897 
898 	val = hellcreek_read(hellcreek, HR_CTRL_C);
899 	val |= HR_CTRL_C_ENABLE;
900 	hellcreek_write(hellcreek, val, HR_CTRL_C);
901 	ret = hellcreek_wait_until_transitioned(hellcreek);
902 
903 	mutex_unlock(&hellcreek->reg_lock);
904 
905 	return ret;
906 }
907 
908 static void hellcreek_setup_cpu_and_tunnel_port(struct hellcreek *hellcreek)
909 {
910 	struct hellcreek_port *tunnel_port = &hellcreek->ports[TUNNEL_PORT];
911 	struct hellcreek_port *cpu_port = &hellcreek->ports[CPU_PORT];
912 	u16 ptcfg = 0;
913 
914 	ptcfg |= HR_PTCFG_LEARNING_EN | HR_PTCFG_ADMIN_EN;
915 
916 	mutex_lock(&hellcreek->reg_lock);
917 
918 	hellcreek_select_port(hellcreek, CPU_PORT);
919 	hellcreek_write(hellcreek, ptcfg, HR_PTCFG);
920 
921 	hellcreek_select_port(hellcreek, TUNNEL_PORT);
922 	hellcreek_write(hellcreek, ptcfg, HR_PTCFG);
923 
924 	cpu_port->ptcfg	   = ptcfg;
925 	tunnel_port->ptcfg = ptcfg;
926 
927 	mutex_unlock(&hellcreek->reg_lock);
928 }
929 
930 static void hellcreek_setup_tc_identity_mapping(struct hellcreek *hellcreek)
931 {
932 	int i;
933 
934 	/* The switch has multiple egress queues per port. The queue is selected
935 	 * via the PCP field in the VLAN header. The switch internally deals
936 	 * with traffic classes instead of PCP values and this mapping is
937 	 * configurable.
938 	 *
939 	 * The default mapping is (PCP - TC):
940 	 *  7 - 7
941 	 *  6 - 6
942 	 *  5 - 5
943 	 *  4 - 4
944 	 *  3 - 3
945 	 *  2 - 1
946 	 *  1 - 0
947 	 *  0 - 2
948 	 *
949 	 * The default should be an identity mapping.
950 	 */
951 
952 	for (i = 0; i < 8; ++i) {
953 		mutex_lock(&hellcreek->reg_lock);
954 
955 		hellcreek_select_prio(hellcreek, i);
956 		hellcreek_write(hellcreek,
957 				i << HR_PRTCCFG_PCP_TC_MAP_SHIFT,
958 				HR_PRTCCFG);
959 
960 		mutex_unlock(&hellcreek->reg_lock);
961 	}
962 }
963 
964 static int hellcreek_setup_fdb(struct hellcreek *hellcreek)
965 {
966 	static struct hellcreek_fdb_entry ptp = {
967 		/* MAC: 01-1B-19-00-00-00 */
968 		.mac	      = { 0x01, 0x1b, 0x19, 0x00, 0x00, 0x00 },
969 		.portmask     = 0x03,	/* Management ports */
970 		.age	      = 0,
971 		.is_obt	      = 0,
972 		.pass_blocked = 0,
973 		.is_static    = 1,
974 		.reprio_tc    = 6,	/* TC: 6 as per IEEE 802.1AS */
975 		.reprio_en    = 1,
976 	};
977 	static struct hellcreek_fdb_entry p2p = {
978 		/* MAC: 01-80-C2-00-00-0E */
979 		.mac	      = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x0e },
980 		.portmask     = 0x03,	/* Management ports */
981 		.age	      = 0,
982 		.is_obt	      = 0,
983 		.pass_blocked = 0,
984 		.is_static    = 1,
985 		.reprio_tc    = 6,	/* TC: 6 as per IEEE 802.1AS */
986 		.reprio_en    = 1,
987 	};
988 	int ret;
989 
990 	mutex_lock(&hellcreek->reg_lock);
991 	ret = __hellcreek_fdb_add(hellcreek, &ptp);
992 	if (ret)
993 		goto out;
994 	ret = __hellcreek_fdb_add(hellcreek, &p2p);
995 out:
996 	mutex_unlock(&hellcreek->reg_lock);
997 
998 	return ret;
999 }
1000 
1001 static int hellcreek_setup(struct dsa_switch *ds)
1002 {
1003 	struct hellcreek *hellcreek = ds->priv;
1004 	u16 swcfg = 0;
1005 	int ret, i;
1006 
1007 	dev_dbg(hellcreek->dev, "Set up the switch\n");
1008 
1009 	/* Let's go */
1010 	ret = hellcreek_enable_ip_core(hellcreek);
1011 	if (ret) {
1012 		dev_err(hellcreek->dev, "Failed to enable IP core!\n");
1013 		return ret;
1014 	}
1015 
1016 	/* Enable CPU/Tunnel ports */
1017 	hellcreek_setup_cpu_and_tunnel_port(hellcreek);
1018 
1019 	/* Switch config: Keep defaults, enable FDB aging and learning and tag
1020 	 * each frame from/to cpu port for DSA tagging.  Also enable the length
1021 	 * aware shaping mode. This eliminates the need for Qbv guard bands.
1022 	 */
1023 	swcfg |= HR_SWCFG_FDBAGE_EN |
1024 		HR_SWCFG_FDBLRN_EN  |
1025 		HR_SWCFG_ALWAYS_OBT |
1026 		(HR_SWCFG_LAS_ON << HR_SWCFG_LAS_MODE_SHIFT);
1027 	hellcreek->swcfg = swcfg;
1028 	hellcreek_write(hellcreek, swcfg, HR_SWCFG);
1029 
1030 	/* Initial vlan membership to reflect port separation */
1031 	for (i = 0; i < ds->num_ports; ++i) {
1032 		if (!dsa_is_user_port(ds, i))
1033 			continue;
1034 
1035 		hellcreek_setup_vlan_membership(ds, i, true);
1036 	}
1037 
1038 	/* Configure PCP <-> TC mapping */
1039 	hellcreek_setup_tc_identity_mapping(hellcreek);
1040 
1041 	/* Allow VLAN configurations while not filtering which is the default
1042 	 * for new DSA drivers.
1043 	 */
1044 	ds->configure_vlan_while_not_filtering = true;
1045 
1046 	/* The VLAN awareness is a global switch setting. Therefore, mixed vlan
1047 	 * filtering setups are not supported.
1048 	 */
1049 	ds->vlan_filtering_is_global = true;
1050 
1051 	/* Intercept _all_ PTP multicast traffic */
1052 	ret = hellcreek_setup_fdb(hellcreek);
1053 	if (ret) {
1054 		dev_err(hellcreek->dev,
1055 			"Failed to insert static PTP FDB entries\n");
1056 		return ret;
1057 	}
1058 
1059 	return 0;
1060 }
1061 
1062 static void hellcreek_phylink_validate(struct dsa_switch *ds, int port,
1063 				       unsigned long *supported,
1064 				       struct phylink_link_state *state)
1065 {
1066 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
1067 	struct hellcreek *hellcreek = ds->priv;
1068 
1069 	dev_dbg(hellcreek->dev, "Phylink validate for port %d\n", port);
1070 
1071 	/* The MAC settings are a hardware configuration option and cannot be
1072 	 * changed at run time or by strapping. Therefore the attached PHYs
1073 	 * should be programmed to only advertise settings which are supported
1074 	 * by the hardware.
1075 	 */
1076 	if (hellcreek->pdata->is_100_mbits)
1077 		phylink_set(mask, 100baseT_Full);
1078 	else
1079 		phylink_set(mask, 1000baseT_Full);
1080 
1081 	bitmap_and(supported, supported, mask,
1082 		   __ETHTOOL_LINK_MODE_MASK_NBITS);
1083 	bitmap_and(state->advertising, state->advertising, mask,
1084 		   __ETHTOOL_LINK_MODE_MASK_NBITS);
1085 }
1086 
1087 static int
1088 hellcreek_port_prechangeupper(struct dsa_switch *ds, int port,
1089 			      struct netdev_notifier_changeupper_info *info)
1090 {
1091 	struct hellcreek *hellcreek = ds->priv;
1092 	bool used = true;
1093 	int ret = -EBUSY;
1094 	u16 vid;
1095 	int i;
1096 
1097 	dev_dbg(hellcreek->dev, "Pre change upper for port %d\n", port);
1098 
1099 	/*
1100 	 * Deny VLAN devices on top of lan ports with the same VLAN ids, because
1101 	 * it breaks the port separation due to the private VLANs. Example:
1102 	 *
1103 	 * lan0.100 *and* lan1.100 cannot be used in parallel. However, lan0.99
1104 	 * and lan1.100 works.
1105 	 */
1106 
1107 	if (!is_vlan_dev(info->upper_dev))
1108 		return 0;
1109 
1110 	vid = vlan_dev_vlan_id(info->upper_dev);
1111 
1112 	/* For all ports, check bitmaps */
1113 	mutex_lock(&hellcreek->vlan_lock);
1114 	for (i = 0; i < hellcreek->pdata->num_ports; ++i) {
1115 		if (!dsa_is_user_port(ds, i))
1116 			continue;
1117 
1118 		if (port == i)
1119 			continue;
1120 
1121 		used = used && test_bit(vid, hellcreek->ports[i].vlan_dev_bitmap);
1122 	}
1123 
1124 	if (used)
1125 		goto out;
1126 
1127 	/* Update bitmap */
1128 	set_bit(vid, hellcreek->ports[port].vlan_dev_bitmap);
1129 
1130 	ret = 0;
1131 
1132 out:
1133 	mutex_unlock(&hellcreek->vlan_lock);
1134 
1135 	return ret;
1136 }
1137 
1138 static const struct dsa_switch_ops hellcreek_ds_ops = {
1139 	.get_ethtool_stats   = hellcreek_get_ethtool_stats,
1140 	.get_sset_count	     = hellcreek_get_sset_count,
1141 	.get_strings	     = hellcreek_get_strings,
1142 	.get_tag_protocol    = hellcreek_get_tag_protocol,
1143 	.get_ts_info	     = hellcreek_get_ts_info,
1144 	.phylink_validate    = hellcreek_phylink_validate,
1145 	.port_bridge_join    = hellcreek_port_bridge_join,
1146 	.port_bridge_leave   = hellcreek_port_bridge_leave,
1147 	.port_disable	     = hellcreek_port_disable,
1148 	.port_enable	     = hellcreek_port_enable,
1149 	.port_fdb_add	     = hellcreek_fdb_add,
1150 	.port_fdb_del	     = hellcreek_fdb_del,
1151 	.port_fdb_dump	     = hellcreek_fdb_dump,
1152 	.port_hwtstamp_set   = hellcreek_port_hwtstamp_set,
1153 	.port_hwtstamp_get   = hellcreek_port_hwtstamp_get,
1154 	.port_prechangeupper = hellcreek_port_prechangeupper,
1155 	.port_rxtstamp	     = hellcreek_port_rxtstamp,
1156 	.port_stp_state_set  = hellcreek_port_stp_state_set,
1157 	.port_txtstamp	     = hellcreek_port_txtstamp,
1158 	.port_vlan_add	     = hellcreek_vlan_add,
1159 	.port_vlan_del	     = hellcreek_vlan_del,
1160 	.port_vlan_filtering = hellcreek_vlan_filtering,
1161 	.port_vlan_prepare   = hellcreek_vlan_prepare,
1162 	.setup		     = hellcreek_setup,
1163 };
1164 
1165 static int hellcreek_probe(struct platform_device *pdev)
1166 {
1167 	struct device *dev = &pdev->dev;
1168 	struct hellcreek *hellcreek;
1169 	struct resource *res;
1170 	int ret, i;
1171 
1172 	hellcreek = devm_kzalloc(dev, sizeof(*hellcreek), GFP_KERNEL);
1173 	if (!hellcreek)
1174 		return -ENOMEM;
1175 
1176 	hellcreek->vidmbrcfg = devm_kcalloc(dev, VLAN_N_VID,
1177 					    sizeof(*hellcreek->vidmbrcfg),
1178 					    GFP_KERNEL);
1179 	if (!hellcreek->vidmbrcfg)
1180 		return -ENOMEM;
1181 
1182 	hellcreek->pdata = of_device_get_match_data(dev);
1183 
1184 	hellcreek->ports = devm_kcalloc(dev, hellcreek->pdata->num_ports,
1185 					sizeof(*hellcreek->ports),
1186 					GFP_KERNEL);
1187 	if (!hellcreek->ports)
1188 		return -ENOMEM;
1189 
1190 	for (i = 0; i < hellcreek->pdata->num_ports; ++i) {
1191 		struct hellcreek_port *port = &hellcreek->ports[i];
1192 
1193 		port->counter_values =
1194 			devm_kcalloc(dev,
1195 				     ARRAY_SIZE(hellcreek_counter),
1196 				     sizeof(*port->counter_values),
1197 				     GFP_KERNEL);
1198 		if (!port->counter_values)
1199 			return -ENOMEM;
1200 
1201 		port->vlan_dev_bitmap =
1202 			devm_kcalloc(dev,
1203 				     BITS_TO_LONGS(VLAN_N_VID),
1204 				     sizeof(unsigned long),
1205 				     GFP_KERNEL);
1206 		if (!port->vlan_dev_bitmap)
1207 			return -ENOMEM;
1208 
1209 		port->hellcreek	= hellcreek;
1210 		port->port	= i;
1211 	}
1212 
1213 	mutex_init(&hellcreek->reg_lock);
1214 	mutex_init(&hellcreek->vlan_lock);
1215 	mutex_init(&hellcreek->ptp_lock);
1216 
1217 	hellcreek->dev = dev;
1218 
1219 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "tsn");
1220 	if (!res) {
1221 		dev_err(dev, "No memory region provided!\n");
1222 		return -ENODEV;
1223 	}
1224 
1225 	hellcreek->base = devm_ioremap_resource(dev, res);
1226 	if (IS_ERR(hellcreek->base)) {
1227 		dev_err(dev, "No memory available!\n");
1228 		return PTR_ERR(hellcreek->base);
1229 	}
1230 
1231 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ptp");
1232 	if (!res) {
1233 		dev_err(dev, "No PTP memory region provided!\n");
1234 		return -ENODEV;
1235 	}
1236 
1237 	hellcreek->ptp_base = devm_ioremap_resource(dev, res);
1238 	if (IS_ERR(hellcreek->ptp_base)) {
1239 		dev_err(dev, "No memory available!\n");
1240 		return PTR_ERR(hellcreek->ptp_base);
1241 	}
1242 
1243 	ret = hellcreek_detect(hellcreek);
1244 	if (ret) {
1245 		dev_err(dev, "No (known) chip found!\n");
1246 		return ret;
1247 	}
1248 
1249 	ret = hellcreek_wait_until_ready(hellcreek);
1250 	if (ret) {
1251 		dev_err(dev, "Switch didn't become ready!\n");
1252 		return ret;
1253 	}
1254 
1255 	hellcreek_feature_detect(hellcreek);
1256 
1257 	hellcreek->ds = devm_kzalloc(dev, sizeof(*hellcreek->ds), GFP_KERNEL);
1258 	if (!hellcreek->ds)
1259 		return -ENOMEM;
1260 
1261 	hellcreek->ds->dev	     = dev;
1262 	hellcreek->ds->priv	     = hellcreek;
1263 	hellcreek->ds->ops	     = &hellcreek_ds_ops;
1264 	hellcreek->ds->num_ports     = hellcreek->pdata->num_ports;
1265 	hellcreek->ds->num_tx_queues = HELLCREEK_NUM_EGRESS_QUEUES;
1266 
1267 	ret = dsa_register_switch(hellcreek->ds);
1268 	if (ret) {
1269 		dev_err_probe(dev, ret, "Unable to register switch\n");
1270 		return ret;
1271 	}
1272 
1273 	ret = hellcreek_ptp_setup(hellcreek);
1274 	if (ret) {
1275 		dev_err(dev, "Failed to setup PTP!\n");
1276 		goto err_ptp_setup;
1277 	}
1278 
1279 	ret = hellcreek_hwtstamp_setup(hellcreek);
1280 	if (ret) {
1281 		dev_err(dev, "Failed to setup hardware timestamping!\n");
1282 		goto err_tstamp_setup;
1283 	}
1284 
1285 	platform_set_drvdata(pdev, hellcreek);
1286 
1287 	return 0;
1288 
1289 err_tstamp_setup:
1290 	hellcreek_ptp_free(hellcreek);
1291 err_ptp_setup:
1292 	dsa_unregister_switch(hellcreek->ds);
1293 
1294 	return ret;
1295 }
1296 
1297 static int hellcreek_remove(struct platform_device *pdev)
1298 {
1299 	struct hellcreek *hellcreek = platform_get_drvdata(pdev);
1300 
1301 	hellcreek_hwtstamp_free(hellcreek);
1302 	hellcreek_ptp_free(hellcreek);
1303 	dsa_unregister_switch(hellcreek->ds);
1304 	platform_set_drvdata(pdev, NULL);
1305 
1306 	return 0;
1307 }
1308 
1309 static const struct hellcreek_platform_data de1soc_r1_pdata = {
1310 	.num_ports	 = 4,
1311 	.is_100_mbits	 = 1,
1312 	.qbv_support	 = 1,
1313 	.qbv_on_cpu_port = 1,
1314 	.qbu_support	 = 0,
1315 	.module_id	 = 0x4c30,
1316 };
1317 
1318 static const struct of_device_id hellcreek_of_match[] = {
1319 	{
1320 		.compatible = "hirschmann,hellcreek-de1soc-r1",
1321 		.data	    = &de1soc_r1_pdata,
1322 	},
1323 	{ /* sentinel */ },
1324 };
1325 MODULE_DEVICE_TABLE(of, hellcreek_of_match);
1326 
1327 static struct platform_driver hellcreek_driver = {
1328 	.probe	= hellcreek_probe,
1329 	.remove = hellcreek_remove,
1330 	.driver = {
1331 		.name = "hellcreek",
1332 		.of_match_table = hellcreek_of_match,
1333 	},
1334 };
1335 module_platform_driver(hellcreek_driver);
1336 
1337 MODULE_AUTHOR("Kurt Kanzenbach <kurt@linutronix.de>");
1338 MODULE_DESCRIPTION("Hirschmann Hellcreek driver");
1339 MODULE_LICENSE("Dual MIT/GPL");
1340