Skip to contentSkip to conversation

mmc: Add LiteSDCard mmc driver

PATCH Superseded Gabriel Somlo 3 participants 18 messages Dec 15, 2021 – Jan 12, 2022

Subsystems (3)

v5 of 14 Superseded Latest is v14 →

lore
Share & tools
lore
#0 lore raw

v5 cover mmc: Add LiteSDCard mmc driver

Add support for the LiteX SD-Card device, LiteSDCard.

LiteSDCard is a simple SD-Card interface available as part of the LiteX environment, used with various RISC-V and other FPGA based SoCs.

New in v5:

MAINTAINERS:

- picked up a/b Mateusz

Doc/dt/bindings/mmc/litex,mmc.yaml:

- picked up r/b Rob, Joel

drivers/mmc/host/litex_mmc.c:

- shorten #define constant names (cosmetic, make them less unwieldy)
- picked up r/b Joel

Quoted 6 lines

New in v4:

Doc/dt/bindings/mmc/litex,mmc.yaml:

- fixed dt_binding_check errors uncovered by Rob's script

Quoted 65 lines

drivers/mmc/host/litex_mmc.c:

- struct litex_mmc_host fields re-ordered so that pahole reports no holes in either 32- or 64-bit builds
- litex_mmc_set_bus_width() now encapsulates check for host->is_bus_width_set
- litex_mmc_request() - factor out dma data setup into separate helper function: litex_mmc_do_dma()

Quoted 56 lines

New in v3:

MAINTAINERS:

- picked up acked-by Joel
- added listing for liteeth driver
- added Joel as additional co-maintainer (thanks!)

Doc/dt/bindings/mmc/litex,mmc.yaml:

- picked up r/b Geert Uytterhoeven <[email protected]> in DT bindings document (please let me know if that was premature, and happy to take further review if needed :)
- add dedicated DT property for source clock frequency

drivers/mmc/host/litex_mmc.c:

- fixed function signature (no line split), and naming (litex_mmc_*)
- more informative MODULE_AUTHOR() entries
- also added matching "Copyright" entries in file header
- fixed description in Kconfig
- fixed DT documentation
- removed magic constants
- removed litex_map_status(), have sdcard_wait_done() return *real* error codes directly instead.
- streamlined litex_mmc_reponse_len()
- call litex_mmc_set_bus_width() only once, and ensure it returns correct error code(s)
- use readx_poll_timeout() -- more concise -- instead of read_poll_timeout()
- use dev_err() in litex_mmc_send_cmd() (instead of pr_err())
- litex_mmc_setclk() will update host->clock before returning
- separate irq initialization into its own function, litex_mmc_irq_init()
- document rationale for f_min, f_max
- use dmam_alloc_coherent(), which simplifies cleanup significantly
- large if (data) { ... } block in litex_mmc_request() left as-is, there are too many variables shared with the rest of the parent function body to easily separate (e.g., len, transfer, direct). If this is indeed a blocker, I can take another shot at refactoring it in a future revision!
- bump dma_set_mask_and_coherent() to 64-bits on suitable architectures
- clock source picked up from dedicated DT clock reference property
- remove gpio card-detect logic (needs testing and a dt binding example before being eligible for upstream inclusion)

Quoted 9 lines

New in v2:
- reword info message in litex_set_clk()
- streamline code in litex_map_status()
- fix typos in Kconfig (thanks Randy Dunlap <[email protected]>)
- improvements suggested by Stafford Horne <[email protected]>
- allow COMPILE_TEST in Kconfig
- use read_poll_timeout() when waiting for cmd/data/DMA xfer completion
- include interrupt.h (thanks kernel test robot <[email protected]>)

Gabriel Somlo (3):
MAINTAINERS: co-maintain LiteX platform
dt-bindings: mmc: Add bindings for LiteSDCard
mmc: Add driver for LiteX's LiteSDCard interface

 .../devicetree/bindings/mmc/litex,mmc.yaml    |  72 ++
 MAINTAINERS                                   |   9 +-
 drivers/mmc/host/Kconfig                      |   9 +
 drivers/mmc/host/Makefile                     |   1 +
 drivers/mmc/host/litex_mmc.c                  | 652 ++++++++++++++++++
 5 files changed, 741 insertions(+), 2 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/mmc/litex,mmc.yaml
 create mode 100644 drivers/mmc/host/litex_mmc.c
Signature · 1 line
2.31.1
#1 lore raw

PATCH v5 1/3 MAINTAINERS: co-maintain LiteX platform

Add the litex_mmc (LiteSDCard) and LiteETH drivers to the list of files maintained under LiteX.

Add Gabriel Somlo and Joel Stanley as maintainers; Joel authored the LiteETH driver, and Gabriel is currently curating the LiteX out-of-tree device drivers as they are tested and prepared for upstream submission, having also co-authored a number of them.

---

New in v5:
- picked up acked-by Mateusz

Quoted 7 lines

New in v4:
- n/a

Quoted 4 lines

New in v3:
- picked up acked-by Joel
- added listing for liteeth driver
- added Joel as additional co-maintainer (thanks!)

 MAINTAINERS | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
MAINTAINERS +7 -2
index 13f9a84a617e..fbbeb6281da5 100644
@@ -11011,12 +11011,17 @@ F:	lib/list-test.c
 LITEX PLATFORM
 M:	Karol Gugala <[email protected]>
 M:	Mateusz Holenko <[email protected]>
+M:	Gabriel Somlo <[email protected]>
+M:	Joel Stanley <[email protected]>
 S:	Maintained
 F:	Documentation/devicetree/bindings/*/litex,*.yaml
 F:	arch/openrisc/boot/dts/or1klitex.dts
-F:	drivers/soc/litex/litex_soc_ctrl.c
-F:	drivers/tty/serial/liteuart.c
 F:	include/linux/litex.h
+F:	drivers/tty/serial/liteuart.c
+F:	drivers/soc/litex/*
+F:	drivers/net/ethernet/litex/*
+F:	drivers/mmc/host/litex_mmc.c
+N:	litex
 
 LIVE PATCHING
 M:	Josh Poimboeuf <[email protected]>
Signature · 1 line
2.31.1
#2 lore raw

PATCH v5 2/3 dt-bindings: mmc: Add bindings for LiteSDCard

In reply to Gabriel Somlo

LiteSDCard is a small footprint, configurable SDCard core forFPGA based system on chips.

---

New in v5:
- picked up reviewd-by Rob, Joel

Quoted 8 lines

New in v4:
- fixed dt_binding_check errors uncovered by Rob's script

Quoted 5 lines

New in v3:
- picked up r/b Geert Uytterhoeven <[email protected]> in DT bindings document (please let me know if that was premature, and happy to take further review if needed :)
- add dedicated DT property for source clock frequency

 .../devicetree/bindings/mmc/litex,mmc.yaml    | 72 +++++++++++++++++++
 1 file changed, 72 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/mmc/litex,mmc.yaml
Documentation/devicetree/bindings/mmc/litex,mmc.yaml new file +72 -0
new file mode 100644
index 000000000000..f57cf42b8db7
@@ -0,0 +1,72 @@
+# SPDX-License-Identifier: GPL-2.0-or-later OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/mmc/litex,mmc.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: LiteX LiteSDCard device
+
+maintainers:
+  - Gabriel Somlo <[email protected]>
+
+description: |
+  LiteSDCard is a small footprint, configurable SDCard core for FPGA based
+  system on chips.
+
+  The hardware source is Open Source and can be found on at
+  https://github.com/enjoy-digital/litesdcard/.
+
+allOf:
+  - $ref: mmc-controller.yaml#
+
+properties:
+  compatible:
+    const: litex,mmc
+
+  reg:
+    items:
+      - description: PHY registers
+      - description: CORE registers
+      - description: DMA Reader buffer
+      - description: DMA Writer buffer
+      - description: IRQ registers
+    minItems: 4
+
+  reg-names:
+    items:
+      - const: phy
+      - const: core
+      - const: reader
+      - const: writer
+      - const: irq
+    minItems: 4
+
+  clocks:
+    maxItems: 1
+    description:
+      Handle to reference clock.
+
+  interrupts:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+  - reg-names
+  - clocks
+
+additionalProperties: false
+
+examples:
+  - |
+    mmc: mmc@12005000 {
+        compatible = "litex,mmc";
+        reg = <0x12005000 0x100>,
+              <0x12003800 0x100>,
+              <0x12003000 0x100>,
+              <0x12004800 0x100>,
+              <0x12004000 0x100>;
+        reg-names = "phy", "core", "reader", "writer", "irq";
+        clocks = <&reference_clk>;
+        interrupts = <4>;
+    };
Signature · 1 line
2.31.1
#3 lore raw

PATCH v5 3/3 mmc: Add driver for LiteX's LiteSDCard interface

In reply to Gabriel Somlo

LiteX (https://github.com/enjoy-digital/litex) is a SoC framework that targets FPGAs. LiteSDCard is a small footprint, configurable SDCard core commonly used in LiteX designs.

The driver was first written in May 2020 and has been maintained cooperatively by the LiteX community. Thanks to all contributors!

Cc Mateusz Holenko <[email protected]>
Cc Karol Gugala <[email protected]>
Cc Joel Stanley <[email protected]>
Cc Stafford Horne <[email protected]>
Cc Geert Uytterhoeven <[email protected]>
Cc David Abdurachmanov <[email protected]>
Cc Florent Kermarrec <[email protected]>

---

New in v5:
- shorter #define constant names (cosmetic, make them less unwieldy)
- picked up reviewed-by Joel

Quoted 37 lines

New in v4:
- struct litex_mmc_host fields re-ordered so that pahole reports no holes on either 32- or 64-bit builds
- litex_mmc_set_bus_width() now encapsulates check for host->is_bus_width_set
- litex_mmc_request() - factor out dma data setup into separate helper function: litex_mmc_do_dma()

Quoted 29 lines

New in v3:
- fixed function signature (no line split), and naming (litex_mmc_*)
- more informative MODULE_AUTHOR() entries
- also added matching "Copyright" entries in file header
- fixed description and dependencies in Kconfig
- removed magic constants
- removed litex_map_status(), have sdcard_wait_done() return *real* error codes directly instead.
- streamlined litex_mmc_reponse_len()
- call litex_mmc_set_bus_width() only once, and ensure it returns correct error code(s)
- use readx_poll_timeout() -- more concise -- instead of read_poll_timeout()
- use dev_err() in litex_mmc_send_cmd() (instead of pr_err())
- litex_mmc_setclk() will update host->clock before returning
- separate irq initialization into its own function, litex_mmc_irq_init()
- document rationale for f_min, f_max
- use dmam_alloc_coherent(), which simplifies cleanup significantly
- bump dma_set_mask_and_coherent() to 64-bits on suitable architectures
- clock source picked up from dedicated DT clock reference property
- remove gpio card-detect logic (needs testing and a dt binding example before being eligible for upstream inclusion)
- large if (data) { ... } block in litex_mmc_request() left as-is, there are too many variables shared with the rest of the parent function body to easily separate (e.g., len, transfer, direct). If this is indeed a blocker, I can take another shot at refactoring it in a future revision!

 drivers/mmc/host/Kconfig     |   9 +
 drivers/mmc/host/Makefile    |   1 +
 drivers/mmc/host/litex_mmc.c | 652 +++++++++++++++++++++++++++++++++++
 3 files changed, 662 insertions(+)
 create mode 100644 drivers/mmc/host/litex_mmc.c
drivers/mmc/host/Kconfig +9 -0
index 5af8494c31b5..c1b66d06d1c9 100644
@@ -1093,3 +1093,12 @@ config MMC_OWL
 
 config MMC_SDHCI_EXTERNAL_DMA
 	bool
+
+config MMC_LITEX
+	tristate "LiteX MMC Host Controller support"
+	depends on OF
+	depends on PPC_MICROWATT || LITEX || COMPILE_TEST
+	help
+	  This selects support for the MMC Host Controller found in LiteX SoCs.
+
+	  If unsure, say N.
drivers/mmc/host/Makefile +1 -0
index ea36d379bd3c..4e4ceb32c4b4 100644
@@ -101,6 +101,7 @@ obj-$(CONFIG_MMC_CQHCI)			+= cqhci.o
 cqhci-y					+= cqhci-core.o
 cqhci-$(CONFIG_MMC_CRYPTO)		+= cqhci-crypto.o
 obj-$(CONFIG_MMC_HSQ)			+= mmc_hsq.o
+obj-$(CONFIG_MMC_LITEX)			+= litex_mmc.o
 
 ifeq ($(CONFIG_CB710_DEBUG),y)
 	CFLAGS-cb710-mmc	+= -DDEBUG
drivers/mmc/host/litex_mmc.c new file +652 -0

652 lines not on this page · raw message

Signature · 1 line
2.31.1
#4 lore raw

Re: [PATCH v5 3/3] mmc: Add driver for LiteX's LiteSDCard interface

Quoted 7 lines · Gabriel Somlo

LiteX (https://github.com/enjoy-digital/litex) is a SoC framework that targets FPGAs. LiteSDCard is a small footprint, configurable SDCard core commonly used in LiteX designs.

The driver was first written in May 2020 and has been maintained cooperatively by the LiteX community. Thanks to all contributors!

...

drivers/mmc/host/litex_mmc.c line 470

Quoted 7 lines
drivers/mmc/host/litex_mmc.c new file +7 -0
+       int ret;
+
+       host->irq = platform_get_irq_optional(host->dev, 0);
+       if (host->irq <= 0) {
+               dev_warn(dev, "Failed to get IRQ, using polling\n");
+               goto use_polling;
+       }

Same comment as per v3.

...

Quoted 1 line

+#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT

Why under ifdeffery?

Quoted 5 lines
drivers/mmc/host/litex_mmc.c new file +5 -0
+       /* increase from default 32 on 64-bit-DMA capable architectures */
+       ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
+       if (ret)
+               goto err;
+#endif
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw

Re: [PATCH v5 3/3] mmc: Add driver for LiteX's LiteSDCard interface

Hi Andy,

Thanks for the feedback!

Quoted 25 lines · Andy Shevchenko
Quoted 7 lines · Gabriel Somlo

LiteX (https://github.com/enjoy-digital/litex) is a SoC framework that targets FPGAs. LiteSDCard is a small footprint, configurable SDCard core commonly used in LiteX designs.

The driver was first written in May 2020 and has been maintained cooperatively by the LiteX community. Thanks to all contributors!

...

drivers/mmc/host/litex_mmc.c line 470

Quoted 7 lines
drivers/mmc/host/litex_mmc.c new file +7 -0
+       int ret;
+
+       host->irq = platform_get_irq_optional(host->dev, 0);
+       if (host->irq <= 0) {
+               dev_warn(dev, "Failed to get IRQ, using polling\n");
+               goto use_polling;
+       }

[Same comment as per v3.]

This is wrong. It missed the deferred probe, for example.

The best approach is

Quoted 8 lines

ret = platform_get_irq_optional(...);
if (ret < 0 && ret != -ENXIO)
return ret;
if (ret > 0)
...we got it...

It will allow the future API fix of platform_get_irq_optional() to be really optional.

Thanks for the example. I still need to work in a decision to use polling, though. How about something like this instead:

ret = platform_get_irq_optional(...);
if (ret == -ENXIO)
goto use_polling;
if (ret < 0)
return ret; // deferred probe (-EAGAIN likely?)
if (ret > 0)
...we got it, keep going...

Quoted 6 lines

...

Quoted 1 line

+#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT

Why under ifdeffery?

Because I only want to do it on 64-bit capable architectures.

The alternative would be to call

  dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));

on *all* architectures, but ignore the returned error (-EIO, presumably on architetures that only support 32-bit DMA).

Do you think that would be cleaner?

Thanks,
--Gabriel

Quoted 8 lines

drivers/mmc/host/litex_mmc.c line 537

Quoted 5 lines
drivers/mmc/host/litex_mmc.c new file +5 -0
+       /* increase from default 32 on 64-bit-DMA capable architectures */
+       ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
+       if (ret)
+               goto err;
+#endif

With Best Regards,
Andy Shevchenko

#6 lore raw

Re: [PATCH v5 3/3] mmc: Add driver for LiteX's LiteSDCard interface

Quoted 80 lines · Gabriel L. Somlo

Hi Andy,

Thanks for the feedback!

Quoted 25 lines · Andy Shevchenko
Quoted 7 lines · Gabriel Somlo

LiteX (https://github.com/enjoy-digital/litex) is a SoC framework that targets FPGAs. LiteSDCard is a small footprint, configurable SDCard core commonly used in LiteX designs.

The driver was first written in May 2020 and has been maintained cooperatively by the LiteX community. Thanks to all contributors!

...

drivers/mmc/host/litex_mmc.c line 470

Quoted 7 lines
drivers/mmc/host/litex_mmc.c new file +7 -0
+       int ret;
+
+       host->irq = platform_get_irq_optional(host->dev, 0);
+       if (host->irq <= 0) {
+               dev_warn(dev, "Failed to get IRQ, using polling\n");
+               goto use_polling;
+       }

[Same comment as per v3.]

This is wrong. It missed the deferred probe, for example.

The best approach is

Quoted 8 lines

ret = platform_get_irq_optional(...);
if (ret < 0 && ret != -ENXIO)
return ret;
if (ret > 0)
...we got it...

It will allow the future API fix of platform_get_irq_optional() to be really optional.

Thanks for the example. I still need to work in a decision to use polling, though. How about something like this instead:

ret = platform_get_irq_optional(...);
if (ret == -ENXIO)
goto use_polling;
if (ret < 0)
return ret; // deferred probe (-EAGAIN likely?)
if (ret > 0)
...we got it, keep going...

Quoted 6 lines

...

Quoted 1 line

+#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT

Why under ifdeffery?

Because I only want to do it on 64-bit capable architectures.

The alternative would be to call

  dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));

on *all* architectures, but ignore the returned error (-EIO, presumably on architetures that only support 32-bit DMA).

Do you think that would be cleaner?

Thanks,
--Gabriel

Quoted 8 lines

drivers/mmc/host/litex_mmc.c line 537

Quoted 5 lines
drivers/mmc/host/litex_mmc.c new file +5 -0
+       /* increase from default 32 on 64-bit-DMA capable architectures */
+       ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
+       if (ret)
+               goto err;
+#endif

With Best Regards,
Andy Shevchenko

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw

Re: [PATCH v5 3/3] mmc: Add driver for LiteX's LiteSDCard interface

In reply to Gabriel L. Somlo
Quoted 2 lines · Gabriel L. Somlo
Quoted 1 line · Andy Shevchenko

On Wed, Dec 15, 2021 at 10:00 PM Gabriel Somlo <[email protected]> wrote:

...

Quoted 16 lines
Quoted 4 lines

This is wrong. It missed the deferred probe, for example.

The best approach is

Quoted 8 lines

ret = platform_get_irq_optional(...);
if (ret < 0 && ret != -ENXIO)
return ret;
if (ret > 0)
...we got it...

It will allow the future API fix of platform_get_irq_optional() to be really optional.

Thanks for the example. I still need to work in a decision to use polling, though. How about something like this instead:

Quoted 7 lines

ret = platform_get_irq_optional(...);
if (ret == -ENXIO)
goto use_polling;
if (ret < 0)
return ret; // deferred probe (-EAGAIN likely?)
if (ret > 0)
...we got it, keep going...

This doesn't define what you should do when you get 0. I suggest to take my variant with below modification

if (ret > 0)
...we have IRQ...
else
goto USE POLLING;

It will take care of the case.

...

Quoted 4 lines
Quoted 3 lines
Quoted 1 line

+#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT

Why under ifdeffery?

Quoted 8 lines

Because I only want to do it on 64-bit capable architectures.

The alternative would be to call

  dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));

on *all* architectures, but ignore the returned error (-EIO, presumably on architetures that only support 32-bit DMA).

I don't understand why you are supposed to ignore errors and why you expect to get such.

Quoted 1 line

Do you think that would be cleaner?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#8 lore raw

Re: [PATCH v5 3/3] mmc: Add driver for LiteX's LiteSDCard interface

Quoted 33 lines · Andy Shevchenko
Quoted 2 lines · Gabriel L. Somlo
Quoted 1 line · Andy Shevchenko

On Wed, Dec 15, 2021 at 10:00 PM Gabriel Somlo <[email protected]> wrote:

...

Quoted 16 lines
Quoted 4 lines

This is wrong. It missed the deferred probe, for example.

The best approach is

Quoted 8 lines

ret = platform_get_irq_optional(...);
if (ret < 0 && ret != -ENXIO)
return ret;
if (ret > 0)
...we got it...

It will allow the future API fix of platform_get_irq_optional() to be really optional.

Thanks for the example. I still need to work in a decision to use polling, though. How about something like this instead:

Quoted 7 lines

ret = platform_get_irq_optional(...);
if (ret == -ENXIO)
goto use_polling;
if (ret < 0)
return ret; // deferred probe (-EAGAIN likely?)
if (ret > 0)
...we got it, keep going...

This doesn't define what you should do when you get 0. I suggest to take my variant with below modification

Quoted 6 lines

if (ret > 0)
...we have IRQ...
else
goto USE POLLING;

It will take care of the case.

OK, will do.

Quoted 10 lines

...

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

+#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT

Why under ifdeffery?

Because I only want to do it on 64-bit capable architectures.

The alternative would be to call

Quoted 7 lines
Quoted 4 lines
  dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));

on *all* architectures, but ignore the returned error (-EIO, presumably on architetures that only support 32-bit DMA).

I don't understand why you are supposed to ignore errors and why you expect to get such.

If I call dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); on a machine where CONFIG_ARCH_DMA_ADDR_T_64BIT is *not* set, I expect an error. The implicit default
(per Documentation/core-api/dma-api-howto.rst), is DMA_BIT_MASK(32). I'm working under the impression that on machines with CONFIG_ARCH_DMA_ADDR_T_64BIT I should increase that to DMA_BIT_MASK(64).

So if I don't #ifdef it, that call will fail on machines supporting only 32-bits.

What am I missing?

Thanks,
--Gabriel

Quoted 5 lines
Quoted 1 line

Do you think that would be cleaner?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#9 lore raw

Re: [PATCH v5 3/3] mmc: Add driver for LiteX's LiteSDCard interface

Quoted 4 lines · Gabriel L. Somlo
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Gabriel L. Somlo
Quoted 1 line · Andy Shevchenko

On Wed, Dec 15, 2021 at 10:00 PM Gabriel Somlo <[email protected]> wrote:

...

Quoted 23 lines
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines
Quoted 1 line

+#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT

Why under ifdeffery?

Because I only want to do it on 64-bit capable architectures.

The alternative would be to call

Quoted 7 lines
Quoted 4 lines
  dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));

on *all* architectures, but ignore the returned error (-EIO, presumably on architetures that only support 32-bit DMA).

I don't understand why you are supposed to ignore errors and why you expect to get such.

If I call dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); on a machine where CONFIG_ARCH_DMA_ADDR_T_64BIT is *not* set, I expect an error. The implicit default
(per Documentation/core-api/dma-api-howto.rst), is DMA_BIT_MASK(32). I'm working under the impression that on machines with CONFIG_ARCH_DMA_ADDR_T_64BIT I should increase that to DMA_BIT_MASK(64).

Quoted 4 lines

So if I don't #ifdef it, that call will fail on machines supporting only 32-bits.

What am I missing?

This thread: https://lkml.org/lkml/2021/6/7/398 ?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#10 lore raw

Re: [PATCH v5 3/3] mmc: Add driver for LiteX's LiteSDCard interface

Quoted 31 lines · Andy Shevchenko
Quoted 4 lines · Gabriel L. Somlo
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Gabriel L. Somlo
Quoted 1 line · Andy Shevchenko

On Wed, Dec 15, 2021 at 10:00 PM Gabriel Somlo <[email protected]> wrote:

...

Quoted 23 lines
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines
Quoted 1 line

+#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT

Why under ifdeffery?

Because I only want to do it on 64-bit capable architectures.

The alternative would be to call

Quoted 7 lines
Quoted 4 lines
  dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));

on *all* architectures, but ignore the returned error (-EIO, presumably on architetures that only support 32-bit DMA).

I don't understand why you are supposed to ignore errors and why you expect to get such.

If I call dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); on a machine where CONFIG_ARCH_DMA_ADDR_T_64BIT is *not* set, I expect an error. The implicit default
(per Documentation/core-api/dma-api-howto.rst), is DMA_BIT_MASK(32). I'm working under the impression that on machines with CONFIG_ARCH_DMA_ADDR_T_64BIT I should increase that to DMA_BIT_MASK(64).

Quoted 6 lines
Quoted 4 lines

So if I don't #ifdef it, that call will fail on machines supporting only 32-bits.

What am I missing?

This thread: https://lkml.org/lkml/2021/6/7/398 ?

OK, so just call dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); regardless of 32- or 64-bit dma capability, *do* check the return value, and it should *not* fail on 32-bit systems.

I'll do that in v6 (should go out in early January '22, since I'm traveling with only occasional email access at the moment).

Thanks,
--Gabriel

#11 lore raw

Re: [PATCH v5 3/3] mmc: Add driver for LiteX's LiteSDCard interface

In reply to Gabriel Somlo
Quoted 23 lines · Gabriel Somlo

LiteX (https://github.com/enjoy-digital/litex) is a SoC framework that targets FPGAs. LiteSDCard is a small footprint, configurable SDCard core commonly used in LiteX designs.

The driver was first written in May 2020 and has been maintained cooperatively by the LiteX community. Thanks to all contributors!

Cc Mateusz Holenko <[email protected]>
Cc Karol Gugala <[email protected]>
Cc Joel Stanley <[email protected]>
Cc Stafford Horne <[email protected]>
Cc Geert Uytterhoeven <[email protected]>
Cc David Abdurachmanov <[email protected]>
Cc Florent Kermarrec <[email protected]>

[...]

Quoted 18 lines
drivers/mmc/host/litex_mmc.c new file +18 -0
+
+static int litex_mmc_set_bus_width(struct litex_mmc_host *host)
+{
+       bool app_cmd_sent;
+       int ret;
+
+       if (host->is_bus_width_set)
+               return 0;
+
+       /* ensure 'app_cmd' precedes 'app_set_bus_width_cmd' */
+       app_cmd_sent = host->app_cmd; /* was preceding command app_cmd? */
+       if (!app_cmd_sent) {
+               ret = litex_mmc_send_app_cmd(host);
+               if (ret)
+                       return ret;
+       }
+
+       /* litesdcard only supports 4-bit bus width */

drivers/mmc/host/litex_mmc.c line 226

Quoted 10 lines
drivers/mmc/host/litex_mmc.c new file +10 -0
+       ret = litex_mmc_send_set_bus_w_cmd(host, MMC_BUS_WIDTH_4);
+       if (ret)
+               return ret;
+
+       /* re-send 'app_cmd' if necessary */
+       if (app_cmd_sent) {
+               ret = litex_mmc_send_app_cmd(host);
+               if (ret)
+                       return ret;
+       }

I understand that you are trying to address the limitation that the controller supports 4-bit width only. In principle it looks like we may need to violate the SD spec to be able to initialise an SD card, right? Isn't that a concern for you?

Quoted 5 lines
drivers/mmc/host/litex_mmc.c new file +5 -0
+
+       host->is_bus_width_set = true;
+
+       return 0;
+}

[...]

Quoted 29 lines
drivers/mmc/host/litex_mmc.c new file +29 -0
+
+static void litex_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
+{
+       struct litex_mmc_host *host = mmc_priv(mmc);
+       struct device *dev = mmc_dev(mmc);
+       struct mmc_command *cmd = mrq->cmd;
+       struct mmc_command *sbc = mrq->sbc;
+       struct mmc_data *data = mrq->data;
+       struct mmc_command *stop = mrq->stop;
+       unsigned int retries = cmd->retries;
+       unsigned int len = 0;
+       bool direct = false;
+       u32 response_len = litex_mmc_response_len(cmd);
+       u8 transfer = SD_CTL_DATA_XFER_NONE;
+
+       /* First check that the card is still there */
+       if (!litex_mmc_get_cd(mmc)) {
+               cmd->error = -ENOMEDIUM;
+               mmc_request_done(mmc, mrq);
+               return;
+       }
+
+       /* Send set-block-count command if needed */
+       if (sbc) {
+               sbc->error = litex_mmc_send_cmd(host, sbc->opcode, sbc->arg,
+                                               litex_mmc_response_len(sbc),
+                                               SD_CTL_DATA_XFER_NONE);
+               if (sbc->error) {
+                       host->is_bus_width_set = false;

drivers/mmc/host/litex_mmc.c line 368

Quoted 10 lines
drivers/mmc/host/litex_mmc.c new file +10 -0
+                       mmc_request_done(mmc, mrq);
+                       return;
+               }
+       }
+
+       if (data) {
+               /* LiteSDCard only supports 4-bit bus width; therefore, we MUST
+                * inject a SET_BUS_WIDTH (acmd6) before the very first data
+                * transfer, earlier than when the mmc subsystem would normally
+                * get around to it!

This means that you may end up trying to switch bus-width, to a width that isn't supported by the card, for example.

As also stated above, I wonder how this conforms to the SD spec from the initialization sequence point of view. Have you verified that this isn't a problem?

Quoted 57 lines
drivers/mmc/host/litex_mmc.c new file +57 -0
+                */
+               cmd->error = litex_mmc_set_bus_width(host);
+               if (cmd->error) {
+                       dev_err(dev, "Can't set bus width!\n");
+                       mmc_request_done(mmc, mrq);
+                       return;
+               }
+
+               litex_mmc_do_dma(host, data, &len, &direct, &transfer);
+       }
+
+       do {
+               cmd->error = litex_mmc_send_cmd(host, cmd->opcode, cmd->arg,
+                                               response_len, transfer);
+       } while (cmd->error && retries-- > 0);
+
+       if (cmd->error) {
+               /* card may be gone; don't assume bus width is still set */
+               host->is_bus_width_set = false;
+       }
+
+       if (response_len == SD_CTL_RESP_SHORT) {
+               /* pull short response fields from appropriate host registers */
+               cmd->resp[0] = host->resp[3];
+               cmd->resp[1] = host->resp[2] & 0xFF;
+       } else if (response_len == SD_CTL_RESP_LONG) {
+               cmd->resp[0] = host->resp[0];
+               cmd->resp[1] = host->resp[1];
+               cmd->resp[2] = host->resp[2];
+               cmd->resp[3] = host->resp[3];
+       }
+
+       /* Send stop-transmission command if required */
+       if (stop && (cmd->error || !sbc)) {
+               stop->error = litex_mmc_send_cmd(host, stop->opcode, stop->arg,
+                                                litex_mmc_response_len(stop),
+                                                SD_CTL_DATA_XFER_NONE);
+               if (stop->error)
+                       host->is_bus_width_set = false;
+       }
+
+       if (data) {
+               dma_unmap_sg(dev, data->sg, data->sg_len,
+                            mmc_get_dma_dir(data));
+       }
+
+       if (!cmd->error && transfer != SD_CTL_DATA_XFER_NONE) {
+               data->bytes_xfered = min(len, mmc->max_req_size);
+               if (transfer == SD_CTL_DATA_XFER_READ && !direct) {
+                       sg_copy_from_buffer(data->sg, sg_nents(data->sg),
+                                           host->buffer, data->bytes_xfered);
+               }
+       }
+
+       mmc_request_done(mmc, mrq);
+}
+

[...]

drivers/mmc/host/litex_mmc.c line 580

Quoted 2 lines
drivers/mmc/host/litex_mmc.c new file +2 -0
+
+       mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;

I noticed that you use these hard coded values and don't really care to manage voltage changes via ->set_ios().

Rather than doing it like this, I would prefer if you can hook up a fixed vmmc regulator in the DTS. Then call mmc_regulator_get_supply() to fetch it from here, which will let the mmc core create the mmc->ocr_avail mask, based upon the voltage level the regulator supports.

This becomes more generic and allows more flexibility for the platform configuration.

Quoted 12 lines
drivers/mmc/host/litex_mmc.c new file +12 -0
+       mmc->ops = &litex_mmc_ops;
+
+       /* set default sd_clk frequency range based on empirical observations
+        * of LiteSDCard gateware behavior on typical SDCard media
+        */
+       mmc->f_min = 12.5e6;
+       mmc->f_max = 50e6;
+
+       ret = mmc_of_parse(mmc);
+       if (ret)
+               goto err;
+

drivers/mmc/host/litex_mmc.c line 602

Quoted 10 lines
drivers/mmc/host/litex_mmc.c new file +10 -0
+       /* force 4-bit bus_width (only width supported by hardware) */
+       mmc->caps &= ~MMC_CAP_8_BIT_DATA;
+       mmc->caps |= MMC_CAP_4_BIT_DATA;
+
+       /* set default capabilities */
+       mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY |
+                    MMC_CAP_DRIVER_TYPE_D |
+                    MMC_CAP_CMD23;
+       mmc->caps2 |= MMC_CAP2_NO_WRITE_PROTECT |
+                     MMC_CAP2_FULL_PWR_CYCLE |

A full power cycle requires you to be able to power on/off the vmmc regulator (unless this is internally managed by the controller). Can you really do that?

Quoted 1 line

+ MMC_CAP2_NO_SDIO;

We should add MMC_CAP2_NO_MMC here as well, as it looks like it can't be supported. Right?

Quoted 40 lines
drivers/mmc/host/litex_mmc.c new file +40 -0
+
+       platform_set_drvdata(pdev, host);
+
+       ret = mmc_add_host(mmc);
+       if (ret < 0)
+               goto err;
+
+       return 0;
+
+err:
+       mmc_free_host(mmc);
+       return ret;
+}
+
+static int litex_mmc_remove(struct platform_device *pdev)
+{
+       struct litex_mmc_host *host = dev_get_drvdata(&pdev->dev);
+
+       if (host->irq > 0)
+               free_irq(host->irq, host->mmc);
+       mmc_remove_host(host->mmc);
+       mmc_free_host(host->mmc);
+
+       return 0;
+}
+
+static const struct of_device_id litex_match[] = {
+       { .compatible = "litex,mmc" },
+       { }
+};
+MODULE_DEVICE_TABLE(of, litex_match);
+
+static struct platform_driver litex_mmc_driver = {
+       .probe = litex_mmc_probe,
+       .remove = litex_mmc_remove,
+       .driver = {
+               .name = "litex-mmc",
+               .of_match_table = of_match_ptr(litex_match),
+       },
+};

drivers/mmc/host/litex_mmc.c

Quoted 10 lines
drivers/mmc/host/litex_mmc.c new file +9 -1
+module_platform_driver(litex_mmc_driver);
+
+MODULE_DESCRIPTION("LiteX SDCard driver");
+MODULE_AUTHOR("Antmicro <[email protected]>");
+MODULE_AUTHOR("Kamil Rakoczy <[email protected]>");
+MODULE_AUTHOR("Maciej Dudek <[email protected]>");
+MODULE_AUTHOR("Paul Mackerras <[email protected]>");
+MODULE_AUTHOR("Gabriel Somlo <[email protected]>");
+MODULE_LICENSE("GPL v2");
--

Other than the comments above, the code looks nice and was easy to review, thanks!

Kind regards
Uffe

#12 lore raw

Re: [PATCH v5 3/3] mmc: Add driver for LiteX's LiteSDCard interface

Thanks for the feedback! Some replies and follow-up comments below.

Quoted 52 lines · Ulf Hansson
Quoted 23 lines · Gabriel Somlo

LiteX (https://github.com/enjoy-digital/litex) is a SoC framework that targets FPGAs. LiteSDCard is a small footprint, configurable SDCard core commonly used in LiteX designs.

The driver was first written in May 2020 and has been maintained cooperatively by the LiteX community. Thanks to all contributors!

Cc Mateusz Holenko <[email protected]>
Cc Karol Gugala <[email protected]>
Cc Joel Stanley <[email protected]>
Cc Stafford Horne <[email protected]>
Cc Geert Uytterhoeven <[email protected]>
Cc David Abdurachmanov <[email protected]>
Cc Florent Kermarrec <[email protected]>

[...]

Quoted 25 lines
drivers/mmc/host/litex_mmc.c new file +25 -0
+
+static int litex_mmc_set_bus_width(struct litex_mmc_host *host)
+{
+       bool app_cmd_sent;
+       int ret;
+
+       if (host->is_bus_width_set)
+               return 0;
+
+       /* ensure 'app_cmd' precedes 'app_set_bus_width_cmd' */
+       app_cmd_sent = host->app_cmd; /* was preceding command app_cmd? */
+       if (!app_cmd_sent) {
+               ret = litex_mmc_send_app_cmd(host);
+               if (ret)
+                       return ret;
+       }
+
+       /* litesdcard only supports 4-bit bus width */
+       ret = litex_mmc_send_set_bus_w_cmd(host, MMC_BUS_WIDTH_4);
+       if (ret)
+               return ret;
+
+       /* re-send 'app_cmd' if necessary */
+       if (app_cmd_sent) {
+               ret = litex_mmc_send_app_cmd(host);
Quoted 8 lines

drivers/mmc/host/litex_mmc.c line 186

Quoted 3 lines
drivers/mmc/host/litex_mmc.c new file +3 -0
+               if (ret)
+                       return ret;
+       }

I understand that you are trying to address the limitation that the controller supports 4-bit width only. In principle it looks like we may need to violate the SD spec to be able to initialise an SD card, right? Isn't that a concern for you?

This would only be a concern for SDcard media that do not support 4-bit wide data transfer (if any such models even/still exist!). This driver simply forces 4-bit wide data mode as soon as the very first data transfer is "snooped", which is AFAICT legal w.r.t. the spec.

If the card does not support 4-bit wide data transfers, then litex_mmc_set_bus_width() will fail, and the card will fail to initialize, but I don't foresee much of anything else going wrong.

Quoted 51 lines
Quoted 5 lines
drivers/mmc/host/litex_mmc.c new file +5 -0
+
+       host->is_bus_width_set = true;
+
+       return 0;
+}

[...]

Quoted 29 lines
drivers/mmc/host/litex_mmc.c new file +29 -0
+
+static void litex_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
+{
+       struct litex_mmc_host *host = mmc_priv(mmc);
+       struct device *dev = mmc_dev(mmc);
+       struct mmc_command *cmd = mrq->cmd;
+       struct mmc_command *sbc = mrq->sbc;
+       struct mmc_data *data = mrq->data;
+       struct mmc_command *stop = mrq->stop;
+       unsigned int retries = cmd->retries;
+       unsigned int len = 0;
+       bool direct = false;
+       u32 response_len = litex_mmc_response_len(cmd);
+       u8 transfer = SD_CTL_DATA_XFER_NONE;
+
+       /* First check that the card is still there */
+       if (!litex_mmc_get_cd(mmc)) {
+               cmd->error = -ENOMEDIUM;
+               mmc_request_done(mmc, mrq);
+               return;
+       }
+
+       /* Send set-block-count command if needed */
+       if (sbc) {
+               sbc->error = litex_mmc_send_cmd(host, sbc->opcode, sbc->arg,
+                                               litex_mmc_response_len(sbc),
+                                               SD_CTL_DATA_XFER_NONE);
+               if (sbc->error) {
+                       host->is_bus_width_set = false;

drivers/mmc/host/litex_mmc.c line 368

Quoted 10 lines
drivers/mmc/host/litex_mmc.c new file +10 -0
+                       mmc_request_done(mmc, mrq);
+                       return;
+               }
+       }
+
+       if (data) {
+               /* LiteSDCard only supports 4-bit bus width; therefore, we MUST
+                * inject a SET_BUS_WIDTH (acmd6) before the very first data
+                * transfer, earlier than when the mmc subsystem would normally
+                * get around to it!

This means that you may end up trying to switch bus-width, to a width that isn't supported by the card, for example.

Quoted 3 lines

As also stated above, I wonder how this conforms to the SD spec from the initialization sequence point of view. Have you verified that this isn't a problem?

During litex_mmc_probe(), I have:

	...
        ret = mmc_of_parse(mmc);
        if (ret)
                goto err;

        /* force 4-bit bus_width (only width supported by hardware) */
        mmc->caps &= ~MMC_CAP_8_BIT_DATA;
        mmc->caps |= MMC_CAP_4_BIT_DATA;
	...

This ensures no bus-width switches to anything other than 4-bit data should ever occur. As far as I understand the SDcard spec, it's legal to both send multiple redundant bus-width-set commands, and to start doing so before the very first data transfer request is processed (regardless of the fact that linux typically does a few 1-bit-wide data transfers during card initialization before switching to a wider mode, if available).

This driver simply ensures that any time we ever have a data transfer, the bus width is set to 4 *before* said transfer is acted upon.

As I mentioned earlier, if we get a "weird" SDcard that can't support 4-bit data transfers, its initialization should fail shortly after detection, and that's all there is to it, as far as I can tell.

Quoted 66 lines
Quoted 57 lines
drivers/mmc/host/litex_mmc.c new file +57 -0
+                */
+               cmd->error = litex_mmc_set_bus_width(host);
+               if (cmd->error) {
+                       dev_err(dev, "Can't set bus width!\n");
+                       mmc_request_done(mmc, mrq);
+                       return;
+               }
+
+               litex_mmc_do_dma(host, data, &len, &direct, &transfer);
+       }
+
+       do {
+               cmd->error = litex_mmc_send_cmd(host, cmd->opcode, cmd->arg,
+                                               response_len, transfer);
+       } while (cmd->error && retries-- > 0);
+
+       if (cmd->error) {
+               /* card may be gone; don't assume bus width is still set */
+               host->is_bus_width_set = false;
+       }
+
+       if (response_len == SD_CTL_RESP_SHORT) {
+               /* pull short response fields from appropriate host registers */
+               cmd->resp[0] = host->resp[3];
+               cmd->resp[1] = host->resp[2] & 0xFF;
+       } else if (response_len == SD_CTL_RESP_LONG) {
+               cmd->resp[0] = host->resp[0];
+               cmd->resp[1] = host->resp[1];
+               cmd->resp[2] = host->resp[2];
+               cmd->resp[3] = host->resp[3];
+       }
+
+       /* Send stop-transmission command if required */
+       if (stop && (cmd->error || !sbc)) {
+               stop->error = litex_mmc_send_cmd(host, stop->opcode, stop->arg,
+                                                litex_mmc_response_len(stop),
+                                                SD_CTL_DATA_XFER_NONE);
+               if (stop->error)
+                       host->is_bus_width_set = false;
+       }
+
+       if (data) {
+               dma_unmap_sg(dev, data->sg, data->sg_len,
+                            mmc_get_dma_dir(data));
+       }
+
+       if (!cmd->error && transfer != SD_CTL_DATA_XFER_NONE) {
+               data->bytes_xfered = min(len, mmc->max_req_size);
+               if (transfer == SD_CTL_DATA_XFER_READ && !direct) {
+                       sg_copy_from_buffer(data->sg, sg_nents(data->sg),
+                                           host->buffer, data->bytes_xfered);
+               }
+       }
+
+       mmc_request_done(mmc, mrq);
+}
+

[...]

drivers/mmc/host/litex_mmc.c line 580

Quoted 2 lines
drivers/mmc/host/litex_mmc.c new file +2 -0
+
+       mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;

I noticed that you use these hard coded values and don't really care to manage voltage changes via ->set_ios().

Quoted 8 lines

Rather than doing it like this, I would prefer if you can hook up a fixed vmmc regulator in the DTS. Then call mmc_regulator_get_supply() to fetch it from here, which will let the mmc core create the mmc->ocr_avail mask, based upon the voltage level the regulator supports.

This becomes more generic and allows more flexibility for the platform configuration.

The LiteSDCard "hardware" (i.e., *gateware*) does not allow modification or selection of voltage from the software side. When a CMD8 is issued, the "voltage supplied" bit pattern is expected to be '0001b', which per the spec means "2.7-3.6V".

I tried adding this to the overall DTS:

	vreg_mmc: vreg_mmc_3v {
		compatible = "regulator-fixed";
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
	};

and then added a reference to it to the LiteSDCard "mmc0" node in DTS, like so:

	mmc0: mmc@12005000 {
		compatible = "litex,mmc";
		reg = <0x12005000 0x100>,
			<0x12003800 0x100>,
			<0x12003000 0x100>,
			<0x12004800 0x100>,
			<0x12004000 0x100>;
		reg-names = "phy", "core", "reader", "writer", "irq";
		clocks = <&sys_clk>;
		vmmc-supply = <&vreg_mmc>; /* <-------- HERE !!! */
		interrupt-parent = <&L1>;
		interrupts = <4>;
	};

Finally, I replaced the hardcoded setting of mmc->ocr_avail with a call to mmc_regulator_get_supply(mmc). Now, I get a bunch of timeouts during attempts to send e.g., CMD8 and CMD55.
(going for 3200000 and 3400000 for min- and max-microvolt, respectively,
-- or anything else in the allowed 2.7-3.6 range -- doesn't help either).

I might be doing something subtly wrong in the way I set things up above, but it feels a bit overengineered, and IMHO fragile.

OTOH, going all out and setting:

        /* allow for generic 2.7-3.6V range, no software tuning available */
        mmc->ocr_avail = MMC_VDD_27_28 | MMC_VDD_28_29 | MMC_VDD_29_30 |
                         MMC_VDD_30_31 | MMC_VDD_31_32 | MMC_VDD_32_33 |
                         MMC_VDD_33_34 | MMC_VDD_34_35 | MMC_VDD_35_36;

seems to work just fine... :) Please do let me know what you think!

Quoted 18 lines
Quoted 18 lines
drivers/mmc/host/litex_mmc.c new file +18 -0
+       mmc->ops = &litex_mmc_ops;
+
+       /* set default sd_clk frequency range based on empirical observations
+        * of LiteSDCard gateware behavior on typical SDCard media
+        */
+       mmc->f_min = 12.5e6;
+       mmc->f_max = 50e6;
+
+       ret = mmc_of_parse(mmc);
+       if (ret)
+               goto err;
+
+       /* force 4-bit bus_width (only width supported by hardware) */
+       mmc->caps &= ~MMC_CAP_8_BIT_DATA;
+       mmc->caps |= MMC_CAP_4_BIT_DATA;
+
+       /* set default capabilities */
+       mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY |
Quoted 8 lines

drivers/mmc/host/litex_mmc.c line 602

Quoted 4 lines
drivers/mmc/host/litex_mmc.c new file +4 -0
+                    MMC_CAP_DRIVER_TYPE_D |
+                    MMC_CAP_CMD23;
+       mmc->caps2 |= MMC_CAP2_NO_WRITE_PROTECT |
+                     MMC_CAP2_FULL_PWR_CYCLE |

A full power cycle requires you to be able to power on/off the vmmc regulator (unless this is internally managed by the controller). Can you really do that?

Nope, I cannot -- so I removed MMC_CAP2_FULL_PWR_CYCLE from the list, should be there in v6 of the series. Thanks for pointing that out!

Quoted 4 lines
Quoted 1 line

+ MMC_CAP2_NO_SDIO;

We should add MMC_CAP2_NO_MMC here as well, as it looks like it can't be supported. Right?

Right, and done -- lined up for v6.

Quoted 45 lines
Quoted 45 lines
drivers/mmc/host/litex_mmc.c new file +45 -0
+
+       platform_set_drvdata(pdev, host);
+
+       ret = mmc_add_host(mmc);
+       if (ret < 0)
+               goto err;
+
+       return 0;
+
+err:
+       mmc_free_host(mmc);
+       return ret;
+}
+
+static int litex_mmc_remove(struct platform_device *pdev)
+{
+       struct litex_mmc_host *host = dev_get_drvdata(&pdev->dev);
+
+       if (host->irq > 0)
+               free_irq(host->irq, host->mmc);
+       mmc_remove_host(host->mmc);
+       mmc_free_host(host->mmc);
+
+       return 0;
+}
+
+static const struct of_device_id litex_match[] = {
+       { .compatible = "litex,mmc" },
+       { }
+};
+MODULE_DEVICE_TABLE(of, litex_match);
+
+static struct platform_driver litex_mmc_driver = {
+       .probe = litex_mmc_probe,
+       .remove = litex_mmc_remove,
+       .driver = {
+               .name = "litex-mmc",
+               .of_match_table = of_match_ptr(litex_match),
+       },
+};
+module_platform_driver(litex_mmc_driver);
+
+MODULE_DESCRIPTION("LiteX SDCard driver");
+MODULE_AUTHOR("Antmicro <[email protected]>");
+MODULE_AUTHOR("Kamil Rakoczy <[email protected]>");
Quoted 8 lines

drivers/mmc/host/litex_mmc.c

Quoted 5 lines
drivers/mmc/host/litex_mmc.c new file +4 -1
+MODULE_AUTHOR("Maciej Dudek <[email protected]>");
+MODULE_AUTHOR("Paul Mackerras <[email protected]>");
+MODULE_AUTHOR("Gabriel Somlo <[email protected]>");
+MODULE_LICENSE("GPL v2");
--

Other than the comments above, the code looks nice and was easy to review, thanks!

Thanks again for the review, and please LMK about the voltage regulator question.

Happy New Year,
--Gabriel

#13 lore raw

Re: [PATCH v5 3/3] mmc: Add driver for LiteX's LiteSDCard interface

Quoted 52 lines · Gabriel L. Somlo
Quoted 11 lines · Ulf Hansson

I noticed that you use these hard coded values and don't really care to manage voltage changes via ->set_ios().

Rather than doing it like this, I would prefer if you can hook up a fixed vmmc regulator in the DTS. Then call mmc_regulator_get_supply() to fetch it from here, which will let the mmc core create the mmc->ocr_avail mask, based upon the voltage level the regulator supports.

This becomes more generic and allows more flexibility for the platform configuration.

The LiteSDCard "hardware" (i.e., *gateware*) does not allow modification or selection of voltage from the software side. When a CMD8 is issued, the "voltage supplied" bit pattern is expected to be '0001b', which per the spec means "2.7-3.6V".

I tried adding this to the overall DTS:

	vreg_mmc: vreg_mmc_3v {
		compatible = "regulator-fixed";
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
	};

and then added a reference to it to the LiteSDCard "mmc0" node in DTS, like so:

	mmc0: mmc@12005000 {
		compatible = "litex,mmc";
		reg = <0x12005000 0x100>,
			<0x12003800 0x100>,
			<0x12003000 0x100>,
			<0x12004800 0x100>,
			<0x12004000 0x100>;
		reg-names = "phy", "core", "reader", "writer", "irq";
		clocks = <&sys_clk>;
		vmmc-supply = <&vreg_mmc>; /* <-------- HERE !!! */
		interrupt-parent = <&L1>;
		interrupts = <4>;
	};

Finally, I replaced the hardcoded setting of mmc->ocr_avail with a call to mmc_regulator_get_supply(mmc). Now, I get a bunch of timeouts during attempts to send e.g., CMD8 and CMD55.
(going for 3200000 and 3400000 for min- and max-microvolt, respectively,
-- or anything else in the allowed 2.7-3.6 range -- doesn't help either).

I might be doing something subtly wrong in the way I set things up above, but it feels a bit overengineered, and IMHO fragile.

Quoted 8 lines

OTOH, going all out and setting:

        /* allow for generic 2.7-3.6V range, no software tuning available */
        mmc->ocr_avail = MMC_VDD_27_28 | MMC_VDD_28_29 | MMC_VDD_29_30 |
                         MMC_VDD_30_31 | MMC_VDD_31_32 | MMC_VDD_32_33 |
                         MMC_VDD_33_34 | MMC_VDD_34_35 | MMC_VDD_35_36;

seems to work just fine... :) Please do let me know what you think!

I dug around drivers/mmc/core/regulator.c a bit more, and it turns out mmc_regulator_get_supply() is allowed to return 0 even if not all regulators have been found, "because they all are optional", and I still need to write additional code to check if my regulator got populated -- I assume that means checking if mmc->ocr_avail was set to something useful, or whether it's still 0.

In my case, with the above-mentioned modifications in DTS, I still end up with mmc->ocr_avail == 0 after calling mmc_regulator_get_supply(), which explains why the card doesnoesn't work correctly after being probed.

Not quite sure what to do in that situation -- any ideas?

I still think it's a bit overkill to set up a dummy regulator in DTS and probe for it when the "hardware" doesn't actually support variable/configurable voltages or dynamic changes in voltage -- a hard-coded constant somehow feels more appropriate, wouldn't you agree?

IMHO, it makes more sense to define the entire generic/standard range described in the SDCard specification (2.7-3.6V) as a constant, e.g.:

#define LITEX_MMC_OCR (MMC_VDD_27_28 | MMC_VDD_28_29 | MMC_VDD_29_30 | \
                       MMC_VDD_30_31 | MMC_VDD_31_32 | MMC_VDD_32_33 | \
                       MMC_VDD_33_34 | MMC_VDD_34_35 | MMC_VDD_35_36)

and then initialize mmc->ocr_avail = LITEX_MMC_OCR to that instead. This is how they do it in drivers/mmc/host/au1xmmc.c, for instance.

I'm happy to learn more about why going the DTS-dummy-regulator configurable route is better, so let me know what you think. I'm going to send out v6 with the hard-coded constant version above soon, unless I hear back from you before then. But we can always go another round (i.e., v7) unless you agree with my argument -- please let me know either way! :)

Thanks again,
--Gabriel

#14 lore raw

Re: [PATCH v5 3/3] mmc: Add driver for LiteX's LiteSDCard interface

In reply to Gabriel L. Somlo

[...]

Quoted 67 lines
Quoted 43 lines
Quoted 29 lines
drivers/mmc/host/litex_mmc.c new file +29 -0
+
+static void litex_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
+{
+       struct litex_mmc_host *host = mmc_priv(mmc);
+       struct device *dev = mmc_dev(mmc);
+       struct mmc_command *cmd = mrq->cmd;
+       struct mmc_command *sbc = mrq->sbc;
+       struct mmc_data *data = mrq->data;
+       struct mmc_command *stop = mrq->stop;
+       unsigned int retries = cmd->retries;
+       unsigned int len = 0;
+       bool direct = false;
+       u32 response_len = litex_mmc_response_len(cmd);
+       u8 transfer = SD_CTL_DATA_XFER_NONE;
+
+       /* First check that the card is still there */
+       if (!litex_mmc_get_cd(mmc)) {
+               cmd->error = -ENOMEDIUM;
+               mmc_request_done(mmc, mrq);
+               return;
+       }
+
+       /* Send set-block-count command if needed */
+       if (sbc) {
+               sbc->error = litex_mmc_send_cmd(host, sbc->opcode, sbc->arg,
+                                               litex_mmc_response_len(sbc),
+                                               SD_CTL_DATA_XFER_NONE);
+               if (sbc->error) {
+                       host->is_bus_width_set = false;

drivers/mmc/host/litex_mmc.c line 368

Quoted 10 lines
drivers/mmc/host/litex_mmc.c new file +10 -0
+                       mmc_request_done(mmc, mrq);
+                       return;
+               }
+       }
+
+       if (data) {
+               /* LiteSDCard only supports 4-bit bus width; therefore, we MUST
+                * inject a SET_BUS_WIDTH (acmd6) before the very first data
+                * transfer, earlier than when the mmc subsystem would normally
+                * get around to it!

This means that you may end up trying to switch bus-width, to a width that isn't supported by the card, for example.

Quoted 3 lines

As also stated above, I wonder how this conforms to the SD spec from the initialization sequence point of view. Have you verified that this isn't a problem?

During litex_mmc_probe(), I have:

        ...
        ret = mmc_of_parse(mmc);
        if (ret)
                goto err;

        /* force 4-bit bus_width (only width supported by hardware) */
        mmc->caps &= ~MMC_CAP_8_BIT_DATA;
        mmc->caps |= MMC_CAP_4_BIT_DATA;
        ...

This ensures no bus-width switches to anything other than 4-bit data should ever occur. As far as I understand the SDcard spec, it's legal to both send multiple redundant bus-width-set commands, and to start doing so before the very first data transfer request is processed (regardless of the fact that linux typically does a few 1-bit-wide data transfers during card initialization before switching to a wider mode, if available).

Quoted 6 lines

This driver simply ensures that any time we ever have a data transfer, the bus width is set to 4 *before* said transfer is acted upon.

As I mentioned earlier, if we get a "weird" SDcard that can't support 4-bit data transfers, its initialization should fail shortly after detection, and that's all there is to it, as far as I can tell.

Alright, I get the point. I guess it should work. I will have another closer look at the corresponding code from your last submitted version.

Quoted 73 lines
Quoted 73 lines
Quoted 58 lines
drivers/mmc/host/litex_mmc.c new file +57 -0
+                */
+               cmd->error = litex_mmc_set_bus_width(host);
+               if (cmd->error) {
+                       dev_err(dev, "Can't set bus width!\n");
+                       mmc_request_done(mmc, mrq);
+                       return;
+               }
+
+               litex_mmc_do_dma(host, data, &len, &direct, &transfer);
+       }
+
+       do {
+               cmd->error = litex_mmc_send_cmd(host, cmd->opcode, cmd->arg,
+                                               response_len, transfer);
+       } while (cmd->error && retries-- > 0);
+
+       if (cmd->error) {
+               /* card may be gone; don't assume bus width is still set */
+               host->is_bus_width_set = false;
+       }
+
+       if (response_len == SD_CTL_RESP_SHORT) {
+               /* pull short response fields from appropriate host registers */
+               cmd->resp[0] = host->resp[3];
+               cmd->resp[1] = host->resp[2] & 0xFF;
+       } else if (response_len == SD_CTL_RESP_LONG) {
+               cmd->resp[0] = host->resp[0];
+               cmd->resp[1] = host->resp[1];
+               cmd->resp[2] = host->resp[2];
+               cmd->resp[3] = host->resp[3];
+       }
+
+       /* Send stop-transmission command if required */
+       if (stop && (cmd->error || !sbc)) {
+               stop->error = litex_mmc_send_cmd(host, stop->opcode, stop->arg,
+                                                litex_mmc_response_len(stop),
+                                                SD_CTL_DATA_XFER_NONE);
+               if (stop->error)
+                       host->is_bus_width_set = false;
+       }
+
+       if (data) {
+               dma_unmap_sg(dev, data->sg, data->sg_len,
+                            mmc_get_dma_dir(data));
+       }
+
+       if (!cmd->error && transfer != SD_CTL_DATA_XFER_NONE) {
+               data->bytes_xfered = min(len, mmc->max_req_size);
+               if (transfer == SD_CTL_DATA_XFER_READ && !direct) {
+                       sg_copy_from_buffer(data->sg, sg_nents(data->sg),
+                                           host->buffer, data->bytes_xfered);
+               }
+       }
+
+       mmc_request_done(mmc, mrq);
+}
+

[...]

drivers/mmc/host/litex_mmc.c line 580

Quoted 2 lines
drivers/mmc/host/litex_mmc.c new file +2 -0
+
+       mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;

I noticed that you use these hard coded values and don't really care to manage voltage changes via ->set_ios().

Rather than doing it like this, I would prefer if you can hook up a fixed vmmc regulator in the DTS. Then call mmc_regulator_get_supply() to fetch it from here, which will let the mmc core create the mmc->ocr_avail mask, based upon the voltage level the regulator supports.

Quoted 7 lines
Quoted 2 lines

This becomes more generic and allows more flexibility for the platform configuration.

The LiteSDCard "hardware" (i.e., *gateware*) does not allow modification or selection of voltage from the software side. When a CMD8 is issued, the "voltage supplied" bit pattern is expected to be '0001b', which per the spec means "2.7-3.6V".

If you provide a range (2.7-3.6V), that means that your hardware supports the entire range, not just one single part of it.

Quoted 26 lines

I tried adding this to the overall DTS:

        vreg_mmc: vreg_mmc_3v {
                compatible = "regulator-fixed";
                regulator-min-microvolt = <3300000>;
                regulator-max-microvolt = <3300000>;
        };

and then added a reference to it to the LiteSDCard "mmc0" node in DTS, like so:

        mmc0: mmc@12005000 {
                compatible = "litex,mmc";
                reg = <0x12005000 0x100>,
                        <0x12003800 0x100>,
                        <0x12003000 0x100>,
                        <0x12004800 0x100>,
                        <0x12004000 0x100>;
                reg-names = "phy", "core", "reader", "writer", "irq";
                clocks = <&sys_clk>;
                vmmc-supply = <&vreg_mmc>; /* <-------- HERE !!! */
                interrupt-parent = <&L1>;
                interrupts = <4>;
        };
Quoted 8 lines

Finally, I replaced the hardcoded setting of mmc->ocr_avail with a call to mmc_regulator_get_supply(mmc). Now, I get a bunch of timeouts during attempts to send e.g., CMD8 and CMD55.
(going for 3200000 and 3400000 for min- and max-microvolt, respectively,
-- or anything else in the allowed 2.7-3.6 range -- doesn't help either).

I might be doing something subtly wrong in the way I set things up above, but it feels a bit overengineered, and IMHO fragile.

At a quick glance, the above looks correct to me. Maybe there is something wrong with the code in the driver instead?

Quoted 9 lines

OTOH, going all out and setting:

        /* allow for generic 2.7-3.6V range, no software tuning available */
        mmc->ocr_avail = MMC_VDD_27_28 | MMC_VDD_28_29 | MMC_VDD_29_30 |
                         MMC_VDD_30_31 | MMC_VDD_31_32 | MMC_VDD_32_33 |
                         MMC_VDD_33_34 | MMC_VDD_34_35 | MMC_VDD_35_36;

seems to work just fine... :) Please do let me know what you think!

No, this isn't the way we want it to work. That's because it means that we would lie to the card about what voltage range the HW actually supports.

It's better to let the DTS file give that information about the HW.

[...]

Kind regards
Uffe

#15 lore raw

Re: [PATCH v5 3/3] mmc: Add driver for LiteX's LiteSDCard interface

Hi Uffe,

Quoted 72 lines · Ulf Hansson

[...]

Quoted 70 lines
Quoted 43 lines
Quoted 29 lines
drivers/mmc/host/litex_mmc.c new file +29 -0
+
+static void litex_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
+{
+       struct litex_mmc_host *host = mmc_priv(mmc);
+       struct device *dev = mmc_dev(mmc);
+       struct mmc_command *cmd = mrq->cmd;
+       struct mmc_command *sbc = mrq->sbc;
+       struct mmc_data *data = mrq->data;
+       struct mmc_command *stop = mrq->stop;
+       unsigned int retries = cmd->retries;
+       unsigned int len = 0;
+       bool direct = false;
+       u32 response_len = litex_mmc_response_len(cmd);
+       u8 transfer = SD_CTL_DATA_XFER_NONE;
+
+       /* First check that the card is still there */
+       if (!litex_mmc_get_cd(mmc)) {
+               cmd->error = -ENOMEDIUM;
+               mmc_request_done(mmc, mrq);
+               return;
+       }
+
+       /* Send set-block-count command if needed */
+       if (sbc) {
+               sbc->error = litex_mmc_send_cmd(host, sbc->opcode, sbc->arg,
+                                               litex_mmc_response_len(sbc),
+                                               SD_CTL_DATA_XFER_NONE);
+               if (sbc->error) {
+                       host->is_bus_width_set = false;

drivers/mmc/host/litex_mmc.c line 368

Quoted 10 lines
drivers/mmc/host/litex_mmc.c new file +10 -0
+                       mmc_request_done(mmc, mrq);
+                       return;
+               }
+       }
+
+       if (data) {
+               /* LiteSDCard only supports 4-bit bus width; therefore, we MUST
+                * inject a SET_BUS_WIDTH (acmd6) before the very first data
+                * transfer, earlier than when the mmc subsystem would normally
+                * get around to it!

This means that you may end up trying to switch bus-width, to a width that isn't supported by the card, for example.

Quoted 3 lines

As also stated above, I wonder how this conforms to the SD spec from the initialization sequence point of view. Have you verified that this isn't a problem?

During litex_mmc_probe(), I have:

        ...
        ret = mmc_of_parse(mmc);
        if (ret)
                goto err;

        /* force 4-bit bus_width (only width supported by hardware) */
        mmc->caps &= ~MMC_CAP_8_BIT_DATA;
        mmc->caps |= MMC_CAP_4_BIT_DATA;
        ...

This ensures no bus-width switches to anything other than 4-bit data should ever occur. As far as I understand the SDcard spec, it's legal to both send multiple redundant bus-width-set commands, and to start doing so before the very first data transfer request is processed (regardless of the fact that linux typically does a few 1-bit-wide data transfers during card initialization before switching to a wider mode, if available).

This driver simply ensures that any time we ever have a data transfer, the bus width is set to 4 *before* said transfer is acted upon.

Quoted 7 lines
Quoted 3 lines

As I mentioned earlier, if we get a "weird" SDcard that can't support 4-bit data transfers, its initialization should fail shortly after detection, and that's all there is to it, as far as I can tell.

Alright, I get the point. I guess it should work. I will have another closer look at the corresponding code from your last submitted version.

Thanks -- it's now on v12 :)

Quoted 76 lines
Quoted 76 lines
Quoted 75 lines
Quoted 58 lines
drivers/mmc/host/litex_mmc.c new file +57 -0
+                */
+               cmd->error = litex_mmc_set_bus_width(host);
+               if (cmd->error) {
+                       dev_err(dev, "Can't set bus width!\n");
+                       mmc_request_done(mmc, mrq);
+                       return;
+               }
+
+               litex_mmc_do_dma(host, data, &len, &direct, &transfer);
+       }
+
+       do {
+               cmd->error = litex_mmc_send_cmd(host, cmd->opcode, cmd->arg,
+                                               response_len, transfer);
+       } while (cmd->error && retries-- > 0);
+
+       if (cmd->error) {
+               /* card may be gone; don't assume bus width is still set */
+               host->is_bus_width_set = false;
+       }
+
+       if (response_len == SD_CTL_RESP_SHORT) {
+               /* pull short response fields from appropriate host registers */
+               cmd->resp[0] = host->resp[3];
+               cmd->resp[1] = host->resp[2] & 0xFF;
+       } else if (response_len == SD_CTL_RESP_LONG) {
+               cmd->resp[0] = host->resp[0];
+               cmd->resp[1] = host->resp[1];
+               cmd->resp[2] = host->resp[2];
+               cmd->resp[3] = host->resp[3];
+       }
+
+       /* Send stop-transmission command if required */
+       if (stop && (cmd->error || !sbc)) {
+               stop->error = litex_mmc_send_cmd(host, stop->opcode, stop->arg,
+                                                litex_mmc_response_len(stop),
+                                                SD_CTL_DATA_XFER_NONE);
+               if (stop->error)
+                       host->is_bus_width_set = false;
+       }
+
+       if (data) {
+               dma_unmap_sg(dev, data->sg, data->sg_len,
+                            mmc_get_dma_dir(data));
+       }
+
+       if (!cmd->error && transfer != SD_CTL_DATA_XFER_NONE) {
+               data->bytes_xfered = min(len, mmc->max_req_size);
+               if (transfer == SD_CTL_DATA_XFER_READ && !direct) {
+                       sg_copy_from_buffer(data->sg, sg_nents(data->sg),
+                                           host->buffer, data->bytes_xfered);
+               }
+       }
+
+       mmc_request_done(mmc, mrq);
+}
+

[...]

drivers/mmc/host/litex_mmc.c line 580

Quoted 2 lines
drivers/mmc/host/litex_mmc.c new file +2 -0
+
+       mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;

I noticed that you use these hard coded values and don't really care to manage voltage changes via ->set_ios().

Rather than doing it like this, I would prefer if you can hook up a fixed vmmc regulator in the DTS. Then call mmc_regulator_get_supply() to fetch it from here, which will let the mmc core create the mmc->ocr_avail mask, based upon the voltage level the regulator supports.

This becomes more generic and allows more flexibility for the platform configuration.

Quoted 7 lines
Quoted 4 lines

The LiteSDCard "hardware" (i.e., *gateware*) does not allow modification or selection of voltage from the software side. When a CMD8 is issued, the "voltage supplied" bit pattern is expected to be '0001b', which per the spec means "2.7-3.6V".

If you provide a range (2.7-3.6V), that means that your hardware supports the entire range, not just one single part of it.

The "gateware" (open source migen/verilog at
https://github.com/enjoy-digital/litesdcard)
supports any value provided by the underlying FPGA dev board (typically 3.3v) -- by not attempting to manage it in any way.

SD media presumably doesn't care as long as voltage is somewhere within 2.7-3.6V (at least that's how I read the spec, there's only one register value representing anything within that range).

Quoted 32 lines
Quoted 32 lines

I tried adding this to the overall DTS:

        vreg_mmc: vreg_mmc_3v {
                compatible = "regulator-fixed";
                regulator-min-microvolt = <3300000>;
                regulator-max-microvolt = <3300000>;
        };

and then added a reference to it to the LiteSDCard "mmc0" node in DTS, like so:

        mmc0: mmc@12005000 {
                compatible = "litex,mmc";
                reg = <0x12005000 0x100>,
                        <0x12003800 0x100>,
                        <0x12003000 0x100>,
                        <0x12004800 0x100>,
                        <0x12004000 0x100>;
                reg-names = "phy", "core", "reader", "writer", "irq";
                clocks = <&sys_clk>;
                vmmc-supply = <&vreg_mmc>; /* <-------- HERE !!! */
                interrupt-parent = <&L1>;
                interrupts = <4>;
        };

Finally, I replaced the hardcoded setting of mmc->ocr_avail with a call to mmc_regulator_get_supply(mmc). Now, I get a bunch of timeouts during attempts to send e.g., CMD8 and CMD55.
(going for 3200000 and 3400000 for min- and max-microvolt, respectively,
-- or anything else in the allowed 2.7-3.6 range -- doesn't help either).

Quoted 5 lines
Quoted 2 lines

I might be doing something subtly wrong in the way I set things up above, but it feels a bit overengineered, and IMHO fragile.

At a quick glance, the above looks correct to me. Maybe there is something wrong with the code in the driver instead?

After some more hacking, I learned that:

- an additional regulator-name line (e.g. regulator-name = "vreg_mmc";) is required

- setting regulator-always-on; seems to help reduce attempts by the kernel to "manage" the regulator, but does not appear to be required

In other words:

	...
        vreg_mmc: vreg_mmc {
                compatible = "regulator-fixed";
                regulator-name = "vreg_mmc";
                regulator-min-microvolt = <3300000>;
                regulator-max-microvolt = <3300000>;
                regulator-always-on;
        };
	...

Additionally, CONFIG_REGULATOR=y and CONFIG_REGULATOR_FIXED_VOLTAGE=y *MUST* be enabled in the kernel's .config file, to prevent either litex_mmc_probe() from being deferred, or mmc_regulator_get_supply() from simply returning 0 without having set mmc->ocr_avail to anything at all!

Presumably this would also mean either select REGULATOR_FIXED_VOLTAGE or depends on REGULATOR_FIXED_VOLTAGE in the mmc driver's Kconfig entry.

Predictably, the "regulator-[min|max]-microvolt = <3300000>" setting gets us

	ocr_avail == MMC_VDD_32_33 | MMC_VDD_33_34
Quoted 8 lines
Quoted 8 lines

OTOH, going all out and setting:

        /* allow for generic 2.7-3.6V range, no software tuning available */
        mmc->ocr_avail = MMC_VDD_27_28 | MMC_VDD_28_29 | MMC_VDD_29_30 |
                         MMC_VDD_30_31 | MMC_VDD_31_32 | MMC_VDD_32_33 |
                         MMC_VDD_33_34 | MMC_VDD_34_35 | MMC_VDD_35_36;
Quoted 7 lines
Quoted 1 line

seems to work just fine... :) Please do let me know what you think!

No, this isn't the way we want it to work. That's because it means that we would lie to the card about what voltage range the HW actually supports.

It's better to let the DTS file give that information about the HW.

I may be needlessly concerned, but it feels a bit weird to me to drag in CONFIG_REGULATOR_FIXED_VOLTAGE as an added dependency for what is ultimately a roundabout way of setting a constant... :)

Thanks in advance for any additional clue!

Best,
--Gabriel

#16 lore raw

Re: [PATCH v5 3/3] mmc: Add driver for LiteX's LiteSDCard interface

[...]

Quoted 99 lines
Quoted 17 lines
Quoted 17 lines
Quoted 16 lines

[...]

drivers/mmc/host/litex_mmc.c line 580

Quoted 2 lines
drivers/mmc/host/litex_mmc.c new file +2 -0
+
+       mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;

I noticed that you use these hard coded values and don't really care to manage voltage changes via ->set_ios().

Rather than doing it like this, I would prefer if you can hook up a fixed vmmc regulator in the DTS. Then call mmc_regulator_get_supply() to fetch it from here, which will let the mmc core create the mmc->ocr_avail mask, based upon the voltage level the regulator supports.

This becomes more generic and allows more flexibility for the platform configuration.

Quoted 7 lines
Quoted 4 lines

The LiteSDCard "hardware" (i.e., *gateware*) does not allow modification or selection of voltage from the software side. When a CMD8 is issued, the "voltage supplied" bit pattern is expected to be '0001b', which per the spec means "2.7-3.6V".

If you provide a range (2.7-3.6V), that means that your hardware supports the entire range, not just one single part of it.

The "gateware" (open source migen/verilog at
https://github.com/enjoy-digital/litesdcard)
supports any value provided by the underlying FPGA dev board (typically 3.3v) -- by not attempting to manage it in any way.

SD media presumably doesn't care as long as voltage is somewhere within 2.7-3.6V (at least that's how I read the spec, there's only one register value representing anything within that range).

Quoted 32 lines
Quoted 32 lines

I tried adding this to the overall DTS:

        vreg_mmc: vreg_mmc_3v {
                compatible = "regulator-fixed";
                regulator-min-microvolt = <3300000>;
                regulator-max-microvolt = <3300000>;
        };

and then added a reference to it to the LiteSDCard "mmc0" node in DTS, like so:

        mmc0: mmc@12005000 {
                compatible = "litex,mmc";
                reg = <0x12005000 0x100>,
                        <0x12003800 0x100>,
                        <0x12003000 0x100>,
                        <0x12004800 0x100>,
                        <0x12004000 0x100>;
                reg-names = "phy", "core", "reader", "writer", "irq";
                clocks = <&sys_clk>;
                vmmc-supply = <&vreg_mmc>; /* <-------- HERE !!! */
                interrupt-parent = <&L1>;
                interrupts = <4>;
        };

Finally, I replaced the hardcoded setting of mmc->ocr_avail with a call to mmc_regulator_get_supply(mmc). Now, I get a bunch of timeouts during attempts to send e.g., CMD8 and CMD55.
(going for 3200000 and 3400000 for min- and max-microvolt, respectively,
-- or anything else in the allowed 2.7-3.6 range -- doesn't help either).

Quoted 5 lines
Quoted 2 lines

I might be doing something subtly wrong in the way I set things up above, but it feels a bit overengineered, and IMHO fragile.

At a quick glance, the above looks correct to me. Maybe there is something wrong with the code in the driver instead?

After some more hacking, I learned that:

- an additional regulator-name line (e.g. regulator-name = "vreg_mmc";) is required

- setting regulator-always-on; seems to help reduce attempts by the kernel to "manage" the regulator, but does not appear to be required

In other words:

        ...
        vreg_mmc: vreg_mmc {
                compatible = "regulator-fixed";
                regulator-name = "vreg_mmc";
                regulator-min-microvolt = <3300000>;
                regulator-max-microvolt = <3300000>;
                regulator-always-on;
        };
        ...

Additionally, CONFIG_REGULATOR=y and CONFIG_REGULATOR_FIXED_VOLTAGE=y *MUST* be enabled in the kernel's .config file, to prevent either litex_mmc_probe() from being deferred, or mmc_regulator_get_supply() from simply returning 0 without having set mmc->ocr_avail to anything at all!

Quoted 3 lines

Presumably this would also mean either select REGULATOR_FIXED_VOLTAGE or depends on REGULATOR_FIXED_VOLTAGE in the mmc driver's Kconfig entry.

Yep, that's correct.

If you don't like to manage that dependency in the Kconfig, an option is to check if mmc->ocr_avail is zero and if so, we could log a message *and* assign mmc->ocr_avail a default value.

Quoted 20 lines

Predictably, the "regulator-[min|max]-microvolt = <3300000>" setting gets us

        ocr_avail == MMC_VDD_32_33 | MMC_VDD_33_34
Quoted 14 lines
Quoted 9 lines

OTOH, going all out and setting:

        /* allow for generic 2.7-3.6V range, no software tuning available */
        mmc->ocr_avail = MMC_VDD_27_28 | MMC_VDD_28_29 | MMC_VDD_29_30 |
                         MMC_VDD_30_31 | MMC_VDD_31_32 | MMC_VDD_32_33 |
                         MMC_VDD_33_34 | MMC_VDD_34_35 | MMC_VDD_35_36;

seems to work just fine... :) Please do let me know what you think!

No, this isn't the way we want it to work. That's because it means that we would lie to the card about what voltage range the HW actually supports.

Quoted 5 lines
Quoted 1 line

It's better to let the DTS file give that information about the HW.

I may be needlessly concerned, but it feels a bit weird to me to drag in CONFIG_REGULATOR_FIXED_VOLTAGE as an added dependency for what is ultimately a roundabout way of setting a constant... :)

The point is, it shouldn't really be a constant set by the driver, because it would mean initialising a card under potentially wrong conditions.

However, I am fine assigning it a default value as a fallback and best effort, if it turns out that DT didn't provide us information about what the HW is capable of.

Quoted 2 lines

Thanks in advance for any additional clue!

Looks like there are two options, just pick one of them, then I am happy. :-)

Kind regards
Uffe

#17 lore raw

Re: [PATCH v5 3/3] mmc: Add driver for LiteX's LiteSDCard interface

Quoted 141 lines · Ulf Hansson

[...]

Quoted 99 lines
Quoted 17 lines
Quoted 17 lines
Quoted 16 lines

[...]

drivers/mmc/host/litex_mmc.c line 580

Quoted 2 lines
drivers/mmc/host/litex_mmc.c new file +2 -0
+
+       mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;

I noticed that you use these hard coded values and don't really care to manage voltage changes via ->set_ios().

Rather than doing it like this, I would prefer if you can hook up a fixed vmmc regulator in the DTS. Then call mmc_regulator_get_supply() to fetch it from here, which will let the mmc core create the mmc->ocr_avail mask, based upon the voltage level the regulator supports.

This becomes more generic and allows more flexibility for the platform configuration.

Quoted 7 lines
Quoted 4 lines

The LiteSDCard "hardware" (i.e., *gateware*) does not allow modification or selection of voltage from the software side. When a CMD8 is issued, the "voltage supplied" bit pattern is expected to be '0001b', which per the spec means "2.7-3.6V".

If you provide a range (2.7-3.6V), that means that your hardware supports the entire range, not just one single part of it.

The "gateware" (open source migen/verilog at
https://github.com/enjoy-digital/litesdcard)
supports any value provided by the underlying FPGA dev board (typically 3.3v) -- by not attempting to manage it in any way.

SD media presumably doesn't care as long as voltage is somewhere within 2.7-3.6V (at least that's how I read the spec, there's only one register value representing anything within that range).

Quoted 32 lines
Quoted 32 lines

I tried adding this to the overall DTS:

        vreg_mmc: vreg_mmc_3v {
                compatible = "regulator-fixed";
                regulator-min-microvolt = <3300000>;
                regulator-max-microvolt = <3300000>;
        };

and then added a reference to it to the LiteSDCard "mmc0" node in DTS, like so:

        mmc0: mmc@12005000 {
                compatible = "litex,mmc";
                reg = <0x12005000 0x100>,
                        <0x12003800 0x100>,
                        <0x12003000 0x100>,
                        <0x12004800 0x100>,
                        <0x12004000 0x100>;
                reg-names = "phy", "core", "reader", "writer", "irq";
                clocks = <&sys_clk>;
                vmmc-supply = <&vreg_mmc>; /* <-------- HERE !!! */
                interrupt-parent = <&L1>;
                interrupts = <4>;
        };

Finally, I replaced the hardcoded setting of mmc->ocr_avail with a call to mmc_regulator_get_supply(mmc). Now, I get a bunch of timeouts during attempts to send e.g., CMD8 and CMD55.
(going for 3200000 and 3400000 for min- and max-microvolt, respectively,
-- or anything else in the allowed 2.7-3.6 range -- doesn't help either).

Quoted 5 lines
Quoted 2 lines

I might be doing something subtly wrong in the way I set things up above, but it feels a bit overengineered, and IMHO fragile.

At a quick glance, the above looks correct to me. Maybe there is something wrong with the code in the driver instead?

After some more hacking, I learned that:

- an additional regulator-name line (e.g. regulator-name = "vreg_mmc";) is required

- setting regulator-always-on; seems to help reduce attempts by the kernel to "manage" the regulator, but does not appear to be required

In other words:

        ...
        vreg_mmc: vreg_mmc {
                compatible = "regulator-fixed";
                regulator-name = "vreg_mmc";
                regulator-min-microvolt = <3300000>;
                regulator-max-microvolt = <3300000>;
                regulator-always-on;
        };
        ...

Additionally, CONFIG_REGULATOR=y and CONFIG_REGULATOR_FIXED_VOLTAGE=y *MUST* be enabled in the kernel's .config file, to prevent either litex_mmc_probe() from being deferred, or mmc_regulator_get_supply() from simply returning 0 without having set mmc->ocr_avail to anything at all!

Quoted 3 lines

Presumably this would also mean either select REGULATOR_FIXED_VOLTAGE or depends on REGULATOR_FIXED_VOLTAGE in the mmc driver's Kconfig entry.

Yep, that's correct.

If you don't like to manage that dependency in the Kconfig, an option is to check if mmc->ocr_avail is zero and if so, we could log a message *and* assign mmc->ocr_avail a default value.

Quoted 20 lines

Predictably, the "regulator-[min|max]-microvolt = <3300000>" setting gets us

        ocr_avail == MMC_VDD_32_33 | MMC_VDD_33_34
Quoted 14 lines
Quoted 9 lines

OTOH, going all out and setting:

        /* allow for generic 2.7-3.6V range, no software tuning available */
        mmc->ocr_avail = MMC_VDD_27_28 | MMC_VDD_28_29 | MMC_VDD_29_30 |
                         MMC_VDD_30_31 | MMC_VDD_31_32 | MMC_VDD_32_33 |
                         MMC_VDD_33_34 | MMC_VDD_34_35 | MMC_VDD_35_36;

seems to work just fine... :) Please do let me know what you think!

No, this isn't the way we want it to work. That's because it means that we would lie to the card about what voltage range the HW actually supports.

Quoted 5 lines
Quoted 1 line

It's better to let the DTS file give that information about the HW.

I may be needlessly concerned, but it feels a bit weird to me to drag in CONFIG_REGULATOR_FIXED_VOLTAGE as an added dependency for what is ultimately a roundabout way of setting a constant... :)

The point is, it shouldn't really be a constant set by the driver, because it would mean initialising a card under potentially wrong conditions.

Quoted 8 lines

However, I am fine assigning it a default value as a fallback and best effort, if it turns out that DT didn't provide us information about what the HW is capable of.

Quoted 2 lines

Thanks in advance for any additional clue!

Looks like there are two options, just pick one of them, then I am happy. :-)

Sounds like a plan! I'll send out lucky v13 once I've had a chance to test it on my FPGA, later this evening.

Thanks again,
--Gabriel