blob: a421acf5f4891841f27f5f4bb5aa20886ffbc958 [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
Yinghai Luca782972007-01-22 20:21:17 +00006 *
Uwe Hermannd1107642007-08-29 17:52:32 +00007 * 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 Luca782972007-01-22 20:21:17 +000011 *
Uwe Hermannd1107642007-08-29 17:52:32 +000012 * 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 Luca782972007-01-22 20:21:17 +000016 *
Uwe Hermannd1107642007-08-29 17:52:32 +000017 * 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 Luca782972007-01-22 20:21:17 +000020 */
21
Yinghai Luca782972007-01-22 20:21:17 +000022#include <stdlib.h>
Yinghai Luca782972007-01-22 20:21:17 +000023#include "flash.h"
Yinghai Luca782972007-01-22 20:21:17 +000024
25#define SECTOR_ERASE 0x30
26#define BLOCK_ERASE 0x20
27#define ERASE 0xD0
28#define AUTO_PGRM 0x10
29#define RESET 0xFF
30#define READ_ID 0x90
31#define READ_STATUS 0x70
32#define CLEAR_STATUS 0x50
33
34#define STATUS_BPS (1 << 1)
35#define STATUS_ESS (1 << 6)
36#define STATUS_WSMS (1 << 7)
37
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +000038static int write_lockbits_49lfxxxc(struct flashchip *flash, unsigned char bits)
Yinghai Luca782972007-01-22 20:21:17 +000039{
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +000040 chipaddr registers = flash->virtual_registers;
41 int i, left = flash->total_size * 1024;
Yinghai Luca782972007-01-22 20:21:17 +000042 unsigned long address;
43
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +000044 printf_debug("\nbios=0x%08lx\n", registers);
Yinghai Luca782972007-01-22 20:21:17 +000045 for (i = 0; left > 65536; i++, left -= 65536) {
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +000046 printf_debug("lockbits at address=0x%08lx is 0x%01x\n",
47 registers + (i * 65536) + 2,
48 chip_readb(registers + (i * 65536) + 2));
49 chip_writeb(bits, registers + (i * 65536) + 2);
Yinghai Luca782972007-01-22 20:21:17 +000050 }
51 address = i * 65536;
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +000052 printf_debug("lockbits at address=0x%08lx is 0x%01x\n",
53 registers + address + 2,
54 chip_readb(registers + address + 2));
55 chip_writeb(bits, registers + address + 2);
Yinghai Luca782972007-01-22 20:21:17 +000056 address += 32768;
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +000057 printf_debug("lockbits at address=0x%08lx is 0x%01x\n",
58 registers + address + 2,
59 chip_readb(registers + address + 2));
60 chip_writeb(bits, registers + address + 2);
Yinghai Luca782972007-01-22 20:21:17 +000061 address += 8192;
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 += 8192;
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
Uwe Hermannffec5f32007-08-23 16:08:21 +000072 return 0;
Yinghai Luca782972007-01-22 20:21:17 +000073}
74
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +000075static int erase_sector_49lfxxxc(struct flashchip *flash, unsigned long address, int sector_size)
Yinghai Luca782972007-01-22 20:21:17 +000076{
77 unsigned char status;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +000078 chipaddr bios = flash->virtual_memory;
Yinghai Luca782972007-01-22 20:21:17 +000079
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000080 chip_writeb(SECTOR_ERASE, bios);
81 chip_writeb(ERASE, bios + address);
Yinghai Luca782972007-01-22 20:21:17 +000082
83 do {
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000084 status = chip_readb(bios);
Yinghai Luca782972007-01-22 20:21:17 +000085 if (status & (STATUS_ESS | STATUS_BPS)) {
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +000086 printf("sector erase FAILED at address=0x%08lx status=0x%01x\n", bios + address, status);
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000087 chip_writeb(CLEAR_STATUS, bios);
Uwe Hermanna7e05482007-05-09 10:17:44 +000088 return (-1);
Yinghai Luca782972007-01-22 20:21:17 +000089 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000090 } while (!(status & STATUS_WSMS));
Carl-Daniel Hailfinger322f3052009-07-20 15:21:18 +000091 chip_writeb(RESET, bios);
Yinghai Luca782972007-01-22 20:21:17 +000092
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +000093 if (check_erased_range(flash, address, sector_size)) {
94 fprintf(stderr, "ERASE FAILED!\n");
95 return -1;
96 }
Uwe Hermannffec5f32007-08-23 16:08:21 +000097 return 0;
Yinghai Luca782972007-01-22 20:21:17 +000098}
99
Uwe Hermann09e04f72009-05-16 22:36:00 +0000100static int write_sector_49lfxxxc(chipaddr bios, uint8_t *src, chipaddr dst,
101 unsigned int page_size)
Yinghai Luca782972007-01-22 20:21:17 +0000102{
103 int i;
104 unsigned char status;
105
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000106 chip_writeb(CLEAR_STATUS, bios);
Yinghai Luca782972007-01-22 20:21:17 +0000107 for (i = 0; i < page_size; i++) {
108 /* transfer data from source to destination */
109 if (*src == 0xFF) {
110 dst++, src++;
111 /* If the data is 0xFF, don't program it */
112 continue;
113 }
114 /*issue AUTO PROGRAM command */
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000115 chip_writeb(AUTO_PGRM, bios);
116 chip_writeb(*src++, dst++);
Yinghai Luca782972007-01-22 20:21:17 +0000117
118 do {
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000119 status = chip_readb(bios);
Yinghai Luca782972007-01-22 20:21:17 +0000120 if (status & (STATUS_ESS | STATUS_BPS)) {
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000121 printf("sector write FAILED at address=0x%08lx status=0x%01x\n", dst, status);
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000122 chip_writeb(CLEAR_STATUS, bios);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000123 return (-1);
Yinghai Luca782972007-01-22 20:21:17 +0000124 }
125 } while (!(status & STATUS_WSMS));
126 }
127
Uwe Hermannffec5f32007-08-23 16:08:21 +0000128 return 0;
Yinghai Luca782972007-01-22 20:21:17 +0000129}
130
131int probe_49lfxxxc(struct flashchip *flash)
132{
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000133 chipaddr bios = flash->virtual_memory;
Yinghai Luca782972007-01-22 20:21:17 +0000134 uint8_t id1, id2;
Yinghai Luca782972007-01-22 20:21:17 +0000135
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000136 chip_writeb(RESET, bios);
Yinghai Luca782972007-01-22 20:21:17 +0000137
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000138 chip_writeb(READ_ID, bios);
139 id1 = chip_readb(bios);
140 id2 = chip_readb(bios + 0x01);
Yinghai Luca782972007-01-22 20:21:17 +0000141
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000142 chip_writeb(RESET, bios);
Yinghai Luca782972007-01-22 20:21:17 +0000143
Uwe Hermann04aa59a2009-09-02 22:09:00 +0000144 printf_debug("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2);
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000145
Yinghai Luca782972007-01-22 20:21:17 +0000146 if (!(id1 == flash->manufacture_id && id2 == flash->model_id))
147 return 0;
148
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000149 map_flash_registers(flash);
150
Yinghai Luca782972007-01-22 20:21:17 +0000151 return 1;
152}
153
154int erase_49lfxxxc(struct flashchip *flash)
155{
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000156 chipaddr bios = flash->virtual_memory;
Yinghai Luca782972007-01-22 20:21:17 +0000157 int i;
158 unsigned int total_size = flash->total_size * 1024;
159
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +0000160 write_lockbits_49lfxxxc(flash, 0);
Yinghai Luca782972007-01-22 20:21:17 +0000161 for (i = 0; i < total_size; i += flash->page_size)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000162 if (erase_sector_49lfxxxc(flash, i, flash->page_size))
Uwe Hermanna7e05482007-05-09 10:17:44 +0000163 return (-1);
Yinghai Luca782972007-01-22 20:21:17 +0000164
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000165 chip_writeb(RESET, bios);
Uwe Hermannffec5f32007-08-23 16:08:21 +0000166
167 return 0;
Yinghai Luca782972007-01-22 20:21:17 +0000168}
169
170int write_49lfxxxc(struct flashchip *flash, uint8_t *buf)
171{
172 int i;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000173 int total_size = flash->total_size * 1024;
174 int page_size = flash->page_size;
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000175 chipaddr bios = flash->virtual_memory;
Yinghai Luca782972007-01-22 20:21:17 +0000176
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +0000177 write_lockbits_49lfxxxc(flash, 0);
Uwe Hermanna502dce2007-10-17 23:55:15 +0000178 printf("Programming page: ");
Yinghai Luca782972007-01-22 20:21:17 +0000179 for (i = 0; i < total_size / page_size; i++) {
180 /* erase the page before programming */
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000181 if (erase_sector_49lfxxxc(flash, i * page_size, flash->page_size)) {
182 fprintf(stderr, "ERASE FAILED!\n");
183 return -1;
184 }
Yinghai Luca782972007-01-22 20:21:17 +0000185
186 /* write to the sector */
187 printf("%04d at address: 0x%08x", i, i * page_size);
188 write_sector_49lfxxxc(bios, buf + i * page_size,
Uwe Hermanna7e05482007-05-09 10:17:44 +0000189 bios + i * page_size, page_size);
Yinghai Luca782972007-01-22 20:21:17 +0000190 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");
191 }
192 printf("\n");
193
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000194 chip_writeb(RESET, bios);
Uwe Hermannffec5f32007-08-23 16:08:21 +0000195
196 return 0;
Yinghai Luca782972007-01-22 20:21:17 +0000197}