sst28sf040.c 4.1 KB
Newer Older
1
/*
2
 * This file is part of the flashrom project.
3
 *
4 5
 * Copyright (C) 2000 Silicon Integrated System Corporation
 * Copyright (C) 2005 coresystems GmbH <stepan@openbios.org>
6
 *
7 8 9 10
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
11
 *
12 13 14 15
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
16
 *
17 18 19
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20 21 22 23 24 25
 */

#include "flash.h"

#define AUTO_PG_ERASE1		0x20
#define AUTO_PG_ERASE2		0xD0
26
#define AUTO_PGRM		0x10
27 28 29 30
#define CHIP_ERASE		0x30
#define RESET			0xFF
#define READ_ID			0x90

31
static void protect_28sf040(chipaddr bios)
32
{
33
	uint8_t tmp;
34

35 36 37 38 39 40 41
	tmp = chip_readb(bios + 0x1823);
	tmp = chip_readb(bios + 0x1820);
	tmp = chip_readb(bios + 0x1822);
	tmp = chip_readb(bios + 0x0418);
	tmp = chip_readb(bios + 0x041B);
	tmp = chip_readb(bios + 0x0419);
	tmp = chip_readb(bios + 0x040A);
42 43
}

44
static void unprotect_28sf040(chipaddr bios)
45
{
46
	uint8_t tmp;
47

48 49 50 51 52 53 54
	tmp = chip_readb(bios + 0x1823);
	tmp = chip_readb(bios + 0x1820);
	tmp = chip_readb(bios + 0x1822);
	tmp = chip_readb(bios + 0x0418);
	tmp = chip_readb(bios + 0x041B);
	tmp = chip_readb(bios + 0x0419);
	tmp = chip_readb(bios + 0x041A);
55 56
}

57
static int erase_sector_28sf040(struct flashchip *flash, unsigned long address, int sector_size)
58
{
59 60
	chipaddr bios = flash->virtual_memory;

61 62
	chip_writeb(AUTO_PG_ERASE1, bios);
	chip_writeb(AUTO_PG_ERASE2, bios + address);
63 64 65

	/* wait for Toggle bit ready         */
	toggle_ready_jedec(bios);
Ronald G. Minnich's avatar
Ronald G. Minnich committed
66

67 68 69 70
	if (check_erased_range(flash, address, sector_size)) {
		fprintf(stderr, "ERASE FAILED!\n");
		return -1;
	}
Uwe Hermann's avatar
Uwe Hermann committed
71
	return 0;
72 73
}

74 75
static int write_sector_28sf040(chipaddr bios, uint8_t *src, chipaddr dst,
				unsigned int page_size)
76 77 78 79 80 81 82 83 84 85 86
{
	int i;

	for (i = 0; i < page_size; i++) {
		/* transfer data from source to destination */
		if (*src == 0xFF) {
			dst++, src++;
			/* If the data is 0xFF, don't program it */
			continue;
		}
		/*issue AUTO PROGRAM command */
87 88
		chip_writeb(AUTO_PGRM, dst);
		chip_writeb(*src++, dst++);
89 90 91 92

		/* wait for Toggle bit ready */
		toggle_ready_jedec(bios);
	}
Ronald G. Minnich's avatar
Ronald G. Minnich committed
93

Uwe Hermann's avatar
Uwe Hermann committed
94
	return 0;
95 96
}

97
int probe_28sf040(struct flashchip *flash)
98
{
99
	chipaddr bios = flash->virtual_memory;
100
	uint8_t id1, id2;
101

102
	chip_writeb(RESET, bios);
103
	programmer_delay(10);
104

105
	chip_writeb(READ_ID, bios);
106
	programmer_delay(10);
107
	id1 = chip_readb(bios);
108
	programmer_delay(10);
109
	id2 = chip_readb(bios + 0x01);
110

111
	chip_writeb(RESET, bios);
112
	programmer_delay(10);
113

114
	printf_debug("%s: id1 0x%02x, id2 0x%02x\n", __FUNCTION__, id1, id2);
115 116 117 118 119 120
	if (id1 == flash->manufacture_id && id2 == flash->model_id)
		return 1;

	return 0;
}

121
int erase_28sf040(struct flashchip *flash)
122
{
123
	chipaddr bios = flash->virtual_memory;
124

125
	unprotect_28sf040(bios);
126 127
	chip_writeb(CHIP_ERASE, bios);
	chip_writeb(CHIP_ERASE, bios);
128
	protect_28sf040(bios);
129

130
	programmer_delay(10);
131
	toggle_ready_jedec(bios);
Ronald G. Minnich's avatar
Ronald G. Minnich committed
132

133 134 135 136
	if (check_erased_range(flash, 0, flash->total_size * 1024)) {
		fprintf(stderr, "ERASE FAILED!\n");
		return -1;
	}
Uwe Hermann's avatar
Uwe Hermann committed
137
	return 0;
138 139
}

140
int write_28sf040(struct flashchip *flash, uint8_t *buf)
141 142
{
	int i;
143 144
	int total_size = flash->total_size * 1024;
	int page_size = flash->page_size;
145
	chipaddr bios = flash->virtual_memory;
146

147
	unprotect_28sf040(bios);
148

149
	printf("Programming page: ");
150
	for (i = 0; i < total_size / page_size; i++) {
151
		/* erase the page before programming */
152 153 154 155
		if (erase_sector_28sf040(flash, i * page_size, page_size)) {
			fprintf(stderr, "ERASE FAILED!\n");
			return -1;
		}
156 157

		/* write to the sector */
158 159 160
		printf("%04d at address: 0x%08x", i, i * page_size);
		write_sector_28sf040(bios, buf + i * page_size,
				     bios + i * page_size, page_size);
161
		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");
162 163 164
	}
	printf("\n");

165
	protect_28sf040(bios);
Ronald G. Minnich's avatar
Ronald G. Minnich committed
166

Uwe Hermann's avatar
Uwe Hermann committed
167
	return 0;
168
}