OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [devs/] [flash/] [intel/] [28fxxx/] [current/] [include/] [flash_28fxxx.inl] - Blame information for rev 857

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
#ifndef CYGONCE_DEVS_FLASH_INTEL_28FXXX_INL
2
#define CYGONCE_DEVS_FLASH_INTEL_28FXXX_INL
3
//==========================================================================
4
//
5
//      flash_28fxxx.inl
6
//
7
//      Intel 28Fxxx series flash driver
8
//
9
//==========================================================================
10
// ####ECOSGPLCOPYRIGHTBEGIN####
11
// -------------------------------------------
12
// This file is part of eCos, the Embedded Configurable Operating System.
13
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
14
//
15
// eCos is free software; you can redistribute it and/or modify it under
16
// the terms of the GNU General Public License as published by the Free
17
// Software Foundation; either version 2 or (at your option) any later
18
// version.
19
//
20
// eCos is distributed in the hope that it will be useful, but WITHOUT
21
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
23
// for more details.
24
//
25
// You should have received a copy of the GNU General Public License
26
// along with eCos; if not, write to the Free Software Foundation, Inc.,
27
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
28
//
29
// As a special exception, if other files instantiate templates or use
30
// macros or inline functions from this file, or you compile this file
31
// and link it with other works to produce a work based on this file,
32
// this file does not by itself cause the resulting work to be covered by
33
// the GNU General Public License. However the source code for this file
34
// must still be made available in accordance with section (3) of the GNU
35
// General Public License v2.
36
//
37
// This exception does not invalidate any other reasons why a work based
38
// on this file might be covered by the GNU General Public License.
39
// -------------------------------------------
40
// ####ECOSGPLCOPYRIGHTEND####
41
//==========================================================================
42
//#####DESCRIPTIONBEGIN####
43
//
44
// Author(s):    jskov
45
// Contributors: jskov
46
// Date:         2001-03-21
47
// Purpose:
48
// Description:
49
//
50
// Notes:        Device table could use unions of flags to save some space
51
//
52
//####DESCRIPTIONEND####
53
//
54
//==========================================================================
55
 
56
#include 
57
#include 
58
#include 
59
 
60
#include 
61
#include 
62
#include CYGHWR_MEMORY_LAYOUT_H
63
 
64
#include 
65
 
66
#define  _FLASH_PRIVATE_
67
#include 
68
 
69
#define nDEBUG
70
 
71
#ifdef DEBUG
72
typedef void (*call_t)(char* str, ...);
73
extern void diag_printf(char* str, ...);
74
call_t d_print = &diag_printf;
75
#endif
76
 
77
//----------------------------------------------------------------------------
78
// Common device details.
79
#define FLASH_Read_ID                   FLASHWORD( 0x90 )
80
#define FLASH_Reset                     FLASHWORD( 0xFF )
81
#define FLASH_Program                   FLASHWORD( 0x40 )
82
#define FLASH_Write_Buffer              FLASHWORD( 0xe8 )
83
#define FLASH_Block_Erase               FLASHWORD( 0x20 )
84
#define FLASH_Confirm                   FLASHWORD( 0xD0 )
85
#define FLASH_Resume                    FLASHWORD( 0xD0 )
86
 
87
#define FLASH_Set_Lock                  FLASHWORD( 0x60 )
88
#define FLASH_Set_Lock_Confirm          FLASHWORD( 0x01 )
89
#define FLASH_Clear_Lock                FLASHWORD( 0x60 )
90
#define FLASH_Clear_Lock_Confirm        FLASHWORD( 0xd0 )
91
 
92
#define FLASH_Read_Status               FLASHWORD( 0x70 )
93
#define FLASH_Clear_Status              FLASHWORD( 0x50 )
94
#define FLASH_Status_Ready              FLASHWORD( 0x80 )
95
 
96
// Status that we read back:
97
#define FLASH_ErrorMask                 FLASHWORD( 0x7E )
98
#define FLASH_ErrorProgram              FLASHWORD( 0x10 )
99
#define FLASH_ErrorErase                FLASHWORD( 0x20 )
100
#define FLASH_ErrorLock                 FLASHWORD( 0x30 )
101
#define FLASH_ErrorLowVoltage           FLASHWORD( 0x08 )
102
#define FLASH_ErrorLocked               FLASHWORD( 0x02 )
103
 
104
// Platform code must define the below
105
// #define CYGNUM_FLASH_INTERLEAVE      : Number of interleaved devices (in parallel)
106
// #define CYGNUM_FLASH_SERIES          : Number of devices in series
107
// #define CYGNUM_FLASH_WIDTH           : Width of devices on platform
108
// #define CYGNUM_FLASH_BASE            : Address of first device
109
 
110
#define CYGNUM_FLASH_BLANK              (1)
111
#define CYGNUM_FLASH_DEVICES            (CYGNUM_FLASH_INTERLEAVE*CYGNUM_FLASH_SERIES)
112
 
113
 
114
#ifndef FLASH_P2V
115
# define FLASH_P2V( _a_ ) ((volatile flash_data_t *)((CYG_ADDRWORD)(_a_)))
116
#endif
117
#ifndef CYGHWR_FLASH_28FXXX_PLF_INIT
118
# define CYGHWR_FLASH_28FXXX_PLF_INIT()
119
#endif
120
#ifndef CYGHWR_FLASH_WRITE_ENABLE
121
#define CYGHWR_FLASH_WRITE_ENABLE()
122
#endif
123
#ifndef CYGHWR_FLASH_WRITE_DISABLE
124
#define CYGHWR_FLASH_WRITE_DISABLE()
125
#endif
126
 
127
//----------------------------------------------------------------------------
128
// Now that device properties are defined, include magic for defining
129
// accessor type and constants.
130
#include 
131
 
132
//----------------------------------------------------------------------------
133
// Information about supported devices
134
typedef struct flash_dev_info {
135
    flash_data_t device_id;
136
    cyg_uint32   block_size;
137
    cyg_int32    block_count;
138
    cyg_uint32   base_mask;
139
    cyg_uint32   device_size;
140
    cyg_bool     locking;               // supports locking
141
    cyg_bool     buffered_w;            // supports buffered writes
142
    cyg_uint32   buffer_size;
143
    cyg_bool     bootblock;
144
    cyg_uint32   bootblocks[12];         // 0 is bootblock offset, 1-11 sub-sector sizes (or 0)
145
    cyg_bool     banked;
146
    cyg_uint32   banks[2];               // bank offets, highest to lowest (lowest should be 0)
147
                                         // (only one entry for now, increase to support devices
148
                                         // with more banks).
149
} flash_dev_info_t;
150
 
151
static const flash_dev_info_t* flash_dev_info;
152
static const flash_dev_info_t supported_devices[] = {
153
#include 
154
};
155
#define NUM_DEVICES (sizeof(supported_devices)/sizeof(flash_dev_info_t))
156
 
157
//----------------------------------------------------------------------------
158
// Functions that put the flash device into non-read mode must reside
159
// in RAM.
160
void flash_query(void* data) __attribute__ ((section (".2ram.flash_query")));
161
int  flash_erase_block(void* block, unsigned int size)
162
    __attribute__ ((section (".2ram.flash_erase_block")));
163
int  flash_program_buf(void* addr, void* data, int len,
164
                       unsigned long block_mask, int buffer_size)
165
    __attribute__ ((section (".2ram.flash_program_buf")));
166
int  flash_lock_block(void* addr)
167
    __attribute__ ((section (".2ram.flash_lock_block")));
168
int flash_unlock_block(void* block, int block_size, int blocks)
169
    __attribute__ ((section (".2ram.flash_unlock_block")));
170
 
171
//----------------------------------------------------------------------------
172
// Initialize driver details
173
int
174
flash_hwr_init(void)
175
{
176
    int i;
177
    flash_data_t id[2];
178
 
179
    CYGHWR_FLASH_28FXXX_PLF_INIT();
180
 
181
    flash_dev_query(id);
182
 
183
    // Look through table for device data
184
    flash_dev_info = supported_devices;
185
    for (i = 0; i < NUM_DEVICES; i++) {
186
        if (flash_dev_info->device_id == id[1])
187
            break;
188
        flash_dev_info++;
189
    }
190
 
191
    // Did we find the device? If not, return error.
192
    if (NUM_DEVICES == i)
193
        return FLASH_ERR_DRV_WRONG_PART;
194
 
195
    // Hard wired for now
196
    flash_info.block_size = flash_dev_info->block_size;
197
    flash_info.blocks = flash_dev_info->block_count * CYGNUM_FLASH_SERIES;
198
    flash_info.start = (void *)CYGNUM_FLASH_BASE;
199
    flash_info.end = (void *)(CYGNUM_FLASH_BASE+ (flash_dev_info->device_size * CYGNUM_FLASH_SERIES));
200
    flash_info.buffer_size = flash_dev_info->buffer_size;
201
 
202
    return FLASH_ERR_OK;
203
}
204
 
205
//----------------------------------------------------------------------------
206
// Map a hardware status to a package error
207
int
208
flash_hwr_map_error(int e)
209
{
210
    return e;
211
}
212
 
213
 
214
//----------------------------------------------------------------------------
215
// See if a range of FLASH addresses overlaps currently running code
216
bool
217
flash_code_overlaps(void *start, void *end)
218
{
219
    extern unsigned char _stext[], _etext[];
220
 
221
    return ((((unsigned long)&_stext >= (unsigned long)start) &&
222
             ((unsigned long)&_stext < (unsigned long)end)) ||
223
            (((unsigned long)&_etext >= (unsigned long)start) &&
224
             ((unsigned long)&_etext < (unsigned long)end)));
225
}
226
 
227
//----------------------------------------------------------------------------
228
// Flash Query
229
//
230
// Only reads the manufacturer and part number codes for the first
231
// device(s) in series. It is assumed that any devices in series
232
// will be of the same type.
233
 
234
void
235
flash_query(void* data)
236
{
237
    volatile flash_data_t *ROM;
238
    flash_data_t* id = (flash_data_t*) data;
239
    flash_data_t w;
240
 
241
    ROM = (volatile flash_data_t*) CYGNUM_FLASH_BASE;
242
 
243
    w = ROM[0];
244
 
245
    CYGHWR_FLASH_WRITE_ENABLE();
246
 
247
    ROM[0] = FLASH_Read_ID;
248
 
249
    // Manufacturers' code
250
    id[0] = ROM[0];
251
    // Part number
252
    id[1] = ROM[1];
253
 
254
    ROM[0] = FLASH_Reset;
255
 
256
    CYGHWR_FLASH_WRITE_DISABLE();
257
 
258
    // Stall, waiting for flash to return to read mode.
259
    while (w != ROM[0]);
260
}
261
 
262
//----------------------------------------------------------------------------
263
// Erase Block
264
int
265
flash_erase_block(void* block, unsigned int block_size)
266
{
267
    int res = FLASH_ERR_OK;
268
    int timeout;
269
    unsigned long len;
270
    int len_ix = 1;
271
    flash_data_t stat;
272
    volatile flash_data_t *ROM;
273
    volatile flash_data_t *b_p = (flash_data_t*) block;
274
    volatile flash_data_t *b_v;
275
    cyg_bool bootblock;
276
 
277
    ROM = FLASH_P2V((unsigned long)block & flash_dev_info->base_mask);
278
 
279
    // Is this the boot sector?
280
    bootblock = (flash_dev_info->bootblock &&
281
                 (flash_dev_info->bootblocks[0] == ((unsigned long)block - (unsigned long)ROM)));
282
    if (bootblock) {
283
        len = flash_dev_info->bootblocks[len_ix++];
284
    } else {
285
        len = flash_dev_info->block_size;
286
    }
287
 
288
    CYGHWR_FLASH_WRITE_ENABLE();
289
 
290
    while (len > 0) {
291
        b_v = FLASH_P2V(b_p);
292
 
293
        // Clear any error conditions
294
        ROM[0] = FLASH_Clear_Status;
295
 
296
        // Erase block
297
        ROM[0] = FLASH_Block_Erase;
298
        *b_v = FLASH_Confirm;
299
 
300
        timeout = CYGNUM_DEVS_FLASH_INTEL_28FXXX_TIMEOUT ;
301
        while(((stat = ROM[0]) & FLASH_Status_Ready) != FLASH_Status_Ready) {
302
            if (--timeout == 0) break;
303
        }
304
 
305
        // Restore ROM to "normal" mode
306
        ROM[0] = FLASH_Reset;
307
 
308
        if (stat & FLASH_ErrorMask) {
309
            if (!(stat & FLASH_ErrorErase)) {
310
                res = FLASH_ERR_HWR;    // Unknown error
311
             } else {
312
                if (stat & FLASH_ErrorLowVoltage)
313
                    res = FLASH_ERR_LOW_VOLTAGE;
314
                else if (stat & FLASH_ErrorLocked)
315
                    res = FLASH_ERR_PROTECT;
316
                else
317
                    res = FLASH_ERR_ERASE;
318
            }
319
        }
320
 
321
        // Check if block got erased
322
        while (len > 0) {
323
            b_v = FLASH_P2V(b_p++);
324
            if (*b_v != FLASH_BlankValue ) {
325
                // Only update return value if erase operation was OK
326
                if (FLASH_ERR_OK == res) res = FLASH_ERR_DRV_VERIFY;
327
                return res;
328
            }
329
            len -= sizeof(*b_p);
330
        }
331
 
332
        if (bootblock)
333
            len = flash_dev_info->bootblocks[len_ix++];
334
    }
335
 
336
    CYGHWR_FLASH_WRITE_DISABLE();
337
 
338
    return res;
339
}
340
 
341
//----------------------------------------------------------------------------
342
// Program Buffer
343
int
344
flash_program_buf(void* addr, void* data, int len,
345
                  unsigned long block_mask, int buffer_size)
346
{
347
    flash_data_t stat = 0;
348
    int timeout;
349
 
350
    volatile flash_data_t* ROM;
351
    volatile flash_data_t* BA;
352
    volatile flash_data_t* addr_v;
353
    volatile flash_data_t* addr_p = (flash_data_t*) addr;
354
    volatile flash_data_t* data_p = (flash_data_t*) data;
355
 
356
    int res = FLASH_ERR_OK;
357
 
358
    // Base address of device(s) being programmed.
359
    ROM = FLASH_P2V((unsigned long)addr & flash_dev_info->base_mask);
360
    BA = FLASH_P2V((unsigned long)addr & ~(flash_dev_info->block_size - 1));
361
 
362
    CYGHWR_FLASH_WRITE_ENABLE();
363
 
364
    // Clear any error conditions
365
    ROM[0] = FLASH_Clear_Status;
366
 
367
#ifdef CYGHWR_DEVS_FLASH_INTEL_BUFFERED_WRITES
368
    // FIXME: This code has not been adjusted to handle bootblock
369
    // parts yet.
370
    // If the buffer size has not been initialised, buffered write will
371
    // be skipped.
372
 
373
     if (flash_dev_info->buffered_w && buffer_size) {
374
        int i, wc;
375
        // Write any big chunks first
376
        while (len >= buffer_size) {
377
            wc = buffer_size;
378
            if (wc > len) wc = len;
379
            len -= wc;
380
            wc = wc / ((CYGNUM_FLASH_WIDTH/8)*CYGNUM_FLASH_INTERLEAVE);  // Word count
381
            timeout = CYGNUM_DEVS_FLASH_INTEL_28FXXX_TIMEOUT;
382
 
383
            *BA = FLASH_Write_Buffer;
384
            while(((stat = ROM[0]) & FLASH_Status_Ready) != FLASH_Status_Ready) {
385
                if (--timeout == 0) {
386
                    res = FLASH_ERR_DRV_TIMEOUT;
387
                    goto bad;
388
                }
389
                *BA = FLASH_Write_Buffer;
390
            }
391
            *BA = FLASHWORD(wc-1);  // Count is 0..N-1
392
            for (i = 0; i < wc;  i++) {
393
                addr_v = FLASH_P2V(addr_p++);
394
                *addr_v = *data_p++;
395
            }
396
            *BA = FLASH_Confirm;
397
 
398
            ROM[0] = FLASH_Read_Status;
399
            timeout = CYGNUM_DEVS_FLASH_INTEL_28FXXX_TIMEOUT;
400
            while(((stat = ROM[0]) & FLASH_Status_Ready) != FLASH_Status_Ready) {
401
                if (--timeout == 0) {
402
                    res = FLASH_ERR_DRV_TIMEOUT;
403
                    goto bad;
404
                }
405
            }
406
        }
407
    }
408
#endif // CYGHWR_DEVS_FLASH_INTEL_BUFFERED_WRITES
409
 
410
    while (len > 0) {
411
        addr_v = FLASH_P2V(addr_p++);
412
        ROM[0] = FLASH_Program;
413
        *addr_v = *data_p;
414
        timeout = CYGNUM_DEVS_FLASH_INTEL_28FXXX_TIMEOUT;
415
        while(((stat = ROM[0]) & FLASH_Status_Ready) != FLASH_Status_Ready) {
416
            if (--timeout == 0) {
417
                res = FLASH_ERR_DRV_TIMEOUT;
418
                goto bad;
419
            }
420
        }
421
        if (stat & FLASH_ErrorMask) {
422
            if (!(stat & FLASH_ErrorProgram))
423
                res = FLASH_ERR_HWR;    // Unknown error
424
            else {
425
                if (stat & FLASH_ErrorLowVoltage)
426
                    res = FLASH_ERR_LOW_VOLTAGE;
427
                else if (stat & FLASH_ErrorLocked)
428
                    res = FLASH_ERR_PROTECT;
429
                else
430
                    res = FLASH_ERR_PROGRAM;
431
            }
432
            break;
433
        }
434
        ROM[0] = FLASH_Clear_Status;
435
        ROM[0] = FLASH_Reset;
436
        if (*addr_v != *data_p++) {
437
            res = FLASH_ERR_DRV_VERIFY;
438
            break;
439
        }
440
        len -= sizeof( flash_data_t );
441
    }
442
 
443
    // Restore ROM to "normal" mode
444
 bad:
445
    ROM[0] = FLASH_Reset;
446
 
447
    CYGHWR_FLASH_WRITE_DISABLE();
448
 
449
    // Ideally, we'd want to return not only the failure code, but also
450
    // the address/device that reported the error.
451
    return res;
452
}
453
 
454
#ifdef CYGHWR_IO_FLASH_BLOCK_LOCKING
455
//----------------------------------------------------------------------------
456
// Lock block
457
int
458
flash_lock_block(void* block)
459
{
460
    volatile flash_data_t *ROM;
461
    int res = FLASH_ERR_OK;
462
    flash_data_t state;
463
    int timeout = CYGNUM_DEVS_FLASH_INTEL_28FXXX_TIMEOUT;
464
    volatile flash_data_t* b_p = (flash_data_t*) block;
465
    volatile flash_data_t *b_v;
466
    cyg_bool bootblock;
467
    int len, len_ix = 1;
468
 
469
    if (!flash_dev_info->locking)
470
        return res;
471
 
472
#ifdef DEBUG
473
    d_print("flash_lock_block %08x\n", block);
474
#endif
475
 
476
    ROM = (volatile flash_data_t*)((unsigned long)block & flash_dev_info->base_mask);
477
 
478
    // Is this the boot sector?
479
    bootblock = (flash_dev_info->bootblock &&
480
                 (flash_dev_info->bootblocks[0] == ((unsigned long)block - (unsigned long)ROM)));
481
    if (bootblock) {
482
        len = flash_dev_info->bootblocks[len_ix++];
483
    } else {
484
        len = flash_dev_info->block_size;
485
    }
486
 
487
    CYGHWR_FLASH_WRITE_ENABLE();
488
 
489
    while (len > 0) {
490
        b_v = FLASH_P2V(b_p);
491
 
492
        // Clear any error conditions
493
        ROM[0] = FLASH_Clear_Status;
494
 
495
        // Set lock bit
496
        *b_v = FLASH_Set_Lock;
497
        *b_v = FLASH_Set_Lock_Confirm;  // Confirmation
498
        while(((state = ROM[0]) & FLASH_Status_Ready) != FLASH_Status_Ready) {
499
            if (--timeout == 0) {
500
                res = FLASH_ERR_DRV_TIMEOUT;
501
                break;
502
            }
503
        }
504
 
505
        // Restore ROM to "normal" mode
506
        ROM[0] = FLASH_Reset;
507
 
508
        // Go to next block
509
        b_p += len / sizeof( flash_data_t );
510
        len = 0;
511
 
512
        if (FLASH_ErrorLock == (state & FLASH_ErrorLock))
513
            res = FLASH_ERR_LOCK;
514
 
515
        if (res != FLASH_ERR_OK)
516
            break;
517
 
518
        if (bootblock)
519
            len = flash_dev_info->bootblocks[len_ix++];
520
    }
521
 
522
    CYGHWR_FLASH_WRITE_DISABLE();
523
 
524
    return res;
525
}
526
 
527
//----------------------------------------------------------------------------
528
// Unlock block
529
 
530
int
531
flash_unlock_block(void* block, int block_size, int blocks)
532
{
533
    volatile flash_data_t *ROM;
534
    int res = FLASH_ERR_OK;
535
    flash_data_t state;
536
    int timeout = CYGNUM_DEVS_FLASH_INTEL_28FXXX_TIMEOUT;
537
    volatile flash_data_t* b_p = (flash_data_t*) block;
538
    volatile flash_data_t *b_v;
539
 
540
#if (defined(CYGHWR_DEVS_FLASH_SHARP_LH28F016SCT_Z4) || defined(CYGHWR_DEVS_FLASH_SHARP_LH28F016SCT_95) )
541
    // The Sharp device follows all the same rules as the Intel 28x part,
542
    // except that the unlocking mechanism unlocks all blocks at once.  This
543
    // is the way the Strata part seems to work.  I will replace the
544
    // flash_unlock_block function with one similar to the Strata function.
545
    // As the Sharp part does not have the bootlock characteristics, I
546
    // will ignore them.
547
//
548
// The difficulty with this operation is that the hardware does not support
549
// unlocking single blocks.  However, the logical layer would like this to
550
// be the case, so this routine emulates it.  The hardware can clear all of
551
// the locks in the device at once.  This routine will use that approach and
552
// then reset the regions which are known to be locked.
553
//
554
 
555
#define MAX_FLASH_BLOCKS (flash_dev_info->block_count * CYGNUM_FLASH_SERIES)
556
 
557
    unsigned char is_locked[MAX_FLASH_BLOCKS];
558
    int i;
559
 
560
    // Get base address and map addresses to virtual addresses
561
#ifdef DEBUG
562
    d_print("\nNow inside low level driver\n");
563
#endif
564
    ROM = (volatile flash_data_t*) CYGNUM_FLASH_BASE;
565
    block = FLASH_P2V(block);
566
 
567
    // Clear any error conditions
568
    ROM[0] = FLASH_Clear_Status;
569
 
570
    // Get current block lock state.  This needs to access each block on
571
    // the device so currently locked blocks can be re-locked.
572
    b_p = ROM;
573
    for (i = 0;  i < blocks;  i++) {
574
        b_v = FLASH_P2V( b_p );
575
        *b_v = FLASH_Read_ID;
576
        if (b_v == block) {
577
            is_locked[i] = 0;
578
        } else {
579
            if(b_v[2]){ /* it is possible that one of the interleaved devices
580
                         * is locked, but others are not.  Coming out of this
581
                         * function, if one was locked, all will be locked.
582
                         */
583
                is_locked[i] = 1;
584
            }else{
585
                is_locked[i] = 0;
586
            }
587
        }
588
#ifdef DEBUG
589
#endif
590
        b_p += block_size / sizeof(*b_p);
591
    }
592
    ROM[0] = FLASH_Reset;
593
#ifdef DEBUG
594
    for (i = 0;  i < blocks;  i++) {
595
        d_print("\nblock %d  %s", i,
596
                is_locked[i] ? "LOCKED" : "UNLOCKED");
597
    }
598
    d_print("\n");
599
#endif
600
 
601
    // Clears all lock bits
602
    ROM[0] = FLASH_Clear_Lock;
603
    ROM[0] = FLASH_Clear_Lock_Confirm;  // Confirmation
604
    timeout = CYGNUM_DEVS_FLASH_INTEL_28FXXX_TIMEOUT;
605
    while(((state = ROM[0]) & FLASH_Status_Ready) != FLASH_Status_Ready) {
606
        if (--timeout == 0) break;
607
    }
608
 
609
    // Restore the lock state
610
    b_p = ROM;
611
    for (i = 0;  i < blocks;  i++) {
612
        b_v = FLASH_P2V( b_p );
613
        if (is_locked[i]) {
614
            *b_v = FLASH_Set_Lock;
615
            *b_v = FLASH_Set_Lock_Confirm;  // Confirmation
616
            timeout = CYGNUM_DEVS_FLASH_INTEL_28FXXX_TIMEOUT;
617
            while(((state = ROM[0]) & FLASH_Status_Ready)
618
                  != FLASH_Status_Ready) {
619
                if (--timeout == 0){
620
                    res = FLASH_ERR_DRV_TIMEOUT;
621
                    break;
622
                }
623
            }
624
            if (FLASH_ErrorLock == (state & FLASH_ErrorLock))
625
                res = FLASH_ERR_LOCK;
626
 
627
            if (res != FLASH_ERR_OK)
628
                break;
629
 
630
        }
631
        b_p += block_size / sizeof(*b_p);
632
    }
633
 
634
    // Restore ROM to "normal" mode
635
    ROM[0] = FLASH_Reset;
636
 
637
    return res;
638
 
639
#else // not CYGHWR_DEVS_FLASH_SHARP_LH28F016SCT_Z4
640
 
641
    cyg_bool bootblock;
642
    int len, len_ix = 1;
643
 
644
    if (!flash_dev_info->locking)
645
        return res;
646
 
647
    ROM = (volatile flash_data_t*)((unsigned long)block & flash_dev_info->base_mask);
648
 
649
#ifdef DEBUG
650
    d_print("flash_unlock_block dev %08x block %08x size %08x count %08x\n", ROM, block, block_size, blocks);
651
#endif
652
 
653
    // Is this the boot sector?
654
    bootblock = (flash_dev_info->bootblock &&
655
                 (flash_dev_info->bootblocks[0] == ((unsigned long)block - (unsigned long)ROM)));
656
    if (bootblock) {
657
        len = flash_dev_info->bootblocks[len_ix++];
658
    } else {
659
        len = flash_dev_info->block_size;
660
    }
661
 
662
    CYGHWR_FLASH_WRITE_ENABLE();
663
 
664
    while (len > 0) {
665
 
666
        b_v = FLASH_P2V(b_p);
667
 
668
        // Clear any error conditions
669
        ROM[0] = FLASH_Clear_Status;
670
 
671
        // Clear lock bit
672
        *b_v = FLASH_Clear_Lock;
673
        *b_v = FLASH_Clear_Lock_Confirm;  // Confirmation
674
        while(((state = ROM[0]) & FLASH_Status_Ready) != FLASH_Status_Ready) {
675
            if (--timeout == 0) {
676
                res = FLASH_ERR_DRV_TIMEOUT;
677
                break;
678
            }
679
        }
680
 
681
        // Restore ROM to "normal" mode
682
        ROM[0] = FLASH_Reset;
683
 
684
        // Go to next block
685
        b_p += len / sizeof( flash_data_t );
686
        len = 0;
687
 
688
        if (FLASH_ErrorLock == (state & FLASH_ErrorLock))
689
            res = FLASH_ERR_LOCK;
690
 
691
        if (res != FLASH_ERR_OK)
692
            break;
693
 
694
        if (bootblock)
695
            len = flash_dev_info->bootblocks[len_ix++];
696
    }
697
 
698
    CYGHWR_FLASH_WRITE_DISABLE();
699
 
700
    return res;
701
 
702
    // FIXME: Unlocking need to support some other parts in the future
703
    // as well which take a little more diddling.
704
#if 0
705
//
706
// The difficulty with this operation is that the hardware does not support
707
// unlocking single blocks.  However, the logical layer would like this to
708
// be the case, so this routine emulates it.  The hardware can clear all of
709
// the locks in the device at once.  This routine will use that approach and
710
// then reset the regions which are known to be locked.
711
//
712
 
713
#define MAX_FLASH_BLOCKS (flash_dev_info->block_count * CYGNUM_FLASH_SERIES)
714
 
715
    unsigned char is_locked[MAX_FLASH_BLOCKS];
716
 
717
    // Get base address and map addresses to virtual addresses
718
    ROM = FLASH_P2V( CYGNUM_FLASH_BASE_MASK & (unsigned int)block );
719
    block = FLASH_P2V(block);
720
 
721
    // Clear any error conditions
722
    ROM[0] = FLASH_Clear_Status;
723
 
724
    // Get current block lock state.  This needs to access each block on
725
    // the device so currently locked blocks can be re-locked.
726
    bp = ROM;
727
    for (i = 0;  i < blocks;  i++) {
728
        bpv = FLASH_P2V( bp );
729
        *bpv = FLASH_Read_Query;
730
        if (bpv == block) {
731
            is_locked[i] = 0;
732
        } else {
733
            is_locked[i] = bpv[2];
734
        }
735
        bp += block_size / sizeof(*bp);
736
    }
737
 
738
    // Clears all lock bits
739
    ROM[0] = FLASH_Clear_Locks;
740
    ROM[0] = FLASH_Clear_Locks_Confirm;  // Confirmation
741
    timeout = CYGNUM_DEVS_FLASH_INTEL_28FXXX_TIMEOUT;
742
    while(((stat = ROM[0]) & FLASH_Status_Ready) != FLASH_Status_Ready) {
743
        if (--timeout == 0) break;
744
    }
745
 
746
    // Restore the lock state
747
    bp = ROM;
748
    for (i = 0;  i < blocks;  i++) {
749
        bpv = FLASH_P2V( bp );
750
        if (is_locked[i]) {
751
            *bpv = FLASH_Set_Lock;
752
            *bpv = FLASH_Set_Lock_Confirm;  // Confirmation
753
            timeout = CYGNUM_DEVS_FLASH_INTEL_28FXXX_TIMEOUT;
754
            while(((stat = ROM[0]) & FLASH_Status_Ready) != FLASH_Status_Ready) {
755
                if (--timeout == 0) break;
756
            }
757
        }
758
        bp += block_size / sizeof(*bp);
759
    }
760
 
761
    // Restore ROM to "normal" mode
762
    ROM[0] = FLASH_Reset;
763
#endif
764
#endif // #CYGHWR_DEVS_FLASH_SHARP_LH28F016SCT_Z4
765
}
766
#endif // CYGHWR_IO_FLASH_BLOCK_LOCKING
767
 
768
#endif // CYGONCE_DEVS_FLASH_INTEL_28FXXX_INL

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.