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