xref: /freebsd/sys/dev/sdhci/sdhci_xenon_acpi.c (revision 3332f1b444d4a73238e9f59cca27bfc95fe936bd)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2021 Semihalf
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
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, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/bus.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/taskqueue.h>
36 
37 #include <dev/mmc/bridge.h>
38 #include <dev/mmc/mmcbrvar.h>
39 #include <dev/mmc/mmcreg.h>
40 
41 #include <dev/mmc/mmc_helpers.h>
42 
43 #include <contrib/dev/acpica/include/acpi.h>
44 #include <contrib/dev/acpica/include/accommon.h>
45 #include <dev/acpica/acpivar.h>
46 
47 #ifdef EXT_RESOURCES
48 #include <dev/extres/regulator/regulator.h>
49 #endif
50 
51 #include <dev/sdhci/sdhci.h>
52 #include <dev/sdhci/sdhci_xenon.h>
53 
54 #include "mmcbr_if.h"
55 #include "sdhci_if.h"
56 
57 #include "opt_mmccam.h"
58 #include "opt_soc.h"
59 
60 static char *sdhci_xenon_hids[] = {
61 	"MRVL0002",
62 	"MRVL0003",
63 	"MRVL0004",
64 	NULL
65 };
66 
67 static int
68 sdhci_xenon_acpi_probe(device_t dev)
69 {
70 	device_t bus;
71 	int err;
72 
73 	bus = device_get_parent(dev);
74 
75 	err = ACPI_ID_PROBE(bus, dev, sdhci_xenon_hids, NULL);
76 	if (err <= 0) {
77 		device_set_desc(dev, "Armada Xenon SDHCI controller");
78 		return (err);
79 	}
80 
81 	return (ENXIO);
82 }
83 
84 static int
85 sdhci_xenon_acpi_attach(device_t dev)
86 {
87 	struct sdhci_xenon_softc *sc;
88 	struct sdhci_slot *slot;
89 	struct mmc_helper mmc_helper;
90 
91 	sc = device_get_softc(dev);
92 	memset(&mmc_helper, 0, sizeof(mmc_helper));
93 
94 	slot = malloc(sizeof(*slot), M_DEVBUF, M_ZERO | M_WAITOK);
95 	if (!slot)
96 		return (ENOMEM);
97 
98 	/*
99 	 * Don't use regularators.
100 	 * In ACPI mode the firmware takes care of them for us
101 	 */
102 	sc->skip_regulators = true;
103 	sc->slot = slot;
104 
105 	if (mmc_parse(dev, &mmc_helper, &slot->host) != 0)
106 		return (ENXIO);
107 
108 	if (mmc_helper.props & MMC_PROP_NON_REMOVABLE) {
109 		slot->opt |= SDHCI_NON_REMOVABLE;
110 		if (bootverbose)
111 			device_printf(dev, "Non-removable media\n");
112 	}
113 
114 	sc->wp_inverted = mmc_helper.props & MMC_PROP_WP_INVERTED;
115 
116 	return (sdhci_xenon_attach(dev));
117 }
118 
119 static device_method_t sdhci_xenon_acpi_methods[] = {
120 	/* device_if */
121 	DEVMETHOD(device_probe,		sdhci_xenon_acpi_probe),
122 	DEVMETHOD(device_attach,	sdhci_xenon_acpi_attach),
123 	DEVMETHOD(device_detach,	sdhci_xenon_detach),
124 
125 	DEVMETHOD(sdhci_get_card_present,	sdhci_generic_get_card_present),
126 
127 	DEVMETHOD_END
128 };
129 
130 DEFINE_CLASS_1(sdhci_xenon, sdhci_xenon_acpi_driver, sdhci_xenon_acpi_methods,
131 	sizeof(struct sdhci_xenon_softc), sdhci_xenon_driver);
132 
133 static devclass_t sdhci_xenon_acpi_devclass;
134 
135 DRIVER_MODULE(sdhci_xenon, acpi, sdhci_xenon_acpi_driver,
136 	sdhci_xenon_acpi_devclass, NULL, NULL);
137 
138 #ifndef MMCCAM
139 MMC_DECLARE_BRIDGE(sdhci_xenon_acpi);
140 #endif
141