xref: /freebsd/sys/dev/mmc/mmc_fdt_helpers.c (revision ef65f7bdf26e2f69e13d54e1ffd671de7d9032f6)
1e63fbd7bSEmmanuel Vadot /*
2e63fbd7bSEmmanuel Vadot  * Copyright 2019 Emmanuel Vadot <manu@freebsd.org>
3e63fbd7bSEmmanuel Vadot  * Copyright (c) 2017 Ian Lepore <ian@freebsd.org> All rights reserved.
4e63fbd7bSEmmanuel Vadot  *
5e63fbd7bSEmmanuel Vadot  * Redistribution and use in source and binary forms, with or without
6e63fbd7bSEmmanuel Vadot  * modification, are permitted provided that the following conditions are
7e63fbd7bSEmmanuel Vadot  * met:
8e63fbd7bSEmmanuel Vadot  *
9e63fbd7bSEmmanuel Vadot  *  1. Redistributions of source code must retain the above copyright
10e63fbd7bSEmmanuel Vadot  *     notice, this list of conditions and the following disclaimer.
11e63fbd7bSEmmanuel Vadot  *  2. Redistributions in binary form must reproduce the above copyright
12e63fbd7bSEmmanuel Vadot  *     notice, this list of conditions and the following disclaimer in the
13e63fbd7bSEmmanuel Vadot  *     documentation and/or other materials provided with the distribution.
14e63fbd7bSEmmanuel Vadot  *
15e63fbd7bSEmmanuel Vadot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16e63fbd7bSEmmanuel Vadot  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17e63fbd7bSEmmanuel Vadot  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18e63fbd7bSEmmanuel Vadot  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE
19e63fbd7bSEmmanuel Vadot  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20e63fbd7bSEmmanuel Vadot  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21e63fbd7bSEmmanuel Vadot  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
22e63fbd7bSEmmanuel Vadot  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23e63fbd7bSEmmanuel Vadot  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24e63fbd7bSEmmanuel Vadot  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25e63fbd7bSEmmanuel Vadot  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26e63fbd7bSEmmanuel Vadot  */
27e63fbd7bSEmmanuel Vadot 
28e63fbd7bSEmmanuel Vadot #include <sys/cdefs.h>
29e63fbd7bSEmmanuel Vadot __FBSDID("$FreeBSD$");
30e63fbd7bSEmmanuel Vadot 
31e63fbd7bSEmmanuel Vadot #include <sys/param.h>
32e63fbd7bSEmmanuel Vadot #include <sys/bus.h>
33e63fbd7bSEmmanuel Vadot #include <sys/kernel.h>
34e63fbd7bSEmmanuel Vadot #include <sys/gpio.h>
35e63fbd7bSEmmanuel Vadot #include <sys/taskqueue.h>
36e63fbd7bSEmmanuel Vadot 
37e63fbd7bSEmmanuel Vadot #include <dev/mmc/bridge.h>
38e63fbd7bSEmmanuel Vadot #include <dev/mmc/mmc_fdt_helpers.h>
39e63fbd7bSEmmanuel Vadot 
40e63fbd7bSEmmanuel Vadot #include <dev/gpio/gpiobusvar.h>
41e63fbd7bSEmmanuel Vadot #include <dev/ofw/ofw_bus.h>
42e63fbd7bSEmmanuel Vadot #include <dev/ofw/ofw_bus_subr.h>
43e63fbd7bSEmmanuel Vadot 
44e63fbd7bSEmmanuel Vadot #ifdef EXT_RESOURCES
45e63fbd7bSEmmanuel Vadot #include <dev/extres/regulator/regulator.h>
46e63fbd7bSEmmanuel Vadot #endif
47e63fbd7bSEmmanuel Vadot 
4803d4e8bbSEmmanuel Vadot #include "mmc_pwrseq_if.h"
4903d4e8bbSEmmanuel Vadot 
50e63fbd7bSEmmanuel Vadot static inline void
51e63fbd7bSEmmanuel Vadot mmc_fdt_parse_sd_speed(phandle_t node, struct mmc_host *host)
52e63fbd7bSEmmanuel Vadot {
53e63fbd7bSEmmanuel Vadot 	bool no_18v = false;
54e63fbd7bSEmmanuel Vadot 
55e63fbd7bSEmmanuel Vadot 	/*
56e63fbd7bSEmmanuel Vadot 	 * Parse SD supported modes
57e63fbd7bSEmmanuel Vadot 	 * All UHS-I modes requires 1.8V signaling.
58e63fbd7bSEmmanuel Vadot 	 */
59e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "no1-8-v"))
60e63fbd7bSEmmanuel Vadot 		no_18v = true;
61e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "cap-sd-highspeed"))
62e63fbd7bSEmmanuel Vadot 		host->caps |= MMC_CAP_HSPEED;
63e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "sd-uhs-sdr12") && no_18v == false)
64e63fbd7bSEmmanuel Vadot 		host->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_SIGNALING_180;
65e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "sd-uhs-sdr25") && no_18v == false)
66e63fbd7bSEmmanuel Vadot 		host->caps |= MMC_CAP_UHS_SDR25 | MMC_CAP_SIGNALING_180;
67e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "sd-uhs-sdr50") && no_18v == false)
68e63fbd7bSEmmanuel Vadot 		host->caps |= MMC_CAP_UHS_SDR50 | MMC_CAP_SIGNALING_180;
69e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "sd-uhs-sdr104") && no_18v == false)
70e63fbd7bSEmmanuel Vadot 		host->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_SIGNALING_180;
71e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "sd-uhs-ddr50") && no_18v == false)
72e63fbd7bSEmmanuel Vadot 		host->caps |= MMC_CAP_UHS_DDR50 | MMC_CAP_SIGNALING_180;
73e63fbd7bSEmmanuel Vadot }
74e63fbd7bSEmmanuel Vadot 
75e63fbd7bSEmmanuel Vadot static inline void
76e63fbd7bSEmmanuel Vadot mmc_fdt_parse_mmc_speed(phandle_t node, struct mmc_host *host)
77e63fbd7bSEmmanuel Vadot {
78e63fbd7bSEmmanuel Vadot 
79e63fbd7bSEmmanuel Vadot 	/* Parse eMMC supported modes */
80e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "cap-mmc-highspeed"))
81e63fbd7bSEmmanuel Vadot 		host->caps |= MMC_CAP_HSPEED;
82e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "mmc-ddr-1_2v"))
83e63fbd7bSEmmanuel Vadot 		host->caps |= MMC_CAP_MMC_DDR52_120 | MMC_CAP_SIGNALING_120;
84e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "mmc-ddr-1_8v"))
85e63fbd7bSEmmanuel Vadot 		host->caps |= MMC_CAP_MMC_DDR52_180 | MMC_CAP_SIGNALING_180;
86e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "mmc-ddr-3_3v"))
87e63fbd7bSEmmanuel Vadot 		host->caps |= MMC_CAP_SIGNALING_330;
88e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "mmc-hs200-1_2v"))
89e63fbd7bSEmmanuel Vadot 		host->caps |= MMC_CAP_MMC_HS200_120 | MMC_CAP_SIGNALING_120;
90e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "mmc-hs200-1_8v"))
91e63fbd7bSEmmanuel Vadot 		host->caps |= MMC_CAP_MMC_HS200_180 | MMC_CAP_SIGNALING_180;
92e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "mmc-hs400-1_2v"))
93e63fbd7bSEmmanuel Vadot 		host->caps |= MMC_CAP_MMC_HS400_120 | MMC_CAP_SIGNALING_120;
94e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "mmc-hs400-1_8v"))
95e63fbd7bSEmmanuel Vadot 		host->caps |= MMC_CAP_MMC_HS400_180 | MMC_CAP_SIGNALING_180;
96e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "mmc-hs400-enhanced-strobe"))
97e63fbd7bSEmmanuel Vadot 		host->caps |= MMC_CAP_MMC_ENH_STROBE;
98e63fbd7bSEmmanuel Vadot }
99e63fbd7bSEmmanuel Vadot 
100e63fbd7bSEmmanuel Vadot int
101e63fbd7bSEmmanuel Vadot mmc_fdt_parse(device_t dev, phandle_t node, struct mmc_fdt_helper *helper,
102e63fbd7bSEmmanuel Vadot     struct mmc_host *host)
103e63fbd7bSEmmanuel Vadot {
104e63fbd7bSEmmanuel Vadot 	uint32_t bus_width;
105b0387990SEmmanuel Vadot 	phandle_t pwrseq_xref;
106e63fbd7bSEmmanuel Vadot 
107e63fbd7bSEmmanuel Vadot 	if (node <= 0)
108e63fbd7bSEmmanuel Vadot 		node = ofw_bus_get_node(dev);
109e63fbd7bSEmmanuel Vadot 	if (node <= 0)
110e63fbd7bSEmmanuel Vadot 		return (ENXIO);
111e63fbd7bSEmmanuel Vadot 
112e63fbd7bSEmmanuel Vadot 	if (OF_getencprop(node, "bus-width", &bus_width, sizeof(uint32_t)) <= 0)
113e63fbd7bSEmmanuel Vadot 		bus_width = 1;
114e63fbd7bSEmmanuel Vadot 
115e63fbd7bSEmmanuel Vadot 	if (bus_width >= 4)
116e63fbd7bSEmmanuel Vadot 		host->caps |= MMC_CAP_4_BIT_DATA;
117e63fbd7bSEmmanuel Vadot 	if (bus_width >= 8)
118e63fbd7bSEmmanuel Vadot 		host->caps |= MMC_CAP_8_BIT_DATA;
119e63fbd7bSEmmanuel Vadot 
120e63fbd7bSEmmanuel Vadot 	/*
121e63fbd7bSEmmanuel Vadot 	 * max-frequency is optional, drivers should tweak this value
122e63fbd7bSEmmanuel Vadot 	 * if it's not present based on the clock that the mmc controller
123e63fbd7bSEmmanuel Vadot 	 * operates on
124e63fbd7bSEmmanuel Vadot 	 */
125e63fbd7bSEmmanuel Vadot 	OF_getencprop(node, "max-frequency", &host->f_max, sizeof(uint32_t));
126e63fbd7bSEmmanuel Vadot 
127e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "broken-cd"))
128e63fbd7bSEmmanuel Vadot 		helper->props |= MMC_PROP_BROKEN_CD;
129e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "non-removable"))
130e63fbd7bSEmmanuel Vadot 		helper->props |= MMC_PROP_NON_REMOVABLE;
131e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "wp-inverted"))
132e63fbd7bSEmmanuel Vadot 		helper->props |= MMC_PROP_WP_INVERTED;
133e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "cd-inverted"))
134e63fbd7bSEmmanuel Vadot 		helper->props |= MMC_PROP_CD_INVERTED;
135e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "no-sdio"))
136e63fbd7bSEmmanuel Vadot 		helper->props |= MMC_PROP_NO_SDIO;
137e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "no-sd"))
138e63fbd7bSEmmanuel Vadot 		helper->props |= MMC_PROP_NO_SD;
139e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "no-mmc"))
140e63fbd7bSEmmanuel Vadot 		helper->props |= MMC_PROP_NO_MMC;
141e63fbd7bSEmmanuel Vadot 
142e63fbd7bSEmmanuel Vadot 	if (!(helper->props & MMC_PROP_NO_SD))
143e63fbd7bSEmmanuel Vadot 		mmc_fdt_parse_sd_speed(node, host);
144e63fbd7bSEmmanuel Vadot 
145e63fbd7bSEmmanuel Vadot 	if (!(helper->props & MMC_PROP_NO_MMC))
146e63fbd7bSEmmanuel Vadot 		mmc_fdt_parse_mmc_speed(node, host);
147e63fbd7bSEmmanuel Vadot 
148e63fbd7bSEmmanuel Vadot #ifdef EXT_RESOURCES
149e63fbd7bSEmmanuel Vadot 	/*
150e63fbd7bSEmmanuel Vadot 	 * Get the regulators if they are supported and
151e63fbd7bSEmmanuel Vadot 	 * clean the non supported modes based on the available voltages.
152e63fbd7bSEmmanuel Vadot 	 */
153e63fbd7bSEmmanuel Vadot 	if (regulator_get_by_ofw_property(dev, 0, "vmmc-supply",
154e63fbd7bSEmmanuel Vadot 	    &helper->vmmc_supply) == 0) {
155e63fbd7bSEmmanuel Vadot 		if (bootverbose)
156e63fbd7bSEmmanuel Vadot 			device_printf(dev, "vmmc-supply regulator found\n");
157e63fbd7bSEmmanuel Vadot 	}
158e63fbd7bSEmmanuel Vadot 	if (regulator_get_by_ofw_property(dev, 0, "vqmmc-supply",
159e63fbd7bSEmmanuel Vadot 	    &helper->vqmmc_supply) == 0 && bootverbose) {
160e63fbd7bSEmmanuel Vadot 		if (bootverbose)
161e63fbd7bSEmmanuel Vadot 			device_printf(dev, "vqmmc-supply regulator found\n");
162e63fbd7bSEmmanuel Vadot 	}
163e63fbd7bSEmmanuel Vadot 
164e63fbd7bSEmmanuel Vadot 	if (helper->vqmmc_supply != NULL) {
165e63fbd7bSEmmanuel Vadot 		if (regulator_check_voltage(helper->vqmmc_supply, 1200000) == 0)
166e63fbd7bSEmmanuel Vadot 			host->caps |= MMC_CAP_SIGNALING_120;
167e63fbd7bSEmmanuel Vadot 		else
168e63fbd7bSEmmanuel Vadot 			host->caps &= ~( MMC_CAP_MMC_HS400_120 |
169e63fbd7bSEmmanuel Vadot 			    MMC_CAP_MMC_HS200_120 |
170e63fbd7bSEmmanuel Vadot 			    MMC_CAP_MMC_DDR52_120);
171e63fbd7bSEmmanuel Vadot 		if (regulator_check_voltage(helper->vqmmc_supply, 1800000) == 0)
172e63fbd7bSEmmanuel Vadot 			host->caps |= MMC_CAP_SIGNALING_180;
173e63fbd7bSEmmanuel Vadot 		else
174e63fbd7bSEmmanuel Vadot 			host->caps &= ~(MMC_CAP_MMC_HS400_180 |
175e63fbd7bSEmmanuel Vadot 			    MMC_CAP_MMC_HS200_180 |
176e63fbd7bSEmmanuel Vadot 			    MMC_CAP_MMC_DDR52_180 |
177e63fbd7bSEmmanuel Vadot 			    MMC_CAP_UHS_DDR50 |
178e63fbd7bSEmmanuel Vadot 			    MMC_CAP_UHS_SDR104 |
179e63fbd7bSEmmanuel Vadot 			    MMC_CAP_UHS_SDR50 |
180e63fbd7bSEmmanuel Vadot 			    MMC_CAP_UHS_SDR25);
181e63fbd7bSEmmanuel Vadot 		if (regulator_check_voltage(helper->vqmmc_supply, 3300000) == 0)
182e63fbd7bSEmmanuel Vadot 			host->caps |= MMC_CAP_SIGNALING_330;
183e63fbd7bSEmmanuel Vadot 	} else
184e63fbd7bSEmmanuel Vadot 		host->caps |= MMC_CAP_SIGNALING_330;
185e63fbd7bSEmmanuel Vadot #endif
186e63fbd7bSEmmanuel Vadot 
187b0387990SEmmanuel Vadot 	if (OF_hasprop(node, "mmc-pwrseq")) {
188b0387990SEmmanuel Vadot 		if (OF_getencprop(node, "mmc-pwrseq", &pwrseq_xref, sizeof(pwrseq_xref)) == -1) {
189b0387990SEmmanuel Vadot 			device_printf(dev, "Cannot get the pwrseq_xref property\n");
190b0387990SEmmanuel Vadot 			return (ENXIO);
191b0387990SEmmanuel Vadot 		}
192b0387990SEmmanuel Vadot 		helper->mmc_pwrseq = OF_device_from_xref(pwrseq_xref);
193b0387990SEmmanuel Vadot 	}
194e63fbd7bSEmmanuel Vadot 	return (0);
195e63fbd7bSEmmanuel Vadot }
196e63fbd7bSEmmanuel Vadot 
197e63fbd7bSEmmanuel Vadot /*
198e63fbd7bSEmmanuel Vadot  * Card detect interrupt handler.
199e63fbd7bSEmmanuel Vadot  */
200e63fbd7bSEmmanuel Vadot static void
201e63fbd7bSEmmanuel Vadot cd_intr(void *arg)
202e63fbd7bSEmmanuel Vadot {
203e63fbd7bSEmmanuel Vadot 	struct mmc_fdt_helper *helper = arg;
204e63fbd7bSEmmanuel Vadot 
205e63fbd7bSEmmanuel Vadot 	taskqueue_enqueue_timeout(taskqueue_swi_giant,
206e63fbd7bSEmmanuel Vadot 	    &helper->cd_delayed_task, -(hz / 2));
207e63fbd7bSEmmanuel Vadot }
208e63fbd7bSEmmanuel Vadot 
209e63fbd7bSEmmanuel Vadot static void
210e63fbd7bSEmmanuel Vadot cd_card_task(void *arg, int pending __unused)
211e63fbd7bSEmmanuel Vadot {
212e63fbd7bSEmmanuel Vadot 	struct mmc_fdt_helper *helper = arg;
213e63fbd7bSEmmanuel Vadot 	bool cd_present;
214e63fbd7bSEmmanuel Vadot 
215e63fbd7bSEmmanuel Vadot 	cd_present = mmc_fdt_gpio_get_present(helper);
216e63fbd7bSEmmanuel Vadot 	if(helper->cd_handler && cd_present != helper->cd_present)
217e63fbd7bSEmmanuel Vadot 		helper->cd_handler(helper->dev,
218e63fbd7bSEmmanuel Vadot 		    cd_present);
219e63fbd7bSEmmanuel Vadot 	helper->cd_present = cd_present;
220e63fbd7bSEmmanuel Vadot 
221e63fbd7bSEmmanuel Vadot 	/* If we're polling re-schedule the task */
222e63fbd7bSEmmanuel Vadot 	if (helper->cd_ihandler == NULL)
223e63fbd7bSEmmanuel Vadot 		taskqueue_enqueue_timeout_sbt(taskqueue_swi_giant,
224e63fbd7bSEmmanuel Vadot 		    &helper->cd_delayed_task, mstosbt(500), 0, C_PREL(2));
225e63fbd7bSEmmanuel Vadot }
226e63fbd7bSEmmanuel Vadot 
227e63fbd7bSEmmanuel Vadot /*
228e63fbd7bSEmmanuel Vadot  * Card detect setup.
229e63fbd7bSEmmanuel Vadot  */
230ac8dcdddSEmmanuel Vadot static void
231e63fbd7bSEmmanuel Vadot cd_setup(struct mmc_fdt_helper *helper, phandle_t node)
232e63fbd7bSEmmanuel Vadot {
233e63fbd7bSEmmanuel Vadot 	int pincaps;
234e63fbd7bSEmmanuel Vadot 	device_t dev;
235e63fbd7bSEmmanuel Vadot 	const char *cd_mode_str;
236e63fbd7bSEmmanuel Vadot 
237e63fbd7bSEmmanuel Vadot 	dev = helper->dev;
2384fda71e8SEmmanuel Vadot 
2394fda71e8SEmmanuel Vadot 	TIMEOUT_TASK_INIT(taskqueue_swi_giant, &helper->cd_delayed_task, 0,
2404fda71e8SEmmanuel Vadot 	    cd_card_task, helper);
2414fda71e8SEmmanuel Vadot 
242e63fbd7bSEmmanuel Vadot 	/*
243e63fbd7bSEmmanuel Vadot 	 * If the device is flagged as non-removable, set that slot option, and
244e63fbd7bSEmmanuel Vadot 	 * set a flag to make sdhci_fdt_gpio_get_present() always return true.
245e63fbd7bSEmmanuel Vadot 	 */
246e63fbd7bSEmmanuel Vadot 	if (helper->props & MMC_PROP_NON_REMOVABLE) {
247e63fbd7bSEmmanuel Vadot 		helper->cd_disabled = true;
248e63fbd7bSEmmanuel Vadot 		if (bootverbose)
249e63fbd7bSEmmanuel Vadot 			device_printf(dev, "Non-removable media\n");
250ac8dcdddSEmmanuel Vadot 		return;
251e63fbd7bSEmmanuel Vadot 	}
252e63fbd7bSEmmanuel Vadot 
253e63fbd7bSEmmanuel Vadot 	/*
254e63fbd7bSEmmanuel Vadot 	 * If there is no cd-gpios property, then presumably the hardware
255e63fbd7bSEmmanuel Vadot 	 * PRESENT_STATE register and interrupts will reflect card state
256e63fbd7bSEmmanuel Vadot 	 * properly, and there's nothing more for us to do.  Our get_present()
257e63fbd7bSEmmanuel Vadot 	 * will return sdhci_generic_get_card_present() because cd_pin is NULL.
258e63fbd7bSEmmanuel Vadot 	 *
259e63fbd7bSEmmanuel Vadot 	 * If there is a property, make sure we can read the pin.
260e63fbd7bSEmmanuel Vadot 	 */
261e63fbd7bSEmmanuel Vadot 	if (gpio_pin_get_by_ofw_property(dev, node, "cd-gpios",
262e63fbd7bSEmmanuel Vadot 	    &helper->cd_pin))
263ac8dcdddSEmmanuel Vadot 		return;
264e63fbd7bSEmmanuel Vadot 
265e63fbd7bSEmmanuel Vadot 	if (gpio_pin_getcaps(helper->cd_pin, &pincaps) != 0 ||
266e63fbd7bSEmmanuel Vadot 	    !(pincaps & GPIO_PIN_INPUT)) {
267e63fbd7bSEmmanuel Vadot 		device_printf(dev, "Cannot read card-detect gpio pin; "
268e63fbd7bSEmmanuel Vadot 		    "setting card-always-present flag.\n");
269e63fbd7bSEmmanuel Vadot 		helper->cd_disabled = true;
270ac8dcdddSEmmanuel Vadot 		return;
271e63fbd7bSEmmanuel Vadot 	}
272e63fbd7bSEmmanuel Vadot 
273e63fbd7bSEmmanuel Vadot 	/*
274e63fbd7bSEmmanuel Vadot 	 * If the pin can trigger an interrupt on both rising and falling edges,
275e63fbd7bSEmmanuel Vadot 	 * we can use it to detect card presence changes.  If not, we'll request
276e63fbd7bSEmmanuel Vadot 	 * card presence polling instead of using interrupts.
277e63fbd7bSEmmanuel Vadot 	 */
278e63fbd7bSEmmanuel Vadot 	if (!(pincaps & GPIO_INTR_EDGE_BOTH)) {
279e63fbd7bSEmmanuel Vadot 		if (bootverbose)
280e63fbd7bSEmmanuel Vadot 			device_printf(dev, "Cannot configure "
281e63fbd7bSEmmanuel Vadot 			    "GPIO_INTR_EDGE_BOTH for card detect\n");
282e63fbd7bSEmmanuel Vadot 		goto without_interrupts;
283e63fbd7bSEmmanuel Vadot 	}
284e63fbd7bSEmmanuel Vadot 
285e63fbd7bSEmmanuel Vadot 	if (helper->cd_handler == NULL) {
286e63fbd7bSEmmanuel Vadot 		if (bootverbose)
287e63fbd7bSEmmanuel Vadot 			device_printf(dev, "Cannot configure "
288e63fbd7bSEmmanuel Vadot 			    "interrupts as no cd_handler is set\n");
289e63fbd7bSEmmanuel Vadot 		goto without_interrupts;
290e63fbd7bSEmmanuel Vadot 	}
291e63fbd7bSEmmanuel Vadot 
292e63fbd7bSEmmanuel Vadot 	/*
293e63fbd7bSEmmanuel Vadot 	 * Create an interrupt resource from the pin and set up the interrupt.
294e63fbd7bSEmmanuel Vadot 	 */
295e63fbd7bSEmmanuel Vadot 	if ((helper->cd_ires = gpio_alloc_intr_resource(dev, &helper->cd_irid,
296e63fbd7bSEmmanuel Vadot 	    RF_ACTIVE, helper->cd_pin, GPIO_INTR_EDGE_BOTH)) == NULL) {
297e63fbd7bSEmmanuel Vadot 		if (bootverbose)
298e63fbd7bSEmmanuel Vadot 			device_printf(dev, "Cannot allocate an IRQ for card "
299e63fbd7bSEmmanuel Vadot 			    "detect GPIO\n");
300e63fbd7bSEmmanuel Vadot 		goto without_interrupts;
301e63fbd7bSEmmanuel Vadot 	}
302e63fbd7bSEmmanuel Vadot 
303e63fbd7bSEmmanuel Vadot 	if (bus_setup_intr(dev, helper->cd_ires, INTR_TYPE_BIO | INTR_MPSAFE,
304e63fbd7bSEmmanuel Vadot 	    NULL, cd_intr, helper, &helper->cd_ihandler) != 0) {
305e63fbd7bSEmmanuel Vadot 		device_printf(dev, "Unable to setup card-detect irq handler\n");
306e63fbd7bSEmmanuel Vadot 		helper->cd_ihandler = NULL;
307e63fbd7bSEmmanuel Vadot 		goto without_interrupts;
308e63fbd7bSEmmanuel Vadot 	}
309e63fbd7bSEmmanuel Vadot 
310e63fbd7bSEmmanuel Vadot without_interrupts:
311e63fbd7bSEmmanuel Vadot 	/*
312e63fbd7bSEmmanuel Vadot 	 * If we have a readable gpio pin, but didn't successfully configure
313e63fbd7bSEmmanuel Vadot 	 * gpio interrupts, setup a timeout task to poll the pin
314e63fbd7bSEmmanuel Vadot 	 */
315e63fbd7bSEmmanuel Vadot 	if (helper->cd_ihandler == NULL) {
316e63fbd7bSEmmanuel Vadot 		cd_mode_str = "polling";
317e63fbd7bSEmmanuel Vadot 	} else {
318e63fbd7bSEmmanuel Vadot 		cd_mode_str = "interrupts";
319e63fbd7bSEmmanuel Vadot 	}
320e63fbd7bSEmmanuel Vadot 
321e63fbd7bSEmmanuel Vadot 	if (bootverbose) {
322e63fbd7bSEmmanuel Vadot 		device_printf(dev, "Card presence detect on %s pin %u, "
323e63fbd7bSEmmanuel Vadot 		    "configured for %s.\n",
324e63fbd7bSEmmanuel Vadot 		    device_get_nameunit(helper->cd_pin->dev), helper->cd_pin->pin,
325e63fbd7bSEmmanuel Vadot 		    cd_mode_str);
326e63fbd7bSEmmanuel Vadot 	}
327e63fbd7bSEmmanuel Vadot }
328e63fbd7bSEmmanuel Vadot 
329e63fbd7bSEmmanuel Vadot /*
330e63fbd7bSEmmanuel Vadot  * Write protect setup.
331e63fbd7bSEmmanuel Vadot  */
332e63fbd7bSEmmanuel Vadot static void
333e63fbd7bSEmmanuel Vadot wp_setup(struct mmc_fdt_helper *helper, phandle_t node)
334e63fbd7bSEmmanuel Vadot {
335e63fbd7bSEmmanuel Vadot 	device_t dev;
336e63fbd7bSEmmanuel Vadot 
337e63fbd7bSEmmanuel Vadot 	dev = helper->dev;
338e63fbd7bSEmmanuel Vadot 
339e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "disable-wp")) {
340e63fbd7bSEmmanuel Vadot 		helper->wp_disabled = true;
341e63fbd7bSEmmanuel Vadot 		if (bootverbose)
342e63fbd7bSEmmanuel Vadot 			device_printf(dev, "Write protect disabled\n");
343e63fbd7bSEmmanuel Vadot 		return;
344e63fbd7bSEmmanuel Vadot 	}
345e63fbd7bSEmmanuel Vadot 
346e63fbd7bSEmmanuel Vadot 	if (gpio_pin_get_by_ofw_property(dev, node, "wp-gpios", &helper->wp_pin))
347e63fbd7bSEmmanuel Vadot 		return;
348e63fbd7bSEmmanuel Vadot 
349e63fbd7bSEmmanuel Vadot 	if (bootverbose)
350e63fbd7bSEmmanuel Vadot 		device_printf(dev, "Write protect switch on %s pin %u\n",
351e63fbd7bSEmmanuel Vadot 		    device_get_nameunit(helper->wp_pin->dev), helper->wp_pin->pin);
352e63fbd7bSEmmanuel Vadot }
353e63fbd7bSEmmanuel Vadot 
354e63fbd7bSEmmanuel Vadot int
355e63fbd7bSEmmanuel Vadot mmc_fdt_gpio_setup(device_t dev, phandle_t node, struct mmc_fdt_helper *helper,
356e63fbd7bSEmmanuel Vadot     mmc_fdt_cd_handler handler)
357e63fbd7bSEmmanuel Vadot {
358e63fbd7bSEmmanuel Vadot 
359e63fbd7bSEmmanuel Vadot 	if (node <= 0)
360e63fbd7bSEmmanuel Vadot 		node = ofw_bus_get_node(dev);
361e63fbd7bSEmmanuel Vadot 	if (node <= 0) {
362e63fbd7bSEmmanuel Vadot 		device_printf(dev, "Cannot get node for device\n");
363e63fbd7bSEmmanuel Vadot 		return (ENXIO);
364e63fbd7bSEmmanuel Vadot 	}
365e63fbd7bSEmmanuel Vadot 
366e63fbd7bSEmmanuel Vadot 	helper->dev = dev;
367e63fbd7bSEmmanuel Vadot 	helper->cd_handler = handler;
368ac8dcdddSEmmanuel Vadot 	cd_setup(helper, node);
369e63fbd7bSEmmanuel Vadot 	wp_setup(helper, node);
370e63fbd7bSEmmanuel Vadot 
371e63fbd7bSEmmanuel Vadot 	/*
372e63fbd7bSEmmanuel Vadot 	 * Schedule a card detection
373e63fbd7bSEmmanuel Vadot 	 */
374e63fbd7bSEmmanuel Vadot 	taskqueue_enqueue_timeout_sbt(taskqueue_swi_giant,
375e63fbd7bSEmmanuel Vadot 	    &helper->cd_delayed_task, mstosbt(500), 0, C_PREL(2));
376e63fbd7bSEmmanuel Vadot 	return (0);
377e63fbd7bSEmmanuel Vadot }
378e63fbd7bSEmmanuel Vadot 
379e63fbd7bSEmmanuel Vadot void
380e63fbd7bSEmmanuel Vadot mmc_fdt_gpio_teardown(struct mmc_fdt_helper *helper)
381e63fbd7bSEmmanuel Vadot {
382e63fbd7bSEmmanuel Vadot 
383e63fbd7bSEmmanuel Vadot 	if (helper == NULL)
384e63fbd7bSEmmanuel Vadot 		return;
385e63fbd7bSEmmanuel Vadot 
386e63fbd7bSEmmanuel Vadot 	if (helper->cd_ihandler != NULL)
387e63fbd7bSEmmanuel Vadot 		bus_teardown_intr(helper->dev, helper->cd_ires, helper->cd_ihandler);
388e63fbd7bSEmmanuel Vadot 	if (helper->wp_pin != NULL)
389e63fbd7bSEmmanuel Vadot 		gpio_pin_release(helper->wp_pin);
390e63fbd7bSEmmanuel Vadot 	if (helper->cd_pin != NULL)
391e63fbd7bSEmmanuel Vadot 		gpio_pin_release(helper->cd_pin);
392e63fbd7bSEmmanuel Vadot 	if (helper->cd_ires != NULL)
393e63fbd7bSEmmanuel Vadot 		bus_release_resource(helper->dev, SYS_RES_IRQ, 0, helper->cd_ires);
3940f2f8632SEmmanuel Vadot 
3950f2f8632SEmmanuel Vadot 	taskqueue_drain_timeout(taskqueue_swi_giant, &helper->cd_delayed_task);
396e63fbd7bSEmmanuel Vadot }
397e63fbd7bSEmmanuel Vadot 
398e63fbd7bSEmmanuel Vadot bool
399e63fbd7bSEmmanuel Vadot mmc_fdt_gpio_get_present(struct mmc_fdt_helper *helper)
400e63fbd7bSEmmanuel Vadot {
401e63fbd7bSEmmanuel Vadot 	bool pinstate;
402e63fbd7bSEmmanuel Vadot 
403e63fbd7bSEmmanuel Vadot 	if (helper->cd_disabled)
404e63fbd7bSEmmanuel Vadot 		return (true);
405e63fbd7bSEmmanuel Vadot 	if (helper->cd_pin == NULL)
406e63fbd7bSEmmanuel Vadot 		return (false);
407e63fbd7bSEmmanuel Vadot 
408e63fbd7bSEmmanuel Vadot 	gpio_pin_is_active(helper->cd_pin, &pinstate);
409e63fbd7bSEmmanuel Vadot 
410*ef65f7bdSWarner Losh 	return (pinstate ^ (bool)(helper->props & MMC_PROP_CD_INVERTED));
411e63fbd7bSEmmanuel Vadot }
412e63fbd7bSEmmanuel Vadot 
413e63fbd7bSEmmanuel Vadot bool
414e63fbd7bSEmmanuel Vadot mmc_fdt_gpio_get_readonly(struct mmc_fdt_helper *helper)
415e63fbd7bSEmmanuel Vadot {
416e63fbd7bSEmmanuel Vadot 	bool pinstate;
417e63fbd7bSEmmanuel Vadot 
418e63fbd7bSEmmanuel Vadot 	if (helper->wp_disabled)
419e63fbd7bSEmmanuel Vadot 		return (false);
420e63fbd7bSEmmanuel Vadot 
421e63fbd7bSEmmanuel Vadot 	if (helper->wp_pin == NULL)
422e63fbd7bSEmmanuel Vadot 		return (false);
423e63fbd7bSEmmanuel Vadot 
424e63fbd7bSEmmanuel Vadot 	gpio_pin_is_active(helper->wp_pin, &pinstate);
425e63fbd7bSEmmanuel Vadot 
426*ef65f7bdSWarner Losh 	return (pinstate ^ (bool)(helper->props & MMC_PROP_WP_INVERTED));
427e63fbd7bSEmmanuel Vadot }
42803d4e8bbSEmmanuel Vadot 
42903d4e8bbSEmmanuel Vadot void
43003d4e8bbSEmmanuel Vadot mmc_fdt_set_power(struct mmc_fdt_helper *helper, enum mmc_power_mode power_mode)
43103d4e8bbSEmmanuel Vadot {
43203d4e8bbSEmmanuel Vadot 	int reg_status;
43303d4e8bbSEmmanuel Vadot 	int rv;
43403d4e8bbSEmmanuel Vadot 
43503d4e8bbSEmmanuel Vadot 	switch (power_mode) {
43603d4e8bbSEmmanuel Vadot 	case power_on:
43703d4e8bbSEmmanuel Vadot 		break;
43803d4e8bbSEmmanuel Vadot 	case power_off:
43903d4e8bbSEmmanuel Vadot 		if (helper->vmmc_supply) {
44003d4e8bbSEmmanuel Vadot 			rv = regulator_status(helper->vmmc_supply, &reg_status);
44103d4e8bbSEmmanuel Vadot 			if (rv == 0 && reg_status == REGULATOR_STATUS_ENABLED)
44203d4e8bbSEmmanuel Vadot 				regulator_disable(helper->vmmc_supply);
44303d4e8bbSEmmanuel Vadot 		}
44403d4e8bbSEmmanuel Vadot 		if (helper->vqmmc_supply) {
44503d4e8bbSEmmanuel Vadot 			rv = regulator_status(helper->vqmmc_supply, &reg_status);
44603d4e8bbSEmmanuel Vadot 			if (rv == 0 && reg_status == REGULATOR_STATUS_ENABLED)
44703d4e8bbSEmmanuel Vadot 				regulator_disable(helper->vqmmc_supply);
44803d4e8bbSEmmanuel Vadot 		}
44903d4e8bbSEmmanuel Vadot 		if (helper->mmc_pwrseq)
45003d4e8bbSEmmanuel Vadot 			MMC_PWRSEQ_SET_POWER(helper->mmc_pwrseq, false);
45103d4e8bbSEmmanuel Vadot 		break;
45203d4e8bbSEmmanuel Vadot 	case power_up:
45303d4e8bbSEmmanuel Vadot 		if (helper->vmmc_supply) {
45403d4e8bbSEmmanuel Vadot 			rv = regulator_status(helper->vmmc_supply, &reg_status);
45503d4e8bbSEmmanuel Vadot 			if (rv == 0 && reg_status != REGULATOR_STATUS_ENABLED)
45603d4e8bbSEmmanuel Vadot 				regulator_enable(helper->vmmc_supply);
45703d4e8bbSEmmanuel Vadot 		}
45803d4e8bbSEmmanuel Vadot 		if (helper->vqmmc_supply) {
45903d4e8bbSEmmanuel Vadot 			rv = regulator_status(helper->vqmmc_supply, &reg_status);
46003d4e8bbSEmmanuel Vadot 			if (rv == 0 && reg_status != REGULATOR_STATUS_ENABLED)
46103d4e8bbSEmmanuel Vadot 				regulator_enable(helper->vqmmc_supply);
46203d4e8bbSEmmanuel Vadot 		}
46303d4e8bbSEmmanuel Vadot 		if (helper->mmc_pwrseq)
46403d4e8bbSEmmanuel Vadot 			MMC_PWRSEQ_SET_POWER(helper->mmc_pwrseq, true);
46503d4e8bbSEmmanuel Vadot 		break;
46603d4e8bbSEmmanuel Vadot 	}
46703d4e8bbSEmmanuel Vadot }
468