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