David Hendricks | d1c55d7 | 2010-08-24 15:14:19 -0700 | [diff] [blame] | 1 | /* |
| 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 Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | |
| 24 | #include "flash.h" |
| 25 | #include "flashchips.h" |
| 26 | #include "chipdrivers.h" |
Louis Yung-Chieh Lo | 52aa930 | 2010-09-06 10:45:02 +0800 | [diff] [blame] | 27 | #include "spi.h" |
David Hendricks | 23cd778 | 2010-08-25 12:42:38 -0700 | [diff] [blame] | 28 | #include "writeprotect.h" |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 29 | |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 30 | /* |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 31 | * 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 Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 35 | * be stupid and simple since clever arithmetic will not work for many chips. |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 36 | */ |
| 37 | |
| 38 | struct wp_range { |
| 39 | unsigned int start; /* starting address */ |
| 40 | unsigned int len; /* len */ |
| 41 | }; |
| 42 | |
| 43 | enum bit_state { |
| 44 | OFF = 0, |
| 45 | ON = 1, |
Louis Yung-Chieh Lo | edd3930 | 2011-11-10 15:43:06 +0800 | [diff] [blame] | 46 | X = -1 /* don't care. Must be bigger than max # of bp. */ |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 49 | /* |
| 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 | |
| 55 | struct 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 | |
| 61 | struct generic_range { |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 62 | struct generic_modifier_bits m; |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 63 | unsigned int bp; /* block protect bitfield */ |
| 64 | struct wp_range range; |
| 65 | }; |
| 66 | |
| 67 | struct generic_wp { |
| 68 | struct status_register_layout sr1; /* status register 1 */ |
| 69 | struct generic_range *ranges; |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 70 | |
| 71 | /* |
| 72 | * Some chips store modifier bits in one or more special control |
| 73 | * registers instead of the status register like many older SPI NOR |
| 74 | * flash chips did. get_modifier_bits() and set_modifier_bits() will do |
| 75 | * any chip-specific operations necessary to get/set these bit values. |
| 76 | */ |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 77 | int (*get_modifier_bits)(const struct flashctx *flash, |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 78 | struct generic_modifier_bits *m); |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 79 | int (*set_modifier_bits)(const struct flashctx *flash, |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 80 | struct generic_modifier_bits *m); |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | /* |
| 84 | * The following ranges and functions are useful for representing Winbond- |
| 85 | * style writeprotect schema in which there are typically 5 bits of |
| 86 | * relevant information stored in status register 1: |
| 87 | * sec: This bit indicates the units (sectors vs. blocks) |
| 88 | * tb: The top-bottom bit indicates if the affected range is at the top of |
| 89 | * the flash memory's address space or at the bottom. |
| 90 | * bp[2:0]: The number of affected sectors/blocks. |
| 91 | */ |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 92 | struct w25q_range { |
| 93 | enum bit_state sec; /* if 1, bp[2:0] describe sectors */ |
| 94 | enum bit_state tb; /* top/bottom select */ |
Louis Yung-Chieh Lo | edd3930 | 2011-11-10 15:43:06 +0800 | [diff] [blame] | 95 | int bp; /* block protect bitfield */ |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 96 | struct wp_range range; |
| 97 | }; |
| 98 | |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 99 | /* |
| 100 | * Mask to extract write-protect enable and range bits |
| 101 | * Status register 1: |
| 102 | * SRP0: bit 7 |
| 103 | * range(BP2-BP0): bit 4-2 |
| 104 | * Status register 2: |
| 105 | * SRP1: bit 1 |
| 106 | */ |
| 107 | #define MASK_WP_AREA (0x9C) |
| 108 | #define MASK_WP2_AREA (0x01) |
| 109 | |
David Hendricks | 57566ed | 2010-08-16 18:24:45 -0700 | [diff] [blame] | 110 | struct w25q_range en25f40_ranges[] = { |
| 111 | { X, X, 0, {0, 0} }, /* none */ |
| 112 | { 0, 0, 0x1, {0x000000, 504 * 1024} }, |
| 113 | { 0, 0, 0x2, {0x000000, 496 * 1024} }, |
| 114 | { 0, 0, 0x3, {0x000000, 480 * 1024} }, |
| 115 | { 0, 0, 0x4, {0x000000, 448 * 1024} }, |
| 116 | { 0, 0, 0x5, {0x000000, 384 * 1024} }, |
| 117 | { 0, 0, 0x6, {0x000000, 256 * 1024} }, |
| 118 | { 0, 0, 0x7, {0x000000, 512 * 1024} }, |
| 119 | }; |
| 120 | |
David Hendricks | e185bf2 | 2011-05-24 15:34:18 -0700 | [diff] [blame] | 121 | struct w25q_range en25q40_ranges[] = { |
| 122 | { 0, 0, 0, {0, 0} }, /* none */ |
| 123 | { 0, 0, 0x1, {0x000000, 504 * 1024} }, |
| 124 | { 0, 0, 0x2, {0x000000, 496 * 1024} }, |
| 125 | { 0, 0, 0x3, {0x000000, 480 * 1024} }, |
| 126 | |
| 127 | { 0, 1, 0x0, {0x000000, 448 * 1024} }, |
| 128 | { 0, 1, 0x1, {0x000000, 384 * 1024} }, |
| 129 | { 0, 1, 0x2, {0x000000, 256 * 1024} }, |
| 130 | { 0, 1, 0x3, {0x000000, 512 * 1024} }, |
| 131 | }; |
| 132 | |
| 133 | struct w25q_range en25q80_ranges[] = { |
| 134 | { 0, 0, 0, {0, 0} }, /* none */ |
| 135 | { 0, 0, 0x1, {0x000000, 1016 * 1024} }, |
| 136 | { 0, 0, 0x2, {0x000000, 1008 * 1024} }, |
| 137 | { 0, 0, 0x3, {0x000000, 992 * 1024} }, |
| 138 | { 0, 0, 0x4, {0x000000, 960 * 1024} }, |
| 139 | { 0, 0, 0x5, {0x000000, 896 * 1024} }, |
| 140 | { 0, 0, 0x6, {0x000000, 768 * 1024} }, |
| 141 | { 0, 0, 0x7, {0x000000, 1024 * 1024} }, |
| 142 | }; |
| 143 | |
| 144 | struct w25q_range en25q32_ranges[] = { |
| 145 | { 0, 0, 0, {0, 0} }, /* none */ |
| 146 | { 0, 0, 0x1, {0x000000, 4032 * 1024} }, |
| 147 | { 0, 0, 0x2, {0x000000, 3968 * 1024} }, |
| 148 | { 0, 0, 0x3, {0x000000, 3840 * 1024} }, |
| 149 | { 0, 0, 0x4, {0x000000, 3584 * 1024} }, |
| 150 | { 0, 0, 0x5, {0x000000, 3072 * 1024} }, |
| 151 | { 0, 0, 0x6, {0x000000, 2048 * 1024} }, |
| 152 | { 0, 0, 0x7, {0x000000, 4096 * 1024} }, |
| 153 | |
| 154 | { 0, 1, 0, {0, 0} }, /* none */ |
| 155 | { 0, 1, 0x1, {0x010000, 4032 * 1024} }, |
| 156 | { 0, 1, 0x2, {0x020000, 3968 * 1024} }, |
| 157 | { 0, 1, 0x3, {0x040000, 3840 * 1024} }, |
| 158 | { 0, 1, 0x4, {0x080000, 3584 * 1024} }, |
| 159 | { 0, 1, 0x5, {0x100000, 3072 * 1024} }, |
| 160 | { 0, 1, 0x6, {0x200000, 2048 * 1024} }, |
| 161 | { 0, 1, 0x7, {0x000000, 4096 * 1024} }, |
| 162 | }; |
| 163 | |
| 164 | struct w25q_range en25q64_ranges[] = { |
| 165 | { 0, 0, 0, {0, 0} }, /* none */ |
| 166 | { 0, 0, 0x1, {0x000000, 8128 * 1024} }, |
| 167 | { 0, 0, 0x2, {0x000000, 8064 * 1024} }, |
| 168 | { 0, 0, 0x3, {0x000000, 7936 * 1024} }, |
| 169 | { 0, 0, 0x4, {0x000000, 7680 * 1024} }, |
| 170 | { 0, 0, 0x5, {0x000000, 7168 * 1024} }, |
| 171 | { 0, 0, 0x6, {0x000000, 6144 * 1024} }, |
| 172 | { 0, 0, 0x7, {0x000000, 8192 * 1024} }, |
| 173 | |
| 174 | { 0, 1, 0, {0, 0} }, /* none */ |
| 175 | { 0, 1, 0x1, {0x010000, 8128 * 1024} }, |
| 176 | { 0, 1, 0x2, {0x020000, 8064 * 1024} }, |
| 177 | { 0, 1, 0x3, {0x040000, 7936 * 1024} }, |
| 178 | { 0, 1, 0x4, {0x080000, 7680 * 1024} }, |
| 179 | { 0, 1, 0x5, {0x100000, 7168 * 1024} }, |
| 180 | { 0, 1, 0x6, {0x200000, 6144 * 1024} }, |
| 181 | { 0, 1, 0x7, {0x000000, 8192 * 1024} }, |
| 182 | }; |
| 183 | |
| 184 | struct w25q_range en25q128_ranges[] = { |
| 185 | { 0, 0, 0, {0, 0} }, /* none */ |
| 186 | { 0, 0, 0x1, {0x000000, 16320 * 1024} }, |
| 187 | { 0, 0, 0x2, {0x000000, 16256 * 1024} }, |
| 188 | { 0, 0, 0x3, {0x000000, 16128 * 1024} }, |
| 189 | { 0, 0, 0x4, {0x000000, 15872 * 1024} }, |
| 190 | { 0, 0, 0x5, {0x000000, 15360 * 1024} }, |
| 191 | { 0, 0, 0x6, {0x000000, 14336 * 1024} }, |
| 192 | { 0, 0, 0x7, {0x000000, 16384 * 1024} }, |
| 193 | |
| 194 | { 0, 1, 0, {0, 0} }, /* none */ |
| 195 | { 0, 1, 0x1, {0x010000, 16320 * 1024} }, |
| 196 | { 0, 1, 0x2, {0x020000, 16256 * 1024} }, |
| 197 | { 0, 1, 0x3, {0x040000, 16128 * 1024} }, |
| 198 | { 0, 1, 0x4, {0x080000, 15872 * 1024} }, |
| 199 | { 0, 1, 0x5, {0x100000, 15360 * 1024} }, |
| 200 | { 0, 1, 0x6, {0x200000, 14336 * 1024} }, |
| 201 | { 0, 1, 0x7, {0x000000, 16384 * 1024} }, |
| 202 | }; |
| 203 | |
Marc Jones | b2f9002 | 2014-04-29 17:37:23 -0600 | [diff] [blame] | 204 | struct w25q_range en25s64_ranges[] = { |
| 205 | { 0, 0, 0, {0, 0} }, /* none */ |
| 206 | { 0, 0, 0x1, {0x000000, 8064 * 1024} }, |
| 207 | { 0, 0, 0x2, {0x000000, 7936 * 1024} }, |
| 208 | { 0, 0, 0x3, {0x000000, 7680 * 1024} }, |
| 209 | { 0, 0, 0x4, {0x000000, 7168 * 1024} }, |
| 210 | { 0, 0, 0x5, {0x000000, 6144 * 1024} }, |
| 211 | { 0, 0, 0x6, {0x000000, 4096 * 1024} }, |
| 212 | { 0, 0, 0x7, {0x000000, 8192 * 1024} }, |
| 213 | |
| 214 | { 0, 1, 0, {0, 0} }, /* none */ |
| 215 | { 0, 1, 0x1, {0x7e0000, 128 * 1024} }, |
| 216 | { 0, 1, 0x2, {0x7c0000, 256 * 1024} }, |
| 217 | { 0, 1, 0x3, {0x780000, 512 * 1024} }, |
| 218 | { 0, 1, 0x4, {0x700000, 1024 * 1024} }, |
| 219 | { 0, 1, 0x5, {0x600000, 2048 * 1024} }, |
| 220 | { 0, 1, 0x6, {0x400000, 4096 * 1024} }, |
| 221 | { 0, 1, 0x7, {0x000000, 8192 * 1024} }, |
| 222 | }; |
| 223 | |
David Hendricks | f8f00c7 | 2011-02-01 12:39:46 -0800 | [diff] [blame] | 224 | /* mx25l1005 ranges also work for the mx25l1005c */ |
| 225 | static struct w25q_range mx25l1005_ranges[] = { |
| 226 | { X, X, 0, {0, 0} }, /* none */ |
| 227 | { X, X, 0x1, {0x010000, 64 * 1024} }, |
| 228 | { X, X, 0x2, {0x000000, 128 * 1024} }, |
| 229 | { X, X, 0x3, {0x000000, 128 * 1024} }, |
| 230 | }; |
| 231 | |
| 232 | static struct w25q_range mx25l2005_ranges[] = { |
| 233 | { X, X, 0, {0, 0} }, /* none */ |
| 234 | { X, X, 0x1, {0x030000, 64 * 1024} }, |
| 235 | { X, X, 0x2, {0x020000, 128 * 1024} }, |
| 236 | { X, X, 0x3, {0x000000, 256 * 1024} }, |
| 237 | }; |
| 238 | |
| 239 | static struct w25q_range mx25l4005_ranges[] = { |
| 240 | { X, X, 0, {0, 0} }, /* none */ |
| 241 | { X, X, 0x1, {0x070000, 64 * 1 * 1024} }, /* block 7 */ |
| 242 | { X, X, 0x2, {0x060000, 64 * 2 * 1024} }, /* blocks 6-7 */ |
| 243 | { X, X, 0x3, {0x040000, 64 * 4 * 1024} }, /* blocks 4-7 */ |
| 244 | { X, X, 0x4, {0x000000, 512 * 1024} }, |
| 245 | { X, X, 0x5, {0x000000, 512 * 1024} }, |
| 246 | { X, X, 0x6, {0x000000, 512 * 1024} }, |
| 247 | { X, X, 0x7, {0x000000, 512 * 1024} }, |
| 248 | }; |
| 249 | |
| 250 | static struct w25q_range mx25l8005_ranges[] = { |
| 251 | { X, X, 0, {0, 0} }, /* none */ |
| 252 | { X, X, 0x1, {0x0f0000, 64 * 1 * 1024} }, /* block 15 */ |
| 253 | { X, X, 0x2, {0x0e0000, 64 * 2 * 1024} }, /* blocks 14-15 */ |
| 254 | { X, X, 0x3, {0x0c0000, 64 * 4 * 1024} }, /* blocks 12-15 */ |
| 255 | { X, X, 0x4, {0x080000, 64 * 8 * 1024} }, /* blocks 8-15 */ |
| 256 | { X, X, 0x5, {0x000000, 1024 * 1024} }, |
| 257 | { X, X, 0x6, {0x000000, 1024 * 1024} }, |
| 258 | { X, X, 0x7, {0x000000, 1024 * 1024} }, |
| 259 | }; |
| 260 | |
| 261 | #if 0 |
| 262 | /* FIXME: mx25l1605 has the same IDs as the mx25l1605d */ |
| 263 | static struct w25q_range mx25l1605_ranges[] = { |
| 264 | { X, X, 0, {0, 0} }, /* none */ |
| 265 | { X, X, 0x1, {0x1f0000, 64 * 1024} }, /* block 31 */ |
| 266 | { X, X, 0x2, {0x1e0000, 128 * 1024} }, /* blocks 30-31 */ |
| 267 | { X, X, 0x3, {0x1c0000, 256 * 1024} }, /* blocks 28-31 */ |
| 268 | { X, X, 0x4, {0x180000, 512 * 1024} }, /* blocks 24-31 */ |
| 269 | { X, X, 0x4, {0x100000, 1024 * 1024} }, /* blocks 16-31 */ |
| 270 | { X, X, 0x6, {0x000000, 2048 * 1024} }, |
| 271 | { X, X, 0x7, {0x000000, 2048 * 1024} }, |
| 272 | }; |
| 273 | #endif |
| 274 | |
| 275 | #if 0 |
| 276 | /* FIXME: mx25l6405 has the same IDs as the mx25l6405d */ |
| 277 | static struct w25q_range mx25l6405_ranges[] = { |
| 278 | { X, 0, 0, {0, 0} }, /* none */ |
| 279 | { X, 0, 0x1, {0x7f0000, 64 * 1 * 1024} }, /* block 127 */ |
| 280 | { X, 0, 0x2, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */ |
| 281 | { X, 0, 0x3, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */ |
| 282 | { X, 0, 0x4, {0x780000, 64 * 8 * 1024} }, /* blocks 120-127 */ |
| 283 | { X, 0, 0x5, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */ |
| 284 | { X, 0, 0x6, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */ |
| 285 | { X, 0, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */ |
| 286 | |
| 287 | { X, 1, 0x0, {0x000000, 8192 * 1024} }, |
| 288 | { X, 1, 0x1, {0x000000, 8192 * 1024} }, |
| 289 | { X, 1, 0x2, {0x000000, 8192 * 1024} }, |
| 290 | { X, 1, 0x3, {0x000000, 8192 * 1024} }, |
| 291 | { X, 1, 0x4, {0x000000, 8192 * 1024} }, |
| 292 | { X, 1, 0x5, {0x000000, 8192 * 1024} }, |
| 293 | { X, 1, 0x6, {0x000000, 8192 * 1024} }, |
| 294 | { X, 1, 0x7, {0x000000, 8192 * 1024} }, |
| 295 | }; |
| 296 | #endif |
| 297 | |
| 298 | static struct w25q_range mx25l1605d_ranges[] = { |
| 299 | { X, 0, 0, {0, 0} }, /* none */ |
| 300 | { X, 0, 0x1, {0x1f0000, 64 * 1 * 1024} }, /* block 31 */ |
| 301 | { X, 0, 0x2, {0x1e0000, 64 * 2 * 1024} }, /* blocks 30-31 */ |
| 302 | { X, 0, 0x3, {0x1c0000, 64 * 4 * 1024} }, /* blocks 28-31 */ |
| 303 | { X, 0, 0x4, {0x180000, 64 * 8 * 1024} }, /* blocks 24-31 */ |
| 304 | { X, 0, 0x5, {0x100000, 64 * 16 * 1024} }, /* blocks 16-31 */ |
| 305 | { X, 0, 0x6, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */ |
| 306 | { X, 0, 0x7, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */ |
| 307 | |
| 308 | { X, 1, 0x0, {0x000000, 2048 * 1024} }, |
| 309 | { X, 1, 0x1, {0x000000, 2048 * 1024} }, |
| 310 | { X, 1, 0x2, {0x000000, 64 * 16 * 1024} }, /* blocks 0-15 */ |
| 311 | { X, 1, 0x3, {0x000000, 64 * 24 * 1024} }, /* blocks 0-23 */ |
| 312 | { X, 1, 0x4, {0x000000, 64 * 28 * 1024} }, /* blocks 0-27 */ |
| 313 | { X, 1, 0x5, {0x000000, 64 * 30 * 1024} }, /* blocks 0-29 */ |
| 314 | { X, 1, 0x6, {0x000000, 64 * 31 * 1024} }, /* blocks 0-30 */ |
| 315 | { X, 1, 0x7, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */ |
| 316 | }; |
| 317 | |
| 318 | /* FIXME: Is there an mx25l3205 (without a trailing letter)? */ |
David Hendricks | ac72e36 | 2010-08-16 18:20:03 -0700 | [diff] [blame] | 319 | static struct w25q_range mx25l3205d_ranges[] = { |
| 320 | { X, 0, 0, {0, 0} }, /* none */ |
| 321 | { X, 0, 0x1, {0x3f0000, 64 * 1024} }, |
| 322 | { X, 0, 0x2, {0x3e0000, 128 * 1024} }, |
| 323 | { X, 0, 0x3, {0x3c0000, 256 * 1024} }, |
| 324 | { X, 0, 0x4, {0x380000, 512 * 1024} }, |
| 325 | { X, 0, 0x5, {0x300000, 1024 * 1024} }, |
| 326 | { X, 0, 0x6, {0x200000, 2048 * 1024} }, |
| 327 | { X, 0, 0x7, {0x000000, 4096 * 1024} }, |
| 328 | |
| 329 | { X, 1, 0x0, {0x000000, 4096 * 1024} }, |
| 330 | { X, 1, 0x1, {0x000000, 2048 * 1024} }, |
| 331 | { X, 1, 0x2, {0x000000, 3072 * 1024} }, |
| 332 | { X, 1, 0x3, {0x000000, 3584 * 1024} }, |
| 333 | { X, 1, 0x4, {0x000000, 3840 * 1024} }, |
| 334 | { X, 1, 0x5, {0x000000, 3968 * 1024} }, |
| 335 | { X, 1, 0x6, {0x000000, 4032 * 1024} }, |
| 336 | { X, 1, 0x7, {0x000000, 4096 * 1024} }, |
| 337 | }; |
| 338 | |
Vincent Palatin | 87e092a | 2013-02-28 15:46:14 -0800 | [diff] [blame] | 339 | static struct w25q_range mx25u3235e_ranges[] = { |
| 340 | { X, 0, 0, {0, 0} }, /* none */ |
| 341 | { 0, 0, 0x1, {0x3f0000, 64 * 1024} }, |
| 342 | { 0, 0, 0x2, {0x3e0000, 128 * 1024} }, |
| 343 | { 0, 0, 0x3, {0x3c0000, 256 * 1024} }, |
| 344 | { 0, 0, 0x4, {0x380000, 512 * 1024} }, |
| 345 | { 0, 0, 0x5, {0x300000, 1024 * 1024} }, |
| 346 | { 0, 0, 0x6, {0x200000, 2048 * 1024} }, |
| 347 | { 0, 0, 0x7, {0x000000, 4096 * 1024} }, |
| 348 | |
| 349 | { 0, 1, 0x0, {0x000000, 4096 * 1024} }, |
| 350 | { 0, 1, 0x1, {0x000000, 2048 * 1024} }, |
| 351 | { 0, 1, 0x2, {0x000000, 3072 * 1024} }, |
| 352 | { 0, 1, 0x3, {0x000000, 3584 * 1024} }, |
| 353 | { 0, 1, 0x4, {0x000000, 3840 * 1024} }, |
| 354 | { 0, 1, 0x5, {0x000000, 3968 * 1024} }, |
| 355 | { 0, 1, 0x6, {0x000000, 4032 * 1024} }, |
| 356 | { 0, 1, 0x7, {0x000000, 4096 * 1024} }, |
| 357 | }; |
| 358 | |
Jongpil | 66a9649 | 2014-08-14 17:59:06 +0900 | [diff] [blame] | 359 | static struct w25q_range mx25u6435e_ranges[] = { |
| 360 | { X, 0, 0, {0, 0} }, /* none */ |
| 361 | { 0, 0, 0x1, {0x7f0000, 1 * 64 * 1024} }, /* block 127 */ |
| 362 | { 0, 0, 0x2, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */ |
| 363 | { 0, 0, 0x3, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */ |
| 364 | { 0, 0, 0x4, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */ |
| 365 | { 0, 0, 0x5, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */ |
| 366 | { 0, 0, 0x6, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */ |
| 367 | { 0, 0, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */ |
| 368 | |
| 369 | { 0, 1, 0x0, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */ |
| 370 | { 0, 1, 0x1, {0x000000, 96 * 64 * 1024} }, /* blocks 0-95 */ |
| 371 | { 0, 1, 0x2, {0x000000, 112 * 64 * 1024} }, /* blocks 0-111 */ |
| 372 | { 0, 1, 0x3, {0x000000, 120 * 64 * 1024} }, /* blocks 0-119 */ |
| 373 | { 0, 1, 0x4, {0x000000, 124 * 64 * 1024} }, /* blocks 0-123 */ |
| 374 | { 0, 1, 0x5, {0x000000, 126 * 64 * 1024} }, /* blocks 0-125 */ |
| 375 | { 0, 1, 0x6, {0x000000, 127 * 64 * 1024} }, /* blocks 0-126 */ |
| 376 | { 0, 1, 0x7, {0x000000, 128 * 64 * 1024} }, /* blocks 0-127 */ |
| 377 | }; |
| 378 | |
David Hendricks | bfa624b | 2012-07-24 12:47:59 -0700 | [diff] [blame] | 379 | static struct w25q_range n25q064_ranges[] = { |
David Hendricks | fe9123b | 2015-04-21 13:18:31 -0700 | [diff] [blame] | 380 | /* |
| 381 | * Note: For N25Q064, sec (usually in bit position 6) is called BP3 |
| 382 | * (block protect bit 3). It is only useful when all blocks are to |
| 383 | * be write-protected. |
| 384 | */ |
David Hendricks | 42a549a | 2015-04-22 11:25:07 -0700 | [diff] [blame] | 385 | { 0, 0, 0, {0, 0} }, /* none */ |
David Hendricks | bfa624b | 2012-07-24 12:47:59 -0700 | [diff] [blame] | 386 | |
| 387 | { 0, 0, 0x1, {0x7f0000, 64 * 1024} }, /* block 127 */ |
| 388 | { 0, 0, 0x2, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */ |
| 389 | { 0, 0, 0x3, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */ |
| 390 | { 0, 0, 0x4, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */ |
| 391 | { 0, 0, 0x5, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */ |
| 392 | { 0, 0, 0x6, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */ |
| 393 | { 0, 0, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */ |
| 394 | |
David Hendricks | fe9123b | 2015-04-21 13:18:31 -0700 | [diff] [blame] | 395 | { 0, 1, 0x1, {0x000000, 64 * 1024} }, /* block 0 */ |
| 396 | { 0, 1, 0x2, {0x000000, 2 * 64 * 1024} }, /* blocks 0-1 */ |
| 397 | { 0, 1, 0x3, {0x000000, 4 * 64 * 1024} }, /* blocks 0-3 */ |
| 398 | { 0, 1, 0x4, {0x000000, 8 * 64 * 1024} }, /* blocks 0-7 */ |
| 399 | { 0, 1, 0x5, {0x000000, 16 * 64 * 1024} }, /* blocks 0-15 */ |
| 400 | { 0, 1, 0x6, {0x000000, 32 * 64 * 1024} }, /* blocks 0-31 */ |
| 401 | { 0, 1, 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */ |
David Hendricks | bfa624b | 2012-07-24 12:47:59 -0700 | [diff] [blame] | 402 | |
| 403 | { X, 1, 0x0, {0x000000, 128 * 64 * 1024} }, /* all */ |
| 404 | { X, 1, 0x1, {0x000000, 128 * 64 * 1024} }, /* all */ |
| 405 | { X, 1, 0x2, {0x000000, 128 * 64 * 1024} }, /* all */ |
| 406 | { X, 1, 0x3, {0x000000, 128 * 64 * 1024} }, /* all */ |
| 407 | { X, 1, 0x4, {0x000000, 128 * 64 * 1024} }, /* all */ |
| 408 | { X, 1, 0x5, {0x000000, 128 * 64 * 1024} }, /* all */ |
| 409 | { X, 1, 0x6, {0x000000, 128 * 64 * 1024} }, /* all */ |
| 410 | { X, 1, 0x7, {0x000000, 128 * 64 * 1024} }, /* all */ |
| 411 | }; |
| 412 | |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 413 | static struct w25q_range w25q16_ranges[] = { |
| 414 | { X, X, 0, {0, 0} }, /* none */ |
| 415 | { 0, 0, 0x1, {0x1f0000, 64 * 1024} }, |
| 416 | { 0, 0, 0x2, {0x1e0000, 128 * 1024} }, |
| 417 | { 0, 0, 0x3, {0x1c0000, 256 * 1024} }, |
| 418 | { 0, 0, 0x4, {0x180000, 512 * 1024} }, |
| 419 | { 0, 0, 0x5, {0x100000, 1024 * 1024} }, |
| 420 | |
| 421 | { 0, 1, 0x1, {0x000000, 64 * 1024} }, |
| 422 | { 0, 1, 0x2, {0x000000, 128 * 1024} }, |
| 423 | { 0, 1, 0x3, {0x000000, 256 * 1024} }, |
| 424 | { 0, 1, 0x4, {0x000000, 512 * 1024} }, |
| 425 | { 0, 1, 0x5, {0x000000, 1024 * 1024} }, |
| 426 | { X, X, 0x6, {0x000000, 2048 * 1024} }, |
| 427 | { X, X, 0x7, {0x000000, 2048 * 1024} }, |
| 428 | |
| 429 | { 1, 0, 0x1, {0x1ff000, 4 * 1024} }, |
| 430 | { 1, 0, 0x2, {0x1fe000, 8 * 1024} }, |
| 431 | { 1, 0, 0x3, {0x1fc000, 16 * 1024} }, |
| 432 | { 1, 0, 0x4, {0x1f8000, 32 * 1024} }, |
| 433 | { 1, 0, 0x5, {0x1f8000, 32 * 1024} }, |
| 434 | |
| 435 | { 1, 1, 0x1, {0x000000, 4 * 1024} }, |
| 436 | { 1, 1, 0x2, {0x000000, 8 * 1024} }, |
| 437 | { 1, 1, 0x3, {0x000000, 16 * 1024} }, |
| 438 | { 1, 1, 0x4, {0x000000, 32 * 1024} }, |
| 439 | { 1, 1, 0x5, {0x000000, 32 * 1024} }, |
| 440 | }; |
| 441 | |
| 442 | static struct w25q_range w25q32_ranges[] = { |
| 443 | { X, X, 0, {0, 0} }, /* none */ |
| 444 | { 0, 0, 0x1, {0x3f0000, 64 * 1024} }, |
| 445 | { 0, 0, 0x2, {0x3e0000, 128 * 1024} }, |
| 446 | { 0, 0, 0x3, {0x3c0000, 256 * 1024} }, |
| 447 | { 0, 0, 0x4, {0x380000, 512 * 1024} }, |
| 448 | { 0, 0, 0x5, {0x300000, 1024 * 1024} }, |
David Hendricks | 05653ff | 2010-06-15 16:05:12 -0700 | [diff] [blame] | 449 | { 0, 0, 0x6, {0x200000, 2048 * 1024} }, |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 450 | |
| 451 | { 0, 1, 0x1, {0x000000, 64 * 1024} }, |
| 452 | { 0, 1, 0x2, {0x000000, 128 * 1024} }, |
| 453 | { 0, 1, 0x3, {0x000000, 256 * 1024} }, |
| 454 | { 0, 1, 0x4, {0x000000, 512 * 1024} }, |
| 455 | { 0, 1, 0x5, {0x000000, 1024 * 1024} }, |
| 456 | { 0, 1, 0x6, {0x000000, 2048 * 1024} }, |
| 457 | { X, X, 0x7, {0x000000, 4096 * 1024} }, |
| 458 | |
| 459 | { 1, 0, 0x1, {0x3ff000, 4 * 1024} }, |
| 460 | { 1, 0, 0x2, {0x3fe000, 8 * 1024} }, |
| 461 | { 1, 0, 0x3, {0x3fc000, 16 * 1024} }, |
| 462 | { 1, 0, 0x4, {0x3f8000, 32 * 1024} }, |
| 463 | { 1, 0, 0x5, {0x3f8000, 32 * 1024} }, |
| 464 | |
| 465 | { 1, 1, 0x1, {0x000000, 4 * 1024} }, |
| 466 | { 1, 1, 0x2, {0x000000, 8 * 1024} }, |
| 467 | { 1, 1, 0x3, {0x000000, 16 * 1024} }, |
| 468 | { 1, 1, 0x4, {0x000000, 32 * 1024} }, |
| 469 | { 1, 1, 0x5, {0x000000, 32 * 1024} }, |
| 470 | }; |
| 471 | |
| 472 | static struct w25q_range w25q80_ranges[] = { |
| 473 | { X, X, 0, {0, 0} }, /* none */ |
| 474 | { 0, 0, 0x1, {0x0f0000, 64 * 1024} }, |
| 475 | { 0, 0, 0x2, {0x0e0000, 128 * 1024} }, |
| 476 | { 0, 0, 0x3, {0x0c0000, 256 * 1024} }, |
| 477 | { 0, 0, 0x4, {0x080000, 512 * 1024} }, |
| 478 | |
| 479 | { 0, 1, 0x1, {0x000000, 64 * 1024} }, |
| 480 | { 0, 1, 0x2, {0x000000, 128 * 1024} }, |
| 481 | { 0, 1, 0x3, {0x000000, 256 * 1024} }, |
| 482 | { 0, 1, 0x4, {0x000000, 512 * 1024} }, |
David Hendricks | 05653ff | 2010-06-15 16:05:12 -0700 | [diff] [blame] | 483 | { X, X, 0x6, {0x000000, 1024 * 1024} }, |
| 484 | { X, X, 0x7, {0x000000, 1024 * 1024} }, |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 485 | |
| 486 | { 1, 0, 0x1, {0x1ff000, 4 * 1024} }, |
| 487 | { 1, 0, 0x2, {0x1fe000, 8 * 1024} }, |
| 488 | { 1, 0, 0x3, {0x1fc000, 16 * 1024} }, |
| 489 | { 1, 0, 0x4, {0x1f8000, 32 * 1024} }, |
| 490 | { 1, 0, 0x5, {0x1f8000, 32 * 1024} }, |
| 491 | |
| 492 | { 1, 1, 0x1, {0x000000, 4 * 1024} }, |
| 493 | { 1, 1, 0x2, {0x000000, 8 * 1024} }, |
| 494 | { 1, 1, 0x3, {0x000000, 16 * 1024} }, |
| 495 | { 1, 1, 0x4, {0x000000, 32 * 1024} }, |
| 496 | { 1, 1, 0x5, {0x000000, 32 * 1024} }, |
| 497 | }; |
| 498 | |
David Hendricks | 2c4a76c | 2010-06-28 14:00:43 -0700 | [diff] [blame] | 499 | static struct w25q_range w25q64_ranges[] = { |
| 500 | { X, X, 0, {0, 0} }, /* none */ |
| 501 | |
| 502 | { 0, 0, 0x1, {0x7e0000, 128 * 1024} }, |
| 503 | { 0, 0, 0x2, {0x7c0000, 256 * 1024} }, |
| 504 | { 0, 0, 0x3, {0x780000, 512 * 1024} }, |
| 505 | { 0, 0, 0x4, {0x700000, 1024 * 1024} }, |
| 506 | { 0, 0, 0x5, {0x600000, 2048 * 1024} }, |
| 507 | { 0, 0, 0x6, {0x400000, 4096 * 1024} }, |
| 508 | |
| 509 | { 0, 1, 0x1, {0x000000, 128 * 1024} }, |
| 510 | { 0, 1, 0x2, {0x000000, 256 * 1024} }, |
| 511 | { 0, 1, 0x3, {0x000000, 512 * 1024} }, |
| 512 | { 0, 1, 0x4, {0x000000, 1024 * 1024} }, |
| 513 | { 0, 1, 0x5, {0x000000, 2048 * 1024} }, |
| 514 | { 0, 1, 0x6, {0x000000, 4096 * 1024} }, |
| 515 | { X, X, 0x7, {0x000000, 8192 * 1024} }, |
| 516 | |
| 517 | { 1, 0, 0x1, {0x7ff000, 4 * 1024} }, |
| 518 | { 1, 0, 0x2, {0x7fe000, 8 * 1024} }, |
| 519 | { 1, 0, 0x3, {0x7fc000, 16 * 1024} }, |
| 520 | { 1, 0, 0x4, {0x7f8000, 32 * 1024} }, |
| 521 | { 1, 0, 0x5, {0x7f8000, 32 * 1024} }, |
| 522 | |
| 523 | { 1, 1, 0x1, {0x000000, 4 * 1024} }, |
| 524 | { 1, 1, 0x2, {0x000000, 8 * 1024} }, |
| 525 | { 1, 1, 0x3, {0x000000, 16 * 1024} }, |
| 526 | { 1, 1, 0x4, {0x000000, 32 * 1024} }, |
| 527 | { 1, 1, 0x5, {0x000000, 32 * 1024} }, |
| 528 | }; |
| 529 | |
Duncan Laurie | ed32d7b | 2015-05-27 11:28:18 -0700 | [diff] [blame] | 530 | static struct w25q_range w25rq128_cmp0_ranges[] = { |
| 531 | { X, X, 0, {0, 0} }, /* NONE */ |
Ramya Vijaykumar | e6a7ca8 | 2015-05-12 14:27:29 +0530 | [diff] [blame] | 532 | |
Duncan Laurie | ed32d7b | 2015-05-27 11:28:18 -0700 | [diff] [blame] | 533 | { 0, 0, 0x1, {0xfc0000, 256 * 1024} }, /* Upper 1/64 */ |
| 534 | { 0, 0, 0x2, {0xf80000, 512 * 1024} }, /* Upper 1/32 */ |
| 535 | { 0, 0, 0x3, {0xf00000, 1024 * 1024} }, /* Upper 1/16 */ |
| 536 | { 0, 0, 0x4, {0xe00000, 2048 * 1024} }, /* Upper 1/8 */ |
| 537 | { 0, 0, 0x5, {0xc00000, 4096 * 1024} }, /* Upper 1/4 */ |
| 538 | { 0, 0, 0x6, {0x800000, 8192 * 1024} }, /* Upper 1/2 */ |
Ramya Vijaykumar | e6a7ca8 | 2015-05-12 14:27:29 +0530 | [diff] [blame] | 539 | |
Duncan Laurie | ed32d7b | 2015-05-27 11:28:18 -0700 | [diff] [blame] | 540 | { 0, 1, 0x1, {0x000000, 256 * 1024} }, /* Lower 1/64 */ |
| 541 | { 0, 1, 0x2, {0x000000, 512 * 1024} }, /* Lower 1/32 */ |
| 542 | { 0, 1, 0x3, {0x000000, 1024 * 1024} }, /* Lower 1/16 */ |
| 543 | { 0, 1, 0x4, {0x000000, 2048 * 1024} }, /* Lower 1/8 */ |
| 544 | { 0, 1, 0x5, {0x000000, 4096 * 1024} }, /* Lower 1/4 */ |
| 545 | { 0, 1, 0x6, {0x000000, 8192 * 1024} }, /* Lower 1/2 */ |
Ramya Vijaykumar | e6a7ca8 | 2015-05-12 14:27:29 +0530 | [diff] [blame] | 546 | |
Duncan Laurie | ed32d7b | 2015-05-27 11:28:18 -0700 | [diff] [blame] | 547 | { X, X, 0x7, {0x000000, 16384 * 1024} }, /* ALL */ |
Ramya Vijaykumar | e6a7ca8 | 2015-05-12 14:27:29 +0530 | [diff] [blame] | 548 | |
Duncan Laurie | ed32d7b | 2015-05-27 11:28:18 -0700 | [diff] [blame] | 549 | { 1, 0, 0x1, {0xfff000, 4 * 1024} }, /* Upper 1/4096 */ |
| 550 | { 1, 0, 0x2, {0xffe000, 8 * 1024} }, /* Upper 1/2048 */ |
| 551 | { 1, 0, 0x3, {0xffc000, 16 * 1024} }, /* Upper 1/1024 */ |
| 552 | { 1, 0, 0x4, {0xff8000, 32 * 1024} }, /* Upper 1/512 */ |
| 553 | { 1, 0, 0x5, {0xff8000, 32 * 1024} }, /* Upper 1/512 */ |
| 554 | |
| 555 | { 1, 1, 0x1, {0x000000, 4 * 1024} }, /* Lower 1/4096 */ |
| 556 | { 1, 1, 0x2, {0x000000, 8 * 1024} }, /* Lower 1/2048 */ |
| 557 | { 1, 1, 0x3, {0x000000, 16 * 1024} }, /* Lower 1/1024 */ |
| 558 | { 1, 1, 0x4, {0x000000, 32 * 1024} }, /* Lower 1/512 */ |
| 559 | { 1, 1, 0x5, {0x000000, 32 * 1024} }, /* Lower 1/512 */ |
| 560 | }; |
| 561 | |
| 562 | static struct w25q_range w25rq128_cmp1_ranges[] = { |
| 563 | { X, X, 0x0, {0x000000, 16 * 1024 * 1024} }, /* ALL */ |
| 564 | |
| 565 | { 0, 0, 0x1, {0x000000, 16128 * 1024} }, /* Lower 63/64 */ |
| 566 | { 0, 0, 0x2, {0x000000, 15872 * 1024} }, /* Lower 31/32 */ |
| 567 | { 0, 0, 0x3, {0x000000, 15 * 1024 * 1024} }, /* Lower 15/16 */ |
| 568 | { 0, 0, 0x4, {0x000000, 14 * 1024 * 1024} }, /* Lower 7/8 */ |
| 569 | { 0, 0, 0x5, {0x000000, 12 * 1024 * 1024} }, /* Lower 3/4 */ |
| 570 | { 0, 0, 0x6, {0x000000, 8 * 1024 * 1024} }, /* Lower 1/2 */ |
| 571 | |
| 572 | { 0, 1, 0x1, {0x040000, 16128 * 1024} }, /* Upper 63/64 */ |
| 573 | { 0, 1, 0x2, {0x080000, 15872 * 1024} }, /* Upper 31/32 */ |
| 574 | { 0, 1, 0x3, {0x100000, 15 * 1024 * 1024} }, /* Upper 15/16 */ |
| 575 | { 0, 1, 0x4, {0x200000, 14 * 1024 * 1024} }, /* Upper 7/8 */ |
| 576 | { 0, 1, 0x5, {0x400000, 12 * 1024 * 1024} }, /* Upper 3/4 */ |
| 577 | { 0, 1, 0x6, {0x800000, 8 * 1024 * 1024} }, /* Upper 1/2 */ |
| 578 | |
| 579 | { X, X, 0x7, {0x000000, 0} }, /* NONE */ |
| 580 | |
| 581 | { 1, 0, 0x1, {0x000000, 16380 * 1024} }, /* Lower 4095/4096 */ |
| 582 | { 1, 0, 0x2, {0x000000, 16376 * 1024} }, /* Lower 2048/2048 */ |
| 583 | { 1, 0, 0x3, {0x000000, 16368 * 1024} }, /* Lower 1023/1024 */ |
| 584 | { 1, 0, 0x4, {0x000000, 16352 * 1024} }, /* Lower 511/512 */ |
| 585 | { 1, 0, 0x5, {0x000000, 16352 * 1024} }, /* Lower 511/512 */ |
| 586 | |
| 587 | { 1, 1, 0x1, {0x001000, 16380 * 1024} }, /* Upper 4095/4096 */ |
| 588 | { 1, 1, 0x2, {0x002000, 16376 * 1024} }, /* Upper 2047/2048 */ |
| 589 | { 1, 1, 0x3, {0x004000, 16368 * 1024} }, /* Upper 1023/1024 */ |
| 590 | { 1, 1, 0x4, {0x008000, 16352 * 1024} }, /* Upper 511/512 */ |
| 591 | { 1, 1, 0x5, {0x008000, 16352 * 1024} }, /* Upper 511/512 */ |
Ramya Vijaykumar | e6a7ca8 | 2015-05-12 14:27:29 +0530 | [diff] [blame] | 592 | }; |
| 593 | |
Louis Yung-Chieh Lo | 232951f | 2010-09-16 11:30:00 +0800 | [diff] [blame] | 594 | struct w25q_range w25x10_ranges[] = { |
| 595 | { X, X, 0, {0, 0} }, /* none */ |
| 596 | { 0, 0, 0x1, {0x010000, 64 * 1024} }, |
| 597 | { 0, 1, 0x1, {0x000000, 64 * 1024} }, |
| 598 | { X, X, 0x2, {0x000000, 128 * 1024} }, |
| 599 | { X, X, 0x3, {0x000000, 128 * 1024} }, |
| 600 | }; |
| 601 | |
| 602 | struct w25q_range w25x20_ranges[] = { |
| 603 | { X, X, 0, {0, 0} }, /* none */ |
| 604 | { 0, 0, 0x1, {0x030000, 64 * 1024} }, |
| 605 | { 0, 0, 0x2, {0x020000, 128 * 1024} }, |
| 606 | { 0, 1, 0x1, {0x000000, 64 * 1024} }, |
| 607 | { 0, 1, 0x2, {0x000000, 128 * 1024} }, |
| 608 | { 0, X, 0x3, {0x000000, 256 * 1024} }, |
| 609 | }; |
| 610 | |
David Hendricks | 470ca95 | 2010-08-13 14:01:53 -0700 | [diff] [blame] | 611 | struct w25q_range w25x40_ranges[] = { |
| 612 | { X, X, 0, {0, 0} }, /* none */ |
| 613 | { 0, 0, 0x1, {0x070000, 64 * 1024} }, |
| 614 | { 0, 0, 0x2, {0x060000, 128 * 1024} }, |
| 615 | { 0, 0, 0x3, {0x040000, 256 * 1024} }, |
| 616 | { 0, 1, 0x1, {0x000000, 64 * 1024} }, |
| 617 | { 0, 1, 0x2, {0x000000, 128 * 1024} }, |
| 618 | { 0, 1, 0x3, {0x000000, 256 * 1024} }, |
| 619 | { 0, X, 0x4, {0x000000, 512 * 1024} }, |
David Hendricks | b389abb | 2016-06-17 16:47:00 -0700 | [diff] [blame] | 620 | { 0, X, 0x5, {0x000000, 512 * 1024} }, |
| 621 | { 0, X, 0x6, {0x000000, 512 * 1024} }, |
| 622 | { 0, X, 0x7, {0x000000, 512 * 1024} }, |
David Hendricks | 470ca95 | 2010-08-13 14:01:53 -0700 | [diff] [blame] | 623 | }; |
| 624 | |
Louis Yung-Chieh Lo | 232951f | 2010-09-16 11:30:00 +0800 | [diff] [blame] | 625 | struct w25q_range w25x80_ranges[] = { |
| 626 | { X, X, 0, {0, 0} }, /* none */ |
| 627 | { 0, 0, 0x1, {0x0F0000, 64 * 1024} }, |
| 628 | { 0, 0, 0x2, {0x0E0000, 128 * 1024} }, |
| 629 | { 0, 0, 0x3, {0x0C0000, 256 * 1024} }, |
| 630 | { 0, 0, 0x4, {0x080000, 512 * 1024} }, |
| 631 | { 0, 1, 0x1, {0x000000, 64 * 1024} }, |
| 632 | { 0, 1, 0x2, {0x000000, 128 * 1024} }, |
| 633 | { 0, 1, 0x3, {0x000000, 256 * 1024} }, |
| 634 | { 0, 1, 0x4, {0x000000, 512 * 1024} }, |
| 635 | { 0, X, 0x5, {0x000000, 1024 * 1024} }, |
| 636 | { 0, X, 0x6, {0x000000, 1024 * 1024} }, |
| 637 | { 0, X, 0x7, {0x000000, 1024 * 1024} }, |
| 638 | }; |
| 639 | |
Shawn Nematbakhsh | 9e8ef49 | 2012-09-01 21:58:03 -0700 | [diff] [blame] | 640 | static struct w25q_range gd25q64_ranges[] = { |
| 641 | { X, X, 0, {0, 0} }, /* none */ |
| 642 | { 0, 0, 0x1, {0x7e0000, 128 * 1024} }, |
| 643 | { 0, 0, 0x2, {0x7c0000, 256 * 1024} }, |
| 644 | { 0, 0, 0x3, {0x780000, 512 * 1024} }, |
| 645 | { 0, 0, 0x4, {0x700000, 1024 * 1024} }, |
| 646 | { 0, 0, 0x5, {0x600000, 2048 * 1024} }, |
| 647 | { 0, 0, 0x6, {0x400000, 4096 * 1024} }, |
| 648 | |
| 649 | { 0, 1, 0x1, {0x000000, 128 * 1024} }, |
| 650 | { 0, 1, 0x2, {0x000000, 256 * 1024} }, |
| 651 | { 0, 1, 0x3, {0x000000, 512 * 1024} }, |
| 652 | { 0, 1, 0x4, {0x000000, 1024 * 1024} }, |
| 653 | { 0, 1, 0x5, {0x000000, 2048 * 1024} }, |
| 654 | { 0, 1, 0x6, {0x000000, 4096 * 1024} }, |
| 655 | { X, X, 0x7, {0x000000, 8192 * 1024} }, |
| 656 | |
| 657 | { 1, 0, 0x1, {0x7ff000, 4 * 1024} }, |
| 658 | { 1, 0, 0x2, {0x7fe000, 8 * 1024} }, |
| 659 | { 1, 0, 0x3, {0x7fc000, 16 * 1024} }, |
| 660 | { 1, 0, 0x4, {0x7f8000, 32 * 1024} }, |
| 661 | { 1, 0, 0x5, {0x7f8000, 32 * 1024} }, |
| 662 | { 1, 0, 0x6, {0x7f8000, 32 * 1024} }, |
| 663 | |
| 664 | { 1, 1, 0x1, {0x000000, 4 * 1024} }, |
| 665 | { 1, 1, 0x2, {0x000000, 8 * 1024} }, |
| 666 | { 1, 1, 0x3, {0x000000, 16 * 1024} }, |
| 667 | { 1, 1, 0x4, {0x000000, 32 * 1024} }, |
| 668 | { 1, 1, 0x5, {0x000000, 32 * 1024} }, |
| 669 | { 1, 1, 0x6, {0x000000, 32 * 1024} }, |
| 670 | }; |
| 671 | |
Louis Yung-Chieh Lo | c8ec715 | 2012-09-17 17:38:35 +0800 | [diff] [blame] | 672 | static struct w25q_range a25l040_ranges[] = { |
| 673 | { X, X, 0x0, {0, 0} }, /* none */ |
| 674 | { X, X, 0x1, {0x70000, 64 * 1024} }, |
| 675 | { X, X, 0x2, {0x60000, 128 * 1024} }, |
| 676 | { X, X, 0x3, {0x40000, 256 * 1024} }, |
| 677 | { X, X, 0x4, {0x00000, 512 * 1024} }, |
| 678 | { X, X, 0x5, {0x00000, 512 * 1024} }, |
| 679 | { X, X, 0x6, {0x00000, 512 * 1024} }, |
| 680 | { X, X, 0x7, {0x00000, 512 * 1024} }, |
| 681 | }; |
| 682 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 683 | static uint8_t do_read_status(const struct flashctx *flash) |
Ramya Vijaykumar | 4af3f82 | 2016-01-27 11:51:27 +0530 | [diff] [blame] | 684 | { |
| 685 | if (flash->read_status) |
| 686 | return flash->read_status(flash); |
| 687 | else |
| 688 | return spi_read_status_register(flash); |
| 689 | } |
| 690 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 691 | static int do_write_status(const struct flashctx *flash, int status) |
Ramya Vijaykumar | 4af3f82 | 2016-01-27 11:51:27 +0530 | [diff] [blame] | 692 | { |
| 693 | if (flash->write_status) |
| 694 | return flash->write_status(flash, status); |
| 695 | else |
| 696 | return spi_write_status_register(flash, status); |
| 697 | } |
| 698 | |
Duncan Laurie | ed32d7b | 2015-05-27 11:28:18 -0700 | [diff] [blame] | 699 | /* FIXME: Move to spi25.c if it's a JEDEC standard opcode */ |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 700 | static uint8_t w25q_read_status_register_2(const struct flashctx *flash) |
Duncan Laurie | ed32d7b | 2015-05-27 11:28:18 -0700 | [diff] [blame] | 701 | { |
| 702 | static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { 0x35 }; |
| 703 | unsigned char readarr[2]; |
| 704 | int ret; |
| 705 | |
| 706 | /* Read Status Register */ |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 707 | ret = spi_send_command(flash, sizeof(cmd), sizeof(readarr), cmd, readarr); |
Duncan Laurie | ed32d7b | 2015-05-27 11:28:18 -0700 | [diff] [blame] | 708 | if (ret) { |
| 709 | /* |
| 710 | * FIXME: make this a benign failure for now in case we are |
| 711 | * unable to execute the opcode |
| 712 | */ |
| 713 | msg_cdbg("RDSR2 failed!\n"); |
| 714 | readarr[0] = 0x00; |
| 715 | } |
| 716 | |
| 717 | return readarr[0]; |
| 718 | } |
| 719 | |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 720 | /* Given a flash chip, this function returns its range table. */ |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 721 | static int w25_range_table(const struct flashctx *flash, |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 722 | struct w25q_range **w25q_ranges, |
| 723 | int *num_entries) |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 724 | { |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 725 | *w25q_ranges = 0; |
| 726 | *num_entries = 0; |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 727 | |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 728 | switch (flash->manufacture_id) { |
| 729 | case WINBOND_NEX_ID: |
| 730 | switch(flash->model_id) { |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 731 | case WINBOND_NEX_W25X10: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 732 | *w25q_ranges = w25x10_ranges; |
| 733 | *num_entries = ARRAY_SIZE(w25x10_ranges); |
Louis Yung-Chieh Lo | 232951f | 2010-09-16 11:30:00 +0800 | [diff] [blame] | 734 | break; |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 735 | case WINBOND_NEX_W25X20: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 736 | *w25q_ranges = w25x20_ranges; |
| 737 | *num_entries = ARRAY_SIZE(w25x20_ranges); |
Louis Yung-Chieh Lo | 232951f | 2010-09-16 11:30:00 +0800 | [diff] [blame] | 738 | break; |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 739 | case WINBOND_NEX_W25X40: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 740 | *w25q_ranges = w25x40_ranges; |
| 741 | *num_entries = ARRAY_SIZE(w25x40_ranges); |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 742 | break; |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 743 | case WINBOND_NEX_W25X80: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 744 | *w25q_ranges = w25x80_ranges; |
| 745 | *num_entries = ARRAY_SIZE(w25x80_ranges); |
Louis Yung-Chieh Lo | 232951f | 2010-09-16 11:30:00 +0800 | [diff] [blame] | 746 | break; |
Patrick Georgi | cc04a45 | 2017-02-06 12:14:43 +0100 | [diff] [blame] | 747 | case WINBOND_NEX_W25Q80_V: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 748 | *w25q_ranges = w25q80_ranges; |
| 749 | *num_entries = ARRAY_SIZE(w25q80_ranges); |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 750 | break; |
Patrick Georgi | cc04a45 | 2017-02-06 12:14:43 +0100 | [diff] [blame] | 751 | case WINBOND_NEX_W25Q16_V: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 752 | *w25q_ranges = w25q16_ranges; |
| 753 | *num_entries = ARRAY_SIZE(w25q16_ranges); |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 754 | break; |
Patrick Georgi | cc04a45 | 2017-02-06 12:14:43 +0100 | [diff] [blame] | 755 | case WINBOND_NEX_W25Q32_V: |
| 756 | case WINBOND_NEX_W25Q32_W: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 757 | *w25q_ranges = w25q32_ranges; |
| 758 | *num_entries = ARRAY_SIZE(w25q32_ranges); |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 759 | break; |
Patrick Georgi | cc04a45 | 2017-02-06 12:14:43 +0100 | [diff] [blame] | 760 | case WINBOND_NEX_W25Q64_V: |
| 761 | case WINBOND_NEX_W25Q64_W: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 762 | *w25q_ranges = w25q64_ranges; |
| 763 | *num_entries = ARRAY_SIZE(w25q64_ranges); |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 764 | break; |
Patrick Georgi | cc04a45 | 2017-02-06 12:14:43 +0100 | [diff] [blame] | 765 | case WINBOND_NEX_W25Q128_V: |
| 766 | case WINBOND_NEX_W25Q128_W: |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 767 | if (w25q_read_status_register_2(flash) & (1 << 6)) { |
Duncan Laurie | ed32d7b | 2015-05-27 11:28:18 -0700 | [diff] [blame] | 768 | /* CMP == 1 */ |
| 769 | *w25q_ranges = w25rq128_cmp1_ranges; |
| 770 | *num_entries = ARRAY_SIZE(w25rq128_cmp1_ranges); |
| 771 | } else { |
| 772 | /* CMP == 0 */ |
| 773 | *w25q_ranges = w25rq128_cmp0_ranges; |
| 774 | *num_entries = ARRAY_SIZE(w25rq128_cmp0_ranges); |
| 775 | } |
Ramya Vijaykumar | e6a7ca8 | 2015-05-12 14:27:29 +0530 | [diff] [blame] | 776 | break; |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 777 | default: |
| 778 | msg_cerr("%s() %d: WINBOND flash chip mismatch (0x%04x)" |
| 779 | ", aborting\n", __func__, __LINE__, |
| 780 | flash->model_id); |
| 781 | return -1; |
| 782 | } |
David Hendricks | 2c4a76c | 2010-06-28 14:00:43 -0700 | [diff] [blame] | 783 | break; |
David Hendricks | 57566ed | 2010-08-16 18:24:45 -0700 | [diff] [blame] | 784 | case EON_ID_NOPREFIX: |
| 785 | switch (flash->model_id) { |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 786 | case EON_EN25F40: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 787 | *w25q_ranges = en25f40_ranges; |
| 788 | *num_entries = ARRAY_SIZE(en25f40_ranges); |
David Hendricks | 57566ed | 2010-08-16 18:24:45 -0700 | [diff] [blame] | 789 | break; |
David Hendricks | e185bf2 | 2011-05-24 15:34:18 -0700 | [diff] [blame] | 790 | case EON_EN25Q40: |
| 791 | *w25q_ranges = en25q40_ranges; |
| 792 | *num_entries = ARRAY_SIZE(en25q40_ranges); |
| 793 | break; |
| 794 | case EON_EN25Q80: |
| 795 | *w25q_ranges = en25q80_ranges; |
| 796 | *num_entries = ARRAY_SIZE(en25q80_ranges); |
| 797 | break; |
| 798 | case EON_EN25Q32: |
| 799 | *w25q_ranges = en25q32_ranges; |
| 800 | *num_entries = ARRAY_SIZE(en25q32_ranges); |
| 801 | break; |
| 802 | case EON_EN25Q64: |
| 803 | *w25q_ranges = en25q64_ranges; |
| 804 | *num_entries = ARRAY_SIZE(en25q64_ranges); |
| 805 | break; |
| 806 | case EON_EN25Q128: |
| 807 | *w25q_ranges = en25q128_ranges; |
| 808 | *num_entries = ARRAY_SIZE(en25q128_ranges); |
| 809 | break; |
Marc Jones | b2f9002 | 2014-04-29 17:37:23 -0600 | [diff] [blame] | 810 | case EON_EN25S64: |
| 811 | *w25q_ranges = en25s64_ranges; |
| 812 | *num_entries = ARRAY_SIZE(en25s64_ranges); |
| 813 | break; |
David Hendricks | 57566ed | 2010-08-16 18:24:45 -0700 | [diff] [blame] | 814 | default: |
| 815 | msg_cerr("%s():%d: EON flash chip mismatch (0x%04x)" |
| 816 | ", aborting\n", __func__, __LINE__, |
| 817 | flash->model_id); |
| 818 | return -1; |
| 819 | } |
| 820 | break; |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 821 | case MACRONIX_ID: |
David Hendricks | ac72e36 | 2010-08-16 18:20:03 -0700 | [diff] [blame] | 822 | switch (flash->model_id) { |
David Hendricks | f8f00c7 | 2011-02-01 12:39:46 -0800 | [diff] [blame] | 823 | case MACRONIX_MX25L1005: |
| 824 | *w25q_ranges = mx25l1005_ranges; |
| 825 | *num_entries = ARRAY_SIZE(mx25l1005_ranges); |
| 826 | break; |
| 827 | case MACRONIX_MX25L2005: |
| 828 | *w25q_ranges = mx25l2005_ranges; |
| 829 | *num_entries = ARRAY_SIZE(mx25l2005_ranges); |
| 830 | break; |
| 831 | case MACRONIX_MX25L4005: |
| 832 | *w25q_ranges = mx25l4005_ranges; |
| 833 | *num_entries = ARRAY_SIZE(mx25l4005_ranges); |
| 834 | break; |
| 835 | case MACRONIX_MX25L8005: |
| 836 | *w25q_ranges = mx25l8005_ranges; |
| 837 | *num_entries = ARRAY_SIZE(mx25l8005_ranges); |
| 838 | break; |
| 839 | case MACRONIX_MX25L1605: |
| 840 | /* FIXME: MX25L1605 and MX25L1605D have different write |
| 841 | * protection capabilities, but share IDs */ |
| 842 | *w25q_ranges = mx25l1605d_ranges; |
| 843 | *num_entries = ARRAY_SIZE(mx25l1605d_ranges); |
| 844 | break; |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 845 | case MACRONIX_MX25L3205: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 846 | *w25q_ranges = mx25l3205d_ranges; |
| 847 | *num_entries = ARRAY_SIZE(mx25l3205d_ranges); |
David Hendricks | ac72e36 | 2010-08-16 18:20:03 -0700 | [diff] [blame] | 848 | break; |
Vincent Palatin | 87e092a | 2013-02-28 15:46:14 -0800 | [diff] [blame] | 849 | case MACRONIX_MX25U3235E: |
| 850 | *w25q_ranges = mx25u3235e_ranges; |
| 851 | *num_entries = ARRAY_SIZE(mx25u3235e_ranges); |
| 852 | break; |
Jongpil | 66a9649 | 2014-08-14 17:59:06 +0900 | [diff] [blame] | 853 | case MACRONIX_MX25U6435E: |
| 854 | *w25q_ranges = mx25u6435e_ranges; |
| 855 | *num_entries = ARRAY_SIZE(mx25u6435e_ranges); |
| 856 | break; |
David Hendricks | ac72e36 | 2010-08-16 18:20:03 -0700 | [diff] [blame] | 857 | default: |
| 858 | msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)" |
| 859 | ", aborting\n", __func__, __LINE__, |
| 860 | flash->model_id); |
| 861 | return -1; |
| 862 | } |
| 863 | break; |
David Hendricks | bfa624b | 2012-07-24 12:47:59 -0700 | [diff] [blame] | 864 | case ST_ID: |
| 865 | switch(flash->model_id) { |
| 866 | case ST_N25Q064__1E: |
| 867 | case ST_N25Q064__3E: |
| 868 | *w25q_ranges = n25q064_ranges; |
| 869 | *num_entries = ARRAY_SIZE(n25q064_ranges); |
| 870 | break; |
| 871 | default: |
| 872 | msg_cerr("%s() %d: Micron flash chip mismatch" |
| 873 | " (0x%04x), aborting\n", __func__, __LINE__, |
| 874 | flash->model_id); |
| 875 | return -1; |
| 876 | } |
| 877 | break; |
Bryan Freed | 9a0051f | 2012-05-22 16:06:09 -0700 | [diff] [blame] | 878 | case GIGADEVICE_ID: |
| 879 | switch(flash->model_id) { |
| 880 | case GIGADEVICE_GD25LQ32: |
| 881 | *w25q_ranges = w25q32_ranges; |
| 882 | *num_entries = ARRAY_SIZE(w25q32_ranges); |
| 883 | break; |
Shawn Nematbakhsh | 9e8ef49 | 2012-09-01 21:58:03 -0700 | [diff] [blame] | 884 | case GIGADEVICE_GD25Q64: |
Marc Jones | b18734f | 2014-04-03 16:19:47 -0600 | [diff] [blame] | 885 | case GIGADEVICE_GD25LQ64: |
Shawn Nematbakhsh | 9e8ef49 | 2012-09-01 21:58:03 -0700 | [diff] [blame] | 886 | *w25q_ranges = gd25q64_ranges; |
| 887 | *num_entries = ARRAY_SIZE(gd25q64_ranges); |
| 888 | break; |
| 889 | /* TODO(shawnn): add support for other GD parts */ |
Bryan Freed | 9a0051f | 2012-05-22 16:06:09 -0700 | [diff] [blame] | 890 | default: |
| 891 | msg_cerr("%s() %d: GigaDevice flash chip mismatch" |
| 892 | " (0x%04x), aborting\n", __func__, __LINE__, |
| 893 | flash->model_id); |
| 894 | return -1; |
| 895 | } |
| 896 | break; |
Louis Yung-Chieh Lo | c8ec715 | 2012-09-17 17:38:35 +0800 | [diff] [blame] | 897 | case AMIC_ID_NOPREFIX: |
| 898 | switch(flash->model_id) { |
| 899 | case AMIC_A25L040: |
| 900 | *w25q_ranges = a25l040_ranges; |
| 901 | *num_entries = ARRAY_SIZE(a25l040_ranges); |
| 902 | break; |
| 903 | default: |
| 904 | msg_cerr("%s() %d: AMIC flash chip mismatch" |
| 905 | " (0x%04x), aborting\n", __func__, __LINE__, |
| 906 | flash->model_id); |
| 907 | return -1; |
| 908 | } |
| 909 | break; |
Furquan Shaikh | b4df8ef | 2017-01-05 15:05:35 -0800 | [diff] [blame] | 910 | case ATMEL_ID: |
| 911 | switch(flash->model_id) { |
| 912 | case ATMEL_AT25SL128A: |
| 913 | if (w25q_read_status_register_2(flash) & (1 << 6)) { |
| 914 | /* CMP == 1 */ |
| 915 | *w25q_ranges = w25rq128_cmp1_ranges; |
| 916 | *num_entries = ARRAY_SIZE(w25rq128_cmp1_ranges); |
| 917 | } else { |
| 918 | /* CMP == 0 */ |
| 919 | *w25q_ranges = w25rq128_cmp0_ranges; |
| 920 | *num_entries = ARRAY_SIZE(w25rq128_cmp0_ranges); |
| 921 | } |
| 922 | break; |
| 923 | default: |
| 924 | msg_cerr("%s() %d: Atmel flash chip mismatch" |
| 925 | " (0x%04x), aborting\n", __func__, __LINE__, |
| 926 | flash->model_id); |
| 927 | return -1; |
| 928 | } |
| 929 | break; |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 930 | default: |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 931 | msg_cerr("%s: flash vendor (0x%x) not found, aborting\n", |
| 932 | __func__, flash->manufacture_id); |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 933 | return -1; |
| 934 | } |
| 935 | |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 936 | return 0; |
| 937 | } |
| 938 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 939 | int w25_range_to_status(const struct flashctx *flash, |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 940 | unsigned int start, unsigned int len, |
| 941 | struct w25q_status *status) |
| 942 | { |
| 943 | struct w25q_range *w25q_ranges; |
| 944 | int i, range_found = 0; |
| 945 | int num_entries; |
| 946 | |
| 947 | if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1; |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 948 | for (i = 0; i < num_entries; i++) { |
| 949 | struct wp_range *r = &w25q_ranges[i].range; |
| 950 | |
| 951 | msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n", |
| 952 | start, len, r->start, r->len); |
| 953 | if ((start == r->start) && (len == r->len)) { |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 954 | status->bp0 = w25q_ranges[i].bp & 1; |
| 955 | status->bp1 = w25q_ranges[i].bp >> 1; |
| 956 | status->bp2 = w25q_ranges[i].bp >> 2; |
| 957 | status->tb = w25q_ranges[i].tb; |
| 958 | status->sec = w25q_ranges[i].sec; |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 959 | |
| 960 | range_found = 1; |
| 961 | break; |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | if (!range_found) { |
| 966 | msg_cerr("matching range not found\n"); |
| 967 | return -1; |
| 968 | } |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 969 | return 0; |
| 970 | } |
| 971 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 972 | int w25_status_to_range(const struct flashctx *flash, |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 973 | const struct w25q_status *status, |
| 974 | unsigned int *start, unsigned int *len) |
| 975 | { |
| 976 | struct w25q_range *w25q_ranges; |
| 977 | int i, status_found = 0; |
| 978 | int num_entries; |
| 979 | |
| 980 | if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1; |
| 981 | for (i = 0; i < num_entries; i++) { |
| 982 | int bp; |
Louis Yung-Chieh Lo | edd3930 | 2011-11-10 15:43:06 +0800 | [diff] [blame] | 983 | int table_bp, table_tb, table_sec; |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 984 | |
| 985 | bp = status->bp0 | (status->bp1 << 1) | (status->bp2 << 2); |
| 986 | msg_cspew("comparing 0x%x 0x%x / 0x%x 0x%x / 0x%x 0x%x\n", |
| 987 | bp, w25q_ranges[i].bp, |
| 988 | status->tb, w25q_ranges[i].tb, |
| 989 | status->sec, w25q_ranges[i].sec); |
Louis Yung-Chieh Lo | edd3930 | 2011-11-10 15:43:06 +0800 | [diff] [blame] | 990 | table_bp = w25q_ranges[i].bp; |
| 991 | table_tb = w25q_ranges[i].tb; |
| 992 | table_sec = w25q_ranges[i].sec; |
| 993 | if ((bp == table_bp || table_bp == X) && |
| 994 | (status->tb == table_tb || table_tb == X) && |
| 995 | (status->sec == table_sec || table_sec == X)) { |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 996 | *start = w25q_ranges[i].range.start; |
| 997 | *len = w25q_ranges[i].range.len; |
| 998 | |
| 999 | status_found = 1; |
| 1000 | break; |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | if (!status_found) { |
| 1005 | msg_cerr("matching status not found\n"); |
| 1006 | return -1; |
| 1007 | } |
| 1008 | return 0; |
| 1009 | } |
| 1010 | |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 1011 | /* Given a [start, len], this function calls w25_range_to_status() to convert |
| 1012 | * it to flash-chip-specific range bits, then sets into status register. |
| 1013 | */ |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 1014 | static int w25_set_range(const struct flashctx *flash, |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 1015 | unsigned int start, unsigned int len) |
| 1016 | { |
| 1017 | struct w25q_status status; |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 1018 | int tmp = 0; |
| 1019 | int expected = 0; |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 1020 | |
| 1021 | memset(&status, 0, sizeof(status)); |
Ramya Vijaykumar | 4af3f82 | 2016-01-27 11:51:27 +0530 | [diff] [blame] | 1022 | tmp = do_read_status(flash); |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 1023 | memcpy(&status, &tmp, 1); |
| 1024 | msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp); |
| 1025 | |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 1026 | if (w25_range_to_status(flash, start, len, &status)) return -1; |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 1027 | |
| 1028 | msg_cdbg("status.busy: %x\n", status.busy); |
| 1029 | msg_cdbg("status.wel: %x\n", status.wel); |
| 1030 | msg_cdbg("status.bp0: %x\n", status.bp0); |
| 1031 | msg_cdbg("status.bp1: %x\n", status.bp1); |
| 1032 | msg_cdbg("status.bp2: %x\n", status.bp2); |
| 1033 | msg_cdbg("status.tb: %x\n", status.tb); |
| 1034 | msg_cdbg("status.sec: %x\n", status.sec); |
| 1035 | msg_cdbg("status.srp0: %x\n", status.srp0); |
| 1036 | |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 1037 | memcpy(&expected, &status, sizeof(status)); |
Ramya Vijaykumar | 4af3f82 | 2016-01-27 11:51:27 +0530 | [diff] [blame] | 1038 | do_write_status(flash, expected); |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 1039 | |
Ramya Vijaykumar | 4af3f82 | 2016-01-27 11:51:27 +0530 | [diff] [blame] | 1040 | tmp = do_read_status(flash); |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 1041 | msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp); |
| 1042 | if ((tmp & MASK_WP_AREA) == (expected & MASK_WP_AREA)) { |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 1043 | return 0; |
| 1044 | } else { |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 1045 | msg_cerr("expected=0x%02x, but actual=0x%02x.\n", |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 1046 | expected, tmp); |
| 1047 | return 1; |
| 1048 | } |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 1049 | } |
| 1050 | |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 1051 | /* Print out the current status register value with human-readable text. */ |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 1052 | static int w25_wp_status(const struct flashctx *flash) |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 1053 | { |
| 1054 | struct w25q_status status; |
| 1055 | int tmp; |
David Hendricks | ce8ded3 | 2010-10-08 11:23:38 -0700 | [diff] [blame] | 1056 | unsigned int start, len; |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 1057 | int ret = 0; |
| 1058 | |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 1059 | memset(&status, 0, sizeof(status)); |
Ramya Vijaykumar | 4af3f82 | 2016-01-27 11:51:27 +0530 | [diff] [blame] | 1060 | tmp = do_read_status(flash); |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 1061 | memcpy(&status, &tmp, 1); |
| 1062 | msg_cinfo("WP: status: 0x%02x\n", tmp); |
| 1063 | msg_cinfo("WP: status.srp0: %x\n", status.srp0); |
| 1064 | msg_cinfo("WP: write protect is %s.\n", |
| 1065 | status.srp0 ? "enabled" : "disabled"); |
| 1066 | |
| 1067 | msg_cinfo("WP: write protect range: "); |
| 1068 | if (w25_status_to_range(flash, &status, &start, &len)) { |
| 1069 | msg_cinfo("(cannot resolve the range)\n"); |
| 1070 | ret = -1; |
| 1071 | } else { |
| 1072 | msg_cinfo("start=0x%08x, len=0x%08x\n", start, len); |
| 1073 | } |
| 1074 | |
| 1075 | return ret; |
| 1076 | } |
| 1077 | |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 1078 | /* Set/clear the SRP0 bit in the status register. */ |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 1079 | static int w25_set_srp0(const struct flashctx *flash, int enable) |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 1080 | { |
| 1081 | struct w25q_status status; |
| 1082 | int tmp = 0; |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 1083 | int expected = 0; |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 1084 | |
| 1085 | memset(&status, 0, sizeof(status)); |
Ramya Vijaykumar | 4af3f82 | 2016-01-27 11:51:27 +0530 | [diff] [blame] | 1086 | tmp = do_read_status(flash); |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 1087 | /* FIXME: this is NOT endian-free copy. */ |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 1088 | memcpy(&status, &tmp, 1); |
| 1089 | msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp); |
| 1090 | |
Louis Yung-Chieh Lo | c19d3c5 | 2010-10-08 11:59:16 +0800 | [diff] [blame] | 1091 | status.srp0 = enable ? 1 : 0; |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 1092 | memcpy(&expected, &status, sizeof(status)); |
Ramya Vijaykumar | 4af3f82 | 2016-01-27 11:51:27 +0530 | [diff] [blame] | 1093 | do_write_status(flash, expected); |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 1094 | |
Ramya Vijaykumar | 4af3f82 | 2016-01-27 11:51:27 +0530 | [diff] [blame] | 1095 | tmp = do_read_status(flash); |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 1096 | msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp); |
| 1097 | if ((tmp & MASK_WP_AREA) != (expected & MASK_WP_AREA)) |
| 1098 | return 1; |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 1099 | |
| 1100 | return 0; |
| 1101 | } |
| 1102 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 1103 | static int w25_enable_writeprotect(const struct flashctx *flash, |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1104 | enum wp_mode wp_mode) |
Louis Yung-Chieh Lo | c19d3c5 | 2010-10-08 11:59:16 +0800 | [diff] [blame] | 1105 | { |
| 1106 | int ret; |
| 1107 | |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1108 | switch (wp_mode) { |
| 1109 | case WP_MODE_HARDWARE: |
| 1110 | ret = w25_set_srp0(flash, 1); |
| 1111 | break; |
| 1112 | default: |
| 1113 | msg_cerr("%s(): unsupported write-protect mode\n", __func__); |
| 1114 | return 1; |
| 1115 | } |
| 1116 | |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 1117 | if (ret) |
| 1118 | msg_cerr("%s(): error=%d.\n", __func__, ret); |
Louis Yung-Chieh Lo | c19d3c5 | 2010-10-08 11:59:16 +0800 | [diff] [blame] | 1119 | return ret; |
| 1120 | } |
| 1121 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 1122 | static int w25_disable_writeprotect(const struct flashctx *flash) |
Louis Yung-Chieh Lo | c19d3c5 | 2010-10-08 11:59:16 +0800 | [diff] [blame] | 1123 | { |
| 1124 | int ret; |
| 1125 | |
| 1126 | ret = w25_set_srp0(flash, 0); |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 1127 | if (ret) |
| 1128 | msg_cerr("%s(): error=%d.\n", __func__, ret); |
Louis Yung-Chieh Lo | c19d3c5 | 2010-10-08 11:59:16 +0800 | [diff] [blame] | 1129 | return ret; |
| 1130 | } |
| 1131 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 1132 | static int w25_list_ranges(const struct flashctx *flash) |
David Hendricks | 0f7f538 | 2011-02-11 18:12:31 -0800 | [diff] [blame] | 1133 | { |
| 1134 | struct w25q_range *w25q_ranges; |
| 1135 | int i, num_entries; |
| 1136 | |
| 1137 | if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1; |
| 1138 | for (i = 0; i < num_entries; i++) { |
| 1139 | msg_cinfo("start: 0x%06x, length: 0x%06x\n", |
| 1140 | w25q_ranges[i].range.start, |
| 1141 | w25q_ranges[i].range.len); |
| 1142 | } |
| 1143 | |
| 1144 | return 0; |
| 1145 | } |
| 1146 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 1147 | static int w25q_wp_status(const struct flashctx *flash) |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1148 | { |
| 1149 | struct w25q_status sr1; |
| 1150 | struct w25q_status_2 sr2; |
David Hendricks | f1bd880 | 2012-10-30 11:37:57 -0700 | [diff] [blame] | 1151 | uint8_t tmp[2]; |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1152 | unsigned int start, len; |
| 1153 | int ret = 0; |
| 1154 | |
| 1155 | memset(&sr1, 0, sizeof(sr1)); |
Ramya Vijaykumar | 4af3f82 | 2016-01-27 11:51:27 +0530 | [diff] [blame] | 1156 | tmp[0] = do_read_status(flash); |
David Hendricks | f1bd880 | 2012-10-30 11:37:57 -0700 | [diff] [blame] | 1157 | memcpy(&sr1, &tmp[0], 1); |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1158 | |
David Hendricks | f1bd880 | 2012-10-30 11:37:57 -0700 | [diff] [blame] | 1159 | memset(&sr2, 0, sizeof(sr2)); |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 1160 | tmp[1] = w25q_read_status_register_2(flash); |
David Hendricks | f1bd880 | 2012-10-30 11:37:57 -0700 | [diff] [blame] | 1161 | memcpy(&sr2, &tmp[1], 1); |
| 1162 | |
| 1163 | msg_cinfo("WP: status: 0x%02x%02x\n", tmp[1], tmp[0]); |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1164 | msg_cinfo("WP: status.srp0: %x\n", sr1.srp0); |
| 1165 | msg_cinfo("WP: status.srp1: %x\n", sr2.srp1); |
| 1166 | msg_cinfo("WP: write protect is %s.\n", |
| 1167 | (sr1.srp0 || sr2.srp1) ? "enabled" : "disabled"); |
| 1168 | |
| 1169 | msg_cinfo("WP: write protect range: "); |
| 1170 | if (w25_status_to_range(flash, &sr1, &start, &len)) { |
| 1171 | msg_cinfo("(cannot resolve the range)\n"); |
| 1172 | ret = -1; |
| 1173 | } else { |
| 1174 | msg_cinfo("start=0x%08x, len=0x%08x\n", start, len); |
| 1175 | } |
| 1176 | |
| 1177 | return ret; |
| 1178 | } |
| 1179 | |
| 1180 | /* |
| 1181 | * W25Q adds an optional byte to the standard WRSR opcode. If /CS is |
| 1182 | * de-asserted after the first byte, then it acts like a JEDEC-standard |
| 1183 | * WRSR command. if /CS is asserted, then the next data byte is written |
| 1184 | * into status register 2. |
| 1185 | */ |
| 1186 | #define W25Q_WRSR_OUTSIZE 0x03 |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 1187 | static int w25q_write_status_register_WREN(const struct flashctx *flash, uint8_t s1, uint8_t s2) |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1188 | { |
| 1189 | int result; |
| 1190 | struct spi_command cmds[] = { |
| 1191 | { |
| 1192 | /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */ |
| 1193 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 1194 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 1195 | .readcnt = 0, |
| 1196 | .readarr = NULL, |
| 1197 | }, { |
| 1198 | .writecnt = W25Q_WRSR_OUTSIZE, |
| 1199 | .writearr = (const unsigned char[]){ JEDEC_WRSR, s1, s2 }, |
| 1200 | .readcnt = 0, |
| 1201 | .readarr = NULL, |
| 1202 | }, { |
| 1203 | .writecnt = 0, |
| 1204 | .writearr = NULL, |
| 1205 | .readcnt = 0, |
| 1206 | .readarr = NULL, |
| 1207 | }}; |
| 1208 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 1209 | result = spi_send_multicommand(flash, cmds); |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1210 | if (result) { |
| 1211 | msg_cerr("%s failed during command execution\n", |
| 1212 | __func__); |
| 1213 | } |
| 1214 | |
| 1215 | /* WRSR performs a self-timed erase before the changes take effect. */ |
David Hendricks | 6082404 | 2014-12-11 17:22:06 -0800 | [diff] [blame] | 1216 | programmer_delay(100 * 1000); |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1217 | |
| 1218 | return result; |
| 1219 | } |
| 1220 | |
| 1221 | /* |
| 1222 | * Set/clear the SRP1 bit in status register 2. |
| 1223 | * FIXME: make this more generic if other chips use the same SR2 layout |
| 1224 | */ |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 1225 | static int w25q_set_srp1(const struct flashctx *flash, int enable) |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1226 | { |
| 1227 | struct w25q_status sr1; |
| 1228 | struct w25q_status_2 sr2; |
| 1229 | uint8_t tmp, expected; |
| 1230 | |
Ramya Vijaykumar | 4af3f82 | 2016-01-27 11:51:27 +0530 | [diff] [blame] | 1231 | tmp = do_read_status(flash); |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1232 | memcpy(&sr1, &tmp, 1); |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 1233 | tmp = w25q_read_status_register_2(flash); |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1234 | memcpy(&sr2, &tmp, 1); |
| 1235 | |
| 1236 | msg_cdbg("%s: old status 2: 0x%02x\n", __func__, tmp); |
| 1237 | |
| 1238 | sr2.srp1 = enable ? 1 : 0; |
| 1239 | |
| 1240 | memcpy(&expected, &sr2, 1); |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 1241 | w25q_write_status_register_WREN(flash, *((uint8_t *)&sr1), *((uint8_t *)&sr2)); |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1242 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 1243 | tmp = w25q_read_status_register_2(flash); |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1244 | msg_cdbg("%s: new status 2: 0x%02x\n", __func__, tmp); |
| 1245 | if ((tmp & MASK_WP2_AREA) != (expected & MASK_WP2_AREA)) |
| 1246 | return 1; |
| 1247 | |
| 1248 | return 0; |
| 1249 | } |
| 1250 | |
| 1251 | enum wp_mode get_wp_mode(const char *mode_str) |
| 1252 | { |
| 1253 | enum wp_mode wp_mode = WP_MODE_UNKNOWN; |
| 1254 | |
| 1255 | if (!strcasecmp(mode_str, "hardware")) |
| 1256 | wp_mode = WP_MODE_HARDWARE; |
| 1257 | else if (!strcasecmp(mode_str, "power_cycle")) |
| 1258 | wp_mode = WP_MODE_POWER_CYCLE; |
| 1259 | else if (!strcasecmp(mode_str, "permanent")) |
| 1260 | wp_mode = WP_MODE_PERMANENT; |
| 1261 | |
| 1262 | return wp_mode; |
| 1263 | } |
| 1264 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 1265 | static int w25q_disable_writeprotect(const struct flashctx *flash, |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1266 | enum wp_mode wp_mode) |
| 1267 | { |
| 1268 | int ret = 1; |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1269 | struct w25q_status_2 sr2; |
| 1270 | uint8_t tmp; |
| 1271 | |
| 1272 | switch (wp_mode) { |
| 1273 | case WP_MODE_HARDWARE: |
| 1274 | ret = w25_set_srp0(flash, 0); |
| 1275 | break; |
| 1276 | case WP_MODE_POWER_CYCLE: |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 1277 | tmp = w25q_read_status_register_2(flash); |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1278 | memcpy(&sr2, &tmp, 1); |
| 1279 | if (sr2.srp1) { |
| 1280 | msg_cerr("%s(): must disconnect power to disable " |
| 1281 | "write-protection\n", __func__); |
| 1282 | } else { |
| 1283 | ret = 0; |
| 1284 | } |
| 1285 | break; |
| 1286 | case WP_MODE_PERMANENT: |
| 1287 | msg_cerr("%s(): cannot disable permanent write-protection\n", |
| 1288 | __func__); |
| 1289 | break; |
| 1290 | default: |
| 1291 | msg_cerr("%s(): invalid mode specified\n", __func__); |
| 1292 | break; |
| 1293 | } |
| 1294 | |
| 1295 | if (ret) |
| 1296 | msg_cerr("%s(): error=%d.\n", __func__, ret); |
| 1297 | return ret; |
| 1298 | } |
| 1299 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 1300 | static int w25q_disable_writeprotect_default(const struct flashctx *flash) |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1301 | { |
| 1302 | return w25q_disable_writeprotect(flash, WP_MODE_HARDWARE); |
| 1303 | } |
| 1304 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 1305 | static int w25q_enable_writeprotect(const struct flashctx *flash, |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1306 | enum wp_mode wp_mode) |
| 1307 | { |
| 1308 | int ret = 1; |
| 1309 | struct w25q_status sr1; |
| 1310 | struct w25q_status_2 sr2; |
| 1311 | uint8_t tmp; |
| 1312 | |
| 1313 | switch (wp_mode) { |
| 1314 | case WP_MODE_HARDWARE: |
| 1315 | if (w25q_disable_writeprotect(flash, WP_MODE_POWER_CYCLE)) { |
| 1316 | msg_cerr("%s(): cannot disable power cycle WP mode\n", |
| 1317 | __func__); |
| 1318 | break; |
| 1319 | } |
| 1320 | |
Ramya Vijaykumar | 4af3f82 | 2016-01-27 11:51:27 +0530 | [diff] [blame] | 1321 | tmp = do_read_status(flash); |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1322 | memcpy(&sr1, &tmp, 1); |
| 1323 | if (sr1.srp0) |
| 1324 | ret = 0; |
| 1325 | else |
| 1326 | ret = w25_set_srp0(flash, 1); |
| 1327 | |
| 1328 | break; |
| 1329 | case WP_MODE_POWER_CYCLE: |
| 1330 | if (w25q_disable_writeprotect(flash, WP_MODE_HARDWARE)) { |
| 1331 | msg_cerr("%s(): cannot disable hardware WP mode\n", |
| 1332 | __func__); |
| 1333 | break; |
| 1334 | } |
| 1335 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 1336 | tmp = w25q_read_status_register_2(flash); |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1337 | memcpy(&sr2, &tmp, 1); |
| 1338 | if (sr2.srp1) |
| 1339 | ret = 0; |
| 1340 | else |
| 1341 | ret = w25q_set_srp1(flash, 1); |
| 1342 | |
| 1343 | break; |
| 1344 | case WP_MODE_PERMANENT: |
Ramya Vijaykumar | 4af3f82 | 2016-01-27 11:51:27 +0530 | [diff] [blame] | 1345 | tmp = do_read_status(flash); |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1346 | memcpy(&sr1, &tmp, 1); |
| 1347 | if (sr1.srp0 == 0) { |
| 1348 | ret = w25_set_srp0(flash, 1); |
| 1349 | if (ret) { |
David Hendricks | f1bd880 | 2012-10-30 11:37:57 -0700 | [diff] [blame] | 1350 | msg_perr("%s(): cannot enable SRP0 for " |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1351 | "permanent WP\n", __func__); |
| 1352 | break; |
| 1353 | } |
| 1354 | } |
| 1355 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 1356 | tmp = w25q_read_status_register_2(flash); |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1357 | memcpy(&sr2, &tmp, 1); |
| 1358 | if (sr2.srp1 == 0) { |
| 1359 | ret = w25q_set_srp1(flash, 1); |
| 1360 | if (ret) { |
David Hendricks | f1bd880 | 2012-10-30 11:37:57 -0700 | [diff] [blame] | 1361 | msg_perr("%s(): cannot enable SRP1 for " |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1362 | "permanent WP\n", __func__); |
| 1363 | break; |
| 1364 | } |
| 1365 | } |
| 1366 | |
| 1367 | break; |
David Hendricks | f1bd880 | 2012-10-30 11:37:57 -0700 | [diff] [blame] | 1368 | default: |
| 1369 | msg_perr("%s(): invalid mode %d\n", __func__, wp_mode); |
| 1370 | break; |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1371 | } |
| 1372 | |
| 1373 | if (ret) |
| 1374 | msg_cerr("%s(): error=%d.\n", __func__, ret); |
| 1375 | return ret; |
| 1376 | } |
| 1377 | |
David Hendricks | c349609 | 2014-11-13 17:20:55 -0800 | [diff] [blame] | 1378 | /* FIXME: Move to spi25.c if it's a JEDEC standard opcode */ |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 1379 | uint8_t mx25l_read_config_register(const struct flashctx *flash) |
David Hendricks | c349609 | 2014-11-13 17:20:55 -0800 | [diff] [blame] | 1380 | { |
| 1381 | static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { 0x15 }; |
| 1382 | unsigned char readarr[2]; /* leave room for dummy byte */ |
| 1383 | int ret; |
| 1384 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 1385 | ret = spi_send_command(flash, sizeof(cmd), sizeof(readarr), cmd, readarr); |
David Hendricks | c349609 | 2014-11-13 17:20:55 -0800 | [diff] [blame] | 1386 | if (ret) { |
| 1387 | msg_cerr("RDCR failed!\n"); |
| 1388 | readarr[0] = 0x00; |
| 1389 | } |
| 1390 | |
| 1391 | return readarr[0]; |
| 1392 | } |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1393 | /* W25P, W25X, and many flash chips from various vendors */ |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 1394 | struct wp wp_w25 = { |
David Hendricks | 0f7f538 | 2011-02-11 18:12:31 -0800 | [diff] [blame] | 1395 | .list_ranges = w25_list_ranges, |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 1396 | .set_range = w25_set_range, |
| 1397 | .enable = w25_enable_writeprotect, |
Louis Yung-Chieh Lo | c19d3c5 | 2010-10-08 11:59:16 +0800 | [diff] [blame] | 1398 | .disable = w25_disable_writeprotect, |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 1399 | .wp_status = w25_wp_status, |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1400 | |
| 1401 | }; |
| 1402 | |
| 1403 | /* W25Q series has features such as a second status register and SFDP */ |
| 1404 | struct wp wp_w25q = { |
| 1405 | .list_ranges = w25_list_ranges, |
| 1406 | .set_range = w25_set_range, |
| 1407 | .enable = w25q_enable_writeprotect, |
| 1408 | /* |
| 1409 | * By default, disable hardware write-protection. We may change |
| 1410 | * this later if we want to add fine-grained write-protect disable |
| 1411 | * as a command-line option. |
| 1412 | */ |
| 1413 | .disable = w25q_disable_writeprotect_default, |
| 1414 | .wp_status = w25q_wp_status, |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 1415 | }; |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 1416 | |
David Hendricks | af3944a | 2014-07-28 18:37:40 -0700 | [diff] [blame] | 1417 | struct generic_range gd25q32_cmp0_ranges[] = { |
| 1418 | /* none, bp4 and bp3 => don't care */ |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1419 | { { }, 0x00, {0, 0} }, |
| 1420 | { { }, 0x08, {0, 0} }, |
| 1421 | { { }, 0x10, {0, 0} }, |
| 1422 | { { }, 0x18, {0, 0} }, |
David Hendricks | af3944a | 2014-07-28 18:37:40 -0700 | [diff] [blame] | 1423 | |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1424 | { { }, 0x01, {0x3f0000, 64 * 1024} }, |
| 1425 | { { }, 0x02, {0x3e0000, 128 * 1024} }, |
| 1426 | { { }, 0x03, {0x3c0000, 256 * 1024} }, |
| 1427 | { { }, 0x04, {0x380000, 512 * 1024} }, |
| 1428 | { { }, 0x05, {0x300000, 1024 * 1024} }, |
| 1429 | { { }, 0x06, {0x200000, 2048 * 1024} }, |
David Hendricks | af3944a | 2014-07-28 18:37:40 -0700 | [diff] [blame] | 1430 | |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1431 | { { }, 0x09, {0x000000, 64 * 1024} }, |
| 1432 | { { }, 0x0a, {0x000000, 128 * 1024} }, |
| 1433 | { { }, 0x0b, {0x000000, 256 * 1024} }, |
| 1434 | { { }, 0x0c, {0x000000, 512 * 1024} }, |
| 1435 | { { }, 0x0d, {0x000000, 1024 * 1024} }, |
| 1436 | { { }, 0x0e, {0x000000, 2048 * 1024} }, |
David Hendricks | af3944a | 2014-07-28 18:37:40 -0700 | [diff] [blame] | 1437 | |
| 1438 | /* all, bp4 and bp3 => don't care */ |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1439 | { { }, 0x07, {0x000000, 4096 * 1024} }, |
| 1440 | { { }, 0x0f, {0x000000, 4096 * 1024} }, |
| 1441 | { { }, 0x17, {0x000000, 4096 * 1024} }, |
| 1442 | { { }, 0x1f, {0x000000, 4096 * 1024} }, |
David Hendricks | af3944a | 2014-07-28 18:37:40 -0700 | [diff] [blame] | 1443 | |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1444 | { { }, 0x11, {0x3ff000, 4 * 1024} }, |
| 1445 | { { }, 0x12, {0x3fe000, 8 * 1024} }, |
| 1446 | { { }, 0x13, {0x3fc000, 16 * 1024} }, |
| 1447 | { { }, 0x14, {0x3f8000, 32 * 1024} }, /* bp0 => don't care */ |
| 1448 | { { }, 0x15, {0x3f8000, 32 * 1024} }, /* bp0 => don't care */ |
| 1449 | { { }, 0x16, {0x3f8000, 32 * 1024} }, |
David Hendricks | af3944a | 2014-07-28 18:37:40 -0700 | [diff] [blame] | 1450 | |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1451 | { { }, 0x19, {0x000000, 4 * 1024} }, |
| 1452 | { { }, 0x1a, {0x000000, 8 * 1024} }, |
| 1453 | { { }, 0x1b, {0x000000, 16 * 1024} }, |
| 1454 | { { }, 0x1c, {0x000000, 32 * 1024} }, /* bp0 => don't care */ |
| 1455 | { { }, 0x1d, {0x000000, 32 * 1024} }, /* bp0 => don't care */ |
| 1456 | { { }, 0x1e, {0x000000, 32 * 1024} }, |
David Hendricks | af3944a | 2014-07-28 18:37:40 -0700 | [diff] [blame] | 1457 | }; |
| 1458 | |
| 1459 | struct generic_range gd25q32_cmp1_ranges[] = { |
| 1460 | /* none, bp4 and bp3 => don't care */ |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1461 | { { }, 0x00, {0, 0} }, |
| 1462 | { { }, 0x08, {0, 0} }, |
| 1463 | { { }, 0x10, {0, 0} }, |
| 1464 | { { }, 0x18, {0, 0} }, |
David Hendricks | af3944a | 2014-07-28 18:37:40 -0700 | [diff] [blame] | 1465 | |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1466 | { { }, 0x01, {0x000000, 4032 * 1024} }, |
| 1467 | { { }, 0x02, {0x000000, 3968 * 1024} }, |
| 1468 | { { }, 0x03, {0x000000, 3840 * 1024} }, |
| 1469 | { { }, 0x04, {0x000000, 3584 * 1024} }, |
| 1470 | { { }, 0x05, {0x000000, 3 * 1024 * 1024} }, |
| 1471 | { { }, 0x06, {0x000000, 2 * 1024 * 1024} }, |
David Hendricks | af3944a | 2014-07-28 18:37:40 -0700 | [diff] [blame] | 1472 | |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1473 | { { }, 0x09, {0x010000, 4032 * 1024} }, |
| 1474 | { { }, 0x0a, {0x020000, 3968 * 1024} }, |
| 1475 | { { }, 0x0b, {0x040000, 3840 * 1024} }, |
| 1476 | { { }, 0x0c, {0x080000, 3584 * 1024} }, |
| 1477 | { { }, 0x0d, {0x100000, 3 * 1024 * 1024} }, |
| 1478 | { { }, 0x0e, {0x200000, 2 * 1024 * 1024} }, |
David Hendricks | af3944a | 2014-07-28 18:37:40 -0700 | [diff] [blame] | 1479 | |
| 1480 | /* all, bp4 and bp3 => don't care */ |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1481 | { { }, 0x07, {0x000000, 4096 * 1024} }, |
| 1482 | { { }, 0x0f, {0x000000, 4096 * 1024} }, |
| 1483 | { { }, 0x17, {0x000000, 4096 * 1024} }, |
| 1484 | { { }, 0x1f, {0x000000, 4096 * 1024} }, |
David Hendricks | af3944a | 2014-07-28 18:37:40 -0700 | [diff] [blame] | 1485 | |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1486 | { { }, 0x11, {0x000000, 4092 * 1024} }, |
| 1487 | { { }, 0x12, {0x000000, 4088 * 1024} }, |
| 1488 | { { }, 0x13, {0x000000, 4080 * 1024} }, |
| 1489 | { { }, 0x14, {0x000000, 4064 * 1024} }, /* bp0 => don't care */ |
| 1490 | { { }, 0x15, {0x000000, 4064 * 1024} }, /* bp0 => don't care */ |
| 1491 | { { }, 0x16, {0x000000, 4064 * 1024} }, |
David Hendricks | af3944a | 2014-07-28 18:37:40 -0700 | [diff] [blame] | 1492 | |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1493 | { { }, 0x19, {0x001000, 4092 * 1024} }, |
| 1494 | { { }, 0x1a, {0x002000, 4088 * 1024} }, |
| 1495 | { { }, 0x1b, {0x040000, 4080 * 1024} }, |
| 1496 | { { }, 0x1c, {0x080000, 4064 * 1024} }, /* bp0 => don't care */ |
| 1497 | { { }, 0x1d, {0x080000, 4064 * 1024} }, /* bp0 => don't care */ |
| 1498 | { { }, 0x1e, {0x080000, 4064 * 1024} }, |
David Hendricks | af3944a | 2014-07-28 18:37:40 -0700 | [diff] [blame] | 1499 | }; |
| 1500 | |
| 1501 | static struct generic_wp gd25q32_wp = { |
| 1502 | /* TODO: map second status register */ |
| 1503 | .sr1 = { .bp0_pos = 2, .bp_bits = 5, .srp_pos = 7 }, |
| 1504 | }; |
| 1505 | |
David Hendricks | 1e9d7ca | 2016-03-14 15:50:34 -0700 | [diff] [blame] | 1506 | struct generic_range gd25q128_cmp0_ranges[] = { |
| 1507 | /* none, bp4 and bp3 => don't care, others = 0 */ |
| 1508 | { { .tb = 0 }, 0x00, {0, 0} }, |
| 1509 | { { .tb = 0 }, 0x08, {0, 0} }, |
| 1510 | { { .tb = 0 }, 0x10, {0, 0} }, |
| 1511 | { { .tb = 0 }, 0x18, {0, 0} }, |
| 1512 | |
| 1513 | { { .tb = 0 }, 0x01, {0xfc0000, 256 * 1024} }, |
| 1514 | { { .tb = 0 }, 0x02, {0xf80000, 512 * 1024} }, |
| 1515 | { { .tb = 0 }, 0x03, {0xf00000, 1024 * 1024} }, |
| 1516 | { { .tb = 0 }, 0x04, {0xe00000, 2048 * 1024} }, |
| 1517 | { { .tb = 0 }, 0x05, {0xc00000, 4096 * 1024} }, |
| 1518 | { { .tb = 0 }, 0x06, {0x800000, 8192 * 1024} }, |
| 1519 | |
| 1520 | { { .tb = 0 }, 0x09, {0x000000, 256 * 1024} }, |
| 1521 | { { .tb = 0 }, 0x0a, {0x000000, 512 * 1024} }, |
| 1522 | { { .tb = 0 }, 0x0b, {0x000000, 1024 * 1024} }, |
| 1523 | { { .tb = 0 }, 0x0c, {0x000000, 2048 * 1024} }, |
| 1524 | { { .tb = 0 }, 0x0d, {0x000000, 4096 * 1024} }, |
| 1525 | { { .tb = 0 }, 0x0e, {0x000000, 8192 * 1024} }, |
| 1526 | |
| 1527 | /* all, bp4 and bp3 => don't care, others = 1 */ |
| 1528 | { { .tb = 0 }, 0x07, {0x000000, 16384 * 1024} }, |
| 1529 | { { .tb = 0 }, 0x0f, {0x000000, 16384 * 1024} }, |
| 1530 | { { .tb = 0 }, 0x17, {0x000000, 16384 * 1024} }, |
| 1531 | { { .tb = 0 }, 0x1f, {0x000000, 16384 * 1024} }, |
| 1532 | |
| 1533 | { { .tb = 0 }, 0x11, {0xfff000, 4 * 1024} }, |
| 1534 | { { .tb = 0 }, 0x12, {0xffe000, 8 * 1024} }, |
| 1535 | { { .tb = 0 }, 0x13, {0xffc000, 16 * 1024} }, |
| 1536 | { { .tb = 0 }, 0x14, {0xff8000, 32 * 1024} }, /* bp0 => don't care */ |
| 1537 | { { .tb = 0 }, 0x15, {0xff8000, 32 * 1024} }, /* bp0 => don't care */ |
| 1538 | |
| 1539 | { { .tb = 0 }, 0x19, {0x000000, 4 * 1024} }, |
| 1540 | { { .tb = 0 }, 0x1a, {0x000000, 8 * 1024} }, |
| 1541 | { { .tb = 0 }, 0x1b, {0x000000, 16 * 1024} }, |
| 1542 | { { .tb = 0 }, 0x1c, {0x000000, 32 * 1024} }, /* bp0 => don't care */ |
| 1543 | { { .tb = 0 }, 0x1d, {0x000000, 32 * 1024} }, /* bp0 => don't care */ |
| 1544 | { { .tb = 0 }, 0x1e, {0x000000, 32 * 1024} }, |
| 1545 | }; |
| 1546 | |
| 1547 | struct generic_range gd25q128_cmp1_ranges[] = { |
| 1548 | /* none, bp4 and bp3 => don't care, others = 0 */ |
| 1549 | { { .tb = 1 }, 0x00, {0x000000, 16384 * 1024} }, |
| 1550 | { { .tb = 1 }, 0x08, {0x000000, 16384 * 1024} }, |
| 1551 | { { .tb = 1 }, 0x10, {0x000000, 16384 * 1024} }, |
| 1552 | { { .tb = 1 }, 0x18, {0x000000, 16384 * 1024} }, |
| 1553 | |
| 1554 | { { .tb = 1 }, 0x01, {0x000000, 16128 * 1024} }, |
| 1555 | { { .tb = 1 }, 0x02, {0x000000, 15872 * 1024} }, |
| 1556 | { { .tb = 1 }, 0x03, {0x000000, 15360 * 1024} }, |
| 1557 | { { .tb = 1 }, 0x04, {0x000000, 14336 * 1024} }, |
| 1558 | { { .tb = 1 }, 0x05, {0x000000, 12288 * 1024} }, |
| 1559 | { { .tb = 1 }, 0x06, {0x000000, 8192 * 1024} }, |
| 1560 | |
| 1561 | { { .tb = 1 }, 0x09, {0x000000, 16128 * 1024} }, |
| 1562 | { { .tb = 1 }, 0x0a, {0x000000, 15872 * 1024} }, |
| 1563 | { { .tb = 1 }, 0x0b, {0x000000, 15360 * 1024} }, |
| 1564 | { { .tb = 1 }, 0x0c, {0x000000, 14336 * 1024} }, |
| 1565 | { { .tb = 1 }, 0x0d, {0x000000, 12288 * 1024} }, |
| 1566 | { { .tb = 1 }, 0x0e, {0x000000, 8192 * 1024} }, |
| 1567 | |
| 1568 | /* none, bp4 and bp3 => don't care, others = 1 */ |
| 1569 | { { .tb = 1 }, 0x07, {0x000000, 16384 * 1024} }, |
| 1570 | { { .tb = 1 }, 0x08, {0x000000, 16384 * 1024} }, |
| 1571 | { { .tb = 1 }, 0x0f, {0x000000, 16384 * 1024} }, |
| 1572 | { { .tb = 1 }, 0x17, {0x000000, 16384 * 1024} }, |
| 1573 | { { .tb = 1 }, 0x1f, {0x000000, 16384 * 1024} }, |
| 1574 | |
| 1575 | { { .tb = 1 }, 0x11, {0x000000, 16380 * 1024} }, |
| 1576 | { { .tb = 1 }, 0x12, {0x000000, 16376 * 1024} }, |
| 1577 | { { .tb = 1 }, 0x13, {0x000000, 16368 * 1024} }, |
| 1578 | { { .tb = 1 }, 0x14, {0x000000, 16352 * 1024} }, /* bp0 => don't care */ |
| 1579 | { { .tb = 1 }, 0x15, {0x000000, 16352 * 1024} }, /* bp0 => don't care */ |
| 1580 | |
| 1581 | { { .tb = 1 }, 0x19, {0x001000, 16380 * 1024} }, |
| 1582 | { { .tb = 1 }, 0x1a, {0x002000, 16376 * 1024} }, |
| 1583 | { { .tb = 1 }, 0x1b, {0x004000, 16368 * 1024} }, |
| 1584 | { { .tb = 1 }, 0x1c, {0x008000, 16352 * 1024} }, /* bp0 => don't care */ |
| 1585 | { { .tb = 1 }, 0x1d, {0x008000, 16352 * 1024} }, /* bp0 => don't care */ |
| 1586 | { { .tb = 1 }, 0x1e, {0x008000, 16352 * 1024} }, |
| 1587 | }; |
| 1588 | |
| 1589 | static struct generic_wp gd25q128_wp = { |
| 1590 | /* TODO: map second and third status registers */ |
| 1591 | .sr1 = { .bp0_pos = 2, .bp_bits = 5, .srp_pos = 7 }, |
| 1592 | }; |
| 1593 | |
David Hendricks | 83541d3 | 2014-07-15 20:58:21 -0700 | [diff] [blame] | 1594 | #if 0 |
| 1595 | /* FIXME: MX25L6405D has same ID as MX25L6406 */ |
| 1596 | static struct w25q_range mx25l6405d_ranges[] = { |
| 1597 | { X, 0, 0, {0, 0} }, /* none */ |
| 1598 | { X, 0, 0x1, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */ |
| 1599 | { X, 0, 0x2, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */ |
| 1600 | { X, 0, 0x3, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */ |
| 1601 | { X, 0, 0x4, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */ |
| 1602 | { X, 0, 0x5, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */ |
| 1603 | { X, 0, 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */ |
| 1604 | { X, 0, 0x7, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */ |
| 1605 | |
| 1606 | { X, 1, 0x0, {0x000000, 8192 * 1024} }, |
| 1607 | { X, 1, 0x1, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */ |
| 1608 | { X, 1, 0x2, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */ |
| 1609 | { X, 1, 0x3, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */ |
| 1610 | { X, 1, 0x4, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */ |
| 1611 | { X, 1, 0x5, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */ |
| 1612 | { X, 1, 0x6, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */ |
| 1613 | { X, 1, 0x7, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */ |
| 1614 | }; |
| 1615 | #endif |
| 1616 | |
| 1617 | /* FIXME: MX25L6406 has same ID as MX25L6405D */ |
| 1618 | struct generic_range mx25l6406e_ranges[] = { |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1619 | { { }, 0, {0, 0} }, /* none */ |
| 1620 | { { }, 0x1, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */ |
| 1621 | { { }, 0x2, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */ |
| 1622 | { { }, 0x3, {0x7a0000, 64 * 8 * 1024} }, /* blocks 120-127 */ |
| 1623 | { { }, 0x4, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */ |
| 1624 | { { }, 0x5, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */ |
| 1625 | { { }, 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */ |
David Hendricks | 83541d3 | 2014-07-15 20:58:21 -0700 | [diff] [blame] | 1626 | |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1627 | { { }, 0x7, {0x000000, 64 * 128 * 1024} }, /* all */ |
| 1628 | { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* all */ |
| 1629 | { { }, 0x9, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */ |
| 1630 | { { }, 0xa, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */ |
| 1631 | { { }, 0xb, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */ |
| 1632 | { { }, 0xc, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */ |
| 1633 | { { }, 0xd, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */ |
| 1634 | { { }, 0xe, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */ |
| 1635 | { { }, 0xf, {0x000000, 64 * 128 * 1024} }, /* all */ |
David Hendricks | 83541d3 | 2014-07-15 20:58:21 -0700 | [diff] [blame] | 1636 | }; |
| 1637 | |
| 1638 | static struct generic_wp mx25l6406e_wp = { |
| 1639 | .sr1 = { .bp0_pos = 2, .bp_bits = 4, .srp_pos = 7 }, |
| 1640 | .ranges = &mx25l6406e_ranges[0], |
| 1641 | }; |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 1642 | |
David Hendricks | c349609 | 2014-11-13 17:20:55 -0800 | [diff] [blame] | 1643 | struct generic_range mx25l6495f_tb0_ranges[] = { |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1644 | { { }, 0, {0, 0} }, /* none */ |
| 1645 | { { }, 0x1, {0x7f0000, 64 * 1 * 1024} }, /* block 127 */ |
| 1646 | { { }, 0x2, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */ |
| 1647 | { { }, 0x3, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */ |
David Hendricks | c349609 | 2014-11-13 17:20:55 -0800 | [diff] [blame] | 1648 | |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1649 | { { }, 0x4, {0x780000, 64 * 8 * 1024} }, /* blocks 120-127 */ |
| 1650 | { { }, 0x5, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */ |
| 1651 | { { }, 0x6, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */ |
| 1652 | { { }, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */ |
| 1653 | { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* all */ |
| 1654 | { { }, 0x9, {0x000000, 64 * 128 * 1024} }, /* all */ |
| 1655 | { { }, 0xa, {0x000000, 64 * 128 * 1024} }, /* all */ |
| 1656 | { { }, 0xb, {0x000000, 64 * 128 * 1024} }, /* all */ |
| 1657 | { { }, 0xc, {0x000000, 64 * 128 * 1024} }, /* all */ |
| 1658 | { { }, 0xd, {0x000000, 64 * 128 * 1024} }, /* all */ |
| 1659 | { { }, 0xe, {0x000000, 64 * 128 * 1024} }, /* all */ |
| 1660 | { { }, 0xf, {0x000000, 64 * 128 * 1024} }, /* all */ |
David Hendricks | c349609 | 2014-11-13 17:20:55 -0800 | [diff] [blame] | 1661 | }; |
| 1662 | |
| 1663 | struct generic_range mx25l6495f_tb1_ranges[] = { |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1664 | { { }, 0, {0, 0} }, /* none */ |
| 1665 | { { }, 0x1, {0x000000, 64 * 1 * 1024} }, /* block 0 */ |
| 1666 | { { }, 0x2, {0x000000, 64 * 2 * 1024} }, /* blocks 0-1 */ |
| 1667 | { { }, 0x3, {0x000000, 64 * 4 * 1024} }, /* blocks 0-3 */ |
| 1668 | { { }, 0x4, {0x000000, 64 * 8 * 1024} }, /* blocks 0-7 */ |
| 1669 | { { }, 0x5, {0x000000, 64 * 16 * 1024} }, /* blocks 0-15 */ |
| 1670 | { { }, 0x6, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */ |
| 1671 | { { }, 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */ |
| 1672 | { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* all */ |
| 1673 | { { }, 0x9, {0x000000, 64 * 128 * 1024} }, /* all */ |
| 1674 | { { }, 0xa, {0x000000, 64 * 128 * 1024} }, /* all */ |
| 1675 | { { }, 0xb, {0x000000, 64 * 128 * 1024} }, /* all */ |
| 1676 | { { }, 0xc, {0x000000, 64 * 128 * 1024} }, /* all */ |
| 1677 | { { }, 0xd, {0x000000, 64 * 128 * 1024} }, /* all */ |
| 1678 | { { }, 0xe, {0x000000, 64 * 128 * 1024} }, /* all */ |
| 1679 | { { }, 0xf, {0x000000, 64 * 128 * 1024} }, /* all */ |
David Hendricks | c349609 | 2014-11-13 17:20:55 -0800 | [diff] [blame] | 1680 | }; |
| 1681 | |
| 1682 | static struct generic_wp mx25l6495f_wp = { |
| 1683 | .sr1 = { .bp0_pos = 2, .bp_bits = 4, .srp_pos = 7 }, |
| 1684 | }; |
| 1685 | |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1686 | struct generic_range s25fs128s_ranges[] = { |
| 1687 | { { .tb = 1 }, 0, {0, 0} }, /* none */ |
| 1688 | { { .tb = 1 }, 0x1, {0x000000, 256 * 1024} }, /* lower 64th */ |
| 1689 | { { .tb = 1 }, 0x2, {0x000000, 512 * 1024} }, /* lower 32nd */ |
| 1690 | { { .tb = 1 }, 0x3, {0x000000, 1024 * 1024} }, /* lower 16th */ |
| 1691 | { { .tb = 1 }, 0x4, {0x000000, 2048 * 1024} }, /* lower 8th */ |
| 1692 | { { .tb = 1 }, 0x5, {0x000000, 4096 * 1024} }, /* lower 4th */ |
| 1693 | { { .tb = 1 }, 0x6, {0x000000, 8192 * 1024} }, /* lower half */ |
| 1694 | { { .tb = 1 }, 0x7, {0x000000, 16384 * 1024} }, /* all */ |
David Hendricks | a988485 | 2014-12-11 15:31:12 -0800 | [diff] [blame] | 1695 | |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1696 | { { .tb = 0 }, 0, {0, 0} }, /* none */ |
| 1697 | { { .tb = 0 }, 0x1, {0xfc0000, 256 * 1024} }, /* upper 64th */ |
| 1698 | { { .tb = 0 }, 0x2, {0xf80000, 512 * 1024} }, /* upper 32nd */ |
| 1699 | { { .tb = 0 }, 0x3, {0xf00000, 1024 * 1024} }, /* upper 16th */ |
| 1700 | { { .tb = 0 }, 0x4, {0xe00000, 2048 * 1024} }, /* upper 8th */ |
| 1701 | { { .tb = 0 }, 0x5, {0xc00000, 4096 * 1024} }, /* upper 4th */ |
| 1702 | { { .tb = 0 }, 0x6, {0x800000, 8192 * 1024} }, /* upper half */ |
| 1703 | { { .tb = 0 }, 0x7, {0x000000, 16384 * 1024} }, /* all */ |
David Hendricks | a988485 | 2014-12-11 15:31:12 -0800 | [diff] [blame] | 1704 | }; |
| 1705 | |
| 1706 | static struct generic_wp s25fs128s_wp = { |
| 1707 | .sr1 = { .bp0_pos = 2, .bp_bits = 3, .srp_pos = 7 }, |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1708 | .get_modifier_bits = s25f_get_modifier_bits, |
| 1709 | .set_modifier_bits = s25f_set_modifier_bits, |
David Hendricks | a988485 | 2014-12-11 15:31:12 -0800 | [diff] [blame] | 1710 | }; |
| 1711 | |
David Hendricks | c694bb8 | 2015-02-25 14:52:17 -0800 | [diff] [blame] | 1712 | |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1713 | struct generic_range s25fl256s_ranges[] = { |
| 1714 | { { .tb = 1 }, 0, {0, 0} }, /* none */ |
| 1715 | { { .tb = 1 }, 0x1, {0x000000, 512 * 1024} }, /* lower 64th */ |
| 1716 | { { .tb = 1 }, 0x2, {0x000000, 1024 * 1024} }, /* lower 32nd */ |
| 1717 | { { .tb = 1 }, 0x3, {0x000000, 2048 * 1024} }, /* lower 16th */ |
| 1718 | { { .tb = 1 }, 0x4, {0x000000, 4096 * 1024} }, /* lower 8th */ |
| 1719 | { { .tb = 1 }, 0x5, {0x000000, 8192 * 1024} }, /* lower 4th */ |
| 1720 | { { .tb = 1 }, 0x6, {0x000000, 16384 * 1024} }, /* lower half */ |
| 1721 | { { .tb = 1 }, 0x7, {0x000000, 32768 * 1024} }, /* all */ |
| 1722 | |
| 1723 | { { .tb = 0 }, 0, {0, 0} }, /* none */ |
| 1724 | { { .tb = 0 }, 0x1, {0x1f80000, 512 * 1024} }, /* upper 64th */ |
| 1725 | { { .tb = 0 }, 0x2, {0x1f00000, 1024 * 1024} }, /* upper 32nd */ |
| 1726 | { { .tb = 0 }, 0x3, {0x1e00000, 2048 * 1024} }, /* upper 16th */ |
| 1727 | { { .tb = 0 }, 0x4, {0x1c00000, 4096 * 1024} }, /* upper 8th */ |
| 1728 | { { .tb = 0 }, 0x5, {0x1800000, 8192 * 1024} }, /* upper 4th */ |
| 1729 | { { .tb = 0 }, 0x6, {0x1000000, 16384 * 1024} }, /* upper half */ |
| 1730 | { { .tb = 0 }, 0x7, {0x000000, 32768 * 1024} }, /* all */ |
David Hendricks | c694bb8 | 2015-02-25 14:52:17 -0800 | [diff] [blame] | 1731 | }; |
| 1732 | |
| 1733 | static struct generic_wp s25fl256s_wp = { |
| 1734 | .sr1 = { .bp0_pos = 2, .bp_bits = 3, .srp_pos = 7 }, |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1735 | .get_modifier_bits = s25f_get_modifier_bits, |
| 1736 | .set_modifier_bits = s25f_set_modifier_bits, |
David Hendricks | c694bb8 | 2015-02-25 14:52:17 -0800 | [diff] [blame] | 1737 | }; |
| 1738 | |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 1739 | /* Given a flash chip, this function returns its writeprotect info. */ |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 1740 | static int generic_range_table(const struct flashctx *flash, |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 1741 | struct generic_wp **wp, |
| 1742 | int *num_entries) |
| 1743 | { |
| 1744 | *wp = NULL; |
| 1745 | *num_entries = 0; |
| 1746 | |
| 1747 | switch (flash->manufacture_id) { |
David Hendricks | af3944a | 2014-07-28 18:37:40 -0700 | [diff] [blame] | 1748 | case GIGADEVICE_ID: |
| 1749 | switch(flash->model_id) { |
David Hendricks | 1e9d7ca | 2016-03-14 15:50:34 -0700 | [diff] [blame] | 1750 | |
David Hendricks | af3944a | 2014-07-28 18:37:40 -0700 | [diff] [blame] | 1751 | case GIGADEVICE_GD25Q32: { |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 1752 | uint8_t sr1 = w25q_read_status_register_2(flash); |
David Hendricks | af3944a | 2014-07-28 18:37:40 -0700 | [diff] [blame] | 1753 | *wp = &gd25q32_wp; |
David Hendricks | 1e9d7ca | 2016-03-14 15:50:34 -0700 | [diff] [blame] | 1754 | |
David Hendricks | af3944a | 2014-07-28 18:37:40 -0700 | [diff] [blame] | 1755 | if (!(sr1 & (1 << 6))) { /* CMP == 0 */ |
| 1756 | (*wp)->ranges = &gd25q32_cmp0_ranges[0]; |
| 1757 | *num_entries = ARRAY_SIZE(gd25q32_cmp0_ranges); |
| 1758 | } else { /* CMP == 1 */ |
| 1759 | (*wp)->ranges = &gd25q32_cmp1_ranges[0]; |
| 1760 | *num_entries = ARRAY_SIZE(gd25q32_cmp1_ranges); |
| 1761 | } |
| 1762 | |
| 1763 | break; |
David Hendricks | 1e9d7ca | 2016-03-14 15:50:34 -0700 | [diff] [blame] | 1764 | } |
Furquan Shaikh | 62cd810 | 2016-07-17 23:04:06 -0700 | [diff] [blame] | 1765 | case GIGADEVICE_GD25Q128: |
| 1766 | case GIGADEVICE_GD25LQ128C: { |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 1767 | uint8_t sr1 = w25q_read_status_register_2(flash); |
David Hendricks | 1e9d7ca | 2016-03-14 15:50:34 -0700 | [diff] [blame] | 1768 | *wp = &gd25q128_wp; |
| 1769 | |
| 1770 | if (!(sr1 & (1 << 6))) { /* CMP == 0 */ |
| 1771 | (*wp)->ranges = &gd25q128_cmp0_ranges[0]; |
| 1772 | *num_entries = ARRAY_SIZE(gd25q128_cmp0_ranges); |
| 1773 | } else { /* CMP == 1 */ |
| 1774 | (*wp)->ranges = &gd25q128_cmp1_ranges[0]; |
| 1775 | *num_entries = ARRAY_SIZE(gd25q128_cmp1_ranges); |
| 1776 | } |
| 1777 | |
| 1778 | break; |
David Hendricks | af3944a | 2014-07-28 18:37:40 -0700 | [diff] [blame] | 1779 | } |
| 1780 | default: |
| 1781 | msg_cerr("%s() %d: GigaDevice flash chip mismatch" |
| 1782 | " (0x%04x), aborting\n", __func__, __LINE__, |
| 1783 | flash->model_id); |
| 1784 | return -1; |
| 1785 | } |
| 1786 | break; |
David Hendricks | 83541d3 | 2014-07-15 20:58:21 -0700 | [diff] [blame] | 1787 | case MACRONIX_ID: |
| 1788 | switch (flash->model_id) { |
| 1789 | case MACRONIX_MX25L6405: |
| 1790 | /* FIXME: MX25L64* chips have mixed capabilities and |
| 1791 | share IDs */ |
| 1792 | *wp = &mx25l6406e_wp; |
| 1793 | *num_entries = ARRAY_SIZE(mx25l6406e_ranges); |
| 1794 | break; |
David Hendricks | c349609 | 2014-11-13 17:20:55 -0800 | [diff] [blame] | 1795 | case MACRONIX_MX25L6495F: { |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 1796 | uint8_t cr = mx25l_read_config_register(flash); |
David Hendricks | c349609 | 2014-11-13 17:20:55 -0800 | [diff] [blame] | 1797 | |
| 1798 | *wp = &mx25l6495f_wp; |
| 1799 | if (!(cr & (1 << 3))) { /* T/B == 0 */ |
| 1800 | (*wp)->ranges = &mx25l6495f_tb0_ranges[0]; |
| 1801 | *num_entries = ARRAY_SIZE(mx25l6495f_tb0_ranges); |
| 1802 | } else { /* T/B == 1 */ |
| 1803 | (*wp)->ranges = &mx25l6495f_tb1_ranges[0]; |
| 1804 | *num_entries = ARRAY_SIZE(mx25l6495f_tb1_ranges); |
| 1805 | } |
| 1806 | break; |
| 1807 | } |
David Hendricks | 83541d3 | 2014-07-15 20:58:21 -0700 | [diff] [blame] | 1808 | default: |
| 1809 | msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)" |
| 1810 | ", aborting\n", __func__, __LINE__, |
| 1811 | flash->model_id); |
| 1812 | return -1; |
| 1813 | } |
| 1814 | break; |
David Hendricks | a988485 | 2014-12-11 15:31:12 -0800 | [diff] [blame] | 1815 | case SPANSION_ID: |
| 1816 | switch (flash->model_id) { |
| 1817 | case SPANSION_S25FS128S_L: |
| 1818 | case SPANSION_S25FS128S_S: { |
David Hendricks | a988485 | 2014-12-11 15:31:12 -0800 | [diff] [blame] | 1819 | *wp = &s25fs128s_wp; |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1820 | (*wp)->ranges = s25fs128s_ranges; |
| 1821 | *num_entries = ARRAY_SIZE(s25fs128s_ranges); |
David Hendricks | a988485 | 2014-12-11 15:31:12 -0800 | [diff] [blame] | 1822 | break; |
| 1823 | } |
David Hendricks | c694bb8 | 2015-02-25 14:52:17 -0800 | [diff] [blame] | 1824 | case SPANSION_S25FL256S_UL: |
| 1825 | case SPANSION_S25FL256S_US: { |
David Hendricks | c694bb8 | 2015-02-25 14:52:17 -0800 | [diff] [blame] | 1826 | *wp = &s25fl256s_wp; |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1827 | (*wp)->ranges = s25fl256s_ranges; |
| 1828 | *num_entries = ARRAY_SIZE(s25fl256s_ranges); |
David Hendricks | c694bb8 | 2015-02-25 14:52:17 -0800 | [diff] [blame] | 1829 | break; |
| 1830 | } |
David Hendricks | a988485 | 2014-12-11 15:31:12 -0800 | [diff] [blame] | 1831 | default: |
| 1832 | msg_cerr("%s():%d Spansion flash chip mismatch (0x%04x)" |
| 1833 | ", aborting\n", __func__, __LINE__, flash->model_id); |
| 1834 | return -1; |
| 1835 | } |
| 1836 | break; |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 1837 | default: |
| 1838 | msg_cerr("%s: flash vendor (0x%x) not found, aborting\n", |
| 1839 | __func__, flash->manufacture_id); |
| 1840 | return -1; |
| 1841 | } |
| 1842 | |
| 1843 | return 0; |
| 1844 | } |
| 1845 | |
| 1846 | /* Given a [start, len], this function finds a block protect bit combination |
| 1847 | * (if possible) and sets the corresponding bits in "status". Remaining bits |
| 1848 | * are preserved. */ |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 1849 | static int generic_range_to_status(const struct flashctx *flash, |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 1850 | unsigned int start, unsigned int len, |
| 1851 | uint8_t *status) |
| 1852 | { |
| 1853 | struct generic_wp *wp; |
| 1854 | struct generic_range *r; |
| 1855 | int i, range_found = 0, num_entries; |
| 1856 | uint8_t bp_mask; |
| 1857 | |
| 1858 | if (generic_range_table(flash, &wp, &num_entries)) |
| 1859 | return -1; |
| 1860 | |
| 1861 | bp_mask = ((1 << (wp->sr1.bp0_pos + wp->sr1.bp_bits)) - 1) - \ |
| 1862 | ((1 << wp->sr1.bp0_pos) - 1); |
| 1863 | |
| 1864 | for (i = 0, r = &wp->ranges[0]; i < num_entries; i++, r++) { |
| 1865 | msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n", |
| 1866 | start, len, r->range.start, r->range.len); |
| 1867 | if ((start == r->range.start) && (len == r->range.len)) { |
| 1868 | *status &= ~(bp_mask); |
| 1869 | *status |= r->bp << (wp->sr1.bp0_pos); |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1870 | |
| 1871 | if (wp->set_modifier_bits) { |
| 1872 | if (wp->set_modifier_bits(flash, &r->m) < 0) { |
| 1873 | msg_cerr("error setting modifier " |
| 1874 | "bits for range.\n"); |
| 1875 | return -1; |
| 1876 | } |
| 1877 | } |
| 1878 | |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 1879 | range_found = 1; |
| 1880 | break; |
| 1881 | } |
| 1882 | } |
| 1883 | |
| 1884 | if (!range_found) { |
| 1885 | msg_cerr("matching range not found\n"); |
| 1886 | return -1; |
| 1887 | } |
| 1888 | return 0; |
| 1889 | } |
| 1890 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 1891 | static int generic_status_to_range(const struct flashctx *flash, |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 1892 | const uint8_t sr1, unsigned int *start, unsigned int *len) |
| 1893 | { |
| 1894 | struct generic_wp *wp; |
| 1895 | struct generic_range *r; |
Duncan Laurie | 04ca117 | 2015-03-12 09:25:34 -0700 | [diff] [blame] | 1896 | int num_entries, i, status_found = 0; |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 1897 | uint8_t sr1_bp; |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1898 | struct generic_modifier_bits m; |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 1899 | |
| 1900 | if (generic_range_table(flash, &wp, &num_entries)) |
| 1901 | return -1; |
| 1902 | |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1903 | /* modifier bits may be compared more than once, so get them here */ |
| 1904 | if (wp->get_modifier_bits) { |
| 1905 | if (wp->get_modifier_bits(flash, &m) < 0) |
| 1906 | return -1; |
| 1907 | } |
| 1908 | |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 1909 | sr1_bp = (sr1 >> wp->sr1.bp0_pos) & ((1 << wp->sr1.bp_bits) - 1); |
| 1910 | |
| 1911 | for (i = 0, r = &wp->ranges[0]; i < num_entries; i++, r++) { |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1912 | if (wp->get_modifier_bits) { |
| 1913 | if (memcmp(&m, &r->m, sizeof(m))) |
| 1914 | continue; |
| 1915 | } |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 1916 | msg_cspew("comparing 0x%02x 0x%02x\n", sr1_bp, r->bp); |
| 1917 | if (sr1_bp == r->bp) { |
| 1918 | *start = r->range.start; |
| 1919 | *len = r->range.len; |
| 1920 | status_found = 1; |
| 1921 | break; |
| 1922 | } |
| 1923 | } |
| 1924 | |
| 1925 | if (!status_found) { |
| 1926 | msg_cerr("matching status not found\n"); |
| 1927 | return -1; |
| 1928 | } |
| 1929 | return 0; |
| 1930 | } |
| 1931 | |
| 1932 | /* Given a [start, len], this function calls generic_range_to_status() to |
| 1933 | * convert it to flash-chip-specific range bits, then sets into status register. |
| 1934 | */ |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 1935 | static int generic_set_range(const struct flashctx *flash, |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 1936 | unsigned int start, unsigned int len) |
| 1937 | { |
| 1938 | uint8_t status, expected; |
| 1939 | |
Ramya Vijaykumar | 4af3f82 | 2016-01-27 11:51:27 +0530 | [diff] [blame] | 1940 | status = do_read_status(flash); |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 1941 | msg_cdbg("%s: old status: 0x%02x\n", __func__, status); |
| 1942 | |
| 1943 | expected = status; /* preserve non-bp bits */ |
| 1944 | if (generic_range_to_status(flash, start, len, &expected)) |
| 1945 | return -1; |
| 1946 | |
Ramya Vijaykumar | 4af3f82 | 2016-01-27 11:51:27 +0530 | [diff] [blame] | 1947 | do_write_status(flash, expected); |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 1948 | |
Ramya Vijaykumar | 4af3f82 | 2016-01-27 11:51:27 +0530 | [diff] [blame] | 1949 | status = do_read_status(flash); |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 1950 | msg_cdbg("%s: new status: 0x%02x\n", __func__, status); |
| 1951 | if (status != expected) { |
| 1952 | msg_cerr("expected=0x%02x, but actual=0x%02x.\n", |
| 1953 | expected, status); |
| 1954 | return 1; |
| 1955 | } |
| 1956 | |
| 1957 | return 0; |
| 1958 | } |
| 1959 | |
| 1960 | /* Set/clear the status regsiter write protect bit in SR1. */ |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 1961 | static int generic_set_srp0(const struct flashctx *flash, int enable) |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 1962 | { |
| 1963 | uint8_t status, expected; |
| 1964 | struct generic_wp *wp; |
| 1965 | int num_entries; |
| 1966 | |
| 1967 | if (generic_range_table(flash, &wp, &num_entries)) |
| 1968 | return -1; |
| 1969 | |
Ramya Vijaykumar | 4af3f82 | 2016-01-27 11:51:27 +0530 | [diff] [blame] | 1970 | expected = do_read_status(flash); |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 1971 | msg_cdbg("%s: old status: 0x%02x\n", __func__, expected); |
| 1972 | |
| 1973 | if (enable) |
| 1974 | expected |= 1 << wp->sr1.srp_pos; |
| 1975 | else |
| 1976 | expected &= ~(1 << wp->sr1.srp_pos); |
| 1977 | |
Ramya Vijaykumar | 4af3f82 | 2016-01-27 11:51:27 +0530 | [diff] [blame] | 1978 | do_write_status(flash, expected); |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 1979 | |
Ramya Vijaykumar | 4af3f82 | 2016-01-27 11:51:27 +0530 | [diff] [blame] | 1980 | status = do_read_status(flash); |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 1981 | msg_cdbg("%s: new status: 0x%02x\n", __func__, status); |
| 1982 | if (status != expected) |
| 1983 | return -1; |
| 1984 | |
| 1985 | return 0; |
| 1986 | } |
| 1987 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 1988 | static int generic_enable_writeprotect(const struct flashctx *flash, |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 1989 | enum wp_mode wp_mode) |
| 1990 | { |
| 1991 | int ret; |
| 1992 | |
| 1993 | switch (wp_mode) { |
| 1994 | case WP_MODE_HARDWARE: |
| 1995 | ret = generic_set_srp0(flash, 1); |
| 1996 | break; |
| 1997 | default: |
| 1998 | msg_cerr("%s(): unsupported write-protect mode\n", __func__); |
| 1999 | return 1; |
| 2000 | } |
| 2001 | |
| 2002 | if (ret) |
| 2003 | msg_cerr("%s(): error=%d.\n", __func__, ret); |
| 2004 | return ret; |
| 2005 | } |
| 2006 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 2007 | static int generic_disable_writeprotect(const struct flashctx *flash) |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 2008 | { |
| 2009 | int ret; |
| 2010 | |
| 2011 | ret = generic_set_srp0(flash, 0); |
| 2012 | if (ret) |
| 2013 | msg_cerr("%s(): error=%d.\n", __func__, ret); |
| 2014 | return ret; |
| 2015 | } |
| 2016 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 2017 | static int generic_list_ranges(const struct flashctx *flash) |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 2018 | { |
| 2019 | struct generic_wp *wp; |
| 2020 | struct generic_range *r; |
| 2021 | int i, num_entries; |
| 2022 | |
| 2023 | if (generic_range_table(flash, &wp, &num_entries)) |
| 2024 | return -1; |
| 2025 | |
| 2026 | r = &wp->ranges[0]; |
| 2027 | for (i = 0; i < num_entries; i++) { |
| 2028 | msg_cinfo("start: 0x%06x, length: 0x%06x\n", |
| 2029 | r->range.start, r->range.len); |
| 2030 | r++; |
| 2031 | } |
| 2032 | |
| 2033 | return 0; |
| 2034 | } |
| 2035 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 2036 | static int generic_wp_status(const struct flashctx *flash) |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 2037 | { |
| 2038 | uint8_t sr1; |
| 2039 | unsigned int start, len; |
| 2040 | int ret = 0; |
| 2041 | struct generic_wp *wp; |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 2042 | int num_entries, wp_en; |
| 2043 | |
| 2044 | if (generic_range_table(flash, &wp, &num_entries)) |
| 2045 | return -1; |
| 2046 | |
Ramya Vijaykumar | 4af3f82 | 2016-01-27 11:51:27 +0530 | [diff] [blame] | 2047 | sr1 = do_read_status(flash); |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 2048 | wp_en = (sr1 >> wp->sr1.srp_pos) & 1; |
| 2049 | |
| 2050 | msg_cinfo("WP: status: 0x%04x\n", sr1); |
| 2051 | msg_cinfo("WP: status.srp0: %x\n", wp_en); |
| 2052 | /* FIXME: SRP1 is not really generic, but we probably should print |
| 2053 | * it anyway to have consistent output. #legacycruft */ |
| 2054 | msg_cinfo("WP: status.srp1: %x\n", 0); |
| 2055 | msg_cinfo("WP: write protect is %s.\n", |
| 2056 | wp_en ? "enabled" : "disabled"); |
| 2057 | |
| 2058 | msg_cinfo("WP: write protect range: "); |
| 2059 | if (generic_status_to_range(flash, sr1, &start, &len)) { |
| 2060 | msg_cinfo("(cannot resolve the range)\n"); |
| 2061 | ret = -1; |
| 2062 | } else { |
| 2063 | msg_cinfo("start=0x%08x, len=0x%08x\n", start, len); |
| 2064 | } |
| 2065 | |
| 2066 | return ret; |
| 2067 | } |
| 2068 | |
| 2069 | struct wp wp_generic = { |
| 2070 | .list_ranges = generic_list_ranges, |
| 2071 | .set_range = generic_set_range, |
| 2072 | .enable = generic_enable_writeprotect, |
| 2073 | .disable = generic_disable_writeprotect, |
| 2074 | .wp_status = generic_wp_status, |
| 2075 | }; |