1 /* $OpenBSD: if_le_lebuffer.c,v 1.14 2022/03/13 13:34:54 mpi Exp $ */ 2 /* $NetBSD: if_le_lebuffer.c,v 1.10 2002/03/11 16:00:56 pk Exp $ */ 3 4 /*- 5 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Charles M. Hannum; Jason R. Thorpe of the Numerical Aerospace 10 * Simulation Facility, NASA Ames Research Center; Paul Kranenburg. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include "bpfilter.h" 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/mbuf.h> 39 #include <sys/syslog.h> 40 #include <sys/socket.h> 41 #include <sys/device.h> 42 #include <sys/malloc.h> 43 44 #include <net/if.h> 45 #include <net/if_media.h> 46 47 #include <netinet/in.h> 48 #include <netinet/if_ether.h> 49 50 #include <machine/bus.h> 51 #include <machine/intr.h> 52 #include <machine/autoconf.h> 53 54 #include <dev/sbus/sbusvar.h> 55 #include <dev/sbus/lebuffervar.h> 56 57 #include <dev/ic/lancereg.h> 58 #include <dev/ic/lancevar.h> 59 #include <dev/ic/am7990reg.h> 60 #include <dev/ic/am7990var.h> 61 62 /* 63 * LANCE registers. 64 */ 65 #define LEREG1_RDP 0 /* Register Data port */ 66 #define LEREG1_RAP 2 /* Register Address port */ 67 68 struct le_softc { 69 struct am7990_softc sc_am7990; /* glue to MI code */ 70 bus_space_tag_t sc_bustag; 71 bus_dma_tag_t sc_dmatag; 72 bus_space_handle_t sc_reg; /* LANCE registers */ 73 }; 74 75 76 int lematch_lebuffer(struct device *, void *, void *); 77 void leattach_lebuffer(struct device *, struct device *, void *); 78 79 /* 80 * Media types supported. 81 */ 82 static uint64_t lemedia[] = { 83 IFM_ETHER | IFM_10_T 84 }; 85 86 const struct cfattach le_lebuffer_ca = { 87 sizeof(struct le_softc), lematch_lebuffer, leattach_lebuffer 88 }; 89 90 extern struct cfdriver le_cd; 91 92 struct cfdriver lebuffer_cd = { 93 NULL, "lebuffer", DV_DULL 94 }; 95 96 void le_lebuffer_wrcsr(struct lance_softc *, uint16_t, uint16_t); 97 uint16_t le_lebuffer_rdcsr(struct lance_softc *, uint16_t); 98 99 void 100 le_lebuffer_wrcsr(struct lance_softc *sc, uint16_t port, uint16_t val) 101 { 102 struct le_softc *lesc = (struct le_softc *)sc; 103 104 bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port); 105 bus_space_barrier(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, 2, 106 BUS_SPACE_BARRIER_WRITE); 107 bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP, val); 108 bus_space_barrier(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP, 2, 109 BUS_SPACE_BARRIER_WRITE); 110 111 #if defined(SUN4M) 112 /* 113 * We need to flush the SBus->MBus write buffers. This can most 114 * easily be accomplished by reading back the register that we 115 * just wrote (thanks to Chris Torek for this solution). 116 */ 117 if (CPU_ISSUN4M) { 118 volatile uint16_t discard; 119 discard = bus_space_read_2(lesc->sc_bustag, lesc->sc_reg, 120 LEREG1_RDP); 121 } 122 #endif 123 } 124 125 uint16_t 126 le_lebuffer_rdcsr(struct lance_softc *sc, uint16_t port) 127 { 128 struct le_softc *lesc = (struct le_softc *)sc; 129 130 bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port); 131 bus_space_barrier(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, 2, 132 BUS_SPACE_BARRIER_WRITE); 133 return (bus_space_read_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP)); 134 } 135 136 int 137 lematch_lebuffer(struct device *parent, void *vcf, void *aux) 138 { 139 struct sbus_attach_args *sa = aux; 140 struct cfdata *cf = vcf; 141 142 return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0); 143 } 144 145 146 void 147 leattach_lebuffer(struct device *parent, struct device *self, void *aux) 148 { 149 struct sbus_attach_args *sa = aux; 150 struct le_softc *lesc = (struct le_softc *)self; 151 struct lance_softc *sc = &lesc->sc_am7990.lsc; 152 struct lebuf_softc *lebuf = (struct lebuf_softc *)parent; 153 /* XXX the following declarations should be elsewhere */ 154 extern void myetheraddr(u_char *); 155 156 lesc->sc_bustag = sa->sa_bustag; 157 lesc->sc_dmatag = sa->sa_dmatag; 158 159 if (sbus_bus_map(sa->sa_bustag, 160 sa->sa_slot, sa->sa_offset, sa->sa_size, 161 0, 0, &lesc->sc_reg)) { 162 printf(": cannot map registers\n"); 163 return; 164 } 165 166 sc->sc_mem = lebuf->sc_buffer; 167 sc->sc_memsize = lebuf->sc_bufsiz; 168 sc->sc_addr = 0; /* Lance view is offset by buffer location */ 169 lebuf->attached = 1; 170 171 /* That old black magic... */ 172 sc->sc_conf3 = getpropint(sa->sa_node, "busmaster-regval", 173 LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON); 174 175 myetheraddr(sc->sc_arpcom.ac_enaddr); 176 177 sc->sc_supmedia = lemedia; 178 sc->sc_nsupmedia = nitems(lemedia); 179 sc->sc_defaultmedia = sc->sc_supmedia[sc->sc_nsupmedia - 1]; 180 181 sc->sc_copytodesc = lance_copytobuf_contig; 182 sc->sc_copyfromdesc = lance_copyfrombuf_contig; 183 sc->sc_copytobuf = lance_copytobuf_contig; 184 sc->sc_copyfrombuf = lance_copyfrombuf_contig; 185 sc->sc_zerobuf = lance_zerobuf_contig; 186 187 sc->sc_rdcsr = le_lebuffer_rdcsr; 188 sc->sc_wrcsr = le_lebuffer_wrcsr; 189 190 am7990_config(&lesc->sc_am7990); 191 192 /* Establish interrupt handler */ 193 if (sa->sa_nintr != 0) 194 (void)bus_intr_establish(lesc->sc_bustag, sa->sa_pri, 195 IPL_NET, 0, am7990_intr, sc, self->dv_xname); 196 } 197