eisaconf.c revision 148612
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 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: head/sys/dev/eisa/eisaconf.c 148612 2005-08-01 07:09:15Z imp $");
35
36#include "opt_eisa.h"
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/queue.h>
41#include <sys/limits.h>
42#include <sys/malloc.h>
43#include <sys/kernel.h>
44#include <sys/module.h>
45#include <sys/bus.h>
46
47#include <machine/bus.h>
48#include <machine/resource.h>
49#include <sys/rman.h>
50
51#include <dev/eisa/eisaconf.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	int	irq_trigger;
66	void	*idesc;
67	TAILQ_ENTRY(irq_node) links;
68};
69
70TAILQ_HEAD(irqlist, irq_node);
71
72struct eisa_ioconf {
73	int		slot;
74	struct resvlist	ioaddrs;	/* list of reserved I/O ranges */
75	struct resvlist maddrs;		/* list of reserved memory ranges */
76	struct irqlist	irqs;		/* list of reserved irqs */
77};
78
79/* To be replaced by the "super device" generic device structure... */
80struct eisa_device {
81	eisa_id_t		id;
82	struct eisa_ioconf	ioconf;
83};
84
85
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;
91TUNABLE_INT("hw.eisa_slots", &num_eisa_slots);
92
93static devclass_t eisa_devclass;
94
95static int eisa_probe_slot(int slot, eisa_id_t *eisa_id);
96static struct irq_node *eisa_find_irq(struct eisa_device *e_dev, int rid);
97static struct resvaddr *eisa_find_maddr(struct eisa_device *e_dev, int rid);
98static struct resvaddr *eisa_find_ioaddr(struct eisa_device *e_dev, int rid);
99
100static int
101mainboard_probe(device_t dev)
102{
103	char *idstring;
104	eisa_id_t id = eisa_get_id(dev);
105
106	if (eisa_get_slot(dev) != 0)
107		return (ENXIO);
108
109	idstring = (char *)malloc(8 + sizeof(" (System Board)") + 1,
110	    M_DEVBUF, M_NOWAIT);
111	if (idstring == NULL)
112		panic("Eisa probe unable to malloc");
113	sprintf(idstring, "%c%c%c%03x%01x (System Board)",
114	    EISA_MFCTR_CHAR0(id), EISA_MFCTR_CHAR1(id), EISA_MFCTR_CHAR2(id),
115	    EISA_PRODUCT_ID(id), EISA_REVISION_ID(id));
116	device_set_desc(dev, idstring);
117
118	return (0);
119}
120
121static int
122mainboard_attach(device_t dev)
123{
124	return (0);
125}
126
127static device_method_t mainboard_methods[] = {
128	/* Device interface */
129	DEVMETHOD(device_probe,		mainboard_probe),
130	DEVMETHOD(device_attach,	mainboard_attach),
131
132	{ 0, 0 }
133};
134
135static driver_t mainboard_driver = {
136	"mainboard",
137	mainboard_methods,
138	1,
139};
140
141static devclass_t mainboard_devclass;
142
143DRIVER_MODULE(mainboard, eisa, mainboard_driver, mainboard_devclass, 0, 0);
144
145/*
146** probe for EISA devices
147*/
148static int
149eisa_probe(device_t dev)
150{
151	int devices_found, slot;
152	struct eisa_device *e_dev;
153	device_t child;
154	eisa_id_t eisa_id;
155
156	device_set_desc(dev, "EISA bus");
157
158	devices_found = 0;
159	for (slot = 0; slot < num_eisa_slots; slot++) {
160		eisa_id = 0;
161		if (eisa_probe_slot(slot, &eisa_id)) {
162			/*
163			 * If there's no card in the first slot (the
164			 * mainboard), then the system doesn't have EISA.
165			 * We abort the probe early in this case since
166			 * continuing on causes a hang on some systems.
167			 * Interestingly enough, the inb has been seen to
168			 * cause the hang.
169			 */
170			if (slot == 0)
171				break;
172			continue;
173		}
174
175		devices_found++;
176
177		/* Prepare an eisa_device_node for this slot */
178		e_dev = (struct eisa_device *)malloc(sizeof(*e_dev),
179		    M_DEVBUF, M_NOWAIT|M_ZERO);
180		if (!e_dev) {
181			device_printf(dev, "cannot malloc eisa_device");
182			break; /* Try to attach what we have already */
183		}
184
185		e_dev->id = eisa_id;
186		e_dev->ioconf.slot = slot;
187
188		/* Initialize our lists of reserved addresses */
189		LIST_INIT(&(e_dev->ioconf.ioaddrs));
190		LIST_INIT(&(e_dev->ioconf.maddrs));
191		TAILQ_INIT(&(e_dev->ioconf.irqs));
192
193		child = device_add_child(dev, NULL, -1);
194		device_set_ivars(child, e_dev);
195	}
196
197	/*
198	 * EISA busses themselves are not easily detectable, the easiest way
199	 * to tell if there is an eisa bus is if we found something - there
200	 * should be a motherboard "card" there somewhere.
201	 */
202	return (devices_found ? 0 : ENXIO);
203}
204
205static int
206eisa_probe_slot(int slot, eisa_id_t *eisa_id)
207{
208	eisa_id_t probe_id;
209	int base, i, id_size;
210
211	probe_id = 0;
212	id_size = sizeof(probe_id);
213	base = 0x0c80 + (slot * 0x1000);
214
215	for (i = 0; i < id_size; i++)
216		probe_id |= inb(base + i) << ((id_size - i - 1) * CHAR_BIT);
217
218	/* If we found a card, return its EISA id. */
219	if ((probe_id & 0x80000000) == 0) {
220		*eisa_id = probe_id;
221		return (0);
222	}
223
224	return (ENXIO);
225}
226
227static void
228eisa_probe_nomatch(device_t dev, device_t child)
229{
230	u_int32_t	eisa_id = eisa_get_id(child);
231	u_int8_t	slot = eisa_get_slot(child);
232
233	device_printf(dev, "%c%c%c%03x%01x (0x%08x) at slot %d (no driver attached)\n",
234	    EISA_MFCTR_CHAR0(eisa_id), EISA_MFCTR_CHAR1(eisa_id),
235	    EISA_MFCTR_CHAR2(eisa_id), EISA_PRODUCT_ID(eisa_id),
236	    EISA_REVISION_ID(eisa_id), eisa_id, slot);
237	return;
238}
239
240static int
241eisa_print_child(device_t dev, device_t child)
242{
243	struct eisa_device *	e_dev = device_get_ivars(child);
244	int			rid;
245	struct irq_node *	irq;
246	struct resvaddr *	resv;
247	int			retval = 0;
248
249	retval += bus_print_child_header(dev, child);
250	rid = 0;
251	while ((resv = eisa_find_ioaddr(e_dev, rid++))) {
252		if (resv->size == 1 || (resv->flags & RESVADDR_BITMASK))
253			retval += printf("%s%lx", rid == 1 ? " port 0x" : ",0x",
254			    resv->addr);
255		else
256			retval += printf("%s%lx-0x%lx", rid == 1 ? " port 0x" :
257			    ",0x", resv->addr, resv->addr + resv->size - 1);
258	}
259	rid = 0;
260	while ((resv = eisa_find_maddr(e_dev, rid++))) {
261		if (resv->size == 1 || (resv->flags & RESVADDR_BITMASK))
262			retval += printf("%s%lx", rid == 1 ? " mem 0x" : ",0x",
263			    resv->addr);
264		else
265			retval += printf("%s%lx-0x%lx", rid == 1 ? " mem 0x" :
266			    ",0x", resv->addr, resv->addr + resv->size - 1);
267	}
268	rid = 0;
269	while ((irq = eisa_find_irq(e_dev, rid++)) != NULL)
270		retval += printf(" irq %d (%s)", irq->irq_no,
271		    irq->irq_trigger ? "level" : "edge");
272	retval += printf(" at slot %d on %s\n", eisa_get_slot(child),
273	    device_get_nameunit(dev));
274
275	return (retval);
276}
277
278static struct irq_node *
279eisa_find_irq(struct eisa_device *e_dev, int rid)
280{
281	int i;
282	struct irq_node *irq;
283
284	for (i = 0, irq = TAILQ_FIRST(&e_dev->ioconf.irqs);
285	     i < rid && irq != NULL; i++, irq = TAILQ_NEXT(irq, links))
286		continue;
287
288	return (irq);
289}
290
291static struct resvaddr *
292eisa_find_maddr(struct eisa_device *e_dev, int rid)
293{
294	int i;
295	struct resvaddr *resv;
296
297	for (i = 0, resv = LIST_FIRST(&e_dev->ioconf.maddrs);
298	     i < rid && resv != NULL; i++, resv = LIST_NEXT(resv, links))
299		continue;
300
301	return (resv);
302}
303
304static struct resvaddr *
305eisa_find_ioaddr(struct eisa_device *e_dev, int rid)
306{
307	int i;
308	struct resvaddr *resv;
309
310	for (i = 0, resv = LIST_FIRST(&e_dev->ioconf.ioaddrs);
311	     i < rid && resv != NULL; i++, resv = LIST_NEXT(resv, links))
312		continue;
313
314	return (resv);
315}
316
317static int
318eisa_read_ivar(device_t dev, device_t child, int which, u_long *result)
319{
320	struct eisa_device *e_dev = device_get_ivars(child);
321	struct irq_node *irq;
322
323	switch (which) {
324	case EISA_IVAR_SLOT:
325		*result = e_dev->ioconf.slot;
326		break;
327
328	case EISA_IVAR_ID:
329		*result = e_dev->id;
330		break;
331
332	case EISA_IVAR_IRQ:
333		/* XXX only first irq */
334		if ((irq = eisa_find_irq(e_dev, 0)) != NULL)
335			*result = irq->irq_no;
336		else
337			*result = -1;
338		break;
339
340	default:
341		return (ENOENT);
342	}
343
344	return (0);
345}
346
347static int
348eisa_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
349{
350	return (EINVAL);
351}
352
353static struct resource *
354eisa_alloc_resource(device_t dev, device_t child, int type, int *rid,
355    u_long start, u_long end, u_long count, u_int flags)
356{
357	int isdefault;
358	struct eisa_device *e_dev = device_get_ivars(child);
359	struct resource *rv, **rvp = 0;
360
361	isdefault = (device_get_parent(child) == dev &&
362	     start == 0UL && end == ~0UL && count == 1);
363
364	switch (type) {
365	case SYS_RES_IRQ:
366		if (isdefault) {
367			struct irq_node * irq = eisa_find_irq(e_dev, *rid);
368			if (irq == NULL)
369				return (NULL);
370			start = end = irq->irq_no;
371			count = 1;
372			if (irq->irq_trigger == EISA_TRIGGER_LEVEL)
373				flags |= RF_SHAREABLE;
374			else
375				flags &= ~RF_SHAREABLE;
376		}
377		break;
378
379	case SYS_RES_MEMORY:
380		if (isdefault) {
381			struct resvaddr *resv;
382
383			resv = eisa_find_maddr(e_dev, *rid);
384			if (resv == NULL)
385				return (NULL);
386
387			start = resv->addr;
388			end = resv->addr + (resv->size - 1);
389			count = resv->size;
390			rvp = &resv->res;
391		}
392		break;
393
394	case SYS_RES_IOPORT:
395		if (isdefault) {
396			struct resvaddr *resv;
397
398			resv = eisa_find_ioaddr(e_dev, *rid);
399			if (resv == NULL)
400				return (NULL);
401
402			start = resv->addr;
403			end = resv->addr + (resv->size - 1);
404			count = resv->size;
405			rvp = &resv->res;
406		}
407		break;
408
409	default:
410		return 0;
411	}
412
413	rv = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
414	     type, rid, start, end, count, flags);
415	if (rvp)
416		*rvp = rv;
417
418	return (rv);
419}
420
421static int
422eisa_release_resource(device_t dev, device_t child, int type, int rid,
423		      struct resource *r)
424{
425	int rv;
426	struct eisa_device *e_dev = device_get_ivars(child);
427	struct resvaddr *resv = 0;
428
429	switch (type) {
430	case SYS_RES_IRQ:
431		if (eisa_find_irq(e_dev, rid) == NULL)
432			return (EINVAL);
433		break;
434
435	case SYS_RES_MEMORY:
436		if (device_get_parent(child) == dev)
437			resv = eisa_find_maddr(e_dev, rid);
438		break;
439
440
441	case SYS_RES_IOPORT:
442		if (device_get_parent(child) == dev)
443			resv = eisa_find_ioaddr(e_dev, rid);
444		break;
445
446	default:
447		return (ENOENT);
448	}
449
450	rv = BUS_RELEASE_RESOURCE(device_get_parent(dev), child, type, rid, r);
451
452	if (rv == 0) {
453		if (resv != NULL)
454			resv->res = 0;
455	}
456
457	return (rv);
458}
459
460static int
461eisa_add_intr_m(device_t eisa, device_t dev, int irq, int trigger)
462{
463	struct eisa_device *e_dev = device_get_ivars(dev);
464	struct irq_node *irq_info;
465
466	irq_info = (struct irq_node *)malloc(sizeof(*irq_info), M_DEVBUF,
467					     M_NOWAIT);
468	if (irq_info == NULL)
469		return (1);
470
471	irq_info->irq_no = irq;
472	irq_info->irq_trigger = trigger;
473	irq_info->idesc = NULL;
474	TAILQ_INSERT_TAIL(&e_dev->ioconf.irqs, irq_info, links);
475	return (0);
476}
477
478static int
479eisa_add_resvaddr(struct eisa_device *e_dev, struct resvlist *head, u_long base,
480		  u_long size, int flags)
481{
482	resvaddr_t *reservation;
483
484	reservation = (resvaddr_t *)malloc(sizeof(resvaddr_t),
485					   M_DEVBUF, M_NOWAIT);
486	if(!reservation)
487		return (ENOMEM);
488
489	reservation->addr = base;
490	reservation->size = size;
491	reservation->flags = flags;
492
493	if (!LIST_FIRST(head)) {
494		LIST_INSERT_HEAD(head, reservation, links);
495	}
496	else {
497		resvaddr_t *node;
498		LIST_FOREACH(node, head, links) {
499			if (node->addr > reservation->addr) {
500				/*
501				 * List is sorted in increasing
502				 * address order.
503				 */
504				LIST_INSERT_BEFORE(node, reservation, links);
505				break;
506			}
507
508			if (node->addr == reservation->addr) {
509				/*
510				 * If the entry we want to add
511				 * matches any already in here,
512				 * fail.
513				 */
514				free(reservation, M_DEVBUF);
515				return (EEXIST);
516			}
517
518			if (!LIST_NEXT(node, links)) {
519				LIST_INSERT_AFTER(node, reservation, links);
520				break;
521			}
522		}
523	}
524	return (0);
525}
526
527static int
528eisa_add_mspace_m(device_t eisa, device_t dev, u_long mbase, u_long msize,
529    int flags)
530{
531	struct eisa_device *e_dev = device_get_ivars(dev);
532
533	return (eisa_add_resvaddr(e_dev, &(e_dev->ioconf.maddrs), mbase, msize,
534	    flags));
535}
536
537static int
538eisa_add_iospace_m(device_t eisa, device_t dev, u_long iobase, u_long iosize,
539    int flags)
540{
541	struct eisa_device *e_dev = device_get_ivars(dev);
542
543	return (eisa_add_resvaddr(e_dev, &(e_dev->ioconf.ioaddrs), iobase,
544	    iosize, flags));
545}
546
547static device_method_t eisa_methods[] = {
548	/* Device interface */
549	DEVMETHOD(device_probe,		eisa_probe),
550	DEVMETHOD(device_attach,	bus_generic_attach),
551	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
552	DEVMETHOD(device_suspend,	bus_generic_suspend),
553	DEVMETHOD(device_resume,	bus_generic_resume),
554
555	/* Bus interface */
556	DEVMETHOD(bus_print_child,	eisa_print_child),
557	DEVMETHOD(bus_probe_nomatch,	eisa_probe_nomatch),
558	DEVMETHOD(bus_read_ivar,	eisa_read_ivar),
559	DEVMETHOD(bus_write_ivar,	eisa_write_ivar),
560	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
561	DEVMETHOD(bus_alloc_resource,	eisa_alloc_resource),
562	DEVMETHOD(bus_release_resource,	eisa_release_resource),
563	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
564	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
565	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
566	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
567
568	/* EISA interface */
569	DEVMETHOD(eisa_add_intr,	eisa_add_intr_m),
570	DEVMETHOD(eisa_add_iospace,	eisa_add_iospace_m),
571	DEVMETHOD(eisa_add_mspace,	eisa_add_mspace_m),
572
573	{ 0, 0 }
574};
575
576static driver_t eisa_driver = {
577	"eisa",
578	eisa_methods,
579	1,			/* no softc */
580};
581
582DRIVER_MODULE(eisa, eisab, eisa_driver, eisa_devclass, 0, 0);
583DRIVER_MODULE(eisa, legacy, eisa_driver, eisa_devclass, 0, 0);
584