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