blob: 1331cda6d8af964fa6d4ae9ab3ed98984861c76a [file] [log] [blame]
Yinghai Luca782972007-01-22 20:21:17 +00001/*
Uwe Hermannd1107642007-08-29 17:52:32 +00002 * This file is part of the flashrom project.
Yinghai Luca782972007-01-22 20:21:17 +00003 *
Uwe Hermannd22a1d42007-09-09 20:21:05 +00004 * Copyright (C) 2000 Silicon Integrated System Corporation
5 * Copyright (C) 2005-2007 coresystems GmbH
Sean Nelson51c83fb2010-01-20 20:55:53 +00006 * Copyright (C) 2009 Sean Nelson <audiohacked@gmail.com>
Yinghai Luca782972007-01-22 20:21:17 +00007 *
Uwe Hermannd1107642007-08-29 17:52:32 +00008 * 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 Luca782972007-01-22 20:21:17 +000012 *
Uwe Hermannd1107642007-08-29 17:52:32 +000013 * 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 Luca782972007-01-22 20:21:17 +000017 *
Uwe Hermannd1107642007-08-29 17:52:32 +000018 * 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 Luca782972007-01-22 20:21:17 +000021 */
22
Yinghai Luca782972007-01-22 20:21:17 +000023#include <stdlib.h>
Yinghai Luca782972007-01-22 20:21:17 +000024#include "flash.h"
Sean Nelson14ba6682010-02-26 05:48:29 +000025#include "chipdrivers.h"
Yinghai Luca782972007-01-22 20:21:17 +000026
Sean Nelson51c83fb2010-01-20 20:55:53 +000027int unlock_block_49lfxxxc(struct flashchip *flash, unsigned long address, unsigned char bits)
28{
29 unsigned long lock = flash->virtual_registers + address + 2;
Sean Nelsoned479d22010-03-24 23:14:32 +000030 msg_cdbg("lockbits at address=0x%08lx is 0x%01x\n", lock, chip_readb(lock));
Sean Nelson51c83fb2010-01-20 20:55:53 +000031 chip_writeb(bits, lock);
32
33 return 0;
34}
35
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +000036static int write_lockbits_49lfxxxc(struct flashchip *flash, unsigned char bits)
Yinghai Luca782972007-01-22 20:21:17 +000037{
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +000038 chipaddr registers = flash->virtual_registers;
39 int i, left = flash->total_size * 1024;
Yinghai Luca782972007-01-22 20:21:17 +000040 unsigned long address;
41
Sean Nelsoned479d22010-03-24 23:14:32 +000042 msg_cdbg("\nbios=0x%08lx\n", registers);
Yinghai Luca782972007-01-22 20:21:17 +000043 for (i = 0; left > 65536; i++, left -= 65536) {
Sean Nelsoned479d22010-03-24 23:14:32 +000044 msg_cdbg("lockbits at address=0x%08lx is 0x%01x\n",
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +000045 registers + (i * 65536) + 2,
46 chip_readb(registers + (i * 65536) + 2));
47 chip_writeb(bits, registers + (i * 65536) + 2);
Yinghai Luca782972007-01-22 20:21:17 +000048 }
49 address = i * 65536;
Sean Nelsoned479d22010-03-24 23:14:32 +000050 msg_cdbg("lockbits at address=0x%08lx is 0x%01x\n",
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +000051 registers + address + 2,
52 chip_readb(registers + address + 2));
53 chip_writeb(bits, registers + address + 2);
Yinghai Luca782972007-01-22 20:21:17 +000054 address += 32768;
Sean Nelsoned479d22010-03-24 23:14:32 +000055 msg_cdbg("lockbits at address=0x%08lx is 0x%01x\n",
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +000056 registers + address + 2,
57 chip_readb(registers + address + 2));
58 chip_writeb(bits, registers + address + 2);
Yinghai Luca782972007-01-22 20:21:17 +000059 address += 8192;
Sean Nelsoned479d22010-03-24 23:14:32 +000060 msg_cdbg("lockbits at address=0x%08lx is 0x%01x\n",
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +000061 registers + address + 2,
62 chip_readb(registers + address + 2));
63 chip_writeb(bits, registers + address + 2);
Yinghai Luca782972007-01-22 20:21:17 +000064 address += 8192;
Sean Nelsoned479d22010-03-24 23:14:32 +000065 msg_cdbg("lockbits at address=0x%08lx is 0x%01x\n",
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +000066 registers + address + 2,
67 chip_readb(registers + address + 2));
68 chip_writeb(bits, registers + address + 2);
Yinghai Luca782972007-01-22 20:21:17 +000069
Uwe Hermannffec5f32007-08-23 16:08:21 +000070 return 0;
Yinghai Luca782972007-01-22 20:21:17 +000071}
72
Sean Nelson6e0b9122010-02-19 00:52:10 +000073int unlock_49lfxxxc(struct flashchip *flash)
74{
75 return write_lockbits_49lfxxxc(flash, 0);
76}
77
Sean Nelson51c83fb2010-01-20 20:55:53 +000078int erase_sector_49lfxxxc(struct flashchip *flash, unsigned int address, unsigned int sector_size)
Yinghai Luca782972007-01-22 20:21:17 +000079{
Sean Nelson69e58112010-03-23 17:10:28 +000080 uint8_t status;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +000081 chipaddr bios = flash->virtual_memory;
Yinghai Luca782972007-01-22 20:21:17 +000082
Sean Nelson69e58112010-03-23 17:10:28 +000083 chip_writeb(0x30, bios);
84 chip_writeb(0xD0, bios + address);
Yinghai Luca782972007-01-22 20:21:17 +000085
Sean Nelson69e58112010-03-23 17:10:28 +000086 status = wait_82802ab(bios);
Yinghai Luca782972007-01-22 20:21:17 +000087
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +000088 if (check_erased_range(flash, address, sector_size)) {
Sean Nelsoned479d22010-03-24 23:14:32 +000089 msg_cerr("ERASE FAILED!\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +000090 return -1;
91 }
Uwe Hermannffec5f32007-08-23 16:08:21 +000092 return 0;
Yinghai Luca782972007-01-22 20:21:17 +000093}
94
Yinghai Luca782972007-01-22 20:21:17 +000095int write_49lfxxxc(struct flashchip *flash, uint8_t *buf)
96{
97 int i;
Uwe Hermanna7e05482007-05-09 10:17:44 +000098 int total_size = flash->total_size * 1024;
99 int page_size = flash->page_size;
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000100 chipaddr bios = flash->virtual_memory;
Yinghai Luca782972007-01-22 20:21:17 +0000101
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +0000102 write_lockbits_49lfxxxc(flash, 0);
Sean Nelsoned479d22010-03-24 23:14:32 +0000103 msg_cinfo("Programming page: ");
Yinghai Luca782972007-01-22 20:21:17 +0000104 for (i = 0; i < total_size / page_size; i++) {
105 /* erase the page before programming */
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000106 if (erase_sector_49lfxxxc(flash, i * page_size, flash->page_size)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000107 msg_cerr("ERASE FAILED!\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000108 return -1;
109 }
Yinghai Luca782972007-01-22 20:21:17 +0000110
111 /* write to the sector */
Sean Nelsoned479d22010-03-24 23:14:32 +0000112 msg_cinfo("%04d at address: 0x%08x", i, i * page_size);
Sean Nelson69e58112010-03-23 17:10:28 +0000113 write_page_82802ab(bios, buf + i * page_size,
Uwe Hermanna7e05482007-05-09 10:17:44 +0000114 bios + i * page_size, page_size);
Sean Nelsoned479d22010-03-24 23:14:32 +0000115 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 Luca782972007-01-22 20:21:17 +0000116 }
Sean Nelsoned479d22010-03-24 23:14:32 +0000117 msg_cinfo("\n");
Yinghai Luca782972007-01-22 20:21:17 +0000118
Sean Nelson69e58112010-03-23 17:10:28 +0000119 chip_writeb(0xFF, bios);
Uwe Hermannffec5f32007-08-23 16:08:21 +0000120
121 return 0;
Yinghai Luca782972007-01-22 20:21:17 +0000122}