ahcisata_pci.c revision 1.1.20.6 1 1.1.20.6 yamt /* $NetBSD: ahcisata_pci.c,v 1.1.20.6 2008/03/17 09:15:10 yamt Exp $ */
2 1.1.20.2 yamt
3 1.1.20.2 yamt /*
4 1.1.20.2 yamt * Copyright (c) 2006 Manuel Bouyer.
5 1.1.20.2 yamt *
6 1.1.20.2 yamt * Redistribution and use in source and binary forms, with or without
7 1.1.20.2 yamt * modification, are permitted provided that the following conditions
8 1.1.20.2 yamt * are met:
9 1.1.20.2 yamt * 1. Redistributions of source code must retain the above copyright
10 1.1.20.2 yamt * notice, this list of conditions and the following disclaimer.
11 1.1.20.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
12 1.1.20.2 yamt * notice, this list of conditions and the following disclaimer in the
13 1.1.20.2 yamt * documentation and/or other materials provided with the distribution.
14 1.1.20.2 yamt * 3. All advertising materials mentioning features or use of this software
15 1.1.20.2 yamt * must display the following acknowledgement:
16 1.1.20.2 yamt * This product includes software developed by Manuel Bouyer.
17 1.1.20.2 yamt * 4. The name of the author may not be used to endorse or promote products
18 1.1.20.2 yamt * derived from this software without specific prior written permission.
19 1.1.20.2 yamt *
20 1.1.20.2 yamt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 1.1.20.2 yamt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 1.1.20.2 yamt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 1.1.20.2 yamt * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 1.1.20.2 yamt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 1.1.20.2 yamt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 1.1.20.2 yamt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 1.1.20.2 yamt * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 1.1.20.2 yamt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 1.1.20.2 yamt * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.1.20.2 yamt *
31 1.1.20.2 yamt */
32 1.1.20.2 yamt
33 1.1.20.2 yamt #include <sys/cdefs.h>
34 1.1.20.6 yamt __KERNEL_RCSID(0, "$NetBSD: ahcisata_pci.c,v 1.1.20.6 2008/03/17 09:15:10 yamt Exp $");
35 1.1.20.2 yamt
36 1.1.20.2 yamt #include <sys/types.h>
37 1.1.20.2 yamt #include <sys/malloc.h>
38 1.1.20.2 yamt #include <sys/param.h>
39 1.1.20.2 yamt #include <sys/kernel.h>
40 1.1.20.2 yamt #include <sys/systm.h>
41 1.1.20.2 yamt #include <sys/disklabel.h>
42 1.1.20.3 yamt #include <sys/pmf.h>
43 1.1.20.2 yamt
44 1.1.20.2 yamt #include <uvm/uvm_extern.h>
45 1.1.20.2 yamt
46 1.1.20.2 yamt #include <dev/pci/pcivar.h>
47 1.1.20.2 yamt #include <dev/pci/pcidevs.h>
48 1.1.20.2 yamt #include <dev/pci/pciidereg.h>
49 1.1.20.2 yamt #include <dev/pci/pciidevar.h>
50 1.1.20.2 yamt #include <dev/ic/ahcisatavar.h>
51 1.1.20.2 yamt
52 1.1.20.3 yamt struct ahci_pci_softc {
53 1.1.20.3 yamt struct ahci_softc ah_sc; /* must come first, struct device */
54 1.1.20.3 yamt pci_chipset_tag_t sc_pc;
55 1.1.20.3 yamt pcitag_t sc_pcitag;
56 1.1.20.3 yamt };
57 1.1.20.3 yamt
58 1.1.20.3 yamt
59 1.1.20.2 yamt static int ahci_pci_match(struct device *, struct cfdata *, void *);
60 1.1.20.2 yamt static void ahci_pci_attach(struct device *, struct device *, void *);
61 1.1.20.6 yamt static bool ahci_pci_resume(device_t PMF_FN_PROTO);
62 1.1.20.3 yamt
63 1.1.20.2 yamt
64 1.1.20.3 yamt CFATTACH_DECL(ahcisata_pci, sizeof(struct ahci_pci_softc),
65 1.1.20.2 yamt ahci_pci_match, ahci_pci_attach, NULL, NULL);
66 1.1.20.2 yamt
67 1.1.20.2 yamt static int
68 1.1.20.2 yamt ahci_pci_match(struct device *parent, struct cfdata *match,
69 1.1.20.2 yamt void *aux)
70 1.1.20.2 yamt {
71 1.1.20.2 yamt struct pci_attach_args *pa = aux;
72 1.1.20.2 yamt bus_space_tag_t regt;
73 1.1.20.2 yamt bus_space_handle_t regh;
74 1.1.20.2 yamt bus_size_t size;
75 1.1.20.2 yamt int ret = 0;
76 1.1.20.2 yamt
77 1.1.20.2 yamt if (PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE &&
78 1.1.20.4 yamt ((PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_SATA &&
79 1.1.20.4 yamt PCI_INTERFACE(pa->pa_class) == PCI_INTERFACE_SATA_AHCI) ||
80 1.1.20.4 yamt PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_RAID)) {
81 1.1.20.2 yamt /* check if the chip is in ahci mode */
82 1.1.20.2 yamt if (pci_mapreg_map(pa, AHCI_PCI_ABAR,
83 1.1.20.2 yamt PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0,
84 1.1.20.2 yamt ®t, ®h, NULL, &size) != 0)
85 1.1.20.5 yamt return 0;
86 1.1.20.5 yamt if (PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_SATA
87 1.1.20.5 yamt && PCI_INTERFACE(pa->pa_class) == PCI_INTERFACE_SATA_AHCI)
88 1.1.20.5 yamt ret = 3;
89 1.1.20.5 yamt else if (bus_space_read_4(regt, regh, AHCI_GHC) & AHCI_GHC_AE)
90 1.1.20.2 yamt ret = 3;
91 1.1.20.2 yamt bus_space_unmap(regt, regh, size);
92 1.1.20.5 yamt return ret;
93 1.1.20.2 yamt }
94 1.1.20.2 yamt
95 1.1.20.5 yamt return ret;
96 1.1.20.2 yamt }
97 1.1.20.2 yamt
98 1.1.20.2 yamt static void
99 1.1.20.2 yamt ahci_pci_attach(struct device *parent, struct device *self, void *aux)
100 1.1.20.2 yamt {
101 1.1.20.2 yamt struct pci_attach_args *pa = aux;
102 1.1.20.3 yamt struct ahci_pci_softc *psc = (struct ahci_pci_softc *)self;
103 1.1.20.3 yamt struct ahci_softc *sc = &psc->ah_sc;
104 1.1.20.2 yamt bus_size_t size;
105 1.1.20.2 yamt char devinfo[256];
106 1.1.20.2 yamt const char *intrstr;
107 1.1.20.2 yamt pci_intr_handle_t intrhandle;
108 1.1.20.2 yamt void *ih;
109 1.1.20.2 yamt
110 1.1.20.2 yamt if (pci_mapreg_map(pa, AHCI_PCI_ABAR,
111 1.1.20.2 yamt PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0,
112 1.1.20.2 yamt &sc->sc_ahcit, &sc->sc_ahcih, NULL, &size) != 0) {
113 1.1.20.2 yamt aprint_error("%s: can't map ahci registers\n", AHCINAME(sc));
114 1.1.20.2 yamt return;
115 1.1.20.2 yamt }
116 1.1.20.3 yamt psc->sc_pc = pa->pa_pc;
117 1.1.20.3 yamt psc->sc_pcitag = pa->pa_tag;
118 1.1.20.3 yamt
119 1.1.20.2 yamt pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
120 1.1.20.2 yamt aprint_naive(": AHCI disk controller\n");
121 1.1.20.2 yamt aprint_normal(": %s\n", devinfo);
122 1.1.20.2 yamt
123 1.1.20.2 yamt if (pci_intr_map(pa, &intrhandle) != 0) {
124 1.1.20.2 yamt aprint_error("%s: couldn't map interrupt\n", AHCINAME(sc));
125 1.1.20.2 yamt return;
126 1.1.20.2 yamt }
127 1.1.20.2 yamt intrstr = pci_intr_string(pa->pa_pc, intrhandle);
128 1.1.20.2 yamt ih = pci_intr_establish(pa->pa_pc, intrhandle, IPL_BIO, ahci_intr, sc);
129 1.1.20.2 yamt if (ih == NULL) {
130 1.1.20.2 yamt aprint_error("%s: couldn't establish interrupt", AHCINAME(sc));
131 1.1.20.2 yamt return;
132 1.1.20.2 yamt }
133 1.1.20.2 yamt aprint_normal("%s: interrupting at %s\n", AHCINAME(sc),
134 1.1.20.2 yamt intrstr ? intrstr : "unknown interrupt");
135 1.1.20.2 yamt sc->sc_dmat = pa->pa_dmat;
136 1.1.20.4 yamt
137 1.1.20.5 yamt if (PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_RAID) {
138 1.1.20.5 yamt AHCIDEBUG_PRINT(("%s: RAID mode\n", AHCINAME(sc)), DEBUG_PROBE);
139 1.1.20.4 yamt sc->sc_atac_capflags = ATAC_CAP_RAID;
140 1.1.20.5 yamt } else {
141 1.1.20.5 yamt AHCIDEBUG_PRINT(("%s: SATA mode\n", AHCINAME(sc)), DEBUG_PROBE);
142 1.1.20.5 yamt }
143 1.1.20.4 yamt
144 1.1.20.2 yamt ahci_attach(sc);
145 1.1.20.3 yamt
146 1.1.20.3 yamt if (!pmf_device_register(self, NULL, ahci_pci_resume))
147 1.1.20.3 yamt aprint_error_dev(self, "couldn't establish power handler\n");
148 1.1.20.3 yamt }
149 1.1.20.3 yamt
150 1.1.20.3 yamt static bool
151 1.1.20.6 yamt ahci_pci_resume(device_t dv PMF_FN_ARGS)
152 1.1.20.3 yamt {
153 1.1.20.3 yamt struct ahci_pci_softc *psc = device_private(dv);
154 1.1.20.3 yamt struct ahci_softc *sc = &psc->ah_sc;
155 1.1.20.3 yamt int s;
156 1.1.20.3 yamt
157 1.1.20.3 yamt s = splbio();
158 1.1.20.3 yamt ahci_reset(sc);
159 1.1.20.3 yamt ahci_setup_ports(sc);
160 1.1.20.3 yamt ahci_reprobe_drives(sc);
161 1.1.20.3 yamt ahci_enable_intrs(sc);
162 1.1.20.3 yamt splx(s);
163 1.1.20.3 yamt
164 1.1.20.3 yamt return true;
165 1.1.20.2 yamt }
166