1 /* $NetBSD: if_le.c,v 1.15 2025/04/26 04:07:05 tsutsui Exp $ */ 2 3 /*- 4 * Copyright (c) 1996, 2000 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Adam Glass, Gordon W. Ross and Wayne Knowles 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 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: if_le.c,v 1.15 2025/04/26 04:07:05 tsutsui Exp $"); 34 35 #include "opt_inet.h" 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/mbuf.h> 40 #include <sys/syslog.h> 41 #include <sys/socket.h> 42 #include <sys/device.h> 43 44 #include <net/if.h> 45 #include <net/if_ether.h> 46 #include <net/if_media.h> 47 48 #ifdef INET 49 #include <netinet/in.h> 50 #include <netinet/if_inarp.h> 51 #endif 52 53 #include <machine/cpu.h> 54 #include <machine/mainboard.h> 55 #include <machine/autoconf.h> 56 #include <machine/bus.h> 57 58 #include <dev/ic/lancereg.h> 59 #include <dev/ic/lancevar.h> 60 #include <dev/ic/am7990reg.h> 61 #include <dev/ic/am7990var.h> 62 63 /* 64 * LANCE registers. 65 */ 66 67 #define LEREG1_RDP 2 /* Offset to LANCE data register */ 68 #define LEREG1_RAP 6 /* Offset to LANCE address register */ 69 70 /* 71 * Ethernet software status per interface. 72 * The real stuff is in dev/ic/am7990var.h 73 */ 74 struct le_softc { 75 struct am7990_softc sc_am7990; /* glue to MI code */ 76 bus_space_tag_t sc_bustag; 77 bus_dma_tag_t sc_dmatag; 78 bus_space_handle_t sc_reg; /* LANCE registers */ 79 bus_dmamap_t sc_dmamap; 80 struct evcnt sc_intrcnt; /* Interrupt event counter */ 81 }; 82 83 static int le_match(device_t, cfdata_t, void *); 84 static void le_attach(device_t, device_t, void *); 85 86 CFATTACH_DECL_NEW(le, sizeof(struct le_softc), 87 le_match, le_attach, NULL, NULL); 88 89 #if defined(_KERNEL_OPT) 90 #include "opt_ddb.h" 91 #endif 92 93 #ifdef DDB 94 #define integrate 95 #define hide 96 #else 97 #define integrate static inline 98 #define hide static 99 #endif 100 101 int le_intr(void *); 102 103 hide void lewrcsr(struct lance_softc *, uint16_t, uint16_t); 104 hide uint16_t lerdcsr(struct lance_softc *, uint16_t); 105 106 hide void 107 lewrcsr(struct lance_softc *sc, uint16_t port, uint16_t val) 108 { 109 struct le_softc *lesc = (struct le_softc *)sc; 110 111 bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port); 112 bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP, val); 113 } 114 115 hide uint16_t 116 lerdcsr(struct lance_softc *sc, uint16_t port) 117 { 118 struct le_softc *lesc = (struct le_softc *)sc; 119 120 bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port); 121 return (bus_space_read_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP)); 122 } 123 124 int 125 le_match(device_t parent, cfdata_t cf, void *aux) 126 { 127 struct confargs *ca = aux; 128 129 if (strcmp(ca->ca_name, "le")) 130 return 0; 131 132 if (badaddr((void *)ca->ca_addr, 1)) 133 return 0; 134 135 return 1; 136 } 137 138 #define LE_MEMSIZE (32*1024) 139 140 void 141 le_attach(device_t parent, device_t self, void *aux) 142 { 143 struct le_softc *lesc = device_private(self); 144 struct lance_softc *sc = &lesc->sc_am7990.lsc; 145 struct confargs *ca = aux; 146 147 bus_dma_tag_t dmat; 148 bus_dma_segment_t seg; 149 int nseg; 150 uint8_t *id; 151 int i; 152 void *kvaddr; 153 154 sc->sc_dev = self; 155 id = (uint8_t *)ETHER_ID; 156 lesc->sc_bustag = ca->ca_bustag; 157 dmat = lesc->sc_dmatag = ca->ca_dmatag; 158 159 if (bus_space_map(ca->ca_bustag, ca->ca_addr, 160 8, /* size */ 161 BUS_SPACE_MAP_LINEAR, 162 &lesc->sc_reg) != 0) { 163 aprint_error(": cannot map registers\n"); 164 return; 165 } 166 167 /* 168 * Allocate a physically contiguous DMA area for the chip. 169 */ 170 if (bus_dmamem_alloc(dmat, LE_MEMSIZE, 0, 0, &seg, 1, 171 &nseg, BUS_DMA_NOWAIT)) { 172 aprint_error(": can't allocate DMA area\n"); 173 goto bad_bsunmap; 174 } 175 /* Map pages into kernel memory */ 176 if (bus_dmamem_map(dmat, &seg, nseg, LE_MEMSIZE, 177 &kvaddr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) { 178 aprint_error(": can't map DMA area\n"); 179 goto bad_free; 180 } 181 /* Build DMA map so we can get physical address */ 182 if (bus_dmamap_create(dmat, LE_MEMSIZE, 1, LE_MEMSIZE, 183 0, BUS_DMA_NOWAIT, &lesc->sc_dmamap)) { 184 aprint_error(": can't create DMA map\n"); 185 goto bad_unmap; 186 } 187 if (bus_dmamap_load(dmat, lesc->sc_dmamap, kvaddr, LE_MEMSIZE, 188 NULL, BUS_DMA_NOWAIT)) { 189 aprint_error(": can't load DMA map\n"); 190 goto bad_destroy; 191 } 192 193 sc->sc_memsize = LE_MEMSIZE; /* 32K Buffer space*/ 194 sc->sc_mem = (void *)MIPS_PHYS_TO_KSEG1(kvaddr); 195 sc->sc_addr = lesc->sc_dmamap->dm_segs[0].ds_addr; 196 197 sc->sc_conf3 = LE_C3_BSWP; 198 199 /* Copy Ethernet hardware address from NVRAM */ 200 for (i = 0; i < ETHER_ADDR_LEN; i++) 201 sc->sc_enaddr[i] = id[i * 4 + 3]; /* XXX */ 202 203 sc->sc_copytodesc = lance_copytobuf_contig; 204 sc->sc_copyfromdesc = lance_copyfrombuf_contig; 205 sc->sc_copytobuf = lance_copytobuf_contig; 206 sc->sc_copyfrombuf = lance_copyfrombuf_contig; 207 sc->sc_zerobuf = lance_zerobuf_contig; 208 209 sc->sc_rdcsr = lerdcsr; 210 sc->sc_wrcsr = lewrcsr; 211 sc->sc_hwinit = NULL; 212 213 evcnt_attach_dynamic(&lesc->sc_intrcnt, EVCNT_TYPE_INTR, NULL, 214 device_xname(self), "intr"); 215 bus_intr_establish(lesc->sc_bustag, SYS_INTR_ETHER, 0, 0, 216 le_intr, lesc); 217 218 am7990_config(&lesc->sc_am7990); 219 return; 220 221 bad_destroy: 222 bus_dmamap_destroy(dmat, lesc->sc_dmamap); 223 bad_unmap: 224 bus_dmamem_unmap(dmat, kvaddr, LE_MEMSIZE); 225 bad_free: 226 bus_dmamem_free(dmat, &seg, nseg); 227 bad_bsunmap: 228 bus_space_unmap(ca->ca_bustag, lesc->sc_reg, 8); 229 } 230 231 int 232 le_intr(void *arg) 233 { 234 struct le_softc *lesc = arg; 235 236 lesc->sc_intrcnt.ev_count++; 237 return am7990_intr(&lesc->sc_am7990); 238 } 239