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