块设备驱动2--sd卡驱动1(linux-3.2.36的sdchi.h分析)

我的经历:

看了某些大神的说明,我胆怯地进入了drivers/mmc/host/。

由于用的是s3c2440,看驱动时,我首先会找s3c字样,于是我注意到sdhci-s3c.c和s3cmci.c。

看了一下Kconfig

config MMC_S3C
    tristate "Samsung S3C SD/MMC Card Interface support"
    depends on ARCH_S3C2410
    help
      This selects a driver for the MCI interface found in
          Samsung's S3C2410, S3C2412, S3C2440, S3C2442 CPUs.
      If you have a board based on one of those and a MMC/SD
      slot, say Y or M here.

      If unsure, say N.

再看Makefile

obj-$(CONFIG_MMC_S3C)       += s3cmci.o

我想我用s3cmci就可以了。我就猜测s3cmci和sdhci-s3c就是对应不一样的芯片。但是当我看了看代码,不完全是这样。

记得大神说过struct mmc_host。sd驱动用到的主要结构,但是sdhci-s3c用到的是struct sdhci_host。

这是什么情况。

首先我就考虑sdhci到底是什么,我看所有sdhci-*.c驱动都用到了sdhci.c,我看到sdhci.c对寄存器操作的偏移都

是一样的,这样我想到了usb的ochi驱动。应该有个sd控制器都遵循的协议。

然后我又打开了s3c6410的datasheet

第一行 SD Standard Host很耀眼。最后我在百度找的了这个:

Part_A2_SD_Host_Controller_Simplified_Specification_Ver2.00.pdf

这个就是所说的协议,sdhci.c要实现的东西。s3c2440并不遵循这个。

先看看sdhci吧,看c之前先看.h文件分析

/*
 *  linux/include/linux/mmc/sdhci.h - Secure Digital Host Controller Interface
 *
 *  Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 */
#ifndef LINUX_MMC_SDHCI_H
#define LINUX_MMC_SDHCI_H

#include <linux/scatterlist.h>
#include <linux/compiler.h>
#include <linux/types.h>
#include <linux/io.h>
#include <linux/mmc/host.h>

struct sdhci_host {
	/* Data set by hardware interface driver */
	const char *hw_name;	/* Hardware bus name */ //硬件总线名

	unsigned int quirks;	/* Deviations from spec. */ //偏离规范

//我们看有哪些怪癖,还有代码会如何处理
/* Controller doesn't honor resets unless we touch the clock register */
#define SDHCI_QUIRK_CLOCK_BEFORE_RESET			(1<<0)
/*
SDHCI_QUIRK_CLOCK_BEFORE_RESET 是有的要在复位前设置时钟寄存器
会在复位前调用host->ops->set_clock(host, clock);驱动提供
*/

/* Controller has bad caps bits, but really supports DMA */
#define SDHCI_QUIRK_FORCE_DMA				(1<<1)
/*
        if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
                host->flags |= SDHCI_USE_SDMA;
就是支持SDMA
*/

/* Controller doesn't like to be reset when there is no card inserted. */
#define SDHCI_QUIRK_NO_CARD_NO_RESET			(1<<2)

/*
上面英文已说的很清楚:
没有卡插入时,控制器不想复位。
在复位时遇到这个标准,会想尝试读当前的状态,成功返回,失败进行复位。
*/

/* Controller doesn't like clearing the power reg before a change */
#define SDHCI_QUIRK_SINGLE_POWER_WRITE			(1<<3)
/*
控制器不想清除电源寄存器在一个改变MMC_VDD前。
遇到这个,直接改电源寄存器值,不清除。
*/

/* Controller has flaky internal state so reset it on each ios change */
#define SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS		(1<<4)
/*
在sdhci_do_set_ios()中会用到。
解释:
有些控制器在一些操作时会发疯,即使是CMD0,解决方法就是复位每一个ios.
*/

/* Controller has an unusable DMA engine */
#define SDHCI_QUIRK_BROKEN_DMA				(1<<5)
//控制器有一个无法使用的DMA引擎

//SDMA: secure DMA 
//ADMA: advanced DMA 

/* Controller has an unusable ADMA engine */
#define SDHCI_QUIRK_BROKEN_ADMA				(1<<6)
//控制器有一个无法使用的ADMA引擎

/* Controller can only DMA from 32-bit aligned addresses */
#define SDHCI_QUIRK_32BIT_DMA_ADDR			(1<<7)
//DMA要32位地址对齐

/* Controller can only DMA chunk sizes that are a multiple of 32 bits */
#define SDHCI_QUIRK_32BIT_DMA_SIZE			(1<<8)
//DMA的大小是32的倍数

/* Controller can only ADMA chunks that are a multiple of 32 bits */
#define SDHCI_QUIRK_32BIT_ADMA_SIZE			(1<<9)
//ADMA的大小是32的倍数
//遇到这些地址和大小限制,会先判断数据。

/* Controller needs to be reset after each request to stay stable */
#define SDHCI_QUIRK_RESET_AFTER_REQUEST			(1<<10)
//每个请求之后控制器需要复位来保持稳定
/*
在sdhci_tasklet_finish()中,即任务完成时,
当host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST成立时,
执行复位。
*/

/* Controller needs voltage and power writes to happen separately */
#define SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER		(1<<11)
/*
这个标准是开启电源时,要先设置电压。
Marvell CaFe的芯片就是如此
代码,先设置,再或上SDHCI_POWER_ON开启
        if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
                sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);

        pwr |= SDHCI_POWER_ON;

        sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);

*/

/* Controller provides an incorrect timeout value for transfers */
#define SDHCI_QUIRK_BROKEN_TIMEOUT_VAL			(1<<12)
//控制器提供一个不正确的超时值。
/*
会用0xE作为timeout.就是让硬件有个长的超时值,比短的好。
        if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
                return 0xE;
*/

/* Controller has an issue with buffer bits for small transfers */
#define SDHCI_QUIRK_BROKEN_SMALL_PIO			(1<<13)
//控制器支持小字节传输
//有些控制器(例如:JMicron JMB38x)弄乱传输位,(小于4字节)。
//只要它有一个block,我们可以忽略这些位。
//寄存器PRNSTS(s3c6410有这个寄存器)的第11位(buffer read enable)会被忽略

/* Controller does not provide transfer-complete interrupt when not busy */
#define SDHCI_QUIRK_NO_BUSY_IRQ				(1<<14)
//当忙的时候,控制器不提供传输完成中断
//如果忙的话,linux会执行下面的代码
/*
        if (intmask & SDHCI_INT_RESPONSE)
                sdhci_finish_command(host);
*/

/* Controller has unreliable card detection */
#define SDHCI_QUIRK_BROKEN_CARD_DETECTION		(1<<15)
//不可靠的卡检测
/*
sdhci_set_card_detection()函数会判断返回:
        if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
                return;
*/

/* Controller reports inverted write-protect state */
#define SDHCI_QUIRK_INVERTED_WRITE_PROTECT		(1<<16)
/*
报告反的写保护状态值:
sdhci_check_ro()函数:
        return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
                !is_readonly : is_readonly;
*/

/* Controller has nonstandard clock management */
#define SDHCI_QUIRK_NONSTANDARD_CLOCK			(1<<17)
//非标准时钟管理
//不标准,你就自己搞吧
/*
sdhci_set_clock()

		if (host->ops->set_clock) {
                host->ops->set_clock(host, clock);
                if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
                        return;
        }

*/

/* Controller does not like fast PIO transfers */
#define SDHCI_QUIRK_PIO_NEEDS_DELAY			(1<<18)
//pio需要延时
//要延时就给他延时
/*
sdhci_transfer_pio()中
                if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
                        udelay(100);
*/

/* Controller losing signal/interrupt enable states after reset */
#define SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET		(1<<19)
//复位后会丢掉信号/中断使能状态
/*
在sdhci_reset()中,
先读出中断使能状态
        if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
                ier = sdhci_readl(host, SDHCI_INT_ENABLE);
复位完再设置一下
        if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
                sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK, ier);
*/

/* Controller has to be forced to use block size of 2048 bytes */
#define SDHCI_QUIRK_FORCE_BLK_SZ_2048			(1<<20)
//控制器被迫使用2048字节的块大小
/*
        if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
                mmc->max_blk_size = 2;
*/

/* Controller cannot do multi-block transfers */
#define SDHCI_QUIRK_NO_MULTIBLOCK			(1<<21)
//不能多块处理
/*
sdhci_add_host()中
        //Maximum block count.
        mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
可以看一下spec,有三种:
Single Block, Multi Block, Infinite Block.
*/

/* Controller can only handle 1-bit data transfers */
#define SDHCI_QUIRK_FORCE_1_BIT_DATA			(1<<22)
//一位数据传输
//不是1位就4位
/*
        if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
                mmc->caps |= MMC_CAP_4_BIT_DATA;
在这段代码之前有一句:
一个控制器可以支持8位宽度,但板子本身可能没有引脚引出。
板支持8位宽度必须在自己的平台代码调用sdhci_add_host()之
前,设置“MMC->caps |= MMC_CAP_8_BIT_DATA;”
对于8-bits不是同一的代码处理
通过sdhci_ops的platform_8bit_width(函数指针)自己处理。
*/

/* Controller needs 10ms delay between applying power and clock */
#define SDHCI_QUIRK_DELAY_AFTER_POWER			(1<<23)
//设置电源和时钟之间要10ms延时
/*
sdhci_set_power()

        if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
                mdelay(10);
*/

/* Controller uses SDCLK instead of TMCLK for data timeouts */
#define SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK		(1<<24)
//用SDCLK代替TMCLK作为数据超时
//SDCLK是sd总线时钟
//TMCLK是专门为了检测超时的时钟

/* Controller reports wrong base clock capability */
#define SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN		(1<<25)
//基本时钟功能报错
/*
        if (host->max_clk == 0 || host->quirks &
                        SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
                if (!host->ops->get_max_clock) {
                        pr_err("%s: Hardware doesn't specify base clock "
                               "frequency.\n", mmc_hostname(mmc));
                        return -ENODEV;
                }
                host->max_clk = host->ops->get_max_clock(host);
        }
可以看出max_clk为0时,我们可以通过host->ops->get_max_clock()获得
*/

/* Controller cannot support End Attribute in NOP ADMA descriptor */
#define SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC		(1<<26)
//不支持NOP ADMA属性
/*
在sdhci_adma_table_pre()中
如果有此标志就不调用
                // nop, end, valid 
                sdhci_set_adma_desc(desc, 0, 0, 0x3);
*/

/* Controller is missing device caps. Use caps provided by host */
#define SDHCI_QUIRK_MISSING_CAPS			(1<<27)
//缺少设备caps,使用主机的caps
/*
sdhci_add_host()
        caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
                sdhci_readl(host, SDHCI_CAPABILITIES);
*/

/* Controller uses Auto CMD12 command to stop the transfer */
#define SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12		(1<<28)
//spec中说的Auto CMD12(停止命令)
/*
最终会通过mode写到寄存里
sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
*/

/* Controller doesn't have HISPD bit field in HI-SPEED SD card */
#define SDHCI_QUIRK_NO_HISPD_BIT			(1<<29)
//没有HISPD位
//这个会通过ctrl写入
/*
 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
*/

/* Controller treats ADMA descriptors with length 0000h incorrectly */
#define SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC		(1<<30)
//长度为0的ADAM描述符认为不正确
/*
                if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
                        mmc->max_seg_size = 65535;
                else
                        mmc->max_seg_size = 65536;
这个max_seg_size是做分散/聚合是每个条目不能大于64k
*/

/* The read-only detection via SDHCI_PRESENT_STATE register is unstable */
#define SDHCI_QUIRK_UNSTABLE_RO_DETECT			(1<<31)
//通过PRESENT_STATE寄存器检测read-only是不准的
//如果这样就多次检测
/*
        ro_count = 0;
        for (i = 0; i < SAMPLE_COUNT; i++) {
                if (sdhci_check_ro(host)) {
                        if (++ro_count > SAMPLE_COUNT / 2)
                                return 1;
                }
                msleep(30);
        }
*/

	unsigned int quirks2;	/* More deviations from spec. */

#define SDHCI_QUIRK2_OWN_CARD_DETECTION			(1<<0)
//自己的卡检测
/*
和上面的SDHCI_QUIRK_BROKEN_CARD_DETECTION一样
*/

	int irq;		/* Device IRQ */ 
	void __iomem *ioaddr;	/* Mapped address */

	const struct sdhci_ops *ops;	/* Low level hw interface */
/*
主要是硬件操作接口
struct sdhci_ops {
#ifdef CONFIG_MMC_SDHCI_IO_ACCESSORS
        u32             (*read_l)(struct sdhci_host *host, int reg);
        u16             (*read_w)(struct sdhci_host *host, int reg);
        u8              (*read_b)(struct sdhci_host *host, int reg);
        void            (*write_l)(struct sdhci_host *host, u32 val, int reg);
        void            (*write_w)(struct sdhci_host *host, u16 val, int reg);
        void            (*write_b)(struct sdhci_host *host, u8 val, int reg);
#endif
上面你不设置就使用内核提供的readl等

        void    (*set_clock)(struct sdhci_host *host, unsigned int clock);//设置时钟

        int             (*enable_dma)(struct sdhci_host *host);//使能dma
        unsigned int    (*get_max_clock)(struct sdhci_host *host);//获取最大时钟
        unsigned int    (*get_min_clock)(struct sdhci_host *host);//获取最小时钟
        unsigned int    (*get_timeout_clock)(struct sdhci_host *host);//获得超时时钟
        int             (*platform_8bit_width)(struct sdhci_host *host,
                                               int width);//八位传输操作
        void (*platform_send_init_74_clocks)(struct sdhci_host *host,
                                             u8 power_mode);//只在sdhci-pxav3.c看到使用,不清楚干嘛
        unsigned int    (*get_ro)(struct sdhci_host *host);//获取readonly状态
        void    (*platform_reset_enter)(struct sdhci_host *host, u8 mask);//复位之前处理
        void    (*platform_reset_exit)(struct sdhci_host *host, u8 mask);//复位之后处理
        int     (*set_uhs_signaling)(struct sdhci_host *host, unsigned int uhs);//uhs:Ultra High Speed 设置超高速信号
        void    (*hw_reset)(struct sdhci_host *host);//复位
};

*/
	struct regulator *vmmc;	/* Power regulator *///电源调节器

	/* Internal data */
	struct mmc_host *mmc;	/* MMC structure */ //大名鼎鼎的mmc结构
	u64 dma_mask;		/* custom DMA mask */

#if defined(CONFIG_LEDS_CLASS) || defined(CONFIG_LEDS_CLASS_MODULE)
	struct led_classdev led;	/* LED control */
	char led_name[32];
#endif

	spinlock_t lock;	/* Mutex */

	int flags;		/* Host attributes */
#define SDHCI_USE_SDMA		(1<<0)	/* Host is SDMA capable */
#define SDHCI_USE_ADMA		(1<<1)	/* Host is ADMA capable */
#define SDHCI_REQ_USE_DMA	(1<<2)	/* Use DMA for this req. */
#define SDHCI_DEVICE_DEAD	(1<<3)	/* Device unresponsive */
#define SDHCI_SDR50_NEEDS_TUNING (1<<4)	/* SDR50 needs tuning */
#define SDHCI_NEEDS_RETUNING	(1<<5)	/* Host needs retuning */
#define SDHCI_AUTO_CMD12	(1<<6)	/* Auto CMD12 support */
#define SDHCI_AUTO_CMD23	(1<<7)	/* Auto CMD23 support */
#define SDHCI_PV_ENABLED	(1<<8)	/* Preset value enabled */
#define SDHCI_SDIO_IRQ_ENABLED	(1<<9)	/* SDIO irq enabled */

	unsigned int version;	/* SDHCI spec. version *///版本,目前是1.0 2.0 3.0

	unsigned int max_clk;	/* Max possible freq (MHz) */
	unsigned int timeout_clk;	/* Timeout freq (KHz) */
	unsigned int clk_mul;	/* Clock Muliplier value */

	unsigned int clock;	/* Current clock (MHz) */
	u8 pwr;			/* Current voltage */

	bool runtime_suspended;	/* Host is runtime suspended */ //挂起标志

	struct mmc_request *mrq;	/* Current request */
	struct mmc_command *cmd;	/* Current command */
	struct mmc_data *data;	/* Current data request */
	unsigned int data_early:1;	/* Data finished before cmd *///数据管理完成在命令完成之前

	struct sg_mapping_iter sg_miter;	/* SG state for PIO *///sg 状态
	unsigned int blocks;	/* remaining PIO blocks */ //剩余PIO的blocks

	int sg_count;		/* Mapped sg entries *///映射的SG项数目

	u8 *adma_desc;		/* ADMA descriptor table */ //AMDA描述符表
	u8 *align_buffer;	/* Bounce buffer */ //反弹缓冲

	dma_addr_t adma_addr;	/* Mapped ADMA descr. table */
	dma_addr_t align_addr;	/* Mapped bounce buffer */

	struct tasklet_struct card_tasklet;	/* Tasklet structures */
	/*
	中断函数中:
	if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE))
	会调用tasklet_schedule(&host->card_tasklet);
    可以看出是处理card insert和remove,对应的函数是 sdhci_tasklet_card()
	*/
	struct tasklet_struct finish_tasklet; 
    /*
    sdhci_remove_host()函数,如果host->mrq不为NULL。
	就会调用tasklet_schedule(&host->finish_tasklet);
    可以看出是有请求时sd被remove会执行的处理,对应的函数sdhci_tasklet_finish();
	*/

	struct timer_list timer;	/* Timer for timeouts *///超时定时器

	unsigned int caps;	/* Alternative capabilities *///另类功能

	unsigned int            ocr_avail_sdio;	/* OCR bit masks */ //ocr 寄存器记录着支持的电压
	unsigned int            ocr_avail_sd;
	unsigned int            ocr_avail_mmc;

	//CMD19在SD spec中是预留的。在SDHCI v3.00中有个tuning command会用到
	wait_queue_head_t	buf_ready_int;	/* Waitqueue for Buffer Read Ready interrupt */
	/*
	在 sdhci_data_irq()
	// CMD19 generates _only_ Buffer Read Ready interrupt 
    if (intmask & SDHCI_INT_DATA_AVAIL) {
            if (SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND)) ==
                MMC_SEND_TUNING_BLOCK) {
                    host->tuning_done = 1;
                    wake_up(&host->buf_ready_int);
                    return;
            }
    }

    唤醒的函数是sdhci_execute_tuning()
	*/
	unsigned int		tuning_done;	/* Condition flag set when CMD19 succeeds *///CMD19成功标志

	unsigned int		tuning_count;	/* Timer count for re-tuning *///tuning计时
	unsigned int		tuning_mode;	/* Re-tuning mode supported by host *///tuning模式支持
#define SDHCI_TUNING_MODE_1	0
	struct timer_list	tuning_timer;	/* Timer for tuning *///tuning用的定时器

	unsigned long private[0] ____cacheline_aligned;
};
#endif /* LINUX_MMC_SDHCI_H */


你可能感兴趣的:(linux,硬件,ARM,移动设备,linux内核)