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