xref: /openbsd/sys/dev/ic/rtl80x9.c (revision 898184e3)
1 /*	$OpenBSD: rtl80x9.c,v 1.8 2008/06/26 05:42:16 ray 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  *
21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "bpfilter.h"
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/mbuf.h>
39 #include <sys/syslog.h>
40 #include <sys/socket.h>
41 #include <sys/device.h>
42 
43 #include <net/if.h>
44 #include <net/if_media.h>
45 
46 #ifdef INET
47 #include <netinet/in.h>
48 #include <netinet/if_ether.h>
49 #endif
50 
51 #include <machine/bus.h>
52 
53 #include <dev/ic/dp8390reg.h>
54 #include <dev/ic/dp8390var.h>
55 
56 #include <dev/ic/ne2000reg.h>
57 #include <dev/ic/ne2000var.h>
58 
59 #include <dev/ic/rtl80x9reg.h>
60 #include <dev/ic/rtl80x9var.h>
61 
62 int
63 rtl80x9_mediachange(dsc)
64 	struct dp8390_softc *dsc;
65 {
66 
67 	/*
68 	 * Current media is already set up.  Just reset the interface
69 	 * to let the new value take hold.  The new media will be
70 	 * set up in ne_pci_rtl8029_init_card() called via dp8390_init().
71 	 */
72 	dp8390_reset(dsc);
73 	return (0);
74 }
75 
76 void
77 rtl80x9_mediastatus(sc, ifmr)
78 	struct dp8390_softc *sc;
79 	struct ifmediareq *ifmr;
80 {
81 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
82 	u_int8_t cr_proto = sc->cr_proto |
83 	    ((ifp->if_flags & IFF_RUNNING) ? ED_CR_STA : ED_CR_STP);
84 
85 	/*
86 	 * Sigh, can detect which media is being used, but can't
87 	 * detect if we have link or not.
88 	 */
89 
90 	/* Set NIC to page 3 registers. */
91 	NIC_PUT(sc->sc_regt, sc->sc_regh, ED_P0_CR, cr_proto | ED_CR_PAGE_3);
92 
93 	if (NIC_GET(sc->sc_regt, sc->sc_regh, NERTL_RTL3_CONFIG0) &
94 	    RTL3_CONFIG0_BNC)
95 		ifmr->ifm_active = IFM_ETHER|IFM_10_2;
96 	else {
97 		ifmr->ifm_active = IFM_ETHER|IFM_10_T;
98 		if (NIC_GET(sc->sc_regt, sc->sc_regh, NERTL_RTL3_CONFIG3) &
99 		    RTL3_CONFIG3_FUDUP)
100 			ifmr->ifm_active |= IFM_FDX;
101 	}
102 
103 	/* Set NIC to page 0 registers. */
104 	NIC_PUT(sc->sc_regt, sc->sc_regh, ED_P0_CR, cr_proto | ED_CR_PAGE_0);
105 }
106 
107 void
108 rtl80x9_init_card(sc)
109 	struct dp8390_softc *sc;
110 {
111 	struct ifmedia *ifm = &sc->sc_media;
112 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
113 	u_int8_t cr_proto = sc->cr_proto |
114 	    ((ifp->if_flags & IFF_RUNNING) ? ED_CR_STA : ED_CR_STP);
115 	u_int8_t reg;
116 
117 	/* Set NIC to page 3 registers. */
118 	NIC_PUT(sc->sc_regt, sc->sc_regh, ED_P0_CR, cr_proto | ED_CR_PAGE_3);
119 
120 	/* write enable config1-3. */
121 	NIC_PUT(sc->sc_regt, sc->sc_regh, NERTL_RTL3_EECR,
122 	    RTL3_EECR_EEM1|RTL3_EECR_EEM0);
123 
124 	/* First, set basic media type. */
125 	reg = NIC_GET(sc->sc_regt, sc->sc_regh, NERTL_RTL3_CONFIG2);
126 	reg &= ~(RTL3_CONFIG2_PL1|RTL3_CONFIG2_PL0);
127 	switch (IFM_SUBTYPE(ifm->ifm_cur->ifm_media)) {
128 	case IFM_AUTO:
129 		/* Nothing to do; both bits clear == auto-detect. */
130 		break;
131 
132 	case IFM_10_T:
133 		/*
134 		 * According to docs, this should be:
135 		 * reg |= RTL3_CONFIG2_PL0;
136 		 * but this doesn't work, so make it the same as AUTO.
137 		 */
138 		break;
139 
140 	case IFM_10_2:
141 		reg |= RTL3_CONFIG2_PL1|RTL3_CONFIG2_PL0;
142 		break;
143 	}
144 	NIC_PUT(sc->sc_regt, sc->sc_regh, NERTL_RTL3_CONFIG2, reg);
145 
146 	/* Now, set duplex mode. */
147 	reg = NIC_GET(sc->sc_regt, sc->sc_regh, NERTL_RTL3_CONFIG3);
148 	if (ifm->ifm_cur->ifm_media & IFM_FDX)
149 		reg |= RTL3_CONFIG3_FUDUP;
150 	else
151 		reg &= ~RTL3_CONFIG3_FUDUP;
152 	NIC_PUT(sc->sc_regt, sc->sc_regh, NERTL_RTL3_CONFIG3, reg);
153 
154 	/* write disable config1-3 */
155 	NIC_PUT(sc->sc_regt, sc->sc_regh, NERTL_RTL3_EECR, 0);
156 
157 	/* Set NIC to page 0 registers. */
158 	NIC_PUT(sc->sc_regt, sc->sc_regh, ED_P0_CR, cr_proto | ED_CR_PAGE_0);
159 }
160 
161 void
162 rtl80x9_media_init(sc)
163 	struct dp8390_softc *sc;
164 {
165 	static int rtl80x9_media[] = {
166 		IFM_ETHER|IFM_AUTO,
167 		IFM_ETHER|IFM_10_T,
168 		IFM_ETHER|IFM_10_T|IFM_FDX,
169 		IFM_ETHER|IFM_10_2,
170 	};
171 	static const int rtl80x9_nmedia =
172 	    sizeof(rtl80x9_media) / sizeof(rtl80x9_media[0]);
173 
174 	int i, defmedia;
175 	u_int8_t conf2, conf3;
176 
177 	/* Set NIC to page 3 registers. */
178 	bus_space_write_1(sc->sc_regt, sc->sc_regh, ED_P0_CR, ED_CR_PAGE_3);
179 
180 	conf2 = bus_space_read_1(sc->sc_regt, sc->sc_regh, NERTL_RTL3_CONFIG2);
181 	conf3 = bus_space_read_1(sc->sc_regt, sc->sc_regh, NERTL_RTL3_CONFIG3);
182 
183 	conf2 &= RTL3_CONFIG2_PL1|RTL3_CONFIG2_PL0;
184 
185 	switch (conf2) {
186 	case 0:
187 		defmedia = IFM_ETHER|IFM_AUTO;
188 		break;
189 
190 	case RTL3_CONFIG2_PL1|RTL3_CONFIG2_PL0:
191 	case RTL3_CONFIG2_PL1:	/* XXX rtl docs sys 10base5, but chip cant do */
192 		defmedia = IFM_ETHER|IFM_10_2;
193 		break;
194 
195 	case RTL3_CONFIG2_PL0:
196 		if (conf3 & RTL3_CONFIG3_FUDUP)
197 			defmedia = IFM_ETHER|IFM_10_T|IFM_FDX;
198 		else
199 			defmedia = IFM_ETHER|IFM_10_T;
200 		break;
201 	}
202 
203 	/* Set NIC to page 0 registers. */
204 	bus_space_write_1(sc->sc_regt, sc->sc_regh, ED_P0_CR, ED_CR_PAGE_0);
205 
206 	ifmedia_init(&sc->sc_media, 0, dp8390_mediachange, dp8390_mediastatus);
207 	for (i = 0; i < rtl80x9_nmedia; i++)
208 		ifmedia_add(&sc->sc_media, rtl80x9_media[i], 0, NULL);
209 	ifmedia_set(&sc->sc_media, defmedia);
210 }
211