-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathflashlib.c
253 lines (190 loc) · 5.22 KB
/
flashlib.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/* FLASH routines for AM29F016 (16-MBit, 32 Sectors, 64Kbyte sector size) */
#include <stdio.h>
#include <stdlib.h>
#include <rtems.h>
#include "flashlib.h"
/* CPU IRQ Level macros */
#define IRQ_GLOBAL_PREPARE(level) rtems_interrupt_level level
#define IRQ_GLOBAL_DISABLE(level) rtems_interrupt_disable(level)
#define IRQ_GLOBAL_ENABLE(level) rtems_interrupt_enable(level)
/* Load one Byte, Force cache miss */
#define LOAD_BYTE(address) _load_byte(address)
static __inline__ unsigned char _load_byte(volatile unsigned char *adr)
{
unsigned char tmp;
asm(" lduba [%1]1, %0 "
: "=r" (tmp)
: "r" (adr)
);
return tmp;
}
#define FLASH_WAIT() {volatile int i = 100; while ( i > 0 ) i--; }
void *flashlib_init(struct flashlib_device *dev)
{
return dev;
}
void flashlib_reset(void *handle)
{
struct flashlib_device *dev = handle;
volatile unsigned char *start;
start = (volatile unsigned char *)dev->start;
/* Start Autoselect command */
start[0] = 0xF0;
/* Wait some time */
FLASH_WAIT();
}
int flashlib_info(void *handle, struct flashlib_dev_info *info)
{
struct flashlib_device *dev = handle;
volatile unsigned char *start;
int i;
flashlib_reset(handle);
start = (volatile unsigned char *)dev->start;
/* Start Autoselect command */
start[0x5555] = 0xAA;
start[0x2AAA] = 0x55;
start[0x5555] = 0x90;
/* Get FLASH Device Information */
info->manufacter = LOAD_BYTE(&start[0x0000]);
info->device = LOAD_BYTE(&start[0x0001]);
/* Get Sector Group Protection information. Four sectors per group */
for (i=0; i<dev->sector_count/4; i++) {
info->secgrp_protected[i] = LOAD_BYTE(&start[dev->sector_size*i*4 + 0x0002]);
}
/* Stop auto select command */
flashlib_reset(handle);
return 0;
}
/* Erase the complete FLASH chip */
int flashlib_erase_chip(void *handle)
{
struct flashlib_device *dev = handle;
volatile unsigned char *start;
int i;
printf("CHIP ERASE\n");
flashlib_reset(handle);
start = (volatile unsigned char *)dev->start;
/* Start Chip Erase command */
start[0x5555] = 0xAA;
start[0x2AAA] = 0x55;
start[0x5555] = 0x80;
start[0x5555] = 0xAA;
start[0x2AAA] = 0x55;
start[0x5555] = 0x10;
/* Wait some time */
FLASH_WAIT();
/* Check if erase chip is done */
i = 0;
while ( (LOAD_BYTE(&start[0]) & 0x80) == 0 ) {
printf("Waiting Chip Erase %d\n", i);
rtems_task_wake_after(50);
i++;
}
/* Stop command */
flashlib_reset(handle);
return 0;
}
/* Errase a Specific Sector */
int flashlib_erase_sector(void *handle, int sector_start)
{
struct flashlib_device *dev = handle;
volatile unsigned char *start;
int i;
printf("SECTOR %d ERASE\n",
((sector_start - dev->start) / dev->sector_size));
flashlib_reset(handle);
start = (volatile unsigned char *)dev->start;
/* Start Sector Erase command */
start[0x5555] = 0xAA;
start[0x2AAA] = 0x55;
start[0x5555] = 0x80;
start[0x5555] = 0xAA;
start[0x2AAA] = 0x55;
*(volatile unsigned char *)sector_start = 0x30;
/* Wait some time */
FLASH_WAIT();
/* Check if sector erase is done */
i = 0;
while ( (LOAD_BYTE((volatile unsigned char *)sector_start) & 0x80) == 0 ) {
printf("Waiting Sector Erase %d\n", i);
rtems_task_wake_after(10);
i++;
}
/* Stop command */
flashlib_reset(handle);
return 0;
}
/* Erase a range of FLASH */
int flashlib_erase(void *handle, int start, int end)
{
struct flashlib_device *dev = handle;
unsigned int sector_mask = dev->sector_size-1;
int sectcnt, i;
printf("RANGE ERASE: 0x%08x - 0x%08x\n", start, end);
if ( start > end )
return -1;
if ( start & sector_mask ) {
/* Start must be aligned to sector size */
return -2;
}
if ( end >= (dev->sector_count*dev->sector_size + dev->start) ) {
/* End of device */
return -3;
}
/* Calculate end sector */
end = end & ~sector_mask;
/* Calculate number of sectors to erase */
sectcnt = 1 + (end - start) / dev->sector_size;
if ( (start == dev->start) && (sectcnt == dev->sector_count) ) {
/* All chip is to be erased, call optimized function */
return flashlib_erase_chip(handle);
}
printf("ERASING %d SECTORS STARTING FROM 0x%08x\n", sectcnt, start);
/* Erase selected sectors */
for (i=0; i<sectcnt; i++) {
/* Erase one Sector */
if ( flashlib_erase_sector(handle, start) ) {
return -4;
}
start += dev->sector_size;
}
return 0;
}
/* Program a data buffer into FLASH memory of device, note that the FLASH must
* be erased before programmed.
*/
int flashlib_program(void *handle, unsigned int start, int length, char *data)
{
struct flashlib_device *dev = handle;
volatile unsigned char *sc, *curr, *end;
unsigned char byte;
printf("PROGRAM: 0x%08x - 0x%08x\n", start, start + length);
if ( dev->start > start ) {
return -1;
}
if ( (dev->start + (dev->sector_size*dev->sector_count)) < (start+length) ) {
return -2;
}
flashlib_reset(handle);
sc = (volatile unsigned char *)dev->start;
curr = (volatile unsigned char *)start;
end = curr + length;
while ( curr < end ) {
/* Start Program command */
sc[0x5555] = 0xAA;
sc[0x2AAA] = 0x55;
sc[0x5555] = 0xA0;
/* Write Byte */
byte = *data;
*curr = byte;
data++;
/* Check if byte program is done. DQ7 is inverse what is programmed */
while ( ((LOAD_BYTE(curr) ^ byte) & 0x80) != 0 ) {
;
}
curr++;
}
/* Stop command */
flashlib_reset(handle);
return 0;
}