blob: cb4012d6cbd5fdd12977d4ca902e3f827267d939 [file] [log] [blame]
David Hendricksd1c55d72010-08-24 15:14:19 -07001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2010 Google Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
David Hendricksf7924d12010-06-10 21:26:44 -070021#include <stdlib.h>
22#include <string.h>
23
24#include "flash.h"
25#include "flashchips.h"
26#include "chipdrivers.h"
Louis Yung-Chieh Lo52aa9302010-09-06 10:45:02 +080027#include "spi.h"
David Hendricks23cd7782010-08-25 12:42:38 -070028#include "writeprotect.h"
David Hendricksf7924d12010-06-10 21:26:44 -070029
David Hendricks1c09f802012-10-03 11:03:48 -070030/*
David Hendricksf7924d12010-06-10 21:26:44 -070031 * The following procedures rely on look-up tables to match the user-specified
32 * range with the chip's supported ranges. This turned out to be the most
33 * elegant approach since diferent flash chips use different levels of
34 * granularity and methods to determine protected ranges. In other words,
David Hendrickse0512a72014-07-15 20:30:47 -070035 * be stupid and simple since clever arithmetic will not work for many chips.
David Hendricksf7924d12010-06-10 21:26:44 -070036 */
37
38struct wp_range {
39 unsigned int start; /* starting address */
40 unsigned int len; /* len */
41};
42
43enum bit_state {
44 OFF = 0,
45 ON = 1,
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +080046 X = -1 /* don't care. Must be bigger than max # of bp. */
David Hendricksf7924d12010-06-10 21:26:44 -070047};
48
David Hendrickse0512a72014-07-15 20:30:47 -070049/*
50 * Generic write-protection schema for 25-series SPI flash chips. This assumes
51 * there is a status register that contains one or more consecutive bits which
52 * determine which address range is protected.
53 */
54
55struct status_register_layout {
56 int bp0_pos; /* position of BP0 */
57 int bp_bits; /* number of block protect bits */
58 int srp_pos; /* position of status register protect enable bit */
59};
60
61struct generic_range {
62 unsigned int bp; /* block protect bitfield */
63 struct wp_range range;
64};
65
66struct generic_wp {
67 struct status_register_layout sr1; /* status register 1 */
68 struct generic_range *ranges;
69};
70
71/*
72 * The following ranges and functions are useful for representing Winbond-
73 * style writeprotect schema in which there are typically 5 bits of
74 * relevant information stored in status register 1:
75 * sec: This bit indicates the units (sectors vs. blocks)
76 * tb: The top-bottom bit indicates if the affected range is at the top of
77 * the flash memory's address space or at the bottom.
78 * bp[2:0]: The number of affected sectors/blocks.
79 */
David Hendricksf7924d12010-06-10 21:26:44 -070080struct w25q_range {
81 enum bit_state sec; /* if 1, bp[2:0] describe sectors */
82 enum bit_state tb; /* top/bottom select */
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +080083 int bp; /* block protect bitfield */
David Hendricksf7924d12010-06-10 21:26:44 -070084 struct wp_range range;
85};
86
David Hendrickse0512a72014-07-15 20:30:47 -070087/*
88 * Mask to extract write-protect enable and range bits
89 * Status register 1:
90 * SRP0: bit 7
91 * range(BP2-BP0): bit 4-2
92 * Status register 2:
93 * SRP1: bit 1
94 */
95#define MASK_WP_AREA (0x9C)
96#define MASK_WP2_AREA (0x01)
97
David Hendricks57566ed2010-08-16 18:24:45 -070098struct w25q_range en25f40_ranges[] = {
99 { X, X, 0, {0, 0} }, /* none */
100 { 0, 0, 0x1, {0x000000, 504 * 1024} },
101 { 0, 0, 0x2, {0x000000, 496 * 1024} },
102 { 0, 0, 0x3, {0x000000, 480 * 1024} },
103 { 0, 0, 0x4, {0x000000, 448 * 1024} },
104 { 0, 0, 0x5, {0x000000, 384 * 1024} },
105 { 0, 0, 0x6, {0x000000, 256 * 1024} },
106 { 0, 0, 0x7, {0x000000, 512 * 1024} },
107};
108
David Hendrickse185bf22011-05-24 15:34:18 -0700109struct w25q_range en25q40_ranges[] = {
110 { 0, 0, 0, {0, 0} }, /* none */
111 { 0, 0, 0x1, {0x000000, 504 * 1024} },
112 { 0, 0, 0x2, {0x000000, 496 * 1024} },
113 { 0, 0, 0x3, {0x000000, 480 * 1024} },
114
115 { 0, 1, 0x0, {0x000000, 448 * 1024} },
116 { 0, 1, 0x1, {0x000000, 384 * 1024} },
117 { 0, 1, 0x2, {0x000000, 256 * 1024} },
118 { 0, 1, 0x3, {0x000000, 512 * 1024} },
119};
120
121struct w25q_range en25q80_ranges[] = {
122 { 0, 0, 0, {0, 0} }, /* none */
123 { 0, 0, 0x1, {0x000000, 1016 * 1024} },
124 { 0, 0, 0x2, {0x000000, 1008 * 1024} },
125 { 0, 0, 0x3, {0x000000, 992 * 1024} },
126 { 0, 0, 0x4, {0x000000, 960 * 1024} },
127 { 0, 0, 0x5, {0x000000, 896 * 1024} },
128 { 0, 0, 0x6, {0x000000, 768 * 1024} },
129 { 0, 0, 0x7, {0x000000, 1024 * 1024} },
130};
131
132struct w25q_range en25q32_ranges[] = {
133 { 0, 0, 0, {0, 0} }, /* none */
134 { 0, 0, 0x1, {0x000000, 4032 * 1024} },
135 { 0, 0, 0x2, {0x000000, 3968 * 1024} },
136 { 0, 0, 0x3, {0x000000, 3840 * 1024} },
137 { 0, 0, 0x4, {0x000000, 3584 * 1024} },
138 { 0, 0, 0x5, {0x000000, 3072 * 1024} },
139 { 0, 0, 0x6, {0x000000, 2048 * 1024} },
140 { 0, 0, 0x7, {0x000000, 4096 * 1024} },
141
142 { 0, 1, 0, {0, 0} }, /* none */
143 { 0, 1, 0x1, {0x010000, 4032 * 1024} },
144 { 0, 1, 0x2, {0x020000, 3968 * 1024} },
145 { 0, 1, 0x3, {0x040000, 3840 * 1024} },
146 { 0, 1, 0x4, {0x080000, 3584 * 1024} },
147 { 0, 1, 0x5, {0x100000, 3072 * 1024} },
148 { 0, 1, 0x6, {0x200000, 2048 * 1024} },
149 { 0, 1, 0x7, {0x000000, 4096 * 1024} },
150};
151
152struct w25q_range en25q64_ranges[] = {
153 { 0, 0, 0, {0, 0} }, /* none */
154 { 0, 0, 0x1, {0x000000, 8128 * 1024} },
155 { 0, 0, 0x2, {0x000000, 8064 * 1024} },
156 { 0, 0, 0x3, {0x000000, 7936 * 1024} },
157 { 0, 0, 0x4, {0x000000, 7680 * 1024} },
158 { 0, 0, 0x5, {0x000000, 7168 * 1024} },
159 { 0, 0, 0x6, {0x000000, 6144 * 1024} },
160 { 0, 0, 0x7, {0x000000, 8192 * 1024} },
161
162 { 0, 1, 0, {0, 0} }, /* none */
163 { 0, 1, 0x1, {0x010000, 8128 * 1024} },
164 { 0, 1, 0x2, {0x020000, 8064 * 1024} },
165 { 0, 1, 0x3, {0x040000, 7936 * 1024} },
166 { 0, 1, 0x4, {0x080000, 7680 * 1024} },
167 { 0, 1, 0x5, {0x100000, 7168 * 1024} },
168 { 0, 1, 0x6, {0x200000, 6144 * 1024} },
169 { 0, 1, 0x7, {0x000000, 8192 * 1024} },
170};
171
172struct w25q_range en25q128_ranges[] = {
173 { 0, 0, 0, {0, 0} }, /* none */
174 { 0, 0, 0x1, {0x000000, 16320 * 1024} },
175 { 0, 0, 0x2, {0x000000, 16256 * 1024} },
176 { 0, 0, 0x3, {0x000000, 16128 * 1024} },
177 { 0, 0, 0x4, {0x000000, 15872 * 1024} },
178 { 0, 0, 0x5, {0x000000, 15360 * 1024} },
179 { 0, 0, 0x6, {0x000000, 14336 * 1024} },
180 { 0, 0, 0x7, {0x000000, 16384 * 1024} },
181
182 { 0, 1, 0, {0, 0} }, /* none */
183 { 0, 1, 0x1, {0x010000, 16320 * 1024} },
184 { 0, 1, 0x2, {0x020000, 16256 * 1024} },
185 { 0, 1, 0x3, {0x040000, 16128 * 1024} },
186 { 0, 1, 0x4, {0x080000, 15872 * 1024} },
187 { 0, 1, 0x5, {0x100000, 15360 * 1024} },
188 { 0, 1, 0x6, {0x200000, 14336 * 1024} },
189 { 0, 1, 0x7, {0x000000, 16384 * 1024} },
190};
191
Marc Jonesb2f90022014-04-29 17:37:23 -0600192struct w25q_range en25s64_ranges[] = {
193 { 0, 0, 0, {0, 0} }, /* none */
194 { 0, 0, 0x1, {0x000000, 8064 * 1024} },
195 { 0, 0, 0x2, {0x000000, 7936 * 1024} },
196 { 0, 0, 0x3, {0x000000, 7680 * 1024} },
197 { 0, 0, 0x4, {0x000000, 7168 * 1024} },
198 { 0, 0, 0x5, {0x000000, 6144 * 1024} },
199 { 0, 0, 0x6, {0x000000, 4096 * 1024} },
200 { 0, 0, 0x7, {0x000000, 8192 * 1024} },
201
202 { 0, 1, 0, {0, 0} }, /* none */
203 { 0, 1, 0x1, {0x7e0000, 128 * 1024} },
204 { 0, 1, 0x2, {0x7c0000, 256 * 1024} },
205 { 0, 1, 0x3, {0x780000, 512 * 1024} },
206 { 0, 1, 0x4, {0x700000, 1024 * 1024} },
207 { 0, 1, 0x5, {0x600000, 2048 * 1024} },
208 { 0, 1, 0x6, {0x400000, 4096 * 1024} },
209 { 0, 1, 0x7, {0x000000, 8192 * 1024} },
210};
211
David Hendricksf8f00c72011-02-01 12:39:46 -0800212/* mx25l1005 ranges also work for the mx25l1005c */
213static struct w25q_range mx25l1005_ranges[] = {
214 { X, X, 0, {0, 0} }, /* none */
215 { X, X, 0x1, {0x010000, 64 * 1024} },
216 { X, X, 0x2, {0x000000, 128 * 1024} },
217 { X, X, 0x3, {0x000000, 128 * 1024} },
218};
219
220static struct w25q_range mx25l2005_ranges[] = {
221 { X, X, 0, {0, 0} }, /* none */
222 { X, X, 0x1, {0x030000, 64 * 1024} },
223 { X, X, 0x2, {0x020000, 128 * 1024} },
224 { X, X, 0x3, {0x000000, 256 * 1024} },
225};
226
227static struct w25q_range mx25l4005_ranges[] = {
228 { X, X, 0, {0, 0} }, /* none */
229 { X, X, 0x1, {0x070000, 64 * 1 * 1024} }, /* block 7 */
230 { X, X, 0x2, {0x060000, 64 * 2 * 1024} }, /* blocks 6-7 */
231 { X, X, 0x3, {0x040000, 64 * 4 * 1024} }, /* blocks 4-7 */
232 { X, X, 0x4, {0x000000, 512 * 1024} },
233 { X, X, 0x5, {0x000000, 512 * 1024} },
234 { X, X, 0x6, {0x000000, 512 * 1024} },
235 { X, X, 0x7, {0x000000, 512 * 1024} },
236};
237
238static struct w25q_range mx25l8005_ranges[] = {
239 { X, X, 0, {0, 0} }, /* none */
240 { X, X, 0x1, {0x0f0000, 64 * 1 * 1024} }, /* block 15 */
241 { X, X, 0x2, {0x0e0000, 64 * 2 * 1024} }, /* blocks 14-15 */
242 { X, X, 0x3, {0x0c0000, 64 * 4 * 1024} }, /* blocks 12-15 */
243 { X, X, 0x4, {0x080000, 64 * 8 * 1024} }, /* blocks 8-15 */
244 { X, X, 0x5, {0x000000, 1024 * 1024} },
245 { X, X, 0x6, {0x000000, 1024 * 1024} },
246 { X, X, 0x7, {0x000000, 1024 * 1024} },
247};
248
249#if 0
250/* FIXME: mx25l1605 has the same IDs as the mx25l1605d */
251static struct w25q_range mx25l1605_ranges[] = {
252 { X, X, 0, {0, 0} }, /* none */
253 { X, X, 0x1, {0x1f0000, 64 * 1024} }, /* block 31 */
254 { X, X, 0x2, {0x1e0000, 128 * 1024} }, /* blocks 30-31 */
255 { X, X, 0x3, {0x1c0000, 256 * 1024} }, /* blocks 28-31 */
256 { X, X, 0x4, {0x180000, 512 * 1024} }, /* blocks 24-31 */
257 { X, X, 0x4, {0x100000, 1024 * 1024} }, /* blocks 16-31 */
258 { X, X, 0x6, {0x000000, 2048 * 1024} },
259 { X, X, 0x7, {0x000000, 2048 * 1024} },
260};
261#endif
262
263#if 0
264/* FIXME: mx25l6405 has the same IDs as the mx25l6405d */
265static struct w25q_range mx25l6405_ranges[] = {
266 { X, 0, 0, {0, 0} }, /* none */
267 { X, 0, 0x1, {0x7f0000, 64 * 1 * 1024} }, /* block 127 */
268 { X, 0, 0x2, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */
269 { X, 0, 0x3, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */
270 { X, 0, 0x4, {0x780000, 64 * 8 * 1024} }, /* blocks 120-127 */
271 { X, 0, 0x5, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */
272 { X, 0, 0x6, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */
273 { X, 0, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
274
275 { X, 1, 0x0, {0x000000, 8192 * 1024} },
276 { X, 1, 0x1, {0x000000, 8192 * 1024} },
277 { X, 1, 0x2, {0x000000, 8192 * 1024} },
278 { X, 1, 0x3, {0x000000, 8192 * 1024} },
279 { X, 1, 0x4, {0x000000, 8192 * 1024} },
280 { X, 1, 0x5, {0x000000, 8192 * 1024} },
281 { X, 1, 0x6, {0x000000, 8192 * 1024} },
282 { X, 1, 0x7, {0x000000, 8192 * 1024} },
283};
284#endif
285
286static struct w25q_range mx25l1605d_ranges[] = {
287 { X, 0, 0, {0, 0} }, /* none */
288 { X, 0, 0x1, {0x1f0000, 64 * 1 * 1024} }, /* block 31 */
289 { X, 0, 0x2, {0x1e0000, 64 * 2 * 1024} }, /* blocks 30-31 */
290 { X, 0, 0x3, {0x1c0000, 64 * 4 * 1024} }, /* blocks 28-31 */
291 { X, 0, 0x4, {0x180000, 64 * 8 * 1024} }, /* blocks 24-31 */
292 { X, 0, 0x5, {0x100000, 64 * 16 * 1024} }, /* blocks 16-31 */
293 { X, 0, 0x6, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
294 { X, 0, 0x7, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
295
296 { X, 1, 0x0, {0x000000, 2048 * 1024} },
297 { X, 1, 0x1, {0x000000, 2048 * 1024} },
298 { X, 1, 0x2, {0x000000, 64 * 16 * 1024} }, /* blocks 0-15 */
299 { X, 1, 0x3, {0x000000, 64 * 24 * 1024} }, /* blocks 0-23 */
300 { X, 1, 0x4, {0x000000, 64 * 28 * 1024} }, /* blocks 0-27 */
301 { X, 1, 0x5, {0x000000, 64 * 30 * 1024} }, /* blocks 0-29 */
302 { X, 1, 0x6, {0x000000, 64 * 31 * 1024} }, /* blocks 0-30 */
303 { X, 1, 0x7, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
304};
305
306/* FIXME: Is there an mx25l3205 (without a trailing letter)? */
David Hendricksac72e362010-08-16 18:20:03 -0700307static struct w25q_range mx25l3205d_ranges[] = {
308 { X, 0, 0, {0, 0} }, /* none */
309 { X, 0, 0x1, {0x3f0000, 64 * 1024} },
310 { X, 0, 0x2, {0x3e0000, 128 * 1024} },
311 { X, 0, 0x3, {0x3c0000, 256 * 1024} },
312 { X, 0, 0x4, {0x380000, 512 * 1024} },
313 { X, 0, 0x5, {0x300000, 1024 * 1024} },
314 { X, 0, 0x6, {0x200000, 2048 * 1024} },
315 { X, 0, 0x7, {0x000000, 4096 * 1024} },
316
317 { X, 1, 0x0, {0x000000, 4096 * 1024} },
318 { X, 1, 0x1, {0x000000, 2048 * 1024} },
319 { X, 1, 0x2, {0x000000, 3072 * 1024} },
320 { X, 1, 0x3, {0x000000, 3584 * 1024} },
321 { X, 1, 0x4, {0x000000, 3840 * 1024} },
322 { X, 1, 0x5, {0x000000, 3968 * 1024} },
323 { X, 1, 0x6, {0x000000, 4032 * 1024} },
324 { X, 1, 0x7, {0x000000, 4096 * 1024} },
325};
326
Vincent Palatin87e092a2013-02-28 15:46:14 -0800327static struct w25q_range mx25u3235e_ranges[] = {
328 { X, 0, 0, {0, 0} }, /* none */
329 { 0, 0, 0x1, {0x3f0000, 64 * 1024} },
330 { 0, 0, 0x2, {0x3e0000, 128 * 1024} },
331 { 0, 0, 0x3, {0x3c0000, 256 * 1024} },
332 { 0, 0, 0x4, {0x380000, 512 * 1024} },
333 { 0, 0, 0x5, {0x300000, 1024 * 1024} },
334 { 0, 0, 0x6, {0x200000, 2048 * 1024} },
335 { 0, 0, 0x7, {0x000000, 4096 * 1024} },
336
337 { 0, 1, 0x0, {0x000000, 4096 * 1024} },
338 { 0, 1, 0x1, {0x000000, 2048 * 1024} },
339 { 0, 1, 0x2, {0x000000, 3072 * 1024} },
340 { 0, 1, 0x3, {0x000000, 3584 * 1024} },
341 { 0, 1, 0x4, {0x000000, 3840 * 1024} },
342 { 0, 1, 0x5, {0x000000, 3968 * 1024} },
343 { 0, 1, 0x6, {0x000000, 4032 * 1024} },
344 { 0, 1, 0x7, {0x000000, 4096 * 1024} },
345};
346
Jongpil66a96492014-08-14 17:59:06 +0900347static struct w25q_range mx25u6435e_ranges[] = {
348 { X, 0, 0, {0, 0} }, /* none */
349 { 0, 0, 0x1, {0x7f0000, 1 * 64 * 1024} }, /* block 127 */
350 { 0, 0, 0x2, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
351 { 0, 0, 0x3, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
352 { 0, 0, 0x4, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
353 { 0, 0, 0x5, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
354 { 0, 0, 0x6, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
355 { 0, 0, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
356
357 { 0, 1, 0x0, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
358 { 0, 1, 0x1, {0x000000, 96 * 64 * 1024} }, /* blocks 0-95 */
359 { 0, 1, 0x2, {0x000000, 112 * 64 * 1024} }, /* blocks 0-111 */
360 { 0, 1, 0x3, {0x000000, 120 * 64 * 1024} }, /* blocks 0-119 */
361 { 0, 1, 0x4, {0x000000, 124 * 64 * 1024} }, /* blocks 0-123 */
362 { 0, 1, 0x5, {0x000000, 126 * 64 * 1024} }, /* blocks 0-125 */
363 { 0, 1, 0x6, {0x000000, 127 * 64 * 1024} }, /* blocks 0-126 */
364 { 0, 1, 0x7, {0x000000, 128 * 64 * 1024} }, /* blocks 0-127 */
365};
366
David Hendricksbfa624b2012-07-24 12:47:59 -0700367static struct w25q_range n25q064_ranges[] = {
368 { X, 0, 0, {0, 0} }, /* none */
369
370 { 0, 0, 0x1, {0x7f0000, 64 * 1024} }, /* block 127 */
371 { 0, 0, 0x2, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
372 { 0, 0, 0x3, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
373 { 0, 0, 0x4, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
374 { 0, 0, 0x5, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
375 { 0, 0, 0x6, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
376 { 0, 0, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
377
378 { 1, 0, 0x1, {0x000000, 64 * 1024} }, /* block 0 */
379 { 1, 0, 0x2, {0x000000, 2 * 64 * 1024} }, /* blocks 0-1 */
380 { 1, 0, 0x3, {0x000000, 4 * 64 * 1024} }, /* blocks 0-3 */
381 { 1, 0, 0x4, {0x000000, 8 * 64 * 1024} }, /* blocks 0-7 */
382 { 1, 0, 0x5, {0x000000, 16 * 64 * 1024} }, /* blocks 0-15 */
383 { 1, 0, 0x6, {0x000000, 32 * 64 * 1024} }, /* blocks 0-31 */
384 { 1, 0, 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
385
386 { X, 1, 0x0, {0x000000, 128 * 64 * 1024} }, /* all */
387 { X, 1, 0x1, {0x000000, 128 * 64 * 1024} }, /* all */
388 { X, 1, 0x2, {0x000000, 128 * 64 * 1024} }, /* all */
389 { X, 1, 0x3, {0x000000, 128 * 64 * 1024} }, /* all */
390 { X, 1, 0x4, {0x000000, 128 * 64 * 1024} }, /* all */
391 { X, 1, 0x5, {0x000000, 128 * 64 * 1024} }, /* all */
392 { X, 1, 0x6, {0x000000, 128 * 64 * 1024} }, /* all */
393 { X, 1, 0x7, {0x000000, 128 * 64 * 1024} }, /* all */
394};
395
David Hendricksf7924d12010-06-10 21:26:44 -0700396static struct w25q_range w25q16_ranges[] = {
397 { X, X, 0, {0, 0} }, /* none */
398 { 0, 0, 0x1, {0x1f0000, 64 * 1024} },
399 { 0, 0, 0x2, {0x1e0000, 128 * 1024} },
400 { 0, 0, 0x3, {0x1c0000, 256 * 1024} },
401 { 0, 0, 0x4, {0x180000, 512 * 1024} },
402 { 0, 0, 0x5, {0x100000, 1024 * 1024} },
403
404 { 0, 1, 0x1, {0x000000, 64 * 1024} },
405 { 0, 1, 0x2, {0x000000, 128 * 1024} },
406 { 0, 1, 0x3, {0x000000, 256 * 1024} },
407 { 0, 1, 0x4, {0x000000, 512 * 1024} },
408 { 0, 1, 0x5, {0x000000, 1024 * 1024} },
409 { X, X, 0x6, {0x000000, 2048 * 1024} },
410 { X, X, 0x7, {0x000000, 2048 * 1024} },
411
412 { 1, 0, 0x1, {0x1ff000, 4 * 1024} },
413 { 1, 0, 0x2, {0x1fe000, 8 * 1024} },
414 { 1, 0, 0x3, {0x1fc000, 16 * 1024} },
415 { 1, 0, 0x4, {0x1f8000, 32 * 1024} },
416 { 1, 0, 0x5, {0x1f8000, 32 * 1024} },
417
418 { 1, 1, 0x1, {0x000000, 4 * 1024} },
419 { 1, 1, 0x2, {0x000000, 8 * 1024} },
420 { 1, 1, 0x3, {0x000000, 16 * 1024} },
421 { 1, 1, 0x4, {0x000000, 32 * 1024} },
422 { 1, 1, 0x5, {0x000000, 32 * 1024} },
423};
424
425static struct w25q_range w25q32_ranges[] = {
426 { X, X, 0, {0, 0} }, /* none */
427 { 0, 0, 0x1, {0x3f0000, 64 * 1024} },
428 { 0, 0, 0x2, {0x3e0000, 128 * 1024} },
429 { 0, 0, 0x3, {0x3c0000, 256 * 1024} },
430 { 0, 0, 0x4, {0x380000, 512 * 1024} },
431 { 0, 0, 0x5, {0x300000, 1024 * 1024} },
David Hendricks05653ff2010-06-15 16:05:12 -0700432 { 0, 0, 0x6, {0x200000, 2048 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700433
434 { 0, 1, 0x1, {0x000000, 64 * 1024} },
435 { 0, 1, 0x2, {0x000000, 128 * 1024} },
436 { 0, 1, 0x3, {0x000000, 256 * 1024} },
437 { 0, 1, 0x4, {0x000000, 512 * 1024} },
438 { 0, 1, 0x5, {0x000000, 1024 * 1024} },
439 { 0, 1, 0x6, {0x000000, 2048 * 1024} },
440 { X, X, 0x7, {0x000000, 4096 * 1024} },
441
442 { 1, 0, 0x1, {0x3ff000, 4 * 1024} },
443 { 1, 0, 0x2, {0x3fe000, 8 * 1024} },
444 { 1, 0, 0x3, {0x3fc000, 16 * 1024} },
445 { 1, 0, 0x4, {0x3f8000, 32 * 1024} },
446 { 1, 0, 0x5, {0x3f8000, 32 * 1024} },
447
448 { 1, 1, 0x1, {0x000000, 4 * 1024} },
449 { 1, 1, 0x2, {0x000000, 8 * 1024} },
450 { 1, 1, 0x3, {0x000000, 16 * 1024} },
451 { 1, 1, 0x4, {0x000000, 32 * 1024} },
452 { 1, 1, 0x5, {0x000000, 32 * 1024} },
453};
454
455static struct w25q_range w25q80_ranges[] = {
456 { X, X, 0, {0, 0} }, /* none */
457 { 0, 0, 0x1, {0x0f0000, 64 * 1024} },
458 { 0, 0, 0x2, {0x0e0000, 128 * 1024} },
459 { 0, 0, 0x3, {0x0c0000, 256 * 1024} },
460 { 0, 0, 0x4, {0x080000, 512 * 1024} },
461
462 { 0, 1, 0x1, {0x000000, 64 * 1024} },
463 { 0, 1, 0x2, {0x000000, 128 * 1024} },
464 { 0, 1, 0x3, {0x000000, 256 * 1024} },
465 { 0, 1, 0x4, {0x000000, 512 * 1024} },
David Hendricks05653ff2010-06-15 16:05:12 -0700466 { X, X, 0x6, {0x000000, 1024 * 1024} },
467 { X, X, 0x7, {0x000000, 1024 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700468
469 { 1, 0, 0x1, {0x1ff000, 4 * 1024} },
470 { 1, 0, 0x2, {0x1fe000, 8 * 1024} },
471 { 1, 0, 0x3, {0x1fc000, 16 * 1024} },
472 { 1, 0, 0x4, {0x1f8000, 32 * 1024} },
473 { 1, 0, 0x5, {0x1f8000, 32 * 1024} },
474
475 { 1, 1, 0x1, {0x000000, 4 * 1024} },
476 { 1, 1, 0x2, {0x000000, 8 * 1024} },
477 { 1, 1, 0x3, {0x000000, 16 * 1024} },
478 { 1, 1, 0x4, {0x000000, 32 * 1024} },
479 { 1, 1, 0x5, {0x000000, 32 * 1024} },
480};
481
David Hendricks2c4a76c2010-06-28 14:00:43 -0700482static struct w25q_range w25q64_ranges[] = {
483 { X, X, 0, {0, 0} }, /* none */
484
485 { 0, 0, 0x1, {0x7e0000, 128 * 1024} },
486 { 0, 0, 0x2, {0x7c0000, 256 * 1024} },
487 { 0, 0, 0x3, {0x780000, 512 * 1024} },
488 { 0, 0, 0x4, {0x700000, 1024 * 1024} },
489 { 0, 0, 0x5, {0x600000, 2048 * 1024} },
490 { 0, 0, 0x6, {0x400000, 4096 * 1024} },
491
492 { 0, 1, 0x1, {0x000000, 128 * 1024} },
493 { 0, 1, 0x2, {0x000000, 256 * 1024} },
494 { 0, 1, 0x3, {0x000000, 512 * 1024} },
495 { 0, 1, 0x4, {0x000000, 1024 * 1024} },
496 { 0, 1, 0x5, {0x000000, 2048 * 1024} },
497 { 0, 1, 0x6, {0x000000, 4096 * 1024} },
498 { X, X, 0x7, {0x000000, 8192 * 1024} },
499
500 { 1, 0, 0x1, {0x7ff000, 4 * 1024} },
501 { 1, 0, 0x2, {0x7fe000, 8 * 1024} },
502 { 1, 0, 0x3, {0x7fc000, 16 * 1024} },
503 { 1, 0, 0x4, {0x7f8000, 32 * 1024} },
504 { 1, 0, 0x5, {0x7f8000, 32 * 1024} },
505
506 { 1, 1, 0x1, {0x000000, 4 * 1024} },
507 { 1, 1, 0x2, {0x000000, 8 * 1024} },
508 { 1, 1, 0x3, {0x000000, 16 * 1024} },
509 { 1, 1, 0x4, {0x000000, 32 * 1024} },
510 { 1, 1, 0x5, {0x000000, 32 * 1024} },
511};
512
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800513struct w25q_range w25x10_ranges[] = {
514 { X, X, 0, {0, 0} }, /* none */
515 { 0, 0, 0x1, {0x010000, 64 * 1024} },
516 { 0, 1, 0x1, {0x000000, 64 * 1024} },
517 { X, X, 0x2, {0x000000, 128 * 1024} },
518 { X, X, 0x3, {0x000000, 128 * 1024} },
519};
520
521struct w25q_range w25x20_ranges[] = {
522 { X, X, 0, {0, 0} }, /* none */
523 { 0, 0, 0x1, {0x030000, 64 * 1024} },
524 { 0, 0, 0x2, {0x020000, 128 * 1024} },
525 { 0, 1, 0x1, {0x000000, 64 * 1024} },
526 { 0, 1, 0x2, {0x000000, 128 * 1024} },
527 { 0, X, 0x3, {0x000000, 256 * 1024} },
528};
529
David Hendricks470ca952010-08-13 14:01:53 -0700530struct w25q_range w25x40_ranges[] = {
531 { X, X, 0, {0, 0} }, /* none */
532 { 0, 0, 0x1, {0x070000, 64 * 1024} },
533 { 0, 0, 0x2, {0x060000, 128 * 1024} },
534 { 0, 0, 0x3, {0x040000, 256 * 1024} },
535 { 0, 1, 0x1, {0x000000, 64 * 1024} },
536 { 0, 1, 0x2, {0x000000, 128 * 1024} },
537 { 0, 1, 0x3, {0x000000, 256 * 1024} },
538 { 0, X, 0x4, {0x000000, 512 * 1024} },
539};
540
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800541struct w25q_range w25x80_ranges[] = {
542 { X, X, 0, {0, 0} }, /* none */
543 { 0, 0, 0x1, {0x0F0000, 64 * 1024} },
544 { 0, 0, 0x2, {0x0E0000, 128 * 1024} },
545 { 0, 0, 0x3, {0x0C0000, 256 * 1024} },
546 { 0, 0, 0x4, {0x080000, 512 * 1024} },
547 { 0, 1, 0x1, {0x000000, 64 * 1024} },
548 { 0, 1, 0x2, {0x000000, 128 * 1024} },
549 { 0, 1, 0x3, {0x000000, 256 * 1024} },
550 { 0, 1, 0x4, {0x000000, 512 * 1024} },
551 { 0, X, 0x5, {0x000000, 1024 * 1024} },
552 { 0, X, 0x6, {0x000000, 1024 * 1024} },
553 { 0, X, 0x7, {0x000000, 1024 * 1024} },
554};
555
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700556static struct w25q_range gd25q64_ranges[] = {
557 { X, X, 0, {0, 0} }, /* none */
558 { 0, 0, 0x1, {0x7e0000, 128 * 1024} },
559 { 0, 0, 0x2, {0x7c0000, 256 * 1024} },
560 { 0, 0, 0x3, {0x780000, 512 * 1024} },
561 { 0, 0, 0x4, {0x700000, 1024 * 1024} },
562 { 0, 0, 0x5, {0x600000, 2048 * 1024} },
563 { 0, 0, 0x6, {0x400000, 4096 * 1024} },
564
565 { 0, 1, 0x1, {0x000000, 128 * 1024} },
566 { 0, 1, 0x2, {0x000000, 256 * 1024} },
567 { 0, 1, 0x3, {0x000000, 512 * 1024} },
568 { 0, 1, 0x4, {0x000000, 1024 * 1024} },
569 { 0, 1, 0x5, {0x000000, 2048 * 1024} },
570 { 0, 1, 0x6, {0x000000, 4096 * 1024} },
571 { X, X, 0x7, {0x000000, 8192 * 1024} },
572
573 { 1, 0, 0x1, {0x7ff000, 4 * 1024} },
574 { 1, 0, 0x2, {0x7fe000, 8 * 1024} },
575 { 1, 0, 0x3, {0x7fc000, 16 * 1024} },
576 { 1, 0, 0x4, {0x7f8000, 32 * 1024} },
577 { 1, 0, 0x5, {0x7f8000, 32 * 1024} },
578 { 1, 0, 0x6, {0x7f8000, 32 * 1024} },
579
580 { 1, 1, 0x1, {0x000000, 4 * 1024} },
581 { 1, 1, 0x2, {0x000000, 8 * 1024} },
582 { 1, 1, 0x3, {0x000000, 16 * 1024} },
583 { 1, 1, 0x4, {0x000000, 32 * 1024} },
584 { 1, 1, 0x5, {0x000000, 32 * 1024} },
585 { 1, 1, 0x6, {0x000000, 32 * 1024} },
586};
587
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +0800588static struct w25q_range a25l040_ranges[] = {
589 { X, X, 0x0, {0, 0} }, /* none */
590 { X, X, 0x1, {0x70000, 64 * 1024} },
591 { X, X, 0x2, {0x60000, 128 * 1024} },
592 { X, X, 0x3, {0x40000, 256 * 1024} },
593 { X, X, 0x4, {0x00000, 512 * 1024} },
594 { X, X, 0x5, {0x00000, 512 * 1024} },
595 { X, X, 0x6, {0x00000, 512 * 1024} },
596 { X, X, 0x7, {0x00000, 512 * 1024} },
597};
598
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800599/* Given a flash chip, this function returns its range table. */
600static int w25_range_table(const struct flashchip *flash,
601 struct w25q_range **w25q_ranges,
602 int *num_entries)
David Hendricksf7924d12010-06-10 21:26:44 -0700603{
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800604 *w25q_ranges = 0;
605 *num_entries = 0;
David Hendricksf7924d12010-06-10 21:26:44 -0700606
David Hendricksd494b0a2010-08-16 16:28:50 -0700607 switch (flash->manufacture_id) {
608 case WINBOND_NEX_ID:
609 switch(flash->model_id) {
David Hendricksc801adb2010-12-09 16:58:56 -0800610 case WINBOND_NEX_W25X10:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800611 *w25q_ranges = w25x10_ranges;
612 *num_entries = ARRAY_SIZE(w25x10_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800613 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800614 case WINBOND_NEX_W25X20:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800615 *w25q_ranges = w25x20_ranges;
616 *num_entries = ARRAY_SIZE(w25x20_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800617 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800618 case WINBOND_NEX_W25X40:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800619 *w25q_ranges = w25x40_ranges;
620 *num_entries = ARRAY_SIZE(w25x40_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700621 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800622 case WINBOND_NEX_W25X80:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800623 *w25q_ranges = w25x80_ranges;
624 *num_entries = ARRAY_SIZE(w25x80_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800625 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800626 case WINBOND_NEX_W25Q80:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800627 *w25q_ranges = w25q80_ranges;
628 *num_entries = ARRAY_SIZE(w25q80_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700629 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800630 case WINBOND_NEX_W25Q16:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800631 *w25q_ranges = w25q16_ranges;
632 *num_entries = ARRAY_SIZE(w25q16_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700633 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800634 case WINBOND_NEX_W25Q32:
Louis Yung-Chieh Lo469707f2012-05-18 16:38:37 +0800635 case WINBOND_NEX_W25Q32DW:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800636 *w25q_ranges = w25q32_ranges;
637 *num_entries = ARRAY_SIZE(w25q32_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700638 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800639 case WINBOND_NEX_W25Q64:
AdamTsai141a2622013-12-31 14:07:15 +0800640 case WINBOND_NEX_W25Q64DW:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800641 *w25q_ranges = w25q64_ranges;
642 *num_entries = ARRAY_SIZE(w25q64_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700643 break;
644 default:
645 msg_cerr("%s() %d: WINBOND flash chip mismatch (0x%04x)"
646 ", aborting\n", __func__, __LINE__,
647 flash->model_id);
648 return -1;
649 }
David Hendricks2c4a76c2010-06-28 14:00:43 -0700650 break;
David Hendricks57566ed2010-08-16 18:24:45 -0700651 case EON_ID_NOPREFIX:
652 switch (flash->model_id) {
David Hendricksc801adb2010-12-09 16:58:56 -0800653 case EON_EN25F40:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800654 *w25q_ranges = en25f40_ranges;
655 *num_entries = ARRAY_SIZE(en25f40_ranges);
David Hendricks57566ed2010-08-16 18:24:45 -0700656 break;
David Hendrickse185bf22011-05-24 15:34:18 -0700657 case EON_EN25Q40:
658 *w25q_ranges = en25q40_ranges;
659 *num_entries = ARRAY_SIZE(en25q40_ranges);
660 break;
661 case EON_EN25Q80:
662 *w25q_ranges = en25q80_ranges;
663 *num_entries = ARRAY_SIZE(en25q80_ranges);
664 break;
665 case EON_EN25Q32:
666 *w25q_ranges = en25q32_ranges;
667 *num_entries = ARRAY_SIZE(en25q32_ranges);
668 break;
669 case EON_EN25Q64:
670 *w25q_ranges = en25q64_ranges;
671 *num_entries = ARRAY_SIZE(en25q64_ranges);
672 break;
673 case EON_EN25Q128:
674 *w25q_ranges = en25q128_ranges;
675 *num_entries = ARRAY_SIZE(en25q128_ranges);
676 break;
Marc Jonesb2f90022014-04-29 17:37:23 -0600677 case EON_EN25S64:
678 *w25q_ranges = en25s64_ranges;
679 *num_entries = ARRAY_SIZE(en25s64_ranges);
680 break;
David Hendricks57566ed2010-08-16 18:24:45 -0700681 default:
682 msg_cerr("%s():%d: EON flash chip mismatch (0x%04x)"
683 ", aborting\n", __func__, __LINE__,
684 flash->model_id);
685 return -1;
686 }
687 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800688 case MACRONIX_ID:
David Hendricksac72e362010-08-16 18:20:03 -0700689 switch (flash->model_id) {
David Hendricksf8f00c72011-02-01 12:39:46 -0800690 case MACRONIX_MX25L1005:
691 *w25q_ranges = mx25l1005_ranges;
692 *num_entries = ARRAY_SIZE(mx25l1005_ranges);
693 break;
694 case MACRONIX_MX25L2005:
695 *w25q_ranges = mx25l2005_ranges;
696 *num_entries = ARRAY_SIZE(mx25l2005_ranges);
697 break;
698 case MACRONIX_MX25L4005:
699 *w25q_ranges = mx25l4005_ranges;
700 *num_entries = ARRAY_SIZE(mx25l4005_ranges);
701 break;
702 case MACRONIX_MX25L8005:
703 *w25q_ranges = mx25l8005_ranges;
704 *num_entries = ARRAY_SIZE(mx25l8005_ranges);
705 break;
706 case MACRONIX_MX25L1605:
707 /* FIXME: MX25L1605 and MX25L1605D have different write
708 * protection capabilities, but share IDs */
709 *w25q_ranges = mx25l1605d_ranges;
710 *num_entries = ARRAY_SIZE(mx25l1605d_ranges);
711 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800712 case MACRONIX_MX25L3205:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800713 *w25q_ranges = mx25l3205d_ranges;
714 *num_entries = ARRAY_SIZE(mx25l3205d_ranges);
David Hendricksac72e362010-08-16 18:20:03 -0700715 break;
Vincent Palatin87e092a2013-02-28 15:46:14 -0800716 case MACRONIX_MX25U3235E:
717 *w25q_ranges = mx25u3235e_ranges;
718 *num_entries = ARRAY_SIZE(mx25u3235e_ranges);
719 break;
Jongpil66a96492014-08-14 17:59:06 +0900720 case MACRONIX_MX25U6435E:
721 *w25q_ranges = mx25u6435e_ranges;
722 *num_entries = ARRAY_SIZE(mx25u6435e_ranges);
723 break;
David Hendricksac72e362010-08-16 18:20:03 -0700724 default:
725 msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)"
726 ", aborting\n", __func__, __LINE__,
727 flash->model_id);
728 return -1;
729 }
730 break;
David Hendricksbfa624b2012-07-24 12:47:59 -0700731 case ST_ID:
732 switch(flash->model_id) {
733 case ST_N25Q064__1E:
734 case ST_N25Q064__3E:
735 *w25q_ranges = n25q064_ranges;
736 *num_entries = ARRAY_SIZE(n25q064_ranges);
737 break;
738 default:
739 msg_cerr("%s() %d: Micron flash chip mismatch"
740 " (0x%04x), aborting\n", __func__, __LINE__,
741 flash->model_id);
742 return -1;
743 }
744 break;
Bryan Freed9a0051f2012-05-22 16:06:09 -0700745 case GIGADEVICE_ID:
746 switch(flash->model_id) {
747 case GIGADEVICE_GD25LQ32:
748 *w25q_ranges = w25q32_ranges;
749 *num_entries = ARRAY_SIZE(w25q32_ranges);
750 break;
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700751 case GIGADEVICE_GD25Q64:
Marc Jonesb18734f2014-04-03 16:19:47 -0600752 case GIGADEVICE_GD25LQ64:
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700753 *w25q_ranges = gd25q64_ranges;
754 *num_entries = ARRAY_SIZE(gd25q64_ranges);
755 break;
756 /* TODO(shawnn): add support for other GD parts */
Bryan Freed9a0051f2012-05-22 16:06:09 -0700757 default:
758 msg_cerr("%s() %d: GigaDevice flash chip mismatch"
759 " (0x%04x), aborting\n", __func__, __LINE__,
760 flash->model_id);
761 return -1;
762 }
763 break;
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +0800764 case AMIC_ID_NOPREFIX:
765 switch(flash->model_id) {
766 case AMIC_A25L040:
767 *w25q_ranges = a25l040_ranges;
768 *num_entries = ARRAY_SIZE(a25l040_ranges);
769 break;
770 default:
771 msg_cerr("%s() %d: AMIC flash chip mismatch"
772 " (0x%04x), aborting\n", __func__, __LINE__,
773 flash->model_id);
774 return -1;
775 }
776 break;
David Hendricksf7924d12010-06-10 21:26:44 -0700777 default:
David Hendricksd494b0a2010-08-16 16:28:50 -0700778 msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
779 __func__, flash->manufacture_id);
David Hendricksf7924d12010-06-10 21:26:44 -0700780 return -1;
781 }
782
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800783 return 0;
784}
785
786int w25_range_to_status(const struct flashchip *flash,
787 unsigned int start, unsigned int len,
788 struct w25q_status *status)
789{
790 struct w25q_range *w25q_ranges;
791 int i, range_found = 0;
792 int num_entries;
793
794 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
David Hendricksf7924d12010-06-10 21:26:44 -0700795 for (i = 0; i < num_entries; i++) {
796 struct wp_range *r = &w25q_ranges[i].range;
797
798 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
799 start, len, r->start, r->len);
800 if ((start == r->start) && (len == r->len)) {
David Hendricksd494b0a2010-08-16 16:28:50 -0700801 status->bp0 = w25q_ranges[i].bp & 1;
802 status->bp1 = w25q_ranges[i].bp >> 1;
803 status->bp2 = w25q_ranges[i].bp >> 2;
804 status->tb = w25q_ranges[i].tb;
805 status->sec = w25q_ranges[i].sec;
David Hendricksf7924d12010-06-10 21:26:44 -0700806
807 range_found = 1;
808 break;
809 }
810 }
811
812 if (!range_found) {
813 msg_cerr("matching range not found\n");
814 return -1;
815 }
David Hendricksd494b0a2010-08-16 16:28:50 -0700816 return 0;
817}
818
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800819int w25_status_to_range(const struct flashchip *flash,
820 const struct w25q_status *status,
821 unsigned int *start, unsigned int *len)
822{
823 struct w25q_range *w25q_ranges;
824 int i, status_found = 0;
825 int num_entries;
826
827 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
828 for (i = 0; i < num_entries; i++) {
829 int bp;
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +0800830 int table_bp, table_tb, table_sec;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800831
832 bp = status->bp0 | (status->bp1 << 1) | (status->bp2 << 2);
833 msg_cspew("comparing 0x%x 0x%x / 0x%x 0x%x / 0x%x 0x%x\n",
834 bp, w25q_ranges[i].bp,
835 status->tb, w25q_ranges[i].tb,
836 status->sec, w25q_ranges[i].sec);
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +0800837 table_bp = w25q_ranges[i].bp;
838 table_tb = w25q_ranges[i].tb;
839 table_sec = w25q_ranges[i].sec;
840 if ((bp == table_bp || table_bp == X) &&
841 (status->tb == table_tb || table_tb == X) &&
842 (status->sec == table_sec || table_sec == X)) {
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800843 *start = w25q_ranges[i].range.start;
844 *len = w25q_ranges[i].range.len;
845
846 status_found = 1;
847 break;
848 }
849 }
850
851 if (!status_found) {
852 msg_cerr("matching status not found\n");
853 return -1;
854 }
855 return 0;
856}
857
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800858/* Given a [start, len], this function calls w25_range_to_status() to convert
859 * it to flash-chip-specific range bits, then sets into status register.
860 */
David Hendricks91040832011-07-08 20:01:09 -0700861static int w25_set_range(const struct flashchip *flash,
David Hendricksd494b0a2010-08-16 16:28:50 -0700862 unsigned int start, unsigned int len)
863{
864 struct w25q_status status;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800865 int tmp = 0;
866 int expected = 0;
David Hendricksd494b0a2010-08-16 16:28:50 -0700867
868 memset(&status, 0, sizeof(status));
869 tmp = spi_read_status_register();
870 memcpy(&status, &tmp, 1);
871 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
872
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800873 if (w25_range_to_status(flash, start, len, &status)) return -1;
David Hendricksf7924d12010-06-10 21:26:44 -0700874
875 msg_cdbg("status.busy: %x\n", status.busy);
876 msg_cdbg("status.wel: %x\n", status.wel);
877 msg_cdbg("status.bp0: %x\n", status.bp0);
878 msg_cdbg("status.bp1: %x\n", status.bp1);
879 msg_cdbg("status.bp2: %x\n", status.bp2);
880 msg_cdbg("status.tb: %x\n", status.tb);
881 msg_cdbg("status.sec: %x\n", status.sec);
882 msg_cdbg("status.srp0: %x\n", status.srp0);
883
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800884 memcpy(&expected, &status, sizeof(status));
David Hendricks60824042014-12-11 17:22:06 -0800885 spi_write_status_register(flash, expected);
David Hendricksf7924d12010-06-10 21:26:44 -0700886
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800887 tmp = spi_read_status_register();
888 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
889 if ((tmp & MASK_WP_AREA) == (expected & MASK_WP_AREA)) {
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800890 return 0;
891 } else {
David Hendricksc801adb2010-12-09 16:58:56 -0800892 msg_cerr("expected=0x%02x, but actual=0x%02x.\n",
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800893 expected, tmp);
894 return 1;
895 }
David Hendricksf7924d12010-06-10 21:26:44 -0700896}
897
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800898/* Print out the current status register value with human-readable text. */
David Hendricks91040832011-07-08 20:01:09 -0700899static int w25_wp_status(const struct flashchip *flash)
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800900{
901 struct w25q_status status;
902 int tmp;
David Hendricksce8ded32010-10-08 11:23:38 -0700903 unsigned int start, len;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800904 int ret = 0;
905
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800906 memset(&status, 0, sizeof(status));
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800907 tmp = spi_read_status_register();
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800908 memcpy(&status, &tmp, 1);
909 msg_cinfo("WP: status: 0x%02x\n", tmp);
910 msg_cinfo("WP: status.srp0: %x\n", status.srp0);
911 msg_cinfo("WP: write protect is %s.\n",
912 status.srp0 ? "enabled" : "disabled");
913
914 msg_cinfo("WP: write protect range: ");
915 if (w25_status_to_range(flash, &status, &start, &len)) {
916 msg_cinfo("(cannot resolve the range)\n");
917 ret = -1;
918 } else {
919 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
920 }
921
922 return ret;
923}
924
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800925/* Set/clear the SRP0 bit in the status register. */
David Hendricks91040832011-07-08 20:01:09 -0700926static int w25_set_srp0(const struct flashchip *flash, int enable)
David Hendricksf7924d12010-06-10 21:26:44 -0700927{
928 struct w25q_status status;
929 int tmp = 0;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800930 int expected = 0;
David Hendricksf7924d12010-06-10 21:26:44 -0700931
932 memset(&status, 0, sizeof(status));
933 tmp = spi_read_status_register();
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800934 /* FIXME: this is NOT endian-free copy. */
David Hendricksf7924d12010-06-10 21:26:44 -0700935 memcpy(&status, &tmp, 1);
936 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
937
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800938 status.srp0 = enable ? 1 : 0;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800939 memcpy(&expected, &status, sizeof(status));
David Hendricks60824042014-12-11 17:22:06 -0800940 spi_write_status_register(flash, expected);
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800941
942 tmp = spi_read_status_register();
943 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
944 if ((tmp & MASK_WP_AREA) != (expected & MASK_WP_AREA))
945 return 1;
David Hendricksf7924d12010-06-10 21:26:44 -0700946
947 return 0;
948}
949
David Hendricks1c09f802012-10-03 11:03:48 -0700950static int w25_enable_writeprotect(const struct flashchip *flash,
951 enum wp_mode wp_mode)
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800952{
953 int ret;
954
David Hendricks1c09f802012-10-03 11:03:48 -0700955 switch (wp_mode) {
956 case WP_MODE_HARDWARE:
957 ret = w25_set_srp0(flash, 1);
958 break;
959 default:
960 msg_cerr("%s(): unsupported write-protect mode\n", __func__);
961 return 1;
962 }
963
David Hendricksc801adb2010-12-09 16:58:56 -0800964 if (ret)
965 msg_cerr("%s(): error=%d.\n", __func__, ret);
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800966 return ret;
967}
968
David Hendricks91040832011-07-08 20:01:09 -0700969static int w25_disable_writeprotect(const struct flashchip *flash)
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800970{
971 int ret;
972
973 ret = w25_set_srp0(flash, 0);
David Hendricksc801adb2010-12-09 16:58:56 -0800974 if (ret)
975 msg_cerr("%s(): error=%d.\n", __func__, ret);
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800976 return ret;
977}
978
David Hendricks91040832011-07-08 20:01:09 -0700979static int w25_list_ranges(const struct flashchip *flash)
David Hendricks0f7f5382011-02-11 18:12:31 -0800980{
981 struct w25q_range *w25q_ranges;
982 int i, num_entries;
983
984 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
985 for (i = 0; i < num_entries; i++) {
986 msg_cinfo("start: 0x%06x, length: 0x%06x\n",
987 w25q_ranges[i].range.start,
988 w25q_ranges[i].range.len);
989 }
990
991 return 0;
992}
993
David Hendricks1c09f802012-10-03 11:03:48 -0700994/* FIXME: Move to spi25.c if it's a JEDEC standard opcode */
995uint8_t w25q_read_status_register_2(void)
996{
997 static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { 0x35 };
998 unsigned char readarr[2];
999 int ret;
1000
1001 /* Read Status Register */
1002 ret = spi_send_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
1003 if (ret) {
1004 /*
1005 * FIXME: make this a benign failure for now in case we are
1006 * unable to execute the opcode
1007 */
1008 msg_cdbg("RDSR2 failed!\n");
1009 readarr[0] = 0x00;
1010 }
1011
1012 return readarr[0];
1013}
1014
1015static int w25q_wp_status(const struct flashchip *flash)
1016{
1017 struct w25q_status sr1;
1018 struct w25q_status_2 sr2;
David Hendricksf1bd8802012-10-30 11:37:57 -07001019 uint8_t tmp[2];
David Hendricks1c09f802012-10-03 11:03:48 -07001020 unsigned int start, len;
1021 int ret = 0;
1022
1023 memset(&sr1, 0, sizeof(sr1));
David Hendricksf1bd8802012-10-30 11:37:57 -07001024 tmp[0] = spi_read_status_register();
1025 memcpy(&sr1, &tmp[0], 1);
David Hendricks1c09f802012-10-03 11:03:48 -07001026
David Hendricksf1bd8802012-10-30 11:37:57 -07001027 memset(&sr2, 0, sizeof(sr2));
1028 tmp[1] = w25q_read_status_register_2();
1029 memcpy(&sr2, &tmp[1], 1);
1030
1031 msg_cinfo("WP: status: 0x%02x%02x\n", tmp[1], tmp[0]);
David Hendricks1c09f802012-10-03 11:03:48 -07001032 msg_cinfo("WP: status.srp0: %x\n", sr1.srp0);
1033 msg_cinfo("WP: status.srp1: %x\n", sr2.srp1);
1034 msg_cinfo("WP: write protect is %s.\n",
1035 (sr1.srp0 || sr2.srp1) ? "enabled" : "disabled");
1036
1037 msg_cinfo("WP: write protect range: ");
1038 if (w25_status_to_range(flash, &sr1, &start, &len)) {
1039 msg_cinfo("(cannot resolve the range)\n");
1040 ret = -1;
1041 } else {
1042 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
1043 }
1044
1045 return ret;
1046}
1047
1048/*
1049 * W25Q adds an optional byte to the standard WRSR opcode. If /CS is
1050 * de-asserted after the first byte, then it acts like a JEDEC-standard
1051 * WRSR command. if /CS is asserted, then the next data byte is written
1052 * into status register 2.
1053 */
1054#define W25Q_WRSR_OUTSIZE 0x03
1055static int w25q_write_status_register_WREN(uint8_t s1, uint8_t s2)
1056{
1057 int result;
1058 struct spi_command cmds[] = {
1059 {
1060 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
1061 .writecnt = JEDEC_WREN_OUTSIZE,
1062 .writearr = (const unsigned char[]){ JEDEC_WREN },
1063 .readcnt = 0,
1064 .readarr = NULL,
1065 }, {
1066 .writecnt = W25Q_WRSR_OUTSIZE,
1067 .writearr = (const unsigned char[]){ JEDEC_WRSR, s1, s2 },
1068 .readcnt = 0,
1069 .readarr = NULL,
1070 }, {
1071 .writecnt = 0,
1072 .writearr = NULL,
1073 .readcnt = 0,
1074 .readarr = NULL,
1075 }};
1076
1077 result = spi_send_multicommand(cmds);
1078 if (result) {
1079 msg_cerr("%s failed during command execution\n",
1080 __func__);
1081 }
1082
1083 /* WRSR performs a self-timed erase before the changes take effect. */
David Hendricks60824042014-12-11 17:22:06 -08001084 programmer_delay(100 * 1000);
David Hendricks1c09f802012-10-03 11:03:48 -07001085
1086 return result;
1087}
1088
1089/*
1090 * Set/clear the SRP1 bit in status register 2.
1091 * FIXME: make this more generic if other chips use the same SR2 layout
1092 */
1093static int w25q_set_srp1(const struct flashchip *flash, int enable)
1094{
1095 struct w25q_status sr1;
1096 struct w25q_status_2 sr2;
1097 uint8_t tmp, expected;
1098
1099 tmp = spi_read_status_register();
1100 memcpy(&sr1, &tmp, 1);
1101 tmp = w25q_read_status_register_2();
1102 memcpy(&sr2, &tmp, 1);
1103
1104 msg_cdbg("%s: old status 2: 0x%02x\n", __func__, tmp);
1105
1106 sr2.srp1 = enable ? 1 : 0;
1107
1108 memcpy(&expected, &sr2, 1);
1109 w25q_write_status_register_WREN(*((uint8_t *)&sr1), *((uint8_t *)&sr2));
1110
1111 tmp = w25q_read_status_register_2();
1112 msg_cdbg("%s: new status 2: 0x%02x\n", __func__, tmp);
1113 if ((tmp & MASK_WP2_AREA) != (expected & MASK_WP2_AREA))
1114 return 1;
1115
1116 return 0;
1117}
1118
1119enum wp_mode get_wp_mode(const char *mode_str)
1120{
1121 enum wp_mode wp_mode = WP_MODE_UNKNOWN;
1122
1123 if (!strcasecmp(mode_str, "hardware"))
1124 wp_mode = WP_MODE_HARDWARE;
1125 else if (!strcasecmp(mode_str, "power_cycle"))
1126 wp_mode = WP_MODE_POWER_CYCLE;
1127 else if (!strcasecmp(mode_str, "permanent"))
1128 wp_mode = WP_MODE_PERMANENT;
1129
1130 return wp_mode;
1131}
1132
1133static int w25q_disable_writeprotect(const struct flashchip *flash,
1134 enum wp_mode wp_mode)
1135{
1136 int ret = 1;
David Hendricks1c09f802012-10-03 11:03:48 -07001137 struct w25q_status_2 sr2;
1138 uint8_t tmp;
1139
1140 switch (wp_mode) {
1141 case WP_MODE_HARDWARE:
1142 ret = w25_set_srp0(flash, 0);
1143 break;
1144 case WP_MODE_POWER_CYCLE:
1145 tmp = w25q_read_status_register_2();
1146 memcpy(&sr2, &tmp, 1);
1147 if (sr2.srp1) {
1148 msg_cerr("%s(): must disconnect power to disable "
1149 "write-protection\n", __func__);
1150 } else {
1151 ret = 0;
1152 }
1153 break;
1154 case WP_MODE_PERMANENT:
1155 msg_cerr("%s(): cannot disable permanent write-protection\n",
1156 __func__);
1157 break;
1158 default:
1159 msg_cerr("%s(): invalid mode specified\n", __func__);
1160 break;
1161 }
1162
1163 if (ret)
1164 msg_cerr("%s(): error=%d.\n", __func__, ret);
1165 return ret;
1166}
1167
1168static int w25q_disable_writeprotect_default(const struct flashchip *flash)
1169{
1170 return w25q_disable_writeprotect(flash, WP_MODE_HARDWARE);
1171}
1172
1173static int w25q_enable_writeprotect(const struct flashchip *flash,
1174 enum wp_mode wp_mode)
1175{
1176 int ret = 1;
1177 struct w25q_status sr1;
1178 struct w25q_status_2 sr2;
1179 uint8_t tmp;
1180
1181 switch (wp_mode) {
1182 case WP_MODE_HARDWARE:
1183 if (w25q_disable_writeprotect(flash, WP_MODE_POWER_CYCLE)) {
1184 msg_cerr("%s(): cannot disable power cycle WP mode\n",
1185 __func__);
1186 break;
1187 }
1188
1189 tmp = spi_read_status_register();
1190 memcpy(&sr1, &tmp, 1);
1191 if (sr1.srp0)
1192 ret = 0;
1193 else
1194 ret = w25_set_srp0(flash, 1);
1195
1196 break;
1197 case WP_MODE_POWER_CYCLE:
1198 if (w25q_disable_writeprotect(flash, WP_MODE_HARDWARE)) {
1199 msg_cerr("%s(): cannot disable hardware WP mode\n",
1200 __func__);
1201 break;
1202 }
1203
1204 tmp = w25q_read_status_register_2();
1205 memcpy(&sr2, &tmp, 1);
1206 if (sr2.srp1)
1207 ret = 0;
1208 else
1209 ret = w25q_set_srp1(flash, 1);
1210
1211 break;
1212 case WP_MODE_PERMANENT:
1213 tmp = spi_read_status_register();
1214 memcpy(&sr1, &tmp, 1);
1215 if (sr1.srp0 == 0) {
1216 ret = w25_set_srp0(flash, 1);
1217 if (ret) {
David Hendricksf1bd8802012-10-30 11:37:57 -07001218 msg_perr("%s(): cannot enable SRP0 for "
David Hendricks1c09f802012-10-03 11:03:48 -07001219 "permanent WP\n", __func__);
1220 break;
1221 }
1222 }
1223
1224 tmp = w25q_read_status_register_2();
1225 memcpy(&sr2, &tmp, 1);
1226 if (sr2.srp1 == 0) {
1227 ret = w25q_set_srp1(flash, 1);
1228 if (ret) {
David Hendricksf1bd8802012-10-30 11:37:57 -07001229 msg_perr("%s(): cannot enable SRP1 for "
David Hendricks1c09f802012-10-03 11:03:48 -07001230 "permanent WP\n", __func__);
1231 break;
1232 }
1233 }
1234
1235 break;
David Hendricksf1bd8802012-10-30 11:37:57 -07001236 default:
1237 msg_perr("%s(): invalid mode %d\n", __func__, wp_mode);
1238 break;
David Hendricks1c09f802012-10-03 11:03:48 -07001239 }
1240
1241 if (ret)
1242 msg_cerr("%s(): error=%d.\n", __func__, ret);
1243 return ret;
1244}
1245
David Hendricksc3496092014-11-13 17:20:55 -08001246/* FIXME: Move to spi25.c if it's a JEDEC standard opcode */
1247uint8_t mx25l_read_config_register(void)
1248{
1249 static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { 0x15 };
1250 unsigned char readarr[2]; /* leave room for dummy byte */
1251 int ret;
1252
1253 ret = spi_send_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
1254 if (ret) {
1255 msg_cerr("RDCR failed!\n");
1256 readarr[0] = 0x00;
1257 }
1258
1259 return readarr[0];
1260}
David Hendricks1c09f802012-10-03 11:03:48 -07001261/* W25P, W25X, and many flash chips from various vendors */
David Hendricksf7924d12010-06-10 21:26:44 -07001262struct wp wp_w25 = {
David Hendricks0f7f5382011-02-11 18:12:31 -08001263 .list_ranges = w25_list_ranges,
David Hendricksf7924d12010-06-10 21:26:44 -07001264 .set_range = w25_set_range,
1265 .enable = w25_enable_writeprotect,
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001266 .disable = w25_disable_writeprotect,
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001267 .wp_status = w25_wp_status,
David Hendricks1c09f802012-10-03 11:03:48 -07001268
1269};
1270
1271/* W25Q series has features such as a second status register and SFDP */
1272struct wp wp_w25q = {
1273 .list_ranges = w25_list_ranges,
1274 .set_range = w25_set_range,
1275 .enable = w25q_enable_writeprotect,
1276 /*
1277 * By default, disable hardware write-protection. We may change
1278 * this later if we want to add fine-grained write-protect disable
1279 * as a command-line option.
1280 */
1281 .disable = w25q_disable_writeprotect_default,
1282 .wp_status = w25q_wp_status,
David Hendricksf7924d12010-06-10 21:26:44 -07001283};
David Hendrickse0512a72014-07-15 20:30:47 -07001284
David Hendricksaf3944a2014-07-28 18:37:40 -07001285struct generic_range gd25q32_cmp0_ranges[] = {
1286 /* none, bp4 and bp3 => don't care */
1287 { 0x00, {0, 0} },
1288 { 0x08, {0, 0} },
1289 { 0x10, {0, 0} },
1290 { 0x18, {0, 0} },
1291
1292 { 0x01, {0x3f0000, 64 * 1024} },
1293 { 0x02, {0x3e0000, 128 * 1024} },
1294 { 0x03, {0x3c0000, 256 * 1024} },
1295 { 0x04, {0x380000, 512 * 1024} },
1296 { 0x05, {0x300000, 1024 * 1024} },
1297 { 0x06, {0x200000, 2048 * 1024} },
1298
1299 { 0x09, {0x000000, 64 * 1024} },
1300 { 0x0a, {0x000000, 128 * 1024} },
1301 { 0x0b, {0x000000, 256 * 1024} },
1302 { 0x0c, {0x000000, 512 * 1024} },
1303 { 0x0d, {0x000000, 1024 * 1024} },
1304 { 0x0e, {0x000000, 2048 * 1024} },
1305
1306 /* all, bp4 and bp3 => don't care */
1307 { 0x07, {0x000000, 4096 * 1024} },
1308 { 0x0f, {0x000000, 4096 * 1024} },
1309 { 0x17, {0x000000, 4096 * 1024} },
1310 { 0x1f, {0x000000, 4096 * 1024} },
1311
1312 { 0x11, {0x3ff000, 4 * 1024} },
1313 { 0x12, {0x3fe000, 8 * 1024} },
1314 { 0x13, {0x3fc000, 16 * 1024} },
1315 { 0x14, {0x3f8000, 32 * 1024} }, /* bp0 => don't care */
1316 { 0x15, {0x3f8000, 32 * 1024} }, /* bp0 => don't care */
1317 { 0x16, {0x3f8000, 32 * 1024} },
1318
1319 { 0x19, {0x000000, 4 * 1024} },
1320 { 0x1a, {0x000000, 8 * 1024} },
1321 { 0x1b, {0x000000, 16 * 1024} },
1322 { 0x1c, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1323 { 0x1d, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1324 { 0x1e, {0x000000, 32 * 1024} },
1325};
1326
1327struct generic_range gd25q32_cmp1_ranges[] = {
1328 /* none, bp4 and bp3 => don't care */
1329 { 0x00, {0, 0} },
1330 { 0x08, {0, 0} },
1331 { 0x10, {0, 0} },
1332 { 0x18, {0, 0} },
1333
1334 { 0x01, {0x000000, 4032 * 1024} },
1335 { 0x02, {0x000000, 3968 * 1024} },
1336 { 0x03, {0x000000, 3840 * 1024} },
1337 { 0x04, {0x000000, 3584 * 1024} },
1338 { 0x05, {0x000000, 3 * 1024 * 1024} },
1339 { 0x06, {0x000000, 2 * 1024 * 1024} },
1340
1341 { 0x09, {0x010000, 4032 * 1024} },
1342 { 0x0a, {0x020000, 3968 * 1024} },
1343 { 0x0b, {0x040000, 3840 * 1024} },
1344 { 0x0c, {0x080000, 3584 * 1024} },
1345 { 0x0d, {0x100000, 3 * 1024 * 1024} },
1346 { 0x0e, {0x200000, 2 * 1024 * 1024} },
1347
1348 /* all, bp4 and bp3 => don't care */
1349 { 0x07, {0x000000, 4096 * 1024} },
1350 { 0x0f, {0x000000, 4096 * 1024} },
1351 { 0x17, {0x000000, 4096 * 1024} },
1352 { 0x1f, {0x000000, 4096 * 1024} },
1353
1354 { 0x11, {0x000000, 4092 * 1024} },
1355 { 0x12, {0x000000, 4088 * 1024} },
1356 { 0x13, {0x000000, 4080 * 1024} },
1357 { 0x14, {0x000000, 4064 * 1024} }, /* bp0 => don't care */
1358 { 0x15, {0x000000, 4064 * 1024} }, /* bp0 => don't care */
1359 { 0x16, {0x000000, 4064 * 1024} },
1360
1361 { 0x19, {0x001000, 4092 * 1024} },
1362 { 0x1a, {0x002000, 4088 * 1024} },
1363 { 0x1b, {0x040000, 4080 * 1024} },
1364 { 0x1c, {0x080000, 4064 * 1024} }, /* bp0 => don't care */
1365 { 0x1d, {0x080000, 4064 * 1024} }, /* bp0 => don't care */
1366 { 0x1e, {0x080000, 4064 * 1024} },
1367};
1368
1369static struct generic_wp gd25q32_wp = {
1370 /* TODO: map second status register */
1371 .sr1 = { .bp0_pos = 2, .bp_bits = 5, .srp_pos = 7 },
1372};
1373
David Hendricks83541d32014-07-15 20:58:21 -07001374#if 0
1375/* FIXME: MX25L6405D has same ID as MX25L6406 */
1376static struct w25q_range mx25l6405d_ranges[] = {
1377 { X, 0, 0, {0, 0} }, /* none */
1378 { X, 0, 0x1, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
1379 { X, 0, 0x2, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
1380 { X, 0, 0x3, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
1381 { X, 0, 0x4, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
1382 { X, 0, 0x5, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
1383 { X, 0, 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
1384 { X, 0, 0x7, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */
1385
1386 { X, 1, 0x0, {0x000000, 8192 * 1024} },
1387 { X, 1, 0x1, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
1388 { X, 1, 0x2, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */
1389 { X, 1, 0x3, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */
1390 { X, 1, 0x4, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */
1391 { X, 1, 0x5, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */
1392 { X, 1, 0x6, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */
1393 { X, 1, 0x7, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */
1394};
1395#endif
1396
1397/* FIXME: MX25L6406 has same ID as MX25L6405D */
1398struct generic_range mx25l6406e_ranges[] = {
1399 { 0, {0, 0} }, /* none */
1400 { 0x1, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */
1401 { 0x2, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */
1402 { 0x3, {0x7a0000, 64 * 8 * 1024} }, /* blocks 120-127 */
1403 { 0x4, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */
1404 { 0x5, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */
1405 { 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
1406
1407 { 0x7, {0x000000, 64 * 128 * 1024} }, /* all */
1408 { 0x8, {0x000000, 64 * 128 * 1024} }, /* all */
1409 { 0x9, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
1410 { 0xa, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */
1411 { 0xb, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */
1412 { 0xc, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */
1413 { 0xd, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */
1414 { 0xe, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */
1415 { 0xf, {0x000000, 64 * 128 * 1024} }, /* all */
1416};
1417
1418static struct generic_wp mx25l6406e_wp = {
1419 .sr1 = { .bp0_pos = 2, .bp_bits = 4, .srp_pos = 7 },
1420 .ranges = &mx25l6406e_ranges[0],
1421};
David Hendrickse0512a72014-07-15 20:30:47 -07001422
David Hendricksc3496092014-11-13 17:20:55 -08001423struct generic_range mx25l6495f_tb0_ranges[] = {
1424 { 0, {0, 0} }, /* none */
1425 { 0x1, {0x7f0000, 64 * 1 * 1024} }, /* block 127 */
1426 { 0x2, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */
1427 { 0x3, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */
1428
1429 { 0x4, {0x780000, 64 * 8 * 1024} }, /* blocks 120-127 */
1430 { 0x5, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */
1431 { 0x6, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */
1432 { 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
1433 { 0x8, {0x000000, 64 * 128 * 1024} }, /* all */
1434 { 0x9, {0x000000, 64 * 128 * 1024} }, /* all */
1435 { 0xa, {0x000000, 64 * 128 * 1024} }, /* all */
1436 { 0xb, {0x000000, 64 * 128 * 1024} }, /* all */
1437 { 0xc, {0x000000, 64 * 128 * 1024} }, /* all */
1438 { 0xd, {0x000000, 64 * 128 * 1024} }, /* all */
1439 { 0xe, {0x000000, 64 * 128 * 1024} }, /* all */
1440 { 0xf, {0x000000, 64 * 128 * 1024} }, /* all */
1441};
1442
1443struct generic_range mx25l6495f_tb1_ranges[] = {
1444 { 0, {0, 0} }, /* none */
1445 { 0x1, {0x000000, 64 * 1 * 1024} }, /* block 0 */
1446 { 0x2, {0x000000, 64 * 2 * 1024} }, /* blocks 0-1 */
1447 { 0x3, {0x000000, 64 * 4 * 1024} }, /* blocks 0-3 */
1448 { 0x4, {0x000000, 64 * 8 * 1024} }, /* blocks 0-7 */
1449 { 0x5, {0x000000, 64 * 16 * 1024} }, /* blocks 0-15 */
1450 { 0x6, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
1451 { 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
1452 { 0x8, {0x000000, 64 * 128 * 1024} }, /* all */
1453 { 0x9, {0x000000, 64 * 128 * 1024} }, /* all */
1454 { 0xa, {0x000000, 64 * 128 * 1024} }, /* all */
1455 { 0xb, {0x000000, 64 * 128 * 1024} }, /* all */
1456 { 0xc, {0x000000, 64 * 128 * 1024} }, /* all */
1457 { 0xd, {0x000000, 64 * 128 * 1024} }, /* all */
1458 { 0xe, {0x000000, 64 * 128 * 1024} }, /* all */
1459 { 0xf, {0x000000, 64 * 128 * 1024} }, /* all */
1460};
1461
1462static struct generic_wp mx25l6495f_wp = {
1463 .sr1 = { .bp0_pos = 2, .bp_bits = 4, .srp_pos = 7 },
1464};
1465
David Hendricksa9884852014-12-11 15:31:12 -08001466struct generic_range s25fs128s_tbprot_o_0_ranges[] = {
1467 { 0, {0, 0} }, /* none */
1468 { 0x1, {0xfc0000, 256 * 1024} }, /* upper 64th */
1469 { 0x2, {0xf80000, 512 * 1024} }, /* upper 32nd */
1470 { 0x3, {0xf00000, 1024 * 1024} }, /* upper 16th */
1471 { 0x4, {0xe00000, 2048 * 1024} }, /* upper 8th */
1472 { 0x5, {0xc00000, 4096 * 1024} }, /* upper 4th */
1473 { 0x6, {0x800000, 8192 * 1024} }, /* upper half */
1474 { 0x7, {0x000000, 16384 * 1024} }, /* all */
1475};
1476
1477struct generic_range s25fs128s_tbprot_o_1_ranges[] = {
1478 { 0, {0, 0} }, /* none */
1479 { 0x1, {0x000000, 256 * 1024} }, /* lower 64th */
1480 { 0x2, {0x000000, 512 * 1024} }, /* lower 32nd */
1481 { 0x3, {0x000000, 1024 * 1024} }, /* lower 16th */
1482 { 0x4, {0x000000, 2048 * 1024} }, /* lower 8th */
1483 { 0x5, {0x000000, 4096 * 1024} }, /* lower 4th */
1484 { 0x6, {0x000000, 8192 * 1024} }, /* lower half */
1485 { 0x7, {0x000000, 16384 * 1024} }, /* all */
1486};
1487
1488static struct generic_wp s25fs128s_wp = {
1489 .sr1 = { .bp0_pos = 2, .bp_bits = 3, .srp_pos = 7 },
1490};
1491
David Hendrickse0512a72014-07-15 20:30:47 -07001492/* Given a flash chip, this function returns its writeprotect info. */
1493static int generic_range_table(const struct flashchip *flash,
1494 struct generic_wp **wp,
1495 int *num_entries)
1496{
1497 *wp = NULL;
1498 *num_entries = 0;
1499
1500 switch (flash->manufacture_id) {
David Hendricksaf3944a2014-07-28 18:37:40 -07001501 case GIGADEVICE_ID:
1502 switch(flash->model_id) {
1503 case GIGADEVICE_GD25Q32: {
1504 uint8_t sr1 = w25q_read_status_register_2();
1505
1506 *wp = &gd25q32_wp;
1507 if (!(sr1 & (1 << 6))) { /* CMP == 0 */
1508 (*wp)->ranges = &gd25q32_cmp0_ranges[0];
1509 *num_entries = ARRAY_SIZE(gd25q32_cmp0_ranges);
1510 } else { /* CMP == 1 */
1511 (*wp)->ranges = &gd25q32_cmp1_ranges[0];
1512 *num_entries = ARRAY_SIZE(gd25q32_cmp1_ranges);
1513 }
1514
1515 break;
1516 /* TODO(shawnn): add support for other GD parts */
1517 }
1518 default:
1519 msg_cerr("%s() %d: GigaDevice flash chip mismatch"
1520 " (0x%04x), aborting\n", __func__, __LINE__,
1521 flash->model_id);
1522 return -1;
1523 }
1524 break;
David Hendricks83541d32014-07-15 20:58:21 -07001525 case MACRONIX_ID:
1526 switch (flash->model_id) {
1527 case MACRONIX_MX25L6405:
1528 /* FIXME: MX25L64* chips have mixed capabilities and
1529 share IDs */
1530 *wp = &mx25l6406e_wp;
1531 *num_entries = ARRAY_SIZE(mx25l6406e_ranges);
1532 break;
David Hendricksc3496092014-11-13 17:20:55 -08001533 case MACRONIX_MX25L6495F: {
1534 uint8_t cr = mx25l_read_config_register();
1535
1536 *wp = &mx25l6495f_wp;
1537 if (!(cr & (1 << 3))) { /* T/B == 0 */
1538 (*wp)->ranges = &mx25l6495f_tb0_ranges[0];
1539 *num_entries = ARRAY_SIZE(mx25l6495f_tb0_ranges);
1540 } else { /* T/B == 1 */
1541 (*wp)->ranges = &mx25l6495f_tb1_ranges[0];
1542 *num_entries = ARRAY_SIZE(mx25l6495f_tb1_ranges);
1543 }
1544 break;
1545 }
David Hendricks83541d32014-07-15 20:58:21 -07001546 default:
1547 msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)"
1548 ", aborting\n", __func__, __LINE__,
1549 flash->model_id);
1550 return -1;
1551 }
1552 break;
David Hendricksa9884852014-12-11 15:31:12 -08001553 case SPANSION_ID:
1554 switch (flash->model_id) {
1555 case SPANSION_S25FS128S_L:
1556 case SPANSION_S25FS128S_S: {
1557 int tbprot_o = s25fs_tbprot_o(flash);
1558
1559 if (tbprot_o < 0) {
1560 msg_cerr("%s(): Cannot determine top/bottom "
1561 "protection status.\n", __func__);
1562 return -1;
1563 }
1564
1565 *wp = &s25fs128s_wp;
1566 if (!tbprot_o) {
1567 (*wp)->ranges = s25fs128s_tbprot_o_0_ranges;
1568 *num_entries = ARRAY_SIZE(s25fs128s_tbprot_o_0_ranges);
1569 } else {
1570 (*wp)->ranges = s25fs128s_tbprot_o_1_ranges;
1571 *num_entries = ARRAY_SIZE(s25fs128s_tbprot_o_1_ranges);
1572 }
1573
1574 break;
1575 }
1576 default:
1577 msg_cerr("%s():%d Spansion flash chip mismatch (0x%04x)"
1578 ", aborting\n", __func__, __LINE__, flash->model_id);
1579 return -1;
1580 }
1581 break;
David Hendrickse0512a72014-07-15 20:30:47 -07001582 default:
1583 msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
1584 __func__, flash->manufacture_id);
1585 return -1;
1586 }
1587
1588 return 0;
1589}
1590
1591/* Given a [start, len], this function finds a block protect bit combination
1592 * (if possible) and sets the corresponding bits in "status". Remaining bits
1593 * are preserved. */
1594static int generic_range_to_status(const struct flashchip *flash,
1595 unsigned int start, unsigned int len,
1596 uint8_t *status)
1597{
1598 struct generic_wp *wp;
1599 struct generic_range *r;
1600 int i, range_found = 0, num_entries;
1601 uint8_t bp_mask;
1602
1603 if (generic_range_table(flash, &wp, &num_entries))
1604 return -1;
1605
1606 bp_mask = ((1 << (wp->sr1.bp0_pos + wp->sr1.bp_bits)) - 1) - \
1607 ((1 << wp->sr1.bp0_pos) - 1);
1608
1609 for (i = 0, r = &wp->ranges[0]; i < num_entries; i++, r++) {
1610 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
1611 start, len, r->range.start, r->range.len);
1612 if ((start == r->range.start) && (len == r->range.len)) {
1613 *status &= ~(bp_mask);
1614 *status |= r->bp << (wp->sr1.bp0_pos);
1615 range_found = 1;
1616 break;
1617 }
1618 }
1619
1620 if (!range_found) {
1621 msg_cerr("matching range not found\n");
1622 return -1;
1623 }
1624 return 0;
1625}
1626
1627static int generic_status_to_range(const struct flashchip *flash,
1628 const uint8_t sr1, unsigned int *start, unsigned int *len)
1629{
1630 struct generic_wp *wp;
1631 struct generic_range *r;
1632 int num_entries, wp_en, i, status_found = 0;
1633 uint8_t sr1_bp;
1634
1635 if (generic_range_table(flash, &wp, &num_entries))
1636 return -1;
1637
1638 sr1_bp = (sr1 >> wp->sr1.bp0_pos) & ((1 << wp->sr1.bp_bits) - 1);
1639
1640 for (i = 0, r = &wp->ranges[0]; i < num_entries; i++, r++) {
1641 msg_cspew("comparing 0x%02x 0x%02x\n", sr1_bp, r->bp);
1642 if (sr1_bp == r->bp) {
1643 *start = r->range.start;
1644 *len = r->range.len;
1645 status_found = 1;
1646 break;
1647 }
1648 }
1649
1650 if (!status_found) {
1651 msg_cerr("matching status not found\n");
1652 return -1;
1653 }
1654 return 0;
1655}
1656
1657/* Given a [start, len], this function calls generic_range_to_status() to
1658 * convert it to flash-chip-specific range bits, then sets into status register.
1659 */
1660static int generic_set_range(const struct flashchip *flash,
1661 unsigned int start, unsigned int len)
1662{
1663 uint8_t status, expected;
1664
1665 status = spi_read_status_register();
1666 msg_cdbg("%s: old status: 0x%02x\n", __func__, status);
1667
1668 expected = status; /* preserve non-bp bits */
1669 if (generic_range_to_status(flash, start, len, &expected))
1670 return -1;
1671
David Hendricks60824042014-12-11 17:22:06 -08001672 spi_write_status_register(flash, expected);
David Hendrickse0512a72014-07-15 20:30:47 -07001673
1674 status = spi_read_status_register();
1675 msg_cdbg("%s: new status: 0x%02x\n", __func__, status);
1676 if (status != expected) {
1677 msg_cerr("expected=0x%02x, but actual=0x%02x.\n",
1678 expected, status);
1679 return 1;
1680 }
1681
1682 return 0;
1683}
1684
1685/* Set/clear the status regsiter write protect bit in SR1. */
1686static int generic_set_srp0(const struct flashchip *flash, int enable)
1687{
1688 uint8_t status, expected;
1689 struct generic_wp *wp;
1690 int num_entries;
1691
1692 if (generic_range_table(flash, &wp, &num_entries))
1693 return -1;
1694
1695 expected = spi_read_status_register();
1696 msg_cdbg("%s: old status: 0x%02x\n", __func__, expected);
1697
1698 if (enable)
1699 expected |= 1 << wp->sr1.srp_pos;
1700 else
1701 expected &= ~(1 << wp->sr1.srp_pos);
1702
David Hendricks60824042014-12-11 17:22:06 -08001703 spi_write_status_register(flash, expected);
David Hendrickse0512a72014-07-15 20:30:47 -07001704
1705 status = spi_read_status_register();
1706 msg_cdbg("%s: new status: 0x%02x\n", __func__, status);
1707 if (status != expected)
1708 return -1;
1709
1710 return 0;
1711}
1712
1713static int generic_enable_writeprotect(const struct flashchip *flash,
1714 enum wp_mode wp_mode)
1715{
1716 int ret;
1717
1718 switch (wp_mode) {
1719 case WP_MODE_HARDWARE:
1720 ret = generic_set_srp0(flash, 1);
1721 break;
1722 default:
1723 msg_cerr("%s(): unsupported write-protect mode\n", __func__);
1724 return 1;
1725 }
1726
1727 if (ret)
1728 msg_cerr("%s(): error=%d.\n", __func__, ret);
1729 return ret;
1730}
1731
1732static int generic_disable_writeprotect(const struct flashchip *flash)
1733{
1734 int ret;
1735
1736 ret = generic_set_srp0(flash, 0);
1737 if (ret)
1738 msg_cerr("%s(): error=%d.\n", __func__, ret);
1739 return ret;
1740}
1741
1742static int generic_list_ranges(const struct flashchip *flash)
1743{
1744 struct generic_wp *wp;
1745 struct generic_range *r;
1746 int i, num_entries;
1747
1748 if (generic_range_table(flash, &wp, &num_entries))
1749 return -1;
1750
1751 r = &wp->ranges[0];
1752 for (i = 0; i < num_entries; i++) {
1753 msg_cinfo("start: 0x%06x, length: 0x%06x\n",
1754 r->range.start, r->range.len);
1755 r++;
1756 }
1757
1758 return 0;
1759}
1760
1761static int generic_wp_status(const struct flashchip *flash)
1762{
1763 uint8_t sr1;
1764 unsigned int start, len;
1765 int ret = 0;
1766 struct generic_wp *wp;
1767 struct generic_range *g;
1768 int num_entries, wp_en;
1769
1770 if (generic_range_table(flash, &wp, &num_entries))
1771 return -1;
1772
1773 sr1 = spi_read_status_register();
1774 wp_en = (sr1 >> wp->sr1.srp_pos) & 1;
1775
1776 msg_cinfo("WP: status: 0x%04x\n", sr1);
1777 msg_cinfo("WP: status.srp0: %x\n", wp_en);
1778 /* FIXME: SRP1 is not really generic, but we probably should print
1779 * it anyway to have consistent output. #legacycruft */
1780 msg_cinfo("WP: status.srp1: %x\n", 0);
1781 msg_cinfo("WP: write protect is %s.\n",
1782 wp_en ? "enabled" : "disabled");
1783
1784 msg_cinfo("WP: write protect range: ");
1785 if (generic_status_to_range(flash, sr1, &start, &len)) {
1786 msg_cinfo("(cannot resolve the range)\n");
1787 ret = -1;
1788 } else {
1789 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
1790 }
1791
1792 return ret;
1793}
1794
1795struct wp wp_generic = {
1796 .list_ranges = generic_list_ranges,
1797 .set_range = generic_set_range,
1798 .enable = generic_enable_writeprotect,
1799 .disable = generic_disable_writeprotect,
1800 .wp_status = generic_wp_status,
1801};