1 |
27 |
unneback |
//==========================================================================
|
2 |
|
|
//
|
3 |
|
|
// strata.c
|
4 |
|
|
//
|
5 |
|
|
// Flash programming
|
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 Red Hat, 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 version.
|
16 |
|
|
//
|
17 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
|
18 |
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
19 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
20 |
|
|
// for more details.
|
21 |
|
|
//
|
22 |
|
|
// You should have received a copy of the GNU General Public License along
|
23 |
|
|
// with eCos; if not, write to the Free Software Foundation, Inc.,
|
24 |
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
25 |
|
|
//
|
26 |
|
|
// As a special exception, if other files instantiate templates or use macros
|
27 |
|
|
// or inline functions from this file, or you compile this file and link it
|
28 |
|
|
// with other works to produce a work based on this file, this file does not
|
29 |
|
|
// by itself cause the resulting work to be covered by the GNU General Public
|
30 |
|
|
// License. However the source code for this file must still be made available
|
31 |
|
|
// in accordance with section (3) of the GNU General Public License.
|
32 |
|
|
//
|
33 |
|
|
// This exception does not invalidate any other reasons why a work based on
|
34 |
|
|
// this file might be covered by the GNU General Public License.
|
35 |
|
|
//
|
36 |
|
|
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
|
37 |
|
|
// at http://sources.redhat.com/ecos/ecos-license/
|
38 |
|
|
// -------------------------------------------
|
39 |
|
|
//####ECOSGPLCOPYRIGHTEND####
|
40 |
|
|
//==========================================================================
|
41 |
|
|
//#####DESCRIPTIONBEGIN####
|
42 |
|
|
//
|
43 |
|
|
// Author(s): gthomas, hmt
|
44 |
|
|
// Contributors: gthomas
|
45 |
|
|
// Date: 2001-02-14
|
46 |
|
|
// Purpose:
|
47 |
|
|
// Description:
|
48 |
|
|
//
|
49 |
|
|
//####DESCRIPTIONEND####
|
50 |
|
|
//
|
51 |
|
|
//==========================================================================
|
52 |
|
|
|
53 |
|
|
#include <pkgconf/system.h>
|
54 |
|
|
#include <pkgconf/hal.h>
|
55 |
|
|
#include <cyg/hal/hal_arch.h>
|
56 |
|
|
#include <cyg/hal/hal_cache.h>
|
57 |
|
|
|
58 |
|
|
#define _FLASH_PRIVATE_
|
59 |
|
|
#include <cyg/io/flash.h>
|
60 |
|
|
|
61 |
|
|
#include "strata.h"
|
62 |
|
|
|
63 |
|
|
#define _si(p) ((p[1]<<8)|p[0])
|
64 |
|
|
|
65 |
|
|
extern void diag_dump_buf(void *buf, CYG_ADDRWORD len);
|
66 |
|
|
|
67 |
|
|
extern int strncmp(const char *s1, const char *s2, size_t len);
|
68 |
|
|
extern void *memcpy( void *, const void *, size_t );
|
69 |
|
|
|
70 |
|
|
int
|
71 |
|
|
flash_hwr_init(void)
|
72 |
|
|
{
|
73 |
|
|
struct FLASH_query data, *qp;
|
74 |
|
|
extern char flash_query[], flash_query_end[];
|
75 |
|
|
typedef int code_fun(unsigned char *);
|
76 |
|
|
code_fun *_flash_query;
|
77 |
|
|
int code_len, stat, num_regions, region_size, buffer_size;
|
78 |
|
|
int icache_on, dcache_on;
|
79 |
|
|
|
80 |
|
|
HAL_DCACHE_IS_ENABLED(dcache_on);
|
81 |
|
|
HAL_ICACHE_IS_ENABLED(icache_on);
|
82 |
|
|
|
83 |
|
|
// Copy 'program' code to RAM for execution
|
84 |
|
|
code_len = (unsigned long)&flash_query_end - (unsigned long)&flash_query;
|
85 |
|
|
_flash_query = (code_fun *)flash_info.work_space;
|
86 |
|
|
memcpy(_flash_query, &flash_query, code_len);
|
87 |
|
|
if (dcache_on) {
|
88 |
|
|
HAL_DCACHE_SYNC(); // Should guarantee this code will run
|
89 |
|
|
}
|
90 |
|
|
if (icache_on) {
|
91 |
|
|
HAL_ICACHE_DISABLE(); // is also required to avoid old contents
|
92 |
|
|
}
|
93 |
|
|
|
94 |
|
|
stat = (*_flash_query)((unsigned char *)&data);
|
95 |
|
|
if (icache_on) {
|
96 |
|
|
HAL_ICACHE_ENABLE();
|
97 |
|
|
}
|
98 |
|
|
|
99 |
|
|
qp = &data;
|
100 |
|
|
if ( (qp->manuf_code == FLASH_Intel_code)
|
101 |
|
|
#ifdef CYGOPT_FLASH_IS_BOOTBLOCK
|
102 |
|
|
// device types go as follows: 0x90 for 16-bits, 0xD0 for 8-bits,
|
103 |
|
|
// plus 0 or 1 for -T (Top Boot) or -B (Bottom Boot)
|
104 |
|
|
// [FIXME: whatever that means :FIXME]
|
105 |
|
|
// [I think it means the boot blocks are top/bottom of addr space]
|
106 |
|
|
// plus the following size codes:
|
107 |
|
|
// 0: 16Mbit 2: 8Mbit 4: 4Mbit
|
108 |
|
|
// 6: 32Mbit 8: 64Mbit
|
109 |
|
|
#if 16 == CYGNUM_FLASH_WIDTH
|
110 |
|
|
&& (0x90 == (0xF0 & qp->device_code)) // 16-bit devices
|
111 |
|
|
#elif 8 == CYGNUM_FLASH_WIDTH
|
112 |
|
|
&& (0xD0 == (0xF0 & qp->device_code)) // 8-bit devices
|
113 |
|
|
#else
|
114 |
|
|
&& 0
|
115 |
|
|
#error Only understand 16 and 8-bit bootblock flash types
|
116 |
|
|
#endif
|
117 |
|
|
) {
|
118 |
|
|
int lookup[] = { 16, 8, 4, 32, 64 };
|
119 |
|
|
#define BLOCKSIZE (0x10000)
|
120 |
|
|
region_size = BLOCKSIZE;
|
121 |
|
|
num_regions = qp->device_code & 0x0F;
|
122 |
|
|
num_regions >>= 1;
|
123 |
|
|
if ( num_regions > 4 )
|
124 |
|
|
goto flash_type_unknown;
|
125 |
|
|
num_regions = lookup[num_regions];
|
126 |
|
|
num_regions *= 1024 * 1024; // to bits
|
127 |
|
|
num_regions /= 8; // to bytes
|
128 |
|
|
num_regions /= BLOCKSIZE; // to blocks
|
129 |
|
|
buffer_size = 0;
|
130 |
|
|
#else // CYGOPT_FLASH_IS_BOOTBLOCK
|
131 |
|
|
&& (strncmp(qp->id, "QRY", 3) == 0)) {
|
132 |
|
|
num_regions = _si(qp->num_regions)+1;
|
133 |
|
|
region_size = _si(qp->region_size)*256;
|
134 |
|
|
if (_si(qp->buffer_size)) {
|
135 |
|
|
buffer_size = CYGNUM_FLASH_DEVICES << _si(qp->buffer_size);
|
136 |
|
|
} else {
|
137 |
|
|
buffer_size = 0;
|
138 |
|
|
}
|
139 |
|
|
#endif // Not CYGOPT_FLASH_IS_BOOTBLOCK
|
140 |
|
|
|
141 |
|
|
flash_info.block_size = region_size*CYGNUM_FLASH_DEVICES;
|
142 |
|
|
flash_info.buffer_size = buffer_size;
|
143 |
|
|
flash_info.blocks = num_regions;
|
144 |
|
|
flash_info.start = (void *)CYGNUM_FLASH_BASE;
|
145 |
|
|
flash_info.end = (void *)(CYGNUM_FLASH_BASE +
|
146 |
|
|
(num_regions*region_size*CYGNUM_FLASH_DEVICES));
|
147 |
|
|
#ifdef CYGNUM_FLASH_BASE_MASK
|
148 |
|
|
// Then this gives us a maximum size for the (visible) device.
|
149 |
|
|
// This is to cope with oversize devices fitted, with some high
|
150 |
|
|
// address lines ignored.
|
151 |
|
|
if ( ((unsigned int)flash_info.start & CYGNUM_FLASH_BASE_MASK) !=
|
152 |
|
|
(((unsigned int)flash_info.end - 1) & CYGNUM_FLASH_BASE_MASK ) ) {
|
153 |
|
|
// then the size of the device appears to span >1 device-worth!
|
154 |
|
|
unsigned int x;
|
155 |
|
|
x = (~(CYGNUM_FLASH_BASE_MASK)) + 1; // expected device size
|
156 |
|
|
x += (unsigned int)flash_info.start;
|
157 |
|
|
if ( x < (unsigned int)flash_info.end ) { // 2nd sanity check
|
158 |
|
|
(*flash_info.pf)("\nFLASH: Oversized device! End addr %p changed to %p\n",
|
159 |
|
|
flash_info.end, (void *)x );
|
160 |
|
|
flash_info.end = (void *)x;
|
161 |
|
|
// Also adjust the block count else unlock crashes!
|
162 |
|
|
x = ((cyg_uint8 *)flash_info.end - (cyg_uint8 *)flash_info.start)
|
163 |
|
|
/ flash_info.block_size;
|
164 |
|
|
flash_info.blocks = x;
|
165 |
|
|
}
|
166 |
|
|
}
|
167 |
|
|
#endif // CYGNUM_FLASH_BASE_MASK
|
168 |
|
|
|
169 |
|
|
return FLASH_ERR_OK;
|
170 |
|
|
}
|
171 |
|
|
#ifdef CYGOPT_FLASH_IS_BOOTBLOCK
|
172 |
|
|
flash_type_unknown:
|
173 |
|
|
#endif
|
174 |
|
|
(*flash_info.pf)("Can't identify FLASH, sorry, man %x, dev %x, id [%4s] stat %x\n",
|
175 |
|
|
qp->manuf_code, qp->device_code, qp->id, stat );
|
176 |
|
|
diag_dump_buf(qp, sizeof(data));
|
177 |
|
|
return FLASH_ERR_HWR;
|
178 |
|
|
}
|
179 |
|
|
|
180 |
|
|
// Map a hardware status to a package error
|
181 |
|
|
int
|
182 |
|
|
flash_hwr_map_error(int err)
|
183 |
|
|
{
|
184 |
|
|
if (err & FLASH_ErrorMask) {
|
185 |
|
|
(*flash_info.pf)("Err = %x\n", err);
|
186 |
|
|
if (err & FLASH_ErrorProgram)
|
187 |
|
|
return FLASH_ERR_PROGRAM;
|
188 |
|
|
else if (err & FLASH_ErrorErase)
|
189 |
|
|
return FLASH_ERR_ERASE;
|
190 |
|
|
else
|
191 |
|
|
return FLASH_ERR_HWR; // FIXME
|
192 |
|
|
} else {
|
193 |
|
|
return FLASH_ERR_OK;
|
194 |
|
|
}
|
195 |
|
|
}
|
196 |
|
|
|
197 |
|
|
// See if a range of FLASH addresses overlaps currently running code
|
198 |
|
|
bool
|
199 |
|
|
flash_code_overlaps(void *start, void *end)
|
200 |
|
|
{
|
201 |
|
|
extern char _stext[], _etext[];
|
202 |
|
|
|
203 |
|
|
return ((((unsigned long)&_stext >= (unsigned long)start) &&
|
204 |
|
|
((unsigned long)&_stext < (unsigned long)end)) ||
|
205 |
|
|
(((unsigned long)&_etext >= (unsigned long)start) &&
|
206 |
|
|
((unsigned long)&_etext < (unsigned long)end)));
|
207 |
|
|
}
|
208 |
|
|
|
209 |
|
|
// EOF strata.c
|