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