ahcisata_pci.c revision 1.20 1 1.20 jakllsch /* $NetBSD: ahcisata_pci.c,v 1.20 2010/07/27 22:07:51 jakllsch Exp $ */
2 1.1 bouyer
3 1.1 bouyer /*
4 1.1 bouyer * Copyright (c) 2006 Manuel Bouyer.
5 1.1 bouyer *
6 1.1 bouyer * Redistribution and use in source and binary forms, with or without
7 1.1 bouyer * modification, are permitted provided that the following conditions
8 1.1 bouyer * are met:
9 1.1 bouyer * 1. Redistributions of source code must retain the above copyright
10 1.1 bouyer * notice, this list of conditions and the following disclaimer.
11 1.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 bouyer * notice, this list of conditions and the following disclaimer in the
13 1.1 bouyer * documentation and/or other materials provided with the distribution.
14 1.1 bouyer *
15 1.1 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 bouyer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 1.1 bouyer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 1.1 bouyer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 1.1 bouyer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 1.1 bouyer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 1.1 bouyer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 1.1 bouyer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 1.1 bouyer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 1.1 bouyer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 1.1 bouyer *
26 1.1 bouyer */
27 1.1 bouyer
28 1.1 bouyer #include <sys/cdefs.h>
29 1.20 jakllsch __KERNEL_RCSID(0, "$NetBSD: ahcisata_pci.c,v 1.20 2010/07/27 22:07:51 jakllsch Exp $");
30 1.1 bouyer
31 1.1 bouyer #include <sys/types.h>
32 1.1 bouyer #include <sys/malloc.h>
33 1.1 bouyer #include <sys/param.h>
34 1.1 bouyer #include <sys/kernel.h>
35 1.1 bouyer #include <sys/systm.h>
36 1.1 bouyer #include <sys/disklabel.h>
37 1.2 jmcneill #include <sys/pmf.h>
38 1.1 bouyer
39 1.1 bouyer #include <uvm/uvm_extern.h>
40 1.1 bouyer
41 1.1 bouyer #include <dev/pci/pcivar.h>
42 1.1 bouyer #include <dev/pci/pcidevs.h>
43 1.1 bouyer #include <dev/pci/pciidereg.h>
44 1.1 bouyer #include <dev/pci/pciidevar.h>
45 1.1 bouyer #include <dev/ic/ahcisatavar.h>
46 1.1 bouyer
47 1.20 jakllsch struct ahci_pci_quirk {
48 1.20 jakllsch pci_vendor_id_t vendor; /* Vendor ID */
49 1.20 jakllsch pci_product_id_t product; /* Product ID */
50 1.20 jakllsch int quirks; /* quirks; see below */
51 1.20 jakllsch };
52 1.20 jakllsch
53 1.20 jakllsch #define AHCI_PCI_QUIRK_FORCE __BIT(0) /* force attach */
54 1.12 dillo
55 1.20 jakllsch static const struct ahci_pci_quirk ahci_pci_quirks[] = {
56 1.12 dillo { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP65_SATA,
57 1.12 dillo AHCI_PCI_QUIRK_FORCE },
58 1.14 dholland { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP65_SATA2,
59 1.14 dholland AHCI_PCI_QUIRK_FORCE },
60 1.14 dholland { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP65_SATA3,
61 1.14 dholland AHCI_PCI_QUIRK_FORCE },
62 1.14 dholland { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP65_SATA4,
63 1.14 dholland AHCI_PCI_QUIRK_FORCE },
64 1.12 dillo { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP67_SATA,
65 1.12 dillo AHCI_PCI_QUIRK_FORCE },
66 1.13 tron { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP73_AHCI_1,
67 1.13 tron AHCI_PCI_QUIRK_FORCE },
68 1.15 cegger { PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_88SE6121,
69 1.15 cegger AHCI_PCI_QUIRK_FORCE },
70 1.12 dillo };
71 1.12 dillo
72 1.2 jmcneill struct ahci_pci_softc {
73 1.9 cube struct ahci_softc ah_sc;
74 1.2 jmcneill pci_chipset_tag_t sc_pc;
75 1.2 jmcneill pcitag_t sc_pcitag;
76 1.20 jakllsch void * sc_ih;
77 1.2 jmcneill };
78 1.2 jmcneill
79 1.20 jakllsch static bool ahci_pci_has_quirk(pci_vendor_id_t, pci_product_id_t, int);
80 1.9 cube static int ahci_pci_match(device_t, cfdata_t, void *);
81 1.9 cube static void ahci_pci_attach(device_t, device_t, void *);
82 1.20 jakllsch static int ahci_pci_detach(device_t, int);
83 1.19 dyoung static bool ahci_pci_resume(device_t, const pmf_qual_t *);
84 1.2 jmcneill
85 1.1 bouyer
86 1.9 cube CFATTACH_DECL_NEW(ahcisata_pci, sizeof(struct ahci_pci_softc),
87 1.20 jakllsch ahci_pci_match, ahci_pci_attach, ahci_pci_detach, NULL);
88 1.20 jakllsch
89 1.20 jakllsch static bool
90 1.20 jakllsch ahci_pci_has_quirk(pci_vendor_id_t vendor, pci_product_id_t product, int quirk)
91 1.20 jakllsch {
92 1.20 jakllsch int i;
93 1.20 jakllsch
94 1.20 jakllsch for (i = 0; i < __arraycount(ahci_pci_quirks); i++)
95 1.20 jakllsch if (vendor == ahci_pci_quirks[i].vendor &&
96 1.20 jakllsch product == ahci_pci_quirks[i].product)
97 1.20 jakllsch return (ahci_pci_quirks[i].quirks & quirk) != 0;
98 1.20 jakllsch return false;
99 1.20 jakllsch }
100 1.1 bouyer
101 1.1 bouyer static int
102 1.9 cube ahci_pci_match(device_t parent, cfdata_t match, void *aux)
103 1.1 bouyer {
104 1.1 bouyer struct pci_attach_args *pa = aux;
105 1.1 bouyer bus_space_tag_t regt;
106 1.1 bouyer bus_space_handle_t regh;
107 1.1 bouyer bus_size_t size;
108 1.1 bouyer int ret = 0;
109 1.20 jakllsch bool force;
110 1.12 dillo
111 1.20 jakllsch force = ahci_pci_has_quirk(PCI_VENDOR(pa->pa_id),
112 1.20 jakllsch PCI_PRODUCT(pa->pa_id),
113 1.20 jakllsch AHCI_PCI_QUIRK_FORCE);
114 1.1 bouyer
115 1.12 dillo /* if wrong class and not forced by quirks, don't match */
116 1.12 dillo if ((PCI_CLASS(pa->pa_class) != PCI_CLASS_MASS_STORAGE ||
117 1.12 dillo ((PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_MASS_STORAGE_SATA ||
118 1.12 dillo PCI_INTERFACE(pa->pa_class) != PCI_INTERFACE_SATA_AHCI) &&
119 1.12 dillo PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_MASS_STORAGE_RAID)) &&
120 1.20 jakllsch (force == false))
121 1.12 dillo return 0;
122 1.12 dillo
123 1.12 dillo if (pci_mapreg_map(pa, AHCI_PCI_ABAR,
124 1.12 dillo PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0,
125 1.12 dillo ®t, ®h, NULL, &size) != 0)
126 1.12 dillo return 0;
127 1.12 dillo
128 1.12 dillo if ((PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_SATA &&
129 1.3 xtraeme PCI_INTERFACE(pa->pa_class) == PCI_INTERFACE_SATA_AHCI) ||
130 1.20 jakllsch (bus_space_read_4(regt, regh, AHCI_GHC) & AHCI_GHC_AE) ||
131 1.20 jakllsch (force == true))
132 1.12 dillo ret = 3;
133 1.1 bouyer
134 1.12 dillo bus_space_unmap(regt, regh, size);
135 1.5 jnemeth return ret;
136 1.1 bouyer }
137 1.1 bouyer
138 1.1 bouyer static void
139 1.9 cube ahci_pci_attach(device_t parent, device_t self, void *aux)
140 1.1 bouyer {
141 1.1 bouyer struct pci_attach_args *pa = aux;
142 1.9 cube struct ahci_pci_softc *psc = device_private(self);
143 1.2 jmcneill struct ahci_softc *sc = &psc->ah_sc;
144 1.1 bouyer char devinfo[256];
145 1.1 bouyer const char *intrstr;
146 1.1 bouyer pci_intr_handle_t intrhandle;
147 1.1 bouyer
148 1.11 cube sc->sc_atac.atac_dev = self;
149 1.10 cube
150 1.1 bouyer if (pci_mapreg_map(pa, AHCI_PCI_ABAR,
151 1.1 bouyer PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0,
152 1.20 jakllsch &sc->sc_ahcit, &sc->sc_ahcih, NULL, &sc->sc_ahcis) != 0) {
153 1.9 cube aprint_error_dev(self, "can't map ahci registers\n");
154 1.1 bouyer return;
155 1.1 bouyer }
156 1.2 jmcneill psc->sc_pc = pa->pa_pc;
157 1.2 jmcneill psc->sc_pcitag = pa->pa_tag;
158 1.2 jmcneill
159 1.1 bouyer pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
160 1.1 bouyer aprint_naive(": AHCI disk controller\n");
161 1.1 bouyer aprint_normal(": %s\n", devinfo);
162 1.1 bouyer
163 1.1 bouyer if (pci_intr_map(pa, &intrhandle) != 0) {
164 1.20 jakllsch aprint_error_dev(self, "couldn't map interrupt\n");
165 1.1 bouyer return;
166 1.1 bouyer }
167 1.1 bouyer intrstr = pci_intr_string(pa->pa_pc, intrhandle);
168 1.20 jakllsch psc->sc_ih = pci_intr_establish(pa->pa_pc, intrhandle, IPL_BIO, ahci_intr, sc);
169 1.20 jakllsch if (psc->sc_ih == NULL) {
170 1.20 jakllsch aprint_error_dev(self, "couldn't establish interrupt\n");
171 1.1 bouyer return;
172 1.1 bouyer }
173 1.20 jakllsch aprint_normal_dev(self, "interrupting at %s\n",
174 1.1 bouyer intrstr ? intrstr : "unknown interrupt");
175 1.1 bouyer sc->sc_dmat = pa->pa_dmat;
176 1.3 xtraeme
177 1.6 mlelstv if (PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_RAID) {
178 1.7 mlelstv AHCIDEBUG_PRINT(("%s: RAID mode\n", AHCINAME(sc)), DEBUG_PROBE);
179 1.3 xtraeme sc->sc_atac_capflags = ATAC_CAP_RAID;
180 1.6 mlelstv } else {
181 1.7 mlelstv AHCIDEBUG_PRINT(("%s: SATA mode\n", AHCINAME(sc)), DEBUG_PROBE);
182 1.6 mlelstv }
183 1.3 xtraeme
184 1.1 bouyer ahci_attach(sc);
185 1.2 jmcneill
186 1.2 jmcneill if (!pmf_device_register(self, NULL, ahci_pci_resume))
187 1.2 jmcneill aprint_error_dev(self, "couldn't establish power handler\n");
188 1.2 jmcneill }
189 1.2 jmcneill
190 1.20 jakllsch static int
191 1.20 jakllsch ahci_pci_detach(device_t dv, int flags)
192 1.20 jakllsch {
193 1.20 jakllsch struct ahci_pci_softc *psc;
194 1.20 jakllsch struct ahci_softc *sc;
195 1.20 jakllsch int rv;
196 1.20 jakllsch
197 1.20 jakllsch psc = device_private(dv);
198 1.20 jakllsch sc = &psc->ah_sc;
199 1.20 jakllsch
200 1.20 jakllsch if ((rv = ahci_detach(sc, flags)))
201 1.20 jakllsch return rv;
202 1.20 jakllsch
203 1.20 jakllsch if (psc->sc_ih != NULL)
204 1.20 jakllsch pci_intr_disestablish(psc->sc_pc, psc->sc_ih);
205 1.20 jakllsch
206 1.20 jakllsch bus_space_unmap(sc->sc_ahcit, sc->sc_ahcih, sc->sc_ahcis);
207 1.20 jakllsch
208 1.20 jakllsch return 0;
209 1.20 jakllsch }
210 1.20 jakllsch
211 1.2 jmcneill static bool
212 1.19 dyoung ahci_pci_resume(device_t dv, const pmf_qual_t *qual)
213 1.2 jmcneill {
214 1.2 jmcneill struct ahci_pci_softc *psc = device_private(dv);
215 1.2 jmcneill struct ahci_softc *sc = &psc->ah_sc;
216 1.2 jmcneill int s;
217 1.2 jmcneill
218 1.2 jmcneill s = splbio();
219 1.20 jakllsch ahci_resume(sc);
220 1.2 jmcneill splx(s);
221 1.2 jmcneill
222 1.2 jmcneill return true;
223 1.1 bouyer }
224