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
26 * $FreeBSD: src/sys/dev/mca/mca_bus.c,v 1.7.2.1 2000/03/17 23:55:48 peter Exp $
27 * $DragonFly: src/sys/bus/mca/Attic/mca_bus.c,v 1.5 2004/04/07 05:54:39 dillon Exp $
32 * The CMU Mach3 microkernel
33 * NetBSD MCA patches by Scott Telford
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>
50 #include "mca_busreg.h"
51 #include "mca_busvar.h"
53 #include <sys/interrupt.h>
57 static void mca_reg_print (device_t, char *, char *, int *);
60 struct resource_list rl; /* Resources */
65 u_int8_t pos[8]; /* Programable Option Select Regs. */
68 /* Not supposed to use this function! */
70 mca_pos_set (dev, reg, data)
75 struct mca_device * m_dev = device_get_ivars(dev);
76 u_int8_t slot = mca_get_slot(dev);
78 if ((slot > MCA_MAX_ADAPTERS) || (reg > MCA_POS7))
81 /* Disable motherboard setup */
82 outb(MCA_MB_SETUP_REG, MCA_MB_SETUP_DIS);
84 /* Select adapter setup regs */
85 outb(MCA_ADAP_SETUP_REG, ((slot & 0x0f) | MCA_ADAP_SET));
87 /* Write the register */
88 outb(MCA_POS_REG(reg), data);
90 /* Disable adapter setup */
91 outb(MCA_ADAP_SETUP_REG, MCA_ADAP_SETUP_DIS);
93 /* Update the IVAR copy */
94 m_dev->pos[reg] = data;
100 mca_pos_get (dev, reg)
104 u_int8_t slot = mca_get_slot(dev);
107 if ((slot > MCA_MAX_ADAPTERS) || (reg > MCA_POS7))
110 /* Disable motherboard setup */
111 outb(MCA_MB_SETUP_REG, MCA_MB_SETUP_DIS);
114 case MCA_MB_SCSI_SLOT:
116 /* Disable adapter setup */
117 outb(MCA_ADAP_SETUP_REG, MCA_ADAP_SETUP_DIS);
119 /* Select motherboard video setup regs */
120 outb(MCA_MB_SETUP_REG, MCA_MB_SETUP_SCSI);
122 /* read the register */
123 data = inb(MCA_POS_REG(reg));
125 /* Disable motherboard setup */
126 outb(MCA_MB_SETUP_REG, MCA_MB_SETUP_DIS);
129 case MCA_MB_VIDEO_SLOT:
130 /* Disable adapter setup */
131 outb(MCA_ADAP_SETUP_REG, MCA_ADAP_SETUP_DIS);
133 /* Select motherboard scsi setup regs */
134 outb(MCA_MB_SETUP_REG, MCA_MB_SETUP_VIDEO);
136 /* read the register */
137 data = inb(MCA_POS_REG(reg));
139 /* Disable motherboard setup */
140 outb(MCA_MB_SETUP_REG, MCA_MB_SETUP_DIS);
144 /* Select adapter setup regs */
145 outb(MCA_ADAP_SETUP_REG,
146 ((slot & 0x0f) | MCA_ADAP_SET));
148 /* read the register */
149 data = inb(MCA_POS_REG(reg));
151 /* Disable adapter setup */
152 outb(MCA_ADAP_SETUP_REG, MCA_ADAP_SETUP_DIS);
160 mca_match_id (id, mca_devs)
162 struct mca_ident * mca_devs;
164 struct mca_ident * m = mca_devs;
165 while(m->name != NULL) {
174 mca_pos_read (dev, reg)
178 struct mca_device * m_dev = device_get_ivars(dev);
183 return (m_dev->pos[reg]);
187 mca_add_irq (dev, irq)
191 struct mca_device * m_dev = device_get_ivars(dev);
194 while (resource_list_find(&(m_dev->rl), SYS_RES_IRQ, rid)) rid++;
195 resource_list_add(&(m_dev->rl), SYS_RES_IRQ, rid, irq, irq, 1);
201 mca_add_drq (dev, drq)
205 struct mca_device * m_dev = device_get_ivars(dev);
208 while (resource_list_find(&(m_dev->rl), SYS_RES_DRQ, rid)) rid++;
209 resource_list_add(&(m_dev->rl), SYS_RES_DRQ, rid, drq, drq, 1);
215 mca_add_mspace (dev, mbase, msize)
220 struct mca_device * m_dev = device_get_ivars(dev);
223 while (resource_list_find(&(m_dev->rl), SYS_RES_MEMORY, rid)) rid++;
224 resource_list_add(&(m_dev->rl), SYS_RES_MEMORY, rid,
225 mbase, (mbase + msize), msize);
231 mca_add_iospace (dev, iobase, iosize)
236 struct mca_device * m_dev = device_get_ivars(dev);
239 while (resource_list_find(&(m_dev->rl), SYS_RES_IOPORT, rid)) rid++;
240 resource_list_add(&(m_dev->rl), SYS_RES_IOPORT, rid,
241 iobase, (iobase + iosize), iosize);
247 mca_probe (device_t dev)
250 struct mca_device * m_dev = NULL;
251 int devices_found = 0;
255 device_set_desc(dev, "MCA bus");
257 /* Disable adapter setup */
258 outb(MCA_ADAP_SETUP_REG, MCA_ADAP_SETUP_DIS);
259 /* Disable motherboard setup */
260 outb(MCA_MB_SETUP_REG, MCA_MB_SETUP_DIS);
263 printf("POS REG 00 01 02 03 04 05 06 07\n");
264 printf("-----------------------------------\n");
267 for (slot = 0; slot < MCA_MAX_SLOTS; slot++) {
270 m_dev = malloc(sizeof(*m_dev), M_DEVBUF, M_WAITOK);
271 bzero(m_dev, sizeof(*m_dev));
273 /* Select adapter setup regs */
274 outb(MCA_ADAP_SETUP_REG, ((slot & 0x0f) | MCA_ADAP_SET));
276 /* Read the POS registers */
277 for (reg = MCA_POS0; reg <= MCA_POS7; reg++) {
278 m_dev->pos[reg] = inb(MCA_POS_REG(reg));
281 /* Disable adapter setup */
282 outb(MCA_ADAP_SETUP_REG, MCA_ADAP_SETUP_DIS);
285 printf("mca slot %d:", slot + 1);
286 for (reg = MCA_POS0; reg <= MCA_POS7; reg++) {
287 printf(" %02x", m_dev->pos[reg]);
292 m_dev->id = (u_int16_t)m_dev->pos[MCA_POS0] |
293 ((u_int16_t)m_dev->pos[MCA_POS1] << 8);
295 if (m_dev->id == 0xffff) {
301 m_dev->enabled = (m_dev->pos[MCA_POS2] & MCA_POS2_ENABLE);
304 resource_list_init(&(m_dev->rl));
306 child = device_add_child(dev, NULL, -1);
307 device_set_ivars(child, m_dev);
313 free(m_dev, M_DEVBUF);
316 return (devices_found ? 0 : ENXIO);
320 mca_reg_print (dev, string, separator, column)
326 int length = strlen(string);
328 length += (separator ? 2 : 1);
330 if (((*column) + length) >= MAX_COL) {
333 } else if ((*column) != 0) {
335 printf("%c", *separator);
342 if ((*column) == 0) {
343 (*column) += device_printf(dev, "%s", string);
345 (*column) += printf("%s", string);
352 mca_print_child (device_t dev, device_t child)
355 struct mca_device * m_dev = device_get_ivars(child);
357 struct resource_list_entry * rle;
358 char separator = ',';
362 if (device_get_desc(child)) {
363 snprintf(buf, sizeof(buf), "<%s>", device_get_desc(child));
364 mca_reg_print(child, buf, NULL, &column);
368 while ((rle = resource_list_find(&(m_dev->rl), SYS_RES_IOPORT, rid++))) {
369 if (rle->count == 1) {
370 snprintf(buf, sizeof(buf), "%s%lx",
371 ((rid == 1) ? "io 0x" : "0x"),
374 snprintf(buf, sizeof(buf), "%s%lx-0x%lx",
375 ((rid == 1) ? "io 0x" : "0x"),
377 (rle->start + rle->count));
379 mca_reg_print(child, buf,
380 ((rid == 2) ? &separator : NULL), &column);
384 while ((rle = resource_list_find(&(m_dev->rl), SYS_RES_MEMORY, rid++))) {
385 if (rle->count == 1) {
386 snprintf(buf, sizeof(buf), "%s%lx",
387 ((rid == 1) ? "mem 0x" : "0x"),
390 snprintf(buf, sizeof(buf), "%s%lx-0x%lx",
391 ((rid == 1) ? "mem 0x" : "0x"),
393 (rle->start + rle->count));
395 mca_reg_print(child, buf,
396 ((rid == 2) ? &separator : NULL), &column);
400 while ((rle = resource_list_find(&(m_dev->rl), SYS_RES_IRQ, rid++))) {
401 snprintf(buf, sizeof(buf), "irq %ld", rle->start);
402 mca_reg_print(child, buf,
403 ((rid == 1) ? &separator : NULL), &column);
407 while ((rle = resource_list_find(&(m_dev->rl), SYS_RES_DRQ, rid++))) {
408 snprintf(buf, sizeof(buf), "drq %lx", rle->start);
409 mca_reg_print(child, buf,
410 ((rid == 1) ? &separator : NULL), &column);
413 snprintf(buf, sizeof(buf), "on %s id %04x slot %d\n",
414 device_get_nameunit(dev),
415 mca_get_id(child), mca_get_slot(child)+1);
416 mca_reg_print(child, buf, NULL, &column);
422 mca_probe_nomatch (device_t dev, device_t child)
424 mca_id_t mca_id = mca_get_id(child);
425 u_int8_t slot = mca_get_slot(child);
426 u_int8_t enabled = mca_get_enabled(child);
428 device_printf(dev, "unknown card (id 0x%04x, %s) at slot %d\n",
430 (enabled ? "enabled" : "disabled"),
437 mca_read_ivar (device_t dev, device_t child, int which, u_long * result)
439 struct mca_device * m_dev = device_get_ivars(child);
443 *result = m_dev->slot;
448 case MCA_IVAR_ENABLED:
449 *result = m_dev->enabled;
460 mca_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
465 static struct resource *
466 mca_alloc_resource (device_t dev, device_t child, int type, int *rid,
467 u_long start, u_long end, u_long count, u_int flags)
469 struct mca_device * m_dev = device_get_ivars(child);
470 struct resource_list_entry * rle;
474 isdefault = (start == 0UL && end == ~0UL);
475 passthrough = (device_get_parent(child) != dev);
477 if (!passthrough && !isdefault) {
478 rle = resource_list_find(&(m_dev->rl), type, *rid);
480 resource_list_add(&(m_dev->rl), type, *rid,
485 if (type == SYS_RES_IRQ) {
486 flags |= RF_SHAREABLE;
489 return (resource_list_alloc(&(m_dev->rl), dev, child, type, rid,
490 start, end, count, flags));
494 mca_release_resource (device_t dev, device_t child, int type, int rid,
497 struct mca_device * m_dev = device_get_ivars(child);
499 return (resource_list_release(&(m_dev->rl), dev, child, type, rid, r));
503 mca_get_resource(device_t dev, device_t child, int type, int rid,
504 u_long *startp, u_long *countp)
506 struct mca_device * m_dev = device_get_ivars(child);
507 struct resource_list * rl = &(m_dev->rl);
508 struct resource_list_entry * rle;
510 rle = resource_list_find(rl, type, rid);
514 *startp = rle->start;
515 *countp = rle->count;
521 mca_set_resource(device_t dev, device_t child, int type, int rid,
522 u_long start, u_long count)
524 struct mca_device * m_dev = device_get_ivars(child);
525 struct resource_list * rl = &(m_dev->rl);
527 resource_list_add(rl, type, rid, start, start + count - 1, count);
532 mca_delete_resource(device_t dev, device_t child, int type, int rid)
534 struct mca_device * m_dev = device_get_ivars(child);
535 struct resource_list * rl = &(m_dev->rl);
537 resource_list_delete(rl, type, rid);
540 static struct resource_list *
541 mca_get_resource_list (device_t dev, device_t child)
543 struct mca_device * m_dev = device_get_ivars(child);
544 struct resource_list * rl = &m_dev->rl;
553 static device_method_t mca_methods[] = {
554 /* Device interface */
555 DEVMETHOD(device_probe, mca_probe),
556 DEVMETHOD(device_attach, bus_generic_attach),
557 DEVMETHOD(device_shutdown, bus_generic_shutdown),
558 DEVMETHOD(device_suspend, bus_generic_suspend),
559 DEVMETHOD(device_resume, bus_generic_resume),
562 DEVMETHOD(bus_print_child, mca_print_child),
563 DEVMETHOD(bus_probe_nomatch, mca_probe_nomatch),
564 DEVMETHOD(bus_read_ivar, mca_read_ivar),
565 DEVMETHOD(bus_write_ivar, mca_write_ivar),
566 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
567 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
568 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
570 DEVMETHOD(bus_get_resource_list,mca_get_resource_list),
571 DEVMETHOD(bus_alloc_resource, mca_alloc_resource),
572 DEVMETHOD(bus_release_resource, mca_release_resource),
573 DEVMETHOD(bus_set_resource, mca_set_resource),
574 DEVMETHOD(bus_get_resource, mca_get_resource),
575 DEVMETHOD(bus_delete_resource, mca_delete_resource),
576 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
577 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
582 static driver_t mca_driver = {
588 static devclass_t mca_devclass;
590 DRIVER_MODULE(mca, nexus, mca_driver, mca_devclass, 0, 0);