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 * If the device is flagged as non-removable, set that slot option, and
230 * set a flag to make sdhci_fdt_gpio_get_present() always return true.
232 if (helper->props & MMC_PROP_NON_REMOVABLE) {
233 helper->cd_disabled = true;
235 device_printf(dev, "Non-removable media\n");
240 * If there is no cd-gpios property, then presumably the hardware
241 * PRESENT_STATE register and interrupts will reflect card state
242 * properly, and there's nothing more for us to do. Our get_present()
243 * will return sdhci_generic_get_card_present() because cd_pin is NULL.
245 * If there is a property, make sure we can read the pin.
247 if (gpio_pin_get_by_ofw_property(dev, node, "cd-gpios",
251 if (gpio_pin_getcaps(helper->cd_pin, &pincaps) != 0 ||
252 !(pincaps & GPIO_PIN_INPUT)) {
253 device_printf(dev, "Cannot read card-detect gpio pin; "
254 "setting card-always-present flag.\n");
255 helper->cd_disabled = true;
260 * If the pin can trigger an interrupt on both rising and falling edges,
261 * we can use it to detect card presence changes. If not, we'll request
262 * card presence polling instead of using interrupts.
264 if (!(pincaps & GPIO_INTR_EDGE_BOTH)) {
266 device_printf(dev, "Cannot configure "
267 "GPIO_INTR_EDGE_BOTH for card detect\n");
268 goto without_interrupts;
271 if (helper->cd_handler == NULL) {
273 device_printf(dev, "Cannot configure "
274 "interrupts as no cd_handler is set\n");
275 goto without_interrupts;
279 * Create an interrupt resource from the pin and set up the interrupt.
281 if ((helper->cd_ires = gpio_alloc_intr_resource(dev, &helper->cd_irid,
282 RF_ACTIVE, helper->cd_pin, GPIO_INTR_EDGE_BOTH)) == NULL) {
284 device_printf(dev, "Cannot allocate an IRQ for card "
286 goto without_interrupts;
289 if (bus_setup_intr(dev, helper->cd_ires, INTR_TYPE_BIO | INTR_MPSAFE,
290 NULL, cd_intr, helper, &helper->cd_ihandler) != 0) {
291 device_printf(dev, "Unable to setup card-detect irq handler\n");
292 helper->cd_ihandler = NULL;
293 goto without_interrupts;
297 TIMEOUT_TASK_INIT(taskqueue_swi_giant, &helper->cd_delayed_task, 0,
298 cd_card_task, helper);
301 * If we have a readable gpio pin, but didn't successfully configure
302 * gpio interrupts, setup a timeout task to poll the pin
304 if (helper->cd_ihandler == NULL) {
305 cd_mode_str = "polling";
307 cd_mode_str = "interrupts";
311 device_printf(dev, "Card presence detect on %s pin %u, "
312 "configured for %s.\n",
313 device_get_nameunit(helper->cd_pin->dev), helper->cd_pin->pin,
321 * Write protect setup.
324 wp_setup(struct mmc_fdt_helper *helper, phandle_t node)
330 if (OF_hasprop(node, "disable-wp")) {
331 helper->wp_disabled = true;
333 device_printf(dev, "Write protect disabled\n");
337 if (gpio_pin_get_by_ofw_property(dev, node, "wp-gpios", &helper->wp_pin))
341 device_printf(dev, "Write protect switch on %s pin %u\n",
342 device_get_nameunit(helper->wp_pin->dev), helper->wp_pin->pin);
346 mmc_fdt_gpio_setup(device_t dev, phandle_t node, struct mmc_fdt_helper *helper,
347 mmc_fdt_cd_handler handler)
351 node = ofw_bus_get_node(dev);
353 device_printf(dev, "Cannot get node for device\n");
358 helper->cd_handler = handler;
359 wp_setup(helper, node);
361 if (cd_setup(helper, node)) {
363 * Schedule a card detection
365 taskqueue_enqueue_timeout_sbt(taskqueue_swi_giant,
366 &helper->cd_delayed_task, mstosbt(500), 0, C_PREL(2));
373 mmc_fdt_gpio_teardown(struct mmc_fdt_helper *helper)
379 if (helper->cd_ihandler != NULL)
380 bus_teardown_intr(helper->dev, helper->cd_ires, helper->cd_ihandler);
381 if (helper->wp_pin != NULL)
382 gpio_pin_release(helper->wp_pin);
383 if (helper->cd_pin != NULL)
384 gpio_pin_release(helper->cd_pin);
385 if (helper->cd_ires != NULL)
386 bus_release_resource(helper->dev, SYS_RES_IRQ, 0, helper->cd_ires);
390 mmc_fdt_gpio_get_present(struct mmc_fdt_helper *helper)
394 if (helper->cd_disabled)
396 if (helper->cd_pin == NULL)
399 gpio_pin_is_active(helper->cd_pin, &pinstate);
401 return (pinstate ^ (helper->props & MMC_PROP_CD_INVERTED));
405 mmc_fdt_gpio_get_readonly(struct mmc_fdt_helper *helper)
409 if (helper->wp_disabled)
412 if (helper->wp_pin == NULL)
415 gpio_pin_is_active(helper->wp_pin, &pinstate);
417 return (pinstate ^ (helper->props & MMC_PROP_WP_INVERTED));