siisata_cardbus.c revision 1.1.2.2 1 /* $NetBSD: siisata_cardbus.c,v 1.1.2.2 2009/07/23 23:31:46 jym Exp $ */
2 /* Id: siisata_pci.c,v 1.11 2008/05/21 16:20:11 jakllsch Exp */
3
4 /*
5 * Copyright (c) 2006 Manuel Bouyer.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Manuel Bouyer.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34 /*-
35 * Copyright (c) 2007, 2008 Jonathan A. Kollasch.
36 * All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
48 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
51 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
56 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57 *
58 */
59
60 #include <sys/cdefs.h>
61
62
63 #include <sys/types.h>
64 #include <sys/malloc.h>
65 #include <sys/param.h>
66 #include <sys/kernel.h>
67 #include <sys/systm.h>
68
69 #include <uvm/uvm_extern.h>
70
71 #include <dev/cardbus/cardbusvar.h>
72 #include <dev/pci/pcidevs.h>
73 #include <dev/ic/siisatavar.h>
74
75 #define cardbus_devinfo pci_devinfo
76
77 struct siisata_cardbus_softc {
78 struct siisata_softc si_sc;
79 cardbus_chipset_tag_t sc_cc;
80 cardbus_function_tag_t sc_cf;
81 cardbus_devfunc_t sc_ct;
82
83 bus_size_t sc_grsize;
84 bus_size_t sc_prsize;
85 void *sc_ih;
86 };
87
88 static int siisata_cardbus_match(device_t, cfdata_t, void *);
89 static void siisata_cardbus_attach(device_t, device_t, void *);
90 static int siisata_cardbus_detach(device_t, int);
91 static bool siisata_cardbus_resume(device_t PMF_FN_PROTO);
92
93 static const struct siisata_cardbus_product {
94 cardbus_vendor_id_t scp_vendor;
95 cardbus_product_id_t scp_product;
96 int scp_ports;
97 int scp_chip;
98
99 } siisata_cardbus_products[] = {
100 {
101 PCI_VENDOR_CMDTECH, PCI_PRODUCT_CMDTECH_3124,
102 4, 3124
103 },
104 {
105 0, 0,
106 0, 0
107 },
108 };
109
110 CFATTACH_DECL_NEW(siisata_cardbus, sizeof(struct siisata_cardbus_softc),
111 siisata_cardbus_match, siisata_cardbus_attach, siisata_cardbus_detach,
112 NULL);
113
114 static const struct siisata_cardbus_product *
115 siisata_cardbus_lookup(const struct cardbus_attach_args *ca)
116 {
117 const struct siisata_cardbus_product *scp;
118
119 for (scp = siisata_cardbus_products; scp->scp_ports > 0; scp++) {
120 if (CARDBUS_VENDOR(ca->ca_id) == scp->scp_vendor &&
121 CARDBUS_PRODUCT(ca->ca_id) == scp->scp_product)
122 return scp;
123 }
124 return NULL;
125 }
126
127 static int
128 siisata_cardbus_match(device_t parent, cfdata_t match, void *aux)
129 {
130 struct cardbus_attach_args *ca = aux;
131
132 if (siisata_cardbus_lookup(ca) != NULL)
133 return 3;
134
135 return 0;
136 }
137
138 static void
139 siisata_cardbus_attach(device_t parent, device_t self, void *aux)
140 {
141 struct cardbus_attach_args *ca = aux;
142 struct cardbus_softc *cbsc = device_private(parent);
143 struct siisata_cardbus_softc *csc = device_private(self);
144 struct siisata_softc *sc = &csc->si_sc;
145 cardbus_devfunc_t ct = ca->ca_ct;
146 cardbus_chipset_tag_t cc = ct->ct_cc;
147 cardbus_function_tag_t cf = ct->ct_cf;
148 cardbusreg_t csr;
149 const struct siisata_cardbus_product *scp;
150 bus_space_tag_t memt;
151 bus_space_handle_t memh;
152 bus_addr_t base;
153 bus_size_t grsize, prsize;
154 uint32_t gcreg;
155 char devinfo[256];
156
157 sc->sc_atac.atac_dev = self;
158
159 csc->sc_cc = cc;
160 csc->sc_cf = cf;
161 csc->sc_ct = ct;
162
163 cardbus_devinfo(ca->ca_id, ca->ca_class, 0, devinfo, sizeof(devinfo));
164 aprint_naive(": SATA-II HBA\n");
165 aprint_normal(": %s\n", devinfo);
166
167 /*
168 * XXXX
169 * Our BAR0/BAR1 type is 64bit Memory. Cardbus_mapreg_map() don't
170 * support 64bit Memory. We map ourself...
171 */
172 /* map bar0 */
173 {
174 #define SIISATA_BAR0_SIZE 128
175 grsize = SIISATA_BAR0_SIZE;
176 csr =
177 cardbus_conf_read(cc, cf, ca->ca_tag, SIISATA_CARDBUS_BAR0);
178 base = PCI_MAPREG_MEM_ADDR(csr);
179 memt = cbsc->sc_memt;
180 if ((*cf->cardbus_space_alloc)(cc, cbsc->sc_rbus_memt, base,
181 grsize, grsize - 1, grsize, 0, &base, &memh)) {
182 aprint_error(
183 "%s: unable to map device global registers\n",
184 SIISATANAME(sc));
185 return;
186 }
187 cardbus_conf_write(cc, cf, ca->ca_tag, SIISATA_CARDBUS_BAR0,
188 base);
189 }
190 sc->sc_grt = memt;
191 sc->sc_grh = memh;
192 csc->sc_grsize = grsize;
193
194 /* map bar1 */
195 {
196 #define SIISATA_BAR1_SIZE (32 * 1024)
197 prsize = SIISATA_BAR1_SIZE;
198 base = PCI_MAPREG_MEM_ADDR(cardbus_conf_read(cc, cf, ca->ca_tag,
199 SIISATA_CARDBUS_BAR1));
200 memt = cbsc->sc_memt;
201 if ((*cf->cardbus_space_alloc)(cc, cbsc->sc_rbus_memt, base,
202 prsize, prsize - 1, prsize, 0, &base, &memh)) {
203 cardbus_conf_write(cc, cf, ca->ca_tag,
204 SIISATA_CARDBUS_BAR0, 0);
205 (*cf->cardbus_space_free)(cc, cbsc->sc_rbus_memt,
206 sc->sc_grh, grsize);
207 aprint_error(
208 "%s: unable to map device port registers\n",
209 SIISATANAME(sc));
210 return;
211 }
212 cardbus_conf_write(cc, cf, ca->ca_tag, SIISATA_CARDBUS_BAR1,
213 base);
214 }
215 sc->sc_prt = memt;
216 sc->sc_prh = memh;
217 csc->sc_prsize = prsize;
218
219 sc->sc_dmat = ca->ca_dmat;
220
221 /* map interrupt */
222 csc->sc_ih = cardbus_intr_establish(cc, cf, ca->ca_intrline, IPL_BIO,
223 siisata_intr, sc);
224 if (csc->sc_ih == NULL) {
225 cardbus_conf_write(cc, cf, ca->ca_tag, SIISATA_CARDBUS_BAR0, 0);
226 (*cf->cardbus_space_free)(cc, cbsc->sc_rbus_memt, sc->sc_grh,
227 grsize);
228 cardbus_conf_write(cc, cf, ca->ca_tag, SIISATA_CARDBUS_BAR1, 0);
229 (*cf->cardbus_space_free)(cc, cbsc->sc_rbus_memt, sc->sc_prh,
230 prsize);
231 aprint_error("%s: couldn't establish interrupt\n",
232 SIISATANAME(sc));
233 return;
234 }
235
236 /* fill in number of ports on this device */
237 scp = siisata_cardbus_lookup(ca);
238 if (scp != NULL)
239 sc->sc_atac.atac_nchannels = scp->scp_ports;
240 else
241 /* _match() should prevent us from getting here */
242 panic("siisata: the universe might be falling apart!\n");
243
244 /* enable bus mastering in case the firmware didn't */
245 csr = cardbus_conf_read(cc, cf, ca->ca_tag, CARDBUS_COMMAND_STATUS_REG);
246 csr |= CARDBUS_COMMAND_MASTER_ENABLE;
247 csr |= CARDBUS_COMMAND_MEM_ENABLE;
248 cardbus_conf_write(cc, cf, ca->ca_tag, CARDBUS_COMMAND_STATUS_REG, csr);
249
250 gcreg = GRREAD(sc, GR_GC);
251
252 /* CardBus supports only 32-bit 33MHz */
253 KASSERT(!(gcreg &
254 (GR_GC_REQ64|GR_GC_DEVSEL|GR_GC_STOP|GR_GC_TRDY|GR_GC_M66EN)));
255
256 aprint_normal("%s: SiI%d on 32-bit, 33MHz PCI (CardBus).",
257 SIISATANAME(sc), scp->scp_chip);
258 if (gcreg & GR_GC_3GBPS)
259 aprint_normal(" 3.0Gb/s capable.\n");
260 else
261 aprint_normal("\n");
262
263 siisata_attach(sc);
264
265 if (!pmf_device_register(self, NULL, siisata_cardbus_resume))
266 aprint_error_dev(self, "couldn't establish power handler\n");
267 }
268
269 static int
270 siisata_cardbus_detach(device_t self, int flags)
271 {
272 struct cardbus_softc *cbsc = device_private(device_parent(self));
273 struct siisata_cardbus_softc *csc = device_private(self);
274 struct siisata_softc *sc = &csc->si_sc;
275 struct cardbus_devfunc *ct = csc->sc_ct;
276 cardbus_chipset_tag_t cc = ct->ct_cc;
277 cardbus_function_tag_t cf = ct->ct_cf;
278 cardbustag_t ctag = cardbus_make_tag(cc, cf, cbsc->sc_bus, ct->ct_func);
279 int rv;
280
281 rv = siisata_detach(sc, flags);
282 if (rv)
283 return (rv);
284 if (csc->sc_ih != NULL) {
285 cardbus_intr_disestablish(csc->sc_cc, csc->sc_cf, csc->sc_ih);
286 csc->sc_ih = NULL;
287 }
288 if (csc->sc_grsize) {
289 cardbus_conf_write(cc, cf, ctag, SIISATA_CARDBUS_BAR0, 0);
290 (*cf->cardbus_space_free)(cc, cbsc->sc_rbus_memt, sc->sc_grh,
291 csc->sc_grsize);
292 csc->sc_grsize = 0;
293 }
294 if (csc->sc_prsize) {
295 cardbus_conf_write(cc, cf, ctag, SIISATA_CARDBUS_BAR1, 0);
296 (*cf->cardbus_space_free)(cc, cbsc->sc_rbus_memt, sc->sc_prh,
297 csc->sc_prsize);
298 csc->sc_prsize = 0;
299 }
300 cardbus_free_tag(cc, cf, ctag);
301 return 0;
302 }
303
304 static bool
305 siisata_cardbus_resume(device_t dv PMF_FN_ARGS)
306 {
307 struct siisata_cardbus_softc *csc = device_private(dv);
308 struct siisata_softc *sc = &csc->si_sc;
309 int s;
310
311 s = splbio();
312 siisata_resume(sc);
313 splx(s);
314
315 return true;
316 }
317