xref: /netbsd-src/sys/dev/ic/ax88190.c (revision a4d5ffe13f0e4b99b752d2968ac9bd06f485e23b)
1 /*	$NetBSD: ax88190.c,v 1.18 2021/07/01 20:39:15 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Enami Tsugutomo.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: ax88190.c,v 1.18 2021/07/01 20:39:15 thorpej Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/device.h>
38 #include <sys/socket.h>
39 
40 #include <net/if.h>
41 #include <net/if_dl.h>
42 #include <net/if_types.h>
43 #include <net/if_media.h>
44 #include <net/if_ether.h>
45 
46 #include <sys/bus.h>
47 
48 #include <dev/mii/miivar.h>
49 #include <dev/mii/mii.h>
50 #include <dev/mii/mii_bitbang.h>
51 
52 #include <dev/ic/dp8390reg.h>
53 #include <dev/ic/dp8390var.h>
54 
55 #include <dev/ic/ne2000reg.h>
56 #include <dev/ic/ne2000var.h>
57 
58 #include <dev/ic/ax88190reg.h>
59 #include <dev/ic/ax88190var.h>
60 
61 static int	ax88190_mii_readreg(device_t, int, int, uint16_t *);
62 static int	ax88190_mii_writereg(device_t, int, int, uint16_t);
63 static void	ax88190_mii_statchg(struct ifnet *);
64 
65 /*
66  * MII bit-bang glue.
67  */
68 static u_int32_t	ax88190_mii_bitbang_read(device_t);
69 static void		ax88190_mii_bitbang_write(device_t, u_int32_t);
70 
71 static const struct mii_bitbang_ops ax88190_mii_bitbang_ops = {
72 	ax88190_mii_bitbang_read,
73 	ax88190_mii_bitbang_write,
74 	{
75 		AX88190_MEMR_MDO,	/* MII_BIT_MDO */
76 		AX88190_MEMR_MDI,	/* MII_BIT_MDI */
77 		AX88190_MEMR_MDC,	/* MII_BIT_MDC */
78 		0,			/* MII_BIT_DIR_HOST_PHY */
79 		AX88190_MEMR_MDIR,	/* MII_BIT_DIR_PHY_HOST */
80 	}
81 };
82 
83 static void
ax88190_tick(void * arg)84 ax88190_tick(void *arg)
85 {
86 	struct dp8390_softc *sc = arg;
87 	int s;
88 
89 	s = splnet();
90 	mii_tick(&sc->sc_mii);
91 	splx(s);
92 
93 	callout_schedule(&sc->sc_tick_ch, hz);
94 }
95 
96 void
ax88190_media_init(struct dp8390_softc * sc)97 ax88190_media_init(struct dp8390_softc *sc)
98 {
99 	struct ifnet *ifp = &sc->sc_ec.ec_if;
100 	struct mii_data *mii = &sc->sc_mii;
101 
102 	callout_setfunc(&sc->sc_tick_ch, ax88190_tick, sc);
103 
104 	sc->sc_ec.ec_mii = mii;
105 
106 	mii->mii_ifp = ifp;
107 	mii->mii_readreg = ax88190_mii_readreg;
108 	mii->mii_writereg = ax88190_mii_writereg;
109 	mii->mii_statchg = ax88190_mii_statchg;
110 	ifmedia_init(&mii->mii_media, IFM_IMASK, dp8390_mediachange,
111 	    dp8390_mediastatus);
112 
113 	mii_attach(sc->sc_dev, mii, 0xffffffff, MII_PHY_ANY,
114 	    MII_OFFSET_ANY, 0);
115 
116 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
117 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
118 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
119 	} else
120 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
121 }
122 
123 void
ax88190_media_fini(struct dp8390_softc * sc)124 ax88190_media_fini(struct dp8390_softc *sc)
125 {
126 
127 	callout_stop(&sc->sc_tick_ch);
128 	mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
129 	/* dp8390_detach() will call ifmedia_fini(). */
130 }
131 
132 int
ax88190_mediachange(struct dp8390_softc * sc)133 ax88190_mediachange(struct dp8390_softc *sc)
134 {
135 	int rc;
136 
137 	if ((rc = mii_mediachg(&sc->sc_mii)) == ENXIO)
138 		return 0;
139 	return rc;
140 }
141 
142 void
ax88190_mediastatus(struct dp8390_softc * sc,struct ifmediareq * ifmr)143 ax88190_mediastatus(struct dp8390_softc *sc, struct ifmediareq *ifmr)
144 {
145 
146 	mii_pollstat(&sc->sc_mii);
147 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
148 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
149 }
150 
151 void
ax88190_init_card(struct dp8390_softc * sc)152 ax88190_init_card(struct dp8390_softc *sc)
153 {
154 
155 	mii_mediachg(&sc->sc_mii);
156 	callout_schedule(&sc->sc_tick_ch, hz);
157 }
158 
159 void
ax88190_stop_card(struct dp8390_softc * sc)160 ax88190_stop_card(struct dp8390_softc *sc)
161 {
162 
163 	callout_stop(&sc->sc_tick_ch);
164 	mii_down(&sc->sc_mii);
165 }
166 
167 static u_int32_t
ax88190_mii_bitbang_read(device_t self)168 ax88190_mii_bitbang_read(device_t self)
169 {
170 	struct ne2000_softc *sc = device_private(self);
171 
172 	return (bus_space_read_1(sc->sc_asict, sc->sc_asich, AX88190_MEMR));
173 }
174 
175 static void
ax88190_mii_bitbang_write(device_t self,uint32_t val)176 ax88190_mii_bitbang_write(device_t self, uint32_t val)
177 {
178 	struct ne2000_softc *sc = device_private(self);
179 
180 	bus_space_write_1(sc->sc_asict, sc->sc_asich, AX88190_MEMR, val);
181 }
182 
183 static int
ax88190_mii_readreg(device_t self,int phy,int reg,uint16_t * val)184 ax88190_mii_readreg(device_t self, int phy, int reg, uint16_t *val)
185 {
186 
187 	return mii_bitbang_readreg(self, &ax88190_mii_bitbang_ops, phy, reg,
188 	    val);
189 }
190 
191 static int
ax88190_mii_writereg(device_t self,int phy,int reg,uint16_t val)192 ax88190_mii_writereg(device_t self, int phy, int reg, uint16_t val)
193 {
194 
195 	return mii_bitbang_writereg(self, &ax88190_mii_bitbang_ops, phy, reg,
196 	    val);
197 }
198 
199 static void
ax88190_mii_statchg(struct ifnet * ifp)200 ax88190_mii_statchg(struct ifnet *ifp)
201 {
202 
203 	/* XXX */
204 }
205