if_le_lebuffer.c revision 1.23.4.2 1 /* $NetBSD: if_le_lebuffer.c,v 1.23.4.2 2009/09/16 13:37:57 yamt Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum; Jason R. Thorpe of the Numerical Aerospace
9 * Simulation Facility, NASA Ames Research Center; Paul Kranenburg.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: if_le_lebuffer.c,v 1.23.4.2 2009/09/16 13:37:57 yamt Exp $");
35
36 #include "opt_inet.h"
37 #include "bpfilter.h"
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/mbuf.h>
42 #include <sys/syslog.h>
43 #include <sys/socket.h>
44 #include <sys/device.h>
45 #include <sys/malloc.h>
46
47 #include <net/if.h>
48 #include <net/if_ether.h>
49 #include <net/if_media.h>
50
51 #ifdef INET
52 #include <netinet/in.h>
53 #include <netinet/if_inarp.h>
54 #endif
55
56 #include <sys/bus.h>
57 #include <sys/intr.h>
58 #include <machine/autoconf.h>
59
60 #include <dev/sbus/sbusvar.h>
61 #include <dev/sbus/lebuffervar.h>
62
63 #include <dev/ic/lancereg.h>
64 #include <dev/ic/lancevar.h>
65 #include <dev/ic/am7990reg.h>
66 #include <dev/ic/am7990var.h>
67
68 #include "ioconf.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(device_t, cfdata_t, void *);
86 void leattach_lebuffer(device_t, device_t, void *);
87
88 static void le_lebuffer_reset(device_t);
89
90 /*
91 * Media types supported.
92 */
93 static int lemedia[] = {
94 IFM_ETHER|IFM_10_T,
95 };
96 #define NLEMEDIA __arraycount(lemedia)
97
98 CFATTACH_DECL_NEW(le_lebuffer, sizeof(struct le_softc),
99 lematch_lebuffer, leattach_lebuffer, NULL, NULL);
100
101 #if defined(_KERNEL_OPT)
102 #include "opt_ddb.h"
103 #endif
104
105 static void lewrcsr(struct lance_softc *, uint16_t, uint16_t);
106 static uint16_t lerdcsr(struct lance_softc *, uint16_t);
107
108 static void
109 lewrcsr(struct lance_softc *sc, uint16_t port, uint16_t val)
110 {
111 struct le_softc *lesc = (struct le_softc *)sc;
112 bus_space_tag_t t = lesc->sc_bustag;
113 bus_space_handle_t h = lesc->sc_reg;
114
115 bus_space_write_2(t, h, LEREG1_RAP, port);
116 bus_space_write_2(t, h, LEREG1_RDP, val);
117
118 #if defined(SUN4M)
119 /*
120 * We need to flush the Sbus->Mbus write buffers. This can most
121 * easily be accomplished by reading back the register that we
122 * just wrote (thanks to Chris Torek for this solution).
123 */
124 (void)bus_space_read_2(t, h, LEREG1_RDP);
125 #endif
126 }
127
128 static uint16_t
129 lerdcsr(struct lance_softc *sc, uint16_t port)
130 {
131 struct le_softc *lesc = (struct le_softc *)sc;
132 bus_space_tag_t t = lesc->sc_bustag;
133 bus_space_handle_t h = lesc->sc_reg;
134
135 bus_space_write_2(t, h, LEREG1_RAP, port);
136 return (bus_space_read_2(t, h, LEREG1_RDP));
137 }
138
139 int
140 lematch_lebuffer(device_t parent, cfdata_t cf, void *aux)
141 {
142 struct sbus_attach_args *sa = aux;
143
144 return (strcmp(cf->cf_name, sa->sa_name) == 0);
145 }
146
147
148 void
149 leattach_lebuffer(device_t parent, device_t self, void *aux)
150 {
151 struct le_softc *lesc = device_private(self);
152 struct lance_softc *sc = &lesc->sc_am7990.lsc;
153 struct lebuf_softc *lebuf = device_private(parent);
154 struct sbus_attach_args *sa = aux;
155
156 sc->sc_dev = self;
157 lesc->sc_bustag = sa->sa_bustag;
158 lesc->sc_dmatag = sa->sa_dmatag;
159
160 if (sbus_bus_map(sa->sa_bustag,
161 sa->sa_slot,
162 sa->sa_offset,
163 sa->sa_size,
164 0, &lesc->sc_reg)) {
165 aprint_error(": cannot map registers\n");
166 return;
167 }
168
169 sc->sc_mem = lebuf->sc_buffer;
170 sc->sc_memsize = lebuf->sc_bufsiz;
171 sc->sc_addr = 0; /* Lance view is offset by buffer location */
172 lebuf->attached = 1;
173
174 /* That old black magic... */
175 sc->sc_conf3 = prom_getpropint(sa->sa_node, "busmaster-regval",
176 LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON);
177
178 /* SBus is grandparent, but sbus_establish() looks for it properly */
179 lesc->sc_sd.sd_reset = le_lebuffer_reset;
180 sbus_establish(&lesc->sc_sd, self);
181
182 sc->sc_supmedia = lemedia;
183 sc->sc_nsupmedia = NLEMEDIA;
184 sc->sc_defaultmedia = lemedia[0];
185
186 prom_getether(sa->sa_node, sc->sc_enaddr);
187
188 sc->sc_copytodesc = lance_copytobuf_contig;
189 sc->sc_copyfromdesc = lance_copyfrombuf_contig;
190 sc->sc_copytobuf = lance_copytobuf_contig;
191 sc->sc_copyfrombuf = lance_copyfrombuf_contig;
192 sc->sc_zerobuf = lance_zerobuf_contig;
193
194 sc->sc_rdcsr = lerdcsr;
195 sc->sc_wrcsr = lewrcsr;
196
197 am7990_config(&lesc->sc_am7990);
198
199 /* Establish interrupt handler */
200 if (sa->sa_nintr != 0)
201 (void)bus_intr_establish(lesc->sc_bustag, sa->sa_pri,
202 IPL_NET, am7990_intr, sc);
203 }
204
205 void
206 le_lebuffer_reset(device_t self)
207 {
208 struct le_softc *lesc = device_private(self);
209 struct lance_softc *sc = &lesc->sc_am7990.lsc;
210
211 lance_reset(sc);
212 }
213