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