blob: 49ff628b9b28e8d9e247debd838ab1689676177b [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
David Hendricks1c9bc9c2011-07-20 15:25:44 -0700283#if 0
284/* FIXME: MX25L6405D has same ID as MX25L6406 */
David Hendricksf8f00c72011-02-01 12:39:46 -0800285static struct w25q_range mx25l6405d_ranges[] = {
286 { X, 0, 0, {0, 0} }, /* none */
287 { X, 0, 0x1, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
288 { X, 0, 0x2, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
289 { X, 0, 0x3, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
290 { X, 0, 0x4, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
291 { X, 0, 0x5, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
292 { X, 0, 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
293 { X, 0, 0x7, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */
294
295 { X, 1, 0x0, {0x000000, 8192 * 1024} },
296 { X, 1, 0x1, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
297 { X, 1, 0x2, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */
298 { X, 1, 0x3, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */
299 { X, 1, 0x4, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */
300 { X, 1, 0x5, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */
301 { X, 1, 0x6, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */
302 { X, 1, 0x7, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */
303};
David Hendricks1c9bc9c2011-07-20 15:25:44 -0700304#endif
305
306/* FIXME: MX25L6406 has same ID as MX25L6405D */
307static struct w25q_range mx25l6406e_ranges[] = {
308 { X, 0, 0, {0, 0} }, /* none */
309 { X, 0, 0x1, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */
310 { X, 0, 0x2, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */
311 { X, 0, 0x3, {0x7a0000, 64 * 8 * 1024} }, /* blocks 120-127 */
312 { X, 0, 0x4, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */
313 { X, 0, 0x5, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */
314 { X, 0, 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
315 { X, 0, 0x7, {0x000000, 64 * 128 * 1024} }, /* all */
316
317 { X, 1, 0x0, {0x000000, 64 * 128 * 1024} }, /* all */
318 { X, 1, 0x1, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
319 { X, 1, 0x2, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */
320 { X, 1, 0x3, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */
321 { X, 1, 0x4, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */
322 { X, 1, 0x5, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */
323 { X, 1, 0x6, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */
324 { X, 1, 0x7, {0x000000, 64 * 128 * 1024} }, /* all */
325};
David Hendricksf8f00c72011-02-01 12:39:46 -0800326
David Hendricksbfa624b2012-07-24 12:47:59 -0700327static struct w25q_range n25q064_ranges[] = {
328 { X, 0, 0, {0, 0} }, /* none */
329
330 { 0, 0, 0x1, {0x7f0000, 64 * 1024} }, /* block 127 */
331 { 0, 0, 0x2, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
332 { 0, 0, 0x3, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
333 { 0, 0, 0x4, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
334 { 0, 0, 0x5, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
335 { 0, 0, 0x6, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
336 { 0, 0, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
337
338 { 1, 0, 0x1, {0x000000, 64 * 1024} }, /* block 0 */
339 { 1, 0, 0x2, {0x000000, 2 * 64 * 1024} }, /* blocks 0-1 */
340 { 1, 0, 0x3, {0x000000, 4 * 64 * 1024} }, /* blocks 0-3 */
341 { 1, 0, 0x4, {0x000000, 8 * 64 * 1024} }, /* blocks 0-7 */
342 { 1, 0, 0x5, {0x000000, 16 * 64 * 1024} }, /* blocks 0-15 */
343 { 1, 0, 0x6, {0x000000, 32 * 64 * 1024} }, /* blocks 0-31 */
344 { 1, 0, 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
345
346 { X, 1, 0x0, {0x000000, 128 * 64 * 1024} }, /* all */
347 { X, 1, 0x1, {0x000000, 128 * 64 * 1024} }, /* all */
348 { X, 1, 0x2, {0x000000, 128 * 64 * 1024} }, /* all */
349 { X, 1, 0x3, {0x000000, 128 * 64 * 1024} }, /* all */
350 { X, 1, 0x4, {0x000000, 128 * 64 * 1024} }, /* all */
351 { X, 1, 0x5, {0x000000, 128 * 64 * 1024} }, /* all */
352 { X, 1, 0x6, {0x000000, 128 * 64 * 1024} }, /* all */
353 { X, 1, 0x7, {0x000000, 128 * 64 * 1024} }, /* all */
354};
355
David Hendricksf7924d12010-06-10 21:26:44 -0700356static struct w25q_range w25q16_ranges[] = {
357 { X, X, 0, {0, 0} }, /* none */
358 { 0, 0, 0x1, {0x1f0000, 64 * 1024} },
359 { 0, 0, 0x2, {0x1e0000, 128 * 1024} },
360 { 0, 0, 0x3, {0x1c0000, 256 * 1024} },
361 { 0, 0, 0x4, {0x180000, 512 * 1024} },
362 { 0, 0, 0x5, {0x100000, 1024 * 1024} },
363
364 { 0, 1, 0x1, {0x000000, 64 * 1024} },
365 { 0, 1, 0x2, {0x000000, 128 * 1024} },
366 { 0, 1, 0x3, {0x000000, 256 * 1024} },
367 { 0, 1, 0x4, {0x000000, 512 * 1024} },
368 { 0, 1, 0x5, {0x000000, 1024 * 1024} },
369 { X, X, 0x6, {0x000000, 2048 * 1024} },
370 { X, X, 0x7, {0x000000, 2048 * 1024} },
371
372 { 1, 0, 0x1, {0x1ff000, 4 * 1024} },
373 { 1, 0, 0x2, {0x1fe000, 8 * 1024} },
374 { 1, 0, 0x3, {0x1fc000, 16 * 1024} },
375 { 1, 0, 0x4, {0x1f8000, 32 * 1024} },
376 { 1, 0, 0x5, {0x1f8000, 32 * 1024} },
377
378 { 1, 1, 0x1, {0x000000, 4 * 1024} },
379 { 1, 1, 0x2, {0x000000, 8 * 1024} },
380 { 1, 1, 0x3, {0x000000, 16 * 1024} },
381 { 1, 1, 0x4, {0x000000, 32 * 1024} },
382 { 1, 1, 0x5, {0x000000, 32 * 1024} },
383};
384
385static struct w25q_range w25q32_ranges[] = {
386 { X, X, 0, {0, 0} }, /* none */
387 { 0, 0, 0x1, {0x3f0000, 64 * 1024} },
388 { 0, 0, 0x2, {0x3e0000, 128 * 1024} },
389 { 0, 0, 0x3, {0x3c0000, 256 * 1024} },
390 { 0, 0, 0x4, {0x380000, 512 * 1024} },
391 { 0, 0, 0x5, {0x300000, 1024 * 1024} },
David Hendricks05653ff2010-06-15 16:05:12 -0700392 { 0, 0, 0x6, {0x200000, 2048 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700393
394 { 0, 1, 0x1, {0x000000, 64 * 1024} },
395 { 0, 1, 0x2, {0x000000, 128 * 1024} },
396 { 0, 1, 0x3, {0x000000, 256 * 1024} },
397 { 0, 1, 0x4, {0x000000, 512 * 1024} },
398 { 0, 1, 0x5, {0x000000, 1024 * 1024} },
399 { 0, 1, 0x6, {0x000000, 2048 * 1024} },
400 { X, X, 0x7, {0x000000, 4096 * 1024} },
401
402 { 1, 0, 0x1, {0x3ff000, 4 * 1024} },
403 { 1, 0, 0x2, {0x3fe000, 8 * 1024} },
404 { 1, 0, 0x3, {0x3fc000, 16 * 1024} },
405 { 1, 0, 0x4, {0x3f8000, 32 * 1024} },
406 { 1, 0, 0x5, {0x3f8000, 32 * 1024} },
407
408 { 1, 1, 0x1, {0x000000, 4 * 1024} },
409 { 1, 1, 0x2, {0x000000, 8 * 1024} },
410 { 1, 1, 0x3, {0x000000, 16 * 1024} },
411 { 1, 1, 0x4, {0x000000, 32 * 1024} },
412 { 1, 1, 0x5, {0x000000, 32 * 1024} },
413};
414
415static struct w25q_range w25q80_ranges[] = {
416 { X, X, 0, {0, 0} }, /* none */
417 { 0, 0, 0x1, {0x0f0000, 64 * 1024} },
418 { 0, 0, 0x2, {0x0e0000, 128 * 1024} },
419 { 0, 0, 0x3, {0x0c0000, 256 * 1024} },
420 { 0, 0, 0x4, {0x080000, 512 * 1024} },
421
422 { 0, 1, 0x1, {0x000000, 64 * 1024} },
423 { 0, 1, 0x2, {0x000000, 128 * 1024} },
424 { 0, 1, 0x3, {0x000000, 256 * 1024} },
425 { 0, 1, 0x4, {0x000000, 512 * 1024} },
David Hendricks05653ff2010-06-15 16:05:12 -0700426 { X, X, 0x6, {0x000000, 1024 * 1024} },
427 { X, X, 0x7, {0x000000, 1024 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700428
429 { 1, 0, 0x1, {0x1ff000, 4 * 1024} },
430 { 1, 0, 0x2, {0x1fe000, 8 * 1024} },
431 { 1, 0, 0x3, {0x1fc000, 16 * 1024} },
432 { 1, 0, 0x4, {0x1f8000, 32 * 1024} },
433 { 1, 0, 0x5, {0x1f8000, 32 * 1024} },
434
435 { 1, 1, 0x1, {0x000000, 4 * 1024} },
436 { 1, 1, 0x2, {0x000000, 8 * 1024} },
437 { 1, 1, 0x3, {0x000000, 16 * 1024} },
438 { 1, 1, 0x4, {0x000000, 32 * 1024} },
439 { 1, 1, 0x5, {0x000000, 32 * 1024} },
440};
441
David Hendricks2c4a76c2010-06-28 14:00:43 -0700442static struct w25q_range w25q64_ranges[] = {
443 { X, X, 0, {0, 0} }, /* none */
444
445 { 0, 0, 0x1, {0x7e0000, 128 * 1024} },
446 { 0, 0, 0x2, {0x7c0000, 256 * 1024} },
447 { 0, 0, 0x3, {0x780000, 512 * 1024} },
448 { 0, 0, 0x4, {0x700000, 1024 * 1024} },
449 { 0, 0, 0x5, {0x600000, 2048 * 1024} },
450 { 0, 0, 0x6, {0x400000, 4096 * 1024} },
451
452 { 0, 1, 0x1, {0x000000, 128 * 1024} },
453 { 0, 1, 0x2, {0x000000, 256 * 1024} },
454 { 0, 1, 0x3, {0x000000, 512 * 1024} },
455 { 0, 1, 0x4, {0x000000, 1024 * 1024} },
456 { 0, 1, 0x5, {0x000000, 2048 * 1024} },
457 { 0, 1, 0x6, {0x000000, 4096 * 1024} },
458 { X, X, 0x7, {0x000000, 8192 * 1024} },
459
460 { 1, 0, 0x1, {0x7ff000, 4 * 1024} },
461 { 1, 0, 0x2, {0x7fe000, 8 * 1024} },
462 { 1, 0, 0x3, {0x7fc000, 16 * 1024} },
463 { 1, 0, 0x4, {0x7f8000, 32 * 1024} },
464 { 1, 0, 0x5, {0x7f8000, 32 * 1024} },
465
466 { 1, 1, 0x1, {0x000000, 4 * 1024} },
467 { 1, 1, 0x2, {0x000000, 8 * 1024} },
468 { 1, 1, 0x3, {0x000000, 16 * 1024} },
469 { 1, 1, 0x4, {0x000000, 32 * 1024} },
470 { 1, 1, 0x5, {0x000000, 32 * 1024} },
471};
472
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800473struct w25q_range w25x10_ranges[] = {
474 { X, X, 0, {0, 0} }, /* none */
475 { 0, 0, 0x1, {0x010000, 64 * 1024} },
476 { 0, 1, 0x1, {0x000000, 64 * 1024} },
477 { X, X, 0x2, {0x000000, 128 * 1024} },
478 { X, X, 0x3, {0x000000, 128 * 1024} },
479};
480
481struct w25q_range w25x20_ranges[] = {
482 { X, X, 0, {0, 0} }, /* none */
483 { 0, 0, 0x1, {0x030000, 64 * 1024} },
484 { 0, 0, 0x2, {0x020000, 128 * 1024} },
485 { 0, 1, 0x1, {0x000000, 64 * 1024} },
486 { 0, 1, 0x2, {0x000000, 128 * 1024} },
487 { 0, X, 0x3, {0x000000, 256 * 1024} },
488};
489
David Hendricks470ca952010-08-13 14:01:53 -0700490struct w25q_range w25x40_ranges[] = {
491 { X, X, 0, {0, 0} }, /* none */
492 { 0, 0, 0x1, {0x070000, 64 * 1024} },
493 { 0, 0, 0x2, {0x060000, 128 * 1024} },
494 { 0, 0, 0x3, {0x040000, 256 * 1024} },
495 { 0, 1, 0x1, {0x000000, 64 * 1024} },
496 { 0, 1, 0x2, {0x000000, 128 * 1024} },
497 { 0, 1, 0x3, {0x000000, 256 * 1024} },
498 { 0, X, 0x4, {0x000000, 512 * 1024} },
499};
500
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800501struct w25q_range w25x80_ranges[] = {
502 { X, X, 0, {0, 0} }, /* none */
503 { 0, 0, 0x1, {0x0F0000, 64 * 1024} },
504 { 0, 0, 0x2, {0x0E0000, 128 * 1024} },
505 { 0, 0, 0x3, {0x0C0000, 256 * 1024} },
506 { 0, 0, 0x4, {0x080000, 512 * 1024} },
507 { 0, 1, 0x1, {0x000000, 64 * 1024} },
508 { 0, 1, 0x2, {0x000000, 128 * 1024} },
509 { 0, 1, 0x3, {0x000000, 256 * 1024} },
510 { 0, 1, 0x4, {0x000000, 512 * 1024} },
511 { 0, X, 0x5, {0x000000, 1024 * 1024} },
512 { 0, X, 0x6, {0x000000, 1024 * 1024} },
513 { 0, X, 0x7, {0x000000, 1024 * 1024} },
514};
515
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700516static struct w25q_range gd25q64_ranges[] = {
517 { X, X, 0, {0, 0} }, /* none */
518 { 0, 0, 0x1, {0x7e0000, 128 * 1024} },
519 { 0, 0, 0x2, {0x7c0000, 256 * 1024} },
520 { 0, 0, 0x3, {0x780000, 512 * 1024} },
521 { 0, 0, 0x4, {0x700000, 1024 * 1024} },
522 { 0, 0, 0x5, {0x600000, 2048 * 1024} },
523 { 0, 0, 0x6, {0x400000, 4096 * 1024} },
524
525 { 0, 1, 0x1, {0x000000, 128 * 1024} },
526 { 0, 1, 0x2, {0x000000, 256 * 1024} },
527 { 0, 1, 0x3, {0x000000, 512 * 1024} },
528 { 0, 1, 0x4, {0x000000, 1024 * 1024} },
529 { 0, 1, 0x5, {0x000000, 2048 * 1024} },
530 { 0, 1, 0x6, {0x000000, 4096 * 1024} },
531 { X, X, 0x7, {0x000000, 8192 * 1024} },
532
533 { 1, 0, 0x1, {0x7ff000, 4 * 1024} },
534 { 1, 0, 0x2, {0x7fe000, 8 * 1024} },
535 { 1, 0, 0x3, {0x7fc000, 16 * 1024} },
536 { 1, 0, 0x4, {0x7f8000, 32 * 1024} },
537 { 1, 0, 0x5, {0x7f8000, 32 * 1024} },
538 { 1, 0, 0x6, {0x7f8000, 32 * 1024} },
539
540 { 1, 1, 0x1, {0x000000, 4 * 1024} },
541 { 1, 1, 0x2, {0x000000, 8 * 1024} },
542 { 1, 1, 0x3, {0x000000, 16 * 1024} },
543 { 1, 1, 0x4, {0x000000, 32 * 1024} },
544 { 1, 1, 0x5, {0x000000, 32 * 1024} },
545 { 1, 1, 0x6, {0x000000, 32 * 1024} },
546};
547
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +0800548static struct w25q_range a25l040_ranges[] = {
549 { X, X, 0x0, {0, 0} }, /* none */
550 { X, X, 0x1, {0x70000, 64 * 1024} },
551 { X, X, 0x2, {0x60000, 128 * 1024} },
552 { X, X, 0x3, {0x40000, 256 * 1024} },
553 { X, X, 0x4, {0x00000, 512 * 1024} },
554 { X, X, 0x5, {0x00000, 512 * 1024} },
555 { X, X, 0x6, {0x00000, 512 * 1024} },
556 { X, X, 0x7, {0x00000, 512 * 1024} },
557};
558
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800559/* Given a flash chip, this function returns its range table. */
560static int w25_range_table(const struct flashchip *flash,
561 struct w25q_range **w25q_ranges,
562 int *num_entries)
David Hendricksf7924d12010-06-10 21:26:44 -0700563{
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800564 *w25q_ranges = 0;
565 *num_entries = 0;
David Hendricksf7924d12010-06-10 21:26:44 -0700566
David Hendricksd494b0a2010-08-16 16:28:50 -0700567 switch (flash->manufacture_id) {
568 case WINBOND_NEX_ID:
569 switch(flash->model_id) {
David Hendricksc801adb2010-12-09 16:58:56 -0800570 case WINBOND_NEX_W25X10:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800571 *w25q_ranges = w25x10_ranges;
572 *num_entries = ARRAY_SIZE(w25x10_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800573 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800574 case WINBOND_NEX_W25X20:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800575 *w25q_ranges = w25x20_ranges;
576 *num_entries = ARRAY_SIZE(w25x20_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800577 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800578 case WINBOND_NEX_W25X40:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800579 *w25q_ranges = w25x40_ranges;
580 *num_entries = ARRAY_SIZE(w25x40_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700581 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800582 case WINBOND_NEX_W25X80:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800583 *w25q_ranges = w25x80_ranges;
584 *num_entries = ARRAY_SIZE(w25x80_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800585 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800586 case WINBOND_NEX_W25Q80:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800587 *w25q_ranges = w25q80_ranges;
588 *num_entries = ARRAY_SIZE(w25q80_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700589 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800590 case WINBOND_NEX_W25Q16:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800591 *w25q_ranges = w25q16_ranges;
592 *num_entries = ARRAY_SIZE(w25q16_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700593 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800594 case WINBOND_NEX_W25Q32:
Louis Yung-Chieh Lo469707f2012-05-18 16:38:37 +0800595 case WINBOND_NEX_W25Q32DW:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800596 *w25q_ranges = w25q32_ranges;
597 *num_entries = ARRAY_SIZE(w25q32_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700598 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800599 case WINBOND_NEX_W25Q64:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800600 *w25q_ranges = w25q64_ranges;
601 *num_entries = ARRAY_SIZE(w25q64_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700602 break;
603 default:
604 msg_cerr("%s() %d: WINBOND flash chip mismatch (0x%04x)"
605 ", aborting\n", __func__, __LINE__,
606 flash->model_id);
607 return -1;
608 }
David Hendricks2c4a76c2010-06-28 14:00:43 -0700609 break;
David Hendricks57566ed2010-08-16 18:24:45 -0700610 case EON_ID_NOPREFIX:
611 switch (flash->model_id) {
David Hendricksc801adb2010-12-09 16:58:56 -0800612 case EON_EN25F40:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800613 *w25q_ranges = en25f40_ranges;
614 *num_entries = ARRAY_SIZE(en25f40_ranges);
David Hendricks57566ed2010-08-16 18:24:45 -0700615 break;
David Hendrickse185bf22011-05-24 15:34:18 -0700616 case EON_EN25Q40:
617 *w25q_ranges = en25q40_ranges;
618 *num_entries = ARRAY_SIZE(en25q40_ranges);
619 break;
620 case EON_EN25Q80:
621 *w25q_ranges = en25q80_ranges;
622 *num_entries = ARRAY_SIZE(en25q80_ranges);
623 break;
624 case EON_EN25Q32:
625 *w25q_ranges = en25q32_ranges;
626 *num_entries = ARRAY_SIZE(en25q32_ranges);
627 break;
628 case EON_EN25Q64:
629 *w25q_ranges = en25q64_ranges;
630 *num_entries = ARRAY_SIZE(en25q64_ranges);
631 break;
632 case EON_EN25Q128:
633 *w25q_ranges = en25q128_ranges;
634 *num_entries = ARRAY_SIZE(en25q128_ranges);
635 break;
David Hendricks57566ed2010-08-16 18:24:45 -0700636 default:
637 msg_cerr("%s():%d: EON flash chip mismatch (0x%04x)"
638 ", aborting\n", __func__, __LINE__,
639 flash->model_id);
640 return -1;
641 }
642 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800643 case MACRONIX_ID:
David Hendricksac72e362010-08-16 18:20:03 -0700644 switch (flash->model_id) {
David Hendricksf8f00c72011-02-01 12:39:46 -0800645 case MACRONIX_MX25L1005:
646 *w25q_ranges = mx25l1005_ranges;
647 *num_entries = ARRAY_SIZE(mx25l1005_ranges);
648 break;
649 case MACRONIX_MX25L2005:
650 *w25q_ranges = mx25l2005_ranges;
651 *num_entries = ARRAY_SIZE(mx25l2005_ranges);
652 break;
653 case MACRONIX_MX25L4005:
654 *w25q_ranges = mx25l4005_ranges;
655 *num_entries = ARRAY_SIZE(mx25l4005_ranges);
656 break;
657 case MACRONIX_MX25L8005:
658 *w25q_ranges = mx25l8005_ranges;
659 *num_entries = ARRAY_SIZE(mx25l8005_ranges);
660 break;
661 case MACRONIX_MX25L1605:
662 /* FIXME: MX25L1605 and MX25L1605D have different write
663 * protection capabilities, but share IDs */
664 *w25q_ranges = mx25l1605d_ranges;
665 *num_entries = ARRAY_SIZE(mx25l1605d_ranges);
666 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800667 case MACRONIX_MX25L3205:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800668 *w25q_ranges = mx25l3205d_ranges;
669 *num_entries = ARRAY_SIZE(mx25l3205d_ranges);
David Hendricksac72e362010-08-16 18:20:03 -0700670 break;
David Hendricksf8f00c72011-02-01 12:39:46 -0800671 case MACRONIX_MX25L6405:
David Hendricks1c9bc9c2011-07-20 15:25:44 -0700672 /* FIXME: MX25L64* chips have mixed capabilities and
673 share IDs */
674 *w25q_ranges = mx25l6406e_ranges;
675 *num_entries = ARRAY_SIZE(mx25l6406e_ranges);
David Hendricksf8f00c72011-02-01 12:39:46 -0800676 break;
David Hendricksac72e362010-08-16 18:20:03 -0700677 default:
678 msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)"
679 ", aborting\n", __func__, __LINE__,
680 flash->model_id);
681 return -1;
682 }
683 break;
David Hendricksbfa624b2012-07-24 12:47:59 -0700684 case ST_ID:
685 switch(flash->model_id) {
686 case ST_N25Q064__1E:
687 case ST_N25Q064__3E:
688 *w25q_ranges = n25q064_ranges;
689 *num_entries = ARRAY_SIZE(n25q064_ranges);
690 break;
691 default:
692 msg_cerr("%s() %d: Micron flash chip mismatch"
693 " (0x%04x), aborting\n", __func__, __LINE__,
694 flash->model_id);
695 return -1;
696 }
697 break;
Bryan Freed9a0051f2012-05-22 16:06:09 -0700698 case GIGADEVICE_ID:
699 switch(flash->model_id) {
700 case GIGADEVICE_GD25LQ32:
701 *w25q_ranges = w25q32_ranges;
702 *num_entries = ARRAY_SIZE(w25q32_ranges);
703 break;
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700704 case GIGADEVICE_GD25Q64:
705 *w25q_ranges = gd25q64_ranges;
706 *num_entries = ARRAY_SIZE(gd25q64_ranges);
707 break;
708 /* TODO(shawnn): add support for other GD parts */
Bryan Freed9a0051f2012-05-22 16:06:09 -0700709 default:
710 msg_cerr("%s() %d: GigaDevice flash chip mismatch"
711 " (0x%04x), aborting\n", __func__, __LINE__,
712 flash->model_id);
713 return -1;
714 }
715 break;
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +0800716 case AMIC_ID_NOPREFIX:
717 switch(flash->model_id) {
718 case AMIC_A25L040:
719 *w25q_ranges = a25l040_ranges;
720 *num_entries = ARRAY_SIZE(a25l040_ranges);
721 break;
722 default:
723 msg_cerr("%s() %d: AMIC flash chip mismatch"
724 " (0x%04x), aborting\n", __func__, __LINE__,
725 flash->model_id);
726 return -1;
727 }
728 break;
David Hendricksf7924d12010-06-10 21:26:44 -0700729 default:
David Hendricksd494b0a2010-08-16 16:28:50 -0700730 msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
731 __func__, flash->manufacture_id);
David Hendricksf7924d12010-06-10 21:26:44 -0700732 return -1;
733 }
734
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800735 return 0;
736}
737
738int w25_range_to_status(const struct flashchip *flash,
739 unsigned int start, unsigned int len,
740 struct w25q_status *status)
741{
742 struct w25q_range *w25q_ranges;
743 int i, range_found = 0;
744 int num_entries;
745
746 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
David Hendricksf7924d12010-06-10 21:26:44 -0700747 for (i = 0; i < num_entries; i++) {
748 struct wp_range *r = &w25q_ranges[i].range;
749
750 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
751 start, len, r->start, r->len);
752 if ((start == r->start) && (len == r->len)) {
David Hendricksd494b0a2010-08-16 16:28:50 -0700753 status->bp0 = w25q_ranges[i].bp & 1;
754 status->bp1 = w25q_ranges[i].bp >> 1;
755 status->bp2 = w25q_ranges[i].bp >> 2;
756 status->tb = w25q_ranges[i].tb;
757 status->sec = w25q_ranges[i].sec;
David Hendricksf7924d12010-06-10 21:26:44 -0700758
759 range_found = 1;
760 break;
761 }
762 }
763
764 if (!range_found) {
765 msg_cerr("matching range not found\n");
766 return -1;
767 }
David Hendricksd494b0a2010-08-16 16:28:50 -0700768 return 0;
769}
770
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800771int w25_status_to_range(const struct flashchip *flash,
772 const struct w25q_status *status,
773 unsigned int *start, unsigned int *len)
774{
775 struct w25q_range *w25q_ranges;
776 int i, status_found = 0;
777 int num_entries;
778
779 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
780 for (i = 0; i < num_entries; i++) {
781 int bp;
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +0800782 int table_bp, table_tb, table_sec;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800783
784 bp = status->bp0 | (status->bp1 << 1) | (status->bp2 << 2);
785 msg_cspew("comparing 0x%x 0x%x / 0x%x 0x%x / 0x%x 0x%x\n",
786 bp, w25q_ranges[i].bp,
787 status->tb, w25q_ranges[i].tb,
788 status->sec, w25q_ranges[i].sec);
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +0800789 table_bp = w25q_ranges[i].bp;
790 table_tb = w25q_ranges[i].tb;
791 table_sec = w25q_ranges[i].sec;
792 if ((bp == table_bp || table_bp == X) &&
793 (status->tb == table_tb || table_tb == X) &&
794 (status->sec == table_sec || table_sec == X)) {
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800795 *start = w25q_ranges[i].range.start;
796 *len = w25q_ranges[i].range.len;
797
798 status_found = 1;
799 break;
800 }
801 }
802
803 if (!status_found) {
804 msg_cerr("matching status not found\n");
805 return -1;
806 }
807 return 0;
808}
809
Louis Yung-Chieh Lo52aa9302010-09-06 10:45:02 +0800810/* Since most chips we use must be WREN-ed before WRSR,
811 * we copy a write status function here before we have a good solution. */
812static int spi_write_status_register_WREN(int status)
813{
814 int result;
815 struct spi_command cmds[] = {
816 {
817 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
818 .writecnt = JEDEC_WREN_OUTSIZE,
819 .writearr = (const unsigned char[]){ JEDEC_WREN },
820 .readcnt = 0,
821 .readarr = NULL,
822 }, {
823 .writecnt = JEDEC_WRSR_OUTSIZE,
824 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
825 .readcnt = 0,
826 .readarr = NULL,
827 }, {
828 .writecnt = 0,
829 .writearr = NULL,
830 .readcnt = 0,
831 .readarr = NULL,
832 }};
833
834 result = spi_send_multicommand(cmds);
835 if (result) {
836 msg_cerr("%s failed during command execution\n",
837 __func__);
838 }
Louis Yung-Chieh Lo96222b12010-11-01 11:48:11 +0800839
840 /* WRSR performs a self-timed erase before the changes take effect. */
841 programmer_delay(WRITE_STATUS_REGISTER_DELAY);
842
Louis Yung-Chieh Lo52aa9302010-09-06 10:45:02 +0800843 return result;
844}
845
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800846/* Given a [start, len], this function calls w25_range_to_status() to convert
847 * it to flash-chip-specific range bits, then sets into status register.
848 */
David Hendricks91040832011-07-08 20:01:09 -0700849static int w25_set_range(const struct flashchip *flash,
David Hendricksd494b0a2010-08-16 16:28:50 -0700850 unsigned int start, unsigned int len)
851{
852 struct w25q_status status;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800853 int tmp = 0;
854 int expected = 0;
David Hendricksd494b0a2010-08-16 16:28:50 -0700855
856 memset(&status, 0, sizeof(status));
857 tmp = spi_read_status_register();
858 memcpy(&status, &tmp, 1);
859 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
860
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800861 if (w25_range_to_status(flash, start, len, &status)) return -1;
David Hendricksf7924d12010-06-10 21:26:44 -0700862
863 msg_cdbg("status.busy: %x\n", status.busy);
864 msg_cdbg("status.wel: %x\n", status.wel);
865 msg_cdbg("status.bp0: %x\n", status.bp0);
866 msg_cdbg("status.bp1: %x\n", status.bp1);
867 msg_cdbg("status.bp2: %x\n", status.bp2);
868 msg_cdbg("status.tb: %x\n", status.tb);
869 msg_cdbg("status.sec: %x\n", status.sec);
870 msg_cdbg("status.srp0: %x\n", status.srp0);
871
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800872 memcpy(&expected, &status, sizeof(status));
873 spi_write_status_register_WREN(expected);
David Hendricksf7924d12010-06-10 21:26:44 -0700874
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800875 tmp = spi_read_status_register();
876 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
877 if ((tmp & MASK_WP_AREA) == (expected & MASK_WP_AREA)) {
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800878 return 0;
879 } else {
David Hendricksc801adb2010-12-09 16:58:56 -0800880 msg_cerr("expected=0x%02x, but actual=0x%02x.\n",
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800881 expected, tmp);
882 return 1;
883 }
David Hendricksf7924d12010-06-10 21:26:44 -0700884}
885
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800886/* Print out the current status register value with human-readable text. */
David Hendricks91040832011-07-08 20:01:09 -0700887static int w25_wp_status(const struct flashchip *flash)
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800888{
889 struct w25q_status status;
890 int tmp;
David Hendricksce8ded32010-10-08 11:23:38 -0700891 unsigned int start, len;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800892 int ret = 0;
893
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800894 memset(&status, 0, sizeof(status));
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800895 tmp = spi_read_status_register();
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800896 memcpy(&status, &tmp, 1);
897 msg_cinfo("WP: status: 0x%02x\n", tmp);
898 msg_cinfo("WP: status.srp0: %x\n", status.srp0);
899 msg_cinfo("WP: write protect is %s.\n",
900 status.srp0 ? "enabled" : "disabled");
901
902 msg_cinfo("WP: write protect range: ");
903 if (w25_status_to_range(flash, &status, &start, &len)) {
904 msg_cinfo("(cannot resolve the range)\n");
905 ret = -1;
906 } else {
907 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
908 }
909
910 return ret;
911}
912
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800913/* Set/clear the SRP0 bit in the status register. */
David Hendricks91040832011-07-08 20:01:09 -0700914static int w25_set_srp0(const struct flashchip *flash, int enable)
David Hendricksf7924d12010-06-10 21:26:44 -0700915{
916 struct w25q_status status;
917 int tmp = 0;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800918 int expected = 0;
David Hendricksf7924d12010-06-10 21:26:44 -0700919
920 memset(&status, 0, sizeof(status));
921 tmp = spi_read_status_register();
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800922 /* FIXME: this is NOT endian-free copy. */
David Hendricksf7924d12010-06-10 21:26:44 -0700923 memcpy(&status, &tmp, 1);
924 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
925
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800926 status.srp0 = enable ? 1 : 0;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800927 memcpy(&expected, &status, sizeof(status));
928 spi_write_status_register_WREN(expected);
929
930 tmp = spi_read_status_register();
931 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
932 if ((tmp & MASK_WP_AREA) != (expected & MASK_WP_AREA))
933 return 1;
David Hendricksf7924d12010-06-10 21:26:44 -0700934
935 return 0;
936}
937
David Hendricks1c09f802012-10-03 11:03:48 -0700938static int w25_enable_writeprotect(const struct flashchip *flash,
939 enum wp_mode wp_mode)
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800940{
941 int ret;
942
David Hendricks1c09f802012-10-03 11:03:48 -0700943 switch (wp_mode) {
944 case WP_MODE_HARDWARE:
945 ret = w25_set_srp0(flash, 1);
946 break;
947 default:
948 msg_cerr("%s(): unsupported write-protect mode\n", __func__);
949 return 1;
950 }
951
David Hendricksc801adb2010-12-09 16:58:56 -0800952 if (ret)
953 msg_cerr("%s(): error=%d.\n", __func__, ret);
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800954 return ret;
955}
956
David Hendricks91040832011-07-08 20:01:09 -0700957static int w25_disable_writeprotect(const struct flashchip *flash)
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800958{
959 int ret;
960
961 ret = w25_set_srp0(flash, 0);
David Hendricksc801adb2010-12-09 16:58:56 -0800962 if (ret)
963 msg_cerr("%s(): error=%d.\n", __func__, ret);
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800964 return ret;
965}
966
David Hendricks91040832011-07-08 20:01:09 -0700967static int w25_list_ranges(const struct flashchip *flash)
David Hendricks0f7f5382011-02-11 18:12:31 -0800968{
969 struct w25q_range *w25q_ranges;
970 int i, num_entries;
971
972 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
973 for (i = 0; i < num_entries; i++) {
974 msg_cinfo("start: 0x%06x, length: 0x%06x\n",
975 w25q_ranges[i].range.start,
976 w25q_ranges[i].range.len);
977 }
978
979 return 0;
980}
981
David Hendricks1c09f802012-10-03 11:03:48 -0700982/* FIXME: Move to spi25.c if it's a JEDEC standard opcode */
983uint8_t w25q_read_status_register_2(void)
984{
985 static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { 0x35 };
986 unsigned char readarr[2];
987 int ret;
988
989 /* Read Status Register */
990 ret = spi_send_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
991 if (ret) {
992 /*
993 * FIXME: make this a benign failure for now in case we are
994 * unable to execute the opcode
995 */
996 msg_cdbg("RDSR2 failed!\n");
997 readarr[0] = 0x00;
998 }
999
1000 return readarr[0];
1001}
1002
1003static int w25q_wp_status(const struct flashchip *flash)
1004{
1005 struct w25q_status sr1;
1006 struct w25q_status_2 sr2;
1007 uint8_t tmp;
1008 unsigned int start, len;
1009 int ret = 0;
1010
1011 memset(&sr1, 0, sizeof(sr1));
1012 tmp = spi_read_status_register();
1013 memcpy(&sr1, &tmp, 1);
1014 memset(&sr2, 0, sizeof(sr2));
1015 tmp = w25q_read_status_register_2();
1016 memcpy(&sr2, &tmp, 1);
1017
1018 msg_cinfo("WP: status: 0x%02x%02x\n", sr2, sr1);
1019 msg_cinfo("WP: status.srp0: %x\n", sr1.srp0);
1020 msg_cinfo("WP: status.srp1: %x\n", sr2.srp1);
1021 msg_cinfo("WP: write protect is %s.\n",
1022 (sr1.srp0 || sr2.srp1) ? "enabled" : "disabled");
1023
1024 msg_cinfo("WP: write protect range: ");
1025 if (w25_status_to_range(flash, &sr1, &start, &len)) {
1026 msg_cinfo("(cannot resolve the range)\n");
1027 ret = -1;
1028 } else {
1029 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
1030 }
1031
1032 return ret;
1033}
1034
1035/*
1036 * W25Q adds an optional byte to the standard WRSR opcode. If /CS is
1037 * de-asserted after the first byte, then it acts like a JEDEC-standard
1038 * WRSR command. if /CS is asserted, then the next data byte is written
1039 * into status register 2.
1040 */
1041#define W25Q_WRSR_OUTSIZE 0x03
1042static int w25q_write_status_register_WREN(uint8_t s1, uint8_t s2)
1043{
1044 int result;
1045 struct spi_command cmds[] = {
1046 {
1047 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
1048 .writecnt = JEDEC_WREN_OUTSIZE,
1049 .writearr = (const unsigned char[]){ JEDEC_WREN },
1050 .readcnt = 0,
1051 .readarr = NULL,
1052 }, {
1053 .writecnt = W25Q_WRSR_OUTSIZE,
1054 .writearr = (const unsigned char[]){ JEDEC_WRSR, s1, s2 },
1055 .readcnt = 0,
1056 .readarr = NULL,
1057 }, {
1058 .writecnt = 0,
1059 .writearr = NULL,
1060 .readcnt = 0,
1061 .readarr = NULL,
1062 }};
1063
1064 result = spi_send_multicommand(cmds);
1065 if (result) {
1066 msg_cerr("%s failed during command execution\n",
1067 __func__);
1068 }
1069
1070 /* WRSR performs a self-timed erase before the changes take effect. */
1071 programmer_delay(WRITE_STATUS_REGISTER_DELAY);
1072
1073 return result;
1074}
1075
1076/*
1077 * Set/clear the SRP1 bit in status register 2.
1078 * FIXME: make this more generic if other chips use the same SR2 layout
1079 */
1080static int w25q_set_srp1(const struct flashchip *flash, int enable)
1081{
1082 struct w25q_status sr1;
1083 struct w25q_status_2 sr2;
1084 uint8_t tmp, expected;
1085
1086 tmp = spi_read_status_register();
1087 memcpy(&sr1, &tmp, 1);
1088 tmp = w25q_read_status_register_2();
1089 memcpy(&sr2, &tmp, 1);
1090
1091 msg_cdbg("%s: old status 2: 0x%02x\n", __func__, tmp);
1092
1093 sr2.srp1 = enable ? 1 : 0;
1094
1095 memcpy(&expected, &sr2, 1);
1096 w25q_write_status_register_WREN(*((uint8_t *)&sr1), *((uint8_t *)&sr2));
1097
1098 tmp = w25q_read_status_register_2();
1099 msg_cdbg("%s: new status 2: 0x%02x\n", __func__, tmp);
1100 if ((tmp & MASK_WP2_AREA) != (expected & MASK_WP2_AREA))
1101 return 1;
1102
1103 return 0;
1104}
1105
1106enum wp_mode get_wp_mode(const char *mode_str)
1107{
1108 enum wp_mode wp_mode = WP_MODE_UNKNOWN;
1109
1110 if (!strcasecmp(mode_str, "hardware"))
1111 wp_mode = WP_MODE_HARDWARE;
1112 else if (!strcasecmp(mode_str, "power_cycle"))
1113 wp_mode = WP_MODE_POWER_CYCLE;
1114 else if (!strcasecmp(mode_str, "permanent"))
1115 wp_mode = WP_MODE_PERMANENT;
1116
1117 return wp_mode;
1118}
1119
1120static int w25q_disable_writeprotect(const struct flashchip *flash,
1121 enum wp_mode wp_mode)
1122{
1123 int ret = 1;
1124 struct w25q_status sr1;
1125 struct w25q_status_2 sr2;
1126 uint8_t tmp;
1127
1128 switch (wp_mode) {
1129 case WP_MODE_HARDWARE:
1130 ret = w25_set_srp0(flash, 0);
1131 break;
1132 case WP_MODE_POWER_CYCLE:
1133 tmp = w25q_read_status_register_2();
1134 memcpy(&sr2, &tmp, 1);
1135 if (sr2.srp1) {
1136 msg_cerr("%s(): must disconnect power to disable "
1137 "write-protection\n", __func__);
1138 } else {
1139 ret = 0;
1140 }
1141 break;
1142 case WP_MODE_PERMANENT:
1143 msg_cerr("%s(): cannot disable permanent write-protection\n",
1144 __func__);
1145 break;
1146 default:
1147 msg_cerr("%s(): invalid mode specified\n", __func__);
1148 break;
1149 }
1150
1151 if (ret)
1152 msg_cerr("%s(): error=%d.\n", __func__, ret);
1153 return ret;
1154}
1155
1156static int w25q_disable_writeprotect_default(const struct flashchip *flash)
1157{
1158 return w25q_disable_writeprotect(flash, WP_MODE_HARDWARE);
1159}
1160
1161static int w25q_enable_writeprotect(const struct flashchip *flash,
1162 enum wp_mode wp_mode)
1163{
1164 int ret = 1;
1165 struct w25q_status sr1;
1166 struct w25q_status_2 sr2;
1167 uint8_t tmp;
1168
1169 switch (wp_mode) {
1170 case WP_MODE_HARDWARE:
1171 if (w25q_disable_writeprotect(flash, WP_MODE_POWER_CYCLE)) {
1172 msg_cerr("%s(): cannot disable power cycle WP mode\n",
1173 __func__);
1174 break;
1175 }
1176
1177 tmp = spi_read_status_register();
1178 memcpy(&sr1, &tmp, 1);
1179 if (sr1.srp0)
1180 ret = 0;
1181 else
1182 ret = w25_set_srp0(flash, 1);
1183
1184 break;
1185 case WP_MODE_POWER_CYCLE:
1186 if (w25q_disable_writeprotect(flash, WP_MODE_HARDWARE)) {
1187 msg_cerr("%s(): cannot disable hardware WP mode\n",
1188 __func__);
1189 break;
1190 }
1191
1192 tmp = w25q_read_status_register_2();
1193 memcpy(&sr2, &tmp, 1);
1194 if (sr2.srp1)
1195 ret = 0;
1196 else
1197 ret = w25q_set_srp1(flash, 1);
1198
1199 break;
1200 case WP_MODE_PERMANENT:
1201 tmp = spi_read_status_register();
1202 memcpy(&sr1, &tmp, 1);
1203 if (sr1.srp0 == 0) {
1204 ret = w25_set_srp0(flash, 1);
1205 if (ret) {
1206 msg_perr("%s(): cannot enable SRP0 for ",
1207 "permanent WP\n", __func__);
1208 break;
1209 }
1210 }
1211
1212 tmp = w25q_read_status_register_2();
1213 memcpy(&sr2, &tmp, 1);
1214 if (sr2.srp1 == 0) {
1215 ret = w25q_set_srp1(flash, 1);
1216 if (ret) {
1217 msg_perr("%s(): cannot enable SRP1 for ",
1218 "permanent WP\n", __func__);
1219 break;
1220 }
1221 }
1222
1223 break;
1224 }
1225
1226 if (ret)
1227 msg_cerr("%s(): error=%d.\n", __func__, ret);
1228 return ret;
1229}
1230
1231/* W25P, W25X, and many flash chips from various vendors */
David Hendricksf7924d12010-06-10 21:26:44 -07001232struct wp wp_w25 = {
David Hendricks0f7f5382011-02-11 18:12:31 -08001233 .list_ranges = w25_list_ranges,
David Hendricksf7924d12010-06-10 21:26:44 -07001234 .set_range = w25_set_range,
1235 .enable = w25_enable_writeprotect,
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001236 .disable = w25_disable_writeprotect,
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001237 .wp_status = w25_wp_status,
David Hendricks1c09f802012-10-03 11:03:48 -07001238
1239};
1240
1241/* W25Q series has features such as a second status register and SFDP */
1242struct wp wp_w25q = {
1243 .list_ranges = w25_list_ranges,
1244 .set_range = w25_set_range,
1245 .enable = w25q_enable_writeprotect,
1246 /*
1247 * By default, disable hardware write-protection. We may change
1248 * this later if we want to add fine-grained write-protect disable
1249 * as a command-line option.
1250 */
1251 .disable = w25q_disable_writeprotect_default,
1252 .wp_status = w25q_wp_status,
David Hendricksf7924d12010-06-10 21:26:44 -07001253};