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