mmc_pwrseq_simple.c revision 1.1 1 1.1 jmcneill /* $NetBSD: mmc_pwrseq_simple.c,v 1.1 2017/10/22 13:56:49 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jmcneill * SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include <sys/cdefs.h>
30 1.1 jmcneill __KERNEL_RCSID(0, "$NetBSD: mmc_pwrseq_simple.c,v 1.1 2017/10/22 13:56:49 jmcneill Exp $");
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/param.h>
33 1.1 jmcneill #include <sys/kernel.h>
34 1.1 jmcneill #include <sys/device.h>
35 1.1 jmcneill #include <sys/systm.h>
36 1.1 jmcneill #include <sys/proc.h>
37 1.1 jmcneill #include <sys/gpio.h>
38 1.1 jmcneill
39 1.1 jmcneill #include <dev/fdt/fdtvar.h>
40 1.1 jmcneill
41 1.1 jmcneill #define MMCPWRSEQ_MAX_PINS 32
42 1.1 jmcneill
43 1.1 jmcneill static const char * const compatible[] = {
44 1.1 jmcneill "mmc-pwrseq-simple",
45 1.1 jmcneill NULL
46 1.1 jmcneill };
47 1.1 jmcneill
48 1.1 jmcneill struct mmcpwrseq_softc {
49 1.1 jmcneill device_t sc_dev;
50 1.1 jmcneill int sc_phandle;
51 1.1 jmcneill struct clk *sc_clk;
52 1.1 jmcneill struct fdtbus_gpio_pin *sc_pins[MMCPWRSEQ_MAX_PINS];
53 1.1 jmcneill u_int sc_npins;
54 1.1 jmcneill u_int sc_post_power_on_delay_ms;
55 1.1 jmcneill u_int sc_power_off_delay_us;
56 1.1 jmcneill };
57 1.1 jmcneill
58 1.1 jmcneill static void
59 1.1 jmcneill mmcpwrseq_pre_power_on(device_t dev)
60 1.1 jmcneill {
61 1.1 jmcneill struct mmcpwrseq_softc * const sc = device_private(dev);
62 1.1 jmcneill int error;
63 1.1 jmcneill
64 1.1 jmcneill if (sc->sc_clk) {
65 1.1 jmcneill error = clk_enable(sc->sc_clk);
66 1.1 jmcneill if (error != 0) {
67 1.1 jmcneill aprint_error_dev(dev, "failed to enable clock: %d\n",
68 1.1 jmcneill error);
69 1.1 jmcneill }
70 1.1 jmcneill }
71 1.1 jmcneill
72 1.1 jmcneill for (u_int n = 0; n < sc->sc_npins; n++)
73 1.1 jmcneill fdtbus_gpio_write(sc->sc_pins[n], 1);
74 1.1 jmcneill }
75 1.1 jmcneill
76 1.1 jmcneill static void
77 1.1 jmcneill mmcpwrseq_post_power_on(device_t dev)
78 1.1 jmcneill {
79 1.1 jmcneill struct mmcpwrseq_softc * const sc = device_private(dev);
80 1.1 jmcneill
81 1.1 jmcneill for (u_int n = 0; n < sc->sc_npins; n++)
82 1.1 jmcneill fdtbus_gpio_write(sc->sc_pins[n], 0);
83 1.1 jmcneill
84 1.1 jmcneill if (sc->sc_post_power_on_delay_ms > 0)
85 1.1 jmcneill kpause("mmcpwrseq", false,
86 1.1 jmcneill mstohz(sc->sc_post_power_on_delay_ms), NULL);
87 1.1 jmcneill }
88 1.1 jmcneill
89 1.1 jmcneill static void
90 1.1 jmcneill mmcpwrseq_power_off(device_t dev)
91 1.1 jmcneill {
92 1.1 jmcneill struct mmcpwrseq_softc * const sc = device_private(dev);
93 1.1 jmcneill
94 1.1 jmcneill for (u_int n = 0; n < sc->sc_npins; n++)
95 1.1 jmcneill fdtbus_gpio_write(sc->sc_pins[n], 1);
96 1.1 jmcneill
97 1.1 jmcneill if (sc->sc_power_off_delay_us > 0)
98 1.1 jmcneill delay(sc->sc_power_off_delay_us);
99 1.1 jmcneill }
100 1.1 jmcneill
101 1.1 jmcneill static const struct fdtbus_mmc_pwrseq_func mmcpwrseq_funcs = {
102 1.1 jmcneill .pre_power_on = mmcpwrseq_pre_power_on,
103 1.1 jmcneill .post_power_on = mmcpwrseq_post_power_on,
104 1.1 jmcneill .power_off = mmcpwrseq_power_off,
105 1.1 jmcneill };
106 1.1 jmcneill
107 1.1 jmcneill static int
108 1.1 jmcneill mmcpwrseq_match(device_t parent, cfdata_t cf, void *aux)
109 1.1 jmcneill {
110 1.1 jmcneill struct fdt_attach_args * const faa = aux;
111 1.1 jmcneill
112 1.1 jmcneill return of_match_compatible(faa->faa_phandle, compatible);
113 1.1 jmcneill }
114 1.1 jmcneill
115 1.1 jmcneill static void
116 1.1 jmcneill mmcpwrseq_attach(device_t parent, device_t self, void *aux)
117 1.1 jmcneill {
118 1.1 jmcneill struct mmcpwrseq_softc * const sc = device_private(self);
119 1.1 jmcneill struct fdt_attach_args * const faa = aux;
120 1.1 jmcneill const int phandle = faa->faa_phandle;
121 1.1 jmcneill
122 1.1 jmcneill sc->sc_dev = self;
123 1.1 jmcneill sc->sc_phandle = phandle;
124 1.1 jmcneill
125 1.1 jmcneill /* Optional external clock provider */
126 1.1 jmcneill if (of_hasprop(phandle, "clocks")) {
127 1.1 jmcneill sc->sc_clk = fdtbus_clock_get(phandle, "ext_clock");
128 1.1 jmcneill if (sc->sc_clk == NULL) {
129 1.1 jmcneill aprint_error(": couldn't acquire ext_clock\n");
130 1.1 jmcneill return;
131 1.1 jmcneill }
132 1.1 jmcneill }
133 1.1 jmcneill
134 1.1 jmcneill /* Optional reset GPIOs */
135 1.1 jmcneill if (of_hasprop(phandle, "reset-gpios")) {
136 1.1 jmcneill for (sc->sc_npins = 0;
137 1.1 jmcneill sc->sc_npins < MMCPWRSEQ_MAX_PINS;
138 1.1 jmcneill sc->sc_npins++) {
139 1.1 jmcneill sc->sc_pins[sc->sc_npins] =
140 1.1 jmcneill fdtbus_gpio_acquire_index(phandle,
141 1.1 jmcneill "reset-gpios", sc->sc_npins, GPIO_PIN_OUTPUT);
142 1.1 jmcneill if (sc->sc_pins[sc->sc_npins] == NULL)
143 1.1 jmcneill break;
144 1.1 jmcneill }
145 1.1 jmcneill if (sc->sc_npins == 0) {
146 1.1 jmcneill aprint_error(": couldn't get reset GPIOs\n");
147 1.1 jmcneill return;
148 1.1 jmcneill }
149 1.1 jmcneill }
150 1.1 jmcneill
151 1.1 jmcneill /* Delay in ms after powering the card and de-asserting reset GPIOs */
152 1.1 jmcneill of_getprop_uint32(phandle, "post-power-on-delay-ms",
153 1.1 jmcneill &sc->sc_post_power_on_delay_ms);
154 1.1 jmcneill /* Delay in us after asserting the reset GPIOs during power off */
155 1.1 jmcneill of_getprop_uint32(phandle, "power-off-delay-us",
156 1.1 jmcneill &sc->sc_power_off_delay_us);
157 1.1 jmcneill
158 1.1 jmcneill aprint_naive("\n");
159 1.1 jmcneill aprint_normal("\n");
160 1.1 jmcneill
161 1.1 jmcneill fdtbus_register_mmc_pwrseq(self, phandle, &mmcpwrseq_funcs);
162 1.1 jmcneill }
163 1.1 jmcneill
164 1.1 jmcneill CFATTACH_DECL_NEW(mmcpwrseq, sizeof(struct mmcpwrseq_softc),
165 1.1 jmcneill mmcpwrseq_match, mmcpwrseq_attach, NULL, NULL);
166