blob: 9f9ee308c5848c48d05684fe901a360b382dd7e7 [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
27#define SECTOR_ERASE 0x30
28#define BLOCK_ERASE 0x20
29#define ERASE 0xD0
30#define AUTO_PGRM 0x10
31#define RESET 0xFF
32#define READ_ID 0x90
33#define READ_STATUS 0x70
34#define CLEAR_STATUS 0x50
35
36#define STATUS_BPS (1 << 1)
37#define STATUS_ESS (1 << 6)
38#define STATUS_WSMS (1 << 7)
39
Sean Nelson51c83fb2010-01-20 20:55:53 +000040int unlock_block_49lfxxxc(struct flashchip *flash, unsigned long address, unsigned char bits)
41{
42 unsigned long lock = flash->virtual_registers + address + 2;
43 printf_debug("lockbits at address=0x%08lx is 0x%01x\n", lock, chip_readb(lock));
44 chip_writeb(bits, lock);
45
46 return 0;
47}
48
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +000049static int write_lockbits_49lfxxxc(struct flashchip *flash, unsigned char bits)
Yinghai Luca782972007-01-22 20:21:17 +000050{
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +000051 chipaddr registers = flash->virtual_registers;
52 int i, left = flash->total_size * 1024;
Yinghai Luca782972007-01-22 20:21:17 +000053 unsigned long address;
54
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +000055 printf_debug("\nbios=0x%08lx\n", registers);
Yinghai Luca782972007-01-22 20:21:17 +000056 for (i = 0; left > 65536; i++, left -= 65536) {
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +000057 printf_debug("lockbits at address=0x%08lx is 0x%01x\n",
58 registers + (i * 65536) + 2,
59 chip_readb(registers + (i * 65536) + 2));
60 chip_writeb(bits, registers + (i * 65536) + 2);
Yinghai Luca782972007-01-22 20:21:17 +000061 }
62 address = i * 65536;
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +000063 printf_debug("lockbits at address=0x%08lx is 0x%01x\n",
64 registers + address + 2,
65 chip_readb(registers + address + 2));
66 chip_writeb(bits, registers + address + 2);
Yinghai Luca782972007-01-22 20:21:17 +000067 address += 32768;
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +000068 printf_debug("lockbits at address=0x%08lx is 0x%01x\n",
69 registers + address + 2,
70 chip_readb(registers + address + 2));
71 chip_writeb(bits, registers + address + 2);
Yinghai Luca782972007-01-22 20:21:17 +000072 address += 8192;
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +000073 printf_debug("lockbits at address=0x%08lx is 0x%01x\n",
74 registers + address + 2,
75 chip_readb(registers + address + 2));
76 chip_writeb(bits, registers + address + 2);
Yinghai Luca782972007-01-22 20:21:17 +000077 address += 8192;
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +000078 printf_debug("lockbits at address=0x%08lx is 0x%01x\n",
79 registers + address + 2,
80 chip_readb(registers + address + 2));
81 chip_writeb(bits, registers + address + 2);
Yinghai Luca782972007-01-22 20:21:17 +000082
Uwe Hermannffec5f32007-08-23 16:08:21 +000083 return 0;
Yinghai Luca782972007-01-22 20:21:17 +000084}
85
Sean Nelson6e0b9122010-02-19 00:52:10 +000086int unlock_49lfxxxc(struct flashchip *flash)
87{
88 return write_lockbits_49lfxxxc(flash, 0);
89}
90
Sean Nelson51c83fb2010-01-20 20:55:53 +000091int erase_sector_49lfxxxc(struct flashchip *flash, unsigned int address, unsigned int sector_size)
Yinghai Luca782972007-01-22 20:21:17 +000092{
93 unsigned char status;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +000094 chipaddr bios = flash->virtual_memory;
Yinghai Luca782972007-01-22 20:21:17 +000095
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000096 chip_writeb(SECTOR_ERASE, bios);
97 chip_writeb(ERASE, bios + address);
Yinghai Luca782972007-01-22 20:21:17 +000098
99 do {
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000100 status = chip_readb(bios);
Yinghai Luca782972007-01-22 20:21:17 +0000101 if (status & (STATUS_ESS | STATUS_BPS)) {
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000102 printf("sector erase FAILED at address=0x%08lx status=0x%01x\n", bios + address, status);
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000103 chip_writeb(CLEAR_STATUS, bios);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000104 return (-1);
Yinghai Luca782972007-01-22 20:21:17 +0000105 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000106 } while (!(status & STATUS_WSMS));
Carl-Daniel Hailfinger322f3052009-07-20 15:21:18 +0000107 chip_writeb(RESET, bios);
Yinghai Luca782972007-01-22 20:21:17 +0000108
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000109 if (check_erased_range(flash, address, sector_size)) {
110 fprintf(stderr, "ERASE FAILED!\n");
111 return -1;
112 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000113 return 0;
Yinghai Luca782972007-01-22 20:21:17 +0000114}
115
Sean Nelson51c83fb2010-01-20 20:55:53 +0000116int erase_block_49lfxxxc(struct flashchip *flash, unsigned int address, unsigned int block_size)
117{
118 unsigned char status;
119 chipaddr bios = flash->virtual_memory;
120
121 chip_writeb(BLOCK_ERASE, bios);
122 chip_writeb(ERASE, bios + address);
123
124 do {
125 status = chip_readb(bios);
126 if (status & (STATUS_ESS | STATUS_BPS)) {
127 printf("block erase FAILED at address=0x%08lx status=0x%01x\n", bios + address, status);
128 chip_writeb(CLEAR_STATUS, bios);
129 return (-1);
130 }
131 } while (!(status & STATUS_WSMS));
132 chip_writeb(RESET, bios);
133
134 if (check_erased_range(flash, address, block_size)) {
135 fprintf(stderr, "ERASE FAILED!\n");
136 return -1;
137 }
138 return 0;
139}
140
Uwe Hermann09e04f72009-05-16 22:36:00 +0000141static int write_sector_49lfxxxc(chipaddr bios, uint8_t *src, chipaddr dst,
142 unsigned int page_size)
Yinghai Luca782972007-01-22 20:21:17 +0000143{
144 int i;
145 unsigned char status;
146
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000147 chip_writeb(CLEAR_STATUS, bios);
Yinghai Luca782972007-01-22 20:21:17 +0000148 for (i = 0; i < page_size; i++) {
149 /* transfer data from source to destination */
150 if (*src == 0xFF) {
151 dst++, src++;
152 /* If the data is 0xFF, don't program it */
153 continue;
154 }
155 /*issue AUTO PROGRAM command */
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000156 chip_writeb(AUTO_PGRM, bios);
157 chip_writeb(*src++, dst++);
Yinghai Luca782972007-01-22 20:21:17 +0000158
159 do {
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000160 status = chip_readb(bios);
Yinghai Luca782972007-01-22 20:21:17 +0000161 if (status & (STATUS_ESS | STATUS_BPS)) {
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000162 printf("sector write FAILED at address=0x%08lx status=0x%01x\n", dst, status);
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000163 chip_writeb(CLEAR_STATUS, bios);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000164 return (-1);
Yinghai Luca782972007-01-22 20:21:17 +0000165 }
166 } while (!(status & STATUS_WSMS));
167 }
168
Uwe Hermannffec5f32007-08-23 16:08:21 +0000169 return 0;
Yinghai Luca782972007-01-22 20:21:17 +0000170}
171
172int probe_49lfxxxc(struct flashchip *flash)
173{
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000174 chipaddr bios = flash->virtual_memory;
Yinghai Luca782972007-01-22 20:21:17 +0000175 uint8_t id1, id2;
Yinghai Luca782972007-01-22 20:21:17 +0000176
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000177 chip_writeb(RESET, bios);
Yinghai Luca782972007-01-22 20:21:17 +0000178
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000179 chip_writeb(READ_ID, bios);
180 id1 = chip_readb(bios);
181 id2 = chip_readb(bios + 0x01);
Yinghai Luca782972007-01-22 20:21:17 +0000182
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000183 chip_writeb(RESET, bios);
Yinghai Luca782972007-01-22 20:21:17 +0000184
Uwe Hermann04aa59a2009-09-02 22:09:00 +0000185 printf_debug("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2);
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000186
Yinghai Luca782972007-01-22 20:21:17 +0000187 if (!(id1 == flash->manufacture_id && id2 == flash->model_id))
188 return 0;
189
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000190 map_flash_registers(flash);
191
Yinghai Luca782972007-01-22 20:21:17 +0000192 return 1;
193}
194
195int erase_49lfxxxc(struct flashchip *flash)
196{
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000197 chipaddr bios = flash->virtual_memory;
Yinghai Luca782972007-01-22 20:21:17 +0000198 int i;
199 unsigned int total_size = flash->total_size * 1024;
200
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +0000201 write_lockbits_49lfxxxc(flash, 0);
Yinghai Luca782972007-01-22 20:21:17 +0000202 for (i = 0; i < total_size; i += flash->page_size)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000203 if (erase_sector_49lfxxxc(flash, i, flash->page_size))
Uwe Hermanna7e05482007-05-09 10:17:44 +0000204 return (-1);
Yinghai Luca782972007-01-22 20:21:17 +0000205
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000206 chip_writeb(RESET, bios);
Uwe Hermannffec5f32007-08-23 16:08:21 +0000207
208 return 0;
Yinghai Luca782972007-01-22 20:21:17 +0000209}
210
211int write_49lfxxxc(struct flashchip *flash, uint8_t *buf)
212{
213 int i;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000214 int total_size = flash->total_size * 1024;
215 int page_size = flash->page_size;
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000216 chipaddr bios = flash->virtual_memory;
Yinghai Luca782972007-01-22 20:21:17 +0000217
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +0000218 write_lockbits_49lfxxxc(flash, 0);
Uwe Hermanna502dce2007-10-17 23:55:15 +0000219 printf("Programming page: ");
Yinghai Luca782972007-01-22 20:21:17 +0000220 for (i = 0; i < total_size / page_size; i++) {
221 /* erase the page before programming */
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000222 if (erase_sector_49lfxxxc(flash, i * page_size, flash->page_size)) {
223 fprintf(stderr, "ERASE FAILED!\n");
224 return -1;
225 }
Yinghai Luca782972007-01-22 20:21:17 +0000226
227 /* write to the sector */
228 printf("%04d at address: 0x%08x", i, i * page_size);
229 write_sector_49lfxxxc(bios, buf + i * page_size,
Uwe Hermanna7e05482007-05-09 10:17:44 +0000230 bios + i * page_size, page_size);
Yinghai Luca782972007-01-22 20:21:17 +0000231 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");
232 }
233 printf("\n");
234
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000235 chip_writeb(RESET, bios);
Uwe Hermannffec5f32007-08-23 16:08:21 +0000236
237 return 0;
Yinghai Luca782972007-01-22 20:21:17 +0000238}