Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / dev / misc / puc / puc.c
1 /*      $NetBSD: puc.c,v 1.7 2000/07/29 17:43:38 jlam Exp $     */
2
3 /*-
4  * Copyright (c) 2002 JF Hay.  All rights reserved.
5  * Copyright (c) 2000 M. Warner Losh.  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 unmodified, this list of conditions, and the following
12  *    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  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 /*
30  * Copyright (c) 1996, 1998, 1999
31  *      Christopher G. Demetriou.  All rights reserved.
32  *
33  * Redistribution and use in source and binary forms, with or without
34  * modification, are permitted provided that the following conditions
35  * are met:
36  * 1. Redistributions of source code must retain the above copyright
37  *    notice, this list of conditions and the following disclaimer.
38  * 2. Redistributions in binary form must reproduce the above copyright
39  *    notice, this list of conditions and the following disclaimer in the
40  *    documentation and/or other materials provided with the distribution.
41  * 3. All advertising materials mentioning features or use of this software
42  *    must display the following acknowledgement:
43  *      This product includes software developed by Christopher G. Demetriou
44  *      for the NetBSD Project.
45  * 4. The name of the author may not be used to endorse or promote products
46  *    derived from this software without specific prior written permission
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
49  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
50  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
51  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
52  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
53  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
54  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
55  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
56  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
57  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58  */
59
60 #include <sys/cdefs.h>
61 __FBSDID("$FreeBSD: src/sys/dev/puc/puc.c,v 1.3.2.5 2003/04/04 08:42:17 sobomax Exp $");
62
63 /*
64  * PCI "universal" communication card device driver, glues com, lpt,
65  * and similar ports to PCI via bridge chip often much larger than
66  * the devices being glued.
67  *
68  * Author: Christopher G. Demetriou, May 14, 1998 (derived from NetBSD
69  * sys/dev/pci/pciide.c, revision 1.6).
70  *
71  * These devices could be (and some times are) described as
72  * communications/{serial,parallel}, etc. devices with known
73  * programming interfaces, but those programming interfaces (in
74  * particular the BAR assignments for devices, etc.) in fact are not
75  * particularly well defined.
76  *
77  * After I/we have seen more of these devices, it may be possible
78  * to generalize some of these bits.  In particular, devices which
79  * describe themselves as communications/serial/16[45]50, and
80  * communications/parallel/??? might be attached via direct
81  * 'com' and 'lpt' attachments to pci.
82  */
83
84 #include <sys/param.h>
85 #include <sys/systm.h>
86 #include <sys/kernel.h>
87 #include <sys/bus.h>
88 #include <sys/conf.h>
89 #include <sys/malloc.h>
90
91 #include <machine/bus.h>
92 #include <machine/resource.h>
93 #include <sys/rman.h>
94
95 #include <pci/pcireg.h>
96 #include <pci/pcivar.h>
97 #include <dev/puc/pucvar.h>
98
99 #include <opt_puc.h>
100
101 struct puc_softc {
102         const struct puc_device_description *sc_desc;
103
104         /* card-global dynamic data */
105         int                     barmuxed;
106         int                     irqrid;
107         struct resource         *irqres;
108         void                    *intr_cookie;
109         int                     ilr_enabled;
110         bus_space_tag_t         ilr_st;
111         bus_space_handle_t      ilr_sh;
112
113         struct {
114                 struct resource *res;
115         } sc_bar_mappings[PUC_MAX_BAR];
116
117         /* per-port dynamic data */
118         struct {
119                 struct device   *dev;
120                 /* filled in by bus_setup_intr() */
121                 void            (*ihand) __P((void *));
122                 void            *ihandarg;
123         } sc_ports[PUC_MAX_PORTS];
124 };
125
126 struct puc_device {
127         struct resource_list resources;
128         u_int serialfreq;
129 };
130
131 static int puc_pci_probe(device_t dev);
132 static int puc_pci_attach(device_t dev);
133 static void puc_intr(void *arg);
134
135 static struct resource *puc_alloc_resource(device_t, device_t, int, int *,
136     u_long, u_long, u_long, u_int);
137 static int puc_release_resource(device_t, device_t, int, int,
138     struct resource *);
139 static int puc_get_resource(device_t, device_t, int, int, u_long *, u_long *);
140 static int puc_setup_intr(device_t, device_t, struct resource *, int,
141     void (*)(void *), void *, void **);
142 static int puc_teardown_intr(device_t, device_t, struct resource *,
143     void *);
144 static int puc_read_ivar(device_t, device_t, int, uintptr_t *);
145
146 static const struct puc_device_description *puc_find_description(uint32_t,
147     uint32_t, uint32_t, uint32_t);
148 static void puc_config_superio(device_t);
149 static void puc_config_win877(struct resource *);
150 static int puc_find_free_unit(char *);
151 #ifdef PUC_DEBUG
152 static void puc_print_win877(bus_space_tag_t, bus_space_handle_t, u_int,
153     u_int);
154 static void puc_print_resource_list(struct resource_list *);
155 #endif
156
157 static int
158 puc_pci_probe(device_t dev)
159 {
160         uint32_t v1, v2, d1, d2;
161         const struct puc_device_description *desc;
162
163         if ((pci_read_config(dev, PCIR_HEADERTYPE, 1) & 0x7f) != 0)
164                 return (ENXIO);
165
166         v1 = pci_read_config(dev, PCIR_VENDOR, 2);
167         d1 = pci_read_config(dev, PCIR_DEVICE, 2);
168         v2 = pci_read_config(dev, PCIR_SUBVEND_0, 2);
169         d2 = pci_read_config(dev, PCIR_SUBDEV_0, 2);
170
171         desc = puc_find_description(v1, d1, v2, d2);
172         if (desc == NULL)
173                 return (ENXIO);
174         device_set_desc(dev, desc->name);
175         return (0);
176 }
177
178 static int
179 puc_probe_ilr(struct puc_softc *sc, struct resource *res)
180 {
181         u_char t1, t2;
182         int i;
183
184         switch (sc->sc_desc->ilr_type) {
185         case PUC_ILR_TYPE_DIGI:
186                 sc->ilr_st = rman_get_bustag(res);
187                 sc->ilr_sh = rman_get_bushandle(res);
188                 for (i = 0; i < 2; i++) {
189                         t1 = bus_space_read_1(sc->ilr_st, sc->ilr_sh,
190                             sc->sc_desc->ilr_offset[i]);
191                         t1 = ~t1;
192                         bus_space_write_1(sc->ilr_st, sc->ilr_sh,
193                             sc->sc_desc->ilr_offset[i], t1);
194                         t2 = bus_space_read_1(sc->ilr_st, sc->ilr_sh,
195                             sc->sc_desc->ilr_offset[i]);
196                         if (t2 == t1)
197                                 return (0);
198                 }
199                 return (1);
200
201         default:
202                 break;
203         }
204         return (0);
205 }
206
207 static int
208 puc_pci_attach(device_t dev)
209 {
210         char *typestr;
211         int bidx, childunit, i, irq_setup, rid;
212         uint32_t v1, v2, d1, d2;
213         struct puc_softc *sc;
214         struct puc_device *pdev;
215         struct resource *res;
216         struct resource_list_entry *rle;
217
218         sc = (struct puc_softc *)device_get_softc(dev);
219         bzero(sc, sizeof(*sc));
220         v1 = pci_read_config(dev, PCIR_VENDOR, 2);
221         d1 = pci_read_config(dev, PCIR_DEVICE, 2);
222         v2 = pci_read_config(dev, PCIR_SUBVEND_0, 2);
223         d2 = pci_read_config(dev, PCIR_SUBDEV_0, 2);
224         sc->sc_desc = puc_find_description(v1, d1, v2, d2);
225         if (sc->sc_desc == NULL)
226                 return (ENXIO);
227
228 #ifdef PUC_DEBUG
229         bootverbose = 1;
230
231         printf("puc: name: %s\n", sc->sc_desc->name);
232 #endif
233         rid = 0;
234         res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
235             RF_ACTIVE | RF_SHAREABLE);
236         if (!res)
237                 return (ENXIO);
238
239         sc->irqres = res;
240         sc->irqrid = rid;
241 #ifdef PUC_FASTINTR
242         irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res,
243             INTR_TYPE_TTY | INTR_TYPE_FAST, puc_intr, sc, &sc->intr_cookie);
244 #else
245         irq_setup = ENXIO;
246 #endif
247         if (irq_setup != 0)
248                 irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res,
249                     INTR_TYPE_TTY, puc_intr, sc, &sc->intr_cookie);
250         if (irq_setup != 0)
251                 return (ENXIO);
252
253         rid = 0;
254         for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
255                 if (rid == sc->sc_desc->ports[i].bar)
256                         sc->barmuxed = 1;
257                 rid = sc->sc_desc->ports[i].bar;
258                 bidx = PUC_PORT_BAR_INDEX(rid);
259
260                 if (sc->sc_bar_mappings[bidx].res != NULL)
261                         continue;
262                 res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
263                     0ul, ~0ul, 1, RF_ACTIVE);
264                 if (res == NULL) {
265                         printf("could not get resource\n");
266                         continue;
267                 }
268                 sc->sc_bar_mappings[bidx].res = res;
269
270                 if (sc->sc_desc->ilr_type != PUC_ILR_TYPE_NONE) {
271                         sc->ilr_enabled = puc_probe_ilr(sc, res);
272                         if (sc->ilr_enabled)
273                                 device_printf(dev, "ILR enabled\n");
274                         else
275                                 device_printf(dev, "ILR disabled\n");
276                 }
277 #ifdef PUC_DEBUG
278                 printf("port bst %x, start %x, end %x\n",
279                     (u_int)rman_get_bustag(res), (u_int)rman_get_start(res),
280                     (u_int)rman_get_end(res));
281 #endif
282         }
283
284         puc_config_superio(dev);
285
286         for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
287                 rid = sc->sc_desc->ports[i].bar;
288                 bidx = PUC_PORT_BAR_INDEX(rid);
289                 if (sc->sc_bar_mappings[bidx].res == NULL)
290                         continue;
291
292                 switch (sc->sc_desc->ports[i].type) {
293                 case PUC_PORT_TYPE_COM:
294                         typestr = "sio";
295                         break;
296                 default:
297                         continue;
298                 }
299                 pdev = malloc(sizeof(struct puc_device), M_DEVBUF,
300                     M_NOWAIT | M_ZERO);
301                 if (!pdev)
302                         continue;
303                 resource_list_init(&pdev->resources);
304
305                 /* First fake up an IRQ resource. */
306                 resource_list_add(&pdev->resources, SYS_RES_IRQ, 0,
307                     rman_get_start(sc->irqres), rman_get_end(sc->irqres),
308                     rman_get_end(sc->irqres) - rman_get_start(sc->irqres) + 1);
309                 rle = resource_list_find(&pdev->resources, SYS_RES_IRQ, 0);
310                 rle->res = sc->irqres;
311
312                 /* Now fake an IOPORT resource */
313                 res = sc->sc_bar_mappings[bidx].res;
314                 resource_list_add(&pdev->resources, SYS_RES_IOPORT, 0,
315                     rman_get_start(res) + sc->sc_desc->ports[i].offset,
316                     rman_get_end(res) + sc->sc_desc->ports[i].offset + 8 - 1,
317                     8);
318                 rle = resource_list_find(&pdev->resources, SYS_RES_IOPORT, 0);
319
320                 if (sc->barmuxed == 0) {
321                         rle->res = sc->sc_bar_mappings[bidx].res;
322                 } else {
323                         rle->res = malloc(sizeof(struct resource), M_DEVBUF,
324                             M_WAITOK | M_ZERO);
325                         if (rle->res == NULL) {
326                                 free(pdev, M_DEVBUF);
327                                 return (ENOMEM);
328                         }
329
330                         rle->res->r_start = rman_get_start(res) +
331                             sc->sc_desc->ports[i].offset;
332                         rle->res->r_end = rle->res->r_start + 8 - 1;
333                         rle->res->r_bustag = rman_get_bustag(res);
334                         bus_space_subregion(rle->res->r_bustag,
335                             rman_get_bushandle(res),
336                             sc->sc_desc->ports[i].offset, 8,
337                             &rle->res->r_bushandle);
338                 }
339
340                 pdev->serialfreq = sc->sc_desc->ports[i].serialfreq;
341
342                 childunit = puc_find_free_unit(typestr);
343                 sc->sc_ports[i].dev = device_add_child(dev, typestr, childunit);
344                 if (sc->sc_ports[i].dev == NULL) {
345                         if (sc->barmuxed) {
346                                 bus_space_unmap(rman_get_bustag(rle->res),
347                                                 rman_get_bushandle(rle->res),
348                                                 8);
349                                 free(rle->res, M_DEVBUF);
350                                 free(pdev, M_DEVBUF);
351                         }
352                         continue;
353                 }
354                 device_set_ivars(sc->sc_ports[i].dev, pdev);
355                 device_set_desc(sc->sc_ports[i].dev, sc->sc_desc->name);
356                 if (!bootverbose)
357                         device_quiet(sc->sc_ports[i].dev);
358 #ifdef PUC_DEBUG
359                 printf("puc: type %d, bar %x, offset %x\n",
360                     sc->sc_desc->ports[i].type,
361                     sc->sc_desc->ports[i].bar,
362                     sc->sc_desc->ports[i].offset);
363                 print_resource_list(&pdev->resources);
364 #endif
365                 device_set_flags(sc->sc_ports[i].dev,
366                     sc->sc_desc->ports[i].flags);
367                 if (device_probe_and_attach(sc->sc_ports[i].dev) != 0) {
368                         if (sc->barmuxed) {
369                                 bus_space_unmap(rman_get_bustag(rle->res),
370                                                 rman_get_bushandle(rle->res),
371                                                 8);
372                                 free(rle->res, M_DEVBUF);
373                                 free(pdev, M_DEVBUF);
374                         }
375                 }
376         }
377
378 #ifdef PUC_DEBUG
379         bootverbose = 0;
380 #endif
381         return (0);
382 }
383
384 static u_int32_t
385 puc_ilr_read(struct puc_softc *sc)
386 {
387         u_int32_t mask;
388         int i;
389
390         mask = 0;
391         switch (sc->sc_desc->ilr_type) {
392         case PUC_ILR_TYPE_DIGI:
393                 for (i = 1; i >= 0; i--) {
394                         mask = (mask << 8) | (bus_space_read_1(sc->ilr_st,
395                             sc->ilr_sh, sc->sc_desc->ilr_offset[i]) & 0xff);
396                 }
397                 break;
398
399         default:
400                 mask = 0xffffffff;
401                 break;
402         }
403         return (mask);
404 }
405
406 /*
407  * This is an interrupt handler. For boards that can't tell us which
408  * device generated the interrupt it just calls all the registered
409  * handlers sequencially, but for boards that can tell us which
410  * device(s) generated the interrupt it calls only handlers for devices
411  * that actually generated the interrupt.
412  */
413 static void
414 puc_intr(void *arg)
415 {
416         int i;
417         u_int32_t ilr_mask;
418         struct puc_softc *sc;
419
420         sc = (struct puc_softc *)arg;
421         ilr_mask = sc->ilr_enabled ? puc_ilr_read(sc) : 0xffffffff;
422         for (i = 0; i < PUC_MAX_PORTS; i++)
423                 if (sc->sc_ports[i].ihand != NULL &&
424                     ((ilr_mask >> i) & 0x00000001))
425                         (sc->sc_ports[i].ihand)(sc->sc_ports[i].ihandarg);
426 }
427
428 static const struct puc_device_description *
429 puc_find_description(uint32_t vend, uint32_t prod, uint32_t svend, 
430     uint32_t sprod)
431 {
432         int i;
433
434 #define checkreg(val, index) \
435     (((val) & puc_devices[i].rmask[(index)]) == puc_devices[i].rval[(index)])
436
437         for (i = 0; puc_devices[i].name != NULL; i++) {
438                 if (checkreg(vend, PUC_REG_VEND) &&
439                     checkreg(prod, PUC_REG_PROD) &&
440                     checkreg(svend, PUC_REG_SVEND) &&
441                     checkreg(sprod, PUC_REG_SPROD))
442                         return (&puc_devices[i]);
443         }
444
445 #undef checkreg
446
447         return (NULL);
448 }
449
450 /*
451  * It might be possible to make these more generic if we can detect patterns.
452  * For instance maybe if the size of a bar is 0x400 (the old isa space) it
453  * might contain one or more superio chips.
454  */
455 static void
456 puc_config_superio(device_t dev)
457 {
458         struct puc_softc *sc = (struct puc_softc *)device_get_softc(dev);
459
460         if (sc->sc_desc->rval[PUC_REG_VEND] == 0x1592 &&
461             sc->sc_desc->rval[PUC_REG_PROD] == 0x0781)
462                 puc_config_win877(sc->sc_bar_mappings[0].res);
463 }
464
465 #define rdspio(indx)            (bus_space_write_1(bst, bsh, efir, indx), \
466                                 bus_space_read_1(bst, bsh, efdr))
467 #define wrspio(indx,data)       (bus_space_write_1(bst, bsh, efir, indx), \
468                                 bus_space_write_1(bst, bsh, efdr, data))
469
470 #ifdef PUC_DEBUG
471 static void
472 puc_print_win877(bus_space_tag_t bst, bus_space_handle_t bsh, u_int efir,
473         u_int efdr)
474 {
475         u_char cr00, cr01, cr04, cr09, cr0d, cr14, cr15, cr16, cr17;
476         u_char cr18, cr19, cr24, cr25, cr28, cr2c, cr31, cr32;
477
478         cr00 = rdspio(0x00);
479         cr01 = rdspio(0x01);
480         cr04 = rdspio(0x04);
481         cr09 = rdspio(0x09);
482         cr0d = rdspio(0x0d);
483         cr14 = rdspio(0x14);
484         cr15 = rdspio(0x15);
485         cr16 = rdspio(0x16);
486         cr17 = rdspio(0x17);
487         cr18 = rdspio(0x18);
488         cr19 = rdspio(0x19);
489         cr24 = rdspio(0x24);
490         cr25 = rdspio(0x25);
491         cr28 = rdspio(0x28);
492         cr2c = rdspio(0x2c);
493         cr31 = rdspio(0x31);
494         cr32 = rdspio(0x32);
495         printf("877T: cr00 %x, cr01 %x, cr04 %x, cr09 %x, cr0d %x, cr14 %x, "
496             "cr15 %x, cr16 %x, cr17 %x, cr18 %x, cr19 %x, cr24 %x, cr25 %x, "
497             "cr28 %x, cr2c %x, cr31 %x, cr32 %x\n", cr00, cr01, cr04, cr09,
498             cr0d, cr14, cr15, cr16, cr17,
499             cr18, cr19, cr24, cr25, cr28, cr2c, cr31, cr32);
500 }
501 #endif
502
503 static void
504 puc_config_win877(struct resource *res)
505 {
506         u_char val;
507         u_int efir, efdr;
508         bus_space_tag_t bst;
509         bus_space_handle_t bsh;
510
511         bst = rman_get_bustag(res);
512         bsh = rman_get_bushandle(res);
513
514         /* configure the first W83877TF */
515         bus_space_write_1(bst, bsh, 0x250, 0x89);
516         efir = 0x251;
517         efdr = 0x252;
518         val = rdspio(0x09) & 0x0f;
519         if (val != 0x0c) {
520                 printf("conf_win877: Oops not a W83877TF\n");
521                 return;
522         }
523
524 #ifdef PUC_DEBUG
525         printf("before: ");
526         puc_print_win877(bst, bsh, efir, efdr);
527 #endif
528
529         val = rdspio(0x16);
530         val |= 0x04;
531         wrspio(0x16, val);
532         val &= ~0x04;
533         wrspio(0x16, val);
534
535         wrspio(0x24, 0x2e8 >> 2);
536         wrspio(0x25, 0x2f8 >> 2);
537         wrspio(0x17, 0x03);
538         wrspio(0x28, 0x43);
539
540 #ifdef PUC_DEBUG
541         printf("after: ");
542         puc_print_win877(bst, bsh, efir, efdr);
543 #endif
544
545         bus_space_write_1(bst, bsh, 0x250, 0xaa);
546
547         /* configure the second W83877TF */
548         bus_space_write_1(bst, bsh, 0x3f0, 0x87);
549         bus_space_write_1(bst, bsh, 0x3f0, 0x87);
550         efir = 0x3f0;
551         efdr = 0x3f1;
552         val = rdspio(0x09) & 0x0f;
553         if (val != 0x0c) {
554                 printf("conf_win877: Oops not a W83877TF\n");
555                 return;
556         }
557
558 #ifdef PUC_DEBUG
559         printf("before: ");
560         puc_print_win877(bst, bsh, efir, efdr);
561 #endif
562
563         val = rdspio(0x16);
564         val |= 0x04;
565         wrspio(0x16, val);
566         val &= ~0x04;
567         wrspio(0x16, val);
568
569         wrspio(0x24, 0x3e8 >> 2);
570         wrspio(0x25, 0x3f8 >> 2);
571         wrspio(0x17, 0x03);
572         wrspio(0x28, 0x43);
573
574 #ifdef PUC_DEBUG
575         printf("after: ");
576         puc_print_win877(bst, bsh, efir, efdr);
577 #endif
578
579         bus_space_write_1(bst, bsh, 0x3f0, 0xaa);
580 }
581
582 #undef rdspio
583 #undef wrspio
584
585 static int puc_find_free_unit(char *name)
586 {
587         devclass_t dc;
588         int start;
589         int unit;
590
591         unit = 0;
592         start = 0;
593         while (resource_int_value(name, unit, "port", &start) == 0 && 
594             start > 0)
595                 unit++;
596         dc = devclass_find(name);
597         if (dc == NULL)
598                 return (-1);
599         while (devclass_get_device(dc, unit))
600                 unit++;
601 #ifdef PUC_DEBUG
602         printf("puc: Using %s%d\n", name, unit);
603 #endif
604         return (unit);
605 }
606
607 #ifdef PUC_DEBUG
608 static void
609 puc_print_resource_list(struct resource_list *rl)
610 {
611         struct resource_list_entry *rle;
612
613         printf("print_resource_list: rl %p\n", rl);
614         SLIST_FOREACH(rle, rl, link)
615                 printf("type %x, rid %x\n", rle->type, rle->rid);
616         printf("print_resource_list: end.\n");
617 }
618 #endif
619
620 static struct resource *
621 puc_alloc_resource(device_t dev, device_t child, int type, int *rid,
622     u_long start, u_long end, u_long count, u_int flags)
623 {
624         struct puc_device *pdev;
625         struct resource *retval;
626         struct resource_list *rl;
627         struct resource_list_entry *rle;
628
629         pdev = device_get_ivars(child);
630         rl = &pdev->resources;
631
632 #ifdef PUC_DEBUG
633         printf("puc_alloc_resource: pdev %p, looking for t %x, r %x\n",
634             pdev, type, *rid);
635         puc_print_resource_list(rl);
636 #endif
637         retval = NULL;
638         rle = resource_list_find(rl, type, *rid);
639         if (rle) {
640                 start = rle->start;
641                 end = rle->end;
642                 count = rle->count;
643 #ifdef PUC_DEBUG
644                 printf("found rle, %lx, %lx, %lx\n", start, end, count);
645 #endif
646                 retval = rle->res;
647         } else
648                 printf("oops rle is gone\n");
649
650         return (retval);
651 }
652
653 static int
654 puc_release_resource(device_t dev, device_t child, int type, int rid,
655     struct resource *res)
656 {
657         return (0);
658 }
659
660 static int
661 puc_get_resource(device_t dev, device_t child, int type, int rid,
662     u_long *startp, u_long *countp)
663 {
664         struct puc_device *pdev;
665         struct resource_list *rl;
666         struct resource_list_entry *rle;
667
668         pdev = device_get_ivars(child);
669         rl = &pdev->resources;
670
671 #ifdef PUC_DEBUG
672         printf("puc_get_resource: pdev %p, looking for t %x, r %x\n", pdev,
673             type, rid);
674         puc_print_resource_list(rl);
675 #endif
676         rle = resource_list_find(rl, type, rid);
677         if (rle) {
678 #ifdef PUC_DEBUG
679                 printf("found rle %p,", rle);
680 #endif
681                 if (startp != NULL)
682                         *startp = rle->start;
683                 if (countp != NULL)
684                         *countp = rle->count;
685 #ifdef PUC_DEBUG
686                 printf(" %lx, %lx\n", rle->start, rle->count);
687 #endif
688                 return (0);
689         } else
690                 printf("oops rle is gone\n");
691         return (ENXIO);
692 }
693
694 static int
695 puc_setup_intr(device_t dev, device_t child, struct resource *r, int flags,
696                void (*ihand)(void *), void *arg, void **cookiep)
697 {
698         int i;
699         struct puc_softc *sc;
700
701         sc = (struct puc_softc *)device_get_softc(dev);
702         for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
703                 if (sc->sc_ports[i].dev == child) {
704                         if (sc->sc_ports[i].ihand != 0)
705                                 return (ENXIO);
706                         sc->sc_ports[i].ihand = ihand;
707                         sc->sc_ports[i].ihandarg = arg;
708                         *cookiep = arg;
709                         return (0);
710                 }
711         }
712         return (ENXIO);
713 }
714
715 static int
716 puc_teardown_intr(device_t dev, device_t child, struct resource *r,
717                   void *cookie)
718 {
719         int i;
720         struct puc_softc *sc;
721
722         sc = (struct puc_softc *)device_get_softc(dev);
723         for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
724                 if (sc->sc_ports[i].dev == child) {
725                         sc->sc_ports[i].ihand = NULL;
726                         sc->sc_ports[i].ihandarg = NULL;
727                         return (0);
728                 }
729         }
730         return (ENXIO);
731 }
732
733 static int
734 puc_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
735 {
736         struct puc_device *pdev;
737
738         pdev = device_get_ivars(child);
739         if (pdev == NULL)
740                 return (ENOENT);
741
742         switch(index) {
743         case PUC_IVAR_FREQ:
744                 *result = pdev->serialfreq;
745                 break;
746         default:
747                 return (ENOENT);
748         }
749         return (0);
750 }
751
752 static device_method_t puc_pci_methods[] = {
753     /* Device interface */
754     DEVMETHOD(device_probe,             puc_pci_probe),
755     DEVMETHOD(device_attach,            puc_pci_attach),
756
757     DEVMETHOD(bus_alloc_resource,       puc_alloc_resource),
758     DEVMETHOD(bus_release_resource,     puc_release_resource),
759     DEVMETHOD(bus_get_resource,         puc_get_resource),
760     DEVMETHOD(bus_read_ivar,            puc_read_ivar),
761     DEVMETHOD(bus_setup_intr,           puc_setup_intr),
762     DEVMETHOD(bus_teardown_intr,        puc_teardown_intr),
763     DEVMETHOD(bus_print_child,          bus_generic_print_child),
764     DEVMETHOD(bus_driver_added,         bus_generic_driver_added),
765     { 0, 0 }
766 };
767
768 static driver_t puc_pci_driver = {
769         "puc",
770         puc_pci_methods,
771         sizeof(struct puc_softc),
772 };
773
774 static devclass_t puc_devclass;
775
776 DRIVER_MODULE(puc, pci, puc_pci_driver, puc_devclass, 0, 0);