1 /* $OpenBSD: if_le_lebuffer.c,v 1.5 2003/07/25 03:50:56 jason 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 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 */
40
41 #include "bpfilter.h"
42
43 #include <sys/cdefs.h>
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/mbuf.h>
47 #include <sys/syslog.h>
48 #include <sys/socket.h>
49 #include <sys/device.h>
50 #include <sys/malloc.h>
51
52 #include <net/if.h>
53 #include <net/if_media.h>
54
55 #ifdef INET
56 #include <netinet/in.h>
57 #include <netinet/if_ether.h>
58 #endif
59
60 #include <machine/bus.h>
61 #include <machine/intr.h>
62 #include <machine/autoconf.h>
63
64 #include <dev/sbus/sbusvar.h>
65 #include <dev/sbus/lebuffervar.h>
66
67 #include <dev/ic/am7990reg.h>
68 #include <dev/ic/am7990var.h>
69
70 /*
71 * LANCE registers.
72 */
73 #define LEREG1_RDP 0 /* Register Data port */
74 #define LEREG1_RAP 2 /* Register Address port */
75
76 struct le_softc {
77 struct am7990_softc sc_am7990; /* glue to MI code */
78 struct sbusdev sc_sd; /* sbus device */
79 bus_space_tag_t sc_bustag;
80 bus_dma_tag_t sc_dmatag;
81 bus_space_handle_t sc_reg; /* LANCE registers */
82 };
83
84
85 int lematch_lebuffer(struct device *, void *, void *);
86 void leattach_lebuffer(struct device *, struct device *, void *);
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 am7990_softc *, u_int16_t, u_int16_t);
99 u_int16_t le_lebuffer_rdcsr(struct am7990_softc *, u_int16_t);
100
101 void
le_lebuffer_wrcsr(struct am7990_softc * sc,u_int16_t port,u_int16_t val)102 le_lebuffer_wrcsr(struct am7990_softc *sc, u_int16_t port, u_int16_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_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP, val);
108
109 #if defined(SUN4M)
110 /*
111 * We need to flush the Sbus->Mbus write buffers. This can most
112 * easily be accomplished by reading back the register that we
113 * just wrote (thanks to Chris Torek for this solution).
114 */
115 if (CPU_ISSUN4M) {
116 volatile u_int16_t discard;
117 discard = bus_space_read_2(lesc->sc_bustag, lesc->sc_reg,
118 LEREG1_RDP);
119 }
120 #endif
121 }
122
123 u_int16_t
le_lebuffer_rdcsr(struct am7990_softc * sc,u_int16_t port)124 le_lebuffer_rdcsr(struct am7990_softc *sc, u_int16_t port)
125 {
126 struct le_softc *lesc = (struct le_softc *)sc;
127
128 bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port);
129 return (bus_space_read_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP));
130 }
131
132 int
lematch_lebuffer(struct device * parent,void * vcf,void * aux)133 lematch_lebuffer(struct device *parent, void *vcf, void *aux)
134 {
135 struct sbus_attach_args *sa = aux;
136 struct cfdata *cf = vcf;
137
138 return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0);
139 }
140
141
142 void
leattach_lebuffer(struct device * parent,struct device * self,void * aux)143 leattach_lebuffer(struct device *parent, struct device *self, void *aux)
144 {
145 struct sbus_attach_args *sa = aux;
146 struct le_softc *lesc = (struct le_softc *)self;
147 struct am7990_softc *sc = &lesc->sc_am7990;
148 struct lebuf_softc *lebuf = (struct lebuf_softc *)parent;
149 /* XXX the following declarations should be elsewhere */
150 extern void myetheraddr(u_char *);
151
152 lesc->sc_bustag = sa->sa_bustag;
153 lesc->sc_dmatag = sa->sa_dmatag;
154
155 if (sbus_bus_map(sa->sa_bustag,
156 sa->sa_slot, sa->sa_offset, sa->sa_size,
157 0, 0, &lesc->sc_reg)) {
158 printf(": cannot map registers\n");
159 return;
160 }
161
162 sc->sc_mem = lebuf->sc_buffer;
163 sc->sc_memsize = lebuf->sc_bufsiz;
164 sc->sc_addr = 0; /* Lance view is offset by buffer location */
165 lebuf->attached = 1;
166
167 /* That old black magic... */
168 sc->sc_conf3 = getpropint(sa->sa_node, "busmaster-regval",
169 LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON);
170
171 /* Assume SBus is grandparent */
172 lesc->sc_sd.sd_reset = (void *)am7990_reset;
173 sbus_establish(&lesc->sc_sd, parent);
174
175 myetheraddr(sc->sc_arpcom.ac_enaddr);
176
177 sc->sc_copytodesc = am7990_copytobuf_contig;
178 sc->sc_copyfromdesc = am7990_copyfrombuf_contig;
179 sc->sc_copytobuf = am7990_copytobuf_contig;
180 sc->sc_copyfrombuf = am7990_copyfrombuf_contig;
181 sc->sc_zerobuf = am7990_zerobuf_contig;
182
183 sc->sc_rdcsr = le_lebuffer_rdcsr;
184 sc->sc_wrcsr = le_lebuffer_wrcsr;
185
186 am7990_config(&lesc->sc_am7990);
187
188 /* Establish interrupt handler */
189 if (sa->sa_nintr != 0)
190 (void)bus_intr_establish(lesc->sc_bustag, sa->sa_pri,
191 IPL_NET, 0, am7990_intr, sc, self->dv_xname);
192 }
193