Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fall back to max eMMC clock to fix Kingston eMMC compat with ODROID-N2 #3665

Merged
merged 1 commit into from
Nov 9, 2024

Conversation

sairon
Copy link
Member

@sairon sairon commented Nov 7, 2024

Replace the original patch that limits the frequency to 24 MHz with a more sophisticated version that first tries to initialize the eMMC at this limited frequency, and if it fails, then tries disabling the limit to use one defined in the upstream driver.

While still hacky, this seems to finally fix the Kingston eMMC issues, and with the current implementation, the initialization should appear the same as on older HAOS versions, as 24 MHz is used first. Better solution that addresses the root cause on some eMMCs (128 GB Microns?) would be still nice though...

refs #3250, refs #3227

Summary by CodeRabbit

  • New Features

    • Introduced a maximum clock frequency limit of 24 MHz for eMMC cards on the Meson GX platform to enhance compatibility during initialization.
  • Bug Fixes

    • Updated driver behavior to disable the frequency cap if initialization fails, allowing for reinitialization at higher clock rates when necessary.

Replace the original patch that limits the frequency to 24 MHz with a more
sophisticated version that first tries to initialize the eMMC at this limited
frequency, and if it fails, then tries disabling the limit to use one defined
in the upstream driver.

While still hacky, this seems to finally fix the Kingston eMMC issues, and with
the current implementation, the initialization should appear the same as on
older HAOS versions, as 24 MHz is used first. Better solution that addresses
the root cause on some eMMCs (128 GB Microns?) would be still nice though...

refs #3250, refs #3227
@sairon sairon added board/odroid Hardkernel's ODROID Boards u-boot U-Boot related issue labels Nov 7, 2024
@sairon sairon requested a review from agners November 7, 2024 13:13
Copy link

coderabbitai bot commented Nov 7, 2024

📝 Walkthrough

Walkthrough

The pull request introduces changes to the MMC driver for the Meson GX platform in U-Boot, specifically to limit the maximum clock frequency for eMMC cards during initialization. A new boolean flag, meson_gx_f_max_hack, is added to the MMC structure to facilitate this limit. The maximum clock frequency is set to 24 MHz, replacing the previous limit of 100 MHz. Modifications were made to the mmc_set_clock function to enforce this limit and allow reinitialization without it if needed.

Changes

File Path Change Summary
drivers/mmc/meson_gx_mmc.c Updated f_max from 100000000 to SD_EMMC_CLKSRC_24M in meson_mmc_probe function.
drivers/mmc/mmc.c Added logic in mmc_set_clock to check meson_gx_f_max_hack flag and limit clock to 24 MHz.
include/mmc.h Added bool meson_gx_f_max_hack to struct mmc.

Sequence Diagram(s)

sequenceDiagram
    participant Driver
    participant MMC
    participant eMMC

    Driver->>MMC: Initialize
    MMC->>eMMC: Set clock frequency
    alt meson_gx_f_max_hack is true
        MMC->>eMMC: Limit clock to 24 MHz
    else
        MMC->>eMMC: Set clock to requested frequency
    end
    eMMC-->>MMC: Initialization complete
    MMC-->>Driver: Return status
Loading

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @coderabbitai title anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (6)
buildroot-external/board/hardkernel/patches/uboot/0001-HACK-mmc-meson-gx-limit-f_max-to-24-MHz-on-the-first.patch (6)

1-26: Consider enhancing the commit message documentation.

While the commit message explains the approach well, it would be helpful to:

  1. Document specific Kingston eMMC models affected
  2. Explain why Kingston chips work better at 52 MHz
  3. Consider removing "HACK" from the subject if this is intended as a permanent solution

31-38: Add documentation for the meson_gx_f_max_hack flag.

The flag's purpose and impact should be documented in the code. Consider adding a comment explaining:

  • Why it's enabled by default
  • Which devices/cards are affected
  • The performance implications
 	cfg->b_max = 511; /* max 512 - 1 blocks */
 	cfg->name = dev->name;
 
+	/* Enable 24MHz frequency limit by default for better compatibility.
+	 * This may be disabled later if card initialization fails.
+	 * Known to affect: Kingston eMMC cards on ODROID-N2
+	 */
+	mmc->meson_gx_f_max_hack = true;
+
 	mmc->priv = pdata;

44-54: Consider improving the clock limit implementation.

The clock limiting logic is correct but could be more robust:

  1. Consider using a named constant for 24MHz instead of magic number
  2. Add debug logging to help troubleshoot timing issues
+#define MESON_GX_SAFE_CLOCK_HZ 24000000
+
 	/* Apply 24 MHz limit that fixes issues with some cards on meson. */
-	if (mmc->meson_gx_f_max_hack && clock > 24000000)
-		clock = 24000000;
+	if (mmc->meson_gx_f_max_hack && clock > MESON_GX_SAFE_CLOCK_HZ) {
+		debug("mmc: limiting clock from %d Hz to %d Hz\n",
+		      clock, MESON_GX_SAFE_CLOCK_HZ);
+		clock = MESON_GX_SAFE_CLOCK_HZ;
+	}

59-66: Enhance error reporting and handling.

The error handling could be improved to:

  1. Provide more detailed error information
  2. Log the actual error code
  3. Mention that a retry will be attempted at higher frequency
 		if (err && mmc->meson_gx_f_max_hack) {
 			/* Some eMMCs (namely Kingston) do not initialize at limited frequency. */
-			printf("Card failed to initialize at %d Hz, disabling meson_gx hack.\n",
-			       mmc->clock);
+			printf("MMC initialization failed (err=%d) at %d Hz\n",
+			       err, mmc->clock);
+			printf("Retrying with maximum supported frequency...\n");
 			mmc->meson_gx_f_max_hack = false;
 			err = mmc_select_mode_and_width(mmc, mmc->card_caps);
+			if (err)
+				printf("MMC initialization failed again (err=%d)\n", err);
 		}

73-79: Document the meson_gx_f_max_hack field.

Consider adding documentation for the new field to explain its purpose and behavior.

 	enum bus_mode user_speed_mode; /* input speed mode from user */
 
-	bool meson_gx_f_max_hack;
+	/* When true, limits initial clock to 24MHz for compatibility.
+	 * This is automatically disabled and retried if initialization fails.
+	 */
+	bool meson_gx_f_max_hack;

1-81: Consider a more robust long-term solution.

While this implementation provides a working solution for Kingston eMMC compatibility, consider these architectural improvements for the future:

  1. Make the initial frequency limit configurable through device tree or U-Boot environment variables
  2. Implement card vendor/model detection to apply the hack selectively
  3. Consider adding support for other frequency steps in the fallback sequence

This would provide more flexibility and better maintainability for future compatibility issues.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between f612d42 and e628463.

📒 Files selected for processing (2)
  • buildroot-external/board/hardkernel/patches/uboot/0001-HACK-mmc-meson-gx-limit-f_max-to-24-MHz-on-the-first.patch (1 hunks)
  • buildroot-external/board/hardkernel/patches/uboot/0001-HACK-mmc-meson-gx-limit-to-24MHz.patch (0 hunks)
💤 Files with no reviewable changes (1)
  • buildroot-external/board/hardkernel/patches/uboot/0001-HACK-mmc-meson-gx-limit-to-24MHz.patch

@sairon
Copy link
Member Author

sairon commented Nov 7, 2024

I ran an automated test where the ODROID cold-booted over 100 times in a row without issues. Now I'm running another one where I'm leaving it to boot to the system and run a reboot afterwards, now at 30 attempts at counting. Looking good on my end.

@frenck frenck merged commit 0dc9c65 into dev Nov 9, 2024
9 checks passed
@frenck frenck deleted the odroid-n2-improved-mmc-hack branch November 9, 2024 18:04
@sairon sairon mentioned this pull request Nov 19, 2024
@sairon sairon mentioned this pull request Dec 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
board/odroid Hardkernel's ODROID Boards cla-signed u-boot U-Boot related issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants