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