xref: /netbsd/sys/arch/mipsco/obio/if_le.c (revision bf9ec67e)
1 /*	$NetBSD: if_le.c,v 1.3 2001/05/30 12:28:46 mrg Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996, 2000 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Adam Glass, Gordon W. Ross and Wayne Knowles
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 #include "opt_inet.h"
40 #include "bpfilter.h"
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/mbuf.h>
45 #include <sys/syslog.h>
46 #include <sys/socket.h>
47 #include <sys/device.h>
48 
49 #include <net/if.h>
50 #include <net/if_ether.h>
51 #include <net/if_media.h>
52 
53 #ifdef INET
54 #include <netinet/in.h>
55 #include <netinet/if_inarp.h>
56 #endif
57 
58 #include <machine/cpu.h>
59 #include <machine/mainboard.h>
60 #include <machine/autoconf.h>
61 #include <machine/bus.h>
62 
63 #include <dev/ic/lancereg.h>
64 #include <dev/ic/lancevar.h>
65 #include <dev/ic/am7990reg.h>
66 #include <dev/ic/am7990var.h>
67 
68 /*
69  * LANCE registers.
70  */
71 
72 #define	LEREG1_RDP	2	/* Offset to LANCE data register */
73 #define	LEREG1_RAP	6	/* Offset to LANCE address register */
74 
75 /*
76  * Ethernet software status per interface.
77  * The real stuff is in dev/ic/am7990var.h
78  */
79 struct	le_softc {
80 	struct am7990_softc	sc_am7990;	/* glue to MI code */
81 	bus_space_tag_t		sc_bustag;
82 	bus_dma_tag_t		sc_dmatag;
83 	bus_space_handle_t	sc_reg;		/* LANCE registers */
84         bus_dmamap_t		sc_dmamap;
85 	struct evcnt		sc_intrcnt;	/* Interrupt event counter */
86 };
87 
88 static int	le_match __P((struct device *, struct cfdata *, void *));
89 static void	le_attach __P((struct device *, struct device *, void *));
90 
91 struct cfattach le_ca = {
92 	sizeof(struct le_softc), le_match, le_attach
93 };
94 
95 #if defined(_KERNEL_OPT)
96 #include "opt_ddb.h"
97 #endif
98 
99 #ifdef DDB
100 #define	integrate
101 #define hide
102 #else
103 #define	integrate	static __inline
104 #define hide		static
105 #endif
106 
107 int le_intr  __P((void *));
108 
109 hide void lewrcsr __P((struct lance_softc *, u_int16_t, u_int16_t));
110 hide u_int16_t lerdcsr __P((struct lance_softc *, u_int16_t));
111 
112 hide void
113 lewrcsr(sc, port, val)
114 	struct lance_softc *sc;
115 	u_int16_t port, val;
116 {
117 	struct le_softc *lesc = (struct le_softc *)sc;
118 
119 	bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port);
120 	bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP, val);
121 }
122 
123 hide u_int16_t
124 lerdcsr(sc, port)
125 	struct lance_softc *sc;
126 	u_int16_t port;
127 {
128 	struct le_softc *lesc = (struct le_softc *)sc;
129 
130 	bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port);
131 	return (bus_space_read_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP));
132 }
133 
134 int
135 le_match(parent, cf, aux)
136 	struct device *parent;
137 	struct cfdata *cf;
138 	void *aux;
139 {
140 	struct confargs *ca = aux;
141 	int addr;
142 
143 	if (strcmp(ca->ca_name, "le"))
144 		return 0;
145 
146 	switch(cf->cf_unit) {
147 
148 	case 0:
149 		addr = LANCE_PORT;
150 		break;
151 	default:
152 		return 0;
153 	}
154 
155 	if (badaddr((void *)addr, 1))
156 		return 0;
157 
158 	return 1;
159 }
160 
161 #define	LE_MEMSIZE	(32*1024)
162 
163 void
164 le_attach(parent, self, aux)
165 	struct device *parent, *self;
166 	void *aux;
167 {
168 	struct le_softc *lesc = (struct le_softc *)self;
169 	struct lance_softc *sc = &lesc->sc_am7990.lsc;
170 	struct confargs *ca = aux;
171 
172 	bus_dma_tag_t dmat;
173 	bus_dma_segment_t seg;
174 	int rseg;
175 
176 	/*struct confargs *ca = aux;*/
177 	u_char *id;
178 	int i;
179 	caddr_t kvaddr;
180 
181 	switch (sc->sc_dev.dv_unit) {
182 	case 0:
183 		id = (u_char *)(ETHER_ID);
184 		break;
185 	default:
186 		panic("le_attach");
187 	}
188 
189 	lesc->sc_bustag = ca->ca_bustag;
190 	dmat = lesc->sc_dmatag = ca->ca_dmatag;
191 
192 	if (bus_space_map(ca->ca_bustag, ca->ca_addr,
193 			  8,	/* size */
194 			  BUS_SPACE_MAP_LINEAR,
195 			  &lesc->sc_reg) != 0) {
196 		printf(": cannot map registers\n");
197 		return;
198 	}
199 
200 	/*
201 	 * Allocate a physically contiguous DMA area for the chip.
202 	 */
203 	if (bus_dmamem_alloc(dmat, LE_MEMSIZE, 0, 0, &seg, 1,
204 			     &rseg, BUS_DMA_NOWAIT)) {
205 		printf(": can't allocate DMA area\n");
206 		return;
207 	}
208 	/* Map pages into kernel memory */
209 	if (bus_dmamem_map(dmat, &seg, rseg, LE_MEMSIZE,
210 	    &kvaddr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) {
211 		printf(": can't map DMA area\n");
212 		bus_dmamem_free(dmat, &seg, rseg);
213 		return;
214 	}
215 	/* Build DMA map so we can get physical address */
216 	if (bus_dmamap_create(dmat, LE_MEMSIZE, 1, LE_MEMSIZE,
217 			      0, BUS_DMA_NOWAIT, &lesc->sc_dmamap)) {
218 		printf(": can't create DMA map\n");
219 		goto bad;
220 	}
221 	if (bus_dmamap_load(dmat, lesc->sc_dmamap, kvaddr, LE_MEMSIZE,
222 			    NULL, BUS_DMA_NOWAIT)) {
223 		printf(": can't load DMA map\n");
224 		goto bad;
225 	}
226 
227 	sc->sc_memsize = LE_MEMSIZE;	/* 16K Buffer space*/
228 	sc->sc_mem  = (void *) MIPS_PHYS_TO_KSEG1(kvaddr);
229 	sc->sc_addr = lesc->sc_dmamap->dm_segs[0].ds_addr;
230 
231 	sc->sc_conf3 = LE_C3_BSWP;
232 
233 	/* Copy Ethernet hardware address from NVRAM */
234 	for (i=0; i < 6; i++)
235 		sc->sc_enaddr[i] = id[i*4+3];	/* XXX */
236 
237 	sc->sc_copytodesc = lance_copytobuf_contig;
238 	sc->sc_copyfromdesc = lance_copyfrombuf_contig;
239 	sc->sc_copytobuf = lance_copytobuf_contig;
240 	sc->sc_copyfrombuf = lance_copyfrombuf_contig;
241 	sc->sc_zerobuf = lance_zerobuf_contig;
242 
243 	sc->sc_rdcsr = lerdcsr;
244 	sc->sc_wrcsr = lewrcsr;
245 	sc->sc_hwinit = NULL;
246 
247 	evcnt_attach_dynamic(&lesc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
248 			     self->dv_xname, "intr");
249 	bus_intr_establish(lesc->sc_bustag, SYS_INTR_ETHER, 0, 0,
250 			   le_intr, lesc);
251 
252 	am7990_config(&lesc->sc_am7990);
253 	return;
254 
255 bad:
256 	bus_dmamem_unmap(dmat, kvaddr, LE_MEMSIZE);
257 	bus_dmamem_free(dmat, &seg, rseg);
258 }
259 
260 int
261 le_intr(arg)
262 	void *arg;
263 {
264 	struct le_softc *lesc = arg;
265 
266 	lesc->sc_intrcnt.ev_count++;
267 	return am7990_intr(&lesc->sc_am7990);
268 }
269