blob: 46153dd66c40785cdca36545b80e67034e792639 [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>
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +00007 *
Uwe Hermannd1107642007-08-29 17:52:32 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000012 *
Uwe Hermannd1107642007-08-29 17:52:32 +000013 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000017 *
Uwe Hermannd1107642007-08-29 17:52:32 +000018 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000021 */
22
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000023#include <stdio.h>
Ollie Lho184a4042005-11-26 21:55:36 +000024#include <stdint.h>
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000025#include "flash.h"
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000026
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +000027#define MAX_REFLASH_TRIES 0x10
28
Uwe Hermann51582f22007-08-23 10:20:40 +000029void toggle_ready_jedec(volatile uint8_t *dst)
30{
31 unsigned int i = 0;
32 uint8_t tmp1, tmp2;
33
34 tmp1 = *dst & 0x40;
35
36 while (i++ < 0xFFFFFFF) {
37 tmp2 = *dst & 0x40;
38 if (tmp1 == tmp2) {
39 break;
40 }
41 tmp1 = tmp2;
42 }
43}
44
45void data_polling_jedec(volatile uint8_t *dst, uint8_t data)
46{
47 unsigned int i = 0;
48 uint8_t tmp;
49
50 data &= 0x80;
51
52 while (i++ < 0xFFFFFFF) {
53 tmp = *dst & 0x80;
54 if (tmp == data) {
55 break;
56 }
57 }
58}
59
60void unprotect_jedec(volatile uint8_t *bios)
61{
62 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
63 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
64 *(volatile uint8_t *)(bios + 0x5555) = 0x80;
65 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
66 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
67 *(volatile uint8_t *)(bios + 0x5555) = 0x20;
68
69 usleep(200);
70}
71
72void protect_jedec(volatile uint8_t *bios)
73{
74 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
75 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
76 *(volatile uint8_t *)(bios + 0x5555) = 0xA0;
77
78 usleep(200);
79}
80
Ollie Lho761bf1b2004-03-20 16:46:10 +000081int probe_jedec(struct flashchip *flash)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000082{
Stefan Reinauerce532972007-05-23 17:20:56 +000083 volatile uint8_t *bios = flash->virtual_memory;
Ollie Lho184a4042005-11-26 21:55:36 +000084 uint8_t id1, id2;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000085
Ollie Lho761bf1b2004-03-20 16:46:10 +000086 /* Issue JEDEC Product ID Entry command */
Uwe Hermanna7e05482007-05-09 10:17:44 +000087 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
Ollie Lho761bf1b2004-03-20 16:46:10 +000088 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +000089 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
Ollie Lho761bf1b2004-03-20 16:46:10 +000090 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +000091 *(volatile uint8_t *)(bios + 0x5555) = 0x90;
Ollie Lho761bf1b2004-03-20 16:46:10 +000092 myusec_delay(10);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000093
Ollie Lho761bf1b2004-03-20 16:46:10 +000094 /* Read product ID */
Uwe Hermanna7e05482007-05-09 10:17:44 +000095 id1 = *(volatile uint8_t *)bios;
96 id2 = *(volatile uint8_t *)(bios + 0x01);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000097
Ollie Lho761bf1b2004-03-20 16:46:10 +000098 /* Issue JEDEC Product ID Exit command */
Uwe Hermanna7e05482007-05-09 10:17:44 +000099 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000100 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000101 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000102 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000103 *(volatile uint8_t *)(bios + 0x5555) = 0xF0;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000104 myusec_delay(10);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000105
Ollie Lho184a4042005-11-26 21:55:36 +0000106 printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000107 if (id1 == flash->manufacture_id && id2 == flash->model_id)
108 return 1;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000109
Ollie Lho761bf1b2004-03-20 16:46:10 +0000110 return 0;
Ollie Lho73eca802004-03-19 22:10:07 +0000111}
112
Ollie Lho184a4042005-11-26 21:55:36 +0000113int erase_sector_jedec(volatile uint8_t *bios, unsigned int page)
Ollie Lho73eca802004-03-19 22:10:07 +0000114{
Ollie Lho761bf1b2004-03-20 16:46:10 +0000115 /* Issue the Sector Erase command */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000116 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
Ollie Lho73eca802004-03-19 22:10:07 +0000117 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000118 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
Ollie Lho73eca802004-03-19 22:10:07 +0000119 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000120 *(volatile uint8_t *)(bios + 0x5555) = 0x80;
Ollie Lho73eca802004-03-19 22:10:07 +0000121 myusec_delay(10);
Ollie Lhoefa28582004-12-08 20:10:01 +0000122
Uwe Hermanna7e05482007-05-09 10:17:44 +0000123 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
Ollie Lho73eca802004-03-19 22:10:07 +0000124 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000125 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
Ollie Lho73eca802004-03-19 22:10:07 +0000126 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000127 *(volatile uint8_t *)(bios + page) = 0x30;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000128 myusec_delay(10);
129
Ollie Lho73eca802004-03-19 22:10:07 +0000130 /* wait for Toggle bit ready */
131 toggle_ready_jedec(bios);
132
Uwe Hermannffec5f32007-08-23 16:08:21 +0000133 return 0;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000134}
Ollie Lho98bea8a2004-12-07 03:15:51 +0000135
Ollie Lho184a4042005-11-26 21:55:36 +0000136int erase_block_jedec(volatile uint8_t *bios, unsigned int block)
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000137{
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000138 /* Issue the Sector Erase command */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000139 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000140 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000141 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000142 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000143 *(volatile uint8_t *)(bios + 0x5555) = 0x80;
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000144 myusec_delay(10);
Ollie Lhoefa28582004-12-08 20:10:01 +0000145
Uwe Hermanna7e05482007-05-09 10:17:44 +0000146 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000147 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000148 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000149 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000150 *(volatile uint8_t *)(bios + block) = 0x50;
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000151 myusec_delay(10);
152
153 /* wait for Toggle bit ready */
154 toggle_ready_jedec(bios);
155
Uwe Hermannffec5f32007-08-23 16:08:21 +0000156 return 0;
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000157}
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000158
Ollie Lho761bf1b2004-03-20 16:46:10 +0000159int erase_chip_jedec(struct flashchip *flash)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000160{
Stefan Reinauerce532972007-05-23 17:20:56 +0000161 volatile uint8_t *bios = flash->virtual_memory;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000162
Ollie Lho761bf1b2004-03-20 16:46:10 +0000163 /* Issue the JEDEC Chip Erase command */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000164 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000165 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000166 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
Ollie Lho73eca802004-03-19 22:10:07 +0000167 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000168 *(volatile uint8_t *)(bios + 0x5555) = 0x80;
Ollie Lho73eca802004-03-19 22:10:07 +0000169 myusec_delay(10);
Ollie Lhoefa28582004-12-08 20:10:01 +0000170
Uwe Hermanna7e05482007-05-09 10:17:44 +0000171 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
Ollie Lho73eca802004-03-19 22:10:07 +0000172 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000173 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
Ollie Lho73eca802004-03-19 22:10:07 +0000174 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000175 *(volatile uint8_t *)(bios + 0x5555) = 0x10;
Ollie Lho73eca802004-03-19 22:10:07 +0000176 myusec_delay(10);
177
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000178 toggle_ready_jedec(bios);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000179
Uwe Hermannffec5f32007-08-23 16:08:21 +0000180 return 0;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000181}
182
Ollie Lho184a4042005-11-26 21:55:36 +0000183int write_page_write_jedec(volatile uint8_t *bios, uint8_t *src,
184 volatile uint8_t *dst, int page_size)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000185{
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000186 int i, tried = 0, start_index = 0, ok;
187 volatile uint8_t *d = dst;
188 uint8_t *s = src;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000189
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000190retry:
Ollie Lho761bf1b2004-03-20 16:46:10 +0000191 /* Issue JEDEC Data Unprotect comand */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000192 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
193 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
194 *(volatile uint8_t *)(bios + 0x5555) = 0xA0;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000195
Ollie Lho98bea8a2004-12-07 03:15:51 +0000196 /* transfer data from source to destination */
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000197 for (i = start_index; i < page_size; i++) {
Ollie Lho98bea8a2004-12-07 03:15:51 +0000198 /* If the data is 0xFF, don't program it */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000199 if (*src != 0xFF)
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000200 *dst = *src;
201 dst++;
202 src++;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000203 }
204
Ollie Lho761bf1b2004-03-20 16:46:10 +0000205 toggle_ready_jedec(dst - 1);
Ollie Lho98bea8a2004-12-07 03:15:51 +0000206
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000207 dst = d;
208 src = s;
209 ok = 1;
210 for (i = 0; i < page_size; i++) {
Uwe Hermanna7e05482007-05-09 10:17:44 +0000211 if (*dst != *src) {
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000212 ok = 0;
213 break;
214 }
215 dst++;
216 src++;
217 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000218
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000219 if (!ok && tried++ < MAX_REFLASH_TRIES) {
220 start_index = i;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000221 goto retry;
222 }
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000223 if (!ok) {
Uwe Hermanna7e05482007-05-09 10:17:44 +0000224 fprintf(stderr, " page %d failed!\n",
225 (unsigned int)(d - bios) / page_size);
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000226 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000227 return !ok;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000228}
229
Ollie Lho184a4042005-11-26 21:55:36 +0000230int write_byte_program_jedec(volatile uint8_t *bios, uint8_t *src,
231 volatile uint8_t *dst)
Ollie Lho070647d2004-03-22 22:19:17 +0000232{
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000233 int tried = 0, ok = 1;
Ollie Lho1b8b6602004-12-08 02:10:33 +0000234
Ollie Lho98bea8a2004-12-07 03:15:51 +0000235 /* If the data is 0xFF, don't program it */
Ollie Lho070647d2004-03-22 22:19:17 +0000236 if (*src == 0xFF) {
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000237 return -1;
Ollie Lho070647d2004-03-22 22:19:17 +0000238 }
Ollie Lho98bea8a2004-12-07 03:15:51 +0000239
Ollie Lho1b8b6602004-12-08 02:10:33 +0000240retry:
Ollie Lho070647d2004-03-22 22:19:17 +0000241 /* Issue JEDEC Byte Program command */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000242 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
243 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
244 *(volatile uint8_t *)(bios + 0x5555) = 0xA0;
Ollie Lho98bea8a2004-12-07 03:15:51 +0000245
246 /* transfer data from source to destination */
Ollie Lho070647d2004-03-22 22:19:17 +0000247 *dst = *src;
248 toggle_ready_jedec(bios);
Ollie Lho8b8897a2004-03-27 00:18:15 +0000249
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000250 if (*dst != *src && tried++ < MAX_REFLASH_TRIES) {
Uwe Hermanna7e05482007-05-09 10:17:44 +0000251 goto retry;
252 }
Ollie Lho1b8b6602004-12-08 02:10:33 +0000253
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000254 if (tried >= MAX_REFLASH_TRIES)
Uwe Hermanna7e05482007-05-09 10:17:44 +0000255 ok = 0;
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000256
Uwe Hermannffec5f32007-08-23 16:08:21 +0000257 return !ok;
Ollie Lho070647d2004-03-22 22:19:17 +0000258}
259
Ollie Lho184a4042005-11-26 21:55:36 +0000260int write_sector_jedec(volatile uint8_t *bios, uint8_t *src,
261 volatile uint8_t *dst, unsigned int page_size)
Ollie Lho761bf1b2004-03-20 16:46:10 +0000262{
263 int i;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000264
265 for (i = 0; i < page_size; i++) {
Ollie Lho8b8897a2004-03-27 00:18:15 +0000266 write_byte_program_jedec(bios, src, dst);
267 dst++, src++;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000268 }
269
Uwe Hermannffec5f32007-08-23 16:08:21 +0000270 return 0;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000271}
272
Ollie Lho184a4042005-11-26 21:55:36 +0000273int write_jedec(struct flashchip *flash, uint8_t *buf)
Ollie Lho761bf1b2004-03-20 16:46:10 +0000274{
275 int i;
Ollie Lho070647d2004-03-22 22:19:17 +0000276 int total_size = flash->total_size * 1024;
277 int page_size = flash->page_size;
Stefan Reinauerce532972007-05-23 17:20:56 +0000278 volatile uint8_t *bios = flash->virtual_memory;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000279
280 erase_chip_jedec(flash);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000281 // dumb check if erase was successful.
282 for (i = 0; i < total_size; i++) {
283 if (bios[i] != (uint8_t) 0xff) {
Uwe Hermanna502dce2007-10-17 23:55:15 +0000284 printf("ERASE FAILED @%d, val %02x!\n", i, bios[i]);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000285 return -1;
286 }
287 }
Giampiero Giancipoli8c5299f2006-11-22 00:29:51 +0000288
Uwe Hermanna502dce2007-10-17 23:55:15 +0000289 printf("Programming page: ");
Ollie Lho761bf1b2004-03-20 16:46:10 +0000290 for (i = 0; i < total_size / page_size; i++) {
291 printf("%04d at address: 0x%08x", i, i * page_size);
Ollie Lho8b8897a2004-03-27 00:18:15 +0000292 write_page_write_jedec(bios, buf + i * page_size,
293 bios + i * page_size, page_size);
Ollie Lho070647d2004-03-22 22:19:17 +0000294 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 +0000295 }
296 printf("\n");
Ollie Lho761bf1b2004-03-20 16:46:10 +0000297 protect_jedec(bios);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000298
Uwe Hermannffec5f32007-08-23 16:08:21 +0000299 return 0;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000300}