aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/mmc/mmc_pwrseq.c
blob: 6e41ac81e4dc551e8381fa3de5319ab104450e74 (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
/*
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright 2021 Emmanuel Vadot <manu@freebsd.org>
 *
 * 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 AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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.
 */

#include <sys/cdefs.h>
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/gpio.h>

#include <dev/gpio/gpiobusvar.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>

#include <dev/extres/clk/clk.h>

#include "mmc_pwrseq_if.h"

enum pwrseq_type {
	PWRSEQ_SIMPLE = 1,
	PWRSEQ_EMMC,
};

static struct ofw_compat_data compat_data[] = {
	{ "mmc-pwrseq-simple",	PWRSEQ_SIMPLE },
	{ "mmc-pwrseq-emmc",	PWRSEQ_EMMC },
	{ NULL,			0 }
};

struct mmc_pwrseq_softc {
	enum pwrseq_type	type;
	clk_t			ext_clock;
	struct gpiobus_pin	*reset_gpio;

	uint32_t		post_power_on_delay_ms;
	uint32_t		power_off_delay_us;
};

static int
mmc_pwrseq_probe(device_t dev)
{
	enum pwrseq_type type;

	if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
		return (ENXIO);

	type = (enum pwrseq_type)ofw_bus_search_compatible(dev, compat_data)->ocd_data;
	switch (type) {
	case PWRSEQ_SIMPLE:
		device_set_desc(dev, "MMC Simple Power sequence");
		break;
	case PWRSEQ_EMMC:
		device_set_desc(dev, "MMC eMMC Power sequence");
		break;
	}
	return (BUS_PROBE_DEFAULT);
}

static int
mmc_pwrseq_attach(device_t dev)
{
	struct mmc_pwrseq_softc *sc;
	phandle_t node;
	int rv;

	sc = device_get_softc(dev);
	sc->type = (enum pwrseq_type)ofw_bus_search_compatible(dev, compat_data)->ocd_data;
	node = ofw_bus_get_node(dev);

	if (sc->type == PWRSEQ_SIMPLE) {
		if (OF_hasprop(node, "clocks")) {
			rv = clk_get_by_ofw_name(dev, 0, "ext_clock", &sc->ext_clock);
			if (rv != 0) {
				device_printf(dev,
				    "Node have a clocks property but no clocks named \"ext_clock\"\n");
				return (ENXIO);
			}
		}
		OF_getencprop(node, "post-power-on-delay-ms", &sc->post_power_on_delay_ms, sizeof(uint32_t));
		OF_getencprop(node, "power-off-delay-us", &sc->power_off_delay_us, sizeof(uint32_t));
	}

	if (OF_hasprop(node, "reset-gpios")) {
		if (gpio_pin_get_by_ofw_property(dev, node, "reset-gpios",
		    &sc->reset_gpio) != 0) {
			device_printf(dev, "Cannot get the reset-gpios\n");
			return (ENXIO);
		}
		gpio_pin_setflags(sc->reset_gpio, GPIO_PIN_OUTPUT);
		gpio_pin_set_active(sc->reset_gpio, true);
	}

	OF_device_register_xref(OF_xref_from_node(node), dev);
	return (0);
}

static int
mmc_pwrseq_detach(device_t dev)
{

	return (EBUSY);
}

static int
mmv_pwrseq_set_power(device_t dev, bool power_on)
{
	struct mmc_pwrseq_softc *sc;
	int rv;

	sc = device_get_softc(dev);

	if (power_on) {
		if (sc->ext_clock) {
			rv = clk_enable(sc->ext_clock);
			if (rv != 0)
				return (rv);
		}

		if (sc->reset_gpio) {
			rv = gpio_pin_set_active(sc->reset_gpio, false);
			if (rv != 0)
				return (rv);
		}

		if (sc->post_power_on_delay_ms)
			DELAY(sc->post_power_on_delay_ms * 1000);
	} else {
		if (sc->reset_gpio) {
			rv = gpio_pin_set_active(sc->reset_gpio, true);
			if (rv != 0)
				return (rv);
		}

		if (sc->ext_clock) {
			rv = clk_stop(sc->ext_clock);
			if (rv != 0)
				return (rv);
		}
		if (sc->power_off_delay_us)
			DELAY(sc->power_off_delay_us);
	}

	return (0);
}

static device_method_t mmc_pwrseq_methods[] = {
	/* Device interface */
	DEVMETHOD(device_probe,		mmc_pwrseq_probe),
	DEVMETHOD(device_attach,	mmc_pwrseq_attach),
	DEVMETHOD(device_detach,	mmc_pwrseq_detach),

	DEVMETHOD(mmc_pwrseq_set_power,	mmv_pwrseq_set_power),
	DEVMETHOD_END
};

static driver_t mmc_pwrseq_driver = {
	"mmc_pwrseq",
	mmc_pwrseq_methods,
	sizeof(struct mmc_pwrseq_softc),
};

static devclass_t mmc_pwrseq_devclass;

EARLY_DRIVER_MODULE(mmc_pwrseq, simplebus, mmc_pwrseq_driver, mmc_pwrseq_devclass, 0, 0,
	BUS_PASS_SUPPORTDEV + BUS_PASS_ORDER_FIRST);
MODULE_VERSION(mmc_pwrseq, 1);
SIMPLEBUS_PNP_INFO(compat_data);