Skip to contentSkip to conversation

Add mmc driver for Sunplus SP7021 SOC

PATCH Superseded Tony Huang 3 participants 5 messages Oct 30, 2022 – Nov 8, 2022

Subsystems (3)

v11 of 12 Superseded Latest is v12 →

lore
Share & tools
lore

Tony Huang

author maintainer
#0 lore raw

v11 cover Add mmc driver for Sunplus SP7021 SOC

This is a patch series for mmc driver for Sunplus SP7021 SOC.

Sunplus SP7021 is an ARM Cortex A7 (4 cores) based SoC. It integrates many peripherals (ex: UART, I2C, SPI, SDIO, eMMC, USB, SD card and etc.) into a single chip. It is designed for industrial control.

Refer to:
https://sunplus-tibbo.atlassian.net/wiki/spaces/doc/overview
https://tibbo.com/store/plus1.html

Tony Huang (2):
dt-binding: mmc: Add mmc yaml file for Sunplus SP7021
mmc: Add mmc driver for Sunplus SP7021

 .../devicetree/bindings/mmc/sunplus,mmc.yaml       |  62 ++
 MAINTAINERS                                        |   7 +
 drivers/mmc/host/Kconfig                           |   9 +
 drivers/mmc/host/Makefile                          |   1 +
 drivers/mmc/host/sunplus-mmc.c                     | 976 +++++++++++++++++++++
 5 files changed, 1055 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/mmc/sunplus,mmc.yaml
 create mode 100644 drivers/mmc/host/sunplus-mmc.c
Signature · 1 line
2.7.4
author maintainer
#1 lore raw

PATCH v11 1/2 dt-binding: mmc: Add mmc yaml file for Sunplus SP7021

Add MMC YAML file for Sunplus SP7021.

---
Changes in v5:
- Addressed comments from Krzysztof.

Changes in v6:
- Addressed comments from Krzysztof.
- To substitute MMC for mmc. To substitute YMAL for ymal.
- Remove max-frequency.
- Fixed wrong file name.

Changes in v7:
-No change.

Changes in v8:
-No change.

Changes in v9:
-No change.

Changes in v10:
-No change.

Changes in v11:
-No change.

 .../devicetree/bindings/mmc/sunplus,mmc.yaml       | 62 ++++++++++++++++++++++
 MAINTAINERS                                        |  6 +++
 2 files changed, 68 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/mmc/sunplus,mmc.yaml
Documentation/devicetree/bindings/mmc/sunplus,mmc.yaml new file +62 -0
new file mode 100644
index 0000000..50f2119
@@ -0,0 +1,62 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+# Copyright (C) Sunplus Ltd. Co. 2021
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/mmc/sunplus,mmc.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Sunplus MMC Controller
+
+maintainers:
+  - Tony Huang <[email protected]>
+  - Li-hao Kuo <[email protected]>
+
+allOf:
+  - $ref: "mmc-controller.yaml"
+
+properties:
+  compatible:
+    enum:
+      - sunplus,sp7021-mmc
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  clocks:
+    maxItems: 1
+
+  resets:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - clocks
+  - resets
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+    #include <dt-bindings/interrupt-controller/arm-gic.h>
+    mmc0: mmc@9c003b00 {
+        compatible = "sunplus,sp7021-mmc";
+        reg = <0x9c003b00 0x180>;
+        interrupts = <20 IRQ_TYPE_LEVEL_HIGH>;
+        clocks = <&clkc 0x4e>;
+        resets = <&rstc 0x3e>;
+        bus-width = <8>;
+        max-frequency = <52000000>;
+        non-removable;
+        disable-wp;
+        cap-mmc-highspeed;
+        mmc-ddr-3_3v;
+        no-sdio;
+        no-sd;
+    };
+
MAINTAINERS +6 -0
index fd768d4..cdd809a 100644
@@ -18869,6 +18869,12 @@ L:	[email protected]
 S:	Maintained
 F:	drivers/net/ethernet/dlink/sundance.c
 
+SUNPLUS MMC DRIVER
+M:	Tony Huang <[email protected]>
+M:	Li-hao Kuo <[email protected]>
+S:	Maintained
+F:	Documentation/devicetree/bindings/mmc/sunplus,mmc.yaml
+
 SUNPLUS OCOTP DRIVER
 M:	Vincent Shih <[email protected]>
 S:	Maintained
Signature · 1 line
2.7.4
#2 lore raw

Re: [PATCH v11 1/2] dt-binding: mmc: Add mmc yaml file for Sunplus SP7021

Quoted 7 lines · Tony Huang

Add MMC YAML file for Sunplus SP7021.

---
Changes in v5:
- Addressed comments from Krzysztof.

Please use scripts/get_maintainers.pl to get a list of necessary people and lists to CC. It might happen, that command when run on an older kernel, gives you outdated entries. Therefore please be sure you base your patches on recent Linux kernel.

Best regards,
Krzysztof

author maintainer
#3 lore raw

PATCH v11 2/2 mmc: Add mmc driver for Sunplus SP7021

In reply to Tony Huang

This is a patch for mmc driver for Sunplus SP7021 SOC. Supports eMMC 4.41 DDR 104MB/s speed mode.

---
Changes in v5:
- Addressed comments from Krzysztof.
- Remove the relevant sdcard code.
- Submit the emmc code only.

Changes in v6:
- Addressed comments from Arnd.
- Use SYSTEM_SLEEP_PM_OPS instead of SET_SYSTEM_SLEEP_PM_OPS.
- Use RUNTIME_PM_OPS instead of SET_RUNTIME_PM_OPS.
- Remove #ifdef check.
- Fixed mutex lock and unlock imbalance issue.

Changes in v7:
- Addressed comments from kernel test robot.
- Remove unused vairable.
- Add previous prototype.
- Addressed make error and warning in kernel 5.18-rc1.

Changes in v8:
- Addressed comments from Uffe.

Changes in v9:
- Addressed comments from Uffe.

Changes in v10:
- Addressed comments from Uffe.
- Addressed MMC test failed.

Changes in v11:
- Addressed comments from Arnd.
- Addressed comments from MMC test failed.

 MAINTAINERS                    |   1 +
 drivers/mmc/host/Kconfig       |   9 +
 drivers/mmc/host/Makefile      |   1 +
 drivers/mmc/host/sunplus-mmc.c | 976 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 987 insertions(+)
 create mode 100644 drivers/mmc/host/sunplus-mmc.c
MAINTAINERS +1 -0
index cdd809a..2439234 100644
@@ -18874,6 +18874,7 @@ M:	Tony Huang <[email protected]>
 M:	Li-hao Kuo <[email protected]>
 S:	Maintained
 F:	Documentation/devicetree/bindings/mmc/sunplus,mmc.yaml
+F:	drivers/mmc/host/sunplus-mmc.c
 
 SUNPLUS OCOTP DRIVER
 M:	Vincent Shih <[email protected]>
drivers/mmc/host/Kconfig +9 -0
index af6c3c3..d99ee3b 100644
@@ -14,6 +14,15 @@ config MMC_DEBUG
 	  added host drivers please don't invent their private macro for
 	  debugging.
 
+config MMC_SUNPLUS
+	tristate "Sunplus SP7021 MMC Controller"
+	depends on ARCH_SUNPLUS || COMPILE_TEST
+	help
+	  If you say yes here, you will get support for eMMC host interface
+	  on Sunplus SoCs.
+
+	  If unsure, say N
+
 config MMC_ARMMMCI
 	tristate "ARM AMBA Multimedia Card Interface support"
 	depends on ARM_AMBA
drivers/mmc/host/Makefile +1 -0
index 4e4ceb3..ba0c6d0 100644
@@ -97,6 +97,7 @@ obj-$(CONFIG_MMC_SDHCI_MICROCHIP_PIC32)	+= sdhci-pic32.o
 obj-$(CONFIG_MMC_SDHCI_BRCMSTB)		+= sdhci-brcmstb.o
 obj-$(CONFIG_MMC_SDHCI_OMAP)		+= sdhci-omap.o
 obj-$(CONFIG_MMC_SDHCI_SPRD)		+= sdhci-sprd.o
+obj-$(CONFIG_MMC_SUNPLUS)		+= sunplus-mmc.o
 obj-$(CONFIG_MMC_CQHCI)			+= cqhci.o
 cqhci-y					+= cqhci-core.o
 cqhci-$(CONFIG_MMC_CRYPTO)		+= cqhci-crypto.o
drivers/mmc/host/sunplus-mmc.c new file +976 -0

976 lines not on this page · raw message

Signature · 1 line
2.7.4
#4 lore raw

Re: [PATCH v11 2/2] mmc: Add mmc driver for Sunplus SP7021

Quoted 6 lines · Tony Huang

This is a patch for mmc driver for Sunplus SP7021 SOC. Supports eMMC 4.41 DDR 104MB/s speed mode.

Overall the code has improved a lot during the iterations, thanks for doing a good job around that!

Although, there are still a few things that I think deserve to be fixed before I am ready to apply this. Please have a look below.

[...]

Quoted 26 lines
drivers/mmc/host/sunplus-mmc.c new file +20 -0
new file mode 100644
index 0000000..d36c700
@@ -0,0 +1,976 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) Sunplus Inc.
+ * Author: Tony Huang <[email protected]>
+ * Author: Li-hao Kuo <[email protected]>
+ */
+
+#include <linux/bitfield.h>
+#include <linux/clk.h>
+#include <linux/delay.h>
+#include <linux/dma-mapping.h>
+#include <linux/interrupt.h>
+#include <linux/iopoll.h>
+#include <linux/mmc/core.h>
+#include <linux/mmc/host.h>
+#include <linux/mmc/mmc.h>
+#include <linux/mmc/sdio.h>
+#include <linux/mmc/slot-gpio.h>
+#include <linux/module.h>
+#include <linux/of.h>

drivers/mmc/host/sunplus-mmc.c line 30

Quoted 10 lines
drivers/mmc/host/sunplus-mmc.c new file +10 -0
+#include <linux/platform_device.h>
+#include <linux/pm.h>
+#include <linux/pm_runtime.h>
+#include <linux/reset.h>
+
+#define SPMMC_MIN_CLK                  400000
+#define SPMMC_MAX_CLK                  52000000
+#define SPMMC_MAX_BLK_COUNT            65536
+#define SPMMC_MAX_TUNABLE_DLY  7
+#define SPMMC_TIMEOUT                  500000

Would you mind renaming this to SPMMC_TIMEOUT_US, to easier understand its value.

[...]

drivers/mmc/host/sunplus-mmc.c line 168

Quoted 7 lines
drivers/mmc/host/sunplus-mmc.c new file +7 -0
+
+static inline int spmmc_wait_finish(struct spmmc_host *host)
+{
+       u32 state;
+
+       return readl_poll_timeout(host->base + SPMMC_SD_STATE_REG, state,
+                                       (state & SPMMC_SDSTATE_FINISH), 10, SPMMC_TIMEOUT);

Would you mind adding a definition for the 10us polling delay? Perhaps SPMMC_POLL_DELAY_US?

drivers/mmc/host/sunplus-mmc.c line 176

Quoted 8 lines
drivers/mmc/host/sunplus-mmc.c new file +8 -0
+}
+
+static inline int spmmc_wait_sdstatus(struct spmmc_host *host, unsigned int status_bit)
+{
+       u32 status;
+
+       return readl_poll_timeout(host->base + SPMMC_SD_STATUS_REG, status,
+                                       (status & status_bit), 10, SPMMC_TIMEOUT);

Ditto.

[...]

Quoted 10 lines
drivers/mmc/host/sunplus-mmc.c new file +10 -0
+static void spmmc_sw_reset(struct spmmc_host *host)
+{
+       u32 value;
+
+       /*
+        * Must reset dma operation first, or it will
+        * be stuck on sd_state == 0x1c00 because of
+        * a controller software reset bug
+        */
+       value = readl(host->base + SPMMC_HW_DMA_CTRL_REG);

drivers/mmc/host/sunplus-mmc.c line 341

Quoted 10 lines
drivers/mmc/host/sunplus-mmc.c new file +10 -0
+       value |= SPMMC_DMAIDLE;
+       writel(value, host->base + SPMMC_HW_DMA_CTRL_REG);
+       value &= ~SPMMC_DMAIDLE;
+       writel(value, host->base + SPMMC_HW_DMA_CTRL_REG);
+       value = readl(host->base + SPMMC_HW_DMA_CTRL_REG);
+       value |= SPMMC_HW_DMA_RST;
+       writel(value, host->base + SPMMC_HW_DMA_CTRL_REG);
+       writel(0x7, host->base + SPMMC_SD_RST_REG);
+       readl_poll_timeout_atomic(host->base + SPMMC_SD_HW_STATE_REG, value,
+                                 !(value & BIT(6)), 1, SPMMC_TIMEOUT);

As Arnd also pointed out earlier, we should strive to avoid polling in atomic context, unless it's really needed and then not use long timeouts.

If the above thing would have been the only case for this driver, I might have been okay with this. Especially, since I believe we should be able to tune the timeout value to something far less than the 500ms timeout for this reset case, don't you think?

That said, let's discuss the other cases below, to figure out how to move forward.

[...]

Quoted 6 lines
drivers/mmc/host/sunplus-mmc.c new file +6 -0
+static void spmmc_send_stop_cmd(struct spmmc_host *host)
+{
+       struct mmc_command stop = {};
+       u32 value;
+
+       stop.opcode = MMC_STOP_TRANSMISSION;

drivers/mmc/host/sunplus-mmc.c line 482

Quoted 10 lines
drivers/mmc/host/sunplus-mmc.c new file +10 -0
+       stop.arg = 0;
+       stop.flags = MMC_RSP_R1B;
+       spmmc_prepare_cmd(host, &stop);
+       value = readl(host->base + SPMMC_SD_INT_REG);
+       value &= ~SPMMC_SDINT_SDCMPEN;
+       value |= FIELD_PREP(SPMMC_SDINT_SDCMPEN, 0);
+       writel(value, host->base + SPMMC_SD_INT_REG);
+       spmmc_trigger_transaction(host);
+       readl_poll_timeout_atomic(host->base + SPMMC_SD_STATE_REG, value,
+                                 (value & SPMMC_SDSTATE_FINISH), 1, SPMMC_TIMEOUT);

This is problematic and even worse (from the polling point of view) if the controller would support HW-busy detection on DAT0, for R1B responses. Do you know if that is supported?

No matter what, polling for a response from a MMC_STOP_TRANSMISSION command should be avoided from within an atomic context. Simply, because the time we would need to poll may be rather long.

To fix this problem, I see in principle two ways to move forward. The best would be to wait for an IRQ to receive the response of the command, thus entirely avoiding the polling. Although, I guess that doesn't work for this HW - or does it?

The second best option is to poll from a non-atomic context. Now, by looking at the code in the IRQ handler spmmc_irq(), that seems rather easy to fix, as we should only need to switch to use the threaded RQ handler spmmc_func_finish_req() instead. Let me elaborate on that more below.

[...]

drivers/mmc/host/sunplus-mmc.c line 578

Quoted 1 line
drivers/mmc/host/sunplus-mmc.c new file +1 -0
+static inline int __find_best_delay(u8 candidate_dly)

Please be consistent with the prefix of the function names. I would prefer, spmmc_find_best_delay(), or something along those lines.

Quoted 13 lines
drivers/mmc/host/sunplus-mmc.c new file +13 -0
+{
+       int f, w, value;
+
+       if (!candidate_dly)
+               return 0;
+       f = ffs(candidate_dly) - 1;
+       w = hweight8(candidate_dly);
+       value = ((1 << w) - 1) << f;
+       if (0xff == (value & ~candidate_dly))
+               return (f + w / 2);
+       else
+               return (f);
+}

[...]

Quoted 27 lines
drivers/mmc/host/sunplus-mmc.c new file +27 -0
+static void spmmc_finish_request(struct spmmc_host *host, struct mmc_request *mrq)
+{
+       struct mmc_command *cmd;
+       struct mmc_data *data;
+
+       if (!mrq)
+               return;
+
+       cmd = mrq->cmd;
+       data = mrq->data;
+
+       if (data && SPMMC_DMA_MODE == host->dmapio_mode) {
+               dma_unmap_sg(host->mmc->parent, data->sg, data->sg_len, mmc_get_dma_dir(data));
+               host->dma_use_int = 0;
+       }
+
+       spmmc_get_rsp(host, cmd);
+       spmmc_check_error(host, mrq);
+       if (mrq->stop)
+               spmmc_send_stop_cmd(host);
+
+       host->mrq = NULL;
+       mmc_request_done(host->mmc, mrq);
+}
+
+/* Interrupt Service Routine */
+static irqreturn_t spmmc_irq(int irq, void *dev_id)

drivers/mmc/host/sunplus-mmc.c line 692

Quoted 10 lines
drivers/mmc/host/sunplus-mmc.c new file +10 -0
+{
+       struct spmmc_host *host = dev_id;
+       u32 value = readl(host->base + SPMMC_SD_INT_REG);
+
+       spin_lock(&host->lock);
+       if ((value & SPMMC_SDINT_SDCMP) && (value & SPMMC_SDINT_SDCMPEN)) {
+               value &= ~SPMMC_SDINT_SDCMPEN;
+               value |= SPMMC_SDINT_SDCMPCLR;
+               writel(value, host->base + SPMMC_SD_INT_REG);
+               spmmc_finish_request(host, host->mrq);

Instead of calling spmmc_finish_request() from here, we should be able to return IRQ_WAKE_THREAD to let the threaded handler spmmc_func_finish_req() to run instead.

As a matter of fact, it looks like the threaded handler spmmc_func_finish_req() is currently never being invoked. Or is it?

An even better option would be to drop the primary IRQ handler completely, but instead always use the threaded handler. This should also allow us to convert the spinlock into a mutex and enables us to move away from readl_poll_timeout_atomic() and use readl_poll_timeout() instead.

drivers/mmc/host/sunplus-mmc.c line 698

Quoted 6 lines
drivers/mmc/host/sunplus-mmc.c new file +6 -0
+       }
+       spin_unlock(&host->lock);
+
+       return IRQ_HANDLED;
+}
+

[...]

Kind regards
Uffe