Skip to contentSkip to conversation

LoongArch: Introduce the Loongson-2K MMC host controller driver

PATCH Superseded Binbin Zhou 5 participants 15 messages May 7, 2025 – Jun 10, 2025

Subsystems (4)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Binbin Zhou

author maintainer
#0 lore raw

v2 cover LoongArch: Introduce the Loongson-2K MMC host controller driver

Hi all:

This patchset introduce the MMC host controller on Loongson-2K series CPUs.

They are similar, except for the interface characteristics and the use of DMA engine, specifically, the Loongson-2K0500/Loongson-2K1000 use an externally shared APBDMA engine, while the Loongson-2K2000 uses an internally exclusive DMA.

Based on this, I'm splitting the driver into two patches.

List of the patchset:
Patch1: bindings for Loongson-2K0500/Loongson-2K1000;
Patch2: driver for MMC controller using externally shared APBDMA engine;
Patch3: bindings for Loongson-2K2000;
Patch4: driver for MMC controller using internally exclusive DMA.

Thanks.

-------
V2:

patch(1/4):
- Add reg define for each reg entry.

patch(2/4):
- Put all code in the c-file;
- Use mmc_from_priv() instead of host->mmc;
- Use sdio_signal_irq() instead of mmc_signal_sdio_irq();
- Use devm_mmc_alloc_host() instead of mmc_alloc_host();
- Use mmc_regulator_get_supply();

patch(4/4):
- Add fix_cmd_interrupt function which is needed by Loongson-2K2000.

Link to V1:
https://lore.kernel.org/linux-mmc/[email protected]/

Binbin Zhou (4):
dt-bindings: mmc: Add Loongson-2K SD/SDIO/eMMC controller binding
mmc: loongson2: Add Loongson-2K SD/SDIO controller driver
dt-bindings: mmc: loongson,ls2k-mmc: Add compatible for

    Loongson-2K2000

mmc: loongson2: Add Loongson-2K2000 SD/SDIO/eMMC controller driver

 .../bindings/mmc/loongson,ls2k-mmc.yaml       |  112 ++
 MAINTAINERS                                   |    7 +
 drivers/mmc/host/Kconfig                      |   13 +
 drivers/mmc/host/Makefile                     |    1 +
 drivers/mmc/host/loongson2-mmc.c              | 1007 +++++++++++++++++
 5 files changed, 1140 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/mmc/loongson,ls2k-mmc.yaml
 create mode 100644 drivers/mmc/host/loongson2-mmc.c

base-commit: 9e12816f9a6195f1f5b7c5dc2e388c2458411b97

Signature · 1 line
2.47.1
author maintainer
#1 lore raw

PATCH v2 1/4 dt-bindings: mmc: Add Loongson-2K SD/SDIO/eMMC controller binding

Add the Loongson-2K SoC's SD/SDIO/eMMC controller binding with DT schema format using json-schema.

---
 .../bindings/mmc/loongson,ls2k-mmc.yaml       | 69 +++++++++++++++++++
 MAINTAINERS                                   |  6 ++
 2 files changed, 75 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/mmc/loongson,ls2k-mmc.yaml
Documentation/devicetree/bindings/mmc/loongson,ls2k-mmc.yaml new file +69 -0
new file mode 100644
index 000000000000..97a0853399f1
@@ -0,0 +1,69 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/mmc/loongson,ls2k-mmc.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: The SD/SDIO/eMMC host controller for Loongson-2K family SoCs
+
+description:
+  The MMC host controller on the Loongson-2K0500/2K1000 (using an externally
+  shared apbdma controller) provides the SD and SDIO device interfaces.
+
+maintainers:
+  - Binbin Zhou <[email protected]>
+
+allOf:
+  - $ref: mmc-controller.yaml#
+
+properties:
+  compatible:
+    enum:
+      - loongson,ls2k0500-mmc
+      - loongson,ls2k1000-mmc
+
+  reg:
+    items:
+      - description: Loongson-2K MMC controller registers.
+      - description: APB DMA config register for Loongson-2K MMC controller.
+
+  interrupts:
+    maxItems: 1
+
+  clocks:
+    maxItems: 1
+
+  dmas:
+    maxItems: 1
+
+  dma-names:
+    const: rx-tx
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - clocks
+  - dmas
+  - dma-names
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/gpio/gpio.h>
+    #include <dt-bindings/interrupt-controller/irq.h>
+    #include <dt-bindings/clock/loongson,ls2k-clk.h>
+
+    mmc@1fe2c000 {
+        compatible = "loongson,ls2k1000-mmc";
+        reg = <0x1fe2c000 0x68>,
+              <0x1fe00438 0x8>;
+        interrupt-parent = <&liointc0>;
+        interrupts = <31 IRQ_TYPE_LEVEL_HIGH>;
+        clocks = <&clk LOONGSON2_APB_CLK>;
+        dmas = <&apbdma1 0>;
+        dma-names = "rx-tx";
+        bus-width = <4>;
+        cd-gpios = <&gpio0 22 GPIO_ACTIVE_LOW>;
+    };
MAINTAINERS +6 -0
index 96b827049501..5bf74aa63299 100644
@@ -13935,6 +13935,12 @@ S:	Maintained
 F:	Documentation/devicetree/bindings/hwinfo/loongson,ls2k-chipid.yaml
 F:	drivers/soc/loongson/loongson2_guts.c
 
+LOONGSON-2 SOC SERIES MMC/SD/SDIO CONTROLLER DRIVER
+M:	Binbin Zhou <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/devicetree/bindings/mmc/loongson,ls2k-mmc.yaml
+
 LOONGSON-2 SOC SERIES PM DRIVER
 M:	Yinbo Zhu <[email protected]>
 L:	[email protected]
Signature · 1 line
2.47.1
author maintainer
#4 lore raw

PATCH v2 2/4 mmc: loongson2: Add Loongson-2K SD/SDIO controller driver

In reply to Binbin Zhou

The MMC controllers on the Loongson-2K series CPUs are similar, except for the interface characteristics and the use of DMA controllers.

This patch describes the MMC controllers on the Loongson-2K0500/2K1000, with the distinguishing feature being the use of an externally shared APBDMA engine.

---
 MAINTAINERS                      |   1 +
 drivers/mmc/host/Kconfig         |  13 +
 drivers/mmc/host/Makefile        |   1 +
 drivers/mmc/host/loongson2-mmc.c | 802 +++++++++++++++++++++++++++++++
 4 files changed, 817 insertions(+)
 create mode 100644 drivers/mmc/host/loongson2-mmc.c
MAINTAINERS +1 -0
index 5bf74aa63299..884666933c4d 100644
@@ -13940,6 +13940,7 @@ M:	Binbin Zhou <[email protected]>
 L:	[email protected]
 S:	Maintained
 F:	Documentation/devicetree/bindings/mmc/loongson,ls2k-mmc.yaml
+F:	drivers/mmc/host/loongson2-mmc.c
 
 LOONGSON-2 SOC SERIES PM DRIVER
 M:	Yinbo Zhu <[email protected]>
drivers/mmc/host/Kconfig +13 -0
index 264e11fa58ea..bb8d63d7bc5b 100644
@@ -1097,6 +1097,19 @@ config MMC_OWL
 	  This selects support for the SD/MMC Host Controller on
 	  Actions Semi Owl SoCs.
 
+config MMC_LOONGSON2
+	tristate "Loongson-2K SD/SDIO/eMMC Host Interface support"
+	depends on LOONGARCH || COMPILE_TEST
+	depends on HAS_DMA
+	help
+	  This selects support for the SD/SDIO/eMMC Host Controller on
+	  Loongson-2K series CPUs.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called mmc_loongson2.
+
+	  If unsure, say N.
+
 config MMC_SDHCI_EXTERNAL_DMA
 	bool
 
drivers/mmc/host/Makefile +1 -0
index 5147467ec825..ad09453d65da 100644
@@ -72,6 +72,7 @@ obj-$(CONFIG_MMC_USDHI6ROL0)	+= usdhi6rol0.o
 obj-$(CONFIG_MMC_TOSHIBA_PCI)	+= toshsd.o
 obj-$(CONFIG_MMC_BCM2835)	+= bcm2835.o
 obj-$(CONFIG_MMC_OWL)		+= owl-mmc.o
+obj-$(CONFIG_MMC_LOONGSON2)	+= loongson2-mmc.o
 
 obj-$(CONFIG_MMC_REALTEK_PCI)	+= rtsx_pci_sdmmc.o
 obj-$(CONFIG_MMC_REALTEK_USB)	+= rtsx_usb_sdmmc.o
drivers/mmc/host/loongson2-mmc.c new file +802 -0

802 lines not on this page · raw message

Signature · 1 line
2.47.1
author maintainer
#9 lore raw

PATCH v2 3/4 dt-bindings: mmc: loongson,ls2k-mmc: Add compatible for Loongson-2K2000

In reply to Binbin Zhou

Add the devicetree compatible for Loongson-2K2000 EMMC/SD/SDIO controller.

---
 .../bindings/mmc/loongson,ls2k-mmc.yaml       | 47 ++++++++++++++++++-
 1 file changed, 45 insertions(+), 2 deletions(-)
Documentation/devicetree/bindings/mmc/loongson,ls2k-mmc.yaml +45 -2
index 97a0853399f1..6130be214d3c 100644
@@ -9,6 +9,9 @@ title: The SD/SDIO/eMMC host controller for Loongson-2K family SoCs
 description:
   The MMC host controller on the Loongson-2K0500/2K1000 (using an externally
   shared apbdma controller) provides the SD and SDIO device interfaces.
+  The two MMC host controllers on the Loongson-2K2000 are similar,
+  except that they use internal exclusive DMA. one controller provides
+  the eMMC interface and the other provides the SD/SDIO interface.
 
 maintainers:
   - Binbin Zhou <[email protected]>
@@ -21,8 +24,10 @@ properties:
     enum:
       - loongson,ls2k0500-mmc
       - loongson,ls2k1000-mmc
+      - loongson,ls2k2000-mmc
 
   reg:
+    minItems: 1
     items:
       - description: Loongson-2K MMC controller registers.
       - description: APB DMA config register for Loongson-2K MMC controller.
@@ -44,11 +49,31 @@ required:
   - reg
   - interrupts
   - clocks
-  - dmas
-  - dma-names
 
 unevaluatedProperties: false
 
+if:
+  properties:
+    compatible:
+      contains:
+        enum:
+          - loongson,ls2k0500-mmc
+          - loongson,ls2k1000-mmc
+
+then:
+  properties:
+    reg:
+      minItems: 2
+
+  required:
+    - dmas
+    - dma-names
+
+else:
+  properties:
+    reg:
+      maxItems: 1
+
 examples:
   - |
     #include <dt-bindings/gpio/gpio.h>
@@ -67,3 +92,21 @@ examples:
         bus-width = <4>;
         cd-gpios = <&gpio0 22 GPIO_ACTIVE_LOW>;
     };
+
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+    #include <dt-bindings/clock/loongson,ls2k-clk.h>
+
+    mmc@79990000 {
+        compatible = "loongson,ls2k2000-mmc";
+        reg = <0x79990000 0x1000>;
+        interrupt-parent = <&pic>;
+        interrupts = <51 IRQ_TYPE_LEVEL_HIGH>;
+        clocks = <&clk LOONGSON2_EMMC_CLK>;
+        bus-width = <8>;
+        non-removable;
+        cap-mmc-highspeed;
+        mmc-hs200-1_8v;
+        no-sd;
+        no-sdio;
+    };
Signature · 1 line
2.47.1
author maintainer
#11 lore raw

PATCH v2 4/4 mmc: loongson2: Add Loongson-2K2000 SD/SDIO/eMMC controller driver

In reply to Binbin Zhou

This patch describes the two MMC controllers of the Loongson-2K2000 SoC, one providing an eMMC interface and the other exporting an SD/SDIO interface.

Compared to the Loongson-2K1000's MMC controllers, their internals are similar, except that we use an internally exclusive DMA engine instead of an externally shared APBDMA engine.

---
 drivers/mmc/host/loongson2-mmc.c | 205 +++++++++++++++++++++++++++++++
 1 file changed, 205 insertions(+)
drivers/mmc/host/loongson2-mmc.c +205 -0
index e52fa0a3bc4a..d89c3a5adae2 100644
@@ -44,6 +44,18 @@
 #define LOONGSON2_MMC_REG_DATA		0x40 /* Data Register */
 #define LOONGSON2_MMC_REG_IEN		0x64 /* Interrupt Enable Register */
 
+/* EMMC DLL Mode Registers */
+#define LOONGSON2_MMC_REG_DLLVAL	0xf0
+#define LOONGSON2_MMC_REG_DLLCTL	0xf4
+#define LOONGSON2_MMC_REG_DELAY		0xf8
+#define LOONGSON2_MMC_REG_SEL		0xfc
+
+/* Exclusive DMA R/W Registers */
+#define LOONGSON2_MMC_REG_WDMA_LO	0x400
+#define LOONGSON2_MMC_REG_WDMA_HI	0x404
+#define LOONGSON2_MMC_REG_RDMA_LO	0x800
+#define LOONGSON2_MMC_REG_RDMA_HI	0x804
+
 /* Bitfields of control register */
 #define LOONGSON2_MMC_CTL_ENCLK		BIT(0)
 #define LOONGSON2_MMC_CTL_EXTCLK	BIT(1)
@@ -109,6 +121,8 @@
 #define LOONGSON2_MMC_DSTS_RESUME	BIT(15)
 #define LOONGSON2_MMC_DSTS_SUSPEND	BIT(16)
 
+#define LOONGSON2_MMC_FSTS_TXFULL	BIT(11)
+
 /* Bitfields of interrupt register */
 #define LOONGSON2_MMC_INT_DFIN		BIT(0)
 #define LOONGSON2_MMC_INT_DTIMEOUT	BIT(1)
@@ -136,6 +150,37 @@
 #define LOONGSON2_MMC_IEN_ALL		GENMASK(9, 0)
 #define LOONGSON2_MMC_INT_CLEAR		GENMASK(9, 0)
 
+#define LOONGSON2_MMC_DLLVAL_DONE	BIT(8)
+
+#define LOONGSON2_MMC_DLLCTL_TIME	GENMASK(7, 0)
+#define LOONGSON2_MMC_DLLCTL_INCRE	GENMASK(15, 8)
+#define LOONGSON2_MMC_DLLCTL_START	GENMASK(23, 16)
+#define LOONGSON2_MMC_DLLCTL_CLK_MODE	BIT(24)
+#define LOONGSON2_MMC_DLLCTL_START_BIT	BIT(25)
+#define LOONGSON2_MMC_DLLCTL_TIME_BPASS	GENMASK(29, 26)
+
+#define LOONGSON2_MMC_DELAY_PAD		GENMASK(7, 0)
+#define LOONGSON2_MMC_DELAY_RD		GENMASK(15, 8)
+
+#define LOONGSON2_MMC_SEL_DATA		BIT(0)	/* 0: SDR, 1: DDR */
+#define LOONGSON2_MMC_SEL_BUS		BIT(0)	/* 0: EMMC, 1: SDIO */
+
+/* Bitfields in Global Configuration Register */
+#define LOONGSON2_MMC_DMA_64BIT_EN	BIT(0) /* 1: 64 bit support */
+#define LOONGSON2_MMC_DMA_UNCOHERENT_EN	BIT(1) /* 0: cache, 1: uncache */
+#define LOONGSON2_MMC_DMA_ASK_VALID	BIT(2)
+#define LOONGSON2_MMC_DMA_START		BIT(3) /* DMA start operation */
+#define LOONGSON2_MMC_DMA_STOP		BIT(4) /* DMA stop operation */
+#define LOONGSON2_MMC_DMA_CONFIG_MASK	GENMASK_ULL(4, 0) /* DMA controller config bits mask */
+
+/* Bitfields in ndesc_addr field of HW descriptor */
+#define LOONGSON2_MMC_DMA_DESC_EN	BIT(0) /*1: The next descriptor is valid */
+#define LOONGSON2_MMC_DMA_DESC_ADDR_LOW	GENMASK(31, 1)
+
+/* Bitfields in cmd field of HW descriptor */
+#define LOONGSON2_MMC_DMA_INT		BIT(1)	/* Enable DMA interrupts */
+#define LOONGSON2_MMC_DMA_DATA_DIR	BIT(12) /* 1: write to device, 0: read from device */
+
 /* Loongson-2K1000 SDIO2 DMA routing register */
 #define LS2K1000_SDIO_DMA_MASK		GENMASK(17, 15)
 #define LS2K1000_DMA0_CONF		0x0
@@ -180,6 +225,8 @@ struct loongson2_mmc_host {
 	struct resource *res;
 	struct clk *clk;
 	u64 rate;
+	void *sg_cpu;
+	dma_addr_t sg_dma;
 	int dma_complete;
 	struct dma_chan *chan;
 	int cmd_is_stop;
@@ -192,6 +239,7 @@ struct loongson2_mmc_host {
 struct loongson2_mmc_pdata {
 	const struct regmap_config regmap_config;
 	void (*reorder_cmd_data)(struct loongson2_mmc_host *host, struct mmc_command *cmd);
+	void (*fix_cmd_interrupt)(struct loongson2_mmc_host *host, struct mmc_command *cmd);
 	int (*setting_dma)(struct loongson2_mmc_host *host, struct platform_device *pdev);
 	int (*prepare_dma)(struct loongson2_mmc_host *host, struct mmc_data *data);
 	void (*release_dma)(struct loongson2_mmc_host *host, struct device *dev);
@@ -282,6 +330,12 @@ static void loongson2_mmc_send_request(struct mmc_host *mmc)
 		return;
 	}
 
+	/* Fix lose interrupt issue when sending MMC_WRITE_BLOCK
+	 * or MMC_WRITE_MULTIPLE_BLOCK commands
+	 */
+	if (host->pdata->fix_cmd_interrupt)
+		host->pdata->fix_cmd_interrupt(host, cmd);
+
 	loongson2_mmc_send_command(host, cmd);
 
 	/* Fix deselect card */
@@ -426,6 +480,36 @@ static irqreturn_t loongson2_mmc_irq(int irq, void *devid)
 	return IRQ_WAKE_THREAD;
 }
 
+static void loongson2_mmc_ddr_mode_init(struct loongson2_mmc_host *host)
+{
+	u32 val, pad_delay, delay, ret;
+
+	regmap_update_bits(host->regmap, LOONGSON2_MMC_REG_SEL,
+			   LOONGSON2_MMC_SEL_DATA, LOONGSON2_MMC_SEL_DATA);
+
+	val = FIELD_PREP(LOONGSON2_MMC_DLLCTL_TIME, 0xc8)
+	    | FIELD_PREP(LOONGSON2_MMC_DLLCTL_INCRE, 0x1)
+	    | FIELD_PREP(LOONGSON2_MMC_DLLCTL_START, 0x1)
+	    | FIELD_PREP(LOONGSON2_MMC_DLLCTL_CLK_MODE, 0x1)
+	    | FIELD_PREP(LOONGSON2_MMC_DLLCTL_START_BIT, 0x1)
+	    | FIELD_PREP(LOONGSON2_MMC_DLLCTL_TIME_BPASS, 0xf);
+
+	regmap_write(host->regmap, LOONGSON2_MMC_REG_DLLCTL, val);
+
+	ret = regmap_read_poll_timeout(host->regmap, LOONGSON2_MMC_REG_DLLVAL, val,
+				       (val & LOONGSON2_MMC_DLLVAL_DONE), 0, 4000);
+	if (ret < 0)
+		return;
+
+	regmap_read(host->regmap, LOONGSON2_MMC_REG_DLLVAL, &val);
+	pad_delay = FIELD_GET(GENMASK(7, 1), val);
+
+	delay = FIELD_PREP(LOONGSON2_MMC_DELAY_PAD, pad_delay)
+	      | FIELD_PREP(LOONGSON2_MMC_DELAY_RD, pad_delay + 1);
+
+	regmap_write(host->regmap, LOONGSON2_MMC_REG_DELAY, delay);
+}
+
 static void loongson2_mmc_set_clk(struct loongson2_mmc_host *host, struct mmc_ios *ios)
 {
 	u32 pre;
@@ -438,6 +522,10 @@ static void loongson2_mmc_set_clk(struct loongson2_mmc_host *host, struct mmc_io
 
 	regmap_update_bits(host->regmap, LOONGSON2_MMC_REG_CTL,
 			   LOONGSON2_MMC_CTL_ENCLK, LOONGSON2_MMC_CTL_ENCLK);
+
+	/* EMMC DDR mode setting */
+	if (ios->timing == MMC_TIMING_UHS_DDR50 || ios->timing == MMC_TIMING_MMC_DDR52)
+		loongson2_mmc_ddr_mode_init(host);
 }
 
 static void loongson2_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
@@ -641,6 +729,122 @@ static struct loongson2_mmc_pdata ls2k1000_mmc_pdata = {
 	.release_dma		= loongson2_mmc_release_external_dma,
 };
 
+static const struct regmap_config ls2k2000_mmc_regmap_config = {
+	.reg_bits = 32,
+	.val_bits = 32,
+	.reg_stride = 4,
+	.max_register = LOONGSON2_MMC_REG_RDMA_HI,
+};
+
+static void ls2k2000_mmc_reorder_cmd_data(struct loongson2_mmc_host *host,
+					  struct mmc_command *cmd)
+{
+	struct scatterlist *sg;
+	u32 *data;
+	int i, j;
+
+	if (cmd->opcode != SD_SWITCH || mmc_cmd_type(cmd) != MMC_CMD_ADTC)
+		return;
+
+	for_each_sg(cmd->data->sg, sg, cmd->data->sg_len, i) {
+		data = sg_virt(&sg[i]);
+		for (j = 0; j < (sg_dma_len(&sg[i]) / 4); j++)
+			data[j] = bitrev8x4(data[j]);
+	}
+}
+
+static void ls2k2000_mmc_fix_cmd_interrupt(struct loongson2_mmc_host *host,
+					   struct mmc_command *cmd)
+{
+	int val;
+
+	if (cmd->opcode != MMC_WRITE_BLOCK && cmd->opcode != MMC_WRITE_MULTIPLE_BLOCK)
+		return;
+
+	regmap_read_poll_timeout(host->regmap, LOONGSON2_MMC_REG_FSTS, val,
+				 (val & LOONGSON2_MMC_FSTS_TXFULL), 0, 500);
+}
+
+static int loongson2_mmc_prepare_internal_dma(struct loongson2_mmc_host *host,
+					      struct mmc_data *data)
+{
+	struct loongson2_dma_desc *pdes = (struct loongson2_dma_desc *)host->sg_cpu;
+	struct mmc_host *mmc = mmc_from_priv(host);
+	dma_addr_t next_desc = host->sg_dma;
+	struct scatterlist *sg;
+	int reg_lo, reg_hi;
+	u64 dma_order;
+	int i, ret;
+
+	ret = dma_map_sg(mmc_dev(mmc), data->sg, data->sg_len,
+			 mmc_get_dma_dir(data));
+	if (!ret)
+		return -ENOMEM;
+
+	for_each_sg(data->sg, sg, data->sg_len, i) {
+		pdes[i].len = sg_dma_len(&sg[i]) / 4;
+		pdes[i].step_len = 0;
+		pdes[i].step_times = 1;
+		pdes[i].mem_addr = lower_32_bits(sg_dma_address(&sg[i]));
+		pdes[i].high_mem_addr = upper_32_bits(sg_dma_address(&sg[i]));
+		pdes[i].apb_addr = host->res->start + LOONGSON2_MMC_REG_DATA;
+		pdes[i].cmd = LOONGSON2_MMC_DMA_INT;
+
+		if (data->flags & MMC_DATA_READ) {
+			reg_lo = LOONGSON2_MMC_REG_RDMA_LO;
+			reg_hi = LOONGSON2_MMC_REG_RDMA_HI;
+		} else {
+			pdes[i].cmd |= LOONGSON2_MMC_DMA_DATA_DIR;
+			reg_lo = LOONGSON2_MMC_REG_WDMA_LO;
+			reg_hi = LOONGSON2_MMC_REG_WDMA_HI;
+		}
+
+		next_desc += sizeof(struct loongson2_dma_desc);
+		pdes[i].ndesc_addr = lower_32_bits(next_desc) |
+				     LOONGSON2_MMC_DMA_DESC_EN;
+		pdes[i].high_ndesc_addr = upper_32_bits(next_desc);
+	}
+
+	/* Setting the last descriptor enable bit */
+	pdes[i - 1].ndesc_addr &= ~LOONGSON2_MMC_DMA_DESC_EN;
+
+	dma_order = (host->sg_dma & ~LOONGSON2_MMC_DMA_CONFIG_MASK) |
+		    LOONGSON2_MMC_DMA_64BIT_EN |
+		    LOONGSON2_MMC_DMA_START;
+
+	regmap_write(host->regmap, reg_hi, upper_32_bits(dma_order));
+	regmap_write(host->regmap, reg_lo, lower_32_bits(dma_order));
+
+	return 0;
+}
+
+static int loongson2_mmc_set_internal_dma(struct loongson2_mmc_host *host,
+					  struct platform_device *pdev)
+{
+	host->sg_cpu = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
+					  &host->sg_dma, GFP_KERNEL);
+	if (!host->sg_cpu)
+		return -ENOMEM;
+
+	memset(host->sg_cpu, 0, PAGE_SIZE);
+	return 0;
+}
+
+static void loongson2_mmc_release_internal_dma(struct loongson2_mmc_host *host,
+					       struct device *dev)
+{
+	dma_free_coherent(dev, PAGE_SIZE, host->sg_cpu, host->sg_dma);
+}
+
+static struct loongson2_mmc_pdata ls2k2000_mmc_pdata = {
+	.regmap_config		= ls2k2000_mmc_regmap_config,
+	.reorder_cmd_data	= ls2k2000_mmc_reorder_cmd_data,
+	.fix_cmd_interrupt	= ls2k2000_mmc_fix_cmd_interrupt,
+	.setting_dma		= loongson2_mmc_set_internal_dma,
+	.prepare_dma		= loongson2_mmc_prepare_internal_dma,
+	.release_dma		= loongson2_mmc_release_internal_dma,
+};
+
 static int loongson2_mmc_resource_request(struct platform_device *pdev,
 					  struct loongson2_mmc_host *host)
 {
@@ -759,6 +963,7 @@ static void loongson2_mmc_remove(struct platform_device *pdev)
 static const struct of_device_id loongson2_mmc_of_ids[] = {
 	{ .compatible = "loongson,ls2k0500-mmc", .data = &ls2k0500_mmc_pdata },
 	{ .compatible = "loongson,ls2k1000-mmc", .data = &ls2k1000_mmc_pdata },
+	{ .compatible = "loongson,ls2k2000-mmc", .data = &ls2k2000_mmc_pdata },
 	{ },
 };
 MODULE_DEVICE_TABLE(of, loongson2_mmc_of_ids);
Signature · 1 line
2.47.1
#5 lore raw

Re: [PATCH v2 2/4] mmc: loongson2: Add Loongson-2K SD/SDIO controller driver

In reply to Binbin Zhou
kernel test robot noticed the following build errors:

Hi Binbin,

kernel test robot noticed the following build errors:

[auto build test ERROR on 9e12816f9a6195f1f5b7c5dc2e388c2458411b97]

url:    https://github.com/intel-lab-lkp/linux/commits/Binbin-Zhou/dt-bindings-mmc-Add-Loongson-2K-SD-SDIO-eMMC-controller-binding/20250507-153435
base:   9e12816f9a6195f1f5b7c5dc2e388c2458411b97
patch link:    https://lore.kernel.org/r/1308b6ca9ffc2674cc0f089cfd163da87e53a8cd.1746581751.git.zhoubinbin%40loongson.cn
patch subject: [PATCH v2 2/4] mmc: loongson2: Add Loongson-2K SD/SDIO controller driver
config: riscv-allyesconfig (https://download.01.org/0day-ci/archive/20250508/[email protected]/config)
compiler: clang version 16.0.6 (https://github.com/llvm/llvm-project 7cbf1a2591520c2491aa35339f227775f4d3adf6)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250508/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

>> drivers/mmc/host/loongson2-mmc.c:605:20: error: initializer element is not a compile-time constant
           .regmap_config          = ls2k1000_regmap_config,
                                     ^~~~~~~~~~~~~~~~~~~~~~
   drivers/mmc/host/loongson2-mmc.c:637:20: error: initializer element is not a compile-time constant
           .regmap_config          = ls2k1000_regmap_config,
                                     ^~~~~~~~~~~~~~~~~~~~~~
   drivers/mmc/host/loongson2-mmc.c:684:39: warning: shift count >= width of type [-Wshift-count-overflow]
           ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
                                                ^~~~~~~~~~~~~~~~
   include/linux/dma-mapping.h:73:54: note: expanded from macro 'DMA_BIT_MASK'
   #define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : ((1ULL<<(n))-1))
                                                        ^ ~~~

1 warning and 2 errors generated.

vim +605 drivers/mmc/host/loongson2-mmc.c

603
604 static struct loongson2_mmc_pdata ls2k0500_mmc_pdata = {

 > 605		.regmap_config		= ls2k1000_regmap_config,

606 .reorder_cmd_data = loongson2_mmc_reorder_cmd_data,
607 .setting_dma = ls2k0500_mmc_set_external_dma,
608 .prepare_dma = loongson2_mmc_prepare_external_dma,
609 .release_dma = loongson2_mmc_release_external_dma,
610 };
611

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#2 lore raw

Re: [PATCH v2 1/4] dt-bindings: mmc: Add Loongson-2K SD/SDIO/eMMC controller binding

In reply to Binbin Zhou
Quoted 16 lines · Binbin Zhou

Add the Loongson-2K SoC's SD/SDIO/eMMC controller binding with DT schema format using json-schema.

---
 .../bindings/mmc/loongson,ls2k-mmc.yaml       | 69 +++++++++++++++++++
 MAINTAINERS                                   |  6 ++
 2 files changed, 75 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/mmc/loongson,ls2k-mmc.yaml
Documentation/devicetree/bindings/mmc/loongson,ls2k-mmc.yaml new file +0 -0
new file mode 100644
index 000000000000..97a0853399f1

Documentation/devicetree/bindings/mmc/loongson,ls2k-mmc.yaml line 4

Quoted 4 lines
Documentation/devicetree/bindings/mmc/loongson,ls2k-mmc.yaml new file +4 -0
@@ -0,0 +1,69 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/mmc/loongson,ls2k-mmc.yaml#

Filename matching a compatible please.
Otherwise this looks okay to me.

Quoted 85 lines
Documentation/devicetree/bindings/mmc/loongson,ls2k-mmc.yaml new file +65 -0
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: The SD/SDIO/eMMC host controller for Loongson-2K family SoCs
+
+description:
+  The MMC host controller on the Loongson-2K0500/2K1000 (using an externally
+  shared apbdma controller) provides the SD and SDIO device interfaces.
+
+maintainers:
+  - Binbin Zhou <[email protected]>
+
+allOf:
+  - $ref: mmc-controller.yaml#
+
+properties:
+  compatible:
+    enum:
+      - loongson,ls2k0500-mmc
+      - loongson,ls2k1000-mmc
+
+  reg:
+    items:
+      - description: Loongson-2K MMC controller registers.
+      - description: APB DMA config register for Loongson-2K MMC controller.
+
+  interrupts:
+    maxItems: 1
+
+  clocks:
+    maxItems: 1
+
+  dmas:
+    maxItems: 1
+
+  dma-names:
+    const: rx-tx
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - clocks
+  - dmas
+  - dma-names
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/gpio/gpio.h>
+    #include <dt-bindings/interrupt-controller/irq.h>
+    #include <dt-bindings/clock/loongson,ls2k-clk.h>
+
+    mmc@1fe2c000 {
+        compatible = "loongson,ls2k1000-mmc";
+        reg = <0x1fe2c000 0x68>,
+              <0x1fe00438 0x8>;
+        interrupt-parent = <&liointc0>;
+        interrupts = <31 IRQ_TYPE_LEVEL_HIGH>;
+        clocks = <&clk LOONGSON2_APB_CLK>;
+        dmas = <&apbdma1 0>;
+        dma-names = "rx-tx";
+        bus-width = <4>;
+        cd-gpios = <&gpio0 22 GPIO_ACTIVE_LOW>;
+    };
MAINTAINERS +6 -0
index 96b827049501..5bf74aa63299 100644
@@ -13935,6 +13935,12 @@ S:	Maintained
 F:	Documentation/devicetree/bindings/hwinfo/loongson,ls2k-chipid.yaml
 F:	drivers/soc/loongson/loongson2_guts.c
 
+LOONGSON-2 SOC SERIES MMC/SD/SDIO CONTROLLER DRIVER
+M:	Binbin Zhou <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/devicetree/bindings/mmc/loongson,ls2k-mmc.yaml
+
 LOONGSON-2 SOC SERIES PM DRIVER
 M:	Yinbo Zhu <[email protected]>
 L:	[email protected]
Signature · 1 line
2.47.1
#3 lore raw

Re: [PATCH v2 1/4] dt-bindings: mmc: Add Loongson-2K SD/SDIO/eMMC controller binding

Hi Conor:

Thanks for your review.

Quoted 12 lines · Conor Dooley
Quoted 10 lines · Binbin Zhou

Add the Loongson-2K SoC's SD/SDIO/eMMC controller binding with DT schema format using json-schema.

---
 .../bindings/mmc/loongson,ls2k-mmc.yaml       | 69 +++++++++++++++++++
 MAINTAINERS                                   |  6 ++
 2 files changed, 75 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/mmc/loongson,ls2k-mmc.yaml
Quoted 13 lines

Documentation/devicetree/bindings/mmc/loongson,ls2k-mmc.yaml line 4

Quoted 10 lines
Documentation/devicetree/bindings/mmc/loongson,ls2k-mmc.yaml new file +4 -0
new file mode 100644
index 000000000000..97a0853399f1
@@ -0,0 +1,69 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/mmc/loongson,ls2k-mmc.yaml#

Filename matching a compatible please.
Otherwise this looks okay to me.

Yes, I forgot about this point, it will be renamed to loongson,ls2k0500-mmc.yaml.

Quoted 74 lines
Quoted 74 lines
Documentation/devicetree/bindings/mmc/loongson,ls2k-mmc.yaml new file +65 -0
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: The SD/SDIO/eMMC host controller for Loongson-2K family SoCs
+
+description:
+  The MMC host controller on the Loongson-2K0500/2K1000 (using an externally
+  shared apbdma controller) provides the SD and SDIO device interfaces.
+
+maintainers:
+  - Binbin Zhou <[email protected]>
+
+allOf:
+  - $ref: mmc-controller.yaml#
+
+properties:
+  compatible:
+    enum:
+      - loongson,ls2k0500-mmc
+      - loongson,ls2k1000-mmc
+
+  reg:
+    items:
+      - description: Loongson-2K MMC controller registers.
+      - description: APB DMA config register for Loongson-2K MMC controller.
+
+  interrupts:
+    maxItems: 1
+
+  clocks:
+    maxItems: 1
+
+  dmas:
+    maxItems: 1
+
+  dma-names:
+    const: rx-tx
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - clocks
+  - dmas
+  - dma-names
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/gpio/gpio.h>
+    #include <dt-bindings/interrupt-controller/irq.h>
+    #include <dt-bindings/clock/loongson,ls2k-clk.h>
+
+    mmc@1fe2c000 {
+        compatible = "loongson,ls2k1000-mmc";
+        reg = <0x1fe2c000 0x68>,
+              <0x1fe00438 0x8>;
+        interrupt-parent = <&liointc0>;
+        interrupts = <31 IRQ_TYPE_LEVEL_HIGH>;
+        clocks = <&clk LOONGSON2_APB_CLK>;
+        dmas = <&apbdma1 0>;
+        dma-names = "rx-tx";
+        bus-width = <4>;
+        cd-gpios = <&gpio0 22 GPIO_ACTIVE_LOW>;
+    };
MAINTAINERS +0 -0
index 96b827049501..5bf74aa63299 100644
@@ -13935,6 +13935,12 @@ S:   Maintained
 F:   Documentation/devicetree/bindings/hwinfo/loongson,ls2k-chipid.yaml
 F:   drivers/soc/loongson/loongson2_guts.c
 
Quoted 12 lines
Quoted 10 lines
MAINTAINERS +6 -1
+LOONGSON-2 SOC SERIES MMC/SD/SDIO CONTROLLER DRIVER
+M:   Binbin Zhou <[email protected]>
+L:   [email protected]
+S:   Maintained
+F:   Documentation/devicetree/bindings/mmc/loongson,ls2k-mmc.yaml
+
 LOONGSON-2 SOC SERIES PM DRIVER
 M:   Yinbo Zhu <[email protected]>
 L:   [email protected]
--
Quoted 2 lines

2.47.1

--
Thanks.
Binbin

#6 lore raw

Re: [PATCH v2 2/4] mmc: loongson2: Add Loongson-2K SD/SDIO controller driver

In reply to Binbin Zhou
kernel test robot noticed the following build warnings:

Hi Binbin,

kernel test robot noticed the following build warnings:

[auto build test WARNING on 9e12816f9a6195f1f5b7c5dc2e388c2458411b97]

url:    https://github.com/intel-lab-lkp/linux/commits/Binbin-Zhou/dt-bindings-mmc-Add-Loongson-2K-SD-SDIO-eMMC-controller-binding/20250507-153435
base:   9e12816f9a6195f1f5b7c5dc2e388c2458411b97
patch link:    https://lore.kernel.org/r/1308b6ca9ffc2674cc0f089cfd163da87e53a8cd.1746581751.git.zhoubinbin%40loongson.cn
patch subject: [PATCH v2 2/4] mmc: loongson2: Add Loongson-2K SD/SDIO controller driver
config: csky-randconfig-r112-20250513 (https://download.01.org/0day-ci/archive/20250513/[email protected]/config)
compiler: csky-linux-gcc (GCC) 14.2.0
reproduce: (https://download.01.org/0day-ci/archive/20250513/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

sparse warnings: (new ones prefixed by >>)

>> drivers/mmc/host/loongson2-mmc.c:534:41: sparse: sparse: incorrect type in assignment (different base types) @@     expected unsigned int [usertype] @@     got restricted __be32 [usertype] @@

drivers/mmc/host/loongson2-mmc.c:534:41: sparse: expected unsigned int [usertype]
drivers/mmc/host/loongson2-mmc.c:534:41: sparse: got restricted __be32 [usertype]

vim +534 drivers/mmc/host/loongson2-mmc.c

500
501 static int loongson2_reorder_cmd_list[] = { SD_APP_SEND_SCR, SD_APP_SEND_NUM_WR_BLKS,
502 SD_APP_SD_STATUS, MMC_SEND_WRITE_PROT,
503 SD_SWITCH };
504
505 /*
506 * According to SD spec, ACMD13, ACMD22, ACMD51 and CMD30
507 * response datas has different byte order with usual data packets.
508 * However sdio controller will send these datas in usual data format,
509 * so we need to adjust these datas to a protocol consistent byte order.
510 */
511 static void loongson2_mmc_reorder_cmd_data(struct loongson2_mmc_host *host,
512 struct mmc_command *cmd)
513 {
514 struct scatterlist *sg;
515 u32 *data;
516 int i, j;
517
518 if (mmc_cmd_type(cmd) != MMC_CMD_ADTC)
519 return;
520
521 for (i = 0; i < ARRAY_SIZE(loongson2_reorder_cmd_list); i++)
522 if (cmd->opcode == loongson2_reorder_cmd_list[i])
523 break;
524
525 if (i == ARRAY_SIZE(loongson2_reorder_cmd_list))
526 return;
527
528 for_each_sg(cmd->data->sg, sg, cmd->data->sg_len, i) {
529 data = sg_virt(&sg[i]);
530 for (j = 0; j < (sg_dma_len(&sg[i]) / 4); j++)
531 if (cmd->opcode == SD_SWITCH)
532 data[j] = bitrev8x4(data[j]);
533 else

 > 534					data[j] = cpu_to_be32(data[j]);

535 }
536 }
537

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#7 lore raw

Re: [PATCH v2 2/4] mmc: loongson2: Add Loongson-2K SD/SDIO controller driver

In reply to Binbin Zhou
Quoted 9 lines · Binbin Zhou

The MMC controllers on the Loongson-2K series CPUs are similar, except for the interface characteristics and the use of DMA controllers.

This patch describes the MMC controllers on the Loongson-2K0500/2K1000, with the distinguishing feature being the use of an externally shared APBDMA engine.

[...]

drivers/mmc/host/loongson2-mmc.c line 445

Quoted 4 lines
drivers/mmc/host/loongson2-mmc.c new file +4 -0
+
+static void loongson2_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
+{
+       struct loongson2_mmc_host *host = mmc_priv(mmc);

As we now have support for regulators, we should use them here too.

Some something along the lines of this at MMC_POWER_OFF:
if (!IS_ERR(mmc->supply.vmmc))

      mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);

and at MMC_POWER_UP:
if (!IS_ERR(mmc->supply.vmmc))

      mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd);

drivers/mmc/host/loongson2-mmc.c line 448

Quoted 3 lines
drivers/mmc/host/loongson2-mmc.c new file +3 -0
+
+       switch (ios->power_mode) {
+       case MMC_POWER_ON:

Is the fallthrough really what we want here?

MMC_POWER_ON is used quite frequently when changing various ios settings when the core calls mmc_set_ios(). MMC_POWER_UP is set only once in mmc_power_up().

Quoted 20 lines
drivers/mmc/host/loongson2-mmc.c new file +20 -0
+       case MMC_POWER_UP:
+               regmap_write(host->regmap, LOONGSON2_MMC_REG_CTL, LOONGSON2_MMC_CTL_RESET);
+               mdelay(10);
+               regmap_write(host->regmap, LOONGSON2_MMC_REG_CTL, LOONGSON2_MMC_CTL_EXTCLK);
+               regmap_write(host->regmap, LOONGSON2_MMC_REG_INT, LOONGSON2_MMC_IEN_ALL);
+               regmap_write(host->regmap, LOONGSON2_MMC_REG_IEN, LOONGSON2_MMC_INT_CLEAR);
+               break;
+       case MMC_POWER_OFF:
+               regmap_update_bits(host->regmap, LOONGSON2_MMC_REG_CTL,
+                                  LOONGSON2_MMC_CTL_RESET, LOONGSON2_MMC_CTL_RESET);
+               return;
+       default:
+               return;
+       }
+
+       loongson2_mmc_set_clk(host, ios);
+
+       host->bus_width = ios->bus_width;
+}
+

[...]

Quoted 5 lines
drivers/mmc/host/loongson2-mmc.c new file +5 -0
+
+static void loongson2_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
+{
+       struct loongson2_mmc_host *host = mmc_priv(mmc);
+

drivers/mmc/host/loongson2-mmc.c line 491

Quoted 10 lines
drivers/mmc/host/loongson2-mmc.c new file +10 -0
+       regmap_update_bits(host->regmap, LOONGSON2_MMC_REG_IEN,
+                          LOONGSON2_MMC_INT_SDIOIRQ, enable);
+}
+
+static struct mmc_host_ops loongson2_mmc_ops = {
+       .request        = loongson2_mmc_request,
+       .set_ios        = loongson2_mmc_set_ios,
+       .get_ro         = mmc_gpio_get_ro,
+       .get_cd         = mmc_gpio_get_cd,
+       .enable_sdio_irq = loongson2_mmc_enable_sdio_irq,

The ->ack_sdio_irq() callback needs to be implemented too.

Moreover we need to set MMC_CAP2_SDIO_IRQ_NOTHREAD.

[...]

Kind regards
Uffe

#12 lore raw

Re: [PATCH v2 4/4] mmc: loongson2: Add Loongson-2K2000 SD/SDIO/eMMC controller driver

In reply to Binbin Zhou
Quoted 10 lines · Binbin Zhou

This patch describes the two MMC controllers of the Loongson-2K2000 SoC, one providing an eMMC interface and the other exporting an SD/SDIO interface.

Compared to the Loongson-2K1000's MMC controllers, their internals are similar, except that we use an internally exclusive DMA engine instead of an externally shared APBDMA engine.

[...]

drivers/mmc/host/loongson2-mmc.c static struct loongson2_mmc_pdata ls2k1000_mmc_pdata = { · line 765

Quoted 11 lines
drivers/mmc/host/loongson2-mmc.c +11 -0
+
+static void ls2k2000_mmc_fix_cmd_interrupt(struct loongson2_mmc_host *host,
+                                          struct mmc_command *cmd)
+{
+       int val;
+
+       if (cmd->opcode != MMC_WRITE_BLOCK && cmd->opcode != MMC_WRITE_MULTIPLE_BLOCK)
+               return;
+
+       regmap_read_poll_timeout(host->regmap, LOONGSON2_MMC_REG_FSTS, val,
+                                (val & LOONGSON2_MMC_FSTS_TXFULL), 0, 500);

Can you please elaborate on what goes on here?

Note that, the mmc core uses a couple of different options to manage busy detection monitoring on DAT0, for those commands that need it.

*) MMC_CAP_WAIT_WHILE_BUSY - if the host HW and the driver for it supports IRQ based busy-detection.
*) host_ops->card_busy() callback if the HW can poll the DAT0 manually for busy-detection.
*) Polling by using CMD13.

[...]

Kind regards
Uffe

#13 lore raw

Re: [PATCH v2 4/4] mmc: loongson2: Add Loongson-2K2000 SD/SDIO/eMMC controller driver

Hi Ulf:

Sorry for the late reply.

Quoted 15 lines · Ulf Hansson
Quoted 10 lines · Binbin Zhou

This patch describes the two MMC controllers of the Loongson-2K2000 SoC, one providing an eMMC interface and the other exporting an SD/SDIO interface.

Compared to the Loongson-2K1000's MMC controllers, their internals are similar, except that we use an internally exclusive DMA engine instead of an externally shared APBDMA engine.

[...]

Quoted 13 lines

drivers/mmc/host/loongson2-mmc.c static struct loongson2_mmc_pdata ls2k1000_mmc_pdata = { · line 765

Quoted 11 lines
drivers/mmc/host/loongson2-mmc.c +11 -0
+
+static void ls2k2000_mmc_fix_cmd_interrupt(struct loongson2_mmc_host *host,
+                                          struct mmc_command *cmd)
+{
+       int val;
+
+       if (cmd->opcode != MMC_WRITE_BLOCK && cmd->opcode != MMC_WRITE_MULTIPLE_BLOCK)
+               return;
+
+       regmap_read_poll_timeout(host->regmap, LOONGSON2_MMC_REG_FSTS, val,
+                                (val & LOONGSON2_MMC_FSTS_TXFULL), 0, 500);

Can you please elaborate on what goes on here?

This is a controller hardware issue. We need to wait until the Tx FIFO full flag is set before sending the write command. Otherwise, a data timeout will occur.

Quoted 10 lines

Note that, the mmc core uses a couple of different options to manage busy detection monitoring on DAT0, for those commands that need it.

*) MMC_CAP_WAIT_WHILE_BUSY - if the host HW and the driver for it supports IRQ based busy-detection.
*) host_ops->card_busy() callback if the HW can poll the DAT0 manually for busy-detection.
*) Polling by using CMD13.

Quoted 4 lines

[...]

Kind regards
Uffe

--
Thanks.
Binbin

#8 lore raw

Re: [PATCH v2 2/4] mmc: loongson2: Add Loongson-2K SD/SDIO controller driver

In reply to Ulf Hansson

Hi Ulf:

Sorry for the late reply.

Quoted 33 lines · Ulf Hansson
Quoted 9 lines · Binbin Zhou

The MMC controllers on the Loongson-2K series CPUs are similar, except for the interface characteristics and the use of DMA controllers.

This patch describes the MMC controllers on the Loongson-2K0500/2K1000, with the distinguishing feature being the use of an externally shared APBDMA engine.

[...]

drivers/mmc/host/loongson2-mmc.c line 445

Quoted 4 lines
drivers/mmc/host/loongson2-mmc.c new file +4 -0
+
+static void loongson2_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
+{
+       struct loongson2_mmc_host *host = mmc_priv(mmc);

As we now have support for regulators, we should use them here too.

Some something along the lines of this at MMC_POWER_OFF:
if (!IS_ERR(mmc->supply.vmmc))

      mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);

and at MMC_POWER_UP:
if (!IS_ERR(mmc->supply.vmmc))

      mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd);

drivers/mmc/host/loongson2-mmc.c line 448

Quoted 3 lines
drivers/mmc/host/loongson2-mmc.c new file +3 -0
+
+       switch (ios->power_mode) {
+       case MMC_POWER_ON:
Quoted 5 lines

Is the fallthrough really what we want here?

MMC_POWER_ON is used quite frequently when changing various ios settings when the core calls mmc_set_ios(). MMC_POWER_UP is set only once in mmc_power_up().

Indeed, this has been handled too roughly here. I will try to subdivide each case and add regulator support.

Quoted 42 lines
Quoted 21 lines
drivers/mmc/host/loongson2-mmc.c new file +20 -0
+       case MMC_POWER_UP:
+               regmap_write(host->regmap, LOONGSON2_MMC_REG_CTL, LOONGSON2_MMC_CTL_RESET);
+               mdelay(10);
+               regmap_write(host->regmap, LOONGSON2_MMC_REG_CTL, LOONGSON2_MMC_CTL_EXTCLK);
+               regmap_write(host->regmap, LOONGSON2_MMC_REG_INT, LOONGSON2_MMC_IEN_ALL);
+               regmap_write(host->regmap, LOONGSON2_MMC_REG_IEN, LOONGSON2_MMC_INT_CLEAR);
+               break;
+       case MMC_POWER_OFF:
+               regmap_update_bits(host->regmap, LOONGSON2_MMC_REG_CTL,
+                                  LOONGSON2_MMC_CTL_RESET, LOONGSON2_MMC_CTL_RESET);
+               return;
+       default:
+               return;
+       }
+
+       loongson2_mmc_set_clk(host, ios);
+
+       host->bus_width = ios->bus_width;
+}
+

[...]

Quoted 5 lines
drivers/mmc/host/loongson2-mmc.c new file +5 -0
+
+static void loongson2_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
+{
+       struct loongson2_mmc_host *host = mmc_priv(mmc);
+

drivers/mmc/host/loongson2-mmc.c line 491

Quoted 10 lines
drivers/mmc/host/loongson2-mmc.c new file +10 -0
+       regmap_update_bits(host->regmap, LOONGSON2_MMC_REG_IEN,
+                          LOONGSON2_MMC_INT_SDIOIRQ, enable);
+}
+
+static struct mmc_host_ops loongson2_mmc_ops = {
+       .request        = loongson2_mmc_request,
+       .set_ios        = loongson2_mmc_set_ios,
+       .get_ro         = mmc_gpio_get_ro,
+       .get_cd         = mmc_gpio_get_cd,
+       .enable_sdio_irq = loongson2_mmc_enable_sdio_irq,

The ->ack_sdio_irq() callback needs to be implemented too.

Quoted 1 line

Moreover we need to set MMC_CAP2_SDIO_IRQ_NOTHREAD.

OK..

Quoted 5 lines

[...]

Kind regards
Uffe

Signature · 2 lines
Thanks.
Binbin
#14 lore raw

Re: [PATCH v2 4/4] mmc: loongson2: Add Loongson-2K2000 SD/SDIO/eMMC controller driver

In reply to Binbin Zhou
Quoted 35 lines · Binbin Zhou

Hi Ulf:

Sorry for the late reply.

Quoted 15 lines · Ulf Hansson
Quoted 10 lines · Binbin Zhou

This patch describes the two MMC controllers of the Loongson-2K2000 SoC, one providing an eMMC interface and the other exporting an SD/SDIO interface.

Compared to the Loongson-2K1000's MMC controllers, their internals are similar, except that we use an internally exclusive DMA engine instead of an externally shared APBDMA engine.

[...]

Quoted 13 lines

drivers/mmc/host/loongson2-mmc.c static struct loongson2_mmc_pdata ls2k1000_mmc_pdata = { · line 765

Quoted 11 lines
drivers/mmc/host/loongson2-mmc.c +11 -0
+
+static void ls2k2000_mmc_fix_cmd_interrupt(struct loongson2_mmc_host *host,
+                                          struct mmc_command *cmd)
+{
+       int val;
+
+       if (cmd->opcode != MMC_WRITE_BLOCK && cmd->opcode != MMC_WRITE_MULTIPLE_BLOCK)
+               return;
+
+       regmap_read_poll_timeout(host->regmap, LOONGSON2_MMC_REG_FSTS, val,
+                                (val & LOONGSON2_MMC_FSTS_TXFULL), 0, 500);

Can you please elaborate on what goes on here?

Quoted 3 lines

This is a controller hardware issue. We need to wait until the Tx FIFO full flag is set before sending the write command. Otherwise, a data timeout will occur.

Okay, thanks for clarifying. Please add a comment in the code about this as I think it useful to know.

Moreover, I think we should have the value of the timeout specified in a define.

[...]

Kind regards
Uffe