2 * EISA bus probe and attach routines
4 * Copyright (c) 1995, 1996 Justin T. Gibbs.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice immediately at the beginning of the file, without modification,
12 * this list of conditions, and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/queue.h>
41 #include <sys/limits.h>
42 #include <sys/malloc.h>
43 #include <sys/kernel.h>
44 #include <sys/module.h>
47 #include <machine/bus.h>
48 #include <machine/resource.h>
51 #include <dev/eisa/eisaconf.h>
53 typedef struct resvaddr {
54 u_long addr; /* start address */
55 u_long size; /* size of reserved area */
57 struct resource *res; /* resource manager handle */
58 LIST_ENTRY(resvaddr) links; /* List links */
61 LIST_HEAD(resvlist, resvaddr);
67 TAILQ_ENTRY(irq_node) links;
70 TAILQ_HEAD(irqlist, irq_node);
74 struct resvlist ioaddrs; /* list of reserved I/O ranges */
75 struct resvlist maddrs; /* list of reserved memory ranges */
76 struct irqlist irqs; /* list of reserved irqs */
79 /* To be replaced by the "super device" generic device structure... */
82 struct eisa_ioconf ioconf;
88 #define EISA_SLOTS 10 /* PCI clashes with higher ones.. fix later */
90 int num_eisa_slots = EISA_SLOTS;
91 TUNABLE_INT("hw.eisa_slots", &num_eisa_slots);
93 static devclass_t eisa_devclass;
95 static int eisa_probe_slot(int slot, eisa_id_t *eisa_id);
96 static struct irq_node *eisa_find_irq(struct eisa_device *e_dev, int rid);
97 static struct resvaddr *eisa_find_maddr(struct eisa_device *e_dev, int rid);
98 static struct resvaddr *eisa_find_ioaddr(struct eisa_device *e_dev, int rid);
101 mainboard_probe(device_t dev)
104 eisa_id_t id = eisa_get_id(dev);
106 if (eisa_get_slot(dev) != 0)
109 idstring = (char *)malloc(8 + sizeof(" (System Board)") + 1,
111 if (idstring == NULL)
112 panic("Eisa probe unable to malloc");
113 sprintf(idstring, "%c%c%c%03x%01x (System Board)",
114 EISA_MFCTR_CHAR0(id), EISA_MFCTR_CHAR1(id), EISA_MFCTR_CHAR2(id),
115 EISA_PRODUCT_ID(id), EISA_REVISION_ID(id));
116 device_set_desc(dev, idstring);
122 mainboard_attach(device_t dev)
127 static device_method_t mainboard_methods[] = {
128 /* Device interface */
129 DEVMETHOD(device_probe, mainboard_probe),
130 DEVMETHOD(device_attach, mainboard_attach),
135 static driver_t mainboard_driver = {
141 static devclass_t mainboard_devclass;
143 DRIVER_MODULE(mainboard, eisa, mainboard_driver, mainboard_devclass, 0, 0);
146 ** probe for EISA devices
149 eisa_probe(device_t dev)
151 int devices_found, slot;
152 struct eisa_device *e_dev;
156 device_set_desc(dev, "EISA bus");
159 for (slot = 0; slot < num_eisa_slots; slot++) {
161 if (eisa_probe_slot(slot, &eisa_id)) {
163 * If there's no card in the first slot (the
164 * mainboard), then the system doesn't have EISA.
165 * We abort the probe early in this case since
166 * continuing on causes a hang on some systems.
167 * Interestingly enough, the inb has been seen to
177 /* Prepare an eisa_device_node for this slot */
178 e_dev = (struct eisa_device *)malloc(sizeof(*e_dev),
179 M_DEVBUF, M_NOWAIT|M_ZERO);
181 device_printf(dev, "cannot malloc eisa_device");
182 break; /* Try to attach what we have already */
186 e_dev->ioconf.slot = slot;
188 /* Initialize our lists of reserved addresses */
189 LIST_INIT(&(e_dev->ioconf.ioaddrs));
190 LIST_INIT(&(e_dev->ioconf.maddrs));
191 TAILQ_INIT(&(e_dev->ioconf.irqs));
193 child = device_add_child(dev, NULL, -1);
194 device_set_ivars(child, e_dev);
198 * EISA busses themselves are not easily detectable, the easiest way
199 * to tell if there is an eisa bus is if we found something - there
200 * should be a motherboard "card" there somewhere.
202 return (devices_found ? 0 : ENXIO);
206 eisa_probe_slot(int slot, eisa_id_t *eisa_id)
209 int base, i, id_size;
212 id_size = sizeof(probe_id);
213 base = 0x0c80 + (slot * 0x1000);
215 for (i = 0; i < id_size; i++)
216 probe_id |= inb(base + i) << ((id_size - i - 1) * CHAR_BIT);
218 /* If we found a card, return its EISA id. */
219 if ((probe_id & 0x80000000) == 0) {
228 eisa_probe_nomatch(device_t dev, device_t child)
230 u_int32_t eisa_id = eisa_get_id(child);
231 u_int8_t slot = eisa_get_slot(child);
233 device_printf(dev, "%c%c%c%03x%01x (0x%08x) at slot %d (no driver attached)\n",
234 EISA_MFCTR_CHAR0(eisa_id), EISA_MFCTR_CHAR1(eisa_id),
235 EISA_MFCTR_CHAR2(eisa_id), EISA_PRODUCT_ID(eisa_id),
236 EISA_REVISION_ID(eisa_id), eisa_id, slot);
241 eisa_print_child(device_t dev, device_t child)
243 struct eisa_device * e_dev = device_get_ivars(child);
245 struct irq_node * irq;
246 struct resvaddr * resv;
249 retval += bus_print_child_header(dev, child);
251 while ((resv = eisa_find_ioaddr(e_dev, rid++))) {
252 if (resv->size == 1 || (resv->flags & RESVADDR_BITMASK))
253 retval += printf("%s%lx", rid == 1 ? " port 0x" : ",0x",
256 retval += printf("%s%lx-0x%lx", rid == 1 ? " port 0x" :
257 ",0x", resv->addr, resv->addr + resv->size - 1);
260 while ((resv = eisa_find_maddr(e_dev, rid++))) {
261 if (resv->size == 1 || (resv->flags & RESVADDR_BITMASK))
262 retval += printf("%s%lx", rid == 1 ? " mem 0x" : ",0x",
265 retval += printf("%s%lx-0x%lx", rid == 1 ? " mem 0x" :
266 ",0x", resv->addr, resv->addr + resv->size - 1);
269 while ((irq = eisa_find_irq(e_dev, rid++)) != NULL)
270 retval += printf(" irq %d (%s)", irq->irq_no,
271 irq->irq_trigger ? "level" : "edge");
272 retval += printf(" at slot %d on %s\n", eisa_get_slot(child),
273 device_get_nameunit(dev));
278 static struct irq_node *
279 eisa_find_irq(struct eisa_device *e_dev, int rid)
282 struct irq_node *irq;
284 for (i = 0, irq = TAILQ_FIRST(&e_dev->ioconf.irqs);
285 i < rid && irq != NULL; i++, irq = TAILQ_NEXT(irq, links))
291 static struct resvaddr *
292 eisa_find_maddr(struct eisa_device *e_dev, int rid)
295 struct resvaddr *resv;
297 for (i = 0, resv = LIST_FIRST(&e_dev->ioconf.maddrs);
298 i < rid && resv != NULL; i++, resv = LIST_NEXT(resv, links))
304 static struct resvaddr *
305 eisa_find_ioaddr(struct eisa_device *e_dev, int rid)
308 struct resvaddr *resv;
310 for (i = 0, resv = LIST_FIRST(&e_dev->ioconf.ioaddrs);
311 i < rid && resv != NULL; i++, resv = LIST_NEXT(resv, links))
318 eisa_read_ivar(device_t dev, device_t child, int which, u_long *result)
320 struct eisa_device *e_dev = device_get_ivars(child);
321 struct irq_node *irq;
325 *result = e_dev->ioconf.slot;
333 /* XXX only first irq */
334 if ((irq = eisa_find_irq(e_dev, 0)) != NULL)
335 *result = irq->irq_no;
348 eisa_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
353 static struct resource *
354 eisa_alloc_resource(device_t dev, device_t child, int type, int *rid,
355 u_long start, u_long end, u_long count, u_int flags)
358 struct eisa_device *e_dev = device_get_ivars(child);
359 struct resource *rv, **rvp = 0;
361 isdefault = (device_get_parent(child) == dev &&
362 start == 0UL && end == ~0UL && count == 1);
367 struct irq_node * irq = eisa_find_irq(e_dev, *rid);
370 start = end = irq->irq_no;
372 if (irq->irq_trigger == EISA_TRIGGER_LEVEL)
373 flags |= RF_SHAREABLE;
375 flags &= ~RF_SHAREABLE;
381 struct resvaddr *resv;
383 resv = eisa_find_maddr(e_dev, *rid);
388 end = resv->addr + (resv->size - 1);
396 struct resvaddr *resv;
398 resv = eisa_find_ioaddr(e_dev, *rid);
403 end = resv->addr + (resv->size - 1);
413 rv = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
414 type, rid, start, end, count, flags);
422 eisa_release_resource(device_t dev, device_t child, int type, int rid,
426 struct eisa_device *e_dev = device_get_ivars(child);
427 struct resvaddr *resv = 0;
431 if (eisa_find_irq(e_dev, rid) == NULL)
436 if (device_get_parent(child) == dev)
437 resv = eisa_find_maddr(e_dev, rid);
442 if (device_get_parent(child) == dev)
443 resv = eisa_find_ioaddr(e_dev, rid);
450 rv = BUS_RELEASE_RESOURCE(device_get_parent(dev), child, type, rid, r);
461 eisa_add_intr_m(device_t eisa, device_t dev, int irq, int trigger)
463 struct eisa_device *e_dev = device_get_ivars(dev);
464 struct irq_node *irq_info;
466 irq_info = (struct irq_node *)malloc(sizeof(*irq_info), M_DEVBUF,
468 if (irq_info == NULL)
471 irq_info->irq_no = irq;
472 irq_info->irq_trigger = trigger;
473 irq_info->idesc = NULL;
474 TAILQ_INSERT_TAIL(&e_dev->ioconf.irqs, irq_info, links);
479 eisa_add_resvaddr(struct eisa_device *e_dev, struct resvlist *head, u_long base,
480 u_long size, int flags)
482 resvaddr_t *reservation;
484 reservation = (resvaddr_t *)malloc(sizeof(resvaddr_t),
489 reservation->addr = base;
490 reservation->size = size;
491 reservation->flags = flags;
493 if (!LIST_FIRST(head)) {
494 LIST_INSERT_HEAD(head, reservation, links);
498 LIST_FOREACH(node, head, links) {
499 if (node->addr > reservation->addr) {
501 * List is sorted in increasing
504 LIST_INSERT_BEFORE(node, reservation, links);
508 if (node->addr == reservation->addr) {
510 * If the entry we want to add
511 * matches any already in here,
514 free(reservation, M_DEVBUF);
518 if (!LIST_NEXT(node, links)) {
519 LIST_INSERT_AFTER(node, reservation, links);
528 eisa_add_mspace_m(device_t eisa, device_t dev, u_long mbase, u_long msize,
531 struct eisa_device *e_dev = device_get_ivars(dev);
533 return (eisa_add_resvaddr(e_dev, &(e_dev->ioconf.maddrs), mbase, msize,
538 eisa_add_iospace_m(device_t eisa, device_t dev, u_long iobase, u_long iosize,
541 struct eisa_device *e_dev = device_get_ivars(dev);
543 return (eisa_add_resvaddr(e_dev, &(e_dev->ioconf.ioaddrs), iobase,
547 static device_method_t eisa_methods[] = {
548 /* Device interface */
549 DEVMETHOD(device_probe, eisa_probe),
550 DEVMETHOD(device_attach, bus_generic_attach),
551 DEVMETHOD(device_shutdown, bus_generic_shutdown),
552 DEVMETHOD(device_suspend, bus_generic_suspend),
553 DEVMETHOD(device_resume, bus_generic_resume),
556 DEVMETHOD(bus_print_child, eisa_print_child),
557 DEVMETHOD(bus_probe_nomatch, eisa_probe_nomatch),
558 DEVMETHOD(bus_read_ivar, eisa_read_ivar),
559 DEVMETHOD(bus_write_ivar, eisa_write_ivar),
560 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
561 DEVMETHOD(bus_alloc_resource, eisa_alloc_resource),
562 DEVMETHOD(bus_release_resource, eisa_release_resource),
563 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
564 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
565 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
566 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
569 DEVMETHOD(eisa_add_intr, eisa_add_intr_m),
570 DEVMETHOD(eisa_add_iospace, eisa_add_iospace_m),
571 DEVMETHOD(eisa_add_mspace, eisa_add_mspace_m),
576 static driver_t eisa_driver = {
582 DRIVER_MODULE(eisa, eisab, eisa_driver, eisa_devclass, 0, 0);
583 DRIVER_MODULE(eisa, legacy, eisa_driver, eisa_devclass, 0, 0);