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 |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 6 | * |
Uwe Hermann | d110764 | 2007-08-29 17:52:32 +0000 | [diff] [blame] | 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. |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 11 | * |
Uwe Hermann | d110764 | 2007-08-29 17:52:32 +0000 | [diff] [blame] | 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. |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 16 | * |
Uwe Hermann | d110764 | 2007-08-29 17:52:32 +0000 | [diff] [blame] | 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 |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include <errno.h> |
| 23 | #include <fcntl.h> |
| 24 | #include <sys/mman.h> |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 25 | #include <stdlib.h> |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 26 | #include "flash.h" |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 27 | |
| 28 | #define SECTOR_ERASE 0x30 |
| 29 | #define BLOCK_ERASE 0x20 |
| 30 | #define ERASE 0xD0 |
| 31 | #define AUTO_PGRM 0x10 |
| 32 | #define RESET 0xFF |
| 33 | #define READ_ID 0x90 |
| 34 | #define READ_STATUS 0x70 |
| 35 | #define CLEAR_STATUS 0x50 |
| 36 | |
| 37 | #define STATUS_BPS (1 << 1) |
| 38 | #define STATUS_ESS (1 << 6) |
| 39 | #define STATUS_WSMS (1 << 7) |
| 40 | |
Carl-Daniel Hailfinger | 500dd9d | 2009-06-05 13:30:49 +0000 | [diff] [blame] | 41 | static int write_lockbits_49lfxxxc(struct flashchip *flash, unsigned char bits) |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 42 | { |
Carl-Daniel Hailfinger | 500dd9d | 2009-06-05 13:30:49 +0000 | [diff] [blame] | 43 | chipaddr registers = flash->virtual_registers; |
| 44 | int i, left = flash->total_size * 1024; |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 45 | unsigned long address; |
| 46 | |
Carl-Daniel Hailfinger | 500dd9d | 2009-06-05 13:30:49 +0000 | [diff] [blame] | 47 | printf_debug("\nbios=0x%08lx\n", registers); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 48 | for (i = 0; left > 65536; i++, left -= 65536) { |
Carl-Daniel Hailfinger | 500dd9d | 2009-06-05 13:30:49 +0000 | [diff] [blame] | 49 | printf_debug("lockbits at address=0x%08lx is 0x%01x\n", |
| 50 | registers + (i * 65536) + 2, |
| 51 | chip_readb(registers + (i * 65536) + 2)); |
| 52 | chip_writeb(bits, registers + (i * 65536) + 2); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 53 | } |
| 54 | address = i * 65536; |
Carl-Daniel Hailfinger | 500dd9d | 2009-06-05 13:30:49 +0000 | [diff] [blame] | 55 | printf_debug("lockbits at address=0x%08lx is 0x%01x\n", |
| 56 | registers + address + 2, |
| 57 | chip_readb(registers + address + 2)); |
| 58 | chip_writeb(bits, registers + address + 2); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 59 | address += 32768; |
Carl-Daniel Hailfinger | 500dd9d | 2009-06-05 13:30:49 +0000 | [diff] [blame] | 60 | printf_debug("lockbits at address=0x%08lx is 0x%01x\n", |
| 61 | registers + address + 2, |
| 62 | chip_readb(registers + address + 2)); |
| 63 | chip_writeb(bits, registers + address + 2); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 64 | address += 8192; |
Carl-Daniel Hailfinger | 500dd9d | 2009-06-05 13:30:49 +0000 | [diff] [blame] | 65 | printf_debug("lockbits at address=0x%08lx is 0x%01x\n", |
| 66 | registers + address + 2, |
| 67 | chip_readb(registers + address + 2)); |
| 68 | chip_writeb(bits, registers + address + 2); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 69 | address += 8192; |
Carl-Daniel Hailfinger | 500dd9d | 2009-06-05 13:30:49 +0000 | [diff] [blame] | 70 | printf_debug("lockbits at address=0x%08lx is 0x%01x\n", |
| 71 | registers + address + 2, |
| 72 | chip_readb(registers + address + 2)); |
| 73 | chip_writeb(bits, registers + address + 2); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 74 | |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 75 | return 0; |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 78 | static int erase_sector_49lfxxxc(struct flashchip *flash, unsigned long address, int sector_size) |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 79 | { |
| 80 | unsigned char status; |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 81 | chipaddr bios = flash->virtual_memory; |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 82 | |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 83 | chip_writeb(SECTOR_ERASE, bios); |
| 84 | chip_writeb(ERASE, bios + address); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 85 | |
| 86 | do { |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 87 | status = chip_readb(bios); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 88 | if (status & (STATUS_ESS | STATUS_BPS)) { |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 89 | 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] | 90 | chip_writeb(CLEAR_STATUS, bios); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 91 | return (-1); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 92 | } |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 93 | } while (!(status & STATUS_WSMS)); |
Carl-Daniel Hailfinger | 322f305 | 2009-07-20 15:21:18 +0000 | [diff] [blame^] | 94 | chip_writeb(RESET, bios); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 95 | |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 96 | if (check_erased_range(flash, address, sector_size)) { |
| 97 | fprintf(stderr, "ERASE FAILED!\n"); |
| 98 | return -1; |
| 99 | } |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 100 | return 0; |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 103 | static int write_sector_49lfxxxc(chipaddr bios, uint8_t *src, chipaddr dst, |
| 104 | unsigned int page_size) |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 105 | { |
| 106 | int i; |
| 107 | unsigned char status; |
| 108 | |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 109 | chip_writeb(CLEAR_STATUS, bios); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 110 | for (i = 0; i < page_size; i++) { |
| 111 | /* transfer data from source to destination */ |
| 112 | if (*src == 0xFF) { |
| 113 | dst++, src++; |
| 114 | /* If the data is 0xFF, don't program it */ |
| 115 | continue; |
| 116 | } |
| 117 | /*issue AUTO PROGRAM command */ |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 118 | chip_writeb(AUTO_PGRM, bios); |
| 119 | chip_writeb(*src++, dst++); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 120 | |
| 121 | do { |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 122 | status = chip_readb(bios); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 123 | if (status & (STATUS_ESS | STATUS_BPS)) { |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 124 | 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] | 125 | chip_writeb(CLEAR_STATUS, bios); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 126 | return (-1); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 127 | } |
| 128 | } while (!(status & STATUS_WSMS)); |
| 129 | } |
| 130 | |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 131 | return 0; |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | int probe_49lfxxxc(struct flashchip *flash) |
| 135 | { |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 136 | chipaddr bios = flash->virtual_memory; |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 137 | uint8_t id1, id2; |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 138 | |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 139 | chip_writeb(RESET, bios); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 140 | |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 141 | chip_writeb(READ_ID, bios); |
| 142 | id1 = chip_readb(bios); |
| 143 | id2 = chip_readb(bios + 0x01); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 144 | |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 145 | chip_writeb(RESET, bios); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 146 | |
Peter Stuge | 5cafc33 | 2009-01-25 23:52:45 +0000 | [diff] [blame] | 147 | printf_debug("%s: id1 0x%02x, id2 0x%02x\n", __FUNCTION__, id1, id2); |
Stefan Reinauer | ff4f197 | 2007-05-24 08:48:10 +0000 | [diff] [blame] | 148 | |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 149 | if (!(id1 == flash->manufacture_id && id2 == flash->model_id)) |
| 150 | return 0; |
| 151 | |
Stefan Reinauer | ff4f197 | 2007-05-24 08:48:10 +0000 | [diff] [blame] | 152 | map_flash_registers(flash); |
| 153 | |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 154 | return 1; |
| 155 | } |
| 156 | |
| 157 | int erase_49lfxxxc(struct flashchip *flash) |
| 158 | { |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 159 | chipaddr bios = flash->virtual_memory; |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 160 | int i; |
| 161 | unsigned int total_size = flash->total_size * 1024; |
| 162 | |
Carl-Daniel Hailfinger | 500dd9d | 2009-06-05 13:30:49 +0000 | [diff] [blame] | 163 | write_lockbits_49lfxxxc(flash, 0); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 164 | for (i = 0; i < total_size; i += flash->page_size) |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 165 | if (erase_sector_49lfxxxc(flash, i, flash->page_size)) |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 166 | return (-1); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 167 | |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 168 | chip_writeb(RESET, bios); |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 169 | |
| 170 | return 0; |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | int write_49lfxxxc(struct flashchip *flash, uint8_t *buf) |
| 174 | { |
| 175 | int i; |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 176 | int total_size = flash->total_size * 1024; |
| 177 | int page_size = flash->page_size; |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 178 | chipaddr bios = flash->virtual_memory; |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 179 | |
Carl-Daniel Hailfinger | 500dd9d | 2009-06-05 13:30:49 +0000 | [diff] [blame] | 180 | write_lockbits_49lfxxxc(flash, 0); |
Uwe Hermann | a502dce | 2007-10-17 23:55:15 +0000 | [diff] [blame] | 181 | printf("Programming page: "); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 182 | for (i = 0; i < total_size / page_size; i++) { |
| 183 | /* erase the page before programming */ |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 184 | if (erase_sector_49lfxxxc(flash, i * page_size, flash->page_size)) { |
| 185 | fprintf(stderr, "ERASE FAILED!\n"); |
| 186 | return -1; |
| 187 | } |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 188 | |
| 189 | /* write to the sector */ |
| 190 | printf("%04d at address: 0x%08x", i, i * page_size); |
| 191 | write_sector_49lfxxxc(bios, buf + i * page_size, |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 192 | bios + i * page_size, page_size); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 193 | 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"); |
| 194 | } |
| 195 | printf("\n"); |
| 196 | |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 197 | chip_writeb(RESET, bios); |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 198 | |
| 199 | return 0; |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 200 | } |