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 | |
Louis Yung-Chieh Lo | 96222b1 | 2010-11-01 11:48:11 +0800 | [diff] [blame] | 30 | /* When update flash's status register, it takes few time to erase register. |
| 31 | * After surveying some flash vendor specs, such as Winbond, MXIC, EON, |
| 32 | * all of their update time are less than 20ms. After refering the spi25.c, |
| 33 | * use 100ms delay. |
| 34 | */ |
| 35 | #define WRITE_STATUS_REGISTER_DELAY 100 * 1000 /* unit: us */ |
| 36 | |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 37 | /* |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 38 | * The following procedures rely on look-up tables to match the user-specified |
| 39 | * range with the chip's supported ranges. This turned out to be the most |
| 40 | * elegant approach since diferent flash chips use different levels of |
| 41 | * granularity and methods to determine protected ranges. In other words, |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame^] | 42 | * 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] | 43 | */ |
| 44 | |
| 45 | struct wp_range { |
| 46 | unsigned int start; /* starting address */ |
| 47 | unsigned int len; /* len */ |
| 48 | }; |
| 49 | |
| 50 | enum bit_state { |
| 51 | OFF = 0, |
| 52 | ON = 1, |
Louis Yung-Chieh Lo | edd3930 | 2011-11-10 15:43:06 +0800 | [diff] [blame] | 53 | X = -1 /* don't care. Must be bigger than max # of bp. */ |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 54 | }; |
| 55 | |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame^] | 56 | /* |
| 57 | * Generic write-protection schema for 25-series SPI flash chips. This assumes |
| 58 | * there is a status register that contains one or more consecutive bits which |
| 59 | * determine which address range is protected. |
| 60 | */ |
| 61 | |
| 62 | struct status_register_layout { |
| 63 | int bp0_pos; /* position of BP0 */ |
| 64 | int bp_bits; /* number of block protect bits */ |
| 65 | int srp_pos; /* position of status register protect enable bit */ |
| 66 | }; |
| 67 | |
| 68 | struct generic_range { |
| 69 | unsigned int bp; /* block protect bitfield */ |
| 70 | struct wp_range range; |
| 71 | }; |
| 72 | |
| 73 | struct generic_wp { |
| 74 | struct status_register_layout sr1; /* status register 1 */ |
| 75 | struct generic_range *ranges; |
| 76 | }; |
| 77 | |
| 78 | /* |
| 79 | * The following ranges and functions are useful for representing Winbond- |
| 80 | * style writeprotect schema in which there are typically 5 bits of |
| 81 | * relevant information stored in status register 1: |
| 82 | * sec: This bit indicates the units (sectors vs. blocks) |
| 83 | * tb: The top-bottom bit indicates if the affected range is at the top of |
| 84 | * the flash memory's address space or at the bottom. |
| 85 | * bp[2:0]: The number of affected sectors/blocks. |
| 86 | */ |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 87 | struct w25q_range { |
| 88 | enum bit_state sec; /* if 1, bp[2:0] describe sectors */ |
| 89 | enum bit_state tb; /* top/bottom select */ |
Louis Yung-Chieh Lo | edd3930 | 2011-11-10 15:43:06 +0800 | [diff] [blame] | 90 | int bp; /* block protect bitfield */ |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 91 | struct wp_range range; |
| 92 | }; |
| 93 | |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame^] | 94 | /* |
| 95 | * Mask to extract write-protect enable and range bits |
| 96 | * Status register 1: |
| 97 | * SRP0: bit 7 |
| 98 | * range(BP2-BP0): bit 4-2 |
| 99 | * Status register 2: |
| 100 | * SRP1: bit 1 |
| 101 | */ |
| 102 | #define MASK_WP_AREA (0x9C) |
| 103 | #define MASK_WP2_AREA (0x01) |
| 104 | |
David Hendricks | 57566ed | 2010-08-16 18:24:45 -0700 | [diff] [blame] | 105 | struct w25q_range en25f40_ranges[] = { |
| 106 | { X, X, 0, {0, 0} }, /* none */ |
| 107 | { 0, 0, 0x1, {0x000000, 504 * 1024} }, |
| 108 | { 0, 0, 0x2, {0x000000, 496 * 1024} }, |
| 109 | { 0, 0, 0x3, {0x000000, 480 * 1024} }, |
| 110 | { 0, 0, 0x4, {0x000000, 448 * 1024} }, |
| 111 | { 0, 0, 0x5, {0x000000, 384 * 1024} }, |
| 112 | { 0, 0, 0x6, {0x000000, 256 * 1024} }, |
| 113 | { 0, 0, 0x7, {0x000000, 512 * 1024} }, |
| 114 | }; |
| 115 | |
David Hendricks | e185bf2 | 2011-05-24 15:34:18 -0700 | [diff] [blame] | 116 | struct w25q_range en25q40_ranges[] = { |
| 117 | { 0, 0, 0, {0, 0} }, /* none */ |
| 118 | { 0, 0, 0x1, {0x000000, 504 * 1024} }, |
| 119 | { 0, 0, 0x2, {0x000000, 496 * 1024} }, |
| 120 | { 0, 0, 0x3, {0x000000, 480 * 1024} }, |
| 121 | |
| 122 | { 0, 1, 0x0, {0x000000, 448 * 1024} }, |
| 123 | { 0, 1, 0x1, {0x000000, 384 * 1024} }, |
| 124 | { 0, 1, 0x2, {0x000000, 256 * 1024} }, |
| 125 | { 0, 1, 0x3, {0x000000, 512 * 1024} }, |
| 126 | }; |
| 127 | |
| 128 | struct w25q_range en25q80_ranges[] = { |
| 129 | { 0, 0, 0, {0, 0} }, /* none */ |
| 130 | { 0, 0, 0x1, {0x000000, 1016 * 1024} }, |
| 131 | { 0, 0, 0x2, {0x000000, 1008 * 1024} }, |
| 132 | { 0, 0, 0x3, {0x000000, 992 * 1024} }, |
| 133 | { 0, 0, 0x4, {0x000000, 960 * 1024} }, |
| 134 | { 0, 0, 0x5, {0x000000, 896 * 1024} }, |
| 135 | { 0, 0, 0x6, {0x000000, 768 * 1024} }, |
| 136 | { 0, 0, 0x7, {0x000000, 1024 * 1024} }, |
| 137 | }; |
| 138 | |
| 139 | struct w25q_range en25q32_ranges[] = { |
| 140 | { 0, 0, 0, {0, 0} }, /* none */ |
| 141 | { 0, 0, 0x1, {0x000000, 4032 * 1024} }, |
| 142 | { 0, 0, 0x2, {0x000000, 3968 * 1024} }, |
| 143 | { 0, 0, 0x3, {0x000000, 3840 * 1024} }, |
| 144 | { 0, 0, 0x4, {0x000000, 3584 * 1024} }, |
| 145 | { 0, 0, 0x5, {0x000000, 3072 * 1024} }, |
| 146 | { 0, 0, 0x6, {0x000000, 2048 * 1024} }, |
| 147 | { 0, 0, 0x7, {0x000000, 4096 * 1024} }, |
| 148 | |
| 149 | { 0, 1, 0, {0, 0} }, /* none */ |
| 150 | { 0, 1, 0x1, {0x010000, 4032 * 1024} }, |
| 151 | { 0, 1, 0x2, {0x020000, 3968 * 1024} }, |
| 152 | { 0, 1, 0x3, {0x040000, 3840 * 1024} }, |
| 153 | { 0, 1, 0x4, {0x080000, 3584 * 1024} }, |
| 154 | { 0, 1, 0x5, {0x100000, 3072 * 1024} }, |
| 155 | { 0, 1, 0x6, {0x200000, 2048 * 1024} }, |
| 156 | { 0, 1, 0x7, {0x000000, 4096 * 1024} }, |
| 157 | }; |
| 158 | |
| 159 | struct w25q_range en25q64_ranges[] = { |
| 160 | { 0, 0, 0, {0, 0} }, /* none */ |
| 161 | { 0, 0, 0x1, {0x000000, 8128 * 1024} }, |
| 162 | { 0, 0, 0x2, {0x000000, 8064 * 1024} }, |
| 163 | { 0, 0, 0x3, {0x000000, 7936 * 1024} }, |
| 164 | { 0, 0, 0x4, {0x000000, 7680 * 1024} }, |
| 165 | { 0, 0, 0x5, {0x000000, 7168 * 1024} }, |
| 166 | { 0, 0, 0x6, {0x000000, 6144 * 1024} }, |
| 167 | { 0, 0, 0x7, {0x000000, 8192 * 1024} }, |
| 168 | |
| 169 | { 0, 1, 0, {0, 0} }, /* none */ |
| 170 | { 0, 1, 0x1, {0x010000, 8128 * 1024} }, |
| 171 | { 0, 1, 0x2, {0x020000, 8064 * 1024} }, |
| 172 | { 0, 1, 0x3, {0x040000, 7936 * 1024} }, |
| 173 | { 0, 1, 0x4, {0x080000, 7680 * 1024} }, |
| 174 | { 0, 1, 0x5, {0x100000, 7168 * 1024} }, |
| 175 | { 0, 1, 0x6, {0x200000, 6144 * 1024} }, |
| 176 | { 0, 1, 0x7, {0x000000, 8192 * 1024} }, |
| 177 | }; |
| 178 | |
| 179 | struct w25q_range en25q128_ranges[] = { |
| 180 | { 0, 0, 0, {0, 0} }, /* none */ |
| 181 | { 0, 0, 0x1, {0x000000, 16320 * 1024} }, |
| 182 | { 0, 0, 0x2, {0x000000, 16256 * 1024} }, |
| 183 | { 0, 0, 0x3, {0x000000, 16128 * 1024} }, |
| 184 | { 0, 0, 0x4, {0x000000, 15872 * 1024} }, |
| 185 | { 0, 0, 0x5, {0x000000, 15360 * 1024} }, |
| 186 | { 0, 0, 0x6, {0x000000, 14336 * 1024} }, |
| 187 | { 0, 0, 0x7, {0x000000, 16384 * 1024} }, |
| 188 | |
| 189 | { 0, 1, 0, {0, 0} }, /* none */ |
| 190 | { 0, 1, 0x1, {0x010000, 16320 * 1024} }, |
| 191 | { 0, 1, 0x2, {0x020000, 16256 * 1024} }, |
| 192 | { 0, 1, 0x3, {0x040000, 16128 * 1024} }, |
| 193 | { 0, 1, 0x4, {0x080000, 15872 * 1024} }, |
| 194 | { 0, 1, 0x5, {0x100000, 15360 * 1024} }, |
| 195 | { 0, 1, 0x6, {0x200000, 14336 * 1024} }, |
| 196 | { 0, 1, 0x7, {0x000000, 16384 * 1024} }, |
| 197 | }; |
| 198 | |
Marc Jones | b2f9002 | 2014-04-29 17:37:23 -0600 | [diff] [blame] | 199 | struct w25q_range en25s64_ranges[] = { |
| 200 | { 0, 0, 0, {0, 0} }, /* none */ |
| 201 | { 0, 0, 0x1, {0x000000, 8064 * 1024} }, |
| 202 | { 0, 0, 0x2, {0x000000, 7936 * 1024} }, |
| 203 | { 0, 0, 0x3, {0x000000, 7680 * 1024} }, |
| 204 | { 0, 0, 0x4, {0x000000, 7168 * 1024} }, |
| 205 | { 0, 0, 0x5, {0x000000, 6144 * 1024} }, |
| 206 | { 0, 0, 0x6, {0x000000, 4096 * 1024} }, |
| 207 | { 0, 0, 0x7, {0x000000, 8192 * 1024} }, |
| 208 | |
| 209 | { 0, 1, 0, {0, 0} }, /* none */ |
| 210 | { 0, 1, 0x1, {0x7e0000, 128 * 1024} }, |
| 211 | { 0, 1, 0x2, {0x7c0000, 256 * 1024} }, |
| 212 | { 0, 1, 0x3, {0x780000, 512 * 1024} }, |
| 213 | { 0, 1, 0x4, {0x700000, 1024 * 1024} }, |
| 214 | { 0, 1, 0x5, {0x600000, 2048 * 1024} }, |
| 215 | { 0, 1, 0x6, {0x400000, 4096 * 1024} }, |
| 216 | { 0, 1, 0x7, {0x000000, 8192 * 1024} }, |
| 217 | }; |
| 218 | |
David Hendricks | f8f00c7 | 2011-02-01 12:39:46 -0800 | [diff] [blame] | 219 | /* mx25l1005 ranges also work for the mx25l1005c */ |
| 220 | static struct w25q_range mx25l1005_ranges[] = { |
| 221 | { X, X, 0, {0, 0} }, /* none */ |
| 222 | { X, X, 0x1, {0x010000, 64 * 1024} }, |
| 223 | { X, X, 0x2, {0x000000, 128 * 1024} }, |
| 224 | { X, X, 0x3, {0x000000, 128 * 1024} }, |
| 225 | }; |
| 226 | |
| 227 | static struct w25q_range mx25l2005_ranges[] = { |
| 228 | { X, X, 0, {0, 0} }, /* none */ |
| 229 | { X, X, 0x1, {0x030000, 64 * 1024} }, |
| 230 | { X, X, 0x2, {0x020000, 128 * 1024} }, |
| 231 | { X, X, 0x3, {0x000000, 256 * 1024} }, |
| 232 | }; |
| 233 | |
| 234 | static struct w25q_range mx25l4005_ranges[] = { |
| 235 | { X, X, 0, {0, 0} }, /* none */ |
| 236 | { X, X, 0x1, {0x070000, 64 * 1 * 1024} }, /* block 7 */ |
| 237 | { X, X, 0x2, {0x060000, 64 * 2 * 1024} }, /* blocks 6-7 */ |
| 238 | { X, X, 0x3, {0x040000, 64 * 4 * 1024} }, /* blocks 4-7 */ |
| 239 | { X, X, 0x4, {0x000000, 512 * 1024} }, |
| 240 | { X, X, 0x5, {0x000000, 512 * 1024} }, |
| 241 | { X, X, 0x6, {0x000000, 512 * 1024} }, |
| 242 | { X, X, 0x7, {0x000000, 512 * 1024} }, |
| 243 | }; |
| 244 | |
| 245 | static struct w25q_range mx25l8005_ranges[] = { |
| 246 | { X, X, 0, {0, 0} }, /* none */ |
| 247 | { X, X, 0x1, {0x0f0000, 64 * 1 * 1024} }, /* block 15 */ |
| 248 | { X, X, 0x2, {0x0e0000, 64 * 2 * 1024} }, /* blocks 14-15 */ |
| 249 | { X, X, 0x3, {0x0c0000, 64 * 4 * 1024} }, /* blocks 12-15 */ |
| 250 | { X, X, 0x4, {0x080000, 64 * 8 * 1024} }, /* blocks 8-15 */ |
| 251 | { X, X, 0x5, {0x000000, 1024 * 1024} }, |
| 252 | { X, X, 0x6, {0x000000, 1024 * 1024} }, |
| 253 | { X, X, 0x7, {0x000000, 1024 * 1024} }, |
| 254 | }; |
| 255 | |
| 256 | #if 0 |
| 257 | /* FIXME: mx25l1605 has the same IDs as the mx25l1605d */ |
| 258 | static struct w25q_range mx25l1605_ranges[] = { |
| 259 | { X, X, 0, {0, 0} }, /* none */ |
| 260 | { X, X, 0x1, {0x1f0000, 64 * 1024} }, /* block 31 */ |
| 261 | { X, X, 0x2, {0x1e0000, 128 * 1024} }, /* blocks 30-31 */ |
| 262 | { X, X, 0x3, {0x1c0000, 256 * 1024} }, /* blocks 28-31 */ |
| 263 | { X, X, 0x4, {0x180000, 512 * 1024} }, /* blocks 24-31 */ |
| 264 | { X, X, 0x4, {0x100000, 1024 * 1024} }, /* blocks 16-31 */ |
| 265 | { X, X, 0x6, {0x000000, 2048 * 1024} }, |
| 266 | { X, X, 0x7, {0x000000, 2048 * 1024} }, |
| 267 | }; |
| 268 | #endif |
| 269 | |
| 270 | #if 0 |
| 271 | /* FIXME: mx25l6405 has the same IDs as the mx25l6405d */ |
| 272 | static struct w25q_range mx25l6405_ranges[] = { |
| 273 | { X, 0, 0, {0, 0} }, /* none */ |
| 274 | { X, 0, 0x1, {0x7f0000, 64 * 1 * 1024} }, /* block 127 */ |
| 275 | { X, 0, 0x2, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */ |
| 276 | { X, 0, 0x3, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */ |
| 277 | { X, 0, 0x4, {0x780000, 64 * 8 * 1024} }, /* blocks 120-127 */ |
| 278 | { X, 0, 0x5, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */ |
| 279 | { X, 0, 0x6, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */ |
| 280 | { X, 0, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */ |
| 281 | |
| 282 | { X, 1, 0x0, {0x000000, 8192 * 1024} }, |
| 283 | { X, 1, 0x1, {0x000000, 8192 * 1024} }, |
| 284 | { X, 1, 0x2, {0x000000, 8192 * 1024} }, |
| 285 | { X, 1, 0x3, {0x000000, 8192 * 1024} }, |
| 286 | { X, 1, 0x4, {0x000000, 8192 * 1024} }, |
| 287 | { X, 1, 0x5, {0x000000, 8192 * 1024} }, |
| 288 | { X, 1, 0x6, {0x000000, 8192 * 1024} }, |
| 289 | { X, 1, 0x7, {0x000000, 8192 * 1024} }, |
| 290 | }; |
| 291 | #endif |
| 292 | |
| 293 | static struct w25q_range mx25l1605d_ranges[] = { |
| 294 | { X, 0, 0, {0, 0} }, /* none */ |
| 295 | { X, 0, 0x1, {0x1f0000, 64 * 1 * 1024} }, /* block 31 */ |
| 296 | { X, 0, 0x2, {0x1e0000, 64 * 2 * 1024} }, /* blocks 30-31 */ |
| 297 | { X, 0, 0x3, {0x1c0000, 64 * 4 * 1024} }, /* blocks 28-31 */ |
| 298 | { X, 0, 0x4, {0x180000, 64 * 8 * 1024} }, /* blocks 24-31 */ |
| 299 | { X, 0, 0x5, {0x100000, 64 * 16 * 1024} }, /* blocks 16-31 */ |
| 300 | { X, 0, 0x6, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */ |
| 301 | { X, 0, 0x7, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */ |
| 302 | |
| 303 | { X, 1, 0x0, {0x000000, 2048 * 1024} }, |
| 304 | { X, 1, 0x1, {0x000000, 2048 * 1024} }, |
| 305 | { X, 1, 0x2, {0x000000, 64 * 16 * 1024} }, /* blocks 0-15 */ |
| 306 | { X, 1, 0x3, {0x000000, 64 * 24 * 1024} }, /* blocks 0-23 */ |
| 307 | { X, 1, 0x4, {0x000000, 64 * 28 * 1024} }, /* blocks 0-27 */ |
| 308 | { X, 1, 0x5, {0x000000, 64 * 30 * 1024} }, /* blocks 0-29 */ |
| 309 | { X, 1, 0x6, {0x000000, 64 * 31 * 1024} }, /* blocks 0-30 */ |
| 310 | { X, 1, 0x7, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */ |
| 311 | }; |
| 312 | |
| 313 | /* FIXME: Is there an mx25l3205 (without a trailing letter)? */ |
David Hendricks | ac72e36 | 2010-08-16 18:20:03 -0700 | [diff] [blame] | 314 | static struct w25q_range mx25l3205d_ranges[] = { |
| 315 | { X, 0, 0, {0, 0} }, /* none */ |
| 316 | { X, 0, 0x1, {0x3f0000, 64 * 1024} }, |
| 317 | { X, 0, 0x2, {0x3e0000, 128 * 1024} }, |
| 318 | { X, 0, 0x3, {0x3c0000, 256 * 1024} }, |
| 319 | { X, 0, 0x4, {0x380000, 512 * 1024} }, |
| 320 | { X, 0, 0x5, {0x300000, 1024 * 1024} }, |
| 321 | { X, 0, 0x6, {0x200000, 2048 * 1024} }, |
| 322 | { X, 0, 0x7, {0x000000, 4096 * 1024} }, |
| 323 | |
| 324 | { X, 1, 0x0, {0x000000, 4096 * 1024} }, |
| 325 | { X, 1, 0x1, {0x000000, 2048 * 1024} }, |
| 326 | { X, 1, 0x2, {0x000000, 3072 * 1024} }, |
| 327 | { X, 1, 0x3, {0x000000, 3584 * 1024} }, |
| 328 | { X, 1, 0x4, {0x000000, 3840 * 1024} }, |
| 329 | { X, 1, 0x5, {0x000000, 3968 * 1024} }, |
| 330 | { X, 1, 0x6, {0x000000, 4032 * 1024} }, |
| 331 | { X, 1, 0x7, {0x000000, 4096 * 1024} }, |
| 332 | }; |
| 333 | |
Vincent Palatin | 87e092a | 2013-02-28 15:46:14 -0800 | [diff] [blame] | 334 | static struct w25q_range mx25u3235e_ranges[] = { |
| 335 | { X, 0, 0, {0, 0} }, /* none */ |
| 336 | { 0, 0, 0x1, {0x3f0000, 64 * 1024} }, |
| 337 | { 0, 0, 0x2, {0x3e0000, 128 * 1024} }, |
| 338 | { 0, 0, 0x3, {0x3c0000, 256 * 1024} }, |
| 339 | { 0, 0, 0x4, {0x380000, 512 * 1024} }, |
| 340 | { 0, 0, 0x5, {0x300000, 1024 * 1024} }, |
| 341 | { 0, 0, 0x6, {0x200000, 2048 * 1024} }, |
| 342 | { 0, 0, 0x7, {0x000000, 4096 * 1024} }, |
| 343 | |
| 344 | { 0, 1, 0x0, {0x000000, 4096 * 1024} }, |
| 345 | { 0, 1, 0x1, {0x000000, 2048 * 1024} }, |
| 346 | { 0, 1, 0x2, {0x000000, 3072 * 1024} }, |
| 347 | { 0, 1, 0x3, {0x000000, 3584 * 1024} }, |
| 348 | { 0, 1, 0x4, {0x000000, 3840 * 1024} }, |
| 349 | { 0, 1, 0x5, {0x000000, 3968 * 1024} }, |
| 350 | { 0, 1, 0x6, {0x000000, 4032 * 1024} }, |
| 351 | { 0, 1, 0x7, {0x000000, 4096 * 1024} }, |
| 352 | }; |
| 353 | |
David Hendricks | 1c9bc9c | 2011-07-20 15:25:44 -0700 | [diff] [blame] | 354 | #if 0 |
| 355 | /* FIXME: MX25L6405D has same ID as MX25L6406 */ |
David Hendricks | f8f00c7 | 2011-02-01 12:39:46 -0800 | [diff] [blame] | 356 | static struct w25q_range mx25l6405d_ranges[] = { |
| 357 | { X, 0, 0, {0, 0} }, /* none */ |
| 358 | { X, 0, 0x1, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */ |
| 359 | { X, 0, 0x2, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */ |
| 360 | { X, 0, 0x3, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */ |
| 361 | { X, 0, 0x4, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */ |
| 362 | { X, 0, 0x5, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */ |
| 363 | { X, 0, 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */ |
| 364 | { X, 0, 0x7, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */ |
| 365 | |
| 366 | { X, 1, 0x0, {0x000000, 8192 * 1024} }, |
| 367 | { X, 1, 0x1, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */ |
| 368 | { X, 1, 0x2, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */ |
| 369 | { X, 1, 0x3, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */ |
| 370 | { X, 1, 0x4, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */ |
| 371 | { X, 1, 0x5, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */ |
| 372 | { X, 1, 0x6, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */ |
| 373 | { X, 1, 0x7, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */ |
| 374 | }; |
David Hendricks | 1c9bc9c | 2011-07-20 15:25:44 -0700 | [diff] [blame] | 375 | #endif |
| 376 | |
| 377 | /* FIXME: MX25L6406 has same ID as MX25L6405D */ |
| 378 | static struct w25q_range mx25l6406e_ranges[] = { |
| 379 | { X, 0, 0, {0, 0} }, /* none */ |
| 380 | { X, 0, 0x1, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */ |
| 381 | { X, 0, 0x2, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */ |
| 382 | { X, 0, 0x3, {0x7a0000, 64 * 8 * 1024} }, /* blocks 120-127 */ |
| 383 | { X, 0, 0x4, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */ |
| 384 | { X, 0, 0x5, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */ |
| 385 | { X, 0, 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */ |
| 386 | { X, 0, 0x7, {0x000000, 64 * 128 * 1024} }, /* all */ |
| 387 | |
| 388 | { X, 1, 0x0, {0x000000, 64 * 128 * 1024} }, /* all */ |
| 389 | { X, 1, 0x1, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */ |
| 390 | { X, 1, 0x2, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */ |
| 391 | { X, 1, 0x3, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */ |
| 392 | { X, 1, 0x4, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */ |
| 393 | { X, 1, 0x5, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */ |
| 394 | { X, 1, 0x6, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */ |
| 395 | { X, 1, 0x7, {0x000000, 64 * 128 * 1024} }, /* all */ |
| 396 | }; |
David Hendricks | f8f00c7 | 2011-02-01 12:39:46 -0800 | [diff] [blame] | 397 | |
David Hendricks | bfa624b | 2012-07-24 12:47:59 -0700 | [diff] [blame] | 398 | static struct w25q_range n25q064_ranges[] = { |
| 399 | { X, 0, 0, {0, 0} }, /* none */ |
| 400 | |
| 401 | { 0, 0, 0x1, {0x7f0000, 64 * 1024} }, /* block 127 */ |
| 402 | { 0, 0, 0x2, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */ |
| 403 | { 0, 0, 0x3, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */ |
| 404 | { 0, 0, 0x4, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */ |
| 405 | { 0, 0, 0x5, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */ |
| 406 | { 0, 0, 0x6, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */ |
| 407 | { 0, 0, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */ |
| 408 | |
| 409 | { 1, 0, 0x1, {0x000000, 64 * 1024} }, /* block 0 */ |
| 410 | { 1, 0, 0x2, {0x000000, 2 * 64 * 1024} }, /* blocks 0-1 */ |
| 411 | { 1, 0, 0x3, {0x000000, 4 * 64 * 1024} }, /* blocks 0-3 */ |
| 412 | { 1, 0, 0x4, {0x000000, 8 * 64 * 1024} }, /* blocks 0-7 */ |
| 413 | { 1, 0, 0x5, {0x000000, 16 * 64 * 1024} }, /* blocks 0-15 */ |
| 414 | { 1, 0, 0x6, {0x000000, 32 * 64 * 1024} }, /* blocks 0-31 */ |
| 415 | { 1, 0, 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */ |
| 416 | |
| 417 | { X, 1, 0x0, {0x000000, 128 * 64 * 1024} }, /* all */ |
| 418 | { X, 1, 0x1, {0x000000, 128 * 64 * 1024} }, /* all */ |
| 419 | { X, 1, 0x2, {0x000000, 128 * 64 * 1024} }, /* all */ |
| 420 | { X, 1, 0x3, {0x000000, 128 * 64 * 1024} }, /* all */ |
| 421 | { X, 1, 0x4, {0x000000, 128 * 64 * 1024} }, /* all */ |
| 422 | { X, 1, 0x5, {0x000000, 128 * 64 * 1024} }, /* all */ |
| 423 | { X, 1, 0x6, {0x000000, 128 * 64 * 1024} }, /* all */ |
| 424 | { X, 1, 0x7, {0x000000, 128 * 64 * 1024} }, /* all */ |
| 425 | }; |
| 426 | |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 427 | static struct w25q_range w25q16_ranges[] = { |
| 428 | { X, X, 0, {0, 0} }, /* none */ |
| 429 | { 0, 0, 0x1, {0x1f0000, 64 * 1024} }, |
| 430 | { 0, 0, 0x2, {0x1e0000, 128 * 1024} }, |
| 431 | { 0, 0, 0x3, {0x1c0000, 256 * 1024} }, |
| 432 | { 0, 0, 0x4, {0x180000, 512 * 1024} }, |
| 433 | { 0, 0, 0x5, {0x100000, 1024 * 1024} }, |
| 434 | |
| 435 | { 0, 1, 0x1, {0x000000, 64 * 1024} }, |
| 436 | { 0, 1, 0x2, {0x000000, 128 * 1024} }, |
| 437 | { 0, 1, 0x3, {0x000000, 256 * 1024} }, |
| 438 | { 0, 1, 0x4, {0x000000, 512 * 1024} }, |
| 439 | { 0, 1, 0x5, {0x000000, 1024 * 1024} }, |
| 440 | { X, X, 0x6, {0x000000, 2048 * 1024} }, |
| 441 | { X, X, 0x7, {0x000000, 2048 * 1024} }, |
| 442 | |
| 443 | { 1, 0, 0x1, {0x1ff000, 4 * 1024} }, |
| 444 | { 1, 0, 0x2, {0x1fe000, 8 * 1024} }, |
| 445 | { 1, 0, 0x3, {0x1fc000, 16 * 1024} }, |
| 446 | { 1, 0, 0x4, {0x1f8000, 32 * 1024} }, |
| 447 | { 1, 0, 0x5, {0x1f8000, 32 * 1024} }, |
| 448 | |
| 449 | { 1, 1, 0x1, {0x000000, 4 * 1024} }, |
| 450 | { 1, 1, 0x2, {0x000000, 8 * 1024} }, |
| 451 | { 1, 1, 0x3, {0x000000, 16 * 1024} }, |
| 452 | { 1, 1, 0x4, {0x000000, 32 * 1024} }, |
| 453 | { 1, 1, 0x5, {0x000000, 32 * 1024} }, |
| 454 | }; |
| 455 | |
| 456 | static struct w25q_range w25q32_ranges[] = { |
| 457 | { X, X, 0, {0, 0} }, /* none */ |
| 458 | { 0, 0, 0x1, {0x3f0000, 64 * 1024} }, |
| 459 | { 0, 0, 0x2, {0x3e0000, 128 * 1024} }, |
| 460 | { 0, 0, 0x3, {0x3c0000, 256 * 1024} }, |
| 461 | { 0, 0, 0x4, {0x380000, 512 * 1024} }, |
| 462 | { 0, 0, 0x5, {0x300000, 1024 * 1024} }, |
David Hendricks | 05653ff | 2010-06-15 16:05:12 -0700 | [diff] [blame] | 463 | { 0, 0, 0x6, {0x200000, 2048 * 1024} }, |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 464 | |
| 465 | { 0, 1, 0x1, {0x000000, 64 * 1024} }, |
| 466 | { 0, 1, 0x2, {0x000000, 128 * 1024} }, |
| 467 | { 0, 1, 0x3, {0x000000, 256 * 1024} }, |
| 468 | { 0, 1, 0x4, {0x000000, 512 * 1024} }, |
| 469 | { 0, 1, 0x5, {0x000000, 1024 * 1024} }, |
| 470 | { 0, 1, 0x6, {0x000000, 2048 * 1024} }, |
| 471 | { X, X, 0x7, {0x000000, 4096 * 1024} }, |
| 472 | |
| 473 | { 1, 0, 0x1, {0x3ff000, 4 * 1024} }, |
| 474 | { 1, 0, 0x2, {0x3fe000, 8 * 1024} }, |
| 475 | { 1, 0, 0x3, {0x3fc000, 16 * 1024} }, |
| 476 | { 1, 0, 0x4, {0x3f8000, 32 * 1024} }, |
| 477 | { 1, 0, 0x5, {0x3f8000, 32 * 1024} }, |
| 478 | |
| 479 | { 1, 1, 0x1, {0x000000, 4 * 1024} }, |
| 480 | { 1, 1, 0x2, {0x000000, 8 * 1024} }, |
| 481 | { 1, 1, 0x3, {0x000000, 16 * 1024} }, |
| 482 | { 1, 1, 0x4, {0x000000, 32 * 1024} }, |
| 483 | { 1, 1, 0x5, {0x000000, 32 * 1024} }, |
| 484 | }; |
| 485 | |
| 486 | static struct w25q_range w25q80_ranges[] = { |
| 487 | { X, X, 0, {0, 0} }, /* none */ |
| 488 | { 0, 0, 0x1, {0x0f0000, 64 * 1024} }, |
| 489 | { 0, 0, 0x2, {0x0e0000, 128 * 1024} }, |
| 490 | { 0, 0, 0x3, {0x0c0000, 256 * 1024} }, |
| 491 | { 0, 0, 0x4, {0x080000, 512 * 1024} }, |
| 492 | |
| 493 | { 0, 1, 0x1, {0x000000, 64 * 1024} }, |
| 494 | { 0, 1, 0x2, {0x000000, 128 * 1024} }, |
| 495 | { 0, 1, 0x3, {0x000000, 256 * 1024} }, |
| 496 | { 0, 1, 0x4, {0x000000, 512 * 1024} }, |
David Hendricks | 05653ff | 2010-06-15 16:05:12 -0700 | [diff] [blame] | 497 | { X, X, 0x6, {0x000000, 1024 * 1024} }, |
| 498 | { X, X, 0x7, {0x000000, 1024 * 1024} }, |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 499 | |
| 500 | { 1, 0, 0x1, {0x1ff000, 4 * 1024} }, |
| 501 | { 1, 0, 0x2, {0x1fe000, 8 * 1024} }, |
| 502 | { 1, 0, 0x3, {0x1fc000, 16 * 1024} }, |
| 503 | { 1, 0, 0x4, {0x1f8000, 32 * 1024} }, |
| 504 | { 1, 0, 0x5, {0x1f8000, 32 * 1024} }, |
| 505 | |
| 506 | { 1, 1, 0x1, {0x000000, 4 * 1024} }, |
| 507 | { 1, 1, 0x2, {0x000000, 8 * 1024} }, |
| 508 | { 1, 1, 0x3, {0x000000, 16 * 1024} }, |
| 509 | { 1, 1, 0x4, {0x000000, 32 * 1024} }, |
| 510 | { 1, 1, 0x5, {0x000000, 32 * 1024} }, |
| 511 | }; |
| 512 | |
David Hendricks | 2c4a76c | 2010-06-28 14:00:43 -0700 | [diff] [blame] | 513 | static struct w25q_range w25q64_ranges[] = { |
| 514 | { X, X, 0, {0, 0} }, /* none */ |
| 515 | |
| 516 | { 0, 0, 0x1, {0x7e0000, 128 * 1024} }, |
| 517 | { 0, 0, 0x2, {0x7c0000, 256 * 1024} }, |
| 518 | { 0, 0, 0x3, {0x780000, 512 * 1024} }, |
| 519 | { 0, 0, 0x4, {0x700000, 1024 * 1024} }, |
| 520 | { 0, 0, 0x5, {0x600000, 2048 * 1024} }, |
| 521 | { 0, 0, 0x6, {0x400000, 4096 * 1024} }, |
| 522 | |
| 523 | { 0, 1, 0x1, {0x000000, 128 * 1024} }, |
| 524 | { 0, 1, 0x2, {0x000000, 256 * 1024} }, |
| 525 | { 0, 1, 0x3, {0x000000, 512 * 1024} }, |
| 526 | { 0, 1, 0x4, {0x000000, 1024 * 1024} }, |
| 527 | { 0, 1, 0x5, {0x000000, 2048 * 1024} }, |
| 528 | { 0, 1, 0x6, {0x000000, 4096 * 1024} }, |
| 529 | { X, X, 0x7, {0x000000, 8192 * 1024} }, |
| 530 | |
| 531 | { 1, 0, 0x1, {0x7ff000, 4 * 1024} }, |
| 532 | { 1, 0, 0x2, {0x7fe000, 8 * 1024} }, |
| 533 | { 1, 0, 0x3, {0x7fc000, 16 * 1024} }, |
| 534 | { 1, 0, 0x4, {0x7f8000, 32 * 1024} }, |
| 535 | { 1, 0, 0x5, {0x7f8000, 32 * 1024} }, |
| 536 | |
| 537 | { 1, 1, 0x1, {0x000000, 4 * 1024} }, |
| 538 | { 1, 1, 0x2, {0x000000, 8 * 1024} }, |
| 539 | { 1, 1, 0x3, {0x000000, 16 * 1024} }, |
| 540 | { 1, 1, 0x4, {0x000000, 32 * 1024} }, |
| 541 | { 1, 1, 0x5, {0x000000, 32 * 1024} }, |
| 542 | }; |
| 543 | |
Louis Yung-Chieh Lo | 232951f | 2010-09-16 11:30:00 +0800 | [diff] [blame] | 544 | struct w25q_range w25x10_ranges[] = { |
| 545 | { X, X, 0, {0, 0} }, /* none */ |
| 546 | { 0, 0, 0x1, {0x010000, 64 * 1024} }, |
| 547 | { 0, 1, 0x1, {0x000000, 64 * 1024} }, |
| 548 | { X, X, 0x2, {0x000000, 128 * 1024} }, |
| 549 | { X, X, 0x3, {0x000000, 128 * 1024} }, |
| 550 | }; |
| 551 | |
| 552 | struct w25q_range w25x20_ranges[] = { |
| 553 | { X, X, 0, {0, 0} }, /* none */ |
| 554 | { 0, 0, 0x1, {0x030000, 64 * 1024} }, |
| 555 | { 0, 0, 0x2, {0x020000, 128 * 1024} }, |
| 556 | { 0, 1, 0x1, {0x000000, 64 * 1024} }, |
| 557 | { 0, 1, 0x2, {0x000000, 128 * 1024} }, |
| 558 | { 0, X, 0x3, {0x000000, 256 * 1024} }, |
| 559 | }; |
| 560 | |
David Hendricks | 470ca95 | 2010-08-13 14:01:53 -0700 | [diff] [blame] | 561 | struct w25q_range w25x40_ranges[] = { |
| 562 | { X, X, 0, {0, 0} }, /* none */ |
| 563 | { 0, 0, 0x1, {0x070000, 64 * 1024} }, |
| 564 | { 0, 0, 0x2, {0x060000, 128 * 1024} }, |
| 565 | { 0, 0, 0x3, {0x040000, 256 * 1024} }, |
| 566 | { 0, 1, 0x1, {0x000000, 64 * 1024} }, |
| 567 | { 0, 1, 0x2, {0x000000, 128 * 1024} }, |
| 568 | { 0, 1, 0x3, {0x000000, 256 * 1024} }, |
| 569 | { 0, X, 0x4, {0x000000, 512 * 1024} }, |
| 570 | }; |
| 571 | |
Louis Yung-Chieh Lo | 232951f | 2010-09-16 11:30:00 +0800 | [diff] [blame] | 572 | struct w25q_range w25x80_ranges[] = { |
| 573 | { X, X, 0, {0, 0} }, /* none */ |
| 574 | { 0, 0, 0x1, {0x0F0000, 64 * 1024} }, |
| 575 | { 0, 0, 0x2, {0x0E0000, 128 * 1024} }, |
| 576 | { 0, 0, 0x3, {0x0C0000, 256 * 1024} }, |
| 577 | { 0, 0, 0x4, {0x080000, 512 * 1024} }, |
| 578 | { 0, 1, 0x1, {0x000000, 64 * 1024} }, |
| 579 | { 0, 1, 0x2, {0x000000, 128 * 1024} }, |
| 580 | { 0, 1, 0x3, {0x000000, 256 * 1024} }, |
| 581 | { 0, 1, 0x4, {0x000000, 512 * 1024} }, |
| 582 | { 0, X, 0x5, {0x000000, 1024 * 1024} }, |
| 583 | { 0, X, 0x6, {0x000000, 1024 * 1024} }, |
| 584 | { 0, X, 0x7, {0x000000, 1024 * 1024} }, |
| 585 | }; |
| 586 | |
Shawn Nematbakhsh | 9e8ef49 | 2012-09-01 21:58:03 -0700 | [diff] [blame] | 587 | static struct w25q_range gd25q64_ranges[] = { |
| 588 | { X, X, 0, {0, 0} }, /* none */ |
| 589 | { 0, 0, 0x1, {0x7e0000, 128 * 1024} }, |
| 590 | { 0, 0, 0x2, {0x7c0000, 256 * 1024} }, |
| 591 | { 0, 0, 0x3, {0x780000, 512 * 1024} }, |
| 592 | { 0, 0, 0x4, {0x700000, 1024 * 1024} }, |
| 593 | { 0, 0, 0x5, {0x600000, 2048 * 1024} }, |
| 594 | { 0, 0, 0x6, {0x400000, 4096 * 1024} }, |
| 595 | |
| 596 | { 0, 1, 0x1, {0x000000, 128 * 1024} }, |
| 597 | { 0, 1, 0x2, {0x000000, 256 * 1024} }, |
| 598 | { 0, 1, 0x3, {0x000000, 512 * 1024} }, |
| 599 | { 0, 1, 0x4, {0x000000, 1024 * 1024} }, |
| 600 | { 0, 1, 0x5, {0x000000, 2048 * 1024} }, |
| 601 | { 0, 1, 0x6, {0x000000, 4096 * 1024} }, |
| 602 | { X, X, 0x7, {0x000000, 8192 * 1024} }, |
| 603 | |
| 604 | { 1, 0, 0x1, {0x7ff000, 4 * 1024} }, |
| 605 | { 1, 0, 0x2, {0x7fe000, 8 * 1024} }, |
| 606 | { 1, 0, 0x3, {0x7fc000, 16 * 1024} }, |
| 607 | { 1, 0, 0x4, {0x7f8000, 32 * 1024} }, |
| 608 | { 1, 0, 0x5, {0x7f8000, 32 * 1024} }, |
| 609 | { 1, 0, 0x6, {0x7f8000, 32 * 1024} }, |
| 610 | |
| 611 | { 1, 1, 0x1, {0x000000, 4 * 1024} }, |
| 612 | { 1, 1, 0x2, {0x000000, 8 * 1024} }, |
| 613 | { 1, 1, 0x3, {0x000000, 16 * 1024} }, |
| 614 | { 1, 1, 0x4, {0x000000, 32 * 1024} }, |
| 615 | { 1, 1, 0x5, {0x000000, 32 * 1024} }, |
| 616 | { 1, 1, 0x6, {0x000000, 32 * 1024} }, |
| 617 | }; |
| 618 | |
Louis Yung-Chieh Lo | c8ec715 | 2012-09-17 17:38:35 +0800 | [diff] [blame] | 619 | static struct w25q_range a25l040_ranges[] = { |
| 620 | { X, X, 0x0, {0, 0} }, /* none */ |
| 621 | { X, X, 0x1, {0x70000, 64 * 1024} }, |
| 622 | { X, X, 0x2, {0x60000, 128 * 1024} }, |
| 623 | { X, X, 0x3, {0x40000, 256 * 1024} }, |
| 624 | { X, X, 0x4, {0x00000, 512 * 1024} }, |
| 625 | { X, X, 0x5, {0x00000, 512 * 1024} }, |
| 626 | { X, X, 0x6, {0x00000, 512 * 1024} }, |
| 627 | { X, X, 0x7, {0x00000, 512 * 1024} }, |
| 628 | }; |
| 629 | |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 630 | /* Given a flash chip, this function returns its range table. */ |
| 631 | static int w25_range_table(const struct flashchip *flash, |
| 632 | struct w25q_range **w25q_ranges, |
| 633 | int *num_entries) |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 634 | { |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 635 | *w25q_ranges = 0; |
| 636 | *num_entries = 0; |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 637 | |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 638 | switch (flash->manufacture_id) { |
| 639 | case WINBOND_NEX_ID: |
| 640 | switch(flash->model_id) { |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 641 | case WINBOND_NEX_W25X10: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 642 | *w25q_ranges = w25x10_ranges; |
| 643 | *num_entries = ARRAY_SIZE(w25x10_ranges); |
Louis Yung-Chieh Lo | 232951f | 2010-09-16 11:30:00 +0800 | [diff] [blame] | 644 | break; |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 645 | case WINBOND_NEX_W25X20: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 646 | *w25q_ranges = w25x20_ranges; |
| 647 | *num_entries = ARRAY_SIZE(w25x20_ranges); |
Louis Yung-Chieh Lo | 232951f | 2010-09-16 11:30:00 +0800 | [diff] [blame] | 648 | break; |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 649 | case WINBOND_NEX_W25X40: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 650 | *w25q_ranges = w25x40_ranges; |
| 651 | *num_entries = ARRAY_SIZE(w25x40_ranges); |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 652 | break; |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 653 | case WINBOND_NEX_W25X80: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 654 | *w25q_ranges = w25x80_ranges; |
| 655 | *num_entries = ARRAY_SIZE(w25x80_ranges); |
Louis Yung-Chieh Lo | 232951f | 2010-09-16 11:30:00 +0800 | [diff] [blame] | 656 | break; |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 657 | case WINBOND_NEX_W25Q80: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 658 | *w25q_ranges = w25q80_ranges; |
| 659 | *num_entries = ARRAY_SIZE(w25q80_ranges); |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 660 | break; |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 661 | case WINBOND_NEX_W25Q16: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 662 | *w25q_ranges = w25q16_ranges; |
| 663 | *num_entries = ARRAY_SIZE(w25q16_ranges); |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 664 | break; |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 665 | case WINBOND_NEX_W25Q32: |
Louis Yung-Chieh Lo | 469707f | 2012-05-18 16:38:37 +0800 | [diff] [blame] | 666 | case WINBOND_NEX_W25Q32DW: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 667 | *w25q_ranges = w25q32_ranges; |
| 668 | *num_entries = ARRAY_SIZE(w25q32_ranges); |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 669 | break; |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 670 | case WINBOND_NEX_W25Q64: |
AdamTsai | 141a262 | 2013-12-31 14:07:15 +0800 | [diff] [blame] | 671 | case WINBOND_NEX_W25Q64DW: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 672 | *w25q_ranges = w25q64_ranges; |
| 673 | *num_entries = ARRAY_SIZE(w25q64_ranges); |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 674 | break; |
| 675 | default: |
| 676 | msg_cerr("%s() %d: WINBOND flash chip mismatch (0x%04x)" |
| 677 | ", aborting\n", __func__, __LINE__, |
| 678 | flash->model_id); |
| 679 | return -1; |
| 680 | } |
David Hendricks | 2c4a76c | 2010-06-28 14:00:43 -0700 | [diff] [blame] | 681 | break; |
David Hendricks | 57566ed | 2010-08-16 18:24:45 -0700 | [diff] [blame] | 682 | case EON_ID_NOPREFIX: |
| 683 | switch (flash->model_id) { |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 684 | case EON_EN25F40: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 685 | *w25q_ranges = en25f40_ranges; |
| 686 | *num_entries = ARRAY_SIZE(en25f40_ranges); |
David Hendricks | 57566ed | 2010-08-16 18:24:45 -0700 | [diff] [blame] | 687 | break; |
David Hendricks | e185bf2 | 2011-05-24 15:34:18 -0700 | [diff] [blame] | 688 | case EON_EN25Q40: |
| 689 | *w25q_ranges = en25q40_ranges; |
| 690 | *num_entries = ARRAY_SIZE(en25q40_ranges); |
| 691 | break; |
| 692 | case EON_EN25Q80: |
| 693 | *w25q_ranges = en25q80_ranges; |
| 694 | *num_entries = ARRAY_SIZE(en25q80_ranges); |
| 695 | break; |
| 696 | case EON_EN25Q32: |
| 697 | *w25q_ranges = en25q32_ranges; |
| 698 | *num_entries = ARRAY_SIZE(en25q32_ranges); |
| 699 | break; |
| 700 | case EON_EN25Q64: |
| 701 | *w25q_ranges = en25q64_ranges; |
| 702 | *num_entries = ARRAY_SIZE(en25q64_ranges); |
| 703 | break; |
| 704 | case EON_EN25Q128: |
| 705 | *w25q_ranges = en25q128_ranges; |
| 706 | *num_entries = ARRAY_SIZE(en25q128_ranges); |
| 707 | break; |
Marc Jones | b2f9002 | 2014-04-29 17:37:23 -0600 | [diff] [blame] | 708 | case EON_EN25S64: |
| 709 | *w25q_ranges = en25s64_ranges; |
| 710 | *num_entries = ARRAY_SIZE(en25s64_ranges); |
| 711 | break; |
David Hendricks | 57566ed | 2010-08-16 18:24:45 -0700 | [diff] [blame] | 712 | default: |
| 713 | msg_cerr("%s():%d: EON flash chip mismatch (0x%04x)" |
| 714 | ", aborting\n", __func__, __LINE__, |
| 715 | flash->model_id); |
| 716 | return -1; |
| 717 | } |
| 718 | break; |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 719 | case MACRONIX_ID: |
David Hendricks | ac72e36 | 2010-08-16 18:20:03 -0700 | [diff] [blame] | 720 | switch (flash->model_id) { |
David Hendricks | f8f00c7 | 2011-02-01 12:39:46 -0800 | [diff] [blame] | 721 | case MACRONIX_MX25L1005: |
| 722 | *w25q_ranges = mx25l1005_ranges; |
| 723 | *num_entries = ARRAY_SIZE(mx25l1005_ranges); |
| 724 | break; |
| 725 | case MACRONIX_MX25L2005: |
| 726 | *w25q_ranges = mx25l2005_ranges; |
| 727 | *num_entries = ARRAY_SIZE(mx25l2005_ranges); |
| 728 | break; |
| 729 | case MACRONIX_MX25L4005: |
| 730 | *w25q_ranges = mx25l4005_ranges; |
| 731 | *num_entries = ARRAY_SIZE(mx25l4005_ranges); |
| 732 | break; |
| 733 | case MACRONIX_MX25L8005: |
| 734 | *w25q_ranges = mx25l8005_ranges; |
| 735 | *num_entries = ARRAY_SIZE(mx25l8005_ranges); |
| 736 | break; |
| 737 | case MACRONIX_MX25L1605: |
| 738 | /* FIXME: MX25L1605 and MX25L1605D have different write |
| 739 | * protection capabilities, but share IDs */ |
| 740 | *w25q_ranges = mx25l1605d_ranges; |
| 741 | *num_entries = ARRAY_SIZE(mx25l1605d_ranges); |
| 742 | break; |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 743 | case MACRONIX_MX25L3205: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 744 | *w25q_ranges = mx25l3205d_ranges; |
| 745 | *num_entries = ARRAY_SIZE(mx25l3205d_ranges); |
David Hendricks | ac72e36 | 2010-08-16 18:20:03 -0700 | [diff] [blame] | 746 | break; |
Vincent Palatin | 87e092a | 2013-02-28 15:46:14 -0800 | [diff] [blame] | 747 | case MACRONIX_MX25U3235E: |
| 748 | *w25q_ranges = mx25u3235e_ranges; |
| 749 | *num_entries = ARRAY_SIZE(mx25u3235e_ranges); |
| 750 | break; |
David Hendricks | f8f00c7 | 2011-02-01 12:39:46 -0800 | [diff] [blame] | 751 | case MACRONIX_MX25L6405: |
David Hendricks | 1c9bc9c | 2011-07-20 15:25:44 -0700 | [diff] [blame] | 752 | /* FIXME: MX25L64* chips have mixed capabilities and |
| 753 | share IDs */ |
| 754 | *w25q_ranges = mx25l6406e_ranges; |
| 755 | *num_entries = ARRAY_SIZE(mx25l6406e_ranges); |
David Hendricks | f8f00c7 | 2011-02-01 12:39:46 -0800 | [diff] [blame] | 756 | break; |
David Hendricks | ac72e36 | 2010-08-16 18:20:03 -0700 | [diff] [blame] | 757 | default: |
| 758 | msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)" |
| 759 | ", aborting\n", __func__, __LINE__, |
| 760 | flash->model_id); |
| 761 | return -1; |
| 762 | } |
| 763 | break; |
David Hendricks | bfa624b | 2012-07-24 12:47:59 -0700 | [diff] [blame] | 764 | case ST_ID: |
| 765 | switch(flash->model_id) { |
| 766 | case ST_N25Q064__1E: |
| 767 | case ST_N25Q064__3E: |
| 768 | *w25q_ranges = n25q064_ranges; |
| 769 | *num_entries = ARRAY_SIZE(n25q064_ranges); |
| 770 | break; |
| 771 | default: |
| 772 | msg_cerr("%s() %d: Micron flash chip mismatch" |
| 773 | " (0x%04x), aborting\n", __func__, __LINE__, |
| 774 | flash->model_id); |
| 775 | return -1; |
| 776 | } |
| 777 | break; |
Bryan Freed | 9a0051f | 2012-05-22 16:06:09 -0700 | [diff] [blame] | 778 | case GIGADEVICE_ID: |
| 779 | switch(flash->model_id) { |
| 780 | case GIGADEVICE_GD25LQ32: |
| 781 | *w25q_ranges = w25q32_ranges; |
| 782 | *num_entries = ARRAY_SIZE(w25q32_ranges); |
| 783 | break; |
Shawn Nematbakhsh | 9e8ef49 | 2012-09-01 21:58:03 -0700 | [diff] [blame] | 784 | case GIGADEVICE_GD25Q64: |
| 785 | *w25q_ranges = gd25q64_ranges; |
| 786 | *num_entries = ARRAY_SIZE(gd25q64_ranges); |
| 787 | break; |
| 788 | /* TODO(shawnn): add support for other GD parts */ |
Bryan Freed | 9a0051f | 2012-05-22 16:06:09 -0700 | [diff] [blame] | 789 | default: |
| 790 | msg_cerr("%s() %d: GigaDevice flash chip mismatch" |
| 791 | " (0x%04x), aborting\n", __func__, __LINE__, |
| 792 | flash->model_id); |
| 793 | return -1; |
| 794 | } |
| 795 | break; |
Louis Yung-Chieh Lo | c8ec715 | 2012-09-17 17:38:35 +0800 | [diff] [blame] | 796 | case AMIC_ID_NOPREFIX: |
| 797 | switch(flash->model_id) { |
| 798 | case AMIC_A25L040: |
| 799 | *w25q_ranges = a25l040_ranges; |
| 800 | *num_entries = ARRAY_SIZE(a25l040_ranges); |
| 801 | break; |
| 802 | default: |
| 803 | msg_cerr("%s() %d: AMIC flash chip mismatch" |
| 804 | " (0x%04x), aborting\n", __func__, __LINE__, |
| 805 | flash->model_id); |
| 806 | return -1; |
| 807 | } |
| 808 | break; |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 809 | default: |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 810 | msg_cerr("%s: flash vendor (0x%x) not found, aborting\n", |
| 811 | __func__, flash->manufacture_id); |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 812 | return -1; |
| 813 | } |
| 814 | |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 815 | return 0; |
| 816 | } |
| 817 | |
| 818 | int w25_range_to_status(const struct flashchip *flash, |
| 819 | unsigned int start, unsigned int len, |
| 820 | struct w25q_status *status) |
| 821 | { |
| 822 | struct w25q_range *w25q_ranges; |
| 823 | int i, range_found = 0; |
| 824 | int num_entries; |
| 825 | |
| 826 | if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1; |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 827 | for (i = 0; i < num_entries; i++) { |
| 828 | struct wp_range *r = &w25q_ranges[i].range; |
| 829 | |
| 830 | msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n", |
| 831 | start, len, r->start, r->len); |
| 832 | if ((start == r->start) && (len == r->len)) { |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 833 | status->bp0 = w25q_ranges[i].bp & 1; |
| 834 | status->bp1 = w25q_ranges[i].bp >> 1; |
| 835 | status->bp2 = w25q_ranges[i].bp >> 2; |
| 836 | status->tb = w25q_ranges[i].tb; |
| 837 | status->sec = w25q_ranges[i].sec; |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 838 | |
| 839 | range_found = 1; |
| 840 | break; |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | if (!range_found) { |
| 845 | msg_cerr("matching range not found\n"); |
| 846 | return -1; |
| 847 | } |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 848 | return 0; |
| 849 | } |
| 850 | |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 851 | int w25_status_to_range(const struct flashchip *flash, |
| 852 | const struct w25q_status *status, |
| 853 | unsigned int *start, unsigned int *len) |
| 854 | { |
| 855 | struct w25q_range *w25q_ranges; |
| 856 | int i, status_found = 0; |
| 857 | int num_entries; |
| 858 | |
| 859 | if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1; |
| 860 | for (i = 0; i < num_entries; i++) { |
| 861 | int bp; |
Louis Yung-Chieh Lo | edd3930 | 2011-11-10 15:43:06 +0800 | [diff] [blame] | 862 | int table_bp, table_tb, table_sec; |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 863 | |
| 864 | bp = status->bp0 | (status->bp1 << 1) | (status->bp2 << 2); |
| 865 | msg_cspew("comparing 0x%x 0x%x / 0x%x 0x%x / 0x%x 0x%x\n", |
| 866 | bp, w25q_ranges[i].bp, |
| 867 | status->tb, w25q_ranges[i].tb, |
| 868 | status->sec, w25q_ranges[i].sec); |
Louis Yung-Chieh Lo | edd3930 | 2011-11-10 15:43:06 +0800 | [diff] [blame] | 869 | table_bp = w25q_ranges[i].bp; |
| 870 | table_tb = w25q_ranges[i].tb; |
| 871 | table_sec = w25q_ranges[i].sec; |
| 872 | if ((bp == table_bp || table_bp == X) && |
| 873 | (status->tb == table_tb || table_tb == X) && |
| 874 | (status->sec == table_sec || table_sec == X)) { |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 875 | *start = w25q_ranges[i].range.start; |
| 876 | *len = w25q_ranges[i].range.len; |
| 877 | |
| 878 | status_found = 1; |
| 879 | break; |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | if (!status_found) { |
| 884 | msg_cerr("matching status not found\n"); |
| 885 | return -1; |
| 886 | } |
| 887 | return 0; |
| 888 | } |
| 889 | |
Louis Yung-Chieh Lo | 52aa930 | 2010-09-06 10:45:02 +0800 | [diff] [blame] | 890 | /* Since most chips we use must be WREN-ed before WRSR, |
| 891 | * we copy a write status function here before we have a good solution. */ |
| 892 | static int spi_write_status_register_WREN(int status) |
| 893 | { |
| 894 | int result; |
| 895 | struct spi_command cmds[] = { |
| 896 | { |
| 897 | /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */ |
| 898 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 899 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 900 | .readcnt = 0, |
| 901 | .readarr = NULL, |
| 902 | }, { |
| 903 | .writecnt = JEDEC_WRSR_OUTSIZE, |
| 904 | .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status }, |
| 905 | .readcnt = 0, |
| 906 | .readarr = NULL, |
| 907 | }, { |
| 908 | .writecnt = 0, |
| 909 | .writearr = NULL, |
| 910 | .readcnt = 0, |
| 911 | .readarr = NULL, |
| 912 | }}; |
| 913 | |
| 914 | result = spi_send_multicommand(cmds); |
| 915 | if (result) { |
| 916 | msg_cerr("%s failed during command execution\n", |
| 917 | __func__); |
| 918 | } |
Louis Yung-Chieh Lo | 96222b1 | 2010-11-01 11:48:11 +0800 | [diff] [blame] | 919 | |
| 920 | /* WRSR performs a self-timed erase before the changes take effect. */ |
| 921 | programmer_delay(WRITE_STATUS_REGISTER_DELAY); |
| 922 | |
Louis Yung-Chieh Lo | 52aa930 | 2010-09-06 10:45:02 +0800 | [diff] [blame] | 923 | return result; |
| 924 | } |
| 925 | |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 926 | /* Given a [start, len], this function calls w25_range_to_status() to convert |
| 927 | * it to flash-chip-specific range bits, then sets into status register. |
| 928 | */ |
David Hendricks | 9104083 | 2011-07-08 20:01:09 -0700 | [diff] [blame] | 929 | static int w25_set_range(const struct flashchip *flash, |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 930 | unsigned int start, unsigned int len) |
| 931 | { |
| 932 | struct w25q_status status; |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 933 | int tmp = 0; |
| 934 | int expected = 0; |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 935 | |
| 936 | memset(&status, 0, sizeof(status)); |
| 937 | tmp = spi_read_status_register(); |
| 938 | memcpy(&status, &tmp, 1); |
| 939 | msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp); |
| 940 | |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 941 | if (w25_range_to_status(flash, start, len, &status)) return -1; |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 942 | |
| 943 | msg_cdbg("status.busy: %x\n", status.busy); |
| 944 | msg_cdbg("status.wel: %x\n", status.wel); |
| 945 | msg_cdbg("status.bp0: %x\n", status.bp0); |
| 946 | msg_cdbg("status.bp1: %x\n", status.bp1); |
| 947 | msg_cdbg("status.bp2: %x\n", status.bp2); |
| 948 | msg_cdbg("status.tb: %x\n", status.tb); |
| 949 | msg_cdbg("status.sec: %x\n", status.sec); |
| 950 | msg_cdbg("status.srp0: %x\n", status.srp0); |
| 951 | |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 952 | memcpy(&expected, &status, sizeof(status)); |
| 953 | spi_write_status_register_WREN(expected); |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 954 | |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 955 | tmp = spi_read_status_register(); |
| 956 | msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp); |
| 957 | if ((tmp & MASK_WP_AREA) == (expected & MASK_WP_AREA)) { |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 958 | return 0; |
| 959 | } else { |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 960 | msg_cerr("expected=0x%02x, but actual=0x%02x.\n", |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 961 | expected, tmp); |
| 962 | return 1; |
| 963 | } |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 964 | } |
| 965 | |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 966 | /* Print out the current status register value with human-readable text. */ |
David Hendricks | 9104083 | 2011-07-08 20:01:09 -0700 | [diff] [blame] | 967 | static int w25_wp_status(const struct flashchip *flash) |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 968 | { |
| 969 | struct w25q_status status; |
| 970 | int tmp; |
David Hendricks | ce8ded3 | 2010-10-08 11:23:38 -0700 | [diff] [blame] | 971 | unsigned int start, len; |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 972 | int ret = 0; |
| 973 | |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 974 | memset(&status, 0, sizeof(status)); |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 975 | tmp = spi_read_status_register(); |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 976 | memcpy(&status, &tmp, 1); |
| 977 | msg_cinfo("WP: status: 0x%02x\n", tmp); |
| 978 | msg_cinfo("WP: status.srp0: %x\n", status.srp0); |
| 979 | msg_cinfo("WP: write protect is %s.\n", |
| 980 | status.srp0 ? "enabled" : "disabled"); |
| 981 | |
| 982 | msg_cinfo("WP: write protect range: "); |
| 983 | if (w25_status_to_range(flash, &status, &start, &len)) { |
| 984 | msg_cinfo("(cannot resolve the range)\n"); |
| 985 | ret = -1; |
| 986 | } else { |
| 987 | msg_cinfo("start=0x%08x, len=0x%08x\n", start, len); |
| 988 | } |
| 989 | |
| 990 | return ret; |
| 991 | } |
| 992 | |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 993 | /* Set/clear the SRP0 bit in the status register. */ |
David Hendricks | 9104083 | 2011-07-08 20:01:09 -0700 | [diff] [blame] | 994 | static int w25_set_srp0(const struct flashchip *flash, int enable) |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 995 | { |
| 996 | struct w25q_status status; |
| 997 | int tmp = 0; |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 998 | int expected = 0; |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 999 | |
| 1000 | memset(&status, 0, sizeof(status)); |
| 1001 | tmp = spi_read_status_register(); |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 1002 | /* FIXME: this is NOT endian-free copy. */ |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 1003 | memcpy(&status, &tmp, 1); |
| 1004 | msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp); |
| 1005 | |
Louis Yung-Chieh Lo | c19d3c5 | 2010-10-08 11:59:16 +0800 | [diff] [blame] | 1006 | status.srp0 = enable ? 1 : 0; |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 1007 | memcpy(&expected, &status, sizeof(status)); |
| 1008 | spi_write_status_register_WREN(expected); |
| 1009 | |
| 1010 | tmp = spi_read_status_register(); |
| 1011 | msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp); |
| 1012 | if ((tmp & MASK_WP_AREA) != (expected & MASK_WP_AREA)) |
| 1013 | return 1; |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 1014 | |
| 1015 | return 0; |
| 1016 | } |
| 1017 | |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1018 | static int w25_enable_writeprotect(const struct flashchip *flash, |
| 1019 | enum wp_mode wp_mode) |
Louis Yung-Chieh Lo | c19d3c5 | 2010-10-08 11:59:16 +0800 | [diff] [blame] | 1020 | { |
| 1021 | int ret; |
| 1022 | |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1023 | switch (wp_mode) { |
| 1024 | case WP_MODE_HARDWARE: |
| 1025 | ret = w25_set_srp0(flash, 1); |
| 1026 | break; |
| 1027 | default: |
| 1028 | msg_cerr("%s(): unsupported write-protect mode\n", __func__); |
| 1029 | return 1; |
| 1030 | } |
| 1031 | |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 1032 | if (ret) |
| 1033 | msg_cerr("%s(): error=%d.\n", __func__, ret); |
Louis Yung-Chieh Lo | c19d3c5 | 2010-10-08 11:59:16 +0800 | [diff] [blame] | 1034 | return ret; |
| 1035 | } |
| 1036 | |
David Hendricks | 9104083 | 2011-07-08 20:01:09 -0700 | [diff] [blame] | 1037 | static int w25_disable_writeprotect(const struct flashchip *flash) |
Louis Yung-Chieh Lo | c19d3c5 | 2010-10-08 11:59:16 +0800 | [diff] [blame] | 1038 | { |
| 1039 | int ret; |
| 1040 | |
| 1041 | ret = w25_set_srp0(flash, 0); |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 1042 | if (ret) |
| 1043 | msg_cerr("%s(): error=%d.\n", __func__, ret); |
Louis Yung-Chieh Lo | c19d3c5 | 2010-10-08 11:59:16 +0800 | [diff] [blame] | 1044 | return ret; |
| 1045 | } |
| 1046 | |
David Hendricks | 9104083 | 2011-07-08 20:01:09 -0700 | [diff] [blame] | 1047 | static int w25_list_ranges(const struct flashchip *flash) |
David Hendricks | 0f7f538 | 2011-02-11 18:12:31 -0800 | [diff] [blame] | 1048 | { |
| 1049 | struct w25q_range *w25q_ranges; |
| 1050 | int i, num_entries; |
| 1051 | |
| 1052 | if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1; |
| 1053 | for (i = 0; i < num_entries; i++) { |
| 1054 | msg_cinfo("start: 0x%06x, length: 0x%06x\n", |
| 1055 | w25q_ranges[i].range.start, |
| 1056 | w25q_ranges[i].range.len); |
| 1057 | } |
| 1058 | |
| 1059 | return 0; |
| 1060 | } |
| 1061 | |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1062 | /* FIXME: Move to spi25.c if it's a JEDEC standard opcode */ |
| 1063 | uint8_t w25q_read_status_register_2(void) |
| 1064 | { |
| 1065 | static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { 0x35 }; |
| 1066 | unsigned char readarr[2]; |
| 1067 | int ret; |
| 1068 | |
| 1069 | /* Read Status Register */ |
| 1070 | ret = spi_send_command(sizeof(cmd), sizeof(readarr), cmd, readarr); |
| 1071 | if (ret) { |
| 1072 | /* |
| 1073 | * FIXME: make this a benign failure for now in case we are |
| 1074 | * unable to execute the opcode |
| 1075 | */ |
| 1076 | msg_cdbg("RDSR2 failed!\n"); |
| 1077 | readarr[0] = 0x00; |
| 1078 | } |
| 1079 | |
| 1080 | return readarr[0]; |
| 1081 | } |
| 1082 | |
| 1083 | static int w25q_wp_status(const struct flashchip *flash) |
| 1084 | { |
| 1085 | struct w25q_status sr1; |
| 1086 | struct w25q_status_2 sr2; |
David Hendricks | f1bd880 | 2012-10-30 11:37:57 -0700 | [diff] [blame] | 1087 | uint8_t tmp[2]; |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1088 | unsigned int start, len; |
| 1089 | int ret = 0; |
| 1090 | |
| 1091 | memset(&sr1, 0, sizeof(sr1)); |
David Hendricks | f1bd880 | 2012-10-30 11:37:57 -0700 | [diff] [blame] | 1092 | tmp[0] = spi_read_status_register(); |
| 1093 | memcpy(&sr1, &tmp[0], 1); |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1094 | |
David Hendricks | f1bd880 | 2012-10-30 11:37:57 -0700 | [diff] [blame] | 1095 | memset(&sr2, 0, sizeof(sr2)); |
| 1096 | tmp[1] = w25q_read_status_register_2(); |
| 1097 | memcpy(&sr2, &tmp[1], 1); |
| 1098 | |
| 1099 | msg_cinfo("WP: status: 0x%02x%02x\n", tmp[1], tmp[0]); |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1100 | msg_cinfo("WP: status.srp0: %x\n", sr1.srp0); |
| 1101 | msg_cinfo("WP: status.srp1: %x\n", sr2.srp1); |
| 1102 | msg_cinfo("WP: write protect is %s.\n", |
| 1103 | (sr1.srp0 || sr2.srp1) ? "enabled" : "disabled"); |
| 1104 | |
| 1105 | msg_cinfo("WP: write protect range: "); |
| 1106 | if (w25_status_to_range(flash, &sr1, &start, &len)) { |
| 1107 | msg_cinfo("(cannot resolve the range)\n"); |
| 1108 | ret = -1; |
| 1109 | } else { |
| 1110 | msg_cinfo("start=0x%08x, len=0x%08x\n", start, len); |
| 1111 | } |
| 1112 | |
| 1113 | return ret; |
| 1114 | } |
| 1115 | |
| 1116 | /* |
| 1117 | * W25Q adds an optional byte to the standard WRSR opcode. If /CS is |
| 1118 | * de-asserted after the first byte, then it acts like a JEDEC-standard |
| 1119 | * WRSR command. if /CS is asserted, then the next data byte is written |
| 1120 | * into status register 2. |
| 1121 | */ |
| 1122 | #define W25Q_WRSR_OUTSIZE 0x03 |
| 1123 | static int w25q_write_status_register_WREN(uint8_t s1, uint8_t s2) |
| 1124 | { |
| 1125 | int result; |
| 1126 | struct spi_command cmds[] = { |
| 1127 | { |
| 1128 | /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */ |
| 1129 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 1130 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 1131 | .readcnt = 0, |
| 1132 | .readarr = NULL, |
| 1133 | }, { |
| 1134 | .writecnt = W25Q_WRSR_OUTSIZE, |
| 1135 | .writearr = (const unsigned char[]){ JEDEC_WRSR, s1, s2 }, |
| 1136 | .readcnt = 0, |
| 1137 | .readarr = NULL, |
| 1138 | }, { |
| 1139 | .writecnt = 0, |
| 1140 | .writearr = NULL, |
| 1141 | .readcnt = 0, |
| 1142 | .readarr = NULL, |
| 1143 | }}; |
| 1144 | |
| 1145 | result = spi_send_multicommand(cmds); |
| 1146 | if (result) { |
| 1147 | msg_cerr("%s failed during command execution\n", |
| 1148 | __func__); |
| 1149 | } |
| 1150 | |
| 1151 | /* WRSR performs a self-timed erase before the changes take effect. */ |
| 1152 | programmer_delay(WRITE_STATUS_REGISTER_DELAY); |
| 1153 | |
| 1154 | return result; |
| 1155 | } |
| 1156 | |
| 1157 | /* |
| 1158 | * Set/clear the SRP1 bit in status register 2. |
| 1159 | * FIXME: make this more generic if other chips use the same SR2 layout |
| 1160 | */ |
| 1161 | static int w25q_set_srp1(const struct flashchip *flash, int enable) |
| 1162 | { |
| 1163 | struct w25q_status sr1; |
| 1164 | struct w25q_status_2 sr2; |
| 1165 | uint8_t tmp, expected; |
| 1166 | |
| 1167 | tmp = spi_read_status_register(); |
| 1168 | memcpy(&sr1, &tmp, 1); |
| 1169 | tmp = w25q_read_status_register_2(); |
| 1170 | memcpy(&sr2, &tmp, 1); |
| 1171 | |
| 1172 | msg_cdbg("%s: old status 2: 0x%02x\n", __func__, tmp); |
| 1173 | |
| 1174 | sr2.srp1 = enable ? 1 : 0; |
| 1175 | |
| 1176 | memcpy(&expected, &sr2, 1); |
| 1177 | w25q_write_status_register_WREN(*((uint8_t *)&sr1), *((uint8_t *)&sr2)); |
| 1178 | |
| 1179 | tmp = w25q_read_status_register_2(); |
| 1180 | msg_cdbg("%s: new status 2: 0x%02x\n", __func__, tmp); |
| 1181 | if ((tmp & MASK_WP2_AREA) != (expected & MASK_WP2_AREA)) |
| 1182 | return 1; |
| 1183 | |
| 1184 | return 0; |
| 1185 | } |
| 1186 | |
| 1187 | enum wp_mode get_wp_mode(const char *mode_str) |
| 1188 | { |
| 1189 | enum wp_mode wp_mode = WP_MODE_UNKNOWN; |
| 1190 | |
| 1191 | if (!strcasecmp(mode_str, "hardware")) |
| 1192 | wp_mode = WP_MODE_HARDWARE; |
| 1193 | else if (!strcasecmp(mode_str, "power_cycle")) |
| 1194 | wp_mode = WP_MODE_POWER_CYCLE; |
| 1195 | else if (!strcasecmp(mode_str, "permanent")) |
| 1196 | wp_mode = WP_MODE_PERMANENT; |
| 1197 | |
| 1198 | return wp_mode; |
| 1199 | } |
| 1200 | |
| 1201 | static int w25q_disable_writeprotect(const struct flashchip *flash, |
| 1202 | enum wp_mode wp_mode) |
| 1203 | { |
| 1204 | int ret = 1; |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1205 | struct w25q_status_2 sr2; |
| 1206 | uint8_t tmp; |
| 1207 | |
| 1208 | switch (wp_mode) { |
| 1209 | case WP_MODE_HARDWARE: |
| 1210 | ret = w25_set_srp0(flash, 0); |
| 1211 | break; |
| 1212 | case WP_MODE_POWER_CYCLE: |
| 1213 | tmp = w25q_read_status_register_2(); |
| 1214 | memcpy(&sr2, &tmp, 1); |
| 1215 | if (sr2.srp1) { |
| 1216 | msg_cerr("%s(): must disconnect power to disable " |
| 1217 | "write-protection\n", __func__); |
| 1218 | } else { |
| 1219 | ret = 0; |
| 1220 | } |
| 1221 | break; |
| 1222 | case WP_MODE_PERMANENT: |
| 1223 | msg_cerr("%s(): cannot disable permanent write-protection\n", |
| 1224 | __func__); |
| 1225 | break; |
| 1226 | default: |
| 1227 | msg_cerr("%s(): invalid mode specified\n", __func__); |
| 1228 | break; |
| 1229 | } |
| 1230 | |
| 1231 | if (ret) |
| 1232 | msg_cerr("%s(): error=%d.\n", __func__, ret); |
| 1233 | return ret; |
| 1234 | } |
| 1235 | |
| 1236 | static int w25q_disable_writeprotect_default(const struct flashchip *flash) |
| 1237 | { |
| 1238 | return w25q_disable_writeprotect(flash, WP_MODE_HARDWARE); |
| 1239 | } |
| 1240 | |
| 1241 | static int w25q_enable_writeprotect(const struct flashchip *flash, |
| 1242 | enum wp_mode wp_mode) |
| 1243 | { |
| 1244 | int ret = 1; |
| 1245 | struct w25q_status sr1; |
| 1246 | struct w25q_status_2 sr2; |
| 1247 | uint8_t tmp; |
| 1248 | |
| 1249 | switch (wp_mode) { |
| 1250 | case WP_MODE_HARDWARE: |
| 1251 | if (w25q_disable_writeprotect(flash, WP_MODE_POWER_CYCLE)) { |
| 1252 | msg_cerr("%s(): cannot disable power cycle WP mode\n", |
| 1253 | __func__); |
| 1254 | break; |
| 1255 | } |
| 1256 | |
| 1257 | tmp = spi_read_status_register(); |
| 1258 | memcpy(&sr1, &tmp, 1); |
| 1259 | if (sr1.srp0) |
| 1260 | ret = 0; |
| 1261 | else |
| 1262 | ret = w25_set_srp0(flash, 1); |
| 1263 | |
| 1264 | break; |
| 1265 | case WP_MODE_POWER_CYCLE: |
| 1266 | if (w25q_disable_writeprotect(flash, WP_MODE_HARDWARE)) { |
| 1267 | msg_cerr("%s(): cannot disable hardware WP mode\n", |
| 1268 | __func__); |
| 1269 | break; |
| 1270 | } |
| 1271 | |
| 1272 | tmp = w25q_read_status_register_2(); |
| 1273 | memcpy(&sr2, &tmp, 1); |
| 1274 | if (sr2.srp1) |
| 1275 | ret = 0; |
| 1276 | else |
| 1277 | ret = w25q_set_srp1(flash, 1); |
| 1278 | |
| 1279 | break; |
| 1280 | case WP_MODE_PERMANENT: |
| 1281 | tmp = spi_read_status_register(); |
| 1282 | memcpy(&sr1, &tmp, 1); |
| 1283 | if (sr1.srp0 == 0) { |
| 1284 | ret = w25_set_srp0(flash, 1); |
| 1285 | if (ret) { |
David Hendricks | f1bd880 | 2012-10-30 11:37:57 -0700 | [diff] [blame] | 1286 | msg_perr("%s(): cannot enable SRP0 for " |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1287 | "permanent WP\n", __func__); |
| 1288 | break; |
| 1289 | } |
| 1290 | } |
| 1291 | |
| 1292 | tmp = w25q_read_status_register_2(); |
| 1293 | memcpy(&sr2, &tmp, 1); |
| 1294 | if (sr2.srp1 == 0) { |
| 1295 | ret = w25q_set_srp1(flash, 1); |
| 1296 | if (ret) { |
David Hendricks | f1bd880 | 2012-10-30 11:37:57 -0700 | [diff] [blame] | 1297 | msg_perr("%s(): cannot enable SRP1 for " |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1298 | "permanent WP\n", __func__); |
| 1299 | break; |
| 1300 | } |
| 1301 | } |
| 1302 | |
| 1303 | break; |
David Hendricks | f1bd880 | 2012-10-30 11:37:57 -0700 | [diff] [blame] | 1304 | default: |
| 1305 | msg_perr("%s(): invalid mode %d\n", __func__, wp_mode); |
| 1306 | break; |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | if (ret) |
| 1310 | msg_cerr("%s(): error=%d.\n", __func__, ret); |
| 1311 | return ret; |
| 1312 | } |
| 1313 | |
| 1314 | /* W25P, W25X, and many flash chips from various vendors */ |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 1315 | struct wp wp_w25 = { |
David Hendricks | 0f7f538 | 2011-02-11 18:12:31 -0800 | [diff] [blame] | 1316 | .list_ranges = w25_list_ranges, |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 1317 | .set_range = w25_set_range, |
| 1318 | .enable = w25_enable_writeprotect, |
Louis Yung-Chieh Lo | c19d3c5 | 2010-10-08 11:59:16 +0800 | [diff] [blame] | 1319 | .disable = w25_disable_writeprotect, |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 1320 | .wp_status = w25_wp_status, |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1321 | |
| 1322 | }; |
| 1323 | |
| 1324 | /* W25Q series has features such as a second status register and SFDP */ |
| 1325 | struct wp wp_w25q = { |
| 1326 | .list_ranges = w25_list_ranges, |
| 1327 | .set_range = w25_set_range, |
| 1328 | .enable = w25q_enable_writeprotect, |
| 1329 | /* |
| 1330 | * By default, disable hardware write-protection. We may change |
| 1331 | * this later if we want to add fine-grained write-protect disable |
| 1332 | * as a command-line option. |
| 1333 | */ |
| 1334 | .disable = w25q_disable_writeprotect_default, |
| 1335 | .wp_status = w25q_wp_status, |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 1336 | }; |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame^] | 1337 | |
| 1338 | |
| 1339 | /* Given a flash chip, this function returns its writeprotect info. */ |
| 1340 | static int generic_range_table(const struct flashchip *flash, |
| 1341 | struct generic_wp **wp, |
| 1342 | int *num_entries) |
| 1343 | { |
| 1344 | *wp = NULL; |
| 1345 | *num_entries = 0; |
| 1346 | |
| 1347 | switch (flash->manufacture_id) { |
| 1348 | default: |
| 1349 | msg_cerr("%s: flash vendor (0x%x) not found, aborting\n", |
| 1350 | __func__, flash->manufacture_id); |
| 1351 | return -1; |
| 1352 | } |
| 1353 | |
| 1354 | return 0; |
| 1355 | } |
| 1356 | |
| 1357 | /* Given a [start, len], this function finds a block protect bit combination |
| 1358 | * (if possible) and sets the corresponding bits in "status". Remaining bits |
| 1359 | * are preserved. */ |
| 1360 | static int generic_range_to_status(const struct flashchip *flash, |
| 1361 | unsigned int start, unsigned int len, |
| 1362 | uint8_t *status) |
| 1363 | { |
| 1364 | struct generic_wp *wp; |
| 1365 | struct generic_range *r; |
| 1366 | int i, range_found = 0, num_entries; |
| 1367 | uint8_t bp_mask; |
| 1368 | |
| 1369 | if (generic_range_table(flash, &wp, &num_entries)) |
| 1370 | return -1; |
| 1371 | |
| 1372 | bp_mask = ((1 << (wp->sr1.bp0_pos + wp->sr1.bp_bits)) - 1) - \ |
| 1373 | ((1 << wp->sr1.bp0_pos) - 1); |
| 1374 | |
| 1375 | for (i = 0, r = &wp->ranges[0]; i < num_entries; i++, r++) { |
| 1376 | msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n", |
| 1377 | start, len, r->range.start, r->range.len); |
| 1378 | if ((start == r->range.start) && (len == r->range.len)) { |
| 1379 | *status &= ~(bp_mask); |
| 1380 | *status |= r->bp << (wp->sr1.bp0_pos); |
| 1381 | range_found = 1; |
| 1382 | break; |
| 1383 | } |
| 1384 | } |
| 1385 | |
| 1386 | if (!range_found) { |
| 1387 | msg_cerr("matching range not found\n"); |
| 1388 | return -1; |
| 1389 | } |
| 1390 | return 0; |
| 1391 | } |
| 1392 | |
| 1393 | static int generic_status_to_range(const struct flashchip *flash, |
| 1394 | const uint8_t sr1, unsigned int *start, unsigned int *len) |
| 1395 | { |
| 1396 | struct generic_wp *wp; |
| 1397 | struct generic_range *r; |
| 1398 | int num_entries, wp_en, i, status_found = 0; |
| 1399 | uint8_t sr1_bp; |
| 1400 | |
| 1401 | if (generic_range_table(flash, &wp, &num_entries)) |
| 1402 | return -1; |
| 1403 | |
| 1404 | sr1_bp = (sr1 >> wp->sr1.bp0_pos) & ((1 << wp->sr1.bp_bits) - 1); |
| 1405 | |
| 1406 | for (i = 0, r = &wp->ranges[0]; i < num_entries; i++, r++) { |
| 1407 | msg_cspew("comparing 0x%02x 0x%02x\n", sr1_bp, r->bp); |
| 1408 | if (sr1_bp == r->bp) { |
| 1409 | *start = r->range.start; |
| 1410 | *len = r->range.len; |
| 1411 | status_found = 1; |
| 1412 | break; |
| 1413 | } |
| 1414 | } |
| 1415 | |
| 1416 | if (!status_found) { |
| 1417 | msg_cerr("matching status not found\n"); |
| 1418 | return -1; |
| 1419 | } |
| 1420 | return 0; |
| 1421 | } |
| 1422 | |
| 1423 | /* Given a [start, len], this function calls generic_range_to_status() to |
| 1424 | * convert it to flash-chip-specific range bits, then sets into status register. |
| 1425 | */ |
| 1426 | static int generic_set_range(const struct flashchip *flash, |
| 1427 | unsigned int start, unsigned int len) |
| 1428 | { |
| 1429 | uint8_t status, expected; |
| 1430 | |
| 1431 | status = spi_read_status_register(); |
| 1432 | msg_cdbg("%s: old status: 0x%02x\n", __func__, status); |
| 1433 | |
| 1434 | expected = status; /* preserve non-bp bits */ |
| 1435 | if (generic_range_to_status(flash, start, len, &expected)) |
| 1436 | return -1; |
| 1437 | |
| 1438 | spi_write_status_register_WREN(expected); |
| 1439 | |
| 1440 | status = spi_read_status_register(); |
| 1441 | msg_cdbg("%s: new status: 0x%02x\n", __func__, status); |
| 1442 | if (status != expected) { |
| 1443 | msg_cerr("expected=0x%02x, but actual=0x%02x.\n", |
| 1444 | expected, status); |
| 1445 | return 1; |
| 1446 | } |
| 1447 | |
| 1448 | return 0; |
| 1449 | } |
| 1450 | |
| 1451 | /* Set/clear the status regsiter write protect bit in SR1. */ |
| 1452 | static int generic_set_srp0(const struct flashchip *flash, int enable) |
| 1453 | { |
| 1454 | uint8_t status, expected; |
| 1455 | struct generic_wp *wp; |
| 1456 | int num_entries; |
| 1457 | |
| 1458 | if (generic_range_table(flash, &wp, &num_entries)) |
| 1459 | return -1; |
| 1460 | |
| 1461 | expected = spi_read_status_register(); |
| 1462 | msg_cdbg("%s: old status: 0x%02x\n", __func__, expected); |
| 1463 | |
| 1464 | if (enable) |
| 1465 | expected |= 1 << wp->sr1.srp_pos; |
| 1466 | else |
| 1467 | expected &= ~(1 << wp->sr1.srp_pos); |
| 1468 | |
| 1469 | spi_write_status_register_WREN(expected); |
| 1470 | |
| 1471 | status = spi_read_status_register(); |
| 1472 | msg_cdbg("%s: new status: 0x%02x\n", __func__, status); |
| 1473 | if (status != expected) |
| 1474 | return -1; |
| 1475 | |
| 1476 | return 0; |
| 1477 | } |
| 1478 | |
| 1479 | static int generic_enable_writeprotect(const struct flashchip *flash, |
| 1480 | enum wp_mode wp_mode) |
| 1481 | { |
| 1482 | int ret; |
| 1483 | |
| 1484 | switch (wp_mode) { |
| 1485 | case WP_MODE_HARDWARE: |
| 1486 | ret = generic_set_srp0(flash, 1); |
| 1487 | break; |
| 1488 | default: |
| 1489 | msg_cerr("%s(): unsupported write-protect mode\n", __func__); |
| 1490 | return 1; |
| 1491 | } |
| 1492 | |
| 1493 | if (ret) |
| 1494 | msg_cerr("%s(): error=%d.\n", __func__, ret); |
| 1495 | return ret; |
| 1496 | } |
| 1497 | |
| 1498 | static int generic_disable_writeprotect(const struct flashchip *flash) |
| 1499 | { |
| 1500 | int ret; |
| 1501 | |
| 1502 | ret = generic_set_srp0(flash, 0); |
| 1503 | if (ret) |
| 1504 | msg_cerr("%s(): error=%d.\n", __func__, ret); |
| 1505 | return ret; |
| 1506 | } |
| 1507 | |
| 1508 | static int generic_list_ranges(const struct flashchip *flash) |
| 1509 | { |
| 1510 | struct generic_wp *wp; |
| 1511 | struct generic_range *r; |
| 1512 | int i, num_entries; |
| 1513 | |
| 1514 | if (generic_range_table(flash, &wp, &num_entries)) |
| 1515 | return -1; |
| 1516 | |
| 1517 | r = &wp->ranges[0]; |
| 1518 | for (i = 0; i < num_entries; i++) { |
| 1519 | msg_cinfo("start: 0x%06x, length: 0x%06x\n", |
| 1520 | r->range.start, r->range.len); |
| 1521 | r++; |
| 1522 | } |
| 1523 | |
| 1524 | return 0; |
| 1525 | } |
| 1526 | |
| 1527 | static int generic_wp_status(const struct flashchip *flash) |
| 1528 | { |
| 1529 | uint8_t sr1; |
| 1530 | unsigned int start, len; |
| 1531 | int ret = 0; |
| 1532 | struct generic_wp *wp; |
| 1533 | struct generic_range *g; |
| 1534 | int num_entries, wp_en; |
| 1535 | |
| 1536 | if (generic_range_table(flash, &wp, &num_entries)) |
| 1537 | return -1; |
| 1538 | |
| 1539 | sr1 = spi_read_status_register(); |
| 1540 | wp_en = (sr1 >> wp->sr1.srp_pos) & 1; |
| 1541 | |
| 1542 | msg_cinfo("WP: status: 0x%04x\n", sr1); |
| 1543 | msg_cinfo("WP: status.srp0: %x\n", wp_en); |
| 1544 | /* FIXME: SRP1 is not really generic, but we probably should print |
| 1545 | * it anyway to have consistent output. #legacycruft */ |
| 1546 | msg_cinfo("WP: status.srp1: %x\n", 0); |
| 1547 | msg_cinfo("WP: write protect is %s.\n", |
| 1548 | wp_en ? "enabled" : "disabled"); |
| 1549 | |
| 1550 | msg_cinfo("WP: write protect range: "); |
| 1551 | if (generic_status_to_range(flash, sr1, &start, &len)) { |
| 1552 | msg_cinfo("(cannot resolve the range)\n"); |
| 1553 | ret = -1; |
| 1554 | } else { |
| 1555 | msg_cinfo("start=0x%08x, len=0x%08x\n", start, len); |
| 1556 | } |
| 1557 | |
| 1558 | return ret; |
| 1559 | } |
| 1560 | |
| 1561 | struct wp wp_generic = { |
| 1562 | .list_ranges = generic_list_ranges, |
| 1563 | .set_range = generic_set_range, |
| 1564 | .enable = generic_enable_writeprotect, |
| 1565 | .disable = generic_disable_writeprotect, |
| 1566 | .wp_status = generic_wp_status, |
| 1567 | }; |