blob: 353b17ab983bcbdda7296efb03e97e5cf943f64b [file] [log] [blame]
rminnich8d3ff912003-10-25 17:01:29 +00001/*
2 * jedec.c: driver for programming JEDEC standard flash parts
3 *
4 *
5 * Copyright 2000 Silicon Integrated System Corporation
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 *
22 * Reference:
23 *
24 * $Id$
25 */
26
27#include <stdio.h>
28#include "flash.h"
29#include "jedec.h"
30
ollie5b621572004-03-20 16:46:10 +000031int probe_jedec(struct flashchip *flash)
rminnich8d3ff912003-10-25 17:01:29 +000032{
ollied891a1c2004-03-27 00:31:03 +000033 volatile unsigned char *bios = flash->virt_addr;
ollie5b621572004-03-20 16:46:10 +000034 unsigned char id1, id2;
rminnich8d3ff912003-10-25 17:01:29 +000035
ollie5b621572004-03-20 16:46:10 +000036 /* Issue JEDEC Product ID Entry command */
37 *(volatile char *) (bios + 0x5555) = 0xAA;
38 myusec_delay(10);
39 *(volatile char *) (bios + 0x2AAA) = 0x55;
40 myusec_delay(10);
41 *(volatile char *) (bios + 0x5555) = 0x90;
42 myusec_delay(10);
rminnich8d3ff912003-10-25 17:01:29 +000043
ollie5b621572004-03-20 16:46:10 +000044 /* Read product ID */
45 id1 = *(volatile unsigned char *) bios;
46 id2 = *(volatile unsigned char *) (bios + 0x01);
rminnich8d3ff912003-10-25 17:01:29 +000047
ollie5b621572004-03-20 16:46:10 +000048 /* Issue JEDEC Product ID Exit command */
49 *(volatile char *) (bios + 0x5555) = 0xAA;
50 myusec_delay(10);
51 *(volatile char *) (bios + 0x2AAA) = 0x55;
52 myusec_delay(10);
53 *(volatile char *) (bios + 0x5555) = 0xF0;
54 myusec_delay(10);
rminnich8d3ff912003-10-25 17:01:29 +000055
56 printf("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2);
ollie5b621572004-03-20 16:46:10 +000057 if (id1 == flash->manufacture_id && id2 == flash->model_id)
58 return 1;
rminnich8d3ff912003-10-25 17:01:29 +000059
ollie5b621572004-03-20 16:46:10 +000060 return 0;
olliea3def632004-03-19 22:10:07 +000061}
62
ollied891a1c2004-03-27 00:31:03 +000063int erase_sector_jedec(volatile unsigned char *bios, unsigned int page)
olliea3def632004-03-19 22:10:07 +000064{
ollie5b621572004-03-20 16:46:10 +000065 /* Issue the Sector Erase command */
ollie0eb62d62004-12-08 20:10:01 +000066 *(volatile char *) (bios + 0x5555) = 0xAA;
olliea3def632004-03-19 22:10:07 +000067 myusec_delay(10);
ollie0eb62d62004-12-08 20:10:01 +000068 *(volatile char *) (bios + 0x2AAA) = 0x55;
olliea3def632004-03-19 22:10:07 +000069 myusec_delay(10);
ollie0eb62d62004-12-08 20:10:01 +000070 *(volatile char *) (bios + 0x5555) = 0x80;
olliea3def632004-03-19 22:10:07 +000071 myusec_delay(10);
ollie0eb62d62004-12-08 20:10:01 +000072
73 *(volatile char *) (bios + 0x5555) = 0xAA;
olliea3def632004-03-19 22:10:07 +000074 myusec_delay(10);
ollie0eb62d62004-12-08 20:10:01 +000075 *(volatile char *) (bios + 0x2AAA) = 0x55;
olliea3def632004-03-19 22:10:07 +000076 myusec_delay(10);
ollie0eb62d62004-12-08 20:10:01 +000077 *(volatile char *) (bios + page) = 0x30;
ollie5b621572004-03-20 16:46:10 +000078 myusec_delay(10);
79
olliea3def632004-03-19 22:10:07 +000080 /* wait for Toggle bit ready */
81 toggle_ready_jedec(bios);
82
ollie5b621572004-03-20 16:46:10 +000083 return (0);
rminnich8d3ff912003-10-25 17:01:29 +000084}
olliea4302802004-12-07 03:15:51 +000085
rminnichdfcbaa72004-09-30 16:37:01 +000086int erase_block_jedec(volatile unsigned char *bios, unsigned int block)
87{
rminnichdfcbaa72004-09-30 16:37:01 +000088 /* Issue the Sector Erase command */
ollie0eb62d62004-12-08 20:10:01 +000089 *(volatile char *) (bios + 0x5555) = 0xAA;
rminnichdfcbaa72004-09-30 16:37:01 +000090 myusec_delay(10);
ollie0eb62d62004-12-08 20:10:01 +000091 *(volatile char *) (bios + 0x2AAA) = 0x55;
rminnichdfcbaa72004-09-30 16:37:01 +000092 myusec_delay(10);
ollie0eb62d62004-12-08 20:10:01 +000093 *(volatile char *) (bios + 0x5555) = 0x80;
rminnichdfcbaa72004-09-30 16:37:01 +000094 myusec_delay(10);
ollie0eb62d62004-12-08 20:10:01 +000095
96 *(volatile char *) (bios + 0x5555) = 0xAA;
rminnichdfcbaa72004-09-30 16:37:01 +000097 myusec_delay(10);
ollie0eb62d62004-12-08 20:10:01 +000098 *(volatile char *) (bios + 0x2AAA) = 0x55;
rminnichdfcbaa72004-09-30 16:37:01 +000099 myusec_delay(10);
ollie0eb62d62004-12-08 20:10:01 +0000100 *(volatile char *) (bios + block) = 0x50;
rminnichdfcbaa72004-09-30 16:37:01 +0000101 myusec_delay(10);
102
103 /* wait for Toggle bit ready */
104 toggle_ready_jedec(bios);
105
106 return (0);
107}
rminnich8d3ff912003-10-25 17:01:29 +0000108
ollie5b621572004-03-20 16:46:10 +0000109int erase_chip_jedec(struct flashchip *flash)
rminnich8d3ff912003-10-25 17:01:29 +0000110{
ollie5b621572004-03-20 16:46:10 +0000111 volatile unsigned char *bios = flash->virt_addr;
rminnich8d3ff912003-10-25 17:01:29 +0000112
ollie5b621572004-03-20 16:46:10 +0000113 /* Issue the JEDEC Chip Erase command */
ollie0eb62d62004-12-08 20:10:01 +0000114 *(volatile char *) (bios + 0x5555) = 0xAA;
rminnich8d3ff912003-10-25 17:01:29 +0000115 myusec_delay(10);
ollie0eb62d62004-12-08 20:10:01 +0000116 *(volatile char *) (bios + 0x2AAA) = 0x55;
olliea3def632004-03-19 22:10:07 +0000117 myusec_delay(10);
ollie0eb62d62004-12-08 20:10:01 +0000118 *(volatile char *) (bios + 0x5555) = 0x80;
olliea3def632004-03-19 22:10:07 +0000119 myusec_delay(10);
ollie0eb62d62004-12-08 20:10:01 +0000120
121 *(volatile char *) (bios + 0x5555) = 0xAA;
olliea3def632004-03-19 22:10:07 +0000122 myusec_delay(10);
ollie0eb62d62004-12-08 20:10:01 +0000123 *(volatile char *) (bios + 0x2AAA) = 0x55;
olliea3def632004-03-19 22:10:07 +0000124 myusec_delay(10);
ollie0eb62d62004-12-08 20:10:01 +0000125 *(volatile char *) (bios + 0x5555) = 0x10;
olliea3def632004-03-19 22:10:07 +0000126 myusec_delay(10);
127
rminnich8d3ff912003-10-25 17:01:29 +0000128 toggle_ready_jedec(bios);
129
ollie5b621572004-03-20 16:46:10 +0000130 return (0);
rminnich8d3ff912003-10-25 17:01:29 +0000131}
132
olliea4302802004-12-07 03:15:51 +0000133int write_page_write_jedec(volatile unsigned char *bios, unsigned char *src,
134 volatile unsigned char *dst, int page_size)
rminnich8d3ff912003-10-25 17:01:29 +0000135{
136 int i;
rminnich8d3ff912003-10-25 17:01:29 +0000137
ollie5b621572004-03-20 16:46:10 +0000138 /* Issue JEDEC Data Unprotect comand */
olliea4302802004-12-07 03:15:51 +0000139 *(volatile unsigned char *) (bios + 0x5555) = 0xAA;
140 *(volatile unsigned char *) (bios + 0x2AAA) = 0x55;
141 *(volatile unsigned char *) (bios + 0x5555) = 0xA0;
ollie5b621572004-03-20 16:46:10 +0000142
olliea4302802004-12-07 03:15:51 +0000143 /* transfer data from source to destination */
ollie5b621572004-03-20 16:46:10 +0000144 for (i = 0; i < page_size; i++) {
olliea4302802004-12-07 03:15:51 +0000145 /* If the data is 0xFF, don't program it */
146 if (*src == 0xFF)
147 continue;
ollie5b621572004-03-20 16:46:10 +0000148 *dst++ = *src++;
149 }
150
ollie5b621572004-03-20 16:46:10 +0000151 toggle_ready_jedec(dst - 1);
olliea4302802004-12-07 03:15:51 +0000152
153 return 0;
ollie5b621572004-03-20 16:46:10 +0000154}
155
ollief1845bd2004-03-27 00:18:15 +0000156int write_byte_program_jedec(volatile unsigned char *bios, unsigned char *src,
olliebb5917a2004-03-22 22:19:17 +0000157 volatile unsigned char *dst)
158{
olliedd68ded2004-12-08 02:10:33 +0000159 int tried = 0;
160
olliea4302802004-12-07 03:15:51 +0000161 /* If the data is 0xFF, don't program it */
olliebb5917a2004-03-22 22:19:17 +0000162 if (*src == 0xFF) {
olliebb5917a2004-03-22 22:19:17 +0000163 return 0;
164 }
olliea4302802004-12-07 03:15:51 +0000165
olliedd68ded2004-12-08 02:10:33 +0000166retry:
olliebb5917a2004-03-22 22:19:17 +0000167 /* Issue JEDEC Byte Program command */
olliea4302802004-12-07 03:15:51 +0000168 *(volatile unsigned char *) (bios + 0x5555) = 0xAA;
169 *(volatile unsigned char *) (bios + 0x2AAA) = 0x55;
170 *(volatile unsigned char *) (bios + 0x5555) = 0xA0;
171
172 /* transfer data from source to destination */
olliebb5917a2004-03-22 22:19:17 +0000173 *dst = *src;
174 toggle_ready_jedec(bios);
ollief1845bd2004-03-27 00:18:15 +0000175
olliedd68ded2004-12-08 02:10:33 +0000176 if (*dst != *src && tried++ < 0x10) {
177 goto retry;
178 }
179
olliebb5917a2004-03-22 22:19:17 +0000180 return 0;
181}
182
183int write_sector_jedec(volatile unsigned char *bios, unsigned char *src,
ollied891a1c2004-03-27 00:31:03 +0000184 volatile unsigned char *dst, unsigned int page_size)
ollie5b621572004-03-20 16:46:10 +0000185{
186 int i;
ollie5b621572004-03-20 16:46:10 +0000187
188 for (i = 0; i < page_size; i++) {
ollief1845bd2004-03-27 00:18:15 +0000189 write_byte_program_jedec(bios, src, dst);
190 dst++, src++;
ollie5b621572004-03-20 16:46:10 +0000191 }
192
193 return (0);
194}
195
196int write_jedec(struct flashchip *flash, unsigned char *buf)
197{
198 int i;
olliebb5917a2004-03-22 22:19:17 +0000199 int total_size = flash->total_size * 1024;
200 int page_size = flash->page_size;
ollie5b621572004-03-20 16:46:10 +0000201 volatile unsigned char *bios = flash->virt_addr;
202
203 erase_chip_jedec(flash);
204 if (*bios != (unsigned char) 0xff) {
rminnich8d3ff912003-10-25 17:01:29 +0000205 printf("ERASE FAILED\n");
206 return -1;
207 }
ollie5b621572004-03-20 16:46:10 +0000208 printf("Programming Page: ");
209 for (i = 0; i < total_size / page_size; i++) {
210 printf("%04d at address: 0x%08x", i, i * page_size);
ollief1845bd2004-03-27 00:18:15 +0000211 write_page_write_jedec(bios, buf + i * page_size,
212 bios + i * page_size, page_size);
olliebb5917a2004-03-22 22:19:17 +0000213 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");
rminnich8d3ff912003-10-25 17:01:29 +0000214 }
215 printf("\n");
ollie5b621572004-03-20 16:46:10 +0000216 protect_jedec(bios);
rminnich8d3ff912003-10-25 17:01:29 +0000217
ollie5b621572004-03-20 16:46:10 +0000218 return (0);
rminnich8d3ff912003-10-25 17:01:29 +0000219}