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