Deleted Added
sdiff udiff text old ( 48100 ) new ( 48754 )
full compact
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.46 1999/06/22 09:44:00 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%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 void
216eisa_print_child(device_t dev, device_t child)
217{
218 /* XXX print resource descriptions? */
219 printf(" at slot %d", eisa_get_slot(child));
220 printf(" on %s", device_get_nameunit(dev));
221}
222
223static int
224eisa_find_irq(struct eisa_device *e_dev, int rid)
225{
226 int i;
227 struct irq_node *irq;
228
229 for (i = 0, irq = TAILQ_FIRST(&e_dev->ioconf.irqs);
230 i < rid && irq;
231 i++, irq = TAILQ_NEXT(irq, links))
232 ;
233
234 if (irq)
235 return irq->irq_no;
236 else
237 return -1;
238}
239
240static struct resvaddr *
241eisa_find_maddr(struct eisa_device *e_dev, int rid)
242{
243 int i;
244 struct resvaddr *resv;
245
246 for (i = 0, resv = LIST_FIRST(&e_dev->ioconf.maddrs);
247 i < rid && resv;
248 i++, resv = LIST_NEXT(resv, links))
249 ;
250
251 return resv;
252}
253
254static struct resvaddr *
255eisa_find_ioaddr(struct eisa_device *e_dev, int rid)
256{
257 int i;
258 struct resvaddr *resv;
259
260 for (i = 0, resv = LIST_FIRST(&e_dev->ioconf.ioaddrs);
261 i < rid && resv;
262 i++, resv = LIST_NEXT(resv, links))
263 ;
264
265 return resv;
266}
267
268static int
269eisa_read_ivar(device_t dev, device_t child, int which, u_long *result)
270{
271 struct eisa_device *e_dev = device_get_ivars(child);
272
273 switch (which) {
274 case EISA_IVAR_SLOT:
275 *result = e_dev->ioconf.slot;
276 break;
277
278 case EISA_IVAR_ID:
279 *result = e_dev->id;
280 break;
281
282 case EISA_IVAR_IRQ:
283 /* XXX only first irq */
284 *result = eisa_find_irq(e_dev, 0);
285 break;
286
287 default:
288 return (ENOENT);
289 }
290
291 return (0);
292}
293
294static int
295eisa_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
296{
297 return (EINVAL);
298}
299
300static struct resource *
301eisa_alloc_resource(device_t dev, device_t child, int type, int *rid,
302 u_long start, u_long end, u_long count, u_int flags)
303{
304 int isdefault;
305 struct eisa_device *e_dev = device_get_ivars(child);
306 struct resource *rv, **rvp = 0;
307
308 isdefault = (device_get_parent(child) == dev
309 && start == 0UL && end == ~0UL && count == 1);
310
311 switch (type) {
312 case SYS_RES_IRQ:
313 if (isdefault) {
314 int irq = eisa_find_irq(e_dev, *rid);
315 if (irq == -1)
316 return 0;
317 start = end = irq;
318 count = 1;
319 }
320 break;
321
322 case SYS_RES_MEMORY:
323 if (isdefault) {
324 struct resvaddr *resv;
325
326 resv = eisa_find_maddr(e_dev, *rid);
327 if (!resv)
328 return 0;
329
330 start = resv->addr;
331 end = resv->addr + (resv->size - 1);
332 count = resv->size;
333 rvp = &resv->res;
334 }
335 break;
336
337 case SYS_RES_IOPORT:
338 if (isdefault) {
339 struct resvaddr *resv;
340
341 resv = eisa_find_ioaddr(e_dev, *rid);
342 if (!resv)
343 return 0;
344
345 start = resv->addr;
346 end = resv->addr + (resv->size - 1);
347 count = resv->size;
348 rvp = &resv->res;
349 }
350 break;
351
352 default:
353 return 0;
354 }
355
356 rv = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
357 type, rid, start, end, count, flags);
358 if (rvp)
359 *rvp = rv;
360
361 return rv;
362}
363
364static int
365eisa_release_resource(device_t dev, device_t child, int type, int rid,
366 struct resource *r)
367{
368 int rv;
369 struct eisa_device *e_dev = device_get_ivars(child);
370 struct resvaddr *resv = 0;
371
372 switch (type) {
373 case SYS_RES_IRQ:
374 if (eisa_find_irq(e_dev, rid) == -1)
375 return EINVAL;
376 break;
377
378 case SYS_RES_MEMORY:
379 if (device_get_parent(child) == dev)
380 resv = eisa_find_maddr(e_dev, rid);
381 break;
382
383
384 case SYS_RES_IOPORT:
385 if (device_get_parent(child) == dev)
386 resv = eisa_find_ioaddr(e_dev, rid);
387 break;
388
389 default:
390 return (ENOENT);
391 }
392
393 rv = BUS_RELEASE_RESOURCE(device_get_parent(dev), child, type, rid, r);
394
395 if (rv == 0) {
396 if (resv)
397 resv->res = 0;
398 }
399
400 return rv;
401}
402
403int
404eisa_add_intr(device_t dev, int irq)
405{
406 struct eisa_device *e_dev = device_get_ivars(dev);
407 struct irq_node *irq_info;
408
409 irq_info = (struct irq_node *)malloc(sizeof(*irq_info), M_DEVBUF,
410 M_NOWAIT);
411 if (irq_info == NULL)
412 return (1);
413
414 irq_info->irq_no = irq;
415 irq_info->idesc = NULL;
416 TAILQ_INSERT_TAIL(&e_dev->ioconf.irqs, irq_info, links);
417 return 0;
418}
419
420static int
421eisa_add_resvaddr(struct eisa_device *e_dev, struct resvlist *head, u_long base,
422 u_long size, int flags)
423{
424 resvaddr_t *reservation;
425
426 reservation = (resvaddr_t *)malloc(sizeof(resvaddr_t),
427 M_DEVBUF, M_NOWAIT);
428 if(!reservation)
429 return (ENOMEM);
430
431 reservation->addr = base;
432 reservation->size = size;
433 reservation->flags = flags;
434
435 if (!head->lh_first) {
436 LIST_INSERT_HEAD(head, reservation, links);
437 }
438 else {
439 resvaddr_t *node;
440 for(node = head->lh_first; node; node = node->links.le_next) {
441 if (node->addr > reservation->addr) {
442 /*
443 * List is sorted in increasing
444 * address order.
445 */
446 LIST_INSERT_BEFORE(node, reservation, links);
447 break;
448 }
449
450 if (node->addr == reservation->addr) {
451 /*
452 * If the entry we want to add
453 * matches any already in here,
454 * fail.
455 */
456 free(reservation, M_DEVBUF);
457 return (EEXIST);
458 }
459
460 if (!node->links.le_next) {
461 LIST_INSERT_AFTER(node, reservation, links);
462 break;
463 }
464 }
465 }
466 return (0);
467}
468
469int
470eisa_add_mspace(device_t dev, u_long mbase, u_long msize, int flags)
471{
472 struct eisa_device *e_dev = device_get_ivars(dev);
473
474 return eisa_add_resvaddr(e_dev, &(e_dev->ioconf.maddrs), mbase, msize,
475 flags);
476}
477
478int
479eisa_add_iospace(device_t dev, u_long iobase, u_long iosize, int flags)
480{
481 struct eisa_device *e_dev = device_get_ivars(dev);
482
483 return eisa_add_resvaddr(e_dev, &(e_dev->ioconf.ioaddrs), iobase,
484 iosize, flags);
485}
486
487static device_method_t eisa_methods[] = {
488 /* Device interface */
489 DEVMETHOD(device_probe, eisa_probe),
490 DEVMETHOD(device_attach, bus_generic_attach),
491 DEVMETHOD(device_shutdown, bus_generic_shutdown),
492 DEVMETHOD(device_suspend, bus_generic_suspend),
493 DEVMETHOD(device_resume, bus_generic_resume),
494
495 /* Bus interface */
496 DEVMETHOD(bus_print_child, eisa_print_child),
497 DEVMETHOD(bus_probe_nomatch, eisa_probe_nomatch),
498 DEVMETHOD(bus_read_ivar, eisa_read_ivar),
499 DEVMETHOD(bus_write_ivar, eisa_write_ivar),
500 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
501 DEVMETHOD(bus_alloc_resource, eisa_alloc_resource),
502 DEVMETHOD(bus_release_resource, eisa_release_resource),
503 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
504 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
505 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
506 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
507
508 { 0, 0 }
509};
510
511static driver_t eisa_driver = {
512 "eisa",
513 eisa_methods,
514 1, /* no softc */
515};
516
517DRIVER_MODULE(eisa, isab, eisa_driver, eisa_devclass, 0, 0);
518DRIVER_MODULE(eisa, nexus, eisa_driver, eisa_devclass, 0, 0);