1 /* 2 * Copyright (c) 2017 Semihalf. 3 * Copyright (c) 2017 Stormshield. 4 * All rights reserved. 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 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/stdint.h> 32 #include <sys/stddef.h> 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/kernel.h> 36 #include <sys/bus.h> 37 #include <sys/module.h> 38 #include <sys/sysctl.h> 39 #include <sys/rman.h> 40 #include <sys/unistd.h> 41 42 #include <machine/bus.h> 43 #include <machine/resource.h> 44 45 #include <dev/ofw/ofw_bus.h> 46 #include <dev/ofw/ofw_bus_subr.h> 47 48 #include <dev/ahci/ahci.h> 49 50 #define AHCI_VENDOR_SPECIFIC_0_ADDR 0xa0 51 #define AHCI_VENDOR_SPECIFIC_0_DATA 0xa4 52 53 #define AHCI_HC_DEVSTR "Marvell AHCI Controller" 54 #define AHCI_HC_VENDOR "Marvell" 55 56 static device_attach_t ahci_mv_fdt_attach; 57 58 static struct ofw_compat_data compatible_data[] = { 59 {"marvell,armada-380-ahci", true}, 60 {NULL, false} 61 }; 62 63 static void 64 ahci_mv_regret_config(struct ahci_controller *ctlr) 65 { 66 67 /* 68 * Enable the regret bit to allow the SATA unit to regret 69 * a request that didn't receive an acknowledge 70 * and a avoid deadlock 71 */ 72 ATA_OUTL(ctlr->r_mem, AHCI_VENDOR_SPECIFIC_0_ADDR, 0x4); 73 ATA_OUTL(ctlr->r_mem, AHCI_VENDOR_SPECIFIC_0_DATA, 0x80); 74 } 75 76 static int 77 ahci_mv_fdt_probe(device_t dev) 78 { 79 80 if (!ofw_bus_status_okay(dev)) 81 return (ENXIO); 82 83 if (!ofw_bus_search_compatible(dev, compatible_data)->ocd_data) 84 return (ENXIO); 85 86 device_set_desc(dev, AHCI_HC_DEVSTR); 87 88 return (BUS_PROBE_DEFAULT); 89 } 90 91 static int 92 ahci_mv_fdt_attach(device_t dev) 93 { 94 struct ahci_controller *ctlr; 95 int rc; 96 97 ctlr = device_get_softc(dev); 98 ctlr->dev = dev; 99 ctlr->r_rid = 0; 100 ctlr->quirks = AHCI_Q_2CH; 101 ctlr->numirqs = 1; 102 103 if (ofw_bus_is_compatible(dev, "marvell,armada-380-ahci")) 104 ctlr->quirks |= AHCI_Q_MRVL_SR_DEL; 105 106 /* Allocate memory for controller */ 107 ctlr->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 108 &ctlr->r_rid, RF_ACTIVE | RF_SHAREABLE); 109 if (ctlr->r_mem == NULL) { 110 device_printf(dev, "Failed to alloc memory for controller\n"); 111 return (ENOMEM); 112 } 113 114 /* Reset controller */ 115 rc = ahci_ctlr_reset(dev); 116 if (rc != 0) { 117 device_printf(dev, "Failed to reset controller\n"); 118 bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem); 119 return (ENXIO); 120 } 121 122 ahci_mv_regret_config(ctlr); 123 124 rc = ahci_attach(dev); 125 if (rc != 0) { 126 device_printf(dev, "Failed to initialize AHCI, with error %d\n", rc); 127 return (ENXIO); 128 } 129 130 return (0); 131 } 132 133 static device_method_t ahci_methods[] = { 134 /* Device interface */ 135 DEVMETHOD(device_probe, ahci_mv_fdt_probe), 136 DEVMETHOD(device_attach, ahci_mv_fdt_attach), 137 DEVMETHOD(device_detach, ahci_detach), 138 DEVMETHOD(bus_alloc_resource, ahci_alloc_resource), 139 DEVMETHOD(bus_release_resource, ahci_release_resource), 140 DEVMETHOD(bus_setup_intr, ahci_setup_intr), 141 DEVMETHOD(bus_teardown_intr, ahci_teardown_intr), 142 DEVMETHOD(bus_print_child, ahci_print_child), 143 DEVMETHOD(bus_child_location, ahci_child_location), 144 DEVMETHOD(bus_get_dma_tag, ahci_get_dma_tag), 145 DEVMETHOD_END 146 }; 147 148 static driver_t ahci_driver = { 149 "ahci", 150 ahci_methods, 151 sizeof(struct ahci_controller) 152 }; 153 154 DRIVER_MODULE(ahci_mv, simplebus, ahci_driver, NULL, NULL); 155 DRIVER_MODULE(ahci_mv, ofwbus, ahci_driver, NULL, NULL); 156