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