blob: f704b39c272ba3add9b8484dd28e1d91cb270ced [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>
Edward O'Callaghanb4300ca2019-09-03 16:15:21 +100020#include <strings.h>
David Hendricksf7924d12010-06-10 21:26:44 -070021
22#include "flash.h"
23#include "flashchips.h"
24#include "chipdrivers.h"
Louis Yung-Chieh Lo52aa9302010-09-06 10:45:02 +080025#include "spi.h"
David Hendricks23cd7782010-08-25 12:42:38 -070026#include "writeprotect.h"
David Hendricksf7924d12010-06-10 21:26:44 -070027
David Hendricks1c09f802012-10-03 11:03:48 -070028/*
David Hendricksf7924d12010-06-10 21:26:44 -070029 * The following procedures rely on look-up tables to match the user-specified
30 * range with the chip's supported ranges. This turned out to be the most
31 * elegant approach since diferent flash chips use different levels of
32 * granularity and methods to determine protected ranges. In other words,
David Hendrickse0512a72014-07-15 20:30:47 -070033 * be stupid and simple since clever arithmetic will not work for many chips.
David Hendricksf7924d12010-06-10 21:26:44 -070034 */
35
36struct wp_range {
37 unsigned int start; /* starting address */
38 unsigned int len; /* len */
39};
40
41enum bit_state {
42 OFF = 0,
43 ON = 1,
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +080044 X = -1 /* don't care. Must be bigger than max # of bp. */
David Hendricksf7924d12010-06-10 21:26:44 -070045};
46
David Hendrickse0512a72014-07-15 20:30:47 -070047/*
48 * Generic write-protection schema for 25-series SPI flash chips. This assumes
49 * there is a status register that contains one or more consecutive bits which
50 * determine which address range is protected.
51 */
52
53struct status_register_layout {
54 int bp0_pos; /* position of BP0 */
55 int bp_bits; /* number of block protect bits */
56 int srp_pos; /* position of status register protect enable bit */
57};
58
Edward O'Callaghan91b38272019-12-04 17:12:43 +110059/*
60 * The following ranges and functions are useful for representing the
61 * writeprotect schema in which there are typically 5 bits of
62 * relevant information stored in status register 1:
63 * m.sec: This bit indicates the units (sectors vs. blocks)
64 * m.tb: The top-bottom bit indicates if the affected range is at the top of
65 * the flash memory's address space or at the bottom.
66 * bp: Bitmask representing the number of affected sectors/blocks.
67 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +110068struct wp_range_descriptor {
Edward O'Callaghan9c4c9a52019-12-04 18:18:01 +110069 struct modifier_bits m;
David Hendrickse0512a72014-07-15 20:30:47 -070070 unsigned int bp; /* block protect bitfield */
71 struct wp_range range;
72};
73
Edward O'Callaghana3edcb22019-12-05 14:30:50 +110074struct wp_context {
David Hendrickse0512a72014-07-15 20:30:47 -070075 struct status_register_layout sr1; /* status register 1 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +110076 struct wp_range_descriptor *descrs;
David Hendricks148a4bf2015-03-13 21:02:42 -070077
78 /*
79 * Some chips store modifier bits in one or more special control
80 * registers instead of the status register like many older SPI NOR
81 * flash chips did. get_modifier_bits() and set_modifier_bits() will do
82 * any chip-specific operations necessary to get/set these bit values.
83 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -070084 int (*get_modifier_bits)(const struct flashctx *flash,
Edward O'Callaghan9c4c9a52019-12-04 18:18:01 +110085 struct modifier_bits *m);
Souvik Ghoshd75cd672016-06-17 14:21:39 -070086 int (*set_modifier_bits)(const struct flashctx *flash,
Edward O'Callaghan9c4c9a52019-12-04 18:18:01 +110087 struct modifier_bits *m);
David Hendrickse0512a72014-07-15 20:30:47 -070088};
89
Edward O'Callaghanc69f6b82019-12-05 16:49:21 +110090struct w25q_status {
91 /* this maps to register layout -- do not change ordering */
92 unsigned char busy : 1;
93 unsigned char wel : 1;
94 unsigned char bp0 : 1;
95 unsigned char bp1 : 1;
96 unsigned char bp2 : 1;
97 unsigned char tb : 1;
98 unsigned char sec : 1;
99 unsigned char srp0 : 1;
100} __attribute__ ((packed));
101
102/* Status register for large flash layouts with 4 BP bits */
103struct w25q_status_large {
104 unsigned char busy : 1;
105 unsigned char wel : 1;
106 unsigned char bp0 : 1;
107 unsigned char bp1 : 1;
108 unsigned char bp2 : 1;
109 unsigned char bp3 : 1;
110 unsigned char tb : 1;
111 unsigned char srp0 : 1;
112} __attribute__ ((packed));
113
114struct w25q_status_2 {
115 unsigned char srp1 : 1;
116 unsigned char qe : 1;
117 unsigned char rsvd : 6;
118} __attribute__ ((packed));
119
120int w25_range_to_status(const struct flashctx *flash,
121 unsigned int start, unsigned int len,
122 struct w25q_status *status);
123int w25_status_to_range(const struct flashctx *flash,
124 const struct w25q_status *status,
125 unsigned int *start, unsigned int *len);
126
David Hendrickse0512a72014-07-15 20:30:47 -0700127/*
David Hendrickse0512a72014-07-15 20:30:47 -0700128 * Mask to extract write-protect enable and range bits
129 * Status register 1:
130 * SRP0: bit 7
131 * range(BP2-BP0): bit 4-2
Duncan Laurie1801f7c2019-01-09 18:02:51 -0800132 * range(BP3-BP0): bit 5-2 (large chips)
David Hendrickse0512a72014-07-15 20:30:47 -0700133 * Status register 2:
134 * SRP1: bit 1
135 */
136#define MASK_WP_AREA (0x9C)
Duncan Laurie1801f7c2019-01-09 18:02:51 -0800137#define MASK_WP_AREA_LARGE (0x9C)
David Hendrickse0512a72014-07-15 20:30:47 -0700138#define MASK_WP2_AREA (0x01)
139
Edward O'Callaghan3b996502020-04-12 20:46:51 +1000140static struct wp_range_descriptor en25f40_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100141 { .m = { .sec = X, .tb = X }, 0, {0, 0} }, /* none */
142 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x000000, 504 * 1024} },
143 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x000000, 496 * 1024} },
144 { .m = { .sec = 0, .tb = 0 }, 0x3, {0x000000, 480 * 1024} },
145 { .m = { .sec = 0, .tb = 0 }, 0x4, {0x000000, 448 * 1024} },
146 { .m = { .sec = 0, .tb = 0 }, 0x5, {0x000000, 384 * 1024} },
147 { .m = { .sec = 0, .tb = 0 }, 0x6, {0x000000, 256 * 1024} },
148 { .m = { .sec = 0, .tb = 0 }, 0x7, {0x000000, 512 * 1024} },
David Hendricks57566ed2010-08-16 18:24:45 -0700149};
150
Edward O'Callaghan3b996502020-04-12 20:46:51 +1000151static struct wp_range_descriptor en25q40_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100152 { .m = { .sec = 0, .tb = 0 }, 0, {0, 0} }, /* none */
153 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x000000, 504 * 1024} },
154 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x000000, 496 * 1024} },
155 { .m = { .sec = 0, .tb = 0 }, 0x3, {0x000000, 480 * 1024} },
David Hendrickse185bf22011-05-24 15:34:18 -0700156
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100157 { .m = { .sec = 0, .tb = 1 }, 0x0, {0x000000, 448 * 1024} },
158 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x000000, 384 * 1024} },
159 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x000000, 256 * 1024} },
160 { .m = { .sec = 0, .tb = 1 }, 0x3, {0x000000, 512 * 1024} },
David Hendrickse185bf22011-05-24 15:34:18 -0700161};
162
Edward O'Callaghan3b996502020-04-12 20:46:51 +1000163static struct wp_range_descriptor en25q80_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100164 { .m = { .sec = 0, .tb = 0 }, 0, {0, 0} }, /* none */
165 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x000000, 1016 * 1024} },
166 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x000000, 1008 * 1024} },
167 { .m = { .sec = 0, .tb = 0 }, 0x3, {0x000000, 992 * 1024} },
168 { .m = { .sec = 0, .tb = 0 }, 0x4, {0x000000, 960 * 1024} },
169 { .m = { .sec = 0, .tb = 0 }, 0x5, {0x000000, 896 * 1024} },
170 { .m = { .sec = 0, .tb = 0 }, 0x6, {0x000000, 768 * 1024} },
171 { .m = { .sec = 0, .tb = 0 }, 0x7, {0x000000, 1024 * 1024} },
David Hendrickse185bf22011-05-24 15:34:18 -0700172};
173
Edward O'Callaghan3b996502020-04-12 20:46:51 +1000174static struct wp_range_descriptor en25q32_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100175 { .m = { .sec = 0, .tb = 0 }, 0, {0, 0} }, /* none */
176 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x000000, 4032 * 1024} },
177 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x000000, 3968 * 1024} },
178 { .m = { .sec = 0, .tb = 0 }, 0x3, {0x000000, 3840 * 1024} },
179 { .m = { .sec = 0, .tb = 0 }, 0x4, {0x000000, 3584 * 1024} },
180 { .m = { .sec = 0, .tb = 0 }, 0x5, {0x000000, 3072 * 1024} },
181 { .m = { .sec = 0, .tb = 0 }, 0x6, {0x000000, 2048 * 1024} },
182 { .m = { .sec = 0, .tb = 0 }, 0x7, {0x000000, 4096 * 1024} },
David Hendrickse185bf22011-05-24 15:34:18 -0700183
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100184 { .m = { .sec = 0, .tb = 1 }, 0, {0, 0} }, /* none */
185 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x010000, 4032 * 1024} },
186 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x020000, 3968 * 1024} },
187 { .m = { .sec = 0, .tb = 1 }, 0x3, {0x040000, 3840 * 1024} },
188 { .m = { .sec = 0, .tb = 1 }, 0x4, {0x080000, 3584 * 1024} },
189 { .m = { .sec = 0, .tb = 1 }, 0x5, {0x100000, 3072 * 1024} },
190 { .m = { .sec = 0, .tb = 1 }, 0x6, {0x200000, 2048 * 1024} },
191 { .m = { .sec = 0, .tb = 1 }, 0x7, {0x000000, 4096 * 1024} },
David Hendrickse185bf22011-05-24 15:34:18 -0700192};
193
Edward O'Callaghan3b996502020-04-12 20:46:51 +1000194static struct wp_range_descriptor en25q64_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100195 { .m = { .sec = 0, .tb = 0 }, 0, {0, 0} }, /* none */
196 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x000000, 8128 * 1024} },
197 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x000000, 8064 * 1024} },
198 { .m = { .sec = 0, .tb = 0 }, 0x3, {0x000000, 7936 * 1024} },
199 { .m = { .sec = 0, .tb = 0 }, 0x4, {0x000000, 7680 * 1024} },
200 { .m = { .sec = 0, .tb = 0 }, 0x5, {0x000000, 7168 * 1024} },
201 { .m = { .sec = 0, .tb = 0 }, 0x6, {0x000000, 6144 * 1024} },
202 { .m = { .sec = 0, .tb = 0 }, 0x7, {0x000000, 8192 * 1024} },
David Hendrickse185bf22011-05-24 15:34:18 -0700203
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100204 { .m = { .sec = 0, .tb = 1 }, 0, {0, 0} }, /* none */
205 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x010000, 8128 * 1024} },
206 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x020000, 8064 * 1024} },
207 { .m = { .sec = 0, .tb = 1 }, 0x3, {0x040000, 7936 * 1024} },
208 { .m = { .sec = 0, .tb = 1 }, 0x4, {0x080000, 7680 * 1024} },
209 { .m = { .sec = 0, .tb = 1 }, 0x5, {0x100000, 7168 * 1024} },
210 { .m = { .sec = 0, .tb = 1 }, 0x6, {0x200000, 6144 * 1024} },
211 { .m = { .sec = 0, .tb = 1 }, 0x7, {0x000000, 8192 * 1024} },
David Hendrickse185bf22011-05-24 15:34:18 -0700212};
213
Edward O'Callaghan3b996502020-04-12 20:46:51 +1000214static struct wp_range_descriptor en25q128_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100215 { .m = { .sec = 0, .tb = 0 }, 0, {0, 0} }, /* none */
216 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x000000, 16320 * 1024} },
217 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x000000, 16256 * 1024} },
218 { .m = { .sec = 0, .tb = 0 }, 0x3, {0x000000, 16128 * 1024} },
219 { .m = { .sec = 0, .tb = 0 }, 0x4, {0x000000, 15872 * 1024} },
220 { .m = { .sec = 0, .tb = 0 }, 0x5, {0x000000, 15360 * 1024} },
221 { .m = { .sec = 0, .tb = 0 }, 0x6, {0x000000, 14336 * 1024} },
222 { .m = { .sec = 0, .tb = 0 }, 0x7, {0x000000, 16384 * 1024} },
David Hendrickse185bf22011-05-24 15:34:18 -0700223
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100224 { .m = { .sec = 0, .tb = 1 }, 0, {0, 0} }, /* none */
225 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x010000, 16320 * 1024} },
226 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x020000, 16256 * 1024} },
227 { .m = { .sec = 0, .tb = 1 }, 0x3, {0x040000, 16128 * 1024} },
228 { .m = { .sec = 0, .tb = 1 }, 0x4, {0x080000, 15872 * 1024} },
229 { .m = { .sec = 0, .tb = 1 }, 0x5, {0x100000, 15360 * 1024} },
230 { .m = { .sec = 0, .tb = 1 }, 0x6, {0x200000, 14336 * 1024} },
231 { .m = { .sec = 0, .tb = 1 }, 0x7, {0x000000, 16384 * 1024} },
David Hendrickse185bf22011-05-24 15:34:18 -0700232};
233
Edward O'Callaghan3b996502020-04-12 20:46:51 +1000234static struct wp_range_descriptor en25s64_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100235 { .m = { .sec = 0, .tb = 0 }, 0, {0, 0} }, /* none */
236 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x000000, 8064 * 1024} },
237 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x000000, 7936 * 1024} },
238 { .m = { .sec = 0, .tb = 0 }, 0x3, {0x000000, 7680 * 1024} },
239 { .m = { .sec = 0, .tb = 0 }, 0x4, {0x000000, 7168 * 1024} },
240 { .m = { .sec = 0, .tb = 0 }, 0x5, {0x000000, 6144 * 1024} },
241 { .m = { .sec = 0, .tb = 0 }, 0x6, {0x000000, 4096 * 1024} },
242 { .m = { .sec = 0, .tb = 0 }, 0x7, {0x000000, 8192 * 1024} },
Marc Jonesb2f90022014-04-29 17:37:23 -0600243
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100244 { .m = { .sec = 0, .tb = 1 }, 0, {0, 0} }, /* none */
245 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x7e0000, 128 * 1024} },
246 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x7c0000, 256 * 1024} },
247 { .m = { .sec = 0, .tb = 1 }, 0x3, {0x780000, 512 * 1024} },
248 { .m = { .sec = 0, .tb = 1 }, 0x4, {0x700000, 1024 * 1024} },
249 { .m = { .sec = 0, .tb = 1 }, 0x5, {0x600000, 2048 * 1024} },
250 { .m = { .sec = 0, .tb = 1 }, 0x6, {0x400000, 4096 * 1024} },
251 { .m = { .sec = 0, .tb = 1 }, 0x7, {0x000000, 8192 * 1024} },
Marc Jonesb2f90022014-04-29 17:37:23 -0600252};
253
David Hendricksf8f00c72011-02-01 12:39:46 -0800254/* mx25l1005 ranges also work for the mx25l1005c */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100255static struct wp_range_descriptor mx25l1005_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100256 { .m = { .sec = X, .tb = X }, 0, {0, 0} }, /* none */
257 { .m = { .sec = X, .tb = X }, 0x1, {0x010000, 64 * 1024} },
258 { .m = { .sec = X, .tb = X }, 0x2, {0x000000, 128 * 1024} },
259 { .m = { .sec = X, .tb = X }, 0x3, {0x000000, 128 * 1024} },
David Hendricksf8f00c72011-02-01 12:39:46 -0800260};
261
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100262static struct wp_range_descriptor mx25l2005_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100263 { .m = { .sec = X, .tb = X }, 0, {0, 0} }, /* none */
264 { .m = { .sec = X, .tb = X }, 0x1, {0x030000, 64 * 1024} },
265 { .m = { .sec = X, .tb = X }, 0x2, {0x020000, 128 * 1024} },
266 { .m = { .sec = X, .tb = X }, 0x3, {0x000000, 256 * 1024} },
David Hendricksf8f00c72011-02-01 12:39:46 -0800267};
268
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100269static struct wp_range_descriptor mx25l4005_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100270 { .m = { .sec = X, .tb = X }, 0, {0, 0} }, /* none */
271 { .m = { .sec = X, .tb = X }, 0x1, {0x070000, 64 * 1 * 1024} }, /* block 7 */
272 { .m = { .sec = X, .tb = X }, 0x2, {0x060000, 64 * 2 * 1024} }, /* blocks 6-7 */
273 { .m = { .sec = X, .tb = X }, 0x3, {0x040000, 64 * 4 * 1024} }, /* blocks 4-7 */
274 { .m = { .sec = X, .tb = X }, 0x4, {0x000000, 512 * 1024} },
275 { .m = { .sec = X, .tb = X }, 0x5, {0x000000, 512 * 1024} },
276 { .m = { .sec = X, .tb = X }, 0x6, {0x000000, 512 * 1024} },
277 { .m = { .sec = X, .tb = X }, 0x7, {0x000000, 512 * 1024} },
David Hendricksf8f00c72011-02-01 12:39:46 -0800278};
279
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100280static struct wp_range_descriptor mx25l8005_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100281 { .m = { .sec = X, .tb = X }, 0, {0, 0} }, /* none */
282 { .m = { .sec = X, .tb = X }, 0x1, {0x0f0000, 64 * 1 * 1024} }, /* block 15 */
283 { .m = { .sec = X, .tb = X }, 0x2, {0x0e0000, 64 * 2 * 1024} }, /* blocks 14-15 */
284 { .m = { .sec = X, .tb = X }, 0x3, {0x0c0000, 64 * 4 * 1024} }, /* blocks 12-15 */
285 { .m = { .sec = X, .tb = X }, 0x4, {0x080000, 64 * 8 * 1024} }, /* blocks 8-15 */
286 { .m = { .sec = X, .tb = X }, 0x5, {0x000000, 1024 * 1024} },
287 { .m = { .sec = X, .tb = X }, 0x6, {0x000000, 1024 * 1024} },
288 { .m = { .sec = X, .tb = X }, 0x7, {0x000000, 1024 * 1024} },
David Hendricksf8f00c72011-02-01 12:39:46 -0800289};
290
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100291static struct wp_range_descriptor mx25l1605d_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100292 { .m = { .sec = X, .tb = 0 }, 0, {0, 0} }, /* none */
293 { .m = { .sec = X, .tb = 0 }, 0x1, {0x1f0000, 64 * 1 * 1024} }, /* block 31 */
294 { .m = { .sec = X, .tb = 0 }, 0x2, {0x1e0000, 64 * 2 * 1024} }, /* blocks 30-31 */
295 { .m = { .sec = X, .tb = 0 }, 0x3, {0x1c0000, 64 * 4 * 1024} }, /* blocks 28-31 */
296 { .m = { .sec = X, .tb = 0 }, 0x4, {0x180000, 64 * 8 * 1024} }, /* blocks 24-31 */
297 { .m = { .sec = X, .tb = 0 }, 0x5, {0x100000, 64 * 16 * 1024} }, /* blocks 16-31 */
298 { .m = { .sec = X, .tb = 0 }, 0x6, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
299 { .m = { .sec = X, .tb = 0 }, 0x7, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
David Hendricksf8f00c72011-02-01 12:39:46 -0800300
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100301 { .m = { .sec = X, .tb = 1 }, 0x0, {0x000000, 2048 * 1024} },
302 { .m = { .sec = X, .tb = 1 }, 0x1, {0x000000, 2048 * 1024} },
303 { .m = { .sec = X, .tb = 1 }, 0x2, {0x000000, 64 * 16 * 1024} }, /* blocks 0-15 */
304 { .m = { .sec = X, .tb = 1 }, 0x3, {0x000000, 64 * 24 * 1024} }, /* blocks 0-23 */
305 { .m = { .sec = X, .tb = 1 }, 0x4, {0x000000, 64 * 28 * 1024} }, /* blocks 0-27 */
306 { .m = { .sec = X, .tb = 1 }, 0x5, {0x000000, 64 * 30 * 1024} }, /* blocks 0-29 */
307 { .m = { .sec = X, .tb = 1 }, 0x6, {0x000000, 64 * 31 * 1024} }, /* blocks 0-30 */
308 { .m = { .sec = X, .tb = 1 }, 0x7, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
David Hendricksf8f00c72011-02-01 12:39:46 -0800309};
310
311/* FIXME: Is there an mx25l3205 (without a trailing letter)? */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100312static struct wp_range_descriptor mx25l3205d_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100313 { .m = { .sec = X, .tb = 0 }, 0, {0, 0} }, /* none */
314 { .m = { .sec = X, .tb = 0 }, 0x1, {0x3f0000, 64 * 1024} },
315 { .m = { .sec = X, .tb = 0 }, 0x2, {0x3e0000, 128 * 1024} },
316 { .m = { .sec = X, .tb = 0 }, 0x3, {0x3c0000, 256 * 1024} },
317 { .m = { .sec = X, .tb = 0 }, 0x4, {0x380000, 512 * 1024} },
318 { .m = { .sec = X, .tb = 0 }, 0x5, {0x300000, 1024 * 1024} },
319 { .m = { .sec = X, .tb = 0 }, 0x6, {0x200000, 2048 * 1024} },
320 { .m = { .sec = X, .tb = 0 }, 0x7, {0x000000, 4096 * 1024} },
David Hendricksac72e362010-08-16 18:20:03 -0700321
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100322 { .m = { .sec = X, .tb = 1 }, 0x0, {0x000000, 4096 * 1024} },
323 { .m = { .sec = X, .tb = 1 }, 0x1, {0x000000, 2048 * 1024} },
324 { .m = { .sec = X, .tb = 1 }, 0x2, {0x000000, 3072 * 1024} },
325 { .m = { .sec = X, .tb = 1 }, 0x3, {0x000000, 3584 * 1024} },
326 { .m = { .sec = X, .tb = 1 }, 0x4, {0x000000, 3840 * 1024} },
327 { .m = { .sec = X, .tb = 1 }, 0x5, {0x000000, 3968 * 1024} },
328 { .m = { .sec = X, .tb = 1 }, 0x6, {0x000000, 4032 * 1024} },
329 { .m = { .sec = X, .tb = 1 }, 0x7, {0x000000, 4096 * 1024} },
David Hendricksac72e362010-08-16 18:20:03 -0700330};
331
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100332static struct wp_range_descriptor mx25u3235e_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100333 { .m = { .sec = X, .tb = 0 }, 0, {0, 0} }, /* none */
334 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x3f0000, 64 * 1024} },
335 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x3e0000, 128 * 1024} },
336 { .m = { .sec = 0, .tb = 0 }, 0x3, {0x3c0000, 256 * 1024} },
337 { .m = { .sec = 0, .tb = 0 }, 0x4, {0x380000, 512 * 1024} },
338 { .m = { .sec = 0, .tb = 0 }, 0x5, {0x300000, 1024 * 1024} },
339 { .m = { .sec = 0, .tb = 0 }, 0x6, {0x200000, 2048 * 1024} },
340 { .m = { .sec = 0, .tb = 0 }, 0x7, {0x000000, 4096 * 1024} },
Vincent Palatin87e092a2013-02-28 15:46:14 -0800341
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100342 { .m = { .sec = 0, .tb = 1 }, 0x0, {0x000000, 4096 * 1024} },
343 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x000000, 2048 * 1024} },
344 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x000000, 3072 * 1024} },
345 { .m = { .sec = 0, .tb = 1 }, 0x3, {0x000000, 3584 * 1024} },
346 { .m = { .sec = 0, .tb = 1 }, 0x4, {0x000000, 3840 * 1024} },
347 { .m = { .sec = 0, .tb = 1 }, 0x5, {0x000000, 3968 * 1024} },
348 { .m = { .sec = 0, .tb = 1 }, 0x6, {0x000000, 4032 * 1024} },
349 { .m = { .sec = 0, .tb = 1 }, 0x7, {0x000000, 4096 * 1024} },
Vincent Palatin87e092a2013-02-28 15:46:14 -0800350};
351
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100352static struct wp_range_descriptor mx25u6435e_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100353 { .m = { .sec = X, .tb = 0 }, 0, {0, 0} }, /* none */
354 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x7f0000, 1 * 64 * 1024} }, /* block 127 */
355 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
356 { .m = { .sec = 0, .tb = 0 }, 0x3, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
357 { .m = { .sec = 0, .tb = 0 }, 0x4, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
358 { .m = { .sec = 0, .tb = 0 }, 0x5, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
359 { .m = { .sec = 0, .tb = 0 }, 0x6, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
360 { .m = { .sec = 0, .tb = 0 }, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
Jongpil66a96492014-08-14 17:59:06 +0900361
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100362 { .m = { .sec = 0, .tb = 1 }, 0x0, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
363 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x000000, 96 * 64 * 1024} }, /* blocks 0-95 */
364 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x000000, 112 * 64 * 1024} }, /* blocks 0-111 */
365 { .m = { .sec = 0, .tb = 1 }, 0x3, {0x000000, 120 * 64 * 1024} }, /* blocks 0-119 */
366 { .m = { .sec = 0, .tb = 1 }, 0x4, {0x000000, 124 * 64 * 1024} }, /* blocks 0-123 */
367 { .m = { .sec = 0, .tb = 1 }, 0x5, {0x000000, 126 * 64 * 1024} }, /* blocks 0-125 */
368 { .m = { .sec = 0, .tb = 1 }, 0x6, {0x000000, 127 * 64 * 1024} }, /* blocks 0-126 */
369 { .m = { .sec = 0, .tb = 1 }, 0x7, {0x000000, 128 * 64 * 1024} }, /* blocks 0-127 */
Jongpil66a96492014-08-14 17:59:06 +0900370};
371
Karthikeyan Ramasubramanianfb166b72019-06-24 12:38:55 -0600372#define MX25U12835E_TB (1 << 3)
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100373static struct wp_range_descriptor mx25u12835e_tb0_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100374 { .m = { .sec = X, .tb = X }, 0, {0, 0} }, /* none */
375 { .m = { .sec = 0, .tb = 0 }, 0x1, {0xff0000, 1 * 64 * 1024} }, /* block 255 */
376 { .m = { .sec = 0, .tb = 0 }, 0x2, {0xfe0000, 2 * 64 * 1024} }, /* blocks 254-255 */
377 { .m = { .sec = 0, .tb = 0 }, 0x3, {0xfc0000, 4 * 64 * 1024} }, /* blocks 252-255 */
378 { .m = { .sec = 0, .tb = 0 }, 0x4, {0xf80000, 8 * 64 * 1024} }, /* blocks 248-255 */
379 { .m = { .sec = 0, .tb = 0 }, 0x5, {0xf00000, 16 * 64 * 1024} }, /* blocks 240-255 */
380 { .m = { .sec = 0, .tb = 0 }, 0x6, {0xe00000, 32 * 64 * 1024} }, /* blocks 224-255 */
381 { .m = { .sec = 0, .tb = 0 }, 0x7, {0xc00000, 64 * 64 * 1024} }, /* blocks 192-255 */
382 { .m = { .sec = 0, .tb = 0 }, 0x8, {0x800000, 128 * 64 * 1024} }, /* blocks 128-255 */
383 { .m = { .sec = 0, .tb = 0 }, 0x9, {0x000000, 256 * 64 * 1024} }, /* blocks all */
384 { .m = { .sec = 0, .tb = 0 }, 0xa, {0x000000, 256 * 64 * 1024} }, /* blocks all */
385 { .m = { .sec = 0, .tb = 0 }, 0xb, {0x000000, 256 * 64 * 1024} }, /* blocks all */
386 { .m = { .sec = 0, .tb = 0 }, 0xc, {0x000000, 256 * 64 * 1024} }, /* blocks all */
387 { .m = { .sec = 0, .tb = 0 }, 0xd, {0x000000, 256 * 64 * 1024} }, /* blocks all */
388 { .m = { .sec = 0, .tb = 0 }, 0xe, {0x000000, 256 * 64 * 1024} }, /* blocks all */
389 { .m = { .sec = 0, .tb = 0 }, 0xf, {0x000000, 256 * 64 * 1024} }, /* blocks all */
Karthikeyan Ramasubramanianfb166b72019-06-24 12:38:55 -0600390};
Alex Lu831c6092017-11-02 23:19:34 -0700391
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100392static struct wp_range_descriptor mx25u12835e_tb1_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100393 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x000000, 1 * 64 * 1024} }, /* block 0 */
394 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x000000, 2 * 64 * 1024} }, /* blocks 0-1 */
395 { .m = { .sec = 0, .tb = 1 }, 0x3, {0x000000, 4 * 64 * 1024} }, /* blocks 0-3 */
396 { .m = { .sec = 0, .tb = 1 }, 0x4, {0x000000, 8 * 64 * 1024} }, /* blocks 0-7 */
397 { .m = { .sec = 0, .tb = 1 }, 0x5, {0x000000, 16 * 64 * 1024} }, /* blocks 0-15 */
398 { .m = { .sec = 0, .tb = 1 }, 0x6, {0x000000, 32 * 64 * 1024} }, /* blocks 0-31 */
399 { .m = { .sec = 0, .tb = 1 }, 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
400 { .m = { .sec = 0, .tb = 1 }, 0x8, {0x000000, 128 * 64 * 1024} }, /* blocks 0-127 */
401 { .m = { .sec = 0, .tb = 1 }, 0x9, {0x000000, 256 * 64 * 1024} }, /* blocks all */
402 { .m = { .sec = 0, .tb = 1 }, 0xa, {0x000000, 256 * 64 * 1024} }, /* blocks all */
403 { .m = { .sec = 0, .tb = 1 }, 0xb, {0x000000, 256 * 64 * 1024} }, /* blocks all */
404 { .m = { .sec = 0, .tb = 1 }, 0xc, {0x000000, 256 * 64 * 1024} }, /* blocks all */
405 { .m = { .sec = 0, .tb = 1 }, 0xd, {0x000000, 256 * 64 * 1024} }, /* blocks all */
406 { .m = { .sec = 0, .tb = 1 }, 0xe, {0x000000, 256 * 64 * 1024} }, /* blocks all */
407 { .m = { .sec = 0, .tb = 1 }, 0xf, {0x000000, 256 * 64 * 1024} }, /* blocks all */
Alex Lu831c6092017-11-02 23:19:34 -0700408};
409
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100410static struct wp_range_descriptor n25q064_ranges[] = {
David Hendricksfe9123b2015-04-21 13:18:31 -0700411 /*
412 * Note: For N25Q064, sec (usually in bit position 6) is called BP3
413 * (block protect bit 3). It is only useful when all blocks are to
414 * be write-protected.
415 */
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100416 { .m = { .sec = 0, .tb = 0 }, 0, {0, 0} }, /* none */
David Hendricksbfa624b2012-07-24 12:47:59 -0700417
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100418 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x7f0000, 64 * 1024} }, /* block 127 */
419 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
420 { .m = { .sec = 0, .tb = 0 }, 0x3, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
421 { .m = { .sec = 0, .tb = 0 }, 0x4, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
422 { .m = { .sec = 0, .tb = 0 }, 0x5, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
423 { .m = { .sec = 0, .tb = 0 }, 0x6, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
424 { .m = { .sec = 0, .tb = 0 }, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
David Hendricksbfa624b2012-07-24 12:47:59 -0700425
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100426 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x000000, 64 * 1024} }, /* block 0 */
427 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x000000, 2 * 64 * 1024} }, /* blocks 0-1 */
428 { .m = { .sec = 0, .tb = 1 }, 0x3, {0x000000, 4 * 64 * 1024} }, /* blocks 0-3 */
429 { .m = { .sec = 0, .tb = 1 }, 0x4, {0x000000, 8 * 64 * 1024} }, /* blocks 0-7 */
430 { .m = { .sec = 0, .tb = 1 }, 0x5, {0x000000, 16 * 64 * 1024} }, /* blocks 0-15 */
431 { .m = { .sec = 0, .tb = 1 }, 0x6, {0x000000, 32 * 64 * 1024} }, /* blocks 0-31 */
432 { .m = { .sec = 0, .tb = 1 }, 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
David Hendricksbfa624b2012-07-24 12:47:59 -0700433
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100434 { .m = { .sec = X, .tb = 1 }, 0x0, {0x000000, 128 * 64 * 1024} }, /* all */
435 { .m = { .sec = X, .tb = 1 }, 0x1, {0x000000, 128 * 64 * 1024} }, /* all */
436 { .m = { .sec = X, .tb = 1 }, 0x2, {0x000000, 128 * 64 * 1024} }, /* all */
437 { .m = { .sec = X, .tb = 1 }, 0x3, {0x000000, 128 * 64 * 1024} }, /* all */
438 { .m = { .sec = X, .tb = 1 }, 0x4, {0x000000, 128 * 64 * 1024} }, /* all */
439 { .m = { .sec = X, .tb = 1 }, 0x5, {0x000000, 128 * 64 * 1024} }, /* all */
440 { .m = { .sec = X, .tb = 1 }, 0x6, {0x000000, 128 * 64 * 1024} }, /* all */
441 { .m = { .sec = X, .tb = 1 }, 0x7, {0x000000, 128 * 64 * 1024} }, /* all */
David Hendricksbfa624b2012-07-24 12:47:59 -0700442};
443
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100444static struct wp_range_descriptor w25q16_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100445 { .m = { .sec = X, .tb = X }, 0, {0, 0} }, /* none */
446 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x1f0000, 64 * 1024} },
447 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x1e0000, 128 * 1024} },
448 { .m = { .sec = 0, .tb = 0 }, 0x3, {0x1c0000, 256 * 1024} },
449 { .m = { .sec = 0, .tb = 0 }, 0x4, {0x180000, 512 * 1024} },
450 { .m = { .sec = 0, .tb = 0 }, 0x5, {0x100000, 1024 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700451
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100452 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x000000, 64 * 1024} },
453 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x000000, 128 * 1024} },
454 { .m = { .sec = 0, .tb = 1 }, 0x3, {0x000000, 256 * 1024} },
455 { .m = { .sec = 0, .tb = 1 }, 0x4, {0x000000, 512 * 1024} },
456 { .m = { .sec = 0, .tb = 1 }, 0x5, {0x000000, 1024 * 1024} },
457 { .m = { .sec = X, .tb = X }, 0x6, {0x000000, 2048 * 1024} },
458 { .m = { .sec = X, .tb = X }, 0x7, {0x000000, 2048 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700459
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100460 { .m = { .sec = 1, .tb = 0 }, 0x1, {0x1ff000, 4 * 1024} },
461 { .m = { .sec = 1, .tb = 0 }, 0x2, {0x1fe000, 8 * 1024} },
462 { .m = { .sec = 1, .tb = 0 }, 0x3, {0x1fc000, 16 * 1024} },
463 { .m = { .sec = 1, .tb = 0 }, 0x4, {0x1f8000, 32 * 1024} },
464 { .m = { .sec = 1, .tb = 0 }, 0x5, {0x1f8000, 32 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700465
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100466 { .m = { .sec = 1, .tb = 1 }, 0x1, {0x000000, 4 * 1024} },
467 { .m = { .sec = 1, .tb = 1 }, 0x2, {0x000000, 8 * 1024} },
468 { .m = { .sec = 1, .tb = 1 }, 0x3, {0x000000, 16 * 1024} },
469 { .m = { .sec = 1, .tb = 1 }, 0x4, {0x000000, 32 * 1024} },
470 { .m = { .sec = 1, .tb = 1 }, 0x5, {0x000000, 32 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700471};
472
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100473static struct wp_range_descriptor w25q32_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100474 { .m = { .sec = X, .tb = X }, 0, {0, 0} }, /* none */
475 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x3f0000, 64 * 1024} },
476 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x3e0000, 128 * 1024} },
477 { .m = { .sec = 0, .tb = 0 }, 0x3, {0x3c0000, 256 * 1024} },
478 { .m = { .sec = 0, .tb = 0 }, 0x4, {0x380000, 512 * 1024} },
479 { .m = { .sec = 0, .tb = 0 }, 0x5, {0x300000, 1024 * 1024} },
480 { .m = { .sec = 0, .tb = 0 }, 0x6, {0x200000, 2048 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700481
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100482 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x000000, 64 * 1024} },
483 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x000000, 128 * 1024} },
484 { .m = { .sec = 0, .tb = 1 }, 0x3, {0x000000, 256 * 1024} },
485 { .m = { .sec = 0, .tb = 1 }, 0x4, {0x000000, 512 * 1024} },
486 { .m = { .sec = 0, .tb = 1 }, 0x5, {0x000000, 1024 * 1024} },
487 { .m = { .sec = 0, .tb = 1 }, 0x6, {0x000000, 2048 * 1024} },
488 { .m = { .sec = X, .tb = X }, 0x7, {0x000000, 4096 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700489
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100490 { .m = { .sec = 1, .tb = 0 }, 0x1, {0x3ff000, 4 * 1024} },
491 { .m = { .sec = 1, .tb = 0 }, 0x2, {0x3fe000, 8 * 1024} },
492 { .m = { .sec = 1, .tb = 0 }, 0x3, {0x3fc000, 16 * 1024} },
493 { .m = { .sec = 1, .tb = 0 }, 0x4, {0x3f8000, 32 * 1024} },
494 { .m = { .sec = 1, .tb = 0 }, 0x5, {0x3f8000, 32 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700495
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100496 { .m = { .sec = 1, .tb = 1 }, 0x1, {0x000000, 4 * 1024} },
497 { .m = { .sec = 1, .tb = 1 }, 0x2, {0x000000, 8 * 1024} },
498 { .m = { .sec = 1, .tb = 1 }, 0x3, {0x000000, 16 * 1024} },
499 { .m = { .sec = 1, .tb = 1 }, 0x4, {0x000000, 32 * 1024} },
500 { .m = { .sec = 1, .tb = 1 }, 0x5, {0x000000, 32 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700501};
502
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100503static struct wp_range_descriptor w25q80_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100504 { .m = { .sec = X, .tb = X }, 0, {0, 0} }, /* none */
505 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x0f0000, 64 * 1024} },
506 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x0e0000, 128 * 1024} },
507 { .m = { .sec = 0, .tb = 0 }, 0x3, {0x0c0000, 256 * 1024} },
508 { .m = { .sec = 0, .tb = 0 }, 0x4, {0x080000, 512 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700509
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100510 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x000000, 64 * 1024} },
511 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x000000, 128 * 1024} },
512 { .m = { .sec = 0, .tb = 1 }, 0x3, {0x000000, 256 * 1024} },
513 { .m = { .sec = 0, .tb = 1 }, 0x4, {0x000000, 512 * 1024} },
514 { .m = { .sec = X, .tb = X }, 0x6, {0x000000, 1024 * 1024} },
515 { .m = { .sec = X, .tb = X }, 0x7, {0x000000, 1024 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700516
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100517 { .m = { .sec = 1, .tb = 0 }, 0x1, {0x1ff000, 4 * 1024} },
518 { .m = { .sec = 1, .tb = 0 }, 0x2, {0x1fe000, 8 * 1024} },
519 { .m = { .sec = 1, .tb = 0 }, 0x3, {0x1fc000, 16 * 1024} },
520 { .m = { .sec = 1, .tb = 0 }, 0x4, {0x1f8000, 32 * 1024} },
521 { .m = { .sec = 1, .tb = 0 }, 0x5, {0x1f8000, 32 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700522
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100523 { .m = { .sec = 1, .tb = 1 }, 0x1, {0x000000, 4 * 1024} },
524 { .m = { .sec = 1, .tb = 1 }, 0x2, {0x000000, 8 * 1024} },
525 { .m = { .sec = 1, .tb = 1 }, 0x3, {0x000000, 16 * 1024} },
526 { .m = { .sec = 1, .tb = 1 }, 0x4, {0x000000, 32 * 1024} },
527 { .m = { .sec = 1, .tb = 1 }, 0x5, {0x000000, 32 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700528};
529
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100530static struct wp_range_descriptor w25q64_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100531 { .m = { .sec = X, .tb = X }, 0, {0, 0} }, /* none */
David Hendricks2c4a76c2010-06-28 14:00:43 -0700532
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100533 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x7e0000, 128 * 1024} },
534 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x7c0000, 256 * 1024} },
535 { .m = { .sec = 0, .tb = 0 }, 0x3, {0x780000, 512 * 1024} },
536 { .m = { .sec = 0, .tb = 0 }, 0x4, {0x700000, 1024 * 1024} },
537 { .m = { .sec = 0, .tb = 0 }, 0x5, {0x600000, 2048 * 1024} },
538 { .m = { .sec = 0, .tb = 0 }, 0x6, {0x400000, 4096 * 1024} },
David Hendricks2c4a76c2010-06-28 14:00:43 -0700539
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100540 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x000000, 128 * 1024} },
541 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x000000, 256 * 1024} },
542 { .m = { .sec = 0, .tb = 1 }, 0x3, {0x000000, 512 * 1024} },
543 { .m = { .sec = 0, .tb = 1 }, 0x4, {0x000000, 1024 * 1024} },
544 { .m = { .sec = 0, .tb = 1 }, 0x5, {0x000000, 2048 * 1024} },
545 { .m = { .sec = 0, .tb = 1 }, 0x6, {0x000000, 4096 * 1024} },
546 { .m = { .sec = X, .tb = X }, 0x7, {0x000000, 8192 * 1024} },
David Hendricks2c4a76c2010-06-28 14:00:43 -0700547
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100548 { .m = { .sec = 1, .tb = 0 }, 0x1, {0x7ff000, 4 * 1024} },
549 { .m = { .sec = 1, .tb = 0 }, 0x2, {0x7fe000, 8 * 1024} },
550 { .m = { .sec = 1, .tb = 0 }, 0x3, {0x7fc000, 16 * 1024} },
551 { .m = { .sec = 1, .tb = 0 }, 0x4, {0x7f8000, 32 * 1024} },
552 { .m = { .sec = 1, .tb = 0 }, 0x5, {0x7f8000, 32 * 1024} },
David Hendricks2c4a76c2010-06-28 14:00:43 -0700553
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100554 { .m = { .sec = 1, .tb = 1 }, 0x1, {0x000000, 4 * 1024} },
555 { .m = { .sec = 1, .tb = 1 }, 0x2, {0x000000, 8 * 1024} },
556 { .m = { .sec = 1, .tb = 1 }, 0x3, {0x000000, 16 * 1024} },
557 { .m = { .sec = 1, .tb = 1 }, 0x4, {0x000000, 32 * 1024} },
558 { .m = { .sec = 1, .tb = 1 }, 0x5, {0x000000, 32 * 1024} },
David Hendricks2c4a76c2010-06-28 14:00:43 -0700559};
560
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100561static struct wp_range_descriptor w25rq128_cmp0_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100562 { .m = { .sec = X, .tb = X }, 0, {0, 0} }, /* NONE */
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530563
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100564 { .m = { .sec = 0, .tb = 0 }, 0x1, {0xfc0000, 256 * 1024} }, /* Upper 1/64 */
565 { .m = { .sec = 0, .tb = 0 }, 0x2, {0xf80000, 512 * 1024} }, /* Upper 1/32 */
566 { .m = { .sec = 0, .tb = 0 }, 0x3, {0xf00000, 1024 * 1024} }, /* Upper 1/16 */
567 { .m = { .sec = 0, .tb = 0 }, 0x4, {0xe00000, 2048 * 1024} }, /* Upper 1/8 */
568 { .m = { .sec = 0, .tb = 0 }, 0x5, {0xc00000, 4096 * 1024} }, /* Upper 1/4 */
569 { .m = { .sec = 0, .tb = 0 }, 0x6, {0x800000, 8192 * 1024} }, /* Upper 1/2 */
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530570
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100571 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x000000, 256 * 1024} }, /* Lower 1/64 */
572 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x000000, 512 * 1024} }, /* Lower 1/32 */
573 { .m = { .sec = 0, .tb = 1 }, 0x3, {0x000000, 1024 * 1024} }, /* Lower 1/16 */
574 { .m = { .sec = 0, .tb = 1 }, 0x4, {0x000000, 2048 * 1024} }, /* Lower 1/8 */
575 { .m = { .sec = 0, .tb = 1 }, 0x5, {0x000000, 4096 * 1024} }, /* Lower 1/4 */
576 { .m = { .sec = 0, .tb = 1 }, 0x6, {0x000000, 8192 * 1024} }, /* Lower 1/2 */
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530577
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100578 { .m = { .sec = X, .tb = X }, 0x7, {0x000000, 16384 * 1024} }, /* ALL */
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530579
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100580 { .m = { .sec = 1, .tb = 0 }, 0x1, {0xfff000, 4 * 1024} }, /* Upper 1/4096 */
581 { .m = { .sec = 1, .tb = 0 }, 0x2, {0xffe000, 8 * 1024} }, /* Upper 1/2048 */
582 { .m = { .sec = 1, .tb = 0 }, 0x3, {0xffc000, 16 * 1024} }, /* Upper 1/1024 */
583 { .m = { .sec = 1, .tb = 0 }, 0x4, {0xff8000, 32 * 1024} }, /* Upper 1/512 */
584 { .m = { .sec = 1, .tb = 0 }, 0x5, {0xff8000, 32 * 1024} }, /* Upper 1/512 */
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700585
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100586 { .m = { .sec = 1, .tb = 1 }, 0x1, {0x000000, 4 * 1024} }, /* Lower 1/4096 */
587 { .m = { .sec = 1, .tb = 1 }, 0x2, {0x000000, 8 * 1024} }, /* Lower 1/2048 */
588 { .m = { .sec = 1, .tb = 1 }, 0x3, {0x000000, 16 * 1024} }, /* Lower 1/1024 */
589 { .m = { .sec = 1, .tb = 1 }, 0x4, {0x000000, 32 * 1024} }, /* Lower 1/512 */
590 { .m = { .sec = 1, .tb = 1 }, 0x5, {0x000000, 32 * 1024} }, /* Lower 1/512 */
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700591};
592
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100593static struct wp_range_descriptor w25rq128_cmp1_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100594 { .m = { .sec = X, .tb = X }, 0x0, {0x000000, 16 * 1024 * 1024} }, /* ALL */
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700595
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100596 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x000000, 16128 * 1024} }, /* Lower 63/64 */
597 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x000000, 15872 * 1024} }, /* Lower 31/32 */
598 { .m = { .sec = 0, .tb = 0 }, 0x3, {0x000000, 15 * 1024 * 1024} }, /* Lower 15/16 */
599 { .m = { .sec = 0, .tb = 0 }, 0x4, {0x000000, 14 * 1024 * 1024} }, /* Lower 7/8 */
600 { .m = { .sec = 0, .tb = 0 }, 0x5, {0x000000, 12 * 1024 * 1024} }, /* Lower 3/4 */
601 { .m = { .sec = 0, .tb = 0 }, 0x6, {0x000000, 8 * 1024 * 1024} }, /* Lower 1/2 */
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700602
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100603 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x040000, 16128 * 1024} }, /* Upper 63/64 */
604 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x080000, 15872 * 1024} }, /* Upper 31/32 */
605 { .m = { .sec = 0, .tb = 1 }, 0x3, {0x100000, 15 * 1024 * 1024} }, /* Upper 15/16 */
606 { .m = { .sec = 0, .tb = 1 }, 0x4, {0x200000, 14 * 1024 * 1024} }, /* Upper 7/8 */
607 { .m = { .sec = 0, .tb = 1 }, 0x5, {0x400000, 12 * 1024 * 1024} }, /* Upper 3/4 */
608 { .m = { .sec = 0, .tb = 1 }, 0x6, {0x800000, 8 * 1024 * 1024} }, /* Upper 1/2 */
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700609
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100610 { .m = { .sec = X, .tb = X }, 0x7, {0x000000, 0} }, /* NONE */
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700611
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100612 { .m = { .sec = 1, .tb = 0 }, 0x1, {0x000000, 16380 * 1024} }, /* Lower 4095/4096 */
613 { .m = { .sec = 1, .tb = 0 }, 0x2, {0x000000, 16376 * 1024} }, /* Lower 2048/2048 */
614 { .m = { .sec = 1, .tb = 0 }, 0x3, {0x000000, 16368 * 1024} }, /* Lower 1023/1024 */
615 { .m = { .sec = 1, .tb = 0 }, 0x4, {0x000000, 16352 * 1024} }, /* Lower 511/512 */
616 { .m = { .sec = 1, .tb = 0 }, 0x5, {0x000000, 16352 * 1024} }, /* Lower 511/512 */
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700617
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100618 { .m = { .sec = 1, .tb = 1 }, 0x1, {0x001000, 16380 * 1024} }, /* Upper 4095/4096 */
619 { .m = { .sec = 1, .tb = 1 }, 0x2, {0x002000, 16376 * 1024} }, /* Upper 2047/2048 */
620 { .m = { .sec = 1, .tb = 1 }, 0x3, {0x004000, 16368 * 1024} }, /* Upper 1023/1024 */
621 { .m = { .sec = 1, .tb = 1 }, 0x4, {0x008000, 16352 * 1024} }, /* Upper 511/512 */
622 { .m = { .sec = 1, .tb = 1 }, 0x5, {0x008000, 16352 * 1024} }, /* Upper 511/512 */
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530623};
624
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100625static struct wp_range_descriptor w25rq256_cmp0_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100626 { .m = { .sec = X, .tb = X }, 0x0, {0x0000000, 0x0000000} }, /* NONE */
Duncan Laurie1801f7c2019-01-09 18:02:51 -0800627
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100628 { .m = { .sec = X, .tb = 0 }, 0x1, {0x1ff0000, 64 * 1 * 1024} }, /* Upper 1/512 */
629 { .m = { .sec = X, .tb = 0 }, 0x2, {0x1fe0000, 64 * 2 * 1024} }, /* Upper 1/256 */
630 { .m = { .sec = X, .tb = 0 }, 0x3, {0x1fc0000, 64 * 4 * 1024} }, /* Upper 1/128 */
631 { .m = { .sec = X, .tb = 0 }, 0x4, {0x1f80000, 64 * 8 * 1024} }, /* Upper 1/64 */
632 { .m = { .sec = X, .tb = 0 }, 0x5, {0x1f00000, 64 * 16 * 1024} }, /* Upper 1/32 */
633 { .m = { .sec = X, .tb = 0 }, 0x6, {0x1e00000, 64 * 32 * 1024} }, /* Upper 1/16 */
634 { .m = { .sec = X, .tb = 0 }, 0x7, {0x1c00000, 64 * 64 * 1024} }, /* Upper 1/8 */
635 { .m = { .sec = X, .tb = 0 }, 0x8, {0x1800000, 64 * 128 * 1024} }, /* Upper 1/4 */
636 { .m = { .sec = X, .tb = 0 }, 0x9, {0x1000000, 64 * 256 * 1024} }, /* Upper 1/2 */
Duncan Laurie1801f7c2019-01-09 18:02:51 -0800637
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100638 { .m = { .sec = X, .tb = 1 }, 0x1, {0x0000000, 64 * 1 * 1024} }, /* Lower 1/512 */
639 { .m = { .sec = X, .tb = 1 }, 0x2, {0x0000000, 64 * 2 * 1024} }, /* Lower 1/256 */
640 { .m = { .sec = X, .tb = 1 }, 0x3, {0x0000000, 64 * 4 * 1024} }, /* Lower 1/128 */
641 { .m = { .sec = X, .tb = 1 }, 0x4, {0x0000000, 64 * 8 * 1024} }, /* Lower 1/64 */
642 { .m = { .sec = X, .tb = 1 }, 0x5, {0x0000000, 64 * 16 * 1024} }, /* Lower 1/32 */
643 { .m = { .sec = X, .tb = 1 }, 0x6, {0x0000000, 64 * 32 * 1024} }, /* Lower 1/16 */
644 { .m = { .sec = X, .tb = 1 }, 0x7, {0x0000000, 64 * 64 * 1024} }, /* Lower 1/8 */
645 { .m = { .sec = X, .tb = 1 }, 0x8, {0x0000000, 64 * 128 * 1024} }, /* Lower 1/4 */
646 { .m = { .sec = X, .tb = 1 }, 0x9, {0x0000000, 64 * 256 * 1024} }, /* Lower 1/2 */
Duncan Laurie1801f7c2019-01-09 18:02:51 -0800647
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100648 { .m = { .sec = X, .tb = X }, 0xa, {0x0000000, 64 * 512 * 1024} }, /* ALL */
649 { .m = { .sec = X, .tb = X }, 0xb, {0x0000000, 64 * 512 * 1024} }, /* ALL */
650 { .m = { .sec = X, .tb = X }, 0xc, {0x0000000, 64 * 512 * 1024} }, /* ALL */
651 { .m = { .sec = X, .tb = X }, 0xd, {0x0000000, 64 * 512 * 1024} }, /* ALL */
652 { .m = { .sec = X, .tb = X }, 0xe, {0x0000000, 64 * 512 * 1024} }, /* ALL */
653 { .m = { .sec = X, .tb = X }, 0xf, {0x0000000, 64 * 512 * 1024} }, /* ALL */
Duncan Laurie1801f7c2019-01-09 18:02:51 -0800654};
655
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100656static struct wp_range_descriptor w25rq256_cmp1_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100657 { .m = { .sec = X, .tb = X }, 0x0, {0x0000000, 64 * 512 * 1024} }, /* ALL */
Duncan Laurie1801f7c2019-01-09 18:02:51 -0800658
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100659 { .m = { .sec = X, .tb = 0 }, 0x1, {0x0000000, 64 * 511 * 1024} }, /* Lower 511/512 */
660 { .m = { .sec = X, .tb = 0 }, 0x2, {0x0000000, 64 * 510 * 1024} }, /* Lower 255/256 */
661 { .m = { .sec = X, .tb = 0 }, 0x3, {0x0000000, 64 * 508 * 1024} }, /* Lower 127/128 */
662 { .m = { .sec = X, .tb = 0 }, 0x4, {0x0000000, 64 * 504 * 1024} }, /* Lower 63/64 */
663 { .m = { .sec = X, .tb = 0 }, 0x5, {0x0000000, 64 * 496 * 1024} }, /* Lower 31/32 */
664 { .m = { .sec = X, .tb = 0 }, 0x6, {0x0000000, 64 * 480 * 1024} }, /* Lower 15/16 */
665 { .m = { .sec = X, .tb = 0 }, 0x7, {0x0000000, 64 * 448 * 1024} }, /* Lower 7/8 */
666 { .m = { .sec = X, .tb = 0 }, 0x8, {0x0000000, 64 * 384 * 1024} }, /* Lower 3/4 */
667 { .m = { .sec = X, .tb = 0 }, 0x9, {0x0000000, 64 * 256 * 1024} }, /* Lower 1/2 */
Duncan Laurie1801f7c2019-01-09 18:02:51 -0800668
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100669 { .m = { .sec = X, .tb = 1 }, 0x1, {0x0010000, 64 * 511 * 1024} }, /* Upper 511/512 */
670 { .m = { .sec = X, .tb = 1 }, 0x2, {0x0020000, 64 * 510 * 1024} }, /* Upper 255/256 */
671 { .m = { .sec = X, .tb = 1 }, 0x3, {0x0040000, 64 * 508 * 1024} }, /* Upper 127/128 */
672 { .m = { .sec = X, .tb = 1 }, 0x4, {0x0080000, 64 * 504 * 1024} }, /* Upper 63/64 */
673 { .m = { .sec = X, .tb = 1 }, 0x5, {0x0100000, 64 * 496 * 1024} }, /* Upper 31/32 */
674 { .m = { .sec = X, .tb = 1 }, 0x6, {0x0200000, 64 * 480 * 1024} }, /* Upper 15/16 */
675 { .m = { .sec = X, .tb = 1 }, 0x7, {0x0400000, 64 * 448 * 1024} }, /* Upper 7/8 */
676 { .m = { .sec = X, .tb = 1 }, 0x8, {0x0800000, 64 * 384 * 1024} }, /* Upper 3/4 */
677 { .m = { .sec = X, .tb = 1 }, 0x9, {0x1000000, 64 * 256 * 1024} }, /* Upper 1/2 */
Duncan Laurie1801f7c2019-01-09 18:02:51 -0800678
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100679 { .m = { .sec = X, .tb = X }, 0xa, {0x0000000, 0x0000000} }, /* NONE */
680 { .m = { .sec = X, .tb = X }, 0xb, {0x0000000, 0x0000000} }, /* NONE */
681 { .m = { .sec = X, .tb = X }, 0xc, {0x0000000, 0x0000000} }, /* NONE */
682 { .m = { .sec = X, .tb = X }, 0xd, {0x0000000, 0x0000000} }, /* NONE */
683 { .m = { .sec = X, .tb = X }, 0xe, {0x0000000, 0x0000000} }, /* NONE */
684 { .m = { .sec = X, .tb = X }, 0xf, {0x0000000, 0x0000000} }, /* NONE */
Duncan Laurie1801f7c2019-01-09 18:02:51 -0800685};
686
Edward O'Callaghan3b996502020-04-12 20:46:51 +1000687static struct wp_range_descriptor w25x10_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100688 { .m = { .sec = X, .tb = X }, 0, {0, 0} }, /* none */
689 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x010000, 64 * 1024} },
690 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x000000, 64 * 1024} },
691 { .m = { .sec = X, .tb = X }, 0x2, {0x000000, 128 * 1024} },
692 { .m = { .sec = X, .tb = X }, 0x3, {0x000000, 128 * 1024} },
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800693};
694
Edward O'Callaghan3b996502020-04-12 20:46:51 +1000695static struct wp_range_descriptor w25x20_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100696 { .m = { .sec = X, .tb = X }, 0, {0, 0} }, /* none */
697 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x030000, 64 * 1024} },
698 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x020000, 128 * 1024} },
699 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x000000, 64 * 1024} },
700 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x000000, 128 * 1024} },
701 { .m = { .sec = 0, .tb = X }, 0x3, {0x000000, 256 * 1024} },
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800702};
703
Edward O'Callaghan3b996502020-04-12 20:46:51 +1000704static struct wp_range_descriptor w25x40_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100705 { .m = { .sec = X, .tb = X }, 0, {0, 0} }, /* none */
706 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x070000, 64 * 1024} },
707 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x060000, 128 * 1024} },
708 { .m = { .sec = 0, .tb = 0 }, 0x3, {0x040000, 256 * 1024} },
709 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x000000, 64 * 1024} },
710 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x000000, 128 * 1024} },
711 { .m = { .sec = 0, .tb = 1 }, 0x3, {0x000000, 256 * 1024} },
712 { .m = { .sec = 0, .tb = X }, 0x4, {0x000000, 512 * 1024} },
713 { .m = { .sec = 0, .tb = X }, 0x5, {0x000000, 512 * 1024} },
714 { .m = { .sec = 0, .tb = X }, 0x6, {0x000000, 512 * 1024} },
715 { .m = { .sec = 0, .tb = X }, 0x7, {0x000000, 512 * 1024} },
David Hendricks470ca952010-08-13 14:01:53 -0700716};
717
Edward O'Callaghan3b996502020-04-12 20:46:51 +1000718static struct wp_range_descriptor w25x80_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100719 { .m = { .sec = X, .tb = X }, 0, {0, 0} }, /* none */
720 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x0F0000, 64 * 1024} },
721 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x0E0000, 128 * 1024} },
722 { .m = { .sec = 0, .tb = 0 }, 0x3, {0x0C0000, 256 * 1024} },
723 { .m = { .sec = 0, .tb = 0 }, 0x4, {0x080000, 512 * 1024} },
724 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x000000, 64 * 1024} },
725 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x000000, 128 * 1024} },
726 { .m = { .sec = 0, .tb = 1 }, 0x3, {0x000000, 256 * 1024} },
727 { .m = { .sec = 0, .tb = 1 }, 0x4, {0x000000, 512 * 1024} },
728 { .m = { .sec = 0, .tb = X }, 0x5, {0x000000, 1024 * 1024} },
729 { .m = { .sec = 0, .tb = X }, 0x6, {0x000000, 1024 * 1024} },
730 { .m = { .sec = 0, .tb = X }, 0x7, {0x000000, 1024 * 1024} },
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800731};
732
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100733static struct wp_range_descriptor gd25q40_cmp0_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100734 { .m = { .sec = X, .tb = X }, 0, {0, 0} }, /* None */
735 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x070000, 64 * 1024} },
736 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x060000, 128 * 1024} },
737 { .m = { .sec = 0, .tb = 0 }, 0x3, {0x040000, 256 * 1024} },
738 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x000000, 64 * 1024} },
739 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x000000, 128 * 1024} },
740 { .m = { .sec = 0, .tb = 1 }, 0x3, {0x000000, 256 * 1024} },
741 { .m = { .sec = 0, .tb = X }, 0x4, {0x000000, 512 * 1024} }, /* All */
742 { .m = { .sec = 0, .tb = X }, 0x5, {0x000000, 512 * 1024} }, /* All */
743 { .m = { .sec = 0, .tb = X }, 0x6, {0x000000, 512 * 1024} }, /* All */
744 { .m = { .sec = 0, .tb = X }, 0x7, {0x000000, 512 * 1024} }, /* All */
745 { .m = { .sec = 1, .tb = 0 }, 0x1, {0x07F000, 4 * 1024} },
746 { .m = { .sec = 1, .tb = 0 }, 0x2, {0x07E000, 8 * 1024} },
747 { .m = { .sec = 1, .tb = 0 }, 0x3, {0x07C000, 16 * 1024} },
748 { .m = { .sec = 1, .tb = 0 }, 0x4, {0x078000, 32 * 1024} },
749 { .m = { .sec = 1, .tb = 0 }, 0x5, {0x078000, 32 * 1024} },
750 { .m = { .sec = 1, .tb = 0 }, 0x6, {0x078000, 32 * 1024} },
751 { .m = { .sec = 1, .tb = 1 }, 0x1, {0x000000, 4 * 1024} },
752 { .m = { .sec = 1, .tb = 1 }, 0x2, {0x000000, 8 * 1024} },
753 { .m = { .sec = 1, .tb = 1 }, 0x3, {0x000000, 16 * 1024} },
754 { .m = { .sec = 1, .tb = 1 }, 0x4, {0x000000, 32 * 1024} },
755 { .m = { .sec = 1, .tb = 1 }, 0x5, {0x000000, 32 * 1024} },
756 { .m = { .sec = 1, .tb = 1 }, 0x6, {0x000000, 32 * 1024} },
757 { .m = { .sec = 1, .tb = X }, 0x7, {0x000000, 512 * 1024} }, /* All */
Martin Rothf3c3d5f2017-04-28 14:56:41 -0600758};
759
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100760static struct wp_range_descriptor gd25q40_cmp1_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100761 { .m = { .sec = X, .tb = X }, 0x0, {0x000000, 512 * 1024} }, /* ALL */
762 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x000000, 448 * 1024} },
763 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x000000, 384 * 1024} },
764 { .m = { .sec = 0, .tb = 0 }, 0x3, {0x000000, 256 * 1024} },
Martin Rothf3c3d5f2017-04-28 14:56:41 -0600765
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100766 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x010000, 448 * 1024} },
767 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x020000, 384 * 1024} },
768 { .m = { .sec = 0, .tb = 1 }, 0x3, {0x040000, 256 * 1024} },
Martin Rothf3c3d5f2017-04-28 14:56:41 -0600769
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100770 { .m = { .sec = 0, .tb = X }, 0x4, {0x000000, 0} }, /* None */
771 { .m = { .sec = 0, .tb = X }, 0x5, {0x000000, 0} }, /* None */
772 { .m = { .sec = 0, .tb = X }, 0x6, {0x000000, 0} }, /* None */
773 { .m = { .sec = 0, .tb = X }, 0x7, {0x000000, 0} }, /* None */
Martin Rothf3c3d5f2017-04-28 14:56:41 -0600774
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100775 { .m = { .sec = 1, .tb = 0 }, 0x1, {0x000000, 508 * 1024} },
776 { .m = { .sec = 1, .tb = 0 }, 0x2, {0x000000, 504 * 1024} },
777 { .m = { .sec = 1, .tb = 0 }, 0x3, {0x000000, 496 * 1024} },
778 { .m = { .sec = 1, .tb = 0 }, 0x4, {0x000000, 480 * 1024} },
779 { .m = { .sec = 1, .tb = 0 }, 0x5, {0x000000, 480 * 1024} },
780 { .m = { .sec = 1, .tb = 0 }, 0x6, {0x000000, 480 * 1024} },
Martin Rothf3c3d5f2017-04-28 14:56:41 -0600781
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100782 { .m = { .sec = 1, .tb = 1 }, 0x1, {0x001000, 508 * 1024} },
783 { .m = { .sec = 1, .tb = 1 }, 0x2, {0x002000, 504 * 1024} },
784 { .m = { .sec = 1, .tb = 1 }, 0x3, {0x004000, 496 * 1024} },
785 { .m = { .sec = 1, .tb = 1 }, 0x4, {0x008000, 480 * 1024} },
786 { .m = { .sec = 1, .tb = 1 }, 0x5, {0x008000, 480 * 1024} },
787 { .m = { .sec = 1, .tb = 1 }, 0x6, {0x008000, 480 * 1024} },
Martin Rothf3c3d5f2017-04-28 14:56:41 -0600788
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100789 { .m = { .sec = 1, .tb = X }, 0x7, {0x000000, 0} }, /* None */
Martin Rothf3c3d5f2017-04-28 14:56:41 -0600790};
791
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100792static struct wp_range_descriptor gd25q64_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100793 { .m = { .sec = X, .tb = X }, 0, {0, 0} }, /* none */
794 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x7e0000, 128 * 1024} },
795 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x7c0000, 256 * 1024} },
796 { .m = { .sec = 0, .tb = 0 }, 0x3, {0x780000, 512 * 1024} },
797 { .m = { .sec = 0, .tb = 0 }, 0x4, {0x700000, 1024 * 1024} },
798 { .m = { .sec = 0, .tb = 0 }, 0x5, {0x600000, 2048 * 1024} },
799 { .m = { .sec = 0, .tb = 0 }, 0x6, {0x400000, 4096 * 1024} },
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700800
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100801 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x000000, 128 * 1024} },
802 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x000000, 256 * 1024} },
803 { .m = { .sec = 0, .tb = 1 }, 0x3, {0x000000, 512 * 1024} },
804 { .m = { .sec = 0, .tb = 1 }, 0x4, {0x000000, 1024 * 1024} },
805 { .m = { .sec = 0, .tb = 1 }, 0x5, {0x000000, 2048 * 1024} },
806 { .m = { .sec = 0, .tb = 1 }, 0x6, {0x000000, 4096 * 1024} },
807 { .m = { .sec = X, .tb = X }, 0x7, {0x000000, 8192 * 1024} },
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700808
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100809 { .m = { .sec = 1, .tb = 0 }, 0x1, {0x7ff000, 4 * 1024} },
810 { .m = { .sec = 1, .tb = 0 }, 0x2, {0x7fe000, 8 * 1024} },
811 { .m = { .sec = 1, .tb = 0 }, 0x3, {0x7fc000, 16 * 1024} },
812 { .m = { .sec = 1, .tb = 0 }, 0x4, {0x7f8000, 32 * 1024} },
813 { .m = { .sec = 1, .tb = 0 }, 0x5, {0x7f8000, 32 * 1024} },
814 { .m = { .sec = 1, .tb = 0 }, 0x6, {0x7f8000, 32 * 1024} },
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700815
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100816 { .m = { .sec = 1, .tb = 1 }, 0x1, {0x000000, 4 * 1024} },
817 { .m = { .sec = 1, .tb = 1 }, 0x2, {0x000000, 8 * 1024} },
818 { .m = { .sec = 1, .tb = 1 }, 0x3, {0x000000, 16 * 1024} },
819 { .m = { .sec = 1, .tb = 1 }, 0x4, {0x000000, 32 * 1024} },
820 { .m = { .sec = 1, .tb = 1 }, 0x5, {0x000000, 32 * 1024} },
821 { .m = { .sec = 1, .tb = 1 }, 0x6, {0x000000, 32 * 1024} },
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700822};
823
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100824static struct wp_range_descriptor a25l040_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100825 { .m = { .sec = X, .tb = X }, 0x0, {0, 0} }, /* none */
826 { .m = { .sec = X, .tb = X }, 0x1, {0x70000, 64 * 1024} },
827 { .m = { .sec = X, .tb = X }, 0x2, {0x60000, 128 * 1024} },
828 { .m = { .sec = X, .tb = X }, 0x3, {0x40000, 256 * 1024} },
829 { .m = { .sec = X, .tb = X }, 0x4, {0x00000, 512 * 1024} },
830 { .m = { .sec = X, .tb = X }, 0x5, {0x00000, 512 * 1024} },
831 { .m = { .sec = X, .tb = X }, 0x6, {0x00000, 512 * 1024} },
832 { .m = { .sec = X, .tb = X }, 0x7, {0x00000, 512 * 1024} },
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +0800833};
834
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700835static uint8_t do_read_status(const struct flashctx *flash)
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +0530836{
Patrick Georgif3fa2992017-02-02 16:24:44 +0100837 if (flash->chip->read_status)
838 return flash->chip->read_status(flash);
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +0530839 else
840 return spi_read_status_register(flash);
841}
842
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700843static int do_write_status(const struct flashctx *flash, int status)
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +0530844{
Patrick Georgif3fa2992017-02-02 16:24:44 +0100845 if (flash->chip->write_status)
846 return flash->chip->write_status(flash, status);
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +0530847 else
848 return spi_write_status_register(flash, status);
849}
850
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700851/* FIXME: Move to spi25.c if it's a JEDEC standard opcode */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700852static uint8_t w25q_read_status_register_2(const struct flashctx *flash)
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700853{
854 static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { 0x35 };
855 unsigned char readarr[2];
856 int ret;
857
858 /* Read Status Register */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700859 ret = spi_send_command(flash, sizeof(cmd), sizeof(readarr), cmd, readarr);
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700860 if (ret) {
861 /*
862 * FIXME: make this a benign failure for now in case we are
863 * unable to execute the opcode
864 */
865 msg_cdbg("RDSR2 failed!\n");
866 readarr[0] = 0x00;
867 }
868
869 return readarr[0];
870}
871
Karthikeyan Ramasubramanianfb166b72019-06-24 12:38:55 -0600872/* FIXME: Move to spi25.c if it's a JEDEC standard opcode */
Edward O'Callaghandf43e902020-11-13 23:08:26 +1100873static uint8_t mx25l_read_config_register(const struct flashctx *flash)
Karthikeyan Ramasubramanianfb166b72019-06-24 12:38:55 -0600874{
875 static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { 0x15 };
876 unsigned char readarr[2]; /* leave room for dummy byte */
877 int ret;
878
879 ret = spi_send_command(flash, sizeof(cmd), sizeof(readarr), cmd, readarr);
880 if (ret) {
881 msg_cdbg("RDCR failed!\n");
882 readarr[0] = 0x00;
883 }
884
885 return readarr[0];
886}
887
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800888/* Given a flash chip, this function returns its range table. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700889static int w25_range_table(const struct flashctx *flash,
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100890 struct wp_range_descriptor **descrs,
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800891 int *num_entries)
David Hendricksf7924d12010-06-10 21:26:44 -0700892{
Karthikeyan Ramasubramanianfb166b72019-06-24 12:38:55 -0600893 uint8_t cr;
894
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100895 *descrs = 0;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800896 *num_entries = 0;
David Hendricksf7924d12010-06-10 21:26:44 -0700897
Patrick Georgif3fa2992017-02-02 16:24:44 +0100898 switch (flash->chip->manufacture_id) {
David Hendricksd494b0a2010-08-16 16:28:50 -0700899 case WINBOND_NEX_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +0100900 switch(flash->chip->model_id) {
David Hendricksc801adb2010-12-09 16:58:56 -0800901 case WINBOND_NEX_W25X10:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100902 *descrs = w25x10_ranges;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800903 *num_entries = ARRAY_SIZE(w25x10_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800904 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800905 case WINBOND_NEX_W25X20:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100906 *descrs = w25x20_ranges;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800907 *num_entries = ARRAY_SIZE(w25x20_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800908 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800909 case WINBOND_NEX_W25X40:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100910 *descrs = w25x40_ranges;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800911 *num_entries = ARRAY_SIZE(w25x40_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700912 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800913 case WINBOND_NEX_W25X80:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100914 *descrs = w25x80_ranges;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800915 *num_entries = ARRAY_SIZE(w25x80_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800916 break;
Patrick Georgicc04a452017-02-06 12:14:43 +0100917 case WINBOND_NEX_W25Q80_V:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100918 *descrs = w25q80_ranges;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800919 *num_entries = ARRAY_SIZE(w25q80_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700920 break;
Patrick Georgicc04a452017-02-06 12:14:43 +0100921 case WINBOND_NEX_W25Q16_V:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100922 *descrs = w25q16_ranges;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800923 *num_entries = ARRAY_SIZE(w25q16_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700924 break;
Patrick Georgicc04a452017-02-06 12:14:43 +0100925 case WINBOND_NEX_W25Q32_V:
926 case WINBOND_NEX_W25Q32_W:
Edward O'Callaghand80cf712019-05-24 22:06:36 +1000927 case WINBOND_NEX_W25Q32JW:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100928 *descrs = w25q32_ranges;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800929 *num_entries = ARRAY_SIZE(w25q32_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700930 break;
Patrick Georgicc04a452017-02-06 12:14:43 +0100931 case WINBOND_NEX_W25Q64_V:
932 case WINBOND_NEX_W25Q64_W:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100933 *descrs = w25q64_ranges;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800934 *num_entries = ARRAY_SIZE(w25q64_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700935 break;
Edward O'Callaghan517cb822019-11-21 14:08:32 +1100936 case WINBOND_NEX_W25Q128_DTR:
Alan Green77a95de2019-07-01 16:40:39 +1000937 case WINBOND_NEX_W25Q128_V_M:
Patrick Georgicc04a452017-02-06 12:14:43 +0100938 case WINBOND_NEX_W25Q128_V:
939 case WINBOND_NEX_W25Q128_W:
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700940 if (w25q_read_status_register_2(flash) & (1 << 6)) {
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700941 /* CMP == 1 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100942 *descrs = w25rq128_cmp1_ranges;
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700943 *num_entries = ARRAY_SIZE(w25rq128_cmp1_ranges);
944 } else {
945 /* CMP == 0 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100946 *descrs = w25rq128_cmp0_ranges;
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700947 *num_entries = ARRAY_SIZE(w25rq128_cmp0_ranges);
948 }
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530949 break;
Justin TerAvest40083232020-08-17 16:34:46 -0600950 case WINBOND_NEX_W25Q256_V:
Alan Green77a95de2019-07-01 16:40:39 +1000951 case WINBOND_NEX_W25Q256JV_M:
Duncan Laurie1801f7c2019-01-09 18:02:51 -0800952 if (w25q_read_status_register_2(flash) & (1 << 6)) {
953 /* CMP == 1 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100954 *descrs = w25rq256_cmp1_ranges;
Duncan Laurie1801f7c2019-01-09 18:02:51 -0800955 *num_entries = ARRAY_SIZE(w25rq256_cmp1_ranges);
956 } else {
957 /* CMP == 0 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100958 *descrs = w25rq256_cmp0_ranges;
Duncan Laurie1801f7c2019-01-09 18:02:51 -0800959 *num_entries = ARRAY_SIZE(w25rq256_cmp0_ranges);
960 }
961 break;
David Hendricksd494b0a2010-08-16 16:28:50 -0700962 default:
963 msg_cerr("%s() %d: WINBOND flash chip mismatch (0x%04x)"
964 ", aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +0100965 flash->chip->model_id);
David Hendricksd494b0a2010-08-16 16:28:50 -0700966 return -1;
967 }
David Hendricks2c4a76c2010-06-28 14:00:43 -0700968 break;
David Hendricks57566ed2010-08-16 18:24:45 -0700969 case EON_ID_NOPREFIX:
Patrick Georgif3fa2992017-02-02 16:24:44 +0100970 switch (flash->chip->model_id) {
David Hendricksc801adb2010-12-09 16:58:56 -0800971 case EON_EN25F40:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100972 *descrs = en25f40_ranges;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800973 *num_entries = ARRAY_SIZE(en25f40_ranges);
David Hendricks57566ed2010-08-16 18:24:45 -0700974 break;
David Hendrickse185bf22011-05-24 15:34:18 -0700975 case EON_EN25Q40:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100976 *descrs = en25q40_ranges;
David Hendrickse185bf22011-05-24 15:34:18 -0700977 *num_entries = ARRAY_SIZE(en25q40_ranges);
978 break;
979 case EON_EN25Q80:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100980 *descrs = en25q80_ranges;
David Hendrickse185bf22011-05-24 15:34:18 -0700981 *num_entries = ARRAY_SIZE(en25q80_ranges);
982 break;
983 case EON_EN25Q32:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100984 *descrs = en25q32_ranges;
David Hendrickse185bf22011-05-24 15:34:18 -0700985 *num_entries = ARRAY_SIZE(en25q32_ranges);
986 break;
987 case EON_EN25Q64:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100988 *descrs = en25q64_ranges;
David Hendrickse185bf22011-05-24 15:34:18 -0700989 *num_entries = ARRAY_SIZE(en25q64_ranges);
990 break;
991 case EON_EN25Q128:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100992 *descrs = en25q128_ranges;
David Hendrickse185bf22011-05-24 15:34:18 -0700993 *num_entries = ARRAY_SIZE(en25q128_ranges);
994 break;
Tim Chen136fd0a2020-06-30 19:12:50 +0800995 case EON_EN25QH128:
996 if (w25q_read_status_register_2(flash) & (1 << 6)) {
997 /* CMP == 1 */
998 *descrs = w25rq128_cmp1_ranges;
999 *num_entries = ARRAY_SIZE(w25rq128_cmp1_ranges);
1000 } else {
1001 /* CMP == 0 */
1002 *descrs = w25rq128_cmp0_ranges;
1003 *num_entries = ARRAY_SIZE(w25rq128_cmp0_ranges);
1004 }
1005 break;
Marc Jonesb2f90022014-04-29 17:37:23 -06001006 case EON_EN25S64:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001007 *descrs = en25s64_ranges;
Marc Jonesb2f90022014-04-29 17:37:23 -06001008 *num_entries = ARRAY_SIZE(en25s64_ranges);
1009 break;
David Hendricks57566ed2010-08-16 18:24:45 -07001010 default:
1011 msg_cerr("%s():%d: EON flash chip mismatch (0x%04x)"
1012 ", aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01001013 flash->chip->model_id);
David Hendricks57566ed2010-08-16 18:24:45 -07001014 return -1;
1015 }
1016 break;
David Hendricksc801adb2010-12-09 16:58:56 -08001017 case MACRONIX_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +01001018 switch (flash->chip->model_id) {
David Hendricksf8f00c72011-02-01 12:39:46 -08001019 case MACRONIX_MX25L1005:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001020 *descrs = mx25l1005_ranges;
David Hendricksf8f00c72011-02-01 12:39:46 -08001021 *num_entries = ARRAY_SIZE(mx25l1005_ranges);
1022 break;
1023 case MACRONIX_MX25L2005:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001024 *descrs = mx25l2005_ranges;
David Hendricksf8f00c72011-02-01 12:39:46 -08001025 *num_entries = ARRAY_SIZE(mx25l2005_ranges);
1026 break;
1027 case MACRONIX_MX25L4005:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001028 *descrs = mx25l4005_ranges;
David Hendricksf8f00c72011-02-01 12:39:46 -08001029 *num_entries = ARRAY_SIZE(mx25l4005_ranges);
1030 break;
1031 case MACRONIX_MX25L8005:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001032 *descrs = mx25l8005_ranges;
David Hendricksf8f00c72011-02-01 12:39:46 -08001033 *num_entries = ARRAY_SIZE(mx25l8005_ranges);
1034 break;
1035 case MACRONIX_MX25L1605:
1036 /* FIXME: MX25L1605 and MX25L1605D have different write
1037 * protection capabilities, but share IDs */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001038 *descrs = mx25l1605d_ranges;
David Hendricksf8f00c72011-02-01 12:39:46 -08001039 *num_entries = ARRAY_SIZE(mx25l1605d_ranges);
1040 break;
David Hendricksc801adb2010-12-09 16:58:56 -08001041 case MACRONIX_MX25L3205:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001042 *descrs = mx25l3205d_ranges;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001043 *num_entries = ARRAY_SIZE(mx25l3205d_ranges);
David Hendricksac72e362010-08-16 18:20:03 -07001044 break;
Vincent Palatin87e092a2013-02-28 15:46:14 -08001045 case MACRONIX_MX25U3235E:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001046 *descrs = mx25u3235e_ranges;
Vincent Palatin87e092a2013-02-28 15:46:14 -08001047 *num_entries = ARRAY_SIZE(mx25u3235e_ranges);
1048 break;
Jongpil66a96492014-08-14 17:59:06 +09001049 case MACRONIX_MX25U6435E:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001050 *descrs = mx25u6435e_ranges;
Jongpil66a96492014-08-14 17:59:06 +09001051 *num_entries = ARRAY_SIZE(mx25u6435e_ranges);
1052 break;
Alan Greendc0792e2019-07-01 15:01:34 +10001053 case MACRONIX_MX25U12835E:
Karthikeyan Ramasubramanianfb166b72019-06-24 12:38:55 -06001054 cr = mx25l_read_config_register(flash);
1055 if (cr & MX25U12835E_TB) { /* T/B == 1 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001056 *descrs = mx25u12835e_tb1_ranges;
Karthikeyan Ramasubramanianfb166b72019-06-24 12:38:55 -06001057 *num_entries = ARRAY_SIZE(mx25u12835e_tb1_ranges);
1058 } else { /* T/B == 0 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001059 *descrs = mx25u12835e_tb0_ranges;
Karthikeyan Ramasubramanianfb166b72019-06-24 12:38:55 -06001060 *num_entries = ARRAY_SIZE(mx25u12835e_tb0_ranges);
1061 }
Alex Lu831c6092017-11-02 23:19:34 -07001062 break;
David Hendricksac72e362010-08-16 18:20:03 -07001063 default:
1064 msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)"
1065 ", aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01001066 flash->chip->model_id);
David Hendricksac72e362010-08-16 18:20:03 -07001067 return -1;
1068 }
1069 break;
David Hendricksbfa624b2012-07-24 12:47:59 -07001070 case ST_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +01001071 switch(flash->chip->model_id) {
David Hendricksbfa624b2012-07-24 12:47:59 -07001072 case ST_N25Q064__1E:
1073 case ST_N25Q064__3E:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001074 *descrs = n25q064_ranges;
David Hendricksbfa624b2012-07-24 12:47:59 -07001075 *num_entries = ARRAY_SIZE(n25q064_ranges);
1076 break;
1077 default:
1078 msg_cerr("%s() %d: Micron flash chip mismatch"
1079 " (0x%04x), aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01001080 flash->chip->model_id);
David Hendricksbfa624b2012-07-24 12:47:59 -07001081 return -1;
1082 }
1083 break;
Bryan Freed9a0051f2012-05-22 16:06:09 -07001084 case GIGADEVICE_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +01001085 switch(flash->chip->model_id) {
Bryan Freed9a0051f2012-05-22 16:06:09 -07001086 case GIGADEVICE_GD25LQ32:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001087 *descrs = w25q32_ranges;
Bryan Freed9a0051f2012-05-22 16:06:09 -07001088 *num_entries = ARRAY_SIZE(w25q32_ranges);
1089 break;
Martin Rothf3c3d5f2017-04-28 14:56:41 -06001090 case GIGADEVICE_GD25Q40:
1091 if (w25q_read_status_register_2(flash) & (1 << 6)) {
1092 /* CMP == 1 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001093 *descrs = gd25q40_cmp1_ranges;
Martin Rothf3c3d5f2017-04-28 14:56:41 -06001094 *num_entries = ARRAY_SIZE(gd25q40_cmp1_ranges);
1095 } else {
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001096 *descrs = gd25q40_cmp0_ranges;
Martin Rothf3c3d5f2017-04-28 14:56:41 -06001097 *num_entries = ARRAY_SIZE(gd25q40_cmp0_ranges);
1098 }
1099 break;
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -07001100 case GIGADEVICE_GD25Q64:
Marc Jonesb18734f2014-04-03 16:19:47 -06001101 case GIGADEVICE_GD25LQ64:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001102 *descrs = gd25q64_ranges;
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -07001103 *num_entries = ARRAY_SIZE(gd25q64_ranges);
1104 break;
Martin Roth1fd87ed2017-02-27 20:50:50 -07001105 case GIGADEVICE_GD25Q128:
Aaron Durbin6c957d72018-08-20 09:31:01 -06001106 case GIGADEVICE_GD25LQ128CD:
Martin Roth1fd87ed2017-02-27 20:50:50 -07001107 if (w25q_read_status_register_2(flash) & (1 << 6)) {
1108 /* CMP == 1 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001109 *descrs = w25rq128_cmp1_ranges;
Martin Roth1fd87ed2017-02-27 20:50:50 -07001110 *num_entries = ARRAY_SIZE(w25rq128_cmp1_ranges);
1111 } else {
1112 /* CMP == 0 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001113 *descrs = w25rq128_cmp0_ranges;
Martin Roth1fd87ed2017-02-27 20:50:50 -07001114 *num_entries = ARRAY_SIZE(w25rq128_cmp0_ranges);
1115 }
1116 break;
Duncan Laurie0c383552019-03-16 12:35:16 -07001117 case GIGADEVICE_GD25Q256D:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001118 *descrs = w25rq256_cmp0_ranges;
Duncan Laurie0c383552019-03-16 12:35:16 -07001119 *num_entries = ARRAY_SIZE(w25rq256_cmp0_ranges);
1120 break;
Bryan Freed9a0051f2012-05-22 16:06:09 -07001121 default:
1122 msg_cerr("%s() %d: GigaDevice flash chip mismatch"
1123 " (0x%04x), aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01001124 flash->chip->model_id);
Bryan Freed9a0051f2012-05-22 16:06:09 -07001125 return -1;
1126 }
1127 break;
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +08001128 case AMIC_ID_NOPREFIX:
Patrick Georgif3fa2992017-02-02 16:24:44 +01001129 switch(flash->chip->model_id) {
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +08001130 case AMIC_A25L040:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001131 *descrs = a25l040_ranges;
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +08001132 *num_entries = ARRAY_SIZE(a25l040_ranges);
1133 break;
1134 default:
1135 msg_cerr("%s() %d: AMIC flash chip mismatch"
1136 " (0x%04x), aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01001137 flash->chip->model_id);
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +08001138 return -1;
1139 }
1140 break;
Furquan Shaikhb4df8ef2017-01-05 15:05:35 -08001141 case ATMEL_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +01001142 switch(flash->chip->model_id) {
Edward O'Callaghan1fa87e02019-05-03 02:27:24 -04001143 case ATMEL_AT25SF128A:
Furquan Shaikhb4df8ef2017-01-05 15:05:35 -08001144 case ATMEL_AT25SL128A:
1145 if (w25q_read_status_register_2(flash) & (1 << 6)) {
1146 /* CMP == 1 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001147 *descrs = w25rq128_cmp1_ranges;
Furquan Shaikhb4df8ef2017-01-05 15:05:35 -08001148 *num_entries = ARRAY_SIZE(w25rq128_cmp1_ranges);
1149 } else {
1150 /* CMP == 0 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001151 *descrs = w25rq128_cmp0_ranges;
Furquan Shaikhb4df8ef2017-01-05 15:05:35 -08001152 *num_entries = ARRAY_SIZE(w25rq128_cmp0_ranges);
1153 }
1154 break;
1155 default:
1156 msg_cerr("%s() %d: Atmel flash chip mismatch"
1157 " (0x%04x), aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01001158 flash->chip->model_id);
Furquan Shaikhb4df8ef2017-01-05 15:05:35 -08001159 return -1;
1160 }
1161 break;
David Hendricksf7924d12010-06-10 21:26:44 -07001162 default:
David Hendricksd494b0a2010-08-16 16:28:50 -07001163 msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
Patrick Georgif3fa2992017-02-02 16:24:44 +01001164 __func__, flash->chip->manufacture_id);
David Hendricksf7924d12010-06-10 21:26:44 -07001165 return -1;
1166 }
1167
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001168 return 0;
1169}
1170
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001171int w25_range_to_status(const struct flashctx *flash,
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001172 unsigned int start, unsigned int len,
1173 struct w25q_status *status)
1174{
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001175 struct wp_range_descriptor *descrs;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001176 int i, range_found = 0;
1177 int num_entries;
1178
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001179 if (w25_range_table(flash, &descrs, &num_entries))
Edward O'Callaghanbea239e2019-12-04 14:42:54 +11001180 return -1;
1181
David Hendricksf7924d12010-06-10 21:26:44 -07001182 for (i = 0; i < num_entries; i++) {
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001183 struct wp_range *r = &descrs[i].range;
David Hendricksf7924d12010-06-10 21:26:44 -07001184
1185 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
1186 start, len, r->start, r->len);
1187 if ((start == r->start) && (len == r->len)) {
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001188 status->bp0 = descrs[i].bp & 1;
1189 status->bp1 = descrs[i].bp >> 1;
1190 status->bp2 = descrs[i].bp >> 2;
1191 status->tb = descrs[i].m.tb;
1192 status->sec = descrs[i].m.sec;
David Hendricksf7924d12010-06-10 21:26:44 -07001193
1194 range_found = 1;
1195 break;
1196 }
1197 }
1198
1199 if (!range_found) {
Edward O'Callaghan3be63e02020-03-27 14:44:24 +11001200 msg_cerr("%s: matching range not found\n", __func__);
David Hendricksf7924d12010-06-10 21:26:44 -07001201 return -1;
1202 }
Edward O'Callaghanbea239e2019-12-04 14:42:54 +11001203
David Hendricksd494b0a2010-08-16 16:28:50 -07001204 return 0;
1205}
1206
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001207int w25_status_to_range(const struct flashctx *flash,
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001208 const struct w25q_status *status,
1209 unsigned int *start, unsigned int *len)
1210{
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001211 struct wp_range_descriptor *descrs;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001212 int i, status_found = 0;
1213 int num_entries;
1214
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001215 if (w25_range_table(flash, &descrs, &num_entries))
Edward O'Callaghanbea239e2019-12-04 14:42:54 +11001216 return -1;
1217
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001218 for (i = 0; i < num_entries; i++) {
1219 int bp;
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +08001220 int table_bp, table_tb, table_sec;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001221
1222 bp = status->bp0 | (status->bp1 << 1) | (status->bp2 << 2);
1223 msg_cspew("comparing 0x%x 0x%x / 0x%x 0x%x / 0x%x 0x%x\n",
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001224 bp, descrs[i].bp,
1225 status->tb, descrs[i].m.tb,
1226 status->sec, descrs[i].m.sec);
1227 table_bp = descrs[i].bp;
1228 table_tb = descrs[i].m.tb;
1229 table_sec = descrs[i].m.sec;
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +08001230 if ((bp == table_bp || table_bp == X) &&
1231 (status->tb == table_tb || table_tb == X) &&
1232 (status->sec == table_sec || table_sec == X)) {
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001233 *start = descrs[i].range.start;
1234 *len = descrs[i].range.len;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001235
1236 status_found = 1;
1237 break;
1238 }
1239 }
1240
1241 if (!status_found) {
1242 msg_cerr("matching status not found\n");
1243 return -1;
1244 }
Edward O'Callaghanbea239e2019-12-04 14:42:54 +11001245
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001246 return 0;
1247}
1248
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001249/* Given a [start, len], this function calls w25_range_to_status() to convert
1250 * it to flash-chip-specific range bits, then sets into status register.
1251 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001252static int w25_set_range(const struct flashctx *flash,
David Hendricksd494b0a2010-08-16 16:28:50 -07001253 unsigned int start, unsigned int len)
1254{
1255 struct w25q_status status;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001256 int tmp = 0;
1257 int expected = 0;
David Hendricksd494b0a2010-08-16 16:28:50 -07001258
1259 memset(&status, 0, sizeof(status));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301260 tmp = do_read_status(flash);
David Hendricksd494b0a2010-08-16 16:28:50 -07001261 memcpy(&status, &tmp, 1);
1262 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
1263
Edward O'Callaghanbea239e2019-12-04 14:42:54 +11001264 if (w25_range_to_status(flash, start, len, &status))
1265 return -1;
David Hendricksf7924d12010-06-10 21:26:44 -07001266
1267 msg_cdbg("status.busy: %x\n", status.busy);
1268 msg_cdbg("status.wel: %x\n", status.wel);
1269 msg_cdbg("status.bp0: %x\n", status.bp0);
1270 msg_cdbg("status.bp1: %x\n", status.bp1);
1271 msg_cdbg("status.bp2: %x\n", status.bp2);
1272 msg_cdbg("status.tb: %x\n", status.tb);
1273 msg_cdbg("status.sec: %x\n", status.sec);
1274 msg_cdbg("status.srp0: %x\n", status.srp0);
1275
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001276 memcpy(&expected, &status, sizeof(status));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301277 do_write_status(flash, expected);
David Hendricksf7924d12010-06-10 21:26:44 -07001278
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301279 tmp = do_read_status(flash);
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001280 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
Edward O'Callaghan2672fb92019-12-04 14:47:58 +11001281 if ((tmp & MASK_WP_AREA) != (expected & MASK_WP_AREA)) {
David Hendricksc801adb2010-12-09 16:58:56 -08001282 msg_cerr("expected=0x%02x, but actual=0x%02x.\n",
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001283 expected, tmp);
1284 return 1;
1285 }
Edward O'Callaghan2672fb92019-12-04 14:47:58 +11001286
1287 return 0;
David Hendricksf7924d12010-06-10 21:26:44 -07001288}
1289
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001290/* Print out the current status register value with human-readable text. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001291static int w25_wp_status(const struct flashctx *flash)
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001292{
1293 struct w25q_status status;
1294 int tmp;
David Hendricksce8ded32010-10-08 11:23:38 -07001295 unsigned int start, len;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001296 int ret = 0;
1297
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001298 memset(&status, 0, sizeof(status));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301299 tmp = do_read_status(flash);
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001300 memcpy(&status, &tmp, 1);
1301 msg_cinfo("WP: status: 0x%02x\n", tmp);
1302 msg_cinfo("WP: status.srp0: %x\n", status.srp0);
1303 msg_cinfo("WP: write protect is %s.\n",
1304 status.srp0 ? "enabled" : "disabled");
1305
1306 msg_cinfo("WP: write protect range: ");
1307 if (w25_status_to_range(flash, &status, &start, &len)) {
1308 msg_cinfo("(cannot resolve the range)\n");
1309 ret = -1;
1310 } else {
1311 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
1312 }
1313
1314 return ret;
1315}
1316
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001317static int w25q_large_range_to_status(const struct flashctx *flash,
1318 unsigned int start, unsigned int len,
1319 struct w25q_status_large *status)
1320{
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001321 struct wp_range_descriptor *descrs;
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001322 int i, range_found = 0;
1323 int num_entries;
1324
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001325 if (w25_range_table(flash, &descrs, &num_entries))
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001326 return -1;
Edward O'Callaghanbea239e2019-12-04 14:42:54 +11001327
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001328 for (i = 0; i < num_entries; i++) {
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001329 struct wp_range *r = &descrs[i].range;
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001330
1331 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
1332 start, len, r->start, r->len);
1333 if ((start == r->start) && (len == r->len)) {
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001334 status->bp0 = descrs[i].bp & 1;
1335 status->bp1 = descrs[i].bp >> 1;
1336 status->bp2 = descrs[i].bp >> 2;
1337 status->bp3 = descrs[i].bp >> 3;
Karthikeyan Ramasubramanianfb166b72019-06-24 12:38:55 -06001338 /*
1339 * For MX25U12835E chip, Top/Bottom (T/B) bit is not
1340 * part of status register and in that bit position is
1341 * Quad Enable (QE)
1342 */
1343 if (flash->chip->manufacture_id != MACRONIX_ID ||
1344 flash->chip->model_id != MACRONIX_MX25U12835E)
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001345 status->tb = descrs[i].m.tb;
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001346
1347 range_found = 1;
1348 break;
1349 }
1350 }
1351
1352 if (!range_found) {
Edward O'Callaghan3be63e02020-03-27 14:44:24 +11001353 msg_cerr("%s: matching range not found\n", __func__);
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001354 return -1;
1355 }
Edward O'Callaghanbea239e2019-12-04 14:42:54 +11001356
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001357 return 0;
1358}
1359
1360static int w25_large_status_to_range(const struct flashctx *flash,
1361 const struct w25q_status_large *status,
1362 unsigned int *start, unsigned int *len)
1363{
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001364 struct wp_range_descriptor *descrs;
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001365 int i, status_found = 0;
1366 int num_entries;
1367
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001368 if (w25_range_table(flash, &descrs, &num_entries))
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001369 return -1;
Edward O'Callaghanbea239e2019-12-04 14:42:54 +11001370
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001371 for (i = 0; i < num_entries; i++) {
1372 int bp;
1373 int table_bp, table_tb;
1374
1375 bp = status->bp0 | (status->bp1 << 1) | (status->bp2 << 2) |
1376 (status->bp3 << 3);
1377 msg_cspew("comparing 0x%x 0x%x / 0x%x 0x%x\n",
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001378 bp, descrs[i].bp,
1379 status->tb, descrs[i].m.tb);
1380 table_bp = descrs[i].bp;
1381 table_tb = descrs[i].m.tb;
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001382 if ((bp == table_bp || table_bp == X) &&
1383 (status->tb == table_tb || table_tb == X)) {
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001384 *start = descrs[i].range.start;
1385 *len = descrs[i].range.len;
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001386
1387 status_found = 1;
1388 break;
1389 }
1390 }
1391
1392 if (!status_found) {
1393 msg_cerr("matching status not found\n");
1394 return -1;
1395 }
Edward O'Callaghanbea239e2019-12-04 14:42:54 +11001396
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001397 return 0;
1398}
1399
1400/* Given a [start, len], this function calls w25_range_to_status() to convert
1401 * it to flash-chip-specific range bits, then sets into status register.
1402 * Returns 0 if successful, -1 on error, and 1 if reading back was different.
1403 */
1404static int w25q_large_set_range(const struct flashctx *flash,
1405 unsigned int start, unsigned int len)
1406{
1407 struct w25q_status_large status;
1408 int tmp;
1409 int expected = 0;
1410
1411 memset(&status, 0, sizeof(status));
1412 tmp = do_read_status(flash);
1413 memcpy(&status, &tmp, 1);
1414 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
1415
1416 if (w25q_large_range_to_status(flash, start, len, &status))
1417 return -1;
1418
1419 msg_cdbg("status.busy: %x\n", status.busy);
1420 msg_cdbg("status.wel: %x\n", status.wel);
1421 msg_cdbg("status.bp0: %x\n", status.bp0);
1422 msg_cdbg("status.bp1: %x\n", status.bp1);
1423 msg_cdbg("status.bp2: %x\n", status.bp2);
1424 msg_cdbg("status.bp3: %x\n", status.bp3);
1425 msg_cdbg("status.tb: %x\n", status.tb);
1426 msg_cdbg("status.srp0: %x\n", status.srp0);
1427
1428 memcpy(&expected, &status, sizeof(status));
1429 do_write_status(flash, expected);
1430
1431 tmp = do_read_status(flash);
1432 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
Edward O'Callaghan2672fb92019-12-04 14:47:58 +11001433 if ((tmp & MASK_WP_AREA_LARGE) != (expected & MASK_WP_AREA_LARGE)) {
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001434 msg_cerr("expected=0x%02x, but actual=0x%02x.\n",
1435 expected, tmp);
1436 return 1;
1437 }
Edward O'Callaghan2672fb92019-12-04 14:47:58 +11001438
1439 return 0;
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001440}
1441
1442static int w25q_large_wp_status(const struct flashctx *flash)
1443{
1444 struct w25q_status_large sr1;
1445 struct w25q_status_2 sr2;
1446 uint8_t tmp[2];
1447 unsigned int start, len;
1448 int ret = 0;
1449
1450 memset(&sr1, 0, sizeof(sr1));
1451 tmp[0] = do_read_status(flash);
1452 memcpy(&sr1, &tmp[0], 1);
1453
1454 memset(&sr2, 0, sizeof(sr2));
1455 tmp[1] = w25q_read_status_register_2(flash);
1456 memcpy(&sr2, &tmp[1], 1);
1457
1458 msg_cinfo("WP: status: 0x%02x%02x\n", tmp[1], tmp[0]);
1459 msg_cinfo("WP: status.srp0: %x\n", sr1.srp0);
1460 msg_cinfo("WP: status.srp1: %x\n", sr2.srp1);
1461 msg_cinfo("WP: write protect is %s.\n",
1462 (sr1.srp0 || sr2.srp1) ? "enabled" : "disabled");
1463
1464 msg_cinfo("WP: write protect range: ");
1465 if (w25_large_status_to_range(flash, &sr1, &start, &len)) {
1466 msg_cinfo("(cannot resolve the range)\n");
1467 ret = -1;
1468 } else {
1469 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
1470 }
1471
1472 return ret;
1473}
1474
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001475/* Set/clear the SRP0 bit in the status register. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001476static int w25_set_srp0(const struct flashctx *flash, int enable)
David Hendricksf7924d12010-06-10 21:26:44 -07001477{
1478 struct w25q_status status;
1479 int tmp = 0;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001480 int expected = 0;
David Hendricksf7924d12010-06-10 21:26:44 -07001481
1482 memset(&status, 0, sizeof(status));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301483 tmp = do_read_status(flash);
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001484 /* FIXME: this is NOT endian-free copy. */
David Hendricksf7924d12010-06-10 21:26:44 -07001485 memcpy(&status, &tmp, 1);
1486 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
1487
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001488 status.srp0 = enable ? 1 : 0;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001489 memcpy(&expected, &status, sizeof(status));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301490 do_write_status(flash, expected);
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001491
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301492 tmp = do_read_status(flash);
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001493 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
1494 if ((tmp & MASK_WP_AREA) != (expected & MASK_WP_AREA))
1495 return 1;
David Hendricksf7924d12010-06-10 21:26:44 -07001496
1497 return 0;
1498}
1499
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001500static int w25_enable_writeprotect(const struct flashctx *flash,
David Hendricks1c09f802012-10-03 11:03:48 -07001501 enum wp_mode wp_mode)
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001502{
1503 int ret;
1504
Edward O'Callaghanca44e5c2019-12-04 14:23:54 +11001505 if (wp_mode != WP_MODE_HARDWARE) {
David Hendricks1c09f802012-10-03 11:03:48 -07001506 msg_cerr("%s(): unsupported write-protect mode\n", __func__);
1507 return 1;
1508 }
1509
Edward O'Callaghanca44e5c2019-12-04 14:23:54 +11001510 ret = w25_set_srp0(flash, 1);
David Hendricksc801adb2010-12-09 16:58:56 -08001511 if (ret)
1512 msg_cerr("%s(): error=%d.\n", __func__, ret);
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001513 return ret;
1514}
1515
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001516static int w25_disable_writeprotect(const struct flashctx *flash)
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001517{
1518 int ret;
1519
1520 ret = w25_set_srp0(flash, 0);
David Hendricksc801adb2010-12-09 16:58:56 -08001521 if (ret)
1522 msg_cerr("%s(): error=%d.\n", __func__, ret);
Edward O'Callaghanbea239e2019-12-04 14:42:54 +11001523
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001524 return ret;
1525}
1526
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001527static int w25_list_ranges(const struct flashctx *flash)
David Hendricks0f7f5382011-02-11 18:12:31 -08001528{
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001529 struct wp_range_descriptor *descrs;
David Hendricks0f7f5382011-02-11 18:12:31 -08001530 int i, num_entries;
1531
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001532 if (w25_range_table(flash, &descrs, &num_entries))
Edward O'Callaghanbea239e2019-12-04 14:42:54 +11001533 return -1;
1534
David Hendricks0f7f5382011-02-11 18:12:31 -08001535 for (i = 0; i < num_entries; i++) {
1536 msg_cinfo("start: 0x%06x, length: 0x%06x\n",
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001537 descrs[i].range.start,
1538 descrs[i].range.len);
David Hendricks0f7f5382011-02-11 18:12:31 -08001539 }
1540
1541 return 0;
1542}
1543
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001544static int w25q_wp_status(const struct flashctx *flash)
David Hendricks1c09f802012-10-03 11:03:48 -07001545{
1546 struct w25q_status sr1;
1547 struct w25q_status_2 sr2;
David Hendricksf1bd8802012-10-30 11:37:57 -07001548 uint8_t tmp[2];
David Hendricks1c09f802012-10-03 11:03:48 -07001549 unsigned int start, len;
1550 int ret = 0;
1551
1552 memset(&sr1, 0, sizeof(sr1));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301553 tmp[0] = do_read_status(flash);
David Hendricksf1bd8802012-10-30 11:37:57 -07001554 memcpy(&sr1, &tmp[0], 1);
David Hendricks1c09f802012-10-03 11:03:48 -07001555
David Hendricksf1bd8802012-10-30 11:37:57 -07001556 memset(&sr2, 0, sizeof(sr2));
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001557 tmp[1] = w25q_read_status_register_2(flash);
David Hendricksf1bd8802012-10-30 11:37:57 -07001558 memcpy(&sr2, &tmp[1], 1);
1559
1560 msg_cinfo("WP: status: 0x%02x%02x\n", tmp[1], tmp[0]);
David Hendricks1c09f802012-10-03 11:03:48 -07001561 msg_cinfo("WP: status.srp0: %x\n", sr1.srp0);
1562 msg_cinfo("WP: status.srp1: %x\n", sr2.srp1);
1563 msg_cinfo("WP: write protect is %s.\n",
1564 (sr1.srp0 || sr2.srp1) ? "enabled" : "disabled");
1565
1566 msg_cinfo("WP: write protect range: ");
1567 if (w25_status_to_range(flash, &sr1, &start, &len)) {
1568 msg_cinfo("(cannot resolve the range)\n");
1569 ret = -1;
1570 } else {
1571 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
1572 }
1573
1574 return ret;
1575}
1576
1577/*
1578 * W25Q adds an optional byte to the standard WRSR opcode. If /CS is
1579 * de-asserted after the first byte, then it acts like a JEDEC-standard
1580 * WRSR command. if /CS is asserted, then the next data byte is written
1581 * into status register 2.
1582 */
1583#define W25Q_WRSR_OUTSIZE 0x03
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001584static int w25q_write_status_register_WREN(const struct flashctx *flash, uint8_t s1, uint8_t s2)
David Hendricks1c09f802012-10-03 11:03:48 -07001585{
1586 int result;
1587 struct spi_command cmds[] = {
1588 {
1589 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
1590 .writecnt = JEDEC_WREN_OUTSIZE,
1591 .writearr = (const unsigned char[]){ JEDEC_WREN },
1592 .readcnt = 0,
1593 .readarr = NULL,
1594 }, {
1595 .writecnt = W25Q_WRSR_OUTSIZE,
1596 .writearr = (const unsigned char[]){ JEDEC_WRSR, s1, s2 },
1597 .readcnt = 0,
1598 .readarr = NULL,
1599 }, {
1600 .writecnt = 0,
1601 .writearr = NULL,
1602 .readcnt = 0,
1603 .readarr = NULL,
1604 }};
1605
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001606 result = spi_send_multicommand(flash, cmds);
David Hendricks1c09f802012-10-03 11:03:48 -07001607 if (result) {
1608 msg_cerr("%s failed during command execution\n",
1609 __func__);
1610 }
1611
1612 /* WRSR performs a self-timed erase before the changes take effect. */
David Hendricks60824042014-12-11 17:22:06 -08001613 programmer_delay(100 * 1000);
David Hendricks1c09f802012-10-03 11:03:48 -07001614
1615 return result;
1616}
1617
1618/*
1619 * Set/clear the SRP1 bit in status register 2.
1620 * FIXME: make this more generic if other chips use the same SR2 layout
1621 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001622static int w25q_set_srp1(const struct flashctx *flash, int enable)
David Hendricks1c09f802012-10-03 11:03:48 -07001623{
1624 struct w25q_status sr1;
1625 struct w25q_status_2 sr2;
1626 uint8_t tmp, expected;
1627
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301628 tmp = do_read_status(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001629 memcpy(&sr1, &tmp, 1);
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001630 tmp = w25q_read_status_register_2(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001631 memcpy(&sr2, &tmp, 1);
1632
1633 msg_cdbg("%s: old status 2: 0x%02x\n", __func__, tmp);
1634
1635 sr2.srp1 = enable ? 1 : 0;
1636
1637 memcpy(&expected, &sr2, 1);
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001638 w25q_write_status_register_WREN(flash, *((uint8_t *)&sr1), *((uint8_t *)&sr2));
David Hendricks1c09f802012-10-03 11:03:48 -07001639
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001640 tmp = w25q_read_status_register_2(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001641 msg_cdbg("%s: new status 2: 0x%02x\n", __func__, tmp);
1642 if ((tmp & MASK_WP2_AREA) != (expected & MASK_WP2_AREA))
1643 return 1;
1644
1645 return 0;
1646}
1647
1648enum wp_mode get_wp_mode(const char *mode_str)
1649{
1650 enum wp_mode wp_mode = WP_MODE_UNKNOWN;
1651
1652 if (!strcasecmp(mode_str, "hardware"))
1653 wp_mode = WP_MODE_HARDWARE;
1654 else if (!strcasecmp(mode_str, "power_cycle"))
1655 wp_mode = WP_MODE_POWER_CYCLE;
1656 else if (!strcasecmp(mode_str, "permanent"))
1657 wp_mode = WP_MODE_PERMANENT;
1658
1659 return wp_mode;
1660}
1661
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001662static int w25q_disable_writeprotect(const struct flashctx *flash,
David Hendricks1c09f802012-10-03 11:03:48 -07001663 enum wp_mode wp_mode)
1664{
1665 int ret = 1;
David Hendricks1c09f802012-10-03 11:03:48 -07001666 struct w25q_status_2 sr2;
1667 uint8_t tmp;
1668
1669 switch (wp_mode) {
1670 case WP_MODE_HARDWARE:
1671 ret = w25_set_srp0(flash, 0);
1672 break;
1673 case WP_MODE_POWER_CYCLE:
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001674 tmp = w25q_read_status_register_2(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001675 memcpy(&sr2, &tmp, 1);
1676 if (sr2.srp1) {
1677 msg_cerr("%s(): must disconnect power to disable "
1678 "write-protection\n", __func__);
1679 } else {
1680 ret = 0;
1681 }
1682 break;
1683 case WP_MODE_PERMANENT:
1684 msg_cerr("%s(): cannot disable permanent write-protection\n",
1685 __func__);
1686 break;
1687 default:
1688 msg_cerr("%s(): invalid mode specified\n", __func__);
1689 break;
1690 }
1691
1692 if (ret)
1693 msg_cerr("%s(): error=%d.\n", __func__, ret);
Edward O'Callaghanbea239e2019-12-04 14:42:54 +11001694
David Hendricks1c09f802012-10-03 11:03:48 -07001695 return ret;
1696}
1697
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001698static int w25q_disable_writeprotect_default(const struct flashctx *flash)
David Hendricks1c09f802012-10-03 11:03:48 -07001699{
1700 return w25q_disable_writeprotect(flash, WP_MODE_HARDWARE);
1701}
1702
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001703static int w25q_enable_writeprotect(const struct flashctx *flash,
David Hendricks1c09f802012-10-03 11:03:48 -07001704 enum wp_mode wp_mode)
1705{
1706 int ret = 1;
1707 struct w25q_status sr1;
1708 struct w25q_status_2 sr2;
1709 uint8_t tmp;
1710
1711 switch (wp_mode) {
1712 case WP_MODE_HARDWARE:
1713 if (w25q_disable_writeprotect(flash, WP_MODE_POWER_CYCLE)) {
1714 msg_cerr("%s(): cannot disable power cycle WP mode\n",
1715 __func__);
1716 break;
1717 }
1718
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301719 tmp = do_read_status(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001720 memcpy(&sr1, &tmp, 1);
1721 if (sr1.srp0)
1722 ret = 0;
1723 else
1724 ret = w25_set_srp0(flash, 1);
1725
1726 break;
1727 case WP_MODE_POWER_CYCLE:
1728 if (w25q_disable_writeprotect(flash, WP_MODE_HARDWARE)) {
1729 msg_cerr("%s(): cannot disable hardware WP mode\n",
1730 __func__);
1731 break;
1732 }
1733
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001734 tmp = w25q_read_status_register_2(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001735 memcpy(&sr2, &tmp, 1);
1736 if (sr2.srp1)
1737 ret = 0;
1738 else
1739 ret = w25q_set_srp1(flash, 1);
1740
1741 break;
1742 case WP_MODE_PERMANENT:
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301743 tmp = do_read_status(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001744 memcpy(&sr1, &tmp, 1);
1745 if (sr1.srp0 == 0) {
1746 ret = w25_set_srp0(flash, 1);
1747 if (ret) {
David Hendricksf1bd8802012-10-30 11:37:57 -07001748 msg_perr("%s(): cannot enable SRP0 for "
David Hendricks1c09f802012-10-03 11:03:48 -07001749 "permanent WP\n", __func__);
1750 break;
1751 }
1752 }
1753
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001754 tmp = w25q_read_status_register_2(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001755 memcpy(&sr2, &tmp, 1);
1756 if (sr2.srp1 == 0) {
1757 ret = w25q_set_srp1(flash, 1);
1758 if (ret) {
David Hendricksf1bd8802012-10-30 11:37:57 -07001759 msg_perr("%s(): cannot enable SRP1 for "
David Hendricks1c09f802012-10-03 11:03:48 -07001760 "permanent WP\n", __func__);
1761 break;
1762 }
1763 }
1764
1765 break;
David Hendricksf1bd8802012-10-30 11:37:57 -07001766 default:
1767 msg_perr("%s(): invalid mode %d\n", __func__, wp_mode);
1768 break;
David Hendricks1c09f802012-10-03 11:03:48 -07001769 }
1770
1771 if (ret)
1772 msg_cerr("%s(): error=%d.\n", __func__, ret);
1773 return ret;
1774}
1775
1776/* W25P, W25X, and many flash chips from various vendors */
David Hendricksf7924d12010-06-10 21:26:44 -07001777struct wp wp_w25 = {
David Hendricks0f7f5382011-02-11 18:12:31 -08001778 .list_ranges = w25_list_ranges,
David Hendricksf7924d12010-06-10 21:26:44 -07001779 .set_range = w25_set_range,
1780 .enable = w25_enable_writeprotect,
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001781 .disable = w25_disable_writeprotect,
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001782 .wp_status = w25_wp_status,
David Hendricks1c09f802012-10-03 11:03:48 -07001783
1784};
1785
1786/* W25Q series has features such as a second status register and SFDP */
1787struct wp wp_w25q = {
1788 .list_ranges = w25_list_ranges,
1789 .set_range = w25_set_range,
1790 .enable = w25q_enable_writeprotect,
1791 /*
1792 * By default, disable hardware write-protection. We may change
1793 * this later if we want to add fine-grained write-protect disable
1794 * as a command-line option.
1795 */
1796 .disable = w25q_disable_writeprotect_default,
1797 .wp_status = w25q_wp_status,
David Hendricksf7924d12010-06-10 21:26:44 -07001798};
David Hendrickse0512a72014-07-15 20:30:47 -07001799
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001800/* W25Q large series has 4 block-protect bits */
1801struct wp wp_w25q_large = {
1802 .list_ranges = w25_list_ranges,
1803 .set_range = w25q_large_set_range,
1804 .enable = w25q_enable_writeprotect,
1805 /*
1806 * By default, disable hardware write-protection. We may change
1807 * this later if we want to add fine-grained write-protect disable
1808 * as a command-line option.
1809 */
1810 .disable = w25q_disable_writeprotect_default,
1811 .wp_status = w25q_large_wp_status,
1812};
1813
Edward O'Callaghan3b996502020-04-12 20:46:51 +10001814static struct wp_range_descriptor gd25q32_cmp0_ranges[] = {
David Hendricksaf3944a2014-07-28 18:37:40 -07001815 /* none, bp4 and bp3 => don't care */
David Hendricks148a4bf2015-03-13 21:02:42 -07001816 { { }, 0x00, {0, 0} },
1817 { { }, 0x08, {0, 0} },
1818 { { }, 0x10, {0, 0} },
1819 { { }, 0x18, {0, 0} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001820
David Hendricks148a4bf2015-03-13 21:02:42 -07001821 { { }, 0x01, {0x3f0000, 64 * 1024} },
1822 { { }, 0x02, {0x3e0000, 128 * 1024} },
1823 { { }, 0x03, {0x3c0000, 256 * 1024} },
1824 { { }, 0x04, {0x380000, 512 * 1024} },
1825 { { }, 0x05, {0x300000, 1024 * 1024} },
1826 { { }, 0x06, {0x200000, 2048 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001827
David Hendricks148a4bf2015-03-13 21:02:42 -07001828 { { }, 0x09, {0x000000, 64 * 1024} },
1829 { { }, 0x0a, {0x000000, 128 * 1024} },
1830 { { }, 0x0b, {0x000000, 256 * 1024} },
1831 { { }, 0x0c, {0x000000, 512 * 1024} },
1832 { { }, 0x0d, {0x000000, 1024 * 1024} },
1833 { { }, 0x0e, {0x000000, 2048 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001834
1835 /* all, bp4 and bp3 => don't care */
David Hendricks148a4bf2015-03-13 21:02:42 -07001836 { { }, 0x07, {0x000000, 4096 * 1024} },
1837 { { }, 0x0f, {0x000000, 4096 * 1024} },
1838 { { }, 0x17, {0x000000, 4096 * 1024} },
1839 { { }, 0x1f, {0x000000, 4096 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001840
David Hendricks148a4bf2015-03-13 21:02:42 -07001841 { { }, 0x11, {0x3ff000, 4 * 1024} },
1842 { { }, 0x12, {0x3fe000, 8 * 1024} },
1843 { { }, 0x13, {0x3fc000, 16 * 1024} },
1844 { { }, 0x14, {0x3f8000, 32 * 1024} }, /* bp0 => don't care */
1845 { { }, 0x15, {0x3f8000, 32 * 1024} }, /* bp0 => don't care */
1846 { { }, 0x16, {0x3f8000, 32 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001847
David Hendricks148a4bf2015-03-13 21:02:42 -07001848 { { }, 0x19, {0x000000, 4 * 1024} },
1849 { { }, 0x1a, {0x000000, 8 * 1024} },
1850 { { }, 0x1b, {0x000000, 16 * 1024} },
1851 { { }, 0x1c, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1852 { { }, 0x1d, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1853 { { }, 0x1e, {0x000000, 32 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001854};
1855
Edward O'Callaghan3b996502020-04-12 20:46:51 +10001856static struct wp_range_descriptor gd25q32_cmp1_ranges[] = {
Martin Roth563a1fe2017-04-18 14:26:27 -06001857 /* All, bp4 and bp3 => don't care */
1858 { { }, 0x00, {0x000000, 4096 * 1024} }, /* All */
1859 { { }, 0x08, {0x000000, 4096 * 1024} },
1860 { { }, 0x10, {0x000000, 4096 * 1024} },
1861 { { }, 0x18, {0x000000, 4096 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001862
David Hendricks148a4bf2015-03-13 21:02:42 -07001863 { { }, 0x01, {0x000000, 4032 * 1024} },
1864 { { }, 0x02, {0x000000, 3968 * 1024} },
1865 { { }, 0x03, {0x000000, 3840 * 1024} },
1866 { { }, 0x04, {0x000000, 3584 * 1024} },
1867 { { }, 0x05, {0x000000, 3 * 1024 * 1024} },
1868 { { }, 0x06, {0x000000, 2 * 1024 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001869
David Hendricks148a4bf2015-03-13 21:02:42 -07001870 { { }, 0x09, {0x010000, 4032 * 1024} },
1871 { { }, 0x0a, {0x020000, 3968 * 1024} },
1872 { { }, 0x0b, {0x040000, 3840 * 1024} },
1873 { { }, 0x0c, {0x080000, 3584 * 1024} },
1874 { { }, 0x0d, {0x100000, 3 * 1024 * 1024} },
1875 { { }, 0x0e, {0x200000, 2 * 1024 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001876
Martin Roth563a1fe2017-04-18 14:26:27 -06001877 /* None, bp4 and bp3 => don't care */
1878 { { }, 0x07, {0, 0} }, /* None */
1879 { { }, 0x0f, {0, 0} },
1880 { { }, 0x17, {0, 0} },
1881 { { }, 0x1f, {0, 0} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001882
David Hendricks148a4bf2015-03-13 21:02:42 -07001883 { { }, 0x11, {0x000000, 4092 * 1024} },
1884 { { }, 0x12, {0x000000, 4088 * 1024} },
1885 { { }, 0x13, {0x000000, 4080 * 1024} },
1886 { { }, 0x14, {0x000000, 4064 * 1024} }, /* bp0 => don't care */
1887 { { }, 0x15, {0x000000, 4064 * 1024} }, /* bp0 => don't care */
1888 { { }, 0x16, {0x000000, 4064 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001889
David Hendricks148a4bf2015-03-13 21:02:42 -07001890 { { }, 0x19, {0x001000, 4092 * 1024} },
1891 { { }, 0x1a, {0x002000, 4088 * 1024} },
1892 { { }, 0x1b, {0x040000, 4080 * 1024} },
1893 { { }, 0x1c, {0x080000, 4064 * 1024} }, /* bp0 => don't care */
1894 { { }, 0x1d, {0x080000, 4064 * 1024} }, /* bp0 => don't care */
1895 { { }, 0x1e, {0x080000, 4064 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001896};
1897
Edward O'Callaghana3edcb22019-12-05 14:30:50 +11001898static struct wp_context gd25q32_wp = {
David Hendricksaf3944a2014-07-28 18:37:40 -07001899 /* TODO: map second status register */
1900 .sr1 = { .bp0_pos = 2, .bp_bits = 5, .srp_pos = 7 },
1901};
1902
Edward O'Callaghan3b996502020-04-12 20:46:51 +10001903static struct wp_range_descriptor gd25q128_cmp0_ranges[] = {
David Hendricks1e9d7ca2016-03-14 15:50:34 -07001904 /* none, bp4 and bp3 => don't care, others = 0 */
1905 { { .tb = 0 }, 0x00, {0, 0} },
1906 { { .tb = 0 }, 0x08, {0, 0} },
1907 { { .tb = 0 }, 0x10, {0, 0} },
1908 { { .tb = 0 }, 0x18, {0, 0} },
1909
1910 { { .tb = 0 }, 0x01, {0xfc0000, 256 * 1024} },
1911 { { .tb = 0 }, 0x02, {0xf80000, 512 * 1024} },
1912 { { .tb = 0 }, 0x03, {0xf00000, 1024 * 1024} },
1913 { { .tb = 0 }, 0x04, {0xe00000, 2048 * 1024} },
1914 { { .tb = 0 }, 0x05, {0xc00000, 4096 * 1024} },
1915 { { .tb = 0 }, 0x06, {0x800000, 8192 * 1024} },
1916
1917 { { .tb = 0 }, 0x09, {0x000000, 256 * 1024} },
1918 { { .tb = 0 }, 0x0a, {0x000000, 512 * 1024} },
1919 { { .tb = 0 }, 0x0b, {0x000000, 1024 * 1024} },
1920 { { .tb = 0 }, 0x0c, {0x000000, 2048 * 1024} },
1921 { { .tb = 0 }, 0x0d, {0x000000, 4096 * 1024} },
1922 { { .tb = 0 }, 0x0e, {0x000000, 8192 * 1024} },
1923
1924 /* all, bp4 and bp3 => don't care, others = 1 */
1925 { { .tb = 0 }, 0x07, {0x000000, 16384 * 1024} },
1926 { { .tb = 0 }, 0x0f, {0x000000, 16384 * 1024} },
1927 { { .tb = 0 }, 0x17, {0x000000, 16384 * 1024} },
1928 { { .tb = 0 }, 0x1f, {0x000000, 16384 * 1024} },
1929
1930 { { .tb = 0 }, 0x11, {0xfff000, 4 * 1024} },
1931 { { .tb = 0 }, 0x12, {0xffe000, 8 * 1024} },
1932 { { .tb = 0 }, 0x13, {0xffc000, 16 * 1024} },
1933 { { .tb = 0 }, 0x14, {0xff8000, 32 * 1024} }, /* bp0 => don't care */
1934 { { .tb = 0 }, 0x15, {0xff8000, 32 * 1024} }, /* bp0 => don't care */
1935
1936 { { .tb = 0 }, 0x19, {0x000000, 4 * 1024} },
1937 { { .tb = 0 }, 0x1a, {0x000000, 8 * 1024} },
1938 { { .tb = 0 }, 0x1b, {0x000000, 16 * 1024} },
1939 { { .tb = 0 }, 0x1c, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1940 { { .tb = 0 }, 0x1d, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1941 { { .tb = 0 }, 0x1e, {0x000000, 32 * 1024} },
1942};
1943
Edward O'Callaghan3b996502020-04-12 20:46:51 +10001944static struct wp_range_descriptor gd25q128_cmp1_ranges[] = {
David Hendricks1e9d7ca2016-03-14 15:50:34 -07001945 /* none, bp4 and bp3 => don't care, others = 0 */
1946 { { .tb = 1 }, 0x00, {0x000000, 16384 * 1024} },
1947 { { .tb = 1 }, 0x08, {0x000000, 16384 * 1024} },
1948 { { .tb = 1 }, 0x10, {0x000000, 16384 * 1024} },
1949 { { .tb = 1 }, 0x18, {0x000000, 16384 * 1024} },
1950
1951 { { .tb = 1 }, 0x01, {0x000000, 16128 * 1024} },
1952 { { .tb = 1 }, 0x02, {0x000000, 15872 * 1024} },
1953 { { .tb = 1 }, 0x03, {0x000000, 15360 * 1024} },
1954 { { .tb = 1 }, 0x04, {0x000000, 14336 * 1024} },
1955 { { .tb = 1 }, 0x05, {0x000000, 12288 * 1024} },
1956 { { .tb = 1 }, 0x06, {0x000000, 8192 * 1024} },
1957
1958 { { .tb = 1 }, 0x09, {0x000000, 16128 * 1024} },
1959 { { .tb = 1 }, 0x0a, {0x000000, 15872 * 1024} },
1960 { { .tb = 1 }, 0x0b, {0x000000, 15360 * 1024} },
1961 { { .tb = 1 }, 0x0c, {0x000000, 14336 * 1024} },
1962 { { .tb = 1 }, 0x0d, {0x000000, 12288 * 1024} },
1963 { { .tb = 1 }, 0x0e, {0x000000, 8192 * 1024} },
1964
1965 /* none, bp4 and bp3 => don't care, others = 1 */
1966 { { .tb = 1 }, 0x07, {0x000000, 16384 * 1024} },
1967 { { .tb = 1 }, 0x08, {0x000000, 16384 * 1024} },
1968 { { .tb = 1 }, 0x0f, {0x000000, 16384 * 1024} },
1969 { { .tb = 1 }, 0x17, {0x000000, 16384 * 1024} },
1970 { { .tb = 1 }, 0x1f, {0x000000, 16384 * 1024} },
1971
1972 { { .tb = 1 }, 0x11, {0x000000, 16380 * 1024} },
1973 { { .tb = 1 }, 0x12, {0x000000, 16376 * 1024} },
1974 { { .tb = 1 }, 0x13, {0x000000, 16368 * 1024} },
1975 { { .tb = 1 }, 0x14, {0x000000, 16352 * 1024} }, /* bp0 => don't care */
1976 { { .tb = 1 }, 0x15, {0x000000, 16352 * 1024} }, /* bp0 => don't care */
1977
1978 { { .tb = 1 }, 0x19, {0x001000, 16380 * 1024} },
1979 { { .tb = 1 }, 0x1a, {0x002000, 16376 * 1024} },
1980 { { .tb = 1 }, 0x1b, {0x004000, 16368 * 1024} },
1981 { { .tb = 1 }, 0x1c, {0x008000, 16352 * 1024} }, /* bp0 => don't care */
1982 { { .tb = 1 }, 0x1d, {0x008000, 16352 * 1024} }, /* bp0 => don't care */
1983 { { .tb = 1 }, 0x1e, {0x008000, 16352 * 1024} },
1984};
1985
Edward O'Callaghana3edcb22019-12-05 14:30:50 +11001986static struct wp_context gd25q128_wp = {
David Hendricks1e9d7ca2016-03-14 15:50:34 -07001987 /* TODO: map second and third status registers */
1988 .sr1 = { .bp0_pos = 2, .bp_bits = 5, .srp_pos = 7 },
1989};
1990
David Hendricks83541d32014-07-15 20:58:21 -07001991/* FIXME: MX25L6406 has same ID as MX25L6405D */
Edward O'Callaghan3b996502020-04-12 20:46:51 +10001992static struct wp_range_descriptor mx25l6406e_ranges[] = {
David Hendricks148a4bf2015-03-13 21:02:42 -07001993 { { }, 0, {0, 0} }, /* none */
1994 { { }, 0x1, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */
1995 { { }, 0x2, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */
1996 { { }, 0x3, {0x7a0000, 64 * 8 * 1024} }, /* blocks 120-127 */
1997 { { }, 0x4, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */
1998 { { }, 0x5, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */
1999 { { }, 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
David Hendricks83541d32014-07-15 20:58:21 -07002000
David Hendricks148a4bf2015-03-13 21:02:42 -07002001 { { }, 0x7, {0x000000, 64 * 128 * 1024} }, /* all */
2002 { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* all */
2003 { { }, 0x9, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
2004 { { }, 0xa, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */
2005 { { }, 0xb, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */
2006 { { }, 0xc, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */
2007 { { }, 0xd, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */
2008 { { }, 0xe, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */
2009 { { }, 0xf, {0x000000, 64 * 128 * 1024} }, /* all */
David Hendricks83541d32014-07-15 20:58:21 -07002010};
2011
Edward O'Callaghana3edcb22019-12-05 14:30:50 +11002012static struct wp_context mx25l6406e_wp = {
David Hendricks83541d32014-07-15 20:58:21 -07002013 .sr1 = { .bp0_pos = 2, .bp_bits = 4, .srp_pos = 7 },
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11002014 .descrs = &mx25l6406e_ranges[0],
David Hendricks83541d32014-07-15 20:58:21 -07002015};
David Hendrickse0512a72014-07-15 20:30:47 -07002016
Edward O'Callaghan3b996502020-04-12 20:46:51 +10002017static struct wp_range_descriptor mx25l6495f_tb0_ranges[] = {
David Hendricks148a4bf2015-03-13 21:02:42 -07002018 { { }, 0, {0, 0} }, /* none */
2019 { { }, 0x1, {0x7f0000, 64 * 1 * 1024} }, /* block 127 */
2020 { { }, 0x2, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */
2021 { { }, 0x3, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */
David Hendricksc3496092014-11-13 17:20:55 -08002022
David Hendricks148a4bf2015-03-13 21:02:42 -07002023 { { }, 0x4, {0x780000, 64 * 8 * 1024} }, /* blocks 120-127 */
2024 { { }, 0x5, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */
2025 { { }, 0x6, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */
2026 { { }, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
2027 { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* all */
2028 { { }, 0x9, {0x000000, 64 * 128 * 1024} }, /* all */
2029 { { }, 0xa, {0x000000, 64 * 128 * 1024} }, /* all */
2030 { { }, 0xb, {0x000000, 64 * 128 * 1024} }, /* all */
2031 { { }, 0xc, {0x000000, 64 * 128 * 1024} }, /* all */
2032 { { }, 0xd, {0x000000, 64 * 128 * 1024} }, /* all */
2033 { { }, 0xe, {0x000000, 64 * 128 * 1024} }, /* all */
2034 { { }, 0xf, {0x000000, 64 * 128 * 1024} }, /* all */
David Hendricksc3496092014-11-13 17:20:55 -08002035};
2036
Edward O'Callaghan3b996502020-04-12 20:46:51 +10002037static struct wp_range_descriptor mx25l6495f_tb1_ranges[] = {
David Hendricks148a4bf2015-03-13 21:02:42 -07002038 { { }, 0, {0, 0} }, /* none */
2039 { { }, 0x1, {0x000000, 64 * 1 * 1024} }, /* block 0 */
2040 { { }, 0x2, {0x000000, 64 * 2 * 1024} }, /* blocks 0-1 */
2041 { { }, 0x3, {0x000000, 64 * 4 * 1024} }, /* blocks 0-3 */
2042 { { }, 0x4, {0x000000, 64 * 8 * 1024} }, /* blocks 0-7 */
2043 { { }, 0x5, {0x000000, 64 * 16 * 1024} }, /* blocks 0-15 */
2044 { { }, 0x6, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
2045 { { }, 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
2046 { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* all */
2047 { { }, 0x9, {0x000000, 64 * 128 * 1024} }, /* all */
2048 { { }, 0xa, {0x000000, 64 * 128 * 1024} }, /* all */
2049 { { }, 0xb, {0x000000, 64 * 128 * 1024} }, /* all */
2050 { { }, 0xc, {0x000000, 64 * 128 * 1024} }, /* all */
2051 { { }, 0xd, {0x000000, 64 * 128 * 1024} }, /* all */
2052 { { }, 0xe, {0x000000, 64 * 128 * 1024} }, /* all */
2053 { { }, 0xf, {0x000000, 64 * 128 * 1024} }, /* all */
David Hendricksc3496092014-11-13 17:20:55 -08002054};
2055
Edward O'Callaghana3edcb22019-12-05 14:30:50 +11002056static struct wp_context mx25l6495f_wp = {
David Hendricksc3496092014-11-13 17:20:55 -08002057 .sr1 = { .bp0_pos = 2, .bp_bits = 4, .srp_pos = 7 },
2058};
2059
Edward O'Callaghan3b996502020-04-12 20:46:51 +10002060static struct wp_range_descriptor mx25l25635f_tb0_ranges[] = {
Vic Yang848bfd12018-03-23 10:24:07 -07002061 { { }, 0, {0, 0} }, /* none */
2062 { { }, 0x1, {0x1ff0000, 64 * 1 * 1024} }, /* block 511 */
2063 { { }, 0x2, {0x1fe0000, 64 * 2 * 1024} }, /* blocks 510-511 */
2064 { { }, 0x3, {0x1fc0000, 64 * 4 * 1024} }, /* blocks 508-511 */
2065 { { }, 0x4, {0x1f80000, 64 * 8 * 1024} }, /* blocks 504-511 */
2066 { { }, 0x5, {0x1f00000, 64 * 16 * 1024} }, /* blocks 496-511 */
2067 { { }, 0x6, {0x1e00000, 64 * 32 * 1024} }, /* blocks 480-511 */
2068 { { }, 0x7, {0x1c00000, 64 * 64 * 1024} }, /* blocks 448-511 */
2069 { { }, 0x8, {0x1800000, 64 * 128 * 1024} }, /* blocks 384-511 */
2070 { { }, 0x9, {0x1000000, 64 * 256 * 1024} }, /* blocks 256-511 */
2071 { { }, 0xa, {0x0000000, 64 * 512 * 1024} }, /* all */
2072 { { }, 0xb, {0x0000000, 64 * 512 * 1024} }, /* all */
2073 { { }, 0xc, {0x0000000, 64 * 512 * 1024} }, /* all */
2074 { { }, 0xd, {0x0000000, 64 * 512 * 1024} }, /* all */
2075 { { }, 0xe, {0x0000000, 64 * 512 * 1024} }, /* all */
2076 { { }, 0xf, {0x0000000, 64 * 512 * 1024} }, /* all */
2077};
2078
Edward O'Callaghan3b996502020-04-12 20:46:51 +10002079static struct wp_range_descriptor mx25l25635f_tb1_ranges[] = {
Vic Yang848bfd12018-03-23 10:24:07 -07002080 { { }, 0, {0, 0} }, /* none */
2081 { { }, 0x1, {0x000000, 64 * 1 * 1024} }, /* block 0 */
2082 { { }, 0x2, {0x000000, 64 * 2 * 1024} }, /* blocks 0-1 */
2083 { { }, 0x3, {0x000000, 64 * 4 * 1024} }, /* blocks 0-3 */
2084 { { }, 0x4, {0x000000, 64 * 8 * 1024} }, /* blocks 0-7 */
2085 { { }, 0x5, {0x000000, 64 * 16 * 1024} }, /* blocks 0-15 */
2086 { { }, 0x6, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
2087 { { }, 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
2088 { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */
2089 { { }, 0x9, {0x000000, 64 * 256 * 1024} }, /* blocks 0-255 */
2090 { { }, 0xa, {0x000000, 64 * 512 * 1024} }, /* all */
2091 { { }, 0xb, {0x000000, 64 * 512 * 1024} }, /* all */
2092 { { }, 0xc, {0x000000, 64 * 512 * 1024} }, /* all */
2093 { { }, 0xd, {0x000000, 64 * 512 * 1024} }, /* all */
2094 { { }, 0xe, {0x000000, 64 * 512 * 1024} }, /* all */
2095 { { }, 0xf, {0x000000, 64 * 512 * 1024} }, /* all */
2096};
2097
Edward O'Callaghana3edcb22019-12-05 14:30:50 +11002098static struct wp_context mx25l25635f_wp = {
Vic Yang848bfd12018-03-23 10:24:07 -07002099 .sr1 = { .bp0_pos = 2, .bp_bits = 4, .srp_pos = 7 },
2100};
2101
Edward O'Callaghan3b996502020-04-12 20:46:51 +10002102static struct wp_range_descriptor s25fs128s_ranges[] = {
David Hendricks148a4bf2015-03-13 21:02:42 -07002103 { { .tb = 1 }, 0, {0, 0} }, /* none */
2104 { { .tb = 1 }, 0x1, {0x000000, 256 * 1024} }, /* lower 64th */
2105 { { .tb = 1 }, 0x2, {0x000000, 512 * 1024} }, /* lower 32nd */
2106 { { .tb = 1 }, 0x3, {0x000000, 1024 * 1024} }, /* lower 16th */
2107 { { .tb = 1 }, 0x4, {0x000000, 2048 * 1024} }, /* lower 8th */
2108 { { .tb = 1 }, 0x5, {0x000000, 4096 * 1024} }, /* lower 4th */
2109 { { .tb = 1 }, 0x6, {0x000000, 8192 * 1024} }, /* lower half */
2110 { { .tb = 1 }, 0x7, {0x000000, 16384 * 1024} }, /* all */
David Hendricksa9884852014-12-11 15:31:12 -08002111
David Hendricks148a4bf2015-03-13 21:02:42 -07002112 { { .tb = 0 }, 0, {0, 0} }, /* none */
2113 { { .tb = 0 }, 0x1, {0xfc0000, 256 * 1024} }, /* upper 64th */
2114 { { .tb = 0 }, 0x2, {0xf80000, 512 * 1024} }, /* upper 32nd */
2115 { { .tb = 0 }, 0x3, {0xf00000, 1024 * 1024} }, /* upper 16th */
2116 { { .tb = 0 }, 0x4, {0xe00000, 2048 * 1024} }, /* upper 8th */
2117 { { .tb = 0 }, 0x5, {0xc00000, 4096 * 1024} }, /* upper 4th */
2118 { { .tb = 0 }, 0x6, {0x800000, 8192 * 1024} }, /* upper half */
2119 { { .tb = 0 }, 0x7, {0x000000, 16384 * 1024} }, /* all */
David Hendricksa9884852014-12-11 15:31:12 -08002120};
2121
Edward O'Callaghana3edcb22019-12-05 14:30:50 +11002122static struct wp_context s25fs128s_wp = {
David Hendricksa9884852014-12-11 15:31:12 -08002123 .sr1 = { .bp0_pos = 2, .bp_bits = 3, .srp_pos = 7 },
David Hendricks148a4bf2015-03-13 21:02:42 -07002124 .get_modifier_bits = s25f_get_modifier_bits,
2125 .set_modifier_bits = s25f_set_modifier_bits,
David Hendricksa9884852014-12-11 15:31:12 -08002126};
2127
David Hendricksc694bb82015-02-25 14:52:17 -08002128
Edward O'Callaghan3b996502020-04-12 20:46:51 +10002129static struct wp_range_descriptor s25fl256s_ranges[] = {
David Hendricks148a4bf2015-03-13 21:02:42 -07002130 { { .tb = 1 }, 0, {0, 0} }, /* none */
2131 { { .tb = 1 }, 0x1, {0x000000, 512 * 1024} }, /* lower 64th */
2132 { { .tb = 1 }, 0x2, {0x000000, 1024 * 1024} }, /* lower 32nd */
2133 { { .tb = 1 }, 0x3, {0x000000, 2048 * 1024} }, /* lower 16th */
2134 { { .tb = 1 }, 0x4, {0x000000, 4096 * 1024} }, /* lower 8th */
2135 { { .tb = 1 }, 0x5, {0x000000, 8192 * 1024} }, /* lower 4th */
2136 { { .tb = 1 }, 0x6, {0x000000, 16384 * 1024} }, /* lower half */
2137 { { .tb = 1 }, 0x7, {0x000000, 32768 * 1024} }, /* all */
2138
2139 { { .tb = 0 }, 0, {0, 0} }, /* none */
2140 { { .tb = 0 }, 0x1, {0x1f80000, 512 * 1024} }, /* upper 64th */
2141 { { .tb = 0 }, 0x2, {0x1f00000, 1024 * 1024} }, /* upper 32nd */
2142 { { .tb = 0 }, 0x3, {0x1e00000, 2048 * 1024} }, /* upper 16th */
2143 { { .tb = 0 }, 0x4, {0x1c00000, 4096 * 1024} }, /* upper 8th */
2144 { { .tb = 0 }, 0x5, {0x1800000, 8192 * 1024} }, /* upper 4th */
2145 { { .tb = 0 }, 0x6, {0x1000000, 16384 * 1024} }, /* upper half */
2146 { { .tb = 0 }, 0x7, {0x000000, 32768 * 1024} }, /* all */
David Hendricksc694bb82015-02-25 14:52:17 -08002147};
2148
Edward O'Callaghana3edcb22019-12-05 14:30:50 +11002149static struct wp_context s25fl256s_wp = {
David Hendricksc694bb82015-02-25 14:52:17 -08002150 .sr1 = { .bp0_pos = 2, .bp_bits = 3, .srp_pos = 7 },
David Hendricks148a4bf2015-03-13 21:02:42 -07002151 .get_modifier_bits = s25f_get_modifier_bits,
2152 .set_modifier_bits = s25f_set_modifier_bits,
David Hendricksc694bb82015-02-25 14:52:17 -08002153};
2154
David Hendrickse0512a72014-07-15 20:30:47 -07002155/* Given a flash chip, this function returns its writeprotect info. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002156static int generic_range_table(const struct flashctx *flash,
Edward O'Callaghana3edcb22019-12-05 14:30:50 +11002157 struct wp_context **wp,
David Hendrickse0512a72014-07-15 20:30:47 -07002158 int *num_entries)
2159{
2160 *wp = NULL;
2161 *num_entries = 0;
2162
Patrick Georgif3fa2992017-02-02 16:24:44 +01002163 switch (flash->chip->manufacture_id) {
David Hendricksaf3944a2014-07-28 18:37:40 -07002164 case GIGADEVICE_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +01002165 switch(flash->chip->model_id) {
David Hendricks1e9d7ca2016-03-14 15:50:34 -07002166
Martin Roth563a1fe2017-04-18 14:26:27 -06002167 case GIGADEVICE_GD25LQ32:
David Hendricksaf3944a2014-07-28 18:37:40 -07002168 case GIGADEVICE_GD25Q32: {
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002169 uint8_t sr1 = w25q_read_status_register_2(flash);
David Hendricksaf3944a2014-07-28 18:37:40 -07002170 *wp = &gd25q32_wp;
David Hendricks1e9d7ca2016-03-14 15:50:34 -07002171
David Hendricksaf3944a2014-07-28 18:37:40 -07002172 if (!(sr1 & (1 << 6))) { /* CMP == 0 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11002173 (*wp)->descrs = &gd25q32_cmp0_ranges[0];
David Hendricksaf3944a2014-07-28 18:37:40 -07002174 *num_entries = ARRAY_SIZE(gd25q32_cmp0_ranges);
2175 } else { /* CMP == 1 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11002176 (*wp)->descrs = &gd25q32_cmp1_ranges[0];
David Hendricksaf3944a2014-07-28 18:37:40 -07002177 *num_entries = ARRAY_SIZE(gd25q32_cmp1_ranges);
2178 }
2179
2180 break;
David Hendricks1e9d7ca2016-03-14 15:50:34 -07002181 }
Furquan Shaikh62cd8102016-07-17 23:04:06 -07002182 case GIGADEVICE_GD25Q128:
Aaron Durbin6c957d72018-08-20 09:31:01 -06002183 case GIGADEVICE_GD25LQ128CD: {
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002184 uint8_t sr1 = w25q_read_status_register_2(flash);
David Hendricks1e9d7ca2016-03-14 15:50:34 -07002185 *wp = &gd25q128_wp;
2186
2187 if (!(sr1 & (1 << 6))) { /* CMP == 0 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11002188 (*wp)->descrs = &gd25q128_cmp0_ranges[0];
David Hendricks1e9d7ca2016-03-14 15:50:34 -07002189 *num_entries = ARRAY_SIZE(gd25q128_cmp0_ranges);
2190 } else { /* CMP == 1 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11002191 (*wp)->descrs = &gd25q128_cmp1_ranges[0];
David Hendricks1e9d7ca2016-03-14 15:50:34 -07002192 *num_entries = ARRAY_SIZE(gd25q128_cmp1_ranges);
2193 }
2194
2195 break;
David Hendricksaf3944a2014-07-28 18:37:40 -07002196 }
2197 default:
2198 msg_cerr("%s() %d: GigaDevice flash chip mismatch"
2199 " (0x%04x), aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01002200 flash->chip->model_id);
David Hendricksaf3944a2014-07-28 18:37:40 -07002201 return -1;
2202 }
2203 break;
David Hendricks83541d32014-07-15 20:58:21 -07002204 case MACRONIX_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +01002205 switch (flash->chip->model_id) {
David Hendricks83541d32014-07-15 20:58:21 -07002206 case MACRONIX_MX25L6405:
2207 /* FIXME: MX25L64* chips have mixed capabilities and
2208 share IDs */
2209 *wp = &mx25l6406e_wp;
2210 *num_entries = ARRAY_SIZE(mx25l6406e_ranges);
2211 break;
David Hendricksc3496092014-11-13 17:20:55 -08002212 case MACRONIX_MX25L6495F: {
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002213 uint8_t cr = mx25l_read_config_register(flash);
David Hendricksc3496092014-11-13 17:20:55 -08002214
2215 *wp = &mx25l6495f_wp;
2216 if (!(cr & (1 << 3))) { /* T/B == 0 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11002217 (*wp)->descrs = &mx25l6495f_tb0_ranges[0];
David Hendricksc3496092014-11-13 17:20:55 -08002218 *num_entries = ARRAY_SIZE(mx25l6495f_tb0_ranges);
2219 } else { /* T/B == 1 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11002220 (*wp)->descrs = &mx25l6495f_tb1_ranges[0];
David Hendricksc3496092014-11-13 17:20:55 -08002221 *num_entries = ARRAY_SIZE(mx25l6495f_tb1_ranges);
2222 }
2223 break;
2224 }
Vic Yang848bfd12018-03-23 10:24:07 -07002225 case MACRONIX_MX25L25635F: {
2226 uint8_t cr = mx25l_read_config_register(flash);
2227
2228 *wp = &mx25l25635f_wp;
2229 if (!(cr & (1 << 3))) { /* T/B == 0 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11002230 (*wp)->descrs = &mx25l25635f_tb0_ranges[0];
Vic Yang848bfd12018-03-23 10:24:07 -07002231 *num_entries = ARRAY_SIZE(mx25l25635f_tb0_ranges);
2232 } else { /* T/B == 1 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11002233 (*wp)->descrs = &mx25l25635f_tb1_ranges[0];
Vic Yang848bfd12018-03-23 10:24:07 -07002234 *num_entries = ARRAY_SIZE(mx25l25635f_tb1_ranges);
2235 }
2236 break;
2237 }
David Hendricks83541d32014-07-15 20:58:21 -07002238 default:
2239 msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)"
2240 ", aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01002241 flash->chip->model_id);
David Hendricks83541d32014-07-15 20:58:21 -07002242 return -1;
2243 }
2244 break;
David Hendricksa9884852014-12-11 15:31:12 -08002245 case SPANSION_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +01002246 switch (flash->chip->model_id) {
David Hendricksa9884852014-12-11 15:31:12 -08002247 case SPANSION_S25FS128S_L:
2248 case SPANSION_S25FS128S_S: {
David Hendricksa9884852014-12-11 15:31:12 -08002249 *wp = &s25fs128s_wp;
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11002250 (*wp)->descrs = s25fs128s_ranges;
David Hendricks148a4bf2015-03-13 21:02:42 -07002251 *num_entries = ARRAY_SIZE(s25fs128s_ranges);
David Hendricksa9884852014-12-11 15:31:12 -08002252 break;
2253 }
David Hendricksc694bb82015-02-25 14:52:17 -08002254 case SPANSION_S25FL256S_UL:
2255 case SPANSION_S25FL256S_US: {
David Hendricksc694bb82015-02-25 14:52:17 -08002256 *wp = &s25fl256s_wp;
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11002257 (*wp)->descrs = s25fl256s_ranges;
David Hendricks148a4bf2015-03-13 21:02:42 -07002258 *num_entries = ARRAY_SIZE(s25fl256s_ranges);
David Hendricksc694bb82015-02-25 14:52:17 -08002259 break;
2260 }
David Hendricksa9884852014-12-11 15:31:12 -08002261 default:
2262 msg_cerr("%s():%d Spansion flash chip mismatch (0x%04x)"
Patrick Georgif3fa2992017-02-02 16:24:44 +01002263 ", aborting\n", __func__, __LINE__,
2264 flash->chip->model_id);
David Hendricksa9884852014-12-11 15:31:12 -08002265 return -1;
2266 }
2267 break;
David Hendrickse0512a72014-07-15 20:30:47 -07002268 default:
2269 msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
Patrick Georgif3fa2992017-02-02 16:24:44 +01002270 __func__, flash->chip->manufacture_id);
David Hendrickse0512a72014-07-15 20:30:47 -07002271 return -1;
2272 }
2273
2274 return 0;
2275}
2276
Edward O'Callaghana3edcb22019-12-05 14:30:50 +11002277static uint8_t generic_get_bp_mask(struct wp_context *wp)
Marco Chen9d5bddb2020-02-11 17:12:56 +08002278{
2279 return ((1 << (wp->sr1.bp0_pos + wp->sr1.bp_bits)) - 1) ^ \
2280 ((1 << wp->sr1.bp0_pos) - 1);
2281}
2282
Edward O'Callaghana3edcb22019-12-05 14:30:50 +11002283static uint8_t generic_get_status_check_mask(struct wp_context *wp)
Marco Chen9d5bddb2020-02-11 17:12:56 +08002284{
2285 return generic_get_bp_mask(wp) | 1 << wp->sr1.srp_pos;
2286}
2287
David Hendrickse0512a72014-07-15 20:30:47 -07002288/* Given a [start, len], this function finds a block protect bit combination
2289 * (if possible) and sets the corresponding bits in "status". Remaining bits
2290 * are preserved. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002291static int generic_range_to_status(const struct flashctx *flash,
David Hendrickse0512a72014-07-15 20:30:47 -07002292 unsigned int start, unsigned int len,
Marco Chen9d5bddb2020-02-11 17:12:56 +08002293 uint8_t *status, uint8_t *check_mask)
David Hendrickse0512a72014-07-15 20:30:47 -07002294{
Edward O'Callaghana3edcb22019-12-05 14:30:50 +11002295 struct wp_context *wp;
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11002296 struct wp_range_descriptor *r;
David Hendrickse0512a72014-07-15 20:30:47 -07002297 int i, range_found = 0, num_entries;
2298 uint8_t bp_mask;
2299
2300 if (generic_range_table(flash, &wp, &num_entries))
2301 return -1;
2302
Marco Chen9d5bddb2020-02-11 17:12:56 +08002303 bp_mask = generic_get_bp_mask(wp);
David Hendrickse0512a72014-07-15 20:30:47 -07002304
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11002305 for (i = 0, r = &wp->descrs[0]; i < num_entries; i++, r++) {
David Hendrickse0512a72014-07-15 20:30:47 -07002306 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
2307 start, len, r->range.start, r->range.len);
2308 if ((start == r->range.start) && (len == r->range.len)) {
2309 *status &= ~(bp_mask);
2310 *status |= r->bp << (wp->sr1.bp0_pos);
David Hendricks148a4bf2015-03-13 21:02:42 -07002311
2312 if (wp->set_modifier_bits) {
2313 if (wp->set_modifier_bits(flash, &r->m) < 0) {
2314 msg_cerr("error setting modifier "
2315 "bits for range.\n");
2316 return -1;
2317 }
2318 }
2319
David Hendrickse0512a72014-07-15 20:30:47 -07002320 range_found = 1;
2321 break;
2322 }
2323 }
2324
2325 if (!range_found) {
Edward O'Callaghan3be63e02020-03-27 14:44:24 +11002326 msg_cerr("%s: matching range not found\n", __func__);
David Hendrickse0512a72014-07-15 20:30:47 -07002327 return -1;
2328 }
Edward O'Callaghanbea239e2019-12-04 14:42:54 +11002329
Marco Chen9d5bddb2020-02-11 17:12:56 +08002330 *check_mask = generic_get_status_check_mask(wp);
David Hendrickse0512a72014-07-15 20:30:47 -07002331 return 0;
2332}
2333
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002334static int generic_status_to_range(const struct flashctx *flash,
David Hendrickse0512a72014-07-15 20:30:47 -07002335 const uint8_t sr1, unsigned int *start, unsigned int *len)
2336{
Edward O'Callaghana3edcb22019-12-05 14:30:50 +11002337 struct wp_context *wp;
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11002338 struct wp_range_descriptor *r;
Duncan Laurie04ca1172015-03-12 09:25:34 -07002339 int num_entries, i, status_found = 0;
David Hendrickse0512a72014-07-15 20:30:47 -07002340 uint8_t sr1_bp;
Edward O'Callaghan9c4c9a52019-12-04 18:18:01 +11002341 struct modifier_bits m;
David Hendrickse0512a72014-07-15 20:30:47 -07002342
2343 if (generic_range_table(flash, &wp, &num_entries))
2344 return -1;
2345
David Hendricks148a4bf2015-03-13 21:02:42 -07002346 /* modifier bits may be compared more than once, so get them here */
Edward O'Callaghanadcc7782019-12-04 14:50:14 +11002347 if (wp->get_modifier_bits && wp->get_modifier_bits(flash, &m) < 0)
David Hendricks148a4bf2015-03-13 21:02:42 -07002348 return -1;
David Hendricks148a4bf2015-03-13 21:02:42 -07002349
David Hendrickse0512a72014-07-15 20:30:47 -07002350 sr1_bp = (sr1 >> wp->sr1.bp0_pos) & ((1 << wp->sr1.bp_bits) - 1);
2351
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11002352 for (i = 0, r = &wp->descrs[0]; i < num_entries; i++, r++) {
David Hendricks148a4bf2015-03-13 21:02:42 -07002353 if (wp->get_modifier_bits) {
2354 if (memcmp(&m, &r->m, sizeof(m)))
2355 continue;
2356 }
David Hendrickse0512a72014-07-15 20:30:47 -07002357 msg_cspew("comparing 0x%02x 0x%02x\n", sr1_bp, r->bp);
2358 if (sr1_bp == r->bp) {
2359 *start = r->range.start;
2360 *len = r->range.len;
2361 status_found = 1;
2362 break;
2363 }
2364 }
2365
2366 if (!status_found) {
2367 msg_cerr("matching status not found\n");
2368 return -1;
2369 }
Edward O'Callaghanbea239e2019-12-04 14:42:54 +11002370
David Hendrickse0512a72014-07-15 20:30:47 -07002371 return 0;
2372}
2373
2374/* Given a [start, len], this function calls generic_range_to_status() to
2375 * convert it to flash-chip-specific range bits, then sets into status register.
2376 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002377static int generic_set_range(const struct flashctx *flash,
David Hendrickse0512a72014-07-15 20:30:47 -07002378 unsigned int start, unsigned int len)
2379{
Marco Chen9d5bddb2020-02-11 17:12:56 +08002380 uint8_t status, expected, check_mask;
David Hendrickse0512a72014-07-15 20:30:47 -07002381
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05302382 status = do_read_status(flash);
David Hendrickse0512a72014-07-15 20:30:47 -07002383 msg_cdbg("%s: old status: 0x%02x\n", __func__, status);
2384
2385 expected = status; /* preserve non-bp bits */
Marco Chen9d5bddb2020-02-11 17:12:56 +08002386 if (generic_range_to_status(flash, start, len, &expected, &check_mask))
David Hendrickse0512a72014-07-15 20:30:47 -07002387 return -1;
2388
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05302389 do_write_status(flash, expected);
David Hendrickse0512a72014-07-15 20:30:47 -07002390
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05302391 status = do_read_status(flash);
David Hendrickse0512a72014-07-15 20:30:47 -07002392 msg_cdbg("%s: new status: 0x%02x\n", __func__, status);
Marco Chen9d5bddb2020-02-11 17:12:56 +08002393 if ((status & check_mask) != (expected & check_mask)) {
2394 msg_cerr("expected=0x%02x, but actual=0x%02x. check mask=0x%02x\n",
2395 expected, status, check_mask);
David Hendrickse0512a72014-07-15 20:30:47 -07002396 return 1;
2397 }
David Hendrickse0512a72014-07-15 20:30:47 -07002398 return 0;
2399}
2400
2401/* Set/clear the status regsiter write protect bit in SR1. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002402static int generic_set_srp0(const struct flashctx *flash, int enable)
David Hendrickse0512a72014-07-15 20:30:47 -07002403{
Marco Chen9d5bddb2020-02-11 17:12:56 +08002404 uint8_t status, expected, check_mask;
Edward O'Callaghana3edcb22019-12-05 14:30:50 +11002405 struct wp_context *wp;
David Hendrickse0512a72014-07-15 20:30:47 -07002406 int num_entries;
2407
2408 if (generic_range_table(flash, &wp, &num_entries))
2409 return -1;
2410
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05302411 expected = do_read_status(flash);
David Hendrickse0512a72014-07-15 20:30:47 -07002412 msg_cdbg("%s: old status: 0x%02x\n", __func__, expected);
2413
2414 if (enable)
2415 expected |= 1 << wp->sr1.srp_pos;
2416 else
2417 expected &= ~(1 << wp->sr1.srp_pos);
2418
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05302419 do_write_status(flash, expected);
David Hendrickse0512a72014-07-15 20:30:47 -07002420
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05302421 status = do_read_status(flash);
David Hendrickse0512a72014-07-15 20:30:47 -07002422 msg_cdbg("%s: new status: 0x%02x\n", __func__, status);
Marco Chen9d5bddb2020-02-11 17:12:56 +08002423
2424 check_mask = generic_get_status_check_mask(wp);
2425 msg_cdbg("%s: check mask: 0x%02x\n", __func__, check_mask);
2426 if ((status & check_mask) != (expected & check_mask)) {
2427 msg_cerr("expected=0x%02x, but actual=0x%02x. check mask=0x%02x\n",
2428 expected, status, check_mask);
David Hendrickse0512a72014-07-15 20:30:47 -07002429 return -1;
Marco Chen9d5bddb2020-02-11 17:12:56 +08002430 }
David Hendrickse0512a72014-07-15 20:30:47 -07002431
2432 return 0;
2433}
2434
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002435static int generic_enable_writeprotect(const struct flashctx *flash,
David Hendrickse0512a72014-07-15 20:30:47 -07002436 enum wp_mode wp_mode)
2437{
2438 int ret;
2439
Edward O'Callaghanca44e5c2019-12-04 14:23:54 +11002440 if (wp_mode != WP_MODE_HARDWARE) {
David Hendrickse0512a72014-07-15 20:30:47 -07002441 msg_cerr("%s(): unsupported write-protect mode\n", __func__);
2442 return 1;
2443 }
2444
Edward O'Callaghanca44e5c2019-12-04 14:23:54 +11002445 ret = generic_set_srp0(flash, 1);
David Hendrickse0512a72014-07-15 20:30:47 -07002446 if (ret)
2447 msg_cerr("%s(): error=%d.\n", __func__, ret);
Edward O'Callaghanca44e5c2019-12-04 14:23:54 +11002448
David Hendrickse0512a72014-07-15 20:30:47 -07002449 return ret;
2450}
2451
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002452static int generic_disable_writeprotect(const struct flashctx *flash)
David Hendrickse0512a72014-07-15 20:30:47 -07002453{
2454 int ret;
2455
2456 ret = generic_set_srp0(flash, 0);
2457 if (ret)
2458 msg_cerr("%s(): error=%d.\n", __func__, ret);
Edward O'Callaghanbea239e2019-12-04 14:42:54 +11002459
David Hendrickse0512a72014-07-15 20:30:47 -07002460 return ret;
2461}
2462
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002463static int generic_list_ranges(const struct flashctx *flash)
David Hendrickse0512a72014-07-15 20:30:47 -07002464{
Edward O'Callaghana3edcb22019-12-05 14:30:50 +11002465 struct wp_context *wp;
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11002466 struct wp_range_descriptor *r;
David Hendrickse0512a72014-07-15 20:30:47 -07002467 int i, num_entries;
2468
2469 if (generic_range_table(flash, &wp, &num_entries))
2470 return -1;
2471
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11002472 r = &wp->descrs[0];
David Hendrickse0512a72014-07-15 20:30:47 -07002473 for (i = 0; i < num_entries; i++) {
2474 msg_cinfo("start: 0x%06x, length: 0x%06x\n",
2475 r->range.start, r->range.len);
2476 r++;
2477 }
2478
2479 return 0;
2480}
2481
Edward O'Callaghana3edcb22019-12-05 14:30:50 +11002482static int wp_context_status(const struct flashctx *flash)
David Hendrickse0512a72014-07-15 20:30:47 -07002483{
2484 uint8_t sr1;
2485 unsigned int start, len;
2486 int ret = 0;
Edward O'Callaghana3edcb22019-12-05 14:30:50 +11002487 struct wp_context *wp;
David Hendrickse0512a72014-07-15 20:30:47 -07002488 int num_entries, wp_en;
2489
2490 if (generic_range_table(flash, &wp, &num_entries))
2491 return -1;
2492
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05302493 sr1 = do_read_status(flash);
David Hendrickse0512a72014-07-15 20:30:47 -07002494 wp_en = (sr1 >> wp->sr1.srp_pos) & 1;
2495
2496 msg_cinfo("WP: status: 0x%04x\n", sr1);
2497 msg_cinfo("WP: status.srp0: %x\n", wp_en);
2498 /* FIXME: SRP1 is not really generic, but we probably should print
2499 * it anyway to have consistent output. #legacycruft */
2500 msg_cinfo("WP: status.srp1: %x\n", 0);
2501 msg_cinfo("WP: write protect is %s.\n",
2502 wp_en ? "enabled" : "disabled");
2503
2504 msg_cinfo("WP: write protect range: ");
2505 if (generic_status_to_range(flash, sr1, &start, &len)) {
2506 msg_cinfo("(cannot resolve the range)\n");
2507 ret = -1;
2508 } else {
2509 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
2510 }
2511
2512 return ret;
2513}
2514
2515struct wp wp_generic = {
2516 .list_ranges = generic_list_ranges,
2517 .set_range = generic_set_range,
2518 .enable = generic_enable_writeprotect,
2519 .disable = generic_disable_writeprotect,
Edward O'Callaghana3edcb22019-12-05 14:30:50 +11002520 .wp_status = wp_context_status,
David Hendrickse0512a72014-07-15 20:30:47 -07002521};