石膏线模具厂家
免费服务热线

Free service

hotline

010-00000000
石膏线模具厂家
热门搜索:
技术资讯
当前位置:首页 > 技术资讯

Linuxspi子操作系统驱动分析

发布时间:2021-01-22 07:10:49 阅读: 来源:石膏线模具厂家

2.6.18内核下已经添加了完整的spi子系统了,参考mtd的分析,将从下到上层,再从上到下层的对其进行分析。

以下先从下到上的进行分析:

driver/spi下有两个底层相关的spi驱动程序:

spi_s3c24xx.c和spi_s3c24xx_gpio.c

其中spi_s3c24xx.c是基于s3c24xx下相应的spi接口的驱动程序,spi_s3c24xx_gpio.c允许用户指定3个gpio口,分别充当spi_clk、spi_mosi和spi_miso接口,模拟标准的spi总线。

s3c2410自带了两个spi接口(spi0和spi1),在此我只研究基于s3c2410下spi接口的驱动程序spi_s3c24xx.c。

首先从spi驱动的检测函数进行分析:

static int s3c24xx_spi_probe(struct platform_device *pdev)

{

struct s3c24xx_spi *hw;

struct spi_master *master;

struct spi_board_info *bi;

struct resource *res;

int err = 0;

int i;

/* pi_alloc_master函数申请了struct spi_master+struct s3c24xx_spi大小的数据,

* spi_master_get_devdata和pi_master_get分别取出struct s3c24xx_spi和struct spi_master结构指针

*/

master = spi_alloc_master(&pdev->dev, sizeof(struct s3c24xx_spi));

if (master == NULL) {

dev_err(&pdev->dev, "No memory for spi_master\n");

err = -ENOMEM;

goto err_nomem;

}

/* 填充struct spi_master结构 */

hw = spi_master_get_devdata(master);

memset(hw, 0, sizeof(struct s3c24xx_spi));

hw->master = spi_master_get(master);

hw->pdata = pdev->atform_data;

hw->dev = &pdev->dev;

if (hw->pdata == NULL) {

dev_err(&pdev->dev, "No platform data supplied\n");

err = -ENOENT;

goto err_no_pdata;

}

platform_set_drvdata(pdev, hw);//dev_set_drvdata(&pdev->dev, hw)

init_completion(&hw->done);

/* setup the state for the bitbang driver */

/* 填充hw->bitbang结构(hw->bitbang结构充当一个中间层,相当与input system的input_handle struct) */

hw->ster = hw->master;

hw->tup_transfer = s3c24xx_spi_setupxfer;

hw->ipselect = s3c24xx_spi_chipsel;

hw->rx_bufs = s3c24xx_spi_txrx;

hw->ster->setup = s3c24xx_spi_setup;

dev_dbg(hw->dev, "bitbang at %p\n", &hw->bitbang);

/* find and map our resources */

/* 申请spi所用到的资源:io、irq、时钟等 */

res = platform_get_resource(pdev, IORESOURCE_MEM, 0);

if (res == NULL) {

dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");

err = -ENOENT;

goto err_no_iores;

}

hw->ioarea = request_mem_region(res->start, (res->end - res->start)+1,

pdev->name);

if (hw->ioarea == NULL) {

dev_err(&pdev->dev, "Cannot reserve region\n");

err = -ENXIO;

goto err_no_iores;

}

hw->regs = ioremap(res->start, (res->end - res->start)+1);

if (hw->regs == NULL) {

dev_err(&pdev->dev, "Cannot map IO\n");

err = -ENXIO;

goto err_no_iomap;

}

hw->irq = platform_get_irq(pdev, 0);

if (hw->irq < 0) {

dev_err(&pdev->dev, "No IRQ specified\n");

err = -ENOENT;

goto err_no_irq;

}

err = request_irq(hw->irq, s3c24xx_spi_irq, 0, pdev->name, hw);

if (err) {

dev_err(&pdev->dev, "Cannot claim IRQ\n");

goto err_no_irq;

}

hw->clk = clk_get(&pdev->dev, "spi");

if (IS_ERR(hw->clk)) {

dev_err(&pdev->dev, "No clock for device\n");

err = PTR_ERR(hw->clk);

goto err_no_clk;

}

/* for the moment, permanently enable the clock */

clk_enable(hw->clk);

/* program defaults into the registers */

/* 初始化spi相关的寄存器 */

writeb(0xff, hw->regs + S3C2410_SPPRE);

writeb(SPPIN_DEFAULT, hw->regs + S3C2410_SPPIN);

writeb(SPCON_DEFAULT, hw->regs + S3C2410_SPCON);

/* add by lfc */

s3c2410_gpio_setpin(S3C2410_GPE13, 0);

s3c2410_gpio_setpin(S3C2410_GPE12, 0);

s3c2410_gpio_cfgpin(S3C2410_GPE13, S3C2410_GPE13_SPICLK0);

s3c2410_gpio_cfgpin(S3C2410_GPE12, S3C2410_GPE12_SPIMOSI0);

s3c2410_gpio_cfgpin(S3C2410_GPE11, S3C2410_GPE11_SPIMISO0);

/* end add */

/* setup any gpio we can */

/* 片选 */

if (!hw->pdata->set_cs) {

s3c2410_gpio_setpin(hw->pdata->pin_cs, 1);

s3c2410_gpio_cfgpin(hw->pdata->pin_cs, S3C2410_GPIO_OUTPUT);

}

/* register our spi controller */

/* 最终通过调用spi_register_master来注册spi控制器(驱动) */

err = spi_bitbang_start(&hw->bitbang);

if (err) {

dev_err(&pdev->dev, "Failed to register SPI master\n");

goto err_register;

}

dev_dbg(hw->dev, "shutdown=%d\n", hw->utdown);

/* register all the devices associated */

/* 注册所用使用本spi驱动的设备 */

bi = &hw->pdata->board_info[0];

for (i = 0; i < hw->pdata->board_size; i++, bi++) {

dev_info(hw->dev, "registering %s\n", bi->modalias);

bi->controller_data = hw;

spi_new_device(master, bi);

}

return 0;

err_register:

clk_disable(hw->clk);

clk_put(hw->clk);

err_no_clk:

free_irq(hw->irq, hw);

err_no_irq:

iounmap(hw->regs);

err_no_iomap:

release_resource(hw->ioarea);

kfree(hw->ioarea);

err_no_iores:

err_no_pdata:

spi_master_put(hw->master);;

err_nomem:

return err;

}

盖世大侠破解版

天天魏蜀吴游戏

贪婪洞窟有神器单机内购破解版1.8

龙腾传世