1 /* $OpenBSD: if_le_lebuffer.c,v 1.11 2013/09/24 20:11:03 miod 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 #ifdef INET 48 #include <netinet/in.h> 49 #include <netinet/if_ether.h> 50 #endif 51 52 #include <machine/bus.h> 53 #include <machine/intr.h> 54 #include <machine/autoconf.h> 55 56 #include <dev/sbus/sbusvar.h> 57 #include <dev/sbus/lebuffervar.h> 58 59 #include <dev/ic/lancereg.h> 60 #include <dev/ic/lancevar.h> 61 #include <dev/ic/am7990reg.h> 62 #include <dev/ic/am7990var.h> 63 64 /* 65 * LANCE registers. 66 */ 67 #define LEREG1_RDP 0 /* Register Data port */ 68 #define LEREG1_RAP 2 /* Register Address port */ 69 70 struct le_softc { 71 struct am7990_softc sc_am7990; /* glue to MI code */ 72 bus_space_tag_t sc_bustag; 73 bus_dma_tag_t sc_dmatag; 74 bus_space_handle_t sc_reg; /* LANCE registers */ 75 }; 76 77 78 int lematch_lebuffer(struct device *, void *, void *); 79 void leattach_lebuffer(struct device *, struct device *, void *); 80 81 /* 82 * Media types supported. 83 */ 84 static int lemedia[] = { 85 IFM_ETHER | IFM_10_T 86 }; 87 88 struct cfattach le_lebuffer_ca = { 89 sizeof(struct le_softc), lematch_lebuffer, leattach_lebuffer 90 }; 91 92 extern struct cfdriver le_cd; 93 94 struct cfdriver lebuffer_cd = { 95 NULL, "lebuffer", DV_DULL 96 }; 97 98 void le_lebuffer_wrcsr(struct lance_softc *, uint16_t, uint16_t); 99 uint16_t le_lebuffer_rdcsr(struct lance_softc *, uint16_t); 100 101 void 102 le_lebuffer_wrcsr(struct lance_softc *sc, uint16_t port, uint16_t val) 103 { 104 struct le_softc *lesc = (struct le_softc *)sc; 105 106 bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port); 107 bus_space_barrier(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, 2, 108 BUS_SPACE_BARRIER_WRITE); 109 bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP, val); 110 bus_space_barrier(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP, 2, 111 BUS_SPACE_BARRIER_WRITE); 112 113 #if defined(SUN4M) 114 /* 115 * We need to flush the SBus->MBus write buffers. This can most 116 * easily be accomplished by reading back the register that we 117 * just wrote (thanks to Chris Torek for this solution). 118 */ 119 if (CPU_ISSUN4M) { 120 volatile uint16_t discard; 121 discard = bus_space_read_2(lesc->sc_bustag, lesc->sc_reg, 122 LEREG1_RDP); 123 } 124 #endif 125 } 126 127 uint16_t 128 le_lebuffer_rdcsr(struct lance_softc *sc, uint16_t port) 129 { 130 struct le_softc *lesc = (struct le_softc *)sc; 131 132 bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port); 133 bus_space_barrier(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, 2, 134 BUS_SPACE_BARRIER_WRITE); 135 return (bus_space_read_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP)); 136 } 137 138 int 139 lematch_lebuffer(struct device *parent, void *vcf, void *aux) 140 { 141 struct sbus_attach_args *sa = aux; 142 struct cfdata *cf = vcf; 143 144 return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0); 145 } 146 147 148 void 149 leattach_lebuffer(struct device *parent, struct device *self, void *aux) 150 { 151 struct sbus_attach_args *sa = aux; 152 struct le_softc *lesc = (struct le_softc *)self; 153 struct lance_softc *sc = &lesc->sc_am7990.lsc; 154 struct lebuf_softc *lebuf = (struct lebuf_softc *)parent; 155 /* XXX the following declarations should be elsewhere */ 156 extern void myetheraddr(u_char *); 157 158 lesc->sc_bustag = sa->sa_bustag; 159 lesc->sc_dmatag = sa->sa_dmatag; 160 161 if (sbus_bus_map(sa->sa_bustag, 162 sa->sa_slot, sa->sa_offset, sa->sa_size, 163 0, 0, &lesc->sc_reg)) { 164 printf(": cannot map registers\n"); 165 return; 166 } 167 168 sc->sc_mem = lebuf->sc_buffer; 169 sc->sc_memsize = lebuf->sc_bufsiz; 170 sc->sc_addr = 0; /* Lance view is offset by buffer location */ 171 lebuf->attached = 1; 172 173 /* That old black magic... */ 174 sc->sc_conf3 = getpropint(sa->sa_node, "busmaster-regval", 175 LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON); 176 177 myetheraddr(sc->sc_arpcom.ac_enaddr); 178 179 sc->sc_supmedia = lemedia; 180 sc->sc_nsupmedia = nitems(lemedia); 181 sc->sc_defaultmedia = sc->sc_supmedia[sc->sc_nsupmedia - 1]; 182 183 sc->sc_copytodesc = lance_copytobuf_contig; 184 sc->sc_copyfromdesc = lance_copyfrombuf_contig; 185 sc->sc_copytobuf = lance_copytobuf_contig; 186 sc->sc_copyfrombuf = lance_copyfrombuf_contig; 187 sc->sc_zerobuf = lance_zerobuf_contig; 188 189 sc->sc_rdcsr = le_lebuffer_rdcsr; 190 sc->sc_wrcsr = le_lebuffer_wrcsr; 191 192 am7990_config(&lesc->sc_am7990); 193 194 /* Establish interrupt handler */ 195 if (sa->sa_nintr != 0) 196 (void)bus_intr_establish(lesc->sc_bustag, sa->sa_pri, 197 IPL_NET, 0, am7990_intr, sc, self->dv_xname); 198 } 199