jedec.c 6.2 KB
Newer Older
1 2 3 4 5
/*
 * jedec.c: driver for programming JEDEC standard flash parts
 *
 *
 * Copyright 2000 Silicon Integrated System Corporation
6 7
 * Copyright 2006 Giampiero Giancipoli <gianci@email.it>
 * Copyright 2006 coresystems GmbH <info@coresystems.de>
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 *	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
28
#include <stdio.h>
29
#include <stdint.h>
30 31
#include "flash.h"
#include "jedec.h"
32
#include "debug.h"
33

34 35
#define MAX_REFLASH_TRIES 0x10

36
int probe_jedec(struct flashchip *flash)
37
{
38 39
	volatile uint8_t *bios = flash->virt_addr;
	uint8_t id1, id2;
40 41

	/* Issue JEDEC Product ID Entry command */
42
	*(volatile uint8_t *) (bios + 0x5555) = 0xAA;
43
	myusec_delay(10);
44
	*(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
45
	myusec_delay(10);
46
	*(volatile uint8_t *) (bios + 0x5555) = 0x90;
47 48 49
	myusec_delay(10);

	/* Read product ID */
50 51
	id1 = *(volatile uint8_t *) bios;
	id2 = *(volatile uint8_t *) (bios + 0x01);
52 53

	/* Issue JEDEC Product ID Exit command */
54
	*(volatile uint8_t *) (bios + 0x5555) = 0xAA;
55
	myusec_delay(10);
56
	*(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
57
	myusec_delay(10);
58
	*(volatile uint8_t *) (bios + 0x5555) = 0xF0;
59
	myusec_delay(10);
60

61
	printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2);
62 63
	if (id1 == flash->manufacture_id && id2 == flash->model_id)
		return 1;
64

65
	return 0;
Ollie Lho's avatar
Ollie Lho committed
66
}
67

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

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

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

	return (0);
}
90

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

101
	*(volatile uint8_t *) (bios + 0x5555) = 0xAA;
102
	myusec_delay(10);
103
	*(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
104
	myusec_delay(10);
105
	*(volatile uint8_t *) (bios + block)  = 0x50;
106
	myusec_delay(10);
107

Ollie Lho's avatar
Ollie Lho committed
108 109
	/* wait for Toggle bit ready         */
	toggle_ready_jedec(bios);
110

111
	return (0);
112 113
}

114
int erase_chip_jedec(struct flashchip *flash)
115
{
116
	volatile uint8_t *bios = flash->virt_addr;
117

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

126
	*(volatile uint8_t *) (bios + 0x5555) = 0xAA;
Ollie Lho's avatar
Ollie Lho committed
127
	myusec_delay(10);
128
	*(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
Ollie Lho's avatar
Ollie Lho committed
129
	myusec_delay(10);
130
	*(volatile uint8_t *) (bios + 0x5555) = 0x10;
Ollie Lho's avatar
Ollie Lho committed
131 132
	myusec_delay(10);

133
	toggle_ready_jedec(bios);
Ronald G. Minnich's avatar
Ronald G. Minnich committed
134

135 136 137
	return (0);
}

138 139
int write_page_write_jedec(volatile uint8_t *bios, uint8_t *src,
			   volatile uint8_t *dst, int page_size)
140
{
141 142 143
	int i, tried = 0, start_index = 0, ok;
	volatile uint8_t *d = dst;
	uint8_t *s = src;
144

145
retry:
146
	/* Issue JEDEC Data Unprotect comand */
147 148 149
	*(volatile uint8_t *) (bios + 0x5555) = 0xAA;
	*(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
	*(volatile uint8_t *) (bios + 0x5555) = 0xA0;
150

151
	/* transfer data from source to destination */
152
	for (i = start_index; i < page_size; i++) {
153
		/* If the data is 0xFF, don't program it */
154 155 156 157
		if (*src != 0xFF ) 
			*dst = *src;
		dst++;
		src++;
158 159 160
	}

	toggle_ready_jedec(dst - 1);
161

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
	dst = d;
	src = s;
	ok = 1;
	for (i = 0; i < page_size; i++) {
		if ( *dst != *src ) 
		{
			ok = 0;
			break;
		}
		dst++;
		src++;
	}
		
	if (!ok && tried++ < MAX_REFLASH_TRIES) {
		start_index = i;
 		goto retry;
 	}
	if (!ok) {
		fprintf( stderr, " page %d failed!\n", (unsigned int)(d-bios)/page_size );
	}
	return (!ok);
183 184
}

185 186
int write_byte_program_jedec(volatile uint8_t *bios, uint8_t *src,
			     volatile uint8_t *dst)
Ollie Lho's avatar
Ollie Lho committed
187
{
188
	int tried = 0, ok = 1;
189

190
	/* If the data is 0xFF, don't program it */
Ollie Lho's avatar
Ollie Lho committed
191
	if (*src == 0xFF) {
192
		return -1;
Ollie Lho's avatar
Ollie Lho committed
193
	}
194

195
retry:
Ollie Lho's avatar
Ollie Lho committed
196
	/* Issue JEDEC Byte Program command */
197 198 199
	*(volatile uint8_t *) (bios + 0x5555) = 0xAA;
	*(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
	*(volatile uint8_t *) (bios + 0x5555) = 0xA0;
200 201

	/* transfer data from source to destination */
Ollie Lho's avatar
Ollie Lho committed
202 203
	*dst = *src;
	toggle_ready_jedec(bios);
204

205
	if (*dst != *src && tried++ < MAX_REFLASH_TRIES) {
206 207 208
 		goto retry;
 	}

209 210 211 212
	if (tried >= MAX_REFLASH_TRIES)
		ok=0;

	return (!ok);
Ollie Lho's avatar
Ollie Lho committed
213 214
}

215 216
int write_sector_jedec(volatile uint8_t *bios, uint8_t *src,
		       volatile uint8_t *dst, unsigned int page_size)
217 218 219 220
{
	int i;

	for (i = 0; i < page_size; i++) {
221 222
		write_byte_program_jedec(bios, src, dst);
		dst++, src++;
223 224 225
	}

	return (0);
226 227
}

228
int write_jedec(struct flashchip *flash, uint8_t *buf)
229 230
{
	int i;
Ollie Lho's avatar
Ollie Lho committed
231 232
	int total_size = flash->total_size * 1024;
	int page_size = flash->page_size;
233
	volatile uint8_t *bios = flash->virt_addr;
234

235
	erase_chip_jedec(flash);
236 237 238 239 240 241 242 243
        // dumb check if erase was successful.
        for (i=0; i < total_size; i++) {
                if (bios[i] != (uint8_t)0xff) {
                        printf("ERASE FAILED\n");
                        return -1;
                }
        }

244 245 246
	printf("Programming Page: ");
	for (i = 0; i < total_size / page_size; i++) {
		printf("%04d at address: 0x%08x", i, i * page_size);
247 248
		write_page_write_jedec(bios, buf + i * page_size,
				       bios + i * page_size, page_size);
Ollie Lho's avatar
Ollie Lho committed
249
		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");
250 251
	}
	printf("\n");
252
	protect_jedec(bios);
Ronald G. Minnich's avatar
Ronald G. Minnich committed
253

254
	return (0);
255
}