xref: /freebsd/sys/dev/ahci/ahci_generic.c (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
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 <sys/param.h>
31 #include <sys/module.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/bus.h>
35 #include <sys/conf.h>
36 #include <sys/endian.h>
37 #include <sys/malloc.h>
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 #include <sys/rman.h>
41 
42 #include <machine/bus.h>
43 #include <machine/resource.h>
44 
45 #include <dev/ahci/ahci.h>
46 
47 #include <dev/ofw/ofw_bus.h>
48 #include <dev/ofw/ofw_bus_subr.h>
49 
50 static struct ofw_compat_data compat_data[] = {
51 	{"generic-ahci", 	1},
52 	{"snps,dwc-ahci",	1},
53 	{NULL,			0}
54 };
55 
56 static int
57 ahci_gen_ctlr_reset(device_t dev)
58 {
59 
60 	return ahci_ctlr_reset(dev);
61 }
62 
63 static int
64 ahci_probe(device_t dev)
65 {
66 
67 	if (!ofw_bus_status_okay(dev))
68 		return (ENXIO);
69 
70 	if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
71 		return (ENXIO);
72 
73 	device_set_desc_copy(dev, "AHCI SATA controller");
74 	return (BUS_PROBE_DEFAULT);
75 }
76 
77 static int
78 ahci_gen_attach(device_t dev)
79 {
80 	struct ahci_controller *ctlr = device_get_softc(dev);
81 	int	error;
82 
83 	ctlr->r_rid = 0;
84 	ctlr->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &ctlr->r_rid,
85 	    RF_ACTIVE);
86 	if (ctlr->r_mem == NULL)
87 		return (ENXIO);
88 
89 	/* Setup controller defaults. */
90 	ctlr->numirqs = 1;
91 
92 	/* Reset controller */
93 	if ((error = ahci_gen_ctlr_reset(dev)) == 0)
94 		error = ahci_attach(dev);
95 
96 	if (error != 0) {
97 		if (ctlr->r_mem != NULL)
98 			bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid,
99 			    ctlr->r_mem);
100 	}
101 	return error;
102 }
103 
104 static int
105 ahci_gen_detach(device_t dev)
106 {
107 
108 	ahci_detach(dev);
109 	return (0);
110 }
111 
112 static devclass_t ahci_gen_devclass;
113 static device_method_t ahci_methods[] = {
114 	DEVMETHOD(device_probe,     ahci_probe),
115 	DEVMETHOD(device_attach,    ahci_gen_attach),
116 	DEVMETHOD(device_detach,    ahci_gen_detach),
117 	DEVMETHOD(bus_print_child,  ahci_print_child),
118 	DEVMETHOD(bus_alloc_resource,       ahci_alloc_resource),
119 	DEVMETHOD(bus_release_resource,     ahci_release_resource),
120 	DEVMETHOD(bus_setup_intr,   ahci_setup_intr),
121 	DEVMETHOD(bus_teardown_intr,ahci_teardown_intr),
122 	DEVMETHOD(bus_child_location_str, ahci_child_location_str),
123 	DEVMETHOD(bus_get_dma_tag,  ahci_get_dma_tag),
124 	DEVMETHOD_END
125 };
126 static driver_t ahci_driver = {
127 	"ahci",
128 	ahci_methods,
129 	sizeof(struct ahci_controller)
130 };
131 DRIVER_MODULE(ahci, simplebus, ahci_driver, ahci_gen_devclass, NULL, NULL);
132