Merge branch 'vendor/OPENSSL'
[dragonfly.git] / sys / dev / serial / si / si_pci.c
1 /*
2  * Device driver for Specialix range (SI/XIO) of serial line multiplexors.
3  *
4  * Copyright (C) 2000, Peter Wemm <peter@netplex.com.au>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notices, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notices, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED
16  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
18  * NO EVENT SHALL THE AUTHORS BE LIABLE.
19  *
20  * $FreeBSD: src/sys/dev/si/si_pci.c,v 1.2 2000/01/24 08:11:10 peter Exp $
21  * $DragonFly: src/sys/dev/serial/si/si_pci.c,v 1.6 2006/10/25 20:56:02 dillon Exp $
22  */
23
24 #include <sys/param.h>
25 #include <sys/systm.h>
26 #include <sys/kernel.h>
27 #include <sys/bus.h>
28 #include <sys/rman.h>
29
30 #include "sireg.h"
31 #include "sivar.h"
32
33 #include <bus/pci/pcivar.h>
34
35 static int
36 si_pci_probe(device_t dev)
37 {
38         const char *desc = NULL;
39
40         switch (pci_get_devid(dev)) {
41         case 0x400011cb:
42                 desc = "Specialix SI/XIO PCI host card";
43                 break;
44         case 0x200011cb:
45                 if (pci_read_config(dev, SIJETSSIDREG, 4) == 0x020011cb)
46                         desc = "Specialix SX PCI host card";
47                 break;
48         }
49         if (desc) {
50                 device_set_desc(dev, desc);
51                 return 0;
52         }
53         return ENXIO;
54 }
55
56 static int
57 si_pci_attach(device_t dev)
58 {
59         struct si_softc *sc;
60         void *ih;
61         int error;
62
63         error = 0;
64         ih = NULL;
65         sc = device_get_softc(dev);
66
67         switch (pci_get_devid(dev)) {
68         case 0x400011cb:
69                 sc->sc_type = SIPCI;
70                 sc->sc_mem_rid = SIPCIBADR;
71                 break;
72         case 0x200011cb:
73                 sc->sc_type = SIJETPCI;
74                 sc->sc_mem_rid = SIJETBADR;
75                 break;
76         }
77
78         sc->sc_mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
79                                             &sc->sc_mem_rid,
80                                             0, ~0, 1, RF_ACTIVE);
81         if (!sc->sc_mem_res) {
82                 device_printf(dev, "couldn't map memory\n");
83                 goto fail;
84         }
85         sc->sc_paddr = (caddr_t)rman_get_start(sc->sc_mem_res);
86         sc->sc_maddr = rman_get_virtual(sc->sc_mem_res);
87
88         sc->sc_irq_rid = 0;
89         sc->sc_irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->sc_irq_rid,
90                                             0, ~0, 1, RF_ACTIVE | RF_SHAREABLE);
91         if (!sc->sc_irq_res) {
92                 device_printf(dev, "couldn't map interrupt\n");
93                 goto fail;
94         }
95         sc->sc_irq = rman_get_start(sc->sc_irq_res);
96         error = bus_setup_intr(dev, sc->sc_irq_res, INTR_MPSAFE, si_intr, sc, &ih, NULL);
97         if (error) {
98                 device_printf(dev, "could not activate interrupt\n");
99                 goto fail;
100         }
101
102         error = siattach(dev);
103         if (error)
104                 goto fail;
105         return (0);             /* success */
106
107 fail:
108         if (error == 0)
109                 error = ENXIO;
110         if (sc->sc_irq_res) {
111                 if (ih)
112                         bus_teardown_intr(dev, sc->sc_irq_res, ih);
113                 bus_release_resource(dev, SYS_RES_IRQ,
114                                      sc->sc_irq_rid, sc->sc_irq_res);
115                 sc->sc_irq_res = 0;
116         }
117         if (sc->sc_mem_res) {
118                 bus_release_resource(dev, SYS_RES_MEMORY,
119                                      sc->sc_mem_rid, sc->sc_mem_res);
120                 sc->sc_mem_res = 0;
121         }
122         return (error);
123 }
124
125 static device_method_t si_pci_methods[] = {
126         /* Device interface */
127         DEVMETHOD(device_probe,         si_pci_probe),
128         DEVMETHOD(device_attach,        si_pci_attach),
129
130         { 0, 0 }
131 };
132
133 static driver_t si_pci_driver = {
134         "si",
135         si_pci_methods,
136         sizeof(struct si_softc),
137 };
138
139 DRIVER_MODULE(si, pci, si_pci_driver, si_devclass, 0, 0);