xref: /freebsd/sys/arm/broadcom/bcm2835/bcm2835_sdhci.c (revision d1488cd762bee79240a715bf04f3d30e9a121b26)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  */
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/conf.h>
34 #include <sys/kernel.h>
35 #include <sys/lock.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/mutex.h>
39 #include <sys/rman.h>
40 #include <sys/sysctl.h>
41 #include <sys/taskqueue.h>
42 
43 #include <machine/bus.h>
44 
45 #include <dev/ofw/ofw_bus.h>
46 #include <dev/ofw/ofw_bus_subr.h>
47 
48 #include <dev/mmc/bridge.h>
49 #include <dev/mmc/mmcreg.h>
50 #include <dev/mmc/mmc_fdt_helpers.h>
51 
52 #include <dev/sdhci/sdhci.h>
53 
54 #include "mmcbr_if.h"
55 #include "sdhci_if.h"
56 
57 #include "opt_mmccam.h"
58 
59 #include "bcm2835_dma.h"
60 #include <arm/broadcom/bcm2835/bcm2835_mbox_prop.h>
61 #ifdef NOTYET
62 #include <arm/broadcom/bcm2835/bcm2835_clkman.h>
63 #endif
64 #include <arm/broadcom/bcm2835/bcm2835_vcbus.h>
65 
66 #define	BCM2835_DEFAULT_SDHCI_FREQ	50
67 #define	BCM2838_DEFAULT_SDHCI_FREQ	100
68 
69 #define	BCM_SDHCI_BUFFER_SIZE		512
70 /*
71  * NUM_DMA_SEGS is the number of DMA segments we want to accommodate on average.
72  * We add in a number of segments based on how much we may need to spill into
73  * another segment due to crossing page boundaries.  e.g. up to PAGE_SIZE, an
74  * extra page is needed as we can cross a page boundary exactly once.
75  */
76 #define	NUM_DMA_SEGS			1
77 #define	NUM_DMA_SPILL_SEGS		\
78 	((((NUM_DMA_SEGS * BCM_SDHCI_BUFFER_SIZE) - 1) / PAGE_SIZE) + 1)
79 #define	ALLOCATED_DMA_SEGS		(NUM_DMA_SEGS +	NUM_DMA_SPILL_SEGS)
80 #define	BCM_DMA_MAXSIZE			(NUM_DMA_SEGS * BCM_SDHCI_BUFFER_SIZE)
81 
82 #define	BCM_SDHCI_SLOT_LEFT(slot)	\
83 	((slot)->curcmd->data->len - (slot)->offset)
84 
85 #define	BCM_SDHCI_SEGSZ_LEFT(slot)	\
86 	min(BCM_DMA_MAXSIZE,		\
87 	    rounddown(BCM_SDHCI_SLOT_LEFT(slot), BCM_SDHCI_BUFFER_SIZE))
88 
89 #define	DATA_PENDING_MASK	(SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)
90 #define	DATA_XFER_MASK		(DATA_PENDING_MASK | SDHCI_INT_DATA_END)
91 
92 #ifdef DEBUG
93 static int bcm2835_sdhci_debug = 0;
94 
95 TUNABLE_INT("hw.bcm2835.sdhci.debug", &bcm2835_sdhci_debug);
96 SYSCTL_INT(_hw_sdhci, OID_AUTO, bcm2835_sdhci_debug, CTLFLAG_RWTUN,
97     &bcm2835_sdhci_debug, 0, "bcm2835 SDHCI debug level");
98 
99 #define	dprintf(fmt, args...)					\
100 	do {							\
101 		if (bcm2835_sdhci_debug)			\
102 			printf("%s: " fmt, __func__, ##args);	\
103 	}  while (0)
104 #else
105 #define dprintf(fmt, args...)
106 #endif
107 
108 static int bcm2835_sdhci_hs = 1;
109 static int bcm2835_sdhci_pio_mode = 0;
110 
111 struct bcm_mmc_conf {
112 	int	clock_id;
113 	int	clock_src;
114 	int	default_freq;
115 	int	quirks;
116 	int	emmc_dreq;
117 };
118 
119 struct bcm_mmc_conf bcm2835_sdhci_conf = {
120 	.clock_id	= BCM2835_MBOX_CLOCK_ID_EMMC,
121 	.clock_src	= -1,
122 	.default_freq	= BCM2835_DEFAULT_SDHCI_FREQ,
123 	.quirks		= SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
124 	    SDHCI_QUIRK_BROKEN_TIMEOUT_VAL | SDHCI_QUIRK_DONT_SET_HISPD_BIT |
125 	    SDHCI_QUIRK_MISSING_CAPS,
126 	.emmc_dreq	= BCM_DMA_DREQ_EMMC,
127 };
128 
129 struct bcm_mmc_conf bcm2838_emmc2_conf = {
130 	.clock_id	= BCM2838_MBOX_CLOCK_ID_EMMC2,
131 	.clock_src	= -1,
132 	.default_freq	= BCM2838_DEFAULT_SDHCI_FREQ,
133 	.quirks		= 0,
134 	.emmc_dreq	= BCM_DMA_DREQ_NONE,
135 };
136 
137 static struct ofw_compat_data compat_data[] = {
138 	{"broadcom,bcm2835-sdhci",	(uintptr_t)&bcm2835_sdhci_conf},
139 	{"brcm,bcm2835-sdhci",		(uintptr_t)&bcm2835_sdhci_conf},
140 	{"brcm,bcm2835-mmc",		(uintptr_t)&bcm2835_sdhci_conf},
141 	{"brcm,bcm2711-emmc2",		(uintptr_t)&bcm2838_emmc2_conf},
142 	{"brcm,bcm2838-emmc2",		(uintptr_t)&bcm2838_emmc2_conf},
143 	{NULL,				0}
144 };
145 
146 TUNABLE_INT("hw.bcm2835.sdhci.hs", &bcm2835_sdhci_hs);
147 TUNABLE_INT("hw.bcm2835.sdhci.pio_mode", &bcm2835_sdhci_pio_mode);
148 
149 struct bcm_sdhci_softc {
150 	device_t		sc_dev;
151 	struct resource *	sc_mem_res;
152 	struct resource *	sc_irq_res;
153 	bus_space_tag_t		sc_bst;
154 	bus_space_handle_t	sc_bsh;
155 	void *			sc_intrhand;
156 	struct mmc_request *	sc_req;
157 	struct sdhci_slot	sc_slot;
158 	struct mmc_helper	sc_mmc_helper;
159 	int			sc_dma_ch;
160 	bus_dma_tag_t		sc_dma_tag;
161 	bus_dmamap_t		sc_dma_map;
162 	vm_paddr_t		sc_sdhci_buffer_phys;
163 	bus_addr_t		dmamap_seg_addrs[ALLOCATED_DMA_SEGS];
164 	bus_size_t		dmamap_seg_sizes[ALLOCATED_DMA_SEGS];
165 	int			dmamap_seg_count;
166 	int			dmamap_seg_index;
167 	int			dmamap_status;
168 	uint32_t		blksz_and_count;
169 	uint32_t		cmd_and_mode;
170 	bool			need_update_blk;
171 #ifdef NOTYET
172 	device_t		clkman;
173 #endif
174 	struct bcm_mmc_conf *	conf;
175 };
176 
177 static int bcm_sdhci_probe(device_t);
178 static int bcm_sdhci_attach(device_t);
179 static int bcm_sdhci_detach(device_t);
180 static void bcm_sdhci_intr(void *);
181 
182 static int bcm_sdhci_get_ro(device_t, device_t);
183 static void bcm_sdhci_dma_intr(int ch, void *arg);
184 static void bcm_sdhci_start_dma(struct sdhci_slot *slot);
185 
186 static void
bcm_sdhci_dmacb(void * arg,bus_dma_segment_t * segs,int nseg,int err)187 bcm_sdhci_dmacb(void *arg, bus_dma_segment_t *segs, int nseg, int err)
188 {
189 	struct bcm_sdhci_softc *sc = arg;
190 	int i;
191 
192 	/* Sanity check: we can only ever have one mapping at a time. */
193 	KASSERT(sc->dmamap_seg_count == 0, ("leaked DMA segment"));
194 	sc->dmamap_status = err;
195 	sc->dmamap_seg_count = nseg;
196 
197 	/* Note nseg is guaranteed to be zero if err is non-zero. */
198 	for (i = 0; i < nseg; i++) {
199 		sc->dmamap_seg_addrs[i] = segs[i].ds_addr;
200 		sc->dmamap_seg_sizes[i] = segs[i].ds_len;
201 	}
202 }
203 
204 static int
bcm_sdhci_probe(device_t dev)205 bcm_sdhci_probe(device_t dev)
206 {
207 
208 	if (!ofw_bus_status_okay(dev))
209 		return (ENXIO);
210 
211 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
212 		return (ENXIO);
213 
214 	device_set_desc(dev, "Broadcom 2708 SDHCI controller");
215 
216 	return (BUS_PROBE_DEFAULT);
217 }
218 
219 static int
bcm_sdhci_attach(device_t dev)220 bcm_sdhci_attach(device_t dev)
221 {
222 	struct bcm_sdhci_softc *sc = device_get_softc(dev);
223 	int rid, err;
224 	phandle_t node;
225 	pcell_t cell;
226 	u_int default_freq;
227 
228 	sc->sc_dev = dev;
229 	sc->sc_req = NULL;
230 
231 	sc->conf = (struct bcm_mmc_conf *)ofw_bus_search_compatible(dev,
232 	    compat_data)->ocd_data;
233 	if (sc->conf == 0)
234 	    return (ENXIO);
235 
236 	err = bcm2835_mbox_set_power_state(BCM2835_MBOX_POWER_ID_EMMC, TRUE);
237 	if (err != 0) {
238 		if (bootverbose)
239 			device_printf(dev, "Unable to enable the power\n");
240 		return (err);
241 	}
242 
243 	default_freq = 0;
244 	err = bcm2835_mbox_get_clock_rate(sc->conf->clock_id, &default_freq);
245 	if (err == 0) {
246 		/* Convert to MHz */
247 		default_freq /= 1000000;
248 	}
249 	if (default_freq == 0) {
250 		node = ofw_bus_get_node(sc->sc_dev);
251 		if ((OF_getencprop(node, "clock-frequency", &cell,
252 		    sizeof(cell))) > 0)
253 			default_freq = cell / 1000000;
254 	}
255 	if (default_freq == 0)
256 		default_freq = sc->conf->default_freq;
257 
258 	if (bootverbose)
259 		device_printf(dev, "SDHCI frequency: %dMHz\n", default_freq);
260 #ifdef NOTYET
261 	if (sc->conf->clock_src > 0) {
262 		uint32_t f;
263 		sc->clkman = devclass_get_device(
264 		    devclass_find("bcm2835_clkman"), 0);
265 		if (sc->clkman == NULL) {
266 			device_printf(dev, "cannot find Clock Manager\n");
267 			return (ENXIO);
268 		}
269 
270 		f = bcm2835_clkman_set_frequency(sc->clkman,
271 		    sc->conf->clock_src, default_freq);
272 		if (f == 0)
273 			return (EINVAL);
274 
275 		if (bootverbose)
276 			device_printf(dev, "Clock source frequency: %dMHz\n",
277 			    f);
278 	}
279 #endif
280 
281 	rid = 0;
282 	sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
283 	    RF_ACTIVE);
284 	if (!sc->sc_mem_res) {
285 		device_printf(dev, "cannot allocate memory window\n");
286 		err = ENXIO;
287 		goto fail;
288 	}
289 
290 	sc->sc_bst = rman_get_bustag(sc->sc_mem_res);
291 	sc->sc_bsh = rman_get_bushandle(sc->sc_mem_res);
292 
293 	rid = 0;
294 	sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
295 	    RF_ACTIVE | RF_SHAREABLE);
296 	if (!sc->sc_irq_res) {
297 		device_printf(dev, "cannot allocate interrupt\n");
298 		err = ENXIO;
299 		goto fail;
300 	}
301 
302 	if (bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
303 	    NULL, bcm_sdhci_intr, sc, &sc->sc_intrhand)) {
304 		device_printf(dev, "cannot setup interrupt handler\n");
305 		err = ENXIO;
306 		goto fail;
307 	}
308 
309 	if (!bcm2835_sdhci_pio_mode)
310 		sc->sc_slot.opt = SDHCI_PLATFORM_TRANSFER;
311 
312 	sc->sc_slot.caps = SDHCI_CAN_VDD_330 | SDHCI_CAN_VDD_180;
313 	if (bcm2835_sdhci_hs)
314 		sc->sc_slot.caps |= SDHCI_CAN_DO_HISPD;
315 	sc->sc_slot.caps |= (default_freq << SDHCI_CLOCK_BASE_SHIFT);
316 	sc->sc_slot.quirks = sc->conf->quirks;
317 
318 	sdhci_init_slot(dev, &sc->sc_slot, 0);
319 	mmc_fdt_parse(dev, 0, &sc->sc_mmc_helper, &sc->sc_slot.host);
320 
321 	sc->sc_dma_ch = bcm_dma_allocate(BCM_DMA_CH_ANY);
322 	if (sc->sc_dma_ch == BCM_DMA_CH_INVALID)
323 		goto fail;
324 
325 	err = bcm_dma_setup_intr(sc->sc_dma_ch, bcm_sdhci_dma_intr, sc);
326 	if (err != 0) {
327 		device_printf(dev,
328 		    "cannot setup dma interrupt handler\n");
329 		err = ENXIO;
330 		goto fail;
331 	}
332 
333 	/* Allocate bus_dma resources. */
334 	err = bus_dma_tag_create(bus_get_dma_tag(dev),
335 	    1, 0, bcm283x_dmabus_peripheral_lowaddr(),
336 	    BUS_SPACE_MAXADDR, NULL, NULL,
337 	    BCM_DMA_MAXSIZE, ALLOCATED_DMA_SEGS, BCM_SDHCI_BUFFER_SIZE,
338 	    BUS_DMA_ALLOCNOW, NULL, NULL,
339 	    &sc->sc_dma_tag);
340 
341 	if (err) {
342 		device_printf(dev, "failed allocate DMA tag");
343 		goto fail;
344 	}
345 
346 	err = bus_dmamap_create(sc->sc_dma_tag, 0, &sc->sc_dma_map);
347 	if (err) {
348 		device_printf(dev, "bus_dmamap_create failed\n");
349 		goto fail;
350 	}
351 
352 	/* FIXME: Fix along with other BUS_SPACE_PHYSADDR instances */
353 	sc->sc_sdhci_buffer_phys = rman_get_start(sc->sc_mem_res) +
354 	    SDHCI_BUFFER;
355 
356 	bus_identify_children(dev);
357 	bus_attach_children(dev);
358 
359 	sdhci_start_slot(&sc->sc_slot);
360 
361 	/* Seed our copies. */
362 	sc->blksz_and_count = SDHCI_READ_4(dev, &sc->sc_slot, SDHCI_BLOCK_SIZE);
363 	sc->cmd_and_mode = SDHCI_READ_4(dev, &sc->sc_slot, SDHCI_TRANSFER_MODE);
364 
365 	return (0);
366 
367 fail:
368 	bcm_dma_free(sc->sc_dma_ch);
369 	if (sc->sc_intrhand)
370 		bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_intrhand);
371 	if (sc->sc_irq_res)
372 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
373 	if (sc->sc_mem_res)
374 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
375 
376 	return (err);
377 }
378 
379 static int
bcm_sdhci_detach(device_t dev)380 bcm_sdhci_detach(device_t dev)
381 {
382 
383 	return (EBUSY);
384 }
385 
386 static void
bcm_sdhci_intr(void * arg)387 bcm_sdhci_intr(void *arg)
388 {
389 	struct bcm_sdhci_softc *sc = arg;
390 
391 	sdhci_generic_intr(&sc->sc_slot);
392 }
393 
394 static int
bcm_sdhci_update_ios(device_t bus,device_t child)395 bcm_sdhci_update_ios(device_t bus, device_t child)
396 {
397 	struct bcm_sdhci_softc *sc;
398 	struct mmc_ios *ios;
399 	int rv;
400 
401 	sc = device_get_softc(bus);
402 	ios = &sc->sc_slot.host.ios;
403 
404 	if (ios->power_mode == power_up) {
405 		if (sc->sc_mmc_helper.vmmc_supply)
406 			regulator_enable(sc->sc_mmc_helper.vmmc_supply);
407 		if (sc->sc_mmc_helper.vqmmc_supply)
408 			regulator_enable(sc->sc_mmc_helper.vqmmc_supply);
409 	}
410 
411 	rv = sdhci_generic_update_ios(bus, child);
412 	if (rv != 0)
413 		return (rv);
414 
415 	if (ios->power_mode == power_off) {
416 		if (sc->sc_mmc_helper.vmmc_supply)
417 			regulator_disable(sc->sc_mmc_helper.vmmc_supply);
418 		if (sc->sc_mmc_helper.vqmmc_supply)
419 			regulator_disable(sc->sc_mmc_helper.vqmmc_supply);
420 	}
421 
422 	return (0);
423 }
424 
425 static int
bcm_sdhci_get_ro(device_t bus,device_t child)426 bcm_sdhci_get_ro(device_t bus, device_t child)
427 {
428 
429 	return (0);
430 }
431 
432 static inline uint32_t
RD4(struct bcm_sdhci_softc * sc,bus_size_t off)433 RD4(struct bcm_sdhci_softc *sc, bus_size_t off)
434 {
435 	uint32_t val = bus_space_read_4(sc->sc_bst, sc->sc_bsh, off);
436 	return val;
437 }
438 
439 static inline void
WR4(struct bcm_sdhci_softc * sc,bus_size_t off,uint32_t val)440 WR4(struct bcm_sdhci_softc *sc, bus_size_t off, uint32_t val)
441 {
442 
443 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, off, val);
444 	/*
445 	 * The Arasan HC has a bug where it may lose the content of
446 	 * consecutive writes to registers that are within two SD-card
447 	 * clock cycles of each other (a clock domain crossing problem).
448 	 */
449 	if (sc->sc_slot.clock > 0)
450 		DELAY(((2 * 1000000) / sc->sc_slot.clock) + 1);
451 }
452 
453 static uint8_t
bcm_sdhci_read_1(device_t dev,struct sdhci_slot * slot,bus_size_t off)454 bcm_sdhci_read_1(device_t dev, struct sdhci_slot *slot, bus_size_t off)
455 {
456 	struct bcm_sdhci_softc *sc = device_get_softc(dev);
457 	uint32_t val = RD4(sc, off & ~3);
458 
459 	return ((val >> (off & 3)*8) & 0xff);
460 }
461 
462 static uint16_t
bcm_sdhci_read_2(device_t dev,struct sdhci_slot * slot,bus_size_t off)463 bcm_sdhci_read_2(device_t dev, struct sdhci_slot *slot, bus_size_t off)
464 {
465 	struct bcm_sdhci_softc *sc = device_get_softc(dev);
466 	uint32_t val32;
467 
468 	/*
469 	 * Standard 32-bit handling of command and transfer mode, as
470 	 * well as block size and count.
471 	 */
472 	if ((off == SDHCI_BLOCK_SIZE || off == SDHCI_BLOCK_COUNT) &&
473 	    sc->need_update_blk)
474 		val32 = sc->blksz_and_count;
475 	else if (off == SDHCI_TRANSFER_MODE || off == SDHCI_COMMAND_FLAGS)
476 		val32 = sc->cmd_and_mode;
477 	else
478 		val32 = RD4(sc, off & ~3);
479 
480 	return ((val32 >> (off & 3)*8) & 0xffff);
481 }
482 
483 static uint32_t
bcm_sdhci_read_4(device_t dev,struct sdhci_slot * slot,bus_size_t off)484 bcm_sdhci_read_4(device_t dev, struct sdhci_slot *slot, bus_size_t off)
485 {
486 	struct bcm_sdhci_softc *sc = device_get_softc(dev);
487 
488 	return RD4(sc, off);
489 }
490 
491 static void
bcm_sdhci_read_multi_4(device_t dev,struct sdhci_slot * slot,bus_size_t off,uint32_t * data,bus_size_t count)492 bcm_sdhci_read_multi_4(device_t dev, struct sdhci_slot *slot, bus_size_t off,
493     uint32_t *data, bus_size_t count)
494 {
495 	struct bcm_sdhci_softc *sc = device_get_softc(dev);
496 
497 	bus_space_read_multi_4(sc->sc_bst, sc->sc_bsh, off, data, count);
498 }
499 
500 static void
bcm_sdhci_write_1(device_t dev,struct sdhci_slot * slot,bus_size_t off,uint8_t val)501 bcm_sdhci_write_1(device_t dev, struct sdhci_slot *slot, bus_size_t off,
502     uint8_t val)
503 {
504 	struct bcm_sdhci_softc *sc = device_get_softc(dev);
505 	uint32_t val32 = RD4(sc, off & ~3);
506 	val32 &= ~(0xff << (off & 3)*8);
507 	val32 |= (val << (off & 3)*8);
508 	WR4(sc, off & ~3, val32);
509 }
510 
511 static void
bcm_sdhci_write_2(device_t dev,struct sdhci_slot * slot,bus_size_t off,uint16_t val)512 bcm_sdhci_write_2(device_t dev, struct sdhci_slot *slot, bus_size_t off,
513     uint16_t val)
514 {
515 	struct bcm_sdhci_softc *sc = device_get_softc(dev);
516 	uint32_t val32;
517 
518 	/*
519 	 * If we have a queued up 16bit value for blk size or count, use and
520 	 * update the saved value rather than doing any real register access.
521 	 * If we did not touch either since the last write, then read from
522 	 * register as at least block count can change.
523 	 * Similarly, if we are about to issue a command, always use the saved
524 	 * value for transfer mode as we can never write that without issuing
525 	 * a command.
526 	 */
527 	if ((off == SDHCI_BLOCK_SIZE || off == SDHCI_BLOCK_COUNT) &&
528 	    sc->need_update_blk)
529 		val32 = sc->blksz_and_count;
530 	else if (off == SDHCI_COMMAND_FLAGS)
531 		val32 = sc->cmd_and_mode;
532 	else
533 		val32 = RD4(sc, off & ~3);
534 
535 	val32 &= ~(0xffff << (off & 3)*8);
536 	val32 |= (val << (off & 3)*8);
537 
538 	if (off == SDHCI_TRANSFER_MODE)
539 		sc->cmd_and_mode = val32;
540 	else if (off == SDHCI_BLOCK_SIZE || off == SDHCI_BLOCK_COUNT) {
541 		sc->blksz_and_count = val32;
542 		sc->need_update_blk = true;
543 	} else {
544 		if (off == SDHCI_COMMAND_FLAGS) {
545 			/* If we saved blk writes, do them now before cmd. */
546 			if (sc->need_update_blk) {
547 				WR4(sc, SDHCI_BLOCK_SIZE, sc->blksz_and_count);
548 				sc->need_update_blk = false;
549 			}
550 			/* Always save cmd and mode registers. */
551 			sc->cmd_and_mode = val32;
552 		}
553 		WR4(sc, off & ~3, val32);
554 	}
555 }
556 
557 static void
bcm_sdhci_write_4(device_t dev,struct sdhci_slot * slot,bus_size_t off,uint32_t val)558 bcm_sdhci_write_4(device_t dev, struct sdhci_slot *slot, bus_size_t off,
559     uint32_t val)
560 {
561 	struct bcm_sdhci_softc *sc = device_get_softc(dev);
562 	WR4(sc, off, val);
563 }
564 
565 static void
bcm_sdhci_write_multi_4(device_t dev,struct sdhci_slot * slot,bus_size_t off,uint32_t * data,bus_size_t count)566 bcm_sdhci_write_multi_4(device_t dev, struct sdhci_slot *slot, bus_size_t off,
567     uint32_t *data, bus_size_t count)
568 {
569 	struct bcm_sdhci_softc *sc = device_get_softc(dev);
570 
571 	bus_space_write_multi_4(sc->sc_bst, sc->sc_bsh, off, data, count);
572 }
573 
574 static void
bcm_sdhci_start_dma_seg(struct bcm_sdhci_softc * sc)575 bcm_sdhci_start_dma_seg(struct bcm_sdhci_softc *sc)
576 {
577 	struct sdhci_slot *slot;
578 	vm_paddr_t pdst, psrc;
579 	int err __diagused, idx, len, sync_op, width;
580 
581 	slot = &sc->sc_slot;
582 	mtx_assert(&slot->mtx, MA_OWNED);
583 	idx = sc->dmamap_seg_index++;
584 	len = sc->dmamap_seg_sizes[idx];
585 	slot->offset += len;
586 	width = (len & 0xf ? BCM_DMA_32BIT : BCM_DMA_128BIT);
587 
588 	if (slot->curcmd->data->flags & MMC_DATA_READ) {
589 		/*
590 		 * Peripherals on the AXI bus do not need DREQ pacing for reads
591 		 * from the ARM core, so we can safely set this to NONE.
592 		 */
593 		bcm_dma_setup_src(sc->sc_dma_ch, BCM_DMA_DREQ_NONE,
594 		    BCM_DMA_SAME_ADDR, BCM_DMA_32BIT);
595 		bcm_dma_setup_dst(sc->sc_dma_ch, BCM_DMA_DREQ_NONE,
596 		    BCM_DMA_INC_ADDR, width);
597 		psrc = sc->sc_sdhci_buffer_phys;
598 		pdst = sc->dmamap_seg_addrs[idx];
599 		sync_op = BUS_DMASYNC_PREREAD;
600 	} else {
601 		/*
602 		 * The ordering here is important, because the last write to
603 		 * dst/src in the dma control block writes the real dreq value.
604 		 */
605 		bcm_dma_setup_src(sc->sc_dma_ch, BCM_DMA_DREQ_NONE,
606 		    BCM_DMA_INC_ADDR, width);
607 		bcm_dma_setup_dst(sc->sc_dma_ch, sc->conf->emmc_dreq,
608 		    BCM_DMA_SAME_ADDR, BCM_DMA_32BIT);
609 		psrc = sc->dmamap_seg_addrs[idx];
610 		pdst = sc->sc_sdhci_buffer_phys;
611 		sync_op = BUS_DMASYNC_PREWRITE;
612 	}
613 
614 	/*
615 	 * When starting a new DMA operation do the busdma sync operation, and
616 	 * disable SDCHI data interrrupts because we'll be driven by DMA
617 	 * interrupts (or SDHCI error interrupts) until the IO is done.
618 	 */
619 	if (idx == 0) {
620 		bus_dmamap_sync(sc->sc_dma_tag, sc->sc_dma_map, sync_op);
621 
622 		slot->intmask &= ~DATA_XFER_MASK;
623 		bcm_sdhci_write_4(sc->sc_dev, slot, SDHCI_SIGNAL_ENABLE,
624 		    slot->intmask);
625 	}
626 
627 	/*
628 	 * Start the DMA transfer.  Only programming errors (like failing to
629 	 * allocate a channel) cause a non-zero return from bcm_dma_start().
630 	 */
631 	err = bcm_dma_start(sc->sc_dma_ch, psrc, pdst, len);
632 	KASSERT((err == 0), ("bcm2835_sdhci: failed DMA start"));
633 }
634 
635 static void
bcm_sdhci_dma_exit(struct bcm_sdhci_softc * sc)636 bcm_sdhci_dma_exit(struct bcm_sdhci_softc *sc)
637 {
638 	struct sdhci_slot *slot = &sc->sc_slot;
639 
640 	mtx_assert(&slot->mtx, MA_OWNED);
641 
642 	/* Re-enable interrupts */
643 	slot->intmask |= DATA_XFER_MASK;
644 	bcm_sdhci_write_4(slot->bus, slot, SDHCI_SIGNAL_ENABLE,
645 	    slot->intmask);
646 }
647 
648 static void
bcm_sdhci_dma_unload(struct bcm_sdhci_softc * sc)649 bcm_sdhci_dma_unload(struct bcm_sdhci_softc *sc)
650 {
651 	struct sdhci_slot *slot = &sc->sc_slot;
652 
653 	if (sc->dmamap_seg_count == 0)
654 		return;
655 	if ((slot->curcmd->data->flags & MMC_DATA_READ) != 0)
656 		bus_dmamap_sync(sc->sc_dma_tag, sc->sc_dma_map,
657 		    BUS_DMASYNC_POSTREAD);
658 	else
659 		bus_dmamap_sync(sc->sc_dma_tag, sc->sc_dma_map,
660 		    BUS_DMASYNC_POSTWRITE);
661 	bus_dmamap_unload(sc->sc_dma_tag, sc->sc_dma_map);
662 
663 	sc->dmamap_seg_count = 0;
664 	sc->dmamap_seg_index = 0;
665 }
666 
667 static void
bcm_sdhci_dma_intr(int ch,void * arg)668 bcm_sdhci_dma_intr(int ch, void *arg)
669 {
670 	struct bcm_sdhci_softc *sc = (struct bcm_sdhci_softc *)arg;
671 	struct sdhci_slot *slot = &sc->sc_slot;
672 	uint32_t reg;
673 
674 	mtx_lock(&slot->mtx);
675 	if (slot->curcmd == NULL)
676 		goto out;
677 	/*
678 	 * If there are more segments for the current dma, start the next one.
679 	 * Otherwise unload the dma map and decide what to do next based on the
680 	 * status of the sdhci controller and whether there's more data left.
681 	 */
682 	if (sc->dmamap_seg_index < sc->dmamap_seg_count) {
683 		bcm_sdhci_start_dma_seg(sc);
684 		goto out;
685 	}
686 
687 	bcm_sdhci_dma_unload(sc);
688 
689 	/*
690 	 * If we had no further segments pending, we need to determine how to
691 	 * proceed next.  If the 'data/space pending' bit is already set and we
692 	 * can continue via DMA, do so.  Otherwise, re-enable interrupts and
693 	 * return.
694 	 */
695 	reg = bcm_sdhci_read_4(slot->bus, slot, SDHCI_INT_STATUS) &
696 	    DATA_XFER_MASK;
697 	if ((reg & DATA_PENDING_MASK) != 0 &&
698 	    BCM_SDHCI_SEGSZ_LEFT(slot) >= BCM_SDHCI_BUFFER_SIZE) {
699 		/* ACK any pending interrupts */
700 		bcm_sdhci_write_4(slot->bus, slot, SDHCI_INT_STATUS,
701 		    DATA_PENDING_MASK);
702 
703 		bcm_sdhci_start_dma(slot);
704 		if (slot->curcmd->error != 0) {
705 			/* We won't recover from this error for this command. */
706 			bcm_sdhci_dma_unload(sc);
707 			bcm_sdhci_dma_exit(sc);
708 			sdhci_finish_data(slot);
709 		}
710 	} else if ((reg & SDHCI_INT_DATA_END) != 0) {
711 		bcm_sdhci_dma_exit(sc);
712 		bcm_sdhci_write_4(slot->bus, slot, SDHCI_INT_STATUS,
713 		    reg);
714 		slot->flags &= ~PLATFORM_DATA_STARTED;
715 		sdhci_finish_data(slot);
716 	} else {
717 		bcm_sdhci_dma_exit(sc);
718 	}
719 out:
720 	mtx_unlock(&slot->mtx);
721 }
722 
723 static void
bcm_sdhci_start_dma(struct sdhci_slot * slot)724 bcm_sdhci_start_dma(struct sdhci_slot *slot)
725 {
726 	struct bcm_sdhci_softc *sc = device_get_softc(slot->bus);
727 	uint8_t *buf;
728 	size_t left;
729 
730 	mtx_assert(&slot->mtx, MA_OWNED);
731 
732 	left = BCM_SDHCI_SEGSZ_LEFT(slot);
733 	buf = (uint8_t *)slot->curcmd->data->data + slot->offset;
734 	KASSERT(left != 0,
735 	    ("%s: DMA handling incorrectly indicated", __func__));
736 
737 	/*
738 	 * No need to check segment count here; if we've not yet unloaded
739 	 * previous segments, we'll catch that in bcm_sdhci_dmacb.
740 	 */
741 	if (bus_dmamap_load(sc->sc_dma_tag, sc->sc_dma_map, buf, left,
742 	    bcm_sdhci_dmacb, sc, BUS_DMA_NOWAIT) != 0 ||
743 	    sc->dmamap_status != 0) {
744 		slot->curcmd->error = MMC_ERR_NO_MEMORY;
745 		return;
746 	}
747 
748 	/* DMA start */
749 	bcm_sdhci_start_dma_seg(sc);
750 }
751 
752 static int
bcm_sdhci_will_handle_transfer(device_t dev,struct sdhci_slot * slot)753 bcm_sdhci_will_handle_transfer(device_t dev, struct sdhci_slot *slot)
754 {
755 #ifdef INVARIANTS
756 	struct bcm_sdhci_softc *sc = device_get_softc(slot->bus);
757 #endif
758 
759 	/*
760 	 * We don't want to perform DMA in this context -- interrupts are
761 	 * disabled, and a transaction may already be in progress.
762 	 */
763 	if (dumping)
764 		return (0);
765 
766 	/*
767 	 * This indicates that we somehow let a data interrupt slip by into the
768 	 * SDHCI framework, when it should not have.  This really needs to be
769 	 * caught and fixed ASAP, as it really shouldn't happen.
770 	 */
771 	KASSERT(sc->dmamap_seg_count == 0,
772 	    ("data pending interrupt pushed through SDHCI framework"));
773 
774 	/*
775 	 * Do not use DMA for transfers less than our block size.  Checking
776 	 * alignment serves little benefit, as we round transfer sizes down to
777 	 * a multiple of the block size and push the transfer back to
778 	 * SDHCI-driven PIO once we're below the block size.
779 	 */
780 	if (BCM_SDHCI_SEGSZ_LEFT(slot) < BCM_DMA_BLOCK_SIZE)
781 		return (0);
782 
783 	return (1);
784 }
785 
786 static void
bcm_sdhci_start_transfer(device_t dev,struct sdhci_slot * slot,uint32_t * intmask)787 bcm_sdhci_start_transfer(device_t dev, struct sdhci_slot *slot,
788     uint32_t *intmask)
789 {
790 
791 	/* DMA transfer FIFO 1KB */
792 	bcm_sdhci_start_dma(slot);
793 }
794 
795 static void
bcm_sdhci_finish_transfer(device_t dev,struct sdhci_slot * slot)796 bcm_sdhci_finish_transfer(device_t dev, struct sdhci_slot *slot)
797 {
798 	struct bcm_sdhci_softc *sc = device_get_softc(slot->bus);
799 
800 	/*
801 	 * Clean up.  Interrupts are clearly enabled, because we received an
802 	 * SDHCI_INT_DATA_END to get this far -- just make sure we don't leave
803 	 * anything laying around.
804 	 */
805 	if (sc->dmamap_seg_count != 0) {
806 		/*
807 		 * Our segment math should have worked out such that we would
808 		 * never finish the transfer without having used up all of the
809 		 * segments.  If we haven't, that means we must have erroneously
810 		 * regressed to SDHCI-driven PIO to finish the operation and
811 		 * this is certainly caused by developer-error.
812 		 */
813 		bcm_sdhci_dma_unload(sc);
814 	}
815 
816 	sdhci_finish_data(slot);
817 }
818 
819 static device_method_t bcm_sdhci_methods[] = {
820 	/* Device interface */
821 	DEVMETHOD(device_probe,		bcm_sdhci_probe),
822 	DEVMETHOD(device_attach,	bcm_sdhci_attach),
823 	DEVMETHOD(device_detach,	bcm_sdhci_detach),
824 
825 	/* Bus interface */
826 	DEVMETHOD(bus_read_ivar,	sdhci_generic_read_ivar),
827 	DEVMETHOD(bus_write_ivar,	sdhci_generic_write_ivar),
828 	DEVMETHOD(bus_add_child,	bus_generic_add_child),
829 
830 	/* MMC bridge interface */
831 	DEVMETHOD(mmcbr_update_ios,	bcm_sdhci_update_ios),
832 	DEVMETHOD(mmcbr_request,	sdhci_generic_request),
833 	DEVMETHOD(mmcbr_get_ro,		bcm_sdhci_get_ro),
834 	DEVMETHOD(mmcbr_acquire_host,	sdhci_generic_acquire_host),
835 	DEVMETHOD(mmcbr_release_host,	sdhci_generic_release_host),
836 
837 	/* Platform transfer methods */
838 	DEVMETHOD(sdhci_platform_will_handle,		bcm_sdhci_will_handle_transfer),
839 	DEVMETHOD(sdhci_platform_start_transfer,	bcm_sdhci_start_transfer),
840 	DEVMETHOD(sdhci_platform_finish_transfer,	bcm_sdhci_finish_transfer),
841 	/* SDHCI registers accessors */
842 	DEVMETHOD(sdhci_read_1,		bcm_sdhci_read_1),
843 	DEVMETHOD(sdhci_read_2,		bcm_sdhci_read_2),
844 	DEVMETHOD(sdhci_read_4,		bcm_sdhci_read_4),
845 	DEVMETHOD(sdhci_read_multi_4,	bcm_sdhci_read_multi_4),
846 	DEVMETHOD(sdhci_write_1,	bcm_sdhci_write_1),
847 	DEVMETHOD(sdhci_write_2,	bcm_sdhci_write_2),
848 	DEVMETHOD(sdhci_write_4,	bcm_sdhci_write_4),
849 	DEVMETHOD(sdhci_write_multi_4,	bcm_sdhci_write_multi_4),
850 
851 	DEVMETHOD_END
852 };
853 
854 static driver_t bcm_sdhci_driver = {
855 	"sdhci_bcm",
856 	bcm_sdhci_methods,
857 	sizeof(struct bcm_sdhci_softc),
858 };
859 
860 DRIVER_MODULE(sdhci_bcm, simplebus, bcm_sdhci_driver, NULL, NULL);
861 #ifdef NOTYET
862 MODULE_DEPEND(sdhci_bcm, bcm2835_clkman, 1, 1, 1);
863 #endif
864 SDHCI_DEPEND(sdhci_bcm);
865 #ifndef MMCCAM
866 MMC_DECLARE_BRIDGE(sdhci_bcm);
867 #endif
868