blob: 2875d649d226d9ca7b5b2a6f77a82691647303c5 [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
22#include <errno.h>
23#include <fcntl.h>
24#include <sys/mman.h>
Yinghai Luca782972007-01-22 20:21:17 +000025#include <stdlib.h>
Yinghai Luca782972007-01-22 20:21:17 +000026#include "flash.h"
Yinghai Luca782972007-01-22 20:21:17 +000027
28#define SECTOR_ERASE 0x30
29#define BLOCK_ERASE 0x20
30#define ERASE 0xD0
31#define AUTO_PGRM 0x10
32#define RESET 0xFF
33#define READ_ID 0x90
34#define READ_STATUS 0x70
35#define CLEAR_STATUS 0x50
36
37#define STATUS_BPS (1 << 1)
38#define STATUS_ESS (1 << 6)
39#define STATUS_WSMS (1 << 7)
40
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +000041static int write_lockbits_49lfxxxc(struct flashchip *flash, unsigned char bits)
Yinghai Luca782972007-01-22 20:21:17 +000042{
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +000043 chipaddr registers = flash->virtual_registers;
44 int i, left = flash->total_size * 1024;
Yinghai Luca782972007-01-22 20:21:17 +000045 unsigned long address;
46
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +000047 printf_debug("\nbios=0x%08lx\n", registers);
Yinghai Luca782972007-01-22 20:21:17 +000048 for (i = 0; left > 65536; i++, left -= 65536) {
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +000049 printf_debug("lockbits at address=0x%08lx is 0x%01x\n",
50 registers + (i * 65536) + 2,
51 chip_readb(registers + (i * 65536) + 2));
52 chip_writeb(bits, registers + (i * 65536) + 2);
Yinghai Luca782972007-01-22 20:21:17 +000053 }
54 address = i * 65536;
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +000055 printf_debug("lockbits at address=0x%08lx is 0x%01x\n",
56 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 += 32768;
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +000060 printf_debug("lockbits at address=0x%08lx is 0x%01x\n",
61 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;
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +000065 printf_debug("lockbits at address=0x%08lx is 0x%01x\n",
66 registers + address + 2,
67 chip_readb(registers + address + 2));
68 chip_writeb(bits, registers + address + 2);
Yinghai Luca782972007-01-22 20:21:17 +000069 address += 8192;
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +000070 printf_debug("lockbits at address=0x%08lx is 0x%01x\n",
71 registers + address + 2,
72 chip_readb(registers + address + 2));
73 chip_writeb(bits, registers + address + 2);
Yinghai Luca782972007-01-22 20:21:17 +000074
Uwe Hermannffec5f32007-08-23 16:08:21 +000075 return 0;
Yinghai Luca782972007-01-22 20:21:17 +000076}
77
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +000078static int erase_sector_49lfxxxc(struct flashchip *flash, unsigned long address, int sector_size)
Yinghai Luca782972007-01-22 20:21:17 +000079{
80 unsigned char status;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +000081 chipaddr bios = flash->virtual_memory;
Yinghai Luca782972007-01-22 20:21:17 +000082
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000083 chip_writeb(SECTOR_ERASE, bios);
84 chip_writeb(ERASE, bios + address);
Yinghai Luca782972007-01-22 20:21:17 +000085
86 do {
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000087 status = chip_readb(bios);
Yinghai Luca782972007-01-22 20:21:17 +000088 if (status & (STATUS_ESS | STATUS_BPS)) {
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +000089 printf("sector erase FAILED at address=0x%08lx status=0x%01x\n", bios + address, status);
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000090 chip_writeb(CLEAR_STATUS, bios);
Uwe Hermanna7e05482007-05-09 10:17:44 +000091 return (-1);
Yinghai Luca782972007-01-22 20:21:17 +000092 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000093 } while (!(status & STATUS_WSMS));
Yinghai Luca782972007-01-22 20:21:17 +000094
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +000095 if (check_erased_range(flash, address, sector_size)) {
96 fprintf(stderr, "ERASE FAILED!\n");
97 return -1;
98 }
Uwe Hermannffec5f32007-08-23 16:08:21 +000099 return 0;
Yinghai Luca782972007-01-22 20:21:17 +0000100}
101
Uwe Hermann09e04f72009-05-16 22:36:00 +0000102static int write_sector_49lfxxxc(chipaddr bios, uint8_t *src, chipaddr dst,
103 unsigned int page_size)
Yinghai Luca782972007-01-22 20:21:17 +0000104{
105 int i;
106 unsigned char status;
107
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000108 chip_writeb(CLEAR_STATUS, bios);
Yinghai Luca782972007-01-22 20:21:17 +0000109 for (i = 0; i < page_size; i++) {
110 /* transfer data from source to destination */
111 if (*src == 0xFF) {
112 dst++, src++;
113 /* If the data is 0xFF, don't program it */
114 continue;
115 }
116 /*issue AUTO PROGRAM command */
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000117 chip_writeb(AUTO_PGRM, bios);
118 chip_writeb(*src++, dst++);
Yinghai Luca782972007-01-22 20:21:17 +0000119
120 do {
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000121 status = chip_readb(bios);
Yinghai Luca782972007-01-22 20:21:17 +0000122 if (status & (STATUS_ESS | STATUS_BPS)) {
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000123 printf("sector write FAILED at address=0x%08lx status=0x%01x\n", dst, status);
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000124 chip_writeb(CLEAR_STATUS, bios);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000125 return (-1);
Yinghai Luca782972007-01-22 20:21:17 +0000126 }
127 } while (!(status & STATUS_WSMS));
128 }
129
Uwe Hermannffec5f32007-08-23 16:08:21 +0000130 return 0;
Yinghai Luca782972007-01-22 20:21:17 +0000131}
132
133int probe_49lfxxxc(struct flashchip *flash)
134{
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000135 chipaddr bios = flash->virtual_memory;
Yinghai Luca782972007-01-22 20:21:17 +0000136 uint8_t id1, id2;
Yinghai Luca782972007-01-22 20:21:17 +0000137
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000138 chip_writeb(RESET, bios);
Yinghai Luca782972007-01-22 20:21:17 +0000139
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000140 chip_writeb(READ_ID, bios);
141 id1 = chip_readb(bios);
142 id2 = chip_readb(bios + 0x01);
Yinghai Luca782972007-01-22 20:21:17 +0000143
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000144 chip_writeb(RESET, bios);
Yinghai Luca782972007-01-22 20:21:17 +0000145
Peter Stuge5cafc332009-01-25 23:52:45 +0000146 printf_debug("%s: id1 0x%02x, id2 0x%02x\n", __FUNCTION__, id1, id2);
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000147
Yinghai Luca782972007-01-22 20:21:17 +0000148 if (!(id1 == flash->manufacture_id && id2 == flash->model_id))
149 return 0;
150
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000151 map_flash_registers(flash);
152
Yinghai Luca782972007-01-22 20:21:17 +0000153 return 1;
154}
155
156int erase_49lfxxxc(struct flashchip *flash)
157{
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000158 chipaddr bios = flash->virtual_memory;
Yinghai Luca782972007-01-22 20:21:17 +0000159 int i;
160 unsigned int total_size = flash->total_size * 1024;
161
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +0000162 write_lockbits_49lfxxxc(flash, 0);
Yinghai Luca782972007-01-22 20:21:17 +0000163 for (i = 0; i < total_size; i += flash->page_size)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000164 if (erase_sector_49lfxxxc(flash, i, flash->page_size))
Uwe Hermanna7e05482007-05-09 10:17:44 +0000165 return (-1);
Yinghai Luca782972007-01-22 20:21:17 +0000166
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000167 chip_writeb(RESET, bios);
Uwe Hermannffec5f32007-08-23 16:08:21 +0000168
169 return 0;
Yinghai Luca782972007-01-22 20:21:17 +0000170}
171
172int write_49lfxxxc(struct flashchip *flash, uint8_t *buf)
173{
174 int i;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000175 int total_size = flash->total_size * 1024;
176 int page_size = flash->page_size;
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000177 chipaddr bios = flash->virtual_memory;
Yinghai Luca782972007-01-22 20:21:17 +0000178
Carl-Daniel Hailfinger500dd9d2009-06-05 13:30:49 +0000179 write_lockbits_49lfxxxc(flash, 0);
Uwe Hermanna502dce2007-10-17 23:55:15 +0000180 printf("Programming page: ");
Yinghai Luca782972007-01-22 20:21:17 +0000181 for (i = 0; i < total_size / page_size; i++) {
182 /* erase the page before programming */
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000183 if (erase_sector_49lfxxxc(flash, i * page_size, flash->page_size)) {
184 fprintf(stderr, "ERASE FAILED!\n");
185 return -1;
186 }
Yinghai Luca782972007-01-22 20:21:17 +0000187
188 /* write to the sector */
189 printf("%04d at address: 0x%08x", i, i * page_size);
190 write_sector_49lfxxxc(bios, buf + i * page_size,
Uwe Hermanna7e05482007-05-09 10:17:44 +0000191 bios + i * page_size, page_size);
Yinghai Luca782972007-01-22 20:21:17 +0000192 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");
193 }
194 printf("\n");
195
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000196 chip_writeb(RESET, bios);
Uwe Hermannffec5f32007-08-23 16:08:21 +0000197
198 return 0;
Yinghai Luca782972007-01-22 20:21:17 +0000199}