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