1 /* $NetBSD: if_le_oioc.c,v 1.2 2010/01/19 22:06:22 pooka Exp $ */ 2 3 /* 4 * Copyright (c) 2009 Stephen M. Rumble 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. The name of the author may not be used to endorse or promote products 13 * derived from this software without specific prior written permission. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __KERNEL_RCSID(0, "$NetBSD: if_le_oioc.c,v 1.2 2010/01/19 22:06:22 pooka Exp $"); 29 30 #include "opt_inet.h" 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/mbuf.h> 35 #include <sys/syslog.h> 36 #include <sys/socket.h> 37 #include <sys/device.h> 38 39 #include <uvm/uvm.h> // for uvm_pglistalloc 40 41 #include <net/if.h> 42 #include <net/if_ether.h> 43 #include <net/if_media.h> 44 45 #ifdef INET 46 #include <netinet/in.h> 47 #include <netinet/if_inarp.h> 48 #endif 49 50 #include <machine/cpu.h> 51 #include <machine/machtype.h> 52 53 #include <dev/ic/lancereg.h> 54 #include <dev/ic/lancevar.h> 55 #include <dev/ic/am7990reg.h> 56 #include <dev/ic/am7990var.h> 57 58 #include <dev/arcbios/arcbios.h> 59 #include <dev/arcbios/arcbiosvar.h> 60 61 #include <sgimips/ioc/oiocvar.h> 62 #include <sgimips/ioc/oiocreg.h> 63 64 #include <mips/include/cache.h> 65 66 #ifndef OIOC_LANCE_NPAGES 67 #define OIOC_LANCE_NPAGES 64 /* 256KB */ 68 #endif 69 70 #if OIOC_LANCE_NPAGES > OIOC_ENET_NPGMAPS 71 #error OIOC_LANCE_NPAGES > OIOC_ENET_NPGMAPS (512) 72 #endif 73 74 /* 75 * Ethernet software status per interface. 76 * The real stuff is in dev/ic/am7990var.h 77 * The really real stuff is in dev/ic/lancevar.h 78 * 79 * Lance is somewhat nasty MI code. We basically get: 80 * struct le_softc { 81 * struct am7990_softc { 82 * struct lance_softc { 83 * device_t sc_dev; 84 * ... 85 * } 86 * } 87 * 88 * bus_space_tag ... 89 * } 90 * 91 * So, we can cast any three to any other three, plus sc_dev->dv_private points 92 * back at the top (i.e. to le_softc, am7990_softc and lance_softc). Bloody 93 * hell! 94 */ 95 struct le_softc { 96 struct am7990_softc sc_am7990; /* glue to MI code */ 97 98 bus_space_tag_t sc_st; 99 bus_space_handle_t sc_maph; /* ioc<->lance page map regs */ 100 bus_space_handle_t sc_rdph; /* lance rdp */ 101 bus_space_handle_t sc_raph; /* lance rap */ 102 }; 103 104 static int le_match(device_t, cfdata_t, void *); 105 static void le_attach(device_t, device_t, void *); 106 107 CFATTACH_DECL_NEW(le, sizeof(struct le_softc), 108 le_match, le_attach, NULL, NULL); 109 110 #if defined(_KERNEL_OPT) 111 #include "opt_ddb.h" 112 #endif 113 114 static void lewrcsr(struct lance_softc *, uint16_t, uint16_t); 115 static uint16_t lerdcsr(struct lance_softc *, uint16_t); 116 static void enaddr_aton(const char *, u_int8_t *); 117 118 static void 119 lewrcsr(struct lance_softc *sc, uint16_t port, uint16_t val) 120 { 121 struct le_softc *lesc = (struct le_softc *)sc; 122 bus_space_write_2(lesc->sc_st, lesc->sc_raph, 0, port); 123 bus_space_write_2(lesc->sc_st, lesc->sc_rdph, 0, val); 124 } 125 126 static uint16_t 127 lerdcsr(struct lance_softc *sc, uint16_t port) 128 { 129 struct le_softc *lesc = (struct le_softc *)sc; 130 bus_space_write_2(lesc->sc_st, lesc->sc_raph, 0, port); 131 return (bus_space_read_2(lesc->sc_st, lesc->sc_rdph, 0)); 132 } 133 134 /* 135 * Always present on IP6 and IP10. IP4? Unknown. 136 */ 137 int 138 le_match(device_t parent, cfdata_t cf, void *aux) 139 { 140 struct oioc_attach_args *oa = aux; 141 142 if (mach_type == MACH_SGI_IP4) 143 return (0); 144 145 if (strcmp(oa->oa_name, cf->cf_name) == 0) 146 return (1); 147 148 return (0); 149 } 150 151 void 152 le_attach(device_t parent, device_t self, void *aux) 153 { 154 extern paddr_t avail_start, avail_end; 155 156 struct le_softc *lesc = device_private(self); 157 struct lance_softc *sc = &lesc->sc_am7990.lsc; 158 struct oioc_attach_args *oa = aux; 159 struct pglist mlist; 160 const char *enaddrstr; 161 char enaddr[6]; 162 char pbuf[9]; 163 int i, error; 164 165 sc->sc_dev = self; 166 lesc->sc_st = oa->oa_st; 167 168 enaddrstr = ARCBIOS->GetEnvironmentVariable("eaddr"); 169 if (enaddrstr == NULL) { 170 aprint_error(": failed to obtain MAC address\n"); 171 return; 172 } 173 174 if ((error = bus_space_subregion(oa->oa_st, oa->oa_sh, OIOC_LANCE_RDP, 175 OIOC_LANCE_RDP_SIZE, &lesc->sc_rdph)) != 0) { 176 printf(": unable to map rdp reg, error=%d\n", error); 177 goto fail_0; 178 } 179 180 if ((error = bus_space_subregion(oa->oa_st, oa->oa_sh, OIOC_LANCE_RAP, 181 OIOC_LANCE_RAP_SIZE, &lesc->sc_raph)) != 0) { 182 printf(": unable to map rap reg, error=%d\n", error); 183 goto fail_1; 184 } 185 186 if ((error = bus_space_subregion(oa->oa_st, oa->oa_sh, 187 OIOC_ENET_PGMAP_BASE, OIOC_ENET_PGMAP_SIZE, &lesc->sc_maph)) != 0) { 188 printf(": unable to map rap reg, error=%d\n", error); 189 goto fail_2; 190 } 191 192 /* Allocate a contiguous chunk of physical memory for the le buffer. */ 193 error = uvm_pglistalloc(OIOC_LANCE_NPAGES * PAGE_SIZE, 194 avail_start, avail_end, PAGE_SIZE, 0, &mlist, 1, 0); 195 if (error) { 196 aprint_error(": failed to allocate ioc<->lance buffer space, " 197 "error = %d\n", error); 198 goto fail_3; 199 } 200 201 /* Use IOC to map the physical memory into the Ethernet chip's space. */ 202 for (i = 0; i < OIOC_LANCE_NPAGES; i++) { 203 bus_space_write_2(lesc->sc_st,lesc->sc_maph, 204 OIOC_ENET_PGMAP_OFF(i), 205 (VM_PAGE_TO_PHYS(mlist.tqh_first) >> PAGE_SHIFT) + i); 206 } 207 208 sc->sc_mem = (void *)MIPS_PHYS_TO_KSEG1( 209 (uint32_t)VM_PAGE_TO_PHYS(mlist.tqh_first)); 210 sc->sc_memsize = OIOC_LANCE_NPAGES * PAGE_SIZE; 211 sc->sc_addr = 0; 212 sc->sc_conf3 = LE_C3_BSWP; 213 214 enaddr_aton(enaddrstr, enaddr); 215 memcpy(sc->sc_enaddr, enaddr, sizeof(sc->sc_enaddr)); 216 217 if (cpu_intr_establish(oa->oa_irq, IPL_NET, am7990_intr, sc) == NULL) { 218 aprint_error(": failed to establish interrupt %d\n",oa->oa_irq); 219 goto fail_4; 220 } 221 222 sc->sc_copytodesc = lance_copytobuf_contig; 223 sc->sc_copyfromdesc = lance_copyfrombuf_contig; 224 sc->sc_copytobuf = lance_copytobuf_contig; 225 sc->sc_copyfrombuf = lance_copyfrombuf_contig; 226 sc->sc_zerobuf = lance_zerobuf_contig; 227 228 sc->sc_rdcsr = lerdcsr; 229 sc->sc_wrcsr = lewrcsr; 230 sc->sc_hwinit = NULL; 231 232 format_bytes(pbuf, sizeof(pbuf), OIOC_LANCE_NPAGES * PAGE_SIZE); 233 aprint_normal(": main memory used = %s\n", pbuf); 234 aprint_normal("%s", device_xname(self)); 235 236 am7990_config(&lesc->sc_am7990); 237 238 return; 239 240 fail_4: 241 uvm_pglistfree(&mlist); 242 fail_3: 243 bus_space_unmap(oa->oa_st, oa->oa_sh, lesc->sc_maph); 244 fail_2: 245 bus_space_unmap(oa->oa_st, oa->oa_sh, lesc->sc_raph); 246 fail_1: 247 bus_space_unmap(oa->oa_st, oa->oa_sh, lesc->sc_rdph); 248 fail_0: 249 return; 250 } 251 252 /* stolen from sgimips/hpc/if_sq.c */ 253 static void 254 enaddr_aton(const char *str, u_int8_t *eaddr) 255 { 256 int i; 257 char c; 258 259 for (i = 0; i < ETHER_ADDR_LEN; i++) { 260 if (*str == ':') 261 str++; 262 263 c = *str++; 264 if (isdigit(c)) { 265 eaddr[i] = (c - '0'); 266 } else if (isxdigit(c)) { 267 eaddr[i] = (toupper(c) + 10 - 'A'); 268 } 269 270 c = *str++; 271 if (isdigit(c)) { 272 eaddr[i] = (eaddr[i] << 4) | (c - '0'); 273 } else if (isxdigit(c)) { 274 eaddr[i] = (eaddr[i] << 4) | (toupper(c) + 10 - 'A'); 275 } 276 } 277 } 278