Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 1 | /* |
Uwe Hermann | d110764 | 2007-08-29 17:52:32 +0000 | [diff] [blame] | 2 | * This file is part of the flashrom project. |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 3 | * |
Uwe Hermann | d22a1d4 | 2007-09-09 20:21:05 +0000 | [diff] [blame] | 4 | * Copyright (C) 2000 Silicon Integrated System Corporation |
| 5 | * Copyright (C) 2005-2007 coresystems GmbH |
Sean Nelson | 51c83fb | 2010-01-20 20:55:53 +0000 | [diff] [blame] | 6 | * Copyright (C) 2009 Sean Nelson <audiohacked@gmail.com> |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 7 | * |
Uwe Hermann | d110764 | 2007-08-29 17:52:32 +0000 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 12 | * |
Uwe Hermann | d110764 | 2007-08-29 17:52:32 +0000 | [diff] [blame] | 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 17 | * |
Uwe Hermann | d110764 | 2007-08-29 17:52:32 +0000 | [diff] [blame] | 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 21 | */ |
| 22 | |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 23 | #include <stdlib.h> |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 24 | #include "flash.h" |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 25 | |
| 26 | #define SECTOR_ERASE 0x30 |
| 27 | #define BLOCK_ERASE 0x20 |
| 28 | #define ERASE 0xD0 |
| 29 | #define AUTO_PGRM 0x10 |
| 30 | #define RESET 0xFF |
| 31 | #define READ_ID 0x90 |
| 32 | #define READ_STATUS 0x70 |
| 33 | #define CLEAR_STATUS 0x50 |
| 34 | |
| 35 | #define STATUS_BPS (1 << 1) |
| 36 | #define STATUS_ESS (1 << 6) |
| 37 | #define STATUS_WSMS (1 << 7) |
| 38 | |
Sean Nelson | 51c83fb | 2010-01-20 20:55:53 +0000 | [diff] [blame] | 39 | int unlock_block_49lfxxxc(struct flashchip *flash, unsigned long address, unsigned char bits) |
| 40 | { |
| 41 | unsigned long lock = flash->virtual_registers + address + 2; |
| 42 | printf_debug("lockbits at address=0x%08lx is 0x%01x\n", lock, chip_readb(lock)); |
| 43 | chip_writeb(bits, lock); |
| 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | |
Carl-Daniel Hailfinger | 500dd9d | 2009-06-05 13:30:49 +0000 | [diff] [blame] | 48 | static int write_lockbits_49lfxxxc(struct flashchip *flash, unsigned char bits) |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 49 | { |
Carl-Daniel Hailfinger | 500dd9d | 2009-06-05 13:30:49 +0000 | [diff] [blame] | 50 | chipaddr registers = flash->virtual_registers; |
| 51 | int i, left = flash->total_size * 1024; |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 52 | unsigned long address; |
| 53 | |
Carl-Daniel Hailfinger | 500dd9d | 2009-06-05 13:30:49 +0000 | [diff] [blame] | 54 | printf_debug("\nbios=0x%08lx\n", registers); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 55 | for (i = 0; left > 65536; i++, left -= 65536) { |
Carl-Daniel Hailfinger | 500dd9d | 2009-06-05 13:30:49 +0000 | [diff] [blame] | 56 | printf_debug("lockbits at address=0x%08lx is 0x%01x\n", |
| 57 | registers + (i * 65536) + 2, |
| 58 | chip_readb(registers + (i * 65536) + 2)); |
| 59 | chip_writeb(bits, registers + (i * 65536) + 2); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 60 | } |
| 61 | address = i * 65536; |
Carl-Daniel Hailfinger | 500dd9d | 2009-06-05 13:30:49 +0000 | [diff] [blame] | 62 | printf_debug("lockbits at address=0x%08lx is 0x%01x\n", |
| 63 | registers + address + 2, |
| 64 | chip_readb(registers + address + 2)); |
| 65 | chip_writeb(bits, registers + address + 2); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 66 | address += 32768; |
Carl-Daniel Hailfinger | 500dd9d | 2009-06-05 13:30:49 +0000 | [diff] [blame] | 67 | printf_debug("lockbits at address=0x%08lx is 0x%01x\n", |
| 68 | registers + address + 2, |
| 69 | chip_readb(registers + address + 2)); |
| 70 | chip_writeb(bits, registers + address + 2); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 71 | address += 8192; |
Carl-Daniel Hailfinger | 500dd9d | 2009-06-05 13:30:49 +0000 | [diff] [blame] | 72 | printf_debug("lockbits at address=0x%08lx is 0x%01x\n", |
| 73 | registers + address + 2, |
| 74 | chip_readb(registers + address + 2)); |
| 75 | chip_writeb(bits, registers + address + 2); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 76 | address += 8192; |
Carl-Daniel Hailfinger | 500dd9d | 2009-06-05 13:30:49 +0000 | [diff] [blame] | 77 | printf_debug("lockbits at address=0x%08lx is 0x%01x\n", |
| 78 | registers + address + 2, |
| 79 | chip_readb(registers + address + 2)); |
| 80 | chip_writeb(bits, registers + address + 2); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 81 | |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 82 | return 0; |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Sean Nelson | 51c83fb | 2010-01-20 20:55:53 +0000 | [diff] [blame] | 85 | int erase_sector_49lfxxxc(struct flashchip *flash, unsigned int address, unsigned int sector_size) |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 86 | { |
| 87 | unsigned char status; |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 88 | chipaddr bios = flash->virtual_memory; |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 89 | |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 90 | chip_writeb(SECTOR_ERASE, bios); |
| 91 | chip_writeb(ERASE, bios + address); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 92 | |
| 93 | do { |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 94 | status = chip_readb(bios); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 95 | if (status & (STATUS_ESS | STATUS_BPS)) { |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 96 | printf("sector erase FAILED at address=0x%08lx status=0x%01x\n", bios + address, status); |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 97 | chip_writeb(CLEAR_STATUS, bios); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 98 | return (-1); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 99 | } |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 100 | } while (!(status & STATUS_WSMS)); |
Carl-Daniel Hailfinger | 322f305 | 2009-07-20 15:21:18 +0000 | [diff] [blame] | 101 | chip_writeb(RESET, bios); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 102 | |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 103 | if (check_erased_range(flash, address, sector_size)) { |
| 104 | fprintf(stderr, "ERASE FAILED!\n"); |
| 105 | return -1; |
| 106 | } |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 107 | return 0; |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Sean Nelson | 51c83fb | 2010-01-20 20:55:53 +0000 | [diff] [blame] | 110 | int erase_block_49lfxxxc(struct flashchip *flash, unsigned int address, unsigned int block_size) |
| 111 | { |
| 112 | unsigned char status; |
| 113 | chipaddr bios = flash->virtual_memory; |
| 114 | |
| 115 | chip_writeb(BLOCK_ERASE, bios); |
| 116 | chip_writeb(ERASE, bios + address); |
| 117 | |
| 118 | do { |
| 119 | status = chip_readb(bios); |
| 120 | if (status & (STATUS_ESS | STATUS_BPS)) { |
| 121 | printf("block erase FAILED at address=0x%08lx status=0x%01x\n", bios + address, status); |
| 122 | chip_writeb(CLEAR_STATUS, bios); |
| 123 | return (-1); |
| 124 | } |
| 125 | } while (!(status & STATUS_WSMS)); |
| 126 | chip_writeb(RESET, bios); |
| 127 | |
| 128 | if (check_erased_range(flash, address, block_size)) { |
| 129 | fprintf(stderr, "ERASE FAILED!\n"); |
| 130 | return -1; |
| 131 | } |
| 132 | return 0; |
| 133 | } |
| 134 | |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 135 | static int write_sector_49lfxxxc(chipaddr bios, uint8_t *src, chipaddr dst, |
| 136 | unsigned int page_size) |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 137 | { |
| 138 | int i; |
| 139 | unsigned char status; |
| 140 | |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 141 | chip_writeb(CLEAR_STATUS, bios); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 142 | for (i = 0; i < page_size; i++) { |
| 143 | /* transfer data from source to destination */ |
| 144 | if (*src == 0xFF) { |
| 145 | dst++, src++; |
| 146 | /* If the data is 0xFF, don't program it */ |
| 147 | continue; |
| 148 | } |
| 149 | /*issue AUTO PROGRAM command */ |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 150 | chip_writeb(AUTO_PGRM, bios); |
| 151 | chip_writeb(*src++, dst++); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 152 | |
| 153 | do { |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 154 | status = chip_readb(bios); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 155 | if (status & (STATUS_ESS | STATUS_BPS)) { |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 156 | printf("sector write FAILED at address=0x%08lx status=0x%01x\n", dst, status); |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 157 | chip_writeb(CLEAR_STATUS, bios); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 158 | return (-1); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 159 | } |
| 160 | } while (!(status & STATUS_WSMS)); |
| 161 | } |
| 162 | |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 163 | return 0; |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | int probe_49lfxxxc(struct flashchip *flash) |
| 167 | { |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 168 | chipaddr bios = flash->virtual_memory; |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 169 | uint8_t id1, id2; |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 170 | |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 171 | chip_writeb(RESET, bios); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 172 | |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 173 | chip_writeb(READ_ID, bios); |
| 174 | id1 = chip_readb(bios); |
| 175 | id2 = chip_readb(bios + 0x01); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 176 | |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 177 | chip_writeb(RESET, bios); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 178 | |
Uwe Hermann | 04aa59a | 2009-09-02 22:09:00 +0000 | [diff] [blame] | 179 | printf_debug("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2); |
Stefan Reinauer | ff4f197 | 2007-05-24 08:48:10 +0000 | [diff] [blame] | 180 | |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 181 | if (!(id1 == flash->manufacture_id && id2 == flash->model_id)) |
| 182 | return 0; |
| 183 | |
Stefan Reinauer | ff4f197 | 2007-05-24 08:48:10 +0000 | [diff] [blame] | 184 | map_flash_registers(flash); |
| 185 | |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 186 | return 1; |
| 187 | } |
| 188 | |
| 189 | int erase_49lfxxxc(struct flashchip *flash) |
| 190 | { |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 191 | chipaddr bios = flash->virtual_memory; |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 192 | int i; |
| 193 | unsigned int total_size = flash->total_size * 1024; |
| 194 | |
Carl-Daniel Hailfinger | 500dd9d | 2009-06-05 13:30:49 +0000 | [diff] [blame] | 195 | write_lockbits_49lfxxxc(flash, 0); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 196 | for (i = 0; i < total_size; i += flash->page_size) |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 197 | if (erase_sector_49lfxxxc(flash, i, flash->page_size)) |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 198 | return (-1); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 199 | |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 200 | chip_writeb(RESET, bios); |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 201 | |
| 202 | return 0; |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | int write_49lfxxxc(struct flashchip *flash, uint8_t *buf) |
| 206 | { |
| 207 | int i; |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 208 | int total_size = flash->total_size * 1024; |
| 209 | int page_size = flash->page_size; |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 210 | chipaddr bios = flash->virtual_memory; |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 211 | |
Carl-Daniel Hailfinger | 500dd9d | 2009-06-05 13:30:49 +0000 | [diff] [blame] | 212 | write_lockbits_49lfxxxc(flash, 0); |
Uwe Hermann | a502dce | 2007-10-17 23:55:15 +0000 | [diff] [blame] | 213 | printf("Programming page: "); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 214 | for (i = 0; i < total_size / page_size; i++) { |
| 215 | /* erase the page before programming */ |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 216 | if (erase_sector_49lfxxxc(flash, i * page_size, flash->page_size)) { |
| 217 | fprintf(stderr, "ERASE FAILED!\n"); |
| 218 | return -1; |
| 219 | } |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 220 | |
| 221 | /* write to the sector */ |
| 222 | printf("%04d at address: 0x%08x", i, i * page_size); |
| 223 | write_sector_49lfxxxc(bios, buf + i * page_size, |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 224 | bios + i * page_size, page_size); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 225 | printf("\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"); |
| 226 | } |
| 227 | printf("\n"); |
| 228 | |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 229 | chip_writeb(RESET, bios); |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 230 | |
| 231 | return 0; |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 232 | } |