1 /* $NetBSD: if_ie_obio.c,v 1.7 2003/07/15 03:36:12 lukem Exp $ */ 2 3 /*- 4 * Copyright (c) 1997 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Paul Kranenburg and Matt Fredette. 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 * Copyright (c) 1995 Charles D. Cranor 41 * All rights reserved. 42 * 43 * Redistribution and use in source and binary forms, with or without 44 * modification, are permitted provided that the following conditions 45 * are met: 46 * 1. Redistributions of source code must retain the above copyright 47 * notice, this list of conditions and the following disclaimer. 48 * 2. Redistributions in binary form must reproduce the above copyright 49 * notice, this list of conditions and the following disclaimer in the 50 * documentation and/or other materials provided with the distribution. 51 * 3. All advertising materials mentioning features or use of this software 52 * must display the following acknowledgement: 53 * This product includes software developed by Charles D. Cranor. 54 * 4. The name of the author may not be used to endorse or promote products 55 * derived from this software without specific prior written permission. 56 * 57 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 58 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 59 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 60 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 61 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 62 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 63 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 64 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 65 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 66 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 67 */ 68 69 70 71 /* 72 * Sun2 OBIO front-end for the Intel 82586 Ethernet driver 73 * 74 * Converted to SUN ie driver by Charles D. Cranor, 75 * October 1994, January 1995. 76 */ 77 78 /* 79 * The i82586 is a very painful chip, found in sun[23]'s, sun-4/100's 80 * sun-4/200's, and VME based suns. The byte order is all wrong for a 81 * SUN, making life difficult. Programming this chip is mostly the same, 82 * but certain details differ from system to system. This driver is 83 * written so that different "ie" interfaces can be controled by the same 84 * driver. 85 */ 86 87 #include <sys/cdefs.h> 88 __KERNEL_RCSID(0, "$NetBSD: if_ie_obio.c,v 1.7 2003/07/15 03:36:12 lukem Exp $"); 89 90 #include <sys/param.h> 91 #include <sys/systm.h> 92 #include <sys/errno.h> 93 #include <sys/device.h> 94 #include <sys/malloc.h> 95 #include <sys/protosw.h> 96 #include <sys/socket.h> 97 98 #include <net/if.h> 99 #include <net/if_types.h> 100 #include <net/if_dl.h> 101 #include <net/if_media.h> 102 #include <net/if_ether.h> 103 104 #include <uvm/uvm_extern.h> 105 106 #include <machine/bus.h> 107 #include <machine/intr.h> 108 #include <machine/autoconf.h> 109 #include <machine/idprom.h> 110 111 #include <dev/ic/i82586reg.h> 112 #include <dev/ic/i82586var.h> 113 114 /* 115 * the on-board interface 116 */ 117 struct ieob { 118 u_char obctrl; 119 }; 120 #define IEOB_NORSET 0x80 /* don't reset the board */ 121 #define IEOB_ONAIR 0x40 /* put us on the air */ 122 #define IEOB_ATTEN 0x20 /* attention! */ 123 #define IEOB_IENAB 0x10 /* interrupt enable */ 124 #define IEOB_XXXXX 0x08 /* free bit */ 125 #define IEOB_XCVRL2 0x04 /* level 2 transceiver? */ 126 #define IEOB_BUSERR 0x02 /* bus error */ 127 #define IEOB_INT 0x01 /* interrupt */ 128 129 #define IEOB_ADBASE 0x000000 /* KVA base addr of 24 bit address space */ 130 131 132 static void ie_obreset __P((struct ie_softc *, int)); 133 static void ie_obattend __P((struct ie_softc *, int)); 134 static void ie_obrun __P((struct ie_softc *)); 135 136 int ie_obio_match __P((struct device *, struct cfdata *, void *)); 137 void ie_obio_attach __P((struct device *, struct device *, void *)); 138 139 CFATTACH_DECL(ie_obio, sizeof(struct ie_softc), 140 ie_obio_match, ie_obio_attach, NULL, NULL); 141 142 /* Supported media */ 143 static int media[] = { 144 IFM_ETHER | IFM_10_2, 145 }; 146 #define NMEDIA (sizeof(media) / sizeof(media[0])) 147 148 149 /* 150 * OBIO ie support routines 151 */ 152 void 153 ie_obreset(sc, what) 154 struct ie_softc *sc; 155 { 156 volatile struct ieob *ieo = (struct ieob *) sc->sc_reg; 157 ieo->obctrl = 0; 158 delay(100); /* XXX could be shorter? */ 159 ieo->obctrl = IEOB_NORSET; 160 } 161 void 162 ie_obattend(sc, why) 163 struct ie_softc *sc; 164 int why; 165 { 166 volatile struct ieob *ieo = (struct ieob *) sc->sc_reg; 167 168 ieo->obctrl |= IEOB_ATTEN; /* flag! */ 169 ieo->obctrl &= ~IEOB_ATTEN; /* down. */ 170 } 171 172 void 173 ie_obrun(sc) 174 struct ie_softc *sc; 175 { 176 volatile struct ieob *ieo = (struct ieob *) sc->sc_reg; 177 178 ieo->obctrl |= (IEOB_ONAIR|IEOB_IENAB|IEOB_NORSET); 179 } 180 181 void ie_obio_memcopyin __P((struct ie_softc *, void *, int, size_t)); 182 void ie_obio_memcopyout __P((struct ie_softc *, const void *, int, size_t)); 183 184 /* 185 * Copy board memory to kernel. 186 */ 187 void 188 ie_obio_memcopyin(sc, p, offset, size) 189 struct ie_softc *sc; 190 void *p; 191 int offset; 192 size_t size; 193 { 194 bus_space_copyin(sc->bt, sc->bh, offset, p, size); 195 } 196 197 /* 198 * Copy from kernel space to naord memory. 199 */ 200 void 201 ie_obio_memcopyout(sc, p, offset, size) 202 struct ie_softc *sc; 203 const void *p; 204 int offset; 205 size_t size; 206 { 207 bus_space_copyout(sc->bt, sc->bh, offset, p, size); 208 } 209 210 /* read a 16-bit value at BH offset */ 211 u_int16_t ie_obio_read16 __P((struct ie_softc *, int offset)); 212 /* write a 16-bit value at BH offset */ 213 void ie_obio_write16 __P((struct ie_softc *, int offset, u_int16_t value)); 214 void ie_obio_write24 __P((struct ie_softc *, int offset, int addr)); 215 216 u_int16_t 217 ie_obio_read16(sc, offset) 218 struct ie_softc *sc; 219 int offset; 220 { 221 u_int16_t v = bus_space_read_2(sc->bt, sc->bh, offset); 222 return (((v&0xff)<<8) | ((v>>8)&0xff)); 223 } 224 225 void 226 ie_obio_write16(sc, offset, v) 227 struct ie_softc *sc; 228 int offset; 229 u_int16_t v; 230 { 231 v = (((v&0xff)<<8) | ((v>>8)&0xff)); 232 bus_space_write_2(sc->bt, sc->bh, offset, v); 233 } 234 235 void 236 ie_obio_write24(sc, offset, addr) 237 struct ie_softc *sc; 238 int offset; 239 int addr; 240 { 241 u_char *f = (u_char *)&addr; 242 u_int16_t v0, v1; 243 u_char *t; 244 245 t = (u_char *)&v0; 246 t[0] = f[3]; t[1] = f[2]; 247 bus_space_write_2(sc->bt, sc->bh, offset, v0); 248 249 t = (u_char *)&v1; 250 t[0] = f[1]; t[1] = 0; 251 bus_space_write_2(sc->bt, sc->bh, offset+2, v1); 252 } 253 254 int 255 ie_obio_match(parent, cf, aux) 256 struct device *parent; 257 struct cfdata *cf; 258 void *aux; 259 { 260 struct obio_attach_args *oba = aux; 261 bus_space_handle_t bh; 262 int matched; 263 u_int8_t ctrl; 264 265 /* No default obio address. */ 266 if (oba->oba_paddr == -1) 267 return(0); 268 269 /* Make sure there is something there... */ 270 if (bus_space_map(oba->oba_bustag, oba->oba_paddr, sizeof(struct ieob), 271 0, &bh)) 272 return (0); 273 matched = (!bus_space_poke_1(oba->oba_bustag, bh, 0, IEOB_NORSET) && 274 !bus_space_peek_1(oba->oba_bustag, bh, 0, &ctrl) && 275 (ctrl & (IEOB_ONAIR|IEOB_IENAB)) == 0); 276 bus_space_unmap(oba->oba_bustag, bh, sizeof(struct ieob)); 277 if (!matched) 278 return (0); 279 280 /* Default interrupt priority. */ 281 if (oba->oba_pri == -1) 282 oba->oba_pri = 3; 283 284 return (1); 285 } 286 287 void 288 ie_obio_attach(parent, self, aux) 289 struct device *parent; 290 struct device *self; 291 void *aux; 292 { 293 struct obio_attach_args *oba = aux; 294 struct ie_softc *sc = (void *) self; 295 bus_dma_tag_t dmatag = oba->oba_dmatag; 296 bus_space_handle_t bh; 297 bus_dma_segment_t seg; 298 int rseg; 299 int error; 300 paddr_t pa; 301 struct intrhand *ih; 302 bus_size_t msize; 303 u_long iebase; 304 u_int8_t myaddr[ETHER_ADDR_LEN]; 305 306 sc->bt = oba->oba_bustag; 307 308 sc->hwreset = ie_obreset; 309 sc->chan_attn = ie_obattend; 310 sc->hwinit = ie_obrun; 311 sc->memcopyout = ie_obio_memcopyout; 312 sc->memcopyin = ie_obio_memcopyin; 313 314 sc->ie_bus_barrier = NULL; 315 sc->ie_bus_read16 = ie_obio_read16; 316 sc->ie_bus_write16 = ie_obio_write16; 317 sc->ie_bus_write24 = ie_obio_write24; 318 sc->sc_msize = msize = 65536; /* XXX */ 319 320 if (bus_space_map(oba->oba_bustag, oba->oba_paddr, sizeof(struct ieob), 321 0, &bh)) 322 panic("ie_obio_attach: can't map regs"); 323 sc->sc_reg = (void *)bh; 324 325 /* 326 * Allocate control & buffer memory. 327 */ 328 if ((error = bus_dmamap_create(dmatag, msize, 1, msize, 0, 329 BUS_DMA_NOWAIT|BUS_DMA_24BIT, 330 &sc->sc_dmamap)) != 0) { 331 printf("%s: DMA map create error %d\n", 332 sc->sc_dev.dv_xname, error); 333 return; 334 } 335 if ((error = bus_dmamem_alloc(dmatag, msize, 64*1024, 0, 336 &seg, 1, &rseg, 337 BUS_DMA_NOWAIT | BUS_DMA_24BIT)) != 0) { 338 printf("%s: DMA memory allocation error %d\n", 339 self->dv_xname, error); 340 return; 341 } 342 343 /* Map DMA buffer in CPU addressable space */ 344 if ((error = bus_dmamem_map(dmatag, &seg, rseg, msize, 345 (caddr_t *)&sc->sc_maddr, 346 BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) { 347 printf("%s: DMA buffer map error %d\n", 348 sc->sc_dev.dv_xname, error); 349 bus_dmamem_free(dmatag, &seg, rseg); 350 return; 351 } 352 353 /* Load the segment */ 354 if ((error = bus_dmamap_load(dmatag, sc->sc_dmamap, 355 sc->sc_maddr, msize, NULL, 356 BUS_DMA_NOWAIT)) != 0) { 357 printf("%s: DMA buffer map load error %d\n", 358 sc->sc_dev.dv_xname, error); 359 bus_dmamem_unmap(dmatag, sc->sc_maddr, msize); 360 bus_dmamem_free(dmatag, &seg, rseg); 361 return; 362 } 363 364 w16zero(sc->sc_maddr, msize); 365 sc->bh = (bus_space_handle_t)(sc->sc_maddr); 366 367 /* 368 * The i82586's 24-bit address space maps to all of 369 * KVA space (). In addition, the SCP must appear 370 * at IE_SCP_ADDR within the 24-bit address space, 371 * i.e. at KVA IEOB_ADBASE+IE_SCP_ADDR, at the very top of 372 * kernel space. We double-map this last page to the first 373 * page (starting at `maddr') of the memory we allocate to the chip. 374 * (a side-effect of this double-map is that the ISCP and SCB 375 * structures also get aliased there, but we ignore this). The 376 * first page at `maddr' is only used for ISCP, SCB and the aliased 377 * SCP; the actual buffers start at maddr+PAGE_SIZE. 378 * 379 * In a picture: 380 381 |---//--- ISCP-SCB-----scp-|--//- buffers -//-|... |iscp-scb-----SCP-| 382 | | | | | | | 383 | |<---PAGE_SIZE-->| | |<--PAGE_SIZE-+-->| 384 | |<------------- msize ------------->| | ^ | 385 | | | | 386 | \@maddr (last page dbl mapped) 387 | | 388 \@IEOB_ADBASE @IEOB_ADBASE+IE_SCP_ADDR-+ 389 390 * 391 */ 392 393 /* Double map the SCP */ 394 if (pmap_extract(pmap_kernel(), (vaddr_t)sc->sc_maddr, &pa) == FALSE) 395 panic("ie pmap_extract"); 396 397 pmap_enter(pmap_kernel(), m68k_trunc_page(IEOB_ADBASE+IE_SCP_ADDR), 398 pa | PMAP_NC /*| PMAP_IOC*/, 399 VM_PROT_READ | VM_PROT_WRITE, PMAP_WIRED); 400 401 /* Map iscp at location 0 (relative to `maddr') */ 402 sc->iscp = 0; 403 404 /* scb follows iscp */ 405 sc->scb = IE_ISCP_SZ; 406 407 /* scp is at the fixed location IE_SCP_ADDR (modulo the page size) */ 408 sc->scp = IE_SCP_ADDR & PGOFSET; 409 410 /* Calculate the 24-bit base of i82586 operations */ 411 iebase = (u_long)sc->sc_dmamap->dm_segs[0].ds_addr - 412 (u_long)IEOB_ADBASE; 413 ie_obio_write16(sc, IE_ISCP_SCB(sc->iscp), sc->scb); 414 ie_obio_write24(sc, IE_ISCP_BASE(sc->iscp), iebase); 415 ie_obio_write24(sc, IE_SCP_ISCP(sc->scp), iebase + sc->iscp); 416 417 /* 418 * Rest of first page is unused (wasted!); the other pages 419 * are used for buffers. 420 */ 421 sc->buf_area = PAGE_SIZE; 422 sc->buf_area_sz = msize - PAGE_SIZE; 423 424 if (i82586_proberam(sc) == 0) { 425 printf(": memory probe failed\n"); 426 return; 427 } 428 429 idprom_etheraddr(myaddr); 430 i82586_attach(sc, "onboard", myaddr, media, NMEDIA, media[0]); 431 432 /* Establish interrupt channel */ 433 ih = bus_intr_establish(oba->oba_bustag, 434 oba->oba_pri, IPL_NET, 0, 435 i82586_intr, sc); 436 } 437