if_le_obio.c revision 1.23 1 /* $NetBSD: if_le_obio.c,v 1.23 2005/11/16 01:09:49 uwe 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 <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: if_le_obio.c,v 1.23 2005/11/16 01:09:49 uwe Exp $");
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/device.h>
46
47 #include <uvm/uvm_extern.h>
48
49 #include <net/if.h>
50 #include <net/if_ether.h>
51 #include <net/if_media.h>
52
53 #include <machine/bus.h>
54 #include <machine/intr.h>
55 #include <machine/autoconf.h>
56
57 #include <dev/ic/lancereg.h>
58 #include <dev/ic/lancevar.h>
59 #include <dev/ic/am7990reg.h>
60 #include <dev/ic/am7990var.h>
61
62 /*
63 * LANCE registers.
64 */
65 #define LEREG1_RDP 0 /* Register Data port */
66 #define LEREG1_RAP 2 /* Register Address port */
67
68 struct le_softc {
69 struct am7990_softc sc_am7990; /* glue to MI code */
70 bus_space_tag_t sc_bustag;
71 bus_dma_tag_t sc_dmatag;
72 bus_dmamap_t sc_dmamap;
73 bus_space_handle_t sc_reg; /* LANCE registers */
74 };
75
76 #define MEMSIZE 0x4000 /* LANCE memory size */
77
78 /*
79 * Media types supported.
80 */
81 static int lemedia[] = {
82 IFM_ETHER|IFM_10_T,
83 };
84 #define NLEMEDIA (sizeof(lemedia) / sizeof(lemedia[0]))
85
86 static int lematch_obio(struct device *, struct cfdata *, void *);
87 static void leattach_obio(struct device *, struct device *, void *);
88
89 CFATTACH_DECL(le_obio, sizeof(struct le_softc),
90 lematch_obio, leattach_obio, NULL, NULL);
91
92
93 static void lewrcsr(struct lance_softc *, uint16_t, uint16_t);
94 static uint16_t lerdcsr(struct lance_softc *, uint16_t);
95
96 static void
97 lewrcsr(struct lance_softc *sc, uint16_t port, uint16_t val)
98 {
99 struct le_softc *lesc = (struct le_softc *)sc;
100 bus_space_tag_t t = lesc->sc_bustag;
101 bus_space_handle_t h = lesc->sc_reg;
102
103 bus_space_write_2(t, h, LEREG1_RAP, port);
104 bus_space_write_2(t, h, LEREG1_RDP, val);
105 }
106
107 static uint16_t
108 lerdcsr(struct lance_softc *sc, uint16_t port)
109 {
110 struct le_softc *lesc = (struct le_softc *)sc;
111 bus_space_tag_t t = lesc->sc_bustag;
112 bus_space_handle_t h = lesc->sc_reg;
113
114 bus_space_write_2(t, h, LEREG1_RAP, port);
115 return (bus_space_read_2(t, h, LEREG1_RDP));
116 }
117
118 static int
119 lematch_obio(struct device *parent, struct cfdata *cf, void *aux)
120 {
121 union obio_attach_args *uoba = aux;
122 struct obio4_attach_args *oba;
123
124 if (uoba->uoba_isobio4 == 0)
125 return (0);
126
127 oba = &uoba->uoba_oba4;
128 return (bus_space_probe(oba->oba_bustag, oba->oba_paddr,
129 2, /* probe size */
130 0, /* offset */
131 0, /* flags */
132 NULL, NULL));
133 }
134
135 static void
136 leattach_obio(struct device *parent, struct device *self, void *aux)
137 {
138 union obio_attach_args *uoba = aux;
139 struct obio4_attach_args *oba = &uoba->uoba_oba4;
140 struct le_softc *lesc = (struct le_softc *)self;
141 struct lance_softc *sc = &lesc->sc_am7990.lsc;
142 bus_dma_segment_t seg;
143 bus_dma_tag_t dmatag;
144 int rseg;
145 int error;
146
147 lesc->sc_bustag = oba->oba_bustag;
148 lesc->sc_dmatag = dmatag = oba->oba_dmatag;
149
150 if (bus_space_map(oba->oba_bustag, oba->oba_paddr,
151 2 * sizeof(uint16_t),
152 0, &lesc->sc_reg) != 0) {
153 printf("%s @ obio: cannot map registers\n", self->dv_xname);
154 return;
155 }
156
157 /* Get a DMA handle */
158 if ((error = bus_dmamap_create(dmatag, MEMSIZE, 1, MEMSIZE, 0,
159 BUS_DMA_NOWAIT|BUS_DMA_24BIT,
160 &lesc->sc_dmamap)) != 0) {
161 printf("%s: DMA map create error %d\n",
162 self->dv_xname, error);
163 return;
164 }
165
166 /* Allocate DMA buffer */
167 if ((error = bus_dmamem_alloc(dmatag, MEMSIZE, PAGE_SIZE, 0,
168 &seg, 1, &rseg,
169 BUS_DMA_NOWAIT | BUS_DMA_24BIT)) != 0) {
170 printf("%s: DMA memory allocation error %d\n",
171 self->dv_xname, error);
172 return;
173 }
174 /* Map DMA buffer into kernel space */
175 if ((error = bus_dmamem_map(dmatag, &seg, rseg, MEMSIZE,
176 (caddr_t *)&sc->sc_mem,
177 BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
178 printf("%s: DMA memory map error %d\n", self->dv_xname, error);
179 bus_dmamem_free(lesc->sc_dmatag, &seg, rseg);
180 return;
181 }
182 /* Load DMA buffer */
183 if ((error = bus_dmamap_load(dmatag, lesc->sc_dmamap,
184 sc->sc_mem, MEMSIZE, NULL,
185 BUS_DMA_NOWAIT)) != 0) {
186 printf("%s: DMA buffer map load error %d\n",
187 self->dv_xname, error);
188 bus_dmamem_unmap(dmatag, (caddr_t)sc->sc_mem, MEMSIZE);
189 bus_dmamem_free(dmatag, &seg, rseg);
190 return;
191 }
192
193 sc->sc_addr = lesc->sc_dmamap->dm_segs[0].ds_addr & 0xffffff;
194 sc->sc_memsize = MEMSIZE;
195 sc->sc_conf3 = LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON;
196
197 sc->sc_supmedia = lemedia;
198 sc->sc_nsupmedia = NLEMEDIA;
199 sc->sc_defaultmedia = lemedia[0];
200
201 prom_getether(0, sc->sc_enaddr);
202
203 sc->sc_copytodesc = lance_copytobuf_contig;
204 sc->sc_copyfromdesc = lance_copyfrombuf_contig;
205 sc->sc_copytobuf = lance_copytobuf_contig;
206 sc->sc_copyfrombuf = lance_copyfrombuf_contig;
207 sc->sc_zerobuf = lance_zerobuf_contig;
208
209 sc->sc_rdcsr = lerdcsr;
210 sc->sc_wrcsr = lewrcsr;
211
212 am7990_config(&lesc->sc_am7990);
213
214 /* Install interrupt */
215 (void)bus_intr_establish(lesc->sc_bustag, oba->oba_pri, IPL_NET,
216 am7990_intr, sc);
217 }
218