ti_sdhc.c revision 1.3 1 1.3 jmcneill /* $NetBSD: ti_sdhc.c,v 1.3 2019/10/29 22:19:13 jmcneill 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.3 jmcneill __KERNEL_RCSID(0, "$NetBSD: ti_sdhc.c,v 1.3 2019/10/29 22:19:13 jmcneill 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.1 jmcneill static const struct of_compat_data compat_data[] = {
89 1.1 jmcneill { "ti,omap2-hsmmc", (uintptr_t)&omap2_hsmmc_config },
90 1.1 jmcneill { "ti,omap3-hsmmc", (uintptr_t)&omap2_hsmmc_config },
91 1.1 jmcneill { "ti,omap3-pre-es3-hsmmc", (uintptr_t)&omap3_pre_es3_hsmmc_config },
92 1.1 jmcneill { "ti,omap4-hsmmc", (uintptr_t)&omap4_hsmmc_config },
93 1.1 jmcneill { NULL }
94 1.1 jmcneill };
95 1.1 jmcneill
96 1.1 jmcneill enum {
97 1.1 jmcneill EDMA_CHAN_TX,
98 1.1 jmcneill EDMA_CHAN_RX,
99 1.1 jmcneill EDMA_NCHAN
100 1.1 jmcneill };
101 1.1 jmcneill
102 1.1 jmcneill struct ti_sdhc_softc {
103 1.1 jmcneill struct sdhc_softc sc;
104 1.1 jmcneill int sc_phandle;
105 1.1 jmcneill bus_addr_t sc_addr;
106 1.1 jmcneill bus_space_tag_t sc_bst;
107 1.1 jmcneill bus_space_handle_t sc_bsh;
108 1.1 jmcneill bus_space_handle_t sc_hl_bsh;
109 1.1 jmcneill bus_space_handle_t sc_sdhc_bsh;
110 1.1 jmcneill struct sdhc_host *sc_hosts[1];
111 1.1 jmcneill void *sc_ih; /* interrupt vectoring */
112 1.1 jmcneill
113 1.1 jmcneill int sc_edma_chan[EDMA_NCHAN];
114 1.1 jmcneill struct edma_channel *sc_edma_tx;
115 1.1 jmcneill struct edma_channel *sc_edma_rx;
116 1.1 jmcneill uint16_t sc_edma_param_tx[EDMA_MAX_PARAMS];
117 1.1 jmcneill uint16_t sc_edma_param_rx[EDMA_MAX_PARAMS];
118 1.1 jmcneill kcondvar_t sc_edma_cv;
119 1.1 jmcneill bus_addr_t sc_edma_fifo;
120 1.1 jmcneill bool sc_edma_pending;
121 1.1 jmcneill bus_dmamap_t sc_edma_dmamap;
122 1.1 jmcneill bus_dma_segment_t sc_edma_segs[1];
123 1.1 jmcneill void *sc_edma_bbuf;
124 1.1 jmcneill };
125 1.1 jmcneill
126 1.1 jmcneill static int ti_sdhc_match(device_t, cfdata_t, void *);
127 1.1 jmcneill static void ti_sdhc_attach(device_t, device_t, void *);
128 1.1 jmcneill
129 1.1 jmcneill static void ti_sdhc_init(struct ti_sdhc_softc *, const struct ti_sdhc_config *);
130 1.1 jmcneill
131 1.1 jmcneill static int ti_sdhc_bus_width(struct sdhc_softc *, int);
132 1.1 jmcneill static int ti_sdhc_rod(struct sdhc_softc *, int);
133 1.1 jmcneill static int ti_sdhc_write_protect(struct sdhc_softc *);
134 1.1 jmcneill static int ti_sdhc_card_detect(struct sdhc_softc *);
135 1.1 jmcneill
136 1.1 jmcneill static int ti_sdhc_edma_init(struct ti_sdhc_softc *, u_int, u_int);
137 1.1 jmcneill static int ti_sdhc_edma_xfer_data(struct sdhc_softc *, struct sdmmc_command *);
138 1.1 jmcneill static void ti_sdhc_edma_done(void *);
139 1.1 jmcneill static int ti_sdhc_edma_transfer(struct sdhc_softc *, struct sdmmc_command *);
140 1.1 jmcneill
141 1.1 jmcneill CFATTACH_DECL_NEW(ti_sdhc, sizeof(struct ti_sdhc_softc),
142 1.1 jmcneill ti_sdhc_match, ti_sdhc_attach, NULL, NULL);
143 1.1 jmcneill
144 1.1 jmcneill static int
145 1.1 jmcneill ti_sdhc_match(device_t parent, cfdata_t cf, void *aux)
146 1.1 jmcneill {
147 1.1 jmcneill struct fdt_attach_args * const faa = aux;
148 1.1 jmcneill
149 1.1 jmcneill return of_match_compat_data(faa->faa_phandle, compat_data);
150 1.1 jmcneill }
151 1.1 jmcneill
152 1.1 jmcneill static void
153 1.1 jmcneill ti_sdhc_attach(device_t parent, device_t self, void *aux)
154 1.1 jmcneill {
155 1.1 jmcneill struct ti_sdhc_softc * const sc = device_private(self);
156 1.1 jmcneill struct fdt_attach_args * const faa = aux;
157 1.1 jmcneill const int phandle = faa->faa_phandle;
158 1.1 jmcneill const struct ti_sdhc_config *conf;
159 1.1 jmcneill bus_addr_t addr;
160 1.1 jmcneill bus_size_t size;
161 1.1 jmcneill u_int bus_width;
162 1.1 jmcneill
163 1.1 jmcneill conf = (const void *)of_search_compatible(phandle, compat_data)->data;
164 1.1 jmcneill
165 1.3 jmcneill if (ti_prcm_enable_hwmod(phandle, 0) != 0) {
166 1.1 jmcneill aprint_error(": couldn't enable module\n");
167 1.1 jmcneill return;
168 1.1 jmcneill }
169 1.1 jmcneill
170 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0 || size <= conf->regoff) {
171 1.1 jmcneill aprint_error(": couldn't get registers\n");
172 1.1 jmcneill return;
173 1.1 jmcneill }
174 1.1 jmcneill addr += conf->regoff;
175 1.1 jmcneill size -= conf->regoff;
176 1.1 jmcneill
177 1.1 jmcneill sc->sc.sc_dmat = faa->faa_dmat;
178 1.1 jmcneill sc->sc.sc_dev = self;
179 1.1 jmcneill sc->sc_phandle = phandle;
180 1.1 jmcneill sc->sc_addr = addr;
181 1.1 jmcneill sc->sc_bst = faa->faa_bst;
182 1.1 jmcneill
183 1.1 jmcneill #if notyet
184 1.1 jmcneill /* XXX use fdtbus_dma API */
185 1.1 jmcneill int len;
186 1.1 jmcneill const u_int *dmas = fdtbus_get_prop(phandle, "dmas", &len);
187 1.1 jmcneill switch (len) {
188 1.1 jmcneill case 24:
189 1.1 jmcneill sc->sc_edma_chan[EDMA_CHAN_TX] = be32toh(dmas[1]);
190 1.1 jmcneill sc->sc_edma_chan[EDMA_CHAN_RX] = be32toh(dmas[4]);
191 1.1 jmcneill break;
192 1.1 jmcneill case 32:
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[5]);
195 1.1 jmcneill break;
196 1.1 jmcneill default:
197 1.1 jmcneill sc->sc_edma_chan[EDMA_CHAN_TX] = -1;
198 1.1 jmcneill sc->sc_edma_chan[EDMA_CHAN_RX] = -1;
199 1.1 jmcneill break;
200 1.1 jmcneill }
201 1.1 jmcneill #else
202 1.1 jmcneill sc->sc_edma_chan[EDMA_CHAN_TX] = -1;
203 1.1 jmcneill sc->sc_edma_chan[EDMA_CHAN_RX] = -1;
204 1.1 jmcneill #endif
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.1 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.1 jmcneill sc->sc_ih = fdtbus_intr_establish(sc->sc_phandle, 0, IPL_VM,
314 1.1 jmcneill 0, sdhc_intr, &sc->sc);
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.1 jmcneill uint32_t con;
437 1.1 jmcneill
438 1.1 jmcneill con = bus_space_read_4(hmsc->sc_bst, hmsc->sc_bsh, MMCHS_CON);
439 1.1 jmcneill if (width == 8) {
440 1.1 jmcneill con |= CON_DW8;
441 1.1 jmcneill } else {
442 1.1 jmcneill con &= ~CON_DW8;
443 1.1 jmcneill }
444 1.1 jmcneill bus_space_write_4(hmsc->sc_bst, hmsc->sc_bsh, MMCHS_CON, con);
445 1.1 jmcneill
446 1.1 jmcneill return 0;
447 1.1 jmcneill }
448 1.1 jmcneill
449 1.1 jmcneill static int
450 1.1 jmcneill ti_sdhc_edma_init(struct ti_sdhc_softc *sc, u_int tx_chan, u_int rx_chan)
451 1.1 jmcneill {
452 1.1 jmcneill int i, error, rseg;
453 1.1 jmcneill
454 1.1 jmcneill /* Request tx and rx DMA channels */
455 1.1 jmcneill sc->sc_edma_tx = edma_channel_alloc(EDMA_TYPE_DMA, tx_chan,
456 1.1 jmcneill ti_sdhc_edma_done, sc);
457 1.1 jmcneill KASSERT(sc->sc_edma_tx != NULL);
458 1.1 jmcneill sc->sc_edma_rx = edma_channel_alloc(EDMA_TYPE_DMA, rx_chan,
459 1.1 jmcneill ti_sdhc_edma_done, sc);
460 1.1 jmcneill KASSERT(sc->sc_edma_rx != NULL);
461 1.1 jmcneill
462 1.1 jmcneill /* Allocate some PaRAM pages */
463 1.1 jmcneill for (i = 0; i < __arraycount(sc->sc_edma_param_tx); i++) {
464 1.1 jmcneill sc->sc_edma_param_tx[i] = edma_param_alloc(sc->sc_edma_tx);
465 1.1 jmcneill KASSERT(sc->sc_edma_param_tx[i] != 0xffff);
466 1.1 jmcneill }
467 1.1 jmcneill for (i = 0; i < __arraycount(sc->sc_edma_param_rx); i++) {
468 1.1 jmcneill sc->sc_edma_param_rx[i] = edma_param_alloc(sc->sc_edma_rx);
469 1.1 jmcneill KASSERT(sc->sc_edma_param_rx[i] != 0xffff);
470 1.1 jmcneill }
471 1.1 jmcneill
472 1.1 jmcneill /* Setup bounce buffer */
473 1.1 jmcneill error = bus_dmamem_alloc(sc->sc.sc_dmat, MAXPHYS, 32, MAXPHYS,
474 1.1 jmcneill sc->sc_edma_segs, 1, &rseg, BUS_DMA_WAITOK);
475 1.1 jmcneill if (error) {
476 1.1 jmcneill aprint_error_dev(sc->sc.sc_dev,
477 1.1 jmcneill "couldn't allocate dmamem: %d\n", error);
478 1.1 jmcneill return error;
479 1.1 jmcneill }
480 1.1 jmcneill KASSERT(rseg == 1);
481 1.1 jmcneill error = bus_dmamem_map(sc->sc.sc_dmat, sc->sc_edma_segs, rseg, MAXPHYS,
482 1.1 jmcneill &sc->sc_edma_bbuf, BUS_DMA_WAITOK);
483 1.1 jmcneill if (error) {
484 1.1 jmcneill aprint_error_dev(sc->sc.sc_dev, "couldn't map dmamem: %d\n",
485 1.1 jmcneill error);
486 1.1 jmcneill return error;
487 1.1 jmcneill }
488 1.1 jmcneill error = bus_dmamap_create(sc->sc.sc_dmat, MAXPHYS, 1, MAXPHYS, 0,
489 1.1 jmcneill BUS_DMA_WAITOK, &sc->sc_edma_dmamap);
490 1.1 jmcneill if (error) {
491 1.1 jmcneill aprint_error_dev(sc->sc.sc_dev, "couldn't create dmamap: %d\n",
492 1.1 jmcneill error);
493 1.1 jmcneill return error;
494 1.1 jmcneill }
495 1.1 jmcneill
496 1.1 jmcneill return error;
497 1.1 jmcneill }
498 1.1 jmcneill
499 1.1 jmcneill static int
500 1.1 jmcneill ti_sdhc_edma_xfer_data(struct sdhc_softc *sdhc_sc, struct sdmmc_command *cmd)
501 1.1 jmcneill {
502 1.1 jmcneill struct ti_sdhc_softc *sc = device_private(sdhc_sc->sc_dev);
503 1.1 jmcneill const bus_dmamap_t map = cmd->c_dmamap;
504 1.1 jmcneill int seg, error;
505 1.1 jmcneill bool bounce;
506 1.1 jmcneill
507 1.1 jmcneill for (bounce = false, seg = 0; seg < cmd->c_dmamap->dm_nsegs; seg++) {
508 1.1 jmcneill if ((cmd->c_dmamap->dm_segs[seg].ds_addr & 0x1f) != 0) {
509 1.1 jmcneill bounce = true;
510 1.1 jmcneill break;
511 1.1 jmcneill }
512 1.1 jmcneill }
513 1.1 jmcneill
514 1.1 jmcneill if (bounce) {
515 1.1 jmcneill error = bus_dmamap_load(sc->sc.sc_dmat, sc->sc_edma_dmamap,
516 1.1 jmcneill sc->sc_edma_bbuf, MAXPHYS, NULL, BUS_DMA_WAITOK);
517 1.1 jmcneill if (error) {
518 1.1 jmcneill device_printf(sc->sc.sc_dev,
519 1.1 jmcneill "[bounce] bus_dmamap_load failed: %d\n", error);
520 1.1 jmcneill return error;
521 1.1 jmcneill }
522 1.1 jmcneill if (ISSET(cmd->c_flags, SCF_CMD_READ)) {
523 1.1 jmcneill bus_dmamap_sync(sc->sc.sc_dmat, sc->sc_edma_dmamap, 0,
524 1.1 jmcneill MAXPHYS, BUS_DMASYNC_PREREAD);
525 1.1 jmcneill } else {
526 1.1 jmcneill memcpy(sc->sc_edma_bbuf, cmd->c_data, cmd->c_datalen);
527 1.1 jmcneill bus_dmamap_sync(sc->sc.sc_dmat, sc->sc_edma_dmamap, 0,
528 1.1 jmcneill MAXPHYS, BUS_DMASYNC_PREWRITE);
529 1.1 jmcneill }
530 1.1 jmcneill
531 1.1 jmcneill cmd->c_dmamap = sc->sc_edma_dmamap;
532 1.1 jmcneill }
533 1.1 jmcneill
534 1.1 jmcneill error = ti_sdhc_edma_transfer(sdhc_sc, cmd);
535 1.1 jmcneill
536 1.1 jmcneill if (bounce) {
537 1.1 jmcneill if (ISSET(cmd->c_flags, SCF_CMD_READ)) {
538 1.1 jmcneill bus_dmamap_sync(sc->sc.sc_dmat, sc->sc_edma_dmamap, 0,
539 1.1 jmcneill MAXPHYS, BUS_DMASYNC_POSTREAD);
540 1.1 jmcneill } else {
541 1.1 jmcneill bus_dmamap_sync(sc->sc.sc_dmat, sc->sc_edma_dmamap, 0,
542 1.1 jmcneill MAXPHYS, BUS_DMASYNC_POSTWRITE);
543 1.1 jmcneill }
544 1.1 jmcneill bus_dmamap_unload(sc->sc.sc_dmat, sc->sc_edma_dmamap);
545 1.1 jmcneill if (ISSET(cmd->c_flags, SCF_CMD_READ) && error == 0) {
546 1.1 jmcneill memcpy(cmd->c_data, sc->sc_edma_bbuf, cmd->c_datalen);
547 1.1 jmcneill }
548 1.1 jmcneill
549 1.1 jmcneill cmd->c_dmamap = map;
550 1.1 jmcneill }
551 1.1 jmcneill
552 1.1 jmcneill return error;
553 1.1 jmcneill }
554 1.1 jmcneill
555 1.1 jmcneill static int
556 1.1 jmcneill ti_sdhc_edma_transfer(struct sdhc_softc *sdhc_sc, struct sdmmc_command *cmd)
557 1.1 jmcneill {
558 1.1 jmcneill struct ti_sdhc_softc *sc = device_private(sdhc_sc->sc_dev);
559 1.1 jmcneill kmutex_t *plock = sdhc_host_lock(sc->sc_hosts[0]);
560 1.1 jmcneill struct edma_channel *edma;
561 1.1 jmcneill uint16_t *edma_param;
562 1.1 jmcneill struct edma_param ep;
563 1.1 jmcneill size_t seg;
564 1.1 jmcneill int error, resid = cmd->c_datalen;
565 1.1 jmcneill int blksize = MIN(cmd->c_datalen, cmd->c_blklen);
566 1.1 jmcneill
567 1.1 jmcneill KASSERT(mutex_owned(plock));
568 1.1 jmcneill
569 1.1 jmcneill edma = ISSET(cmd->c_flags, SCF_CMD_READ) ?
570 1.1 jmcneill sc->sc_edma_rx : sc->sc_edma_tx;
571 1.1 jmcneill edma_param = ISSET(cmd->c_flags, SCF_CMD_READ) ?
572 1.1 jmcneill sc->sc_edma_param_rx : sc->sc_edma_param_tx;
573 1.1 jmcneill
574 1.1 jmcneill DPRINTF(1, (sc->sc.sc_dev, "edma xfer: nsegs=%d ch# %d\n",
575 1.1 jmcneill cmd->c_dmamap->dm_nsegs, edma_channel_index(edma)));
576 1.1 jmcneill
577 1.1 jmcneill if (cmd->c_dmamap->dm_nsegs > EDMA_MAX_PARAMS) {
578 1.1 jmcneill return ENOMEM;
579 1.1 jmcneill }
580 1.1 jmcneill
581 1.1 jmcneill for (seg = 0; seg < cmd->c_dmamap->dm_nsegs; seg++) {
582 1.1 jmcneill KASSERT(resid > 0);
583 1.1 jmcneill const int xferlen = uimin(resid,
584 1.1 jmcneill cmd->c_dmamap->dm_segs[seg].ds_len);
585 1.1 jmcneill KASSERT(xferlen == cmd->c_dmamap->dm_segs[seg].ds_len ||
586 1.1 jmcneill seg == cmd->c_dmamap->dm_nsegs - 1);
587 1.1 jmcneill resid -= xferlen;
588 1.1 jmcneill KASSERT((xferlen & 0x3) == 0);
589 1.1 jmcneill ep.ep_opt = __SHIFTIN(2, EDMA_PARAM_OPT_FWID) /* 32-bit */;
590 1.1 jmcneill ep.ep_opt |= __SHIFTIN(edma_channel_index(edma),
591 1.1 jmcneill EDMA_PARAM_OPT_TCC);
592 1.1 jmcneill if (seg == cmd->c_dmamap->dm_nsegs - 1) {
593 1.1 jmcneill ep.ep_opt |= EDMA_PARAM_OPT_TCINTEN;
594 1.1 jmcneill ep.ep_link = 0xffff;
595 1.1 jmcneill } else {
596 1.1 jmcneill ep.ep_link = EDMA_PARAM_BASE(edma_param[seg+1]);
597 1.1 jmcneill }
598 1.1 jmcneill if (ISSET(cmd->c_flags, SCF_CMD_READ)) {
599 1.1 jmcneill ep.ep_opt |= EDMA_PARAM_OPT_SAM;
600 1.1 jmcneill ep.ep_src = sc->sc_edma_fifo;
601 1.1 jmcneill ep.ep_dst = cmd->c_dmamap->dm_segs[seg].ds_addr;
602 1.1 jmcneill } else {
603 1.1 jmcneill ep.ep_opt |= EDMA_PARAM_OPT_DAM;
604 1.1 jmcneill ep.ep_src = cmd->c_dmamap->dm_segs[seg].ds_addr;
605 1.1 jmcneill ep.ep_dst = sc->sc_edma_fifo;
606 1.1 jmcneill }
607 1.1 jmcneill
608 1.1 jmcneill KASSERT(xferlen <= 65536 * 4);
609 1.1 jmcneill
610 1.1 jmcneill /*
611 1.1 jmcneill * In constant addressing mode, the address must be aligned
612 1.1 jmcneill * to 256-bits.
613 1.1 jmcneill */
614 1.1 jmcneill KASSERT((cmd->c_dmamap->dm_segs[seg].ds_addr & 0x1f) == 0);
615 1.1 jmcneill
616 1.1 jmcneill /*
617 1.1 jmcneill * For unknown reason, the A-DMA transfers never completes for
618 1.1 jmcneill * transfers larger than 64 butes. So use a AB transfer,
619 1.1 jmcneill * with a 64 bytes A len
620 1.1 jmcneill */
621 1.1 jmcneill ep.ep_bcntrld = 0; /* not used for AB-synchronous mode */
622 1.1 jmcneill ep.ep_opt |= EDMA_PARAM_OPT_SYNCDIM;
623 1.1 jmcneill ep.ep_acnt = uimin(xferlen, 64);
624 1.1 jmcneill ep.ep_bcnt = uimin(xferlen, blksize) / ep.ep_acnt;
625 1.1 jmcneill ep.ep_ccnt = xferlen / (ep.ep_acnt * ep.ep_bcnt);
626 1.1 jmcneill ep.ep_srcbidx = ep.ep_dstbidx = 0;
627 1.1 jmcneill ep.ep_srccidx = ep.ep_dstcidx = 0;
628 1.1 jmcneill if (ISSET(cmd->c_flags, SCF_CMD_READ)) {
629 1.1 jmcneill ep.ep_dstbidx = ep.ep_acnt;
630 1.1 jmcneill ep.ep_dstcidx = ep.ep_acnt * ep.ep_bcnt;
631 1.1 jmcneill } else {
632 1.1 jmcneill ep.ep_srcbidx = ep.ep_acnt;
633 1.1 jmcneill ep.ep_srccidx = ep.ep_acnt * ep.ep_bcnt;
634 1.1 jmcneill }
635 1.1 jmcneill
636 1.1 jmcneill edma_set_param(edma, edma_param[seg], &ep);
637 1.1 jmcneill #ifdef TISDHC_DEBUG
638 1.1 jmcneill if (tisdhcdebug >= 1) {
639 1.1 jmcneill printf("target OPT: %08x\n", ep.ep_opt);
640 1.1 jmcneill edma_dump_param(edma, edma_param[seg]);
641 1.1 jmcneill }
642 1.1 jmcneill #endif
643 1.1 jmcneill }
644 1.1 jmcneill
645 1.1 jmcneill error = 0;
646 1.1 jmcneill sc->sc_edma_pending = true;
647 1.1 jmcneill edma_transfer_enable(edma, edma_param[0]);
648 1.1 jmcneill while (sc->sc_edma_pending) {
649 1.1 jmcneill error = cv_timedwait(&sc->sc_edma_cv, plock, hz*10);
650 1.1 jmcneill if (error == EWOULDBLOCK) {
651 1.1 jmcneill device_printf(sc->sc.sc_dev, "transfer timeout!\n");
652 1.1 jmcneill edma_dump(edma);
653 1.1 jmcneill edma_dump_param(edma, edma_param[0]);
654 1.1 jmcneill edma_halt(edma);
655 1.1 jmcneill sc->sc_edma_pending = false;
656 1.1 jmcneill error = ETIMEDOUT;
657 1.1 jmcneill break;
658 1.1 jmcneill }
659 1.1 jmcneill }
660 1.1 jmcneill edma_halt(edma);
661 1.1 jmcneill
662 1.1 jmcneill return error;
663 1.1 jmcneill }
664 1.1 jmcneill
665 1.1 jmcneill static void
666 1.1 jmcneill ti_sdhc_edma_done(void *priv)
667 1.1 jmcneill {
668 1.1 jmcneill struct ti_sdhc_softc *sc = priv;
669 1.1 jmcneill kmutex_t *plock = sdhc_host_lock(sc->sc_hosts[0]);
670 1.1 jmcneill
671 1.1 jmcneill mutex_enter(plock);
672 1.1 jmcneill KASSERT(sc->sc_edma_pending == true);
673 1.1 jmcneill sc->sc_edma_pending = false;
674 1.1 jmcneill cv_broadcast(&sc->sc_edma_cv);
675 1.1 jmcneill mutex_exit(plock);
676 1.1 jmcneill }
677