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