blob: 65e6f4383ed9b7c11b4493be0a03effe80c1332d [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"
David Hendricks23cd7782010-08-25 12:42:38 -070027#include "writeprotect.h"
David Hendricksf7924d12010-06-10 21:26:44 -070028
29/*
30 * The following procedures rely on look-up tables to match the user-specified
31 * range with the chip's supported ranges. This turned out to be the most
32 * elegant approach since diferent flash chips use different levels of
33 * granularity and methods to determine protected ranges. In other words,
34 * be stupid and simple since clever arithmetic will not for many chips.
35 */
36
37struct wp_range {
38 unsigned int start; /* starting address */
39 unsigned int len; /* len */
40};
41
42enum bit_state {
43 OFF = 0,
44 ON = 1,
45 X = 0 /* don't care */
46};
47
48struct w25q_range {
49 enum bit_state sec; /* if 1, bp[2:0] describe sectors */
50 enum bit_state tb; /* top/bottom select */
51 unsigned short int bp : 3; /* block protect bitfield */
52 struct wp_range range;
53};
54
David Hendricks57566ed2010-08-16 18:24:45 -070055struct w25q_range en25f40_ranges[] = {
56 { X, X, 0, {0, 0} }, /* none */
57 { 0, 0, 0x1, {0x000000, 504 * 1024} },
58 { 0, 0, 0x2, {0x000000, 496 * 1024} },
59 { 0, 0, 0x3, {0x000000, 480 * 1024} },
60 { 0, 0, 0x4, {0x000000, 448 * 1024} },
61 { 0, 0, 0x5, {0x000000, 384 * 1024} },
62 { 0, 0, 0x6, {0x000000, 256 * 1024} },
63 { 0, 0, 0x7, {0x000000, 512 * 1024} },
64};
65
David Hendricksac72e362010-08-16 18:20:03 -070066static struct w25q_range mx25l3205d_ranges[] = {
67 { X, 0, 0, {0, 0} }, /* none */
68 { X, 0, 0x1, {0x3f0000, 64 * 1024} },
69 { X, 0, 0x2, {0x3e0000, 128 * 1024} },
70 { X, 0, 0x3, {0x3c0000, 256 * 1024} },
71 { X, 0, 0x4, {0x380000, 512 * 1024} },
72 { X, 0, 0x5, {0x300000, 1024 * 1024} },
73 { X, 0, 0x6, {0x200000, 2048 * 1024} },
74 { X, 0, 0x7, {0x000000, 4096 * 1024} },
75
76 { X, 1, 0x0, {0x000000, 4096 * 1024} },
77 { X, 1, 0x1, {0x000000, 2048 * 1024} },
78 { X, 1, 0x2, {0x000000, 3072 * 1024} },
79 { X, 1, 0x3, {0x000000, 3584 * 1024} },
80 { X, 1, 0x4, {0x000000, 3840 * 1024} },
81 { X, 1, 0x5, {0x000000, 3968 * 1024} },
82 { X, 1, 0x6, {0x000000, 4032 * 1024} },
83 { X, 1, 0x7, {0x000000, 4096 * 1024} },
84};
85
David Hendricksf7924d12010-06-10 21:26:44 -070086static struct w25q_range w25q16_ranges[] = {
87 { X, X, 0, {0, 0} }, /* none */
88 { 0, 0, 0x1, {0x1f0000, 64 * 1024} },
89 { 0, 0, 0x2, {0x1e0000, 128 * 1024} },
90 { 0, 0, 0x3, {0x1c0000, 256 * 1024} },
91 { 0, 0, 0x4, {0x180000, 512 * 1024} },
92 { 0, 0, 0x5, {0x100000, 1024 * 1024} },
93
94 { 0, 1, 0x1, {0x000000, 64 * 1024} },
95 { 0, 1, 0x2, {0x000000, 128 * 1024} },
96 { 0, 1, 0x3, {0x000000, 256 * 1024} },
97 { 0, 1, 0x4, {0x000000, 512 * 1024} },
98 { 0, 1, 0x5, {0x000000, 1024 * 1024} },
99 { X, X, 0x6, {0x000000, 2048 * 1024} },
100 { X, X, 0x7, {0x000000, 2048 * 1024} },
101
102 { 1, 0, 0x1, {0x1ff000, 4 * 1024} },
103 { 1, 0, 0x2, {0x1fe000, 8 * 1024} },
104 { 1, 0, 0x3, {0x1fc000, 16 * 1024} },
105 { 1, 0, 0x4, {0x1f8000, 32 * 1024} },
106 { 1, 0, 0x5, {0x1f8000, 32 * 1024} },
107
108 { 1, 1, 0x1, {0x000000, 4 * 1024} },
109 { 1, 1, 0x2, {0x000000, 8 * 1024} },
110 { 1, 1, 0x3, {0x000000, 16 * 1024} },
111 { 1, 1, 0x4, {0x000000, 32 * 1024} },
112 { 1, 1, 0x5, {0x000000, 32 * 1024} },
113};
114
115static struct w25q_range w25q32_ranges[] = {
116 { X, X, 0, {0, 0} }, /* none */
117 { 0, 0, 0x1, {0x3f0000, 64 * 1024} },
118 { 0, 0, 0x2, {0x3e0000, 128 * 1024} },
119 { 0, 0, 0x3, {0x3c0000, 256 * 1024} },
120 { 0, 0, 0x4, {0x380000, 512 * 1024} },
121 { 0, 0, 0x5, {0x300000, 1024 * 1024} },
David Hendricks05653ff2010-06-15 16:05:12 -0700122 { 0, 0, 0x6, {0x200000, 2048 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700123
124 { 0, 1, 0x1, {0x000000, 64 * 1024} },
125 { 0, 1, 0x2, {0x000000, 128 * 1024} },
126 { 0, 1, 0x3, {0x000000, 256 * 1024} },
127 { 0, 1, 0x4, {0x000000, 512 * 1024} },
128 { 0, 1, 0x5, {0x000000, 1024 * 1024} },
129 { 0, 1, 0x6, {0x000000, 2048 * 1024} },
130 { X, X, 0x7, {0x000000, 4096 * 1024} },
131
132 { 1, 0, 0x1, {0x3ff000, 4 * 1024} },
133 { 1, 0, 0x2, {0x3fe000, 8 * 1024} },
134 { 1, 0, 0x3, {0x3fc000, 16 * 1024} },
135 { 1, 0, 0x4, {0x3f8000, 32 * 1024} },
136 { 1, 0, 0x5, {0x3f8000, 32 * 1024} },
137
138 { 1, 1, 0x1, {0x000000, 4 * 1024} },
139 { 1, 1, 0x2, {0x000000, 8 * 1024} },
140 { 1, 1, 0x3, {0x000000, 16 * 1024} },
141 { 1, 1, 0x4, {0x000000, 32 * 1024} },
142 { 1, 1, 0x5, {0x000000, 32 * 1024} },
143};
144
145static struct w25q_range w25q80_ranges[] = {
146 { X, X, 0, {0, 0} }, /* none */
147 { 0, 0, 0x1, {0x0f0000, 64 * 1024} },
148 { 0, 0, 0x2, {0x0e0000, 128 * 1024} },
149 { 0, 0, 0x3, {0x0c0000, 256 * 1024} },
150 { 0, 0, 0x4, {0x080000, 512 * 1024} },
151
152 { 0, 1, 0x1, {0x000000, 64 * 1024} },
153 { 0, 1, 0x2, {0x000000, 128 * 1024} },
154 { 0, 1, 0x3, {0x000000, 256 * 1024} },
155 { 0, 1, 0x4, {0x000000, 512 * 1024} },
David Hendricks05653ff2010-06-15 16:05:12 -0700156 { X, X, 0x6, {0x000000, 1024 * 1024} },
157 { X, X, 0x7, {0x000000, 1024 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700158
159 { 1, 0, 0x1, {0x1ff000, 4 * 1024} },
160 { 1, 0, 0x2, {0x1fe000, 8 * 1024} },
161 { 1, 0, 0x3, {0x1fc000, 16 * 1024} },
162 { 1, 0, 0x4, {0x1f8000, 32 * 1024} },
163 { 1, 0, 0x5, {0x1f8000, 32 * 1024} },
164
165 { 1, 1, 0x1, {0x000000, 4 * 1024} },
166 { 1, 1, 0x2, {0x000000, 8 * 1024} },
167 { 1, 1, 0x3, {0x000000, 16 * 1024} },
168 { 1, 1, 0x4, {0x000000, 32 * 1024} },
169 { 1, 1, 0x5, {0x000000, 32 * 1024} },
170};
171
David Hendricks2c4a76c2010-06-28 14:00:43 -0700172static struct w25q_range w25q64_ranges[] = {
173 { X, X, 0, {0, 0} }, /* none */
174
175 { 0, 0, 0x1, {0x7e0000, 128 * 1024} },
176 { 0, 0, 0x2, {0x7c0000, 256 * 1024} },
177 { 0, 0, 0x3, {0x780000, 512 * 1024} },
178 { 0, 0, 0x4, {0x700000, 1024 * 1024} },
179 { 0, 0, 0x5, {0x600000, 2048 * 1024} },
180 { 0, 0, 0x6, {0x400000, 4096 * 1024} },
181
182 { 0, 1, 0x1, {0x000000, 128 * 1024} },
183 { 0, 1, 0x2, {0x000000, 256 * 1024} },
184 { 0, 1, 0x3, {0x000000, 512 * 1024} },
185 { 0, 1, 0x4, {0x000000, 1024 * 1024} },
186 { 0, 1, 0x5, {0x000000, 2048 * 1024} },
187 { 0, 1, 0x6, {0x000000, 4096 * 1024} },
188 { X, X, 0x7, {0x000000, 8192 * 1024} },
189
190 { 1, 0, 0x1, {0x7ff000, 4 * 1024} },
191 { 1, 0, 0x2, {0x7fe000, 8 * 1024} },
192 { 1, 0, 0x3, {0x7fc000, 16 * 1024} },
193 { 1, 0, 0x4, {0x7f8000, 32 * 1024} },
194 { 1, 0, 0x5, {0x7f8000, 32 * 1024} },
195
196 { 1, 1, 0x1, {0x000000, 4 * 1024} },
197 { 1, 1, 0x2, {0x000000, 8 * 1024} },
198 { 1, 1, 0x3, {0x000000, 16 * 1024} },
199 { 1, 1, 0x4, {0x000000, 32 * 1024} },
200 { 1, 1, 0x5, {0x000000, 32 * 1024} },
201};
202
David Hendricks470ca952010-08-13 14:01:53 -0700203struct w25q_range w25x40_ranges[] = {
204 { X, X, 0, {0, 0} }, /* none */
205 { 0, 0, 0x1, {0x070000, 64 * 1024} },
206 { 0, 0, 0x2, {0x060000, 128 * 1024} },
207 { 0, 0, 0x3, {0x040000, 256 * 1024} },
208 { 0, 1, 0x1, {0x000000, 64 * 1024} },
209 { 0, 1, 0x2, {0x000000, 128 * 1024} },
210 { 0, 1, 0x3, {0x000000, 256 * 1024} },
211 { 0, X, 0x4, {0x000000, 512 * 1024} },
212};
213
David Hendricksd494b0a2010-08-16 16:28:50 -0700214int wp_get_status(const struct flashchip *flash,
215 unsigned int start, unsigned int len,
216 struct w25q_status *status)
David Hendricksf7924d12010-06-10 21:26:44 -0700217{
David Hendricksf7924d12010-06-10 21:26:44 -0700218 struct w25q_range *w25q_ranges;
219 int i, num_entries = 0;
220 int tmp = 0, range_found = 0;
221
David Hendricksd494b0a2010-08-16 16:28:50 -0700222 switch (flash->manufacture_id) {
223 case WINBOND_NEX_ID:
224 switch(flash->model_id) {
225 case W_25X40:
226 w25q_ranges = w25x40_ranges;
227 num_entries = ARRAY_SIZE(w25x40_ranges);
228 break;
229 case W_25Q80:
230 w25q_ranges = w25q80_ranges;
231 num_entries = ARRAY_SIZE(w25q80_ranges);
232 break;
233 case W_25Q16:
234 w25q_ranges = w25q16_ranges;
235 num_entries = ARRAY_SIZE(w25q16_ranges);
236 break;
237 case W_25Q32:
238 w25q_ranges = w25q32_ranges;
239 num_entries = ARRAY_SIZE(w25q32_ranges);
240 break;
241 case W_25Q64:
242 w25q_ranges = w25q64_ranges;
243 num_entries = ARRAY_SIZE(w25q64_ranges);
244 break;
245 default:
246 msg_cerr("%s() %d: WINBOND flash chip mismatch (0x%04x)"
247 ", aborting\n", __func__, __LINE__,
248 flash->model_id);
249 return -1;
250 }
David Hendricks2c4a76c2010-06-28 14:00:43 -0700251 break;
David Hendricks57566ed2010-08-16 18:24:45 -0700252 case EON_ID_NOPREFIX:
253 switch (flash->model_id) {
254 case EN_25F40:
255 w25q_ranges = en25f40_ranges;
256 num_entries = ARRAY_SIZE(en25f40_ranges);
257 break;
258 default:
259 msg_cerr("%s():%d: EON flash chip mismatch (0x%04x)"
260 ", aborting\n", __func__, __LINE__,
261 flash->model_id);
262 return -1;
263 }
264 break;
David Hendricksac72e362010-08-16 18:20:03 -0700265 case MX_ID:
266 switch (flash->model_id) {
267 case MX_25L3205:
268 w25q_ranges = mx25l3205d_ranges;
269 num_entries = ARRAY_SIZE(mx25l3205d_ranges);
270 break;
271 default:
272 msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)"
273 ", aborting\n", __func__, __LINE__,
274 flash->model_id);
275 return -1;
276 }
277 break;
David Hendricksf7924d12010-06-10 21:26:44 -0700278 default:
David Hendricksd494b0a2010-08-16 16:28:50 -0700279 msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
280 __func__, flash->manufacture_id);
David Hendricksf7924d12010-06-10 21:26:44 -0700281 return -1;
282 }
283
284 memset(&status, 0, sizeof(status));
285 tmp = spi_read_status_register();
286 memcpy(&status, &tmp, 1);
287 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
288
289 for (i = 0; i < num_entries; i++) {
290 struct wp_range *r = &w25q_ranges[i].range;
291
292 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
293 start, len, r->start, r->len);
294 if ((start == r->start) && (len == r->len)) {
David Hendricksd494b0a2010-08-16 16:28:50 -0700295 status->bp0 = w25q_ranges[i].bp & 1;
296 status->bp1 = w25q_ranges[i].bp >> 1;
297 status->bp2 = w25q_ranges[i].bp >> 2;
298 status->tb = w25q_ranges[i].tb;
299 status->sec = w25q_ranges[i].sec;
David Hendricksf7924d12010-06-10 21:26:44 -0700300
301 range_found = 1;
302 break;
303 }
304 }
305
306 if (!range_found) {
307 msg_cerr("matching range not found\n");
308 return -1;
309 }
David Hendricksd494b0a2010-08-16 16:28:50 -0700310 return 0;
311}
312
313static int w25_set_range(struct flashchip *flash,
314 unsigned int start, unsigned int len)
315{
316 struct w25q_status status;
317 int tmp;
318
319 memset(&status, 0, sizeof(status));
320 tmp = spi_read_status_register();
321 memcpy(&status, &tmp, 1);
322 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
323
324 if (wp_get_status(flash, start, len, &status)) return -1;
David Hendricksf7924d12010-06-10 21:26:44 -0700325
326 msg_cdbg("status.busy: %x\n", status.busy);
327 msg_cdbg("status.wel: %x\n", status.wel);
328 msg_cdbg("status.bp0: %x\n", status.bp0);
329 msg_cdbg("status.bp1: %x\n", status.bp1);
330 msg_cdbg("status.bp2: %x\n", status.bp2);
331 msg_cdbg("status.tb: %x\n", status.tb);
332 msg_cdbg("status.sec: %x\n", status.sec);
333 msg_cdbg("status.srp0: %x\n", status.srp0);
334
335 memcpy(&tmp, &status, sizeof(status));
David Hendricks82fd8ae2010-08-04 14:34:54 -0700336 spi_write_status_register(flash, tmp);
David Hendricksf7924d12010-06-10 21:26:44 -0700337 msg_cdbg("%s: new status: 0x%02x\n",
338 __func__, spi_read_status_register());
339
340 return 0;
341}
342
343static int w25_enable_writeprotect(struct flashchip *flash)
344{
345 struct w25q_status status;
346 int tmp = 0;
347
348 memset(&status, 0, sizeof(status));
349 tmp = spi_read_status_register();
350 memcpy(&status, &tmp, 1);
351 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
352
353 status.srp0 = 1;
354 memcpy(&tmp, &status, sizeof(status));
355 spi_write_status_enable();
David Hendricks82fd8ae2010-08-04 14:34:54 -0700356 spi_write_status_register(flash, tmp);
David Hendricksf7924d12010-06-10 21:26:44 -0700357 msg_cdbg("%s: new status: 0x%02x\n",
358 __func__, spi_read_status_register());
359
360 return 0;
361}
362
363struct wp wp_w25 = {
364 .set_range = w25_set_range,
365 .enable = w25_enable_writeprotect,
366};