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 $
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 struct resource_list rl; /* Resources */
64 u_int8_t pos[8]; /* Programable Option Select Regs. */
67 /* Not supposed to use this function! */
69 mca_pos_set (dev, reg, data)
74 struct mca_device * m_dev = device_get_ivars(dev);
75 u_int8_t slot = mca_get_slot(dev);
77 if ((slot > MCA_MAX_ADAPTERS) || (reg > MCA_POS7))
80 /* Disable motherboard setup */
81 outb(MCA_MB_SETUP_REG, MCA_MB_SETUP_DIS);
83 /* Select adapter setup regs */
84 outb(MCA_ADAP_SETUP_REG, ((slot & 0x0f) | MCA_ADAP_SET));
86 /* Write the register */
87 outb(MCA_POS_REG(reg), data);
89 /* Disable adapter setup */
90 outb(MCA_ADAP_SETUP_REG, MCA_ADAP_SETUP_DIS);
92 /* Update the IVAR copy */
93 m_dev->pos[reg] = data;
99 mca_pos_get (dev, reg)
103 u_int8_t slot = mca_get_slot(dev);
106 if ((slot > MCA_MAX_ADAPTERS) || (reg > MCA_POS7))
109 /* Disable motherboard setup */
110 outb(MCA_MB_SETUP_REG, MCA_MB_SETUP_DIS);
113 case MCA_MB_SCSI_SLOT:
115 /* Disable adapter setup */
116 outb(MCA_ADAP_SETUP_REG, MCA_ADAP_SETUP_DIS);
118 /* Select motherboard video setup regs */
119 outb(MCA_MB_SETUP_REG, MCA_MB_SETUP_SCSI);
121 /* read the register */
122 data = inb(MCA_POS_REG(reg));
124 /* Disable motherboard setup */
125 outb(MCA_MB_SETUP_REG, MCA_MB_SETUP_DIS);
128 case MCA_MB_VIDEO_SLOT:
129 /* Disable adapter setup */
130 outb(MCA_ADAP_SETUP_REG, MCA_ADAP_SETUP_DIS);
132 /* Select motherboard scsi setup regs */
133 outb(MCA_MB_SETUP_REG, MCA_MB_SETUP_VIDEO);
135 /* read the register */
136 data = inb(MCA_POS_REG(reg));
138 /* Disable motherboard setup */
139 outb(MCA_MB_SETUP_REG, MCA_MB_SETUP_DIS);
143 /* Select adapter setup regs */
144 outb(MCA_ADAP_SETUP_REG,
145 ((slot & 0x0f) | MCA_ADAP_SET));
147 /* read the register */
148 data = inb(MCA_POS_REG(reg));
150 /* Disable adapter setup */
151 outb(MCA_ADAP_SETUP_REG, MCA_ADAP_SETUP_DIS);
159 mca_match_id (id, mca_devs)
161 struct mca_ident * mca_devs;
163 struct mca_ident * m = mca_devs;
164 while(m->name != NULL) {
173 mca_pos_read (dev, reg)
177 struct mca_device * m_dev = device_get_ivars(dev);
182 return (m_dev->pos[reg]);
186 mca_add_irq (dev, irq)
190 struct mca_device * m_dev = device_get_ivars(dev);
193 while (resource_list_find(&(m_dev->rl), SYS_RES_IRQ, rid)) rid++;
194 resource_list_add(&(m_dev->rl), SYS_RES_IRQ, rid, irq, irq, 1);
200 mca_add_drq (dev, drq)
204 struct mca_device * m_dev = device_get_ivars(dev);
207 while (resource_list_find(&(m_dev->rl), SYS_RES_DRQ, rid)) rid++;
208 resource_list_add(&(m_dev->rl), SYS_RES_DRQ, rid, drq, drq, 1);
214 mca_add_mspace (dev, mbase, msize)
219 struct mca_device * m_dev = device_get_ivars(dev);
222 while (resource_list_find(&(m_dev->rl), SYS_RES_MEMORY, rid)) rid++;
223 resource_list_add(&(m_dev->rl), SYS_RES_MEMORY, rid,
224 mbase, (mbase + msize), msize);
230 mca_add_iospace (dev, iobase, iosize)
235 struct mca_device * m_dev = device_get_ivars(dev);
238 while (resource_list_find(&(m_dev->rl), SYS_RES_IOPORT, rid)) rid++;
239 resource_list_add(&(m_dev->rl), SYS_RES_IOPORT, rid,
240 iobase, (iobase + iosize), iosize);
246 mca_probe (device_t dev)
249 struct mca_device * m_dev = NULL;
250 int devices_found = 0;
254 device_set_desc(dev, "MCA bus");
256 /* Disable adapter setup */
257 outb(MCA_ADAP_SETUP_REG, MCA_ADAP_SETUP_DIS);
258 /* Disable motherboard setup */
259 outb(MCA_MB_SETUP_REG, MCA_MB_SETUP_DIS);
262 printf("POS REG 00 01 02 03 04 05 06 07\n");
263 printf("-----------------------------------\n");
266 for (slot = 0; slot < MCA_MAX_SLOTS; slot++) {
269 m_dev = (struct mca_device *)malloc(sizeof(*m_dev),
272 device_printf(dev, "cannot malloc mca_device");
276 bzero(m_dev, sizeof(*m_dev));
278 /* Select adapter setup regs */
279 outb(MCA_ADAP_SETUP_REG, ((slot & 0x0f) | MCA_ADAP_SET));
281 /* Read the POS registers */
282 for (reg = MCA_POS0; reg <= MCA_POS7; reg++) {
283 m_dev->pos[reg] = inb(MCA_POS_REG(reg));
286 /* Disable adapter setup */
287 outb(MCA_ADAP_SETUP_REG, MCA_ADAP_SETUP_DIS);
290 printf("mca slot %d:", slot + 1);
291 for (reg = MCA_POS0; reg <= MCA_POS7; reg++) {
292 printf(" %02x", m_dev->pos[reg]);
297 m_dev->id = (u_int16_t)m_dev->pos[MCA_POS0] |
298 ((u_int16_t)m_dev->pos[MCA_POS1] << 8);
300 if (m_dev->id == 0xffff) {
306 m_dev->enabled = (m_dev->pos[MCA_POS2] & MCA_POS2_ENABLE);
309 resource_list_init(&(m_dev->rl));
311 child = device_add_child(dev, NULL, -1);
312 device_set_ivars(child, m_dev);
318 free(m_dev, M_DEVBUF);
321 return (devices_found ? 0 : ENXIO);
325 mca_reg_print (dev, string, separator, column)
331 int length = strlen(string);
333 length += (separator ? 2 : 1);
335 if (((*column) + length) >= MAX_COL) {
338 } else if ((*column) != 0) {
340 printf("%c", *separator);
347 if ((*column) == 0) {
348 (*column) += device_printf(dev, "%s", string);
350 (*column) += printf("%s", string);
357 mca_print_child (device_t dev, device_t child)
360 struct mca_device * m_dev = device_get_ivars(child);
362 struct resource_list_entry * rle;
363 char separator = ',';
367 if (device_get_desc(child)) {
368 snprintf(buf, sizeof(buf), "<%s>", device_get_desc(child));
369 mca_reg_print(child, buf, NULL, &column);
373 while ((rle = resource_list_find(&(m_dev->rl), SYS_RES_IOPORT, rid++))) {
374 if (rle->count == 1) {
375 snprintf(buf, sizeof(buf), "%s%lx",
376 ((rid == 1) ? "io 0x" : "0x"),
379 snprintf(buf, sizeof(buf), "%s%lx-0x%lx",
380 ((rid == 1) ? "io 0x" : "0x"),
382 (rle->start + rle->count));
384 mca_reg_print(child, buf,
385 ((rid == 2) ? &separator : NULL), &column);
389 while ((rle = resource_list_find(&(m_dev->rl), SYS_RES_MEMORY, rid++))) {
390 if (rle->count == 1) {
391 snprintf(buf, sizeof(buf), "%s%lx",
392 ((rid == 1) ? "mem 0x" : "0x"),
395 snprintf(buf, sizeof(buf), "%s%lx-0x%lx",
396 ((rid == 1) ? "mem 0x" : "0x"),
398 (rle->start + rle->count));
400 mca_reg_print(child, buf,
401 ((rid == 2) ? &separator : NULL), &column);
405 while ((rle = resource_list_find(&(m_dev->rl), SYS_RES_IRQ, rid++))) {
406 snprintf(buf, sizeof(buf), "irq %ld", rle->start);
407 mca_reg_print(child, buf,
408 ((rid == 1) ? &separator : NULL), &column);
412 while ((rle = resource_list_find(&(m_dev->rl), SYS_RES_DRQ, rid++))) {
413 snprintf(buf, sizeof(buf), "drq %lx", rle->start);
414 mca_reg_print(child, buf,
415 ((rid == 1) ? &separator : NULL), &column);
418 snprintf(buf, sizeof(buf), "on %s id %04x slot %d\n",
419 device_get_nameunit(dev),
420 mca_get_id(child), mca_get_slot(child)+1);
421 mca_reg_print(child, buf, NULL, &column);
427 mca_probe_nomatch (device_t dev, device_t child)
429 mca_id_t mca_id = mca_get_id(child);
430 u_int8_t slot = mca_get_slot(child);
431 u_int8_t enabled = mca_get_enabled(child);
433 device_printf(dev, "unknown card (id 0x%04x, %s) at slot %d\n",
435 (enabled ? "enabled" : "disabled"),
442 mca_read_ivar (device_t dev, device_t child, int which, u_long * result)
444 struct mca_device * m_dev = device_get_ivars(child);
448 *result = m_dev->slot;
453 case MCA_IVAR_ENABLED:
454 *result = m_dev->enabled;
465 mca_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
470 static struct resource *
471 mca_alloc_resource (device_t dev, device_t child, int type, int *rid,
472 u_long start, u_long end, u_long count, u_int flags)
474 struct mca_device * m_dev = device_get_ivars(child);
475 struct resource_list_entry * rle;
479 isdefault = (start == 0UL && end == ~0UL);
480 passthrough = (device_get_parent(child) != dev);
482 if (!passthrough && !isdefault) {
483 rle = resource_list_find(&(m_dev->rl), type, *rid);
485 resource_list_add(&(m_dev->rl), type, *rid,
490 if (type == SYS_RES_IRQ) {
491 flags |= RF_SHAREABLE;
494 return (resource_list_alloc(&(m_dev->rl), dev, child, type, rid,
495 start, end, count, flags));
499 mca_release_resource (device_t dev, device_t child, int type, int rid,
502 struct mca_device * m_dev = device_get_ivars(child);
504 return (resource_list_release(&(m_dev->rl), dev, child, type, rid, r));
508 mca_get_resource(device_t dev, device_t child, int type, int rid,
509 u_long *startp, u_long *countp)
511 struct mca_device * m_dev = device_get_ivars(child);
512 struct resource_list * rl = &(m_dev->rl);
513 struct resource_list_entry * rle;
515 rle = resource_list_find(rl, type, rid);
519 *startp = rle->start;
520 *countp = rle->count;
526 mca_set_resource(device_t dev, device_t child, int type, int rid,
527 u_long start, u_long count)
529 struct mca_device * m_dev = device_get_ivars(child);
530 struct resource_list * rl = &(m_dev->rl);
532 resource_list_add(rl, type, rid, start, start + count - 1, count);
537 mca_delete_resource(device_t dev, device_t child, int type, int rid)
539 struct mca_device * m_dev = device_get_ivars(child);
540 struct resource_list * rl = &(m_dev->rl);
542 resource_list_delete(rl, type, rid);
545 static device_method_t mca_methods[] = {
546 /* Device interface */
547 DEVMETHOD(device_probe, mca_probe),
548 DEVMETHOD(device_attach, bus_generic_attach),
549 DEVMETHOD(device_shutdown, bus_generic_shutdown),
550 DEVMETHOD(device_suspend, bus_generic_suspend),
551 DEVMETHOD(device_resume, bus_generic_resume),
554 DEVMETHOD(bus_print_child, mca_print_child),
555 DEVMETHOD(bus_probe_nomatch, mca_probe_nomatch),
556 DEVMETHOD(bus_read_ivar, mca_read_ivar),
557 DEVMETHOD(bus_write_ivar, mca_write_ivar),
558 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
559 DEVMETHOD(bus_alloc_resource, mca_alloc_resource),
560 DEVMETHOD(bus_release_resource, mca_release_resource),
561 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
562 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
563 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
564 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
566 DEVMETHOD(bus_set_resource, mca_set_resource),
567 DEVMETHOD(bus_get_resource, mca_get_resource),
568 DEVMETHOD(bus_delete_resource, mca_delete_resource),
573 static driver_t mca_driver = {
579 static devclass_t mca_devclass;
581 DRIVER_MODULE(mca, nexus, mca_driver, mca_devclass, 0, 0);