1 /* $NetBSD: puc.c,v 1.7 2000/07/29 17:43:38 jlam Exp $ */
4 * Copyright (c) 2002 JF Hay. All rights reserved.
5 * Copyright (c) 2000 M. Warner Losh. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
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.
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.
30 * Copyright (c) 1996, 1998, 1999
31 * Christopher G. Demetriou. All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
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
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.
60 #include <sys/cdefs.h>
61 __FBSDID("$FreeBSD$");
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>
91 #include <machine/bus.h>
92 #include <machine/resource.h>
95 #include <dev/pci/pcireg.h>
96 #include <dev/pci/pcivar.h>
97 #include <dev/puc/pucvar.h>
102 const struct puc_device_description *sc_desc;
104 /* card-global dynamic data */
107 struct resource *irqres;
111 struct resource *res;
112 } sc_bar_mappings[PUC_MAX_BAR];
114 /* per-port dynamic data */
117 /* filled in by bus_setup_intr() */
118 void (*ihand)(void *);
120 } sc_ports[PUC_MAX_PORTS];
124 struct resource_list resources;
128 static int puc_pci_probe(device_t dev);
129 static int puc_pci_attach(device_t dev);
130 static void puc_intr(void *arg);
132 static struct resource *puc_alloc_resource(device_t, device_t, int, int *,
133 u_long, u_long, u_long, u_int);
134 static int puc_release_resource(device_t, device_t, int, int,
136 static int puc_get_resource(device_t, device_t, int, int, u_long *, u_long *);
137 static int puc_setup_intr(device_t, device_t, struct resource *, int,
138 void (*)(void *), void *, void **);
139 static int puc_teardown_intr(device_t, device_t, struct resource *,
141 static int puc_read_ivar(device_t, device_t, int, uintptr_t *);
143 static const struct puc_device_description *puc_find_description(uint32_t,
144 uint32_t, uint32_t, uint32_t);
145 static void puc_config_superio(device_t);
146 static void puc_config_win877(struct resource *);
147 static int puc_find_free_unit(char *);
149 static void puc_print_win877(bus_space_tag_t, bus_space_handle_t, u_int,
151 static void puc_print_resource_list(struct resource_list *);
155 puc_pci_probe(device_t dev)
157 uint32_t v1, v2, d1, d2;
158 const struct puc_device_description *desc;
160 if ((pci_read_config(dev, PCIR_HEADERTYPE, 1) & 0x7f) != 0)
163 v1 = pci_read_config(dev, PCIR_VENDOR, 2);
164 d1 = pci_read_config(dev, PCIR_DEVICE, 2);
165 v2 = pci_read_config(dev, PCIR_SUBVEND_0, 2);
166 d2 = pci_read_config(dev, PCIR_SUBDEV_0, 2);
168 desc = puc_find_description(v1, d1, v2, d2);
171 device_set_desc(dev, desc->name);
176 puc_pci_attach(device_t dev)
179 int bidx, childunit, i, irq_setup, rid;
180 uint32_t v1, v2, d1, d2;
181 struct puc_softc *sc;
182 struct puc_device *pdev;
183 struct resource *res;
184 struct resource_list_entry *rle;
186 sc = (struct puc_softc *)device_get_softc(dev);
187 bzero(sc, sizeof(*sc));
188 v1 = pci_read_config(dev, PCIR_VENDOR, 2);
189 d1 = pci_read_config(dev, PCIR_DEVICE, 2);
190 v2 = pci_read_config(dev, PCIR_SUBVEND_0, 2);
191 d2 = pci_read_config(dev, PCIR_SUBDEV_0, 2);
192 sc->sc_desc = puc_find_description(v1, d1, v2, d2);
193 if (sc->sc_desc == NULL)
199 printf("puc: name: %s\n", sc->sc_desc->name);
202 res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
203 RF_ACTIVE | RF_SHAREABLE);
210 irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res,
211 INTR_TYPE_TTY | INTR_FAST, puc_intr, sc, &sc->intr_cookie);
216 irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res,
217 INTR_TYPE_TTY, puc_intr, sc, &sc->intr_cookie);
222 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
223 if (rid == sc->sc_desc->ports[i].bar)
225 rid = sc->sc_desc->ports[i].bar;
226 bidx = PUC_PORT_BAR_INDEX(rid);
228 if (sc->sc_bar_mappings[bidx].res != NULL)
230 res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
231 0ul, ~0ul, 1, RF_ACTIVE);
233 printf("could not get resource\n");
236 sc->sc_bar_mappings[bidx].res = res;
238 printf("port bst %x, start %x, end %x\n",
239 (u_int)rman_get_bustag(res), (u_int)rman_get_start(res),
240 (u_int)rman_get_end(res));
244 puc_config_superio(dev);
246 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
247 rid = sc->sc_desc->ports[i].bar;
248 bidx = PUC_PORT_BAR_INDEX(rid);
249 if (sc->sc_bar_mappings[bidx].res == NULL)
252 switch (sc->sc_desc->ports[i].type) {
253 case PUC_PORT_TYPE_COM:
259 pdev = malloc(sizeof(struct puc_device), M_DEVBUF,
263 resource_list_init(&pdev->resources);
265 /* First fake up an IRQ resource. */
266 resource_list_add(&pdev->resources, SYS_RES_IRQ, 0,
267 rman_get_start(sc->irqres), rman_get_end(sc->irqres),
268 rman_get_end(sc->irqres) - rman_get_start(sc->irqres) + 1);
269 rle = resource_list_find(&pdev->resources, SYS_RES_IRQ, 0);
270 rle->res = sc->irqres;
272 /* Now fake an IOPORT resource */
273 res = sc->sc_bar_mappings[bidx].res;
274 resource_list_add(&pdev->resources, SYS_RES_IOPORT, 0,
275 rman_get_start(res) + sc->sc_desc->ports[i].offset,
276 rman_get_end(res) + sc->sc_desc->ports[i].offset + 8 - 1,
278 rle = resource_list_find(&pdev->resources, SYS_RES_IOPORT, 0);
280 if (sc->barmuxed == 0) {
281 rle->res = sc->sc_bar_mappings[bidx].res;
283 rle->res = malloc(sizeof(struct resource), M_DEVBUF,
285 if (rle->res == NULL) {
286 free(pdev, M_DEVBUF);
290 rle->res->r_start = rman_get_start(res) +
291 sc->sc_desc->ports[i].offset;
292 rle->res->r_end = rle->res->r_start + 8 - 1;
293 rle->res->r_bustag = rman_get_bustag(res);
294 bus_space_subregion(rle->res->r_bustag,
295 rman_get_bushandle(res),
296 sc->sc_desc->ports[i].offset, 8,
297 &rle->res->r_bushandle);
300 pdev->serialfreq = sc->sc_desc->ports[i].serialfreq;
302 childunit = puc_find_free_unit(typestr);
303 sc->sc_ports[i].dev = device_add_child(dev, typestr, childunit);
304 if (sc->sc_ports[i].dev == NULL) {
306 bus_space_unmap(rman_get_bustag(rle->res),
307 rman_get_bushandle(rle->res),
309 free(rle->res, M_DEVBUF);
310 free(pdev, M_DEVBUF);
314 device_set_ivars(sc->sc_ports[i].dev, pdev);
315 device_set_desc(sc->sc_ports[i].dev, sc->sc_desc->name);
317 device_quiet(sc->sc_ports[i].dev);
319 printf("puc: type %d, bar %x, offset %x\n",
320 sc->sc_desc->ports[i].type,
321 sc->sc_desc->ports[i].bar,
322 sc->sc_desc->ports[i].offset);
323 print_resource_list(&pdev->resources);
325 if (device_probe_and_attach(sc->sc_ports[i].dev) != 0) {
327 bus_space_unmap(rman_get_bustag(rle->res),
328 rman_get_bushandle(rle->res),
330 free(rle->res, M_DEVBUF);
331 free(pdev, M_DEVBUF);
343 * This is just an brute force interrupt handler. It just calls all the
344 * registered handlers sequencially.
346 * Later on we should maybe have a different handler for boards that can
347 * tell us which device generated the interrupt.
353 struct puc_softc *sc;
355 sc = (struct puc_softc *)arg;
356 for (i = 0; i < PUC_MAX_PORTS; i++)
357 if (sc->sc_ports[i].ihand != NULL)
358 (sc->sc_ports[i].ihand)(sc->sc_ports[i].ihandarg);
361 static const struct puc_device_description *
362 puc_find_description(uint32_t vend, uint32_t prod, uint32_t svend,
367 #define checkreg(val, index) \
368 (((val) & puc_devices[i].rmask[(index)]) == puc_devices[i].rval[(index)])
370 for (i = 0; puc_devices[i].name != NULL; i++) {
371 if (checkreg(vend, PUC_REG_VEND) &&
372 checkreg(prod, PUC_REG_PROD) &&
373 checkreg(svend, PUC_REG_SVEND) &&
374 checkreg(sprod, PUC_REG_SPROD))
375 return (&puc_devices[i]);
384 * It might be possible to make these more generic if we can detect patterns.
385 * For instance maybe if the size of a bar is 0x400 (the old isa space) it
386 * might contain one or more superio chips.
389 puc_config_superio(device_t dev)
391 struct puc_softc *sc = (struct puc_softc *)device_get_softc(dev);
393 if (sc->sc_desc->rval[PUC_REG_VEND] == 0x1592 &&
394 sc->sc_desc->rval[PUC_REG_PROD] == 0x0781)
395 puc_config_win877(sc->sc_bar_mappings[0].res);
398 #define rdspio(indx) (bus_space_write_1(bst, bsh, efir, indx), \
399 bus_space_read_1(bst, bsh, efdr))
400 #define wrspio(indx,data) (bus_space_write_1(bst, bsh, efir, indx), \
401 bus_space_write_1(bst, bsh, efdr, data))
405 puc_print_win877(bus_space_tag_t bst, bus_space_handle_t bsh, u_int efir,
408 u_char cr00, cr01, cr04, cr09, cr0d, cr14, cr15, cr16, cr17;
409 u_char cr18, cr19, cr24, cr25, cr28, cr2c, cr31, cr32;
428 printf("877T: cr00 %x, cr01 %x, cr04 %x, cr09 %x, cr0d %x, cr14 %x, "
429 "cr15 %x, cr16 %x, cr17 %x, cr18 %x, cr19 %x, cr24 %x, cr25 %x, "
430 "cr28 %x, cr2c %x, cr31 %x, cr32 %x\n", cr00, cr01, cr04, cr09,
431 cr0d, cr14, cr15, cr16, cr17,
432 cr18, cr19, cr24, cr25, cr28, cr2c, cr31, cr32);
437 puc_config_win877(struct resource *res)
442 bus_space_handle_t bsh;
444 bst = rman_get_bustag(res);
445 bsh = rman_get_bushandle(res);
447 /* configure the first W83877TF */
448 bus_space_write_1(bst, bsh, 0x250, 0x89);
451 val = rdspio(0x09) & 0x0f;
453 printf("conf_win877: Oops not a W83877TF\n");
459 puc_print_win877(bst, bsh, efir, efdr);
468 wrspio(0x24, 0x2e8 >> 2);
469 wrspio(0x25, 0x2f8 >> 2);
475 puc_print_win877(bst, bsh, efir, efdr);
478 bus_space_write_1(bst, bsh, 0x250, 0xaa);
480 /* configure the second W83877TF */
481 bus_space_write_1(bst, bsh, 0x3f0, 0x87);
482 bus_space_write_1(bst, bsh, 0x3f0, 0x87);
485 val = rdspio(0x09) & 0x0f;
487 printf("conf_win877: Oops not a W83877TF\n");
493 puc_print_win877(bst, bsh, efir, efdr);
502 wrspio(0x24, 0x3e8 >> 2);
503 wrspio(0x25, 0x3f8 >> 2);
509 puc_print_win877(bst, bsh, efir, efdr);
512 bus_space_write_1(bst, bsh, 0x3f0, 0xaa);
518 static int puc_find_free_unit(char *name)
526 while (resource_int_value(name, unit, "port", &start) == 0 &&
529 dc = devclass_find(name);
532 while (devclass_get_device(dc, unit))
535 printf("puc: Using %s%d\n", name, unit);
542 puc_print_resource_list(struct resource_list *rl)
544 struct resource_list_entry *rle;
546 printf("print_resource_list: rl %p\n", rl);
547 SLIST_FOREACH(rle, rl, link)
548 printf("type %x, rid %x\n", rle->type, rle->rid);
549 printf("print_resource_list: end.\n");
553 static struct resource *
554 puc_alloc_resource(device_t dev, device_t child, int type, int *rid,
555 u_long start, u_long end, u_long count, u_int flags)
557 struct puc_device *pdev;
558 struct resource *retval;
559 struct resource_list *rl;
560 struct resource_list_entry *rle;
562 pdev = device_get_ivars(child);
563 rl = &pdev->resources;
566 printf("puc_alloc_resource: pdev %p, looking for t %x, r %x\n",
568 puc_print_resource_list(rl);
571 rle = resource_list_find(rl, type, *rid);
577 printf("found rle, %lx, %lx, %lx\n", start, end, count);
581 printf("oops rle is gone\n");
587 puc_release_resource(device_t dev, device_t child, int type, int rid,
588 struct resource *res)
594 puc_get_resource(device_t dev, device_t child, int type, int rid,
595 u_long *startp, u_long *countp)
597 struct puc_device *pdev;
598 struct resource_list *rl;
599 struct resource_list_entry *rle;
601 pdev = device_get_ivars(child);
602 rl = &pdev->resources;
605 printf("puc_get_resource: pdev %p, looking for t %x, r %x\n", pdev,
607 puc_print_resource_list(rl);
609 rle = resource_list_find(rl, type, rid);
612 printf("found rle %p,", rle);
615 *startp = rle->start;
617 *countp = rle->count;
619 printf(" %lx, %lx\n", rle->start, rle->count);
623 printf("oops rle is gone\n");
628 puc_setup_intr(device_t dev, device_t child, struct resource *r, int flags,
629 void (*ihand)(void *), void *arg, void **cookiep)
632 struct puc_softc *sc;
634 sc = (struct puc_softc *)device_get_softc(dev);
635 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
636 if (sc->sc_ports[i].dev == child) {
637 if (sc->sc_ports[i].ihand != 0)
639 sc->sc_ports[i].ihand = ihand;
640 sc->sc_ports[i].ihandarg = arg;
649 puc_teardown_intr(device_t dev, device_t child, struct resource *r,
653 struct puc_softc *sc;
655 sc = (struct puc_softc *)device_get_softc(dev);
656 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
657 if (sc->sc_ports[i].dev == child) {
658 sc->sc_ports[i].ihand = NULL;
659 sc->sc_ports[i].ihandarg = NULL;
667 puc_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
669 struct puc_device *pdev;
671 pdev = device_get_ivars(child);
677 *result = pdev->serialfreq;
685 static device_method_t puc_pci_methods[] = {
686 /* Device interface */
687 DEVMETHOD(device_probe, puc_pci_probe),
688 DEVMETHOD(device_attach, puc_pci_attach),
690 DEVMETHOD(bus_alloc_resource, puc_alloc_resource),
691 DEVMETHOD(bus_release_resource, puc_release_resource),
692 DEVMETHOD(bus_get_resource, puc_get_resource),
693 DEVMETHOD(bus_read_ivar, puc_read_ivar),
694 DEVMETHOD(bus_setup_intr, puc_setup_intr),
695 DEVMETHOD(bus_teardown_intr, puc_teardown_intr),
696 DEVMETHOD(bus_print_child, bus_generic_print_child),
697 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
701 static driver_t puc_pci_driver = {
704 sizeof(struct puc_softc),
707 static devclass_t puc_devclass;
709 DRIVER_MODULE(puc, pci, puc_pci_driver, puc_devclass, 0, 0);