xref: /freebsd/sys/dev/mmc/mmc_fdt_helpers.c (revision b0387990a784050ef3a14ea49cb513b5eb737844)
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 
48e63fbd7bSEmmanuel Vadot static inline void
49e63fbd7bSEmmanuel Vadot mmc_fdt_parse_sd_speed(phandle_t node, struct mmc_host *host)
50e63fbd7bSEmmanuel Vadot {
51e63fbd7bSEmmanuel Vadot 	bool no_18v = false;
52e63fbd7bSEmmanuel Vadot 
53e63fbd7bSEmmanuel Vadot 	/*
54e63fbd7bSEmmanuel Vadot 	 * Parse SD supported modes
55e63fbd7bSEmmanuel Vadot 	 * All UHS-I modes requires 1.8V signaling.
56e63fbd7bSEmmanuel Vadot 	 */
57e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "no1-8-v"))
58e63fbd7bSEmmanuel Vadot 		no_18v = true;
59e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "cap-sd-highspeed"))
60e63fbd7bSEmmanuel Vadot 		host->caps |= MMC_CAP_HSPEED;
61e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "sd-uhs-sdr12") && no_18v == false)
62e63fbd7bSEmmanuel Vadot 		host->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_SIGNALING_180;
63e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "sd-uhs-sdr25") && no_18v == false)
64e63fbd7bSEmmanuel Vadot 		host->caps |= MMC_CAP_UHS_SDR25 | MMC_CAP_SIGNALING_180;
65e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "sd-uhs-sdr50") && no_18v == false)
66e63fbd7bSEmmanuel Vadot 		host->caps |= MMC_CAP_UHS_SDR50 | MMC_CAP_SIGNALING_180;
67e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "sd-uhs-sdr104") && no_18v == false)
68e63fbd7bSEmmanuel Vadot 		host->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_SIGNALING_180;
69e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "sd-uhs-ddr50") && no_18v == false)
70e63fbd7bSEmmanuel Vadot 		host->caps |= MMC_CAP_UHS_DDR50 | MMC_CAP_SIGNALING_180;
71e63fbd7bSEmmanuel Vadot }
72e63fbd7bSEmmanuel Vadot 
73e63fbd7bSEmmanuel Vadot static inline void
74e63fbd7bSEmmanuel Vadot mmc_fdt_parse_mmc_speed(phandle_t node, struct mmc_host *host)
75e63fbd7bSEmmanuel Vadot {
76e63fbd7bSEmmanuel Vadot 
77e63fbd7bSEmmanuel Vadot 	/* Parse eMMC supported modes */
78e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "cap-mmc-highspeed"))
79e63fbd7bSEmmanuel Vadot 		host->caps |= MMC_CAP_HSPEED;
80e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "mmc-ddr-1_2v"))
81e63fbd7bSEmmanuel Vadot 		host->caps |= MMC_CAP_MMC_DDR52_120 | MMC_CAP_SIGNALING_120;
82e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "mmc-ddr-1_8v"))
83e63fbd7bSEmmanuel Vadot 		host->caps |= MMC_CAP_MMC_DDR52_180 | MMC_CAP_SIGNALING_180;
84e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "mmc-ddr-3_3v"))
85e63fbd7bSEmmanuel Vadot 		host->caps |= MMC_CAP_SIGNALING_330;
86e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "mmc-hs200-1_2v"))
87e63fbd7bSEmmanuel Vadot 		host->caps |= MMC_CAP_MMC_HS200_120 | MMC_CAP_SIGNALING_120;
88e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "mmc-hs200-1_8v"))
89e63fbd7bSEmmanuel Vadot 		host->caps |= MMC_CAP_MMC_HS200_180 | MMC_CAP_SIGNALING_180;
90e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "mmc-hs400-1_2v"))
91e63fbd7bSEmmanuel Vadot 		host->caps |= MMC_CAP_MMC_HS400_120 | MMC_CAP_SIGNALING_120;
92e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "mmc-hs400-1_8v"))
93e63fbd7bSEmmanuel Vadot 		host->caps |= MMC_CAP_MMC_HS400_180 | MMC_CAP_SIGNALING_180;
94e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "mmc-hs400-enhanced-strobe"))
95e63fbd7bSEmmanuel Vadot 		host->caps |= MMC_CAP_MMC_ENH_STROBE;
96e63fbd7bSEmmanuel Vadot }
97e63fbd7bSEmmanuel Vadot 
98e63fbd7bSEmmanuel Vadot int
99e63fbd7bSEmmanuel Vadot mmc_fdt_parse(device_t dev, phandle_t node, struct mmc_fdt_helper *helper,
100e63fbd7bSEmmanuel Vadot     struct mmc_host *host)
101e63fbd7bSEmmanuel Vadot {
102e63fbd7bSEmmanuel Vadot 	uint32_t bus_width;
103*b0387990SEmmanuel Vadot 	phandle_t pwrseq_xref;
104e63fbd7bSEmmanuel Vadot 
105e63fbd7bSEmmanuel Vadot 	if (node <= 0)
106e63fbd7bSEmmanuel Vadot 		node = ofw_bus_get_node(dev);
107e63fbd7bSEmmanuel Vadot 	if (node <= 0)
108e63fbd7bSEmmanuel Vadot 		return (ENXIO);
109e63fbd7bSEmmanuel Vadot 
110e63fbd7bSEmmanuel Vadot 	if (OF_getencprop(node, "bus-width", &bus_width, sizeof(uint32_t)) <= 0)
111e63fbd7bSEmmanuel Vadot 		bus_width = 1;
112e63fbd7bSEmmanuel Vadot 
113e63fbd7bSEmmanuel Vadot 	if (bus_width >= 4)
114e63fbd7bSEmmanuel Vadot 		host->caps |= MMC_CAP_4_BIT_DATA;
115e63fbd7bSEmmanuel Vadot 	if (bus_width >= 8)
116e63fbd7bSEmmanuel Vadot 		host->caps |= MMC_CAP_8_BIT_DATA;
117e63fbd7bSEmmanuel Vadot 
118e63fbd7bSEmmanuel Vadot 	/*
119e63fbd7bSEmmanuel Vadot 	 * max-frequency is optional, drivers should tweak this value
120e63fbd7bSEmmanuel Vadot 	 * if it's not present based on the clock that the mmc controller
121e63fbd7bSEmmanuel Vadot 	 * operates on
122e63fbd7bSEmmanuel Vadot 	 */
123e63fbd7bSEmmanuel Vadot 	OF_getencprop(node, "max-frequency", &host->f_max, sizeof(uint32_t));
124e63fbd7bSEmmanuel Vadot 
125e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "broken-cd"))
126e63fbd7bSEmmanuel Vadot 		helper->props |= MMC_PROP_BROKEN_CD;
127e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "non-removable"))
128e63fbd7bSEmmanuel Vadot 		helper->props |= MMC_PROP_NON_REMOVABLE;
129e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "wp-inverted"))
130e63fbd7bSEmmanuel Vadot 		helper->props |= MMC_PROP_WP_INVERTED;
131e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "cd-inverted"))
132e63fbd7bSEmmanuel Vadot 		helper->props |= MMC_PROP_CD_INVERTED;
133e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "no-sdio"))
134e63fbd7bSEmmanuel Vadot 		helper->props |= MMC_PROP_NO_SDIO;
135e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "no-sd"))
136e63fbd7bSEmmanuel Vadot 		helper->props |= MMC_PROP_NO_SD;
137e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "no-mmc"))
138e63fbd7bSEmmanuel Vadot 		helper->props |= MMC_PROP_NO_MMC;
139e63fbd7bSEmmanuel Vadot 
140e63fbd7bSEmmanuel Vadot 	if (!(helper->props & MMC_PROP_NO_SD))
141e63fbd7bSEmmanuel Vadot 		mmc_fdt_parse_sd_speed(node, host);
142e63fbd7bSEmmanuel Vadot 
143e63fbd7bSEmmanuel Vadot 	if (!(helper->props & MMC_PROP_NO_MMC))
144e63fbd7bSEmmanuel Vadot 		mmc_fdt_parse_mmc_speed(node, host);
145e63fbd7bSEmmanuel Vadot 
146e63fbd7bSEmmanuel Vadot #ifdef EXT_RESOURCES
147e63fbd7bSEmmanuel Vadot 	/*
148e63fbd7bSEmmanuel Vadot 	 * Get the regulators if they are supported and
149e63fbd7bSEmmanuel Vadot 	 * clean the non supported modes based on the available voltages.
150e63fbd7bSEmmanuel Vadot 	 */
151e63fbd7bSEmmanuel Vadot 	if (regulator_get_by_ofw_property(dev, 0, "vmmc-supply",
152e63fbd7bSEmmanuel Vadot 	    &helper->vmmc_supply) == 0) {
153e63fbd7bSEmmanuel Vadot 		if (bootverbose)
154e63fbd7bSEmmanuel Vadot 			device_printf(dev, "vmmc-supply regulator found\n");
155e63fbd7bSEmmanuel Vadot 	}
156e63fbd7bSEmmanuel Vadot 	if (regulator_get_by_ofw_property(dev, 0, "vqmmc-supply",
157e63fbd7bSEmmanuel Vadot 	    &helper->vqmmc_supply) == 0 && bootverbose) {
158e63fbd7bSEmmanuel Vadot 		if (bootverbose)
159e63fbd7bSEmmanuel Vadot 			device_printf(dev, "vqmmc-supply regulator found\n");
160e63fbd7bSEmmanuel Vadot 	}
161e63fbd7bSEmmanuel Vadot 
162e63fbd7bSEmmanuel Vadot 	if (helper->vqmmc_supply != NULL) {
163e63fbd7bSEmmanuel Vadot 		if (regulator_check_voltage(helper->vqmmc_supply, 1200000) == 0)
164e63fbd7bSEmmanuel Vadot 			host->caps |= MMC_CAP_SIGNALING_120;
165e63fbd7bSEmmanuel Vadot 		else
166e63fbd7bSEmmanuel Vadot 			host->caps &= ~( MMC_CAP_MMC_HS400_120 |
167e63fbd7bSEmmanuel Vadot 			    MMC_CAP_MMC_HS200_120 |
168e63fbd7bSEmmanuel Vadot 			    MMC_CAP_MMC_DDR52_120);
169e63fbd7bSEmmanuel Vadot 		if (regulator_check_voltage(helper->vqmmc_supply, 1800000) == 0)
170e63fbd7bSEmmanuel Vadot 			host->caps |= MMC_CAP_SIGNALING_180;
171e63fbd7bSEmmanuel Vadot 		else
172e63fbd7bSEmmanuel Vadot 			host->caps &= ~(MMC_CAP_MMC_HS400_180 |
173e63fbd7bSEmmanuel Vadot 			    MMC_CAP_MMC_HS200_180 |
174e63fbd7bSEmmanuel Vadot 			    MMC_CAP_MMC_DDR52_180 |
175e63fbd7bSEmmanuel Vadot 			    MMC_CAP_UHS_DDR50 |
176e63fbd7bSEmmanuel Vadot 			    MMC_CAP_UHS_SDR104 |
177e63fbd7bSEmmanuel Vadot 			    MMC_CAP_UHS_SDR50 |
178e63fbd7bSEmmanuel Vadot 			    MMC_CAP_UHS_SDR25);
179e63fbd7bSEmmanuel Vadot 		if (regulator_check_voltage(helper->vqmmc_supply, 3300000) == 0)
180e63fbd7bSEmmanuel Vadot 			host->caps |= MMC_CAP_SIGNALING_330;
181e63fbd7bSEmmanuel Vadot 	} else
182e63fbd7bSEmmanuel Vadot 		host->caps |= MMC_CAP_SIGNALING_330;
183e63fbd7bSEmmanuel Vadot #endif
184e63fbd7bSEmmanuel Vadot 
185*b0387990SEmmanuel Vadot 	if (OF_hasprop(node, "mmc-pwrseq")) {
186*b0387990SEmmanuel Vadot 		if (OF_getencprop(node, "mmc-pwrseq", &pwrseq_xref, sizeof(pwrseq_xref)) == -1) {
187*b0387990SEmmanuel Vadot 			device_printf(dev, "Cannot get the pwrseq_xref property\n");
188*b0387990SEmmanuel Vadot 			return (ENXIO);
189*b0387990SEmmanuel Vadot 		}
190*b0387990SEmmanuel Vadot 		helper->mmc_pwrseq = OF_device_from_xref(pwrseq_xref);
191*b0387990SEmmanuel Vadot 	}
192e63fbd7bSEmmanuel Vadot 	return (0);
193e63fbd7bSEmmanuel Vadot }
194e63fbd7bSEmmanuel Vadot 
195e63fbd7bSEmmanuel Vadot /*
196e63fbd7bSEmmanuel Vadot  * Card detect interrupt handler.
197e63fbd7bSEmmanuel Vadot  */
198e63fbd7bSEmmanuel Vadot static void
199e63fbd7bSEmmanuel Vadot cd_intr(void *arg)
200e63fbd7bSEmmanuel Vadot {
201e63fbd7bSEmmanuel Vadot 	struct mmc_fdt_helper *helper = arg;
202e63fbd7bSEmmanuel Vadot 
203e63fbd7bSEmmanuel Vadot 	taskqueue_enqueue_timeout(taskqueue_swi_giant,
204e63fbd7bSEmmanuel Vadot 	    &helper->cd_delayed_task, -(hz / 2));
205e63fbd7bSEmmanuel Vadot }
206e63fbd7bSEmmanuel Vadot 
207e63fbd7bSEmmanuel Vadot static void
208e63fbd7bSEmmanuel Vadot cd_card_task(void *arg, int pending __unused)
209e63fbd7bSEmmanuel Vadot {
210e63fbd7bSEmmanuel Vadot 	struct mmc_fdt_helper *helper = arg;
211e63fbd7bSEmmanuel Vadot 	bool cd_present;
212e63fbd7bSEmmanuel Vadot 
213e63fbd7bSEmmanuel Vadot 	cd_present = mmc_fdt_gpio_get_present(helper);
214e63fbd7bSEmmanuel Vadot 	if(helper->cd_handler && cd_present != helper->cd_present)
215e63fbd7bSEmmanuel Vadot 		helper->cd_handler(helper->dev,
216e63fbd7bSEmmanuel Vadot 		    cd_present);
217e63fbd7bSEmmanuel Vadot 	helper->cd_present = cd_present;
218e63fbd7bSEmmanuel Vadot 
219e63fbd7bSEmmanuel Vadot 	/* If we're polling re-schedule the task */
220e63fbd7bSEmmanuel Vadot 	if (helper->cd_ihandler == NULL)
221e63fbd7bSEmmanuel Vadot 		taskqueue_enqueue_timeout_sbt(taskqueue_swi_giant,
222e63fbd7bSEmmanuel Vadot 		    &helper->cd_delayed_task, mstosbt(500), 0, C_PREL(2));
223e63fbd7bSEmmanuel Vadot }
224e63fbd7bSEmmanuel Vadot 
225e63fbd7bSEmmanuel Vadot /*
226e63fbd7bSEmmanuel Vadot  * Card detect setup.
227e63fbd7bSEmmanuel Vadot  */
228ac8dcdddSEmmanuel Vadot static void
229e63fbd7bSEmmanuel Vadot cd_setup(struct mmc_fdt_helper *helper, phandle_t node)
230e63fbd7bSEmmanuel Vadot {
231e63fbd7bSEmmanuel Vadot 	int pincaps;
232e63fbd7bSEmmanuel Vadot 	device_t dev;
233e63fbd7bSEmmanuel Vadot 	const char *cd_mode_str;
234e63fbd7bSEmmanuel Vadot 
235e63fbd7bSEmmanuel Vadot 	dev = helper->dev;
2364fda71e8SEmmanuel Vadot 
2374fda71e8SEmmanuel Vadot 	TIMEOUT_TASK_INIT(taskqueue_swi_giant, &helper->cd_delayed_task, 0,
2384fda71e8SEmmanuel Vadot 	    cd_card_task, helper);
2394fda71e8SEmmanuel Vadot 
240e63fbd7bSEmmanuel Vadot 	/*
241e63fbd7bSEmmanuel Vadot 	 * If the device is flagged as non-removable, set that slot option, and
242e63fbd7bSEmmanuel Vadot 	 * set a flag to make sdhci_fdt_gpio_get_present() always return true.
243e63fbd7bSEmmanuel Vadot 	 */
244e63fbd7bSEmmanuel Vadot 	if (helper->props & MMC_PROP_NON_REMOVABLE) {
245e63fbd7bSEmmanuel Vadot 		helper->cd_disabled = true;
246e63fbd7bSEmmanuel Vadot 		if (bootverbose)
247e63fbd7bSEmmanuel Vadot 			device_printf(dev, "Non-removable media\n");
248ac8dcdddSEmmanuel Vadot 		return;
249e63fbd7bSEmmanuel Vadot 	}
250e63fbd7bSEmmanuel Vadot 
251e63fbd7bSEmmanuel Vadot 	/*
252e63fbd7bSEmmanuel Vadot 	 * If there is no cd-gpios property, then presumably the hardware
253e63fbd7bSEmmanuel Vadot 	 * PRESENT_STATE register and interrupts will reflect card state
254e63fbd7bSEmmanuel Vadot 	 * properly, and there's nothing more for us to do.  Our get_present()
255e63fbd7bSEmmanuel Vadot 	 * will return sdhci_generic_get_card_present() because cd_pin is NULL.
256e63fbd7bSEmmanuel Vadot 	 *
257e63fbd7bSEmmanuel Vadot 	 * If there is a property, make sure we can read the pin.
258e63fbd7bSEmmanuel Vadot 	 */
259e63fbd7bSEmmanuel Vadot 	if (gpio_pin_get_by_ofw_property(dev, node, "cd-gpios",
260e63fbd7bSEmmanuel Vadot 	    &helper->cd_pin))
261ac8dcdddSEmmanuel Vadot 		return;
262e63fbd7bSEmmanuel Vadot 
263e63fbd7bSEmmanuel Vadot 	if (gpio_pin_getcaps(helper->cd_pin, &pincaps) != 0 ||
264e63fbd7bSEmmanuel Vadot 	    !(pincaps & GPIO_PIN_INPUT)) {
265e63fbd7bSEmmanuel Vadot 		device_printf(dev, "Cannot read card-detect gpio pin; "
266e63fbd7bSEmmanuel Vadot 		    "setting card-always-present flag.\n");
267e63fbd7bSEmmanuel Vadot 		helper->cd_disabled = true;
268ac8dcdddSEmmanuel Vadot 		return;
269e63fbd7bSEmmanuel Vadot 	}
270e63fbd7bSEmmanuel Vadot 
271e63fbd7bSEmmanuel Vadot 	/*
272e63fbd7bSEmmanuel Vadot 	 * If the pin can trigger an interrupt on both rising and falling edges,
273e63fbd7bSEmmanuel Vadot 	 * we can use it to detect card presence changes.  If not, we'll request
274e63fbd7bSEmmanuel Vadot 	 * card presence polling instead of using interrupts.
275e63fbd7bSEmmanuel Vadot 	 */
276e63fbd7bSEmmanuel Vadot 	if (!(pincaps & GPIO_INTR_EDGE_BOTH)) {
277e63fbd7bSEmmanuel Vadot 		if (bootverbose)
278e63fbd7bSEmmanuel Vadot 			device_printf(dev, "Cannot configure "
279e63fbd7bSEmmanuel Vadot 			    "GPIO_INTR_EDGE_BOTH for card detect\n");
280e63fbd7bSEmmanuel Vadot 		goto without_interrupts;
281e63fbd7bSEmmanuel Vadot 	}
282e63fbd7bSEmmanuel Vadot 
283e63fbd7bSEmmanuel Vadot 	if (helper->cd_handler == NULL) {
284e63fbd7bSEmmanuel Vadot 		if (bootverbose)
285e63fbd7bSEmmanuel Vadot 			device_printf(dev, "Cannot configure "
286e63fbd7bSEmmanuel Vadot 			    "interrupts as no cd_handler is set\n");
287e63fbd7bSEmmanuel Vadot 		goto without_interrupts;
288e63fbd7bSEmmanuel Vadot 	}
289e63fbd7bSEmmanuel Vadot 
290e63fbd7bSEmmanuel Vadot 	/*
291e63fbd7bSEmmanuel Vadot 	 * Create an interrupt resource from the pin and set up the interrupt.
292e63fbd7bSEmmanuel Vadot 	 */
293e63fbd7bSEmmanuel Vadot 	if ((helper->cd_ires = gpio_alloc_intr_resource(dev, &helper->cd_irid,
294e63fbd7bSEmmanuel Vadot 	    RF_ACTIVE, helper->cd_pin, GPIO_INTR_EDGE_BOTH)) == NULL) {
295e63fbd7bSEmmanuel Vadot 		if (bootverbose)
296e63fbd7bSEmmanuel Vadot 			device_printf(dev, "Cannot allocate an IRQ for card "
297e63fbd7bSEmmanuel Vadot 			    "detect GPIO\n");
298e63fbd7bSEmmanuel Vadot 		goto without_interrupts;
299e63fbd7bSEmmanuel Vadot 	}
300e63fbd7bSEmmanuel Vadot 
301e63fbd7bSEmmanuel Vadot 	if (bus_setup_intr(dev, helper->cd_ires, INTR_TYPE_BIO | INTR_MPSAFE,
302e63fbd7bSEmmanuel Vadot 	    NULL, cd_intr, helper, &helper->cd_ihandler) != 0) {
303e63fbd7bSEmmanuel Vadot 		device_printf(dev, "Unable to setup card-detect irq handler\n");
304e63fbd7bSEmmanuel Vadot 		helper->cd_ihandler = NULL;
305e63fbd7bSEmmanuel Vadot 		goto without_interrupts;
306e63fbd7bSEmmanuel Vadot 	}
307e63fbd7bSEmmanuel Vadot 
308e63fbd7bSEmmanuel Vadot without_interrupts:
309e63fbd7bSEmmanuel Vadot 	/*
310e63fbd7bSEmmanuel Vadot 	 * If we have a readable gpio pin, but didn't successfully configure
311e63fbd7bSEmmanuel Vadot 	 * gpio interrupts, setup a timeout task to poll the pin
312e63fbd7bSEmmanuel Vadot 	 */
313e63fbd7bSEmmanuel Vadot 	if (helper->cd_ihandler == NULL) {
314e63fbd7bSEmmanuel Vadot 		cd_mode_str = "polling";
315e63fbd7bSEmmanuel Vadot 	} else {
316e63fbd7bSEmmanuel Vadot 		cd_mode_str = "interrupts";
317e63fbd7bSEmmanuel Vadot 	}
318e63fbd7bSEmmanuel Vadot 
319e63fbd7bSEmmanuel Vadot 	if (bootverbose) {
320e63fbd7bSEmmanuel Vadot 		device_printf(dev, "Card presence detect on %s pin %u, "
321e63fbd7bSEmmanuel Vadot 		    "configured for %s.\n",
322e63fbd7bSEmmanuel Vadot 		    device_get_nameunit(helper->cd_pin->dev), helper->cd_pin->pin,
323e63fbd7bSEmmanuel Vadot 		    cd_mode_str);
324e63fbd7bSEmmanuel Vadot 	}
325e63fbd7bSEmmanuel Vadot }
326e63fbd7bSEmmanuel Vadot 
327e63fbd7bSEmmanuel Vadot /*
328e63fbd7bSEmmanuel Vadot  * Write protect setup.
329e63fbd7bSEmmanuel Vadot  */
330e63fbd7bSEmmanuel Vadot static void
331e63fbd7bSEmmanuel Vadot wp_setup(struct mmc_fdt_helper *helper, phandle_t node)
332e63fbd7bSEmmanuel Vadot {
333e63fbd7bSEmmanuel Vadot 	device_t dev;
334e63fbd7bSEmmanuel Vadot 
335e63fbd7bSEmmanuel Vadot 	dev = helper->dev;
336e63fbd7bSEmmanuel Vadot 
337e63fbd7bSEmmanuel Vadot 	if (OF_hasprop(node, "disable-wp")) {
338e63fbd7bSEmmanuel Vadot 		helper->wp_disabled = true;
339e63fbd7bSEmmanuel Vadot 		if (bootverbose)
340e63fbd7bSEmmanuel Vadot 			device_printf(dev, "Write protect disabled\n");
341e63fbd7bSEmmanuel Vadot 		return;
342e63fbd7bSEmmanuel Vadot 	}
343e63fbd7bSEmmanuel Vadot 
344e63fbd7bSEmmanuel Vadot 	if (gpio_pin_get_by_ofw_property(dev, node, "wp-gpios", &helper->wp_pin))
345e63fbd7bSEmmanuel Vadot 		return;
346e63fbd7bSEmmanuel Vadot 
347e63fbd7bSEmmanuel Vadot 	if (bootverbose)
348e63fbd7bSEmmanuel Vadot 		device_printf(dev, "Write protect switch on %s pin %u\n",
349e63fbd7bSEmmanuel Vadot 		    device_get_nameunit(helper->wp_pin->dev), helper->wp_pin->pin);
350e63fbd7bSEmmanuel Vadot }
351e63fbd7bSEmmanuel Vadot 
352e63fbd7bSEmmanuel Vadot int
353e63fbd7bSEmmanuel Vadot mmc_fdt_gpio_setup(device_t dev, phandle_t node, struct mmc_fdt_helper *helper,
354e63fbd7bSEmmanuel Vadot     mmc_fdt_cd_handler handler)
355e63fbd7bSEmmanuel Vadot {
356e63fbd7bSEmmanuel Vadot 
357e63fbd7bSEmmanuel Vadot 	if (node <= 0)
358e63fbd7bSEmmanuel Vadot 		node = ofw_bus_get_node(dev);
359e63fbd7bSEmmanuel Vadot 	if (node <= 0) {
360e63fbd7bSEmmanuel Vadot 		device_printf(dev, "Cannot get node for device\n");
361e63fbd7bSEmmanuel Vadot 		return (ENXIO);
362e63fbd7bSEmmanuel Vadot 	}
363e63fbd7bSEmmanuel Vadot 
364e63fbd7bSEmmanuel Vadot 	helper->dev = dev;
365e63fbd7bSEmmanuel Vadot 	helper->cd_handler = handler;
366ac8dcdddSEmmanuel Vadot 	cd_setup(helper, node);
367e63fbd7bSEmmanuel Vadot 	wp_setup(helper, node);
368e63fbd7bSEmmanuel Vadot 
369e63fbd7bSEmmanuel Vadot 	/*
370e63fbd7bSEmmanuel Vadot 	 * Schedule a card detection
371e63fbd7bSEmmanuel Vadot 	 */
372e63fbd7bSEmmanuel Vadot 	taskqueue_enqueue_timeout_sbt(taskqueue_swi_giant,
373e63fbd7bSEmmanuel Vadot 	    &helper->cd_delayed_task, mstosbt(500), 0, C_PREL(2));
374e63fbd7bSEmmanuel Vadot 	return (0);
375e63fbd7bSEmmanuel Vadot }
376e63fbd7bSEmmanuel Vadot 
377e63fbd7bSEmmanuel Vadot void
378e63fbd7bSEmmanuel Vadot mmc_fdt_gpio_teardown(struct mmc_fdt_helper *helper)
379e63fbd7bSEmmanuel Vadot {
380e63fbd7bSEmmanuel Vadot 
381e63fbd7bSEmmanuel Vadot 	if (helper == NULL)
382e63fbd7bSEmmanuel Vadot 		return;
383e63fbd7bSEmmanuel Vadot 
384e63fbd7bSEmmanuel Vadot 	if (helper->cd_ihandler != NULL)
385e63fbd7bSEmmanuel Vadot 		bus_teardown_intr(helper->dev, helper->cd_ires, helper->cd_ihandler);
386e63fbd7bSEmmanuel Vadot 	if (helper->wp_pin != NULL)
387e63fbd7bSEmmanuel Vadot 		gpio_pin_release(helper->wp_pin);
388e63fbd7bSEmmanuel Vadot 	if (helper->cd_pin != NULL)
389e63fbd7bSEmmanuel Vadot 		gpio_pin_release(helper->cd_pin);
390e63fbd7bSEmmanuel Vadot 	if (helper->cd_ires != NULL)
391e63fbd7bSEmmanuel Vadot 		bus_release_resource(helper->dev, SYS_RES_IRQ, 0, helper->cd_ires);
3920f2f8632SEmmanuel Vadot 
3930f2f8632SEmmanuel Vadot 	taskqueue_drain_timeout(taskqueue_swi_giant, &helper->cd_delayed_task);
394e63fbd7bSEmmanuel Vadot }
395e63fbd7bSEmmanuel Vadot 
396e63fbd7bSEmmanuel Vadot bool
397e63fbd7bSEmmanuel Vadot mmc_fdt_gpio_get_present(struct mmc_fdt_helper *helper)
398e63fbd7bSEmmanuel Vadot {
399e63fbd7bSEmmanuel Vadot 	bool pinstate;
400e63fbd7bSEmmanuel Vadot 
401e63fbd7bSEmmanuel Vadot 	if (helper->cd_disabled)
402e63fbd7bSEmmanuel Vadot 		return (true);
403e63fbd7bSEmmanuel Vadot 	if (helper->cd_pin == NULL)
404e63fbd7bSEmmanuel Vadot 		return (false);
405e63fbd7bSEmmanuel Vadot 
406e63fbd7bSEmmanuel Vadot 	gpio_pin_is_active(helper->cd_pin, &pinstate);
407e63fbd7bSEmmanuel Vadot 
408e63fbd7bSEmmanuel Vadot 	return (pinstate ^ (helper->props & MMC_PROP_CD_INVERTED));
409e63fbd7bSEmmanuel Vadot }
410e63fbd7bSEmmanuel Vadot 
411e63fbd7bSEmmanuel Vadot bool
412e63fbd7bSEmmanuel Vadot mmc_fdt_gpio_get_readonly(struct mmc_fdt_helper *helper)
413e63fbd7bSEmmanuel Vadot {
414e63fbd7bSEmmanuel Vadot 	bool pinstate;
415e63fbd7bSEmmanuel Vadot 
416e63fbd7bSEmmanuel Vadot 	if (helper->wp_disabled)
417e63fbd7bSEmmanuel Vadot 		return (false);
418e63fbd7bSEmmanuel Vadot 
419e63fbd7bSEmmanuel Vadot 	if (helper->wp_pin == NULL)
420e63fbd7bSEmmanuel Vadot 		return (false);
421e63fbd7bSEmmanuel Vadot 
422e63fbd7bSEmmanuel Vadot 	gpio_pin_is_active(helper->wp_pin, &pinstate);
423e63fbd7bSEmmanuel Vadot 
424e63fbd7bSEmmanuel Vadot 	return (pinstate ^ (helper->props & MMC_PROP_WP_INVERTED));
425e63fbd7bSEmmanuel Vadot }
426