2 * Copyright (c) 2017 Ian Lepore <ian@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * Support routines usable by any SoC sdhci bridge driver that uses gpio pins
28 * for card detect and write protect, and uses FDT data to describe those pins.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
37 #include <sys/sysctl.h>
38 #include <sys/systm.h>
39 #include <sys/taskqueue.h>
41 #include <dev/gpio/gpiobusvar.h>
42 #include <dev/mmc/bridge.h>
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/ofw_bus_subr.h>
45 #include <dev/sdhci/sdhci.h>
46 #include <dev/sdhci/sdhci_fdt_gpio.h>
48 struct sdhci_fdt_gpio {
50 struct sdhci_slot * slot;
54 struct resource * cd_ires;
63 * Card detect interrupt handler.
68 struct sdhci_fdt_gpio *gpio = arg;
70 sdhci_handle_card_present(gpio->slot, sdhci_fdt_gpio_get_present(gpio));
77 cd_setup(struct sdhci_fdt_gpio *gpio, phandle_t node)
81 const char *cd_mode_str;
86 * If the device is flagged as non-removable, set that slot option, and
87 * set a flag to make sdhci_fdt_gpio_get_present() always return true.
89 if (OF_hasprop(node, "non-removable")) {
90 gpio->slot->opt |= SDHCI_NON_REMOVABLE;
91 gpio->cd_disabled = true;
93 device_printf(dev, "Non-removable media\n");
98 * If there is no cd-gpios property, then presumably the hardware
99 * PRESENT_STATE register and interrupts will reflect card state
100 * properly, and there's nothing more for us to do. Our get_present()
101 * will return sdhci_generic_get_card_present() because cd_pin is NULL.
103 * If there is a property, make sure we can read the pin.
105 if (gpio_pin_get_by_ofw_property(dev, node, "cd-gpios", &gpio->cd_pin))
108 if (gpio_pin_getcaps(gpio->cd_pin, &pincaps) != 0 ||
109 !(pincaps & GPIO_PIN_INPUT)) {
110 device_printf(dev, "Cannot read card-detect gpio pin; "
111 "setting card-always-present flag.\n");
112 gpio->cd_disabled = true;
116 if (OF_hasprop(node, "cd-inverted"))
117 gpio->cd_inverted = true;
120 * If the pin can trigger an interrupt on both rising and falling edges,
121 * we can use it to detect card presence changes. If not, we'll request
122 * card presence polling instead of using interrupts.
124 if (!(pincaps & GPIO_INTR_EDGE_BOTH)) {
126 device_printf(dev, "Cannot configure "
127 "GPIO_INTR_EDGE_BOTH for card detect\n");
128 goto without_interrupts;
132 * Create an interrupt resource from the pin and set up the interrupt.
134 if ((gpio->cd_ires = gpio_alloc_intr_resource(dev, &gpio->cd_irid,
135 RF_ACTIVE, gpio->cd_pin, GPIO_INTR_EDGE_BOTH)) == NULL) {
137 device_printf(dev, "Cannot allocate an IRQ for card "
139 goto without_interrupts;
142 if (bus_setup_intr(dev, gpio->cd_ires, INTR_TYPE_BIO | INTR_MPSAFE,
143 NULL, cd_intr, gpio, &gpio->cd_ihandler) != 0) {
144 device_printf(dev, "Unable to setup card-detect irq handler\n");
145 gpio->cd_ihandler = NULL;
146 goto without_interrupts;
152 * If we have a readable gpio pin, but didn't successfully configure
153 * gpio interrupts, ask the sdhci driver to poll from a callout.
155 if (gpio->cd_ihandler == NULL) {
156 cd_mode_str = "polling";
157 gpio->slot->quirks |= SDHCI_QUIRK_POLL_CARD_PRESENT;
159 cd_mode_str = "interrupts";
163 device_printf(dev, "Card presence detect on %s pin %u, "
164 "configured for %s.\n",
165 device_get_nameunit(gpio->cd_pin->dev), gpio->cd_pin->pin,
171 * Write protect setup.
174 wp_setup(struct sdhci_fdt_gpio *gpio, phandle_t node)
180 if (OF_hasprop(node, "wp-disable")) {
181 gpio->wp_disabled = true;
183 device_printf(dev, "Write protect disabled\n");
187 if (gpio_pin_get_by_ofw_property(dev, node, "wp-gpios", &gpio->wp_pin))
190 if (OF_hasprop(node, "wp-inverted"))
191 gpio->wp_inverted = true;
194 device_printf(dev, "Write protect switch on %s pin %u\n",
195 device_get_nameunit(gpio->wp_pin->dev), gpio->wp_pin->pin);
198 struct sdhci_fdt_gpio *
199 sdhci_fdt_gpio_setup(device_t dev, struct sdhci_slot *slot)
202 struct sdhci_fdt_gpio *gpio;
204 gpio = malloc(sizeof(*gpio), M_DEVBUF, M_ZERO | M_WAITOK);
208 node = ofw_bus_get_node(dev);
210 wp_setup(gpio, node);
211 cd_setup(gpio, node);
217 sdhci_fdt_gpio_teardown(struct sdhci_fdt_gpio *gpio)
223 if (gpio->cd_ihandler != NULL)
224 bus_teardown_intr(gpio->dev, gpio->cd_ires, gpio->cd_ihandler);
225 if (gpio->wp_pin != NULL)
226 gpio_pin_release(gpio->wp_pin);
227 if (gpio->cd_pin != NULL)
228 gpio_pin_release(gpio->cd_pin);
229 if (gpio->cd_ires != NULL)
230 bus_release_resource(gpio->dev, SYS_RES_IRQ, 0, gpio->cd_ires);
232 free(gpio, M_DEVBUF);
236 sdhci_fdt_gpio_get_present(struct sdhci_fdt_gpio *gpio)
240 if (gpio->cd_disabled)
243 if (gpio->cd_pin == NULL)
244 return (sdhci_generic_get_card_present(gpio->slot->bus,
247 gpio_pin_is_active(gpio->cd_pin, &pinstate);
249 return (pinstate ^ gpio->cd_inverted);
253 sdhci_fdt_gpio_get_readonly(struct sdhci_fdt_gpio *gpio)
257 if (gpio->wp_disabled)
260 if (gpio->wp_pin == NULL)
261 return (sdhci_generic_get_ro(gpio->slot->bus, gpio->slot->dev));
263 gpio_pin_is_active(gpio->wp_pin, &pinstate);
265 return (pinstate ^ gpio->wp_inverted);