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