blob: e818294f0e2eb1dc8cd06a39fe28d670d336cb25 [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
hailfinger79cf3672008-05-14 12:03:06 +000030/* Check one byte for odd parity */
31uint8_t oddparity(uint8_t val)
32{
33 val = (val ^ (val >> 4)) & 0xf;
34 val = (val ^ (val >> 2)) & 0x3;
35 return (val ^ (val >> 1)) & 0x1;
36}
37
uwedf467892007-08-23 10:20:40 +000038void toggle_ready_jedec(volatile uint8_t *dst)
39{
40 unsigned int i = 0;
41 uint8_t tmp1, tmp2;
42
43 tmp1 = *dst & 0x40;
44
45 while (i++ < 0xFFFFFFF) {
46 tmp2 = *dst & 0x40;
47 if (tmp1 == tmp2) {
48 break;
49 }
50 tmp1 = tmp2;
51 }
52}
53
54void data_polling_jedec(volatile uint8_t *dst, uint8_t data)
55{
56 unsigned int i = 0;
57 uint8_t tmp;
58
59 data &= 0x80;
60
61 while (i++ < 0xFFFFFFF) {
62 tmp = *dst & 0x80;
63 if (tmp == data) {
64 break;
65 }
66 }
67}
68
69void unprotect_jedec(volatile uint8_t *bios)
70{
71 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
72 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
73 *(volatile uint8_t *)(bios + 0x5555) = 0x80;
74 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
75 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
76 *(volatile uint8_t *)(bios + 0x5555) = 0x20;
77
78 usleep(200);
79}
80
81void protect_jedec(volatile uint8_t *bios)
82{
83 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
84 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
85 *(volatile uint8_t *)(bios + 0x5555) = 0xA0;
86
87 usleep(200);
88}
89
ollie5b621572004-03-20 16:46:10 +000090int probe_jedec(struct flashchip *flash)
rminnich8d3ff912003-10-25 17:01:29 +000091{
stepan7cd945e2007-05-23 17:20:56 +000092 volatile uint8_t *bios = flash->virtual_memory;
ollie6a600992005-11-26 21:55:36 +000093 uint8_t id1, id2;
hailfinger428f2012007-12-31 01:49:00 +000094 uint32_t largeid1, largeid2;
rminnich8d3ff912003-10-25 17:01:29 +000095
ollie5b621572004-03-20 16:46:10 +000096 /* Issue JEDEC Product ID Entry command */
uwef6641642007-05-09 10:17:44 +000097 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
ollie5b621572004-03-20 16:46:10 +000098 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +000099 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
ollie5b621572004-03-20 16:46:10 +0000100 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000101 *(volatile uint8_t *)(bios + 0x5555) = 0x90;
hailfinger9c715292007-11-13 14:56:54 +0000102 /* Older chips may need up to 100 us to respond. The ATMEL 29C020
103 * needs 10 ms according to the data sheet, but it has been tested
104 * to work reliably with 20 us. Allow a factor of 2 safety margin.
105 */
106 myusec_delay(40);
rminnich8d3ff912003-10-25 17:01:29 +0000107
ollie5b621572004-03-20 16:46:10 +0000108 /* Read product ID */
uwef6641642007-05-09 10:17:44 +0000109 id1 = *(volatile uint8_t *)bios;
110 id2 = *(volatile uint8_t *)(bios + 0x01);
hailfinger428f2012007-12-31 01:49:00 +0000111 largeid1 = id1;
112 largeid2 = id2;
113
114 /* Check if it is a continuation ID, this should be a while loop. */
115 if (id1 == 0x7F) {
116 largeid1 <<= 8;
117 id1 = *(volatile uint8_t *)(bios + 0x100);
118 largeid1 |= id1;
119 }
120 if (id2 == 0x7F) {
121 largeid2 <<= 8;
122 id2 = *(volatile uint8_t *)(bios + 0x101);
123 largeid2 |= id2;
124 }
rminnich8d3ff912003-10-25 17:01:29 +0000125
ollie5b621572004-03-20 16:46:10 +0000126 /* Issue JEDEC Product ID Exit command */
uwef6641642007-05-09 10:17:44 +0000127 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
ollie5b621572004-03-20 16:46:10 +0000128 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000129 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
ollie5b621572004-03-20 16:46:10 +0000130 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000131 *(volatile uint8_t *)(bios + 0x5555) = 0xF0;
hailfinger9c715292007-11-13 14:56:54 +0000132 myusec_delay(40);
rminnich8d3ff912003-10-25 17:01:29 +0000133
hailfinger79cf3672008-05-14 12:03:06 +0000134 printf_debug("%s: id1 0x%x, id2 0x%x", __FUNCTION__, largeid1, largeid2);
135 if (!oddparity(id1))
136 printf_debug(", id1 parity violation");
137 printf_debug("\n");
hailfinger428f2012007-12-31 01:49:00 +0000138 if (largeid1 == flash->manufacture_id && largeid2 == flash->model_id)
ollie5b621572004-03-20 16:46:10 +0000139 return 1;
rminnich8d3ff912003-10-25 17:01:29 +0000140
ollie5b621572004-03-20 16:46:10 +0000141 return 0;
olliea3def632004-03-19 22:10:07 +0000142}
143
ollie6a600992005-11-26 21:55:36 +0000144int erase_sector_jedec(volatile uint8_t *bios, unsigned int page)
olliea3def632004-03-19 22:10:07 +0000145{
ollie5b621572004-03-20 16:46:10 +0000146 /* Issue the Sector Erase command */
uwef6641642007-05-09 10:17:44 +0000147 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
olliea3def632004-03-19 22:10:07 +0000148 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000149 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
olliea3def632004-03-19 22:10:07 +0000150 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000151 *(volatile uint8_t *)(bios + 0x5555) = 0x80;
olliea3def632004-03-19 22:10:07 +0000152 myusec_delay(10);
ollie0eb62d62004-12-08 20:10:01 +0000153
uwef6641642007-05-09 10:17:44 +0000154 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
olliea3def632004-03-19 22:10:07 +0000155 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000156 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
olliea3def632004-03-19 22:10:07 +0000157 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000158 *(volatile uint8_t *)(bios + page) = 0x30;
ollie5b621572004-03-20 16:46:10 +0000159 myusec_delay(10);
160
olliea3def632004-03-19 22:10:07 +0000161 /* wait for Toggle bit ready */
162 toggle_ready_jedec(bios);
163
uwebe4477b2007-08-23 16:08:21 +0000164 return 0;
rminnich8d3ff912003-10-25 17:01:29 +0000165}
olliea4302802004-12-07 03:15:51 +0000166
ollie6a600992005-11-26 21:55:36 +0000167int erase_block_jedec(volatile uint8_t *bios, unsigned int block)
rminnichdfcbaa72004-09-30 16:37:01 +0000168{
rminnichdfcbaa72004-09-30 16:37:01 +0000169 /* Issue the Sector Erase command */
uwef6641642007-05-09 10:17:44 +0000170 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
rminnichdfcbaa72004-09-30 16:37:01 +0000171 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000172 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
rminnichdfcbaa72004-09-30 16:37:01 +0000173 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000174 *(volatile uint8_t *)(bios + 0x5555) = 0x80;
rminnichdfcbaa72004-09-30 16:37:01 +0000175 myusec_delay(10);
ollie0eb62d62004-12-08 20:10:01 +0000176
uwef6641642007-05-09 10:17:44 +0000177 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
rminnichdfcbaa72004-09-30 16:37:01 +0000178 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000179 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
rminnichdfcbaa72004-09-30 16:37:01 +0000180 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000181 *(volatile uint8_t *)(bios + block) = 0x50;
rminnichdfcbaa72004-09-30 16:37:01 +0000182 myusec_delay(10);
183
184 /* wait for Toggle bit ready */
185 toggle_ready_jedec(bios);
186
uwebe4477b2007-08-23 16:08:21 +0000187 return 0;
rminnichdfcbaa72004-09-30 16:37:01 +0000188}
rminnich8d3ff912003-10-25 17:01:29 +0000189
ollie5b621572004-03-20 16:46:10 +0000190int erase_chip_jedec(struct flashchip *flash)
rminnich8d3ff912003-10-25 17:01:29 +0000191{
stepan7cd945e2007-05-23 17:20:56 +0000192 volatile uint8_t *bios = flash->virtual_memory;
rminnich8d3ff912003-10-25 17:01:29 +0000193
ollie5b621572004-03-20 16:46:10 +0000194 /* Issue the JEDEC Chip Erase command */
uwef6641642007-05-09 10:17:44 +0000195 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
rminnich8d3ff912003-10-25 17:01:29 +0000196 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000197 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
olliea3def632004-03-19 22:10:07 +0000198 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000199 *(volatile uint8_t *)(bios + 0x5555) = 0x80;
olliea3def632004-03-19 22:10:07 +0000200 myusec_delay(10);
ollie0eb62d62004-12-08 20:10:01 +0000201
uwef6641642007-05-09 10:17:44 +0000202 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
olliea3def632004-03-19 22:10:07 +0000203 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000204 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
olliea3def632004-03-19 22:10:07 +0000205 myusec_delay(10);
uwef6641642007-05-09 10:17:44 +0000206 *(volatile uint8_t *)(bios + 0x5555) = 0x10;
olliea3def632004-03-19 22:10:07 +0000207 myusec_delay(10);
208
rminnich8d3ff912003-10-25 17:01:29 +0000209 toggle_ready_jedec(bios);
210
uwebe4477b2007-08-23 16:08:21 +0000211 return 0;
rminnich8d3ff912003-10-25 17:01:29 +0000212}
213
ollie6a600992005-11-26 21:55:36 +0000214int write_page_write_jedec(volatile uint8_t *bios, uint8_t *src,
215 volatile uint8_t *dst, int page_size)
rminnich8d3ff912003-10-25 17:01:29 +0000216{
stepan7abc6322006-11-22 00:29:51 +0000217 int i, tried = 0, start_index = 0, ok;
218 volatile uint8_t *d = dst;
219 uint8_t *s = src;
rminnich8d3ff912003-10-25 17:01:29 +0000220
stepan7abc6322006-11-22 00:29:51 +0000221retry:
ollie5b621572004-03-20 16:46:10 +0000222 /* Issue JEDEC Data Unprotect comand */
uwef6641642007-05-09 10:17:44 +0000223 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
224 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
225 *(volatile uint8_t *)(bios + 0x5555) = 0xA0;
ollie5b621572004-03-20 16:46:10 +0000226
olliea4302802004-12-07 03:15:51 +0000227 /* transfer data from source to destination */
stepan7abc6322006-11-22 00:29:51 +0000228 for (i = start_index; i < page_size; i++) {
olliea4302802004-12-07 03:15:51 +0000229 /* If the data is 0xFF, don't program it */
uwef6641642007-05-09 10:17:44 +0000230 if (*src != 0xFF)
stepan7abc6322006-11-22 00:29:51 +0000231 *dst = *src;
232 dst++;
233 src++;
ollie5b621572004-03-20 16:46:10 +0000234 }
235
ollie5b621572004-03-20 16:46:10 +0000236 toggle_ready_jedec(dst - 1);
olliea4302802004-12-07 03:15:51 +0000237
stepan7abc6322006-11-22 00:29:51 +0000238 dst = d;
239 src = s;
240 ok = 1;
241 for (i = 0; i < page_size; i++) {
uwef6641642007-05-09 10:17:44 +0000242 if (*dst != *src) {
stepan7abc6322006-11-22 00:29:51 +0000243 ok = 0;
244 break;
245 }
246 dst++;
247 src++;
248 }
uwef6641642007-05-09 10:17:44 +0000249
stepan7abc6322006-11-22 00:29:51 +0000250 if (!ok && tried++ < MAX_REFLASH_TRIES) {
251 start_index = i;
uwef6641642007-05-09 10:17:44 +0000252 goto retry;
253 }
stepan7abc6322006-11-22 00:29:51 +0000254 if (!ok) {
uwef6641642007-05-09 10:17:44 +0000255 fprintf(stderr, " page %d failed!\n",
256 (unsigned int)(d - bios) / page_size);
stepan7abc6322006-11-22 00:29:51 +0000257 }
uwebe4477b2007-08-23 16:08:21 +0000258 return !ok;
ollie5b621572004-03-20 16:46:10 +0000259}
260
ollie6a600992005-11-26 21:55:36 +0000261int write_byte_program_jedec(volatile uint8_t *bios, uint8_t *src,
262 volatile uint8_t *dst)
olliebb5917a2004-03-22 22:19:17 +0000263{
stepan7abc6322006-11-22 00:29:51 +0000264 int tried = 0, ok = 1;
olliedd68ded2004-12-08 02:10:33 +0000265
olliea4302802004-12-07 03:15:51 +0000266 /* If the data is 0xFF, don't program it */
olliebb5917a2004-03-22 22:19:17 +0000267 if (*src == 0xFF) {
stepan7abc6322006-11-22 00:29:51 +0000268 return -1;
olliebb5917a2004-03-22 22:19:17 +0000269 }
olliea4302802004-12-07 03:15:51 +0000270
olliedd68ded2004-12-08 02:10:33 +0000271retry:
olliebb5917a2004-03-22 22:19:17 +0000272 /* Issue JEDEC Byte Program command */
uwef6641642007-05-09 10:17:44 +0000273 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
274 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
275 *(volatile uint8_t *)(bios + 0x5555) = 0xA0;
olliea4302802004-12-07 03:15:51 +0000276
277 /* transfer data from source to destination */
olliebb5917a2004-03-22 22:19:17 +0000278 *dst = *src;
279 toggle_ready_jedec(bios);
ollief1845bd2004-03-27 00:18:15 +0000280
stepan7abc6322006-11-22 00:29:51 +0000281 if (*dst != *src && tried++ < MAX_REFLASH_TRIES) {
uwef6641642007-05-09 10:17:44 +0000282 goto retry;
283 }
olliedd68ded2004-12-08 02:10:33 +0000284
stepan7abc6322006-11-22 00:29:51 +0000285 if (tried >= MAX_REFLASH_TRIES)
uwef6641642007-05-09 10:17:44 +0000286 ok = 0;
stepan7abc6322006-11-22 00:29:51 +0000287
uwebe4477b2007-08-23 16:08:21 +0000288 return !ok;
olliebb5917a2004-03-22 22:19:17 +0000289}
290
ollie6a600992005-11-26 21:55:36 +0000291int write_sector_jedec(volatile uint8_t *bios, uint8_t *src,
292 volatile uint8_t *dst, unsigned int page_size)
ollie5b621572004-03-20 16:46:10 +0000293{
294 int i;
ollie5b621572004-03-20 16:46:10 +0000295
296 for (i = 0; i < page_size; i++) {
ollief1845bd2004-03-27 00:18:15 +0000297 write_byte_program_jedec(bios, src, dst);
298 dst++, src++;
ollie5b621572004-03-20 16:46:10 +0000299 }
300
uwebe4477b2007-08-23 16:08:21 +0000301 return 0;
ollie5b621572004-03-20 16:46:10 +0000302}
303
ollie6a600992005-11-26 21:55:36 +0000304int write_jedec(struct flashchip *flash, uint8_t *buf)
ollie5b621572004-03-20 16:46:10 +0000305{
306 int i;
olliebb5917a2004-03-22 22:19:17 +0000307 int total_size = flash->total_size * 1024;
308 int page_size = flash->page_size;
stepan7cd945e2007-05-23 17:20:56 +0000309 volatile uint8_t *bios = flash->virtual_memory;
ollie5b621572004-03-20 16:46:10 +0000310
311 erase_chip_jedec(flash);
uwef6641642007-05-09 10:17:44 +0000312 // dumb check if erase was successful.
313 for (i = 0; i < total_size; i++) {
314 if (bios[i] != (uint8_t) 0xff) {
uwefd2d0fe2007-10-17 23:55:15 +0000315 printf("ERASE FAILED @%d, val %02x!\n", i, bios[i]);
uwef6641642007-05-09 10:17:44 +0000316 return -1;
317 }
318 }
stepan7abc6322006-11-22 00:29:51 +0000319
uwefd2d0fe2007-10-17 23:55:15 +0000320 printf("Programming page: ");
ollie5b621572004-03-20 16:46:10 +0000321 for (i = 0; i < total_size / page_size; i++) {
322 printf("%04d at address: 0x%08x", i, i * page_size);
ollief1845bd2004-03-27 00:18:15 +0000323 write_page_write_jedec(bios, buf + i * page_size,
324 bios + i * page_size, page_size);
olliebb5917a2004-03-22 22:19:17 +0000325 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 +0000326 }
327 printf("\n");
ollie5b621572004-03-20 16:46:10 +0000328 protect_jedec(bios);
rminnich8d3ff912003-10-25 17:01:29 +0000329
uwebe4477b2007-08-23 16:08:21 +0000330 return 0;
rminnich8d3ff912003-10-25 17:01:29 +0000331}