blob: a65450e5ab65460893611b4ea8a77ea4e95948fe [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"
Yinghai Luca782972007-01-22 20:21:17 +000025
26#define SECTOR_ERASE 0x30
27#define BLOCK_ERASE 0x20
28#define ERASE 0xD0
29#define AUTO_PGRM 0x10
30#define RESET 0xFF
31#define READ_ID 0x90
32#define READ_STATUS 0x70
33#define CLEAR_STATUS 0x50
34
35#define STATUS_BPS (1 << 1)
36#define STATUS_ESS (1 << 6)
37#define STATUS_WSMS (1 << 7)
38
Sean Nelson51c83fb2010-01-20 20:55:53 +000039int unlock_block_49lfxxxc(struct flashchip *flash, unsigned long address, unsigned char bits)
40{
41 unsigned long lock = flash->virtual_registers + address + 2;
42 printf_debug("lockbits at address=0x%08lx is 0x%01x\n", lock, chip_readb(lock));
43 chip_writeb(bits, lock);
44
45 return 0;
46}
47
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +000048static int write_lockbits_49lfxxxc(struct flashchip *flash, unsigned char bits)
Yinghai Luca782972007-01-22 20:21:17 +000049{
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +000050 chipaddr registers = flash->virtual_registers;
51 int i, left = flash->total_size * 1024;
Yinghai Luca782972007-01-22 20:21:17 +000052 unsigned long address;
53
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +000054 printf_debug("\nbios=0x%08lx\n", registers);
Yinghai Luca782972007-01-22 20:21:17 +000055 for (i = 0; left > 65536; i++, left -= 65536) {
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +000056 printf_debug("lockbits at address=0x%08lx is 0x%01x\n",
57 registers + (i * 65536) + 2,
58 chip_readb(registers + (i * 65536) + 2));
59 chip_writeb(bits, registers + (i * 65536) + 2);
Yinghai Luca782972007-01-22 20:21:17 +000060 }
61 address = i * 65536;
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +000062 printf_debug("lockbits at address=0x%08lx is 0x%01x\n",
63 registers + address + 2,
64 chip_readb(registers + address + 2));
65 chip_writeb(bits, registers + address + 2);
Yinghai Luca782972007-01-22 20:21:17 +000066 address += 32768;
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +000067 printf_debug("lockbits at address=0x%08lx is 0x%01x\n",
68 registers + address + 2,
69 chip_readb(registers + address + 2));
70 chip_writeb(bits, registers + address + 2);
Yinghai Luca782972007-01-22 20:21:17 +000071 address += 8192;
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +000072 printf_debug("lockbits at address=0x%08lx is 0x%01x\n",
73 registers + address + 2,
74 chip_readb(registers + address + 2));
75 chip_writeb(bits, registers + address + 2);
Yinghai Luca782972007-01-22 20:21:17 +000076 address += 8192;
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +000077 printf_debug("lockbits at address=0x%08lx is 0x%01x\n",
78 registers + address + 2,
79 chip_readb(registers + address + 2));
80 chip_writeb(bits, registers + address + 2);
Yinghai Luca782972007-01-22 20:21:17 +000081
Uwe Hermannffec5f32007-08-23 16:08:21 +000082 return 0;
Yinghai Luca782972007-01-22 20:21:17 +000083}
84
Sean Nelson6e0b9122010-02-19 00:52:10 +000085int unlock_49lfxxxc(struct flashchip *flash)
86{
87 return write_lockbits_49lfxxxc(flash, 0);
88}
89
Sean Nelson51c83fb2010-01-20 20:55:53 +000090int erase_sector_49lfxxxc(struct flashchip *flash, unsigned int address, unsigned int sector_size)
Yinghai Luca782972007-01-22 20:21:17 +000091{
92 unsigned char status;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +000093 chipaddr bios = flash->virtual_memory;
Yinghai Luca782972007-01-22 20:21:17 +000094
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000095 chip_writeb(SECTOR_ERASE, bios);
96 chip_writeb(ERASE, bios + address);
Yinghai Luca782972007-01-22 20:21:17 +000097
98 do {
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000099 status = chip_readb(bios);
Yinghai Luca782972007-01-22 20:21:17 +0000100 if (status & (STATUS_ESS | STATUS_BPS)) {
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000101 printf("sector erase FAILED at address=0x%08lx status=0x%01x\n", bios + address, status);
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000102 chip_writeb(CLEAR_STATUS, bios);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000103 return (-1);
Yinghai Luca782972007-01-22 20:21:17 +0000104 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000105 } while (!(status & STATUS_WSMS));
Carl-Daniel Hailfinger322f3052009-07-20 15:21:18 +0000106 chip_writeb(RESET, bios);
Yinghai Luca782972007-01-22 20:21:17 +0000107
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000108 if (check_erased_range(flash, address, sector_size)) {
109 fprintf(stderr, "ERASE FAILED!\n");
110 return -1;
111 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000112 return 0;
Yinghai Luca782972007-01-22 20:21:17 +0000113}
114
Sean Nelson51c83fb2010-01-20 20:55:53 +0000115int erase_block_49lfxxxc(struct flashchip *flash, unsigned int address, unsigned int block_size)
116{
117 unsigned char status;
118 chipaddr bios = flash->virtual_memory;
119
120 chip_writeb(BLOCK_ERASE, bios);
121 chip_writeb(ERASE, bios + address);
122
123 do {
124 status = chip_readb(bios);
125 if (status & (STATUS_ESS | STATUS_BPS)) {
126 printf("block erase FAILED at address=0x%08lx status=0x%01x\n", bios + address, status);
127 chip_writeb(CLEAR_STATUS, bios);
128 return (-1);
129 }
130 } while (!(status & STATUS_WSMS));
131 chip_writeb(RESET, bios);
132
133 if (check_erased_range(flash, address, block_size)) {
134 fprintf(stderr, "ERASE FAILED!\n");
135 return -1;
136 }
137 return 0;
138}
139
Uwe Hermann09e04f72009-05-16 22:36:00 +0000140static int write_sector_49lfxxxc(chipaddr bios, uint8_t *src, chipaddr dst,
141 unsigned int page_size)
Yinghai Luca782972007-01-22 20:21:17 +0000142{
143 int i;
144 unsigned char status;
145
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000146 chip_writeb(CLEAR_STATUS, bios);
Yinghai Luca782972007-01-22 20:21:17 +0000147 for (i = 0; i < page_size; i++) {
148 /* transfer data from source to destination */
149 if (*src == 0xFF) {
150 dst++, src++;
151 /* If the data is 0xFF, don't program it */
152 continue;
153 }
154 /*issue AUTO PROGRAM command */
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000155 chip_writeb(AUTO_PGRM, bios);
156 chip_writeb(*src++, dst++);
Yinghai Luca782972007-01-22 20:21:17 +0000157
158 do {
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000159 status = chip_readb(bios);
Yinghai Luca782972007-01-22 20:21:17 +0000160 if (status & (STATUS_ESS | STATUS_BPS)) {
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000161 printf("sector write FAILED at address=0x%08lx status=0x%01x\n", dst, status);
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000162 chip_writeb(CLEAR_STATUS, bios);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000163 return (-1);
Yinghai Luca782972007-01-22 20:21:17 +0000164 }
165 } while (!(status & STATUS_WSMS));
166 }
167
Uwe Hermannffec5f32007-08-23 16:08:21 +0000168 return 0;
Yinghai Luca782972007-01-22 20:21:17 +0000169}
170
171int probe_49lfxxxc(struct flashchip *flash)
172{
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000173 chipaddr bios = flash->virtual_memory;
Yinghai Luca782972007-01-22 20:21:17 +0000174 uint8_t id1, id2;
Yinghai Luca782972007-01-22 20:21:17 +0000175
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000176 chip_writeb(RESET, bios);
Yinghai Luca782972007-01-22 20:21:17 +0000177
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000178 chip_writeb(READ_ID, bios);
179 id1 = chip_readb(bios);
180 id2 = chip_readb(bios + 0x01);
Yinghai Luca782972007-01-22 20:21:17 +0000181
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000182 chip_writeb(RESET, bios);
Yinghai Luca782972007-01-22 20:21:17 +0000183
Uwe Hermann04aa59a2009-09-02 22:09:00 +0000184 printf_debug("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2);
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000185
Yinghai Luca782972007-01-22 20:21:17 +0000186 if (!(id1 == flash->manufacture_id && id2 == flash->model_id))
187 return 0;
188
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000189 map_flash_registers(flash);
190
Yinghai Luca782972007-01-22 20:21:17 +0000191 return 1;
192}
193
194int erase_49lfxxxc(struct flashchip *flash)
195{
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000196 chipaddr bios = flash->virtual_memory;
Yinghai Luca782972007-01-22 20:21:17 +0000197 int i;
198 unsigned int total_size = flash->total_size * 1024;
199
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +0000200 write_lockbits_49lfxxxc(flash, 0);
Yinghai Luca782972007-01-22 20:21:17 +0000201 for (i = 0; i < total_size; i += flash->page_size)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000202 if (erase_sector_49lfxxxc(flash, i, flash->page_size))
Uwe Hermanna7e05482007-05-09 10:17:44 +0000203 return (-1);
Yinghai Luca782972007-01-22 20:21:17 +0000204
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000205 chip_writeb(RESET, bios);
Uwe Hermannffec5f32007-08-23 16:08:21 +0000206
207 return 0;
Yinghai Luca782972007-01-22 20:21:17 +0000208}
209
210int write_49lfxxxc(struct flashchip *flash, uint8_t *buf)
211{
212 int i;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000213 int total_size = flash->total_size * 1024;
214 int page_size = flash->page_size;
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000215 chipaddr bios = flash->virtual_memory;
Yinghai Luca782972007-01-22 20:21:17 +0000216
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +0000217 write_lockbits_49lfxxxc(flash, 0);
Uwe Hermanna502dce2007-10-17 23:55:15 +0000218 printf("Programming page: ");
Yinghai Luca782972007-01-22 20:21:17 +0000219 for (i = 0; i < total_size / page_size; i++) {
220 /* erase the page before programming */
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000221 if (erase_sector_49lfxxxc(flash, i * page_size, flash->page_size)) {
222 fprintf(stderr, "ERASE FAILED!\n");
223 return -1;
224 }
Yinghai Luca782972007-01-22 20:21:17 +0000225
226 /* write to the sector */
227 printf("%04d at address: 0x%08x", i, i * page_size);
228 write_sector_49lfxxxc(bios, buf + i * page_size,
Uwe Hermanna7e05482007-05-09 10:17:44 +0000229 bios + i * page_size, page_size);
Yinghai Luca782972007-01-22 20:21:17 +0000230 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");
231 }
232 printf("\n");
233
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000234 chip_writeb(RESET, bios);
Uwe Hermannffec5f32007-08-23 16:08:21 +0000235
236 return 0;
Yinghai Luca782972007-01-22 20:21:17 +0000237}