-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNX_flash.c
2661 lines (2129 loc) · 77.4 KB
/
NX_flash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*********************************************************************************
*Flash (Data Record Management) driver *
*Version: 1.0.0 *
*2007/07/16 writed by Bulla Liu
*(C) Copyright 2007-2009, Emeson NetworkPower China Inc.
*
*
*********************************************************************************/
#include <linux/config.h>
#include <linux/version.h>
#include <linux/types.h>
#include <linux/sched.h>
#include <linux/timer.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/delay.h>
#include <linux/time.h>
#include <asm/param.h>
//#include <linux/malloc.h>
#include <asm/uaccess.h>
#include <linux/errno.h>
#include <linux/vmalloc.h>
#include <linux/signal.h>
#include <asm/bitops.h>
#include <linux/ctype.h>
//#include <asm/irq.h>
#include <asm/ptrace.h>
#include <linux/fs.h>
#include <linux/wrapper.h>
#include <linux/wait.h>
#include <linux/interrupt.h>
#include <asm/io.h>
#include <linux/ioport.h>
#include <linux/spinlock.h>
#ifdef MODULE
#include <linux/module.h>
#else
#define MOD_INC_USE_COUNT
#define MOD_DEC_USE_COUNT
#endif
#include "NX_flash.h"
#include "NX_record.h"
/*==================================================*/
/*=========以下为驱动设备通用定义部分===============*/
/*==================================================*/
#define DEVICE_NAME "dataflash"
#define DEVICE_MAJOR 166 /* Reserverd for "data flash" */
//#define DEBUG 1 /*调试模式,可以打印一些调试信息*/
/* Function Prototypes */
static int dataflash_device_init (void) __init;
static int dataflash_device_release(struct inode *, struct file *);
static int dataflash_device_open(struct inode *, struct file *);
static ssize_t dataflash_device_read(struct file *,char *,size_t,loff_t *);
static ssize_t dataflash_device_write(struct file *file,char *buf,size_t count,loff_t *ppos);
static int dataflash_device_ioctl(struct inode *inode, struct file *filp,unsigned int cmd, unsigned long arg);
static int Device_Open=0;
static struct file_operations Fops_dataflash =
{
owner: THIS_MODULE,
open: dataflash_device_open,
release: dataflash_device_release,
read: dataflash_device_read,
write: dataflash_device_write,
ioctl: dataflash_device_ioctl,
};
/*==================================================*/
/*=========以下为驱动设备专用定义部分==============*/
/*==================================================*/
static char * dataflash_device_driver_ver="Emerson NX data flash driver version: 1.00\n";
//Flash物料操作的地址指针
static volatile unsigned short * data_flash_base = NULL;
//Flash的基本数据信息
static CFIDENT CFIident;
static CFIFRI CFIpri;
static CFISYS CFIsys;
//Flash的操作状态:Unknown, Reading, Programming, Block erasing, Chip erasing.
static volatile unsigned char ucFlashStatus = 0;
//Flash的擦除的计数器
static volatile unsigned int uiErase200msCnt = 0;
//当前正在擦除的Block编号
static volatile unsigned char ucBlockInErasing = 0xFF;
//Flash中哪个扇区在充当基本扇区.
static volatile unsigned int uiBasicBlock;
//Flash中每个扇区的在整个Flash空间的偏移地址,偏移相对于短整型数据类型. 且第一个Boot扇区定义为第一个扇区号。
static volatile unsigned int uiBlockOffsetAddr[200];
//Flash中每个扇区的在大小,单位为短整型数据类型.
static volatile unsigned int BlockSizeList[200];
//擦除计数器, 当Flash处于读取状态, 会定期搜索Dirty扇区进行擦除.
static struct timer_list erase_timer;
static void erase_state_poll(unsigned long period);
//读取Flash芯片的CFI接口数据
static void ReadCFI(void);
//Flash芯片的底层操作函数
static void EraseSuspend();
static void EraseResume();
static inline unsigned short ReadWord(unsigned int addr);
static void BlockErase(unsigned short No);
static void ChipErase(void);
static void RecordDataBlockErase(unsigned short uBlockSeclect);
static void Send_CMD(unsigned char cmd);
static unsigned char WriteWord(unsigned int addr, unsigned short data);
//Flash芯片的基本信息数据结构变量
static BASIC_FLASH_INFO_T tBasicFlashInfo;
//Flash芯片中记录的通用数据结构变量
static FLASH_RECORD_T tFlashRecordW;
//Flash芯片记录管理数据结构变量
static RECORD_MANAGE_T tRecordManageParameterp[INV_FAULT_RECORD+1];
//向Flash中写入一条指定的记录数据
static unsigned char ucWriteOneRecord(FLASH_RECORD_T *pFlashRecord);
//从Flash中读出一条指定的记录数据
static unsigned char ucReadOneRecord(FLASH_RECORD_T *pFlashRecord);
//刷新Flash的基本信息, 如果没有建立基本信息记录,就重新建立
static unsigned char ucUpdateFlashBasicInformation(void);
//根据Flash中各个记录的现状, 刷新Flash操作的数据结构
static unsigned char ucInitialRecordManagement(void);
//从Flash中读取一条记录指定编号的记录数据
static unsigned char ucReadOneRecordFromFlash(FLASH_RECORD_T *pFlashRecord);
//向Flash中写入一条新记录数据
static unsigned char ucWriteOneNewRecordToFlash(FLASH_RECORD_T * pRecord);
static FLASH_RECORD_T tFlashRecordR;
static FLASH_RECORD_T tRecordForTest;
static void vWriteAEventRecordForTest();
static void vReadAEventRecordForTest();
//static unsigned char ucReadOneFaultRecordFromFlash(FLASH_RECORD_T *pFlashRecord);
//static unsigned char ucWriteOneFaultRecordToFlash(FLASH_RECORD_T * pRecord);
static void vWriteAFaultRecordForTest();
static void vReadAFaultRecordForTest();
static void vPrintAllValidDataOnOneBlock(unsigned char ucBlock);
static void vPrintAllValidRecordOnOneBlock(unsigned char ucBlock);
static void vWriteABlockRecordForTest(unsigned char ucRecordType);
static void vReadABlockRecordForTest(unsigned char ucRecordType);
int Debug = 1;
MODULE_PARM(Debug,"i");
/* Initialize the driver - Register the character device */
static int __init dataflash_device_init (void)
{
int ret;
int i;
int x,y;
struct timeval tv1,tv2;
unsigned long tCurrentTime;
unsigned long flags;
/* Register the character device */
if ((ret = register_chrdev(DEVICE_MAJOR, DEVICE_NAME, &Fops_dataflash)) < 0)
{
printk ("dataflash Device failed with %d\n", ret);
return (ret);
}
PRINT_TRACE(Debug)("%s the major is %d.\n",dataflash_device_driver_ver,DEVICE_MAJOR);
data_flash_base = (volatile unsigned short *)ioremap(DATA_FLASH_BASE,0x800000);
ReadCFI();
Send_CMD(CMD_RESET_DATA);
//ChipErase();
ret = ucUpdateFlashBasicInformation();
ret = ucInitialRecordManagement();
//for test
printk ("--flash:record block1= %d,block2=%d---\n", tRecordManageParameterp[EVENT_RECORD].tUsedBlocks[0].ucBlockNum,
tRecordManageParameterp[EVENT_RECORD].tUsedBlocks[1].ucBlockNum);
del_timer_sync(&erase_timer);
init_timer(&erase_timer);
erase_timer.function = erase_state_poll;
erase_timer.data = ERASE_CHECK_PERIOD; /*check state per 100ms*/
erase_timer.expires = jiffies + erase_timer.data;
add_timer(&erase_timer);
return (0);
}
static int dataflash_device_open (struct inode *inode, struct file *file)
{
int num = MAJOR(inode->i_rdev);
int type = MINOR(inode->i_rdev);
printk ("flash device_open(%p,%p)\n", inode, file);
printk("dataflash device major is %d\n",num);
printk("dataflash device minor is %d\n",type);
if (Device_Open>0)
{
printk("dataflash Device is busy!\n");
return -EBUSY;
}
MOD_INC_USE_COUNT;
Device_Open++;
return 0;
}
static int dataflash_device_release (struct inode *inode, struct file *file)
{
printk ("flash device_release(%p,%p)\n", inode, file);
MOD_DEC_USE_COUNT;
Device_Open--;
return 0;
}
static ssize_t dataflash_device_read (struct file *file,char *buf, size_t count, loff_t *ppos)
{
int rc=0;
unsigned char i;
if (count!=sizeof(FLASH_RECORD_T))
{
printk("dataflash Read Frame size is error!\n");
return ERROR_READ_SYSCALL_COUNT;
}
rc = verify_area(VERIFY_WRITE,buf,count);
if (rc == -EFAULT)
{
printk("dataflash driver read syscall verify area fail\n");
return ERROR_READ_SYSCALL_VERIRY;
}
copy_from_user((char *)(&tFlashRecordR), buf, count);
PRINT_TRACE(Debug)("dataflash read syscall entry ---- %d\n", tFlashRecordR.usID);
//开始读取Flash中的记录
rc = ucReadOneRecord(&tFlashRecordR);
if (rc == 0x00)
{
copy_to_user((char *)buf,(char *)(&tFlashRecordR),count);
}
return rc;
}
static ssize_t dataflash_device_write(struct file *file,char *buf,size_t count,loff_t *ppos)
{
int rc=0;
unsigned char ret, i;
if (count!=sizeof(FLASH_RECORD_T))
{
printk("dataflash Write Frame size is error!\n");
return (ERROR_WRITE_SYSCALL_COUNT);
}
rc = verify_area(VERIFY_WRITE,buf,count);
if (rc == -EFAULT)
{
printk("dataflash Write read syscall verify area fail\n");
return ERROR_WRITE_SYSCALL_VERIRY;
}
copy_from_user((char *)(&tFlashRecordW), buf, count);
ret = ucWriteOneRecord(&tFlashRecordW);
return(ret);
}
static int dataflash_device_ioctl(struct inode *inode, struct file *file,
unsigned int cmd, unsigned long arg)
{
int rc = 0;
/*控制命令必须是已定义的*/
if ((cmd != CMD_SECTOR_ERASE_UNLOCK_DATA) && (cmd != CMD_CHIP_ERASE_UNLOCK_DATA)
&& (cmd != CMD_GET_FLASH_OPRATION_STATE) && (cmd != CMD_EVENT_RECORD_ERASE))
{
printk("dataflash_device_ioctl() Error: no recognized cmd value!\n");
rc = -EINVAL;
return(ERROR_IOCTL_SYSCALL_CMD);
}
/*控制命令的参数也必须是已经定义过的*/
if (cmd == CMD_SECTOR_ERASE_UNLOCK_DATA)
{
if ((arg > tBasicFlashInfo.usTotalBlocks)||(arg == tBasicFlashInfo.usTotalBlocks))
{
printk("dataflash_device_ioctl() Error: no recognized arg value! --- %d\n",arg);
rc = -EINVAL;
return(ERROR_IOCTL_SYSCALL_ARG);
}
}
//FLASH state
if (cmd == CMD_GET_FLASH_OPRATION_STATE)
{
return ucFlashStatus;
}
if (ucFlashStatus != READING)
{
printk("dataflash_device_ioctl() Error: The chip is not in Reading state -- %d\n", ucFlashStatus);
rc = -EINVAL;
return(ERROR_IOCTL_SYSCALL_BUSY);
}
//块擦除
if (cmd == CMD_SECTOR_ERASE_UNLOCK_DATA)
{
BlockErase(arg);
}
//片擦除
if (cmd == CMD_CHIP_ERASE_UNLOCK_DATA)
{
ChipErase();
}
//erase event record
if (cmd == CMD_EVENT_RECORD_ERASE)
{
RecordDataBlockErase(arg);
}
return (rc);
}
/*
* Cleanup - unregister the driver
*/
static void __exit uninst_dataflash_device(void)
{
int minor, ret;
del_timer_sync(&erase_timer);
/* Unregister the device */
ret = unregister_chrdev (DEVICE_MAJOR, DEVICE_NAME);
iounmap((unsigned char *)data_flash_base);
/* If there's an error, report it */
if (ret < 0)
{
printk ("unregister_chrdev: error %d\n", ret);
return;
}
PRINT_TRACE(Debug)("dataflash driver succesfully uninstalled\n");
}
/****************************************************/
/*---对 Flash 器件的底层操作函数集------*/
/****************************************************/
static unsigned int uiPower(unsigned char base, unsigned short factor)
{
unsigned short i;
unsigned int result;
result = 1;
for (i=0; i<factor; i++)
{
result = result * base;
}
return(result);
}
/****************************************************/
/*函数名称:erase_state_poll */
/*函数功能:Erase process counter */
/*输入参数: */
/* period: */
/*输出参数: N/A */
/*修改记录: */
/****************************************************/
static int testvar = 0;
static void erase_state_poll(unsigned long period)
{
unsigned char i,j;
unsigned short status1, status2, ret;
unsigned char ucSize;
unsigned int uiStartAddressOfFlag;
ucBlockInErasing = 0xFF;
if ((ucFlashStatus == BLOCKERASING) || (ucFlashStatus == CHIPERASING))
{
status1 = * data_flash_base; /*读取一次状态寄存器*/
status2 = * data_flash_base; /*读取一次状态寄存器*/
if ((status1 & 0x40) == (status2 & 0x40))
{
//正常结束
ucFlashStatus = READING; //Register a reading status
uiErase200msCnt = 0;
PRINT_TRACE(Debug)("Erase is finished\n");
//清除擦除标志
for (i=0; i<INV_FAULT_RECORD; i++)
{
if (tRecordManageParameterp[i+1].tUsedBlocks[0].ucBlockStatus == BLOCK_ERASING)
{
tRecordManageParameterp[i+1].tUsedBlocks[0].ucBlockStatus = BLOCK_NO_DATA;
break;
}
else if (tRecordManageParameterp[i+1].tUsedBlocks[1].ucBlockStatus == BLOCK_ERASING)
{
tRecordManageParameterp[i+1].tUsedBlocks[1].ucBlockStatus = BLOCK_NO_DATA;
break;
}
}
}
else if ((status1 & 0x0020) == 0x0020)
{
/*异常结束*/
ucFlashStatus = READING; /*Register a reading status*/
uiErase200msCnt = 0; /*stop a 100ms counter*/
/*强制恢复到读状态*/
*(data_flash_base+0x55)=0xF0;
//清除擦除标志
for (i=0; i<INV_FAULT_RECORD; i++)
{
if (tRecordManageParameterp[i+1].tUsedBlocks[0].ucBlockStatus == BLOCK_ERASING)
{
tRecordManageParameterp[i+1].tUsedBlocks[0].ucBlockStatus = BLOCK_NO_DATA;
break;
}
else if (tRecordManageParameterp[i+1].tUsedBlocks[1].ucBlockStatus == BLOCK_ERASING)
{
tRecordManageParameterp[i+1].tUsedBlocks[1].ucBlockStatus = BLOCK_NO_DATA;
break;
}
}
}
else
{
uiErase200msCnt ++;
if (ucFlashStatus == CHIPERASING)
{
PRINT_TRACE(Debug)("In progress of Chip erasing ---%d\n", uiErase200msCnt);
}
}
}
else if (ucFlashStatus == READING) //Flash 处于读状态时,主动找Dirty状态扇区来擦写
{
//新方法:查找一个Dirty扇区进行擦除
for (i=0; i<INV_FAULT_RECORD; i++)
{
if (tRecordManageParameterp[i+1].tUsedBlocks[0].ucBlockStatus == BLOCK_IS_DIRTY)
{
ucBlockInErasing = tRecordManageParameterp[i+1].tUsedBlocks[0].ucBlockNum;
BlockErase(ucBlockInErasing); //找到一个Dirty扇区,就开始擦除
tRecordManageParameterp[i+1].tUsedBlocks[0].ucBlockStatus = BLOCK_ERASING;
PRINT_TRACE(Debug)("******The %d block will be erased \n", ucBlockInErasing);
return;
}
else if (tRecordManageParameterp[i+1].tUsedBlocks[1].ucBlockStatus == BLOCK_IS_DIRTY)
{
ucBlockInErasing = tRecordManageParameterp[i+1].tUsedBlocks[1].ucBlockNum;
BlockErase(ucBlockInErasing); //找到一个Dirty扇区,就开始擦除
tRecordManageParameterp[i+1].tUsedBlocks[1].ucBlockStatus = BLOCK_ERASING;
PRINT_TRACE(Debug)("******The %d block will be erased \n", ucBlockInErasing);
return;
}
}
}
//没有找到一个Dirty扇区,或者没在读取状态下,便设置下次调用为 200ms 以后
erase_timer.data = ERASE_CHECK_PERIOD;
erase_timer.expires = jiffies + erase_timer.data;
add_timer(&erase_timer);
}
/****************************************************/
/*函数名称:ReadCFI */
/*函数功能:读取flash的功能flash接口 */
/*输入参数: N/A */
/* */
/*输出参数: N/A */
/*修改记录: */
/****************************************************/
static void ReadCFI(void)
{
unsigned char i,j,k;
unsigned int FlashSize;
unsigned short *p, number, blocksize;
/* 读取CFI 信息 */
/* 1, 先写入控制命令*/
*(data_flash_base + ADDR_CFI_READ) = CMD_CFI_READ;
/* 2, 读取CFI内容*/
CFIident.qry[0] = *(data_flash_base + 0x10);
CFIident.qry[1] = *(data_flash_base + 0x11);
CFIident.qry[2] = *(data_flash_base + 0x12);
if ((CFIident.qry[0] == 'Q') || (CFIident.qry[1] == 'R') ||(CFIident.qry[2] == 'Y'))
{
/*继续读取CFI identify*/
CFIident.P_ID = *(data_flash_base + 0x13);
CFIident.P_ADR = *(data_flash_base + 0x15);
CFIident.DevSize = *(data_flash_base + 0x27);
CFIident.InterfaceDesc = *(data_flash_base + 0x28);
CFIident.MaxBufWriteSize = *(data_flash_base + 0x2A);
CFIident.NumEraseRegions = *(data_flash_base + 0x2C);
CFIident.NumFirstRegion = ((*(data_flash_base + 0x2E))<<8) + (*(data_flash_base + 0x2D));
CFIident.SizeFirstRegion = ((*(data_flash_base + 0x30))<<8) + (*(data_flash_base + 0x2F));
CFIident.NumSecondRegion = ((*(data_flash_base + 0x32))<<8) + (*(data_flash_base + 0x31));
CFIident.SizeSecondRegion = ((*(data_flash_base + 0x34))<<8) + (*(data_flash_base + 0x33));
CFIident.NumThirdRegion = ((*(data_flash_base + 0x36))<<8) + (*(data_flash_base + 0x35));
CFIident.SizeThirdRegion = ((*(data_flash_base + 0x38))<<8) + (*(data_flash_base + 0x37));
CFIident.NumFouthRegion = ((*(data_flash_base + 0x3a))<<8) + (*(data_flash_base + 0x39));
CFIident.SizeFouthRegion = ((*(data_flash_base + 0x3c))<<8) + (*(data_flash_base + 0x3b));
/*继续读取CFI system*/
CFIsys.TyProgram = *(data_flash_base + 0x1F);
CFIsys.TyBlkErase = *(data_flash_base + 0x21);
CFIsys.TyChipErase = *(data_flash_base + 0x22);
CFIsys.MaxProgrom = *(data_flash_base + 0x23);
CFIsys.MaxBlkErase = *(data_flash_base + 0x25);
CFIsys.MaxChipErase = *(data_flash_base + 0x26);
/*继续读取CFI pri*/
CFIpri.pri[0] = *(data_flash_base + 0x40);
CFIpri.pri[1] = *(data_flash_base + 0x41);
CFIpri.pri[2] = *(data_flash_base + 0x42);
CFIpri.MajorVersion = *(data_flash_base + 0x43);
CFIpri.MinorVersion = *(data_flash_base + 0x44);
CFIpri.EraseSuspend = *(data_flash_base + 0x46);
CFIpri.TopBottomType = *(data_flash_base + 0x4F);
////////////////////////////////////
//打印Flash的基本信息
PRINT_TRACE(Debug)(" ========================================= \n");
PRINT_TRACE(Debug)(" Flash Information (Support CFI interface) \n");
PRINT_TRACE(Debug)(" ========================================= \n");
PRINT_TRACE(Debug)(" 1, Algorithm type: %d \n", CFIident.P_ID);
PRINT_TRACE(Debug)(" 2, Flash Bus type: %d (0:8 1:16 2:8,16) \n", CFIident.InterfaceDesc);
PRINT_TRACE(Debug)(" 3, Flash Size : %d %d bytes \n", CFIident.DevSize,uiPower(2,CFIident.DevSize));
PRINT_TRACE(Debug)(" 4, Flsh Boot type: %d (0:None 2:B 3:T) \n", CFIpri.TopBottomType);
PRINT_TRACE(Debug)(" 5, Flash Erase Sus: %d (0:None 1:OR 2:RW) \n", CFIpri.EraseSuspend);
PRINT_TRACE(Debug)(" 6, Block Regions: %d \n", CFIident.NumEraseRegions);
PRINT_TRACE(Debug)(" 7, 1nd Region Num: %d \n", CFIident.NumFirstRegion+1);
PRINT_TRACE(Debug)(" 8, 1nd Region Size: %d \n", CFIident.SizeFirstRegion * 256);
PRINT_TRACE(Debug)(" 9, 2nd Region Num: %d \n", CFIident.NumSecondRegion+1);
PRINT_TRACE(Debug)(" a, 2nd Region Size: %d \n", CFIident.SizeSecondRegion * 256);
PRINT_TRACE(Debug)(" b, 3nd Region Num: %d \n", CFIident.NumThirdRegion+1);
PRINT_TRACE(Debug)(" c, 3nd Region Size: %d \n", CFIident.SizeThirdRegion * 256);
PRINT_TRACE(Debug)(" d, 4nd Region Num: %d \n", CFIident.NumFouthRegion+1);
PRINT_TRACE(Debug)(" e, 4nd Region Size: %d \n", CFIident.SizeFouthRegion * 256);
PRINT_TRACE(Debug)(" f, Type Prg time: %d %d us \n", CFIsys.TyProgram,uiPower(2,CFIsys.TyProgram));
PRINT_TRACE(Debug)(" g, Type B Er time: %d %d ms \n", CFIsys.TyBlkErase,uiPower(2,CFIsys.TyBlkErase));
PRINT_TRACE(Debug)(" h, Type C Er time: %d %d ms \n", CFIsys.TyChipErase,uiPower(2,CFIsys.TyChipErase));
PRINT_TRACE(Debug)(" i, Max Prg time: %d %d us \n", CFIsys.MaxProgrom,uiPower(2,CFIsys.TyProgram) * uiPower(2,CFIsys.MaxProgrom));
PRINT_TRACE(Debug)(" j, Max B Er time: %d %d ms \n", CFIsys.MaxBlkErase,uiPower(2,CFIsys.TyBlkErase) * uiPower(2,CFIsys.MaxBlkErase));
PRINT_TRACE(Debug)(" k, Max C Er time: %d %d ms \n", CFIsys.MaxChipErase,uiPower(2,CFIsys.MaxChipErase));
PRINT_TRACE(Debug)(" ========================================= \n");
/***************************************************/
/*算出每个Block的起始地址 --- TBD*/
/***************************************************/
for (i=0; i<200; i++)
{
BlockSizeList[i] = 0;
uiBlockOffsetAddr[i] = 0;
}
p = (unsigned short *)(&CFIident.NumFirstRegion);
k = 0;
for (i=0; i<4; i++)
{
number = *(p + 2*i) + 1;
blocksize = *(p + 2*i +1);
for (j=0; j<number; j++)
{
BlockSizeList[k++] = 256 * blocksize; //单位为short int型
}
}
FlashSize = uiPower(2,CFIident.DevSize);
if (CFIpri.TopBottomType == BOTTOM_FLASH)
{
uiBlockOffsetAddr[0] = 0x00;
for (i=1; i<200; i++)
{
uiBlockOffsetAddr[i] = uiBlockOffsetAddr[i-1] + BlockSizeList[i-1];
}
}
else if (CFIpri.TopBottomType == TOP_FLASH)
{
uiBlockOffsetAddr[0] = FlashSize - BlockSizeList[0];
for (i=1; i<200; i++)
{
uiBlockOffsetAddr[i] = uiBlockOffsetAddr[i-1] - BlockSizeList[i];
}
}
//要求下面两个数组的单位必须是INT16
for (i=0; i<200; i++)
{
BlockSizeList[i] = (BlockSizeList[i] >> 1);
uiBlockOffsetAddr[i] = (uiBlockOffsetAddr[i] >> 1);
}
for (i=0; i<200; i++)
{
PRINT_TRACE(Debug)(" %d block offset is %d\n",i,uiBlockOffsetAddr[i]);
}
Send_CMD(CMD_RESET_DATA);
//Flash状态处于读
ucFlashStatus = READING;
}
else
{
PRINT_TRACE(Debug)("This flash is not a CFI standard device.\n");
}
}
/****************************************************/
/*函数名称:Send_CMD */
/*函数功能:向flash接口发送指定的命令 */
/*输入参数: cmd */
/* */
/*输出参数: N/A */
/*修改记录: */
/****************************************************/
static void Send_CMD(unsigned char cmd)
{
switch (cmd)
{
case CMD_RESET_DATA:
{
*(data_flash_base + ADDR_UNLOCK_1) = CMD_UNLOCK_DATA_1;
*(data_flash_base + ADDR_UNLOCK_2) = CMD_UNLOCK_DATA_2;
*(data_flash_base + ADDR_UNLOCK_2) = CMD_RESET_DATA;
break;
}
case CMD_MANUFACTURER_UNLOCK_DATA:
{
*(data_flash_base + ADDR_UNLOCK_1) = CMD_UNLOCK_DATA_1;
*(data_flash_base + ADDR_UNLOCK_2) = CMD_UNLOCK_DATA_2;
*(data_flash_base + ADDR_UNLOCK_1) = CMD_MANUFACTURER_UNLOCK_DATA;
break;
}
case CMD_PROGRAM_UNLOCK_DATA:
{
*(data_flash_base + ADDR_UNLOCK_1) = CMD_UNLOCK_DATA_1;
*(data_flash_base + ADDR_UNLOCK_2) = CMD_UNLOCK_DATA_2;
*(data_flash_base + ADDR_UNLOCK_1) = CMD_PROGRAM_UNLOCK_DATA;
break;
}
case CMD_SECTOR_ERASE_UNLOCK_DATA:
{
*(data_flash_base + ADDR_UNLOCK_1) = CMD_UNLOCK_DATA_1;
*(data_flash_base + ADDR_UNLOCK_2) = CMD_UNLOCK_DATA_2;
*(data_flash_base + ADDR_UNLOCK_1) = CMD_CHIP_SECTOR_ERASE;
*(data_flash_base + ADDR_UNLOCK_1) = CMD_UNLOCK_DATA_1;
*(data_flash_base + ADDR_UNLOCK_2) = CMD_UNLOCK_DATA_2;
break;
}
case CMD_CHIP_ERASE_UNLOCK_DATA:
{
*(data_flash_base + ADDR_UNLOCK_1) = CMD_UNLOCK_DATA_1;
*(data_flash_base + ADDR_UNLOCK_2) = CMD_UNLOCK_DATA_2;
*(data_flash_base + ADDR_UNLOCK_1) = CMD_CHIP_SECTOR_ERASE;
*(data_flash_base + ADDR_UNLOCK_1) = CMD_UNLOCK_DATA_1;
*(data_flash_base + ADDR_UNLOCK_2) = CMD_UNLOCK_DATA_2;
*(data_flash_base + ADDR_UNLOCK_1) = CMD_CHIP_ERASE_UNLOCK_DATA;
break;
}
default:
printk("Unvalid Flash CMD\n");
break;
}
}
/****************************************************/
/*函数名称:ChipErase */
/*函数功能:Erase the entire chip */
/*输入参数: N/A */
/* */
/*输出参数: N/A */
/*修改记录: */
/****************************************************/
static void ChipErase(void)
{
unsigned char ret;
Send_CMD(CMD_CHIP_ERASE_UNLOCK_DATA); /*Send a chip erase command*/
ucFlashStatus = CHIPERASING; /*Register a chiperaseing status*/
uiErase200msCnt = 1; /*Start a 100ms counter*/
del_timer_sync(&erase_timer);
init_timer(&erase_timer);
erase_timer.function = erase_state_poll;
erase_timer.data = ERASE_CHECK_PERIOD; /*check state per 100ms*/
erase_timer.expires = jiffies + erase_timer.data;
add_timer(&erase_timer);
return;
}
/****************************************************/
/*函数名称:BlockErase */
/*函数功能:Erase the designated block */
/*输入参数: */
/* No: the Block Number */
/*输出参数: N/A */
/*修改记录: */
/****************************************************/
static void BlockErase(unsigned short No)
{
unsigned char ret;
//unsigned short TypeBlockEraseTime;
Send_CMD(CMD_RESET_DATA); /*Send a reset common command*/
Send_CMD(CMD_SECTOR_ERASE_UNLOCK_DATA); /*Send a block erase common command*/
*(data_flash_base + uiBlockOffsetAddr[No]) = CMD_SECTOR_ERASE_UNLOCK_DATA;
ucFlashStatus = BLOCKERASING; /*Register a chiperaseing status*/
uiErase200msCnt = 1; /*Start a 100ms counter*/
//TypeBlockEraseTime = uiPower(2,CFIsys.TyBlkErase);
del_timer_sync(&erase_timer);
init_timer(&erase_timer);
erase_timer.function = erase_state_poll;
erase_timer.data = ERASE_CHECK_PERIOD;
erase_timer.expires = jiffies + erase_timer.data;
add_timer(&erase_timer);
return;
}
/****************************************************/
/*函数名称:RecordDataBlockErase */
/*函数功能:Erase the record block */
/*输入参数: */
/* No: the Block Number */
/*输出参数: N/A */
/*修改记录: */
/****************************************************/
static void RecordDataBlockErase(unsigned short uBlockSeclect)
{
if (uBlockSeclect>1)
{
uBlockSeclect = 0;
}
unsigned char ucBlockWillErasing = tRecordManageParameterp[EVENT_RECORD].tUsedBlocks[uBlockSeclect].ucBlockNum;
//for test
printk ("--flash:will erase block= %d--\n", ucBlockWillErasing);
BlockErase(ucBlockWillErasing);
return;
}
/****************************************************/
/*函数名称:EraseSuspend */
/*函数功能:Erase process is pending */
/*输入参数: N/A */
/*输出参数: N/A */
/*修改记录: */
/****************************************************/
static void EraseSuspend(void)
{
unsigned char ret;
if ((ucFlashStatus == BLOCKERASING) || (ucFlashStatus == CHIPERASING))
{
*(data_flash_base + ADDR_UNLOCK_1) = CMD_ERASE_SUSPEND;
ucFlashStatus = ERASEPENDING; /*Register a erase pending status*/
/*停止计数检查*/
del_timer_sync(&erase_timer);
}
else
{
PRINT_TRACE(Debug)("Flash is not in erase process\n");
}
return;
}
/****************************************************/
/*函数名称:EraseResume */
/*函数功能:Erase process to be continued */
/*输入参数: N/A */
/*输出参数: N/A */
/*修改记录: */
/****************************************************/
static void EraseResume(void)
{
unsigned char ret;
if (ucFlashStatus == ERASEPENDING)
{
*(data_flash_base + ADDR_UNLOCK_1) = CMD_ERASE_RESUME;
ucFlashStatus = BLOCKERASING; /*Register a block erase status*/
/*继续计数检查*/
init_timer(&erase_timer);
erase_timer.function = erase_state_poll;
erase_timer.data = ERASE_CHECK_PERIOD;
erase_timer.expires = jiffies + erase_timer.data;
add_timer(&erase_timer);
}
else
{
PRINT_TRACE(Debug)("Flash is not in erase pending process\n");
}
return;
}
/****************************************************/
/*函数名称:ReadByte */
/*函数功能:Read a bytes from Flash */
/*输入参数: */
/* addr: start address of flash */
/*输出参数: */
/* value: result of this byte */
/*修改记录: */
/****************************************************/
static unsigned char ReadByte(unsigned int addr)
{
if ((addr & 0x01) == 0x01)
{
return ((*(data_flash_base + (addr >> 1))) >> 8) ;
}
else
{
return ((*(data_flash_base + (addr >> 1))) & 0xFF) ;
}
}
/****************************************************/
/*函数名称:ReadWord */
/*函数功能:Read a short int data from Flash */
/*输入参数: */
/* addr: the offset of flash addr */
/* per a short int type */
/*输出参数: */
/* value: result of this short int */
/*修改记录: */
/****************************************************/
static inline unsigned short ReadWord(unsigned int addr)
{
return *(data_flash_base + addr);
}
/****************************************************/
/*函数名称:WriteWord */
/*函数功能:Write a short int data from Flash */
/*输入参数: */
/* addr: the offset of flash addr */
/* per a short int type */
/* data: the data to be writen */
/*输出参数: */
/* result: successful or fail */
/*修改记录: */
/****************************************************/
static unsigned char WriteWord(unsigned int addr, unsigned short data)
{
unsigned short status1, status2;
unsigned char i, Maxtimes, TypeT, result;
// unsigned char ucFlashStatusBak;
// printk("The addr is %d , data is %d\n", addr, data);
// ucFlashStatusBak = ucFlashStatus;
// ucFlashStatus = PROGRAMING; /*Register a programming status*/
/*发送编程命令*/
*(data_flash_base + ADDR_UNLOCK_1) = CMD_UNLOCK_DATA_1;
*(data_flash_base + ADDR_UNLOCK_2) = CMD_UNLOCK_DATA_2;
*(data_flash_base + ADDR_UNLOCK_1) = CMD_PROGRAM_UNLOCK_DATA;
*(data_flash_base + addr) = data;
/*算出典型的编程时间以及最大等待循环*/
TypeT = uiPower(2,CFIsys.TyProgram);
Maxtimes = uiPower(2,CFIsys.MaxProgrom);
udelay(TypeT);