jedec.c 5.41 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * jedec.c: driver for programming JEDEC standard flash parts
 *
 *
 * Copyright 2000 Silicon Integrated System Corporation
 *
 *	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.
 *
 *	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.
 *
 *	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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *
 * Reference:
 *
 */

Ronald G. Minnich's avatar
Ronald G. Minnich committed
26
#include <stdio.h>
27
#include <stdint.h>
28 29
#include "flash.h"
#include "jedec.h"
30
#include "debug.h"
31

32
int probe_jedec(struct flashchip *flash)
33
{
34 35
	volatile uint8_t *bios = flash->virt_addr;
	uint8_t id1, id2;
36 37

	/* Issue JEDEC Product ID Entry command */
38
	*(volatile uint8_t *) (bios + 0x5555) = 0xAA;
39
	myusec_delay(10);
40
	*(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
41
	myusec_delay(10);
42
	*(volatile uint8_t *) (bios + 0x5555) = 0x90;
43 44 45
	myusec_delay(10);

	/* Read product ID */
46 47
	id1 = *(volatile uint8_t *) bios;
	id2 = *(volatile uint8_t *) (bios + 0x01);
48 49

	/* Issue JEDEC Product ID Exit command */
50
	*(volatile uint8_t *) (bios + 0x5555) = 0xAA;
51
	myusec_delay(10);
52
	*(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
53
	myusec_delay(10);
54
	*(volatile uint8_t *) (bios + 0x5555) = 0xF0;
55
	myusec_delay(10);
56

57
	printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2);
58 59
	if (id1 == flash->manufacture_id && id2 == flash->model_id)
		return 1;
60

61
	return 0;
Ollie Lho's avatar
Ollie Lho committed
62
}
63

64
int erase_sector_jedec(volatile uint8_t *bios, unsigned int page)
Ollie Lho's avatar
Ollie Lho committed
65
{
66
	/*  Issue the Sector Erase command   */
67
	*(volatile uint8_t *) (bios + 0x5555) = 0xAA;
68
	myusec_delay(10);
69
	*(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
Ronald G. Minnich's avatar
Fixes  
Ronald G. Minnich committed
70
	myusec_delay(10);
71
	*(volatile uint8_t *) (bios + 0x5555) = 0x80;
Ollie Lho's avatar
Ollie Lho committed
72
	myusec_delay(10);
73

74
	*(volatile uint8_t *) (bios + 0x5555) = 0xAA;
Ollie Lho's avatar
Ollie Lho committed
75
	myusec_delay(10);
76
	*(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
Ollie Lho's avatar
Ollie Lho committed
77
	myusec_delay(10);
78
	*(volatile uint8_t *) (bios + page)   = 0x30;
Ollie Lho's avatar
Ollie Lho committed
79
	myusec_delay(10);
80 81 82 83 84 85

	/* wait for Toggle bit ready         */
	toggle_ready_jedec(bios);

	return (0);
}
86

87
int erase_block_jedec(volatile uint8_t *bios, unsigned int block)
88 89
{
	/*  Issue the Sector Erase command   */
90
	*(volatile uint8_t *) (bios + 0x5555) = 0xAA;
91
	myusec_delay(10);
92
	*(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
93
	myusec_delay(10);
94
	*(volatile uint8_t *) (bios + 0x5555) = 0x80;
95
	myusec_delay(10);
96

97
	*(volatile uint8_t *) (bios + 0x5555) = 0xAA;
98
	myusec_delay(10);
99
	*(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
100
	myusec_delay(10);
101
	*(volatile uint8_t *) (bios + block)  = 0x50;
102
	myusec_delay(10);
103

Ollie Lho's avatar
Ollie Lho committed
104 105
	/* wait for Toggle bit ready         */
	toggle_ready_jedec(bios);
106

107
	return (0);
108 109
}

110
int erase_chip_jedec(struct flashchip *flash)
111
{
112
	volatile uint8_t *bios = flash->virt_addr;
113

114
	/*  Issue the JEDEC Chip Erase command   */
115
	*(volatile uint8_t *) (bios + 0x5555) = 0xAA;
Ollie Lho's avatar
Ollie Lho committed
116
	myusec_delay(10);
117
	*(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
Ronald G. Minnich's avatar
Fixes  
Ronald G. Minnich committed
118
	myusec_delay(10);
119
	*(volatile uint8_t *) (bios + 0x5555) = 0x80;
Ollie Lho's avatar
Ollie Lho committed
120
	myusec_delay(10);
121

122
	*(volatile uint8_t *) (bios + 0x5555) = 0xAA;
Ollie Lho's avatar
Ollie Lho committed
123
	myusec_delay(10);
124
	*(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
Ollie Lho's avatar
Ollie Lho committed
125
	myusec_delay(10);
126
	*(volatile uint8_t *) (bios + 0x5555) = 0x10;
Ollie Lho's avatar
Ollie Lho committed
127 128
	myusec_delay(10);

129
	toggle_ready_jedec(bios);
Ronald G. Minnich's avatar
Ronald G. Minnich committed
130

131 132 133
	return (0);
}

134 135
int write_page_write_jedec(volatile uint8_t *bios, uint8_t *src,
			   volatile uint8_t *dst, int page_size)
136 137 138 139
{
	int i;

	/* Issue JEDEC Data Unprotect comand */
140 141 142
	*(volatile uint8_t *) (bios + 0x5555) = 0xAA;
	*(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
	*(volatile uint8_t *) (bios + 0x5555) = 0xA0;
143

144
	/* transfer data from source to destination */
145
	for (i = 0; i < page_size; i++) {
146 147 148
		/* If the data is 0xFF, don't program it */
		if (*src == 0xFF)
			continue;
149 150 151 152
		*dst++ = *src++;
	}

	toggle_ready_jedec(dst - 1);
153 154

	return 0;
155 156
}

157 158
int write_byte_program_jedec(volatile uint8_t *bios, uint8_t *src,
			     volatile uint8_t *dst)
Ollie Lho's avatar
Ollie Lho committed
159
{
160 161
	int tried = 0;

162
	/* If the data is 0xFF, don't program it */
Ollie Lho's avatar
Ollie Lho committed
163 164 165
	if (*src == 0xFF) {
		return 0;
	}
166

167
retry:
Ollie Lho's avatar
Ollie Lho committed
168
	/* Issue JEDEC Byte Program command */
169 170 171
	*(volatile uint8_t *) (bios + 0x5555) = 0xAA;
	*(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
	*(volatile uint8_t *) (bios + 0x5555) = 0xA0;
172 173

	/* transfer data from source to destination */
Ollie Lho's avatar
Ollie Lho committed
174 175
	*dst = *src;
	toggle_ready_jedec(bios);
176

177 178 179 180
	if (*dst != *src && tried++ < 0x10) {
 		goto retry;
 	}

Ollie Lho's avatar
Ollie Lho committed
181 182 183
	return 0;
}

184 185
int write_sector_jedec(volatile uint8_t *bios, uint8_t *src,
		       volatile uint8_t *dst, unsigned int page_size)
186 187 188 189
{
	int i;

	for (i = 0; i < page_size; i++) {
190 191
		write_byte_program_jedec(bios, src, dst);
		dst++, src++;
192 193 194
	}

	return (0);
195 196
}

197
int write_jedec(struct flashchip *flash, uint8_t *buf)
198 199
{
	int i;
Ollie Lho's avatar
Ollie Lho committed
200 201
	int total_size = flash->total_size * 1024;
	int page_size = flash->page_size;
202
	volatile uint8_t *bios = flash->virt_addr;
203

204
	erase_chip_jedec(flash);
205
	if (*bios != (uint8_t) 0xff) {
Ronald G. Minnich's avatar
Ronald G. Minnich committed
206 207 208
		printf("ERASE FAILED\n");
		return -1;
	}
209 210 211
	printf("Programming Page: ");
	for (i = 0; i < total_size / page_size; i++) {
		printf("%04d at address: 0x%08x", i, i * page_size);
212 213
		write_page_write_jedec(bios, buf + i * page_size,
				       bios + i * page_size, page_size);
Ollie Lho's avatar
Ollie Lho committed
214
		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");
215 216
	}
	printf("\n");
217
	protect_jedec(bios);
Ronald G. Minnich's avatar
Ronald G. Minnich committed
218

219
	return (0);
220
}