xref: /netbsd-src/sys/dev/isapnp/if_le_isapnp.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: if_le_isapnp.c,v 1.32 2007/10/19 12:00:31 ad Exp $	*/
2 
3 /*-
4  * Copyright (c) 1997 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * Copyright (c) 1997 Jonathan Stone <jonathan@NetBSD.org> and
42  * Matthias Drochner. All rights reserved.
43  *
44  * Redistribution and use in source and binary forms, with or without
45  * modification, are permitted provided that the following conditions
46  * are met:
47  * 1. Redistributions of source code must retain the above copyright
48  *    notice, this list of conditions and the following disclaimer.
49  * 2. Redistributions in binary form must reproduce the above copyright
50  *    notice, this list of conditions and the following disclaimer in the
51  *    documentation and/or other materials provided with the distribution.
52  * 3. All advertising materials mentioning features or use of this software
53  *    must display the following acknowledgement:
54  *      This product includes software developed by Jonathan Stone.
55  * 4. The name of the author may not be used to endorse or promote products
56  *    derived from this software without specific prior written permission.
57  *
58  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
59  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
60  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
61  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
62  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
63  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
64  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
65  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
66  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
67  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
68  */
69 
70 #include <sys/cdefs.h>
71 __KERNEL_RCSID(0, "$NetBSD: if_le_isapnp.c,v 1.32 2007/10/19 12:00:31 ad Exp $");
72 
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/mbuf.h>
76 #include <sys/socket.h>
77 #include <sys/ioctl.h>
78 #include <sys/errno.h>
79 #include <sys/syslog.h>
80 #include <sys/select.h>
81 #include <sys/device.h>
82 
83 #include <uvm/uvm_extern.h>
84 
85 #include <net/if.h>
86 #include <net/if_dl.h>
87 #include <net/if_ether.h>
88 #include <net/if_media.h>
89 
90 #include <sys/cpu.h>
91 #include <sys/bus.h>
92 #include <sys/intr.h>
93 
94 #include <dev/isa/isavar.h>
95 #include <dev/isa/isadmavar.h>
96 
97 #include <dev/isapnp/isapnpreg.h>
98 #include <dev/isapnp/isapnpvar.h>
99 #include <dev/isapnp/isapnpdevs.h>
100 
101 #include <dev/ic/lancereg.h>
102 #include <dev/ic/lancevar.h>
103 #include <dev/ic/am7990reg.h>
104 #include <dev/ic/am7990var.h>
105 
106 #define	LE_ISAPNP_MEMSIZE	16384
107 
108 #define	PCNET_SAPROM	0x00
109 #define	PCNET_RDP	0x10
110 #define	PCNET_RAP	0x12
111 
112 /*
113  * Ethernet software status per interface.
114  *
115  * Each interface is referenced by a network interface structure,
116  * ethercom.ec_if, which the routing code uses to locate the interface.
117  * This structure contains the output queue for the interface, its address, ...
118  */
119 struct le_isapnp_softc {
120 	struct	am7990_softc sc_am7990;	/* glue to MI code */
121 
122 	void	*sc_ih;
123 	bus_space_tag_t sc_iot;		/* space cookie */
124 	bus_space_handle_t sc_ioh;	/* bus space handle */
125 	bus_dma_tag_t	sc_dmat;	/* bus dma tag */
126 	bus_dmamap_t	sc_dmam;	/* bus dma map */
127 	int	sc_rap, sc_rdp;		/* offsets to LANCE registers */
128 };
129 
130 int le_isapnp_match(struct device *, struct cfdata *, void *);
131 void le_isapnp_attach(struct device *, struct device *, void *);
132 
133 CFATTACH_DECL(le_isapnp, sizeof(struct le_isapnp_softc),
134     le_isapnp_match, le_isapnp_attach, NULL, NULL);
135 
136 int	le_isapnp_intredge(void *);
137 static void le_isapnp_wrcsr(struct lance_softc *, u_int16_t, u_int16_t);
138 static u_int16_t le_isapnp_rdcsr(struct lance_softc *, u_int16_t);
139 
140 static void
141 le_isapnp_wrcsr(sc, port, val)
142 	struct lance_softc *sc;
143 	u_int16_t port, val;
144 {
145 	struct le_isapnp_softc *lesc = (struct le_isapnp_softc *)sc;
146 	bus_space_tag_t iot = lesc->sc_iot;
147 	bus_space_handle_t ioh = lesc->sc_ioh;
148 
149 	bus_space_write_2(iot, ioh, lesc->sc_rap, port);
150 	bus_space_write_2(iot, ioh, lesc->sc_rdp, val);
151 }
152 
153 static u_int16_t
154 le_isapnp_rdcsr(sc, port)
155 	struct lance_softc *sc;
156 	u_int16_t port;
157 {
158 	struct le_isapnp_softc *lesc = (struct le_isapnp_softc *)sc;
159 	bus_space_tag_t iot = lesc->sc_iot;
160 	bus_space_handle_t ioh = lesc->sc_ioh;
161 	u_int16_t val;
162 
163 	bus_space_write_2(iot, ioh, lesc->sc_rap, port);
164 	val = bus_space_read_2(iot, ioh, lesc->sc_rdp);
165 	return (val);
166 }
167 
168 int
169 le_isapnp_match(struct device *parent, struct cfdata *match,
170     void *aux)
171 {
172 	int pri, variant;
173 
174 	pri = isapnp_devmatch(aux, &isapnp_le_devinfo, &variant);
175 	if (pri && variant > 0)
176 		pri = 0;
177 	return (pri);
178 }
179 
180 void
181 le_isapnp_attach(struct device *parent, struct device *self, void *aux)
182 {
183 	struct le_isapnp_softc *lesc = device_private(self);
184 	struct lance_softc *sc = &lesc->sc_am7990.lsc;
185 	struct isapnp_attach_args *ipa = aux;
186 	bus_space_tag_t iot;
187 	bus_space_handle_t ioh;
188 	bus_dma_tag_t dmat;
189 	bus_dma_segment_t seg;
190 	int i, rseg, error;
191 
192 	if (isapnp_config(ipa->ipa_iot, ipa->ipa_memt, ipa)) {
193 		printf(": error in region allocation\n");
194 		return;
195 	}
196 
197 	lesc->sc_iot = iot = ipa->ipa_iot;
198 	lesc->sc_ioh = ioh = ipa->ipa_io[0].h;
199 	lesc->sc_dmat = dmat = ipa->ipa_dmat;
200 
201 	lesc->sc_rap = PCNET_RAP;
202 	lesc->sc_rdp = PCNET_RDP;
203 
204 	/*
205 	 * Extract the physical MAC address from the ROM.
206 	 */
207 	for (i = 0; i < sizeof(sc->sc_enaddr); i++)
208 		sc->sc_enaddr[i] = bus_space_read_1(iot, ioh, PCNET_SAPROM + i);
209 
210 	/*
211 	 * Allocate a DMA area for the card.
212 	 */
213 	if (bus_dmamem_alloc(dmat, LE_ISAPNP_MEMSIZE, PAGE_SIZE, 0, &seg, 1,
214 	    &rseg, BUS_DMA_NOWAIT)) {
215 		printf(": couldn't allocate memory for card\n");
216 		return;
217 	}
218 	if (bus_dmamem_map(dmat, &seg, rseg, LE_ISAPNP_MEMSIZE,
219 	    (void **)&sc->sc_mem, BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) {
220 		printf(": couldn't map memory for card\n");
221 		return;
222 	}
223 
224 	/*
225 	 * Create and load the DMA map for the DMA area.
226 	 */
227 	if (bus_dmamap_create(dmat, LE_ISAPNP_MEMSIZE, 1,
228 	    LE_ISAPNP_MEMSIZE, 0, BUS_DMA_NOWAIT, &lesc->sc_dmam)) {
229 		printf(": couldn't create DMA map\n");
230 		bus_dmamem_free(dmat, &seg, rseg);
231 		return;
232 	}
233 	if (bus_dmamap_load(dmat, lesc->sc_dmam,
234 	    sc->sc_mem, LE_ISAPNP_MEMSIZE, NULL, BUS_DMA_NOWAIT)) {
235 		printf(": coundn't load DMA map\n");
236 		bus_dmamem_free(dmat, &seg, rseg);
237 		return;
238 	}
239 
240 	sc->sc_conf3 = 0;
241 	sc->sc_addr = lesc->sc_dmam->dm_segs[0].ds_addr;
242 	sc->sc_memsize = LE_ISAPNP_MEMSIZE;
243 
244 	sc->sc_copytodesc = lance_copytobuf_contig;
245 	sc->sc_copyfromdesc = lance_copyfrombuf_contig;
246 	sc->sc_copytobuf = lance_copytobuf_contig;
247 	sc->sc_copyfrombuf = lance_copyfrombuf_contig;
248 	sc->sc_zerobuf = lance_zerobuf_contig;
249 
250 	sc->sc_rdcsr = le_isapnp_rdcsr;
251 	sc->sc_wrcsr = le_isapnp_wrcsr;
252 	sc->sc_hwinit = NULL;
253 
254 	if (ipa->ipa_ndrq > 0) {
255 		if ((error = isa_dmacascade(ipa->ipa_ic,
256 		    ipa->ipa_drq[0].num)) != 0) {
257 			printf(": unable to cascade DRQ, error = %d\n", error);
258 			return;
259 		}
260 	}
261 
262 	lesc->sc_ih = isa_intr_establish(ipa->ipa_ic, ipa->ipa_irq[0].num,
263 	    ipa->ipa_irq[0].type, IPL_NET, le_isapnp_intredge, sc);
264 
265 	printf(": %s %s\n", ipa->ipa_devident, ipa->ipa_devclass);
266 
267 	printf("%s", sc->sc_dev.dv_xname);
268 	am7990_config(&lesc->sc_am7990);
269 }
270 
271 
272 /*
273  * Controller interrupt.
274  */
275 int
276 le_isapnp_intredge(arg)
277 	void *arg;
278 {
279 
280 	if (am7990_intr(arg) == 0)
281 		return (0);
282 	for (;;)
283 		if (am7990_intr(arg) == 0)
284 			return (1);
285 }
286