David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame^] | 1 | #include <stdlib.h> |
| 2 | #include <string.h> |
| 3 | |
| 4 | #include "flash.h" |
| 5 | #include "flashchips.h" |
| 6 | #include "chipdrivers.h" |
| 7 | |
| 8 | /* |
| 9 | * The following procedures rely on look-up tables to match the user-specified |
| 10 | * range with the chip's supported ranges. This turned out to be the most |
| 11 | * elegant approach since diferent flash chips use different levels of |
| 12 | * granularity and methods to determine protected ranges. In other words, |
| 13 | * be stupid and simple since clever arithmetic will not for many chips. |
| 14 | */ |
| 15 | |
| 16 | struct wp_range { |
| 17 | unsigned int start; /* starting address */ |
| 18 | unsigned int len; /* len */ |
| 19 | }; |
| 20 | |
| 21 | enum bit_state { |
| 22 | OFF = 0, |
| 23 | ON = 1, |
| 24 | X = 0 /* don't care */ |
| 25 | }; |
| 26 | |
| 27 | struct w25q_range { |
| 28 | enum bit_state sec; /* if 1, bp[2:0] describe sectors */ |
| 29 | enum bit_state tb; /* top/bottom select */ |
| 30 | unsigned short int bp : 3; /* block protect bitfield */ |
| 31 | struct wp_range range; |
| 32 | }; |
| 33 | |
| 34 | static struct w25q_range w25q16_ranges[] = { |
| 35 | { X, X, 0, {0, 0} }, /* none */ |
| 36 | { 0, 0, 0x1, {0x1f0000, 64 * 1024} }, |
| 37 | { 0, 0, 0x2, {0x1e0000, 128 * 1024} }, |
| 38 | { 0, 0, 0x3, {0x1c0000, 256 * 1024} }, |
| 39 | { 0, 0, 0x4, {0x180000, 512 * 1024} }, |
| 40 | { 0, 0, 0x5, {0x100000, 1024 * 1024} }, |
| 41 | |
| 42 | { 0, 1, 0x1, {0x000000, 64 * 1024} }, |
| 43 | { 0, 1, 0x2, {0x000000, 128 * 1024} }, |
| 44 | { 0, 1, 0x3, {0x000000, 256 * 1024} }, |
| 45 | { 0, 1, 0x4, {0x000000, 512 * 1024} }, |
| 46 | { 0, 1, 0x5, {0x000000, 1024 * 1024} }, |
| 47 | { X, X, 0x6, {0x000000, 2048 * 1024} }, |
| 48 | { X, X, 0x7, {0x000000, 2048 * 1024} }, |
| 49 | |
| 50 | { 1, 0, 0x1, {0x1ff000, 4 * 1024} }, |
| 51 | { 1, 0, 0x2, {0x1fe000, 8 * 1024} }, |
| 52 | { 1, 0, 0x3, {0x1fc000, 16 * 1024} }, |
| 53 | { 1, 0, 0x4, {0x1f8000, 32 * 1024} }, |
| 54 | { 1, 0, 0x5, {0x1f8000, 32 * 1024} }, |
| 55 | |
| 56 | { 1, 1, 0x1, {0x000000, 4 * 1024} }, |
| 57 | { 1, 1, 0x2, {0x000000, 8 * 1024} }, |
| 58 | { 1, 1, 0x3, {0x000000, 16 * 1024} }, |
| 59 | { 1, 1, 0x4, {0x000000, 32 * 1024} }, |
| 60 | { 1, 1, 0x5, {0x000000, 32 * 1024} }, |
| 61 | }; |
| 62 | |
| 63 | static struct w25q_range w25q32_ranges[] = { |
| 64 | { X, X, 0, {0, 0} }, /* none */ |
| 65 | { 0, 0, 0x1, {0x3f0000, 64 * 1024} }, |
| 66 | { 0, 0, 0x2, {0x3e0000, 128 * 1024} }, |
| 67 | { 0, 0, 0x3, {0x3c0000, 256 * 1024} }, |
| 68 | { 0, 0, 0x4, {0x380000, 512 * 1024} }, |
| 69 | { 0, 0, 0x5, {0x300000, 1024 * 1024} }, |
| 70 | { 0, 0, 0x6, {0x200000, 2024 * 1024} }, |
| 71 | |
| 72 | { 0, 1, 0x1, {0x000000, 64 * 1024} }, |
| 73 | { 0, 1, 0x2, {0x000000, 128 * 1024} }, |
| 74 | { 0, 1, 0x3, {0x000000, 256 * 1024} }, |
| 75 | { 0, 1, 0x4, {0x000000, 512 * 1024} }, |
| 76 | { 0, 1, 0x5, {0x000000, 1024 * 1024} }, |
| 77 | { 0, 1, 0x6, {0x000000, 2048 * 1024} }, |
| 78 | { X, X, 0x7, {0x000000, 4096 * 1024} }, |
| 79 | |
| 80 | { 1, 0, 0x1, {0x3ff000, 4 * 1024} }, |
| 81 | { 1, 0, 0x2, {0x3fe000, 8 * 1024} }, |
| 82 | { 1, 0, 0x3, {0x3fc000, 16 * 1024} }, |
| 83 | { 1, 0, 0x4, {0x3f8000, 32 * 1024} }, |
| 84 | { 1, 0, 0x5, {0x3f8000, 32 * 1024} }, |
| 85 | |
| 86 | { 1, 1, 0x1, {0x000000, 4 * 1024} }, |
| 87 | { 1, 1, 0x2, {0x000000, 8 * 1024} }, |
| 88 | { 1, 1, 0x3, {0x000000, 16 * 1024} }, |
| 89 | { 1, 1, 0x4, {0x000000, 32 * 1024} }, |
| 90 | { 1, 1, 0x5, {0x000000, 32 * 1024} }, |
| 91 | }; |
| 92 | |
| 93 | static struct w25q_range w25q80_ranges[] = { |
| 94 | { X, X, 0, {0, 0} }, /* none */ |
| 95 | { 0, 0, 0x1, {0x0f0000, 64 * 1024} }, |
| 96 | { 0, 0, 0x2, {0x0e0000, 128 * 1024} }, |
| 97 | { 0, 0, 0x3, {0x0c0000, 256 * 1024} }, |
| 98 | { 0, 0, 0x4, {0x080000, 512 * 1024} }, |
| 99 | |
| 100 | { 0, 1, 0x1, {0x000000, 64 * 1024} }, |
| 101 | { 0, 1, 0x2, {0x000000, 128 * 1024} }, |
| 102 | { 0, 1, 0x3, {0x000000, 256 * 1024} }, |
| 103 | { 0, 1, 0x4, {0x000000, 512 * 1024} }, |
| 104 | { X, X, 0x6, {0x000000, 1048 * 1024} }, |
| 105 | { X, X, 0x7, {0x000000, 1048 * 1024} }, |
| 106 | |
| 107 | { 1, 0, 0x1, {0x1ff000, 4 * 1024} }, |
| 108 | { 1, 0, 0x2, {0x1fe000, 8 * 1024} }, |
| 109 | { 1, 0, 0x3, {0x1fc000, 16 * 1024} }, |
| 110 | { 1, 0, 0x4, {0x1f8000, 32 * 1024} }, |
| 111 | { 1, 0, 0x5, {0x1f8000, 32 * 1024} }, |
| 112 | |
| 113 | { 1, 1, 0x1, {0x000000, 4 * 1024} }, |
| 114 | { 1, 1, 0x2, {0x000000, 8 * 1024} }, |
| 115 | { 1, 1, 0x3, {0x000000, 16 * 1024} }, |
| 116 | { 1, 1, 0x4, {0x000000, 32 * 1024} }, |
| 117 | { 1, 1, 0x5, {0x000000, 32 * 1024} }, |
| 118 | }; |
| 119 | |
| 120 | struct w25q_status { |
| 121 | /* this maps to register layout -- do not change ordering */ |
| 122 | unsigned char busy : 1; |
| 123 | unsigned char wel : 1; |
| 124 | unsigned char bp0 : 1; |
| 125 | unsigned char bp1 : 1; |
| 126 | unsigned char bp2 : 1; |
| 127 | unsigned char tb : 1; |
| 128 | unsigned char sec : 1; |
| 129 | unsigned char srp0 : 1; |
| 130 | /* FIXME: what about the second status register? */ |
| 131 | // unsigned char srp1 : 1; |
| 132 | // unsigned char qe : 1; |
| 133 | } __attribute__ ((packed)); |
| 134 | |
| 135 | static int w25_set_range(struct flashchip *flash, |
| 136 | unsigned int start, unsigned int len) |
| 137 | { |
| 138 | struct w25q_status status; |
| 139 | struct w25q_range *w25q_ranges; |
| 140 | int i, num_entries = 0; |
| 141 | int tmp = 0, range_found = 0; |
| 142 | |
| 143 | if (flash->manufacture_id != WINBOND_NEX_ID) |
| 144 | return -1; |
| 145 | switch(flash->model_id) { |
| 146 | case W_25Q80: |
| 147 | w25q_ranges = w25q80_ranges; |
| 148 | num_entries = ARRAY_SIZE(w25q80_ranges); |
| 149 | break; |
| 150 | case W_25Q16: |
| 151 | w25q_ranges = w25q16_ranges; |
| 152 | num_entries = ARRAY_SIZE(w25q16_ranges); |
| 153 | break; |
| 154 | case W_25Q32: |
| 155 | w25q_ranges = w25q32_ranges; |
| 156 | num_entries = ARRAY_SIZE(w25q32_ranges); |
| 157 | break; |
| 158 | // case W_25Q64: |
| 159 | // break; |
| 160 | default: |
| 161 | msg_cerr("%s: flash chip mismatch, aborting\n", __func__); |
| 162 | return -1; |
| 163 | } |
| 164 | |
| 165 | memset(&status, 0, sizeof(status)); |
| 166 | tmp = spi_read_status_register(); |
| 167 | memcpy(&status, &tmp, 1); |
| 168 | msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp); |
| 169 | |
| 170 | for (i = 0; i < num_entries; i++) { |
| 171 | struct wp_range *r = &w25q_ranges[i].range; |
| 172 | |
| 173 | msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n", |
| 174 | start, len, r->start, r->len); |
| 175 | if ((start == r->start) && (len == r->len)) { |
| 176 | status.bp0 = w25q_ranges[i].bp & 1; |
| 177 | status.bp1 = w25q_ranges[i].bp >> 1; |
| 178 | status.bp2 = w25q_ranges[i].bp >> 2; |
| 179 | status.tb = w25q_ranges[i].tb; |
| 180 | status.sec = w25q_ranges[i].sec; |
| 181 | |
| 182 | range_found = 1; |
| 183 | break; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | if (!range_found) { |
| 188 | msg_cerr("matching range not found\n"); |
| 189 | return -1; |
| 190 | } |
| 191 | |
| 192 | msg_cdbg("status.busy: %x\n", status.busy); |
| 193 | msg_cdbg("status.wel: %x\n", status.wel); |
| 194 | msg_cdbg("status.bp0: %x\n", status.bp0); |
| 195 | msg_cdbg("status.bp1: %x\n", status.bp1); |
| 196 | msg_cdbg("status.bp2: %x\n", status.bp2); |
| 197 | msg_cdbg("status.tb: %x\n", status.tb); |
| 198 | msg_cdbg("status.sec: %x\n", status.sec); |
| 199 | msg_cdbg("status.srp0: %x\n", status.srp0); |
| 200 | |
| 201 | memcpy(&tmp, &status, sizeof(status)); |
| 202 | spi_write_status_enable(); |
| 203 | spi_write_status_register(tmp); |
| 204 | msg_cdbg("%s: new status: 0x%02x\n", |
| 205 | __func__, spi_read_status_register()); |
| 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | static int w25_enable_writeprotect(struct flashchip *flash) |
| 211 | { |
| 212 | struct w25q_status status; |
| 213 | int tmp = 0; |
| 214 | |
| 215 | memset(&status, 0, sizeof(status)); |
| 216 | tmp = spi_read_status_register(); |
| 217 | memcpy(&status, &tmp, 1); |
| 218 | msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp); |
| 219 | |
| 220 | status.srp0 = 1; |
| 221 | memcpy(&tmp, &status, sizeof(status)); |
| 222 | spi_write_status_enable(); |
| 223 | spi_write_status_register(tmp); |
| 224 | msg_cdbg("%s: new status: 0x%02x\n", |
| 225 | __func__, spi_read_status_register()); |
| 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | struct wp wp_w25 = { |
| 231 | .set_range = w25_set_range, |
| 232 | .enable = w25_enable_writeprotect, |
| 233 | }; |