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
31 * $FreeBSD: src/sys/dev/eisa/eisaconf.c,v 1.55 2000/01/14 07:13:57 peter Exp $
32 * $DragonFly: src/sys/bus/eisa/eisaconf.c,v 1.7 2006/09/05 00:55:34 dillon Exp $
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/queue.h>
40 #include <sys/malloc.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
45 #include <machine/limits.h>
46 #include <machine/bus.h>
47 #include <machine/resource.h>
52 typedef struct resvaddr {
53 u_long addr; /* start address */
54 u_long size; /* size of reserved area */
56 struct resource *res; /* resource manager handle */
57 LIST_ENTRY(resvaddr) links; /* List links */
60 LIST_HEAD(resvlist, resvaddr);
66 TAILQ_ENTRY(irq_node) links;
69 TAILQ_HEAD(irqlist, irq_node);
73 struct resvlist ioaddrs; /* list of reserved I/O ranges */
74 struct resvlist maddrs; /* list of reserved memory ranges */
75 struct irqlist irqs; /* list of reserved irqs */
78 /* To be replaced by the "super device" generic device structure... */
81 struct eisa_ioconf ioconf;
85 /* Global variable, so UserConfig can change it. */
88 #define EISA_SLOTS 10 /* PCI clashes with higher ones.. fix later */
90 int num_eisa_slots = EISA_SLOTS;
92 static devclass_t eisa_devclass;
94 static void eisa_reg_print(device_t, char *, char *, int *);
95 static struct irq_node * eisa_find_irq(struct eisa_device *e_dev, int rid);
96 static struct resvaddr * eisa_find_maddr(struct eisa_device *e_dev, int rid);
97 static struct resvaddr * eisa_find_ioaddr(struct eisa_device *e_dev, int rid);
100 mainboard_probe(device_t dev)
103 eisa_id_t id = eisa_get_id(dev);
105 if (eisa_get_slot(dev) != 0)
108 idstring = malloc(8 + sizeof(" (System Board)") + 1,
109 M_DEVBUF, M_INTWAIT);
110 sprintf(idstring, "%c%c%c%03x%01x (System Board)",
111 EISA_MFCTR_CHAR0(id),
112 EISA_MFCTR_CHAR1(id),
113 EISA_MFCTR_CHAR2(id),
115 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)
152 struct eisa_device *e_dev;
154 int eisaBase = 0xc80;
156 int devices_found = 0;
158 device_set_desc(dev, "EISA bus");
160 for (slot = 0; slot < num_eisa_slots; eisaBase+=0x1000, slot++) {
161 int id_size = sizeof(eisa_id);
163 for( i = 0; i < id_size; i++ ) {
164 outb(eisaBase,0x80 + i); /*Some cards require priming*/
165 eisa_id |= inb(eisaBase+i) << ((id_size-i-1)*CHAR_BIT);
167 if (eisa_id & 0x80000000) {
170 continue; /* no EISA card in slot */
175 /* Prepare an eisa_device_node for this slot */
176 e_dev = kmalloc(sizeof(*e_dev), M_DEVBUF, M_INTWAIT | M_ZERO);
179 e_dev->ioconf.slot = slot;
181 /* Initialize our lists of reserved addresses */
182 LIST_INIT(&(e_dev->ioconf.ioaddrs));
183 LIST_INIT(&(e_dev->ioconf.maddrs));
184 TAILQ_INIT(&(e_dev->ioconf.irqs));
186 child = device_add_child(dev, NULL, -1);
187 device_set_ivars(child, e_dev);
191 * EISA busses themselves are not easily detectable, the easiest way
192 * to tell if there is an eisa bus is if we found something - there
193 * should be a motherboard "card" there somewhere.
195 return devices_found ? 0 : ENXIO;
199 eisa_probe_nomatch(device_t dev, device_t child)
201 u_int32_t eisa_id = eisa_get_id(child);
202 u_int8_t slot = eisa_get_slot(child);
204 device_printf(dev, "unknown card %c%c%c%03x%01x (0x%08x) at slot %d\n",
205 EISA_MFCTR_CHAR0(eisa_id),
206 EISA_MFCTR_CHAR1(eisa_id),
207 EISA_MFCTR_CHAR2(eisa_id),
208 EISA_PRODUCT_ID(eisa_id),
209 EISA_REVISION_ID(eisa_id),
217 eisa_reg_print(device_t dev, char *string, char *separator, int *column)
219 int length = strlen(string);
221 length += (separator ? 2 : 1);
223 if (((*column) + length) >= MAX_COL) {
226 } else if ((*column) != 0) {
228 printf("%c", *separator);
235 if ((*column) == 0) {
236 (*column) += device_printf(dev, "%s", string);
238 (*column) += printf("%s", string);
245 eisa_print_child(device_t dev, device_t child)
248 struct eisa_device * e_dev = device_get_ivars(child);
250 struct irq_node * irq;
251 struct resvaddr * resv;
252 char separator = ',';
256 if (device_get_desc(child)) {
257 snprintf(buf, sizeof(buf), "<%s>", device_get_desc(child));
258 eisa_reg_print(child, buf, NULL, &column);
262 while ((resv = eisa_find_ioaddr(e_dev, rid++))) {
263 if ((resv->size == 1) ||
264 (resv->flags & RESVADDR_BITMASK)) {
265 snprintf(buf, sizeof(buf), "%s%lx",
266 ((rid == 1) ? "at 0x" : "0x"),
269 snprintf(buf, sizeof(buf), "%s%lx-0x%lx",
270 ((rid == 1) ? "at 0x" : "0x"),
272 (resv->addr + (resv->size - 1)));
274 eisa_reg_print(child, buf,
275 ((rid == 2) ? &separator : NULL), &column);
279 while ((resv = eisa_find_maddr(e_dev, rid++))) {
280 if ((resv->size == 1) ||
281 (resv->flags & RESVADDR_BITMASK)) {
282 snprintf(buf, sizeof(buf), "%s%lx",
283 ((rid == 1) ? "at 0x" : "0x"),
286 snprintf(buf, sizeof(buf), "%s%lx-0x%lx",
287 ((rid == 1) ? "at 0x" : "0x"),
289 (resv->addr + (resv->size - 1)));
291 eisa_reg_print(child, buf,
292 ((rid == 2) ? &separator : NULL), &column);
296 while ((irq = eisa_find_irq(e_dev, rid++)) != NULL) {
297 snprintf(buf, sizeof(buf), "irq %d (%s)", irq->irq_no,
298 (irq->irq_trigger ? "level" : "edge"));
299 eisa_reg_print(child, buf,
300 ((rid == 1) ? &separator : NULL), &column);
303 snprintf(buf, sizeof(buf), "on %s slot %d\n",
304 device_get_nameunit(dev), eisa_get_slot(child));
305 eisa_reg_print(child, buf, NULL, &column);
310 static struct irq_node *
311 eisa_find_irq(struct eisa_device *e_dev, int rid)
314 struct irq_node *irq;
316 for (i = 0, irq = TAILQ_FIRST(&e_dev->ioconf.irqs);
318 i++, irq = TAILQ_NEXT(irq, links))
327 static struct resvaddr *
328 eisa_find_maddr(struct eisa_device *e_dev, int rid)
331 struct resvaddr *resv;
333 for (i = 0, resv = LIST_FIRST(&e_dev->ioconf.maddrs);
335 i++, resv = LIST_NEXT(resv, links))
341 static struct resvaddr *
342 eisa_find_ioaddr(struct eisa_device *e_dev, int rid)
345 struct resvaddr *resv;
347 for (i = 0, resv = LIST_FIRST(&e_dev->ioconf.ioaddrs);
349 i++, resv = LIST_NEXT(resv, links))
356 eisa_read_ivar(device_t dev, device_t child, int which, u_long *result)
358 struct eisa_device *e_dev = device_get_ivars(child);
359 struct irq_node *irq;
363 *result = e_dev->ioconf.slot;
371 /* XXX only first irq */
372 if ((irq = eisa_find_irq(e_dev, 0)) != NULL) {
373 *result = irq->irq_no;
387 eisa_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
392 static struct resource *
393 eisa_alloc_resource(device_t dev, device_t child, int type, int *rid,
394 u_long start, u_long end, u_long count, u_int flags)
397 struct eisa_device *e_dev = device_get_ivars(child);
398 struct resource *rv, **rvp = 0;
400 isdefault = (device_get_parent(child) == dev
401 && start == 0UL && end == ~0UL && count == 1);
406 struct irq_node * irq = eisa_find_irq(e_dev, *rid);
409 start = end = irq->irq_no;
411 if (irq->irq_trigger == EISA_TRIGGER_LEVEL) {
412 flags |= RF_SHAREABLE;
414 flags &= ~RF_SHAREABLE;
421 struct resvaddr *resv;
423 resv = eisa_find_maddr(e_dev, *rid);
428 end = resv->addr + (resv->size - 1);
436 struct resvaddr *resv;
438 resv = eisa_find_ioaddr(e_dev, *rid);
443 end = resv->addr + (resv->size - 1);
453 rv = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
454 type, rid, start, end, count, flags);
462 eisa_release_resource(device_t dev, device_t child, int type, int rid,
466 struct eisa_device *e_dev = device_get_ivars(child);
467 struct resvaddr *resv = 0;
471 if (eisa_find_irq(e_dev, rid) == NULL)
476 if (device_get_parent(child) == dev)
477 resv = eisa_find_maddr(e_dev, rid);
482 if (device_get_parent(child) == dev)
483 resv = eisa_find_ioaddr(e_dev, rid);
490 rv = BUS_RELEASE_RESOURCE(device_get_parent(dev), child, type, rid, r);
501 eisa_add_intr(device_t dev, int irq, int trigger)
503 struct eisa_device *e_dev = device_get_ivars(dev);
504 struct irq_node *irq_info;
506 irq_info = kmalloc(sizeof(*irq_info), M_DEVBUF, M_INTWAIT);
507 irq_info->irq_no = irq;
508 irq_info->irq_trigger = trigger;
509 irq_info->idesc = NULL;
510 TAILQ_INSERT_TAIL(&e_dev->ioconf.irqs, irq_info, links);
515 eisa_add_resvaddr(struct eisa_device *e_dev, struct resvlist *head, u_long base,
516 u_long size, int flags)
518 resvaddr_t *reservation;
520 reservation = kmalloc(sizeof(resvaddr_t), M_DEVBUF, M_INTWAIT);
521 reservation->addr = base;
522 reservation->size = size;
523 reservation->flags = flags;
525 if (!head->lh_first) {
526 LIST_INSERT_HEAD(head, reservation, links);
530 for(node = head->lh_first; node; node = node->links.le_next) {
531 if (node->addr > reservation->addr) {
533 * List is sorted in increasing
536 LIST_INSERT_BEFORE(node, reservation, links);
540 if (node->addr == reservation->addr) {
542 * If the entry we want to add
543 * matches any already in here,
546 kfree(reservation, M_DEVBUF);
550 if (!node->links.le_next) {
551 LIST_INSERT_AFTER(node, reservation, links);
560 eisa_add_mspace(device_t dev, u_long mbase, u_long msize, int flags)
562 struct eisa_device *e_dev = device_get_ivars(dev);
564 return eisa_add_resvaddr(e_dev, &(e_dev->ioconf.maddrs), mbase, msize,
569 eisa_add_iospace(device_t dev, u_long iobase, u_long iosize, int flags)
571 struct eisa_device *e_dev = device_get_ivars(dev);
573 return eisa_add_resvaddr(e_dev, &(e_dev->ioconf.ioaddrs), iobase,
577 static device_method_t eisa_methods[] = {
578 /* Device interface */
579 DEVMETHOD(device_probe, eisa_probe),
580 DEVMETHOD(device_attach, bus_generic_attach),
581 DEVMETHOD(device_shutdown, bus_generic_shutdown),
582 DEVMETHOD(device_suspend, bus_generic_suspend),
583 DEVMETHOD(device_resume, bus_generic_resume),
586 DEVMETHOD(bus_print_child, eisa_print_child),
587 DEVMETHOD(bus_probe_nomatch, eisa_probe_nomatch),
588 DEVMETHOD(bus_read_ivar, eisa_read_ivar),
589 DEVMETHOD(bus_write_ivar, eisa_write_ivar),
590 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
591 DEVMETHOD(bus_alloc_resource, eisa_alloc_resource),
592 DEVMETHOD(bus_release_resource, eisa_release_resource),
593 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
594 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
595 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
596 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
601 static driver_t eisa_driver = {
607 DRIVER_MODULE(eisa, isab, eisa_driver, eisa_devclass, 0, 0);
608 DRIVER_MODULE(eisa, nexus, eisa_driver, eisa_devclass, 0, 0);