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