1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD: stable/12/sys/arm/broadcom/bcm2835/bcm2835_sdhci.c 370678 2021-09-28 14:25:03Z mhorne $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/conf.h>
36 #include <sys/kernel.h>
37 #include <sys/lock.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/rman.h>
42 #include <sys/sysctl.h>
43 #include <sys/taskqueue.h>
44
45 #include <machine/bus.h>
46
47 #include <dev/ofw/ofw_bus.h>
48 #include <dev/ofw/ofw_bus_subr.h>
49
50 #include <dev/mmc/bridge.h>
51 #include <dev/mmc/mmcreg.h>
52
53 #include <dev/sdhci/sdhci.h>
54
55 #include "mmcbr_if.h"
56 #include "sdhci_if.h"
57
58 #include "opt_mmccam.h"
59
60 #include "bcm2835_dma.h"
61 #include <arm/broadcom/bcm2835/bcm2835_mbox_prop.h>
62 #ifdef NOTYET
63 #include <arm/broadcom/bcm2835/bcm2835_clkman.h>
64 #endif
65 #include <arm/broadcom/bcm2835/bcm2835_vcbus.h>
66
67 #define BCM2835_DEFAULT_SDHCI_FREQ 50
68 #define BCM2838_DEFAULT_SDHCI_FREQ 100
69
70 #define BCM_SDHCI_BUFFER_SIZE 512
71 /*
72 * NUM_DMA_SEGS is the number of DMA segments we want to accommodate on average.
73 * We add in a number of segments based on how much we may need to spill into
74 * another segment due to crossing page boundaries. e.g. up to PAGE_SIZE, an
75 * extra page is needed as we can cross a page boundary exactly once.
76 */
77 #define NUM_DMA_SEGS 1
78 #define NUM_DMA_SPILL_SEGS \
79 ((((NUM_DMA_SEGS * BCM_SDHCI_BUFFER_SIZE) - 1) / PAGE_SIZE) + 1)
80 #define ALLOCATED_DMA_SEGS (NUM_DMA_SEGS + NUM_DMA_SPILL_SEGS)
81 #define BCM_DMA_MAXSIZE (NUM_DMA_SEGS * BCM_SDHCI_BUFFER_SIZE)
82
83 #define BCM_SDHCI_SLOT_LEFT(slot) \
84 ((slot)->curcmd->data->len - (slot)->offset)
85
86 #define BCM_SDHCI_SEGSZ_LEFT(slot) \
87 min(BCM_DMA_MAXSIZE, \
88 rounddown(BCM_SDHCI_SLOT_LEFT(slot), BCM_SDHCI_BUFFER_SIZE))
89
90 #define DATA_PENDING_MASK (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)
91 #define DATA_XFER_MASK (DATA_PENDING_MASK | SDHCI_INT_DATA_END)
92
93 #ifdef DEBUG
94 static int bcm2835_sdhci_debug = 0;
95
96 TUNABLE_INT("hw.bcm2835.sdhci.debug", &bcm2835_sdhci_debug);
97 SYSCTL_INT(_hw_sdhci, OID_AUTO, bcm2835_sdhci_debug, CTLFLAG_RWTUN,
98 &bcm2835_sdhci_debug, 0, "bcm2835 SDHCI debug level");
99
100 #define dprintf(fmt, args...) \
101 do { \
102 if (bcm2835_sdhci_debug) \
103 printf("%s: " fmt, __func__, ##args); \
104 } while (0)
105 #else
106 #define dprintf(fmt, args...)
107 #endif
108
109 static int bcm2835_sdhci_hs = 1;
110 static int bcm2835_sdhci_pio_mode = 0;
111
112 struct bcm_mmc_conf {
113 int clock_id;
114 int clock_src;
115 int default_freq;
116 int quirks;
117 int emmc_dreq;
118 };
119
120 struct bcm_mmc_conf bcm2835_sdhci_conf = {
121 .clock_id = BCM2835_MBOX_CLOCK_ID_EMMC,
122 .clock_src = -1,
123 .default_freq = BCM2835_DEFAULT_SDHCI_FREQ,
124 .quirks = SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
125 SDHCI_QUIRK_BROKEN_TIMEOUT_VAL | SDHCI_QUIRK_DONT_SET_HISPD_BIT |
126 SDHCI_QUIRK_MISSING_CAPS,
127 .emmc_dreq = BCM_DMA_DREQ_EMMC,
128 };
129
130 struct bcm_mmc_conf bcm2838_emmc2_conf = {
131 .clock_id = BCM2838_MBOX_CLOCK_ID_EMMC2,
132 .clock_src = -1,
133 .default_freq = BCM2838_DEFAULT_SDHCI_FREQ,
134 .quirks = 0,
135 .emmc_dreq = BCM_DMA_DREQ_NONE,
136 };
137
138 static struct ofw_compat_data compat_data[] = {
139 {"broadcom,bcm2835-sdhci", (uintptr_t)&bcm2835_sdhci_conf},
140 {"brcm,bcm2835-sdhci", (uintptr_t)&bcm2835_sdhci_conf},
141 {"brcm,bcm2835-mmc", (uintptr_t)&bcm2835_sdhci_conf},
142 {"brcm,bcm2711-emmc2", (uintptr_t)&bcm2838_emmc2_conf},
143 {"brcm,bcm2838-emmc2", (uintptr_t)&bcm2838_emmc2_conf},
144 {NULL, 0}
145 };
146
147 TUNABLE_INT("hw.bcm2835.sdhci.hs", &bcm2835_sdhci_hs);
148 TUNABLE_INT("hw.bcm2835.sdhci.pio_mode", &bcm2835_sdhci_pio_mode);
149
150 struct bcm_sdhci_softc {
151 device_t sc_dev;
152 struct resource * sc_mem_res;
153 struct resource * sc_irq_res;
154 bus_space_tag_t sc_bst;
155 bus_space_handle_t sc_bsh;
156 void * sc_intrhand;
157 struct mmc_request * sc_req;
158 struct sdhci_slot sc_slot;
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
320 sc->sc_dma_ch = bcm_dma_allocate(BCM_DMA_CH_ANY);
321 if (sc->sc_dma_ch == BCM_DMA_CH_INVALID)
322 goto fail;
323
324 err = bcm_dma_setup_intr(sc->sc_dma_ch, bcm_sdhci_dma_intr, sc);
325 if (err != 0) {
326 device_printf(dev,
327 "cannot setup dma interrupt handler\n");
328 err = ENXIO;
329 goto fail;
330 }
331
332 /* Allocate bus_dma resources. */
333 err = bus_dma_tag_create(bus_get_dma_tag(dev),
334 1, 0, bcm283x_dmabus_peripheral_lowaddr(),
335 BUS_SPACE_MAXADDR, NULL, NULL,
336 BCM_DMA_MAXSIZE, ALLOCATED_DMA_SEGS, BCM_SDHCI_BUFFER_SIZE,
337 BUS_DMA_ALLOCNOW, NULL, NULL,
338 &sc->sc_dma_tag);
339
340 if (err) {
341 device_printf(dev, "failed allocate DMA tag");
342 goto fail;
343 }
344
345 err = bus_dmamap_create(sc->sc_dma_tag, 0, &sc->sc_dma_map);
346 if (err) {
347 device_printf(dev, "bus_dmamap_create failed\n");
348 goto fail;
349 }
350
351 /* FIXME: Fix along with other BUS_SPACE_PHYSADDR instances */
352 sc->sc_sdhci_buffer_phys = rman_get_start(sc->sc_mem_res) +
353 SDHCI_BUFFER;
354
355 bus_generic_probe(dev);
356 bus_generic_attach(dev);
357
358 sdhci_start_slot(&sc->sc_slot);
359
360 /* Seed our copies. */
361 sc->blksz_and_count = SDHCI_READ_4(dev, &sc->sc_slot, SDHCI_BLOCK_SIZE);
362 sc->cmd_and_mode = SDHCI_READ_4(dev, &sc->sc_slot, SDHCI_TRANSFER_MODE);
363
364 return (0);
365
366 fail:
367 if (sc->sc_intrhand)
368 bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_intrhand);
369 if (sc->sc_irq_res)
370 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
371 if (sc->sc_mem_res)
372 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
373
374 return (err);
375 }
376
377 static int
bcm_sdhci_detach(device_t dev)378 bcm_sdhci_detach(device_t dev)
379 {
380
381 return (EBUSY);
382 }
383
384 static void
bcm_sdhci_intr(void * arg)385 bcm_sdhci_intr(void *arg)
386 {
387 struct bcm_sdhci_softc *sc = arg;
388
389 sdhci_generic_intr(&sc->sc_slot);
390 }
391
392 static int
bcm_sdhci_get_ro(device_t bus,device_t child)393 bcm_sdhci_get_ro(device_t bus, device_t child)
394 {
395
396 return (0);
397 }
398
399 static inline uint32_t
RD4(struct bcm_sdhci_softc * sc,bus_size_t off)400 RD4(struct bcm_sdhci_softc *sc, bus_size_t off)
401 {
402 uint32_t val = bus_space_read_4(sc->sc_bst, sc->sc_bsh, off);
403 return val;
404 }
405
406 static inline void
WR4(struct bcm_sdhci_softc * sc,bus_size_t off,uint32_t val)407 WR4(struct bcm_sdhci_softc *sc, bus_size_t off, uint32_t val)
408 {
409
410 bus_space_write_4(sc->sc_bst, sc->sc_bsh, off, val);
411 /*
412 * The Arasan HC has a bug where it may lose the content of
413 * consecutive writes to registers that are within two SD-card
414 * clock cycles of each other (a clock domain crossing problem).
415 */
416 if (sc->sc_slot.clock > 0)
417 DELAY(((2 * 1000000) / sc->sc_slot.clock) + 1);
418 }
419
420 static uint8_t
bcm_sdhci_read_1(device_t dev,struct sdhci_slot * slot,bus_size_t off)421 bcm_sdhci_read_1(device_t dev, struct sdhci_slot *slot, bus_size_t off)
422 {
423 struct bcm_sdhci_softc *sc = device_get_softc(dev);
424 uint32_t val = RD4(sc, off & ~3);
425
426 return ((val >> (off & 3)*8) & 0xff);
427 }
428
429 static uint16_t
bcm_sdhci_read_2(device_t dev,struct sdhci_slot * slot,bus_size_t off)430 bcm_sdhci_read_2(device_t dev, struct sdhci_slot *slot, bus_size_t off)
431 {
432 struct bcm_sdhci_softc *sc = device_get_softc(dev);
433 uint32_t val32;
434
435 /*
436 * Standard 32-bit handling of command and transfer mode, as
437 * well as block size and count.
438 */
439 if ((off == SDHCI_BLOCK_SIZE || off == SDHCI_BLOCK_COUNT) &&
440 sc->need_update_blk)
441 val32 = sc->blksz_and_count;
442 else if (off == SDHCI_TRANSFER_MODE || off == SDHCI_COMMAND_FLAGS)
443 val32 = sc->cmd_and_mode;
444 else
445 val32 = RD4(sc, off & ~3);
446
447 return ((val32 >> (off & 3)*8) & 0xffff);
448 }
449
450 static uint32_t
bcm_sdhci_read_4(device_t dev,struct sdhci_slot * slot,bus_size_t off)451 bcm_sdhci_read_4(device_t dev, struct sdhci_slot *slot, bus_size_t off)
452 {
453 struct bcm_sdhci_softc *sc = device_get_softc(dev);
454
455 return RD4(sc, off);
456 }
457
458 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)459 bcm_sdhci_read_multi_4(device_t dev, struct sdhci_slot *slot, bus_size_t off,
460 uint32_t *data, bus_size_t count)
461 {
462 struct bcm_sdhci_softc *sc = device_get_softc(dev);
463
464 bus_space_read_multi_4(sc->sc_bst, sc->sc_bsh, off, data, count);
465 }
466
467 static void
bcm_sdhci_write_1(device_t dev,struct sdhci_slot * slot,bus_size_t off,uint8_t val)468 bcm_sdhci_write_1(device_t dev, struct sdhci_slot *slot, bus_size_t off,
469 uint8_t val)
470 {
471 struct bcm_sdhci_softc *sc = device_get_softc(dev);
472 uint32_t val32 = RD4(sc, off & ~3);
473 val32 &= ~(0xff << (off & 3)*8);
474 val32 |= (val << (off & 3)*8);
475 WR4(sc, off & ~3, val32);
476 }
477
478 static void
bcm_sdhci_write_2(device_t dev,struct sdhci_slot * slot,bus_size_t off,uint16_t val)479 bcm_sdhci_write_2(device_t dev, struct sdhci_slot *slot, bus_size_t off,
480 uint16_t val)
481 {
482 struct bcm_sdhci_softc *sc = device_get_softc(dev);
483 uint32_t val32;
484
485 /*
486 * If we have a queued up 16bit value for blk size or count, use and
487 * update the saved value rather than doing any real register access.
488 * If we did not touch either since the last write, then read from
489 * register as at least block count can change.
490 * Similarly, if we are about to issue a command, always use the saved
491 * value for transfer mode as we can never write that without issuing
492 * a command.
493 */
494 if ((off == SDHCI_BLOCK_SIZE || off == SDHCI_BLOCK_COUNT) &&
495 sc->need_update_blk)
496 val32 = sc->blksz_and_count;
497 else if (off == SDHCI_COMMAND_FLAGS)
498 val32 = sc->cmd_and_mode;
499 else
500 val32 = RD4(sc, off & ~3);
501
502 val32 &= ~(0xffff << (off & 3)*8);
503 val32 |= (val << (off & 3)*8);
504
505 if (off == SDHCI_TRANSFER_MODE)
506 sc->cmd_and_mode = val32;
507 else if (off == SDHCI_BLOCK_SIZE || off == SDHCI_BLOCK_COUNT) {
508 sc->blksz_and_count = val32;
509 sc->need_update_blk = true;
510 } else {
511 if (off == SDHCI_COMMAND_FLAGS) {
512 /* If we saved blk writes, do them now before cmd. */
513 if (sc->need_update_blk) {
514 WR4(sc, SDHCI_BLOCK_SIZE, sc->blksz_and_count);
515 sc->need_update_blk = false;
516 }
517 /* Always save cmd and mode registers. */
518 sc->cmd_and_mode = val32;
519 }
520 WR4(sc, off & ~3, val32);
521 }
522 }
523
524 static void
bcm_sdhci_write_4(device_t dev,struct sdhci_slot * slot,bus_size_t off,uint32_t val)525 bcm_sdhci_write_4(device_t dev, struct sdhci_slot *slot, bus_size_t off,
526 uint32_t val)
527 {
528 struct bcm_sdhci_softc *sc = device_get_softc(dev);
529 WR4(sc, off, val);
530 }
531
532 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)533 bcm_sdhci_write_multi_4(device_t dev, struct sdhci_slot *slot, bus_size_t off,
534 uint32_t *data, bus_size_t count)
535 {
536 struct bcm_sdhci_softc *sc = device_get_softc(dev);
537
538 bus_space_write_multi_4(sc->sc_bst, sc->sc_bsh, off, data, count);
539 }
540
541 static void
bcm_sdhci_start_dma_seg(struct bcm_sdhci_softc * sc)542 bcm_sdhci_start_dma_seg(struct bcm_sdhci_softc *sc)
543 {
544 struct sdhci_slot *slot;
545 vm_paddr_t pdst, psrc;
546 int err, idx, len, sync_op, width;
547
548 slot = &sc->sc_slot;
549 mtx_assert(&slot->mtx, MA_OWNED);
550 idx = sc->dmamap_seg_index++;
551 len = sc->dmamap_seg_sizes[idx];
552 slot->offset += len;
553 width = (len & 0xf ? BCM_DMA_32BIT : BCM_DMA_128BIT);
554
555 if (slot->curcmd->data->flags & MMC_DATA_READ) {
556 /*
557 * Peripherals on the AXI bus do not need DREQ pacing for reads
558 * from the ARM core, so we can safely set this to NONE.
559 */
560 bcm_dma_setup_src(sc->sc_dma_ch, BCM_DMA_DREQ_NONE,
561 BCM_DMA_SAME_ADDR, BCM_DMA_32BIT);
562 bcm_dma_setup_dst(sc->sc_dma_ch, BCM_DMA_DREQ_NONE,
563 BCM_DMA_INC_ADDR, width);
564 psrc = sc->sc_sdhci_buffer_phys;
565 pdst = sc->dmamap_seg_addrs[idx];
566 sync_op = BUS_DMASYNC_PREREAD;
567 } else {
568 /*
569 * The ordering here is important, because the last write to
570 * dst/src in the dma control block writes the real dreq value.
571 */
572 bcm_dma_setup_src(sc->sc_dma_ch, BCM_DMA_DREQ_NONE,
573 BCM_DMA_INC_ADDR, width);
574 bcm_dma_setup_dst(sc->sc_dma_ch, sc->conf->emmc_dreq,
575 BCM_DMA_SAME_ADDR, BCM_DMA_32BIT);
576 psrc = sc->dmamap_seg_addrs[idx];
577 pdst = sc->sc_sdhci_buffer_phys;
578 sync_op = BUS_DMASYNC_PREWRITE;
579 }
580
581 /*
582 * When starting a new DMA operation do the busdma sync operation, and
583 * disable SDCHI data interrrupts because we'll be driven by DMA
584 * interrupts (or SDHCI error interrupts) until the IO is done.
585 */
586 if (idx == 0) {
587 bus_dmamap_sync(sc->sc_dma_tag, sc->sc_dma_map, sync_op);
588
589 slot->intmask &= ~DATA_XFER_MASK;
590 bcm_sdhci_write_4(sc->sc_dev, slot, SDHCI_SIGNAL_ENABLE,
591 slot->intmask);
592 }
593
594 /*
595 * Start the DMA transfer. Only programming errors (like failing to
596 * allocate a channel) cause a non-zero return from bcm_dma_start().
597 */
598 err = bcm_dma_start(sc->sc_dma_ch, psrc, pdst, len);
599 KASSERT((err == 0), ("bcm2835_sdhci: failed DMA start"));
600 }
601
602 static void
bcm_sdhci_dma_exit(struct bcm_sdhci_softc * sc)603 bcm_sdhci_dma_exit(struct bcm_sdhci_softc *sc)
604 {
605 struct sdhci_slot *slot = &sc->sc_slot;
606
607 mtx_assert(&slot->mtx, MA_OWNED);
608
609 /* Re-enable interrupts */
610 slot->intmask |= DATA_XFER_MASK;
611 bcm_sdhci_write_4(slot->bus, slot, SDHCI_SIGNAL_ENABLE,
612 slot->intmask);
613 }
614
615 static void
bcm_sdhci_dma_unload(struct bcm_sdhci_softc * sc)616 bcm_sdhci_dma_unload(struct bcm_sdhci_softc *sc)
617 {
618 struct sdhci_slot *slot = &sc->sc_slot;
619
620 if (sc->dmamap_seg_count == 0)
621 return;
622 if ((slot->curcmd->data->flags & MMC_DATA_READ) != 0)
623 bus_dmamap_sync(sc->sc_dma_tag, sc->sc_dma_map,
624 BUS_DMASYNC_POSTREAD);
625 else
626 bus_dmamap_sync(sc->sc_dma_tag, sc->sc_dma_map,
627 BUS_DMASYNC_POSTWRITE);
628 bus_dmamap_unload(sc->sc_dma_tag, sc->sc_dma_map);
629
630 sc->dmamap_seg_count = 0;
631 sc->dmamap_seg_index = 0;
632 }
633
634 static void
bcm_sdhci_dma_intr(int ch,void * arg)635 bcm_sdhci_dma_intr(int ch, void *arg)
636 {
637 struct bcm_sdhci_softc *sc = (struct bcm_sdhci_softc *)arg;
638 struct sdhci_slot *slot = &sc->sc_slot;
639 uint32_t reg;
640
641 mtx_lock(&slot->mtx);
642 if (slot->curcmd == NULL)
643 goto out;
644 /*
645 * If there are more segments for the current dma, start the next one.
646 * Otherwise unload the dma map and decide what to do next based on the
647 * status of the sdhci controller and whether there's more data left.
648 */
649 if (sc->dmamap_seg_index < sc->dmamap_seg_count) {
650 bcm_sdhci_start_dma_seg(sc);
651 goto out;
652 }
653
654 bcm_sdhci_dma_unload(sc);
655
656 /*
657 * If we had no further segments pending, we need to determine how to
658 * proceed next. If the 'data/space pending' bit is already set and we
659 * can continue via DMA, do so. Otherwise, re-enable interrupts and
660 * return.
661 */
662 reg = bcm_sdhci_read_4(slot->bus, slot, SDHCI_INT_STATUS) &
663 DATA_XFER_MASK;
664 if ((reg & DATA_PENDING_MASK) != 0 &&
665 BCM_SDHCI_SEGSZ_LEFT(slot) >= BCM_SDHCI_BUFFER_SIZE) {
666 /* ACK any pending interrupts */
667 bcm_sdhci_write_4(slot->bus, slot, SDHCI_INT_STATUS,
668 DATA_PENDING_MASK);
669
670 bcm_sdhci_start_dma(slot);
671 if (slot->curcmd->error != 0) {
672 /* We won't recover from this error for this command. */
673 bcm_sdhci_dma_unload(sc);
674 bcm_sdhci_dma_exit(sc);
675 sdhci_finish_data(slot);
676 }
677 } else if ((reg & SDHCI_INT_DATA_END) != 0) {
678 bcm_sdhci_dma_exit(sc);
679 bcm_sdhci_write_4(slot->bus, slot, SDHCI_INT_STATUS,
680 reg);
681 slot->flags &= ~PLATFORM_DATA_STARTED;
682 sdhci_finish_data(slot);
683 } else {
684 bcm_sdhci_dma_exit(sc);
685 }
686 out:
687 mtx_unlock(&slot->mtx);
688 }
689
690 static void
bcm_sdhci_start_dma(struct sdhci_slot * slot)691 bcm_sdhci_start_dma(struct sdhci_slot *slot)
692 {
693 struct bcm_sdhci_softc *sc = device_get_softc(slot->bus);
694 uint8_t *buf;
695 size_t left;
696
697 mtx_assert(&slot->mtx, MA_OWNED);
698
699 left = BCM_SDHCI_SEGSZ_LEFT(slot);
700 buf = (uint8_t *)slot->curcmd->data->data + slot->offset;
701 KASSERT(left != 0,
702 ("%s: DMA handling incorrectly indicated", __func__));
703
704 /*
705 * No need to check segment count here; if we've not yet unloaded
706 * previous segments, we'll catch that in bcm_sdhci_dmacb.
707 */
708 if (bus_dmamap_load(sc->sc_dma_tag, sc->sc_dma_map, buf, left,
709 bcm_sdhci_dmacb, sc, BUS_DMA_NOWAIT) != 0 ||
710 sc->dmamap_status != 0) {
711 slot->curcmd->error = MMC_ERR_NO_MEMORY;
712 return;
713 }
714
715 /* DMA start */
716 bcm_sdhci_start_dma_seg(sc);
717 }
718
719 static int
bcm_sdhci_will_handle_transfer(device_t dev,struct sdhci_slot * slot)720 bcm_sdhci_will_handle_transfer(device_t dev, struct sdhci_slot *slot)
721 {
722 #ifdef INVARIANTS
723 struct bcm_sdhci_softc *sc = device_get_softc(slot->bus);
724 #endif
725
726 /*
727 * We don't want to perform DMA in this context -- interrupts are
728 * disabled, and a transaction may already be in progress.
729 */
730 if (dumping)
731 return (0);
732
733 /*
734 * This indicates that we somehow let a data interrupt slip by into the
735 * SDHCI framework, when it should not have. This really needs to be
736 * caught and fixed ASAP, as it really shouldn't happen.
737 */
738 KASSERT(sc->dmamap_seg_count == 0,
739 ("data pending interrupt pushed through SDHCI framework"));
740
741 /*
742 * Do not use DMA for transfers less than our block size. Checking
743 * alignment serves little benefit, as we round transfer sizes down to
744 * a multiple of the block size and push the transfer back to
745 * SDHCI-driven PIO once we're below the block size.
746 */
747 if (BCM_SDHCI_SEGSZ_LEFT(slot) < BCM_DMA_BLOCK_SIZE)
748 return (0);
749
750 return (1);
751 }
752
753 static void
bcm_sdhci_start_transfer(device_t dev,struct sdhci_slot * slot,uint32_t * intmask)754 bcm_sdhci_start_transfer(device_t dev, struct sdhci_slot *slot,
755 uint32_t *intmask)
756 {
757
758 /* DMA transfer FIFO 1KB */
759 bcm_sdhci_start_dma(slot);
760 }
761
762 static void
bcm_sdhci_finish_transfer(device_t dev,struct sdhci_slot * slot)763 bcm_sdhci_finish_transfer(device_t dev, struct sdhci_slot *slot)
764 {
765 struct bcm_sdhci_softc *sc = device_get_softc(slot->bus);
766
767 /*
768 * Clean up. Interrupts are clearly enabled, because we received an
769 * SDHCI_INT_DATA_END to get this far -- just make sure we don't leave
770 * anything laying around.
771 */
772 if (sc->dmamap_seg_count != 0) {
773 /*
774 * Our segment math should have worked out such that we would
775 * never finish the transfer without having used up all of the
776 * segments. If we haven't, that means we must have erroneously
777 * regressed to SDHCI-driven PIO to finish the operation and
778 * this is certainly caused by developer-error.
779 */
780 bcm_sdhci_dma_unload(sc);
781 }
782
783 sdhci_finish_data(slot);
784 }
785
786 static device_method_t bcm_sdhci_methods[] = {
787 /* Device interface */
788 DEVMETHOD(device_probe, bcm_sdhci_probe),
789 DEVMETHOD(device_attach, bcm_sdhci_attach),
790 DEVMETHOD(device_detach, bcm_sdhci_detach),
791
792 /* Bus interface */
793 DEVMETHOD(bus_read_ivar, sdhci_generic_read_ivar),
794 DEVMETHOD(bus_write_ivar, sdhci_generic_write_ivar),
795
796 /* MMC bridge interface */
797 DEVMETHOD(mmcbr_update_ios, sdhci_generic_update_ios),
798 DEVMETHOD(mmcbr_request, sdhci_generic_request),
799 DEVMETHOD(mmcbr_get_ro, bcm_sdhci_get_ro),
800 DEVMETHOD(mmcbr_acquire_host, sdhci_generic_acquire_host),
801 DEVMETHOD(mmcbr_release_host, sdhci_generic_release_host),
802
803 /* Platform transfer methods */
804 DEVMETHOD(sdhci_platform_will_handle, bcm_sdhci_will_handle_transfer),
805 DEVMETHOD(sdhci_platform_start_transfer, bcm_sdhci_start_transfer),
806 DEVMETHOD(sdhci_platform_finish_transfer, bcm_sdhci_finish_transfer),
807 /* SDHCI registers accessors */
808 DEVMETHOD(sdhci_read_1, bcm_sdhci_read_1),
809 DEVMETHOD(sdhci_read_2, bcm_sdhci_read_2),
810 DEVMETHOD(sdhci_read_4, bcm_sdhci_read_4),
811 DEVMETHOD(sdhci_read_multi_4, bcm_sdhci_read_multi_4),
812 DEVMETHOD(sdhci_write_1, bcm_sdhci_write_1),
813 DEVMETHOD(sdhci_write_2, bcm_sdhci_write_2),
814 DEVMETHOD(sdhci_write_4, bcm_sdhci_write_4),
815 DEVMETHOD(sdhci_write_multi_4, bcm_sdhci_write_multi_4),
816
817 DEVMETHOD_END
818 };
819
820 static devclass_t bcm_sdhci_devclass;
821
822 static driver_t bcm_sdhci_driver = {
823 "sdhci_bcm",
824 bcm_sdhci_methods,
825 sizeof(struct bcm_sdhci_softc),
826 };
827
828 DRIVER_MODULE(sdhci_bcm, simplebus, bcm_sdhci_driver, bcm_sdhci_devclass,
829 NULL, NULL);
830 #ifdef NOTYET
831 MODULE_DEPEND(sdhci_bcm, bcm2835_clkman, 1, 1, 1);
832 #endif
833 SDHCI_DEPEND(sdhci_bcm);
834 #ifndef MMCCAM
835 MMC_DECLARE_BRIDGE(sdhci_bcm);
836 #endif
837