xref: /netbsd/sys/dev/mca/if_le_mca.c (revision c4a72b64)
1 /*	$NetBSD: if_le_mca.c,v 1.6 2002/10/02 16:34:11 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 Jaromir Dolecek.
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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Driver for SKNET Personal and MC2+ cards, which are AMD Lance 7990 based
41  * cards made by Syskonnect, former Schneider & Koch Datensysteme GmbH.
42  *
43  * Syskonnect was very helpful and provided docs for these cards promptly.
44  * I wish all vendors would be like that!
45  * I'd like to thank to Alfred Arnold, author of the Linux driver, for
46  * giving me contact to The Right Syskonnect person, too :-)
47  *
48  * Sources:
49  * SKNET MC+ Technical Manual, version 1.1, July 21 1993
50  * SKNET personal Technisches Manual, version 1.2, April 14 1988
51  * SKNET junior Technisches Manual, version 1.0, July 14 1987
52  */
53 
54 #include <sys/cdefs.h>
55 __KERNEL_RCSID(0, "$NetBSD: if_le_mca.c,v 1.6 2002/10/02 16:34:11 thorpej Exp $");
56 
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/mbuf.h>
60 #include <sys/syslog.h>
61 #include <sys/socket.h>
62 #include <sys/device.h>
63 
64 #include <uvm/uvm_extern.h>
65 
66 #include <net/if.h>
67 #include <net/if_ether.h>
68 #include <net/if_media.h>
69 
70 #include <machine/cpu.h>
71 #include <machine/intr.h>
72 #include <machine/bus.h>
73 
74 #include <dev/ic/lancereg.h>
75 #include <dev/ic/lancevar.h>
76 #include <dev/ic/am7990reg.h>
77 #include <dev/ic/am7990var.h>
78 
79 #include <dev/mca/mcareg.h>
80 #include <dev/mca/mcavar.h>
81 #include <dev/mca/mcadevs.h>
82 
83 #include <dev/mca/if_lereg.h>
84 
85 int 	le_mca_match __P((struct device *, struct cfdata *, void *));
86 void	le_mca_attach __P((struct device *, struct device *, void *));
87 
88 struct le_mca_softc {
89 	struct	am7990_softc sc_am7990;	/* glue to MI code */
90 
91 	void	*sc_ih;
92 	bus_space_tag_t sc_memt;
93 	bus_space_handle_t sc_memh;
94 };
95 
96 static void le_mca_wrcsr __P((struct lance_softc *, u_int16_t, u_int16_t));
97 static u_int16_t le_mca_rdcsr __P((struct lance_softc *, u_int16_t));
98 static void le_mca_hwreset __P((struct lance_softc *));
99 static int le_mca_intredge __P((void *));
100 
101 static void	le_mca_copytobuf(struct lance_softc *, void *, int, int);
102 static void	le_mca_copyfrombuf(struct lance_softc *, void *, int, int);
103 static void	le_mca_zerobuf(struct lance_softc *, int, int);
104 
105 static __inline void le_mca_wrreg __P((struct le_mca_softc *, int, int));
106 #define le_mca_set_RAP(sc, reg_number) \
107 		le_mca_wrreg(sc, reg_number, RAP | REGWRITE)
108 
109 CFATTACH_DECL(le_mca, sizeof(struct le_mca_softc),
110     le_mca_match, le_mca_attach, NULL, NULL);
111 
112 /* SKNET MC+ POS mapping */
113 static const u_int8_t sknet_mcp_irq[] = {
114 	3, 5, 10, 11
115 };
116 static const u_int8_t sknet_mcp_media[] = {
117 	IFM_ETHER|IFM_10_2,
118 	IFM_ETHER|IFM_10_T,
119 	IFM_ETHER|IFM_10_5,
120 	0
121 };
122 
123 int
124 le_mca_match(struct device *parent, struct cfdata *cf, void *aux)
125 {
126 	struct mca_attach_args *ma = aux;
127 
128 	switch(ma->ma_id) {
129 	case MCA_PRODUCT_SKNETPER:
130 	case MCA_PRODUCT_SKNETG:
131 		return (1);
132 	}
133 
134 	return (0);
135 }
136 
137 void
138 le_mca_attach(struct device *parent, struct device *self, void *aux)
139 {
140 	struct le_mca_softc *lesc = (struct le_mca_softc *) self;
141 	struct lance_softc *sc = &lesc->sc_am7990.lsc;
142 	struct mca_attach_args *ma = aux;
143 	int i, pos2, pos3, pos4, irq, membase, supmedia=0;
144 	const char *typestr;
145 
146 	/*
147 	 * SKNET Personal:
148 	 *
149 	 * POS register 2: (adf pos0)
150 	 *
151 	 * 7 6 5 4 3 2 1 0
152 	 *       | \___/ \__ enable: 0=adapter disabled, 1=adapter enabled
153 	 *        \    \____ Memory: 0xC0000-0xC3FFF + XX*0x4000
154 	 *         \________ IRQ: 0=10 1=11
155 	 *
156 	 *
157 	 * SKNET MC+:
158 	 * POS register 2: (adf pos0)
159 	 *
160 	 * 7 6 5 4 3 2 1 0
161 	 *       \___/ \ \__ enable: 0=adapter disabled, 1=adapter enabled
162 	 *           \  \___ BootEPROM disable
163 	 *            \_____ BootEPROM start address: 0xC0000 + XX*0x4000
164 	 *
165 	 * POS register 3: (adf pos1)
166 	 *
167 	 * 7 6 5 4 3 2 1 0
168 	 * 0 0 1 1 \_____/
169 	 *               \__ RAM: 0xC0000 + XX*0x4000
170 	 *
171 	 * POS register 4: (adf pos2)
172 	 *
173 	 * 7 6 5 4 3 2 1 0
174 	 * \_/     \_/ \_/
175 	 *   \       \   \__ Need to be reset to 0 0 after boot
176 	 *    \       \_____ IRQ: 00=3 01=5 10=10 11=11
177 	 *     \____________ Medium: 00=BNC 01=UTP 10=AUI 11=not allowed
178 	 */
179 
180 	switch (ma->ma_id) {
181 	case MCA_PRODUCT_SKNETPER:
182 		typestr = "Personal MC2";
183 
184 		pos2 = mca_conf_read(ma->ma_mc, ma->ma_slot, 2);
185 		irq = (pos2 & (1<<4)) ? 11 : 10;
186 		membase = 0xc0000 + ((pos2 & 0x0e) >> 1) * 0x4000;
187 		break;
188 	case MCA_PRODUCT_SKNETG:
189 		typestr = "MC2+";
190 
191 		/*
192 		 * SKNET MC+ needs the driver to clear 0, 1 bits of pos4
193 		 * and explicitly set the enable bit. Somebody at Syskonnect
194 		 * was obviously misguided when the card was designed ...
195 		 */
196 		pos3 = mca_conf_read(ma->ma_mc, ma->ma_slot, 3);
197 		pos4 = mca_conf_read(ma->ma_mc, ma->ma_slot, 4);
198 		if ((pos4 & 0x03) != 0) {
199 			/* clear the bits 0, 1 */
200 			mca_conf_write(ma->ma_mc, ma->ma_slot, 4,
201 				pos4 & ~0x03);
202 		}
203 
204 		pos2 = mca_conf_read(ma->ma_mc, ma->ma_slot, 2);
205 		if ((pos2 & 0x01) == 0) {
206 			/* enable the card */
207 			mca_conf_write(ma->ma_mc, ma->ma_slot, 2, pos2 | 0x01);
208 		}
209 
210 		/* get irq and memory base */
211 		irq = sknet_mcp_irq[(pos4 & 0x0c) >> 2];
212 		membase = 0xc0000 + ((pos3 & 0x0f) * 0x4000);
213 
214 		/* Get configured media type */
215 		supmedia = sknet_mcp_media[(pos4 & 0xc0) >> 6];
216 		break;
217 	}
218 
219 	lesc->sc_memt = ma->ma_memt;
220 
221 	if (bus_space_map(lesc->sc_memt, membase, LE_MCA_MEMSIZE,
222 		0, &lesc->sc_memh)) {
223 		printf("%s: can't map memory\n", sc->sc_dev.dv_xname);
224 		return;
225 	}
226 
227 	printf(" slot %d irq %d: SKNET %s Ethernet\n",
228 		ma->ma_slot + 1, irq, typestr);
229 
230 	/*
231 	 * Extract the physical MAC address from the ROM.
232 	 */
233 	for (i = 0; i < ETHER_ADDR_LEN; i++)
234 		sc->sc_enaddr[i] = bus_space_read_1(lesc->sc_memt,
235 				lesc->sc_memh, LE_PROMOFF + i*2);
236 
237 	sc->sc_conf3 = LE_C3_ACON;
238 	sc->sc_addr = 0;
239 	sc->sc_memsize = LE_MCA_RAMSIZE;
240 
241 	sc->sc_copytodesc = le_mca_copytobuf;
242 	sc->sc_copyfromdesc = le_mca_copyfrombuf;
243 	sc->sc_copytobuf = le_mca_copytobuf;
244 	sc->sc_copyfrombuf = le_mca_copyfrombuf;
245 	sc->sc_zerobuf = le_mca_zerobuf;
246 
247 	sc->sc_rdcsr = le_mca_rdcsr;
248 	sc->sc_wrcsr = le_mca_wrcsr;
249 	sc->sc_hwinit = NULL;
250 
251 	sc->sc_hwreset = le_mca_hwreset;
252 
253 	/*
254 	 * This is merely cosmetic since it's not possible to switch
255 	 * the media anyway, even for MC2+.
256 	 */
257 	if (supmedia != 0) {
258 		sc->sc_supmedia = &supmedia;
259 		sc->sc_nsupmedia = 1;
260 		sc->sc_defaultmedia = supmedia;
261 	}
262 
263 	lesc->sc_ih = mca_intr_establish(ma->ma_mc, irq, IPL_NET,
264 			le_mca_intredge, sc);
265 	if (lesc->sc_ih == NULL) {
266 		printf("%s: couldn't establish interrupt handler\n",
267 			sc->sc_dev.dv_xname);
268 		return;
269 	}
270 
271 	printf("%s", sc->sc_dev.dv_xname);
272 	am7990_config(&lesc->sc_am7990);
273 }
274 
275 /*
276  * Controller interrupt.
277  */
278 int
279 le_mca_intredge(arg)
280 	void *arg;
281 {
282 	/*
283 	 * We could check the IRQ bit of LE_PORT, but it seems to be unset
284 	 * at this time anyway.
285 	 */
286 
287 	if (am7990_intr(arg) == 0);
288 		return (0);
289 	for(;;)
290 		if (am7990_intr(arg) == 0)
291 			return (1);
292 }
293 
294 /*
295  * Push a value to LANCE controller.
296  */
297 static __inline void
298 le_mca_wrreg(sc, val, type)
299 	struct le_mca_softc *sc;
300 	int val, type;
301 {
302 	/*
303 	 * This follows steps in SKNET Personal/MC2+ docs:
304 	 * 1. write reg. number to LANCE register
305 	 * 2. write value RESET | type to port
306 	 * 3. flag REGDO
307 	 * 4. wait until REGREQ is cleared
308 	 */
309 	if ((type & REGREAD) == 0)
310 		bus_space_write_2(sc->sc_memt, sc->sc_memh, LE_LANCEREG, val);
311 	bus_space_write_1(sc->sc_memt, sc->sc_memh, LE_PORT,
312 		RESET | type);
313 	bus_space_write_1(sc->sc_memt, sc->sc_memh, LE_REGIO,
314 		REGDO);
315 	/* Delay here doesn't seem to be necessary */
316 	/* delay(1);  */
317 	while(bus_space_read_1(sc->sc_memt, sc->sc_memh, LE_PORT) & REGREQ)
318 		;
319 }
320 
321 static void
322 le_mca_wrcsr(sc, port, val)
323 	struct lance_softc *sc;
324 	u_int16_t port, val;
325 {
326 	struct le_mca_softc *lsc = (struct le_mca_softc *)sc;
327 
328 	le_mca_set_RAP(lsc, port);
329 	le_mca_wrreg(lsc, val, RDATA | REGWRITE);
330 }
331 
332 static u_int16_t
333 le_mca_rdcsr(sc, port)
334 	struct lance_softc *sc;
335 	u_int16_t port;
336 {
337 	struct le_mca_softc *lsc = (struct le_mca_softc *)sc;
338 
339 	le_mca_set_RAP(lsc, port);
340 	le_mca_wrreg(lsc, 0, RDATA | REGREAD);
341 
342 	return (bus_space_read_2(lsc->sc_memt, lsc->sc_memh, LE_LANCEREG));
343 }
344 
345 static void
346 le_mca_hwreset(sc)
347 	struct lance_softc *sc;
348 {
349 	struct le_mca_softc *lsc = (struct le_mca_softc *)sc;
350 
351 	bus_space_write_1(lsc->sc_memt, lsc->sc_memh, LE_PORT, 0);
352 	delay(5);	/* Delay >= 5 microseconds */
353 	bus_space_write_1(lsc->sc_memt, lsc->sc_memh, LE_PORT, RESET);
354 }
355 
356 static void
357 le_mca_copytobuf(struct lance_softc *sc, void *from, int boff, int len)
358 {
359 	struct le_mca_softc *dsc = (void *) sc;
360 
361 	bus_space_write_region_1(dsc->sc_memt, dsc->sc_memh, boff,
362 	    from, len);
363 }
364 
365 static void
366 le_mca_copyfrombuf(struct lance_softc *sc, void *to, int boff, int len)
367 {
368 	struct le_mca_softc *dsc = (void *) sc;
369 
370 	bus_space_read_region_1(dsc->sc_memt, dsc->sc_memh, boff,
371 	    to, len);
372 }
373 
374 static void
375 le_mca_zerobuf(struct lance_softc *sc, int boff, int len)
376 {
377 	struct le_mca_softc *dsc = (void *) sc;
378 
379 	bus_space_set_region_1(dsc->sc_memt, dsc->sc_memh, boff,
380 	    0x00, len);
381 }
382