blob: 8e542ceebe44f8d144ba0274eb38b6cee6a6d00d [file] [log] [blame]
David Hendricksd1c55d72010-08-24 15:14:19 -07001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2010 Google Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
David Hendricksf7924d12010-06-10 21:26:44 -070021#include <stdlib.h>
22#include <string.h>
23
24#include "flash.h"
25#include "flashchips.h"
26#include "chipdrivers.h"
27
28/*
29 * The following procedures rely on look-up tables to match the user-specified
30 * range with the chip's supported ranges. This turned out to be the most
31 * elegant approach since diferent flash chips use different levels of
32 * granularity and methods to determine protected ranges. In other words,
33 * be stupid and simple since clever arithmetic will not for many chips.
34 */
35
36struct wp_range {
37 unsigned int start; /* starting address */
38 unsigned int len; /* len */
39};
40
41enum bit_state {
42 OFF = 0,
43 ON = 1,
44 X = 0 /* don't care */
45};
46
47struct w25q_range {
48 enum bit_state sec; /* if 1, bp[2:0] describe sectors */
49 enum bit_state tb; /* top/bottom select */
50 unsigned short int bp : 3; /* block protect bitfield */
51 struct wp_range range;
52};
53
David Hendricks57566ed2010-08-16 18:24:45 -070054struct w25q_range en25f40_ranges[] = {
55 { X, X, 0, {0, 0} }, /* none */
56 { 0, 0, 0x1, {0x000000, 504 * 1024} },
57 { 0, 0, 0x2, {0x000000, 496 * 1024} },
58 { 0, 0, 0x3, {0x000000, 480 * 1024} },
59 { 0, 0, 0x4, {0x000000, 448 * 1024} },
60 { 0, 0, 0x5, {0x000000, 384 * 1024} },
61 { 0, 0, 0x6, {0x000000, 256 * 1024} },
62 { 0, 0, 0x7, {0x000000, 512 * 1024} },
63};
64
David Hendricksac72e362010-08-16 18:20:03 -070065static struct w25q_range mx25l3205d_ranges[] = {
66 { X, 0, 0, {0, 0} }, /* none */
67 { X, 0, 0x1, {0x3f0000, 64 * 1024} },
68 { X, 0, 0x2, {0x3e0000, 128 * 1024} },
69 { X, 0, 0x3, {0x3c0000, 256 * 1024} },
70 { X, 0, 0x4, {0x380000, 512 * 1024} },
71 { X, 0, 0x5, {0x300000, 1024 * 1024} },
72 { X, 0, 0x6, {0x200000, 2048 * 1024} },
73 { X, 0, 0x7, {0x000000, 4096 * 1024} },
74
75 { X, 1, 0x0, {0x000000, 4096 * 1024} },
76 { X, 1, 0x1, {0x000000, 2048 * 1024} },
77 { X, 1, 0x2, {0x000000, 3072 * 1024} },
78 { X, 1, 0x3, {0x000000, 3584 * 1024} },
79 { X, 1, 0x4, {0x000000, 3840 * 1024} },
80 { X, 1, 0x5, {0x000000, 3968 * 1024} },
81 { X, 1, 0x6, {0x000000, 4032 * 1024} },
82 { X, 1, 0x7, {0x000000, 4096 * 1024} },
83};
84
David Hendricksf7924d12010-06-10 21:26:44 -070085static struct w25q_range w25q16_ranges[] = {
86 { X, X, 0, {0, 0} }, /* none */
87 { 0, 0, 0x1, {0x1f0000, 64 * 1024} },
88 { 0, 0, 0x2, {0x1e0000, 128 * 1024} },
89 { 0, 0, 0x3, {0x1c0000, 256 * 1024} },
90 { 0, 0, 0x4, {0x180000, 512 * 1024} },
91 { 0, 0, 0x5, {0x100000, 1024 * 1024} },
92
93 { 0, 1, 0x1, {0x000000, 64 * 1024} },
94 { 0, 1, 0x2, {0x000000, 128 * 1024} },
95 { 0, 1, 0x3, {0x000000, 256 * 1024} },
96 { 0, 1, 0x4, {0x000000, 512 * 1024} },
97 { 0, 1, 0x5, {0x000000, 1024 * 1024} },
98 { X, X, 0x6, {0x000000, 2048 * 1024} },
99 { X, X, 0x7, {0x000000, 2048 * 1024} },
100
101 { 1, 0, 0x1, {0x1ff000, 4 * 1024} },
102 { 1, 0, 0x2, {0x1fe000, 8 * 1024} },
103 { 1, 0, 0x3, {0x1fc000, 16 * 1024} },
104 { 1, 0, 0x4, {0x1f8000, 32 * 1024} },
105 { 1, 0, 0x5, {0x1f8000, 32 * 1024} },
106
107 { 1, 1, 0x1, {0x000000, 4 * 1024} },
108 { 1, 1, 0x2, {0x000000, 8 * 1024} },
109 { 1, 1, 0x3, {0x000000, 16 * 1024} },
110 { 1, 1, 0x4, {0x000000, 32 * 1024} },
111 { 1, 1, 0x5, {0x000000, 32 * 1024} },
112};
113
114static struct w25q_range w25q32_ranges[] = {
115 { X, X, 0, {0, 0} }, /* none */
116 { 0, 0, 0x1, {0x3f0000, 64 * 1024} },
117 { 0, 0, 0x2, {0x3e0000, 128 * 1024} },
118 { 0, 0, 0x3, {0x3c0000, 256 * 1024} },
119 { 0, 0, 0x4, {0x380000, 512 * 1024} },
120 { 0, 0, 0x5, {0x300000, 1024 * 1024} },
David Hendricks05653ff2010-06-15 16:05:12 -0700121 { 0, 0, 0x6, {0x200000, 2048 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700122
123 { 0, 1, 0x1, {0x000000, 64 * 1024} },
124 { 0, 1, 0x2, {0x000000, 128 * 1024} },
125 { 0, 1, 0x3, {0x000000, 256 * 1024} },
126 { 0, 1, 0x4, {0x000000, 512 * 1024} },
127 { 0, 1, 0x5, {0x000000, 1024 * 1024} },
128 { 0, 1, 0x6, {0x000000, 2048 * 1024} },
129 { X, X, 0x7, {0x000000, 4096 * 1024} },
130
131 { 1, 0, 0x1, {0x3ff000, 4 * 1024} },
132 { 1, 0, 0x2, {0x3fe000, 8 * 1024} },
133 { 1, 0, 0x3, {0x3fc000, 16 * 1024} },
134 { 1, 0, 0x4, {0x3f8000, 32 * 1024} },
135 { 1, 0, 0x5, {0x3f8000, 32 * 1024} },
136
137 { 1, 1, 0x1, {0x000000, 4 * 1024} },
138 { 1, 1, 0x2, {0x000000, 8 * 1024} },
139 { 1, 1, 0x3, {0x000000, 16 * 1024} },
140 { 1, 1, 0x4, {0x000000, 32 * 1024} },
141 { 1, 1, 0x5, {0x000000, 32 * 1024} },
142};
143
144static struct w25q_range w25q80_ranges[] = {
145 { X, X, 0, {0, 0} }, /* none */
146 { 0, 0, 0x1, {0x0f0000, 64 * 1024} },
147 { 0, 0, 0x2, {0x0e0000, 128 * 1024} },
148 { 0, 0, 0x3, {0x0c0000, 256 * 1024} },
149 { 0, 0, 0x4, {0x080000, 512 * 1024} },
150
151 { 0, 1, 0x1, {0x000000, 64 * 1024} },
152 { 0, 1, 0x2, {0x000000, 128 * 1024} },
153 { 0, 1, 0x3, {0x000000, 256 * 1024} },
154 { 0, 1, 0x4, {0x000000, 512 * 1024} },
David Hendricks05653ff2010-06-15 16:05:12 -0700155 { X, X, 0x6, {0x000000, 1024 * 1024} },
156 { X, X, 0x7, {0x000000, 1024 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700157
158 { 1, 0, 0x1, {0x1ff000, 4 * 1024} },
159 { 1, 0, 0x2, {0x1fe000, 8 * 1024} },
160 { 1, 0, 0x3, {0x1fc000, 16 * 1024} },
161 { 1, 0, 0x4, {0x1f8000, 32 * 1024} },
162 { 1, 0, 0x5, {0x1f8000, 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 Hendricks2c4a76c2010-06-28 14:00:43 -0700171static struct w25q_range w25q64_ranges[] = {
172 { X, X, 0, {0, 0} }, /* none */
173
174 { 0, 0, 0x1, {0x7e0000, 128 * 1024} },
175 { 0, 0, 0x2, {0x7c0000, 256 * 1024} },
176 { 0, 0, 0x3, {0x780000, 512 * 1024} },
177 { 0, 0, 0x4, {0x700000, 1024 * 1024} },
178 { 0, 0, 0x5, {0x600000, 2048 * 1024} },
179 { 0, 0, 0x6, {0x400000, 4096 * 1024} },
180
181 { 0, 1, 0x1, {0x000000, 128 * 1024} },
182 { 0, 1, 0x2, {0x000000, 256 * 1024} },
183 { 0, 1, 0x3, {0x000000, 512 * 1024} },
184 { 0, 1, 0x4, {0x000000, 1024 * 1024} },
185 { 0, 1, 0x5, {0x000000, 2048 * 1024} },
186 { 0, 1, 0x6, {0x000000, 4096 * 1024} },
187 { X, X, 0x7, {0x000000, 8192 * 1024} },
188
189 { 1, 0, 0x1, {0x7ff000, 4 * 1024} },
190 { 1, 0, 0x2, {0x7fe000, 8 * 1024} },
191 { 1, 0, 0x3, {0x7fc000, 16 * 1024} },
192 { 1, 0, 0x4, {0x7f8000, 32 * 1024} },
193 { 1, 0, 0x5, {0x7f8000, 32 * 1024} },
194
195 { 1, 1, 0x1, {0x000000, 4 * 1024} },
196 { 1, 1, 0x2, {0x000000, 8 * 1024} },
197 { 1, 1, 0x3, {0x000000, 16 * 1024} },
198 { 1, 1, 0x4, {0x000000, 32 * 1024} },
199 { 1, 1, 0x5, {0x000000, 32 * 1024} },
200};
201
David Hendricks470ca952010-08-13 14:01:53 -0700202struct w25q_range w25x40_ranges[] = {
203 { X, X, 0, {0, 0} }, /* none */
204 { 0, 0, 0x1, {0x070000, 64 * 1024} },
205 { 0, 0, 0x2, {0x060000, 128 * 1024} },
206 { 0, 0, 0x3, {0x040000, 256 * 1024} },
207 { 0, 1, 0x1, {0x000000, 64 * 1024} },
208 { 0, 1, 0x2, {0x000000, 128 * 1024} },
209 { 0, 1, 0x3, {0x000000, 256 * 1024} },
210 { 0, X, 0x4, {0x000000, 512 * 1024} },
211};
212
David Hendricksf7924d12010-06-10 21:26:44 -0700213struct w25q_status {
214 /* this maps to register layout -- do not change ordering */
215 unsigned char busy : 1;
216 unsigned char wel : 1;
217 unsigned char bp0 : 1;
218 unsigned char bp1 : 1;
219 unsigned char bp2 : 1;
220 unsigned char tb : 1;
221 unsigned char sec : 1;
222 unsigned char srp0 : 1;
223 /* FIXME: what about the second status register? */
224// unsigned char srp1 : 1;
225// unsigned char qe : 1;
226} __attribute__ ((packed));
227
David Hendricksd494b0a2010-08-16 16:28:50 -0700228int wp_get_status(const struct flashchip *flash,
229 unsigned int start, unsigned int len,
230 struct w25q_status *status)
David Hendricksf7924d12010-06-10 21:26:44 -0700231{
David Hendricksf7924d12010-06-10 21:26:44 -0700232 struct w25q_range *w25q_ranges;
233 int i, num_entries = 0;
234 int tmp = 0, range_found = 0;
235
David Hendricksd494b0a2010-08-16 16:28:50 -0700236 switch (flash->manufacture_id) {
237 case WINBOND_NEX_ID:
238 switch(flash->model_id) {
239 case W_25X40:
240 w25q_ranges = w25x40_ranges;
241 num_entries = ARRAY_SIZE(w25x40_ranges);
242 break;
243 case W_25Q80:
244 w25q_ranges = w25q80_ranges;
245 num_entries = ARRAY_SIZE(w25q80_ranges);
246 break;
247 case W_25Q16:
248 w25q_ranges = w25q16_ranges;
249 num_entries = ARRAY_SIZE(w25q16_ranges);
250 break;
251 case W_25Q32:
252 w25q_ranges = w25q32_ranges;
253 num_entries = ARRAY_SIZE(w25q32_ranges);
254 break;
255 case W_25Q64:
256 w25q_ranges = w25q64_ranges;
257 num_entries = ARRAY_SIZE(w25q64_ranges);
258 break;
259 default:
260 msg_cerr("%s() %d: WINBOND flash chip mismatch (0x%04x)"
261 ", aborting\n", __func__, __LINE__,
262 flash->model_id);
263 return -1;
264 }
David Hendricks2c4a76c2010-06-28 14:00:43 -0700265 break;
David Hendricks57566ed2010-08-16 18:24:45 -0700266 case EON_ID_NOPREFIX:
267 switch (flash->model_id) {
268 case EN_25F40:
269 w25q_ranges = en25f40_ranges;
270 num_entries = ARRAY_SIZE(en25f40_ranges);
271 break;
272 default:
273 msg_cerr("%s():%d: EON flash chip mismatch (0x%04x)"
274 ", aborting\n", __func__, __LINE__,
275 flash->model_id);
276 return -1;
277 }
278 break;
David Hendricksac72e362010-08-16 18:20:03 -0700279 case MX_ID:
280 switch (flash->model_id) {
281 case MX_25L3205:
282 w25q_ranges = mx25l3205d_ranges;
283 num_entries = ARRAY_SIZE(mx25l3205d_ranges);
284 break;
285 default:
286 msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)"
287 ", aborting\n", __func__, __LINE__,
288 flash->model_id);
289 return -1;
290 }
291 break;
David Hendricksf7924d12010-06-10 21:26:44 -0700292 default:
David Hendricksd494b0a2010-08-16 16:28:50 -0700293 msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
294 __func__, flash->manufacture_id);
David Hendricksf7924d12010-06-10 21:26:44 -0700295 return -1;
296 }
297
298 memset(&status, 0, sizeof(status));
299 tmp = spi_read_status_register();
300 memcpy(&status, &tmp, 1);
301 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
302
303 for (i = 0; i < num_entries; i++) {
304 struct wp_range *r = &w25q_ranges[i].range;
305
306 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
307 start, len, r->start, r->len);
308 if ((start == r->start) && (len == r->len)) {
David Hendricksd494b0a2010-08-16 16:28:50 -0700309 status->bp0 = w25q_ranges[i].bp & 1;
310 status->bp1 = w25q_ranges[i].bp >> 1;
311 status->bp2 = w25q_ranges[i].bp >> 2;
312 status->tb = w25q_ranges[i].tb;
313 status->sec = w25q_ranges[i].sec;
David Hendricksf7924d12010-06-10 21:26:44 -0700314
315 range_found = 1;
316 break;
317 }
318 }
319
320 if (!range_found) {
321 msg_cerr("matching range not found\n");
322 return -1;
323 }
David Hendricksd494b0a2010-08-16 16:28:50 -0700324 return 0;
325}
326
327static int w25_set_range(struct flashchip *flash,
328 unsigned int start, unsigned int len)
329{
330 struct w25q_status status;
331 int tmp;
332
333 memset(&status, 0, sizeof(status));
334 tmp = spi_read_status_register();
335 memcpy(&status, &tmp, 1);
336 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
337
338 if (wp_get_status(flash, start, len, &status)) return -1;
David Hendricksf7924d12010-06-10 21:26:44 -0700339
340 msg_cdbg("status.busy: %x\n", status.busy);
341 msg_cdbg("status.wel: %x\n", status.wel);
342 msg_cdbg("status.bp0: %x\n", status.bp0);
343 msg_cdbg("status.bp1: %x\n", status.bp1);
344 msg_cdbg("status.bp2: %x\n", status.bp2);
345 msg_cdbg("status.tb: %x\n", status.tb);
346 msg_cdbg("status.sec: %x\n", status.sec);
347 msg_cdbg("status.srp0: %x\n", status.srp0);
348
349 memcpy(&tmp, &status, sizeof(status));
David Hendricks82fd8ae2010-08-04 14:34:54 -0700350 spi_write_status_register(flash, tmp);
David Hendricksf7924d12010-06-10 21:26:44 -0700351 msg_cdbg("%s: new status: 0x%02x\n",
352 __func__, spi_read_status_register());
353
354 return 0;
355}
356
357static int w25_enable_writeprotect(struct flashchip *flash)
358{
359 struct w25q_status status;
360 int tmp = 0;
361
362 memset(&status, 0, sizeof(status));
363 tmp = spi_read_status_register();
364 memcpy(&status, &tmp, 1);
365 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
366
367 status.srp0 = 1;
368 memcpy(&tmp, &status, sizeof(status));
369 spi_write_status_enable();
David Hendricks82fd8ae2010-08-04 14:34:54 -0700370 spi_write_status_register(flash, tmp);
David Hendricksf7924d12010-06-10 21:26:44 -0700371 msg_cdbg("%s: new status: 0x%02x\n",
372 __func__, spi_read_status_register());
373
374 return 0;
375}
376
377struct wp wp_w25 = {
378 .set_range = w25_set_range,
379 .enable = w25_enable_writeprotect,
380};