blob: e6412a84425f782b44d4d70d40276fbfdd7e47ab [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"
Louis Yung-Chieh Lo52aa9302010-09-06 10:45:02 +080027#include "spi.h"
David Hendricks23cd7782010-08-25 12:42:38 -070028#include "writeprotect.h"
David Hendricksf7924d12010-06-10 21:26:44 -070029
30/*
31 * The following procedures rely on look-up tables to match the user-specified
32 * range with the chip's supported ranges. This turned out to be the most
33 * elegant approach since diferent flash chips use different levels of
34 * granularity and methods to determine protected ranges. In other words,
35 * be stupid and simple since clever arithmetic will not for many chips.
36 */
37
38struct wp_range {
39 unsigned int start; /* starting address */
40 unsigned int len; /* len */
41};
42
43enum bit_state {
44 OFF = 0,
45 ON = 1,
46 X = 0 /* don't care */
47};
48
49struct w25q_range {
50 enum bit_state sec; /* if 1, bp[2:0] describe sectors */
51 enum bit_state tb; /* top/bottom select */
52 unsigned short int bp : 3; /* block protect bitfield */
53 struct wp_range range;
54};
55
David Hendricks57566ed2010-08-16 18:24:45 -070056struct w25q_range en25f40_ranges[] = {
57 { X, X, 0, {0, 0} }, /* none */
58 { 0, 0, 0x1, {0x000000, 504 * 1024} },
59 { 0, 0, 0x2, {0x000000, 496 * 1024} },
60 { 0, 0, 0x3, {0x000000, 480 * 1024} },
61 { 0, 0, 0x4, {0x000000, 448 * 1024} },
62 { 0, 0, 0x5, {0x000000, 384 * 1024} },
63 { 0, 0, 0x6, {0x000000, 256 * 1024} },
64 { 0, 0, 0x7, {0x000000, 512 * 1024} },
65};
66
David Hendricksac72e362010-08-16 18:20:03 -070067static struct w25q_range mx25l3205d_ranges[] = {
68 { X, 0, 0, {0, 0} }, /* none */
69 { X, 0, 0x1, {0x3f0000, 64 * 1024} },
70 { X, 0, 0x2, {0x3e0000, 128 * 1024} },
71 { X, 0, 0x3, {0x3c0000, 256 * 1024} },
72 { X, 0, 0x4, {0x380000, 512 * 1024} },
73 { X, 0, 0x5, {0x300000, 1024 * 1024} },
74 { X, 0, 0x6, {0x200000, 2048 * 1024} },
75 { X, 0, 0x7, {0x000000, 4096 * 1024} },
76
77 { X, 1, 0x0, {0x000000, 4096 * 1024} },
78 { X, 1, 0x1, {0x000000, 2048 * 1024} },
79 { X, 1, 0x2, {0x000000, 3072 * 1024} },
80 { X, 1, 0x3, {0x000000, 3584 * 1024} },
81 { X, 1, 0x4, {0x000000, 3840 * 1024} },
82 { X, 1, 0x5, {0x000000, 3968 * 1024} },
83 { X, 1, 0x6, {0x000000, 4032 * 1024} },
84 { X, 1, 0x7, {0x000000, 4096 * 1024} },
85};
86
David Hendricksf7924d12010-06-10 21:26:44 -070087static struct w25q_range w25q16_ranges[] = {
88 { X, X, 0, {0, 0} }, /* none */
89 { 0, 0, 0x1, {0x1f0000, 64 * 1024} },
90 { 0, 0, 0x2, {0x1e0000, 128 * 1024} },
91 { 0, 0, 0x3, {0x1c0000, 256 * 1024} },
92 { 0, 0, 0x4, {0x180000, 512 * 1024} },
93 { 0, 0, 0x5, {0x100000, 1024 * 1024} },
94
95 { 0, 1, 0x1, {0x000000, 64 * 1024} },
96 { 0, 1, 0x2, {0x000000, 128 * 1024} },
97 { 0, 1, 0x3, {0x000000, 256 * 1024} },
98 { 0, 1, 0x4, {0x000000, 512 * 1024} },
99 { 0, 1, 0x5, {0x000000, 1024 * 1024} },
100 { X, X, 0x6, {0x000000, 2048 * 1024} },
101 { X, X, 0x7, {0x000000, 2048 * 1024} },
102
103 { 1, 0, 0x1, {0x1ff000, 4 * 1024} },
104 { 1, 0, 0x2, {0x1fe000, 8 * 1024} },
105 { 1, 0, 0x3, {0x1fc000, 16 * 1024} },
106 { 1, 0, 0x4, {0x1f8000, 32 * 1024} },
107 { 1, 0, 0x5, {0x1f8000, 32 * 1024} },
108
109 { 1, 1, 0x1, {0x000000, 4 * 1024} },
110 { 1, 1, 0x2, {0x000000, 8 * 1024} },
111 { 1, 1, 0x3, {0x000000, 16 * 1024} },
112 { 1, 1, 0x4, {0x000000, 32 * 1024} },
113 { 1, 1, 0x5, {0x000000, 32 * 1024} },
114};
115
116static struct w25q_range w25q32_ranges[] = {
117 { X, X, 0, {0, 0} }, /* none */
118 { 0, 0, 0x1, {0x3f0000, 64 * 1024} },
119 { 0, 0, 0x2, {0x3e0000, 128 * 1024} },
120 { 0, 0, 0x3, {0x3c0000, 256 * 1024} },
121 { 0, 0, 0x4, {0x380000, 512 * 1024} },
122 { 0, 0, 0x5, {0x300000, 1024 * 1024} },
David Hendricks05653ff2010-06-15 16:05:12 -0700123 { 0, 0, 0x6, {0x200000, 2048 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700124
125 { 0, 1, 0x1, {0x000000, 64 * 1024} },
126 { 0, 1, 0x2, {0x000000, 128 * 1024} },
127 { 0, 1, 0x3, {0x000000, 256 * 1024} },
128 { 0, 1, 0x4, {0x000000, 512 * 1024} },
129 { 0, 1, 0x5, {0x000000, 1024 * 1024} },
130 { 0, 1, 0x6, {0x000000, 2048 * 1024} },
131 { X, X, 0x7, {0x000000, 4096 * 1024} },
132
133 { 1, 0, 0x1, {0x3ff000, 4 * 1024} },
134 { 1, 0, 0x2, {0x3fe000, 8 * 1024} },
135 { 1, 0, 0x3, {0x3fc000, 16 * 1024} },
136 { 1, 0, 0x4, {0x3f8000, 32 * 1024} },
137 { 1, 0, 0x5, {0x3f8000, 32 * 1024} },
138
139 { 1, 1, 0x1, {0x000000, 4 * 1024} },
140 { 1, 1, 0x2, {0x000000, 8 * 1024} },
141 { 1, 1, 0x3, {0x000000, 16 * 1024} },
142 { 1, 1, 0x4, {0x000000, 32 * 1024} },
143 { 1, 1, 0x5, {0x000000, 32 * 1024} },
144};
145
146static struct w25q_range w25q80_ranges[] = {
147 { X, X, 0, {0, 0} }, /* none */
148 { 0, 0, 0x1, {0x0f0000, 64 * 1024} },
149 { 0, 0, 0x2, {0x0e0000, 128 * 1024} },
150 { 0, 0, 0x3, {0x0c0000, 256 * 1024} },
151 { 0, 0, 0x4, {0x080000, 512 * 1024} },
152
153 { 0, 1, 0x1, {0x000000, 64 * 1024} },
154 { 0, 1, 0x2, {0x000000, 128 * 1024} },
155 { 0, 1, 0x3, {0x000000, 256 * 1024} },
156 { 0, 1, 0x4, {0x000000, 512 * 1024} },
David Hendricks05653ff2010-06-15 16:05:12 -0700157 { X, X, 0x6, {0x000000, 1024 * 1024} },
158 { X, X, 0x7, {0x000000, 1024 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700159
160 { 1, 0, 0x1, {0x1ff000, 4 * 1024} },
161 { 1, 0, 0x2, {0x1fe000, 8 * 1024} },
162 { 1, 0, 0x3, {0x1fc000, 16 * 1024} },
163 { 1, 0, 0x4, {0x1f8000, 32 * 1024} },
164 { 1, 0, 0x5, {0x1f8000, 32 * 1024} },
165
166 { 1, 1, 0x1, {0x000000, 4 * 1024} },
167 { 1, 1, 0x2, {0x000000, 8 * 1024} },
168 { 1, 1, 0x3, {0x000000, 16 * 1024} },
169 { 1, 1, 0x4, {0x000000, 32 * 1024} },
170 { 1, 1, 0x5, {0x000000, 32 * 1024} },
171};
172
David Hendricks2c4a76c2010-06-28 14:00:43 -0700173static struct w25q_range w25q64_ranges[] = {
174 { X, X, 0, {0, 0} }, /* none */
175
176 { 0, 0, 0x1, {0x7e0000, 128 * 1024} },
177 { 0, 0, 0x2, {0x7c0000, 256 * 1024} },
178 { 0, 0, 0x3, {0x780000, 512 * 1024} },
179 { 0, 0, 0x4, {0x700000, 1024 * 1024} },
180 { 0, 0, 0x5, {0x600000, 2048 * 1024} },
181 { 0, 0, 0x6, {0x400000, 4096 * 1024} },
182
183 { 0, 1, 0x1, {0x000000, 128 * 1024} },
184 { 0, 1, 0x2, {0x000000, 256 * 1024} },
185 { 0, 1, 0x3, {0x000000, 512 * 1024} },
186 { 0, 1, 0x4, {0x000000, 1024 * 1024} },
187 { 0, 1, 0x5, {0x000000, 2048 * 1024} },
188 { 0, 1, 0x6, {0x000000, 4096 * 1024} },
189 { X, X, 0x7, {0x000000, 8192 * 1024} },
190
191 { 1, 0, 0x1, {0x7ff000, 4 * 1024} },
192 { 1, 0, 0x2, {0x7fe000, 8 * 1024} },
193 { 1, 0, 0x3, {0x7fc000, 16 * 1024} },
194 { 1, 0, 0x4, {0x7f8000, 32 * 1024} },
195 { 1, 0, 0x5, {0x7f8000, 32 * 1024} },
196
197 { 1, 1, 0x1, {0x000000, 4 * 1024} },
198 { 1, 1, 0x2, {0x000000, 8 * 1024} },
199 { 1, 1, 0x3, {0x000000, 16 * 1024} },
200 { 1, 1, 0x4, {0x000000, 32 * 1024} },
201 { 1, 1, 0x5, {0x000000, 32 * 1024} },
202};
203
David Hendricks470ca952010-08-13 14:01:53 -0700204struct w25q_range w25x40_ranges[] = {
205 { X, X, 0, {0, 0} }, /* none */
206 { 0, 0, 0x1, {0x070000, 64 * 1024} },
207 { 0, 0, 0x2, {0x060000, 128 * 1024} },
208 { 0, 0, 0x3, {0x040000, 256 * 1024} },
209 { 0, 1, 0x1, {0x000000, 64 * 1024} },
210 { 0, 1, 0x2, {0x000000, 128 * 1024} },
211 { 0, 1, 0x3, {0x000000, 256 * 1024} },
212 { 0, X, 0x4, {0x000000, 512 * 1024} },
213};
214
David Hendricksd494b0a2010-08-16 16:28:50 -0700215int wp_get_status(const struct flashchip *flash,
216 unsigned int start, unsigned int len,
217 struct w25q_status *status)
David Hendricksf7924d12010-06-10 21:26:44 -0700218{
David Hendricksf7924d12010-06-10 21:26:44 -0700219 struct w25q_range *w25q_ranges;
220 int i, num_entries = 0;
221 int tmp = 0, range_found = 0;
222
David Hendricksd494b0a2010-08-16 16:28:50 -0700223 switch (flash->manufacture_id) {
224 case WINBOND_NEX_ID:
225 switch(flash->model_id) {
226 case W_25X40:
227 w25q_ranges = w25x40_ranges;
228 num_entries = ARRAY_SIZE(w25x40_ranges);
229 break;
230 case W_25Q80:
231 w25q_ranges = w25q80_ranges;
232 num_entries = ARRAY_SIZE(w25q80_ranges);
233 break;
234 case W_25Q16:
235 w25q_ranges = w25q16_ranges;
236 num_entries = ARRAY_SIZE(w25q16_ranges);
237 break;
238 case W_25Q32:
239 w25q_ranges = w25q32_ranges;
240 num_entries = ARRAY_SIZE(w25q32_ranges);
241 break;
242 case W_25Q64:
243 w25q_ranges = w25q64_ranges;
244 num_entries = ARRAY_SIZE(w25q64_ranges);
245 break;
246 default:
247 msg_cerr("%s() %d: WINBOND flash chip mismatch (0x%04x)"
248 ", aborting\n", __func__, __LINE__,
249 flash->model_id);
250 return -1;
251 }
David Hendricks2c4a76c2010-06-28 14:00:43 -0700252 break;
David Hendricks57566ed2010-08-16 18:24:45 -0700253 case EON_ID_NOPREFIX:
254 switch (flash->model_id) {
255 case EN_25F40:
256 w25q_ranges = en25f40_ranges;
257 num_entries = ARRAY_SIZE(en25f40_ranges);
258 break;
259 default:
260 msg_cerr("%s():%d: EON flash chip mismatch (0x%04x)"
261 ", aborting\n", __func__, __LINE__,
262 flash->model_id);
263 return -1;
264 }
265 break;
David Hendricksac72e362010-08-16 18:20:03 -0700266 case MX_ID:
267 switch (flash->model_id) {
268 case MX_25L3205:
269 w25q_ranges = mx25l3205d_ranges;
270 num_entries = ARRAY_SIZE(mx25l3205d_ranges);
271 break;
272 default:
273 msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)"
274 ", aborting\n", __func__, __LINE__,
275 flash->model_id);
276 return -1;
277 }
278 break;
David Hendricksf7924d12010-06-10 21:26:44 -0700279 default:
David Hendricksd494b0a2010-08-16 16:28:50 -0700280 msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
281 __func__, flash->manufacture_id);
David Hendricksf7924d12010-06-10 21:26:44 -0700282 return -1;
283 }
284
David Hendricksf7924d12010-06-10 21:26:44 -0700285 for (i = 0; i < num_entries; i++) {
286 struct wp_range *r = &w25q_ranges[i].range;
287
288 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
289 start, len, r->start, r->len);
290 if ((start == r->start) && (len == r->len)) {
David Hendricksd494b0a2010-08-16 16:28:50 -0700291 status->bp0 = w25q_ranges[i].bp & 1;
292 status->bp1 = w25q_ranges[i].bp >> 1;
293 status->bp2 = w25q_ranges[i].bp >> 2;
294 status->tb = w25q_ranges[i].tb;
295 status->sec = w25q_ranges[i].sec;
David Hendricksf7924d12010-06-10 21:26:44 -0700296
297 range_found = 1;
298 break;
299 }
300 }
301
302 if (!range_found) {
303 msg_cerr("matching range not found\n");
304 return -1;
305 }
David Hendricksd494b0a2010-08-16 16:28:50 -0700306 return 0;
307}
308
Louis Yung-Chieh Lo52aa9302010-09-06 10:45:02 +0800309/* Since most chips we use must be WREN-ed before WRSR,
310 * we copy a write status function here before we have a good solution. */
311static int spi_write_status_register_WREN(int status)
312{
313 int result;
314 struct spi_command cmds[] = {
315 {
316 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
317 .writecnt = JEDEC_WREN_OUTSIZE,
318 .writearr = (const unsigned char[]){ JEDEC_WREN },
319 .readcnt = 0,
320 .readarr = NULL,
321 }, {
322 .writecnt = JEDEC_WRSR_OUTSIZE,
323 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
324 .readcnt = 0,
325 .readarr = NULL,
326 }, {
327 .writecnt = 0,
328 .writearr = NULL,
329 .readcnt = 0,
330 .readarr = NULL,
331 }};
332
333 result = spi_send_multicommand(cmds);
334 if (result) {
335 msg_cerr("%s failed during command execution\n",
336 __func__);
337 }
338 return result;
339}
340
David Hendricksd494b0a2010-08-16 16:28:50 -0700341static int w25_set_range(struct flashchip *flash,
342 unsigned int start, unsigned int len)
343{
344 struct w25q_status status;
345 int tmp;
346
347 memset(&status, 0, sizeof(status));
348 tmp = spi_read_status_register();
349 memcpy(&status, &tmp, 1);
350 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
351
352 if (wp_get_status(flash, start, len, &status)) return -1;
David Hendricksf7924d12010-06-10 21:26:44 -0700353
354 msg_cdbg("status.busy: %x\n", status.busy);
355 msg_cdbg("status.wel: %x\n", status.wel);
356 msg_cdbg("status.bp0: %x\n", status.bp0);
357 msg_cdbg("status.bp1: %x\n", status.bp1);
358 msg_cdbg("status.bp2: %x\n", status.bp2);
359 msg_cdbg("status.tb: %x\n", status.tb);
360 msg_cdbg("status.sec: %x\n", status.sec);
361 msg_cdbg("status.srp0: %x\n", status.srp0);
362
363 memcpy(&tmp, &status, sizeof(status));
Louis Yung-Chieh Lo52aa9302010-09-06 10:45:02 +0800364 spi_write_status_register_WREN(tmp);
David Hendricksf7924d12010-06-10 21:26:44 -0700365 msg_cdbg("%s: new status: 0x%02x\n",
366 __func__, spi_read_status_register());
367
368 return 0;
369}
370
371static int w25_enable_writeprotect(struct flashchip *flash)
372{
373 struct w25q_status status;
374 int tmp = 0;
375
376 memset(&status, 0, sizeof(status));
377 tmp = spi_read_status_register();
378 memcpy(&status, &tmp, 1);
379 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
380
381 status.srp0 = 1;
382 memcpy(&tmp, &status, sizeof(status));
Louis Yung-Chieh Lo52aa9302010-09-06 10:45:02 +0800383 spi_write_status_register_WREN(tmp);
David Hendricksf7924d12010-06-10 21:26:44 -0700384 msg_cdbg("%s: new status: 0x%02x\n",
385 __func__, spi_read_status_register());
386
387 return 0;
388}
389
390struct wp wp_w25 = {
391 .set_range = w25_set_range,
392 .enable = w25_enable_writeprotect,
393};