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
|
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#include <errno.h>
#include <unistd.h>
#include <hal_timer.h>
#include <hal_cmd.h>
#include "inter.h"
#define NOR_CMD_BLK_LOCK_ALL (0x7E)
#define NOR_CMD_BLK_UNLOCK_ALL (0x98)
/* #define DEBUG_WRLOCK */
#ifdef DEBUG_WRLOCK
int skip_lock = 0;
#endif
int nor_wr_lock_init(struct nor_flash *nor)
{
if (nor->factory->init_lock)
return nor->factory->init_lock(nor);
return 0;
}
void nor_wr_lock_deinit(struct nor_flash *nor)
{
if (nor->factory->deinit_lock)
nor->factory->deinit_lock(nor);
}
bool nor_wr_islock(struct nor_flash *nor, unsigned int addr, unsigned int len)
{
unsigned int align_size;
unsigned int align_addr;
bool ret = false;
if (nor->factory->islock) {
while (len) {
/* the first and the last block (64K) is lock on sector (4K) unit */
if (addr >= nor->total_size - SZ_64K || addr < SZ_64K)
align_size = SZ_4K;
else
align_size = SZ_64K;
align_addr = ALIGN_DOWN(addr, align_size);
ret = nor->factory->islock(nor, align_addr, align_size);
if (ret)
break;
len -= MIN(align_size, len);
addr += MIN(align_size, len);
}
}
return ret;
}
int nor_wr_lock(struct nor_flash *nor, unsigned int addr, unsigned int len)
{
unsigned int align_size;
unsigned int align_addr;
int ret = 0;
#ifdef DEBUG_WRLOCK
if (skip_lock == 1) {
printf("skip handle lock\n");
return 0;
}
#endif
if (nor->factory->lock) {
while (len) {
/* the first and the last block (64K) is lock on sector (4K) unit */
if (addr >= nor->total_size - SZ_64K || addr < SZ_64K)
align_size = SZ_4K;
else
align_size = SZ_64K;
align_addr = ALIGN_DOWN(addr, align_size);
ret = nor->factory->lock(nor, align_addr, align_size);
if (ret)
break;
len -= MIN(align_size, len);
addr += MIN(align_size, len);
}
}
return ret;
}
int nor_wr_unlock(struct nor_flash *nor, unsigned int addr, unsigned int len)
{
unsigned int align_size;
unsigned int align_addr;
int ret = 0;
#ifdef DEBUG_WRLOCK
if (skip_lock == 1) {
printf("skip handle lock\n");
return 0;
}
#endif
if (nor->factory->unlock) {
while (len) {
/* the first and the last block (64K) is lock on sector (4K) unit */
if (addr >= nor->total_size - SZ_64K || addr < SZ_64K)
align_size = SZ_4K;
else
align_size = SZ_64K;
align_addr = ALIGN_DOWN(addr, align_size);
ret = nor->factory->unlock(nor, align_addr, align_size);
if (ret)
break;
len -= MIN(align_size, len);
addr += MIN(align_size, len);
}
}
return ret;
}
int nor_wr_unlock_all(struct nor_flash *nor)
{
int ret;
#ifdef DEBUG_WRLOCK
if (skip_lock == 1) {
printf("skip handle lock\n");
return 0;
}
#endif
ret = nor_write_enable();
if (ret)
return ret;
ret = nor_send_cmd(NOR_CMD_BLK_UNLOCK_ALL);
if (ret) {
SPINOR_ERR("unlock all block failed - %d\n", ret);
return ret;
}
return 0;
}
int nor_wr_lock_all(struct nor_flash *nor)
{
int ret;
#ifdef DEBUG_WRLOCK
if (skip_lock == 1) {
printf("skip handle lock\n");
return 0;
}
#endif
ret = nor_write_enable();
if (ret)
return ret;
ret = nor_send_cmd(NOR_CMD_BLK_LOCK_ALL);
if (ret) {
SPINOR_ERR("lock all block failed - %d\n", ret);
return ret;
}
return 0;
}
#ifdef DEBUG_WRLOCK
int cmd_skip_lock(int argc, char ** argv)
{
printf("skip lock\n");
skip_lock = 1;
}
FINSH_FUNCTION_EXPORT_CMD(cmd_skip_lock, skip_lock, skip lock)
int cmd_not_skip_lock(int argc, char ** argv)
{
printf("not skip lock\n");
skip_lock = 0;
}
FINSH_FUNCTION_EXPORT_CMD(cmd_not_skip_lock, not_skip_lock, not skip lock)
#endif
|