blob: 1b7fd722e97998fd223b8e1360a61c35f01e6054 [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
David Hendricks1c09f802012-10-03 11:03:48 -070030/*
David Hendricksf7924d12010-06-10 21:26:44 -070031 * The following procedures rely on look-up tables to match the user-specified
32 * range with the chip's supported ranges. This turned out to be the most
33 * elegant approach since diferent flash chips use different levels of
34 * granularity and methods to determine protected ranges. In other words,
David Hendrickse0512a72014-07-15 20:30:47 -070035 * be stupid and simple since clever arithmetic will not work for many chips.
David Hendricksf7924d12010-06-10 21:26:44 -070036 */
37
38struct wp_range {
39 unsigned int start; /* starting address */
40 unsigned int len; /* len */
41};
42
43enum bit_state {
44 OFF = 0,
45 ON = 1,
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +080046 X = -1 /* don't care. Must be bigger than max # of bp. */
David Hendricksf7924d12010-06-10 21:26:44 -070047};
48
David Hendrickse0512a72014-07-15 20:30:47 -070049/*
50 * Generic write-protection schema for 25-series SPI flash chips. This assumes
51 * there is a status register that contains one or more consecutive bits which
52 * determine which address range is protected.
53 */
54
55struct status_register_layout {
56 int bp0_pos; /* position of BP0 */
57 int bp_bits; /* number of block protect bits */
58 int srp_pos; /* position of status register protect enable bit */
59};
60
61struct generic_range {
David Hendricks148a4bf2015-03-13 21:02:42 -070062 struct generic_modifier_bits m;
David Hendrickse0512a72014-07-15 20:30:47 -070063 unsigned int bp; /* block protect bitfield */
64 struct wp_range range;
65};
66
67struct generic_wp {
68 struct status_register_layout sr1; /* status register 1 */
69 struct generic_range *ranges;
David Hendricks148a4bf2015-03-13 21:02:42 -070070
71 /*
72 * Some chips store modifier bits in one or more special control
73 * registers instead of the status register like many older SPI NOR
74 * flash chips did. get_modifier_bits() and set_modifier_bits() will do
75 * any chip-specific operations necessary to get/set these bit values.
76 */
77 int (*get_modifier_bits)(const struct flashchip *flash,
78 struct generic_modifier_bits *m);
79 int (*set_modifier_bits)(const struct flashchip *flash,
80 struct generic_modifier_bits *m);
David Hendrickse0512a72014-07-15 20:30:47 -070081};
82
83/*
84 * The following ranges and functions are useful for representing Winbond-
85 * style writeprotect schema in which there are typically 5 bits of
86 * relevant information stored in status register 1:
87 * sec: This bit indicates the units (sectors vs. blocks)
88 * tb: The top-bottom bit indicates if the affected range is at the top of
89 * the flash memory's address space or at the bottom.
90 * bp[2:0]: The number of affected sectors/blocks.
91 */
David Hendricksf7924d12010-06-10 21:26:44 -070092struct w25q_range {
93 enum bit_state sec; /* if 1, bp[2:0] describe sectors */
94 enum bit_state tb; /* top/bottom select */
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +080095 int bp; /* block protect bitfield */
David Hendricksf7924d12010-06-10 21:26:44 -070096 struct wp_range range;
97};
98
David Hendrickse0512a72014-07-15 20:30:47 -070099/*
100 * Mask to extract write-protect enable and range bits
101 * Status register 1:
102 * SRP0: bit 7
103 * range(BP2-BP0): bit 4-2
104 * Status register 2:
105 * SRP1: bit 1
106 */
107#define MASK_WP_AREA (0x9C)
108#define MASK_WP2_AREA (0x01)
109
David Hendricks57566ed2010-08-16 18:24:45 -0700110struct w25q_range en25f40_ranges[] = {
111 { X, X, 0, {0, 0} }, /* none */
112 { 0, 0, 0x1, {0x000000, 504 * 1024} },
113 { 0, 0, 0x2, {0x000000, 496 * 1024} },
114 { 0, 0, 0x3, {0x000000, 480 * 1024} },
115 { 0, 0, 0x4, {0x000000, 448 * 1024} },
116 { 0, 0, 0x5, {0x000000, 384 * 1024} },
117 { 0, 0, 0x6, {0x000000, 256 * 1024} },
118 { 0, 0, 0x7, {0x000000, 512 * 1024} },
119};
120
David Hendrickse185bf22011-05-24 15:34:18 -0700121struct w25q_range en25q40_ranges[] = {
122 { 0, 0, 0, {0, 0} }, /* none */
123 { 0, 0, 0x1, {0x000000, 504 * 1024} },
124 { 0, 0, 0x2, {0x000000, 496 * 1024} },
125 { 0, 0, 0x3, {0x000000, 480 * 1024} },
126
127 { 0, 1, 0x0, {0x000000, 448 * 1024} },
128 { 0, 1, 0x1, {0x000000, 384 * 1024} },
129 { 0, 1, 0x2, {0x000000, 256 * 1024} },
130 { 0, 1, 0x3, {0x000000, 512 * 1024} },
131};
132
133struct w25q_range en25q80_ranges[] = {
134 { 0, 0, 0, {0, 0} }, /* none */
135 { 0, 0, 0x1, {0x000000, 1016 * 1024} },
136 { 0, 0, 0x2, {0x000000, 1008 * 1024} },
137 { 0, 0, 0x3, {0x000000, 992 * 1024} },
138 { 0, 0, 0x4, {0x000000, 960 * 1024} },
139 { 0, 0, 0x5, {0x000000, 896 * 1024} },
140 { 0, 0, 0x6, {0x000000, 768 * 1024} },
141 { 0, 0, 0x7, {0x000000, 1024 * 1024} },
142};
143
144struct w25q_range en25q32_ranges[] = {
145 { 0, 0, 0, {0, 0} }, /* none */
146 { 0, 0, 0x1, {0x000000, 4032 * 1024} },
147 { 0, 0, 0x2, {0x000000, 3968 * 1024} },
148 { 0, 0, 0x3, {0x000000, 3840 * 1024} },
149 { 0, 0, 0x4, {0x000000, 3584 * 1024} },
150 { 0, 0, 0x5, {0x000000, 3072 * 1024} },
151 { 0, 0, 0x6, {0x000000, 2048 * 1024} },
152 { 0, 0, 0x7, {0x000000, 4096 * 1024} },
153
154 { 0, 1, 0, {0, 0} }, /* none */
155 { 0, 1, 0x1, {0x010000, 4032 * 1024} },
156 { 0, 1, 0x2, {0x020000, 3968 * 1024} },
157 { 0, 1, 0x3, {0x040000, 3840 * 1024} },
158 { 0, 1, 0x4, {0x080000, 3584 * 1024} },
159 { 0, 1, 0x5, {0x100000, 3072 * 1024} },
160 { 0, 1, 0x6, {0x200000, 2048 * 1024} },
161 { 0, 1, 0x7, {0x000000, 4096 * 1024} },
162};
163
164struct w25q_range en25q64_ranges[] = {
165 { 0, 0, 0, {0, 0} }, /* none */
166 { 0, 0, 0x1, {0x000000, 8128 * 1024} },
167 { 0, 0, 0x2, {0x000000, 8064 * 1024} },
168 { 0, 0, 0x3, {0x000000, 7936 * 1024} },
169 { 0, 0, 0x4, {0x000000, 7680 * 1024} },
170 { 0, 0, 0x5, {0x000000, 7168 * 1024} },
171 { 0, 0, 0x6, {0x000000, 6144 * 1024} },
172 { 0, 0, 0x7, {0x000000, 8192 * 1024} },
173
174 { 0, 1, 0, {0, 0} }, /* none */
175 { 0, 1, 0x1, {0x010000, 8128 * 1024} },
176 { 0, 1, 0x2, {0x020000, 8064 * 1024} },
177 { 0, 1, 0x3, {0x040000, 7936 * 1024} },
178 { 0, 1, 0x4, {0x080000, 7680 * 1024} },
179 { 0, 1, 0x5, {0x100000, 7168 * 1024} },
180 { 0, 1, 0x6, {0x200000, 6144 * 1024} },
181 { 0, 1, 0x7, {0x000000, 8192 * 1024} },
182};
183
184struct w25q_range en25q128_ranges[] = {
185 { 0, 0, 0, {0, 0} }, /* none */
186 { 0, 0, 0x1, {0x000000, 16320 * 1024} },
187 { 0, 0, 0x2, {0x000000, 16256 * 1024} },
188 { 0, 0, 0x3, {0x000000, 16128 * 1024} },
189 { 0, 0, 0x4, {0x000000, 15872 * 1024} },
190 { 0, 0, 0x5, {0x000000, 15360 * 1024} },
191 { 0, 0, 0x6, {0x000000, 14336 * 1024} },
192 { 0, 0, 0x7, {0x000000, 16384 * 1024} },
193
194 { 0, 1, 0, {0, 0} }, /* none */
195 { 0, 1, 0x1, {0x010000, 16320 * 1024} },
196 { 0, 1, 0x2, {0x020000, 16256 * 1024} },
197 { 0, 1, 0x3, {0x040000, 16128 * 1024} },
198 { 0, 1, 0x4, {0x080000, 15872 * 1024} },
199 { 0, 1, 0x5, {0x100000, 15360 * 1024} },
200 { 0, 1, 0x6, {0x200000, 14336 * 1024} },
201 { 0, 1, 0x7, {0x000000, 16384 * 1024} },
202};
203
Marc Jonesb2f90022014-04-29 17:37:23 -0600204struct w25q_range en25s64_ranges[] = {
205 { 0, 0, 0, {0, 0} }, /* none */
206 { 0, 0, 0x1, {0x000000, 8064 * 1024} },
207 { 0, 0, 0x2, {0x000000, 7936 * 1024} },
208 { 0, 0, 0x3, {0x000000, 7680 * 1024} },
209 { 0, 0, 0x4, {0x000000, 7168 * 1024} },
210 { 0, 0, 0x5, {0x000000, 6144 * 1024} },
211 { 0, 0, 0x6, {0x000000, 4096 * 1024} },
212 { 0, 0, 0x7, {0x000000, 8192 * 1024} },
213
214 { 0, 1, 0, {0, 0} }, /* none */
215 { 0, 1, 0x1, {0x7e0000, 128 * 1024} },
216 { 0, 1, 0x2, {0x7c0000, 256 * 1024} },
217 { 0, 1, 0x3, {0x780000, 512 * 1024} },
218 { 0, 1, 0x4, {0x700000, 1024 * 1024} },
219 { 0, 1, 0x5, {0x600000, 2048 * 1024} },
220 { 0, 1, 0x6, {0x400000, 4096 * 1024} },
221 { 0, 1, 0x7, {0x000000, 8192 * 1024} },
222};
223
David Hendricksf8f00c72011-02-01 12:39:46 -0800224/* mx25l1005 ranges also work for the mx25l1005c */
225static struct w25q_range mx25l1005_ranges[] = {
226 { X, X, 0, {0, 0} }, /* none */
227 { X, X, 0x1, {0x010000, 64 * 1024} },
228 { X, X, 0x2, {0x000000, 128 * 1024} },
229 { X, X, 0x3, {0x000000, 128 * 1024} },
230};
231
232static struct w25q_range mx25l2005_ranges[] = {
233 { X, X, 0, {0, 0} }, /* none */
234 { X, X, 0x1, {0x030000, 64 * 1024} },
235 { X, X, 0x2, {0x020000, 128 * 1024} },
236 { X, X, 0x3, {0x000000, 256 * 1024} },
237};
238
239static struct w25q_range mx25l4005_ranges[] = {
240 { X, X, 0, {0, 0} }, /* none */
241 { X, X, 0x1, {0x070000, 64 * 1 * 1024} }, /* block 7 */
242 { X, X, 0x2, {0x060000, 64 * 2 * 1024} }, /* blocks 6-7 */
243 { X, X, 0x3, {0x040000, 64 * 4 * 1024} }, /* blocks 4-7 */
244 { X, X, 0x4, {0x000000, 512 * 1024} },
245 { X, X, 0x5, {0x000000, 512 * 1024} },
246 { X, X, 0x6, {0x000000, 512 * 1024} },
247 { X, X, 0x7, {0x000000, 512 * 1024} },
248};
249
250static struct w25q_range mx25l8005_ranges[] = {
251 { X, X, 0, {0, 0} }, /* none */
252 { X, X, 0x1, {0x0f0000, 64 * 1 * 1024} }, /* block 15 */
253 { X, X, 0x2, {0x0e0000, 64 * 2 * 1024} }, /* blocks 14-15 */
254 { X, X, 0x3, {0x0c0000, 64 * 4 * 1024} }, /* blocks 12-15 */
255 { X, X, 0x4, {0x080000, 64 * 8 * 1024} }, /* blocks 8-15 */
256 { X, X, 0x5, {0x000000, 1024 * 1024} },
257 { X, X, 0x6, {0x000000, 1024 * 1024} },
258 { X, X, 0x7, {0x000000, 1024 * 1024} },
259};
260
261#if 0
262/* FIXME: mx25l1605 has the same IDs as the mx25l1605d */
263static struct w25q_range mx25l1605_ranges[] = {
264 { X, X, 0, {0, 0} }, /* none */
265 { X, X, 0x1, {0x1f0000, 64 * 1024} }, /* block 31 */
266 { X, X, 0x2, {0x1e0000, 128 * 1024} }, /* blocks 30-31 */
267 { X, X, 0x3, {0x1c0000, 256 * 1024} }, /* blocks 28-31 */
268 { X, X, 0x4, {0x180000, 512 * 1024} }, /* blocks 24-31 */
269 { X, X, 0x4, {0x100000, 1024 * 1024} }, /* blocks 16-31 */
270 { X, X, 0x6, {0x000000, 2048 * 1024} },
271 { X, X, 0x7, {0x000000, 2048 * 1024} },
272};
273#endif
274
275#if 0
276/* FIXME: mx25l6405 has the same IDs as the mx25l6405d */
277static struct w25q_range mx25l6405_ranges[] = {
278 { X, 0, 0, {0, 0} }, /* none */
279 { X, 0, 0x1, {0x7f0000, 64 * 1 * 1024} }, /* block 127 */
280 { X, 0, 0x2, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */
281 { X, 0, 0x3, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */
282 { X, 0, 0x4, {0x780000, 64 * 8 * 1024} }, /* blocks 120-127 */
283 { X, 0, 0x5, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */
284 { X, 0, 0x6, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */
285 { X, 0, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
286
287 { X, 1, 0x0, {0x000000, 8192 * 1024} },
288 { X, 1, 0x1, {0x000000, 8192 * 1024} },
289 { X, 1, 0x2, {0x000000, 8192 * 1024} },
290 { X, 1, 0x3, {0x000000, 8192 * 1024} },
291 { X, 1, 0x4, {0x000000, 8192 * 1024} },
292 { X, 1, 0x5, {0x000000, 8192 * 1024} },
293 { X, 1, 0x6, {0x000000, 8192 * 1024} },
294 { X, 1, 0x7, {0x000000, 8192 * 1024} },
295};
296#endif
297
298static struct w25q_range mx25l1605d_ranges[] = {
299 { X, 0, 0, {0, 0} }, /* none */
300 { X, 0, 0x1, {0x1f0000, 64 * 1 * 1024} }, /* block 31 */
301 { X, 0, 0x2, {0x1e0000, 64 * 2 * 1024} }, /* blocks 30-31 */
302 { X, 0, 0x3, {0x1c0000, 64 * 4 * 1024} }, /* blocks 28-31 */
303 { X, 0, 0x4, {0x180000, 64 * 8 * 1024} }, /* blocks 24-31 */
304 { X, 0, 0x5, {0x100000, 64 * 16 * 1024} }, /* blocks 16-31 */
305 { X, 0, 0x6, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
306 { X, 0, 0x7, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
307
308 { X, 1, 0x0, {0x000000, 2048 * 1024} },
309 { X, 1, 0x1, {0x000000, 2048 * 1024} },
310 { X, 1, 0x2, {0x000000, 64 * 16 * 1024} }, /* blocks 0-15 */
311 { X, 1, 0x3, {0x000000, 64 * 24 * 1024} }, /* blocks 0-23 */
312 { X, 1, 0x4, {0x000000, 64 * 28 * 1024} }, /* blocks 0-27 */
313 { X, 1, 0x5, {0x000000, 64 * 30 * 1024} }, /* blocks 0-29 */
314 { X, 1, 0x6, {0x000000, 64 * 31 * 1024} }, /* blocks 0-30 */
315 { X, 1, 0x7, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
316};
317
318/* FIXME: Is there an mx25l3205 (without a trailing letter)? */
David Hendricksac72e362010-08-16 18:20:03 -0700319static struct w25q_range mx25l3205d_ranges[] = {
320 { X, 0, 0, {0, 0} }, /* none */
321 { X, 0, 0x1, {0x3f0000, 64 * 1024} },
322 { X, 0, 0x2, {0x3e0000, 128 * 1024} },
323 { X, 0, 0x3, {0x3c0000, 256 * 1024} },
324 { X, 0, 0x4, {0x380000, 512 * 1024} },
325 { X, 0, 0x5, {0x300000, 1024 * 1024} },
326 { X, 0, 0x6, {0x200000, 2048 * 1024} },
327 { X, 0, 0x7, {0x000000, 4096 * 1024} },
328
329 { X, 1, 0x0, {0x000000, 4096 * 1024} },
330 { X, 1, 0x1, {0x000000, 2048 * 1024} },
331 { X, 1, 0x2, {0x000000, 3072 * 1024} },
332 { X, 1, 0x3, {0x000000, 3584 * 1024} },
333 { X, 1, 0x4, {0x000000, 3840 * 1024} },
334 { X, 1, 0x5, {0x000000, 3968 * 1024} },
335 { X, 1, 0x6, {0x000000, 4032 * 1024} },
336 { X, 1, 0x7, {0x000000, 4096 * 1024} },
337};
338
Vincent Palatin87e092a2013-02-28 15:46:14 -0800339static struct w25q_range mx25u3235e_ranges[] = {
340 { X, 0, 0, {0, 0} }, /* none */
341 { 0, 0, 0x1, {0x3f0000, 64 * 1024} },
342 { 0, 0, 0x2, {0x3e0000, 128 * 1024} },
343 { 0, 0, 0x3, {0x3c0000, 256 * 1024} },
344 { 0, 0, 0x4, {0x380000, 512 * 1024} },
345 { 0, 0, 0x5, {0x300000, 1024 * 1024} },
346 { 0, 0, 0x6, {0x200000, 2048 * 1024} },
347 { 0, 0, 0x7, {0x000000, 4096 * 1024} },
348
349 { 0, 1, 0x0, {0x000000, 4096 * 1024} },
350 { 0, 1, 0x1, {0x000000, 2048 * 1024} },
351 { 0, 1, 0x2, {0x000000, 3072 * 1024} },
352 { 0, 1, 0x3, {0x000000, 3584 * 1024} },
353 { 0, 1, 0x4, {0x000000, 3840 * 1024} },
354 { 0, 1, 0x5, {0x000000, 3968 * 1024} },
355 { 0, 1, 0x6, {0x000000, 4032 * 1024} },
356 { 0, 1, 0x7, {0x000000, 4096 * 1024} },
357};
358
Jongpil66a96492014-08-14 17:59:06 +0900359static struct w25q_range mx25u6435e_ranges[] = {
360 { X, 0, 0, {0, 0} }, /* none */
361 { 0, 0, 0x1, {0x7f0000, 1 * 64 * 1024} }, /* block 127 */
362 { 0, 0, 0x2, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
363 { 0, 0, 0x3, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
364 { 0, 0, 0x4, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
365 { 0, 0, 0x5, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
366 { 0, 0, 0x6, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
367 { 0, 0, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
368
369 { 0, 1, 0x0, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
370 { 0, 1, 0x1, {0x000000, 96 * 64 * 1024} }, /* blocks 0-95 */
371 { 0, 1, 0x2, {0x000000, 112 * 64 * 1024} }, /* blocks 0-111 */
372 { 0, 1, 0x3, {0x000000, 120 * 64 * 1024} }, /* blocks 0-119 */
373 { 0, 1, 0x4, {0x000000, 124 * 64 * 1024} }, /* blocks 0-123 */
374 { 0, 1, 0x5, {0x000000, 126 * 64 * 1024} }, /* blocks 0-125 */
375 { 0, 1, 0x6, {0x000000, 127 * 64 * 1024} }, /* blocks 0-126 */
376 { 0, 1, 0x7, {0x000000, 128 * 64 * 1024} }, /* blocks 0-127 */
377};
378
David Hendricksbfa624b2012-07-24 12:47:59 -0700379static struct w25q_range n25q064_ranges[] = {
David Hendricksfe9123b2015-04-21 13:18:31 -0700380 /*
381 * Note: For N25Q064, sec (usually in bit position 6) is called BP3
382 * (block protect bit 3). It is only useful when all blocks are to
383 * be write-protected.
384 */
David Hendricks42a549a2015-04-22 11:25:07 -0700385 { 0, 0, 0, {0, 0} }, /* none */
David Hendricksbfa624b2012-07-24 12:47:59 -0700386
387 { 0, 0, 0x1, {0x7f0000, 64 * 1024} }, /* block 127 */
388 { 0, 0, 0x2, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
389 { 0, 0, 0x3, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
390 { 0, 0, 0x4, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
391 { 0, 0, 0x5, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
392 { 0, 0, 0x6, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
393 { 0, 0, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
394
David Hendricksfe9123b2015-04-21 13:18:31 -0700395 { 0, 1, 0x1, {0x000000, 64 * 1024} }, /* block 0 */
396 { 0, 1, 0x2, {0x000000, 2 * 64 * 1024} }, /* blocks 0-1 */
397 { 0, 1, 0x3, {0x000000, 4 * 64 * 1024} }, /* blocks 0-3 */
398 { 0, 1, 0x4, {0x000000, 8 * 64 * 1024} }, /* blocks 0-7 */
399 { 0, 1, 0x5, {0x000000, 16 * 64 * 1024} }, /* blocks 0-15 */
400 { 0, 1, 0x6, {0x000000, 32 * 64 * 1024} }, /* blocks 0-31 */
401 { 0, 1, 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
David Hendricksbfa624b2012-07-24 12:47:59 -0700402
403 { X, 1, 0x0, {0x000000, 128 * 64 * 1024} }, /* all */
404 { X, 1, 0x1, {0x000000, 128 * 64 * 1024} }, /* all */
405 { X, 1, 0x2, {0x000000, 128 * 64 * 1024} }, /* all */
406 { X, 1, 0x3, {0x000000, 128 * 64 * 1024} }, /* all */
407 { X, 1, 0x4, {0x000000, 128 * 64 * 1024} }, /* all */
408 { X, 1, 0x5, {0x000000, 128 * 64 * 1024} }, /* all */
409 { X, 1, 0x6, {0x000000, 128 * 64 * 1024} }, /* all */
410 { X, 1, 0x7, {0x000000, 128 * 64 * 1024} }, /* all */
411};
412
David Hendricksf7924d12010-06-10 21:26:44 -0700413static struct w25q_range w25q16_ranges[] = {
414 { X, X, 0, {0, 0} }, /* none */
415 { 0, 0, 0x1, {0x1f0000, 64 * 1024} },
416 { 0, 0, 0x2, {0x1e0000, 128 * 1024} },
417 { 0, 0, 0x3, {0x1c0000, 256 * 1024} },
418 { 0, 0, 0x4, {0x180000, 512 * 1024} },
419 { 0, 0, 0x5, {0x100000, 1024 * 1024} },
420
421 { 0, 1, 0x1, {0x000000, 64 * 1024} },
422 { 0, 1, 0x2, {0x000000, 128 * 1024} },
423 { 0, 1, 0x3, {0x000000, 256 * 1024} },
424 { 0, 1, 0x4, {0x000000, 512 * 1024} },
425 { 0, 1, 0x5, {0x000000, 1024 * 1024} },
426 { X, X, 0x6, {0x000000, 2048 * 1024} },
427 { X, X, 0x7, {0x000000, 2048 * 1024} },
428
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
442static struct w25q_range w25q32_ranges[] = {
443 { X, X, 0, {0, 0} }, /* none */
444 { 0, 0, 0x1, {0x3f0000, 64 * 1024} },
445 { 0, 0, 0x2, {0x3e0000, 128 * 1024} },
446 { 0, 0, 0x3, {0x3c0000, 256 * 1024} },
447 { 0, 0, 0x4, {0x380000, 512 * 1024} },
448 { 0, 0, 0x5, {0x300000, 1024 * 1024} },
David Hendricks05653ff2010-06-15 16:05:12 -0700449 { 0, 0, 0x6, {0x200000, 2048 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700450
451 { 0, 1, 0x1, {0x000000, 64 * 1024} },
452 { 0, 1, 0x2, {0x000000, 128 * 1024} },
453 { 0, 1, 0x3, {0x000000, 256 * 1024} },
454 { 0, 1, 0x4, {0x000000, 512 * 1024} },
455 { 0, 1, 0x5, {0x000000, 1024 * 1024} },
456 { 0, 1, 0x6, {0x000000, 2048 * 1024} },
457 { X, X, 0x7, {0x000000, 4096 * 1024} },
458
459 { 1, 0, 0x1, {0x3ff000, 4 * 1024} },
460 { 1, 0, 0x2, {0x3fe000, 8 * 1024} },
461 { 1, 0, 0x3, {0x3fc000, 16 * 1024} },
462 { 1, 0, 0x4, {0x3f8000, 32 * 1024} },
463 { 1, 0, 0x5, {0x3f8000, 32 * 1024} },
464
465 { 1, 1, 0x1, {0x000000, 4 * 1024} },
466 { 1, 1, 0x2, {0x000000, 8 * 1024} },
467 { 1, 1, 0x3, {0x000000, 16 * 1024} },
468 { 1, 1, 0x4, {0x000000, 32 * 1024} },
469 { 1, 1, 0x5, {0x000000, 32 * 1024} },
470};
471
472static struct w25q_range w25q80_ranges[] = {
473 { X, X, 0, {0, 0} }, /* none */
474 { 0, 0, 0x1, {0x0f0000, 64 * 1024} },
475 { 0, 0, 0x2, {0x0e0000, 128 * 1024} },
476 { 0, 0, 0x3, {0x0c0000, 256 * 1024} },
477 { 0, 0, 0x4, {0x080000, 512 * 1024} },
478
479 { 0, 1, 0x1, {0x000000, 64 * 1024} },
480 { 0, 1, 0x2, {0x000000, 128 * 1024} },
481 { 0, 1, 0x3, {0x000000, 256 * 1024} },
482 { 0, 1, 0x4, {0x000000, 512 * 1024} },
David Hendricks05653ff2010-06-15 16:05:12 -0700483 { X, X, 0x6, {0x000000, 1024 * 1024} },
484 { X, X, 0x7, {0x000000, 1024 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700485
486 { 1, 0, 0x1, {0x1ff000, 4 * 1024} },
487 { 1, 0, 0x2, {0x1fe000, 8 * 1024} },
488 { 1, 0, 0x3, {0x1fc000, 16 * 1024} },
489 { 1, 0, 0x4, {0x1f8000, 32 * 1024} },
490 { 1, 0, 0x5, {0x1f8000, 32 * 1024} },
491
492 { 1, 1, 0x1, {0x000000, 4 * 1024} },
493 { 1, 1, 0x2, {0x000000, 8 * 1024} },
494 { 1, 1, 0x3, {0x000000, 16 * 1024} },
495 { 1, 1, 0x4, {0x000000, 32 * 1024} },
496 { 1, 1, 0x5, {0x000000, 32 * 1024} },
497};
498
David Hendricks2c4a76c2010-06-28 14:00:43 -0700499static struct w25q_range w25q64_ranges[] = {
500 { X, X, 0, {0, 0} }, /* none */
501
502 { 0, 0, 0x1, {0x7e0000, 128 * 1024} },
503 { 0, 0, 0x2, {0x7c0000, 256 * 1024} },
504 { 0, 0, 0x3, {0x780000, 512 * 1024} },
505 { 0, 0, 0x4, {0x700000, 1024 * 1024} },
506 { 0, 0, 0x5, {0x600000, 2048 * 1024} },
507 { 0, 0, 0x6, {0x400000, 4096 * 1024} },
508
509 { 0, 1, 0x1, {0x000000, 128 * 1024} },
510 { 0, 1, 0x2, {0x000000, 256 * 1024} },
511 { 0, 1, 0x3, {0x000000, 512 * 1024} },
512 { 0, 1, 0x4, {0x000000, 1024 * 1024} },
513 { 0, 1, 0x5, {0x000000, 2048 * 1024} },
514 { 0, 1, 0x6, {0x000000, 4096 * 1024} },
515 { X, X, 0x7, {0x000000, 8192 * 1024} },
516
517 { 1, 0, 0x1, {0x7ff000, 4 * 1024} },
518 { 1, 0, 0x2, {0x7fe000, 8 * 1024} },
519 { 1, 0, 0x3, {0x7fc000, 16 * 1024} },
520 { 1, 0, 0x4, {0x7f8000, 32 * 1024} },
521 { 1, 0, 0x5, {0x7f8000, 32 * 1024} },
522
523 { 1, 1, 0x1, {0x000000, 4 * 1024} },
524 { 1, 1, 0x2, {0x000000, 8 * 1024} },
525 { 1, 1, 0x3, {0x000000, 16 * 1024} },
526 { 1, 1, 0x4, {0x000000, 32 * 1024} },
527 { 1, 1, 0x5, {0x000000, 32 * 1024} },
528};
529
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530530struct w25q_range w25r128_ranges[] = {
531 { X, X, 0, {0, 0} }, /* none */
532
533 { 0, 0, 0x1, {0xfc0000, 256 * 1024} },
534 { 0, 0, 0x2, {0xf80000, 512 * 1024} },
535 { 0, 0, 0x3, {0xf00000, 1024 * 1024} },
536 { 0, 0, 0x4, {0xe00000, 2048 * 1024} },
537 { 0, 0, 0x5, {0xc00000, 4096 * 1024} },
538 { 0, 0, 0x6, {0x800000, 8192 * 1024} },
539
540 { 0, 1, 0x1, {0x000000, 256 * 1024} },
541 { 0, 1, 0x2, {0x000000, 512 * 1024} },
542 { 0, 1, 0x3, {0x000000, 1024 * 1024} },
543 { 0, 1, 0x4, {0x000000, 2048 * 1024} },
544 { 0, 1, 0x5, {0x000000, 4096 * 1024} },
545 { 0, 1, 0x6, {0x000000, 8192 * 1024} },
546 { X, X, 0x7, {0x000000, 16384 * 1024} },
547
548 { 1, 0, 0x1, {0xfff000, 4 * 1024} },
549 { 1, 0, 0x2, {0xffe000, 8 * 1024} },
550 { 1, 0, 0x3, {0xffc000, 16 * 1024} },
551 { 1, 0, 0x4, {0xff8000, 32 * 1024} },
552 { 1, 0, 0x5, {0xff8000, 32 * 1024} },
553
554 { 1, 1, 0x1, {0x000000, 4 * 1024} },
555 { 1, 1, 0x2, {0x000000, 8 * 1024} },
556 { 1, 1, 0x3, {0x000000, 16 * 1024} },
557 { 1, 1, 0x4, {0x000000, 32 * 1024} },
558 { 1, 1, 0x5, {0x000000, 32 * 1024} },
559};
560
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800561struct w25q_range w25x10_ranges[] = {
562 { X, X, 0, {0, 0} }, /* none */
563 { 0, 0, 0x1, {0x010000, 64 * 1024} },
564 { 0, 1, 0x1, {0x000000, 64 * 1024} },
565 { X, X, 0x2, {0x000000, 128 * 1024} },
566 { X, X, 0x3, {0x000000, 128 * 1024} },
567};
568
569struct w25q_range w25x20_ranges[] = {
570 { X, X, 0, {0, 0} }, /* none */
571 { 0, 0, 0x1, {0x030000, 64 * 1024} },
572 { 0, 0, 0x2, {0x020000, 128 * 1024} },
573 { 0, 1, 0x1, {0x000000, 64 * 1024} },
574 { 0, 1, 0x2, {0x000000, 128 * 1024} },
575 { 0, X, 0x3, {0x000000, 256 * 1024} },
576};
577
David Hendricks470ca952010-08-13 14:01:53 -0700578struct w25q_range w25x40_ranges[] = {
579 { X, X, 0, {0, 0} }, /* none */
580 { 0, 0, 0x1, {0x070000, 64 * 1024} },
581 { 0, 0, 0x2, {0x060000, 128 * 1024} },
582 { 0, 0, 0x3, {0x040000, 256 * 1024} },
583 { 0, 1, 0x1, {0x000000, 64 * 1024} },
584 { 0, 1, 0x2, {0x000000, 128 * 1024} },
585 { 0, 1, 0x3, {0x000000, 256 * 1024} },
586 { 0, X, 0x4, {0x000000, 512 * 1024} },
587};
588
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800589struct w25q_range w25x80_ranges[] = {
590 { X, X, 0, {0, 0} }, /* none */
591 { 0, 0, 0x1, {0x0F0000, 64 * 1024} },
592 { 0, 0, 0x2, {0x0E0000, 128 * 1024} },
593 { 0, 0, 0x3, {0x0C0000, 256 * 1024} },
594 { 0, 0, 0x4, {0x080000, 512 * 1024} },
595 { 0, 1, 0x1, {0x000000, 64 * 1024} },
596 { 0, 1, 0x2, {0x000000, 128 * 1024} },
597 { 0, 1, 0x3, {0x000000, 256 * 1024} },
598 { 0, 1, 0x4, {0x000000, 512 * 1024} },
599 { 0, X, 0x5, {0x000000, 1024 * 1024} },
600 { 0, X, 0x6, {0x000000, 1024 * 1024} },
601 { 0, X, 0x7, {0x000000, 1024 * 1024} },
602};
603
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700604static struct w25q_range gd25q64_ranges[] = {
605 { X, X, 0, {0, 0} }, /* none */
606 { 0, 0, 0x1, {0x7e0000, 128 * 1024} },
607 { 0, 0, 0x2, {0x7c0000, 256 * 1024} },
608 { 0, 0, 0x3, {0x780000, 512 * 1024} },
609 { 0, 0, 0x4, {0x700000, 1024 * 1024} },
610 { 0, 0, 0x5, {0x600000, 2048 * 1024} },
611 { 0, 0, 0x6, {0x400000, 4096 * 1024} },
612
613 { 0, 1, 0x1, {0x000000, 128 * 1024} },
614 { 0, 1, 0x2, {0x000000, 256 * 1024} },
615 { 0, 1, 0x3, {0x000000, 512 * 1024} },
616 { 0, 1, 0x4, {0x000000, 1024 * 1024} },
617 { 0, 1, 0x5, {0x000000, 2048 * 1024} },
618 { 0, 1, 0x6, {0x000000, 4096 * 1024} },
619 { X, X, 0x7, {0x000000, 8192 * 1024} },
620
621 { 1, 0, 0x1, {0x7ff000, 4 * 1024} },
622 { 1, 0, 0x2, {0x7fe000, 8 * 1024} },
623 { 1, 0, 0x3, {0x7fc000, 16 * 1024} },
624 { 1, 0, 0x4, {0x7f8000, 32 * 1024} },
625 { 1, 0, 0x5, {0x7f8000, 32 * 1024} },
626 { 1, 0, 0x6, {0x7f8000, 32 * 1024} },
627
628 { 1, 1, 0x1, {0x000000, 4 * 1024} },
629 { 1, 1, 0x2, {0x000000, 8 * 1024} },
630 { 1, 1, 0x3, {0x000000, 16 * 1024} },
631 { 1, 1, 0x4, {0x000000, 32 * 1024} },
632 { 1, 1, 0x5, {0x000000, 32 * 1024} },
633 { 1, 1, 0x6, {0x000000, 32 * 1024} },
634};
635
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +0800636static struct w25q_range a25l040_ranges[] = {
637 { X, X, 0x0, {0, 0} }, /* none */
638 { X, X, 0x1, {0x70000, 64 * 1024} },
639 { X, X, 0x2, {0x60000, 128 * 1024} },
640 { X, X, 0x3, {0x40000, 256 * 1024} },
641 { X, X, 0x4, {0x00000, 512 * 1024} },
642 { X, X, 0x5, {0x00000, 512 * 1024} },
643 { X, X, 0x6, {0x00000, 512 * 1024} },
644 { X, X, 0x7, {0x00000, 512 * 1024} },
645};
646
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800647/* Given a flash chip, this function returns its range table. */
648static int w25_range_table(const struct flashchip *flash,
649 struct w25q_range **w25q_ranges,
650 int *num_entries)
David Hendricksf7924d12010-06-10 21:26:44 -0700651{
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800652 *w25q_ranges = 0;
653 *num_entries = 0;
David Hendricksf7924d12010-06-10 21:26:44 -0700654
David Hendricksd494b0a2010-08-16 16:28:50 -0700655 switch (flash->manufacture_id) {
656 case WINBOND_NEX_ID:
657 switch(flash->model_id) {
David Hendricksc801adb2010-12-09 16:58:56 -0800658 case WINBOND_NEX_W25X10:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800659 *w25q_ranges = w25x10_ranges;
660 *num_entries = ARRAY_SIZE(w25x10_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800661 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800662 case WINBOND_NEX_W25X20:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800663 *w25q_ranges = w25x20_ranges;
664 *num_entries = ARRAY_SIZE(w25x20_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800665 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800666 case WINBOND_NEX_W25X40:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800667 *w25q_ranges = w25x40_ranges;
668 *num_entries = ARRAY_SIZE(w25x40_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700669 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800670 case WINBOND_NEX_W25X80:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800671 *w25q_ranges = w25x80_ranges;
672 *num_entries = ARRAY_SIZE(w25x80_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800673 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800674 case WINBOND_NEX_W25Q80:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800675 *w25q_ranges = w25q80_ranges;
676 *num_entries = ARRAY_SIZE(w25q80_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700677 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800678 case WINBOND_NEX_W25Q16:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800679 *w25q_ranges = w25q16_ranges;
680 *num_entries = ARRAY_SIZE(w25q16_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700681 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800682 case WINBOND_NEX_W25Q32:
Louis Yung-Chieh Lo469707f2012-05-18 16:38:37 +0800683 case WINBOND_NEX_W25Q32DW:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800684 *w25q_ranges = w25q32_ranges;
685 *num_entries = ARRAY_SIZE(w25q32_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700686 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800687 case WINBOND_NEX_W25Q64:
AdamTsai141a2622013-12-31 14:07:15 +0800688 case WINBOND_NEX_W25Q64DW:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800689 *w25q_ranges = w25q64_ranges;
690 *num_entries = ARRAY_SIZE(w25q64_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700691 break;
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530692 case WINBOND_NEX_W25R128FV:
693 *w25q_ranges = w25r128_ranges;
694 *num_entries = ARRAY_SIZE(w25r128_ranges);
695 break;
David Hendricksd494b0a2010-08-16 16:28:50 -0700696 default:
697 msg_cerr("%s() %d: WINBOND flash chip mismatch (0x%04x)"
698 ", aborting\n", __func__, __LINE__,
699 flash->model_id);
700 return -1;
701 }
David Hendricks2c4a76c2010-06-28 14:00:43 -0700702 break;
David Hendricks57566ed2010-08-16 18:24:45 -0700703 case EON_ID_NOPREFIX:
704 switch (flash->model_id) {
David Hendricksc801adb2010-12-09 16:58:56 -0800705 case EON_EN25F40:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800706 *w25q_ranges = en25f40_ranges;
707 *num_entries = ARRAY_SIZE(en25f40_ranges);
David Hendricks57566ed2010-08-16 18:24:45 -0700708 break;
David Hendrickse185bf22011-05-24 15:34:18 -0700709 case EON_EN25Q40:
710 *w25q_ranges = en25q40_ranges;
711 *num_entries = ARRAY_SIZE(en25q40_ranges);
712 break;
713 case EON_EN25Q80:
714 *w25q_ranges = en25q80_ranges;
715 *num_entries = ARRAY_SIZE(en25q80_ranges);
716 break;
717 case EON_EN25Q32:
718 *w25q_ranges = en25q32_ranges;
719 *num_entries = ARRAY_SIZE(en25q32_ranges);
720 break;
721 case EON_EN25Q64:
722 *w25q_ranges = en25q64_ranges;
723 *num_entries = ARRAY_SIZE(en25q64_ranges);
724 break;
725 case EON_EN25Q128:
726 *w25q_ranges = en25q128_ranges;
727 *num_entries = ARRAY_SIZE(en25q128_ranges);
728 break;
Marc Jonesb2f90022014-04-29 17:37:23 -0600729 case EON_EN25S64:
730 *w25q_ranges = en25s64_ranges;
731 *num_entries = ARRAY_SIZE(en25s64_ranges);
732 break;
David Hendricks57566ed2010-08-16 18:24:45 -0700733 default:
734 msg_cerr("%s():%d: EON flash chip mismatch (0x%04x)"
735 ", aborting\n", __func__, __LINE__,
736 flash->model_id);
737 return -1;
738 }
739 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800740 case MACRONIX_ID:
David Hendricksac72e362010-08-16 18:20:03 -0700741 switch (flash->model_id) {
David Hendricksf8f00c72011-02-01 12:39:46 -0800742 case MACRONIX_MX25L1005:
743 *w25q_ranges = mx25l1005_ranges;
744 *num_entries = ARRAY_SIZE(mx25l1005_ranges);
745 break;
746 case MACRONIX_MX25L2005:
747 *w25q_ranges = mx25l2005_ranges;
748 *num_entries = ARRAY_SIZE(mx25l2005_ranges);
749 break;
750 case MACRONIX_MX25L4005:
751 *w25q_ranges = mx25l4005_ranges;
752 *num_entries = ARRAY_SIZE(mx25l4005_ranges);
753 break;
754 case MACRONIX_MX25L8005:
755 *w25q_ranges = mx25l8005_ranges;
756 *num_entries = ARRAY_SIZE(mx25l8005_ranges);
757 break;
758 case MACRONIX_MX25L1605:
759 /* FIXME: MX25L1605 and MX25L1605D have different write
760 * protection capabilities, but share IDs */
761 *w25q_ranges = mx25l1605d_ranges;
762 *num_entries = ARRAY_SIZE(mx25l1605d_ranges);
763 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800764 case MACRONIX_MX25L3205:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800765 *w25q_ranges = mx25l3205d_ranges;
766 *num_entries = ARRAY_SIZE(mx25l3205d_ranges);
David Hendricksac72e362010-08-16 18:20:03 -0700767 break;
Vincent Palatin87e092a2013-02-28 15:46:14 -0800768 case MACRONIX_MX25U3235E:
769 *w25q_ranges = mx25u3235e_ranges;
770 *num_entries = ARRAY_SIZE(mx25u3235e_ranges);
771 break;
Jongpil66a96492014-08-14 17:59:06 +0900772 case MACRONIX_MX25U6435E:
773 *w25q_ranges = mx25u6435e_ranges;
774 *num_entries = ARRAY_SIZE(mx25u6435e_ranges);
775 break;
David Hendricksac72e362010-08-16 18:20:03 -0700776 default:
777 msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)"
778 ", aborting\n", __func__, __LINE__,
779 flash->model_id);
780 return -1;
781 }
782 break;
David Hendricksbfa624b2012-07-24 12:47:59 -0700783 case ST_ID:
784 switch(flash->model_id) {
785 case ST_N25Q064__1E:
786 case ST_N25Q064__3E:
787 *w25q_ranges = n25q064_ranges;
788 *num_entries = ARRAY_SIZE(n25q064_ranges);
789 break;
790 default:
791 msg_cerr("%s() %d: Micron flash chip mismatch"
792 " (0x%04x), aborting\n", __func__, __LINE__,
793 flash->model_id);
794 return -1;
795 }
796 break;
Bryan Freed9a0051f2012-05-22 16:06:09 -0700797 case GIGADEVICE_ID:
798 switch(flash->model_id) {
799 case GIGADEVICE_GD25LQ32:
800 *w25q_ranges = w25q32_ranges;
801 *num_entries = ARRAY_SIZE(w25q32_ranges);
802 break;
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700803 case GIGADEVICE_GD25Q64:
Marc Jonesb18734f2014-04-03 16:19:47 -0600804 case GIGADEVICE_GD25LQ64:
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700805 *w25q_ranges = gd25q64_ranges;
806 *num_entries = ARRAY_SIZE(gd25q64_ranges);
807 break;
808 /* TODO(shawnn): add support for other GD parts */
Bryan Freed9a0051f2012-05-22 16:06:09 -0700809 default:
810 msg_cerr("%s() %d: GigaDevice flash chip mismatch"
811 " (0x%04x), aborting\n", __func__, __LINE__,
812 flash->model_id);
813 return -1;
814 }
815 break;
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +0800816 case AMIC_ID_NOPREFIX:
817 switch(flash->model_id) {
818 case AMIC_A25L040:
819 *w25q_ranges = a25l040_ranges;
820 *num_entries = ARRAY_SIZE(a25l040_ranges);
821 break;
822 default:
823 msg_cerr("%s() %d: AMIC flash chip mismatch"
824 " (0x%04x), aborting\n", __func__, __LINE__,
825 flash->model_id);
826 return -1;
827 }
828 break;
David Hendricksf7924d12010-06-10 21:26:44 -0700829 default:
David Hendricksd494b0a2010-08-16 16:28:50 -0700830 msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
831 __func__, flash->manufacture_id);
David Hendricksf7924d12010-06-10 21:26:44 -0700832 return -1;
833 }
834
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800835 return 0;
836}
837
838int w25_range_to_status(const struct flashchip *flash,
839 unsigned int start, unsigned int len,
840 struct w25q_status *status)
841{
842 struct w25q_range *w25q_ranges;
843 int i, range_found = 0;
844 int num_entries;
845
846 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
David Hendricksf7924d12010-06-10 21:26:44 -0700847 for (i = 0; i < num_entries; i++) {
848 struct wp_range *r = &w25q_ranges[i].range;
849
850 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
851 start, len, r->start, r->len);
852 if ((start == r->start) && (len == r->len)) {
David Hendricksd494b0a2010-08-16 16:28:50 -0700853 status->bp0 = w25q_ranges[i].bp & 1;
854 status->bp1 = w25q_ranges[i].bp >> 1;
855 status->bp2 = w25q_ranges[i].bp >> 2;
856 status->tb = w25q_ranges[i].tb;
857 status->sec = w25q_ranges[i].sec;
David Hendricksf7924d12010-06-10 21:26:44 -0700858
859 range_found = 1;
860 break;
861 }
862 }
863
864 if (!range_found) {
865 msg_cerr("matching range not found\n");
866 return -1;
867 }
David Hendricksd494b0a2010-08-16 16:28:50 -0700868 return 0;
869}
870
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800871int w25_status_to_range(const struct flashchip *flash,
872 const struct w25q_status *status,
873 unsigned int *start, unsigned int *len)
874{
875 struct w25q_range *w25q_ranges;
876 int i, status_found = 0;
877 int num_entries;
878
879 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
880 for (i = 0; i < num_entries; i++) {
881 int bp;
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +0800882 int table_bp, table_tb, table_sec;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800883
884 bp = status->bp0 | (status->bp1 << 1) | (status->bp2 << 2);
885 msg_cspew("comparing 0x%x 0x%x / 0x%x 0x%x / 0x%x 0x%x\n",
886 bp, w25q_ranges[i].bp,
887 status->tb, w25q_ranges[i].tb,
888 status->sec, w25q_ranges[i].sec);
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +0800889 table_bp = w25q_ranges[i].bp;
890 table_tb = w25q_ranges[i].tb;
891 table_sec = w25q_ranges[i].sec;
892 if ((bp == table_bp || table_bp == X) &&
893 (status->tb == table_tb || table_tb == X) &&
894 (status->sec == table_sec || table_sec == X)) {
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800895 *start = w25q_ranges[i].range.start;
896 *len = w25q_ranges[i].range.len;
897
898 status_found = 1;
899 break;
900 }
901 }
902
903 if (!status_found) {
904 msg_cerr("matching status not found\n");
905 return -1;
906 }
907 return 0;
908}
909
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800910/* Given a [start, len], this function calls w25_range_to_status() to convert
911 * it to flash-chip-specific range bits, then sets into status register.
912 */
David Hendricks91040832011-07-08 20:01:09 -0700913static int w25_set_range(const struct flashchip *flash,
David Hendricksd494b0a2010-08-16 16:28:50 -0700914 unsigned int start, unsigned int len)
915{
916 struct w25q_status status;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800917 int tmp = 0;
918 int expected = 0;
David Hendricksd494b0a2010-08-16 16:28:50 -0700919
920 memset(&status, 0, sizeof(status));
921 tmp = spi_read_status_register();
922 memcpy(&status, &tmp, 1);
923 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
924
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800925 if (w25_range_to_status(flash, start, len, &status)) return -1;
David Hendricksf7924d12010-06-10 21:26:44 -0700926
927 msg_cdbg("status.busy: %x\n", status.busy);
928 msg_cdbg("status.wel: %x\n", status.wel);
929 msg_cdbg("status.bp0: %x\n", status.bp0);
930 msg_cdbg("status.bp1: %x\n", status.bp1);
931 msg_cdbg("status.bp2: %x\n", status.bp2);
932 msg_cdbg("status.tb: %x\n", status.tb);
933 msg_cdbg("status.sec: %x\n", status.sec);
934 msg_cdbg("status.srp0: %x\n", status.srp0);
935
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800936 memcpy(&expected, &status, sizeof(status));
David Hendricks60824042014-12-11 17:22:06 -0800937 spi_write_status_register(flash, expected);
David Hendricksf7924d12010-06-10 21:26:44 -0700938
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800939 tmp = spi_read_status_register();
940 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
941 if ((tmp & MASK_WP_AREA) == (expected & MASK_WP_AREA)) {
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800942 return 0;
943 } else {
David Hendricksc801adb2010-12-09 16:58:56 -0800944 msg_cerr("expected=0x%02x, but actual=0x%02x.\n",
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800945 expected, tmp);
946 return 1;
947 }
David Hendricksf7924d12010-06-10 21:26:44 -0700948}
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530949static int w25r_set_range(const struct flashchip *flash,
950 unsigned int start, unsigned int len)
951{
952 struct w25q_status status;
953 struct flashchip chip;
954 uint8_t arr, expected;
955 int ret;
956
957 memset(&status, 0, sizeof(status));
958 memset(&chip, 0, sizeof(chip));
959 memcpy(&chip, flash, sizeof(chip));
960
961 /* passing a copy of flash since it is read only */
962 ret = flash->read(&chip, &arr, 0, 1);
963 if (ret) {
964 msg_cerr("Read status register failed.\n");
965 return ret;
966 }
967 memcpy(&status, &arr, 1);
968 msg_cdbg("%s: old status: 0x%02x\n", __func__, arr);
969
970 if (w25_range_to_status(flash, start, len, &status))
971 return -1;
972
973 msg_cdbg("status.busy: %x\n", status.busy);
974 msg_cdbg("status.wel: %x\n", status.wel);
975 msg_cdbg("status.bp0: %x\n", status.bp0);
976 msg_cdbg("status.bp1: %x\n", status.bp1);
977 msg_cdbg("status.bp2: %x\n", status.bp2);
978 msg_cdbg("status.tb: %x\n", status.tb);
979 msg_cdbg("status.sec: %x\n", status.sec);
980 msg_cdbg("status.srp0: %x\n", status.srp0);
981
982 memcpy(&expected, &status, sizeof(status));
983 ret = flash->write(&chip, &expected, 0, 1);
984 if (ret) {
985 msg_cerr("Write status register failed.\n");
986 return ret;
987 }
988 ret = flash->read(&chip, &arr, 0, 1);
989 if (ret) {
990 msg_cerr("Read status register failed.\n");
991 return ret;
992 }
993 msg_cdbg("%s: new status: 0x%02x\n", __func__, arr);
994
995 if ((arr & MASK_WP_AREA) == (expected & MASK_WP_AREA)) {
996 return 0;
997 } else {
998 msg_cerr("expected=0x%02x, but actual=0x%02x.\n",
999 expected, arr);
1000 return 1;
1001 }
1002}
1003
1004static int w25r_wp_status(const struct flashchip *flash)
1005{
1006 struct w25q_status sr;
1007 struct flashchip chip;
1008 uint8_t tmp;
1009 unsigned int start, len;
1010 int ret = 0;
1011
1012 memset(&sr, 0, sizeof(sr));
1013 memset(&chip, 0, sizeof(chip));
1014 memcpy(&chip, flash, sizeof(chip));
1015
1016 ret = flash->read(&chip, &tmp, 0, 1);
1017 if (ret) {
1018 msg_cerr("Read status register failed.\n");
1019 return ret;
1020 }
1021 memcpy(&sr, &tmp, 1);
1022 msg_cinfo("WP: status: 0x%02x\n", tmp);
1023 msg_cinfo("WP: status.srp0: %x\n", sr.srp0);
1024 msg_cinfo("WP: write protect is %s.\n",
1025 (sr.srp0) ? "enabled" : "disabled");
1026 msg_cinfo("WP: write protect range: ");
1027 if (w25_status_to_range(flash, &sr, &start, &len)) {
1028 msg_cinfo("(cannot resolve the range)\n");
1029 ret = -1;
1030 } else {
1031 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
1032 }
1033 return ret;
1034}
1035
David Hendricksf7924d12010-06-10 21:26:44 -07001036
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001037/* Print out the current status register value with human-readable text. */
David Hendricks91040832011-07-08 20:01:09 -07001038static int w25_wp_status(const struct flashchip *flash)
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001039{
1040 struct w25q_status status;
1041 int tmp;
David Hendricksce8ded32010-10-08 11:23:38 -07001042 unsigned int start, len;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001043 int ret = 0;
1044
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001045 memset(&status, 0, sizeof(status));
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001046 tmp = spi_read_status_register();
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001047 memcpy(&status, &tmp, 1);
1048 msg_cinfo("WP: status: 0x%02x\n", tmp);
1049 msg_cinfo("WP: status.srp0: %x\n", status.srp0);
1050 msg_cinfo("WP: write protect is %s.\n",
1051 status.srp0 ? "enabled" : "disabled");
1052
1053 msg_cinfo("WP: write protect range: ");
1054 if (w25_status_to_range(flash, &status, &start, &len)) {
1055 msg_cinfo("(cannot resolve the range)\n");
1056 ret = -1;
1057 } else {
1058 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
1059 }
1060
1061 return ret;
1062}
1063
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001064/* Set/clear the SRP0 bit in the status register. */
David Hendricks91040832011-07-08 20:01:09 -07001065static int w25_set_srp0(const struct flashchip *flash, int enable)
David Hendricksf7924d12010-06-10 21:26:44 -07001066{
1067 struct w25q_status status;
1068 int tmp = 0;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001069 int expected = 0;
David Hendricksf7924d12010-06-10 21:26:44 -07001070
1071 memset(&status, 0, sizeof(status));
1072 tmp = spi_read_status_register();
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001073 /* FIXME: this is NOT endian-free copy. */
David Hendricksf7924d12010-06-10 21:26:44 -07001074 memcpy(&status, &tmp, 1);
1075 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
1076
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001077 status.srp0 = enable ? 1 : 0;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001078 memcpy(&expected, &status, sizeof(status));
David Hendricks60824042014-12-11 17:22:06 -08001079 spi_write_status_register(flash, expected);
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001080
1081 tmp = spi_read_status_register();
1082 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
1083 if ((tmp & MASK_WP_AREA) != (expected & MASK_WP_AREA))
1084 return 1;
David Hendricksf7924d12010-06-10 21:26:44 -07001085
1086 return 0;
1087}
1088
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301089static int w25_set_srp(const struct flashchip *flash, int enable)
1090{
1091 struct w25q_status status;
1092 struct flashchip chip;
1093 int tmp = 0;
1094 uint8_t arr, expected;
1095
1096 memset(&status, 0, sizeof(status));
1097 memset(&chip, 0, sizeof(chip));
1098 memcpy(&chip, flash, sizeof(chip));
1099
1100 tmp = flash->read(&chip, &arr, 0, 1);
1101 if (tmp) {
1102 msg_cerr("Read status register failed.\n");
1103 return tmp;
1104 }
1105 memcpy(&status, &arr, 1);
1106 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
1107
1108 status.srp0 = enable ? 1 : 0;
1109 memcpy(&expected, &status, sizeof(status));
1110 tmp = flash->write(&chip, &expected, 0, 1);
1111 if (tmp) {
1112 msg_cerr("Write status register failed.\n");
1113 return tmp;
1114 }
1115 tmp = flash->read(&chip, &arr, 0, 1);
1116 if (tmp) {
1117 msg_cerr("Read status register failed.\n");
1118 return tmp;
1119 }
1120 msg_cdbg("%s: new status: 0x%02x\n", __func__, arr);
1121 if ((arr & MASK_WP_AREA) != (expected & MASK_WP_AREA))
1122 return 1;
1123
1124 return 0;
1125}
1126
David Hendricks1c09f802012-10-03 11:03:48 -07001127static int w25_enable_writeprotect(const struct flashchip *flash,
1128 enum wp_mode wp_mode)
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001129{
1130 int ret;
1131
David Hendricks1c09f802012-10-03 11:03:48 -07001132 switch (wp_mode) {
1133 case WP_MODE_HARDWARE:
1134 ret = w25_set_srp0(flash, 1);
1135 break;
1136 default:
1137 msg_cerr("%s(): unsupported write-protect mode\n", __func__);
1138 return 1;
1139 }
1140
David Hendricksc801adb2010-12-09 16:58:56 -08001141 if (ret)
1142 msg_cerr("%s(): error=%d.\n", __func__, ret);
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001143 return ret;
1144}
1145
David Hendricks91040832011-07-08 20:01:09 -07001146static int w25_disable_writeprotect(const struct flashchip *flash)
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001147{
1148 int ret;
1149
1150 ret = w25_set_srp0(flash, 0);
David Hendricksc801adb2010-12-09 16:58:56 -08001151 if (ret)
1152 msg_cerr("%s(): error=%d.\n", __func__, ret);
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001153 return ret;
1154}
1155
David Hendricks91040832011-07-08 20:01:09 -07001156static int w25_list_ranges(const struct flashchip *flash)
David Hendricks0f7f5382011-02-11 18:12:31 -08001157{
1158 struct w25q_range *w25q_ranges;
1159 int i, num_entries;
1160
1161 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
1162 for (i = 0; i < num_entries; i++) {
1163 msg_cinfo("start: 0x%06x, length: 0x%06x\n",
1164 w25q_ranges[i].range.start,
1165 w25q_ranges[i].range.len);
1166 }
1167
1168 return 0;
1169}
1170
David Hendricks1c09f802012-10-03 11:03:48 -07001171/* FIXME: Move to spi25.c if it's a JEDEC standard opcode */
1172uint8_t w25q_read_status_register_2(void)
1173{
1174 static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { 0x35 };
1175 unsigned char readarr[2];
1176 int ret;
1177
1178 /* Read Status Register */
1179 ret = spi_send_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
1180 if (ret) {
1181 /*
1182 * FIXME: make this a benign failure for now in case we are
1183 * unable to execute the opcode
1184 */
1185 msg_cdbg("RDSR2 failed!\n");
1186 readarr[0] = 0x00;
1187 }
1188
1189 return readarr[0];
1190}
1191
1192static int w25q_wp_status(const struct flashchip *flash)
1193{
1194 struct w25q_status sr1;
1195 struct w25q_status_2 sr2;
David Hendricksf1bd8802012-10-30 11:37:57 -07001196 uint8_t tmp[2];
David Hendricks1c09f802012-10-03 11:03:48 -07001197 unsigned int start, len;
1198 int ret = 0;
1199
1200 memset(&sr1, 0, sizeof(sr1));
David Hendricksf1bd8802012-10-30 11:37:57 -07001201 tmp[0] = spi_read_status_register();
1202 memcpy(&sr1, &tmp[0], 1);
David Hendricks1c09f802012-10-03 11:03:48 -07001203
David Hendricksf1bd8802012-10-30 11:37:57 -07001204 memset(&sr2, 0, sizeof(sr2));
1205 tmp[1] = w25q_read_status_register_2();
1206 memcpy(&sr2, &tmp[1], 1);
1207
1208 msg_cinfo("WP: status: 0x%02x%02x\n", tmp[1], tmp[0]);
David Hendricks1c09f802012-10-03 11:03:48 -07001209 msg_cinfo("WP: status.srp0: %x\n", sr1.srp0);
1210 msg_cinfo("WP: status.srp1: %x\n", sr2.srp1);
1211 msg_cinfo("WP: write protect is %s.\n",
1212 (sr1.srp0 || sr2.srp1) ? "enabled" : "disabled");
1213
1214 msg_cinfo("WP: write protect range: ");
1215 if (w25_status_to_range(flash, &sr1, &start, &len)) {
1216 msg_cinfo("(cannot resolve the range)\n");
1217 ret = -1;
1218 } else {
1219 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
1220 }
1221
1222 return ret;
1223}
1224
1225/*
1226 * W25Q adds an optional byte to the standard WRSR opcode. If /CS is
1227 * de-asserted after the first byte, then it acts like a JEDEC-standard
1228 * WRSR command. if /CS is asserted, then the next data byte is written
1229 * into status register 2.
1230 */
1231#define W25Q_WRSR_OUTSIZE 0x03
1232static int w25q_write_status_register_WREN(uint8_t s1, uint8_t s2)
1233{
1234 int result;
1235 struct spi_command cmds[] = {
1236 {
1237 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
1238 .writecnt = JEDEC_WREN_OUTSIZE,
1239 .writearr = (const unsigned char[]){ JEDEC_WREN },
1240 .readcnt = 0,
1241 .readarr = NULL,
1242 }, {
1243 .writecnt = W25Q_WRSR_OUTSIZE,
1244 .writearr = (const unsigned char[]){ JEDEC_WRSR, s1, s2 },
1245 .readcnt = 0,
1246 .readarr = NULL,
1247 }, {
1248 .writecnt = 0,
1249 .writearr = NULL,
1250 .readcnt = 0,
1251 .readarr = NULL,
1252 }};
1253
1254 result = spi_send_multicommand(cmds);
1255 if (result) {
1256 msg_cerr("%s failed during command execution\n",
1257 __func__);
1258 }
1259
1260 /* WRSR performs a self-timed erase before the changes take effect. */
David Hendricks60824042014-12-11 17:22:06 -08001261 programmer_delay(100 * 1000);
David Hendricks1c09f802012-10-03 11:03:48 -07001262
1263 return result;
1264}
1265
1266/*
1267 * Set/clear the SRP1 bit in status register 2.
1268 * FIXME: make this more generic if other chips use the same SR2 layout
1269 */
1270static int w25q_set_srp1(const struct flashchip *flash, int enable)
1271{
1272 struct w25q_status sr1;
1273 struct w25q_status_2 sr2;
1274 uint8_t tmp, expected;
1275
1276 tmp = spi_read_status_register();
1277 memcpy(&sr1, &tmp, 1);
1278 tmp = w25q_read_status_register_2();
1279 memcpy(&sr2, &tmp, 1);
1280
1281 msg_cdbg("%s: old status 2: 0x%02x\n", __func__, tmp);
1282
1283 sr2.srp1 = enable ? 1 : 0;
1284
1285 memcpy(&expected, &sr2, 1);
1286 w25q_write_status_register_WREN(*((uint8_t *)&sr1), *((uint8_t *)&sr2));
1287
1288 tmp = w25q_read_status_register_2();
1289 msg_cdbg("%s: new status 2: 0x%02x\n", __func__, tmp);
1290 if ((tmp & MASK_WP2_AREA) != (expected & MASK_WP2_AREA))
1291 return 1;
1292
1293 return 0;
1294}
1295
1296enum wp_mode get_wp_mode(const char *mode_str)
1297{
1298 enum wp_mode wp_mode = WP_MODE_UNKNOWN;
1299
1300 if (!strcasecmp(mode_str, "hardware"))
1301 wp_mode = WP_MODE_HARDWARE;
1302 else if (!strcasecmp(mode_str, "power_cycle"))
1303 wp_mode = WP_MODE_POWER_CYCLE;
1304 else if (!strcasecmp(mode_str, "permanent"))
1305 wp_mode = WP_MODE_PERMANENT;
1306
1307 return wp_mode;
1308}
1309
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301310static int w25r_disable_writeprotect(const struct flashchip *flash)
1311{
1312 int ret;
1313
1314 ret = w25_set_srp(flash, 0);
1315 if (ret)
1316 msg_cerr("%s(): error=%d.\n", __func__, ret);
1317
1318 return ret;
1319}
1320
David Hendricks1c09f802012-10-03 11:03:48 -07001321static int w25q_disable_writeprotect(const struct flashchip *flash,
1322 enum wp_mode wp_mode)
1323{
1324 int ret = 1;
David Hendricks1c09f802012-10-03 11:03:48 -07001325 struct w25q_status_2 sr2;
1326 uint8_t tmp;
1327
1328 switch (wp_mode) {
1329 case WP_MODE_HARDWARE:
1330 ret = w25_set_srp0(flash, 0);
1331 break;
1332 case WP_MODE_POWER_CYCLE:
1333 tmp = w25q_read_status_register_2();
1334 memcpy(&sr2, &tmp, 1);
1335 if (sr2.srp1) {
1336 msg_cerr("%s(): must disconnect power to disable "
1337 "write-protection\n", __func__);
1338 } else {
1339 ret = 0;
1340 }
1341 break;
1342 case WP_MODE_PERMANENT:
1343 msg_cerr("%s(): cannot disable permanent write-protection\n",
1344 __func__);
1345 break;
1346 default:
1347 msg_cerr("%s(): invalid mode specified\n", __func__);
1348 break;
1349 }
1350
1351 if (ret)
1352 msg_cerr("%s(): error=%d.\n", __func__, ret);
1353 return ret;
1354}
1355
1356static int w25q_disable_writeprotect_default(const struct flashchip *flash)
1357{
1358 return w25q_disable_writeprotect(flash, WP_MODE_HARDWARE);
1359}
1360
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301361static int w25r_enable_writeprotect(const struct flashchip *flash,
1362 enum wp_mode wp_mode)
1363{
1364 int ret;
1365
1366 switch (wp_mode) {
1367 case WP_MODE_HARDWARE:
1368 ret = w25_set_srp(flash, 1);
1369 break;
1370 default:
1371 msg_perr("%s(): invalid mode for Sunrise Point %d\n",
1372 __func__, wp_mode);
1373 break;
1374 }
1375 if (ret)
1376 msg_cerr("%s(): error=%d.\n", __func__, ret);
1377
1378 return ret;
1379}
1380
David Hendricks1c09f802012-10-03 11:03:48 -07001381static int w25q_enable_writeprotect(const struct flashchip *flash,
1382 enum wp_mode wp_mode)
1383{
1384 int ret = 1;
1385 struct w25q_status sr1;
1386 struct w25q_status_2 sr2;
1387 uint8_t tmp;
1388
1389 switch (wp_mode) {
1390 case WP_MODE_HARDWARE:
1391 if (w25q_disable_writeprotect(flash, WP_MODE_POWER_CYCLE)) {
1392 msg_cerr("%s(): cannot disable power cycle WP mode\n",
1393 __func__);
1394 break;
1395 }
1396
1397 tmp = spi_read_status_register();
1398 memcpy(&sr1, &tmp, 1);
1399 if (sr1.srp0)
1400 ret = 0;
1401 else
1402 ret = w25_set_srp0(flash, 1);
1403
1404 break;
1405 case WP_MODE_POWER_CYCLE:
1406 if (w25q_disable_writeprotect(flash, WP_MODE_HARDWARE)) {
1407 msg_cerr("%s(): cannot disable hardware WP mode\n",
1408 __func__);
1409 break;
1410 }
1411
1412 tmp = w25q_read_status_register_2();
1413 memcpy(&sr2, &tmp, 1);
1414 if (sr2.srp1)
1415 ret = 0;
1416 else
1417 ret = w25q_set_srp1(flash, 1);
1418
1419 break;
1420 case WP_MODE_PERMANENT:
1421 tmp = spi_read_status_register();
1422 memcpy(&sr1, &tmp, 1);
1423 if (sr1.srp0 == 0) {
1424 ret = w25_set_srp0(flash, 1);
1425 if (ret) {
David Hendricksf1bd8802012-10-30 11:37:57 -07001426 msg_perr("%s(): cannot enable SRP0 for "
David Hendricks1c09f802012-10-03 11:03:48 -07001427 "permanent WP\n", __func__);
1428 break;
1429 }
1430 }
1431
1432 tmp = w25q_read_status_register_2();
1433 memcpy(&sr2, &tmp, 1);
1434 if (sr2.srp1 == 0) {
1435 ret = w25q_set_srp1(flash, 1);
1436 if (ret) {
David Hendricksf1bd8802012-10-30 11:37:57 -07001437 msg_perr("%s(): cannot enable SRP1 for "
David Hendricks1c09f802012-10-03 11:03:48 -07001438 "permanent WP\n", __func__);
1439 break;
1440 }
1441 }
1442
1443 break;
David Hendricksf1bd8802012-10-30 11:37:57 -07001444 default:
1445 msg_perr("%s(): invalid mode %d\n", __func__, wp_mode);
1446 break;
David Hendricks1c09f802012-10-03 11:03:48 -07001447 }
1448
1449 if (ret)
1450 msg_cerr("%s(): error=%d.\n", __func__, ret);
1451 return ret;
1452}
1453
David Hendricksc3496092014-11-13 17:20:55 -08001454/* FIXME: Move to spi25.c if it's a JEDEC standard opcode */
1455uint8_t mx25l_read_config_register(void)
1456{
1457 static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { 0x15 };
1458 unsigned char readarr[2]; /* leave room for dummy byte */
1459 int ret;
1460
1461 ret = spi_send_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
1462 if (ret) {
1463 msg_cerr("RDCR failed!\n");
1464 readarr[0] = 0x00;
1465 }
1466
1467 return readarr[0];
1468}
David Hendricks1c09f802012-10-03 11:03:48 -07001469/* W25P, W25X, and many flash chips from various vendors */
David Hendricksf7924d12010-06-10 21:26:44 -07001470struct wp wp_w25 = {
David Hendricks0f7f5382011-02-11 18:12:31 -08001471 .list_ranges = w25_list_ranges,
David Hendricksf7924d12010-06-10 21:26:44 -07001472 .set_range = w25_set_range,
1473 .enable = w25_enable_writeprotect,
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001474 .disable = w25_disable_writeprotect,
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001475 .wp_status = w25_wp_status,
David Hendricks1c09f802012-10-03 11:03:48 -07001476
1477};
1478
1479/* W25Q series has features such as a second status register and SFDP */
1480struct wp wp_w25q = {
1481 .list_ranges = w25_list_ranges,
1482 .set_range = w25_set_range,
1483 .enable = w25q_enable_writeprotect,
1484 /*
1485 * By default, disable hardware write-protection. We may change
1486 * this later if we want to add fine-grained write-protect disable
1487 * as a command-line option.
1488 */
1489 .disable = w25q_disable_writeprotect_default,
1490 .wp_status = w25q_wp_status,
David Hendricksf7924d12010-06-10 21:26:44 -07001491};
David Hendrickse0512a72014-07-15 20:30:47 -07001492
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301493/* W25R Series */
1494struct wp wp_w25r = {
1495 .list_ranges = w25_list_ranges,
1496 .set_range = w25r_set_range,
1497 .enable = w25r_enable_writeprotect,
1498 .disable = w25r_disable_writeprotect,
1499 .wp_status = w25r_wp_status,
1500};
1501
David Hendricksaf3944a2014-07-28 18:37:40 -07001502struct generic_range gd25q32_cmp0_ranges[] = {
1503 /* none, bp4 and bp3 => don't care */
David Hendricks148a4bf2015-03-13 21:02:42 -07001504 { { }, 0x00, {0, 0} },
1505 { { }, 0x08, {0, 0} },
1506 { { }, 0x10, {0, 0} },
1507 { { }, 0x18, {0, 0} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001508
David Hendricks148a4bf2015-03-13 21:02:42 -07001509 { { }, 0x01, {0x3f0000, 64 * 1024} },
1510 { { }, 0x02, {0x3e0000, 128 * 1024} },
1511 { { }, 0x03, {0x3c0000, 256 * 1024} },
1512 { { }, 0x04, {0x380000, 512 * 1024} },
1513 { { }, 0x05, {0x300000, 1024 * 1024} },
1514 { { }, 0x06, {0x200000, 2048 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001515
David Hendricks148a4bf2015-03-13 21:02:42 -07001516 { { }, 0x09, {0x000000, 64 * 1024} },
1517 { { }, 0x0a, {0x000000, 128 * 1024} },
1518 { { }, 0x0b, {0x000000, 256 * 1024} },
1519 { { }, 0x0c, {0x000000, 512 * 1024} },
1520 { { }, 0x0d, {0x000000, 1024 * 1024} },
1521 { { }, 0x0e, {0x000000, 2048 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001522
1523 /* all, bp4 and bp3 => don't care */
David Hendricks148a4bf2015-03-13 21:02:42 -07001524 { { }, 0x07, {0x000000, 4096 * 1024} },
1525 { { }, 0x0f, {0x000000, 4096 * 1024} },
1526 { { }, 0x17, {0x000000, 4096 * 1024} },
1527 { { }, 0x1f, {0x000000, 4096 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001528
David Hendricks148a4bf2015-03-13 21:02:42 -07001529 { { }, 0x11, {0x3ff000, 4 * 1024} },
1530 { { }, 0x12, {0x3fe000, 8 * 1024} },
1531 { { }, 0x13, {0x3fc000, 16 * 1024} },
1532 { { }, 0x14, {0x3f8000, 32 * 1024} }, /* bp0 => don't care */
1533 { { }, 0x15, {0x3f8000, 32 * 1024} }, /* bp0 => don't care */
1534 { { }, 0x16, {0x3f8000, 32 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001535
David Hendricks148a4bf2015-03-13 21:02:42 -07001536 { { }, 0x19, {0x000000, 4 * 1024} },
1537 { { }, 0x1a, {0x000000, 8 * 1024} },
1538 { { }, 0x1b, {0x000000, 16 * 1024} },
1539 { { }, 0x1c, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1540 { { }, 0x1d, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1541 { { }, 0x1e, {0x000000, 32 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001542};
1543
1544struct generic_range gd25q32_cmp1_ranges[] = {
1545 /* none, bp4 and bp3 => don't care */
David Hendricks148a4bf2015-03-13 21:02:42 -07001546 { { }, 0x00, {0, 0} },
1547 { { }, 0x08, {0, 0} },
1548 { { }, 0x10, {0, 0} },
1549 { { }, 0x18, {0, 0} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001550
David Hendricks148a4bf2015-03-13 21:02:42 -07001551 { { }, 0x01, {0x000000, 4032 * 1024} },
1552 { { }, 0x02, {0x000000, 3968 * 1024} },
1553 { { }, 0x03, {0x000000, 3840 * 1024} },
1554 { { }, 0x04, {0x000000, 3584 * 1024} },
1555 { { }, 0x05, {0x000000, 3 * 1024 * 1024} },
1556 { { }, 0x06, {0x000000, 2 * 1024 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001557
David Hendricks148a4bf2015-03-13 21:02:42 -07001558 { { }, 0x09, {0x010000, 4032 * 1024} },
1559 { { }, 0x0a, {0x020000, 3968 * 1024} },
1560 { { }, 0x0b, {0x040000, 3840 * 1024} },
1561 { { }, 0x0c, {0x080000, 3584 * 1024} },
1562 { { }, 0x0d, {0x100000, 3 * 1024 * 1024} },
1563 { { }, 0x0e, {0x200000, 2 * 1024 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001564
1565 /* all, bp4 and bp3 => don't care */
David Hendricks148a4bf2015-03-13 21:02:42 -07001566 { { }, 0x07, {0x000000, 4096 * 1024} },
1567 { { }, 0x0f, {0x000000, 4096 * 1024} },
1568 { { }, 0x17, {0x000000, 4096 * 1024} },
1569 { { }, 0x1f, {0x000000, 4096 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001570
David Hendricks148a4bf2015-03-13 21:02:42 -07001571 { { }, 0x11, {0x000000, 4092 * 1024} },
1572 { { }, 0x12, {0x000000, 4088 * 1024} },
1573 { { }, 0x13, {0x000000, 4080 * 1024} },
1574 { { }, 0x14, {0x000000, 4064 * 1024} }, /* bp0 => don't care */
1575 { { }, 0x15, {0x000000, 4064 * 1024} }, /* bp0 => don't care */
1576 { { }, 0x16, {0x000000, 4064 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001577
David Hendricks148a4bf2015-03-13 21:02:42 -07001578 { { }, 0x19, {0x001000, 4092 * 1024} },
1579 { { }, 0x1a, {0x002000, 4088 * 1024} },
1580 { { }, 0x1b, {0x040000, 4080 * 1024} },
1581 { { }, 0x1c, {0x080000, 4064 * 1024} }, /* bp0 => don't care */
1582 { { }, 0x1d, {0x080000, 4064 * 1024} }, /* bp0 => don't care */
1583 { { }, 0x1e, {0x080000, 4064 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001584};
1585
1586static struct generic_wp gd25q32_wp = {
1587 /* TODO: map second status register */
1588 .sr1 = { .bp0_pos = 2, .bp_bits = 5, .srp_pos = 7 },
1589};
1590
David Hendricks83541d32014-07-15 20:58:21 -07001591#if 0
1592/* FIXME: MX25L6405D has same ID as MX25L6406 */
1593static struct w25q_range mx25l6405d_ranges[] = {
1594 { X, 0, 0, {0, 0} }, /* none */
1595 { X, 0, 0x1, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
1596 { X, 0, 0x2, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
1597 { X, 0, 0x3, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
1598 { X, 0, 0x4, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
1599 { X, 0, 0x5, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
1600 { X, 0, 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
1601 { X, 0, 0x7, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */
1602
1603 { X, 1, 0x0, {0x000000, 8192 * 1024} },
1604 { X, 1, 0x1, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
1605 { X, 1, 0x2, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */
1606 { X, 1, 0x3, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */
1607 { X, 1, 0x4, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */
1608 { X, 1, 0x5, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */
1609 { X, 1, 0x6, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */
1610 { X, 1, 0x7, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */
1611};
1612#endif
1613
1614/* FIXME: MX25L6406 has same ID as MX25L6405D */
1615struct generic_range mx25l6406e_ranges[] = {
David Hendricks148a4bf2015-03-13 21:02:42 -07001616 { { }, 0, {0, 0} }, /* none */
1617 { { }, 0x1, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */
1618 { { }, 0x2, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */
1619 { { }, 0x3, {0x7a0000, 64 * 8 * 1024} }, /* blocks 120-127 */
1620 { { }, 0x4, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */
1621 { { }, 0x5, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */
1622 { { }, 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
David Hendricks83541d32014-07-15 20:58:21 -07001623
David Hendricks148a4bf2015-03-13 21:02:42 -07001624 { { }, 0x7, {0x000000, 64 * 128 * 1024} }, /* all */
1625 { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* all */
1626 { { }, 0x9, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
1627 { { }, 0xa, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */
1628 { { }, 0xb, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */
1629 { { }, 0xc, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */
1630 { { }, 0xd, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */
1631 { { }, 0xe, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */
1632 { { }, 0xf, {0x000000, 64 * 128 * 1024} }, /* all */
David Hendricks83541d32014-07-15 20:58:21 -07001633};
1634
1635static struct generic_wp mx25l6406e_wp = {
1636 .sr1 = { .bp0_pos = 2, .bp_bits = 4, .srp_pos = 7 },
1637 .ranges = &mx25l6406e_ranges[0],
1638};
David Hendrickse0512a72014-07-15 20:30:47 -07001639
David Hendricksc3496092014-11-13 17:20:55 -08001640struct generic_range mx25l6495f_tb0_ranges[] = {
David Hendricks148a4bf2015-03-13 21:02:42 -07001641 { { }, 0, {0, 0} }, /* none */
1642 { { }, 0x1, {0x7f0000, 64 * 1 * 1024} }, /* block 127 */
1643 { { }, 0x2, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */
1644 { { }, 0x3, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */
David Hendricksc3496092014-11-13 17:20:55 -08001645
David Hendricks148a4bf2015-03-13 21:02:42 -07001646 { { }, 0x4, {0x780000, 64 * 8 * 1024} }, /* blocks 120-127 */
1647 { { }, 0x5, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */
1648 { { }, 0x6, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */
1649 { { }, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
1650 { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* all */
1651 { { }, 0x9, {0x000000, 64 * 128 * 1024} }, /* all */
1652 { { }, 0xa, {0x000000, 64 * 128 * 1024} }, /* all */
1653 { { }, 0xb, {0x000000, 64 * 128 * 1024} }, /* all */
1654 { { }, 0xc, {0x000000, 64 * 128 * 1024} }, /* all */
1655 { { }, 0xd, {0x000000, 64 * 128 * 1024} }, /* all */
1656 { { }, 0xe, {0x000000, 64 * 128 * 1024} }, /* all */
1657 { { }, 0xf, {0x000000, 64 * 128 * 1024} }, /* all */
David Hendricksc3496092014-11-13 17:20:55 -08001658};
1659
1660struct generic_range mx25l6495f_tb1_ranges[] = {
David Hendricks148a4bf2015-03-13 21:02:42 -07001661 { { }, 0, {0, 0} }, /* none */
1662 { { }, 0x1, {0x000000, 64 * 1 * 1024} }, /* block 0 */
1663 { { }, 0x2, {0x000000, 64 * 2 * 1024} }, /* blocks 0-1 */
1664 { { }, 0x3, {0x000000, 64 * 4 * 1024} }, /* blocks 0-3 */
1665 { { }, 0x4, {0x000000, 64 * 8 * 1024} }, /* blocks 0-7 */
1666 { { }, 0x5, {0x000000, 64 * 16 * 1024} }, /* blocks 0-15 */
1667 { { }, 0x6, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
1668 { { }, 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
1669 { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* all */
1670 { { }, 0x9, {0x000000, 64 * 128 * 1024} }, /* all */
1671 { { }, 0xa, {0x000000, 64 * 128 * 1024} }, /* all */
1672 { { }, 0xb, {0x000000, 64 * 128 * 1024} }, /* all */
1673 { { }, 0xc, {0x000000, 64 * 128 * 1024} }, /* all */
1674 { { }, 0xd, {0x000000, 64 * 128 * 1024} }, /* all */
1675 { { }, 0xe, {0x000000, 64 * 128 * 1024} }, /* all */
1676 { { }, 0xf, {0x000000, 64 * 128 * 1024} }, /* all */
David Hendricksc3496092014-11-13 17:20:55 -08001677};
1678
1679static struct generic_wp mx25l6495f_wp = {
1680 .sr1 = { .bp0_pos = 2, .bp_bits = 4, .srp_pos = 7 },
1681};
1682
David Hendricks148a4bf2015-03-13 21:02:42 -07001683struct generic_range s25fs128s_ranges[] = {
1684 { { .tb = 1 }, 0, {0, 0} }, /* none */
1685 { { .tb = 1 }, 0x1, {0x000000, 256 * 1024} }, /* lower 64th */
1686 { { .tb = 1 }, 0x2, {0x000000, 512 * 1024} }, /* lower 32nd */
1687 { { .tb = 1 }, 0x3, {0x000000, 1024 * 1024} }, /* lower 16th */
1688 { { .tb = 1 }, 0x4, {0x000000, 2048 * 1024} }, /* lower 8th */
1689 { { .tb = 1 }, 0x5, {0x000000, 4096 * 1024} }, /* lower 4th */
1690 { { .tb = 1 }, 0x6, {0x000000, 8192 * 1024} }, /* lower half */
1691 { { .tb = 1 }, 0x7, {0x000000, 16384 * 1024} }, /* all */
David Hendricksa9884852014-12-11 15:31:12 -08001692
David Hendricks148a4bf2015-03-13 21:02:42 -07001693 { { .tb = 0 }, 0, {0, 0} }, /* none */
1694 { { .tb = 0 }, 0x1, {0xfc0000, 256 * 1024} }, /* upper 64th */
1695 { { .tb = 0 }, 0x2, {0xf80000, 512 * 1024} }, /* upper 32nd */
1696 { { .tb = 0 }, 0x3, {0xf00000, 1024 * 1024} }, /* upper 16th */
1697 { { .tb = 0 }, 0x4, {0xe00000, 2048 * 1024} }, /* upper 8th */
1698 { { .tb = 0 }, 0x5, {0xc00000, 4096 * 1024} }, /* upper 4th */
1699 { { .tb = 0 }, 0x6, {0x800000, 8192 * 1024} }, /* upper half */
1700 { { .tb = 0 }, 0x7, {0x000000, 16384 * 1024} }, /* all */
David Hendricksa9884852014-12-11 15:31:12 -08001701};
1702
1703static struct generic_wp s25fs128s_wp = {
1704 .sr1 = { .bp0_pos = 2, .bp_bits = 3, .srp_pos = 7 },
David Hendricks148a4bf2015-03-13 21:02:42 -07001705 .get_modifier_bits = s25f_get_modifier_bits,
1706 .set_modifier_bits = s25f_set_modifier_bits,
David Hendricksa9884852014-12-11 15:31:12 -08001707};
1708
David Hendricksc694bb82015-02-25 14:52:17 -08001709
David Hendricks148a4bf2015-03-13 21:02:42 -07001710struct generic_range s25fl256s_ranges[] = {
1711 { { .tb = 1 }, 0, {0, 0} }, /* none */
1712 { { .tb = 1 }, 0x1, {0x000000, 512 * 1024} }, /* lower 64th */
1713 { { .tb = 1 }, 0x2, {0x000000, 1024 * 1024} }, /* lower 32nd */
1714 { { .tb = 1 }, 0x3, {0x000000, 2048 * 1024} }, /* lower 16th */
1715 { { .tb = 1 }, 0x4, {0x000000, 4096 * 1024} }, /* lower 8th */
1716 { { .tb = 1 }, 0x5, {0x000000, 8192 * 1024} }, /* lower 4th */
1717 { { .tb = 1 }, 0x6, {0x000000, 16384 * 1024} }, /* lower half */
1718 { { .tb = 1 }, 0x7, {0x000000, 32768 * 1024} }, /* all */
1719
1720 { { .tb = 0 }, 0, {0, 0} }, /* none */
1721 { { .tb = 0 }, 0x1, {0x1f80000, 512 * 1024} }, /* upper 64th */
1722 { { .tb = 0 }, 0x2, {0x1f00000, 1024 * 1024} }, /* upper 32nd */
1723 { { .tb = 0 }, 0x3, {0x1e00000, 2048 * 1024} }, /* upper 16th */
1724 { { .tb = 0 }, 0x4, {0x1c00000, 4096 * 1024} }, /* upper 8th */
1725 { { .tb = 0 }, 0x5, {0x1800000, 8192 * 1024} }, /* upper 4th */
1726 { { .tb = 0 }, 0x6, {0x1000000, 16384 * 1024} }, /* upper half */
1727 { { .tb = 0 }, 0x7, {0x000000, 32768 * 1024} }, /* all */
David Hendricksc694bb82015-02-25 14:52:17 -08001728};
1729
1730static struct generic_wp s25fl256s_wp = {
1731 .sr1 = { .bp0_pos = 2, .bp_bits = 3, .srp_pos = 7 },
David Hendricks148a4bf2015-03-13 21:02:42 -07001732 .get_modifier_bits = s25f_get_modifier_bits,
1733 .set_modifier_bits = s25f_set_modifier_bits,
David Hendricksc694bb82015-02-25 14:52:17 -08001734};
1735
David Hendrickse0512a72014-07-15 20:30:47 -07001736/* Given a flash chip, this function returns its writeprotect info. */
1737static int generic_range_table(const struct flashchip *flash,
1738 struct generic_wp **wp,
1739 int *num_entries)
1740{
1741 *wp = NULL;
1742 *num_entries = 0;
1743
1744 switch (flash->manufacture_id) {
David Hendricksaf3944a2014-07-28 18:37:40 -07001745 case GIGADEVICE_ID:
1746 switch(flash->model_id) {
1747 case GIGADEVICE_GD25Q32: {
1748 uint8_t sr1 = w25q_read_status_register_2();
1749
1750 *wp = &gd25q32_wp;
1751 if (!(sr1 & (1 << 6))) { /* CMP == 0 */
1752 (*wp)->ranges = &gd25q32_cmp0_ranges[0];
1753 *num_entries = ARRAY_SIZE(gd25q32_cmp0_ranges);
1754 } else { /* CMP == 1 */
1755 (*wp)->ranges = &gd25q32_cmp1_ranges[0];
1756 *num_entries = ARRAY_SIZE(gd25q32_cmp1_ranges);
1757 }
1758
1759 break;
1760 /* TODO(shawnn): add support for other GD parts */
1761 }
1762 default:
1763 msg_cerr("%s() %d: GigaDevice flash chip mismatch"
1764 " (0x%04x), aborting\n", __func__, __LINE__,
1765 flash->model_id);
1766 return -1;
1767 }
1768 break;
David Hendricks83541d32014-07-15 20:58:21 -07001769 case MACRONIX_ID:
1770 switch (flash->model_id) {
1771 case MACRONIX_MX25L6405:
1772 /* FIXME: MX25L64* chips have mixed capabilities and
1773 share IDs */
1774 *wp = &mx25l6406e_wp;
1775 *num_entries = ARRAY_SIZE(mx25l6406e_ranges);
1776 break;
David Hendricksc3496092014-11-13 17:20:55 -08001777 case MACRONIX_MX25L6495F: {
1778 uint8_t cr = mx25l_read_config_register();
1779
1780 *wp = &mx25l6495f_wp;
1781 if (!(cr & (1 << 3))) { /* T/B == 0 */
1782 (*wp)->ranges = &mx25l6495f_tb0_ranges[0];
1783 *num_entries = ARRAY_SIZE(mx25l6495f_tb0_ranges);
1784 } else { /* T/B == 1 */
1785 (*wp)->ranges = &mx25l6495f_tb1_ranges[0];
1786 *num_entries = ARRAY_SIZE(mx25l6495f_tb1_ranges);
1787 }
1788 break;
1789 }
David Hendricks83541d32014-07-15 20:58:21 -07001790 default:
1791 msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)"
1792 ", aborting\n", __func__, __LINE__,
1793 flash->model_id);
1794 return -1;
1795 }
1796 break;
David Hendricksa9884852014-12-11 15:31:12 -08001797 case SPANSION_ID:
1798 switch (flash->model_id) {
1799 case SPANSION_S25FS128S_L:
1800 case SPANSION_S25FS128S_S: {
David Hendricksa9884852014-12-11 15:31:12 -08001801 *wp = &s25fs128s_wp;
David Hendricks148a4bf2015-03-13 21:02:42 -07001802 (*wp)->ranges = s25fs128s_ranges;
1803 *num_entries = ARRAY_SIZE(s25fs128s_ranges);
David Hendricksa9884852014-12-11 15:31:12 -08001804 break;
1805 }
David Hendricksc694bb82015-02-25 14:52:17 -08001806 case SPANSION_S25FL256S_UL:
1807 case SPANSION_S25FL256S_US: {
David Hendricksc694bb82015-02-25 14:52:17 -08001808 *wp = &s25fl256s_wp;
David Hendricks148a4bf2015-03-13 21:02:42 -07001809 (*wp)->ranges = s25fl256s_ranges;
1810 *num_entries = ARRAY_SIZE(s25fl256s_ranges);
David Hendricksc694bb82015-02-25 14:52:17 -08001811 break;
1812 }
David Hendricksa9884852014-12-11 15:31:12 -08001813 default:
1814 msg_cerr("%s():%d Spansion flash chip mismatch (0x%04x)"
1815 ", aborting\n", __func__, __LINE__, flash->model_id);
1816 return -1;
1817 }
1818 break;
David Hendrickse0512a72014-07-15 20:30:47 -07001819 default:
1820 msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
1821 __func__, flash->manufacture_id);
1822 return -1;
1823 }
1824
1825 return 0;
1826}
1827
1828/* Given a [start, len], this function finds a block protect bit combination
1829 * (if possible) and sets the corresponding bits in "status". Remaining bits
1830 * are preserved. */
1831static int generic_range_to_status(const struct flashchip *flash,
1832 unsigned int start, unsigned int len,
1833 uint8_t *status)
1834{
1835 struct generic_wp *wp;
1836 struct generic_range *r;
1837 int i, range_found = 0, num_entries;
1838 uint8_t bp_mask;
1839
1840 if (generic_range_table(flash, &wp, &num_entries))
1841 return -1;
1842
1843 bp_mask = ((1 << (wp->sr1.bp0_pos + wp->sr1.bp_bits)) - 1) - \
1844 ((1 << wp->sr1.bp0_pos) - 1);
1845
1846 for (i = 0, r = &wp->ranges[0]; i < num_entries; i++, r++) {
1847 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
1848 start, len, r->range.start, r->range.len);
1849 if ((start == r->range.start) && (len == r->range.len)) {
1850 *status &= ~(bp_mask);
1851 *status |= r->bp << (wp->sr1.bp0_pos);
David Hendricks148a4bf2015-03-13 21:02:42 -07001852
1853 if (wp->set_modifier_bits) {
1854 if (wp->set_modifier_bits(flash, &r->m) < 0) {
1855 msg_cerr("error setting modifier "
1856 "bits for range.\n");
1857 return -1;
1858 }
1859 }
1860
David Hendrickse0512a72014-07-15 20:30:47 -07001861 range_found = 1;
1862 break;
1863 }
1864 }
1865
1866 if (!range_found) {
1867 msg_cerr("matching range not found\n");
1868 return -1;
1869 }
1870 return 0;
1871}
1872
1873static int generic_status_to_range(const struct flashchip *flash,
1874 const uint8_t sr1, unsigned int *start, unsigned int *len)
1875{
1876 struct generic_wp *wp;
1877 struct generic_range *r;
Duncan Laurie04ca1172015-03-12 09:25:34 -07001878 int num_entries, i, status_found = 0;
David Hendrickse0512a72014-07-15 20:30:47 -07001879 uint8_t sr1_bp;
David Hendricks148a4bf2015-03-13 21:02:42 -07001880 struct generic_modifier_bits m;
David Hendrickse0512a72014-07-15 20:30:47 -07001881
1882 if (generic_range_table(flash, &wp, &num_entries))
1883 return -1;
1884
David Hendricks148a4bf2015-03-13 21:02:42 -07001885 /* modifier bits may be compared more than once, so get them here */
1886 if (wp->get_modifier_bits) {
1887 if (wp->get_modifier_bits(flash, &m) < 0)
1888 return -1;
1889 }
1890
David Hendrickse0512a72014-07-15 20:30:47 -07001891 sr1_bp = (sr1 >> wp->sr1.bp0_pos) & ((1 << wp->sr1.bp_bits) - 1);
1892
1893 for (i = 0, r = &wp->ranges[0]; i < num_entries; i++, r++) {
David Hendricks148a4bf2015-03-13 21:02:42 -07001894 if (wp->get_modifier_bits) {
1895 if (memcmp(&m, &r->m, sizeof(m)))
1896 continue;
1897 }
David Hendrickse0512a72014-07-15 20:30:47 -07001898 msg_cspew("comparing 0x%02x 0x%02x\n", sr1_bp, r->bp);
1899 if (sr1_bp == r->bp) {
1900 *start = r->range.start;
1901 *len = r->range.len;
1902 status_found = 1;
1903 break;
1904 }
1905 }
1906
1907 if (!status_found) {
1908 msg_cerr("matching status not found\n");
1909 return -1;
1910 }
1911 return 0;
1912}
1913
1914/* Given a [start, len], this function calls generic_range_to_status() to
1915 * convert it to flash-chip-specific range bits, then sets into status register.
1916 */
1917static int generic_set_range(const struct flashchip *flash,
1918 unsigned int start, unsigned int len)
1919{
1920 uint8_t status, expected;
1921
1922 status = spi_read_status_register();
1923 msg_cdbg("%s: old status: 0x%02x\n", __func__, status);
1924
1925 expected = status; /* preserve non-bp bits */
1926 if (generic_range_to_status(flash, start, len, &expected))
1927 return -1;
1928
David Hendricks60824042014-12-11 17:22:06 -08001929 spi_write_status_register(flash, expected);
David Hendrickse0512a72014-07-15 20:30:47 -07001930
1931 status = spi_read_status_register();
1932 msg_cdbg("%s: new status: 0x%02x\n", __func__, status);
1933 if (status != expected) {
1934 msg_cerr("expected=0x%02x, but actual=0x%02x.\n",
1935 expected, status);
1936 return 1;
1937 }
1938
1939 return 0;
1940}
1941
1942/* Set/clear the status regsiter write protect bit in SR1. */
1943static int generic_set_srp0(const struct flashchip *flash, int enable)
1944{
1945 uint8_t status, expected;
1946 struct generic_wp *wp;
1947 int num_entries;
1948
1949 if (generic_range_table(flash, &wp, &num_entries))
1950 return -1;
1951
1952 expected = spi_read_status_register();
1953 msg_cdbg("%s: old status: 0x%02x\n", __func__, expected);
1954
1955 if (enable)
1956 expected |= 1 << wp->sr1.srp_pos;
1957 else
1958 expected &= ~(1 << wp->sr1.srp_pos);
1959
David Hendricks60824042014-12-11 17:22:06 -08001960 spi_write_status_register(flash, expected);
David Hendrickse0512a72014-07-15 20:30:47 -07001961
1962 status = spi_read_status_register();
1963 msg_cdbg("%s: new status: 0x%02x\n", __func__, status);
1964 if (status != expected)
1965 return -1;
1966
1967 return 0;
1968}
1969
1970static int generic_enable_writeprotect(const struct flashchip *flash,
1971 enum wp_mode wp_mode)
1972{
1973 int ret;
1974
1975 switch (wp_mode) {
1976 case WP_MODE_HARDWARE:
1977 ret = generic_set_srp0(flash, 1);
1978 break;
1979 default:
1980 msg_cerr("%s(): unsupported write-protect mode\n", __func__);
1981 return 1;
1982 }
1983
1984 if (ret)
1985 msg_cerr("%s(): error=%d.\n", __func__, ret);
1986 return ret;
1987}
1988
1989static int generic_disable_writeprotect(const struct flashchip *flash)
1990{
1991 int ret;
1992
1993 ret = generic_set_srp0(flash, 0);
1994 if (ret)
1995 msg_cerr("%s(): error=%d.\n", __func__, ret);
1996 return ret;
1997}
1998
1999static int generic_list_ranges(const struct flashchip *flash)
2000{
2001 struct generic_wp *wp;
2002 struct generic_range *r;
2003 int i, num_entries;
2004
2005 if (generic_range_table(flash, &wp, &num_entries))
2006 return -1;
2007
2008 r = &wp->ranges[0];
2009 for (i = 0; i < num_entries; i++) {
2010 msg_cinfo("start: 0x%06x, length: 0x%06x\n",
2011 r->range.start, r->range.len);
2012 r++;
2013 }
2014
2015 return 0;
2016}
2017
2018static int generic_wp_status(const struct flashchip *flash)
2019{
2020 uint8_t sr1;
2021 unsigned int start, len;
2022 int ret = 0;
2023 struct generic_wp *wp;
David Hendrickse0512a72014-07-15 20:30:47 -07002024 int num_entries, wp_en;
2025
2026 if (generic_range_table(flash, &wp, &num_entries))
2027 return -1;
2028
2029 sr1 = spi_read_status_register();
2030 wp_en = (sr1 >> wp->sr1.srp_pos) & 1;
2031
2032 msg_cinfo("WP: status: 0x%04x\n", sr1);
2033 msg_cinfo("WP: status.srp0: %x\n", wp_en);
2034 /* FIXME: SRP1 is not really generic, but we probably should print
2035 * it anyway to have consistent output. #legacycruft */
2036 msg_cinfo("WP: status.srp1: %x\n", 0);
2037 msg_cinfo("WP: write protect is %s.\n",
2038 wp_en ? "enabled" : "disabled");
2039
2040 msg_cinfo("WP: write protect range: ");
2041 if (generic_status_to_range(flash, sr1, &start, &len)) {
2042 msg_cinfo("(cannot resolve the range)\n");
2043 ret = -1;
2044 } else {
2045 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
2046 }
2047
2048 return ret;
2049}
2050
2051struct wp wp_generic = {
2052 .list_ranges = generic_list_ranges,
2053 .set_range = generic_set_range,
2054 .enable = generic_enable_writeprotect,
2055 .disable = generic_disable_writeprotect,
2056 .wp_status = generic_wp_status,
2057};