SPI驱动程序(S3C2440)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
2410_SPI接口与linux驱动
以下先从下到上的进行分析:
driver/spi下有两个底层相关的spi驱动程序:
spi_s3c24xx.c和spi_s3c24xx_gpio.c
其中spi_s3c24xx.c是基于s3c24xx下相应的spi接口的驱动程序,spi_s3c24xx_gpio.c允许用户指定3个gpio口接口,模拟标准的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;
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;
}
hw = spi_master_get_devdata(master);
memset(hw, 0, sizeof(struct s3c24xx_spi));
hw->master = spi_master_get(master);
hw->pdata = pdev->dev.platform_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);
hw->bitbang.master = hw->master;
hw->bitbang.setup_transfer = s3c24xx_spi_setupxfer;
hw->bitbang.chipselect = s3c24xx_spi_chipsel;
hw->bitbang.txrx_bufs = s3c24xx_spi_txrx;
hw->bitbang.master->setup = s3c24xx_spi_setup;
dev_dbg(hw->dev, "bitbang at %p\n", &hw->bitbang);
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;
}
clk_enable(hw->clk);
writeb(0xff, hw->regs + S3C2410_SPPRE);
writeb(SPPIN_DEFAULT, hw->regs + S3C2410_SPPIN);
writeb(SPCON_DEFAULT, hw->regs + S3C2410_SPCON);
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);
if (!hw->pdata->set_cs) {
s3c2410_gpio_setpin(hw->pdata->pin_cs, 1);
s3c2410_gpio_cfgpin(hw->pdata->pin_cs, S3C2410_GPIO_OUTPUT); }
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->bitbang.shutdown);
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;
}
struct spi_master * __init_or_module
spi_alloc_master(struct device *dev, unsigned size) {
struct spi_master *master;
if (!dev)
return NULL;
master = kzalloc(size + sizeof *master, SLAB_KERNEL); if (!master)
return NULL;
class_device_initialize(&master->cdev);
master->cdev.class = &spi_master_class;
master->cdev.dev = get_device(dev);
spi_master_set_devdata(master, &master[1]);
return master;
}
int spi_bitbang_start(struct spi_bitbang *bitbang)
{
int status;
if (!bitbang->master || !bitbang->chipselect)
return -EINVAL;
INIT_WORK(&bitbang->work, bitbang_work, bitbang);
spin_lock_init(&bitbang->lock);
spi_new_device INIT_LIST_HEAD(&bitbang->queue);
if (!bitbang->master->transfer)
bitbang->master->transfer = spi_bitbang_transfer;//spi数据的传输就是通过调用这个方法来实现的
if (!bitbang->txrx_bufs) {
bitbang->use_dma = 0;
bitbang->txrx_bufs = spi_bitbang_bufs;
if (!bitbang->master->setup) {
if (!bitbang->setup_transfer)
bitbang->setup_transfer =
spi_bitbang_setup_transfer;
bitbang->master->setup = spi_bitbang_setup;
bitbang->master->cleanup = spi_bitbang_cleanup;
}
} else if (!bitbang->master->setup)
return -EINVAL;
bitbang->busy = 0;
bitbang->workqueue = create_singlethread_workqueue(
bitbang->master->cdev.dev->bus_id);
if (bitbang->workqueue == NULL) {
status = -EBUSY;
goto err1;
}
status = spi_register_master(bitbang->master);
if (status < 0)
goto err2;
return status;
err2:
destroy_workqueue(bitbang->workqueue);
err1:
return status;
}
int __init_or_module
spi_register_master(struct spi_master *master)
{
static atomic_t dyn_bus_id = ATOMIC_INIT((1<<16) - 1);
struct device *dev = master->cdev.dev;
int status = -ENODEV;
int dynamic = 0;
if (!dev)
return -ENODEV;
if (master->bus_num < 0) {
master->bus_num = atomic_dec_return(&dyn_bus_id); dynamic = 1;
}
snprintf(master->cdev.class_id, sizeof master->cdev.class_id, "spi%u", master->bus_num);
status = class_device_add(&master->cdev);//注册设备
if (status < 0)
goto done;
dev_dbg(dev, "registered master %s%s\n", master->cdev.class_id, dynamic ? " (dynamic)" : "");
scan_boardinfo(master);
status = 0;
done:
return status;
}
static void __init_or_module
scan_boardinfo(struct spi_master *master)
{
struct boardinfo *bi;
struct device *dev = master->cdev.dev;
down(&board_lock);
list_for_each_entry(bi, &board_list, list) {
struct spi_board_info *chip = bi->board_info;
unsigned n;
for (n = bi->n_board_info; n > 0; n--, chip++) {
if (chip->bus_num != master->bus_num)
continue;
if (chip->chip_select >= master->num_chipselect
&& master->num_chipselect) {
dev_dbg(dev, "cs%d > max %d\n",
chip->chip_select,
master->num_chipselect);
continue;
}
(void) spi_new_device(master, chip);
}
}
up(&board_lock);
}
int __init
spi_register_board_info(struct spi_board_info const *info, unsigned n)
struct boardinfo *bi;
bi = kmalloc(sizeof(*bi) + n * sizeof *info, GFP_KERNEL);
if (!bi)
return -ENOMEM;
bi->n_board_info = n;
memcpy(bi->board_info, info, n * sizeof *info);
down(&board_lock);
list_add_tail(&bi->list, &board_list);
up(&board_lock);
return 0;
}
struct spi_device *__init_or_module
spi_new_device(struct spi_master *master, struct spi_board_info *chip)
{
struct spi_device *proxy;//这个结构很重要,以后就是通过这个结构来操作实际的spi设备的struct device *dev = master->cdev.dev;
int status;
if (!spi_master_get(master))
return NULL;
proxy = kzalloc(sizeof *proxy, GFP_KERNEL);
if (!proxy) {
dev_err(dev, "can't alloc dev for cs%d\n",
chip->chip_select);
goto fail;
}
proxy->master = master;
proxy->chip_select = chip->chip_select;
proxy->max_speed_hz = chip->max_speed_hz;
proxy->mode = chip->mode;
proxy->irq = chip->irq;
proxy->modalias = chip->modalias;
snprintf(proxy->dev.bus_id, sizeof proxy->dev.bus_id,
"%s.%u", master->cdev.class_id,
chip->chip_select);
proxy->dev.parent = dev;
proxy->dev.bus = &spi_bus_type;
proxy->dev.platform_data = (void *) chip->platform_data; proxy->controller_data = chip->controller_data;
proxy->controller_state = NULL;
proxy->dev.release = spidev_release;
status = master->setup(proxy);
if (status < 0) {
dev_dbg(dev, "can't %s %s, status %d\n",
"setup", proxy->dev.bus_id, status);
goto fail;
}
status = device_register(&proxy->dev);//真正注册原始设备if (status < 0) {
dev_dbg(dev, "can't %s %s, status %d\n",
"add", proxy->dev.bus_id, status);
goto fail;
}
dev_dbg(dev, "registered child %s\n", proxy->dev.bus_id); return proxy;
fail:
spi_master_put(master);
kfree(proxy);
return NULL;
}
static int s3c24xx_spi_setup(struct spi_device *spi)
{
int ret;
if (!spi->bits_per_word)
spi->bits_per_word = 8;
if ((spi->mode & SPI_LSB_FIRST) != 0)
return -EINVAL;
ret = s3c24xx_spi_setupxfer(spi, NULL);
if (ret < 0) {
dev_err(&spi->dev, "setupxfer returned %d\n", ret);
return ret;
}
dev_dbg(&spi->dev, "%s: mode %d, %u bpw, %d hz\n",
__FUNCTION__, spi->mode, spi->bits_per_word,
spi->max_speed_hz);
return 0;
}
static int s3c24xx_spi_setupxfer(struct spi_device *spi,
struct spi_transfer *t)
{
struct s3c24xx_spi *hw = to_hw(spi);
unsigned int bpw;
unsigned int hz;
unsigned int div;
bpw = t ? t->bits_per_word : spi->bits_per_word;
hz = t ? t->speed_hz : spi->max_speed_hz;
if (bpw != 8) {
dev_err(&spi->dev, "invalid bits-per-word (%d)\n", bpw);
return -EINVAL;
}
div = clk_get_rate(hw->clk) / hz;
div = (div / 2) - 1;//求出预分频值
if (div < 0)
div = 1;
if (div > 255)
div = 255;
dev_dbg(&spi->dev, "setting pre-scaler to %d (hz %d)\n", div, hz);
writeb(div, hw->regs + S3C2410_SPPRE);//设置预分频值
spin_lock(&hw->bitbang.lock);
if (!hw->bitbang.busy) {
hw->bitbang.chipselect(spi, BITBANG_CS_INACTIVE);//修改时钟,先禁用spi
}
spin_unlock(&hw->bitbang.lock);
return 0;
}
static void s3c24xx_spi_chipsel(struct spi_device *spi, int value)
{
struct s3c24xx_spi *hw = to_hw(spi);
unsigned int cspol = spi->mode & SPI_CS_HIGH ? 1 : 0;
unsigned int spcon;
switch (value) {
case BITBANG_CS_INACTIVE:
if (hw->pdata->set_cs)
hw->pdata->set_cs(hw->pdata, value, cspol);
else
s3c2410_gpio_setpin(hw->pdata->pin_cs, cspol ^ 1);
break;
case BITBANG_CS_ACTIVE:
spcon = readb(hw->regs + S3C2410_SPCON);
if (spi->mode & SPI_CPHA)
spcon |= S3C2410_SPCON_CPHA_FMTB;
else
spcon &= ~S3C2410_SPCON_CPHA_FMTB;
if (spi->mode & SPI_CPOL)
spcon |= S3C2410_SPCON_CPOL_HIGH;
else
spcon &= ~S3C2410_SPCON_CPOL_HIGH;
spcon |= S3C2410_SPCON_ENSCK;
writeb(spcon, hw->regs + S3C2410_SPCON);
if (hw->pdata->set_cs)
hw->pdata->set_cs(hw->pdata, value, cspol);
else
s3c2410_gpio_setpin(hw->pdata->pin_cs, cspol);
break;
}
}
好了,至此spi主控制器(驱动)和板上spi设备注册完毕,以后要使用spi来传输数据的话,只要先获得spi设备交道了(就好像你要操作一个文件,先要获取文件句柄一样,明白吧^_^)
原文地址/luofuchong/archive/2007/09/24/33942.html
~~~~~~~~~~~~~~~~~~``
任何SPI都有4种模式
2008-11-06 22:17
void McBSPObj::McBSP0Init(void)//McBSP从设备SPI硬件配置
{
//PCR设置过程(FSM=0,CLKM=0)
McBSP0->SPSA = PCR;
McBSP1->SPSD = (0 << PCR_XIOEN) //发送非通用I/O模式位
| (0 << PCR_RIOEN) //接收非通用I/O模式位
| (0 << PCR_FSXM) //外部发送帧同步脉冲(外部片选)
| (0 << PCR_FSRM) //外部接收帧同步脉冲(外部片选)
| (0 << PCR_CLKXM) //外部发送时钟(外部时钟源)
| (0 << PCR_CLKRM) //外部接收时钟(外部时钟源)
#if SPIMODE == 0
//SPI设置过程00(0--FS高电平有效,0--CLK上升沿收发数据)
| (0 << PCR_FSXP) //发送帧同步脉冲极性(高电平有效)
| (0 << PCR_FSRP) //接收帧同步脉冲极性(高电平有效)
| (0 << PCR_CLKXP) //发送时钟极性(上升沿发送数据)
| (0 << PCR_CLKRP);//接收时钟极性(上升沿接收数据)
#endif
#if SPIMODE == 1
//SPI设置过程01(0--FS高电平有效,1--CLK下降沿收发数据)
| (0 << PCR_FSXP) //发送帧同步脉冲极性(高电平有效)
| (0 << PCR_FSRP) //接收帧同步脉冲极性(高电平有效)
| (1 << PCR_CLKXP) //发送时钟极性(下降沿发送数据)
| (1 << PCR_CLKRP);//接收时钟极性(下降沿接收数据)
#endif
#if SPIMODE == 2
//SPI设置过程10(1--FS低电平有效,0--CLK上升沿收发数据)
| (1 << PCR_FSXP) //发送帧同步脉冲极性(低电平有效)
| (1 << PCR_FSRP) //接收帧同步脉冲极性(低电平有效)
| (0 << PCR_CLKXP) //发送时钟极性(上升沿发送数据)
| (0 << PCR_CLKRP);//接收时钟极性(上升沿接收数据)
#endif
#if SPIMODE == 3
//SPI设置过程11(1--FS低电平有效,1--CLK下降沿收发数据)
| (1 << PCR_FSXP) //发送帧同步脉冲极性(低电平有效)
| (1 << PCR_FSRP) //接收帧同步脉冲极性(低电平有效)
| (1 << PCR_CLKXP) //发送时钟极性(下降沿发送数据)
| (1 << PCR_CLKRP);//接收时钟极性(下降沿接收数据)
#endif
//MCR1设置过程(RMCM=0)
McBSP0->SPSA = MCR1;//
McBSP0->SPSD = (0 << MCR1_RMCM) //允许接收多通道选择(0)
| (0x00 << MCR1_RPBBLK)
| (0x00 << MCR1_RPABLK)
| (0x00 << MCR1_RCBLK);
//MCR2设置过程(XMCM=0)
McBSP0->SPSA = MCR2;//
McBSP0->SPSD = (0x00 << MCR2_XMCM) //允许发送多通道选择(00b) | (0x00 << MCR2_XPBBLK)
| (0x00 << MCR2_XPABLK)
| (0x00 << MCR2_XCBLK);
//RCERA设置过程
McBSP0->SPSA = RCERA;//
McBSP0->SPSD = 0;
//RCERB设置过程
McBSP0->SPSA = RCERB;//
McBSP0->SPSD = 0;
//XCERA设置过程
McBSP0->SPSA = XCERA;//
McBSP0->SPSD = 0;
//XCERB设置过程
McBSP0->SPSA = XCERB;//
McBSP0->SPSD = 0;
//XCR1设置过程
McBSP0->SPSA = XCR1;//
McBSP0->SPSD = (0x00 << XCR1_XFRLEN1) //每帧1个字(每帧中断的次数1!!!) // | (0x02 << XCR1_XWDLEN1);//每字16位长(每次中断的字节数2!!!)
| (0x00 << XCR1_XWDLEN1);//每字8位长(每次中断的字节数2!!!)
//XCR2设置过程
McBSP0->SPSA = XCR2;
McBSP0->SPSD = (0 << XCR2_XPHASE) //单相帧(其他设置都为0)
| (0x00 << XCR2_XCOMPAND)//发送数据从最高位(MSB)开始
| (0x00 << XCR2_XDATDLY);//同步后延迟0位数据
//RCR1设置过程
McBSP0->SPSA = RCR1;
McBSP0->SPSD = (0x00 << RCR1_RFRLEN1) //每帧1个字(每帧中断的次数1!!!) // | (0x02 << RCR1_RWDLEN1);//每字16位长(每次中断的字节数2!!!)
| (0x00 << RCR1_RWDLEN1);//每字8位长(每次中断的字节数2!!!)
//RCR2设置过程
McBSP0->SPSA = RCR2;
McBSP0->SPSD = (0 << RCR2_RPHASE) //单相帧(其他设置都为0)
| (0x00 << RCR2_RCOMPAND)//接收数据从最高位(MSB)开始
| (0x00 << RCR2_RDATDLY);//同步后延迟0位数据
//SRGR1设置过程
McBSP0->SPSA = SRGR1;
McBSP0->SPSD = (0x00 << SRGR1_CLKGDV);//1
//SRGR2设置过程
McBSP0->SPSA = SRGR2;
McBSP0->SPSD = (0 << SRGR2_FSGM)
| (1 << SRGR2_CLKSM)//由CPU时钟产生的采样率时钟1
| (0 << SRGR2_CLKSP)//0
| (1 << SRGR2_GSYNC)//
| (0x0f << SRGR2_FPER);//0x0f
//SPCR1设置过程(CLKSTP=1Xb,RINTM=00b)
McBSP0->SPSA = SPCR1;
McBSP0->SPSD = (0x00 << SPCR1_RINTM) //接收中断模式00(每帧接收1次中? | (0 << SPCR1_DLB) //禁止回送
| (1 << SPCR1_DXENA) //DX使能
| (0x00 << SPCR1_RJUST) //接收符号不扩展
| (0x02 << SPCR1_CLKSTP);//SPI模式时钟开始于上升沿(无延迟)
//SPCR2设置过程(XINTM=02b)
McBSP0->SPSA = SPCR2;
McBSP0->SPSD = (0x02 << SPCR2_XINTM)//发送中断模式02
| (1 << SPCR2_XEMPTY) //发送移位寄存器空
| (1 << SPCR2_XRDY); //发送准备好
//SPCR1复位过程
McBSP0->SPSA = SPCR1;
McBSP0->SPSD|= (1 << SPCR1_RRST);//接收器复位
//SPCR2复位过程
McBSP0->SPSA = SPCR2;
McBSP0->SPSD|= (1 << SPCR2_XRST)//发送器复位
| (1 << SPCR2_GRST)//采样率发生器复位
| (1 << SPCR2_FRST);//帧同步发生器复位
//清除允许BXINT0中断过程
// SREG->IFR = (1 << IFR_BXINT0);//清除BXINT0中断标志// SREG->IMR |= (1 << IMR_BXINT0);//允许BXINT0中断//清除允许BRINT0中断过程
SREG->IFR = (1 << IFR_BRINT0);//清除BRINT0中断标志SREG->IMR |= (1 << IMR_BRINT0);//允许BRINT0中断
}
void McBSPObj::McBSP1Init(void)//GPIO配置
{
McBSP1->SPSA = SPCR1;
McBSP1->SPSD = 0;
McBSP1->SPSA = SPCR2;
McBSP1->SPSD = 0;
McBSP1->SPSA = PCR;
//设置收发。