bcm2835_sdhost.c revision 1.6 1 1.6 rin /* $NetBSD: bcm2835_sdhost.c,v 1.6 2020/12/01 04:15:04 rin 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.6 rin __KERNEL_RCSID(0, "$NetBSD: bcm2835_sdhost.c,v 1.6 2020/12/01 04:15:04 rin 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.1 jmcneill static int
178 1.1 jmcneill sdhost_match(device_t parent, cfdata_t cf, void *aux)
179 1.1 jmcneill {
180 1.4 skrll const char * const compatible[] = {
181 1.4 skrll "brcm,bcm2835-sdhost",
182 1.4 skrll NULL
183 1.4 skrll };
184 1.4 skrll struct fdt_attach_args * const faa = aux;
185 1.1 jmcneill
186 1.4 skrll return of_match_compatible(faa->faa_phandle, compatible);
187 1.1 jmcneill }
188 1.1 jmcneill
189 1.1 jmcneill static void
190 1.1 jmcneill sdhost_attach(device_t parent, device_t self, void *aux)
191 1.1 jmcneill {
192 1.1 jmcneill struct sdhost_softc * const sc = device_private(self);
193 1.4 skrll struct fdt_attach_args * const faa = aux;
194 1.1 jmcneill
195 1.1 jmcneill sc->sc_dev = self;
196 1.4 skrll sc->sc_bst = faa->faa_bst;
197 1.4 skrll sc->sc_dmat = faa->faa_dmat;
198 1.4 skrll
199 1.4 skrll const int phandle = faa->faa_phandle;
200 1.4 skrll bus_addr_t addr;
201 1.4 skrll bus_size_t size;
202 1.4 skrll
203 1.4 skrll if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
204 1.4 skrll aprint_error(": missing 'reg' property\n");
205 1.4 skrll return;
206 1.4 skrll }
207 1.4 skrll
208 1.4 skrll sc->sc_addr = addr;
209 1.1 jmcneill mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_BIO);
210 1.1 jmcneill cv_init(&sc->sc_intr_cv, "sdhostintr");
211 1.1 jmcneill cv_init(&sc->sc_dma_cv, "sdhostdma");
212 1.1 jmcneill
213 1.4 skrll if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
214 1.1 jmcneill aprint_error(": couldn't map registers\n");
215 1.1 jmcneill return;
216 1.1 jmcneill }
217 1.1 jmcneill
218 1.1 jmcneill aprint_naive("\n");
219 1.1 jmcneill aprint_normal(": SD HOST controller\n");
220 1.1 jmcneill
221 1.4 skrll /* Enable clocks */
222 1.4 skrll struct clk *clk;
223 1.4 skrll for (int i = 0; (clk = fdtbus_clock_get_index(phandle, i)); i++) {
224 1.4 skrll if (clk_enable(clk) != 0) {
225 1.4 skrll aprint_error(": failed to enable clock #%d\n", i);
226 1.4 skrll return;
227 1.4 skrll }
228 1.4 skrll if (i == 0)
229 1.4 skrll sc->sc_rate = clk_get_rate(clk);
230 1.1 jmcneill }
231 1.1 jmcneill
232 1.2 jmcneill aprint_debug_dev(self, "ref freq %u Hz\n", sc->sc_rate);
233 1.1 jmcneill
234 1.1 jmcneill if (sdhost_dma_setup(sc) != 0) {
235 1.1 jmcneill aprint_error_dev(self, "failed to setup DMA\n");
236 1.1 jmcneill return;
237 1.1 jmcneill }
238 1.1 jmcneill
239 1.4 skrll char intrstr[128];
240 1.4 skrll if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
241 1.4 skrll aprint_error(": failed to decode interrupt\n");
242 1.4 skrll return;
243 1.4 skrll }
244 1.4 skrll
245 1.4 skrll sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_SDMMC,
246 1.4 skrll FDT_INTR_MPSAFE, sdhost_intr, sc);
247 1.1 jmcneill if (sc->sc_ih == NULL) {
248 1.4 skrll aprint_error_dev(self, "failed to establish interrupt %s\n",
249 1.4 skrll intrstr);
250 1.1 jmcneill return;
251 1.1 jmcneill }
252 1.4 skrll aprint_normal_dev(self, "interrupting on %s\n", intrstr);
253 1.1 jmcneill
254 1.1 jmcneill config_interrupts(self, sdhost_attach_i);
255 1.1 jmcneill }
256 1.1 jmcneill
257 1.1 jmcneill static int
258 1.1 jmcneill sdhost_dma_setup(struct sdhost_softc *sc)
259 1.1 jmcneill {
260 1.1 jmcneill int error, rseg;
261 1.1 jmcneill
262 1.1 jmcneill sc->sc_dmac = bcm_dmac_alloc(BCM_DMAC_TYPE_NORMAL, IPL_SDMMC,
263 1.1 jmcneill sdhost_dma_done, sc);
264 1.1 jmcneill if (sc->sc_dmac == NULL)
265 1.1 jmcneill return ENXIO;
266 1.1 jmcneill
267 1.1 jmcneill error = bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE, PAGE_SIZE,
268 1.1 jmcneill PAGE_SIZE, sc->sc_segs, 1, &rseg, BUS_DMA_WAITOK);
269 1.1 jmcneill if (error)
270 1.1 jmcneill return error;
271 1.1 jmcneill
272 1.1 jmcneill error = bus_dmamem_map(sc->sc_dmat, sc->sc_segs, rseg, PAGE_SIZE,
273 1.1 jmcneill (void **)&sc->sc_cblk, BUS_DMA_WAITOK);
274 1.1 jmcneill if (error)
275 1.1 jmcneill return error;
276 1.1 jmcneill
277 1.1 jmcneill memset(sc->sc_cblk, 0, PAGE_SIZE);
278 1.1 jmcneill
279 1.1 jmcneill error = bus_dmamap_create(sc->sc_dmat, PAGE_SIZE, 1, PAGE_SIZE, 0,
280 1.1 jmcneill BUS_DMA_WAITOK, &sc->sc_dmamap);
281 1.1 jmcneill if (error)
282 1.1 jmcneill return error;
283 1.1 jmcneill
284 1.1 jmcneill error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap, sc->sc_cblk,
285 1.1 jmcneill PAGE_SIZE, NULL, BUS_DMA_WAITOK|BUS_DMA_WRITE);
286 1.1 jmcneill if (error)
287 1.1 jmcneill return error;
288 1.1 jmcneill
289 1.1 jmcneill return 0;
290 1.1 jmcneill }
291 1.1 jmcneill
292 1.1 jmcneill static void
293 1.1 jmcneill sdhost_attach_i(device_t self)
294 1.1 jmcneill {
295 1.1 jmcneill struct sdhost_softc *sc = device_private(self);
296 1.1 jmcneill struct sdmmcbus_attach_args saa;
297 1.1 jmcneill
298 1.1 jmcneill sdhost_host_reset(sc);
299 1.1 jmcneill sdhost_bus_width(sc, 1);
300 1.1 jmcneill sdhost_bus_clock(sc, 400, false);
301 1.1 jmcneill
302 1.1 jmcneill memset(&saa, 0, sizeof(saa));
303 1.1 jmcneill saa.saa_busname = "sdmmc";
304 1.1 jmcneill saa.saa_sct = &sdhost_chip_functions;
305 1.1 jmcneill saa.saa_sch = sc;
306 1.1 jmcneill saa.saa_dmat = sc->sc_dmat;
307 1.1 jmcneill saa.saa_clkmin = 400;
308 1.1 jmcneill saa.saa_clkmax = 50000;
309 1.1 jmcneill saa.saa_caps = SMC_CAPS_DMA |
310 1.1 jmcneill SMC_CAPS_MULTI_SEG_DMA |
311 1.1 jmcneill SMC_CAPS_SD_HIGHSPEED |
312 1.1 jmcneill SMC_CAPS_MMC_HIGHSPEED |
313 1.1 jmcneill SMC_CAPS_4BIT_MODE;
314 1.1 jmcneill
315 1.1 jmcneill sc->sc_sdmmc_dev = config_found(self, &saa, NULL);
316 1.1 jmcneill }
317 1.1 jmcneill
318 1.1 jmcneill static int
319 1.1 jmcneill sdhost_intr(void *priv)
320 1.1 jmcneill {
321 1.1 jmcneill struct sdhost_softc * const sc = priv;
322 1.1 jmcneill
323 1.1 jmcneill mutex_enter(&sc->sc_intr_lock);
324 1.1 jmcneill const uint32_t hsts = SDHOST_READ(sc, SDHSTS);
325 1.1 jmcneill if (!hsts) {
326 1.1 jmcneill mutex_exit(&sc->sc_intr_lock);
327 1.1 jmcneill return 0;
328 1.1 jmcneill }
329 1.1 jmcneill SDHOST_WRITE(sc, SDHSTS, hsts);
330 1.1 jmcneill
331 1.1 jmcneill #ifdef SDHOST_DEBUG
332 1.1 jmcneill device_printf(sc->sc_dev, "mmc intr hsts %#x\n", hsts);
333 1.1 jmcneill #endif
334 1.1 jmcneill
335 1.1 jmcneill if (hsts) {
336 1.1 jmcneill sc->sc_intr_hsts |= hsts;
337 1.1 jmcneill cv_broadcast(&sc->sc_intr_cv);
338 1.1 jmcneill }
339 1.1 jmcneill
340 1.1 jmcneill mutex_exit(&sc->sc_intr_lock);
341 1.1 jmcneill
342 1.1 jmcneill return 1;
343 1.1 jmcneill }
344 1.1 jmcneill
345 1.1 jmcneill static int
346 1.1 jmcneill sdhost_dma_transfer(struct sdhost_softc *sc, struct sdmmc_command *cmd)
347 1.1 jmcneill {
348 1.1 jmcneill size_t seg;
349 1.1 jmcneill int error;
350 1.1 jmcneill
351 1.1 jmcneill KASSERT(mutex_owned(&sc->sc_intr_lock));
352 1.1 jmcneill
353 1.1 jmcneill for (seg = 0; seg < cmd->c_dmamap->dm_nsegs; seg++) {
354 1.1 jmcneill sc->sc_cblk[seg].cb_ti =
355 1.1 jmcneill __SHIFTIN(13, DMAC_TI_PERMAP); /* SD HOST */
356 1.1 jmcneill sc->sc_cblk[seg].cb_txfr_len =
357 1.1 jmcneill cmd->c_dmamap->dm_segs[seg].ds_len;
358 1.4 skrll const bus_addr_t ad_sddata = sc->sc_addr + SDDATA;
359 1.4 skrll
360 1.1 jmcneill /*
361 1.1 jmcneill * All transfers are assumed to be multiples of 32-bits.
362 1.1 jmcneill */
363 1.1 jmcneill KASSERTMSG((sc->sc_cblk[seg].cb_txfr_len & 0x3) == 0,
364 1.1 jmcneill "seg %zu len %d", seg, sc->sc_cblk[seg].cb_txfr_len);
365 1.1 jmcneill if (ISSET(cmd->c_flags, SCF_CMD_READ)) {
366 1.1 jmcneill sc->sc_cblk[seg].cb_ti |= DMAC_TI_DEST_INC;
367 1.1 jmcneill /*
368 1.1 jmcneill * Use 128-bit mode if transfer is a multiple of
369 1.1 jmcneill * 16-bytes.
370 1.1 jmcneill */
371 1.1 jmcneill if ((sc->sc_cblk[seg].cb_txfr_len & 0xf) == 0)
372 1.1 jmcneill sc->sc_cblk[seg].cb_ti |= DMAC_TI_DEST_WIDTH;
373 1.1 jmcneill sc->sc_cblk[seg].cb_ti |= DMAC_TI_SRC_DREQ;
374 1.4 skrll sc->sc_cblk[seg].cb_source_ad = ad_sddata;
375 1.1 jmcneill sc->sc_cblk[seg].cb_dest_ad =
376 1.1 jmcneill cmd->c_dmamap->dm_segs[seg].ds_addr;
377 1.1 jmcneill } else {
378 1.1 jmcneill sc->sc_cblk[seg].cb_ti |= DMAC_TI_SRC_INC;
379 1.1 jmcneill /*
380 1.1 jmcneill * Use 128-bit mode if transfer is a multiple of
381 1.1 jmcneill * 16-bytes.
382 1.1 jmcneill */
383 1.1 jmcneill if ((sc->sc_cblk[seg].cb_txfr_len & 0xf) == 0)
384 1.1 jmcneill sc->sc_cblk[seg].cb_ti |= DMAC_TI_SRC_WIDTH;
385 1.1 jmcneill sc->sc_cblk[seg].cb_ti |= DMAC_TI_DEST_DREQ;
386 1.1 jmcneill sc->sc_cblk[seg].cb_ti |= DMAC_TI_WAIT_RESP;
387 1.1 jmcneill sc->sc_cblk[seg].cb_source_ad =
388 1.1 jmcneill cmd->c_dmamap->dm_segs[seg].ds_addr;
389 1.4 skrll sc->sc_cblk[seg].cb_dest_ad = ad_sddata;
390 1.1 jmcneill }
391 1.1 jmcneill sc->sc_cblk[seg].cb_stride = 0;
392 1.1 jmcneill if (seg == cmd->c_dmamap->dm_nsegs - 1) {
393 1.1 jmcneill sc->sc_cblk[seg].cb_ti |= DMAC_TI_INTEN;
394 1.1 jmcneill sc->sc_cblk[seg].cb_nextconbk = 0;
395 1.1 jmcneill } else {
396 1.1 jmcneill sc->sc_cblk[seg].cb_nextconbk =
397 1.1 jmcneill sc->sc_dmamap->dm_segs[0].ds_addr +
398 1.1 jmcneill sizeof(struct bcm_dmac_conblk) * (seg+1);
399 1.1 jmcneill }
400 1.6 rin bcm_dmac_swap_conblk(&sc->sc_cblk[seg]);
401 1.1 jmcneill sc->sc_cblk[seg].cb_padding[0] = 0;
402 1.1 jmcneill sc->sc_cblk[seg].cb_padding[1] = 0;
403 1.1 jmcneill }
404 1.1 jmcneill
405 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, 0,
406 1.1 jmcneill sc->sc_dmamap->dm_mapsize, BUS_DMASYNC_PREWRITE);
407 1.1 jmcneill
408 1.1 jmcneill error = 0;
409 1.1 jmcneill
410 1.1 jmcneill sc->sc_dma_status = 0;
411 1.1 jmcneill sc->sc_dma_error = 0;
412 1.1 jmcneill
413 1.1 jmcneill bcm_dmac_set_conblk_addr(sc->sc_dmac,
414 1.1 jmcneill sc->sc_dmamap->dm_segs[0].ds_addr);
415 1.1 jmcneill error = bcm_dmac_transfer(sc->sc_dmac);
416 1.1 jmcneill if (error)
417 1.1 jmcneill return error;
418 1.1 jmcneill
419 1.1 jmcneill return 0;
420 1.1 jmcneill }
421 1.1 jmcneill
422 1.1 jmcneill static int
423 1.1 jmcneill sdhost_dma_wait(struct sdhost_softc *sc, struct sdmmc_command *cmd)
424 1.1 jmcneill {
425 1.1 jmcneill int error = 0;
426 1.1 jmcneill
427 1.1 jmcneill while (sc->sc_dma_status == 0 && sc->sc_dma_error == 0) {
428 1.1 jmcneill error = cv_timedwait(&sc->sc_dma_cv, &sc->sc_intr_lock, hz*5);
429 1.1 jmcneill if (error == EWOULDBLOCK) {
430 1.1 jmcneill device_printf(sc->sc_dev, "transfer timeout!\n");
431 1.1 jmcneill bcm_dmac_halt(sc->sc_dmac);
432 1.1 jmcneill error = ETIMEDOUT;
433 1.1 jmcneill break;
434 1.1 jmcneill }
435 1.1 jmcneill }
436 1.1 jmcneill
437 1.1 jmcneill if (sc->sc_dma_status & DMAC_CS_END) {
438 1.1 jmcneill cmd->c_resid = 0;
439 1.1 jmcneill error = 0;
440 1.1 jmcneill } else {
441 1.1 jmcneill error = EIO;
442 1.1 jmcneill }
443 1.1 jmcneill
444 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, 0,
445 1.1 jmcneill sc->sc_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
446 1.1 jmcneill
447 1.1 jmcneill return error;
448 1.1 jmcneill }
449 1.1 jmcneill
450 1.1 jmcneill static void
451 1.1 jmcneill sdhost_dma_done(uint32_t status, uint32_t error, void *arg)
452 1.1 jmcneill {
453 1.1 jmcneill struct sdhost_softc * const sc = arg;
454 1.1 jmcneill
455 1.1 jmcneill if (status != (DMAC_CS_INT|DMAC_CS_END))
456 1.1 jmcneill device_printf(sc->sc_dev, "dma status %#x error %#x\n",
457 1.1 jmcneill status, error);
458 1.1 jmcneill
459 1.1 jmcneill mutex_enter(&sc->sc_intr_lock);
460 1.1 jmcneill sc->sc_dma_status = status;
461 1.1 jmcneill sc->sc_dma_error = error;
462 1.1 jmcneill cv_broadcast(&sc->sc_dma_cv);
463 1.1 jmcneill mutex_exit(&sc->sc_intr_lock);
464 1.1 jmcneill }
465 1.1 jmcneill
466 1.1 jmcneill static int
467 1.1 jmcneill sdhost_wait_idle(struct sdhost_softc *sc, int timeout)
468 1.1 jmcneill {
469 1.1 jmcneill int retry;
470 1.1 jmcneill
471 1.1 jmcneill KASSERT(mutex_owned(&sc->sc_intr_lock));
472 1.1 jmcneill
473 1.1 jmcneill retry = timeout * 1000;
474 1.1 jmcneill
475 1.1 jmcneill while (--retry > 0) {
476 1.1 jmcneill const uint32_t cmd = SDHOST_READ(sc, SDCMD);
477 1.1 jmcneill if ((cmd & SDCMD_NEW) == 0)
478 1.1 jmcneill return 0;
479 1.1 jmcneill delay(1);
480 1.1 jmcneill }
481 1.1 jmcneill
482 1.1 jmcneill return ETIMEDOUT;
483 1.1 jmcneill }
484 1.1 jmcneill
485 1.1 jmcneill static int
486 1.1 jmcneill sdhost_host_reset(sdmmc_chipset_handle_t sch)
487 1.1 jmcneill {
488 1.1 jmcneill struct sdhost_softc * const sc = sch;
489 1.1 jmcneill uint32_t edm;
490 1.1 jmcneill
491 1.1 jmcneill SDHOST_WRITE(sc, SDVDD, 0);
492 1.1 jmcneill SDHOST_WRITE(sc, SDCMD, 0);
493 1.1 jmcneill SDHOST_WRITE(sc, SDARG, 0);
494 1.1 jmcneill SDHOST_WRITE(sc, SDTOUT, SDTOUT_DEFAULT);
495 1.1 jmcneill SDHOST_WRITE(sc, SDCDIV, 0);
496 1.1 jmcneill SDHOST_WRITE(sc, SDHSTS, SDHOST_READ(sc, SDHSTS));
497 1.1 jmcneill SDHOST_WRITE(sc, SDHCFG, 0);
498 1.1 jmcneill SDHOST_WRITE(sc, SDHBCT, 0);
499 1.1 jmcneill SDHOST_WRITE(sc, SDHBLC, 0);
500 1.1 jmcneill
501 1.1 jmcneill edm = SDHOST_READ(sc, SDEDM);
502 1.1 jmcneill edm &= ~(SDEDM_RD_FIFO|SDEDM_WR_FIFO);
503 1.1 jmcneill edm |= __SHIFTIN(4, SDEDM_RD_FIFO);
504 1.1 jmcneill edm |= __SHIFTIN(4, SDEDM_WR_FIFO);
505 1.1 jmcneill SDHOST_WRITE(sc, SDEDM, edm);
506 1.1 jmcneill delay(20000);
507 1.1 jmcneill SDHOST_WRITE(sc, SDVDD, SDVDD_POWER);
508 1.1 jmcneill delay(20000);
509 1.1 jmcneill
510 1.1 jmcneill SDHOST_WRITE(sc, SDHCFG, 0);
511 1.1 jmcneill SDHOST_WRITE(sc, SDCDIV, SDCDIV_MASK);
512 1.1 jmcneill
513 1.1 jmcneill return 0;
514 1.1 jmcneill }
515 1.1 jmcneill
516 1.1 jmcneill static uint32_t
517 1.1 jmcneill sdhost_host_ocr(sdmmc_chipset_handle_t sch)
518 1.1 jmcneill {
519 1.1 jmcneill return MMC_OCR_3_2V_3_3V | MMC_OCR_3_3V_3_4V | MMC_OCR_HCS;
520 1.1 jmcneill }
521 1.1 jmcneill
522 1.1 jmcneill static int
523 1.1 jmcneill sdhost_host_maxblklen(sdmmc_chipset_handle_t sch)
524 1.1 jmcneill {
525 1.1 jmcneill return 8192;
526 1.1 jmcneill }
527 1.1 jmcneill
528 1.1 jmcneill static int
529 1.1 jmcneill sdhost_card_detect(sdmmc_chipset_handle_t sch)
530 1.1 jmcneill {
531 1.1 jmcneill return 1; /* XXX */
532 1.1 jmcneill }
533 1.1 jmcneill
534 1.1 jmcneill static int
535 1.1 jmcneill sdhost_write_protect(sdmmc_chipset_handle_t sch)
536 1.1 jmcneill {
537 1.1 jmcneill return 0; /* no write protect pin, assume rw */
538 1.1 jmcneill }
539 1.1 jmcneill
540 1.1 jmcneill static int
541 1.1 jmcneill sdhost_bus_power(sdmmc_chipset_handle_t sch, uint32_t ocr)
542 1.1 jmcneill {
543 1.1 jmcneill return 0;
544 1.1 jmcneill }
545 1.1 jmcneill
546 1.1 jmcneill static int
547 1.1 jmcneill sdhost_bus_clock(sdmmc_chipset_handle_t sch, int freq, bool ddr)
548 1.1 jmcneill {
549 1.1 jmcneill struct sdhost_softc * const sc = sch;
550 1.1 jmcneill u_int target_rate = freq * 1000;
551 1.1 jmcneill int div;
552 1.1 jmcneill
553 1.1 jmcneill if (freq == 0)
554 1.1 jmcneill div = SDCDIV_MASK;
555 1.1 jmcneill else {
556 1.1 jmcneill div = sc->sc_rate / target_rate;
557 1.1 jmcneill if (div < 2)
558 1.1 jmcneill div = 2;
559 1.1 jmcneill if ((sc->sc_rate / div) > target_rate)
560 1.1 jmcneill div++;
561 1.1 jmcneill div -= 2;
562 1.1 jmcneill if (div > SDCDIV_MASK)
563 1.1 jmcneill div = SDCDIV_MASK;
564 1.1 jmcneill }
565 1.1 jmcneill
566 1.1 jmcneill SDHOST_WRITE(sc, SDCDIV, div);
567 1.1 jmcneill
568 1.1 jmcneill return 0;
569 1.1 jmcneill }
570 1.1 jmcneill
571 1.1 jmcneill static int
572 1.1 jmcneill sdhost_bus_width(sdmmc_chipset_handle_t sch, int width)
573 1.1 jmcneill {
574 1.1 jmcneill struct sdhost_softc * const sc = sch;
575 1.1 jmcneill uint32_t hcfg;
576 1.1 jmcneill
577 1.1 jmcneill #ifdef SDHOST_DEBUG
578 1.1 jmcneill aprint_normal_dev(sc->sc_dev, "width = %d\n", width);
579 1.1 jmcneill #endif
580 1.1 jmcneill
581 1.1 jmcneill hcfg = SDHOST_READ(sc, SDHCFG);
582 1.1 jmcneill if (width == 4)
583 1.1 jmcneill hcfg |= SDHCFG_WIDE_EXT;
584 1.1 jmcneill else
585 1.1 jmcneill hcfg &= ~SDHCFG_WIDE_EXT;
586 1.1 jmcneill hcfg |= (SDHCFG_WIDE_INT | SDHCFG_SLOW);
587 1.1 jmcneill SDHOST_WRITE(sc, SDHCFG, hcfg);
588 1.1 jmcneill
589 1.1 jmcneill return 0;
590 1.1 jmcneill }
591 1.1 jmcneill
592 1.1 jmcneill static int
593 1.1 jmcneill sdhost_bus_rod(sdmmc_chipset_handle_t sch, int on)
594 1.1 jmcneill {
595 1.1 jmcneill return -1;
596 1.1 jmcneill }
597 1.1 jmcneill
598 1.1 jmcneill static void
599 1.1 jmcneill sdhost_exec_command(sdmmc_chipset_handle_t sch, struct sdmmc_command *cmd)
600 1.1 jmcneill {
601 1.1 jmcneill struct sdhost_softc * const sc = sch;
602 1.1 jmcneill uint32_t cmdval, hcfg;
603 1.1 jmcneill u_int nblks;
604 1.1 jmcneill
605 1.1 jmcneill #ifdef SDHOST_DEBUG
606 1.1 jmcneill aprint_normal_dev(sc->sc_dev,
607 1.1 jmcneill "opcode %d flags 0x%x data %p datalen %d blklen %d\n",
608 1.1 jmcneill cmd->c_opcode, cmd->c_flags, cmd->c_data, cmd->c_datalen,
609 1.1 jmcneill cmd->c_blklen);
610 1.1 jmcneill #endif
611 1.1 jmcneill
612 1.1 jmcneill mutex_enter(&sc->sc_intr_lock);
613 1.1 jmcneill
614 1.1 jmcneill hcfg = SDHOST_READ(sc, SDHCFG);
615 1.1 jmcneill SDHOST_WRITE(sc, SDHCFG, hcfg | SDHCFG_BUSY_EN);
616 1.1 jmcneill
617 1.1 jmcneill sc->sc_intr_hsts = 0;
618 1.1 jmcneill
619 1.1 jmcneill cmd->c_error = sdhost_wait_idle(sc, 5000);
620 1.1 jmcneill if (cmd->c_error != 0) {
621 1.1 jmcneill #ifdef SDHOST_DEBUG
622 1.1 jmcneill device_printf(sc->sc_dev, "device is busy\n");
623 1.1 jmcneill #endif
624 1.1 jmcneill goto done;
625 1.1 jmcneill }
626 1.1 jmcneill
627 1.1 jmcneill cmdval = SDCMD_NEW;
628 1.1 jmcneill if (!ISSET(cmd->c_flags, SCF_RSP_PRESENT))
629 1.1 jmcneill cmdval |= SDCMD_NORESP;
630 1.1 jmcneill if (ISSET(cmd->c_flags, SCF_RSP_136))
631 1.1 jmcneill cmdval |= SDCMD_LONGRESP;
632 1.1 jmcneill if (ISSET(cmd->c_flags, SCF_RSP_BSY))
633 1.1 jmcneill cmdval |= SDCMD_BUSY;
634 1.1 jmcneill
635 1.1 jmcneill if (cmd->c_datalen > 0) {
636 1.1 jmcneill if (ISSET(cmd->c_flags, SCF_CMD_READ))
637 1.1 jmcneill cmdval |= SDCMD_READ;
638 1.1 jmcneill else
639 1.1 jmcneill cmdval |= SDCMD_WRITE;
640 1.1 jmcneill
641 1.1 jmcneill nblks = cmd->c_datalen / cmd->c_blklen;
642 1.1 jmcneill if (nblks == 0 || (cmd->c_datalen % cmd->c_blklen) != 0)
643 1.1 jmcneill ++nblks;
644 1.1 jmcneill
645 1.1 jmcneill SDHOST_WRITE(sc, SDHBCT, cmd->c_blklen);
646 1.1 jmcneill SDHOST_WRITE(sc, SDHBLC, nblks);
647 1.1 jmcneill
648 1.1 jmcneill cmd->c_resid = cmd->c_datalen;
649 1.1 jmcneill cmd->c_error = sdhost_dma_transfer(sc, cmd);
650 1.1 jmcneill if (cmd->c_error != 0) {
651 1.1 jmcneill #ifdef SDHOST_DEBUG
652 1.1 jmcneill device_printf(sc->sc_dev, "dma transfer failed: %d\n",
653 1.1 jmcneill cmd->c_error);
654 1.1 jmcneill #endif
655 1.1 jmcneill goto done;
656 1.1 jmcneill }
657 1.1 jmcneill }
658 1.1 jmcneill
659 1.1 jmcneill SDHOST_WRITE(sc, SDARG, cmd->c_arg);
660 1.1 jmcneill SDHOST_WRITE(sc, SDCMD, cmdval | cmd->c_opcode);
661 1.1 jmcneill
662 1.1 jmcneill if (cmd->c_datalen > 0) {
663 1.1 jmcneill cmd->c_error = sdhost_dma_wait(sc, cmd);
664 1.1 jmcneill if (cmd->c_error != 0) {
665 1.1 jmcneill #ifdef SDHOST_DEBUG
666 1.1 jmcneill device_printf(sc->sc_dev,
667 1.1 jmcneill "wait dma failed: %d\n", cmd->c_error);
668 1.1 jmcneill #endif
669 1.1 jmcneill goto done;
670 1.1 jmcneill }
671 1.1 jmcneill }
672 1.1 jmcneill
673 1.1 jmcneill cmd->c_error = sdhost_wait_idle(sc, 5000);
674 1.1 jmcneill if (cmd->c_error != 0) {
675 1.1 jmcneill #ifdef SDHOST_DEBUG
676 1.1 jmcneill device_printf(sc->sc_dev,
677 1.1 jmcneill "wait cmd idle (%#x) failed: %d\n",
678 1.1 jmcneill SDHOST_READ(sc, SDCMD), cmd->c_error);
679 1.1 jmcneill #endif
680 1.1 jmcneill }
681 1.1 jmcneill
682 1.1 jmcneill if ((SDHOST_READ(sc, SDCMD) & SDCMD_FAIL) != 0) {
683 1.1 jmcneill #ifdef SDHOST_DEBUG
684 1.1 jmcneill device_printf(sc->sc_dev, "SDCMD: %#x\n",
685 1.1 jmcneill SDHOST_READ(sc, SDCMD));
686 1.1 jmcneill #endif
687 1.1 jmcneill cmd->c_error = EIO;
688 1.1 jmcneill goto done;
689 1.1 jmcneill }
690 1.1 jmcneill
691 1.1 jmcneill if (ISSET(cmd->c_flags, SCF_RSP_PRESENT)) {
692 1.1 jmcneill if (ISSET(cmd->c_flags, SCF_RSP_136)) {
693 1.1 jmcneill cmd->c_resp[0] = SDHOST_READ(sc, SDRSP0);
694 1.1 jmcneill cmd->c_resp[1] = SDHOST_READ(sc, SDRSP1);
695 1.1 jmcneill cmd->c_resp[2] = SDHOST_READ(sc, SDRSP2);
696 1.1 jmcneill cmd->c_resp[3] = SDHOST_READ(sc, SDRSP3);
697 1.1 jmcneill if (ISSET(cmd->c_flags, SCF_RSP_CRC)) {
698 1.1 jmcneill cmd->c_resp[0] = (cmd->c_resp[0] >> 8) |
699 1.1 jmcneill (cmd->c_resp[1] << 24);
700 1.1 jmcneill cmd->c_resp[1] = (cmd->c_resp[1] >> 8) |
701 1.1 jmcneill (cmd->c_resp[2] << 24);
702 1.1 jmcneill cmd->c_resp[2] = (cmd->c_resp[2] >> 8) |
703 1.1 jmcneill (cmd->c_resp[3] << 24);
704 1.1 jmcneill cmd->c_resp[3] = (cmd->c_resp[3] >> 8);
705 1.1 jmcneill }
706 1.1 jmcneill } else {
707 1.1 jmcneill cmd->c_resp[0] = SDHOST_READ(sc, SDRSP0);
708 1.1 jmcneill }
709 1.1 jmcneill }
710 1.1 jmcneill
711 1.1 jmcneill done:
712 1.1 jmcneill cmd->c_flags |= SCF_ITSDONE;
713 1.1 jmcneill SDHOST_WRITE(sc, SDHCFG, hcfg);
714 1.1 jmcneill SDHOST_WRITE(sc, SDHSTS, SDHOST_READ(sc, SDHSTS));
715 1.1 jmcneill mutex_exit(&sc->sc_intr_lock);
716 1.1 jmcneill
717 1.1 jmcneill #ifdef SDHOST_DEBUG
718 1.1 jmcneill if (cmd->c_error != 0)
719 1.1 jmcneill device_printf(sc->sc_dev, "command failed with error %d\n",
720 1.1 jmcneill cmd->c_error);
721 1.1 jmcneill #endif
722 1.1 jmcneill }
723 1.1 jmcneill
724 1.1 jmcneill static void
725 1.1 jmcneill sdhost_card_enable_intr(sdmmc_chipset_handle_t sch, int enable)
726 1.1 jmcneill {
727 1.1 jmcneill }
728 1.1 jmcneill
729 1.1 jmcneill static void
730 1.1 jmcneill sdhost_card_intr_ack(sdmmc_chipset_handle_t sch)
731 1.1 jmcneill {
732 1.1 jmcneill }
733