blob: 8236596a3dde3471f07eaa158bf47ad8d1399039 [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
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800204struct w25q_range w25x10_ranges[] = {
205 { X, X, 0, {0, 0} }, /* none */
206 { 0, 0, 0x1, {0x010000, 64 * 1024} },
207 { 0, 1, 0x1, {0x000000, 64 * 1024} },
208 { X, X, 0x2, {0x000000, 128 * 1024} },
209 { X, X, 0x3, {0x000000, 128 * 1024} },
210};
211
212struct w25q_range w25x20_ranges[] = {
213 { X, X, 0, {0, 0} }, /* none */
214 { 0, 0, 0x1, {0x030000, 64 * 1024} },
215 { 0, 0, 0x2, {0x020000, 128 * 1024} },
216 { 0, 1, 0x1, {0x000000, 64 * 1024} },
217 { 0, 1, 0x2, {0x000000, 128 * 1024} },
218 { 0, X, 0x3, {0x000000, 256 * 1024} },
219};
220
David Hendricks470ca952010-08-13 14:01:53 -0700221struct w25q_range w25x40_ranges[] = {
222 { X, X, 0, {0, 0} }, /* none */
223 { 0, 0, 0x1, {0x070000, 64 * 1024} },
224 { 0, 0, 0x2, {0x060000, 128 * 1024} },
225 { 0, 0, 0x3, {0x040000, 256 * 1024} },
226 { 0, 1, 0x1, {0x000000, 64 * 1024} },
227 { 0, 1, 0x2, {0x000000, 128 * 1024} },
228 { 0, 1, 0x3, {0x000000, 256 * 1024} },
229 { 0, X, 0x4, {0x000000, 512 * 1024} },
230};
231
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800232struct w25q_range w25x80_ranges[] = {
233 { X, X, 0, {0, 0} }, /* none */
234 { 0, 0, 0x1, {0x0F0000, 64 * 1024} },
235 { 0, 0, 0x2, {0x0E0000, 128 * 1024} },
236 { 0, 0, 0x3, {0x0C0000, 256 * 1024} },
237 { 0, 0, 0x4, {0x080000, 512 * 1024} },
238 { 0, 1, 0x1, {0x000000, 64 * 1024} },
239 { 0, 1, 0x2, {0x000000, 128 * 1024} },
240 { 0, 1, 0x3, {0x000000, 256 * 1024} },
241 { 0, 1, 0x4, {0x000000, 512 * 1024} },
242 { 0, X, 0x5, {0x000000, 1024 * 1024} },
243 { 0, X, 0x6, {0x000000, 1024 * 1024} },
244 { 0, X, 0x7, {0x000000, 1024 * 1024} },
245};
246
David Hendricksd494b0a2010-08-16 16:28:50 -0700247int wp_get_status(const struct flashchip *flash,
248 unsigned int start, unsigned int len,
249 struct w25q_status *status)
David Hendricksf7924d12010-06-10 21:26:44 -0700250{
David Hendricksf7924d12010-06-10 21:26:44 -0700251 struct w25q_range *w25q_ranges;
252 int i, num_entries = 0;
David Hendrickse4aa62b2010-10-04 15:37:30 -0700253 int range_found = 0;
David Hendricksf7924d12010-06-10 21:26:44 -0700254
David Hendricksd494b0a2010-08-16 16:28:50 -0700255 switch (flash->manufacture_id) {
256 case WINBOND_NEX_ID:
257 switch(flash->model_id) {
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800258 case W_25X10:
259 w25q_ranges = w25x10_ranges;
260 num_entries = ARRAY_SIZE(w25x10_ranges);
261 break;
262 case W_25X20:
263 w25q_ranges = w25x20_ranges;
264 num_entries = ARRAY_SIZE(w25x20_ranges);
265 break;
David Hendricksd494b0a2010-08-16 16:28:50 -0700266 case W_25X40:
267 w25q_ranges = w25x40_ranges;
268 num_entries = ARRAY_SIZE(w25x40_ranges);
269 break;
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800270 case W_25X80:
271 w25q_ranges = w25x80_ranges;
272 num_entries = ARRAY_SIZE(w25x80_ranges);
273 break;
David Hendricksd494b0a2010-08-16 16:28:50 -0700274 case W_25Q80:
275 w25q_ranges = w25q80_ranges;
276 num_entries = ARRAY_SIZE(w25q80_ranges);
277 break;
278 case W_25Q16:
279 w25q_ranges = w25q16_ranges;
280 num_entries = ARRAY_SIZE(w25q16_ranges);
281 break;
282 case W_25Q32:
283 w25q_ranges = w25q32_ranges;
284 num_entries = ARRAY_SIZE(w25q32_ranges);
285 break;
286 case W_25Q64:
287 w25q_ranges = w25q64_ranges;
288 num_entries = ARRAY_SIZE(w25q64_ranges);
289 break;
290 default:
291 msg_cerr("%s() %d: WINBOND flash chip mismatch (0x%04x)"
292 ", aborting\n", __func__, __LINE__,
293 flash->model_id);
294 return -1;
295 }
David Hendricks2c4a76c2010-06-28 14:00:43 -0700296 break;
David Hendricks57566ed2010-08-16 18:24:45 -0700297 case EON_ID_NOPREFIX:
298 switch (flash->model_id) {
299 case EN_25F40:
300 w25q_ranges = en25f40_ranges;
301 num_entries = ARRAY_SIZE(en25f40_ranges);
302 break;
303 default:
304 msg_cerr("%s():%d: EON flash chip mismatch (0x%04x)"
305 ", aborting\n", __func__, __LINE__,
306 flash->model_id);
307 return -1;
308 }
309 break;
David Hendricksac72e362010-08-16 18:20:03 -0700310 case MX_ID:
311 switch (flash->model_id) {
312 case MX_25L3205:
313 w25q_ranges = mx25l3205d_ranges;
314 num_entries = ARRAY_SIZE(mx25l3205d_ranges);
315 break;
316 default:
317 msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)"
318 ", aborting\n", __func__, __LINE__,
319 flash->model_id);
320 return -1;
321 }
322 break;
David Hendricksf7924d12010-06-10 21:26:44 -0700323 default:
David Hendricksd494b0a2010-08-16 16:28:50 -0700324 msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
325 __func__, flash->manufacture_id);
David Hendricksf7924d12010-06-10 21:26:44 -0700326 return -1;
327 }
328
David Hendricksf7924d12010-06-10 21:26:44 -0700329 for (i = 0; i < num_entries; i++) {
330 struct wp_range *r = &w25q_ranges[i].range;
331
332 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
333 start, len, r->start, r->len);
334 if ((start == r->start) && (len == r->len)) {
David Hendricksd494b0a2010-08-16 16:28:50 -0700335 status->bp0 = w25q_ranges[i].bp & 1;
336 status->bp1 = w25q_ranges[i].bp >> 1;
337 status->bp2 = w25q_ranges[i].bp >> 2;
338 status->tb = w25q_ranges[i].tb;
339 status->sec = w25q_ranges[i].sec;
David Hendricksf7924d12010-06-10 21:26:44 -0700340
341 range_found = 1;
342 break;
343 }
344 }
345
346 if (!range_found) {
347 msg_cerr("matching range not found\n");
348 return -1;
349 }
David Hendricksd494b0a2010-08-16 16:28:50 -0700350 return 0;
351}
352
Louis Yung-Chieh Lo52aa9302010-09-06 10:45:02 +0800353/* Since most chips we use must be WREN-ed before WRSR,
354 * we copy a write status function here before we have a good solution. */
355static int spi_write_status_register_WREN(int status)
356{
357 int result;
358 struct spi_command cmds[] = {
359 {
360 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
361 .writecnt = JEDEC_WREN_OUTSIZE,
362 .writearr = (const unsigned char[]){ JEDEC_WREN },
363 .readcnt = 0,
364 .readarr = NULL,
365 }, {
366 .writecnt = JEDEC_WRSR_OUTSIZE,
367 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
368 .readcnt = 0,
369 .readarr = NULL,
370 }, {
371 .writecnt = 0,
372 .writearr = NULL,
373 .readcnt = 0,
374 .readarr = NULL,
375 }};
376
377 result = spi_send_multicommand(cmds);
378 if (result) {
379 msg_cerr("%s failed during command execution\n",
380 __func__);
381 }
382 return result;
383}
384
David Hendricksd494b0a2010-08-16 16:28:50 -0700385static int w25_set_range(struct flashchip *flash,
386 unsigned int start, unsigned int len)
387{
388 struct w25q_status status;
389 int tmp;
390
391 memset(&status, 0, sizeof(status));
392 tmp = spi_read_status_register();
393 memcpy(&status, &tmp, 1);
394 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
395
396 if (wp_get_status(flash, start, len, &status)) return -1;
David Hendricksf7924d12010-06-10 21:26:44 -0700397
398 msg_cdbg("status.busy: %x\n", status.busy);
399 msg_cdbg("status.wel: %x\n", status.wel);
400 msg_cdbg("status.bp0: %x\n", status.bp0);
401 msg_cdbg("status.bp1: %x\n", status.bp1);
402 msg_cdbg("status.bp2: %x\n", status.bp2);
403 msg_cdbg("status.tb: %x\n", status.tb);
404 msg_cdbg("status.sec: %x\n", status.sec);
405 msg_cdbg("status.srp0: %x\n", status.srp0);
406
407 memcpy(&tmp, &status, sizeof(status));
Louis Yung-Chieh Lo52aa9302010-09-06 10:45:02 +0800408 spi_write_status_register_WREN(tmp);
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800409 msg_cinfo("%s: new status: 0x%02x\n",
David Hendricksf7924d12010-06-10 21:26:44 -0700410 __func__, spi_read_status_register());
411
412 return 0;
413}
414
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800415static int w25_set_srp0(struct flashchip *flash, int enable)
David Hendricksf7924d12010-06-10 21:26:44 -0700416{
417 struct w25q_status status;
418 int tmp = 0;
419
420 memset(&status, 0, sizeof(status));
421 tmp = spi_read_status_register();
422 memcpy(&status, &tmp, 1);
423 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
424
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800425 status.srp0 = enable ? 1 : 0;
David Hendricksf7924d12010-06-10 21:26:44 -0700426 memcpy(&tmp, &status, sizeof(status));
Louis Yung-Chieh Lo52aa9302010-09-06 10:45:02 +0800427 spi_write_status_register_WREN(tmp);
David Hendricksf7924d12010-06-10 21:26:44 -0700428 msg_cdbg("%s: new status: 0x%02x\n",
429 __func__, spi_read_status_register());
430
431 return 0;
432}
433
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800434static int w25_enable_writeprotect(struct flashchip *flash)
435{
436 int ret;
437
438 ret = w25_set_srp0(flash, 1);
439 if (!ret)
440 msg_cinfo("SUCCESS.\n");
441 else
442 msg_cinfo("FAILED, error=%d.\n", ret);
443 return ret;
444}
445
446static int w25_disable_writeprotect(struct flashchip *flash)
447{
448 int ret;
449
450 ret = w25_set_srp0(flash, 0);
451 if (!ret)
452 msg_cinfo("SUCCESS.\n");
453 else
454 msg_cinfo("FAILED, error=%d.\n", ret);
455 return ret;
456}
457
David Hendricksf7924d12010-06-10 21:26:44 -0700458struct wp wp_w25 = {
459 .set_range = w25_set_range,
460 .enable = w25_enable_writeprotect,
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800461 .disable = w25_disable_writeprotect,
David Hendricksf7924d12010-06-10 21:26:44 -0700462};