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