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" |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 25 | #include "chipdrivers.h" |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 26 | |
Sean Nelson | 51c83fb | 2010-01-20 20:55:53 +0000 | [diff] [blame] | 27 | int unlock_block_49lfxxxc(struct flashchip *flash, unsigned long address, unsigned char bits) |
| 28 | { |
| 29 | unsigned long lock = flash->virtual_registers + address + 2; |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 30 | msg_cdbg("lockbits at address=0x%08lx is 0x%01x\n", lock, chip_readb(lock)); |
Sean Nelson | 51c83fb | 2010-01-20 20:55:53 +0000 | [diff] [blame] | 31 | chip_writeb(bits, lock); |
| 32 | |
| 33 | return 0; |
| 34 | } |
| 35 | |
Carl-Daniel Hailfinger | 500dd9d | 2009-06-05 13:30:49 +0000 | [diff] [blame] | 36 | static int write_lockbits_49lfxxxc(struct flashchip *flash, unsigned char bits) |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 37 | { |
Carl-Daniel Hailfinger | 500dd9d | 2009-06-05 13:30:49 +0000 | [diff] [blame] | 38 | chipaddr registers = flash->virtual_registers; |
| 39 | int i, left = flash->total_size * 1024; |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 40 | unsigned long address; |
| 41 | |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 42 | msg_cdbg("\nbios=0x%08lx\n", registers); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 43 | for (i = 0; left > 65536; i++, left -= 65536) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 44 | msg_cdbg("lockbits at address=0x%08lx is 0x%01x\n", |
Carl-Daniel Hailfinger | 500dd9d | 2009-06-05 13:30:49 +0000 | [diff] [blame] | 45 | registers + (i * 65536) + 2, |
| 46 | chip_readb(registers + (i * 65536) + 2)); |
| 47 | chip_writeb(bits, registers + (i * 65536) + 2); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 48 | } |
| 49 | address = i * 65536; |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 50 | msg_cdbg("lockbits at address=0x%08lx is 0x%01x\n", |
Carl-Daniel Hailfinger | 500dd9d | 2009-06-05 13:30:49 +0000 | [diff] [blame] | 51 | registers + address + 2, |
| 52 | chip_readb(registers + address + 2)); |
| 53 | chip_writeb(bits, registers + address + 2); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 54 | address += 32768; |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 55 | msg_cdbg("lockbits at address=0x%08lx is 0x%01x\n", |
Carl-Daniel Hailfinger | 500dd9d | 2009-06-05 13:30:49 +0000 | [diff] [blame] | 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 += 8192; |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 60 | msg_cdbg("lockbits at address=0x%08lx is 0x%01x\n", |
Carl-Daniel Hailfinger | 500dd9d | 2009-06-05 13:30:49 +0000 | [diff] [blame] | 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; |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 65 | msg_cdbg("lockbits at address=0x%08lx is 0x%01x\n", |
Carl-Daniel Hailfinger | 500dd9d | 2009-06-05 13:30:49 +0000 | [diff] [blame] | 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 | |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 70 | return 0; |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Sean Nelson | 6e0b912 | 2010-02-19 00:52:10 +0000 | [diff] [blame] | 73 | int unlock_49lfxxxc(struct flashchip *flash) |
| 74 | { |
| 75 | return write_lockbits_49lfxxxc(flash, 0); |
| 76 | } |
| 77 | |
Sean Nelson | 51c83fb | 2010-01-20 20:55:53 +0000 | [diff] [blame] | 78 | 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] | 79 | { |
Sean Nelson | 69e5811 | 2010-03-23 17:10:28 +0000 | [diff] [blame] | 80 | uint8_t 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 | |
Sean Nelson | 69e5811 | 2010-03-23 17:10:28 +0000 | [diff] [blame] | 83 | chip_writeb(0x30, bios); |
| 84 | chip_writeb(0xD0, bios + address); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 85 | |
Sean Nelson | 69e5811 | 2010-03-23 17:10:28 +0000 | [diff] [blame] | 86 | status = wait_82802ab(bios); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 87 | |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 88 | if (check_erased_range(flash, address, sector_size)) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 89 | msg_cerr("ERASE FAILED!\n"); |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 90 | return -1; |
| 91 | } |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 92 | return 0; |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 95 | int write_49lfxxxc(struct flashchip *flash, uint8_t *buf) |
| 96 | { |
| 97 | int i; |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 98 | int total_size = flash->total_size * 1024; |
| 99 | int page_size = flash->page_size; |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 100 | chipaddr bios = flash->virtual_memory; |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 101 | |
Carl-Daniel Hailfinger | 500dd9d | 2009-06-05 13:30:49 +0000 | [diff] [blame] | 102 | write_lockbits_49lfxxxc(flash, 0); |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 103 | msg_cinfo("Programming page: "); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 104 | for (i = 0; i < total_size / page_size; i++) { |
| 105 | /* erase the page before programming */ |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 106 | if (erase_sector_49lfxxxc(flash, i * page_size, flash->page_size)) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 107 | msg_cerr("ERASE FAILED!\n"); |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 108 | return -1; |
| 109 | } |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 110 | |
| 111 | /* write to the sector */ |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 112 | msg_cinfo("%04d at address: 0x%08x", i, i * page_size); |
Sean Nelson | 69e5811 | 2010-03-23 17:10:28 +0000 | [diff] [blame] | 113 | write_page_82802ab(bios, buf + i * page_size, |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 114 | bios + i * page_size, page_size); |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 115 | msg_cinfo("\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"); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 116 | } |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 117 | msg_cinfo("\n"); |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 118 | |
Sean Nelson | 69e5811 | 2010-03-23 17:10:28 +0000 | [diff] [blame] | 119 | chip_writeb(0xFF, bios); |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 120 | |
| 121 | return 0; |
Yinghai Lu | ca78297 | 2007-01-22 20:21:17 +0000 | [diff] [blame] | 122 | } |