xref: /freebsd/sys/dev/mmc/mmc_fdt_helpers.c (revision 8a8166e5bcfb50e2b7280581b600d098fa6c9fc7)
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 
48*8a8166e5SBartlomiej Grzesik #include <dev/mmc/mmc_helpers.h>
49*8a8166e5SBartlomiej Grzesik 
5003d4e8bbSEmmanuel Vadot #include "mmc_pwrseq_if.h"
5103d4e8bbSEmmanuel Vadot 
52e63fbd7bSEmmanuel Vadot int
53*8a8166e5SBartlomiej Grzesik mmc_fdt_parse(device_t dev, phandle_t node, struct mmc_helper *helper,
54e63fbd7bSEmmanuel Vadot     struct mmc_host *host)
55e63fbd7bSEmmanuel Vadot {
56*8a8166e5SBartlomiej Grzesik 	struct mmc_helper mmc_helper;
57b0387990SEmmanuel Vadot 	phandle_t pwrseq_xref;
58e63fbd7bSEmmanuel Vadot 
59*8a8166e5SBartlomiej Grzesik 	memset(&mmc_helper, 0, sizeof(mmc_helper));
60*8a8166e5SBartlomiej Grzesik 	mmc_parse(dev, &mmc_helper, host);
61e63fbd7bSEmmanuel Vadot 
62*8a8166e5SBartlomiej Grzesik 	helper->props = mmc_helper.props;
63e63fbd7bSEmmanuel Vadot 
64e63fbd7bSEmmanuel Vadot #ifdef EXT_RESOURCES
65e63fbd7bSEmmanuel Vadot 	/*
66e63fbd7bSEmmanuel Vadot 	 * Get the regulators if they are supported and
67e63fbd7bSEmmanuel Vadot 	 * clean the non supported modes based on the available voltages.
68e63fbd7bSEmmanuel Vadot 	 */
69e63fbd7bSEmmanuel Vadot 	if (regulator_get_by_ofw_property(dev, 0, "vmmc-supply",
70e63fbd7bSEmmanuel Vadot 	    &helper->vmmc_supply) == 0) {
71e63fbd7bSEmmanuel Vadot 		if (bootverbose)
72e63fbd7bSEmmanuel Vadot 			device_printf(dev, "vmmc-supply regulator found\n");
73e63fbd7bSEmmanuel Vadot 	}
74e63fbd7bSEmmanuel Vadot 	if (regulator_get_by_ofw_property(dev, 0, "vqmmc-supply",
75e63fbd7bSEmmanuel Vadot 	    &helper->vqmmc_supply) == 0 && bootverbose) {
76e63fbd7bSEmmanuel Vadot 		if (bootverbose)
77e63fbd7bSEmmanuel Vadot 			device_printf(dev, "vqmmc-supply regulator found\n");
78e63fbd7bSEmmanuel Vadot 	}
79e63fbd7bSEmmanuel Vadot 
80e63fbd7bSEmmanuel Vadot 	if (helper->vqmmc_supply != NULL) {
81e63fbd7bSEmmanuel Vadot 		if (regulator_check_voltage(helper->vqmmc_supply, 1200000) == 0)
82e63fbd7bSEmmanuel Vadot 			host->caps |= MMC_CAP_SIGNALING_120;
83e63fbd7bSEmmanuel Vadot 		else
84e63fbd7bSEmmanuel Vadot 			host->caps &= ~( MMC_CAP_MMC_HS400_120 |
85e63fbd7bSEmmanuel Vadot 			    MMC_CAP_MMC_HS200_120 |
86e63fbd7bSEmmanuel Vadot 			    MMC_CAP_MMC_DDR52_120);
87e63fbd7bSEmmanuel Vadot 		if (regulator_check_voltage(helper->vqmmc_supply, 1800000) == 0)
88e63fbd7bSEmmanuel Vadot 			host->caps |= MMC_CAP_SIGNALING_180;
89e63fbd7bSEmmanuel Vadot 		else
90e63fbd7bSEmmanuel Vadot 			host->caps &= ~(MMC_CAP_MMC_HS400_180 |
91e63fbd7bSEmmanuel Vadot 			    MMC_CAP_MMC_HS200_180 |
92e63fbd7bSEmmanuel Vadot 			    MMC_CAP_MMC_DDR52_180 |
93e63fbd7bSEmmanuel Vadot 			    MMC_CAP_UHS_DDR50 |
94e63fbd7bSEmmanuel Vadot 			    MMC_CAP_UHS_SDR104 |
95e63fbd7bSEmmanuel Vadot 			    MMC_CAP_UHS_SDR50 |
96e63fbd7bSEmmanuel Vadot 			    MMC_CAP_UHS_SDR25);
97e63fbd7bSEmmanuel Vadot 		if (regulator_check_voltage(helper->vqmmc_supply, 3300000) == 0)
98e63fbd7bSEmmanuel Vadot 			host->caps |= MMC_CAP_SIGNALING_330;
99e63fbd7bSEmmanuel Vadot 	} else
100e63fbd7bSEmmanuel Vadot 		host->caps |= MMC_CAP_SIGNALING_330;
101e63fbd7bSEmmanuel Vadot #endif
102e63fbd7bSEmmanuel Vadot 
103b0387990SEmmanuel Vadot 	if (OF_hasprop(node, "mmc-pwrseq")) {
104b0387990SEmmanuel Vadot 		if (OF_getencprop(node, "mmc-pwrseq", &pwrseq_xref, sizeof(pwrseq_xref)) == -1) {
105b0387990SEmmanuel Vadot 			device_printf(dev, "Cannot get the pwrseq_xref property\n");
106b0387990SEmmanuel Vadot 			return (ENXIO);
107b0387990SEmmanuel Vadot 		}
108b0387990SEmmanuel Vadot 		helper->mmc_pwrseq = OF_device_from_xref(pwrseq_xref);
109b0387990SEmmanuel Vadot 	}
110e63fbd7bSEmmanuel Vadot 	return (0);
111e63fbd7bSEmmanuel Vadot }
112e63fbd7bSEmmanuel Vadot 
113e63fbd7bSEmmanuel Vadot /*
114e63fbd7bSEmmanuel Vadot  * Card detect interrupt handler.
115e63fbd7bSEmmanuel Vadot  */
116e63fbd7bSEmmanuel Vadot static void
117e63fbd7bSEmmanuel Vadot cd_intr(void *arg)
118e63fbd7bSEmmanuel Vadot {
119*8a8166e5SBartlomiej Grzesik 	struct mmc_helper *helper = arg;
120e63fbd7bSEmmanuel Vadot 
121e63fbd7bSEmmanuel Vadot 	taskqueue_enqueue_timeout(taskqueue_swi_giant,
122e63fbd7bSEmmanuel Vadot 	    &helper->cd_delayed_task, -(hz / 2));
123e63fbd7bSEmmanuel Vadot }
124e63fbd7bSEmmanuel Vadot 
125e63fbd7bSEmmanuel Vadot static void
126e63fbd7bSEmmanuel Vadot cd_card_task(void *arg, int pending __unused)
127e63fbd7bSEmmanuel Vadot {
128*8a8166e5SBartlomiej Grzesik 	struct mmc_helper *helper = arg;
129e63fbd7bSEmmanuel Vadot 	bool cd_present;
130e63fbd7bSEmmanuel Vadot 
131e63fbd7bSEmmanuel Vadot 	cd_present = mmc_fdt_gpio_get_present(helper);
132e63fbd7bSEmmanuel Vadot 	if(helper->cd_handler && cd_present != helper->cd_present)
133e63fbd7bSEmmanuel Vadot 		helper->cd_handler(helper->dev,
134e63fbd7bSEmmanuel Vadot 		    cd_present);
135e63fbd7bSEmmanuel Vadot 	helper->cd_present = cd_present;
136e63fbd7bSEmmanuel Vadot 
137e63fbd7bSEmmanuel Vadot 	/* If we're polling re-schedule the task */
138e63fbd7bSEmmanuel Vadot 	if (helper->cd_ihandler == NULL)
139e63fbd7bSEmmanuel Vadot 		taskqueue_enqueue_timeout_sbt(taskqueue_swi_giant,
140e63fbd7bSEmmanuel Vadot 		    &helper->cd_delayed_task, mstosbt(500), 0, C_PREL(2));
141e63fbd7bSEmmanuel Vadot }
142e63fbd7bSEmmanuel Vadot 
143e63fbd7bSEmmanuel Vadot /*
144e63fbd7bSEmmanuel Vadot  * Card detect setup.
145e63fbd7bSEmmanuel Vadot  */
146ac8dcdddSEmmanuel Vadot static void
147*8a8166e5SBartlomiej Grzesik cd_setup(struct mmc_helper *helper, phandle_t node)
148e63fbd7bSEmmanuel Vadot {
149e63fbd7bSEmmanuel Vadot 	int pincaps;
150e63fbd7bSEmmanuel Vadot 	device_t dev;
151e63fbd7bSEmmanuel Vadot 	const char *cd_mode_str;
152e63fbd7bSEmmanuel Vadot 
153e63fbd7bSEmmanuel Vadot 	dev = helper->dev;
1544fda71e8SEmmanuel Vadot 
1554fda71e8SEmmanuel Vadot 	TIMEOUT_TASK_INIT(taskqueue_swi_giant, &helper->cd_delayed_task, 0,
1564fda71e8SEmmanuel Vadot 	    cd_card_task, helper);
1574fda71e8SEmmanuel Vadot 
158e63fbd7bSEmmanuel Vadot 	/*
159e63fbd7bSEmmanuel Vadot 	 * If the device is flagged as non-removable, set that slot option, and
160e63fbd7bSEmmanuel Vadot 	 * set a flag to make sdhci_fdt_gpio_get_present() always return true.
161e63fbd7bSEmmanuel Vadot 	 */
162e63fbd7bSEmmanuel Vadot 	if (helper->props & MMC_PROP_NON_REMOVABLE) {
163e63fbd7bSEmmanuel Vadot 		helper->cd_disabled = true;
164e63fbd7bSEmmanuel Vadot 		if (bootverbose)
165e63fbd7bSEmmanuel Vadot 			device_printf(dev, "Non-removable media\n");
166ac8dcdddSEmmanuel Vadot 		return;
167e63fbd7bSEmmanuel Vadot 	}
168e63fbd7bSEmmanuel Vadot 
169e63fbd7bSEmmanuel Vadot 	/*
170e63fbd7bSEmmanuel Vadot 	 * If there is no cd-gpios property, then presumably the hardware
171e63fbd7bSEmmanuel Vadot 	 * PRESENT_STATE register and interrupts will reflect card state
172e63fbd7bSEmmanuel Vadot 	 * properly, and there's nothing more for us to do.  Our get_present()
173e63fbd7bSEmmanuel Vadot 	 * will return sdhci_generic_get_card_present() because cd_pin is NULL.
174e63fbd7bSEmmanuel Vadot 	 *
175e63fbd7bSEmmanuel Vadot 	 * If there is a property, make sure we can read the pin.
176e63fbd7bSEmmanuel Vadot 	 */
177e63fbd7bSEmmanuel Vadot 	if (gpio_pin_get_by_ofw_property(dev, node, "cd-gpios",
178e63fbd7bSEmmanuel Vadot 	    &helper->cd_pin))
179ac8dcdddSEmmanuel Vadot 		return;
180e63fbd7bSEmmanuel Vadot 
181e63fbd7bSEmmanuel Vadot 	if (gpio_pin_getcaps(helper->cd_pin, &pincaps) != 0 ||
182e63fbd7bSEmmanuel Vadot 	    !(pincaps & GPIO_PIN_INPUT)) {
183e63fbd7bSEmmanuel Vadot 		device_printf(dev, "Cannot read card-detect gpio pin; "
184e63fbd7bSEmmanuel Vadot 		    "setting card-always-present flag.\n");
185e63fbd7bSEmmanuel Vadot 		helper->cd_disabled = true;
186ac8dcdddSEmmanuel Vadot 		return;
187e63fbd7bSEmmanuel Vadot 	}
188e63fbd7bSEmmanuel Vadot 
189e63fbd7bSEmmanuel Vadot 	/*
190e63fbd7bSEmmanuel Vadot 	 * If the pin can trigger an interrupt on both rising and falling edges,
191e63fbd7bSEmmanuel Vadot 	 * we can use it to detect card presence changes.  If not, we'll request
192e63fbd7bSEmmanuel Vadot 	 * card presence polling instead of using interrupts.
193e63fbd7bSEmmanuel Vadot 	 */
194e63fbd7bSEmmanuel Vadot 	if (!(pincaps & GPIO_INTR_EDGE_BOTH)) {
195e63fbd7bSEmmanuel Vadot 		if (bootverbose)
196e63fbd7bSEmmanuel Vadot 			device_printf(dev, "Cannot configure "
197e63fbd7bSEmmanuel Vadot 			    "GPIO_INTR_EDGE_BOTH for card detect\n");
198e63fbd7bSEmmanuel Vadot 		goto without_interrupts;
199e63fbd7bSEmmanuel Vadot 	}
200e63fbd7bSEmmanuel Vadot 
201e63fbd7bSEmmanuel Vadot 	if (helper->cd_handler == NULL) {
202e63fbd7bSEmmanuel Vadot 		if (bootverbose)
203e63fbd7bSEmmanuel Vadot 			device_printf(dev, "Cannot configure "
204e63fbd7bSEmmanuel Vadot 			    "interrupts as no cd_handler is set\n");
205e63fbd7bSEmmanuel Vadot 		goto without_interrupts;
206e63fbd7bSEmmanuel Vadot 	}
207e63fbd7bSEmmanuel Vadot 
208e63fbd7bSEmmanuel Vadot 	/*
209e63fbd7bSEmmanuel Vadot 	 * Create an interrupt resource from the pin and set up the interrupt.
210e63fbd7bSEmmanuel Vadot 	 */
211e63fbd7bSEmmanuel Vadot 	if ((helper->cd_ires = gpio_alloc_intr_resource(dev, &helper->cd_irid,
212e63fbd7bSEmmanuel Vadot 	    RF_ACTIVE, helper->cd_pin, GPIO_INTR_EDGE_BOTH)) == NULL) {
213e63fbd7bSEmmanuel Vadot 		if (bootverbose)
214e63fbd7bSEmmanuel Vadot 			device_printf(dev, "Cannot allocate an IRQ for card "
215e63fbd7bSEmmanuel Vadot 			    "detect GPIO\n");
216e63fbd7bSEmmanuel Vadot 		goto without_interrupts;
217e63fbd7bSEmmanuel Vadot 	}
218e63fbd7bSEmmanuel Vadot 
219e63fbd7bSEmmanuel Vadot 	if (bus_setup_intr(dev, helper->cd_ires, INTR_TYPE_BIO | INTR_MPSAFE,
220e63fbd7bSEmmanuel Vadot 	    NULL, cd_intr, helper, &helper->cd_ihandler) != 0) {
221e63fbd7bSEmmanuel Vadot 		device_printf(dev, "Unable to setup card-detect irq handler\n");
222e63fbd7bSEmmanuel Vadot 		helper->cd_ihandler = NULL;
223e63fbd7bSEmmanuel Vadot 		goto without_interrupts;
224e63fbd7bSEmmanuel Vadot 	}
225e63fbd7bSEmmanuel Vadot 
226e63fbd7bSEmmanuel Vadot without_interrupts:
227e63fbd7bSEmmanuel Vadot 	/*
228e63fbd7bSEmmanuel Vadot 	 * If we have a readable gpio pin, but didn't successfully configure
229e63fbd7bSEmmanuel Vadot 	 * gpio interrupts, setup a timeout task to poll the pin
230e63fbd7bSEmmanuel Vadot 	 */
231e63fbd7bSEmmanuel Vadot 	if (helper->cd_ihandler == NULL) {
232e63fbd7bSEmmanuel Vadot 		cd_mode_str = "polling";
233e63fbd7bSEmmanuel Vadot 	} else {
234e63fbd7bSEmmanuel Vadot 		cd_mode_str = "interrupts";
235e63fbd7bSEmmanuel Vadot 	}
236e63fbd7bSEmmanuel Vadot 
237e63fbd7bSEmmanuel Vadot 	if (bootverbose) {
238e63fbd7bSEmmanuel Vadot 		device_printf(dev, "Card presence detect on %s pin %u, "
239e63fbd7bSEmmanuel Vadot 		    "configured for %s.\n",
240e63fbd7bSEmmanuel Vadot 		    device_get_nameunit(helper->cd_pin->dev), helper->cd_pin->pin,
241e63fbd7bSEmmanuel Vadot 		    cd_mode_str);
242e63fbd7bSEmmanuel Vadot 	}
243e63fbd7bSEmmanuel Vadot }
244e63fbd7bSEmmanuel Vadot 
245e63fbd7bSEmmanuel Vadot /*
246e63fbd7bSEmmanuel Vadot  * Write protect setup.
247e63fbd7bSEmmanuel Vadot  */
248e63fbd7bSEmmanuel Vadot static void
249*8a8166e5SBartlomiej Grzesik wp_setup(struct mmc_helper *helper, phandle_t node)
250e63fbd7bSEmmanuel Vadot {
251e63fbd7bSEmmanuel Vadot 	device_t dev;
252e63fbd7bSEmmanuel Vadot 
253e63fbd7bSEmmanuel Vadot 	dev = helper->dev;
254e63fbd7bSEmmanuel Vadot 
255e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "disable-wp")) {
256e63fbd7bSEmmanuel Vadot 		helper->wp_disabled = true;
257e63fbd7bSEmmanuel Vadot 		if (bootverbose)
258e63fbd7bSEmmanuel Vadot 			device_printf(dev, "Write protect disabled\n");
259e63fbd7bSEmmanuel Vadot 		return;
260e63fbd7bSEmmanuel Vadot 	}
261e63fbd7bSEmmanuel Vadot 
262e63fbd7bSEmmanuel Vadot 	if (gpio_pin_get_by_ofw_property(dev, node, "wp-gpios", &helper->wp_pin))
263e63fbd7bSEmmanuel Vadot 		return;
264e63fbd7bSEmmanuel Vadot 
265e63fbd7bSEmmanuel Vadot 	if (bootverbose)
266e63fbd7bSEmmanuel Vadot 		device_printf(dev, "Write protect switch on %s pin %u\n",
267e63fbd7bSEmmanuel Vadot 		    device_get_nameunit(helper->wp_pin->dev), helper->wp_pin->pin);
268e63fbd7bSEmmanuel Vadot }
269e63fbd7bSEmmanuel Vadot 
270e63fbd7bSEmmanuel Vadot int
271*8a8166e5SBartlomiej Grzesik mmc_fdt_gpio_setup(device_t dev, phandle_t node, struct mmc_helper *helper,
272e63fbd7bSEmmanuel Vadot     mmc_fdt_cd_handler handler)
273e63fbd7bSEmmanuel Vadot {
274e63fbd7bSEmmanuel Vadot 
275e63fbd7bSEmmanuel Vadot 	if (node <= 0)
276e63fbd7bSEmmanuel Vadot 		node = ofw_bus_get_node(dev);
277e63fbd7bSEmmanuel Vadot 	if (node <= 0) {
278e63fbd7bSEmmanuel Vadot 		device_printf(dev, "Cannot get node for device\n");
279e63fbd7bSEmmanuel Vadot 		return (ENXIO);
280e63fbd7bSEmmanuel Vadot 	}
281e63fbd7bSEmmanuel Vadot 
282e63fbd7bSEmmanuel Vadot 	helper->dev = dev;
283e63fbd7bSEmmanuel Vadot 	helper->cd_handler = handler;
284ac8dcdddSEmmanuel Vadot 	cd_setup(helper, node);
285e63fbd7bSEmmanuel Vadot 	wp_setup(helper, node);
286e63fbd7bSEmmanuel Vadot 
287e63fbd7bSEmmanuel Vadot 	/*
288e63fbd7bSEmmanuel Vadot 	 * Schedule a card detection
289e63fbd7bSEmmanuel Vadot 	 */
290e63fbd7bSEmmanuel Vadot 	taskqueue_enqueue_timeout_sbt(taskqueue_swi_giant,
291e63fbd7bSEmmanuel Vadot 	    &helper->cd_delayed_task, mstosbt(500), 0, C_PREL(2));
292e63fbd7bSEmmanuel Vadot 	return (0);
293e63fbd7bSEmmanuel Vadot }
294e63fbd7bSEmmanuel Vadot 
295e63fbd7bSEmmanuel Vadot void
296*8a8166e5SBartlomiej Grzesik mmc_fdt_gpio_teardown(struct mmc_helper *helper)
297e63fbd7bSEmmanuel Vadot {
298e63fbd7bSEmmanuel Vadot 
299e63fbd7bSEmmanuel Vadot 	if (helper == NULL)
300e63fbd7bSEmmanuel Vadot 		return;
301e63fbd7bSEmmanuel Vadot 
302e63fbd7bSEmmanuel Vadot 	if (helper->cd_ihandler != NULL)
303e63fbd7bSEmmanuel Vadot 		bus_teardown_intr(helper->dev, helper->cd_ires, helper->cd_ihandler);
304e63fbd7bSEmmanuel Vadot 	if (helper->wp_pin != NULL)
305e63fbd7bSEmmanuel Vadot 		gpio_pin_release(helper->wp_pin);
306e63fbd7bSEmmanuel Vadot 	if (helper->cd_pin != NULL)
307e63fbd7bSEmmanuel Vadot 		gpio_pin_release(helper->cd_pin);
308e63fbd7bSEmmanuel Vadot 	if (helper->cd_ires != NULL)
309e63fbd7bSEmmanuel Vadot 		bus_release_resource(helper->dev, SYS_RES_IRQ, 0, helper->cd_ires);
3100f2f8632SEmmanuel Vadot 
3110f2f8632SEmmanuel Vadot 	taskqueue_drain_timeout(taskqueue_swi_giant, &helper->cd_delayed_task);
312e63fbd7bSEmmanuel Vadot }
313e63fbd7bSEmmanuel Vadot 
314e63fbd7bSEmmanuel Vadot bool
315*8a8166e5SBartlomiej Grzesik mmc_fdt_gpio_get_present(struct mmc_helper *helper)
316e63fbd7bSEmmanuel Vadot {
317e63fbd7bSEmmanuel Vadot 	bool pinstate;
318e63fbd7bSEmmanuel Vadot 
319e63fbd7bSEmmanuel Vadot 	if (helper->cd_disabled)
320e63fbd7bSEmmanuel Vadot 		return (true);
321e63fbd7bSEmmanuel Vadot 	if (helper->cd_pin == NULL)
322e63fbd7bSEmmanuel Vadot 		return (false);
323e63fbd7bSEmmanuel Vadot 
324e63fbd7bSEmmanuel Vadot 	gpio_pin_is_active(helper->cd_pin, &pinstate);
325e63fbd7bSEmmanuel Vadot 
326ef65f7bdSWarner Losh 	return (pinstate ^ (bool)(helper->props & MMC_PROP_CD_INVERTED));
327e63fbd7bSEmmanuel Vadot }
328e63fbd7bSEmmanuel Vadot 
329e63fbd7bSEmmanuel Vadot bool
330*8a8166e5SBartlomiej Grzesik mmc_fdt_gpio_get_readonly(struct mmc_helper *helper)
331e63fbd7bSEmmanuel Vadot {
332e63fbd7bSEmmanuel Vadot 	bool pinstate;
333e63fbd7bSEmmanuel Vadot 
334e63fbd7bSEmmanuel Vadot 	if (helper->wp_disabled)
335e63fbd7bSEmmanuel Vadot 		return (false);
336e63fbd7bSEmmanuel Vadot 
337e63fbd7bSEmmanuel Vadot 	if (helper->wp_pin == NULL)
338e63fbd7bSEmmanuel Vadot 		return (false);
339e63fbd7bSEmmanuel Vadot 
340e63fbd7bSEmmanuel Vadot 	gpio_pin_is_active(helper->wp_pin, &pinstate);
341e63fbd7bSEmmanuel Vadot 
342ef65f7bdSWarner Losh 	return (pinstate ^ (bool)(helper->props & MMC_PROP_WP_INVERTED));
343e63fbd7bSEmmanuel Vadot }
34403d4e8bbSEmmanuel Vadot 
34503d4e8bbSEmmanuel Vadot void
346*8a8166e5SBartlomiej Grzesik mmc_fdt_set_power(struct mmc_helper *helper, enum mmc_power_mode power_mode)
34703d4e8bbSEmmanuel Vadot {
34803d4e8bbSEmmanuel Vadot 	int reg_status;
34903d4e8bbSEmmanuel Vadot 	int rv;
35003d4e8bbSEmmanuel Vadot 
35103d4e8bbSEmmanuel Vadot 	switch (power_mode) {
35203d4e8bbSEmmanuel Vadot 	case power_on:
35303d4e8bbSEmmanuel Vadot 		break;
35403d4e8bbSEmmanuel Vadot 	case power_off:
35503d4e8bbSEmmanuel Vadot 		if (helper->vmmc_supply) {
35603d4e8bbSEmmanuel Vadot 			rv = regulator_status(helper->vmmc_supply, &reg_status);
35703d4e8bbSEmmanuel Vadot 			if (rv == 0 && reg_status == REGULATOR_STATUS_ENABLED)
35803d4e8bbSEmmanuel Vadot 				regulator_disable(helper->vmmc_supply);
35903d4e8bbSEmmanuel Vadot 		}
36003d4e8bbSEmmanuel Vadot 		if (helper->vqmmc_supply) {
36103d4e8bbSEmmanuel Vadot 			rv = regulator_status(helper->vqmmc_supply, &reg_status);
36203d4e8bbSEmmanuel Vadot 			if (rv == 0 && reg_status == REGULATOR_STATUS_ENABLED)
36303d4e8bbSEmmanuel Vadot 				regulator_disable(helper->vqmmc_supply);
36403d4e8bbSEmmanuel Vadot 		}
36503d4e8bbSEmmanuel Vadot 		if (helper->mmc_pwrseq)
36603d4e8bbSEmmanuel Vadot 			MMC_PWRSEQ_SET_POWER(helper->mmc_pwrseq, false);
36703d4e8bbSEmmanuel Vadot 		break;
36803d4e8bbSEmmanuel Vadot 	case power_up:
36903d4e8bbSEmmanuel Vadot 		if (helper->vmmc_supply) {
37003d4e8bbSEmmanuel Vadot 			rv = regulator_status(helper->vmmc_supply, &reg_status);
37103d4e8bbSEmmanuel Vadot 			if (rv == 0 && reg_status != REGULATOR_STATUS_ENABLED)
37203d4e8bbSEmmanuel Vadot 				regulator_enable(helper->vmmc_supply);
37303d4e8bbSEmmanuel Vadot 		}
37403d4e8bbSEmmanuel Vadot 		if (helper->vqmmc_supply) {
37503d4e8bbSEmmanuel Vadot 			rv = regulator_status(helper->vqmmc_supply, &reg_status);
37603d4e8bbSEmmanuel Vadot 			if (rv == 0 && reg_status != REGULATOR_STATUS_ENABLED)
37703d4e8bbSEmmanuel Vadot 				regulator_enable(helper->vqmmc_supply);
37803d4e8bbSEmmanuel Vadot 		}
37903d4e8bbSEmmanuel Vadot 		if (helper->mmc_pwrseq)
38003d4e8bbSEmmanuel Vadot 			MMC_PWRSEQ_SET_POWER(helper->mmc_pwrseq, true);
38103d4e8bbSEmmanuel Vadot 		break;
38203d4e8bbSEmmanuel Vadot 	}
38303d4e8bbSEmmanuel Vadot }
384