comparison packages/io/flash/current/src/flash.c @ 115:6ed91473a1cd ecos-sw-2000-08-21

Merge from eCos master repository on 2000-08-21-22:40:54-BST
author jlarmour
date Fri, 25 Aug 2000 17:32:38 +0000
parents
children bd1a71e3e476
comparison
equal deleted inserted replaced
114:5ad2b71d525e 115:6ed91473a1cd
1 //==========================================================================
2 //
3 // flash.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
36 // Date: 2000-07-26
37 // Purpose:
38 // Description:
39 //
40 //####DESCRIPTIONEND####
41 //
42 //==========================================================================
43
44 #include <cyg/hal/hal_arch.h>
45 #include <cyg/hal/hal_intr.h>
46 #include <cyg/hal/hal_cache.h>
47
48 #define _FLASH_PRIVATE_
49 #include <cyg/io/flash.h>
50
51 struct flash_info flash_info;
52
53 int
54 flash_init(void *work_space, int work_space_size)
55 {
56 int err;
57
58 if (flash_info.init) return FLASH_ERR_OK;
59 flash_info.work_space = work_space;
60 flash_info.work_space_size = flash_info.work_space_size;
61 if ((err = flash_hwr_init()) != FLASH_ERR_OK) {
62 return err;
63 }
64 flash_info.block_mask = ~(flash_info.block_size-1);
65 flash_info.init = 1;
66 return FLASH_ERR_OK;
67 }
68
69 int
70 flash_verify_addr(void *target)
71 {
72 if (!flash_info.init) {
73 return FLASH_ERR_NOT_INIT;
74 }
75 if (((unsigned long)target >= (unsigned long)flash_info.start) &&
76 ((unsigned long)target < (unsigned long)flash_info.end)) {
77 return FLASH_ERR_OK;
78 } else {
79 return FLASH_ERR_INVALID;
80 }
81 }
82
83 int
84 flash_get_limits(void *target, void **start, void **end)
85 {
86 if (!flash_info.init) {
87 return FLASH_ERR_NOT_INIT;
88 }
89 *start = flash_info.start;
90 *end = flash_info.end;
91 return FLASH_ERR_OK;
92 }
93
94 int
95 flash_get_block_info(int *block_size, int *blocks)
96 {
97 if (!flash_info.init) {
98 return FLASH_ERR_NOT_INIT;
99 }
100 *block_size = flash_info.block_size;
101 *blocks = flash_info.blocks;
102 return FLASH_ERR_OK;
103 }
104
105 int
106 flash_erase(void *addr, int len, void **err_addr)
107 {
108 unsigned short *block, *end_addr;
109 int stat = 0;
110 extern char flash_erase_block, flash_erase_block_end;
111 int code_len;
112 typedef int code_fun(unsigned short *);
113 code_fun *_flash_erase_block;
114
115 if (!flash_info.init) {
116 return FLASH_ERR_NOT_INIT;
117 }
118
119 // Copy 'erase' code to RAM for execution
120 code_len = (unsigned long)&flash_erase_block_end - (unsigned long)&flash_erase_block;
121 _flash_erase_block = (code_fun *)flash_info.work_space;
122 memcpy(_flash_erase_block, &flash_erase_block, code_len);
123 HAL_DCACHE_SYNC(); // Should guarantee this code will run
124
125 block = (unsigned short *)((unsigned long)addr & flash_info.block_mask);
126 end_addr = (unsigned short *)((unsigned long)addr+len);
127
128 printf("... Erase from %p-%p: ", block, end_addr);
129
130 while (block < end_addr) {
131 stat = (*_flash_erase_block)(block);
132 stat = flash_hwr_map_error(stat);
133 if (stat) {
134 *err_addr = (void *)block;
135 break;
136 }
137 block += flash_info.block_size / sizeof(*block);
138 printf(".");
139 }
140 printf("\n");
141 return (stat);
142 }
143
144 int
145 flash_program(void *_addr, void *_data, int len, void **err_addr)
146 {
147 int stat = 0;
148 int code_len, size;
149 extern char flash_program_buf, flash_program_buf_end;
150 typedef int code_fun(unsigned short *, unsigned short *, int);
151 code_fun *_flash_program_buf;
152 unsigned short *addr = (unsigned short *)_addr;
153 unsigned short *data = (unsigned short *)_data;
154
155 if (!flash_info.init) {
156 return FLASH_ERR_NOT_INIT;
157 }
158
159 // Copy 'program' code to RAM for execution
160 code_len = (unsigned long)&flash_program_buf_end - (unsigned long)&flash_program_buf;
161 _flash_program_buf = (code_fun *)flash_info.work_space;
162 memcpy(_flash_program_buf, &flash_program_buf, code_len);
163 HAL_DCACHE_SYNC(); // Should guarantee this code will run
164
165 printf("... Program from %p-%p at %p: ", data, ((unsigned long)data)+len, addr);
166
167 while (len > 0) {
168 size = len;
169 if (size > flash_info.block_size) size = flash_info.block_size;
170 stat = (*_flash_program_buf)(addr, data, size);
171 stat = flash_hwr_map_error(stat);
172 if (stat) {
173 *err_addr = (void *)addr;
174 break;
175 }
176 printf(".");
177 len -= size;
178 addr += size/sizeof(*addr);
179 data += size/sizeof(*data);
180 }
181 printf("\n");
182 return (stat);
183 }
184
185 char *
186 flash_errmsg(int err)
187 {
188 switch (err) {
189 case FLASH_ERR_OK:
190 return "No error - operation complete";
191 case FLASH_ERR_INVALID:
192 return "Invalid FLASH address";
193 case FLASH_ERR_ERASE:
194 return "Error trying to erase";
195 case FLASH_ERR_LOCK:
196 return "Error trying to lock/unlock";
197 case FLASH_ERR_PROGRAM:
198 return "Error trying to program";
199 case FLASH_ERR_PROTOCOL:
200 return "Generic error";
201 case FLASH_ERR_PROTECT:
202 return "Device/region is write-protected";
203 case FLASH_ERR_NOT_INIT:
204 return "FLASH sub-system not initialized";
205 default:
206 return "Unknown error";
207 }
208 }