blob: 2a07b44518b397f555cd1cf522d332c4c735723e [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>
Ollie Lho184a4042005-11-26 21:55:36 +000028#include <stdint.h>
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000029#include "flash.h"
30#include "jedec.h"
Ollie Lho184a4042005-11-26 21:55:36 +000031#include "debug.h"
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000032
Ollie Lho761bf1b2004-03-20 16:46:10 +000033int probe_jedec(struct flashchip *flash)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000034{
Ollie Lho184a4042005-11-26 21:55:36 +000035 volatile uint8_t *bios = flash->virt_addr;
36 uint8_t id1, id2;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000037
Ollie Lho761bf1b2004-03-20 16:46:10 +000038 /* Issue JEDEC Product ID Entry command */
Ollie Lho184a4042005-11-26 21:55:36 +000039 *(volatile uint8_t *) (bios + 0x5555) = 0xAA;
Ollie Lho761bf1b2004-03-20 16:46:10 +000040 myusec_delay(10);
Ollie Lho184a4042005-11-26 21:55:36 +000041 *(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
Ollie Lho761bf1b2004-03-20 16:46:10 +000042 myusec_delay(10);
Ollie Lho184a4042005-11-26 21:55:36 +000043 *(volatile uint8_t *) (bios + 0x5555) = 0x90;
Ollie Lho761bf1b2004-03-20 16:46:10 +000044 myusec_delay(10);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000045
Ollie Lho761bf1b2004-03-20 16:46:10 +000046 /* Read product ID */
Ollie Lho184a4042005-11-26 21:55:36 +000047 id1 = *(volatile uint8_t *) bios;
48 id2 = *(volatile uint8_t *) (bios + 0x01);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000049
Ollie Lho761bf1b2004-03-20 16:46:10 +000050 /* Issue JEDEC Product ID Exit command */
Ollie Lho184a4042005-11-26 21:55:36 +000051 *(volatile uint8_t *) (bios + 0x5555) = 0xAA;
Ollie Lho761bf1b2004-03-20 16:46:10 +000052 myusec_delay(10);
Ollie Lho184a4042005-11-26 21:55:36 +000053 *(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
Ollie Lho761bf1b2004-03-20 16:46:10 +000054 myusec_delay(10);
Ollie Lho184a4042005-11-26 21:55:36 +000055 *(volatile uint8_t *) (bios + 0x5555) = 0xF0;
Ollie Lho761bf1b2004-03-20 16:46:10 +000056 myusec_delay(10);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000057
Ollie Lho184a4042005-11-26 21:55:36 +000058 printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2);
Ollie Lho761bf1b2004-03-20 16:46:10 +000059 if (id1 == flash->manufacture_id && id2 == flash->model_id)
60 return 1;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000061
Ollie Lho761bf1b2004-03-20 16:46:10 +000062 return 0;
Ollie Lho73eca802004-03-19 22:10:07 +000063}
64
Ollie Lho184a4042005-11-26 21:55:36 +000065int erase_sector_jedec(volatile uint8_t *bios, unsigned int page)
Ollie Lho73eca802004-03-19 22:10:07 +000066{
Ollie Lho761bf1b2004-03-20 16:46:10 +000067 /* Issue the Sector Erase command */
Ollie Lho184a4042005-11-26 21:55:36 +000068 *(volatile uint8_t *) (bios + 0x5555) = 0xAA;
Ollie Lho73eca802004-03-19 22:10:07 +000069 myusec_delay(10);
Ollie Lho184a4042005-11-26 21:55:36 +000070 *(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
Ollie Lho73eca802004-03-19 22:10:07 +000071 myusec_delay(10);
Ollie Lho184a4042005-11-26 21:55:36 +000072 *(volatile uint8_t *) (bios + 0x5555) = 0x80;
Ollie Lho73eca802004-03-19 22:10:07 +000073 myusec_delay(10);
Ollie Lhoefa28582004-12-08 20:10:01 +000074
Ollie Lho184a4042005-11-26 21:55:36 +000075 *(volatile uint8_t *) (bios + 0x5555) = 0xAA;
Ollie Lho73eca802004-03-19 22:10:07 +000076 myusec_delay(10);
Ollie Lho184a4042005-11-26 21:55:36 +000077 *(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
Ollie Lho73eca802004-03-19 22:10:07 +000078 myusec_delay(10);
Ollie Lho184a4042005-11-26 21:55:36 +000079 *(volatile uint8_t *) (bios + page) = 0x30;
Ollie Lho761bf1b2004-03-20 16:46:10 +000080 myusec_delay(10);
81
Ollie Lho73eca802004-03-19 22:10:07 +000082 /* wait for Toggle bit ready */
83 toggle_ready_jedec(bios);
84
Ollie Lho761bf1b2004-03-20 16:46:10 +000085 return (0);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000086}
Ollie Lho98bea8a2004-12-07 03:15:51 +000087
Ollie Lho184a4042005-11-26 21:55:36 +000088int erase_block_jedec(volatile uint8_t *bios, unsigned int block)
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +000089{
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +000090 /* Issue the Sector Erase command */
Ollie Lho184a4042005-11-26 21:55:36 +000091 *(volatile uint8_t *) (bios + 0x5555) = 0xAA;
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +000092 myusec_delay(10);
Ollie Lho184a4042005-11-26 21:55:36 +000093 *(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +000094 myusec_delay(10);
Ollie Lho184a4042005-11-26 21:55:36 +000095 *(volatile uint8_t *) (bios + 0x5555) = 0x80;
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +000096 myusec_delay(10);
Ollie Lhoefa28582004-12-08 20:10:01 +000097
Ollie Lho184a4042005-11-26 21:55:36 +000098 *(volatile uint8_t *) (bios + 0x5555) = 0xAA;
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +000099 myusec_delay(10);
Ollie Lho184a4042005-11-26 21:55:36 +0000100 *(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000101 myusec_delay(10);
Ollie Lho184a4042005-11-26 21:55:36 +0000102 *(volatile uint8_t *) (bios + block) = 0x50;
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000103 myusec_delay(10);
104
105 /* wait for Toggle bit ready */
106 toggle_ready_jedec(bios);
107
108 return (0);
109}
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000110
Ollie Lho761bf1b2004-03-20 16:46:10 +0000111int erase_chip_jedec(struct flashchip *flash)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000112{
Ollie Lho184a4042005-11-26 21:55:36 +0000113 volatile uint8_t *bios = flash->virt_addr;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000114
Ollie Lho761bf1b2004-03-20 16:46:10 +0000115 /* Issue the JEDEC Chip Erase command */
Ollie Lho184a4042005-11-26 21:55:36 +0000116 *(volatile uint8_t *) (bios + 0x5555) = 0xAA;
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000117 myusec_delay(10);
Ollie Lho184a4042005-11-26 21:55:36 +0000118 *(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
Ollie Lho73eca802004-03-19 22:10:07 +0000119 myusec_delay(10);
Ollie Lho184a4042005-11-26 21:55:36 +0000120 *(volatile uint8_t *) (bios + 0x5555) = 0x80;
Ollie Lho73eca802004-03-19 22:10:07 +0000121 myusec_delay(10);
Ollie Lhoefa28582004-12-08 20:10:01 +0000122
Ollie Lho184a4042005-11-26 21:55:36 +0000123 *(volatile uint8_t *) (bios + 0x5555) = 0xAA;
Ollie Lho73eca802004-03-19 22:10:07 +0000124 myusec_delay(10);
Ollie Lho184a4042005-11-26 21:55:36 +0000125 *(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
Ollie Lho73eca802004-03-19 22:10:07 +0000126 myusec_delay(10);
Ollie Lho184a4042005-11-26 21:55:36 +0000127 *(volatile uint8_t *) (bios + 0x5555) = 0x10;
Ollie Lho73eca802004-03-19 22:10:07 +0000128 myusec_delay(10);
129
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000130 toggle_ready_jedec(bios);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000131
Ollie Lho761bf1b2004-03-20 16:46:10 +0000132 return (0);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000133}
134
Ollie Lho184a4042005-11-26 21:55:36 +0000135int write_page_write_jedec(volatile uint8_t *bios, uint8_t *src,
136 volatile uint8_t *dst, int page_size)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000137{
138 int i;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000139
Ollie Lho761bf1b2004-03-20 16:46:10 +0000140 /* Issue JEDEC Data Unprotect comand */
Ollie Lho184a4042005-11-26 21:55:36 +0000141 *(volatile uint8_t *) (bios + 0x5555) = 0xAA;
142 *(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
143 *(volatile uint8_t *) (bios + 0x5555) = 0xA0;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000144
Ollie Lho98bea8a2004-12-07 03:15:51 +0000145 /* transfer data from source to destination */
Ollie Lho761bf1b2004-03-20 16:46:10 +0000146 for (i = 0; i < page_size; i++) {
Ollie Lho98bea8a2004-12-07 03:15:51 +0000147 /* If the data is 0xFF, don't program it */
148 if (*src == 0xFF)
149 continue;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000150 *dst++ = *src++;
151 }
152
Ollie Lho761bf1b2004-03-20 16:46:10 +0000153 toggle_ready_jedec(dst - 1);
Ollie Lho98bea8a2004-12-07 03:15:51 +0000154
155 return 0;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000156}
157
Ollie Lho184a4042005-11-26 21:55:36 +0000158int write_byte_program_jedec(volatile uint8_t *bios, uint8_t *src,
159 volatile uint8_t *dst)
Ollie Lho070647d2004-03-22 22:19:17 +0000160{
Ollie Lho1b8b6602004-12-08 02:10:33 +0000161 int tried = 0;
162
Ollie Lho98bea8a2004-12-07 03:15:51 +0000163 /* If the data is 0xFF, don't program it */
Ollie Lho070647d2004-03-22 22:19:17 +0000164 if (*src == 0xFF) {
Ollie Lho070647d2004-03-22 22:19:17 +0000165 return 0;
166 }
Ollie Lho98bea8a2004-12-07 03:15:51 +0000167
Ollie Lho1b8b6602004-12-08 02:10:33 +0000168retry:
Ollie Lho070647d2004-03-22 22:19:17 +0000169 /* Issue JEDEC Byte Program command */
Ollie Lho184a4042005-11-26 21:55:36 +0000170 *(volatile uint8_t *) (bios + 0x5555) = 0xAA;
171 *(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
172 *(volatile uint8_t *) (bios + 0x5555) = 0xA0;
Ollie Lho98bea8a2004-12-07 03:15:51 +0000173
174 /* transfer data from source to destination */
Ollie Lho070647d2004-03-22 22:19:17 +0000175 *dst = *src;
176 toggle_ready_jedec(bios);
Ollie Lho8b8897a2004-03-27 00:18:15 +0000177
Ollie Lho1b8b6602004-12-08 02:10:33 +0000178 if (*dst != *src && tried++ < 0x10) {
179 goto retry;
180 }
181
Ollie Lho070647d2004-03-22 22:19:17 +0000182 return 0;
183}
184
Ollie Lho184a4042005-11-26 21:55:36 +0000185int write_sector_jedec(volatile uint8_t *bios, uint8_t *src,
186 volatile uint8_t *dst, unsigned int page_size)
Ollie Lho761bf1b2004-03-20 16:46:10 +0000187{
188 int i;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000189
190 for (i = 0; i < page_size; i++) {
Ollie Lho8b8897a2004-03-27 00:18:15 +0000191 write_byte_program_jedec(bios, src, dst);
192 dst++, src++;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000193 }
194
195 return (0);
196}
197
Ollie Lho184a4042005-11-26 21:55:36 +0000198int write_jedec(struct flashchip *flash, uint8_t *buf)
Ollie Lho761bf1b2004-03-20 16:46:10 +0000199{
200 int i;
Ollie Lho070647d2004-03-22 22:19:17 +0000201 int total_size = flash->total_size * 1024;
202 int page_size = flash->page_size;
Ollie Lho184a4042005-11-26 21:55:36 +0000203 volatile uint8_t *bios = flash->virt_addr;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000204
205 erase_chip_jedec(flash);
Ollie Lho184a4042005-11-26 21:55:36 +0000206 if (*bios != (uint8_t) 0xff) {
Ronald G. Minnich56439422002-09-06 16:58:14 +0000207 printf("ERASE FAILED\n");
208 return -1;
209 }
Ollie Lho761bf1b2004-03-20 16:46:10 +0000210 printf("Programming Page: ");
211 for (i = 0; i < total_size / page_size; i++) {
212 printf("%04d at address: 0x%08x", i, i * page_size);
Ollie Lho8b8897a2004-03-27 00:18:15 +0000213 write_page_write_jedec(bios, buf + i * page_size,
214 bios + i * page_size, page_size);
Ollie Lho070647d2004-03-22 22:19:17 +0000215 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");
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000216 }
217 printf("\n");
Ollie Lho761bf1b2004-03-20 16:46:10 +0000218 protect_jedec(bios);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000219
Ollie Lho761bf1b2004-03-20 16:46:10 +0000220 return (0);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000221}