eisaconf.c revision 45818
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.40 1999/04/19 07:58:34 peter 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#ifndef EISA_SLOTS
87#define EISA_SLOTS 10   /* PCI clashes with higher ones.. fix later */
88#endif
89int num_eisa_slots = EISA_SLOTS;
90
91static devclass_t eisa_devclass;
92
93static int
94mainboard_probe(device_t dev)
95{
96	char *idstring;
97	eisa_id_t id = eisa_get_id(dev);
98
99	if (eisa_get_slot(dev) != 0)
100		return (ENXIO);
101
102	idstring = (char *)malloc(8 + sizeof(" (System Board)") + 1,
103				  M_DEVBUF, M_NOWAIT);
104	if (idstring == NULL) {
105		panic("Eisa probe unable to malloc");
106	}
107	sprintf(idstring, "%c%c%c%x%x (System Board)",
108		EISA_MFCTR_CHAR0(id),
109		EISA_MFCTR_CHAR1(id),
110		EISA_MFCTR_CHAR2(id),
111		EISA_PRODUCT_ID(id),
112		EISA_REVISION_ID(id));
113	device_set_desc(dev, idstring);
114
115	return (0);
116}
117
118static int
119mainboard_attach(device_t dev)
120{
121	return (0);
122}
123
124static device_method_t mainboard_methods[] = {
125	/* Device interface */
126	DEVMETHOD(device_probe,		mainboard_probe),
127	DEVMETHOD(device_attach,	mainboard_attach),
128
129	{ 0, 0 }
130};
131
132static driver_t mainboard_driver = {
133	"mainboard",
134	mainboard_methods,
135	DRIVER_TYPE_MISC,
136	1,
137};
138
139static devclass_t mainboard_devclass;
140
141DRIVER_MODULE(mainboard, eisa, mainboard_driver, mainboard_devclass, 0, 0);
142
143/*
144** probe for EISA devices
145*/
146static int
147eisa_probe(device_t dev)
148{
149	int i,slot;
150	struct eisa_device *e_dev;
151	int eisaBase = 0xc80;
152	eisa_id_t eisa_id;
153
154	device_set_desc(dev, "EISA bus");
155
156	for (slot = 0; slot < num_eisa_slots; eisaBase+=0x1000, slot++) {
157		int id_size = sizeof(eisa_id);
158		eisa_id = 0;
159    		for( i = 0; i < id_size; i++ ) {
160			outb(eisaBase,0x80 + i); /*Some cards require priming*/
161			eisa_id |= inb(eisaBase+i) << ((id_size-i-1)*CHAR_BIT);
162		}
163		if (eisa_id & 0x80000000)
164			continue;  /* no EISA card in slot */
165
166		/* Prepare an eisa_device_node for this slot */
167		e_dev = (struct eisa_device *)malloc(sizeof(*e_dev),
168						     M_DEVBUF, M_NOWAIT);
169		if (!e_dev) {
170			device_printf(dev, "cannot malloc eisa_device");
171			break; /* Try to attach what we have already */
172		}
173		bzero(e_dev, sizeof(*e_dev));
174
175		e_dev->id = eisa_id;
176
177		e_dev->ioconf.slot = slot;
178
179		/* Initialize our lists of reserved addresses */
180		LIST_INIT(&(e_dev->ioconf.ioaddrs));
181		LIST_INIT(&(e_dev->ioconf.maddrs));
182		TAILQ_INIT(&(e_dev->ioconf.irqs));
183
184		device_add_child(dev, NULL, -1, e_dev);
185	}
186
187	return 0;
188}
189
190static void
191eisa_print_child(device_t dev, device_t child)
192{
193	/* XXX print resource descriptions? */
194	printf(" at slot %d", eisa_get_slot(child));
195	printf(" on %s", device_get_nameunit(dev));
196}
197
198static int
199eisa_find_irq(struct eisa_device *e_dev, int rid)
200{
201	int i;
202	struct irq_node *irq;
203
204	for (i = 0, irq = TAILQ_FIRST(&e_dev->ioconf.irqs);
205	     i < rid && irq;
206	     i++, irq = TAILQ_NEXT(irq, links))
207		;
208
209	if (irq)
210		return irq->irq_no;
211	else
212		return -1;
213}
214
215static struct resvaddr *
216eisa_find_maddr(struct eisa_device *e_dev, int rid)
217{
218	int i;
219	struct resvaddr *resv;
220
221	for (i = 0, resv = LIST_FIRST(&e_dev->ioconf.maddrs);
222	     i < rid && resv;
223	     i++, resv = LIST_NEXT(resv, links))
224		;
225
226	return resv;
227}
228
229static struct resvaddr *
230eisa_find_ioaddr(struct eisa_device *e_dev, int rid)
231{
232	int i;
233	struct resvaddr *resv;
234
235	for (i = 0, resv = LIST_FIRST(&e_dev->ioconf.ioaddrs);
236	     i < rid && resv;
237	     i++, resv = LIST_NEXT(resv, links))
238		;
239
240	return resv;
241}
242
243static int
244eisa_read_ivar(device_t dev, device_t child, int which, u_long *result)
245{
246	struct eisa_device *e_dev = device_get_ivars(child);
247
248	switch (which) {
249	case EISA_IVAR_SLOT:
250		*result = e_dev->ioconf.slot;
251		break;
252
253	case EISA_IVAR_ID:
254		*result = e_dev->id;
255		break;
256
257	case EISA_IVAR_IRQ:
258		/* XXX only first irq */
259		*result = eisa_find_irq(e_dev, 0);
260		break;
261
262	default:
263		return (ENOENT);
264	}
265
266	return (0);
267}
268
269static int
270eisa_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
271{
272	return (EINVAL);
273}
274
275static struct resource *
276eisa_alloc_resource(device_t dev, device_t child, int type, int *rid,
277		    u_long start, u_long end, u_long count, u_int flags)
278{
279	int isdefault;
280	struct eisa_device *e_dev = device_get_ivars(child);
281	struct resource *rv, **rvp = 0;
282
283	isdefault = (device_get_parent(child) == dev
284		     && start == 0UL && end == ~0UL && count == 1);
285
286	switch (type) {
287	case SYS_RES_IRQ:
288		if (isdefault) {
289			int irq = eisa_find_irq(e_dev, *rid);
290			if (irq == -1)
291				return 0;
292			start = end = irq;
293			count = 1;
294		}
295		break;
296
297	case SYS_RES_MEMORY:
298		if (isdefault) {
299			struct resvaddr *resv;
300
301			resv = eisa_find_maddr(e_dev, *rid);
302			if (!resv)
303				return 0;
304
305			start = resv->addr;
306			end = resv->size - 1;
307			count = resv->size;
308			rvp = &resv->res;
309		}
310		break;
311
312	case SYS_RES_IOPORT:
313		if (isdefault) {
314			struct resvaddr *resv;
315
316			resv = eisa_find_ioaddr(e_dev, *rid);
317			if (!resv)
318				return 0;
319
320			start = resv->addr;
321			end = resv->size - 1;
322			count = resv->size;
323			rvp = &resv->res;
324		}
325		break;
326
327	default:
328		return 0;
329	}
330
331	rv = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
332				 type, rid, start, end, count, flags);
333	if (rvp)
334		*rvp = rv;
335
336	return rv;
337}
338
339static int
340eisa_release_resource(device_t dev, device_t child, int type, int rid,
341		      struct resource *r)
342{
343	int rv;
344	struct eisa_device *e_dev = device_get_ivars(child);
345	struct resvaddr *resv = 0;
346
347	switch (type) {
348	case SYS_RES_IRQ:
349		if (eisa_find_irq(e_dev, rid) == -1)
350			return EINVAL;
351		break;
352
353	case SYS_RES_MEMORY:
354		if (device_get_parent(child) == dev)
355			resv = eisa_find_maddr(e_dev, rid);
356		break;
357
358
359	case SYS_RES_IOPORT:
360		if (device_get_parent(child) == dev)
361			resv = eisa_find_ioaddr(e_dev, rid);
362		break;
363
364	default:
365		return (ENOENT);
366	}
367
368	rv = BUS_RELEASE_RESOURCE(device_get_parent(dev), child, type, rid, r);
369
370	if (rv == 0) {
371		if (resv)
372			resv->res = 0;
373	}
374
375	return rv;
376}
377
378int
379eisa_add_intr(dev, irq)
380	device_t dev;
381	int irq;
382{
383	struct eisa_device *e_dev = device_get_ivars(dev);
384	struct	irq_node *irq_info;
385
386	irq_info = (struct irq_node *)malloc(sizeof(*irq_info), M_DEVBUF,
387					     M_NOWAIT);
388	if (irq_info == NULL)
389		return (1);
390
391	irq_info->irq_no = irq;
392	irq_info->idesc = NULL;
393	TAILQ_INSERT_TAIL(&e_dev->ioconf.irqs, irq_info, links);
394	return 0;
395}
396
397static int
398eisa_add_resvaddr(e_dev, head, base, size, flags)
399	struct eisa_device *e_dev;
400	struct resvlist *head;
401	u_long	base;
402	u_long	size;
403	int	flags;
404{
405	resvaddr_t *reservation;
406
407	reservation = (resvaddr_t *)malloc(sizeof(resvaddr_t),
408					   M_DEVBUF, M_NOWAIT);
409	if(!reservation)
410		return (ENOMEM);
411
412	reservation->addr = base;
413	reservation->size = size;
414	reservation->flags = flags;
415
416	if (!head->lh_first) {
417		LIST_INSERT_HEAD(head, reservation, links);
418	}
419	else {
420		resvaddr_t *node;
421		for(node = head->lh_first; node; node = node->links.le_next) {
422			if (node->addr > reservation->addr) {
423				/*
424				 * List is sorted in increasing
425				 * address order.
426				 */
427				LIST_INSERT_BEFORE(node, reservation, links);
428				break;
429			}
430
431			if (node->addr == reservation->addr) {
432				/*
433				 * If the entry we want to add
434				 * matches any already in here,
435				 * fail.
436				 */
437				free(reservation, M_DEVBUF);
438				return (EEXIST);
439			}
440
441			if (!node->links.le_next) {
442				LIST_INSERT_AFTER(node, reservation, links);
443				break;
444			}
445		}
446	}
447	return (0);
448}
449
450int
451eisa_add_mspace(dev, mbase, msize, flags)
452	device_t dev;
453	u_long	mbase;
454	u_long	msize;
455	int	flags;
456{
457	struct eisa_device *e_dev = device_get_ivars(dev);
458	return	eisa_add_resvaddr(e_dev, &(e_dev->ioconf.maddrs), mbase, msize,
459				  flags);
460}
461
462int
463eisa_add_iospace(dev, iobase, iosize, flags)
464	device_t dev;
465	u_long	iobase;
466	u_long	iosize;
467	int	flags;
468{
469	struct eisa_device *e_dev = device_get_ivars(dev);
470	return	eisa_add_resvaddr(e_dev, &(e_dev->ioconf.ioaddrs), iobase,
471				  iosize, flags);
472}
473
474static device_method_t eisa_methods[] = {
475	/* Device interface */
476	DEVMETHOD(device_probe,		eisa_probe),
477	DEVMETHOD(device_attach,	bus_generic_attach),
478	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
479
480	/* Bus interface */
481	DEVMETHOD(bus_print_child,	eisa_print_child),
482	DEVMETHOD(bus_read_ivar,	eisa_read_ivar),
483	DEVMETHOD(bus_write_ivar,	eisa_write_ivar),
484	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
485	DEVMETHOD(bus_alloc_resource,	eisa_alloc_resource),
486	DEVMETHOD(bus_release_resource,	eisa_release_resource),
487	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
488	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
489	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
490	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
491
492	{ 0, 0 }
493};
494
495static driver_t eisa_driver = {
496	"eisa",
497	eisa_methods,
498	DRIVER_TYPE_MISC,
499	1,			/* no softc */
500};
501
502DRIVER_MODULE(eisa, isab, eisa_driver, eisa_devclass, 0, 0);
503DRIVER_MODULE(eisa, nexus, eisa_driver, eisa_devclass, 0, 0);
504