xref: /netbsd-src/sys/dev/isa/if_cs_isa.c (revision 220b5c059a84c51ea44107ea8951a57ffaecdc8c)
1 /*	$NetBSD: if_cs_isa.c,v 1.5 2001/11/26 19:17:06 yamt Exp $	*/
2 
3 /*
4  * Copyright 1997
5  * Digital Equipment Corporation. All rights reserved.
6  *
7  * This software is furnished under license and may be used and
8  * copied only in accordance with the following terms and conditions.
9  * Subject to these conditions, you may download, copy, install,
10  * use, modify and distribute this software in source and/or binary
11  * form. No title or ownership is transferred hereby.
12  *
13  * 1) Any source code used, modified or distributed must reproduce
14  *    and retain this copyright notice and list of conditions as
15  *    they appear in the source file.
16  *
17  * 2) No right is granted to use any trade name, trademark, or logo of
18  *    Digital Equipment Corporation. Neither the "Digital Equipment
19  *    Corporation" name nor any trademark or logo of Digital Equipment
20  *    Corporation may be used to endorse or promote products derived
21  *    from this software without the prior written permission of
22  *    Digital Equipment Corporation.
23  *
24  * 3) This software is provided "AS-IS" and any express or implied
25  *    warranties, including but not limited to, any implied warranties
26  *    of merchantability, fitness for a particular purpose, or
27  *    non-infringement are disclaimed. In no event shall DIGITAL be
28  *    liable for any damages whatsoever, and in particular, DIGITAL
29  *    shall not be liable for special, indirect, consequential, or
30  *    incidental damages or damages for lost profits, loss of
31  *    revenue or loss of use, whether such damages arise in contract,
32  *    negligence, tort, under statute, in equity, at law or otherwise,
33  *    even if advised of the possibility of such damage.
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: if_cs_isa.c,v 1.5 2001/11/26 19:17:06 yamt Exp $");
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/socket.h>
42 #include <sys/device.h>
43 
44 #include "rnd.h"
45 #if NRND > 0
46 #include <sys/rnd.h>
47 #endif
48 
49 #include <net/if.h>
50 #include <net/if_ether.h>
51 #include <net/if_media.h>
52 
53 #include <machine/bus.h>
54 #include <machine/intr.h>
55 
56 #include <dev/isa/isareg.h>
57 #include <dev/isa/isavar.h>
58 #include <dev/isa/isadmavar.h>
59 
60 #include <dev/ic/cs89x0reg.h>
61 #include <dev/ic/cs89x0var.h>
62 #include <dev/isa/cs89x0isavar.h>
63 
64 int	cs_isa_probe __P((struct device *, struct cfdata *, void *));
65 void	cs_isa_attach __P((struct device *, struct device *, void *));
66 
67 struct cfattach cs_isa_ca = {
68 	sizeof(struct cs_softc_isa), cs_isa_probe, cs_isa_attach
69 };
70 
71 int
72 cs_isa_probe(parent, cf, aux)
73 	struct device *parent;
74 	struct cfdata *cf;
75 	void *aux;
76 {
77 	struct isa_attach_args *ia = aux;
78 	bus_space_tag_t iot = ia->ia_iot;
79 	bus_space_tag_t memt = ia->ia_memt;
80 	bus_space_handle_t ioh, memh;
81 	int rv = 0, have_io = 0, have_mem = 0;
82 	u_int16_t isa_cfg, isa_membase;
83 	bus_addr_t maddr = ia->ia_maddr;
84 	int irq = ia->ia_irq;
85 
86 	/*
87 	 * Disallow wildcarded I/O base.
88 	 */
89 	if (ia->ia_iobase == ISACF_PORT_DEFAULT)
90 		return (0);
91 
92 	/*
93 	 * Map the I/O space.
94 	 */
95 	if (bus_space_map(ia->ia_iot, ia->ia_iobase, CS8900_IOSIZE, 0, &ioh))
96 		goto out;
97 	have_io = 1;
98 
99 	/* Verify that it's a Crystal product. */
100 	if (CS_READ_PACKET_PAGE_IO(iot, ioh, PKTPG_EISA_NUM) !=
101 	    EISA_NUM_CRYSTAL)
102 		goto out;
103 
104 	/*
105 	 * Verify that it's a supported chip.
106 	 */
107 	switch (CS_READ_PACKET_PAGE_IO(iot, ioh, PKTPG_PRODUCT_ID) &
108 	    PROD_ID_MASK) {
109 	case PROD_ID_CS8900:
110 #ifdef notyet
111 	case PROD_ID_CS8920:
112 	case PROD_ID_CS8920M:
113 #endif
114 		rv = 1;
115 	}
116 
117 	/*
118 	 * If the IRQ or memory address were not specified, read the
119 	 * ISA_CFG EEPROM location.
120 	 */
121 	if (maddr == ISACF_IOMEM_DEFAULT || irq == ISACF_IRQ_DEFAULT) {
122 		if (cs_verify_eeprom(iot, ioh) == CS_ERROR) {
123 			printf("cs_isa_probe: EEPROM bad or missing\n");
124 			goto out;
125 		}
126 		if (cs_read_eeprom(iot, ioh, EEPROM_ISA_CFG, &isa_cfg)
127 		    == CS_ERROR) {
128 			printf("cs_isa_probe: unable to read ISA_CFG\n");
129 			goto out;
130 		}
131 	}
132 
133 	/*
134 	 * If the IRQ wasn't specified, get it from the EEPROM.
135 	 */
136 	if (irq == ISACF_IRQ_DEFAULT) {
137 		irq = isa_cfg & ISA_CFG_IRQ_MASK;
138 		if (irq == 3)
139 			irq = 5;
140 		else
141 			irq += 10;
142 	}
143 
144 	/*
145 	 * If the memory address wasn't specified, get it from the EEPROM.
146 	 */
147 	if (maddr == ISACF_IOMEM_DEFAULT) {
148 		if ((isa_cfg & ISA_CFG_MEM_MODE) == 0) {
149 			/* EEPROM says don't use memory mode. */
150 			goto out;
151 		}
152 		if (cs_read_eeprom(iot, ioh, EEPROM_MEM_BASE, &isa_membase)
153 		    == CS_ERROR) {
154 			printf("cs_isa_probe: unable to read MEM_BASE\n");
155 			goto out;
156 		}
157 
158 		isa_membase &= MEM_BASE_MASK;
159 		maddr = (int)isa_membase << 8;
160 	}
161 
162 	/*
163 	 * We now have a valid mem address; attempt to map it.
164 	 */
165 	if (bus_space_map(ia->ia_memt, maddr, CS8900_MEMSIZE, 0, &memh)) {
166 		/* Can't map it; fall back on i/o-only mode. */
167 		printf("cs_isa_probe: unable to map memory space\n");
168 		maddr = ISACF_IOMEM_DEFAULT;
169 	} else
170 		have_mem = 1;
171 
172  out:
173 	if (have_io)
174 		bus_space_unmap(iot, ioh, CS8900_IOSIZE);
175 	if (have_mem)
176 		bus_space_unmap(memt, memh, CS8900_MEMSIZE);
177 
178 	if (rv) {
179 		ia->ia_iosize = CS8900_IOSIZE;
180 		ia->ia_maddr = maddr;
181 		ia->ia_irq = irq;
182 		if (ia->ia_maddr != ISACF_IOMEM_DEFAULT)
183 			ia->ia_msize = CS8900_MEMSIZE;
184 	}
185 	return (rv);
186 }
187 
188 void
189 cs_isa_attach(parent, self, aux)
190 	struct device *parent, *self;
191 	void *aux;
192 {
193 	struct cs_softc *sc = (struct cs_softc *) self;
194 	struct cs_softc_isa *isc = (void *) self;
195 	struct isa_attach_args *ia = aux;
196 
197 	isc->sc_ic = ia->ia_ic;
198 	sc->sc_iot = ia->ia_iot;
199 	sc->sc_memt = ia->ia_memt;
200 
201 	isc->sc_drq = ia->ia_drq;
202 	sc->sc_irq = ia->ia_irq;
203 
204 	printf("\n");
205 
206 	/*
207 	 * Map the device.
208 	 */
209 	if (bus_space_map(sc->sc_iot, ia->ia_iobase, ia->ia_iosize,
210 	    0, &sc->sc_ioh)) {
211 		printf("%s: unable to map i/o space\n", sc->sc_dev.dv_xname);
212 		return;
213 	}
214 
215 	/*
216 	 * Validate IRQ.
217 	 */
218 	if (CS8900_IRQ_ISVALID(sc->sc_irq) == 0) {
219 		printf("%s: invalid IRQ %d\n", sc->sc_dev.dv_xname, sc->sc_irq);
220 		return;
221 	}
222 
223 	/*
224 	 * Map the memory space if it was specified.  If we can do this,
225 	 * we set ourselves up to use memory mode forever.  Otherwise,
226 	 * we fall back on I/O mode.
227 	 */
228 	if (ia->ia_maddr != ISACF_IOMEM_DEFAULT &&
229 	    ia->ia_msize == CS8900_MEMSIZE &&
230 	    CS8900_MEMBASE_ISVALID(ia->ia_maddr)) {
231 		if (bus_space_map(sc->sc_memt, ia->ia_maddr, ia->ia_msize,
232 		    0, &sc->sc_memh)) {
233 			printf("%s: unable to map memory space\n",
234 			    sc->sc_dev.dv_xname);
235 		} else {
236 			sc->sc_cfgflags |= CFGFLG_MEM_MODE;
237 			sc->sc_pktpgaddr = ia->ia_maddr;
238 		}
239 	}
240 
241 	sc->sc_ih = isa_intr_establish(ia->ia_ic, sc->sc_irq, IST_EDGE,
242 	    IPL_NET, cs_intr, sc);
243 	if (sc->sc_ih == NULL) {
244 		printf("%s: unable to establish interrupt\n",
245 		    sc->sc_dev.dv_xname);
246 		return;
247 	}
248 
249 	sc->sc_dma_chipinit = cs_isa_dma_chipinit;
250 	sc->sc_dma_attach = cs_isa_dma_attach;
251 	sc->sc_dma_process_rx = cs_process_rx_dma;
252 
253 	cs_attach(sc, NULL, NULL, 0, 0);
254 }
255