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 #include "opt_acpi.h" 29 #include "opt_platform.h" 30 31 #include <sys/param.h> 32 #include <sys/module.h> 33 #include <sys/systm.h> 34 #include <sys/kernel.h> 35 #include <sys/bus.h> 36 #include <sys/conf.h> 37 #include <sys/endian.h> 38 #include <sys/malloc.h> 39 #include <sys/lock.h> 40 #include <sys/mutex.h> 41 #include <sys/rman.h> 42 43 #include <machine/bus.h> 44 #include <machine/resource.h> 45 46 #include <dev/ahci/ahci.h> 47 48 #ifdef DEV_ACPI 49 #include <contrib/dev/acpica/include/acpi.h> 50 #include <dev/acpica/acpivar.h> 51 52 #include <dev/pci/pcivar.h> 53 #include <dev/pci/pcireg.h> 54 #endif 55 56 #ifdef FDT 57 #include <dev/ofw/ofw_bus.h> 58 #include <dev/ofw/ofw_bus_subr.h> 59 60 static struct ofw_compat_data compat_data[] = { 61 {"generic-ahci", 1}, 62 {"snps,dwc-ahci", 1}, 63 {"marvell,armada-3700-ahci", 1}, 64 {NULL, 0} 65 }; 66 67 static int 68 ahci_fdt_probe(device_t dev) 69 { 70 struct ahci_controller *ctlr = device_get_softc(dev); 71 phandle_t node; 72 73 if (!ofw_bus_status_okay(dev)) 74 return (ENXIO); 75 76 if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data) 77 return (ENXIO); 78 79 device_set_desc_copy(dev, "AHCI SATA controller"); 80 node = ofw_bus_get_node(dev); 81 ctlr->dma_coherent = OF_hasprop(node, "dma-coherent"); 82 return (BUS_PROBE_DEFAULT); 83 } 84 #endif 85 86 #ifdef DEV_ACPI 87 88 static const struct ahci_acpi_quirk { 89 const char* hid; 90 u_int quirks; 91 } ahci_acpi_quirks[] = { 92 { "NXP0004", AHCI_Q_NOPMP }, 93 { NULL, 0 } 94 }; 95 96 97 static int 98 ahci_acpi_probe(device_t dev) 99 { 100 struct ahci_controller *ctlr = device_get_softc(dev); 101 ACPI_HANDLE h; 102 int i; 103 104 if ((h = acpi_get_handle(dev)) == NULL) 105 return (ENXIO); 106 107 if (pci_get_class(dev) == PCIC_STORAGE && 108 pci_get_subclass(dev) == PCIS_STORAGE_SATA && 109 pci_get_progif(dev) == PCIP_STORAGE_SATA_AHCI_1_0) { 110 device_set_desc_copy(dev, "AHCI SATA controller"); 111 if (ACPI_FAILURE(acpi_GetInteger(h, "_CCA", 112 &ctlr->dma_coherent))) 113 ctlr->dma_coherent = 0; 114 if (bootverbose) 115 device_printf(dev, "Bus is%s cache-coherent\n", 116 ctlr->dma_coherent ? "" : " not"); 117 for (i = 0; ahci_acpi_quirks[i].hid != NULL; i++) { 118 if (acpi_MatchHid(h, ahci_acpi_quirks[i].hid) != ACPI_MATCHHID_NOMATCH) { 119 ctlr->quirks = ahci_acpi_quirks[i].quirks; 120 break; 121 } 122 } 123 return (BUS_PROBE_DEFAULT); 124 } 125 126 return (ENXIO); 127 } 128 #endif 129 130 static int 131 ahci_gen_ctlr_reset(device_t dev) 132 { 133 134 return ahci_ctlr_reset(dev); 135 } 136 137 static int 138 ahci_gen_attach(device_t dev) 139 { 140 struct ahci_controller *ctlr = device_get_softc(dev); 141 int error; 142 143 ctlr->r_rid = 0; 144 ctlr->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &ctlr->r_rid, 145 RF_ACTIVE); 146 if (ctlr->r_mem == NULL) 147 return (ENXIO); 148 149 /* Setup controller defaults. */ 150 ctlr->numirqs = 1; 151 152 /* Reset controller */ 153 if ((error = ahci_gen_ctlr_reset(dev)) == 0) 154 error = ahci_attach(dev); 155 156 if (error != 0) { 157 if (ctlr->r_mem != NULL) 158 bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, 159 ctlr->r_mem); 160 } 161 return error; 162 } 163 164 static int 165 ahci_gen_detach(device_t dev) 166 { 167 168 ahci_detach(dev); 169 return (0); 170 } 171 172 #ifdef FDT 173 static device_method_t ahci_fdt_methods[] = { 174 DEVMETHOD(device_probe, ahci_fdt_probe), 175 DEVMETHOD(device_attach, ahci_gen_attach), 176 DEVMETHOD(device_detach, ahci_gen_detach), 177 DEVMETHOD(bus_print_child, ahci_print_child), 178 DEVMETHOD(bus_alloc_resource, ahci_alloc_resource), 179 DEVMETHOD(bus_release_resource, ahci_release_resource), 180 DEVMETHOD(bus_setup_intr, ahci_setup_intr), 181 DEVMETHOD(bus_teardown_intr,ahci_teardown_intr), 182 DEVMETHOD(bus_child_location, ahci_child_location), 183 DEVMETHOD(bus_get_dma_tag, ahci_get_dma_tag), 184 DEVMETHOD_END 185 }; 186 static driver_t ahci_fdt_driver = { 187 "ahci", 188 ahci_fdt_methods, 189 sizeof(struct ahci_controller) 190 }; 191 DRIVER_MODULE(ahci_fdt, simplebus, ahci_fdt_driver, NULL, NULL); 192 #endif 193 194 #ifdef DEV_ACPI 195 static device_method_t ahci_acpi_methods[] = { 196 DEVMETHOD(device_probe, ahci_acpi_probe), 197 DEVMETHOD(device_attach, ahci_gen_attach), 198 DEVMETHOD(device_detach, ahci_gen_detach), 199 DEVMETHOD(bus_print_child, ahci_print_child), 200 DEVMETHOD(bus_alloc_resource, ahci_alloc_resource), 201 DEVMETHOD(bus_release_resource, ahci_release_resource), 202 DEVMETHOD(bus_setup_intr, ahci_setup_intr), 203 DEVMETHOD(bus_teardown_intr,ahci_teardown_intr), 204 DEVMETHOD(bus_child_location, ahci_child_location), 205 DEVMETHOD(bus_get_dma_tag, ahci_get_dma_tag), 206 DEVMETHOD_END 207 }; 208 static driver_t ahci_acpi_driver = { 209 "ahci", 210 ahci_acpi_methods, 211 sizeof(struct ahci_controller) 212 }; 213 DRIVER_MODULE(ahci_acpi, acpi, ahci_acpi_driver, NULL, NULL); 214 #endif 215