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