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