if_le_obio.c revision 1.7 1 /* $NetBSD: if_le_obio.c,v 1.7 2000/05/09 22:42:08 pk 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 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 #include "opt_inet.h"
41 #include "bpfilter.h"
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/device.h>
46 #include <sys/malloc.h>
47 #include <sys/socket.h>
48
49 #include <net/if.h>
50 #include <net/if_ether.h>
51 #include <net/if_media.h>
52
53 #include <machine/autoconf.h>
54 #include <machine/cpu.h>
55
56 #include <dev/ic/lancereg.h>
57 #include <dev/ic/lancevar.h>
58 #include <dev/ic/am7990reg.h>
59 #include <dev/ic/am7990var.h>
60
61 /*
62 * LANCE registers.
63 */
64 #define LEREG1_RDP 0 /* Register Data port */
65 #define LEREG1_RAP 2 /* Register Address port */
66
67 struct le_softc {
68 struct am7990_softc sc_am7990; /* glue to MI code */
69 bus_space_tag_t sc_bustag;
70 bus_dma_tag_t sc_dmatag;
71 bus_dmamap_t sc_dmamap;
72 bus_space_handle_t sc_reg; /* LANCE registers */
73 };
74
75 #define MEMSIZE 0x4000 /* LANCE memory size */
76
77 int lematch_obio __P((struct device *, struct cfdata *, void *));
78 void leattach_obio __P((struct device *, struct device *, void *));
79
80 /*
81 * Media types supported.
82 */
83 static int lemedia[] = {
84 IFM_ETHER|IFM_10_T,
85 };
86 #define NLEMEDIA (sizeof(lemedia) / sizeof(lemedia[0]))
87
88 struct cfattach le_obio_ca = {
89 sizeof(struct le_softc), lematch_obio, leattach_obio
90 };
91
92 extern struct cfdriver le_cd;
93
94 #if defined(_KERNEL) && !defined(_LKM)
95 #include "opt_ddb.h"
96 #endif
97
98 #ifdef DDB
99 #define integrate
100 #define hide
101 #else
102 #define integrate static __inline
103 #define hide static
104 #endif
105
106 static void lewrcsr __P((struct lance_softc *, u_int16_t, u_int16_t));
107 static u_int16_t lerdcsr __P((struct lance_softc *, u_int16_t));
108
109 static void
110 lewrcsr(sc, port, val)
111 struct lance_softc *sc;
112 u_int16_t port, val;
113 {
114 struct le_softc *lesc = (struct le_softc *)sc;
115
116 bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port);
117 bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP, val);
118 }
119
120 static u_int16_t
121 lerdcsr(sc, port)
122 struct lance_softc *sc;
123 u_int16_t port;
124 {
125 struct le_softc *lesc = (struct le_softc *)sc;
126
127 bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port);
128 return (bus_space_read_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP));
129 }
130
131 int
132 lematch_obio(parent, cf, aux)
133 struct device *parent;
134 struct cfdata *cf;
135 void *aux;
136 {
137 union obio_attach_args *uoba = aux;
138 struct obio4_attach_args *oba;
139
140 if (uoba->uoba_isobio4 == 0)
141 return (0);
142
143 oba = &uoba->uoba_oba4;
144 return (bus_space_probe(oba->oba_bustag, 0, oba->oba_paddr,
145 2, /* probe size */
146 0, /* offset */
147 0, /* flags */
148 NULL, NULL));
149 }
150
151 void
152 leattach_obio(parent, self, aux)
153 struct device *parent, *self;
154 void *aux;
155 {
156 union obio_attach_args *uoba = aux;
157 struct obio4_attach_args *oba = &uoba->uoba_oba4;
158 struct le_softc *lesc = (struct le_softc *)self;
159 struct lance_softc *sc = &lesc->sc_am7990.lsc;
160 bus_dma_segment_t seg;
161 bus_dma_tag_t dmatag;
162 int rseg;
163 int error;
164 /* XXX the following declarations should be elsewhere */
165 extern void myetheraddr __P((u_char *));
166
167 lesc->sc_bustag = oba->oba_bustag;
168 lesc->sc_dmatag = dmatag = oba->oba_dmatag;
169
170 if (obio_bus_map(oba->oba_bustag, oba->oba_paddr,
171 0, 2 * sizeof(u_int16_t),
172 0, 0,
173 &lesc->sc_reg) != 0) {
174 printf("%s @ obio: cannot map registers\n", self->dv_xname);
175 return;
176 }
177
178 /* Get a DMA handle */
179 if ((error = bus_dmamap_create(dmatag, MEMSIZE, 1, MEMSIZE, 0,
180 BUS_DMA_NOWAIT|BUS_DMA_24BIT,
181 &lesc->sc_dmamap)) != 0) {
182 printf("%s: DMA map create error %d\n",
183 self->dv_xname, error);
184 return;
185 }
186
187 /* Allocate DMA buffer */
188 if ((error = bus_dmamem_alloc(dmatag, MEMSIZE, NBPG, 0,
189 &seg, 1, &rseg,
190 BUS_DMA_NOWAIT | BUS_DMA_24BIT)) != 0) {
191 printf("%s: DMA memory allocation error %d\n",
192 self->dv_xname, error);
193 return;
194 }
195 /* Map DMA buffer into kernel space */
196 if ((error = bus_dmamem_map(dmatag, &seg, rseg, MEMSIZE,
197 (caddr_t *)&sc->sc_mem,
198 BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
199 printf("%s: DMA memory map error %d\n", self->dv_xname, error);
200 bus_dmamem_free(lesc->sc_dmatag, &seg, rseg);
201 return;
202 }
203 /* Load DMA buffer */
204 if ((error = bus_dmamap_load_raw(dmatag, lesc->sc_dmamap,
205 &seg, rseg,
206 MEMSIZE, BUS_DMA_NOWAIT)) != 0) {
207 printf("%s: DMA buffer map load error %d\n",
208 self->dv_xname, error);
209 bus_dmamem_unmap(dmatag, (caddr_t)sc->sc_mem, MEMSIZE);
210 bus_dmamem_free(dmatag, &seg, rseg);
211 return;
212 }
213
214 sc->sc_addr = lesc->sc_dmamap->dm_segs[0].ds_addr & 0xffffff;
215 sc->sc_memsize = MEMSIZE;
216 sc->sc_conf3 = LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON;
217
218 sc->sc_supmedia = lemedia;
219 sc->sc_nsupmedia = NLEMEDIA;
220 sc->sc_defaultmedia = lemedia[0];
221
222 myetheraddr(sc->sc_enaddr);
223
224 sc->sc_copytodesc = lance_copytobuf_contig;
225 sc->sc_copyfromdesc = lance_copyfrombuf_contig;
226 sc->sc_copytobuf = lance_copytobuf_contig;
227 sc->sc_copyfrombuf = lance_copyfrombuf_contig;
228 sc->sc_zerobuf = lance_zerobuf_contig;
229
230 sc->sc_rdcsr = lerdcsr;
231 sc->sc_wrcsr = lewrcsr;
232
233 am7990_config(&lesc->sc_am7990);
234
235 /* Install interrupt */
236 (void)bus_intr_establish(lesc->sc_bustag, oba->oba_pri, 0,
237 am7990_intr, sc);
238 }
239