bcm2835_sdhost.c revision 1.7 1 1.7 thorpej /* $NetBSD: bcm2835_sdhost.c,v 1.7 2021/01/27 03:10:19 thorpej 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.7 thorpej __KERNEL_RCSID(0, "$NetBSD: bcm2835_sdhost.c,v 1.7 2021/01/27 03:10:19 thorpej Exp $");
31 1.4 skrll
32 1.4 skrll #include "bcmdmac.h"
33 1.1 jmcneill
34 1.1 jmcneill #include <sys/param.h>
35 1.1 jmcneill #include <sys/bus.h>
36 1.1 jmcneill #include <sys/device.h>
37 1.1 jmcneill #include <sys/intr.h>
38 1.1 jmcneill #include <sys/systm.h>
39 1.1 jmcneill #include <sys/kernel.h>
40 1.1 jmcneill #include <sys/gpio.h>
41 1.1 jmcneill
42 1.1 jmcneill #include <arm/broadcom/bcm2835reg.h>
43 1.1 jmcneill #include <arm/broadcom/bcm2835_dmac.h>
44 1.1 jmcneill
45 1.1 jmcneill #include <dev/sdmmc/sdmmcvar.h>
46 1.1 jmcneill #include <dev/sdmmc/sdmmcchip.h>
47 1.1 jmcneill #include <dev/sdmmc/sdmmc_ioreg.h>
48 1.1 jmcneill
49 1.4 skrll #include <dev/fdt/fdtvar.h>
50 1.4 skrll
51 1.4 skrll #include <arm/fdt/arm_fdtvar.h>
52 1.4 skrll
53 1.1 jmcneill #define SDCMD 0x00
54 1.1 jmcneill #define SDCMD_NEW __BIT(15)
55 1.1 jmcneill #define SDCMD_FAIL __BIT(14)
56 1.1 jmcneill #define SDCMD_BUSY __BIT(11)
57 1.1 jmcneill #define SDCMD_NORESP __BIT(10)
58 1.1 jmcneill #define SDCMD_LONGRESP __BIT(9)
59 1.1 jmcneill #define SDCMD_WRITE __BIT(7)
60 1.1 jmcneill #define SDCMD_READ __BIT(6)
61 1.1 jmcneill #define SDARG 0x04
62 1.1 jmcneill #define SDTOUT 0x08
63 1.1 jmcneill #define SDTOUT_DEFAULT 0xf00000
64 1.1 jmcneill #define SDCDIV 0x0c
65 1.1 jmcneill #define SDCDIV_MASK __BITS(10,0)
66 1.1 jmcneill #define SDRSP0 0x10
67 1.1 jmcneill #define SDRSP1 0x14
68 1.1 jmcneill #define SDRSP2 0x18
69 1.1 jmcneill #define SDRSP3 0x1c
70 1.1 jmcneill #define SDHSTS 0x20
71 1.1 jmcneill #define SDHSTS_BUSY __BIT(10)
72 1.1 jmcneill #define SDHSTS_BLOCK __BIT(9)
73 1.1 jmcneill #define SDHSTS_SDIO __BIT(8)
74 1.1 jmcneill #define SDHSTS_REW_TO __BIT(7)
75 1.1 jmcneill #define SDHSTS_CMD_TO __BIT(6)
76 1.1 jmcneill #define SDHSTS_CRC16_E __BIT(5)
77 1.1 jmcneill #define SDHSTS_CRC7_E __BIT(4)
78 1.1 jmcneill #define SDHSTS_FIFO_E __BIT(3)
79 1.1 jmcneill #define SDHSTS_DATA __BIT(0)
80 1.1 jmcneill #define SDVDD 0x30
81 1.1 jmcneill #define SDVDD_POWER __BIT(0)
82 1.1 jmcneill #define SDEDM 0x34
83 1.1 jmcneill #define SDEDM_RD_FIFO __BITS(18,14)
84 1.1 jmcneill #define SDEDM_WR_FIFO __BITS(13,9)
85 1.1 jmcneill #define SDHCFG 0x38
86 1.1 jmcneill #define SDHCFG_BUSY_EN __BIT(10)
87 1.1 jmcneill #define SDHCFG_BLOCK_EN __BIT(8)
88 1.1 jmcneill #define SDHCFG_SDIO_EN __BIT(5)
89 1.1 jmcneill #define SDHCFG_DATA_EN __BIT(4)
90 1.1 jmcneill #define SDHCFG_SLOW __BIT(3)
91 1.1 jmcneill #define SDHCFG_WIDE_EXT __BIT(2)
92 1.1 jmcneill #define SDHCFG_WIDE_INT __BIT(1)
93 1.1 jmcneill #define SDHCFG_REL_CMD __BIT(0)
94 1.1 jmcneill #define SDHBCT 0x3c
95 1.1 jmcneill #define SDDATA 0x40
96 1.1 jmcneill #define SDHBLC 0x50
97 1.1 jmcneill
98 1.1 jmcneill struct sdhost_softc;
99 1.1 jmcneill
100 1.1 jmcneill static int sdhost_match(device_t, cfdata_t, void *);
101 1.1 jmcneill static void sdhost_attach(device_t, device_t, void *);
102 1.1 jmcneill static void sdhost_attach_i(device_t);
103 1.1 jmcneill
104 1.1 jmcneill static int sdhost_intr(void *);
105 1.1 jmcneill static int sdhost_dma_setup(struct sdhost_softc *);
106 1.1 jmcneill static void sdhost_dma_done(uint32_t, uint32_t, void *);
107 1.1 jmcneill
108 1.1 jmcneill static int sdhost_host_reset(sdmmc_chipset_handle_t);
109 1.1 jmcneill static uint32_t sdhost_host_ocr(sdmmc_chipset_handle_t);
110 1.1 jmcneill static int sdhost_host_maxblklen(sdmmc_chipset_handle_t);
111 1.1 jmcneill static int sdhost_card_detect(sdmmc_chipset_handle_t);
112 1.1 jmcneill static int sdhost_write_protect(sdmmc_chipset_handle_t);
113 1.1 jmcneill static int sdhost_bus_power(sdmmc_chipset_handle_t, uint32_t);
114 1.1 jmcneill static int sdhost_bus_clock(sdmmc_chipset_handle_t, int, bool);
115 1.1 jmcneill static int sdhost_bus_width(sdmmc_chipset_handle_t, int);
116 1.1 jmcneill static int sdhost_bus_rod(sdmmc_chipset_handle_t, int);
117 1.1 jmcneill static void sdhost_exec_command(sdmmc_chipset_handle_t,
118 1.1 jmcneill struct sdmmc_command *);
119 1.1 jmcneill static void sdhost_card_enable_intr(sdmmc_chipset_handle_t, int);
120 1.1 jmcneill static void sdhost_card_intr_ack(sdmmc_chipset_handle_t);
121 1.1 jmcneill
122 1.1 jmcneill static struct sdmmc_chip_functions sdhost_chip_functions = {
123 1.1 jmcneill .host_reset = sdhost_host_reset,
124 1.1 jmcneill .host_ocr = sdhost_host_ocr,
125 1.1 jmcneill .host_maxblklen = sdhost_host_maxblklen,
126 1.1 jmcneill .card_detect = sdhost_card_detect,
127 1.1 jmcneill .write_protect = sdhost_write_protect,
128 1.1 jmcneill .bus_power = sdhost_bus_power,
129 1.1 jmcneill .bus_clock_ddr = sdhost_bus_clock,
130 1.1 jmcneill .bus_width = sdhost_bus_width,
131 1.1 jmcneill .bus_rod = sdhost_bus_rod,
132 1.1 jmcneill .exec_command = sdhost_exec_command,
133 1.1 jmcneill .card_enable_intr = sdhost_card_enable_intr,
134 1.1 jmcneill .card_intr_ack = sdhost_card_intr_ack,
135 1.1 jmcneill };
136 1.1 jmcneill
137 1.1 jmcneill struct sdhost_softc {
138 1.1 jmcneill device_t sc_dev;
139 1.1 jmcneill bus_space_tag_t sc_bst;
140 1.1 jmcneill bus_space_handle_t sc_bsh;
141 1.1 jmcneill bus_dma_tag_t sc_dmat;
142 1.1 jmcneill
143 1.1 jmcneill bus_addr_t sc_addr;
144 1.1 jmcneill
145 1.1 jmcneill void *sc_ih;
146 1.1 jmcneill kmutex_t sc_intr_lock;
147 1.1 jmcneill kcondvar_t sc_intr_cv;
148 1.1 jmcneill kcondvar_t sc_dma_cv;
149 1.1 jmcneill
150 1.1 jmcneill u_int sc_rate;
151 1.1 jmcneill
152 1.1 jmcneill int sc_mmc_width;
153 1.1 jmcneill int sc_mmc_present;
154 1.1 jmcneill
155 1.1 jmcneill device_t sc_sdmmc_dev;
156 1.1 jmcneill
157 1.1 jmcneill struct bcm_dmac_channel *sc_dmac;
158 1.1 jmcneill
159 1.1 jmcneill bus_dmamap_t sc_dmamap;
160 1.1 jmcneill bus_dma_segment_t sc_segs[1];
161 1.1 jmcneill struct bcm_dmac_conblk *sc_cblk;
162 1.1 jmcneill
163 1.1 jmcneill uint32_t sc_intr_hsts;
164 1.1 jmcneill
165 1.1 jmcneill uint32_t sc_dma_status;
166 1.1 jmcneill uint32_t sc_dma_error;
167 1.1 jmcneill };
168 1.1 jmcneill
169 1.1 jmcneill CFATTACH_DECL_NEW(bcmsdhost, sizeof(struct sdhost_softc),
170 1.1 jmcneill sdhost_match, sdhost_attach, NULL, NULL);
171 1.1 jmcneill
172 1.1 jmcneill #define SDHOST_WRITE(sc, reg, val) \
173 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
174 1.1 jmcneill #define SDHOST_READ(sc, reg) \
175 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
176 1.1 jmcneill
177 1.7 thorpej static const struct device_compatible_entry compat_data[] = {
178 1.7 thorpej { .compat = "brcm,bcm2835-sdhost" },
179 1.7 thorpej DEVICE_COMPAT_EOL
180 1.7 thorpej };
181 1.7 thorpej
182 1.1 jmcneill static int
183 1.1 jmcneill sdhost_match(device_t parent, cfdata_t cf, void *aux)
184 1.1 jmcneill {
185 1.4 skrll struct fdt_attach_args * const faa = aux;
186 1.1 jmcneill
187 1.7 thorpej return of_compatible_match(faa->faa_phandle, compat_data);
188 1.1 jmcneill }
189 1.1 jmcneill
190 1.1 jmcneill static void
191 1.1 jmcneill sdhost_attach(device_t parent, device_t self, void *aux)
192 1.1 jmcneill {
193 1.1 jmcneill struct sdhost_softc * const sc = device_private(self);
194 1.4 skrll struct fdt_attach_args * const faa = aux;
195 1.1 jmcneill
196 1.1 jmcneill sc->sc_dev = self;
197 1.4 skrll sc->sc_bst = faa->faa_bst;
198 1.4 skrll sc->sc_dmat = faa->faa_dmat;
199 1.4 skrll
200 1.4 skrll const int phandle = faa->faa_phandle;
201 1.4 skrll bus_addr_t addr;
202 1.4 skrll bus_size_t size;
203 1.4 skrll
204 1.4 skrll if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
205 1.4 skrll aprint_error(": missing 'reg' property\n");
206 1.4 skrll return;
207 1.4 skrll }
208 1.4 skrll
209 1.4 skrll sc->sc_addr = addr;
210 1.1 jmcneill mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_BIO);
211 1.1 jmcneill cv_init(&sc->sc_intr_cv, "sdhostintr");
212 1.1 jmcneill cv_init(&sc->sc_dma_cv, "sdhostdma");
213 1.1 jmcneill
214 1.4 skrll if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
215 1.1 jmcneill aprint_error(": couldn't map registers\n");
216 1.1 jmcneill return;
217 1.1 jmcneill }
218 1.1 jmcneill
219 1.1 jmcneill aprint_naive("\n");
220 1.1 jmcneill aprint_normal(": SD HOST controller\n");
221 1.1 jmcneill
222 1.4 skrll /* Enable clocks */
223 1.4 skrll struct clk *clk;
224 1.4 skrll for (int i = 0; (clk = fdtbus_clock_get_index(phandle, i)); i++) {
225 1.4 skrll if (clk_enable(clk) != 0) {
226 1.4 skrll aprint_error(": failed to enable clock #%d\n", i);
227 1.4 skrll return;
228 1.4 skrll }
229 1.4 skrll if (i == 0)
230 1.4 skrll sc->sc_rate = clk_get_rate(clk);
231 1.1 jmcneill }
232 1.1 jmcneill
233 1.2 jmcneill aprint_debug_dev(self, "ref freq %u Hz\n", sc->sc_rate);
234 1.1 jmcneill
235 1.1 jmcneill if (sdhost_dma_setup(sc) != 0) {
236 1.1 jmcneill aprint_error_dev(self, "failed to setup DMA\n");
237 1.1 jmcneill return;
238 1.1 jmcneill }
239 1.1 jmcneill
240 1.4 skrll char intrstr[128];
241 1.4 skrll if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
242 1.4 skrll aprint_error(": failed to decode interrupt\n");
243 1.4 skrll return;
244 1.4 skrll }
245 1.4 skrll
246 1.4 skrll sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_SDMMC,
247 1.4 skrll FDT_INTR_MPSAFE, sdhost_intr, sc);
248 1.1 jmcneill if (sc->sc_ih == NULL) {
249 1.4 skrll aprint_error_dev(self, "failed to establish interrupt %s\n",
250 1.4 skrll intrstr);
251 1.1 jmcneill return;
252 1.1 jmcneill }
253 1.4 skrll aprint_normal_dev(self, "interrupting on %s\n", intrstr);
254 1.1 jmcneill
255 1.1 jmcneill config_interrupts(self, sdhost_attach_i);
256 1.1 jmcneill }
257 1.1 jmcneill
258 1.1 jmcneill static int
259 1.1 jmcneill sdhost_dma_setup(struct sdhost_softc *sc)
260 1.1 jmcneill {
261 1.1 jmcneill int error, rseg;
262 1.1 jmcneill
263 1.1 jmcneill sc->sc_dmac = bcm_dmac_alloc(BCM_DMAC_TYPE_NORMAL, IPL_SDMMC,
264 1.1 jmcneill sdhost_dma_done, sc);
265 1.1 jmcneill if (sc->sc_dmac == NULL)
266 1.1 jmcneill return ENXIO;
267 1.1 jmcneill
268 1.1 jmcneill error = bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE, PAGE_SIZE,
269 1.1 jmcneill PAGE_SIZE, sc->sc_segs, 1, &rseg, BUS_DMA_WAITOK);
270 1.1 jmcneill if (error)
271 1.1 jmcneill return error;
272 1.1 jmcneill
273 1.1 jmcneill error = bus_dmamem_map(sc->sc_dmat, sc->sc_segs, rseg, PAGE_SIZE,
274 1.1 jmcneill (void **)&sc->sc_cblk, BUS_DMA_WAITOK);
275 1.1 jmcneill if (error)
276 1.1 jmcneill return error;
277 1.1 jmcneill
278 1.1 jmcneill memset(sc->sc_cblk, 0, PAGE_SIZE);
279 1.1 jmcneill
280 1.1 jmcneill error = bus_dmamap_create(sc->sc_dmat, PAGE_SIZE, 1, PAGE_SIZE, 0,
281 1.1 jmcneill BUS_DMA_WAITOK, &sc->sc_dmamap);
282 1.1 jmcneill if (error)
283 1.1 jmcneill return error;
284 1.1 jmcneill
285 1.1 jmcneill error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap, sc->sc_cblk,
286 1.1 jmcneill PAGE_SIZE, NULL, BUS_DMA_WAITOK|BUS_DMA_WRITE);
287 1.1 jmcneill if (error)
288 1.1 jmcneill return error;
289 1.1 jmcneill
290 1.1 jmcneill return 0;
291 1.1 jmcneill }
292 1.1 jmcneill
293 1.1 jmcneill static void
294 1.1 jmcneill sdhost_attach_i(device_t self)
295 1.1 jmcneill {
296 1.1 jmcneill struct sdhost_softc *sc = device_private(self);
297 1.1 jmcneill struct sdmmcbus_attach_args saa;
298 1.1 jmcneill
299 1.1 jmcneill sdhost_host_reset(sc);
300 1.1 jmcneill sdhost_bus_width(sc, 1);
301 1.1 jmcneill sdhost_bus_clock(sc, 400, false);
302 1.1 jmcneill
303 1.1 jmcneill memset(&saa, 0, sizeof(saa));
304 1.1 jmcneill saa.saa_busname = "sdmmc";
305 1.1 jmcneill saa.saa_sct = &sdhost_chip_functions;
306 1.1 jmcneill saa.saa_sch = sc;
307 1.1 jmcneill saa.saa_dmat = sc->sc_dmat;
308 1.1 jmcneill saa.saa_clkmin = 400;
309 1.1 jmcneill saa.saa_clkmax = 50000;
310 1.1 jmcneill saa.saa_caps = SMC_CAPS_DMA |
311 1.1 jmcneill SMC_CAPS_MULTI_SEG_DMA |
312 1.1 jmcneill SMC_CAPS_SD_HIGHSPEED |
313 1.1 jmcneill SMC_CAPS_MMC_HIGHSPEED |
314 1.1 jmcneill SMC_CAPS_4BIT_MODE;
315 1.1 jmcneill
316 1.1 jmcneill sc->sc_sdmmc_dev = config_found(self, &saa, NULL);
317 1.1 jmcneill }
318 1.1 jmcneill
319 1.1 jmcneill static int
320 1.1 jmcneill sdhost_intr(void *priv)
321 1.1 jmcneill {
322 1.1 jmcneill struct sdhost_softc * const sc = priv;
323 1.1 jmcneill
324 1.1 jmcneill mutex_enter(&sc->sc_intr_lock);
325 1.1 jmcneill const uint32_t hsts = SDHOST_READ(sc, SDHSTS);
326 1.1 jmcneill if (!hsts) {
327 1.1 jmcneill mutex_exit(&sc->sc_intr_lock);
328 1.1 jmcneill return 0;
329 1.1 jmcneill }
330 1.1 jmcneill SDHOST_WRITE(sc, SDHSTS, hsts);
331 1.1 jmcneill
332 1.1 jmcneill #ifdef SDHOST_DEBUG
333 1.1 jmcneill device_printf(sc->sc_dev, "mmc intr hsts %#x\n", hsts);
334 1.1 jmcneill #endif
335 1.1 jmcneill
336 1.1 jmcneill if (hsts) {
337 1.1 jmcneill sc->sc_intr_hsts |= hsts;
338 1.1 jmcneill cv_broadcast(&sc->sc_intr_cv);
339 1.1 jmcneill }
340 1.1 jmcneill
341 1.1 jmcneill mutex_exit(&sc->sc_intr_lock);
342 1.1 jmcneill
343 1.1 jmcneill return 1;
344 1.1 jmcneill }
345 1.1 jmcneill
346 1.1 jmcneill static int
347 1.1 jmcneill sdhost_dma_transfer(struct sdhost_softc *sc, struct sdmmc_command *cmd)
348 1.1 jmcneill {
349 1.1 jmcneill size_t seg;
350 1.1 jmcneill int error;
351 1.1 jmcneill
352 1.1 jmcneill KASSERT(mutex_owned(&sc->sc_intr_lock));
353 1.1 jmcneill
354 1.1 jmcneill for (seg = 0; seg < cmd->c_dmamap->dm_nsegs; seg++) {
355 1.1 jmcneill sc->sc_cblk[seg].cb_ti =
356 1.1 jmcneill __SHIFTIN(13, DMAC_TI_PERMAP); /* SD HOST */
357 1.1 jmcneill sc->sc_cblk[seg].cb_txfr_len =
358 1.1 jmcneill cmd->c_dmamap->dm_segs[seg].ds_len;
359 1.4 skrll const bus_addr_t ad_sddata = sc->sc_addr + SDDATA;
360 1.4 skrll
361 1.1 jmcneill /*
362 1.1 jmcneill * All transfers are assumed to be multiples of 32-bits.
363 1.1 jmcneill */
364 1.1 jmcneill KASSERTMSG((sc->sc_cblk[seg].cb_txfr_len & 0x3) == 0,
365 1.1 jmcneill "seg %zu len %d", seg, sc->sc_cblk[seg].cb_txfr_len);
366 1.1 jmcneill if (ISSET(cmd->c_flags, SCF_CMD_READ)) {
367 1.1 jmcneill sc->sc_cblk[seg].cb_ti |= DMAC_TI_DEST_INC;
368 1.1 jmcneill /*
369 1.1 jmcneill * Use 128-bit mode if transfer is a multiple of
370 1.1 jmcneill * 16-bytes.
371 1.1 jmcneill */
372 1.1 jmcneill if ((sc->sc_cblk[seg].cb_txfr_len & 0xf) == 0)
373 1.1 jmcneill sc->sc_cblk[seg].cb_ti |= DMAC_TI_DEST_WIDTH;
374 1.1 jmcneill sc->sc_cblk[seg].cb_ti |= DMAC_TI_SRC_DREQ;
375 1.4 skrll sc->sc_cblk[seg].cb_source_ad = ad_sddata;
376 1.1 jmcneill sc->sc_cblk[seg].cb_dest_ad =
377 1.1 jmcneill cmd->c_dmamap->dm_segs[seg].ds_addr;
378 1.1 jmcneill } else {
379 1.1 jmcneill sc->sc_cblk[seg].cb_ti |= DMAC_TI_SRC_INC;
380 1.1 jmcneill /*
381 1.1 jmcneill * Use 128-bit mode if transfer is a multiple of
382 1.1 jmcneill * 16-bytes.
383 1.1 jmcneill */
384 1.1 jmcneill if ((sc->sc_cblk[seg].cb_txfr_len & 0xf) == 0)
385 1.1 jmcneill sc->sc_cblk[seg].cb_ti |= DMAC_TI_SRC_WIDTH;
386 1.1 jmcneill sc->sc_cblk[seg].cb_ti |= DMAC_TI_DEST_DREQ;
387 1.1 jmcneill sc->sc_cblk[seg].cb_ti |= DMAC_TI_WAIT_RESP;
388 1.1 jmcneill sc->sc_cblk[seg].cb_source_ad =
389 1.1 jmcneill cmd->c_dmamap->dm_segs[seg].ds_addr;
390 1.4 skrll sc->sc_cblk[seg].cb_dest_ad = ad_sddata;
391 1.1 jmcneill }
392 1.1 jmcneill sc->sc_cblk[seg].cb_stride = 0;
393 1.1 jmcneill if (seg == cmd->c_dmamap->dm_nsegs - 1) {
394 1.1 jmcneill sc->sc_cblk[seg].cb_ti |= DMAC_TI_INTEN;
395 1.1 jmcneill sc->sc_cblk[seg].cb_nextconbk = 0;
396 1.1 jmcneill } else {
397 1.1 jmcneill sc->sc_cblk[seg].cb_nextconbk =
398 1.1 jmcneill sc->sc_dmamap->dm_segs[0].ds_addr +
399 1.1 jmcneill sizeof(struct bcm_dmac_conblk) * (seg+1);
400 1.1 jmcneill }
401 1.6 rin bcm_dmac_swap_conblk(&sc->sc_cblk[seg]);
402 1.1 jmcneill sc->sc_cblk[seg].cb_padding[0] = 0;
403 1.1 jmcneill sc->sc_cblk[seg].cb_padding[1] = 0;
404 1.1 jmcneill }
405 1.1 jmcneill
406 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, 0,
407 1.1 jmcneill sc->sc_dmamap->dm_mapsize, BUS_DMASYNC_PREWRITE);
408 1.1 jmcneill
409 1.1 jmcneill error = 0;
410 1.1 jmcneill
411 1.1 jmcneill sc->sc_dma_status = 0;
412 1.1 jmcneill sc->sc_dma_error = 0;
413 1.1 jmcneill
414 1.1 jmcneill bcm_dmac_set_conblk_addr(sc->sc_dmac,
415 1.1 jmcneill sc->sc_dmamap->dm_segs[0].ds_addr);
416 1.1 jmcneill error = bcm_dmac_transfer(sc->sc_dmac);
417 1.1 jmcneill if (error)
418 1.1 jmcneill return error;
419 1.1 jmcneill
420 1.1 jmcneill return 0;
421 1.1 jmcneill }
422 1.1 jmcneill
423 1.1 jmcneill static int
424 1.1 jmcneill sdhost_dma_wait(struct sdhost_softc *sc, struct sdmmc_command *cmd)
425 1.1 jmcneill {
426 1.1 jmcneill int error = 0;
427 1.1 jmcneill
428 1.1 jmcneill while (sc->sc_dma_status == 0 && sc->sc_dma_error == 0) {
429 1.1 jmcneill error = cv_timedwait(&sc->sc_dma_cv, &sc->sc_intr_lock, hz*5);
430 1.1 jmcneill if (error == EWOULDBLOCK) {
431 1.1 jmcneill device_printf(sc->sc_dev, "transfer timeout!\n");
432 1.1 jmcneill bcm_dmac_halt(sc->sc_dmac);
433 1.1 jmcneill error = ETIMEDOUT;
434 1.1 jmcneill break;
435 1.1 jmcneill }
436 1.1 jmcneill }
437 1.1 jmcneill
438 1.1 jmcneill if (sc->sc_dma_status & DMAC_CS_END) {
439 1.1 jmcneill cmd->c_resid = 0;
440 1.1 jmcneill error = 0;
441 1.1 jmcneill } else {
442 1.1 jmcneill error = EIO;
443 1.1 jmcneill }
444 1.1 jmcneill
445 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, 0,
446 1.1 jmcneill sc->sc_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
447 1.1 jmcneill
448 1.1 jmcneill return error;
449 1.1 jmcneill }
450 1.1 jmcneill
451 1.1 jmcneill static void
452 1.1 jmcneill sdhost_dma_done(uint32_t status, uint32_t error, void *arg)
453 1.1 jmcneill {
454 1.1 jmcneill struct sdhost_softc * const sc = arg;
455 1.1 jmcneill
456 1.1 jmcneill if (status != (DMAC_CS_INT|DMAC_CS_END))
457 1.1 jmcneill device_printf(sc->sc_dev, "dma status %#x error %#x\n",
458 1.1 jmcneill status, error);
459 1.1 jmcneill
460 1.1 jmcneill mutex_enter(&sc->sc_intr_lock);
461 1.1 jmcneill sc->sc_dma_status = status;
462 1.1 jmcneill sc->sc_dma_error = error;
463 1.1 jmcneill cv_broadcast(&sc->sc_dma_cv);
464 1.1 jmcneill mutex_exit(&sc->sc_intr_lock);
465 1.1 jmcneill }
466 1.1 jmcneill
467 1.1 jmcneill static int
468 1.1 jmcneill sdhost_wait_idle(struct sdhost_softc *sc, int timeout)
469 1.1 jmcneill {
470 1.1 jmcneill int retry;
471 1.1 jmcneill
472 1.1 jmcneill KASSERT(mutex_owned(&sc->sc_intr_lock));
473 1.1 jmcneill
474 1.1 jmcneill retry = timeout * 1000;
475 1.1 jmcneill
476 1.1 jmcneill while (--retry > 0) {
477 1.1 jmcneill const uint32_t cmd = SDHOST_READ(sc, SDCMD);
478 1.1 jmcneill if ((cmd & SDCMD_NEW) == 0)
479 1.1 jmcneill return 0;
480 1.1 jmcneill delay(1);
481 1.1 jmcneill }
482 1.1 jmcneill
483 1.1 jmcneill return ETIMEDOUT;
484 1.1 jmcneill }
485 1.1 jmcneill
486 1.1 jmcneill static int
487 1.1 jmcneill sdhost_host_reset(sdmmc_chipset_handle_t sch)
488 1.1 jmcneill {
489 1.1 jmcneill struct sdhost_softc * const sc = sch;
490 1.1 jmcneill uint32_t edm;
491 1.1 jmcneill
492 1.1 jmcneill SDHOST_WRITE(sc, SDVDD, 0);
493 1.1 jmcneill SDHOST_WRITE(sc, SDCMD, 0);
494 1.1 jmcneill SDHOST_WRITE(sc, SDARG, 0);
495 1.1 jmcneill SDHOST_WRITE(sc, SDTOUT, SDTOUT_DEFAULT);
496 1.1 jmcneill SDHOST_WRITE(sc, SDCDIV, 0);
497 1.1 jmcneill SDHOST_WRITE(sc, SDHSTS, SDHOST_READ(sc, SDHSTS));
498 1.1 jmcneill SDHOST_WRITE(sc, SDHCFG, 0);
499 1.1 jmcneill SDHOST_WRITE(sc, SDHBCT, 0);
500 1.1 jmcneill SDHOST_WRITE(sc, SDHBLC, 0);
501 1.1 jmcneill
502 1.1 jmcneill edm = SDHOST_READ(sc, SDEDM);
503 1.1 jmcneill edm &= ~(SDEDM_RD_FIFO|SDEDM_WR_FIFO);
504 1.1 jmcneill edm |= __SHIFTIN(4, SDEDM_RD_FIFO);
505 1.1 jmcneill edm |= __SHIFTIN(4, SDEDM_WR_FIFO);
506 1.1 jmcneill SDHOST_WRITE(sc, SDEDM, edm);
507 1.1 jmcneill delay(20000);
508 1.1 jmcneill SDHOST_WRITE(sc, SDVDD, SDVDD_POWER);
509 1.1 jmcneill delay(20000);
510 1.1 jmcneill
511 1.1 jmcneill SDHOST_WRITE(sc, SDHCFG, 0);
512 1.1 jmcneill SDHOST_WRITE(sc, SDCDIV, SDCDIV_MASK);
513 1.1 jmcneill
514 1.1 jmcneill return 0;
515 1.1 jmcneill }
516 1.1 jmcneill
517 1.1 jmcneill static uint32_t
518 1.1 jmcneill sdhost_host_ocr(sdmmc_chipset_handle_t sch)
519 1.1 jmcneill {
520 1.1 jmcneill return MMC_OCR_3_2V_3_3V | MMC_OCR_3_3V_3_4V | MMC_OCR_HCS;
521 1.1 jmcneill }
522 1.1 jmcneill
523 1.1 jmcneill static int
524 1.1 jmcneill sdhost_host_maxblklen(sdmmc_chipset_handle_t sch)
525 1.1 jmcneill {
526 1.1 jmcneill return 8192;
527 1.1 jmcneill }
528 1.1 jmcneill
529 1.1 jmcneill static int
530 1.1 jmcneill sdhost_card_detect(sdmmc_chipset_handle_t sch)
531 1.1 jmcneill {
532 1.1 jmcneill return 1; /* XXX */
533 1.1 jmcneill }
534 1.1 jmcneill
535 1.1 jmcneill static int
536 1.1 jmcneill sdhost_write_protect(sdmmc_chipset_handle_t sch)
537 1.1 jmcneill {
538 1.1 jmcneill return 0; /* no write protect pin, assume rw */
539 1.1 jmcneill }
540 1.1 jmcneill
541 1.1 jmcneill static int
542 1.1 jmcneill sdhost_bus_power(sdmmc_chipset_handle_t sch, uint32_t ocr)
543 1.1 jmcneill {
544 1.1 jmcneill return 0;
545 1.1 jmcneill }
546 1.1 jmcneill
547 1.1 jmcneill static int
548 1.1 jmcneill sdhost_bus_clock(sdmmc_chipset_handle_t sch, int freq, bool ddr)
549 1.1 jmcneill {
550 1.1 jmcneill struct sdhost_softc * const sc = sch;
551 1.1 jmcneill u_int target_rate = freq * 1000;
552 1.1 jmcneill int div;
553 1.1 jmcneill
554 1.1 jmcneill if (freq == 0)
555 1.1 jmcneill div = SDCDIV_MASK;
556 1.1 jmcneill else {
557 1.1 jmcneill div = sc->sc_rate / target_rate;
558 1.1 jmcneill if (div < 2)
559 1.1 jmcneill div = 2;
560 1.1 jmcneill if ((sc->sc_rate / div) > target_rate)
561 1.1 jmcneill div++;
562 1.1 jmcneill div -= 2;
563 1.1 jmcneill if (div > SDCDIV_MASK)
564 1.1 jmcneill div = SDCDIV_MASK;
565 1.1 jmcneill }
566 1.1 jmcneill
567 1.1 jmcneill SDHOST_WRITE(sc, SDCDIV, div);
568 1.1 jmcneill
569 1.1 jmcneill return 0;
570 1.1 jmcneill }
571 1.1 jmcneill
572 1.1 jmcneill static int
573 1.1 jmcneill sdhost_bus_width(sdmmc_chipset_handle_t sch, int width)
574 1.1 jmcneill {
575 1.1 jmcneill struct sdhost_softc * const sc = sch;
576 1.1 jmcneill uint32_t hcfg;
577 1.1 jmcneill
578 1.1 jmcneill #ifdef SDHOST_DEBUG
579 1.1 jmcneill aprint_normal_dev(sc->sc_dev, "width = %d\n", width);
580 1.1 jmcneill #endif
581 1.1 jmcneill
582 1.1 jmcneill hcfg = SDHOST_READ(sc, SDHCFG);
583 1.1 jmcneill if (width == 4)
584 1.1 jmcneill hcfg |= SDHCFG_WIDE_EXT;
585 1.1 jmcneill else
586 1.1 jmcneill hcfg &= ~SDHCFG_WIDE_EXT;
587 1.1 jmcneill hcfg |= (SDHCFG_WIDE_INT | SDHCFG_SLOW);
588 1.1 jmcneill SDHOST_WRITE(sc, SDHCFG, hcfg);
589 1.1 jmcneill
590 1.1 jmcneill return 0;
591 1.1 jmcneill }
592 1.1 jmcneill
593 1.1 jmcneill static int
594 1.1 jmcneill sdhost_bus_rod(sdmmc_chipset_handle_t sch, int on)
595 1.1 jmcneill {
596 1.1 jmcneill return -1;
597 1.1 jmcneill }
598 1.1 jmcneill
599 1.1 jmcneill static void
600 1.1 jmcneill sdhost_exec_command(sdmmc_chipset_handle_t sch, struct sdmmc_command *cmd)
601 1.1 jmcneill {
602 1.1 jmcneill struct sdhost_softc * const sc = sch;
603 1.1 jmcneill uint32_t cmdval, hcfg;
604 1.1 jmcneill u_int nblks;
605 1.1 jmcneill
606 1.1 jmcneill #ifdef SDHOST_DEBUG
607 1.1 jmcneill aprint_normal_dev(sc->sc_dev,
608 1.1 jmcneill "opcode %d flags 0x%x data %p datalen %d blklen %d\n",
609 1.1 jmcneill cmd->c_opcode, cmd->c_flags, cmd->c_data, cmd->c_datalen,
610 1.1 jmcneill cmd->c_blklen);
611 1.1 jmcneill #endif
612 1.1 jmcneill
613 1.1 jmcneill mutex_enter(&sc->sc_intr_lock);
614 1.1 jmcneill
615 1.1 jmcneill hcfg = SDHOST_READ(sc, SDHCFG);
616 1.1 jmcneill SDHOST_WRITE(sc, SDHCFG, hcfg | SDHCFG_BUSY_EN);
617 1.1 jmcneill
618 1.1 jmcneill sc->sc_intr_hsts = 0;
619 1.1 jmcneill
620 1.1 jmcneill cmd->c_error = sdhost_wait_idle(sc, 5000);
621 1.1 jmcneill if (cmd->c_error != 0) {
622 1.1 jmcneill #ifdef SDHOST_DEBUG
623 1.1 jmcneill device_printf(sc->sc_dev, "device is busy\n");
624 1.1 jmcneill #endif
625 1.1 jmcneill goto done;
626 1.1 jmcneill }
627 1.1 jmcneill
628 1.1 jmcneill cmdval = SDCMD_NEW;
629 1.1 jmcneill if (!ISSET(cmd->c_flags, SCF_RSP_PRESENT))
630 1.1 jmcneill cmdval |= SDCMD_NORESP;
631 1.1 jmcneill if (ISSET(cmd->c_flags, SCF_RSP_136))
632 1.1 jmcneill cmdval |= SDCMD_LONGRESP;
633 1.1 jmcneill if (ISSET(cmd->c_flags, SCF_RSP_BSY))
634 1.1 jmcneill cmdval |= SDCMD_BUSY;
635 1.1 jmcneill
636 1.1 jmcneill if (cmd->c_datalen > 0) {
637 1.1 jmcneill if (ISSET(cmd->c_flags, SCF_CMD_READ))
638 1.1 jmcneill cmdval |= SDCMD_READ;
639 1.1 jmcneill else
640 1.1 jmcneill cmdval |= SDCMD_WRITE;
641 1.1 jmcneill
642 1.1 jmcneill nblks = cmd->c_datalen / cmd->c_blklen;
643 1.1 jmcneill if (nblks == 0 || (cmd->c_datalen % cmd->c_blklen) != 0)
644 1.1 jmcneill ++nblks;
645 1.1 jmcneill
646 1.1 jmcneill SDHOST_WRITE(sc, SDHBCT, cmd->c_blklen);
647 1.1 jmcneill SDHOST_WRITE(sc, SDHBLC, nblks);
648 1.1 jmcneill
649 1.1 jmcneill cmd->c_resid = cmd->c_datalen;
650 1.1 jmcneill cmd->c_error = sdhost_dma_transfer(sc, cmd);
651 1.1 jmcneill if (cmd->c_error != 0) {
652 1.1 jmcneill #ifdef SDHOST_DEBUG
653 1.1 jmcneill device_printf(sc->sc_dev, "dma transfer failed: %d\n",
654 1.1 jmcneill cmd->c_error);
655 1.1 jmcneill #endif
656 1.1 jmcneill goto done;
657 1.1 jmcneill }
658 1.1 jmcneill }
659 1.1 jmcneill
660 1.1 jmcneill SDHOST_WRITE(sc, SDARG, cmd->c_arg);
661 1.1 jmcneill SDHOST_WRITE(sc, SDCMD, cmdval | cmd->c_opcode);
662 1.1 jmcneill
663 1.1 jmcneill if (cmd->c_datalen > 0) {
664 1.1 jmcneill cmd->c_error = sdhost_dma_wait(sc, cmd);
665 1.1 jmcneill if (cmd->c_error != 0) {
666 1.1 jmcneill #ifdef SDHOST_DEBUG
667 1.1 jmcneill device_printf(sc->sc_dev,
668 1.1 jmcneill "wait dma failed: %d\n", cmd->c_error);
669 1.1 jmcneill #endif
670 1.1 jmcneill goto done;
671 1.1 jmcneill }
672 1.1 jmcneill }
673 1.1 jmcneill
674 1.1 jmcneill cmd->c_error = sdhost_wait_idle(sc, 5000);
675 1.1 jmcneill if (cmd->c_error != 0) {
676 1.1 jmcneill #ifdef SDHOST_DEBUG
677 1.1 jmcneill device_printf(sc->sc_dev,
678 1.1 jmcneill "wait cmd idle (%#x) failed: %d\n",
679 1.1 jmcneill SDHOST_READ(sc, SDCMD), cmd->c_error);
680 1.1 jmcneill #endif
681 1.1 jmcneill }
682 1.1 jmcneill
683 1.1 jmcneill if ((SDHOST_READ(sc, SDCMD) & SDCMD_FAIL) != 0) {
684 1.1 jmcneill #ifdef SDHOST_DEBUG
685 1.1 jmcneill device_printf(sc->sc_dev, "SDCMD: %#x\n",
686 1.1 jmcneill SDHOST_READ(sc, SDCMD));
687 1.1 jmcneill #endif
688 1.1 jmcneill cmd->c_error = EIO;
689 1.1 jmcneill goto done;
690 1.1 jmcneill }
691 1.1 jmcneill
692 1.1 jmcneill if (ISSET(cmd->c_flags, SCF_RSP_PRESENT)) {
693 1.1 jmcneill if (ISSET(cmd->c_flags, SCF_RSP_136)) {
694 1.1 jmcneill cmd->c_resp[0] = SDHOST_READ(sc, SDRSP0);
695 1.1 jmcneill cmd->c_resp[1] = SDHOST_READ(sc, SDRSP1);
696 1.1 jmcneill cmd->c_resp[2] = SDHOST_READ(sc, SDRSP2);
697 1.1 jmcneill cmd->c_resp[3] = SDHOST_READ(sc, SDRSP3);
698 1.1 jmcneill if (ISSET(cmd->c_flags, SCF_RSP_CRC)) {
699 1.1 jmcneill cmd->c_resp[0] = (cmd->c_resp[0] >> 8) |
700 1.1 jmcneill (cmd->c_resp[1] << 24);
701 1.1 jmcneill cmd->c_resp[1] = (cmd->c_resp[1] >> 8) |
702 1.1 jmcneill (cmd->c_resp[2] << 24);
703 1.1 jmcneill cmd->c_resp[2] = (cmd->c_resp[2] >> 8) |
704 1.1 jmcneill (cmd->c_resp[3] << 24);
705 1.1 jmcneill cmd->c_resp[3] = (cmd->c_resp[3] >> 8);
706 1.1 jmcneill }
707 1.1 jmcneill } else {
708 1.1 jmcneill cmd->c_resp[0] = SDHOST_READ(sc, SDRSP0);
709 1.1 jmcneill }
710 1.1 jmcneill }
711 1.1 jmcneill
712 1.1 jmcneill done:
713 1.1 jmcneill cmd->c_flags |= SCF_ITSDONE;
714 1.1 jmcneill SDHOST_WRITE(sc, SDHCFG, hcfg);
715 1.1 jmcneill SDHOST_WRITE(sc, SDHSTS, SDHOST_READ(sc, SDHSTS));
716 1.1 jmcneill mutex_exit(&sc->sc_intr_lock);
717 1.1 jmcneill
718 1.1 jmcneill #ifdef SDHOST_DEBUG
719 1.1 jmcneill if (cmd->c_error != 0)
720 1.1 jmcneill device_printf(sc->sc_dev, "command failed with error %d\n",
721 1.1 jmcneill cmd->c_error);
722 1.1 jmcneill #endif
723 1.1 jmcneill }
724 1.1 jmcneill
725 1.1 jmcneill static void
726 1.1 jmcneill sdhost_card_enable_intr(sdmmc_chipset_handle_t sch, int enable)
727 1.1 jmcneill {
728 1.1 jmcneill }
729 1.1 jmcneill
730 1.1 jmcneill static void
731 1.1 jmcneill sdhost_card_intr_ack(sdmmc_chipset_handle_t sch)
732 1.1 jmcneill {
733 1.1 jmcneill }
734