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