xref: /netbsd-src/sys/arch/vax/if/if_le_vsbus.c (revision 274254cdae52594c1aa480a736aef78313d15c9c)
1 /*	$NetBSD: if_le_vsbus.c,v 1.25 2009/03/18 17:06:47 cegger 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.25 2009/03/18 17:06:47 cegger Exp $");
68 
69 #include "opt_inet.h"
70 #include "bpfilter.h"
71 
72 #include <sys/param.h>
73 #include <sys/syslog.h>
74 #include <sys/socket.h>
75 #include <sys/device.h>
76 #include <sys/reboot.h>
77 
78 #include <uvm/uvm_extern.h>
79 
80 #include <net/if.h>
81 #include <net/if_ether.h>
82 #include <net/if_media.h>
83 
84 #ifdef INET
85 #include <netinet/in.h>
86 #include <netinet/if_inarp.h>
87 #endif
88 
89 #include <machine/cpu.h>
90 #include <machine/sid.h>
91 #include <machine/scb.h>
92 #include <machine/bus.h>
93 #include <machine/vsbus.h>
94 
95 #include <dev/ic/lancereg.h>
96 #include <dev/ic/lancevar.h>
97 #include <dev/ic/am7990reg.h>
98 #include <dev/ic/am7990var.h>
99 
100 #include "ioconf.h"
101 
102 struct le_softc {
103 	struct	am7990_softc sc_am7990; /* Must be first */
104 	struct	evcnt sc_intrcnt;
105 	bus_dmamap_t sc_dm;
106 	volatile uint16_t *sc_rap;
107 	volatile uint16_t *sc_rdp;
108 };
109 
110 static	int	le_vsbus_match(device_t, cfdata_t, void *);
111 static	void	le_vsbus_attach(device_t, device_t, void *);
112 static	void	lewrcsr(struct lance_softc *, uint16_t, uint16_t);
113 static	uint16_t lerdcsr(struct lance_softc *, uint16_t);
114 
115 CFATTACH_DECL_NEW(le_vsbus, sizeof(struct le_softc),
116     le_vsbus_match, le_vsbus_attach, NULL, NULL);
117 
118 void
119 lewrcsr(struct lance_softc *ls, uint16_t port, uint16_t val)
120 {
121 	struct le_softc * const sc = (void *)ls;
122 
123 	*sc->sc_rap = port;
124 	*sc->sc_rdp = val;
125 }
126 
127 uint16_t
128 lerdcsr(struct lance_softc *ls, uint16_t port)
129 {
130 	struct le_softc * const sc = (void *)ls;
131 
132 	*sc->sc_rap = port;
133 	return *sc->sc_rdp;
134 }
135 
136 static int
137 le_vsbus_match(device_t parent, cfdata_t cf, void *aux)
138 {
139 	struct vsbus_attach_args * const va = aux;
140 	volatile uint16_t *rdp, *rap;
141 	struct leinit initblock;
142 	bus_dmamap_t map;
143 	int i;
144 	int rv = 0;
145 	int error;
146 
147 	if (vax_boardtype == VAX_BTYP_49 || vax_boardtype == VAX_BTYP_53)
148 		return 0;
149 
150 	error = bus_dmamap_create(va->va_dmat, sizeof(initblock), 1,
151 	    sizeof(initblock), 0, BUS_DMA_NOWAIT, &map);
152 	if (error) {
153 		return 0;
154 	}
155 
156 	error = bus_dmamap_load(va->va_dmat, map, &initblock,
157 	    sizeof(initblock), NULL, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
158 	if (error) {
159 		bus_dmamap_destroy(va->va_dmat, map);
160 		return 0;
161 	}
162 
163 	memset(&initblock, 0, sizeof(initblock));
164 
165 	rdp = (uint16_t *)va->va_addr;
166 	rap = rdp + 2;
167 
168 	/* Make sure the chip is stopped. */
169 	*rap = LE_CSR0;
170 	*rdp = LE_C0_STOP;
171 	DELAY(100);
172 	*rap = LE_CSR1;
173 	*rdp = map->dm_segs->ds_addr & 0xffff;
174 	*rap = LE_CSR2;
175 	*rdp = (map->dm_segs->ds_addr >> 16) & 0xffff;
176 	*rap = LE_CSR0;
177 	*rdp = LE_C0_INIT|LE_C0_INEA;
178 
179 	/* Wait for initialization to finish. */
180 	for (i = 100; i >= 0; i--) {
181 		DELAY(1000);
182 		/* Should have interrupted by now */
183 		if (*rdp & LE_C0_IDON)
184 			rv = 1;
185 	}
186 	*rap = LE_CSR0;
187 	*rdp = LE_C0_STOP;
188 
189 	bus_dmamap_unload(va->va_dmat, map);
190 	bus_dmamap_destroy(va->va_dmat, map);
191 	return rv;
192 }
193 
194 static void
195 le_vsbus_attach(device_t parent, device_t self, void *aux)
196 {
197 	struct vsbus_attach_args * const va = aux;
198 	struct le_softc * const sc = device_private(self);
199 	bus_dma_segment_t seg;
200 	int *lance_addr;
201 	int i, err, rseg;
202 
203 	sc->sc_am7990.lsc.sc_dev = self;
204 	sc->sc_rdp = (uint16_t *) vax_map_physmem(NI_BASE, 1);
205 	sc->sc_rap = sc->sc_rdp + 2;
206 
207 	/*
208 	 * MD functions.
209 	 */
210 	sc->sc_am7990.lsc.sc_rdcsr = lerdcsr;
211 	sc->sc_am7990.lsc.sc_wrcsr = lewrcsr;
212 	sc->sc_am7990.lsc.sc_nocarrier = NULL;
213 
214 	scb_vecalloc(va->va_cvec, (void (*)(void *)) am7990_intr, sc,
215 		SCB_ISTACK, &sc->sc_intrcnt);
216 	evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
217 		device_xname(self), "intr");
218 
219 	/*
220 	 * Allocate a (DMA-safe) block for all descriptors and buffers.
221 	 */
222 
223 #define ALLOCSIZ (64 * 1024)
224 	err = bus_dmamem_alloc(va->va_dmat, ALLOCSIZ, PAGE_SIZE, 0,
225 	    &seg, 1, &rseg, BUS_DMA_NOWAIT);
226 	if (err) {
227 		aprint_error(": unable to alloc buffer block: err %d\n", err);
228 		return;
229 	}
230 	err = bus_dmamem_map(va->va_dmat, &seg, rseg, ALLOCSIZ,
231 	    (void **)&sc->sc_am7990.lsc.sc_mem,
232 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
233 	if (err) {
234 		aprint_error(": unable to map buffer block: err %d\n", err);
235 		bus_dmamem_free(va->va_dmat, &seg, rseg);
236 		return;
237 	}
238 	bus_dmamap_create(va->va_dmat, ALLOCSIZ, rseg, ALLOCSIZ,
239 	    0, BUS_DMA_NOWAIT, &sc->sc_dm);
240 	if (err) {
241 		aprint_error(": unable to create DMA map: err %d\n", err);
242 		bus_dmamem_free(va->va_dmat, &seg, rseg);
243 		return;
244 	}
245 	err = bus_dmamap_load(va->va_dmat, sc->sc_dm, sc->sc_am7990.lsc.sc_mem,
246 	    ALLOCSIZ, NULL, BUS_DMA_NOWAIT);
247 	if (err) {
248 		aprint_error(": unable to load DMA map: err %d\n", err);
249 		bus_dmamap_destroy(va->va_dmat, sc->sc_dm);
250 		bus_dmamem_free(va->va_dmat, &seg, rseg);
251 		return;
252 	}
253 	aprint_normal(" buf 0x%lx-0x%lx", sc->sc_dm->dm_segs->ds_addr,
254 	    sc->sc_dm->dm_segs->ds_addr + sc->sc_dm->dm_segs->ds_len - 1);
255 	sc->sc_am7990.lsc.sc_addr = sc->sc_dm->dm_segs->ds_addr & 0xffffff;
256 	sc->sc_am7990.lsc.sc_memsize = sc->sc_dm->dm_segs->ds_len;
257 
258 	sc->sc_am7990.lsc.sc_copytodesc = lance_copytobuf_contig;
259 	sc->sc_am7990.lsc.sc_copyfromdesc = lance_copyfrombuf_contig;
260 	sc->sc_am7990.lsc.sc_copytobuf = lance_copytobuf_contig;
261 	sc->sc_am7990.lsc.sc_copyfrombuf = lance_copyfrombuf_contig;
262 	sc->sc_am7990.lsc.sc_zerobuf = lance_zerobuf_contig;
263 
264 #ifdef LEDEBUG
265 	sc->sc_am7990.lsc.sc_debug = 1;
266 #endif
267 	/*
268 	 * Get the ethernet address out of rom
269 	 */
270 	lance_addr = (int *)vax_map_physmem(NI_ADDR, 1);
271 	for (i = 0; i < ETHER_ADDR_LEN; i++)
272 		sc->sc_am7990.lsc.sc_enaddr[i] = (u_char)lance_addr[i];
273 	vax_unmap_physmem((vaddr_t)lance_addr, 1);
274 
275 	memcpy( sc->sc_am7990.lsc.sc_ethercom.ec_if.if_xname, device_xname(self),
276 	    IFNAMSIZ);
277 
278 	/* Prettier printout */
279 	aprint_normal("\n%s", device_xname(self));
280 
281 	am7990_config(&sc->sc_am7990);
282 }
283