xref: /netbsd-src/sys/dev/mca/if_le_mca.c (revision 8ac07aec990b9d2e483062509d0a9fa5b4f57cf2)
1 /*	$NetBSD: if_le_mca.c,v 1.17 2008/04/04 12:25:07 tsutsui 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.17 2008/04/04 12:25:07 tsutsui 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(device_t, cfdata_t, void *);
86 void	le_mca_attach(device_t, device_t, 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 *, uint16_t, uint16_t);
97 static uint16_t le_mca_rdcsr(struct lance_softc *, uint16_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_NEW(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(device_t parent, cfdata_t 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(device_t parent, device_t self, void *aux)
139 {
140 	struct le_mca_softc *lesc = device_private(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 	sc->sc_dev = self;
147 
148 	/*
149 	 * SKNET Personal:
150 	 *
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 	 *        \    \____ Memory: 0xC0000-0xC3FFF + XX*0x4000
156 	 *         \________ IRQ: 0=10 1=11
157 	 *
158 	 *
159 	 * SKNET MC+:
160 	 * POS register 2: (adf pos0)
161 	 *
162 	 * 7 6 5 4 3 2 1 0
163 	 *       \___/ \ \__ enable: 0=adapter disabled, 1=adapter enabled
164 	 *           \  \___ BootEPROM disable
165 	 *            \_____ BootEPROM start address: 0xC0000 + XX*0x4000
166 	 *
167 	 * POS register 3: (adf pos1)
168 	 *
169 	 * 7 6 5 4 3 2 1 0
170 	 * 0 0 1 1 \_____/
171 	 *               \__ RAM: 0xC0000 + XX*0x4000
172 	 *
173 	 * POS register 4: (adf pos2)
174 	 *
175 	 * 7 6 5 4 3 2 1 0
176 	 * \_/     \_/ \_/
177 	 *   \       \   \__ Need to be reset to 0 0 after boot
178 	 *    \       \_____ IRQ: 00=3 01=5 10=10 11=11
179 	 *     \____________ Medium: 00=BNC 01=UTP 10=AUI 11=not allowed
180 	 */
181 
182 	switch (ma->ma_id) {
183 	case MCA_PRODUCT_SKNETPER:
184 		typestr = "Personal MC2";
185 
186 		pos2 = mca_conf_read(ma->ma_mc, ma->ma_slot, 2);
187 		irq = (pos2 & (1<<4)) ? 11 : 10;
188 		membase = 0xc0000 + ((pos2 & 0x0e) >> 1) * 0x4000;
189 		break;
190 	case MCA_PRODUCT_SKNETG:
191 		typestr = "MC2+";
192 
193 		/*
194 		 * SKNET MC+ needs the driver to clear 0, 1 bits of pos4
195 		 * and explicitly set the enable bit. Somebody at Syskonnect
196 		 * was obviously misguided when the card was designed ...
197 		 */
198 		pos3 = mca_conf_read(ma->ma_mc, ma->ma_slot, 3);
199 		pos4 = mca_conf_read(ma->ma_mc, ma->ma_slot, 4);
200 		if ((pos4 & 0x03) != 0) {
201 			/* clear the bits 0, 1 */
202 			mca_conf_write(ma->ma_mc, ma->ma_slot, 4,
203 				pos4 & ~0x03);
204 		}
205 
206 		pos2 = mca_conf_read(ma->ma_mc, ma->ma_slot, 2);
207 		if ((pos2 & 0x01) == 0) {
208 			/* enable the card */
209 			mca_conf_write(ma->ma_mc, ma->ma_slot, 2, pos2 | 0x01);
210 		}
211 
212 		/* get irq and memory base */
213 		irq = sknet_mcp_irq[(pos4 & 0x0c) >> 2];
214 		membase = 0xc0000 + ((pos3 & 0x0f) * 0x4000);
215 
216 		/* Get configured media type */
217 		supmedia = sknet_mcp_media[(pos4 & 0xc0) >> 6];
218 		break;
219 	default:
220 		aprint_error(": unknown product %d\n", 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 		aprint_error(": can't map memory\n");
229 		return;
230 	}
231 
232 	aprint_normal(" 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 		aprint_error_dev(self,
272 		    "couldn't establish interrupt handler\n");
273 		return;
274 	}
275 
276 	aprint_normal("%s", device_xname(self));
277 	am7990_config(&lesc->sc_am7990);
278 }
279 
280 /*
281  * Controller interrupt.
282  */
283 int
284 le_mca_intredge(void *arg)
285 {
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(struct le_mca_softc *sc, int val, int type)
304 {
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(struct lance_softc *sc, uint16_t port, uint16_t val)
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, val, RDATA | REGWRITE);
332 }
333 
334 static uint16_t
335 le_mca_rdcsr(struct lance_softc *sc, uint16_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(struct lance_softc *sc)
347 {
348 	struct le_mca_softc *lsc = (struct le_mca_softc *)sc;
349 
350 	bus_space_write_1(lsc->sc_memt, lsc->sc_memh, LE_PORT, 0);
351 	delay(5);	/* Delay >= 5 microseconds */
352 	bus_space_write_1(lsc->sc_memt, lsc->sc_memh, LE_PORT, RESET);
353 }
354 
355 static void
356 le_mca_copytobuf(struct lance_softc *sc, void *from, int boff, int len)
357 {
358 	struct le_mca_softc *lsc = (struct le_mca_softc *)sc;
359 
360 	bus_space_write_region_1(lsc->sc_memt, lsc->sc_memh, boff,
361 	    from, len);
362 }
363 
364 static void
365 le_mca_copyfrombuf(struct lance_softc *sc, void *to, int boff, int len)
366 {
367 	struct le_mca_softc *lsc = (struct le_mca_softc *)sc;
368 
369 	bus_space_read_region_1(lsc->sc_memt, lsc->sc_memh, boff,
370 	    to, len);
371 }
372 
373 static void
374 le_mca_zerobuf(struct lance_softc *sc, int boff, int len)
375 {
376 	struct le_mca_softc *lsc = (struct le_mca_softc *)sc;
377 
378 	bus_space_set_region_1(lsc->sc_memt, lsc->sc_memh, boff,
379 	    0x00, len);
380 }
381