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