xref: /freebsd/sys/dev/mmc/mmc_fdt_helpers.c (revision 031beb4e239bfce798af17f5fe8dba8bcaf13d99)
1 /*
2  * Copyright 2019 Emmanuel Vadot <manu@freebsd.org>
3  * Copyright (c) 2017 Ian Lepore <ian@freebsd.org> All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
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.
14  *
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.
26  */
27 
28 #include <sys/cdefs.h>
29 #include <sys/param.h>
30 #include <sys/bus.h>
31 #include <sys/kernel.h>
32 #include <sys/gpio.h>
33 #include <sys/taskqueue.h>
34 
35 #include <dev/mmc/bridge.h>
36 #include <dev/mmc/mmc_fdt_helpers.h>
37 
38 #include <dev/gpio/gpiobusvar.h>
39 #include <dev/ofw/ofw_bus.h>
40 #include <dev/ofw/ofw_bus_subr.h>
41 
42 #include <dev/extres/regulator/regulator.h>
43 
44 #include <dev/mmc/mmc_helpers.h>
45 
46 #include "mmc_pwrseq_if.h"
47 
48 int
49 mmc_fdt_parse(device_t dev, phandle_t node, struct mmc_helper *helper,
50     struct mmc_host *host)
51 {
52 	struct mmc_helper mmc_helper;
53 	phandle_t pwrseq_xref;
54 
55 	memset(&mmc_helper, 0, sizeof(mmc_helper));
56 	mmc_parse(dev, &mmc_helper, host);
57 
58 	helper->props = mmc_helper.props;
59 
60 	/*
61 	 * Get the regulators if they are supported and
62 	 * clean the non supported modes based on the available voltages.
63 	 */
64 	if (regulator_get_by_ofw_property(dev, 0, "vmmc-supply",
65 	    &helper->vmmc_supply) == 0) {
66 		if (bootverbose)
67 			device_printf(dev, "vmmc-supply regulator found\n");
68 	}
69 	if (regulator_get_by_ofw_property(dev, 0, "vqmmc-supply",
70 	    &helper->vqmmc_supply) == 0 && bootverbose) {
71 		if (bootverbose)
72 			device_printf(dev, "vqmmc-supply regulator found\n");
73 	}
74 
75 	if (helper->vqmmc_supply != NULL) {
76 		if (regulator_check_voltage(helper->vqmmc_supply, 1200000) == 0)
77 			host->caps |= MMC_CAP_SIGNALING_120;
78 		else
79 			host->caps &= ~( MMC_CAP_MMC_HS400_120 |
80 			    MMC_CAP_MMC_HS200_120 |
81 			    MMC_CAP_MMC_DDR52_120);
82 		if (regulator_check_voltage(helper->vqmmc_supply, 1800000) == 0)
83 			host->caps |= MMC_CAP_SIGNALING_180;
84 		else
85 			host->caps &= ~(MMC_CAP_MMC_HS400_180 |
86 			    MMC_CAP_MMC_HS200_180 |
87 			    MMC_CAP_MMC_DDR52_180 |
88 			    MMC_CAP_UHS_DDR50 |
89 			    MMC_CAP_UHS_SDR104 |
90 			    MMC_CAP_UHS_SDR50 |
91 			    MMC_CAP_UHS_SDR25);
92 		if (regulator_check_voltage(helper->vqmmc_supply, 3300000) == 0)
93 			host->caps |= MMC_CAP_SIGNALING_330;
94 	} else
95 		host->caps |= MMC_CAP_SIGNALING_330;
96 
97 	if (OF_hasprop(node, "mmc-pwrseq")) {
98 		if (OF_getencprop(node, "mmc-pwrseq", &pwrseq_xref, sizeof(pwrseq_xref)) == -1) {
99 			device_printf(dev, "Cannot get the pwrseq_xref property\n");
100 			return (ENXIO);
101 		}
102 		helper->mmc_pwrseq = OF_device_from_xref(pwrseq_xref);
103 	}
104 	return (0);
105 }
106 
107 /*
108  * Card detect interrupt handler.
109  */
110 static void
111 cd_intr(void *arg)
112 {
113 	struct mmc_helper *helper = arg;
114 
115 	taskqueue_enqueue_timeout(taskqueue_swi_giant,
116 	    &helper->cd_delayed_task, -(hz / 2));
117 }
118 
119 static void
120 cd_card_task(void *arg, int pending __unused)
121 {
122 	struct mmc_helper *helper = arg;
123 	bool cd_present;
124 
125 	cd_present = mmc_fdt_gpio_get_present(helper);
126 	if(helper->cd_handler && cd_present != helper->cd_present)
127 		helper->cd_handler(helper->dev,
128 		    cd_present);
129 	helper->cd_present = cd_present;
130 
131 	/* If we're polling re-schedule the task */
132 	if (helper->cd_ihandler == NULL)
133 		taskqueue_enqueue_timeout_sbt(taskqueue_swi_giant,
134 		    &helper->cd_delayed_task, mstosbt(500), 0, C_PREL(2));
135 }
136 
137 /*
138  * Card detect setup.
139  */
140 static void
141 cd_setup(struct mmc_helper *helper, phandle_t node)
142 {
143 	int pincaps;
144 	device_t dev;
145 	const char *cd_mode_str;
146 
147 	dev = helper->dev;
148 
149 	TIMEOUT_TASK_INIT(taskqueue_swi_giant, &helper->cd_delayed_task, 0,
150 	    cd_card_task, helper);
151 
152 	/*
153 	 * If the device is flagged as non-removable, set that slot option, and
154 	 * set a flag to make sdhci_fdt_gpio_get_present() always return true.
155 	 */
156 	if (helper->props & MMC_PROP_NON_REMOVABLE) {
157 		helper->cd_disabled = true;
158 		if (bootverbose)
159 			device_printf(dev, "Non-removable media\n");
160 		return;
161 	}
162 
163 	/*
164 	 * If there is no cd-gpios property, then presumably the hardware
165 	 * PRESENT_STATE register and interrupts will reflect card state
166 	 * properly, and there's nothing more for us to do.  Our get_present()
167 	 * will return sdhci_generic_get_card_present() because cd_pin is NULL.
168 	 *
169 	 * If there is a property, make sure we can read the pin.
170 	 */
171 	if (gpio_pin_get_by_ofw_property(dev, node, "cd-gpios",
172 	    &helper->cd_pin))
173 		return;
174 
175 	if (gpio_pin_getcaps(helper->cd_pin, &pincaps) != 0 ||
176 	    !(pincaps & GPIO_PIN_INPUT)) {
177 		device_printf(dev, "Cannot read card-detect gpio pin; "
178 		    "setting card-always-present flag.\n");
179 		helper->cd_disabled = true;
180 		return;
181 	}
182 
183 	/*
184 	 * If the pin can trigger an interrupt on both rising and falling edges,
185 	 * we can use it to detect card presence changes.  If not, we'll request
186 	 * card presence polling instead of using interrupts.
187 	 */
188 	if (!(pincaps & GPIO_INTR_EDGE_BOTH)) {
189 		if (bootverbose)
190 			device_printf(dev, "Cannot configure "
191 			    "GPIO_INTR_EDGE_BOTH for card detect\n");
192 		goto without_interrupts;
193 	}
194 
195 	if (helper->cd_handler == NULL) {
196 		if (bootverbose)
197 			device_printf(dev, "Cannot configure "
198 			    "interrupts as no cd_handler is set\n");
199 		goto without_interrupts;
200 	}
201 
202 	/*
203 	 * Create an interrupt resource from the pin and set up the interrupt.
204 	 */
205 	if ((helper->cd_ires = gpio_alloc_intr_resource(dev, &helper->cd_irid,
206 	    RF_ACTIVE, helper->cd_pin, GPIO_INTR_EDGE_BOTH)) == NULL) {
207 		if (bootverbose)
208 			device_printf(dev, "Cannot allocate an IRQ for card "
209 			    "detect GPIO\n");
210 		goto without_interrupts;
211 	}
212 
213 	if (bus_setup_intr(dev, helper->cd_ires, INTR_TYPE_BIO | INTR_MPSAFE,
214 	    NULL, cd_intr, helper, &helper->cd_ihandler) != 0) {
215 		device_printf(dev, "Unable to setup card-detect irq handler\n");
216 		helper->cd_ihandler = NULL;
217 		goto without_interrupts;
218 	}
219 
220 without_interrupts:
221 	/*
222 	 * If we have a readable gpio pin, but didn't successfully configure
223 	 * gpio interrupts, setup a timeout task to poll the pin
224 	 */
225 	if (helper->cd_ihandler == NULL) {
226 		cd_mode_str = "polling";
227 	} else {
228 		cd_mode_str = "interrupts";
229 	}
230 
231 	if (bootverbose) {
232 		device_printf(dev, "Card presence detect on %s pin %u, "
233 		    "configured for %s.\n",
234 		    device_get_nameunit(helper->cd_pin->dev), helper->cd_pin->pin,
235 		    cd_mode_str);
236 	}
237 }
238 
239 /*
240  * Write protect setup.
241  */
242 static void
243 wp_setup(struct mmc_helper *helper, phandle_t node)
244 {
245 	device_t dev;
246 
247 	dev = helper->dev;
248 
249 	if (OF_hasprop(node, "disable-wp")) {
250 		helper->wp_disabled = true;
251 		if (bootverbose)
252 			device_printf(dev, "Write protect disabled\n");
253 		return;
254 	}
255 
256 	if (gpio_pin_get_by_ofw_property(dev, node, "wp-gpios", &helper->wp_pin))
257 		return;
258 
259 	if (bootverbose)
260 		device_printf(dev, "Write protect switch on %s pin %u\n",
261 		    device_get_nameunit(helper->wp_pin->dev), helper->wp_pin->pin);
262 }
263 
264 int
265 mmc_fdt_gpio_setup(device_t dev, phandle_t node, struct mmc_helper *helper,
266     mmc_fdt_cd_handler handler)
267 {
268 
269 	if (node <= 0)
270 		node = ofw_bus_get_node(dev);
271 	if (node <= 0) {
272 		device_printf(dev, "Cannot get node for device\n");
273 		return (ENXIO);
274 	}
275 
276 	helper->dev = dev;
277 	helper->cd_handler = handler;
278 	cd_setup(helper, node);
279 	wp_setup(helper, node);
280 
281 	/*
282 	 * Schedule a card detection
283 	 */
284 	taskqueue_enqueue_timeout_sbt(taskqueue_swi_giant,
285 	    &helper->cd_delayed_task, mstosbt(500), 0, C_PREL(2));
286 	return (0);
287 }
288 
289 void
290 mmc_fdt_gpio_teardown(struct mmc_helper *helper)
291 {
292 
293 	if (helper == NULL)
294 		return;
295 
296 	if (helper->cd_ihandler != NULL)
297 		bus_teardown_intr(helper->dev, helper->cd_ires, helper->cd_ihandler);
298 	if (helper->wp_pin != NULL)
299 		gpio_pin_release(helper->wp_pin);
300 	if (helper->cd_pin != NULL)
301 		gpio_pin_release(helper->cd_pin);
302 	if (helper->cd_ires != NULL)
303 		bus_release_resource(helper->dev, SYS_RES_IRQ, 0, helper->cd_ires);
304 
305 	taskqueue_drain_timeout(taskqueue_swi_giant, &helper->cd_delayed_task);
306 }
307 
308 bool
309 mmc_fdt_gpio_get_present(struct mmc_helper *helper)
310 {
311 	bool pinstate;
312 
313 	if (helper->cd_disabled)
314 		return (true);
315 	if (helper->cd_pin == NULL)
316 		return (false);
317 
318 	gpio_pin_is_active(helper->cd_pin, &pinstate);
319 
320 	return (pinstate ^ (bool)(helper->props & MMC_PROP_CD_INVERTED));
321 }
322 
323 bool
324 mmc_fdt_gpio_get_readonly(struct mmc_helper *helper)
325 {
326 	bool pinstate;
327 
328 	if (helper->wp_disabled)
329 		return (false);
330 
331 	if (helper->wp_pin == NULL)
332 		return (false);
333 
334 	gpio_pin_is_active(helper->wp_pin, &pinstate);
335 
336 	return (pinstate ^ (bool)(helper->props & MMC_PROP_WP_INVERTED));
337 }
338 
339 void
340 mmc_fdt_set_power(struct mmc_helper *helper, enum mmc_power_mode power_mode)
341 {
342 	int reg_status;
343 	int rv;
344 
345 	switch (power_mode) {
346 	case power_on:
347 		break;
348 	case power_off:
349 		if (helper->vmmc_supply) {
350 			rv = regulator_status(helper->vmmc_supply, &reg_status);
351 			if (rv == 0 && reg_status == REGULATOR_STATUS_ENABLED)
352 				regulator_disable(helper->vmmc_supply);
353 		}
354 		if (helper->vqmmc_supply) {
355 			rv = regulator_status(helper->vqmmc_supply, &reg_status);
356 			if (rv == 0 && reg_status == REGULATOR_STATUS_ENABLED)
357 				regulator_disable(helper->vqmmc_supply);
358 		}
359 		if (helper->mmc_pwrseq)
360 			MMC_PWRSEQ_SET_POWER(helper->mmc_pwrseq, false);
361 		break;
362 	case power_up:
363 		if (helper->vmmc_supply) {
364 			rv = regulator_status(helper->vmmc_supply, &reg_status);
365 			if (rv == 0 && reg_status != REGULATOR_STATUS_ENABLED)
366 				regulator_enable(helper->vmmc_supply);
367 		}
368 		if (helper->vqmmc_supply) {
369 			rv = regulator_status(helper->vqmmc_supply, &reg_status);
370 			if (rv == 0 && reg_status != REGULATOR_STATUS_ENABLED)
371 				regulator_enable(helper->vqmmc_supply);
372 		}
373 		if (helper->mmc_pwrseq)
374 			MMC_PWRSEQ_SET_POWER(helper->mmc_pwrseq, true);
375 		break;
376 	}
377 }
378