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_copy(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 		if (sc->ifp[phy] == NULL) {
125 			device_printf(sc->sc_dev, "couldn't allocate ifnet structure\n");
126 			err = ENOMEM;
127 			break;
128 		}
129 
130 		sc->ifp[phy]->if_softc = sc;
131 		sc->ifp[phy]->if_flags |= IFF_UP | IFF_BROADCAST |
132 		    IFF_DRV_RUNNING | IFF_SIMPLEX;
133 		sc->ifname[phy] = malloc(strlen(name) + 1, M_DEVBUF, M_WAITOK);
134 		bcopy(name, sc->ifname[phy], strlen(name) + 1);
135 		if_initname(sc->ifp[phy], sc->ifname[phy],
136 		    mtkswitch_portforphy(phy));
137 		err = mii_attach(sc->sc_dev, &sc->miibus[phy], sc->ifp[phy],
138 		    mtkswitch_ifmedia_upd, mtkswitch_ifmedia_sts,
139 		    BMSR_DEFCAPMASK, phy, MII_OFFSET_ANY, 0);
140 		if (err != 0) {
141 			device_printf(sc->sc_dev,
142 			    "attaching PHY %d failed\n",
143 			    phy);
144 		} else {
145 			DPRINTF(sc->sc_dev, "%s attached to pseudo interface "
146 			    "%s\n", device_get_nameunit(sc->miibus[phy]),
147 			    sc->ifp[phy]->if_xname);
148 		}
149 	}
150 	return (err);
151 }
152 
153 static int
mtkswitch_set_vlan_mode(struct mtkswitch_softc * sc,uint32_t mode)154 mtkswitch_set_vlan_mode(struct mtkswitch_softc *sc, uint32_t mode)
155 {
156 
157 	/* Check for invalid modes. */
158 	if ((mode & sc->info.es_vlan_caps) != mode)
159 		return (EINVAL);
160 
161 	sc->vlan_mode = mode;
162 
163 	/* Reset VLANs. */
164 	sc->hal.mtkswitch_vlan_init_hw(sc);
165 
166 	return (0);
167 }
168 
169 static int
mtkswitch_attach(device_t dev)170 mtkswitch_attach(device_t dev)
171 {
172 	struct mtkswitch_softc *sc;
173 	int err = 0;
174 	int port, rid;
175 
176 	sc = device_get_softc(dev);
177 
178 	/* sc->sc_switchtype is already decided in mtkswitch_probe() */
179 	sc->numports = MTKSWITCH_MAX_PORTS;
180 	sc->numphys = MTKSWITCH_MAX_PHYS;
181 	sc->cpuport = MTKSWITCH_CPU_PORT;
182 	sc->sc_dev = dev;
183 
184 	/* Attach switch related functions */
185 	if (sc->sc_switchtype == MTK_SWITCH_NONE) {
186 		device_printf(dev, "Unknown switch type\n");
187 		return (ENXIO);
188 	}
189 
190 	if (sc->sc_switchtype == MTK_SWITCH_MT7620 ||
191 	    sc->sc_switchtype == MTK_SWITCH_MT7621)
192 		mtk_attach_switch_mt7620(sc);
193 	else
194 		mtk_attach_switch_rt3050(sc);
195 
196 	/* Allocate resources */
197 	rid = 0;
198 	sc->sc_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
199 	    RF_ACTIVE);
200 	if (sc->sc_res == NULL) {
201 		device_printf(dev, "could not map memory\n");
202 		return (ENXIO);
203 	}
204 
205 	mtx_init(&sc->sc_mtx, "mtkswitch", NULL, MTX_DEF);
206 
207 	/* Reset the switch */
208 	if (sc->hal.mtkswitch_reset(sc)) {
209 		DPRINTF(dev, "%s: mtkswitch_reset: failed\n", __func__);
210 		return (ENXIO);
211 	}
212 
213 	err = sc->hal.mtkswitch_hw_setup(sc);
214 	DPRINTF(dev, "%s: hw_setup: err=%d\n", __func__, err);
215 	if (err != 0)
216 		return (err);
217 
218 	err = sc->hal.mtkswitch_hw_global_setup(sc);
219 	DPRINTF(dev, "%s: hw_global_setup: err=%d\n", __func__, err);
220 	if (err != 0)
221 		return (err);
222 
223 	/* Initialize the switch ports */
224 	for (port = 0; port < sc->numports; port++) {
225 		sc->hal.mtkswitch_port_init(sc, port);
226 	}
227 
228 	/* Attach the PHYs and complete the bus enumeration */
229 	err = mtkswitch_attach_phys(sc);
230 	DPRINTF(dev, "%s: attach_phys: err=%d\n", __func__, err);
231 	if (err != 0)
232 		return (err);
233 
234 	/* Default to ingress filters off. */
235 	err = mtkswitch_set_vlan_mode(sc, ETHERSWITCH_VLAN_DOT1Q);
236 	DPRINTF(dev, "%s: set_vlan_mode: err=%d\n", __func__, err);
237 	if (err != 0)
238 		return (err);
239 
240 	bus_generic_probe(dev);
241 	bus_enumerate_hinted_children(dev);
242 	err = bus_generic_attach(dev);
243 	DPRINTF(dev, "%s: bus_generic_attach: err=%d\n", __func__, err);
244 	if (err != 0)
245 		return (err);
246 
247 	callout_init_mtx(&sc->callout_tick, &sc->sc_mtx, 0);
248 
249 	MTKSWITCH_LOCK(sc);
250 	mtkswitch_tick(sc);
251 	MTKSWITCH_UNLOCK(sc);
252 
253 	return (0);
254 }
255 
256 static int
mtkswitch_detach(device_t dev)257 mtkswitch_detach(device_t dev)
258 {
259 	struct mtkswitch_softc *sc = device_get_softc(dev);
260 	int phy;
261 
262 	callout_drain(&sc->callout_tick);
263 
264 	for (phy = 0; phy < MTKSWITCH_MAX_PHYS; phy++) {
265 		if (sc->miibus[phy] != NULL)
266 			device_delete_child(dev, sc->miibus[phy]);
267 		if (sc->ifp[phy] != NULL)
268 			if_free(sc->ifp[phy]);
269 		free(sc->ifname[phy], M_DEVBUF);
270 	}
271 
272 	bus_generic_detach(dev);
273 	mtx_destroy(&sc->sc_mtx);
274 
275 	return (0);
276 }
277 
278 /* PHY <-> port mapping is currently 1:1 */
279 static inline int
mtkswitch_portforphy(int phy)280 mtkswitch_portforphy(int phy)
281 {
282 
283 	return (phy);
284 }
285 
286 static inline int
mtkswitch_phyforport(int port)287 mtkswitch_phyforport(int port)
288 {
289 
290 	return (port);
291 }
292 
293 static inline struct mii_data *
mtkswitch_miiforport(struct mtkswitch_softc * sc,int port)294 mtkswitch_miiforport(struct mtkswitch_softc *sc, int port)
295 {
296 	int phy = mtkswitch_phyforport(port);
297 
298 	if (phy < 0 || phy >= MTKSWITCH_MAX_PHYS || sc->miibus[phy] == NULL)
299 		return (NULL);
300 
301 	return (device_get_softc(sc->miibus[phy]));
302 }
303 
304 static inline if_t
mtkswitch_ifpforport(struct mtkswitch_softc * sc,int port)305 mtkswitch_ifpforport(struct mtkswitch_softc *sc, int port)
306 {
307 	int phy = mtkswitch_phyforport(port);
308 
309 	if (phy < 0 || phy >= MTKSWITCH_MAX_PHYS)
310 		return (NULL);
311 
312 	return (sc->ifp[phy]);
313 }
314 
315 /*
316  * Convert port status to ifmedia.
317  */
318 static void
mtkswitch_update_ifmedia(uint32_t portstatus,u_int * media_status,u_int * media_active)319 mtkswitch_update_ifmedia(uint32_t portstatus, u_int *media_status,
320     u_int *media_active)
321 {
322 	*media_active = IFM_ETHER;
323 	*media_status = IFM_AVALID;
324 
325 	if ((portstatus & MTKSWITCH_LINK_UP) != 0)
326 		*media_status |= IFM_ACTIVE;
327 	else {
328 		*media_active |= IFM_NONE;
329 		return;
330 	}
331 
332 	switch (portstatus & MTKSWITCH_SPEED_MASK) {
333 	case MTKSWITCH_SPEED_10:
334 		*media_active |= IFM_10_T;
335 		break;
336 	case MTKSWITCH_SPEED_100:
337 		*media_active |= IFM_100_TX;
338 		break;
339 	case MTKSWITCH_SPEED_1000:
340 		*media_active |= IFM_1000_T;
341 		break;
342 	}
343 
344 	if ((portstatus & MTKSWITCH_DUPLEX) != 0)
345 		*media_active |= IFM_FDX;
346 	else
347 		*media_active |= IFM_HDX;
348 
349 	if ((portstatus & MTKSWITCH_TXFLOW) != 0)
350 		*media_active |= IFM_ETH_TXPAUSE;
351 	if ((portstatus & MTKSWITCH_RXFLOW) != 0)
352 		*media_active |= IFM_ETH_RXPAUSE;
353 }
354 
355 static void
mtkswitch_miipollstat(struct mtkswitch_softc * sc)356 mtkswitch_miipollstat(struct mtkswitch_softc *sc)
357 {
358 	struct mii_data *mii;
359 	struct mii_softc *miisc;
360 	uint32_t portstatus;
361 	int i, port_flap = 0;
362 
363 	MTKSWITCH_LOCK_ASSERT(sc, MA_OWNED);
364 
365 	for (i = 0; i < sc->numphys; i++) {
366 		if (sc->miibus[i] == NULL)
367 			continue;
368 		mii = device_get_softc(sc->miibus[i]);
369 		portstatus = sc->hal.mtkswitch_get_port_status(sc,
370 		    mtkswitch_portforphy(i));
371 
372 		/* If a port has flapped - mark it so we can flush the ATU */
373 		if (((mii->mii_media_status & IFM_ACTIVE) == 0 &&
374 		    (portstatus & MTKSWITCH_LINK_UP) != 0) ||
375 		    ((mii->mii_media_status & IFM_ACTIVE) != 0 &&
376 		    (portstatus & MTKSWITCH_LINK_UP) == 0)) {
377 			port_flap = 1;
378 		}
379 
380 		mtkswitch_update_ifmedia(portstatus, &mii->mii_media_status,
381 		    &mii->mii_media_active);
382 		LIST_FOREACH(miisc, &mii->mii_phys, mii_list) {
383 			if (IFM_INST(mii->mii_media.ifm_cur->ifm_media) !=
384 			    miisc->mii_inst)
385 				continue;
386 			mii_phy_update(miisc, MII_POLLSTAT);
387 		}
388 	}
389 
390 	if (port_flap)
391 		sc->hal.mtkswitch_atu_flush(sc);
392 }
393 
394 static void
mtkswitch_tick(void * arg)395 mtkswitch_tick(void *arg)
396 {
397 	struct mtkswitch_softc *sc = arg;
398 
399 	mtkswitch_miipollstat(sc);
400 	callout_reset(&sc->callout_tick, hz, mtkswitch_tick, sc);
401 }
402 
403 static void
mtkswitch_lock(device_t dev)404 mtkswitch_lock(device_t dev)
405 {
406         struct mtkswitch_softc *sc = device_get_softc(dev);
407 
408 	MTKSWITCH_LOCK_ASSERT(sc, MA_NOTOWNED);
409 	MTKSWITCH_LOCK(sc);
410 }
411 
412 static void
mtkswitch_unlock(device_t dev)413 mtkswitch_unlock(device_t dev)
414 {
415 	struct mtkswitch_softc *sc = device_get_softc(dev);
416 
417 	MTKSWITCH_LOCK_ASSERT(sc, MA_OWNED);
418 	MTKSWITCH_UNLOCK(sc);
419 }
420 
421 static etherswitch_info_t *
mtkswitch_getinfo(device_t dev)422 mtkswitch_getinfo(device_t dev)
423 {
424 	struct mtkswitch_softc *sc = device_get_softc(dev);
425 
426 	return (&sc->info);
427 }
428 
429 static inline int
mtkswitch_is_cpuport(struct mtkswitch_softc * sc,int port)430 mtkswitch_is_cpuport(struct mtkswitch_softc *sc, int port)
431 {
432 
433 	return (sc->cpuport == port);
434 }
435 
436 static int
mtkswitch_getport(device_t dev,etherswitch_port_t * p)437 mtkswitch_getport(device_t dev, etherswitch_port_t *p)
438 {
439 	struct mtkswitch_softc *sc;
440 	struct mii_data *mii;
441 	struct ifmediareq *ifmr;
442 	int err;
443 
444 	sc = device_get_softc(dev);
445 	if (p->es_port < 0 || p->es_port > sc->info.es_nports)
446 		return (ENXIO);
447 
448 	err = sc->hal.mtkswitch_port_vlan_get(sc, p);
449 	if (err != 0)
450 		return (err);
451 
452 	mii = mtkswitch_miiforport(sc, p->es_port);
453 	if (mtkswitch_is_cpuport(sc, p->es_port)) {
454 		/* fill in fixed values for CPU port */
455 		/* XXX is this valid in all cases? */
456 		p->es_flags |= ETHERSWITCH_PORT_CPU;
457 		ifmr = &p->es_ifmr;
458 		ifmr->ifm_count = 0;
459 		ifmr->ifm_current = ifmr->ifm_active =
460 		    IFM_ETHER | IFM_1000_T | IFM_FDX;
461 		ifmr->ifm_mask = 0;
462 		ifmr->ifm_status = IFM_ACTIVE | IFM_AVALID;
463 	} else if (mii != NULL) {
464 		err = ifmedia_ioctl(mii->mii_ifp, &p->es_ifr,
465 		    &mii->mii_media, SIOCGIFMEDIA);
466 		if (err)
467 			return (err);
468 	} else {
469 		ifmr = &p->es_ifmr;
470 		ifmr->ifm_count = 0;
471 		ifmr->ifm_current = ifmr->ifm_active = IFM_NONE;
472 		ifmr->ifm_mask = 0;
473 		ifmr->ifm_status = 0;
474 	}
475 	return (0);
476 }
477 
478 static int
mtkswitch_setport(device_t dev,etherswitch_port_t * p)479 mtkswitch_setport(device_t dev, etherswitch_port_t *p)
480 {
481 	int err;
482 	struct mtkswitch_softc *sc;
483 	struct ifmedia *ifm;
484 	struct mii_data *mii;
485 	if_t ifp;
486 
487 	sc = device_get_softc(dev);
488 	if (p->es_port < 0 || p->es_port > sc->info.es_nports)
489 		return (ENXIO);
490 
491 	/* Port flags. */
492 	if (sc->vlan_mode == ETHERSWITCH_VLAN_DOT1Q) {
493 		err = sc->hal.mtkswitch_port_vlan_setup(sc, p);
494 		if (err)
495 			return (err);
496 	}
497 
498 	/* Do not allow media changes on CPU port. */
499 	if (mtkswitch_is_cpuport(sc, p->es_port))
500 		return (0);
501 
502 	mii = mtkswitch_miiforport(sc, p->es_port);
503 	if (mii == NULL)
504 		return (ENXIO);
505 
506 	ifp = mtkswitch_ifpforport(sc, p->es_port);
507 
508 	ifm = &mii->mii_media;
509 	return (ifmedia_ioctl(ifp, &p->es_ifr, ifm, SIOCSIFMEDIA));
510 }
511 
512 static void
mtkswitch_statchg(device_t dev)513 mtkswitch_statchg(device_t dev)
514 {
515 
516 	DPRINTF(dev, "%s\n", __func__);
517 }
518 
519 static int
mtkswitch_ifmedia_upd(if_t ifp)520 mtkswitch_ifmedia_upd(if_t ifp)
521 {
522 	struct mtkswitch_softc *sc = if_getsoftc(ifp);
523 	struct mii_data *mii = mtkswitch_miiforport(sc, if_getdunit(ifp));
524 
525 	if (mii == NULL)
526 		return (ENXIO);
527 	mii_mediachg(mii);
528 	return (0);
529 }
530 
531 static void
mtkswitch_ifmedia_sts(if_t ifp,struct ifmediareq * ifmr)532 mtkswitch_ifmedia_sts(if_t ifp, struct ifmediareq *ifmr)
533 {
534 	struct mtkswitch_softc *sc = if_getsoftc(ifp);
535 	struct mii_data *mii = mtkswitch_miiforport(sc, if_getdunit(ifp));
536 
537 	DPRINTF(sc->sc_dev, "%s\n", __func__);
538 
539 	if (mii == NULL)
540 		return;
541 	mii_pollstat(mii);
542 	ifmr->ifm_active = mii->mii_media_active;
543 	ifmr->ifm_status = mii->mii_media_status;
544 }
545 
546 static int
mtkswitch_getconf(device_t dev,etherswitch_conf_t * conf)547 mtkswitch_getconf(device_t dev, etherswitch_conf_t *conf)
548 {
549 	struct mtkswitch_softc *sc;
550 
551 	sc = device_get_softc(dev);
552 
553 	/* Return the VLAN mode. */
554 	conf->cmd = ETHERSWITCH_CONF_VLAN_MODE;
555 	conf->vlan_mode = sc->vlan_mode;
556 
557 	return (0);
558 }
559 
560 static int
mtkswitch_setconf(device_t dev,etherswitch_conf_t * conf)561 mtkswitch_setconf(device_t dev, etherswitch_conf_t *conf)
562 {
563 	struct mtkswitch_softc *sc;
564 	int err;
565 
566 	sc = device_get_softc(dev);
567 
568 	/* Set the VLAN mode. */
569 	if (conf->cmd & ETHERSWITCH_CONF_VLAN_MODE) {
570 		err = mtkswitch_set_vlan_mode(sc, conf->vlan_mode);
571 		if (err != 0)
572 			return (err);
573 	}
574 
575 	return (0);
576 }
577 
578 static int
mtkswitch_getvgroup(device_t dev,etherswitch_vlangroup_t * e)579 mtkswitch_getvgroup(device_t dev, etherswitch_vlangroup_t *e)
580 {
581         struct mtkswitch_softc *sc = device_get_softc(dev);
582 
583         return (sc->hal.mtkswitch_vlan_getvgroup(sc, e));
584 }
585 
586 static int
mtkswitch_setvgroup(device_t dev,etherswitch_vlangroup_t * e)587 mtkswitch_setvgroup(device_t dev, etherswitch_vlangroup_t *e)
588 {
589 	struct mtkswitch_softc *sc = device_get_softc(dev);
590 
591 	return (sc->hal.mtkswitch_vlan_setvgroup(sc, e));
592 }
593 
594 static int
mtkswitch_readphy(device_t dev,int phy,int reg)595 mtkswitch_readphy(device_t dev, int phy, int reg)
596 {
597 	struct mtkswitch_softc *sc = device_get_softc(dev);
598 
599 	return (sc->hal.mtkswitch_phy_read(dev, phy, reg));
600 }
601 
602 static int
mtkswitch_writephy(device_t dev,int phy,int reg,int val)603 mtkswitch_writephy(device_t dev, int phy, int reg, int val)
604 {
605 	struct mtkswitch_softc *sc = device_get_softc(dev);
606 
607 	return (sc->hal.mtkswitch_phy_write(dev, phy, reg, val));
608 }
609 
610 static int
mtkswitch_readreg(device_t dev,int addr)611 mtkswitch_readreg(device_t dev, int addr)
612 {
613 	struct mtkswitch_softc *sc = device_get_softc(dev);
614 
615 	return (sc->hal.mtkswitch_reg_read(dev, addr));
616 }
617 
618 static int
mtkswitch_writereg(device_t dev,int addr,int value)619 mtkswitch_writereg(device_t dev, int addr, int value)
620 {
621 	struct mtkswitch_softc *sc = device_get_softc(dev);
622 
623 	return (sc->hal.mtkswitch_reg_write(dev, addr, value));
624 }
625 
626 static device_method_t mtkswitch_methods[] = {
627 	/* Device interface */
628 	DEVMETHOD(device_probe,		mtkswitch_probe),
629 	DEVMETHOD(device_attach,	mtkswitch_attach),
630 	DEVMETHOD(device_detach,	mtkswitch_detach),
631 
632 	/* bus interface */
633 	DEVMETHOD(bus_add_child,	device_add_child_ordered),
634 
635 	/* MII interface */
636 	DEVMETHOD(miibus_readreg,	mtkswitch_readphy),
637 	DEVMETHOD(miibus_writereg,	mtkswitch_writephy),
638 	DEVMETHOD(miibus_statchg,	mtkswitch_statchg),
639 
640 	/* MDIO interface */
641 	DEVMETHOD(mdio_readreg,		mtkswitch_readphy),
642 	DEVMETHOD(mdio_writereg,	mtkswitch_writephy),
643 
644 	/* ehterswitch interface */
645 	DEVMETHOD(etherswitch_lock,	mtkswitch_lock),
646 	DEVMETHOD(etherswitch_unlock,	mtkswitch_unlock),
647 	DEVMETHOD(etherswitch_getinfo,	mtkswitch_getinfo),
648 	DEVMETHOD(etherswitch_readreg,	mtkswitch_readreg),
649 	DEVMETHOD(etherswitch_writereg,	mtkswitch_writereg),
650 	DEVMETHOD(etherswitch_readphyreg,	mtkswitch_readphy),
651 	DEVMETHOD(etherswitch_writephyreg,	mtkswitch_writephy),
652 	DEVMETHOD(etherswitch_getport,	mtkswitch_getport),
653 	DEVMETHOD(etherswitch_setport,	mtkswitch_setport),
654 	DEVMETHOD(etherswitch_getvgroup,	mtkswitch_getvgroup),
655 	DEVMETHOD(etherswitch_setvgroup,	mtkswitch_setvgroup),
656 	DEVMETHOD(etherswitch_getconf,	mtkswitch_getconf),
657 	DEVMETHOD(etherswitch_setconf,	mtkswitch_setconf),
658 
659 	DEVMETHOD_END
660 };
661 
662 DEFINE_CLASS_0(mtkswitch, mtkswitch_driver, mtkswitch_methods,
663     sizeof(struct mtkswitch_softc));
664 
665 DRIVER_MODULE(mtkswitch, simplebus, mtkswitch_driver, 0, 0);
666 DRIVER_MODULE(miibus, mtkswitch, miibus_driver, 0, 0);
667 DRIVER_MODULE(mdio, mtkswitch, mdio_driver, 0, 0);
668 DRIVER_MODULE(etherswitch, mtkswitch, etherswitch_driver, 0, 0);
669 MODULE_VERSION(mtkswitch, 1);
670 MODULE_DEPEND(mtkswitch, miibus, 1, 1, 1);
671 MODULE_DEPEND(mtkswitch, etherswitch, 1, 1, 1);
672