xref: /netbsd-src/sys/dev/mca/if_le_mca.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: if_le_mca.c,v 1.16 2007/10/19 12:00:35 ad 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.16 2007/10/19 12:00:35 ad 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 <sys/cpu.h>
71 #include <sys/intr.h>
72 #include <sys/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(struct device *, struct cfdata *, void *);
86 void	le_mca_attach(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(struct lance_softc *, u_int16_t, u_int16_t);
97 static u_int16_t le_mca_rdcsr(struct lance_softc *, u_int16_t);
98 static void le_mca_hwreset(struct lance_softc *);
99 static int le_mca_intredge(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(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,
125     void *aux)
126 {
127 	struct mca_attach_args *ma = aux;
128 
129 	switch(ma->ma_id) {
130 	case MCA_PRODUCT_SKNETPER:
131 	case MCA_PRODUCT_SKNETG:
132 		return (1);
133 	}
134 
135 	return (0);
136 }
137 
138 void
139 le_mca_attach(struct device *parent, struct device *self, void *aux)
140 {
141 	struct le_mca_softc *lesc = device_private(self);
142 	struct lance_softc *sc = &lesc->sc_am7990.lsc;
143 	struct mca_attach_args *ma = aux;
144 	int i, pos2, pos3, pos4, irq, membase, supmedia=0;
145 	const char *typestr;
146 
147 	/*
148 	 * SKNET Personal:
149 	 *
150 	 * POS register 2: (adf pos0)
151 	 *
152 	 * 7 6 5 4 3 2 1 0
153 	 *       | \___/ \__ enable: 0=adapter disabled, 1=adapter enabled
154 	 *        \    \____ Memory: 0xC0000-0xC3FFF + XX*0x4000
155 	 *         \________ IRQ: 0=10 1=11
156 	 *
157 	 *
158 	 * SKNET MC+:
159 	 * POS register 2: (adf pos0)
160 	 *
161 	 * 7 6 5 4 3 2 1 0
162 	 *       \___/ \ \__ enable: 0=adapter disabled, 1=adapter enabled
163 	 *           \  \___ BootEPROM disable
164 	 *            \_____ BootEPROM start address: 0xC0000 + XX*0x4000
165 	 *
166 	 * POS register 3: (adf pos1)
167 	 *
168 	 * 7 6 5 4 3 2 1 0
169 	 * 0 0 1 1 \_____/
170 	 *               \__ RAM: 0xC0000 + XX*0x4000
171 	 *
172 	 * POS register 4: (adf pos2)
173 	 *
174 	 * 7 6 5 4 3 2 1 0
175 	 * \_/     \_/ \_/
176 	 *   \       \   \__ Need to be reset to 0 0 after boot
177 	 *    \       \_____ IRQ: 00=3 01=5 10=10 11=11
178 	 *     \____________ Medium: 00=BNC 01=UTP 10=AUI 11=not allowed
179 	 */
180 
181 	switch (ma->ma_id) {
182 	case MCA_PRODUCT_SKNETPER:
183 		typestr = "Personal MC2";
184 
185 		pos2 = mca_conf_read(ma->ma_mc, ma->ma_slot, 2);
186 		irq = (pos2 & (1<<4)) ? 11 : 10;
187 		membase = 0xc0000 + ((pos2 & 0x0e) >> 1) * 0x4000;
188 		break;
189 	case MCA_PRODUCT_SKNETG:
190 		typestr = "MC2+";
191 
192 		/*
193 		 * SKNET MC+ needs the driver to clear 0, 1 bits of pos4
194 		 * and explicitly set the enable bit. Somebody at Syskonnect
195 		 * was obviously misguided when the card was designed ...
196 		 */
197 		pos3 = mca_conf_read(ma->ma_mc, ma->ma_slot, 3);
198 		pos4 = mca_conf_read(ma->ma_mc, ma->ma_slot, 4);
199 		if ((pos4 & 0x03) != 0) {
200 			/* clear the bits 0, 1 */
201 			mca_conf_write(ma->ma_mc, ma->ma_slot, 4,
202 				pos4 & ~0x03);
203 		}
204 
205 		pos2 = mca_conf_read(ma->ma_mc, ma->ma_slot, 2);
206 		if ((pos2 & 0x01) == 0) {
207 			/* enable the card */
208 			mca_conf_write(ma->ma_mc, ma->ma_slot, 2, pos2 | 0x01);
209 		}
210 
211 		/* get irq and memory base */
212 		irq = sknet_mcp_irq[(pos4 & 0x0c) >> 2];
213 		membase = 0xc0000 + ((pos3 & 0x0f) * 0x4000);
214 
215 		/* Get configured media type */
216 		supmedia = sknet_mcp_media[(pos4 & 0xc0) >> 6];
217 		break;
218 	default:
219 		printf("%s: unknown product %d\n", sc->sc_dev.dv_xname,
220 		    ma->ma_id);
221 		return;
222 	}
223 
224 	lesc->sc_memt = ma->ma_memt;
225 
226 	if (bus_space_map(lesc->sc_memt, membase, LE_MCA_MEMSIZE,
227 		0, &lesc->sc_memh)) {
228 		printf("%s: can't map memory\n", sc->sc_dev.dv_xname);
229 		return;
230 	}
231 
232 	printf(" slot %d irq %d: SKNET %s Ethernet\n",
233 		ma->ma_slot + 1, irq, typestr);
234 
235 	/*
236 	 * Extract the physical MAC address from the ROM.
237 	 */
238 	for (i = 0; i < ETHER_ADDR_LEN; i++)
239 		sc->sc_enaddr[i] = bus_space_read_1(lesc->sc_memt,
240 				lesc->sc_memh, LE_PROMOFF + i*2);
241 
242 	sc->sc_conf3 = LE_C3_ACON;
243 	sc->sc_addr = 0;
244 	sc->sc_memsize = LE_MCA_RAMSIZE;
245 
246 	sc->sc_copytodesc = le_mca_copytobuf;
247 	sc->sc_copyfromdesc = le_mca_copyfrombuf;
248 	sc->sc_copytobuf = le_mca_copytobuf;
249 	sc->sc_copyfrombuf = le_mca_copyfrombuf;
250 	sc->sc_zerobuf = le_mca_zerobuf;
251 
252 	sc->sc_rdcsr = le_mca_rdcsr;
253 	sc->sc_wrcsr = le_mca_wrcsr;
254 	sc->sc_hwinit = NULL;
255 
256 	sc->sc_hwreset = le_mca_hwreset;
257 
258 	/*
259 	 * This is merely cosmetic since it's not possible to switch
260 	 * the media anyway, even for MC2+.
261 	 */
262 	if (supmedia != 0) {
263 		sc->sc_supmedia = &supmedia;
264 		sc->sc_nsupmedia = 1;
265 		sc->sc_defaultmedia = supmedia;
266 	}
267 
268 	lesc->sc_ih = mca_intr_establish(ma->ma_mc, irq, IPL_NET,
269 			le_mca_intredge, sc);
270 	if (lesc->sc_ih == NULL) {
271 		printf("%s: couldn't establish interrupt handler\n",
272 			sc->sc_dev.dv_xname);
273 		return;
274 	}
275 
276 	printf("%s", sc->sc_dev.dv_xname);
277 	am7990_config(&lesc->sc_am7990);
278 }
279 
280 /*
281  * Controller interrupt.
282  */
283 int
284 le_mca_intredge(arg)
285 	void *arg;
286 {
287 	/*
288 	 * We could check the IRQ bit of LE_PORT, but it seems to be unset
289 	 * at this time anyway.
290 	 */
291 
292 	if (am7990_intr(arg) == 0)
293 		return (0);
294 	for(;;)
295 		if (am7990_intr(arg) == 0)
296 			return (1);
297 }
298 
299 /*
300  * Push a value to LANCE controller.
301  */
302 static inline void
303 le_mca_wrreg(sc, val, type)
304 	struct le_mca_softc *sc;
305 	int val, type;
306 {
307 	/*
308 	 * This follows steps in SKNET Personal/MC2+ docs:
309 	 * 1. write reg. number to LANCE register
310 	 * 2. write value RESET | type to port
311 	 * 3. flag REGDO
312 	 * 4. wait until REGREQ is cleared
313 	 */
314 	if ((type & REGREAD) == 0)
315 		bus_space_write_2(sc->sc_memt, sc->sc_memh, LE_LANCEREG, val);
316 	bus_space_write_1(sc->sc_memt, sc->sc_memh, LE_PORT,
317 		RESET | type);
318 	bus_space_write_1(sc->sc_memt, sc->sc_memh, LE_REGIO,
319 		REGDO);
320 	/* Delay here doesn't seem to be necessary */
321 	/* delay(1);  */
322 	while(bus_space_read_1(sc->sc_memt, sc->sc_memh, LE_PORT) & REGREQ)
323 		;
324 }
325 
326 static void
327 le_mca_wrcsr(sc, port, val)
328 	struct lance_softc *sc;
329 	u_int16_t port, val;
330 {
331 	struct le_mca_softc *lsc = (struct le_mca_softc *)sc;
332 
333 	le_mca_set_RAP(lsc, port);
334 	le_mca_wrreg(lsc, val, RDATA | REGWRITE);
335 }
336 
337 static u_int16_t
338 le_mca_rdcsr(sc, port)
339 	struct lance_softc *sc;
340 	u_int16_t port;
341 {
342 	struct le_mca_softc *lsc = (struct le_mca_softc *)sc;
343 
344 	le_mca_set_RAP(lsc, port);
345 	le_mca_wrreg(lsc, 0, RDATA | REGREAD);
346 
347 	return (bus_space_read_2(lsc->sc_memt, lsc->sc_memh, LE_LANCEREG));
348 }
349 
350 static void
351 le_mca_hwreset(sc)
352 	struct lance_softc *sc;
353 {
354 	struct le_mca_softc *lsc = (struct le_mca_softc *)sc;
355 
356 	bus_space_write_1(lsc->sc_memt, lsc->sc_memh, LE_PORT, 0);
357 	delay(5);	/* Delay >= 5 microseconds */
358 	bus_space_write_1(lsc->sc_memt, lsc->sc_memh, LE_PORT, RESET);
359 }
360 
361 static void
362 le_mca_copytobuf(struct lance_softc *sc, void *from, int boff, int len)
363 {
364 	struct le_mca_softc *dsc = (void *) sc;
365 
366 	bus_space_write_region_1(dsc->sc_memt, dsc->sc_memh, boff,
367 	    from, len);
368 }
369 
370 static void
371 le_mca_copyfrombuf(struct lance_softc *sc, void *to, int boff, int len)
372 {
373 	struct le_mca_softc *dsc = (void *) sc;
374 
375 	bus_space_read_region_1(dsc->sc_memt, dsc->sc_memh, boff,
376 	    to, len);
377 }
378 
379 static void
380 le_mca_zerobuf(struct lance_softc *sc, int boff, int len)
381 {
382 	struct le_mca_softc *dsc = (void *) sc;
383 
384 	bus_space_set_region_1(dsc->sc_memt, dsc->sc_memh, boff,
385 	    0x00, len);
386 }
387