eisaconf.c revision 49281
1/*
2 * EISA bus probe and attach routines
3 *
4 * Copyright (c) 1995, 1996 Justin T. Gibbs.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice immediately at the beginning of the file, without modification,
12 *    this list of conditions, and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 *    derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 *	$Id: eisaconf.c,v 1.48 1999/07/29 01:02:51 mdodd Exp $
32 */
33
34#include "opt_eisa.h"
35
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>
42#include <sys/bus.h>
43
44#include <machine/limits.h>
45#include <machine/bus.h>
46#include <machine/resource.h>
47#include <sys/rman.h>
48
49#include <i386/eisa/eisaconf.h>
50
51#include <sys/interrupt.h>
52
53typedef struct resvaddr {
54        u_long	addr;				/* start address */
55        u_long	size;				/* size of reserved area */
56	int	flags;
57	struct resource *res;			/* resource manager handle */
58	LIST_ENTRY(resvaddr) links;		/* List links */
59} resvaddr_t;
60
61LIST_HEAD(resvlist, resvaddr);
62
63struct irq_node {
64	int	irq_no;
65	void	*idesc;
66	TAILQ_ENTRY(irq_node) links;
67};
68
69TAILQ_HEAD(irqlist, irq_node);
70
71struct eisa_ioconf {
72	int		slot;
73	struct resvlist	ioaddrs;	/* list of reserved I/O ranges */
74	struct resvlist maddrs;		/* list of reserved memory ranges */
75	struct irqlist	irqs;		/* list of reserved irqs */
76};
77
78/* To be replaced by the "super device" generic device structure... */
79struct eisa_device {
80	eisa_id_t		id;
81	struct eisa_ioconf	ioconf;
82};
83
84
85/* Global variable, so UserConfig can change it. */
86#define MAX_COL		79
87#ifndef EISA_SLOTS
88#define EISA_SLOTS 10   /* PCI clashes with higher ones.. fix later */
89#endif
90int num_eisa_slots = EISA_SLOTS;
91
92static devclass_t eisa_devclass;
93
94static void eisa_reg_print (device_t, char *, char *, int *);
95static int eisa_find_irq(struct eisa_device *e_dev, int rid);
96static struct resvaddr * eisa_find_maddr(struct eisa_device *e_dev, int rid);
97static struct resvaddr * eisa_find_ioaddr(struct eisa_device *e_dev, int rid);
98
99static int
100mainboard_probe(device_t dev)
101{
102	char *idstring;
103	eisa_id_t id = eisa_get_id(dev);
104
105	if (eisa_get_slot(dev) != 0)
106		return (ENXIO);
107
108	idstring = (char *)malloc(8 + sizeof(" (System Board)") + 1,
109				  M_DEVBUF, M_NOWAIT);
110	if (idstring == NULL) {
111		panic("Eisa probe unable to malloc");
112	}
113	sprintf(idstring, "%c%c%c%03x%01x (System Board)",
114		EISA_MFCTR_CHAR0(id),
115		EISA_MFCTR_CHAR1(id),
116		EISA_MFCTR_CHAR2(id),
117		EISA_PRODUCT_ID(id),
118		EISA_REVISION_ID(id));
119	device_set_desc(dev, idstring);
120
121	return (0);
122}
123
124static int
125mainboard_attach(device_t dev)
126{
127	return (0);
128}
129
130static device_method_t mainboard_methods[] = {
131	/* Device interface */
132	DEVMETHOD(device_probe,		mainboard_probe),
133	DEVMETHOD(device_attach,	mainboard_attach),
134
135	{ 0, 0 }
136};
137
138static driver_t mainboard_driver = {
139	"mainboard",
140	mainboard_methods,
141	1,
142};
143
144static devclass_t mainboard_devclass;
145
146DRIVER_MODULE(mainboard, eisa, mainboard_driver, mainboard_devclass, 0, 0);
147
148/*
149** probe for EISA devices
150*/
151static int
152eisa_probe(device_t dev)
153{
154	int i,slot;
155	struct eisa_device *e_dev;
156	int eisaBase = 0xc80;
157	eisa_id_t eisa_id;
158	int devices_found = 0;
159
160	device_set_desc(dev, "EISA bus");
161
162	for (slot = 0; slot < num_eisa_slots; eisaBase+=0x1000, slot++) {
163		int id_size = sizeof(eisa_id);
164		eisa_id = 0;
165    		for( i = 0; i < id_size; i++ ) {
166			outb(eisaBase,0x80 + i); /*Some cards require priming*/
167			eisa_id |= inb(eisaBase+i) << ((id_size-i-1)*CHAR_BIT);
168		}
169		if (eisa_id & 0x80000000)
170			continue;  /* no EISA card in slot */
171
172		devices_found++;
173
174		/* Prepare an eisa_device_node for this slot */
175		e_dev = (struct eisa_device *)malloc(sizeof(*e_dev),
176						     M_DEVBUF, M_NOWAIT);
177		if (!e_dev) {
178			device_printf(dev, "cannot malloc eisa_device");
179			break; /* Try to attach what we have already */
180		}
181		bzero(e_dev, sizeof(*e_dev));
182
183		e_dev->id = eisa_id;
184
185		e_dev->ioconf.slot = slot;
186
187		/* Initialize our lists of reserved addresses */
188		LIST_INIT(&(e_dev->ioconf.ioaddrs));
189		LIST_INIT(&(e_dev->ioconf.maddrs));
190		TAILQ_INIT(&(e_dev->ioconf.irqs));
191
192		device_add_child(dev, NULL, -1, e_dev);
193	}
194
195	/*
196	 * EISA busses themselves are not easily detectable, the easiest way
197	 * to tell if there is an eisa bus is if we found something - there
198	 * should be a motherboard "card" there somewhere.
199	 */
200	return devices_found ? 0 : ENXIO;
201}
202
203static void
204eisa_probe_nomatch(device_t dev, device_t child)
205{
206	u_int32_t	eisa_id = eisa_get_id(child);
207	u_int8_t	slot = eisa_get_slot(child);
208
209	device_printf(dev, "unknown card %c%c%c%03x%01x (0x%08x) at slot %d\n",
210		EISA_MFCTR_CHAR0(eisa_id),
211		EISA_MFCTR_CHAR1(eisa_id),
212		EISA_MFCTR_CHAR2(eisa_id),
213		EISA_PRODUCT_ID(eisa_id),
214		EISA_REVISION_ID(eisa_id),
215		eisa_id,
216		slot);
217
218	return;
219}
220
221static void
222eisa_reg_print (dev, string, separator, column)
223	device_t	dev;
224	char *		string;
225	char *		separator;
226	int *		column;
227{
228	int length = strlen(string);
229
230	length += (separator ? 2 : 1);
231
232	if (((*column) + length) >= MAX_COL) {
233		printf("\n");
234		(*column) = 0;
235	} else if ((*column) != 0) {
236		if (separator) {
237			printf("%c", *separator);
238			(*column)++;
239		}
240		printf(" ");
241		(*column)++;
242	}
243
244	if ((*column) == 0) {
245		(*column) += device_printf(dev, "%s", string);
246	} else {
247		(*column) += printf("%s", string);
248	}
249
250	return;
251}
252
253static int
254eisa_print_child(device_t dev, device_t child)
255{
256	char			buf[81];
257	struct eisa_device *	e_dev = device_get_ivars(child);
258	int			rid;
259	int			irq;
260	struct resvaddr *	resv;
261	char			separator = ',';
262	int			column = 0;
263	int			retval = 0;
264
265	if (device_get_desc(child)) {
266		snprintf(buf, sizeof(buf), "<%s>", device_get_desc(child));
267		eisa_reg_print(child, buf, NULL, &column);
268	}
269
270	rid = 0;
271	while ((resv = eisa_find_ioaddr(e_dev, rid++))) {
272		if ((resv->size == 1) ||
273		    (resv->flags & RESVADDR_BITMASK)) {
274			snprintf(buf, sizeof(buf), "%s%lx",
275				((rid == 1) ? "at 0x" : "0x"),
276				resv->addr);
277		} else {
278			snprintf(buf, sizeof(buf), "%s%lx-0x%lx",
279				((rid == 1) ? "at 0x" : "0x"),
280				resv->addr,
281				(resv->addr + (resv->size - 1)));
282		}
283		eisa_reg_print(child, buf,
284			((rid == 2) ? &separator : NULL), &column);
285	}
286
287	rid = 0;
288	while ((resv = eisa_find_maddr(e_dev, rid++))) {
289		if ((resv->size == 1) ||
290		    (resv->flags & RESVADDR_BITMASK)) {
291			snprintf(buf, sizeof(buf), "%s%lx",
292				((rid == 1) ? "at 0x" : "0x"),
293				resv->addr);
294		} else {
295			snprintf(buf, sizeof(buf), "%s%lx-0x%lx",
296				((rid == 1) ? "at 0x" : "0x"),
297				resv->addr,
298				(resv->addr + (resv->size - 1)));
299		}
300		eisa_reg_print(child, buf,
301			((rid == 2) ? &separator : NULL), &column);
302	}
303
304	rid = 0;
305	while ((irq = eisa_find_irq(e_dev, rid++)) != -1) {
306		snprintf(buf, sizeof(buf), "irq %d", irq);
307		eisa_reg_print(child, buf,
308			((rid == 1) ? &separator : NULL), &column);
309	}
310
311	snprintf(buf, sizeof(buf), "on %s slot %d\n",
312		device_get_nameunit(dev), eisa_get_slot(child));
313	eisa_reg_print(child, buf, NULL, &column);
314
315	return (retval);
316}
317
318static int
319eisa_find_irq(struct eisa_device *e_dev, int rid)
320{
321	int i;
322	struct irq_node *irq;
323
324	for (i = 0, irq = TAILQ_FIRST(&e_dev->ioconf.irqs);
325	     i < rid && irq;
326	     i++, irq = TAILQ_NEXT(irq, links))
327		;
328
329	if (irq)
330		return irq->irq_no;
331	else
332		return -1;
333}
334
335static struct resvaddr *
336eisa_find_maddr(struct eisa_device *e_dev, int rid)
337{
338	int i;
339	struct resvaddr *resv;
340
341	for (i = 0, resv = LIST_FIRST(&e_dev->ioconf.maddrs);
342	     i < rid && resv;
343	     i++, resv = LIST_NEXT(resv, links))
344		;
345
346	return resv;
347}
348
349static struct resvaddr *
350eisa_find_ioaddr(struct eisa_device *e_dev, int rid)
351{
352	int i;
353	struct resvaddr *resv;
354
355	for (i = 0, resv = LIST_FIRST(&e_dev->ioconf.ioaddrs);
356	     i < rid && resv;
357	     i++, resv = LIST_NEXT(resv, links))
358		;
359
360	return resv;
361}
362
363static int
364eisa_read_ivar(device_t dev, device_t child, int which, u_long *result)
365{
366	struct eisa_device *e_dev = device_get_ivars(child);
367
368	switch (which) {
369	case EISA_IVAR_SLOT:
370		*result = e_dev->ioconf.slot;
371		break;
372
373	case EISA_IVAR_ID:
374		*result = e_dev->id;
375		break;
376
377	case EISA_IVAR_IRQ:
378		/* XXX only first irq */
379		*result = eisa_find_irq(e_dev, 0);
380		break;
381
382	default:
383		return (ENOENT);
384	}
385
386	return (0);
387}
388
389static int
390eisa_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
391{
392	return (EINVAL);
393}
394
395static struct resource *
396eisa_alloc_resource(device_t dev, device_t child, int type, int *rid,
397		    u_long start, u_long end, u_long count, u_int flags)
398{
399	int isdefault;
400	struct eisa_device *e_dev = device_get_ivars(child);
401	struct resource *rv, **rvp = 0;
402
403	isdefault = (device_get_parent(child) == dev
404		     && start == 0UL && end == ~0UL && count == 1);
405
406	switch (type) {
407	case SYS_RES_IRQ:
408		if (isdefault) {
409			int irq = eisa_find_irq(e_dev, *rid);
410			if (irq == -1)
411				return 0;
412			start = end = irq;
413			count = 1;
414		}
415		break;
416
417	case SYS_RES_MEMORY:
418		if (isdefault) {
419			struct resvaddr *resv;
420
421			resv = eisa_find_maddr(e_dev, *rid);
422			if (!resv)
423				return 0;
424
425			start = resv->addr;
426			end = resv->addr + (resv->size - 1);
427			count = resv->size;
428			rvp = &resv->res;
429		}
430		break;
431
432	case SYS_RES_IOPORT:
433		if (isdefault) {
434			struct resvaddr *resv;
435
436			resv = eisa_find_ioaddr(e_dev, *rid);
437			if (!resv)
438				return 0;
439
440			start = resv->addr;
441			end = resv->addr + (resv->size - 1);
442			count = resv->size;
443			rvp = &resv->res;
444		}
445		break;
446
447	default:
448		return 0;
449	}
450
451	rv = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
452				 type, rid, start, end, count, flags);
453	if (rvp)
454		*rvp = rv;
455
456	return rv;
457}
458
459static int
460eisa_release_resource(device_t dev, device_t child, int type, int rid,
461		      struct resource *r)
462{
463	int rv;
464	struct eisa_device *e_dev = device_get_ivars(child);
465	struct resvaddr *resv = 0;
466
467	switch (type) {
468	case SYS_RES_IRQ:
469		if (eisa_find_irq(e_dev, rid) == -1)
470			return EINVAL;
471		break;
472
473	case SYS_RES_MEMORY:
474		if (device_get_parent(child) == dev)
475			resv = eisa_find_maddr(e_dev, rid);
476		break;
477
478
479	case SYS_RES_IOPORT:
480		if (device_get_parent(child) == dev)
481			resv = eisa_find_ioaddr(e_dev, rid);
482		break;
483
484	default:
485		return (ENOENT);
486	}
487
488	rv = BUS_RELEASE_RESOURCE(device_get_parent(dev), child, type, rid, r);
489
490	if (rv == 0) {
491		if (resv)
492			resv->res = 0;
493	}
494
495	return rv;
496}
497
498int
499eisa_add_intr(device_t dev, int irq)
500{
501	struct eisa_device *e_dev = device_get_ivars(dev);
502	struct	irq_node *irq_info;
503
504	irq_info = (struct irq_node *)malloc(sizeof(*irq_info), M_DEVBUF,
505					     M_NOWAIT);
506	if (irq_info == NULL)
507		return (1);
508
509	irq_info->irq_no = irq;
510	irq_info->idesc = NULL;
511	TAILQ_INSERT_TAIL(&e_dev->ioconf.irqs, irq_info, links);
512	return 0;
513}
514
515static int
516eisa_add_resvaddr(struct eisa_device *e_dev, struct resvlist *head, u_long base,
517		  u_long size, int flags)
518{
519	resvaddr_t *reservation;
520
521	reservation = (resvaddr_t *)malloc(sizeof(resvaddr_t),
522					   M_DEVBUF, M_NOWAIT);
523	if(!reservation)
524		return (ENOMEM);
525
526	reservation->addr = base;
527	reservation->size = size;
528	reservation->flags = flags;
529
530	if (!head->lh_first) {
531		LIST_INSERT_HEAD(head, reservation, links);
532	}
533	else {
534		resvaddr_t *node;
535		for(node = head->lh_first; node; node = node->links.le_next) {
536			if (node->addr > reservation->addr) {
537				/*
538				 * List is sorted in increasing
539				 * address order.
540				 */
541				LIST_INSERT_BEFORE(node, reservation, links);
542				break;
543			}
544
545			if (node->addr == reservation->addr) {
546				/*
547				 * If the entry we want to add
548				 * matches any already in here,
549				 * fail.
550				 */
551				free(reservation, M_DEVBUF);
552				return (EEXIST);
553			}
554
555			if (!node->links.le_next) {
556				LIST_INSERT_AFTER(node, reservation, links);
557				break;
558			}
559		}
560	}
561	return (0);
562}
563
564int
565eisa_add_mspace(device_t dev, u_long mbase, u_long msize, int flags)
566{
567	struct eisa_device *e_dev = device_get_ivars(dev);
568
569	return	eisa_add_resvaddr(e_dev, &(e_dev->ioconf.maddrs), mbase, msize,
570				  flags);
571}
572
573int
574eisa_add_iospace(device_t dev, u_long iobase, u_long iosize, int flags)
575{
576	struct eisa_device *e_dev = device_get_ivars(dev);
577
578	return	eisa_add_resvaddr(e_dev, &(e_dev->ioconf.ioaddrs), iobase,
579				  iosize, flags);
580}
581
582static device_method_t eisa_methods[] = {
583	/* Device interface */
584	DEVMETHOD(device_probe,		eisa_probe),
585	DEVMETHOD(device_attach,	bus_generic_attach),
586	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
587	DEVMETHOD(device_suspend,	bus_generic_suspend),
588	DEVMETHOD(device_resume,	bus_generic_resume),
589
590	/* Bus interface */
591	DEVMETHOD(bus_print_child,	eisa_print_child),
592	DEVMETHOD(bus_probe_nomatch,	eisa_probe_nomatch),
593	DEVMETHOD(bus_read_ivar,	eisa_read_ivar),
594	DEVMETHOD(bus_write_ivar,	eisa_write_ivar),
595	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
596	DEVMETHOD(bus_alloc_resource,	eisa_alloc_resource),
597	DEVMETHOD(bus_release_resource,	eisa_release_resource),
598	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
599	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
600	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
601	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
602
603	{ 0, 0 }
604};
605
606static driver_t eisa_driver = {
607	"eisa",
608	eisa_methods,
609	1,			/* no softc */
610};
611
612DRIVER_MODULE(eisa, isab, eisa_driver, eisa_devclass, 0, 0);
613DRIVER_MODULE(eisa, nexus, eisa_driver, eisa_devclass, 0, 0);
614