blob: 41fda423e240851d9c413e81fed813a06cead81d [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
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +00006 * Copyright 2006 Giampiero Giancipoli <gianci@email.it>
7 * Copyright 2006 coresystems GmbH <info@coresystems.de>
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +00008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 *
24 * Reference:
25 *
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000026 */
27
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000028#include <stdio.h>
Ollie Lho184a4042005-11-26 21:55:36 +000029#include <stdint.h>
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000030#include "flash.h"
31#include "jedec.h"
Ollie Lho184a4042005-11-26 21:55:36 +000032#include "debug.h"
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000033
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +000034#define MAX_REFLASH_TRIES 0x10
35
Ollie Lho761bf1b2004-03-20 16:46:10 +000036int probe_jedec(struct flashchip *flash)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000037{
Ollie Lho184a4042005-11-26 21:55:36 +000038 volatile uint8_t *bios = flash->virt_addr;
39 uint8_t id1, id2;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000040
Ollie Lho761bf1b2004-03-20 16:46:10 +000041 /* Issue JEDEC Product ID Entry command */
Uwe Hermanna7e05482007-05-09 10:17:44 +000042 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
Ollie Lho761bf1b2004-03-20 16:46:10 +000043 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +000044 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
Ollie Lho761bf1b2004-03-20 16:46:10 +000045 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +000046 *(volatile uint8_t *)(bios + 0x5555) = 0x90;
Ollie Lho761bf1b2004-03-20 16:46:10 +000047 myusec_delay(10);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000048
Ollie Lho761bf1b2004-03-20 16:46:10 +000049 /* Read product ID */
Uwe Hermanna7e05482007-05-09 10:17:44 +000050 id1 = *(volatile uint8_t *)bios;
51 id2 = *(volatile uint8_t *)(bios + 0x01);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000052
Ollie Lho761bf1b2004-03-20 16:46:10 +000053 /* Issue JEDEC Product ID Exit command */
Uwe Hermanna7e05482007-05-09 10:17:44 +000054 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
Ollie Lho761bf1b2004-03-20 16:46:10 +000055 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +000056 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
Ollie Lho761bf1b2004-03-20 16:46:10 +000057 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +000058 *(volatile uint8_t *)(bios + 0x5555) = 0xF0;
Ollie Lho761bf1b2004-03-20 16:46:10 +000059 myusec_delay(10);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000060
Ollie Lho184a4042005-11-26 21:55:36 +000061 printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2);
Ollie Lho761bf1b2004-03-20 16:46:10 +000062 if (id1 == flash->manufacture_id && id2 == flash->model_id)
63 return 1;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000064
Ollie Lho761bf1b2004-03-20 16:46:10 +000065 return 0;
Ollie Lho73eca802004-03-19 22:10:07 +000066}
67
Ollie Lho184a4042005-11-26 21:55:36 +000068int erase_sector_jedec(volatile uint8_t *bios, unsigned int page)
Ollie Lho73eca802004-03-19 22:10:07 +000069{
Ollie Lho761bf1b2004-03-20 16:46:10 +000070 /* Issue the Sector Erase command */
Uwe Hermanna7e05482007-05-09 10:17:44 +000071 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
Ollie Lho73eca802004-03-19 22:10:07 +000072 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +000073 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
Ollie Lho73eca802004-03-19 22:10:07 +000074 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +000075 *(volatile uint8_t *)(bios + 0x5555) = 0x80;
Ollie Lho73eca802004-03-19 22:10:07 +000076 myusec_delay(10);
Ollie Lhoefa28582004-12-08 20:10:01 +000077
Uwe Hermanna7e05482007-05-09 10:17:44 +000078 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
Ollie Lho73eca802004-03-19 22:10:07 +000079 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +000080 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
Ollie Lho73eca802004-03-19 22:10:07 +000081 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +000082 *(volatile uint8_t *)(bios + page) = 0x30;
Ollie Lho761bf1b2004-03-20 16:46:10 +000083 myusec_delay(10);
84
Ollie Lho73eca802004-03-19 22:10:07 +000085 /* wait for Toggle bit ready */
86 toggle_ready_jedec(bios);
87
Ollie Lho761bf1b2004-03-20 16:46:10 +000088 return (0);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000089}
Ollie Lho98bea8a2004-12-07 03:15:51 +000090
Ollie Lho184a4042005-11-26 21:55:36 +000091int erase_block_jedec(volatile uint8_t *bios, unsigned int block)
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +000092{
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +000093 /* Issue the Sector Erase command */
Uwe Hermanna7e05482007-05-09 10:17:44 +000094 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +000095 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +000096 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +000097 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +000098 *(volatile uint8_t *)(bios + 0x5555) = 0x80;
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +000099 myusec_delay(10);
Ollie Lhoefa28582004-12-08 20:10:01 +0000100
Uwe Hermanna7e05482007-05-09 10:17:44 +0000101 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000102 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000103 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000104 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000105 *(volatile uint8_t *)(bios + block) = 0x50;
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000106 myusec_delay(10);
107
108 /* wait for Toggle bit ready */
109 toggle_ready_jedec(bios);
110
111 return (0);
112}
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000113
Ollie Lho761bf1b2004-03-20 16:46:10 +0000114int erase_chip_jedec(struct flashchip *flash)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000115{
Ollie Lho184a4042005-11-26 21:55:36 +0000116 volatile uint8_t *bios = flash->virt_addr;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000117
Ollie Lho761bf1b2004-03-20 16:46:10 +0000118 /* Issue the JEDEC Chip Erase command */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000119 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000120 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000121 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
Ollie Lho73eca802004-03-19 22:10:07 +0000122 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000123 *(volatile uint8_t *)(bios + 0x5555) = 0x80;
Ollie Lho73eca802004-03-19 22:10:07 +0000124 myusec_delay(10);
Ollie Lhoefa28582004-12-08 20:10:01 +0000125
Uwe Hermanna7e05482007-05-09 10:17:44 +0000126 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
Ollie Lho73eca802004-03-19 22:10:07 +0000127 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000128 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
Ollie Lho73eca802004-03-19 22:10:07 +0000129 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000130 *(volatile uint8_t *)(bios + 0x5555) = 0x10;
Ollie Lho73eca802004-03-19 22:10:07 +0000131 myusec_delay(10);
132
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000133 toggle_ready_jedec(bios);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000134
Ollie Lho761bf1b2004-03-20 16:46:10 +0000135 return (0);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000136}
137
Ollie Lho184a4042005-11-26 21:55:36 +0000138int write_page_write_jedec(volatile uint8_t *bios, uint8_t *src,
139 volatile uint8_t *dst, int page_size)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000140{
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000141 int i, tried = 0, start_index = 0, ok;
142 volatile uint8_t *d = dst;
143 uint8_t *s = src;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000144
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000145retry:
Ollie Lho761bf1b2004-03-20 16:46:10 +0000146 /* Issue JEDEC Data Unprotect comand */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000147 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
148 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
149 *(volatile uint8_t *)(bios + 0x5555) = 0xA0;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000150
Ollie Lho98bea8a2004-12-07 03:15:51 +0000151 /* transfer data from source to destination */
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000152 for (i = start_index; i < page_size; i++) {
Ollie Lho98bea8a2004-12-07 03:15:51 +0000153 /* If the data is 0xFF, don't program it */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000154 if (*src != 0xFF)
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000155 *dst = *src;
156 dst++;
157 src++;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000158 }
159
Ollie Lho761bf1b2004-03-20 16:46:10 +0000160 toggle_ready_jedec(dst - 1);
Ollie Lho98bea8a2004-12-07 03:15:51 +0000161
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000162 dst = d;
163 src = s;
164 ok = 1;
165 for (i = 0; i < page_size; i++) {
Uwe Hermanna7e05482007-05-09 10:17:44 +0000166 if (*dst != *src) {
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000167 ok = 0;
168 break;
169 }
170 dst++;
171 src++;
172 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000173
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000174 if (!ok && tried++ < MAX_REFLASH_TRIES) {
175 start_index = i;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000176 goto retry;
177 }
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000178 if (!ok) {
Uwe Hermanna7e05482007-05-09 10:17:44 +0000179 fprintf(stderr, " page %d failed!\n",
180 (unsigned int)(d - bios) / page_size);
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000181 }
182 return (!ok);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000183}
184
Ollie Lho184a4042005-11-26 21:55:36 +0000185int write_byte_program_jedec(volatile uint8_t *bios, uint8_t *src,
186 volatile uint8_t *dst)
Ollie Lho070647d2004-03-22 22:19:17 +0000187{
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000188 int tried = 0, ok = 1;
Ollie Lho1b8b6602004-12-08 02:10:33 +0000189
Ollie Lho98bea8a2004-12-07 03:15:51 +0000190 /* If the data is 0xFF, don't program it */
Ollie Lho070647d2004-03-22 22:19:17 +0000191 if (*src == 0xFF) {
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000192 return -1;
Ollie Lho070647d2004-03-22 22:19:17 +0000193 }
Ollie Lho98bea8a2004-12-07 03:15:51 +0000194
Ollie Lho1b8b6602004-12-08 02:10:33 +0000195retry:
Ollie Lho070647d2004-03-22 22:19:17 +0000196 /* Issue JEDEC Byte Program command */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000197 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
198 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
199 *(volatile uint8_t *)(bios + 0x5555) = 0xA0;
Ollie Lho98bea8a2004-12-07 03:15:51 +0000200
201 /* transfer data from source to destination */
Ollie Lho070647d2004-03-22 22:19:17 +0000202 *dst = *src;
203 toggle_ready_jedec(bios);
Ollie Lho8b8897a2004-03-27 00:18:15 +0000204
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000205 if (*dst != *src && tried++ < MAX_REFLASH_TRIES) {
Uwe Hermanna7e05482007-05-09 10:17:44 +0000206 goto retry;
207 }
Ollie Lho1b8b6602004-12-08 02:10:33 +0000208
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000209 if (tried >= MAX_REFLASH_TRIES)
Uwe Hermanna7e05482007-05-09 10:17:44 +0000210 ok = 0;
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000211
212 return (!ok);
Ollie Lho070647d2004-03-22 22:19:17 +0000213}
214
Ollie Lho184a4042005-11-26 21:55:36 +0000215int write_sector_jedec(volatile uint8_t *bios, uint8_t *src,
216 volatile uint8_t *dst, unsigned int page_size)
Ollie Lho761bf1b2004-03-20 16:46:10 +0000217{
218 int i;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000219
220 for (i = 0; i < page_size; i++) {
Ollie Lho8b8897a2004-03-27 00:18:15 +0000221 write_byte_program_jedec(bios, src, dst);
222 dst++, src++;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000223 }
224
225 return (0);
226}
227
Ollie Lho184a4042005-11-26 21:55:36 +0000228int write_jedec(struct flashchip *flash, uint8_t *buf)
Ollie Lho761bf1b2004-03-20 16:46:10 +0000229{
230 int i;
Ollie Lho070647d2004-03-22 22:19:17 +0000231 int total_size = flash->total_size * 1024;
232 int page_size = flash->page_size;
Ollie Lho184a4042005-11-26 21:55:36 +0000233 volatile uint8_t *bios = flash->virt_addr;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000234
235 erase_chip_jedec(flash);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000236 // dumb check if erase was successful.
237 for (i = 0; i < total_size; i++) {
238 if (bios[i] != (uint8_t) 0xff) {
239 printf("ERASE FAILED\n");
240 return -1;
241 }
242 }
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000243
Ollie Lho761bf1b2004-03-20 16:46:10 +0000244 printf("Programming Page: ");
245 for (i = 0; i < total_size / page_size; i++) {
246 printf("%04d at address: 0x%08x", i, i * page_size);
Ollie Lho8b8897a2004-03-27 00:18:15 +0000247 write_page_write_jedec(bios, buf + i * page_size,
248 bios + i * page_size, page_size);
Ollie Lho070647d2004-03-22 22:19:17 +0000249 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 +0000250 }
251 printf("\n");
Ollie Lho761bf1b2004-03-20 16:46:10 +0000252 protect_jedec(bios);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000253
Ollie Lho761bf1b2004-03-20 16:46:10 +0000254 return (0);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000255}