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

From: Stafford Horne
Date: Mon Jan 10 2022 - 17:07:54 EST


On Sun, Jan 09, 2022 at 06:20:03PM -0500, Gabriel Somlo wrote:
> 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!
>
> Co-developed-by: Kamil Rakoczy <krakoczy@xxxxxxxxxxxx>
> Signed-off-by: Kamil Rakoczy <krakoczy@xxxxxxxxxxxx>
> Co-developed-by: Maciej Dudek <mdudek@xxxxxxxxxxxxxxxxxxxxxxxx>
> Signed-off-by: Maciej Dudek <mdudek@xxxxxxxxxxxxxxxxxxxxxxxx>
> Co-developed-by: Paul Mackerras <paulus@xxxxxxxxxx>
> Signed-off-by: Paul Mackerras <paulus@xxxxxxxxxx>
> Signed-off-by: Gabriel Somlo <gsomlo@xxxxxxxxx>
> Reviewed-by: Joel Stanley <joel@xxxxxxxxx>
> Reviewed-by: Andy Shevchenko <andy.shevchenko@xxxxxxxxx>
> ---
>
> New in v11:
> - picked up r/b from Andy
> drivers/mmc/host/litex_mmc.c:
> - defensive coding of litex_mmc_interrupt() return logic
> - remove `dev` member of `struct litex_mmc_host`, only used during probe
>
> >New in v10:
> >drivers/mmc/host/litex_mmc.c:
> > - group `linux/mmc/*` includes by themselves
> > - clean-up of `return` style (multiple locations throughout source)
> > - create `mmc_free_host()` wrapper for use with
> > `devm_add_action_or_reset()`
> > - use GFP_KERNEL with `dmam_alloc_coherent()`
> >
> >>New in v9:
> >>drivers/mmc/host/Kconfig:
> >> - fix OF dependency
> >>drivers/mmc/host/litex_mmc.c:
> >> - remove `linux/of.h` include, no longer needed since dropping
> >> `of_match_ptr()`
> >> - add `linux/mod_devicetable.h` include
> >> - use devm_action_or_reset() to devm-ify mmc_alloc_host(), and obviate
> >> the need to call mmc_free_host() explicitly during either probe()
> >> error path or during remove()
> >>
> >>>New in v8:
> >>> - remove `Cc:` lines from commit blurb
> >>>drivers/mmc/host/litex_mmc.c:
> >>> - fix file header comment (for real, this time)
> >>> - add explicit `bits.h` include
> >>> - remove `of_match_ptr()` wrapper from around .of_match_table argument
> >>> - fix devm ordering issues: use `devm_request_irq()`, which precludes
> >>> the need to call `free_irq()` on `probe()` error path or from `remove()`
> >>>
> >>>>New in v7:
> >>>>drivers/mmc/host/Kconfig:
> >>>> - added module name in LiteSDCard Kconfig entry
> >>>>drivers/mmc/host/litex_mmc.c:
> >>>> - fixed comment formatting, ordering, and capitalization throughout
> >>>> the entire file
> >>>> - sorted header #include statements
> >>>> - removed redundant parantheses in readx_poll_timeout() condition
> >>>> - explicit handling of readx_poll_timeout() timeout scenarios
> >>>> - dev_err() used in litex_mmc_sdcard_wait_done()
> >>>> - use memcpy_fromio() to grab command response
> >>>> - no need to apply 0xffff mask to a 32-bit value right-shifted by 16
> >>>> (host->resp[3])
> >>>> - use clamp() instead of min(max(...)...)
> >>>> - reworked platform_get_irq_optional() error handling logic
> >>>> - no need to explicitly zero host->irq, kzalloc() does that already
> >>>> - added missing free_irq() in litex_mmc_probe() error path
> >>>> - reordered calls inside litex_mmc_remove() (calling mmc_free_host()
> >>>> before free_irq()
> >>>>
> >>>>>New in v6:
> >>>>> - fix handling of deferred probe vs. platform_get_irq_optional()
> >>>>> - don't #ifdef dma_set_mask_and_coherent(), since it automatically
> >>>>> does the right thing on both 32- and 64-bit DMA capable arches
> >>>>> - remove MMC_CAP2_FULL_PWR_CYCLE, add MMC_CAP2_NO_MMC to list of
> >>>>> hardcoded capabilities during litex_mmc_probe()
> >>>>> - hardcode mmc->ocr_avail to the full 2.7-3.6V range allowed by the
> >>>>> SDCard spec (the LiteSDCard device doesn't accept software
> >>>>> configuration)
> >>>>>
> >>>>>>New in v5:
> >>>>>> - shorter #define constant names (cosmetic, make them less unwieldy)
> >>>>>> - picked up reviewed-by Joel
> >>>>>>
> >>>>>>>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()
> >>>>>>>
> >>>>>>>> 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 | 657 +++++++++++++++++++++++++++++++++++
> 3 files changed, 667 insertions(+)
> create mode 100644 drivers/mmc/host/litex_mmc.c
>
> diff --git a/drivers/mmc/host/Kconfig b/drivers/mmc/host/Kconfig
> index 5af8494c31b5..8f661f52b502 100644
> --- a/drivers/mmc/host/Kconfig
> +++ b/drivers/mmc/host/Kconfig
> @@ -1093,3 +1093,12 @@ config MMC_OWL
>
> config MMC_SDHCI_EXTERNAL_DMA
> bool
> +
> +config MMC_LITEX
> + tristate "LiteX MMC Host Controller support"
> + depends on ((PPC_MICROWATT || LITEX) && OF) || COMPILE_TEST
> + help
> + This selects support for the MMC Host Controller found in LiteX SoCs.

See comment below.

> + To compile this driver as a module, choose M here: the
> + module will be called litex_mmc.

Can we add this here too?

If unsure, say N.

> diff --git a/drivers/mmc/host/Makefile b/drivers/mmc/host/Makefile
> index ea36d379bd3c..4e4ceb32c4b4 100644
> --- a/drivers/mmc/host/Makefile
> +++ b/drivers/mmc/host/Makefile
> @@ -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
> diff --git a/drivers/mmc/host/litex_mmc.c b/drivers/mmc/host/litex_mmc.c
> new file mode 100644
> index 000000000000..cc2848030e6b
> --- /dev/null
> +++ b/drivers/mmc/host/litex_mmc.c
> @@ -0,0 +1,657 @@
...
> +
> + /* Initialize clock source */
> + clk = devm_clk_get(dev, NULL);
> + if (IS_ERR(clk))
> + return dev_err_probe(dev, PTR_ERR(clk), "can't get clock\n");
> + host->ref_clk = clk_get_rate(clk);

Note, we used to have:

cpu = of_get_next_cpu_node(NULL);
ret = of_property_read_u32(cpu, "clock-frequency", &host->freq);
of_node_put(cpu);
if (ret) {
dev_err(&pdev->dev, "No \"clock-frequency\" property in DT\n");
goto err_free_host;
}

As we agree, this worked but was hacky. The new use of devm_clk_get above uses
the COMMON_CLK framework. Which is great but OpenRISC doesn't yet support
COMMON_CLK, so this driver no longer works on OpenRISC.

I think the ideal thing is to not change the driver code and I will just add
support for COMMON_CLK on openrisc (already done and tested). But as we
discussed off e-mail I think we should change the config constraints to.

depends on ((PPC_MICROWATT || LITEX) && OF && HAVE_CLK) || COMPILE_TEST

-Stafford