xref: /freebsd/sys/dev/etherswitch/micrel/ksz8995ma.c (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1 /*-
2  * Copyright (c) 2016 Hiroki Mori
3  * Copyright (c) 2013 Luiz Otavio O Souza.
4  * Copyright (c) 2011-2012 Stefan Bethke.
5  * Copyright (c) 2012 Adrian Chadd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 /*
31  * This is Micrel KSZ8995MA driver code. KSZ8995MA use SPI bus on control.
32  * This code development on @SRCHACK's ksz8995ma board and FON2100 with
33  * gpiospi.
34  * etherswitchcfg command port option support addtag, ingress, striptag,
35  * dropuntagged.
36  */
37 
38 #include <sys/param.h>
39 #include <sys/bus.h>
40 #include <sys/errno.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
45 #include <sys/mutex.h>
46 #include <sys/socket.h>
47 #include <sys/sockio.h>
48 #include <sys/sysctl.h>
49 #include <sys/systm.h>
50 
51 #include <net/if.h>
52 #include <net/if_var.h>
53 #include <net/ethernet.h>
54 #include <net/if_media.h>
55 #include <net/if_types.h>
56 
57 #include <machine/bus.h>
58 #include <dev/mii/mii.h>
59 #include <dev/mii/miivar.h>
60 
61 #include <dev/etherswitch/etherswitch.h>
62 
63 #include <dev/spibus/spi.h>
64 
65 #include "spibus_if.h"
66 #include "miibus_if.h"
67 #include "etherswitch_if.h"
68 
69 #define	KSZ8995MA_SPI_READ		0x03
70 #define	KSZ8995MA_SPI_WRITE		0x02
71 
72 #define	KSZ8995MA_CID0			0x00
73 #define	KSZ8995MA_CID1			0x01
74 
75 #define	KSZ8995MA_GC0			0x02
76 #define	KSZ8995MA_GC1			0x03
77 #define	KSZ8995MA_GC2			0x04
78 #define	KSZ8995MA_GC3			0x05
79 
80 #define	KSZ8995MA_PORT_SIZE		0x10
81 
82 #define	KSZ8995MA_PC0_BASE		0x10
83 #define	KSZ8995MA_PC1_BASE		0x11
84 #define	KSZ8995MA_PC2_BASE		0x12
85 #define	KSZ8995MA_PC3_BASE		0x13
86 #define	KSZ8995MA_PC4_BASE		0x14
87 #define	KSZ8995MA_PC5_BASE		0x15
88 #define	KSZ8995MA_PC6_BASE		0x16
89 #define	KSZ8995MA_PC7_BASE		0x17
90 #define	KSZ8995MA_PC8_BASE		0x18
91 #define	KSZ8995MA_PC9_BASE		0x19
92 #define	KSZ8995MA_PC10_BASE		0x1a
93 #define	KSZ8995MA_PC11_BASE		0x1b
94 #define	KSZ8995MA_PC12_BASE		0x1c
95 #define	KSZ8995MA_PC13_BASE		0x1d
96 
97 #define	KSZ8995MA_PS0_BASE		0x1e
98 
99 #define	KSZ8995MA_PC14_BASE		0x1f
100 
101 #define	KSZ8995MA_IAC0			0x6e
102 #define	KSZ8995MA_IAC1			0x6f
103 #define	KSZ8995MA_IDR8			0x70
104 #define	KSZ8995MA_IDR7			0x71
105 #define	KSZ8995MA_IDR6			0x72
106 #define	KSZ8995MA_IDR5			0x73
107 #define	KSZ8995MA_IDR4			0x74
108 #define	KSZ8995MA_IDR3			0x75
109 #define	KSZ8995MA_IDR2			0x76
110 #define	KSZ8995MA_IDR1			0x77
111 #define	KSZ8995MA_IDR0			0x78
112 
113 #define	KSZ8995MA_FAMILI_ID		0x95
114 #define	KSZ8995MA_CHIP_ID		0x00
115 #define	KSZ8995MA_CHIP_ID_MASK		0xf0
116 #define	KSZ8995MA_START			0x01
117 #define	KSZ8995MA_VLAN_ENABLE		0x80
118 #define	KSZ8995MA_TAG_INS		0x04
119 #define	KSZ8995MA_TAG_RM		0x02
120 #define	KSZ8995MA_INGR_FILT		0x40
121 #define	KSZ8995MA_DROP_NONPVID		0x20
122 
123 #define	KSZ8995MA_PDOWN			0x08
124 #define	KSZ8995MA_STARTNEG		0x20
125 
126 #define	KSZ8995MA_MII_STAT		0x7808
127 #define	KSZ8995MA_MII_PHYID_H		0x0022
128 #define	KSZ8995MA_MII_PHYID_L		0x1450
129 #define	KSZ8995MA_MII_AA		0x0401
130 
131 #define	KSZ8995MA_VLAN_TABLE_VALID	0x20
132 #define	KSZ8995MA_VLAN_TABLE_READ	0x14
133 #define	KSZ8995MA_VLAN_TABLE_WRITE	0x04
134 
135 #define	KSZ8995MA_MAX_PORT		5
136 
137 MALLOC_DECLARE(M_KSZ8995MA);
138 MALLOC_DEFINE(M_KSZ8995MA, "ksz8995ma", "ksz8995ma data structures");
139 
140 struct ksz8995ma_softc {
141 	struct mtx	sc_mtx;		/* serialize access to softc */
142 	device_t	sc_dev;
143 	int		vlan_mode;
144 	int		media;		/* cpu port media */
145 	int		cpuport;	/* which PHY is connected to the CPU */
146 	int		phymask;	/* PHYs we manage */
147 	int		numports;	/* number of ports */
148 	int		ifpport[KSZ8995MA_MAX_PORT];
149 	int		*portphy;
150 	char		**ifname;
151 	device_t	**miibus;
152 	if_t *ifp;
153 	struct callout	callout_tick;
154 	etherswitch_info_t	info;
155 };
156 
157 #define	KSZ8995MA_LOCK(_sc)			\
158 	    mtx_lock(&(_sc)->sc_mtx)
159 #define	KSZ8995MA_UNLOCK(_sc)			\
160 	    mtx_unlock(&(_sc)->sc_mtx)
161 #define	KSZ8995MA_LOCK_ASSERT(_sc, _what)	\
162 	    mtx_assert(&(_sc)->sc_mtx, (_what))
163 #define	KSZ8995MA_TRYLOCK(_sc)			\
164 	    mtx_trylock(&(_sc)->sc_mtx)
165 
166 #if defined(DEBUG)
167 #define	DPRINTF(dev, args...) device_printf(dev, args)
168 #else
169 #define	DPRINTF(dev, args...)
170 #endif
171 
172 static inline int ksz8995ma_portforphy(struct ksz8995ma_softc *, int);
173 static void ksz8995ma_tick(void *);
174 static int ksz8995ma_ifmedia_upd(if_t);
175 static void ksz8995ma_ifmedia_sts(if_t, struct ifmediareq *);
176 static int ksz8995ma_readreg(device_t dev, int addr);
177 static int ksz8995ma_writereg(device_t dev, int addr, int value);
178 static void ksz8995ma_portvlanreset(device_t dev);
179 
180 static int
181 ksz8995ma_probe(device_t dev)
182 {
183 	int id0, id1;
184 	struct ksz8995ma_softc *sc;
185 
186 	sc = device_get_softc(dev);
187 	bzero(sc, sizeof(*sc));
188 
189 	id0 = ksz8995ma_readreg(dev, KSZ8995MA_CID0);
190 	id1 = ksz8995ma_readreg(dev, KSZ8995MA_CID1);
191 	if (bootverbose)
192 		device_printf(dev,"Chip Identifier Register %x %x\n", id0, id1);
193 
194 	/* check Product Code */
195 	if (id0 != KSZ8995MA_FAMILI_ID || (id1 & KSZ8995MA_CHIP_ID_MASK) !=
196 	    KSZ8995MA_CHIP_ID) {
197 		return (ENXIO);
198 	}
199 
200 	device_set_desc(dev, "Micrel KSZ8995MA SPI switch driver");
201 	return (BUS_PROBE_DEFAULT);
202 }
203 
204 static int
205 ksz8995ma_attach_phys(struct ksz8995ma_softc *sc)
206 {
207 	int phy, port, err;
208 	char name[IFNAMSIZ];
209 
210 	port = 0;
211 	err = 0;
212 	/* PHYs need an interface, so we generate a dummy one */
213 	snprintf(name, IFNAMSIZ, "%sport", device_get_nameunit(sc->sc_dev));
214 	for (phy = 0; phy < sc->numports; phy++) {
215 		if (phy == sc->cpuport)
216 			continue;
217 		if (((1 << phy) & sc->phymask) == 0)
218 			continue;
219 		sc->ifpport[phy] = port;
220 		sc->portphy[port] = phy;
221 		sc->ifp[port] = if_alloc(IFT_ETHER);
222 		sc->ifp[port]->if_softc = sc;
223 		sc->ifp[port]->if_flags |= IFF_UP | IFF_BROADCAST |
224 		    IFF_DRV_RUNNING | IFF_SIMPLEX;
225 		if_initname(sc->ifp[port], name, port);
226 		sc->miibus[port] = malloc(sizeof(device_t), M_KSZ8995MA,
227 		    M_WAITOK | M_ZERO);
228 		err = mii_attach(sc->sc_dev, sc->miibus[port], sc->ifp[port],
229 		    ksz8995ma_ifmedia_upd, ksz8995ma_ifmedia_sts, \
230 		    BMSR_DEFCAPMASK, phy, MII_OFFSET_ANY, 0);
231 		DPRINTF(sc->sc_dev, "%s attached to pseudo interface %s\n",
232 		    device_get_nameunit(*sc->miibus[port]),
233 		    sc->ifp[port]->if_xname);
234 		if (err != 0) {
235 			device_printf(sc->sc_dev,
236 			    "attaching PHY %d failed\n",
237 			    phy);
238 			goto failed;
239 		}
240 		++port;
241 	}
242 	sc->info.es_nports = port;
243 	if (sc->cpuport != -1) {
244 		/* cpu port is MAC5 on ksz8995ma */
245 		sc->ifpport[sc->cpuport] = port;
246 		sc->portphy[port] = sc->cpuport;
247 		++sc->info.es_nports;
248 	}
249 
250 	return (0);
251 
252 failed:
253 	for (phy = 0; phy < sc->numports; phy++) {
254 		if (((1 << phy) & sc->phymask) == 0)
255 			continue;
256 		port = ksz8995ma_portforphy(sc, phy);
257 		if (sc->miibus[port] != NULL)
258 			device_delete_child(sc->sc_dev, (*sc->miibus[port]));
259 		if (sc->ifp[port] != NULL)
260 			if_free(sc->ifp[port]);
261 		if (sc->ifname[port] != NULL)
262 			free(sc->ifname[port], M_KSZ8995MA);
263 		if (sc->miibus[port] != NULL)
264 			free(sc->miibus[port], M_KSZ8995MA);
265 	}
266 	return (err);
267 }
268 
269 static int
270 ksz8995ma_attach(device_t dev)
271 {
272 	struct ksz8995ma_softc	*sc;
273 	int			 err, reg;
274 
275 	err = 0;
276 	sc = device_get_softc(dev);
277 
278 	sc->sc_dev = dev;
279 	mtx_init(&sc->sc_mtx, "ksz8995ma", NULL, MTX_DEF);
280 	strlcpy(sc->info.es_name, device_get_desc(dev),
281 	    sizeof(sc->info.es_name));
282 
283 	/* KSZ8995MA Defaults */
284 	sc->numports = KSZ8995MA_MAX_PORT;
285 	sc->phymask = (1 << (KSZ8995MA_MAX_PORT + 1)) - 1;
286 	sc->cpuport = -1;
287 	sc->media = 100;
288 
289 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
290 	    "cpuport", &sc->cpuport);
291 
292 	sc->info.es_nvlangroups = 16;
293 	sc->info.es_vlan_caps = ETHERSWITCH_VLAN_PORT | ETHERSWITCH_VLAN_DOT1Q;
294 
295 	sc->ifp = malloc(sizeof(if_t) * sc->numports, M_KSZ8995MA,
296 	    M_WAITOK | M_ZERO);
297 	sc->ifname = malloc(sizeof(char *) * sc->numports, M_KSZ8995MA,
298 	    M_WAITOK | M_ZERO);
299 	sc->miibus = malloc(sizeof(device_t *) * sc->numports, M_KSZ8995MA,
300 	    M_WAITOK | M_ZERO);
301 	sc->portphy = malloc(sizeof(int) * sc->numports, M_KSZ8995MA,
302 	    M_WAITOK | M_ZERO);
303 
304 	/*
305 	 * Attach the PHYs and complete the bus enumeration.
306 	 */
307 	err = ksz8995ma_attach_phys(sc);
308 	if (err != 0)
309 		goto failed;
310 
311 	bus_identify_children(dev);
312 	bus_enumerate_hinted_children(dev);
313 	bus_attach_children(dev);
314 
315 	callout_init(&sc->callout_tick, 0);
316 
317 	ksz8995ma_tick(sc);
318 
319 	/* start switch */
320 	sc->vlan_mode = 0;
321 	reg = ksz8995ma_readreg(dev, KSZ8995MA_GC3);
322 	ksz8995ma_writereg(dev, KSZ8995MA_GC3,
323 	    reg & ~KSZ8995MA_VLAN_ENABLE);
324 	ksz8995ma_portvlanreset(dev);
325 	ksz8995ma_writereg(dev, KSZ8995MA_CID1, KSZ8995MA_START);
326 
327 	return (0);
328 
329 failed:
330 	free(sc->portphy, M_KSZ8995MA);
331 	free(sc->miibus, M_KSZ8995MA);
332 	free(sc->ifname, M_KSZ8995MA);
333 	free(sc->ifp, M_KSZ8995MA);
334 
335 	return (err);
336 }
337 
338 static int
339 ksz8995ma_detach(device_t dev)
340 {
341 	struct ksz8995ma_softc	*sc;
342 	int			 i, port;
343 
344 	sc = device_get_softc(dev);
345 
346 	callout_drain(&sc->callout_tick);
347 
348 	for (i = 0; i < KSZ8995MA_MAX_PORT; i++) {
349 		if (((1 << i) & sc->phymask) == 0)
350 			continue;
351 		port = ksz8995ma_portforphy(sc, i);
352 		if (sc->miibus[port] != NULL)
353 			device_delete_child(dev, (*sc->miibus[port]));
354 		if (sc->ifp[port] != NULL)
355 			if_free(sc->ifp[port]);
356 		free(sc->ifname[port], M_KSZ8995MA);
357 		free(sc->miibus[port], M_KSZ8995MA);
358 	}
359 
360 	free(sc->portphy, M_KSZ8995MA);
361 	free(sc->miibus, M_KSZ8995MA);
362 	free(sc->ifname, M_KSZ8995MA);
363 	free(sc->ifp, M_KSZ8995MA);
364 
365 	bus_generic_detach(dev);
366 	mtx_destroy(&sc->sc_mtx);
367 
368 	return (0);
369 }
370 
371 /*
372  * Convert PHY number to port number.
373  */
374 static inline int
375 ksz8995ma_portforphy(struct ksz8995ma_softc *sc, int phy)
376 {
377 
378 	return (sc->ifpport[phy]);
379 }
380 
381 static inline struct mii_data *
382 ksz8995ma_miiforport(struct ksz8995ma_softc *sc, int port)
383 {
384 
385 	if (port < 0 || port > sc->numports)
386 		return (NULL);
387 	if (port == sc->cpuport)
388 		return (NULL);
389 	return (device_get_softc(*sc->miibus[port]));
390 }
391 
392 static inline if_t
393 ksz8995ma_ifpforport(struct ksz8995ma_softc *sc, int port)
394 {
395 
396 	if (port < 0 || port > sc->numports)
397 		return (NULL);
398 	return (sc->ifp[port]);
399 }
400 
401 /*
402  * Poll the status for all PHYs.
403  */
404 static void
405 ksz8995ma_miipollstat(struct ksz8995ma_softc *sc)
406 {
407 	int i, port;
408 	struct mii_data *mii;
409 	struct mii_softc *miisc;
410 
411 	KSZ8995MA_LOCK_ASSERT(sc, MA_NOTOWNED);
412 
413 	for (i = 0; i < KSZ8995MA_MAX_PORT; i++) {
414 		if (i == sc->cpuport)
415 			continue;
416 		if (((1 << i) & sc->phymask) == 0)
417 			continue;
418 		port = ksz8995ma_portforphy(sc, i);
419 		if ((*sc->miibus[port]) == NULL)
420 			continue;
421 		mii = device_get_softc(*sc->miibus[port]);
422 		LIST_FOREACH(miisc, &mii->mii_phys, mii_list) {
423 			if (IFM_INST(mii->mii_media.ifm_cur->ifm_media) !=
424 			    miisc->mii_inst)
425 				continue;
426 			ukphy_status(miisc);
427 			mii_phy_update(miisc, MII_POLLSTAT);
428 		}
429 	}
430 }
431 
432 static void
433 ksz8995ma_tick(void *arg)
434 {
435 	struct ksz8995ma_softc *sc;
436 
437 	sc = arg;
438 
439 	ksz8995ma_miipollstat(sc);
440 	callout_reset(&sc->callout_tick, hz, ksz8995ma_tick, sc);
441 }
442 
443 static void
444 ksz8995ma_lock(device_t dev)
445 {
446 	struct ksz8995ma_softc *sc;
447 
448 	sc = device_get_softc(dev);
449 
450 	KSZ8995MA_LOCK_ASSERT(sc, MA_NOTOWNED);
451 	KSZ8995MA_LOCK(sc);
452 }
453 
454 static void
455 ksz8995ma_unlock(device_t dev)
456 {
457 	struct ksz8995ma_softc *sc;
458 
459 	sc = device_get_softc(dev);
460 
461 	KSZ8995MA_LOCK_ASSERT(sc, MA_OWNED);
462 	KSZ8995MA_UNLOCK(sc);
463 }
464 
465 static etherswitch_info_t *
466 ksz8995ma_getinfo(device_t dev)
467 {
468 	struct ksz8995ma_softc *sc;
469 
470 	sc = device_get_softc(dev);
471 
472 	return (&sc->info);
473 }
474 
475 static int
476 ksz8995ma_getport(device_t dev, etherswitch_port_t *p)
477 {
478 	struct ksz8995ma_softc *sc;
479 	struct mii_data *mii;
480 	struct ifmediareq *ifmr;
481 	int phy, err;
482 	int tag1, tag2, portreg;
483 
484 	sc = device_get_softc(dev);
485 	ifmr = &p->es_ifmr;
486 
487 	if (p->es_port < 0 || p->es_port >= sc->numports)
488 		return (ENXIO);
489 
490 	if (sc->vlan_mode == ETHERSWITCH_VLAN_DOT1Q) {
491 		tag1 = ksz8995ma_readreg(dev, KSZ8995MA_PC3_BASE +
492 		    KSZ8995MA_PORT_SIZE * p->es_port);
493 		tag2 = ksz8995ma_readreg(dev, KSZ8995MA_PC4_BASE +
494 		    KSZ8995MA_PORT_SIZE * p->es_port);
495 		p->es_pvid = (tag1 & 0x0f) << 8 | tag2;
496 
497 		portreg = ksz8995ma_readreg(dev, KSZ8995MA_PC0_BASE +
498 		    KSZ8995MA_PORT_SIZE * p->es_port);
499 		if (portreg & KSZ8995MA_TAG_INS)
500 			p->es_flags |= ETHERSWITCH_PORT_ADDTAG;
501 		if (portreg & KSZ8995MA_TAG_RM)
502 			p->es_flags |= ETHERSWITCH_PORT_STRIPTAG;
503 
504 		portreg = ksz8995ma_readreg(dev, KSZ8995MA_PC2_BASE +
505 		    KSZ8995MA_PORT_SIZE * p->es_port);
506 		if (portreg & KSZ8995MA_DROP_NONPVID)
507 			p->es_flags |= ETHERSWITCH_PORT_DROPUNTAGGED;
508 		if (portreg & KSZ8995MA_INGR_FILT)
509 			p->es_flags |= ETHERSWITCH_PORT_INGRESS;
510 	}
511 
512 	phy = sc->portphy[p->es_port];
513 	mii = ksz8995ma_miiforport(sc, p->es_port);
514 	if (sc->cpuport != -1 && phy == sc->cpuport) {
515 		/* fill in fixed values for CPU port */
516 		p->es_flags |= ETHERSWITCH_PORT_CPU;
517 		ifmr->ifm_count = 0;
518 		if (sc->media == 100)
519 			ifmr->ifm_current = ifmr->ifm_active =
520 			    IFM_ETHER | IFM_100_TX | IFM_FDX;
521 		else
522 			ifmr->ifm_current = ifmr->ifm_active =
523 			    IFM_ETHER | IFM_1000_T | IFM_FDX;
524 		ifmr->ifm_mask = 0;
525 		ifmr->ifm_status = IFM_ACTIVE | IFM_AVALID;
526 	} else if (mii != NULL) {
527 		err = ifmedia_ioctl(mii->mii_ifp, &p->es_ifr,
528 		    &mii->mii_media, SIOCGIFMEDIA);
529 		if (err)
530 			return (err);
531 	} else {
532 		return (ENXIO);
533 	}
534 
535 	return (0);
536 }
537 
538 static int
539 ksz8995ma_setport(device_t dev, etherswitch_port_t *p)
540 {
541 	struct ksz8995ma_softc *sc;
542 	struct mii_data *mii;
543         struct ifmedia *ifm;
544         if_t ifp;
545 	int phy, err;
546 	int portreg;
547 
548 	sc = device_get_softc(dev);
549 
550 	if (p->es_port < 0 || p->es_port >= sc->numports)
551 		return (ENXIO);
552 
553 	if (sc->vlan_mode == ETHERSWITCH_VLAN_DOT1Q) {
554 		ksz8995ma_writereg(dev, KSZ8995MA_PC4_BASE +
555 		    KSZ8995MA_PORT_SIZE * p->es_port, p->es_pvid & 0xff);
556 		portreg = ksz8995ma_readreg(dev, KSZ8995MA_PC3_BASE +
557 		    KSZ8995MA_PORT_SIZE * p->es_port);
558 		ksz8995ma_writereg(dev, KSZ8995MA_PC3_BASE +
559 		    KSZ8995MA_PORT_SIZE * p->es_port,
560 		    (portreg & 0xf0) | ((p->es_pvid >> 8) & 0x0f));
561 
562 		portreg = ksz8995ma_readreg(dev, KSZ8995MA_PC0_BASE +
563 		    KSZ8995MA_PORT_SIZE * p->es_port);
564 		if (p->es_flags & ETHERSWITCH_PORT_ADDTAG)
565 			portreg |= KSZ8995MA_TAG_INS;
566 		else
567 			portreg &= ~KSZ8995MA_TAG_INS;
568 		if (p->es_flags & ETHERSWITCH_PORT_STRIPTAG)
569 			portreg |= KSZ8995MA_TAG_RM;
570 		else
571 			portreg &= ~KSZ8995MA_TAG_RM;
572 		ksz8995ma_writereg(dev, KSZ8995MA_PC0_BASE +
573 		    KSZ8995MA_PORT_SIZE * p->es_port, portreg);
574 
575 		portreg = ksz8995ma_readreg(dev, KSZ8995MA_PC2_BASE +
576 		    KSZ8995MA_PORT_SIZE * p->es_port);
577 		if (p->es_flags & ETHERSWITCH_PORT_DROPUNTAGGED)
578 			portreg |= KSZ8995MA_DROP_NONPVID;
579 		else
580 			portreg &= ~KSZ8995MA_DROP_NONPVID;
581 		if (p->es_flags & ETHERSWITCH_PORT_INGRESS)
582 			portreg |= KSZ8995MA_INGR_FILT;
583 		else
584 			portreg &= ~KSZ8995MA_INGR_FILT;
585 		ksz8995ma_writereg(dev, KSZ8995MA_PC2_BASE +
586 		    KSZ8995MA_PORT_SIZE * p->es_port, portreg);
587 	}
588 
589 	phy = sc->portphy[p->es_port];
590 	mii = ksz8995ma_miiforport(sc, p->es_port);
591 	if (phy != sc->cpuport) {
592 		if (mii == NULL)
593 			return (ENXIO);
594 		ifp = ksz8995ma_ifpforport(sc, p->es_port);
595 		ifm = &mii->mii_media;
596 		err = ifmedia_ioctl(ifp, &p->es_ifr, ifm, SIOCSIFMEDIA);
597 	}
598 	return (0);
599 }
600 
601 static int
602 ksz8995ma_getvgroup(device_t dev, etherswitch_vlangroup_t *vg)
603 {
604 	int data0, data1, data2;
605 	int vlantab;
606 	struct ksz8995ma_softc *sc;
607 
608 	sc = device_get_softc(dev);
609 
610 	if (sc->vlan_mode == ETHERSWITCH_VLAN_PORT) {
611 		if (vg->es_vlangroup < sc->numports) {
612 			vg->es_vid = ETHERSWITCH_VID_VALID;
613 			vg->es_vid |= vg->es_vlangroup;
614 			data0 = ksz8995ma_readreg(dev, KSZ8995MA_PC1_BASE +
615 			    KSZ8995MA_PORT_SIZE * vg->es_vlangroup);
616 			vg->es_member_ports = data0 & 0x1f;
617 			vg->es_untagged_ports = vg->es_member_ports;
618 			vg->es_fid = 0;
619 		} else {
620 			vg->es_vid = 0;
621 		}
622 	} else if (sc->vlan_mode == ETHERSWITCH_VLAN_DOT1Q) {
623 		ksz8995ma_writereg(dev, KSZ8995MA_IAC0,
624 		    KSZ8995MA_VLAN_TABLE_READ);
625 		ksz8995ma_writereg(dev, KSZ8995MA_IAC1, vg->es_vlangroup);
626 		data2 = ksz8995ma_readreg(dev, KSZ8995MA_IDR2);
627 		data1 = ksz8995ma_readreg(dev, KSZ8995MA_IDR1);
628 		data0 = ksz8995ma_readreg(dev, KSZ8995MA_IDR0);
629 		vlantab = data2 << 16 | data1 << 8 | data0;
630 		if (data2 & KSZ8995MA_VLAN_TABLE_VALID) {
631 			vg->es_vid = ETHERSWITCH_VID_VALID;
632 			vg->es_vid |= vlantab & 0xfff;
633 			vg->es_member_ports = (vlantab >> 16) & 0x1f;
634 			vg->es_untagged_ports = vg->es_member_ports;
635 			vg->es_fid = (vlantab >> 12) & 0x0f;
636 		} else {
637 			vg->es_fid = 0;
638 		}
639 	}
640 
641 	return (0);
642 }
643 
644 static int
645 ksz8995ma_setvgroup(device_t dev, etherswitch_vlangroup_t *vg)
646 {
647 	struct ksz8995ma_softc *sc;
648 	int data0;
649 
650 	sc = device_get_softc(dev);
651 
652 	if (sc->vlan_mode == ETHERSWITCH_VLAN_PORT) {
653 		data0 = ksz8995ma_readreg(dev, KSZ8995MA_PC1_BASE +
654 		    KSZ8995MA_PORT_SIZE * vg->es_vlangroup);
655 		ksz8995ma_writereg(dev, KSZ8995MA_PC1_BASE +
656 		    KSZ8995MA_PORT_SIZE * vg->es_vlangroup,
657 		    (data0 & 0xe0) | (vg->es_member_ports & 0x1f));
658 	} else if (sc->vlan_mode == ETHERSWITCH_VLAN_DOT1Q) {
659 		if (vg->es_member_ports != 0) {
660 			ksz8995ma_writereg(dev, KSZ8995MA_IDR2,
661 			    KSZ8995MA_VLAN_TABLE_VALID |
662 			    (vg->es_member_ports & 0x1f));
663 			ksz8995ma_writereg(dev, KSZ8995MA_IDR1,
664 			    vg->es_fid << 4 | vg->es_vid >> 8);
665 			ksz8995ma_writereg(dev, KSZ8995MA_IDR0,
666 			    vg->es_vid & 0xff);
667 		} else {
668 			ksz8995ma_writereg(dev, KSZ8995MA_IDR2, 0);
669 			ksz8995ma_writereg(dev, KSZ8995MA_IDR1, 0);
670 			ksz8995ma_writereg(dev, KSZ8995MA_IDR0, 0);
671 		}
672 		ksz8995ma_writereg(dev, KSZ8995MA_IAC0,
673 		    KSZ8995MA_VLAN_TABLE_WRITE);
674 		ksz8995ma_writereg(dev, KSZ8995MA_IAC1, vg->es_vlangroup);
675 	}
676 
677 	return (0);
678 }
679 
680 static int
681 ksz8995ma_getconf(device_t dev, etherswitch_conf_t *conf)
682 {
683 	struct ksz8995ma_softc *sc;
684 
685 	sc = device_get_softc(dev);
686 
687 	/* Return the VLAN mode. */
688 	conf->cmd = ETHERSWITCH_CONF_VLAN_MODE;
689 	conf->vlan_mode = sc->vlan_mode;
690 
691 	return (0);
692 }
693 
694 static void
695 ksz8995ma_portvlanreset(device_t dev)
696 {
697 	int i, data;
698 	struct ksz8995ma_softc *sc;
699 
700 	sc = device_get_softc(dev);
701 
702 	for (i = 0; i < sc->numports; ++i) {
703 		data = ksz8995ma_readreg(dev, KSZ8995MA_PC1_BASE +
704 		    KSZ8995MA_PORT_SIZE * i);
705 		ksz8995ma_writereg(dev, KSZ8995MA_PC1_BASE +
706 		    KSZ8995MA_PORT_SIZE * i, (data & 0xe0) | 0x1f);
707 	}
708 }
709 
710 static int
711 ksz8995ma_setconf(device_t dev, etherswitch_conf_t *conf)
712 {
713 	int reg;
714 	struct ksz8995ma_softc *sc;
715 
716 	sc = device_get_softc(dev);
717 
718 	if ((conf->cmd & ETHERSWITCH_CONF_VLAN_MODE) == 0)
719 		return (0);
720 
721 	if (conf->vlan_mode == ETHERSWITCH_VLAN_PORT) {
722 		sc->vlan_mode = ETHERSWITCH_VLAN_PORT;
723 		reg = ksz8995ma_readreg(dev, KSZ8995MA_GC3);
724 		ksz8995ma_writereg(dev, KSZ8995MA_GC3,
725 		    reg & ~KSZ8995MA_VLAN_ENABLE);
726 		ksz8995ma_portvlanreset(dev);
727 	} else if (conf->vlan_mode == ETHERSWITCH_VLAN_DOT1Q) {
728 		sc->vlan_mode = ETHERSWITCH_VLAN_DOT1Q;
729 		reg = ksz8995ma_readreg(dev, KSZ8995MA_GC3);
730 		ksz8995ma_writereg(dev, KSZ8995MA_GC3,
731 		    reg | KSZ8995MA_VLAN_ENABLE);
732 	} else {
733 		sc->vlan_mode = 0;
734 		reg = ksz8995ma_readreg(dev, KSZ8995MA_GC3);
735 		ksz8995ma_writereg(dev, KSZ8995MA_GC3,
736 		    reg & ~KSZ8995MA_VLAN_ENABLE);
737 		ksz8995ma_portvlanreset(dev);
738 	}
739 	return (0);
740 }
741 
742 static void
743 ksz8995ma_statchg(device_t dev)
744 {
745 
746 	DPRINTF(dev, "%s\n", __func__);
747 }
748 
749 static int
750 ksz8995ma_ifmedia_upd(if_t ifp)
751 {
752 	struct ksz8995ma_softc *sc;
753 	struct mii_data *mii;
754 
755 	sc = if_getsoftc(ifp);
756 	mii = ksz8995ma_miiforport(sc, if_getdunit(ifp));
757 
758 	DPRINTF(sc->sc_dev, "%s\n", __func__);
759 	if (mii == NULL)
760 		return (ENXIO);
761 	mii_mediachg(mii);
762 	return (0);
763 }
764 
765 static void
766 ksz8995ma_ifmedia_sts(if_t ifp, struct ifmediareq *ifmr)
767 {
768 	struct ksz8995ma_softc *sc;
769 	struct mii_data *mii;
770 
771 	sc = if_getsoftc(ifp);
772 	mii = ksz8995ma_miiforport(sc, if_getdunit(ifp));
773 
774 	DPRINTF(sc->sc_dev, "%s\n", __func__);
775 
776 	if (mii == NULL)
777 		return;
778 	mii_pollstat(mii);
779 	ifmr->ifm_active = mii->mii_media_active;
780 	ifmr->ifm_status = mii->mii_media_status;
781 }
782 
783 static int
784 ksz8995ma_readphy(device_t dev, int phy, int reg)
785 {
786 int portreg;
787 
788 	/*
789 	 * This is no mdio/mdc connection code.
790          * simulate MIIM Registers via the SPI interface
791 	 */
792 	if (reg == MII_BMSR) {
793 		portreg = ksz8995ma_readreg(dev, KSZ8995MA_PS0_BASE +
794 			KSZ8995MA_PORT_SIZE * phy);
795 		return (KSZ8995MA_MII_STAT |
796 		    (portreg & 0x20 ? BMSR_LINK : 0x00) |
797 		    (portreg & 0x40 ? BMSR_ACOMP : 0x00));
798 	} else if (reg == MII_PHYIDR1) {
799 		return (KSZ8995MA_MII_PHYID_H);
800 	} else if (reg == MII_PHYIDR2) {
801 		return (KSZ8995MA_MII_PHYID_L);
802 	} else if (reg == MII_ANAR) {
803 		portreg = ksz8995ma_readreg(dev, KSZ8995MA_PC12_BASE +
804 			KSZ8995MA_PORT_SIZE * phy);
805 		return (KSZ8995MA_MII_AA | (portreg & 0x0f) << 5);
806 	} else if (reg == MII_ANLPAR) {
807 		portreg = ksz8995ma_readreg(dev, KSZ8995MA_PS0_BASE +
808 			KSZ8995MA_PORT_SIZE * phy);
809 		return (((portreg & 0x0f) << 5) | 0x01);
810 	}
811 
812 	return (0);
813 }
814 
815 static int
816 ksz8995ma_writephy(device_t dev, int phy, int reg, int data)
817 {
818 int portreg;
819 
820 	/*
821 	 * This is no mdio/mdc connection code.
822          * simulate MIIM Registers via the SPI interface
823 	 */
824 	if (reg == MII_BMCR) {
825 		portreg = ksz8995ma_readreg(dev, KSZ8995MA_PC13_BASE +
826 			KSZ8995MA_PORT_SIZE * phy);
827 		if (data & BMCR_PDOWN)
828 			portreg |= KSZ8995MA_PDOWN;
829 		else
830 			portreg &= ~KSZ8995MA_PDOWN;
831 		if (data & BMCR_STARTNEG)
832 			portreg |= KSZ8995MA_STARTNEG;
833 		else
834 			portreg &= ~KSZ8995MA_STARTNEG;
835 		ksz8995ma_writereg(dev, KSZ8995MA_PC13_BASE +
836 			KSZ8995MA_PORT_SIZE * phy, portreg);
837 	} else if (reg == MII_ANAR) {
838 		portreg = ksz8995ma_readreg(dev, KSZ8995MA_PC12_BASE +
839 			KSZ8995MA_PORT_SIZE * phy);
840 		portreg &= 0xf;
841 		portreg |= ((data >> 5) & 0x0f);
842 		ksz8995ma_writereg(dev, KSZ8995MA_PC12_BASE +
843 			KSZ8995MA_PORT_SIZE * phy, portreg);
844 	}
845 	return (0);
846 }
847 
848 static int
849 ksz8995ma_readreg(device_t dev, int addr)
850 {
851 	uint8_t txBuf[8], rxBuf[8];
852 	struct spi_command cmd;
853 	int err;
854 
855 	memset(&cmd, 0, sizeof(cmd));
856 	memset(txBuf, 0, sizeof(txBuf));
857 	memset(rxBuf, 0, sizeof(rxBuf));
858 
859 	/* read spi */
860 	txBuf[0] = KSZ8995MA_SPI_READ;
861 	txBuf[1] = addr;
862 	cmd.tx_cmd = &txBuf;
863 	cmd.rx_cmd = &rxBuf;
864 	cmd.tx_cmd_sz = 3;
865 	cmd.rx_cmd_sz = 3;
866         err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd);
867 	if (err)
868 		return(0);
869 
870 	return (rxBuf[2]);
871 }
872 
873 static int
874 ksz8995ma_writereg(device_t dev, int addr, int value)
875 {
876 	uint8_t txBuf[8], rxBuf[8];
877 	struct spi_command cmd;
878 	int err;
879 
880 	memset(&cmd, 0, sizeof(cmd));
881 	memset(txBuf, 0, sizeof(txBuf));
882 	memset(rxBuf, 0, sizeof(rxBuf));
883 
884 	/* write spi */
885 	txBuf[0] = KSZ8995MA_SPI_WRITE;
886 	txBuf[1] = addr;
887 	txBuf[2] = value;
888 	cmd.tx_cmd = &txBuf;
889 	cmd.rx_cmd = &rxBuf;
890 	cmd.tx_cmd_sz = 3;
891 	cmd.rx_cmd_sz = 3;
892         err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd);
893 	if (err)
894 		return(0);
895 
896 	return (0);
897 }
898 
899 static device_method_t ksz8995ma_methods[] = {
900 	/* Device interface */
901 	DEVMETHOD(device_probe,			ksz8995ma_probe),
902 	DEVMETHOD(device_attach,		ksz8995ma_attach),
903 	DEVMETHOD(device_detach,		ksz8995ma_detach),
904 
905 	/* bus interface */
906 	DEVMETHOD(bus_add_child,		device_add_child_ordered),
907 
908 	/* MII interface */
909 	DEVMETHOD(miibus_readreg,		ksz8995ma_readphy),
910 	DEVMETHOD(miibus_writereg,		ksz8995ma_writephy),
911 	DEVMETHOD(miibus_statchg,		ksz8995ma_statchg),
912 
913 	/* etherswitch interface */
914 	DEVMETHOD(etherswitch_lock,		ksz8995ma_lock),
915 	DEVMETHOD(etherswitch_unlock,		ksz8995ma_unlock),
916 	DEVMETHOD(etherswitch_getinfo,		ksz8995ma_getinfo),
917 	DEVMETHOD(etherswitch_readreg,		ksz8995ma_readreg),
918 	DEVMETHOD(etherswitch_writereg,		ksz8995ma_writereg),
919 	DEVMETHOD(etherswitch_readphyreg,	ksz8995ma_readphy),
920 	DEVMETHOD(etherswitch_writephyreg,	ksz8995ma_writephy),
921 	DEVMETHOD(etherswitch_getport,		ksz8995ma_getport),
922 	DEVMETHOD(etherswitch_setport,		ksz8995ma_setport),
923 	DEVMETHOD(etherswitch_getvgroup,	ksz8995ma_getvgroup),
924 	DEVMETHOD(etherswitch_setvgroup,	ksz8995ma_setvgroup),
925 	DEVMETHOD(etherswitch_setconf,		ksz8995ma_setconf),
926 	DEVMETHOD(etherswitch_getconf,		ksz8995ma_getconf),
927 
928 	DEVMETHOD_END
929 };
930 
931 DEFINE_CLASS_0(ksz8995ma, ksz8995ma_driver, ksz8995ma_methods,
932     sizeof(struct ksz8995ma_softc));
933 
934 DRIVER_MODULE(ksz8995ma, spibus, ksz8995ma_driver, 0, 0);
935 DRIVER_MODULE(miibus, ksz8995ma, miibus_driver, 0, 0);
936 DRIVER_MODULE(etherswitch, ksz8995ma, etherswitch_driver, 0, 0);
937 MODULE_VERSION(ksz8995ma, 1);
938 MODULE_DEPEND(ksz8995ma, spibus, 1, 1, 1); /* XXX which versions? */
939 MODULE_DEPEND(ksz8995ma, miibus, 1, 1, 1); /* XXX which versions? */
940 MODULE_DEPEND(ksz8995ma, etherswitch, 1, 1, 1); /* XXX which versions? */
941