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);
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 rle = resource_list_find(&pdev->resources, SYS_RES_IRQ, 0);
297 rle->res = sc->irqres;
299 /* Now fake an IOPORT resource */
300 res = sc->sc_bar_mappings[bidx].res;
301 resource_list_add(&pdev->resources, SYS_RES_IOPORT, 0,
302 rman_get_start(res) + sc->sc_desc->ports[i].offset,
303 rman_get_end(res) + sc->sc_desc->ports[i].offset + 8 - 1,
305 rle = resource_list_find(&pdev->resources, SYS_RES_IOPORT, 0);
307 if (sc->barmuxed == 0) {
308 rle->res = sc->sc_bar_mappings[bidx].res;
310 rle->res = kmalloc(sizeof(struct resource), M_DEVBUF,
313 rle->res->r_start = rman_get_start(res) +
314 sc->sc_desc->ports[i].offset;
315 rle->res->r_end = rle->res->r_start + 8 - 1;
316 rle->res->r_bustag = rman_get_bustag(res);
317 bus_space_subregion(rle->res->r_bustag,
318 rman_get_bushandle(res),
319 sc->sc_desc->ports[i].offset, 8,
320 &rle->res->r_bushandle);
323 pdev->serialfreq = sc->sc_desc->ports[i].serialfreq;
325 childunit = puc_find_free_unit(typestr);
326 sc->sc_ports[i].dev = device_add_child(dev, typestr, childunit);
327 if (sc->sc_ports[i].dev == NULL) {
329 bus_space_unmap(rman_get_bustag(rle->res),
330 rman_get_bushandle(rle->res),
332 kfree(rle->res, M_DEVBUF);
333 kfree(pdev, M_DEVBUF);
337 device_set_ivars(sc->sc_ports[i].dev, pdev);
338 device_set_desc(sc->sc_ports[i].dev, sc->sc_desc->name);
340 device_quiet(sc->sc_ports[i].dev);
342 kprintf("puc: type %d, bar %x, offset %x\n",
343 sc->sc_desc->ports[i].type,
344 sc->sc_desc->ports[i].bar,
345 sc->sc_desc->ports[i].offset);
346 print_resource_list(&pdev->resources);
348 device_set_flags(sc->sc_ports[i].dev,
349 sc->sc_desc->ports[i].flags);
350 if (device_probe_and_attach(sc->sc_ports[i].dev) != 0) {
352 bus_space_unmap(rman_get_bustag(rle->res),
353 rman_get_bushandle(rle->res),
355 kfree(rle->res, M_DEVBUF);
356 kfree(pdev, M_DEVBUF);
368 puc_ilr_read(struct puc_softc *sc)
374 switch (sc->sc_desc->ilr_type) {
375 case PUC_ILR_TYPE_DIGI:
376 for (i = 1; i >= 0; i--) {
377 mask = (mask << 8) | (bus_space_read_1(sc->ilr_st,
378 sc->ilr_sh, sc->sc_desc->ilr_offset[i]) & 0xff);
390 * This is an interrupt handler. For boards that can't tell us which
391 * device generated the interrupt it just calls all the registered
392 * handlers sequencially, but for boards that can tell us which
393 * device(s) generated the interrupt it calls only handlers for devices
394 * that actually generated the interrupt.
401 struct puc_softc *sc;
403 sc = (struct puc_softc *)arg;
404 ilr_mask = sc->ilr_enabled ? puc_ilr_read(sc) : 0xffffffff;
405 for (i = 0; i < PUC_MAX_PORTS; i++)
406 if (sc->sc_ports[i].ihand != NULL &&
407 ((ilr_mask >> i) & 0x00000001))
408 (sc->sc_ports[i].ihand)(sc->sc_ports[i].ihandarg);
411 static const struct puc_device_description *
412 puc_find_description(uint32_t vend, uint32_t prod, uint32_t svend,
417 #define checkreg(val, index) \
418 (((val) & puc_devices[i].rmask[(index)]) == puc_devices[i].rval[(index)])
420 for (i = 0; puc_devices[i].name != NULL; i++) {
421 if (checkreg(vend, PUC_REG_VEND) &&
422 checkreg(prod, PUC_REG_PROD) &&
423 checkreg(svend, PUC_REG_SVEND) &&
424 checkreg(sprod, PUC_REG_SPROD))
425 return (&puc_devices[i]);
434 * It might be possible to make these more generic if we can detect patterns.
435 * For instance maybe if the size of a bar is 0x400 (the old isa space) it
436 * might contain one or more superio chips.
439 puc_config_superio(device_t dev)
441 struct puc_softc *sc = (struct puc_softc *)device_get_softc(dev);
443 if (sc->sc_desc->rval[PUC_REG_VEND] == 0x1592 &&
444 sc->sc_desc->rval[PUC_REG_PROD] == 0x0781)
445 puc_config_win877(sc->sc_bar_mappings[0].res);
448 #define rdspio(indx) (bus_space_write_1(bst, bsh, efir, indx), \
449 bus_space_read_1(bst, bsh, efdr))
450 #define wrspio(indx,data) (bus_space_write_1(bst, bsh, efir, indx), \
451 bus_space_write_1(bst, bsh, efdr, data))
455 puc_print_win877(bus_space_tag_t bst, bus_space_handle_t bsh, u_int efir,
458 u_char cr00, cr01, cr04, cr09, cr0d, cr14, cr15, cr16, cr17;
459 u_char cr18, cr19, cr24, cr25, cr28, cr2c, cr31, cr32;
478 kprintf("877T: cr00 %x, cr01 %x, cr04 %x, cr09 %x, cr0d %x, cr14 %x, "
479 "cr15 %x, cr16 %x, cr17 %x, cr18 %x, cr19 %x, cr24 %x, cr25 %x, "
480 "cr28 %x, cr2c %x, cr31 %x, cr32 %x\n", cr00, cr01, cr04, cr09,
481 cr0d, cr14, cr15, cr16, cr17,
482 cr18, cr19, cr24, cr25, cr28, cr2c, cr31, cr32);
487 puc_config_win877(struct resource *res)
492 bus_space_handle_t bsh;
494 bst = rman_get_bustag(res);
495 bsh = rman_get_bushandle(res);
497 /* configure the first W83877TF */
498 bus_space_write_1(bst, bsh, 0x250, 0x89);
501 val = rdspio(0x09) & 0x0f;
503 kprintf("conf_win877: Oops not a W83877TF\n");
509 puc_print_win877(bst, bsh, efir, efdr);
518 wrspio(0x24, 0x2e8 >> 2);
519 wrspio(0x25, 0x2f8 >> 2);
525 puc_print_win877(bst, bsh, efir, efdr);
528 bus_space_write_1(bst, bsh, 0x250, 0xaa);
530 /* configure the second W83877TF */
531 bus_space_write_1(bst, bsh, 0x3f0, 0x87);
532 bus_space_write_1(bst, bsh, 0x3f0, 0x87);
535 val = rdspio(0x09) & 0x0f;
537 kprintf("conf_win877: Oops not a W83877TF\n");
543 puc_print_win877(bst, bsh, efir, efdr);
552 wrspio(0x24, 0x3e8 >> 2);
553 wrspio(0x25, 0x3f8 >> 2);
559 puc_print_win877(bst, bsh, efir, efdr);
562 bus_space_write_1(bst, bsh, 0x3f0, 0xaa);
568 static int puc_find_free_unit(char *name)
576 while (resource_int_value(name, unit, "port", &start) == 0 &&
579 dc = devclass_find(name);
582 while (devclass_get_device(dc, unit))
585 kprintf("puc: Using %s%d\n", name, unit);
592 puc_print_resource_list(struct resource_list *rl)
594 struct resource_list_entry *rle;
596 kprintf("print_resource_list: rl %p\n", rl);
597 SLIST_FOREACH(rle, rl, link)
598 kprintf("type %x, rid %x\n", rle->type, rle->rid);
599 kprintf("print_resource_list: end.\n");
603 static struct resource *
604 puc_alloc_resource(device_t dev, device_t child, int type, int *rid,
605 u_long start, u_long end, u_long count, u_int flags)
607 struct puc_device *pdev;
608 struct resource *retval;
609 struct resource_list *rl;
610 struct resource_list_entry *rle;
612 pdev = device_get_ivars(child);
613 rl = &pdev->resources;
616 kprintf("puc_alloc_resource: pdev %p, looking for t %x, r %x\n",
618 puc_print_resource_list(rl);
621 rle = resource_list_find(rl, type, *rid);
627 kprintf("found rle, %lx, %lx, %lx\n", start, end, count);
631 kprintf("oops rle is gone\n");
637 puc_release_resource(device_t dev, device_t child, int type, int rid,
638 struct resource *res)
644 puc_get_resource(device_t dev, device_t child, int type, int rid,
645 u_long *startp, u_long *countp)
647 struct puc_device *pdev;
648 struct resource_list *rl;
649 struct resource_list_entry *rle;
651 pdev = device_get_ivars(child);
652 rl = &pdev->resources;
655 kprintf("puc_get_resource: pdev %p, looking for t %x, r %x\n", pdev,
657 puc_print_resource_list(rl);
659 rle = resource_list_find(rl, type, rid);
662 kprintf("found rle %p,", rle);
665 *startp = rle->start;
667 *countp = rle->count;
669 kprintf(" %lx, %lx\n", rle->start, rle->count);
673 kprintf("oops rle is gone\n");
678 puc_setup_intr(device_t dev, device_t child, struct resource *r, int flags,
679 void (*ihand)(void *), void *arg,
680 void **cookiep, lwkt_serialize_t serializer)
683 struct puc_softc *sc;
685 sc = (struct puc_softc *)device_get_softc(dev);
686 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
687 if (sc->sc_ports[i].dev == child) {
688 if (sc->sc_ports[i].ihand != 0)
690 sc->sc_ports[i].ihand = ihand;
691 sc->sc_ports[i].ihandarg = arg;
692 KKASSERT(serializer == NULL); /* not handled yet XXX */
701 puc_teardown_intr(device_t dev, device_t child, struct resource *r,
705 struct puc_softc *sc;
707 sc = (struct puc_softc *)device_get_softc(dev);
708 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
709 if (sc->sc_ports[i].dev == child) {
710 sc->sc_ports[i].ihand = NULL;
711 sc->sc_ports[i].ihandarg = NULL;
719 puc_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
721 struct puc_device *pdev;
723 pdev = device_get_ivars(child);
729 *result = pdev->serialfreq;
737 static device_method_t puc_pci_methods[] = {
738 /* Device interface */
739 DEVMETHOD(device_probe, puc_pci_probe),
740 DEVMETHOD(device_attach, puc_pci_attach),
742 DEVMETHOD(bus_alloc_resource, puc_alloc_resource),
743 DEVMETHOD(bus_release_resource, puc_release_resource),
744 DEVMETHOD(bus_get_resource, puc_get_resource),
745 DEVMETHOD(bus_read_ivar, puc_read_ivar),
746 DEVMETHOD(bus_setup_intr, puc_setup_intr),
747 DEVMETHOD(bus_teardown_intr, puc_teardown_intr),
748 DEVMETHOD(bus_print_child, bus_generic_print_child),
749 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
753 static driver_t puc_pci_driver = {
756 sizeof(struct puc_softc),
759 static devclass_t puc_devclass;
761 DRIVER_MODULE(puc, pci, puc_pci_driver, puc_devclass, NULL, NULL);