你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux下SPI驱动开发

[日期:2011-09-01] 来源:Linux社区  作者:hongtao_liu [字体:大 中 小]

四、m25p10驱动测试

    目标:在华清远见的FS_S5PC100平台上编写一个简单的spi驱动模块,在probe阶段实现对m25p10的ID号探测、flash擦除、flash状态读取、flash写入、flash读取等操作。代码已经经过测试,运行于2.6.35内核。理解下面代码需要参照m25p10的芯片手册。其实下面的代码和处理器没有太大关系,这也是spi子系统的分层特点。

  1. #include <linux/platform_device.h>   
  2. #include <linux/spi/spi.h>   
  3. #include <linux/init.h>  
  4. #include <linux/module.h>  
  5. #include <linux/device.h>  
  6. #include <linux/interrupt.h>  
  7. #include <linux/mutex.h>  
  8. #include <linux/slab.h>   // kzalloc  
  9. #include <linux/delay.h>  
  10.   
  11. #define FLASH_PAGE_SIZE     256  
  12.   
  13. /* Flash Operating Commands */  
  14. #define CMD_READ_ID         0x9f  
  15. #define CMD_WRITE_ENABLE    0x06      
  16. #define CMD_BULK_ERASE      0xc7  
  17. #define CMD_READ_BYTES      0x03  
  18. #define CMD_PAGE_PROGRAM    0x02  
  19. #define CMD_RDSR            0x05      
  20.   
  21. /* Status Register bits. */  
  22. #define SR_WIP          1   /* Write in progress */  
  23. #define SR_WEL          2   /* Write enable latch */  
  24.   
  25. /* ID Numbers */  
  26. #define MANUFACTURER_ID     0x20  
  27. #define DEVICE_ID           0x1120  
  28.   
  29. /* Define max times to check status register before we give up. */  
  30. #define MAX_READY_WAIT_COUNT    100000  
  31. #define CMD_SZ  4  
  32.   
  33. struct m25p10a {  
  34.     struct spi_device   *spi;  
  35.     struct mutex        lock;  
  36.     char    erase_opcode;  
  37.     char    cmd[ CMD_SZ ];  
  38. };  
  39.   
  40. /*  
  41.  * Internal Helper functions   
  42.  */  
  43.   
  44. /*  
  45.  * Read the status register, returning its value in the location  
  46.  * Return the status register value.  
  47.  * Returns negative if error occurred.  
  48.  */  
  49. static int read_sr(struct m25p10a *flash)  
  50. {  
  51.     ssize_t retval;  
  52.     u8 code = CMD_RDSR;  
  53.     u8 val;  
  54.   
  55.     retval = spi_write_then_read(flash->spi, &code, 1, &val, 1);  
  56.   
  57.     if (retval < 0) {  
  58.         dev_err(&flash->spi->dev, "error %d reading SR\n",  
  59.                 (int) retval);  
  60.         return retval;  
  61.     }  
  62.   
  63.     return val;  
  64. }  
  65.   
  66. /*  
  67.  * Service routine to read status register until ready, or timeout occurs.  
  68.  * Returns non-zero if error.  
  69.  */  
  70. static int wait_till_ready(struct m25p10a *flash)  
  71. {  
  72.     int count;  
  73.     int sr;   
  74.   
  75.     /* one chip guarantees max 5 msec wait here after page writes,  
  76.      * but potentially three seconds (!) after page erase.  
  77.      */  
  78.     for (count = 0; count < MAX_READY_WAIT_COUNT; count++) {  
  79.         if ((sr = read_sr(flash)) < 0)  
  80.             break;  
  81.         else if (!(sr & SR_WIP))  
  82.             return 0;  
  83.   
  84.         /* REVISIT sometimes sleeping would be best */  
  85.     }     
  86.     printk( "in (%s): count = %d\n", count );  
  87.   
  88.     return 1;  
  89. }  
  90.   
  91. /*  
  92.  * Set write enable latch with Write Enable command.  
  93.  * Returns negative if error occurred.  
  94.  */  
  95. static inline int write_enable( struct m25p10a *flash )  
  96. {  
  97.     flash->cmd[0] = CMD_WRITE_ENABLE;  
  98.     return spi_write( flash->spi, flash->cmd, 1 );  
  99. }  
  100.   
  101. /*  
  102.  * Erase the whole flash memory  
  103.  *  
  104.  * Returns 0 if successful, non-zero otherwise.  
  105.  */  
  106. static int erase_chip( struct m25p10a *flash )  
  107. {  
  108.     /* Wait until finished previous write command. */  
  109.     if (wait_till_ready(flash))  
  110.         return -1;  
  111.   
  112.     /* Send write enable, then erase commands. */  
  113.     write_enable( flash );  
  114.     flash->cmd[0] = CMD_BULK_ERASE;  
  115.     return spi_write( flash->spi, flash->cmd, 1 );  
  116. }  
  117.   
  118. /*  
  119.  * Read an address range from the flash chip.  The address range  
  120.  * may be any size provided it is within the physical boundaries.  
  121.  */  
  122. static int m25p10a_read( struct m25p10a *flash, loff_t from,   
  123.         size_t len, char *buf )  
  124. {  
  125.     int r_count = 0, i;  
  126.   
  127.     flash->cmd[0] = CMD_READ_BYTES;  
  128.     flash->cmd[1] = from >> 16;  
  129.     flash->cmd[2] = from >> 8;  
  130.     flash->cmd[3] = from;  
  131.       
  132. #if 1  
  133.     struct spi_transfer st[2];  
  134.     struct spi_message  msg;  
  135.       
  136.     spi_message_init( &msg );  
  137.     memset( st, 0, sizeof(st) );  
  138.   
  139.     flash->cmd[0] = CMD_READ_BYTES;  
  140.     flash->cmd[1] = from >> 16;  
  141.     flash->cmd[2] = from >> 8;  
  142.     flash->cmd[3] = from;  
  143.   
  144.     st[ 0 ].tx_buf = flash->cmd;  
  145.     st[ 0 ].len = CMD_SZ;  
  146.     spi_message_add_tail( &st[0], &msg );  
  147.   
  148.     st[ 1 ].rx_buf = buf;  
  149.     st[ 1 ].len = len;  
  150.     spi_message_add_tail( &st[1], &msg );  
  151.   
  152.     mutex_lock( &flash->lock );  
  153.       
  154.     /* Wait until finished previous write command. */  
  155.     if (wait_till_ready(flash)) {  
  156.         mutex_unlock( &flash->lock );  
  157.         return -1;  
  158.     }  
  159.   
  160.     spi_sync( flash->spi, &msg );  
  161.     r_count = msg.actual_length - CMD_SZ;  
  162.     printk( "in (%s): read %d bytes\n", __func__, r_count );  
  163.     for( i = 0; i < r_count; i++ ) {  
  164.         printk( "0x%02x\n", buf[ i ] );  
  165.     }  
  166.   
  167.     mutex_unlock( &flash->lock );  
  168. #endif  
  169.   
  170.     return 0;  
  171. }  
  172.   
  173. /*  
  174.  * Write an address range to the flash chip.  Data must be written in  
  175.  * FLASH_PAGE_SIZE chunks.  The address range may be any size provided  
  176.  * it is within the physical boundaries.  
  177.  */  
  178. static int m25p10a_write( struct m25p10a *flash, loff_t to,   
  179.         size_t len, const char *buf )  
  180. {  
  181.     int w_count = 0, i, page_offset;  
  182.     struct spi_transfer st[2];  
  183.     struct spi_message  msg;  
  184. #if 1  
  185.     if (wait_till_ready(flash)) {    //读状态,等待ready  
  186.         mutex_unlock( &flash->lock );  
  187.         return -1;  
  188.     }  
  189. #endif  
  190.     write_enable( flash );  //写使能  
  191.       
  192.     spi_message_init( &msg );  
  193.     memset( st, 0, sizeof(st) );  
  194.   
  195.     flash->cmd[0] = CMD_PAGE_PROGRAM;  
  196.     flash->cmd[1] = to >> 16;  
  197.     flash->cmd[2] = to >> 8;  
  198.     flash->cmd[3] = to;  
  199.   
  200.     st[ 0 ].tx_buf = flash->cmd;  
  201.     st[ 0 ].len = CMD_SZ;  
  202.     spi_message_add_tail( &st[0], &msg );  
  203.   
  204.     st[ 1 ].tx_buf = buf;  
  205.     st[ 1 ].len = len;  
  206.     spi_message_add_tail( &st[1], &msg );  
  207.   
  208.     mutex_lock( &flash->lock );  
  209.   
  210.     /* get offset address inside a page */  
  211.     page_offset = to % FLASH_PAGE_SIZE;       
  212.   
  213.     /* do all the bytes fit onto one page? */  
  214.     if( page_offset + len <= FLASH_PAGE_SIZE ) { // yes  
  215.         st[ 1 ].len = len;   
  216.         printk("%d, cmd = %d\n", st[ 1 ].len, *(char *)st[0].tx_buf);  
  217.         //while(1)  
  218.         {  
  219.         spi_sync( flash->spi, &msg );  
  220.         }  
  221.         w_count = msg.actual_length - CMD_SZ;  
  222.     }  
  223.     else {  // no  
  224.     }  
  225.     printk( "in (%s): write %d bytes to flash in total\n", __func__, w_count );  
  226.     mutex_unlock( &flash->lock );  
  227.     return 0;  
  228. }  
  229.   
  230. static int check_id( struct m25p10a *flash )   
  231. {   
  232.     char buf[10] = {0};   
  233.     flash->cmd[0] = CMD_READ_ID;  
  234.     spi_write_then_read( flash->spi, flash->cmd, 1, buf, 3 );   
  235.     printk( "Manufacture ID: 0x%x\n", buf[0] );  
  236.     printk( "Device ID: 0x%x\n", buf[1] | buf[2]  << 8 );  
  237.     return buf[2] << 16 | buf[1] << 8 | buf[0];   
  238. }   
  239.   
  240. static int m25p10a_probe(struct spi_device *spi)   
  241. {   
  242.     int ret = 0;  
  243.     struct m25p10a  *flash;  
  244.     char buf[ 256 ];  
  245.     printk( "%s was called\n", __func__ );  
  246.     flash = kzalloc( sizeof(struct m25p10a), GFP_KERNEL );  
  247.     if( !flash ) {  
  248.         return -ENOMEM;  
  249.     }  
  250.     flash->spispi = spi;  
  251.     mutex_init( &flash->lock );  
  252.     /* save flash as driver's private data */  
  253.     spi_set_drvdata( spi, flash );  
  254.       
  255.     check_id( flash );    //读取ID  
  256. #if 1  
  257.     ret = erase_chip( flash );  //擦除  
  258.     if( ret < 0 ) {  
  259.         printk( "erase the entirely chip failed\n" );  
  260.     }  
  261.     printk( "erase the whole chip done\n" );  
  262.     memset( buf, 0x7, 256 );  
  263.     m25p10a_write( flash, 0, 20, buf); //0地址写入20个7  
  264.     memset( buf, 0, 256 );  
  265.     m25p10a_read( flash, 0, 25, buf ); //0地址读出25个数  
  266. #endif  
  267.     return 0;   
  268. }   
  269.   
  270. static int m25p10a_remove(struct spi_device *spi)   
  271. {   
  272.     return 0;   
  273. }   
  274.   
  275. static struct spi_driver m25p10a_driver = {   
  276.     .probe = m25p10a_probe,   
  277.     .remove = m25p10a_remove,   
  278.     .driver = {   
  279.         .name = "m25p10a",   
  280.     },   
  281. };   
  282.   
  283. static int __init m25p10a_init(void)   
  284. {   
  285.     return spi_register_driver(&m25p10a_driver);   
  286. }   
  287.   
  288. static void __exit m25p10a_exit(void)   
  289. {   
  290.     spi_unregister_driver(&m25p10a_driver);   
  291. }   
  292.   
  293. module_init(m25p10a_init);   
  294. module_exit(m25p10a_exit);   
  295.   
  296. MODULE_DESCRIPTION("m25p10a driver for FS_S5PC100");   
  297. MODULE_LICENSE("GPL");  
linux
相关资讯       Linux驱动  SPI驱动 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款