jedec.c 6.97 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 26
/*
 * 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:
 *
 * $Id$
 */

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

31
int probe_jedec(struct flashchip *flash)
32
{
Ollie Lho's avatar
Ollie Lho committed
33
	volatile unsigned char *bios = flash->virt_addr;
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
	unsigned char id1, id2;

	/* Issue JEDEC Product ID Entry command */
	*(volatile char *) (bios + 0x5555) = 0xAA;
	myusec_delay(10);
	*(volatile char *) (bios + 0x2AAA) = 0x55;
	myusec_delay(10);
	*(volatile char *) (bios + 0x5555) = 0x90;
	myusec_delay(10);

	/* Read product ID */
	id1 = *(volatile unsigned char *) bios;
	id2 = *(volatile unsigned char *) (bios + 0x01);

	/* Issue JEDEC Product ID Exit command */
	*(volatile char *) (bios + 0x5555) = 0xAA;
	myusec_delay(10);
	*(volatile char *) (bios + 0x2AAA) = 0x55;
	myusec_delay(10);
	*(volatile char *) (bios + 0x5555) = 0xF0;
	myusec_delay(10);
55

Ollie Lho's avatar
Ollie Lho committed
56
	printf("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2);
57 58
	if (id1 == flash->manufacture_id && id2 == flash->model_id)
		return 1;
59

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

Ollie Lho's avatar
Ollie Lho committed
63
int erase_sector_jedec(volatile unsigned char *bios, unsigned int page)
Ollie Lho's avatar
Ollie Lho committed
64 65
{
	volatile unsigned char *Temp;
66

67 68 69 70 71 72
	/*  Issue the Sector Erase command   */
	Temp = bios + 0x5555;	/* set up address to be BASE:5555h      */
	*Temp = 0xAA;		/* write data 0xAA to the address       */
	myusec_delay(10);
	Temp = bios + 0x2AAA;	/* set up address to be BASE:2AAAh      */
	*Temp = 0x55;		/* write data 0x55 to the address       */
Ronald G. Minnich's avatar
Fixes  
Ronald G. Minnich committed
73
	myusec_delay(10);
74 75
	Temp = bios + 0x5555;	/* set up address to be BASE:5555h      */
	*Temp = 0x80;		/* write data 0x80 to the address       */
Ollie Lho's avatar
Ollie Lho committed
76
	myusec_delay(10);
77 78
	Temp = bios + 0x5555;	/* set up address to be BASE:5555h      */
	*Temp = 0xAA;		/* write data 0xAA to the address       */
Ollie Lho's avatar
Ollie Lho committed
79
	myusec_delay(10);
80 81
	Temp = bios + 0x2AAA;	/* set up address to be BASE:2AAAh      */
	*Temp = 0x55;		/* write data 0x55 to the address       */
Ollie Lho's avatar
Ollie Lho committed
82
	myusec_delay(10);
83 84
	Temp = bios + page;	/* set up address to be the current sector */
	*Temp = 0x30;		/* write data 0x30 to the address       */
Ollie Lho's avatar
Ollie Lho committed
85
	myusec_delay(10);
86 87 88 89 90 91

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

	return (0);
}
92

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
int erase_block_jedec(volatile unsigned char *bios, unsigned int block)
{
	volatile unsigned char *Temp;

	/*  Issue the Sector Erase command   */
	Temp = bios + 0x5555;	/* set up address to be BASE:5555h      */
	*Temp = 0xAA;		/* write data 0xAA to the address       */
	myusec_delay(10);
	Temp = bios + 0x2AAA;	/* set up address to be BASE:2AAAh      */
	*Temp = 0x55;		/* write data 0x55 to the address       */
	myusec_delay(10);
	Temp = bios + 0x5555;	/* set up address to be BASE:5555h      */
	*Temp = 0x80;		/* write data 0x80 to the address       */
	myusec_delay(10);
	Temp = bios + 0x5555;	/* set up address to be BASE:5555h      */
	*Temp = 0xAA;		/* write data 0xAA to the address       */
	myusec_delay(10);
	Temp = bios + 0x2AAA;	/* set up address to be BASE:2AAAh      */
	*Temp = 0x55;		/* write data 0x55 to the address       */
	myusec_delay(10);
	Temp = bios + block;	/* set up address to be the current sector */
	*Temp = 0x50;		/* write data 0x30 to the address       */
	myusec_delay(10);
116

Ollie Lho's avatar
Ollie Lho committed
117 118
	/* wait for Toggle bit ready         */
	toggle_ready_jedec(bios);
119

120
	return (0);
121 122
}

123
int erase_chip_jedec(struct flashchip *flash)
124
{
125
	volatile unsigned char *bios = flash->virt_addr;
Ollie Lho's avatar
Ollie Lho committed
126
	volatile unsigned char *Temp;
127

128 129 130
	/*  Issue the JEDEC Chip Erase command   */
	Temp = bios + 0x5555;	/* set up address to be BASE:5555h      */
	*Temp = 0xAA;		/* write data 0xAA to the address       */
Ollie Lho's avatar
Ollie Lho committed
131
	myusec_delay(10);
132 133
	Temp = bios + 0x2AAA;	/* set up address to be BASE:2AAAh      */
	*Temp = 0x55;		/* write data 0x55 to the address       */
Ronald G. Minnich's avatar
Fixes  
Ronald G. Minnich committed
134
	myusec_delay(10);
135 136
	Temp = bios + 0x5555;	/* set up address to be BASE:5555h      */
	*Temp = 0x80;		/* write data 0x80 to the address       */
Ollie Lho's avatar
Ollie Lho committed
137
	myusec_delay(10);
138 139
	Temp = bios + 0x5555;	/* set up address to be BASE:5555h      */
	*Temp = 0xAA;		/* write data 0xAA to the address       */
Ollie Lho's avatar
Ollie Lho committed
140
	myusec_delay(10);
141 142
	Temp = bios + 0x2AAA;	/* set up address to be BASE:2AAAh      */
	*Temp = 0x55;		/* write data 0x55 to the address       */
Ollie Lho's avatar
Ollie Lho committed
143
	myusec_delay(10);
144 145
	Temp = bios + 0x5555;	/* set up address to be BASEy:5555h     */
	*Temp = 0x10;		/* write data 0x10 to the address       */
Ollie Lho's avatar
Ollie Lho committed
146 147
	myusec_delay(10);

148
	toggle_ready_jedec(bios);
Ronald G. Minnich's avatar
Ronald G. Minnich committed
149

150 151 152
	return (0);
}

153 154
int write_page_write_jedec(volatile unsigned char *bios, unsigned char *src,
			   volatile unsigned char *dst, int page_size)
155 156 157 158
{
	int i;

	/* Issue JEDEC Data Unprotect comand */
159 160 161
	*(volatile unsigned char *) (bios + 0x5555) = 0xAA;
	*(volatile unsigned char *) (bios + 0x2AAA) = 0x55;
	*(volatile unsigned char *) (bios + 0x5555) = 0xA0;
162

163
	/* transfer data from source to destination */
164
	for (i = 0; i < page_size; i++) {
165 166 167
		/* If the data is 0xFF, don't program it */
		if (*src == 0xFF)
			continue;
168 169 170 171
		*dst++ = *src++;
	}

	toggle_ready_jedec(dst - 1);
172 173

	return 0;
174 175
}

176
int write_byte_program_jedec(volatile unsigned char *bios, unsigned char *src,
Ollie Lho's avatar
Ollie Lho committed
177 178
			     volatile unsigned char *dst)
{
179 180
	int tried = 0;

181
	/* If the data is 0xFF, don't program it */
Ollie Lho's avatar
Ollie Lho committed
182 183 184
	if (*src == 0xFF) {
		return 0;
	}
185

186
retry:
Ollie Lho's avatar
Ollie Lho committed
187
	/* Issue JEDEC Byte Program command */
188 189 190 191 192
	*(volatile unsigned char *) (bios + 0x5555) = 0xAA;
	*(volatile unsigned char *) (bios + 0x2AAA) = 0x55;
	*(volatile unsigned char *) (bios + 0x5555) = 0xA0;

	/* transfer data from source to destination */
Ollie Lho's avatar
Ollie Lho committed
193 194
	*dst = *src;
	toggle_ready_jedec(bios);
195

196 197 198 199
	if (*dst != *src && tried++ < 0x10) {
 		goto retry;
 	}

Ollie Lho's avatar
Ollie Lho committed
200 201 202 203
	return 0;
}

int write_sector_jedec(volatile unsigned char *bios, unsigned char *src,
Ollie Lho's avatar
Ollie Lho committed
204
		       volatile unsigned char *dst, unsigned int page_size)
205 206 207 208
{
	int i;

	for (i = 0; i < page_size; i++) {
209 210
		write_byte_program_jedec(bios, src, dst);
		dst++, src++;
211 212 213
	}

	return (0);
214 215
}

216
int write_jedec(struct flashchip *flash, unsigned char *buf)
217 218
{
	int i;
Ollie Lho's avatar
Ollie Lho committed
219 220
	int total_size = flash->total_size * 1024;
	int page_size = flash->page_size;
221
	volatile unsigned char *bios = flash->virt_addr;
222

223 224
	erase_chip_jedec(flash);
	if (*bios != (unsigned char) 0xff) {
Ronald G. Minnich's avatar
Ronald G. Minnich committed
225 226 227
		printf("ERASE FAILED\n");
		return -1;
	}
228 229 230
	printf("Programming Page: ");
	for (i = 0; i < total_size / page_size; i++) {
		printf("%04d at address: 0x%08x", i, i * page_size);
231 232
		write_page_write_jedec(bios, buf + i * page_size,
				       bios + i * page_size, page_size);
Ollie Lho's avatar
Ollie Lho committed
233
		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");
234 235
	}
	printf("\n");
236
	protect_jedec(bios);
Ronald G. Minnich's avatar
Ronald G. Minnich committed
237

238
	return (0);
239
}