blob: bae1ddc439737c1d56a0c9446421123cc4c5fe82 [file] [log] [blame]
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +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
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000027#include <stdio.h>
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000028#include "flash.h"
29#include "jedec.h"
30
Ollie Lho761bf1b2004-03-20 16:46:10 +000031int probe_jedec(struct flashchip *flash)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000032{
Ollie Lho761bf1b2004-03-20 16:46:10 +000033 volatile char *bios = flash->virt_addr;
34 unsigned char id1, id2;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000035
Ollie Lho761bf1b2004-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);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000043
Ollie Lho761bf1b2004-03-20 16:46:10 +000044 /* Read product ID */
45 id1 = *(volatile unsigned char *) bios;
46 id2 = *(volatile unsigned char *) (bios + 0x01);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000047
Ollie Lho761bf1b2004-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);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000055
Ronald G. Minnichd4228fd2003-02-28 17:21:38 +000056 printf("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2);
Ollie Lho761bf1b2004-03-20 16:46:10 +000057 if (id1 == flash->manufacture_id && id2 == flash->model_id)
58 return 1;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000059
Ollie Lho761bf1b2004-03-20 16:46:10 +000060 return 0;
Ollie Lho73eca802004-03-19 22:10:07 +000061}
62
Ollie Lho761bf1b2004-03-20 16:46:10 +000063int erase_sector_jedec(volatile char *bios, unsigned int page)
Ollie Lho73eca802004-03-19 22:10:07 +000064{
Ollie Lho73eca802004-03-19 22:10:07 +000065 volatile unsigned char *Temp;
66
Ollie Lho761bf1b2004-03-20 16:46:10 +000067 /* Issue the Sector Erase command */
68 Temp = bios + 0x5555; /* set up address to be BASE:5555h */
69 *Temp = 0xAA; /* write data 0xAA to the address */
Ollie Lho73eca802004-03-19 22:10:07 +000070 myusec_delay(10);
Ollie Lho761bf1b2004-03-20 16:46:10 +000071 Temp = bios + 0x2AAA; /* set up address to be BASE:2AAAh */
72 *Temp = 0x55; /* write data 0x55 to the address */
Ollie Lho73eca802004-03-19 22:10:07 +000073 myusec_delay(10);
Ollie Lho761bf1b2004-03-20 16:46:10 +000074 Temp = bios + 0x5555; /* set up address to be BASE:5555h */
75 *Temp = 0x80; /* write data 0x80 to the address */
Ollie Lho73eca802004-03-19 22:10:07 +000076 myusec_delay(10);
Ollie Lho761bf1b2004-03-20 16:46:10 +000077 Temp = bios + 0x5555; /* set up address to be BASE:5555h */
78 *Temp = 0xAA; /* write data 0xAA to the address */
Ollie Lho73eca802004-03-19 22:10:07 +000079 myusec_delay(10);
Ollie Lho761bf1b2004-03-20 16:46:10 +000080 Temp = bios + 0x2AAA; /* set up address to be BASE:2AAAh */
81 *Temp = 0x55; /* write data 0x55 to the address */
Ollie Lho73eca802004-03-19 22:10:07 +000082 myusec_delay(10);
Ollie Lho761bf1b2004-03-20 16:46:10 +000083 Temp = bios + page; /* set up address to be the current sector */
84 *Temp = 0x30; /* write data 0x30 to the address */
85 myusec_delay(10);
86
Ollie Lho73eca802004-03-19 22:10:07 +000087 /* wait for Toggle bit ready */
88 toggle_ready_jedec(bios);
89
Ollie Lho761bf1b2004-03-20 16:46:10 +000090 return (0);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000091}
92
Ollie Lho761bf1b2004-03-20 16:46:10 +000093int erase_chip_jedec(struct flashchip *flash)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000094{
Ollie Lho761bf1b2004-03-20 16:46:10 +000095 volatile unsigned char *bios = flash->virt_addr;
Ollie Lho73eca802004-03-19 22:10:07 +000096 volatile unsigned char *Temp;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000097
Ollie Lho761bf1b2004-03-20 16:46:10 +000098 /* Issue the JEDEC Chip Erase command */
99 Temp = bios + 0x5555; /* set up address to be BASE:5555h */
100 *Temp = 0xAA; /* write data 0xAA to the address */
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000101 myusec_delay(10);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000102 Temp = bios + 0x2AAA; /* set up address to be BASE:2AAAh */
103 *Temp = 0x55; /* write data 0x55 to the address */
Ollie Lho73eca802004-03-19 22:10:07 +0000104 myusec_delay(10);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000105 Temp = bios + 0x5555; /* set up address to be BASE:5555h */
106 *Temp = 0x80; /* write data 0x80 to the address */
Ollie Lho73eca802004-03-19 22:10:07 +0000107 myusec_delay(10);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000108 Temp = bios + 0x5555; /* set up address to be BASE:5555h */
109 *Temp = 0xAA; /* write data 0xAA to the address */
Ollie Lho73eca802004-03-19 22:10:07 +0000110 myusec_delay(10);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000111 Temp = bios + 0x2AAA; /* set up address to be BASE:2AAAh */
112 *Temp = 0x55; /* write data 0x55 to the address */
Ollie Lho73eca802004-03-19 22:10:07 +0000113 myusec_delay(10);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000114 Temp = bios + 0x5555; /* set up address to be BASEy:5555h */
115 *Temp = 0x10; /* write data 0x10 to the address */
Ollie Lho73eca802004-03-19 22:10:07 +0000116 myusec_delay(10);
117
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000118 toggle_ready_jedec(bios);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000119
Ollie Lho761bf1b2004-03-20 16:46:10 +0000120 return (0);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000121}
122
Ollie Lho761bf1b2004-03-20 16:46:10 +0000123void write_page_jedec(volatile char *bios, char *src, volatile char *dst,
124 int page_size)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000125{
126 int i;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000127
Ollie Lho761bf1b2004-03-20 16:46:10 +0000128 /* Issue JEDEC Data Unprotect comand */
129 *(volatile char *) (bios + 0x5555) = 0xAA;
130 *(volatile char *) (bios + 0x2AAA) = 0x55;
131 *(volatile char *) (bios + 0x5555) = 0xA0;
132
133 for (i = 0; i < page_size; i++) {
134 /* transfer data from source to destination */
135 *dst++ = *src++;
136 }
137
138 usleep(100);
139 toggle_ready_jedec(dst - 1);
140}
141
142int write_sector_jedec(volatile char *bios,
143 unsigned char *src,
Ollie Lho3a3dea72004-03-20 17:39:43 +0000144 volatile unsigned char *dst,
145 unsigned int page_size)
Ollie Lho761bf1b2004-03-20 16:46:10 +0000146{
147 int i;
148 volatile char *Temp;
149
150 for (i = 0; i < page_size; i++) {
151 if (*dst != 0xff) {
Ollie Lho3a3dea72004-03-20 17:39:43 +0000152 printf("FATAL: dst %p not erased (val 0x%x)\n",
153 dst, *dst);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000154 return (-1);
155 }
156 /* transfer data from source to destination */
157 if (*src == 0xFF) {
158 dst++, src++;
159 /* If the data is 0xFF, don't program it */
160 continue;
161 }
162 /* Issue JEDEC Byte Program command */
163 Temp = bios + 0x5555;
164 *Temp = 0xAA;
165 Temp = bios + 0x2AAA;
166 *Temp = 0x55;
167 Temp = bios + 0x5555;
168 *Temp = 0xA0;
169 *dst = *src;
170 toggle_ready_jedec(bios);
171 if (*dst != *src)
172 printf("BAD! dst 0x%lx val 0x%x src 0x%x\n",
173 (unsigned long) dst, *dst, *src);
174 dst++, src++;
175 }
176
177 return (0);
178}
179
180int write_jedec(struct flashchip *flash, unsigned char *buf)
181{
182 int i;
183 int total_size = flash->total_size * 1024, page_size =
184 flash->page_size;
185 volatile unsigned char *bios = flash->virt_addr;
186
187 erase_chip_jedec(flash);
188 if (*bios != (unsigned char) 0xff) {
Ronald G. Minnich56439422002-09-06 16:58:14 +0000189 printf("ERASE FAILED\n");
190 return -1;
191 }
Ollie Lho761bf1b2004-03-20 16:46:10 +0000192 printf("Programming Page: ");
193 for (i = 0; i < total_size / page_size; i++) {
194 printf("%04d at address: 0x%08x", i, i * page_size);
195 write_page_jedec(bios, buf + i * page_size,
196 bios + i * page_size, page_size);
197 printf
198 ("\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");
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000199 }
200 printf("\n");
Ollie Lho761bf1b2004-03-20 16:46:10 +0000201 protect_jedec(bios);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000202
Ollie Lho761bf1b2004-03-20 16:46:10 +0000203 return (0);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000204}