⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 conf_erase.c

📁 uclinux下 intel mx flash驱动源码!
💻 C
字号:
/*************************************************
  Copyright (C), 1997-2006, Centnet Tech. Co., Ltd.
  File name:     adspdev.c
  Author:Zhoujh       Version: 1.0       Date: April 05 2006
  Description: 
                  
  Others:     
  Function List: 
    1. ....
  History:       
                  
    1. Date:
       Author:
       Modification:
    2. ...
*************************************************/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/file.h>
#include <linux/smp_lock.h>
#include <linux/devfs_fs_kernel.h>
#include <linux/moduleparam.h>
#include <asm/page.h>
#include <asm/uaccess.h>
#include <asm/unistd.h>

#include <linux/config.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/string.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/interrupt.h>
#include <linux/mm.h>
#include <linux/types.h>
#include <linux/sched.h>
#include <linux/timer.h>
#include <asm/irq.h>
#include <asm/blackfin.h>
#include <linux/reboot.h>

#include "uboot_common.h"


#define SETDEFAULT_FIO_PATTERN			PF7
#if 0
int getCmdForNumber(const char* str,int slen,char* besc, int *blen)
{
    int derection = 0;
    int temp = 0;
    int size_besc = 0;
    int i = 0;
    int j = 0;
    /*对入口参数进行检查*/
    if( (slen < 1) || (*blen < 1) || (str == NULL) || (besc == NULL) )
    {
	 printk("%s %d\n\r", __FILE__, __LINE__);
        return 0;
    }
    for(i = 0; i < slen; i++)
    {
        /*查找单词line*/ 
        while( (str[i] == 'l')  && (str[i+1] == 'i') && 
            (str[i+2] == 'n') && (str[i+3] == 'e') )
        {
	     printk("##\n\r");
            /*由于每一条命令行的长度要占最多85个字节,字符串还要有一个结束符号,所以要加85*/ 
            if( size_besc+85 > *blen)              /*若besc字符串的长度不够*/
            {
		   printk("%s %d\n\r", __FILE__, __LINE__);
                return 0;
            } 
               
            derection = size_besc;                /*记录写入字母l所在的位置*/
			
            for(j = 0; j < 4; j++)            /*写入line*/
            {
                besc[size_besc++] = str[i++];
            }
            while( str[i] == ' ')                /*跳过空格*/
            {
                i++;             
            }
            besc[size_besc++] = ' ';            /*写入空格*/          
            while( (str[i] >= '0') && (str[i] <= '9') ) 
            {
                besc[size_besc++] = str[i++];   /*写入line后面的数字*/  
            }
            while( str[i] == ' ')               /*跳过空格*/
            {
                i++;             
            }
            if( (str[i] == '\n')  || (str[i] == '\r') )
            {
                /*写入回车换行*/
                besc[size_besc++] = '\n';            
                besc[size_besc++] = '\r';
            }
            /*查找单词number,确保单词number的完整性*/
            while( (str[i] != 'n')  || (str[i+1] != 'u') || (str[i+2] != 'm') || 
                   (str[i+3] != 'b') || (str[i+4] != 'e') || (str[i+5] != 'r') )
		{

			if( (str[i] == 'l')  && (str[i+1] == 'i') && (str[i+2] == 'n') && (str[i+3] == 'e') )
			{
				//清除无效的命令行
				for(j = derection; j < size_besc; j++)
				{
					besc[j] = '\0';
				}
				size_besc = derection; 
				printk("%s %d size_besc %d\n\r", __FILE__, __LINE__, size_besc);
				break;
			}
			i++;
			/*防止访问越界*/
			if(i > slen)			
			{
				/*清除无效的命令行*/ 
				for(j = derection; j < size_besc; j++)
				{
					besc[j] = '\0';
				}
				size_besc = derection; 
				printk("%s %d size_besc %d\n\r", __FILE__, __LINE__, size_besc);
				break;
			}
		}
            	if( (str[i] == 'n')  && (str[i+1] == 'u') && (str[i+2] == 'm') && 
                (str[i+3] == 'b') && (str[i+4] == 'e') && (str[i+5] == 'r') )
            	{
                /*写入number和空格*/
                for(j = 0; j < 7; j++)
                {
                    besc[size_besc++] = str[i++];
                }
                   
                while( str[i] == ' ')               /*跳过空格*/
                {
                    i++;             
                }
                while( (str[i] >= '0') && (str[i] <= '9') ) 
                {
                    besc[size_besc++] = str[i++];   /*写入number后面的数字*/  
                }
                
                while( str[i] == ' ')               /*跳过空格*/
                {
                    i++;             
                }
				i++;
				if( (str[i] == '\n') || (str[i] == '\r') )
				{
					besc[size_besc++] = '\n';
					besc[size_besc++] = '\r';
				}
				i++;
				
				if(i > slen-1)
				{
					/*清除无效的命令行*/ 
					for(j = derection; j < size_besc; j++)
					{
						besc[j] = '\0';
					}
					size_besc = derection;
					printk("%s %d size_besc %d\n\r", __FILE__, __LINE__, size_besc);
				}

				/*确认命令结束行是否有“!”,校验命令的正确性*/
				while(i < slen)
				{
					if( str[i] == '!' )
					{
						besc[size_besc++] = '!';
						i++;
						break;		
					}
					else
					{
						i++;
					}
					if( i > slen - 2)		/*没有找到\n\r*/
					{
						/*清除无效的命令行*/ 
						for(j = derection; j < size_besc; j++)
						{
							besc[j] = '\0';
						}
						size_besc = derection;
						printk("%s %d size_besc %d\n\r", __FILE__, __LINE__, size_besc);
					}
				}
				while( i < slen )
				{
					if( (str[i] == '\n') || (str[i] == '\r') )
					{
						besc[size_besc++] = '\n';
						besc[size_besc++] = '\r';
						break;
					}
					i++;
					if( i > slen)		/*没有找到!*/
					{
						/*清除无效的命令行*/ 
						for(j = derection; j < size_besc; j++)
						{
							besc[j] = '\0';
						}
						size_besc = derection;
						printk("%s %d size_besc %d\n\r", __FILE__, __LINE__, size_besc);
					}
				}
            } 
            /*end if*/                                                                                       
        }
        /*end while*/                         
    }
    /*end for*/ 
    if( (size_besc > *blen ) || (size_besc == 0))            /*若besc的长度不够*/
    {
        printk("%s %d size_besc %d blen %d\n\r", __FILE__, __LINE__, size_besc, *blen);
        return 0;
    }
    besc[size_besc++] = '\0';                              /*加入字符串结束标志*/
    *blen = size_besc;
    return *blen;          
}    


int getCmdListForKey(const char *cmd, int slen, char *desc, int *dlen, char * keyword)
{

	char *str = cmd;
	char *str1 = NULL;
	char *str2 = NULL;
	int len = 0;
	
	while((str1 = strstr(str, keyword)) != NULL)
	{
		if((str1 - str) > 0)
		{
			if(*(str1-1) != '\n' && *(str1-1) != '\r' && *(str1-1) != ' ')
			{
				str = (str1+strlen(keyword));
				continue;
			}
		}

		while((str2 = strstr(str1, "!")) != NULL)
		{
			if((slen - (str1-cmd)> (str2-str1)) + strlen("!"))
			{
				if(str2[strlen("!")] != ' ' && str2[strlen("!")] != '\n' && str2[strlen("!")] != '\r')
				{
					str1 = (str2+strlen("!"));
					continue;
				}
			}
			
			memcpy(&desc[len], str1, (str2-str1));
			desc[str2-str1+len] = '!';
			desc[str2-str1+1+len] = '\n';
			desc[str2-str1+2+len] = '\r';
			desc[str2-str1+3+len] = '\0';
			len += str2 - str1+3;
			str=str2;
			break;
		}

	}
	*dlen = len;
	return len;
}


int getCmdList(const char *cmd, int clen, char *desc, int *dlen)
{

	char buff[512] = {0};
	int len = 0;
	int contlen = 0;

	len=512;
	if((len = getCmdListForKey(cmd, clen,buff, &len, "line")) > 0)
	{
		if(len >= dlen-contlen)
			return 0;
		
		memcpy((char*)(desc+contlen), buff, len);
		contlen += len;
	}
	
	len=512;
	if((len = getCmdListForKey(cmd, clen,buff, &len, "h323")) > 0)
	{
		if(len >= dlen-contlen)
			return 0;
		
		memcpy((char*)(desc+contlen), buff, len);
		contlen += len;
	}
	
	len=512;
	if((len= getCmdListForKey(cmd, clen,buff, &len, "sip")) > 0)
	{
		if(len >= dlen-contlen)
			return 0;
		memcpy((char*)(desc+contlen), buff, len);;		
		contlen += len;
	}
	
	*dlen = contlen;
	return *dlen;
}

#endif

 /******************************************************************
  Function Name : pf7_interrupt_handle()
  Author : Zhoujh
  Create Date : Aug 05 2006
  Description :
  Parameter Description :
  Modify History : 
 ******************************************************************/
irqreturn_t pf7_interrupt_handle(int irq, void *dev_id, struct pt_regs *pt_regs)
{
	#define RESET_CMD_BUFF_LEN 10240
	ulong	end_addr;
	ulong	flash_sect_addr;
	unsigned char *buff = NULL;
	unsigned char *cmd = NULL;
	unsigned char *str1 = NULL;
	unsigned int cmdlen = 0;
	int rc = 0;
	int i = 0;
	int isreset = 0;
	
	*pFIO_FLAG_C |= SETDEFAULT_FIO_PATTERN; /* clear irq status on interrupt lines */
	flash_sect_addr = 0x201f0000;
	end_addr = 0x201fffff;
	
	buff = (unsigned char *)kmalloc(RESET_CMD_BUFF_LEN, GFP_KERNEL);
	if (buff == NULL)
	{
		printk("error alloc memory in pf7_interrupt_handle().\n\r");
		return IRQ_HANDLED;
	}
	
	memset(buff, 0, RESET_CMD_BUFF_LEN);
	
	cmd = (unsigned char*)flash_sect_addr;

	cmdlen = ((unsigned int * )cmd)[0];
	
	if (cmdlen < RESET_CMD_BUFF_LEN && cmdlen > 0)
	{
		memcpy(buff, cmd, cmdlen);
		if ((str1 = strstr(&buff[4], "enpass")) != NULL)
		{
			for(i = 0; i <cmdlen && str1[i] != '\n' && str1[i] != '\r' && str1[i] != 0; i++)
			{
				str1[i] ='\n';
			}
			isreset = 1;
		}
		if ((str1 = strstr(&buff[4], "telconf")) != NULL)
		{
			for(i = 0; i <cmdlen && str1[i] != '\n' && str1[i] != '\r' && str1[i] != 0; i++)
			{
				str1[i] ='\n';
			}
			isreset = 1;
		}
		if ((str1 = strstr(&buff[4], "webconf")) != NULL)
		{
			for(i = 0; i <cmdlen && str1[i] != '\n' && str1[i] != '\r' && str1[i] != 0; i++)
			{
				str1[i] ='\n';
			}
			isreset = 1;
		}
		if ((str1 = strstr(&buff[4], "ivr")) != NULL)
		{
			if(str1> &buff[4] && (*(str1-1) == '\n' || *(str1-1) =='\r') &&(*(str1+3) == '\n' || *(str1+3) =='\r'))
			{
				for(i = 0; i<cmdlen && str1[i+3] != 0; i++)
				{
					if (str1[i+3] != '\n' && str1[i+3] != '\r')
					{
						str1 = &str1[i+3];
						for(i = 0; i <cmdlen && str1[i] != '!' && str1[i] != 0; i++)
						{
							str1[i] ='\n';
						}
						isreset = 1;
						break;
					}
				}
				
			}

		}
	}
	else 
	{
		kfree(buff);
		return IRQ_HANDLED;
	}
	
	if (isreset == 0)
	{
		kfree(buff);
		return IRQ_HANDLED;
	}
	
	if (uboot_flash_sect_protect (0, flash_sect_addr, end_addr))
	{
		printk("uboot_flash_sect_protect  error\n");
		kfree(buff);
		return IRQ_HANDLED;
	}

	uboot_flash_sect_erase (flash_sect_addr, end_addr);	//0x201F0000~ 0x201FFFFF
	
       if ((rc = uboot_flash_write(buff, flash_sect_addr, (ulong)cmdlen)) != 0)/*"cmd\n\r"0000*/
	{
		uboot_flash_perror (rc);
	}
	
       uboot_flash_sect_protect (1, flash_sect_addr, end_addr);

	kfree(buff);

	machine_restart(NULL);

	return IRQ_HANDLED;    
}
 
#if 0
{
	#define RESET_CMD_BUFF_LEN 1024
	ulong	end_addr;
	ulong	flash_sect_addr;
	char buff[RESET_CMD_BUFF_LEN] = {0};
	int len = 0;
	int i = 0;
	int rc = 0;
	char *cmd = NULL;
	int cmdlen = 0;
	
	*pFIO_FLAG_C |= SETDEFAULT_FIO_PATTERN; /* clear irq status on interrupt lines */
	flash_sect_addr = 0x201f0000;
	end_addr = 0x201fffff;

	cmd = (char*)flash_sect_addr;

	if(cmd[0] != 0xff && cmd[0] != 0)	
	{
		len = RESET_CMD_BUFF_LEN;
		cmdlen = (int)(cmd[0]);
		len = getCmdList(&cmd[4], cmdlen, &buff[4], &len);
	}
	
	if (uboot_flash_sect_protect (0, flash_sect_addr, end_addr))
	{
		printk("uboot_flash_sect_protect  error\n");
		return IRQ_HANDLED;
	}

	printk("Erase configuration...");
	uboot_flash_sect_erase (flash_sect_addr, end_addr);	//0x201F0000~ 0x201FFFFF
	printk("OK\n");
	
	memcpy(&buff[0], &len, 4);
       uboot_flash_write(buff, flash_sect_addr, len+5);
	if (rc != 0)
	{
		uboot_flash_perror (rc);
	}

       uboot_flash_sect_protect (1, flash_sect_addr, end_addr);

	machine_restart(NULL);
	return IRQ_HANDLED;    
}
#endif
 /******************************************************************
  Function Name : init_pf7_interrupt()
  Author : Zhoujh
  Create Date : Aug 08 2006
  Description :
  Parameter Description :
  Modify History : 
 ******************************************************************/
int init_pf7_interrupt(void)
{
	int ret_irq = 0;

	__builtin_bfin_csync();
	*pFIO_MASKA_C  |=  SETDEFAULT_FIO_PATTERN;
	__builtin_bfin_ssync();

	*pFIO_POLAR   |= SETDEFAULT_FIO_PATTERN;
	*pFIO_EDGE    &= ~SETDEFAULT_FIO_PATTERN;
	*pFIO_BOTH    &= ~SETDEFAULT_FIO_PATTERN;

	*pFIO_DIR     &= ~SETDEFAULT_FIO_PATTERN;
	*pFIO_FLAG_C   |=  SETDEFAULT_FIO_PATTERN;
	*pFIO_INEN    |=  SETDEFAULT_FIO_PATTERN;

	__builtin_bfin_ssync();
	*pFIO_MASKA_S  |=  SETDEFAULT_FIO_PATTERN;
	__builtin_bfin_ssync();

	ret_irq = request_irq(IRQ_PROG_INTA, pf7_interrupt_handle,SA_INTERRUPT, "pf7intA", NULL);

	if(ret_irq != 0)
	{
		printk("\nFailed to request IRQ_PROG_INTA\n\n");
		return -1;
	}

	return 0;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -