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.4 2004/03/12 03:24:43 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)
168 continue; /* no EISA card in slot */
172 /* Prepare an eisa_device_node for this slot */
173 e_dev = malloc(sizeof(*e_dev), M_DEVBUF, M_INTWAIT | M_ZERO);
176 e_dev->ioconf.slot = slot;
178 /* Initialize our lists of reserved addresses */
179 LIST_INIT(&(e_dev->ioconf.ioaddrs));
180 LIST_INIT(&(e_dev->ioconf.maddrs));
181 TAILQ_INIT(&(e_dev->ioconf.irqs));
183 child = device_add_child(dev, NULL, -1);
184 device_set_ivars(child, e_dev);
188 * EISA busses themselves are not easily detectable, the easiest way
189 * to tell if there is an eisa bus is if we found something - there
190 * should be a motherboard "card" there somewhere.
192 return devices_found ? 0 : ENXIO;
196 eisa_probe_nomatch(device_t dev, device_t child)
198 u_int32_t eisa_id = eisa_get_id(child);
199 u_int8_t slot = eisa_get_slot(child);
201 device_printf(dev, "unknown card %c%c%c%03x%01x (0x%08x) at slot %d\n",
202 EISA_MFCTR_CHAR0(eisa_id),
203 EISA_MFCTR_CHAR1(eisa_id),
204 EISA_MFCTR_CHAR2(eisa_id),
205 EISA_PRODUCT_ID(eisa_id),
206 EISA_REVISION_ID(eisa_id),
214 eisa_reg_print (dev, string, separator, column)
220 int length = strlen(string);
222 length += (separator ? 2 : 1);
224 if (((*column) + length) >= MAX_COL) {
227 } else if ((*column) != 0) {
229 printf("%c", *separator);
236 if ((*column) == 0) {
237 (*column) += device_printf(dev, "%s", string);
239 (*column) += printf("%s", string);
246 eisa_print_child(device_t dev, device_t child)
249 struct eisa_device * e_dev = device_get_ivars(child);
251 struct irq_node * irq;
252 struct resvaddr * resv;
253 char separator = ',';
257 if (device_get_desc(child)) {
258 snprintf(buf, sizeof(buf), "<%s>", device_get_desc(child));
259 eisa_reg_print(child, buf, NULL, &column);
263 while ((resv = eisa_find_ioaddr(e_dev, rid++))) {
264 if ((resv->size == 1) ||
265 (resv->flags & RESVADDR_BITMASK)) {
266 snprintf(buf, sizeof(buf), "%s%lx",
267 ((rid == 1) ? "at 0x" : "0x"),
270 snprintf(buf, sizeof(buf), "%s%lx-0x%lx",
271 ((rid == 1) ? "at 0x" : "0x"),
273 (resv->addr + (resv->size - 1)));
275 eisa_reg_print(child, buf,
276 ((rid == 2) ? &separator : NULL), &column);
280 while ((resv = eisa_find_maddr(e_dev, rid++))) {
281 if ((resv->size == 1) ||
282 (resv->flags & RESVADDR_BITMASK)) {
283 snprintf(buf, sizeof(buf), "%s%lx",
284 ((rid == 1) ? "at 0x" : "0x"),
287 snprintf(buf, sizeof(buf), "%s%lx-0x%lx",
288 ((rid == 1) ? "at 0x" : "0x"),
290 (resv->addr + (resv->size - 1)));
292 eisa_reg_print(child, buf,
293 ((rid == 2) ? &separator : NULL), &column);
297 while ((irq = eisa_find_irq(e_dev, rid++)) != NULL) {
298 snprintf(buf, sizeof(buf), "irq %d (%s)", irq->irq_no,
299 (irq->irq_trigger ? "level" : "edge"));
300 eisa_reg_print(child, buf,
301 ((rid == 1) ? &separator : NULL), &column);
304 snprintf(buf, sizeof(buf), "on %s slot %d\n",
305 device_get_nameunit(dev), eisa_get_slot(child));
306 eisa_reg_print(child, buf, NULL, &column);
311 static struct irq_node *
312 eisa_find_irq(struct eisa_device *e_dev, int rid)
315 struct irq_node *irq;
317 for (i = 0, irq = TAILQ_FIRST(&e_dev->ioconf.irqs);
319 i++, irq = TAILQ_NEXT(irq, links))
328 static struct resvaddr *
329 eisa_find_maddr(struct eisa_device *e_dev, int rid)
332 struct resvaddr *resv;
334 for (i = 0, resv = LIST_FIRST(&e_dev->ioconf.maddrs);
336 i++, resv = LIST_NEXT(resv, links))
342 static struct resvaddr *
343 eisa_find_ioaddr(struct eisa_device *e_dev, int rid)
346 struct resvaddr *resv;
348 for (i = 0, resv = LIST_FIRST(&e_dev->ioconf.ioaddrs);
350 i++, resv = LIST_NEXT(resv, links))
357 eisa_read_ivar(device_t dev, device_t child, int which, u_long *result)
359 struct eisa_device *e_dev = device_get_ivars(child);
360 struct irq_node *irq;
364 *result = e_dev->ioconf.slot;
372 /* XXX only first irq */
373 if ((irq = eisa_find_irq(e_dev, 0)) != NULL) {
374 *result = irq->irq_no;
388 eisa_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
393 static struct resource *
394 eisa_alloc_resource(device_t dev, device_t child, int type, int *rid,
395 u_long start, u_long end, u_long count, u_int flags)
398 struct eisa_device *e_dev = device_get_ivars(child);
399 struct resource *rv, **rvp = 0;
401 isdefault = (device_get_parent(child) == dev
402 && start == 0UL && end == ~0UL && count == 1);
407 struct irq_node * irq = eisa_find_irq(e_dev, *rid);
410 start = end = irq->irq_no;
412 if (irq->irq_trigger == EISA_TRIGGER_LEVEL) {
413 flags |= RF_SHAREABLE;
415 flags &= ~RF_SHAREABLE;
422 struct resvaddr *resv;
424 resv = eisa_find_maddr(e_dev, *rid);
429 end = resv->addr + (resv->size - 1);
437 struct resvaddr *resv;
439 resv = eisa_find_ioaddr(e_dev, *rid);
444 end = resv->addr + (resv->size - 1);
454 rv = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
455 type, rid, start, end, count, flags);
463 eisa_release_resource(device_t dev, device_t child, int type, int rid,
467 struct eisa_device *e_dev = device_get_ivars(child);
468 struct resvaddr *resv = 0;
472 if (eisa_find_irq(e_dev, rid) == NULL)
477 if (device_get_parent(child) == dev)
478 resv = eisa_find_maddr(e_dev, rid);
483 if (device_get_parent(child) == dev)
484 resv = eisa_find_ioaddr(e_dev, rid);
491 rv = BUS_RELEASE_RESOURCE(device_get_parent(dev), child, type, rid, r);
502 eisa_add_intr(device_t dev, int irq, int trigger)
504 struct eisa_device *e_dev = device_get_ivars(dev);
505 struct irq_node *irq_info;
507 irq_info = malloc(sizeof(*irq_info), M_DEVBUF, M_INTWAIT);
508 irq_info->irq_no = irq;
509 irq_info->irq_trigger = trigger;
510 irq_info->idesc = NULL;
511 TAILQ_INSERT_TAIL(&e_dev->ioconf.irqs, irq_info, links);
516 eisa_add_resvaddr(struct eisa_device *e_dev, struct resvlist *head, u_long base,
517 u_long size, int flags)
519 resvaddr_t *reservation;
521 reservation = malloc(sizeof(resvaddr_t), M_DEVBUF, M_INTWAIT);
522 reservation->addr = base;
523 reservation->size = size;
524 reservation->flags = flags;
526 if (!head->lh_first) {
527 LIST_INSERT_HEAD(head, reservation, links);
531 for(node = head->lh_first; node; node = node->links.le_next) {
532 if (node->addr > reservation->addr) {
534 * List is sorted in increasing
537 LIST_INSERT_BEFORE(node, reservation, links);
541 if (node->addr == reservation->addr) {
543 * If the entry we want to add
544 * matches any already in here,
547 free(reservation, M_DEVBUF);
551 if (!node->links.le_next) {
552 LIST_INSERT_AFTER(node, reservation, links);
561 eisa_add_mspace(device_t dev, u_long mbase, u_long msize, int flags)
563 struct eisa_device *e_dev = device_get_ivars(dev);
565 return eisa_add_resvaddr(e_dev, &(e_dev->ioconf.maddrs), mbase, msize,
570 eisa_add_iospace(device_t dev, u_long iobase, u_long iosize, int flags)
572 struct eisa_device *e_dev = device_get_ivars(dev);
574 return eisa_add_resvaddr(e_dev, &(e_dev->ioconf.ioaddrs), iobase,
578 static device_method_t eisa_methods[] = {
579 /* Device interface */
580 DEVMETHOD(device_probe, eisa_probe),
581 DEVMETHOD(device_attach, bus_generic_attach),
582 DEVMETHOD(device_shutdown, bus_generic_shutdown),
583 DEVMETHOD(device_suspend, bus_generic_suspend),
584 DEVMETHOD(device_resume, bus_generic_resume),
587 DEVMETHOD(bus_print_child, eisa_print_child),
588 DEVMETHOD(bus_probe_nomatch, eisa_probe_nomatch),
589 DEVMETHOD(bus_read_ivar, eisa_read_ivar),
590 DEVMETHOD(bus_write_ivar, eisa_write_ivar),
591 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
592 DEVMETHOD(bus_alloc_resource, eisa_alloc_resource),
593 DEVMETHOD(bus_release_resource, eisa_release_resource),
594 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
595 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
596 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
597 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
602 static driver_t eisa_driver = {
608 DRIVER_MODULE(eisa, isab, eisa_driver, eisa_devclass, 0, 0);
609 DRIVER_MODULE(eisa, nexus, eisa_driver, eisa_devclass, 0, 0);