blob: 473b01b32175634e5790e724b51a015f6c7e7ef6 [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
David Hendricks1c09f802012-10-03 11:03:48 -070037/*
38 * Mask to extract write-protect enable and range bits
39 * Status register 1:
40 * SRP0: bit 7
41 * range(BP2-BP0): bit 4-2
42 * Status register 2:
43 * SRP1: bit 1
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +080044 */
45#define MASK_WP_AREA (0x9C)
David Hendricks1c09f802012-10-03 11:03:48 -070046#define MASK_WP2_AREA (0x01)
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +080047
David Hendricksf7924d12010-06-10 21:26:44 -070048/*
49 * The following procedures rely on look-up tables to match the user-specified
50 * range with the chip's supported ranges. This turned out to be the most
51 * elegant approach since diferent flash chips use different levels of
52 * granularity and methods to determine protected ranges. In other words,
53 * be stupid and simple since clever arithmetic will not for many chips.
54 */
55
56struct wp_range {
57 unsigned int start; /* starting address */
58 unsigned int len; /* len */
59};
60
61enum bit_state {
62 OFF = 0,
63 ON = 1,
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +080064 X = -1 /* don't care. Must be bigger than max # of bp. */
David Hendricksf7924d12010-06-10 21:26:44 -070065};
66
67struct w25q_range {
68 enum bit_state sec; /* if 1, bp[2:0] describe sectors */
69 enum bit_state tb; /* top/bottom select */
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +080070 int bp; /* block protect bitfield */
David Hendricksf7924d12010-06-10 21:26:44 -070071 struct wp_range range;
72};
73
David Hendricks57566ed2010-08-16 18:24:45 -070074struct w25q_range en25f40_ranges[] = {
75 { X, X, 0, {0, 0} }, /* none */
76 { 0, 0, 0x1, {0x000000, 504 * 1024} },
77 { 0, 0, 0x2, {0x000000, 496 * 1024} },
78 { 0, 0, 0x3, {0x000000, 480 * 1024} },
79 { 0, 0, 0x4, {0x000000, 448 * 1024} },
80 { 0, 0, 0x5, {0x000000, 384 * 1024} },
81 { 0, 0, 0x6, {0x000000, 256 * 1024} },
82 { 0, 0, 0x7, {0x000000, 512 * 1024} },
83};
84
David Hendrickse185bf22011-05-24 15:34:18 -070085struct w25q_range en25q40_ranges[] = {
86 { 0, 0, 0, {0, 0} }, /* none */
87 { 0, 0, 0x1, {0x000000, 504 * 1024} },
88 { 0, 0, 0x2, {0x000000, 496 * 1024} },
89 { 0, 0, 0x3, {0x000000, 480 * 1024} },
90
91 { 0, 1, 0x0, {0x000000, 448 * 1024} },
92 { 0, 1, 0x1, {0x000000, 384 * 1024} },
93 { 0, 1, 0x2, {0x000000, 256 * 1024} },
94 { 0, 1, 0x3, {0x000000, 512 * 1024} },
95};
96
97struct w25q_range en25q80_ranges[] = {
98 { 0, 0, 0, {0, 0} }, /* none */
99 { 0, 0, 0x1, {0x000000, 1016 * 1024} },
100 { 0, 0, 0x2, {0x000000, 1008 * 1024} },
101 { 0, 0, 0x3, {0x000000, 992 * 1024} },
102 { 0, 0, 0x4, {0x000000, 960 * 1024} },
103 { 0, 0, 0x5, {0x000000, 896 * 1024} },
104 { 0, 0, 0x6, {0x000000, 768 * 1024} },
105 { 0, 0, 0x7, {0x000000, 1024 * 1024} },
106};
107
108struct w25q_range en25q32_ranges[] = {
109 { 0, 0, 0, {0, 0} }, /* none */
110 { 0, 0, 0x1, {0x000000, 4032 * 1024} },
111 { 0, 0, 0x2, {0x000000, 3968 * 1024} },
112 { 0, 0, 0x3, {0x000000, 3840 * 1024} },
113 { 0, 0, 0x4, {0x000000, 3584 * 1024} },
114 { 0, 0, 0x5, {0x000000, 3072 * 1024} },
115 { 0, 0, 0x6, {0x000000, 2048 * 1024} },
116 { 0, 0, 0x7, {0x000000, 4096 * 1024} },
117
118 { 0, 1, 0, {0, 0} }, /* none */
119 { 0, 1, 0x1, {0x010000, 4032 * 1024} },
120 { 0, 1, 0x2, {0x020000, 3968 * 1024} },
121 { 0, 1, 0x3, {0x040000, 3840 * 1024} },
122 { 0, 1, 0x4, {0x080000, 3584 * 1024} },
123 { 0, 1, 0x5, {0x100000, 3072 * 1024} },
124 { 0, 1, 0x6, {0x200000, 2048 * 1024} },
125 { 0, 1, 0x7, {0x000000, 4096 * 1024} },
126};
127
128struct w25q_range en25q64_ranges[] = {
129 { 0, 0, 0, {0, 0} }, /* none */
130 { 0, 0, 0x1, {0x000000, 8128 * 1024} },
131 { 0, 0, 0x2, {0x000000, 8064 * 1024} },
132 { 0, 0, 0x3, {0x000000, 7936 * 1024} },
133 { 0, 0, 0x4, {0x000000, 7680 * 1024} },
134 { 0, 0, 0x5, {0x000000, 7168 * 1024} },
135 { 0, 0, 0x6, {0x000000, 6144 * 1024} },
136 { 0, 0, 0x7, {0x000000, 8192 * 1024} },
137
138 { 0, 1, 0, {0, 0} }, /* none */
139 { 0, 1, 0x1, {0x010000, 8128 * 1024} },
140 { 0, 1, 0x2, {0x020000, 8064 * 1024} },
141 { 0, 1, 0x3, {0x040000, 7936 * 1024} },
142 { 0, 1, 0x4, {0x080000, 7680 * 1024} },
143 { 0, 1, 0x5, {0x100000, 7168 * 1024} },
144 { 0, 1, 0x6, {0x200000, 6144 * 1024} },
145 { 0, 1, 0x7, {0x000000, 8192 * 1024} },
146};
147
148struct w25q_range en25q128_ranges[] = {
149 { 0, 0, 0, {0, 0} }, /* none */
150 { 0, 0, 0x1, {0x000000, 16320 * 1024} },
151 { 0, 0, 0x2, {0x000000, 16256 * 1024} },
152 { 0, 0, 0x3, {0x000000, 16128 * 1024} },
153 { 0, 0, 0x4, {0x000000, 15872 * 1024} },
154 { 0, 0, 0x5, {0x000000, 15360 * 1024} },
155 { 0, 0, 0x6, {0x000000, 14336 * 1024} },
156 { 0, 0, 0x7, {0x000000, 16384 * 1024} },
157
158 { 0, 1, 0, {0, 0} }, /* none */
159 { 0, 1, 0x1, {0x010000, 16320 * 1024} },
160 { 0, 1, 0x2, {0x020000, 16256 * 1024} },
161 { 0, 1, 0x3, {0x040000, 16128 * 1024} },
162 { 0, 1, 0x4, {0x080000, 15872 * 1024} },
163 { 0, 1, 0x5, {0x100000, 15360 * 1024} },
164 { 0, 1, 0x6, {0x200000, 14336 * 1024} },
165 { 0, 1, 0x7, {0x000000, 16384 * 1024} },
166};
167
David Hendricksf8f00c72011-02-01 12:39:46 -0800168/* mx25l1005 ranges also work for the mx25l1005c */
169static struct w25q_range mx25l1005_ranges[] = {
170 { X, X, 0, {0, 0} }, /* none */
171 { X, X, 0x1, {0x010000, 64 * 1024} },
172 { X, X, 0x2, {0x000000, 128 * 1024} },
173 { X, X, 0x3, {0x000000, 128 * 1024} },
174};
175
176static struct w25q_range mx25l2005_ranges[] = {
177 { X, X, 0, {0, 0} }, /* none */
178 { X, X, 0x1, {0x030000, 64 * 1024} },
179 { X, X, 0x2, {0x020000, 128 * 1024} },
180 { X, X, 0x3, {0x000000, 256 * 1024} },
181};
182
183static struct w25q_range mx25l4005_ranges[] = {
184 { X, X, 0, {0, 0} }, /* none */
185 { X, X, 0x1, {0x070000, 64 * 1 * 1024} }, /* block 7 */
186 { X, X, 0x2, {0x060000, 64 * 2 * 1024} }, /* blocks 6-7 */
187 { X, X, 0x3, {0x040000, 64 * 4 * 1024} }, /* blocks 4-7 */
188 { X, X, 0x4, {0x000000, 512 * 1024} },
189 { X, X, 0x5, {0x000000, 512 * 1024} },
190 { X, X, 0x6, {0x000000, 512 * 1024} },
191 { X, X, 0x7, {0x000000, 512 * 1024} },
192};
193
194static struct w25q_range mx25l8005_ranges[] = {
195 { X, X, 0, {0, 0} }, /* none */
196 { X, X, 0x1, {0x0f0000, 64 * 1 * 1024} }, /* block 15 */
197 { X, X, 0x2, {0x0e0000, 64 * 2 * 1024} }, /* blocks 14-15 */
198 { X, X, 0x3, {0x0c0000, 64 * 4 * 1024} }, /* blocks 12-15 */
199 { X, X, 0x4, {0x080000, 64 * 8 * 1024} }, /* blocks 8-15 */
200 { X, X, 0x5, {0x000000, 1024 * 1024} },
201 { X, X, 0x6, {0x000000, 1024 * 1024} },
202 { X, X, 0x7, {0x000000, 1024 * 1024} },
203};
204
205#if 0
206/* FIXME: mx25l1605 has the same IDs as the mx25l1605d */
207static struct w25q_range mx25l1605_ranges[] = {
208 { X, X, 0, {0, 0} }, /* none */
209 { X, X, 0x1, {0x1f0000, 64 * 1024} }, /* block 31 */
210 { X, X, 0x2, {0x1e0000, 128 * 1024} }, /* blocks 30-31 */
211 { X, X, 0x3, {0x1c0000, 256 * 1024} }, /* blocks 28-31 */
212 { X, X, 0x4, {0x180000, 512 * 1024} }, /* blocks 24-31 */
213 { X, X, 0x4, {0x100000, 1024 * 1024} }, /* blocks 16-31 */
214 { X, X, 0x6, {0x000000, 2048 * 1024} },
215 { X, X, 0x7, {0x000000, 2048 * 1024} },
216};
217#endif
218
219#if 0
220/* FIXME: mx25l6405 has the same IDs as the mx25l6405d */
221static struct w25q_range mx25l6405_ranges[] = {
222 { X, 0, 0, {0, 0} }, /* none */
223 { X, 0, 0x1, {0x7f0000, 64 * 1 * 1024} }, /* block 127 */
224 { X, 0, 0x2, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */
225 { X, 0, 0x3, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */
226 { X, 0, 0x4, {0x780000, 64 * 8 * 1024} }, /* blocks 120-127 */
227 { X, 0, 0x5, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */
228 { X, 0, 0x6, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */
229 { X, 0, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
230
231 { X, 1, 0x0, {0x000000, 8192 * 1024} },
232 { X, 1, 0x1, {0x000000, 8192 * 1024} },
233 { X, 1, 0x2, {0x000000, 8192 * 1024} },
234 { X, 1, 0x3, {0x000000, 8192 * 1024} },
235 { X, 1, 0x4, {0x000000, 8192 * 1024} },
236 { X, 1, 0x5, {0x000000, 8192 * 1024} },
237 { X, 1, 0x6, {0x000000, 8192 * 1024} },
238 { X, 1, 0x7, {0x000000, 8192 * 1024} },
239};
240#endif
241
242static struct w25q_range mx25l1605d_ranges[] = {
243 { X, 0, 0, {0, 0} }, /* none */
244 { X, 0, 0x1, {0x1f0000, 64 * 1 * 1024} }, /* block 31 */
245 { X, 0, 0x2, {0x1e0000, 64 * 2 * 1024} }, /* blocks 30-31 */
246 { X, 0, 0x3, {0x1c0000, 64 * 4 * 1024} }, /* blocks 28-31 */
247 { X, 0, 0x4, {0x180000, 64 * 8 * 1024} }, /* blocks 24-31 */
248 { X, 0, 0x5, {0x100000, 64 * 16 * 1024} }, /* blocks 16-31 */
249 { X, 0, 0x6, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
250 { X, 0, 0x7, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
251
252 { X, 1, 0x0, {0x000000, 2048 * 1024} },
253 { X, 1, 0x1, {0x000000, 2048 * 1024} },
254 { X, 1, 0x2, {0x000000, 64 * 16 * 1024} }, /* blocks 0-15 */
255 { X, 1, 0x3, {0x000000, 64 * 24 * 1024} }, /* blocks 0-23 */
256 { X, 1, 0x4, {0x000000, 64 * 28 * 1024} }, /* blocks 0-27 */
257 { X, 1, 0x5, {0x000000, 64 * 30 * 1024} }, /* blocks 0-29 */
258 { X, 1, 0x6, {0x000000, 64 * 31 * 1024} }, /* blocks 0-30 */
259 { X, 1, 0x7, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
260};
261
262/* FIXME: Is there an mx25l3205 (without a trailing letter)? */
David Hendricksac72e362010-08-16 18:20:03 -0700263static struct w25q_range mx25l3205d_ranges[] = {
264 { X, 0, 0, {0, 0} }, /* none */
265 { X, 0, 0x1, {0x3f0000, 64 * 1024} },
266 { X, 0, 0x2, {0x3e0000, 128 * 1024} },
267 { X, 0, 0x3, {0x3c0000, 256 * 1024} },
268 { X, 0, 0x4, {0x380000, 512 * 1024} },
269 { X, 0, 0x5, {0x300000, 1024 * 1024} },
270 { X, 0, 0x6, {0x200000, 2048 * 1024} },
271 { X, 0, 0x7, {0x000000, 4096 * 1024} },
272
273 { X, 1, 0x0, {0x000000, 4096 * 1024} },
274 { X, 1, 0x1, {0x000000, 2048 * 1024} },
275 { X, 1, 0x2, {0x000000, 3072 * 1024} },
276 { X, 1, 0x3, {0x000000, 3584 * 1024} },
277 { X, 1, 0x4, {0x000000, 3840 * 1024} },
278 { X, 1, 0x5, {0x000000, 3968 * 1024} },
279 { X, 1, 0x6, {0x000000, 4032 * 1024} },
280 { X, 1, 0x7, {0x000000, 4096 * 1024} },
281};
282
Vincent Palatin87e092a2013-02-28 15:46:14 -0800283static struct w25q_range mx25u3235e_ranges[] = {
284 { X, 0, 0, {0, 0} }, /* none */
285 { 0, 0, 0x1, {0x3f0000, 64 * 1024} },
286 { 0, 0, 0x2, {0x3e0000, 128 * 1024} },
287 { 0, 0, 0x3, {0x3c0000, 256 * 1024} },
288 { 0, 0, 0x4, {0x380000, 512 * 1024} },
289 { 0, 0, 0x5, {0x300000, 1024 * 1024} },
290 { 0, 0, 0x6, {0x200000, 2048 * 1024} },
291 { 0, 0, 0x7, {0x000000, 4096 * 1024} },
292
293 { 0, 1, 0x0, {0x000000, 4096 * 1024} },
294 { 0, 1, 0x1, {0x000000, 2048 * 1024} },
295 { 0, 1, 0x2, {0x000000, 3072 * 1024} },
296 { 0, 1, 0x3, {0x000000, 3584 * 1024} },
297 { 0, 1, 0x4, {0x000000, 3840 * 1024} },
298 { 0, 1, 0x5, {0x000000, 3968 * 1024} },
299 { 0, 1, 0x6, {0x000000, 4032 * 1024} },
300 { 0, 1, 0x7, {0x000000, 4096 * 1024} },
301};
302
David Hendricks1c9bc9c2011-07-20 15:25:44 -0700303#if 0
304/* FIXME: MX25L6405D has same ID as MX25L6406 */
David Hendricksf8f00c72011-02-01 12:39:46 -0800305static struct w25q_range mx25l6405d_ranges[] = {
306 { X, 0, 0, {0, 0} }, /* none */
307 { X, 0, 0x1, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
308 { X, 0, 0x2, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
309 { X, 0, 0x3, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
310 { X, 0, 0x4, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
311 { X, 0, 0x5, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
312 { X, 0, 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
313 { X, 0, 0x7, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */
314
315 { X, 1, 0x0, {0x000000, 8192 * 1024} },
316 { X, 1, 0x1, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
317 { X, 1, 0x2, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */
318 { X, 1, 0x3, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */
319 { X, 1, 0x4, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */
320 { X, 1, 0x5, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */
321 { X, 1, 0x6, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */
322 { X, 1, 0x7, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */
323};
David Hendricks1c9bc9c2011-07-20 15:25:44 -0700324#endif
325
326/* FIXME: MX25L6406 has same ID as MX25L6405D */
327static struct w25q_range mx25l6406e_ranges[] = {
328 { X, 0, 0, {0, 0} }, /* none */
329 { X, 0, 0x1, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */
330 { X, 0, 0x2, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */
331 { X, 0, 0x3, {0x7a0000, 64 * 8 * 1024} }, /* blocks 120-127 */
332 { X, 0, 0x4, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */
333 { X, 0, 0x5, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */
334 { X, 0, 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
335 { X, 0, 0x7, {0x000000, 64 * 128 * 1024} }, /* all */
336
337 { X, 1, 0x0, {0x000000, 64 * 128 * 1024} }, /* all */
338 { X, 1, 0x1, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
339 { X, 1, 0x2, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */
340 { X, 1, 0x3, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */
341 { X, 1, 0x4, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */
342 { X, 1, 0x5, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */
343 { X, 1, 0x6, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */
344 { X, 1, 0x7, {0x000000, 64 * 128 * 1024} }, /* all */
345};
David Hendricksf8f00c72011-02-01 12:39:46 -0800346
David Hendricksbfa624b2012-07-24 12:47:59 -0700347static struct w25q_range n25q064_ranges[] = {
348 { X, 0, 0, {0, 0} }, /* none */
349
350 { 0, 0, 0x1, {0x7f0000, 64 * 1024} }, /* block 127 */
351 { 0, 0, 0x2, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
352 { 0, 0, 0x3, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
353 { 0, 0, 0x4, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
354 { 0, 0, 0x5, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
355 { 0, 0, 0x6, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
356 { 0, 0, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
357
358 { 1, 0, 0x1, {0x000000, 64 * 1024} }, /* block 0 */
359 { 1, 0, 0x2, {0x000000, 2 * 64 * 1024} }, /* blocks 0-1 */
360 { 1, 0, 0x3, {0x000000, 4 * 64 * 1024} }, /* blocks 0-3 */
361 { 1, 0, 0x4, {0x000000, 8 * 64 * 1024} }, /* blocks 0-7 */
362 { 1, 0, 0x5, {0x000000, 16 * 64 * 1024} }, /* blocks 0-15 */
363 { 1, 0, 0x6, {0x000000, 32 * 64 * 1024} }, /* blocks 0-31 */
364 { 1, 0, 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
365
366 { X, 1, 0x0, {0x000000, 128 * 64 * 1024} }, /* all */
367 { X, 1, 0x1, {0x000000, 128 * 64 * 1024} }, /* all */
368 { X, 1, 0x2, {0x000000, 128 * 64 * 1024} }, /* all */
369 { X, 1, 0x3, {0x000000, 128 * 64 * 1024} }, /* all */
370 { X, 1, 0x4, {0x000000, 128 * 64 * 1024} }, /* all */
371 { X, 1, 0x5, {0x000000, 128 * 64 * 1024} }, /* all */
372 { X, 1, 0x6, {0x000000, 128 * 64 * 1024} }, /* all */
373 { X, 1, 0x7, {0x000000, 128 * 64 * 1024} }, /* all */
374};
375
David Hendricksf7924d12010-06-10 21:26:44 -0700376static struct w25q_range w25q16_ranges[] = {
377 { X, X, 0, {0, 0} }, /* none */
378 { 0, 0, 0x1, {0x1f0000, 64 * 1024} },
379 { 0, 0, 0x2, {0x1e0000, 128 * 1024} },
380 { 0, 0, 0x3, {0x1c0000, 256 * 1024} },
381 { 0, 0, 0x4, {0x180000, 512 * 1024} },
382 { 0, 0, 0x5, {0x100000, 1024 * 1024} },
383
384 { 0, 1, 0x1, {0x000000, 64 * 1024} },
385 { 0, 1, 0x2, {0x000000, 128 * 1024} },
386 { 0, 1, 0x3, {0x000000, 256 * 1024} },
387 { 0, 1, 0x4, {0x000000, 512 * 1024} },
388 { 0, 1, 0x5, {0x000000, 1024 * 1024} },
389 { X, X, 0x6, {0x000000, 2048 * 1024} },
390 { X, X, 0x7, {0x000000, 2048 * 1024} },
391
392 { 1, 0, 0x1, {0x1ff000, 4 * 1024} },
393 { 1, 0, 0x2, {0x1fe000, 8 * 1024} },
394 { 1, 0, 0x3, {0x1fc000, 16 * 1024} },
395 { 1, 0, 0x4, {0x1f8000, 32 * 1024} },
396 { 1, 0, 0x5, {0x1f8000, 32 * 1024} },
397
398 { 1, 1, 0x1, {0x000000, 4 * 1024} },
399 { 1, 1, 0x2, {0x000000, 8 * 1024} },
400 { 1, 1, 0x3, {0x000000, 16 * 1024} },
401 { 1, 1, 0x4, {0x000000, 32 * 1024} },
402 { 1, 1, 0x5, {0x000000, 32 * 1024} },
403};
404
405static struct w25q_range w25q32_ranges[] = {
406 { X, X, 0, {0, 0} }, /* none */
407 { 0, 0, 0x1, {0x3f0000, 64 * 1024} },
408 { 0, 0, 0x2, {0x3e0000, 128 * 1024} },
409 { 0, 0, 0x3, {0x3c0000, 256 * 1024} },
410 { 0, 0, 0x4, {0x380000, 512 * 1024} },
411 { 0, 0, 0x5, {0x300000, 1024 * 1024} },
David Hendricks05653ff2010-06-15 16:05:12 -0700412 { 0, 0, 0x6, {0x200000, 2048 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700413
414 { 0, 1, 0x1, {0x000000, 64 * 1024} },
415 { 0, 1, 0x2, {0x000000, 128 * 1024} },
416 { 0, 1, 0x3, {0x000000, 256 * 1024} },
417 { 0, 1, 0x4, {0x000000, 512 * 1024} },
418 { 0, 1, 0x5, {0x000000, 1024 * 1024} },
419 { 0, 1, 0x6, {0x000000, 2048 * 1024} },
420 { X, X, 0x7, {0x000000, 4096 * 1024} },
421
422 { 1, 0, 0x1, {0x3ff000, 4 * 1024} },
423 { 1, 0, 0x2, {0x3fe000, 8 * 1024} },
424 { 1, 0, 0x3, {0x3fc000, 16 * 1024} },
425 { 1, 0, 0x4, {0x3f8000, 32 * 1024} },
426 { 1, 0, 0x5, {0x3f8000, 32 * 1024} },
427
428 { 1, 1, 0x1, {0x000000, 4 * 1024} },
429 { 1, 1, 0x2, {0x000000, 8 * 1024} },
430 { 1, 1, 0x3, {0x000000, 16 * 1024} },
431 { 1, 1, 0x4, {0x000000, 32 * 1024} },
432 { 1, 1, 0x5, {0x000000, 32 * 1024} },
433};
434
435static struct w25q_range w25q80_ranges[] = {
436 { X, X, 0, {0, 0} }, /* none */
437 { 0, 0, 0x1, {0x0f0000, 64 * 1024} },
438 { 0, 0, 0x2, {0x0e0000, 128 * 1024} },
439 { 0, 0, 0x3, {0x0c0000, 256 * 1024} },
440 { 0, 0, 0x4, {0x080000, 512 * 1024} },
441
442 { 0, 1, 0x1, {0x000000, 64 * 1024} },
443 { 0, 1, 0x2, {0x000000, 128 * 1024} },
444 { 0, 1, 0x3, {0x000000, 256 * 1024} },
445 { 0, 1, 0x4, {0x000000, 512 * 1024} },
David Hendricks05653ff2010-06-15 16:05:12 -0700446 { X, X, 0x6, {0x000000, 1024 * 1024} },
447 { X, X, 0x7, {0x000000, 1024 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700448
449 { 1, 0, 0x1, {0x1ff000, 4 * 1024} },
450 { 1, 0, 0x2, {0x1fe000, 8 * 1024} },
451 { 1, 0, 0x3, {0x1fc000, 16 * 1024} },
452 { 1, 0, 0x4, {0x1f8000, 32 * 1024} },
453 { 1, 0, 0x5, {0x1f8000, 32 * 1024} },
454
455 { 1, 1, 0x1, {0x000000, 4 * 1024} },
456 { 1, 1, 0x2, {0x000000, 8 * 1024} },
457 { 1, 1, 0x3, {0x000000, 16 * 1024} },
458 { 1, 1, 0x4, {0x000000, 32 * 1024} },
459 { 1, 1, 0x5, {0x000000, 32 * 1024} },
460};
461
David Hendricks2c4a76c2010-06-28 14:00:43 -0700462static struct w25q_range w25q64_ranges[] = {
463 { X, X, 0, {0, 0} }, /* none */
464
465 { 0, 0, 0x1, {0x7e0000, 128 * 1024} },
466 { 0, 0, 0x2, {0x7c0000, 256 * 1024} },
467 { 0, 0, 0x3, {0x780000, 512 * 1024} },
468 { 0, 0, 0x4, {0x700000, 1024 * 1024} },
469 { 0, 0, 0x5, {0x600000, 2048 * 1024} },
470 { 0, 0, 0x6, {0x400000, 4096 * 1024} },
471
472 { 0, 1, 0x1, {0x000000, 128 * 1024} },
473 { 0, 1, 0x2, {0x000000, 256 * 1024} },
474 { 0, 1, 0x3, {0x000000, 512 * 1024} },
475 { 0, 1, 0x4, {0x000000, 1024 * 1024} },
476 { 0, 1, 0x5, {0x000000, 2048 * 1024} },
477 { 0, 1, 0x6, {0x000000, 4096 * 1024} },
478 { X, X, 0x7, {0x000000, 8192 * 1024} },
479
480 { 1, 0, 0x1, {0x7ff000, 4 * 1024} },
481 { 1, 0, 0x2, {0x7fe000, 8 * 1024} },
482 { 1, 0, 0x3, {0x7fc000, 16 * 1024} },
483 { 1, 0, 0x4, {0x7f8000, 32 * 1024} },
484 { 1, 0, 0x5, {0x7f8000, 32 * 1024} },
485
486 { 1, 1, 0x1, {0x000000, 4 * 1024} },
487 { 1, 1, 0x2, {0x000000, 8 * 1024} },
488 { 1, 1, 0x3, {0x000000, 16 * 1024} },
489 { 1, 1, 0x4, {0x000000, 32 * 1024} },
490 { 1, 1, 0x5, {0x000000, 32 * 1024} },
491};
492
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800493struct w25q_range w25x10_ranges[] = {
494 { X, X, 0, {0, 0} }, /* none */
495 { 0, 0, 0x1, {0x010000, 64 * 1024} },
496 { 0, 1, 0x1, {0x000000, 64 * 1024} },
497 { X, X, 0x2, {0x000000, 128 * 1024} },
498 { X, X, 0x3, {0x000000, 128 * 1024} },
499};
500
501struct w25q_range w25x20_ranges[] = {
502 { X, X, 0, {0, 0} }, /* none */
503 { 0, 0, 0x1, {0x030000, 64 * 1024} },
504 { 0, 0, 0x2, {0x020000, 128 * 1024} },
505 { 0, 1, 0x1, {0x000000, 64 * 1024} },
506 { 0, 1, 0x2, {0x000000, 128 * 1024} },
507 { 0, X, 0x3, {0x000000, 256 * 1024} },
508};
509
David Hendricks470ca952010-08-13 14:01:53 -0700510struct w25q_range w25x40_ranges[] = {
511 { X, X, 0, {0, 0} }, /* none */
512 { 0, 0, 0x1, {0x070000, 64 * 1024} },
513 { 0, 0, 0x2, {0x060000, 128 * 1024} },
514 { 0, 0, 0x3, {0x040000, 256 * 1024} },
515 { 0, 1, 0x1, {0x000000, 64 * 1024} },
516 { 0, 1, 0x2, {0x000000, 128 * 1024} },
517 { 0, 1, 0x3, {0x000000, 256 * 1024} },
518 { 0, X, 0x4, {0x000000, 512 * 1024} },
519};
520
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800521struct w25q_range w25x80_ranges[] = {
522 { X, X, 0, {0, 0} }, /* none */
523 { 0, 0, 0x1, {0x0F0000, 64 * 1024} },
524 { 0, 0, 0x2, {0x0E0000, 128 * 1024} },
525 { 0, 0, 0x3, {0x0C0000, 256 * 1024} },
526 { 0, 0, 0x4, {0x080000, 512 * 1024} },
527 { 0, 1, 0x1, {0x000000, 64 * 1024} },
528 { 0, 1, 0x2, {0x000000, 128 * 1024} },
529 { 0, 1, 0x3, {0x000000, 256 * 1024} },
530 { 0, 1, 0x4, {0x000000, 512 * 1024} },
531 { 0, X, 0x5, {0x000000, 1024 * 1024} },
532 { 0, X, 0x6, {0x000000, 1024 * 1024} },
533 { 0, X, 0x7, {0x000000, 1024 * 1024} },
534};
535
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700536static struct w25q_range gd25q64_ranges[] = {
537 { X, X, 0, {0, 0} }, /* none */
538 { 0, 0, 0x1, {0x7e0000, 128 * 1024} },
539 { 0, 0, 0x2, {0x7c0000, 256 * 1024} },
540 { 0, 0, 0x3, {0x780000, 512 * 1024} },
541 { 0, 0, 0x4, {0x700000, 1024 * 1024} },
542 { 0, 0, 0x5, {0x600000, 2048 * 1024} },
543 { 0, 0, 0x6, {0x400000, 4096 * 1024} },
544
545 { 0, 1, 0x1, {0x000000, 128 * 1024} },
546 { 0, 1, 0x2, {0x000000, 256 * 1024} },
547 { 0, 1, 0x3, {0x000000, 512 * 1024} },
548 { 0, 1, 0x4, {0x000000, 1024 * 1024} },
549 { 0, 1, 0x5, {0x000000, 2048 * 1024} },
550 { 0, 1, 0x6, {0x000000, 4096 * 1024} },
551 { X, X, 0x7, {0x000000, 8192 * 1024} },
552
553 { 1, 0, 0x1, {0x7ff000, 4 * 1024} },
554 { 1, 0, 0x2, {0x7fe000, 8 * 1024} },
555 { 1, 0, 0x3, {0x7fc000, 16 * 1024} },
556 { 1, 0, 0x4, {0x7f8000, 32 * 1024} },
557 { 1, 0, 0x5, {0x7f8000, 32 * 1024} },
558 { 1, 0, 0x6, {0x7f8000, 32 * 1024} },
559
560 { 1, 1, 0x1, {0x000000, 4 * 1024} },
561 { 1, 1, 0x2, {0x000000, 8 * 1024} },
562 { 1, 1, 0x3, {0x000000, 16 * 1024} },
563 { 1, 1, 0x4, {0x000000, 32 * 1024} },
564 { 1, 1, 0x5, {0x000000, 32 * 1024} },
565 { 1, 1, 0x6, {0x000000, 32 * 1024} },
566};
567
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +0800568static struct w25q_range a25l040_ranges[] = {
569 { X, X, 0x0, {0, 0} }, /* none */
570 { X, X, 0x1, {0x70000, 64 * 1024} },
571 { X, X, 0x2, {0x60000, 128 * 1024} },
572 { X, X, 0x3, {0x40000, 256 * 1024} },
573 { X, X, 0x4, {0x00000, 512 * 1024} },
574 { X, X, 0x5, {0x00000, 512 * 1024} },
575 { X, X, 0x6, {0x00000, 512 * 1024} },
576 { X, X, 0x7, {0x00000, 512 * 1024} },
577};
578
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800579/* Given a flash chip, this function returns its range table. */
580static int w25_range_table(const struct flashchip *flash,
581 struct w25q_range **w25q_ranges,
582 int *num_entries)
David Hendricksf7924d12010-06-10 21:26:44 -0700583{
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800584 *w25q_ranges = 0;
585 *num_entries = 0;
David Hendricksf7924d12010-06-10 21:26:44 -0700586
David Hendricksd494b0a2010-08-16 16:28:50 -0700587 switch (flash->manufacture_id) {
588 case WINBOND_NEX_ID:
589 switch(flash->model_id) {
David Hendricksc801adb2010-12-09 16:58:56 -0800590 case WINBOND_NEX_W25X10:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800591 *w25q_ranges = w25x10_ranges;
592 *num_entries = ARRAY_SIZE(w25x10_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800593 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800594 case WINBOND_NEX_W25X20:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800595 *w25q_ranges = w25x20_ranges;
596 *num_entries = ARRAY_SIZE(w25x20_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800597 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800598 case WINBOND_NEX_W25X40:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800599 *w25q_ranges = w25x40_ranges;
600 *num_entries = ARRAY_SIZE(w25x40_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700601 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800602 case WINBOND_NEX_W25X80:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800603 *w25q_ranges = w25x80_ranges;
604 *num_entries = ARRAY_SIZE(w25x80_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800605 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800606 case WINBOND_NEX_W25Q80:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800607 *w25q_ranges = w25q80_ranges;
608 *num_entries = ARRAY_SIZE(w25q80_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700609 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800610 case WINBOND_NEX_W25Q16:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800611 *w25q_ranges = w25q16_ranges;
612 *num_entries = ARRAY_SIZE(w25q16_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700613 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800614 case WINBOND_NEX_W25Q32:
Louis Yung-Chieh Lo469707f2012-05-18 16:38:37 +0800615 case WINBOND_NEX_W25Q32DW:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800616 *w25q_ranges = w25q32_ranges;
617 *num_entries = ARRAY_SIZE(w25q32_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700618 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800619 case WINBOND_NEX_W25Q64:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800620 *w25q_ranges = w25q64_ranges;
621 *num_entries = ARRAY_SIZE(w25q64_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700622 break;
623 default:
624 msg_cerr("%s() %d: WINBOND flash chip mismatch (0x%04x)"
625 ", aborting\n", __func__, __LINE__,
626 flash->model_id);
627 return -1;
628 }
David Hendricks2c4a76c2010-06-28 14:00:43 -0700629 break;
David Hendricks57566ed2010-08-16 18:24:45 -0700630 case EON_ID_NOPREFIX:
631 switch (flash->model_id) {
David Hendricksc801adb2010-12-09 16:58:56 -0800632 case EON_EN25F40:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800633 *w25q_ranges = en25f40_ranges;
634 *num_entries = ARRAY_SIZE(en25f40_ranges);
David Hendricks57566ed2010-08-16 18:24:45 -0700635 break;
David Hendrickse185bf22011-05-24 15:34:18 -0700636 case EON_EN25Q40:
637 *w25q_ranges = en25q40_ranges;
638 *num_entries = ARRAY_SIZE(en25q40_ranges);
639 break;
640 case EON_EN25Q80:
641 *w25q_ranges = en25q80_ranges;
642 *num_entries = ARRAY_SIZE(en25q80_ranges);
643 break;
644 case EON_EN25Q32:
645 *w25q_ranges = en25q32_ranges;
646 *num_entries = ARRAY_SIZE(en25q32_ranges);
647 break;
648 case EON_EN25Q64:
649 *w25q_ranges = en25q64_ranges;
650 *num_entries = ARRAY_SIZE(en25q64_ranges);
651 break;
652 case EON_EN25Q128:
653 *w25q_ranges = en25q128_ranges;
654 *num_entries = ARRAY_SIZE(en25q128_ranges);
655 break;
David Hendricks57566ed2010-08-16 18:24:45 -0700656 default:
657 msg_cerr("%s():%d: EON flash chip mismatch (0x%04x)"
658 ", aborting\n", __func__, __LINE__,
659 flash->model_id);
660 return -1;
661 }
662 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800663 case MACRONIX_ID:
David Hendricksac72e362010-08-16 18:20:03 -0700664 switch (flash->model_id) {
David Hendricksf8f00c72011-02-01 12:39:46 -0800665 case MACRONIX_MX25L1005:
666 *w25q_ranges = mx25l1005_ranges;
667 *num_entries = ARRAY_SIZE(mx25l1005_ranges);
668 break;
669 case MACRONIX_MX25L2005:
670 *w25q_ranges = mx25l2005_ranges;
671 *num_entries = ARRAY_SIZE(mx25l2005_ranges);
672 break;
673 case MACRONIX_MX25L4005:
674 *w25q_ranges = mx25l4005_ranges;
675 *num_entries = ARRAY_SIZE(mx25l4005_ranges);
676 break;
677 case MACRONIX_MX25L8005:
678 *w25q_ranges = mx25l8005_ranges;
679 *num_entries = ARRAY_SIZE(mx25l8005_ranges);
680 break;
681 case MACRONIX_MX25L1605:
682 /* FIXME: MX25L1605 and MX25L1605D have different write
683 * protection capabilities, but share IDs */
684 *w25q_ranges = mx25l1605d_ranges;
685 *num_entries = ARRAY_SIZE(mx25l1605d_ranges);
686 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800687 case MACRONIX_MX25L3205:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800688 *w25q_ranges = mx25l3205d_ranges;
689 *num_entries = ARRAY_SIZE(mx25l3205d_ranges);
David Hendricksac72e362010-08-16 18:20:03 -0700690 break;
Vincent Palatin87e092a2013-02-28 15:46:14 -0800691 case MACRONIX_MX25U3235E:
692 *w25q_ranges = mx25u3235e_ranges;
693 *num_entries = ARRAY_SIZE(mx25u3235e_ranges);
694 break;
David Hendricksf8f00c72011-02-01 12:39:46 -0800695 case MACRONIX_MX25L6405:
David Hendricks1c9bc9c2011-07-20 15:25:44 -0700696 /* FIXME: MX25L64* chips have mixed capabilities and
697 share IDs */
698 *w25q_ranges = mx25l6406e_ranges;
699 *num_entries = ARRAY_SIZE(mx25l6406e_ranges);
David Hendricksf8f00c72011-02-01 12:39:46 -0800700 break;
David Hendricksac72e362010-08-16 18:20:03 -0700701 default:
702 msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)"
703 ", aborting\n", __func__, __LINE__,
704 flash->model_id);
705 return -1;
706 }
707 break;
David Hendricksbfa624b2012-07-24 12:47:59 -0700708 case ST_ID:
709 switch(flash->model_id) {
710 case ST_N25Q064__1E:
711 case ST_N25Q064__3E:
712 *w25q_ranges = n25q064_ranges;
713 *num_entries = ARRAY_SIZE(n25q064_ranges);
714 break;
715 default:
716 msg_cerr("%s() %d: Micron flash chip mismatch"
717 " (0x%04x), aborting\n", __func__, __LINE__,
718 flash->model_id);
719 return -1;
720 }
721 break;
Bryan Freed9a0051f2012-05-22 16:06:09 -0700722 case GIGADEVICE_ID:
723 switch(flash->model_id) {
724 case GIGADEVICE_GD25LQ32:
725 *w25q_ranges = w25q32_ranges;
726 *num_entries = ARRAY_SIZE(w25q32_ranges);
727 break;
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700728 case GIGADEVICE_GD25Q64:
729 *w25q_ranges = gd25q64_ranges;
730 *num_entries = ARRAY_SIZE(gd25q64_ranges);
731 break;
732 /* TODO(shawnn): add support for other GD parts */
Bryan Freed9a0051f2012-05-22 16:06:09 -0700733 default:
734 msg_cerr("%s() %d: GigaDevice flash chip mismatch"
735 " (0x%04x), aborting\n", __func__, __LINE__,
736 flash->model_id);
737 return -1;
738 }
739 break;
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +0800740 case AMIC_ID_NOPREFIX:
741 switch(flash->model_id) {
742 case AMIC_A25L040:
743 *w25q_ranges = a25l040_ranges;
744 *num_entries = ARRAY_SIZE(a25l040_ranges);
745 break;
746 default:
747 msg_cerr("%s() %d: AMIC flash chip mismatch"
748 " (0x%04x), aborting\n", __func__, __LINE__,
749 flash->model_id);
750 return -1;
751 }
752 break;
David Hendricksf7924d12010-06-10 21:26:44 -0700753 default:
David Hendricksd494b0a2010-08-16 16:28:50 -0700754 msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
755 __func__, flash->manufacture_id);
David Hendricksf7924d12010-06-10 21:26:44 -0700756 return -1;
757 }
758
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800759 return 0;
760}
761
762int w25_range_to_status(const struct flashchip *flash,
763 unsigned int start, unsigned int len,
764 struct w25q_status *status)
765{
766 struct w25q_range *w25q_ranges;
767 int i, range_found = 0;
768 int num_entries;
769
770 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
David Hendricksf7924d12010-06-10 21:26:44 -0700771 for (i = 0; i < num_entries; i++) {
772 struct wp_range *r = &w25q_ranges[i].range;
773
774 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
775 start, len, r->start, r->len);
776 if ((start == r->start) && (len == r->len)) {
David Hendricksd494b0a2010-08-16 16:28:50 -0700777 status->bp0 = w25q_ranges[i].bp & 1;
778 status->bp1 = w25q_ranges[i].bp >> 1;
779 status->bp2 = w25q_ranges[i].bp >> 2;
780 status->tb = w25q_ranges[i].tb;
781 status->sec = w25q_ranges[i].sec;
David Hendricksf7924d12010-06-10 21:26:44 -0700782
783 range_found = 1;
784 break;
785 }
786 }
787
788 if (!range_found) {
789 msg_cerr("matching range not found\n");
790 return -1;
791 }
David Hendricksd494b0a2010-08-16 16:28:50 -0700792 return 0;
793}
794
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800795int w25_status_to_range(const struct flashchip *flash,
796 const struct w25q_status *status,
797 unsigned int *start, unsigned int *len)
798{
799 struct w25q_range *w25q_ranges;
800 int i, status_found = 0;
801 int num_entries;
802
803 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
804 for (i = 0; i < num_entries; i++) {
805 int bp;
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +0800806 int table_bp, table_tb, table_sec;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800807
808 bp = status->bp0 | (status->bp1 << 1) | (status->bp2 << 2);
809 msg_cspew("comparing 0x%x 0x%x / 0x%x 0x%x / 0x%x 0x%x\n",
810 bp, w25q_ranges[i].bp,
811 status->tb, w25q_ranges[i].tb,
812 status->sec, w25q_ranges[i].sec);
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +0800813 table_bp = w25q_ranges[i].bp;
814 table_tb = w25q_ranges[i].tb;
815 table_sec = w25q_ranges[i].sec;
816 if ((bp == table_bp || table_bp == X) &&
817 (status->tb == table_tb || table_tb == X) &&
818 (status->sec == table_sec || table_sec == X)) {
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800819 *start = w25q_ranges[i].range.start;
820 *len = w25q_ranges[i].range.len;
821
822 status_found = 1;
823 break;
824 }
825 }
826
827 if (!status_found) {
828 msg_cerr("matching status not found\n");
829 return -1;
830 }
831 return 0;
832}
833
Louis Yung-Chieh Lo52aa9302010-09-06 10:45:02 +0800834/* Since most chips we use must be WREN-ed before WRSR,
835 * we copy a write status function here before we have a good solution. */
836static int spi_write_status_register_WREN(int status)
837{
838 int result;
839 struct spi_command cmds[] = {
840 {
841 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
842 .writecnt = JEDEC_WREN_OUTSIZE,
843 .writearr = (const unsigned char[]){ JEDEC_WREN },
844 .readcnt = 0,
845 .readarr = NULL,
846 }, {
847 .writecnt = JEDEC_WRSR_OUTSIZE,
848 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
849 .readcnt = 0,
850 .readarr = NULL,
851 }, {
852 .writecnt = 0,
853 .writearr = NULL,
854 .readcnt = 0,
855 .readarr = NULL,
856 }};
857
858 result = spi_send_multicommand(cmds);
859 if (result) {
860 msg_cerr("%s failed during command execution\n",
861 __func__);
862 }
Louis Yung-Chieh Lo96222b12010-11-01 11:48:11 +0800863
864 /* WRSR performs a self-timed erase before the changes take effect. */
865 programmer_delay(WRITE_STATUS_REGISTER_DELAY);
866
Louis Yung-Chieh Lo52aa9302010-09-06 10:45:02 +0800867 return result;
868}
869
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800870/* Given a [start, len], this function calls w25_range_to_status() to convert
871 * it to flash-chip-specific range bits, then sets into status register.
872 */
David Hendricks91040832011-07-08 20:01:09 -0700873static int w25_set_range(const struct flashchip *flash,
David Hendricksd494b0a2010-08-16 16:28:50 -0700874 unsigned int start, unsigned int len)
875{
876 struct w25q_status status;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800877 int tmp = 0;
878 int expected = 0;
David Hendricksd494b0a2010-08-16 16:28:50 -0700879
880 memset(&status, 0, sizeof(status));
881 tmp = spi_read_status_register();
882 memcpy(&status, &tmp, 1);
883 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
884
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800885 if (w25_range_to_status(flash, start, len, &status)) return -1;
David Hendricksf7924d12010-06-10 21:26:44 -0700886
887 msg_cdbg("status.busy: %x\n", status.busy);
888 msg_cdbg("status.wel: %x\n", status.wel);
889 msg_cdbg("status.bp0: %x\n", status.bp0);
890 msg_cdbg("status.bp1: %x\n", status.bp1);
891 msg_cdbg("status.bp2: %x\n", status.bp2);
892 msg_cdbg("status.tb: %x\n", status.tb);
893 msg_cdbg("status.sec: %x\n", status.sec);
894 msg_cdbg("status.srp0: %x\n", status.srp0);
895
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800896 memcpy(&expected, &status, sizeof(status));
897 spi_write_status_register_WREN(expected);
David Hendricksf7924d12010-06-10 21:26:44 -0700898
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800899 tmp = spi_read_status_register();
900 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
901 if ((tmp & MASK_WP_AREA) == (expected & MASK_WP_AREA)) {
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800902 return 0;
903 } else {
David Hendricksc801adb2010-12-09 16:58:56 -0800904 msg_cerr("expected=0x%02x, but actual=0x%02x.\n",
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800905 expected, tmp);
906 return 1;
907 }
David Hendricksf7924d12010-06-10 21:26:44 -0700908}
909
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800910/* Print out the current status register value with human-readable text. */
David Hendricks91040832011-07-08 20:01:09 -0700911static int w25_wp_status(const struct flashchip *flash)
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800912{
913 struct w25q_status status;
914 int tmp;
David Hendricksce8ded32010-10-08 11:23:38 -0700915 unsigned int start, len;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800916 int ret = 0;
917
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800918 memset(&status, 0, sizeof(status));
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800919 tmp = spi_read_status_register();
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800920 memcpy(&status, &tmp, 1);
921 msg_cinfo("WP: status: 0x%02x\n", tmp);
922 msg_cinfo("WP: status.srp0: %x\n", status.srp0);
923 msg_cinfo("WP: write protect is %s.\n",
924 status.srp0 ? "enabled" : "disabled");
925
926 msg_cinfo("WP: write protect range: ");
927 if (w25_status_to_range(flash, &status, &start, &len)) {
928 msg_cinfo("(cannot resolve the range)\n");
929 ret = -1;
930 } else {
931 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
932 }
933
934 return ret;
935}
936
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800937/* Set/clear the SRP0 bit in the status register. */
David Hendricks91040832011-07-08 20:01:09 -0700938static int w25_set_srp0(const struct flashchip *flash, int enable)
David Hendricksf7924d12010-06-10 21:26:44 -0700939{
940 struct w25q_status status;
941 int tmp = 0;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800942 int expected = 0;
David Hendricksf7924d12010-06-10 21:26:44 -0700943
944 memset(&status, 0, sizeof(status));
945 tmp = spi_read_status_register();
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800946 /* FIXME: this is NOT endian-free copy. */
David Hendricksf7924d12010-06-10 21:26:44 -0700947 memcpy(&status, &tmp, 1);
948 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
949
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800950 status.srp0 = enable ? 1 : 0;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800951 memcpy(&expected, &status, sizeof(status));
952 spi_write_status_register_WREN(expected);
953
954 tmp = spi_read_status_register();
955 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
956 if ((tmp & MASK_WP_AREA) != (expected & MASK_WP_AREA))
957 return 1;
David Hendricksf7924d12010-06-10 21:26:44 -0700958
959 return 0;
960}
961
David Hendricks1c09f802012-10-03 11:03:48 -0700962static int w25_enable_writeprotect(const struct flashchip *flash,
963 enum wp_mode wp_mode)
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800964{
965 int ret;
966
David Hendricks1c09f802012-10-03 11:03:48 -0700967 switch (wp_mode) {
968 case WP_MODE_HARDWARE:
969 ret = w25_set_srp0(flash, 1);
970 break;
971 default:
972 msg_cerr("%s(): unsupported write-protect mode\n", __func__);
973 return 1;
974 }
975
David Hendricksc801adb2010-12-09 16:58:56 -0800976 if (ret)
977 msg_cerr("%s(): error=%d.\n", __func__, ret);
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800978 return ret;
979}
980
David Hendricks91040832011-07-08 20:01:09 -0700981static int w25_disable_writeprotect(const struct flashchip *flash)
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800982{
983 int ret;
984
985 ret = w25_set_srp0(flash, 0);
David Hendricksc801adb2010-12-09 16:58:56 -0800986 if (ret)
987 msg_cerr("%s(): error=%d.\n", __func__, ret);
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800988 return ret;
989}
990
David Hendricks91040832011-07-08 20:01:09 -0700991static int w25_list_ranges(const struct flashchip *flash)
David Hendricks0f7f5382011-02-11 18:12:31 -0800992{
993 struct w25q_range *w25q_ranges;
994 int i, num_entries;
995
996 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
997 for (i = 0; i < num_entries; i++) {
998 msg_cinfo("start: 0x%06x, length: 0x%06x\n",
999 w25q_ranges[i].range.start,
1000 w25q_ranges[i].range.len);
1001 }
1002
1003 return 0;
1004}
1005
David Hendricks1c09f802012-10-03 11:03:48 -07001006/* FIXME: Move to spi25.c if it's a JEDEC standard opcode */
1007uint8_t w25q_read_status_register_2(void)
1008{
1009 static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { 0x35 };
1010 unsigned char readarr[2];
1011 int ret;
1012
1013 /* Read Status Register */
1014 ret = spi_send_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
1015 if (ret) {
1016 /*
1017 * FIXME: make this a benign failure for now in case we are
1018 * unable to execute the opcode
1019 */
1020 msg_cdbg("RDSR2 failed!\n");
1021 readarr[0] = 0x00;
1022 }
1023
1024 return readarr[0];
1025}
1026
1027static int w25q_wp_status(const struct flashchip *flash)
1028{
1029 struct w25q_status sr1;
1030 struct w25q_status_2 sr2;
David Hendricksf1bd8802012-10-30 11:37:57 -07001031 uint8_t tmp[2];
David Hendricks1c09f802012-10-03 11:03:48 -07001032 unsigned int start, len;
1033 int ret = 0;
1034
1035 memset(&sr1, 0, sizeof(sr1));
David Hendricksf1bd8802012-10-30 11:37:57 -07001036 tmp[0] = spi_read_status_register();
1037 memcpy(&sr1, &tmp[0], 1);
David Hendricks1c09f802012-10-03 11:03:48 -07001038
David Hendricksf1bd8802012-10-30 11:37:57 -07001039 memset(&sr2, 0, sizeof(sr2));
1040 tmp[1] = w25q_read_status_register_2();
1041 memcpy(&sr2, &tmp[1], 1);
1042
1043 msg_cinfo("WP: status: 0x%02x%02x\n", tmp[1], tmp[0]);
David Hendricks1c09f802012-10-03 11:03:48 -07001044 msg_cinfo("WP: status.srp0: %x\n", sr1.srp0);
1045 msg_cinfo("WP: status.srp1: %x\n", sr2.srp1);
1046 msg_cinfo("WP: write protect is %s.\n",
1047 (sr1.srp0 || sr2.srp1) ? "enabled" : "disabled");
1048
1049 msg_cinfo("WP: write protect range: ");
1050 if (w25_status_to_range(flash, &sr1, &start, &len)) {
1051 msg_cinfo("(cannot resolve the range)\n");
1052 ret = -1;
1053 } else {
1054 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
1055 }
1056
1057 return ret;
1058}
1059
1060/*
1061 * W25Q adds an optional byte to the standard WRSR opcode. If /CS is
1062 * de-asserted after the first byte, then it acts like a JEDEC-standard
1063 * WRSR command. if /CS is asserted, then the next data byte is written
1064 * into status register 2.
1065 */
1066#define W25Q_WRSR_OUTSIZE 0x03
1067static int w25q_write_status_register_WREN(uint8_t s1, uint8_t s2)
1068{
1069 int result;
1070 struct spi_command cmds[] = {
1071 {
1072 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
1073 .writecnt = JEDEC_WREN_OUTSIZE,
1074 .writearr = (const unsigned char[]){ JEDEC_WREN },
1075 .readcnt = 0,
1076 .readarr = NULL,
1077 }, {
1078 .writecnt = W25Q_WRSR_OUTSIZE,
1079 .writearr = (const unsigned char[]){ JEDEC_WRSR, s1, s2 },
1080 .readcnt = 0,
1081 .readarr = NULL,
1082 }, {
1083 .writecnt = 0,
1084 .writearr = NULL,
1085 .readcnt = 0,
1086 .readarr = NULL,
1087 }};
1088
1089 result = spi_send_multicommand(cmds);
1090 if (result) {
1091 msg_cerr("%s failed during command execution\n",
1092 __func__);
1093 }
1094
1095 /* WRSR performs a self-timed erase before the changes take effect. */
1096 programmer_delay(WRITE_STATUS_REGISTER_DELAY);
1097
1098 return result;
1099}
1100
1101/*
1102 * Set/clear the SRP1 bit in status register 2.
1103 * FIXME: make this more generic if other chips use the same SR2 layout
1104 */
1105static int w25q_set_srp1(const struct flashchip *flash, int enable)
1106{
1107 struct w25q_status sr1;
1108 struct w25q_status_2 sr2;
1109 uint8_t tmp, expected;
1110
1111 tmp = spi_read_status_register();
1112 memcpy(&sr1, &tmp, 1);
1113 tmp = w25q_read_status_register_2();
1114 memcpy(&sr2, &tmp, 1);
1115
1116 msg_cdbg("%s: old status 2: 0x%02x\n", __func__, tmp);
1117
1118 sr2.srp1 = enable ? 1 : 0;
1119
1120 memcpy(&expected, &sr2, 1);
1121 w25q_write_status_register_WREN(*((uint8_t *)&sr1), *((uint8_t *)&sr2));
1122
1123 tmp = w25q_read_status_register_2();
1124 msg_cdbg("%s: new status 2: 0x%02x\n", __func__, tmp);
1125 if ((tmp & MASK_WP2_AREA) != (expected & MASK_WP2_AREA))
1126 return 1;
1127
1128 return 0;
1129}
1130
1131enum wp_mode get_wp_mode(const char *mode_str)
1132{
1133 enum wp_mode wp_mode = WP_MODE_UNKNOWN;
1134
1135 if (!strcasecmp(mode_str, "hardware"))
1136 wp_mode = WP_MODE_HARDWARE;
1137 else if (!strcasecmp(mode_str, "power_cycle"))
1138 wp_mode = WP_MODE_POWER_CYCLE;
1139 else if (!strcasecmp(mode_str, "permanent"))
1140 wp_mode = WP_MODE_PERMANENT;
1141
1142 return wp_mode;
1143}
1144
1145static int w25q_disable_writeprotect(const struct flashchip *flash,
1146 enum wp_mode wp_mode)
1147{
1148 int ret = 1;
David Hendricks1c09f802012-10-03 11:03:48 -07001149 struct w25q_status_2 sr2;
1150 uint8_t tmp;
1151
1152 switch (wp_mode) {
1153 case WP_MODE_HARDWARE:
1154 ret = w25_set_srp0(flash, 0);
1155 break;
1156 case WP_MODE_POWER_CYCLE:
1157 tmp = w25q_read_status_register_2();
1158 memcpy(&sr2, &tmp, 1);
1159 if (sr2.srp1) {
1160 msg_cerr("%s(): must disconnect power to disable "
1161 "write-protection\n", __func__);
1162 } else {
1163 ret = 0;
1164 }
1165 break;
1166 case WP_MODE_PERMANENT:
1167 msg_cerr("%s(): cannot disable permanent write-protection\n",
1168 __func__);
1169 break;
1170 default:
1171 msg_cerr("%s(): invalid mode specified\n", __func__);
1172 break;
1173 }
1174
1175 if (ret)
1176 msg_cerr("%s(): error=%d.\n", __func__, ret);
1177 return ret;
1178}
1179
1180static int w25q_disable_writeprotect_default(const struct flashchip *flash)
1181{
1182 return w25q_disable_writeprotect(flash, WP_MODE_HARDWARE);
1183}
1184
1185static int w25q_enable_writeprotect(const struct flashchip *flash,
1186 enum wp_mode wp_mode)
1187{
1188 int ret = 1;
1189 struct w25q_status sr1;
1190 struct w25q_status_2 sr2;
1191 uint8_t tmp;
1192
1193 switch (wp_mode) {
1194 case WP_MODE_HARDWARE:
1195 if (w25q_disable_writeprotect(flash, WP_MODE_POWER_CYCLE)) {
1196 msg_cerr("%s(): cannot disable power cycle WP mode\n",
1197 __func__);
1198 break;
1199 }
1200
1201 tmp = spi_read_status_register();
1202 memcpy(&sr1, &tmp, 1);
1203 if (sr1.srp0)
1204 ret = 0;
1205 else
1206 ret = w25_set_srp0(flash, 1);
1207
1208 break;
1209 case WP_MODE_POWER_CYCLE:
1210 if (w25q_disable_writeprotect(flash, WP_MODE_HARDWARE)) {
1211 msg_cerr("%s(): cannot disable hardware WP mode\n",
1212 __func__);
1213 break;
1214 }
1215
1216 tmp = w25q_read_status_register_2();
1217 memcpy(&sr2, &tmp, 1);
1218 if (sr2.srp1)
1219 ret = 0;
1220 else
1221 ret = w25q_set_srp1(flash, 1);
1222
1223 break;
1224 case WP_MODE_PERMANENT:
1225 tmp = spi_read_status_register();
1226 memcpy(&sr1, &tmp, 1);
1227 if (sr1.srp0 == 0) {
1228 ret = w25_set_srp0(flash, 1);
1229 if (ret) {
David Hendricksf1bd8802012-10-30 11:37:57 -07001230 msg_perr("%s(): cannot enable SRP0 for "
David Hendricks1c09f802012-10-03 11:03:48 -07001231 "permanent WP\n", __func__);
1232 break;
1233 }
1234 }
1235
1236 tmp = w25q_read_status_register_2();
1237 memcpy(&sr2, &tmp, 1);
1238 if (sr2.srp1 == 0) {
1239 ret = w25q_set_srp1(flash, 1);
1240 if (ret) {
David Hendricksf1bd8802012-10-30 11:37:57 -07001241 msg_perr("%s(): cannot enable SRP1 for "
David Hendricks1c09f802012-10-03 11:03:48 -07001242 "permanent WP\n", __func__);
1243 break;
1244 }
1245 }
1246
1247 break;
David Hendricksf1bd8802012-10-30 11:37:57 -07001248 default:
1249 msg_perr("%s(): invalid mode %d\n", __func__, wp_mode);
1250 break;
David Hendricks1c09f802012-10-03 11:03:48 -07001251 }
1252
1253 if (ret)
1254 msg_cerr("%s(): error=%d.\n", __func__, ret);
1255 return ret;
1256}
1257
1258/* W25P, W25X, and many flash chips from various vendors */
David Hendricksf7924d12010-06-10 21:26:44 -07001259struct wp wp_w25 = {
David Hendricks0f7f5382011-02-11 18:12:31 -08001260 .list_ranges = w25_list_ranges,
David Hendricksf7924d12010-06-10 21:26:44 -07001261 .set_range = w25_set_range,
1262 .enable = w25_enable_writeprotect,
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001263 .disable = w25_disable_writeprotect,
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001264 .wp_status = w25_wp_status,
David Hendricks1c09f802012-10-03 11:03:48 -07001265
1266};
1267
1268/* W25Q series has features such as a second status register and SFDP */
1269struct wp wp_w25q = {
1270 .list_ranges = w25_list_ranges,
1271 .set_range = w25_set_range,
1272 .enable = w25q_enable_writeprotect,
1273 /*
1274 * By default, disable hardware write-protection. We may change
1275 * this later if we want to add fine-grained write-protect disable
1276 * as a command-line option.
1277 */
1278 .disable = w25q_disable_writeprotect_default,
1279 .wp_status = w25q_wp_status,
David Hendricksf7924d12010-06-10 21:26:44 -07001280};