xref: /freebsd/sys/dev/sdhci/sdhci_xenon_fdt.c (revision 81b22a98)
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/types.h>
35 #include <sys/taskqueue.h>
36 #include <sys/module.h>
37 
38 #include <machine/bus.h>
39 
40 #include <dev/ofw/ofw_bus.h>
41 #include <dev/ofw/ofw_bus_subr.h>
42 
43 #include <dev/mmc/bridge.h>
44 #include <dev/mmc/mmcbrvar.h>
45 #include <dev/mmc/mmcreg.h>
46 
47 #include <dev/fdt/fdt_common.h>
48 #include <dev/mmc/mmc_fdt_helpers.h>
49 
50 #include <dev/sdhci/sdhci.h>
51 #include <dev/sdhci/sdhci_fdt_gpio.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 struct ofw_compat_data compat_data[] = {
61 	{ "marvell,armada-3700-sdhci",	1 },
62 #ifdef SOC_MARVELL_8K
63 	{ "marvell,armada-cp110-sdhci",	1 },
64 	{ "marvell,armada-ap806-sdhci",	1 },
65 	{ "marvell,armada-ap807-sdhci",	1 },
66 #endif
67 	{ NULL, 0 }
68 };
69 
70 static bool
71 sdhci_xenon_fdt_get_card_present(device_t dev, struct sdhci_slot *slot)
72 {
73 	struct sdhci_xenon_softc *sc;
74 
75 	sc = device_get_softc(dev);
76 
77 	return (sdhci_fdt_gpio_get_present(sc->gpio));
78 }
79 
80 static int
81 sdhci_xenon_fdt_probe(device_t dev)
82 {
83 	if (!ofw_bus_status_okay(dev))
84 		return (ENXIO);
85 
86 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
87 		return (ENXIO);
88 
89 	device_set_desc(dev, "Armada Xenon SDHCI controller");
90 
91 	return (BUS_PROBE_SPECIFIC);
92 }
93 
94 static void
95 sdhci_xenon_fdt_parse(device_t dev, struct sdhci_slot *slot)
96 {
97 	struct sdhci_xenon_softc *sc;
98 	struct mmc_helper mmc_helper;
99 
100 	sc = device_get_softc(dev);
101 	memset(&mmc_helper, 0, sizeof(mmc_helper));
102 
103 	/* MMC helper for parsing FDT */
104 	mmc_fdt_parse(dev, 0, &mmc_helper, &slot->host);
105 
106 	sc->skip_regulators = false;
107 	sc->vmmc_supply = mmc_helper.vmmc_supply;
108 	sc->vqmmc_supply = mmc_helper.vqmmc_supply;
109 	sc->wp_inverted = mmc_helper.props & MMC_PROP_WP_INVERTED;
110 
111 	/* Check if the device is flagged as non-removable. */
112 	if (mmc_helper.props & MMC_PROP_NON_REMOVABLE) {
113 		slot->opt |= SDHCI_NON_REMOVABLE;
114 		if (bootverbose)
115 			device_printf(dev, "Non-removable media\n");
116 	}
117 }
118 
119 static int
120 sdhci_xenon_fdt_attach(device_t dev)
121 {
122 	struct sdhci_xenon_softc *sc;
123 	struct sdhci_slot *slot;
124 
125 	sc = device_get_softc(dev);
126 	slot = malloc(sizeof(*slot), M_DEVBUF, M_ZERO | M_WAITOK);
127 
128 	sdhci_xenon_fdt_parse(dev, slot);
129 
130 	/*
131 	 * Set up any gpio pin handling described in the FDT data. This cannot
132 	 * fail; see comments in sdhci_fdt_gpio.h for details.
133 	 */
134 	sc->gpio = sdhci_fdt_gpio_setup(dev, slot);
135 	sc->slot = slot;
136 
137 	return (sdhci_xenon_attach(dev));
138 }
139 
140 static int
141 sdhci_xenon_fdt_detach(device_t dev)
142 {
143 	struct sdhci_xenon_softc *sc;
144 
145 	sc = device_get_softc(dev);
146 	if (sc->gpio != NULL)
147 		sdhci_fdt_gpio_teardown(sc->gpio);
148 
149 	return (sdhci_xenon_detach(dev));
150 }
151 
152 static device_method_t sdhci_xenon_fdt_methods[] = {
153 	/* device_if */
154 	DEVMETHOD(device_probe,		sdhci_xenon_fdt_probe),
155 	DEVMETHOD(device_attach,	sdhci_xenon_fdt_attach),
156 	DEVMETHOD(device_detach,	sdhci_xenon_fdt_detach),
157 
158 	DEVMETHOD(sdhci_get_card_present, sdhci_xenon_fdt_get_card_present),
159 
160 	DEVMETHOD_END
161 };
162 
163 DEFINE_CLASS_1(sdhci_xenon, sdhci_xenon_fdt_driver, sdhci_xenon_fdt_methods,
164     sizeof(struct sdhci_xenon_softc), sdhci_xenon_driver);
165 
166 static devclass_t sdhci_xenon_fdt_devclass;
167 
168 DRIVER_MODULE(sdhci_xenon, simplebus, sdhci_xenon_fdt_driver,
169     sdhci_xenon_fdt_devclass, NULL, NULL);
170 
171 #ifndef MMCCAM
172 MMC_DECLARE_BRIDGE(sdhci_xenon_fdt);
173 #endif
174