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