2 * $NetBSD: puc.c,v 1.7 2000/07/29 17:43:38 jlam Exp $
3 * $FreeBSD: src/sys/dev/puc/puc.c,v 1.3.2.5 2003/04/04 08:42:17 sobomax Exp $
7 * Copyright (c) 2002 JF Hay. All rights reserved.
8 * Copyright (c) 2000 M. Warner Losh. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice unmodified, this list of conditions, and the following
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (c) 1996, 1998, 1999
34 * Christopher G. Demetriou. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by Christopher G. Demetriou
47 * for the NetBSD Project.
48 * 4. The name of the author may not be used to endorse or promote products
49 * derived from this software without specific prior written permission
51 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
52 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
53 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
54 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
55 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
56 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
60 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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.
68 * Author: Christopher G. Demetriou, May 14, 1998 (derived from NetBSD
69 * sys/dev/pci/pciide.c, revision 1.6).
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.
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.
84 #include <sys/param.h>
85 #include <sys/systm.h>
86 #include <sys/kernel.h>
89 #include <sys/malloc.h>
92 #include <bus/pci/pcireg.h>
93 #include <bus/pci/pcivar.h>
97 const struct puc_device_description *sc_desc;
99 /* card-global dynamic data */
102 struct resource *irqres;
105 bus_space_tag_t ilr_st;
106 bus_space_handle_t ilr_sh;
109 struct resource *res;
110 } sc_bar_mappings[PUC_MAX_BAR];
112 /* per-port dynamic data */
115 /* filled in by bus_setup_intr() */
116 void (*ihand) (void *);
118 } sc_ports[PUC_MAX_PORTS];
122 struct resource_list resources;
126 static int puc_pci_probe(device_t dev);
127 static int puc_pci_attach(device_t dev);
128 static void puc_intr(void *arg);
130 static struct resource *puc_alloc_resource(device_t, device_t, int, int *,
131 u_long, u_long, u_long, u_int, int);
132 static int puc_release_resource(device_t, device_t, int, int,
134 static int puc_get_resource(device_t, device_t, int, int, u_long *, u_long *);
135 static int puc_setup_intr(device_t, device_t, struct resource *, int,
136 void (*)(void *), void *, void **, lwkt_serialize_t);
137 static int puc_teardown_intr(device_t, device_t, struct resource *,
139 static int puc_read_ivar(device_t, device_t, int, uintptr_t *);
141 static const struct puc_device_description *puc_find_description(uint32_t,
142 uint32_t, uint32_t, uint32_t);
143 static void puc_config_superio(device_t);
144 static void puc_config_win877(struct resource *);
145 static int puc_find_free_unit(char *);
147 static void puc_print_win877(bus_space_tag_t, bus_space_handle_t, u_int,
149 static void puc_print_resource_list(struct resource_list *);
153 puc_pci_probe(device_t dev)
155 uint32_t v1, v2, d1, d2;
156 const struct puc_device_description *desc;
158 if ((pci_read_config(dev, PCIR_HDRTYPE, 1) & PCIM_HDRTYPE) != 0)
161 v1 = pci_read_config(dev, PCIR_VENDOR, 2);
162 d1 = pci_read_config(dev, PCIR_DEVICE, 2);
163 v2 = pci_read_config(dev, PCIR_SUBVEND_0, 2);
164 d2 = pci_read_config(dev, PCIR_SUBDEV_0, 2);
166 desc = puc_find_description(v1, d1, v2, d2);
169 device_set_desc(dev, desc->name);
174 puc_probe_ilr(struct puc_softc *sc, struct resource *res)
179 switch (sc->sc_desc->ilr_type) {
180 case PUC_ILR_TYPE_DIGI:
181 sc->ilr_st = rman_get_bustag(res);
182 sc->ilr_sh = rman_get_bushandle(res);
183 for (i = 0; i < 2; i++) {
184 t1 = bus_space_read_1(sc->ilr_st, sc->ilr_sh,
185 sc->sc_desc->ilr_offset[i]);
187 bus_space_write_1(sc->ilr_st, sc->ilr_sh,
188 sc->sc_desc->ilr_offset[i], t1);
189 t2 = bus_space_read_1(sc->ilr_st, sc->ilr_sh,
190 sc->sc_desc->ilr_offset[i]);
203 puc_pci_attach(device_t dev)
206 int bidx, childunit, i, irq_setup, rid;
207 uint32_t v1, v2, d1, d2;
208 struct puc_softc *sc;
209 struct puc_device *pdev;
210 struct resource *res;
211 struct resource_list_entry *rle;
213 sc = (struct puc_softc *)device_get_softc(dev);
214 bzero(sc, sizeof(*sc));
215 v1 = pci_read_config(dev, PCIR_VENDOR, 2);
216 d1 = pci_read_config(dev, PCIR_DEVICE, 2);
217 v2 = pci_read_config(dev, PCIR_SUBVEND_0, 2);
218 d2 = pci_read_config(dev, PCIR_SUBDEV_0, 2);
219 sc->sc_desc = puc_find_description(v1, d1, v2, d2);
220 if (sc->sc_desc == NULL)
226 kprintf("puc: name: %s\n", sc->sc_desc->name);
229 res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
230 RF_ACTIVE | RF_SHAREABLE);
236 irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res,
238 &sc->intr_cookie, NULL);
243 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
244 if (rid == sc->sc_desc->ports[i].bar)
246 rid = sc->sc_desc->ports[i].bar;
247 bidx = PUC_PORT_BAR_INDEX(rid);
249 if (sc->sc_bar_mappings[bidx].res != NULL)
251 res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
252 0ul, ~0ul, 1, RF_ACTIVE);
254 kprintf("could not get resource\n");
257 sc->sc_bar_mappings[bidx].res = res;
259 if (sc->sc_desc->ilr_type != PUC_ILR_TYPE_NONE) {
260 sc->ilr_enabled = puc_probe_ilr(sc, res);
262 device_printf(dev, "ILR enabled\n");
264 device_printf(dev, "ILR disabled\n");
267 kprintf("port bst %x, start %x, end %x\n",
268 (u_int)rman_get_bustag(res), (u_int)rman_get_start(res),
269 (u_int)rman_get_end(res));
273 puc_config_superio(dev);
275 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
276 rid = sc->sc_desc->ports[i].bar;
277 bidx = PUC_PORT_BAR_INDEX(rid);
278 if (sc->sc_bar_mappings[bidx].res == NULL)
281 switch (sc->sc_desc->ports[i].type) {
282 case PUC_PORT_TYPE_COM:
288 pdev = kmalloc(sizeof(struct puc_device), M_DEVBUF,
290 resource_list_init(&pdev->resources);
292 /* First fake up an IRQ resource. */
293 resource_list_add(&pdev->resources, SYS_RES_IRQ, 0,
294 rman_get_start(sc->irqres), rman_get_end(sc->irqres),
295 rman_get_end(sc->irqres) - rman_get_start(sc->irqres) + 1,
296 rman_get_cpuid(sc->irqres));
297 rle = resource_list_find(&pdev->resources, SYS_RES_IRQ, 0);
298 rle->res = sc->irqres;
300 /* Now fake an IOPORT resource */
301 res = sc->sc_bar_mappings[bidx].res;
302 resource_list_add(&pdev->resources, SYS_RES_IOPORT, 0,
303 rman_get_start(res) + sc->sc_desc->ports[i].offset,
304 rman_get_end(res) + sc->sc_desc->ports[i].offset + 8 - 1,
306 rle = resource_list_find(&pdev->resources, SYS_RES_IOPORT, 0);
308 if (sc->barmuxed == 0) {
309 rle->res = sc->sc_bar_mappings[bidx].res;
311 rle->res = kmalloc(sizeof(struct resource), M_DEVBUF,
314 rle->res->r_start = rman_get_start(res) +
315 sc->sc_desc->ports[i].offset;
316 rle->res->r_end = rle->res->r_start + 8 - 1;
317 rle->res->r_bustag = rman_get_bustag(res);
318 bus_space_subregion(rle->res->r_bustag,
319 rman_get_bushandle(res),
320 sc->sc_desc->ports[i].offset, 8,
321 &rle->res->r_bushandle);
324 pdev->serialfreq = sc->sc_desc->ports[i].serialfreq;
326 childunit = puc_find_free_unit(typestr);
327 sc->sc_ports[i].dev = device_add_child(dev, typestr, childunit);
328 if (sc->sc_ports[i].dev == NULL) {
330 bus_space_unmap(rman_get_bustag(rle->res),
331 rman_get_bushandle(rle->res),
333 kfree(rle->res, M_DEVBUF);
334 kfree(pdev, M_DEVBUF);
338 device_set_ivars(sc->sc_ports[i].dev, pdev);
339 device_set_desc(sc->sc_ports[i].dev, sc->sc_desc->name);
341 device_quiet(sc->sc_ports[i].dev);
343 kprintf("puc: type %d, bar %x, offset %x\n",
344 sc->sc_desc->ports[i].type,
345 sc->sc_desc->ports[i].bar,
346 sc->sc_desc->ports[i].offset);
347 print_resource_list(&pdev->resources);
349 device_set_flags(sc->sc_ports[i].dev,
350 sc->sc_desc->ports[i].flags);
351 if (device_probe_and_attach(sc->sc_ports[i].dev) != 0) {
353 bus_space_unmap(rman_get_bustag(rle->res),
354 rman_get_bushandle(rle->res),
356 kfree(rle->res, M_DEVBUF);
357 kfree(pdev, M_DEVBUF);
369 puc_ilr_read(struct puc_softc *sc)
375 switch (sc->sc_desc->ilr_type) {
376 case PUC_ILR_TYPE_DIGI:
377 for (i = 1; i >= 0; i--) {
378 mask = (mask << 8) | (bus_space_read_1(sc->ilr_st,
379 sc->ilr_sh, sc->sc_desc->ilr_offset[i]) & 0xff);
391 * This is an interrupt handler. For boards that can't tell us which
392 * device generated the interrupt it just calls all the registered
393 * handlers sequencially, but for boards that can tell us which
394 * device(s) generated the interrupt it calls only handlers for devices
395 * that actually generated the interrupt.
402 struct puc_softc *sc;
404 sc = (struct puc_softc *)arg;
405 ilr_mask = sc->ilr_enabled ? puc_ilr_read(sc) : 0xffffffff;
406 for (i = 0; i < PUC_MAX_PORTS; i++)
407 if (sc->sc_ports[i].ihand != NULL &&
408 ((ilr_mask >> i) & 0x00000001))
409 (sc->sc_ports[i].ihand)(sc->sc_ports[i].ihandarg);
412 static const struct puc_device_description *
413 puc_find_description(uint32_t vend, uint32_t prod, uint32_t svend,
418 #define checkreg(val, index) \
419 (((val) & puc_devices[i].rmask[(index)]) == puc_devices[i].rval[(index)])
421 for (i = 0; puc_devices[i].name != NULL; i++) {
422 if (checkreg(vend, PUC_REG_VEND) &&
423 checkreg(prod, PUC_REG_PROD) &&
424 checkreg(svend, PUC_REG_SVEND) &&
425 checkreg(sprod, PUC_REG_SPROD))
426 return (&puc_devices[i]);
435 * It might be possible to make these more generic if we can detect patterns.
436 * For instance maybe if the size of a bar is 0x400 (the old isa space) it
437 * might contain one or more superio chips.
440 puc_config_superio(device_t dev)
442 struct puc_softc *sc = (struct puc_softc *)device_get_softc(dev);
444 if (sc->sc_desc->rval[PUC_REG_VEND] == 0x1592 &&
445 sc->sc_desc->rval[PUC_REG_PROD] == 0x0781)
446 puc_config_win877(sc->sc_bar_mappings[0].res);
449 #define rdspio(indx) (bus_space_write_1(bst, bsh, efir, indx), \
450 bus_space_read_1(bst, bsh, efdr))
451 #define wrspio(indx,data) (bus_space_write_1(bst, bsh, efir, indx), \
452 bus_space_write_1(bst, bsh, efdr, data))
456 puc_print_win877(bus_space_tag_t bst, bus_space_handle_t bsh, u_int efir,
459 u_char cr00, cr01, cr04, cr09, cr0d, cr14, cr15, cr16, cr17;
460 u_char cr18, cr19, cr24, cr25, cr28, cr2c, cr31, cr32;
479 kprintf("877T: cr00 %x, cr01 %x, cr04 %x, cr09 %x, cr0d %x, cr14 %x, "
480 "cr15 %x, cr16 %x, cr17 %x, cr18 %x, cr19 %x, cr24 %x, cr25 %x, "
481 "cr28 %x, cr2c %x, cr31 %x, cr32 %x\n", cr00, cr01, cr04, cr09,
482 cr0d, cr14, cr15, cr16, cr17,
483 cr18, cr19, cr24, cr25, cr28, cr2c, cr31, cr32);
488 puc_config_win877(struct resource *res)
493 bus_space_handle_t bsh;
495 bst = rman_get_bustag(res);
496 bsh = rman_get_bushandle(res);
498 /* configure the first W83877TF */
499 bus_space_write_1(bst, bsh, 0x250, 0x89);
502 val = rdspio(0x09) & 0x0f;
504 kprintf("conf_win877: Oops not a W83877TF\n");
510 puc_print_win877(bst, bsh, efir, efdr);
519 wrspio(0x24, 0x2e8 >> 2);
520 wrspio(0x25, 0x2f8 >> 2);
526 puc_print_win877(bst, bsh, efir, efdr);
529 bus_space_write_1(bst, bsh, 0x250, 0xaa);
531 /* configure the second W83877TF */
532 bus_space_write_1(bst, bsh, 0x3f0, 0x87);
533 bus_space_write_1(bst, bsh, 0x3f0, 0x87);
536 val = rdspio(0x09) & 0x0f;
538 kprintf("conf_win877: Oops not a W83877TF\n");
544 puc_print_win877(bst, bsh, efir, efdr);
553 wrspio(0x24, 0x3e8 >> 2);
554 wrspio(0x25, 0x3f8 >> 2);
560 puc_print_win877(bst, bsh, efir, efdr);
563 bus_space_write_1(bst, bsh, 0x3f0, 0xaa);
569 static int puc_find_free_unit(char *name)
577 while (resource_int_value(name, unit, "port", &start) == 0 &&
580 dc = devclass_find(name);
583 while (devclass_get_device(dc, unit))
586 kprintf("puc: Using %s%d\n", name, unit);
593 puc_print_resource_list(struct resource_list *rl)
595 struct resource_list_entry *rle;
597 kprintf("print_resource_list: rl %p\n", rl);
598 SLIST_FOREACH(rle, rl, link)
599 kprintf("type %x, rid %x\n", rle->type, rle->rid);
600 kprintf("print_resource_list: end.\n");
604 static struct resource *
605 puc_alloc_resource(device_t dev, device_t child, int type, int *rid,
606 u_long start, u_long end, u_long count, u_int flags, int cpuid __unused)
608 struct puc_device *pdev;
609 struct resource *retval;
610 struct resource_list *rl;
611 struct resource_list_entry *rle;
613 pdev = device_get_ivars(child);
614 rl = &pdev->resources;
617 kprintf("puc_alloc_resource: pdev %p, looking for t %x, r %x\n",
619 puc_print_resource_list(rl);
622 rle = resource_list_find(rl, type, *rid);
628 kprintf("found rle, %lx, %lx, %lx\n", start, end, count);
632 kprintf("oops rle is gone\n");
638 puc_release_resource(device_t dev, device_t child, int type, int rid,
639 struct resource *res)
645 puc_get_resource(device_t dev, device_t child, int type, int rid,
646 u_long *startp, u_long *countp)
648 struct puc_device *pdev;
649 struct resource_list *rl;
650 struct resource_list_entry *rle;
652 pdev = device_get_ivars(child);
653 rl = &pdev->resources;
656 kprintf("puc_get_resource: pdev %p, looking for t %x, r %x\n", pdev,
658 puc_print_resource_list(rl);
660 rle = resource_list_find(rl, type, rid);
663 kprintf("found rle %p,", rle);
666 *startp = rle->start;
668 *countp = rle->count;
670 kprintf(" %lx, %lx\n", rle->start, rle->count);
674 kprintf("oops rle is gone\n");
679 puc_setup_intr(device_t dev, device_t child, struct resource *r, int flags,
680 void (*ihand)(void *), void *arg,
681 void **cookiep, lwkt_serialize_t serializer)
684 struct puc_softc *sc;
686 sc = (struct puc_softc *)device_get_softc(dev);
687 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
688 if (sc->sc_ports[i].dev == child) {
689 if (sc->sc_ports[i].ihand != NULL)
691 sc->sc_ports[i].ihand = ihand;
692 sc->sc_ports[i].ihandarg = arg;
693 KKASSERT(serializer == NULL); /* not handled yet XXX */
702 puc_teardown_intr(device_t dev, device_t child, struct resource *r,
706 struct puc_softc *sc;
708 sc = (struct puc_softc *)device_get_softc(dev);
709 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
710 if (sc->sc_ports[i].dev == child) {
711 sc->sc_ports[i].ihand = NULL;
712 sc->sc_ports[i].ihandarg = NULL;
720 puc_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
722 struct puc_device *pdev;
724 pdev = device_get_ivars(child);
730 *result = pdev->serialfreq;
738 static device_method_t puc_pci_methods[] = {
739 /* Device interface */
740 DEVMETHOD(device_probe, puc_pci_probe),
741 DEVMETHOD(device_attach, puc_pci_attach),
743 DEVMETHOD(bus_alloc_resource, puc_alloc_resource),
744 DEVMETHOD(bus_release_resource, puc_release_resource),
745 DEVMETHOD(bus_get_resource, puc_get_resource),
746 DEVMETHOD(bus_read_ivar, puc_read_ivar),
747 DEVMETHOD(bus_setup_intr, puc_setup_intr),
748 DEVMETHOD(bus_teardown_intr, puc_teardown_intr),
749 DEVMETHOD(bus_print_child, bus_generic_print_child),
750 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
754 static driver_t puc_pci_driver = {
757 sizeof(struct puc_softc),
760 static devclass_t puc_devclass;
762 DRIVER_MODULE(puc, pci, puc_pci_driver, puc_devclass, NULL, NULL);