if_le_vsbus.c revision 1.27 1 /* $NetBSD: if_le_vsbus.c,v 1.27 2010/01/19 22:06:23 pooka Exp $ */
2
3 /*-
4 * Copyright (c) 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.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*-
33 * Copyright (c) 1992, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * This code is derived from software contributed to Berkeley by
37 * Ralph Campbell and Rick Macklem.
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 *
63 * @(#)if_le.c 8.2 (Berkeley) 11/16/93
64 */
65
66 #include <sys/cdefs.h>
67 __KERNEL_RCSID(0, "$NetBSD: if_le_vsbus.c,v 1.27 2010/01/19 22:06:23 pooka Exp $");
68
69 #include "opt_inet.h"
70
71 #include <sys/param.h>
72 #include <sys/syslog.h>
73 #include <sys/socket.h>
74 #include <sys/device.h>
75 #include <sys/reboot.h>
76
77 #include <uvm/uvm_extern.h>
78
79 #include <net/if.h>
80 #include <net/if_ether.h>
81 #include <net/if_media.h>
82
83 #ifdef INET
84 #include <netinet/in.h>
85 #include <netinet/if_inarp.h>
86 #endif
87
88 #include <machine/cpu.h>
89 #include <machine/sid.h>
90 #include <machine/scb.h>
91 #include <machine/bus.h>
92 #include <machine/vsbus.h>
93
94 #include <dev/ic/lancereg.h>
95 #include <dev/ic/lancevar.h>
96 #include <dev/ic/am7990reg.h>
97 #include <dev/ic/am7990var.h>
98
99 #include "ioconf.h"
100
101 struct le_softc {
102 struct am7990_softc sc_am7990; /* Must be first */
103 struct evcnt sc_intrcnt;
104 bus_dmamap_t sc_dm;
105 volatile uint16_t *sc_rap;
106 volatile uint16_t *sc_rdp;
107 };
108
109 static int le_vsbus_match(device_t, cfdata_t, void *);
110 static void le_vsbus_attach(device_t, device_t, void *);
111 static void lewrcsr(struct lance_softc *, uint16_t, uint16_t);
112 static uint16_t lerdcsr(struct lance_softc *, uint16_t);
113
114 CFATTACH_DECL_NEW(le_vsbus, sizeof(struct le_softc),
115 le_vsbus_match, le_vsbus_attach, NULL, NULL);
116
117 void
118 lewrcsr(struct lance_softc *ls, uint16_t port, uint16_t val)
119 {
120 struct le_softc * const sc = (void *)ls;
121
122 *sc->sc_rap = port;
123 *sc->sc_rdp = val;
124 }
125
126 uint16_t
127 lerdcsr(struct lance_softc *ls, uint16_t port)
128 {
129 struct le_softc * const sc = (void *)ls;
130
131 *sc->sc_rap = port;
132 return *sc->sc_rdp;
133 }
134
135 static int
136 le_vsbus_match(device_t parent, cfdata_t cf, void *aux)
137 {
138 struct vsbus_attach_args * const va = aux;
139 volatile uint16_t *rdp, *rap;
140 struct leinit initblock;
141 bus_dmamap_t map;
142 int i;
143 int rv = 0;
144 int error;
145
146 if (vax_boardtype == VAX_BTYP_49 || vax_boardtype == VAX_BTYP_53)
147 return 0;
148
149 error = bus_dmamap_create(va->va_dmat, sizeof(initblock), 1,
150 sizeof(initblock), 0, BUS_DMA_NOWAIT, &map);
151 if (error) {
152 return 0;
153 }
154
155 error = bus_dmamap_load(va->va_dmat, map, &initblock,
156 sizeof(initblock), NULL, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
157 if (error) {
158 bus_dmamap_destroy(va->va_dmat, map);
159 return 0;
160 }
161
162 memset(&initblock, 0, sizeof(initblock));
163
164 rdp = (uint16_t *)va->va_addr;
165 rap = rdp + 2;
166
167 /* Make sure the chip is stopped. */
168 *rap = LE_CSR0;
169 *rdp = LE_C0_STOP;
170 DELAY(100);
171 *rap = LE_CSR1;
172 *rdp = map->dm_segs->ds_addr & 0xffff;
173 *rap = LE_CSR2;
174 *rdp = (map->dm_segs->ds_addr >> 16) & 0xffff;
175 *rap = LE_CSR0;
176 *rdp = LE_C0_INIT|LE_C0_INEA;
177
178 /* Wait for initialization to finish. */
179 for (i = 100; i >= 0; i--) {
180 DELAY(1000);
181 /* Should have interrupted by now */
182 if (*rdp & LE_C0_IDON)
183 rv = 1;
184 }
185 *rap = LE_CSR0;
186 *rdp = LE_C0_STOP;
187
188 bus_dmamap_unload(va->va_dmat, map);
189 bus_dmamap_destroy(va->va_dmat, map);
190 return rv;
191 }
192
193 static void
194 le_vsbus_attach(device_t parent, device_t self, void *aux)
195 {
196 struct vsbus_attach_args * const va = aux;
197 struct le_softc * const sc = device_private(self);
198 bus_dma_segment_t seg;
199 int *lance_addr;
200 int i, err, rseg;
201
202 sc->sc_am7990.lsc.sc_dev = self;
203 sc->sc_rdp = (uint16_t *) vax_map_physmem(NI_BASE, 1);
204 sc->sc_rap = sc->sc_rdp + 2;
205
206 /*
207 * MD functions.
208 */
209 sc->sc_am7990.lsc.sc_rdcsr = lerdcsr;
210 sc->sc_am7990.lsc.sc_wrcsr = lewrcsr;
211 sc->sc_am7990.lsc.sc_nocarrier = NULL;
212
213 scb_vecalloc(va->va_cvec, (void (*)(void *)) am7990_intr, sc,
214 SCB_ISTACK, &sc->sc_intrcnt);
215 evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
216 device_xname(self), "intr");
217
218 /*
219 * Allocate a (DMA-safe) block for all descriptors and buffers.
220 */
221
222 #define ALLOCSIZ (64 * 1024)
223 err = bus_dmamem_alloc(va->va_dmat, ALLOCSIZ, PAGE_SIZE, 0,
224 &seg, 1, &rseg, BUS_DMA_NOWAIT);
225 if (err) {
226 aprint_error(": unable to alloc buffer block: err %d\n", err);
227 return;
228 }
229 err = bus_dmamem_map(va->va_dmat, &seg, rseg, ALLOCSIZ,
230 (void **)&sc->sc_am7990.lsc.sc_mem,
231 BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
232 if (err) {
233 aprint_error(": unable to map buffer block: err %d\n", err);
234 bus_dmamem_free(va->va_dmat, &seg, rseg);
235 return;
236 }
237 bus_dmamap_create(va->va_dmat, ALLOCSIZ, rseg, ALLOCSIZ,
238 0, BUS_DMA_NOWAIT, &sc->sc_dm);
239 if (err) {
240 aprint_error(": unable to create DMA map: err %d\n", err);
241 bus_dmamem_free(va->va_dmat, &seg, rseg);
242 return;
243 }
244 err = bus_dmamap_load(va->va_dmat, sc->sc_dm, sc->sc_am7990.lsc.sc_mem,
245 ALLOCSIZ, NULL, BUS_DMA_NOWAIT);
246 if (err) {
247 aprint_error(": unable to load DMA map: err %d\n", err);
248 bus_dmamap_destroy(va->va_dmat, sc->sc_dm);
249 bus_dmamem_free(va->va_dmat, &seg, rseg);
250 return;
251 }
252 aprint_normal(" buf 0x%lx-0x%lx", sc->sc_dm->dm_segs->ds_addr,
253 sc->sc_dm->dm_segs->ds_addr + sc->sc_dm->dm_segs->ds_len - 1);
254 sc->sc_am7990.lsc.sc_addr = sc->sc_dm->dm_segs->ds_addr & 0xffffff;
255 sc->sc_am7990.lsc.sc_memsize = sc->sc_dm->dm_segs->ds_len;
256
257 sc->sc_am7990.lsc.sc_copytodesc = lance_copytobuf_contig;
258 sc->sc_am7990.lsc.sc_copyfromdesc = lance_copyfrombuf_contig;
259 sc->sc_am7990.lsc.sc_copytobuf = lance_copytobuf_contig;
260 sc->sc_am7990.lsc.sc_copyfrombuf = lance_copyfrombuf_contig;
261 sc->sc_am7990.lsc.sc_zerobuf = lance_zerobuf_contig;
262
263 #ifdef LEDEBUG
264 sc->sc_am7990.lsc.sc_debug = 1;
265 #endif
266 /*
267 * Get the ethernet address out of rom
268 */
269 lance_addr = (int *)vax_map_physmem(NI_ADDR, 1);
270 for (i = 0; i < ETHER_ADDR_LEN; i++)
271 sc->sc_am7990.lsc.sc_enaddr[i] = (u_char)lance_addr[i];
272 vax_unmap_physmem((vaddr_t)lance_addr, 1);
273
274 memcpy(sc->sc_am7990.lsc.sc_ethercom.ec_if.if_xname, device_xname(self),
275 IFNAMSIZ);
276
277 /* Prettier printout */
278 aprint_normal("\n%s", device_xname(self));
279
280 am7990_config(&sc->sc_am7990);
281 }
282