blob: a5f06406dc593204cca1279ffdd7f2824e49e9c8 [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
Louis Yung-Chieh Lo96222b12010-11-01 11:48:11 +080030/* When update flash's status register, it takes few time to erase register.
31 * After surveying some flash vendor specs, such as Winbond, MXIC, EON,
32 * all of their update time are less than 20ms. After refering the spi25.c,
33 * use 100ms delay.
34 */
35#define WRITE_STATUS_REGISTER_DELAY 100 * 1000 /* unit: us */
36
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +080037/* Mask to extract SRP0 and range bits in status register.
38 * SRP0: bit 7
39 * range(BP2-BP0): bit 4-2
40 */
41#define MASK_WP_AREA (0x9C)
42
David Hendricksf7924d12010-06-10 21:26:44 -070043/*
44 * The following procedures rely on look-up tables to match the user-specified
45 * range with the chip's supported ranges. This turned out to be the most
46 * elegant approach since diferent flash chips use different levels of
47 * granularity and methods to determine protected ranges. In other words,
48 * be stupid and simple since clever arithmetic will not for many chips.
49 */
50
51struct wp_range {
52 unsigned int start; /* starting address */
53 unsigned int len; /* len */
54};
55
56enum bit_state {
57 OFF = 0,
58 ON = 1,
59 X = 0 /* don't care */
60};
61
62struct w25q_range {
63 enum bit_state sec; /* if 1, bp[2:0] describe sectors */
64 enum bit_state tb; /* top/bottom select */
65 unsigned short int bp : 3; /* block protect bitfield */
66 struct wp_range range;
67};
68
David Hendricks57566ed2010-08-16 18:24:45 -070069struct w25q_range en25f40_ranges[] = {
70 { X, X, 0, {0, 0} }, /* none */
71 { 0, 0, 0x1, {0x000000, 504 * 1024} },
72 { 0, 0, 0x2, {0x000000, 496 * 1024} },
73 { 0, 0, 0x3, {0x000000, 480 * 1024} },
74 { 0, 0, 0x4, {0x000000, 448 * 1024} },
75 { 0, 0, 0x5, {0x000000, 384 * 1024} },
76 { 0, 0, 0x6, {0x000000, 256 * 1024} },
77 { 0, 0, 0x7, {0x000000, 512 * 1024} },
78};
79
David Hendricksac72e362010-08-16 18:20:03 -070080static struct w25q_range mx25l3205d_ranges[] = {
81 { X, 0, 0, {0, 0} }, /* none */
82 { X, 0, 0x1, {0x3f0000, 64 * 1024} },
83 { X, 0, 0x2, {0x3e0000, 128 * 1024} },
84 { X, 0, 0x3, {0x3c0000, 256 * 1024} },
85 { X, 0, 0x4, {0x380000, 512 * 1024} },
86 { X, 0, 0x5, {0x300000, 1024 * 1024} },
87 { X, 0, 0x6, {0x200000, 2048 * 1024} },
88 { X, 0, 0x7, {0x000000, 4096 * 1024} },
89
90 { X, 1, 0x0, {0x000000, 4096 * 1024} },
91 { X, 1, 0x1, {0x000000, 2048 * 1024} },
92 { X, 1, 0x2, {0x000000, 3072 * 1024} },
93 { X, 1, 0x3, {0x000000, 3584 * 1024} },
94 { X, 1, 0x4, {0x000000, 3840 * 1024} },
95 { X, 1, 0x5, {0x000000, 3968 * 1024} },
96 { X, 1, 0x6, {0x000000, 4032 * 1024} },
97 { X, 1, 0x7, {0x000000, 4096 * 1024} },
98};
99
David Hendricksf7924d12010-06-10 21:26:44 -0700100static struct w25q_range w25q16_ranges[] = {
101 { X, X, 0, {0, 0} }, /* none */
102 { 0, 0, 0x1, {0x1f0000, 64 * 1024} },
103 { 0, 0, 0x2, {0x1e0000, 128 * 1024} },
104 { 0, 0, 0x3, {0x1c0000, 256 * 1024} },
105 { 0, 0, 0x4, {0x180000, 512 * 1024} },
106 { 0, 0, 0x5, {0x100000, 1024 * 1024} },
107
108 { 0, 1, 0x1, {0x000000, 64 * 1024} },
109 { 0, 1, 0x2, {0x000000, 128 * 1024} },
110 { 0, 1, 0x3, {0x000000, 256 * 1024} },
111 { 0, 1, 0x4, {0x000000, 512 * 1024} },
112 { 0, 1, 0x5, {0x000000, 1024 * 1024} },
113 { X, X, 0x6, {0x000000, 2048 * 1024} },
114 { X, X, 0x7, {0x000000, 2048 * 1024} },
115
116 { 1, 0, 0x1, {0x1ff000, 4 * 1024} },
117 { 1, 0, 0x2, {0x1fe000, 8 * 1024} },
118 { 1, 0, 0x3, {0x1fc000, 16 * 1024} },
119 { 1, 0, 0x4, {0x1f8000, 32 * 1024} },
120 { 1, 0, 0x5, {0x1f8000, 32 * 1024} },
121
122 { 1, 1, 0x1, {0x000000, 4 * 1024} },
123 { 1, 1, 0x2, {0x000000, 8 * 1024} },
124 { 1, 1, 0x3, {0x000000, 16 * 1024} },
125 { 1, 1, 0x4, {0x000000, 32 * 1024} },
126 { 1, 1, 0x5, {0x000000, 32 * 1024} },
127};
128
129static struct w25q_range w25q32_ranges[] = {
130 { X, X, 0, {0, 0} }, /* none */
131 { 0, 0, 0x1, {0x3f0000, 64 * 1024} },
132 { 0, 0, 0x2, {0x3e0000, 128 * 1024} },
133 { 0, 0, 0x3, {0x3c0000, 256 * 1024} },
134 { 0, 0, 0x4, {0x380000, 512 * 1024} },
135 { 0, 0, 0x5, {0x300000, 1024 * 1024} },
David Hendricks05653ff2010-06-15 16:05:12 -0700136 { 0, 0, 0x6, {0x200000, 2048 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700137
138 { 0, 1, 0x1, {0x000000, 64 * 1024} },
139 { 0, 1, 0x2, {0x000000, 128 * 1024} },
140 { 0, 1, 0x3, {0x000000, 256 * 1024} },
141 { 0, 1, 0x4, {0x000000, 512 * 1024} },
142 { 0, 1, 0x5, {0x000000, 1024 * 1024} },
143 { 0, 1, 0x6, {0x000000, 2048 * 1024} },
144 { X, X, 0x7, {0x000000, 4096 * 1024} },
145
146 { 1, 0, 0x1, {0x3ff000, 4 * 1024} },
147 { 1, 0, 0x2, {0x3fe000, 8 * 1024} },
148 { 1, 0, 0x3, {0x3fc000, 16 * 1024} },
149 { 1, 0, 0x4, {0x3f8000, 32 * 1024} },
150 { 1, 0, 0x5, {0x3f8000, 32 * 1024} },
151
152 { 1, 1, 0x1, {0x000000, 4 * 1024} },
153 { 1, 1, 0x2, {0x000000, 8 * 1024} },
154 { 1, 1, 0x3, {0x000000, 16 * 1024} },
155 { 1, 1, 0x4, {0x000000, 32 * 1024} },
156 { 1, 1, 0x5, {0x000000, 32 * 1024} },
157};
158
159static struct w25q_range w25q80_ranges[] = {
160 { X, X, 0, {0, 0} }, /* none */
161 { 0, 0, 0x1, {0x0f0000, 64 * 1024} },
162 { 0, 0, 0x2, {0x0e0000, 128 * 1024} },
163 { 0, 0, 0x3, {0x0c0000, 256 * 1024} },
164 { 0, 0, 0x4, {0x080000, 512 * 1024} },
165
166 { 0, 1, 0x1, {0x000000, 64 * 1024} },
167 { 0, 1, 0x2, {0x000000, 128 * 1024} },
168 { 0, 1, 0x3, {0x000000, 256 * 1024} },
169 { 0, 1, 0x4, {0x000000, 512 * 1024} },
David Hendricks05653ff2010-06-15 16:05:12 -0700170 { X, X, 0x6, {0x000000, 1024 * 1024} },
171 { X, X, 0x7, {0x000000, 1024 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700172
173 { 1, 0, 0x1, {0x1ff000, 4 * 1024} },
174 { 1, 0, 0x2, {0x1fe000, 8 * 1024} },
175 { 1, 0, 0x3, {0x1fc000, 16 * 1024} },
176 { 1, 0, 0x4, {0x1f8000, 32 * 1024} },
177 { 1, 0, 0x5, {0x1f8000, 32 * 1024} },
178
179 { 1, 1, 0x1, {0x000000, 4 * 1024} },
180 { 1, 1, 0x2, {0x000000, 8 * 1024} },
181 { 1, 1, 0x3, {0x000000, 16 * 1024} },
182 { 1, 1, 0x4, {0x000000, 32 * 1024} },
183 { 1, 1, 0x5, {0x000000, 32 * 1024} },
184};
185
David Hendricks2c4a76c2010-06-28 14:00:43 -0700186static struct w25q_range w25q64_ranges[] = {
187 { X, X, 0, {0, 0} }, /* none */
188
189 { 0, 0, 0x1, {0x7e0000, 128 * 1024} },
190 { 0, 0, 0x2, {0x7c0000, 256 * 1024} },
191 { 0, 0, 0x3, {0x780000, 512 * 1024} },
192 { 0, 0, 0x4, {0x700000, 1024 * 1024} },
193 { 0, 0, 0x5, {0x600000, 2048 * 1024} },
194 { 0, 0, 0x6, {0x400000, 4096 * 1024} },
195
196 { 0, 1, 0x1, {0x000000, 128 * 1024} },
197 { 0, 1, 0x2, {0x000000, 256 * 1024} },
198 { 0, 1, 0x3, {0x000000, 512 * 1024} },
199 { 0, 1, 0x4, {0x000000, 1024 * 1024} },
200 { 0, 1, 0x5, {0x000000, 2048 * 1024} },
201 { 0, 1, 0x6, {0x000000, 4096 * 1024} },
202 { X, X, 0x7, {0x000000, 8192 * 1024} },
203
204 { 1, 0, 0x1, {0x7ff000, 4 * 1024} },
205 { 1, 0, 0x2, {0x7fe000, 8 * 1024} },
206 { 1, 0, 0x3, {0x7fc000, 16 * 1024} },
207 { 1, 0, 0x4, {0x7f8000, 32 * 1024} },
208 { 1, 0, 0x5, {0x7f8000, 32 * 1024} },
209
210 { 1, 1, 0x1, {0x000000, 4 * 1024} },
211 { 1, 1, 0x2, {0x000000, 8 * 1024} },
212 { 1, 1, 0x3, {0x000000, 16 * 1024} },
213 { 1, 1, 0x4, {0x000000, 32 * 1024} },
214 { 1, 1, 0x5, {0x000000, 32 * 1024} },
215};
216
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800217struct w25q_range w25x10_ranges[] = {
218 { X, X, 0, {0, 0} }, /* none */
219 { 0, 0, 0x1, {0x010000, 64 * 1024} },
220 { 0, 1, 0x1, {0x000000, 64 * 1024} },
221 { X, X, 0x2, {0x000000, 128 * 1024} },
222 { X, X, 0x3, {0x000000, 128 * 1024} },
223};
224
225struct w25q_range w25x20_ranges[] = {
226 { X, X, 0, {0, 0} }, /* none */
227 { 0, 0, 0x1, {0x030000, 64 * 1024} },
228 { 0, 0, 0x2, {0x020000, 128 * 1024} },
229 { 0, 1, 0x1, {0x000000, 64 * 1024} },
230 { 0, 1, 0x2, {0x000000, 128 * 1024} },
231 { 0, X, 0x3, {0x000000, 256 * 1024} },
232};
233
David Hendricks470ca952010-08-13 14:01:53 -0700234struct w25q_range w25x40_ranges[] = {
235 { X, X, 0, {0, 0} }, /* none */
236 { 0, 0, 0x1, {0x070000, 64 * 1024} },
237 { 0, 0, 0x2, {0x060000, 128 * 1024} },
238 { 0, 0, 0x3, {0x040000, 256 * 1024} },
239 { 0, 1, 0x1, {0x000000, 64 * 1024} },
240 { 0, 1, 0x2, {0x000000, 128 * 1024} },
241 { 0, 1, 0x3, {0x000000, 256 * 1024} },
242 { 0, X, 0x4, {0x000000, 512 * 1024} },
243};
244
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800245struct w25q_range w25x80_ranges[] = {
246 { X, X, 0, {0, 0} }, /* none */
247 { 0, 0, 0x1, {0x0F0000, 64 * 1024} },
248 { 0, 0, 0x2, {0x0E0000, 128 * 1024} },
249 { 0, 0, 0x3, {0x0C0000, 256 * 1024} },
250 { 0, 0, 0x4, {0x080000, 512 * 1024} },
251 { 0, 1, 0x1, {0x000000, 64 * 1024} },
252 { 0, 1, 0x2, {0x000000, 128 * 1024} },
253 { 0, 1, 0x3, {0x000000, 256 * 1024} },
254 { 0, 1, 0x4, {0x000000, 512 * 1024} },
255 { 0, X, 0x5, {0x000000, 1024 * 1024} },
256 { 0, X, 0x6, {0x000000, 1024 * 1024} },
257 { 0, X, 0x7, {0x000000, 1024 * 1024} },
258};
259
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800260/* Given a flash chip, this function returns its range table. */
261static int w25_range_table(const struct flashchip *flash,
262 struct w25q_range **w25q_ranges,
263 int *num_entries)
David Hendricksf7924d12010-06-10 21:26:44 -0700264{
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800265 *w25q_ranges = 0;
266 *num_entries = 0;
David Hendricksf7924d12010-06-10 21:26:44 -0700267
David Hendricksd494b0a2010-08-16 16:28:50 -0700268 switch (flash->manufacture_id) {
269 case WINBOND_NEX_ID:
270 switch(flash->model_id) {
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800271 case W_25X10:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800272 *w25q_ranges = w25x10_ranges;
273 *num_entries = ARRAY_SIZE(w25x10_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800274 break;
275 case W_25X20:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800276 *w25q_ranges = w25x20_ranges;
277 *num_entries = ARRAY_SIZE(w25x20_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800278 break;
David Hendricksd494b0a2010-08-16 16:28:50 -0700279 case W_25X40:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800280 *w25q_ranges = w25x40_ranges;
281 *num_entries = ARRAY_SIZE(w25x40_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700282 break;
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800283 case W_25X80:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800284 *w25q_ranges = w25x80_ranges;
285 *num_entries = ARRAY_SIZE(w25x80_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800286 break;
David Hendricksd494b0a2010-08-16 16:28:50 -0700287 case W_25Q80:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800288 *w25q_ranges = w25q80_ranges;
289 *num_entries = ARRAY_SIZE(w25q80_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700290 break;
291 case W_25Q16:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800292 *w25q_ranges = w25q16_ranges;
293 *num_entries = ARRAY_SIZE(w25q16_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700294 break;
295 case W_25Q32:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800296 *w25q_ranges = w25q32_ranges;
297 *num_entries = ARRAY_SIZE(w25q32_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700298 break;
299 case W_25Q64:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800300 *w25q_ranges = w25q64_ranges;
301 *num_entries = ARRAY_SIZE(w25q64_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700302 break;
303 default:
304 msg_cerr("%s() %d: WINBOND flash chip mismatch (0x%04x)"
305 ", aborting\n", __func__, __LINE__,
306 flash->model_id);
307 return -1;
308 }
David Hendricks2c4a76c2010-06-28 14:00:43 -0700309 break;
David Hendricks57566ed2010-08-16 18:24:45 -0700310 case EON_ID_NOPREFIX:
311 switch (flash->model_id) {
312 case EN_25F40:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800313 *w25q_ranges = en25f40_ranges;
314 *num_entries = ARRAY_SIZE(en25f40_ranges);
David Hendricks57566ed2010-08-16 18:24:45 -0700315 break;
316 default:
317 msg_cerr("%s():%d: EON flash chip mismatch (0x%04x)"
318 ", aborting\n", __func__, __LINE__,
319 flash->model_id);
320 return -1;
321 }
322 break;
David Hendricksac72e362010-08-16 18:20:03 -0700323 case MX_ID:
324 switch (flash->model_id) {
325 case MX_25L3205:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800326 *w25q_ranges = mx25l3205d_ranges;
327 *num_entries = ARRAY_SIZE(mx25l3205d_ranges);
David Hendricksac72e362010-08-16 18:20:03 -0700328 break;
329 default:
330 msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)"
331 ", aborting\n", __func__, __LINE__,
332 flash->model_id);
333 return -1;
334 }
335 break;
David Hendricksf7924d12010-06-10 21:26:44 -0700336 default:
David Hendricksd494b0a2010-08-16 16:28:50 -0700337 msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
338 __func__, flash->manufacture_id);
David Hendricksf7924d12010-06-10 21:26:44 -0700339 return -1;
340 }
341
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800342 return 0;
343}
344
345int w25_range_to_status(const struct flashchip *flash,
346 unsigned int start, unsigned int len,
347 struct w25q_status *status)
348{
349 struct w25q_range *w25q_ranges;
350 int i, range_found = 0;
351 int num_entries;
352
353 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
David Hendricksf7924d12010-06-10 21:26:44 -0700354 for (i = 0; i < num_entries; i++) {
355 struct wp_range *r = &w25q_ranges[i].range;
356
357 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
358 start, len, r->start, r->len);
359 if ((start == r->start) && (len == r->len)) {
David Hendricksd494b0a2010-08-16 16:28:50 -0700360 status->bp0 = w25q_ranges[i].bp & 1;
361 status->bp1 = w25q_ranges[i].bp >> 1;
362 status->bp2 = w25q_ranges[i].bp >> 2;
363 status->tb = w25q_ranges[i].tb;
364 status->sec = w25q_ranges[i].sec;
David Hendricksf7924d12010-06-10 21:26:44 -0700365
366 range_found = 1;
367 break;
368 }
369 }
370
371 if (!range_found) {
372 msg_cerr("matching range not found\n");
373 return -1;
374 }
David Hendricksd494b0a2010-08-16 16:28:50 -0700375 return 0;
376}
377
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800378int w25_status_to_range(const struct flashchip *flash,
379 const struct w25q_status *status,
380 unsigned int *start, unsigned int *len)
381{
382 struct w25q_range *w25q_ranges;
383 int i, status_found = 0;
384 int num_entries;
385
386 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
387 for (i = 0; i < num_entries; i++) {
388 int bp;
389
390 bp = status->bp0 | (status->bp1 << 1) | (status->bp2 << 2);
391 msg_cspew("comparing 0x%x 0x%x / 0x%x 0x%x / 0x%x 0x%x\n",
392 bp, w25q_ranges[i].bp,
393 status->tb, w25q_ranges[i].tb,
394 status->sec, w25q_ranges[i].sec);
395 if ((bp == w25q_ranges[i].bp) &&
396 (status->tb == w25q_ranges[i].tb) &&
397 (status->sec == w25q_ranges[i].sec)) {
398 *start = w25q_ranges[i].range.start;
399 *len = w25q_ranges[i].range.len;
400
401 status_found = 1;
402 break;
403 }
404 }
405
406 if (!status_found) {
407 msg_cerr("matching status not found\n");
408 return -1;
409 }
410 return 0;
411}
412
Louis Yung-Chieh Lo52aa9302010-09-06 10:45:02 +0800413/* Since most chips we use must be WREN-ed before WRSR,
414 * we copy a write status function here before we have a good solution. */
415static int spi_write_status_register_WREN(int status)
416{
417 int result;
418 struct spi_command cmds[] = {
419 {
420 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
421 .writecnt = JEDEC_WREN_OUTSIZE,
422 .writearr = (const unsigned char[]){ JEDEC_WREN },
423 .readcnt = 0,
424 .readarr = NULL,
425 }, {
426 .writecnt = JEDEC_WRSR_OUTSIZE,
427 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
428 .readcnt = 0,
429 .readarr = NULL,
430 }, {
431 .writecnt = 0,
432 .writearr = NULL,
433 .readcnt = 0,
434 .readarr = NULL,
435 }};
436
437 result = spi_send_multicommand(cmds);
438 if (result) {
439 msg_cerr("%s failed during command execution\n",
440 __func__);
441 }
Louis Yung-Chieh Lo96222b12010-11-01 11:48:11 +0800442
443 /* WRSR performs a self-timed erase before the changes take effect. */
444 programmer_delay(WRITE_STATUS_REGISTER_DELAY);
445
Louis Yung-Chieh Lo52aa9302010-09-06 10:45:02 +0800446 return result;
447}
448
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800449/* Given a [start, len], this function calls w25_range_to_status() to convert
450 * it to flash-chip-specific range bits, then sets into status register.
451 */
David Hendricksd494b0a2010-08-16 16:28:50 -0700452static int w25_set_range(struct flashchip *flash,
453 unsigned int start, unsigned int len)
454{
455 struct w25q_status status;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800456 int tmp = 0;
457 int expected = 0;
David Hendricksd494b0a2010-08-16 16:28:50 -0700458
459 memset(&status, 0, sizeof(status));
460 tmp = spi_read_status_register();
461 memcpy(&status, &tmp, 1);
462 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
463
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800464 if (w25_range_to_status(flash, start, len, &status)) return -1;
David Hendricksf7924d12010-06-10 21:26:44 -0700465
466 msg_cdbg("status.busy: %x\n", status.busy);
467 msg_cdbg("status.wel: %x\n", status.wel);
468 msg_cdbg("status.bp0: %x\n", status.bp0);
469 msg_cdbg("status.bp1: %x\n", status.bp1);
470 msg_cdbg("status.bp2: %x\n", status.bp2);
471 msg_cdbg("status.tb: %x\n", status.tb);
472 msg_cdbg("status.sec: %x\n", status.sec);
473 msg_cdbg("status.srp0: %x\n", status.srp0);
474
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800475 memcpy(&expected, &status, sizeof(status));
476 spi_write_status_register_WREN(expected);
David Hendricksf7924d12010-06-10 21:26:44 -0700477
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800478 tmp = spi_read_status_register();
479 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
480 if ((tmp & MASK_WP_AREA) == (expected & MASK_WP_AREA)) {
481 msg_cinfo("SUCCESS.\n");
482 return 0;
483 } else {
484 msg_cinfo("FAILED, expected=0x%02x, but actual=0x%02x.\n",
485 expected, tmp);
486 return 1;
487 }
David Hendricksf7924d12010-06-10 21:26:44 -0700488}
489
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800490/* Print out the current status register value with human-readable text. */
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800491static int w25_wp_status(struct flashchip *flash)
492{
493 struct w25q_status status;
494 int tmp;
David Hendricksce8ded32010-10-08 11:23:38 -0700495 unsigned int start, len;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800496 int ret = 0;
497
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800498 memset(&status, 0, sizeof(status));
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800499 tmp = spi_read_status_register();
500 /* FIXME: this is NOT endian-free copy. */
501 memcpy(&status, &tmp, 1);
502 msg_cinfo("WP: status: 0x%02x\n", tmp);
503 msg_cinfo("WP: status.srp0: %x\n", status.srp0);
504 msg_cinfo("WP: write protect is %s.\n",
505 status.srp0 ? "enabled" : "disabled");
506
507 msg_cinfo("WP: write protect range: ");
508 if (w25_status_to_range(flash, &status, &start, &len)) {
509 msg_cinfo("(cannot resolve the range)\n");
510 ret = -1;
511 } else {
512 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
513 }
514
515 return ret;
516}
517
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800518/* Set/clear the SRP0 bit in the status register. */
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800519static int w25_set_srp0(struct flashchip *flash, int enable)
David Hendricksf7924d12010-06-10 21:26:44 -0700520{
521 struct w25q_status status;
522 int tmp = 0;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800523 int expected = 0;
David Hendricksf7924d12010-06-10 21:26:44 -0700524
525 memset(&status, 0, sizeof(status));
526 tmp = spi_read_status_register();
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800527 /* FIXME: this is NOT endian-free copy. */
David Hendricksf7924d12010-06-10 21:26:44 -0700528 memcpy(&status, &tmp, 1);
529 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
530
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800531 status.srp0 = enable ? 1 : 0;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800532 memcpy(&expected, &status, sizeof(status));
533 spi_write_status_register_WREN(expected);
534
535 tmp = spi_read_status_register();
536 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
537 if ((tmp & MASK_WP_AREA) != (expected & MASK_WP_AREA))
538 return 1;
David Hendricksf7924d12010-06-10 21:26:44 -0700539
540 return 0;
541}
542
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800543static int w25_enable_writeprotect(struct flashchip *flash)
544{
545 int ret;
546
547 ret = w25_set_srp0(flash, 1);
548 if (!ret)
549 msg_cinfo("SUCCESS.\n");
550 else
551 msg_cinfo("FAILED, error=%d.\n", ret);
552 return ret;
553}
554
555static int w25_disable_writeprotect(struct flashchip *flash)
556{
557 int ret;
558
559 ret = w25_set_srp0(flash, 0);
560 if (!ret)
561 msg_cinfo("SUCCESS.\n");
562 else
563 msg_cinfo("FAILED, error=%d.\n", ret);
564 return ret;
565}
566
David Hendricksf7924d12010-06-10 21:26:44 -0700567struct wp wp_w25 = {
568 .set_range = w25_set_range,
569 .enable = w25_enable_writeprotect,
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800570 .disable = w25_disable_writeprotect,
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800571 .wp_status = w25_wp_status,
David Hendricksf7924d12010-06-10 21:26:44 -0700572};