blob: aba9c4fe0d43b509a6862f12588b015a7d1367eb [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/*
David Hendricksf7924d12010-06-10 21:26:44 -070038 * The following procedures rely on look-up tables to match the user-specified
39 * range with the chip's supported ranges. This turned out to be the most
40 * elegant approach since diferent flash chips use different levels of
41 * granularity and methods to determine protected ranges. In other words,
David Hendrickse0512a72014-07-15 20:30:47 -070042 * be stupid and simple since clever arithmetic will not work for many chips.
David Hendricksf7924d12010-06-10 21:26:44 -070043 */
44
45struct wp_range {
46 unsigned int start; /* starting address */
47 unsigned int len; /* len */
48};
49
50enum bit_state {
51 OFF = 0,
52 ON = 1,
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +080053 X = -1 /* don't care. Must be bigger than max # of bp. */
David Hendricksf7924d12010-06-10 21:26:44 -070054};
55
David Hendrickse0512a72014-07-15 20:30:47 -070056/*
57 * Generic write-protection schema for 25-series SPI flash chips. This assumes
58 * there is a status register that contains one or more consecutive bits which
59 * determine which address range is protected.
60 */
61
62struct status_register_layout {
63 int bp0_pos; /* position of BP0 */
64 int bp_bits; /* number of block protect bits */
65 int srp_pos; /* position of status register protect enable bit */
66};
67
68struct generic_range {
69 unsigned int bp; /* block protect bitfield */
70 struct wp_range range;
71};
72
73struct generic_wp {
74 struct status_register_layout sr1; /* status register 1 */
75 struct generic_range *ranges;
76};
77
78/*
79 * The following ranges and functions are useful for representing Winbond-
80 * style writeprotect schema in which there are typically 5 bits of
81 * relevant information stored in status register 1:
82 * sec: This bit indicates the units (sectors vs. blocks)
83 * tb: The top-bottom bit indicates if the affected range is at the top of
84 * the flash memory's address space or at the bottom.
85 * bp[2:0]: The number of affected sectors/blocks.
86 */
David Hendricksf7924d12010-06-10 21:26:44 -070087struct w25q_range {
88 enum bit_state sec; /* if 1, bp[2:0] describe sectors */
89 enum bit_state tb; /* top/bottom select */
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +080090 int bp; /* block protect bitfield */
David Hendricksf7924d12010-06-10 21:26:44 -070091 struct wp_range range;
92};
93
David Hendrickse0512a72014-07-15 20:30:47 -070094/*
95 * Mask to extract write-protect enable and range bits
96 * Status register 1:
97 * SRP0: bit 7
98 * range(BP2-BP0): bit 4-2
99 * Status register 2:
100 * SRP1: bit 1
101 */
102#define MASK_WP_AREA (0x9C)
103#define MASK_WP2_AREA (0x01)
104
David Hendricks57566ed2010-08-16 18:24:45 -0700105struct w25q_range en25f40_ranges[] = {
106 { X, X, 0, {0, 0} }, /* none */
107 { 0, 0, 0x1, {0x000000, 504 * 1024} },
108 { 0, 0, 0x2, {0x000000, 496 * 1024} },
109 { 0, 0, 0x3, {0x000000, 480 * 1024} },
110 { 0, 0, 0x4, {0x000000, 448 * 1024} },
111 { 0, 0, 0x5, {0x000000, 384 * 1024} },
112 { 0, 0, 0x6, {0x000000, 256 * 1024} },
113 { 0, 0, 0x7, {0x000000, 512 * 1024} },
114};
115
David Hendrickse185bf22011-05-24 15:34:18 -0700116struct w25q_range en25q40_ranges[] = {
117 { 0, 0, 0, {0, 0} }, /* none */
118 { 0, 0, 0x1, {0x000000, 504 * 1024} },
119 { 0, 0, 0x2, {0x000000, 496 * 1024} },
120 { 0, 0, 0x3, {0x000000, 480 * 1024} },
121
122 { 0, 1, 0x0, {0x000000, 448 * 1024} },
123 { 0, 1, 0x1, {0x000000, 384 * 1024} },
124 { 0, 1, 0x2, {0x000000, 256 * 1024} },
125 { 0, 1, 0x3, {0x000000, 512 * 1024} },
126};
127
128struct w25q_range en25q80_ranges[] = {
129 { 0, 0, 0, {0, 0} }, /* none */
130 { 0, 0, 0x1, {0x000000, 1016 * 1024} },
131 { 0, 0, 0x2, {0x000000, 1008 * 1024} },
132 { 0, 0, 0x3, {0x000000, 992 * 1024} },
133 { 0, 0, 0x4, {0x000000, 960 * 1024} },
134 { 0, 0, 0x5, {0x000000, 896 * 1024} },
135 { 0, 0, 0x6, {0x000000, 768 * 1024} },
136 { 0, 0, 0x7, {0x000000, 1024 * 1024} },
137};
138
139struct w25q_range en25q32_ranges[] = {
140 { 0, 0, 0, {0, 0} }, /* none */
141 { 0, 0, 0x1, {0x000000, 4032 * 1024} },
142 { 0, 0, 0x2, {0x000000, 3968 * 1024} },
143 { 0, 0, 0x3, {0x000000, 3840 * 1024} },
144 { 0, 0, 0x4, {0x000000, 3584 * 1024} },
145 { 0, 0, 0x5, {0x000000, 3072 * 1024} },
146 { 0, 0, 0x6, {0x000000, 2048 * 1024} },
147 { 0, 0, 0x7, {0x000000, 4096 * 1024} },
148
149 { 0, 1, 0, {0, 0} }, /* none */
150 { 0, 1, 0x1, {0x010000, 4032 * 1024} },
151 { 0, 1, 0x2, {0x020000, 3968 * 1024} },
152 { 0, 1, 0x3, {0x040000, 3840 * 1024} },
153 { 0, 1, 0x4, {0x080000, 3584 * 1024} },
154 { 0, 1, 0x5, {0x100000, 3072 * 1024} },
155 { 0, 1, 0x6, {0x200000, 2048 * 1024} },
156 { 0, 1, 0x7, {0x000000, 4096 * 1024} },
157};
158
159struct w25q_range en25q64_ranges[] = {
160 { 0, 0, 0, {0, 0} }, /* none */
161 { 0, 0, 0x1, {0x000000, 8128 * 1024} },
162 { 0, 0, 0x2, {0x000000, 8064 * 1024} },
163 { 0, 0, 0x3, {0x000000, 7936 * 1024} },
164 { 0, 0, 0x4, {0x000000, 7680 * 1024} },
165 { 0, 0, 0x5, {0x000000, 7168 * 1024} },
166 { 0, 0, 0x6, {0x000000, 6144 * 1024} },
167 { 0, 0, 0x7, {0x000000, 8192 * 1024} },
168
169 { 0, 1, 0, {0, 0} }, /* none */
170 { 0, 1, 0x1, {0x010000, 8128 * 1024} },
171 { 0, 1, 0x2, {0x020000, 8064 * 1024} },
172 { 0, 1, 0x3, {0x040000, 7936 * 1024} },
173 { 0, 1, 0x4, {0x080000, 7680 * 1024} },
174 { 0, 1, 0x5, {0x100000, 7168 * 1024} },
175 { 0, 1, 0x6, {0x200000, 6144 * 1024} },
176 { 0, 1, 0x7, {0x000000, 8192 * 1024} },
177};
178
179struct w25q_range en25q128_ranges[] = {
180 { 0, 0, 0, {0, 0} }, /* none */
181 { 0, 0, 0x1, {0x000000, 16320 * 1024} },
182 { 0, 0, 0x2, {0x000000, 16256 * 1024} },
183 { 0, 0, 0x3, {0x000000, 16128 * 1024} },
184 { 0, 0, 0x4, {0x000000, 15872 * 1024} },
185 { 0, 0, 0x5, {0x000000, 15360 * 1024} },
186 { 0, 0, 0x6, {0x000000, 14336 * 1024} },
187 { 0, 0, 0x7, {0x000000, 16384 * 1024} },
188
189 { 0, 1, 0, {0, 0} }, /* none */
190 { 0, 1, 0x1, {0x010000, 16320 * 1024} },
191 { 0, 1, 0x2, {0x020000, 16256 * 1024} },
192 { 0, 1, 0x3, {0x040000, 16128 * 1024} },
193 { 0, 1, 0x4, {0x080000, 15872 * 1024} },
194 { 0, 1, 0x5, {0x100000, 15360 * 1024} },
195 { 0, 1, 0x6, {0x200000, 14336 * 1024} },
196 { 0, 1, 0x7, {0x000000, 16384 * 1024} },
197};
198
Marc Jonesb2f90022014-04-29 17:37:23 -0600199struct w25q_range en25s64_ranges[] = {
200 { 0, 0, 0, {0, 0} }, /* none */
201 { 0, 0, 0x1, {0x000000, 8064 * 1024} },
202 { 0, 0, 0x2, {0x000000, 7936 * 1024} },
203 { 0, 0, 0x3, {0x000000, 7680 * 1024} },
204 { 0, 0, 0x4, {0x000000, 7168 * 1024} },
205 { 0, 0, 0x5, {0x000000, 6144 * 1024} },
206 { 0, 0, 0x6, {0x000000, 4096 * 1024} },
207 { 0, 0, 0x7, {0x000000, 8192 * 1024} },
208
209 { 0, 1, 0, {0, 0} }, /* none */
210 { 0, 1, 0x1, {0x7e0000, 128 * 1024} },
211 { 0, 1, 0x2, {0x7c0000, 256 * 1024} },
212 { 0, 1, 0x3, {0x780000, 512 * 1024} },
213 { 0, 1, 0x4, {0x700000, 1024 * 1024} },
214 { 0, 1, 0x5, {0x600000, 2048 * 1024} },
215 { 0, 1, 0x6, {0x400000, 4096 * 1024} },
216 { 0, 1, 0x7, {0x000000, 8192 * 1024} },
217};
218
David Hendricksf8f00c72011-02-01 12:39:46 -0800219/* mx25l1005 ranges also work for the mx25l1005c */
220static struct w25q_range mx25l1005_ranges[] = {
221 { X, X, 0, {0, 0} }, /* none */
222 { X, X, 0x1, {0x010000, 64 * 1024} },
223 { X, X, 0x2, {0x000000, 128 * 1024} },
224 { X, X, 0x3, {0x000000, 128 * 1024} },
225};
226
227static struct w25q_range mx25l2005_ranges[] = {
228 { X, X, 0, {0, 0} }, /* none */
229 { X, X, 0x1, {0x030000, 64 * 1024} },
230 { X, X, 0x2, {0x020000, 128 * 1024} },
231 { X, X, 0x3, {0x000000, 256 * 1024} },
232};
233
234static struct w25q_range mx25l4005_ranges[] = {
235 { X, X, 0, {0, 0} }, /* none */
236 { X, X, 0x1, {0x070000, 64 * 1 * 1024} }, /* block 7 */
237 { X, X, 0x2, {0x060000, 64 * 2 * 1024} }, /* blocks 6-7 */
238 { X, X, 0x3, {0x040000, 64 * 4 * 1024} }, /* blocks 4-7 */
239 { X, X, 0x4, {0x000000, 512 * 1024} },
240 { X, X, 0x5, {0x000000, 512 * 1024} },
241 { X, X, 0x6, {0x000000, 512 * 1024} },
242 { X, X, 0x7, {0x000000, 512 * 1024} },
243};
244
245static struct w25q_range mx25l8005_ranges[] = {
246 { X, X, 0, {0, 0} }, /* none */
247 { X, X, 0x1, {0x0f0000, 64 * 1 * 1024} }, /* block 15 */
248 { X, X, 0x2, {0x0e0000, 64 * 2 * 1024} }, /* blocks 14-15 */
249 { X, X, 0x3, {0x0c0000, 64 * 4 * 1024} }, /* blocks 12-15 */
250 { X, X, 0x4, {0x080000, 64 * 8 * 1024} }, /* blocks 8-15 */
251 { X, X, 0x5, {0x000000, 1024 * 1024} },
252 { X, X, 0x6, {0x000000, 1024 * 1024} },
253 { X, X, 0x7, {0x000000, 1024 * 1024} },
254};
255
256#if 0
257/* FIXME: mx25l1605 has the same IDs as the mx25l1605d */
258static struct w25q_range mx25l1605_ranges[] = {
259 { X, X, 0, {0, 0} }, /* none */
260 { X, X, 0x1, {0x1f0000, 64 * 1024} }, /* block 31 */
261 { X, X, 0x2, {0x1e0000, 128 * 1024} }, /* blocks 30-31 */
262 { X, X, 0x3, {0x1c0000, 256 * 1024} }, /* blocks 28-31 */
263 { X, X, 0x4, {0x180000, 512 * 1024} }, /* blocks 24-31 */
264 { X, X, 0x4, {0x100000, 1024 * 1024} }, /* blocks 16-31 */
265 { X, X, 0x6, {0x000000, 2048 * 1024} },
266 { X, X, 0x7, {0x000000, 2048 * 1024} },
267};
268#endif
269
270#if 0
271/* FIXME: mx25l6405 has the same IDs as the mx25l6405d */
272static struct w25q_range mx25l6405_ranges[] = {
273 { X, 0, 0, {0, 0} }, /* none */
274 { X, 0, 0x1, {0x7f0000, 64 * 1 * 1024} }, /* block 127 */
275 { X, 0, 0x2, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */
276 { X, 0, 0x3, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */
277 { X, 0, 0x4, {0x780000, 64 * 8 * 1024} }, /* blocks 120-127 */
278 { X, 0, 0x5, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */
279 { X, 0, 0x6, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */
280 { X, 0, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
281
282 { X, 1, 0x0, {0x000000, 8192 * 1024} },
283 { X, 1, 0x1, {0x000000, 8192 * 1024} },
284 { X, 1, 0x2, {0x000000, 8192 * 1024} },
285 { X, 1, 0x3, {0x000000, 8192 * 1024} },
286 { X, 1, 0x4, {0x000000, 8192 * 1024} },
287 { X, 1, 0x5, {0x000000, 8192 * 1024} },
288 { X, 1, 0x6, {0x000000, 8192 * 1024} },
289 { X, 1, 0x7, {0x000000, 8192 * 1024} },
290};
291#endif
292
293static struct w25q_range mx25l1605d_ranges[] = {
294 { X, 0, 0, {0, 0} }, /* none */
295 { X, 0, 0x1, {0x1f0000, 64 * 1 * 1024} }, /* block 31 */
296 { X, 0, 0x2, {0x1e0000, 64 * 2 * 1024} }, /* blocks 30-31 */
297 { X, 0, 0x3, {0x1c0000, 64 * 4 * 1024} }, /* blocks 28-31 */
298 { X, 0, 0x4, {0x180000, 64 * 8 * 1024} }, /* blocks 24-31 */
299 { X, 0, 0x5, {0x100000, 64 * 16 * 1024} }, /* blocks 16-31 */
300 { X, 0, 0x6, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
301 { X, 0, 0x7, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
302
303 { X, 1, 0x0, {0x000000, 2048 * 1024} },
304 { X, 1, 0x1, {0x000000, 2048 * 1024} },
305 { X, 1, 0x2, {0x000000, 64 * 16 * 1024} }, /* blocks 0-15 */
306 { X, 1, 0x3, {0x000000, 64 * 24 * 1024} }, /* blocks 0-23 */
307 { X, 1, 0x4, {0x000000, 64 * 28 * 1024} }, /* blocks 0-27 */
308 { X, 1, 0x5, {0x000000, 64 * 30 * 1024} }, /* blocks 0-29 */
309 { X, 1, 0x6, {0x000000, 64 * 31 * 1024} }, /* blocks 0-30 */
310 { X, 1, 0x7, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
311};
312
313/* FIXME: Is there an mx25l3205 (without a trailing letter)? */
David Hendricksac72e362010-08-16 18:20:03 -0700314static struct w25q_range mx25l3205d_ranges[] = {
315 { X, 0, 0, {0, 0} }, /* none */
316 { X, 0, 0x1, {0x3f0000, 64 * 1024} },
317 { X, 0, 0x2, {0x3e0000, 128 * 1024} },
318 { X, 0, 0x3, {0x3c0000, 256 * 1024} },
319 { X, 0, 0x4, {0x380000, 512 * 1024} },
320 { X, 0, 0x5, {0x300000, 1024 * 1024} },
321 { X, 0, 0x6, {0x200000, 2048 * 1024} },
322 { X, 0, 0x7, {0x000000, 4096 * 1024} },
323
324 { X, 1, 0x0, {0x000000, 4096 * 1024} },
325 { X, 1, 0x1, {0x000000, 2048 * 1024} },
326 { X, 1, 0x2, {0x000000, 3072 * 1024} },
327 { X, 1, 0x3, {0x000000, 3584 * 1024} },
328 { X, 1, 0x4, {0x000000, 3840 * 1024} },
329 { X, 1, 0x5, {0x000000, 3968 * 1024} },
330 { X, 1, 0x6, {0x000000, 4032 * 1024} },
331 { X, 1, 0x7, {0x000000, 4096 * 1024} },
332};
333
Vincent Palatin87e092a2013-02-28 15:46:14 -0800334static struct w25q_range mx25u3235e_ranges[] = {
335 { X, 0, 0, {0, 0} }, /* none */
336 { 0, 0, 0x1, {0x3f0000, 64 * 1024} },
337 { 0, 0, 0x2, {0x3e0000, 128 * 1024} },
338 { 0, 0, 0x3, {0x3c0000, 256 * 1024} },
339 { 0, 0, 0x4, {0x380000, 512 * 1024} },
340 { 0, 0, 0x5, {0x300000, 1024 * 1024} },
341 { 0, 0, 0x6, {0x200000, 2048 * 1024} },
342 { 0, 0, 0x7, {0x000000, 4096 * 1024} },
343
344 { 0, 1, 0x0, {0x000000, 4096 * 1024} },
345 { 0, 1, 0x1, {0x000000, 2048 * 1024} },
346 { 0, 1, 0x2, {0x000000, 3072 * 1024} },
347 { 0, 1, 0x3, {0x000000, 3584 * 1024} },
348 { 0, 1, 0x4, {0x000000, 3840 * 1024} },
349 { 0, 1, 0x5, {0x000000, 3968 * 1024} },
350 { 0, 1, 0x6, {0x000000, 4032 * 1024} },
351 { 0, 1, 0x7, {0x000000, 4096 * 1024} },
352};
353
Jongpil66a96492014-08-14 17:59:06 +0900354static struct w25q_range mx25u6435e_ranges[] = {
355 { X, 0, 0, {0, 0} }, /* none */
356 { 0, 0, 0x1, {0x7f0000, 1 * 64 * 1024} }, /* block 127 */
357 { 0, 0, 0x2, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
358 { 0, 0, 0x3, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
359 { 0, 0, 0x4, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
360 { 0, 0, 0x5, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
361 { 0, 0, 0x6, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
362 { 0, 0, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
363
364 { 0, 1, 0x0, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
365 { 0, 1, 0x1, {0x000000, 96 * 64 * 1024} }, /* blocks 0-95 */
366 { 0, 1, 0x2, {0x000000, 112 * 64 * 1024} }, /* blocks 0-111 */
367 { 0, 1, 0x3, {0x000000, 120 * 64 * 1024} }, /* blocks 0-119 */
368 { 0, 1, 0x4, {0x000000, 124 * 64 * 1024} }, /* blocks 0-123 */
369 { 0, 1, 0x5, {0x000000, 126 * 64 * 1024} }, /* blocks 0-125 */
370 { 0, 1, 0x6, {0x000000, 127 * 64 * 1024} }, /* blocks 0-126 */
371 { 0, 1, 0x7, {0x000000, 128 * 64 * 1024} }, /* blocks 0-127 */
372};
373
David Hendricksbfa624b2012-07-24 12:47:59 -0700374static struct w25q_range n25q064_ranges[] = {
375 { X, 0, 0, {0, 0} }, /* none */
376
377 { 0, 0, 0x1, {0x7f0000, 64 * 1024} }, /* block 127 */
378 { 0, 0, 0x2, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
379 { 0, 0, 0x3, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
380 { 0, 0, 0x4, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
381 { 0, 0, 0x5, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
382 { 0, 0, 0x6, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
383 { 0, 0, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
384
385 { 1, 0, 0x1, {0x000000, 64 * 1024} }, /* block 0 */
386 { 1, 0, 0x2, {0x000000, 2 * 64 * 1024} }, /* blocks 0-1 */
387 { 1, 0, 0x3, {0x000000, 4 * 64 * 1024} }, /* blocks 0-3 */
388 { 1, 0, 0x4, {0x000000, 8 * 64 * 1024} }, /* blocks 0-7 */
389 { 1, 0, 0x5, {0x000000, 16 * 64 * 1024} }, /* blocks 0-15 */
390 { 1, 0, 0x6, {0x000000, 32 * 64 * 1024} }, /* blocks 0-31 */
391 { 1, 0, 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
392
393 { X, 1, 0x0, {0x000000, 128 * 64 * 1024} }, /* all */
394 { X, 1, 0x1, {0x000000, 128 * 64 * 1024} }, /* all */
395 { X, 1, 0x2, {0x000000, 128 * 64 * 1024} }, /* all */
396 { X, 1, 0x3, {0x000000, 128 * 64 * 1024} }, /* all */
397 { X, 1, 0x4, {0x000000, 128 * 64 * 1024} }, /* all */
398 { X, 1, 0x5, {0x000000, 128 * 64 * 1024} }, /* all */
399 { X, 1, 0x6, {0x000000, 128 * 64 * 1024} }, /* all */
400 { X, 1, 0x7, {0x000000, 128 * 64 * 1024} }, /* all */
401};
402
David Hendricksf7924d12010-06-10 21:26:44 -0700403static struct w25q_range w25q16_ranges[] = {
404 { X, X, 0, {0, 0} }, /* none */
405 { 0, 0, 0x1, {0x1f0000, 64 * 1024} },
406 { 0, 0, 0x2, {0x1e0000, 128 * 1024} },
407 { 0, 0, 0x3, {0x1c0000, 256 * 1024} },
408 { 0, 0, 0x4, {0x180000, 512 * 1024} },
409 { 0, 0, 0x5, {0x100000, 1024 * 1024} },
410
411 { 0, 1, 0x1, {0x000000, 64 * 1024} },
412 { 0, 1, 0x2, {0x000000, 128 * 1024} },
413 { 0, 1, 0x3, {0x000000, 256 * 1024} },
414 { 0, 1, 0x4, {0x000000, 512 * 1024} },
415 { 0, 1, 0x5, {0x000000, 1024 * 1024} },
416 { X, X, 0x6, {0x000000, 2048 * 1024} },
417 { X, X, 0x7, {0x000000, 2048 * 1024} },
418
419 { 1, 0, 0x1, {0x1ff000, 4 * 1024} },
420 { 1, 0, 0x2, {0x1fe000, 8 * 1024} },
421 { 1, 0, 0x3, {0x1fc000, 16 * 1024} },
422 { 1, 0, 0x4, {0x1f8000, 32 * 1024} },
423 { 1, 0, 0x5, {0x1f8000, 32 * 1024} },
424
425 { 1, 1, 0x1, {0x000000, 4 * 1024} },
426 { 1, 1, 0x2, {0x000000, 8 * 1024} },
427 { 1, 1, 0x3, {0x000000, 16 * 1024} },
428 { 1, 1, 0x4, {0x000000, 32 * 1024} },
429 { 1, 1, 0x5, {0x000000, 32 * 1024} },
430};
431
432static struct w25q_range w25q32_ranges[] = {
433 { X, X, 0, {0, 0} }, /* none */
434 { 0, 0, 0x1, {0x3f0000, 64 * 1024} },
435 { 0, 0, 0x2, {0x3e0000, 128 * 1024} },
436 { 0, 0, 0x3, {0x3c0000, 256 * 1024} },
437 { 0, 0, 0x4, {0x380000, 512 * 1024} },
438 { 0, 0, 0x5, {0x300000, 1024 * 1024} },
David Hendricks05653ff2010-06-15 16:05:12 -0700439 { 0, 0, 0x6, {0x200000, 2048 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700440
441 { 0, 1, 0x1, {0x000000, 64 * 1024} },
442 { 0, 1, 0x2, {0x000000, 128 * 1024} },
443 { 0, 1, 0x3, {0x000000, 256 * 1024} },
444 { 0, 1, 0x4, {0x000000, 512 * 1024} },
445 { 0, 1, 0x5, {0x000000, 1024 * 1024} },
446 { 0, 1, 0x6, {0x000000, 2048 * 1024} },
447 { X, X, 0x7, {0x000000, 4096 * 1024} },
448
449 { 1, 0, 0x1, {0x3ff000, 4 * 1024} },
450 { 1, 0, 0x2, {0x3fe000, 8 * 1024} },
451 { 1, 0, 0x3, {0x3fc000, 16 * 1024} },
452 { 1, 0, 0x4, {0x3f8000, 32 * 1024} },
453 { 1, 0, 0x5, {0x3f8000, 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
462static struct w25q_range w25q80_ranges[] = {
463 { X, X, 0, {0, 0} }, /* none */
464 { 0, 0, 0x1, {0x0f0000, 64 * 1024} },
465 { 0, 0, 0x2, {0x0e0000, 128 * 1024} },
466 { 0, 0, 0x3, {0x0c0000, 256 * 1024} },
467 { 0, 0, 0x4, {0x080000, 512 * 1024} },
468
469 { 0, 1, 0x1, {0x000000, 64 * 1024} },
470 { 0, 1, 0x2, {0x000000, 128 * 1024} },
471 { 0, 1, 0x3, {0x000000, 256 * 1024} },
472 { 0, 1, 0x4, {0x000000, 512 * 1024} },
David Hendricks05653ff2010-06-15 16:05:12 -0700473 { X, X, 0x6, {0x000000, 1024 * 1024} },
474 { X, X, 0x7, {0x000000, 1024 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700475
476 { 1, 0, 0x1, {0x1ff000, 4 * 1024} },
477 { 1, 0, 0x2, {0x1fe000, 8 * 1024} },
478 { 1, 0, 0x3, {0x1fc000, 16 * 1024} },
479 { 1, 0, 0x4, {0x1f8000, 32 * 1024} },
480 { 1, 0, 0x5, {0x1f8000, 32 * 1024} },
481
482 { 1, 1, 0x1, {0x000000, 4 * 1024} },
483 { 1, 1, 0x2, {0x000000, 8 * 1024} },
484 { 1, 1, 0x3, {0x000000, 16 * 1024} },
485 { 1, 1, 0x4, {0x000000, 32 * 1024} },
486 { 1, 1, 0x5, {0x000000, 32 * 1024} },
487};
488
David Hendricks2c4a76c2010-06-28 14:00:43 -0700489static struct w25q_range w25q64_ranges[] = {
490 { X, X, 0, {0, 0} }, /* none */
491
492 { 0, 0, 0x1, {0x7e0000, 128 * 1024} },
493 { 0, 0, 0x2, {0x7c0000, 256 * 1024} },
494 { 0, 0, 0x3, {0x780000, 512 * 1024} },
495 { 0, 0, 0x4, {0x700000, 1024 * 1024} },
496 { 0, 0, 0x5, {0x600000, 2048 * 1024} },
497 { 0, 0, 0x6, {0x400000, 4096 * 1024} },
498
499 { 0, 1, 0x1, {0x000000, 128 * 1024} },
500 { 0, 1, 0x2, {0x000000, 256 * 1024} },
501 { 0, 1, 0x3, {0x000000, 512 * 1024} },
502 { 0, 1, 0x4, {0x000000, 1024 * 1024} },
503 { 0, 1, 0x5, {0x000000, 2048 * 1024} },
504 { 0, 1, 0x6, {0x000000, 4096 * 1024} },
505 { X, X, 0x7, {0x000000, 8192 * 1024} },
506
507 { 1, 0, 0x1, {0x7ff000, 4 * 1024} },
508 { 1, 0, 0x2, {0x7fe000, 8 * 1024} },
509 { 1, 0, 0x3, {0x7fc000, 16 * 1024} },
510 { 1, 0, 0x4, {0x7f8000, 32 * 1024} },
511 { 1, 0, 0x5, {0x7f8000, 32 * 1024} },
512
513 { 1, 1, 0x1, {0x000000, 4 * 1024} },
514 { 1, 1, 0x2, {0x000000, 8 * 1024} },
515 { 1, 1, 0x3, {0x000000, 16 * 1024} },
516 { 1, 1, 0x4, {0x000000, 32 * 1024} },
517 { 1, 1, 0x5, {0x000000, 32 * 1024} },
518};
519
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800520struct w25q_range w25x10_ranges[] = {
521 { X, X, 0, {0, 0} }, /* none */
522 { 0, 0, 0x1, {0x010000, 64 * 1024} },
523 { 0, 1, 0x1, {0x000000, 64 * 1024} },
524 { X, X, 0x2, {0x000000, 128 * 1024} },
525 { X, X, 0x3, {0x000000, 128 * 1024} },
526};
527
528struct w25q_range w25x20_ranges[] = {
529 { X, X, 0, {0, 0} }, /* none */
530 { 0, 0, 0x1, {0x030000, 64 * 1024} },
531 { 0, 0, 0x2, {0x020000, 128 * 1024} },
532 { 0, 1, 0x1, {0x000000, 64 * 1024} },
533 { 0, 1, 0x2, {0x000000, 128 * 1024} },
534 { 0, X, 0x3, {0x000000, 256 * 1024} },
535};
536
David Hendricks470ca952010-08-13 14:01:53 -0700537struct w25q_range w25x40_ranges[] = {
538 { X, X, 0, {0, 0} }, /* none */
539 { 0, 0, 0x1, {0x070000, 64 * 1024} },
540 { 0, 0, 0x2, {0x060000, 128 * 1024} },
541 { 0, 0, 0x3, {0x040000, 256 * 1024} },
542 { 0, 1, 0x1, {0x000000, 64 * 1024} },
543 { 0, 1, 0x2, {0x000000, 128 * 1024} },
544 { 0, 1, 0x3, {0x000000, 256 * 1024} },
545 { 0, X, 0x4, {0x000000, 512 * 1024} },
546};
547
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800548struct w25q_range w25x80_ranges[] = {
549 { X, X, 0, {0, 0} }, /* none */
550 { 0, 0, 0x1, {0x0F0000, 64 * 1024} },
551 { 0, 0, 0x2, {0x0E0000, 128 * 1024} },
552 { 0, 0, 0x3, {0x0C0000, 256 * 1024} },
553 { 0, 0, 0x4, {0x080000, 512 * 1024} },
554 { 0, 1, 0x1, {0x000000, 64 * 1024} },
555 { 0, 1, 0x2, {0x000000, 128 * 1024} },
556 { 0, 1, 0x3, {0x000000, 256 * 1024} },
557 { 0, 1, 0x4, {0x000000, 512 * 1024} },
558 { 0, X, 0x5, {0x000000, 1024 * 1024} },
559 { 0, X, 0x6, {0x000000, 1024 * 1024} },
560 { 0, X, 0x7, {0x000000, 1024 * 1024} },
561};
562
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700563static struct w25q_range gd25q64_ranges[] = {
564 { X, X, 0, {0, 0} }, /* none */
565 { 0, 0, 0x1, {0x7e0000, 128 * 1024} },
566 { 0, 0, 0x2, {0x7c0000, 256 * 1024} },
567 { 0, 0, 0x3, {0x780000, 512 * 1024} },
568 { 0, 0, 0x4, {0x700000, 1024 * 1024} },
569 { 0, 0, 0x5, {0x600000, 2048 * 1024} },
570 { 0, 0, 0x6, {0x400000, 4096 * 1024} },
571
572 { 0, 1, 0x1, {0x000000, 128 * 1024} },
573 { 0, 1, 0x2, {0x000000, 256 * 1024} },
574 { 0, 1, 0x3, {0x000000, 512 * 1024} },
575 { 0, 1, 0x4, {0x000000, 1024 * 1024} },
576 { 0, 1, 0x5, {0x000000, 2048 * 1024} },
577 { 0, 1, 0x6, {0x000000, 4096 * 1024} },
578 { X, X, 0x7, {0x000000, 8192 * 1024} },
579
580 { 1, 0, 0x1, {0x7ff000, 4 * 1024} },
581 { 1, 0, 0x2, {0x7fe000, 8 * 1024} },
582 { 1, 0, 0x3, {0x7fc000, 16 * 1024} },
583 { 1, 0, 0x4, {0x7f8000, 32 * 1024} },
584 { 1, 0, 0x5, {0x7f8000, 32 * 1024} },
585 { 1, 0, 0x6, {0x7f8000, 32 * 1024} },
586
587 { 1, 1, 0x1, {0x000000, 4 * 1024} },
588 { 1, 1, 0x2, {0x000000, 8 * 1024} },
589 { 1, 1, 0x3, {0x000000, 16 * 1024} },
590 { 1, 1, 0x4, {0x000000, 32 * 1024} },
591 { 1, 1, 0x5, {0x000000, 32 * 1024} },
592 { 1, 1, 0x6, {0x000000, 32 * 1024} },
593};
594
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +0800595static struct w25q_range a25l040_ranges[] = {
596 { X, X, 0x0, {0, 0} }, /* none */
597 { X, X, 0x1, {0x70000, 64 * 1024} },
598 { X, X, 0x2, {0x60000, 128 * 1024} },
599 { X, X, 0x3, {0x40000, 256 * 1024} },
600 { X, X, 0x4, {0x00000, 512 * 1024} },
601 { X, X, 0x5, {0x00000, 512 * 1024} },
602 { X, X, 0x6, {0x00000, 512 * 1024} },
603 { X, X, 0x7, {0x00000, 512 * 1024} },
604};
605
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800606/* Given a flash chip, this function returns its range table. */
607static int w25_range_table(const struct flashchip *flash,
608 struct w25q_range **w25q_ranges,
609 int *num_entries)
David Hendricksf7924d12010-06-10 21:26:44 -0700610{
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800611 *w25q_ranges = 0;
612 *num_entries = 0;
David Hendricksf7924d12010-06-10 21:26:44 -0700613
David Hendricksd494b0a2010-08-16 16:28:50 -0700614 switch (flash->manufacture_id) {
615 case WINBOND_NEX_ID:
616 switch(flash->model_id) {
David Hendricksc801adb2010-12-09 16:58:56 -0800617 case WINBOND_NEX_W25X10:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800618 *w25q_ranges = w25x10_ranges;
619 *num_entries = ARRAY_SIZE(w25x10_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800620 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800621 case WINBOND_NEX_W25X20:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800622 *w25q_ranges = w25x20_ranges;
623 *num_entries = ARRAY_SIZE(w25x20_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800624 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800625 case WINBOND_NEX_W25X40:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800626 *w25q_ranges = w25x40_ranges;
627 *num_entries = ARRAY_SIZE(w25x40_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700628 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800629 case WINBOND_NEX_W25X80:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800630 *w25q_ranges = w25x80_ranges;
631 *num_entries = ARRAY_SIZE(w25x80_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800632 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800633 case WINBOND_NEX_W25Q80:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800634 *w25q_ranges = w25q80_ranges;
635 *num_entries = ARRAY_SIZE(w25q80_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700636 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800637 case WINBOND_NEX_W25Q16:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800638 *w25q_ranges = w25q16_ranges;
639 *num_entries = ARRAY_SIZE(w25q16_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700640 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800641 case WINBOND_NEX_W25Q32:
Louis Yung-Chieh Lo469707f2012-05-18 16:38:37 +0800642 case WINBOND_NEX_W25Q32DW:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800643 *w25q_ranges = w25q32_ranges;
644 *num_entries = ARRAY_SIZE(w25q32_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700645 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800646 case WINBOND_NEX_W25Q64:
AdamTsai141a2622013-12-31 14:07:15 +0800647 case WINBOND_NEX_W25Q64DW:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800648 *w25q_ranges = w25q64_ranges;
649 *num_entries = ARRAY_SIZE(w25q64_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700650 break;
651 default:
652 msg_cerr("%s() %d: WINBOND flash chip mismatch (0x%04x)"
653 ", aborting\n", __func__, __LINE__,
654 flash->model_id);
655 return -1;
656 }
David Hendricks2c4a76c2010-06-28 14:00:43 -0700657 break;
David Hendricks57566ed2010-08-16 18:24:45 -0700658 case EON_ID_NOPREFIX:
659 switch (flash->model_id) {
David Hendricksc801adb2010-12-09 16:58:56 -0800660 case EON_EN25F40:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800661 *w25q_ranges = en25f40_ranges;
662 *num_entries = ARRAY_SIZE(en25f40_ranges);
David Hendricks57566ed2010-08-16 18:24:45 -0700663 break;
David Hendrickse185bf22011-05-24 15:34:18 -0700664 case EON_EN25Q40:
665 *w25q_ranges = en25q40_ranges;
666 *num_entries = ARRAY_SIZE(en25q40_ranges);
667 break;
668 case EON_EN25Q80:
669 *w25q_ranges = en25q80_ranges;
670 *num_entries = ARRAY_SIZE(en25q80_ranges);
671 break;
672 case EON_EN25Q32:
673 *w25q_ranges = en25q32_ranges;
674 *num_entries = ARRAY_SIZE(en25q32_ranges);
675 break;
676 case EON_EN25Q64:
677 *w25q_ranges = en25q64_ranges;
678 *num_entries = ARRAY_SIZE(en25q64_ranges);
679 break;
680 case EON_EN25Q128:
681 *w25q_ranges = en25q128_ranges;
682 *num_entries = ARRAY_SIZE(en25q128_ranges);
683 break;
Marc Jonesb2f90022014-04-29 17:37:23 -0600684 case EON_EN25S64:
685 *w25q_ranges = en25s64_ranges;
686 *num_entries = ARRAY_SIZE(en25s64_ranges);
687 break;
David Hendricks57566ed2010-08-16 18:24:45 -0700688 default:
689 msg_cerr("%s():%d: EON flash chip mismatch (0x%04x)"
690 ", aborting\n", __func__, __LINE__,
691 flash->model_id);
692 return -1;
693 }
694 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800695 case MACRONIX_ID:
David Hendricksac72e362010-08-16 18:20:03 -0700696 switch (flash->model_id) {
David Hendricksf8f00c72011-02-01 12:39:46 -0800697 case MACRONIX_MX25L1005:
698 *w25q_ranges = mx25l1005_ranges;
699 *num_entries = ARRAY_SIZE(mx25l1005_ranges);
700 break;
701 case MACRONIX_MX25L2005:
702 *w25q_ranges = mx25l2005_ranges;
703 *num_entries = ARRAY_SIZE(mx25l2005_ranges);
704 break;
705 case MACRONIX_MX25L4005:
706 *w25q_ranges = mx25l4005_ranges;
707 *num_entries = ARRAY_SIZE(mx25l4005_ranges);
708 break;
709 case MACRONIX_MX25L8005:
710 *w25q_ranges = mx25l8005_ranges;
711 *num_entries = ARRAY_SIZE(mx25l8005_ranges);
712 break;
713 case MACRONIX_MX25L1605:
714 /* FIXME: MX25L1605 and MX25L1605D have different write
715 * protection capabilities, but share IDs */
716 *w25q_ranges = mx25l1605d_ranges;
717 *num_entries = ARRAY_SIZE(mx25l1605d_ranges);
718 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800719 case MACRONIX_MX25L3205:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800720 *w25q_ranges = mx25l3205d_ranges;
721 *num_entries = ARRAY_SIZE(mx25l3205d_ranges);
David Hendricksac72e362010-08-16 18:20:03 -0700722 break;
Vincent Palatin87e092a2013-02-28 15:46:14 -0800723 case MACRONIX_MX25U3235E:
724 *w25q_ranges = mx25u3235e_ranges;
725 *num_entries = ARRAY_SIZE(mx25u3235e_ranges);
726 break;
Jongpil66a96492014-08-14 17:59:06 +0900727 case MACRONIX_MX25U6435E:
728 *w25q_ranges = mx25u6435e_ranges;
729 *num_entries = ARRAY_SIZE(mx25u6435e_ranges);
730 break;
David Hendricksac72e362010-08-16 18:20:03 -0700731 default:
732 msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)"
733 ", aborting\n", __func__, __LINE__,
734 flash->model_id);
735 return -1;
736 }
737 break;
David Hendricksbfa624b2012-07-24 12:47:59 -0700738 case ST_ID:
739 switch(flash->model_id) {
740 case ST_N25Q064__1E:
741 case ST_N25Q064__3E:
742 *w25q_ranges = n25q064_ranges;
743 *num_entries = ARRAY_SIZE(n25q064_ranges);
744 break;
745 default:
746 msg_cerr("%s() %d: Micron flash chip mismatch"
747 " (0x%04x), aborting\n", __func__, __LINE__,
748 flash->model_id);
749 return -1;
750 }
751 break;
Bryan Freed9a0051f2012-05-22 16:06:09 -0700752 case GIGADEVICE_ID:
753 switch(flash->model_id) {
754 case GIGADEVICE_GD25LQ32:
755 *w25q_ranges = w25q32_ranges;
756 *num_entries = ARRAY_SIZE(w25q32_ranges);
757 break;
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700758 case GIGADEVICE_GD25Q64:
759 *w25q_ranges = gd25q64_ranges;
760 *num_entries = ARRAY_SIZE(gd25q64_ranges);
761 break;
762 /* TODO(shawnn): add support for other GD parts */
Bryan Freed9a0051f2012-05-22 16:06:09 -0700763 default:
764 msg_cerr("%s() %d: GigaDevice flash chip mismatch"
765 " (0x%04x), aborting\n", __func__, __LINE__,
766 flash->model_id);
767 return -1;
768 }
769 break;
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +0800770 case AMIC_ID_NOPREFIX:
771 switch(flash->model_id) {
772 case AMIC_A25L040:
773 *w25q_ranges = a25l040_ranges;
774 *num_entries = ARRAY_SIZE(a25l040_ranges);
775 break;
776 default:
777 msg_cerr("%s() %d: AMIC flash chip mismatch"
778 " (0x%04x), aborting\n", __func__, __LINE__,
779 flash->model_id);
780 return -1;
781 }
782 break;
David Hendricksf7924d12010-06-10 21:26:44 -0700783 default:
David Hendricksd494b0a2010-08-16 16:28:50 -0700784 msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
785 __func__, flash->manufacture_id);
David Hendricksf7924d12010-06-10 21:26:44 -0700786 return -1;
787 }
788
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800789 return 0;
790}
791
792int w25_range_to_status(const struct flashchip *flash,
793 unsigned int start, unsigned int len,
794 struct w25q_status *status)
795{
796 struct w25q_range *w25q_ranges;
797 int i, range_found = 0;
798 int num_entries;
799
800 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
David Hendricksf7924d12010-06-10 21:26:44 -0700801 for (i = 0; i < num_entries; i++) {
802 struct wp_range *r = &w25q_ranges[i].range;
803
804 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
805 start, len, r->start, r->len);
806 if ((start == r->start) && (len == r->len)) {
David Hendricksd494b0a2010-08-16 16:28:50 -0700807 status->bp0 = w25q_ranges[i].bp & 1;
808 status->bp1 = w25q_ranges[i].bp >> 1;
809 status->bp2 = w25q_ranges[i].bp >> 2;
810 status->tb = w25q_ranges[i].tb;
811 status->sec = w25q_ranges[i].sec;
David Hendricksf7924d12010-06-10 21:26:44 -0700812
813 range_found = 1;
814 break;
815 }
816 }
817
818 if (!range_found) {
819 msg_cerr("matching range not found\n");
820 return -1;
821 }
David Hendricksd494b0a2010-08-16 16:28:50 -0700822 return 0;
823}
824
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800825int w25_status_to_range(const struct flashchip *flash,
826 const struct w25q_status *status,
827 unsigned int *start, unsigned int *len)
828{
829 struct w25q_range *w25q_ranges;
830 int i, status_found = 0;
831 int num_entries;
832
833 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
834 for (i = 0; i < num_entries; i++) {
835 int bp;
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +0800836 int table_bp, table_tb, table_sec;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800837
838 bp = status->bp0 | (status->bp1 << 1) | (status->bp2 << 2);
839 msg_cspew("comparing 0x%x 0x%x / 0x%x 0x%x / 0x%x 0x%x\n",
840 bp, w25q_ranges[i].bp,
841 status->tb, w25q_ranges[i].tb,
842 status->sec, w25q_ranges[i].sec);
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +0800843 table_bp = w25q_ranges[i].bp;
844 table_tb = w25q_ranges[i].tb;
845 table_sec = w25q_ranges[i].sec;
846 if ((bp == table_bp || table_bp == X) &&
847 (status->tb == table_tb || table_tb == X) &&
848 (status->sec == table_sec || table_sec == X)) {
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800849 *start = w25q_ranges[i].range.start;
850 *len = w25q_ranges[i].range.len;
851
852 status_found = 1;
853 break;
854 }
855 }
856
857 if (!status_found) {
858 msg_cerr("matching status not found\n");
859 return -1;
860 }
861 return 0;
862}
863
Louis Yung-Chieh Lo52aa9302010-09-06 10:45:02 +0800864/* Since most chips we use must be WREN-ed before WRSR,
865 * we copy a write status function here before we have a good solution. */
866static int spi_write_status_register_WREN(int status)
867{
868 int result;
869 struct spi_command cmds[] = {
870 {
871 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
872 .writecnt = JEDEC_WREN_OUTSIZE,
873 .writearr = (const unsigned char[]){ JEDEC_WREN },
874 .readcnt = 0,
875 .readarr = NULL,
876 }, {
877 .writecnt = JEDEC_WRSR_OUTSIZE,
878 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
879 .readcnt = 0,
880 .readarr = NULL,
881 }, {
882 .writecnt = 0,
883 .writearr = NULL,
884 .readcnt = 0,
885 .readarr = NULL,
886 }};
887
888 result = spi_send_multicommand(cmds);
889 if (result) {
890 msg_cerr("%s failed during command execution\n",
891 __func__);
892 }
Louis Yung-Chieh Lo96222b12010-11-01 11:48:11 +0800893
894 /* WRSR performs a self-timed erase before the changes take effect. */
895 programmer_delay(WRITE_STATUS_REGISTER_DELAY);
896
Louis Yung-Chieh Lo52aa9302010-09-06 10:45:02 +0800897 return result;
898}
899
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800900/* Given a [start, len], this function calls w25_range_to_status() to convert
901 * it to flash-chip-specific range bits, then sets into status register.
902 */
David Hendricks91040832011-07-08 20:01:09 -0700903static int w25_set_range(const struct flashchip *flash,
David Hendricksd494b0a2010-08-16 16:28:50 -0700904 unsigned int start, unsigned int len)
905{
906 struct w25q_status status;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800907 int tmp = 0;
908 int expected = 0;
David Hendricksd494b0a2010-08-16 16:28:50 -0700909
910 memset(&status, 0, sizeof(status));
911 tmp = spi_read_status_register();
912 memcpy(&status, &tmp, 1);
913 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
914
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800915 if (w25_range_to_status(flash, start, len, &status)) return -1;
David Hendricksf7924d12010-06-10 21:26:44 -0700916
917 msg_cdbg("status.busy: %x\n", status.busy);
918 msg_cdbg("status.wel: %x\n", status.wel);
919 msg_cdbg("status.bp0: %x\n", status.bp0);
920 msg_cdbg("status.bp1: %x\n", status.bp1);
921 msg_cdbg("status.bp2: %x\n", status.bp2);
922 msg_cdbg("status.tb: %x\n", status.tb);
923 msg_cdbg("status.sec: %x\n", status.sec);
924 msg_cdbg("status.srp0: %x\n", status.srp0);
925
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800926 memcpy(&expected, &status, sizeof(status));
927 spi_write_status_register_WREN(expected);
David Hendricksf7924d12010-06-10 21:26:44 -0700928
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800929 tmp = spi_read_status_register();
930 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
931 if ((tmp & MASK_WP_AREA) == (expected & MASK_WP_AREA)) {
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800932 return 0;
933 } else {
David Hendricksc801adb2010-12-09 16:58:56 -0800934 msg_cerr("expected=0x%02x, but actual=0x%02x.\n",
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800935 expected, tmp);
936 return 1;
937 }
David Hendricksf7924d12010-06-10 21:26:44 -0700938}
939
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800940/* Print out the current status register value with human-readable text. */
David Hendricks91040832011-07-08 20:01:09 -0700941static int w25_wp_status(const struct flashchip *flash)
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800942{
943 struct w25q_status status;
944 int tmp;
David Hendricksce8ded32010-10-08 11:23:38 -0700945 unsigned int start, len;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800946 int ret = 0;
947
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800948 memset(&status, 0, sizeof(status));
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800949 tmp = spi_read_status_register();
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800950 memcpy(&status, &tmp, 1);
951 msg_cinfo("WP: status: 0x%02x\n", tmp);
952 msg_cinfo("WP: status.srp0: %x\n", status.srp0);
953 msg_cinfo("WP: write protect is %s.\n",
954 status.srp0 ? "enabled" : "disabled");
955
956 msg_cinfo("WP: write protect range: ");
957 if (w25_status_to_range(flash, &status, &start, &len)) {
958 msg_cinfo("(cannot resolve the range)\n");
959 ret = -1;
960 } else {
961 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
962 }
963
964 return ret;
965}
966
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800967/* Set/clear the SRP0 bit in the status register. */
David Hendricks91040832011-07-08 20:01:09 -0700968static int w25_set_srp0(const struct flashchip *flash, int enable)
David Hendricksf7924d12010-06-10 21:26:44 -0700969{
970 struct w25q_status status;
971 int tmp = 0;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800972 int expected = 0;
David Hendricksf7924d12010-06-10 21:26:44 -0700973
974 memset(&status, 0, sizeof(status));
975 tmp = spi_read_status_register();
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800976 /* FIXME: this is NOT endian-free copy. */
David Hendricksf7924d12010-06-10 21:26:44 -0700977 memcpy(&status, &tmp, 1);
978 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
979
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800980 status.srp0 = enable ? 1 : 0;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800981 memcpy(&expected, &status, sizeof(status));
982 spi_write_status_register_WREN(expected);
983
984 tmp = spi_read_status_register();
985 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
986 if ((tmp & MASK_WP_AREA) != (expected & MASK_WP_AREA))
987 return 1;
David Hendricksf7924d12010-06-10 21:26:44 -0700988
989 return 0;
990}
991
David Hendricks1c09f802012-10-03 11:03:48 -0700992static int w25_enable_writeprotect(const struct flashchip *flash,
993 enum wp_mode wp_mode)
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800994{
995 int ret;
996
David Hendricks1c09f802012-10-03 11:03:48 -0700997 switch (wp_mode) {
998 case WP_MODE_HARDWARE:
999 ret = w25_set_srp0(flash, 1);
1000 break;
1001 default:
1002 msg_cerr("%s(): unsupported write-protect mode\n", __func__);
1003 return 1;
1004 }
1005
David Hendricksc801adb2010-12-09 16:58:56 -08001006 if (ret)
1007 msg_cerr("%s(): error=%d.\n", __func__, ret);
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001008 return ret;
1009}
1010
David Hendricks91040832011-07-08 20:01:09 -07001011static int w25_disable_writeprotect(const struct flashchip *flash)
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001012{
1013 int ret;
1014
1015 ret = w25_set_srp0(flash, 0);
David Hendricksc801adb2010-12-09 16:58:56 -08001016 if (ret)
1017 msg_cerr("%s(): error=%d.\n", __func__, ret);
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001018 return ret;
1019}
1020
David Hendricks91040832011-07-08 20:01:09 -07001021static int w25_list_ranges(const struct flashchip *flash)
David Hendricks0f7f5382011-02-11 18:12:31 -08001022{
1023 struct w25q_range *w25q_ranges;
1024 int i, num_entries;
1025
1026 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
1027 for (i = 0; i < num_entries; i++) {
1028 msg_cinfo("start: 0x%06x, length: 0x%06x\n",
1029 w25q_ranges[i].range.start,
1030 w25q_ranges[i].range.len);
1031 }
1032
1033 return 0;
1034}
1035
David Hendricks1c09f802012-10-03 11:03:48 -07001036/* FIXME: Move to spi25.c if it's a JEDEC standard opcode */
1037uint8_t w25q_read_status_register_2(void)
1038{
1039 static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { 0x35 };
1040 unsigned char readarr[2];
1041 int ret;
1042
1043 /* Read Status Register */
1044 ret = spi_send_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
1045 if (ret) {
1046 /*
1047 * FIXME: make this a benign failure for now in case we are
1048 * unable to execute the opcode
1049 */
1050 msg_cdbg("RDSR2 failed!\n");
1051 readarr[0] = 0x00;
1052 }
1053
1054 return readarr[0];
1055}
1056
1057static int w25q_wp_status(const struct flashchip *flash)
1058{
1059 struct w25q_status sr1;
1060 struct w25q_status_2 sr2;
David Hendricksf1bd8802012-10-30 11:37:57 -07001061 uint8_t tmp[2];
David Hendricks1c09f802012-10-03 11:03:48 -07001062 unsigned int start, len;
1063 int ret = 0;
1064
1065 memset(&sr1, 0, sizeof(sr1));
David Hendricksf1bd8802012-10-30 11:37:57 -07001066 tmp[0] = spi_read_status_register();
1067 memcpy(&sr1, &tmp[0], 1);
David Hendricks1c09f802012-10-03 11:03:48 -07001068
David Hendricksf1bd8802012-10-30 11:37:57 -07001069 memset(&sr2, 0, sizeof(sr2));
1070 tmp[1] = w25q_read_status_register_2();
1071 memcpy(&sr2, &tmp[1], 1);
1072
1073 msg_cinfo("WP: status: 0x%02x%02x\n", tmp[1], tmp[0]);
David Hendricks1c09f802012-10-03 11:03:48 -07001074 msg_cinfo("WP: status.srp0: %x\n", sr1.srp0);
1075 msg_cinfo("WP: status.srp1: %x\n", sr2.srp1);
1076 msg_cinfo("WP: write protect is %s.\n",
1077 (sr1.srp0 || sr2.srp1) ? "enabled" : "disabled");
1078
1079 msg_cinfo("WP: write protect range: ");
1080 if (w25_status_to_range(flash, &sr1, &start, &len)) {
1081 msg_cinfo("(cannot resolve the range)\n");
1082 ret = -1;
1083 } else {
1084 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
1085 }
1086
1087 return ret;
1088}
1089
1090/*
1091 * W25Q adds an optional byte to the standard WRSR opcode. If /CS is
1092 * de-asserted after the first byte, then it acts like a JEDEC-standard
1093 * WRSR command. if /CS is asserted, then the next data byte is written
1094 * into status register 2.
1095 */
1096#define W25Q_WRSR_OUTSIZE 0x03
1097static int w25q_write_status_register_WREN(uint8_t s1, uint8_t s2)
1098{
1099 int result;
1100 struct spi_command cmds[] = {
1101 {
1102 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
1103 .writecnt = JEDEC_WREN_OUTSIZE,
1104 .writearr = (const unsigned char[]){ JEDEC_WREN },
1105 .readcnt = 0,
1106 .readarr = NULL,
1107 }, {
1108 .writecnt = W25Q_WRSR_OUTSIZE,
1109 .writearr = (const unsigned char[]){ JEDEC_WRSR, s1, s2 },
1110 .readcnt = 0,
1111 .readarr = NULL,
1112 }, {
1113 .writecnt = 0,
1114 .writearr = NULL,
1115 .readcnt = 0,
1116 .readarr = NULL,
1117 }};
1118
1119 result = spi_send_multicommand(cmds);
1120 if (result) {
1121 msg_cerr("%s failed during command execution\n",
1122 __func__);
1123 }
1124
1125 /* WRSR performs a self-timed erase before the changes take effect. */
1126 programmer_delay(WRITE_STATUS_REGISTER_DELAY);
1127
1128 return result;
1129}
1130
1131/*
1132 * Set/clear the SRP1 bit in status register 2.
1133 * FIXME: make this more generic if other chips use the same SR2 layout
1134 */
1135static int w25q_set_srp1(const struct flashchip *flash, int enable)
1136{
1137 struct w25q_status sr1;
1138 struct w25q_status_2 sr2;
1139 uint8_t tmp, expected;
1140
1141 tmp = spi_read_status_register();
1142 memcpy(&sr1, &tmp, 1);
1143 tmp = w25q_read_status_register_2();
1144 memcpy(&sr2, &tmp, 1);
1145
1146 msg_cdbg("%s: old status 2: 0x%02x\n", __func__, tmp);
1147
1148 sr2.srp1 = enable ? 1 : 0;
1149
1150 memcpy(&expected, &sr2, 1);
1151 w25q_write_status_register_WREN(*((uint8_t *)&sr1), *((uint8_t *)&sr2));
1152
1153 tmp = w25q_read_status_register_2();
1154 msg_cdbg("%s: new status 2: 0x%02x\n", __func__, tmp);
1155 if ((tmp & MASK_WP2_AREA) != (expected & MASK_WP2_AREA))
1156 return 1;
1157
1158 return 0;
1159}
1160
1161enum wp_mode get_wp_mode(const char *mode_str)
1162{
1163 enum wp_mode wp_mode = WP_MODE_UNKNOWN;
1164
1165 if (!strcasecmp(mode_str, "hardware"))
1166 wp_mode = WP_MODE_HARDWARE;
1167 else if (!strcasecmp(mode_str, "power_cycle"))
1168 wp_mode = WP_MODE_POWER_CYCLE;
1169 else if (!strcasecmp(mode_str, "permanent"))
1170 wp_mode = WP_MODE_PERMANENT;
1171
1172 return wp_mode;
1173}
1174
1175static int w25q_disable_writeprotect(const struct flashchip *flash,
1176 enum wp_mode wp_mode)
1177{
1178 int ret = 1;
David Hendricks1c09f802012-10-03 11:03:48 -07001179 struct w25q_status_2 sr2;
1180 uint8_t tmp;
1181
1182 switch (wp_mode) {
1183 case WP_MODE_HARDWARE:
1184 ret = w25_set_srp0(flash, 0);
1185 break;
1186 case WP_MODE_POWER_CYCLE:
1187 tmp = w25q_read_status_register_2();
1188 memcpy(&sr2, &tmp, 1);
1189 if (sr2.srp1) {
1190 msg_cerr("%s(): must disconnect power to disable "
1191 "write-protection\n", __func__);
1192 } else {
1193 ret = 0;
1194 }
1195 break;
1196 case WP_MODE_PERMANENT:
1197 msg_cerr("%s(): cannot disable permanent write-protection\n",
1198 __func__);
1199 break;
1200 default:
1201 msg_cerr("%s(): invalid mode specified\n", __func__);
1202 break;
1203 }
1204
1205 if (ret)
1206 msg_cerr("%s(): error=%d.\n", __func__, ret);
1207 return ret;
1208}
1209
1210static int w25q_disable_writeprotect_default(const struct flashchip *flash)
1211{
1212 return w25q_disable_writeprotect(flash, WP_MODE_HARDWARE);
1213}
1214
1215static int w25q_enable_writeprotect(const struct flashchip *flash,
1216 enum wp_mode wp_mode)
1217{
1218 int ret = 1;
1219 struct w25q_status sr1;
1220 struct w25q_status_2 sr2;
1221 uint8_t tmp;
1222
1223 switch (wp_mode) {
1224 case WP_MODE_HARDWARE:
1225 if (w25q_disable_writeprotect(flash, WP_MODE_POWER_CYCLE)) {
1226 msg_cerr("%s(): cannot disable power cycle WP mode\n",
1227 __func__);
1228 break;
1229 }
1230
1231 tmp = spi_read_status_register();
1232 memcpy(&sr1, &tmp, 1);
1233 if (sr1.srp0)
1234 ret = 0;
1235 else
1236 ret = w25_set_srp0(flash, 1);
1237
1238 break;
1239 case WP_MODE_POWER_CYCLE:
1240 if (w25q_disable_writeprotect(flash, WP_MODE_HARDWARE)) {
1241 msg_cerr("%s(): cannot disable hardware WP mode\n",
1242 __func__);
1243 break;
1244 }
1245
1246 tmp = w25q_read_status_register_2();
1247 memcpy(&sr2, &tmp, 1);
1248 if (sr2.srp1)
1249 ret = 0;
1250 else
1251 ret = w25q_set_srp1(flash, 1);
1252
1253 break;
1254 case WP_MODE_PERMANENT:
1255 tmp = spi_read_status_register();
1256 memcpy(&sr1, &tmp, 1);
1257 if (sr1.srp0 == 0) {
1258 ret = w25_set_srp0(flash, 1);
1259 if (ret) {
David Hendricksf1bd8802012-10-30 11:37:57 -07001260 msg_perr("%s(): cannot enable SRP0 for "
David Hendricks1c09f802012-10-03 11:03:48 -07001261 "permanent WP\n", __func__);
1262 break;
1263 }
1264 }
1265
1266 tmp = w25q_read_status_register_2();
1267 memcpy(&sr2, &tmp, 1);
1268 if (sr2.srp1 == 0) {
1269 ret = w25q_set_srp1(flash, 1);
1270 if (ret) {
David Hendricksf1bd8802012-10-30 11:37:57 -07001271 msg_perr("%s(): cannot enable SRP1 for "
David Hendricks1c09f802012-10-03 11:03:48 -07001272 "permanent WP\n", __func__);
1273 break;
1274 }
1275 }
1276
1277 break;
David Hendricksf1bd8802012-10-30 11:37:57 -07001278 default:
1279 msg_perr("%s(): invalid mode %d\n", __func__, wp_mode);
1280 break;
David Hendricks1c09f802012-10-03 11:03:48 -07001281 }
1282
1283 if (ret)
1284 msg_cerr("%s(): error=%d.\n", __func__, ret);
1285 return ret;
1286}
1287
1288/* W25P, W25X, and many flash chips from various vendors */
David Hendricksf7924d12010-06-10 21:26:44 -07001289struct wp wp_w25 = {
David Hendricks0f7f5382011-02-11 18:12:31 -08001290 .list_ranges = w25_list_ranges,
David Hendricksf7924d12010-06-10 21:26:44 -07001291 .set_range = w25_set_range,
1292 .enable = w25_enable_writeprotect,
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001293 .disable = w25_disable_writeprotect,
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001294 .wp_status = w25_wp_status,
David Hendricks1c09f802012-10-03 11:03:48 -07001295
1296};
1297
1298/* W25Q series has features such as a second status register and SFDP */
1299struct wp wp_w25q = {
1300 .list_ranges = w25_list_ranges,
1301 .set_range = w25_set_range,
1302 .enable = w25q_enable_writeprotect,
1303 /*
1304 * By default, disable hardware write-protection. We may change
1305 * this later if we want to add fine-grained write-protect disable
1306 * as a command-line option.
1307 */
1308 .disable = w25q_disable_writeprotect_default,
1309 .wp_status = w25q_wp_status,
David Hendricksf7924d12010-06-10 21:26:44 -07001310};
David Hendrickse0512a72014-07-15 20:30:47 -07001311
David Hendricksaf3944a2014-07-28 18:37:40 -07001312struct generic_range gd25q32_cmp0_ranges[] = {
1313 /* none, bp4 and bp3 => don't care */
1314 { 0x00, {0, 0} },
1315 { 0x08, {0, 0} },
1316 { 0x10, {0, 0} },
1317 { 0x18, {0, 0} },
1318
1319 { 0x01, {0x3f0000, 64 * 1024} },
1320 { 0x02, {0x3e0000, 128 * 1024} },
1321 { 0x03, {0x3c0000, 256 * 1024} },
1322 { 0x04, {0x380000, 512 * 1024} },
1323 { 0x05, {0x300000, 1024 * 1024} },
1324 { 0x06, {0x200000, 2048 * 1024} },
1325
1326 { 0x09, {0x000000, 64 * 1024} },
1327 { 0x0a, {0x000000, 128 * 1024} },
1328 { 0x0b, {0x000000, 256 * 1024} },
1329 { 0x0c, {0x000000, 512 * 1024} },
1330 { 0x0d, {0x000000, 1024 * 1024} },
1331 { 0x0e, {0x000000, 2048 * 1024} },
1332
1333 /* all, bp4 and bp3 => don't care */
1334 { 0x07, {0x000000, 4096 * 1024} },
1335 { 0x0f, {0x000000, 4096 * 1024} },
1336 { 0x17, {0x000000, 4096 * 1024} },
1337 { 0x1f, {0x000000, 4096 * 1024} },
1338
1339 { 0x11, {0x3ff000, 4 * 1024} },
1340 { 0x12, {0x3fe000, 8 * 1024} },
1341 { 0x13, {0x3fc000, 16 * 1024} },
1342 { 0x14, {0x3f8000, 32 * 1024} }, /* bp0 => don't care */
1343 { 0x15, {0x3f8000, 32 * 1024} }, /* bp0 => don't care */
1344 { 0x16, {0x3f8000, 32 * 1024} },
1345
1346 { 0x19, {0x000000, 4 * 1024} },
1347 { 0x1a, {0x000000, 8 * 1024} },
1348 { 0x1b, {0x000000, 16 * 1024} },
1349 { 0x1c, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1350 { 0x1d, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1351 { 0x1e, {0x000000, 32 * 1024} },
1352};
1353
1354struct generic_range gd25q32_cmp1_ranges[] = {
1355 /* none, bp4 and bp3 => don't care */
1356 { 0x00, {0, 0} },
1357 { 0x08, {0, 0} },
1358 { 0x10, {0, 0} },
1359 { 0x18, {0, 0} },
1360
1361 { 0x01, {0x000000, 4032 * 1024} },
1362 { 0x02, {0x000000, 3968 * 1024} },
1363 { 0x03, {0x000000, 3840 * 1024} },
1364 { 0x04, {0x000000, 3584 * 1024} },
1365 { 0x05, {0x000000, 3 * 1024 * 1024} },
1366 { 0x06, {0x000000, 2 * 1024 * 1024} },
1367
1368 { 0x09, {0x010000, 4032 * 1024} },
1369 { 0x0a, {0x020000, 3968 * 1024} },
1370 { 0x0b, {0x040000, 3840 * 1024} },
1371 { 0x0c, {0x080000, 3584 * 1024} },
1372 { 0x0d, {0x100000, 3 * 1024 * 1024} },
1373 { 0x0e, {0x200000, 2 * 1024 * 1024} },
1374
1375 /* all, bp4 and bp3 => don't care */
1376 { 0x07, {0x000000, 4096 * 1024} },
1377 { 0x0f, {0x000000, 4096 * 1024} },
1378 { 0x17, {0x000000, 4096 * 1024} },
1379 { 0x1f, {0x000000, 4096 * 1024} },
1380
1381 { 0x11, {0x000000, 4092 * 1024} },
1382 { 0x12, {0x000000, 4088 * 1024} },
1383 { 0x13, {0x000000, 4080 * 1024} },
1384 { 0x14, {0x000000, 4064 * 1024} }, /* bp0 => don't care */
1385 { 0x15, {0x000000, 4064 * 1024} }, /* bp0 => don't care */
1386 { 0x16, {0x000000, 4064 * 1024} },
1387
1388 { 0x19, {0x001000, 4092 * 1024} },
1389 { 0x1a, {0x002000, 4088 * 1024} },
1390 { 0x1b, {0x040000, 4080 * 1024} },
1391 { 0x1c, {0x080000, 4064 * 1024} }, /* bp0 => don't care */
1392 { 0x1d, {0x080000, 4064 * 1024} }, /* bp0 => don't care */
1393 { 0x1e, {0x080000, 4064 * 1024} },
1394};
1395
1396static struct generic_wp gd25q32_wp = {
1397 /* TODO: map second status register */
1398 .sr1 = { .bp0_pos = 2, .bp_bits = 5, .srp_pos = 7 },
1399};
1400
David Hendricks83541d32014-07-15 20:58:21 -07001401#if 0
1402/* FIXME: MX25L6405D has same ID as MX25L6406 */
1403static struct w25q_range mx25l6405d_ranges[] = {
1404 { X, 0, 0, {0, 0} }, /* none */
1405 { X, 0, 0x1, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
1406 { X, 0, 0x2, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
1407 { X, 0, 0x3, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
1408 { X, 0, 0x4, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
1409 { X, 0, 0x5, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
1410 { X, 0, 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
1411 { X, 0, 0x7, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */
1412
1413 { X, 1, 0x0, {0x000000, 8192 * 1024} },
1414 { X, 1, 0x1, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
1415 { X, 1, 0x2, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */
1416 { X, 1, 0x3, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */
1417 { X, 1, 0x4, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */
1418 { X, 1, 0x5, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */
1419 { X, 1, 0x6, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */
1420 { X, 1, 0x7, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */
1421};
1422#endif
1423
1424/* FIXME: MX25L6406 has same ID as MX25L6405D */
1425struct generic_range mx25l6406e_ranges[] = {
1426 { 0, {0, 0} }, /* none */
1427 { 0x1, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */
1428 { 0x2, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */
1429 { 0x3, {0x7a0000, 64 * 8 * 1024} }, /* blocks 120-127 */
1430 { 0x4, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */
1431 { 0x5, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */
1432 { 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
1433
1434 { 0x7, {0x000000, 64 * 128 * 1024} }, /* all */
1435 { 0x8, {0x000000, 64 * 128 * 1024} }, /* all */
1436 { 0x9, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
1437 { 0xa, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */
1438 { 0xb, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */
1439 { 0xc, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */
1440 { 0xd, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */
1441 { 0xe, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */
1442 { 0xf, {0x000000, 64 * 128 * 1024} }, /* all */
1443};
1444
1445static struct generic_wp mx25l6406e_wp = {
1446 .sr1 = { .bp0_pos = 2, .bp_bits = 4, .srp_pos = 7 },
1447 .ranges = &mx25l6406e_ranges[0],
1448};
David Hendrickse0512a72014-07-15 20:30:47 -07001449
1450/* Given a flash chip, this function returns its writeprotect info. */
1451static int generic_range_table(const struct flashchip *flash,
1452 struct generic_wp **wp,
1453 int *num_entries)
1454{
1455 *wp = NULL;
1456 *num_entries = 0;
1457
1458 switch (flash->manufacture_id) {
David Hendricksaf3944a2014-07-28 18:37:40 -07001459 case GIGADEVICE_ID:
1460 switch(flash->model_id) {
1461 case GIGADEVICE_GD25Q32: {
1462 uint8_t sr1 = w25q_read_status_register_2();
1463
1464 *wp = &gd25q32_wp;
1465 if (!(sr1 & (1 << 6))) { /* CMP == 0 */
1466 (*wp)->ranges = &gd25q32_cmp0_ranges[0];
1467 *num_entries = ARRAY_SIZE(gd25q32_cmp0_ranges);
1468 } else { /* CMP == 1 */
1469 (*wp)->ranges = &gd25q32_cmp1_ranges[0];
1470 *num_entries = ARRAY_SIZE(gd25q32_cmp1_ranges);
1471 }
1472
1473 break;
1474 /* TODO(shawnn): add support for other GD parts */
1475 }
1476 default:
1477 msg_cerr("%s() %d: GigaDevice flash chip mismatch"
1478 " (0x%04x), aborting\n", __func__, __LINE__,
1479 flash->model_id);
1480 return -1;
1481 }
1482 break;
David Hendricks83541d32014-07-15 20:58:21 -07001483 case MACRONIX_ID:
1484 switch (flash->model_id) {
1485 case MACRONIX_MX25L6405:
1486 /* FIXME: MX25L64* chips have mixed capabilities and
1487 share IDs */
1488 *wp = &mx25l6406e_wp;
1489 *num_entries = ARRAY_SIZE(mx25l6406e_ranges);
1490 break;
1491 default:
1492 msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)"
1493 ", aborting\n", __func__, __LINE__,
1494 flash->model_id);
1495 return -1;
1496 }
1497 break;
David Hendrickse0512a72014-07-15 20:30:47 -07001498 default:
1499 msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
1500 __func__, flash->manufacture_id);
1501 return -1;
1502 }
1503
1504 return 0;
1505}
1506
1507/* Given a [start, len], this function finds a block protect bit combination
1508 * (if possible) and sets the corresponding bits in "status". Remaining bits
1509 * are preserved. */
1510static int generic_range_to_status(const struct flashchip *flash,
1511 unsigned int start, unsigned int len,
1512 uint8_t *status)
1513{
1514 struct generic_wp *wp;
1515 struct generic_range *r;
1516 int i, range_found = 0, num_entries;
1517 uint8_t bp_mask;
1518
1519 if (generic_range_table(flash, &wp, &num_entries))
1520 return -1;
1521
1522 bp_mask = ((1 << (wp->sr1.bp0_pos + wp->sr1.bp_bits)) - 1) - \
1523 ((1 << wp->sr1.bp0_pos) - 1);
1524
1525 for (i = 0, r = &wp->ranges[0]; i < num_entries; i++, r++) {
1526 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
1527 start, len, r->range.start, r->range.len);
1528 if ((start == r->range.start) && (len == r->range.len)) {
1529 *status &= ~(bp_mask);
1530 *status |= r->bp << (wp->sr1.bp0_pos);
1531 range_found = 1;
1532 break;
1533 }
1534 }
1535
1536 if (!range_found) {
1537 msg_cerr("matching range not found\n");
1538 return -1;
1539 }
1540 return 0;
1541}
1542
1543static int generic_status_to_range(const struct flashchip *flash,
1544 const uint8_t sr1, unsigned int *start, unsigned int *len)
1545{
1546 struct generic_wp *wp;
1547 struct generic_range *r;
1548 int num_entries, wp_en, i, status_found = 0;
1549 uint8_t sr1_bp;
1550
1551 if (generic_range_table(flash, &wp, &num_entries))
1552 return -1;
1553
1554 sr1_bp = (sr1 >> wp->sr1.bp0_pos) & ((1 << wp->sr1.bp_bits) - 1);
1555
1556 for (i = 0, r = &wp->ranges[0]; i < num_entries; i++, r++) {
1557 msg_cspew("comparing 0x%02x 0x%02x\n", sr1_bp, r->bp);
1558 if (sr1_bp == r->bp) {
1559 *start = r->range.start;
1560 *len = r->range.len;
1561 status_found = 1;
1562 break;
1563 }
1564 }
1565
1566 if (!status_found) {
1567 msg_cerr("matching status not found\n");
1568 return -1;
1569 }
1570 return 0;
1571}
1572
1573/* Given a [start, len], this function calls generic_range_to_status() to
1574 * convert it to flash-chip-specific range bits, then sets into status register.
1575 */
1576static int generic_set_range(const struct flashchip *flash,
1577 unsigned int start, unsigned int len)
1578{
1579 uint8_t status, expected;
1580
1581 status = spi_read_status_register();
1582 msg_cdbg("%s: old status: 0x%02x\n", __func__, status);
1583
1584 expected = status; /* preserve non-bp bits */
1585 if (generic_range_to_status(flash, start, len, &expected))
1586 return -1;
1587
1588 spi_write_status_register_WREN(expected);
1589
1590 status = spi_read_status_register();
1591 msg_cdbg("%s: new status: 0x%02x\n", __func__, status);
1592 if (status != expected) {
1593 msg_cerr("expected=0x%02x, but actual=0x%02x.\n",
1594 expected, status);
1595 return 1;
1596 }
1597
1598 return 0;
1599}
1600
1601/* Set/clear the status regsiter write protect bit in SR1. */
1602static int generic_set_srp0(const struct flashchip *flash, int enable)
1603{
1604 uint8_t status, expected;
1605 struct generic_wp *wp;
1606 int num_entries;
1607
1608 if (generic_range_table(flash, &wp, &num_entries))
1609 return -1;
1610
1611 expected = spi_read_status_register();
1612 msg_cdbg("%s: old status: 0x%02x\n", __func__, expected);
1613
1614 if (enable)
1615 expected |= 1 << wp->sr1.srp_pos;
1616 else
1617 expected &= ~(1 << wp->sr1.srp_pos);
1618
1619 spi_write_status_register_WREN(expected);
1620
1621 status = spi_read_status_register();
1622 msg_cdbg("%s: new status: 0x%02x\n", __func__, status);
1623 if (status != expected)
1624 return -1;
1625
1626 return 0;
1627}
1628
1629static int generic_enable_writeprotect(const struct flashchip *flash,
1630 enum wp_mode wp_mode)
1631{
1632 int ret;
1633
1634 switch (wp_mode) {
1635 case WP_MODE_HARDWARE:
1636 ret = generic_set_srp0(flash, 1);
1637 break;
1638 default:
1639 msg_cerr("%s(): unsupported write-protect mode\n", __func__);
1640 return 1;
1641 }
1642
1643 if (ret)
1644 msg_cerr("%s(): error=%d.\n", __func__, ret);
1645 return ret;
1646}
1647
1648static int generic_disable_writeprotect(const struct flashchip *flash)
1649{
1650 int ret;
1651
1652 ret = generic_set_srp0(flash, 0);
1653 if (ret)
1654 msg_cerr("%s(): error=%d.\n", __func__, ret);
1655 return ret;
1656}
1657
1658static int generic_list_ranges(const struct flashchip *flash)
1659{
1660 struct generic_wp *wp;
1661 struct generic_range *r;
1662 int i, num_entries;
1663
1664 if (generic_range_table(flash, &wp, &num_entries))
1665 return -1;
1666
1667 r = &wp->ranges[0];
1668 for (i = 0; i < num_entries; i++) {
1669 msg_cinfo("start: 0x%06x, length: 0x%06x\n",
1670 r->range.start, r->range.len);
1671 r++;
1672 }
1673
1674 return 0;
1675}
1676
1677static int generic_wp_status(const struct flashchip *flash)
1678{
1679 uint8_t sr1;
1680 unsigned int start, len;
1681 int ret = 0;
1682 struct generic_wp *wp;
1683 struct generic_range *g;
1684 int num_entries, wp_en;
1685
1686 if (generic_range_table(flash, &wp, &num_entries))
1687 return -1;
1688
1689 sr1 = spi_read_status_register();
1690 wp_en = (sr1 >> wp->sr1.srp_pos) & 1;
1691
1692 msg_cinfo("WP: status: 0x%04x\n", sr1);
1693 msg_cinfo("WP: status.srp0: %x\n", wp_en);
1694 /* FIXME: SRP1 is not really generic, but we probably should print
1695 * it anyway to have consistent output. #legacycruft */
1696 msg_cinfo("WP: status.srp1: %x\n", 0);
1697 msg_cinfo("WP: write protect is %s.\n",
1698 wp_en ? "enabled" : "disabled");
1699
1700 msg_cinfo("WP: write protect range: ");
1701 if (generic_status_to_range(flash, sr1, &start, &len)) {
1702 msg_cinfo("(cannot resolve the range)\n");
1703 ret = -1;
1704 } else {
1705 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
1706 }
1707
1708 return ret;
1709}
1710
1711struct wp wp_generic = {
1712 .list_ranges = generic_list_ranges,
1713 .set_range = generic_set_range,
1714 .enable = generic_enable_writeprotect,
1715 .disable = generic_disable_writeprotect,
1716 .wp_status = generic_wp_status,
1717};