blob: 62c9be0eaf342e623c75f7e4767632721dee4290 [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
stepan7abc6322006-11-22 00:29:51 +00006 * Copyright 2006 Giampiero Giancipoli <gianci@email.it>
7 * Copyright 2006 coresystems GmbH <info@coresystems.de>
rminnich8d3ff912003-10-25 17:01:29 +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 *
rminnich8d3ff912003-10-25 17:01:29 +000026 */
27
28#include <stdio.h>
ollie6a600992005-11-26 21:55:36 +000029#include <stdint.h>
rminnich8d3ff912003-10-25 17:01:29 +000030#include "flash.h"
rminnich8d3ff912003-10-25 17:01:29 +000031
stepan7abc6322006-11-22 00:29:51 +000032#define MAX_REFLASH_TRIES 0x10
33
uwedf467892007-08-23 10:20:40 +000034void toggle_ready_jedec(volatile uint8_t *dst)
35{
36 unsigned int i = 0;
37 uint8_t tmp1, tmp2;
38
39 tmp1 = *dst & 0x40;
40
41 while (i++ < 0xFFFFFFF) {
42 tmp2 = *dst & 0x40;
43 if (tmp1 == tmp2) {
44 break;
45 }
46 tmp1 = tmp2;
47 }
48}
49
50void data_polling_jedec(volatile uint8_t *dst, uint8_t data)
51{
52 unsigned int i = 0;
53 uint8_t tmp;
54
55 data &= 0x80;
56
57 while (i++ < 0xFFFFFFF) {
58 tmp = *dst & 0x80;
59 if (tmp == data) {
60 break;
61 }
62 }
63}
64
65void unprotect_jedec(volatile uint8_t *bios)
66{
67 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
68 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
69 *(volatile uint8_t *)(bios + 0x5555) = 0x80;
70 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
71 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
72 *(volatile uint8_t *)(bios + 0x5555) = 0x20;
73
74 usleep(200);
75}
76
77void protect_jedec(volatile uint8_t *bios)
78{
79 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
80 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
81 *(volatile uint8_t *)(bios + 0x5555) = 0xA0;
82
83 usleep(200);
84}
85
ollie5b621572004-03-20 16:46:10 +000086int probe_jedec(struct flashchip *flash)
rminnich8d3ff912003-10-25 17:01:29 +000087{
stepan7cd945e2007-05-23 17:20:56 +000088 volatile uint8_t *bios = flash->virtual_memory;
ollie6a600992005-11-26 21:55:36 +000089 uint8_t id1, id2;
rminnich8d3ff912003-10-25 17:01:29 +000090
ollie5b621572004-03-20 16:46:10 +000091 /* Issue JEDEC Product ID Entry command */
uwef6641642007-05-09 10:17:44 +000092 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
ollie5b621572004-03-20 16:46:10 +000093 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +000094 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
ollie5b621572004-03-20 16:46:10 +000095 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +000096 *(volatile uint8_t *)(bios + 0x5555) = 0x90;
ollie5b621572004-03-20 16:46:10 +000097 myusec_delay(10);
rminnich8d3ff912003-10-25 17:01:29 +000098
ollie5b621572004-03-20 16:46:10 +000099 /* Read product ID */
uwef6641642007-05-09 10:17:44 +0000100 id1 = *(volatile uint8_t *)bios;
101 id2 = *(volatile uint8_t *)(bios + 0x01);
rminnich8d3ff912003-10-25 17:01:29 +0000102
ollie5b621572004-03-20 16:46:10 +0000103 /* Issue JEDEC Product ID Exit command */
uwef6641642007-05-09 10:17:44 +0000104 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
ollie5b621572004-03-20 16:46:10 +0000105 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000106 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
ollie5b621572004-03-20 16:46:10 +0000107 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000108 *(volatile uint8_t *)(bios + 0x5555) = 0xF0;
ollie5b621572004-03-20 16:46:10 +0000109 myusec_delay(10);
rminnich8d3ff912003-10-25 17:01:29 +0000110
ollie6a600992005-11-26 21:55:36 +0000111 printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2);
ollie5b621572004-03-20 16:46:10 +0000112 if (id1 == flash->manufacture_id && id2 == flash->model_id)
113 return 1;
rminnich8d3ff912003-10-25 17:01:29 +0000114
ollie5b621572004-03-20 16:46:10 +0000115 return 0;
olliea3def632004-03-19 22:10:07 +0000116}
117
ollie6a600992005-11-26 21:55:36 +0000118int erase_sector_jedec(volatile uint8_t *bios, unsigned int page)
olliea3def632004-03-19 22:10:07 +0000119{
ollie5b621572004-03-20 16:46:10 +0000120 /* Issue the Sector Erase command */
uwef6641642007-05-09 10:17:44 +0000121 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
olliea3def632004-03-19 22:10:07 +0000122 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000123 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
olliea3def632004-03-19 22:10:07 +0000124 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000125 *(volatile uint8_t *)(bios + 0x5555) = 0x80;
olliea3def632004-03-19 22:10:07 +0000126 myusec_delay(10);
ollie0eb62d62004-12-08 20:10:01 +0000127
uwef6641642007-05-09 10:17:44 +0000128 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
olliea3def632004-03-19 22:10:07 +0000129 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000130 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
olliea3def632004-03-19 22:10:07 +0000131 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000132 *(volatile uint8_t *)(bios + page) = 0x30;
ollie5b621572004-03-20 16:46:10 +0000133 myusec_delay(10);
134
olliea3def632004-03-19 22:10:07 +0000135 /* wait for Toggle bit ready */
136 toggle_ready_jedec(bios);
137
uwebe4477b2007-08-23 16:08:21 +0000138 return 0;
rminnich8d3ff912003-10-25 17:01:29 +0000139}
olliea4302802004-12-07 03:15:51 +0000140
ollie6a600992005-11-26 21:55:36 +0000141int erase_block_jedec(volatile uint8_t *bios, unsigned int block)
rminnichdfcbaa72004-09-30 16:37:01 +0000142{
rminnichdfcbaa72004-09-30 16:37:01 +0000143 /* Issue the Sector Erase command */
uwef6641642007-05-09 10:17:44 +0000144 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
rminnichdfcbaa72004-09-30 16:37:01 +0000145 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000146 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
rminnichdfcbaa72004-09-30 16:37:01 +0000147 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000148 *(volatile uint8_t *)(bios + 0x5555) = 0x80;
rminnichdfcbaa72004-09-30 16:37:01 +0000149 myusec_delay(10);
ollie0eb62d62004-12-08 20:10:01 +0000150
uwef6641642007-05-09 10:17:44 +0000151 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
rminnichdfcbaa72004-09-30 16:37:01 +0000152 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000153 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
rminnichdfcbaa72004-09-30 16:37:01 +0000154 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000155 *(volatile uint8_t *)(bios + block) = 0x50;
rminnichdfcbaa72004-09-30 16:37:01 +0000156 myusec_delay(10);
157
158 /* wait for Toggle bit ready */
159 toggle_ready_jedec(bios);
160
uwebe4477b2007-08-23 16:08:21 +0000161 return 0;
rminnichdfcbaa72004-09-30 16:37:01 +0000162}
rminnich8d3ff912003-10-25 17:01:29 +0000163
ollie5b621572004-03-20 16:46:10 +0000164int erase_chip_jedec(struct flashchip *flash)
rminnich8d3ff912003-10-25 17:01:29 +0000165{
stepan7cd945e2007-05-23 17:20:56 +0000166 volatile uint8_t *bios = flash->virtual_memory;
rminnich8d3ff912003-10-25 17:01:29 +0000167
ollie5b621572004-03-20 16:46:10 +0000168 /* Issue the JEDEC Chip Erase command */
uwef6641642007-05-09 10:17:44 +0000169 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
rminnich8d3ff912003-10-25 17:01:29 +0000170 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000171 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
olliea3def632004-03-19 22:10:07 +0000172 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000173 *(volatile uint8_t *)(bios + 0x5555) = 0x80;
olliea3def632004-03-19 22:10:07 +0000174 myusec_delay(10);
ollie0eb62d62004-12-08 20:10:01 +0000175
uwef6641642007-05-09 10:17:44 +0000176 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
olliea3def632004-03-19 22:10:07 +0000177 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000178 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
olliea3def632004-03-19 22:10:07 +0000179 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000180 *(volatile uint8_t *)(bios + 0x5555) = 0x10;
olliea3def632004-03-19 22:10:07 +0000181 myusec_delay(10);
182
rminnich8d3ff912003-10-25 17:01:29 +0000183 toggle_ready_jedec(bios);
184
uwebe4477b2007-08-23 16:08:21 +0000185 return 0;
rminnich8d3ff912003-10-25 17:01:29 +0000186}
187
ollie6a600992005-11-26 21:55:36 +0000188int write_page_write_jedec(volatile uint8_t *bios, uint8_t *src,
189 volatile uint8_t *dst, int page_size)
rminnich8d3ff912003-10-25 17:01:29 +0000190{
stepan7abc6322006-11-22 00:29:51 +0000191 int i, tried = 0, start_index = 0, ok;
192 volatile uint8_t *d = dst;
193 uint8_t *s = src;
rminnich8d3ff912003-10-25 17:01:29 +0000194
stepan7abc6322006-11-22 00:29:51 +0000195retry:
ollie5b621572004-03-20 16:46:10 +0000196 /* Issue JEDEC Data Unprotect comand */
uwef6641642007-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;
ollie5b621572004-03-20 16:46:10 +0000200
olliea4302802004-12-07 03:15:51 +0000201 /* transfer data from source to destination */
stepan7abc6322006-11-22 00:29:51 +0000202 for (i = start_index; i < page_size; i++) {
olliea4302802004-12-07 03:15:51 +0000203 /* If the data is 0xFF, don't program it */
uwef6641642007-05-09 10:17:44 +0000204 if (*src != 0xFF)
stepan7abc6322006-11-22 00:29:51 +0000205 *dst = *src;
206 dst++;
207 src++;
ollie5b621572004-03-20 16:46:10 +0000208 }
209
ollie5b621572004-03-20 16:46:10 +0000210 toggle_ready_jedec(dst - 1);
olliea4302802004-12-07 03:15:51 +0000211
stepan7abc6322006-11-22 00:29:51 +0000212 dst = d;
213 src = s;
214 ok = 1;
215 for (i = 0; i < page_size; i++) {
uwef6641642007-05-09 10:17:44 +0000216 if (*dst != *src) {
stepan7abc6322006-11-22 00:29:51 +0000217 ok = 0;
218 break;
219 }
220 dst++;
221 src++;
222 }
uwef6641642007-05-09 10:17:44 +0000223
stepan7abc6322006-11-22 00:29:51 +0000224 if (!ok && tried++ < MAX_REFLASH_TRIES) {
225 start_index = i;
uwef6641642007-05-09 10:17:44 +0000226 goto retry;
227 }
stepan7abc6322006-11-22 00:29:51 +0000228 if (!ok) {
uwef6641642007-05-09 10:17:44 +0000229 fprintf(stderr, " page %d failed!\n",
230 (unsigned int)(d - bios) / page_size);
stepan7abc6322006-11-22 00:29:51 +0000231 }
uwebe4477b2007-08-23 16:08:21 +0000232 return !ok;
ollie5b621572004-03-20 16:46:10 +0000233}
234
ollie6a600992005-11-26 21:55:36 +0000235int write_byte_program_jedec(volatile uint8_t *bios, uint8_t *src,
236 volatile uint8_t *dst)
olliebb5917a2004-03-22 22:19:17 +0000237{
stepan7abc6322006-11-22 00:29:51 +0000238 int tried = 0, ok = 1;
olliedd68ded2004-12-08 02:10:33 +0000239
olliea4302802004-12-07 03:15:51 +0000240 /* If the data is 0xFF, don't program it */
olliebb5917a2004-03-22 22:19:17 +0000241 if (*src == 0xFF) {
stepan7abc6322006-11-22 00:29:51 +0000242 return -1;
olliebb5917a2004-03-22 22:19:17 +0000243 }
olliea4302802004-12-07 03:15:51 +0000244
olliedd68ded2004-12-08 02:10:33 +0000245retry:
olliebb5917a2004-03-22 22:19:17 +0000246 /* Issue JEDEC Byte Program command */
uwef6641642007-05-09 10:17:44 +0000247 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
248 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
249 *(volatile uint8_t *)(bios + 0x5555) = 0xA0;
olliea4302802004-12-07 03:15:51 +0000250
251 /* transfer data from source to destination */
olliebb5917a2004-03-22 22:19:17 +0000252 *dst = *src;
253 toggle_ready_jedec(bios);
ollief1845bd2004-03-27 00:18:15 +0000254
stepan7abc6322006-11-22 00:29:51 +0000255 if (*dst != *src && tried++ < MAX_REFLASH_TRIES) {
uwef6641642007-05-09 10:17:44 +0000256 goto retry;
257 }
olliedd68ded2004-12-08 02:10:33 +0000258
stepan7abc6322006-11-22 00:29:51 +0000259 if (tried >= MAX_REFLASH_TRIES)
uwef6641642007-05-09 10:17:44 +0000260 ok = 0;
stepan7abc6322006-11-22 00:29:51 +0000261
uwebe4477b2007-08-23 16:08:21 +0000262 return !ok;
olliebb5917a2004-03-22 22:19:17 +0000263}
264
ollie6a600992005-11-26 21:55:36 +0000265int write_sector_jedec(volatile uint8_t *bios, uint8_t *src,
266 volatile uint8_t *dst, unsigned int page_size)
ollie5b621572004-03-20 16:46:10 +0000267{
268 int i;
ollie5b621572004-03-20 16:46:10 +0000269
270 for (i = 0; i < page_size; i++) {
ollief1845bd2004-03-27 00:18:15 +0000271 write_byte_program_jedec(bios, src, dst);
272 dst++, src++;
ollie5b621572004-03-20 16:46:10 +0000273 }
274
uwebe4477b2007-08-23 16:08:21 +0000275 return 0;
ollie5b621572004-03-20 16:46:10 +0000276}
277
ollie6a600992005-11-26 21:55:36 +0000278int write_jedec(struct flashchip *flash, uint8_t *buf)
ollie5b621572004-03-20 16:46:10 +0000279{
280 int i;
olliebb5917a2004-03-22 22:19:17 +0000281 int total_size = flash->total_size * 1024;
282 int page_size = flash->page_size;
stepan7cd945e2007-05-23 17:20:56 +0000283 volatile uint8_t *bios = flash->virtual_memory;
ollie5b621572004-03-20 16:46:10 +0000284
285 erase_chip_jedec(flash);
uwef6641642007-05-09 10:17:44 +0000286 // dumb check if erase was successful.
287 for (i = 0; i < total_size; i++) {
288 if (bios[i] != (uint8_t) 0xff) {
289 printf("ERASE FAILED\n");
290 return -1;
291 }
292 }
stepan7abc6322006-11-22 00:29:51 +0000293
ollie5b621572004-03-20 16:46:10 +0000294 printf("Programming Page: ");
295 for (i = 0; i < total_size / page_size; i++) {
296 printf("%04d at address: 0x%08x", i, i * page_size);
ollief1845bd2004-03-27 00:18:15 +0000297 write_page_write_jedec(bios, buf + i * page_size,
298 bios + i * page_size, page_size);
olliebb5917a2004-03-22 22:19:17 +0000299 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 +0000300 }
301 printf("\n");
ollie5b621572004-03-20 16:46:10 +0000302 protect_jedec(bios);
rminnich8d3ff912003-10-25 17:01:29 +0000303
uwebe4477b2007-08-23 16:08:21 +0000304 return 0;
rminnich8d3ff912003-10-25 17:01:29 +0000305}