xref: /netbsd-src/sys/dev/mca/if_le_mca.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: if_le_mca.c,v 1.7 2003/10/25 18:37:03 christos 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.7 2003/10/25 18:37:03 christos 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 	default:
218 		printf("%s: unknown product %d\n", sc->sc_dev.dv_xname,
219 		    ma->ma_id);
220 		return;
221 	}
222 
223 	lesc->sc_memt = ma->ma_memt;
224 
225 	if (bus_space_map(lesc->sc_memt, membase, LE_MCA_MEMSIZE,
226 		0, &lesc->sc_memh)) {
227 		printf("%s: can't map memory\n", sc->sc_dev.dv_xname);
228 		return;
229 	}
230 
231 	printf(" slot %d irq %d: SKNET %s Ethernet\n",
232 		ma->ma_slot + 1, irq, typestr);
233 
234 	/*
235 	 * Extract the physical MAC address from the ROM.
236 	 */
237 	for (i = 0; i < ETHER_ADDR_LEN; i++)
238 		sc->sc_enaddr[i] = bus_space_read_1(lesc->sc_memt,
239 				lesc->sc_memh, LE_PROMOFF + i*2);
240 
241 	sc->sc_conf3 = LE_C3_ACON;
242 	sc->sc_addr = 0;
243 	sc->sc_memsize = LE_MCA_RAMSIZE;
244 
245 	sc->sc_copytodesc = le_mca_copytobuf;
246 	sc->sc_copyfromdesc = le_mca_copyfrombuf;
247 	sc->sc_copytobuf = le_mca_copytobuf;
248 	sc->sc_copyfrombuf = le_mca_copyfrombuf;
249 	sc->sc_zerobuf = le_mca_zerobuf;
250 
251 	sc->sc_rdcsr = le_mca_rdcsr;
252 	sc->sc_wrcsr = le_mca_wrcsr;
253 	sc->sc_hwinit = NULL;
254 
255 	sc->sc_hwreset = le_mca_hwreset;
256 
257 	/*
258 	 * This is merely cosmetic since it's not possible to switch
259 	 * the media anyway, even for MC2+.
260 	 */
261 	if (supmedia != 0) {
262 		sc->sc_supmedia = &supmedia;
263 		sc->sc_nsupmedia = 1;
264 		sc->sc_defaultmedia = supmedia;
265 	}
266 
267 	lesc->sc_ih = mca_intr_establish(ma->ma_mc, irq, IPL_NET,
268 			le_mca_intredge, sc);
269 	if (lesc->sc_ih == NULL) {
270 		printf("%s: couldn't establish interrupt handler\n",
271 			sc->sc_dev.dv_xname);
272 		return;
273 	}
274 
275 	printf("%s", sc->sc_dev.dv_xname);
276 	am7990_config(&lesc->sc_am7990);
277 }
278 
279 /*
280  * Controller interrupt.
281  */
282 int
283 le_mca_intredge(arg)
284 	void *arg;
285 {
286 	/*
287 	 * We could check the IRQ bit of LE_PORT, but it seems to be unset
288 	 * at this time anyway.
289 	 */
290 
291 	if (am7990_intr(arg) == 0);
292 		return (0);
293 	for(;;)
294 		if (am7990_intr(arg) == 0)
295 			return (1);
296 }
297 
298 /*
299  * Push a value to LANCE controller.
300  */
301 static __inline void
302 le_mca_wrreg(sc, val, type)
303 	struct le_mca_softc *sc;
304 	int val, type;
305 {
306 	/*
307 	 * This follows steps in SKNET Personal/MC2+ docs:
308 	 * 1. write reg. number to LANCE register
309 	 * 2. write value RESET | type to port
310 	 * 3. flag REGDO
311 	 * 4. wait until REGREQ is cleared
312 	 */
313 	if ((type & REGREAD) == 0)
314 		bus_space_write_2(sc->sc_memt, sc->sc_memh, LE_LANCEREG, val);
315 	bus_space_write_1(sc->sc_memt, sc->sc_memh, LE_PORT,
316 		RESET | type);
317 	bus_space_write_1(sc->sc_memt, sc->sc_memh, LE_REGIO,
318 		REGDO);
319 	/* Delay here doesn't seem to be necessary */
320 	/* delay(1);  */
321 	while(bus_space_read_1(sc->sc_memt, sc->sc_memh, LE_PORT) & REGREQ)
322 		;
323 }
324 
325 static void
326 le_mca_wrcsr(sc, port, val)
327 	struct lance_softc *sc;
328 	u_int16_t port, val;
329 {
330 	struct le_mca_softc *lsc = (struct le_mca_softc *)sc;
331 
332 	le_mca_set_RAP(lsc, port);
333 	le_mca_wrreg(lsc, val, RDATA | REGWRITE);
334 }
335 
336 static u_int16_t
337 le_mca_rdcsr(sc, port)
338 	struct lance_softc *sc;
339 	u_int16_t port;
340 {
341 	struct le_mca_softc *lsc = (struct le_mca_softc *)sc;
342 
343 	le_mca_set_RAP(lsc, port);
344 	le_mca_wrreg(lsc, 0, RDATA | REGREAD);
345 
346 	return (bus_space_read_2(lsc->sc_memt, lsc->sc_memh, LE_LANCEREG));
347 }
348 
349 static void
350 le_mca_hwreset(sc)
351 	struct lance_softc *sc;
352 {
353 	struct le_mca_softc *lsc = (struct le_mca_softc *)sc;
354 
355 	bus_space_write_1(lsc->sc_memt, lsc->sc_memh, LE_PORT, 0);
356 	delay(5);	/* Delay >= 5 microseconds */
357 	bus_space_write_1(lsc->sc_memt, lsc->sc_memh, LE_PORT, RESET);
358 }
359 
360 static void
361 le_mca_copytobuf(struct lance_softc *sc, void *from, int boff, int len)
362 {
363 	struct le_mca_softc *dsc = (void *) sc;
364 
365 	bus_space_write_region_1(dsc->sc_memt, dsc->sc_memh, boff,
366 	    from, len);
367 }
368 
369 static void
370 le_mca_copyfrombuf(struct lance_softc *sc, void *to, int boff, int len)
371 {
372 	struct le_mca_softc *dsc = (void *) sc;
373 
374 	bus_space_read_region_1(dsc->sc_memt, dsc->sc_memh, boff,
375 	    to, len);
376 }
377 
378 static void
379 le_mca_zerobuf(struct lance_softc *sc, int boff, int len)
380 {
381 	struct le_mca_softc *dsc = (void *) sc;
382 
383 	bus_space_set_region_1(dsc->sc_memt, dsc->sc_memh, boff,
384 	    0x00, len);
385 }
386