comparison packages/devs/flash/amd/am29f040b/current/src/flash_program_buf.c @ 156:b939e9cada46

Merge from eCos master repository on 2001-03-29-21:44:39-BST
author jlarmour
date Fri, 06 Apr 2001 17:20:13 +0000
parents
children
comparison
equal deleted inserted replaced
155:70a4cc6d69d2 156:b939e9cada46
1 //==========================================================================
2 //
3 // flash_program_buf.c
4 //
5 // Flash programming
6 //
7 //==========================================================================
8 //####COPYRIGHTBEGIN####
9 //
10 // -------------------------------------------
11 // The contents of this file are subject to the Red Hat eCos Public License
12 // Version 1.1 (the "License"); you may not use this file except in
13 // compliance with the License. You may obtain a copy of the License at
14 // http://www.redhat.com/
15 //
16 // Software distributed under the License is distributed on an "AS IS"
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
18 // License for the specific language governing rights and limitations under
19 // the License.
20 //
21 // The Original Code is eCos - Embedded Configurable Operating System,
22 // released September 30, 1998.
23 //
24 // The Initial Developer of the Original Code is Red Hat.
25 // Portions created by Red Hat are
26 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
27 // All Rights Reserved.
28 // -------------------------------------------
29 //
30 //####COPYRIGHTEND####
31 //==========================================================================
32 //#####DESCRIPTIONBEGIN####
33 //
34 // Author(s): gthomas
35 // Contributors: gthomas, jskov
36 // Date: 2000-12-05
37 // Purpose:
38 // Description:
39 //
40 //####DESCRIPTIONEND####
41 //
42 //==========================================================================
43
44 #include "flash.h"
45
46 #include <pkgconf/hal.h>
47 #include <cyg/hal/hal_arch.h>
48 #include <cyg/hal/hal_cache.h>
49
50 //
51 // CAUTION! This code must be copied to RAM before execution. Therefore,
52 // it must not contain any code which might be position dependent!
53 //
54
55 int
56 flash_program_buf(volatile flash_data_t *addr, flash_data_t *data, int len)
57 {
58 volatile flash_data_t *ROM;
59 int timeout;
60 int fail = 0;
61 flash_data_t array1[CYGHWR_FLASH_INTERLEAVE];
62 flash_data_t array2[CYGHWR_FLASH_INTERLEAVE];
63 flash_data_t *state, *new_state, *tmp_state;
64
65 #if 0
66 CYG_ASSERT(data & (sizeof(flash_data_t)-1) == 0,
67 "Data not properly aligned");
68 CYG_ASSERT(addr & (CYGHWR_FLASH_INTERLEAVE*sizeof(flash_data_t)-1) == 0,
69 "Addr not properly aligned (to first interleaved device)");
70 #endif
71
72 while (len > 0) {
73 int i;
74
75 // Base address of device(s) being programmed.
76 ROM = (volatile flash_data_t*)((unsigned long)addr & ~(FLASH_DEVICE_SIZE-1));
77
78 #if 1 // So much for my good intentions
79 {
80 int erase_state2, new_state2;
81
82 // Program data [byte] - 4 step sequence
83 ROM[FLASH_Setup_Addr1] = FLASH_Setup_Code1;
84 ROM[FLASH_Setup_Addr2] = FLASH_Setup_Code2;
85 ROM[FLASH_Setup_Addr1] = FLASH_Program;
86 *addr = *data;
87 for (timeout = 50; timeout--; timeout > 0) ;
88 erase_state2 = *addr & FLASH_Busy;
89 while (true) {
90 new_state2 = *addr;
91 if ((new_state2 & FLASH_Busy) == erase_state2) break; // "toggle" stopped
92 erase_state2 = new_state2 & FLASH_Busy;
93 if (new_state2 & FLASH_Err) break;
94 if (--timeout == 0) break;
95 }
96 ROM[0] = FLASH_Reset;
97 if (*addr++ != *data++) {
98 new_state2 = 0xFF;
99 break;
100 }
101 len -= sizeof(*data);
102 }
103
104 #else
105 // Program data - 4 step sequence
106 FLASH_CMD_WRITE(ROM+FLASH_Setup_Addr1, FLASH_Setup_Code1);
107 FLASH_CMD_WRITE(ROM+FLASH_Setup_Addr2, FLASH_Setup_Code2);
108 FLASH_CMD_WRITE(ROM+FLASH_Setup_Addr1, FLASH_Program);
109 FLASH_DATA_WRITE(addr, data);
110
111 state = array1;
112 new_state = array2;
113
114 FLASH_DATA_READ(addr, state);
115 timeout = 5000000; // how many retries?
116 while (true) {
117 int i;
118 int done = 1;
119
120 FLASH_DATA_READ(addr, new_state);
121 for (i = 0; i < CYGHWR_FLASH_INTERLEAVE; i++) {
122 if ((new_state[i] & FLASH_Busy) != (state[i] & FLASH_Busy)) {
123 done = 0;
124 if (new_state[i] & FLASH_Err)
125 fail = FLASH_Err;
126 }
127 }
128 // Switch state arrays
129 tmp_state = state;
130 state = new_state;
131 new_state = state;
132
133 if (done) break; // "toggle" stopped
134 if (fail) break; // device failure
135 if (--timeout == 0) break; // too many tries without success
136 }
137 FLASH_CMD_WRITE(ROM, FLASH_Reset);
138 // We have state in 'state' now, use 'new_state' to verify data.
139 for (i = 0; i < CYGHWR_FLASH_INTERLEAVE; i++) {
140 if (*addr++ != *data++) {
141 state[i] = FLASH_Drv_Verify_Err;
142 fail = FLASH_Drv_Verify_Err;
143 }
144 }
145 if (fail) break;
146
147 len -= sizeof(flash_data_t) * CYGHWR_FLASH_INTERLEAVE;
148 #endif
149 }
150
151 // Ideally, we'd want to return not only the failure code, but also
152 // the address/device that reported the error.
153 return fail;
154 }