if_lc_isa.c revision 1.1 1 1.1 matt /* $NetBSD: if_lc_isa.c,v 1.1 1997/07/31 21:58:19 matt Exp $ */
2 1.1 matt
3 1.1 matt /*-
4 1.1 matt * Copyright (c) 1994, 1995, 1997 Matt Thomas <matt (at) 3am-software.com>
5 1.1 matt * All rights reserved.
6 1.1 matt *
7 1.1 matt * Redistribution and use in source and binary forms, with or without
8 1.1 matt * modification, are permitted provided that the following conditions
9 1.1 matt * are met:
10 1.1 matt * 1. Redistributions of source code must retain the above copyright
11 1.1 matt * notice, this list of conditions and the following disclaimer.
12 1.1 matt * 2. The name of the author may not be used to endorse or promote products
13 1.1 matt * derived from this software withough specific prior written permission
14 1.1 matt *
15 1.1 matt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 matt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 1.1 matt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 1.1 matt * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 1.1 matt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 1.1 matt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 1.1 matt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 1.1 matt * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 1.1 matt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 1.1 matt * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 1.1 matt */
26 1.1 matt
27 1.1 matt /*
28 1.1 matt * DEC EtherWORKS 3 Ethernet Controllers
29 1.1 matt *
30 1.1 matt * Written by Matt Thomas
31 1.1 matt *
32 1.1 matt * This driver supports the LEMAC (DE203, DE204, and DE205) cards.
33 1.1 matt */
34 1.1 matt
35 1.1 matt #include "bpfilter.h"
36 1.1 matt
37 1.1 matt #include <sys/param.h>
38 1.1 matt #include <sys/systm.h>
39 1.1 matt #include <sys/mbuf.h>
40 1.1 matt #include <sys/socket.h>
41 1.1 matt #include <sys/ioctl.h>
42 1.1 matt #include <sys/errno.h>
43 1.1 matt #include <sys/syslog.h>
44 1.1 matt #include <sys/select.h>
45 1.1 matt #include <sys/device.h>
46 1.1 matt #include <sys/queue.h>
47 1.1 matt
48 1.1 matt #include <net/if.h>
49 1.1 matt #include <net/if_dl.h>
50 1.1 matt #include <net/if_ether.h>
51 1.1 matt #include <net/if_media.h>
52 1.1 matt
53 1.1 matt
54 1.1 matt #ifdef INET
55 1.1 matt #include <netinet/in.h>
56 1.1 matt #include <netinet/in_systm.h>
57 1.1 matt #include <netinet/in_var.h>
58 1.1 matt #include <netinet/ip.h>
59 1.1 matt #include <netinet/if_inarp.h>
60 1.1 matt #endif
61 1.1 matt
62 1.1 matt #ifdef NS
63 1.1 matt #include <netns/ns.h>
64 1.1 matt #include <netns/ns_if.h>
65 1.1 matt #endif
66 1.1 matt
67 1.1 matt #if NBPFILTER > 0
68 1.1 matt #include <net/bpf.h>
69 1.1 matt #include <net/bpfdesc.h>
70 1.1 matt #endif
71 1.1 matt
72 1.1 matt #include <machine/cpu.h>
73 1.1 matt #include <machine/bus.h>
74 1.1 matt #include <machine/intr.h>
75 1.1 matt
76 1.1 matt #include <dev/ic/lemacreg.h>
77 1.1 matt #include <dev/ic/lemacvar.h>
78 1.1 matt
79 1.1 matt #include <dev/isa/isavar.h>
80 1.1 matt
81 1.1 matt /*
82 1.1 matt * This keeps track of which ISAs have been through a lc probe sequence.
83 1.1 matt * A simple static variable isn't enough, since it's conceivable that
84 1.1 matt * a system might have more than one ISA bus.
85 1.1 matt *
86 1.1 matt * The "isa_bus" member is a pointer to the parent ISA bus device struct
87 1.1 matt * which will unique per ISA bus.
88 1.1 matt */
89 1.1 matt
90 1.1 matt #define MAXCARDS_PER_ISABUS 4 /* if you have more than 4, you lose */
91 1.1 matt #define MAXSLOTS_PER_BUS 16 /* 0x200-0x3FF in 0x20 incr. */
92 1.1 matt
93 1.1 matt struct lemac_isabus {
94 1.1 matt LIST_ENTRY(lemac_isabus) isa_link;
95 1.1 matt struct device *isa_bus;
96 1.1 matt struct lemac_card {
97 1.1 matt bus_addr_t iobase;
98 1.1 matt bus_addr_t maddr;
99 1.1 matt bus_size_t msize;
100 1.1 matt int irq;
101 1.1 matt int available;
102 1.1 matt } isa_cards[MAXCARDS_PER_ISABUS];
103 1.1 matt };
104 1.1 matt
105 1.1 matt static LIST_HEAD(, lemac_isabus) lemac_isa_buses;
106 1.1 matt static int lemac_isa_buses_inited;
107 1.1 matt
108 1.1 matt static void
109 1.1 matt lemac_isa_card_add(
110 1.1 matt struct lemac_isabus *bus,
111 1.1 matt bus_addr_t iobase,
112 1.1 matt bus_addr_t maddr,
113 1.1 matt bus_size_t msize,
114 1.1 matt int irq)
115 1.1 matt {
116 1.1 matt int idx;
117 1.1 matt
118 1.1 matt for (idx = 0; idx < MAXCARDS_PER_ISABUS; idx++) {
119 1.1 matt if (bus->isa_cards[idx].available == 0) {
120 1.1 matt bus->isa_cards[idx].iobase = iobase;
121 1.1 matt bus->isa_cards[idx].maddr = maddr;
122 1.1 matt bus->isa_cards[idx].msize = msize;
123 1.1 matt bus->isa_cards[idx].irq = irq;
124 1.1 matt bus->isa_cards[idx].available = 1;
125 1.1 matt break;
126 1.1 matt }
127 1.1 matt }
128 1.1 matt }
129 1.1 matt
130 1.1 matt /*
131 1.1 matt * We'd like like to allow the irq, maddr, and iobase addresses to be
132 1.1 matt * wildcarded. So, we probe all the cards the first time lemac_isa_probe()
133 1.1 matt * is called. On subsequent calls we look for matching cards.
134 1.1 matt */
135 1.1 matt static int
136 1.1 matt lemac_isa_probe(
137 1.1 matt struct device *parent,
138 1.1 matt #ifdef __BROKEN_INDIRECT_CONFIG
139 1.1 matt void *match,
140 1.1 matt #else
141 1.1 matt struct cfdata *match,
142 1.1 matt #endif
143 1.1 matt void *aux)
144 1.1 matt {
145 1.1 matt struct isa_attach_args * const ia = aux;
146 1.1 matt struct lemac_isabus *bus;
147 1.1 matt int idx;
148 1.1 matt
149 1.1 matt if (lemac_isa_buses_inited == 0) {
150 1.1 matt LIST_INIT(&lemac_isa_buses);
151 1.1 matt lemac_isa_buses_inited = 1;
152 1.1 matt }
153 1.1 matt
154 1.1 matt /*
155 1.1 matt * Probe this bus if we haven't done so already.
156 1.1 matt */
157 1.1 matt for (bus = lemac_isa_buses.lh_first; bus != NULL;
158 1.1 matt bus = bus->isa_link.le_next) {
159 1.1 matt if (bus->isa_bus == parent)
160 1.1 matt break;
161 1.1 matt }
162 1.1 matt
163 1.1 matt if (bus == NULL) {
164 1.1 matt bus_addr_t iobase;
165 1.1 matt /*
166 1.1 matt * Mark this bus so we don't probe it again.
167 1.1 matt */
168 1.1 matt bus = (struct lemac_isabus *)
169 1.1 matt malloc(sizeof(struct lemac_isabus), M_DEVBUF, M_NOWAIT);
170 1.1 matt if (bus == NULL)
171 1.1 matt panic("lemac_isa_probe: can't allocate state storage for %s",
172 1.1 matt parent->dv_xname);
173 1.1 matt
174 1.1 matt bus->isa_bus = parent;
175 1.1 matt LIST_INSERT_HEAD(&lemac_isa_buses, bus, isa_link);
176 1.1 matt
177 1.1 matt for (iobase = LEMAC_IOBASE_LOW; iobase < LEMAC_IOBASE_HIGH;
178 1.1 matt iobase += LEMAC_IOSIZE) {
179 1.1 matt bus_space_handle_t ioh;
180 1.1 matt /*
181 1.1 matt * Map the LEMACs port-space for the probe sequence.
182 1.1 matt */
183 1.1 matt if (bus_space_map(ia->ia_iot, iobase, LEMAC_IOSIZE, 0, &ioh))
184 1.1 matt continue;
185 1.1 matt
186 1.1 matt /*
187 1.1 matt * Read the Ethernet address from the EEPROM.
188 1.1 matt * It must start with on the DEC OUIs and pass the
189 1.1 matt * DEC ethernet checksum test.
190 1.1 matt */
191 1.1 matt
192 1.1 matt if (lemac_port_check(ia->ia_iot, ioh)) {
193 1.1 matt int irq;
194 1.1 matt bus_addr_t maddr;
195 1.1 matt bus_addr_t msize;
196 1.1 matt bus_space_handle_t memh;
197 1.1 matt
198 1.1 matt lemac_info_get(ia->ia_iot, ioh, &maddr, &msize, &irq);
199 1.1 matt if (!bus_space_map(ia->ia_memt, maddr, msize, 0, &memh)) {
200 1.1 matt lemac_isa_card_add(bus, iobase, maddr, msize, irq);
201 1.1 matt bus_space_unmap(ia->ia_memt, memh, msize);
202 1.1 matt }
203 1.1 matt }
204 1.1 matt
205 1.1 matt bus_space_unmap(ia->ia_iot, ioh, LEMAC_IOSIZE);
206 1.1 matt }
207 1.1 matt }
208 1.1 matt
209 1.1 matt for (idx = 0; idx < MAXCARDS_PER_ISABUS; idx++) {
210 1.1 matt if (bus->isa_cards[idx].available != 1)
211 1.1 matt continue;
212 1.1 matt if (ia->ia_iobase != IOBASEUNK
213 1.1 matt && ia->ia_iobase != bus->isa_cards[idx].iobase)
214 1.1 matt continue;
215 1.1 matt if (ia->ia_maddr != MADDRUNK
216 1.1 matt && ia->ia_maddr != bus->isa_cards[idx].maddr)
217 1.1 matt continue;
218 1.1 matt if (ia->ia_irq != IRQUNK && ia->ia_irq != bus->isa_cards[idx].irq)
219 1.1 matt continue;
220 1.1 matt break;
221 1.1 matt }
222 1.1 matt if (idx == MAXCARDS_PER_ISABUS)
223 1.1 matt return 0;
224 1.1 matt
225 1.1 matt bus->isa_cards[idx].available++;
226 1.1 matt ia->ia_iobase = bus->isa_cards[idx].iobase;
227 1.1 matt ia->ia_irq = bus->isa_cards[idx].irq;
228 1.1 matt ia->ia_iosize = LEMAC_IOSIZE;
229 1.1 matt ia->ia_maddr = bus->isa_cards[idx].maddr;
230 1.1 matt ia->ia_msize = bus->isa_cards[idx].msize;
231 1.1 matt return 1;
232 1.1 matt }
233 1.1 matt
234 1.1 matt static void
235 1.1 matt lemac_isa_attach(
236 1.1 matt struct device *parent,
237 1.1 matt struct device *self,
238 1.1 matt void *aux)
239 1.1 matt {
240 1.1 matt lemac_softc_t *sc = (void *)self;
241 1.1 matt struct isa_attach_args *ia = aux;
242 1.1 matt
243 1.1 matt
244 1.1 matt /* Map i/o space. */
245 1.1 matt sc->sc_iot = ia->ia_iot;
246 1.1 matt if (bus_space_map(sc->sc_iot, ia->ia_iobase, ia->ia_iosize, 0, &sc->sc_ioh))
247 1.1 matt panic("\n%s: can't map i/o space 0x%x-0x%x\n",
248 1.1 matt sc->sc_dv.dv_xname,
249 1.1 matt ia->ia_iobase, ia->ia_iobase + ia->ia_iosize - 1);
250 1.1 matt
251 1.1 matt if (ia->ia_msize && ia->ia_maddr) {
252 1.1 matt sc->sc_memt = ia->ia_memt;
253 1.1 matt /* Map memory space. */
254 1.1 matt if (bus_space_map(sc->sc_memt, ia->ia_maddr, ia->ia_msize, 0, &sc->sc_memh))
255 1.1 matt panic("\n%s: can't map iomem space 0x%x-0x%x\n",
256 1.1 matt sc->sc_dv.dv_xname,
257 1.1 matt ia->ia_maddr, ia->ia_maddr + ia->ia_msize - 1);
258 1.1 matt }
259 1.1 matt
260 1.1 matt sc->sc_ats = shutdownhook_establish(lemac_shutdown, sc);
261 1.1 matt if (sc->sc_ats == NULL)
262 1.1 matt printf("\n%s: warning: couldn't establish shutdown hook\n",
263 1.1 matt sc->sc_dv.dv_xname);
264 1.1 matt
265 1.1 matt lemac_ifattach(sc);
266 1.1 matt
267 1.1 matt sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
268 1.1 matt IPL_NET, lemac_intr, sc);
269 1.1 matt }
270 1.1 matt
271 1.1 matt struct cfattach lc_isa_ca = {
272 1.1 matt sizeof(lemac_softc_t), lemac_isa_probe, lemac_isa_attach
273 1.1 matt };
274