ti_sdhc.c revision 1.7 1 1.7 thorpej /* $NetBSD: ti_sdhc.c,v 1.7 2021/01/18 02:35:49 thorpej Exp $ */
2 1.1 jmcneill /*-
3 1.1 jmcneill * Copyright (c) 2011 The NetBSD Foundation, Inc.
4 1.1 jmcneill * All rights reserved.
5 1.1 jmcneill *
6 1.1 jmcneill * This code is derived from software contributed to The NetBSD Foundation
7 1.1 jmcneill * by Matt Thomas of 3am Software Foundry.
8 1.1 jmcneill *
9 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
10 1.1 jmcneill * modification, are permitted provided that the following conditions
11 1.1 jmcneill * are met:
12 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
14 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
16 1.1 jmcneill * documentation and/or other materials provided with the distribution.
17 1.1 jmcneill *
18 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 1.1 jmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 1.1 jmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 1.1 jmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 1.1 jmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 1.1 jmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 1.1 jmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 1.1 jmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 1.1 jmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 1.1 jmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 1.1 jmcneill * POSSIBILITY OF SUCH DAMAGE.
29 1.1 jmcneill */
30 1.1 jmcneill
31 1.1 jmcneill #include <sys/cdefs.h>
32 1.7 thorpej __KERNEL_RCSID(0, "$NetBSD: ti_sdhc.c,v 1.7 2021/01/18 02:35:49 thorpej Exp $");
33 1.1 jmcneill
34 1.1 jmcneill #include <sys/param.h>
35 1.1 jmcneill #include <sys/systm.h>
36 1.1 jmcneill #include <sys/device.h>
37 1.1 jmcneill #include <sys/errno.h>
38 1.1 jmcneill #include <sys/kernel.h>
39 1.1 jmcneill #include <sys/proc.h>
40 1.1 jmcneill #include <sys/queue.h>
41 1.1 jmcneill #include <sys/mutex.h>
42 1.1 jmcneill #include <sys/condvar.h>
43 1.1 jmcneill #include <sys/bus.h>
44 1.1 jmcneill
45 1.1 jmcneill #include <arm/ti/ti_prcm.h>
46 1.1 jmcneill #include <arm/ti/ti_edma.h>
47 1.1 jmcneill #include <arm/ti/ti_sdhcreg.h>
48 1.1 jmcneill
49 1.1 jmcneill #include <dev/sdmmc/sdhcreg.h>
50 1.1 jmcneill #include <dev/sdmmc/sdhcvar.h>
51 1.1 jmcneill #include <dev/sdmmc/sdmmcvar.h>
52 1.1 jmcneill
53 1.1 jmcneill #include <dev/fdt/fdtvar.h>
54 1.1 jmcneill
55 1.1 jmcneill #define EDMA_MAX_PARAMS 32
56 1.1 jmcneill
57 1.1 jmcneill #ifdef TISDHC_DEBUG
58 1.1 jmcneill int tisdhcdebug = 1;
59 1.1 jmcneill #define DPRINTF(n,s) do { if ((n) <= tisdhcdebug) device_printf s; } while (0)
60 1.1 jmcneill #else
61 1.1 jmcneill #define DPRINTF(n,s) do {} while (0)
62 1.1 jmcneill #endif
63 1.1 jmcneill
64 1.1 jmcneill
65 1.1 jmcneill #define CLKD(kz) (sc->sc.sc_clkbase / (kz))
66 1.1 jmcneill
67 1.1 jmcneill #define SDHC_READ(sc, reg) \
68 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_sdhc_bsh, (reg))
69 1.1 jmcneill #define SDHC_WRITE(sc, reg, val) \
70 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_sdhc_bsh, (reg), (val))
71 1.1 jmcneill
72 1.1 jmcneill struct ti_sdhc_config {
73 1.1 jmcneill bus_size_t regoff;
74 1.1 jmcneill uint32_t flags;
75 1.1 jmcneill };
76 1.1 jmcneill
77 1.1 jmcneill static const struct ti_sdhc_config omap2_hsmmc_config = {
78 1.1 jmcneill };
79 1.1 jmcneill
80 1.1 jmcneill static const struct ti_sdhc_config omap3_pre_es3_hsmmc_config = {
81 1.1 jmcneill .flags = SDHC_FLAG_SINGLE_ONLY
82 1.1 jmcneill };
83 1.1 jmcneill
84 1.1 jmcneill static const struct ti_sdhc_config omap4_hsmmc_config = {
85 1.1 jmcneill .regoff = 0x100
86 1.1 jmcneill };
87 1.1 jmcneill
88 1.7 thorpej static const struct device_compatible_entry compat_data[] = {
89 1.7 thorpej { .compat = "ti,omap2-hsmmc",
90 1.7 thorpej .data = &omap2_hsmmc_config },
91 1.7 thorpej { .compat = "ti,omap3-hsmmc",
92 1.7 thorpej .data = &omap2_hsmmc_config },
93 1.7 thorpej { .compat = "ti,omap3-pre-es3-hsmmc",
94 1.7 thorpej .data = &omap3_pre_es3_hsmmc_config },
95 1.7 thorpej { .compat = "ti,omap4-hsmmc",
96 1.7 thorpej .data = &omap4_hsmmc_config },
97 1.7 thorpej
98 1.7 thorpej { 0 }
99 1.1 jmcneill };
100 1.1 jmcneill
101 1.1 jmcneill enum {
102 1.1 jmcneill EDMA_CHAN_TX,
103 1.1 jmcneill EDMA_CHAN_RX,
104 1.1 jmcneill EDMA_NCHAN
105 1.1 jmcneill };
106 1.1 jmcneill
107 1.1 jmcneill struct ti_sdhc_softc {
108 1.1 jmcneill struct sdhc_softc sc;
109 1.1 jmcneill int sc_phandle;
110 1.1 jmcneill bus_addr_t sc_addr;
111 1.1 jmcneill bus_space_tag_t sc_bst;
112 1.1 jmcneill bus_space_handle_t sc_bsh;
113 1.1 jmcneill bus_space_handle_t sc_hl_bsh;
114 1.1 jmcneill bus_space_handle_t sc_sdhc_bsh;
115 1.1 jmcneill struct sdhc_host *sc_hosts[1];
116 1.1 jmcneill void *sc_ih; /* interrupt vectoring */
117 1.1 jmcneill
118 1.1 jmcneill int sc_edma_chan[EDMA_NCHAN];
119 1.1 jmcneill struct edma_channel *sc_edma_tx;
120 1.1 jmcneill struct edma_channel *sc_edma_rx;
121 1.1 jmcneill uint16_t sc_edma_param_tx[EDMA_MAX_PARAMS];
122 1.1 jmcneill uint16_t sc_edma_param_rx[EDMA_MAX_PARAMS];
123 1.1 jmcneill kcondvar_t sc_edma_cv;
124 1.1 jmcneill bus_addr_t sc_edma_fifo;
125 1.1 jmcneill bool sc_edma_pending;
126 1.1 jmcneill bus_dmamap_t sc_edma_dmamap;
127 1.1 jmcneill bus_dma_segment_t sc_edma_segs[1];
128 1.1 jmcneill void *sc_edma_bbuf;
129 1.1 jmcneill };
130 1.1 jmcneill
131 1.1 jmcneill static int ti_sdhc_match(device_t, cfdata_t, void *);
132 1.1 jmcneill static void ti_sdhc_attach(device_t, device_t, void *);
133 1.1 jmcneill
134 1.1 jmcneill static void ti_sdhc_init(struct ti_sdhc_softc *, const struct ti_sdhc_config *);
135 1.1 jmcneill
136 1.1 jmcneill static int ti_sdhc_bus_width(struct sdhc_softc *, int);
137 1.1 jmcneill static int ti_sdhc_rod(struct sdhc_softc *, int);
138 1.1 jmcneill static int ti_sdhc_write_protect(struct sdhc_softc *);
139 1.1 jmcneill static int ti_sdhc_card_detect(struct sdhc_softc *);
140 1.1 jmcneill
141 1.1 jmcneill static int ti_sdhc_edma_init(struct ti_sdhc_softc *, u_int, u_int);
142 1.1 jmcneill static int ti_sdhc_edma_xfer_data(struct sdhc_softc *, struct sdmmc_command *);
143 1.1 jmcneill static void ti_sdhc_edma_done(void *);
144 1.1 jmcneill static int ti_sdhc_edma_transfer(struct sdhc_softc *, struct sdmmc_command *);
145 1.1 jmcneill
146 1.1 jmcneill CFATTACH_DECL_NEW(ti_sdhc, sizeof(struct ti_sdhc_softc),
147 1.1 jmcneill ti_sdhc_match, ti_sdhc_attach, NULL, NULL);
148 1.1 jmcneill
149 1.1 jmcneill static int
150 1.1 jmcneill ti_sdhc_match(device_t parent, cfdata_t cf, void *aux)
151 1.1 jmcneill {
152 1.1 jmcneill struct fdt_attach_args * const faa = aux;
153 1.1 jmcneill
154 1.1 jmcneill return of_match_compat_data(faa->faa_phandle, compat_data);
155 1.1 jmcneill }
156 1.1 jmcneill
157 1.1 jmcneill static void
158 1.1 jmcneill ti_sdhc_attach(device_t parent, device_t self, void *aux)
159 1.1 jmcneill {
160 1.1 jmcneill struct ti_sdhc_softc * const sc = device_private(self);
161 1.1 jmcneill struct fdt_attach_args * const faa = aux;
162 1.1 jmcneill const int phandle = faa->faa_phandle;
163 1.1 jmcneill const struct ti_sdhc_config *conf;
164 1.1 jmcneill bus_addr_t addr;
165 1.1 jmcneill bus_size_t size;
166 1.1 jmcneill u_int bus_width;
167 1.1 jmcneill
168 1.7 thorpej conf = of_search_compatible(phandle, compat_data)->data;
169 1.1 jmcneill
170 1.3 jmcneill if (ti_prcm_enable_hwmod(phandle, 0) != 0) {
171 1.1 jmcneill aprint_error(": couldn't enable module\n");
172 1.1 jmcneill return;
173 1.1 jmcneill }
174 1.1 jmcneill
175 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0 || size <= conf->regoff) {
176 1.1 jmcneill aprint_error(": couldn't get registers\n");
177 1.1 jmcneill return;
178 1.1 jmcneill }
179 1.1 jmcneill addr += conf->regoff;
180 1.1 jmcneill size -= conf->regoff;
181 1.1 jmcneill
182 1.1 jmcneill sc->sc.sc_dmat = faa->faa_dmat;
183 1.1 jmcneill sc->sc.sc_dev = self;
184 1.1 jmcneill sc->sc_phandle = phandle;
185 1.1 jmcneill sc->sc_addr = addr;
186 1.1 jmcneill sc->sc_bst = faa->faa_bst;
187 1.1 jmcneill
188 1.1 jmcneill /* XXX use fdtbus_dma API */
189 1.1 jmcneill int len;
190 1.1 jmcneill const u_int *dmas = fdtbus_get_prop(phandle, "dmas", &len);
191 1.1 jmcneill switch (len) {
192 1.1 jmcneill case 24:
193 1.1 jmcneill sc->sc_edma_chan[EDMA_CHAN_TX] = be32toh(dmas[1]);
194 1.1 jmcneill sc->sc_edma_chan[EDMA_CHAN_RX] = be32toh(dmas[4]);
195 1.1 jmcneill break;
196 1.1 jmcneill case 32:
197 1.1 jmcneill sc->sc_edma_chan[EDMA_CHAN_TX] = be32toh(dmas[1]);
198 1.1 jmcneill sc->sc_edma_chan[EDMA_CHAN_RX] = be32toh(dmas[5]);
199 1.1 jmcneill break;
200 1.1 jmcneill default:
201 1.1 jmcneill sc->sc_edma_chan[EDMA_CHAN_TX] = -1;
202 1.1 jmcneill sc->sc_edma_chan[EDMA_CHAN_RX] = -1;
203 1.1 jmcneill break;
204 1.1 jmcneill }
205 1.1 jmcneill
206 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
207 1.1 jmcneill aprint_error(": couldn't map registers\n");
208 1.1 jmcneill return;
209 1.1 jmcneill }
210 1.1 jmcneill
211 1.1 jmcneill if (of_getprop_uint32(phandle, "bus-width", &bus_width) != 0)
212 1.1 jmcneill bus_width = 4;
213 1.1 jmcneill
214 1.1 jmcneill sc->sc.sc_flags |= conf->flags;
215 1.1 jmcneill sc->sc.sc_flags |= SDHC_FLAG_32BIT_ACCESS;
216 1.1 jmcneill sc->sc.sc_flags |= SDHC_FLAG_NO_LED_ON;
217 1.1 jmcneill sc->sc.sc_flags |= SDHC_FLAG_RSP136_CRC;
218 1.1 jmcneill if (bus_width == 8)
219 1.1 jmcneill sc->sc.sc_flags |= SDHC_FLAG_8BIT_MODE;
220 1.1 jmcneill if (of_hasprop(phandle, "ti,needs-special-reset"))
221 1.1 jmcneill sc->sc.sc_flags |= SDHC_FLAG_WAIT_RESET;
222 1.4 jmcneill if (!of_hasprop(phandle, "ti,needs-special-hs-handling"))
223 1.1 jmcneill sc->sc.sc_flags |= SDHC_FLAG_NO_HS_BIT;
224 1.1 jmcneill if (of_hasprop(phandle, "ti,dual-volt"))
225 1.1 jmcneill sc->sc.sc_caps = SDHC_VOLTAGE_SUPP_3_0V;
226 1.1 jmcneill
227 1.1 jmcneill sc->sc.sc_host = sc->sc_hosts;
228 1.1 jmcneill sc->sc.sc_clkbase = 96000; /* 96MHZ */
229 1.1 jmcneill sc->sc.sc_clkmsk = 0x0000ffc0;
230 1.1 jmcneill sc->sc.sc_vendor_rod = ti_sdhc_rod;
231 1.1 jmcneill sc->sc.sc_vendor_write_protect = ti_sdhc_write_protect;
232 1.1 jmcneill sc->sc.sc_vendor_card_detect = ti_sdhc_card_detect;
233 1.1 jmcneill sc->sc.sc_vendor_bus_width = ti_sdhc_bus_width;
234 1.1 jmcneill
235 1.1 jmcneill if (bus_space_subregion(sc->sc_bst, sc->sc_bsh, 0x100, 0x100,
236 1.1 jmcneill &sc->sc_sdhc_bsh) != 0) {
237 1.1 jmcneill aprint_error(": couldn't map subregion\n");
238 1.1 jmcneill return;
239 1.1 jmcneill }
240 1.1 jmcneill
241 1.1 jmcneill aprint_naive("\n");
242 1.1 jmcneill aprint_normal(": MMCHS\n");
243 1.1 jmcneill
244 1.1 jmcneill ti_sdhc_init(sc, conf);
245 1.1 jmcneill }
246 1.1 jmcneill
247 1.1 jmcneill static void
248 1.1 jmcneill ti_sdhc_init(struct ti_sdhc_softc *sc, const struct ti_sdhc_config *conf)
249 1.1 jmcneill {
250 1.1 jmcneill device_t dev = sc->sc.sc_dev;
251 1.1 jmcneill uint32_t clkd, stat;
252 1.1 jmcneill int error, timo, clksft, n;
253 1.1 jmcneill char intrstr[128];
254 1.1 jmcneill
255 1.1 jmcneill const int tx_chan = sc->sc_edma_chan[EDMA_CHAN_TX];
256 1.1 jmcneill const int rx_chan = sc->sc_edma_chan[EDMA_CHAN_RX];
257 1.1 jmcneill
258 1.1 jmcneill if (tx_chan != -1 && rx_chan != -1) {
259 1.1 jmcneill aprint_normal_dev(dev,
260 1.1 jmcneill "EDMA tx channel %d, rx channel %d\n",
261 1.1 jmcneill tx_chan, rx_chan);
262 1.1 jmcneill
263 1.1 jmcneill if (ti_sdhc_edma_init(sc, tx_chan, rx_chan) != 0) {
264 1.1 jmcneill aprint_error_dev(dev, "EDMA disabled\n");
265 1.1 jmcneill goto no_dma;
266 1.1 jmcneill }
267 1.1 jmcneill
268 1.1 jmcneill cv_init(&sc->sc_edma_cv, "sdhcedma");
269 1.1 jmcneill sc->sc_edma_fifo = sc->sc_addr + 0x100 + SDHC_DATA;
270 1.1 jmcneill sc->sc.sc_flags |= SDHC_FLAG_USE_DMA;
271 1.1 jmcneill sc->sc.sc_flags |= SDHC_FLAG_EXTERNAL_DMA;
272 1.1 jmcneill sc->sc.sc_flags |= SDHC_FLAG_EXTDMA_DMAEN;
273 1.1 jmcneill sc->sc.sc_vendor_transfer_data_dma = ti_sdhc_edma_xfer_data;
274 1.1 jmcneill }
275 1.1 jmcneill no_dma:
276 1.1 jmcneill
277 1.1 jmcneill /* XXXXXX: Turn-on regulator via I2C. */
278 1.1 jmcneill /* XXXXXX: And enable ICLOCK/FCLOCK. */
279 1.1 jmcneill
280 1.1 jmcneill SDHC_WRITE(sc, SDHC_CAPABILITIES,
281 1.1 jmcneill SDHC_READ(sc, SDHC_CAPABILITIES) | SDHC_VOLTAGE_SUPP_1_8V);
282 1.1 jmcneill if (sc->sc.sc_caps & SDHC_VOLTAGE_SUPP_3_0V)
283 1.1 jmcneill SDHC_WRITE(sc, SDHC_CAPABILITIES,
284 1.1 jmcneill SDHC_READ(sc, SDHC_CAPABILITIES) | SDHC_VOLTAGE_SUPP_3_0V);
285 1.1 jmcneill
286 1.1 jmcneill /* MMCHS Soft reset */
287 1.1 jmcneill bus_space_write_4(sc->sc_bst, sc->sc_bsh, MMCHS_SYSCONFIG,
288 1.1 jmcneill SYSCONFIG_SOFTRESET);
289 1.1 jmcneill timo = 3000000; /* XXXX 3 sec. */
290 1.1 jmcneill while (timo--) {
291 1.1 jmcneill if (bus_space_read_4(sc->sc_bst, sc->sc_bsh, MMCHS_SYSSTATUS) &
292 1.1 jmcneill SYSSTATUS_RESETDONE)
293 1.1 jmcneill break;
294 1.1 jmcneill delay(1);
295 1.1 jmcneill }
296 1.1 jmcneill if (timo == 0)
297 1.1 jmcneill aprint_error_dev(dev, "Soft reset timeout\n");
298 1.1 jmcneill bus_space_write_4(sc->sc_bst, sc->sc_bsh, MMCHS_SYSCONFIG,
299 1.1 jmcneill SYSCONFIG_ENAWAKEUP |
300 1.2 jmcneill #if notyet
301 1.1 jmcneill SYSCONFIG_AUTOIDLE |
302 1.1 jmcneill SYSCONFIG_SIDLEMODE_AUTO |
303 1.2 jmcneill #else
304 1.2 jmcneill SYSCONFIG_SIDLEMODE_IGNORE |
305 1.2 jmcneill #endif
306 1.1 jmcneill SYSCONFIG_CLOCKACTIVITY_FCLK |
307 1.1 jmcneill SYSCONFIG_CLOCKACTIVITY_ICLK);
308 1.1 jmcneill
309 1.1 jmcneill if (!fdtbus_intr_str(sc->sc_phandle, 0, intrstr, sizeof(intrstr))) {
310 1.1 jmcneill aprint_error_dev(dev, "couldn't decode interrupt\n");
311 1.1 jmcneill return;
312 1.1 jmcneill }
313 1.6 jmcneill sc->sc_ih = fdtbus_intr_establish_xname(sc->sc_phandle, 0, IPL_VM,
314 1.6 jmcneill 0, sdhc_intr, &sc->sc, device_xname(dev));
315 1.1 jmcneill if (sc->sc_ih == NULL) {
316 1.1 jmcneill aprint_error_dev(dev, "couldn't establish interrupt\n");
317 1.1 jmcneill return;
318 1.1 jmcneill }
319 1.1 jmcneill aprint_normal_dev(dev, "interrupting on %s\n", intrstr);
320 1.1 jmcneill
321 1.1 jmcneill error = sdhc_host_found(&sc->sc, sc->sc_bst, sc->sc_sdhc_bsh, 0x100);
322 1.1 jmcneill if (error != 0) {
323 1.1 jmcneill aprint_error_dev(dev, "couldn't initialize host, error=%d\n",
324 1.1 jmcneill error);
325 1.1 jmcneill fdtbus_intr_disestablish(sc->sc_phandle, sc->sc_ih);
326 1.1 jmcneill return;
327 1.1 jmcneill }
328 1.1 jmcneill
329 1.1 jmcneill clksft = ffs(sc->sc.sc_clkmsk) - 1;
330 1.1 jmcneill
331 1.1 jmcneill /* Set SDVS 1.8v and DTW 1bit mode */
332 1.1 jmcneill SDHC_WRITE(sc, SDHC_HOST_CTL,
333 1.1 jmcneill SDHC_VOLTAGE_1_8V << (SDHC_VOLTAGE_SHIFT + 8));
334 1.1 jmcneill SDHC_WRITE(sc, SDHC_CLOCK_CTL,
335 1.1 jmcneill SDHC_READ(sc, SDHC_CLOCK_CTL) | SDHC_INTCLK_ENABLE |
336 1.1 jmcneill SDHC_SDCLK_ENABLE);
337 1.1 jmcneill SDHC_WRITE(sc, SDHC_HOST_CTL,
338 1.1 jmcneill SDHC_READ(sc, SDHC_HOST_CTL) | SDHC_BUS_POWER << 8);
339 1.1 jmcneill SDHC_WRITE(sc, SDHC_CLOCK_CTL,
340 1.1 jmcneill SDHC_READ(sc, SDHC_CLOCK_CTL) | CLKD(150) << clksft);
341 1.1 jmcneill
342 1.1 jmcneill /*
343 1.1 jmcneill * 22.6.1.3.1.5 MMCHS Controller INIT Procedure Start
344 1.1 jmcneill * from 'OMAP35x Applications Processor Technical Reference Manual'.
345 1.1 jmcneill *
346 1.1 jmcneill * During the INIT procedure, the MMCHS controller generates 80 clock
347 1.1 jmcneill * periods. In order to keep the 1ms gap, the MMCHS controller should
348 1.1 jmcneill * be configured to generate a clock whose frequency is smaller or
349 1.1 jmcneill * equal to 80 KHz.
350 1.1 jmcneill */
351 1.1 jmcneill
352 1.1 jmcneill SDHC_WRITE(sc, SDHC_CLOCK_CTL,
353 1.1 jmcneill SDHC_READ(sc, SDHC_CLOCK_CTL) & ~SDHC_SDCLK_ENABLE);
354 1.1 jmcneill SDHC_WRITE(sc, SDHC_CLOCK_CTL,
355 1.1 jmcneill SDHC_READ(sc, SDHC_CLOCK_CTL) & ~sc->sc.sc_clkmsk);
356 1.1 jmcneill clkd = CLKD(80);
357 1.1 jmcneill n = 1;
358 1.1 jmcneill while (clkd & ~(sc->sc.sc_clkmsk >> clksft)) {
359 1.1 jmcneill clkd >>= 1;
360 1.1 jmcneill n <<= 1;
361 1.1 jmcneill }
362 1.1 jmcneill SDHC_WRITE(sc, SDHC_CLOCK_CTL,
363 1.1 jmcneill SDHC_READ(sc, SDHC_CLOCK_CTL) | (clkd << clksft));
364 1.1 jmcneill SDHC_WRITE(sc, SDHC_CLOCK_CTL,
365 1.1 jmcneill SDHC_READ(sc, SDHC_CLOCK_CTL) | SDHC_SDCLK_ENABLE);
366 1.1 jmcneill
367 1.1 jmcneill bus_space_write_4(sc->sc_bst, sc->sc_bsh, MMCHS_CON,
368 1.1 jmcneill bus_space_read_4(sc->sc_bst, sc->sc_bsh, MMCHS_CON) | CON_INIT);
369 1.1 jmcneill SDHC_WRITE(sc, SDHC_TRANSFER_MODE, 0x00000000);
370 1.1 jmcneill delay(1000);
371 1.1 jmcneill stat = SDHC_READ(sc, SDHC_NINTR_STATUS);
372 1.1 jmcneill SDHC_WRITE(sc, SDHC_NINTR_STATUS, stat | SDHC_COMMAND_COMPLETE);
373 1.1 jmcneill bus_space_write_4(sc->sc_bst, sc->sc_bsh, MMCHS_CON,
374 1.1 jmcneill bus_space_read_4(sc->sc_bst, sc->sc_bsh, MMCHS_CON) & ~CON_INIT);
375 1.1 jmcneill SDHC_WRITE(sc, SDHC_NINTR_STATUS, 0xffffffff);
376 1.1 jmcneill
377 1.1 jmcneill SDHC_WRITE(sc, SDHC_CLOCK_CTL,
378 1.1 jmcneill SDHC_READ(sc, SDHC_CLOCK_CTL) & ~SDHC_SDCLK_ENABLE);
379 1.1 jmcneill SDHC_WRITE(sc, SDHC_CLOCK_CTL,
380 1.1 jmcneill SDHC_READ(sc, SDHC_CLOCK_CTL) & ~sc->sc.sc_clkmsk);
381 1.1 jmcneill SDHC_WRITE(sc, SDHC_CLOCK_CTL,
382 1.1 jmcneill SDHC_READ(sc, SDHC_CLOCK_CTL) | CLKD(150) << clksft);
383 1.1 jmcneill timo = 3000000; /* XXXX 3 sec. */
384 1.1 jmcneill while (--timo) {
385 1.1 jmcneill if (SDHC_READ(sc, SDHC_CLOCK_CTL) & SDHC_INTCLK_STABLE)
386 1.1 jmcneill break;
387 1.1 jmcneill delay(1);
388 1.1 jmcneill }
389 1.1 jmcneill if (timo == 0)
390 1.1 jmcneill aprint_error_dev(dev, "ICS timeout\n");
391 1.1 jmcneill SDHC_WRITE(sc, SDHC_CLOCK_CTL,
392 1.1 jmcneill SDHC_READ(sc, SDHC_CLOCK_CTL) | SDHC_SDCLK_ENABLE);
393 1.1 jmcneill
394 1.1 jmcneill if (sc->sc.sc_flags & SDHC_FLAG_USE_ADMA2)
395 1.1 jmcneill bus_space_write_4(sc->sc_bst, sc->sc_bsh, MMCHS_CON,
396 1.1 jmcneill bus_space_read_4(sc->sc_bst, sc->sc_bsh, MMCHS_CON) |
397 1.1 jmcneill CON_MNS);
398 1.1 jmcneill }
399 1.1 jmcneill
400 1.1 jmcneill static int
401 1.1 jmcneill ti_sdhc_rod(struct sdhc_softc *sc, int on)
402 1.1 jmcneill {
403 1.1 jmcneill struct ti_sdhc_softc *hmsc = (struct ti_sdhc_softc *)sc;
404 1.1 jmcneill uint32_t con;
405 1.1 jmcneill
406 1.1 jmcneill con = bus_space_read_4(hmsc->sc_bst, hmsc->sc_bsh, MMCHS_CON);
407 1.1 jmcneill if (on)
408 1.1 jmcneill con |= CON_OD;
409 1.1 jmcneill else
410 1.1 jmcneill con &= ~CON_OD;
411 1.1 jmcneill bus_space_write_4(hmsc->sc_bst, hmsc->sc_bsh, MMCHS_CON, con);
412 1.1 jmcneill
413 1.1 jmcneill return 0;
414 1.1 jmcneill }
415 1.1 jmcneill
416 1.1 jmcneill static int
417 1.1 jmcneill ti_sdhc_write_protect(struct sdhc_softc *sc)
418 1.1 jmcneill {
419 1.1 jmcneill
420 1.1 jmcneill /* Maybe board dependent, using GPIO. Get GPIO-pin from prop? */
421 1.1 jmcneill return 0; /* XXXXXXX */
422 1.1 jmcneill }
423 1.1 jmcneill
424 1.1 jmcneill static int
425 1.1 jmcneill ti_sdhc_card_detect(struct sdhc_softc *sc)
426 1.1 jmcneill {
427 1.1 jmcneill
428 1.1 jmcneill /* Maybe board dependent, using GPIO. Get GPIO-pin from prop? */
429 1.1 jmcneill return 1; /* XXXXXXXX */
430 1.1 jmcneill }
431 1.1 jmcneill
432 1.1 jmcneill static int
433 1.1 jmcneill ti_sdhc_bus_width(struct sdhc_softc *sc, int width)
434 1.1 jmcneill {
435 1.1 jmcneill struct ti_sdhc_softc *hmsc = (struct ti_sdhc_softc *)sc;
436 1.5 jmcneill uint32_t con, hctl;
437 1.1 jmcneill
438 1.1 jmcneill con = bus_space_read_4(hmsc->sc_bst, hmsc->sc_bsh, MMCHS_CON);
439 1.5 jmcneill hctl = SDHC_READ(hmsc, SDHC_HOST_CTL);
440 1.1 jmcneill if (width == 8) {
441 1.1 jmcneill con |= CON_DW8;
442 1.5 jmcneill } else if (width == 4) {
443 1.5 jmcneill con &= ~CON_DW8;
444 1.5 jmcneill hctl |= SDHC_4BIT_MODE;
445 1.1 jmcneill } else {
446 1.1 jmcneill con &= ~CON_DW8;
447 1.5 jmcneill hctl &= ~SDHC_4BIT_MODE;
448 1.1 jmcneill }
449 1.1 jmcneill bus_space_write_4(hmsc->sc_bst, hmsc->sc_bsh, MMCHS_CON, con);
450 1.5 jmcneill SDHC_WRITE(hmsc, SDHC_HOST_CTL, hctl);
451 1.1 jmcneill
452 1.1 jmcneill return 0;
453 1.1 jmcneill }
454 1.1 jmcneill
455 1.1 jmcneill static int
456 1.1 jmcneill ti_sdhc_edma_init(struct ti_sdhc_softc *sc, u_int tx_chan, u_int rx_chan)
457 1.1 jmcneill {
458 1.1 jmcneill int i, error, rseg;
459 1.1 jmcneill
460 1.1 jmcneill /* Request tx and rx DMA channels */
461 1.1 jmcneill sc->sc_edma_tx = edma_channel_alloc(EDMA_TYPE_DMA, tx_chan,
462 1.1 jmcneill ti_sdhc_edma_done, sc);
463 1.1 jmcneill KASSERT(sc->sc_edma_tx != NULL);
464 1.1 jmcneill sc->sc_edma_rx = edma_channel_alloc(EDMA_TYPE_DMA, rx_chan,
465 1.1 jmcneill ti_sdhc_edma_done, sc);
466 1.1 jmcneill KASSERT(sc->sc_edma_rx != NULL);
467 1.1 jmcneill
468 1.1 jmcneill /* Allocate some PaRAM pages */
469 1.1 jmcneill for (i = 0; i < __arraycount(sc->sc_edma_param_tx); i++) {
470 1.1 jmcneill sc->sc_edma_param_tx[i] = edma_param_alloc(sc->sc_edma_tx);
471 1.1 jmcneill KASSERT(sc->sc_edma_param_tx[i] != 0xffff);
472 1.1 jmcneill }
473 1.1 jmcneill for (i = 0; i < __arraycount(sc->sc_edma_param_rx); i++) {
474 1.1 jmcneill sc->sc_edma_param_rx[i] = edma_param_alloc(sc->sc_edma_rx);
475 1.1 jmcneill KASSERT(sc->sc_edma_param_rx[i] != 0xffff);
476 1.1 jmcneill }
477 1.1 jmcneill
478 1.1 jmcneill /* Setup bounce buffer */
479 1.1 jmcneill error = bus_dmamem_alloc(sc->sc.sc_dmat, MAXPHYS, 32, MAXPHYS,
480 1.1 jmcneill sc->sc_edma_segs, 1, &rseg, BUS_DMA_WAITOK);
481 1.1 jmcneill if (error) {
482 1.1 jmcneill aprint_error_dev(sc->sc.sc_dev,
483 1.1 jmcneill "couldn't allocate dmamem: %d\n", error);
484 1.1 jmcneill return error;
485 1.1 jmcneill }
486 1.1 jmcneill KASSERT(rseg == 1);
487 1.1 jmcneill error = bus_dmamem_map(sc->sc.sc_dmat, sc->sc_edma_segs, rseg, MAXPHYS,
488 1.1 jmcneill &sc->sc_edma_bbuf, BUS_DMA_WAITOK);
489 1.1 jmcneill if (error) {
490 1.1 jmcneill aprint_error_dev(sc->sc.sc_dev, "couldn't map dmamem: %d\n",
491 1.1 jmcneill error);
492 1.1 jmcneill return error;
493 1.1 jmcneill }
494 1.1 jmcneill error = bus_dmamap_create(sc->sc.sc_dmat, MAXPHYS, 1, MAXPHYS, 0,
495 1.1 jmcneill BUS_DMA_WAITOK, &sc->sc_edma_dmamap);
496 1.1 jmcneill if (error) {
497 1.1 jmcneill aprint_error_dev(sc->sc.sc_dev, "couldn't create dmamap: %d\n",
498 1.1 jmcneill error);
499 1.1 jmcneill return error;
500 1.1 jmcneill }
501 1.5 jmcneill error = bus_dmamap_load(sc->sc.sc_dmat, sc->sc_edma_dmamap,
502 1.5 jmcneill sc->sc_edma_bbuf, MAXPHYS, NULL, BUS_DMA_WAITOK);
503 1.5 jmcneill if (error) {
504 1.5 jmcneill device_printf(sc->sc.sc_dev, "couldn't load dmamap: %d\n",
505 1.5 jmcneill error);
506 1.5 jmcneill return error;
507 1.5 jmcneill }
508 1.1 jmcneill
509 1.1 jmcneill return error;
510 1.1 jmcneill }
511 1.1 jmcneill
512 1.1 jmcneill static int
513 1.1 jmcneill ti_sdhc_edma_xfer_data(struct sdhc_softc *sdhc_sc, struct sdmmc_command *cmd)
514 1.1 jmcneill {
515 1.1 jmcneill struct ti_sdhc_softc *sc = device_private(sdhc_sc->sc_dev);
516 1.1 jmcneill const bus_dmamap_t map = cmd->c_dmamap;
517 1.1 jmcneill bool bounce;
518 1.5 jmcneill int error;
519 1.1 jmcneill
520 1.5 jmcneill #if notyet
521 1.5 jmcneill bounce = false;
522 1.5 jmcneill for (int seg = 0; seg < cmd->c_dmamap->dm_nsegs; seg++) {
523 1.5 jmcneill if ((cmd->c_dmamap->dm_segs[seg].ds_addr & 0x1f) != 0 ||
524 1.5 jmcneill (cmd->c_dmamap->dm_segs[seg].ds_len & 3) != 0) {
525 1.1 jmcneill bounce = true;
526 1.1 jmcneill break;
527 1.1 jmcneill }
528 1.1 jmcneill }
529 1.5 jmcneill #else
530 1.5 jmcneill bounce = true;
531 1.5 jmcneill #endif
532 1.1 jmcneill
533 1.1 jmcneill if (bounce) {
534 1.1 jmcneill if (ISSET(cmd->c_flags, SCF_CMD_READ)) {
535 1.1 jmcneill bus_dmamap_sync(sc->sc.sc_dmat, sc->sc_edma_dmamap, 0,
536 1.1 jmcneill MAXPHYS, BUS_DMASYNC_PREREAD);
537 1.1 jmcneill } else {
538 1.1 jmcneill memcpy(sc->sc_edma_bbuf, cmd->c_data, cmd->c_datalen);
539 1.1 jmcneill bus_dmamap_sync(sc->sc.sc_dmat, sc->sc_edma_dmamap, 0,
540 1.1 jmcneill MAXPHYS, BUS_DMASYNC_PREWRITE);
541 1.1 jmcneill }
542 1.1 jmcneill
543 1.1 jmcneill cmd->c_dmamap = sc->sc_edma_dmamap;
544 1.1 jmcneill }
545 1.1 jmcneill
546 1.1 jmcneill error = ti_sdhc_edma_transfer(sdhc_sc, cmd);
547 1.1 jmcneill
548 1.1 jmcneill if (bounce) {
549 1.1 jmcneill if (ISSET(cmd->c_flags, SCF_CMD_READ)) {
550 1.1 jmcneill bus_dmamap_sync(sc->sc.sc_dmat, sc->sc_edma_dmamap, 0,
551 1.1 jmcneill MAXPHYS, BUS_DMASYNC_POSTREAD);
552 1.1 jmcneill } else {
553 1.1 jmcneill bus_dmamap_sync(sc->sc.sc_dmat, sc->sc_edma_dmamap, 0,
554 1.1 jmcneill MAXPHYS, BUS_DMASYNC_POSTWRITE);
555 1.1 jmcneill }
556 1.1 jmcneill if (ISSET(cmd->c_flags, SCF_CMD_READ) && error == 0) {
557 1.1 jmcneill memcpy(cmd->c_data, sc->sc_edma_bbuf, cmd->c_datalen);
558 1.1 jmcneill }
559 1.1 jmcneill
560 1.1 jmcneill cmd->c_dmamap = map;
561 1.1 jmcneill }
562 1.1 jmcneill
563 1.1 jmcneill return error;
564 1.1 jmcneill }
565 1.1 jmcneill
566 1.1 jmcneill static int
567 1.1 jmcneill ti_sdhc_edma_transfer(struct sdhc_softc *sdhc_sc, struct sdmmc_command *cmd)
568 1.1 jmcneill {
569 1.1 jmcneill struct ti_sdhc_softc *sc = device_private(sdhc_sc->sc_dev);
570 1.1 jmcneill kmutex_t *plock = sdhc_host_lock(sc->sc_hosts[0]);
571 1.1 jmcneill struct edma_channel *edma;
572 1.1 jmcneill uint16_t *edma_param;
573 1.1 jmcneill struct edma_param ep;
574 1.1 jmcneill size_t seg;
575 1.1 jmcneill int error, resid = cmd->c_datalen;
576 1.1 jmcneill int blksize = MIN(cmd->c_datalen, cmd->c_blklen);
577 1.1 jmcneill
578 1.1 jmcneill KASSERT(mutex_owned(plock));
579 1.1 jmcneill
580 1.1 jmcneill edma = ISSET(cmd->c_flags, SCF_CMD_READ) ?
581 1.1 jmcneill sc->sc_edma_rx : sc->sc_edma_tx;
582 1.1 jmcneill edma_param = ISSET(cmd->c_flags, SCF_CMD_READ) ?
583 1.1 jmcneill sc->sc_edma_param_rx : sc->sc_edma_param_tx;
584 1.1 jmcneill
585 1.1 jmcneill DPRINTF(1, (sc->sc.sc_dev, "edma xfer: nsegs=%d ch# %d\n",
586 1.1 jmcneill cmd->c_dmamap->dm_nsegs, edma_channel_index(edma)));
587 1.1 jmcneill
588 1.1 jmcneill if (cmd->c_dmamap->dm_nsegs > EDMA_MAX_PARAMS) {
589 1.1 jmcneill return ENOMEM;
590 1.1 jmcneill }
591 1.1 jmcneill
592 1.1 jmcneill for (seg = 0; seg < cmd->c_dmamap->dm_nsegs; seg++) {
593 1.1 jmcneill KASSERT(resid > 0);
594 1.1 jmcneill const int xferlen = uimin(resid,
595 1.1 jmcneill cmd->c_dmamap->dm_segs[seg].ds_len);
596 1.1 jmcneill KASSERT(xferlen == cmd->c_dmamap->dm_segs[seg].ds_len ||
597 1.1 jmcneill seg == cmd->c_dmamap->dm_nsegs - 1);
598 1.1 jmcneill resid -= xferlen;
599 1.1 jmcneill KASSERT((xferlen & 0x3) == 0);
600 1.1 jmcneill ep.ep_opt = __SHIFTIN(2, EDMA_PARAM_OPT_FWID) /* 32-bit */;
601 1.1 jmcneill ep.ep_opt |= __SHIFTIN(edma_channel_index(edma),
602 1.1 jmcneill EDMA_PARAM_OPT_TCC);
603 1.1 jmcneill if (seg == cmd->c_dmamap->dm_nsegs - 1) {
604 1.1 jmcneill ep.ep_opt |= EDMA_PARAM_OPT_TCINTEN;
605 1.1 jmcneill ep.ep_link = 0xffff;
606 1.1 jmcneill } else {
607 1.1 jmcneill ep.ep_link = EDMA_PARAM_BASE(edma_param[seg+1]);
608 1.1 jmcneill }
609 1.1 jmcneill if (ISSET(cmd->c_flags, SCF_CMD_READ)) {
610 1.1 jmcneill ep.ep_opt |= EDMA_PARAM_OPT_SAM;
611 1.1 jmcneill ep.ep_src = sc->sc_edma_fifo;
612 1.1 jmcneill ep.ep_dst = cmd->c_dmamap->dm_segs[seg].ds_addr;
613 1.1 jmcneill } else {
614 1.1 jmcneill ep.ep_opt |= EDMA_PARAM_OPT_DAM;
615 1.1 jmcneill ep.ep_src = cmd->c_dmamap->dm_segs[seg].ds_addr;
616 1.1 jmcneill ep.ep_dst = sc->sc_edma_fifo;
617 1.1 jmcneill }
618 1.1 jmcneill
619 1.1 jmcneill KASSERT(xferlen <= 65536 * 4);
620 1.1 jmcneill
621 1.1 jmcneill /*
622 1.1 jmcneill * In constant addressing mode, the address must be aligned
623 1.1 jmcneill * to 256-bits.
624 1.1 jmcneill */
625 1.1 jmcneill KASSERT((cmd->c_dmamap->dm_segs[seg].ds_addr & 0x1f) == 0);
626 1.1 jmcneill
627 1.1 jmcneill /*
628 1.1 jmcneill * For unknown reason, the A-DMA transfers never completes for
629 1.1 jmcneill * transfers larger than 64 butes. So use a AB transfer,
630 1.1 jmcneill * with a 64 bytes A len
631 1.1 jmcneill */
632 1.1 jmcneill ep.ep_bcntrld = 0; /* not used for AB-synchronous mode */
633 1.1 jmcneill ep.ep_opt |= EDMA_PARAM_OPT_SYNCDIM;
634 1.1 jmcneill ep.ep_acnt = uimin(xferlen, 64);
635 1.1 jmcneill ep.ep_bcnt = uimin(xferlen, blksize) / ep.ep_acnt;
636 1.1 jmcneill ep.ep_ccnt = xferlen / (ep.ep_acnt * ep.ep_bcnt);
637 1.1 jmcneill ep.ep_srcbidx = ep.ep_dstbidx = 0;
638 1.1 jmcneill ep.ep_srccidx = ep.ep_dstcidx = 0;
639 1.1 jmcneill if (ISSET(cmd->c_flags, SCF_CMD_READ)) {
640 1.1 jmcneill ep.ep_dstbidx = ep.ep_acnt;
641 1.1 jmcneill ep.ep_dstcidx = ep.ep_acnt * ep.ep_bcnt;
642 1.1 jmcneill } else {
643 1.1 jmcneill ep.ep_srcbidx = ep.ep_acnt;
644 1.1 jmcneill ep.ep_srccidx = ep.ep_acnt * ep.ep_bcnt;
645 1.1 jmcneill }
646 1.1 jmcneill
647 1.1 jmcneill edma_set_param(edma, edma_param[seg], &ep);
648 1.1 jmcneill #ifdef TISDHC_DEBUG
649 1.1 jmcneill if (tisdhcdebug >= 1) {
650 1.1 jmcneill printf("target OPT: %08x\n", ep.ep_opt);
651 1.1 jmcneill edma_dump_param(edma, edma_param[seg]);
652 1.1 jmcneill }
653 1.1 jmcneill #endif
654 1.1 jmcneill }
655 1.1 jmcneill
656 1.1 jmcneill error = 0;
657 1.1 jmcneill sc->sc_edma_pending = true;
658 1.1 jmcneill edma_transfer_enable(edma, edma_param[0]);
659 1.1 jmcneill while (sc->sc_edma_pending) {
660 1.1 jmcneill error = cv_timedwait(&sc->sc_edma_cv, plock, hz*10);
661 1.1 jmcneill if (error == EWOULDBLOCK) {
662 1.1 jmcneill device_printf(sc->sc.sc_dev, "transfer timeout!\n");
663 1.1 jmcneill edma_dump(edma);
664 1.1 jmcneill edma_dump_param(edma, edma_param[0]);
665 1.1 jmcneill edma_halt(edma);
666 1.1 jmcneill sc->sc_edma_pending = false;
667 1.1 jmcneill error = ETIMEDOUT;
668 1.1 jmcneill break;
669 1.1 jmcneill }
670 1.1 jmcneill }
671 1.1 jmcneill edma_halt(edma);
672 1.1 jmcneill
673 1.1 jmcneill return error;
674 1.1 jmcneill }
675 1.1 jmcneill
676 1.1 jmcneill static void
677 1.1 jmcneill ti_sdhc_edma_done(void *priv)
678 1.1 jmcneill {
679 1.1 jmcneill struct ti_sdhc_softc *sc = priv;
680 1.1 jmcneill kmutex_t *plock = sdhc_host_lock(sc->sc_hosts[0]);
681 1.1 jmcneill
682 1.1 jmcneill mutex_enter(plock);
683 1.1 jmcneill KASSERT(sc->sc_edma_pending == true);
684 1.1 jmcneill sc->sc_edma_pending = false;
685 1.1 jmcneill cv_broadcast(&sc->sc_edma_cv);
686 1.1 jmcneill mutex_exit(plock);
687 1.1 jmcneill }
688