eisaconf.c revision 49195
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.47 1999/07/11 13:42:35 dfr 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%03x%01x (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	int devices_found = 0;
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		devices_found++;
167
168		/* Prepare an eisa_device_node for this slot */
169		e_dev = (struct eisa_device *)malloc(sizeof(*e_dev),
170						     M_DEVBUF, M_NOWAIT);
171		if (!e_dev) {
172			device_printf(dev, "cannot malloc eisa_device");
173			break; /* Try to attach what we have already */
174		}
175		bzero(e_dev, sizeof(*e_dev));
176
177		e_dev->id = eisa_id;
178
179		e_dev->ioconf.slot = slot;
180
181		/* Initialize our lists of reserved addresses */
182		LIST_INIT(&(e_dev->ioconf.ioaddrs));
183		LIST_INIT(&(e_dev->ioconf.maddrs));
184		TAILQ_INIT(&(e_dev->ioconf.irqs));
185
186		device_add_child(dev, NULL, -1, e_dev);
187	}
188
189	/*
190	 * EISA busses themselves are not easily detectable, the easiest way
191	 * to tell if there is an eisa bus is if we found something - there
192	 * should be a motherboard "card" there somewhere.
193	 */
194	return devices_found ? 0 : ENXIO;
195}
196
197static void
198eisa_probe_nomatch(device_t dev, device_t child)
199{
200	u_int32_t	eisa_id = eisa_get_id(child);
201	u_int8_t	slot = eisa_get_slot(child);
202
203	device_printf(dev, "unknown card %c%c%c%03x%01x (0x%08x) at slot %d\n",
204		EISA_MFCTR_CHAR0(eisa_id),
205		EISA_MFCTR_CHAR1(eisa_id),
206		EISA_MFCTR_CHAR2(eisa_id),
207		EISA_PRODUCT_ID(eisa_id),
208		EISA_REVISION_ID(eisa_id),
209		eisa_id,
210		slot);
211
212	return;
213}
214
215static int
216eisa_print_child(device_t dev, device_t child)
217{
218	int retval = 0;
219
220	bus_print_child_header(dev, child);
221
222	retval += printf(" on %s slot %d", device_get_nameunit(dev),
223			 eisa_get_slot(child));
224
225	return (retval);
226}
227
228static int
229eisa_find_irq(struct eisa_device *e_dev, int rid)
230{
231	int i;
232	struct irq_node *irq;
233
234	for (i = 0, irq = TAILQ_FIRST(&e_dev->ioconf.irqs);
235	     i < rid && irq;
236	     i++, irq = TAILQ_NEXT(irq, links))
237		;
238
239	if (irq)
240		return irq->irq_no;
241	else
242		return -1;
243}
244
245static struct resvaddr *
246eisa_find_maddr(struct eisa_device *e_dev, int rid)
247{
248	int i;
249	struct resvaddr *resv;
250
251	for (i = 0, resv = LIST_FIRST(&e_dev->ioconf.maddrs);
252	     i < rid && resv;
253	     i++, resv = LIST_NEXT(resv, links))
254		;
255
256	return resv;
257}
258
259static struct resvaddr *
260eisa_find_ioaddr(struct eisa_device *e_dev, int rid)
261{
262	int i;
263	struct resvaddr *resv;
264
265	for (i = 0, resv = LIST_FIRST(&e_dev->ioconf.ioaddrs);
266	     i < rid && resv;
267	     i++, resv = LIST_NEXT(resv, links))
268		;
269
270	return resv;
271}
272
273static int
274eisa_read_ivar(device_t dev, device_t child, int which, u_long *result)
275{
276	struct eisa_device *e_dev = device_get_ivars(child);
277
278	switch (which) {
279	case EISA_IVAR_SLOT:
280		*result = e_dev->ioconf.slot;
281		break;
282
283	case EISA_IVAR_ID:
284		*result = e_dev->id;
285		break;
286
287	case EISA_IVAR_IRQ:
288		/* XXX only first irq */
289		*result = eisa_find_irq(e_dev, 0);
290		break;
291
292	default:
293		return (ENOENT);
294	}
295
296	return (0);
297}
298
299static int
300eisa_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
301{
302	return (EINVAL);
303}
304
305static struct resource *
306eisa_alloc_resource(device_t dev, device_t child, int type, int *rid,
307		    u_long start, u_long end, u_long count, u_int flags)
308{
309	int isdefault;
310	struct eisa_device *e_dev = device_get_ivars(child);
311	struct resource *rv, **rvp = 0;
312
313	isdefault = (device_get_parent(child) == dev
314		     && start == 0UL && end == ~0UL && count == 1);
315
316	switch (type) {
317	case SYS_RES_IRQ:
318		if (isdefault) {
319			int irq = eisa_find_irq(e_dev, *rid);
320			if (irq == -1)
321				return 0;
322			start = end = irq;
323			count = 1;
324		}
325		break;
326
327	case SYS_RES_MEMORY:
328		if (isdefault) {
329			struct resvaddr *resv;
330
331			resv = eisa_find_maddr(e_dev, *rid);
332			if (!resv)
333				return 0;
334
335			start = resv->addr;
336			end = resv->addr + (resv->size - 1);
337			count = resv->size;
338			rvp = &resv->res;
339		}
340		break;
341
342	case SYS_RES_IOPORT:
343		if (isdefault) {
344			struct resvaddr *resv;
345
346			resv = eisa_find_ioaddr(e_dev, *rid);
347			if (!resv)
348				return 0;
349
350			start = resv->addr;
351			end = resv->addr + (resv->size - 1);
352			count = resv->size;
353			rvp = &resv->res;
354		}
355		break;
356
357	default:
358		return 0;
359	}
360
361	rv = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
362				 type, rid, start, end, count, flags);
363	if (rvp)
364		*rvp = rv;
365
366	return rv;
367}
368
369static int
370eisa_release_resource(device_t dev, device_t child, int type, int rid,
371		      struct resource *r)
372{
373	int rv;
374	struct eisa_device *e_dev = device_get_ivars(child);
375	struct resvaddr *resv = 0;
376
377	switch (type) {
378	case SYS_RES_IRQ:
379		if (eisa_find_irq(e_dev, rid) == -1)
380			return EINVAL;
381		break;
382
383	case SYS_RES_MEMORY:
384		if (device_get_parent(child) == dev)
385			resv = eisa_find_maddr(e_dev, rid);
386		break;
387
388
389	case SYS_RES_IOPORT:
390		if (device_get_parent(child) == dev)
391			resv = eisa_find_ioaddr(e_dev, rid);
392		break;
393
394	default:
395		return (ENOENT);
396	}
397
398	rv = BUS_RELEASE_RESOURCE(device_get_parent(dev), child, type, rid, r);
399
400	if (rv == 0) {
401		if (resv)
402			resv->res = 0;
403	}
404
405	return rv;
406}
407
408int
409eisa_add_intr(device_t dev, int irq)
410{
411	struct eisa_device *e_dev = device_get_ivars(dev);
412	struct	irq_node *irq_info;
413
414	irq_info = (struct irq_node *)malloc(sizeof(*irq_info), M_DEVBUF,
415					     M_NOWAIT);
416	if (irq_info == NULL)
417		return (1);
418
419	irq_info->irq_no = irq;
420	irq_info->idesc = NULL;
421	TAILQ_INSERT_TAIL(&e_dev->ioconf.irqs, irq_info, links);
422	return 0;
423}
424
425static int
426eisa_add_resvaddr(struct eisa_device *e_dev, struct resvlist *head, u_long base,
427		  u_long size, int flags)
428{
429	resvaddr_t *reservation;
430
431	reservation = (resvaddr_t *)malloc(sizeof(resvaddr_t),
432					   M_DEVBUF, M_NOWAIT);
433	if(!reservation)
434		return (ENOMEM);
435
436	reservation->addr = base;
437	reservation->size = size;
438	reservation->flags = flags;
439
440	if (!head->lh_first) {
441		LIST_INSERT_HEAD(head, reservation, links);
442	}
443	else {
444		resvaddr_t *node;
445		for(node = head->lh_first; node; node = node->links.le_next) {
446			if (node->addr > reservation->addr) {
447				/*
448				 * List is sorted in increasing
449				 * address order.
450				 */
451				LIST_INSERT_BEFORE(node, reservation, links);
452				break;
453			}
454
455			if (node->addr == reservation->addr) {
456				/*
457				 * If the entry we want to add
458				 * matches any already in here,
459				 * fail.
460				 */
461				free(reservation, M_DEVBUF);
462				return (EEXIST);
463			}
464
465			if (!node->links.le_next) {
466				LIST_INSERT_AFTER(node, reservation, links);
467				break;
468			}
469		}
470	}
471	return (0);
472}
473
474int
475eisa_add_mspace(device_t dev, u_long mbase, u_long msize, int flags)
476{
477	struct eisa_device *e_dev = device_get_ivars(dev);
478
479	return	eisa_add_resvaddr(e_dev, &(e_dev->ioconf.maddrs), mbase, msize,
480				  flags);
481}
482
483int
484eisa_add_iospace(device_t dev, u_long iobase, u_long iosize, int flags)
485{
486	struct eisa_device *e_dev = device_get_ivars(dev);
487
488	return	eisa_add_resvaddr(e_dev, &(e_dev->ioconf.ioaddrs), iobase,
489				  iosize, flags);
490}
491
492static device_method_t eisa_methods[] = {
493	/* Device interface */
494	DEVMETHOD(device_probe,		eisa_probe),
495	DEVMETHOD(device_attach,	bus_generic_attach),
496	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
497	DEVMETHOD(device_suspend,	bus_generic_suspend),
498	DEVMETHOD(device_resume,	bus_generic_resume),
499
500	/* Bus interface */
501	DEVMETHOD(bus_print_child,	eisa_print_child),
502	DEVMETHOD(bus_probe_nomatch,	eisa_probe_nomatch),
503	DEVMETHOD(bus_read_ivar,	eisa_read_ivar),
504	DEVMETHOD(bus_write_ivar,	eisa_write_ivar),
505	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
506	DEVMETHOD(bus_alloc_resource,	eisa_alloc_resource),
507	DEVMETHOD(bus_release_resource,	eisa_release_resource),
508	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
509	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
510	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
511	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
512
513	{ 0, 0 }
514};
515
516static driver_t eisa_driver = {
517	"eisa",
518	eisa_methods,
519	1,			/* no softc */
520};
521
522DRIVER_MODULE(eisa, isab, eisa_driver, eisa_devclass, 0, 0);
523DRIVER_MODULE(eisa, nexus, eisa_driver, eisa_devclass, 0, 0);
524