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 | */ |
| 77 | int (*get_modifier_bits)(const struct flashchip *flash, |
| 78 | struct generic_modifier_bits *m); |
| 79 | int (*set_modifier_bits)(const struct flashchip *flash, |
| 80 | struct generic_modifier_bits *m); |
David 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} }, |
| 620 | }; |
| 621 | |
Louis Yung-Chieh Lo | 232951f | 2010-09-16 11:30:00 +0800 | [diff] [blame] | 622 | struct w25q_range w25x80_ranges[] = { |
| 623 | { X, X, 0, {0, 0} }, /* none */ |
| 624 | { 0, 0, 0x1, {0x0F0000, 64 * 1024} }, |
| 625 | { 0, 0, 0x2, {0x0E0000, 128 * 1024} }, |
| 626 | { 0, 0, 0x3, {0x0C0000, 256 * 1024} }, |
| 627 | { 0, 0, 0x4, {0x080000, 512 * 1024} }, |
| 628 | { 0, 1, 0x1, {0x000000, 64 * 1024} }, |
| 629 | { 0, 1, 0x2, {0x000000, 128 * 1024} }, |
| 630 | { 0, 1, 0x3, {0x000000, 256 * 1024} }, |
| 631 | { 0, 1, 0x4, {0x000000, 512 * 1024} }, |
| 632 | { 0, X, 0x5, {0x000000, 1024 * 1024} }, |
| 633 | { 0, X, 0x6, {0x000000, 1024 * 1024} }, |
| 634 | { 0, X, 0x7, {0x000000, 1024 * 1024} }, |
| 635 | }; |
| 636 | |
Shawn Nematbakhsh | 9e8ef49 | 2012-09-01 21:58:03 -0700 | [diff] [blame] | 637 | static struct w25q_range gd25q64_ranges[] = { |
| 638 | { X, X, 0, {0, 0} }, /* none */ |
| 639 | { 0, 0, 0x1, {0x7e0000, 128 * 1024} }, |
| 640 | { 0, 0, 0x2, {0x7c0000, 256 * 1024} }, |
| 641 | { 0, 0, 0x3, {0x780000, 512 * 1024} }, |
| 642 | { 0, 0, 0x4, {0x700000, 1024 * 1024} }, |
| 643 | { 0, 0, 0x5, {0x600000, 2048 * 1024} }, |
| 644 | { 0, 0, 0x6, {0x400000, 4096 * 1024} }, |
| 645 | |
| 646 | { 0, 1, 0x1, {0x000000, 128 * 1024} }, |
| 647 | { 0, 1, 0x2, {0x000000, 256 * 1024} }, |
| 648 | { 0, 1, 0x3, {0x000000, 512 * 1024} }, |
| 649 | { 0, 1, 0x4, {0x000000, 1024 * 1024} }, |
| 650 | { 0, 1, 0x5, {0x000000, 2048 * 1024} }, |
| 651 | { 0, 1, 0x6, {0x000000, 4096 * 1024} }, |
| 652 | { X, X, 0x7, {0x000000, 8192 * 1024} }, |
| 653 | |
| 654 | { 1, 0, 0x1, {0x7ff000, 4 * 1024} }, |
| 655 | { 1, 0, 0x2, {0x7fe000, 8 * 1024} }, |
| 656 | { 1, 0, 0x3, {0x7fc000, 16 * 1024} }, |
| 657 | { 1, 0, 0x4, {0x7f8000, 32 * 1024} }, |
| 658 | { 1, 0, 0x5, {0x7f8000, 32 * 1024} }, |
| 659 | { 1, 0, 0x6, {0x7f8000, 32 * 1024} }, |
| 660 | |
| 661 | { 1, 1, 0x1, {0x000000, 4 * 1024} }, |
| 662 | { 1, 1, 0x2, {0x000000, 8 * 1024} }, |
| 663 | { 1, 1, 0x3, {0x000000, 16 * 1024} }, |
| 664 | { 1, 1, 0x4, {0x000000, 32 * 1024} }, |
| 665 | { 1, 1, 0x5, {0x000000, 32 * 1024} }, |
| 666 | { 1, 1, 0x6, {0x000000, 32 * 1024} }, |
| 667 | }; |
| 668 | |
Louis Yung-Chieh Lo | c8ec715 | 2012-09-17 17:38:35 +0800 | [diff] [blame] | 669 | static struct w25q_range a25l040_ranges[] = { |
| 670 | { X, X, 0x0, {0, 0} }, /* none */ |
| 671 | { X, X, 0x1, {0x70000, 64 * 1024} }, |
| 672 | { X, X, 0x2, {0x60000, 128 * 1024} }, |
| 673 | { X, X, 0x3, {0x40000, 256 * 1024} }, |
| 674 | { X, X, 0x4, {0x00000, 512 * 1024} }, |
| 675 | { X, X, 0x5, {0x00000, 512 * 1024} }, |
| 676 | { X, X, 0x6, {0x00000, 512 * 1024} }, |
| 677 | { X, X, 0x7, {0x00000, 512 * 1024} }, |
| 678 | }; |
| 679 | |
Duncan Laurie | ed32d7b | 2015-05-27 11:28:18 -0700 | [diff] [blame^] | 680 | /* FIXME: Move to spi25.c if it's a JEDEC standard opcode */ |
| 681 | static uint8_t w25q_read_status_register_2(void) |
| 682 | { |
| 683 | static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { 0x35 }; |
| 684 | unsigned char readarr[2]; |
| 685 | int ret; |
| 686 | |
| 687 | /* Read Status Register */ |
| 688 | ret = spi_send_command(sizeof(cmd), sizeof(readarr), cmd, readarr); |
| 689 | if (ret) { |
| 690 | /* |
| 691 | * FIXME: make this a benign failure for now in case we are |
| 692 | * unable to execute the opcode |
| 693 | */ |
| 694 | msg_cdbg("RDSR2 failed!\n"); |
| 695 | readarr[0] = 0x00; |
| 696 | } |
| 697 | |
| 698 | return readarr[0]; |
| 699 | } |
| 700 | |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 701 | /* Given a flash chip, this function returns its range table. */ |
| 702 | static int w25_range_table(const struct flashchip *flash, |
| 703 | struct w25q_range **w25q_ranges, |
| 704 | int *num_entries) |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 705 | { |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 706 | *w25q_ranges = 0; |
| 707 | *num_entries = 0; |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 708 | |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 709 | switch (flash->manufacture_id) { |
| 710 | case WINBOND_NEX_ID: |
| 711 | switch(flash->model_id) { |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 712 | case WINBOND_NEX_W25X10: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 713 | *w25q_ranges = w25x10_ranges; |
| 714 | *num_entries = ARRAY_SIZE(w25x10_ranges); |
Louis Yung-Chieh Lo | 232951f | 2010-09-16 11:30:00 +0800 | [diff] [blame] | 715 | break; |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 716 | case WINBOND_NEX_W25X20: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 717 | *w25q_ranges = w25x20_ranges; |
| 718 | *num_entries = ARRAY_SIZE(w25x20_ranges); |
Louis Yung-Chieh Lo | 232951f | 2010-09-16 11:30:00 +0800 | [diff] [blame] | 719 | break; |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 720 | case WINBOND_NEX_W25X40: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 721 | *w25q_ranges = w25x40_ranges; |
| 722 | *num_entries = ARRAY_SIZE(w25x40_ranges); |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 723 | break; |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 724 | case WINBOND_NEX_W25X80: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 725 | *w25q_ranges = w25x80_ranges; |
| 726 | *num_entries = ARRAY_SIZE(w25x80_ranges); |
Louis Yung-Chieh Lo | 232951f | 2010-09-16 11:30:00 +0800 | [diff] [blame] | 727 | break; |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 728 | case WINBOND_NEX_W25Q80: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 729 | *w25q_ranges = w25q80_ranges; |
| 730 | *num_entries = ARRAY_SIZE(w25q80_ranges); |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 731 | break; |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 732 | case WINBOND_NEX_W25Q16: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 733 | *w25q_ranges = w25q16_ranges; |
| 734 | *num_entries = ARRAY_SIZE(w25q16_ranges); |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 735 | break; |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 736 | case WINBOND_NEX_W25Q32: |
Louis Yung-Chieh Lo | 469707f | 2012-05-18 16:38:37 +0800 | [diff] [blame] | 737 | case WINBOND_NEX_W25Q32DW: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 738 | *w25q_ranges = w25q32_ranges; |
| 739 | *num_entries = ARRAY_SIZE(w25q32_ranges); |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 740 | break; |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 741 | case WINBOND_NEX_W25Q64: |
AdamTsai | 141a262 | 2013-12-31 14:07:15 +0800 | [diff] [blame] | 742 | case WINBOND_NEX_W25Q64DW: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 743 | *w25q_ranges = w25q64_ranges; |
| 744 | *num_entries = ARRAY_SIZE(w25q64_ranges); |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 745 | break; |
Duncan Laurie | ed32d7b | 2015-05-27 11:28:18 -0700 | [diff] [blame^] | 746 | case WINBOND_NEX_W25Q128: |
| 747 | if (w25q_read_status_register_2() & (1 << 6)) { |
| 748 | /* CMP == 1 */ |
| 749 | *w25q_ranges = w25rq128_cmp1_ranges; |
| 750 | *num_entries = ARRAY_SIZE(w25rq128_cmp1_ranges); |
| 751 | } else { |
| 752 | /* CMP == 0 */ |
| 753 | *w25q_ranges = w25rq128_cmp0_ranges; |
| 754 | *num_entries = ARRAY_SIZE(w25rq128_cmp0_ranges); |
| 755 | } |
Ramya Vijaykumar | e6a7ca8 | 2015-05-12 14:27:29 +0530 | [diff] [blame] | 756 | break; |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 757 | default: |
| 758 | msg_cerr("%s() %d: WINBOND flash chip mismatch (0x%04x)" |
| 759 | ", aborting\n", __func__, __LINE__, |
| 760 | flash->model_id); |
| 761 | return -1; |
| 762 | } |
David Hendricks | 2c4a76c | 2010-06-28 14:00:43 -0700 | [diff] [blame] | 763 | break; |
David Hendricks | 57566ed | 2010-08-16 18:24:45 -0700 | [diff] [blame] | 764 | case EON_ID_NOPREFIX: |
| 765 | switch (flash->model_id) { |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 766 | case EON_EN25F40: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 767 | *w25q_ranges = en25f40_ranges; |
| 768 | *num_entries = ARRAY_SIZE(en25f40_ranges); |
David Hendricks | 57566ed | 2010-08-16 18:24:45 -0700 | [diff] [blame] | 769 | break; |
David Hendricks | e185bf2 | 2011-05-24 15:34:18 -0700 | [diff] [blame] | 770 | case EON_EN25Q40: |
| 771 | *w25q_ranges = en25q40_ranges; |
| 772 | *num_entries = ARRAY_SIZE(en25q40_ranges); |
| 773 | break; |
| 774 | case EON_EN25Q80: |
| 775 | *w25q_ranges = en25q80_ranges; |
| 776 | *num_entries = ARRAY_SIZE(en25q80_ranges); |
| 777 | break; |
| 778 | case EON_EN25Q32: |
| 779 | *w25q_ranges = en25q32_ranges; |
| 780 | *num_entries = ARRAY_SIZE(en25q32_ranges); |
| 781 | break; |
| 782 | case EON_EN25Q64: |
| 783 | *w25q_ranges = en25q64_ranges; |
| 784 | *num_entries = ARRAY_SIZE(en25q64_ranges); |
| 785 | break; |
| 786 | case EON_EN25Q128: |
| 787 | *w25q_ranges = en25q128_ranges; |
| 788 | *num_entries = ARRAY_SIZE(en25q128_ranges); |
| 789 | break; |
Marc Jones | b2f9002 | 2014-04-29 17:37:23 -0600 | [diff] [blame] | 790 | case EON_EN25S64: |
| 791 | *w25q_ranges = en25s64_ranges; |
| 792 | *num_entries = ARRAY_SIZE(en25s64_ranges); |
| 793 | break; |
David Hendricks | 57566ed | 2010-08-16 18:24:45 -0700 | [diff] [blame] | 794 | default: |
| 795 | msg_cerr("%s():%d: EON flash chip mismatch (0x%04x)" |
| 796 | ", aborting\n", __func__, __LINE__, |
| 797 | flash->model_id); |
| 798 | return -1; |
| 799 | } |
| 800 | break; |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 801 | case MACRONIX_ID: |
David Hendricks | ac72e36 | 2010-08-16 18:20:03 -0700 | [diff] [blame] | 802 | switch (flash->model_id) { |
David Hendricks | f8f00c7 | 2011-02-01 12:39:46 -0800 | [diff] [blame] | 803 | case MACRONIX_MX25L1005: |
| 804 | *w25q_ranges = mx25l1005_ranges; |
| 805 | *num_entries = ARRAY_SIZE(mx25l1005_ranges); |
| 806 | break; |
| 807 | case MACRONIX_MX25L2005: |
| 808 | *w25q_ranges = mx25l2005_ranges; |
| 809 | *num_entries = ARRAY_SIZE(mx25l2005_ranges); |
| 810 | break; |
| 811 | case MACRONIX_MX25L4005: |
| 812 | *w25q_ranges = mx25l4005_ranges; |
| 813 | *num_entries = ARRAY_SIZE(mx25l4005_ranges); |
| 814 | break; |
| 815 | case MACRONIX_MX25L8005: |
| 816 | *w25q_ranges = mx25l8005_ranges; |
| 817 | *num_entries = ARRAY_SIZE(mx25l8005_ranges); |
| 818 | break; |
| 819 | case MACRONIX_MX25L1605: |
| 820 | /* FIXME: MX25L1605 and MX25L1605D have different write |
| 821 | * protection capabilities, but share IDs */ |
| 822 | *w25q_ranges = mx25l1605d_ranges; |
| 823 | *num_entries = ARRAY_SIZE(mx25l1605d_ranges); |
| 824 | break; |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 825 | case MACRONIX_MX25L3205: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 826 | *w25q_ranges = mx25l3205d_ranges; |
| 827 | *num_entries = ARRAY_SIZE(mx25l3205d_ranges); |
David Hendricks | ac72e36 | 2010-08-16 18:20:03 -0700 | [diff] [blame] | 828 | break; |
Vincent Palatin | 87e092a | 2013-02-28 15:46:14 -0800 | [diff] [blame] | 829 | case MACRONIX_MX25U3235E: |
| 830 | *w25q_ranges = mx25u3235e_ranges; |
| 831 | *num_entries = ARRAY_SIZE(mx25u3235e_ranges); |
| 832 | break; |
Jongpil | 66a9649 | 2014-08-14 17:59:06 +0900 | [diff] [blame] | 833 | case MACRONIX_MX25U6435E: |
| 834 | *w25q_ranges = mx25u6435e_ranges; |
| 835 | *num_entries = ARRAY_SIZE(mx25u6435e_ranges); |
| 836 | break; |
David Hendricks | ac72e36 | 2010-08-16 18:20:03 -0700 | [diff] [blame] | 837 | default: |
| 838 | msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)" |
| 839 | ", aborting\n", __func__, __LINE__, |
| 840 | flash->model_id); |
| 841 | return -1; |
| 842 | } |
| 843 | break; |
David Hendricks | bfa624b | 2012-07-24 12:47:59 -0700 | [diff] [blame] | 844 | case ST_ID: |
| 845 | switch(flash->model_id) { |
| 846 | case ST_N25Q064__1E: |
| 847 | case ST_N25Q064__3E: |
| 848 | *w25q_ranges = n25q064_ranges; |
| 849 | *num_entries = ARRAY_SIZE(n25q064_ranges); |
| 850 | break; |
| 851 | default: |
| 852 | msg_cerr("%s() %d: Micron flash chip mismatch" |
| 853 | " (0x%04x), aborting\n", __func__, __LINE__, |
| 854 | flash->model_id); |
| 855 | return -1; |
| 856 | } |
| 857 | break; |
Bryan Freed | 9a0051f | 2012-05-22 16:06:09 -0700 | [diff] [blame] | 858 | case GIGADEVICE_ID: |
| 859 | switch(flash->model_id) { |
| 860 | case GIGADEVICE_GD25LQ32: |
| 861 | *w25q_ranges = w25q32_ranges; |
| 862 | *num_entries = ARRAY_SIZE(w25q32_ranges); |
| 863 | break; |
Shawn Nematbakhsh | 9e8ef49 | 2012-09-01 21:58:03 -0700 | [diff] [blame] | 864 | case GIGADEVICE_GD25Q64: |
Marc Jones | b18734f | 2014-04-03 16:19:47 -0600 | [diff] [blame] | 865 | case GIGADEVICE_GD25LQ64: |
Shawn Nematbakhsh | 9e8ef49 | 2012-09-01 21:58:03 -0700 | [diff] [blame] | 866 | *w25q_ranges = gd25q64_ranges; |
| 867 | *num_entries = ARRAY_SIZE(gd25q64_ranges); |
| 868 | break; |
| 869 | /* TODO(shawnn): add support for other GD parts */ |
Bryan Freed | 9a0051f | 2012-05-22 16:06:09 -0700 | [diff] [blame] | 870 | default: |
| 871 | msg_cerr("%s() %d: GigaDevice flash chip mismatch" |
| 872 | " (0x%04x), aborting\n", __func__, __LINE__, |
| 873 | flash->model_id); |
| 874 | return -1; |
| 875 | } |
| 876 | break; |
Louis Yung-Chieh Lo | c8ec715 | 2012-09-17 17:38:35 +0800 | [diff] [blame] | 877 | case AMIC_ID_NOPREFIX: |
| 878 | switch(flash->model_id) { |
| 879 | case AMIC_A25L040: |
| 880 | *w25q_ranges = a25l040_ranges; |
| 881 | *num_entries = ARRAY_SIZE(a25l040_ranges); |
| 882 | break; |
| 883 | default: |
| 884 | msg_cerr("%s() %d: AMIC flash chip mismatch" |
| 885 | " (0x%04x), aborting\n", __func__, __LINE__, |
| 886 | flash->model_id); |
| 887 | return -1; |
| 888 | } |
| 889 | break; |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 890 | default: |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 891 | msg_cerr("%s: flash vendor (0x%x) not found, aborting\n", |
| 892 | __func__, flash->manufacture_id); |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 893 | return -1; |
| 894 | } |
| 895 | |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 896 | return 0; |
| 897 | } |
| 898 | |
| 899 | int w25_range_to_status(const struct flashchip *flash, |
| 900 | unsigned int start, unsigned int len, |
| 901 | struct w25q_status *status) |
| 902 | { |
| 903 | struct w25q_range *w25q_ranges; |
| 904 | int i, range_found = 0; |
| 905 | int num_entries; |
| 906 | |
| 907 | if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1; |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 908 | for (i = 0; i < num_entries; i++) { |
| 909 | struct wp_range *r = &w25q_ranges[i].range; |
| 910 | |
| 911 | msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n", |
| 912 | start, len, r->start, r->len); |
| 913 | if ((start == r->start) && (len == r->len)) { |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 914 | status->bp0 = w25q_ranges[i].bp & 1; |
| 915 | status->bp1 = w25q_ranges[i].bp >> 1; |
| 916 | status->bp2 = w25q_ranges[i].bp >> 2; |
| 917 | status->tb = w25q_ranges[i].tb; |
| 918 | status->sec = w25q_ranges[i].sec; |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 919 | |
| 920 | range_found = 1; |
| 921 | break; |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | if (!range_found) { |
| 926 | msg_cerr("matching range not found\n"); |
| 927 | return -1; |
| 928 | } |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 929 | return 0; |
| 930 | } |
| 931 | |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 932 | int w25_status_to_range(const struct flashchip *flash, |
| 933 | const struct w25q_status *status, |
| 934 | unsigned int *start, unsigned int *len) |
| 935 | { |
| 936 | struct w25q_range *w25q_ranges; |
| 937 | int i, status_found = 0; |
| 938 | int num_entries; |
| 939 | |
| 940 | if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1; |
| 941 | for (i = 0; i < num_entries; i++) { |
| 942 | int bp; |
Louis Yung-Chieh Lo | edd3930 | 2011-11-10 15:43:06 +0800 | [diff] [blame] | 943 | int table_bp, table_tb, table_sec; |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 944 | |
| 945 | bp = status->bp0 | (status->bp1 << 1) | (status->bp2 << 2); |
| 946 | msg_cspew("comparing 0x%x 0x%x / 0x%x 0x%x / 0x%x 0x%x\n", |
| 947 | bp, w25q_ranges[i].bp, |
| 948 | status->tb, w25q_ranges[i].tb, |
| 949 | status->sec, w25q_ranges[i].sec); |
Louis Yung-Chieh Lo | edd3930 | 2011-11-10 15:43:06 +0800 | [diff] [blame] | 950 | table_bp = w25q_ranges[i].bp; |
| 951 | table_tb = w25q_ranges[i].tb; |
| 952 | table_sec = w25q_ranges[i].sec; |
| 953 | if ((bp == table_bp || table_bp == X) && |
| 954 | (status->tb == table_tb || table_tb == X) && |
| 955 | (status->sec == table_sec || table_sec == X)) { |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 956 | *start = w25q_ranges[i].range.start; |
| 957 | *len = w25q_ranges[i].range.len; |
| 958 | |
| 959 | status_found = 1; |
| 960 | break; |
| 961 | } |
| 962 | } |
| 963 | |
| 964 | if (!status_found) { |
| 965 | msg_cerr("matching status not found\n"); |
| 966 | return -1; |
| 967 | } |
| 968 | return 0; |
| 969 | } |
| 970 | |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 971 | /* Given a [start, len], this function calls w25_range_to_status() to convert |
| 972 | * it to flash-chip-specific range bits, then sets into status register. |
| 973 | */ |
David Hendricks | 9104083 | 2011-07-08 20:01:09 -0700 | [diff] [blame] | 974 | static int w25_set_range(const struct flashchip *flash, |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 975 | unsigned int start, unsigned int len) |
| 976 | { |
| 977 | struct w25q_status status; |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 978 | int tmp = 0; |
| 979 | int expected = 0; |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 980 | |
| 981 | memset(&status, 0, sizeof(status)); |
| 982 | tmp = spi_read_status_register(); |
| 983 | memcpy(&status, &tmp, 1); |
| 984 | msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp); |
| 985 | |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 986 | if (w25_range_to_status(flash, start, len, &status)) return -1; |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 987 | |
| 988 | msg_cdbg("status.busy: %x\n", status.busy); |
| 989 | msg_cdbg("status.wel: %x\n", status.wel); |
| 990 | msg_cdbg("status.bp0: %x\n", status.bp0); |
| 991 | msg_cdbg("status.bp1: %x\n", status.bp1); |
| 992 | msg_cdbg("status.bp2: %x\n", status.bp2); |
| 993 | msg_cdbg("status.tb: %x\n", status.tb); |
| 994 | msg_cdbg("status.sec: %x\n", status.sec); |
| 995 | msg_cdbg("status.srp0: %x\n", status.srp0); |
| 996 | |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 997 | memcpy(&expected, &status, sizeof(status)); |
David Hendricks | 6082404 | 2014-12-11 17:22:06 -0800 | [diff] [blame] | 998 | spi_write_status_register(flash, expected); |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 999 | |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 1000 | tmp = spi_read_status_register(); |
| 1001 | msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp); |
| 1002 | if ((tmp & MASK_WP_AREA) == (expected & MASK_WP_AREA)) { |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 1003 | return 0; |
| 1004 | } else { |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 1005 | msg_cerr("expected=0x%02x, but actual=0x%02x.\n", |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 1006 | expected, tmp); |
| 1007 | return 1; |
| 1008 | } |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 1009 | } |
Ramya Vijaykumar | e6a7ca8 | 2015-05-12 14:27:29 +0530 | [diff] [blame] | 1010 | static int w25r_set_range(const struct flashchip *flash, |
| 1011 | unsigned int start, unsigned int len) |
| 1012 | { |
| 1013 | struct w25q_status status; |
| 1014 | struct flashchip chip; |
| 1015 | uint8_t arr, expected; |
| 1016 | int ret; |
| 1017 | |
| 1018 | memset(&status, 0, sizeof(status)); |
| 1019 | memset(&chip, 0, sizeof(chip)); |
| 1020 | memcpy(&chip, flash, sizeof(chip)); |
| 1021 | |
| 1022 | /* passing a copy of flash since it is read only */ |
| 1023 | ret = flash->read(&chip, &arr, 0, 1); |
| 1024 | if (ret) { |
| 1025 | msg_cerr("Read status register failed.\n"); |
| 1026 | return ret; |
| 1027 | } |
| 1028 | memcpy(&status, &arr, 1); |
| 1029 | msg_cdbg("%s: old status: 0x%02x\n", __func__, arr); |
| 1030 | |
| 1031 | if (w25_range_to_status(flash, start, len, &status)) |
| 1032 | return -1; |
| 1033 | |
| 1034 | msg_cdbg("status.busy: %x\n", status.busy); |
| 1035 | msg_cdbg("status.wel: %x\n", status.wel); |
| 1036 | msg_cdbg("status.bp0: %x\n", status.bp0); |
| 1037 | msg_cdbg("status.bp1: %x\n", status.bp1); |
| 1038 | msg_cdbg("status.bp2: %x\n", status.bp2); |
| 1039 | msg_cdbg("status.tb: %x\n", status.tb); |
| 1040 | msg_cdbg("status.sec: %x\n", status.sec); |
| 1041 | msg_cdbg("status.srp0: %x\n", status.srp0); |
| 1042 | |
| 1043 | memcpy(&expected, &status, sizeof(status)); |
| 1044 | ret = flash->write(&chip, &expected, 0, 1); |
| 1045 | if (ret) { |
| 1046 | msg_cerr("Write status register failed.\n"); |
| 1047 | return ret; |
| 1048 | } |
| 1049 | ret = flash->read(&chip, &arr, 0, 1); |
| 1050 | if (ret) { |
| 1051 | msg_cerr("Read status register failed.\n"); |
| 1052 | return ret; |
| 1053 | } |
| 1054 | msg_cdbg("%s: new status: 0x%02x\n", __func__, arr); |
| 1055 | |
| 1056 | if ((arr & MASK_WP_AREA) == (expected & MASK_WP_AREA)) { |
| 1057 | return 0; |
| 1058 | } else { |
| 1059 | msg_cerr("expected=0x%02x, but actual=0x%02x.\n", |
| 1060 | expected, arr); |
| 1061 | return 1; |
| 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | static int w25r_wp_status(const struct flashchip *flash) |
| 1066 | { |
| 1067 | struct w25q_status sr; |
| 1068 | struct flashchip chip; |
| 1069 | uint8_t tmp; |
| 1070 | unsigned int start, len; |
| 1071 | int ret = 0; |
| 1072 | |
| 1073 | memset(&sr, 0, sizeof(sr)); |
| 1074 | memset(&chip, 0, sizeof(chip)); |
| 1075 | memcpy(&chip, flash, sizeof(chip)); |
| 1076 | |
| 1077 | ret = flash->read(&chip, &tmp, 0, 1); |
| 1078 | if (ret) { |
| 1079 | msg_cerr("Read status register failed.\n"); |
| 1080 | return ret; |
| 1081 | } |
| 1082 | memcpy(&sr, &tmp, 1); |
| 1083 | msg_cinfo("WP: status: 0x%02x\n", tmp); |
| 1084 | msg_cinfo("WP: status.srp0: %x\n", sr.srp0); |
| 1085 | msg_cinfo("WP: write protect is %s.\n", |
| 1086 | (sr.srp0) ? "enabled" : "disabled"); |
| 1087 | msg_cinfo("WP: write protect range: "); |
| 1088 | if (w25_status_to_range(flash, &sr, &start, &len)) { |
| 1089 | msg_cinfo("(cannot resolve the range)\n"); |
| 1090 | ret = -1; |
| 1091 | } else { |
| 1092 | msg_cinfo("start=0x%08x, len=0x%08x\n", start, len); |
| 1093 | } |
| 1094 | return ret; |
| 1095 | } |
| 1096 | |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 1097 | |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 1098 | /* Print out the current status register value with human-readable text. */ |
David Hendricks | 9104083 | 2011-07-08 20:01:09 -0700 | [diff] [blame] | 1099 | static int w25_wp_status(const struct flashchip *flash) |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 1100 | { |
| 1101 | struct w25q_status status; |
| 1102 | int tmp; |
David Hendricks | ce8ded3 | 2010-10-08 11:23:38 -0700 | [diff] [blame] | 1103 | unsigned int start, len; |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 1104 | int ret = 0; |
| 1105 | |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 1106 | memset(&status, 0, sizeof(status)); |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 1107 | tmp = spi_read_status_register(); |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 1108 | memcpy(&status, &tmp, 1); |
| 1109 | msg_cinfo("WP: status: 0x%02x\n", tmp); |
| 1110 | msg_cinfo("WP: status.srp0: %x\n", status.srp0); |
| 1111 | msg_cinfo("WP: write protect is %s.\n", |
| 1112 | status.srp0 ? "enabled" : "disabled"); |
| 1113 | |
| 1114 | msg_cinfo("WP: write protect range: "); |
| 1115 | if (w25_status_to_range(flash, &status, &start, &len)) { |
| 1116 | msg_cinfo("(cannot resolve the range)\n"); |
| 1117 | ret = -1; |
| 1118 | } else { |
| 1119 | msg_cinfo("start=0x%08x, len=0x%08x\n", start, len); |
| 1120 | } |
| 1121 | |
| 1122 | return ret; |
| 1123 | } |
| 1124 | |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 1125 | /* Set/clear the SRP0 bit in the status register. */ |
David Hendricks | 9104083 | 2011-07-08 20:01:09 -0700 | [diff] [blame] | 1126 | static int w25_set_srp0(const struct flashchip *flash, int enable) |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 1127 | { |
| 1128 | struct w25q_status status; |
| 1129 | int tmp = 0; |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 1130 | int expected = 0; |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 1131 | |
| 1132 | memset(&status, 0, sizeof(status)); |
| 1133 | tmp = spi_read_status_register(); |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 1134 | /* FIXME: this is NOT endian-free copy. */ |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 1135 | memcpy(&status, &tmp, 1); |
| 1136 | msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp); |
| 1137 | |
Louis Yung-Chieh Lo | c19d3c5 | 2010-10-08 11:59:16 +0800 | [diff] [blame] | 1138 | status.srp0 = enable ? 1 : 0; |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 1139 | memcpy(&expected, &status, sizeof(status)); |
David Hendricks | 6082404 | 2014-12-11 17:22:06 -0800 | [diff] [blame] | 1140 | spi_write_status_register(flash, expected); |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 1141 | |
| 1142 | tmp = spi_read_status_register(); |
| 1143 | msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp); |
| 1144 | if ((tmp & MASK_WP_AREA) != (expected & MASK_WP_AREA)) |
| 1145 | return 1; |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 1146 | |
| 1147 | return 0; |
| 1148 | } |
| 1149 | |
Ramya Vijaykumar | e6a7ca8 | 2015-05-12 14:27:29 +0530 | [diff] [blame] | 1150 | static int w25_set_srp(const struct flashchip *flash, int enable) |
| 1151 | { |
| 1152 | struct w25q_status status; |
| 1153 | struct flashchip chip; |
| 1154 | int tmp = 0; |
| 1155 | uint8_t arr, expected; |
| 1156 | |
| 1157 | memset(&status, 0, sizeof(status)); |
| 1158 | memset(&chip, 0, sizeof(chip)); |
| 1159 | memcpy(&chip, flash, sizeof(chip)); |
| 1160 | |
| 1161 | tmp = flash->read(&chip, &arr, 0, 1); |
| 1162 | if (tmp) { |
| 1163 | msg_cerr("Read status register failed.\n"); |
| 1164 | return tmp; |
| 1165 | } |
| 1166 | memcpy(&status, &arr, 1); |
| 1167 | msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp); |
| 1168 | |
| 1169 | status.srp0 = enable ? 1 : 0; |
| 1170 | memcpy(&expected, &status, sizeof(status)); |
| 1171 | tmp = flash->write(&chip, &expected, 0, 1); |
| 1172 | if (tmp) { |
| 1173 | msg_cerr("Write status register failed.\n"); |
| 1174 | return tmp; |
| 1175 | } |
| 1176 | tmp = flash->read(&chip, &arr, 0, 1); |
| 1177 | if (tmp) { |
| 1178 | msg_cerr("Read status register failed.\n"); |
| 1179 | return tmp; |
| 1180 | } |
| 1181 | msg_cdbg("%s: new status: 0x%02x\n", __func__, arr); |
| 1182 | if ((arr & MASK_WP_AREA) != (expected & MASK_WP_AREA)) |
| 1183 | return 1; |
| 1184 | |
| 1185 | return 0; |
| 1186 | } |
| 1187 | |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1188 | static int w25_enable_writeprotect(const struct flashchip *flash, |
| 1189 | enum wp_mode wp_mode) |
Louis Yung-Chieh Lo | c19d3c5 | 2010-10-08 11:59:16 +0800 | [diff] [blame] | 1190 | { |
| 1191 | int ret; |
| 1192 | |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1193 | switch (wp_mode) { |
| 1194 | case WP_MODE_HARDWARE: |
| 1195 | ret = w25_set_srp0(flash, 1); |
| 1196 | break; |
| 1197 | default: |
| 1198 | msg_cerr("%s(): unsupported write-protect mode\n", __func__); |
| 1199 | return 1; |
| 1200 | } |
| 1201 | |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 1202 | if (ret) |
| 1203 | msg_cerr("%s(): error=%d.\n", __func__, ret); |
Louis Yung-Chieh Lo | c19d3c5 | 2010-10-08 11:59:16 +0800 | [diff] [blame] | 1204 | return ret; |
| 1205 | } |
| 1206 | |
David Hendricks | 9104083 | 2011-07-08 20:01:09 -0700 | [diff] [blame] | 1207 | static int w25_disable_writeprotect(const struct flashchip *flash) |
Louis Yung-Chieh Lo | c19d3c5 | 2010-10-08 11:59:16 +0800 | [diff] [blame] | 1208 | { |
| 1209 | int ret; |
| 1210 | |
| 1211 | ret = w25_set_srp0(flash, 0); |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 1212 | if (ret) |
| 1213 | msg_cerr("%s(): error=%d.\n", __func__, ret); |
Louis Yung-Chieh Lo | c19d3c5 | 2010-10-08 11:59:16 +0800 | [diff] [blame] | 1214 | return ret; |
| 1215 | } |
| 1216 | |
David Hendricks | 9104083 | 2011-07-08 20:01:09 -0700 | [diff] [blame] | 1217 | static int w25_list_ranges(const struct flashchip *flash) |
David Hendricks | 0f7f538 | 2011-02-11 18:12:31 -0800 | [diff] [blame] | 1218 | { |
| 1219 | struct w25q_range *w25q_ranges; |
| 1220 | int i, num_entries; |
| 1221 | |
| 1222 | if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1; |
| 1223 | for (i = 0; i < num_entries; i++) { |
| 1224 | msg_cinfo("start: 0x%06x, length: 0x%06x\n", |
| 1225 | w25q_ranges[i].range.start, |
| 1226 | w25q_ranges[i].range.len); |
| 1227 | } |
| 1228 | |
| 1229 | return 0; |
| 1230 | } |
| 1231 | |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1232 | static int w25q_wp_status(const struct flashchip *flash) |
| 1233 | { |
| 1234 | struct w25q_status sr1; |
| 1235 | struct w25q_status_2 sr2; |
David Hendricks | f1bd880 | 2012-10-30 11:37:57 -0700 | [diff] [blame] | 1236 | uint8_t tmp[2]; |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1237 | unsigned int start, len; |
| 1238 | int ret = 0; |
| 1239 | |
| 1240 | memset(&sr1, 0, sizeof(sr1)); |
David Hendricks | f1bd880 | 2012-10-30 11:37:57 -0700 | [diff] [blame] | 1241 | tmp[0] = spi_read_status_register(); |
| 1242 | memcpy(&sr1, &tmp[0], 1); |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1243 | |
David Hendricks | f1bd880 | 2012-10-30 11:37:57 -0700 | [diff] [blame] | 1244 | memset(&sr2, 0, sizeof(sr2)); |
| 1245 | tmp[1] = w25q_read_status_register_2(); |
| 1246 | memcpy(&sr2, &tmp[1], 1); |
| 1247 | |
| 1248 | msg_cinfo("WP: status: 0x%02x%02x\n", tmp[1], tmp[0]); |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1249 | msg_cinfo("WP: status.srp0: %x\n", sr1.srp0); |
| 1250 | msg_cinfo("WP: status.srp1: %x\n", sr2.srp1); |
| 1251 | msg_cinfo("WP: write protect is %s.\n", |
| 1252 | (sr1.srp0 || sr2.srp1) ? "enabled" : "disabled"); |
| 1253 | |
| 1254 | msg_cinfo("WP: write protect range: "); |
| 1255 | if (w25_status_to_range(flash, &sr1, &start, &len)) { |
| 1256 | msg_cinfo("(cannot resolve the range)\n"); |
| 1257 | ret = -1; |
| 1258 | } else { |
| 1259 | msg_cinfo("start=0x%08x, len=0x%08x\n", start, len); |
| 1260 | } |
| 1261 | |
| 1262 | return ret; |
| 1263 | } |
| 1264 | |
| 1265 | /* |
| 1266 | * W25Q adds an optional byte to the standard WRSR opcode. If /CS is |
| 1267 | * de-asserted after the first byte, then it acts like a JEDEC-standard |
| 1268 | * WRSR command. if /CS is asserted, then the next data byte is written |
| 1269 | * into status register 2. |
| 1270 | */ |
| 1271 | #define W25Q_WRSR_OUTSIZE 0x03 |
| 1272 | static int w25q_write_status_register_WREN(uint8_t s1, uint8_t s2) |
| 1273 | { |
| 1274 | int result; |
| 1275 | struct spi_command cmds[] = { |
| 1276 | { |
| 1277 | /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */ |
| 1278 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 1279 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 1280 | .readcnt = 0, |
| 1281 | .readarr = NULL, |
| 1282 | }, { |
| 1283 | .writecnt = W25Q_WRSR_OUTSIZE, |
| 1284 | .writearr = (const unsigned char[]){ JEDEC_WRSR, s1, s2 }, |
| 1285 | .readcnt = 0, |
| 1286 | .readarr = NULL, |
| 1287 | }, { |
| 1288 | .writecnt = 0, |
| 1289 | .writearr = NULL, |
| 1290 | .readcnt = 0, |
| 1291 | .readarr = NULL, |
| 1292 | }}; |
| 1293 | |
| 1294 | result = spi_send_multicommand(cmds); |
| 1295 | if (result) { |
| 1296 | msg_cerr("%s failed during command execution\n", |
| 1297 | __func__); |
| 1298 | } |
| 1299 | |
| 1300 | /* WRSR performs a self-timed erase before the changes take effect. */ |
David Hendricks | 6082404 | 2014-12-11 17:22:06 -0800 | [diff] [blame] | 1301 | programmer_delay(100 * 1000); |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1302 | |
| 1303 | return result; |
| 1304 | } |
| 1305 | |
| 1306 | /* |
| 1307 | * Set/clear the SRP1 bit in status register 2. |
| 1308 | * FIXME: make this more generic if other chips use the same SR2 layout |
| 1309 | */ |
| 1310 | static int w25q_set_srp1(const struct flashchip *flash, int enable) |
| 1311 | { |
| 1312 | struct w25q_status sr1; |
| 1313 | struct w25q_status_2 sr2; |
| 1314 | uint8_t tmp, expected; |
| 1315 | |
| 1316 | tmp = spi_read_status_register(); |
| 1317 | memcpy(&sr1, &tmp, 1); |
| 1318 | tmp = w25q_read_status_register_2(); |
| 1319 | memcpy(&sr2, &tmp, 1); |
| 1320 | |
| 1321 | msg_cdbg("%s: old status 2: 0x%02x\n", __func__, tmp); |
| 1322 | |
| 1323 | sr2.srp1 = enable ? 1 : 0; |
| 1324 | |
| 1325 | memcpy(&expected, &sr2, 1); |
| 1326 | w25q_write_status_register_WREN(*((uint8_t *)&sr1), *((uint8_t *)&sr2)); |
| 1327 | |
| 1328 | tmp = w25q_read_status_register_2(); |
| 1329 | msg_cdbg("%s: new status 2: 0x%02x\n", __func__, tmp); |
| 1330 | if ((tmp & MASK_WP2_AREA) != (expected & MASK_WP2_AREA)) |
| 1331 | return 1; |
| 1332 | |
| 1333 | return 0; |
| 1334 | } |
| 1335 | |
| 1336 | enum wp_mode get_wp_mode(const char *mode_str) |
| 1337 | { |
| 1338 | enum wp_mode wp_mode = WP_MODE_UNKNOWN; |
| 1339 | |
| 1340 | if (!strcasecmp(mode_str, "hardware")) |
| 1341 | wp_mode = WP_MODE_HARDWARE; |
| 1342 | else if (!strcasecmp(mode_str, "power_cycle")) |
| 1343 | wp_mode = WP_MODE_POWER_CYCLE; |
| 1344 | else if (!strcasecmp(mode_str, "permanent")) |
| 1345 | wp_mode = WP_MODE_PERMANENT; |
| 1346 | |
| 1347 | return wp_mode; |
| 1348 | } |
| 1349 | |
Ramya Vijaykumar | e6a7ca8 | 2015-05-12 14:27:29 +0530 | [diff] [blame] | 1350 | static int w25r_disable_writeprotect(const struct flashchip *flash) |
| 1351 | { |
| 1352 | int ret; |
| 1353 | |
| 1354 | ret = w25_set_srp(flash, 0); |
| 1355 | if (ret) |
| 1356 | msg_cerr("%s(): error=%d.\n", __func__, ret); |
| 1357 | |
| 1358 | return ret; |
| 1359 | } |
| 1360 | |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1361 | static int w25q_disable_writeprotect(const struct flashchip *flash, |
| 1362 | enum wp_mode wp_mode) |
| 1363 | { |
| 1364 | int ret = 1; |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1365 | struct w25q_status_2 sr2; |
| 1366 | uint8_t tmp; |
| 1367 | |
| 1368 | switch (wp_mode) { |
| 1369 | case WP_MODE_HARDWARE: |
| 1370 | ret = w25_set_srp0(flash, 0); |
| 1371 | break; |
| 1372 | case WP_MODE_POWER_CYCLE: |
| 1373 | tmp = w25q_read_status_register_2(); |
| 1374 | memcpy(&sr2, &tmp, 1); |
| 1375 | if (sr2.srp1) { |
| 1376 | msg_cerr("%s(): must disconnect power to disable " |
| 1377 | "write-protection\n", __func__); |
| 1378 | } else { |
| 1379 | ret = 0; |
| 1380 | } |
| 1381 | break; |
| 1382 | case WP_MODE_PERMANENT: |
| 1383 | msg_cerr("%s(): cannot disable permanent write-protection\n", |
| 1384 | __func__); |
| 1385 | break; |
| 1386 | default: |
| 1387 | msg_cerr("%s(): invalid mode specified\n", __func__); |
| 1388 | break; |
| 1389 | } |
| 1390 | |
| 1391 | if (ret) |
| 1392 | msg_cerr("%s(): error=%d.\n", __func__, ret); |
| 1393 | return ret; |
| 1394 | } |
| 1395 | |
| 1396 | static int w25q_disable_writeprotect_default(const struct flashchip *flash) |
| 1397 | { |
| 1398 | return w25q_disable_writeprotect(flash, WP_MODE_HARDWARE); |
| 1399 | } |
| 1400 | |
Ramya Vijaykumar | e6a7ca8 | 2015-05-12 14:27:29 +0530 | [diff] [blame] | 1401 | static int w25r_enable_writeprotect(const struct flashchip *flash, |
| 1402 | enum wp_mode wp_mode) |
| 1403 | { |
| 1404 | int ret; |
| 1405 | |
| 1406 | switch (wp_mode) { |
| 1407 | case WP_MODE_HARDWARE: |
| 1408 | ret = w25_set_srp(flash, 1); |
| 1409 | break; |
| 1410 | default: |
| 1411 | msg_perr("%s(): invalid mode for Sunrise Point %d\n", |
| 1412 | __func__, wp_mode); |
| 1413 | break; |
| 1414 | } |
| 1415 | if (ret) |
| 1416 | msg_cerr("%s(): error=%d.\n", __func__, ret); |
| 1417 | |
| 1418 | return ret; |
| 1419 | } |
| 1420 | |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1421 | static int w25q_enable_writeprotect(const struct flashchip *flash, |
| 1422 | enum wp_mode wp_mode) |
| 1423 | { |
| 1424 | int ret = 1; |
| 1425 | struct w25q_status sr1; |
| 1426 | struct w25q_status_2 sr2; |
| 1427 | uint8_t tmp; |
| 1428 | |
| 1429 | switch (wp_mode) { |
| 1430 | case WP_MODE_HARDWARE: |
| 1431 | if (w25q_disable_writeprotect(flash, WP_MODE_POWER_CYCLE)) { |
| 1432 | msg_cerr("%s(): cannot disable power cycle WP mode\n", |
| 1433 | __func__); |
| 1434 | break; |
| 1435 | } |
| 1436 | |
| 1437 | tmp = spi_read_status_register(); |
| 1438 | memcpy(&sr1, &tmp, 1); |
| 1439 | if (sr1.srp0) |
| 1440 | ret = 0; |
| 1441 | else |
| 1442 | ret = w25_set_srp0(flash, 1); |
| 1443 | |
| 1444 | break; |
| 1445 | case WP_MODE_POWER_CYCLE: |
| 1446 | if (w25q_disable_writeprotect(flash, WP_MODE_HARDWARE)) { |
| 1447 | msg_cerr("%s(): cannot disable hardware WP mode\n", |
| 1448 | __func__); |
| 1449 | break; |
| 1450 | } |
| 1451 | |
| 1452 | tmp = w25q_read_status_register_2(); |
| 1453 | memcpy(&sr2, &tmp, 1); |
| 1454 | if (sr2.srp1) |
| 1455 | ret = 0; |
| 1456 | else |
| 1457 | ret = w25q_set_srp1(flash, 1); |
| 1458 | |
| 1459 | break; |
| 1460 | case WP_MODE_PERMANENT: |
| 1461 | tmp = spi_read_status_register(); |
| 1462 | memcpy(&sr1, &tmp, 1); |
| 1463 | if (sr1.srp0 == 0) { |
| 1464 | ret = w25_set_srp0(flash, 1); |
| 1465 | if (ret) { |
David Hendricks | f1bd880 | 2012-10-30 11:37:57 -0700 | [diff] [blame] | 1466 | msg_perr("%s(): cannot enable SRP0 for " |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1467 | "permanent WP\n", __func__); |
| 1468 | break; |
| 1469 | } |
| 1470 | } |
| 1471 | |
| 1472 | tmp = w25q_read_status_register_2(); |
| 1473 | memcpy(&sr2, &tmp, 1); |
| 1474 | if (sr2.srp1 == 0) { |
| 1475 | ret = w25q_set_srp1(flash, 1); |
| 1476 | if (ret) { |
David Hendricks | f1bd880 | 2012-10-30 11:37:57 -0700 | [diff] [blame] | 1477 | msg_perr("%s(): cannot enable SRP1 for " |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1478 | "permanent WP\n", __func__); |
| 1479 | break; |
| 1480 | } |
| 1481 | } |
| 1482 | |
| 1483 | break; |
David Hendricks | f1bd880 | 2012-10-30 11:37:57 -0700 | [diff] [blame] | 1484 | default: |
| 1485 | msg_perr("%s(): invalid mode %d\n", __func__, wp_mode); |
| 1486 | break; |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1487 | } |
| 1488 | |
| 1489 | if (ret) |
| 1490 | msg_cerr("%s(): error=%d.\n", __func__, ret); |
| 1491 | return ret; |
| 1492 | } |
| 1493 | |
David Hendricks | c349609 | 2014-11-13 17:20:55 -0800 | [diff] [blame] | 1494 | /* FIXME: Move to spi25.c if it's a JEDEC standard opcode */ |
| 1495 | uint8_t mx25l_read_config_register(void) |
| 1496 | { |
| 1497 | static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { 0x15 }; |
| 1498 | unsigned char readarr[2]; /* leave room for dummy byte */ |
| 1499 | int ret; |
| 1500 | |
| 1501 | ret = spi_send_command(sizeof(cmd), sizeof(readarr), cmd, readarr); |
| 1502 | if (ret) { |
| 1503 | msg_cerr("RDCR failed!\n"); |
| 1504 | readarr[0] = 0x00; |
| 1505 | } |
| 1506 | |
| 1507 | return readarr[0]; |
| 1508 | } |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1509 | /* W25P, W25X, and many flash chips from various vendors */ |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 1510 | struct wp wp_w25 = { |
David Hendricks | 0f7f538 | 2011-02-11 18:12:31 -0800 | [diff] [blame] | 1511 | .list_ranges = w25_list_ranges, |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 1512 | .set_range = w25_set_range, |
| 1513 | .enable = w25_enable_writeprotect, |
Louis Yung-Chieh Lo | c19d3c5 | 2010-10-08 11:59:16 +0800 | [diff] [blame] | 1514 | .disable = w25_disable_writeprotect, |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 1515 | .wp_status = w25_wp_status, |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1516 | |
| 1517 | }; |
| 1518 | |
| 1519 | /* W25Q series has features such as a second status register and SFDP */ |
| 1520 | struct wp wp_w25q = { |
| 1521 | .list_ranges = w25_list_ranges, |
| 1522 | .set_range = w25_set_range, |
| 1523 | .enable = w25q_enable_writeprotect, |
| 1524 | /* |
| 1525 | * By default, disable hardware write-protection. We may change |
| 1526 | * this later if we want to add fine-grained write-protect disable |
| 1527 | * as a command-line option. |
| 1528 | */ |
| 1529 | .disable = w25q_disable_writeprotect_default, |
| 1530 | .wp_status = w25q_wp_status, |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 1531 | }; |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 1532 | |
Ramya Vijaykumar | e6a7ca8 | 2015-05-12 14:27:29 +0530 | [diff] [blame] | 1533 | /* W25R Series */ |
| 1534 | struct wp wp_w25r = { |
| 1535 | .list_ranges = w25_list_ranges, |
| 1536 | .set_range = w25r_set_range, |
| 1537 | .enable = w25r_enable_writeprotect, |
| 1538 | .disable = w25r_disable_writeprotect, |
| 1539 | .wp_status = w25r_wp_status, |
| 1540 | }; |
| 1541 | |
David Hendricks | af3944a | 2014-07-28 18:37:40 -0700 | [diff] [blame] | 1542 | struct generic_range gd25q32_cmp0_ranges[] = { |
| 1543 | /* none, bp4 and bp3 => don't care */ |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1544 | { { }, 0x00, {0, 0} }, |
| 1545 | { { }, 0x08, {0, 0} }, |
| 1546 | { { }, 0x10, {0, 0} }, |
| 1547 | { { }, 0x18, {0, 0} }, |
David Hendricks | af3944a | 2014-07-28 18:37:40 -0700 | [diff] [blame] | 1548 | |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1549 | { { }, 0x01, {0x3f0000, 64 * 1024} }, |
| 1550 | { { }, 0x02, {0x3e0000, 128 * 1024} }, |
| 1551 | { { }, 0x03, {0x3c0000, 256 * 1024} }, |
| 1552 | { { }, 0x04, {0x380000, 512 * 1024} }, |
| 1553 | { { }, 0x05, {0x300000, 1024 * 1024} }, |
| 1554 | { { }, 0x06, {0x200000, 2048 * 1024} }, |
David Hendricks | af3944a | 2014-07-28 18:37:40 -0700 | [diff] [blame] | 1555 | |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1556 | { { }, 0x09, {0x000000, 64 * 1024} }, |
| 1557 | { { }, 0x0a, {0x000000, 128 * 1024} }, |
| 1558 | { { }, 0x0b, {0x000000, 256 * 1024} }, |
| 1559 | { { }, 0x0c, {0x000000, 512 * 1024} }, |
| 1560 | { { }, 0x0d, {0x000000, 1024 * 1024} }, |
| 1561 | { { }, 0x0e, {0x000000, 2048 * 1024} }, |
David Hendricks | af3944a | 2014-07-28 18:37:40 -0700 | [diff] [blame] | 1562 | |
| 1563 | /* all, bp4 and bp3 => don't care */ |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1564 | { { }, 0x07, {0x000000, 4096 * 1024} }, |
| 1565 | { { }, 0x0f, {0x000000, 4096 * 1024} }, |
| 1566 | { { }, 0x17, {0x000000, 4096 * 1024} }, |
| 1567 | { { }, 0x1f, {0x000000, 4096 * 1024} }, |
David Hendricks | af3944a | 2014-07-28 18:37:40 -0700 | [diff] [blame] | 1568 | |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1569 | { { }, 0x11, {0x3ff000, 4 * 1024} }, |
| 1570 | { { }, 0x12, {0x3fe000, 8 * 1024} }, |
| 1571 | { { }, 0x13, {0x3fc000, 16 * 1024} }, |
| 1572 | { { }, 0x14, {0x3f8000, 32 * 1024} }, /* bp0 => don't care */ |
| 1573 | { { }, 0x15, {0x3f8000, 32 * 1024} }, /* bp0 => don't care */ |
| 1574 | { { }, 0x16, {0x3f8000, 32 * 1024} }, |
David Hendricks | af3944a | 2014-07-28 18:37:40 -0700 | [diff] [blame] | 1575 | |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1576 | { { }, 0x19, {0x000000, 4 * 1024} }, |
| 1577 | { { }, 0x1a, {0x000000, 8 * 1024} }, |
| 1578 | { { }, 0x1b, {0x000000, 16 * 1024} }, |
| 1579 | { { }, 0x1c, {0x000000, 32 * 1024} }, /* bp0 => don't care */ |
| 1580 | { { }, 0x1d, {0x000000, 32 * 1024} }, /* bp0 => don't care */ |
| 1581 | { { }, 0x1e, {0x000000, 32 * 1024} }, |
David Hendricks | af3944a | 2014-07-28 18:37:40 -0700 | [diff] [blame] | 1582 | }; |
| 1583 | |
| 1584 | struct generic_range gd25q32_cmp1_ranges[] = { |
| 1585 | /* none, bp4 and bp3 => don't care */ |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1586 | { { }, 0x00, {0, 0} }, |
| 1587 | { { }, 0x08, {0, 0} }, |
| 1588 | { { }, 0x10, {0, 0} }, |
| 1589 | { { }, 0x18, {0, 0} }, |
David Hendricks | af3944a | 2014-07-28 18:37:40 -0700 | [diff] [blame] | 1590 | |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1591 | { { }, 0x01, {0x000000, 4032 * 1024} }, |
| 1592 | { { }, 0x02, {0x000000, 3968 * 1024} }, |
| 1593 | { { }, 0x03, {0x000000, 3840 * 1024} }, |
| 1594 | { { }, 0x04, {0x000000, 3584 * 1024} }, |
| 1595 | { { }, 0x05, {0x000000, 3 * 1024 * 1024} }, |
| 1596 | { { }, 0x06, {0x000000, 2 * 1024 * 1024} }, |
David Hendricks | af3944a | 2014-07-28 18:37:40 -0700 | [diff] [blame] | 1597 | |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1598 | { { }, 0x09, {0x010000, 4032 * 1024} }, |
| 1599 | { { }, 0x0a, {0x020000, 3968 * 1024} }, |
| 1600 | { { }, 0x0b, {0x040000, 3840 * 1024} }, |
| 1601 | { { }, 0x0c, {0x080000, 3584 * 1024} }, |
| 1602 | { { }, 0x0d, {0x100000, 3 * 1024 * 1024} }, |
| 1603 | { { }, 0x0e, {0x200000, 2 * 1024 * 1024} }, |
David Hendricks | af3944a | 2014-07-28 18:37:40 -0700 | [diff] [blame] | 1604 | |
| 1605 | /* all, bp4 and bp3 => don't care */ |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1606 | { { }, 0x07, {0x000000, 4096 * 1024} }, |
| 1607 | { { }, 0x0f, {0x000000, 4096 * 1024} }, |
| 1608 | { { }, 0x17, {0x000000, 4096 * 1024} }, |
| 1609 | { { }, 0x1f, {0x000000, 4096 * 1024} }, |
David Hendricks | af3944a | 2014-07-28 18:37:40 -0700 | [diff] [blame] | 1610 | |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1611 | { { }, 0x11, {0x000000, 4092 * 1024} }, |
| 1612 | { { }, 0x12, {0x000000, 4088 * 1024} }, |
| 1613 | { { }, 0x13, {0x000000, 4080 * 1024} }, |
| 1614 | { { }, 0x14, {0x000000, 4064 * 1024} }, /* bp0 => don't care */ |
| 1615 | { { }, 0x15, {0x000000, 4064 * 1024} }, /* bp0 => don't care */ |
| 1616 | { { }, 0x16, {0x000000, 4064 * 1024} }, |
David Hendricks | af3944a | 2014-07-28 18:37:40 -0700 | [diff] [blame] | 1617 | |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1618 | { { }, 0x19, {0x001000, 4092 * 1024} }, |
| 1619 | { { }, 0x1a, {0x002000, 4088 * 1024} }, |
| 1620 | { { }, 0x1b, {0x040000, 4080 * 1024} }, |
| 1621 | { { }, 0x1c, {0x080000, 4064 * 1024} }, /* bp0 => don't care */ |
| 1622 | { { }, 0x1d, {0x080000, 4064 * 1024} }, /* bp0 => don't care */ |
| 1623 | { { }, 0x1e, {0x080000, 4064 * 1024} }, |
David Hendricks | af3944a | 2014-07-28 18:37:40 -0700 | [diff] [blame] | 1624 | }; |
| 1625 | |
| 1626 | static struct generic_wp gd25q32_wp = { |
| 1627 | /* TODO: map second status register */ |
| 1628 | .sr1 = { .bp0_pos = 2, .bp_bits = 5, .srp_pos = 7 }, |
| 1629 | }; |
| 1630 | |
David Hendricks | 83541d3 | 2014-07-15 20:58:21 -0700 | [diff] [blame] | 1631 | #if 0 |
| 1632 | /* FIXME: MX25L6405D has same ID as MX25L6406 */ |
| 1633 | static struct w25q_range mx25l6405d_ranges[] = { |
| 1634 | { X, 0, 0, {0, 0} }, /* none */ |
| 1635 | { X, 0, 0x1, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */ |
| 1636 | { X, 0, 0x2, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */ |
| 1637 | { X, 0, 0x3, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */ |
| 1638 | { X, 0, 0x4, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */ |
| 1639 | { X, 0, 0x5, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */ |
| 1640 | { X, 0, 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */ |
| 1641 | { X, 0, 0x7, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */ |
| 1642 | |
| 1643 | { X, 1, 0x0, {0x000000, 8192 * 1024} }, |
| 1644 | { X, 1, 0x1, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */ |
| 1645 | { X, 1, 0x2, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */ |
| 1646 | { X, 1, 0x3, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */ |
| 1647 | { X, 1, 0x4, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */ |
| 1648 | { X, 1, 0x5, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */ |
| 1649 | { X, 1, 0x6, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */ |
| 1650 | { X, 1, 0x7, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */ |
| 1651 | }; |
| 1652 | #endif |
| 1653 | |
| 1654 | /* FIXME: MX25L6406 has same ID as MX25L6405D */ |
| 1655 | struct generic_range mx25l6406e_ranges[] = { |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1656 | { { }, 0, {0, 0} }, /* none */ |
| 1657 | { { }, 0x1, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */ |
| 1658 | { { }, 0x2, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */ |
| 1659 | { { }, 0x3, {0x7a0000, 64 * 8 * 1024} }, /* blocks 120-127 */ |
| 1660 | { { }, 0x4, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */ |
| 1661 | { { }, 0x5, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */ |
| 1662 | { { }, 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */ |
David Hendricks | 83541d3 | 2014-07-15 20:58:21 -0700 | [diff] [blame] | 1663 | |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1664 | { { }, 0x7, {0x000000, 64 * 128 * 1024} }, /* all */ |
| 1665 | { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* all */ |
| 1666 | { { }, 0x9, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */ |
| 1667 | { { }, 0xa, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */ |
| 1668 | { { }, 0xb, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */ |
| 1669 | { { }, 0xc, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */ |
| 1670 | { { }, 0xd, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */ |
| 1671 | { { }, 0xe, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */ |
| 1672 | { { }, 0xf, {0x000000, 64 * 128 * 1024} }, /* all */ |
David Hendricks | 83541d3 | 2014-07-15 20:58:21 -0700 | [diff] [blame] | 1673 | }; |
| 1674 | |
| 1675 | static struct generic_wp mx25l6406e_wp = { |
| 1676 | .sr1 = { .bp0_pos = 2, .bp_bits = 4, .srp_pos = 7 }, |
| 1677 | .ranges = &mx25l6406e_ranges[0], |
| 1678 | }; |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 1679 | |
David Hendricks | c349609 | 2014-11-13 17:20:55 -0800 | [diff] [blame] | 1680 | struct generic_range mx25l6495f_tb0_ranges[] = { |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1681 | { { }, 0, {0, 0} }, /* none */ |
| 1682 | { { }, 0x1, {0x7f0000, 64 * 1 * 1024} }, /* block 127 */ |
| 1683 | { { }, 0x2, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */ |
| 1684 | { { }, 0x3, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */ |
David Hendricks | c349609 | 2014-11-13 17:20:55 -0800 | [diff] [blame] | 1685 | |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1686 | { { }, 0x4, {0x780000, 64 * 8 * 1024} }, /* blocks 120-127 */ |
| 1687 | { { }, 0x5, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */ |
| 1688 | { { }, 0x6, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */ |
| 1689 | { { }, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */ |
| 1690 | { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* all */ |
| 1691 | { { }, 0x9, {0x000000, 64 * 128 * 1024} }, /* all */ |
| 1692 | { { }, 0xa, {0x000000, 64 * 128 * 1024} }, /* all */ |
| 1693 | { { }, 0xb, {0x000000, 64 * 128 * 1024} }, /* all */ |
| 1694 | { { }, 0xc, {0x000000, 64 * 128 * 1024} }, /* all */ |
| 1695 | { { }, 0xd, {0x000000, 64 * 128 * 1024} }, /* all */ |
| 1696 | { { }, 0xe, {0x000000, 64 * 128 * 1024} }, /* all */ |
| 1697 | { { }, 0xf, {0x000000, 64 * 128 * 1024} }, /* all */ |
David Hendricks | c349609 | 2014-11-13 17:20:55 -0800 | [diff] [blame] | 1698 | }; |
| 1699 | |
| 1700 | struct generic_range mx25l6495f_tb1_ranges[] = { |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1701 | { { }, 0, {0, 0} }, /* none */ |
| 1702 | { { }, 0x1, {0x000000, 64 * 1 * 1024} }, /* block 0 */ |
| 1703 | { { }, 0x2, {0x000000, 64 * 2 * 1024} }, /* blocks 0-1 */ |
| 1704 | { { }, 0x3, {0x000000, 64 * 4 * 1024} }, /* blocks 0-3 */ |
| 1705 | { { }, 0x4, {0x000000, 64 * 8 * 1024} }, /* blocks 0-7 */ |
| 1706 | { { }, 0x5, {0x000000, 64 * 16 * 1024} }, /* blocks 0-15 */ |
| 1707 | { { }, 0x6, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */ |
| 1708 | { { }, 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */ |
| 1709 | { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* all */ |
| 1710 | { { }, 0x9, {0x000000, 64 * 128 * 1024} }, /* all */ |
| 1711 | { { }, 0xa, {0x000000, 64 * 128 * 1024} }, /* all */ |
| 1712 | { { }, 0xb, {0x000000, 64 * 128 * 1024} }, /* all */ |
| 1713 | { { }, 0xc, {0x000000, 64 * 128 * 1024} }, /* all */ |
| 1714 | { { }, 0xd, {0x000000, 64 * 128 * 1024} }, /* all */ |
| 1715 | { { }, 0xe, {0x000000, 64 * 128 * 1024} }, /* all */ |
| 1716 | { { }, 0xf, {0x000000, 64 * 128 * 1024} }, /* all */ |
David Hendricks | c349609 | 2014-11-13 17:20:55 -0800 | [diff] [blame] | 1717 | }; |
| 1718 | |
| 1719 | static struct generic_wp mx25l6495f_wp = { |
| 1720 | .sr1 = { .bp0_pos = 2, .bp_bits = 4, .srp_pos = 7 }, |
| 1721 | }; |
| 1722 | |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1723 | struct generic_range s25fs128s_ranges[] = { |
| 1724 | { { .tb = 1 }, 0, {0, 0} }, /* none */ |
| 1725 | { { .tb = 1 }, 0x1, {0x000000, 256 * 1024} }, /* lower 64th */ |
| 1726 | { { .tb = 1 }, 0x2, {0x000000, 512 * 1024} }, /* lower 32nd */ |
| 1727 | { { .tb = 1 }, 0x3, {0x000000, 1024 * 1024} }, /* lower 16th */ |
| 1728 | { { .tb = 1 }, 0x4, {0x000000, 2048 * 1024} }, /* lower 8th */ |
| 1729 | { { .tb = 1 }, 0x5, {0x000000, 4096 * 1024} }, /* lower 4th */ |
| 1730 | { { .tb = 1 }, 0x6, {0x000000, 8192 * 1024} }, /* lower half */ |
| 1731 | { { .tb = 1 }, 0x7, {0x000000, 16384 * 1024} }, /* all */ |
David Hendricks | a988485 | 2014-12-11 15:31:12 -0800 | [diff] [blame] | 1732 | |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1733 | { { .tb = 0 }, 0, {0, 0} }, /* none */ |
| 1734 | { { .tb = 0 }, 0x1, {0xfc0000, 256 * 1024} }, /* upper 64th */ |
| 1735 | { { .tb = 0 }, 0x2, {0xf80000, 512 * 1024} }, /* upper 32nd */ |
| 1736 | { { .tb = 0 }, 0x3, {0xf00000, 1024 * 1024} }, /* upper 16th */ |
| 1737 | { { .tb = 0 }, 0x4, {0xe00000, 2048 * 1024} }, /* upper 8th */ |
| 1738 | { { .tb = 0 }, 0x5, {0xc00000, 4096 * 1024} }, /* upper 4th */ |
| 1739 | { { .tb = 0 }, 0x6, {0x800000, 8192 * 1024} }, /* upper half */ |
| 1740 | { { .tb = 0 }, 0x7, {0x000000, 16384 * 1024} }, /* all */ |
David Hendricks | a988485 | 2014-12-11 15:31:12 -0800 | [diff] [blame] | 1741 | }; |
| 1742 | |
| 1743 | static struct generic_wp s25fs128s_wp = { |
| 1744 | .sr1 = { .bp0_pos = 2, .bp_bits = 3, .srp_pos = 7 }, |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1745 | .get_modifier_bits = s25f_get_modifier_bits, |
| 1746 | .set_modifier_bits = s25f_set_modifier_bits, |
David Hendricks | a988485 | 2014-12-11 15:31:12 -0800 | [diff] [blame] | 1747 | }; |
| 1748 | |
David Hendricks | c694bb8 | 2015-02-25 14:52:17 -0800 | [diff] [blame] | 1749 | |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1750 | struct generic_range s25fl256s_ranges[] = { |
| 1751 | { { .tb = 1 }, 0, {0, 0} }, /* none */ |
| 1752 | { { .tb = 1 }, 0x1, {0x000000, 512 * 1024} }, /* lower 64th */ |
| 1753 | { { .tb = 1 }, 0x2, {0x000000, 1024 * 1024} }, /* lower 32nd */ |
| 1754 | { { .tb = 1 }, 0x3, {0x000000, 2048 * 1024} }, /* lower 16th */ |
| 1755 | { { .tb = 1 }, 0x4, {0x000000, 4096 * 1024} }, /* lower 8th */ |
| 1756 | { { .tb = 1 }, 0x5, {0x000000, 8192 * 1024} }, /* lower 4th */ |
| 1757 | { { .tb = 1 }, 0x6, {0x000000, 16384 * 1024} }, /* lower half */ |
| 1758 | { { .tb = 1 }, 0x7, {0x000000, 32768 * 1024} }, /* all */ |
| 1759 | |
| 1760 | { { .tb = 0 }, 0, {0, 0} }, /* none */ |
| 1761 | { { .tb = 0 }, 0x1, {0x1f80000, 512 * 1024} }, /* upper 64th */ |
| 1762 | { { .tb = 0 }, 0x2, {0x1f00000, 1024 * 1024} }, /* upper 32nd */ |
| 1763 | { { .tb = 0 }, 0x3, {0x1e00000, 2048 * 1024} }, /* upper 16th */ |
| 1764 | { { .tb = 0 }, 0x4, {0x1c00000, 4096 * 1024} }, /* upper 8th */ |
| 1765 | { { .tb = 0 }, 0x5, {0x1800000, 8192 * 1024} }, /* upper 4th */ |
| 1766 | { { .tb = 0 }, 0x6, {0x1000000, 16384 * 1024} }, /* upper half */ |
| 1767 | { { .tb = 0 }, 0x7, {0x000000, 32768 * 1024} }, /* all */ |
David Hendricks | c694bb8 | 2015-02-25 14:52:17 -0800 | [diff] [blame] | 1768 | }; |
| 1769 | |
| 1770 | static struct generic_wp s25fl256s_wp = { |
| 1771 | .sr1 = { .bp0_pos = 2, .bp_bits = 3, .srp_pos = 7 }, |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1772 | .get_modifier_bits = s25f_get_modifier_bits, |
| 1773 | .set_modifier_bits = s25f_set_modifier_bits, |
David Hendricks | c694bb8 | 2015-02-25 14:52:17 -0800 | [diff] [blame] | 1774 | }; |
| 1775 | |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 1776 | /* Given a flash chip, this function returns its writeprotect info. */ |
| 1777 | static int generic_range_table(const struct flashchip *flash, |
| 1778 | struct generic_wp **wp, |
| 1779 | int *num_entries) |
| 1780 | { |
| 1781 | *wp = NULL; |
| 1782 | *num_entries = 0; |
| 1783 | |
| 1784 | switch (flash->manufacture_id) { |
David Hendricks | af3944a | 2014-07-28 18:37:40 -0700 | [diff] [blame] | 1785 | case GIGADEVICE_ID: |
| 1786 | switch(flash->model_id) { |
| 1787 | case GIGADEVICE_GD25Q32: { |
| 1788 | uint8_t sr1 = w25q_read_status_register_2(); |
| 1789 | |
| 1790 | *wp = &gd25q32_wp; |
| 1791 | if (!(sr1 & (1 << 6))) { /* CMP == 0 */ |
| 1792 | (*wp)->ranges = &gd25q32_cmp0_ranges[0]; |
| 1793 | *num_entries = ARRAY_SIZE(gd25q32_cmp0_ranges); |
| 1794 | } else { /* CMP == 1 */ |
| 1795 | (*wp)->ranges = &gd25q32_cmp1_ranges[0]; |
| 1796 | *num_entries = ARRAY_SIZE(gd25q32_cmp1_ranges); |
| 1797 | } |
| 1798 | |
| 1799 | break; |
| 1800 | /* TODO(shawnn): add support for other GD parts */ |
| 1801 | } |
| 1802 | default: |
| 1803 | msg_cerr("%s() %d: GigaDevice flash chip mismatch" |
| 1804 | " (0x%04x), aborting\n", __func__, __LINE__, |
| 1805 | flash->model_id); |
| 1806 | return -1; |
| 1807 | } |
| 1808 | break; |
David Hendricks | 83541d3 | 2014-07-15 20:58:21 -0700 | [diff] [blame] | 1809 | case MACRONIX_ID: |
| 1810 | switch (flash->model_id) { |
| 1811 | case MACRONIX_MX25L6405: |
| 1812 | /* FIXME: MX25L64* chips have mixed capabilities and |
| 1813 | share IDs */ |
| 1814 | *wp = &mx25l6406e_wp; |
| 1815 | *num_entries = ARRAY_SIZE(mx25l6406e_ranges); |
| 1816 | break; |
David Hendricks | c349609 | 2014-11-13 17:20:55 -0800 | [diff] [blame] | 1817 | case MACRONIX_MX25L6495F: { |
| 1818 | uint8_t cr = mx25l_read_config_register(); |
| 1819 | |
| 1820 | *wp = &mx25l6495f_wp; |
| 1821 | if (!(cr & (1 << 3))) { /* T/B == 0 */ |
| 1822 | (*wp)->ranges = &mx25l6495f_tb0_ranges[0]; |
| 1823 | *num_entries = ARRAY_SIZE(mx25l6495f_tb0_ranges); |
| 1824 | } else { /* T/B == 1 */ |
| 1825 | (*wp)->ranges = &mx25l6495f_tb1_ranges[0]; |
| 1826 | *num_entries = ARRAY_SIZE(mx25l6495f_tb1_ranges); |
| 1827 | } |
| 1828 | break; |
| 1829 | } |
David Hendricks | 83541d3 | 2014-07-15 20:58:21 -0700 | [diff] [blame] | 1830 | default: |
| 1831 | msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)" |
| 1832 | ", aborting\n", __func__, __LINE__, |
| 1833 | flash->model_id); |
| 1834 | return -1; |
| 1835 | } |
| 1836 | break; |
David Hendricks | a988485 | 2014-12-11 15:31:12 -0800 | [diff] [blame] | 1837 | case SPANSION_ID: |
| 1838 | switch (flash->model_id) { |
| 1839 | case SPANSION_S25FS128S_L: |
| 1840 | case SPANSION_S25FS128S_S: { |
David Hendricks | a988485 | 2014-12-11 15:31:12 -0800 | [diff] [blame] | 1841 | *wp = &s25fs128s_wp; |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1842 | (*wp)->ranges = s25fs128s_ranges; |
| 1843 | *num_entries = ARRAY_SIZE(s25fs128s_ranges); |
David Hendricks | a988485 | 2014-12-11 15:31:12 -0800 | [diff] [blame] | 1844 | break; |
| 1845 | } |
David Hendricks | c694bb8 | 2015-02-25 14:52:17 -0800 | [diff] [blame] | 1846 | case SPANSION_S25FL256S_UL: |
| 1847 | case SPANSION_S25FL256S_US: { |
David Hendricks | c694bb8 | 2015-02-25 14:52:17 -0800 | [diff] [blame] | 1848 | *wp = &s25fl256s_wp; |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1849 | (*wp)->ranges = s25fl256s_ranges; |
| 1850 | *num_entries = ARRAY_SIZE(s25fl256s_ranges); |
David Hendricks | c694bb8 | 2015-02-25 14:52:17 -0800 | [diff] [blame] | 1851 | break; |
| 1852 | } |
David Hendricks | a988485 | 2014-12-11 15:31:12 -0800 | [diff] [blame] | 1853 | default: |
| 1854 | msg_cerr("%s():%d Spansion flash chip mismatch (0x%04x)" |
| 1855 | ", aborting\n", __func__, __LINE__, flash->model_id); |
| 1856 | return -1; |
| 1857 | } |
| 1858 | break; |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 1859 | default: |
| 1860 | msg_cerr("%s: flash vendor (0x%x) not found, aborting\n", |
| 1861 | __func__, flash->manufacture_id); |
| 1862 | return -1; |
| 1863 | } |
| 1864 | |
| 1865 | return 0; |
| 1866 | } |
| 1867 | |
| 1868 | /* Given a [start, len], this function finds a block protect bit combination |
| 1869 | * (if possible) and sets the corresponding bits in "status". Remaining bits |
| 1870 | * are preserved. */ |
| 1871 | static int generic_range_to_status(const struct flashchip *flash, |
| 1872 | unsigned int start, unsigned int len, |
| 1873 | uint8_t *status) |
| 1874 | { |
| 1875 | struct generic_wp *wp; |
| 1876 | struct generic_range *r; |
| 1877 | int i, range_found = 0, num_entries; |
| 1878 | uint8_t bp_mask; |
| 1879 | |
| 1880 | if (generic_range_table(flash, &wp, &num_entries)) |
| 1881 | return -1; |
| 1882 | |
| 1883 | bp_mask = ((1 << (wp->sr1.bp0_pos + wp->sr1.bp_bits)) - 1) - \ |
| 1884 | ((1 << wp->sr1.bp0_pos) - 1); |
| 1885 | |
| 1886 | for (i = 0, r = &wp->ranges[0]; i < num_entries; i++, r++) { |
| 1887 | msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n", |
| 1888 | start, len, r->range.start, r->range.len); |
| 1889 | if ((start == r->range.start) && (len == r->range.len)) { |
| 1890 | *status &= ~(bp_mask); |
| 1891 | *status |= r->bp << (wp->sr1.bp0_pos); |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1892 | |
| 1893 | if (wp->set_modifier_bits) { |
| 1894 | if (wp->set_modifier_bits(flash, &r->m) < 0) { |
| 1895 | msg_cerr("error setting modifier " |
| 1896 | "bits for range.\n"); |
| 1897 | return -1; |
| 1898 | } |
| 1899 | } |
| 1900 | |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 1901 | range_found = 1; |
| 1902 | break; |
| 1903 | } |
| 1904 | } |
| 1905 | |
| 1906 | if (!range_found) { |
| 1907 | msg_cerr("matching range not found\n"); |
| 1908 | return -1; |
| 1909 | } |
| 1910 | return 0; |
| 1911 | } |
| 1912 | |
| 1913 | static int generic_status_to_range(const struct flashchip *flash, |
| 1914 | const uint8_t sr1, unsigned int *start, unsigned int *len) |
| 1915 | { |
| 1916 | struct generic_wp *wp; |
| 1917 | struct generic_range *r; |
Duncan Laurie | 04ca117 | 2015-03-12 09:25:34 -0700 | [diff] [blame] | 1918 | int num_entries, i, status_found = 0; |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 1919 | uint8_t sr1_bp; |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1920 | struct generic_modifier_bits m; |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 1921 | |
| 1922 | if (generic_range_table(flash, &wp, &num_entries)) |
| 1923 | return -1; |
| 1924 | |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1925 | /* modifier bits may be compared more than once, so get them here */ |
| 1926 | if (wp->get_modifier_bits) { |
| 1927 | if (wp->get_modifier_bits(flash, &m) < 0) |
| 1928 | return -1; |
| 1929 | } |
| 1930 | |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 1931 | sr1_bp = (sr1 >> wp->sr1.bp0_pos) & ((1 << wp->sr1.bp_bits) - 1); |
| 1932 | |
| 1933 | for (i = 0, r = &wp->ranges[0]; i < num_entries; i++, r++) { |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 1934 | if (wp->get_modifier_bits) { |
| 1935 | if (memcmp(&m, &r->m, sizeof(m))) |
| 1936 | continue; |
| 1937 | } |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 1938 | msg_cspew("comparing 0x%02x 0x%02x\n", sr1_bp, r->bp); |
| 1939 | if (sr1_bp == r->bp) { |
| 1940 | *start = r->range.start; |
| 1941 | *len = r->range.len; |
| 1942 | status_found = 1; |
| 1943 | break; |
| 1944 | } |
| 1945 | } |
| 1946 | |
| 1947 | if (!status_found) { |
| 1948 | msg_cerr("matching status not found\n"); |
| 1949 | return -1; |
| 1950 | } |
| 1951 | return 0; |
| 1952 | } |
| 1953 | |
| 1954 | /* Given a [start, len], this function calls generic_range_to_status() to |
| 1955 | * convert it to flash-chip-specific range bits, then sets into status register. |
| 1956 | */ |
| 1957 | static int generic_set_range(const struct flashchip *flash, |
| 1958 | unsigned int start, unsigned int len) |
| 1959 | { |
| 1960 | uint8_t status, expected; |
| 1961 | |
| 1962 | status = spi_read_status_register(); |
| 1963 | msg_cdbg("%s: old status: 0x%02x\n", __func__, status); |
| 1964 | |
| 1965 | expected = status; /* preserve non-bp bits */ |
| 1966 | if (generic_range_to_status(flash, start, len, &expected)) |
| 1967 | return -1; |
| 1968 | |
David Hendricks | 6082404 | 2014-12-11 17:22:06 -0800 | [diff] [blame] | 1969 | spi_write_status_register(flash, expected); |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 1970 | |
| 1971 | status = spi_read_status_register(); |
| 1972 | msg_cdbg("%s: new status: 0x%02x\n", __func__, status); |
| 1973 | if (status != expected) { |
| 1974 | msg_cerr("expected=0x%02x, but actual=0x%02x.\n", |
| 1975 | expected, status); |
| 1976 | return 1; |
| 1977 | } |
| 1978 | |
| 1979 | return 0; |
| 1980 | } |
| 1981 | |
| 1982 | /* Set/clear the status regsiter write protect bit in SR1. */ |
| 1983 | static int generic_set_srp0(const struct flashchip *flash, int enable) |
| 1984 | { |
| 1985 | uint8_t status, expected; |
| 1986 | struct generic_wp *wp; |
| 1987 | int num_entries; |
| 1988 | |
| 1989 | if (generic_range_table(flash, &wp, &num_entries)) |
| 1990 | return -1; |
| 1991 | |
| 1992 | expected = spi_read_status_register(); |
| 1993 | msg_cdbg("%s: old status: 0x%02x\n", __func__, expected); |
| 1994 | |
| 1995 | if (enable) |
| 1996 | expected |= 1 << wp->sr1.srp_pos; |
| 1997 | else |
| 1998 | expected &= ~(1 << wp->sr1.srp_pos); |
| 1999 | |
David Hendricks | 6082404 | 2014-12-11 17:22:06 -0800 | [diff] [blame] | 2000 | spi_write_status_register(flash, expected); |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 2001 | |
| 2002 | status = spi_read_status_register(); |
| 2003 | msg_cdbg("%s: new status: 0x%02x\n", __func__, status); |
| 2004 | if (status != expected) |
| 2005 | return -1; |
| 2006 | |
| 2007 | return 0; |
| 2008 | } |
| 2009 | |
| 2010 | static int generic_enable_writeprotect(const struct flashchip *flash, |
| 2011 | enum wp_mode wp_mode) |
| 2012 | { |
| 2013 | int ret; |
| 2014 | |
| 2015 | switch (wp_mode) { |
| 2016 | case WP_MODE_HARDWARE: |
| 2017 | ret = generic_set_srp0(flash, 1); |
| 2018 | break; |
| 2019 | default: |
| 2020 | msg_cerr("%s(): unsupported write-protect mode\n", __func__); |
| 2021 | return 1; |
| 2022 | } |
| 2023 | |
| 2024 | if (ret) |
| 2025 | msg_cerr("%s(): error=%d.\n", __func__, ret); |
| 2026 | return ret; |
| 2027 | } |
| 2028 | |
| 2029 | static int generic_disable_writeprotect(const struct flashchip *flash) |
| 2030 | { |
| 2031 | int ret; |
| 2032 | |
| 2033 | ret = generic_set_srp0(flash, 0); |
| 2034 | if (ret) |
| 2035 | msg_cerr("%s(): error=%d.\n", __func__, ret); |
| 2036 | return ret; |
| 2037 | } |
| 2038 | |
| 2039 | static int generic_list_ranges(const struct flashchip *flash) |
| 2040 | { |
| 2041 | struct generic_wp *wp; |
| 2042 | struct generic_range *r; |
| 2043 | int i, num_entries; |
| 2044 | |
| 2045 | if (generic_range_table(flash, &wp, &num_entries)) |
| 2046 | return -1; |
| 2047 | |
| 2048 | r = &wp->ranges[0]; |
| 2049 | for (i = 0; i < num_entries; i++) { |
| 2050 | msg_cinfo("start: 0x%06x, length: 0x%06x\n", |
| 2051 | r->range.start, r->range.len); |
| 2052 | r++; |
| 2053 | } |
| 2054 | |
| 2055 | return 0; |
| 2056 | } |
| 2057 | |
| 2058 | static int generic_wp_status(const struct flashchip *flash) |
| 2059 | { |
| 2060 | uint8_t sr1; |
| 2061 | unsigned int start, len; |
| 2062 | int ret = 0; |
| 2063 | struct generic_wp *wp; |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 2064 | int num_entries, wp_en; |
| 2065 | |
| 2066 | if (generic_range_table(flash, &wp, &num_entries)) |
| 2067 | return -1; |
| 2068 | |
| 2069 | sr1 = spi_read_status_register(); |
| 2070 | wp_en = (sr1 >> wp->sr1.srp_pos) & 1; |
| 2071 | |
| 2072 | msg_cinfo("WP: status: 0x%04x\n", sr1); |
| 2073 | msg_cinfo("WP: status.srp0: %x\n", wp_en); |
| 2074 | /* FIXME: SRP1 is not really generic, but we probably should print |
| 2075 | * it anyway to have consistent output. #legacycruft */ |
| 2076 | msg_cinfo("WP: status.srp1: %x\n", 0); |
| 2077 | msg_cinfo("WP: write protect is %s.\n", |
| 2078 | wp_en ? "enabled" : "disabled"); |
| 2079 | |
| 2080 | msg_cinfo("WP: write protect range: "); |
| 2081 | if (generic_status_to_range(flash, sr1, &start, &len)) { |
| 2082 | msg_cinfo("(cannot resolve the range)\n"); |
| 2083 | ret = -1; |
| 2084 | } else { |
| 2085 | msg_cinfo("start=0x%08x, len=0x%08x\n", start, len); |
| 2086 | } |
| 2087 | |
| 2088 | return ret; |
| 2089 | } |
| 2090 | |
| 2091 | struct wp wp_generic = { |
| 2092 | .list_ranges = generic_list_ranges, |
| 2093 | .set_range = generic_set_range, |
| 2094 | .enable = generic_enable_writeprotect, |
| 2095 | .disable = generic_disable_writeprotect, |
| 2096 | .wp_status = generic_wp_status, |
| 2097 | }; |