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