summaryrefslogtreecommitdiffstats
path: root/freebsd/sys/dev/sdhci/sdhci_fdt_gpio.c
blob: 2490d49b6f74d391fb4c3015aec563f583caf55e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include <machine/rtems-bsd-kernel-space.h>

/*-
 * Copyright (c) 2017 Ian Lepore <ian@freebsd.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Support routines usable by any SoC sdhci bridge driver that uses gpio pins
 * for card detect and write protect, and uses FDT data to describe those pins.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/bus.h>
#include <sys/gpio.h>
#include <sys/sysctl.h>
#include <sys/systm.h>
#include <sys/taskqueue.h>

#include <dev/gpio/gpiobusvar.h>
#include <dev/mmc/bridge.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#include <dev/sdhci/sdhci.h>
#include <dev/sdhci/sdhci_fdt_gpio.h>

struct sdhci_fdt_gpio {
	device_t		dev;
	struct sdhci_slot *	slot;
	gpio_pin_t		wp_pin;
	gpio_pin_t		cd_pin;
	void *			cd_ihandler;
	struct resource *	cd_ires;
	int			cd_irid;
	bool			wp_disabled;
	bool			wp_inverted;
	bool			cd_disabled;
	bool			cd_inverted;
};

/*
 * Card detect interrupt handler.
 */
static void
cd_intr(void *arg)
{
	struct sdhci_fdt_gpio *gpio = arg;

	sdhci_handle_card_present(gpio->slot, sdhci_fdt_gpio_get_present(gpio));
}

/*
 * Card detect setup.
 */
static void
cd_setup(struct sdhci_fdt_gpio *gpio, phandle_t node)
{
	int pincaps;
	device_t dev;
	const char *cd_mode_str;

	dev = gpio->dev;

	/*
	 * If the device is flagged as non-removable, set that slot option, and
	 * set a flag to make sdhci_fdt_gpio_get_present() always return true.
	 */
	if (OF_hasprop(node, "non-removable")) {
		gpio->slot->opt |= SDHCI_NON_REMOVABLE;
		gpio->cd_disabled = true;
		if (bootverbose)
			device_printf(dev, "Non-removable media\n");
		return;
	}

	/*
	 * If there is no cd-gpios property, then presumably the hardware
	 * PRESENT_STATE register and interrupts will reflect card state
	 * properly, and there's nothing more for us to do.  Our get_present()
	 * will return sdhci_generic_get_card_present() because cd_pin is NULL.
	 *
	 * If there is a property, make sure we can read the pin.
	 */
	if (gpio_pin_get_by_ofw_property(dev, node, "cd-gpios", &gpio->cd_pin))
		return;

	if (gpio_pin_getcaps(gpio->cd_pin, &pincaps) != 0 ||
	    !(pincaps & GPIO_PIN_INPUT)) {
		device_printf(dev, "Cannot read card-detect gpio pin; "
		    "setting card-always-present flag.\n");
		gpio->cd_disabled = true;
		return;
	}

	if (OF_hasprop(node, "cd-inverted"))
		gpio->cd_inverted = true;

	/*
	 * If the pin can trigger an interrupt on both rising and falling edges,
	 * we can use it to detect card presence changes.  If not, we'll request
	 * card presence polling instead of using interrupts.
	 */
	if (!(pincaps & GPIO_INTR_EDGE_BOTH)) {
		if (bootverbose)
			device_printf(dev, "Cannot configure "
			    "GPIO_INTR_EDGE_BOTH for card detect\n");
		goto without_interrupts;
	}

	/*
	 * Create an interrupt resource from the pin and set up the interrupt.
	 */
	if ((gpio->cd_ires = gpio_alloc_intr_resource(dev, &gpio->cd_irid,
	    RF_ACTIVE, gpio->cd_pin, GPIO_INTR_EDGE_BOTH)) == NULL) {
		if (bootverbose)
			device_printf(dev, "Cannot allocate an IRQ for card "
			    "detect GPIO\n");
		goto without_interrupts;
	}

	if (bus_setup_intr(dev, gpio->cd_ires, INTR_TYPE_BIO | INTR_MPSAFE,
	    NULL, cd_intr, gpio, &gpio->cd_ihandler) != 0) {
		device_printf(dev, "Unable to setup card-detect irq handler\n");
		gpio->cd_ihandler = NULL;
		goto without_interrupts;
	}

without_interrupts:

	/*
	 * If we have a readable gpio pin, but didn't successfully configure
	 * gpio interrupts, ask the sdhci driver to poll from a callout.
	 */
	if (gpio->cd_ihandler == NULL) {
		cd_mode_str = "polling";
		gpio->slot->quirks |= SDHCI_QUIRK_POLL_CARD_PRESENT;
	} else {
		cd_mode_str = "interrupts";
	}

	if (bootverbose) {
		device_printf(dev, "Card presence detect on %s pin %u, "
		    "configured for %s.\n",
		    device_get_nameunit(gpio->cd_pin->dev), gpio->cd_pin->pin,
		    cd_mode_str);
	}
}

/*
 * Write protect setup.
 */
static void
wp_setup(struct sdhci_fdt_gpio *gpio, phandle_t node)
{
	device_t dev;

	dev = gpio->dev;

	if (OF_hasprop(node, "wp-disable")) {
		gpio->wp_disabled = true;
		if (bootverbose)
			device_printf(dev, "Write protect disabled\n");
		return;
	}

	if (gpio_pin_get_by_ofw_property(dev, node, "wp-gpios", &gpio->wp_pin))
		return;

	if (OF_hasprop(node, "wp-inverted"))
		gpio->wp_inverted = true;

	if (bootverbose)
		device_printf(dev, "Write protect switch on %s pin %u\n",
		    device_get_nameunit(gpio->wp_pin->dev), gpio->wp_pin->pin);
}

struct sdhci_fdt_gpio *
sdhci_fdt_gpio_setup(device_t dev, struct sdhci_slot *slot)
{
	phandle_t node;
	struct sdhci_fdt_gpio *gpio;

	gpio = malloc(sizeof(*gpio), M_DEVBUF, M_ZERO | M_WAITOK);
	gpio->dev  = dev;
	gpio->slot = slot;

	node = ofw_bus_get_node(dev);

	wp_setup(gpio, node);
	cd_setup(gpio, node);

	return (gpio);
}

void
sdhci_fdt_gpio_teardown(struct sdhci_fdt_gpio *gpio)
{

	if (gpio == NULL)
		return;

	if (gpio->cd_ihandler != NULL)
		bus_teardown_intr(gpio->dev, gpio->cd_ires, gpio->cd_ihandler);
	if (gpio->wp_pin != NULL)
		gpio_pin_release(gpio->wp_pin);
	if (gpio->cd_pin != NULL)
		gpio_pin_release(gpio->cd_pin);
	if (gpio->cd_ires != NULL)
		bus_release_resource(gpio->dev, SYS_RES_IRQ, 0, gpio->cd_ires);

	free(gpio, M_DEVBUF);
}

bool
sdhci_fdt_gpio_get_present(struct sdhci_fdt_gpio *gpio)
{
	bool pinstate;

	if (gpio->cd_disabled)
		return (true);

	if (gpio->cd_pin == NULL)
		return (sdhci_generic_get_card_present(gpio->slot->bus,
		    gpio->slot));

	gpio_pin_is_active(gpio->cd_pin, &pinstate);

	return (pinstate ^ gpio->cd_inverted);
}

int
sdhci_fdt_gpio_get_readonly(struct sdhci_fdt_gpio *gpio)
{
	bool pinstate;

	if (gpio->wp_disabled)
		return (false);

	if (gpio->wp_pin == NULL)
		return (sdhci_generic_get_ro(gpio->slot->bus, gpio->slot->dev));

	gpio_pin_is_active(gpio->wp_pin, &pinstate);

	return (pinstate ^ gpio->wp_inverted);
}