blob: ddcf34c3b996914b90a4867b769f4cc093aef531 [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 Hendrickse185bf22011-05-24 15:34:18 -070080struct w25q_range en25q40_ranges[] = {
81 { 0, 0, 0, {0, 0} }, /* none */
82 { 0, 0, 0x1, {0x000000, 504 * 1024} },
83 { 0, 0, 0x2, {0x000000, 496 * 1024} },
84 { 0, 0, 0x3, {0x000000, 480 * 1024} },
85
86 { 0, 1, 0x0, {0x000000, 448 * 1024} },
87 { 0, 1, 0x1, {0x000000, 384 * 1024} },
88 { 0, 1, 0x2, {0x000000, 256 * 1024} },
89 { 0, 1, 0x3, {0x000000, 512 * 1024} },
90};
91
92struct w25q_range en25q80_ranges[] = {
93 { 0, 0, 0, {0, 0} }, /* none */
94 { 0, 0, 0x1, {0x000000, 1016 * 1024} },
95 { 0, 0, 0x2, {0x000000, 1008 * 1024} },
96 { 0, 0, 0x3, {0x000000, 992 * 1024} },
97 { 0, 0, 0x4, {0x000000, 960 * 1024} },
98 { 0, 0, 0x5, {0x000000, 896 * 1024} },
99 { 0, 0, 0x6, {0x000000, 768 * 1024} },
100 { 0, 0, 0x7, {0x000000, 1024 * 1024} },
101};
102
103struct w25q_range en25q32_ranges[] = {
104 { 0, 0, 0, {0, 0} }, /* none */
105 { 0, 0, 0x1, {0x000000, 4032 * 1024} },
106 { 0, 0, 0x2, {0x000000, 3968 * 1024} },
107 { 0, 0, 0x3, {0x000000, 3840 * 1024} },
108 { 0, 0, 0x4, {0x000000, 3584 * 1024} },
109 { 0, 0, 0x5, {0x000000, 3072 * 1024} },
110 { 0, 0, 0x6, {0x000000, 2048 * 1024} },
111 { 0, 0, 0x7, {0x000000, 4096 * 1024} },
112
113 { 0, 1, 0, {0, 0} }, /* none */
114 { 0, 1, 0x1, {0x010000, 4032 * 1024} },
115 { 0, 1, 0x2, {0x020000, 3968 * 1024} },
116 { 0, 1, 0x3, {0x040000, 3840 * 1024} },
117 { 0, 1, 0x4, {0x080000, 3584 * 1024} },
118 { 0, 1, 0x5, {0x100000, 3072 * 1024} },
119 { 0, 1, 0x6, {0x200000, 2048 * 1024} },
120 { 0, 1, 0x7, {0x000000, 4096 * 1024} },
121};
122
123struct w25q_range en25q64_ranges[] = {
124 { 0, 0, 0, {0, 0} }, /* none */
125 { 0, 0, 0x1, {0x000000, 8128 * 1024} },
126 { 0, 0, 0x2, {0x000000, 8064 * 1024} },
127 { 0, 0, 0x3, {0x000000, 7936 * 1024} },
128 { 0, 0, 0x4, {0x000000, 7680 * 1024} },
129 { 0, 0, 0x5, {0x000000, 7168 * 1024} },
130 { 0, 0, 0x6, {0x000000, 6144 * 1024} },
131 { 0, 0, 0x7, {0x000000, 8192 * 1024} },
132
133 { 0, 1, 0, {0, 0} }, /* none */
134 { 0, 1, 0x1, {0x010000, 8128 * 1024} },
135 { 0, 1, 0x2, {0x020000, 8064 * 1024} },
136 { 0, 1, 0x3, {0x040000, 7936 * 1024} },
137 { 0, 1, 0x4, {0x080000, 7680 * 1024} },
138 { 0, 1, 0x5, {0x100000, 7168 * 1024} },
139 { 0, 1, 0x6, {0x200000, 6144 * 1024} },
140 { 0, 1, 0x7, {0x000000, 8192 * 1024} },
141};
142
143struct w25q_range en25q128_ranges[] = {
144 { 0, 0, 0, {0, 0} }, /* none */
145 { 0, 0, 0x1, {0x000000, 16320 * 1024} },
146 { 0, 0, 0x2, {0x000000, 16256 * 1024} },
147 { 0, 0, 0x3, {0x000000, 16128 * 1024} },
148 { 0, 0, 0x4, {0x000000, 15872 * 1024} },
149 { 0, 0, 0x5, {0x000000, 15360 * 1024} },
150 { 0, 0, 0x6, {0x000000, 14336 * 1024} },
151 { 0, 0, 0x7, {0x000000, 16384 * 1024} },
152
153 { 0, 1, 0, {0, 0} }, /* none */
154 { 0, 1, 0x1, {0x010000, 16320 * 1024} },
155 { 0, 1, 0x2, {0x020000, 16256 * 1024} },
156 { 0, 1, 0x3, {0x040000, 16128 * 1024} },
157 { 0, 1, 0x4, {0x080000, 15872 * 1024} },
158 { 0, 1, 0x5, {0x100000, 15360 * 1024} },
159 { 0, 1, 0x6, {0x200000, 14336 * 1024} },
160 { 0, 1, 0x7, {0x000000, 16384 * 1024} },
161};
162
David Hendricksf8f00c72011-02-01 12:39:46 -0800163/* mx25l1005 ranges also work for the mx25l1005c */
164static struct w25q_range mx25l1005_ranges[] = {
165 { X, X, 0, {0, 0} }, /* none */
166 { X, X, 0x1, {0x010000, 64 * 1024} },
167 { X, X, 0x2, {0x000000, 128 * 1024} },
168 { X, X, 0x3, {0x000000, 128 * 1024} },
169};
170
171static struct w25q_range mx25l2005_ranges[] = {
172 { X, X, 0, {0, 0} }, /* none */
173 { X, X, 0x1, {0x030000, 64 * 1024} },
174 { X, X, 0x2, {0x020000, 128 * 1024} },
175 { X, X, 0x3, {0x000000, 256 * 1024} },
176};
177
178static struct w25q_range mx25l4005_ranges[] = {
179 { X, X, 0, {0, 0} }, /* none */
180 { X, X, 0x1, {0x070000, 64 * 1 * 1024} }, /* block 7 */
181 { X, X, 0x2, {0x060000, 64 * 2 * 1024} }, /* blocks 6-7 */
182 { X, X, 0x3, {0x040000, 64 * 4 * 1024} }, /* blocks 4-7 */
183 { X, X, 0x4, {0x000000, 512 * 1024} },
184 { X, X, 0x5, {0x000000, 512 * 1024} },
185 { X, X, 0x6, {0x000000, 512 * 1024} },
186 { X, X, 0x7, {0x000000, 512 * 1024} },
187};
188
189static struct w25q_range mx25l8005_ranges[] = {
190 { X, X, 0, {0, 0} }, /* none */
191 { X, X, 0x1, {0x0f0000, 64 * 1 * 1024} }, /* block 15 */
192 { X, X, 0x2, {0x0e0000, 64 * 2 * 1024} }, /* blocks 14-15 */
193 { X, X, 0x3, {0x0c0000, 64 * 4 * 1024} }, /* blocks 12-15 */
194 { X, X, 0x4, {0x080000, 64 * 8 * 1024} }, /* blocks 8-15 */
195 { X, X, 0x5, {0x000000, 1024 * 1024} },
196 { X, X, 0x6, {0x000000, 1024 * 1024} },
197 { X, X, 0x7, {0x000000, 1024 * 1024} },
198};
199
200#if 0
201/* FIXME: mx25l1605 has the same IDs as the mx25l1605d */
202static struct w25q_range mx25l1605_ranges[] = {
203 { X, X, 0, {0, 0} }, /* none */
204 { X, X, 0x1, {0x1f0000, 64 * 1024} }, /* block 31 */
205 { X, X, 0x2, {0x1e0000, 128 * 1024} }, /* blocks 30-31 */
206 { X, X, 0x3, {0x1c0000, 256 * 1024} }, /* blocks 28-31 */
207 { X, X, 0x4, {0x180000, 512 * 1024} }, /* blocks 24-31 */
208 { X, X, 0x4, {0x100000, 1024 * 1024} }, /* blocks 16-31 */
209 { X, X, 0x6, {0x000000, 2048 * 1024} },
210 { X, X, 0x7, {0x000000, 2048 * 1024} },
211};
212#endif
213
214#if 0
215/* FIXME: mx25l6405 has the same IDs as the mx25l6405d */
216static struct w25q_range mx25l6405_ranges[] = {
217 { X, 0, 0, {0, 0} }, /* none */
218 { X, 0, 0x1, {0x7f0000, 64 * 1 * 1024} }, /* block 127 */
219 { X, 0, 0x2, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */
220 { X, 0, 0x3, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */
221 { X, 0, 0x4, {0x780000, 64 * 8 * 1024} }, /* blocks 120-127 */
222 { X, 0, 0x5, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */
223 { X, 0, 0x6, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */
224 { X, 0, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
225
226 { X, 1, 0x0, {0x000000, 8192 * 1024} },
227 { X, 1, 0x1, {0x000000, 8192 * 1024} },
228 { X, 1, 0x2, {0x000000, 8192 * 1024} },
229 { X, 1, 0x3, {0x000000, 8192 * 1024} },
230 { X, 1, 0x4, {0x000000, 8192 * 1024} },
231 { X, 1, 0x5, {0x000000, 8192 * 1024} },
232 { X, 1, 0x6, {0x000000, 8192 * 1024} },
233 { X, 1, 0x7, {0x000000, 8192 * 1024} },
234};
235#endif
236
237static struct w25q_range mx25l1605d_ranges[] = {
238 { X, 0, 0, {0, 0} }, /* none */
239 { X, 0, 0x1, {0x1f0000, 64 * 1 * 1024} }, /* block 31 */
240 { X, 0, 0x2, {0x1e0000, 64 * 2 * 1024} }, /* blocks 30-31 */
241 { X, 0, 0x3, {0x1c0000, 64 * 4 * 1024} }, /* blocks 28-31 */
242 { X, 0, 0x4, {0x180000, 64 * 8 * 1024} }, /* blocks 24-31 */
243 { X, 0, 0x5, {0x100000, 64 * 16 * 1024} }, /* blocks 16-31 */
244 { X, 0, 0x6, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
245 { X, 0, 0x7, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
246
247 { X, 1, 0x0, {0x000000, 2048 * 1024} },
248 { X, 1, 0x1, {0x000000, 2048 * 1024} },
249 { X, 1, 0x2, {0x000000, 64 * 16 * 1024} }, /* blocks 0-15 */
250 { X, 1, 0x3, {0x000000, 64 * 24 * 1024} }, /* blocks 0-23 */
251 { X, 1, 0x4, {0x000000, 64 * 28 * 1024} }, /* blocks 0-27 */
252 { X, 1, 0x5, {0x000000, 64 * 30 * 1024} }, /* blocks 0-29 */
253 { X, 1, 0x6, {0x000000, 64 * 31 * 1024} }, /* blocks 0-30 */
254 { X, 1, 0x7, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
255};
256
257/* FIXME: Is there an mx25l3205 (without a trailing letter)? */
David Hendricksac72e362010-08-16 18:20:03 -0700258static struct w25q_range mx25l3205d_ranges[] = {
259 { X, 0, 0, {0, 0} }, /* none */
260 { X, 0, 0x1, {0x3f0000, 64 * 1024} },
261 { X, 0, 0x2, {0x3e0000, 128 * 1024} },
262 { X, 0, 0x3, {0x3c0000, 256 * 1024} },
263 { X, 0, 0x4, {0x380000, 512 * 1024} },
264 { X, 0, 0x5, {0x300000, 1024 * 1024} },
265 { X, 0, 0x6, {0x200000, 2048 * 1024} },
266 { X, 0, 0x7, {0x000000, 4096 * 1024} },
267
268 { X, 1, 0x0, {0x000000, 4096 * 1024} },
269 { X, 1, 0x1, {0x000000, 2048 * 1024} },
270 { X, 1, 0x2, {0x000000, 3072 * 1024} },
271 { X, 1, 0x3, {0x000000, 3584 * 1024} },
272 { X, 1, 0x4, {0x000000, 3840 * 1024} },
273 { X, 1, 0x5, {0x000000, 3968 * 1024} },
274 { X, 1, 0x6, {0x000000, 4032 * 1024} },
275 { X, 1, 0x7, {0x000000, 4096 * 1024} },
276};
277
David Hendricksf8f00c72011-02-01 12:39:46 -0800278static struct w25q_range mx25l6405d_ranges[] = {
279 { X, 0, 0, {0, 0} }, /* none */
280 { X, 0, 0x1, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
281 { X, 0, 0x2, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
282 { X, 0, 0x3, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
283 { X, 0, 0x4, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
284 { X, 0, 0x5, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
285 { X, 0, 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
286 { X, 0, 0x7, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */
287
288 { X, 1, 0x0, {0x000000, 8192 * 1024} },
289 { X, 1, 0x1, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
290 { X, 1, 0x2, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */
291 { X, 1, 0x3, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */
292 { X, 1, 0x4, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */
293 { X, 1, 0x5, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */
294 { X, 1, 0x6, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */
295 { X, 1, 0x7, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */
296};
297
David Hendricksf7924d12010-06-10 21:26:44 -0700298static struct w25q_range w25q16_ranges[] = {
299 { X, X, 0, {0, 0} }, /* none */
300 { 0, 0, 0x1, {0x1f0000, 64 * 1024} },
301 { 0, 0, 0x2, {0x1e0000, 128 * 1024} },
302 { 0, 0, 0x3, {0x1c0000, 256 * 1024} },
303 { 0, 0, 0x4, {0x180000, 512 * 1024} },
304 { 0, 0, 0x5, {0x100000, 1024 * 1024} },
305
306 { 0, 1, 0x1, {0x000000, 64 * 1024} },
307 { 0, 1, 0x2, {0x000000, 128 * 1024} },
308 { 0, 1, 0x3, {0x000000, 256 * 1024} },
309 { 0, 1, 0x4, {0x000000, 512 * 1024} },
310 { 0, 1, 0x5, {0x000000, 1024 * 1024} },
311 { X, X, 0x6, {0x000000, 2048 * 1024} },
312 { X, X, 0x7, {0x000000, 2048 * 1024} },
313
314 { 1, 0, 0x1, {0x1ff000, 4 * 1024} },
315 { 1, 0, 0x2, {0x1fe000, 8 * 1024} },
316 { 1, 0, 0x3, {0x1fc000, 16 * 1024} },
317 { 1, 0, 0x4, {0x1f8000, 32 * 1024} },
318 { 1, 0, 0x5, {0x1f8000, 32 * 1024} },
319
320 { 1, 1, 0x1, {0x000000, 4 * 1024} },
321 { 1, 1, 0x2, {0x000000, 8 * 1024} },
322 { 1, 1, 0x3, {0x000000, 16 * 1024} },
323 { 1, 1, 0x4, {0x000000, 32 * 1024} },
324 { 1, 1, 0x5, {0x000000, 32 * 1024} },
325};
326
327static struct w25q_range w25q32_ranges[] = {
328 { X, X, 0, {0, 0} }, /* none */
329 { 0, 0, 0x1, {0x3f0000, 64 * 1024} },
330 { 0, 0, 0x2, {0x3e0000, 128 * 1024} },
331 { 0, 0, 0x3, {0x3c0000, 256 * 1024} },
332 { 0, 0, 0x4, {0x380000, 512 * 1024} },
333 { 0, 0, 0x5, {0x300000, 1024 * 1024} },
David Hendricks05653ff2010-06-15 16:05:12 -0700334 { 0, 0, 0x6, {0x200000, 2048 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700335
336 { 0, 1, 0x1, {0x000000, 64 * 1024} },
337 { 0, 1, 0x2, {0x000000, 128 * 1024} },
338 { 0, 1, 0x3, {0x000000, 256 * 1024} },
339 { 0, 1, 0x4, {0x000000, 512 * 1024} },
340 { 0, 1, 0x5, {0x000000, 1024 * 1024} },
341 { 0, 1, 0x6, {0x000000, 2048 * 1024} },
342 { X, X, 0x7, {0x000000, 4096 * 1024} },
343
344 { 1, 0, 0x1, {0x3ff000, 4 * 1024} },
345 { 1, 0, 0x2, {0x3fe000, 8 * 1024} },
346 { 1, 0, 0x3, {0x3fc000, 16 * 1024} },
347 { 1, 0, 0x4, {0x3f8000, 32 * 1024} },
348 { 1, 0, 0x5, {0x3f8000, 32 * 1024} },
349
350 { 1, 1, 0x1, {0x000000, 4 * 1024} },
351 { 1, 1, 0x2, {0x000000, 8 * 1024} },
352 { 1, 1, 0x3, {0x000000, 16 * 1024} },
353 { 1, 1, 0x4, {0x000000, 32 * 1024} },
354 { 1, 1, 0x5, {0x000000, 32 * 1024} },
355};
356
357static struct w25q_range w25q80_ranges[] = {
358 { X, X, 0, {0, 0} }, /* none */
359 { 0, 0, 0x1, {0x0f0000, 64 * 1024} },
360 { 0, 0, 0x2, {0x0e0000, 128 * 1024} },
361 { 0, 0, 0x3, {0x0c0000, 256 * 1024} },
362 { 0, 0, 0x4, {0x080000, 512 * 1024} },
363
364 { 0, 1, 0x1, {0x000000, 64 * 1024} },
365 { 0, 1, 0x2, {0x000000, 128 * 1024} },
366 { 0, 1, 0x3, {0x000000, 256 * 1024} },
367 { 0, 1, 0x4, {0x000000, 512 * 1024} },
David Hendricks05653ff2010-06-15 16:05:12 -0700368 { X, X, 0x6, {0x000000, 1024 * 1024} },
369 { X, X, 0x7, {0x000000, 1024 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700370
371 { 1, 0, 0x1, {0x1ff000, 4 * 1024} },
372 { 1, 0, 0x2, {0x1fe000, 8 * 1024} },
373 { 1, 0, 0x3, {0x1fc000, 16 * 1024} },
374 { 1, 0, 0x4, {0x1f8000, 32 * 1024} },
375 { 1, 0, 0x5, {0x1f8000, 32 * 1024} },
376
377 { 1, 1, 0x1, {0x000000, 4 * 1024} },
378 { 1, 1, 0x2, {0x000000, 8 * 1024} },
379 { 1, 1, 0x3, {0x000000, 16 * 1024} },
380 { 1, 1, 0x4, {0x000000, 32 * 1024} },
381 { 1, 1, 0x5, {0x000000, 32 * 1024} },
382};
383
David Hendricks2c4a76c2010-06-28 14:00:43 -0700384static struct w25q_range w25q64_ranges[] = {
385 { X, X, 0, {0, 0} }, /* none */
386
387 { 0, 0, 0x1, {0x7e0000, 128 * 1024} },
388 { 0, 0, 0x2, {0x7c0000, 256 * 1024} },
389 { 0, 0, 0x3, {0x780000, 512 * 1024} },
390 { 0, 0, 0x4, {0x700000, 1024 * 1024} },
391 { 0, 0, 0x5, {0x600000, 2048 * 1024} },
392 { 0, 0, 0x6, {0x400000, 4096 * 1024} },
393
394 { 0, 1, 0x1, {0x000000, 128 * 1024} },
395 { 0, 1, 0x2, {0x000000, 256 * 1024} },
396 { 0, 1, 0x3, {0x000000, 512 * 1024} },
397 { 0, 1, 0x4, {0x000000, 1024 * 1024} },
398 { 0, 1, 0x5, {0x000000, 2048 * 1024} },
399 { 0, 1, 0x6, {0x000000, 4096 * 1024} },
400 { X, X, 0x7, {0x000000, 8192 * 1024} },
401
402 { 1, 0, 0x1, {0x7ff000, 4 * 1024} },
403 { 1, 0, 0x2, {0x7fe000, 8 * 1024} },
404 { 1, 0, 0x3, {0x7fc000, 16 * 1024} },
405 { 1, 0, 0x4, {0x7f8000, 32 * 1024} },
406 { 1, 0, 0x5, {0x7f8000, 32 * 1024} },
407
408 { 1, 1, 0x1, {0x000000, 4 * 1024} },
409 { 1, 1, 0x2, {0x000000, 8 * 1024} },
410 { 1, 1, 0x3, {0x000000, 16 * 1024} },
411 { 1, 1, 0x4, {0x000000, 32 * 1024} },
412 { 1, 1, 0x5, {0x000000, 32 * 1024} },
413};
414
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800415struct w25q_range w25x10_ranges[] = {
416 { X, X, 0, {0, 0} }, /* none */
417 { 0, 0, 0x1, {0x010000, 64 * 1024} },
418 { 0, 1, 0x1, {0x000000, 64 * 1024} },
419 { X, X, 0x2, {0x000000, 128 * 1024} },
420 { X, X, 0x3, {0x000000, 128 * 1024} },
421};
422
423struct w25q_range w25x20_ranges[] = {
424 { X, X, 0, {0, 0} }, /* none */
425 { 0, 0, 0x1, {0x030000, 64 * 1024} },
426 { 0, 0, 0x2, {0x020000, 128 * 1024} },
427 { 0, 1, 0x1, {0x000000, 64 * 1024} },
428 { 0, 1, 0x2, {0x000000, 128 * 1024} },
429 { 0, X, 0x3, {0x000000, 256 * 1024} },
430};
431
David Hendricks470ca952010-08-13 14:01:53 -0700432struct w25q_range w25x40_ranges[] = {
433 { X, X, 0, {0, 0} }, /* none */
434 { 0, 0, 0x1, {0x070000, 64 * 1024} },
435 { 0, 0, 0x2, {0x060000, 128 * 1024} },
436 { 0, 0, 0x3, {0x040000, 256 * 1024} },
437 { 0, 1, 0x1, {0x000000, 64 * 1024} },
438 { 0, 1, 0x2, {0x000000, 128 * 1024} },
439 { 0, 1, 0x3, {0x000000, 256 * 1024} },
440 { 0, X, 0x4, {0x000000, 512 * 1024} },
441};
442
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800443struct w25q_range w25x80_ranges[] = {
444 { X, X, 0, {0, 0} }, /* none */
445 { 0, 0, 0x1, {0x0F0000, 64 * 1024} },
446 { 0, 0, 0x2, {0x0E0000, 128 * 1024} },
447 { 0, 0, 0x3, {0x0C0000, 256 * 1024} },
448 { 0, 0, 0x4, {0x080000, 512 * 1024} },
449 { 0, 1, 0x1, {0x000000, 64 * 1024} },
450 { 0, 1, 0x2, {0x000000, 128 * 1024} },
451 { 0, 1, 0x3, {0x000000, 256 * 1024} },
452 { 0, 1, 0x4, {0x000000, 512 * 1024} },
453 { 0, X, 0x5, {0x000000, 1024 * 1024} },
454 { 0, X, 0x6, {0x000000, 1024 * 1024} },
455 { 0, X, 0x7, {0x000000, 1024 * 1024} },
456};
457
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800458/* Given a flash chip, this function returns its range table. */
459static int w25_range_table(const struct flashchip *flash,
460 struct w25q_range **w25q_ranges,
461 int *num_entries)
David Hendricksf7924d12010-06-10 21:26:44 -0700462{
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800463 *w25q_ranges = 0;
464 *num_entries = 0;
David Hendricksf7924d12010-06-10 21:26:44 -0700465
David Hendricksd494b0a2010-08-16 16:28:50 -0700466 switch (flash->manufacture_id) {
467 case WINBOND_NEX_ID:
468 switch(flash->model_id) {
David Hendricksc801adb2010-12-09 16:58:56 -0800469 case WINBOND_NEX_W25X10:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800470 *w25q_ranges = w25x10_ranges;
471 *num_entries = ARRAY_SIZE(w25x10_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800472 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800473 case WINBOND_NEX_W25X20:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800474 *w25q_ranges = w25x20_ranges;
475 *num_entries = ARRAY_SIZE(w25x20_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800476 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800477 case WINBOND_NEX_W25X40:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800478 *w25q_ranges = w25x40_ranges;
479 *num_entries = ARRAY_SIZE(w25x40_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700480 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800481 case WINBOND_NEX_W25X80:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800482 *w25q_ranges = w25x80_ranges;
483 *num_entries = ARRAY_SIZE(w25x80_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800484 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800485 case WINBOND_NEX_W25Q80:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800486 *w25q_ranges = w25q80_ranges;
487 *num_entries = ARRAY_SIZE(w25q80_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700488 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800489 case WINBOND_NEX_W25Q16:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800490 *w25q_ranges = w25q16_ranges;
491 *num_entries = ARRAY_SIZE(w25q16_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700492 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800493 case WINBOND_NEX_W25Q32:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800494 *w25q_ranges = w25q32_ranges;
495 *num_entries = ARRAY_SIZE(w25q32_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700496 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800497 case WINBOND_NEX_W25Q64:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800498 *w25q_ranges = w25q64_ranges;
499 *num_entries = ARRAY_SIZE(w25q64_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700500 break;
501 default:
502 msg_cerr("%s() %d: WINBOND flash chip mismatch (0x%04x)"
503 ", aborting\n", __func__, __LINE__,
504 flash->model_id);
505 return -1;
506 }
David Hendricks2c4a76c2010-06-28 14:00:43 -0700507 break;
David Hendricks57566ed2010-08-16 18:24:45 -0700508 case EON_ID_NOPREFIX:
509 switch (flash->model_id) {
David Hendricksc801adb2010-12-09 16:58:56 -0800510 case EON_EN25F40:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800511 *w25q_ranges = en25f40_ranges;
512 *num_entries = ARRAY_SIZE(en25f40_ranges);
David Hendricks57566ed2010-08-16 18:24:45 -0700513 break;
David Hendrickse185bf22011-05-24 15:34:18 -0700514 case EON_EN25Q40:
515 *w25q_ranges = en25q40_ranges;
516 *num_entries = ARRAY_SIZE(en25q40_ranges);
517 break;
518 case EON_EN25Q80:
519 *w25q_ranges = en25q80_ranges;
520 *num_entries = ARRAY_SIZE(en25q80_ranges);
521 break;
522 case EON_EN25Q32:
523 *w25q_ranges = en25q32_ranges;
524 *num_entries = ARRAY_SIZE(en25q32_ranges);
525 break;
526 case EON_EN25Q64:
527 *w25q_ranges = en25q64_ranges;
528 *num_entries = ARRAY_SIZE(en25q64_ranges);
529 break;
530 case EON_EN25Q128:
531 *w25q_ranges = en25q128_ranges;
532 *num_entries = ARRAY_SIZE(en25q128_ranges);
533 break;
David Hendricks57566ed2010-08-16 18:24:45 -0700534 default:
535 msg_cerr("%s():%d: EON flash chip mismatch (0x%04x)"
536 ", aborting\n", __func__, __LINE__,
537 flash->model_id);
538 return -1;
539 }
540 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800541 case MACRONIX_ID:
David Hendricksac72e362010-08-16 18:20:03 -0700542 switch (flash->model_id) {
David Hendricksf8f00c72011-02-01 12:39:46 -0800543 case MACRONIX_MX25L1005:
544 *w25q_ranges = mx25l1005_ranges;
545 *num_entries = ARRAY_SIZE(mx25l1005_ranges);
546 break;
547 case MACRONIX_MX25L2005:
548 *w25q_ranges = mx25l2005_ranges;
549 *num_entries = ARRAY_SIZE(mx25l2005_ranges);
550 break;
551 case MACRONIX_MX25L4005:
552 *w25q_ranges = mx25l4005_ranges;
553 *num_entries = ARRAY_SIZE(mx25l4005_ranges);
554 break;
555 case MACRONIX_MX25L8005:
556 *w25q_ranges = mx25l8005_ranges;
557 *num_entries = ARRAY_SIZE(mx25l8005_ranges);
558 break;
559 case MACRONIX_MX25L1605:
560 /* FIXME: MX25L1605 and MX25L1605D have different write
561 * protection capabilities, but share IDs */
562 *w25q_ranges = mx25l1605d_ranges;
563 *num_entries = ARRAY_SIZE(mx25l1605d_ranges);
564 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800565 case MACRONIX_MX25L3205:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800566 *w25q_ranges = mx25l3205d_ranges;
567 *num_entries = ARRAY_SIZE(mx25l3205d_ranges);
David Hendricksac72e362010-08-16 18:20:03 -0700568 break;
David Hendricksf8f00c72011-02-01 12:39:46 -0800569 case MACRONIX_MX25L6405:
570 /* FIXME: MX25L6405 and MX25L6405D have different write
571 * protection capabilities, but share IDs */
572 *w25q_ranges = mx25l6405d_ranges;
573 *num_entries = ARRAY_SIZE(mx25l6405d_ranges);
574 break;
David Hendricksac72e362010-08-16 18:20:03 -0700575 default:
576 msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)"
577 ", aborting\n", __func__, __LINE__,
578 flash->model_id);
579 return -1;
580 }
581 break;
David Hendricksf7924d12010-06-10 21:26:44 -0700582 default:
David Hendricksd494b0a2010-08-16 16:28:50 -0700583 msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
584 __func__, flash->manufacture_id);
David Hendricksf7924d12010-06-10 21:26:44 -0700585 return -1;
586 }
587
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800588 return 0;
589}
590
591int w25_range_to_status(const struct flashchip *flash,
592 unsigned int start, unsigned int len,
593 struct w25q_status *status)
594{
595 struct w25q_range *w25q_ranges;
596 int i, range_found = 0;
597 int num_entries;
598
599 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
David Hendricksf7924d12010-06-10 21:26:44 -0700600 for (i = 0; i < num_entries; i++) {
601 struct wp_range *r = &w25q_ranges[i].range;
602
603 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
604 start, len, r->start, r->len);
605 if ((start == r->start) && (len == r->len)) {
David Hendricksd494b0a2010-08-16 16:28:50 -0700606 status->bp0 = w25q_ranges[i].bp & 1;
607 status->bp1 = w25q_ranges[i].bp >> 1;
608 status->bp2 = w25q_ranges[i].bp >> 2;
609 status->tb = w25q_ranges[i].tb;
610 status->sec = w25q_ranges[i].sec;
David Hendricksf7924d12010-06-10 21:26:44 -0700611
612 range_found = 1;
613 break;
614 }
615 }
616
617 if (!range_found) {
618 msg_cerr("matching range not found\n");
619 return -1;
620 }
David Hendricksd494b0a2010-08-16 16:28:50 -0700621 return 0;
622}
623
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800624int w25_status_to_range(const struct flashchip *flash,
625 const struct w25q_status *status,
626 unsigned int *start, unsigned int *len)
627{
628 struct w25q_range *w25q_ranges;
629 int i, status_found = 0;
630 int num_entries;
631
632 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
633 for (i = 0; i < num_entries; i++) {
634 int bp;
635
636 bp = status->bp0 | (status->bp1 << 1) | (status->bp2 << 2);
637 msg_cspew("comparing 0x%x 0x%x / 0x%x 0x%x / 0x%x 0x%x\n",
638 bp, w25q_ranges[i].bp,
639 status->tb, w25q_ranges[i].tb,
640 status->sec, w25q_ranges[i].sec);
641 if ((bp == w25q_ranges[i].bp) &&
642 (status->tb == w25q_ranges[i].tb) &&
643 (status->sec == w25q_ranges[i].sec)) {
644 *start = w25q_ranges[i].range.start;
645 *len = w25q_ranges[i].range.len;
646
647 status_found = 1;
648 break;
649 }
650 }
651
652 if (!status_found) {
653 msg_cerr("matching status not found\n");
654 return -1;
655 }
656 return 0;
657}
658
Louis Yung-Chieh Lo52aa9302010-09-06 10:45:02 +0800659/* Since most chips we use must be WREN-ed before WRSR,
660 * we copy a write status function here before we have a good solution. */
661static int spi_write_status_register_WREN(int status)
662{
663 int result;
664 struct spi_command cmds[] = {
665 {
666 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
667 .writecnt = JEDEC_WREN_OUTSIZE,
668 .writearr = (const unsigned char[]){ JEDEC_WREN },
669 .readcnt = 0,
670 .readarr = NULL,
671 }, {
672 .writecnt = JEDEC_WRSR_OUTSIZE,
673 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
674 .readcnt = 0,
675 .readarr = NULL,
676 }, {
677 .writecnt = 0,
678 .writearr = NULL,
679 .readcnt = 0,
680 .readarr = NULL,
681 }};
682
683 result = spi_send_multicommand(cmds);
684 if (result) {
685 msg_cerr("%s failed during command execution\n",
686 __func__);
687 }
Louis Yung-Chieh Lo96222b12010-11-01 11:48:11 +0800688
689 /* WRSR performs a self-timed erase before the changes take effect. */
690 programmer_delay(WRITE_STATUS_REGISTER_DELAY);
691
Louis Yung-Chieh Lo52aa9302010-09-06 10:45:02 +0800692 return result;
693}
694
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800695/* Given a [start, len], this function calls w25_range_to_status() to convert
696 * it to flash-chip-specific range bits, then sets into status register.
697 */
David Hendricks91040832011-07-08 20:01:09 -0700698static int w25_set_range(const struct flashchip *flash,
David Hendricksd494b0a2010-08-16 16:28:50 -0700699 unsigned int start, unsigned int len)
700{
701 struct w25q_status status;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800702 int tmp = 0;
703 int expected = 0;
David Hendricksd494b0a2010-08-16 16:28:50 -0700704
705 memset(&status, 0, sizeof(status));
706 tmp = spi_read_status_register();
707 memcpy(&status, &tmp, 1);
708 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
709
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800710 if (w25_range_to_status(flash, start, len, &status)) return -1;
David Hendricksf7924d12010-06-10 21:26:44 -0700711
712 msg_cdbg("status.busy: %x\n", status.busy);
713 msg_cdbg("status.wel: %x\n", status.wel);
714 msg_cdbg("status.bp0: %x\n", status.bp0);
715 msg_cdbg("status.bp1: %x\n", status.bp1);
716 msg_cdbg("status.bp2: %x\n", status.bp2);
717 msg_cdbg("status.tb: %x\n", status.tb);
718 msg_cdbg("status.sec: %x\n", status.sec);
719 msg_cdbg("status.srp0: %x\n", status.srp0);
720
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800721 memcpy(&expected, &status, sizeof(status));
722 spi_write_status_register_WREN(expected);
David Hendricksf7924d12010-06-10 21:26:44 -0700723
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800724 tmp = spi_read_status_register();
725 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
726 if ((tmp & MASK_WP_AREA) == (expected & MASK_WP_AREA)) {
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800727 return 0;
728 } else {
David Hendricksc801adb2010-12-09 16:58:56 -0800729 msg_cerr("expected=0x%02x, but actual=0x%02x.\n",
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800730 expected, tmp);
731 return 1;
732 }
David Hendricksf7924d12010-06-10 21:26:44 -0700733}
734
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800735/* Print out the current status register value with human-readable text. */
David Hendricks91040832011-07-08 20:01:09 -0700736static int w25_wp_status(const struct flashchip *flash)
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800737{
738 struct w25q_status status;
739 int tmp;
David Hendricksce8ded32010-10-08 11:23:38 -0700740 unsigned int start, len;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800741 int ret = 0;
742
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800743 memset(&status, 0, sizeof(status));
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800744 tmp = spi_read_status_register();
745 /* FIXME: this is NOT endian-free copy. */
746 memcpy(&status, &tmp, 1);
747 msg_cinfo("WP: status: 0x%02x\n", tmp);
748 msg_cinfo("WP: status.srp0: %x\n", status.srp0);
749 msg_cinfo("WP: write protect is %s.\n",
750 status.srp0 ? "enabled" : "disabled");
751
752 msg_cinfo("WP: write protect range: ");
753 if (w25_status_to_range(flash, &status, &start, &len)) {
754 msg_cinfo("(cannot resolve the range)\n");
755 ret = -1;
756 } else {
757 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
758 }
759
760 return ret;
761}
762
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800763/* Set/clear the SRP0 bit in the status register. */
David Hendricks91040832011-07-08 20:01:09 -0700764static int w25_set_srp0(const struct flashchip *flash, int enable)
David Hendricksf7924d12010-06-10 21:26:44 -0700765{
766 struct w25q_status status;
767 int tmp = 0;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800768 int expected = 0;
David Hendricksf7924d12010-06-10 21:26:44 -0700769
770 memset(&status, 0, sizeof(status));
771 tmp = spi_read_status_register();
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800772 /* FIXME: this is NOT endian-free copy. */
David Hendricksf7924d12010-06-10 21:26:44 -0700773 memcpy(&status, &tmp, 1);
774 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
775
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800776 status.srp0 = enable ? 1 : 0;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800777 memcpy(&expected, &status, sizeof(status));
778 spi_write_status_register_WREN(expected);
779
780 tmp = spi_read_status_register();
781 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
782 if ((tmp & MASK_WP_AREA) != (expected & MASK_WP_AREA))
783 return 1;
David Hendricksf7924d12010-06-10 21:26:44 -0700784
785 return 0;
786}
787
David Hendricks91040832011-07-08 20:01:09 -0700788static int w25_enable_writeprotect(const struct flashchip *flash)
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800789{
790 int ret;
791
792 ret = w25_set_srp0(flash, 1);
David Hendricksc801adb2010-12-09 16:58:56 -0800793 if (ret)
794 msg_cerr("%s(): error=%d.\n", __func__, ret);
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800795 return ret;
796}
797
David Hendricks91040832011-07-08 20:01:09 -0700798static int w25_disable_writeprotect(const struct flashchip *flash)
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800799{
800 int ret;
801
802 ret = w25_set_srp0(flash, 0);
David Hendricksc801adb2010-12-09 16:58:56 -0800803 if (ret)
804 msg_cerr("%s(): error=%d.\n", __func__, ret);
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800805 return ret;
806}
807
David Hendricks91040832011-07-08 20:01:09 -0700808static int w25_list_ranges(const struct flashchip *flash)
David Hendricks0f7f5382011-02-11 18:12:31 -0800809{
810 struct w25q_range *w25q_ranges;
811 int i, num_entries;
812
813 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
814 for (i = 0; i < num_entries; i++) {
815 msg_cinfo("start: 0x%06x, length: 0x%06x\n",
816 w25q_ranges[i].range.start,
817 w25q_ranges[i].range.len);
818 }
819
820 return 0;
821}
822
David Hendricksf7924d12010-06-10 21:26:44 -0700823struct wp wp_w25 = {
David Hendricks0f7f5382011-02-11 18:12:31 -0800824 .list_ranges = w25_list_ranges,
David Hendricksf7924d12010-06-10 21:26:44 -0700825 .set_range = w25_set_range,
826 .enable = w25_enable_writeprotect,
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800827 .disable = w25_disable_writeprotect,
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800828 .wp_status = w25_wp_status,
David Hendricksf7924d12010-06-10 21:26:44 -0700829};
David Hendricksf8f00c72011-02-01 12:39:46 -0800830
831