xref: /freebsd/sys/isa/isahint.c (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
16c2e3ddeSDoug Rabson /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3fe267a55SPedro F. Giffuni  *
46c2e3ddeSDoug Rabson  * Copyright (c) 1999 Doug Rabson
56c2e3ddeSDoug Rabson  * All rights reserved.
66c2e3ddeSDoug Rabson  *
76c2e3ddeSDoug Rabson  * Redistribution and use in source and binary forms, with or without
86c2e3ddeSDoug Rabson  * modification, are permitted provided that the following conditions
96c2e3ddeSDoug Rabson  * are met:
106c2e3ddeSDoug Rabson  * 1. Redistributions of source code must retain the above copyright
116c2e3ddeSDoug Rabson  *    notice, this list of conditions and the following disclaimer.
126c2e3ddeSDoug Rabson  * 2. Redistributions in binary form must reproduce the above copyright
136c2e3ddeSDoug Rabson  *    notice, this list of conditions and the following disclaimer in the
146c2e3ddeSDoug Rabson  *    documentation and/or other materials provided with the distribution.
156c2e3ddeSDoug Rabson  *
166c2e3ddeSDoug Rabson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
176c2e3ddeSDoug Rabson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
186c2e3ddeSDoug Rabson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
196c2e3ddeSDoug Rabson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
206c2e3ddeSDoug Rabson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
216c2e3ddeSDoug Rabson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
226c2e3ddeSDoug Rabson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
236c2e3ddeSDoug Rabson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
246c2e3ddeSDoug Rabson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
256c2e3ddeSDoug Rabson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
266c2e3ddeSDoug Rabson  * SUCH DAMAGE.
276c2e3ddeSDoug Rabson  */
286c2e3ddeSDoug Rabson 
298c9bbf48SDavid E. O'Brien #include <sys/cdefs.h>
308c9bbf48SDavid E. O'Brien __FBSDID("$FreeBSD$");
318c9bbf48SDavid E. O'Brien 
326c2e3ddeSDoug Rabson #include <sys/param.h>
336c2e3ddeSDoug Rabson #include <sys/systm.h>
346c2e3ddeSDoug Rabson #include <sys/kernel.h>
356c2e3ddeSDoug Rabson #include <sys/bus.h>
366c2e3ddeSDoug Rabson #include <sys/module.h>
376c2e3ddeSDoug Rabson #include <isa/isavar.h>
380d484d24SJohn Baldwin #include <isa/isa_common.h>
39a3be63b3SDoug Rabson #include <machine/resource.h>
406c2e3ddeSDoug Rabson 
410d484d24SJohn Baldwin void
420d484d24SJohn Baldwin isa_hinted_child(device_t parent, const char *name, int unit)
436c2e3ddeSDoug Rabson {
446c2e3ddeSDoug Rabson 	device_t	child;
45e5979322SNate Lawson 	int		sensitive, start, count;
46bea6af4dSDoug Rabson 	int		order;
476c2e3ddeSDoug Rabson 
486c2e3ddeSDoug Rabson 	/* device-specific flag overrides any wildcard */
496c2e3ddeSDoug Rabson 	sensitive = 0;
506c2e3ddeSDoug Rabson 	if (resource_int_value(name, unit, "sensitive", &sensitive) != 0)
516c2e3ddeSDoug Rabson 		resource_int_value(name, -1, "sensitive", &sensitive);
526c2e3ddeSDoug Rabson 
536c2e3ddeSDoug Rabson 	if (sensitive)
54bea6af4dSDoug Rabson 		order = ISA_ORDER_SENSITIVE;
556c2e3ddeSDoug Rabson 	else
56bea6af4dSDoug Rabson 		order = ISA_ORDER_SPECULATIVE;
57bea6af4dSDoug Rabson 
58bea6af4dSDoug Rabson 	child = BUS_ADD_CHILD(parent, order, name, unit);
596c2e3ddeSDoug Rabson 	if (child == 0)
606c2e3ddeSDoug Rabson 		return;
616c2e3ddeSDoug Rabson 
62a3be63b3SDoug Rabson 	start = 0;
63a3be63b3SDoug Rabson 	count = 0;
640197892dSYoshihiro Takahashi 	resource_int_value(name, unit, "port", &start);
650197892dSYoshihiro Takahashi 	resource_int_value(name, unit, "portsize", &count);
660197892dSYoshihiro Takahashi 	if (start > 0 || count > 0)
6725afb89bSDoug Rabson 		bus_set_resource(child, SYS_RES_IOPORT, 0, start, count);
686c2e3ddeSDoug Rabson 
69a3be63b3SDoug Rabson 	start = 0;
70a3be63b3SDoug Rabson 	count = 0;
710197892dSYoshihiro Takahashi 	resource_int_value(name, unit, "maddr", &start);
720197892dSYoshihiro Takahashi 	resource_int_value(name, unit, "msize", &count);
730197892dSYoshihiro Takahashi 	if (start > 0 || count > 0)
7425afb89bSDoug Rabson 		bus_set_resource(child, SYS_RES_MEMORY, 0, start, count);
756c2e3ddeSDoug Rabson 
76828cb040SKazutaka YOKOTA 	if (resource_int_value(name, unit, "irq", &start) == 0 && start > 0)
7725afb89bSDoug Rabson 		bus_set_resource(child, SYS_RES_IRQ, 0, start, 1);
786c2e3ddeSDoug Rabson 
790e7e521cSAndrew Gallatin 	if (resource_int_value(name, unit, "drq", &start) == 0 && start >= 0)
8025afb89bSDoug Rabson 		bus_set_resource(child, SYS_RES_DRQ, 0, start, 1);
816c2e3ddeSDoug Rabson 
828a9bc9c0SJohn Baldwin 	if (resource_disabled(name, unit))
836c2e3ddeSDoug Rabson 		device_disable(child);
842ca94fcaSMatthew N. Dodd 
852ca94fcaSMatthew N. Dodd 	isa_set_configattr(child, (isa_get_configattr(child)|ISACFGATTR_HINTS));
866c2e3ddeSDoug Rabson }
876c2e3ddeSDoug Rabson 
880d484d24SJohn Baldwin static int
890d484d24SJohn Baldwin isa_match_resource_hint(device_t dev, int type, long value)
906c2e3ddeSDoug Rabson {
910d484d24SJohn Baldwin 	struct isa_device* idev = DEVTOISA(dev);
920d484d24SJohn Baldwin 	struct resource_list *rl = &idev->id_resources;
930d484d24SJohn Baldwin 	struct resource_list_entry *rle;
946c2e3ddeSDoug Rabson 
950d484d24SJohn Baldwin 	STAILQ_FOREACH(rle, rl, link) {
960d484d24SJohn Baldwin 		if (rle->type != type)
970d484d24SJohn Baldwin 			continue;
980d484d24SJohn Baldwin 		if (rle->start <= value && rle->end >= value)
990d484d24SJohn Baldwin 			return (1);
1000d484d24SJohn Baldwin 	}
1010d484d24SJohn Baldwin 	return (0);
1026c2e3ddeSDoug Rabson }
1036c2e3ddeSDoug Rabson 
1040d484d24SJohn Baldwin void
1050d484d24SJohn Baldwin isa_hint_device_unit(device_t bus, device_t child, const char *name, int *unitp)
1060d484d24SJohn Baldwin {
1070d484d24SJohn Baldwin 	const char *s;
1080d484d24SJohn Baldwin 	long value;
1090d484d24SJohn Baldwin 	int line, matches, unit;
1106c2e3ddeSDoug Rabson 
1110d484d24SJohn Baldwin 	line = 0;
1120d484d24SJohn Baldwin 	for (;;) {
1130d484d24SJohn Baldwin 		if (resource_find_dev(&line, name, &unit, "at", NULL) != 0)
1140d484d24SJohn Baldwin 			break;
1156c2e3ddeSDoug Rabson 
1160d484d24SJohn Baldwin 		/* Must have an "at" for isa. */
1170d484d24SJohn Baldwin 		resource_string_value(name, unit, "at", &s);
1180d484d24SJohn Baldwin 		if (!(strcmp(s, device_get_nameunit(bus)) == 0 ||
1190d484d24SJohn Baldwin 		    strcmp(s, device_get_name(bus)) == 0))
1200d484d24SJohn Baldwin 			continue;
1216c2e3ddeSDoug Rabson 
1220d484d24SJohn Baldwin 		/*
1232fcd493cSJohn Baldwin 		 * Check for matching resources.  We must have at
1242fcd493cSJohn Baldwin 		 * least one match.  Since I/O and memory resources
1252fcd493cSJohn Baldwin 		 * cannot be shared, if we get a match on either of
1262fcd493cSJohn Baldwin 		 * those, ignore any mismatches in IRQs or DRQs.
1270d484d24SJohn Baldwin 		 *
1282fcd493cSJohn Baldwin 		 * XXX: We may want to revisit this to be more lenient
1292fcd493cSJohn Baldwin 		 * and wire as long as it gets one match.
1300d484d24SJohn Baldwin 		 */
1310d484d24SJohn Baldwin 		matches = 0;
1320d484d24SJohn Baldwin 		if (resource_long_value(name, unit, "port", &value) == 0) {
1332fcd493cSJohn Baldwin 			/*
1342fcd493cSJohn Baldwin 			 * Floppy drive controllers are notorious for
1352fcd493cSJohn Baldwin 			 * having a wide variety of resources not all
1362fcd493cSJohn Baldwin 			 * of which include the first port that is
1372fcd493cSJohn Baldwin 			 * specified by the hint (typically 0x3f0)
1382fcd493cSJohn Baldwin 			 * (see the comment above
1392fcd493cSJohn Baldwin 			 * fdc_isa_alloc_resources() in fdc_isa.c).
1402fcd493cSJohn Baldwin 			 * However, they do all seem to include port +
1412fcd493cSJohn Baldwin 			 * 2 (e.g. 0x3f2) so for a floppy device, look
1422fcd493cSJohn Baldwin 			 * for 'value + 2' in the port resources
1432fcd493cSJohn Baldwin 			 * instead of the hint value.
1442fcd493cSJohn Baldwin 			 */
1452fcd493cSJohn Baldwin 			if (strcmp(name, "fdc") == 0)
1462fcd493cSJohn Baldwin 				value += 2;
1470d484d24SJohn Baldwin 			if (isa_match_resource_hint(child, SYS_RES_IOPORT,
1480d484d24SJohn Baldwin 			    value))
1490d484d24SJohn Baldwin 				matches++;
1500d484d24SJohn Baldwin 			else
1510d484d24SJohn Baldwin 				continue;
1520d484d24SJohn Baldwin 		}
1530d484d24SJohn Baldwin 		if (resource_long_value(name, unit, "maddr", &value) == 0) {
1540d484d24SJohn Baldwin 			if (isa_match_resource_hint(child, SYS_RES_MEMORY,
1550d484d24SJohn Baldwin 			    value))
1560d484d24SJohn Baldwin 				matches++;
1570d484d24SJohn Baldwin 			else
1580d484d24SJohn Baldwin 				continue;
1590d484d24SJohn Baldwin 		}
1602fcd493cSJohn Baldwin 		if (matches > 0)
1612fcd493cSJohn Baldwin 			goto matched;
1620d484d24SJohn Baldwin 		if (resource_long_value(name, unit, "irq", &value) == 0) {
1630d484d24SJohn Baldwin 			if (isa_match_resource_hint(child, SYS_RES_IRQ, value))
1640d484d24SJohn Baldwin 				matches++;
1650d484d24SJohn Baldwin 			else
1660d484d24SJohn Baldwin 				continue;
1670d484d24SJohn Baldwin 		}
1680d484d24SJohn Baldwin 		if (resource_long_value(name, unit, "drq", &value) == 0) {
1690d484d24SJohn Baldwin 			if (isa_match_resource_hint(child, SYS_RES_DRQ, value))
1700d484d24SJohn Baldwin 				matches++;
1710d484d24SJohn Baldwin 			else
1720d484d24SJohn Baldwin 				continue;
1730d484d24SJohn Baldwin 		}
1746c2e3ddeSDoug Rabson 
1752fcd493cSJohn Baldwin 	matched:
1760d484d24SJohn Baldwin 		if (matches > 0) {
1770d484d24SJohn Baldwin 			/* We have a winner! */
1780d484d24SJohn Baldwin 			*unitp = unit;
1790d484d24SJohn Baldwin 			break;
1800d484d24SJohn Baldwin 		}
1810d484d24SJohn Baldwin 	}
1820d484d24SJohn Baldwin }
183