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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
|
/* Copyright 2013-2014 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <libflash/libflash.h>
#include <libflash/libflash-priv.h>
#include "../libflash.c"
#include "../ecc.c"
#define __unused __attribute__((unused))
#define ERR(fmt...) fprintf(stderr, fmt)
/* Flash commands */
#define CMD_PP 0x02
#define CMD_READ 0x03
#define CMD_WRDI 0x04
#define CMD_RDSR 0x05
#define CMD_WREN 0x06
#define CMD_SE 0x20
#define CMD_RDSCUR 0x2b
#define CMD_BE32K 0x52
#define CMD_CE 0x60
#define CMD_RDID 0x9f
#define CMD_EN4B 0xb7
#define CMD_BE 0xd8
#define CMD_RDDPB 0xe0
#define CMD_RDSPB 0xe2
#define CMD_EX4B 0xe9
/* Flash status bits */
#define STAT_WIP 0x01
#define STAT_WEN 0x02
static uint8_t *sim_image;
static uint32_t sim_image_sz = 0x100000;
static uint32_t sim_index;
static uint32_t sim_addr;
static uint32_t sim_er_size;
static uint8_t sim_sr;
static bool sim_fl_4b;
static bool sim_ct_4b;
static enum sim_state {
sim_state_idle,
sim_state_rdid,
sim_state_rdsr,
sim_state_read_addr,
sim_state_read_data,
sim_state_write_addr,
sim_state_write_data,
sim_state_erase_addr,
sim_state_erase_done,
} sim_state;
/*
* Simulated flash & controller
*/
static int sim_start_cmd(uint8_t cmd)
{
if (sim_state != sim_state_idle) {
ERR("SIM: Command %02x in wrong state %d\n", cmd, sim_state);
return -1;
}
sim_index = 0;
sim_addr = 0;
switch(cmd) {
case CMD_RDID:
sim_state = sim_state_rdid;
break;
case CMD_RDSR:
sim_state = sim_state_rdsr;
break;
case CMD_EX4B:
sim_fl_4b = false;
break;
case CMD_EN4B:
sim_fl_4b = true;
break;
case CMD_WREN:
sim_sr |= STAT_WEN;
break;
case CMD_READ:
sim_state = sim_state_read_addr;
if (sim_ct_4b != sim_fl_4b)
ERR("SIM: 4b mode mismatch in READ !\n");
break;
case CMD_PP:
sim_state = sim_state_write_addr;
if (sim_ct_4b != sim_fl_4b)
ERR("SIM: 4b mode mismatch in PP !\n");
if (!(sim_sr & STAT_WEN))
ERR("SIM: PP without WEN, ignoring... \n");
break;
case CMD_SE:
case CMD_BE32K:
case CMD_BE:
if (sim_ct_4b != sim_fl_4b)
ERR("SIM: 4b mode mismatch in SE/BE !\n");
if (!(sim_sr & STAT_WEN))
ERR("SIM: SE/BE without WEN, ignoring... \n");
sim_state = sim_state_erase_addr;
switch(cmd) {
case CMD_SE: sim_er_size = 0x1000; break;
case CMD_BE32K: sim_er_size = 0x8000; break;
case CMD_BE: sim_er_size = 0x10000; break;
}
break;
case CMD_CE:
if (!(sim_sr & STAT_WEN)) {
ERR("SIM: CE without WEN, ignoring... \n");
break;
}
memset(sim_image, 0xff, sim_image_sz);
sim_sr |= STAT_WIP;
sim_sr &= ~STAT_WEN;
break;
default:
ERR("SIM: Unsupported command %02x\n", cmd);
return -1;
}
return 0;
}
static void sim_end_cmd(void)
{
/* For write and sector/block erase, set WIP & clear WEN here */
if (sim_state == sim_state_write_data) {
sim_sr |= STAT_WIP;
sim_sr &= ~STAT_WEN;
}
sim_state = sim_state_idle;
}
static bool sim_do_address(const uint8_t **buf, uint32_t *len)
{
uint8_t asize = sim_fl_4b ? 4 : 3;
const uint8_t *p = *buf;
while(*len) {
sim_addr = (sim_addr << 8) | *(p++);
*buf = p;
*len = *len - 1;
sim_index++;
if (sim_index >= asize)
return true;
}
return false;
}
static int sim_wbytes(const void *buf, uint32_t len)
{
const uint8_t *b = buf;
bool addr_complete;
again:
switch(sim_state) {
case sim_state_read_addr:
addr_complete = sim_do_address(&b, &len);
if (addr_complete) {
sim_state = sim_state_read_data;
sim_index = 0;
if (len)
goto again;
}
break;
case sim_state_write_addr:
addr_complete = sim_do_address(&b, &len);
if (addr_complete) {
sim_state = sim_state_write_data;
sim_index = 0;
if (len)
goto again;
}
break;
case sim_state_write_data:
if (!(sim_sr & STAT_WEN))
break;
while(len--) {
uint8_t c = *(b++);
if (sim_addr >= sim_image_sz) {
ERR("SIM: Write past end of flash\n");
return -1;
}
/* Flash write only clears bits */
sim_image[sim_addr] &= c;
sim_addr = (sim_addr & 0xffffff00) |
((sim_addr + 1) & 0xff);
}
break;
case sim_state_erase_addr:
if (!(sim_sr & STAT_WEN))
break;
addr_complete = sim_do_address(&b, &len);
if (addr_complete) {
memset(sim_image + sim_addr, 0xff, sim_er_size);
sim_sr |= STAT_WIP;
sim_sr &= ~STAT_WEN;
sim_state = sim_state_erase_done;
}
break;
default:
ERR("SIM: Write in wrong state %d\n", sim_state);
return -1;
}
return 0;
}
static int sim_rbytes(void *buf, uint32_t len)
{
uint8_t *b = buf;
switch(sim_state) {
case sim_state_rdid:
while(len--) {
switch(sim_index) {
case 0:
*(b++) = 0x55;
break;
case 1:
*(b++) = 0xaa;
break;
case 2:
*(b++) = 0x55;
break;
default:
ERR("SIM: RDID index %d\n", sim_index);
*(b++) = 0;
break;
}
sim_index++;
}
break;
case sim_state_rdsr:
while(len--) {
*(b++) = sim_sr;
if (sim_index > 0)
ERR("SIM: RDSR index %d\n", sim_index);
sim_index++;
/* If WIP was 1, clear it, ie, simulate write/erase
* completion
*/
sim_sr &= ~STAT_WIP;
}
break;
case sim_state_read_data:
while(len--) {
if (sim_addr >= sim_image_sz) {
ERR("SIM: Read past end of flash\n");
return -1;
}
*(b++) = sim_image[sim_addr++];
}
break;
default:
ERR("SIM: Read in wrong state %d\n", sim_state);
return -1;
}
return 0;
}
static int sim_send_addr(uint32_t addr)
{
const void *ap;
/* Layout address MSB first in memory */
addr = cpu_to_be32(addr);
/* Send the right amount of bytes */
ap = (char *)&addr;
if (sim_ct_4b)
return sim_wbytes(ap, 4);
else
return sim_wbytes(ap + 1, 3);
}
static int sim_cmd_rd(struct spi_flash_ctrl *ctrl __unused, uint8_t cmd,
bool has_addr, uint32_t addr, void *buffer,
uint32_t size)
{
int rc;
rc = sim_start_cmd(cmd);
if (rc)
goto bail;
if (has_addr) {
rc = sim_send_addr(addr);
if (rc)
goto bail;
}
if (buffer && size)
rc = sim_rbytes(buffer, size);
bail:
sim_end_cmd();
return rc;
}
static int sim_cmd_wr(struct spi_flash_ctrl *ctrl __unused, uint8_t cmd,
bool has_addr, uint32_t addr, const void *buffer,
uint32_t size)
{
int rc;
rc = sim_start_cmd(cmd);
if (rc)
goto bail;
if (has_addr) {
rc = sim_send_addr(addr);
if (rc)
goto bail;
}
if (buffer && size)
rc = sim_wbytes(buffer, size);
bail:
sim_end_cmd();
return rc;
}
static int sim_set_4b(struct spi_flash_ctrl *ctrl __unused, bool enable)
{
sim_ct_4b = enable;
return 0;
}
static int sim_read(struct spi_flash_ctrl *ctrl __unused, uint32_t pos,
void *buf, uint32_t len)
{
if (sim_ct_4b != sim_fl_4b)
ERR("SIM: 4b mode mismatch in autoread !\n");
if ((pos + len) < pos)
return -1;
if ((pos + len) > sim_image_sz)
return -1;
memcpy(buf, sim_image + pos, len);
return 0;
};
struct spi_flash_ctrl sim_ctrl = {
.cmd_wr = sim_cmd_wr,
.cmd_rd = sim_cmd_rd,
.set_4b = sim_set_4b,
.read = sim_read,
};
int main(void)
{
struct flash_chip *fl;
uint32_t total_size, erase_granule;
const char *name;
uint16_t *test;
int i, rc;
sim_image = malloc(sim_image_sz);
memset(sim_image, 0xff, sim_image_sz);
test = malloc(0x10000 * 2);
rc = flash_init(&sim_ctrl, &fl);
if (rc) {
ERR("flash_init failed with err %d\n", rc);
exit(1);
}
rc = flash_get_info(fl, &name, &total_size, &erase_granule);
if (rc) {
ERR("flash_get_info failed with err %d\n", rc);
exit(1);
}
/* Make up a test pattern */
for (i=0; i<0x10000;i++)
test[i] = cpu_to_be16(i);
/* Write 64k of stuff at 0 and at 128k */
printf("Writing test patterns...\n");
flash_smart_write(fl, 0, test, 0x10000);
flash_smart_write(fl, 0x20000, test, 0x10000);
/* Write "Hello world" straddling the 64k boundary */
#define HW "Hello World"
printf("Writing test string...\n");
flash_smart_write(fl, 0xfffc, HW, sizeof(HW));
/* Check result */
if (memcmp(sim_image + 0xfffc, HW, sizeof(HW))) {
ERR("Test string mismatch !\n");
exit(1);
}
printf("Test string pass\n");
if (memcmp(sim_image, test, 0xfffc)) {
ERR("Test pattern mismatch !\n");
exit(1);
}
printf("Test pattern pass\n");
flash_exit(fl);
return 0;
}
|