Module Name:    src
Committed By:   jmcneill
Date:           Sun Jan  9 15:03:43 UTC 2022

Modified Files:
        src/sys/dev/ic: dwc_mmc.c dwc_mmc_reg.h dwc_mmc_var.h

Log Message:
dwcmmc: Add support for card detect using SDMMC_CDETECT register


To generate a diff of this commit:
cvs rdiff -u -r1.28 -r1.29 src/sys/dev/ic/dwc_mmc.c
cvs rdiff -u -r1.10 -r1.11 src/sys/dev/ic/dwc_mmc_reg.h
cvs rdiff -u -r1.14 -r1.15 src/sys/dev/ic/dwc_mmc_var.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/ic/dwc_mmc.c
diff -u src/sys/dev/ic/dwc_mmc.c:1.28 src/sys/dev/ic/dwc_mmc.c:1.29
--- src/sys/dev/ic/dwc_mmc.c:1.28	Sat Aug  7 16:19:12 2021
+++ src/sys/dev/ic/dwc_mmc.c	Sun Jan  9 15:03:43 2022
@@ -1,4 +1,4 @@
-/* $NetBSD: dwc_mmc.c,v 1.28 2021/08/07 16:19:12 thorpej Exp $ */
+/* $NetBSD: dwc_mmc.c,v 1.29 2022/01/09 15:03:43 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2014-2017 Jared McNeill <jmcne...@invisible.ca>
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: dwc_mmc.c,v 1.28 2021/08/07 16:19:12 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dwc_mmc.c,v 1.29 2022/01/09 15:03:43 jmcneill Exp $");
 
 #include <sys/param.h>
 #include <sys/bus.h>
@@ -162,6 +162,10 @@ dwc_mmc_attach_i(device_t self)
 {
 	struct dwc_mmc_softc *sc = device_private(self);
 	struct sdmmcbus_attach_args saa;
+	bool poll_detect;
+
+	poll_detect = sc->sc_card_detect != NULL ||
+	   (sc->sc_flags & (DWC_MMC_F_BROKEN_CD|DWC_MMC_F_NON_REMOVABLE)) == 0;
 
 	if (sc->sc_pre_power_on)
 		sc->sc_pre_power_on(sc);
@@ -185,12 +189,15 @@ dwc_mmc_attach_i(device_t self)
 		       SMC_CAPS_AUTO_STOP |
 		       SMC_CAPS_DMA |
 		       SMC_CAPS_MULTI_SEG_DMA;
-	if (sc->sc_bus_width == 8)
+	if (sc->sc_bus_width == 8) {
 		saa.saa_caps |= SMC_CAPS_8BIT_MODE;
-	else
+	} else {
 		saa.saa_caps |= SMC_CAPS_4BIT_MODE;
-	if (sc->sc_card_detect)
+	}
+
+	if (poll_detect) {
 		saa.saa_caps |= SMC_CAPS_POLL_CARD_DET;
+	}
 
 	sc->sc_sdmmc_dev = config_found(self, &saa, NULL, CFARGS_NONE);
 }
@@ -276,11 +283,21 @@ static int
 dwc_mmc_card_detect(sdmmc_chipset_handle_t sch)
 {
 	struct dwc_mmc_softc *sc = sch;
+	int det_n;
+
+	if (sc->sc_card_detect != NULL) {
+		return sc->sc_card_detect(sc);
+	}
+	if ((sc->sc_flags & DWC_MMC_F_BROKEN_CD) != 0) {
+		return 1;	/* broken card detect, assume present */
+	}
+	if ((sc->sc_flags & DWC_MMC_F_NON_REMOVABLE) != 0) {
+		return 1;	/* non-removable device is always present */
+	}
 
-	if (!sc->sc_card_detect)
-		return 1;	/* no card detect pin, assume present */
+	det_n = MMC_READ(sc, DWC_MMC_CDETECT) & DWC_MMC_CDETECT_CARD_DETECT_N;
 
-	return sc->sc_card_detect(sc);
+	return !det_n;
 }
 
 static int

Index: src/sys/dev/ic/dwc_mmc_reg.h
diff -u src/sys/dev/ic/dwc_mmc_reg.h:1.10 src/sys/dev/ic/dwc_mmc_reg.h:1.11
--- src/sys/dev/ic/dwc_mmc_reg.h:1.10	Fri Mar 20 17:02:16 2020
+++ src/sys/dev/ic/dwc_mmc_reg.h	Sun Jan  9 15:03:43 2022
@@ -1,4 +1,4 @@
-/* $NetBSD: dwc_mmc_reg.h,v 1.10 2020/03/20 17:02:16 skrll Exp $ */
+/* $NetBSD: dwc_mmc_reg.h,v 1.11 2022/01/09 15:03:43 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2014-2017 Jared McNeill <jmcne...@invisible.ca>
@@ -147,6 +147,8 @@
 #define	DWC_MMC_FIFOTH_RX_WMARK		__BITS(27,16)
 #define	DWC_MMC_FIFOTH_TX_WMARK		__BITS(11,0)
 
+#define	DWC_MMC_CDETECT_CARD_DETECT_N	__BIT(0)
+
 #define DWC_MMC_DMAC_IDMA_ON		__BIT(7)
 #define DWC_MMC_DMAC_FIX_BURST		__BIT(1)
 #define DWC_MMC_DMAC_SOFTRESET		__BIT(0)

Index: src/sys/dev/ic/dwc_mmc_var.h
diff -u src/sys/dev/ic/dwc_mmc_var.h:1.14 src/sys/dev/ic/dwc_mmc_var.h:1.15
--- src/sys/dev/ic/dwc_mmc_var.h:1.14	Fri Mar 20 17:07:17 2020
+++ src/sys/dev/ic/dwc_mmc_var.h	Sun Jan  9 15:03:43 2022
@@ -1,4 +1,4 @@
-/* $NetBSD: dwc_mmc_var.h,v 1.14 2020/03/20 17:07:17 skrll Exp $ */
+/* $NetBSD: dwc_mmc_var.h,v 1.15 2022/01/09 15:03:43 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2014-2017 Jared McNeill <jmcne...@invisible.ca>
@@ -40,6 +40,8 @@ struct dwc_mmc_softc {
 #define	DWC_MMC_F_DMA		__BIT(0)
 #define	DWC_MMC_F_USE_HOLD_REG	__BIT(1)
 #define	DWC_MMC_F_PWREN_INV	__BIT(2)
+#define	DWC_MMC_F_BROKEN_CD	__BIT(3)
+#define	DWC_MMC_F_NON_REMOVABLE	__BIT(4)
 	uint32_t sc_fifo_reg;
 	uint32_t sc_fifo_depth;
 	u_int sc_clock_freq;

Reply via email to