blob: d9dcd9bd33e9e0052fa7d98483ed1cea56afcfce [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
Uwe Hermann09e04f72009-05-16 22:36:00 +000041static int write_lockbits_49lfxxxc(chipaddr bios, int size,
42 unsigned char bits)
Yinghai Luca782972007-01-22 20:21:17 +000043{
44 int i, left = size;
45 unsigned long address;
46
47 //printf("bios=0x%08lx\n", (unsigned long)bios);
48 for (i = 0; left > 65536; i++, left -= 65536) {
49 //printf("lockbits at address=0x%08lx is 0x%01x\n", (unsigned long)0xFFC00000 - size + (i * 65536) + 2, *(bios + (i * 65536) + 2) );
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000050 chip_writeb(bits, bios + (i * 65536) + 2);
Yinghai Luca782972007-01-22 20:21:17 +000051 }
52 address = i * 65536;
Uwe Hermanna7e05482007-05-09 10:17:44 +000053 //printf("lockbits at address=0x%08lx is 0x%01x\n", (unsigned long)0xFFc00000 - size + address + 2, *(bios + address + 2) );
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000054 chip_writeb(bits, bios + address + 2);
Yinghai Luca782972007-01-22 20:21:17 +000055 address += 32768;
Uwe Hermanna7e05482007-05-09 10:17:44 +000056 //printf("lockbits at address=0x%08lx is 0x%01x\n", (unsigned long)0xFFc00000 - size + address + 2, *(bios + address + 2) );
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000057 chip_writeb(bits, bios + address + 2);
Yinghai Luca782972007-01-22 20:21:17 +000058 address += 8192;
Uwe Hermanna7e05482007-05-09 10:17:44 +000059 //printf("lockbits at address=0x%08lx is 0x%01x\n", (unsigned long)0xFFc00000 - size + address + 2, *(bios + address + 2) );
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000060 chip_writeb(bits, bios + address + 2);
Yinghai Luca782972007-01-22 20:21:17 +000061 address += 8192;
Uwe Hermanna7e05482007-05-09 10:17:44 +000062 //printf("lockbits at address=0x%08lx is 0x%01x\n", (unsigned long)0xFFc00000 - size + address + 2, *(bios + address + 2) );
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000063 chip_writeb(bits, bios + address + 2);
Yinghai Luca782972007-01-22 20:21:17 +000064
Uwe Hermannffec5f32007-08-23 16:08:21 +000065 return 0;
Yinghai Luca782972007-01-22 20:21:17 +000066}
67
Uwe Hermann09e04f72009-05-16 22:36:00 +000068static int erase_sector_49lfxxxc(chipaddr bios, unsigned long address)
Yinghai Luca782972007-01-22 20:21:17 +000069{
70 unsigned char status;
71
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000072 chip_writeb(SECTOR_ERASE, bios);
73 chip_writeb(ERASE, bios + address);
Yinghai Luca782972007-01-22 20:21:17 +000074
75 do {
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000076 status = chip_readb(bios);
Yinghai Luca782972007-01-22 20:21:17 +000077 if (status & (STATUS_ESS | STATUS_BPS)) {
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +000078 printf("sector erase FAILED at address=0x%08lx status=0x%01x\n", bios + address, status);
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000079 chip_writeb(CLEAR_STATUS, bios);
Uwe Hermanna7e05482007-05-09 10:17:44 +000080 return (-1);
Yinghai Luca782972007-01-22 20:21:17 +000081 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000082 } while (!(status & STATUS_WSMS));
Yinghai Luca782972007-01-22 20:21:17 +000083
Uwe Hermannffec5f32007-08-23 16:08:21 +000084 return 0;
Yinghai Luca782972007-01-22 20:21:17 +000085}
86
Uwe Hermann09e04f72009-05-16 22:36:00 +000087static int write_sector_49lfxxxc(chipaddr bios, uint8_t *src, chipaddr dst,
88 unsigned int page_size)
Yinghai Luca782972007-01-22 20:21:17 +000089{
90 int i;
91 unsigned char status;
92
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000093 chip_writeb(CLEAR_STATUS, bios);
Yinghai Luca782972007-01-22 20:21:17 +000094 for (i = 0; i < page_size; i++) {
95 /* transfer data from source to destination */
96 if (*src == 0xFF) {
97 dst++, src++;
98 /* If the data is 0xFF, don't program it */
99 continue;
100 }
101 /*issue AUTO PROGRAM command */
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000102 chip_writeb(AUTO_PGRM, bios);
103 chip_writeb(*src++, dst++);
Yinghai Luca782972007-01-22 20:21:17 +0000104
105 do {
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000106 status = chip_readb(bios);
Yinghai Luca782972007-01-22 20:21:17 +0000107 if (status & (STATUS_ESS | STATUS_BPS)) {
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000108 printf("sector write FAILED at address=0x%08lx status=0x%01x\n", dst, status);
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000109 chip_writeb(CLEAR_STATUS, bios);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000110 return (-1);
Yinghai Luca782972007-01-22 20:21:17 +0000111 }
112 } while (!(status & STATUS_WSMS));
113 }
114
Uwe Hermannffec5f32007-08-23 16:08:21 +0000115 return 0;
Yinghai Luca782972007-01-22 20:21:17 +0000116}
117
118int probe_49lfxxxc(struct flashchip *flash)
119{
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000120 chipaddr bios = flash->virtual_memory;
Stefan Reinauerce532972007-05-23 17:20:56 +0000121
Yinghai Luca782972007-01-22 20:21:17 +0000122 uint8_t id1, id2;
Yinghai Luca782972007-01-22 20:21:17 +0000123
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000124 chip_writeb(RESET, bios);
Yinghai Luca782972007-01-22 20:21:17 +0000125
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000126 chip_writeb(READ_ID, bios);
127 id1 = chip_readb(bios);
128 id2 = chip_readb(bios + 0x01);
Yinghai Luca782972007-01-22 20:21:17 +0000129
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000130 chip_writeb(RESET, bios);
Yinghai Luca782972007-01-22 20:21:17 +0000131
Peter Stuge5cafc332009-01-25 23:52:45 +0000132 printf_debug("%s: id1 0x%02x, id2 0x%02x\n", __FUNCTION__, id1, id2);
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000133
Yinghai Luca782972007-01-22 20:21:17 +0000134 if (!(id1 == flash->manufacture_id && id2 == flash->model_id))
135 return 0;
136
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000137 map_flash_registers(flash);
138
Yinghai Luca782972007-01-22 20:21:17 +0000139 return 1;
140}
141
142int erase_49lfxxxc(struct flashchip *flash)
143{
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000144 chipaddr bios = flash->virtual_memory;
145 chipaddr registers = flash->virtual_registers;
Yinghai Luca782972007-01-22 20:21:17 +0000146 int i;
147 unsigned int total_size = flash->total_size * 1024;
148
Stefan Reinauerce532972007-05-23 17:20:56 +0000149 write_lockbits_49lfxxxc(registers, total_size, 0);
Yinghai Luca782972007-01-22 20:21:17 +0000150 for (i = 0; i < total_size; i += flash->page_size)
Uwe Hermanna7e05482007-05-09 10:17:44 +0000151 if (erase_sector_49lfxxxc(bios, i) != 0)
152 return (-1);
Yinghai Luca782972007-01-22 20:21:17 +0000153
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000154 chip_writeb(RESET, bios);
Uwe Hermannffec5f32007-08-23 16:08:21 +0000155
156 return 0;
Yinghai Luca782972007-01-22 20:21:17 +0000157}
158
159int write_49lfxxxc(struct flashchip *flash, uint8_t *buf)
160{
161 int i;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000162 int total_size = flash->total_size * 1024;
163 int page_size = flash->page_size;
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000164 chipaddr bios = flash->virtual_memory;
Yinghai Luca782972007-01-22 20:21:17 +0000165
Stefan Reinauerce532972007-05-23 17:20:56 +0000166 write_lockbits_49lfxxxc(flash->virtual_registers, total_size, 0);
Uwe Hermanna502dce2007-10-17 23:55:15 +0000167 printf("Programming page: ");
Yinghai Luca782972007-01-22 20:21:17 +0000168 for (i = 0; i < total_size / page_size; i++) {
169 /* erase the page before programming */
170 erase_sector_49lfxxxc(bios, i * page_size);
171
172 /* write to the sector */
173 printf("%04d at address: 0x%08x", i, i * page_size);
174 write_sector_49lfxxxc(bios, buf + i * page_size,
Uwe Hermanna7e05482007-05-09 10:17:44 +0000175 bios + i * page_size, page_size);
Yinghai Luca782972007-01-22 20:21:17 +0000176 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");
177 }
178 printf("\n");
179
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000180 chip_writeb(RESET, bios);
Uwe Hermannffec5f32007-08-23 16:08:21 +0000181
182 return 0;
Yinghai Luca782972007-01-22 20:21:17 +0000183}