blob: 958875194a272b0d872c13090219fc2037a93505 [file] [log] [blame]
rminnich8d3ff912003-10-25 17:01:29 +00001/*
uweb25f1ea2007-08-29 17:52:32 +00002 * This file is part of the flashrom project.
rminnich8d3ff912003-10-25 17:01:29 +00003 *
uwe555dd972007-09-09 20:21:05 +00004 * Copyright (C) 2000 Silicon Integrated System Corporation
5 * Copyright (C) 2006 Giampiero Giancipoli <gianci@email.it>
6 * Copyright (C) 2006 coresystems GmbH <info@coresystems.de>
hailfinger428f2012007-12-31 01:49:00 +00007 * Copyright (C) 2007 Carl-Daniel Hailfinger
rminnich8d3ff912003-10-25 17:01:29 +00008 *
uweb25f1ea2007-08-29 17:52:32 +00009 * 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.
rminnich8d3ff912003-10-25 17:01:29 +000013 *
uweb25f1ea2007-08-29 17:52:32 +000014 * 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.
rminnich8d3ff912003-10-25 17:01:29 +000018 *
uweb25f1ea2007-08-29 17:52:32 +000019 * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
rminnich8d3ff912003-10-25 17:01:29 +000022 */
23
24#include <stdio.h>
ollie6a600992005-11-26 21:55:36 +000025#include <stdint.h>
rminnich8d3ff912003-10-25 17:01:29 +000026#include "flash.h"
rminnich8d3ff912003-10-25 17:01:29 +000027
stepan7abc6322006-11-22 00:29:51 +000028#define MAX_REFLASH_TRIES 0x10
29
uwedf467892007-08-23 10:20:40 +000030void toggle_ready_jedec(volatile uint8_t *dst)
31{
32 unsigned int i = 0;
33 uint8_t tmp1, tmp2;
34
35 tmp1 = *dst & 0x40;
36
37 while (i++ < 0xFFFFFFF) {
38 tmp2 = *dst & 0x40;
39 if (tmp1 == tmp2) {
40 break;
41 }
42 tmp1 = tmp2;
43 }
44}
45
46void data_polling_jedec(volatile uint8_t *dst, uint8_t data)
47{
48 unsigned int i = 0;
49 uint8_t tmp;
50
51 data &= 0x80;
52
53 while (i++ < 0xFFFFFFF) {
54 tmp = *dst & 0x80;
55 if (tmp == data) {
56 break;
57 }
58 }
59}
60
61void unprotect_jedec(volatile uint8_t *bios)
62{
63 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
64 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
65 *(volatile uint8_t *)(bios + 0x5555) = 0x80;
66 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
67 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
68 *(volatile uint8_t *)(bios + 0x5555) = 0x20;
69
70 usleep(200);
71}
72
73void protect_jedec(volatile uint8_t *bios)
74{
75 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
76 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
77 *(volatile uint8_t *)(bios + 0x5555) = 0xA0;
78
79 usleep(200);
80}
81
ollie5b621572004-03-20 16:46:10 +000082int probe_jedec(struct flashchip *flash)
rminnich8d3ff912003-10-25 17:01:29 +000083{
stepan7cd945e2007-05-23 17:20:56 +000084 volatile uint8_t *bios = flash->virtual_memory;
ollie6a600992005-11-26 21:55:36 +000085 uint8_t id1, id2;
hailfinger428f2012007-12-31 01:49:00 +000086 uint32_t largeid1, largeid2;
rminnich8d3ff912003-10-25 17:01:29 +000087
ollie5b621572004-03-20 16:46:10 +000088 /* Issue JEDEC Product ID Entry command */
uwef6641642007-05-09 10:17:44 +000089 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
ollie5b621572004-03-20 16:46:10 +000090 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +000091 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
ollie5b621572004-03-20 16:46:10 +000092 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +000093 *(volatile uint8_t *)(bios + 0x5555) = 0x90;
hailfinger9c715292007-11-13 14:56:54 +000094 /* Older chips may need up to 100 us to respond. The ATMEL 29C020
95 * needs 10 ms according to the data sheet, but it has been tested
96 * to work reliably with 20 us. Allow a factor of 2 safety margin.
97 */
98 myusec_delay(40);
rminnich8d3ff912003-10-25 17:01:29 +000099
ollie5b621572004-03-20 16:46:10 +0000100 /* Read product ID */
uwef6641642007-05-09 10:17:44 +0000101 id1 = *(volatile uint8_t *)bios;
102 id2 = *(volatile uint8_t *)(bios + 0x01);
hailfinger428f2012007-12-31 01:49:00 +0000103 largeid1 = id1;
104 largeid2 = id2;
105
106 /* Check if it is a continuation ID, this should be a while loop. */
107 if (id1 == 0x7F) {
108 largeid1 <<= 8;
109 id1 = *(volatile uint8_t *)(bios + 0x100);
110 largeid1 |= id1;
111 }
112 if (id2 == 0x7F) {
113 largeid2 <<= 8;
114 id2 = *(volatile uint8_t *)(bios + 0x101);
115 largeid2 |= id2;
116 }
rminnich8d3ff912003-10-25 17:01:29 +0000117
ollie5b621572004-03-20 16:46:10 +0000118 /* Issue JEDEC Product ID Exit command */
uwef6641642007-05-09 10:17:44 +0000119 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
ollie5b621572004-03-20 16:46:10 +0000120 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000121 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
ollie5b621572004-03-20 16:46:10 +0000122 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000123 *(volatile uint8_t *)(bios + 0x5555) = 0xF0;
hailfinger9c715292007-11-13 14:56:54 +0000124 myusec_delay(40);
rminnich8d3ff912003-10-25 17:01:29 +0000125
hailfinger428f2012007-12-31 01:49:00 +0000126 printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, largeid1, largeid2);
127 if (largeid1 == flash->manufacture_id && largeid2 == flash->model_id)
ollie5b621572004-03-20 16:46:10 +0000128 return 1;
rminnich8d3ff912003-10-25 17:01:29 +0000129
ollie5b621572004-03-20 16:46:10 +0000130 return 0;
olliea3def632004-03-19 22:10:07 +0000131}
132
ollie6a600992005-11-26 21:55:36 +0000133int erase_sector_jedec(volatile uint8_t *bios, unsigned int page)
olliea3def632004-03-19 22:10:07 +0000134{
ollie5b621572004-03-20 16:46:10 +0000135 /* Issue the Sector Erase command */
uwef6641642007-05-09 10:17:44 +0000136 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
olliea3def632004-03-19 22:10:07 +0000137 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000138 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
olliea3def632004-03-19 22:10:07 +0000139 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000140 *(volatile uint8_t *)(bios + 0x5555) = 0x80;
olliea3def632004-03-19 22:10:07 +0000141 myusec_delay(10);
ollie0eb62d62004-12-08 20:10:01 +0000142
uwef6641642007-05-09 10:17:44 +0000143 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
olliea3def632004-03-19 22:10:07 +0000144 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000145 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
olliea3def632004-03-19 22:10:07 +0000146 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000147 *(volatile uint8_t *)(bios + page) = 0x30;
ollie5b621572004-03-20 16:46:10 +0000148 myusec_delay(10);
149
olliea3def632004-03-19 22:10:07 +0000150 /* wait for Toggle bit ready */
151 toggle_ready_jedec(bios);
152
uwebe4477b2007-08-23 16:08:21 +0000153 return 0;
rminnich8d3ff912003-10-25 17:01:29 +0000154}
olliea4302802004-12-07 03:15:51 +0000155
ollie6a600992005-11-26 21:55:36 +0000156int erase_block_jedec(volatile uint8_t *bios, unsigned int block)
rminnichdfcbaa72004-09-30 16:37:01 +0000157{
rminnichdfcbaa72004-09-30 16:37:01 +0000158 /* Issue the Sector Erase command */
uwef6641642007-05-09 10:17:44 +0000159 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
rminnichdfcbaa72004-09-30 16:37:01 +0000160 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000161 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
rminnichdfcbaa72004-09-30 16:37:01 +0000162 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000163 *(volatile uint8_t *)(bios + 0x5555) = 0x80;
rminnichdfcbaa72004-09-30 16:37:01 +0000164 myusec_delay(10);
ollie0eb62d62004-12-08 20:10:01 +0000165
uwef6641642007-05-09 10:17:44 +0000166 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
rminnichdfcbaa72004-09-30 16:37:01 +0000167 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000168 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
rminnichdfcbaa72004-09-30 16:37:01 +0000169 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000170 *(volatile uint8_t *)(bios + block) = 0x50;
rminnichdfcbaa72004-09-30 16:37:01 +0000171 myusec_delay(10);
172
173 /* wait for Toggle bit ready */
174 toggle_ready_jedec(bios);
175
uwebe4477b2007-08-23 16:08:21 +0000176 return 0;
rminnichdfcbaa72004-09-30 16:37:01 +0000177}
rminnich8d3ff912003-10-25 17:01:29 +0000178
ollie5b621572004-03-20 16:46:10 +0000179int erase_chip_jedec(struct flashchip *flash)
rminnich8d3ff912003-10-25 17:01:29 +0000180{
stepan7cd945e2007-05-23 17:20:56 +0000181 volatile uint8_t *bios = flash->virtual_memory;
rminnich8d3ff912003-10-25 17:01:29 +0000182
ollie5b621572004-03-20 16:46:10 +0000183 /* Issue the JEDEC Chip Erase command */
uwef6641642007-05-09 10:17:44 +0000184 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
rminnich8d3ff912003-10-25 17:01:29 +0000185 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000186 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
olliea3def632004-03-19 22:10:07 +0000187 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000188 *(volatile uint8_t *)(bios + 0x5555) = 0x80;
olliea3def632004-03-19 22:10:07 +0000189 myusec_delay(10);
ollie0eb62d62004-12-08 20:10:01 +0000190
uwef6641642007-05-09 10:17:44 +0000191 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
olliea3def632004-03-19 22:10:07 +0000192 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000193 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
olliea3def632004-03-19 22:10:07 +0000194 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000195 *(volatile uint8_t *)(bios + 0x5555) = 0x10;
olliea3def632004-03-19 22:10:07 +0000196 myusec_delay(10);
197
rminnich8d3ff912003-10-25 17:01:29 +0000198 toggle_ready_jedec(bios);
199
uwebe4477b2007-08-23 16:08:21 +0000200 return 0;
rminnich8d3ff912003-10-25 17:01:29 +0000201}
202
ollie6a600992005-11-26 21:55:36 +0000203int write_page_write_jedec(volatile uint8_t *bios, uint8_t *src,
204 volatile uint8_t *dst, int page_size)
rminnich8d3ff912003-10-25 17:01:29 +0000205{
stepan7abc6322006-11-22 00:29:51 +0000206 int i, tried = 0, start_index = 0, ok;
207 volatile uint8_t *d = dst;
208 uint8_t *s = src;
rminnich8d3ff912003-10-25 17:01:29 +0000209
stepan7abc6322006-11-22 00:29:51 +0000210retry:
ollie5b621572004-03-20 16:46:10 +0000211 /* Issue JEDEC Data Unprotect comand */
uwef6641642007-05-09 10:17:44 +0000212 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
213 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
214 *(volatile uint8_t *)(bios + 0x5555) = 0xA0;
ollie5b621572004-03-20 16:46:10 +0000215
olliea4302802004-12-07 03:15:51 +0000216 /* transfer data from source to destination */
stepan7abc6322006-11-22 00:29:51 +0000217 for (i = start_index; i < page_size; i++) {
olliea4302802004-12-07 03:15:51 +0000218 /* If the data is 0xFF, don't program it */
uwef6641642007-05-09 10:17:44 +0000219 if (*src != 0xFF)
stepan7abc6322006-11-22 00:29:51 +0000220 *dst = *src;
221 dst++;
222 src++;
ollie5b621572004-03-20 16:46:10 +0000223 }
224
ollie5b621572004-03-20 16:46:10 +0000225 toggle_ready_jedec(dst - 1);
olliea4302802004-12-07 03:15:51 +0000226
stepan7abc6322006-11-22 00:29:51 +0000227 dst = d;
228 src = s;
229 ok = 1;
230 for (i = 0; i < page_size; i++) {
uwef6641642007-05-09 10:17:44 +0000231 if (*dst != *src) {
stepan7abc6322006-11-22 00:29:51 +0000232 ok = 0;
233 break;
234 }
235 dst++;
236 src++;
237 }
uwef6641642007-05-09 10:17:44 +0000238
stepan7abc6322006-11-22 00:29:51 +0000239 if (!ok && tried++ < MAX_REFLASH_TRIES) {
240 start_index = i;
uwef6641642007-05-09 10:17:44 +0000241 goto retry;
242 }
stepan7abc6322006-11-22 00:29:51 +0000243 if (!ok) {
uwef6641642007-05-09 10:17:44 +0000244 fprintf(stderr, " page %d failed!\n",
245 (unsigned int)(d - bios) / page_size);
stepan7abc6322006-11-22 00:29:51 +0000246 }
uwebe4477b2007-08-23 16:08:21 +0000247 return !ok;
ollie5b621572004-03-20 16:46:10 +0000248}
249
ollie6a600992005-11-26 21:55:36 +0000250int write_byte_program_jedec(volatile uint8_t *bios, uint8_t *src,
251 volatile uint8_t *dst)
olliebb5917a2004-03-22 22:19:17 +0000252{
stepan7abc6322006-11-22 00:29:51 +0000253 int tried = 0, ok = 1;
olliedd68ded2004-12-08 02:10:33 +0000254
olliea4302802004-12-07 03:15:51 +0000255 /* If the data is 0xFF, don't program it */
olliebb5917a2004-03-22 22:19:17 +0000256 if (*src == 0xFF) {
stepan7abc6322006-11-22 00:29:51 +0000257 return -1;
olliebb5917a2004-03-22 22:19:17 +0000258 }
olliea4302802004-12-07 03:15:51 +0000259
olliedd68ded2004-12-08 02:10:33 +0000260retry:
olliebb5917a2004-03-22 22:19:17 +0000261 /* Issue JEDEC Byte Program command */
uwef6641642007-05-09 10:17:44 +0000262 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
263 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
264 *(volatile uint8_t *)(bios + 0x5555) = 0xA0;
olliea4302802004-12-07 03:15:51 +0000265
266 /* transfer data from source to destination */
olliebb5917a2004-03-22 22:19:17 +0000267 *dst = *src;
268 toggle_ready_jedec(bios);
ollief1845bd2004-03-27 00:18:15 +0000269
stepan7abc6322006-11-22 00:29:51 +0000270 if (*dst != *src && tried++ < MAX_REFLASH_TRIES) {
uwef6641642007-05-09 10:17:44 +0000271 goto retry;
272 }
olliedd68ded2004-12-08 02:10:33 +0000273
stepan7abc6322006-11-22 00:29:51 +0000274 if (tried >= MAX_REFLASH_TRIES)
uwef6641642007-05-09 10:17:44 +0000275 ok = 0;
stepan7abc6322006-11-22 00:29:51 +0000276
uwebe4477b2007-08-23 16:08:21 +0000277 return !ok;
olliebb5917a2004-03-22 22:19:17 +0000278}
279
ollie6a600992005-11-26 21:55:36 +0000280int write_sector_jedec(volatile uint8_t *bios, uint8_t *src,
281 volatile uint8_t *dst, unsigned int page_size)
ollie5b621572004-03-20 16:46:10 +0000282{
283 int i;
ollie5b621572004-03-20 16:46:10 +0000284
285 for (i = 0; i < page_size; i++) {
ollief1845bd2004-03-27 00:18:15 +0000286 write_byte_program_jedec(bios, src, dst);
287 dst++, src++;
ollie5b621572004-03-20 16:46:10 +0000288 }
289
uwebe4477b2007-08-23 16:08:21 +0000290 return 0;
ollie5b621572004-03-20 16:46:10 +0000291}
292
ollie6a600992005-11-26 21:55:36 +0000293int write_jedec(struct flashchip *flash, uint8_t *buf)
ollie5b621572004-03-20 16:46:10 +0000294{
295 int i;
olliebb5917a2004-03-22 22:19:17 +0000296 int total_size = flash->total_size * 1024;
297 int page_size = flash->page_size;
stepan7cd945e2007-05-23 17:20:56 +0000298 volatile uint8_t *bios = flash->virtual_memory;
ollie5b621572004-03-20 16:46:10 +0000299
300 erase_chip_jedec(flash);
uwef6641642007-05-09 10:17:44 +0000301 // dumb check if erase was successful.
302 for (i = 0; i < total_size; i++) {
303 if (bios[i] != (uint8_t) 0xff) {
uwefd2d0fe2007-10-17 23:55:15 +0000304 printf("ERASE FAILED @%d, val %02x!\n", i, bios[i]);
uwef6641642007-05-09 10:17:44 +0000305 return -1;
306 }
307 }
stepan7abc6322006-11-22 00:29:51 +0000308
uwefd2d0fe2007-10-17 23:55:15 +0000309 printf("Programming page: ");
ollie5b621572004-03-20 16:46:10 +0000310 for (i = 0; i < total_size / page_size; i++) {
311 printf("%04d at address: 0x%08x", i, i * page_size);
ollief1845bd2004-03-27 00:18:15 +0000312 write_page_write_jedec(bios, buf + i * page_size,
313 bios + i * page_size, page_size);
olliebb5917a2004-03-22 22:19:17 +0000314 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 +0000315 }
316 printf("\n");
ollie5b621572004-03-20 16:46:10 +0000317 protect_jedec(bios);
rminnich8d3ff912003-10-25 17:01:29 +0000318
uwebe4477b2007-08-23 16:08:21 +0000319 return 0;
rminnich8d3ff912003-10-25 17:01:29 +0000320}