blob: bfbd93e1ddaf6c35da143520ca652078eeca7f5a [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
hailfinger1ff6e362009-03-06 22:26:00 +000043 tmp1 = chip_readb(dst) & 0x40;
uwedf467892007-08-23 10:20:40 +000044
45 while (i++ < 0xFFFFFFF) {
hailfinger1ff6e362009-03-06 22:26:00 +000046 tmp2 = chip_readb(dst) & 0x40;
uwedf467892007-08-23 10:20:40 +000047 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) {
hailfinger1ff6e362009-03-06 22:26:00 +000062 tmp = chip_readb(dst) & 0x80;
uwedf467892007-08-23 10:20:40 +000063 if (tmp == data) {
64 break;
65 }
66 }
67}
68
69void unprotect_jedec(volatile uint8_t *bios)
70{
hailfinger1ff6e362009-03-06 22:26:00 +000071 chip_writeb(0xAA, bios + 0x5555);
72 chip_writeb(0x55, bios + 0x2AAA);
73 chip_writeb(0x80, bios + 0x5555);
74 chip_writeb(0xAA, bios + 0x5555);
75 chip_writeb(0x55, bios + 0x2AAA);
76 chip_writeb(0x20, bios + 0x5555);
uwedf467892007-08-23 10:20:40 +000077
78 usleep(200);
79}
80
81void protect_jedec(volatile uint8_t *bios)
82{
hailfinger1ff6e362009-03-06 22:26:00 +000083 chip_writeb(0xAA, bios + 0x5555);
84 chip_writeb(0x55, bios + 0x2AAA);
85 chip_writeb(0xA0, bios + 0x5555);
uwedf467892007-08-23 10:20:40 +000086
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;
hailfinger027d7d92009-05-11 14:40:31 +000095 uint32_t flashcontent1, flashcontent2;
rminnich8d3ff912003-10-25 17:01:29 +000096
ollie5b621572004-03-20 16:46:10 +000097 /* Issue JEDEC Product ID Entry command */
hailfinger1ff6e362009-03-06 22:26:00 +000098 chip_writeb(0xAA, bios + 0x5555);
ollie5b621572004-03-20 16:46:10 +000099 myusec_delay(10);
hailfinger1ff6e362009-03-06 22:26:00 +0000100 chip_writeb(0x55, bios + 0x2AAA);
ollie5b621572004-03-20 16:46:10 +0000101 myusec_delay(10);
hailfinger1ff6e362009-03-06 22:26:00 +0000102 chip_writeb(0x90, bios + 0x5555);
hailfinger9c715292007-11-13 14:56:54 +0000103 /* Older chips may need up to 100 us to respond. The ATMEL 29C020
stugeeeb86d02008-06-24 02:09:09 +0000104 * needs 10 ms according to the data sheet.
hailfinger9c715292007-11-13 14:56:54 +0000105 */
stugeeeb86d02008-06-24 02:09:09 +0000106 myusec_delay(10000);
rminnich8d3ff912003-10-25 17:01:29 +0000107
ollie5b621572004-03-20 16:46:10 +0000108 /* Read product ID */
hailfinger1ff6e362009-03-06 22:26:00 +0000109 id1 = chip_readb(bios);
110 id2 = chip_readb(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;
hailfinger1ff6e362009-03-06 22:26:00 +0000117 id1 = chip_readb(bios + 0x100);
hailfinger428f2012007-12-31 01:49:00 +0000118 largeid1 |= id1;
119 }
120 if (id2 == 0x7F) {
121 largeid2 <<= 8;
hailfinger1ff6e362009-03-06 22:26:00 +0000122 id2 = chip_readb(bios + 0x101);
hailfinger428f2012007-12-31 01:49:00 +0000123 largeid2 |= id2;
124 }
rminnich8d3ff912003-10-25 17:01:29 +0000125
ollie5b621572004-03-20 16:46:10 +0000126 /* Issue JEDEC Product ID Exit command */
hailfinger1ff6e362009-03-06 22:26:00 +0000127 chip_writeb(0xAA, bios + 0x5555);
ollie5b621572004-03-20 16:46:10 +0000128 myusec_delay(10);
hailfinger1ff6e362009-03-06 22:26:00 +0000129 chip_writeb(0x55, bios + 0x2AAA);
ollie5b621572004-03-20 16:46:10 +0000130 myusec_delay(10);
hailfinger1ff6e362009-03-06 22:26:00 +0000131 chip_writeb(0xF0, bios + 0x5555);
hailfinger9c715292007-11-13 14:56:54 +0000132 myusec_delay(40);
rminnich8d3ff912003-10-25 17:01:29 +0000133
stugef45dc842009-01-25 23:52:45 +0000134 printf_debug("%s: id1 0x%02x, id2 0x%02x", __FUNCTION__, largeid1, largeid2);
hailfinger79cf3672008-05-14 12:03:06 +0000135 if (!oddparity(id1))
136 printf_debug(", id1 parity violation");
hailfinger027d7d92009-05-11 14:40:31 +0000137
138 /* Read the product ID location again. We should now see normal flash contents. */
139 flashcontent1 = chip_readb(bios);
140 flashcontent2 = chip_readb(bios + 0x01);
141
142 /* Check if it is a continuation ID, this should be a while loop. */
143 if (flashcontent1 == 0x7F) {
144 flashcontent1 <<= 8;
145 flashcontent1 |= chip_readb(bios + 0x100);
146 }
147 if (flashcontent2 == 0x7F) {
148 flashcontent2 <<= 8;
149 flashcontent2 |= chip_readb(bios + 0x101);
150 }
151
152 if (largeid1 == flashcontent1)
153 printf_debug(", id1 is normal flash content");
154 if (largeid2 == flashcontent2)
155 printf_debug(", id2 is normal flash content");
156
hailfinger79cf3672008-05-14 12:03:06 +0000157 printf_debug("\n");
hailfinger428f2012007-12-31 01:49:00 +0000158 if (largeid1 == flash->manufacture_id && largeid2 == flash->model_id)
ollie5b621572004-03-20 16:46:10 +0000159 return 1;
rminnich8d3ff912003-10-25 17:01:29 +0000160
ollie5b621572004-03-20 16:46:10 +0000161 return 0;
olliea3def632004-03-19 22:10:07 +0000162}
163
ollie6a600992005-11-26 21:55:36 +0000164int erase_sector_jedec(volatile uint8_t *bios, unsigned int page)
olliea3def632004-03-19 22:10:07 +0000165{
ollie5b621572004-03-20 16:46:10 +0000166 /* Issue the Sector Erase command */
hailfinger1ff6e362009-03-06 22:26:00 +0000167 chip_writeb(0xAA, bios + 0x5555);
olliea3def632004-03-19 22:10:07 +0000168 myusec_delay(10);
hailfinger1ff6e362009-03-06 22:26:00 +0000169 chip_writeb(0x55, bios + 0x2AAA);
olliea3def632004-03-19 22:10:07 +0000170 myusec_delay(10);
hailfinger1ff6e362009-03-06 22:26:00 +0000171 chip_writeb(0x80, bios + 0x5555);
olliea3def632004-03-19 22:10:07 +0000172 myusec_delay(10);
ollie0eb62d62004-12-08 20:10:01 +0000173
hailfinger1ff6e362009-03-06 22:26:00 +0000174 chip_writeb(0xAA, bios + 0x5555);
olliea3def632004-03-19 22:10:07 +0000175 myusec_delay(10);
hailfinger1ff6e362009-03-06 22:26:00 +0000176 chip_writeb(0x55, bios + 0x2AAA);
olliea3def632004-03-19 22:10:07 +0000177 myusec_delay(10);
hailfinger1ff6e362009-03-06 22:26:00 +0000178 chip_writeb(0x30, bios + page);
ollie5b621572004-03-20 16:46:10 +0000179 myusec_delay(10);
180
olliea3def632004-03-19 22:10:07 +0000181 /* wait for Toggle bit ready */
182 toggle_ready_jedec(bios);
183
uwebe4477b2007-08-23 16:08:21 +0000184 return 0;
rminnich8d3ff912003-10-25 17:01:29 +0000185}
olliea4302802004-12-07 03:15:51 +0000186
ollie6a600992005-11-26 21:55:36 +0000187int erase_block_jedec(volatile uint8_t *bios, unsigned int block)
rminnichdfcbaa72004-09-30 16:37:01 +0000188{
rminnichdfcbaa72004-09-30 16:37:01 +0000189 /* Issue the Sector Erase command */
hailfinger1ff6e362009-03-06 22:26:00 +0000190 chip_writeb(0xAA, bios + 0x5555);
rminnichdfcbaa72004-09-30 16:37:01 +0000191 myusec_delay(10);
hailfinger1ff6e362009-03-06 22:26:00 +0000192 chip_writeb(0x55, bios + 0x2AAA);
rminnichdfcbaa72004-09-30 16:37:01 +0000193 myusec_delay(10);
hailfinger1ff6e362009-03-06 22:26:00 +0000194 chip_writeb(0x80, bios + 0x5555);
rminnichdfcbaa72004-09-30 16:37:01 +0000195 myusec_delay(10);
ollie0eb62d62004-12-08 20:10:01 +0000196
hailfinger1ff6e362009-03-06 22:26:00 +0000197 chip_writeb(0xAA, bios + 0x5555);
rminnichdfcbaa72004-09-30 16:37:01 +0000198 myusec_delay(10);
hailfinger1ff6e362009-03-06 22:26:00 +0000199 chip_writeb(0x55, bios + 0x2AAA);
rminnichdfcbaa72004-09-30 16:37:01 +0000200 myusec_delay(10);
hailfinger1ff6e362009-03-06 22:26:00 +0000201 chip_writeb(0x50, bios + block);
rminnichdfcbaa72004-09-30 16:37:01 +0000202 myusec_delay(10);
203
204 /* wait for Toggle bit ready */
205 toggle_ready_jedec(bios);
206
uwebe4477b2007-08-23 16:08:21 +0000207 return 0;
rminnichdfcbaa72004-09-30 16:37:01 +0000208}
rminnich8d3ff912003-10-25 17:01:29 +0000209
ollie5b621572004-03-20 16:46:10 +0000210int erase_chip_jedec(struct flashchip *flash)
rminnich8d3ff912003-10-25 17:01:29 +0000211{
stepan7cd945e2007-05-23 17:20:56 +0000212 volatile uint8_t *bios = flash->virtual_memory;
rminnich8d3ff912003-10-25 17:01:29 +0000213
ollie5b621572004-03-20 16:46:10 +0000214 /* Issue the JEDEC Chip Erase command */
hailfinger1ff6e362009-03-06 22:26:00 +0000215 chip_writeb(0xAA, bios + 0x5555);
rminnich8d3ff912003-10-25 17:01:29 +0000216 myusec_delay(10);
hailfinger1ff6e362009-03-06 22:26:00 +0000217 chip_writeb(0x55, bios + 0x2AAA);
olliea3def632004-03-19 22:10:07 +0000218 myusec_delay(10);
hailfinger1ff6e362009-03-06 22:26:00 +0000219 chip_writeb(0x80, bios + 0x5555);
olliea3def632004-03-19 22:10:07 +0000220 myusec_delay(10);
ollie0eb62d62004-12-08 20:10:01 +0000221
hailfinger1ff6e362009-03-06 22:26:00 +0000222 chip_writeb(0xAA, bios + 0x5555);
olliea3def632004-03-19 22:10:07 +0000223 myusec_delay(10);
hailfinger1ff6e362009-03-06 22:26:00 +0000224 chip_writeb(0x55, bios + 0x2AAA);
olliea3def632004-03-19 22:10:07 +0000225 myusec_delay(10);
hailfinger1ff6e362009-03-06 22:26:00 +0000226 chip_writeb(0x10, bios + 0x5555);
olliea3def632004-03-19 22:10:07 +0000227 myusec_delay(10);
228
rminnich8d3ff912003-10-25 17:01:29 +0000229 toggle_ready_jedec(bios);
230
uwebe4477b2007-08-23 16:08:21 +0000231 return 0;
rminnich8d3ff912003-10-25 17:01:29 +0000232}
233
ollie6a600992005-11-26 21:55:36 +0000234int write_page_write_jedec(volatile uint8_t *bios, uint8_t *src,
235 volatile uint8_t *dst, int page_size)
rminnich8d3ff912003-10-25 17:01:29 +0000236{
stepan7abc6322006-11-22 00:29:51 +0000237 int i, tried = 0, start_index = 0, ok;
238 volatile uint8_t *d = dst;
239 uint8_t *s = src;
rminnich8d3ff912003-10-25 17:01:29 +0000240
stepan7abc6322006-11-22 00:29:51 +0000241retry:
ollie5b621572004-03-20 16:46:10 +0000242 /* Issue JEDEC Data Unprotect comand */
hailfinger1ff6e362009-03-06 22:26:00 +0000243 chip_writeb(0xAA, bios + 0x5555);
244 chip_writeb(0x55, bios + 0x2AAA);
245 chip_writeb(0xA0, bios + 0x5555);
ollie5b621572004-03-20 16:46:10 +0000246
olliea4302802004-12-07 03:15:51 +0000247 /* transfer data from source to destination */
stepan7abc6322006-11-22 00:29:51 +0000248 for (i = start_index; i < page_size; i++) {
olliea4302802004-12-07 03:15:51 +0000249 /* If the data is 0xFF, don't program it */
uwef6641642007-05-09 10:17:44 +0000250 if (*src != 0xFF)
hailfinger1ff6e362009-03-06 22:26:00 +0000251 chip_writeb(*src, dst);
stepan7abc6322006-11-22 00:29:51 +0000252 dst++;
253 src++;
ollie5b621572004-03-20 16:46:10 +0000254 }
255
ollie5b621572004-03-20 16:46:10 +0000256 toggle_ready_jedec(dst - 1);
olliea4302802004-12-07 03:15:51 +0000257
stepan7abc6322006-11-22 00:29:51 +0000258 dst = d;
259 src = s;
260 ok = 1;
261 for (i = 0; i < page_size; i++) {
hailfinger1ff6e362009-03-06 22:26:00 +0000262 if (chip_readb(dst) != *src) {
stepan7abc6322006-11-22 00:29:51 +0000263 ok = 0;
264 break;
265 }
266 dst++;
267 src++;
268 }
uwef6641642007-05-09 10:17:44 +0000269
stepan7abc6322006-11-22 00:29:51 +0000270 if (!ok && tried++ < MAX_REFLASH_TRIES) {
271 start_index = i;
uwef6641642007-05-09 10:17:44 +0000272 goto retry;
273 }
stepan7abc6322006-11-22 00:29:51 +0000274 if (!ok) {
uwef6641642007-05-09 10:17:44 +0000275 fprintf(stderr, " page %d failed!\n",
276 (unsigned int)(d - bios) / page_size);
stepan7abc6322006-11-22 00:29:51 +0000277 }
uwebe4477b2007-08-23 16:08:21 +0000278 return !ok;
ollie5b621572004-03-20 16:46:10 +0000279}
280
ollie6a600992005-11-26 21:55:36 +0000281int write_byte_program_jedec(volatile uint8_t *bios, uint8_t *src,
282 volatile uint8_t *dst)
olliebb5917a2004-03-22 22:19:17 +0000283{
stepan7abc6322006-11-22 00:29:51 +0000284 int tried = 0, ok = 1;
olliedd68ded2004-12-08 02:10:33 +0000285
olliea4302802004-12-07 03:15:51 +0000286 /* If the data is 0xFF, don't program it */
olliebb5917a2004-03-22 22:19:17 +0000287 if (*src == 0xFF) {
stepan7abc6322006-11-22 00:29:51 +0000288 return -1;
olliebb5917a2004-03-22 22:19:17 +0000289 }
olliea4302802004-12-07 03:15:51 +0000290
olliedd68ded2004-12-08 02:10:33 +0000291retry:
olliebb5917a2004-03-22 22:19:17 +0000292 /* Issue JEDEC Byte Program command */
hailfinger1ff6e362009-03-06 22:26:00 +0000293 chip_writeb(0xAA, bios + 0x5555);
294 chip_writeb(0x55, bios + 0x2AAA);
295 chip_writeb(0xA0, bios + 0x5555);
olliea4302802004-12-07 03:15:51 +0000296
297 /* transfer data from source to destination */
hailfinger1ff6e362009-03-06 22:26:00 +0000298 chip_writeb(*src, dst);
olliebb5917a2004-03-22 22:19:17 +0000299 toggle_ready_jedec(bios);
ollief1845bd2004-03-27 00:18:15 +0000300
hailfinger1ff6e362009-03-06 22:26:00 +0000301 if (chip_readb(dst) != *src && tried++ < MAX_REFLASH_TRIES) {
uwef6641642007-05-09 10:17:44 +0000302 goto retry;
303 }
olliedd68ded2004-12-08 02:10:33 +0000304
stepan7abc6322006-11-22 00:29:51 +0000305 if (tried >= MAX_REFLASH_TRIES)
uwef6641642007-05-09 10:17:44 +0000306 ok = 0;
stepan7abc6322006-11-22 00:29:51 +0000307
uwebe4477b2007-08-23 16:08:21 +0000308 return !ok;
olliebb5917a2004-03-22 22:19:17 +0000309}
310
ollie6a600992005-11-26 21:55:36 +0000311int write_sector_jedec(volatile uint8_t *bios, uint8_t *src,
312 volatile uint8_t *dst, unsigned int page_size)
ollie5b621572004-03-20 16:46:10 +0000313{
314 int i;
ollie5b621572004-03-20 16:46:10 +0000315
316 for (i = 0; i < page_size; i++) {
ollief1845bd2004-03-27 00:18:15 +0000317 write_byte_program_jedec(bios, src, dst);
318 dst++, src++;
ollie5b621572004-03-20 16:46:10 +0000319 }
320
uwebe4477b2007-08-23 16:08:21 +0000321 return 0;
ollie5b621572004-03-20 16:46:10 +0000322}
323
ollie6a600992005-11-26 21:55:36 +0000324int write_jedec(struct flashchip *flash, uint8_t *buf)
ollie5b621572004-03-20 16:46:10 +0000325{
326 int i;
olliebb5917a2004-03-22 22:19:17 +0000327 int total_size = flash->total_size * 1024;
328 int page_size = flash->page_size;
stepan7cd945e2007-05-23 17:20:56 +0000329 volatile uint8_t *bios = flash->virtual_memory;
ollie5b621572004-03-20 16:46:10 +0000330
331 erase_chip_jedec(flash);
uwef6641642007-05-09 10:17:44 +0000332 // dumb check if erase was successful.
333 for (i = 0; i < total_size; i++) {
334 if (bios[i] != (uint8_t) 0xff) {
uwefd2d0fe2007-10-17 23:55:15 +0000335 printf("ERASE FAILED @%d, val %02x!\n", i, bios[i]);
uwef6641642007-05-09 10:17:44 +0000336 return -1;
337 }
338 }
stepan7abc6322006-11-22 00:29:51 +0000339
uwefd2d0fe2007-10-17 23:55:15 +0000340 printf("Programming page: ");
ollie5b621572004-03-20 16:46:10 +0000341 for (i = 0; i < total_size / page_size; i++) {
342 printf("%04d at address: 0x%08x", i, i * page_size);
ollief1845bd2004-03-27 00:18:15 +0000343 write_page_write_jedec(bios, buf + i * page_size,
344 bios + i * page_size, page_size);
olliebb5917a2004-03-22 22:19:17 +0000345 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 +0000346 }
347 printf("\n");
ollie5b621572004-03-20 16:46:10 +0000348 protect_jedec(bios);
rminnich8d3ff912003-10-25 17:01:29 +0000349
uwebe4477b2007-08-23 16:08:21 +0000350 return 0;
rminnich8d3ff912003-10-25 17:01:29 +0000351}