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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
|
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#include <errno.h>
#include <unistd.h>
#include <hal_timer.h>
#include "inter.h"
#define addr_to_blk(nor, addr) ((addr) / nor->blk_size)
#define addr_to_page(nor, addr) ((addr) / nor->page_size)
typedef struct {
#define CACHE_SIZE SZ_64K
char *buf;
#define INVALID_CACHE_ADDR ((unsigned int)-1)
unsigned int addr;
} cache_t;
struct nor_cache {
cache_t cache;
unsigned int last_write;
unsigned long *bitmap_page;
unsigned int page_cnt;
unsigned long *bitmap_blk;
unsigned int blk_cnt;
struct nor_flash *nor;
};
static struct nor_cache g_nor_cache;
hal_sem_t lock_nor_cache;
static inline int nor_lock_init(void)
{
lock_nor_cache = hal_sem_create(1);
if (!lock_nor_cache) {
SPINOR_ERR("create hal_sem lock for nor_flash failed\n");
return -1;
}
return 0;
}
static int nor_cache_lock(void)
{
return hal_sem_wait(lock_nor_cache);
}
static int nor_cache_trylock(void)
{
return hal_sem_trywait(lock_nor_cache);
}
static int nor_cache_unlock(void)
{
return hal_sem_post(lock_nor_cache);
}
static void clear_cache(cache_t *c)
{
SPINOR_DEBUG("clear cache addr 0x%x\n", c->addr);
memset(c->buf, 0xFF, CACHE_SIZE);
c->addr = INVALID_CACHE_ADDR;
}
static void delete_cache(cache_t *c)
{
free(c->buf);
c->buf = NULL;
c->addr = 0;
}
static int init_cache(cache_t *c)
{
c->buf = malloc(CACHE_SIZE);
if (!c->buf)
return -ENOMEM;
clear_cache(c);
return 0;
}
static int init_bitmap(struct nor_cache *nc)
{
struct nor_flash *nor = nc->nor;
nc->page_cnt = CACHE_SIZE / nor->page_size;
nc->bitmap_page = malloc(BITS_TO_LONGS(nc->page_cnt) * sizeof(long));
if (!nc->bitmap_page)
return -ENOMEM;
memset(nc->bitmap_page, 0, BITS_TO_LONGS(nc->page_cnt) * sizeof(long));
nc->blk_cnt = CACHE_SIZE / nor->blk_size;
nc->bitmap_blk = malloc(BITS_TO_LONGS(nc->blk_cnt) * sizeof(long));
if (!nc->blk_cnt)
goto free_page;
memset(nc->bitmap_blk, 0, BITS_TO_LONGS(nc->blk_cnt) * sizeof(long));
return 0;
free_page:
free(nc->bitmap_page);
return -ENOMEM;
}
static void delete_bitmap(struct nor_cache *nc)
{
free(nc->bitmap_blk);
nc->bitmap_blk = NULL;
free(nc->bitmap_page);
nc->bitmap_page = NULL;
}
int nor_cache_init(struct nor_flash *nor)
{
int ret = -ENOMEM;
struct nor_cache *nc = &g_nor_cache;
/* initialized before */
if (nc->cache.buf)
return -EBUSY;
ret = nor_lock_init();
if (ret)
return ret;
nor_cache_lock();
ret = init_cache(&nc->cache);
if (ret)
goto unlock;
nc->nor = nor;
ret = init_bitmap(nc);
if (ret)
goto free_cache;
nor_cache_unlock();
return 0;
/*
* free_bitmap:
* delete_bitmap(nc);
*/
free_cache:
delete_cache(&nc->cache);
unlock:
nor_cache_unlock();
return ret;
}
void nor_cache_exit(void)
{
struct nor_cache *nc = &g_nor_cache;
delete_cache(&nc->cache);
delete_bitmap(nc);
}
static int check_cache_addr(cache_t *c, unsigned int addr, unsigned int len)
{
if (c->addr == INVALID_CACHE_ADDR)
return -1;
/* start boundary */
if (ALIGN_DOWN(addr, CACHE_SIZE) != c->addr)
return -1;
/* end boundary */
if (ALIGN_DOWN(addr + len - 1, CACHE_SIZE) != c->addr)
return -1;
return 0;
}
static inline void set_cache_addr(cache_t *c, unsigned int addr)
{
c->addr = ALIGN_DOWN(addr, CACHE_SIZE);
SPINOR_DEBUG("set cache addr as 0x%x\n", c->addr);
}
static inline unsigned int get_addr_by_page(unsigned int page)
{
struct nor_cache *nc = &g_nor_cache;
struct nor_flash *nor = nc->nor;
cache_t *c = &nc->cache;
return c->addr + page * nor->page_size;
}
static inline unsigned int get_addr_by_blk(unsigned int blk)
{
struct nor_cache *nc = &g_nor_cache;
struct nor_flash *nor = nc->nor;
cache_t *c = &nc->cache;
return c->addr + blk * nor->blk_size;
}
static inline void set_bit(int nr, unsigned long *addr)
{
addr[nr / BITS_PER_LONG] |= 1UL << (nr % BITS_PER_LONG);
}
static __always_inline int test_bit(unsigned int nr, const unsigned long *addr)
{
return ((1UL << (nr % BITS_PER_LONG)) &
(((unsigned long *)addr)[nr / BITS_PER_LONG])) != 0;
}
static int nor_flush_erase(struct nor_cache *nc)
{
struct nor_flash *nor = nc->nor;
unsigned int start, end;
int ret;
for (end = start = 0; end < nc->blk_cnt; end++) {
/* we should do erase lazy to get more continuous erase block */
if (test_bit(end, nc->bitmap_blk))
continue;
/* continuous zero, do nothing */
if (start == end) {
start = end + 1;
continue;
}
SPINOR_DEBUG("flush erase: addr 0x%x blk cnt %u\n",
get_addr_by_blk(start), end - start);
ret = nor_erase(get_addr_by_blk(start), (end - start) * nor->blk_size);
if (ret)
return ret;
start = end + 1;
}
if (start != end) {
SPINOR_DEBUG("flush erase: addr 0x%x blk cnt %u\n",
get_addr_by_blk(start), end - start);
ret = nor_erase(get_addr_by_blk(start), (end - start) * nor->blk_size);
if (ret)
return ret;
}
memset(nc->bitmap_blk, 0, BITS_TO_LONGS(nc->blk_cnt) * sizeof(long));
return 0;
}
static int nor_flush_write(struct nor_cache *nc)
{
struct nor_flash *nor = nc->nor;
cache_t *c = &nc->cache;
unsigned int page, i;
char *buf = c->buf;
int ret;
for (i = 0; i < nc->page_cnt; i++) {
/* let start with (last_write page + 1) */
page = (i + nc->last_write + 1) % nc->page_cnt;
if (!test_bit(page, nc->bitmap_page))
continue;
SPINOR_DEBUG("flush write: addr 0x%x\n", get_addr_by_page(page));
ret = nor_write(get_addr_by_page(page),
buf + page * nor->page_size, nor->page_size);
if (ret)
return ret;
}
memset(nc->bitmap_page, 0, BITS_TO_LONGS(nc->page_cnt) * sizeof(long));
return 0;
}
static int nor_flush_cache(struct nor_cache *nc)
{
int ret;
if (nc->cache.addr == INVALID_CACHE_ADDR)
return 0;
ret = nor_flush_erase(nc);
if (ret)
return ret;
ret = nor_flush_write(nc);
if (ret)
return ret;
clear_cache(&nc->cache);
return 0;
}
int nor_cache_write(unsigned int addr, char *buf, unsigned int len)
{
struct nor_cache *nc = &g_nor_cache;
struct nor_flash *nor = nc->nor;
cache_t *c = &nc->cache;
char *pbuf;
unsigned int page;
int ret;
SPINOR_DEBUG("try to write addr 0x%x with size %u\n", addr, len);
if (addr % nor->page_size) {
SPINOR_ERR("addr %u must align to page size %u\n", addr, nor->page_size);
return -EINVAL;
}
if (len % nor->page_size) {
SPINOR_ERR("len %u must align to page size %u\n", len, nor->page_size);
return -EINVAL;
}
ret = nor_cache_lock();
if (ret)
return ret;
/* if over CACHE_SIZE, write roughly */
if (len > CACHE_SIZE ||
(addr - ALIGN_DOWN(addr, CACHE_SIZE) + len > CACHE_SIZE)) {
ret = nor_flush_cache(nc);
if (ret)
goto unlock;
ret = nor_write(addr, buf, len);
goto unlock;
}
ret = check_cache_addr(c, addr, len);
if (ret) {
SPINOR_DEBUG("write addr 0x%x len %d over cache addr 0x%x\n",
addr, len, c->addr);
/* if write to a new addr out of cache, just flush cache for new one */
ret = nor_flush_cache(nc);
if (ret)
goto unlock;
/* ready new cache for data */
set_cache_addr(c, addr);
}
pbuf = c->buf + (addr - c->addr);
page = addr_to_page(nor, addr - c->addr);
while (len) {
unsigned int size = min(len, nor->page_size);
memcpy(pbuf, buf, size);
SPINOR_DEBUG("write: mark page %d abs addr 0x%x\n", page,
get_addr_by_page(page));
set_bit(page, nc->bitmap_page);
/*
* The order of page to flush-write is very pivotal. On lfs,
* the last page before sync always meta data. We must ensure normal
* data to write to flash before meta data. So, we must save the
* last page and flush this page at last.
*/
nc->last_write = page;
page += 1;
pbuf += size;
buf += size;
len -= size;
}
ret = 0;
unlock:
nor_cache_unlock();
return ret;
}
int nor_cache_read(unsigned int addr, char *buf, unsigned int len)
{
struct nor_cache *nc = &g_nor_cache;
struct nor_flash *nor = nc->nor;
cache_t *c = &nc->cache;
char *pbuf;
unsigned int page, blk;
int ret;
SPINOR_DEBUG("try to read addr 0x%x with size %u\n", addr, len);
if (addr % nor->page_size) {
SPINOR_ERR("addr %u must align to page size %u\n", addr, nor->page_size);
return -EINVAL;
}
if (len % nor->page_size) {
SPINOR_ERR("len %u must align to page size %u\n", len, nor->page_size);
return -EINVAL;
}
ret = nor_cache_lock();
if (ret)
return ret;
/* if over CACHE_SIZE, write roughly */
if (len > CACHE_SIZE ||
(addr - ALIGN_DOWN(addr, CACHE_SIZE) + len > CACHE_SIZE)) {
ret = nor_flush_cache(nc);
if (ret)
goto unlock;
ret = nor_read(addr, buf, len);
goto unlock;
}
ret = check_cache_addr(c, addr, len);
if (ret) {
ret = nor_read(addr, buf, len);
goto unlock;
}
pbuf = c->buf + (addr - c->addr);
page = addr_to_page(nor, addr - c->addr);
while (len) {
unsigned int size = min(len, nor->page_size);
if (test_bit(page, nc->bitmap_page)) {
SPINOR_DEBUG("read match cache page %d addr 0x%x\n", page,
get_addr_by_page(page));
memcpy(buf, pbuf, size);
} else {
blk = addr_to_blk(nor, addr - c->addr);
if (test_bit(blk, nc->bitmap_blk)) {
SPINOR_DEBUG("read match cache erase blk 0x%x addr 0x%x\n",
blk, get_addr_by_page(page));
memset(buf, 0xFF, size);
} else {
SPINOR_DEBUG("read not match cache addr 0x%x\n",
get_addr_by_page(page));
ret = nor_read(get_addr_by_page(page), buf, size);
if (ret)
goto unlock;
}
}
page += 1;
pbuf += size;
buf += size;
len -= size;
}
ret = 0;
unlock:
nor_cache_unlock();
return ret;
}
int nor_cache_sync(void)
{
struct nor_cache *nc = &g_nor_cache;
int ret;
SPINOR_DEBUG("try to sync nor cache\n");
ret = nor_cache_trylock();
if (ret) {
SPINOR_DEBUG("trylock fail, skip sync nor cache\n");
return ret;
}
ret = nor_flush_cache(nc);
nor_cache_unlock();
return ret? -1 : 0;
}
int nor_cache_erase(unsigned int addr, unsigned int len)
{
struct nor_cache *nc = &g_nor_cache;
struct nor_flash *nor = nc->nor;
cache_t *c = &nc->cache;
unsigned int blk;
int ret;
SPINOR_DEBUG("try to erase addr 0x%x with size %u\n", addr, len);
if (addr % nor->blk_size) {
SPINOR_ERR("addr %u must align to blk size %u\n", addr, nor->blk_size);
return -EINVAL;
}
if (len % nor->blk_size) {
SPINOR_ERR("len %u must align to blk size %u\n", len, nor->blk_size);
return -EINVAL;
}
ret = nor_cache_lock();
if (ret)
return ret;
/* if over CACHE_SIZE, erase roughly */
if (len > CACHE_SIZE ||
(addr - ALIGN_DOWN(addr, CACHE_SIZE) + len > CACHE_SIZE)) {
/* just flush earse operation */
ret = nor_flush_erase(nc);
if (ret)
goto unlock;
ret = nor_erase(addr, len);
goto unlock;
}
ret = check_cache_addr(c, addr, len);
if (ret) {
SPINOR_DEBUG("erase addr 0x%x len %d over cache addr 0x%x\n",
addr, len, c->addr);
/* if erase to a new addr out of cache, just flush cache for new one */
ret = nor_flush_cache(nc);
if (ret)
goto unlock;
/* ready new cache for data */
set_cache_addr(c, addr);
}
blk = addr_to_blk(nor, addr - c->addr);
for (; len; len -= nor->blk_size, blk++) {
SPINOR_DEBUG("erase: mark blk %d addr 0x%x\n", blk, get_addr_by_blk(blk));
set_bit(blk, nc->bitmap_blk);
}
ret = 0;
unlock:
nor_cache_unlock();
return ret;
}
|