blob: f4a386f10b62c6999cfd00c1cf5d4982a9411e9d [file] [log] [blame]
David Hendricksf7924d12010-06-10 21:26:44 -07001#include <stdlib.h>
2#include <string.h>
3
4#include "flash.h"
5#include "flashchips.h"
6#include "chipdrivers.h"
7
8/*
9 * The following procedures rely on look-up tables to match the user-specified
10 * range with the chip's supported ranges. This turned out to be the most
11 * elegant approach since diferent flash chips use different levels of
12 * granularity and methods to determine protected ranges. In other words,
13 * be stupid and simple since clever arithmetic will not for many chips.
14 */
15
16struct wp_range {
17 unsigned int start; /* starting address */
18 unsigned int len; /* len */
19};
20
21enum bit_state {
22 OFF = 0,
23 ON = 1,
24 X = 0 /* don't care */
25};
26
27struct w25q_range {
28 enum bit_state sec; /* if 1, bp[2:0] describe sectors */
29 enum bit_state tb; /* top/bottom select */
30 unsigned short int bp : 3; /* block protect bitfield */
31 struct wp_range range;
32};
33
David Hendricksac72e362010-08-16 18:20:03 -070034static struct w25q_range mx25l3205d_ranges[] = {
35 { X, 0, 0, {0, 0} }, /* none */
36 { X, 0, 0x1, {0x3f0000, 64 * 1024} },
37 { X, 0, 0x2, {0x3e0000, 128 * 1024} },
38 { X, 0, 0x3, {0x3c0000, 256 * 1024} },
39 { X, 0, 0x4, {0x380000, 512 * 1024} },
40 { X, 0, 0x5, {0x300000, 1024 * 1024} },
41 { X, 0, 0x6, {0x200000, 2048 * 1024} },
42 { X, 0, 0x7, {0x000000, 4096 * 1024} },
43
44 { X, 1, 0x0, {0x000000, 4096 * 1024} },
45 { X, 1, 0x1, {0x000000, 2048 * 1024} },
46 { X, 1, 0x2, {0x000000, 3072 * 1024} },
47 { X, 1, 0x3, {0x000000, 3584 * 1024} },
48 { X, 1, 0x4, {0x000000, 3840 * 1024} },
49 { X, 1, 0x5, {0x000000, 3968 * 1024} },
50 { X, 1, 0x6, {0x000000, 4032 * 1024} },
51 { X, 1, 0x7, {0x000000, 4096 * 1024} },
52};
53
David Hendricksf7924d12010-06-10 21:26:44 -070054static struct w25q_range w25q16_ranges[] = {
55 { X, X, 0, {0, 0} }, /* none */
56 { 0, 0, 0x1, {0x1f0000, 64 * 1024} },
57 { 0, 0, 0x2, {0x1e0000, 128 * 1024} },
58 { 0, 0, 0x3, {0x1c0000, 256 * 1024} },
59 { 0, 0, 0x4, {0x180000, 512 * 1024} },
60 { 0, 0, 0x5, {0x100000, 1024 * 1024} },
61
62 { 0, 1, 0x1, {0x000000, 64 * 1024} },
63 { 0, 1, 0x2, {0x000000, 128 * 1024} },
64 { 0, 1, 0x3, {0x000000, 256 * 1024} },
65 { 0, 1, 0x4, {0x000000, 512 * 1024} },
66 { 0, 1, 0x5, {0x000000, 1024 * 1024} },
67 { X, X, 0x6, {0x000000, 2048 * 1024} },
68 { X, X, 0x7, {0x000000, 2048 * 1024} },
69
70 { 1, 0, 0x1, {0x1ff000, 4 * 1024} },
71 { 1, 0, 0x2, {0x1fe000, 8 * 1024} },
72 { 1, 0, 0x3, {0x1fc000, 16 * 1024} },
73 { 1, 0, 0x4, {0x1f8000, 32 * 1024} },
74 { 1, 0, 0x5, {0x1f8000, 32 * 1024} },
75
76 { 1, 1, 0x1, {0x000000, 4 * 1024} },
77 { 1, 1, 0x2, {0x000000, 8 * 1024} },
78 { 1, 1, 0x3, {0x000000, 16 * 1024} },
79 { 1, 1, 0x4, {0x000000, 32 * 1024} },
80 { 1, 1, 0x5, {0x000000, 32 * 1024} },
81};
82
83static struct w25q_range w25q32_ranges[] = {
84 { X, X, 0, {0, 0} }, /* none */
85 { 0, 0, 0x1, {0x3f0000, 64 * 1024} },
86 { 0, 0, 0x2, {0x3e0000, 128 * 1024} },
87 { 0, 0, 0x3, {0x3c0000, 256 * 1024} },
88 { 0, 0, 0x4, {0x380000, 512 * 1024} },
89 { 0, 0, 0x5, {0x300000, 1024 * 1024} },
David Hendricks05653ff2010-06-15 16:05:12 -070090 { 0, 0, 0x6, {0x200000, 2048 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -070091
92 { 0, 1, 0x1, {0x000000, 64 * 1024} },
93 { 0, 1, 0x2, {0x000000, 128 * 1024} },
94 { 0, 1, 0x3, {0x000000, 256 * 1024} },
95 { 0, 1, 0x4, {0x000000, 512 * 1024} },
96 { 0, 1, 0x5, {0x000000, 1024 * 1024} },
97 { 0, 1, 0x6, {0x000000, 2048 * 1024} },
98 { X, X, 0x7, {0x000000, 4096 * 1024} },
99
100 { 1, 0, 0x1, {0x3ff000, 4 * 1024} },
101 { 1, 0, 0x2, {0x3fe000, 8 * 1024} },
102 { 1, 0, 0x3, {0x3fc000, 16 * 1024} },
103 { 1, 0, 0x4, {0x3f8000, 32 * 1024} },
104 { 1, 0, 0x5, {0x3f8000, 32 * 1024} },
105
106 { 1, 1, 0x1, {0x000000, 4 * 1024} },
107 { 1, 1, 0x2, {0x000000, 8 * 1024} },
108 { 1, 1, 0x3, {0x000000, 16 * 1024} },
109 { 1, 1, 0x4, {0x000000, 32 * 1024} },
110 { 1, 1, 0x5, {0x000000, 32 * 1024} },
111};
112
113static struct w25q_range w25q80_ranges[] = {
114 { X, X, 0, {0, 0} }, /* none */
115 { 0, 0, 0x1, {0x0f0000, 64 * 1024} },
116 { 0, 0, 0x2, {0x0e0000, 128 * 1024} },
117 { 0, 0, 0x3, {0x0c0000, 256 * 1024} },
118 { 0, 0, 0x4, {0x080000, 512 * 1024} },
119
120 { 0, 1, 0x1, {0x000000, 64 * 1024} },
121 { 0, 1, 0x2, {0x000000, 128 * 1024} },
122 { 0, 1, 0x3, {0x000000, 256 * 1024} },
123 { 0, 1, 0x4, {0x000000, 512 * 1024} },
David Hendricks05653ff2010-06-15 16:05:12 -0700124 { X, X, 0x6, {0x000000, 1024 * 1024} },
125 { X, X, 0x7, {0x000000, 1024 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700126
127 { 1, 0, 0x1, {0x1ff000, 4 * 1024} },
128 { 1, 0, 0x2, {0x1fe000, 8 * 1024} },
129 { 1, 0, 0x3, {0x1fc000, 16 * 1024} },
130 { 1, 0, 0x4, {0x1f8000, 32 * 1024} },
131 { 1, 0, 0x5, {0x1f8000, 32 * 1024} },
132
133 { 1, 1, 0x1, {0x000000, 4 * 1024} },
134 { 1, 1, 0x2, {0x000000, 8 * 1024} },
135 { 1, 1, 0x3, {0x000000, 16 * 1024} },
136 { 1, 1, 0x4, {0x000000, 32 * 1024} },
137 { 1, 1, 0x5, {0x000000, 32 * 1024} },
138};
139
David Hendricks2c4a76c2010-06-28 14:00:43 -0700140static struct w25q_range w25q64_ranges[] = {
141 { X, X, 0, {0, 0} }, /* none */
142
143 { 0, 0, 0x1, {0x7e0000, 128 * 1024} },
144 { 0, 0, 0x2, {0x7c0000, 256 * 1024} },
145 { 0, 0, 0x3, {0x780000, 512 * 1024} },
146 { 0, 0, 0x4, {0x700000, 1024 * 1024} },
147 { 0, 0, 0x5, {0x600000, 2048 * 1024} },
148 { 0, 0, 0x6, {0x400000, 4096 * 1024} },
149
150 { 0, 1, 0x1, {0x000000, 128 * 1024} },
151 { 0, 1, 0x2, {0x000000, 256 * 1024} },
152 { 0, 1, 0x3, {0x000000, 512 * 1024} },
153 { 0, 1, 0x4, {0x000000, 1024 * 1024} },
154 { 0, 1, 0x5, {0x000000, 2048 * 1024} },
155 { 0, 1, 0x6, {0x000000, 4096 * 1024} },
156 { X, X, 0x7, {0x000000, 8192 * 1024} },
157
158 { 1, 0, 0x1, {0x7ff000, 4 * 1024} },
159 { 1, 0, 0x2, {0x7fe000, 8 * 1024} },
160 { 1, 0, 0x3, {0x7fc000, 16 * 1024} },
161 { 1, 0, 0x4, {0x7f8000, 32 * 1024} },
162 { 1, 0, 0x5, {0x7f8000, 32 * 1024} },
163
164 { 1, 1, 0x1, {0x000000, 4 * 1024} },
165 { 1, 1, 0x2, {0x000000, 8 * 1024} },
166 { 1, 1, 0x3, {0x000000, 16 * 1024} },
167 { 1, 1, 0x4, {0x000000, 32 * 1024} },
168 { 1, 1, 0x5, {0x000000, 32 * 1024} },
169};
170
David Hendricks470ca952010-08-13 14:01:53 -0700171struct w25q_range w25x40_ranges[] = {
172 { X, X, 0, {0, 0} }, /* none */
173 { 0, 0, 0x1, {0x070000, 64 * 1024} },
174 { 0, 0, 0x2, {0x060000, 128 * 1024} },
175 { 0, 0, 0x3, {0x040000, 256 * 1024} },
176 { 0, 1, 0x1, {0x000000, 64 * 1024} },
177 { 0, 1, 0x2, {0x000000, 128 * 1024} },
178 { 0, 1, 0x3, {0x000000, 256 * 1024} },
179 { 0, X, 0x4, {0x000000, 512 * 1024} },
180};
181
David Hendricksf7924d12010-06-10 21:26:44 -0700182struct w25q_status {
183 /* this maps to register layout -- do not change ordering */
184 unsigned char busy : 1;
185 unsigned char wel : 1;
186 unsigned char bp0 : 1;
187 unsigned char bp1 : 1;
188 unsigned char bp2 : 1;
189 unsigned char tb : 1;
190 unsigned char sec : 1;
191 unsigned char srp0 : 1;
192 /* FIXME: what about the second status register? */
193// unsigned char srp1 : 1;
194// unsigned char qe : 1;
195} __attribute__ ((packed));
196
David Hendricksd494b0a2010-08-16 16:28:50 -0700197int wp_get_status(const struct flashchip *flash,
198 unsigned int start, unsigned int len,
199 struct w25q_status *status)
David Hendricksf7924d12010-06-10 21:26:44 -0700200{
David Hendricksf7924d12010-06-10 21:26:44 -0700201 struct w25q_range *w25q_ranges;
202 int i, num_entries = 0;
203 int tmp = 0, range_found = 0;
204
David Hendricksd494b0a2010-08-16 16:28:50 -0700205 switch (flash->manufacture_id) {
206 case WINBOND_NEX_ID:
207 switch(flash->model_id) {
208 case W_25X40:
209 w25q_ranges = w25x40_ranges;
210 num_entries = ARRAY_SIZE(w25x40_ranges);
211 break;
212 case W_25Q80:
213 w25q_ranges = w25q80_ranges;
214 num_entries = ARRAY_SIZE(w25q80_ranges);
215 break;
216 case W_25Q16:
217 w25q_ranges = w25q16_ranges;
218 num_entries = ARRAY_SIZE(w25q16_ranges);
219 break;
220 case W_25Q32:
221 w25q_ranges = w25q32_ranges;
222 num_entries = ARRAY_SIZE(w25q32_ranges);
223 break;
224 case W_25Q64:
225 w25q_ranges = w25q64_ranges;
226 num_entries = ARRAY_SIZE(w25q64_ranges);
227 break;
228 default:
229 msg_cerr("%s() %d: WINBOND flash chip mismatch (0x%04x)"
230 ", aborting\n", __func__, __LINE__,
231 flash->model_id);
232 return -1;
233 }
David Hendricks2c4a76c2010-06-28 14:00:43 -0700234 break;
David Hendricksac72e362010-08-16 18:20:03 -0700235 case MX_ID:
236 switch (flash->model_id) {
237 case MX_25L3205:
238 w25q_ranges = mx25l3205d_ranges;
239 num_entries = ARRAY_SIZE(mx25l3205d_ranges);
240 break;
241 default:
242 msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)"
243 ", aborting\n", __func__, __LINE__,
244 flash->model_id);
245 return -1;
246 }
247 break;
David Hendricksf7924d12010-06-10 21:26:44 -0700248 default:
David Hendricksd494b0a2010-08-16 16:28:50 -0700249 msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
250 __func__, flash->manufacture_id);
David Hendricksf7924d12010-06-10 21:26:44 -0700251 return -1;
252 }
253
254 memset(&status, 0, sizeof(status));
255 tmp = spi_read_status_register();
256 memcpy(&status, &tmp, 1);
257 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
258
259 for (i = 0; i < num_entries; i++) {
260 struct wp_range *r = &w25q_ranges[i].range;
261
262 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
263 start, len, r->start, r->len);
264 if ((start == r->start) && (len == r->len)) {
David Hendricksd494b0a2010-08-16 16:28:50 -0700265 status->bp0 = w25q_ranges[i].bp & 1;
266 status->bp1 = w25q_ranges[i].bp >> 1;
267 status->bp2 = w25q_ranges[i].bp >> 2;
268 status->tb = w25q_ranges[i].tb;
269 status->sec = w25q_ranges[i].sec;
David Hendricksf7924d12010-06-10 21:26:44 -0700270
271 range_found = 1;
272 break;
273 }
274 }
275
276 if (!range_found) {
277 msg_cerr("matching range not found\n");
278 return -1;
279 }
David Hendricksd494b0a2010-08-16 16:28:50 -0700280 return 0;
281}
282
283static int w25_set_range(struct flashchip *flash,
284 unsigned int start, unsigned int len)
285{
286 struct w25q_status status;
287 int tmp;
288
289 memset(&status, 0, sizeof(status));
290 tmp = spi_read_status_register();
291 memcpy(&status, &tmp, 1);
292 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
293
294 if (wp_get_status(flash, start, len, &status)) return -1;
David Hendricksf7924d12010-06-10 21:26:44 -0700295
296 msg_cdbg("status.busy: %x\n", status.busy);
297 msg_cdbg("status.wel: %x\n", status.wel);
298 msg_cdbg("status.bp0: %x\n", status.bp0);
299 msg_cdbg("status.bp1: %x\n", status.bp1);
300 msg_cdbg("status.bp2: %x\n", status.bp2);
301 msg_cdbg("status.tb: %x\n", status.tb);
302 msg_cdbg("status.sec: %x\n", status.sec);
303 msg_cdbg("status.srp0: %x\n", status.srp0);
304
305 memcpy(&tmp, &status, sizeof(status));
David Hendricks82fd8ae2010-08-04 14:34:54 -0700306 spi_write_status_register(flash, tmp);
David Hendricksf7924d12010-06-10 21:26:44 -0700307 msg_cdbg("%s: new status: 0x%02x\n",
308 __func__, spi_read_status_register());
309
310 return 0;
311}
312
313static int w25_enable_writeprotect(struct flashchip *flash)
314{
315 struct w25q_status status;
316 int tmp = 0;
317
318 memset(&status, 0, sizeof(status));
319 tmp = spi_read_status_register();
320 memcpy(&status, &tmp, 1);
321 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
322
323 status.srp0 = 1;
324 memcpy(&tmp, &status, sizeof(status));
325 spi_write_status_enable();
David Hendricks82fd8ae2010-08-04 14:34:54 -0700326 spi_write_status_register(flash, tmp);
David Hendricksf7924d12010-06-10 21:26:44 -0700327 msg_cdbg("%s: new status: 0x%02x\n",
328 __func__, spi_read_status_register());
329
330 return 0;
331}
332
333struct wp wp_w25 = {
334 .set_range = w25_set_range,
335 .enable = w25_enable_writeprotect,
336};