xref: /freebsd/sys/dev/etherswitch/mtkswitch/mtkswitch.c (revision 18250ec6c089c0c50cbd9fd87d78e03ff89916df)
1 /*-
2  * Copyright (c) 2016 Stanislav Galabov.
3  * Copyright (c) 2011-2012 Stefan Bethke.
4  * Copyright (c) 2012 Adrian Chadd.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/bus.h>
31 #include <sys/errno.h>
32 #include <sys/kernel.h>
33 #include <sys/lock.h>
34 #include <sys/malloc.h>
35 #include <sys/module.h>
36 #include <sys/mutex.h>
37 #include <sys/socket.h>
38 #include <sys/sockio.h>
39 #include <sys/sysctl.h>
40 #include <sys/systm.h>
41 
42 #include <net/if.h>
43 #include <net/if_var.h>
44 #include <net/ethernet.h>
45 #include <net/if_media.h>
46 #include <net/if_types.h>
47 
48 #include <machine/bus.h>
49 #include <dev/mii/mii.h>
50 #include <dev/mii/miivar.h>
51 #include <dev/mdio/mdio.h>
52 
53 #include <dev/etherswitch/etherswitch.h>
54 #include <dev/etherswitch/mtkswitch/mtkswitchvar.h>
55 
56 #include <dev/ofw/ofw_bus_subr.h>
57 
58 #include "mdio_if.h"
59 #include "miibus_if.h"
60 #include "etherswitch_if.h"
61 
62 #define DEBUG
63 
64 #if defined(DEBUG)
65 static SYSCTL_NODE(_debug, OID_AUTO, mtkswitch, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
66     "mtkswitch");
67 #endif
68 
69 static inline int mtkswitch_portforphy(int phy);
70 static int mtkswitch_ifmedia_upd(if_t ifp);
71 static void mtkswitch_ifmedia_sts(if_t ifp, struct ifmediareq *ifmr);
72 static void mtkswitch_tick(void *arg);
73 
74 static const struct ofw_compat_data compat_data[] = {
75 	{ "ralink,rt3050-esw",		MTK_SWITCH_RT3050 },
76 	{ "ralink,rt3352-esw",		MTK_SWITCH_RT3352 },
77 	{ "ralink,rt5350-esw",		MTK_SWITCH_RT5350 },
78 	{ "mediatek,mt7620-gsw",	MTK_SWITCH_MT7620 },
79 	{ "mediatek,mt7621-gsw",	MTK_SWITCH_MT7621 },
80 	{ "mediatek,mt7628-esw",	MTK_SWITCH_MT7628 },
81 
82 	/* Sentinel */
83 	{ NULL,				MTK_SWITCH_NONE }
84 };
85 
86 static int
mtkswitch_probe(device_t dev)87 mtkswitch_probe(device_t dev)
88 {
89 	struct mtkswitch_softc *sc;
90 	mtk_switch_type switch_type;
91 
92 	if (!ofw_bus_status_okay(dev))
93 		return (ENXIO);
94 
95 	switch_type = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
96 	if (switch_type == MTK_SWITCH_NONE)
97 		return (ENXIO);
98 
99 	sc = device_get_softc(dev);
100 	bzero(sc, sizeof(*sc));
101 	sc->sc_switchtype = switch_type;
102 
103 	device_set_desc(dev, "MTK Switch Driver");
104 
105 	return (0);
106 }
107 
108 static int
mtkswitch_attach_phys(struct mtkswitch_softc * sc)109 mtkswitch_attach_phys(struct mtkswitch_softc *sc)
110 {
111 	int phy, err = 0;
112 	char name[IFNAMSIZ];
113 
114 	/* PHYs need an interface, so we generate a dummy one */
115 	snprintf(name, IFNAMSIZ, "%sport", device_get_nameunit(sc->sc_dev));
116 	for (phy = 0; phy < sc->numphys; phy++) {
117 		if ((sc->phymap & (1u << phy)) == 0) {
118 			sc->ifp[phy] = NULL;
119 			sc->ifname[phy] = NULL;
120 			sc->miibus[phy] = NULL;
121 			continue;
122 		}
123 		sc->ifp[phy] = if_alloc(IFT_ETHER);
124 		sc->ifp[phy]->if_softc = sc;
125 		sc->ifp[phy]->if_flags |= IFF_UP | IFF_BROADCAST |
126 		    IFF_DRV_RUNNING | IFF_SIMPLEX;
127 		sc->ifname[phy] = malloc(strlen(name) + 1, M_DEVBUF, M_WAITOK);
128 		bcopy(name, sc->ifname[phy], strlen(name) + 1);
129 		if_initname(sc->ifp[phy], sc->ifname[phy],
130 		    mtkswitch_portforphy(phy));
131 		err = mii_attach(sc->sc_dev, &sc->miibus[phy], sc->ifp[phy],
132 		    mtkswitch_ifmedia_upd, mtkswitch_ifmedia_sts,
133 		    BMSR_DEFCAPMASK, phy, MII_OFFSET_ANY, 0);
134 		if (err != 0) {
135 			device_printf(sc->sc_dev,
136 			    "attaching PHY %d failed\n",
137 			    phy);
138 		} else {
139 			DPRINTF(sc->sc_dev, "%s attached to pseudo interface "
140 			    "%s\n", device_get_nameunit(sc->miibus[phy]),
141 			    sc->ifp[phy]->if_xname);
142 		}
143 	}
144 	return (err);
145 }
146 
147 static int
mtkswitch_set_vlan_mode(struct mtkswitch_softc * sc,uint32_t mode)148 mtkswitch_set_vlan_mode(struct mtkswitch_softc *sc, uint32_t mode)
149 {
150 
151 	/* Check for invalid modes. */
152 	if ((mode & sc->info.es_vlan_caps) != mode)
153 		return (EINVAL);
154 
155 	sc->vlan_mode = mode;
156 
157 	/* Reset VLANs. */
158 	sc->hal.mtkswitch_vlan_init_hw(sc);
159 
160 	return (0);
161 }
162 
163 static int
mtkswitch_attach(device_t dev)164 mtkswitch_attach(device_t dev)
165 {
166 	struct mtkswitch_softc *sc;
167 	int err = 0;
168 	int port, rid;
169 
170 	sc = device_get_softc(dev);
171 
172 	/* sc->sc_switchtype is already decided in mtkswitch_probe() */
173 	sc->numports = MTKSWITCH_MAX_PORTS;
174 	sc->numphys = MTKSWITCH_MAX_PHYS;
175 	sc->cpuport = MTKSWITCH_CPU_PORT;
176 	sc->sc_dev = dev;
177 
178 	/* Attach switch related functions */
179 	if (sc->sc_switchtype == MTK_SWITCH_NONE) {
180 		device_printf(dev, "Unknown switch type\n");
181 		return (ENXIO);
182 	}
183 
184 	if (sc->sc_switchtype == MTK_SWITCH_MT7620 ||
185 	    sc->sc_switchtype == MTK_SWITCH_MT7621)
186 		mtk_attach_switch_mt7620(sc);
187 	else
188 		mtk_attach_switch_rt3050(sc);
189 
190 	/* Allocate resources */
191 	rid = 0;
192 	sc->sc_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
193 	    RF_ACTIVE);
194 	if (sc->sc_res == NULL) {
195 		device_printf(dev, "could not map memory\n");
196 		return (ENXIO);
197 	}
198 
199 	mtx_init(&sc->sc_mtx, "mtkswitch", NULL, MTX_DEF);
200 
201 	/* Reset the switch */
202 	if (sc->hal.mtkswitch_reset(sc)) {
203 		DPRINTF(dev, "%s: mtkswitch_reset: failed\n", __func__);
204 		return (ENXIO);
205 	}
206 
207 	err = sc->hal.mtkswitch_hw_setup(sc);
208 	DPRINTF(dev, "%s: hw_setup: err=%d\n", __func__, err);
209 	if (err != 0)
210 		return (err);
211 
212 	err = sc->hal.mtkswitch_hw_global_setup(sc);
213 	DPRINTF(dev, "%s: hw_global_setup: err=%d\n", __func__, err);
214 	if (err != 0)
215 		return (err);
216 
217 	/* Initialize the switch ports */
218 	for (port = 0; port < sc->numports; port++) {
219 		sc->hal.mtkswitch_port_init(sc, port);
220 	}
221 
222 	/* Attach the PHYs and complete the bus enumeration */
223 	err = mtkswitch_attach_phys(sc);
224 	DPRINTF(dev, "%s: attach_phys: err=%d\n", __func__, err);
225 	if (err != 0)
226 		return (err);
227 
228 	/* Default to ingress filters off. */
229 	err = mtkswitch_set_vlan_mode(sc, ETHERSWITCH_VLAN_DOT1Q);
230 	DPRINTF(dev, "%s: set_vlan_mode: err=%d\n", __func__, err);
231 	if (err != 0)
232 		return (err);
233 
234 	bus_identify_children(dev);
235 	bus_enumerate_hinted_children(dev);
236 	bus_attach_children(dev);
237 
238 	callout_init_mtx(&sc->callout_tick, &sc->sc_mtx, 0);
239 
240 	MTKSWITCH_LOCK(sc);
241 	mtkswitch_tick(sc);
242 	MTKSWITCH_UNLOCK(sc);
243 
244 	return (0);
245 }
246 
247 static int
mtkswitch_detach(device_t dev)248 mtkswitch_detach(device_t dev)
249 {
250 	struct mtkswitch_softc *sc = device_get_softc(dev);
251 	int phy;
252 
253 	callout_drain(&sc->callout_tick);
254 
255 	for (phy = 0; phy < MTKSWITCH_MAX_PHYS; phy++) {
256 		if (sc->miibus[phy] != NULL)
257 			device_delete_child(dev, sc->miibus[phy]);
258 		if (sc->ifp[phy] != NULL)
259 			if_free(sc->ifp[phy]);
260 		free(sc->ifname[phy], M_DEVBUF);
261 	}
262 
263 	bus_generic_detach(dev);
264 	mtx_destroy(&sc->sc_mtx);
265 
266 	return (0);
267 }
268 
269 /* PHY <-> port mapping is currently 1:1 */
270 static inline int
mtkswitch_portforphy(int phy)271 mtkswitch_portforphy(int phy)
272 {
273 
274 	return (phy);
275 }
276 
277 static inline int
mtkswitch_phyforport(int port)278 mtkswitch_phyforport(int port)
279 {
280 
281 	return (port);
282 }
283 
284 static inline struct mii_data *
mtkswitch_miiforport(struct mtkswitch_softc * sc,int port)285 mtkswitch_miiforport(struct mtkswitch_softc *sc, int port)
286 {
287 	int phy = mtkswitch_phyforport(port);
288 
289 	if (phy < 0 || phy >= MTKSWITCH_MAX_PHYS || sc->miibus[phy] == NULL)
290 		return (NULL);
291 
292 	return (device_get_softc(sc->miibus[phy]));
293 }
294 
295 static inline if_t
mtkswitch_ifpforport(struct mtkswitch_softc * sc,int port)296 mtkswitch_ifpforport(struct mtkswitch_softc *sc, int port)
297 {
298 	int phy = mtkswitch_phyforport(port);
299 
300 	if (phy < 0 || phy >= MTKSWITCH_MAX_PHYS)
301 		return (NULL);
302 
303 	return (sc->ifp[phy]);
304 }
305 
306 /*
307  * Convert port status to ifmedia.
308  */
309 static void
mtkswitch_update_ifmedia(uint32_t portstatus,u_int * media_status,u_int * media_active)310 mtkswitch_update_ifmedia(uint32_t portstatus, u_int *media_status,
311     u_int *media_active)
312 {
313 	*media_active = IFM_ETHER;
314 	*media_status = IFM_AVALID;
315 
316 	if ((portstatus & MTKSWITCH_LINK_UP) != 0)
317 		*media_status |= IFM_ACTIVE;
318 	else {
319 		*media_active |= IFM_NONE;
320 		return;
321 	}
322 
323 	switch (portstatus & MTKSWITCH_SPEED_MASK) {
324 	case MTKSWITCH_SPEED_10:
325 		*media_active |= IFM_10_T;
326 		break;
327 	case MTKSWITCH_SPEED_100:
328 		*media_active |= IFM_100_TX;
329 		break;
330 	case MTKSWITCH_SPEED_1000:
331 		*media_active |= IFM_1000_T;
332 		break;
333 	}
334 
335 	if ((portstatus & MTKSWITCH_DUPLEX) != 0)
336 		*media_active |= IFM_FDX;
337 	else
338 		*media_active |= IFM_HDX;
339 
340 	if ((portstatus & MTKSWITCH_TXFLOW) != 0)
341 		*media_active |= IFM_ETH_TXPAUSE;
342 	if ((portstatus & MTKSWITCH_RXFLOW) != 0)
343 		*media_active |= IFM_ETH_RXPAUSE;
344 }
345 
346 static void
mtkswitch_miipollstat(struct mtkswitch_softc * sc)347 mtkswitch_miipollstat(struct mtkswitch_softc *sc)
348 {
349 	struct mii_data *mii;
350 	struct mii_softc *miisc;
351 	uint32_t portstatus;
352 	int i, port_flap = 0;
353 
354 	MTKSWITCH_LOCK_ASSERT(sc, MA_OWNED);
355 
356 	for (i = 0; i < sc->numphys; i++) {
357 		if (sc->miibus[i] == NULL)
358 			continue;
359 		mii = device_get_softc(sc->miibus[i]);
360 		portstatus = sc->hal.mtkswitch_get_port_status(sc,
361 		    mtkswitch_portforphy(i));
362 
363 		/* If a port has flapped - mark it so we can flush the ATU */
364 		if (((mii->mii_media_status & IFM_ACTIVE) == 0 &&
365 		    (portstatus & MTKSWITCH_LINK_UP) != 0) ||
366 		    ((mii->mii_media_status & IFM_ACTIVE) != 0 &&
367 		    (portstatus & MTKSWITCH_LINK_UP) == 0)) {
368 			port_flap = 1;
369 		}
370 
371 		mtkswitch_update_ifmedia(portstatus, &mii->mii_media_status,
372 		    &mii->mii_media_active);
373 		LIST_FOREACH(miisc, &mii->mii_phys, mii_list) {
374 			if (IFM_INST(mii->mii_media.ifm_cur->ifm_media) !=
375 			    miisc->mii_inst)
376 				continue;
377 			mii_phy_update(miisc, MII_POLLSTAT);
378 		}
379 	}
380 
381 	if (port_flap)
382 		sc->hal.mtkswitch_atu_flush(sc);
383 }
384 
385 static void
mtkswitch_tick(void * arg)386 mtkswitch_tick(void *arg)
387 {
388 	struct mtkswitch_softc *sc = arg;
389 
390 	mtkswitch_miipollstat(sc);
391 	callout_reset(&sc->callout_tick, hz, mtkswitch_tick, sc);
392 }
393 
394 static void
mtkswitch_lock(device_t dev)395 mtkswitch_lock(device_t dev)
396 {
397         struct mtkswitch_softc *sc = device_get_softc(dev);
398 
399 	MTKSWITCH_LOCK_ASSERT(sc, MA_NOTOWNED);
400 	MTKSWITCH_LOCK(sc);
401 }
402 
403 static void
mtkswitch_unlock(device_t dev)404 mtkswitch_unlock(device_t dev)
405 {
406 	struct mtkswitch_softc *sc = device_get_softc(dev);
407 
408 	MTKSWITCH_LOCK_ASSERT(sc, MA_OWNED);
409 	MTKSWITCH_UNLOCK(sc);
410 }
411 
412 static etherswitch_info_t *
mtkswitch_getinfo(device_t dev)413 mtkswitch_getinfo(device_t dev)
414 {
415 	struct mtkswitch_softc *sc = device_get_softc(dev);
416 
417 	return (&sc->info);
418 }
419 
420 static inline int
mtkswitch_is_cpuport(struct mtkswitch_softc * sc,int port)421 mtkswitch_is_cpuport(struct mtkswitch_softc *sc, int port)
422 {
423 
424 	return (sc->cpuport == port);
425 }
426 
427 static int
mtkswitch_getport(device_t dev,etherswitch_port_t * p)428 mtkswitch_getport(device_t dev, etherswitch_port_t *p)
429 {
430 	struct mtkswitch_softc *sc;
431 	struct mii_data *mii;
432 	struct ifmediareq *ifmr;
433 	int err;
434 
435 	sc = device_get_softc(dev);
436 	if (p->es_port < 0 || p->es_port > sc->info.es_nports)
437 		return (ENXIO);
438 
439 	err = sc->hal.mtkswitch_port_vlan_get(sc, p);
440 	if (err != 0)
441 		return (err);
442 
443 	mii = mtkswitch_miiforport(sc, p->es_port);
444 	if (mtkswitch_is_cpuport(sc, p->es_port)) {
445 		/* fill in fixed values for CPU port */
446 		/* XXX is this valid in all cases? */
447 		p->es_flags |= ETHERSWITCH_PORT_CPU;
448 		ifmr = &p->es_ifmr;
449 		ifmr->ifm_count = 0;
450 		ifmr->ifm_current = ifmr->ifm_active =
451 		    IFM_ETHER | IFM_1000_T | IFM_FDX;
452 		ifmr->ifm_mask = 0;
453 		ifmr->ifm_status = IFM_ACTIVE | IFM_AVALID;
454 	} else if (mii != NULL) {
455 		err = ifmedia_ioctl(mii->mii_ifp, &p->es_ifr,
456 		    &mii->mii_media, SIOCGIFMEDIA);
457 		if (err)
458 			return (err);
459 	} else {
460 		ifmr = &p->es_ifmr;
461 		ifmr->ifm_count = 0;
462 		ifmr->ifm_current = ifmr->ifm_active = IFM_NONE;
463 		ifmr->ifm_mask = 0;
464 		ifmr->ifm_status = 0;
465 	}
466 	return (0);
467 }
468 
469 static int
mtkswitch_setport(device_t dev,etherswitch_port_t * p)470 mtkswitch_setport(device_t dev, etherswitch_port_t *p)
471 {
472 	int err;
473 	struct mtkswitch_softc *sc;
474 	struct ifmedia *ifm;
475 	struct mii_data *mii;
476 	if_t ifp;
477 
478 	sc = device_get_softc(dev);
479 	if (p->es_port < 0 || p->es_port > sc->info.es_nports)
480 		return (ENXIO);
481 
482 	/* Port flags. */
483 	if (sc->vlan_mode == ETHERSWITCH_VLAN_DOT1Q) {
484 		err = sc->hal.mtkswitch_port_vlan_setup(sc, p);
485 		if (err)
486 			return (err);
487 	}
488 
489 	/* Do not allow media changes on CPU port. */
490 	if (mtkswitch_is_cpuport(sc, p->es_port))
491 		return (0);
492 
493 	mii = mtkswitch_miiforport(sc, p->es_port);
494 	if (mii == NULL)
495 		return (ENXIO);
496 
497 	ifp = mtkswitch_ifpforport(sc, p->es_port);
498 
499 	ifm = &mii->mii_media;
500 	return (ifmedia_ioctl(ifp, &p->es_ifr, ifm, SIOCSIFMEDIA));
501 }
502 
503 static void
mtkswitch_statchg(device_t dev)504 mtkswitch_statchg(device_t dev)
505 {
506 
507 	DPRINTF(dev, "%s\n", __func__);
508 }
509 
510 static int
mtkswitch_ifmedia_upd(if_t ifp)511 mtkswitch_ifmedia_upd(if_t ifp)
512 {
513 	struct mtkswitch_softc *sc = if_getsoftc(ifp);
514 	struct mii_data *mii = mtkswitch_miiforport(sc, if_getdunit(ifp));
515 
516 	if (mii == NULL)
517 		return (ENXIO);
518 	mii_mediachg(mii);
519 	return (0);
520 }
521 
522 static void
mtkswitch_ifmedia_sts(if_t ifp,struct ifmediareq * ifmr)523 mtkswitch_ifmedia_sts(if_t ifp, struct ifmediareq *ifmr)
524 {
525 	struct mtkswitch_softc *sc = if_getsoftc(ifp);
526 	struct mii_data *mii = mtkswitch_miiforport(sc, if_getdunit(ifp));
527 
528 	DPRINTF(sc->sc_dev, "%s\n", __func__);
529 
530 	if (mii == NULL)
531 		return;
532 	mii_pollstat(mii);
533 	ifmr->ifm_active = mii->mii_media_active;
534 	ifmr->ifm_status = mii->mii_media_status;
535 }
536 
537 static int
mtkswitch_getconf(device_t dev,etherswitch_conf_t * conf)538 mtkswitch_getconf(device_t dev, etherswitch_conf_t *conf)
539 {
540 	struct mtkswitch_softc *sc;
541 
542 	sc = device_get_softc(dev);
543 
544 	/* Return the VLAN mode. */
545 	conf->cmd = ETHERSWITCH_CONF_VLAN_MODE;
546 	conf->vlan_mode = sc->vlan_mode;
547 
548 	return (0);
549 }
550 
551 static int
mtkswitch_setconf(device_t dev,etherswitch_conf_t * conf)552 mtkswitch_setconf(device_t dev, etherswitch_conf_t *conf)
553 {
554 	struct mtkswitch_softc *sc;
555 	int err;
556 
557 	sc = device_get_softc(dev);
558 
559 	/* Set the VLAN mode. */
560 	if (conf->cmd & ETHERSWITCH_CONF_VLAN_MODE) {
561 		err = mtkswitch_set_vlan_mode(sc, conf->vlan_mode);
562 		if (err != 0)
563 			return (err);
564 	}
565 
566 	return (0);
567 }
568 
569 static int
mtkswitch_getvgroup(device_t dev,etherswitch_vlangroup_t * e)570 mtkswitch_getvgroup(device_t dev, etherswitch_vlangroup_t *e)
571 {
572         struct mtkswitch_softc *sc = device_get_softc(dev);
573 
574         return (sc->hal.mtkswitch_vlan_getvgroup(sc, e));
575 }
576 
577 static int
mtkswitch_setvgroup(device_t dev,etherswitch_vlangroup_t * e)578 mtkswitch_setvgroup(device_t dev, etherswitch_vlangroup_t *e)
579 {
580 	struct mtkswitch_softc *sc = device_get_softc(dev);
581 
582 	return (sc->hal.mtkswitch_vlan_setvgroup(sc, e));
583 }
584 
585 static int
mtkswitch_readphy(device_t dev,int phy,int reg)586 mtkswitch_readphy(device_t dev, int phy, int reg)
587 {
588 	struct mtkswitch_softc *sc = device_get_softc(dev);
589 
590 	return (sc->hal.mtkswitch_phy_read(dev, phy, reg));
591 }
592 
593 static int
mtkswitch_writephy(device_t dev,int phy,int reg,int val)594 mtkswitch_writephy(device_t dev, int phy, int reg, int val)
595 {
596 	struct mtkswitch_softc *sc = device_get_softc(dev);
597 
598 	return (sc->hal.mtkswitch_phy_write(dev, phy, reg, val));
599 }
600 
601 static int
mtkswitch_readreg(device_t dev,int addr)602 mtkswitch_readreg(device_t dev, int addr)
603 {
604 	struct mtkswitch_softc *sc = device_get_softc(dev);
605 
606 	return (sc->hal.mtkswitch_reg_read(dev, addr));
607 }
608 
609 static int
mtkswitch_writereg(device_t dev,int addr,int value)610 mtkswitch_writereg(device_t dev, int addr, int value)
611 {
612 	struct mtkswitch_softc *sc = device_get_softc(dev);
613 
614 	return (sc->hal.mtkswitch_reg_write(dev, addr, value));
615 }
616 
617 static device_method_t mtkswitch_methods[] = {
618 	/* Device interface */
619 	DEVMETHOD(device_probe,		mtkswitch_probe),
620 	DEVMETHOD(device_attach,	mtkswitch_attach),
621 	DEVMETHOD(device_detach,	mtkswitch_detach),
622 
623 	/* bus interface */
624 	DEVMETHOD(bus_add_child,	device_add_child_ordered),
625 
626 	/* MII interface */
627 	DEVMETHOD(miibus_readreg,	mtkswitch_readphy),
628 	DEVMETHOD(miibus_writereg,	mtkswitch_writephy),
629 	DEVMETHOD(miibus_statchg,	mtkswitch_statchg),
630 
631 	/* MDIO interface */
632 	DEVMETHOD(mdio_readreg,		mtkswitch_readphy),
633 	DEVMETHOD(mdio_writereg,	mtkswitch_writephy),
634 
635 	/* ehterswitch interface */
636 	DEVMETHOD(etherswitch_lock,	mtkswitch_lock),
637 	DEVMETHOD(etherswitch_unlock,	mtkswitch_unlock),
638 	DEVMETHOD(etherswitch_getinfo,	mtkswitch_getinfo),
639 	DEVMETHOD(etherswitch_readreg,	mtkswitch_readreg),
640 	DEVMETHOD(etherswitch_writereg,	mtkswitch_writereg),
641 	DEVMETHOD(etherswitch_readphyreg,	mtkswitch_readphy),
642 	DEVMETHOD(etherswitch_writephyreg,	mtkswitch_writephy),
643 	DEVMETHOD(etherswitch_getport,	mtkswitch_getport),
644 	DEVMETHOD(etherswitch_setport,	mtkswitch_setport),
645 	DEVMETHOD(etherswitch_getvgroup,	mtkswitch_getvgroup),
646 	DEVMETHOD(etherswitch_setvgroup,	mtkswitch_setvgroup),
647 	DEVMETHOD(etherswitch_getconf,	mtkswitch_getconf),
648 	DEVMETHOD(etherswitch_setconf,	mtkswitch_setconf),
649 
650 	DEVMETHOD_END
651 };
652 
653 DEFINE_CLASS_0(mtkswitch, mtkswitch_driver, mtkswitch_methods,
654     sizeof(struct mtkswitch_softc));
655 
656 DRIVER_MODULE(mtkswitch, simplebus, mtkswitch_driver, 0, 0);
657 DRIVER_MODULE(miibus, mtkswitch, miibus_driver, 0, 0);
658 DRIVER_MODULE(mdio, mtkswitch, mdio_driver, 0, 0);
659 DRIVER_MODULE(etherswitch, mtkswitch, etherswitch_driver, 0, 0);
660 MODULE_VERSION(mtkswitch, 1);
661 MODULE_DEPEND(mtkswitch, miibus, 1, 1, 1);
662 MODULE_DEPEND(mtkswitch, etherswitch, 1, 1, 1);
663