blob: 418a6319f57da5b2dd6e671cbee1eee53c16ba39 [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:
AdamTsai141a2622013-12-31 14:07:15 +0800620 case WINBOND_NEX_W25Q64DW:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800621 *w25q_ranges = w25q64_ranges;
622 *num_entries = ARRAY_SIZE(w25q64_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700623 break;
624 default:
625 msg_cerr("%s() %d: WINBOND flash chip mismatch (0x%04x)"
626 ", aborting\n", __func__, __LINE__,
627 flash->model_id);
628 return -1;
629 }
David Hendricks2c4a76c2010-06-28 14:00:43 -0700630 break;
David Hendricks57566ed2010-08-16 18:24:45 -0700631 case EON_ID_NOPREFIX:
632 switch (flash->model_id) {
David Hendricksc801adb2010-12-09 16:58:56 -0800633 case EON_EN25F40:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800634 *w25q_ranges = en25f40_ranges;
635 *num_entries = ARRAY_SIZE(en25f40_ranges);
David Hendricks57566ed2010-08-16 18:24:45 -0700636 break;
David Hendrickse185bf22011-05-24 15:34:18 -0700637 case EON_EN25Q40:
638 *w25q_ranges = en25q40_ranges;
639 *num_entries = ARRAY_SIZE(en25q40_ranges);
640 break;
641 case EON_EN25Q80:
642 *w25q_ranges = en25q80_ranges;
643 *num_entries = ARRAY_SIZE(en25q80_ranges);
644 break;
645 case EON_EN25Q32:
646 *w25q_ranges = en25q32_ranges;
647 *num_entries = ARRAY_SIZE(en25q32_ranges);
648 break;
649 case EON_EN25Q64:
650 *w25q_ranges = en25q64_ranges;
651 *num_entries = ARRAY_SIZE(en25q64_ranges);
652 break;
653 case EON_EN25Q128:
654 *w25q_ranges = en25q128_ranges;
655 *num_entries = ARRAY_SIZE(en25q128_ranges);
656 break;
David Hendricks57566ed2010-08-16 18:24:45 -0700657 default:
658 msg_cerr("%s():%d: EON flash chip mismatch (0x%04x)"
659 ", aborting\n", __func__, __LINE__,
660 flash->model_id);
661 return -1;
662 }
663 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800664 case MACRONIX_ID:
David Hendricksac72e362010-08-16 18:20:03 -0700665 switch (flash->model_id) {
David Hendricksf8f00c72011-02-01 12:39:46 -0800666 case MACRONIX_MX25L1005:
667 *w25q_ranges = mx25l1005_ranges;
668 *num_entries = ARRAY_SIZE(mx25l1005_ranges);
669 break;
670 case MACRONIX_MX25L2005:
671 *w25q_ranges = mx25l2005_ranges;
672 *num_entries = ARRAY_SIZE(mx25l2005_ranges);
673 break;
674 case MACRONIX_MX25L4005:
675 *w25q_ranges = mx25l4005_ranges;
676 *num_entries = ARRAY_SIZE(mx25l4005_ranges);
677 break;
678 case MACRONIX_MX25L8005:
679 *w25q_ranges = mx25l8005_ranges;
680 *num_entries = ARRAY_SIZE(mx25l8005_ranges);
681 break;
682 case MACRONIX_MX25L1605:
683 /* FIXME: MX25L1605 and MX25L1605D have different write
684 * protection capabilities, but share IDs */
685 *w25q_ranges = mx25l1605d_ranges;
686 *num_entries = ARRAY_SIZE(mx25l1605d_ranges);
687 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800688 case MACRONIX_MX25L3205:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800689 *w25q_ranges = mx25l3205d_ranges;
690 *num_entries = ARRAY_SIZE(mx25l3205d_ranges);
David Hendricksac72e362010-08-16 18:20:03 -0700691 break;
Vincent Palatin87e092a2013-02-28 15:46:14 -0800692 case MACRONIX_MX25U3235E:
693 *w25q_ranges = mx25u3235e_ranges;
694 *num_entries = ARRAY_SIZE(mx25u3235e_ranges);
695 break;
David Hendricksf8f00c72011-02-01 12:39:46 -0800696 case MACRONIX_MX25L6405:
David Hendricks1c9bc9c2011-07-20 15:25:44 -0700697 /* FIXME: MX25L64* chips have mixed capabilities and
698 share IDs */
699 *w25q_ranges = mx25l6406e_ranges;
700 *num_entries = ARRAY_SIZE(mx25l6406e_ranges);
David Hendricksf8f00c72011-02-01 12:39:46 -0800701 break;
David Hendricksac72e362010-08-16 18:20:03 -0700702 default:
703 msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)"
704 ", aborting\n", __func__, __LINE__,
705 flash->model_id);
706 return -1;
707 }
708 break;
David Hendricksbfa624b2012-07-24 12:47:59 -0700709 case ST_ID:
710 switch(flash->model_id) {
711 case ST_N25Q064__1E:
712 case ST_N25Q064__3E:
713 *w25q_ranges = n25q064_ranges;
714 *num_entries = ARRAY_SIZE(n25q064_ranges);
715 break;
716 default:
717 msg_cerr("%s() %d: Micron flash chip mismatch"
718 " (0x%04x), aborting\n", __func__, __LINE__,
719 flash->model_id);
720 return -1;
721 }
722 break;
Bryan Freed9a0051f2012-05-22 16:06:09 -0700723 case GIGADEVICE_ID:
724 switch(flash->model_id) {
725 case GIGADEVICE_GD25LQ32:
726 *w25q_ranges = w25q32_ranges;
727 *num_entries = ARRAY_SIZE(w25q32_ranges);
728 break;
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700729 case GIGADEVICE_GD25Q64:
730 *w25q_ranges = gd25q64_ranges;
731 *num_entries = ARRAY_SIZE(gd25q64_ranges);
732 break;
733 /* TODO(shawnn): add support for other GD parts */
Bryan Freed9a0051f2012-05-22 16:06:09 -0700734 default:
735 msg_cerr("%s() %d: GigaDevice flash chip mismatch"
736 " (0x%04x), aborting\n", __func__, __LINE__,
737 flash->model_id);
738 return -1;
739 }
740 break;
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +0800741 case AMIC_ID_NOPREFIX:
742 switch(flash->model_id) {
743 case AMIC_A25L040:
744 *w25q_ranges = a25l040_ranges;
745 *num_entries = ARRAY_SIZE(a25l040_ranges);
746 break;
747 default:
748 msg_cerr("%s() %d: AMIC flash chip mismatch"
749 " (0x%04x), aborting\n", __func__, __LINE__,
750 flash->model_id);
751 return -1;
752 }
753 break;
David Hendricksf7924d12010-06-10 21:26:44 -0700754 default:
David Hendricksd494b0a2010-08-16 16:28:50 -0700755 msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
756 __func__, flash->manufacture_id);
David Hendricksf7924d12010-06-10 21:26:44 -0700757 return -1;
758 }
759
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800760 return 0;
761}
762
763int w25_range_to_status(const struct flashchip *flash,
764 unsigned int start, unsigned int len,
765 struct w25q_status *status)
766{
767 struct w25q_range *w25q_ranges;
768 int i, range_found = 0;
769 int num_entries;
770
771 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
David Hendricksf7924d12010-06-10 21:26:44 -0700772 for (i = 0; i < num_entries; i++) {
773 struct wp_range *r = &w25q_ranges[i].range;
774
775 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
776 start, len, r->start, r->len);
777 if ((start == r->start) && (len == r->len)) {
David Hendricksd494b0a2010-08-16 16:28:50 -0700778 status->bp0 = w25q_ranges[i].bp & 1;
779 status->bp1 = w25q_ranges[i].bp >> 1;
780 status->bp2 = w25q_ranges[i].bp >> 2;
781 status->tb = w25q_ranges[i].tb;
782 status->sec = w25q_ranges[i].sec;
David Hendricksf7924d12010-06-10 21:26:44 -0700783
784 range_found = 1;
785 break;
786 }
787 }
788
789 if (!range_found) {
790 msg_cerr("matching range not found\n");
791 return -1;
792 }
David Hendricksd494b0a2010-08-16 16:28:50 -0700793 return 0;
794}
795
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800796int w25_status_to_range(const struct flashchip *flash,
797 const struct w25q_status *status,
798 unsigned int *start, unsigned int *len)
799{
800 struct w25q_range *w25q_ranges;
801 int i, status_found = 0;
802 int num_entries;
803
804 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
805 for (i = 0; i < num_entries; i++) {
806 int bp;
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +0800807 int table_bp, table_tb, table_sec;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800808
809 bp = status->bp0 | (status->bp1 << 1) | (status->bp2 << 2);
810 msg_cspew("comparing 0x%x 0x%x / 0x%x 0x%x / 0x%x 0x%x\n",
811 bp, w25q_ranges[i].bp,
812 status->tb, w25q_ranges[i].tb,
813 status->sec, w25q_ranges[i].sec);
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +0800814 table_bp = w25q_ranges[i].bp;
815 table_tb = w25q_ranges[i].tb;
816 table_sec = w25q_ranges[i].sec;
817 if ((bp == table_bp || table_bp == X) &&
818 (status->tb == table_tb || table_tb == X) &&
819 (status->sec == table_sec || table_sec == X)) {
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800820 *start = w25q_ranges[i].range.start;
821 *len = w25q_ranges[i].range.len;
822
823 status_found = 1;
824 break;
825 }
826 }
827
828 if (!status_found) {
829 msg_cerr("matching status not found\n");
830 return -1;
831 }
832 return 0;
833}
834
Louis Yung-Chieh Lo52aa9302010-09-06 10:45:02 +0800835/* Since most chips we use must be WREN-ed before WRSR,
836 * we copy a write status function here before we have a good solution. */
837static int spi_write_status_register_WREN(int status)
838{
839 int result;
840 struct spi_command cmds[] = {
841 {
842 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
843 .writecnt = JEDEC_WREN_OUTSIZE,
844 .writearr = (const unsigned char[]){ JEDEC_WREN },
845 .readcnt = 0,
846 .readarr = NULL,
847 }, {
848 .writecnt = JEDEC_WRSR_OUTSIZE,
849 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
850 .readcnt = 0,
851 .readarr = NULL,
852 }, {
853 .writecnt = 0,
854 .writearr = NULL,
855 .readcnt = 0,
856 .readarr = NULL,
857 }};
858
859 result = spi_send_multicommand(cmds);
860 if (result) {
861 msg_cerr("%s failed during command execution\n",
862 __func__);
863 }
Louis Yung-Chieh Lo96222b12010-11-01 11:48:11 +0800864
865 /* WRSR performs a self-timed erase before the changes take effect. */
866 programmer_delay(WRITE_STATUS_REGISTER_DELAY);
867
Louis Yung-Chieh Lo52aa9302010-09-06 10:45:02 +0800868 return result;
869}
870
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800871/* Given a [start, len], this function calls w25_range_to_status() to convert
872 * it to flash-chip-specific range bits, then sets into status register.
873 */
David Hendricks91040832011-07-08 20:01:09 -0700874static int w25_set_range(const struct flashchip *flash,
David Hendricksd494b0a2010-08-16 16:28:50 -0700875 unsigned int start, unsigned int len)
876{
877 struct w25q_status status;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800878 int tmp = 0;
879 int expected = 0;
David Hendricksd494b0a2010-08-16 16:28:50 -0700880
881 memset(&status, 0, sizeof(status));
882 tmp = spi_read_status_register();
883 memcpy(&status, &tmp, 1);
884 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
885
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800886 if (w25_range_to_status(flash, start, len, &status)) return -1;
David Hendricksf7924d12010-06-10 21:26:44 -0700887
888 msg_cdbg("status.busy: %x\n", status.busy);
889 msg_cdbg("status.wel: %x\n", status.wel);
890 msg_cdbg("status.bp0: %x\n", status.bp0);
891 msg_cdbg("status.bp1: %x\n", status.bp1);
892 msg_cdbg("status.bp2: %x\n", status.bp2);
893 msg_cdbg("status.tb: %x\n", status.tb);
894 msg_cdbg("status.sec: %x\n", status.sec);
895 msg_cdbg("status.srp0: %x\n", status.srp0);
896
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800897 memcpy(&expected, &status, sizeof(status));
898 spi_write_status_register_WREN(expected);
David Hendricksf7924d12010-06-10 21:26:44 -0700899
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800900 tmp = spi_read_status_register();
901 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
902 if ((tmp & MASK_WP_AREA) == (expected & MASK_WP_AREA)) {
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800903 return 0;
904 } else {
David Hendricksc801adb2010-12-09 16:58:56 -0800905 msg_cerr("expected=0x%02x, but actual=0x%02x.\n",
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800906 expected, tmp);
907 return 1;
908 }
David Hendricksf7924d12010-06-10 21:26:44 -0700909}
910
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800911/* Print out the current status register value with human-readable text. */
David Hendricks91040832011-07-08 20:01:09 -0700912static int w25_wp_status(const struct flashchip *flash)
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800913{
914 struct w25q_status status;
915 int tmp;
David Hendricksce8ded32010-10-08 11:23:38 -0700916 unsigned int start, len;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800917 int ret = 0;
918
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800919 memset(&status, 0, sizeof(status));
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800920 tmp = spi_read_status_register();
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800921 memcpy(&status, &tmp, 1);
922 msg_cinfo("WP: status: 0x%02x\n", tmp);
923 msg_cinfo("WP: status.srp0: %x\n", status.srp0);
924 msg_cinfo("WP: write protect is %s.\n",
925 status.srp0 ? "enabled" : "disabled");
926
927 msg_cinfo("WP: write protect range: ");
928 if (w25_status_to_range(flash, &status, &start, &len)) {
929 msg_cinfo("(cannot resolve the range)\n");
930 ret = -1;
931 } else {
932 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
933 }
934
935 return ret;
936}
937
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800938/* Set/clear the SRP0 bit in the status register. */
David Hendricks91040832011-07-08 20:01:09 -0700939static int w25_set_srp0(const struct flashchip *flash, int enable)
David Hendricksf7924d12010-06-10 21:26:44 -0700940{
941 struct w25q_status status;
942 int tmp = 0;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800943 int expected = 0;
David Hendricksf7924d12010-06-10 21:26:44 -0700944
945 memset(&status, 0, sizeof(status));
946 tmp = spi_read_status_register();
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800947 /* FIXME: this is NOT endian-free copy. */
David Hendricksf7924d12010-06-10 21:26:44 -0700948 memcpy(&status, &tmp, 1);
949 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
950
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800951 status.srp0 = enable ? 1 : 0;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800952 memcpy(&expected, &status, sizeof(status));
953 spi_write_status_register_WREN(expected);
954
955 tmp = spi_read_status_register();
956 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
957 if ((tmp & MASK_WP_AREA) != (expected & MASK_WP_AREA))
958 return 1;
David Hendricksf7924d12010-06-10 21:26:44 -0700959
960 return 0;
961}
962
David Hendricks1c09f802012-10-03 11:03:48 -0700963static int w25_enable_writeprotect(const struct flashchip *flash,
964 enum wp_mode wp_mode)
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800965{
966 int ret;
967
David Hendricks1c09f802012-10-03 11:03:48 -0700968 switch (wp_mode) {
969 case WP_MODE_HARDWARE:
970 ret = w25_set_srp0(flash, 1);
971 break;
972 default:
973 msg_cerr("%s(): unsupported write-protect mode\n", __func__);
974 return 1;
975 }
976
David Hendricksc801adb2010-12-09 16:58:56 -0800977 if (ret)
978 msg_cerr("%s(): error=%d.\n", __func__, ret);
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800979 return ret;
980}
981
David Hendricks91040832011-07-08 20:01:09 -0700982static int w25_disable_writeprotect(const struct flashchip *flash)
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800983{
984 int ret;
985
986 ret = w25_set_srp0(flash, 0);
David Hendricksc801adb2010-12-09 16:58:56 -0800987 if (ret)
988 msg_cerr("%s(): error=%d.\n", __func__, ret);
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800989 return ret;
990}
991
David Hendricks91040832011-07-08 20:01:09 -0700992static int w25_list_ranges(const struct flashchip *flash)
David Hendricks0f7f5382011-02-11 18:12:31 -0800993{
994 struct w25q_range *w25q_ranges;
995 int i, num_entries;
996
997 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
998 for (i = 0; i < num_entries; i++) {
999 msg_cinfo("start: 0x%06x, length: 0x%06x\n",
1000 w25q_ranges[i].range.start,
1001 w25q_ranges[i].range.len);
1002 }
1003
1004 return 0;
1005}
1006
David Hendricks1c09f802012-10-03 11:03:48 -07001007/* FIXME: Move to spi25.c if it's a JEDEC standard opcode */
1008uint8_t w25q_read_status_register_2(void)
1009{
1010 static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { 0x35 };
1011 unsigned char readarr[2];
1012 int ret;
1013
1014 /* Read Status Register */
1015 ret = spi_send_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
1016 if (ret) {
1017 /*
1018 * FIXME: make this a benign failure for now in case we are
1019 * unable to execute the opcode
1020 */
1021 msg_cdbg("RDSR2 failed!\n");
1022 readarr[0] = 0x00;
1023 }
1024
1025 return readarr[0];
1026}
1027
1028static int w25q_wp_status(const struct flashchip *flash)
1029{
1030 struct w25q_status sr1;
1031 struct w25q_status_2 sr2;
David Hendricksf1bd8802012-10-30 11:37:57 -07001032 uint8_t tmp[2];
David Hendricks1c09f802012-10-03 11:03:48 -07001033 unsigned int start, len;
1034 int ret = 0;
1035
1036 memset(&sr1, 0, sizeof(sr1));
David Hendricksf1bd8802012-10-30 11:37:57 -07001037 tmp[0] = spi_read_status_register();
1038 memcpy(&sr1, &tmp[0], 1);
David Hendricks1c09f802012-10-03 11:03:48 -07001039
David Hendricksf1bd8802012-10-30 11:37:57 -07001040 memset(&sr2, 0, sizeof(sr2));
1041 tmp[1] = w25q_read_status_register_2();
1042 memcpy(&sr2, &tmp[1], 1);
1043
1044 msg_cinfo("WP: status: 0x%02x%02x\n", tmp[1], tmp[0]);
David Hendricks1c09f802012-10-03 11:03:48 -07001045 msg_cinfo("WP: status.srp0: %x\n", sr1.srp0);
1046 msg_cinfo("WP: status.srp1: %x\n", sr2.srp1);
1047 msg_cinfo("WP: write protect is %s.\n",
1048 (sr1.srp0 || sr2.srp1) ? "enabled" : "disabled");
1049
1050 msg_cinfo("WP: write protect range: ");
1051 if (w25_status_to_range(flash, &sr1, &start, &len)) {
1052 msg_cinfo("(cannot resolve the range)\n");
1053 ret = -1;
1054 } else {
1055 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
1056 }
1057
1058 return ret;
1059}
1060
1061/*
1062 * W25Q adds an optional byte to the standard WRSR opcode. If /CS is
1063 * de-asserted after the first byte, then it acts like a JEDEC-standard
1064 * WRSR command. if /CS is asserted, then the next data byte is written
1065 * into status register 2.
1066 */
1067#define W25Q_WRSR_OUTSIZE 0x03
1068static int w25q_write_status_register_WREN(uint8_t s1, uint8_t s2)
1069{
1070 int result;
1071 struct spi_command cmds[] = {
1072 {
1073 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
1074 .writecnt = JEDEC_WREN_OUTSIZE,
1075 .writearr = (const unsigned char[]){ JEDEC_WREN },
1076 .readcnt = 0,
1077 .readarr = NULL,
1078 }, {
1079 .writecnt = W25Q_WRSR_OUTSIZE,
1080 .writearr = (const unsigned char[]){ JEDEC_WRSR, s1, s2 },
1081 .readcnt = 0,
1082 .readarr = NULL,
1083 }, {
1084 .writecnt = 0,
1085 .writearr = NULL,
1086 .readcnt = 0,
1087 .readarr = NULL,
1088 }};
1089
1090 result = spi_send_multicommand(cmds);
1091 if (result) {
1092 msg_cerr("%s failed during command execution\n",
1093 __func__);
1094 }
1095
1096 /* WRSR performs a self-timed erase before the changes take effect. */
1097 programmer_delay(WRITE_STATUS_REGISTER_DELAY);
1098
1099 return result;
1100}
1101
1102/*
1103 * Set/clear the SRP1 bit in status register 2.
1104 * FIXME: make this more generic if other chips use the same SR2 layout
1105 */
1106static int w25q_set_srp1(const struct flashchip *flash, int enable)
1107{
1108 struct w25q_status sr1;
1109 struct w25q_status_2 sr2;
1110 uint8_t tmp, expected;
1111
1112 tmp = spi_read_status_register();
1113 memcpy(&sr1, &tmp, 1);
1114 tmp = w25q_read_status_register_2();
1115 memcpy(&sr2, &tmp, 1);
1116
1117 msg_cdbg("%s: old status 2: 0x%02x\n", __func__, tmp);
1118
1119 sr2.srp1 = enable ? 1 : 0;
1120
1121 memcpy(&expected, &sr2, 1);
1122 w25q_write_status_register_WREN(*((uint8_t *)&sr1), *((uint8_t *)&sr2));
1123
1124 tmp = w25q_read_status_register_2();
1125 msg_cdbg("%s: new status 2: 0x%02x\n", __func__, tmp);
1126 if ((tmp & MASK_WP2_AREA) != (expected & MASK_WP2_AREA))
1127 return 1;
1128
1129 return 0;
1130}
1131
1132enum wp_mode get_wp_mode(const char *mode_str)
1133{
1134 enum wp_mode wp_mode = WP_MODE_UNKNOWN;
1135
1136 if (!strcasecmp(mode_str, "hardware"))
1137 wp_mode = WP_MODE_HARDWARE;
1138 else if (!strcasecmp(mode_str, "power_cycle"))
1139 wp_mode = WP_MODE_POWER_CYCLE;
1140 else if (!strcasecmp(mode_str, "permanent"))
1141 wp_mode = WP_MODE_PERMANENT;
1142
1143 return wp_mode;
1144}
1145
1146static int w25q_disable_writeprotect(const struct flashchip *flash,
1147 enum wp_mode wp_mode)
1148{
1149 int ret = 1;
David Hendricks1c09f802012-10-03 11:03:48 -07001150 struct w25q_status_2 sr2;
1151 uint8_t tmp;
1152
1153 switch (wp_mode) {
1154 case WP_MODE_HARDWARE:
1155 ret = w25_set_srp0(flash, 0);
1156 break;
1157 case WP_MODE_POWER_CYCLE:
1158 tmp = w25q_read_status_register_2();
1159 memcpy(&sr2, &tmp, 1);
1160 if (sr2.srp1) {
1161 msg_cerr("%s(): must disconnect power to disable "
1162 "write-protection\n", __func__);
1163 } else {
1164 ret = 0;
1165 }
1166 break;
1167 case WP_MODE_PERMANENT:
1168 msg_cerr("%s(): cannot disable permanent write-protection\n",
1169 __func__);
1170 break;
1171 default:
1172 msg_cerr("%s(): invalid mode specified\n", __func__);
1173 break;
1174 }
1175
1176 if (ret)
1177 msg_cerr("%s(): error=%d.\n", __func__, ret);
1178 return ret;
1179}
1180
1181static int w25q_disable_writeprotect_default(const struct flashchip *flash)
1182{
1183 return w25q_disable_writeprotect(flash, WP_MODE_HARDWARE);
1184}
1185
1186static int w25q_enable_writeprotect(const struct flashchip *flash,
1187 enum wp_mode wp_mode)
1188{
1189 int ret = 1;
1190 struct w25q_status sr1;
1191 struct w25q_status_2 sr2;
1192 uint8_t tmp;
1193
1194 switch (wp_mode) {
1195 case WP_MODE_HARDWARE:
1196 if (w25q_disable_writeprotect(flash, WP_MODE_POWER_CYCLE)) {
1197 msg_cerr("%s(): cannot disable power cycle WP mode\n",
1198 __func__);
1199 break;
1200 }
1201
1202 tmp = spi_read_status_register();
1203 memcpy(&sr1, &tmp, 1);
1204 if (sr1.srp0)
1205 ret = 0;
1206 else
1207 ret = w25_set_srp0(flash, 1);
1208
1209 break;
1210 case WP_MODE_POWER_CYCLE:
1211 if (w25q_disable_writeprotect(flash, WP_MODE_HARDWARE)) {
1212 msg_cerr("%s(): cannot disable hardware WP mode\n",
1213 __func__);
1214 break;
1215 }
1216
1217 tmp = w25q_read_status_register_2();
1218 memcpy(&sr2, &tmp, 1);
1219 if (sr2.srp1)
1220 ret = 0;
1221 else
1222 ret = w25q_set_srp1(flash, 1);
1223
1224 break;
1225 case WP_MODE_PERMANENT:
1226 tmp = spi_read_status_register();
1227 memcpy(&sr1, &tmp, 1);
1228 if (sr1.srp0 == 0) {
1229 ret = w25_set_srp0(flash, 1);
1230 if (ret) {
David Hendricksf1bd8802012-10-30 11:37:57 -07001231 msg_perr("%s(): cannot enable SRP0 for "
David Hendricks1c09f802012-10-03 11:03:48 -07001232 "permanent WP\n", __func__);
1233 break;
1234 }
1235 }
1236
1237 tmp = w25q_read_status_register_2();
1238 memcpy(&sr2, &tmp, 1);
1239 if (sr2.srp1 == 0) {
1240 ret = w25q_set_srp1(flash, 1);
1241 if (ret) {
David Hendricksf1bd8802012-10-30 11:37:57 -07001242 msg_perr("%s(): cannot enable SRP1 for "
David Hendricks1c09f802012-10-03 11:03:48 -07001243 "permanent WP\n", __func__);
1244 break;
1245 }
1246 }
1247
1248 break;
David Hendricksf1bd8802012-10-30 11:37:57 -07001249 default:
1250 msg_perr("%s(): invalid mode %d\n", __func__, wp_mode);
1251 break;
David Hendricks1c09f802012-10-03 11:03:48 -07001252 }
1253
1254 if (ret)
1255 msg_cerr("%s(): error=%d.\n", __func__, ret);
1256 return ret;
1257}
1258
1259/* W25P, W25X, and many flash chips from various vendors */
David Hendricksf7924d12010-06-10 21:26:44 -07001260struct wp wp_w25 = {
David Hendricks0f7f5382011-02-11 18:12:31 -08001261 .list_ranges = w25_list_ranges,
David Hendricksf7924d12010-06-10 21:26:44 -07001262 .set_range = w25_set_range,
1263 .enable = w25_enable_writeprotect,
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001264 .disable = w25_disable_writeprotect,
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001265 .wp_status = w25_wp_status,
David Hendricks1c09f802012-10-03 11:03:48 -07001266
1267};
1268
1269/* W25Q series has features such as a second status register and SFDP */
1270struct wp wp_w25q = {
1271 .list_ranges = w25_list_ranges,
1272 .set_range = w25_set_range,
1273 .enable = w25q_enable_writeprotect,
1274 /*
1275 * By default, disable hardware write-protection. We may change
1276 * this later if we want to add fine-grained write-protect disable
1277 * as a command-line option.
1278 */
1279 .disable = w25q_disable_writeprotect_default,
1280 .wp_status = w25q_wp_status,
David Hendricksf7924d12010-06-10 21:26:44 -07001281};