tegra_sdhc.c revision 1.6 1 /* $NetBSD: tegra_sdhc.c,v 1.6 2015/05/30 18:53:39 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.6 2015/05/30 18:53:39 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_SINGLE_POWER_WRITE |
97 SDHC_FLAG_USE_DMA;
98 if (SDMMC_8BIT_P(loc->loc_port)) {
99 sc->sc.sc_flags |= SDHC_FLAG_8BIT_MODE;
100 }
101 sc->sc.sc_host = &sc->sc_host;
102
103 sc->sc_bst = tio->tio_bst;
104 bus_space_subregion(tio->tio_bst, tio->tio_bsh,
105 loc->loc_offset, loc->loc_size, &sc->sc_bsh);
106 sc->sc_bsz = loc->loc_size;
107 sc->sc_port = loc->loc_port;
108
109 if (prop_dictionary_get_cstring_nocopy(prop, "power-gpio", &pin)) {
110 sc->sc_pin_power = tegra_gpio_acquire(pin, GPIO_PIN_OUTPUT);
111 if (sc->sc_pin_power)
112 tegra_gpio_write(sc->sc_pin_power, 1);
113 }
114
115 if (prop_dictionary_get_cstring_nocopy(prop, "cd-gpio", &pin))
116 sc->sc_pin_cd = tegra_gpio_acquire(pin, GPIO_PIN_INPUT);
117 if (prop_dictionary_get_cstring_nocopy(prop, "wp-gpio", &pin))
118 sc->sc_pin_wp = tegra_gpio_acquire(pin, GPIO_PIN_INPUT);
119
120 if (sc->sc_pin_cd)
121 sc->sc.sc_vendor_card_detect = tegra_sdhc_card_detect;
122 if (sc->sc_pin_wp)
123 sc->sc.sc_vendor_write_protect = tegra_sdhc_write_protect;
124
125 #if notyet
126 tegra_car_periph_sdmmc_set_div(sc->sc_port, 1);
127 #else
128 const u_int div = howmany(tegra_car_pllp0_rate() / 1000, 50000);
129 tegra_car_periph_sdmmc_set_div(sc->sc_port, div);
130 #endif
131 sc->sc.sc_clkbase = tegra_car_periph_sdmmc_rate(sc->sc_port) / 1000;
132
133 aprint_naive("\n");
134 aprint_normal(": SDMMC%d\n", loc->loc_port + 1);
135
136 if (sc->sc.sc_clkbase == 0) {
137 aprint_error_dev(self, "couldn't determine frequency\n");
138 return;
139 }
140
141 sc->sc_ih = intr_establish(loc->loc_intr, IPL_SDMMC, IST_LEVEL,
142 sdhc_intr, &sc->sc);
143 if (sc->sc_ih == NULL) {
144 aprint_error_dev(self, "couldn't establish interrupt %d\n",
145 loc->loc_intr);
146 return;
147 }
148 aprint_normal_dev(self, "interrupting on irq %d\n", loc->loc_intr);
149
150 error = sdhc_host_found(&sc->sc, sc->sc_bst, sc->sc_bsh, sc->sc_bsz);
151 if (error) {
152 aprint_error_dev(self, "couldn't initialize host, error = %d\n",
153 error);
154 intr_disestablish(sc->sc_ih);
155 sc->sc_ih = NULL;
156 return;
157 }
158 }
159
160 static int
161 tegra_sdhc_card_detect(struct sdhc_softc *ssc)
162 {
163 struct tegra_sdhc_softc *sc = device_private(ssc->sc_dev);
164
165 KASSERT(sc->sc_pin_cd != NULL);
166
167 return !tegra_gpio_read(sc->sc_pin_cd);
168 }
169
170 static int
171 tegra_sdhc_write_protect(struct sdhc_softc *ssc)
172 {
173 struct tegra_sdhc_softc *sc = device_private(ssc->sc_dev);
174
175 KASSERT(sc->sc_pin_wp != NULL);
176
177 return tegra_gpio_read(sc->sc_pin_wp);
178 }
179