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