主题 : SPI设备驱动移植求救 复制链接 | 浏览器收藏 | 打印
级别: 骑士
UID: 4234
精华: 0
发帖: 152
金钱: 1420 两
威望: 1142 点
综合积分: 304 分
注册时间: 2009-03-05
最后登录: 2012-08-18
楼主  发表于: 2009-11-02 16:39

 SPI设备驱动移植求救

(我昨天搞错了,目前状态是对的,已经对spi2进行了注册,我又在这个基础上进行了进一步的修改,目的是为了实现2个spi设备的连接,修改如下:)
主要是对arch/arm/mach-s3c2410/mach-smdk2410.c文件进行一定的修改,部分代码如下(我认为是必须修改的部分,这里我也参照了本站中的SPI驱动的移植(Linux2.6.24)的文章):
static struct platform_device *smdk2410_devices[] __initdata = {
    &s3c_device_usb,
    &s3c_device_lcd,
    &s3c_device_wdt,
    &s3c_device_i2c,
    &s3c_device_iis,
    
    &s3c_device_spi0,
    &s3c_device_spi1,

};
.......
static struct spi_board_info spi0[]={

    [0]={
                .modalias ="mcp2515",//设备驱动文件名
                .irq=IRQ_EINT9,
                .max_speed_hz=1*1000*1000,
        .bus_num=0,
                .chip_select=0,

    },
[0]={
.modalias ="mcp2515",
.irq=IRQ_EINT16,
.max_speed_hz=1*1000*1000,
.bus_num=1,
.chip_select=0,

},

};
..........
static struct s3c2410_spi_info s3c_spi0_info={
.pin_cs =S3C2410_GPG2,
.board_size=ARRAY_SIZE(spi0),
.board_info=spi0,
};
static struct s3c2410_spi_info s3c_spi1_info={
.pin_cs =S3C2410_GPG3,
.board_size=ARRAY_SIZE(spi0),//这里我感觉是有问题,但又不知道怎么改,请朋友们帮忙看看,我感觉这里的board_size应该是sip0数组一个成员的大小,但是我用sizeof(spi0[0])还是不行,
.board_info=spi0,
};


static void __init smdk2410_init(void)
{
    ..............................
    s3c_device_spi0.dev.platform_data= &s3c_spi0_info;
                   s3c_device_spi1.dev.platform_data= &s3c_spi1_info;


    platform_add_devices(smdk2410_devices, ARRAY_SIZE(smdk2410_devices));
    smdk_machine_init();
}
出现的问题是可以编译通过,并可以在文件系统中通过ls -al命令在/sys/bus/platform/devices目录下得到如下信息:

drwxr-xr-x 2 0 0 0 Jan 1 00:01 .
drwxr-xr-x 4 0 0 0 Jan 1 00:01 ..
lrwxrwxrwx 1 0 0 0 Jan 1 00:01 dm9000.0 -> ../../../dev
ices/platform/dm9000.0
lrwxrwxrwx 1 0 0 0 Jan 1 00:01 s3c2410-i2c -> ../../../
devices/platform/s3c2410-i2c
lrwxrwxrwx 1 0 0 0 Jan 1 00:01 s3c2410-iis -> ../../../
devices/platform/s3c2410-iis
lrwxrwxrwx 1 0 0 0 Jan 1 00:01 s3c2410-lcd -> ../../../
devices/platform/s3c2410-lcd
lrwxrwxrwx 1 0 0 0 Jan 1 00:01 s3c2410-nand -> ../../..
/devices/platform/s3c2410-nand
lrwxrwxrwx 1 0 0 0 Jan 1 00:01 s3c2410-ohci -> ../../..
/devices/platform/s3c2410-ohci
lrwxrwxrwx 1 0 0 0 Jan 1 00:01 s3c2410-spi.0 -> ../../.
./devices/platform/s3c2410-spi.0
lrwxrwxrwx 1 0 0 0 Jan 1 00:01 s3c2410-spi.1 -> ../../.
./devices/platform/s3c2410-spi.1
lrwxrwxrwx 1 0 0 0 Jan 1 00:01 s3c2410-uart.0 -> ../../

../devices/platform/s3c2410-uart.0
lrwxrwxrwx 1 0 0 0 Jan 1 00:01 s3c2410-uart.1 -> ../../
../devices/platform/s3c2410-uart.1
lrwxrwxrwx 1 0 0 0 Jan 1 00:01 s3c2410-uart.2 -> ../../
../devices/platform/s3c2410-uart.2
lrwxrwxrwx 1 0 0 0 Jan 1 00:01 s3c2410-wdt -> ../../../
devices/platform/s3c2410-wdt
lrwxrwxrwx 1 0 0 0 Jan 1 00:01 s3c24xx_led.0 -> ../../.
./devices/platform/s3c24xx_led.0
lrwxrwxrwx 1 0 0 0 Jan 1 00:01 s3c24xx_led.1 -> ../../.
./devices/platform/s3c24xx_led.1
lrwxrwxrwx 1 0 0 0 Jan 1 00:01 s3c24xx_led.2 -> ../../.
./devices/platform/s3c24xx_led.2
lrwxrwxrwx 1 0 0 0 Jan 1 00:01 s3c24xx_led.3 -> ../../.
./devices/platform/s3c24xx_led.3
lrwxrwxrwx 1 0 0 0 Jan 1 00:01 serial8250 -> ../../../d
evices/platform/serial8250
lrwxrwxrwx 1 0 0 0 Jan 1 00:01 zlg7290 -> ../../../devi
ces/platform/zlg7290
但启动后出现如下错误信息:
s3c2410-spi s3c2410-spi.0: registering mcp2515
kobject_add failed for spi0 with -EEXIST, don't try to register things with the
same name in the same directory.
[<c002ddac>] (dump_stack+0x0/0x14) from [<c0192c60>] (kobject_shadow_add+0x190/0
x1ac)
[<c0192ad0>] (kobject_shadow_add+0x0/0x1ac) from [<c0192c90>] (kobject_add+0x14/
0x18)
r7:00000000 r6:c037f150 r5:c07e4e00 r4:c07e4ea0
[<c0192c7c>] (kobject_add+0x0/0x18) from [<c01d7624>] (class_device_add+0x84/0x4
1c)
[<c01d75a0>] (class_device_add+0x0/0x41c) from [<c021b140>] (spi_register_master
+0x6c/0x140)
[<c021b0d4>] (spi_register_master+0x0/0x140) from [<c021bc80>] (spi_bitbang_star
t+0x100/0x134)
[<c021bb80>] (spi_bitbang_start+0x0/0x134) from [<c021c27c>] (s3c24xx_spi_probe+
0x2ac/0x3c0)
r5:c07e4ea0 r4:c037f2ac
[<c021bfd0>] (s3c24xx_spi_probe+0x0/0x3c0) from [<c01d83fc>] (platform_drv_probe
+0x20/0x24)
[<c01d83dc>] (platform_drv_probe+0x0/0x24) from [<c01d64bc>] (driver_probe_devic
e+0xe8/0x18c)
[<c01d63d4>] (driver_probe_device+0x0/0x18c) from [<c01d669c>] (__driver_attach+
0x80/0xe0)
r8:00000000 r7:c03a9cf8 r6:c01d661c r5:c037f158 r4:c037f230
[<c01d661c>] (__driver_attach+0x0/0xe0) from [<c01d54bc>] (bus_for_each_dev+0x50
/0x84)
r5:c048deec r4:00000000
[<c01d546c>] (bus_for_each_dev+0x0/0x84) from [<c01d671c>] (driver_attach+0x20/0
x28)
r7:c03a6b28 r6:c03a9d00 r5:c03a9cf8 r4:00000000
[<c01d66fc>] (driver_attach+0x0/0x28) from [<c01d5c8c>] (bus_add_driver+0x7c/0x1
b4)
[<c01d5c10>] (bus_add_driver+0x0/0x1b4) from [<c01d6b98>] (driver_register+0x80/
0x88)
[<c01d6b18>] (driver_register+0x0/0x88) from [<c01d8510>] (platform_driver_regis
ter+0x6c/0x88)
r4:00000000
[<c01d84a4>] (platform_driver_register+0x0/0x88) from [<c001ceb4>] (s3c24xx_spi_
init+0x14/0x1c)
[<c001cea0>] (s3c24xx_spi_init+0x0/0x1c) from [<c0008c14>] (kernel_init+0xd4/0x2
8c)
[<c0008b40>] (kernel_init+0x0/0x28c) from [<c0047a98>] (do_exit+0x0/0x760)
s3c2410-spi s3c2410-spi.1: Failed to register SPI master
s3c2410-spi: probe of s3c2410-spi.1 failed with error -17
这是什么原因,没搞清楚!请朋友们帮忙看看!
[ 此帖被feiyangczm在2009-11-03 11:05重新编辑 ]
级别: 骑士
UID: 4234
精华: 0
发帖: 152
金钱: 1420 两
威望: 1142 点
综合积分: 304 分
注册时间: 2009-03-05
最后登录: 2012-08-18
1楼  发表于: 2009-11-03 11:09
要被淹没了,一方面自己顶一下,令一方面期待回复!
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
2楼  发表于: 2009-11-05 17:18
s3c2410-spi s3c2410-spi.0: registering mcp2515
kobject_add failed for spi0 with -EEXIST, don't try to register things with the
same name in the same directory.

不是已经说的很清楚了?
"If you have an apple and I have an apple and we exchange apples, then you and I will
still each have one apple. But if you have an idea and I have an idea and we exchange
these ideas, then each of us will have two ideas."
级别: 骑士
UID: 4234
精华: 0
发帖: 152
金钱: 1420 两
威望: 1142 点
综合积分: 304 分
注册时间: 2009-03-05
最后登录: 2012-08-18
3楼  发表于: 2009-11-06 09:27
我今天发现了这个问题,我发现即使是单独使用spi1,但是在sys/bus/spi/device目录下也只有一个spi0.0文件,就是说在注册地时候spi0注册了一个spi0,但是spi1又去注册,从而造成sp1注册失败,是不是这个意思?该如何解决?理不顺思路了!
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
4楼  发表于: 2009-11-06 10:45
static struct spi_board_info spi0[]={

    [0]={
                .modalias ="mcp2515",//设备驱动文件名
                .irq=IRQ_EINT9,
                .max_speed_hz=1*1000*1000,
        .bus_num=0,
                .chip_select=0,

    },
[0]={
.modalias ="mcp2515",
.irq=IRQ_EINT16,
.max_speed_hz=1*1000*1000,
.bus_num=1,
.chip_select=0,

},

你为什么要在同一个SPI Host上注册两个相同的设备?
"If you have an apple and I have an apple and we exchange apples, then you and I will
still each have one apple. But if you have an idea and I have an idea and we exchange
these ideas, then each of us will have two ideas."
级别: 骑士
UID: 4234
精华: 0
发帖: 152
金钱: 1420 两
威望: 1142 点
综合积分: 304 分
注册时间: 2009-03-05
最后登录: 2012-08-18
5楼  发表于: 2009-11-06 16:11
我是看到http://www.aiothome.com/bbs/read.php?tid=2378&page=1#12083中说
static struct spi_board_info s3c2410_spi0_board[] = {

        [0] = {

                .modalias         = "spidev",    /*跟spidev.ko驱动名要相同,不然挂载不上*/

                .platform_data         = NULL,

                .bus_num        = 0,    /*使用的第一条BUS线,2410支持0,1两根*/

                .chip_select        = 1, /*片选信号,和上面结合,在/dev 里面可以看到spidev0.1*/

                .max_speed_hz         = 500*1000, /CLK频率*/

        },

};

而我的2个SPI设备都是用的mcp2515,我打算用这一个驱动文件,我不知道我这样做是否合适。
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
6楼  发表于: 2009-11-06 17:54
把你的硬件原理图附上来,不同的设备连接配置是不一样的。
"If you have an apple and I have an apple and we exchange apples, then you and I will
still each have one apple. But if you have an idea and I have an idea and we exchange
these ideas, then each of us will have two ideas."
级别: 骑士
UID: 4234
精华: 0
发帖: 152
金钱: 1420 两
威望: 1142 点
综合积分: 304 分
注册时间: 2009-03-05
最后登录: 2012-08-18
7楼  发表于: 2009-11-09 08:56
电路很简单,只是2515的spi口对应的口与2410的spi口对应相连,spi0和spi1分别连接一个2515。
另外这两天我也在分析这个代码,发现了1个地方似乎是我这个问题的关键所在
我通过打印调试发现故障提示信息是在int spi_register_master(struct spi_master *master)(drivers/spi.c)函数运行的时候出现的,而该函数中我发现函数中的master->bus_num始终为0,打印信息如下:
s3c24xx_spi_probe:master->bus_num=0
s3c24xx_spi_probe:before spi_bitbang_start
spi_register_master:bus_num=0
spi_register_master:before class_device_add
spi_register_master:class_device_add return 0
s3c2410-spi s3c2410-spi.0: registering mcp2515//spi0注册完毕,并与mcp2515文件建立起链接
s3c24xx_spi_probe:master->bus_num=0//这里我理解应该为1了,可是还是为0
s3c24xx_spi_probe:before spi_bitbang_start
spi_register_master:bus_num=0
spi_register_master:before class_device_add
kobject_add failed for spi0 with -EEXIST, don't try to register things with the
same name in the same directory.

而在struct spi_master 的定义中又由这样的注释:
           /* other than negative (== assign one dynamically), bus_num is fully
     * board-specific.  usually that simplifies to being SOC-specific.
     * example:  one SOC has three SPI controllers, numbered 0..2,
     * and one board's schematics might show it using SPI-2.  software
     * would normally use bus_num=2 for that controller.
     */
所以我认为在分别注册spi0和spi1的时候,这里的master->bus_num分别应该是0和1
追溯回去,我认为问题是在spi_s3c24xx.c文件中的static int s3c24xx_spi_probe(struct platform_device *pdev)函数中没有将
mach-smdk2410.c文件中spi_board_info结构体中的bus_num传递给hw->bitbang中去,从而造成注册时产生的文件只有spi0.0,
于是我在static int s3c24xx_spi_probe加入了这样的代码:
//add
    s3c2410_spix_info=pdev->dev.platform_data;
    spix_board_info=s3c2410_spix_info->board_info;
    (hw->master)->bus_num=spix_board_info->bus_num;

//end
但是又了新的问题,下面是编译下载启动后的串口部分信息(只和spi相关的以及我加入的打印行)
s3c24xx_spi_probe:master->bus_num=0
s3c24xx_spi_probe:before spi_bitbang_start
spi_register_master:bus_num=0
spi_register_master:before class_device_add
spi_register_master:class_device_add return 0
s3c2410-spi s3c2410-spi.0: registering mcp2515//到这里spi0已经注册完毕,并和mcp2515建立了链接
s3c24xx_spi_probe:master->bus_num=0
s3c24xx_spi_probe:before spi_bitbang_start
spi_register_master:bus_num=1
spi_register_master:before class_device_add
spi_register_master:class_device_add return 0
s3c2410-spi s3c2410-spi.1: registering mcp2515]//到这里spi1已经注册完毕,并和mcp2515建立了链接
usbmon: debugfs is not available
s3c2410-ohci s3c2410-ohci: S3C24XX OHCI
s3c2410-ohci s3c2410-ohci: new USB bus registered, assigned bus number 1
s3c2410-ohci s3c2410-ohci: irq 42, io mem 0x49000000
usb usb1: configuration #1 chosen from 1 choice
hub 1-0:1.0: USB hub found
hub 1-0:1.0: 2 ports detected
Initializing USB Mass Storage driver...
usb 1-1: new full speed USB device using s3c2410-ohci and address 2
usb 1-1: configuration #1 chosen from 1 choice
scsi0 : SCSI emulation for USB Mass Storage devices
usbcore: registered new interface driver usb-storage
USB Mass Storage support registered.
串口信息到这里就没有动静了,如果去除掉我加入代码中的(hw->master)->bus_num=spix_board_info->bus_num;
就可以完全运行完,spi和USB还有冲突吗?现在我是彻底无语了。。。
[ 此帖被feiyangczm在2009-11-09 09:30重新编辑 ]
级别: 侠客
UID: 8904
精华: 0
发帖: 54
金钱: 400 两
威望: 155 点
综合积分: 108 分
注册时间: 2009-09-10
最后登录: 2017-09-13
8楼  发表于: 2009-11-10 15:24

 回 7楼(feiyangczm) 的帖子

请问你的内核是多少?我在2.6.29上移植总是不成功,spi_board_info无处调用,而且在s3c2410_spi_info中设置的.pin_cs不被系统接受,出现得不到片选的错误。大家帮帮忙。谢谢
级别: 新手上路
UID: 18316
精华: 0
发帖: 1
金钱: 5 两
威望: 1 点
综合积分: 2 分
注册时间: 2010-04-08
最后登录: 2017-09-13
9楼  发表于: 2010-04-09 16:29
我也在做,不知道怎么弄啊,有谁弄成功了,告诉我啊,还有spi bus 0的接口在哪里啊