eisaconf.c revision 94275
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 * $FreeBSD: head/sys/dev/eisa/eisaconf.c 94275 2002-04-09 11:18:46Z phk $
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 <dev/eisa/eisaconf.h>
50
51typedef struct resvaddr {
52        u_long	addr;				/* start address */
53        u_long	size;				/* size of reserved area */
54	int	flags;
55	struct resource *res;			/* resource manager handle */
56	LIST_ENTRY(resvaddr) links;		/* List links */
57} resvaddr_t;
58
59LIST_HEAD(resvlist, resvaddr);
60
61struct irq_node {
62	int	irq_no;
63	int	irq_trigger;
64	void	*idesc;
65	TAILQ_ENTRY(irq_node) links;
66};
67
68TAILQ_HEAD(irqlist, irq_node);
69
70struct eisa_ioconf {
71	int		slot;
72	struct resvlist	ioaddrs;	/* list of reserved I/O ranges */
73	struct resvlist maddrs;		/* list of reserved memory ranges */
74	struct irqlist	irqs;		/* list of reserved irqs */
75};
76
77/* To be replaced by the "super device" generic device structure... */
78struct eisa_device {
79	eisa_id_t		id;
80	struct eisa_ioconf	ioconf;
81};
82
83
84#define MAX_COL		79
85#ifndef EISA_SLOTS
86#define EISA_SLOTS 10   /* PCI clashes with higher ones.. fix later */
87#endif
88int num_eisa_slots = EISA_SLOTS;
89
90static devclass_t eisa_devclass;
91
92static void eisa_reg_print (device_t, char *, char *, int *);
93static struct irq_node * eisa_find_irq(struct eisa_device *e_dev, int rid);
94static struct resvaddr * eisa_find_maddr(struct eisa_device *e_dev, int rid);
95static struct resvaddr * eisa_find_ioaddr(struct eisa_device *e_dev, int rid);
96
97static int
98mainboard_probe(device_t dev)
99{
100	char *idstring;
101	eisa_id_t id = eisa_get_id(dev);
102
103	if (eisa_get_slot(dev) != 0)
104		return (ENXIO);
105
106	idstring = (char *)malloc(8 + sizeof(" (System Board)") + 1,
107				  M_DEVBUF, M_NOWAIT);
108	if (idstring == NULL) {
109		panic("Eisa probe unable to malloc");
110	}
111	sprintf(idstring, "%c%c%c%03x%01x (System Board)",
112		EISA_MFCTR_CHAR0(id),
113		EISA_MFCTR_CHAR1(id),
114		EISA_MFCTR_CHAR2(id),
115		EISA_PRODUCT_ID(id),
116		EISA_REVISION_ID(id));
117	device_set_desc(dev, idstring);
118
119	return (0);
120}
121
122static int
123mainboard_attach(device_t dev)
124{
125	return (0);
126}
127
128static device_method_t mainboard_methods[] = {
129	/* Device interface */
130	DEVMETHOD(device_probe,		mainboard_probe),
131	DEVMETHOD(device_attach,	mainboard_attach),
132
133	{ 0, 0 }
134};
135
136static driver_t mainboard_driver = {
137	"mainboard",
138	mainboard_methods,
139	1,
140};
141
142static devclass_t mainboard_devclass;
143
144DRIVER_MODULE(mainboard, eisa, mainboard_driver, mainboard_devclass, 0, 0);
145
146/*
147** probe for EISA devices
148*/
149static int
150eisa_probe(device_t dev)
151{
152	int i,slot;
153	struct eisa_device *e_dev;
154	device_t child;
155	int eisaBase = 0xc80;
156	eisa_id_t eisa_id;
157	int devices_found = 0;
158
159	device_set_desc(dev, "EISA bus");
160
161	for (slot = 0; slot < num_eisa_slots; eisaBase+=0x1000, slot++) {
162		int id_size = sizeof(eisa_id);
163		eisa_id = 0;
164    		for( i = 0; i < id_size; i++ ) {
165			outb(eisaBase,0x80 + i); /*Some cards require priming*/
166			eisa_id |= inb(eisaBase+i) << ((id_size-i-1)*CHAR_BIT);
167		}
168		if (eisa_id & 0x80000000)
169			continue;  /* no EISA card in slot */
170
171		devices_found++;
172
173		/* Prepare an eisa_device_node for this slot */
174		e_dev = (struct eisa_device *)malloc(sizeof(*e_dev),
175						     M_DEVBUF, M_NOWAIT|M_ZERO);
176		if (!e_dev) {
177			device_printf(dev, "cannot malloc eisa_device");
178			break; /* Try to attach what we have already */
179		}
180
181		e_dev->id = eisa_id;
182
183		e_dev->ioconf.slot = slot;
184
185		/* Initialize our lists of reserved addresses */
186		LIST_INIT(&(e_dev->ioconf.ioaddrs));
187		LIST_INIT(&(e_dev->ioconf.maddrs));
188		TAILQ_INIT(&(e_dev->ioconf.irqs));
189
190		child = device_add_child(dev, NULL, -1);
191		device_set_ivars(child, e_dev);
192	}
193
194	/*
195	 * EISA busses themselves are not easily detectable, the easiest way
196	 * to tell if there is an eisa bus is if we found something - there
197	 * should be a motherboard "card" there somewhere.
198	 */
199	return devices_found ? 0 : ENXIO;
200}
201
202static void
203eisa_probe_nomatch(device_t dev, device_t child)
204{
205	u_int32_t	eisa_id = eisa_get_id(child);
206	u_int8_t	slot = eisa_get_slot(child);
207
208	device_printf(dev, "unknown card %c%c%c%03x%01x (0x%08x) at slot %d\n",
209		EISA_MFCTR_CHAR0(eisa_id),
210		EISA_MFCTR_CHAR1(eisa_id),
211		EISA_MFCTR_CHAR2(eisa_id),
212		EISA_PRODUCT_ID(eisa_id),
213		EISA_REVISION_ID(eisa_id),
214		eisa_id,
215		slot);
216
217	return;
218}
219
220static void
221eisa_reg_print (dev, string, separator, column)
222	device_t	dev;
223	char *		string;
224	char *		separator;
225	int *		column;
226{
227	int length = strlen(string);
228
229	length += (separator ? 2 : 1);
230
231	if (((*column) + length) >= MAX_COL) {
232		printf("\n");
233		(*column) = 0;
234	} else if ((*column) != 0) {
235		if (separator) {
236			printf("%c", *separator);
237			(*column)++;
238		}
239		printf(" ");
240		(*column)++;
241	}
242
243	if ((*column) == 0) {
244		(*column) += device_printf(dev, "%s", string);
245	} else {
246		(*column) += printf("%s", string);
247	}
248
249	return;
250}
251
252static int
253eisa_print_child(device_t dev, device_t child)
254{
255	char			buf[81];
256	struct eisa_device *	e_dev = device_get_ivars(child);
257	int			rid;
258	struct irq_node *	irq;
259	struct resvaddr *	resv;
260	char			separator = ',';
261	int			column = 0;
262	int			retval = 0;
263
264	if (device_get_desc(child)) {
265		snprintf(buf, sizeof(buf), "<%s>", device_get_desc(child));
266		eisa_reg_print(child, buf, NULL, &column);
267	}
268
269	rid = 0;
270	while ((resv = eisa_find_ioaddr(e_dev, rid++))) {
271		if ((resv->size == 1) ||
272		    (resv->flags & RESVADDR_BITMASK)) {
273			snprintf(buf, sizeof(buf), "%s%lx",
274				((rid == 1) ? "at 0x" : "0x"),
275				resv->addr);
276		} else {
277			snprintf(buf, sizeof(buf), "%s%lx-0x%lx",
278				((rid == 1) ? "at 0x" : "0x"),
279				resv->addr,
280				(resv->addr + (resv->size - 1)));
281		}
282		eisa_reg_print(child, buf,
283			((rid == 2) ? &separator : NULL), &column);
284	}
285
286	rid = 0;
287	while ((resv = eisa_find_maddr(e_dev, rid++))) {
288		if ((resv->size == 1) ||
289		    (resv->flags & RESVADDR_BITMASK)) {
290			snprintf(buf, sizeof(buf), "%s%lx",
291				((rid == 1) ? "at 0x" : "0x"),
292				resv->addr);
293		} else {
294			snprintf(buf, sizeof(buf), "%s%lx-0x%lx",
295				((rid == 1) ? "at 0x" : "0x"),
296				resv->addr,
297				(resv->addr + (resv->size - 1)));
298		}
299		eisa_reg_print(child, buf,
300			((rid == 2) ? &separator : NULL), &column);
301	}
302
303	rid = 0;
304	while ((irq = eisa_find_irq(e_dev, rid++)) != NULL) {
305		snprintf(buf, sizeof(buf), "irq %d (%s)", irq->irq_no,
306			 (irq->irq_trigger ? "level" : "edge"));
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 struct irq_node *
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);
331	else
332		return (NULL);
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	struct irq_node *irq;
368
369	switch (which) {
370	case EISA_IVAR_SLOT:
371		*result = e_dev->ioconf.slot;
372		break;
373
374	case EISA_IVAR_ID:
375		*result = e_dev->id;
376		break;
377
378	case EISA_IVAR_IRQ:
379		/* XXX only first irq */
380		if ((irq = eisa_find_irq(e_dev, 0)) != NULL) {
381			*result = irq->irq_no;
382		} else {
383			*result = -1;
384		}
385		break;
386
387	default:
388		return (ENOENT);
389	}
390
391	return (0);
392}
393
394static int
395eisa_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
396{
397	return (EINVAL);
398}
399
400static struct resource *
401eisa_alloc_resource(device_t dev, device_t child, int type, int *rid,
402		    u_long start, u_long end, u_long count, u_int flags)
403{
404	int isdefault;
405	struct eisa_device *e_dev = device_get_ivars(child);
406	struct resource *rv, **rvp = 0;
407
408	isdefault = (device_get_parent(child) == dev
409		     && start == 0UL && end == ~0UL && count == 1);
410
411	switch (type) {
412	case SYS_RES_IRQ:
413		if (isdefault) {
414			struct irq_node * irq = eisa_find_irq(e_dev, *rid);
415			if (irq == NULL)
416				return 0;
417			start = end = irq->irq_no;
418			count = 1;
419			if (irq->irq_trigger == EISA_TRIGGER_LEVEL) {
420				flags |= RF_SHAREABLE;
421			} else {
422				flags &= ~RF_SHAREABLE;
423			}
424		}
425		break;
426
427	case SYS_RES_MEMORY:
428		if (isdefault) {
429			struct resvaddr *resv;
430
431			resv = eisa_find_maddr(e_dev, *rid);
432			if (!resv)
433				return 0;
434
435			start = resv->addr;
436			end = resv->addr + (resv->size - 1);
437			count = resv->size;
438			rvp = &resv->res;
439		}
440		break;
441
442	case SYS_RES_IOPORT:
443		if (isdefault) {
444			struct resvaddr *resv;
445
446			resv = eisa_find_ioaddr(e_dev, *rid);
447			if (!resv)
448				return 0;
449
450			start = resv->addr;
451			end = resv->addr + (resv->size - 1);
452			count = resv->size;
453			rvp = &resv->res;
454		}
455		break;
456
457	default:
458		return 0;
459	}
460
461	rv = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
462				 type, rid, start, end, count, flags);
463	if (rvp)
464		*rvp = rv;
465
466	return rv;
467}
468
469static int
470eisa_release_resource(device_t dev, device_t child, int type, int rid,
471		      struct resource *r)
472{
473	int rv;
474	struct eisa_device *e_dev = device_get_ivars(child);
475	struct resvaddr *resv = 0;
476
477	switch (type) {
478	case SYS_RES_IRQ:
479		if (eisa_find_irq(e_dev, rid) == NULL)
480			return EINVAL;
481		break;
482
483	case SYS_RES_MEMORY:
484		if (device_get_parent(child) == dev)
485			resv = eisa_find_maddr(e_dev, rid);
486		break;
487
488
489	case SYS_RES_IOPORT:
490		if (device_get_parent(child) == dev)
491			resv = eisa_find_ioaddr(e_dev, rid);
492		break;
493
494	default:
495		return (ENOENT);
496	}
497
498	rv = BUS_RELEASE_RESOURCE(device_get_parent(dev), child, type, rid, r);
499
500	if (rv == 0) {
501		if (resv)
502			resv->res = 0;
503	}
504
505	return rv;
506}
507
508int
509eisa_add_intr(device_t dev, int irq, int trigger)
510{
511	struct eisa_device *e_dev = device_get_ivars(dev);
512	struct irq_node *irq_info;
513
514	irq_info = (struct irq_node *)malloc(sizeof(*irq_info), M_DEVBUF,
515					     M_NOWAIT);
516	if (irq_info == NULL)
517		return (1);
518
519	irq_info->irq_no = irq;
520	irq_info->irq_trigger = trigger;
521	irq_info->idesc = NULL;
522	TAILQ_INSERT_TAIL(&e_dev->ioconf.irqs, irq_info, links);
523	return 0;
524}
525
526static int
527eisa_add_resvaddr(struct eisa_device *e_dev, struct resvlist *head, u_long base,
528		  u_long size, int flags)
529{
530	resvaddr_t *reservation;
531
532	reservation = (resvaddr_t *)malloc(sizeof(resvaddr_t),
533					   M_DEVBUF, M_NOWAIT);
534	if(!reservation)
535		return (ENOMEM);
536
537	reservation->addr = base;
538	reservation->size = size;
539	reservation->flags = flags;
540
541	if (!LIST_FIRST(head)) {
542		LIST_INSERT_HEAD(head, reservation, links);
543	}
544	else {
545		resvaddr_t *node;
546		LIST_FOREACH(node, head, links) {
547			if (node->addr > reservation->addr) {
548				/*
549				 * List is sorted in increasing
550				 * address order.
551				 */
552				LIST_INSERT_BEFORE(node, reservation, links);
553				break;
554			}
555
556			if (node->addr == reservation->addr) {
557				/*
558				 * If the entry we want to add
559				 * matches any already in here,
560				 * fail.
561				 */
562				free(reservation, M_DEVBUF);
563				return (EEXIST);
564			}
565
566			if (!LIST_NEXT(node, links)) {
567				LIST_INSERT_AFTER(node, reservation, links);
568				break;
569			}
570		}
571	}
572	return (0);
573}
574
575int
576eisa_add_mspace(device_t dev, u_long mbase, u_long msize, int flags)
577{
578	struct eisa_device *e_dev = device_get_ivars(dev);
579
580	return	eisa_add_resvaddr(e_dev, &(e_dev->ioconf.maddrs), mbase, msize,
581				  flags);
582}
583
584int
585eisa_add_iospace(device_t dev, u_long iobase, u_long iosize, int flags)
586{
587	struct eisa_device *e_dev = device_get_ivars(dev);
588
589	return	eisa_add_resvaddr(e_dev, &(e_dev->ioconf.ioaddrs), iobase,
590				  iosize, flags);
591}
592
593static device_method_t eisa_methods[] = {
594	/* Device interface */
595	DEVMETHOD(device_probe,		eisa_probe),
596	DEVMETHOD(device_attach,	bus_generic_attach),
597	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
598	DEVMETHOD(device_suspend,	bus_generic_suspend),
599	DEVMETHOD(device_resume,	bus_generic_resume),
600
601	/* Bus interface */
602	DEVMETHOD(bus_print_child,	eisa_print_child),
603	DEVMETHOD(bus_probe_nomatch,	eisa_probe_nomatch),
604	DEVMETHOD(bus_read_ivar,	eisa_read_ivar),
605	DEVMETHOD(bus_write_ivar,	eisa_write_ivar),
606	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
607	DEVMETHOD(bus_alloc_resource,	eisa_alloc_resource),
608	DEVMETHOD(bus_release_resource,	eisa_release_resource),
609	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
610	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
611	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
612	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
613
614	{ 0, 0 }
615};
616
617static driver_t eisa_driver = {
618	"eisa",
619	eisa_methods,
620	1,			/* no softc */
621};
622
623DRIVER_MODULE(eisa, eisab, eisa_driver, eisa_devclass, 0, 0);
624DRIVER_MODULE(eisa, nexus, eisa_driver, eisa_devclass, 0, 0);
625