2 * Copyright 2019 Emmanuel Vadot <manu@freebsd.org>
3 * Copyright (c) 2017 Ian Lepore <ian@freebsd.org> All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
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.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
22 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
33 #include <sys/kernel.h>
35 #include <sys/taskqueue.h>
37 #include <dev/mmc/bridge.h>
38 #include <dev/mmc/mmc_fdt_helpers.h>
40 #include <dev/gpio/gpiobusvar.h>
41 #include <dev/ofw/ofw_bus.h>
42 #include <dev/ofw/ofw_bus_subr.h>
45 #include <dev/extres/regulator/regulator.h>
49 mmc_fdt_parse_sd_speed(phandle_t node, struct mmc_host *host)
54 * Parse SD supported modes
55 * All UHS-I modes requires 1.8V signaling.
57 if (OF_hasprop(node, "no1-8-v"))
59 if (OF_hasprop(node, "cap-sd-highspeed"))
60 host->caps |= MMC_CAP_HSPEED;
61 if (OF_hasprop(node, "sd-uhs-sdr12") && no_18v == false)
62 host->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_SIGNALING_180;
63 if (OF_hasprop(node, "sd-uhs-sdr25") && no_18v == false)
64 host->caps |= MMC_CAP_UHS_SDR25 | MMC_CAP_SIGNALING_180;
65 if (OF_hasprop(node, "sd-uhs-sdr50") && no_18v == false)
66 host->caps |= MMC_CAP_UHS_SDR50 | MMC_CAP_SIGNALING_180;
67 if (OF_hasprop(node, "sd-uhs-sdr104") && no_18v == false)
68 host->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_SIGNALING_180;
69 if (OF_hasprop(node, "sd-uhs-ddr50") && no_18v == false)
70 host->caps |= MMC_CAP_UHS_DDR50 | MMC_CAP_SIGNALING_180;
74 mmc_fdt_parse_mmc_speed(phandle_t node, struct mmc_host *host)
77 /* Parse eMMC supported modes */
78 if (OF_hasprop(node, "cap-mmc-highspeed"))
79 host->caps |= MMC_CAP_HSPEED;
80 if (OF_hasprop(node, "mmc-ddr-1_2v"))
81 host->caps |= MMC_CAP_MMC_DDR52_120 | MMC_CAP_SIGNALING_120;
82 if (OF_hasprop(node, "mmc-ddr-1_8v"))
83 host->caps |= MMC_CAP_MMC_DDR52_180 | MMC_CAP_SIGNALING_180;
84 if (OF_hasprop(node, "mmc-ddr-3_3v"))
85 host->caps |= MMC_CAP_SIGNALING_330;
86 if (OF_hasprop(node, "mmc-hs200-1_2v"))
87 host->caps |= MMC_CAP_MMC_HS200_120 | MMC_CAP_SIGNALING_120;
88 if (OF_hasprop(node, "mmc-hs200-1_8v"))
89 host->caps |= MMC_CAP_MMC_HS200_180 | MMC_CAP_SIGNALING_180;
90 if (OF_hasprop(node, "mmc-hs400-1_2v"))
91 host->caps |= MMC_CAP_MMC_HS400_120 | MMC_CAP_SIGNALING_120;
92 if (OF_hasprop(node, "mmc-hs400-1_8v"))
93 host->caps |= MMC_CAP_MMC_HS400_180 | MMC_CAP_SIGNALING_180;
94 if (OF_hasprop(node, "mmc-hs400-enhanced-strobe"))
95 host->caps |= MMC_CAP_MMC_ENH_STROBE;
99 mmc_fdt_parse(device_t dev, phandle_t node, struct mmc_fdt_helper *helper,
100 struct mmc_host *host)
105 node = ofw_bus_get_node(dev);
109 if (OF_getencprop(node, "bus-width", &bus_width, sizeof(uint32_t)) <= 0)
113 host->caps |= MMC_CAP_4_BIT_DATA;
115 host->caps |= MMC_CAP_8_BIT_DATA;
118 * max-frequency is optional, drivers should tweak this value
119 * if it's not present based on the clock that the mmc controller
122 OF_getencprop(node, "max-frequency", &host->f_max, sizeof(uint32_t));
124 if (OF_hasprop(node, "broken-cd"))
125 helper->props |= MMC_PROP_BROKEN_CD;
126 if (OF_hasprop(node, "non-removable"))
127 helper->props |= MMC_PROP_NON_REMOVABLE;
128 if (OF_hasprop(node, "wp-inverted"))
129 helper->props |= MMC_PROP_WP_INVERTED;
130 if (OF_hasprop(node, "cd-inverted"))
131 helper->props |= MMC_PROP_CD_INVERTED;
132 if (OF_hasprop(node, "no-sdio"))
133 helper->props |= MMC_PROP_NO_SDIO;
134 if (OF_hasprop(node, "no-sd"))
135 helper->props |= MMC_PROP_NO_SD;
136 if (OF_hasprop(node, "no-mmc"))
137 helper->props |= MMC_PROP_NO_MMC;
139 if (!(helper->props & MMC_PROP_NO_SD))
140 mmc_fdt_parse_sd_speed(node, host);
142 if (!(helper->props & MMC_PROP_NO_MMC))
143 mmc_fdt_parse_mmc_speed(node, host);
147 * Get the regulators if they are supported and
148 * clean the non supported modes based on the available voltages.
150 if (regulator_get_by_ofw_property(dev, 0, "vmmc-supply",
151 &helper->vmmc_supply) == 0) {
153 device_printf(dev, "vmmc-supply regulator found\n");
155 if (regulator_get_by_ofw_property(dev, 0, "vqmmc-supply",
156 &helper->vqmmc_supply) == 0 && bootverbose) {
158 device_printf(dev, "vqmmc-supply regulator found\n");
161 if (helper->vqmmc_supply != NULL) {
162 if (regulator_check_voltage(helper->vqmmc_supply, 1200000) == 0)
163 host->caps |= MMC_CAP_SIGNALING_120;
165 host->caps &= ~( MMC_CAP_MMC_HS400_120 |
166 MMC_CAP_MMC_HS200_120 |
167 MMC_CAP_MMC_DDR52_120);
168 if (regulator_check_voltage(helper->vqmmc_supply, 1800000) == 0)
169 host->caps |= MMC_CAP_SIGNALING_180;
171 host->caps &= ~(MMC_CAP_MMC_HS400_180 |
172 MMC_CAP_MMC_HS200_180 |
173 MMC_CAP_MMC_DDR52_180 |
178 if (regulator_check_voltage(helper->vqmmc_supply, 3300000) == 0)
179 host->caps |= MMC_CAP_SIGNALING_330;
181 host->caps |= MMC_CAP_SIGNALING_330;
188 * Card detect interrupt handler.
193 struct mmc_fdt_helper *helper = arg;
195 taskqueue_enqueue_timeout(taskqueue_swi_giant,
196 &helper->cd_delayed_task, -(hz / 2));
200 cd_card_task(void *arg, int pending __unused)
202 struct mmc_fdt_helper *helper = arg;
205 cd_present = mmc_fdt_gpio_get_present(helper);
206 if(helper->cd_handler && cd_present != helper->cd_present)
207 helper->cd_handler(helper->dev,
209 helper->cd_present = cd_present;
211 /* If we're polling re-schedule the task */
212 if (helper->cd_ihandler == NULL)
213 taskqueue_enqueue_timeout_sbt(taskqueue_swi_giant,
214 &helper->cd_delayed_task, mstosbt(500), 0, C_PREL(2));
221 cd_setup(struct mmc_fdt_helper *helper, phandle_t node)
225 const char *cd_mode_str;
229 TIMEOUT_TASK_INIT(taskqueue_swi_giant, &helper->cd_delayed_task, 0,
230 cd_card_task, helper);
233 * If the device is flagged as non-removable, set that slot option, and
234 * set a flag to make sdhci_fdt_gpio_get_present() always return true.
236 if (helper->props & MMC_PROP_NON_REMOVABLE) {
237 helper->cd_disabled = true;
239 device_printf(dev, "Non-removable media\n");
244 * If there is no cd-gpios property, then presumably the hardware
245 * PRESENT_STATE register and interrupts will reflect card state
246 * properly, and there's nothing more for us to do. Our get_present()
247 * will return sdhci_generic_get_card_present() because cd_pin is NULL.
249 * If there is a property, make sure we can read the pin.
251 if (gpio_pin_get_by_ofw_property(dev, node, "cd-gpios",
255 if (gpio_pin_getcaps(helper->cd_pin, &pincaps) != 0 ||
256 !(pincaps & GPIO_PIN_INPUT)) {
257 device_printf(dev, "Cannot read card-detect gpio pin; "
258 "setting card-always-present flag.\n");
259 helper->cd_disabled = true;
264 * If the pin can trigger an interrupt on both rising and falling edges,
265 * we can use it to detect card presence changes. If not, we'll request
266 * card presence polling instead of using interrupts.
268 if (!(pincaps & GPIO_INTR_EDGE_BOTH)) {
270 device_printf(dev, "Cannot configure "
271 "GPIO_INTR_EDGE_BOTH for card detect\n");
272 goto without_interrupts;
275 if (helper->cd_handler == NULL) {
277 device_printf(dev, "Cannot configure "
278 "interrupts as no cd_handler is set\n");
279 goto without_interrupts;
283 * Create an interrupt resource from the pin and set up the interrupt.
285 if ((helper->cd_ires = gpio_alloc_intr_resource(dev, &helper->cd_irid,
286 RF_ACTIVE, helper->cd_pin, GPIO_INTR_EDGE_BOTH)) == NULL) {
288 device_printf(dev, "Cannot allocate an IRQ for card "
290 goto without_interrupts;
293 if (bus_setup_intr(dev, helper->cd_ires, INTR_TYPE_BIO | INTR_MPSAFE,
294 NULL, cd_intr, helper, &helper->cd_ihandler) != 0) {
295 device_printf(dev, "Unable to setup card-detect irq handler\n");
296 helper->cd_ihandler = NULL;
297 goto without_interrupts;
302 * If we have a readable gpio pin, but didn't successfully configure
303 * gpio interrupts, setup a timeout task to poll the pin
305 if (helper->cd_ihandler == NULL) {
306 cd_mode_str = "polling";
308 cd_mode_str = "interrupts";
312 device_printf(dev, "Card presence detect on %s pin %u, "
313 "configured for %s.\n",
314 device_get_nameunit(helper->cd_pin->dev), helper->cd_pin->pin,
320 * Write protect setup.
323 wp_setup(struct mmc_fdt_helper *helper, phandle_t node)
329 if (OF_hasprop(node, "disable-wp")) {
330 helper->wp_disabled = true;
332 device_printf(dev, "Write protect disabled\n");
336 if (gpio_pin_get_by_ofw_property(dev, node, "wp-gpios", &helper->wp_pin))
340 device_printf(dev, "Write protect switch on %s pin %u\n",
341 device_get_nameunit(helper->wp_pin->dev), helper->wp_pin->pin);
345 mmc_fdt_gpio_setup(device_t dev, phandle_t node, struct mmc_fdt_helper *helper,
346 mmc_fdt_cd_handler handler)
350 node = ofw_bus_get_node(dev);
352 device_printf(dev, "Cannot get node for device\n");
357 helper->cd_handler = handler;
358 cd_setup(helper, node);
359 wp_setup(helper, node);
362 * Schedule a card detection
364 taskqueue_enqueue_timeout_sbt(taskqueue_swi_giant,
365 &helper->cd_delayed_task, mstosbt(500), 0, C_PREL(2));
370 mmc_fdt_gpio_teardown(struct mmc_fdt_helper *helper)
376 if (helper->cd_ihandler != NULL)
377 bus_teardown_intr(helper->dev, helper->cd_ires, helper->cd_ihandler);
378 if (helper->wp_pin != NULL)
379 gpio_pin_release(helper->wp_pin);
380 if (helper->cd_pin != NULL)
381 gpio_pin_release(helper->cd_pin);
382 if (helper->cd_ires != NULL)
383 bus_release_resource(helper->dev, SYS_RES_IRQ, 0, helper->cd_ires);
385 taskqueue_drain_timeout(taskqueue_swi_giant, &helper->cd_delayed_task);
389 mmc_fdt_gpio_get_present(struct mmc_fdt_helper *helper)
393 if (helper->cd_disabled)
395 if (helper->cd_pin == NULL)
398 gpio_pin_is_active(helper->cd_pin, &pinstate);
400 return (pinstate ^ (helper->props & MMC_PROP_CD_INVERTED));
404 mmc_fdt_gpio_get_readonly(struct mmc_fdt_helper *helper)
408 if (helper->wp_disabled)
411 if (helper->wp_pin == NULL)
414 gpio_pin_is_active(helper->wp_pin, &pinstate);
416 return (pinstate ^ (helper->props & MMC_PROP_WP_INVERTED));