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