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 | |
| 30 | /* |
| 31 | * The following procedures rely on look-up tables to match the user-specified |
| 32 | * range with the chip's supported ranges. This turned out to be the most |
| 33 | * elegant approach since diferent flash chips use different levels of |
| 34 | * granularity and methods to determine protected ranges. In other words, |
| 35 | * be stupid and simple since clever arithmetic will not for many chips. |
| 36 | */ |
| 37 | |
| 38 | struct wp_range { |
| 39 | unsigned int start; /* starting address */ |
| 40 | unsigned int len; /* len */ |
| 41 | }; |
| 42 | |
| 43 | enum bit_state { |
| 44 | OFF = 0, |
| 45 | ON = 1, |
| 46 | X = 0 /* don't care */ |
| 47 | }; |
| 48 | |
| 49 | struct w25q_range { |
| 50 | enum bit_state sec; /* if 1, bp[2:0] describe sectors */ |
| 51 | enum bit_state tb; /* top/bottom select */ |
| 52 | unsigned short int bp : 3; /* block protect bitfield */ |
| 53 | struct wp_range range; |
| 54 | }; |
| 55 | |
David Hendricks | 57566ed | 2010-08-16 18:24:45 -0700 | [diff] [blame] | 56 | struct w25q_range en25f40_ranges[] = { |
| 57 | { X, X, 0, {0, 0} }, /* none */ |
| 58 | { 0, 0, 0x1, {0x000000, 504 * 1024} }, |
| 59 | { 0, 0, 0x2, {0x000000, 496 * 1024} }, |
| 60 | { 0, 0, 0x3, {0x000000, 480 * 1024} }, |
| 61 | { 0, 0, 0x4, {0x000000, 448 * 1024} }, |
| 62 | { 0, 0, 0x5, {0x000000, 384 * 1024} }, |
| 63 | { 0, 0, 0x6, {0x000000, 256 * 1024} }, |
| 64 | { 0, 0, 0x7, {0x000000, 512 * 1024} }, |
| 65 | }; |
| 66 | |
David Hendricks | ac72e36 | 2010-08-16 18:20:03 -0700 | [diff] [blame] | 67 | static struct w25q_range mx25l3205d_ranges[] = { |
| 68 | { X, 0, 0, {0, 0} }, /* none */ |
| 69 | { X, 0, 0x1, {0x3f0000, 64 * 1024} }, |
| 70 | { X, 0, 0x2, {0x3e0000, 128 * 1024} }, |
| 71 | { X, 0, 0x3, {0x3c0000, 256 * 1024} }, |
| 72 | { X, 0, 0x4, {0x380000, 512 * 1024} }, |
| 73 | { X, 0, 0x5, {0x300000, 1024 * 1024} }, |
| 74 | { X, 0, 0x6, {0x200000, 2048 * 1024} }, |
| 75 | { X, 0, 0x7, {0x000000, 4096 * 1024} }, |
| 76 | |
| 77 | { X, 1, 0x0, {0x000000, 4096 * 1024} }, |
| 78 | { X, 1, 0x1, {0x000000, 2048 * 1024} }, |
| 79 | { X, 1, 0x2, {0x000000, 3072 * 1024} }, |
| 80 | { X, 1, 0x3, {0x000000, 3584 * 1024} }, |
| 81 | { X, 1, 0x4, {0x000000, 3840 * 1024} }, |
| 82 | { X, 1, 0x5, {0x000000, 3968 * 1024} }, |
| 83 | { X, 1, 0x6, {0x000000, 4032 * 1024} }, |
| 84 | { X, 1, 0x7, {0x000000, 4096 * 1024} }, |
| 85 | }; |
| 86 | |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 87 | static struct w25q_range w25q16_ranges[] = { |
| 88 | { X, X, 0, {0, 0} }, /* none */ |
| 89 | { 0, 0, 0x1, {0x1f0000, 64 * 1024} }, |
| 90 | { 0, 0, 0x2, {0x1e0000, 128 * 1024} }, |
| 91 | { 0, 0, 0x3, {0x1c0000, 256 * 1024} }, |
| 92 | { 0, 0, 0x4, {0x180000, 512 * 1024} }, |
| 93 | { 0, 0, 0x5, {0x100000, 1024 * 1024} }, |
| 94 | |
| 95 | { 0, 1, 0x1, {0x000000, 64 * 1024} }, |
| 96 | { 0, 1, 0x2, {0x000000, 128 * 1024} }, |
| 97 | { 0, 1, 0x3, {0x000000, 256 * 1024} }, |
| 98 | { 0, 1, 0x4, {0x000000, 512 * 1024} }, |
| 99 | { 0, 1, 0x5, {0x000000, 1024 * 1024} }, |
| 100 | { X, X, 0x6, {0x000000, 2048 * 1024} }, |
| 101 | { X, X, 0x7, {0x000000, 2048 * 1024} }, |
| 102 | |
| 103 | { 1, 0, 0x1, {0x1ff000, 4 * 1024} }, |
| 104 | { 1, 0, 0x2, {0x1fe000, 8 * 1024} }, |
| 105 | { 1, 0, 0x3, {0x1fc000, 16 * 1024} }, |
| 106 | { 1, 0, 0x4, {0x1f8000, 32 * 1024} }, |
| 107 | { 1, 0, 0x5, {0x1f8000, 32 * 1024} }, |
| 108 | |
| 109 | { 1, 1, 0x1, {0x000000, 4 * 1024} }, |
| 110 | { 1, 1, 0x2, {0x000000, 8 * 1024} }, |
| 111 | { 1, 1, 0x3, {0x000000, 16 * 1024} }, |
| 112 | { 1, 1, 0x4, {0x000000, 32 * 1024} }, |
| 113 | { 1, 1, 0x5, {0x000000, 32 * 1024} }, |
| 114 | }; |
| 115 | |
| 116 | static struct w25q_range w25q32_ranges[] = { |
| 117 | { X, X, 0, {0, 0} }, /* none */ |
| 118 | { 0, 0, 0x1, {0x3f0000, 64 * 1024} }, |
| 119 | { 0, 0, 0x2, {0x3e0000, 128 * 1024} }, |
| 120 | { 0, 0, 0x3, {0x3c0000, 256 * 1024} }, |
| 121 | { 0, 0, 0x4, {0x380000, 512 * 1024} }, |
| 122 | { 0, 0, 0x5, {0x300000, 1024 * 1024} }, |
David Hendricks | 05653ff | 2010-06-15 16:05:12 -0700 | [diff] [blame] | 123 | { 0, 0, 0x6, {0x200000, 2048 * 1024} }, |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 124 | |
| 125 | { 0, 1, 0x1, {0x000000, 64 * 1024} }, |
| 126 | { 0, 1, 0x2, {0x000000, 128 * 1024} }, |
| 127 | { 0, 1, 0x3, {0x000000, 256 * 1024} }, |
| 128 | { 0, 1, 0x4, {0x000000, 512 * 1024} }, |
| 129 | { 0, 1, 0x5, {0x000000, 1024 * 1024} }, |
| 130 | { 0, 1, 0x6, {0x000000, 2048 * 1024} }, |
| 131 | { X, X, 0x7, {0x000000, 4096 * 1024} }, |
| 132 | |
| 133 | { 1, 0, 0x1, {0x3ff000, 4 * 1024} }, |
| 134 | { 1, 0, 0x2, {0x3fe000, 8 * 1024} }, |
| 135 | { 1, 0, 0x3, {0x3fc000, 16 * 1024} }, |
| 136 | { 1, 0, 0x4, {0x3f8000, 32 * 1024} }, |
| 137 | { 1, 0, 0x5, {0x3f8000, 32 * 1024} }, |
| 138 | |
| 139 | { 1, 1, 0x1, {0x000000, 4 * 1024} }, |
| 140 | { 1, 1, 0x2, {0x000000, 8 * 1024} }, |
| 141 | { 1, 1, 0x3, {0x000000, 16 * 1024} }, |
| 142 | { 1, 1, 0x4, {0x000000, 32 * 1024} }, |
| 143 | { 1, 1, 0x5, {0x000000, 32 * 1024} }, |
| 144 | }; |
| 145 | |
| 146 | static struct w25q_range w25q80_ranges[] = { |
| 147 | { X, X, 0, {0, 0} }, /* none */ |
| 148 | { 0, 0, 0x1, {0x0f0000, 64 * 1024} }, |
| 149 | { 0, 0, 0x2, {0x0e0000, 128 * 1024} }, |
| 150 | { 0, 0, 0x3, {0x0c0000, 256 * 1024} }, |
| 151 | { 0, 0, 0x4, {0x080000, 512 * 1024} }, |
| 152 | |
| 153 | { 0, 1, 0x1, {0x000000, 64 * 1024} }, |
| 154 | { 0, 1, 0x2, {0x000000, 128 * 1024} }, |
| 155 | { 0, 1, 0x3, {0x000000, 256 * 1024} }, |
| 156 | { 0, 1, 0x4, {0x000000, 512 * 1024} }, |
David Hendricks | 05653ff | 2010-06-15 16:05:12 -0700 | [diff] [blame] | 157 | { X, X, 0x6, {0x000000, 1024 * 1024} }, |
| 158 | { X, X, 0x7, {0x000000, 1024 * 1024} }, |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 159 | |
| 160 | { 1, 0, 0x1, {0x1ff000, 4 * 1024} }, |
| 161 | { 1, 0, 0x2, {0x1fe000, 8 * 1024} }, |
| 162 | { 1, 0, 0x3, {0x1fc000, 16 * 1024} }, |
| 163 | { 1, 0, 0x4, {0x1f8000, 32 * 1024} }, |
| 164 | { 1, 0, 0x5, {0x1f8000, 32 * 1024} }, |
| 165 | |
| 166 | { 1, 1, 0x1, {0x000000, 4 * 1024} }, |
| 167 | { 1, 1, 0x2, {0x000000, 8 * 1024} }, |
| 168 | { 1, 1, 0x3, {0x000000, 16 * 1024} }, |
| 169 | { 1, 1, 0x4, {0x000000, 32 * 1024} }, |
| 170 | { 1, 1, 0x5, {0x000000, 32 * 1024} }, |
| 171 | }; |
| 172 | |
David Hendricks | 2c4a76c | 2010-06-28 14:00:43 -0700 | [diff] [blame] | 173 | static struct w25q_range w25q64_ranges[] = { |
| 174 | { X, X, 0, {0, 0} }, /* none */ |
| 175 | |
| 176 | { 0, 0, 0x1, {0x7e0000, 128 * 1024} }, |
| 177 | { 0, 0, 0x2, {0x7c0000, 256 * 1024} }, |
| 178 | { 0, 0, 0x3, {0x780000, 512 * 1024} }, |
| 179 | { 0, 0, 0x4, {0x700000, 1024 * 1024} }, |
| 180 | { 0, 0, 0x5, {0x600000, 2048 * 1024} }, |
| 181 | { 0, 0, 0x6, {0x400000, 4096 * 1024} }, |
| 182 | |
| 183 | { 0, 1, 0x1, {0x000000, 128 * 1024} }, |
| 184 | { 0, 1, 0x2, {0x000000, 256 * 1024} }, |
| 185 | { 0, 1, 0x3, {0x000000, 512 * 1024} }, |
| 186 | { 0, 1, 0x4, {0x000000, 1024 * 1024} }, |
| 187 | { 0, 1, 0x5, {0x000000, 2048 * 1024} }, |
| 188 | { 0, 1, 0x6, {0x000000, 4096 * 1024} }, |
| 189 | { X, X, 0x7, {0x000000, 8192 * 1024} }, |
| 190 | |
| 191 | { 1, 0, 0x1, {0x7ff000, 4 * 1024} }, |
| 192 | { 1, 0, 0x2, {0x7fe000, 8 * 1024} }, |
| 193 | { 1, 0, 0x3, {0x7fc000, 16 * 1024} }, |
| 194 | { 1, 0, 0x4, {0x7f8000, 32 * 1024} }, |
| 195 | { 1, 0, 0x5, {0x7f8000, 32 * 1024} }, |
| 196 | |
| 197 | { 1, 1, 0x1, {0x000000, 4 * 1024} }, |
| 198 | { 1, 1, 0x2, {0x000000, 8 * 1024} }, |
| 199 | { 1, 1, 0x3, {0x000000, 16 * 1024} }, |
| 200 | { 1, 1, 0x4, {0x000000, 32 * 1024} }, |
| 201 | { 1, 1, 0x5, {0x000000, 32 * 1024} }, |
| 202 | }; |
| 203 | |
Louis Yung-Chieh Lo | 232951f | 2010-09-16 11:30:00 +0800 | [diff] [blame] | 204 | struct w25q_range w25x10_ranges[] = { |
| 205 | { X, X, 0, {0, 0} }, /* none */ |
| 206 | { 0, 0, 0x1, {0x010000, 64 * 1024} }, |
| 207 | { 0, 1, 0x1, {0x000000, 64 * 1024} }, |
| 208 | { X, X, 0x2, {0x000000, 128 * 1024} }, |
| 209 | { X, X, 0x3, {0x000000, 128 * 1024} }, |
| 210 | }; |
| 211 | |
| 212 | struct w25q_range w25x20_ranges[] = { |
| 213 | { X, X, 0, {0, 0} }, /* none */ |
| 214 | { 0, 0, 0x1, {0x030000, 64 * 1024} }, |
| 215 | { 0, 0, 0x2, {0x020000, 128 * 1024} }, |
| 216 | { 0, 1, 0x1, {0x000000, 64 * 1024} }, |
| 217 | { 0, 1, 0x2, {0x000000, 128 * 1024} }, |
| 218 | { 0, X, 0x3, {0x000000, 256 * 1024} }, |
| 219 | }; |
| 220 | |
David Hendricks | 470ca95 | 2010-08-13 14:01:53 -0700 | [diff] [blame] | 221 | struct w25q_range w25x40_ranges[] = { |
| 222 | { X, X, 0, {0, 0} }, /* none */ |
| 223 | { 0, 0, 0x1, {0x070000, 64 * 1024} }, |
| 224 | { 0, 0, 0x2, {0x060000, 128 * 1024} }, |
| 225 | { 0, 0, 0x3, {0x040000, 256 * 1024} }, |
| 226 | { 0, 1, 0x1, {0x000000, 64 * 1024} }, |
| 227 | { 0, 1, 0x2, {0x000000, 128 * 1024} }, |
| 228 | { 0, 1, 0x3, {0x000000, 256 * 1024} }, |
| 229 | { 0, X, 0x4, {0x000000, 512 * 1024} }, |
| 230 | }; |
| 231 | |
Louis Yung-Chieh Lo | 232951f | 2010-09-16 11:30:00 +0800 | [diff] [blame] | 232 | struct w25q_range w25x80_ranges[] = { |
| 233 | { X, X, 0, {0, 0} }, /* none */ |
| 234 | { 0, 0, 0x1, {0x0F0000, 64 * 1024} }, |
| 235 | { 0, 0, 0x2, {0x0E0000, 128 * 1024} }, |
| 236 | { 0, 0, 0x3, {0x0C0000, 256 * 1024} }, |
| 237 | { 0, 0, 0x4, {0x080000, 512 * 1024} }, |
| 238 | { 0, 1, 0x1, {0x000000, 64 * 1024} }, |
| 239 | { 0, 1, 0x2, {0x000000, 128 * 1024} }, |
| 240 | { 0, 1, 0x3, {0x000000, 256 * 1024} }, |
| 241 | { 0, 1, 0x4, {0x000000, 512 * 1024} }, |
| 242 | { 0, X, 0x5, {0x000000, 1024 * 1024} }, |
| 243 | { 0, X, 0x6, {0x000000, 1024 * 1024} }, |
| 244 | { 0, X, 0x7, {0x000000, 1024 * 1024} }, |
| 245 | }; |
| 246 | |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 247 | /* Given a flash chip, this function returns its range table. */ |
| 248 | static int w25_range_table(const struct flashchip *flash, |
| 249 | struct w25q_range **w25q_ranges, |
| 250 | int *num_entries) |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 251 | { |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 252 | *w25q_ranges = 0; |
| 253 | *num_entries = 0; |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 254 | |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 255 | switch (flash->manufacture_id) { |
| 256 | case WINBOND_NEX_ID: |
| 257 | switch(flash->model_id) { |
Louis Yung-Chieh Lo | 232951f | 2010-09-16 11:30:00 +0800 | [diff] [blame] | 258 | case W_25X10: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 259 | *w25q_ranges = w25x10_ranges; |
| 260 | *num_entries = ARRAY_SIZE(w25x10_ranges); |
Louis Yung-Chieh Lo | 232951f | 2010-09-16 11:30:00 +0800 | [diff] [blame] | 261 | break; |
| 262 | case W_25X20: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 263 | *w25q_ranges = w25x20_ranges; |
| 264 | *num_entries = ARRAY_SIZE(w25x20_ranges); |
Louis Yung-Chieh Lo | 232951f | 2010-09-16 11:30:00 +0800 | [diff] [blame] | 265 | break; |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 266 | case W_25X40: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 267 | *w25q_ranges = w25x40_ranges; |
| 268 | *num_entries = ARRAY_SIZE(w25x40_ranges); |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 269 | break; |
Louis Yung-Chieh Lo | 232951f | 2010-09-16 11:30:00 +0800 | [diff] [blame] | 270 | case W_25X80: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 271 | *w25q_ranges = w25x80_ranges; |
| 272 | *num_entries = ARRAY_SIZE(w25x80_ranges); |
Louis Yung-Chieh Lo | 232951f | 2010-09-16 11:30:00 +0800 | [diff] [blame] | 273 | break; |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 274 | case W_25Q80: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 275 | *w25q_ranges = w25q80_ranges; |
| 276 | *num_entries = ARRAY_SIZE(w25q80_ranges); |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 277 | break; |
| 278 | case W_25Q16: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 279 | *w25q_ranges = w25q16_ranges; |
| 280 | *num_entries = ARRAY_SIZE(w25q16_ranges); |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 281 | break; |
| 282 | case W_25Q32: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 283 | *w25q_ranges = w25q32_ranges; |
| 284 | *num_entries = ARRAY_SIZE(w25q32_ranges); |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 285 | break; |
| 286 | case W_25Q64: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 287 | *w25q_ranges = w25q64_ranges; |
| 288 | *num_entries = ARRAY_SIZE(w25q64_ranges); |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 289 | break; |
| 290 | default: |
| 291 | msg_cerr("%s() %d: WINBOND flash chip mismatch (0x%04x)" |
| 292 | ", aborting\n", __func__, __LINE__, |
| 293 | flash->model_id); |
| 294 | return -1; |
| 295 | } |
David Hendricks | 2c4a76c | 2010-06-28 14:00:43 -0700 | [diff] [blame] | 296 | break; |
David Hendricks | 57566ed | 2010-08-16 18:24:45 -0700 | [diff] [blame] | 297 | case EON_ID_NOPREFIX: |
| 298 | switch (flash->model_id) { |
| 299 | case EN_25F40: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 300 | *w25q_ranges = en25f40_ranges; |
| 301 | *num_entries = ARRAY_SIZE(en25f40_ranges); |
David Hendricks | 57566ed | 2010-08-16 18:24:45 -0700 | [diff] [blame] | 302 | break; |
| 303 | default: |
| 304 | msg_cerr("%s():%d: EON flash chip mismatch (0x%04x)" |
| 305 | ", aborting\n", __func__, __LINE__, |
| 306 | flash->model_id); |
| 307 | return -1; |
| 308 | } |
| 309 | break; |
David Hendricks | ac72e36 | 2010-08-16 18:20:03 -0700 | [diff] [blame] | 310 | case MX_ID: |
| 311 | switch (flash->model_id) { |
| 312 | case MX_25L3205: |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 313 | *w25q_ranges = mx25l3205d_ranges; |
| 314 | *num_entries = ARRAY_SIZE(mx25l3205d_ranges); |
David Hendricks | ac72e36 | 2010-08-16 18:20:03 -0700 | [diff] [blame] | 315 | break; |
| 316 | default: |
| 317 | msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)" |
| 318 | ", aborting\n", __func__, __LINE__, |
| 319 | flash->model_id); |
| 320 | return -1; |
| 321 | } |
| 322 | break; |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 323 | default: |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 324 | msg_cerr("%s: flash vendor (0x%x) not found, aborting\n", |
| 325 | __func__, flash->manufacture_id); |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 326 | return -1; |
| 327 | } |
| 328 | |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | int w25_range_to_status(const struct flashchip *flash, |
| 333 | unsigned int start, unsigned int len, |
| 334 | struct w25q_status *status) |
| 335 | { |
| 336 | struct w25q_range *w25q_ranges; |
| 337 | int i, range_found = 0; |
| 338 | int num_entries; |
| 339 | |
| 340 | if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1; |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 341 | for (i = 0; i < num_entries; i++) { |
| 342 | struct wp_range *r = &w25q_ranges[i].range; |
| 343 | |
| 344 | msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n", |
| 345 | start, len, r->start, r->len); |
| 346 | if ((start == r->start) && (len == r->len)) { |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 347 | status->bp0 = w25q_ranges[i].bp & 1; |
| 348 | status->bp1 = w25q_ranges[i].bp >> 1; |
| 349 | status->bp2 = w25q_ranges[i].bp >> 2; |
| 350 | status->tb = w25q_ranges[i].tb; |
| 351 | status->sec = w25q_ranges[i].sec; |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 352 | |
| 353 | range_found = 1; |
| 354 | break; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | if (!range_found) { |
| 359 | msg_cerr("matching range not found\n"); |
| 360 | return -1; |
| 361 | } |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 362 | return 0; |
| 363 | } |
| 364 | |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 365 | int w25_status_to_range(const struct flashchip *flash, |
| 366 | const struct w25q_status *status, |
| 367 | unsigned int *start, unsigned int *len) |
| 368 | { |
| 369 | struct w25q_range *w25q_ranges; |
| 370 | int i, status_found = 0; |
| 371 | int num_entries; |
| 372 | |
| 373 | if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1; |
| 374 | for (i = 0; i < num_entries; i++) { |
| 375 | int bp; |
| 376 | |
| 377 | bp = status->bp0 | (status->bp1 << 1) | (status->bp2 << 2); |
| 378 | msg_cspew("comparing 0x%x 0x%x / 0x%x 0x%x / 0x%x 0x%x\n", |
| 379 | bp, w25q_ranges[i].bp, |
| 380 | status->tb, w25q_ranges[i].tb, |
| 381 | status->sec, w25q_ranges[i].sec); |
| 382 | if ((bp == w25q_ranges[i].bp) && |
| 383 | (status->tb == w25q_ranges[i].tb) && |
| 384 | (status->sec == w25q_ranges[i].sec)) { |
| 385 | *start = w25q_ranges[i].range.start; |
| 386 | *len = w25q_ranges[i].range.len; |
| 387 | |
| 388 | status_found = 1; |
| 389 | break; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | if (!status_found) { |
| 394 | msg_cerr("matching status not found\n"); |
| 395 | return -1; |
| 396 | } |
| 397 | return 0; |
| 398 | } |
| 399 | |
Louis Yung-Chieh Lo | 52aa930 | 2010-09-06 10:45:02 +0800 | [diff] [blame] | 400 | /* Since most chips we use must be WREN-ed before WRSR, |
| 401 | * we copy a write status function here before we have a good solution. */ |
| 402 | static int spi_write_status_register_WREN(int status) |
| 403 | { |
| 404 | int result; |
| 405 | struct spi_command cmds[] = { |
| 406 | { |
| 407 | /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */ |
| 408 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 409 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 410 | .readcnt = 0, |
| 411 | .readarr = NULL, |
| 412 | }, { |
| 413 | .writecnt = JEDEC_WRSR_OUTSIZE, |
| 414 | .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status }, |
| 415 | .readcnt = 0, |
| 416 | .readarr = NULL, |
| 417 | }, { |
| 418 | .writecnt = 0, |
| 419 | .writearr = NULL, |
| 420 | .readcnt = 0, |
| 421 | .readarr = NULL, |
| 422 | }}; |
| 423 | |
| 424 | result = spi_send_multicommand(cmds); |
| 425 | if (result) { |
| 426 | msg_cerr("%s failed during command execution\n", |
| 427 | __func__); |
| 428 | } |
| 429 | return result; |
| 430 | } |
| 431 | |
David Hendricks | d494b0a | 2010-08-16 16:28:50 -0700 | [diff] [blame] | 432 | static int w25_set_range(struct flashchip *flash, |
| 433 | unsigned int start, unsigned int len) |
| 434 | { |
| 435 | struct w25q_status status; |
| 436 | int tmp; |
| 437 | |
| 438 | memset(&status, 0, sizeof(status)); |
| 439 | tmp = spi_read_status_register(); |
| 440 | memcpy(&status, &tmp, 1); |
| 441 | msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp); |
| 442 | |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 443 | if (w25_range_to_status(flash, start, len, &status)) return -1; |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 444 | |
| 445 | msg_cdbg("status.busy: %x\n", status.busy); |
| 446 | msg_cdbg("status.wel: %x\n", status.wel); |
| 447 | msg_cdbg("status.bp0: %x\n", status.bp0); |
| 448 | msg_cdbg("status.bp1: %x\n", status.bp1); |
| 449 | msg_cdbg("status.bp2: %x\n", status.bp2); |
| 450 | msg_cdbg("status.tb: %x\n", status.tb); |
| 451 | msg_cdbg("status.sec: %x\n", status.sec); |
| 452 | msg_cdbg("status.srp0: %x\n", status.srp0); |
| 453 | |
| 454 | memcpy(&tmp, &status, sizeof(status)); |
Louis Yung-Chieh Lo | 52aa930 | 2010-09-06 10:45:02 +0800 | [diff] [blame] | 455 | spi_write_status_register_WREN(tmp); |
Louis Yung-Chieh Lo | c19d3c5 | 2010-10-08 11:59:16 +0800 | [diff] [blame] | 456 | msg_cinfo("%s: new status: 0x%02x\n", |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 457 | __func__, spi_read_status_register()); |
| 458 | |
| 459 | return 0; |
| 460 | } |
| 461 | |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 462 | static int w25_wp_status(struct flashchip *flash) |
| 463 | { |
| 464 | struct w25q_status status; |
| 465 | int tmp; |
| 466 | int start, len; |
| 467 | int ret = 0; |
| 468 | |
| 469 | tmp = spi_read_status_register(); |
| 470 | /* FIXME: this is NOT endian-free copy. */ |
| 471 | memcpy(&status, &tmp, 1); |
| 472 | msg_cinfo("WP: status: 0x%02x\n", tmp); |
| 473 | msg_cinfo("WP: status.srp0: %x\n", status.srp0); |
| 474 | msg_cinfo("WP: write protect is %s.\n", |
| 475 | status.srp0 ? "enabled" : "disabled"); |
| 476 | |
| 477 | msg_cinfo("WP: write protect range: "); |
| 478 | if (w25_status_to_range(flash, &status, &start, &len)) { |
| 479 | msg_cinfo("(cannot resolve the range)\n"); |
| 480 | ret = -1; |
| 481 | } else { |
| 482 | msg_cinfo("start=0x%08x, len=0x%08x\n", start, len); |
| 483 | } |
| 484 | |
| 485 | return ret; |
| 486 | } |
| 487 | |
Louis Yung-Chieh Lo | c19d3c5 | 2010-10-08 11:59:16 +0800 | [diff] [blame] | 488 | static int w25_set_srp0(struct flashchip *flash, int enable) |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 489 | { |
| 490 | struct w25q_status status; |
| 491 | int tmp = 0; |
| 492 | |
| 493 | memset(&status, 0, sizeof(status)); |
| 494 | tmp = spi_read_status_register(); |
| 495 | memcpy(&status, &tmp, 1); |
| 496 | msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp); |
| 497 | |
Louis Yung-Chieh Lo | c19d3c5 | 2010-10-08 11:59:16 +0800 | [diff] [blame] | 498 | status.srp0 = enable ? 1 : 0; |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 499 | memcpy(&tmp, &status, sizeof(status)); |
Louis Yung-Chieh Lo | 52aa930 | 2010-09-06 10:45:02 +0800 | [diff] [blame] | 500 | spi_write_status_register_WREN(tmp); |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 501 | msg_cdbg("%s: new status: 0x%02x\n", |
| 502 | __func__, spi_read_status_register()); |
| 503 | |
| 504 | return 0; |
| 505 | } |
| 506 | |
Louis Yung-Chieh Lo | c19d3c5 | 2010-10-08 11:59:16 +0800 | [diff] [blame] | 507 | static int w25_enable_writeprotect(struct flashchip *flash) |
| 508 | { |
| 509 | int ret; |
| 510 | |
| 511 | ret = w25_set_srp0(flash, 1); |
| 512 | if (!ret) |
| 513 | msg_cinfo("SUCCESS.\n"); |
| 514 | else |
| 515 | msg_cinfo("FAILED, error=%d.\n", ret); |
| 516 | return ret; |
| 517 | } |
| 518 | |
| 519 | static int w25_disable_writeprotect(struct flashchip *flash) |
| 520 | { |
| 521 | int ret; |
| 522 | |
| 523 | ret = w25_set_srp0(flash, 0); |
| 524 | if (!ret) |
| 525 | msg_cinfo("SUCCESS.\n"); |
| 526 | else |
| 527 | msg_cinfo("FAILED, error=%d.\n", ret); |
| 528 | return ret; |
| 529 | } |
| 530 | |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 531 | struct wp wp_w25 = { |
| 532 | .set_range = w25_set_range, |
| 533 | .enable = w25_enable_writeprotect, |
Louis Yung-Chieh Lo | c19d3c5 | 2010-10-08 11:59:16 +0800 | [diff] [blame] | 534 | .disable = w25_disable_writeprotect, |
Louis Yung-Chieh Lo | a92e8b2 | 2010-10-08 13:31:27 +0800 | [diff] [blame] | 535 | .wp_status = w25_wp_status, |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 536 | }; |