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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [hal/] [arm/] [integrator/] [current/] [src/] [prog_flash.c] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//        prog_flash.c
4
//
5
//        ARM INTEGRATOR eval board FLASH program tool
6
//
7
//==========================================================================
8
// ####ECOSGPLCOPYRIGHTBEGIN####                                            
9
// -------------------------------------------                              
10
// This file is part of eCos, the Embedded Configurable Operating System.   
11
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
12
//
13
// eCos is free software; you can redistribute it and/or modify it under    
14
// the terms of the GNU General Public License as published by the Free     
15
// Software Foundation; either version 2 or (at your option) any later      
16
// version.                                                                 
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT      
19
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or    
20
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License    
21
// for more details.                                                        
22
//
23
// You should have received a copy of the GNU General Public License        
24
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
25
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
26
//
27
// As a special exception, if other files instantiate templates or use      
28
// macros or inline functions from this file, or you compile this file      
29
// and link it with other works to produce a work based on this file,       
30
// this file does not by itself cause the resulting work to be covered by   
31
// the GNU General Public License. However the source code for this file    
32
// must still be made available in accordance with section (3) of the GNU   
33
// General Public License v2.                                               
34
//
35
// This exception does not invalidate any other reasons why a work based    
36
// on this file might be covered by the GNU General Public License.         
37
// -------------------------------------------                              
38
// ####ECOSGPLCOPYRIGHTEND####                                              
39
//==========================================================================
40
//#####DESCRIPTIONBEGIN####
41
//
42
// Author(s):     gthomas
43
// Contributors:  Philippe Robin
44
// Date:          November 7, 2000
45
// Description:   Tool used to program onboard FLASH image
46
//####DESCRIPTIONEND####
47
 
48
//
49
// This program will program the FLASH on the PID board
50
// It is similar to 'flash' (which also downloads S records) but it always
51
// programs from a fixed buffer.  This is sufficient to load/update the GDB
52
// stubs on the board.
53
//
54
 
55
#include <pkgconf/libc.h>   // Configuration header
56
 
57
#include <cyg/kernel/kapi.h>
58
#include <stdlib.h>
59
#include <ctype.h>
60
#include <cyg/infra/testcase.h>
61
#include <sys/cstartup.h>
62
#include <cyg/hal/hal_integrator.h>
63
 
64
#ifndef FALSE
65
#define FALSE 0
66
#define TRUE  1
67
#endif
68
 
69
 
70
#define INVALID_FTYPE           0x00000000
71
#define UNKNOWN_FTYPE           0xFFFFFFFF
72
#define ATMEL_FTYPE             0x00000001
73
#define INTEL_FTYPE             0x00000002
74
 
75
#define FLASH_TYPE_MASK         (ATMEL_FTYPE | INTEL_FTYPE)
76
 
77
// On Some platforms Boot and program flash may be part of the same device
78
#define INTEGRATED_FTYPE        0x80000000
79
#define BOOT_FTYPE              0x40000000
80
#define APP_FTYPE               0x20000000
81
 
82
#define FLASH_USAGE_MASK        (BOOT_FTYPE | APP_FTYPE)
83
 
84
#define DEFAULT_FLASH_MASK 0xFFFFFFF8
85
#define FLASH_BLOCK_SIZE        0x00020000      // 128Kb
86
#define EPROM_BASE              0x20000000
87
#define EPROM_SIZE              0x00080000      // 512Kb
88
#define FLASH_BASE              0x24000000
89
#define FLASH_SIZE              0x02000000      // 32Mb
90
 
91
typedef struct flashType {
92
    char *base;                 // Base Address of flash
93
    char *physicalBase;         // before mem initialisation
94
    unsigned int size;          // Size of flash, in bytes
95
    unsigned int type;          // Atmel / Intel (CFI) / Unknown
96
    unsigned int writeSize;     // Size of physical block
97
    unsigned int eraseSize;     // Size of block erase
98
    unsigned int logicalSize;   // Size of logical block
99
    char *ident;                // identification string
100
} tFlash;
101
 
102
tFlash Integrator_Flash[2] =
103
{
104
    {
105
        (char *)EPROM_BASE,             // Base Address of flash
106
        (char *)EPROM_BASE,             // Physical Address of flash
107
        EPROM_SIZE,                     // Size of flash, in bytes (512K)
108
        BOOT_FTYPE | ATMEL_FTYPE,       // Flash type
109
        FLASH_BLOCK_SIZE,       // Size of physical block
110
        FLASH_BLOCK_SIZE,       // Size of block erase
111
        FLASH_BLOCK_SIZE,       // Size of logical block
112
        "Atmel",                // Null terminated Info string
113
    },
114
    {
115
        (char *)FLASH_BASE,     // Base Address of flash
116
        (char *)FLASH_BASE,     // Physical Address of flash
117
        FLASH_SIZE,             // Size of flash, in bytes
118
        APP_FTYPE | INTEL_FTYPE,        // Flash type
119
        FLASH_BLOCK_SIZE,       // Size of physical block
120
        FLASH_BLOCK_SIZE,       // Size of block erase
121
        FLASH_BLOCK_SIZE,       // Size of logical block
122
        "Intel 28F320S3",       // Null terminated Info string
123
   }
124
};
125
 
126
 
127
extern void diag_printf(const char *, ...);
128
int identify_FLASH(void);
129
void write_sector(int, char *);
130
 
131
char *flash_buffer =     (char *)0x60000;
132
char *flash_buffer_end = (char *)0x80000;
133
 
134
#ifdef BE_IMAGE
135
#define BUF(x) buf[x^3]
136
#else
137
#define BUF(x) buf[x]
138
#endif
139
 
140
// FUNCTIONS
141
 
142
externC void
143
cyg_package_start( void )
144
{
145
#ifdef CYGPKG_LIBC
146
    cyg_iso_c_start();
147
#else
148
    (void)main(0, NULL);
149
#endif
150
} // cyg_package_start()
151
 
152
int
153
main( int argc, char *argv[] )
154
{
155
    int i;
156
 
157
    diag_printf("FLASH here!\n");
158
    while (identify_FLASH() == 0) {
159
        diag_printf("... Please change FLASH jumper\n");
160
        cyg_thread_delay(5*100);
161
    }
162
    diag_printf("About to program FLASH using data at %x..%x\n", flash_buffer, flash_buffer_end);
163
    diag_printf("*** Press RESET now to abort!\n");
164
    cyg_thread_delay(5*100);
165
    diag_printf("\n");
166
    diag_printf("...Programming FLASH\n");
167
 
168
    i = 0;
169
    while (flash_buffer < flash_buffer_end) {
170
        write_sector(i++, flash_buffer);
171
        flash_buffer += 256;
172
    }
173
    diag_printf("All done!\n");
174
    while (1) ;
175
}
176
 
177
// Adapted from ARM sample code
178
#define SEQ_ADD1                0x5555
179
#define SEQ_ADD2                0x2aaa
180
#define START_CMD1              0xaaaaaaaa
181
#define START_CMD2              0x55555555
182
#define ID_CMD                  0x90909090
183
#define PROG_CMD                0xA0
184
#define STOP_CMD                0xf0f0f0f0
185
 
186
#define MAN_ATMEL               0x1F
187
#define ATMEL_AT29C040_ID       0X5B
188
#define ATMEL_AT29C040A_ID      0XA4
189
#define ATMEL_AT29C1024_ID      0X25
190
#define ATMEL_SECTOR_SIZE       256
191
#define ATMEL_MAX_SECTORS       2048
192
 
193
#define MAN_INTEL               0xB0
194
#define INTEL_28F320S3_ID       0xD4
195
 
196
int manuf_code, device_code;
197
int sector_size, max_no_of_sectors, word_mode;
198
volatile unsigned int *FLASH = (volatile unsigned int *)0x24000000;
199
 
200
int
201
identify_FLASH(void )
202
{
203
    unsigned int *ptr = (unsigned int *)FLASH;
204
 
205
    HAL_FLASH_WRITE_ENABLE();
206
 
207
    // Enter Software Product Identification Mode
208
    FLASH[SEQ_ADD1] = START_CMD1;
209
    FLASH[SEQ_ADD2] = START_CMD2;
210
    FLASH[SEQ_ADD1] = ID_CMD;
211
 
212
    // Wait at least 10ms
213
    cyg_thread_delay(4);
214
 
215
    // Read Manufacturer and device code from the device
216
    manuf_code   = *ptr++ & 0xff;
217
    device_code  = *ptr & 0xff;
218
 
219
    diag_printf("manuf: 0x%x, device: 0x%x\n", manuf_code, device_code);
220
 
221
    // Exit Software Product Identification Mode
222
    FLASH[SEQ_ADD1] = START_CMD1;
223
    FLASH[SEQ_ADD2] = START_CMD2;
224
    FLASH[SEQ_ADD1] = STOP_CMD;
225
 
226
    // Wait at least 10ms
227
    cyg_thread_delay(5);
228
 
229
    HAL_FLASH_WRITE_DISABLE();
230
 
231
    if (manuf_code != MAN_ATMEL || manuf_code != MAN_INTEL) {
232
        diag_printf ( "Error: Wrong Manufaturer: %02x\n", manuf_code );
233
        return (0);
234
    }
235
 
236
    switch (device_code) {
237
    case  ATMEL_AT29C040A_ID:
238
        diag_printf ("AT29C040A recognised\n");
239
        sector_size = ATMEL_SECTOR_SIZE;
240
        max_no_of_sectors = ATMEL_MAX_SECTORS;
241
        word_mode = FALSE;
242
        break;
243
 
244
    case  ATMEL_AT29C1024_ID:
245
        diag_printf ("AT29C1024 recognised\n");
246
        sector_size = ATMEL_SECTOR_SIZE;
247
        max_no_of_sectors = ATMEL_MAX_SECTORS;
248
        word_mode = TRUE;
249
        break;
250
 
251
    case  INTEL_28F320S3_ID:
252
        diag_printf ("INTEL_28F320S3 recognised\n");
253
        sector_size = FLASH_BLOCK_SIZE;
254
        //        max_no_of_sectors = ATMEL_MAX_SECTORS;
255
        word_mode = TRUE;
256
        break;
257
 
258
    default :
259
        diag_printf ( "Error: Unsupported device: %02x\n", device_code);
260
        return (0);
261
    }
262
    return (1);
263
}
264
 
265
void
266
write_sector(int num, char *buf)
267
{
268
    int i, cnt;
269
    volatile char *wrt = (volatile char *)&FLASH[num*sector_size];
270
 
271
//    diag_printf("Writing to %08x\n", wrt);
272
    // Enter Program Mode
273
    FLASH[SEQ_ADD1] = START_CMD1;
274
    FLASH[SEQ_ADD2] = START_CMD2;
275
    FLASH[SEQ_ADD1] = PROG_CMD;
276
 
277
    // Note: write bytes as longs regardless of bus width
278
    for (i = 0;  i < sector_size;  i++) {
279
        wrt[i] = BUF(i);
280
    }
281
 
282
    // Wait for sector to program
283
    cnt = 0;
284
    i = sector_size - 1;
285
    while (wrt[i] != BUF(i)) {
286
        if (cnt++ > 0x01000000) break;
287
    }
288
//    diag_printf("Out - i: %d, wrt[i] = %08X.%08X, BUF(i) = %08X, count = %x\n", i, &wrt[i], wrt[i], BUF(i), cnt);
289
 
290
    // Verify
291
    for (i = 0;  i < sector_size;  i++) {
292
        for (cnt = 0;  cnt < 10;  cnt++) {
293
            if (wrt[i] == BUF(i)) break;
294
            cyg_thread_delay(1);
295
        }
296
        if (cnt == 10) {
297
            diag_printf("Can't program at 0x%08X: %02X not %02X\n", wrt, *wrt, BUF(0));
298
        }
299
    }
300
}

powered by: WebSVN 2.1.0

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