summaryrefslogtreecommitdiff
path: root/Board/Flash/FlashWrite.c
blob: b77f971ff7f72bc96cc5a3b8bf50f1da6afa8cd5 (plain)
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
//*************************************************************************
//*************************************************************************
//**                                                                     **
//**        (C)Copyright 1985-2009, American Megatrends, Inc.            **
//**                                                                     **
//**                       All Rights Reserved.                          **
//**                                                                     **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093           **
//**                                                                     **
//**                       Phone: (770)-246-8600                         **
//**                                                                     **
//*************************************************************************
//*************************************************************************

//**********************************************************************
// $Header: /Alaska/SOURCE/Flash_Combined_2/Core/FlashWrite.c 23    3/29/12 6:02a Klzhan $
//
// $Revision: 23 $
//
// $Date: 3/29/12 6:02a $
//**********************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Flash_Combined_2/Core/FlashWrite.c $
// 
// 23    3/29/12 6:02a Klzhan
// [TAG]  		EIP86044
// [Category]  	Improvement
// [Description]  	customized AFUWIN to support a parameter to display the
// current SPI part's name and Size
// 
// 22    7/21/11 5:09a Calvinchen
// [TAG]  		EIP60956
// [Category]  	Improvement
// [Description]  	Added "OemFlashValidTableList" eLink for OEM/ODM
// invalidates the flash part supports those they don't needed.
// [Files]  		Flash.sdl
// Flash.mak
// FlashWrite.c
// Flash.chm
// 
// 21    10/25/10 5:56a Calvinchen
// Added support for PEI Flash Library.
// 
// 20    8/06/10 12:04a Calvinchen
// Changed the priority for oem hook "OemFlashDeviceWriteEnable" in
// FlashDeviceWriteEnable procedure. 
// 
// 19    3/11/10 6:44a Calvinchen
// Improvement : 
// 1.With Core 4.6.3.7 or later, the FlashVirtualFixup function gets
// called from Nvramdxe.c and flash.c, do anything when the function is
// called second time.
// 2.Added SST AAIWordProgram SPI command to speed up the programming
// time.
// 
// 18    12/23/09 6:13a Calvinchen
// Improvement:
// 1.Added FWH/LPC/STD Flash Device Number support.
// 2.Changed for SMIFlash module Label "4.6.3.6_SMIFLASH_12" or later
// 
// 17    12/15/09 5:46a Calvinchen
// Improvement : Added Flash Chip Name support.
// 
// 16    6/24/09 3:11a Calvinchen
// (EIP22177) Updated for Aptio Source Enhancement. 
//
// 14    6/26/08 6:58a Calvinchen
// Added convert FlashBlockSize for virtual mode.
//
// 12    1/07/08 2:32p Robert
// updated to doing standard
//
// 11    11/09/07 3:52p Alexp
// New CommonFlashRead function.
//
// 10    4/09/07 11:36a Robert
//
// 9     3/26/07 7:21p Robert
// Coding standard updates
//
// 7     3/12/07 4:33p Robert
// Added porting hooks in the FlashDeviceWriteEnable() and
// FlashDeviceWriteDisable() functions
//
//**********************************************************************
//<AMI_FHDR_START>
//
// Name:    FlashWrite.c
//
// Description: Flash update routines
//
//<AMI_FHDR_END>
//**********************************************************************

//----------------------------------------------------------------------
// Includes
#include <EFI.h>
#include <Token.h>
#include <Flash.h>
#include <FlashPart.h>
#if defined _DXE_FLASH_LIB_
#include <AmiDxeLib.h>
#endif
//----------------------------------------------------------------------
// component MACROs


//----------------------------------------------------------------------
// Module defined global variables
FLASH_PART      *FlashAPI = NULL;

// flash part list creation code must be in this order
typedef BOOLEAN (IDENTIFY)(
    volatile UINT8*     pBlockAddress,
    FLASH_PART          **Struct
    );

extern IDENTIFY FLASH_LIST EndOfInitList;

IDENTIFY* FlashList[] = {FLASH_LIST NULL};

// oem flash write enable/disable list creation code must be in this order
typedef VOID (OEM_FLASH_WRITE) (VOID);
extern OEM_FLASH_WRITE OEM_FLASH_WRITE_ENABLE_LIST EndOfOemFlashList;
extern OEM_FLASH_WRITE OEM_FLASH_WRITE_DISABLE_LIST EndOfOemFlashList;
OEM_FLASH_WRITE* OemFlashWriteEnable[] = {OEM_FLASH_WRITE_ENABLE_LIST NULL};
OEM_FLASH_WRITE* OemFlashWriteDisable[] = {OEM_FLASH_WRITE_DISABLE_LIST NULL};
UINT16  OemFlashValidTable[] = {OEM_FLASH_VALID_TABLE_LIST NULL};
// END flash part list creation code
//----------------------------------------------------------------------
// Module specific global variable
UINT16  gFlashId = 0;
UINT8   gbDeviceWriteState = FALSE;
UINT8   pFlashDeviceNumber[FLASH_PART_STRING_LENGTH];

//----------------------------------------------------------------------
// externally defined variables
extern VOID ChipsetFlashDeviceWriteEnable();
extern VOID ChipsetFlashDeviceWriteDisable();

//----------------------------------------------------------------------
// Function definitions
VOID OemFlashDeviceWriteEnable ( VOID );
VOID OemFlashDeviceWriteDisable ( VOID );

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   OemIsFlashValid
//
// Description: This function identifies the supported SPI flash parts and
//              returns TRUE. If flash part is not supported by this
//              module it will return FALSE.
//
// Input:       dFlashId    Present Flash Part ID
//
// Output:      None
//
// Return:      TRUE            Flash part is Valid.
//              FALSE           Flash part is not Valid.
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
BOOLEAN 
OemIsFlashValid(VOID)
{
    UINTN           i = 0;
    if (OemFlashValidTable[0] == NULL) return TRUE;
    for (i = 0; OemFlashValidTable[i] != NULL; i++) {
        if (gFlashId == OemFlashValidTable[i]) return TRUE;
    }
    return FALSE;
}
//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Procedure:   FlashInit
//
// Description: This function goes through the elinked list of identify
//      functions giving control until one part is identified properly
//
// Input:   *pBlockAddress - Address to access the flash part
//
// Output: None
//
// Returns: EFI_SUCCESS
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS
FlashInit   (
    IN volatile UINT8*      pBlockAddress
)
{
    UINTN i;
    BOOLEAN found = FALSE;

    for(i=0; !found && FlashList[i]; i++)
    {
        found=FlashList[i](pBlockAddress, &FlashAPI);
    }
    if (found && !OemIsFlashValid()) FlashAPI = NULL;
    return EFI_SUCCESS;
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Procedure:   FlashEraseCommand
//
// Description: This function Chooses the correct flash part to call and then
//      initiates flash block erase command
//
// Input:   *pBlockAddress - Address to access flash part
//
// Output:  None
//
// Returns: None
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>
VOID
FlashEraseCommand   (
    IN volatile UINT8*      pBlockAddress
)
{
    if ( FlashAPI == NULL)
        FlashInit(pBlockAddress);

    // if FlashAPI is still NULL return
    if ( FlashAPI == NULL)
        return;

    FlashAPI->FlashEraseCommand(pBlockAddress);

}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Procedure:   FlashIsEraseCompleted
//
// Description: This function chooses the correct flash part to call and
//      then checks current status of the last erase operation
//
// Input:   *pBlockAddress - Address to access flash part
//          *pError - Boolean that tells if fatal error occured
//          *pStatus - Status of the erase command
//
// Output:  *pError - Boolean that tells if fatal error occured
//          *pStatus - Status of the erase command
//
// Returns: TRUE - erase complete
//          FALSE - erase not completed
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>
BOOLEAN
FlashIsEraseCompleted   (
    IN volatile UINT8     *pBlockAddress,
    OUT         BOOLEAN   *pError,
    OUT         UINTN     *pStatus)
{
    BOOLEAN Temp;

    if ( FlashAPI == NULL)
        FlashInit(pBlockAddress);

    Temp = FlashAPI->FlashIsEraseCompleted(pBlockAddress, pError, pStatus);

    return Temp;
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Procedure:   FlashProgramCommand
//
// Description: This function chooses the correct flash part to call and
//      then initiates the program command
//
// Input:   *pBlockAddress - Address to access flash part
//          *Byte - pointer to data to write to the flash part
//          Length - The total amount of data that Byte points to
//
// Output:  None
//
// Return:  None
//----------------------------------------------------------------------
//<AMI_PHDR_END>
VOID
FlashProgramCommand (
    IN volatile UINT8*      pByteAddress,
    IN UINT8                *Byte,
    IN UINT32               Length
)
{
    volatile UINT8 *a=(volatile UINT8 *) pByteAddress;
    UINT32  TempLength = Length;
    UINT8   *DataPtr = Byte;

    if ( FlashAPI == NULL)
        FlashInit((volatile UINT8*)BLOCK(pByteAddress));

    // if FlashAPI is still NULL return
    if ( FlashAPI == NULL)
        return;

    do {
        FlashAPI->FlashProgramCommand(a, DataPtr, &TempLength);
        a += (Length - TempLength);
        DataPtr += (Length - TempLength);
        Length = TempLength;
    } while (TempLength != 0);
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Procedure:   FlashReadCommand
//
// Description: This function chooses the correct flash part to call and
//      then initiates the program command
//
// Input:   *pBlockAddress - Address to access flash part
//          *Byte - pointer to data to write to the flash part
//          Length - The total amount of data that Byte points to
//
// Output:  None
//
// Return:  None
//----------------------------------------------------------------------
//<AMI_PHDR_END>
VOID
FlashReadCommand    (
    IN  volatile UINT8*     pByteAddress,
    OUT UINT8               *Byte,
    IN  UINT32              Length
)
{
    volatile UINT8 *a=(volatile UINT8 *) pByteAddress;
    UINT32  TempLength = Length;
    UINT8   *DataPtr = Byte;

    if ( FlashAPI == NULL)
        FlashInit((volatile UINT8*)BLOCK(pByteAddress));

    // if FlashAPI is still NULL return
    if ( FlashAPI == NULL)
        return;

    do {
        FlashAPI->FlashReadCommand(a, DataPtr, &TempLength);
        a += (Length - TempLength);
        DataPtr += (Length - TempLength);
        Length = TempLength;
    } while (TempLength != 0);
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Procedure:   FlashIsProgramCompleted
//
// Description: This function chooses the correct flash part to call
//      and then checks current status of the last program operation
//
// Input:   *pBlockAddress - Address to access flash part
//          *Byte - values previously written to the Flash Device
//          Length - The amount of data that needs to be checked
//          *pError - Boolean that tells if fatal error occured
//          *pStatus - Status of the erase command
//
// Output: *pError - Boolean that tells if fatal error occured
//         *pStatus - Status of the erase command
//
// Return:  TRUE - Program completed, check pError for fatal error
//          FALSE - programming in progress
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>
BOOLEAN
FlashIsProgramCompleted (
    IN  volatile UINT8*     pByteAddress,
    IN  UINT8               *Byte,
    IN  UINT32              Length,
    OUT BOOLEAN             *pError,
    OUT UINTN               *pStatus
)
{
//FlashProgramCommand implementation already checks the status
//So this function is no longer needed
//Just return success
    if (pError) *pError = FALSE;
    return TRUE;
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Procedure:   FlashBlockWriteEnable
//
// Description: This function chooses the correct flash part to call and
//      then enables write operations(erase/programming) for a specific block
//
// Input:   *pBlockAddress - Address to access flash part
//
// Output:  None
//
// Return:  None
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>
VOID
FlashBlockWriteEnable   (
    IN UINT8*               pBlockAddress
)
{
    if ( FlashAPI == NULL)
        FlashInit(pBlockAddress);

    // if FlashAPI is still NULL return
    if ( FlashAPI == NULL)
        return;

    FlashAPI->FlashBlockWriteEnable(pBlockAddress);
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Procedure:   FlashBlockWriteDisable
//
// Description: This function chooses the correct flash part to call and
//      then disables write operations(erase/programming) for a specific
//      block
//
// Input:   *pBlockAddress - Address to access flash part
//
// Output:  None
//
// Return:  None
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>
VOID
FlashBlockWriteDisable  (
    IN UINT8*               pBlockAddress
)
{
    if ( FlashAPI == NULL)
        FlashInit(pBlockAddress);

    // if FlashAPI is still NULL return
    if ( FlashAPI == NULL)
        return;

    FlashAPI->FlashBlockWriteDisable(pBlockAddress);

}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Procedure:   FlashDeviceWriteEnable
//
// Description: This function chooses the correct flash part to call and
//      then enables write operation for a flash device
//
// Input:   None
//
// Output:  None
//
// Return:  None
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>
VOID
FlashDeviceWriteEnable  (VOID)
{
    OemFlashDeviceWriteEnable ();

    ChipsetFlashDeviceWriteEnable();

    if ( FlashAPI == NULL)
        FlashInit((UINT8 *)FlashDeviceBase);

    // if FlashAPI is still NULL return
    if ( FlashAPI == NULL)
        return;

    FlashAPI->FlashDeviceWriteEnable();

}

//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Procedure:   FlashDeviceWriteDisable
//
// Description: This function chooses the correct flash part to call and
//      then disables write operation for a flash device
//
// Input:   None
//
// Output:  None
//
// Return:  None
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>
VOID
FlashDeviceWriteDisable (VOID)
{
    ChipsetFlashDeviceWriteDisable();

    if ( FlashAPI == NULL)
        FlashInit((UINT8 *)FlashDeviceBase);

    // if FlashAPI is still NULL return
    if ( FlashAPI == NULL)
        return;

    FlashAPI->FlashDeviceWriteDisable();

    OemFlashDeviceWriteDisable ();
}
//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Procedure:   OemFlashDeviceWriteEnable
//
// Description: This function goes through the elinked list of oem flash
//     write enable functions giving control.
//
// Input:   None
//
// Output:  None
//
// Return:  None
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>
VOID
OemFlashDeviceWriteEnable (VOID)
{
    UINT8   i;

    if ( gbDeviceWriteState != TRUE ) {
        gbDeviceWriteState = TRUE;
        for(i = 0; OemFlashWriteEnable[i] != NULL; i++) 
            OemFlashWriteEnable[i]();
    }
}
//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Procedure:   OemFlashDeviceWriteDisable
//
// Description: This function goes through the elinked list of oem flash
//     write disable functions giving control.
//
// Input:   None
//
// Output:  None
//
// Return:  None
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>
VOID
OemFlashDeviceWriteDisable (VOID)
{
    UINT8   i;

    if ( gbDeviceWriteState != FALSE ) {
        gbDeviceWriteState = FALSE;
        for(i = 0; OemFlashWriteDisable[i] != NULL; i++) 
            OemFlashWriteDisable[i]();
    }
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Procedure:   FlashVirtualFixup
//
// Description: Fixup global data for for a virtual address space.
//      This routine must be called by the library consumer in the
//      EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event handler
//
// Input:   EFI_RUNTIME_SERVICES *pRS - pointer to the Runtime Services Table
//
// Output:  None
//
// Return:  None
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>
VOID
FlashVirtualFixup   (
    IN EFI_RUNTIME_SERVICES *pRS
)
{
    static BOOLEAN  bFlashVirtual = FALSE;
    VOID            **p;
    UINT8           i;

    //if FlashAPI is NULL, nothing to fix up
    //TODO: Current implementation of Identify routines
    //will not work in virtual mode since absolute addresses are used
    //That's Ok for now since current flash library consumers
    //call flash routines at boot time, so we should have FlashAPI
    //initialized at this point
    if (!FlashAPI) return;
    
    // This function gets called from Nvramdxe.c and flash.c, do anything when 
    // the function is called second time.
    if (bFlashVirtual == TRUE) return;
    else bFlashVirtual = TRUE;

    //Do device specific fixups
    FlashAPI->FlashVirtualFixup(pRS);

    //Fixup FlashAPI member functions
    for(p = (VOID**)FlashAPI; p < (VOID**)(FlashAPI + 1); p++)
        pRS->ConvertPointer(0, p);

    //Fixup FlashAPI pointer
    pRS->ConvertPointer(0, &FlashAPI);

    //convert FeatureSpaceBase address
    pRS->ConvertPointer(0, (VOID**)&FwhFeatureSpaceBase);

    //convert FlashDeviceBase address
    pRS->ConvertPointer(0, (VOID**)&FlashDeviceBase);

    //convert FlashBlockSize address
    pRS->ConvertPointer(0, (VOID**)&FlashBlockSize);

    //Fixup OemFlashWriteEnable list
    for(i = 0; OemFlashWriteEnable[i] != NULL; i++)
        pRS->ConvertPointer(0, (VOID**)&OemFlashWriteEnable[i]);

    //Fixup OemFlashWriteDisable list
    for(i = 0; OemFlashWriteDisable[i] != NULL; i++)
        pRS->ConvertPointer(0, (VOID**)&OemFlashWriteDisable[i]);
}

#if defined _DXE_FLASH_LIB_
//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Procedure:   GetFlashPartInfomation
//
// Description: 
//
// Input:   *pBlockAddress - Address to access the flash part
//          *Buffer - Buffer to write the flash part number string.
//
// Output: None
//
// Returns: EFI_SUCCESS
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>
VOID
GetFlashPartInfomation  (
    IN  UINT8*          pBlockAddress,
    IN  UINT8*          Buffer
)
{
    UINT8           bFlashStringSign[4] = {'$','F','P','S'};
    UINT32          dFlashCapacity;
    
    if ( FlashAPI == NULL) {
        MemSet( &pFlashDeviceNumber, FLASH_PART_STRING_LENGTH, 0 );
        FlashInit(pBlockAddress);
    }        
    if ((FlashAPI != NULL) && ( FlashAPI->FlashPartNumber != NULL )) {
        MemCpy ( Buffer, bFlashStringSign, 4 );
        Buffer += sizeof (UINT32);
        *(UINT32*)Buffer = FLASH_PART_STRING_LENGTH;
        dFlashCapacity = *(UINT32*)(FlashAPI->FlashPartNumber + \
                                            FLASH_PART_STRING_LENGTH - 4);
                                            
        if ((dFlashCapacity != 0) && !(dFlashCapacity % FlashAPI->FlashSectorSize))
            *(UINT32*)Buffer = FLASH_PART_STRING_LENGTH + (dFlashCapacity >> 4);
        Buffer += sizeof (UINT32);
        MemCpy ( Buffer, FlashAPI->FlashPartNumber, FLASH_PART_STRING_LENGTH );
    }
}    
#endif  // #ifdef _DXE_FLASHLIB_
//*************************************************************************
//*************************************************************************
//**                                                                     **
//**        (C)Copyright 1985-2009, American Megatrends, Inc.            **
//**                                                                     **
//**                       All Rights Reserved.                          **
//**                                                                     **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093           **
//**                                                                     **
//**                       Phone: (770)-246-8600                         **
//**                                                                     **
//*************************************************************************
//*************************************************************************