Line data Source code
1 : /* $OpenBSD: sili_pci.c,v 1.14 2013/12/06 21:03:04 deraadt Exp $ */
2 :
3 : /*
4 : * Copyright (c) 2007 David Gwynne <dlg@openbsd.org>
5 : *
6 : * Permission to use, copy, modify, and distribute this software for any
7 : * purpose with or without fee is hereby granted, provided that the above
8 : * copyright notice and this permission notice appear in all copies.
9 : *
10 : * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 : * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 : * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 : * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 : * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 : * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 : * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 : */
18 :
19 : #include <sys/param.h>
20 : #include <sys/systm.h>
21 : #include <sys/kernel.h>
22 : #include <sys/malloc.h>
23 : #include <sys/device.h>
24 : #include <sys/timeout.h>
25 :
26 : #include <machine/bus.h>
27 :
28 : #include <dev/pci/pcireg.h>
29 : #include <dev/pci/pcivar.h>
30 : #include <dev/pci/pcidevs.h>
31 :
32 : #include <dev/ata/atascsi.h>
33 :
34 : #include <dev/ic/silireg.h>
35 : #include <dev/ic/silivar.h>
36 :
37 : int sili_pci_match(struct device *, void *, void *);
38 : void sili_pci_attach(struct device *, struct device *, void *);
39 : int sili_pci_detach(struct device *, int);
40 : int sili_pci_activate(struct device *, int);
41 :
42 : struct sili_pci_softc {
43 : struct sili_softc psc_sili;
44 :
45 : pci_chipset_tag_t psc_pc;
46 : pcitag_t psc_tag;
47 :
48 : void *psc_ih;
49 : };
50 :
51 : struct cfattach sili_pci_ca = {
52 : sizeof(struct sili_pci_softc),
53 : sili_pci_match,
54 : sili_pci_attach,
55 : sili_pci_detach,
56 : sili_pci_activate
57 : };
58 :
59 : struct sili_device {
60 : pci_vendor_id_t sd_vendor;
61 : pci_product_id_t sd_product;
62 : u_int sd_nports;
63 : };
64 :
65 : const struct sili_device *sili_lookup(struct pci_attach_args *);
66 :
67 : static const struct sili_device sili_devices[] = {
68 : { PCI_VENDOR_CMDTECH, PCI_PRODUCT_CMDTECH_3124, 4 },
69 : { PCI_VENDOR_CMDTECH, PCI_PRODUCT_CMDTECH_3131, 1 },
70 : { PCI_VENDOR_CMDTECH, PCI_PRODUCT_CMDTECH_3132, 2 },
71 : { PCI_VENDOR_CMDTECH, PCI_PRODUCT_CMDTECH_3531, 1 },
72 : { PCI_VENDOR_CMDTECH, PCI_PRODUCT_CMDTECH_AAR_1220SA, 2 },
73 : { PCI_VENDOR_CMDTECH, PCI_PRODUCT_CMDTECH_AAR_1225SA, 2 },
74 : { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_3124, 4 }
75 : };
76 :
77 : const struct sili_device *
78 0 : sili_lookup(struct pci_attach_args *pa)
79 : {
80 : int i;
81 : const struct sili_device *sd;
82 :
83 0 : for (i = 0; i < nitems(sili_devices); i++) {
84 0 : sd = &sili_devices[i];
85 0 : if (sd->sd_vendor == PCI_VENDOR(pa->pa_id) &&
86 0 : sd->sd_product == PCI_PRODUCT(pa->pa_id))
87 0 : return (sd);
88 : }
89 :
90 0 : return (NULL);
91 0 : }
92 :
93 : int
94 0 : sili_pci_match(struct device *parent, void *match, void *aux)
95 : {
96 0 : return (sili_lookup((struct pci_attach_args *)aux) != NULL);
97 : }
98 :
99 : void
100 0 : sili_pci_attach(struct device *parent, struct device *self, void *aux)
101 : {
102 0 : struct sili_pci_softc *psc = (void *)self;
103 0 : struct sili_softc *sc = &psc->psc_sili;
104 0 : struct pci_attach_args *pa = aux;
105 : const struct sili_device *sd;
106 : pcireg_t memtype;
107 0 : pci_intr_handle_t ih;
108 : const char *intrstr;
109 :
110 0 : sd = sili_lookup(pa);
111 :
112 0 : psc->psc_pc = pa->pa_pc;
113 0 : psc->psc_tag = pa->pa_tag;
114 0 : psc->psc_ih = NULL;
115 0 : sc->sc_dmat = pa->pa_dmat;
116 0 : sc->sc_ios_global = 0;
117 0 : sc->sc_ios_port = 0;
118 0 : sc->sc_nports = sd->sd_nports;
119 :
120 0 : memtype = pci_mapreg_type(psc->psc_pc, psc->psc_tag,
121 : SILI_PCI_BAR_GLOBAL);
122 0 : if (pci_mapreg_map(pa, SILI_PCI_BAR_GLOBAL, memtype, 0,
123 0 : &sc->sc_iot_global, &sc->sc_ioh_global,
124 0 : NULL, &sc->sc_ios_global, 0) != 0) {
125 0 : printf(": unable to map global registers\n");
126 0 : return;
127 : }
128 :
129 0 : memtype = pci_mapreg_type(psc->psc_pc, psc->psc_tag,
130 : SILI_PCI_BAR_PORT);
131 0 : if (pci_mapreg_map(pa, SILI_PCI_BAR_PORT, memtype, 0,
132 0 : &sc->sc_iot_port, &sc->sc_ioh_port,
133 0 : NULL, &sc->sc_ios_port, 0) != 0) {
134 0 : printf(": unable to map port registers\n");
135 0 : goto unmap_global;
136 : }
137 :
138 : /* hook up the interrupt */
139 0 : if (pci_intr_map(pa, &ih)) {
140 0 : printf(": unable to map interrupt\n");
141 0 : goto unmap_port;
142 : }
143 0 : intrstr = pci_intr_string(psc->psc_pc, ih);
144 0 : psc->psc_ih = pci_intr_establish(psc->psc_pc, ih, IPL_BIO,
145 0 : sili_intr, sc, sc->sc_dev.dv_xname);
146 0 : if (psc->psc_ih == NULL) {
147 0 : printf(": unable to map interrupt%s%s\n",
148 0 : intrstr == NULL ? "" : " at ",
149 0 : intrstr == NULL ? "" : intrstr);
150 0 : goto unmap_port;
151 : }
152 0 : printf(": %s", intrstr);
153 :
154 0 : if (sili_attach(sc) != 0) {
155 : /* error printed by sili_attach */
156 : goto deintr;
157 : }
158 :
159 0 : return;
160 :
161 : deintr:
162 0 : pci_intr_disestablish(psc->psc_pc, psc->psc_ih);
163 0 : psc->psc_ih = NULL;
164 : unmap_port:
165 0 : bus_space_unmap(sc->sc_iot_port, sc->sc_ioh_port, sc->sc_ios_port);
166 0 : sc->sc_ios_port = 0;
167 : unmap_global:
168 0 : bus_space_unmap(sc->sc_iot_global, sc->sc_ioh_global,
169 0 : sc->sc_ios_global);
170 0 : sc->sc_ios_global = 0;
171 0 : }
172 :
173 : int
174 0 : sili_pci_detach(struct device *self, int flags)
175 : {
176 0 : struct sili_pci_softc *psc = (struct sili_pci_softc *)self;
177 0 : struct sili_softc *sc = &psc->psc_sili;
178 : int rv;
179 :
180 0 : rv = sili_detach(sc, flags);
181 0 : if (rv != 0)
182 0 : return (rv);
183 :
184 0 : if (psc->psc_ih != NULL) {
185 0 : pci_intr_disestablish(psc->psc_pc, psc->psc_ih);
186 0 : psc->psc_ih = NULL;
187 0 : }
188 0 : if (sc->sc_ios_port != 0) {
189 0 : bus_space_unmap(sc->sc_iot_port, sc->sc_ioh_port,
190 : sc->sc_ios_port);
191 0 : sc->sc_ios_port = 0;
192 0 : }
193 0 : if (sc->sc_ios_global != 0) {
194 0 : bus_space_unmap(sc->sc_iot_global, sc->sc_ioh_global,
195 : sc->sc_ios_global);
196 0 : sc->sc_ios_global = 0;
197 0 : }
198 :
199 0 : return (0);
200 0 : }
201 :
202 : int
203 0 : sili_pci_activate(struct device *self, int act)
204 : {
205 0 : struct sili_softc *sc = (struct sili_softc *)self;
206 : int rv = 0;
207 :
208 0 : switch (act) {
209 : case DVACT_RESUME:
210 0 : sili_resume(sc);
211 0 : rv = config_activate_children(self, act);
212 0 : break;
213 : default:
214 0 : rv = config_activate_children(self, act);
215 0 : break;
216 : }
217 0 : return (rv);
218 : }
|