sst28sf040.c 3.72 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(chipaddr bios, unsigned long address)
58
{
59 60
	chip_writeb(AUTO_PG_ERASE1, bios);
	chip_writeb(AUTO_PG_ERASE2, bios + address);
61 62 63

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

Uwe Hermann's avatar
Uwe Hermann committed
65
	return 0;
66 67
}

68 69
static int write_sector_28sf040(chipaddr bios, uint8_t *src, chipaddr dst,
				unsigned int page_size)
70 71 72 73 74 75 76 77 78 79 80
{
	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 */
81 82
		chip_writeb(AUTO_PGRM, dst);
		chip_writeb(*src++, dst++);
83 84 85 86

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

Uwe Hermann's avatar
Uwe Hermann committed
88
	return 0;
89 90
}

91
int probe_28sf040(struct flashchip *flash)
92
{
93
	chipaddr bios = flash->virtual_memory;
94
	uint8_t id1, id2;
95

96
	chip_writeb(RESET, bios);
Ronald G. Minnich's avatar
Fixes  
Ronald G. Minnich committed
97
	myusec_delay(10);
98

99
	chip_writeb(READ_ID, bios);
Ronald G. Minnich's avatar
Fixes  
Ronald G. Minnich committed
100
	myusec_delay(10);
101
	id1 = chip_readb(bios);
Ronald G. Minnich's avatar
Fixes  
Ronald G. Minnich committed
102
	myusec_delay(10);
103
	id2 = chip_readb(bios + 0x01);
104

105
	chip_writeb(RESET, bios);
Ronald G. Minnich's avatar
Fixes  
Ronald G. Minnich committed
106
	myusec_delay(10);
107

108
	printf_debug("%s: id1 0x%02x, id2 0x%02x\n", __FUNCTION__, id1, id2);
109 110 111 112 113 114
	if (id1 == flash->manufacture_id && id2 == flash->model_id)
		return 1;

	return 0;
}

115
int erase_28sf040(struct flashchip *flash)
116
{
117
	chipaddr bios = flash->virtual_memory;
118

119
	unprotect_28sf040(bios);
120 121
	chip_writeb(CHIP_ERASE, bios);
	chip_writeb(CHIP_ERASE, bios);
122
	protect_28sf040(bios);
123

Ronald G. Minnich's avatar
Fixes  
Ronald G. Minnich committed
124
	myusec_delay(10);
125
	toggle_ready_jedec(bios);
Ronald G. Minnich's avatar
Ronald G. Minnich committed
126

Uwe Hermann's avatar
Uwe Hermann committed
127
	return 0;
128 129
}

130
int write_28sf040(struct flashchip *flash, uint8_t *buf)
131 132
{
	int i;
133 134
	int total_size = flash->total_size * 1024;
	int page_size = flash->page_size;
135
	chipaddr bios = flash->virtual_memory;
136

137
	unprotect_28sf040(bios);
138

139
	printf("Programming page: ");
140
	for (i = 0; i < total_size / page_size; i++) {
141 142 143 144
		/* erase the page before programming */
		erase_sector_28sf040(bios, i * page_size);

		/* write to the sector */
145 146 147
		printf("%04d at address: 0x%08x", i, i * page_size);
		write_sector_28sf040(bios, buf + i * page_size,
				     bios + i * page_size, page_size);
148
		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");
149 150 151
	}
	printf("\n");

152
	protect_28sf040(bios);
Ronald G. Minnich's avatar
Ronald G. Minnich committed
153

Uwe Hermann's avatar
Uwe Hermann committed
154
	return 0;
155
}