2 * Copyright (c) 2017 Semihalf.
3 * Copyright (c) 2017 Stormshield.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
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>
37 #include <sys/module.h>
38 #include <sys/sysctl.h>
40 #include <sys/unistd.h>
42 #include <machine/bus.h>
43 #include <machine/resource.h>
45 #include <dev/ofw/ofw_bus.h>
46 #include <dev/ofw/ofw_bus_subr.h>
48 #include <dev/ahci/ahci.h>
50 #define AHCI_VENDOR_SPECIFIC_0_ADDR 0xa0
51 #define AHCI_VENDOR_SPECIFIC_0_DATA 0xa4
53 #define AHCI_HC_DEVSTR "Marvell AHCI Controller"
54 #define AHCI_HC_VENDOR "Marvell"
56 static device_attach_t ahci_mv_fdt_attach;
58 static struct ofw_compat_data compatible_data[] = {
59 {"marvell,armada-380-ahci", true},
64 ahci_mv_regret_config(struct ahci_controller *ctlr)
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
72 ATA_OUTL(ctlr->r_mem, AHCI_VENDOR_SPECIFIC_0_ADDR, 0x4);
73 ATA_OUTL(ctlr->r_mem, AHCI_VENDOR_SPECIFIC_0_DATA, 0x80);
77 ahci_mv_fdt_probe(device_t dev)
80 if (!ofw_bus_status_okay(dev))
83 if (!ofw_bus_search_compatible(dev, compatible_data)->ocd_data)
86 device_set_desc(dev, AHCI_HC_DEVSTR);
88 return (BUS_PROBE_DEFAULT);
92 ahci_mv_fdt_attach(device_t dev)
94 struct ahci_controller *ctlr;
97 ctlr = device_get_softc(dev);
100 ctlr->quirks = AHCI_Q_2CH;
103 if (ofw_bus_is_compatible(dev, "marvell,armada-380-ahci"))
104 ctlr->quirks |= AHCI_Q_MRVL_SR_DEL;
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");
114 /* Reset controller */
115 rc = ahci_ctlr_reset(dev);
117 device_printf(dev, "Failed to reset controller\n");
118 bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
122 ahci_mv_regret_config(ctlr);
124 rc = ahci_attach(dev);
126 device_printf(dev, "Failed to initialize AHCI, with error %d\n", rc);
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_str, ahci_child_location_str),
144 DEVMETHOD(bus_get_dma_tag, ahci_get_dma_tag),
148 static devclass_t ahci_devclass;
149 static driver_t ahci_driver = {
152 sizeof(struct ahci_controller)
155 DRIVER_MODULE(ahci, simplebus, ahci_driver, ahci_devclass, NULL, NULL);
156 DRIVER_MODULE(ahci, ofwbus, ahci_driver, ahci_devclass, NULL, NULL);