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