xref: /netbsd-src/sys/dev/isa/if_sm_isa.c (revision 8a8f936f250a330d54f8a24ed0e92aadf9743a7b)
1 /*	$NetBSD: if_sm_isa.c,v 1.7 2001/07/08 17:55:50 thorpej 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 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/mbuf.h>
43 #include <sys/socket.h>
44 #include <sys/ioctl.h>
45 #include <sys/errno.h>
46 #include <sys/syslog.h>
47 #include <sys/select.h>
48 #include <sys/device.h>
49 
50 #include <net/if.h>
51 #include <net/if_dl.h>
52 #include <net/if_ether.h>
53 #include <net/if_media.h>
54 
55 #include <machine/intr.h>
56 #include <machine/bus.h>
57 
58 #include <dev/mii/mii.h>
59 #include <dev/mii/miivar.h>
60 
61 #include <dev/ic/smc91cxxreg.h>
62 #include <dev/ic/smc91cxxvar.h>
63 
64 #include <dev/isa/isavar.h>
65 
66 int	sm_isa_match __P((struct device *, struct cfdata *, void *));
67 void	sm_isa_attach __P((struct device *, struct device *, void *));
68 
69 struct sm_isa_softc {
70 	struct	smc91cxx_softc sc_smc;		/* real "smc" softc */
71 
72 	/* ISA-specific goo. */
73 	void	*sc_ih;				/* interrupt cookie */
74 };
75 
76 struct cfattach sm_isa_ca = {
77 	sizeof(struct sm_isa_softc), sm_isa_match, sm_isa_attach
78 };
79 
80 int
81 sm_isa_match(parent, match, aux)
82 	struct device *parent;
83 	struct cfdata *match;
84 	void *aux;
85 {
86 	struct isa_attach_args *ia = aux;
87 	bus_space_tag_t iot = ia->ia_iot;
88 	bus_space_handle_t ioh;
89 	u_int16_t tmp;
90 	int rv = 0;
91 	extern const char *smc91cxx_idstrs[];
92 
93 	/* Disallow wildcarded values. */
94 	if (ia->ia_irq == -1)
95 		return (0);
96 	if (ia->ia_iobase == -1)
97 		return (0);
98 
99 	/* Map i/o space. */
100 	if (bus_space_map(iot, ia->ia_iobase, SMC_IOSIZE, 0, &ioh))
101 		return (0);
102 
103 	/* Check that high byte of BANK_SELECT is what we expect. */
104 	tmp = bus_space_read_2(iot, ioh, BANK_SELECT_REG_W);
105 	if ((tmp & BSR_DETECT_MASK) != BSR_DETECT_VALUE)
106 		goto out;
107 
108 	/*
109 	 * Switch to bank 0 and perform the test again.
110 	 * XXX INVASIVE!
111 	 */
112 	bus_space_write_2(iot, ioh, BANK_SELECT_REG_W, 0);
113 	tmp = bus_space_read_2(iot, ioh, BANK_SELECT_REG_W);
114 	if ((tmp & BSR_DETECT_MASK) != BSR_DETECT_VALUE)
115 		goto out;
116 
117 	/*
118 	 * Switch to bank 1 and check the base address register.
119 	 * XXX INVASIVE!
120 	 */
121 	bus_space_write_2(iot, ioh, BANK_SELECT_REG_W, 1);
122 	tmp = bus_space_read_2(iot, ioh, BASE_ADDR_REG_W);
123 	if (ia->ia_iobase != ((tmp >> 3) & 0x3e0))
124 		goto out;
125 
126 	/*
127 	 * Check for a recognized chip id.
128 	 * XXX INVASIVE!
129 	 */
130 	bus_space_write_2(iot, ioh, BANK_SELECT_REG_W, 3);
131 	tmp = bus_space_read_2(iot, ioh, REVISION_REG_W);
132 	if (smc91cxx_idstrs[RR_ID(tmp)] == NULL)
133 		goto out;
134 
135 	/*
136 	 * Assume we have an SMC91Cxx.
137 	 */
138 	ia->ia_iosize = SMC_IOSIZE;
139 	rv = 1;
140 
141  out:
142 	bus_space_unmap(iot, ioh, SMC_IOSIZE);
143 	return (rv);
144 }
145 
146 void
147 sm_isa_attach(parent, self, aux)
148 	struct device *parent, *self;
149 	void *aux;
150 {
151 	struct sm_isa_softc *isc = (struct sm_isa_softc *)self;
152 	struct smc91cxx_softc *sc = &isc->sc_smc;
153 	struct isa_attach_args *ia = aux;
154 	bus_space_tag_t iot = ia->ia_iot;
155 	bus_space_handle_t ioh;
156 
157 	printf("\n");
158 
159 	/* Map i/o space. */
160 	if (bus_space_map(iot, ia->ia_iobase, ia->ia_iosize, 0, &ioh))
161 		panic("sm_isa_attach: can't map i/o space");
162 
163 	sc->sc_bst = iot;
164 	sc->sc_bsh = ioh;
165 
166 	/* should always be enabled */
167 	sc->sc_flags |= SMC_FLAGS_ENABLED;
168 
169 	/* XXX Should get Ethernet address from EEPROM!! */
170 
171 	/* Perform generic intialization. */
172 	smc91cxx_attach(sc, NULL);
173 
174 	/* Establish the interrupt handler. */
175 	isc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
176 	    IPL_NET, smc91cxx_intr, sc);
177 	if (isc->sc_ih == NULL)
178 		printf("%s: couldn't establish interrupt handler\n",
179 		    sc->sc_dev.dv_xname);
180 }
181