sni_emmc.c revision 1.5
1/* $NetBSD: sni_emmc.c,v 1.5 2020/03/25 22:15:53 nisimura Exp $ */ 2 3/*- 4 * Copyright (c) 2020 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Tohru Nishimura. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32/* 33 * Socionext SC2A11 SynQuacer eMMC driver 34 */ 35 36#include <sys/cdefs.h> 37__KERNEL_RCSID(0, "$NetBSD: sni_emmc.c,v 1.5 2020/03/25 22:15:53 nisimura Exp $"); 38 39#include <sys/param.h> 40#include <sys/bus.h> 41#include <sys/intr.h> 42#include <sys/device.h> 43#include <sys/errno.h> 44#include <sys/kernel.h> 45#include <sys/systm.h> 46 47#include <machine/endian.h> 48 49#include <dev/sdmmc/sdhcreg.h> 50#include <dev/sdmmc/sdhcvar.h> 51#include <dev/sdmmc/sdmmcvar.h> 52 53#include <dev/fdt/fdtvar.h> 54#include <dev/acpi/acpireg.h> 55#include <dev/acpi/acpivar.h> 56#include <dev/acpi/acpi_intr.h> 57 58static int sniemmc_fdt_match(device_t, struct cfdata *, void *); 59static void sniemmc_fdt_attach(device_t, device_t, void *); 60static int sniemmc_acpi_match(device_t, struct cfdata *, void *); 61static void sniemmc_acpi_attach(device_t, device_t, void *); 62 63struct sniemmc_softc { 64 struct sdhc_softc sc; 65 bus_space_tag_t sc_iot; 66 bus_space_handle_t sc_ioh; 67 bus_addr_t sc_iob; 68 bus_size_t sc_ios; 69 void *sc_ih; 70 struct sdhc_host *sc_hosts[1]; 71 bus_dmamap_t sc_dmamap; 72 bus_dma_segment_t sc_segs[1]; 73 kcondvar_t sc_cv; 74 int sc_phandle; 75}; 76 77CFATTACH_DECL_NEW(sniemmc_fdt, sizeof(struct sniemmc_softc), 78 sniemmc_fdt_match, sniemmc_fdt_attach, NULL, NULL); 79 80CFATTACH_DECL_NEW(sniemmc_acpi, sizeof(struct sniemmc_softc), 81 sniemmc_acpi_match, sniemmc_acpi_attach, NULL, NULL); 82 83static void sniemmc_attach_i(device_t); 84 85static int 86sniemmc_fdt_match(device_t parent, struct cfdata *match, void *aux) 87{ 88 static const char * compatible[] = { 89 "socionext,synquacer-sdhci", 90 "fujitsu,mb86s70-sdhci-3.0", 91 NULL 92 }; 93 struct fdt_attach_args * const faa = aux; 94 95 return of_match_compatible(faa->faa_phandle, compatible); 96} 97 98static void 99sniemmc_fdt_attach(device_t parent, device_t self, void *aux) 100{ 101 struct sniemmc_softc * const sc = device_private(self); 102 struct fdt_attach_args * const faa = aux; 103 prop_dictionary_t dict = device_properties(self); 104 const int phandle = faa->faa_phandle; 105 bus_space_handle_t ioh; 106 bus_addr_t addr; 107 bus_size_t size; 108 char intrstr[128]; 109 _Bool disable; 110 111 prop_dictionary_get_bool(dict, "disable", &disable); 112 if (disable) { 113 aprint_naive(": disabled\n"); 114 aprint_normal(": disabled\n"); 115 return; 116 } 117 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0 118 || bus_space_map(faa->faa_bst, addr, size, 0, &ioh) != 0) { 119 aprint_error(": unable to map device\n"); 120 return; 121 } 122 if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) { 123 aprint_error(": failed to decode interrupt\n"); 124 goto fail; 125 } 126 sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_SDMMC, 0, 127 sdhc_intr, &sc->sc); 128 if (sc->sc_ih == NULL) { 129 aprint_error_dev(self, "couldn't establish interrupt on %s\n", 130 intrstr); 131 goto fail; 132 } 133 134 aprint_naive("\n"); 135 aprint_normal_dev(self, "interrupting on %s\n", intrstr); 136 137 sc->sc.sc_dev = self; 138 sc->sc.sc_dmat = faa->faa_dmat; 139 sc->sc.sc_host = sc->sc_hosts; 140 sc->sc_phandle = phandle; 141 sc->sc_iot = faa->faa_bst; 142 sc->sc_ioh = ioh; 143 sc->sc_iob = addr; 144 sc->sc_ios = size; 145 146 config_defer(self, sniemmc_attach_i); 147 return; 148 fail: 149 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios); 150 return; 151} 152 153static int 154sniemmc_acpi_match(device_t parent, struct cfdata *match, void *aux) 155{ 156 static const char * compatible[] = { 157 "SCX0002", 158 NULL 159 }; 160 struct acpi_attach_args *aa = aux; 161 162 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE) 163 return 0; 164 return acpi_match_hid(aa->aa_node->ad_devinfo, compatible); 165} 166 167static void 168sniemmc_acpi_attach(device_t parent, device_t self, void *aux) 169{ 170 struct sniemmc_softc * const sc = device_private(self); 171 struct acpi_attach_args *aa = aux; 172 bus_space_handle_t ioh; 173 struct acpi_resources res; 174 struct acpi_mem *mem; 175 struct acpi_irq *irq; 176 ACPI_STATUS rv; 177 178 rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS", 179 &res, &acpi_resource_parse_ops_default); 180 if (ACPI_FAILURE(rv)) 181 return; 182 mem = acpi_res_mem(&res, 0); 183 irq = acpi_res_irq(&res, 0); 184 if (mem == NULL || irq == NULL || mem->ar_length == 0) { 185 aprint_error(": incomplete resources\n"); 186 return; 187 } 188 if (bus_space_map(aa->aa_memt, mem->ar_base, mem->ar_length, 0, 189 &ioh)) { 190 aprint_error(": couldn't map registers\n"); 191 return; 192 } 193 sc->sc_ih = acpi_intr_establish(self, 194 (uint64_t)(uintptr_t)aa->aa_node->ad_handle, 195 IPL_BIO, false, sdhc_intr, &sc->sc, device_xname(self)); 196 if (sc->sc_ih == NULL) { 197 aprint_error_dev(self, "couldn't establish interrupt\n"); 198 goto fail; 199 } 200 201 aprint_naive("\n"); 202 203 sc->sc.sc_dev = self; 204 sc->sc.sc_dmat = aa->aa_dmat; 205 sc->sc.sc_host = sc->sc_hosts; 206 sc->sc_iot = aa->aa_memt; 207 sc->sc_ioh = ioh; 208 sc->sc_ios = mem->ar_length; 209 210 config_defer(self, sniemmc_attach_i); 211 212 acpi_resource_cleanup(&res); 213 return; 214 fail: 215 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios); 216 acpi_resource_cleanup(&res); 217 return; 218} 219 220static void 221sniemmc_attach_i(device_t self) 222{ 223 struct sniemmc_softc * const sc = device_private(self); 224 int error; 225 226 sc->sc.sc_flags = 0; 227 sc->sc.sc_flags |= SDHC_FLAG_32BIT_ACCESS; 228 sc->sc.sc_clkbase = 50000; /* Default to 50MHz */ 229 230 aprint_normal_dev(sc->sc.sc_dev, "Socionext eMMC controller\n"); 231#if 0 232 error = sdhc_host_found(&sc->sc, sc->sc_iot, sc->sc_ioh, sc->sc_ios); 233#endif 234 error = 0; 235 if (error) { 236 aprint_error_dev(self, "couldn't intialize host, error=%d\n", error); 237 goto fail; 238 } 239 return; 240 fail: 241 if (sc->sc_phandle) 242 fdtbus_intr_disestablish(sc->sc_phandle, sc->sc_ih); 243 else 244 acpi_intr_disestablish(sc->sc_ih); 245 sc->sc_ih = NULL; 246 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios); 247 return; 248} 249