1 /* $NetBSD: ahcisata_fdt.c,v 1.1 2018/09/03 23:15:12 jmcneill Exp $ */ 2 3 /*- 4 * Copyright (c) 2018 Jared McNeill <jmcneill@invisible.ca> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 31 __KERNEL_RCSID(0, "$NetBSD: ahcisata_fdt.c,v 1.1 2018/09/03 23:15:12 jmcneill Exp $"); 32 33 #include <sys/param.h> 34 #include <sys/bus.h> 35 #include <sys/device.h> 36 #include <sys/intr.h> 37 #include <sys/systm.h> 38 39 #include <dev/ata/atavar.h> 40 #include <dev/ic/ahcisatavar.h> 41 42 #include <dev/fdt/fdtvar.h> 43 44 static const char * compatible[] = { 45 "snps,dwc-ahci", 46 "generic-ahci", 47 NULL 48 }; 49 50 static int 51 ahcisata_fdt_match(device_t parent, cfdata_t cf, void *aux) 52 { 53 struct fdt_attach_args * const faa = aux; 54 55 return of_match_compatible(faa->faa_phandle, compatible); 56 } 57 58 static void 59 ahcisata_fdt_attach(device_t parent, device_t self, void *aux) 60 { 61 struct ahci_softc * const sc = device_private(self); 62 struct fdt_attach_args * const faa = aux; 63 const int phandle = faa->faa_phandle; 64 struct fdtbus_reset *rst; 65 struct clk *clk; 66 char intrstr[128]; 67 bus_addr_t addr; 68 bus_size_t size; 69 int i; 70 71 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 72 aprint_error(": couldn't get registers\n"); 73 return; 74 } 75 76 sc->sc_atac.atac_dev = self; 77 sc->sc_dmat = faa->faa_dmat; 78 sc->sc_ahcit = faa->faa_bst; 79 sc->sc_ahcis = size; 80 if (bus_space_map(sc->sc_ahcit, addr, size, 0, &sc->sc_ahcih) != 0) { 81 aprint_error(": couldn't map registers\n"); 82 return; 83 } 84 if (of_getprop_uint32(phandle, "ports-implemented", &sc->sc_ahci_ports) != 0) 85 sc->sc_save_init_data = true; 86 87 if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) { 88 aprint_error(": failed to decode interrupt\n"); 89 return; 90 } 91 92 for (i = 0; (clk = fdtbus_clock_get_index(phandle, i)) != NULL; i++) 93 if (clk_enable(clk) != 0) { 94 aprint_error(": couldn't enable clock #%d\n", i); 95 return; 96 } 97 for (i = 0; (rst = fdtbus_reset_get_index(phandle, i)) != NULL; i++) 98 if (fdtbus_reset_deassert(rst) != 0) { 99 aprint_error(": couldn't de-assert reset #%d\n", i); 100 return; 101 } 102 103 aprint_naive("\n"); 104 aprint_normal(": AHCI SATA controller\n"); 105 106 if (fdtbus_intr_establish(phandle, 0, IPL_BIO, 0, ahci_intr, sc) == NULL) { 107 aprint_error_dev(self, "failed to establish interrupt on %s\n", intrstr); 108 return; 109 } 110 aprint_normal_dev(self, "interrupting on %s\n", intrstr); 111 112 ahci_attach(sc); 113 } 114 115 CFATTACH_DECL_NEW(ahcisata_fdt, sizeof(struct ahci_softc), 116 ahcisata_fdt_match, ahcisata_fdt_attach, NULL, NULL); 117