bcm2838_emmc2_acpi.c revision 1.2
1/*	$NetBSD: bcm2838_emmc2_acpi.c,v 1.2 2021/08/08 10:59:27 jmcneill Exp $	*/
2
3/*
4 * Copyright (c) 2021 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. 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: bcm2838_emmc2_acpi.c,v 1.2 2021/08/08 10:59:27 jmcneill 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#include <dev/acpi/acpi_intr.h>
39
40#include <dev/sdmmc/sdhcreg.h>
41#include <dev/sdmmc/sdhcvar.h>
42#include <dev/sdmmc/sdmmcvar.h>
43
44#define _COMPONENT	ACPI_RESOURCE_COMPONENT
45ACPI_MODULE_NAME	("bcmemmc2_acpi")
46
47static int	bcmemmc2_acpi_match(device_t, cfdata_t, void *);
48static void	bcmemmc2_acpi_attach(device_t, device_t, void *);
49static void	bcmemmc2_acpi_attach1(device_t);
50
51static const char * const compatible[] = {
52	"BRCME88C",
53	NULL
54};
55
56struct bcmemmc2_acpi_softc {
57	struct sdhc_softc sc;
58	bus_space_tag_t sc_memt;
59	bus_space_handle_t sc_memh;
60	bus_size_t sc_memsize;
61	void *sc_ih;
62	struct sdhc_host *sc_hosts[1];
63};
64
65CFATTACH_DECL_NEW(bcmemmc2_acpi, sizeof(struct bcmemmc2_acpi_softc),
66    bcmemmc2_acpi_match, bcmemmc2_acpi_attach, NULL, NULL);
67
68static int
69bcmemmc2_acpi_match(device_t parent, cfdata_t match, void *opaque)
70{
71	struct acpi_attach_args *aa = opaque;
72
73	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
74		return 0;
75
76	return acpi_match_hid(aa->aa_node->ad_devinfo, compatible);
77}
78
79static void
80bcmemmc2_acpi_attach(device_t parent, device_t self, void *opaque)
81{
82	struct bcmemmc2_acpi_softc *sc = device_private(self);
83	struct acpi_attach_args *aa = opaque;
84	struct acpi_resources res;
85	struct acpi_mem *mem;
86	struct acpi_irq *irq;
87	ACPI_STATUS rv;
88
89	sc->sc.sc_dev = self;
90	sc->sc.sc_dmat = aa->aa_dmat;
91	sc->sc.sc_host = sc->sc_hosts;
92	sc->sc_memt = aa->aa_memt;
93
94	rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
95	    &res, &acpi_resource_parse_ops_default);
96	if (ACPI_FAILURE(rv))
97		return;
98
99	mem = acpi_res_mem(&res, 0);
100	irq = acpi_res_irq(&res, 0);
101	if (mem == NULL || irq == NULL) {
102		aprint_error_dev(self, "incomplete resources\n");
103		goto cleanup;
104	}
105	if (mem->ar_length == 0) {
106		aprint_error_dev(self, "zero length memory resource\n");
107		goto cleanup;
108	}
109	sc->sc_memsize = mem->ar_length;
110
111	if (bus_space_map(sc->sc_memt, mem->ar_base, sc->sc_memsize, 0,
112	    &sc->sc_memh)) {
113		aprint_error_dev(self, "couldn't map registers\n");
114		goto cleanup;
115	}
116
117	/*
118	 * The controller doesn't honour standard SDHCI voltage registers, so
119	 * disable UHS modes.
120	 */
121	sc->sc.sc_flags = SDHC_FLAG_32BIT_ACCESS |
122#if notyet
123			  SDHC_FLAG_USE_DMA |
124			  SDHC_FLAG_USE_ADMA2 |
125#endif
126			  SDHC_FLAG_NO_1_8_V;
127
128	sc->sc_ih = acpi_intr_establish(self,
129	    (uint64_t)(uintptr_t)aa->aa_node->ad_handle,
130	    IPL_BIO, true, sdhc_intr, &sc->sc, device_xname(self));
131	if (sc->sc_ih == NULL) {
132		aprint_error_dev(self,
133		    "couldn't establish interrupt handler\n");
134		goto unmap;
135	}
136
137	config_defer(self, bcmemmc2_acpi_attach1);
138
139	acpi_resource_cleanup(&res);
140	return;
141
142unmap:
143	bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_memsize);
144	sc->sc_memsize = 0;
145cleanup:
146	acpi_resource_cleanup(&res);
147}
148
149static void
150bcmemmc2_acpi_attach1(device_t self)
151{
152	struct bcmemmc2_acpi_softc *sc = device_private(self);
153
154	if (sdhc_host_found(&sc->sc, sc->sc_memt, sc->sc_memh,
155	    sc->sc_memsize) != 0) {
156		aprint_error_dev(self, "couldn't initialize host\n");
157		goto fail;
158	}
159
160	return;
161
162fail:
163	if (sc->sc_ih != NULL)
164		acpi_intr_disestablish(sc->sc_ih);
165	sc->sc_ih = NULL;
166}
167