塊裝置驅動2--sd卡驅動1(linux-3.2.36的sdchi.h分析)__linux

來源:互聯網
上載者:User

我的經曆:

看了某些大神的說明,我膽怯地進入了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];#endifspinlock_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.0unsigned 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的blocksint 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_tbuf_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 inttuning_done;/* Condition flag set when CMD19 succeeds *///CMD19成功標誌unsigned inttuning_count;/* Timer count for re-tuning *///tuning計時unsigned inttuning_mode;/* Re-tuning mode supported by host *///tuning模式支援#define SDHCI_TUNING_MODE_10struct timer_listtuning_timer;/* Timer for tuning *///tuning用的定時器unsigned long private[0] ____cacheline_aligned;};#endif /* LINUX_MMC_SDHCI_H */


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.