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