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