tegra_sdhc.c revision 1.10 1 /* $NetBSD: tegra_sdhc.c,v 1.10 2015/08/02 21:45:52 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include "locators.h"
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: tegra_sdhc.c,v 1.10 2015/08/02 21:45:52 jmcneill Exp $");
33
34 #include <sys/param.h>
35 #include <sys/bus.h>
36 #include <sys/device.h>
37 #include <sys/intr.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40
41 #include <dev/sdmmc/sdhcreg.h>
42 #include <dev/sdmmc/sdhcvar.h>
43 #include <dev/sdmmc/sdmmcvar.h>
44
45 #include <arm/nvidia/tegra_var.h>
46
47 /* 8-bit eMMC is supported on SDMMC2 and SDMMC4 */
48 #define SDMMC_8BIT_P(port) ((port) == 1 || (port) == 3)
49
50 static int tegra_sdhc_match(device_t, cfdata_t, void *);
51 static void tegra_sdhc_attach(device_t, device_t, void *);
52
53 static int tegra_sdhc_card_detect(struct sdhc_softc *);
54 static int tegra_sdhc_write_protect(struct sdhc_softc *);
55
56 struct tegra_sdhc_softc {
57 struct sdhc_softc sc;
58
59 u_int sc_port;
60
61 bus_space_tag_t sc_bst;
62 bus_space_handle_t sc_bsh;
63 bus_size_t sc_bsz;
64 struct sdhc_host *sc_host;
65 void *sc_ih;
66
67 struct tegra_gpio_pin *sc_pin_cd;
68 struct tegra_gpio_pin *sc_pin_power;
69 struct tegra_gpio_pin *sc_pin_wp;
70 };
71
72 CFATTACH_DECL_NEW(tegra_sdhc, sizeof(struct tegra_sdhc_softc),
73 tegra_sdhc_match, tegra_sdhc_attach, NULL, NULL);
74
75 static int
76 tegra_sdhc_match(device_t parent, cfdata_t cf, void *aux)
77 {
78 return 1;
79 }
80
81 static void
82 tegra_sdhc_attach(device_t parent, device_t self, void *aux)
83 {
84 struct tegra_sdhc_softc * const sc = device_private(self);
85 struct tegraio_attach_args * const tio = aux;
86 const struct tegra_locators * const loc = &tio->tio_loc;
87 prop_dictionary_t prop = device_properties(self);
88 const char *pin;
89 int error;
90
91 sc->sc.sc_dev = self;
92 sc->sc.sc_dmat = tio->tio_dmat;
93 sc->sc.sc_flags = SDHC_FLAG_32BIT_ACCESS |
94 SDHC_FLAG_NO_PWR0 |
95 SDHC_FLAG_NO_CLKBASE |
96 SDHC_FLAG_NO_TIMEOUT |
97 SDHC_FLAG_SINGLE_POWER_WRITE |
98 SDHC_FLAG_USE_DMA |
99 SDHC_FLAG_USE_ADMA2;
100 if (SDMMC_8BIT_P(loc->loc_port)) {
101 sc->sc.sc_flags |= SDHC_FLAG_8BIT_MODE;
102 }
103 sc->sc.sc_host = &sc->sc_host;
104
105 sc->sc_bst = tio->tio_bst;
106 bus_space_subregion(tio->tio_bst, tio->tio_bsh,
107 loc->loc_offset, loc->loc_size, &sc->sc_bsh);
108 sc->sc_bsz = loc->loc_size;
109 sc->sc_port = loc->loc_port;
110
111 if (prop_dictionary_get_cstring_nocopy(prop, "power-gpio", &pin)) {
112 sc->sc_pin_power = tegra_gpio_acquire(pin, GPIO_PIN_OUTPUT);
113 if (sc->sc_pin_power)
114 tegra_gpio_write(sc->sc_pin_power, 1);
115 }
116
117 if (prop_dictionary_get_cstring_nocopy(prop, "cd-gpio", &pin))
118 sc->sc_pin_cd = tegra_gpio_acquire(pin, GPIO_PIN_INPUT);
119 if (prop_dictionary_get_cstring_nocopy(prop, "wp-gpio", &pin))
120 sc->sc_pin_wp = tegra_gpio_acquire(pin, GPIO_PIN_INPUT);
121
122 if (sc->sc_pin_cd)
123 sc->sc.sc_vendor_card_detect = tegra_sdhc_card_detect;
124 if (sc->sc_pin_wp)
125 sc->sc.sc_vendor_write_protect = tegra_sdhc_write_protect;
126
127 tegra_car_periph_sdmmc_set_rate(sc->sc_port, 204000000);
128 sc->sc.sc_clkbase = tegra_car_periph_sdmmc_rate(sc->sc_port) / 1000;
129
130 aprint_naive("\n");
131 aprint_normal(": SDMMC%d\n", loc->loc_port + 1);
132
133 if (sc->sc.sc_clkbase == 0) {
134 aprint_error_dev(self, "couldn't determine frequency\n");
135 return;
136 }
137
138 sc->sc_ih = intr_establish(loc->loc_intr, IPL_SDMMC, IST_LEVEL,
139 sdhc_intr, &sc->sc);
140 if (sc->sc_ih == NULL) {
141 aprint_error_dev(self, "couldn't establish interrupt %d\n",
142 loc->loc_intr);
143 return;
144 }
145 aprint_normal_dev(self, "interrupting on irq %d\n", loc->loc_intr);
146
147 error = sdhc_host_found(&sc->sc, sc->sc_bst, sc->sc_bsh, sc->sc_bsz);
148 if (error) {
149 aprint_error_dev(self, "couldn't initialize host, error = %d\n",
150 error);
151 intr_disestablish(sc->sc_ih);
152 sc->sc_ih = NULL;
153 return;
154 }
155 }
156
157 static int
158 tegra_sdhc_card_detect(struct sdhc_softc *ssc)
159 {
160 struct tegra_sdhc_softc *sc = device_private(ssc->sc_dev);
161
162 KASSERT(sc->sc_pin_cd != NULL);
163
164 return !tegra_gpio_read(sc->sc_pin_cd);
165 }
166
167 static int
168 tegra_sdhc_write_protect(struct sdhc_softc *ssc)
169 {
170 struct tegra_sdhc_softc *sc = device_private(ssc->sc_dev);
171
172 KASSERT(sc->sc_pin_wp != NULL);
173
174 return tegra_gpio_read(sc->sc_pin_wp);
175 }
176