Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2008 Claus Gindhart <claus.gindhart@kontron.com> |
| 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 |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | /* |
| 22 | * This module is designed for supporting the devices |
| 23 | * ST M50FLW040A (not yet tested) |
| 24 | * ST M50FLW040B (not yet tested) |
| 25 | * ST M50FLW080A |
| 26 | * ST M50FLW080B (not yet tested) |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 27 | */ |
| 28 | |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 29 | #include <string.h> |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 30 | #include "flash.h" |
| 31 | |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 32 | void protect_stm50flw0x0x(chipaddr bios) |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 33 | { |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 34 | chip_writeb(0xAA, bios + 0x5555); |
| 35 | chip_writeb(0x55, bios + 0x2AAA); |
| 36 | chip_writeb(0xA0, bios + 0x5555); |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 37 | |
| 38 | usleep(200); |
| 39 | } |
| 40 | |
| 41 | int probe_stm50flw0x0x(struct flashchip *flash) |
| 42 | { |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 43 | chipaddr bios = flash->virtual_memory; |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 44 | uint8_t id1, id2; |
| 45 | uint32_t largeid1, largeid2; |
| 46 | |
| 47 | /* Issue JEDEC Product ID Entry command */ |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 48 | chip_writeb(0xAA, bios + 0x5555); |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 49 | myusec_delay(10); |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 50 | chip_writeb(0x55, bios + 0x2AAA); |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 51 | myusec_delay(10); |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 52 | chip_writeb(0x90, bios + 0x5555); |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 53 | myusec_delay(40); |
| 54 | |
| 55 | /* Read product ID */ |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 56 | id1 = chip_readb(bios); |
| 57 | id2 = chip_readb(bios + 0x01); |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 58 | largeid1 = id1; |
| 59 | largeid2 = id2; |
| 60 | |
| 61 | /* Check if it is a continuation ID, this should be a while loop. */ |
| 62 | if (id1 == 0x7F) { |
| 63 | largeid1 <<= 8; |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 64 | id1 = chip_readb(bios + 0x100); |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 65 | largeid1 |= id1; |
| 66 | } |
| 67 | if (id2 == 0x7F) { |
| 68 | largeid2 <<= 8; |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 69 | id2 = chip_readb(bios + 0x101); |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 70 | largeid2 |= id2; |
| 71 | } |
| 72 | |
| 73 | /* Issue JEDEC Product ID Exit command */ |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 74 | chip_writeb(0xAA, bios + 0x5555); |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 75 | myusec_delay(10); |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 76 | chip_writeb(0x55, bios + 0x2AAA); |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 77 | myusec_delay(10); |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 78 | chip_writeb(0xF0, bios + 0x5555); |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 79 | myusec_delay(40); |
| 80 | |
Peter Stuge | 5cafc33 | 2009-01-25 23:52:45 +0000 | [diff] [blame] | 81 | printf_debug("%s: id1 0x%02x, id2 0x%02x\n", __FUNCTION__, largeid1, |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 82 | largeid2); |
| 83 | |
| 84 | if (largeid1 != flash->manufacture_id || largeid2 != flash->model_id) |
| 85 | return 0; |
| 86 | |
| 87 | map_flash_registers(flash); |
| 88 | |
| 89 | return 1; |
| 90 | } |
| 91 | |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 92 | static void wait_stm50flw0x0x(chipaddr bios) |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 93 | { |
| 94 | uint8_t id1; |
| 95 | // id2; |
| 96 | |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 97 | chip_writeb(0x70, bios); |
| 98 | if ((chip_readb(bios) & 0x80) == 0) { // it's busy |
| 99 | while ((chip_readb(bios) & 0x80) == 0) ; |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 100 | } |
| 101 | // put another command to get out of status register mode |
| 102 | |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 103 | chip_writeb(0x90, bios); |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 104 | myusec_delay(10); |
| 105 | |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 106 | id1 = chip_readb(bios); |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 107 | |
| 108 | // this is needed to jam it out of "read id" mode |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 109 | chip_writeb(0xAA, bios + 0x5555); |
| 110 | chip_writeb(0x55, bios + 0x2AAA); |
| 111 | chip_writeb(0xF0, bios + 0x5555); |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /* |
| 115 | * claus.gindhart@kontron.com |
| 116 | * The ST M50FLW080B and STM50FLW080B chips have to be unlocked, |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 117 | * before you can erase them or write to them. |
| 118 | */ |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 119 | int unlock_block_stm50flw0x0x(struct flashchip *flash, int offset) |
| 120 | { |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 121 | chipaddr wrprotect = flash->virtual_registers + 2; |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 122 | const uint8_t unlock_sector = 0x00; |
| 123 | int j; |
| 124 | |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 125 | /* |
| 126 | * These chips have to be unlocked before you can erase them or write |
| 127 | * to them. The size of the locking sectors depends on the type |
| 128 | * of chip. |
| 129 | * |
| 130 | * Sometimes, the BIOS does this for you; so you propably |
| 131 | * don't need to worry about that. |
| 132 | */ |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 133 | |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 134 | /* Check, if it's is a top/bottom-block with 4k-sectors. */ |
| 135 | /* TODO: What about the other types? */ |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 136 | if ((offset == 0) || |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 137 | (offset == (flash->model_id == ST_M50FLW080A ? 0xE0000 : 0x10000)) |
| 138 | || (offset == 0xF0000)) { |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 139 | |
| 140 | // unlock each 4k-sector |
| 141 | for (j = 0; j < 0x10000; j += 0x1000) { |
| 142 | printf_debug("unlocking at 0x%x\n", offset + j); |
Carl-Daniel Hailfinger | d13775e | 2009-05-11 20:04:30 +0000 | [diff] [blame] | 143 | chip_writeb(unlock_sector, wrprotect + offset + j); |
| 144 | if (chip_readb(wrprotect + offset + j) != unlock_sector) { |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 145 | printf("Cannot unlock sector @ 0x%x\n", |
| 146 | offset + j); |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 147 | return -1; |
| 148 | } |
| 149 | } |
| 150 | } else { |
| 151 | printf_debug("unlocking at 0x%x\n", offset); |
Carl-Daniel Hailfinger | d13775e | 2009-05-11 20:04:30 +0000 | [diff] [blame] | 152 | chip_writeb(unlock_sector, wrprotect + offset); |
| 153 | if (chip_readb(wrprotect + offset) != unlock_sector) { |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 154 | printf("Cannot unlock sector @ 0x%x\n", offset); |
| 155 | return -1; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | int erase_block_stm50flw0x0x(struct flashchip *flash, int offset) |
| 163 | { |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 164 | chipaddr bios = flash->virtual_memory + offset; |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 165 | int j; |
| 166 | |
| 167 | // clear status register |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 168 | chip_writeb(0x50, bios); |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 169 | printf_debug("Erase at 0x%lx\n", bios); |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 170 | // now start it |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 171 | chip_writeb(0x20, bios); |
| 172 | chip_writeb(0xd0, bios); |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 173 | myusec_delay(10); |
| 174 | |
| 175 | wait_stm50flw0x0x(flash->virtual_memory); |
| 176 | |
| 177 | for (j = 0; j < flash->page_size; j++) { |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 178 | if (chip_readb(bios + j) != 0xFF) { |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 179 | printf("Erase failed at 0x%x\n", offset + j); |
| 180 | return -1; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | printf("DONE BLOCK 0x%x\n", offset); |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 189 | int write_page_stm50flw0x0x(chipaddr bios, uint8_t *src, |
| 190 | chipaddr dst, int page_size) |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 191 | { |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 192 | int i, rc = 0; |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 193 | chipaddr d = dst; |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 194 | uint8_t *s = src; |
| 195 | |
| 196 | /* transfer data from source to destination */ |
| 197 | for (i = 0; i < page_size; i++) { |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 198 | chip_writeb(0x40, dst); |
| 199 | chip_writeb(*src++, dst++); |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 200 | wait_stm50flw0x0x(bios); |
| 201 | } |
| 202 | |
| 203 | /* claus.gindhart@kontron.com |
| 204 | * TODO |
| 205 | * I think, that verification is not required, but |
| 206 | * i leave it in anyway |
| 207 | */ |
| 208 | dst = d; |
| 209 | src = s; |
| 210 | for (i = 0; i < page_size; i++) { |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 211 | if (chip_readb(dst) != *src) { |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 212 | rc = -1; |
| 213 | break; |
| 214 | } |
| 215 | dst++; |
| 216 | src++; |
| 217 | } |
| 218 | |
| 219 | if (rc) { |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 220 | fprintf(stderr, " page 0x%lx failed!\n", |
| 221 | (d - bios) / page_size); |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | return rc; |
| 225 | } |
| 226 | |
| 227 | /* I simply erase block by block |
| 228 | * I Chip This is not the fastest way, but it works |
| 229 | */ |
| 230 | int erase_stm50flw0x0x(struct flashchip *flash) |
| 231 | { |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 232 | int i, rc = 0; |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 233 | int total_size = flash->total_size * 1024; |
| 234 | int page_size = flash->page_size; |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 235 | chipaddr bios = flash->virtual_memory; |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 236 | |
| 237 | printf("Erasing page:\n"); |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 238 | for (i = 0; (i < total_size / page_size) && (rc == 0); i++) { |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 239 | printf |
| 240 | ("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"); |
| 241 | printf("%04d at address: 0x%08x ", i, i * page_size); |
| 242 | rc = unlock_block_stm50flw0x0x(flash, i * page_size); |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 243 | if (!rc) |
| 244 | rc = erase_block_stm50flw0x0x(flash, i * page_size); |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 245 | } |
| 246 | printf("\n"); |
| 247 | protect_stm50flw0x0x(bios); |
| 248 | |
| 249 | return rc; |
| 250 | } |
| 251 | |
| 252 | int write_stm50flw0x0x(struct flashchip *flash, uint8_t * buf) |
| 253 | { |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 254 | int i, rc = 0; |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 255 | int total_size = flash->total_size * 1024; |
| 256 | int page_size = flash->page_size; |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 257 | chipaddr bios = flash->virtual_memory; |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 258 | |
| 259 | printf("Programming page: \n"); |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 260 | for (i = 0; (i < total_size / page_size) && (rc == 0); i++) { |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 261 | printf |
| 262 | ("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"); |
| 263 | printf("%04d at address: 0x%08x ", i, i * page_size); |
| 264 | |
| 265 | /* Auto Skip Blocks, which already contain the desired data |
| 266 | * Faster, because we only write, what has changed |
| 267 | * More secure, because blocks, which are excluded |
| 268 | * (with the exclude or layout feature) |
| 269 | * are not erased and rewritten; data is retained also |
| 270 | * in sudden power off situations |
| 271 | */ |
| 272 | if (!memcmp((void *)(buf + i * page_size), |
| 273 | (void *)(bios + i * page_size), page_size)) { |
| 274 | printf("SKIPPED\n"); |
| 275 | continue; |
| 276 | } |
| 277 | |
| 278 | rc = unlock_block_stm50flw0x0x(flash, i * page_size); |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 279 | if (!rc) |
| 280 | rc = erase_block_stm50flw0x0x(flash, i * page_size); |
| 281 | if (!rc) |
| 282 | write_page_stm50flw0x0x(bios, buf + i * page_size, |
| 283 | bios + i * page_size, page_size); |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 284 | } |
| 285 | printf("\n"); |
| 286 | protect_stm50flw0x0x(bios); |
| 287 | |
| 288 | return rc; |
| 289 | } |