1 #ifndef CYGONCE_DEVS_FLASH_ATMEL_AT29CXXXX_INL
2 #define CYGONCE_DEVS_FLASH_ATMEL_AT29CXXXX_INL
3 //==========================================================================
7 // Atmel AT29Cxxxx series flash driver
9 //==========================================================================
10 //####ECOSGPLCOPYRIGHTBEGIN####
11 // -------------------------------------------
12 // This file is part of eCos, the Embedded Configurable Operating System.
13 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
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 version.
19 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
20 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 // You should have received a copy of the GNU General Public License along
25 // with eCos; if not, write to the Free Software Foundation, Inc.,
26 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
28 // As a special exception, if other files instantiate templates or use macros
29 // or inline functions from this file, or you compile this file and link it
30 // with other works to produce a work based on this file, this file does not
31 // by itself cause the resulting work to be covered by the GNU General Public
32 // License. However the source code for this file must still be made available
33 // in accordance with section (3) of the GNU General Public License.
35 // This exception does not invalidate any other reasons why a work based on
36 // this file might be covered by the GNU General Public License.
38 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
39 // at http://sources.redhat.com/ecos/ecos-license/
40 // -------------------------------------------
41 //####ECOSGPLCOPYRIGHTEND####
42 //==========================================================================
43 //#####DESCRIPTIONBEGIN####
46 // Contributors: gthomas, jskov
51 // Notes: FLASH_P2V is not properly used.
53 //####DESCRIPTIONEND####
55 //==========================================================================
57 #include <pkgconf/hal.h>
58 #include <cyg/hal/hal_arch.h>
59 #include <cyg/hal/hal_cache.h>
60 #include CYGHWR_MEMORY_LAYOUT_H
62 #define _FLASH_PRIVATE_
63 #include <cyg/io/flash.h>
66 //----------------------------------------------------------------------------
67 // Common device details.
68 #define FLASH_Read_ID FLASHWORD( 0x90 )
69 #define FLASH_Read_ID_Exit FLASHWORD( 0xF0 )
70 #define FLASH_Reset FLASHWORD( 0xFF )
71 #define FLASH_Program FLASHWORD( 0xA0 )
72 #define FLASH_Block_Erase FLASHWORD( 0x30 )
74 #define FLASH_Data FLASHWORD( 0x80 ) // Data complement
75 #define FLASH_Busy FLASHWORD( 0x40 ) // "Toggle" bit
76 #define FLASH_Err FLASHWORD( 0x20 )
77 #define FLASH_Sector_Erase_Timer FLASHWORD( 0x08 )
79 #define FLASH_Setup_Addr1 (0x5555)
80 #define FLASH_Setup_Addr2 (0x2AAA)
81 #define FLASH_Setup_Code1 FLASHWORD( 0xAA )
82 #define FLASH_Setup_Code2 FLASHWORD( 0x55 )
83 #define FLASH_Setup_Erase FLASHWORD( 0x80 )
85 // Platform code must define the below
86 // #define CYGNUM_FLASH_INTERLEAVE : Number of interleaved devices (in parallel)
87 // #define CYGNUM_FLASH_SERIES : Number of devices in series
88 // #define CYGNUM_FLASH_BASE : Address of first device
89 // And select one of the below device variants
91 #ifdef CYGPKG_DEVS_FLASH_ATMEL_AT29C040A
92 # define FLASH_BLOCK_SIZE (0x10000*CYGNUM_FLASH_INTERLEAVE)
93 # define FLASH_NUM_REGIONS (8)
94 # define CYGNUM_FLASH_BASE_MASK (0xFFF80000u) // 512kB devices
95 # define CYGNUM_FLASH_WIDTH (8)
96 # define CYGNUM_FLASH_BLANK (1)
97 # define CYGNUM_FLASH_ID_MANUFACTURER FLASHWORD(0x1F)
98 # define CYGNUM_FLASH_ID_DEVICE FLASHWORD(0xA4)
100 #ifdef CYGPKG_DEVS_FLASH_ATMEL_AT29LV1024
101 # define FLASH_BLOCK_SIZE (0x100*CYGNUM_FLASH_INTERLEAVE)
102 # define FLASH_NUM_REGIONS (512)
103 # define CYGNUM_FLASH_BASE_MASK (0xFFFe0000u) // 128kB devices
104 # define CYGNUM_FLASH_WIDTH (16)
105 # define CYGNUM_FLASH_BLANK (1)
106 # define CYGNUM_FLASH_ID_MANUFACTURER FLASHWORD(0x1F)
107 # define CYGNUM_FLASH_ID_DEVICE FLASHWORD(0x26)
110 #define FLASH_DEVICE_SIZE (FLASH_BLOCK_SIZE*FLASH_NUM_REGIONS)
111 #define CYGNUM_FLASH_DEVICES (CYGNUM_FLASH_INTERLEAVE*CYGNUM_FLASH_SERIES)
113 //----------------------------------------------------------------------------
114 // Now that device properties are defined, include magic for defining
115 // accessor type and constants.
116 #include <cyg/io/flash_dev.h>
118 //----------------------------------------------------------------------------
119 // Functions that put the flash device into non-read mode must reside
121 void flash_query(void* data) __attribute__ ((section (".2ram.flash_query")));
122 int flash_erase_block(void* block, unsigned int size)
123 __attribute__ ((section (".2ram.flash_erase_block")));
124 int flash_program_buf(void* addr, void* data, int len)
125 __attribute__ ((section (".2ram.flash_program_buf")));
128 //----------------------------------------------------------------------------
129 // Initialize driver details
137 // Check that flash_id data is matching the one the driver was
139 if (id[0] != CYGNUM_FLASH_ID_MANUFACTURER
140 || id[1] != CYGNUM_FLASH_ID_DEVICE)
141 return FLASH_ERR_DRV_WRONG_PART;
143 // Hard wired for now
144 flash_info.block_size = FLASH_BLOCK_SIZE;
145 flash_info.blocks = FLASH_NUM_REGIONS;
146 flash_info.start = (void *)CYGNUM_FLASH_BASE;
147 flash_info.end = (void *)(CYGNUM_FLASH_BASE+ (FLASH_NUM_REGIONS * FLASH_BLOCK_SIZE * CYGNUM_FLASH_SERIES));
151 //----------------------------------------------------------------------------
152 // Map a hardware status to a package error
154 flash_hwr_map_error(int e)
160 //----------------------------------------------------------------------------
161 // See if a range of FLASH addresses overlaps currently running code
163 flash_code_overlaps(void *start, void *end)
165 extern unsigned char _stext[], _etext[];
167 return ((((unsigned long)&_stext >= (unsigned long)start) &&
168 ((unsigned long)&_stext < (unsigned long)end)) ||
169 (((unsigned long)&_etext >= (unsigned long)start) &&
170 ((unsigned long)&_etext < (unsigned long)end)));
173 //----------------------------------------------------------------------------
176 // Only reads the manufacturer and part number codes for the first
177 // device(s) in series. It is assumed that any devices in series
178 // will be of the same type.
181 flash_query(void* data)
183 volatile flash_data_t *ROM;
184 flash_data_t* id = (flash_data_t*) data;
187 ROM = (volatile flash_data_t*) CYGNUM_FLASH_BASE;
189 ROM[FLASH_Setup_Addr1] = FLASH_Setup_Code1;
190 ROM[FLASH_Setup_Addr2] = FLASH_Setup_Code2;
191 ROM[FLASH_Setup_Addr1] = FLASH_Read_ID;
194 for (i = 10000; i > 0; i--);
196 // Manufacturers' code
201 ROM[FLASH_Setup_Addr1] = FLASH_Setup_Code1;
202 ROM[FLASH_Setup_Addr2] = FLASH_Setup_Code2;
203 ROM[FLASH_Setup_Addr1] = FLASH_Read_ID_Exit;
206 for (i = 10000; i > 0; i--);
209 //----------------------------------------------------------------------------
212 flash_erase_block(void* block, unsigned int len)
214 volatile flash_data_t* ROM;
215 volatile flash_data_t* addr_ptr = (volatile flash_data_t*) block;
216 volatile flash_data_t* addr_ptr2;
218 int res = FLASH_ERR_OK;
220 while ((FLASH_ERR_OK == res) && (len > 0)) {
221 int len2, i, timeout;
222 flash_data_t state, prev_state;
224 // Base address of device(s) being programmed.
225 ROM = (volatile flash_data_t*)((unsigned long)block & ~(FLASH_DEVICE_SIZE-1));
227 // Program data [byte] - 4 step sequence
228 ROM[FLASH_Setup_Addr1] = FLASH_Setup_Code1;
229 ROM[FLASH_Setup_Addr2] = FLASH_Setup_Code2;
230 ROM[FLASH_Setup_Addr1] = FLASH_Program;
232 addr_ptr2 = addr_ptr;
235 // Always load 256 times
236 for (i = 0; i < 256; i++) {
237 *addr_ptr2++ = FLASH_BlankValue;
240 // Wait for completion (bit 6 stops toggling)
242 prev_state = *addr_ptr & FLASH_Busy;
244 state = *addr_ptr & FLASH_Busy;
245 if (prev_state == state) {
248 if (--timeout == 0) {
249 res = FLASH_ERR_DRV_TIMEOUT;
255 // Verify loaded data bytes
256 for (i = 0; (len > 0) && (i < 256) ;) {
257 if (*addr_ptr != FLASH_BlankValue) {
258 // Only update return value if erase operation was OK
259 if (FLASH_ERR_OK == res) res = FLASH_ERR_DRV_VERIFY;
263 len -= sizeof(*addr_ptr);
264 i += sizeof(*addr_ptr);
271 //----------------------------------------------------------------------------
274 flash_program_buf(void* addr, void* data, int len)
276 volatile flash_data_t* ROM;
277 volatile flash_data_t* addr_ptr = (volatile flash_data_t*) addr;
278 volatile flash_data_t* data_ptr = (volatile flash_data_t*) data;
279 volatile flash_data_t* addr_ptr2;
280 volatile flash_data_t* data_ptr2;
282 int res = FLASH_ERR_OK;
285 CYG_ASSERT((unsigned long)data_ptr & (sizeof(flash_data_t)-1) == 0,
286 "Data not properly aligned");
287 CYG_ASSERT((unsigned long)addr_ptr & (CYGNUM_FLASH_INTERLEAVE*sizeof(flash_data_t)-1) == 0,
288 "Addr not properly aligned (to first interleaved device)");
291 while ((FLASH_ERR_OK == res) && (len > 0)) {
292 int len2, i, timeout;
293 flash_data_t state, prev_state;
295 // Base address of device(s) being programmed.
296 ROM = (volatile flash_data_t*)((unsigned long)addr & ~(FLASH_DEVICE_SIZE-1));
298 // Program data [byte] - 4 step sequence
299 ROM[FLASH_Setup_Addr1] = FLASH_Setup_Code1;
300 ROM[FLASH_Setup_Addr2] = FLASH_Setup_Code2;
301 ROM[FLASH_Setup_Addr1] = FLASH_Program;
303 addr_ptr2 = addr_ptr;
304 data_ptr2 = data_ptr;
307 // Always load 256 times
308 for (i = 0; i < 256; i++) {
310 *addr_ptr2++ = *data_ptr2++;
311 len2 -= sizeof(*data_ptr2);
313 *addr_ptr2++ = FLASH_BlankValue;
317 // Wait for completion (bit 6 stops toggling)
319 prev_state = *addr_ptr & FLASH_Busy;
321 state = *addr_ptr & FLASH_Busy;
322 if (prev_state == state) {
325 if (--timeout == 0) {
326 res = FLASH_ERR_DRV_TIMEOUT;
332 // Verify loaded data bytes
333 for (i = 0; (len > 0) && (i < 256) ;) {
334 if (*addr_ptr != *data_ptr) {
335 // Only update return value if erase operation was OK
336 if (FLASH_ERR_OK == res) res = FLASH_ERR_DRV_VERIFY;
341 len -= sizeof(*data_ptr);
342 i += sizeof(*data_ptr);
347 // Ideally, we'd want to return not only the failure code, but also
348 // the address/device that reported the error.
352 #endif // CYGONCE_DEVS_FLASH_ATMEL_AT29CXXXX_INL