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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
33 * The CMU Mach3 microkernel
34 * NetBSD MCA patches by Scott Telford
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/queue.h>
41 #include <sys/malloc.h>
42 #include <sys/kernel.h>
43 #include <sys/limits.h>
44 #include <sys/module.h>
47 #include <machine/bus.h>
48 #include <machine/resource.h>
51 #include <dev/mca/mca_busreg.h>
52 #include <dev/mca/mca_busvar.h>
54 #include <sys/interrupt.h>
58 static void mca_reg_print (device_t, char *, char *, int *);
61 struct resource_list rl; /* Resources */
66 u_int8_t pos[8]; /* Programable Option Select Regs. */
69 /* Not supposed to use this function! */
71 mca_pos_set (dev, reg, data)
76 struct mca_device * m_dev = device_get_ivars(dev);
77 u_int8_t slot = mca_get_slot(dev);
79 if ((slot > MCA_MAX_ADAPTERS) || (reg > MCA_POS7))
82 /* Disable motherboard setup */
83 outb(MCA_MB_SETUP_REG, MCA_MB_SETUP_DIS);
85 /* Select adapter setup regs */
86 outb(MCA_ADAP_SETUP_REG, ((slot & 0x0f) | MCA_ADAP_SET));
88 /* Write the register */
89 outb(MCA_POS_REG(reg), data);
91 /* Disable adapter setup */
92 outb(MCA_ADAP_SETUP_REG, MCA_ADAP_SETUP_DIS);
94 /* Update the IVAR copy */
95 m_dev->pos[reg] = data;
101 mca_pos_get (dev, reg)
105 u_int8_t slot = mca_get_slot(dev);
108 if ((slot > MCA_MAX_ADAPTERS) || (reg > MCA_POS7))
111 /* Disable motherboard setup */
112 outb(MCA_MB_SETUP_REG, MCA_MB_SETUP_DIS);
115 case MCA_MB_SCSI_SLOT:
117 /* Disable adapter setup */
118 outb(MCA_ADAP_SETUP_REG, MCA_ADAP_SETUP_DIS);
120 /* Select motherboard video setup regs */
121 outb(MCA_MB_SETUP_REG, MCA_MB_SETUP_SCSI);
123 /* read the register */
124 data = inb(MCA_POS_REG(reg));
126 /* Disable motherboard setup */
127 outb(MCA_MB_SETUP_REG, MCA_MB_SETUP_DIS);
130 case MCA_MB_VIDEO_SLOT:
131 /* Disable adapter setup */
132 outb(MCA_ADAP_SETUP_REG, MCA_ADAP_SETUP_DIS);
134 /* Select motherboard scsi setup regs */
135 outb(MCA_MB_SETUP_REG, MCA_MB_SETUP_VIDEO);
137 /* read the register */
138 data = inb(MCA_POS_REG(reg));
140 /* Disable motherboard setup */
141 outb(MCA_MB_SETUP_REG, MCA_MB_SETUP_DIS);
145 /* Select adapter setup regs */
146 outb(MCA_ADAP_SETUP_REG,
147 ((slot & 0x0f) | MCA_ADAP_SET));
149 /* read the register */
150 data = inb(MCA_POS_REG(reg));
152 /* Disable adapter setup */
153 outb(MCA_ADAP_SETUP_REG, MCA_ADAP_SETUP_DIS);
161 mca_match_id (id, mca_devs)
163 struct mca_ident * mca_devs;
165 struct mca_ident * m = mca_devs;
166 while(m->name != NULL) {
175 mca_pos_read (dev, reg)
179 struct mca_device * m_dev = device_get_ivars(dev);
184 return (m_dev->pos[reg]);
188 mca_add_irq (dev, irq)
192 struct mca_device * m_dev = device_get_ivars(dev);
195 while (resource_list_find(&(m_dev->rl), SYS_RES_IRQ, rid)) rid++;
196 resource_list_add(&(m_dev->rl), SYS_RES_IRQ, rid, irq, irq, 1);
202 mca_add_drq (dev, drq)
206 struct mca_device * m_dev = device_get_ivars(dev);
209 while (resource_list_find(&(m_dev->rl), SYS_RES_DRQ, rid)) rid++;
210 resource_list_add(&(m_dev->rl), SYS_RES_DRQ, rid, drq, drq, 1);
216 mca_add_mspace (dev, mbase, msize)
221 struct mca_device * m_dev = device_get_ivars(dev);
224 while (resource_list_find(&(m_dev->rl), SYS_RES_MEMORY, rid)) rid++;
225 resource_list_add(&(m_dev->rl), SYS_RES_MEMORY, rid,
226 mbase, (mbase + msize), msize);
232 mca_add_iospace (dev, iobase, iosize)
237 struct mca_device * m_dev = device_get_ivars(dev);
240 while (resource_list_find(&(m_dev->rl), SYS_RES_IOPORT, rid)) rid++;
241 resource_list_add(&(m_dev->rl), SYS_RES_IOPORT, rid,
242 iobase, (iobase + iosize), iosize);
248 mca_probe (device_t dev)
251 struct mca_device * m_dev = NULL;
252 int devices_found = 0;
256 device_set_desc(dev, "MCA bus");
258 /* Disable adapter setup */
259 outb(MCA_ADAP_SETUP_REG, MCA_ADAP_SETUP_DIS);
260 /* Disable motherboard setup */
261 outb(MCA_MB_SETUP_REG, MCA_MB_SETUP_DIS);
264 printf("POS REG 00 01 02 03 04 05 06 07\n");
265 printf("-----------------------------------\n");
268 for (slot = 0; slot < MCA_MAX_SLOTS; slot++) {
271 m_dev = (struct mca_device *)malloc(sizeof(*m_dev),
274 device_printf(dev, "cannot malloc mca_device");
278 bzero(m_dev, sizeof(*m_dev));
280 /* Select adapter setup regs */
281 outb(MCA_ADAP_SETUP_REG, ((slot & 0x0f) | MCA_ADAP_SET));
283 /* Read the POS registers */
284 for (reg = MCA_POS0; reg <= MCA_POS7; reg++) {
285 m_dev->pos[reg] = inb(MCA_POS_REG(reg));
288 /* Disable adapter setup */
289 outb(MCA_ADAP_SETUP_REG, MCA_ADAP_SETUP_DIS);
292 printf("mca slot %d:", slot + 1);
293 for (reg = MCA_POS0; reg <= MCA_POS7; reg++) {
294 printf(" %02x", m_dev->pos[reg]);
299 m_dev->id = (u_int16_t)m_dev->pos[MCA_POS0] |
300 ((u_int16_t)m_dev->pos[MCA_POS1] << 8);
302 if (m_dev->id == 0xffff) {
308 m_dev->enabled = (m_dev->pos[MCA_POS2] & MCA_POS2_ENABLE);
311 resource_list_init(&(m_dev->rl));
313 child = device_add_child(dev, NULL, -1);
314 device_set_ivars(child, m_dev);
320 free(m_dev, M_DEVBUF);
323 return (devices_found ? 0 : ENXIO);
327 mca_reg_print (dev, string, separator, column)
333 int length = strlen(string);
335 length += (separator ? 2 : 1);
337 if (((*column) + length) >= MAX_COL) {
340 } else if ((*column) != 0) {
342 printf("%c", *separator);
349 if ((*column) == 0) {
350 (*column) += device_printf(dev, "%s", string);
352 (*column) += printf("%s", string);
359 mca_print_child (device_t dev, device_t child)
362 struct mca_device * m_dev = device_get_ivars(child);
364 struct resource_list_entry * rle;
365 char separator = ',';
369 if (device_get_desc(child)) {
370 snprintf(buf, sizeof(buf), "<%s>", device_get_desc(child));
371 mca_reg_print(child, buf, NULL, &column);
375 while ((rle = resource_list_find(&(m_dev->rl), SYS_RES_IOPORT, rid++))) {
376 if (rle->count == 1) {
377 snprintf(buf, sizeof(buf), "%s%lx",
378 ((rid == 1) ? "io 0x" : "0x"),
381 snprintf(buf, sizeof(buf), "%s%lx-0x%lx",
382 ((rid == 1) ? "io 0x" : "0x"),
384 (rle->start + rle->count));
386 mca_reg_print(child, buf,
387 ((rid == 2) ? &separator : NULL), &column);
391 while ((rle = resource_list_find(&(m_dev->rl), SYS_RES_MEMORY, rid++))) {
392 if (rle->count == 1) {
393 snprintf(buf, sizeof(buf), "%s%lx",
394 ((rid == 1) ? "mem 0x" : "0x"),
397 snprintf(buf, sizeof(buf), "%s%lx-0x%lx",
398 ((rid == 1) ? "mem 0x" : "0x"),
400 (rle->start + rle->count));
402 mca_reg_print(child, buf,
403 ((rid == 2) ? &separator : NULL), &column);
407 while ((rle = resource_list_find(&(m_dev->rl), SYS_RES_IRQ, rid++))) {
408 snprintf(buf, sizeof(buf), "irq %ld", rle->start);
409 mca_reg_print(child, buf,
410 ((rid == 1) ? &separator : NULL), &column);
414 while ((rle = resource_list_find(&(m_dev->rl), SYS_RES_DRQ, rid++))) {
415 snprintf(buf, sizeof(buf), "drq %lx", rle->start);
416 mca_reg_print(child, buf,
417 ((rid == 1) ? &separator : NULL), &column);
420 snprintf(buf, sizeof(buf), "on %s id %04x slot %d\n",
421 device_get_nameunit(dev),
422 mca_get_id(child), mca_get_slot(child)+1);
423 mca_reg_print(child, buf, NULL, &column);
429 mca_probe_nomatch (device_t dev, device_t child)
431 mca_id_t mca_id = mca_get_id(child);
432 u_int8_t slot = mca_get_slot(child);
433 u_int8_t enabled = mca_get_enabled(child);
435 device_printf(dev, "unknown card (id 0x%04x, %s) at slot %d\n",
437 (enabled ? "enabled" : "disabled"),
444 mca_read_ivar (device_t dev, device_t child, int which, u_long * result)
446 struct mca_device * m_dev = device_get_ivars(child);
450 *result = m_dev->slot;
455 case MCA_IVAR_ENABLED:
456 *result = m_dev->enabled;
466 static struct resource *
467 mca_alloc_resource (device_t dev, device_t child, int type, int *rid,
468 u_long start, u_long end, u_long count, u_int flags)
470 struct mca_device * m_dev = device_get_ivars(child);
471 struct resource_list_entry * rle;
475 isdefault = (start == 0UL && end == ~0UL);
476 passthrough = (device_get_parent(child) != dev);
478 if (!passthrough && !isdefault) {
479 rle = resource_list_find(&(m_dev->rl), type, *rid);
481 resource_list_add(&(m_dev->rl), type, *rid,
486 if (type == SYS_RES_IRQ) {
487 flags |= RF_SHAREABLE;
490 return (resource_list_alloc(&(m_dev->rl), dev, child, type, rid,
491 start, end, count, flags));
494 static struct resource_list *
495 mca_get_resource_list (device_t dev, device_t child)
497 struct mca_device * m_dev = device_get_ivars(child);
498 struct resource_list * rl = &m_dev->rl;
506 static device_method_t mca_methods[] = {
507 /* Device interface */
508 DEVMETHOD(device_probe, mca_probe),
509 DEVMETHOD(device_attach, bus_generic_attach),
510 DEVMETHOD(device_shutdown, bus_generic_shutdown),
511 DEVMETHOD(device_suspend, bus_generic_suspend),
512 DEVMETHOD(device_resume, bus_generic_resume),
515 DEVMETHOD(bus_print_child, mca_print_child),
516 DEVMETHOD(bus_probe_nomatch, mca_probe_nomatch),
517 DEVMETHOD(bus_read_ivar, mca_read_ivar),
518 DEVMETHOD(bus_write_ivar, bus_generic_write_ivar),
519 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
520 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
521 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
523 DEVMETHOD(bus_get_resource_list,mca_get_resource_list),
524 DEVMETHOD(bus_alloc_resource, mca_alloc_resource),
525 DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource),
526 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
527 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
528 DEVMETHOD(bus_delete_resource, bus_generic_rl_delete_resource),
529 DEVMETHOD(bus_activate_resource,bus_generic_activate_resource),
530 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
535 static driver_t mca_driver = {
541 static devclass_t mca_devclass;
543 DRIVER_MODULE(mca, legacy, mca_driver, mca_devclass, 0, 0);