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