xref: /openbsd/sys/dev/pci/sili_pci.c (revision 73471bf0)
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 sili_lookup(struct pci_attach_args *pa)
79 {
80 	int				i;
81 	const struct sili_device	*sd;
82 
83 	for (i = 0; i < nitems(sili_devices); i++) {
84 		sd = &sili_devices[i];
85 		if (sd->sd_vendor == PCI_VENDOR(pa->pa_id) &&
86 		    sd->sd_product == PCI_PRODUCT(pa->pa_id))
87 			return (sd);
88 	}
89 
90 	return (NULL);
91 }
92 
93 int
94 sili_pci_match(struct device *parent, void *match, void *aux)
95 {
96 	return (sili_lookup((struct pci_attach_args *)aux) != NULL);
97 }
98 
99 void
100 sili_pci_attach(struct device *parent, struct device *self, void *aux)
101 {
102 	struct sili_pci_softc		*psc = (void *)self;
103 	struct sili_softc		*sc = &psc->psc_sili;
104 	struct pci_attach_args		*pa = aux;
105 	const struct sili_device	*sd;
106 	pcireg_t			memtype;
107 	pci_intr_handle_t		ih;
108 	const char			*intrstr;
109 
110 	sd = sili_lookup(pa);
111 
112 	psc->psc_pc = pa->pa_pc;
113 	psc->psc_tag = pa->pa_tag;
114 	psc->psc_ih = NULL;
115 	sc->sc_dmat = pa->pa_dmat;
116 	sc->sc_ios_global = 0;
117 	sc->sc_ios_port = 0;
118 	sc->sc_nports = sd->sd_nports;
119 
120 	memtype = pci_mapreg_type(psc->psc_pc, psc->psc_tag,
121 	    SILI_PCI_BAR_GLOBAL);
122 	if (pci_mapreg_map(pa, SILI_PCI_BAR_GLOBAL, memtype, 0,
123 	    &sc->sc_iot_global, &sc->sc_ioh_global,
124 	    NULL, &sc->sc_ios_global, 0) != 0) {
125 		printf(": unable to map global registers\n");
126 		return;
127 	}
128 
129 	memtype = pci_mapreg_type(psc->psc_pc, psc->psc_tag,
130 	    SILI_PCI_BAR_PORT);
131 	if (pci_mapreg_map(pa, SILI_PCI_BAR_PORT, memtype, 0,
132 	    &sc->sc_iot_port, &sc->sc_ioh_port,
133 	    NULL, &sc->sc_ios_port, 0) != 0) {
134 		printf(": unable to map port registers\n");
135 		goto unmap_global;
136 	}
137 
138 	/* hook up the interrupt */
139 	if (pci_intr_map(pa, &ih)) {
140 		printf(": unable to map interrupt\n");
141 		goto unmap_port;
142 	}
143 	intrstr = pci_intr_string(psc->psc_pc, ih);
144 	psc->psc_ih = pci_intr_establish(psc->psc_pc, ih, IPL_BIO,
145 	    sili_intr, sc, sc->sc_dev.dv_xname);
146 	if (psc->psc_ih == NULL) {
147 		printf(": unable to map interrupt%s%s\n",
148 		    intrstr == NULL ? "" : " at ",
149 		    intrstr == NULL ? "" : intrstr);
150 		goto unmap_port;
151 	}
152 	printf(": %s", intrstr);
153 
154 	if (sili_attach(sc) != 0) {
155 		/* error printed by sili_attach */
156 		goto deintr;
157 	}
158 
159 	return;
160 
161 deintr:
162 	pci_intr_disestablish(psc->psc_pc, psc->psc_ih);
163 	psc->psc_ih = NULL;
164 unmap_port:
165 	bus_space_unmap(sc->sc_iot_port, sc->sc_ioh_port, sc->sc_ios_port);
166 	sc->sc_ios_port = 0;
167 unmap_global:
168 	bus_space_unmap(sc->sc_iot_global, sc->sc_ioh_global,
169 	    sc->sc_ios_global);
170 	sc->sc_ios_global = 0;
171 }
172 
173 int
174 sili_pci_detach(struct device *self, int flags)
175 {
176 	struct sili_pci_softc		*psc = (struct sili_pci_softc *)self;
177 	struct sili_softc		*sc = &psc->psc_sili;
178 	int				rv;
179 
180 	rv = sili_detach(sc, flags);
181 	if (rv != 0)
182 		return (rv);
183 
184 	if (psc->psc_ih != NULL) {
185 		pci_intr_disestablish(psc->psc_pc, psc->psc_ih);
186 		psc->psc_ih = NULL;
187 	}
188 	if (sc->sc_ios_port != 0) {
189 		bus_space_unmap(sc->sc_iot_port, sc->sc_ioh_port,
190 		    sc->sc_ios_port);
191 		sc->sc_ios_port = 0;
192 	}
193 	if (sc->sc_ios_global != 0) {
194 		bus_space_unmap(sc->sc_iot_global, sc->sc_ioh_global,
195 		    sc->sc_ios_global);
196 		sc->sc_ios_global = 0;
197 	}
198 
199 	return (0);
200 }
201 
202 int
203 sili_pci_activate(struct device *self, int act)
204 {
205 	struct sili_softc		*sc = (struct sili_softc *)self;
206 	int				 rv = 0;
207 
208 	switch (act) {
209 	case DVACT_RESUME:
210 		sili_resume(sc);
211 		rv = config_activate_children(self, act);
212 		break;
213 	default:
214 		rv = config_activate_children(self, act);
215 		break;
216 	}
217 	return (rv);
218 }
219