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.2 2003/06/17 04:28:28 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 <dev/mca/mca_busreg.h>
51 #include <dev/mca/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 = (struct mca_device *)malloc(sizeof(*m_dev),
273 device_printf(dev, "cannot malloc mca_device");
277 bzero(m_dev, sizeof(*m_dev));
279 /* Select adapter setup regs */
280 outb(MCA_ADAP_SETUP_REG, ((slot & 0x0f) | MCA_ADAP_SET));
282 /* Read the POS registers */
283 for (reg = MCA_POS0; reg <= MCA_POS7; reg++) {
284 m_dev->pos[reg] = inb(MCA_POS_REG(reg));
287 /* Disable adapter setup */
288 outb(MCA_ADAP_SETUP_REG, MCA_ADAP_SETUP_DIS);
291 printf("mca slot %d:", slot + 1);
292 for (reg = MCA_POS0; reg <= MCA_POS7; reg++) {
293 printf(" %02x", m_dev->pos[reg]);
298 m_dev->id = (u_int16_t)m_dev->pos[MCA_POS0] |
299 ((u_int16_t)m_dev->pos[MCA_POS1] << 8);
301 if (m_dev->id == 0xffff) {
307 m_dev->enabled = (m_dev->pos[MCA_POS2] & MCA_POS2_ENABLE);
310 resource_list_init(&(m_dev->rl));
312 child = device_add_child(dev, NULL, -1);
313 device_set_ivars(child, m_dev);
319 free(m_dev, M_DEVBUF);
322 return (devices_found ? 0 : ENXIO);
326 mca_reg_print (dev, string, separator, column)
332 int length = strlen(string);
334 length += (separator ? 2 : 1);
336 if (((*column) + length) >= MAX_COL) {
339 } else if ((*column) != 0) {
341 printf("%c", *separator);
348 if ((*column) == 0) {
349 (*column) += device_printf(dev, "%s", string);
351 (*column) += printf("%s", string);
358 mca_print_child (device_t dev, device_t child)
361 struct mca_device * m_dev = device_get_ivars(child);
363 struct resource_list_entry * rle;
364 char separator = ',';
368 if (device_get_desc(child)) {
369 snprintf(buf, sizeof(buf), "<%s>", device_get_desc(child));
370 mca_reg_print(child, buf, NULL, &column);
374 while ((rle = resource_list_find(&(m_dev->rl), SYS_RES_IOPORT, rid++))) {
375 if (rle->count == 1) {
376 snprintf(buf, sizeof(buf), "%s%lx",
377 ((rid == 1) ? "io 0x" : "0x"),
380 snprintf(buf, sizeof(buf), "%s%lx-0x%lx",
381 ((rid == 1) ? "io 0x" : "0x"),
383 (rle->start + rle->count));
385 mca_reg_print(child, buf,
386 ((rid == 2) ? &separator : NULL), &column);
390 while ((rle = resource_list_find(&(m_dev->rl), SYS_RES_MEMORY, rid++))) {
391 if (rle->count == 1) {
392 snprintf(buf, sizeof(buf), "%s%lx",
393 ((rid == 1) ? "mem 0x" : "0x"),
396 snprintf(buf, sizeof(buf), "%s%lx-0x%lx",
397 ((rid == 1) ? "mem 0x" : "0x"),
399 (rle->start + rle->count));
401 mca_reg_print(child, buf,
402 ((rid == 2) ? &separator : NULL), &column);
406 while ((rle = resource_list_find(&(m_dev->rl), SYS_RES_IRQ, rid++))) {
407 snprintf(buf, sizeof(buf), "irq %ld", rle->start);
408 mca_reg_print(child, buf,
409 ((rid == 1) ? &separator : NULL), &column);
413 while ((rle = resource_list_find(&(m_dev->rl), SYS_RES_DRQ, rid++))) {
414 snprintf(buf, sizeof(buf), "drq %lx", rle->start);
415 mca_reg_print(child, buf,
416 ((rid == 1) ? &separator : NULL), &column);
419 snprintf(buf, sizeof(buf), "on %s id %04x slot %d\n",
420 device_get_nameunit(dev),
421 mca_get_id(child), mca_get_slot(child)+1);
422 mca_reg_print(child, buf, NULL, &column);
428 mca_probe_nomatch (device_t dev, device_t child)
430 mca_id_t mca_id = mca_get_id(child);
431 u_int8_t slot = mca_get_slot(child);
432 u_int8_t enabled = mca_get_enabled(child);
434 device_printf(dev, "unknown card (id 0x%04x, %s) at slot %d\n",
436 (enabled ? "enabled" : "disabled"),
443 mca_read_ivar (device_t dev, device_t child, int which, u_long * result)
445 struct mca_device * m_dev = device_get_ivars(child);
449 *result = m_dev->slot;
454 case MCA_IVAR_ENABLED:
455 *result = m_dev->enabled;
466 mca_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
471 static struct resource *
472 mca_alloc_resource (device_t dev, device_t child, int type, int *rid,
473 u_long start, u_long end, u_long count, u_int flags)
475 struct mca_device * m_dev = device_get_ivars(child);
476 struct resource_list_entry * rle;
480 isdefault = (start == 0UL && end == ~0UL);
481 passthrough = (device_get_parent(child) != dev);
483 if (!passthrough && !isdefault) {
484 rle = resource_list_find(&(m_dev->rl), type, *rid);
486 resource_list_add(&(m_dev->rl), type, *rid,
491 if (type == SYS_RES_IRQ) {
492 flags |= RF_SHAREABLE;
495 return (resource_list_alloc(&(m_dev->rl), dev, child, type, rid,
496 start, end, count, flags));
500 mca_release_resource (device_t dev, device_t child, int type, int rid,
503 struct mca_device * m_dev = device_get_ivars(child);
505 return (resource_list_release(&(m_dev->rl), dev, child, type, rid, r));
509 mca_get_resource(device_t dev, device_t child, int type, int rid,
510 u_long *startp, u_long *countp)
512 struct mca_device * m_dev = device_get_ivars(child);
513 struct resource_list * rl = &(m_dev->rl);
514 struct resource_list_entry * rle;
516 rle = resource_list_find(rl, type, rid);
520 *startp = rle->start;
521 *countp = rle->count;
527 mca_set_resource(device_t dev, device_t child, int type, int rid,
528 u_long start, u_long count)
530 struct mca_device * m_dev = device_get_ivars(child);
531 struct resource_list * rl = &(m_dev->rl);
533 resource_list_add(rl, type, rid, start, start + count - 1, count);
538 mca_delete_resource(device_t dev, device_t child, int type, int rid)
540 struct mca_device * m_dev = device_get_ivars(child);
541 struct resource_list * rl = &(m_dev->rl);
543 resource_list_delete(rl, type, rid);
546 static device_method_t mca_methods[] = {
547 /* Device interface */
548 DEVMETHOD(device_probe, mca_probe),
549 DEVMETHOD(device_attach, bus_generic_attach),
550 DEVMETHOD(device_shutdown, bus_generic_shutdown),
551 DEVMETHOD(device_suspend, bus_generic_suspend),
552 DEVMETHOD(device_resume, bus_generic_resume),
555 DEVMETHOD(bus_print_child, mca_print_child),
556 DEVMETHOD(bus_probe_nomatch, mca_probe_nomatch),
557 DEVMETHOD(bus_read_ivar, mca_read_ivar),
558 DEVMETHOD(bus_write_ivar, mca_write_ivar),
559 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
560 DEVMETHOD(bus_alloc_resource, mca_alloc_resource),
561 DEVMETHOD(bus_release_resource, mca_release_resource),
562 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
563 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
564 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
565 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
567 DEVMETHOD(bus_set_resource, mca_set_resource),
568 DEVMETHOD(bus_get_resource, mca_get_resource),
569 DEVMETHOD(bus_delete_resource, mca_delete_resource),
574 static driver_t mca_driver = {
580 static devclass_t mca_devclass;
582 DRIVER_MODULE(mca, nexus, mca_driver, mca_devclass, 0, 0);