1 //==========================================================================
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.
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.
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
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.
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.
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.
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####
43 // Author(s): gthomas, hmt
44 // Contributors: gthomas
49 //####DESCRIPTIONEND####
51 //==========================================================================
53 #include <pkgconf/system.h>
54 #include <pkgconf/hal.h>
55 #include <cyg/hal/hal_arch.h>
57 #define _FLASH_PRIVATE_
58 #include <cyg/io/flash.h>
62 #define _si(p) ((p[1]<<8)|p[0])
64 extern void diag_dump_buf(void *buf, CYG_ADDRWORD len);
66 extern int strncmp(const char *s1, const char *s2, size_t len);
67 extern void *memcpy( void *, const void *, size_t );
72 struct FLASH_query data, *qp;
73 int num_regions, region_size, buffer_size;
75 flash_dev_query(&data);
77 if ( ((qp->manuf_code == FLASH_Intel_code) ||
78 (qp->manuf_code == FLASH_STMicro_code))
79 #ifdef CYGOPT_FLASH_IS_BOOTBLOCK
80 // device types go as follows: 0x90 for 16-bits, 0xD0 for 8-bits,
81 // plus 0 or 1 for -T (Top Boot) or -B (Bottom Boot)
82 // [FIXME: whatever that means :FIXME]
83 // [I think it means the boot blocks are top/bottom of addr space]
84 // plus the following size codes:
85 // 0: 16Mbit 2: 8Mbit 4: 4Mbit
86 // 6: 32Mbit 8: 64Mbit
87 #if 16 == CYGNUM_FLASH_WIDTH
88 && (0x90 == (0xF0 & qp->device_code)) // 16-bit devices
89 #elif 8 == CYGNUM_FLASH_WIDTH
90 && (0xD0 == (0xF0 & qp->device_code)) // 8-bit devices
93 #error Only understand 16 and 8-bit bootblock flash types
96 int lookup[] = { 16, 8, 4, 32, 64 };
97 #define BLOCKSIZE (0x10000)
98 region_size = BLOCKSIZE;
99 num_regions = qp->device_code & 0x0F;
101 if ( num_regions > 4 )
102 goto flash_type_unknown;
103 num_regions = lookup[num_regions];
104 num_regions *= 1024 * 1024; // to bits
105 num_regions /= 8; // to bytes
106 num_regions /= BLOCKSIZE; // to blocks
108 #else // CYGOPT_FLASH_IS_BOOTBLOCK
109 && (strncmp(qp->id, "QRY", 3) == 0)) {
110 num_regions = _si(qp->num_regions)+2;
111 region_size = _si(qp->region_size)*256;
112 if (_si(qp->buffer_size)) {
113 buffer_size = CYGNUM_FLASH_DEVICES << _si(qp->buffer_size);
117 #endif // Not CYGOPT_FLASH_IS_BOOTBLOCK
119 flash_info.block_size = region_size*CYGNUM_FLASH_DEVICES;
120 flash_info.buffer_size = buffer_size;
121 flash_info.blocks = num_regions;
122 flash_info.start = (void *)CYGNUM_FLASH_BASE;
123 flash_info.end = (void *)(CYGNUM_FLASH_BASE +
124 (num_regions*region_size*CYGNUM_FLASH_DEVICES));
125 #ifdef CYGNUM_FLASH_BASE_MASK
126 // Then this gives us a maximum size for the (visible) device.
127 // This is to cope with oversize devices fitted, with some high
128 // address lines ignored.
129 if ( ((unsigned int)flash_info.start & CYGNUM_FLASH_BASE_MASK) !=
130 (((unsigned int)flash_info.end - 1) & CYGNUM_FLASH_BASE_MASK ) ) {
131 // then the size of the device appears to span >1 device-worth!
133 x = (~(CYGNUM_FLASH_BASE_MASK)) + 1; // expected device size
134 x += (unsigned int)flash_info.start;
135 if ( x < (unsigned int)flash_info.end ) { // 2nd sanity check
136 (*flash_info.pf)("\nFLASH: Oversized device! End addr %p changed to %p\n",
137 flash_info.end, (void *)x );
138 flash_info.end = (void *)x;
139 // Also adjust the block count else unlock crashes!
140 x = ((cyg_uint8 *)flash_info.end - (cyg_uint8 *)flash_info.start)
141 / flash_info.block_size;
142 flash_info.blocks = x;
145 #endif // CYGNUM_FLASH_BASE_MASK
149 #ifdef CYGOPT_FLASH_IS_BOOTBLOCK
152 (*flash_info.pf)("Can't identify FLASH, sorry, man %x, dev %x, id [%4s] \n",
153 qp->manuf_code, qp->device_code, qp->id );
154 diag_dump_buf(qp, sizeof(data));
155 return FLASH_ERR_HWR;
158 // Map a hardware status to a package error
160 flash_hwr_map_error(int err)
162 if (err & FLASH_ErrorMask) {
163 (*flash_info.pf)("Err = %x\n", err);
164 if (err & FLASH_ErrorProgram)
165 return FLASH_ERR_PROGRAM;
166 else if (err & FLASH_ErrorErase)
167 return FLASH_ERR_ERASE;
169 return FLASH_ERR_HWR; // FIXME
175 // See if a range of FLASH addresses overlaps currently running code
177 flash_code_overlaps(void *start, void *end)
179 extern char _stext[], _etext[];
181 return ((((unsigned long)&_stext >= (unsigned long)start) &&
182 ((unsigned long)&_stext < (unsigned long)end)) ||
183 (((unsigned long)&_etext >= (unsigned long)start) &&
184 ((unsigned long)&_etext < (unsigned long)end)));