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