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