xref: /openbsd/sys/dev/isa/if_sm_isa.c (revision 898184e3)
1 /*	$OpenBSD: if_sm_isa.c,v 1.10 2008/06/26 05:42:16 ray Exp $	*/
2 /*	$NetBSD: if_sm_isa.c,v 1.4 1998/07/05 06:49:14 jonathan Exp $	*/
3 
4 /*-
5  * Copyright (c) 1997 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
10  * NASA Ames Research Center.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "bpfilter.h"
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/mbuf.h>
39 #include <sys/socket.h>
40 #include <sys/ioctl.h>
41 #include <sys/errno.h>
42 #include <sys/syslog.h>
43 #include <sys/selinfo.h>
44 #include <sys/timeout.h>
45 #include <sys/device.h>
46 
47 #include <net/if.h>
48 #include <net/if_dl.h>
49 #include <net/if_media.h>
50 
51 #ifdef INET
52 #include <netinet/in.h>
53 #include <netinet/in_systm.h>
54 #include <netinet/in_var.h>
55 #include <netinet/ip.h>
56 #include <netinet/if_ether.h>
57 #endif
58 
59 #if NBPFILTER > 0
60 #include <net/bpf.h>
61 #endif
62 
63 #include <machine/intr.h>
64 #include <machine/bus.h>
65 
66 #include <net/if_media.h>
67 
68 #include <dev/mii/mii.h>
69 #include <dev/mii/miivar.h>
70 
71 #include <dev/ic/smc91cxxreg.h>
72 #include <dev/ic/smc91cxxvar.h>
73 
74 #include <dev/isa/isavar.h>
75 
76 int	sm_isa_match(struct device *, void *, void *);
77 void	sm_isa_attach(struct device *, struct device *, void *);
78 
79 struct sm_isa_softc {
80 	struct	smc91cxx_softc sc_smc;		/* real "smc" softc */
81 
82 	/* ISA-specific goo. */
83 	void	*sc_ih;				/* interrupt cookie */
84 };
85 
86 struct cfattach sm_isa_ca = {
87 	sizeof(struct sm_isa_softc), sm_isa_match, sm_isa_attach
88 };
89 
90 int
91 sm_isa_match(parent, match, aux)
92 	struct device *parent;
93 	void *match, *aux;
94 {
95 	struct isa_attach_args *ia = aux;
96 	bus_space_tag_t iot = ia->ia_iot;
97 	bus_space_handle_t ioh;
98 	u_int16_t tmp;
99 	int rv = 0;
100 	extern const char *smc91cxx_idstrs[];
101 
102 	/* Disallow wildcarded values. */
103 	if (ia->ia_irq == -1)
104 		return (0);
105 	if (ia->ia_iobase == -1)
106 		return (0);
107 
108 	/* Map i/o space. */
109 	if (bus_space_map(iot, ia->ia_iobase, SMC_IOSIZE, 0, &ioh))
110 		return (0);
111 
112 	/* Check that high byte of BANK_SELECT is what we expect. */
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 0 and perform the test again.
119 	 * XXX INVASIVE!
120 	 */
121 	bus_space_write_2(iot, ioh, BANK_SELECT_REG_W, 0);
122 	tmp = bus_space_read_2(iot, ioh, BANK_SELECT_REG_W);
123 	if ((tmp & BSR_DETECT_MASK) != BSR_DETECT_VALUE)
124 		goto out;
125 
126 	/*
127 	 * Switch to bank 1 and check the base address register.
128 	 * XXX INVASIVE!
129 	 */
130 	bus_space_write_2(iot, ioh, BANK_SELECT_REG_W, 1);
131 	tmp = bus_space_read_2(iot, ioh, BASE_ADDR_REG_W);
132 	if (ia->ia_iobase != ((tmp >> 3) & 0x3e0))
133 		goto out;
134 
135 	/*
136 	 * Check for a recognized chip id.
137 	 * XXX INVASIVE!
138 	 */
139 	bus_space_write_2(iot, ioh, BANK_SELECT_REG_W, 3);
140 	tmp = bus_space_read_2(iot, ioh, REVISION_REG_W);
141 	if (smc91cxx_idstrs[RR_ID(tmp)] == NULL)
142 		goto out;
143 
144 	/*
145 	 * Assume we have an SMC91Cxx.
146 	 */
147 	ia->ia_iosize = SMC_IOSIZE;
148 	rv = 1;
149 
150  out:
151 	bus_space_unmap(iot, ioh, SMC_IOSIZE);
152 	return (rv);
153 }
154 
155 void
156 sm_isa_attach(parent, self, aux)
157 	struct device *parent, *self;
158 	void *aux;
159 {
160 	struct sm_isa_softc *isc = (struct sm_isa_softc *)self;
161 	struct smc91cxx_softc *sc = &isc->sc_smc;
162 	struct isa_attach_args *ia = aux;
163 	bus_space_tag_t iot = ia->ia_iot;
164 	bus_space_handle_t ioh;
165 
166 	printf("\n");
167 
168 	/* Map i/o space. */
169 	if (bus_space_map(iot, ia->ia_iobase, ia->ia_iosize, 0, &ioh))
170 		panic("sm_isa_attach: can't map i/o space");
171 
172 	sc->sc_bst = iot;
173 	sc->sc_bsh = ioh;
174 
175 	/* should always be enabled */
176 	sc->sc_enabled = 1;
177 
178 	/* XXX Should get Ethernet address from EEPROM!! */
179 
180 	/* Perform generic initialization. */
181 	smc91cxx_attach(sc, NULL);
182 
183 	/* Establish the interrupt handler. */
184 	isc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
185 	    IPL_NET, smc91cxx_intr, sc, sc->sc_dev.dv_xname);
186 	if (isc->sc_ih == NULL)
187 		printf("%s: couldn't establish interrupt handler\n",
188 		    sc->sc_dev.dv_xname);
189 }
190