Now that I understand the poorly written BSD routing code and what
[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.5 2004/11/14 15:20:05 joerg 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                         if (slot == 0)
169                                 break;
170                         continue;  /* no EISA card in slot */
171                 }
172
173                 devices_found++;
174
175                 /* Prepare an eisa_device_node for this slot */
176                 e_dev = malloc(sizeof(*e_dev), M_DEVBUF, M_INTWAIT | M_ZERO);
177
178                 e_dev->id = eisa_id;
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                 child = device_add_child(dev, NULL, -1);
187                 device_set_ivars(child, e_dev);
188         }
189
190         /*
191          * EISA busses themselves are not easily detectable, the easiest way
192          * to tell if there is an eisa bus is if we found something - there
193          * should be a motherboard "card" there somewhere.
194          */
195         return devices_found ? 0 : ENXIO;
196 }
197
198 static void
199 eisa_probe_nomatch(device_t dev, device_t child)
200 {
201         u_int32_t       eisa_id = eisa_get_id(child);
202         u_int8_t        slot = eisa_get_slot(child);
203
204         device_printf(dev, "unknown card %c%c%c%03x%01x (0x%08x) at slot %d\n",
205                 EISA_MFCTR_CHAR0(eisa_id),
206                 EISA_MFCTR_CHAR1(eisa_id),
207                 EISA_MFCTR_CHAR2(eisa_id),
208                 EISA_PRODUCT_ID(eisa_id),
209                 EISA_REVISION_ID(eisa_id),
210                 eisa_id,
211                 slot);
212
213         return;
214 }
215
216 static void
217 eisa_reg_print (dev, string, separator, column)
218         device_t        dev;
219         char *          string;
220         char *          separator;
221         int *           column;
222 {
223         int length = strlen(string);
224
225         length += (separator ? 2 : 1);
226
227         if (((*column) + length) >= MAX_COL) {
228                 printf("\n");
229                 (*column) = 0;
230         } else if ((*column) != 0) {
231                 if (separator) {
232                         printf("%c", *separator);
233                         (*column)++;
234                 }
235                 printf(" ");
236                 (*column)++;
237         }
238
239         if ((*column) == 0) {
240                 (*column) += device_printf(dev, "%s", string);
241         } else {
242                 (*column) += printf("%s", string);
243         }
244
245         return;
246 }
247
248 static int
249 eisa_print_child(device_t dev, device_t child)
250 {
251         char                    buf[81];
252         struct eisa_device *    e_dev = device_get_ivars(child);
253         int                     rid;
254         struct irq_node *       irq;
255         struct resvaddr *       resv;
256         char                    separator = ',';
257         int                     column = 0;
258         int                     retval = 0;
259
260         if (device_get_desc(child)) {
261                 snprintf(buf, sizeof(buf), "<%s>", device_get_desc(child));
262                 eisa_reg_print(child, buf, NULL, &column);
263         }
264
265         rid = 0;
266         while ((resv = eisa_find_ioaddr(e_dev, rid++))) {
267                 if ((resv->size == 1) ||
268                     (resv->flags & RESVADDR_BITMASK)) {
269                         snprintf(buf, sizeof(buf), "%s%lx",
270                                 ((rid == 1) ? "at 0x" : "0x"),
271                                 resv->addr);
272                 } else {
273                         snprintf(buf, sizeof(buf), "%s%lx-0x%lx",
274                                 ((rid == 1) ? "at 0x" : "0x"),
275                                 resv->addr,
276                                 (resv->addr + (resv->size - 1)));
277                 }
278                 eisa_reg_print(child, buf, 
279                         ((rid == 2) ? &separator : NULL), &column);
280         }
281
282         rid = 0;
283         while ((resv = eisa_find_maddr(e_dev, rid++))) {
284                 if ((resv->size == 1) ||
285                     (resv->flags & RESVADDR_BITMASK)) {
286                         snprintf(buf, sizeof(buf), "%s%lx",
287                                 ((rid == 1) ? "at 0x" : "0x"),
288                                 resv->addr);
289                 } else {
290                         snprintf(buf, sizeof(buf), "%s%lx-0x%lx",
291                                 ((rid == 1) ? "at 0x" : "0x"),
292                                 resv->addr,
293                                 (resv->addr + (resv->size - 1)));
294                 }
295                 eisa_reg_print(child, buf, 
296                         ((rid == 2) ? &separator : NULL), &column);
297         }
298
299         rid = 0;
300         while ((irq = eisa_find_irq(e_dev, rid++)) != NULL) {
301                 snprintf(buf, sizeof(buf), "irq %d (%s)", irq->irq_no,
302                          (irq->irq_trigger ? "level" : "edge"));
303                 eisa_reg_print(child, buf, 
304                         ((rid == 1) ? &separator : NULL), &column);
305         }
306
307         snprintf(buf, sizeof(buf), "on %s slot %d\n",
308                 device_get_nameunit(dev), eisa_get_slot(child));
309         eisa_reg_print(child, buf, NULL, &column);
310
311         return (retval);
312 }
313
314 static struct irq_node *
315 eisa_find_irq(struct eisa_device *e_dev, int rid)
316 {
317         int i;
318         struct irq_node *irq;
319
320         for (i = 0, irq = TAILQ_FIRST(&e_dev->ioconf.irqs);
321              i < rid && irq;
322              i++, irq = TAILQ_NEXT(irq, links))
323                 ;
324         
325         if (irq)
326                 return (irq);
327         else
328                 return (NULL);
329 }
330
331 static struct resvaddr *
332 eisa_find_maddr(struct eisa_device *e_dev, int rid)
333 {
334         int i;
335         struct resvaddr *resv;
336
337         for (i = 0, resv = LIST_FIRST(&e_dev->ioconf.maddrs);
338              i < rid && resv;
339              i++, resv = LIST_NEXT(resv, links))
340                 ;
341
342         return resv;
343 }
344
345 static struct resvaddr *
346 eisa_find_ioaddr(struct eisa_device *e_dev, int rid)
347 {
348         int i;
349         struct resvaddr *resv;
350
351         for (i = 0, resv = LIST_FIRST(&e_dev->ioconf.ioaddrs);
352              i < rid && resv;
353              i++, resv = LIST_NEXT(resv, links))
354                 ;
355
356         return resv;
357 }
358
359 static int
360 eisa_read_ivar(device_t dev, device_t child, int which, u_long *result)
361 {
362         struct eisa_device *e_dev = device_get_ivars(child);
363         struct irq_node *irq;
364
365         switch (which) {
366         case EISA_IVAR_SLOT:
367                 *result = e_dev->ioconf.slot;
368                 break;
369                 
370         case EISA_IVAR_ID:
371                 *result = e_dev->id;
372                 break;
373
374         case EISA_IVAR_IRQ:
375                 /* XXX only first irq */
376                 if ((irq = eisa_find_irq(e_dev, 0)) != NULL) {
377                         *result = irq->irq_no;
378                 } else {
379                         *result = -1;
380                 }
381                 break;
382
383         default:
384                 return (ENOENT);
385         }
386
387         return (0);
388 }
389
390 static int
391 eisa_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
392 {
393         return (EINVAL);
394 }
395
396 static struct resource *
397 eisa_alloc_resource(device_t dev, device_t child, int type, int *rid,
398                     u_long start, u_long end, u_long count, u_int flags)
399 {
400         int isdefault;
401         struct eisa_device *e_dev = device_get_ivars(child);
402         struct resource *rv, **rvp = 0;
403
404         isdefault = (device_get_parent(child) == dev
405                      && start == 0UL && end == ~0UL && count == 1);
406
407         switch (type) {
408         case SYS_RES_IRQ:
409                 if (isdefault) {
410                         struct irq_node * irq = eisa_find_irq(e_dev, *rid);
411                         if (irq == NULL)
412                                 return 0;
413                         start = end = irq->irq_no;
414                         count = 1;
415                         if (irq->irq_trigger == EISA_TRIGGER_LEVEL) {
416                                 flags |= RF_SHAREABLE;
417                         } else {
418                                 flags &= ~RF_SHAREABLE;
419                         }
420                 }
421                 break;
422
423         case SYS_RES_MEMORY:
424                 if (isdefault) {
425                         struct resvaddr *resv;
426
427                         resv = eisa_find_maddr(e_dev, *rid);
428                         if (!resv)
429                                 return 0;
430
431                         start = resv->addr;
432                         end = resv->addr + (resv->size - 1);
433                         count = resv->size;
434                         rvp = &resv->res;
435                 }
436                 break;
437
438         case SYS_RES_IOPORT:
439                 if (isdefault) {
440                         struct resvaddr *resv;
441
442                         resv = eisa_find_ioaddr(e_dev, *rid);
443                         if (!resv)
444                                 return 0;
445
446                         start = resv->addr;
447                         end = resv->addr + (resv->size - 1);
448                         count = resv->size;
449                         rvp = &resv->res;
450                 }
451                 break;
452
453         default:
454                 return 0;
455         }
456
457         rv = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
458                                  type, rid, start, end, count, flags);
459         if (rvp)
460                 *rvp = rv;
461
462         return rv;
463 }
464
465 static int
466 eisa_release_resource(device_t dev, device_t child, int type, int rid,
467                       struct resource *r)
468 {
469         int rv;
470         struct eisa_device *e_dev = device_get_ivars(child);
471         struct resvaddr *resv = 0;
472
473         switch (type) {
474         case SYS_RES_IRQ:
475                 if (eisa_find_irq(e_dev, rid) == NULL)
476                         return EINVAL;
477                 break;
478
479         case SYS_RES_MEMORY:
480                 if (device_get_parent(child) == dev)
481                         resv = eisa_find_maddr(e_dev, rid);
482                 break;
483
484
485         case SYS_RES_IOPORT:
486                 if (device_get_parent(child) == dev)
487                         resv = eisa_find_ioaddr(e_dev, rid);
488                 break;
489
490         default:
491                 return (ENOENT);
492         }
493
494         rv = BUS_RELEASE_RESOURCE(device_get_parent(dev), child, type, rid, r);
495
496         if (rv == 0) {
497                 if (resv)
498                         resv->res = 0;
499         }
500
501         return rv;
502 }
503
504 int
505 eisa_add_intr(device_t dev, int irq, int trigger)
506 {
507         struct eisa_device *e_dev = device_get_ivars(dev);
508         struct irq_node *irq_info;
509  
510         irq_info = malloc(sizeof(*irq_info), M_DEVBUF, M_INTWAIT);
511         irq_info->irq_no = irq;
512         irq_info->irq_trigger = trigger;
513         irq_info->idesc = NULL;
514         TAILQ_INSERT_TAIL(&e_dev->ioconf.irqs, irq_info, links);
515         return 0;
516 }
517
518 static int
519 eisa_add_resvaddr(struct eisa_device *e_dev, struct resvlist *head, u_long base,
520                   u_long size, int flags)
521 {
522         resvaddr_t *reservation;
523
524         reservation = malloc(sizeof(resvaddr_t), M_DEVBUF, M_INTWAIT);
525         reservation->addr = base;
526         reservation->size = size;
527         reservation->flags = flags;
528
529         if (!head->lh_first) {
530                 LIST_INSERT_HEAD(head, reservation, links);
531         }
532         else {
533                 resvaddr_t *node;
534                 for(node = head->lh_first; node; node = node->links.le_next) {
535                         if (node->addr > reservation->addr) {
536                                 /*
537                                  * List is sorted in increasing
538                                  * address order.
539                                  */
540                                 LIST_INSERT_BEFORE(node, reservation, links);
541                                 break;
542                         }
543
544                         if (node->addr == reservation->addr) {
545                                 /*
546                                  * If the entry we want to add
547                                  * matches any already in here,
548                                  * fail.
549                                  */
550                                 free(reservation, M_DEVBUF);
551                                 return (EEXIST);
552                         }
553
554                         if (!node->links.le_next) {
555                                 LIST_INSERT_AFTER(node, reservation, links);
556                                 break;
557                         }
558                 }
559         }
560         return (0);
561 }
562
563 int
564 eisa_add_mspace(device_t dev, u_long mbase, u_long msize, int flags)
565 {
566         struct eisa_device *e_dev = device_get_ivars(dev);
567
568         return  eisa_add_resvaddr(e_dev, &(e_dev->ioconf.maddrs), mbase, msize,
569                                   flags);
570 }
571
572 int
573 eisa_add_iospace(device_t dev, u_long iobase, u_long iosize, int flags)
574 {
575         struct eisa_device *e_dev = device_get_ivars(dev);
576
577         return  eisa_add_resvaddr(e_dev, &(e_dev->ioconf.ioaddrs), iobase,
578                                   iosize, flags);
579 }
580
581 static device_method_t eisa_methods[] = {
582         /* Device interface */
583         DEVMETHOD(device_probe,         eisa_probe),
584         DEVMETHOD(device_attach,        bus_generic_attach),
585         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
586         DEVMETHOD(device_suspend,       bus_generic_suspend),
587         DEVMETHOD(device_resume,        bus_generic_resume),
588
589         /* Bus interface */
590         DEVMETHOD(bus_print_child,      eisa_print_child),
591         DEVMETHOD(bus_probe_nomatch,    eisa_probe_nomatch),
592         DEVMETHOD(bus_read_ivar,        eisa_read_ivar),
593         DEVMETHOD(bus_write_ivar,       eisa_write_ivar),
594         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
595         DEVMETHOD(bus_alloc_resource,   eisa_alloc_resource),
596         DEVMETHOD(bus_release_resource, eisa_release_resource),
597         DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
598         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
599         DEVMETHOD(bus_setup_intr,       bus_generic_setup_intr),
600         DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),
601
602         { 0, 0 }
603 };
604
605 static driver_t eisa_driver = {
606         "eisa",
607         eisa_methods,
608         1,                      /* no softc */
609 };
610
611 DRIVER_MODULE(eisa, isab, eisa_driver, eisa_devclass, 0, 0);
612 DRIVER_MODULE(eisa, nexus, eisa_driver, eisa_devclass, 0, 0);