xref: /freebsd/sys/dev/ahci/ahci_generic.c (revision cbd30a72ca196976c1c700400ecd424baa1b9c16)
1 /*-
2  * Copyright (c) 2009-2012 Alexander Motin <mav@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification, immediately at the beginning of the file.
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  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include "opt_acpi.h"
31 #include "opt_platform.h"
32 
33 #include <sys/param.h>
34 #include <sys/module.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/bus.h>
38 #include <sys/conf.h>
39 #include <sys/endian.h>
40 #include <sys/malloc.h>
41 #include <sys/lock.h>
42 #include <sys/mutex.h>
43 #include <sys/rman.h>
44 
45 #include <machine/bus.h>
46 #include <machine/resource.h>
47 
48 #include <dev/ahci/ahci.h>
49 
50 #ifdef DEV_ACPI
51 #include <contrib/dev/acpica/include/acpi.h>
52 #include <dev/acpica/acpivar.h>
53 
54 #include <dev/pci/pcivar.h>
55 #include <dev/pci/pcireg.h>
56 #endif
57 
58 #ifdef FDT
59 #include <dev/ofw/ofw_bus.h>
60 #include <dev/ofw/ofw_bus_subr.h>
61 
62 static struct ofw_compat_data compat_data[] = {
63 	{"generic-ahci", 	1},
64 	{"snps,dwc-ahci",	1},
65 	{NULL,			0}
66 };
67 
68 static int
69 ahci_fdt_probe(device_t dev)
70 {
71 
72 	if (!ofw_bus_status_okay(dev))
73 		return (ENXIO);
74 
75 	if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
76 		return (ENXIO);
77 
78 	device_set_desc_copy(dev, "AHCI SATA controller");
79 	return (BUS_PROBE_DEFAULT);
80 }
81 #endif
82 
83 #ifdef DEV_ACPI
84 static int
85 ahci_acpi_probe(device_t dev)
86 {
87 	ACPI_HANDLE h;
88 
89 	if ((h = acpi_get_handle(dev)) == NULL)
90 		return (ENXIO);
91 
92 	if (pci_get_class(dev) == PCIC_STORAGE &&
93 	    pci_get_subclass(dev) == PCIS_STORAGE_SATA &&
94 	    pci_get_progif(dev) == PCIP_STORAGE_SATA_AHCI_1_0) {
95 		device_set_desc_copy(dev, "AHCI SATA controller");
96 		return (BUS_PROBE_DEFAULT);
97 	}
98 
99 	return (ENXIO);
100 }
101 #endif
102 
103 static int
104 ahci_gen_ctlr_reset(device_t dev)
105 {
106 
107 	return ahci_ctlr_reset(dev);
108 }
109 
110 static int
111 ahci_gen_attach(device_t dev)
112 {
113 	struct ahci_controller *ctlr = device_get_softc(dev);
114 	int	error;
115 
116 	ctlr->r_rid = 0;
117 	ctlr->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &ctlr->r_rid,
118 	    RF_ACTIVE);
119 	if (ctlr->r_mem == NULL)
120 		return (ENXIO);
121 
122 	/* Setup controller defaults. */
123 	ctlr->numirqs = 1;
124 
125 	/* Reset controller */
126 	if ((error = ahci_gen_ctlr_reset(dev)) == 0)
127 		error = ahci_attach(dev);
128 
129 	if (error != 0) {
130 		if (ctlr->r_mem != NULL)
131 			bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid,
132 			    ctlr->r_mem);
133 	}
134 	return error;
135 }
136 
137 static int
138 ahci_gen_detach(device_t dev)
139 {
140 
141 	ahci_detach(dev);
142 	return (0);
143 }
144 
145 #ifdef FDT
146 static devclass_t ahci_gen_fdt_devclass;
147 static device_method_t ahci_fdt_methods[] = {
148 	DEVMETHOD(device_probe,     ahci_fdt_probe),
149 	DEVMETHOD(device_attach,    ahci_gen_attach),
150 	DEVMETHOD(device_detach,    ahci_gen_detach),
151 	DEVMETHOD(bus_print_child,  ahci_print_child),
152 	DEVMETHOD(bus_alloc_resource,       ahci_alloc_resource),
153 	DEVMETHOD(bus_release_resource,     ahci_release_resource),
154 	DEVMETHOD(bus_setup_intr,   ahci_setup_intr),
155 	DEVMETHOD(bus_teardown_intr,ahci_teardown_intr),
156 	DEVMETHOD(bus_child_location_str, ahci_child_location_str),
157 	DEVMETHOD(bus_get_dma_tag,  ahci_get_dma_tag),
158 	DEVMETHOD_END
159 };
160 static driver_t ahci_fdt_driver = {
161 	"ahci",
162 	ahci_fdt_methods,
163 	sizeof(struct ahci_controller)
164 };
165 DRIVER_MODULE(ahci_fdt, simplebus, ahci_fdt_driver, ahci_gen_fdt_devclass,
166     NULL, NULL);
167 #endif
168 
169 #ifdef DEV_ACPI
170 static devclass_t ahci_gen_acpi_devclass;
171 static device_method_t ahci_acpi_methods[] = {
172 	DEVMETHOD(device_probe,     ahci_acpi_probe),
173 	DEVMETHOD(device_attach,    ahci_gen_attach),
174 	DEVMETHOD(device_detach,    ahci_gen_detach),
175 	DEVMETHOD(bus_print_child,  ahci_print_child),
176 	DEVMETHOD(bus_alloc_resource,       ahci_alloc_resource),
177 	DEVMETHOD(bus_release_resource,     ahci_release_resource),
178 	DEVMETHOD(bus_setup_intr,   ahci_setup_intr),
179 	DEVMETHOD(bus_teardown_intr,ahci_teardown_intr),
180 	DEVMETHOD(bus_child_location_str, ahci_child_location_str),
181 	DEVMETHOD(bus_get_dma_tag,  ahci_get_dma_tag),
182 	DEVMETHOD_END
183 };
184 static driver_t ahci_acpi_driver = {
185 	"ahci",
186 	ahci_acpi_methods,
187 	sizeof(struct ahci_controller)
188 };
189 DRIVER_MODULE(ahci_acpi, acpi, ahci_acpi_driver, ahci_gen_acpi_devclass,
190     NULL, NULL);
191 #endif
192