xref: /netbsd/sys/dev/ic/rtl80x9.c (revision bf9ec67e)
1 /*	$NetBSD: rtl80x9.c,v 1.8 2001/11/13 13:14:43 lukem Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: rtl80x9.c,v 1.8 2001/11/13 13:14:43 lukem Exp $");
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/mbuf.h>
46 #include <sys/syslog.h>
47 #include <sys/socket.h>
48 #include <sys/device.h>
49 
50 #include <net/if.h>
51 #include <net/if_ether.h>
52 #include <net/if_media.h>
53 
54 #include <machine/bus.h>
55 #include <machine/intr.h>
56 
57 #include <dev/ic/dp8390reg.h>
58 #include <dev/ic/dp8390var.h>
59 
60 #include <dev/ic/ne2000reg.h>
61 #include <dev/ic/ne2000var.h>
62 
63 #include <dev/ic/rtl80x9reg.h>
64 #include <dev/ic/rtl80x9var.h>
65 
66 int
67 rtl80x9_mediachange(dsc)
68 	struct dp8390_softc *dsc;
69 {
70 
71 	/*
72 	 * Current media is already set up.  Just reset the interface
73 	 * to let the new value take hold.  The new media will be
74 	 * set up in ne_pci_rtl8029_init_card() called via dp8390_init().
75 	 */
76 	dp8390_reset(dsc);
77 	return (0);
78 }
79 
80 void
81 rtl80x9_mediastatus(sc, ifmr)
82 	struct dp8390_softc *sc;
83 	struct ifmediareq *ifmr;
84 {
85 	struct ifnet *ifp = &sc->sc_ec.ec_if;
86 	u_int8_t cr_proto = sc->cr_proto |
87 	    ((ifp->if_flags & IFF_RUNNING) ? ED_CR_STA : ED_CR_STP);
88 
89 	/*
90 	 * Sigh, can detect which media is being used, but can't
91 	 * detect if we have link or not.
92 	 */
93 
94 	/* Set NIC to page 3 registers. */
95 	NIC_PUT(sc->sc_regt, sc->sc_regh, ED_P0_CR, cr_proto | ED_CR_PAGE_3);
96 
97 	if (NIC_GET(sc->sc_regt, sc->sc_regh, NERTL_RTL3_CONFIG0) &
98 	    RTL3_CONFIG0_BNC)
99 		ifmr->ifm_active = IFM_ETHER|IFM_10_2;
100 	else {
101 		ifmr->ifm_active = IFM_ETHER|IFM_10_T;
102 		if (NIC_GET(sc->sc_regt, sc->sc_regh, NERTL_RTL3_CONFIG3) &
103 		    RTL3_CONFIG3_FUDUP)
104 			ifmr->ifm_active |= IFM_FDX;
105 	}
106 
107 	/* Set NIC to page 0 registers. */
108 	NIC_PUT(sc->sc_regt, sc->sc_regh, ED_P0_CR, cr_proto | ED_CR_PAGE_0);
109 }
110 
111 void
112 rtl80x9_init_card(sc)
113 	struct dp8390_softc *sc;
114 {
115 	struct ifmedia *ifm = &sc->sc_media;
116 	struct ifnet *ifp = &sc->sc_ec.ec_if;
117 	u_int8_t cr_proto = sc->cr_proto |
118 	    ((ifp->if_flags & IFF_RUNNING) ? ED_CR_STA : ED_CR_STP);
119 	u_int8_t reg;
120 
121 	/* Set NIC to page 3 registers. */
122 	NIC_PUT(sc->sc_regt, sc->sc_regh, ED_P0_CR, cr_proto | ED_CR_PAGE_3);
123 
124 	/* write enable config1-3. */
125 	NIC_PUT(sc->sc_regt, sc->sc_regh, NERTL_RTL3_EECR,
126 	    RTL3_EECR_EEM1|RTL3_EECR_EEM0);
127 
128 	/* First, set basic media type. */
129 	reg = NIC_GET(sc->sc_regt, sc->sc_regh, NERTL_RTL3_CONFIG2);
130 	reg &= ~(RTL3_CONFIG2_PL1|RTL3_CONFIG2_PL0);
131 	switch (IFM_SUBTYPE(ifm->ifm_cur->ifm_media)) {
132 	case IFM_AUTO:
133 		/* Nothing to do; both bits clear == auto-detect. */
134 		break;
135 
136 	case IFM_10_T:
137 		/*
138 		 * According to docs, this should be:
139 		 * reg |= RTL3_CONFIG2_PL0;
140 		 * but this doesn't work, so make it the same as AUTO.
141 		 */
142 		break;
143 
144 	case IFM_10_2:
145 		reg |= RTL3_CONFIG2_PL1|RTL3_CONFIG2_PL0;
146 		break;
147 	}
148 	NIC_PUT(sc->sc_regt, sc->sc_regh, NERTL_RTL3_CONFIG2, reg);
149 
150 	/* Now, set duplex mode. */
151 	reg = NIC_GET(sc->sc_regt, sc->sc_regh, NERTL_RTL3_CONFIG3);
152 	if (ifm->ifm_cur->ifm_media & IFM_FDX)
153 		reg |= RTL3_CONFIG3_FUDUP;
154 	else
155 		reg &= ~RTL3_CONFIG3_FUDUP;
156 	NIC_PUT(sc->sc_regt, sc->sc_regh, NERTL_RTL3_CONFIG3, reg);
157 
158 	/* write disable config1-3 */
159 	NIC_PUT(sc->sc_regt, sc->sc_regh, NERTL_RTL3_EECR, 0);
160 
161 	/* Set NIC to page 0 registers. */
162 	NIC_PUT(sc->sc_regt, sc->sc_regh, ED_P0_CR, cr_proto | ED_CR_PAGE_0);
163 }
164 
165 void
166 rtl80x9_media_init(sc)
167 	struct dp8390_softc *sc;
168 {
169 	static int rtl80x9_media[] = {
170 		IFM_ETHER|IFM_AUTO,
171 		IFM_ETHER|IFM_10_T,
172 		IFM_ETHER|IFM_10_T|IFM_FDX,
173 		IFM_ETHER|IFM_10_2,
174 	};
175 	static const int rtl80x9_nmedia =
176 	    sizeof(rtl80x9_media) / sizeof(rtl80x9_media[0]);
177 
178 	int i, defmedia;
179 	u_int8_t conf2, conf3;
180 
181 	printf("%s: 10base2, 10baseT, 10baseT-FDX, auto, default ",
182 	    sc->sc_dev.dv_xname);
183 
184 	bus_space_write_1(sc->sc_regt, sc->sc_regh, ED_P0_CR, ED_CR_PAGE_3);
185 
186 	conf2 = bus_space_read_1(sc->sc_regt, sc->sc_regh, NERTL_RTL3_CONFIG2);
187 	conf3 = bus_space_read_1(sc->sc_regt, sc->sc_regh, NERTL_RTL3_CONFIG3);
188 	printf("[0x%02x 0x%02x] ", conf2, conf3);
189 
190 	conf2 &= RTL3_CONFIG2_PL1|RTL3_CONFIG2_PL0;
191 
192 	switch (conf2) {
193 	case 0:
194 		defmedia = IFM_ETHER|IFM_AUTO;
195 		printf("auto\n");
196 		break;
197 
198 	case RTL3_CONFIG2_PL1|RTL3_CONFIG2_PL0:
199 	case RTL3_CONFIG2_PL1:	/* XXX rtl docs sys 10base5, but chip cant do */
200 		defmedia = IFM_ETHER|IFM_10_2;
201 		printf("10base2\n");
202 		break;
203 
204 	case RTL3_CONFIG2_PL0:
205 		if (conf3 & RTL3_CONFIG3_FUDUP) {
206 			defmedia = IFM_ETHER|IFM_10_T|IFM_FDX;
207 			printf("10baseT-FDX\n");
208 		} else {
209 			defmedia = IFM_ETHER|IFM_10_T;
210 			printf("10baseT\n");
211 		}
212 		break;
213 	}
214 
215 	bus_space_write_1(sc->sc_regt, sc->sc_regh, ED_P0_CR, ED_CR_PAGE_0);
216 
217 	ifmedia_init(&sc->sc_media, 0, dp8390_mediachange, dp8390_mediastatus);
218 	for (i = 0; i < rtl80x9_nmedia; i++)
219 		ifmedia_add(&sc->sc_media, rtl80x9_media[i], 0, NULL);
220 	ifmedia_set(&sc->sc_media, defmedia);
221 }
222