当前位置: 首页 > 工具软件 > kernel-msm > 使用案例 >

Linux内核4.14版本——mmc host(4)——host实例(sdhci-msm说明)

董喜
2023-12-01

1. 说明

       sdhci-msm是指高通的mmc host,其使用了标准SDHC标准。故可以使用前面说的《host(第二章)——sdhci》和《host(第三章)——sdhci-pltfm说明》的接口。
      后续代码以msm8916平台的host实现以及linux 4.14版本中的sdhci-msm的实现为例,这部分代码都是开源的。
      由于有一些寄存器内容需要文档的支撑但我们并没有,所以这里只是简单地介绍一下设计思想和代码结构。

     源码:drivers\mmc\host\sdhci-msm.c

2. dtsi节点

    aliases {
        sdhc1 = &sdhc_1; /* SDC1 eMMC slot */
    };

        sdhc_1: sdhci@07824000 {
            compatible = "qcom,sdhci-msm-v4";
                        // 会和sdhci-msm.c的driver匹配

            reg = <0x07824900 0x11c>, <0x07824000 0x800>;
            reg-names = "hc_mem", "core_mem";    
                        // 两部分寄存器,hc_mem表示sdhci使用的寄存器,core_mem表示msm host独立于sdhci标准之外的、自己需要使用的寄存器
                        // 因为驱动里面会使用sdhci-pltfm来进行解析,所以这里必须把hc_mem放在寄存器的第一个属性,具体参考《host(第三章)——sdhci-pltfm说明》

            interrupts = <0 123 0>, <0 138 0>;
            interrupt-names = "hc_irq", "pwr_irq";
                        // 两部分中断,hc_irq表示sdhci使用的中断,core_mem表示msm host检测host电源状态的中断、独立于sdhci标准
                        // 因为驱动里面会使用sdhci-pltfm来进行解析,所以这里必须把hc_irq放在中断的第一个属性,具体参考《host(第三章)——sdhci-pltfm说明》

            clocks = <&gcc GCC_SDCC1_APPS_CLK>,
                 <&gcc GCC_SDCC1_AHB_CLK>;
            clock-names = "core", "iface";
                        // 两个时钟
                        // core->GCC_SDCC1_APPS_CLK,工作时钟,也就是输出时钟
                        // iface->GCC_SDCC1_AHB_CLK,总线时钟

            bus-width = <8>;
                        // 总线宽度设置为8
            non-removable;
                        // 设置为不可移除
            status = "disabled";
        };

3. 数据结构

3.1 struct sdhci_msm_host

      sdhci-msm host driver根据自身资源定制的host结构体:

struct sdhci_msm_host {
    struct platform_device *pdev;    // 对应dtsi节点解析出来的平台设备
    void __iomem *core_mem; /* MSM SDCC mapped address */    // host自身的寄存器基地址
    struct clk *clk;    /* main SD/MMC bus clock */    // 工作时钟,对应
    struct clk *pclk;   /* SDHC peripheral bus clock */
    struct clk *bus_clk;    /* SDHC bus voter clock */
    struct mmc_host *mmc;    // 对应的mmc_host结构体,具体参考《mmc core》
};

3.2 struct sdhci_pltfm_data

      struct sdhci_pltfm_data类型。
      提供给sdhci-pltfm接口使用的平台数据结构体,定义了sdhci_host的ops、quirks和quirks2。会在调用sdhci_pltfm_init生成sdhci_host的时候使用。
      具体参考《host(第三章)——sdhci-pltfm说明》

static const struct sdhci_pltfm_data sdhci_msm_pdata = {
	.quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
		  SDHCI_QUIRK_NO_CARD_NO_RESET |
		  SDHCI_QUIRK_SINGLE_POWER_WRITE |
		  SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
		  SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12,

	.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
	.ops = &sdhci_msm_ops,
};

3.3 struct sdhci_ops sdhci_msm_ops

static const struct sdhci_ops sdhci_msm_ops = {
	.reset = sdhci_reset,
	.set_clock = sdhci_msm_set_clock,
	.get_min_clock = sdhci_msm_get_min_clock,
	.get_max_clock = sdhci_msm_get_max_clock,
	.set_bus_width = sdhci_set_bus_width,
	.set_uhs_signaling = sdhci_msm_set_uhs_signaling,
	.voltage_switch = sdhci_msm_voltage_switch,
	.write_w = sdhci_msm_write_w,
};

      struct sdhci_ops类型。
      为sdhci_host提供出sdhci标准之外的一些实际的硬件操作方法的操作集给sdhci core。

4. 代码说明

4.1 设备驱动代码

static const struct dev_pm_ops sdhci_msm_pm_ops = {
	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
				pm_runtime_force_resume)
	SET_RUNTIME_PM_OPS(sdhci_msm_runtime_suspend,
			   sdhci_msm_runtime_resume,
			   NULL)
};

static struct platform_driver sdhci_msm_driver = {
	.probe = sdhci_msm_probe,
	.remove = sdhci_msm_remove,
	.driver = {
		   .name = "sdhci_msm",
		   .of_match_table = sdhci_msm_dt_match,
		   .pm = &sdhci_msm_pm_ops,
	},
};

module_platform_driver(sdhci_msm_driver);

4.2 sdhci_msm_probe

static int sdhci_msm_probe(struct platform_device *pdev)
{
	struct sdhci_host *host;
	struct sdhci_pltfm_host *pltfm_host;
	struct sdhci_msm_host *msm_host;
	struct resource *core_memres;
	int ret;
	u16 host_version, core_minor;
	u32 core_version, config;
	u8 core_major;

	host = sdhci_pltfm_init(pdev, &sdhci_msm_pdata, sizeof(*msm_host));
	if (IS_ERR(host))
		return PTR_ERR(host);

	host->sdma_boundary = 0;
	pltfm_host = sdhci_priv(host);
	msm_host = sdhci_pltfm_priv(pltfm_host);
	msm_host->mmc = host->mmc;
	msm_host->pdev = pdev;

	ret = mmc_of_parse(host->mmc);
	if (ret)
		goto pltfm_free;

	sdhci_get_of_property(pdev);

	msm_host->saved_tuning_phase = INVALID_TUNING_PHASE;

	/* Setup SDCC bus voter clock. */
	msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus");
	if (!IS_ERR(msm_host->bus_clk)) {
		/* Vote for max. clk rate for max. performance */
		ret = clk_set_rate(msm_host->bus_clk, INT_MAX);
		if (ret)
			goto pltfm_free;
		ret = clk_prepare_enable(msm_host->bus_clk);
		if (ret)
			goto pltfm_free;
	}

	/* Setup main peripheral bus clock */
	msm_host->pclk = devm_clk_get(&pdev->dev, "iface");
	if (IS_ERR(msm_host->pclk)) {
		ret = PTR_ERR(msm_host->pclk);
		dev_err(&pdev->dev, "Peripheral clk setup failed (%d)\n", ret);
		goto bus_clk_disable;
	}

	ret = clk_prepare_enable(msm_host->pclk);
	if (ret)
		goto bus_clk_disable;

	/* Setup SDC MMC clock */
	msm_host->clk = devm_clk_get(&pdev->dev, "core");
	if (IS_ERR(msm_host->clk)) {
		ret = PTR_ERR(msm_host->clk);
		dev_err(&pdev->dev, "SDC MMC clk setup failed (%d)\n", ret);
		goto pclk_disable;
	}

	/*
	 * xo clock is needed for FLL feature of cm_dll.
	 * In case if xo clock is not mentioned in DT, warn and proceed.
	 */
	msm_host->xo_clk = devm_clk_get(&pdev->dev, "xo");
	if (IS_ERR(msm_host->xo_clk)) {
		ret = PTR_ERR(msm_host->xo_clk);
		dev_warn(&pdev->dev, "TCXO clk not present (%d)\n", ret);
	}

	/* Vote for maximum clock rate for maximum performance */
	ret = clk_set_rate(msm_host->clk, INT_MAX);
	if (ret)
		dev_warn(&pdev->dev, "core clock boost failed\n");

	ret = clk_prepare_enable(msm_host->clk);
	if (ret)
		goto pclk_disable;

	core_memres = platform_get_resource(pdev, IORESOURCE_MEM, 1);
	msm_host->core_mem = devm_ioremap_resource(&pdev->dev, core_memres);

	if (IS_ERR(msm_host->core_mem)) {
		dev_err(&pdev->dev, "Failed to remap registers\n");
		ret = PTR_ERR(msm_host->core_mem);
		goto clk_disable;
	}

	/* Reset the vendor spec register to power on reset state */
	writel_relaxed(CORE_VENDOR_SPEC_POR_VAL,
		       host->ioaddr + CORE_VENDOR_SPEC);

	/* Set HC_MODE_EN bit in HC_MODE register */
	writel_relaxed(HC_MODE_EN, (msm_host->core_mem + CORE_HC_MODE));

	config = readl_relaxed(msm_host->core_mem + CORE_HC_MODE);
	config |= FF_CLK_SW_RST_DIS;
	writel_relaxed(config, msm_host->core_mem + CORE_HC_MODE);

	host_version = readw_relaxed((host->ioaddr + SDHCI_HOST_VERSION));
	dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n",
		host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >>
			       SDHCI_VENDOR_VER_SHIFT));

	core_version = readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION);
	core_major = (core_version & CORE_VERSION_MAJOR_MASK) >>
		      CORE_VERSION_MAJOR_SHIFT;
	core_minor = core_version & CORE_VERSION_MINOR_MASK;
	dev_dbg(&pdev->dev, "MCI Version: 0x%08x, major: 0x%04x, minor: 0x%02x\n",
		core_version, core_major, core_minor);

	if (core_major == 1 && core_minor >= 0x42)
		msm_host->use_14lpp_dll_reset = true;

	/*
	 * SDCC 5 controller with major version 1, minor version 0x34 and later
	 * with HS 400 mode support will use CM DLL instead of CDC LP 533 DLL.
	 */
	if (core_major == 1 && core_minor < 0x34)
		msm_host->use_cdclp533 = true;

	/*
	 * Support for some capabilities is not advertised by newer
	 * controller versions and must be explicitly enabled.
	 */
	if (core_major >= 1 && core_minor != 0x11 && core_minor != 0x12) {
		config = readl_relaxed(host->ioaddr + SDHCI_CAPABILITIES);
		config |= SDHCI_CAN_VDD_300 | SDHCI_CAN_DO_8BIT;
		writel_relaxed(config, host->ioaddr +
			       CORE_VENDOR_SPEC_CAPABILITIES0);
	}

	/*
	 * Power on reset state may trigger power irq if previous status of
	 * PWRCTL was either BUS_ON or IO_HIGH_V. So before enabling pwr irq
	 * interrupt in GIC, any pending power irq interrupt should be
	 * acknowledged. Otherwise power irq interrupt handler would be
	 * fired prematurely.
	 */
	sdhci_msm_voltage_switch(host);

	/*
	 * Ensure that above writes are propogated before interrupt enablement
	 * in GIC.
	 */
	mb();

	/* Setup IRQ for handling power/voltage tasks with PMIC */
	msm_host->pwr_irq = platform_get_irq_byname(pdev, "pwr_irq");
	if (msm_host->pwr_irq < 0) {
		dev_err(&pdev->dev, "Get pwr_irq failed (%d)\n",
			msm_host->pwr_irq);
		ret = msm_host->pwr_irq;
		goto clk_disable;
	}

	/* Enable pwr irq interrupts */
	writel_relaxed(INT_MASK, msm_host->core_mem + CORE_PWRCTL_MASK);

	ret = devm_request_threaded_irq(&pdev->dev, msm_host->pwr_irq, NULL,
					sdhci_msm_pwr_irq, IRQF_ONESHOT,
					dev_name(&pdev->dev), host);
	if (ret) {
		dev_err(&pdev->dev, "Request IRQ failed (%d)\n", ret);
		goto clk_disable;
	}

	pm_runtime_get_noresume(&pdev->dev);
	pm_runtime_set_active(&pdev->dev);
	pm_runtime_enable(&pdev->dev);
	pm_runtime_set_autosuspend_delay(&pdev->dev,
					 MSM_MMC_AUTOSUSPEND_DELAY_MS);
	pm_runtime_use_autosuspend(&pdev->dev);

	host->mmc_host_ops.execute_tuning = sdhci_msm_execute_tuning;
	ret = sdhci_add_host(host);
	if (ret)
		goto pm_runtime_disable;

	pm_runtime_mark_last_busy(&pdev->dev);
	pm_runtime_put_autosuspend(&pdev->dev);

	return 0;

pm_runtime_disable:
	pm_runtime_disable(&pdev->dev);
	pm_runtime_set_suspended(&pdev->dev);
	pm_runtime_put_noidle(&pdev->dev);
clk_disable:
	clk_disable_unprepare(msm_host->clk);
pclk_disable:
	clk_disable_unprepare(msm_host->pclk);
bus_clk_disable:
	if (!IS_ERR(msm_host->bus_clk))
		clk_disable_unprepare(msm_host->bus_clk);
pltfm_free:
	sdhci_pltfm_free(pdev);
	return ret;
}

主要工作:

      调用调用sdhci_pltfm_init为sdhci_host、sdhci_pltfm_host、sdhci_msm_host分配内存、设置
      关联mmc_host、sdhci_host、sdhci_pltfm_host、sdhci_msm_host
      解析dtsi属性设置到mmc_host和sdhci_host中
      获取各种时钟
      获取host独立于sdhci的寄存器基地址
      调用sdhci_add_host将sdhci_host注册到sdhci core中,相应的mmc_host也会被调用到mmc core中了。host的注册就完成了。

 类似资料: