2 * Copyright (c) 1999 Matthew N. Dodd <winter@jurai.net>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * The CMU Mach3 microkernel
32 * NetBSD MCA patches by Scott Telford
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/queue.h>
39 #include <sys/malloc.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
44 #include <machine/limits.h>
45 #include <machine/bus.h>
46 #include <machine/resource.h>
49 #include <dev/mca/mca_busreg.h>
50 #include <dev/mca/mca_busvar.h>
52 #include <sys/interrupt.h>
56 static void mca_reg_print (device_t, char *, char *, int *);
59 u_long addr; /* start address */
60 u_long size; /* size of reserved area */
62 struct resource *res; /* resource manager handle */
63 LIST_ENTRY(resvaddr) links; /* List links */
66 LIST_HEAD(resvlist, resvaddr);
71 TAILQ_ENTRY(res_node) links;
74 TAILQ_HEAD(nodelist, res_node);
80 u_int8_t pos[8]; /* Programable Option Select Regs. */
82 struct nodelist irqs; /* list of reserved IRQs */
83 struct nodelist drqs; /* list of reserved DRQs */
84 struct resvlist ioaddrs; /* list of reserved I/O ranges */
85 struct resvlist maddrs; /* list of reserved memory ranges */
88 static struct res_node *
89 mca_find_irq (struct mca_device * m_dev, int rid)
92 struct res_node * irq;
94 for (i = 0, irq = TAILQ_FIRST(&m_dev->irqs);
96 i++, irq = TAILQ_NEXT(irq, links))
105 static struct res_node *
106 mca_find_drq (struct mca_device * m_dev, int rid)
109 struct res_node * drq;
111 for (i = 0, drq = TAILQ_FIRST(&m_dev->drqs);
113 i++, drq = TAILQ_NEXT(drq, links))
122 static struct resvaddr *
123 mca_find_ioaddr (struct mca_device *m_dev, int rid)
126 struct resvaddr * resv;
128 for (i = 0, resv = LIST_FIRST(&m_dev->ioaddrs);
130 i++, resv = LIST_NEXT(resv, links))
136 static struct resvaddr *
137 mca_find_maddr (struct mca_device * m_dev, int rid)
140 struct resvaddr * resv;
142 for (i = 0, resv = LIST_FIRST(&m_dev->maddrs);
144 i++, resv = LIST_NEXT(resv, links))
150 /* Not supposed to use this function! */
152 mca_pos_set (dev, reg, data)
157 struct mca_device * m_dev = device_get_ivars(dev);
158 u_int8_t slot = mca_get_slot(dev);
160 if ((slot > MCA_MAX_ADAPTERS) || (reg > MCA_POS7))
163 /* Disable motherboard setup */
164 outb(MCA_MB_SETUP_REG, MCA_MB_SETUP_DIS);
166 /* Select adapter setup regs */
167 outb(MCA_ADAP_SETUP_REG, ((slot & 0x0f) | MCA_ADAP_SET));
169 /* Write the register */
170 outb(MCA_POS_REG(reg), data);
172 /* Disable adapter setup */
173 outb(MCA_ADAP_SETUP_REG, MCA_ADAP_SETUP_DIS);
175 /* Update the IVAR copy */
176 m_dev->pos[reg] = data;
182 mca_pos_get (dev, reg)
186 u_int8_t slot = mca_get_slot(dev);
189 if ((slot > MCA_MAX_ADAPTERS) || (reg > MCA_POS7))
192 /* Disable motherboard setup */
193 outb(MCA_MB_SETUP_REG, MCA_MB_SETUP_DIS);
196 case MCA_MB_SCSI_SLOT:
198 /* Disable adapter setup */
199 outb(MCA_ADAP_SETUP_REG, MCA_ADAP_SETUP_DIS);
201 /* Select motherboard video setup regs */
202 outb(MCA_MB_SETUP_REG, MCA_MB_SETUP_SCSI);
204 /* read the register */
205 data = inb(MCA_POS_REG(reg));
207 /* Disable motherboard setup */
208 outb(MCA_MB_SETUP_REG, MCA_MB_SETUP_DIS);
211 case MCA_MB_VIDEO_SLOT:
212 /* Disable adapter setup */
213 outb(MCA_ADAP_SETUP_REG, MCA_ADAP_SETUP_DIS);
215 /* Select motherboard scsi setup regs */
216 outb(MCA_MB_SETUP_REG, MCA_MB_SETUP_VIDEO);
218 /* read the register */
219 data = inb(MCA_POS_REG(reg));
221 /* Disable motherboard setup */
222 outb(MCA_MB_SETUP_REG, MCA_MB_SETUP_DIS);
226 /* Select adapter setup regs */
227 outb(MCA_ADAP_SETUP_REG,
228 ((slot & 0x0f) | MCA_ADAP_SET));
230 /* read the register */
231 data = inb(MCA_POS_REG(reg));
233 /* Disable adapter setup */
234 outb(MCA_ADAP_SETUP_REG, MCA_ADAP_SETUP_DIS);
242 mca_match_id (id, mca_devs)
244 struct mca_ident * mca_devs;
246 struct mca_ident * m = mca_devs;
247 while(m->name != NULL) {
256 mca_pos_read (dev, reg)
260 struct mca_device * m_dev = device_get_ivars(dev);
265 return (m_dev->pos[reg]);
269 mca_add_irq (dev, irq)
273 struct mca_device * m_dev = device_get_ivars(dev);
274 struct res_node * irq_info;
276 irq_info = (struct res_node *)malloc(sizeof(*irq_info),
278 if (irq_info == NULL)
282 irq_info->desc = NULL;
283 TAILQ_INSERT_TAIL(&m_dev->irqs, irq_info, links);
289 mca_add_drq (dev, drq)
293 struct mca_device * m_dev = device_get_ivars(dev);
294 struct res_node * drq_info;
296 drq_info = (struct res_node *)malloc(sizeof(*drq_info),
298 if (drq_info == NULL)
302 drq_info->desc = NULL;
303 TAILQ_INSERT_TAIL(&m_dev->drqs, drq_info, links);
309 mca_add_resvaddr (struct mca_device * m_dev, struct resvlist * head,
310 u_long base, u_long size, int flags)
312 struct resvaddr * reservation;
314 reservation = (struct resvaddr *)malloc(sizeof(struct resvaddr),
319 reservation->addr = base;
320 reservation->size = size;
321 reservation->flags = flags;
323 if (!head->lh_first) {
324 LIST_INSERT_HEAD(head, reservation, links);
326 struct resvaddr * node;
327 for(node = head->lh_first; node; node = node->links.le_next) {
328 if (node->addr > reservation->addr) {
330 * List is sorted in increasing
333 LIST_INSERT_BEFORE(node, reservation, links);
337 if (node->addr == reservation->addr) {
339 * If the entry we want to add
340 * matches any already in here,
343 free(reservation, M_DEVBUF);
347 if (!node->links.le_next) {
348 LIST_INSERT_AFTER(node, reservation, links);
357 mca_add_mspace (dev, mbase, msize, flags)
363 struct mca_device *m_dev = device_get_ivars(dev);
365 return (mca_add_resvaddr(m_dev, &(m_dev->maddrs),
366 mbase, msize, flags));
370 mca_add_iospace (dev, iobase, iosize, flags)
376 struct mca_device * m_dev = device_get_ivars(dev);
378 return (mca_add_resvaddr(m_dev, &(m_dev->ioaddrs),
379 iobase, iosize, flags));
383 mca_probe (device_t dev)
385 struct mca_device * m_dev = NULL;
386 int devices_found = 0;
390 device_set_desc(dev, "MCA bus");
392 /* Disable adapter setup */
393 outb(MCA_ADAP_SETUP_REG, MCA_ADAP_SETUP_DIS);
394 /* Disable motherboard setup */
395 outb(MCA_MB_SETUP_REG, MCA_MB_SETUP_DIS);
398 printf("POS REG 00 01 02 03 04 05 06 07\n");
399 printf("-----------------------------------\n");
402 for (slot = 0; slot < MCA_MAX_SLOTS; slot++) {
405 m_dev = (struct mca_device *)malloc(sizeof(*m_dev),
408 device_printf(dev, "cannot malloc mca_device");
412 bzero(m_dev, sizeof(*m_dev));
414 /* Select adapter setup regs */
415 outb(MCA_ADAP_SETUP_REG, ((slot & 0x0f) | MCA_ADAP_SET));
417 /* Read the POS registers */
418 for (reg = MCA_POS0; reg <= MCA_POS7; reg++) {
419 m_dev->pos[reg] = inb(MCA_POS_REG(reg));
422 /* Disable adapter setup */
423 outb(MCA_ADAP_SETUP_REG, MCA_ADAP_SETUP_DIS);
426 printf("mca slot %d:", slot + 1);
427 for (reg = MCA_POS0; reg <= MCA_POS7; reg++) {
428 printf(" %02x", m_dev->pos[reg]);
433 m_dev->id = (u_int16_t)m_dev->pos[MCA_POS0] |
434 ((u_int16_t)m_dev->pos[MCA_POS1] << 8);
436 if (m_dev->id == 0xffff) {
442 m_dev->enabled = (m_dev->pos[MCA_POS2] & MCA_POS2_ENABLE);
445 /* Initialize our lists of reserved addresses */
446 LIST_INIT(&(m_dev->ioaddrs));
447 LIST_INIT(&(m_dev->maddrs));
448 TAILQ_INIT(&(m_dev->irqs));
449 TAILQ_INIT(&(m_dev->drqs));
451 device_add_child(dev, NULL, -1, m_dev);
456 free(m_dev, M_DEVBUF);
459 return (devices_found ? 0 : ENXIO);
463 mca_reg_print (dev, string, separator, column)
469 int length = strlen(string);
471 length += (separator ? 2 : 1);
473 if (((*column) + length) >= MAX_COL) {
476 } else if ((*column) != 0) {
478 printf("%c", *separator);
485 if ((*column) == 0) {
486 (*column) += device_printf(dev, "%s", string);
488 (*column) += printf("%s", string);
495 mca_print_child (device_t dev, device_t child)
498 struct mca_device * m_dev = device_get_ivars(child);
500 struct res_node * node;
501 struct resvaddr * resv;
502 char separator = ',';
506 if (device_get_desc(child)) {
507 snprintf(buf, sizeof(buf), "<%s>", device_get_desc(child));
508 mca_reg_print(child, buf, NULL, &column);
512 while ((resv = mca_find_ioaddr(m_dev, rid++))) {
513 if (resv->size == 1) {
514 snprintf(buf, sizeof(buf), "%s%lx",
515 ((rid == 1) ? "io 0x" : "0x"),
518 snprintf(buf, sizeof(buf), "%s%lx-0x%lx",
519 ((rid == 1) ? "io 0x" : "0x"),
521 (resv->addr + resv->size));
523 mca_reg_print(child, buf,
524 ((rid == 2) ? &separator : NULL), &column);
528 while ((resv = mca_find_maddr(m_dev, rid++))) {
529 if (resv->size == 1) {
530 snprintf(buf, sizeof(buf), "%s%lx",
531 ((rid == 1) ? "mem 0x" : "0x"),
534 snprintf(buf, sizeof(buf), "%s%lx-0x%lx",
535 ((rid == 1) ? "mem 0x" : "0x"),
537 (resv->addr + resv->size));
539 mca_reg_print(child, buf,
540 ((rid == 2) ? &separator : NULL), &column);
544 while ((node = mca_find_irq(m_dev, rid++)) != NULL) {
545 snprintf(buf, sizeof(buf), "irq %d", node->num);
546 mca_reg_print(child, buf,
547 ((rid == 1) ? &separator : NULL), &column);
551 while ((node = mca_find_drq(m_dev, rid++)) != NULL) {
552 snprintf(buf, sizeof(buf), "drq %x", node->num);
553 mca_reg_print(child, buf,
554 ((rid == 1) ? &separator : NULL), &column);
557 snprintf(buf, sizeof(buf), "on %s id %04x slot %d\n",
558 device_get_nameunit(dev),
559 mca_get_id(child), mca_get_slot(child)+1);
560 mca_reg_print(child, buf, NULL, &column);
566 mca_probe_nomatch (device_t dev, device_t child)
568 mca_id_t mca_id = mca_get_id(child);
569 u_int8_t slot = mca_get_slot(child);
570 u_int8_t enabled = mca_get_enabled(child);
572 device_printf(dev, "unknown card (id 0x%04x, %s) at slot %d\n",
574 (enabled ? "enabled" : "disabled"),
581 mca_read_ivar (device_t dev, device_t child, int which, u_long * result)
583 struct mca_device * m_dev = device_get_ivars(child);
584 struct res_node * node;
588 *result = m_dev->slot;
593 case MCA_IVAR_ENABLED:
594 *result = m_dev->enabled;
597 if ((node = mca_find_irq(m_dev, 0)))
603 if ((node = mca_find_drq(m_dev, 0)))
617 mca_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
622 static struct resource *
623 mca_alloc_resource (device_t dev, device_t child, int type, int *rid,
624 u_long start, u_long end, u_long count, u_int flags)
626 struct mca_device * m_dev = device_get_ivars(child);
627 struct resource * rv;
628 struct resource ** rvp = NULL;
629 struct resvaddr * resv;
630 struct res_node * node;
633 isdefault = ((device_get_parent(child) == dev)
640 node = mca_find_irq(m_dev, *rid);
644 start = end = node->num;
646 flags |= RF_SHAREABLE;
651 node = mca_find_drq(m_dev, *rid);
655 start = end = node->num;
661 resv = mca_find_maddr(m_dev, *rid);
666 end = resv->addr + resv->size;
673 resv = mca_find_ioaddr(m_dev, *rid);
678 end = resv->addr + resv->size;
688 rv = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
689 type, rid, start, end, count, flags);
698 mca_release_resource (device_t dev, device_t child, int type, int rid,
701 struct mca_device * m_dev = device_get_ivars(child);
702 struct resvaddr * resv = NULL;
706 isdefault = (device_get_parent(child) == dev);
710 if (mca_find_irq(m_dev, rid) == NULL)
714 if (mca_find_drq(m_dev, rid) == NULL)
719 resv = mca_find_maddr(m_dev, rid);
724 resv = mca_find_ioaddr(m_dev, rid);
731 rv = BUS_RELEASE_RESOURCE(device_get_parent(dev), child, type, rid, r);
741 static device_method_t mca_methods[] = {
742 /* Device interface */
743 DEVMETHOD(device_probe, mca_probe),
744 DEVMETHOD(device_attach, bus_generic_attach),
745 DEVMETHOD(device_shutdown, bus_generic_shutdown),
746 DEVMETHOD(device_suspend, bus_generic_suspend),
747 DEVMETHOD(device_resume, bus_generic_resume),
750 DEVMETHOD(bus_print_child, mca_print_child),
751 DEVMETHOD(bus_probe_nomatch, mca_probe_nomatch),
752 DEVMETHOD(bus_read_ivar, mca_read_ivar),
753 DEVMETHOD(bus_write_ivar, mca_write_ivar),
754 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
755 DEVMETHOD(bus_alloc_resource, mca_alloc_resource),
756 DEVMETHOD(bus_release_resource, mca_release_resource),
757 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
758 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
759 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
760 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
765 static driver_t mca_driver = {
771 static devclass_t mca_devclass;
773 DRIVER_MODULE(mca, nexus, mca_driver, mca_devclass, 0, 0);