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 | |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 36 | /* |
| 37 | * claus.gindhart@kontron.com |
| 38 | * The ST M50FLW080B and STM50FLW080B chips have to be unlocked, |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 39 | * before you can erase them or write to them. |
| 40 | */ |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 41 | int unlock_block_stm50flw0x0x(struct flashchip *flash, int offset) |
| 42 | { |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 43 | chipaddr wrprotect = flash->virtual_registers + 2; |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 44 | const uint8_t unlock_sector = 0x00; |
| 45 | int j; |
| 46 | |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 47 | /* |
| 48 | * These chips have to be unlocked before you can erase them or write |
| 49 | * to them. The size of the locking sectors depends on the type |
| 50 | * of chip. |
| 51 | * |
| 52 | * Sometimes, the BIOS does this for you; so you propably |
| 53 | * don't need to worry about that. |
| 54 | */ |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 55 | |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 56 | /* Check, if it's is a top/bottom-block with 4k-sectors. */ |
| 57 | /* TODO: What about the other types? */ |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 58 | if ((offset == 0) || |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 59 | (offset == (flash->model_id == ST_M50FLW080A ? 0xE0000 : 0x10000)) |
| 60 | || (offset == 0xF0000)) { |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 61 | |
| 62 | // unlock each 4k-sector |
| 63 | for (j = 0; j < 0x10000; j += 0x1000) { |
| 64 | printf_debug("unlocking at 0x%x\n", offset + j); |
Carl-Daniel Hailfinger | d13775e | 2009-05-11 20:04:30 +0000 | [diff] [blame] | 65 | chip_writeb(unlock_sector, wrprotect + offset + j); |
| 66 | if (chip_readb(wrprotect + offset + j) != unlock_sector) { |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 67 | printf("Cannot unlock sector @ 0x%x\n", |
| 68 | offset + j); |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 69 | return -1; |
| 70 | } |
| 71 | } |
| 72 | } else { |
| 73 | printf_debug("unlocking at 0x%x\n", offset); |
Carl-Daniel Hailfinger | d13775e | 2009-05-11 20:04:30 +0000 | [diff] [blame] | 74 | chip_writeb(unlock_sector, wrprotect + offset); |
| 75 | if (chip_readb(wrprotect + offset) != unlock_sector) { |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 76 | printf("Cannot unlock sector @ 0x%x\n", offset); |
| 77 | return -1; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
Sean Nelson | 28accc2 | 2010-03-19 18:47:06 +0000 | [diff] [blame] | 84 | int unlock_stm50flw0x0x(struct flashchip *flash) |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 85 | { |
Sean Nelson | 28accc2 | 2010-03-19 18:47:06 +0000 | [diff] [blame] | 86 | int i; |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 87 | |
Sean Nelson | 4631319 | 2010-03-20 15:15:36 +0000 | [diff] [blame] | 88 | for (i = 0; i < flash->total_size * 1024; i+= flash->page_size) { |
Sean Nelson | 28accc2 | 2010-03-19 18:47:06 +0000 | [diff] [blame] | 89 | if(unlock_block_stm50flw0x0x(flash, i)) { |
| 90 | fprintf(stderr, "UNLOCK FAILED!\n"); |
| 91 | return -1; |
| 92 | } |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 93 | } |
Sean Nelson | 56358aa | 2010-01-19 16:08:51 +0000 | [diff] [blame] | 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | int erase_sector_stm50flw0x0x(struct flashchip *flash, unsigned int sector, unsigned int sectorsize) |
| 99 | { |
| 100 | chipaddr bios = flash->virtual_memory + sector; |
| 101 | |
| 102 | // clear status register |
| 103 | chip_writeb(0x50, bios); |
| 104 | printf_debug("Erase at 0x%lx\n", bios); |
| 105 | // now start it |
| 106 | chip_writeb(0x32, bios); |
| 107 | chip_writeb(0xd0, bios); |
| 108 | programmer_delay(10); |
| 109 | |
Sean Nelson | 28accc2 | 2010-03-19 18:47:06 +0000 | [diff] [blame] | 110 | wait_82802ab(flash->virtual_memory); |
Sean Nelson | 56358aa | 2010-01-19 16:08:51 +0000 | [diff] [blame] | 111 | |
| 112 | if (check_erased_range(flash, sector, sectorsize)) { |
| 113 | fprintf(stderr, "ERASE FAILED!\n"); |
| 114 | return -1; |
| 115 | } |
| 116 | printf("DONE BLOCK 0x%x\n", sector); |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
Sean Nelson | 28accc2 | 2010-03-19 18:47:06 +0000 | [diff] [blame] | 121 | int erase_chip_stm50flw0x0x(struct flashchip *flash, unsigned int addr, unsigned int blocklen) |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 122 | { |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 123 | int i; |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 124 | int total_size = flash->total_size * 1024; |
| 125 | int page_size = flash->page_size; |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 126 | |
Sean Nelson | 28accc2 | 2010-03-19 18:47:06 +0000 | [diff] [blame] | 127 | if ((addr != 0) || (blocklen != flash->total_size * 1024)) { |
| 128 | msg_cerr("%s called with incorrect arguments\n", |
| 129 | __func__); |
| 130 | return -1; |
| 131 | } |
| 132 | |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 133 | printf("Erasing page:\n"); |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 134 | for (i = 0; i < total_size / page_size; i++) { |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 135 | printf |
| 136 | ("\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"); |
| 137 | printf("%04d at address: 0x%08x ", i, i * page_size); |
Sean Nelson | 28accc2 | 2010-03-19 18:47:06 +0000 | [diff] [blame] | 138 | //if (unlock_block_stm50flw0x0x(flash, i * page_size)) { |
| 139 | // fprintf(stderr, "UNLOCK FAILED!\n"); |
| 140 | // return -1; |
| 141 | //} |
| 142 | if (erase_block_82802ab(flash, i * page_size, page_size)) { |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 143 | fprintf(stderr, "ERASE FAILED!\n"); |
| 144 | return -1; |
| 145 | } |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 146 | } |
| 147 | printf("\n"); |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 148 | |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 149 | return 0; |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 150 | } |