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