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