blob: 958875194a272b0d872c13090219fc2037a93505 [file] [log] [blame]
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +00001/*
Uwe Hermannd1107642007-08-29 17:52:32 +00002 * This file is part of the flashrom project.
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +00003 *
Uwe Hermannd22a1d42007-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>
Carl-Daniel Hailfingerae8afa92007-12-31 01:49:00 +00007 * Copyright (C) 2007 Carl-Daniel Hailfinger
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +00008 *
Uwe Hermannd1107642007-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.
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000013 *
Uwe Hermannd1107642007-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.
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000018 *
Uwe Hermannd1107642007-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
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000022 */
23
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000024#include <stdio.h>
Ollie Lho184a4042005-11-26 21:55:36 +000025#include <stdint.h>
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000026#include "flash.h"
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000027
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +000028#define MAX_REFLASH_TRIES 0x10
29
Uwe Hermann51582f22007-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
Ollie Lho761bf1b2004-03-20 16:46:10 +000082int probe_jedec(struct flashchip *flash)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000083{
Stefan Reinauerce532972007-05-23 17:20:56 +000084 volatile uint8_t *bios = flash->virtual_memory;
Ollie Lho184a4042005-11-26 21:55:36 +000085 uint8_t id1, id2;
Carl-Daniel Hailfingerae8afa92007-12-31 01:49:00 +000086 uint32_t largeid1, largeid2;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000087
Ollie Lho761bf1b2004-03-20 16:46:10 +000088 /* Issue JEDEC Product ID Entry command */
Uwe Hermanna7e05482007-05-09 10:17:44 +000089 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
Ollie Lho761bf1b2004-03-20 16:46:10 +000090 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +000091 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
Ollie Lho761bf1b2004-03-20 16:46:10 +000092 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +000093 *(volatile uint8_t *)(bios + 0x5555) = 0x90;
Carl-Daniel Hailfinger03d28262007-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);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000099
Ollie Lho761bf1b2004-03-20 16:46:10 +0000100 /* Read product ID */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000101 id1 = *(volatile uint8_t *)bios;
102 id2 = *(volatile uint8_t *)(bios + 0x01);
Carl-Daniel Hailfingerae8afa92007-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 }
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000117
Ollie Lho761bf1b2004-03-20 16:46:10 +0000118 /* Issue JEDEC Product ID Exit command */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000119 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000120 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000121 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000122 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000123 *(volatile uint8_t *)(bios + 0x5555) = 0xF0;
Carl-Daniel Hailfinger03d28262007-11-13 14:56:54 +0000124 myusec_delay(40);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000125
Carl-Daniel Hailfingerae8afa92007-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)
Ollie Lho761bf1b2004-03-20 16:46:10 +0000128 return 1;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000129
Ollie Lho761bf1b2004-03-20 16:46:10 +0000130 return 0;
Ollie Lho73eca802004-03-19 22:10:07 +0000131}
132
Ollie Lho184a4042005-11-26 21:55:36 +0000133int erase_sector_jedec(volatile uint8_t *bios, unsigned int page)
Ollie Lho73eca802004-03-19 22:10:07 +0000134{
Ollie Lho761bf1b2004-03-20 16:46:10 +0000135 /* Issue the Sector Erase command */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000136 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
Ollie Lho73eca802004-03-19 22:10:07 +0000137 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000138 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
Ollie Lho73eca802004-03-19 22:10:07 +0000139 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000140 *(volatile uint8_t *)(bios + 0x5555) = 0x80;
Ollie Lho73eca802004-03-19 22:10:07 +0000141 myusec_delay(10);
Ollie Lhoefa28582004-12-08 20:10:01 +0000142
Uwe Hermanna7e05482007-05-09 10:17:44 +0000143 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
Ollie Lho73eca802004-03-19 22:10:07 +0000144 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000145 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
Ollie Lho73eca802004-03-19 22:10:07 +0000146 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000147 *(volatile uint8_t *)(bios + page) = 0x30;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000148 myusec_delay(10);
149
Ollie Lho73eca802004-03-19 22:10:07 +0000150 /* wait for Toggle bit ready */
151 toggle_ready_jedec(bios);
152
Uwe Hermannffec5f32007-08-23 16:08:21 +0000153 return 0;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000154}
Ollie Lho98bea8a2004-12-07 03:15:51 +0000155
Ollie Lho184a4042005-11-26 21:55:36 +0000156int erase_block_jedec(volatile uint8_t *bios, unsigned int block)
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000157{
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000158 /* Issue the Sector Erase command */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000159 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000160 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000161 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000162 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000163 *(volatile uint8_t *)(bios + 0x5555) = 0x80;
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000164 myusec_delay(10);
Ollie Lhoefa28582004-12-08 20:10:01 +0000165
Uwe Hermanna7e05482007-05-09 10:17:44 +0000166 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000167 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000168 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000169 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000170 *(volatile uint8_t *)(bios + block) = 0x50;
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000171 myusec_delay(10);
172
173 /* wait for Toggle bit ready */
174 toggle_ready_jedec(bios);
175
Uwe Hermannffec5f32007-08-23 16:08:21 +0000176 return 0;
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000177}
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000178
Ollie Lho761bf1b2004-03-20 16:46:10 +0000179int erase_chip_jedec(struct flashchip *flash)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000180{
Stefan Reinauerce532972007-05-23 17:20:56 +0000181 volatile uint8_t *bios = flash->virtual_memory;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000182
Ollie Lho761bf1b2004-03-20 16:46:10 +0000183 /* Issue the JEDEC Chip Erase command */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000184 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000185 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000186 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
Ollie Lho73eca802004-03-19 22:10:07 +0000187 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000188 *(volatile uint8_t *)(bios + 0x5555) = 0x80;
Ollie Lho73eca802004-03-19 22:10:07 +0000189 myusec_delay(10);
Ollie Lhoefa28582004-12-08 20:10:01 +0000190
Uwe Hermanna7e05482007-05-09 10:17:44 +0000191 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
Ollie Lho73eca802004-03-19 22:10:07 +0000192 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000193 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
Ollie Lho73eca802004-03-19 22:10:07 +0000194 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000195 *(volatile uint8_t *)(bios + 0x5555) = 0x10;
Ollie Lho73eca802004-03-19 22:10:07 +0000196 myusec_delay(10);
197
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000198 toggle_ready_jedec(bios);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000199
Uwe Hermannffec5f32007-08-23 16:08:21 +0000200 return 0;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000201}
202
Ollie Lho184a4042005-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)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000205{
Giampiero Giancipoli8c5299f2006-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;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000209
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000210retry:
Ollie Lho761bf1b2004-03-20 16:46:10 +0000211 /* Issue JEDEC Data Unprotect comand */
Uwe Hermanna7e05482007-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;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000215
Ollie Lho98bea8a2004-12-07 03:15:51 +0000216 /* transfer data from source to destination */
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000217 for (i = start_index; i < page_size; i++) {
Ollie Lho98bea8a2004-12-07 03:15:51 +0000218 /* If the data is 0xFF, don't program it */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000219 if (*src != 0xFF)
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000220 *dst = *src;
221 dst++;
222 src++;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000223 }
224
Ollie Lho761bf1b2004-03-20 16:46:10 +0000225 toggle_ready_jedec(dst - 1);
Ollie Lho98bea8a2004-12-07 03:15:51 +0000226
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000227 dst = d;
228 src = s;
229 ok = 1;
230 for (i = 0; i < page_size; i++) {
Uwe Hermanna7e05482007-05-09 10:17:44 +0000231 if (*dst != *src) {
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000232 ok = 0;
233 break;
234 }
235 dst++;
236 src++;
237 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000238
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000239 if (!ok && tried++ < MAX_REFLASH_TRIES) {
240 start_index = i;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000241 goto retry;
242 }
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000243 if (!ok) {
Uwe Hermanna7e05482007-05-09 10:17:44 +0000244 fprintf(stderr, " page %d failed!\n",
245 (unsigned int)(d - bios) / page_size);
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000246 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000247 return !ok;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000248}
249
Ollie Lho184a4042005-11-26 21:55:36 +0000250int write_byte_program_jedec(volatile uint8_t *bios, uint8_t *src,
251 volatile uint8_t *dst)
Ollie Lho070647d2004-03-22 22:19:17 +0000252{
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000253 int tried = 0, ok = 1;
Ollie Lho1b8b6602004-12-08 02:10:33 +0000254
Ollie Lho98bea8a2004-12-07 03:15:51 +0000255 /* If the data is 0xFF, don't program it */
Ollie Lho070647d2004-03-22 22:19:17 +0000256 if (*src == 0xFF) {
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000257 return -1;
Ollie Lho070647d2004-03-22 22:19:17 +0000258 }
Ollie Lho98bea8a2004-12-07 03:15:51 +0000259
Ollie Lho1b8b6602004-12-08 02:10:33 +0000260retry:
Ollie Lho070647d2004-03-22 22:19:17 +0000261 /* Issue JEDEC Byte Program command */
Uwe Hermanna7e05482007-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;
Ollie Lho98bea8a2004-12-07 03:15:51 +0000265
266 /* transfer data from source to destination */
Ollie Lho070647d2004-03-22 22:19:17 +0000267 *dst = *src;
268 toggle_ready_jedec(bios);
Ollie Lho8b8897a2004-03-27 00:18:15 +0000269
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000270 if (*dst != *src && tried++ < MAX_REFLASH_TRIES) {
Uwe Hermanna7e05482007-05-09 10:17:44 +0000271 goto retry;
272 }
Ollie Lho1b8b6602004-12-08 02:10:33 +0000273
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000274 if (tried >= MAX_REFLASH_TRIES)
Uwe Hermanna7e05482007-05-09 10:17:44 +0000275 ok = 0;
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000276
Uwe Hermannffec5f32007-08-23 16:08:21 +0000277 return !ok;
Ollie Lho070647d2004-03-22 22:19:17 +0000278}
279
Ollie Lho184a4042005-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)
Ollie Lho761bf1b2004-03-20 16:46:10 +0000282{
283 int i;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000284
285 for (i = 0; i < page_size; i++) {
Ollie Lho8b8897a2004-03-27 00:18:15 +0000286 write_byte_program_jedec(bios, src, dst);
287 dst++, src++;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000288 }
289
Uwe Hermannffec5f32007-08-23 16:08:21 +0000290 return 0;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000291}
292
Ollie Lho184a4042005-11-26 21:55:36 +0000293int write_jedec(struct flashchip *flash, uint8_t *buf)
Ollie Lho761bf1b2004-03-20 16:46:10 +0000294{
295 int i;
Ollie Lho070647d2004-03-22 22:19:17 +0000296 int total_size = flash->total_size * 1024;
297 int page_size = flash->page_size;
Stefan Reinauerce532972007-05-23 17:20:56 +0000298 volatile uint8_t *bios = flash->virtual_memory;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000299
300 erase_chip_jedec(flash);
Uwe Hermanna7e05482007-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) {
Uwe Hermanna502dce2007-10-17 23:55:15 +0000304 printf("ERASE FAILED @%d, val %02x!\n", i, bios[i]);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000305 return -1;
306 }
307 }
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000308
Uwe Hermanna502dce2007-10-17 23:55:15 +0000309 printf("Programming page: ");
Ollie Lho761bf1b2004-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);
Ollie Lho8b8897a2004-03-27 00:18:15 +0000312 write_page_write_jedec(bios, buf + i * page_size,
313 bios + i * page_size, page_size);
Ollie Lho070647d2004-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");
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000315 }
316 printf("\n");
Ollie Lho761bf1b2004-03-20 16:46:10 +0000317 protect_jedec(bios);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000318
Uwe Hermannffec5f32007-08-23 16:08:21 +0000319 return 0;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000320}