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 | |
Jongpil | 66a9649 | 2014-08-14 17:59:06 +0900 | [diff] [blame] | 354 | static struct w25q_range mx25u6435e_ranges[] = { |
| 355 | { X, 0, 0, {0, 0} }, /* none */ |
| 356 | { 0, 0, 0x1, {0x7f0000, 1 * 64 * 1024} }, /* block 127 */ |
| 357 | { 0, 0, 0x2, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */ |
| 358 | { 0, 0, 0x3, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */ |
| 359 | { 0, 0, 0x4, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */ |
| 360 | { 0, 0, 0x5, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */ |
| 361 | { 0, 0, 0x6, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */ |
| 362 | { 0, 0, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */ |
| 363 | |
| 364 | { 0, 1, 0x0, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */ |
| 365 | { 0, 1, 0x1, {0x000000, 96 * 64 * 1024} }, /* blocks 0-95 */ |
| 366 | { 0, 1, 0x2, {0x000000, 112 * 64 * 1024} }, /* blocks 0-111 */ |
| 367 | { 0, 1, 0x3, {0x000000, 120 * 64 * 1024} }, /* blocks 0-119 */ |
| 368 | { 0, 1, 0x4, {0x000000, 124 * 64 * 1024} }, /* blocks 0-123 */ |
| 369 | { 0, 1, 0x5, {0x000000, 126 * 64 * 1024} }, /* blocks 0-125 */ |
| 370 | { 0, 1, 0x6, {0x000000, 127 * 64 * 1024} }, /* blocks 0-126 */ |
| 371 | { 0, 1, 0x7, {0x000000, 128 * 64 * 1024} }, /* blocks 0-127 */ |
| 372 | }; |
| 373 | |
David Hendricks | bfa624b | 2012-07-24 12:47:59 -0700 | [diff] [blame] | 374 | static struct w25q_range n25q064_ranges[] = { |
| 375 | { X, 0, 0, {0, 0} }, /* none */ |
| 376 | |
| 377 | { 0, 0, 0x1, {0x7f0000, 64 * 1024} }, /* block 127 */ |
| 378 | { 0, 0, 0x2, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */ |
| 379 | { 0, 0, 0x3, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */ |
| 380 | { 0, 0, 0x4, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */ |
| 381 | { 0, 0, 0x5, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */ |
| 382 | { 0, 0, 0x6, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */ |
| 383 | { 0, 0, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */ |
| 384 | |
| 385 | { 1, 0, 0x1, {0x000000, 64 * 1024} }, /* block 0 */ |
| 386 | { 1, 0, 0x2, {0x000000, 2 * 64 * 1024} }, /* blocks 0-1 */ |
| 387 | { 1, 0, 0x3, {0x000000, 4 * 64 * 1024} }, /* blocks 0-3 */ |
| 388 | { 1, 0, 0x4, {0x000000, 8 * 64 * 1024} }, /* blocks 0-7 */ |
| 389 | { 1, 0, 0x5, {0x000000, 16 * 64 * 1024} }, /* blocks 0-15 */ |
| 390 | { 1, 0, 0x6, {0x000000, 32 * 64 * 1024} }, /* blocks 0-31 */ |
| 391 | { 1, 0, 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */ |
| 392 | |
| 393 | { X, 1, 0x0, {0x000000, 128 * 64 * 1024} }, /* all */ |
| 394 | { X, 1, 0x1, {0x000000, 128 * 64 * 1024} }, /* all */ |
| 395 | { X, 1, 0x2, {0x000000, 128 * 64 * 1024} }, /* all */ |
| 396 | { X, 1, 0x3, {0x000000, 128 * 64 * 1024} }, /* all */ |
| 397 | { X, 1, 0x4, {0x000000, 128 * 64 * 1024} }, /* all */ |
| 398 | { X, 1, 0x5, {0x000000, 128 * 64 * 1024} }, /* all */ |
| 399 | { X, 1, 0x6, {0x000000, 128 * 64 * 1024} }, /* all */ |
| 400 | { X, 1, 0x7, {0x000000, 128 * 64 * 1024} }, /* all */ |
| 401 | }; |
| 402 | |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 403 | static struct w25q_range w25q16_ranges[] = { |
| 404 | { X, X, 0, {0, 0} }, /* none */ |
| 405 | { 0, 0, 0x1, {0x1f0000, 64 * 1024} }, |
| 406 | { 0, 0, 0x2, {0x1e0000, 128 * 1024} }, |
| 407 | { 0, 0, 0x3, {0x1c0000, 256 * 1024} }, |
| 408 | { 0, 0, 0x4, {0x180000, 512 * 1024} }, |
| 409 | { 0, 0, 0x5, {0x100000, 1024 * 1024} }, |
| 410 | |
| 411 | { 0, 1, 0x1, {0x000000, 64 * 1024} }, |
| 412 | { 0, 1, 0x2, {0x000000, 128 * 1024} }, |
| 413 | { 0, 1, 0x3, {0x000000, 256 * 1024} }, |
| 414 | { 0, 1, 0x4, {0x000000, 512 * 1024} }, |
| 415 | { 0, 1, 0x5, {0x000000, 1024 * 1024} }, |
| 416 | { X, X, 0x6, {0x000000, 2048 * 1024} }, |
| 417 | { X, X, 0x7, {0x000000, 2048 * 1024} }, |
| 418 | |
| 419 | { 1, 0, 0x1, {0x1ff000, 4 * 1024} }, |
| 420 | { 1, 0, 0x2, {0x1fe000, 8 * 1024} }, |
| 421 | { 1, 0, 0x3, {0x1fc000, 16 * 1024} }, |
| 422 | { 1, 0, 0x4, {0x1f8000, 32 * 1024} }, |
| 423 | { 1, 0, 0x5, {0x1f8000, 32 * 1024} }, |
| 424 | |
| 425 | { 1, 1, 0x1, {0x000000, 4 * 1024} }, |
| 426 | { 1, 1, 0x2, {0x000000, 8 * 1024} }, |
| 427 | { 1, 1, 0x3, {0x000000, 16 * 1024} }, |
| 428 | { 1, 1, 0x4, {0x000000, 32 * 1024} }, |
| 429 | { 1, 1, 0x5, {0x000000, 32 * 1024} }, |
| 430 | }; |
| 431 | |
| 432 | static struct w25q_range w25q32_ranges[] = { |
| 433 | { X, X, 0, {0, 0} }, /* none */ |
| 434 | { 0, 0, 0x1, {0x3f0000, 64 * 1024} }, |
| 435 | { 0, 0, 0x2, {0x3e0000, 128 * 1024} }, |
| 436 | { 0, 0, 0x3, {0x3c0000, 256 * 1024} }, |
| 437 | { 0, 0, 0x4, {0x380000, 512 * 1024} }, |
| 438 | { 0, 0, 0x5, {0x300000, 1024 * 1024} }, |
David Hendricks | 05653ff | 2010-06-15 16:05:12 -0700 | [diff] [blame] | 439 | { 0, 0, 0x6, {0x200000, 2048 * 1024} }, |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 440 | |
| 441 | { 0, 1, 0x1, {0x000000, 64 * 1024} }, |
| 442 | { 0, 1, 0x2, {0x000000, 128 * 1024} }, |
| 443 | { 0, 1, 0x3, {0x000000, 256 * 1024} }, |
| 444 | { 0, 1, 0x4, {0x000000, 512 * 1024} }, |
| 445 | { 0, 1, 0x5, {0x000000, 1024 * 1024} }, |
| 446 | { 0, 1, 0x6, {0x000000, 2048 * 1024} }, |
| 447 | { X, X, 0x7, {0x000000, 4096 * 1024} }, |
| 448 | |
| 449 | { 1, 0, 0x1, {0x3ff000, 4 * 1024} }, |
| 450 | { 1, 0, 0x2, {0x3fe000, 8 * 1024} }, |
| 451 | { 1, 0, 0x3, {0x3fc000, 16 * 1024} }, |
| 452 | { 1, 0, 0x4, {0x3f8000, 32 * 1024} }, |
| 453 | { 1, 0, 0x5, {0x3f8000, 32 * 1024} }, |
| 454 | |
| 455 | { 1, 1, 0x1, {0x000000, 4 * 1024} }, |
| 456 | { 1, 1, 0x2, {0x000000, 8 * 1024} }, |
| 457 | { 1, 1, 0x3, {0x000000, 16 * 1024} }, |
| 458 | { 1, 1, 0x4, {0x000000, 32 * 1024} }, |
| 459 | { 1, 1, 0x5, {0x000000, 32 * 1024} }, |
| 460 | }; |
| 461 | |
| 462 | static struct w25q_range w25q80_ranges[] = { |
| 463 | { X, X, 0, {0, 0} }, /* none */ |
| 464 | { 0, 0, 0x1, {0x0f0000, 64 * 1024} }, |
| 465 | { 0, 0, 0x2, {0x0e0000, 128 * 1024} }, |
| 466 | { 0, 0, 0x3, {0x0c0000, 256 * 1024} }, |
| 467 | { 0, 0, 0x4, {0x080000, 512 * 1024} }, |
| 468 | |
| 469 | { 0, 1, 0x1, {0x000000, 64 * 1024} }, |
| 470 | { 0, 1, 0x2, {0x000000, 128 * 1024} }, |
| 471 | { 0, 1, 0x3, {0x000000, 256 * 1024} }, |
| 472 | { 0, 1, 0x4, {0x000000, 512 * 1024} }, |
David Hendricks | 05653ff | 2010-06-15 16:05:12 -0700 | [diff] [blame] | 473 | { X, X, 0x6, {0x000000, 1024 * 1024} }, |
| 474 | { X, X, 0x7, {0x000000, 1024 * 1024} }, |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 475 | |
| 476 | { 1, 0, 0x1, {0x1ff000, 4 * 1024} }, |
| 477 | { 1, 0, 0x2, {0x1fe000, 8 * 1024} }, |
| 478 | { 1, 0, 0x3, {0x1fc000, 16 * 1024} }, |
| 479 | { 1, 0, 0x4, {0x1f8000, 32 * 1024} }, |
| 480 | { 1, 0, 0x5, {0x1f8000, 32 * 1024} }, |
| 481 | |
| 482 | { 1, 1, 0x1, {0x000000, 4 * 1024} }, |
| 483 | { 1, 1, 0x2, {0x000000, 8 * 1024} }, |
| 484 | { 1, 1, 0x3, {0x000000, 16 * 1024} }, |
| 485 | { 1, 1, 0x4, {0x000000, 32 * 1024} }, |
| 486 | { 1, 1, 0x5, {0x000000, 32 * 1024} }, |
| 487 | }; |
| 488 | |
David Hendricks | 2c4a76c | 2010-06-28 14:00:43 -0700 | [diff] [blame] | 489 | static struct w25q_range w25q64_ranges[] = { |
| 490 | { X, X, 0, {0, 0} }, /* none */ |
| 491 | |
| 492 | { 0, 0, 0x1, {0x7e0000, 128 * 1024} }, |
| 493 | { 0, 0, 0x2, {0x7c0000, 256 * 1024} }, |
| 494 | { 0, 0, 0x3, {0x780000, 512 * 1024} }, |
| 495 | { 0, 0, 0x4, {0x700000, 1024 * 1024} }, |
| 496 | { 0, 0, 0x5, {0x600000, 2048 * 1024} }, |
| 497 | { 0, 0, 0x6, {0x400000, 4096 * 1024} }, |
| 498 | |
| 499 | { 0, 1, 0x1, {0x000000, 128 * 1024} }, |
| 500 | { 0, 1, 0x2, {0x000000, 256 * 1024} }, |
| 501 | { 0, 1, 0x3, {0x000000, 512 * 1024} }, |
| 502 | { 0, 1, 0x4, {0x000000, 1024 * 1024} }, |
| 503 | { 0, 1, 0x5, {0x000000, 2048 * 1024} }, |
| 504 | { 0, 1, 0x6, {0x000000, 4096 * 1024} }, |
| 505 | { X, X, 0x7, {0x000000, 8192 * 1024} }, |
| 506 | |
| 507 | { 1, 0, 0x1, {0x7ff000, 4 * 1024} }, |
| 508 | { 1, 0, 0x2, {0x7fe000, 8 * 1024} }, |
| 509 | { 1, 0, 0x3, {0x7fc000, 16 * 1024} }, |
| 510 | { 1, 0, 0x4, {0x7f8000, 32 * 1024} }, |
| 511 | { 1, 0, 0x5, {0x7f8000, 32 * 1024} }, |
| 512 | |
| 513 | { 1, 1, 0x1, {0x000000, 4 * 1024} }, |
| 514 | { 1, 1, 0x2, {0x000000, 8 * 1024} }, |
| 515 | { 1, 1, 0x3, {0x000000, 16 * 1024} }, |
| 516 | { 1, 1, 0x4, {0x000000, 32 * 1024} }, |
| 517 | { 1, 1, 0x5, {0x000000, 32 * 1024} }, |
| 518 | }; |
| 519 | |
Louis Yung-Chieh Lo | 232951f | 2010-09-16 11:30:00 +0800 | [diff] [blame] | 520 | struct w25q_range w25x10_ranges[] = { |
| 521 | { X, X, 0, {0, 0} }, /* none */ |
| 522 | { 0, 0, 0x1, {0x010000, 64 * 1024} }, |
| 523 | { 0, 1, 0x1, {0x000000, 64 * 1024} }, |
| 524 | { X, X, 0x2, {0x000000, 128 * 1024} }, |
| 525 | { X, X, 0x3, {0x000000, 128 * 1024} }, |
| 526 | }; |
| 527 | |
| 528 | struct w25q_range w25x20_ranges[] = { |
| 529 | { X, X, 0, {0, 0} }, /* none */ |
| 530 | { 0, 0, 0x1, {0x030000, 64 * 1024} }, |
| 531 | { 0, 0, 0x2, {0x020000, 128 * 1024} }, |
| 532 | { 0, 1, 0x1, {0x000000, 64 * 1024} }, |
| 533 | { 0, 1, 0x2, {0x000000, 128 * 1024} }, |
| 534 | { 0, X, 0x3, {0x000000, 256 * 1024} }, |
| 535 | }; |
| 536 | |
David Hendricks | 470ca95 | 2010-08-13 14:01:53 -0700 | [diff] [blame] | 537 | struct w25q_range w25x40_ranges[] = { |
| 538 | { X, X, 0, {0, 0} }, /* none */ |
| 539 | { 0, 0, 0x1, {0x070000, 64 * 1024} }, |
| 540 | { 0, 0, 0x2, {0x060000, 128 * 1024} }, |
| 541 | { 0, 0, 0x3, {0x040000, 256 * 1024} }, |
| 542 | { 0, 1, 0x1, {0x000000, 64 * 1024} }, |
| 543 | { 0, 1, 0x2, {0x000000, 128 * 1024} }, |
| 544 | { 0, 1, 0x3, {0x000000, 256 * 1024} }, |
| 545 | { 0, X, 0x4, {0x000000, 512 * 1024} }, |
| 546 | }; |
| 547 | |
Louis Yung-Chieh Lo | 232951f | 2010-09-16 11:30:00 +0800 | [diff] [blame] | 548 | struct w25q_range w25x80_ranges[] = { |
| 549 | { X, X, 0, {0, 0} }, /* none */ |
| 550 | { 0, 0, 0x1, {0x0F0000, 64 * 1024} }, |
| 551 | { 0, 0, 0x2, {0x0E0000, 128 * 1024} }, |
| 552 | { 0, 0, 0x3, {0x0C0000, 256 * 1024} }, |
| 553 | { 0, 0, 0x4, {0x080000, 512 * 1024} }, |
| 554 | { 0, 1, 0x1, {0x000000, 64 * 1024} }, |
| 555 | { 0, 1, 0x2, {0x000000, 128 * 1024} }, |
| 556 | { 0, 1, 0x3, {0x000000, 256 * 1024} }, |
| 557 | { 0, 1, 0x4, {0x000000, 512 * 1024} }, |
| 558 | { 0, X, 0x5, {0x000000, 1024 * 1024} }, |
| 559 | { 0, X, 0x6, {0x000000, 1024 * 1024} }, |
| 560 | { 0, X, 0x7, {0x000000, 1024 * 1024} }, |
| 561 | }; |
| 562 | |
Shawn Nematbakhsh | 9e8ef49 | 2012-09-01 21:58:03 -0700 | [diff] [blame] | 563 | static struct w25q_range gd25q64_ranges[] = { |
| 564 | { X, X, 0, {0, 0} }, /* none */ |
| 565 | { 0, 0, 0x1, {0x7e0000, 128 * 1024} }, |
| 566 | { 0, 0, 0x2, {0x7c0000, 256 * 1024} }, |
| 567 | { 0, 0, 0x3, {0x780000, 512 * 1024} }, |
| 568 | { 0, 0, 0x4, {0x700000, 1024 * 1024} }, |
| 569 | { 0, 0, 0x5, {0x600000, 2048 * 1024} }, |
| 570 | { 0, 0, 0x6, {0x400000, 4096 * 1024} }, |
| 571 | |
| 572 | { 0, 1, 0x1, {0x000000, 128 * 1024} }, |
| 573 | { 0, 1, 0x2, {0x000000, 256 * 1024} }, |
| 574 | { 0, 1, 0x3, {0x000000, 512 * 1024} }, |
| 575 | { 0, 1, 0x4, {0x000000, 1024 * 1024} }, |
| 576 | { 0, 1, 0x5, {0x000000, 2048 * 1024} }, |
| 577 | { 0, 1, 0x6, {0x000000, 4096 * 1024} }, |
| 578 | { X, X, 0x7, {0x000000, 8192 * 1024} }, |
| 579 | |
| 580 | { 1, 0, 0x1, {0x7ff000, 4 * 1024} }, |
| 581 | { 1, 0, 0x2, {0x7fe000, 8 * 1024} }, |
| 582 | { 1, 0, 0x3, {0x7fc000, 16 * 1024} }, |
| 583 | { 1, 0, 0x4, {0x7f8000, 32 * 1024} }, |
| 584 | { 1, 0, 0x5, {0x7f8000, 32 * 1024} }, |
| 585 | { 1, 0, 0x6, {0x7f8000, 32 * 1024} }, |
| 586 | |
| 587 | { 1, 1, 0x1, {0x000000, 4 * 1024} }, |
| 588 | { 1, 1, 0x2, {0x000000, 8 * 1024} }, |
| 589 | { 1, 1, 0x3, {0x000000, 16 * 1024} }, |
| 590 | { 1, 1, 0x4, {0x000000, 32 * 1024} }, |
| 591 | { 1, 1, 0x5, {0x000000, 32 * 1024} }, |
| 592 | { 1, 1, 0x6, {0x000000, 32 * 1024} }, |
| 593 | }; |
| 594 | |
Louis Yung-Chieh Lo | c8ec715 | 2012-09-17 17:38:35 +0800 | [diff] [blame] | 595 | static struct w25q_range a25l040_ranges[] = { |
| 596 | { X, X, 0x0, {0, 0} }, /* none */ |
| 597 | { X, X, 0x1, {0x70000, 64 * 1024} }, |
| 598 | { X, X, 0x2, {0x60000, 128 * 1024} }, |
| 599 | { X, X, 0x3, {0x40000, 256 * 1024} }, |
| 600 | { X, X, 0x4, {0x00000, 512 * 1024} }, |
| 601 | { X, X, 0x5, {0x00000, 512 * 1024} }, |
| 602 | { X, X, 0x6, {0x00000, 512 * 1024} }, |
| 603 | { X, X, 0x7, {0x00000, 512 * 1024} }, |
| 604 | }; |
| 605 | |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 606 | /* Given a flash chip, this function returns its range table. */ |
| 607 | static int w25_range_table(const struct flashchip *flash, |
| 608 | struct w25q_range **w25q_ranges, |
| 609 | int *num_entries) |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 610 | { |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 611 | *w25q_ranges = 0; |
| 612 | *num_entries = 0; |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 613 | |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 614 | switch (flash->manufacture_id) { |
| 615 | case WINBOND_NEX_ID: |
| 616 | switch(flash->model_id) { |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 617 | case WINBOND_NEX_W25X10: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 618 | *w25q_ranges = w25x10_ranges; |
| 619 | *num_entries = ARRAY_SIZE(w25x10_ranges); |
Louis Yung-Chieh Lo | 232951f | 2010-09-16 11:30:00 +0800 | [diff] [blame] | 620 | break; |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 621 | case WINBOND_NEX_W25X20: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 622 | *w25q_ranges = w25x20_ranges; |
| 623 | *num_entries = ARRAY_SIZE(w25x20_ranges); |
Louis Yung-Chieh Lo | 232951f | 2010-09-16 11:30:00 +0800 | [diff] [blame] | 624 | break; |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 625 | case WINBOND_NEX_W25X40: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 626 | *w25q_ranges = w25x40_ranges; |
| 627 | *num_entries = ARRAY_SIZE(w25x40_ranges); |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 628 | break; |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 629 | case WINBOND_NEX_W25X80: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 630 | *w25q_ranges = w25x80_ranges; |
| 631 | *num_entries = ARRAY_SIZE(w25x80_ranges); |
Louis Yung-Chieh Lo | 232951f | 2010-09-16 11:30:00 +0800 | [diff] [blame] | 632 | break; |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 633 | case WINBOND_NEX_W25Q80: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 634 | *w25q_ranges = w25q80_ranges; |
| 635 | *num_entries = ARRAY_SIZE(w25q80_ranges); |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 636 | break; |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 637 | case WINBOND_NEX_W25Q16: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 638 | *w25q_ranges = w25q16_ranges; |
| 639 | *num_entries = ARRAY_SIZE(w25q16_ranges); |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 640 | break; |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 641 | case WINBOND_NEX_W25Q32: |
Louis Yung-Chieh Lo | 469707f | 2012-05-18 16:38:37 +0800 | [diff] [blame] | 642 | case WINBOND_NEX_W25Q32DW: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 643 | *w25q_ranges = w25q32_ranges; |
| 644 | *num_entries = ARRAY_SIZE(w25q32_ranges); |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 645 | break; |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 646 | case WINBOND_NEX_W25Q64: |
AdamTsai | 141a262 | 2013-12-31 14:07:15 +0800 | [diff] [blame] | 647 | case WINBOND_NEX_W25Q64DW: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 648 | *w25q_ranges = w25q64_ranges; |
| 649 | *num_entries = ARRAY_SIZE(w25q64_ranges); |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 650 | break; |
| 651 | default: |
| 652 | msg_cerr("%s() %d: WINBOND flash chip mismatch (0x%04x)" |
| 653 | ", aborting\n", __func__, __LINE__, |
| 654 | flash->model_id); |
| 655 | return -1; |
| 656 | } |
David Hendricks | 2c4a76c | 2010-06-28 14:00:43 -0700 | [diff] [blame] | 657 | break; |
David Hendricks | 57566ed | 2010-08-16 18:24:45 -0700 | [diff] [blame] | 658 | case EON_ID_NOPREFIX: |
| 659 | switch (flash->model_id) { |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 660 | case EON_EN25F40: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 661 | *w25q_ranges = en25f40_ranges; |
| 662 | *num_entries = ARRAY_SIZE(en25f40_ranges); |
David Hendricks | 57566ed | 2010-08-16 18:24:45 -0700 | [diff] [blame] | 663 | break; |
David Hendricks | e185bf2 | 2011-05-24 15:34:18 -0700 | [diff] [blame] | 664 | case EON_EN25Q40: |
| 665 | *w25q_ranges = en25q40_ranges; |
| 666 | *num_entries = ARRAY_SIZE(en25q40_ranges); |
| 667 | break; |
| 668 | case EON_EN25Q80: |
| 669 | *w25q_ranges = en25q80_ranges; |
| 670 | *num_entries = ARRAY_SIZE(en25q80_ranges); |
| 671 | break; |
| 672 | case EON_EN25Q32: |
| 673 | *w25q_ranges = en25q32_ranges; |
| 674 | *num_entries = ARRAY_SIZE(en25q32_ranges); |
| 675 | break; |
| 676 | case EON_EN25Q64: |
| 677 | *w25q_ranges = en25q64_ranges; |
| 678 | *num_entries = ARRAY_SIZE(en25q64_ranges); |
| 679 | break; |
| 680 | case EON_EN25Q128: |
| 681 | *w25q_ranges = en25q128_ranges; |
| 682 | *num_entries = ARRAY_SIZE(en25q128_ranges); |
| 683 | break; |
Marc Jones | b2f9002 | 2014-04-29 17:37:23 -0600 | [diff] [blame] | 684 | case EON_EN25S64: |
| 685 | *w25q_ranges = en25s64_ranges; |
| 686 | *num_entries = ARRAY_SIZE(en25s64_ranges); |
| 687 | break; |
David Hendricks | 57566ed | 2010-08-16 18:24:45 -0700 | [diff] [blame] | 688 | default: |
| 689 | msg_cerr("%s():%d: EON flash chip mismatch (0x%04x)" |
| 690 | ", aborting\n", __func__, __LINE__, |
| 691 | flash->model_id); |
| 692 | return -1; |
| 693 | } |
| 694 | break; |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 695 | case MACRONIX_ID: |
David Hendricks | ac72e36 | 2010-08-16 18:20:03 -0700 | [diff] [blame] | 696 | switch (flash->model_id) { |
David Hendricks | f8f00c7 | 2011-02-01 12:39:46 -0800 | [diff] [blame] | 697 | case MACRONIX_MX25L1005: |
| 698 | *w25q_ranges = mx25l1005_ranges; |
| 699 | *num_entries = ARRAY_SIZE(mx25l1005_ranges); |
| 700 | break; |
| 701 | case MACRONIX_MX25L2005: |
| 702 | *w25q_ranges = mx25l2005_ranges; |
| 703 | *num_entries = ARRAY_SIZE(mx25l2005_ranges); |
| 704 | break; |
| 705 | case MACRONIX_MX25L4005: |
| 706 | *w25q_ranges = mx25l4005_ranges; |
| 707 | *num_entries = ARRAY_SIZE(mx25l4005_ranges); |
| 708 | break; |
| 709 | case MACRONIX_MX25L8005: |
| 710 | *w25q_ranges = mx25l8005_ranges; |
| 711 | *num_entries = ARRAY_SIZE(mx25l8005_ranges); |
| 712 | break; |
| 713 | case MACRONIX_MX25L1605: |
| 714 | /* FIXME: MX25L1605 and MX25L1605D have different write |
| 715 | * protection capabilities, but share IDs */ |
| 716 | *w25q_ranges = mx25l1605d_ranges; |
| 717 | *num_entries = ARRAY_SIZE(mx25l1605d_ranges); |
| 718 | break; |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 719 | case MACRONIX_MX25L3205: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 720 | *w25q_ranges = mx25l3205d_ranges; |
| 721 | *num_entries = ARRAY_SIZE(mx25l3205d_ranges); |
David Hendricks | ac72e36 | 2010-08-16 18:20:03 -0700 | [diff] [blame] | 722 | break; |
Vincent Palatin | 87e092a | 2013-02-28 15:46:14 -0800 | [diff] [blame] | 723 | case MACRONIX_MX25U3235E: |
| 724 | *w25q_ranges = mx25u3235e_ranges; |
| 725 | *num_entries = ARRAY_SIZE(mx25u3235e_ranges); |
| 726 | break; |
Jongpil | 66a9649 | 2014-08-14 17:59:06 +0900 | [diff] [blame] | 727 | case MACRONIX_MX25U6435E: |
| 728 | *w25q_ranges = mx25u6435e_ranges; |
| 729 | *num_entries = ARRAY_SIZE(mx25u6435e_ranges); |
| 730 | break; |
David Hendricks | ac72e36 | 2010-08-16 18:20:03 -0700 | [diff] [blame] | 731 | default: |
| 732 | msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)" |
| 733 | ", aborting\n", __func__, __LINE__, |
| 734 | flash->model_id); |
| 735 | return -1; |
| 736 | } |
| 737 | break; |
David Hendricks | bfa624b | 2012-07-24 12:47:59 -0700 | [diff] [blame] | 738 | case ST_ID: |
| 739 | switch(flash->model_id) { |
| 740 | case ST_N25Q064__1E: |
| 741 | case ST_N25Q064__3E: |
| 742 | *w25q_ranges = n25q064_ranges; |
| 743 | *num_entries = ARRAY_SIZE(n25q064_ranges); |
| 744 | break; |
| 745 | default: |
| 746 | msg_cerr("%s() %d: Micron flash chip mismatch" |
| 747 | " (0x%04x), aborting\n", __func__, __LINE__, |
| 748 | flash->model_id); |
| 749 | return -1; |
| 750 | } |
| 751 | break; |
Bryan Freed | 9a0051f | 2012-05-22 16:06:09 -0700 | [diff] [blame] | 752 | case GIGADEVICE_ID: |
| 753 | switch(flash->model_id) { |
| 754 | case GIGADEVICE_GD25LQ32: |
| 755 | *w25q_ranges = w25q32_ranges; |
| 756 | *num_entries = ARRAY_SIZE(w25q32_ranges); |
| 757 | break; |
Shawn Nematbakhsh | 9e8ef49 | 2012-09-01 21:58:03 -0700 | [diff] [blame] | 758 | case GIGADEVICE_GD25Q64: |
Marc Jones | b18734f | 2014-04-03 16:19:47 -0600 | [diff] [blame] | 759 | case GIGADEVICE_GD25LQ64: |
Shawn Nematbakhsh | 9e8ef49 | 2012-09-01 21:58:03 -0700 | [diff] [blame] | 760 | *w25q_ranges = gd25q64_ranges; |
| 761 | *num_entries = ARRAY_SIZE(gd25q64_ranges); |
| 762 | break; |
| 763 | /* TODO(shawnn): add support for other GD parts */ |
Bryan Freed | 9a0051f | 2012-05-22 16:06:09 -0700 | [diff] [blame] | 764 | default: |
| 765 | msg_cerr("%s() %d: GigaDevice flash chip mismatch" |
| 766 | " (0x%04x), aborting\n", __func__, __LINE__, |
| 767 | flash->model_id); |
| 768 | return -1; |
| 769 | } |
| 770 | break; |
Louis Yung-Chieh Lo | c8ec715 | 2012-09-17 17:38:35 +0800 | [diff] [blame] | 771 | case AMIC_ID_NOPREFIX: |
| 772 | switch(flash->model_id) { |
| 773 | case AMIC_A25L040: |
| 774 | *w25q_ranges = a25l040_ranges; |
| 775 | *num_entries = ARRAY_SIZE(a25l040_ranges); |
| 776 | break; |
| 777 | default: |
| 778 | msg_cerr("%s() %d: AMIC flash chip mismatch" |
| 779 | " (0x%04x), aborting\n", __func__, __LINE__, |
| 780 | flash->model_id); |
| 781 | return -1; |
| 782 | } |
| 783 | break; |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 784 | default: |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 785 | msg_cerr("%s: flash vendor (0x%x) not found, aborting\n", |
| 786 | __func__, flash->manufacture_id); |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 787 | return -1; |
| 788 | } |
| 789 | |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 790 | return 0; |
| 791 | } |
| 792 | |
| 793 | int w25_range_to_status(const struct flashchip *flash, |
| 794 | unsigned int start, unsigned int len, |
| 795 | struct w25q_status *status) |
| 796 | { |
| 797 | struct w25q_range *w25q_ranges; |
| 798 | int i, range_found = 0; |
| 799 | int num_entries; |
| 800 | |
| 801 | if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1; |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 802 | for (i = 0; i < num_entries; i++) { |
| 803 | struct wp_range *r = &w25q_ranges[i].range; |
| 804 | |
| 805 | msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n", |
| 806 | start, len, r->start, r->len); |
| 807 | if ((start == r->start) && (len == r->len)) { |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 808 | status->bp0 = w25q_ranges[i].bp & 1; |
| 809 | status->bp1 = w25q_ranges[i].bp >> 1; |
| 810 | status->bp2 = w25q_ranges[i].bp >> 2; |
| 811 | status->tb = w25q_ranges[i].tb; |
| 812 | status->sec = w25q_ranges[i].sec; |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 813 | |
| 814 | range_found = 1; |
| 815 | break; |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | if (!range_found) { |
| 820 | msg_cerr("matching range not found\n"); |
| 821 | return -1; |
| 822 | } |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 823 | return 0; |
| 824 | } |
| 825 | |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 826 | int w25_status_to_range(const struct flashchip *flash, |
| 827 | const struct w25q_status *status, |
| 828 | unsigned int *start, unsigned int *len) |
| 829 | { |
| 830 | struct w25q_range *w25q_ranges; |
| 831 | int i, status_found = 0; |
| 832 | int num_entries; |
| 833 | |
| 834 | if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1; |
| 835 | for (i = 0; i < num_entries; i++) { |
| 836 | int bp; |
Louis Yung-Chieh Lo | edd3930 | 2011-11-10 15:43:06 +0800 | [diff] [blame] | 837 | int table_bp, table_tb, table_sec; |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 838 | |
| 839 | bp = status->bp0 | (status->bp1 << 1) | (status->bp2 << 2); |
| 840 | msg_cspew("comparing 0x%x 0x%x / 0x%x 0x%x / 0x%x 0x%x\n", |
| 841 | bp, w25q_ranges[i].bp, |
| 842 | status->tb, w25q_ranges[i].tb, |
| 843 | status->sec, w25q_ranges[i].sec); |
Louis Yung-Chieh Lo | edd3930 | 2011-11-10 15:43:06 +0800 | [diff] [blame] | 844 | table_bp = w25q_ranges[i].bp; |
| 845 | table_tb = w25q_ranges[i].tb; |
| 846 | table_sec = w25q_ranges[i].sec; |
| 847 | if ((bp == table_bp || table_bp == X) && |
| 848 | (status->tb == table_tb || table_tb == X) && |
| 849 | (status->sec == table_sec || table_sec == X)) { |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 850 | *start = w25q_ranges[i].range.start; |
| 851 | *len = w25q_ranges[i].range.len; |
| 852 | |
| 853 | status_found = 1; |
| 854 | break; |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | if (!status_found) { |
| 859 | msg_cerr("matching status not found\n"); |
| 860 | return -1; |
| 861 | } |
| 862 | return 0; |
| 863 | } |
| 864 | |
Louis Yung-Chieh Lo | 52aa930 | 2010-09-06 10:45:02 +0800 | [diff] [blame] | 865 | /* Since most chips we use must be WREN-ed before WRSR, |
| 866 | * we copy a write status function here before we have a good solution. */ |
| 867 | static int spi_write_status_register_WREN(int status) |
| 868 | { |
| 869 | int result; |
| 870 | struct spi_command cmds[] = { |
| 871 | { |
| 872 | /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */ |
| 873 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 874 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 875 | .readcnt = 0, |
| 876 | .readarr = NULL, |
| 877 | }, { |
| 878 | .writecnt = JEDEC_WRSR_OUTSIZE, |
| 879 | .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status }, |
| 880 | .readcnt = 0, |
| 881 | .readarr = NULL, |
| 882 | }, { |
| 883 | .writecnt = 0, |
| 884 | .writearr = NULL, |
| 885 | .readcnt = 0, |
| 886 | .readarr = NULL, |
| 887 | }}; |
| 888 | |
| 889 | result = spi_send_multicommand(cmds); |
| 890 | if (result) { |
| 891 | msg_cerr("%s failed during command execution\n", |
| 892 | __func__); |
| 893 | } |
Louis Yung-Chieh Lo | 96222b1 | 2010-11-01 11:48:11 +0800 | [diff] [blame] | 894 | |
| 895 | /* WRSR performs a self-timed erase before the changes take effect. */ |
| 896 | programmer_delay(WRITE_STATUS_REGISTER_DELAY); |
| 897 | |
Louis Yung-Chieh Lo | 52aa930 | 2010-09-06 10:45:02 +0800 | [diff] [blame] | 898 | return result; |
| 899 | } |
| 900 | |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 901 | /* Given a [start, len], this function calls w25_range_to_status() to convert |
| 902 | * it to flash-chip-specific range bits, then sets into status register. |
| 903 | */ |
David Hendricks | 9104083 | 2011-07-08 20:01:09 -0700 | [diff] [blame] | 904 | static int w25_set_range(const struct flashchip *flash, |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 905 | unsigned int start, unsigned int len) |
| 906 | { |
| 907 | struct w25q_status status; |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 908 | int tmp = 0; |
| 909 | int expected = 0; |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 910 | |
| 911 | memset(&status, 0, sizeof(status)); |
| 912 | tmp = spi_read_status_register(); |
| 913 | memcpy(&status, &tmp, 1); |
| 914 | msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp); |
| 915 | |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 916 | if (w25_range_to_status(flash, start, len, &status)) return -1; |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 917 | |
| 918 | msg_cdbg("status.busy: %x\n", status.busy); |
| 919 | msg_cdbg("status.wel: %x\n", status.wel); |
| 920 | msg_cdbg("status.bp0: %x\n", status.bp0); |
| 921 | msg_cdbg("status.bp1: %x\n", status.bp1); |
| 922 | msg_cdbg("status.bp2: %x\n", status.bp2); |
| 923 | msg_cdbg("status.tb: %x\n", status.tb); |
| 924 | msg_cdbg("status.sec: %x\n", status.sec); |
| 925 | msg_cdbg("status.srp0: %x\n", status.srp0); |
| 926 | |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 927 | memcpy(&expected, &status, sizeof(status)); |
| 928 | spi_write_status_register_WREN(expected); |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 929 | |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 930 | tmp = spi_read_status_register(); |
| 931 | msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp); |
| 932 | if ((tmp & MASK_WP_AREA) == (expected & MASK_WP_AREA)) { |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 933 | return 0; |
| 934 | } else { |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 935 | msg_cerr("expected=0x%02x, but actual=0x%02x.\n", |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 936 | expected, tmp); |
| 937 | return 1; |
| 938 | } |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 939 | } |
| 940 | |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 941 | /* Print out the current status register value with human-readable text. */ |
David Hendricks | 9104083 | 2011-07-08 20:01:09 -0700 | [diff] [blame] | 942 | static int w25_wp_status(const struct flashchip *flash) |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 943 | { |
| 944 | struct w25q_status status; |
| 945 | int tmp; |
David Hendricks | ce8ded3 | 2010-10-08 11:23:38 -0700 | [diff] [blame] | 946 | unsigned int start, len; |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 947 | int ret = 0; |
| 948 | |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 949 | memset(&status, 0, sizeof(status)); |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 950 | tmp = spi_read_status_register(); |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 951 | memcpy(&status, &tmp, 1); |
| 952 | msg_cinfo("WP: status: 0x%02x\n", tmp); |
| 953 | msg_cinfo("WP: status.srp0: %x\n", status.srp0); |
| 954 | msg_cinfo("WP: write protect is %s.\n", |
| 955 | status.srp0 ? "enabled" : "disabled"); |
| 956 | |
| 957 | msg_cinfo("WP: write protect range: "); |
| 958 | if (w25_status_to_range(flash, &status, &start, &len)) { |
| 959 | msg_cinfo("(cannot resolve the range)\n"); |
| 960 | ret = -1; |
| 961 | } else { |
| 962 | msg_cinfo("start=0x%08x, len=0x%08x\n", start, len); |
| 963 | } |
| 964 | |
| 965 | return ret; |
| 966 | } |
| 967 | |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 968 | /* Set/clear the SRP0 bit in the status register. */ |
David Hendricks | 9104083 | 2011-07-08 20:01:09 -0700 | [diff] [blame] | 969 | static int w25_set_srp0(const struct flashchip *flash, int enable) |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 970 | { |
| 971 | struct w25q_status status; |
| 972 | int tmp = 0; |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 973 | int expected = 0; |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 974 | |
| 975 | memset(&status, 0, sizeof(status)); |
| 976 | tmp = spi_read_status_register(); |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 977 | /* FIXME: this is NOT endian-free copy. */ |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 978 | memcpy(&status, &tmp, 1); |
| 979 | msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp); |
| 980 | |
Louis Yung-Chieh Lo | c19d3c5 | 2010-10-08 11:59:16 +0800 | [diff] [blame] | 981 | status.srp0 = enable ? 1 : 0; |
Louis Yung-Chieh Lo | 165b464 | 2010-11-26 16:35:26 +0800 | [diff] [blame] | 982 | memcpy(&expected, &status, sizeof(status)); |
| 983 | spi_write_status_register_WREN(expected); |
| 984 | |
| 985 | tmp = spi_read_status_register(); |
| 986 | msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp); |
| 987 | if ((tmp & MASK_WP_AREA) != (expected & MASK_WP_AREA)) |
| 988 | return 1; |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 989 | |
| 990 | return 0; |
| 991 | } |
| 992 | |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 993 | static int w25_enable_writeprotect(const struct flashchip *flash, |
| 994 | enum wp_mode wp_mode) |
Louis Yung-Chieh Lo | c19d3c5 | 2010-10-08 11:59:16 +0800 | [diff] [blame] | 995 | { |
| 996 | int ret; |
| 997 | |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 998 | switch (wp_mode) { |
| 999 | case WP_MODE_HARDWARE: |
| 1000 | ret = w25_set_srp0(flash, 1); |
| 1001 | break; |
| 1002 | default: |
| 1003 | msg_cerr("%s(): unsupported write-protect mode\n", __func__); |
| 1004 | return 1; |
| 1005 | } |
| 1006 | |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 1007 | if (ret) |
| 1008 | msg_cerr("%s(): error=%d.\n", __func__, ret); |
Louis Yung-Chieh Lo | c19d3c5 | 2010-10-08 11:59:16 +0800 | [diff] [blame] | 1009 | return ret; |
| 1010 | } |
| 1011 | |
David Hendricks | 9104083 | 2011-07-08 20:01:09 -0700 | [diff] [blame] | 1012 | static int w25_disable_writeprotect(const struct flashchip *flash) |
Louis Yung-Chieh Lo | c19d3c5 | 2010-10-08 11:59:16 +0800 | [diff] [blame] | 1013 | { |
| 1014 | int ret; |
| 1015 | |
| 1016 | ret = w25_set_srp0(flash, 0); |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 1017 | if (ret) |
| 1018 | msg_cerr("%s(): error=%d.\n", __func__, ret); |
Louis Yung-Chieh Lo | c19d3c5 | 2010-10-08 11:59:16 +0800 | [diff] [blame] | 1019 | return ret; |
| 1020 | } |
| 1021 | |
David Hendricks | 9104083 | 2011-07-08 20:01:09 -0700 | [diff] [blame] | 1022 | static int w25_list_ranges(const struct flashchip *flash) |
David Hendricks | 0f7f538 | 2011-02-11 18:12:31 -0800 | [diff] [blame] | 1023 | { |
| 1024 | struct w25q_range *w25q_ranges; |
| 1025 | int i, num_entries; |
| 1026 | |
| 1027 | if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1; |
| 1028 | for (i = 0; i < num_entries; i++) { |
| 1029 | msg_cinfo("start: 0x%06x, length: 0x%06x\n", |
| 1030 | w25q_ranges[i].range.start, |
| 1031 | w25q_ranges[i].range.len); |
| 1032 | } |
| 1033 | |
| 1034 | return 0; |
| 1035 | } |
| 1036 | |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1037 | /* FIXME: Move to spi25.c if it's a JEDEC standard opcode */ |
| 1038 | uint8_t w25q_read_status_register_2(void) |
| 1039 | { |
| 1040 | static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { 0x35 }; |
| 1041 | unsigned char readarr[2]; |
| 1042 | int ret; |
| 1043 | |
| 1044 | /* Read Status Register */ |
| 1045 | ret = spi_send_command(sizeof(cmd), sizeof(readarr), cmd, readarr); |
| 1046 | if (ret) { |
| 1047 | /* |
| 1048 | * FIXME: make this a benign failure for now in case we are |
| 1049 | * unable to execute the opcode |
| 1050 | */ |
| 1051 | msg_cdbg("RDSR2 failed!\n"); |
| 1052 | readarr[0] = 0x00; |
| 1053 | } |
| 1054 | |
| 1055 | return readarr[0]; |
| 1056 | } |
| 1057 | |
| 1058 | static int w25q_wp_status(const struct flashchip *flash) |
| 1059 | { |
| 1060 | struct w25q_status sr1; |
| 1061 | struct w25q_status_2 sr2; |
David Hendricks | f1bd880 | 2012-10-30 11:37:57 -0700 | [diff] [blame] | 1062 | uint8_t tmp[2]; |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1063 | unsigned int start, len; |
| 1064 | int ret = 0; |
| 1065 | |
| 1066 | memset(&sr1, 0, sizeof(sr1)); |
David Hendricks | f1bd880 | 2012-10-30 11:37:57 -0700 | [diff] [blame] | 1067 | tmp[0] = spi_read_status_register(); |
| 1068 | memcpy(&sr1, &tmp[0], 1); |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1069 | |
David Hendricks | f1bd880 | 2012-10-30 11:37:57 -0700 | [diff] [blame] | 1070 | memset(&sr2, 0, sizeof(sr2)); |
| 1071 | tmp[1] = w25q_read_status_register_2(); |
| 1072 | memcpy(&sr2, &tmp[1], 1); |
| 1073 | |
| 1074 | msg_cinfo("WP: status: 0x%02x%02x\n", tmp[1], tmp[0]); |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1075 | msg_cinfo("WP: status.srp0: %x\n", sr1.srp0); |
| 1076 | msg_cinfo("WP: status.srp1: %x\n", sr2.srp1); |
| 1077 | msg_cinfo("WP: write protect is %s.\n", |
| 1078 | (sr1.srp0 || sr2.srp1) ? "enabled" : "disabled"); |
| 1079 | |
| 1080 | msg_cinfo("WP: write protect range: "); |
| 1081 | if (w25_status_to_range(flash, &sr1, &start, &len)) { |
| 1082 | msg_cinfo("(cannot resolve the range)\n"); |
| 1083 | ret = -1; |
| 1084 | } else { |
| 1085 | msg_cinfo("start=0x%08x, len=0x%08x\n", start, len); |
| 1086 | } |
| 1087 | |
| 1088 | return ret; |
| 1089 | } |
| 1090 | |
| 1091 | /* |
| 1092 | * W25Q adds an optional byte to the standard WRSR opcode. If /CS is |
| 1093 | * de-asserted after the first byte, then it acts like a JEDEC-standard |
| 1094 | * WRSR command. if /CS is asserted, then the next data byte is written |
| 1095 | * into status register 2. |
| 1096 | */ |
| 1097 | #define W25Q_WRSR_OUTSIZE 0x03 |
| 1098 | static int w25q_write_status_register_WREN(uint8_t s1, uint8_t s2) |
| 1099 | { |
| 1100 | int result; |
| 1101 | struct spi_command cmds[] = { |
| 1102 | { |
| 1103 | /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */ |
| 1104 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 1105 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 1106 | .readcnt = 0, |
| 1107 | .readarr = NULL, |
| 1108 | }, { |
| 1109 | .writecnt = W25Q_WRSR_OUTSIZE, |
| 1110 | .writearr = (const unsigned char[]){ JEDEC_WRSR, s1, s2 }, |
| 1111 | .readcnt = 0, |
| 1112 | .readarr = NULL, |
| 1113 | }, { |
| 1114 | .writecnt = 0, |
| 1115 | .writearr = NULL, |
| 1116 | .readcnt = 0, |
| 1117 | .readarr = NULL, |
| 1118 | }}; |
| 1119 | |
| 1120 | result = spi_send_multicommand(cmds); |
| 1121 | if (result) { |
| 1122 | msg_cerr("%s failed during command execution\n", |
| 1123 | __func__); |
| 1124 | } |
| 1125 | |
| 1126 | /* WRSR performs a self-timed erase before the changes take effect. */ |
| 1127 | programmer_delay(WRITE_STATUS_REGISTER_DELAY); |
| 1128 | |
| 1129 | return result; |
| 1130 | } |
| 1131 | |
| 1132 | /* |
| 1133 | * Set/clear the SRP1 bit in status register 2. |
| 1134 | * FIXME: make this more generic if other chips use the same SR2 layout |
| 1135 | */ |
| 1136 | static int w25q_set_srp1(const struct flashchip *flash, int enable) |
| 1137 | { |
| 1138 | struct w25q_status sr1; |
| 1139 | struct w25q_status_2 sr2; |
| 1140 | uint8_t tmp, expected; |
| 1141 | |
| 1142 | tmp = spi_read_status_register(); |
| 1143 | memcpy(&sr1, &tmp, 1); |
| 1144 | tmp = w25q_read_status_register_2(); |
| 1145 | memcpy(&sr2, &tmp, 1); |
| 1146 | |
| 1147 | msg_cdbg("%s: old status 2: 0x%02x\n", __func__, tmp); |
| 1148 | |
| 1149 | sr2.srp1 = enable ? 1 : 0; |
| 1150 | |
| 1151 | memcpy(&expected, &sr2, 1); |
| 1152 | w25q_write_status_register_WREN(*((uint8_t *)&sr1), *((uint8_t *)&sr2)); |
| 1153 | |
| 1154 | tmp = w25q_read_status_register_2(); |
| 1155 | msg_cdbg("%s: new status 2: 0x%02x\n", __func__, tmp); |
| 1156 | if ((tmp & MASK_WP2_AREA) != (expected & MASK_WP2_AREA)) |
| 1157 | return 1; |
| 1158 | |
| 1159 | return 0; |
| 1160 | } |
| 1161 | |
| 1162 | enum wp_mode get_wp_mode(const char *mode_str) |
| 1163 | { |
| 1164 | enum wp_mode wp_mode = WP_MODE_UNKNOWN; |
| 1165 | |
| 1166 | if (!strcasecmp(mode_str, "hardware")) |
| 1167 | wp_mode = WP_MODE_HARDWARE; |
| 1168 | else if (!strcasecmp(mode_str, "power_cycle")) |
| 1169 | wp_mode = WP_MODE_POWER_CYCLE; |
| 1170 | else if (!strcasecmp(mode_str, "permanent")) |
| 1171 | wp_mode = WP_MODE_PERMANENT; |
| 1172 | |
| 1173 | return wp_mode; |
| 1174 | } |
| 1175 | |
| 1176 | static int w25q_disable_writeprotect(const struct flashchip *flash, |
| 1177 | enum wp_mode wp_mode) |
| 1178 | { |
| 1179 | int ret = 1; |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1180 | struct w25q_status_2 sr2; |
| 1181 | uint8_t tmp; |
| 1182 | |
| 1183 | switch (wp_mode) { |
| 1184 | case WP_MODE_HARDWARE: |
| 1185 | ret = w25_set_srp0(flash, 0); |
| 1186 | break; |
| 1187 | case WP_MODE_POWER_CYCLE: |
| 1188 | tmp = w25q_read_status_register_2(); |
| 1189 | memcpy(&sr2, &tmp, 1); |
| 1190 | if (sr2.srp1) { |
| 1191 | msg_cerr("%s(): must disconnect power to disable " |
| 1192 | "write-protection\n", __func__); |
| 1193 | } else { |
| 1194 | ret = 0; |
| 1195 | } |
| 1196 | break; |
| 1197 | case WP_MODE_PERMANENT: |
| 1198 | msg_cerr("%s(): cannot disable permanent write-protection\n", |
| 1199 | __func__); |
| 1200 | break; |
| 1201 | default: |
| 1202 | msg_cerr("%s(): invalid mode specified\n", __func__); |
| 1203 | break; |
| 1204 | } |
| 1205 | |
| 1206 | if (ret) |
| 1207 | msg_cerr("%s(): error=%d.\n", __func__, ret); |
| 1208 | return ret; |
| 1209 | } |
| 1210 | |
| 1211 | static int w25q_disable_writeprotect_default(const struct flashchip *flash) |
| 1212 | { |
| 1213 | return w25q_disable_writeprotect(flash, WP_MODE_HARDWARE); |
| 1214 | } |
| 1215 | |
| 1216 | static int w25q_enable_writeprotect(const struct flashchip *flash, |
| 1217 | enum wp_mode wp_mode) |
| 1218 | { |
| 1219 | int ret = 1; |
| 1220 | struct w25q_status sr1; |
| 1221 | struct w25q_status_2 sr2; |
| 1222 | uint8_t tmp; |
| 1223 | |
| 1224 | switch (wp_mode) { |
| 1225 | case WP_MODE_HARDWARE: |
| 1226 | if (w25q_disable_writeprotect(flash, WP_MODE_POWER_CYCLE)) { |
| 1227 | msg_cerr("%s(): cannot disable power cycle WP mode\n", |
| 1228 | __func__); |
| 1229 | break; |
| 1230 | } |
| 1231 | |
| 1232 | tmp = spi_read_status_register(); |
| 1233 | memcpy(&sr1, &tmp, 1); |
| 1234 | if (sr1.srp0) |
| 1235 | ret = 0; |
| 1236 | else |
| 1237 | ret = w25_set_srp0(flash, 1); |
| 1238 | |
| 1239 | break; |
| 1240 | case WP_MODE_POWER_CYCLE: |
| 1241 | if (w25q_disable_writeprotect(flash, WP_MODE_HARDWARE)) { |
| 1242 | msg_cerr("%s(): cannot disable hardware WP mode\n", |
| 1243 | __func__); |
| 1244 | break; |
| 1245 | } |
| 1246 | |
| 1247 | tmp = w25q_read_status_register_2(); |
| 1248 | memcpy(&sr2, &tmp, 1); |
| 1249 | if (sr2.srp1) |
| 1250 | ret = 0; |
| 1251 | else |
| 1252 | ret = w25q_set_srp1(flash, 1); |
| 1253 | |
| 1254 | break; |
| 1255 | case WP_MODE_PERMANENT: |
| 1256 | tmp = spi_read_status_register(); |
| 1257 | memcpy(&sr1, &tmp, 1); |
| 1258 | if (sr1.srp0 == 0) { |
| 1259 | ret = w25_set_srp0(flash, 1); |
| 1260 | if (ret) { |
David Hendricks | f1bd880 | 2012-10-30 11:37:57 -0700 | [diff] [blame] | 1261 | msg_perr("%s(): cannot enable SRP0 for " |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1262 | "permanent WP\n", __func__); |
| 1263 | break; |
| 1264 | } |
| 1265 | } |
| 1266 | |
| 1267 | tmp = w25q_read_status_register_2(); |
| 1268 | memcpy(&sr2, &tmp, 1); |
| 1269 | if (sr2.srp1 == 0) { |
| 1270 | ret = w25q_set_srp1(flash, 1); |
| 1271 | if (ret) { |
David Hendricks | f1bd880 | 2012-10-30 11:37:57 -0700 | [diff] [blame] | 1272 | msg_perr("%s(): cannot enable SRP1 for " |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1273 | "permanent WP\n", __func__); |
| 1274 | break; |
| 1275 | } |
| 1276 | } |
| 1277 | |
| 1278 | break; |
David Hendricks | f1bd880 | 2012-10-30 11:37:57 -0700 | [diff] [blame] | 1279 | default: |
| 1280 | msg_perr("%s(): invalid mode %d\n", __func__, wp_mode); |
| 1281 | break; |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1282 | } |
| 1283 | |
| 1284 | if (ret) |
| 1285 | msg_cerr("%s(): error=%d.\n", __func__, ret); |
| 1286 | return ret; |
| 1287 | } |
| 1288 | |
| 1289 | /* W25P, W25X, and many flash chips from various vendors */ |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 1290 | struct wp wp_w25 = { |
David Hendricks | 0f7f538 | 2011-02-11 18:12:31 -0800 | [diff] [blame] | 1291 | .list_ranges = w25_list_ranges, |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 1292 | .set_range = w25_set_range, |
| 1293 | .enable = w25_enable_writeprotect, |
Louis Yung-Chieh Lo | c19d3c5 | 2010-10-08 11:59:16 +0800 | [diff] [blame] | 1294 | .disable = w25_disable_writeprotect, |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 1295 | .wp_status = w25_wp_status, |
David Hendricks | 1c09f80 | 2012-10-03 11:03:48 -0700 | [diff] [blame] | 1296 | |
| 1297 | }; |
| 1298 | |
| 1299 | /* W25Q series has features such as a second status register and SFDP */ |
| 1300 | struct wp wp_w25q = { |
| 1301 | .list_ranges = w25_list_ranges, |
| 1302 | .set_range = w25_set_range, |
| 1303 | .enable = w25q_enable_writeprotect, |
| 1304 | /* |
| 1305 | * By default, disable hardware write-protection. We may change |
| 1306 | * this later if we want to add fine-grained write-protect disable |
| 1307 | * as a command-line option. |
| 1308 | */ |
| 1309 | .disable = w25q_disable_writeprotect_default, |
| 1310 | .wp_status = w25q_wp_status, |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 1311 | }; |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 1312 | |
David Hendricks | af3944a | 2014-07-28 18:37:40 -0700 | [diff] [blame] | 1313 | struct generic_range gd25q32_cmp0_ranges[] = { |
| 1314 | /* none, bp4 and bp3 => don't care */ |
| 1315 | { 0x00, {0, 0} }, |
| 1316 | { 0x08, {0, 0} }, |
| 1317 | { 0x10, {0, 0} }, |
| 1318 | { 0x18, {0, 0} }, |
| 1319 | |
| 1320 | { 0x01, {0x3f0000, 64 * 1024} }, |
| 1321 | { 0x02, {0x3e0000, 128 * 1024} }, |
| 1322 | { 0x03, {0x3c0000, 256 * 1024} }, |
| 1323 | { 0x04, {0x380000, 512 * 1024} }, |
| 1324 | { 0x05, {0x300000, 1024 * 1024} }, |
| 1325 | { 0x06, {0x200000, 2048 * 1024} }, |
| 1326 | |
| 1327 | { 0x09, {0x000000, 64 * 1024} }, |
| 1328 | { 0x0a, {0x000000, 128 * 1024} }, |
| 1329 | { 0x0b, {0x000000, 256 * 1024} }, |
| 1330 | { 0x0c, {0x000000, 512 * 1024} }, |
| 1331 | { 0x0d, {0x000000, 1024 * 1024} }, |
| 1332 | { 0x0e, {0x000000, 2048 * 1024} }, |
| 1333 | |
| 1334 | /* all, bp4 and bp3 => don't care */ |
| 1335 | { 0x07, {0x000000, 4096 * 1024} }, |
| 1336 | { 0x0f, {0x000000, 4096 * 1024} }, |
| 1337 | { 0x17, {0x000000, 4096 * 1024} }, |
| 1338 | { 0x1f, {0x000000, 4096 * 1024} }, |
| 1339 | |
| 1340 | { 0x11, {0x3ff000, 4 * 1024} }, |
| 1341 | { 0x12, {0x3fe000, 8 * 1024} }, |
| 1342 | { 0x13, {0x3fc000, 16 * 1024} }, |
| 1343 | { 0x14, {0x3f8000, 32 * 1024} }, /* bp0 => don't care */ |
| 1344 | { 0x15, {0x3f8000, 32 * 1024} }, /* bp0 => don't care */ |
| 1345 | { 0x16, {0x3f8000, 32 * 1024} }, |
| 1346 | |
| 1347 | { 0x19, {0x000000, 4 * 1024} }, |
| 1348 | { 0x1a, {0x000000, 8 * 1024} }, |
| 1349 | { 0x1b, {0x000000, 16 * 1024} }, |
| 1350 | { 0x1c, {0x000000, 32 * 1024} }, /* bp0 => don't care */ |
| 1351 | { 0x1d, {0x000000, 32 * 1024} }, /* bp0 => don't care */ |
| 1352 | { 0x1e, {0x000000, 32 * 1024} }, |
| 1353 | }; |
| 1354 | |
| 1355 | struct generic_range gd25q32_cmp1_ranges[] = { |
| 1356 | /* none, bp4 and bp3 => don't care */ |
| 1357 | { 0x00, {0, 0} }, |
| 1358 | { 0x08, {0, 0} }, |
| 1359 | { 0x10, {0, 0} }, |
| 1360 | { 0x18, {0, 0} }, |
| 1361 | |
| 1362 | { 0x01, {0x000000, 4032 * 1024} }, |
| 1363 | { 0x02, {0x000000, 3968 * 1024} }, |
| 1364 | { 0x03, {0x000000, 3840 * 1024} }, |
| 1365 | { 0x04, {0x000000, 3584 * 1024} }, |
| 1366 | { 0x05, {0x000000, 3 * 1024 * 1024} }, |
| 1367 | { 0x06, {0x000000, 2 * 1024 * 1024} }, |
| 1368 | |
| 1369 | { 0x09, {0x010000, 4032 * 1024} }, |
| 1370 | { 0x0a, {0x020000, 3968 * 1024} }, |
| 1371 | { 0x0b, {0x040000, 3840 * 1024} }, |
| 1372 | { 0x0c, {0x080000, 3584 * 1024} }, |
| 1373 | { 0x0d, {0x100000, 3 * 1024 * 1024} }, |
| 1374 | { 0x0e, {0x200000, 2 * 1024 * 1024} }, |
| 1375 | |
| 1376 | /* all, bp4 and bp3 => don't care */ |
| 1377 | { 0x07, {0x000000, 4096 * 1024} }, |
| 1378 | { 0x0f, {0x000000, 4096 * 1024} }, |
| 1379 | { 0x17, {0x000000, 4096 * 1024} }, |
| 1380 | { 0x1f, {0x000000, 4096 * 1024} }, |
| 1381 | |
| 1382 | { 0x11, {0x000000, 4092 * 1024} }, |
| 1383 | { 0x12, {0x000000, 4088 * 1024} }, |
| 1384 | { 0x13, {0x000000, 4080 * 1024} }, |
| 1385 | { 0x14, {0x000000, 4064 * 1024} }, /* bp0 => don't care */ |
| 1386 | { 0x15, {0x000000, 4064 * 1024} }, /* bp0 => don't care */ |
| 1387 | { 0x16, {0x000000, 4064 * 1024} }, |
| 1388 | |
| 1389 | { 0x19, {0x001000, 4092 * 1024} }, |
| 1390 | { 0x1a, {0x002000, 4088 * 1024} }, |
| 1391 | { 0x1b, {0x040000, 4080 * 1024} }, |
| 1392 | { 0x1c, {0x080000, 4064 * 1024} }, /* bp0 => don't care */ |
| 1393 | { 0x1d, {0x080000, 4064 * 1024} }, /* bp0 => don't care */ |
| 1394 | { 0x1e, {0x080000, 4064 * 1024} }, |
| 1395 | }; |
| 1396 | |
| 1397 | static struct generic_wp gd25q32_wp = { |
| 1398 | /* TODO: map second status register */ |
| 1399 | .sr1 = { .bp0_pos = 2, .bp_bits = 5, .srp_pos = 7 }, |
| 1400 | }; |
| 1401 | |
David Hendricks | 83541d3 | 2014-07-15 20:58:21 -0700 | [diff] [blame] | 1402 | #if 0 |
| 1403 | /* FIXME: MX25L6405D has same ID as MX25L6406 */ |
| 1404 | static struct w25q_range mx25l6405d_ranges[] = { |
| 1405 | { X, 0, 0, {0, 0} }, /* none */ |
| 1406 | { X, 0, 0x1, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */ |
| 1407 | { X, 0, 0x2, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */ |
| 1408 | { X, 0, 0x3, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */ |
| 1409 | { X, 0, 0x4, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */ |
| 1410 | { X, 0, 0x5, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */ |
| 1411 | { X, 0, 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */ |
| 1412 | { X, 0, 0x7, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */ |
| 1413 | |
| 1414 | { X, 1, 0x0, {0x000000, 8192 * 1024} }, |
| 1415 | { X, 1, 0x1, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */ |
| 1416 | { X, 1, 0x2, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */ |
| 1417 | { X, 1, 0x3, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */ |
| 1418 | { X, 1, 0x4, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */ |
| 1419 | { X, 1, 0x5, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */ |
| 1420 | { X, 1, 0x6, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */ |
| 1421 | { X, 1, 0x7, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */ |
| 1422 | }; |
| 1423 | #endif |
| 1424 | |
| 1425 | /* FIXME: MX25L6406 has same ID as MX25L6405D */ |
| 1426 | struct generic_range mx25l6406e_ranges[] = { |
| 1427 | { 0, {0, 0} }, /* none */ |
| 1428 | { 0x1, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */ |
| 1429 | { 0x2, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */ |
| 1430 | { 0x3, {0x7a0000, 64 * 8 * 1024} }, /* blocks 120-127 */ |
| 1431 | { 0x4, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */ |
| 1432 | { 0x5, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */ |
| 1433 | { 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */ |
| 1434 | |
| 1435 | { 0x7, {0x000000, 64 * 128 * 1024} }, /* all */ |
| 1436 | { 0x8, {0x000000, 64 * 128 * 1024} }, /* all */ |
| 1437 | { 0x9, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */ |
| 1438 | { 0xa, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */ |
| 1439 | { 0xb, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */ |
| 1440 | { 0xc, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */ |
| 1441 | { 0xd, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */ |
| 1442 | { 0xe, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */ |
| 1443 | { 0xf, {0x000000, 64 * 128 * 1024} }, /* all */ |
| 1444 | }; |
| 1445 | |
| 1446 | static struct generic_wp mx25l6406e_wp = { |
| 1447 | .sr1 = { .bp0_pos = 2, .bp_bits = 4, .srp_pos = 7 }, |
| 1448 | .ranges = &mx25l6406e_ranges[0], |
| 1449 | }; |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 1450 | |
| 1451 | /* Given a flash chip, this function returns its writeprotect info. */ |
| 1452 | static int generic_range_table(const struct flashchip *flash, |
| 1453 | struct generic_wp **wp, |
| 1454 | int *num_entries) |
| 1455 | { |
| 1456 | *wp = NULL; |
| 1457 | *num_entries = 0; |
| 1458 | |
| 1459 | switch (flash->manufacture_id) { |
David Hendricks | af3944a | 2014-07-28 18:37:40 -0700 | [diff] [blame] | 1460 | case GIGADEVICE_ID: |
| 1461 | switch(flash->model_id) { |
| 1462 | case GIGADEVICE_GD25Q32: { |
| 1463 | uint8_t sr1 = w25q_read_status_register_2(); |
| 1464 | |
| 1465 | *wp = &gd25q32_wp; |
| 1466 | if (!(sr1 & (1 << 6))) { /* CMP == 0 */ |
| 1467 | (*wp)->ranges = &gd25q32_cmp0_ranges[0]; |
| 1468 | *num_entries = ARRAY_SIZE(gd25q32_cmp0_ranges); |
| 1469 | } else { /* CMP == 1 */ |
| 1470 | (*wp)->ranges = &gd25q32_cmp1_ranges[0]; |
| 1471 | *num_entries = ARRAY_SIZE(gd25q32_cmp1_ranges); |
| 1472 | } |
| 1473 | |
| 1474 | break; |
| 1475 | /* TODO(shawnn): add support for other GD parts */ |
| 1476 | } |
| 1477 | default: |
| 1478 | msg_cerr("%s() %d: GigaDevice flash chip mismatch" |
| 1479 | " (0x%04x), aborting\n", __func__, __LINE__, |
| 1480 | flash->model_id); |
| 1481 | return -1; |
| 1482 | } |
| 1483 | break; |
David Hendricks | 83541d3 | 2014-07-15 20:58:21 -0700 | [diff] [blame] | 1484 | case MACRONIX_ID: |
| 1485 | switch (flash->model_id) { |
| 1486 | case MACRONIX_MX25L6405: |
| 1487 | /* FIXME: MX25L64* chips have mixed capabilities and |
| 1488 | share IDs */ |
| 1489 | *wp = &mx25l6406e_wp; |
| 1490 | *num_entries = ARRAY_SIZE(mx25l6406e_ranges); |
| 1491 | break; |
| 1492 | default: |
| 1493 | msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)" |
| 1494 | ", aborting\n", __func__, __LINE__, |
| 1495 | flash->model_id); |
| 1496 | return -1; |
| 1497 | } |
| 1498 | break; |
David Hendricks | e0512a7 | 2014-07-15 20:30:47 -0700 | [diff] [blame] | 1499 | default: |
| 1500 | msg_cerr("%s: flash vendor (0x%x) not found, aborting\n", |
| 1501 | __func__, flash->manufacture_id); |
| 1502 | return -1; |
| 1503 | } |
| 1504 | |
| 1505 | return 0; |
| 1506 | } |
| 1507 | |
| 1508 | /* Given a [start, len], this function finds a block protect bit combination |
| 1509 | * (if possible) and sets the corresponding bits in "status". Remaining bits |
| 1510 | * are preserved. */ |
| 1511 | static int generic_range_to_status(const struct flashchip *flash, |
| 1512 | unsigned int start, unsigned int len, |
| 1513 | uint8_t *status) |
| 1514 | { |
| 1515 | struct generic_wp *wp; |
| 1516 | struct generic_range *r; |
| 1517 | int i, range_found = 0, num_entries; |
| 1518 | uint8_t bp_mask; |
| 1519 | |
| 1520 | if (generic_range_table(flash, &wp, &num_entries)) |
| 1521 | return -1; |
| 1522 | |
| 1523 | bp_mask = ((1 << (wp->sr1.bp0_pos + wp->sr1.bp_bits)) - 1) - \ |
| 1524 | ((1 << wp->sr1.bp0_pos) - 1); |
| 1525 | |
| 1526 | for (i = 0, r = &wp->ranges[0]; i < num_entries; i++, r++) { |
| 1527 | msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n", |
| 1528 | start, len, r->range.start, r->range.len); |
| 1529 | if ((start == r->range.start) && (len == r->range.len)) { |
| 1530 | *status &= ~(bp_mask); |
| 1531 | *status |= r->bp << (wp->sr1.bp0_pos); |
| 1532 | range_found = 1; |
| 1533 | break; |
| 1534 | } |
| 1535 | } |
| 1536 | |
| 1537 | if (!range_found) { |
| 1538 | msg_cerr("matching range not found\n"); |
| 1539 | return -1; |
| 1540 | } |
| 1541 | return 0; |
| 1542 | } |
| 1543 | |
| 1544 | static int generic_status_to_range(const struct flashchip *flash, |
| 1545 | const uint8_t sr1, unsigned int *start, unsigned int *len) |
| 1546 | { |
| 1547 | struct generic_wp *wp; |
| 1548 | struct generic_range *r; |
| 1549 | int num_entries, wp_en, i, status_found = 0; |
| 1550 | uint8_t sr1_bp; |
| 1551 | |
| 1552 | if (generic_range_table(flash, &wp, &num_entries)) |
| 1553 | return -1; |
| 1554 | |
| 1555 | sr1_bp = (sr1 >> wp->sr1.bp0_pos) & ((1 << wp->sr1.bp_bits) - 1); |
| 1556 | |
| 1557 | for (i = 0, r = &wp->ranges[0]; i < num_entries; i++, r++) { |
| 1558 | msg_cspew("comparing 0x%02x 0x%02x\n", sr1_bp, r->bp); |
| 1559 | if (sr1_bp == r->bp) { |
| 1560 | *start = r->range.start; |
| 1561 | *len = r->range.len; |
| 1562 | status_found = 1; |
| 1563 | break; |
| 1564 | } |
| 1565 | } |
| 1566 | |
| 1567 | if (!status_found) { |
| 1568 | msg_cerr("matching status not found\n"); |
| 1569 | return -1; |
| 1570 | } |
| 1571 | return 0; |
| 1572 | } |
| 1573 | |
| 1574 | /* Given a [start, len], this function calls generic_range_to_status() to |
| 1575 | * convert it to flash-chip-specific range bits, then sets into status register. |
| 1576 | */ |
| 1577 | static int generic_set_range(const struct flashchip *flash, |
| 1578 | unsigned int start, unsigned int len) |
| 1579 | { |
| 1580 | uint8_t status, expected; |
| 1581 | |
| 1582 | status = spi_read_status_register(); |
| 1583 | msg_cdbg("%s: old status: 0x%02x\n", __func__, status); |
| 1584 | |
| 1585 | expected = status; /* preserve non-bp bits */ |
| 1586 | if (generic_range_to_status(flash, start, len, &expected)) |
| 1587 | return -1; |
| 1588 | |
| 1589 | spi_write_status_register_WREN(expected); |
| 1590 | |
| 1591 | status = spi_read_status_register(); |
| 1592 | msg_cdbg("%s: new status: 0x%02x\n", __func__, status); |
| 1593 | if (status != expected) { |
| 1594 | msg_cerr("expected=0x%02x, but actual=0x%02x.\n", |
| 1595 | expected, status); |
| 1596 | return 1; |
| 1597 | } |
| 1598 | |
| 1599 | return 0; |
| 1600 | } |
| 1601 | |
| 1602 | /* Set/clear the status regsiter write protect bit in SR1. */ |
| 1603 | static int generic_set_srp0(const struct flashchip *flash, int enable) |
| 1604 | { |
| 1605 | uint8_t status, expected; |
| 1606 | struct generic_wp *wp; |
| 1607 | int num_entries; |
| 1608 | |
| 1609 | if (generic_range_table(flash, &wp, &num_entries)) |
| 1610 | return -1; |
| 1611 | |
| 1612 | expected = spi_read_status_register(); |
| 1613 | msg_cdbg("%s: old status: 0x%02x\n", __func__, expected); |
| 1614 | |
| 1615 | if (enable) |
| 1616 | expected |= 1 << wp->sr1.srp_pos; |
| 1617 | else |
| 1618 | expected &= ~(1 << wp->sr1.srp_pos); |
| 1619 | |
| 1620 | spi_write_status_register_WREN(expected); |
| 1621 | |
| 1622 | status = spi_read_status_register(); |
| 1623 | msg_cdbg("%s: new status: 0x%02x\n", __func__, status); |
| 1624 | if (status != expected) |
| 1625 | return -1; |
| 1626 | |
| 1627 | return 0; |
| 1628 | } |
| 1629 | |
| 1630 | static int generic_enable_writeprotect(const struct flashchip *flash, |
| 1631 | enum wp_mode wp_mode) |
| 1632 | { |
| 1633 | int ret; |
| 1634 | |
| 1635 | switch (wp_mode) { |
| 1636 | case WP_MODE_HARDWARE: |
| 1637 | ret = generic_set_srp0(flash, 1); |
| 1638 | break; |
| 1639 | default: |
| 1640 | msg_cerr("%s(): unsupported write-protect mode\n", __func__); |
| 1641 | return 1; |
| 1642 | } |
| 1643 | |
| 1644 | if (ret) |
| 1645 | msg_cerr("%s(): error=%d.\n", __func__, ret); |
| 1646 | return ret; |
| 1647 | } |
| 1648 | |
| 1649 | static int generic_disable_writeprotect(const struct flashchip *flash) |
| 1650 | { |
| 1651 | int ret; |
| 1652 | |
| 1653 | ret = generic_set_srp0(flash, 0); |
| 1654 | if (ret) |
| 1655 | msg_cerr("%s(): error=%d.\n", __func__, ret); |
| 1656 | return ret; |
| 1657 | } |
| 1658 | |
| 1659 | static int generic_list_ranges(const struct flashchip *flash) |
| 1660 | { |
| 1661 | struct generic_wp *wp; |
| 1662 | struct generic_range *r; |
| 1663 | int i, num_entries; |
| 1664 | |
| 1665 | if (generic_range_table(flash, &wp, &num_entries)) |
| 1666 | return -1; |
| 1667 | |
| 1668 | r = &wp->ranges[0]; |
| 1669 | for (i = 0; i < num_entries; i++) { |
| 1670 | msg_cinfo("start: 0x%06x, length: 0x%06x\n", |
| 1671 | r->range.start, r->range.len); |
| 1672 | r++; |
| 1673 | } |
| 1674 | |
| 1675 | return 0; |
| 1676 | } |
| 1677 | |
| 1678 | static int generic_wp_status(const struct flashchip *flash) |
| 1679 | { |
| 1680 | uint8_t sr1; |
| 1681 | unsigned int start, len; |
| 1682 | int ret = 0; |
| 1683 | struct generic_wp *wp; |
| 1684 | struct generic_range *g; |
| 1685 | int num_entries, wp_en; |
| 1686 | |
| 1687 | if (generic_range_table(flash, &wp, &num_entries)) |
| 1688 | return -1; |
| 1689 | |
| 1690 | sr1 = spi_read_status_register(); |
| 1691 | wp_en = (sr1 >> wp->sr1.srp_pos) & 1; |
| 1692 | |
| 1693 | msg_cinfo("WP: status: 0x%04x\n", sr1); |
| 1694 | msg_cinfo("WP: status.srp0: %x\n", wp_en); |
| 1695 | /* FIXME: SRP1 is not really generic, but we probably should print |
| 1696 | * it anyway to have consistent output. #legacycruft */ |
| 1697 | msg_cinfo("WP: status.srp1: %x\n", 0); |
| 1698 | msg_cinfo("WP: write protect is %s.\n", |
| 1699 | wp_en ? "enabled" : "disabled"); |
| 1700 | |
| 1701 | msg_cinfo("WP: write protect range: "); |
| 1702 | if (generic_status_to_range(flash, sr1, &start, &len)) { |
| 1703 | msg_cinfo("(cannot resolve the range)\n"); |
| 1704 | ret = -1; |
| 1705 | } else { |
| 1706 | msg_cinfo("start=0x%08x, len=0x%08x\n", start, len); |
| 1707 | } |
| 1708 | |
| 1709 | return ret; |
| 1710 | } |
| 1711 | |
| 1712 | struct wp wp_generic = { |
| 1713 | .list_ranges = generic_list_ranges, |
| 1714 | .set_range = generic_set_range, |
| 1715 | .enable = generic_enable_writeprotect, |
| 1716 | .disable = generic_disable_writeprotect, |
| 1717 | .wp_status = generic_wp_status, |
| 1718 | }; |