| 1 | /* $NetBSD: sdhc_acpi.c,v 1.3 2016/08/11 01:54:30 nonaka Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 2016 Kimihiro Nonaka <nonaka@NetBSD.org> |
| 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. The name of the author may not be used to endorse or promote products |
| 13 | * derived from this software without specific prior written permission. |
| 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, |
| 20 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 22 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 23 | * 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 | __KERNEL_RCSID(0, "$NetBSD: sdhc_acpi.c,v 1.3 2016/08/11 01:54:30 nonaka Exp $" ); |
| 30 | |
| 31 | #include <sys/param.h> |
| 32 | #include <sys/device.h> |
| 33 | #include <sys/systm.h> |
| 34 | #include <sys/kmem.h> |
| 35 | |
| 36 | #include <dev/acpi/acpireg.h> |
| 37 | #include <dev/acpi/acpivar.h> |
| 38 | |
| 39 | #include <dev/sdmmc/sdhcreg.h> |
| 40 | #include <dev/sdmmc/sdhcvar.h> |
| 41 | #include <dev/sdmmc/sdmmcvar.h> |
| 42 | |
| 43 | #define _COMPONENT ACPI_RESOURCE_COMPONENT |
| 44 | ACPI_MODULE_NAME ("sdhc_acpi" ) |
| 45 | |
| 46 | static int sdhc_acpi_match(device_t, cfdata_t, void *); |
| 47 | static void sdhc_acpi_attach(device_t, device_t, void *); |
| 48 | static int sdhc_acpi_detach(device_t, int); |
| 49 | static bool sdhc_acpi_resume(device_t, const pmf_qual_t *); |
| 50 | |
| 51 | struct sdhc_acpi_softc { |
| 52 | struct sdhc_softc sc; |
| 53 | int sc_irq; |
| 54 | |
| 55 | ACPI_HANDLE sc_crs, sc_srs; |
| 56 | ACPI_BUFFER sc_crs_buffer; |
| 57 | }; |
| 58 | |
| 59 | CFATTACH_DECL_NEW(sdhc_acpi, sizeof(struct sdhc_acpi_softc), |
| 60 | sdhc_acpi_match, sdhc_acpi_attach, sdhc_acpi_detach, NULL); |
| 61 | |
| 62 | static uint32_t sdhc_acpi_intr(void *); |
| 63 | |
| 64 | static const char * const sdhc_acpi_ids[] = { |
| 65 | "80860F14" , |
| 66 | "80860F16" , |
| 67 | NULL |
| 68 | }; |
| 69 | |
| 70 | static int |
| 71 | sdhc_acpi_match(device_t parent, cfdata_t match, void *opaque) |
| 72 | { |
| 73 | struct acpi_attach_args *aa = opaque; |
| 74 | |
| 75 | if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE) |
| 76 | return 0; |
| 77 | |
| 78 | return acpi_match_hid(aa->aa_node->ad_devinfo, sdhc_acpi_ids); |
| 79 | } |
| 80 | |
| 81 | static void |
| 82 | sdhc_acpi_attach(device_t parent, device_t self, void *opaque) |
| 83 | { |
| 84 | struct sdhc_acpi_softc *sc = device_private(self); |
| 85 | struct acpi_attach_args *aa = opaque; |
| 86 | struct acpi_resources res; |
| 87 | struct acpi_mem *mem; |
| 88 | struct acpi_irq *irq; |
| 89 | bus_space_handle_t memh; |
| 90 | ACPI_STATUS rv; |
| 91 | |
| 92 | sc->sc.sc_dev = self; |
| 93 | sc->sc.sc_dmat = aa->aa_dmat; |
| 94 | sc->sc.sc_host = NULL; |
| 95 | sc->sc_irq = -1; |
| 96 | |
| 97 | rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS" , |
| 98 | &res, &acpi_resource_parse_ops_default); |
| 99 | if (ACPI_FAILURE(rv)) |
| 100 | return; |
| 101 | |
| 102 | AcpiGetHandle(aa->aa_node->ad_handle, "_CRS" , &sc->sc_crs); |
| 103 | AcpiGetHandle(aa->aa_node->ad_handle, "_SRS" , &sc->sc_srs); |
| 104 | if (sc->sc_crs && sc->sc_srs) { |
| 105 | /* XXX Why need this? */ |
| 106 | sc->sc_crs_buffer.Pointer = NULL; |
| 107 | sc->sc_crs_buffer.Length = ACPI_ALLOCATE_LOCAL_BUFFER; |
| 108 | rv = AcpiGetCurrentResources(sc->sc_crs, &sc->sc_crs_buffer); |
| 109 | if (ACPI_FAILURE(rv)) |
| 110 | sc->sc_crs = sc->sc_srs = NULL; |
| 111 | } |
| 112 | |
| 113 | mem = acpi_res_mem(&res, 0); |
| 114 | irq = acpi_res_irq(&res, 0); |
| 115 | if (mem == NULL || irq == NULL) { |
| 116 | aprint_error_dev(self, "incomplete resources\n" ); |
| 117 | goto cleanup; |
| 118 | } |
| 119 | |
| 120 | if (bus_space_map(aa->aa_memt, mem->ar_base, mem->ar_length, 0, |
| 121 | &memh)) { |
| 122 | aprint_error_dev(self, "couldn't map registers\n" ); |
| 123 | goto cleanup; |
| 124 | } |
| 125 | |
| 126 | /* XXX acpi_intr_establish? */ |
| 127 | rv = AcpiOsInstallInterruptHandler(irq->ar_irq, sdhc_acpi_intr, sc); |
| 128 | if (ACPI_FAILURE(rv)) { |
| 129 | aprint_error_dev(self, |
| 130 | "couldn't establish interrupt handler\n" ); |
| 131 | goto cleanup; |
| 132 | } |
| 133 | sc->sc_irq = irq->ar_irq; |
| 134 | |
| 135 | sc->sc.sc_host = kmem_zalloc(sizeof(struct sdhc_host *), KM_NOSLEEP); |
| 136 | if (sc->sc.sc_host == NULL) { |
| 137 | aprint_error_dev(self, "couldn't alloc memory\n" ); |
| 138 | goto cleanup; |
| 139 | } |
| 140 | |
| 141 | /* Enable DMA transfer */ |
| 142 | sc->sc.sc_flags |= SDHC_FLAG_USE_DMA; |
| 143 | |
| 144 | if (sdhc_host_found(&sc->sc, aa->aa_memt, memh, mem->ar_length) != 0) { |
| 145 | aprint_error_dev(self, "couldn't initialize host\n" ); |
| 146 | goto cleanup; |
| 147 | } |
| 148 | |
| 149 | if (!pmf_device_register1(self, sdhc_suspend, sdhc_acpi_resume, |
| 150 | sdhc_shutdown)) { |
| 151 | aprint_error_dev(self, "couldn't establish powerhook\n" ); |
| 152 | } |
| 153 | |
| 154 | acpi_resource_cleanup(&res); |
| 155 | return; |
| 156 | |
| 157 | cleanup: |
| 158 | acpi_resource_cleanup(&res); |
| 159 | if (sc->sc_crs_buffer.Pointer) |
| 160 | ACPI_FREE(sc->sc_crs_buffer.Pointer); |
| 161 | if (sc->sc_irq >= 0) |
| 162 | /* XXX acpi_intr_disestablish? */ |
| 163 | AcpiOsRemoveInterruptHandler(sc->sc_irq, sdhc_acpi_intr); |
| 164 | if (sc->sc.sc_host != NULL) |
| 165 | kmem_free(sc->sc.sc_host, sizeof(struct sdhc_host *)); |
| 166 | } |
| 167 | |
| 168 | static int |
| 169 | sdhc_acpi_detach(device_t self, int flags) |
| 170 | { |
| 171 | struct sdhc_acpi_softc *sc = device_private(self); |
| 172 | int rv; |
| 173 | |
| 174 | pmf_device_deregister(self); |
| 175 | |
| 176 | if (sc->sc_crs_buffer.Pointer) |
| 177 | ACPI_FREE(sc->sc_crs_buffer.Pointer); |
| 178 | |
| 179 | rv = sdhc_detach(&sc->sc, flags); |
| 180 | if (rv) |
| 181 | return rv; |
| 182 | |
| 183 | if (sc->sc_irq >= 0) |
| 184 | /* XXX acpi_intr_disestablish? */ |
| 185 | AcpiOsRemoveInterruptHandler(sc->sc_irq, sdhc_acpi_intr); |
| 186 | |
| 187 | if (sc->sc.sc_host != NULL) |
| 188 | kmem_free(sc->sc.sc_host, sizeof(struct sdhc_host *)); |
| 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | static bool |
| 194 | sdhc_acpi_resume(device_t self, const pmf_qual_t *qual) |
| 195 | { |
| 196 | struct sdhc_acpi_softc *sc = device_private(self); |
| 197 | ACPI_STATUS rv; |
| 198 | |
| 199 | if (sc->sc_crs && sc->sc_srs) { |
| 200 | rv = AcpiSetCurrentResources(sc->sc_srs, &sc->sc_crs_buffer); |
| 201 | if (ACPI_FAILURE(rv)) |
| 202 | printf("%s: _SRS failed: %s\n" , |
| 203 | device_xname(self), AcpiFormatException(rv)); |
| 204 | } |
| 205 | |
| 206 | return sdhc_resume(self, qual); |
| 207 | } |
| 208 | |
| 209 | static uint32_t |
| 210 | sdhc_acpi_intr(void *context) |
| 211 | { |
| 212 | struct sdhc_acpi_softc *sc = context; |
| 213 | |
| 214 | if (!sdhc_intr(&sc->sc)) |
| 215 | return ACPI_INTERRUPT_NOT_HANDLED; |
| 216 | return ACPI_INTERRUPT_HANDLED; |
| 217 | } |
| 218 | |