blob: 34f923e526feaa313fc09cc95f712b258ca64a34 [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 *
David Hendricksd1c55d72010-08-24 15:14:19 -070016 */
17
David Hendricksf7924d12010-06-10 21:26:44 -070018#include <stdlib.h>
19#include <string.h>
20
21#include "flash.h"
22#include "flashchips.h"
23#include "chipdrivers.h"
Louis Yung-Chieh Lo52aa9302010-09-06 10:45:02 +080024#include "spi.h"
David Hendricks23cd7782010-08-25 12:42:38 -070025#include "writeprotect.h"
David Hendricksf7924d12010-06-10 21:26:44 -070026
David Hendricks1c09f802012-10-03 11:03:48 -070027/*
David Hendricksf7924d12010-06-10 21:26:44 -070028 * The following procedures rely on look-up tables to match the user-specified
29 * range with the chip's supported ranges. This turned out to be the most
30 * elegant approach since diferent flash chips use different levels of
31 * granularity and methods to determine protected ranges. In other words,
David Hendrickse0512a72014-07-15 20:30:47 -070032 * be stupid and simple since clever arithmetic will not work for many chips.
David Hendricksf7924d12010-06-10 21:26:44 -070033 */
34
35struct wp_range {
36 unsigned int start; /* starting address */
37 unsigned int len; /* len */
38};
39
40enum bit_state {
41 OFF = 0,
42 ON = 1,
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +080043 X = -1 /* don't care. Must be bigger than max # of bp. */
David Hendricksf7924d12010-06-10 21:26:44 -070044};
45
David Hendrickse0512a72014-07-15 20:30:47 -070046/*
47 * Generic write-protection schema for 25-series SPI flash chips. This assumes
48 * there is a status register that contains one or more consecutive bits which
49 * determine which address range is protected.
50 */
51
52struct status_register_layout {
53 int bp0_pos; /* position of BP0 */
54 int bp_bits; /* number of block protect bits */
55 int srp_pos; /* position of status register protect enable bit */
56};
57
58struct generic_range {
David Hendricks148a4bf2015-03-13 21:02:42 -070059 struct generic_modifier_bits m;
David Hendrickse0512a72014-07-15 20:30:47 -070060 unsigned int bp; /* block protect bitfield */
61 struct wp_range range;
62};
63
64struct generic_wp {
65 struct status_register_layout sr1; /* status register 1 */
66 struct generic_range *ranges;
David Hendricks148a4bf2015-03-13 21:02:42 -070067
68 /*
69 * Some chips store modifier bits in one or more special control
70 * registers instead of the status register like many older SPI NOR
71 * flash chips did. get_modifier_bits() and set_modifier_bits() will do
72 * any chip-specific operations necessary to get/set these bit values.
73 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -070074 int (*get_modifier_bits)(const struct flashctx *flash,
David Hendricks148a4bf2015-03-13 21:02:42 -070075 struct generic_modifier_bits *m);
Souvik Ghoshd75cd672016-06-17 14:21:39 -070076 int (*set_modifier_bits)(const struct flashctx *flash,
David Hendricks148a4bf2015-03-13 21:02:42 -070077 struct generic_modifier_bits *m);
David Hendrickse0512a72014-07-15 20:30:47 -070078};
79
80/*
81 * The following ranges and functions are useful for representing Winbond-
82 * style writeprotect schema in which there are typically 5 bits of
83 * relevant information stored in status register 1:
84 * sec: This bit indicates the units (sectors vs. blocks)
85 * tb: The top-bottom bit indicates if the affected range is at the top of
86 * the flash memory's address space or at the bottom.
Duncan Laurie1801f7c2019-01-09 18:02:51 -080087 * bp: Bitmask representing the number of affected sectors/blocks.
David Hendrickse0512a72014-07-15 20:30:47 -070088 */
David Hendricksf7924d12010-06-10 21:26:44 -070089struct w25q_range {
Duncan Laurie1801f7c2019-01-09 18:02:51 -080090 enum bit_state sec; /* if 1, bp bits describe sectors */
David Hendricksf7924d12010-06-10 21:26:44 -070091 enum bit_state tb; /* top/bottom select */
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +080092 int bp; /* block protect bitfield */
David Hendricksf7924d12010-06-10 21:26:44 -070093 struct wp_range range;
94};
95
David Hendrickse0512a72014-07-15 20:30:47 -070096/*
97 * Mask to extract write-protect enable and range bits
98 * Status register 1:
99 * SRP0: bit 7
100 * range(BP2-BP0): bit 4-2
Duncan Laurie1801f7c2019-01-09 18:02:51 -0800101 * range(BP3-BP0): bit 5-2 (large chips)
David Hendrickse0512a72014-07-15 20:30:47 -0700102 * Status register 2:
103 * SRP1: bit 1
104 */
105#define MASK_WP_AREA (0x9C)
Duncan Laurie1801f7c2019-01-09 18:02:51 -0800106#define MASK_WP_AREA_LARGE (0x9C)
David Hendrickse0512a72014-07-15 20:30:47 -0700107#define MASK_WP2_AREA (0x01)
108
David Hendricks57566ed2010-08-16 18:24:45 -0700109struct w25q_range en25f40_ranges[] = {
110 { X, X, 0, {0, 0} }, /* none */
111 { 0, 0, 0x1, {0x000000, 504 * 1024} },
112 { 0, 0, 0x2, {0x000000, 496 * 1024} },
113 { 0, 0, 0x3, {0x000000, 480 * 1024} },
114 { 0, 0, 0x4, {0x000000, 448 * 1024} },
115 { 0, 0, 0x5, {0x000000, 384 * 1024} },
116 { 0, 0, 0x6, {0x000000, 256 * 1024} },
117 { 0, 0, 0x7, {0x000000, 512 * 1024} },
118};
119
David Hendrickse185bf22011-05-24 15:34:18 -0700120struct w25q_range en25q40_ranges[] = {
121 { 0, 0, 0, {0, 0} }, /* none */
122 { 0, 0, 0x1, {0x000000, 504 * 1024} },
123 { 0, 0, 0x2, {0x000000, 496 * 1024} },
124 { 0, 0, 0x3, {0x000000, 480 * 1024} },
125
126 { 0, 1, 0x0, {0x000000, 448 * 1024} },
127 { 0, 1, 0x1, {0x000000, 384 * 1024} },
128 { 0, 1, 0x2, {0x000000, 256 * 1024} },
129 { 0, 1, 0x3, {0x000000, 512 * 1024} },
130};
131
132struct w25q_range en25q80_ranges[] = {
133 { 0, 0, 0, {0, 0} }, /* none */
134 { 0, 0, 0x1, {0x000000, 1016 * 1024} },
135 { 0, 0, 0x2, {0x000000, 1008 * 1024} },
136 { 0, 0, 0x3, {0x000000, 992 * 1024} },
137 { 0, 0, 0x4, {0x000000, 960 * 1024} },
138 { 0, 0, 0x5, {0x000000, 896 * 1024} },
139 { 0, 0, 0x6, {0x000000, 768 * 1024} },
140 { 0, 0, 0x7, {0x000000, 1024 * 1024} },
141};
142
143struct w25q_range en25q32_ranges[] = {
144 { 0, 0, 0, {0, 0} }, /* none */
145 { 0, 0, 0x1, {0x000000, 4032 * 1024} },
146 { 0, 0, 0x2, {0x000000, 3968 * 1024} },
147 { 0, 0, 0x3, {0x000000, 3840 * 1024} },
148 { 0, 0, 0x4, {0x000000, 3584 * 1024} },
149 { 0, 0, 0x5, {0x000000, 3072 * 1024} },
150 { 0, 0, 0x6, {0x000000, 2048 * 1024} },
151 { 0, 0, 0x7, {0x000000, 4096 * 1024} },
152
153 { 0, 1, 0, {0, 0} }, /* none */
154 { 0, 1, 0x1, {0x010000, 4032 * 1024} },
155 { 0, 1, 0x2, {0x020000, 3968 * 1024} },
156 { 0, 1, 0x3, {0x040000, 3840 * 1024} },
157 { 0, 1, 0x4, {0x080000, 3584 * 1024} },
158 { 0, 1, 0x5, {0x100000, 3072 * 1024} },
159 { 0, 1, 0x6, {0x200000, 2048 * 1024} },
160 { 0, 1, 0x7, {0x000000, 4096 * 1024} },
161};
162
163struct w25q_range en25q64_ranges[] = {
164 { 0, 0, 0, {0, 0} }, /* none */
165 { 0, 0, 0x1, {0x000000, 8128 * 1024} },
166 { 0, 0, 0x2, {0x000000, 8064 * 1024} },
167 { 0, 0, 0x3, {0x000000, 7936 * 1024} },
168 { 0, 0, 0x4, {0x000000, 7680 * 1024} },
169 { 0, 0, 0x5, {0x000000, 7168 * 1024} },
170 { 0, 0, 0x6, {0x000000, 6144 * 1024} },
171 { 0, 0, 0x7, {0x000000, 8192 * 1024} },
172
173 { 0, 1, 0, {0, 0} }, /* none */
174 { 0, 1, 0x1, {0x010000, 8128 * 1024} },
175 { 0, 1, 0x2, {0x020000, 8064 * 1024} },
176 { 0, 1, 0x3, {0x040000, 7936 * 1024} },
177 { 0, 1, 0x4, {0x080000, 7680 * 1024} },
178 { 0, 1, 0x5, {0x100000, 7168 * 1024} },
179 { 0, 1, 0x6, {0x200000, 6144 * 1024} },
180 { 0, 1, 0x7, {0x000000, 8192 * 1024} },
181};
182
183struct w25q_range en25q128_ranges[] = {
184 { 0, 0, 0, {0, 0} }, /* none */
185 { 0, 0, 0x1, {0x000000, 16320 * 1024} },
186 { 0, 0, 0x2, {0x000000, 16256 * 1024} },
187 { 0, 0, 0x3, {0x000000, 16128 * 1024} },
188 { 0, 0, 0x4, {0x000000, 15872 * 1024} },
189 { 0, 0, 0x5, {0x000000, 15360 * 1024} },
190 { 0, 0, 0x6, {0x000000, 14336 * 1024} },
191 { 0, 0, 0x7, {0x000000, 16384 * 1024} },
192
193 { 0, 1, 0, {0, 0} }, /* none */
194 { 0, 1, 0x1, {0x010000, 16320 * 1024} },
195 { 0, 1, 0x2, {0x020000, 16256 * 1024} },
196 { 0, 1, 0x3, {0x040000, 16128 * 1024} },
197 { 0, 1, 0x4, {0x080000, 15872 * 1024} },
198 { 0, 1, 0x5, {0x100000, 15360 * 1024} },
199 { 0, 1, 0x6, {0x200000, 14336 * 1024} },
200 { 0, 1, 0x7, {0x000000, 16384 * 1024} },
201};
202
Marc Jonesb2f90022014-04-29 17:37:23 -0600203struct w25q_range en25s64_ranges[] = {
204 { 0, 0, 0, {0, 0} }, /* none */
205 { 0, 0, 0x1, {0x000000, 8064 * 1024} },
206 { 0, 0, 0x2, {0x000000, 7936 * 1024} },
207 { 0, 0, 0x3, {0x000000, 7680 * 1024} },
208 { 0, 0, 0x4, {0x000000, 7168 * 1024} },
209 { 0, 0, 0x5, {0x000000, 6144 * 1024} },
210 { 0, 0, 0x6, {0x000000, 4096 * 1024} },
211 { 0, 0, 0x7, {0x000000, 8192 * 1024} },
212
213 { 0, 1, 0, {0, 0} }, /* none */
214 { 0, 1, 0x1, {0x7e0000, 128 * 1024} },
215 { 0, 1, 0x2, {0x7c0000, 256 * 1024} },
216 { 0, 1, 0x3, {0x780000, 512 * 1024} },
217 { 0, 1, 0x4, {0x700000, 1024 * 1024} },
218 { 0, 1, 0x5, {0x600000, 2048 * 1024} },
219 { 0, 1, 0x6, {0x400000, 4096 * 1024} },
220 { 0, 1, 0x7, {0x000000, 8192 * 1024} },
221};
222
David Hendricksf8f00c72011-02-01 12:39:46 -0800223/* mx25l1005 ranges also work for the mx25l1005c */
224static struct w25q_range mx25l1005_ranges[] = {
225 { X, X, 0, {0, 0} }, /* none */
226 { X, X, 0x1, {0x010000, 64 * 1024} },
227 { X, X, 0x2, {0x000000, 128 * 1024} },
228 { X, X, 0x3, {0x000000, 128 * 1024} },
229};
230
231static struct w25q_range mx25l2005_ranges[] = {
232 { X, X, 0, {0, 0} }, /* none */
233 { X, X, 0x1, {0x030000, 64 * 1024} },
234 { X, X, 0x2, {0x020000, 128 * 1024} },
235 { X, X, 0x3, {0x000000, 256 * 1024} },
236};
237
238static struct w25q_range mx25l4005_ranges[] = {
239 { X, X, 0, {0, 0} }, /* none */
240 { X, X, 0x1, {0x070000, 64 * 1 * 1024} }, /* block 7 */
241 { X, X, 0x2, {0x060000, 64 * 2 * 1024} }, /* blocks 6-7 */
242 { X, X, 0x3, {0x040000, 64 * 4 * 1024} }, /* blocks 4-7 */
243 { X, X, 0x4, {0x000000, 512 * 1024} },
244 { X, X, 0x5, {0x000000, 512 * 1024} },
245 { X, X, 0x6, {0x000000, 512 * 1024} },
246 { X, X, 0x7, {0x000000, 512 * 1024} },
247};
248
249static struct w25q_range mx25l8005_ranges[] = {
250 { X, X, 0, {0, 0} }, /* none */
251 { X, X, 0x1, {0x0f0000, 64 * 1 * 1024} }, /* block 15 */
252 { X, X, 0x2, {0x0e0000, 64 * 2 * 1024} }, /* blocks 14-15 */
253 { X, X, 0x3, {0x0c0000, 64 * 4 * 1024} }, /* blocks 12-15 */
254 { X, X, 0x4, {0x080000, 64 * 8 * 1024} }, /* blocks 8-15 */
255 { X, X, 0x5, {0x000000, 1024 * 1024} },
256 { X, X, 0x6, {0x000000, 1024 * 1024} },
257 { X, X, 0x7, {0x000000, 1024 * 1024} },
258};
259
260#if 0
261/* FIXME: mx25l1605 has the same IDs as the mx25l1605d */
262static struct w25q_range mx25l1605_ranges[] = {
263 { X, X, 0, {0, 0} }, /* none */
264 { X, X, 0x1, {0x1f0000, 64 * 1024} }, /* block 31 */
265 { X, X, 0x2, {0x1e0000, 128 * 1024} }, /* blocks 30-31 */
266 { X, X, 0x3, {0x1c0000, 256 * 1024} }, /* blocks 28-31 */
267 { X, X, 0x4, {0x180000, 512 * 1024} }, /* blocks 24-31 */
268 { X, X, 0x4, {0x100000, 1024 * 1024} }, /* blocks 16-31 */
269 { X, X, 0x6, {0x000000, 2048 * 1024} },
270 { X, X, 0x7, {0x000000, 2048 * 1024} },
271};
272#endif
273
274#if 0
275/* FIXME: mx25l6405 has the same IDs as the mx25l6405d */
276static struct w25q_range mx25l6405_ranges[] = {
277 { X, 0, 0, {0, 0} }, /* none */
278 { X, 0, 0x1, {0x7f0000, 64 * 1 * 1024} }, /* block 127 */
279 { X, 0, 0x2, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */
280 { X, 0, 0x3, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */
281 { X, 0, 0x4, {0x780000, 64 * 8 * 1024} }, /* blocks 120-127 */
282 { X, 0, 0x5, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */
283 { X, 0, 0x6, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */
284 { X, 0, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
285
286 { X, 1, 0x0, {0x000000, 8192 * 1024} },
287 { X, 1, 0x1, {0x000000, 8192 * 1024} },
288 { X, 1, 0x2, {0x000000, 8192 * 1024} },
289 { X, 1, 0x3, {0x000000, 8192 * 1024} },
290 { X, 1, 0x4, {0x000000, 8192 * 1024} },
291 { X, 1, 0x5, {0x000000, 8192 * 1024} },
292 { X, 1, 0x6, {0x000000, 8192 * 1024} },
293 { X, 1, 0x7, {0x000000, 8192 * 1024} },
294};
295#endif
296
297static struct w25q_range mx25l1605d_ranges[] = {
298 { X, 0, 0, {0, 0} }, /* none */
299 { X, 0, 0x1, {0x1f0000, 64 * 1 * 1024} }, /* block 31 */
300 { X, 0, 0x2, {0x1e0000, 64 * 2 * 1024} }, /* blocks 30-31 */
301 { X, 0, 0x3, {0x1c0000, 64 * 4 * 1024} }, /* blocks 28-31 */
302 { X, 0, 0x4, {0x180000, 64 * 8 * 1024} }, /* blocks 24-31 */
303 { X, 0, 0x5, {0x100000, 64 * 16 * 1024} }, /* blocks 16-31 */
304 { X, 0, 0x6, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
305 { X, 0, 0x7, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
306
307 { X, 1, 0x0, {0x000000, 2048 * 1024} },
308 { X, 1, 0x1, {0x000000, 2048 * 1024} },
309 { X, 1, 0x2, {0x000000, 64 * 16 * 1024} }, /* blocks 0-15 */
310 { X, 1, 0x3, {0x000000, 64 * 24 * 1024} }, /* blocks 0-23 */
311 { X, 1, 0x4, {0x000000, 64 * 28 * 1024} }, /* blocks 0-27 */
312 { X, 1, 0x5, {0x000000, 64 * 30 * 1024} }, /* blocks 0-29 */
313 { X, 1, 0x6, {0x000000, 64 * 31 * 1024} }, /* blocks 0-30 */
314 { X, 1, 0x7, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
315};
316
317/* FIXME: Is there an mx25l3205 (without a trailing letter)? */
David Hendricksac72e362010-08-16 18:20:03 -0700318static struct w25q_range mx25l3205d_ranges[] = {
319 { X, 0, 0, {0, 0} }, /* none */
320 { X, 0, 0x1, {0x3f0000, 64 * 1024} },
321 { X, 0, 0x2, {0x3e0000, 128 * 1024} },
322 { X, 0, 0x3, {0x3c0000, 256 * 1024} },
323 { X, 0, 0x4, {0x380000, 512 * 1024} },
324 { X, 0, 0x5, {0x300000, 1024 * 1024} },
325 { X, 0, 0x6, {0x200000, 2048 * 1024} },
326 { X, 0, 0x7, {0x000000, 4096 * 1024} },
327
328 { X, 1, 0x0, {0x000000, 4096 * 1024} },
329 { X, 1, 0x1, {0x000000, 2048 * 1024} },
330 { X, 1, 0x2, {0x000000, 3072 * 1024} },
331 { X, 1, 0x3, {0x000000, 3584 * 1024} },
332 { X, 1, 0x4, {0x000000, 3840 * 1024} },
333 { X, 1, 0x5, {0x000000, 3968 * 1024} },
334 { X, 1, 0x6, {0x000000, 4032 * 1024} },
335 { X, 1, 0x7, {0x000000, 4096 * 1024} },
336};
337
Vincent Palatin87e092a2013-02-28 15:46:14 -0800338static struct w25q_range mx25u3235e_ranges[] = {
339 { X, 0, 0, {0, 0} }, /* none */
340 { 0, 0, 0x1, {0x3f0000, 64 * 1024} },
341 { 0, 0, 0x2, {0x3e0000, 128 * 1024} },
342 { 0, 0, 0x3, {0x3c0000, 256 * 1024} },
343 { 0, 0, 0x4, {0x380000, 512 * 1024} },
344 { 0, 0, 0x5, {0x300000, 1024 * 1024} },
345 { 0, 0, 0x6, {0x200000, 2048 * 1024} },
346 { 0, 0, 0x7, {0x000000, 4096 * 1024} },
347
348 { 0, 1, 0x0, {0x000000, 4096 * 1024} },
349 { 0, 1, 0x1, {0x000000, 2048 * 1024} },
350 { 0, 1, 0x2, {0x000000, 3072 * 1024} },
351 { 0, 1, 0x3, {0x000000, 3584 * 1024} },
352 { 0, 1, 0x4, {0x000000, 3840 * 1024} },
353 { 0, 1, 0x5, {0x000000, 3968 * 1024} },
354 { 0, 1, 0x6, {0x000000, 4032 * 1024} },
355 { 0, 1, 0x7, {0x000000, 4096 * 1024} },
356};
357
Jongpil66a96492014-08-14 17:59:06 +0900358static struct w25q_range mx25u6435e_ranges[] = {
359 { X, 0, 0, {0, 0} }, /* none */
360 { 0, 0, 0x1, {0x7f0000, 1 * 64 * 1024} }, /* block 127 */
361 { 0, 0, 0x2, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
362 { 0, 0, 0x3, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
363 { 0, 0, 0x4, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
364 { 0, 0, 0x5, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
365 { 0, 0, 0x6, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
366 { 0, 0, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
367
368 { 0, 1, 0x0, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
369 { 0, 1, 0x1, {0x000000, 96 * 64 * 1024} }, /* blocks 0-95 */
370 { 0, 1, 0x2, {0x000000, 112 * 64 * 1024} }, /* blocks 0-111 */
371 { 0, 1, 0x3, {0x000000, 120 * 64 * 1024} }, /* blocks 0-119 */
372 { 0, 1, 0x4, {0x000000, 124 * 64 * 1024} }, /* blocks 0-123 */
373 { 0, 1, 0x5, {0x000000, 126 * 64 * 1024} }, /* blocks 0-125 */
374 { 0, 1, 0x6, {0x000000, 127 * 64 * 1024} }, /* blocks 0-126 */
375 { 0, 1, 0x7, {0x000000, 128 * 64 * 1024} }, /* blocks 0-127 */
376};
377
Karthikeyan Ramasubramanianfb166b72019-06-24 12:38:55 -0600378#define MX25U12835E_TB (1 << 3)
379static struct w25q_range mx25u12835e_tb0_ranges[] = {
Paul Fagerburg90571582019-03-15 11:32:57 -0600380 { X, X, 0, {0, 0} }, /* none */
Alex Lu831c6092017-11-02 23:19:34 -0700381 { 0, 0, 0x1, {0xff0000, 1 * 64 * 1024} }, /* block 255 */
382 { 0, 0, 0x2, {0xfe0000, 2 * 64 * 1024} }, /* blocks 254-255 */
383 { 0, 0, 0x3, {0xfc0000, 4 * 64 * 1024} }, /* blocks 252-255 */
384 { 0, 0, 0x4, {0xf80000, 8 * 64 * 1024} }, /* blocks 248-255 */
385 { 0, 0, 0x5, {0xf00000, 16 * 64 * 1024} }, /* blocks 240-255 */
386 { 0, 0, 0x6, {0xe00000, 32 * 64 * 1024} }, /* blocks 224-255 */
387 { 0, 0, 0x7, {0xc00000, 64 * 64 * 1024} }, /* blocks 192-255 */
Paul Fagerburg90571582019-03-15 11:32:57 -0600388 { 0, 0, 0x8, {0x800000, 128 * 64 * 1024} }, /* blocks 128-255 */
389 { 0, 0, 0x9, {0x000000, 256 * 64 * 1024} }, /* blocks all */
390 { 0, 0, 0xa, {0x000000, 256 * 64 * 1024} }, /* blocks all */
391 { 0, 0, 0xb, {0x000000, 256 * 64 * 1024} }, /* blocks all */
392 { 0, 0, 0xc, {0x000000, 256 * 64 * 1024} }, /* blocks all */
393 { 0, 0, 0xd, {0x000000, 256 * 64 * 1024} }, /* blocks all */
394 { 0, 0, 0xe, {0x000000, 256 * 64 * 1024} }, /* blocks all */
395 { 0, 0, 0xf, {0x000000, 256 * 64 * 1024} }, /* blocks all */
Karthikeyan Ramasubramanianfb166b72019-06-24 12:38:55 -0600396};
Alex Lu831c6092017-11-02 23:19:34 -0700397
Karthikeyan Ramasubramanianfb166b72019-06-24 12:38:55 -0600398static struct w25q_range mx25u12835e_tb1_ranges[] = {
Paul Fagerburg90571582019-03-15 11:32:57 -0600399 { 0, 1, 0x1, {0x000000, 1 * 64 * 1024} }, /* block 0 */
400 { 0, 1, 0x2, {0x000000, 2 * 64 * 1024} }, /* blocks 0-1 */
401 { 0, 1, 0x3, {0x000000, 4 * 64 * 1024} }, /* blocks 0-3 */
402 { 0, 1, 0x4, {0x000000, 8 * 64 * 1024} }, /* blocks 0-7 */
403 { 0, 1, 0x5, {0x000000, 16 * 64 * 1024} }, /* blocks 0-15 */
404 { 0, 1, 0x6, {0x000000, 32 * 64 * 1024} }, /* blocks 0-31 */
405 { 0, 1, 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
406 { 0, 1, 0x8, {0x000000, 128 * 64 * 1024} }, /* blocks 0-127 */
407 { 0, 1, 0x9, {0x000000, 256 * 64 * 1024} }, /* blocks all */
408 { 0, 1, 0xa, {0x000000, 256 * 64 * 1024} }, /* blocks all */
409 { 0, 1, 0xb, {0x000000, 256 * 64 * 1024} }, /* blocks all */
410 { 0, 1, 0xc, {0x000000, 256 * 64 * 1024} }, /* blocks all */
411 { 0, 1, 0xd, {0x000000, 256 * 64 * 1024} }, /* blocks all */
412 { 0, 1, 0xe, {0x000000, 256 * 64 * 1024} }, /* blocks all */
413 { 0, 1, 0xf, {0x000000, 256 * 64 * 1024} }, /* blocks all */
Alex Lu831c6092017-11-02 23:19:34 -0700414};
415
David Hendricksbfa624b2012-07-24 12:47:59 -0700416static struct w25q_range n25q064_ranges[] = {
David Hendricksfe9123b2015-04-21 13:18:31 -0700417 /*
418 * Note: For N25Q064, sec (usually in bit position 6) is called BP3
419 * (block protect bit 3). It is only useful when all blocks are to
420 * be write-protected.
421 */
David Hendricks42a549a2015-04-22 11:25:07 -0700422 { 0, 0, 0, {0, 0} }, /* none */
David Hendricksbfa624b2012-07-24 12:47:59 -0700423
424 { 0, 0, 0x1, {0x7f0000, 64 * 1024} }, /* block 127 */
425 { 0, 0, 0x2, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
426 { 0, 0, 0x3, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
427 { 0, 0, 0x4, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
428 { 0, 0, 0x5, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
429 { 0, 0, 0x6, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
430 { 0, 0, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
431
David Hendricksfe9123b2015-04-21 13:18:31 -0700432 { 0, 1, 0x1, {0x000000, 64 * 1024} }, /* block 0 */
433 { 0, 1, 0x2, {0x000000, 2 * 64 * 1024} }, /* blocks 0-1 */
434 { 0, 1, 0x3, {0x000000, 4 * 64 * 1024} }, /* blocks 0-3 */
435 { 0, 1, 0x4, {0x000000, 8 * 64 * 1024} }, /* blocks 0-7 */
436 { 0, 1, 0x5, {0x000000, 16 * 64 * 1024} }, /* blocks 0-15 */
437 { 0, 1, 0x6, {0x000000, 32 * 64 * 1024} }, /* blocks 0-31 */
438 { 0, 1, 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
David Hendricksbfa624b2012-07-24 12:47:59 -0700439
440 { X, 1, 0x0, {0x000000, 128 * 64 * 1024} }, /* all */
441 { X, 1, 0x1, {0x000000, 128 * 64 * 1024} }, /* all */
442 { X, 1, 0x2, {0x000000, 128 * 64 * 1024} }, /* all */
443 { X, 1, 0x3, {0x000000, 128 * 64 * 1024} }, /* all */
444 { X, 1, 0x4, {0x000000, 128 * 64 * 1024} }, /* all */
445 { X, 1, 0x5, {0x000000, 128 * 64 * 1024} }, /* all */
446 { X, 1, 0x6, {0x000000, 128 * 64 * 1024} }, /* all */
447 { X, 1, 0x7, {0x000000, 128 * 64 * 1024} }, /* all */
448};
449
David Hendricksf7924d12010-06-10 21:26:44 -0700450static struct w25q_range w25q16_ranges[] = {
451 { X, X, 0, {0, 0} }, /* none */
452 { 0, 0, 0x1, {0x1f0000, 64 * 1024} },
453 { 0, 0, 0x2, {0x1e0000, 128 * 1024} },
454 { 0, 0, 0x3, {0x1c0000, 256 * 1024} },
455 { 0, 0, 0x4, {0x180000, 512 * 1024} },
456 { 0, 0, 0x5, {0x100000, 1024 * 1024} },
457
458 { 0, 1, 0x1, {0x000000, 64 * 1024} },
459 { 0, 1, 0x2, {0x000000, 128 * 1024} },
460 { 0, 1, 0x3, {0x000000, 256 * 1024} },
461 { 0, 1, 0x4, {0x000000, 512 * 1024} },
462 { 0, 1, 0x5, {0x000000, 1024 * 1024} },
463 { X, X, 0x6, {0x000000, 2048 * 1024} },
464 { X, X, 0x7, {0x000000, 2048 * 1024} },
465
466 { 1, 0, 0x1, {0x1ff000, 4 * 1024} },
467 { 1, 0, 0x2, {0x1fe000, 8 * 1024} },
468 { 1, 0, 0x3, {0x1fc000, 16 * 1024} },
469 { 1, 0, 0x4, {0x1f8000, 32 * 1024} },
Paul Fagerburg90571582019-03-15 11:32:57 -0600470 { 1, 0, 0x5, {0x1f8000, 32 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700471
472 { 1, 1, 0x1, {0x000000, 4 * 1024} },
473 { 1, 1, 0x2, {0x000000, 8 * 1024} },
474 { 1, 1, 0x3, {0x000000, 16 * 1024} },
Paul Fagerburg90571582019-03-15 11:32:57 -0600475 { 1, 1, 0x4, {0x000000, 32 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700476 { 1, 1, 0x5, {0x000000, 32 * 1024} },
477};
478
479static struct w25q_range w25q32_ranges[] = {
480 { X, X, 0, {0, 0} }, /* none */
481 { 0, 0, 0x1, {0x3f0000, 64 * 1024} },
482 { 0, 0, 0x2, {0x3e0000, 128 * 1024} },
483 { 0, 0, 0x3, {0x3c0000, 256 * 1024} },
484 { 0, 0, 0x4, {0x380000, 512 * 1024} },
485 { 0, 0, 0x5, {0x300000, 1024 * 1024} },
David Hendricks05653ff2010-06-15 16:05:12 -0700486 { 0, 0, 0x6, {0x200000, 2048 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700487
488 { 0, 1, 0x1, {0x000000, 64 * 1024} },
489 { 0, 1, 0x2, {0x000000, 128 * 1024} },
490 { 0, 1, 0x3, {0x000000, 256 * 1024} },
491 { 0, 1, 0x4, {0x000000, 512 * 1024} },
492 { 0, 1, 0x5, {0x000000, 1024 * 1024} },
493 { 0, 1, 0x6, {0x000000, 2048 * 1024} },
494 { X, X, 0x7, {0x000000, 4096 * 1024} },
495
496 { 1, 0, 0x1, {0x3ff000, 4 * 1024} },
497 { 1, 0, 0x2, {0x3fe000, 8 * 1024} },
498 { 1, 0, 0x3, {0x3fc000, 16 * 1024} },
499 { 1, 0, 0x4, {0x3f8000, 32 * 1024} },
Paul Fagerburg90571582019-03-15 11:32:57 -0600500 { 1, 0, 0x5, {0x3f8000, 32 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700501
502 { 1, 1, 0x1, {0x000000, 4 * 1024} },
503 { 1, 1, 0x2, {0x000000, 8 * 1024} },
504 { 1, 1, 0x3, {0x000000, 16 * 1024} },
505 { 1, 1, 0x4, {0x000000, 32 * 1024} },
506 { 1, 1, 0x5, {0x000000, 32 * 1024} },
507};
508
509static struct w25q_range w25q80_ranges[] = {
510 { X, X, 0, {0, 0} }, /* none */
511 { 0, 0, 0x1, {0x0f0000, 64 * 1024} },
512 { 0, 0, 0x2, {0x0e0000, 128 * 1024} },
513 { 0, 0, 0x3, {0x0c0000, 256 * 1024} },
514 { 0, 0, 0x4, {0x080000, 512 * 1024} },
515
516 { 0, 1, 0x1, {0x000000, 64 * 1024} },
517 { 0, 1, 0x2, {0x000000, 128 * 1024} },
518 { 0, 1, 0x3, {0x000000, 256 * 1024} },
519 { 0, 1, 0x4, {0x000000, 512 * 1024} },
David Hendricks05653ff2010-06-15 16:05:12 -0700520 { X, X, 0x6, {0x000000, 1024 * 1024} },
521 { X, X, 0x7, {0x000000, 1024 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700522
523 { 1, 0, 0x1, {0x1ff000, 4 * 1024} },
524 { 1, 0, 0x2, {0x1fe000, 8 * 1024} },
525 { 1, 0, 0x3, {0x1fc000, 16 * 1024} },
526 { 1, 0, 0x4, {0x1f8000, 32 * 1024} },
527 { 1, 0, 0x5, {0x1f8000, 32 * 1024} },
528
529 { 1, 1, 0x1, {0x000000, 4 * 1024} },
530 { 1, 1, 0x2, {0x000000, 8 * 1024} },
531 { 1, 1, 0x3, {0x000000, 16 * 1024} },
532 { 1, 1, 0x4, {0x000000, 32 * 1024} },
533 { 1, 1, 0x5, {0x000000, 32 * 1024} },
534};
535
David Hendricks2c4a76c2010-06-28 14:00:43 -0700536static struct w25q_range w25q64_ranges[] = {
537 { X, X, 0, {0, 0} }, /* none */
538
539 { 0, 0, 0x1, {0x7e0000, 128 * 1024} },
540 { 0, 0, 0x2, {0x7c0000, 256 * 1024} },
541 { 0, 0, 0x3, {0x780000, 512 * 1024} },
542 { 0, 0, 0x4, {0x700000, 1024 * 1024} },
543 { 0, 0, 0x5, {0x600000, 2048 * 1024} },
544 { 0, 0, 0x6, {0x400000, 4096 * 1024} },
545
546 { 0, 1, 0x1, {0x000000, 128 * 1024} },
547 { 0, 1, 0x2, {0x000000, 256 * 1024} },
548 { 0, 1, 0x3, {0x000000, 512 * 1024} },
549 { 0, 1, 0x4, {0x000000, 1024 * 1024} },
550 { 0, 1, 0x5, {0x000000, 2048 * 1024} },
551 { 0, 1, 0x6, {0x000000, 4096 * 1024} },
552 { X, X, 0x7, {0x000000, 8192 * 1024} },
553
554 { 1, 0, 0x1, {0x7ff000, 4 * 1024} },
555 { 1, 0, 0x2, {0x7fe000, 8 * 1024} },
556 { 1, 0, 0x3, {0x7fc000, 16 * 1024} },
557 { 1, 0, 0x4, {0x7f8000, 32 * 1024} },
558 { 1, 0, 0x5, {0x7f8000, 32 * 1024} },
559
560 { 1, 1, 0x1, {0x000000, 4 * 1024} },
561 { 1, 1, 0x2, {0x000000, 8 * 1024} },
562 { 1, 1, 0x3, {0x000000, 16 * 1024} },
563 { 1, 1, 0x4, {0x000000, 32 * 1024} },
564 { 1, 1, 0x5, {0x000000, 32 * 1024} },
565};
566
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700567static struct w25q_range w25rq128_cmp0_ranges[] = {
568 { X, X, 0, {0, 0} }, /* NONE */
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530569
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700570 { 0, 0, 0x1, {0xfc0000, 256 * 1024} }, /* Upper 1/64 */
571 { 0, 0, 0x2, {0xf80000, 512 * 1024} }, /* Upper 1/32 */
572 { 0, 0, 0x3, {0xf00000, 1024 * 1024} }, /* Upper 1/16 */
573 { 0, 0, 0x4, {0xe00000, 2048 * 1024} }, /* Upper 1/8 */
574 { 0, 0, 0x5, {0xc00000, 4096 * 1024} }, /* Upper 1/4 */
575 { 0, 0, 0x6, {0x800000, 8192 * 1024} }, /* Upper 1/2 */
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530576
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700577 { 0, 1, 0x1, {0x000000, 256 * 1024} }, /* Lower 1/64 */
578 { 0, 1, 0x2, {0x000000, 512 * 1024} }, /* Lower 1/32 */
579 { 0, 1, 0x3, {0x000000, 1024 * 1024} }, /* Lower 1/16 */
580 { 0, 1, 0x4, {0x000000, 2048 * 1024} }, /* Lower 1/8 */
581 { 0, 1, 0x5, {0x000000, 4096 * 1024} }, /* Lower 1/4 */
582 { 0, 1, 0x6, {0x000000, 8192 * 1024} }, /* Lower 1/2 */
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530583
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700584 { X, X, 0x7, {0x000000, 16384 * 1024} }, /* ALL */
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530585
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700586 { 1, 0, 0x1, {0xfff000, 4 * 1024} }, /* Upper 1/4096 */
587 { 1, 0, 0x2, {0xffe000, 8 * 1024} }, /* Upper 1/2048 */
588 { 1, 0, 0x3, {0xffc000, 16 * 1024} }, /* Upper 1/1024 */
589 { 1, 0, 0x4, {0xff8000, 32 * 1024} }, /* Upper 1/512 */
590 { 1, 0, 0x5, {0xff8000, 32 * 1024} }, /* Upper 1/512 */
591
592 { 1, 1, 0x1, {0x000000, 4 * 1024} }, /* Lower 1/4096 */
593 { 1, 1, 0x2, {0x000000, 8 * 1024} }, /* Lower 1/2048 */
594 { 1, 1, 0x3, {0x000000, 16 * 1024} }, /* Lower 1/1024 */
595 { 1, 1, 0x4, {0x000000, 32 * 1024} }, /* Lower 1/512 */
596 { 1, 1, 0x5, {0x000000, 32 * 1024} }, /* Lower 1/512 */
597};
598
599static struct w25q_range w25rq128_cmp1_ranges[] = {
600 { X, X, 0x0, {0x000000, 16 * 1024 * 1024} }, /* ALL */
601
602 { 0, 0, 0x1, {0x000000, 16128 * 1024} }, /* Lower 63/64 */
603 { 0, 0, 0x2, {0x000000, 15872 * 1024} }, /* Lower 31/32 */
604 { 0, 0, 0x3, {0x000000, 15 * 1024 * 1024} }, /* Lower 15/16 */
605 { 0, 0, 0x4, {0x000000, 14 * 1024 * 1024} }, /* Lower 7/8 */
606 { 0, 0, 0x5, {0x000000, 12 * 1024 * 1024} }, /* Lower 3/4 */
607 { 0, 0, 0x6, {0x000000, 8 * 1024 * 1024} }, /* Lower 1/2 */
608
609 { 0, 1, 0x1, {0x040000, 16128 * 1024} }, /* Upper 63/64 */
610 { 0, 1, 0x2, {0x080000, 15872 * 1024} }, /* Upper 31/32 */
611 { 0, 1, 0x3, {0x100000, 15 * 1024 * 1024} }, /* Upper 15/16 */
612 { 0, 1, 0x4, {0x200000, 14 * 1024 * 1024} }, /* Upper 7/8 */
613 { 0, 1, 0x5, {0x400000, 12 * 1024 * 1024} }, /* Upper 3/4 */
614 { 0, 1, 0x6, {0x800000, 8 * 1024 * 1024} }, /* Upper 1/2 */
615
616 { X, X, 0x7, {0x000000, 0} }, /* NONE */
617
618 { 1, 0, 0x1, {0x000000, 16380 * 1024} }, /* Lower 4095/4096 */
619 { 1, 0, 0x2, {0x000000, 16376 * 1024} }, /* Lower 2048/2048 */
620 { 1, 0, 0x3, {0x000000, 16368 * 1024} }, /* Lower 1023/1024 */
621 { 1, 0, 0x4, {0x000000, 16352 * 1024} }, /* Lower 511/512 */
622 { 1, 0, 0x5, {0x000000, 16352 * 1024} }, /* Lower 511/512 */
623
624 { 1, 1, 0x1, {0x001000, 16380 * 1024} }, /* Upper 4095/4096 */
625 { 1, 1, 0x2, {0x002000, 16376 * 1024} }, /* Upper 2047/2048 */
626 { 1, 1, 0x3, {0x004000, 16368 * 1024} }, /* Upper 1023/1024 */
627 { 1, 1, 0x4, {0x008000, 16352 * 1024} }, /* Upper 511/512 */
628 { 1, 1, 0x5, {0x008000, 16352 * 1024} }, /* Upper 511/512 */
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530629};
630
Duncan Laurie1801f7c2019-01-09 18:02:51 -0800631static struct w25q_range w25rq256_cmp0_ranges[] = {
632 { X, X, 0x0, {0x0000000, 0x0000000} }, /* NONE */
633
634 { X, 0, 0x1, {0x1ff0000, 64 * 1 * 1024} }, /* Upper 1/512 */
635 { X, 0, 0x2, {0x1fe0000, 64 * 2 * 1024} }, /* Upper 1/256 */
636 { X, 0, 0x3, {0x1fc0000, 64 * 4 * 1024} }, /* Upper 1/128 */
637 { X, 0, 0x4, {0x1f80000, 64 * 8 * 1024} }, /* Upper 1/64 */
638 { X, 0, 0x5, {0x1f00000, 64 * 16 * 1024} }, /* Upper 1/32 */
639 { X, 0, 0x6, {0x1e00000, 64 * 32 * 1024} }, /* Upper 1/16 */
640 { X, 0, 0x7, {0x1c00000, 64 * 64 * 1024} }, /* Upper 1/8 */
641 { X, 0, 0x8, {0x1800000, 64 * 128 * 1024} }, /* Upper 1/4 */
642 { X, 0, 0x9, {0x1000000, 64 * 256 * 1024} }, /* Upper 1/2 */
643
644 { X, 1, 0x1, {0x0000000, 64 * 1 * 1024} }, /* Lower 1/512 */
645 { X, 1, 0x2, {0x0000000, 64 * 2 * 1024} }, /* Lower 1/256 */
646 { X, 1, 0x3, {0x0000000, 64 * 4 * 1024} }, /* Lower 1/128 */
647 { X, 1, 0x4, {0x0000000, 64 * 8 * 1024} }, /* Lower 1/64 */
648 { X, 1, 0x5, {0x0000000, 64 * 16 * 1024} }, /* Lower 1/32 */
649 { X, 1, 0x6, {0x0000000, 64 * 32 * 1024} }, /* Lower 1/16 */
650 { X, 1, 0x7, {0x0000000, 64 * 64 * 1024} }, /* Lower 1/8 */
651 { X, 1, 0x8, {0x0000000, 64 * 128 * 1024} }, /* Lower 1/4 */
652 { X, 1, 0x9, {0x0000000, 64 * 256 * 1024} }, /* Lower 1/2 */
653
654 { X, X, 0xa, {0x0000000, 64 * 512 * 1024} }, /* ALL */
655 { X, X, 0xb, {0x0000000, 64 * 512 * 1024} }, /* ALL */
656 { X, X, 0xc, {0x0000000, 64 * 512 * 1024} }, /* ALL */
657 { X, X, 0xd, {0x0000000, 64 * 512 * 1024} }, /* ALL */
658 { X, X, 0xe, {0x0000000, 64 * 512 * 1024} }, /* ALL */
659 { X, X, 0xf, {0x0000000, 64 * 512 * 1024} }, /* ALL */
660};
661
662static struct w25q_range w25rq256_cmp1_ranges[] = {
663 { X, X, 0x0, {0x0000000, 64 * 512 * 1024} }, /* ALL */
664
665 { X, 0, 0x1, {0x0000000, 64 * 511 * 1024} }, /* Lower 511/512 */
666 { X, 0, 0x2, {0x0000000, 64 * 510 * 1024} }, /* Lower 255/256 */
667 { X, 0, 0x3, {0x0000000, 64 * 508 * 1024} }, /* Lower 127/128 */
668 { X, 0, 0x4, {0x0000000, 64 * 504 * 1024} }, /* Lower 63/64 */
669 { X, 0, 0x5, {0x0000000, 64 * 496 * 1024} }, /* Lower 31/32 */
670 { X, 0, 0x6, {0x0000000, 64 * 480 * 1024} }, /* Lower 15/16 */
671 { X, 0, 0x7, {0x0000000, 64 * 448 * 1024} }, /* Lower 7/8 */
672 { X, 0, 0x8, {0x0000000, 64 * 384 * 1024} }, /* Lower 3/4 */
673 { X, 0, 0x9, {0x0000000, 64 * 256 * 1024} }, /* Lower 1/2 */
674
675 { X, 1, 0x1, {0x0010000, 64 * 511 * 1024} }, /* Upper 511/512 */
676 { X, 1, 0x2, {0x0020000, 64 * 510 * 1024} }, /* Upper 255/256 */
677 { X, 1, 0x3, {0x0040000, 64 * 508 * 1024} }, /* Upper 127/128 */
678 { X, 1, 0x4, {0x0080000, 64 * 504 * 1024} }, /* Upper 63/64 */
679 { X, 1, 0x5, {0x0100000, 64 * 496 * 1024} }, /* Upper 31/32 */
680 { X, 1, 0x6, {0x0200000, 64 * 480 * 1024} }, /* Upper 15/16 */
681 { X, 1, 0x7, {0x0400000, 64 * 448 * 1024} }, /* Upper 7/8 */
682 { X, 1, 0x8, {0x0800000, 64 * 384 * 1024} }, /* Upper 3/4 */
683 { X, 1, 0x9, {0x1000000, 64 * 256 * 1024} }, /* Upper 1/2 */
684
685 { X, X, 0xa, {0x0000000, 0x0000000} }, /* NONE */
686 { X, X, 0xb, {0x0000000, 0x0000000} }, /* NONE */
687 { X, X, 0xc, {0x0000000, 0x0000000} }, /* NONE */
688 { X, X, 0xd, {0x0000000, 0x0000000} }, /* NONE */
689 { X, X, 0xe, {0x0000000, 0x0000000} }, /* NONE */
690 { X, X, 0xf, {0x0000000, 0x0000000} }, /* NONE */
691};
692
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800693struct w25q_range w25x10_ranges[] = {
694 { X, X, 0, {0, 0} }, /* none */
695 { 0, 0, 0x1, {0x010000, 64 * 1024} },
696 { 0, 1, 0x1, {0x000000, 64 * 1024} },
697 { X, X, 0x2, {0x000000, 128 * 1024} },
698 { X, X, 0x3, {0x000000, 128 * 1024} },
699};
700
701struct w25q_range w25x20_ranges[] = {
702 { X, X, 0, {0, 0} }, /* none */
703 { 0, 0, 0x1, {0x030000, 64 * 1024} },
704 { 0, 0, 0x2, {0x020000, 128 * 1024} },
705 { 0, 1, 0x1, {0x000000, 64 * 1024} },
706 { 0, 1, 0x2, {0x000000, 128 * 1024} },
707 { 0, X, 0x3, {0x000000, 256 * 1024} },
708};
709
David Hendricks470ca952010-08-13 14:01:53 -0700710struct w25q_range w25x40_ranges[] = {
711 { X, X, 0, {0, 0} }, /* none */
712 { 0, 0, 0x1, {0x070000, 64 * 1024} },
713 { 0, 0, 0x2, {0x060000, 128 * 1024} },
714 { 0, 0, 0x3, {0x040000, 256 * 1024} },
715 { 0, 1, 0x1, {0x000000, 64 * 1024} },
716 { 0, 1, 0x2, {0x000000, 128 * 1024} },
717 { 0, 1, 0x3, {0x000000, 256 * 1024} },
718 { 0, X, 0x4, {0x000000, 512 * 1024} },
David Hendricksb389abb2016-06-17 16:47:00 -0700719 { 0, X, 0x5, {0x000000, 512 * 1024} },
720 { 0, X, 0x6, {0x000000, 512 * 1024} },
721 { 0, X, 0x7, {0x000000, 512 * 1024} },
David Hendricks470ca952010-08-13 14:01:53 -0700722};
723
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800724struct w25q_range w25x80_ranges[] = {
725 { X, X, 0, {0, 0} }, /* none */
726 { 0, 0, 0x1, {0x0F0000, 64 * 1024} },
727 { 0, 0, 0x2, {0x0E0000, 128 * 1024} },
728 { 0, 0, 0x3, {0x0C0000, 256 * 1024} },
729 { 0, 0, 0x4, {0x080000, 512 * 1024} },
730 { 0, 1, 0x1, {0x000000, 64 * 1024} },
731 { 0, 1, 0x2, {0x000000, 128 * 1024} },
732 { 0, 1, 0x3, {0x000000, 256 * 1024} },
733 { 0, 1, 0x4, {0x000000, 512 * 1024} },
734 { 0, X, 0x5, {0x000000, 1024 * 1024} },
735 { 0, X, 0x6, {0x000000, 1024 * 1024} },
736 { 0, X, 0x7, {0x000000, 1024 * 1024} },
737};
738
Martin Rothf3c3d5f2017-04-28 14:56:41 -0600739static struct w25q_range gd25q40_cmp0_ranges[] = {
740 { X, X, 0, {0, 0} }, /* None */
741 { 0, 0, 0x1, {0x070000, 64 * 1024} },
742 { 0, 0, 0x2, {0x060000, 128 * 1024} },
743 { 0, 0, 0x3, {0x040000, 256 * 1024} },
744 { 0, 1, 0x1, {0x000000, 64 * 1024} },
745 { 0, 1, 0x2, {0x000000, 128 * 1024} },
746 { 0, 1, 0x3, {0x000000, 256 * 1024} },
747 { 0, X, 0x4, {0x000000, 512 * 1024} }, /* All */
748 { 0, X, 0x5, {0x000000, 512 * 1024} }, /* All */
749 { 0, X, 0x6, {0x000000, 512 * 1024} }, /* All */
750 { 0, X, 0x7, {0x000000, 512 * 1024} }, /* All */
751 { 1, 0, 0x1, {0x07F000, 4 * 1024} },
752 { 1, 0, 0x2, {0x07E000, 8 * 1024} },
753 { 1, 0, 0x3, {0x07C000, 16 * 1024} },
754 { 1, 0, 0x4, {0x078000, 32 * 1024} },
755 { 1, 0, 0x5, {0x078000, 32 * 1024} },
756 { 1, 0, 0x6, {0x078000, 32 * 1024} },
757 { 1, 1, 0x1, {0x000000, 4 * 1024} },
758 { 1, 1, 0x2, {0x000000, 8 * 1024} },
759 { 1, 1, 0x3, {0x000000, 16 * 1024} },
760 { 1, 1, 0x4, {0x000000, 32 * 1024} },
761 { 1, 1, 0x5, {0x000000, 32 * 1024} },
762 { 1, 1, 0x6, {0x000000, 32 * 1024} },
763 { 1, X, 0x7, {0x000000, 512 * 1024} }, /* All */
764};
765
766static struct w25q_range gd25q40_cmp1_ranges[] = {
767 { X, X, 0x0, {0x000000, 512 * 1024} }, /* ALL */
768 { 0, 0, 0x1, {0x000000, 448 * 1024} },
769 { 0, 0, 0x2, {0x000000, 384 * 1024} },
770 { 0, 0, 0x3, {0x000000, 256 * 1024} },
771
772 { 0, 1, 0x1, {0x010000, 448 * 1024} },
773 { 0, 1, 0x2, {0x020000, 384 * 1024} },
774 { 0, 1, 0x3, {0x040000, 256 * 1024} },
775
776 { 0, X, 0x4, {0x000000, 0} }, /* None */
777 { 0, X, 0x5, {0x000000, 0} }, /* None */
778 { 0, X, 0x6, {0x000000, 0} }, /* None */
779 { 0, X, 0x7, {0x000000, 0} }, /* None */
780
781 { 1, 0, 0x1, {0x000000, 508 * 1024} },
782 { 1, 0, 0x2, {0x000000, 504 * 1024} },
783 { 1, 0, 0x3, {0x000000, 496 * 1024} },
784 { 1, 0, 0x4, {0x000000, 480 * 1024} },
785 { 1, 0, 0x5, {0x000000, 480 * 1024} },
786 { 1, 0, 0x6, {0x000000, 480 * 1024} },
787
788 { 1, 1, 0x1, {0x001000, 508 * 1024} },
789 { 1, 1, 0x2, {0x002000, 504 * 1024} },
790 { 1, 1, 0x3, {0x004000, 496 * 1024} },
791 { 1, 1, 0x4, {0x008000, 480 * 1024} },
792 { 1, 1, 0x5, {0x008000, 480 * 1024} },
793 { 1, 1, 0x6, {0x008000, 480 * 1024} },
794
795 { 1, X, 0x7, {0x000000, 0} }, /* None */
796};
797
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700798static struct w25q_range gd25q64_ranges[] = {
799 { X, X, 0, {0, 0} }, /* none */
800 { 0, 0, 0x1, {0x7e0000, 128 * 1024} },
801 { 0, 0, 0x2, {0x7c0000, 256 * 1024} },
802 { 0, 0, 0x3, {0x780000, 512 * 1024} },
803 { 0, 0, 0x4, {0x700000, 1024 * 1024} },
804 { 0, 0, 0x5, {0x600000, 2048 * 1024} },
805 { 0, 0, 0x6, {0x400000, 4096 * 1024} },
806
807 { 0, 1, 0x1, {0x000000, 128 * 1024} },
808 { 0, 1, 0x2, {0x000000, 256 * 1024} },
809 { 0, 1, 0x3, {0x000000, 512 * 1024} },
810 { 0, 1, 0x4, {0x000000, 1024 * 1024} },
811 { 0, 1, 0x5, {0x000000, 2048 * 1024} },
812 { 0, 1, 0x6, {0x000000, 4096 * 1024} },
813 { X, X, 0x7, {0x000000, 8192 * 1024} },
814
815 { 1, 0, 0x1, {0x7ff000, 4 * 1024} },
816 { 1, 0, 0x2, {0x7fe000, 8 * 1024} },
817 { 1, 0, 0x3, {0x7fc000, 16 * 1024} },
818 { 1, 0, 0x4, {0x7f8000, 32 * 1024} },
819 { 1, 0, 0x5, {0x7f8000, 32 * 1024} },
820 { 1, 0, 0x6, {0x7f8000, 32 * 1024} },
821
822 { 1, 1, 0x1, {0x000000, 4 * 1024} },
823 { 1, 1, 0x2, {0x000000, 8 * 1024} },
824 { 1, 1, 0x3, {0x000000, 16 * 1024} },
825 { 1, 1, 0x4, {0x000000, 32 * 1024} },
826 { 1, 1, 0x5, {0x000000, 32 * 1024} },
827 { 1, 1, 0x6, {0x000000, 32 * 1024} },
828};
829
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +0800830static struct w25q_range a25l040_ranges[] = {
831 { X, X, 0x0, {0, 0} }, /* none */
832 { X, X, 0x1, {0x70000, 64 * 1024} },
833 { X, X, 0x2, {0x60000, 128 * 1024} },
834 { X, X, 0x3, {0x40000, 256 * 1024} },
835 { X, X, 0x4, {0x00000, 512 * 1024} },
836 { X, X, 0x5, {0x00000, 512 * 1024} },
837 { X, X, 0x6, {0x00000, 512 * 1024} },
838 { X, X, 0x7, {0x00000, 512 * 1024} },
839};
840
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700841static uint8_t do_read_status(const struct flashctx *flash)
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +0530842{
Patrick Georgif3fa2992017-02-02 16:24:44 +0100843 if (flash->chip->read_status)
844 return flash->chip->read_status(flash);
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +0530845 else
846 return spi_read_status_register(flash);
847}
848
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700849static int do_write_status(const struct flashctx *flash, int status)
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +0530850{
Patrick Georgif3fa2992017-02-02 16:24:44 +0100851 if (flash->chip->write_status)
852 return flash->chip->write_status(flash, status);
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +0530853 else
854 return spi_write_status_register(flash, status);
855}
856
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700857/* FIXME: Move to spi25.c if it's a JEDEC standard opcode */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700858static uint8_t w25q_read_status_register_2(const struct flashctx *flash)
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700859{
860 static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { 0x35 };
861 unsigned char readarr[2];
862 int ret;
863
864 /* Read Status Register */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700865 ret = spi_send_command(flash, sizeof(cmd), sizeof(readarr), cmd, readarr);
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700866 if (ret) {
867 /*
868 * FIXME: make this a benign failure for now in case we are
869 * unable to execute the opcode
870 */
871 msg_cdbg("RDSR2 failed!\n");
872 readarr[0] = 0x00;
873 }
874
875 return readarr[0];
876}
877
Karthikeyan Ramasubramanianfb166b72019-06-24 12:38:55 -0600878/* FIXME: Move to spi25.c if it's a JEDEC standard opcode */
879uint8_t mx25l_read_config_register(const struct flashctx *flash)
880{
881 static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { 0x15 };
882 unsigned char readarr[2]; /* leave room for dummy byte */
883 int ret;
884
885 ret = spi_send_command(flash, sizeof(cmd), sizeof(readarr), cmd, readarr);
886 if (ret) {
887 msg_cdbg("RDCR failed!\n");
888 readarr[0] = 0x00;
889 }
890
891 return readarr[0];
892}
893
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800894/* Given a flash chip, this function returns its range table. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700895static int w25_range_table(const struct flashctx *flash,
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800896 struct w25q_range **w25q_ranges,
897 int *num_entries)
David Hendricksf7924d12010-06-10 21:26:44 -0700898{
Karthikeyan Ramasubramanianfb166b72019-06-24 12:38:55 -0600899 uint8_t cr;
900
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800901 *w25q_ranges = 0;
902 *num_entries = 0;
David Hendricksf7924d12010-06-10 21:26:44 -0700903
Patrick Georgif3fa2992017-02-02 16:24:44 +0100904 switch (flash->chip->manufacture_id) {
David Hendricksd494b0a2010-08-16 16:28:50 -0700905 case WINBOND_NEX_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +0100906 switch(flash->chip->model_id) {
David Hendricksc801adb2010-12-09 16:58:56 -0800907 case WINBOND_NEX_W25X10:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800908 *w25q_ranges = w25x10_ranges;
909 *num_entries = ARRAY_SIZE(w25x10_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800910 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800911 case WINBOND_NEX_W25X20:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800912 *w25q_ranges = w25x20_ranges;
913 *num_entries = ARRAY_SIZE(w25x20_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800914 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800915 case WINBOND_NEX_W25X40:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800916 *w25q_ranges = w25x40_ranges;
917 *num_entries = ARRAY_SIZE(w25x40_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700918 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800919 case WINBOND_NEX_W25X80:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800920 *w25q_ranges = w25x80_ranges;
921 *num_entries = ARRAY_SIZE(w25x80_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800922 break;
Patrick Georgicc04a452017-02-06 12:14:43 +0100923 case WINBOND_NEX_W25Q80_V:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800924 *w25q_ranges = w25q80_ranges;
925 *num_entries = ARRAY_SIZE(w25q80_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700926 break;
Patrick Georgicc04a452017-02-06 12:14:43 +0100927 case WINBOND_NEX_W25Q16_V:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800928 *w25q_ranges = w25q16_ranges;
929 *num_entries = ARRAY_SIZE(w25q16_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700930 break;
Patrick Georgicc04a452017-02-06 12:14:43 +0100931 case WINBOND_NEX_W25Q32_V:
932 case WINBOND_NEX_W25Q32_W:
Edward O'Callaghand80cf712019-05-24 22:06:36 +1000933 case WINBOND_NEX_W25Q32JW:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800934 *w25q_ranges = w25q32_ranges;
935 *num_entries = ARRAY_SIZE(w25q32_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700936 break;
Patrick Georgicc04a452017-02-06 12:14:43 +0100937 case WINBOND_NEX_W25Q64_V:
938 case WINBOND_NEX_W25Q64_W:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800939 *w25q_ranges = w25q64_ranges;
940 *num_entries = ARRAY_SIZE(w25q64_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700941 break;
Alan Green77a95de2019-07-01 16:40:39 +1000942 case WINBOND_NEX_W25Q128_V_M:
Patrick Georgicc04a452017-02-06 12:14:43 +0100943 case WINBOND_NEX_W25Q128_V:
944 case WINBOND_NEX_W25Q128_W:
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700945 if (w25q_read_status_register_2(flash) & (1 << 6)) {
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700946 /* CMP == 1 */
947 *w25q_ranges = w25rq128_cmp1_ranges;
948 *num_entries = ARRAY_SIZE(w25rq128_cmp1_ranges);
949 } else {
950 /* CMP == 0 */
951 *w25q_ranges = w25rq128_cmp0_ranges;
952 *num_entries = ARRAY_SIZE(w25rq128_cmp0_ranges);
953 }
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530954 break;
Alan Green77a95de2019-07-01 16:40:39 +1000955 case WINBOND_NEX_W25Q256JV_M:
Duncan Laurie1801f7c2019-01-09 18:02:51 -0800956 if (w25q_read_status_register_2(flash) & (1 << 6)) {
957 /* CMP == 1 */
958 *w25q_ranges = w25rq256_cmp1_ranges;
959 *num_entries = ARRAY_SIZE(w25rq256_cmp1_ranges);
960 } else {
961 /* CMP == 0 */
962 *w25q_ranges = w25rq256_cmp0_ranges;
963 *num_entries = ARRAY_SIZE(w25rq256_cmp0_ranges);
964 }
965 break;
David Hendricksd494b0a2010-08-16 16:28:50 -0700966 default:
967 msg_cerr("%s() %d: WINBOND flash chip mismatch (0x%04x)"
968 ", aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +0100969 flash->chip->model_id);
David Hendricksd494b0a2010-08-16 16:28:50 -0700970 return -1;
971 }
David Hendricks2c4a76c2010-06-28 14:00:43 -0700972 break;
David Hendricks57566ed2010-08-16 18:24:45 -0700973 case EON_ID_NOPREFIX:
Patrick Georgif3fa2992017-02-02 16:24:44 +0100974 switch (flash->chip->model_id) {
David Hendricksc801adb2010-12-09 16:58:56 -0800975 case EON_EN25F40:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800976 *w25q_ranges = en25f40_ranges;
977 *num_entries = ARRAY_SIZE(en25f40_ranges);
David Hendricks57566ed2010-08-16 18:24:45 -0700978 break;
David Hendrickse185bf22011-05-24 15:34:18 -0700979 case EON_EN25Q40:
980 *w25q_ranges = en25q40_ranges;
981 *num_entries = ARRAY_SIZE(en25q40_ranges);
982 break;
983 case EON_EN25Q80:
984 *w25q_ranges = en25q80_ranges;
985 *num_entries = ARRAY_SIZE(en25q80_ranges);
986 break;
987 case EON_EN25Q32:
988 *w25q_ranges = en25q32_ranges;
989 *num_entries = ARRAY_SIZE(en25q32_ranges);
990 break;
991 case EON_EN25Q64:
992 *w25q_ranges = en25q64_ranges;
993 *num_entries = ARRAY_SIZE(en25q64_ranges);
994 break;
995 case EON_EN25Q128:
996 *w25q_ranges = en25q128_ranges;
997 *num_entries = ARRAY_SIZE(en25q128_ranges);
998 break;
Marc Jonesb2f90022014-04-29 17:37:23 -0600999 case EON_EN25S64:
1000 *w25q_ranges = en25s64_ranges;
1001 *num_entries = ARRAY_SIZE(en25s64_ranges);
1002 break;
David Hendricks57566ed2010-08-16 18:24:45 -07001003 default:
1004 msg_cerr("%s():%d: EON flash chip mismatch (0x%04x)"
1005 ", aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01001006 flash->chip->model_id);
David Hendricks57566ed2010-08-16 18:24:45 -07001007 return -1;
1008 }
1009 break;
David Hendricksc801adb2010-12-09 16:58:56 -08001010 case MACRONIX_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +01001011 switch (flash->chip->model_id) {
David Hendricksf8f00c72011-02-01 12:39:46 -08001012 case MACRONIX_MX25L1005:
1013 *w25q_ranges = mx25l1005_ranges;
1014 *num_entries = ARRAY_SIZE(mx25l1005_ranges);
1015 break;
1016 case MACRONIX_MX25L2005:
1017 *w25q_ranges = mx25l2005_ranges;
1018 *num_entries = ARRAY_SIZE(mx25l2005_ranges);
1019 break;
1020 case MACRONIX_MX25L4005:
1021 *w25q_ranges = mx25l4005_ranges;
1022 *num_entries = ARRAY_SIZE(mx25l4005_ranges);
1023 break;
1024 case MACRONIX_MX25L8005:
1025 *w25q_ranges = mx25l8005_ranges;
1026 *num_entries = ARRAY_SIZE(mx25l8005_ranges);
1027 break;
1028 case MACRONIX_MX25L1605:
1029 /* FIXME: MX25L1605 and MX25L1605D have different write
1030 * protection capabilities, but share IDs */
1031 *w25q_ranges = mx25l1605d_ranges;
1032 *num_entries = ARRAY_SIZE(mx25l1605d_ranges);
1033 break;
David Hendricksc801adb2010-12-09 16:58:56 -08001034 case MACRONIX_MX25L3205:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001035 *w25q_ranges = mx25l3205d_ranges;
1036 *num_entries = ARRAY_SIZE(mx25l3205d_ranges);
David Hendricksac72e362010-08-16 18:20:03 -07001037 break;
Vincent Palatin87e092a2013-02-28 15:46:14 -08001038 case MACRONIX_MX25U3235E:
1039 *w25q_ranges = mx25u3235e_ranges;
1040 *num_entries = ARRAY_SIZE(mx25u3235e_ranges);
1041 break;
Jongpil66a96492014-08-14 17:59:06 +09001042 case MACRONIX_MX25U6435E:
1043 *w25q_ranges = mx25u6435e_ranges;
1044 *num_entries = ARRAY_SIZE(mx25u6435e_ranges);
1045 break;
Alan Greendc0792e2019-07-01 15:01:34 +10001046 case MACRONIX_MX25U12835E:
Karthikeyan Ramasubramanianfb166b72019-06-24 12:38:55 -06001047 cr = mx25l_read_config_register(flash);
1048 if (cr & MX25U12835E_TB) { /* T/B == 1 */
1049 *w25q_ranges = mx25u12835e_tb1_ranges;
1050 *num_entries = ARRAY_SIZE(mx25u12835e_tb1_ranges);
1051 } else { /* T/B == 0 */
1052 *w25q_ranges = mx25u12835e_tb0_ranges;
1053 *num_entries = ARRAY_SIZE(mx25u12835e_tb0_ranges);
1054 }
Alex Lu831c6092017-11-02 23:19:34 -07001055 break;
David Hendricksac72e362010-08-16 18:20:03 -07001056 default:
1057 msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)"
1058 ", aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01001059 flash->chip->model_id);
David Hendricksac72e362010-08-16 18:20:03 -07001060 return -1;
1061 }
1062 break;
David Hendricksbfa624b2012-07-24 12:47:59 -07001063 case ST_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +01001064 switch(flash->chip->model_id) {
David Hendricksbfa624b2012-07-24 12:47:59 -07001065 case ST_N25Q064__1E:
1066 case ST_N25Q064__3E:
1067 *w25q_ranges = n25q064_ranges;
1068 *num_entries = ARRAY_SIZE(n25q064_ranges);
1069 break;
1070 default:
1071 msg_cerr("%s() %d: Micron flash chip mismatch"
1072 " (0x%04x), aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01001073 flash->chip->model_id);
David Hendricksbfa624b2012-07-24 12:47:59 -07001074 return -1;
1075 }
1076 break;
Bryan Freed9a0051f2012-05-22 16:06:09 -07001077 case GIGADEVICE_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +01001078 switch(flash->chip->model_id) {
Bryan Freed9a0051f2012-05-22 16:06:09 -07001079 case GIGADEVICE_GD25LQ32:
1080 *w25q_ranges = w25q32_ranges;
1081 *num_entries = ARRAY_SIZE(w25q32_ranges);
1082 break;
Martin Rothf3c3d5f2017-04-28 14:56:41 -06001083 case GIGADEVICE_GD25Q40:
1084 if (w25q_read_status_register_2(flash) & (1 << 6)) {
1085 /* CMP == 1 */
1086 *w25q_ranges = gd25q40_cmp1_ranges;
1087 *num_entries = ARRAY_SIZE(gd25q40_cmp1_ranges);
1088 } else {
1089 *w25q_ranges = gd25q40_cmp0_ranges;
1090 *num_entries = ARRAY_SIZE(gd25q40_cmp0_ranges);
1091 }
1092 break;
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -07001093 case GIGADEVICE_GD25Q64:
Marc Jonesb18734f2014-04-03 16:19:47 -06001094 case GIGADEVICE_GD25LQ64:
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -07001095 *w25q_ranges = gd25q64_ranges;
1096 *num_entries = ARRAY_SIZE(gd25q64_ranges);
1097 break;
Martin Roth1fd87ed2017-02-27 20:50:50 -07001098 case GIGADEVICE_GD25Q128:
Aaron Durbin6c957d72018-08-20 09:31:01 -06001099 case GIGADEVICE_GD25LQ128CD:
Martin Roth1fd87ed2017-02-27 20:50:50 -07001100 if (w25q_read_status_register_2(flash) & (1 << 6)) {
1101 /* CMP == 1 */
1102 *w25q_ranges = w25rq128_cmp1_ranges;
1103 *num_entries = ARRAY_SIZE(w25rq128_cmp1_ranges);
1104 } else {
1105 /* CMP == 0 */
1106 *w25q_ranges = w25rq128_cmp0_ranges;
1107 *num_entries = ARRAY_SIZE(w25rq128_cmp0_ranges);
1108 }
1109 break;
Duncan Laurie0c383552019-03-16 12:35:16 -07001110 case GIGADEVICE_GD25Q256D:
1111 *w25q_ranges = w25rq256_cmp0_ranges;
1112 *num_entries = ARRAY_SIZE(w25rq256_cmp0_ranges);
1113 break;
Bryan Freed9a0051f2012-05-22 16:06:09 -07001114 default:
1115 msg_cerr("%s() %d: GigaDevice flash chip mismatch"
1116 " (0x%04x), aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01001117 flash->chip->model_id);
Bryan Freed9a0051f2012-05-22 16:06:09 -07001118 return -1;
1119 }
1120 break;
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +08001121 case AMIC_ID_NOPREFIX:
Patrick Georgif3fa2992017-02-02 16:24:44 +01001122 switch(flash->chip->model_id) {
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +08001123 case AMIC_A25L040:
1124 *w25q_ranges = a25l040_ranges;
1125 *num_entries = ARRAY_SIZE(a25l040_ranges);
1126 break;
1127 default:
1128 msg_cerr("%s() %d: AMIC flash chip mismatch"
1129 " (0x%04x), aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01001130 flash->chip->model_id);
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +08001131 return -1;
1132 }
1133 break;
Furquan Shaikhb4df8ef2017-01-05 15:05:35 -08001134 case ATMEL_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +01001135 switch(flash->chip->model_id) {
Edward O'Callaghan1fa87e02019-05-03 02:27:24 -04001136 case ATMEL_AT25SF128A:
Furquan Shaikhb4df8ef2017-01-05 15:05:35 -08001137 case ATMEL_AT25SL128A:
1138 if (w25q_read_status_register_2(flash) & (1 << 6)) {
1139 /* CMP == 1 */
1140 *w25q_ranges = w25rq128_cmp1_ranges;
1141 *num_entries = ARRAY_SIZE(w25rq128_cmp1_ranges);
1142 } else {
1143 /* CMP == 0 */
1144 *w25q_ranges = w25rq128_cmp0_ranges;
1145 *num_entries = ARRAY_SIZE(w25rq128_cmp0_ranges);
1146 }
1147 break;
1148 default:
1149 msg_cerr("%s() %d: Atmel flash chip mismatch"
1150 " (0x%04x), aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01001151 flash->chip->model_id);
Furquan Shaikhb4df8ef2017-01-05 15:05:35 -08001152 return -1;
1153 }
1154 break;
David Hendricksf7924d12010-06-10 21:26:44 -07001155 default:
David Hendricksd494b0a2010-08-16 16:28:50 -07001156 msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
Patrick Georgif3fa2992017-02-02 16:24:44 +01001157 __func__, flash->chip->manufacture_id);
David Hendricksf7924d12010-06-10 21:26:44 -07001158 return -1;
1159 }
1160
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001161 return 0;
1162}
1163
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001164int w25_range_to_status(const struct flashctx *flash,
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001165 unsigned int start, unsigned int len,
1166 struct w25q_status *status)
1167{
1168 struct w25q_range *w25q_ranges;
1169 int i, range_found = 0;
1170 int num_entries;
1171
1172 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
David Hendricksf7924d12010-06-10 21:26:44 -07001173 for (i = 0; i < num_entries; i++) {
1174 struct wp_range *r = &w25q_ranges[i].range;
1175
1176 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
1177 start, len, r->start, r->len);
1178 if ((start == r->start) && (len == r->len)) {
David Hendricksd494b0a2010-08-16 16:28:50 -07001179 status->bp0 = w25q_ranges[i].bp & 1;
1180 status->bp1 = w25q_ranges[i].bp >> 1;
1181 status->bp2 = w25q_ranges[i].bp >> 2;
1182 status->tb = w25q_ranges[i].tb;
1183 status->sec = w25q_ranges[i].sec;
David Hendricksf7924d12010-06-10 21:26:44 -07001184
1185 range_found = 1;
1186 break;
1187 }
1188 }
1189
1190 if (!range_found) {
1191 msg_cerr("matching range not found\n");
1192 return -1;
1193 }
David Hendricksd494b0a2010-08-16 16:28:50 -07001194 return 0;
1195}
1196
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001197int w25_status_to_range(const struct flashctx *flash,
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001198 const struct w25q_status *status,
1199 unsigned int *start, unsigned int *len)
1200{
1201 struct w25q_range *w25q_ranges;
1202 int i, status_found = 0;
1203 int num_entries;
1204
1205 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
1206 for (i = 0; i < num_entries; i++) {
1207 int bp;
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +08001208 int table_bp, table_tb, table_sec;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001209
1210 bp = status->bp0 | (status->bp1 << 1) | (status->bp2 << 2);
1211 msg_cspew("comparing 0x%x 0x%x / 0x%x 0x%x / 0x%x 0x%x\n",
1212 bp, w25q_ranges[i].bp,
1213 status->tb, w25q_ranges[i].tb,
1214 status->sec, w25q_ranges[i].sec);
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +08001215 table_bp = w25q_ranges[i].bp;
1216 table_tb = w25q_ranges[i].tb;
1217 table_sec = w25q_ranges[i].sec;
1218 if ((bp == table_bp || table_bp == X) &&
1219 (status->tb == table_tb || table_tb == X) &&
1220 (status->sec == table_sec || table_sec == X)) {
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001221 *start = w25q_ranges[i].range.start;
1222 *len = w25q_ranges[i].range.len;
1223
1224 status_found = 1;
1225 break;
1226 }
1227 }
1228
1229 if (!status_found) {
1230 msg_cerr("matching status not found\n");
1231 return -1;
1232 }
1233 return 0;
1234}
1235
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001236/* Given a [start, len], this function calls w25_range_to_status() to convert
1237 * it to flash-chip-specific range bits, then sets into status register.
1238 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001239static int w25_set_range(const struct flashctx *flash,
David Hendricksd494b0a2010-08-16 16:28:50 -07001240 unsigned int start, unsigned int len)
1241{
1242 struct w25q_status status;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001243 int tmp = 0;
1244 int expected = 0;
David Hendricksd494b0a2010-08-16 16:28:50 -07001245
1246 memset(&status, 0, sizeof(status));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301247 tmp = do_read_status(flash);
David Hendricksd494b0a2010-08-16 16:28:50 -07001248 memcpy(&status, &tmp, 1);
1249 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
1250
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001251 if (w25_range_to_status(flash, start, len, &status)) return -1;
David Hendricksf7924d12010-06-10 21:26:44 -07001252
1253 msg_cdbg("status.busy: %x\n", status.busy);
1254 msg_cdbg("status.wel: %x\n", status.wel);
1255 msg_cdbg("status.bp0: %x\n", status.bp0);
1256 msg_cdbg("status.bp1: %x\n", status.bp1);
1257 msg_cdbg("status.bp2: %x\n", status.bp2);
1258 msg_cdbg("status.tb: %x\n", status.tb);
1259 msg_cdbg("status.sec: %x\n", status.sec);
1260 msg_cdbg("status.srp0: %x\n", status.srp0);
1261
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001262 memcpy(&expected, &status, sizeof(status));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301263 do_write_status(flash, expected);
David Hendricksf7924d12010-06-10 21:26:44 -07001264
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301265 tmp = do_read_status(flash);
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001266 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
1267 if ((tmp & MASK_WP_AREA) == (expected & MASK_WP_AREA)) {
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001268 return 0;
1269 } else {
David Hendricksc801adb2010-12-09 16:58:56 -08001270 msg_cerr("expected=0x%02x, but actual=0x%02x.\n",
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001271 expected, tmp);
1272 return 1;
1273 }
David Hendricksf7924d12010-06-10 21:26:44 -07001274}
1275
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001276/* Print out the current status register value with human-readable text. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001277static int w25_wp_status(const struct flashctx *flash)
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001278{
1279 struct w25q_status status;
1280 int tmp;
David Hendricksce8ded32010-10-08 11:23:38 -07001281 unsigned int start, len;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001282 int ret = 0;
1283
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001284 memset(&status, 0, sizeof(status));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301285 tmp = do_read_status(flash);
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001286 memcpy(&status, &tmp, 1);
1287 msg_cinfo("WP: status: 0x%02x\n", tmp);
1288 msg_cinfo("WP: status.srp0: %x\n", status.srp0);
1289 msg_cinfo("WP: write protect is %s.\n",
1290 status.srp0 ? "enabled" : "disabled");
1291
1292 msg_cinfo("WP: write protect range: ");
1293 if (w25_status_to_range(flash, &status, &start, &len)) {
1294 msg_cinfo("(cannot resolve the range)\n");
1295 ret = -1;
1296 } else {
1297 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
1298 }
1299
1300 return ret;
1301}
1302
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001303static int w25q_large_range_to_status(const struct flashctx *flash,
1304 unsigned int start, unsigned int len,
1305 struct w25q_status_large *status)
1306{
1307 struct w25q_range *w25q_ranges;
1308 int i, range_found = 0;
1309 int num_entries;
1310
1311 if (w25_range_table(flash, &w25q_ranges, &num_entries))
1312 return -1;
1313 for (i = 0; i < num_entries; i++) {
1314 struct wp_range *r = &w25q_ranges[i].range;
1315
1316 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
1317 start, len, r->start, r->len);
1318 if ((start == r->start) && (len == r->len)) {
1319 status->bp0 = w25q_ranges[i].bp & 1;
1320 status->bp1 = w25q_ranges[i].bp >> 1;
1321 status->bp2 = w25q_ranges[i].bp >> 2;
1322 status->bp3 = w25q_ranges[i].bp >> 3;
Karthikeyan Ramasubramanianfb166b72019-06-24 12:38:55 -06001323 /*
1324 * For MX25U12835E chip, Top/Bottom (T/B) bit is not
1325 * part of status register and in that bit position is
1326 * Quad Enable (QE)
1327 */
1328 if (flash->chip->manufacture_id != MACRONIX_ID ||
1329 flash->chip->model_id != MACRONIX_MX25U12835E)
1330 status->tb = w25q_ranges[i].tb;
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001331
1332 range_found = 1;
1333 break;
1334 }
1335 }
1336
1337 if (!range_found) {
1338 msg_cerr("matching range not found\n");
1339 return -1;
1340 }
1341 return 0;
1342}
1343
1344static int w25_large_status_to_range(const struct flashctx *flash,
1345 const struct w25q_status_large *status,
1346 unsigned int *start, unsigned int *len)
1347{
1348 struct w25q_range *w25q_ranges;
1349 int i, status_found = 0;
1350 int num_entries;
1351
1352 if (w25_range_table(flash, &w25q_ranges, &num_entries))
1353 return -1;
1354 for (i = 0; i < num_entries; i++) {
1355 int bp;
1356 int table_bp, table_tb;
1357
1358 bp = status->bp0 | (status->bp1 << 1) | (status->bp2 << 2) |
1359 (status->bp3 << 3);
1360 msg_cspew("comparing 0x%x 0x%x / 0x%x 0x%x\n",
1361 bp, w25q_ranges[i].bp,
1362 status->tb, w25q_ranges[i].tb);
1363 table_bp = w25q_ranges[i].bp;
1364 table_tb = w25q_ranges[i].tb;
1365 if ((bp == table_bp || table_bp == X) &&
1366 (status->tb == table_tb || table_tb == X)) {
1367 *start = w25q_ranges[i].range.start;
1368 *len = w25q_ranges[i].range.len;
1369
1370 status_found = 1;
1371 break;
1372 }
1373 }
1374
1375 if (!status_found) {
1376 msg_cerr("matching status not found\n");
1377 return -1;
1378 }
1379 return 0;
1380}
1381
1382/* Given a [start, len], this function calls w25_range_to_status() to convert
1383 * it to flash-chip-specific range bits, then sets into status register.
1384 * Returns 0 if successful, -1 on error, and 1 if reading back was different.
1385 */
1386static int w25q_large_set_range(const struct flashctx *flash,
1387 unsigned int start, unsigned int len)
1388{
1389 struct w25q_status_large status;
1390 int tmp;
1391 int expected = 0;
1392
1393 memset(&status, 0, sizeof(status));
1394 tmp = do_read_status(flash);
1395 memcpy(&status, &tmp, 1);
1396 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
1397
1398 if (w25q_large_range_to_status(flash, start, len, &status))
1399 return -1;
1400
1401 msg_cdbg("status.busy: %x\n", status.busy);
1402 msg_cdbg("status.wel: %x\n", status.wel);
1403 msg_cdbg("status.bp0: %x\n", status.bp0);
1404 msg_cdbg("status.bp1: %x\n", status.bp1);
1405 msg_cdbg("status.bp2: %x\n", status.bp2);
1406 msg_cdbg("status.bp3: %x\n", status.bp3);
1407 msg_cdbg("status.tb: %x\n", status.tb);
1408 msg_cdbg("status.srp0: %x\n", status.srp0);
1409
1410 memcpy(&expected, &status, sizeof(status));
1411 do_write_status(flash, expected);
1412
1413 tmp = do_read_status(flash);
1414 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
1415 if ((tmp & MASK_WP_AREA_LARGE) == (expected & MASK_WP_AREA_LARGE)) {
1416 return 0;
1417 } else {
1418 msg_cerr("expected=0x%02x, but actual=0x%02x.\n",
1419 expected, tmp);
1420 return 1;
1421 }
1422}
1423
1424static int w25q_large_wp_status(const struct flashctx *flash)
1425{
1426 struct w25q_status_large sr1;
1427 struct w25q_status_2 sr2;
1428 uint8_t tmp[2];
1429 unsigned int start, len;
1430 int ret = 0;
1431
1432 memset(&sr1, 0, sizeof(sr1));
1433 tmp[0] = do_read_status(flash);
1434 memcpy(&sr1, &tmp[0], 1);
1435
1436 memset(&sr2, 0, sizeof(sr2));
1437 tmp[1] = w25q_read_status_register_2(flash);
1438 memcpy(&sr2, &tmp[1], 1);
1439
1440 msg_cinfo("WP: status: 0x%02x%02x\n", tmp[1], tmp[0]);
1441 msg_cinfo("WP: status.srp0: %x\n", sr1.srp0);
1442 msg_cinfo("WP: status.srp1: %x\n", sr2.srp1);
1443 msg_cinfo("WP: write protect is %s.\n",
1444 (sr1.srp0 || sr2.srp1) ? "enabled" : "disabled");
1445
1446 msg_cinfo("WP: write protect range: ");
1447 if (w25_large_status_to_range(flash, &sr1, &start, &len)) {
1448 msg_cinfo("(cannot resolve the range)\n");
1449 ret = -1;
1450 } else {
1451 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
1452 }
1453
1454 return ret;
1455}
1456
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001457/* Set/clear the SRP0 bit in the status register. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001458static int w25_set_srp0(const struct flashctx *flash, int enable)
David Hendricksf7924d12010-06-10 21:26:44 -07001459{
1460 struct w25q_status status;
1461 int tmp = 0;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001462 int expected = 0;
David Hendricksf7924d12010-06-10 21:26:44 -07001463
1464 memset(&status, 0, sizeof(status));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301465 tmp = do_read_status(flash);
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001466 /* FIXME: this is NOT endian-free copy. */
David Hendricksf7924d12010-06-10 21:26:44 -07001467 memcpy(&status, &tmp, 1);
1468 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
1469
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001470 status.srp0 = enable ? 1 : 0;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001471 memcpy(&expected, &status, sizeof(status));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301472 do_write_status(flash, expected);
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001473
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301474 tmp = do_read_status(flash);
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001475 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
1476 if ((tmp & MASK_WP_AREA) != (expected & MASK_WP_AREA))
1477 return 1;
David Hendricksf7924d12010-06-10 21:26:44 -07001478
1479 return 0;
1480}
1481
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001482static int w25_enable_writeprotect(const struct flashctx *flash,
David Hendricks1c09f802012-10-03 11:03:48 -07001483 enum wp_mode wp_mode)
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001484{
1485 int ret;
1486
David Hendricks1c09f802012-10-03 11:03:48 -07001487 switch (wp_mode) {
1488 case WP_MODE_HARDWARE:
1489 ret = w25_set_srp0(flash, 1);
1490 break;
1491 default:
1492 msg_cerr("%s(): unsupported write-protect mode\n", __func__);
1493 return 1;
1494 }
1495
David Hendricksc801adb2010-12-09 16:58:56 -08001496 if (ret)
1497 msg_cerr("%s(): error=%d.\n", __func__, ret);
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001498 return ret;
1499}
1500
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001501static int w25_disable_writeprotect(const struct flashctx *flash)
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001502{
1503 int ret;
1504
1505 ret = w25_set_srp0(flash, 0);
David Hendricksc801adb2010-12-09 16:58:56 -08001506 if (ret)
1507 msg_cerr("%s(): error=%d.\n", __func__, ret);
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001508 return ret;
1509}
1510
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001511static int w25_list_ranges(const struct flashctx *flash)
David Hendricks0f7f5382011-02-11 18:12:31 -08001512{
1513 struct w25q_range *w25q_ranges;
1514 int i, num_entries;
1515
1516 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
1517 for (i = 0; i < num_entries; i++) {
1518 msg_cinfo("start: 0x%06x, length: 0x%06x\n",
1519 w25q_ranges[i].range.start,
1520 w25q_ranges[i].range.len);
1521 }
1522
1523 return 0;
1524}
1525
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001526static int w25q_wp_status(const struct flashctx *flash)
David Hendricks1c09f802012-10-03 11:03:48 -07001527{
1528 struct w25q_status sr1;
1529 struct w25q_status_2 sr2;
David Hendricksf1bd8802012-10-30 11:37:57 -07001530 uint8_t tmp[2];
David Hendricks1c09f802012-10-03 11:03:48 -07001531 unsigned int start, len;
1532 int ret = 0;
1533
1534 memset(&sr1, 0, sizeof(sr1));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301535 tmp[0] = do_read_status(flash);
David Hendricksf1bd8802012-10-30 11:37:57 -07001536 memcpy(&sr1, &tmp[0], 1);
David Hendricks1c09f802012-10-03 11:03:48 -07001537
David Hendricksf1bd8802012-10-30 11:37:57 -07001538 memset(&sr2, 0, sizeof(sr2));
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001539 tmp[1] = w25q_read_status_register_2(flash);
David Hendricksf1bd8802012-10-30 11:37:57 -07001540 memcpy(&sr2, &tmp[1], 1);
1541
1542 msg_cinfo("WP: status: 0x%02x%02x\n", tmp[1], tmp[0]);
David Hendricks1c09f802012-10-03 11:03:48 -07001543 msg_cinfo("WP: status.srp0: %x\n", sr1.srp0);
1544 msg_cinfo("WP: status.srp1: %x\n", sr2.srp1);
1545 msg_cinfo("WP: write protect is %s.\n",
1546 (sr1.srp0 || sr2.srp1) ? "enabled" : "disabled");
1547
1548 msg_cinfo("WP: write protect range: ");
1549 if (w25_status_to_range(flash, &sr1, &start, &len)) {
1550 msg_cinfo("(cannot resolve the range)\n");
1551 ret = -1;
1552 } else {
1553 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
1554 }
1555
1556 return ret;
1557}
1558
1559/*
1560 * W25Q adds an optional byte to the standard WRSR opcode. If /CS is
1561 * de-asserted after the first byte, then it acts like a JEDEC-standard
1562 * WRSR command. if /CS is asserted, then the next data byte is written
1563 * into status register 2.
1564 */
1565#define W25Q_WRSR_OUTSIZE 0x03
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001566static int w25q_write_status_register_WREN(const struct flashctx *flash, uint8_t s1, uint8_t s2)
David Hendricks1c09f802012-10-03 11:03:48 -07001567{
1568 int result;
1569 struct spi_command cmds[] = {
1570 {
1571 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
1572 .writecnt = JEDEC_WREN_OUTSIZE,
1573 .writearr = (const unsigned char[]){ JEDEC_WREN },
1574 .readcnt = 0,
1575 .readarr = NULL,
1576 }, {
1577 .writecnt = W25Q_WRSR_OUTSIZE,
1578 .writearr = (const unsigned char[]){ JEDEC_WRSR, s1, s2 },
1579 .readcnt = 0,
1580 .readarr = NULL,
1581 }, {
1582 .writecnt = 0,
1583 .writearr = NULL,
1584 .readcnt = 0,
1585 .readarr = NULL,
1586 }};
1587
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001588 result = spi_send_multicommand(flash, cmds);
David Hendricks1c09f802012-10-03 11:03:48 -07001589 if (result) {
1590 msg_cerr("%s failed during command execution\n",
1591 __func__);
1592 }
1593
1594 /* WRSR performs a self-timed erase before the changes take effect. */
David Hendricks60824042014-12-11 17:22:06 -08001595 programmer_delay(100 * 1000);
David Hendricks1c09f802012-10-03 11:03:48 -07001596
1597 return result;
1598}
1599
1600/*
1601 * Set/clear the SRP1 bit in status register 2.
1602 * FIXME: make this more generic if other chips use the same SR2 layout
1603 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001604static int w25q_set_srp1(const struct flashctx *flash, int enable)
David Hendricks1c09f802012-10-03 11:03:48 -07001605{
1606 struct w25q_status sr1;
1607 struct w25q_status_2 sr2;
1608 uint8_t tmp, expected;
1609
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301610 tmp = do_read_status(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001611 memcpy(&sr1, &tmp, 1);
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001612 tmp = w25q_read_status_register_2(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001613 memcpy(&sr2, &tmp, 1);
1614
1615 msg_cdbg("%s: old status 2: 0x%02x\n", __func__, tmp);
1616
1617 sr2.srp1 = enable ? 1 : 0;
1618
1619 memcpy(&expected, &sr2, 1);
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001620 w25q_write_status_register_WREN(flash, *((uint8_t *)&sr1), *((uint8_t *)&sr2));
David Hendricks1c09f802012-10-03 11:03:48 -07001621
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001622 tmp = w25q_read_status_register_2(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001623 msg_cdbg("%s: new status 2: 0x%02x\n", __func__, tmp);
1624 if ((tmp & MASK_WP2_AREA) != (expected & MASK_WP2_AREA))
1625 return 1;
1626
1627 return 0;
1628}
1629
1630enum wp_mode get_wp_mode(const char *mode_str)
1631{
1632 enum wp_mode wp_mode = WP_MODE_UNKNOWN;
1633
1634 if (!strcasecmp(mode_str, "hardware"))
1635 wp_mode = WP_MODE_HARDWARE;
1636 else if (!strcasecmp(mode_str, "power_cycle"))
1637 wp_mode = WP_MODE_POWER_CYCLE;
1638 else if (!strcasecmp(mode_str, "permanent"))
1639 wp_mode = WP_MODE_PERMANENT;
1640
1641 return wp_mode;
1642}
1643
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001644static int w25q_disable_writeprotect(const struct flashctx *flash,
David Hendricks1c09f802012-10-03 11:03:48 -07001645 enum wp_mode wp_mode)
1646{
1647 int ret = 1;
David Hendricks1c09f802012-10-03 11:03:48 -07001648 struct w25q_status_2 sr2;
1649 uint8_t tmp;
1650
1651 switch (wp_mode) {
1652 case WP_MODE_HARDWARE:
1653 ret = w25_set_srp0(flash, 0);
1654 break;
1655 case WP_MODE_POWER_CYCLE:
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001656 tmp = w25q_read_status_register_2(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001657 memcpy(&sr2, &tmp, 1);
1658 if (sr2.srp1) {
1659 msg_cerr("%s(): must disconnect power to disable "
1660 "write-protection\n", __func__);
1661 } else {
1662 ret = 0;
1663 }
1664 break;
1665 case WP_MODE_PERMANENT:
1666 msg_cerr("%s(): cannot disable permanent write-protection\n",
1667 __func__);
1668 break;
1669 default:
1670 msg_cerr("%s(): invalid mode specified\n", __func__);
1671 break;
1672 }
1673
1674 if (ret)
1675 msg_cerr("%s(): error=%d.\n", __func__, ret);
1676 return ret;
1677}
1678
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001679static int w25q_disable_writeprotect_default(const struct flashctx *flash)
David Hendricks1c09f802012-10-03 11:03:48 -07001680{
1681 return w25q_disable_writeprotect(flash, WP_MODE_HARDWARE);
1682}
1683
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001684static int w25q_enable_writeprotect(const struct flashctx *flash,
David Hendricks1c09f802012-10-03 11:03:48 -07001685 enum wp_mode wp_mode)
1686{
1687 int ret = 1;
1688 struct w25q_status sr1;
1689 struct w25q_status_2 sr2;
1690 uint8_t tmp;
1691
1692 switch (wp_mode) {
1693 case WP_MODE_HARDWARE:
1694 if (w25q_disable_writeprotect(flash, WP_MODE_POWER_CYCLE)) {
1695 msg_cerr("%s(): cannot disable power cycle WP mode\n",
1696 __func__);
1697 break;
1698 }
1699
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301700 tmp = do_read_status(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001701 memcpy(&sr1, &tmp, 1);
1702 if (sr1.srp0)
1703 ret = 0;
1704 else
1705 ret = w25_set_srp0(flash, 1);
1706
1707 break;
1708 case WP_MODE_POWER_CYCLE:
1709 if (w25q_disable_writeprotect(flash, WP_MODE_HARDWARE)) {
1710 msg_cerr("%s(): cannot disable hardware WP mode\n",
1711 __func__);
1712 break;
1713 }
1714
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001715 tmp = w25q_read_status_register_2(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001716 memcpy(&sr2, &tmp, 1);
1717 if (sr2.srp1)
1718 ret = 0;
1719 else
1720 ret = w25q_set_srp1(flash, 1);
1721
1722 break;
1723 case WP_MODE_PERMANENT:
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301724 tmp = do_read_status(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001725 memcpy(&sr1, &tmp, 1);
1726 if (sr1.srp0 == 0) {
1727 ret = w25_set_srp0(flash, 1);
1728 if (ret) {
David Hendricksf1bd8802012-10-30 11:37:57 -07001729 msg_perr("%s(): cannot enable SRP0 for "
David Hendricks1c09f802012-10-03 11:03:48 -07001730 "permanent WP\n", __func__);
1731 break;
1732 }
1733 }
1734
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001735 tmp = w25q_read_status_register_2(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001736 memcpy(&sr2, &tmp, 1);
1737 if (sr2.srp1 == 0) {
1738 ret = w25q_set_srp1(flash, 1);
1739 if (ret) {
David Hendricksf1bd8802012-10-30 11:37:57 -07001740 msg_perr("%s(): cannot enable SRP1 for "
David Hendricks1c09f802012-10-03 11:03:48 -07001741 "permanent WP\n", __func__);
1742 break;
1743 }
1744 }
1745
1746 break;
David Hendricksf1bd8802012-10-30 11:37:57 -07001747 default:
1748 msg_perr("%s(): invalid mode %d\n", __func__, wp_mode);
1749 break;
David Hendricks1c09f802012-10-03 11:03:48 -07001750 }
1751
1752 if (ret)
1753 msg_cerr("%s(): error=%d.\n", __func__, ret);
1754 return ret;
1755}
1756
1757/* W25P, W25X, and many flash chips from various vendors */
David Hendricksf7924d12010-06-10 21:26:44 -07001758struct wp wp_w25 = {
David Hendricks0f7f5382011-02-11 18:12:31 -08001759 .list_ranges = w25_list_ranges,
David Hendricksf7924d12010-06-10 21:26:44 -07001760 .set_range = w25_set_range,
1761 .enable = w25_enable_writeprotect,
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001762 .disable = w25_disable_writeprotect,
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001763 .wp_status = w25_wp_status,
David Hendricks1c09f802012-10-03 11:03:48 -07001764
1765};
1766
1767/* W25Q series has features such as a second status register and SFDP */
1768struct wp wp_w25q = {
1769 .list_ranges = w25_list_ranges,
1770 .set_range = w25_set_range,
1771 .enable = w25q_enable_writeprotect,
1772 /*
1773 * By default, disable hardware write-protection. We may change
1774 * this later if we want to add fine-grained write-protect disable
1775 * as a command-line option.
1776 */
1777 .disable = w25q_disable_writeprotect_default,
1778 .wp_status = w25q_wp_status,
David Hendricksf7924d12010-06-10 21:26:44 -07001779};
David Hendrickse0512a72014-07-15 20:30:47 -07001780
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001781/* W25Q large series has 4 block-protect bits */
1782struct wp wp_w25q_large = {
1783 .list_ranges = w25_list_ranges,
1784 .set_range = w25q_large_set_range,
1785 .enable = w25q_enable_writeprotect,
1786 /*
1787 * By default, disable hardware write-protection. We may change
1788 * this later if we want to add fine-grained write-protect disable
1789 * as a command-line option.
1790 */
1791 .disable = w25q_disable_writeprotect_default,
1792 .wp_status = w25q_large_wp_status,
1793};
1794
David Hendricksaf3944a2014-07-28 18:37:40 -07001795struct generic_range gd25q32_cmp0_ranges[] = {
1796 /* none, bp4 and bp3 => don't care */
David Hendricks148a4bf2015-03-13 21:02:42 -07001797 { { }, 0x00, {0, 0} },
1798 { { }, 0x08, {0, 0} },
1799 { { }, 0x10, {0, 0} },
1800 { { }, 0x18, {0, 0} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001801
David Hendricks148a4bf2015-03-13 21:02:42 -07001802 { { }, 0x01, {0x3f0000, 64 * 1024} },
1803 { { }, 0x02, {0x3e0000, 128 * 1024} },
1804 { { }, 0x03, {0x3c0000, 256 * 1024} },
1805 { { }, 0x04, {0x380000, 512 * 1024} },
1806 { { }, 0x05, {0x300000, 1024 * 1024} },
1807 { { }, 0x06, {0x200000, 2048 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001808
David Hendricks148a4bf2015-03-13 21:02:42 -07001809 { { }, 0x09, {0x000000, 64 * 1024} },
1810 { { }, 0x0a, {0x000000, 128 * 1024} },
1811 { { }, 0x0b, {0x000000, 256 * 1024} },
1812 { { }, 0x0c, {0x000000, 512 * 1024} },
1813 { { }, 0x0d, {0x000000, 1024 * 1024} },
1814 { { }, 0x0e, {0x000000, 2048 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001815
1816 /* all, bp4 and bp3 => don't care */
David Hendricks148a4bf2015-03-13 21:02:42 -07001817 { { }, 0x07, {0x000000, 4096 * 1024} },
1818 { { }, 0x0f, {0x000000, 4096 * 1024} },
1819 { { }, 0x17, {0x000000, 4096 * 1024} },
1820 { { }, 0x1f, {0x000000, 4096 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001821
David Hendricks148a4bf2015-03-13 21:02:42 -07001822 { { }, 0x11, {0x3ff000, 4 * 1024} },
1823 { { }, 0x12, {0x3fe000, 8 * 1024} },
1824 { { }, 0x13, {0x3fc000, 16 * 1024} },
1825 { { }, 0x14, {0x3f8000, 32 * 1024} }, /* bp0 => don't care */
1826 { { }, 0x15, {0x3f8000, 32 * 1024} }, /* bp0 => don't care */
1827 { { }, 0x16, {0x3f8000, 32 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001828
David Hendricks148a4bf2015-03-13 21:02:42 -07001829 { { }, 0x19, {0x000000, 4 * 1024} },
1830 { { }, 0x1a, {0x000000, 8 * 1024} },
1831 { { }, 0x1b, {0x000000, 16 * 1024} },
1832 { { }, 0x1c, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1833 { { }, 0x1d, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1834 { { }, 0x1e, {0x000000, 32 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001835};
1836
1837struct generic_range gd25q32_cmp1_ranges[] = {
Martin Roth563a1fe2017-04-18 14:26:27 -06001838 /* All, bp4 and bp3 => don't care */
1839 { { }, 0x00, {0x000000, 4096 * 1024} }, /* All */
1840 { { }, 0x08, {0x000000, 4096 * 1024} },
1841 { { }, 0x10, {0x000000, 4096 * 1024} },
1842 { { }, 0x18, {0x000000, 4096 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001843
David Hendricks148a4bf2015-03-13 21:02:42 -07001844 { { }, 0x01, {0x000000, 4032 * 1024} },
1845 { { }, 0x02, {0x000000, 3968 * 1024} },
1846 { { }, 0x03, {0x000000, 3840 * 1024} },
1847 { { }, 0x04, {0x000000, 3584 * 1024} },
1848 { { }, 0x05, {0x000000, 3 * 1024 * 1024} },
1849 { { }, 0x06, {0x000000, 2 * 1024 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001850
David Hendricks148a4bf2015-03-13 21:02:42 -07001851 { { }, 0x09, {0x010000, 4032 * 1024} },
1852 { { }, 0x0a, {0x020000, 3968 * 1024} },
1853 { { }, 0x0b, {0x040000, 3840 * 1024} },
1854 { { }, 0x0c, {0x080000, 3584 * 1024} },
1855 { { }, 0x0d, {0x100000, 3 * 1024 * 1024} },
1856 { { }, 0x0e, {0x200000, 2 * 1024 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001857
Martin Roth563a1fe2017-04-18 14:26:27 -06001858 /* None, bp4 and bp3 => don't care */
1859 { { }, 0x07, {0, 0} }, /* None */
1860 { { }, 0x0f, {0, 0} },
1861 { { }, 0x17, {0, 0} },
1862 { { }, 0x1f, {0, 0} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001863
David Hendricks148a4bf2015-03-13 21:02:42 -07001864 { { }, 0x11, {0x000000, 4092 * 1024} },
1865 { { }, 0x12, {0x000000, 4088 * 1024} },
1866 { { }, 0x13, {0x000000, 4080 * 1024} },
1867 { { }, 0x14, {0x000000, 4064 * 1024} }, /* bp0 => don't care */
1868 { { }, 0x15, {0x000000, 4064 * 1024} }, /* bp0 => don't care */
1869 { { }, 0x16, {0x000000, 4064 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001870
David Hendricks148a4bf2015-03-13 21:02:42 -07001871 { { }, 0x19, {0x001000, 4092 * 1024} },
1872 { { }, 0x1a, {0x002000, 4088 * 1024} },
1873 { { }, 0x1b, {0x040000, 4080 * 1024} },
1874 { { }, 0x1c, {0x080000, 4064 * 1024} }, /* bp0 => don't care */
1875 { { }, 0x1d, {0x080000, 4064 * 1024} }, /* bp0 => don't care */
1876 { { }, 0x1e, {0x080000, 4064 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001877};
1878
1879static struct generic_wp gd25q32_wp = {
1880 /* TODO: map second status register */
1881 .sr1 = { .bp0_pos = 2, .bp_bits = 5, .srp_pos = 7 },
1882};
1883
David Hendricks1e9d7ca2016-03-14 15:50:34 -07001884struct generic_range gd25q128_cmp0_ranges[] = {
1885 /* none, bp4 and bp3 => don't care, others = 0 */
1886 { { .tb = 0 }, 0x00, {0, 0} },
1887 { { .tb = 0 }, 0x08, {0, 0} },
1888 { { .tb = 0 }, 0x10, {0, 0} },
1889 { { .tb = 0 }, 0x18, {0, 0} },
1890
1891 { { .tb = 0 }, 0x01, {0xfc0000, 256 * 1024} },
1892 { { .tb = 0 }, 0x02, {0xf80000, 512 * 1024} },
1893 { { .tb = 0 }, 0x03, {0xf00000, 1024 * 1024} },
1894 { { .tb = 0 }, 0x04, {0xe00000, 2048 * 1024} },
1895 { { .tb = 0 }, 0x05, {0xc00000, 4096 * 1024} },
1896 { { .tb = 0 }, 0x06, {0x800000, 8192 * 1024} },
1897
1898 { { .tb = 0 }, 0x09, {0x000000, 256 * 1024} },
1899 { { .tb = 0 }, 0x0a, {0x000000, 512 * 1024} },
1900 { { .tb = 0 }, 0x0b, {0x000000, 1024 * 1024} },
1901 { { .tb = 0 }, 0x0c, {0x000000, 2048 * 1024} },
1902 { { .tb = 0 }, 0x0d, {0x000000, 4096 * 1024} },
1903 { { .tb = 0 }, 0x0e, {0x000000, 8192 * 1024} },
1904
1905 /* all, bp4 and bp3 => don't care, others = 1 */
1906 { { .tb = 0 }, 0x07, {0x000000, 16384 * 1024} },
1907 { { .tb = 0 }, 0x0f, {0x000000, 16384 * 1024} },
1908 { { .tb = 0 }, 0x17, {0x000000, 16384 * 1024} },
1909 { { .tb = 0 }, 0x1f, {0x000000, 16384 * 1024} },
1910
1911 { { .tb = 0 }, 0x11, {0xfff000, 4 * 1024} },
1912 { { .tb = 0 }, 0x12, {0xffe000, 8 * 1024} },
1913 { { .tb = 0 }, 0x13, {0xffc000, 16 * 1024} },
1914 { { .tb = 0 }, 0x14, {0xff8000, 32 * 1024} }, /* bp0 => don't care */
1915 { { .tb = 0 }, 0x15, {0xff8000, 32 * 1024} }, /* bp0 => don't care */
1916
1917 { { .tb = 0 }, 0x19, {0x000000, 4 * 1024} },
1918 { { .tb = 0 }, 0x1a, {0x000000, 8 * 1024} },
1919 { { .tb = 0 }, 0x1b, {0x000000, 16 * 1024} },
1920 { { .tb = 0 }, 0x1c, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1921 { { .tb = 0 }, 0x1d, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1922 { { .tb = 0 }, 0x1e, {0x000000, 32 * 1024} },
1923};
1924
1925struct generic_range gd25q128_cmp1_ranges[] = {
1926 /* none, bp4 and bp3 => don't care, others = 0 */
1927 { { .tb = 1 }, 0x00, {0x000000, 16384 * 1024} },
1928 { { .tb = 1 }, 0x08, {0x000000, 16384 * 1024} },
1929 { { .tb = 1 }, 0x10, {0x000000, 16384 * 1024} },
1930 { { .tb = 1 }, 0x18, {0x000000, 16384 * 1024} },
1931
1932 { { .tb = 1 }, 0x01, {0x000000, 16128 * 1024} },
1933 { { .tb = 1 }, 0x02, {0x000000, 15872 * 1024} },
1934 { { .tb = 1 }, 0x03, {0x000000, 15360 * 1024} },
1935 { { .tb = 1 }, 0x04, {0x000000, 14336 * 1024} },
1936 { { .tb = 1 }, 0x05, {0x000000, 12288 * 1024} },
1937 { { .tb = 1 }, 0x06, {0x000000, 8192 * 1024} },
1938
1939 { { .tb = 1 }, 0x09, {0x000000, 16128 * 1024} },
1940 { { .tb = 1 }, 0x0a, {0x000000, 15872 * 1024} },
1941 { { .tb = 1 }, 0x0b, {0x000000, 15360 * 1024} },
1942 { { .tb = 1 }, 0x0c, {0x000000, 14336 * 1024} },
1943 { { .tb = 1 }, 0x0d, {0x000000, 12288 * 1024} },
1944 { { .tb = 1 }, 0x0e, {0x000000, 8192 * 1024} },
1945
1946 /* none, bp4 and bp3 => don't care, others = 1 */
1947 { { .tb = 1 }, 0x07, {0x000000, 16384 * 1024} },
1948 { { .tb = 1 }, 0x08, {0x000000, 16384 * 1024} },
1949 { { .tb = 1 }, 0x0f, {0x000000, 16384 * 1024} },
1950 { { .tb = 1 }, 0x17, {0x000000, 16384 * 1024} },
1951 { { .tb = 1 }, 0x1f, {0x000000, 16384 * 1024} },
1952
1953 { { .tb = 1 }, 0x11, {0x000000, 16380 * 1024} },
1954 { { .tb = 1 }, 0x12, {0x000000, 16376 * 1024} },
1955 { { .tb = 1 }, 0x13, {0x000000, 16368 * 1024} },
1956 { { .tb = 1 }, 0x14, {0x000000, 16352 * 1024} }, /* bp0 => don't care */
1957 { { .tb = 1 }, 0x15, {0x000000, 16352 * 1024} }, /* bp0 => don't care */
1958
1959 { { .tb = 1 }, 0x19, {0x001000, 16380 * 1024} },
1960 { { .tb = 1 }, 0x1a, {0x002000, 16376 * 1024} },
1961 { { .tb = 1 }, 0x1b, {0x004000, 16368 * 1024} },
1962 { { .tb = 1 }, 0x1c, {0x008000, 16352 * 1024} }, /* bp0 => don't care */
1963 { { .tb = 1 }, 0x1d, {0x008000, 16352 * 1024} }, /* bp0 => don't care */
1964 { { .tb = 1 }, 0x1e, {0x008000, 16352 * 1024} },
1965};
1966
1967static struct generic_wp gd25q128_wp = {
1968 /* TODO: map second and third status registers */
1969 .sr1 = { .bp0_pos = 2, .bp_bits = 5, .srp_pos = 7 },
1970};
1971
David Hendricks83541d32014-07-15 20:58:21 -07001972#if 0
1973/* FIXME: MX25L6405D has same ID as MX25L6406 */
1974static struct w25q_range mx25l6405d_ranges[] = {
1975 { X, 0, 0, {0, 0} }, /* none */
1976 { X, 0, 0x1, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
1977 { X, 0, 0x2, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
1978 { X, 0, 0x3, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
1979 { X, 0, 0x4, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
1980 { X, 0, 0x5, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
1981 { X, 0, 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
1982 { X, 0, 0x7, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */
1983
1984 { X, 1, 0x0, {0x000000, 8192 * 1024} },
1985 { X, 1, 0x1, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
1986 { X, 1, 0x2, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */
1987 { X, 1, 0x3, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */
1988 { X, 1, 0x4, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */
1989 { X, 1, 0x5, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */
1990 { X, 1, 0x6, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */
1991 { X, 1, 0x7, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */
1992};
1993#endif
1994
1995/* FIXME: MX25L6406 has same ID as MX25L6405D */
1996struct generic_range mx25l6406e_ranges[] = {
David Hendricks148a4bf2015-03-13 21:02:42 -07001997 { { }, 0, {0, 0} }, /* none */
1998 { { }, 0x1, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */
1999 { { }, 0x2, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */
2000 { { }, 0x3, {0x7a0000, 64 * 8 * 1024} }, /* blocks 120-127 */
2001 { { }, 0x4, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */
2002 { { }, 0x5, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */
2003 { { }, 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
David Hendricks83541d32014-07-15 20:58:21 -07002004
David Hendricks148a4bf2015-03-13 21:02:42 -07002005 { { }, 0x7, {0x000000, 64 * 128 * 1024} }, /* all */
2006 { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* all */
2007 { { }, 0x9, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
2008 { { }, 0xa, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */
2009 { { }, 0xb, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */
2010 { { }, 0xc, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */
2011 { { }, 0xd, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */
2012 { { }, 0xe, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */
2013 { { }, 0xf, {0x000000, 64 * 128 * 1024} }, /* all */
David Hendricks83541d32014-07-15 20:58:21 -07002014};
2015
2016static struct generic_wp mx25l6406e_wp = {
2017 .sr1 = { .bp0_pos = 2, .bp_bits = 4, .srp_pos = 7 },
2018 .ranges = &mx25l6406e_ranges[0],
2019};
David Hendrickse0512a72014-07-15 20:30:47 -07002020
David Hendricksc3496092014-11-13 17:20:55 -08002021struct generic_range mx25l6495f_tb0_ranges[] = {
David Hendricks148a4bf2015-03-13 21:02:42 -07002022 { { }, 0, {0, 0} }, /* none */
2023 { { }, 0x1, {0x7f0000, 64 * 1 * 1024} }, /* block 127 */
2024 { { }, 0x2, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */
2025 { { }, 0x3, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */
David Hendricksc3496092014-11-13 17:20:55 -08002026
David Hendricks148a4bf2015-03-13 21:02:42 -07002027 { { }, 0x4, {0x780000, 64 * 8 * 1024} }, /* blocks 120-127 */
2028 { { }, 0x5, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */
2029 { { }, 0x6, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */
2030 { { }, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
2031 { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* all */
2032 { { }, 0x9, {0x000000, 64 * 128 * 1024} }, /* all */
2033 { { }, 0xa, {0x000000, 64 * 128 * 1024} }, /* all */
2034 { { }, 0xb, {0x000000, 64 * 128 * 1024} }, /* all */
2035 { { }, 0xc, {0x000000, 64 * 128 * 1024} }, /* all */
2036 { { }, 0xd, {0x000000, 64 * 128 * 1024} }, /* all */
2037 { { }, 0xe, {0x000000, 64 * 128 * 1024} }, /* all */
2038 { { }, 0xf, {0x000000, 64 * 128 * 1024} }, /* all */
David Hendricksc3496092014-11-13 17:20:55 -08002039};
2040
2041struct generic_range mx25l6495f_tb1_ranges[] = {
David Hendricks148a4bf2015-03-13 21:02:42 -07002042 { { }, 0, {0, 0} }, /* none */
2043 { { }, 0x1, {0x000000, 64 * 1 * 1024} }, /* block 0 */
2044 { { }, 0x2, {0x000000, 64 * 2 * 1024} }, /* blocks 0-1 */
2045 { { }, 0x3, {0x000000, 64 * 4 * 1024} }, /* blocks 0-3 */
2046 { { }, 0x4, {0x000000, 64 * 8 * 1024} }, /* blocks 0-7 */
2047 { { }, 0x5, {0x000000, 64 * 16 * 1024} }, /* blocks 0-15 */
2048 { { }, 0x6, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
2049 { { }, 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
2050 { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* all */
2051 { { }, 0x9, {0x000000, 64 * 128 * 1024} }, /* all */
2052 { { }, 0xa, {0x000000, 64 * 128 * 1024} }, /* all */
2053 { { }, 0xb, {0x000000, 64 * 128 * 1024} }, /* all */
2054 { { }, 0xc, {0x000000, 64 * 128 * 1024} }, /* all */
2055 { { }, 0xd, {0x000000, 64 * 128 * 1024} }, /* all */
2056 { { }, 0xe, {0x000000, 64 * 128 * 1024} }, /* all */
2057 { { }, 0xf, {0x000000, 64 * 128 * 1024} }, /* all */
David Hendricksc3496092014-11-13 17:20:55 -08002058};
2059
2060static struct generic_wp mx25l6495f_wp = {
2061 .sr1 = { .bp0_pos = 2, .bp_bits = 4, .srp_pos = 7 },
2062};
2063
Vic Yang848bfd12018-03-23 10:24:07 -07002064struct generic_range mx25l25635f_tb0_ranges[] = {
2065 { { }, 0, {0, 0} }, /* none */
2066 { { }, 0x1, {0x1ff0000, 64 * 1 * 1024} }, /* block 511 */
2067 { { }, 0x2, {0x1fe0000, 64 * 2 * 1024} }, /* blocks 510-511 */
2068 { { }, 0x3, {0x1fc0000, 64 * 4 * 1024} }, /* blocks 508-511 */
2069 { { }, 0x4, {0x1f80000, 64 * 8 * 1024} }, /* blocks 504-511 */
2070 { { }, 0x5, {0x1f00000, 64 * 16 * 1024} }, /* blocks 496-511 */
2071 { { }, 0x6, {0x1e00000, 64 * 32 * 1024} }, /* blocks 480-511 */
2072 { { }, 0x7, {0x1c00000, 64 * 64 * 1024} }, /* blocks 448-511 */
2073 { { }, 0x8, {0x1800000, 64 * 128 * 1024} }, /* blocks 384-511 */
2074 { { }, 0x9, {0x1000000, 64 * 256 * 1024} }, /* blocks 256-511 */
2075 { { }, 0xa, {0x0000000, 64 * 512 * 1024} }, /* all */
2076 { { }, 0xb, {0x0000000, 64 * 512 * 1024} }, /* all */
2077 { { }, 0xc, {0x0000000, 64 * 512 * 1024} }, /* all */
2078 { { }, 0xd, {0x0000000, 64 * 512 * 1024} }, /* all */
2079 { { }, 0xe, {0x0000000, 64 * 512 * 1024} }, /* all */
2080 { { }, 0xf, {0x0000000, 64 * 512 * 1024} }, /* all */
2081};
2082
2083struct generic_range mx25l25635f_tb1_ranges[] = {
2084 { { }, 0, {0, 0} }, /* none */
2085 { { }, 0x1, {0x000000, 64 * 1 * 1024} }, /* block 0 */
2086 { { }, 0x2, {0x000000, 64 * 2 * 1024} }, /* blocks 0-1 */
2087 { { }, 0x3, {0x000000, 64 * 4 * 1024} }, /* blocks 0-3 */
2088 { { }, 0x4, {0x000000, 64 * 8 * 1024} }, /* blocks 0-7 */
2089 { { }, 0x5, {0x000000, 64 * 16 * 1024} }, /* blocks 0-15 */
2090 { { }, 0x6, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
2091 { { }, 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
2092 { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */
2093 { { }, 0x9, {0x000000, 64 * 256 * 1024} }, /* blocks 0-255 */
2094 { { }, 0xa, {0x000000, 64 * 512 * 1024} }, /* all */
2095 { { }, 0xb, {0x000000, 64 * 512 * 1024} }, /* all */
2096 { { }, 0xc, {0x000000, 64 * 512 * 1024} }, /* all */
2097 { { }, 0xd, {0x000000, 64 * 512 * 1024} }, /* all */
2098 { { }, 0xe, {0x000000, 64 * 512 * 1024} }, /* all */
2099 { { }, 0xf, {0x000000, 64 * 512 * 1024} }, /* all */
2100};
2101
2102static struct generic_wp mx25l25635f_wp = {
2103 .sr1 = { .bp0_pos = 2, .bp_bits = 4, .srp_pos = 7 },
2104};
2105
David Hendricks148a4bf2015-03-13 21:02:42 -07002106struct generic_range s25fs128s_ranges[] = {
2107 { { .tb = 1 }, 0, {0, 0} }, /* none */
2108 { { .tb = 1 }, 0x1, {0x000000, 256 * 1024} }, /* lower 64th */
2109 { { .tb = 1 }, 0x2, {0x000000, 512 * 1024} }, /* lower 32nd */
2110 { { .tb = 1 }, 0x3, {0x000000, 1024 * 1024} }, /* lower 16th */
2111 { { .tb = 1 }, 0x4, {0x000000, 2048 * 1024} }, /* lower 8th */
2112 { { .tb = 1 }, 0x5, {0x000000, 4096 * 1024} }, /* lower 4th */
2113 { { .tb = 1 }, 0x6, {0x000000, 8192 * 1024} }, /* lower half */
2114 { { .tb = 1 }, 0x7, {0x000000, 16384 * 1024} }, /* all */
David Hendricksa9884852014-12-11 15:31:12 -08002115
David Hendricks148a4bf2015-03-13 21:02:42 -07002116 { { .tb = 0 }, 0, {0, 0} }, /* none */
2117 { { .tb = 0 }, 0x1, {0xfc0000, 256 * 1024} }, /* upper 64th */
2118 { { .tb = 0 }, 0x2, {0xf80000, 512 * 1024} }, /* upper 32nd */
2119 { { .tb = 0 }, 0x3, {0xf00000, 1024 * 1024} }, /* upper 16th */
2120 { { .tb = 0 }, 0x4, {0xe00000, 2048 * 1024} }, /* upper 8th */
2121 { { .tb = 0 }, 0x5, {0xc00000, 4096 * 1024} }, /* upper 4th */
2122 { { .tb = 0 }, 0x6, {0x800000, 8192 * 1024} }, /* upper half */
2123 { { .tb = 0 }, 0x7, {0x000000, 16384 * 1024} }, /* all */
David Hendricksa9884852014-12-11 15:31:12 -08002124};
2125
2126static struct generic_wp s25fs128s_wp = {
2127 .sr1 = { .bp0_pos = 2, .bp_bits = 3, .srp_pos = 7 },
David Hendricks148a4bf2015-03-13 21:02:42 -07002128 .get_modifier_bits = s25f_get_modifier_bits,
2129 .set_modifier_bits = s25f_set_modifier_bits,
David Hendricksa9884852014-12-11 15:31:12 -08002130};
2131
David Hendricksc694bb82015-02-25 14:52:17 -08002132
David Hendricks148a4bf2015-03-13 21:02:42 -07002133struct generic_range s25fl256s_ranges[] = {
2134 { { .tb = 1 }, 0, {0, 0} }, /* none */
2135 { { .tb = 1 }, 0x1, {0x000000, 512 * 1024} }, /* lower 64th */
2136 { { .tb = 1 }, 0x2, {0x000000, 1024 * 1024} }, /* lower 32nd */
2137 { { .tb = 1 }, 0x3, {0x000000, 2048 * 1024} }, /* lower 16th */
2138 { { .tb = 1 }, 0x4, {0x000000, 4096 * 1024} }, /* lower 8th */
2139 { { .tb = 1 }, 0x5, {0x000000, 8192 * 1024} }, /* lower 4th */
2140 { { .tb = 1 }, 0x6, {0x000000, 16384 * 1024} }, /* lower half */
2141 { { .tb = 1 }, 0x7, {0x000000, 32768 * 1024} }, /* all */
2142
2143 { { .tb = 0 }, 0, {0, 0} }, /* none */
2144 { { .tb = 0 }, 0x1, {0x1f80000, 512 * 1024} }, /* upper 64th */
2145 { { .tb = 0 }, 0x2, {0x1f00000, 1024 * 1024} }, /* upper 32nd */
2146 { { .tb = 0 }, 0x3, {0x1e00000, 2048 * 1024} }, /* upper 16th */
2147 { { .tb = 0 }, 0x4, {0x1c00000, 4096 * 1024} }, /* upper 8th */
2148 { { .tb = 0 }, 0x5, {0x1800000, 8192 * 1024} }, /* upper 4th */
2149 { { .tb = 0 }, 0x6, {0x1000000, 16384 * 1024} }, /* upper half */
2150 { { .tb = 0 }, 0x7, {0x000000, 32768 * 1024} }, /* all */
David Hendricksc694bb82015-02-25 14:52:17 -08002151};
2152
2153static struct generic_wp s25fl256s_wp = {
2154 .sr1 = { .bp0_pos = 2, .bp_bits = 3, .srp_pos = 7 },
David Hendricks148a4bf2015-03-13 21:02:42 -07002155 .get_modifier_bits = s25f_get_modifier_bits,
2156 .set_modifier_bits = s25f_set_modifier_bits,
David Hendricksc694bb82015-02-25 14:52:17 -08002157};
2158
David Hendrickse0512a72014-07-15 20:30:47 -07002159/* Given a flash chip, this function returns its writeprotect info. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002160static int generic_range_table(const struct flashctx *flash,
David Hendrickse0512a72014-07-15 20:30:47 -07002161 struct generic_wp **wp,
2162 int *num_entries)
2163{
2164 *wp = NULL;
2165 *num_entries = 0;
2166
Patrick Georgif3fa2992017-02-02 16:24:44 +01002167 switch (flash->chip->manufacture_id) {
David Hendricksaf3944a2014-07-28 18:37:40 -07002168 case GIGADEVICE_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +01002169 switch(flash->chip->model_id) {
David Hendricks1e9d7ca2016-03-14 15:50:34 -07002170
Martin Roth563a1fe2017-04-18 14:26:27 -06002171 case GIGADEVICE_GD25LQ32:
David Hendricksaf3944a2014-07-28 18:37:40 -07002172 case GIGADEVICE_GD25Q32: {
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002173 uint8_t sr1 = w25q_read_status_register_2(flash);
David Hendricksaf3944a2014-07-28 18:37:40 -07002174 *wp = &gd25q32_wp;
David Hendricks1e9d7ca2016-03-14 15:50:34 -07002175
David Hendricksaf3944a2014-07-28 18:37:40 -07002176 if (!(sr1 & (1 << 6))) { /* CMP == 0 */
2177 (*wp)->ranges = &gd25q32_cmp0_ranges[0];
2178 *num_entries = ARRAY_SIZE(gd25q32_cmp0_ranges);
2179 } else { /* CMP == 1 */
2180 (*wp)->ranges = &gd25q32_cmp1_ranges[0];
2181 *num_entries = ARRAY_SIZE(gd25q32_cmp1_ranges);
2182 }
2183
2184 break;
David Hendricks1e9d7ca2016-03-14 15:50:34 -07002185 }
Furquan Shaikh62cd8102016-07-17 23:04:06 -07002186 case GIGADEVICE_GD25Q128:
Aaron Durbin6c957d72018-08-20 09:31:01 -06002187 case GIGADEVICE_GD25LQ128CD: {
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002188 uint8_t sr1 = w25q_read_status_register_2(flash);
David Hendricks1e9d7ca2016-03-14 15:50:34 -07002189 *wp = &gd25q128_wp;
2190
2191 if (!(sr1 & (1 << 6))) { /* CMP == 0 */
2192 (*wp)->ranges = &gd25q128_cmp0_ranges[0];
2193 *num_entries = ARRAY_SIZE(gd25q128_cmp0_ranges);
2194 } else { /* CMP == 1 */
2195 (*wp)->ranges = &gd25q128_cmp1_ranges[0];
2196 *num_entries = ARRAY_SIZE(gd25q128_cmp1_ranges);
2197 }
2198
2199 break;
David Hendricksaf3944a2014-07-28 18:37:40 -07002200 }
2201 default:
2202 msg_cerr("%s() %d: GigaDevice flash chip mismatch"
2203 " (0x%04x), aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01002204 flash->chip->model_id);
David Hendricksaf3944a2014-07-28 18:37:40 -07002205 return -1;
2206 }
2207 break;
David Hendricks83541d32014-07-15 20:58:21 -07002208 case MACRONIX_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +01002209 switch (flash->chip->model_id) {
David Hendricks83541d32014-07-15 20:58:21 -07002210 case MACRONIX_MX25L6405:
2211 /* FIXME: MX25L64* chips have mixed capabilities and
2212 share IDs */
2213 *wp = &mx25l6406e_wp;
2214 *num_entries = ARRAY_SIZE(mx25l6406e_ranges);
2215 break;
David Hendricksc3496092014-11-13 17:20:55 -08002216 case MACRONIX_MX25L6495F: {
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002217 uint8_t cr = mx25l_read_config_register(flash);
David Hendricksc3496092014-11-13 17:20:55 -08002218
2219 *wp = &mx25l6495f_wp;
2220 if (!(cr & (1 << 3))) { /* T/B == 0 */
2221 (*wp)->ranges = &mx25l6495f_tb0_ranges[0];
2222 *num_entries = ARRAY_SIZE(mx25l6495f_tb0_ranges);
2223 } else { /* T/B == 1 */
2224 (*wp)->ranges = &mx25l6495f_tb1_ranges[0];
2225 *num_entries = ARRAY_SIZE(mx25l6495f_tb1_ranges);
2226 }
2227 break;
2228 }
Vic Yang848bfd12018-03-23 10:24:07 -07002229 case MACRONIX_MX25L25635F: {
2230 uint8_t cr = mx25l_read_config_register(flash);
2231
2232 *wp = &mx25l25635f_wp;
2233 if (!(cr & (1 << 3))) { /* T/B == 0 */
2234 (*wp)->ranges = &mx25l25635f_tb0_ranges[0];
2235 *num_entries = ARRAY_SIZE(mx25l25635f_tb0_ranges);
2236 } else { /* T/B == 1 */
2237 (*wp)->ranges = &mx25l25635f_tb1_ranges[0];
2238 *num_entries = ARRAY_SIZE(mx25l25635f_tb1_ranges);
2239 }
2240 break;
2241 }
David Hendricks83541d32014-07-15 20:58:21 -07002242 default:
2243 msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)"
2244 ", aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01002245 flash->chip->model_id);
David Hendricks83541d32014-07-15 20:58:21 -07002246 return -1;
2247 }
2248 break;
David Hendricksa9884852014-12-11 15:31:12 -08002249 case SPANSION_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +01002250 switch (flash->chip->model_id) {
David Hendricksa9884852014-12-11 15:31:12 -08002251 case SPANSION_S25FS128S_L:
2252 case SPANSION_S25FS128S_S: {
David Hendricksa9884852014-12-11 15:31:12 -08002253 *wp = &s25fs128s_wp;
David Hendricks148a4bf2015-03-13 21:02:42 -07002254 (*wp)->ranges = s25fs128s_ranges;
2255 *num_entries = ARRAY_SIZE(s25fs128s_ranges);
David Hendricksa9884852014-12-11 15:31:12 -08002256 break;
2257 }
David Hendricksc694bb82015-02-25 14:52:17 -08002258 case SPANSION_S25FL256S_UL:
2259 case SPANSION_S25FL256S_US: {
David Hendricksc694bb82015-02-25 14:52:17 -08002260 *wp = &s25fl256s_wp;
David Hendricks148a4bf2015-03-13 21:02:42 -07002261 (*wp)->ranges = s25fl256s_ranges;
2262 *num_entries = ARRAY_SIZE(s25fl256s_ranges);
David Hendricksc694bb82015-02-25 14:52:17 -08002263 break;
2264 }
David Hendricksa9884852014-12-11 15:31:12 -08002265 default:
2266 msg_cerr("%s():%d Spansion flash chip mismatch (0x%04x)"
Patrick Georgif3fa2992017-02-02 16:24:44 +01002267 ", aborting\n", __func__, __LINE__,
2268 flash->chip->model_id);
David Hendricksa9884852014-12-11 15:31:12 -08002269 return -1;
2270 }
2271 break;
David Hendrickse0512a72014-07-15 20:30:47 -07002272 default:
2273 msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
Patrick Georgif3fa2992017-02-02 16:24:44 +01002274 __func__, flash->chip->manufacture_id);
David Hendrickse0512a72014-07-15 20:30:47 -07002275 return -1;
2276 }
2277
2278 return 0;
2279}
2280
2281/* Given a [start, len], this function finds a block protect bit combination
2282 * (if possible) and sets the corresponding bits in "status". Remaining bits
2283 * are preserved. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002284static int generic_range_to_status(const struct flashctx *flash,
David Hendrickse0512a72014-07-15 20:30:47 -07002285 unsigned int start, unsigned int len,
2286 uint8_t *status)
2287{
2288 struct generic_wp *wp;
2289 struct generic_range *r;
2290 int i, range_found = 0, num_entries;
2291 uint8_t bp_mask;
2292
2293 if (generic_range_table(flash, &wp, &num_entries))
2294 return -1;
2295
2296 bp_mask = ((1 << (wp->sr1.bp0_pos + wp->sr1.bp_bits)) - 1) - \
2297 ((1 << wp->sr1.bp0_pos) - 1);
2298
2299 for (i = 0, r = &wp->ranges[0]; i < num_entries; i++, r++) {
2300 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
2301 start, len, r->range.start, r->range.len);
2302 if ((start == r->range.start) && (len == r->range.len)) {
2303 *status &= ~(bp_mask);
2304 *status |= r->bp << (wp->sr1.bp0_pos);
David Hendricks148a4bf2015-03-13 21:02:42 -07002305
2306 if (wp->set_modifier_bits) {
2307 if (wp->set_modifier_bits(flash, &r->m) < 0) {
2308 msg_cerr("error setting modifier "
2309 "bits for range.\n");
2310 return -1;
2311 }
2312 }
2313
David Hendrickse0512a72014-07-15 20:30:47 -07002314 range_found = 1;
2315 break;
2316 }
2317 }
2318
2319 if (!range_found) {
2320 msg_cerr("matching range not found\n");
2321 return -1;
2322 }
2323 return 0;
2324}
2325
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002326static int generic_status_to_range(const struct flashctx *flash,
David Hendrickse0512a72014-07-15 20:30:47 -07002327 const uint8_t sr1, unsigned int *start, unsigned int *len)
2328{
2329 struct generic_wp *wp;
2330 struct generic_range *r;
Duncan Laurie04ca1172015-03-12 09:25:34 -07002331 int num_entries, i, status_found = 0;
David Hendrickse0512a72014-07-15 20:30:47 -07002332 uint8_t sr1_bp;
David Hendricks148a4bf2015-03-13 21:02:42 -07002333 struct generic_modifier_bits m;
David Hendrickse0512a72014-07-15 20:30:47 -07002334
2335 if (generic_range_table(flash, &wp, &num_entries))
2336 return -1;
2337
David Hendricks148a4bf2015-03-13 21:02:42 -07002338 /* modifier bits may be compared more than once, so get them here */
2339 if (wp->get_modifier_bits) {
2340 if (wp->get_modifier_bits(flash, &m) < 0)
2341 return -1;
2342 }
2343
David Hendrickse0512a72014-07-15 20:30:47 -07002344 sr1_bp = (sr1 >> wp->sr1.bp0_pos) & ((1 << wp->sr1.bp_bits) - 1);
2345
2346 for (i = 0, r = &wp->ranges[0]; i < num_entries; i++, r++) {
David Hendricks148a4bf2015-03-13 21:02:42 -07002347 if (wp->get_modifier_bits) {
2348 if (memcmp(&m, &r->m, sizeof(m)))
2349 continue;
2350 }
David Hendrickse0512a72014-07-15 20:30:47 -07002351 msg_cspew("comparing 0x%02x 0x%02x\n", sr1_bp, r->bp);
2352 if (sr1_bp == r->bp) {
2353 *start = r->range.start;
2354 *len = r->range.len;
2355 status_found = 1;
2356 break;
2357 }
2358 }
2359
2360 if (!status_found) {
2361 msg_cerr("matching status not found\n");
2362 return -1;
2363 }
2364 return 0;
2365}
2366
2367/* Given a [start, len], this function calls generic_range_to_status() to
2368 * convert it to flash-chip-specific range bits, then sets into status register.
2369 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002370static int generic_set_range(const struct flashctx *flash,
David Hendrickse0512a72014-07-15 20:30:47 -07002371 unsigned int start, unsigned int len)
2372{
2373 uint8_t status, expected;
2374
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05302375 status = do_read_status(flash);
David Hendrickse0512a72014-07-15 20:30:47 -07002376 msg_cdbg("%s: old status: 0x%02x\n", __func__, status);
2377
2378 expected = status; /* preserve non-bp bits */
2379 if (generic_range_to_status(flash, start, len, &expected))
2380 return -1;
2381
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05302382 do_write_status(flash, expected);
David Hendrickse0512a72014-07-15 20:30:47 -07002383
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05302384 status = do_read_status(flash);
David Hendrickse0512a72014-07-15 20:30:47 -07002385 msg_cdbg("%s: new status: 0x%02x\n", __func__, status);
2386 if (status != expected) {
2387 msg_cerr("expected=0x%02x, but actual=0x%02x.\n",
2388 expected, status);
2389 return 1;
2390 }
2391
2392 return 0;
2393}
2394
2395/* Set/clear the status regsiter write protect bit in SR1. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002396static int generic_set_srp0(const struct flashctx *flash, int enable)
David Hendrickse0512a72014-07-15 20:30:47 -07002397{
2398 uint8_t status, expected;
2399 struct generic_wp *wp;
2400 int num_entries;
2401
2402 if (generic_range_table(flash, &wp, &num_entries))
2403 return -1;
2404
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05302405 expected = do_read_status(flash);
David Hendrickse0512a72014-07-15 20:30:47 -07002406 msg_cdbg("%s: old status: 0x%02x\n", __func__, expected);
2407
2408 if (enable)
2409 expected |= 1 << wp->sr1.srp_pos;
2410 else
2411 expected &= ~(1 << wp->sr1.srp_pos);
2412
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05302413 do_write_status(flash, expected);
David Hendrickse0512a72014-07-15 20:30:47 -07002414
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05302415 status = do_read_status(flash);
David Hendrickse0512a72014-07-15 20:30:47 -07002416 msg_cdbg("%s: new status: 0x%02x\n", __func__, status);
2417 if (status != expected)
2418 return -1;
2419
2420 return 0;
2421}
2422
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002423static int generic_enable_writeprotect(const struct flashctx *flash,
David Hendrickse0512a72014-07-15 20:30:47 -07002424 enum wp_mode wp_mode)
2425{
2426 int ret;
2427
2428 switch (wp_mode) {
2429 case WP_MODE_HARDWARE:
2430 ret = generic_set_srp0(flash, 1);
2431 break;
2432 default:
2433 msg_cerr("%s(): unsupported write-protect mode\n", __func__);
2434 return 1;
2435 }
2436
2437 if (ret)
2438 msg_cerr("%s(): error=%d.\n", __func__, ret);
2439 return ret;
2440}
2441
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002442static int generic_disable_writeprotect(const struct flashctx *flash)
David Hendrickse0512a72014-07-15 20:30:47 -07002443{
2444 int ret;
2445
2446 ret = generic_set_srp0(flash, 0);
2447 if (ret)
2448 msg_cerr("%s(): error=%d.\n", __func__, ret);
2449 return ret;
2450}
2451
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002452static int generic_list_ranges(const struct flashctx *flash)
David Hendrickse0512a72014-07-15 20:30:47 -07002453{
2454 struct generic_wp *wp;
2455 struct generic_range *r;
2456 int i, num_entries;
2457
2458 if (generic_range_table(flash, &wp, &num_entries))
2459 return -1;
2460
2461 r = &wp->ranges[0];
2462 for (i = 0; i < num_entries; i++) {
2463 msg_cinfo("start: 0x%06x, length: 0x%06x\n",
2464 r->range.start, r->range.len);
2465 r++;
2466 }
2467
2468 return 0;
2469}
2470
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002471static int generic_wp_status(const struct flashctx *flash)
David Hendrickse0512a72014-07-15 20:30:47 -07002472{
2473 uint8_t sr1;
2474 unsigned int start, len;
2475 int ret = 0;
2476 struct generic_wp *wp;
David Hendrickse0512a72014-07-15 20:30:47 -07002477 int num_entries, wp_en;
2478
2479 if (generic_range_table(flash, &wp, &num_entries))
2480 return -1;
2481
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05302482 sr1 = do_read_status(flash);
David Hendrickse0512a72014-07-15 20:30:47 -07002483 wp_en = (sr1 >> wp->sr1.srp_pos) & 1;
2484
2485 msg_cinfo("WP: status: 0x%04x\n", sr1);
2486 msg_cinfo("WP: status.srp0: %x\n", wp_en);
2487 /* FIXME: SRP1 is not really generic, but we probably should print
2488 * it anyway to have consistent output. #legacycruft */
2489 msg_cinfo("WP: status.srp1: %x\n", 0);
2490 msg_cinfo("WP: write protect is %s.\n",
2491 wp_en ? "enabled" : "disabled");
2492
2493 msg_cinfo("WP: write protect range: ");
2494 if (generic_status_to_range(flash, sr1, &start, &len)) {
2495 msg_cinfo("(cannot resolve the range)\n");
2496 ret = -1;
2497 } else {
2498 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
2499 }
2500
2501 return ret;
2502}
2503
2504struct wp wp_generic = {
2505 .list_ranges = generic_list_ranges,
2506 .set_range = generic_set_range,
2507 .enable = generic_enable_writeprotect,
2508 .disable = generic_disable_writeprotect,
2509 .wp_status = generic_wp_status,
2510};